@mariozechner/pi-coding-agent 0.16.0 → 0.18.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/CHANGELOG.md +38 -0
- package/README.md +58 -1
- package/dist/cli/args.d.ts +1 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +5 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -0
- package/dist/config.js.map +1 -1
- package/dist/core/agent-session.d.ts +30 -2
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +181 -21
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/compaction.d.ts +30 -5
- package/dist/core/compaction.d.ts.map +1 -1
- package/dist/core/compaction.js +194 -61
- package/dist/core/compaction.js.map +1 -1
- package/dist/core/hooks/index.d.ts +5 -0
- package/dist/core/hooks/index.d.ts.map +1 -0
- package/dist/core/hooks/index.js +4 -0
- package/dist/core/hooks/index.js.map +1 -0
- package/dist/core/hooks/loader.d.ts +56 -0
- package/dist/core/hooks/loader.d.ts.map +1 -0
- package/dist/core/hooks/loader.js +158 -0
- package/dist/core/hooks/loader.js.map +1 -0
- package/dist/core/hooks/runner.d.ts +69 -0
- package/dist/core/hooks/runner.d.ts.map +1 -0
- package/dist/core/hooks/runner.js +203 -0
- package/dist/core/hooks/runner.js.map +1 -0
- package/dist/core/hooks/tool-wrapper.d.ts +16 -0
- package/dist/core/hooks/tool-wrapper.d.ts.map +1 -0
- package/dist/core/hooks/tool-wrapper.js +71 -0
- package/dist/core/hooks/tool-wrapper.js.map +1 -0
- package/dist/core/hooks/types.d.ts +220 -0
- package/dist/core/hooks/types.d.ts.map +1 -0
- package/dist/core/hooks/types.js +8 -0
- package/dist/core/hooks/types.js.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/session-manager.d.ts +10 -3
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +78 -28
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +6 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +14 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +5 -3
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/truncate.d.ts +6 -2
- package/dist/core/tools/truncate.d.ts.map +1 -1
- package/dist/core/tools/truncate.js +11 -1
- package/dist/core/tools/truncate.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +23 -12
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/bash-execution.d.ts +1 -0
- package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/bash-execution.js +17 -6
- package/dist/modes/interactive/components/bash-execution.js.map +1 -1
- package/dist/modes/interactive/components/hook-input.d.ts +12 -0
- package/dist/modes/interactive/components/hook-input.d.ts.map +1 -0
- package/dist/modes/interactive/components/hook-input.js +46 -0
- package/dist/modes/interactive/components/hook-input.js.map +1 -0
- package/dist/modes/interactive/components/hook-selector.d.ts +16 -0
- package/dist/modes/interactive/components/hook-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/hook-selector.js +76 -0
- package/dist/modes/interactive/components/hook-selector.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +12 -7
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +37 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +190 -7
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +15 -0
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts +2 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +118 -3
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +41 -0
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/docs/compaction.md +519 -0
- package/docs/hooks.md +609 -0
- package/docs/rpc.md +870 -0
- package/docs/session.md +89 -0
- package/docs/theme.md +586 -0
- package/docs/truncation.md +235 -0
- package/docs/undercompaction.md +313 -0
- package/package.json +18 -6
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook loader - loads TypeScript hook modules using jiti.
|
|
3
|
+
*/
|
|
4
|
+
import * as fs from "node:fs";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import { createJiti } from "jiti";
|
|
8
|
+
import { getAgentDir } from "../../config.js";
|
|
9
|
+
/**
|
|
10
|
+
* Expand path with ~ support.
|
|
11
|
+
*/
|
|
12
|
+
function expandPath(p) {
|
|
13
|
+
if (p.startsWith("~/")) {
|
|
14
|
+
return path.join(os.homedir(), p.slice(2));
|
|
15
|
+
}
|
|
16
|
+
if (p.startsWith("~")) {
|
|
17
|
+
return path.join(os.homedir(), p.slice(1));
|
|
18
|
+
}
|
|
19
|
+
return p;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve hook path.
|
|
23
|
+
* - Absolute paths used as-is
|
|
24
|
+
* - Paths starting with ~ expanded to home directory
|
|
25
|
+
* - Relative paths resolved from cwd
|
|
26
|
+
*/
|
|
27
|
+
function resolveHookPath(hookPath, cwd) {
|
|
28
|
+
const expanded = expandPath(hookPath);
|
|
29
|
+
if (path.isAbsolute(expanded)) {
|
|
30
|
+
return expanded;
|
|
31
|
+
}
|
|
32
|
+
// Relative paths resolved from cwd
|
|
33
|
+
return path.resolve(cwd, expanded);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a HookAPI instance that collects handlers.
|
|
37
|
+
* Returns the API and a function to set the send handler later.
|
|
38
|
+
*/
|
|
39
|
+
function createHookAPI(handlers) {
|
|
40
|
+
let sendHandler = () => {
|
|
41
|
+
// Default no-op until mode sets the handler
|
|
42
|
+
};
|
|
43
|
+
const api = {
|
|
44
|
+
on(event, handler) {
|
|
45
|
+
const list = handlers.get(event) ?? [];
|
|
46
|
+
list.push(handler);
|
|
47
|
+
handlers.set(event, list);
|
|
48
|
+
},
|
|
49
|
+
send(text, attachments) {
|
|
50
|
+
sendHandler(text, attachments);
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
api,
|
|
55
|
+
setSendHandler: (handler) => {
|
|
56
|
+
sendHandler = handler;
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Load a single hook module using jiti.
|
|
62
|
+
*/
|
|
63
|
+
async function loadHook(hookPath, cwd) {
|
|
64
|
+
const resolvedPath = resolveHookPath(hookPath, cwd);
|
|
65
|
+
try {
|
|
66
|
+
// Create jiti instance for TypeScript/ESM loading
|
|
67
|
+
const jiti = createJiti(import.meta.url);
|
|
68
|
+
// Import the module
|
|
69
|
+
const module = await jiti.import(resolvedPath, { default: true });
|
|
70
|
+
const factory = module;
|
|
71
|
+
if (typeof factory !== "function") {
|
|
72
|
+
return { hook: null, error: "Hook must export a default function" };
|
|
73
|
+
}
|
|
74
|
+
// Create handlers map and API
|
|
75
|
+
const handlers = new Map();
|
|
76
|
+
const { api, setSendHandler } = createHookAPI(handlers);
|
|
77
|
+
// Call factory to register handlers
|
|
78
|
+
factory(api);
|
|
79
|
+
return {
|
|
80
|
+
hook: { path: hookPath, resolvedPath, handlers, setSendHandler },
|
|
81
|
+
error: null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
86
|
+
return { hook: null, error: `Failed to load hook: ${message}` };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Load all hooks from configuration.
|
|
91
|
+
* @param paths - Array of hook file paths
|
|
92
|
+
* @param cwd - Current working directory for resolving relative paths
|
|
93
|
+
*/
|
|
94
|
+
export async function loadHooks(paths, cwd) {
|
|
95
|
+
const hooks = [];
|
|
96
|
+
const errors = [];
|
|
97
|
+
for (const hookPath of paths) {
|
|
98
|
+
const { hook, error } = await loadHook(hookPath, cwd);
|
|
99
|
+
if (error) {
|
|
100
|
+
errors.push({ path: hookPath, error });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (hook) {
|
|
104
|
+
hooks.push(hook);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { hooks, errors };
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Discover hook files from a directory.
|
|
111
|
+
* Returns all .ts files in the directory (non-recursive).
|
|
112
|
+
*/
|
|
113
|
+
function discoverHooksInDir(dir) {
|
|
114
|
+
if (!fs.existsSync(dir)) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
119
|
+
return entries.filter((e) => e.isFile() && e.name.endsWith(".ts")).map((e) => path.join(dir, e.name));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Discover and load hooks from standard locations:
|
|
127
|
+
* 1. ~/.pi/agent/hooks/*.ts (global)
|
|
128
|
+
* 2. cwd/.pi/hooks/*.ts (project-local)
|
|
129
|
+
*
|
|
130
|
+
* Plus any explicitly configured paths from settings.
|
|
131
|
+
*
|
|
132
|
+
* @param configuredPaths - Explicit paths from settings.json
|
|
133
|
+
* @param cwd - Current working directory
|
|
134
|
+
*/
|
|
135
|
+
export async function discoverAndLoadHooks(configuredPaths, cwd) {
|
|
136
|
+
const allPaths = [];
|
|
137
|
+
const seen = new Set();
|
|
138
|
+
// Helper to add paths without duplicates
|
|
139
|
+
const addPaths = (paths) => {
|
|
140
|
+
for (const p of paths) {
|
|
141
|
+
const resolved = path.resolve(p);
|
|
142
|
+
if (!seen.has(resolved)) {
|
|
143
|
+
seen.add(resolved);
|
|
144
|
+
allPaths.push(p);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
// 1. Global hooks: ~/.pi/agent/hooks/
|
|
149
|
+
const globalHooksDir = path.join(getAgentDir(), "hooks");
|
|
150
|
+
addPaths(discoverHooksInDir(globalHooksDir));
|
|
151
|
+
// 2. Project-local hooks: cwd/.pi/hooks/
|
|
152
|
+
const localHooksDir = path.join(cwd, ".pi", "hooks");
|
|
153
|
+
addPaths(discoverHooksInDir(localHooksDir));
|
|
154
|
+
// 3. Explicitly configured paths (can override/add)
|
|
155
|
+
addPaths(configuredPaths.map((p) => resolveHookPath(p, cwd)));
|
|
156
|
+
return loadHooks(allPaths, cwd);
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../../../src/core/hooks/loader.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAElC,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAClC,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAqC9C;;GAEG;AACH,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:fs\";\nimport * as os from \"node:os\";\nimport * as path from \"node:path\";\nimport type { Attachment } from \"@mariozechner/pi-agent-core\";\nimport { createJiti } from \"jiti\";\nimport { getAgentDir } from \"../../config.js\";\nimport type { HookAPI, HookFactory } from \"./types.js\";\n\n/**\n * Generic handler function type.\n */\ntype HandlerFn = (...args: unknown[]) => Promise<unknown>;\n\n/**\n * Send handler type for pi.send().\n */\nexport type SendHandler = (text: string, attachments?: Attachment[]) => void;\n\n/**\n * Registered handlers for a loaded hook.\n */\nexport interface LoadedHook {\n\t/** Original path from config */\n\tpath: string;\n\t/** Resolved absolute path */\n\tresolvedPath: string;\n\t/** Map of event type to handler functions */\n\thandlers: Map<string, HandlerFn[]>;\n\t/** Set the send handler for this hook's pi.send() */\n\tsetSendHandler: (handler: SendHandler) => void;\n}\n\n/**\n * Result of loading hooks.\n */\nexport interface LoadHooksResult {\n\t/** Successfully loaded hooks */\n\thooks: LoadedHook[];\n\t/** Errors encountered during loading */\n\terrors: Array<{ path: string; error: string }>;\n}\n\n/**\n * Expand path with ~ support.\n */\nfunction expandPath(p: string): string {\n\tif (p.startsWith(\"~/\")) {\n\t\treturn path.join(os.homedir(), p.slice(2));\n\t}\n\tif (p.startsWith(\"~\")) {\n\t\treturn path.join(os.homedir(), p.slice(1));\n\t}\n\treturn p;\n}\n\n/**\n * Resolve hook path.\n * - Absolute paths used as-is\n * - Paths starting with ~ expanded to home directory\n * - Relative paths resolved from cwd\n */\nfunction resolveHookPath(hookPath: string, cwd: string): string {\n\tconst expanded = expandPath(hookPath);\n\n\tif (path.isAbsolute(expanded)) {\n\t\treturn expanded;\n\t}\n\n\t// Relative paths resolved from cwd\n\treturn path.resolve(cwd, expanded);\n}\n\n/**\n * Create a HookAPI instance that collects handlers.\n * Returns the API and a function to set the send handler later.\n */\nfunction createHookAPI(handlers: Map<string, HandlerFn[]>): {\n\tapi: HookAPI;\n\tsetSendHandler: (handler: SendHandler) => void;\n} {\n\tlet sendHandler: SendHandler = () => {\n\t\t// Default no-op until mode sets the handler\n\t};\n\n\tconst api: HookAPI = {\n\t\ton(event: string, handler: HandlerFn): void {\n\t\t\tconst list = handlers.get(event) ?? [];\n\t\t\tlist.push(handler);\n\t\t\thandlers.set(event, list);\n\t\t},\n\t\tsend(text: string, attachments?: Attachment[]): void {\n\t\t\tsendHandler(text, attachments);\n\t\t},\n\t} as HookAPI;\n\n\treturn {\n\t\tapi,\n\t\tsetSendHandler: (handler: SendHandler) => {\n\t\t\tsendHandler = handler;\n\t\t},\n\t};\n}\n\n/**\n * Load a single hook module using jiti.\n */\nasync function loadHook(hookPath: string, cwd: string): Promise<{ hook: LoadedHook | null; error: string | null }> {\n\tconst resolvedPath = resolveHookPath(hookPath, cwd);\n\n\ttry {\n\t\t// Create jiti instance for TypeScript/ESM loading\n\t\tconst jiti = createJiti(import.meta.url);\n\n\t\t// Import the module\n\t\tconst module = await jiti.import(resolvedPath, { default: true });\n\t\tconst factory = module as HookFactory;\n\n\t\tif (typeof factory !== \"function\") {\n\t\t\treturn { hook: null, error: \"Hook must export a default function\" };\n\t\t}\n\n\t\t// Create handlers map and API\n\t\tconst handlers = new Map<string, HandlerFn[]>();\n\t\tconst { api, setSendHandler } = createHookAPI(handlers);\n\n\t\t// Call factory to register handlers\n\t\tfactory(api);\n\n\t\treturn {\n\t\t\thook: { path: hookPath, resolvedPath, handlers, setSendHandler },\n\t\t\terror: null,\n\t\t};\n\t} catch (err) {\n\t\tconst message = err instanceof Error ? err.message : String(err);\n\t\treturn { hook: null, error: `Failed to load hook: ${message}` };\n\t}\n}\n\n/**\n * Load all hooks from configuration.\n * @param paths - Array of hook file paths\n * @param cwd - Current working directory for resolving relative paths\n */\nexport async function loadHooks(paths: string[], cwd: string): Promise<LoadHooksResult> {\n\tconst hooks: LoadedHook[] = [];\n\tconst errors: Array<{ path: string; error: string }> = [];\n\n\tfor (const hookPath of paths) {\n\t\tconst { hook, error } = await loadHook(hookPath, cwd);\n\n\t\tif (error) {\n\t\t\terrors.push({ path: hookPath, error });\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (hook) {\n\t\t\thooks.push(hook);\n\t\t}\n\t}\n\n\treturn { hooks, errors };\n}\n\n/**\n * Discover hook files from a directory.\n * Returns all .ts files in the directory (non-recursive).\n */\nfunction discoverHooksInDir(dir: string): string[] {\n\tif (!fs.existsSync(dir)) {\n\t\treturn [];\n\t}\n\n\ttry {\n\t\tconst entries = fs.readdirSync(dir, { withFileTypes: true });\n\t\treturn entries.filter((e) => e.isFile() && e.name.endsWith(\".ts\")).map((e) => path.join(dir, e.name));\n\t} catch {\n\t\treturn [];\n\t}\n}\n\n/**\n * Discover and load hooks from standard locations:\n * 1. ~/.pi/agent/hooks/*.ts (global)\n * 2. cwd/.pi/hooks/*.ts (project-local)\n *\n * Plus any explicitly configured paths from settings.\n *\n * @param configuredPaths - Explicit paths from settings.json\n * @param cwd - Current working directory\n */\nexport async function discoverAndLoadHooks(configuredPaths: string[], cwd: string): Promise<LoadHooksResult> {\n\tconst allPaths: string[] = [];\n\tconst seen = new Set<string>();\n\n\t// Helper to add paths without duplicates\n\tconst addPaths = (paths: string[]) => {\n\t\tfor (const p of paths) {\n\t\t\tconst resolved = path.resolve(p);\n\t\t\tif (!seen.has(resolved)) {\n\t\t\t\tseen.add(resolved);\n\t\t\t\tallPaths.push(p);\n\t\t\t}\n\t\t}\n\t};\n\n\t// 1. Global hooks: ~/.pi/agent/hooks/\n\tconst globalHooksDir = path.join(getAgentDir(), \"hooks\");\n\taddPaths(discoverHooksInDir(globalHooksDir));\n\n\t// 2. Project-local hooks: cwd/.pi/hooks/\n\tconst localHooksDir = path.join(cwd, \".pi\", \"hooks\");\n\taddPaths(discoverHooksInDir(localHooksDir));\n\n\t// 3. Explicitly configured paths (can override/add)\n\taddPaths(configuredPaths.map((p) => resolveHookPath(p, cwd)));\n\n\treturn loadHooks(allPaths, cwd);\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook runner - executes hooks and manages their lifecycle.
|
|
3
|
+
*/
|
|
4
|
+
import type { LoadedHook, SendHandler } from "./loader.js";
|
|
5
|
+
import type { BranchEventResult, HookError, HookEvent, HookUIContext, ToolCallEvent, ToolCallEventResult, ToolResultEventResult } from "./types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Listener for hook errors.
|
|
8
|
+
*/
|
|
9
|
+
export type HookErrorListener = (error: HookError) => void;
|
|
10
|
+
/**
|
|
11
|
+
* HookRunner executes hooks and manages event emission.
|
|
12
|
+
*/
|
|
13
|
+
export declare class HookRunner {
|
|
14
|
+
private hooks;
|
|
15
|
+
private uiContext;
|
|
16
|
+
private hasUI;
|
|
17
|
+
private cwd;
|
|
18
|
+
private sessionFile;
|
|
19
|
+
private timeout;
|
|
20
|
+
private errorListeners;
|
|
21
|
+
constructor(hooks: LoadedHook[], cwd: string, timeout?: number);
|
|
22
|
+
/**
|
|
23
|
+
* Set the UI context for hooks.
|
|
24
|
+
* Call this when the mode initializes and UI is available.
|
|
25
|
+
*/
|
|
26
|
+
setUIContext(uiContext: HookUIContext, hasUI: boolean): void;
|
|
27
|
+
/**
|
|
28
|
+
* Get the paths of all loaded hooks.
|
|
29
|
+
*/
|
|
30
|
+
getHookPaths(): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Set the session file path.
|
|
33
|
+
*/
|
|
34
|
+
setSessionFile(sessionFile: string | null): void;
|
|
35
|
+
/**
|
|
36
|
+
* Set the send handler for all hooks' pi.send().
|
|
37
|
+
* Call this when the mode initializes.
|
|
38
|
+
*/
|
|
39
|
+
setSendHandler(handler: SendHandler): void;
|
|
40
|
+
/**
|
|
41
|
+
* Subscribe to hook errors.
|
|
42
|
+
* @returns Unsubscribe function
|
|
43
|
+
*/
|
|
44
|
+
onError(listener: HookErrorListener): () => void;
|
|
45
|
+
/**
|
|
46
|
+
* Emit an error to all listeners.
|
|
47
|
+
*/
|
|
48
|
+
private emitError;
|
|
49
|
+
/**
|
|
50
|
+
* Check if any hooks have handlers for the given event type.
|
|
51
|
+
*/
|
|
52
|
+
hasHandlers(eventType: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Create the event context for handlers.
|
|
55
|
+
*/
|
|
56
|
+
private createContext;
|
|
57
|
+
/**
|
|
58
|
+
* Emit an event to all hooks.
|
|
59
|
+
* Returns the result from branch/tool_result events (if any handler returns one).
|
|
60
|
+
*/
|
|
61
|
+
emit(event: HookEvent): Promise<BranchEventResult | ToolResultEventResult | undefined>;
|
|
62
|
+
/**
|
|
63
|
+
* Emit a tool_call event to all hooks.
|
|
64
|
+
* No timeout - user prompts can take as long as needed.
|
|
65
|
+
* Errors are thrown (not swallowed) so caller can block on failure.
|
|
66
|
+
*/
|
|
67
|
+
emitToolCall(event: ToolCallEvent): Promise<ToolCallEventResult | undefined>;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../../../src/core/hooks/runner.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,KAAK,EACX,iBAAiB,EAEjB,SAAS,EACT,SAAS,EAET,aAAa,EACb,aAAa,EACb,mBAAmB,EACnB,qBAAqB,EACrB,MAAM,YAAY,CAAC;AAOpB;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;AAoD3D;;GAEG;AACH,qBAAa,UAAU;IACtB,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,SAAS,CAAgB;IACjC,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAqC;IAE3D,YAAY,KAAK,EAAE,UAAU,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,MAAwB,EAO9E;IAED;;;OAGG;IACH,YAAY,CAAC,SAAS,EAAE,aAAa,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI,CAG3D;IAED;;OAEG;IACH,YAAY,IAAI,MAAM,EAAE,CAEvB;IAED;;OAEG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAE/C;IAED;;;OAGG;IACH,cAAc,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,CAIzC;IAED;;;OAGG;IACH,OAAO,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,IAAI,CAG/C;IAED;;OAEG;IACH,OAAO,CAAC,SAAS;IAMjB;;OAEG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAQtC;IAED;;OAEG;IACH,OAAO,CAAC,aAAa;IAUrB;;;OAGG;IACG,IAAI,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,iBAAiB,GAAG,qBAAqB,GAAG,SAAS,CAAC,CAmC3F;IAED;;;;OAIG;IACG,YAAY,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,mBAAmB,GAAG,SAAS,CAAC,CAuBjF;CACD","sourcesContent":["/**\n * Hook runner - executes hooks and manages their lifecycle.\n */\n\nimport { spawn } from \"node:child_process\";\nimport type { LoadedHook, SendHandler } from \"./loader.js\";\nimport type {\n\tBranchEventResult,\n\tExecResult,\n\tHookError,\n\tHookEvent,\n\tHookEventContext,\n\tHookUIContext,\n\tToolCallEvent,\n\tToolCallEventResult,\n\tToolResultEventResult,\n} from \"./types.js\";\n\n/**\n * Default timeout for hook execution (30 seconds).\n */\nconst DEFAULT_TIMEOUT = 30000;\n\n/**\n * Listener for hook errors.\n */\nexport type HookErrorListener = (error: HookError) => void;\n\n/**\n * Execute a command and return stdout/stderr/code.\n */\nasync function exec(command: string, args: string[], cwd: string): Promise<ExecResult> {\n\treturn new Promise((resolve) => {\n\t\tconst proc = spawn(command, args, { cwd, shell: false });\n\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\n\t\tproc.stdout?.on(\"data\", (data) => {\n\t\t\tstdout += data.toString();\n\t\t});\n\n\t\tproc.stderr?.on(\"data\", (data) => {\n\t\t\tstderr += data.toString();\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tresolve({ stdout, stderr, code: code ?? 0 });\n\t\t});\n\n\t\tproc.on(\"error\", (_err) => {\n\t\t\tresolve({ stdout, stderr, code: 1 });\n\t\t});\n\t});\n}\n\n/**\n * Create a promise that rejects after a timeout.\n */\nfunction createTimeout(ms: number): { promise: Promise<never>; clear: () => void } {\n\tlet timeoutId: NodeJS.Timeout;\n\tconst promise = new Promise<never>((_, reject) => {\n\t\ttimeoutId = setTimeout(() => reject(new Error(`Hook timed out after ${ms}ms`)), ms);\n\t});\n\treturn {\n\t\tpromise,\n\t\tclear: () => clearTimeout(timeoutId),\n\t};\n}\n\n/** No-op UI context used when no UI is available */\nconst noOpUIContext: HookUIContext = {\n\tselect: async () => null,\n\tconfirm: async () => false,\n\tinput: async () => null,\n\tnotify: () => {},\n};\n\n/**\n * HookRunner executes hooks and manages event emission.\n */\nexport class HookRunner {\n\tprivate hooks: LoadedHook[];\n\tprivate uiContext: HookUIContext;\n\tprivate hasUI: boolean;\n\tprivate cwd: string;\n\tprivate sessionFile: string | null;\n\tprivate timeout: number;\n\tprivate errorListeners: Set<HookErrorListener> = new Set();\n\n\tconstructor(hooks: LoadedHook[], cwd: string, timeout: number = DEFAULT_TIMEOUT) {\n\t\tthis.hooks = hooks;\n\t\tthis.uiContext = noOpUIContext;\n\t\tthis.hasUI = false;\n\t\tthis.cwd = cwd;\n\t\tthis.sessionFile = null;\n\t\tthis.timeout = timeout;\n\t}\n\n\t/**\n\t * Set the UI context for hooks.\n\t * Call this when the mode initializes and UI is available.\n\t */\n\tsetUIContext(uiContext: HookUIContext, hasUI: boolean): void {\n\t\tthis.uiContext = uiContext;\n\t\tthis.hasUI = hasUI;\n\t}\n\n\t/**\n\t * Get the paths of all loaded hooks.\n\t */\n\tgetHookPaths(): string[] {\n\t\treturn this.hooks.map((h) => h.path);\n\t}\n\n\t/**\n\t * Set the session file path.\n\t */\n\tsetSessionFile(sessionFile: string | null): void {\n\t\tthis.sessionFile = sessionFile;\n\t}\n\n\t/**\n\t * Set the send handler for all hooks' pi.send().\n\t * Call this when the mode initializes.\n\t */\n\tsetSendHandler(handler: SendHandler): void {\n\t\tfor (const hook of this.hooks) {\n\t\t\thook.setSendHandler(handler);\n\t\t}\n\t}\n\n\t/**\n\t * Subscribe to hook errors.\n\t * @returns Unsubscribe function\n\t */\n\tonError(listener: HookErrorListener): () => void {\n\t\tthis.errorListeners.add(listener);\n\t\treturn () => this.errorListeners.delete(listener);\n\t}\n\n\t/**\n\t * Emit an error to all listeners.\n\t */\n\tprivate emitError(error: HookError): void {\n\t\tfor (const listener of this.errorListeners) {\n\t\t\tlistener(error);\n\t\t}\n\t}\n\n\t/**\n\t * Check if any hooks have handlers for the given event type.\n\t */\n\thasHandlers(eventType: string): boolean {\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(eventType);\n\t\t\tif (handlers && handlers.length > 0) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Create the event context for handlers.\n\t */\n\tprivate createContext(): HookEventContext {\n\t\treturn {\n\t\t\texec: (command: string, args: string[]) => exec(command, args, this.cwd),\n\t\t\tui: this.uiContext,\n\t\t\thasUI: this.hasUI,\n\t\t\tcwd: this.cwd,\n\t\t\tsessionFile: this.sessionFile,\n\t\t};\n\t}\n\n\t/**\n\t * Emit an event to all hooks.\n\t * Returns the result from branch/tool_result events (if any handler returns one).\n\t */\n\tasync emit(event: HookEvent): Promise<BranchEventResult | ToolResultEventResult | undefined> {\n\t\tconst ctx = this.createContext();\n\t\tlet result: BranchEventResult | ToolResultEventResult | undefined;\n\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(event.type);\n\t\t\tif (!handlers || handlers.length === 0) continue;\n\n\t\t\tfor (const handler of handlers) {\n\t\t\t\ttry {\n\t\t\t\t\tconst timeout = createTimeout(this.timeout);\n\t\t\t\t\tconst handlerResult = await Promise.race([handler(event, ctx), timeout.promise]);\n\t\t\t\t\ttimeout.clear();\n\n\t\t\t\t\t// For branch events, capture the result\n\t\t\t\t\tif (event.type === \"branch\" && handlerResult) {\n\t\t\t\t\t\tresult = handlerResult as BranchEventResult;\n\t\t\t\t\t}\n\n\t\t\t\t\t// For tool_result events, capture the result\n\t\t\t\t\tif (event.type === \"tool_result\" && handlerResult) {\n\t\t\t\t\t\tresult = handlerResult as ToolResultEventResult;\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconst message = err instanceof Error ? err.message : String(err);\n\t\t\t\t\tthis.emitError({\n\t\t\t\t\t\thookPath: hook.path,\n\t\t\t\t\t\tevent: event.type,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Emit a tool_call event to all hooks.\n\t * No timeout - user prompts can take as long as needed.\n\t * Errors are thrown (not swallowed) so caller can block on failure.\n\t */\n\tasync emitToolCall(event: ToolCallEvent): Promise<ToolCallEventResult | undefined> {\n\t\tconst ctx = this.createContext();\n\t\tlet result: ToolCallEventResult | undefined;\n\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(\"tool_call\");\n\t\t\tif (!handlers || handlers.length === 0) continue;\n\n\t\t\tfor (const handler of handlers) {\n\t\t\t\t// No timeout - let user take their time\n\t\t\t\tconst handlerResult = await handler(event, ctx);\n\n\t\t\t\tif (handlerResult) {\n\t\t\t\t\tresult = handlerResult as ToolCallEventResult;\n\t\t\t\t\t// If blocked, stop processing further hooks\n\t\t\t\t\tif (result.block) {\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook runner - executes hooks and manages their lifecycle.
|
|
3
|
+
*/
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
/**
|
|
6
|
+
* Default timeout for hook execution (30 seconds).
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_TIMEOUT = 30000;
|
|
9
|
+
/**
|
|
10
|
+
* Execute a command and return stdout/stderr/code.
|
|
11
|
+
*/
|
|
12
|
+
async function exec(command, args, cwd) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
const proc = spawn(command, args, { cwd, shell: false });
|
|
15
|
+
let stdout = "";
|
|
16
|
+
let stderr = "";
|
|
17
|
+
proc.stdout?.on("data", (data) => {
|
|
18
|
+
stdout += data.toString();
|
|
19
|
+
});
|
|
20
|
+
proc.stderr?.on("data", (data) => {
|
|
21
|
+
stderr += data.toString();
|
|
22
|
+
});
|
|
23
|
+
proc.on("close", (code) => {
|
|
24
|
+
resolve({ stdout, stderr, code: code ?? 0 });
|
|
25
|
+
});
|
|
26
|
+
proc.on("error", (_err) => {
|
|
27
|
+
resolve({ stdout, stderr, code: 1 });
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a promise that rejects after a timeout.
|
|
33
|
+
*/
|
|
34
|
+
function createTimeout(ms) {
|
|
35
|
+
let timeoutId;
|
|
36
|
+
const promise = new Promise((_, reject) => {
|
|
37
|
+
timeoutId = setTimeout(() => reject(new Error(`Hook timed out after ${ms}ms`)), ms);
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
promise,
|
|
41
|
+
clear: () => clearTimeout(timeoutId),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** No-op UI context used when no UI is available */
|
|
45
|
+
const noOpUIContext = {
|
|
46
|
+
select: async () => null,
|
|
47
|
+
confirm: async () => false,
|
|
48
|
+
input: async () => null,
|
|
49
|
+
notify: () => { },
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* HookRunner executes hooks and manages event emission.
|
|
53
|
+
*/
|
|
54
|
+
export class HookRunner {
|
|
55
|
+
hooks;
|
|
56
|
+
uiContext;
|
|
57
|
+
hasUI;
|
|
58
|
+
cwd;
|
|
59
|
+
sessionFile;
|
|
60
|
+
timeout;
|
|
61
|
+
errorListeners = new Set();
|
|
62
|
+
constructor(hooks, cwd, timeout = DEFAULT_TIMEOUT) {
|
|
63
|
+
this.hooks = hooks;
|
|
64
|
+
this.uiContext = noOpUIContext;
|
|
65
|
+
this.hasUI = false;
|
|
66
|
+
this.cwd = cwd;
|
|
67
|
+
this.sessionFile = null;
|
|
68
|
+
this.timeout = timeout;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Set the UI context for hooks.
|
|
72
|
+
* Call this when the mode initializes and UI is available.
|
|
73
|
+
*/
|
|
74
|
+
setUIContext(uiContext, hasUI) {
|
|
75
|
+
this.uiContext = uiContext;
|
|
76
|
+
this.hasUI = hasUI;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get the paths of all loaded hooks.
|
|
80
|
+
*/
|
|
81
|
+
getHookPaths() {
|
|
82
|
+
return this.hooks.map((h) => h.path);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Set the session file path.
|
|
86
|
+
*/
|
|
87
|
+
setSessionFile(sessionFile) {
|
|
88
|
+
this.sessionFile = sessionFile;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Set the send handler for all hooks' pi.send().
|
|
92
|
+
* Call this when the mode initializes.
|
|
93
|
+
*/
|
|
94
|
+
setSendHandler(handler) {
|
|
95
|
+
for (const hook of this.hooks) {
|
|
96
|
+
hook.setSendHandler(handler);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Subscribe to hook errors.
|
|
101
|
+
* @returns Unsubscribe function
|
|
102
|
+
*/
|
|
103
|
+
onError(listener) {
|
|
104
|
+
this.errorListeners.add(listener);
|
|
105
|
+
return () => this.errorListeners.delete(listener);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Emit an error to all listeners.
|
|
109
|
+
*/
|
|
110
|
+
emitError(error) {
|
|
111
|
+
for (const listener of this.errorListeners) {
|
|
112
|
+
listener(error);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Check if any hooks have handlers for the given event type.
|
|
117
|
+
*/
|
|
118
|
+
hasHandlers(eventType) {
|
|
119
|
+
for (const hook of this.hooks) {
|
|
120
|
+
const handlers = hook.handlers.get(eventType);
|
|
121
|
+
if (handlers && handlers.length > 0) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Create the event context for handlers.
|
|
129
|
+
*/
|
|
130
|
+
createContext() {
|
|
131
|
+
return {
|
|
132
|
+
exec: (command, args) => exec(command, args, this.cwd),
|
|
133
|
+
ui: this.uiContext,
|
|
134
|
+
hasUI: this.hasUI,
|
|
135
|
+
cwd: this.cwd,
|
|
136
|
+
sessionFile: this.sessionFile,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Emit an event to all hooks.
|
|
141
|
+
* Returns the result from branch/tool_result events (if any handler returns one).
|
|
142
|
+
*/
|
|
143
|
+
async emit(event) {
|
|
144
|
+
const ctx = this.createContext();
|
|
145
|
+
let result;
|
|
146
|
+
for (const hook of this.hooks) {
|
|
147
|
+
const handlers = hook.handlers.get(event.type);
|
|
148
|
+
if (!handlers || handlers.length === 0)
|
|
149
|
+
continue;
|
|
150
|
+
for (const handler of handlers) {
|
|
151
|
+
try {
|
|
152
|
+
const timeout = createTimeout(this.timeout);
|
|
153
|
+
const handlerResult = await Promise.race([handler(event, ctx), timeout.promise]);
|
|
154
|
+
timeout.clear();
|
|
155
|
+
// For branch events, capture the result
|
|
156
|
+
if (event.type === "branch" && handlerResult) {
|
|
157
|
+
result = handlerResult;
|
|
158
|
+
}
|
|
159
|
+
// For tool_result events, capture the result
|
|
160
|
+
if (event.type === "tool_result" && handlerResult) {
|
|
161
|
+
result = handlerResult;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
166
|
+
this.emitError({
|
|
167
|
+
hookPath: hook.path,
|
|
168
|
+
event: event.type,
|
|
169
|
+
error: message,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Emit a tool_call event to all hooks.
|
|
178
|
+
* No timeout - user prompts can take as long as needed.
|
|
179
|
+
* Errors are thrown (not swallowed) so caller can block on failure.
|
|
180
|
+
*/
|
|
181
|
+
async emitToolCall(event) {
|
|
182
|
+
const ctx = this.createContext();
|
|
183
|
+
let result;
|
|
184
|
+
for (const hook of this.hooks) {
|
|
185
|
+
const handlers = hook.handlers.get("tool_call");
|
|
186
|
+
if (!handlers || handlers.length === 0)
|
|
187
|
+
continue;
|
|
188
|
+
for (const handler of handlers) {
|
|
189
|
+
// No timeout - let user take their time
|
|
190
|
+
const handlerResult = await handler(event, ctx);
|
|
191
|
+
if (handlerResult) {
|
|
192
|
+
result = handlerResult;
|
|
193
|
+
// If blocked, stop processing further hooks
|
|
194
|
+
if (result.block) {
|
|
195
|
+
return result;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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child_process\";\nimport type { LoadedHook, SendHandler } from \"./loader.js\";\nimport type {\n\tBranchEventResult,\n\tExecResult,\n\tHookError,\n\tHookEvent,\n\tHookEventContext,\n\tHookUIContext,\n\tToolCallEvent,\n\tToolCallEventResult,\n\tToolResultEventResult,\n} from \"./types.js\";\n\n/**\n * Default timeout for hook execution (30 seconds).\n */\nconst DEFAULT_TIMEOUT = 30000;\n\n/**\n * Listener for hook errors.\n */\nexport type HookErrorListener = (error: HookError) => void;\n\n/**\n * Execute a command and return stdout/stderr/code.\n */\nasync function exec(command: string, args: string[], cwd: string): Promise<ExecResult> {\n\treturn new Promise((resolve) => {\n\t\tconst proc = spawn(command, args, { cwd, shell: false });\n\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\n\t\tproc.stdout?.on(\"data\", (data) => {\n\t\t\tstdout += data.toString();\n\t\t});\n\n\t\tproc.stderr?.on(\"data\", (data) => {\n\t\t\tstderr += data.toString();\n\t\t});\n\n\t\tproc.on(\"close\", (code) => {\n\t\t\tresolve({ stdout, stderr, code: code ?? 0 });\n\t\t});\n\n\t\tproc.on(\"error\", (_err) => {\n\t\t\tresolve({ stdout, stderr, code: 1 });\n\t\t});\n\t});\n}\n\n/**\n * Create a promise that rejects after a timeout.\n */\nfunction createTimeout(ms: number): { promise: Promise<never>; clear: () => void } {\n\tlet timeoutId: NodeJS.Timeout;\n\tconst promise = new Promise<never>((_, reject) => {\n\t\ttimeoutId = setTimeout(() => reject(new Error(`Hook timed out after ${ms}ms`)), ms);\n\t});\n\treturn {\n\t\tpromise,\n\t\tclear: () => clearTimeout(timeoutId),\n\t};\n}\n\n/** No-op UI context used when no UI is available */\nconst noOpUIContext: HookUIContext = {\n\tselect: async () => null,\n\tconfirm: async () => false,\n\tinput: async () => null,\n\tnotify: () => {},\n};\n\n/**\n * HookRunner executes hooks and manages event emission.\n */\nexport class HookRunner {\n\tprivate hooks: LoadedHook[];\n\tprivate uiContext: HookUIContext;\n\tprivate hasUI: boolean;\n\tprivate cwd: string;\n\tprivate sessionFile: string | null;\n\tprivate timeout: number;\n\tprivate errorListeners: Set<HookErrorListener> = new Set();\n\n\tconstructor(hooks: LoadedHook[], cwd: string, timeout: number = DEFAULT_TIMEOUT) {\n\t\tthis.hooks = hooks;\n\t\tthis.uiContext = noOpUIContext;\n\t\tthis.hasUI = false;\n\t\tthis.cwd = cwd;\n\t\tthis.sessionFile = null;\n\t\tthis.timeout = timeout;\n\t}\n\n\t/**\n\t * Set the UI context for hooks.\n\t * Call this when the mode initializes and UI is available.\n\t */\n\tsetUIContext(uiContext: HookUIContext, hasUI: boolean): void {\n\t\tthis.uiContext = uiContext;\n\t\tthis.hasUI = hasUI;\n\t}\n\n\t/**\n\t * Get the paths of all loaded hooks.\n\t */\n\tgetHookPaths(): string[] {\n\t\treturn this.hooks.map((h) => h.path);\n\t}\n\n\t/**\n\t * Set the session file path.\n\t */\n\tsetSessionFile(sessionFile: string | null): void {\n\t\tthis.sessionFile = sessionFile;\n\t}\n\n\t/**\n\t * Set the send handler for all hooks' pi.send().\n\t * Call this when the mode initializes.\n\t */\n\tsetSendHandler(handler: SendHandler): void {\n\t\tfor (const hook of this.hooks) {\n\t\t\thook.setSendHandler(handler);\n\t\t}\n\t}\n\n\t/**\n\t * Subscribe to hook errors.\n\t * @returns Unsubscribe function\n\t */\n\tonError(listener: HookErrorListener): () => void {\n\t\tthis.errorListeners.add(listener);\n\t\treturn () => this.errorListeners.delete(listener);\n\t}\n\n\t/**\n\t * Emit an error to all listeners.\n\t */\n\tprivate emitError(error: HookError): void {\n\t\tfor (const listener of this.errorListeners) {\n\t\t\tlistener(error);\n\t\t}\n\t}\n\n\t/**\n\t * Check if any hooks have handlers for the given event type.\n\t */\n\thasHandlers(eventType: string): boolean {\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(eventType);\n\t\t\tif (handlers && handlers.length > 0) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\treturn false;\n\t}\n\n\t/**\n\t * Create the event context for handlers.\n\t */\n\tprivate createContext(): HookEventContext {\n\t\treturn {\n\t\t\texec: (command: string, args: string[]) => exec(command, args, this.cwd),\n\t\t\tui: this.uiContext,\n\t\t\thasUI: this.hasUI,\n\t\t\tcwd: this.cwd,\n\t\t\tsessionFile: this.sessionFile,\n\t\t};\n\t}\n\n\t/**\n\t * Emit an event to all hooks.\n\t * Returns the result from branch/tool_result events (if any handler returns one).\n\t */\n\tasync emit(event: HookEvent): Promise<BranchEventResult | ToolResultEventResult | undefined> {\n\t\tconst ctx = this.createContext();\n\t\tlet result: BranchEventResult | ToolResultEventResult | undefined;\n\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(event.type);\n\t\t\tif (!handlers || handlers.length === 0) continue;\n\n\t\t\tfor (const handler of handlers) {\n\t\t\t\ttry {\n\t\t\t\t\tconst timeout = createTimeout(this.timeout);\n\t\t\t\t\tconst handlerResult = await Promise.race([handler(event, ctx), timeout.promise]);\n\t\t\t\t\ttimeout.clear();\n\n\t\t\t\t\t// For branch events, capture the result\n\t\t\t\t\tif (event.type === \"branch\" && handlerResult) {\n\t\t\t\t\t\tresult = handlerResult as BranchEventResult;\n\t\t\t\t\t}\n\n\t\t\t\t\t// For tool_result events, capture the result\n\t\t\t\t\tif (event.type === \"tool_result\" && handlerResult) {\n\t\t\t\t\t\tresult = handlerResult as ToolResultEventResult;\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\tconst message = err instanceof Error ? err.message : String(err);\n\t\t\t\t\tthis.emitError({\n\t\t\t\t\t\thookPath: hook.path,\n\t\t\t\t\t\tevent: event.type,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Emit a tool_call event to all hooks.\n\t * No timeout - user prompts can take as long as needed.\n\t * Errors are thrown (not swallowed) so caller can block on failure.\n\t */\n\tasync emitToolCall(event: ToolCallEvent): Promise<ToolCallEventResult | undefined> {\n\t\tconst ctx = this.createContext();\n\t\tlet result: ToolCallEventResult | undefined;\n\n\t\tfor (const hook of this.hooks) {\n\t\t\tconst handlers = hook.handlers.get(\"tool_call\");\n\t\t\tif (!handlers || handlers.length === 0) continue;\n\n\t\t\tfor (const handler of handlers) {\n\t\t\t\t// No timeout - let user take their time\n\t\t\t\tconst handlerResult = await handler(event, ctx);\n\n\t\t\t\tif (handlerResult) {\n\t\t\t\t\tresult = handlerResult as ToolCallEventResult;\n\t\t\t\t\t// If blocked, stop processing further hooks\n\t\t\t\t\tif (result.block) {\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool wrapper - wraps tools with hook callbacks for interception.
|
|
3
|
+
*/
|
|
4
|
+
import type { AgentTool } from "@mariozechner/pi-ai";
|
|
5
|
+
import type { HookRunner } from "./runner.js";
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a tool with hook callbacks.
|
|
8
|
+
* - Emits tool_call event before execution (can block)
|
|
9
|
+
* - Emits tool_result event after execution (can modify result)
|
|
10
|
+
*/
|
|
11
|
+
export declare function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T>;
|
|
12
|
+
/**
|
|
13
|
+
* Wrap all tools with hook callbacks.
|
|
14
|
+
*/
|
|
15
|
+
export declare function wrapToolsWithHooks<T>(tools: AgentTool<any, T>[], hookRunner: HookRunner): AgentTool<any, T>[];
|
|
16
|
+
//# sourceMappingURL=tool-wrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-wrapper.d.ts","sourceRoot":"","sources":["../../../src/core/hooks/tool-wrapper.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAG9C;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,CA4DvG;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,EAAE,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,CAE7G","sourcesContent":["/**\n * Tool wrapper - wraps tools with hook callbacks for interception.\n */\n\nimport type { AgentTool } from \"@mariozechner/pi-ai\";\nimport type { HookRunner } from \"./runner.js\";\nimport type { ToolCallEventResult, ToolResultEventResult } from \"./types.js\";\n\n/**\n * Wrap a tool with hook callbacks.\n * - Emits tool_call event before execution (can block)\n * - Emits tool_result event after execution (can modify result)\n */\nexport function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T> {\n\treturn {\n\t\t...tool,\n\t\texecute: async (toolCallId: string, params: Record<string, unknown>, signal?: AbortSignal) => {\n\t\t\t// Emit tool_call event - hooks can block execution\n\t\t\t// If hook errors/times out, block by default (fail-safe)\n\t\t\tif (hookRunner.hasHandlers(\"tool_call\")) {\n\t\t\t\ttry {\n\t\t\t\t\tconst callResult = (await hookRunner.emitToolCall({\n\t\t\t\t\t\ttype: \"tool_call\",\n\t\t\t\t\t\ttoolName: tool.name,\n\t\t\t\t\t\ttoolCallId,\n\t\t\t\t\t\tinput: params,\n\t\t\t\t\t})) as ToolCallEventResult | undefined;\n\n\t\t\t\t\tif (callResult?.block) {\n\t\t\t\t\t\tconst reason = callResult.reason || \"Tool execution was blocked by a hook\";\n\t\t\t\t\t\tthrow new Error(reason);\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// Hook error or block - throw to mark as error\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tthrow err;\n\t\t\t\t\t}\n\t\t\t\t\tthrow new Error(`Hook failed, blocking execution: ${String(err)}`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Execute the actual tool\n\t\t\tconst result = await tool.execute(toolCallId, params, signal);\n\n\t\t\t// Emit tool_result event - hooks can modify the result\n\t\t\tif (hookRunner.hasHandlers(\"tool_result\")) {\n\t\t\t\t// Extract text from result for hooks\n\t\t\t\tconst resultText = result.content\n\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t.join(\"\\n\");\n\n\t\t\t\tconst resultResult = (await hookRunner.emit({\n\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\ttoolName: tool.name,\n\t\t\t\t\ttoolCallId,\n\t\t\t\t\tinput: params,\n\t\t\t\t\tresult: resultText,\n\t\t\t\t\tisError: false,\n\t\t\t\t})) as ToolResultEventResult | undefined;\n\n\t\t\t\t// Apply modifications if any\n\t\t\t\tif (resultResult?.result !== undefined) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...result,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: resultResult.result }],\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t},\n\t};\n}\n\n/**\n * Wrap all tools with hook callbacks.\n */\nexport function wrapToolsWithHooks<T>(tools: AgentTool<any, T>[], hookRunner: HookRunner): AgentTool<any, T>[] {\n\treturn tools.map((tool) => wrapToolWithHooks(tool, hookRunner));\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool wrapper - wraps tools with hook callbacks for interception.
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Wrap a tool with hook callbacks.
|
|
6
|
+
* - Emits tool_call event before execution (can block)
|
|
7
|
+
* - Emits tool_result event after execution (can modify result)
|
|
8
|
+
*/
|
|
9
|
+
export function wrapToolWithHooks(tool, hookRunner) {
|
|
10
|
+
return {
|
|
11
|
+
...tool,
|
|
12
|
+
execute: async (toolCallId, params, signal) => {
|
|
13
|
+
// Emit tool_call event - hooks can block execution
|
|
14
|
+
// If hook errors/times out, block by default (fail-safe)
|
|
15
|
+
if (hookRunner.hasHandlers("tool_call")) {
|
|
16
|
+
try {
|
|
17
|
+
const callResult = (await hookRunner.emitToolCall({
|
|
18
|
+
type: "tool_call",
|
|
19
|
+
toolName: tool.name,
|
|
20
|
+
toolCallId,
|
|
21
|
+
input: params,
|
|
22
|
+
}));
|
|
23
|
+
if (callResult?.block) {
|
|
24
|
+
const reason = callResult.reason || "Tool execution was blocked by a hook";
|
|
25
|
+
throw new Error(reason);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
// Hook error or block - throw to mark as error
|
|
30
|
+
if (err instanceof Error) {
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Hook failed, blocking execution: ${String(err)}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Execute the actual tool
|
|
37
|
+
const result = await tool.execute(toolCallId, params, signal);
|
|
38
|
+
// Emit tool_result event - hooks can modify the result
|
|
39
|
+
if (hookRunner.hasHandlers("tool_result")) {
|
|
40
|
+
// Extract text from result for hooks
|
|
41
|
+
const resultText = result.content
|
|
42
|
+
.filter((c) => c.type === "text")
|
|
43
|
+
.map((c) => c.text)
|
|
44
|
+
.join("\n");
|
|
45
|
+
const resultResult = (await hookRunner.emit({
|
|
46
|
+
type: "tool_result",
|
|
47
|
+
toolName: tool.name,
|
|
48
|
+
toolCallId,
|
|
49
|
+
input: params,
|
|
50
|
+
result: resultText,
|
|
51
|
+
isError: false,
|
|
52
|
+
}));
|
|
53
|
+
// Apply modifications if any
|
|
54
|
+
if (resultResult?.result !== undefined) {
|
|
55
|
+
return {
|
|
56
|
+
...result,
|
|
57
|
+
content: [{ type: "text", text: resultResult.result }],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Wrap all tools with hook callbacks.
|
|
67
|
+
*/
|
|
68
|
+
export function wrapToolsWithHooks(tools, hookRunner) {
|
|
69
|
+
return tools.map((tool) => wrapToolWithHooks(tool, hookRunner));
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=tool-wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-wrapper.js","sourceRoot":"","sources":["../../../src/core/hooks/tool-wrapper.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAI,IAAuB,EAAE,UAAsB,EAAqB;IACxG,OAAO;QACN,GAAG,IAAI;QACP,OAAO,EAAE,KAAK,EAAE,UAAkB,EAAE,MAA+B,EAAE,MAAoB,EAAE,EAAE,CAAC;YAC7F,mDAAmD;YACnD,yDAAyD;YACzD,IAAI,UAAU,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC;gBACzC,IAAI,CAAC;oBACJ,MAAM,UAAU,GAAG,CAAC,MAAM,UAAU,CAAC,YAAY,CAAC;wBACjD,IAAI,EAAE,WAAW;wBACjB,QAAQ,EAAE,IAAI,CAAC,IAAI;wBACnB,UAAU;wBACV,KAAK,EAAE,MAAM;qBACb,CAAC,CAAoC,CAAC;oBAEvC,IAAI,UAAU,EAAE,KAAK,EAAE,CAAC;wBACvB,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,IAAI,sCAAsC,CAAC;wBAC3E,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC;oBACzB,CAAC;gBACF,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACd,+CA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EAAuB;IAC9G,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;AAAA,CAChE","sourcesContent":["/**\n * Tool wrapper - wraps tools with hook callbacks for interception.\n */\n\nimport type { AgentTool } from \"@mariozechner/pi-ai\";\nimport type { HookRunner } from \"./runner.js\";\nimport type { ToolCallEventResult, ToolResultEventResult } from \"./types.js\";\n\n/**\n * Wrap a tool with hook callbacks.\n * - Emits tool_call event before execution (can block)\n * - Emits tool_result event after execution (can modify result)\n */\nexport function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T> {\n\treturn {\n\t\t...tool,\n\t\texecute: async (toolCallId: string, params: Record<string, unknown>, signal?: AbortSignal) => {\n\t\t\t// Emit tool_call event - hooks can block execution\n\t\t\t// If hook errors/times out, block by default (fail-safe)\n\t\t\tif (hookRunner.hasHandlers(\"tool_call\")) {\n\t\t\t\ttry {\n\t\t\t\t\tconst callResult = (await hookRunner.emitToolCall({\n\t\t\t\t\t\ttype: \"tool_call\",\n\t\t\t\t\t\ttoolName: tool.name,\n\t\t\t\t\t\ttoolCallId,\n\t\t\t\t\t\tinput: params,\n\t\t\t\t\t})) as ToolCallEventResult | undefined;\n\n\t\t\t\t\tif (callResult?.block) {\n\t\t\t\t\t\tconst reason = callResult.reason || \"Tool execution was blocked by a hook\";\n\t\t\t\t\t\tthrow new Error(reason);\n\t\t\t\t\t}\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// Hook error or block - throw to mark as error\n\t\t\t\t\tif (err instanceof Error) {\n\t\t\t\t\t\tthrow err;\n\t\t\t\t\t}\n\t\t\t\t\tthrow new Error(`Hook failed, blocking execution: ${String(err)}`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Execute the actual tool\n\t\t\tconst result = await tool.execute(toolCallId, params, signal);\n\n\t\t\t// Emit tool_result event - hooks can modify the result\n\t\t\tif (hookRunner.hasHandlers(\"tool_result\")) {\n\t\t\t\t// Extract text from result for hooks\n\t\t\t\tconst resultText = result.content\n\t\t\t\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t\t\t\t.map((c) => c.text)\n\t\t\t\t\t.join(\"\\n\");\n\n\t\t\t\tconst resultResult = (await hookRunner.emit({\n\t\t\t\t\ttype: \"tool_result\",\n\t\t\t\t\ttoolName: tool.name,\n\t\t\t\t\ttoolCallId,\n\t\t\t\t\tinput: params,\n\t\t\t\t\tresult: resultText,\n\t\t\t\t\tisError: false,\n\t\t\t\t})) as ToolResultEventResult | undefined;\n\n\t\t\t\t// Apply modifications if any\n\t\t\t\tif (resultResult?.result !== undefined) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...result,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: resultResult.result }],\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t},\n\t};\n}\n\n/**\n * Wrap all tools with hook callbacks.\n */\nexport function wrapToolsWithHooks<T>(tools: AgentTool<any, T>[], hookRunner: HookRunner): AgentTool<any, T>[] {\n\treturn tools.map((tool) => wrapToolWithHooks(tool, hookRunner));\n}\n"]}
|