-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.ts
More file actions
86 lines (74 loc) · 2.92 KB
/
Copy pathopencode.ts
File metadata and controls
86 lines (74 loc) · 2.92 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
81
82
83
84
85
86
import { detectOpenCode } from "./detect";
import { resolveCommitlyArgv, type InstallOutcome, type InstallScope } from "./install";
const PLUGIN_VERSION = 1;
const MARKER = `commitly-plugin-version: ${PLUGIN_VERSION}`;
function renderPlugin(commitlyArgv: string[]): string {
return `// AUTO-GENERATED by \`commitly init\` — do not edit by hand.
// Re-run \`commitly init --force --tools opencode\` to regenerate.
// ${MARKER}
//
// OpenCode has no dedicated session-close hook analogous to Claude Code's
// SessionEnd — session.deleted is the closest thing (not guaranteed to fire
// on abrupt exit) and is treated as an authoritative close by
// src/ingest/normalize.opencode.ts. session.idle fires per-turn and serves
// as the idle-gap heartbeat, same role as Claude Code's Stop / Codex's Stop.
const COMMITLY_ARGV = ${JSON.stringify(commitlyArgv)};
const TRACKED_EVENTS = new Set(["session.created", "session.idle", "session.deleted"]);
function extractSessionId(event) {
return (
event?.properties?.sessionID ??
event?.properties?.info?.id ??
event?.properties?.id ??
event?.sessionID ??
null
);
}
export const CommitlyPlugin = async ({ directory }) => {
return {
event: async ({ event }) => {
if (!event || !TRACKED_EVENTS.has(event.type)) return;
const sessionId = extractSessionId(event);
if (!sessionId) return;
const payload = { sessionId, cwd: directory ?? process.cwd() };
const base64Payload = Buffer.from(JSON.stringify(payload), "utf8").toString("base64");
try {
// Bun.spawn with an explicit argv array — deliberately not a shell
// template — so there is zero ambiguity tokenizing a multi-word
// COMMITLY_ARGV like ["bun", "/abs/path/cli.ts"].
const proc = Bun.spawn({
cmd: [...COMMITLY_ARGV, "hook", "opencode", event.type, base64Payload],
stdin: "ignore",
stdout: "ignore",
stderr: "ignore",
});
await proc.exited;
} catch {
// Commitly must never be able to break an OpenCode session.
}
},
};
};
export default CommitlyPlugin;
`;
}
export async function installOpenCodeHooks(
scope: InstallScope,
cwd: string,
opts: { force?: boolean } = {},
): Promise<InstallOutcome> {
const detection = await detectOpenCode(cwd);
const configPath = scope === "user" ? detection.userConfigPath : detection.projectConfigPath;
const file = Bun.file(configPath);
const alreadyInstalled = (await file.exists()) && (await file.text()).includes(MARKER);
if (alreadyInstalled && !opts.force) {
return { tool: "opencode", scope, configPath, events: [{ event: "plugin", status: "already-present" }] };
}
const commitlyArgv = await resolveCommitlyArgv();
await Bun.write(configPath, renderPlugin(commitlyArgv));
return {
tool: "opencode",
scope,
configPath,
events: [{ event: "plugin", status: alreadyInstalled ? "replaced" : "installed" }],
};
}