aegiscode 3.1.10 → 3.2.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/bin/aegis-cli-linux-x64 +0 -0
- package/bin/cli.js +68 -0
- package/package.json +11 -48
- package/scripts/download-binary.mjs +155 -0
- package/dist/main.js +0 -2171
- package/gui/run-gui.cjs +0 -13
- package/src/agent/Agent.ts +0 -903
- package/src/agent/SimpleAgent.ts +0 -48
- package/src/agent/index.ts +0 -54
- package/src/agent/orchestrator/AppBuilder.ts +0 -443
- package/src/agent/orchestrator/CouncilAgent.ts +0 -310
- package/src/agent/orchestrator/DiscussionRoom.ts +0 -462
- package/src/agent/orchestrator/OrchestratorAgent.ts +0 -637
- package/src/agent/orchestrator/index.ts +0 -38
- package/src/agent/orchestrator/utils.ts +0 -397
- package/src/agent/pricing.ts +0 -115
- package/src/agent/router.ts +0 -74
- package/src/agent/routerStats.ts +0 -121
- package/src/agent/types.ts +0 -318
- package/src/auth/login.ts +0 -383
- package/src/cli/config.ts +0 -189
- package/src/cli/index.ts +0 -17
- package/src/cli/middleware.ts +0 -119
- package/src/cli/types.ts +0 -75
- package/src/config/ConfigManager.ts +0 -587
- package/src/config/index.ts +0 -7
- package/src/config/types.ts +0 -584
- package/src/context/CompactionService.ts +0 -300
- package/src/context/ContextManager.ts +0 -450
- package/src/context/FileAnalyzer.ts +0 -267
- package/src/context/TokenCounter.ts +0 -265
- package/src/context/index.ts +0 -27
- package/src/context/storage/CacheStore.ts +0 -176
- package/src/context/storage/JSONLStore.ts +0 -201
- package/src/context/storage/MemoryStore.ts +0 -205
- package/src/context/storage/PersistentStore.ts +0 -327
- package/src/context/storage/index.ts +0 -9
- package/src/context/storage/pathUtils.ts +0 -114
- package/src/context/test.ts +0 -309
- package/src/context/types.ts +0 -268
- package/src/hooks/HookExecutor.ts +0 -434
- package/src/hooks/HookManager.ts +0 -596
- package/src/hooks/HookService.ts +0 -269
- package/src/hooks/Matcher.ts +0 -157
- package/src/hooks/index.ts +0 -63
- package/src/hooks/types.ts +0 -424
- package/src/main.tsx +0 -596
- package/src/mcp/HealthMonitor.ts +0 -150
- package/src/mcp/McpClient.ts +0 -491
- package/src/mcp/McpRegistry.ts +0 -321
- package/src/mcp/createMcpTool.ts +0 -251
- package/src/mcp/index.ts +0 -15
- package/src/mcp/server.ts +0 -334
- package/src/mcp/test-server.ts +0 -88
- package/src/mcp/test.ts +0 -372
- package/src/mcp/types.ts +0 -247
- package/src/memory/AgentMemoryBus.ts +0 -432
- package/src/memory/CloudSync.ts +0 -99
- package/src/memory/DriveSync.ts +0 -106
- package/src/memory/SharedMemory.ts +0 -951
- package/src/memory/index.ts +0 -14
- package/src/memory/machineFingerprint.ts +0 -40
- package/src/orchestrator/SubAgentMetadata.ts +0 -136
- package/src/prompts/builder.ts +0 -213
- package/src/prompts/default.ts +0 -144
- package/src/prompts/index.ts +0 -16
- package/src/prompts/plan.ts +0 -64
- package/src/prompts/test.ts +0 -78
- package/src/services/AnthropicChatService.ts +0 -341
- package/src/services/ChatService.ts +0 -347
- package/src/services/ClaudeCliChatService.ts +0 -256
- package/src/services/CloudSync.ts +0 -168
- package/src/services/CostLedger.ts +0 -211
- package/src/services/Heartbeat.ts +0 -135
- package/src/services/LearningCollector.ts +0 -291
- package/src/services/OllamaInstaller.ts +0 -342
- package/src/services/VersionChecker.ts +0 -445
- package/src/services/index.ts +0 -57
- package/src/services/streaming/OpenAIEventAdapter.ts +0 -126
- package/src/services/streaming/RenderingProfile.ts +0 -90
- package/src/services/streaming/StreamEventParser.ts +0 -181
- package/src/services/streaming/ThrottledRenderer.ts +0 -139
- package/src/services/streaming/TranscriptBuffer.ts +0 -574
- package/src/services/streaming/eventStatusMap.ts +0 -52
- package/src/services/streaming/index.ts +0 -46
- package/src/services/streaming/renderFormatting.ts +0 -79
- package/src/services/streaming/types.ts +0 -234
- package/src/skills/SkillLoader.ts +0 -126
- package/src/skills/SkillRegistry.ts +0 -366
- package/src/skills/index.ts +0 -48
- package/src/skills/types.ts +0 -146
- package/src/slash-commands/billing.ts +0 -70
- package/src/slash-commands/build.ts +0 -413
- package/src/slash-commands/builtinCommands.ts +0 -2733
- package/src/slash-commands/clone.ts +0 -242
- package/src/slash-commands/council.ts +0 -125
- package/src/slash-commands/custom/CustomCommandExecutor.ts +0 -143
- package/src/slash-commands/custom/CustomCommandLoader.ts +0 -251
- package/src/slash-commands/custom/CustomCommandRegistry.ts +0 -144
- package/src/slash-commands/custom/index.ts +0 -7
- package/src/slash-commands/debate.ts +0 -254
- package/src/slash-commands/gmail.ts +0 -105
- package/src/slash-commands/index.ts +0 -388
- package/src/slash-commands/mcpCommand.ts +0 -205
- package/src/slash-commands/types.ts +0 -201
- package/src/store/index.ts +0 -76
- package/src/store/selectors.ts +0 -246
- package/src/store/slices/appSlice.ts +0 -205
- package/src/store/slices/commandSlice.ts +0 -115
- package/src/store/slices/configSlice.ts +0 -46
- package/src/store/slices/focusSlice.ts +0 -64
- package/src/store/slices/index.ts +0 -9
- package/src/store/slices/sessionSlice.ts +0 -424
- package/src/store/streaming-buffer.ts +0 -425
- package/src/store/test.ts +0 -296
- package/src/store/types.ts +0 -274
- package/src/store/vanilla.ts +0 -186
- package/src/tools/builtin/bash.ts +0 -236
- package/src/tools/builtin/council.ts +0 -105
- package/src/tools/builtin/edit.ts +0 -213
- package/src/tools/builtin/glob.ts +0 -136
- package/src/tools/builtin/grep.ts +0 -263
- package/src/tools/builtin/index.ts +0 -61
- package/src/tools/builtin/memory.ts +0 -66
- package/src/tools/builtin/read.ts +0 -168
- package/src/tools/builtin/skill.ts +0 -97
- package/src/tools/builtin/snapshot.ts +0 -40
- package/src/tools/builtin/task.ts +0 -106
- package/src/tools/builtin/write.ts +0 -134
- package/src/tools/createTool.ts +0 -221
- package/src/tools/execution/ExecutionPipeline.ts +0 -263
- package/src/tools/execution/index.ts +0 -40
- package/src/tools/execution/stages/CacheStage.ts +0 -131
- package/src/tools/execution/stages/ConfirmationStage.ts +0 -203
- package/src/tools/execution/stages/DiscoveryStage.ts +0 -46
- package/src/tools/execution/stages/ExecutionStage.ts +0 -48
- package/src/tools/execution/stages/FormattingStage.ts +0 -44
- package/src/tools/execution/stages/HookStage.ts +0 -72
- package/src/tools/execution/stages/PermissionStage.ts +0 -287
- package/src/tools/execution/stages/PostHookStage.ts +0 -71
- package/src/tools/execution/stages/index.ts +0 -12
- package/src/tools/execution/test.ts +0 -266
- package/src/tools/execution/types.ts +0 -273
- package/src/tools/index.ts +0 -81
- package/src/tools/registry.ts +0 -304
- package/src/tools/schemas.ts +0 -109
- package/src/tools/test.ts +0 -220
- package/src/tools/types.ts +0 -175
- package/src/tools/validation/PermissionChecker.ts +0 -242
- package/src/tools/validation/SensitiveFileDetector.ts +0 -210
- package/src/tools/validation/index.ts +0 -11
- package/src/ui/App.tsx +0 -166
- package/src/ui/components/AegisInterface.tsx +0 -484
- package/src/ui/components/common/ChatSearch.tsx +0 -150
- package/src/ui/components/common/ErrorBoundary.tsx +0 -82
- package/src/ui/components/common/ExitMessage.tsx +0 -120
- package/src/ui/components/common/LoadingIndicator.tsx +0 -49
- package/src/ui/components/common/index.ts +0 -6
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +0 -208
- package/src/ui/components/dialog/InteractiveSelector.tsx +0 -149
- package/src/ui/components/dialog/SetupWizard.tsx +0 -297
- package/src/ui/components/dialog/UpdatePrompt.tsx +0 -155
- package/src/ui/components/dialog/index.ts +0 -8
- package/src/ui/components/index.ts +0 -28
- package/src/ui/components/input/CommandSuggestions.tsx +0 -139
- package/src/ui/components/input/CustomTextInput.tsx +0 -220
- package/src/ui/components/input/InputArea.tsx +0 -361
- package/src/ui/components/input/PromptSuggestions.tsx +0 -66
- package/src/ui/components/input/index.ts +0 -6
- package/src/ui/components/layout/ChatStatusBar.tsx +0 -90
- package/src/ui/components/layout/ContextBar.tsx +0 -79
- package/src/ui/components/layout/MessageArea.tsx +0 -96
- package/src/ui/components/layout/MessageList.tsx +0 -647
- package/src/ui/components/layout/MessageSeparator.tsx +0 -26
- package/src/ui/components/layout/WelcomeMessage.tsx +0 -93
- package/src/ui/components/layout/index.ts +0 -7
- package/src/ui/components/markdown/CodeHighlighter.tsx +0 -292
- package/src/ui/components/markdown/MessageRenderer.tsx +0 -1211
- package/src/ui/components/markdown/index.ts +0 -8
- package/src/ui/components/markdown/parser.ts +0 -336
- package/src/ui/components/markdown/types.ts +0 -66
- package/src/ui/focus/FocusManager.ts +0 -137
- package/src/ui/focus/index.ts +0 -13
- package/src/ui/focus/types.ts +0 -54
- package/src/ui/focus/useFocus.ts +0 -75
- package/src/ui/hooks/index.ts +0 -11
- package/src/ui/hooks/useAgent.ts +0 -284
- package/src/ui/hooks/useCommandHistory.ts +0 -87
- package/src/ui/hooks/useCommandProcessor.ts +0 -443
- package/src/ui/hooks/useConfirmation.ts +0 -99
- package/src/ui/hooks/useCtrlCHandler.ts +0 -100
- package/src/ui/hooks/useInputBuffer.ts +0 -122
- package/src/ui/hooks/useTerminalSize.ts +0 -68
- package/src/ui/hooks/useTerminalWidth.ts +0 -5
- package/src/ui/hooks/useWindowedList.ts +0 -118
- package/src/ui/render-debugger.ts +0 -621
- package/src/ui/test.ts +0 -189
- package/src/ui/themes/ThemeManager.ts +0 -332
- package/src/ui/themes/aegisTheme.ts +0 -87
- package/src/ui/themes/darkTheme.ts +0 -87
- package/src/ui/themes/defaultTheme.ts +0 -85
- package/src/ui/themes/index.ts +0 -10
- package/src/ui/themes/lightTheme.ts +0 -89
- package/src/ui/themes/popularThemes.ts +0 -187
- package/src/ui/themes/types.ts +0 -130
- package/src/utils/clipboard.ts +0 -48
- package/src/utils/debug.ts +0 -43
- package/src/utils/environment.ts +0 -68
- package/src/utils/index.ts +0 -10
package/src/hooks/HookManager.ts
DELETED
|
@@ -1,596 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Hook 管理器
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* - 加载和管理 Hook 配置
|
|
6
|
-
* - 协调 Hook 的匹配和执行
|
|
7
|
-
* - 防止重复执行
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { nanoid } from 'nanoid';
|
|
11
|
-
import { Matcher, extractFilePath, extractCommand } from './Matcher.js';
|
|
12
|
-
import { HookExecutor } from './HookExecutor.js';
|
|
13
|
-
import {
|
|
14
|
-
HookEvent,
|
|
15
|
-
DEFAULT_HOOK_CONFIG,
|
|
16
|
-
type HookConfig,
|
|
17
|
-
type HookContext,
|
|
18
|
-
type HookExecutionContext,
|
|
19
|
-
type PreToolUseInput,
|
|
20
|
-
type PostToolUseInput,
|
|
21
|
-
type PostToolUseFailureInput,
|
|
22
|
-
type PermissionRequestInput,
|
|
23
|
-
type UserPromptSubmitInput,
|
|
24
|
-
type SessionStartInput,
|
|
25
|
-
type SessionEndInput,
|
|
26
|
-
type StopInput,
|
|
27
|
-
type CompactionInput,
|
|
28
|
-
type PreToolHookResult,
|
|
29
|
-
type PostToolHookResult,
|
|
30
|
-
type PermissionHookResult,
|
|
31
|
-
type StopHookResult,
|
|
32
|
-
type UserPromptHookResult,
|
|
33
|
-
type CompactionHookResult,
|
|
34
|
-
type MatchContext,
|
|
35
|
-
type Hook,
|
|
36
|
-
} from './types.js';
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
*
|
|
40
|
-
*/
|
|
41
|
-
class HookExecutionGuard {
|
|
42
|
-
private executed: Map<string, Set<HookEvent>> = new Map();
|
|
43
|
-
|
|
44
|
-
canExecute(toolUseId: string, event: HookEvent): boolean {
|
|
45
|
-
const events = this.executed.get(toolUseId);
|
|
46
|
-
if (!events) return true;
|
|
47
|
-
return !events.has(event);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
markExecuted(toolUseId: string, event: HookEvent): void {
|
|
51
|
-
let events = this.executed.get(toolUseId);
|
|
52
|
-
if (!events) {
|
|
53
|
-
events = new Set();
|
|
54
|
-
this.executed.set(toolUseId, events);
|
|
55
|
-
}
|
|
56
|
-
events.add(event);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
clear(): void {
|
|
60
|
-
this.executed.clear();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Hook 管理器
|
|
66
|
-
*/
|
|
67
|
-
export class HookManager {
|
|
68
|
-
private static instance: HookManager | null = null;
|
|
69
|
-
|
|
70
|
-
private config: HookConfig = DEFAULT_HOOK_CONFIG;
|
|
71
|
-
private executor = new HookExecutor();
|
|
72
|
-
private matcher = new Matcher();
|
|
73
|
-
private guard = new HookExecutionGuard();
|
|
74
|
-
private sessionDisabled = false;
|
|
75
|
-
private initialized = false;
|
|
76
|
-
|
|
77
|
-
private constructor() {}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
*
|
|
81
|
-
*/
|
|
82
|
-
static getInstance(): HookManager {
|
|
83
|
-
if (!HookManager.instance) {
|
|
84
|
-
HookManager.instance = new HookManager();
|
|
85
|
-
}
|
|
86
|
-
return HookManager.instance;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
*
|
|
91
|
-
*/
|
|
92
|
-
static resetInstance(): void {
|
|
93
|
-
HookManager.instance = null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
*
|
|
98
|
-
*/
|
|
99
|
-
loadConfig(config: Partial<HookConfig>): void {
|
|
100
|
-
this.config = this.mergeConfig(DEFAULT_HOOK_CONFIG, config);
|
|
101
|
-
this.initialized = true;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
*
|
|
106
|
-
*/
|
|
107
|
-
private mergeConfig(base: HookConfig, override: Partial<HookConfig>): HookConfig {
|
|
108
|
-
return {
|
|
109
|
-
...base,
|
|
110
|
-
...override,
|
|
111
|
-
// 合并各事件类型的 Hook 列
|
|
112
|
-
PreToolUse: override.PreToolUse ?? base.PreToolUse,
|
|
113
|
-
PostToolUse: override.PostToolUse ?? base.PostToolUse,
|
|
114
|
-
PostToolUseFailure: override.PostToolUseFailure ?? base.PostToolUseFailure,
|
|
115
|
-
PermissionRequest: override.PermissionRequest ?? base.PermissionRequest,
|
|
116
|
-
UserPromptSubmit: override.UserPromptSubmit ?? base.UserPromptSubmit,
|
|
117
|
-
SessionStart: override.SessionStart ?? base.SessionStart,
|
|
118
|
-
SessionEnd: override.SessionEnd ?? base.SessionEnd,
|
|
119
|
-
Stop: override.Stop ?? base.Stop,
|
|
120
|
-
SubagentStop: override.SubagentStop ?? base.SubagentStop,
|
|
121
|
-
Notification: override.Notification ?? base.Notification,
|
|
122
|
-
Compaction: override.Compaction ?? base.Compaction,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
*
|
|
128
|
-
*/
|
|
129
|
-
isEnabled(): boolean {
|
|
130
|
-
return (this.config.enabled ?? true) && !this.sessionDisabled;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
disableForSession(): void {
|
|
137
|
-
this.sessionDisabled = true;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
*
|
|
142
|
-
*/
|
|
143
|
-
enableForSession(): void {
|
|
144
|
-
this.sessionDisabled = false;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
*/
|
|
150
|
-
getConfig(): HookConfig {
|
|
151
|
-
return this.config;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
*
|
|
156
|
-
*/
|
|
157
|
-
isInitialized(): boolean {
|
|
158
|
-
return this.initialized;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
163
|
-
*/
|
|
164
|
-
private getMatchingHooks(event: HookEvent, context: MatchContext): Hook[] {
|
|
165
|
-
const matchers = this.config[event];
|
|
166
|
-
return this.matcher.getMatchingHooks(matchers, context);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
*
|
|
171
|
-
*/
|
|
172
|
-
private createExecutionContext(context: HookContext): HookExecutionContext {
|
|
173
|
-
return {
|
|
174
|
-
...context,
|
|
175
|
-
config: this.config,
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// ==================== PreToolUse ====================
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
*
|
|
183
|
-
*/
|
|
184
|
-
async executePreToolHooks(
|
|
185
|
-
toolName: string,
|
|
186
|
-
toolUseId: string,
|
|
187
|
-
toolInput: Record<string, unknown>,
|
|
188
|
-
context: HookContext
|
|
189
|
-
): Promise<PreToolHookResult> {
|
|
190
|
-
if (!this.isEnabled()) {
|
|
191
|
-
return { decision: 'allow' };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Plan 模式跳
|
|
195
|
-
if (context.permissionMode === 'plan') {
|
|
196
|
-
return { decision: 'allow' };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// 检查是否已执
|
|
200
|
-
if (!this.guard.canExecute(toolUseId, HookEvent.PreToolUse)) {
|
|
201
|
-
return { decision: 'allow' };
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// 获取匹配
|
|
205
|
-
const hooks = this.getMatchingHooks(HookEvent.PreToolUse, {
|
|
206
|
-
toolName,
|
|
207
|
-
filePath: extractFilePath(toolInput),
|
|
208
|
-
command: extractCommand(toolName, toolInput),
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
if (hooks.length === 0) {
|
|
212
|
-
return { decision: 'allow' };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// 构建输
|
|
216
|
-
const hookInput: PreToolUseInput = {
|
|
217
|
-
hook_event_name: HookEvent.PreToolUse,
|
|
218
|
-
hook_execution_id: nanoid(),
|
|
219
|
-
timestamp: new Date().toISOString(),
|
|
220
|
-
tool_name: toolName,
|
|
221
|
-
tool_use_id: toolUseId,
|
|
222
|
-
tool_input: toolInput,
|
|
223
|
-
project_dir: context.projectDir,
|
|
224
|
-
session_id: context.sessionId,
|
|
225
|
-
permission_mode: context.permissionMode,
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
// 执
|
|
229
|
-
const result = await this.executor.executePreToolHooks(
|
|
230
|
-
hooks,
|
|
231
|
-
hookInput,
|
|
232
|
-
this.createExecutionContext(context)
|
|
233
|
-
);
|
|
234
|
-
|
|
235
|
-
// 标记已执
|
|
236
|
-
this.guard.markExecuted(toolUseId, HookEvent.PreToolUse);
|
|
237
|
-
|
|
238
|
-
// YOLO 模式:将 ask 转为 allow,但保
|
|
239
|
-
if (context.permissionMode === 'yolo' && result.decision === 'ask') {
|
|
240
|
-
return { ...result, decision: 'allow' };
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
return result;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// ==================== PostToolUse ====================
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
*
|
|
250
|
-
*/
|
|
251
|
-
async executePostToolHooks(
|
|
252
|
-
toolName: string,
|
|
253
|
-
toolUseId: string,
|
|
254
|
-
toolInput: Record<string, unknown>,
|
|
255
|
-
toolOutput: unknown,
|
|
256
|
-
context: HookContext
|
|
257
|
-
): Promise<PostToolHookResult> {
|
|
258
|
-
if (!this.isEnabled()) {
|
|
259
|
-
return {};
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
// 检查是否已执
|
|
263
|
-
if (!this.guard.canExecute(toolUseId, HookEvent.PostToolUse)) {
|
|
264
|
-
return {};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
// 获取匹配
|
|
268
|
-
const hooks = this.getMatchingHooks(HookEvent.PostToolUse, {
|
|
269
|
-
toolName,
|
|
270
|
-
filePath: extractFilePath(toolInput),
|
|
271
|
-
command: extractCommand(toolName, toolInput),
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
if (hooks.length === 0) {
|
|
275
|
-
return {};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// 构建输
|
|
279
|
-
const hookInput: PostToolUseInput = {
|
|
280
|
-
hook_event_name: HookEvent.PostToolUse,
|
|
281
|
-
hook_execution_id: nanoid(),
|
|
282
|
-
timestamp: new Date().toISOString(),
|
|
283
|
-
tool_name: toolName,
|
|
284
|
-
tool_use_id: toolUseId,
|
|
285
|
-
tool_input: toolInput,
|
|
286
|
-
tool_output: toolOutput,
|
|
287
|
-
project_dir: context.projectDir,
|
|
288
|
-
session_id: context.sessionId,
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
// 执
|
|
292
|
-
const result = await this.executor.executePostToolHooks(
|
|
293
|
-
hooks,
|
|
294
|
-
hookInput,
|
|
295
|
-
this.createExecutionContext(context)
|
|
296
|
-
);
|
|
297
|
-
|
|
298
|
-
// 标记已执
|
|
299
|
-
this.guard.markExecuted(toolUseId, HookEvent.PostToolUse);
|
|
300
|
-
|
|
301
|
-
return result;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// ==================== PostToolUseFailure ====================
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
*
|
|
308
|
-
*/
|
|
309
|
-
async executePostToolFailureHooks(
|
|
310
|
-
toolName: string,
|
|
311
|
-
toolUseId: string,
|
|
312
|
-
toolInput: Record<string, unknown>,
|
|
313
|
-
error: string,
|
|
314
|
-
context: HookContext
|
|
315
|
-
): Promise<void> {
|
|
316
|
-
if (!this.isEnabled()) return;
|
|
317
|
-
|
|
318
|
-
const hooks = this.getMatchingHooks(HookEvent.PostToolUseFailure, {
|
|
319
|
-
toolName,
|
|
320
|
-
filePath: extractFilePath(toolInput),
|
|
321
|
-
command: extractCommand(toolName, toolInput),
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
if (hooks.length === 0) return;
|
|
325
|
-
|
|
326
|
-
const hookInput: PostToolUseFailureInput = {
|
|
327
|
-
hook_event_name: HookEvent.PostToolUseFailure,
|
|
328
|
-
hook_execution_id: nanoid(),
|
|
329
|
-
timestamp: new Date().toISOString(),
|
|
330
|
-
tool_name: toolName,
|
|
331
|
-
tool_use_id: toolUseId,
|
|
332
|
-
tool_input: toolInput,
|
|
333
|
-
error,
|
|
334
|
-
project_dir: context.projectDir,
|
|
335
|
-
session_id: context.sessionId,
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
await this.executor.executeGenericHooks(
|
|
339
|
-
hooks,
|
|
340
|
-
hookInput,
|
|
341
|
-
this.createExecutionContext(context)
|
|
342
|
-
);
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// ==================== PermissionRequest ====================
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
*
|
|
349
|
-
*/
|
|
350
|
-
async executePermissionHooks(
|
|
351
|
-
toolName: string,
|
|
352
|
-
toolInput: Record<string, unknown>,
|
|
353
|
-
context: HookContext
|
|
354
|
-
): Promise<PermissionHookResult> {
|
|
355
|
-
if (!this.isEnabled()) {
|
|
356
|
-
return { decision: 'ask' };
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
const hooks = this.getMatchingHooks(HookEvent.PermissionRequest, {
|
|
360
|
-
toolName,
|
|
361
|
-
filePath: extractFilePath(toolInput),
|
|
362
|
-
command: extractCommand(toolName, toolInput),
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
if (hooks.length === 0) {
|
|
366
|
-
return { decision: 'ask' };
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
const hookInput: PermissionRequestInput = {
|
|
370
|
-
hook_event_name: HookEvent.PermissionRequest,
|
|
371
|
-
hook_execution_id: nanoid(),
|
|
372
|
-
timestamp: new Date().toISOString(),
|
|
373
|
-
tool_name: toolName,
|
|
374
|
-
tool_input: toolInput,
|
|
375
|
-
project_dir: context.projectDir,
|
|
376
|
-
session_id: context.sessionId,
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
return this.executor.executePermissionHooks(
|
|
380
|
-
hooks,
|
|
381
|
-
hookInput,
|
|
382
|
-
this.createExecutionContext(context)
|
|
383
|
-
);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// ==================== UserPromptSubmit ====================
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
*
|
|
390
|
-
*/
|
|
391
|
-
async executeUserPromptHooks(
|
|
392
|
-
promptContent: string,
|
|
393
|
-
context: HookContext
|
|
394
|
-
): Promise<UserPromptHookResult> {
|
|
395
|
-
if (!this.isEnabled()) {
|
|
396
|
-
return {};
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
const hooks = this.getMatchingHooks(HookEvent.UserPromptSubmit, {});
|
|
400
|
-
|
|
401
|
-
if (hooks.length === 0) {
|
|
402
|
-
return {};
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
const hookInput: UserPromptSubmitInput = {
|
|
406
|
-
hook_event_name: HookEvent.UserPromptSubmit,
|
|
407
|
-
hook_execution_id: nanoid(),
|
|
408
|
-
timestamp: new Date().toISOString(),
|
|
409
|
-
prompt_content: promptContent,
|
|
410
|
-
project_dir: context.projectDir,
|
|
411
|
-
session_id: context.sessionId,
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
return this.executor.executeUserPromptHooks(
|
|
415
|
-
hooks,
|
|
416
|
-
hookInput,
|
|
417
|
-
this.createExecutionContext(context)
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// ==================== SessionStart ====================
|
|
422
|
-
|
|
423
|
-
/**
|
|
424
|
-
*
|
|
425
|
-
*/
|
|
426
|
-
async executeSessionStartHooks(context: HookContext): Promise<void> {
|
|
427
|
-
if (!this.isEnabled()) return;
|
|
428
|
-
|
|
429
|
-
const hooks = this.getMatchingHooks(HookEvent.SessionStart, {});
|
|
430
|
-
|
|
431
|
-
if (hooks.length === 0) return;
|
|
432
|
-
|
|
433
|
-
const hookInput: SessionStartInput = {
|
|
434
|
-
hook_event_name: HookEvent.SessionStart,
|
|
435
|
-
hook_execution_id: nanoid(),
|
|
436
|
-
timestamp: new Date().toISOString(),
|
|
437
|
-
project_dir: context.projectDir,
|
|
438
|
-
session_id: context.sessionId,
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
await this.executor.executeGenericHooks(
|
|
442
|
-
hooks,
|
|
443
|
-
hookInput,
|
|
444
|
-
this.createExecutionContext(context)
|
|
445
|
-
);
|
|
446
|
-
|
|
447
|
-
// 清理执行防
|
|
448
|
-
this.guard.clear();
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// ==================== SessionEnd ====================
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
*
|
|
455
|
-
*/
|
|
456
|
-
async executeSessionEndHooks(context: HookContext): Promise<void> {
|
|
457
|
-
if (!this.isEnabled()) return;
|
|
458
|
-
|
|
459
|
-
const hooks = this.getMatchingHooks(HookEvent.SessionEnd, {});
|
|
460
|
-
|
|
461
|
-
if (hooks.length === 0) return;
|
|
462
|
-
|
|
463
|
-
const hookInput: SessionEndInput = {
|
|
464
|
-
hook_event_name: HookEvent.SessionEnd,
|
|
465
|
-
hook_execution_id: nanoid(),
|
|
466
|
-
timestamp: new Date().toISOString(),
|
|
467
|
-
project_dir: context.projectDir,
|
|
468
|
-
session_id: context.sessionId,
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
await this.executor.executeGenericHooks(
|
|
472
|
-
hooks,
|
|
473
|
-
hookInput,
|
|
474
|
-
this.createExecutionContext(context)
|
|
475
|
-
);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
// ==================== Stop ====================
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
*
|
|
482
|
-
*/
|
|
483
|
-
async executeStopHooks(
|
|
484
|
-
stopReason: string | undefined,
|
|
485
|
-
context: HookContext
|
|
486
|
-
): Promise<StopHookResult> {
|
|
487
|
-
if (!this.isEnabled()) {
|
|
488
|
-
return { shouldContinue: false };
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
const hooks = this.getMatchingHooks(HookEvent.Stop, {});
|
|
492
|
-
|
|
493
|
-
if (hooks.length === 0) {
|
|
494
|
-
return { shouldContinue: false };
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
const hookInput: StopInput = {
|
|
498
|
-
hook_event_name: HookEvent.Stop,
|
|
499
|
-
hook_execution_id: nanoid(),
|
|
500
|
-
timestamp: new Date().toISOString(),
|
|
501
|
-
stop_reason: stopReason,
|
|
502
|
-
project_dir: context.projectDir,
|
|
503
|
-
session_id: context.sessionId,
|
|
504
|
-
};
|
|
505
|
-
|
|
506
|
-
return this.executor.executeStopHooks(
|
|
507
|
-
hooks,
|
|
508
|
-
hookInput,
|
|
509
|
-
this.createExecutionContext(context)
|
|
510
|
-
);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// ==================== Compaction ====================
|
|
514
|
-
|
|
515
|
-
/**
|
|
516
|
-
*
|
|
517
|
-
*/
|
|
518
|
-
async executeCompactionHooks(
|
|
519
|
-
preTokens: number,
|
|
520
|
-
messageCount: number,
|
|
521
|
-
context: HookContext
|
|
522
|
-
): Promise<CompactionHookResult> {
|
|
523
|
-
if (!this.isEnabled()) {
|
|
524
|
-
return { shouldPrevent: false };
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
const hooks = this.getMatchingHooks(HookEvent.Compaction, {});
|
|
528
|
-
|
|
529
|
-
if (hooks.length === 0) {
|
|
530
|
-
return { shouldPrevent: false };
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
const hookInput: CompactionInput = {
|
|
534
|
-
hook_event_name: HookEvent.Compaction,
|
|
535
|
-
hook_execution_id: nanoid(),
|
|
536
|
-
timestamp: new Date().toISOString(),
|
|
537
|
-
pre_tokens: preTokens,
|
|
538
|
-
message_count: messageCount,
|
|
539
|
-
project_dir: context.projectDir,
|
|
540
|
-
session_id: context.sessionId,
|
|
541
|
-
};
|
|
542
|
-
|
|
543
|
-
return this.executor.executeCompactionHooks(
|
|
544
|
-
hooks,
|
|
545
|
-
hookInput,
|
|
546
|
-
this.createExecutionContext(context)
|
|
547
|
-
);
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
// ==================== 统计信
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
*
|
|
554
|
-
*/
|
|
555
|
-
getHookCounts(): Record<string, number> {
|
|
556
|
-
const counts: Record<string, number> = {};
|
|
557
|
-
|
|
558
|
-
for (const event of Object.values(HookEvent)) {
|
|
559
|
-
const matchers = this.config[event];
|
|
560
|
-
if (matchers && Array.isArray(matchers)) {
|
|
561
|
-
let count = 0;
|
|
562
|
-
for (const matcher of matchers) {
|
|
563
|
-
count += matcher.hooks?.length || 0;
|
|
564
|
-
}
|
|
565
|
-
if (count > 0) {
|
|
566
|
-
counts[event] = count;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
return counts;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
/**
|
|
575
|
-
*
|
|
576
|
-
*/
|
|
577
|
-
getConfiguredEvents(): HookEvent[] {
|
|
578
|
-
const events: HookEvent[] = [];
|
|
579
|
-
|
|
580
|
-
for (const event of Object.values(HookEvent)) {
|
|
581
|
-
const matchers = this.config[event];
|
|
582
|
-
if (matchers && Array.isArray(matchers) && matchers.length > 0) {
|
|
583
|
-
events.push(event);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
return events;
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
/**
|
|
592
|
-
*
|
|
593
|
-
*/
|
|
594
|
-
export function getHookManager(): HookManager {
|
|
595
|
-
return HookManager.getInstance();
|
|
596
|
-
}
|