aegiscode 3.1.7 → 3.1.10
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 +23 -15
- package/dist/main.js +555 -556
- package/package.json +4 -2
- package/src/agent/Agent.ts +903 -0
- package/src/agent/SimpleAgent.ts +48 -0
- package/src/agent/index.ts +54 -0
- package/src/agent/orchestrator/AppBuilder.ts +443 -0
- package/src/agent/orchestrator/CouncilAgent.ts +310 -0
- package/src/agent/orchestrator/DiscussionRoom.ts +462 -0
- package/src/agent/orchestrator/OrchestratorAgent.ts +637 -0
- package/src/agent/orchestrator/index.ts +38 -0
- package/src/agent/orchestrator/utils.ts +397 -0
- package/src/agent/pricing.ts +115 -0
- package/src/agent/router.ts +74 -0
- package/src/agent/routerStats.ts +121 -0
- package/src/agent/types.ts +318 -0
- package/src/auth/login.ts +383 -0
- package/src/cli/config.ts +189 -0
- package/src/cli/index.ts +17 -0
- package/src/cli/middleware.ts +119 -0
- package/src/cli/types.ts +75 -0
- package/src/config/ConfigManager.ts +587 -0
- package/src/config/index.ts +7 -0
- package/src/config/types.ts +584 -0
- package/src/context/CompactionService.ts +300 -0
- package/src/context/ContextManager.ts +450 -0
- package/src/context/FileAnalyzer.ts +267 -0
- package/src/context/TokenCounter.ts +265 -0
- package/src/context/index.ts +27 -0
- package/src/context/storage/CacheStore.ts +176 -0
- package/src/context/storage/JSONLStore.ts +201 -0
- package/src/context/storage/MemoryStore.ts +205 -0
- package/src/context/storage/PersistentStore.ts +327 -0
- package/src/context/storage/index.ts +9 -0
- package/src/context/storage/pathUtils.ts +114 -0
- package/src/context/test.ts +309 -0
- package/src/context/types.ts +268 -0
- package/src/hooks/HookExecutor.ts +434 -0
- package/src/hooks/HookManager.ts +596 -0
- package/src/hooks/HookService.ts +269 -0
- package/src/hooks/Matcher.ts +157 -0
- package/src/hooks/index.ts +63 -0
- package/src/hooks/types.ts +424 -0
- package/src/main.tsx +596 -0
- package/src/mcp/HealthMonitor.ts +150 -0
- package/src/mcp/McpClient.ts +491 -0
- package/src/mcp/McpRegistry.ts +321 -0
- package/src/mcp/createMcpTool.ts +251 -0
- package/src/mcp/index.ts +15 -0
- package/src/mcp/server.ts +334 -0
- package/src/mcp/test-server.ts +88 -0
- package/src/mcp/test.ts +372 -0
- package/src/mcp/types.ts +247 -0
- package/src/memory/AgentMemoryBus.ts +432 -0
- package/src/memory/CloudSync.ts +99 -0
- package/src/memory/DriveSync.ts +106 -0
- package/src/memory/SharedMemory.ts +951 -0
- package/src/memory/index.ts +14 -0
- package/src/memory/machineFingerprint.ts +40 -0
- package/src/orchestrator/SubAgentMetadata.ts +136 -0
- package/src/prompts/builder.ts +213 -0
- package/src/prompts/default.ts +144 -0
- package/src/prompts/index.ts +16 -0
- package/src/prompts/plan.ts +64 -0
- package/src/prompts/test.ts +78 -0
- package/src/services/AnthropicChatService.ts +341 -0
- package/src/services/ChatService.ts +347 -0
- package/src/services/ClaudeCliChatService.ts +256 -0
- package/src/services/CloudSync.ts +168 -0
- package/src/services/CostLedger.ts +211 -0
- package/src/services/Heartbeat.ts +135 -0
- package/src/services/LearningCollector.ts +291 -0
- package/src/services/OllamaInstaller.ts +342 -0
- package/src/services/VersionChecker.ts +445 -0
- package/src/services/index.ts +57 -0
- package/src/services/streaming/OpenAIEventAdapter.ts +126 -0
- package/src/services/streaming/RenderingProfile.ts +90 -0
- package/src/services/streaming/StreamEventParser.ts +181 -0
- package/src/services/streaming/ThrottledRenderer.ts +139 -0
- package/src/services/streaming/TranscriptBuffer.ts +574 -0
- package/src/services/streaming/eventStatusMap.ts +52 -0
- package/src/services/streaming/index.ts +46 -0
- package/src/services/streaming/renderFormatting.ts +79 -0
- package/src/services/streaming/types.ts +234 -0
- package/src/skills/SkillLoader.ts +126 -0
- package/src/skills/SkillRegistry.ts +366 -0
- package/src/skills/index.ts +48 -0
- package/src/skills/types.ts +146 -0
- package/src/slash-commands/billing.ts +70 -0
- package/src/slash-commands/build.ts +413 -0
- package/src/slash-commands/builtinCommands.ts +2733 -0
- package/src/slash-commands/clone.ts +242 -0
- package/src/slash-commands/council.ts +125 -0
- package/src/slash-commands/custom/CustomCommandExecutor.ts +143 -0
- package/src/slash-commands/custom/CustomCommandLoader.ts +251 -0
- package/src/slash-commands/custom/CustomCommandRegistry.ts +144 -0
- package/src/slash-commands/custom/index.ts +7 -0
- package/src/slash-commands/debate.ts +254 -0
- package/src/slash-commands/gmail.ts +105 -0
- package/src/slash-commands/index.ts +388 -0
- package/src/slash-commands/mcpCommand.ts +205 -0
- package/src/slash-commands/types.ts +201 -0
- package/src/store/index.ts +76 -0
- package/src/store/selectors.ts +246 -0
- package/src/store/slices/appSlice.ts +205 -0
- package/src/store/slices/commandSlice.ts +115 -0
- package/src/store/slices/configSlice.ts +46 -0
- package/src/store/slices/focusSlice.ts +64 -0
- package/src/store/slices/index.ts +9 -0
- package/src/store/slices/sessionSlice.ts +424 -0
- package/src/store/streaming-buffer.ts +425 -0
- package/src/store/test.ts +296 -0
- package/src/store/types.ts +274 -0
- package/src/store/vanilla.ts +186 -0
- package/src/tools/builtin/bash.ts +236 -0
- package/src/tools/builtin/council.ts +105 -0
- package/src/tools/builtin/edit.ts +213 -0
- package/src/tools/builtin/glob.ts +136 -0
- package/src/tools/builtin/grep.ts +263 -0
- package/src/tools/builtin/index.ts +61 -0
- package/src/tools/builtin/memory.ts +66 -0
- package/src/tools/builtin/read.ts +168 -0
- package/src/tools/builtin/skill.ts +97 -0
- package/src/tools/builtin/snapshot.ts +40 -0
- package/src/tools/builtin/task.ts +106 -0
- package/src/tools/builtin/write.ts +134 -0
- package/src/tools/createTool.ts +221 -0
- package/src/tools/execution/ExecutionPipeline.ts +263 -0
- package/src/tools/execution/index.ts +40 -0
- package/src/tools/execution/stages/CacheStage.ts +131 -0
- package/src/tools/execution/stages/ConfirmationStage.ts +203 -0
- package/src/tools/execution/stages/DiscoveryStage.ts +46 -0
- package/src/tools/execution/stages/ExecutionStage.ts +48 -0
- package/src/tools/execution/stages/FormattingStage.ts +44 -0
- package/src/tools/execution/stages/HookStage.ts +72 -0
- package/src/tools/execution/stages/PermissionStage.ts +287 -0
- package/src/tools/execution/stages/PostHookStage.ts +71 -0
- package/src/tools/execution/stages/index.ts +12 -0
- package/src/tools/execution/test.ts +266 -0
- package/src/tools/execution/types.ts +273 -0
- package/src/tools/index.ts +81 -0
- package/src/tools/registry.ts +304 -0
- package/src/tools/schemas.ts +109 -0
- package/src/tools/test.ts +220 -0
- package/src/tools/types.ts +175 -0
- package/src/tools/validation/PermissionChecker.ts +242 -0
- package/src/tools/validation/SensitiveFileDetector.ts +210 -0
- package/src/tools/validation/index.ts +11 -0
- package/src/ui/App.tsx +166 -0
- package/src/ui/components/AegisInterface.tsx +484 -0
- package/src/ui/components/common/ChatSearch.tsx +150 -0
- package/src/ui/components/common/ErrorBoundary.tsx +82 -0
- package/src/ui/components/common/ExitMessage.tsx +120 -0
- package/src/ui/components/common/LoadingIndicator.tsx +49 -0
- package/src/ui/components/common/index.ts +6 -0
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +208 -0
- package/src/ui/components/dialog/InteractiveSelector.tsx +149 -0
- package/src/ui/components/dialog/SetupWizard.tsx +297 -0
- package/src/ui/components/dialog/UpdatePrompt.tsx +155 -0
- package/src/ui/components/dialog/index.ts +8 -0
- package/src/ui/components/index.ts +28 -0
- package/src/ui/components/input/CommandSuggestions.tsx +139 -0
- package/src/ui/components/input/CustomTextInput.tsx +220 -0
- package/src/ui/components/input/InputArea.tsx +361 -0
- package/src/ui/components/input/PromptSuggestions.tsx +66 -0
- package/src/ui/components/input/index.ts +6 -0
- package/src/ui/components/layout/ChatStatusBar.tsx +90 -0
- package/src/ui/components/layout/ContextBar.tsx +79 -0
- package/src/ui/components/layout/MessageArea.tsx +96 -0
- package/src/ui/components/layout/MessageList.tsx +647 -0
- package/src/ui/components/layout/MessageSeparator.tsx +26 -0
- package/src/ui/components/layout/WelcomeMessage.tsx +93 -0
- package/src/ui/components/layout/index.ts +7 -0
- package/src/ui/components/markdown/CodeHighlighter.tsx +292 -0
- package/src/ui/components/markdown/MessageRenderer.tsx +1211 -0
- package/src/ui/components/markdown/index.ts +8 -0
- package/src/ui/components/markdown/parser.ts +336 -0
- package/src/ui/components/markdown/types.ts +66 -0
- package/src/ui/focus/FocusManager.ts +137 -0
- package/src/ui/focus/index.ts +13 -0
- package/src/ui/focus/types.ts +54 -0
- package/src/ui/focus/useFocus.ts +75 -0
- package/src/ui/hooks/index.ts +11 -0
- package/src/ui/hooks/useAgent.ts +284 -0
- package/src/ui/hooks/useCommandHistory.ts +87 -0
- package/src/ui/hooks/useCommandProcessor.ts +443 -0
- package/src/ui/hooks/useConfirmation.ts +99 -0
- package/src/ui/hooks/useCtrlCHandler.ts +100 -0
- package/src/ui/hooks/useInputBuffer.ts +122 -0
- package/src/ui/hooks/useTerminalSize.ts +68 -0
- package/src/ui/hooks/useTerminalWidth.ts +5 -0
- package/src/ui/hooks/useWindowedList.ts +118 -0
- package/src/ui/render-debugger.ts +621 -0
- package/src/ui/test.ts +189 -0
- package/src/ui/themes/ThemeManager.ts +332 -0
- package/src/ui/themes/aegisTheme.ts +87 -0
- package/src/ui/themes/darkTheme.ts +87 -0
- package/src/ui/themes/defaultTheme.ts +85 -0
- package/src/ui/themes/index.ts +10 -0
- package/src/ui/themes/lightTheme.ts +89 -0
- package/src/ui/themes/popularThemes.ts +187 -0
- package/src/ui/themes/types.ts +130 -0
- package/src/utils/clipboard.ts +48 -0
- package/src/utils/debug.ts +43 -0
- package/src/utils/environment.ts +68 -0
- package/src/utils/index.ts +10 -0
|
@@ -0,0 +1,434 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook 执行器
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import type {
|
|
9
|
+
Hook,
|
|
10
|
+
CommandHook,
|
|
11
|
+
HookInput,
|
|
12
|
+
HookExecutionContext,
|
|
13
|
+
HookExecutionResult,
|
|
14
|
+
HookExitCode,
|
|
15
|
+
HookSpecificOutput,
|
|
16
|
+
PreToolHookResult,
|
|
17
|
+
PostToolHookResult,
|
|
18
|
+
PermissionHookResult,
|
|
19
|
+
StopHookResult,
|
|
20
|
+
UserPromptHookResult,
|
|
21
|
+
CompactionHookResult,
|
|
22
|
+
} from './types.js';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Hook 执行器
|
|
26
|
+
*/
|
|
27
|
+
export class HookExecutor {
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
*
|
|
31
|
+
*
|
|
32
|
+
* 1. 第一个 deny 需要立即中断
|
|
33
|
+
* 2. updatedInput 需要累积应用
|
|
34
|
+
*/
|
|
35
|
+
async executePreToolHooks(
|
|
36
|
+
hooks: Hook[],
|
|
37
|
+
input: HookInput,
|
|
38
|
+
context: HookExecutionContext
|
|
39
|
+
): Promise<PreToolHookResult> {
|
|
40
|
+
let cumulativeInput = (input as { tool_input?: Record<string, unknown> }).tool_input || {};
|
|
41
|
+
const warnings: string[] = [];
|
|
42
|
+
|
|
43
|
+
for (const hook of hooks) {
|
|
44
|
+
const hookInput = { ...input, tool_input: cumulativeInput };
|
|
45
|
+
const result = await this.executeHook(hook, hookInput, context);
|
|
46
|
+
|
|
47
|
+
// 处理执行失
|
|
48
|
+
if (!result.success) {
|
|
49
|
+
if (result.blocking) {
|
|
50
|
+
return { decision: 'deny', reason: result.error };
|
|
51
|
+
}
|
|
52
|
+
if (result.needsConfirmation) {
|
|
53
|
+
return { decision: 'ask', reason: result.warning };
|
|
54
|
+
}
|
|
55
|
+
if (result.warning) {
|
|
56
|
+
warnings.push(result.warning);
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 处
|
|
62
|
+
const specific = result.output?.hookSpecificOutput;
|
|
63
|
+
if (specific && 'permissionDecision' in specific) {
|
|
64
|
+
if (specific.permissionDecision === 'deny') {
|
|
65
|
+
return { decision: 'deny', reason: specific.permissionDecisionReason };
|
|
66
|
+
}
|
|
67
|
+
if (specific.permissionDecision === 'ask') {
|
|
68
|
+
return { decision: 'ask', reason: specific.permissionDecisionReason };
|
|
69
|
+
}
|
|
70
|
+
// 累
|
|
71
|
+
if (specific.updatedInput) {
|
|
72
|
+
cumulativeInput = { ...cumulativeInput, ...specific.updatedInput };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
decision: 'allow',
|
|
79
|
+
modifiedInput: cumulativeInput,
|
|
80
|
+
warning: warnings.length > 0 ? warnings.join('\n') : undefined,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
*
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
async executePostToolHooks(
|
|
90
|
+
hooks: Hook[],
|
|
91
|
+
input: HookInput,
|
|
92
|
+
context: HookExecutionContext
|
|
93
|
+
): Promise<PostToolHookResult> {
|
|
94
|
+
const maxConcurrent = context.config.maxConcurrentHooks || 5;
|
|
95
|
+
const results = await this.executeHooksConcurrently(hooks, input, context, maxConcurrent);
|
|
96
|
+
|
|
97
|
+
// 合并结
|
|
98
|
+
const additionalContexts: string[] = [];
|
|
99
|
+
let modifiedOutput: unknown;
|
|
100
|
+
|
|
101
|
+
for (const result of results) {
|
|
102
|
+
// 如果有 stdout 输出,作为额外上下
|
|
103
|
+
if (result.stdout && result.stdout.trim()) {
|
|
104
|
+
additionalContexts.push(result.stdout.trim());
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const specific = result.output?.hookSpecificOutput;
|
|
108
|
+
if (specific && 'additionalContext' in specific) {
|
|
109
|
+
if (specific.additionalContext) {
|
|
110
|
+
additionalContexts.push(specific.additionalContext);
|
|
111
|
+
}
|
|
112
|
+
if (specific.updatedOutput !== undefined) {
|
|
113
|
+
modifiedOutput = specific.updatedOutput;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
additionalContext: additionalContexts.length > 0
|
|
120
|
+
? additionalContexts.join('\n\n')
|
|
121
|
+
: undefined,
|
|
122
|
+
modifiedOutput,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
*
|
|
128
|
+
*
|
|
129
|
+
*
|
|
130
|
+
*/
|
|
131
|
+
async executePermissionHooks(
|
|
132
|
+
hooks: Hook[],
|
|
133
|
+
input: HookInput,
|
|
134
|
+
context: HookExecutionContext
|
|
135
|
+
): Promise<PermissionHookResult> {
|
|
136
|
+
for (const hook of hooks) {
|
|
137
|
+
const result = await this.executeHook(hook, input, context);
|
|
138
|
+
|
|
139
|
+
if (!result.success) continue;
|
|
140
|
+
|
|
141
|
+
const specific = result.output?.hookSpecificOutput;
|
|
142
|
+
if (specific && 'decision' in specific) {
|
|
143
|
+
if (specific.decision === 'approve' || specific.decision === 'deny') {
|
|
144
|
+
return { decision: specific.decision, reason: specific.reason };
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { decision: 'ask' };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
*
|
|
155
|
+
*
|
|
156
|
+
*/
|
|
157
|
+
async executeStopHooks(
|
|
158
|
+
hooks: Hook[],
|
|
159
|
+
input: HookInput,
|
|
160
|
+
context: HookExecutionContext
|
|
161
|
+
): Promise<StopHookResult> {
|
|
162
|
+
for (const hook of hooks) {
|
|
163
|
+
const result = await this.executeHook(hook, input, context);
|
|
164
|
+
|
|
165
|
+
if (!result.success) continue;
|
|
166
|
+
|
|
167
|
+
const specific = result.output?.hookSpecificOutput;
|
|
168
|
+
if (specific && 'continue' in specific && specific.continue) {
|
|
169
|
+
return { shouldContinue: true, reason: specific.reason };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return { shouldContinue: false };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
*
|
|
178
|
+
*
|
|
179
|
+
* stdout 合并注入
|
|
180
|
+
*/
|
|
181
|
+
async executeUserPromptHooks(
|
|
182
|
+
hooks: Hook[],
|
|
183
|
+
input: HookInput,
|
|
184
|
+
context: HookExecutionContext
|
|
185
|
+
): Promise<UserPromptHookResult> {
|
|
186
|
+
const maxConcurrent = context.config.maxConcurrentHooks || 5;
|
|
187
|
+
const results = await this.executeHooksConcurrently(hooks, input, context, maxConcurrent);
|
|
188
|
+
|
|
189
|
+
const contexts: string[] = [];
|
|
190
|
+
|
|
191
|
+
for (const result of results) {
|
|
192
|
+
if (result.success && result.stdout && result.stdout.trim()) {
|
|
193
|
+
contexts.push(result.stdout.trim());
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
injectedContext: contexts.length > 0 ? contexts.join('\n\n') : undefined,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
*/
|
|
205
|
+
async executeCompactionHooks(
|
|
206
|
+
hooks: Hook[],
|
|
207
|
+
input: HookInput,
|
|
208
|
+
context: HookExecutionContext
|
|
209
|
+
): Promise<CompactionHookResult> {
|
|
210
|
+
for (const hook of hooks) {
|
|
211
|
+
const result = await this.executeHook(hook, input, context);
|
|
212
|
+
|
|
213
|
+
if (!result.success) continue;
|
|
214
|
+
|
|
215
|
+
const specific = result.output?.hookSpecificOutput;
|
|
216
|
+
if (specific && 'prevent' in specific && specific.prevent) {
|
|
217
|
+
return { shouldPrevent: true, reason: specific.reason };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return { shouldPrevent: false };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
*
|
|
226
|
+
*/
|
|
227
|
+
async executeGenericHooks(
|
|
228
|
+
hooks: Hook[],
|
|
229
|
+
input: HookInput,
|
|
230
|
+
context: HookExecutionContext
|
|
231
|
+
): Promise<void> {
|
|
232
|
+
const maxConcurrent = context.config.maxConcurrentHooks || 5;
|
|
233
|
+
await this.executeHooksConcurrently(hooks, input, context, maxConcurrent);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
*
|
|
238
|
+
*/
|
|
239
|
+
private async executeHooksConcurrently(
|
|
240
|
+
hooks: Hook[],
|
|
241
|
+
input: HookInput,
|
|
242
|
+
context: HookExecutionContext,
|
|
243
|
+
maxConcurrent: number
|
|
244
|
+
): Promise<HookExecutionResult[]> {
|
|
245
|
+
const results: HookExecutionResult[] = [];
|
|
246
|
+
|
|
247
|
+
// 分批执
|
|
248
|
+
for (let i = 0; i < hooks.length; i += maxConcurrent) {
|
|
249
|
+
const batch = hooks.slice(i, i + maxConcurrent);
|
|
250
|
+
const batchResults = await Promise.all(
|
|
251
|
+
batch.map(hook => this.executeHook(hook, input, context))
|
|
252
|
+
);
|
|
253
|
+
results.push(...batchResults);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return results;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
*
|
|
261
|
+
*/
|
|
262
|
+
private async executeHook(
|
|
263
|
+
hook: Hook,
|
|
264
|
+
input: HookInput,
|
|
265
|
+
context: HookExecutionContext
|
|
266
|
+
): Promise<HookExecutionResult> {
|
|
267
|
+
if (hook.type === 'command') {
|
|
268
|
+
return this.executeCommandHook(hook, input, context);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
success: false,
|
|
273
|
+
exitCode: 1,
|
|
274
|
+
stdout: '',
|
|
275
|
+
stderr: `Unknown hook type: ${(hook as { type: string }).type}`,
|
|
276
|
+
duration: 0,
|
|
277
|
+
error: 'Unknown hook type',
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
*
|
|
283
|
+
*/
|
|
284
|
+
private async executeCommandHook(
|
|
285
|
+
hook: CommandHook,
|
|
286
|
+
input: HookInput,
|
|
287
|
+
context: HookExecutionContext
|
|
288
|
+
): Promise<HookExecutionResult> {
|
|
289
|
+
const timeoutMs = (hook.timeout ?? context.config.defaultTimeout ?? 60) * 1000;
|
|
290
|
+
const startTime = Date.now();
|
|
291
|
+
|
|
292
|
+
return new Promise((resolve) => {
|
|
293
|
+
let stdout = '';
|
|
294
|
+
let stderr = '';
|
|
295
|
+
let timedOut = false;
|
|
296
|
+
|
|
297
|
+
// 启动子进
|
|
298
|
+
const child = spawn('sh', ['-c', hook.command], {
|
|
299
|
+
cwd: context.projectDir,
|
|
300
|
+
env: {
|
|
301
|
+
...process.env,
|
|
302
|
+
// 注入环境变
|
|
303
|
+
HOOK_EVENT: input.hook_event_name,
|
|
304
|
+
SESSION_ID: input.session_id,
|
|
305
|
+
PROJECT_DIR: input.project_dir,
|
|
306
|
+
},
|
|
307
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
// 发送 JSON 输入
|
|
311
|
+
child.stdin.write(JSON.stringify(input));
|
|
312
|
+
child.stdin.end();
|
|
313
|
+
|
|
314
|
+
// 收集输
|
|
315
|
+
child.stdout.on('data', (data) => {
|
|
316
|
+
stdout += data.toString();
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
child.stderr.on('data', (data) => {
|
|
320
|
+
stderr += data.toString();
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// 超时处
|
|
324
|
+
const timer = setTimeout(() => {
|
|
325
|
+
timedOut = true;
|
|
326
|
+
child.kill('SIGTERM');
|
|
327
|
+
}, timeoutMs);
|
|
328
|
+
|
|
329
|
+
// 完成处
|
|
330
|
+
child.on('close', (code) => {
|
|
331
|
+
clearTimeout(timer);
|
|
332
|
+
const duration = Date.now() - startTime;
|
|
333
|
+
|
|
334
|
+
if (timedOut) {
|
|
335
|
+
resolve(this.handleTimeout(context, duration));
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const exitCode = code ?? 0;
|
|
340
|
+
resolve(this.parseResult(exitCode, stdout, stderr, duration, context));
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
child.on('error', (err) => {
|
|
344
|
+
clearTimeout(timer);
|
|
345
|
+
const duration = Date.now() - startTime;
|
|
346
|
+
resolve({
|
|
347
|
+
success: false,
|
|
348
|
+
exitCode: 1,
|
|
349
|
+
stdout,
|
|
350
|
+
stderr: err.message,
|
|
351
|
+
duration,
|
|
352
|
+
error: err.message,
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
*
|
|
360
|
+
*/
|
|
361
|
+
private handleTimeout(
|
|
362
|
+
context: HookExecutionContext,
|
|
363
|
+
duration: number
|
|
364
|
+
): HookExecutionResult {
|
|
365
|
+
const behavior = context.config.timeoutBehavior || 'ignore';
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
success: behavior === 'ignore',
|
|
369
|
+
exitCode: 124, // HookExitCode.TIMEOUT
|
|
370
|
+
stdout: '',
|
|
371
|
+
stderr: 'Hook execution timed out',
|
|
372
|
+
duration,
|
|
373
|
+
error: 'Timeout',
|
|
374
|
+
blocking: behavior === 'deny',
|
|
375
|
+
needsConfirmation: behavior === 'ask',
|
|
376
|
+
warning: 'Hook timed out',
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
*
|
|
382
|
+
*/
|
|
383
|
+
private parseResult(
|
|
384
|
+
exitCode: number,
|
|
385
|
+
stdout: string,
|
|
386
|
+
stderr: string,
|
|
387
|
+
duration: number,
|
|
388
|
+
context: HookExecutionContext
|
|
389
|
+
): HookExecutionResult {
|
|
390
|
+
const result: HookExecutionResult = {
|
|
391
|
+
success: exitCode === 0,
|
|
392
|
+
exitCode,
|
|
393
|
+
stdout,
|
|
394
|
+
stderr,
|
|
395
|
+
duration,
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// 处理退出
|
|
399
|
+
if (exitCode === 2) {
|
|
400
|
+
// BLOCKING_ERROR
|
|
401
|
+
result.blocking = true;
|
|
402
|
+
result.error = stderr || 'Blocking error';
|
|
403
|
+
return result;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (exitCode === 1) {
|
|
407
|
+
// NON_BLOCKING_ERROR
|
|
408
|
+
const behavior = context.config.failureBehavior || 'ignore';
|
|
409
|
+
result.success = behavior === 'ignore';
|
|
410
|
+
result.blocking = behavior === 'deny';
|
|
411
|
+
result.needsConfirmation = behavior === 'ask';
|
|
412
|
+
result.warning = stderr || 'Non-blocking error';
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// 成功 - 尝试解析 JSON 输
|
|
417
|
+
if (stdout.trim()) {
|
|
418
|
+
try {
|
|
419
|
+
const parsed = JSON.parse(stdout.trim());
|
|
420
|
+
result.output = {
|
|
421
|
+
hookSpecificOutput: parsed.hookSpecificOutput || parsed,
|
|
422
|
+
rawOutput: stdout,
|
|
423
|
+
};
|
|
424
|
+
} catch {
|
|
425
|
+
// 非 JSON 输出,作为原始文
|
|
426
|
+
result.output = {
|
|
427
|
+
rawOutput: stdout,
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return result;
|
|
433
|
+
}
|
|
434
|
+
}
|