context-mode 0.9.21 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/hooks/hooks.json +46 -4
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +4 -4
- package/README.md +377 -191
- package/build/adapters/claude-code/config.d.ts +8 -0
- package/build/adapters/claude-code/config.js +8 -0
- package/build/adapters/claude-code/hooks.d.ts +53 -0
- package/build/adapters/claude-code/hooks.js +88 -0
- package/build/adapters/claude-code/index.d.ts +50 -0
- package/build/adapters/claude-code/index.js +523 -0
- package/build/adapters/codex/config.d.ts +8 -0
- package/build/adapters/codex/config.js +8 -0
- package/build/adapters/codex/hooks.d.ts +21 -0
- package/build/adapters/codex/hooks.js +27 -0
- package/build/adapters/codex/index.d.ts +44 -0
- package/build/adapters/codex/index.js +223 -0
- package/build/adapters/detect.d.ts +26 -0
- package/build/adapters/detect.js +131 -0
- package/build/adapters/gemini-cli/config.d.ts +8 -0
- package/build/adapters/gemini-cli/config.js +8 -0
- package/build/adapters/gemini-cli/hooks.d.ts +44 -0
- package/build/adapters/gemini-cli/hooks.js +64 -0
- package/build/adapters/gemini-cli/index.d.ts +57 -0
- package/build/adapters/gemini-cli/index.js +468 -0
- package/build/adapters/opencode/config.d.ts +8 -0
- package/build/adapters/opencode/config.js +8 -0
- package/build/adapters/opencode/hooks.d.ts +38 -0
- package/build/adapters/opencode/hooks.js +50 -0
- package/build/adapters/opencode/index.d.ts +52 -0
- package/build/adapters/opencode/index.js +386 -0
- package/build/adapters/types.d.ts +218 -0
- package/build/adapters/types.js +13 -0
- package/build/adapters/vscode-copilot/config.d.ts +8 -0
- package/build/adapters/vscode-copilot/config.js +8 -0
- package/build/adapters/vscode-copilot/hooks.d.ts +49 -0
- package/build/adapters/vscode-copilot/hooks.js +76 -0
- package/build/adapters/vscode-copilot/index.d.ts +58 -0
- package/build/adapters/vscode-copilot/index.js +512 -0
- package/build/cli.d.ts +9 -6
- package/build/cli.js +133 -423
- package/build/db-base.d.ts +84 -0
- package/build/db-base.js +128 -0
- package/build/executor.d.ts +6 -7
- package/build/executor.js +111 -51
- package/build/opencode-plugin.d.ts +37 -0
- package/build/opencode-plugin.js +118 -0
- package/build/runtime.js +1 -1
- package/build/server.js +436 -117
- package/build/session/db.d.ts +110 -0
- package/build/session/db.js +285 -0
- package/build/session/extract.d.ts +51 -0
- package/build/session/extract.js +407 -0
- package/build/session/snapshot.d.ts +70 -0
- package/build/session/snapshot.js +309 -0
- package/build/store.d.ts +4 -22
- package/build/store.js +67 -55
- package/build/truncate.d.ts +59 -0
- package/build/truncate.js +157 -0
- package/build/types.d.ts +101 -0
- package/build/types.js +20 -0
- package/configs/claude-code/CLAUDE.md +62 -0
- package/configs/codex/AGENTS.md +58 -0
- package/configs/codex/config.toml +5 -0
- package/configs/gemini-cli/GEMINI.md +58 -0
- package/configs/gemini-cli/mcp.json +7 -0
- package/configs/gemini-cli/settings.json +49 -0
- package/configs/opencode/AGENTS.md +58 -0
- package/configs/opencode/opencode.json +10 -0
- package/configs/vscode-copilot/copilot-instructions.md +58 -0
- package/configs/vscode-copilot/hooks.json +16 -0
- package/configs/vscode-copilot/mcp.json +8 -0
- package/hooks/core/formatters.mjs +86 -0
- package/hooks/core/routing.mjs +262 -0
- package/hooks/core/stdin.mjs +19 -0
- package/hooks/formatters/claude-code.mjs +57 -0
- package/hooks/formatters/gemini-cli.mjs +55 -0
- package/hooks/formatters/vscode-copilot.mjs +55 -0
- package/hooks/gemini-cli/aftertool.mjs +58 -0
- package/hooks/gemini-cli/beforetool.mjs +25 -0
- package/hooks/gemini-cli/precompress.mjs +51 -0
- package/hooks/gemini-cli/sessionstart.mjs +117 -0
- package/hooks/hooks.json +46 -4
- package/hooks/posttooluse.mjs +53 -0
- package/hooks/precompact.mjs +55 -0
- package/hooks/pretooluse.mjs +23 -266
- package/hooks/routing-block.mjs +19 -6
- package/hooks/session-directive.mjs +353 -0
- package/hooks/session-helpers.mjs +112 -0
- package/hooks/sessionstart.mjs +123 -16
- package/hooks/userpromptsubmit.mjs +58 -0
- package/hooks/vscode-copilot/posttooluse.mjs +58 -0
- package/hooks/vscode-copilot/precompact.mjs +51 -0
- package/hooks/vscode-copilot/pretooluse.mjs +25 -0
- package/hooks/vscode-copilot/sessionstart.mjs +115 -0
- package/package.json +20 -17
- package/skills/context-mode/SKILL.md +49 -49
- package/skills/{doctor → ctx-doctor}/SKILL.md +3 -3
- package/skills/{stats → ctx-stats}/SKILL.md +3 -3
- package/skills/{upgrade → ctx-upgrade}/SKILL.md +3 -3
- package/start.mjs +47 -0
- package/hooks/pretooluse.sh +0 -147
- package/server.bundle.mjs +0 -341
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/gemini-cli — Gemini CLI platform adapter.
|
|
3
|
+
*
|
|
4
|
+
* Implements HookAdapter for Gemini CLI's JSON stdin/stdout hook paradigm.
|
|
5
|
+
*
|
|
6
|
+
* Gemini CLI hook specifics:
|
|
7
|
+
* - I/O: JSON on stdin, JSON on stdout (same paradigm as Claude Code)
|
|
8
|
+
* - Hook names: BeforeTool, AfterTool, PreCompress, SessionStart
|
|
9
|
+
* - Arg modification: `hookSpecificOutput.tool_input` (merged with original)
|
|
10
|
+
* - Blocking: `decision: "deny"` in response (NOT permissionDecision)
|
|
11
|
+
* - Output modification: `decision: "deny"` + reason replaces output,
|
|
12
|
+
* `hookSpecificOutput.additionalContext` appends
|
|
13
|
+
* - PreCompress: advisory only (async, cannot block)
|
|
14
|
+
* - No `decision: "ask"` support
|
|
15
|
+
* - Hooks don't fire for subagents yet
|
|
16
|
+
* - Config: ~/.gemini/settings.json (user), .gemini/settings.json (project)
|
|
17
|
+
* - Session ID: session_id field
|
|
18
|
+
* - Project dir env: GEMINI_PROJECT_DIR (also CLAUDE_PROJECT_DIR alias)
|
|
19
|
+
* - Session dir: ~/.gemini/context-mode/sessions/
|
|
20
|
+
*/
|
|
21
|
+
import { createHash } from "node:crypto";
|
|
22
|
+
import { readFileSync, writeFileSync, mkdirSync, copyFileSync, accessSync, chmodSync, constants, } from "node:fs";
|
|
23
|
+
import { resolve, join } from "node:path";
|
|
24
|
+
import { homedir } from "node:os";
|
|
25
|
+
// ─────────────────────────────────────────────────────────
|
|
26
|
+
// Hook constants (re-exported from hooks.ts)
|
|
27
|
+
// ─────────────────────────────────────────────────────────
|
|
28
|
+
import { HOOK_TYPES as GEMINI_HOOK_NAMES, HOOK_SCRIPTS as GEMINI_HOOK_SCRIPTS, } from "./hooks.js";
|
|
29
|
+
// ─────────────────────────────────────────────────────────
|
|
30
|
+
// Adapter implementation
|
|
31
|
+
// ─────────────────────────────────────────────────────────
|
|
32
|
+
export class GeminiCLIAdapter {
|
|
33
|
+
name = "Gemini CLI";
|
|
34
|
+
paradigm = "json-stdio";
|
|
35
|
+
capabilities = {
|
|
36
|
+
preToolUse: true,
|
|
37
|
+
postToolUse: true,
|
|
38
|
+
preCompact: true,
|
|
39
|
+
sessionStart: true,
|
|
40
|
+
canModifyArgs: true,
|
|
41
|
+
canModifyOutput: true,
|
|
42
|
+
canInjectSessionContext: true,
|
|
43
|
+
};
|
|
44
|
+
// ── Input parsing ──────────────────────────────────────
|
|
45
|
+
parsePreToolUseInput(raw) {
|
|
46
|
+
const input = raw;
|
|
47
|
+
return {
|
|
48
|
+
toolName: input.tool_name ?? "",
|
|
49
|
+
toolInput: input.tool_input ?? {},
|
|
50
|
+
sessionId: this.extractSessionId(input),
|
|
51
|
+
projectDir: this.getProjectDir(),
|
|
52
|
+
raw,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
parsePostToolUseInput(raw) {
|
|
56
|
+
const input = raw;
|
|
57
|
+
return {
|
|
58
|
+
toolName: input.tool_name ?? "",
|
|
59
|
+
toolInput: input.tool_input ?? {},
|
|
60
|
+
toolOutput: input.tool_output,
|
|
61
|
+
isError: input.is_error,
|
|
62
|
+
sessionId: this.extractSessionId(input),
|
|
63
|
+
projectDir: this.getProjectDir(),
|
|
64
|
+
raw,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
parsePreCompactInput(raw) {
|
|
68
|
+
const input = raw;
|
|
69
|
+
return {
|
|
70
|
+
sessionId: this.extractSessionId(input),
|
|
71
|
+
projectDir: this.getProjectDir(),
|
|
72
|
+
raw,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
parseSessionStartInput(raw) {
|
|
76
|
+
const input = raw;
|
|
77
|
+
const rawSource = input.source ?? "startup";
|
|
78
|
+
let source;
|
|
79
|
+
switch (rawSource) {
|
|
80
|
+
case "compact":
|
|
81
|
+
source = "compact";
|
|
82
|
+
break;
|
|
83
|
+
case "resume":
|
|
84
|
+
source = "resume";
|
|
85
|
+
break;
|
|
86
|
+
case "clear":
|
|
87
|
+
source = "clear";
|
|
88
|
+
break;
|
|
89
|
+
default:
|
|
90
|
+
source = "startup";
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
sessionId: this.extractSessionId(input),
|
|
94
|
+
source,
|
|
95
|
+
projectDir: this.getProjectDir(),
|
|
96
|
+
raw,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// ── Response formatting ────────────────────────────────
|
|
100
|
+
formatPreToolUseResponse(response) {
|
|
101
|
+
if (response.decision === "deny") {
|
|
102
|
+
return {
|
|
103
|
+
decision: "deny",
|
|
104
|
+
reason: response.reason ?? "Blocked by context-mode hook",
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (response.decision === "modify" && response.updatedInput) {
|
|
108
|
+
return {
|
|
109
|
+
hookSpecificOutput: {
|
|
110
|
+
tool_input: response.updatedInput,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (response.decision === "context" && response.additionalContext) {
|
|
115
|
+
// Gemini CLI: inject additionalContext via hookSpecificOutput
|
|
116
|
+
return {
|
|
117
|
+
hookSpecificOutput: {
|
|
118
|
+
additionalContext: response.additionalContext,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (response.decision === "ask") {
|
|
123
|
+
// Gemini CLI: no native "ask" — deny to be safe
|
|
124
|
+
return {
|
|
125
|
+
decision: "deny",
|
|
126
|
+
reason: response.reason ?? "Action requires user confirmation (security policy)",
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// "allow" — return undefined for passthrough
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
formatPostToolUseResponse(response) {
|
|
133
|
+
if (response.updatedOutput) {
|
|
134
|
+
// Gemini CLI: decision "deny" + reason replaces output
|
|
135
|
+
return {
|
|
136
|
+
decision: "deny",
|
|
137
|
+
reason: response.updatedOutput,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (response.additionalContext) {
|
|
141
|
+
return {
|
|
142
|
+
hookSpecificOutput: {
|
|
143
|
+
additionalContext: response.additionalContext,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
formatPreCompactResponse(response) {
|
|
150
|
+
// PreCompress is advisory only (async), but we can still return context
|
|
151
|
+
return response.context ?? "";
|
|
152
|
+
}
|
|
153
|
+
formatSessionStartResponse(response) {
|
|
154
|
+
return response.context ?? "";
|
|
155
|
+
}
|
|
156
|
+
// ── Configuration ──────────────────────────────────────
|
|
157
|
+
getSettingsPath() {
|
|
158
|
+
return resolve(homedir(), ".gemini", "settings.json");
|
|
159
|
+
}
|
|
160
|
+
getSessionDir() {
|
|
161
|
+
const dir = join(homedir(), ".gemini", "context-mode", "sessions");
|
|
162
|
+
mkdirSync(dir, { recursive: true });
|
|
163
|
+
return dir;
|
|
164
|
+
}
|
|
165
|
+
getSessionDBPath(projectDir) {
|
|
166
|
+
const hash = createHash("sha256")
|
|
167
|
+
.update(projectDir)
|
|
168
|
+
.digest("hex")
|
|
169
|
+
.slice(0, 16);
|
|
170
|
+
return join(this.getSessionDir(), `${hash}.db`);
|
|
171
|
+
}
|
|
172
|
+
getSessionEventsPath(projectDir) {
|
|
173
|
+
const hash = createHash("sha256")
|
|
174
|
+
.update(projectDir)
|
|
175
|
+
.digest("hex")
|
|
176
|
+
.slice(0, 16);
|
|
177
|
+
return join(this.getSessionDir(), `${hash}-events.md`);
|
|
178
|
+
}
|
|
179
|
+
generateHookConfig(_pluginRoot) {
|
|
180
|
+
return {
|
|
181
|
+
[GEMINI_HOOK_NAMES.BEFORE_TOOL]: [
|
|
182
|
+
{
|
|
183
|
+
matcher: "",
|
|
184
|
+
hooks: [
|
|
185
|
+
{
|
|
186
|
+
type: "command",
|
|
187
|
+
command: `context-mode hook gemini-cli ${GEMINI_HOOK_NAMES.BEFORE_TOOL.toLowerCase()}`,
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
[GEMINI_HOOK_NAMES.AFTER_TOOL]: [
|
|
193
|
+
{
|
|
194
|
+
matcher: "",
|
|
195
|
+
hooks: [
|
|
196
|
+
{
|
|
197
|
+
type: "command",
|
|
198
|
+
command: `context-mode hook gemini-cli ${GEMINI_HOOK_NAMES.AFTER_TOOL.toLowerCase()}`,
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
[GEMINI_HOOK_NAMES.PRE_COMPRESS]: [
|
|
204
|
+
{
|
|
205
|
+
matcher: "",
|
|
206
|
+
hooks: [
|
|
207
|
+
{
|
|
208
|
+
type: "command",
|
|
209
|
+
command: `context-mode hook gemini-cli ${GEMINI_HOOK_NAMES.PRE_COMPRESS.toLowerCase()}`,
|
|
210
|
+
},
|
|
211
|
+
],
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
[GEMINI_HOOK_NAMES.SESSION_START]: [
|
|
215
|
+
{
|
|
216
|
+
matcher: "",
|
|
217
|
+
hooks: [
|
|
218
|
+
{
|
|
219
|
+
type: "command",
|
|
220
|
+
command: `context-mode hook gemini-cli ${GEMINI_HOOK_NAMES.SESSION_START.toLowerCase()}`,
|
|
221
|
+
},
|
|
222
|
+
],
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
readSettings() {
|
|
228
|
+
try {
|
|
229
|
+
const raw = readFileSync(this.getSettingsPath(), "utf-8");
|
|
230
|
+
return JSON.parse(raw);
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
writeSettings(settings) {
|
|
237
|
+
const dir = resolve(homedir(), ".gemini");
|
|
238
|
+
mkdirSync(dir, { recursive: true });
|
|
239
|
+
writeFileSync(this.getSettingsPath(), JSON.stringify(settings, null, 2) + "\n", "utf-8");
|
|
240
|
+
}
|
|
241
|
+
// ── Diagnostics (doctor) ─────────────────────────────────
|
|
242
|
+
validateHooks(pluginRoot) {
|
|
243
|
+
const results = [];
|
|
244
|
+
const settings = this.readSettings();
|
|
245
|
+
if (!settings) {
|
|
246
|
+
results.push({
|
|
247
|
+
check: "BeforeTool hook",
|
|
248
|
+
status: "fail",
|
|
249
|
+
message: "Could not read ~/.gemini/settings.json",
|
|
250
|
+
fix: "context-mode upgrade",
|
|
251
|
+
});
|
|
252
|
+
return results;
|
|
253
|
+
}
|
|
254
|
+
const hooks = settings.hooks;
|
|
255
|
+
// Check BeforeTool
|
|
256
|
+
const beforeTool = hooks?.[GEMINI_HOOK_NAMES.BEFORE_TOOL];
|
|
257
|
+
if (beforeTool && beforeTool.length > 0) {
|
|
258
|
+
const hasHook = beforeTool.some((entry) => entry.hooks?.some((h) => h.command?.includes("context-mode")));
|
|
259
|
+
results.push({
|
|
260
|
+
check: "BeforeTool hook",
|
|
261
|
+
status: hasHook ? "pass" : "fail",
|
|
262
|
+
message: hasHook
|
|
263
|
+
? "BeforeTool hook configured"
|
|
264
|
+
: "BeforeTool exists but does not point to context-mode",
|
|
265
|
+
fix: hasHook ? undefined : "context-mode upgrade",
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
results.push({
|
|
270
|
+
check: "BeforeTool hook",
|
|
271
|
+
status: "fail",
|
|
272
|
+
message: "No BeforeTool hooks found",
|
|
273
|
+
fix: "context-mode upgrade",
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
// Check SessionStart
|
|
277
|
+
const sessionStart = hooks?.[GEMINI_HOOK_NAMES.SESSION_START];
|
|
278
|
+
if (sessionStart && sessionStart.length > 0) {
|
|
279
|
+
const hasHook = sessionStart.some((entry) => entry.hooks?.some((h) => h.command?.includes("context-mode")));
|
|
280
|
+
results.push({
|
|
281
|
+
check: "SessionStart hook",
|
|
282
|
+
status: hasHook ? "pass" : "fail",
|
|
283
|
+
message: hasHook
|
|
284
|
+
? "SessionStart hook configured"
|
|
285
|
+
: "SessionStart exists but does not point to context-mode",
|
|
286
|
+
fix: hasHook ? undefined : "context-mode upgrade",
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
results.push({
|
|
291
|
+
check: "SessionStart hook",
|
|
292
|
+
status: "fail",
|
|
293
|
+
message: "No SessionStart hooks found",
|
|
294
|
+
fix: "context-mode upgrade",
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return results;
|
|
298
|
+
}
|
|
299
|
+
checkPluginRegistration() {
|
|
300
|
+
const settings = this.readSettings();
|
|
301
|
+
if (!settings) {
|
|
302
|
+
return {
|
|
303
|
+
check: "Plugin registration",
|
|
304
|
+
status: "warn",
|
|
305
|
+
message: "Could not read ~/.gemini/settings.json",
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
// Check in extensions or settings for context-mode
|
|
309
|
+
const extensions = settings.extensions;
|
|
310
|
+
if (extensions) {
|
|
311
|
+
const hasPlugin = Array.isArray(extensions)
|
|
312
|
+
? extensions.some((e) => typeof e === "string" && e.includes("context-mode"))
|
|
313
|
+
: Object.keys(extensions).some((k) => k.includes("context-mode"));
|
|
314
|
+
if (hasPlugin) {
|
|
315
|
+
return {
|
|
316
|
+
check: "Plugin registration",
|
|
317
|
+
status: "pass",
|
|
318
|
+
message: "context-mode found in extensions",
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
check: "Plugin registration",
|
|
324
|
+
status: "warn",
|
|
325
|
+
message: "context-mode not found in extensions (might be using standalone MCP mode)",
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
getInstalledVersion() {
|
|
329
|
+
// Check ~/.gemini/ extension cache for context-mode
|
|
330
|
+
try {
|
|
331
|
+
const cachePath = resolve(homedir(), ".gemini", "extensions", "context-mode", "package.json");
|
|
332
|
+
const pkg = JSON.parse(readFileSync(cachePath, "utf-8"));
|
|
333
|
+
if (typeof pkg.version === "string")
|
|
334
|
+
return pkg.version;
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
/* not found */
|
|
338
|
+
}
|
|
339
|
+
return "not installed";
|
|
340
|
+
}
|
|
341
|
+
// ── Upgrade ────────────────────────────────────────────
|
|
342
|
+
configureAllHooks(_pluginRoot) {
|
|
343
|
+
const settings = this.readSettings() ?? {};
|
|
344
|
+
const hooks = (settings.hooks ?? {});
|
|
345
|
+
const changes = [];
|
|
346
|
+
const hookConfigs = [
|
|
347
|
+
{ name: GEMINI_HOOK_NAMES.BEFORE_TOOL },
|
|
348
|
+
{ name: GEMINI_HOOK_NAMES.SESSION_START },
|
|
349
|
+
];
|
|
350
|
+
for (const config of hookConfigs) {
|
|
351
|
+
const command = `context-mode hook gemini-cli ${config.name.toLowerCase()}`;
|
|
352
|
+
const entry = {
|
|
353
|
+
matcher: "",
|
|
354
|
+
hooks: [{ type: "command", command }],
|
|
355
|
+
};
|
|
356
|
+
const existing = hooks[config.name];
|
|
357
|
+
if (existing && Array.isArray(existing)) {
|
|
358
|
+
const idx = existing.findIndex((e) => {
|
|
359
|
+
const entryHooks = e.hooks;
|
|
360
|
+
return entryHooks?.some((h) => h.command?.includes("context-mode"));
|
|
361
|
+
});
|
|
362
|
+
if (idx >= 0) {
|
|
363
|
+
existing[idx] = entry;
|
|
364
|
+
changes.push(`Updated existing ${config.name} hook entry`);
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
existing.push(entry);
|
|
368
|
+
changes.push(`Added ${config.name} hook entry`);
|
|
369
|
+
}
|
|
370
|
+
hooks[config.name] = existing;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
hooks[config.name] = [entry];
|
|
374
|
+
changes.push(`Created ${config.name} hooks section`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
settings.hooks = hooks;
|
|
378
|
+
this.writeSettings(settings);
|
|
379
|
+
return changes;
|
|
380
|
+
}
|
|
381
|
+
backupSettings() {
|
|
382
|
+
const settingsPath = this.getSettingsPath();
|
|
383
|
+
try {
|
|
384
|
+
accessSync(settingsPath, constants.R_OK);
|
|
385
|
+
const backupPath = settingsPath + ".bak";
|
|
386
|
+
copyFileSync(settingsPath, backupPath);
|
|
387
|
+
return backupPath;
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
setHookPermissions(pluginRoot) {
|
|
394
|
+
const set = [];
|
|
395
|
+
const hooksDir = join(pluginRoot, "hooks", "gemini-cli");
|
|
396
|
+
for (const scriptName of Object.values(GEMINI_HOOK_SCRIPTS)) {
|
|
397
|
+
const scriptPath = resolve(hooksDir, scriptName);
|
|
398
|
+
try {
|
|
399
|
+
accessSync(scriptPath, constants.R_OK);
|
|
400
|
+
chmodSync(scriptPath, 0o755);
|
|
401
|
+
set.push(scriptPath);
|
|
402
|
+
}
|
|
403
|
+
catch {
|
|
404
|
+
/* skip missing scripts */
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return set;
|
|
408
|
+
}
|
|
409
|
+
updatePluginRegistry(pluginRoot, version) {
|
|
410
|
+
// Gemini CLI doesn't have a formal plugin registry like Claude Code.
|
|
411
|
+
// Update the extension cache package.json if it exists.
|
|
412
|
+
try {
|
|
413
|
+
const pkgPath = resolve(homedir(), ".gemini", "extensions", "context-mode", "package.json");
|
|
414
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
415
|
+
pkg.version = version;
|
|
416
|
+
pkg.installPath = pluginRoot;
|
|
417
|
+
pkg.lastUpdated = new Date().toISOString();
|
|
418
|
+
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n", "utf-8");
|
|
419
|
+
}
|
|
420
|
+
catch {
|
|
421
|
+
/* best effort */
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// ── Routing Instructions (soft enforcement) ────────────
|
|
425
|
+
getRoutingInstructionsConfig() {
|
|
426
|
+
return {
|
|
427
|
+
fileName: "GEMINI.md",
|
|
428
|
+
globalPath: resolve(homedir(), ".gemini", "GEMINI.md"),
|
|
429
|
+
projectRelativePath: "GEMINI.md",
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
writeRoutingInstructions(projectDir, pluginRoot) {
|
|
433
|
+
const config = this.getRoutingInstructionsConfig();
|
|
434
|
+
const targetPath = resolve(projectDir, config.projectRelativePath);
|
|
435
|
+
const sourcePath = resolve(pluginRoot, "configs", "gemini-cli", config.fileName);
|
|
436
|
+
try {
|
|
437
|
+
const content = readFileSync(sourcePath, "utf-8");
|
|
438
|
+
try {
|
|
439
|
+
const existing = readFileSync(targetPath, "utf-8");
|
|
440
|
+
if (existing.includes("context-mode"))
|
|
441
|
+
return null;
|
|
442
|
+
writeFileSync(targetPath, existing.trimEnd() + "\n\n" + content, "utf-8");
|
|
443
|
+
return targetPath;
|
|
444
|
+
}
|
|
445
|
+
catch {
|
|
446
|
+
writeFileSync(targetPath, content, "utf-8");
|
|
447
|
+
return targetPath;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
catch {
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
// ── Internal helpers ───────────────────────────────────
|
|
455
|
+
/** Get the project directory from environment variables. */
|
|
456
|
+
getProjectDir() {
|
|
457
|
+
return process.env.GEMINI_PROJECT_DIR ?? process.env.CLAUDE_PROJECT_DIR;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Extract session ID from Gemini CLI hook input.
|
|
461
|
+
* Priority: session_id field > env fallback > ppid fallback.
|
|
462
|
+
*/
|
|
463
|
+
extractSessionId(input) {
|
|
464
|
+
if (input.session_id)
|
|
465
|
+
return input.session_id;
|
|
466
|
+
return `pid-${process.ppid}`;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/opencode/config — Thin re-exports from OpenCodeAdapter.
|
|
3
|
+
*
|
|
4
|
+
* This module exists for backward compatibility. All logic lives in the
|
|
5
|
+
* adapter class (index.ts). New code should use getAdapter() from detect.ts.
|
|
6
|
+
*/
|
|
7
|
+
export { OpenCodeAdapter } from "./index.js";
|
|
8
|
+
export { HOOK_TYPES, REQUIRED_HOOKS, OPTIONAL_HOOKS } from "./hooks.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/opencode/config — Thin re-exports from OpenCodeAdapter.
|
|
3
|
+
*
|
|
4
|
+
* This module exists for backward compatibility. All logic lives in the
|
|
5
|
+
* adapter class (index.ts). New code should use getAdapter() from detect.ts.
|
|
6
|
+
*/
|
|
7
|
+
export { OpenCodeAdapter } from "./index.js";
|
|
8
|
+
export { HOOK_TYPES, REQUIRED_HOOKS, OPTIONAL_HOOKS } from "./hooks.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/opencode/hooks — OpenCode hook definitions and validators.
|
|
3
|
+
*
|
|
4
|
+
* Defines the hook types and validation helpers specific to OpenCode's
|
|
5
|
+
* TypeScript plugin paradigm. This module is used by:
|
|
6
|
+
* - CLI setup/upgrade commands (to configure plugin in opencode.json)
|
|
7
|
+
* - Doctor command (to validate plugin configuration)
|
|
8
|
+
*
|
|
9
|
+
* OpenCode hook system reference:
|
|
10
|
+
* - I/O: TS plugin functions (not JSON stdin/stdout)
|
|
11
|
+
* - Hook names: tool.execute.before, tool.execute.after, experimental.session.compacting
|
|
12
|
+
* - Arg modification: output.args mutation
|
|
13
|
+
* - Blocking: throw Error in tool.execute.before
|
|
14
|
+
* - SessionStart: broken (#14808, no hook #5409)
|
|
15
|
+
* - Config: opencode.json plugin array, .opencode/plugins/*.ts
|
|
16
|
+
*/
|
|
17
|
+
/** OpenCode hook types (TS plugin event names). */
|
|
18
|
+
export declare const HOOK_TYPES: {
|
|
19
|
+
readonly BEFORE: "tool.execute.before";
|
|
20
|
+
readonly AFTER: "tool.execute.after";
|
|
21
|
+
readonly COMPACTING: "experimental.session.compacting";
|
|
22
|
+
};
|
|
23
|
+
export type HookType = (typeof HOOK_TYPES)[keyof typeof HOOK_TYPES];
|
|
24
|
+
/**
|
|
25
|
+
* Required hooks that must be active for context-mode to function.
|
|
26
|
+
* OpenCode uses TS plugin paradigm — no scripts, just event hooks.
|
|
27
|
+
*/
|
|
28
|
+
export declare const REQUIRED_HOOKS: HookType[];
|
|
29
|
+
/**
|
|
30
|
+
* Optional hooks that enhance functionality but aren't critical.
|
|
31
|
+
* experimental.session.compacting is advisory.
|
|
32
|
+
*/
|
|
33
|
+
export declare const OPTIONAL_HOOKS: HookType[];
|
|
34
|
+
/**
|
|
35
|
+
* Check if an OpenCode plugin entry is the context-mode plugin.
|
|
36
|
+
* OpenCode plugins are registered as strings in the plugin array.
|
|
37
|
+
*/
|
|
38
|
+
export declare function isContextModePlugin(pluginEntry: string): boolean;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/opencode/hooks — OpenCode hook definitions and validators.
|
|
3
|
+
*
|
|
4
|
+
* Defines the hook types and validation helpers specific to OpenCode's
|
|
5
|
+
* TypeScript plugin paradigm. This module is used by:
|
|
6
|
+
* - CLI setup/upgrade commands (to configure plugin in opencode.json)
|
|
7
|
+
* - Doctor command (to validate plugin configuration)
|
|
8
|
+
*
|
|
9
|
+
* OpenCode hook system reference:
|
|
10
|
+
* - I/O: TS plugin functions (not JSON stdin/stdout)
|
|
11
|
+
* - Hook names: tool.execute.before, tool.execute.after, experimental.session.compacting
|
|
12
|
+
* - Arg modification: output.args mutation
|
|
13
|
+
* - Blocking: throw Error in tool.execute.before
|
|
14
|
+
* - SessionStart: broken (#14808, no hook #5409)
|
|
15
|
+
* - Config: opencode.json plugin array, .opencode/plugins/*.ts
|
|
16
|
+
*/
|
|
17
|
+
// ─────────────────────────────────────────────────────────
|
|
18
|
+
// Hook type constants
|
|
19
|
+
// ─────────────────────────────────────────────────────────
|
|
20
|
+
/** OpenCode hook types (TS plugin event names). */
|
|
21
|
+
export const HOOK_TYPES = {
|
|
22
|
+
BEFORE: "tool.execute.before",
|
|
23
|
+
AFTER: "tool.execute.after",
|
|
24
|
+
COMPACTING: "experimental.session.compacting",
|
|
25
|
+
};
|
|
26
|
+
// ─────────────────────────────────────────────────────────
|
|
27
|
+
// Hook validation
|
|
28
|
+
// ─────────────────────────────────────────────────────────
|
|
29
|
+
/**
|
|
30
|
+
* Required hooks that must be active for context-mode to function.
|
|
31
|
+
* OpenCode uses TS plugin paradigm — no scripts, just event hooks.
|
|
32
|
+
*/
|
|
33
|
+
export const REQUIRED_HOOKS = [
|
|
34
|
+
HOOK_TYPES.BEFORE,
|
|
35
|
+
HOOK_TYPES.AFTER,
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* Optional hooks that enhance functionality but aren't critical.
|
|
39
|
+
* experimental.session.compacting is advisory.
|
|
40
|
+
*/
|
|
41
|
+
export const OPTIONAL_HOOKS = [
|
|
42
|
+
HOOK_TYPES.COMPACTING,
|
|
43
|
+
];
|
|
44
|
+
/**
|
|
45
|
+
* Check if an OpenCode plugin entry is the context-mode plugin.
|
|
46
|
+
* OpenCode plugins are registered as strings in the plugin array.
|
|
47
|
+
*/
|
|
48
|
+
export function isContextModePlugin(pluginEntry) {
|
|
49
|
+
return pluginEntry.includes("context-mode");
|
|
50
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adapters/opencode — OpenCode platform adapter.
|
|
3
|
+
*
|
|
4
|
+
* Implements HookAdapter for OpenCode's TypeScript plugin paradigm.
|
|
5
|
+
*
|
|
6
|
+
* OpenCode hook specifics:
|
|
7
|
+
* - I/O: TS plugin functions (not JSON stdin/stdout)
|
|
8
|
+
* - Hook names: tool.execute.before, tool.execute.after, experimental.session.compacting
|
|
9
|
+
* - Arg modification: output.args mutation
|
|
10
|
+
* - Blocking: throw Error in tool.execute.before
|
|
11
|
+
* - Output modification: output.output mutation (TUI bug for bash #13575)
|
|
12
|
+
* - SessionStart: broken (#14808, no hook #5409)
|
|
13
|
+
* - Session ID: input.sessionID (camelCase!)
|
|
14
|
+
* - Project dir: ctx.directory in plugin init (no env var)
|
|
15
|
+
* - Config: opencode.json plugin array, .opencode/plugins/*.ts
|
|
16
|
+
* - Session dir: ~/.config/opencode/context-mode/sessions/
|
|
17
|
+
*/
|
|
18
|
+
import type { HookAdapter, HookParadigm, PlatformCapabilities, DiagnosticResult, PreToolUseEvent, PostToolUseEvent, PreCompactEvent, SessionStartEvent, PreToolUseResponse, PostToolUseResponse, PreCompactResponse, SessionStartResponse, HookRegistration, RoutingInstructionsConfig } from "../types.js";
|
|
19
|
+
export declare class OpenCodeAdapter implements HookAdapter {
|
|
20
|
+
readonly name = "OpenCode";
|
|
21
|
+
readonly paradigm: HookParadigm;
|
|
22
|
+
readonly capabilities: PlatformCapabilities;
|
|
23
|
+
parsePreToolUseInput(raw: unknown): PreToolUseEvent;
|
|
24
|
+
parsePostToolUseInput(raw: unknown): PostToolUseEvent;
|
|
25
|
+
parsePreCompactInput(raw: unknown): PreCompactEvent;
|
|
26
|
+
parseSessionStartInput(raw: unknown): SessionStartEvent;
|
|
27
|
+
formatPreToolUseResponse(response: PreToolUseResponse): unknown;
|
|
28
|
+
formatPostToolUseResponse(response: PostToolUseResponse): unknown;
|
|
29
|
+
formatPreCompactResponse(response: PreCompactResponse): unknown;
|
|
30
|
+
formatSessionStartResponse(response: SessionStartResponse): unknown;
|
|
31
|
+
getSettingsPath(): string;
|
|
32
|
+
getSessionDir(): string;
|
|
33
|
+
getSessionDBPath(projectDir: string): string;
|
|
34
|
+
getSessionEventsPath(projectDir: string): string;
|
|
35
|
+
generateHookConfig(_pluginRoot: string): HookRegistration;
|
|
36
|
+
readSettings(): Record<string, unknown> | null;
|
|
37
|
+
writeSettings(settings: Record<string, unknown>): void;
|
|
38
|
+
validateHooks(_pluginRoot: string): DiagnosticResult[];
|
|
39
|
+
checkPluginRegistration(): DiagnosticResult;
|
|
40
|
+
getInstalledVersion(): string;
|
|
41
|
+
configureAllHooks(_pluginRoot: string): string[];
|
|
42
|
+
backupSettings(): string | null;
|
|
43
|
+
setHookPermissions(_pluginRoot: string): string[];
|
|
44
|
+
updatePluginRegistry(_pluginRoot: string, _version: string): void;
|
|
45
|
+
getRoutingInstructionsConfig(): RoutingInstructionsConfig;
|
|
46
|
+
writeRoutingInstructions(projectDir: string, pluginRoot: string): string | null;
|
|
47
|
+
/**
|
|
48
|
+
* Extract session ID from OpenCode hook input.
|
|
49
|
+
* OpenCode uses camelCase sessionID.
|
|
50
|
+
*/
|
|
51
|
+
private extractSessionId;
|
|
52
|
+
}
|