@omnitype-code/cli 0.1.1 → 0.1.3

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.
@@ -0,0 +1,703 @@
1
+ "use strict";
2
+ /**
3
+ * Auto-installs OmniType model-detection hooks into AI tools that support
4
+ * a preToolUse/postToolUse hook system.
5
+ *
6
+ * Hooks write {model, tool, ts, file} to <project_root>/.omnitype/active-model.json.
7
+ * The project root is process.cwd() at hook invocation time — this isolates every
8
+ * workspace so concurrent sessions in different windows never bleed into each other.
9
+ * Falls back to ~/.omnitype/ if cwd is outside any workspace.
10
+ *
11
+ * Each installer is idempotent — safe to call on every startup.
12
+ * Fails silently so it never breaks the CLI for the user.
13
+ */
14
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ var desc = Object.getOwnPropertyDescriptor(m, k);
17
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
18
+ desc = { enumerable: true, get: function() { return m[k]; } };
19
+ }
20
+ Object.defineProperty(o, k2, desc);
21
+ }) : (function(o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ }));
25
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
26
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
27
+ }) : function(o, v) {
28
+ o["default"] = v;
29
+ });
30
+ var __importStar = (this && this.__importStar) || (function () {
31
+ var ownKeys = function(o) {
32
+ ownKeys = Object.getOwnPropertyNames || function (o) {
33
+ var ar = [];
34
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
35
+ return ar;
36
+ };
37
+ return ownKeys(o);
38
+ };
39
+ return function (mod) {
40
+ if (mod && mod.__esModule) return mod;
41
+ var result = {};
42
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ })();
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.HOOK_VERSION = exports.GLOBAL_OMNITYPE_DIR = void 0;
49
+ exports.installAllToolHooks = installAllToolHooks;
50
+ exports.checkHookStatus = checkHookStatus;
51
+ const fs = __importStar(require("fs"));
52
+ const os = __importStar(require("os"));
53
+ const path = __importStar(require("path"));
54
+ exports.GLOBAL_OMNITYPE_DIR = path.join(os.homedir(), '.omnitype');
55
+ // Version token embedded in every hook command.
56
+ // Bump whenever hook logic changes so stale installs are replaced automatically.
57
+ exports.HOOK_VERSION = 'omnitype-hook-v4';
58
+ // Shared sentinel-write snippet: resolves dir from process.cwd(), falls back to ~/.omnitype/.
59
+ // `modelExpr` is a JS expression that evaluates to the model string (already extracted).
60
+ // `tool` is the literal tool name string.
61
+ const WRITE_SENTINEL = (tool) => `const fs=require('fs'),p=require('path'),os=require('os');` +
62
+ `const dir=p.join(process.cwd(),'.omnitype');` +
63
+ `const fbDir=p.join(os.homedir(),'.omnitype');` +
64
+ `fs.mkdirSync(dir,{recursive:true});` +
65
+ `fs.mkdirSync(fbDir,{recursive:true});` +
66
+ `const payload=JSON.stringify(Object.assign({model:m,tool:'${tool}',ts:Date.now()},file&&{file}));` +
67
+ `fs.writeFileSync(p.join(dir,'active-model.json'),payload);` +
68
+ `fs.writeFileSync(p.join(fbDir,'active-model.json'),payload);`;
69
+ // Generic hook: reads model from stdin JSON, writes sentinel to project root + global fallback.
70
+ function buildHookCommand(tool, modelField) {
71
+ return (`node -e "/*${exports.HOOK_VERSION}*/let b='';process.stdin.on('data',c=>b+=c);` +
72
+ `process.stdin.on('end',()=>{` +
73
+ `try{const j=JSON.parse(b);` +
74
+ `let m=j['${modelField}']||j.model;` +
75
+ `if(!m)return;` +
76
+ `let file;try{file=j?.tool_input?.path||j?.tool_input?.file_path||j?.toolInput?.path;}catch{}` +
77
+ WRITE_SENTINEL(tool) +
78
+ `}catch{}})"`);
79
+ }
80
+ // Claude Code hook: reads model from stdin first, then env, then settings.json.
81
+ const CLAUDE_HOOK_CMD = `node -e "/*${exports.HOOK_VERSION}*/let b='';process.stdin.on('data',c=>b+=c);process.stdin.on('end',()=>{` +
82
+ `try{` +
83
+ `let m,file;` +
84
+ `try{const j=JSON.parse(b);m=j.model;file=j?.tool_input?.path||j?.tool_input?.file_path||j?.toolInput?.path;}catch{}` +
85
+ `if(!m)m=process.env.CLAUDE_MODEL||process.env.ANTHROPIC_MODEL;` +
86
+ `if(!m){try{const _fs=require('fs'),_p=require('path'),_os=require('os');` +
87
+ `const s=JSON.parse(_fs.readFileSync(_p.join(_os.homedir(),'.claude','settings.json'),'utf8'));` +
88
+ `m=s.model||s.defaultModel;}catch{}}` +
89
+ `if(!m)return;` +
90
+ WRITE_SENTINEL('claude-code') +
91
+ `}catch{}})"`;
92
+ function readJson(filePath) {
93
+ try {
94
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
95
+ }
96
+ catch {
97
+ return {};
98
+ }
99
+ }
100
+ function writeJsonAtomic(filePath, data) {
101
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
102
+ const tmp = filePath + '.tmp';
103
+ fs.writeFileSync(tmp, JSON.stringify(data, null, 2) + '\n');
104
+ fs.renameSync(tmp, filePath);
105
+ }
106
+ /** True if a hook command string belongs to us (any version). */
107
+ function isOurs(cmd) { return cmd.includes('.omnitype'); }
108
+ /** True if a hook command belongs to us AND is current (no upgrade needed). */
109
+ function isCurrent(cmd) { return cmd.includes(exports.HOOK_VERSION); }
110
+ /** Remove stale omnitype entries from an array of hook objects (any shape). */
111
+ function purgeStale(arr, cmdExtract) {
112
+ return arr.filter(h => {
113
+ const cmd = cmdExtract(h) ?? '';
114
+ return !(isOurs(cmd) && !isCurrent(cmd));
115
+ });
116
+ }
117
+ // ── Claude Code ───────────────────────────────────────────────────────────────
118
+ const CLAUDE_DIR = path.join(os.homedir(), '.claude');
119
+ const CLAUDE_SETTINGS = path.join(CLAUDE_DIR, 'settings.json');
120
+ const CLAUDE_HOOK_ENTRY = {
121
+ matcher: 'Write|Edit|MultiEdit|NotebookEdit',
122
+ hooks: [{ type: 'command', command: CLAUDE_HOOK_CMD }],
123
+ };
124
+ function installClaudeHooks() {
125
+ if (!fs.existsSync(CLAUDE_DIR))
126
+ return;
127
+ const s = readJson(CLAUDE_SETTINGS);
128
+ if (!s.hooks)
129
+ s.hooks = {};
130
+ if (!Array.isArray(s.hooks.PreToolUse))
131
+ s.hooks.PreToolUse = [];
132
+ // Remove stale versions, then skip if current version already present.
133
+ s.hooks.PreToolUse = purgeStale(s.hooks.PreToolUse, h => h?.hooks?.[0]?.command);
134
+ if (s.hooks.PreToolUse.some((h) => isCurrent(h?.hooks?.[0]?.command ?? '')))
135
+ return;
136
+ s.hooks.PreToolUse.push(CLAUDE_HOOK_ENTRY);
137
+ writeJsonAtomic(CLAUDE_SETTINGS, s);
138
+ }
139
+ // ── Cursor ────────────────────────────────────────────────────────────────────
140
+ const CURSOR_HOOKS_PATH = path.join(os.homedir(), '.cursor', 'hooks.json');
141
+ const CURSOR_CMD = buildHookCommand('cursor', 'model');
142
+ function installCursorHooks() {
143
+ if (!fs.existsSync(path.join(os.homedir(), '.cursor')))
144
+ return;
145
+ const settings = readJson(CURSOR_HOOKS_PATH);
146
+ if (!settings.hooks)
147
+ settings.hooks = {};
148
+ let changed = false;
149
+ for (const event of ['preToolUse', 'postToolUse']) {
150
+ if (!Array.isArray(settings.hooks[event]))
151
+ settings.hooks[event] = [];
152
+ settings.hooks[event] = purgeStale(settings.hooks[event], h => h?.command);
153
+ if (!settings.hooks[event].some((h) => isCurrent(h?.command ?? ''))) {
154
+ settings.hooks[event].push({ command: CURSOR_CMD });
155
+ changed = true;
156
+ }
157
+ }
158
+ if (!settings.version)
159
+ settings.version = 1;
160
+ if (changed)
161
+ writeJsonAtomic(CURSOR_HOOKS_PATH, settings);
162
+ }
163
+ // ── Windsurf ──────────────────────────────────────────────────────────────────
164
+ const WINDSURF_HOOK_PATHS = [
165
+ path.join(os.homedir(), '.codeium', 'windsurf', 'hooks.json'),
166
+ path.join(os.homedir(), '.codeium', 'hooks.json'),
167
+ ];
168
+ const WINDSURF_EVENTS = ['pre_write_code', 'post_write_code', 'pre_run_command', 'post_run_command'];
169
+ const WINDSURF_CMD = buildHookCommand('windsurf', 'model_name');
170
+ function installWindsurfHooks() {
171
+ if (!fs.existsSync(path.join(os.homedir(), '.codeium')))
172
+ return;
173
+ for (const hookPath of WINDSURF_HOOK_PATHS) {
174
+ if (!fs.existsSync(hookPath) && !fs.existsSync(path.dirname(hookPath)))
175
+ continue;
176
+ const settings = readJson(hookPath);
177
+ if (!settings.hooks)
178
+ settings.hooks = {};
179
+ let changed = false;
180
+ for (const event of WINDSURF_EVENTS) {
181
+ if (!Array.isArray(settings.hooks[event]))
182
+ settings.hooks[event] = [];
183
+ settings.hooks[event] = purgeStale(settings.hooks[event], h => h?.command);
184
+ if (!settings.hooks[event].some((h) => isCurrent(h?.command ?? ''))) {
185
+ settings.hooks[event].push({ command: WINDSURF_CMD });
186
+ changed = true;
187
+ }
188
+ }
189
+ if (changed)
190
+ writeJsonAtomic(hookPath, settings);
191
+ }
192
+ }
193
+ // ── Codex ─────────────────────────────────────────────────────────────────────
194
+ const CODEX_HOOKS_PATH = path.join(os.homedir(), '.codex', 'hooks.json');
195
+ const CODEX_CMD = buildHookCommand('codex', 'model');
196
+ function installCodexHooks() {
197
+ if (!fs.existsSync(path.join(os.homedir(), '.codex')))
198
+ return;
199
+ const settings = readJson(CODEX_HOOKS_PATH);
200
+ let changed = false;
201
+ for (const event of ['PreToolUse', 'PostToolUse']) {
202
+ if (!Array.isArray(settings[event]))
203
+ settings[event] = [];
204
+ settings[event] = purgeStale(settings[event], h => h?.command);
205
+ if (!settings[event].some((h) => isCurrent(h?.command ?? ''))) {
206
+ settings[event].push({ type: 'command', command: CODEX_CMD });
207
+ changed = true;
208
+ }
209
+ }
210
+ if (changed)
211
+ writeJsonAtomic(CODEX_HOOKS_PATH, settings);
212
+ }
213
+ // ── Cline ─────────────────────────────────────────────────────────────────────
214
+ // Cline looks for executable scripts in ~/Documents/Cline/Hooks/ named after
215
+ // hook events. We drop a PreToolUse script that reads stdin JSON and writes
216
+ // the sentinel only for file-modifying tools, including the target file path.
217
+ const CLINE_HOOKS_DIR = path.join(os.homedir(), 'Documents', 'Cline', 'Hooks');
218
+ const CLINE_HOOK_SCRIPT = `#!/usr/bin/env node
219
+ // ${exports.HOOK_VERSION}
220
+ const FILE_WRITE_TOOLS = new Set(['write_to_file','apply_diff','insert_content','search_and_replace']);
221
+ let buf = '';
222
+ process.stdin.on('data', c => buf += c);
223
+ process.stdin.on('end', () => {
224
+ try {
225
+ const j = JSON.parse(buf);
226
+ if (!FILE_WRITE_TOOLS.has(j.toolName)) return;
227
+ const m = j.model || j.preToolUse?.model || '';
228
+ const file = j.toolInput?.path || j.toolInput?.file_path || j.tool_input?.path || undefined;
229
+ const fs = require('fs'), p = require('path'), os = require('os');
230
+ const dir = p.join(process.cwd(), '.omnitype');
231
+ const fbDir = p.join(os.homedir(), '.omnitype');
232
+ fs.mkdirSync(dir, { recursive: true });
233
+ fs.mkdirSync(fbDir, { recursive: true });
234
+ const payload = JSON.stringify(Object.assign({ model: m, tool: 'cline', ts: Date.now() }, file && { file }));
235
+ fs.writeFileSync(p.join(dir, 'active-model.json'), payload);
236
+ fs.writeFileSync(p.join(fbDir, 'active-model.json'), payload);
237
+ } catch {}
238
+ });
239
+ `;
240
+ function installClineHooks() {
241
+ if (!fs.existsSync(CLINE_HOOKS_DIR))
242
+ return;
243
+ const scriptPath = path.join(CLINE_HOOKS_DIR, 'PreToolUse');
244
+ let existing = '';
245
+ try {
246
+ existing = fs.readFileSync(scriptPath, 'utf8');
247
+ }
248
+ catch { }
249
+ if (existing.includes(exports.HOOK_VERSION))
250
+ return; // already current
251
+ // Write (or overwrite stale version)
252
+ fs.writeFileSync(scriptPath, CLINE_HOOK_SCRIPT, 'utf8');
253
+ fs.chmodSync(scriptPath, 0o755);
254
+ }
255
+ // ── Gemini CLI ────────────────────────────────────────────────────────────────
256
+ // ~/.gemini/settings.json — BeforeTool / AfterTool with nested hooks array
257
+ const GEMINI_SETTINGS_PATH = path.join(os.homedir(), '.gemini', 'settings.json');
258
+ const GEMINI_CMD = buildHookCommand('gemini-cli', 'model');
259
+ const GEMINI_HOOK_ENTRY = { matcher: '*', hooks: [{ type: 'command', command: GEMINI_CMD }] };
260
+ function installGeminiHooks() {
261
+ if (!fs.existsSync(path.join(os.homedir(), '.gemini')))
262
+ return;
263
+ const s = readJson(GEMINI_SETTINGS_PATH);
264
+ let changed = false;
265
+ for (const ev of ['BeforeTool', 'AfterTool']) {
266
+ if (!Array.isArray(s[ev]))
267
+ s[ev] = [];
268
+ s[ev] = purgeStale(s[ev], h => h?.hooks?.[0]?.command);
269
+ if (!s[ev].some((h) => isCurrent(h?.hooks?.[0]?.command ?? ''))) {
270
+ s[ev].push(GEMINI_HOOK_ENTRY);
271
+ changed = true;
272
+ }
273
+ }
274
+ if (changed)
275
+ writeJsonAtomic(GEMINI_SETTINGS_PATH, s);
276
+ }
277
+ // ── Droid (Factory) ───────────────────────────────────────────────────────────
278
+ // ~/.factory/settings.json — PreToolUse with nested hooks array
279
+ const DROID_SETTINGS_PATH = path.join(os.homedir(), '.factory', 'settings.json');
280
+ const DROID_CMD = buildHookCommand('droid', 'model');
281
+ const DROID_HOOK_ENTRY = { matcher: '*', hooks: [{ type: 'command', command: DROID_CMD }] };
282
+ function installDroidHooks() {
283
+ if (!fs.existsSync(path.join(os.homedir(), '.factory')))
284
+ return;
285
+ const s = readJson(DROID_SETTINGS_PATH);
286
+ if (!Array.isArray(s.PreToolUse))
287
+ s.PreToolUse = [];
288
+ s.PreToolUse = purgeStale(s.PreToolUse, h => h?.hooks?.[0]?.command);
289
+ if (s.PreToolUse.some((h) => isCurrent(h?.hooks?.[0]?.command ?? '')))
290
+ return;
291
+ s.PreToolUse.push(DROID_HOOK_ENTRY);
292
+ writeJsonAtomic(DROID_SETTINGS_PATH, s);
293
+ }
294
+ // ── Firebender ────────────────────────────────────────────────────────────────
295
+ // ~/.firebender/hooks.json — preToolUse / postToolUse flat command arrays
296
+ const FIREBENDER_HOOKS_PATH = path.join(os.homedir(), '.firebender', 'hooks.json');
297
+ const FIREBENDER_CMD = buildHookCommand('firebender', 'model');
298
+ function installFirebenderHooks() {
299
+ if (!fs.existsSync(path.join(os.homedir(), '.firebender')))
300
+ return;
301
+ const s = readJson(FIREBENDER_HOOKS_PATH);
302
+ let changed = false;
303
+ for (const ev of ['preToolUse', 'postToolUse']) {
304
+ if (!Array.isArray(s[ev]))
305
+ s[ev] = [];
306
+ s[ev] = purgeStale(s[ev], h => h?.command);
307
+ if (!s[ev].some((h) => isCurrent(h?.command ?? ''))) {
308
+ s[ev].push({ command: FIREBENDER_CMD });
309
+ changed = true;
310
+ }
311
+ }
312
+ if (changed)
313
+ writeJsonAtomic(FIREBENDER_HOOKS_PATH, s);
314
+ }
315
+ // ── Amp ───────────────────────────────────────────────────────────────────────
316
+ // Amp uses a TypeScript plugin (not hooks.json). Drop a .ts file to
317
+ // ~/.config/amp/plugins/omnitype.ts
318
+ const AMP_PLUGINS_DIR = path.join(os.homedir(), '.config', 'amp', 'plugins');
319
+ const AMP_PLUGIN_PATH = path.join(AMP_PLUGINS_DIR, 'omnitype.ts');
320
+ const AMP_PLUGIN_SRC = `// @i-know-the-amp-plugin-api-is-wip-and-very-experimental-right-now
321
+ // ${exports.HOOK_VERSION}
322
+ import * as amp from 'amp';
323
+ import * as fs from 'fs';
324
+ import * as path from 'path';
325
+ import * as os from 'os';
326
+
327
+ function writeSentinel(model: string, file?: string) {
328
+ const payload = JSON.stringify(Object.assign({ model, tool: 'amp', ts: Date.now() }, file && { file }));
329
+ for (const dir of [path.join(process.cwd(), '.omnitype'), path.join(os.homedir(), '.omnitype')]) {
330
+ try { fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'active-model.json'), payload); } catch {}
331
+ }
332
+ }
333
+
334
+ amp.on('tool.call', (event: any) => {
335
+ try {
336
+ const model = event?.model ?? event?.session?.model;
337
+ if (!model) return;
338
+ writeSentinel(model, event?.input?.path ?? event?.input?.file_path);
339
+ } catch {}
340
+ });
341
+ `;
342
+ function installAmpPlugin() {
343
+ const ampDataDir = process.platform === 'win32'
344
+ ? path.join(process.env.LOCALAPPDATA ?? os.homedir(), 'amp')
345
+ : path.join(process.env.XDG_DATA_HOME ?? path.join(os.homedir(), '.local', 'share'), 'amp');
346
+ if (!fs.existsSync(ampDataDir) && !fs.existsSync(AMP_PLUGINS_DIR))
347
+ return;
348
+ let existing = '';
349
+ try {
350
+ existing = fs.readFileSync(AMP_PLUGIN_PATH, 'utf8');
351
+ }
352
+ catch { }
353
+ if (existing.includes(exports.HOOK_VERSION))
354
+ return;
355
+ fs.mkdirSync(AMP_PLUGINS_DIR, { recursive: true });
356
+ fs.writeFileSync(AMP_PLUGIN_PATH, AMP_PLUGIN_SRC, 'utf8');
357
+ }
358
+ // ── OpenCode ──────────────────────────────────────────────────────────────────
359
+ // TS plugin at ~/.config/opencode/plugins/omnitype.ts
360
+ const OPENCODE_PLUGINS_DIR = path.join(os.homedir(), '.config', 'opencode', 'plugins');
361
+ const OPENCODE_PLUGIN_PATH = path.join(OPENCODE_PLUGINS_DIR, 'omnitype.ts');
362
+ const OPENCODE_PLUGIN_SRC = `// ${exports.HOOK_VERSION}
363
+ import * as fs from 'fs';
364
+ import * as path from 'path';
365
+ import * as os from 'os';
366
+
367
+ function writeSentinel(model: string, file?: string) {
368
+ const payload = JSON.stringify(Object.assign({ model, tool: 'opencode', ts: Date.now() }, file && { file }));
369
+ for (const dir of [path.join(process.cwd(), '.omnitype'), path.join(os.homedir(), '.omnitype')]) {
370
+ try { fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'active-model.json'), payload); } catch {}
371
+ }
372
+ }
373
+
374
+ export function activate(ctx: any) {
375
+ ctx.on('tool.execute.before', (event: any) => {
376
+ try {
377
+ const model = event?.model ?? event?.session?.model;
378
+ if (!model) return;
379
+ writeSentinel(model, event?.input?.path ?? event?.input?.file_path);
380
+ } catch {}
381
+ });
382
+ }
383
+ `;
384
+ function installOpenCodePlugin() {
385
+ if (!fs.existsSync(path.join(os.homedir(), '.config', 'opencode')))
386
+ return;
387
+ let existing = '';
388
+ try {
389
+ existing = fs.readFileSync(OPENCODE_PLUGIN_PATH, 'utf8');
390
+ }
391
+ catch { }
392
+ if (existing.includes(exports.HOOK_VERSION))
393
+ return;
394
+ fs.mkdirSync(OPENCODE_PLUGINS_DIR, { recursive: true });
395
+ fs.writeFileSync(OPENCODE_PLUGIN_PATH, OPENCODE_PLUGIN_SRC, 'utf8');
396
+ }
397
+ // ── Pi ────────────────────────────────────────────────────────────────────────
398
+ // TS extension at ~/.pi/agent/extensions/omnitype.ts
399
+ const PI_EXTENSIONS_DIR = path.join(os.homedir(), '.pi', 'agent', 'extensions');
400
+ const PI_PLUGIN_PATH = path.join(PI_EXTENSIONS_DIR, 'omnitype.ts');
401
+ const PI_PLUGIN_SRC = `// ${exports.HOOK_VERSION}
402
+ import * as fs from 'fs';
403
+ import * as path from 'path';
404
+ import * as os from 'os';
405
+
406
+ function writeSentinel(model: string, file?: string) {
407
+ const payload = JSON.stringify(Object.assign({ model, tool: 'pi', ts: Date.now() }, file && { file }));
408
+ for (const dir of [path.join(process.cwd(), '.omnitype'), path.join(os.homedir(), '.omnitype')]) {
409
+ try { fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'active-model.json'), payload); } catch {}
410
+ }
411
+ }
412
+
413
+ export default {
414
+ name: 'omnitype',
415
+ onToolCall(event: any) {
416
+ try {
417
+ const model = event?.model ?? event?.agent?.model;
418
+ if (!model) return;
419
+ writeSentinel(model, event?.input?.path ?? event?.input?.file_path);
420
+ } catch {}
421
+ },
422
+ };
423
+ `;
424
+ function installPiPlugin() {
425
+ if (!fs.existsSync(path.join(os.homedir(), '.pi')))
426
+ return;
427
+ let existing = '';
428
+ try {
429
+ existing = fs.readFileSync(PI_PLUGIN_PATH, 'utf8');
430
+ }
431
+ catch { }
432
+ if (existing.includes(exports.HOOK_VERSION))
433
+ return;
434
+ fs.mkdirSync(PI_EXTENSIONS_DIR, { recursive: true });
435
+ fs.writeFileSync(PI_PLUGIN_PATH, PI_PLUGIN_SRC, 'utf8');
436
+ }
437
+ // ── GitHub Copilot ────────────────────────────────────────────────────────────
438
+ // Copilot supports a hooks file at ~/.copilot/hooks/omnitype.json with
439
+ // PreToolUse / PostToolUse arrays — same shape as Claude Code.
440
+ const COPILOT_HOOKS_PATH = path.join(os.homedir(), '.copilot', 'hooks', 'omnitype.json');
441
+ const COPILOT_CMD = buildHookCommand('copilot', 'model');
442
+ function installCopilotHooks() {
443
+ // Require either ~/.copilot or ~/.vscode or VS Code settings to exist
444
+ const hasVscode = fs.existsSync(path.join(os.homedir(), '.vscode')) ||
445
+ fs.existsSync(path.join(os.homedir(), 'Library', 'Application Support', 'Code'));
446
+ const hasCopilotDir = fs.existsSync(path.join(os.homedir(), '.copilot'));
447
+ if (!hasVscode && !hasCopilotDir)
448
+ return;
449
+ const s = readJson(COPILOT_HOOKS_PATH);
450
+ if (!s.hooks)
451
+ s.hooks = {};
452
+ let changed = false;
453
+ for (const ev of ['PreToolUse', 'PostToolUse']) {
454
+ if (!Array.isArray(s.hooks[ev]))
455
+ s.hooks[ev] = [];
456
+ s.hooks[ev] = purgeStale(s.hooks[ev], h => h?.command);
457
+ if (!s.hooks[ev].some((h) => isCurrent(h?.command ?? ''))) {
458
+ s.hooks[ev].push({ type: 'command', command: COPILOT_CMD });
459
+ changed = true;
460
+ }
461
+ }
462
+ if (changed)
463
+ writeJsonAtomic(COPILOT_HOOKS_PATH, s);
464
+ }
465
+ // ── Public API ────────────────────────────────────────────────────────────────
466
+ function installAllToolHooks() {
467
+ try {
468
+ installClaudeHooks();
469
+ }
470
+ catch { }
471
+ try {
472
+ installCursorHooks();
473
+ }
474
+ catch { }
475
+ try {
476
+ installWindsurfHooks();
477
+ }
478
+ catch { }
479
+ try {
480
+ installCodexHooks();
481
+ }
482
+ catch { }
483
+ try {
484
+ installClineHooks();
485
+ }
486
+ catch { }
487
+ try {
488
+ installCopilotHooks();
489
+ }
490
+ catch { }
491
+ try {
492
+ installGeminiHooks();
493
+ }
494
+ catch { }
495
+ try {
496
+ installDroidHooks();
497
+ }
498
+ catch { }
499
+ try {
500
+ installFirebenderHooks();
501
+ }
502
+ catch { }
503
+ try {
504
+ installAmpPlugin();
505
+ }
506
+ catch { }
507
+ try {
508
+ installOpenCodePlugin();
509
+ }
510
+ catch { }
511
+ try {
512
+ installPiPlugin();
513
+ }
514
+ catch { }
515
+ }
516
+ function checkHookStatus() {
517
+ const results = [];
518
+ // Claude Code
519
+ if (!fs.existsSync(CLAUDE_DIR)) {
520
+ results.push({ tool: 'claude-code', status: 'tool-absent' });
521
+ }
522
+ else {
523
+ const hooks = readJson(CLAUDE_SETTINGS)?.hooks?.PreToolUse ?? [];
524
+ const cmd = (h) => h?.hooks?.[0]?.command ?? '';
525
+ if (hooks.some(h => isCurrent(cmd(h))))
526
+ results.push({ tool: 'claude-code', status: 'installed' });
527
+ else if (hooks.some(h => isOurs(cmd(h))))
528
+ results.push({ tool: 'claude-code', status: 'stale' });
529
+ else
530
+ results.push({ tool: 'claude-code', status: 'not-installed' });
531
+ }
532
+ // Cursor
533
+ if (!fs.existsSync(path.join(os.homedir(), '.cursor'))) {
534
+ results.push({ tool: 'cursor', status: 'tool-absent' });
535
+ }
536
+ else {
537
+ const hooks = readJson(CURSOR_HOOKS_PATH)?.hooks?.preToolUse ?? [];
538
+ if (hooks.some(h => isCurrent(h?.command ?? '')))
539
+ results.push({ tool: 'cursor', status: 'installed' });
540
+ else if (hooks.some(h => isOurs(h?.command ?? '')))
541
+ results.push({ tool: 'cursor', status: 'stale' });
542
+ else
543
+ results.push({ tool: 'cursor', status: 'not-installed' });
544
+ }
545
+ // Windsurf
546
+ if (!fs.existsSync(path.join(os.homedir(), '.codeium'))) {
547
+ results.push({ tool: 'windsurf', status: 'tool-absent' });
548
+ }
549
+ else {
550
+ let status = 'not-installed';
551
+ for (const hp of WINDSURF_HOOK_PATHS) {
552
+ const hooks = readJson(hp)?.hooks?.pre_write_code ?? [];
553
+ if (hooks.some(h => isCurrent(h?.command ?? ''))) {
554
+ status = 'installed';
555
+ break;
556
+ }
557
+ if (hooks.some(h => isOurs(h?.command ?? ''))) {
558
+ status = 'stale';
559
+ }
560
+ }
561
+ results.push({ tool: 'windsurf', status });
562
+ }
563
+ // Codex
564
+ if (!fs.existsSync(path.join(os.homedir(), '.codex'))) {
565
+ results.push({ tool: 'codex', status: 'tool-absent' });
566
+ }
567
+ else {
568
+ const hooks = readJson(CODEX_HOOKS_PATH)?.PreToolUse ?? [];
569
+ if (hooks.some(h => isCurrent(h?.command ?? '')))
570
+ results.push({ tool: 'codex', status: 'installed' });
571
+ else if (hooks.some(h => isOurs(h?.command ?? '')))
572
+ results.push({ tool: 'codex', status: 'stale' });
573
+ else
574
+ results.push({ tool: 'codex', status: 'not-installed' });
575
+ }
576
+ // Cline
577
+ if (!fs.existsSync(CLINE_HOOKS_DIR)) {
578
+ results.push({ tool: 'cline', status: 'tool-absent' });
579
+ }
580
+ else {
581
+ let existing = '';
582
+ try {
583
+ existing = fs.readFileSync(path.join(CLINE_HOOKS_DIR, 'PreToolUse'), 'utf8');
584
+ }
585
+ catch { }
586
+ if (existing.includes(exports.HOOK_VERSION))
587
+ results.push({ tool: 'cline', status: 'installed' });
588
+ else if (existing.includes('.omnitype'))
589
+ results.push({ tool: 'cline', status: 'stale' });
590
+ else
591
+ results.push({ tool: 'cline', status: 'not-installed' });
592
+ }
593
+ // GitHub Copilot
594
+ const hasVscode = fs.existsSync(path.join(os.homedir(), '.vscode')) ||
595
+ fs.existsSync(path.join(os.homedir(), 'Library', 'Application Support', 'Code'));
596
+ const hasCopilotDir = fs.existsSync(path.join(os.homedir(), '.copilot'));
597
+ if (!hasVscode && !hasCopilotDir) {
598
+ results.push({ tool: 'copilot', status: 'tool-absent' });
599
+ }
600
+ else {
601
+ const hooks = readJson(COPILOT_HOOKS_PATH)?.hooks?.PreToolUse ?? [];
602
+ if (hooks.some(h => isCurrent(h?.command ?? '')))
603
+ results.push({ tool: 'copilot', status: 'installed' });
604
+ else if (hooks.some(h => isOurs(h?.command ?? '')))
605
+ results.push({ tool: 'copilot', status: 'stale' });
606
+ else
607
+ results.push({ tool: 'copilot', status: 'not-installed' });
608
+ }
609
+ // Gemini CLI
610
+ if (!fs.existsSync(path.join(os.homedir(), '.gemini'))) {
611
+ results.push({ tool: 'gemini-cli', status: 'tool-absent' });
612
+ }
613
+ else {
614
+ const hooks = readJson(GEMINI_SETTINGS_PATH)?.BeforeTool ?? [];
615
+ if (hooks.some(h => isCurrent(h?.hooks?.[0]?.command ?? '')))
616
+ results.push({ tool: 'gemini-cli', status: 'installed' });
617
+ else if (hooks.some(h => isOurs(h?.hooks?.[0]?.command ?? '')))
618
+ results.push({ tool: 'gemini-cli', status: 'stale' });
619
+ else
620
+ results.push({ tool: 'gemini-cli', status: 'not-installed' });
621
+ }
622
+ // Droid
623
+ if (!fs.existsSync(path.join(os.homedir(), '.factory'))) {
624
+ results.push({ tool: 'droid', status: 'tool-absent' });
625
+ }
626
+ else {
627
+ const hooks = readJson(DROID_SETTINGS_PATH)?.PreToolUse ?? [];
628
+ if (hooks.some(h => isCurrent(h?.hooks?.[0]?.command ?? '')))
629
+ results.push({ tool: 'droid', status: 'installed' });
630
+ else if (hooks.some(h => isOurs(h?.hooks?.[0]?.command ?? '')))
631
+ results.push({ tool: 'droid', status: 'stale' });
632
+ else
633
+ results.push({ tool: 'droid', status: 'not-installed' });
634
+ }
635
+ // Firebender
636
+ if (!fs.existsSync(path.join(os.homedir(), '.firebender'))) {
637
+ results.push({ tool: 'firebender', status: 'tool-absent' });
638
+ }
639
+ else {
640
+ const hooks = readJson(FIREBENDER_HOOKS_PATH)?.preToolUse ?? [];
641
+ if (hooks.some(h => isCurrent(h?.command ?? '')))
642
+ results.push({ tool: 'firebender', status: 'installed' });
643
+ else if (hooks.some(h => isOurs(h?.command ?? '')))
644
+ results.push({ tool: 'firebender', status: 'stale' });
645
+ else
646
+ results.push({ tool: 'firebender', status: 'not-installed' });
647
+ }
648
+ // Amp plugin
649
+ const ampDataDir = process.platform === 'win32'
650
+ ? path.join(process.env.LOCALAPPDATA ?? os.homedir(), 'amp')
651
+ : path.join(process.env.XDG_DATA_HOME ?? path.join(os.homedir(), '.local', 'share'), 'amp');
652
+ if (!fs.existsSync(ampDataDir) && !fs.existsSync(AMP_PLUGINS_DIR)) {
653
+ results.push({ tool: 'amp', status: 'tool-absent' });
654
+ }
655
+ else {
656
+ let src = '';
657
+ try {
658
+ src = fs.readFileSync(AMP_PLUGIN_PATH, 'utf8');
659
+ }
660
+ catch { }
661
+ if (src.includes(exports.HOOK_VERSION))
662
+ results.push({ tool: 'amp', status: 'installed' });
663
+ else if (src.includes('.omnitype'))
664
+ results.push({ tool: 'amp', status: 'stale' });
665
+ else
666
+ results.push({ tool: 'amp', status: 'not-installed' });
667
+ }
668
+ // OpenCode plugin
669
+ if (!fs.existsSync(path.join(os.homedir(), '.config', 'opencode'))) {
670
+ results.push({ tool: 'opencode', status: 'tool-absent' });
671
+ }
672
+ else {
673
+ let src = '';
674
+ try {
675
+ src = fs.readFileSync(OPENCODE_PLUGIN_PATH, 'utf8');
676
+ }
677
+ catch { }
678
+ if (src.includes(exports.HOOK_VERSION))
679
+ results.push({ tool: 'opencode', status: 'installed' });
680
+ else if (src.includes('.omnitype'))
681
+ results.push({ tool: 'opencode', status: 'stale' });
682
+ else
683
+ results.push({ tool: 'opencode', status: 'not-installed' });
684
+ }
685
+ // Pi plugin
686
+ if (!fs.existsSync(path.join(os.homedir(), '.pi'))) {
687
+ results.push({ tool: 'pi', status: 'tool-absent' });
688
+ }
689
+ else {
690
+ let src = '';
691
+ try {
692
+ src = fs.readFileSync(PI_PLUGIN_PATH, 'utf8');
693
+ }
694
+ catch { }
695
+ if (src.includes(exports.HOOK_VERSION))
696
+ results.push({ tool: 'pi', status: 'installed' });
697
+ else if (src.includes('.omnitype'))
698
+ results.push({ tool: 'pi', status: 'stale' });
699
+ else
700
+ results.push({ tool: 'pi', status: 'not-installed' });
701
+ }
702
+ return results;
703
+ }