Repository navigation
Expand file tree
/
Copy pathgraphify.js
More file actions
80 lines (75 loc) · 3.77 KB
/
Copy pathgraphify.js
File metadata and controls
80 lines (75 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// graphify OpenCode plugin, adapted from `graphify opencode install`.
//
// Appends a one-time knowledge-graph reminder to the output of the first bash call in each
// session. The command itself is never modified, so the plugin behaves the same under any shell
// OpenCode uses (bash, zsh, Windows PowerShell 5.1, PowerShell 7) and cannot change what runs.
// Rerunning `graphify opencode install` restores upstream's command-prefixing version; keep this one.
import { existsSync } from "fs";
import { join } from "path";
// Quote the path as one literal shell argument, or return null when no quoting
// is correct everywhere. The plugin API reports only directory and worktree and
// the repository pins no shell, so nothing here may assume one: a Windows host
// can run Bash, a POSIX host can run PowerShell, and cmd.exe does not treat
// single quotes as quoting at all.
//
// 1. On Windows a backslash is a path separator and forward slashes are
// accepted by every Windows shell, so normalise first. On POSIX a backslash
// is a legal filename character and must be left alone.
// 2. A path with no shell metacharacter then needs no quoting at all.
// 3. Double quotes group in POSIX shells, PowerShell and cmd.exe alike, and
// inside them POSIX shells and PowerShell treat every character literally.
// The other two shells do not: cmd.exe still expands %VAR% and, with
// delayed expansion enabled, !VAR! inside double quotes, and interactive
// bash performs history expansion on !. A path free of " $ ` \ % and ! is
// therefore safe to double-quote, which covers spaces and apostrophes.
// 4. A path containing one of those has no portable form: it needs POSIX
// '\'' or PowerShell '' for an apostrophe, and cmd.exe cannot group it
// and would expand the variables anyway. Rather than emit a command that is
// wrong in some shell, the caller drops the runnable example.
const SHELL_SAFE_PATH = /^[A-Za-z0-9_@+=:,./-]+$/;
const DOUBLE_QUOTE_UNSAFE = /["$`\\%!]/;
const shellQuote = (path) => {
const normalised =
process.platform === "win32" ? path.replace(/\\/g, "/") : path;
if (SHELL_SAFE_PATH.test(normalised)) return normalised;
if (!DOUBLE_QUOTE_UNSAFE.test(normalised)) return `"${normalised}"`;
return null;
};
const reminder = (graph) => {
const quoted = shellQuote(graph);
if (!quoted) {
return (
`[graphify] knowledge graph at ${graph}. Its path contains a character no ` +
"shell quotes the same way, so no runnable scoped example is offered here; " +
"read GRAPH_REPORT.md instead of grepping raw files."
);
}
return (
`[graphify] knowledge graph at ${graph}. For focused questions, run ` +
`graphify query "<question>" --graph ${quoted} (scoped subgraph, usually much smaller than ` +
"GRAPH_REPORT.md) instead of grepping raw files. Read GRAPH_REPORT.md next to it only for " +
"broad architecture context."
);
};
export const GraphifyPlugin = async ({ directory, worktree }) => {
const reminded = new Set();
// Sessions can start below the repository root, so prefer the worktree root's graph, and give
// the agent its absolute path so the suggested command works from any working directory.
const findGraph = () =>
[worktree, directory]
.filter(Boolean)
.map((d) => join(d, "graphify-out", "graph.json"))
.find((p) => existsSync(p));
return {
"tool.execute.after": async (input, output) => {
if (input.tool !== "bash" || reminded.has(input.sessionID)) return;
const graph = findGraph();
if (!graph) return;
reminded.add(input.sessionID);
output.output = `${output.output ?? ""}\n\n${reminder(graph)}`;
},
event: async ({ event }) => {
if (event.type === "session.deleted") reminded.delete(event.properties?.info?.id);
},
};
};