-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.ts
More file actions
468 lines (422 loc) · 13.9 KB
/
Copy pathhooks.ts
File metadata and controls
468 lines (422 loc) · 13.9 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import { mkdir, readdir, writeFile } from "node:fs/promises";
import { createHash } from "node:crypto";
import { homedir, tmpdir } from "node:os";
import { extname, join } from "node:path";
import { pathToFileURL } from "node:url";
import type { ReactorEmittedEvent } from "@intx/inference";
import type {
ConversationTurn,
LastCycleSource,
TokenUsage,
ToolCall,
ToolResult,
} from "@intx/types/runtime";
import { onTurnBoundary } from "../agent/reactor-events.js";
import { COMMAND_NAME, SETTINGS_DIR_NAME } from "../branding.js";
export type HookKind = "postTurn" | "postRun";
export type LifecycleHookType = "typescript" | "shell";
export type HookExitStatus = {
code: number | null;
signal: string | null;
stderr: string;
};
export type LifecycleHook = {
id: string;
name: string;
type: LifecycleHookType;
path: string;
};
export type LifecycleHookStatus = LifecycleHook & {
enabled: boolean;
lastFiredAt?: number;
lastKind?: HookKind;
lastExitStatus?: HookExitStatus;
};
export type TurnContext = {
turnIndex: number;
assistantTurn: ConversationTurn;
toolCalls: ToolCall[];
toolResults: ToolResult[];
usage: TokenUsage;
source: LastCycleSource;
durationMs: number;
};
export type RunSummary = {
task: string;
status: "done" | "failed" | "cancelled";
startedAt: number;
finishedAt: number;
durationMs: number;
turnsUsed: number;
tokenUsage: TokenUsage;
turns: TurnContext[];
toolCallCount: number;
error?: string;
};
export type LifecycleHookEvent =
| { type: "hooks.loaded"; hooks: LifecycleHookStatus[] }
| { type: "hook.updated"; hook: LifecycleHookStatus };
export type LifecycleHookManager = {
getStatuses(): LifecycleHookStatus[];
setEnabled(id: string, enabled: boolean): void;
dispatchPostTurn(ctx: TurnContext): void;
dispatchPostRun(summary: RunSummary): Promise<void>;
};
type PendingTurn = {
startedAt: number;
turnIndex: number;
assistantTurn: ConversationTurn;
toolCalls: ToolCall[];
toolResults: ToolResult[];
usage: TokenUsage;
source: LastCycleSource;
};
const emptyUsage: TokenUsage = {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
thinking: 0,
};
export const RETAINED_TURN_CONTEXT_LIMIT = 200;
// Retained turns are only built when a hook consumes them (over stdin, not
// the model), so a tail-anchored budget well below any model context limit
// keeps up to 200 turns of tool output from becoming a second full copy of
// recent history in memory.
export const HOOK_PAYLOAD_TOOL_RESULT_CHARS = 4_000;
export function hooksDirectory(): string {
return globalHooksDirectory();
}
export function localHooksDirectory(cwd: string = process.cwd()): string {
return join(cwd, SETTINGS_DIR_NAME, "hooks");
}
export function globalHooksDirectory(): string {
return join(homedir(), SETTINGS_DIR_NAME, "hooks");
}
export function hookDirectories(cwd: string = process.cwd()): string[] {
return [localHooksDirectory(cwd), globalHooksDirectory()];
}
export async function discoverLifecycleHooks(
directories: string | string[] = hookDirectories(),
): Promise<LifecycleHook[]> {
const resolvedDirectories = Array.isArray(directories) ? directories : [directories];
const hooksByName = new Map<string, LifecycleHook>();
for (const directory of resolvedDirectories) {
for (const hook of await discoverHooksInDirectory(directory)) {
if (!hooksByName.has(hook.name)) {
hooksByName.set(hook.name, hook);
}
}
}
return [...hooksByName.values()];
}
async function discoverHooksInDirectory(directory: string): Promise<LifecycleHook[]> {
let entries: string[];
try {
entries = await readdir(directory);
} catch (err) {
if (isENOENT(err)) return [];
throw err;
}
return entries
.filter((entry) => !entry.startsWith("."))
.map((entry): LifecycleHook | null => {
const path = join(directory, entry);
const extension = extname(entry);
if (extension === ".ts") {
return {
id: path,
name: entry,
type: "typescript",
path,
};
}
if (extension === ".sh") {
return {
id: path,
name: entry,
type: "shell",
path,
};
}
return null;
})
.filter((hook): hook is LifecycleHook => hook !== null)
.sort((a, b) => a.path.localeCompare(b.path));
}
export type TurnContextCollectorOptions = {
// Turn/token/tool-call counts are cheap scalars needed regardless of
// consumers. The turns array (with truncated tool results) is the actual
// standing copy of recent history, so callers with nothing to hand it to
// (no lifecycle hook) can opt out of retaining it.
retainHistory?: boolean;
// Resuming a session should continue the persisted run.json turn count
// rather than restart it at zero.
initialTurnCount?: number;
};
export function createTurnContextCollector(
onTurn: (ctx: TurnContext) => void,
now: () => number = Date.now,
options: TurnContextCollectorOptions = {},
): {
observe(event: ReactorEmittedEvent): void;
getTurns(): TurnContext[];
getTurnCount(): number;
getTokenUsage(): TokenUsage;
// Usage reported for the most recent turn alone (not summed across turns),
// since a provider's per-turn `input` already reflects the whole resent
// conversation — the right basis for "how full is the context window now."
getLastTurnUsage(): TokenUsage;
getToolCallCount(): number;
} {
const retainHistory = options.retainHistory ?? true;
const turns: TurnContext[] = [];
let turnCount = options.initialTurnCount ?? 0;
let pending: PendingTurn | null = null;
let cycleStartedAt = now();
let tokenUsage: TokenUsage = { ...emptyUsage };
let lastTurnUsage: TokenUsage = { ...emptyUsage };
let toolCallCount = 0;
function completePending(): void {
if (pending === null) return;
if (pending.toolResults.length < pending.toolCalls.length) return;
const ctx: TurnContext = {
turnIndex: pending.turnIndex,
assistantTurn: pending.assistantTurn,
toolCalls: pending.toolCalls,
toolResults: retainHistory
? pending.toolResults.map(truncateToolResultForHookPayload)
: pending.toolResults,
usage: pending.usage,
source: pending.source,
durationMs: Math.max(0, now() - pending.startedAt),
};
turnCount++;
if (retainHistory) {
turns.push(ctx);
if (turns.length > RETAINED_TURN_CONTEXT_LIMIT) {
turns.splice(0, turns.length - RETAINED_TURN_CONTEXT_LIMIT);
}
}
pending = null;
cycleStartedAt = now();
onTurn(ctx);
}
return {
observe(event: ReactorEmittedEvent): void {
if (event.type === "inference.start" && pending === null) {
cycleStartedAt = now();
return;
}
if (onTurnBoundary(event)) {
const toolCalls = event.data.turn.content
.filter((block): block is Extract<typeof block, { type: "tool_call" }> => block.type === "tool_call")
.map((block): ToolCall => ({
id: block.id,
name: block.name,
arguments: block.arguments,
}));
toolCallCount += toolCalls.length;
tokenUsage = addUsage(tokenUsage, event.data.usage);
lastTurnUsage = event.data.usage;
pending = {
startedAt: cycleStartedAt,
turnIndex: turnCount,
assistantTurn: event.data.turn,
toolCalls,
toolResults: [],
usage: event.data.usage,
source: event.data.source,
};
completePending();
return;
}
if (event.type === "tool.done" && pending !== null) {
pending.toolResults.push(event.data.result);
completePending();
}
},
getTurns(): TurnContext[] {
return [...turns];
},
getTurnCount(): number {
return turnCount;
},
getTokenUsage(): TokenUsage {
return { ...tokenUsage };
},
getLastTurnUsage(): TokenUsage {
return { ...lastTurnUsage };
},
getToolCallCount(): number {
return toolCallCount;
},
};
}
export function createRunSummary(args: {
task: string;
status: "done" | "failed" | "cancelled";
startedAt: number;
finishedAt: number;
turnsUsed: number;
tokenUsage: TokenUsage;
turns: TurnContext[];
toolCallCount: number;
error?: string;
}): RunSummary {
return {
task: args.task,
status: args.status,
startedAt: args.startedAt,
finishedAt: args.finishedAt,
durationMs: Math.max(0, args.finishedAt - args.startedAt),
turnsUsed: args.turnsUsed,
tokenUsage: args.tokenUsage,
turns: args.turns,
toolCallCount: args.toolCallCount,
...(args.error !== undefined ? { error: args.error } : {}),
};
}
export function createLifecycleHookManager(args: {
hooks: LifecycleHook[];
onEvent?: (event: LifecycleHookEvent) => void;
logError?: (message: string) => void;
// Persisted enable/disable state, keyed by hook id. A hook absent here starts
// enabled, matching discovery's default before any state was ever saved.
initialEnabled?: Record<string, boolean>;
}): LifecycleHookManager {
const onEvent = args.onEvent ?? (() => {});
const logError = args.logError ?? (() => {});
const initialEnabled = args.initialEnabled ?? {};
const statuses = new Map<string, LifecycleHookStatus>();
for (const hook of args.hooks) {
statuses.set(hook.id, { ...hook, enabled: initialEnabled[hook.id] ?? true });
}
function snapshot(): LifecycleHookStatus[] {
return [...statuses.values()].map((status) => ({ ...status }));
}
function updateStatus(id: string, update: Partial<LifecycleHookStatus>): void {
const current = statuses.get(id);
if (current === undefined) return;
const next = { ...current, ...update };
statuses.set(id, next);
onEvent({ type: "hook.updated", hook: { ...next } });
}
function runHook(status: LifecycleHookStatus, kind: HookKind, payload: TurnContext | RunSummary): Promise<void> {
updateStatus(status.id, { lastFiredAt: Date.now(), lastKind: kind });
return runLifecycleHook(status, kind, payload).then(
(exitStatus) => updateStatus(status.id, { lastExitStatus: exitStatus }),
(err: unknown) => {
const message = err instanceof Error ? err.message : String(err);
logError(`Hook ${status.name} failed: ${message}`);
updateStatus(status.id, {
lastExitStatus: { code: null, signal: null, stderr: message },
});
},
);
}
function dispatch(kind: HookKind, payload: TurnContext | RunSummary): Promise<void> {
const pending: Promise<void>[] = [];
for (const status of statuses.values()) {
if (!status.enabled) continue;
pending.push(runHook(status, kind, payload));
}
return Promise.all(pending).then(() => {});
}
onEvent({ type: "hooks.loaded", hooks: snapshot() });
return {
getStatuses: snapshot,
setEnabled(id: string, enabled: boolean): void {
updateStatus(id, { enabled });
},
dispatchPostTurn(ctx: TurnContext): void {
void dispatch("postTurn", ctx);
},
dispatchPostRun(summary: RunSummary): Promise<void> {
return dispatch("postRun", summary);
},
};
}
async function runLifecycleHook(
hook: LifecycleHook,
kind: HookKind,
payload: TurnContext | RunSummary,
): Promise<HookExitStatus> {
const command = hook.type === "typescript"
? await createTypeScriptHookCommand(hook, kind)
: hookCommand(hook, kind);
const proc = Bun.spawn(command, {
stdin: "pipe",
stdout: "ignore",
stderr: "pipe",
});
proc.stdin.write(JSON.stringify(payload));
proc.stdin.end();
const [exitCode, stderr] = await Promise.all([
proc.exited,
new Response(proc.stderr).text(),
]);
return {
code: exitCode,
signal: proc.signalCode,
stderr,
};
}
function hookCommand(hook: LifecycleHook, kind: HookKind): string[] {
return ["sh", hook.path, kind];
}
async function createTypeScriptHookCommand(
hook: LifecycleHook,
kind: HookKind,
): Promise<string[]> {
const runnerDir = join(tmpdir(), `${COMMAND_NAME}-hook-runners`);
const runnerPath = join(runnerDir, `${hashHookRunner(hook.path, kind)}.ts`);
await mkdir(runnerDir, { recursive: true });
await writeFile(runnerPath, typeScriptHookRunnerSource(hook.path, kind));
return ["bun", runnerPath];
}
function typeScriptHookRunnerSource(path: string, kind: HookKind): string {
const hookURL = pathToFileURL(path).href;
return [
`import * as hook from ${JSON.stringify(hookURL)};`,
"const chunks = [];",
"for await (const chunk of Bun.stdin.stream()) chunks.push(chunk);",
"const payload = new TextDecoder().decode(Buffer.concat(chunks));",
`const selected = hook[${JSON.stringify(kind)}];`,
"if (typeof selected !== 'function') process.exit(0);",
"await selected(JSON.parse(payload));",
"",
].join("\n");
}
function hashHookRunner(path: string, kind: HookKind): string {
return createHash("sha256").update(`${path}:${kind}`).digest("hex").slice(0, 24);
}
function truncateToolResultForHookPayload(result: ToolResult): ToolResult {
const raw = typeof result.content === "string" ? result.content : JSON.stringify(result.content);
const content = raw.length <= HOOK_PAYLOAD_TOOL_RESULT_CHARS
? raw
: `…[${raw.length - HOOK_PAYLOAD_TOOL_RESULT_CHARS} chars omitted]${raw.slice(raw.length - HOOK_PAYLOAD_TOOL_RESULT_CHARS)}`;
return {
callId: result.callId,
content,
...(result.isError !== undefined ? { isError: result.isError } : {}),
...(result.pendingMarker !== undefined ? { pendingMarker: result.pendingMarker } : {}),
};
}
function addUsage(a: TokenUsage, b: TokenUsage): TokenUsage {
return {
input: a.input + b.input,
output: a.output + b.output,
cacheRead: a.cacheRead + b.cacheRead,
cacheWrite: a.cacheWrite + b.cacheWrite,
thinking: a.thinking + b.thinking,
};
}
function isENOENT(err: unknown): boolean {
return (
typeof err === "object" &&
err !== null &&
"code" in err &&
(err as { code?: unknown }).code === "ENOENT"
);
}