graphify.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // graphify OpenCode plugin
  2. // Injects a knowledge graph reminder before bash tool calls when the graph exists.
  3. import { existsSync } from "fs";
  4. import { join, relative } from "path";
  5. /** graph.json 可能出现的目录(相对仓库根);CRM 主前端图谱通常在 crm/web/graphify-out */
  6. function findGraphifyOutRoot(workspaceRoot) {
  7. const roots = [
  8. join(workspaceRoot, "graphify-out"),
  9. join(workspaceRoot, "crm", "web", "graphify-out"),
  10. join(workspaceRoot, "crm", "graphify-out"),
  11. join(workspaceRoot, "crm", "backend", "graphify-out"),
  12. ];
  13. for (const root of roots) {
  14. if (existsSync(join(root, "graph.json"))) return root;
  15. }
  16. return null;
  17. }
  18. export const GraphifyPlugin = async ({ directory }) => {
  19. let reminded = false;
  20. return {
  21. "tool.execute.before": async (input, output) => {
  22. if (reminded) return;
  23. const graphifyRoot = findGraphifyOutRoot(directory);
  24. if (!graphifyRoot) return;
  25. if (input.tool === "bash") {
  26. const rel =
  27. relative(directory, graphifyRoot) || "graphify-out";
  28. output.args.command =
  29. `echo "[graphify] Knowledge graph found at ${rel}/. Read ${rel}/GRAPH_REPORT.md for context before wide file search." && ` +
  30. output.args.command;
  31. reminded = true;
  32. }
  33. },
  34. };
  35. };