graphify.js 1.4 KB

123456789101112131415161718192021222324252627282930
  1. // graphify OpenCode plugin
  2. // Injects a knowledge graph reminder before bash tool calls when the graph exists.
  3. //
  4. // IMPORTANT: keep the reminder string free of backticks and $(...) constructs.
  5. // The hook prepends `echo "<reminder>" && <cmd>` to the user's bash command;
  6. // backticks inside the double-quoted echo trigger bash command substitution,
  7. // which both corrupts tool output and silently executes the very graphify
  8. // command we are only suggesting. Plain words render fine in opencode's TUI.
  9. import { existsSync } from "fs";
  10. import { join } from "path";
  11. export const GraphifyPlugin = async ({ directory }) => {
  12. let reminded = false;
  13. return {
  14. "tool.execute.before": async (input, output) => {
  15. if (reminded) return;
  16. if (!existsSync(join(directory, "graphify-out", "graph.json"))) return;
  17. if (input.tool === "bash") {
  18. // ';' not '&&' — Windows PowerShell 5.1 rejects '&&' as a statement
  19. // separator, breaking the first bash command of the session (#1646).
  20. output.args.command =
  21. 'echo "[graphify] knowledge graph at graphify-out/. For focused questions, run graphify query with your question (scoped subgraph, usually much smaller than GRAPH_REPORT.md) instead of grepping raw files. Read GRAPH_REPORT.md only for broad architecture context." ; ' +
  22. output.args.command;
  23. reminded = true;
  24. }
  25. },
  26. };
  27. };