-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathconstants.ts
More file actions
299 lines (291 loc) · 11.3 KB
/
Copy pathconstants.ts
File metadata and controls
299 lines (291 loc) · 11.3 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
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
import {
HooksConfigSource,
HookEventName,
hookEventSupportsMatcher,
} from '@qwen-code/qwen-code-core';
import type { HookExitCode, HookEventDisplayInfo } from './types.js';
import { t } from '../../../i18n/index.js';
/**
* Exit code descriptions for different hook types
*/
export function getHookExitCodes(eventName: string): HookExitCode[] {
const exitCodesMap: Record<string, HookExitCode[]> = {
[HookEventName.Stop]: [
{ code: 0, description: t('stdout/stderr not shown') },
{
code: 2,
description: t('show stderr to model and continue conversation'),
},
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.PreToolUse]: [
{ code: 0, description: t('stdout/stderr not shown') },
{ code: 2, description: t('show stderr to model and block tool call') },
{
code: 'Other',
description: t('show stderr to user only but continue with tool call'),
},
],
[HookEventName.PostToolUse]: [
{ code: 0, description: t('stdout shown in transcript mode (ctrl+o)') },
{ code: 2, description: t('show stderr to model immediately') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.PostToolUseFailure]: [
{ code: 0, description: t('stdout shown in transcript mode (ctrl+o)') },
{ code: 2, description: t('show stderr to model immediately') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.PostToolBatch]: [
{ code: 0, description: t('stdout shown in transcript mode (ctrl+o)') },
{ code: 2, description: t('show stderr to model immediately') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.Notification]: [
{ code: 0, description: t('stdout/stderr not shown') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.InstructionsLoaded]: [
{ code: 0, description: t('stdout/stderr not shown') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.UserPromptSubmit]: [
{ code: 0, description: t('stdout shown to Qwen') },
{
code: 2,
description: t(
'block processing, erase original prompt, and show stderr to user only',
),
},
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.UserPromptExpansion]: [
{ code: 0, description: t('stdout shown to Qwen') },
{
code: 2,
description: t(
'block expanded prompt submission and show stderr to user only',
),
},
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.SessionStart]: [
{ code: 0, description: t('stdout shown to Qwen') },
{
code: 'Other',
description: t('show stderr to user only (blocking errors ignored)'),
},
],
[HookEventName.SessionEnd]: [
{ code: 0, description: t('command completes successfully') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.SubagentStart]: [
{ code: 0, description: t('stdout shown to subagent') },
{
code: 'Other',
description: t('show stderr to user only (blocking errors ignored)'),
},
],
[HookEventName.SubagentStop]: [
{ code: 0, description: t('stdout/stderr not shown') },
{
code: 2,
description: t('show stderr to subagent and continue having it run'),
},
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.PreCompact]: [
{
code: 0,
description: t('stdout appended as custom compact instructions'),
},
{ code: 2, description: t('block compaction') },
{
code: 'Other',
description: t('show stderr to user only but continue with compaction'),
},
],
[HookEventName.PostCompact]: [
{ code: 0, description: t('stdout/stderr not shown') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.StopFailure]: [
{
code: 0,
description: t('fire-and-forget; exit status is ignored'),
},
{
code: 'Other',
description: t('fire-and-forget; exit status is ignored'),
},
],
[HookEventName.PermissionRequest]: [
{ code: 0, description: t('use hook decision if provided') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.PermissionDenied]: [
{ code: 0, description: t('stdout/stderr not shown') },
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.TodoCreated]: [
{ code: 0, description: t('allow todo creation') },
{
code: 2,
description: t('block todo creation and show reason to model'),
},
{ code: 'Other', description: t('show stderr to user only') },
],
[HookEventName.TodoCompleted]: [
{ code: 0, description: t('allow todo completion') },
{
code: 2,
description: t('block todo completion and show reason to model'),
},
{ code: 'Other', description: t('show stderr to user only') },
],
};
return exitCodesMap[eventName] || [];
}
/**
* Short one-line description for hooks list view
*/
export function getHookShortDescription(eventName: string): string {
const descriptions: Record<string, string> = {
[HookEventName.PreToolUse]: t('Before tool execution'),
[HookEventName.PostToolUse]: t('After tool execution'),
[HookEventName.PostToolUseFailure]: t('After tool execution fails'),
[HookEventName.PostToolBatch]: t('After all tool calls in a batch resolve'),
[HookEventName.Notification]: t('When notifications are sent'),
[HookEventName.InstructionsLoaded]: t('When instruction files are loaded'),
[HookEventName.UserPromptSubmit]: t('When the user submits a prompt'),
[HookEventName.UserPromptExpansion]: t(
'When a slash command expands into a prompt',
),
[HookEventName.SessionStart]: t('When a new session is started'),
[HookEventName.Stop]: t('Right before Qwen Code concludes its response'),
[HookEventName.SubagentStart]: t(
'When a subagent (Agent tool call) is started',
),
[HookEventName.SubagentStop]: t(
'Right before a subagent concludes its response',
),
[HookEventName.PreCompact]: t('Before conversation compaction'),
[HookEventName.PostCompact]: t('After conversation compaction'),
[HookEventName.StopFailure]: t(
'When the turn ends due to an API error (fires instead of Stop)',
),
[HookEventName.SessionEnd]: t('When a session is ending'),
[HookEventName.PermissionRequest]: t(
'When a permission dialog is displayed',
),
[HookEventName.PermissionDenied]: t(
'When a tool call is denied before a permission dialog is displayed',
),
[HookEventName.TodoCreated]: t('When a new todo item is created'),
[HookEventName.TodoCompleted]: t('When a todo item is marked as completed'),
};
return descriptions[eventName] || '';
}
/**
* Detailed description for each hook event type (shown in detail view)
*/
export function getHookDescription(eventName: string): string {
const descriptions: Record<string, string> = {
[HookEventName.Stop]: '',
[HookEventName.PreToolUse]: t(
'Input to command is JSON of tool call arguments.',
),
[HookEventName.PostToolUse]: t(
'Input to command is JSON with fields "inputs" (tool call arguments) and "response" (tool call response).',
),
[HookEventName.PostToolUseFailure]: t(
'Input to command is JSON with tool_name, tool_input, tool_use_id, error, error_type, is_interrupt, and is_timeout.',
),
[HookEventName.PostToolBatch]: t(
'Input to command is JSON with tool_calls, an array of resolved tool calls containing tool_name, tool_input, tool_use_id, and tool_response.',
),
[HookEventName.Notification]: t(
'Input to command is JSON with notification message and type.',
),
[HookEventName.InstructionsLoaded]: t(
'Input to command is JSON with file_path, memory_type, load_reason, and optional trigger_file_path and parent_file_path.',
),
[HookEventName.UserPromptSubmit]: t(
'Input to command is JSON with original user prompt text.',
),
[HookEventName.UserPromptExpansion]: t(
'Input to command is JSON with command_name, command_args, and expanded prompt text.',
),
[HookEventName.SessionStart]: t(
'Input to command is JSON with session start source.',
),
[HookEventName.SessionEnd]: t(
'Input to command is JSON with session end reason.',
),
[HookEventName.SubagentStart]: t(
'Input to command is JSON with agent_id and agent_type.',
),
[HookEventName.SubagentStop]: t(
'Input to command is JSON with agent_id, agent_type, and agent_transcript_path.',
),
[HookEventName.PreCompact]: t(
'Input to command is JSON with compaction details.',
),
[HookEventName.PostCompact]: t(
'Input to command is JSON with trigger (manual/auto) and compact_summary. Output is ignored for control purposes.',
),
[HookEventName.StopFailure]: t(
'Input to command is JSON with error (rate_limit, authentication_failed, billing_error, invalid_request, server_error, max_output_tokens, unknown) and optional error_details. Fire-and-forget: output and exit status are ignored.',
),
[HookEventName.PermissionRequest]: t(
'Input to command is JSON with tool_name, tool_input, and tool_use_id. Output JSON with hookSpecificOutput containing decision to allow or deny.',
),
[HookEventName.PermissionDenied]: t(
'Input to command is JSON with tool_name, tool_input, tool_use_id, and reason.',
),
[HookEventName.TodoCreated]: t(
'Input to command is JSON with todo_id, todo_content, todo_status, all_todos, and phase. In validation, output JSON with decision (allow/block/deny) and reason. In postWrite, block/deny is ignored.',
),
[HookEventName.TodoCompleted]: t(
'Input to command is JSON with todo_id, todo_content, previous_status, all_todos, and phase. In validation, output JSON with decision (allow/block/deny) and reason. In postWrite, block/deny is ignored.',
),
};
return descriptions[eventName] || '';
}
/**
* Source display mapping (translated)
*/
export function getTranslatedSourceDisplayMap(): Record<
HooksConfigSource,
string
> {
return {
[HooksConfigSource.Project]: t('Local Settings'),
[HooksConfigSource.User]: t('User Settings'),
[HooksConfigSource.System]: t('System Settings'),
[HooksConfigSource.Extensions]: t('Extensions'),
[HooksConfigSource.Session]: t('Session (temporary)'),
};
}
export const DISPLAY_HOOK_EVENTS: HookEventName[] =
Object.values(HookEventName);
export function supportsMatchers(eventName: HookEventName): boolean {
return hookEventSupportsMatcher(eventName);
}
export function createEmptyHookEventInfo(
eventName: HookEventName,
): HookEventDisplayInfo {
return {
event: eventName,
shortDescription: getHookShortDescription(eventName),
description: getHookDescription(eventName),
exitCodes: getHookExitCodes(eventName),
matcherGroups: [],
};
}