opencode-command-hooks 0.1.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/README.md +796 -0
- package/dist/config/agent.d.ts +82 -0
- package/dist/config/agent.d.ts.map +1 -0
- package/dist/config/agent.js +145 -0
- package/dist/config/agent.js.map +1 -0
- package/dist/config/global.d.ts +36 -0
- package/dist/config/global.d.ts.map +1 -0
- package/dist/config/global.js +219 -0
- package/dist/config/global.js.map +1 -0
- package/dist/config/markdown.d.ts +119 -0
- package/dist/config/markdown.d.ts.map +1 -0
- package/dist/config/markdown.js +373 -0
- package/dist/config/markdown.js.map +1 -0
- package/dist/config/merge.d.ts +67 -0
- package/dist/config/merge.d.ts.map +1 -0
- package/dist/config/merge.js +192 -0
- package/dist/config/merge.js.map +1 -0
- package/dist/execution/shell.d.ts +55 -0
- package/dist/execution/shell.d.ts.map +1 -0
- package/dist/execution/shell.js +187 -0
- package/dist/execution/shell.js.map +1 -0
- package/dist/execution/template.d.ts +55 -0
- package/dist/execution/template.d.ts.map +1 -0
- package/dist/execution/template.js +106 -0
- package/dist/execution/template.js.map +1 -0
- package/dist/executor.d.ts +54 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +314 -0
- package/dist/executor.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +359 -0
- package/dist/index.js.map +1 -0
- package/dist/logging.d.ts +24 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +57 -0
- package/dist/logging.js.map +1 -0
- package/dist/schemas.d.ts +425 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +150 -0
- package/dist/schemas.js.map +1 -0
- package/dist/types/hooks.d.ts +635 -0
- package/dist/types/hooks.d.ts.map +1 -0
- package/dist/types/hooks.js +12 -0
- package/dist/types/hooks.js.map +1 -0
- package/package.json +66 -0
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for command hooks configuration
|
|
3
|
+
* Supports tool hooks and session hooks with flexible matching and injection
|
|
4
|
+
*
|
|
5
|
+
* Hooks allow users to declaratively attach shell commands to agent/tool/slash-command
|
|
6
|
+
* lifecycle events. They can be defined in:
|
|
7
|
+
* - Global config in opencode.json/.opencode.jsonc
|
|
8
|
+
* - Per-agent YAML frontmatter in agent markdown
|
|
9
|
+
* - Per-slash-command YAML frontmatter in command markdown
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Simplified agent hooks configuration for markdown frontmatter
|
|
13
|
+
*
|
|
14
|
+
* This is the new, simplified schema for defining hooks in agent markdown files.
|
|
15
|
+
* It's much more concise than the full CommandHooksConfig format.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```yaml
|
|
19
|
+
* hooks:
|
|
20
|
+
* before:
|
|
21
|
+
* - run: "echo 'Starting...'"
|
|
22
|
+
* after:
|
|
23
|
+
* - run: ["npm run typecheck", "npm run lint"]
|
|
24
|
+
* inject: "Validation results:\n{stdout}"
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export interface AgentHooks {
|
|
28
|
+
/**
|
|
29
|
+
* Hooks that run before agent execution
|
|
30
|
+
* Optional; omit if no before hooks needed
|
|
31
|
+
*/
|
|
32
|
+
before?: AgentHookEntry[];
|
|
33
|
+
/**
|
|
34
|
+
* Hooks that run after agent execution
|
|
35
|
+
* Optional; omit if no after hooks needed
|
|
36
|
+
*/
|
|
37
|
+
after?: AgentHookEntry[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Single hook entry in the simplified agent hooks format
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```yaml
|
|
44
|
+
* - run: "npm run typecheck"
|
|
45
|
+
* inject: "Typecheck results:\n{stdout}"
|
|
46
|
+
* toast:
|
|
47
|
+
* message: "Typecheck {exitCode, select, 0 {passed} other {failed}}"
|
|
48
|
+
* variant: "success"
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export interface AgentHookEntry {
|
|
52
|
+
/**
|
|
53
|
+
* Shell command(s) to execute when this hook runs.
|
|
54
|
+
* - Single string: executed as-is
|
|
55
|
+
* - Array of strings: executed sequentially
|
|
56
|
+
*
|
|
57
|
+
* Required field - hook must have a run command.
|
|
58
|
+
*/
|
|
59
|
+
run: string | string[];
|
|
60
|
+
/**
|
|
61
|
+
* Optional template for injecting hook results into the session.
|
|
62
|
+
* Supports the same placeholder substitution as ToolHook.inject.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```yaml
|
|
66
|
+
* inject: "Results:\n{stdout}"
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
inject?: string;
|
|
70
|
+
/**
|
|
71
|
+
* Optional toast notification to display after the hook runs.
|
|
72
|
+
* Supports template placeholder substitution.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```yaml
|
|
76
|
+
* toast:
|
|
77
|
+
* message: "Tests {exitCode, select, 0 {passed} other {failed}}"
|
|
78
|
+
* variant: "success"
|
|
79
|
+
* duration: 3000
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
toast?: {
|
|
83
|
+
/**
|
|
84
|
+
* Optional title for the toast notification.
|
|
85
|
+
* Supports template placeholder substitution.
|
|
86
|
+
*/
|
|
87
|
+
title?: string;
|
|
88
|
+
/**
|
|
89
|
+
* Required message for the toast notification.
|
|
90
|
+
* Supports template placeholder substitution.
|
|
91
|
+
*/
|
|
92
|
+
message: string;
|
|
93
|
+
/**
|
|
94
|
+
* Visual variant/style for the toast.
|
|
95
|
+
* - "info": informational message (default)
|
|
96
|
+
* - "success": success message
|
|
97
|
+
* - "warning": warning message
|
|
98
|
+
* - "error": error message
|
|
99
|
+
*/
|
|
100
|
+
variant?: "info" | "success" | "warning" | "error";
|
|
101
|
+
/**
|
|
102
|
+
* Duration in milliseconds for which the toast should be displayed.
|
|
103
|
+
* If omitted, uses the default duration.
|
|
104
|
+
*/
|
|
105
|
+
duration?: number;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Tool hook configuration
|
|
110
|
+
*
|
|
111
|
+
* Runs shell command(s) before or after a tool execution. Hooks can be filtered
|
|
112
|
+
* by tool name, calling agent, and slash command context. Results can optionally
|
|
113
|
+
* be injected into the session as messages.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```json
|
|
117
|
+
* {
|
|
118
|
+
* "id": "global-tests-after-task",
|
|
119
|
+
* "when": {
|
|
120
|
+
* "phase": "after",
|
|
121
|
+
* "tool": ["task"],
|
|
122
|
+
* "callingAgent": ["*"]
|
|
123
|
+
* },
|
|
124
|
+
* "run": ["pnpm test --runInBand"],
|
|
125
|
+
* "inject": {
|
|
126
|
+
* "template": "Tests after {tool}: exit {exitCode}\n```sh\n{stdout}\n```"
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
export interface ToolHook {
|
|
132
|
+
/**
|
|
133
|
+
* Unique identifier for this hook within its scope (global or per-entity).
|
|
134
|
+
* Used for error reporting and logging.
|
|
135
|
+
* Must be unique within the same config source (global or markdown file).
|
|
136
|
+
*/
|
|
137
|
+
id: string;
|
|
138
|
+
/**
|
|
139
|
+
* Matching conditions that determine when this hook should run.
|
|
140
|
+
* All specified conditions must match for the hook to execute.
|
|
141
|
+
*/
|
|
142
|
+
when: ToolHookWhen;
|
|
143
|
+
/**
|
|
144
|
+
* Shell command(s) to execute when this hook matches.
|
|
145
|
+
* - Single string: executed as-is
|
|
146
|
+
* - Array of strings: executed sequentially, even if earlier commands fail
|
|
147
|
+
*
|
|
148
|
+
* Commands are executed via Bun's shell API ($) with proper error handling.
|
|
149
|
+
* Non-zero exit codes do not block subsequent commands or normal tool execution.
|
|
150
|
+
*/
|
|
151
|
+
run: string | string[];
|
|
152
|
+
/**
|
|
153
|
+
* Optional template string for injecting hook results into the calling session.
|
|
154
|
+
* If omitted, the hook still runs but output is only logged.
|
|
155
|
+
*
|
|
156
|
+
* Supports placeholder substitution with the following variables:
|
|
157
|
+
* - {id}: hook ID
|
|
158
|
+
* - {agent}: agent name (if available)
|
|
159
|
+
* - {tool}: tool name (for tool hooks)
|
|
160
|
+
* - {cmd}: command string that was executed
|
|
161
|
+
* - {stdout}: captured standard output (truncated to limit)
|
|
162
|
+
* - {stderr}: captured standard error (truncated to limit)
|
|
163
|
+
* - {exitCode}: command exit code (integer)
|
|
164
|
+
*
|
|
165
|
+
* Placeholders for unavailable values are replaced with empty string.
|
|
166
|
+
*/
|
|
167
|
+
inject?: string;
|
|
168
|
+
/**
|
|
169
|
+
* Optional toast notification to display to the user.
|
|
170
|
+
* Supports template placeholder substitution like the inject template.
|
|
171
|
+
* If omitted, no toast is displayed.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```json
|
|
175
|
+
* {
|
|
176
|
+
* "title": "Lint Complete",
|
|
177
|
+
* "message": "Exit code: {exitCode}",
|
|
178
|
+
* "variant": "success",
|
|
179
|
+
* "duration": 3000
|
|
180
|
+
* }
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
toast?: {
|
|
184
|
+
/**
|
|
185
|
+
* Optional title for the toast notification.
|
|
186
|
+
* Supports template placeholder substitution.
|
|
187
|
+
*/
|
|
188
|
+
title?: string;
|
|
189
|
+
/**
|
|
190
|
+
* Required message for the toast notification.
|
|
191
|
+
* Supports template placeholder substitution.
|
|
192
|
+
*/
|
|
193
|
+
message: string;
|
|
194
|
+
/**
|
|
195
|
+
* Visual variant/style for the toast.
|
|
196
|
+
* - "info": informational message (default)
|
|
197
|
+
* - "success": success message
|
|
198
|
+
* - "warning": warning message
|
|
199
|
+
* - "error": error message
|
|
200
|
+
*/
|
|
201
|
+
variant?: "info" | "success" | "warning" | "error";
|
|
202
|
+
/**
|
|
203
|
+
* Duration in milliseconds for which the toast should be displayed.
|
|
204
|
+
* If omitted, uses the default duration.
|
|
205
|
+
*/
|
|
206
|
+
duration?: number;
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Matching conditions for tool hooks
|
|
211
|
+
*
|
|
212
|
+
* Determines when a tool hook should execute. All specified conditions must match.
|
|
213
|
+
* Omitted fields default to matching all values (wildcard behavior).
|
|
214
|
+
*/
|
|
215
|
+
export interface ToolHookWhen {
|
|
216
|
+
/**
|
|
217
|
+
* Execution phase: "before" or "after" tool execution.
|
|
218
|
+
* - "before": runs before the tool is invoked
|
|
219
|
+
* - "after": runs after the tool completes (regardless of success/failure)
|
|
220
|
+
*/
|
|
221
|
+
phase: "before" | "after";
|
|
222
|
+
/**
|
|
223
|
+
* Tool name(s) to match.
|
|
224
|
+
* - Omitted or "*": matches all tools
|
|
225
|
+
* - Array of strings: matches if tool name is in the array
|
|
226
|
+
* - Single string: matches if tool name equals the string
|
|
227
|
+
*
|
|
228
|
+
* Examples: "task", ["bash", "write"], "*"
|
|
229
|
+
*/
|
|
230
|
+
tool?: string | string[];
|
|
231
|
+
/**
|
|
232
|
+
* Calling agent name(s) to match.
|
|
233
|
+
* - Omitted: defaults to "*" in global config, "this agent" in markdown
|
|
234
|
+
* - "*": matches any agent
|
|
235
|
+
* - Array of strings: matches if agent name is in the array
|
|
236
|
+
* - Single string: matches if agent name equals the string
|
|
237
|
+
*
|
|
238
|
+
* When defined in agent markdown, omitting this field means the hook
|
|
239
|
+
* applies only to that agent (implicit scoping).
|
|
240
|
+
*/
|
|
241
|
+
callingAgent?: string | string[];
|
|
242
|
+
/**
|
|
243
|
+
* Slash command name(s) to match (optional).
|
|
244
|
+
* Only applies when the tool invocation is associated with a slash command.
|
|
245
|
+
* - Omitted: matches tool calls regardless of slash command context
|
|
246
|
+
* - "*": matches any slash command
|
|
247
|
+
* - Array of strings: matches if slash command name is in the array
|
|
248
|
+
* - Single string: matches if slash command name equals the string
|
|
249
|
+
*
|
|
250
|
+
* Note: Slash command detection may not be available for all tool types.
|
|
251
|
+
*/
|
|
252
|
+
slashCommand?: string | string[];
|
|
253
|
+
/**
|
|
254
|
+
* Tool argument filters (optional, only available in tool.execute.before).
|
|
255
|
+
* Match based on the tool's input arguments.
|
|
256
|
+
* Useful for filtering task tool calls by subagentType.
|
|
257
|
+
*
|
|
258
|
+
* Example: { "subagentType": "validator" } matches only task calls with subagentType="validator"
|
|
259
|
+
* Example: { "subagentType": ["validator", "reviewer"] } matches multiple values
|
|
260
|
+
*/
|
|
261
|
+
toolArgs?: Record<string, string | string[]>;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Session hook configuration
|
|
265
|
+
*
|
|
266
|
+
* Runs shell command(s) on session lifecycle events (start, idle, end).
|
|
267
|
+
* Can be filtered by agent name. Results can optionally be injected into
|
|
268
|
+
* the session as messages.
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```json
|
|
272
|
+
* {
|
|
273
|
+
* "id": "global-bootstrap",
|
|
274
|
+
* "when": {
|
|
275
|
+
* "event": "session.start",
|
|
276
|
+
* "agent": ["build", "validator", "*"]
|
|
277
|
+
* },
|
|
278
|
+
* "run": ["git status --short"],
|
|
279
|
+
* "inject": {
|
|
280
|
+
* "template": "Repo status for {agent}:\n```sh\n{stdout}\n```"
|
|
281
|
+
* }
|
|
282
|
+
* }
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
export interface SessionHook {
|
|
286
|
+
/**
|
|
287
|
+
* Unique identifier for this hook within its scope (global or per-entity).
|
|
288
|
+
* Used for error reporting and logging.
|
|
289
|
+
* Must be unique within the same config source (global or markdown file).
|
|
290
|
+
*/
|
|
291
|
+
id: string;
|
|
292
|
+
/**
|
|
293
|
+
* Matching conditions that determine when this hook should run.
|
|
294
|
+
* All specified conditions must match for the hook to execute.
|
|
295
|
+
*/
|
|
296
|
+
when: SessionHookWhen;
|
|
297
|
+
/**
|
|
298
|
+
* Shell command(s) to execute when this hook matches.
|
|
299
|
+
* - Single string: executed as-is
|
|
300
|
+
* - Array of strings: executed sequentially, even if earlier commands fail
|
|
301
|
+
*
|
|
302
|
+
* Commands are executed via Bun's shell API ($) with proper error handling.
|
|
303
|
+
* Non-zero exit codes do not block subsequent commands or normal session flow.
|
|
304
|
+
*/
|
|
305
|
+
run: string | string[];
|
|
306
|
+
/**
|
|
307
|
+
* Optional template string for injecting hook results into the calling session.
|
|
308
|
+
* If omitted, the hook still runs but output is only logged.
|
|
309
|
+
*
|
|
310
|
+
* Supports placeholder substitution with the following variables:
|
|
311
|
+
* - {id}: hook ID
|
|
312
|
+
* - {agent}: agent name (if available)
|
|
313
|
+
* - {cmd}: command string that was executed
|
|
314
|
+
* - {stdout}: captured standard output (truncated to limit)
|
|
315
|
+
* - {stderr}: captured standard error (truncated to limit)
|
|
316
|
+
* - {exitCode}: command exit code (integer)
|
|
317
|
+
*
|
|
318
|
+
* Placeholders for unavailable values are replaced with empty string.
|
|
319
|
+
*/
|
|
320
|
+
inject?: string;
|
|
321
|
+
/**
|
|
322
|
+
* Optional toast notification to display to the user.
|
|
323
|
+
* Supports template placeholder substitution like the inject template.
|
|
324
|
+
* If omitted, no toast is displayed.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```json
|
|
328
|
+
* {
|
|
329
|
+
* "title": "Session Started",
|
|
330
|
+
* "message": "Agent {agent} is ready",
|
|
331
|
+
* "variant": "info",
|
|
332
|
+
* "duration": 2000
|
|
333
|
+
* }
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
toast?: {
|
|
337
|
+
/**
|
|
338
|
+
* Optional title for the toast notification.
|
|
339
|
+
* Supports template placeholder substitution.
|
|
340
|
+
*/
|
|
341
|
+
title?: string;
|
|
342
|
+
/**
|
|
343
|
+
* Required message for the toast notification.
|
|
344
|
+
* Supports template placeholder substitution.
|
|
345
|
+
*/
|
|
346
|
+
message: string;
|
|
347
|
+
/**
|
|
348
|
+
* Visual variant/style for the toast.
|
|
349
|
+
* - "info": informational message (default)
|
|
350
|
+
* - "success": success message
|
|
351
|
+
* - "warning": warning message
|
|
352
|
+
* - "error": error message
|
|
353
|
+
*/
|
|
354
|
+
variant?: "info" | "success" | "warning" | "error";
|
|
355
|
+
/**
|
|
356
|
+
* Duration in milliseconds for which the toast should be displayed.
|
|
357
|
+
* If omitted, uses the default duration.
|
|
358
|
+
*/
|
|
359
|
+
duration?: number;
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Matching conditions for session hooks
|
|
364
|
+
*
|
|
365
|
+
* Determines when a session hook should execute. All specified conditions must match.
|
|
366
|
+
* Omitted fields default to matching all values (wildcard behavior).
|
|
367
|
+
*/
|
|
368
|
+
export interface SessionHookWhen {
|
|
369
|
+
/**
|
|
370
|
+
* Session lifecycle event type.
|
|
371
|
+
* - "session.start": fires when session is ready to receive pre-context
|
|
372
|
+
* - "session.idle": fires after agent turn completes (for after-turn hooks)
|
|
373
|
+
* - "session.end": fires when session ends
|
|
374
|
+
*/
|
|
375
|
+
event: "session.start" | "session.idle" | "session.end";
|
|
376
|
+
/**
|
|
377
|
+
* Agent name(s) to match.
|
|
378
|
+
* - Omitted: defaults to "*" in global config, "this agent" in markdown
|
|
379
|
+
* - "*": matches any agent
|
|
380
|
+
* - Array of strings: matches if agent name is in the array
|
|
381
|
+
* - Single string: matches if agent name equals the string
|
|
382
|
+
*
|
|
383
|
+
* When defined in agent markdown, omitting this field means the hook
|
|
384
|
+
* applies only to that agent (implicit scoping).
|
|
385
|
+
*/
|
|
386
|
+
agent?: string | string[];
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Top-level command hooks configuration
|
|
390
|
+
*
|
|
391
|
+
* This is the root configuration object that appears in opencode.json/.opencode.jsonc
|
|
392
|
+
* under the "command_hooks" key, or in YAML frontmatter of agent/slash-command markdown.
|
|
393
|
+
*
|
|
394
|
+
* @example
|
|
395
|
+
* In opencode.json:
|
|
396
|
+
* ```json
|
|
397
|
+
* {
|
|
398
|
+
* "command_hooks": {
|
|
399
|
+
* "tool": [...],
|
|
400
|
+
* "session": [...]
|
|
401
|
+
* }
|
|
402
|
+
* }
|
|
403
|
+
* ```
|
|
404
|
+
*
|
|
405
|
+
* In agent markdown frontmatter:
|
|
406
|
+
* ```yaml
|
|
407
|
+
* ---
|
|
408
|
+
* command_hooks:
|
|
409
|
+
* tool: [...]
|
|
410
|
+
* session: [...]
|
|
411
|
+
* ---
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
414
|
+
export interface CommandHooksConfig {
|
|
415
|
+
/**
|
|
416
|
+
* Array of tool execution hooks.
|
|
417
|
+
* Hooks run before or after tool calls based on matching conditions.
|
|
418
|
+
* Optional; omit if no tool hooks are needed.
|
|
419
|
+
*/
|
|
420
|
+
tool?: ToolHook[];
|
|
421
|
+
/**
|
|
422
|
+
* Array of session lifecycle hooks.
|
|
423
|
+
* Hooks run on session events (start, idle, end) based on matching conditions.
|
|
424
|
+
* Optional; omit if no session hooks are needed.
|
|
425
|
+
*/
|
|
426
|
+
session?: SessionHook[];
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Result of executing a single hook command
|
|
430
|
+
*
|
|
431
|
+
* Captures the outcome of running a hook's shell command(s), including
|
|
432
|
+
* output, exit code, and any errors that occurred.
|
|
433
|
+
*/
|
|
434
|
+
export interface HookExecutionResult {
|
|
435
|
+
/**
|
|
436
|
+
* ID of the hook that was executed
|
|
437
|
+
*/
|
|
438
|
+
hookId: string;
|
|
439
|
+
/**
|
|
440
|
+
* Whether the hook executed successfully (exit code 0)
|
|
441
|
+
*/
|
|
442
|
+
success: boolean;
|
|
443
|
+
/**
|
|
444
|
+
* Exit code from the last command in the hook's run array
|
|
445
|
+
* - 0: success
|
|
446
|
+
* - non-zero: command failed
|
|
447
|
+
* - undefined: command did not execute (e.g., binary not found)
|
|
448
|
+
*/
|
|
449
|
+
exitCode?: number;
|
|
450
|
+
/**
|
|
451
|
+
* Captured standard output from the command(s).
|
|
452
|
+
* Truncated to the configured limit (default 4096 chars).
|
|
453
|
+
* May be empty if command produced no output.
|
|
454
|
+
*/
|
|
455
|
+
stdout?: string;
|
|
456
|
+
/**
|
|
457
|
+
* Captured standard error from the command(s).
|
|
458
|
+
* Truncated to the configured limit (default 4096 chars).
|
|
459
|
+
* May be empty if command produced no error output.
|
|
460
|
+
*/
|
|
461
|
+
stderr?: string;
|
|
462
|
+
/**
|
|
463
|
+
* Error message if the hook failed to execute.
|
|
464
|
+
* Examples:
|
|
465
|
+
* - "Command not found: pnpm"
|
|
466
|
+
* - "Timeout executing hook"
|
|
467
|
+
* - "Failed to inject message into session"
|
|
468
|
+
*/
|
|
469
|
+
error?: string;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Context object for template placeholder substitution
|
|
473
|
+
*
|
|
474
|
+
* When injecting hook results into a session, the template string is processed
|
|
475
|
+
* with placeholder substitution using values from this context. All fields are
|
|
476
|
+
* optional as not all contexts have all values available.
|
|
477
|
+
*
|
|
478
|
+
* @example
|
|
479
|
+
* ```typescript
|
|
480
|
+
* const context: TemplateContext = {
|
|
481
|
+
* id: "tests-after-task",
|
|
482
|
+
* agent: "build",
|
|
483
|
+
* tool: "task",
|
|
484
|
+
* cmd: "pnpm test --runInBand",
|
|
485
|
+
* stdout: "✓ All tests passed",
|
|
486
|
+
* stderr: "",
|
|
487
|
+
* exitCode: 0
|
|
488
|
+
* }
|
|
489
|
+
*
|
|
490
|
+
* const template = "Hook {id} for {tool} completed: exit {exitCode}"
|
|
491
|
+
* // Result: "Hook tests-after-task for task completed: exit 0"
|
|
492
|
+
* ```
|
|
493
|
+
*/
|
|
494
|
+
export interface TemplateContext {
|
|
495
|
+
/**
|
|
496
|
+
* Hook ID (always available)
|
|
497
|
+
*/
|
|
498
|
+
id: string;
|
|
499
|
+
/**
|
|
500
|
+
* Agent name (available for tool hooks and session hooks)
|
|
501
|
+
* May be undefined if agent context is not available
|
|
502
|
+
*/
|
|
503
|
+
agent?: string;
|
|
504
|
+
/**
|
|
505
|
+
* Tool name (available for tool hooks only)
|
|
506
|
+
* May be undefined for session hooks
|
|
507
|
+
*/
|
|
508
|
+
tool?: string;
|
|
509
|
+
/**
|
|
510
|
+
* Command string that was executed (available when hook runs)
|
|
511
|
+
* May be undefined if command execution failed before running
|
|
512
|
+
*/
|
|
513
|
+
cmd?: string;
|
|
514
|
+
/**
|
|
515
|
+
* Captured standard output from command execution
|
|
516
|
+
* Truncated to configured limit (default 4096 chars)
|
|
517
|
+
* May be undefined or empty if command produced no output
|
|
518
|
+
*/
|
|
519
|
+
stdout?: string;
|
|
520
|
+
/**
|
|
521
|
+
* Captured standard error from command execution
|
|
522
|
+
* Truncated to configured limit (default 4096 chars)
|
|
523
|
+
* May be undefined or empty if command produced no error output
|
|
524
|
+
*/
|
|
525
|
+
stderr?: string;
|
|
526
|
+
/**
|
|
527
|
+
* Exit code from command execution
|
|
528
|
+
* - 0: success
|
|
529
|
+
* - non-zero: command failed
|
|
530
|
+
* May be undefined if command did not execute
|
|
531
|
+
*/
|
|
532
|
+
exitCode?: number;
|
|
533
|
+
/**
|
|
534
|
+
* Additional context fields for future expansion
|
|
535
|
+
* Examples: subagentSummary, taskResult, etc.
|
|
536
|
+
*/
|
|
537
|
+
[key: string]: unknown;
|
|
538
|
+
}
|
|
539
|
+
/**
|
|
540
|
+
* Normalized representation of a hook's matching conditions
|
|
541
|
+
*
|
|
542
|
+
* Converts flexible input formats (string | string[]) into consistent
|
|
543
|
+
* array format for easier matching logic.
|
|
544
|
+
*/
|
|
545
|
+
export interface NormalizedToolHookWhen {
|
|
546
|
+
phase: "before" | "after";
|
|
547
|
+
tool: string[];
|
|
548
|
+
callingAgent: string[];
|
|
549
|
+
slashCommand: string[];
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Normalized representation of session hook matching conditions
|
|
553
|
+
*/
|
|
554
|
+
export interface NormalizedSessionHookWhen {
|
|
555
|
+
event: "session.start" | "session.idle" | "session.end";
|
|
556
|
+
agent: string[];
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Hook validation error
|
|
560
|
+
*
|
|
561
|
+
* Represents a validation error found in hook configuration.
|
|
562
|
+
* Used for reporting configuration issues without blocking execution.
|
|
563
|
+
*/
|
|
564
|
+
export interface HookValidationError {
|
|
565
|
+
/**
|
|
566
|
+
* Hook ID (if available; may be undefined if id field is missing)
|
|
567
|
+
*/
|
|
568
|
+
hookId?: string;
|
|
569
|
+
/**
|
|
570
|
+
* Type of validation error
|
|
571
|
+
*/
|
|
572
|
+
type: "missing_id" | "missing_when" | "missing_run" | "invalid_phase" | "invalid_event" | "invalid_injection_target" | "invalid_injection_as" | "duplicate_id" | "unknown";
|
|
573
|
+
/**
|
|
574
|
+
* Human-readable error message
|
|
575
|
+
*/
|
|
576
|
+
message: string;
|
|
577
|
+
/**
|
|
578
|
+
* Severity level
|
|
579
|
+
*/
|
|
580
|
+
severity: "error" | "warning";
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Loaded and merged hooks for a specific context
|
|
584
|
+
*
|
|
585
|
+
* Represents the final set of hooks that apply to a given agent/session/tool call,
|
|
586
|
+
* after merging global and entity-specific hooks with proper precedence.
|
|
587
|
+
*/
|
|
588
|
+
export interface MergedHooksContext {
|
|
589
|
+
/**
|
|
590
|
+
* Tool hooks that apply to this context
|
|
591
|
+
*/
|
|
592
|
+
toolHooks: ToolHook[];
|
|
593
|
+
/**
|
|
594
|
+
* Session hooks that apply to this context
|
|
595
|
+
*/
|
|
596
|
+
sessionHooks: SessionHook[];
|
|
597
|
+
/**
|
|
598
|
+
* Validation errors found during loading/merging
|
|
599
|
+
* Hooks with errors are included but marked for error reporting
|
|
600
|
+
*/
|
|
601
|
+
validationErrors: HookValidationError[];
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Hook execution context
|
|
605
|
+
*
|
|
606
|
+
* Information about the current execution context that hooks may need
|
|
607
|
+
*/
|
|
608
|
+
export interface HookExecutionContext {
|
|
609
|
+
/**
|
|
610
|
+
* Current session ID
|
|
611
|
+
*/
|
|
612
|
+
sessionId: string;
|
|
613
|
+
/**
|
|
614
|
+
* Current agent name
|
|
615
|
+
*/
|
|
616
|
+
agent: string;
|
|
617
|
+
/**
|
|
618
|
+
* Tool name (for tool hooks)
|
|
619
|
+
*/
|
|
620
|
+
tool?: string;
|
|
621
|
+
/**
|
|
622
|
+
* Slash command name (if applicable)
|
|
623
|
+
*/
|
|
624
|
+
slashCommand?: string;
|
|
625
|
+
/**
|
|
626
|
+
* Tool call ID provided by OpenCode (if available)
|
|
627
|
+
*/
|
|
628
|
+
callId?: string;
|
|
629
|
+
/**
|
|
630
|
+
* Tool arguments (available for tool.execute.before hooks)
|
|
631
|
+
* For task tool, this includes: description, prompt, subagentType
|
|
632
|
+
*/
|
|
633
|
+
toolArgs?: Record<string, unknown>;
|
|
634
|
+
}
|
|
635
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/types/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,MAAM,CAAC,EAAE,cAAc,EAAE,CAAA;IAEzB;;;OAGG;IACH,KAAK,CAAC,EAAE,cAAc,EAAE,CAAA;CACzB;AAED;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,cAAc;IAC7B;;;;;;OAMG;IACH,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAEtB;;;;;;;;OAQG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;;;;;;;;;OAWG;IACH,KAAK,CAAC,EAAE;QACN;;;WAGG;QACH,KAAK,CAAC,EAAE,MAAM,CAAA;QAEd;;;WAGG;QACH,OAAO,EAAE,MAAM,CAAA;QAEf;;;;;;WAMG;QACH,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YAAY,EAAE,WAAW,EAAE,CAAA;IAE3B;;;OAGG;IACH,gBAAgB,EAAE,mBAAmB,EAAE,CAAA;CACxC;AAED;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IAEjB;;OAEG;IACH,KAAK,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IAEb;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IAEpB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for command hooks configuration
|
|
3
|
+
* Supports tool hooks and session hooks with flexible matching and injection
|
|
4
|
+
*
|
|
5
|
+
* Hooks allow users to declaratively attach shell commands to agent/tool/slash-command
|
|
6
|
+
* lifecycle events. They can be defined in:
|
|
7
|
+
* - Global config in opencode.json/.opencode.jsonc
|
|
8
|
+
* - Per-agent YAML frontmatter in agent markdown
|
|
9
|
+
* - Per-slash-command YAML frontmatter in command markdown
|
|
10
|
+
*/
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=hooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/types/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
|