aegiscode 3.1.8 → 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.
Files changed (206) hide show
  1. package/README.md +23 -15
  2. package/dist/main.js +549 -552
  3. package/package.json +4 -2
  4. package/src/agent/Agent.ts +903 -0
  5. package/src/agent/SimpleAgent.ts +48 -0
  6. package/src/agent/index.ts +54 -0
  7. package/src/agent/orchestrator/AppBuilder.ts +443 -0
  8. package/src/agent/orchestrator/CouncilAgent.ts +310 -0
  9. package/src/agent/orchestrator/DiscussionRoom.ts +462 -0
  10. package/src/agent/orchestrator/OrchestratorAgent.ts +637 -0
  11. package/src/agent/orchestrator/index.ts +38 -0
  12. package/src/agent/orchestrator/utils.ts +397 -0
  13. package/src/agent/pricing.ts +115 -0
  14. package/src/agent/router.ts +74 -0
  15. package/src/agent/routerStats.ts +121 -0
  16. package/src/agent/types.ts +318 -0
  17. package/src/auth/login.ts +383 -0
  18. package/src/cli/config.ts +189 -0
  19. package/src/cli/index.ts +17 -0
  20. package/src/cli/middleware.ts +119 -0
  21. package/src/cli/types.ts +75 -0
  22. package/src/config/ConfigManager.ts +587 -0
  23. package/src/config/index.ts +7 -0
  24. package/src/config/types.ts +584 -0
  25. package/src/context/CompactionService.ts +300 -0
  26. package/src/context/ContextManager.ts +450 -0
  27. package/src/context/FileAnalyzer.ts +267 -0
  28. package/src/context/TokenCounter.ts +265 -0
  29. package/src/context/index.ts +27 -0
  30. package/src/context/storage/CacheStore.ts +176 -0
  31. package/src/context/storage/JSONLStore.ts +201 -0
  32. package/src/context/storage/MemoryStore.ts +205 -0
  33. package/src/context/storage/PersistentStore.ts +327 -0
  34. package/src/context/storage/index.ts +9 -0
  35. package/src/context/storage/pathUtils.ts +114 -0
  36. package/src/context/test.ts +309 -0
  37. package/src/context/types.ts +268 -0
  38. package/src/hooks/HookExecutor.ts +434 -0
  39. package/src/hooks/HookManager.ts +596 -0
  40. package/src/hooks/HookService.ts +269 -0
  41. package/src/hooks/Matcher.ts +157 -0
  42. package/src/hooks/index.ts +63 -0
  43. package/src/hooks/types.ts +424 -0
  44. package/src/main.tsx +596 -0
  45. package/src/mcp/HealthMonitor.ts +150 -0
  46. package/src/mcp/McpClient.ts +491 -0
  47. package/src/mcp/McpRegistry.ts +321 -0
  48. package/src/mcp/createMcpTool.ts +251 -0
  49. package/src/mcp/index.ts +15 -0
  50. package/src/mcp/server.ts +334 -0
  51. package/src/mcp/test-server.ts +88 -0
  52. package/src/mcp/test.ts +372 -0
  53. package/src/mcp/types.ts +247 -0
  54. package/src/memory/AgentMemoryBus.ts +432 -0
  55. package/src/memory/CloudSync.ts +99 -0
  56. package/src/memory/DriveSync.ts +106 -0
  57. package/src/memory/SharedMemory.ts +951 -0
  58. package/src/memory/index.ts +14 -0
  59. package/src/memory/machineFingerprint.ts +40 -0
  60. package/src/orchestrator/SubAgentMetadata.ts +136 -0
  61. package/src/prompts/builder.ts +213 -0
  62. package/src/prompts/default.ts +144 -0
  63. package/src/prompts/index.ts +16 -0
  64. package/src/prompts/plan.ts +64 -0
  65. package/src/prompts/test.ts +78 -0
  66. package/src/services/AnthropicChatService.ts +341 -0
  67. package/src/services/ChatService.ts +347 -0
  68. package/src/services/ClaudeCliChatService.ts +256 -0
  69. package/src/services/CloudSync.ts +168 -0
  70. package/src/services/CostLedger.ts +211 -0
  71. package/src/services/Heartbeat.ts +135 -0
  72. package/src/services/LearningCollector.ts +291 -0
  73. package/src/services/OllamaInstaller.ts +342 -0
  74. package/src/services/VersionChecker.ts +445 -0
  75. package/src/services/index.ts +57 -0
  76. package/src/services/streaming/OpenAIEventAdapter.ts +126 -0
  77. package/src/services/streaming/RenderingProfile.ts +90 -0
  78. package/src/services/streaming/StreamEventParser.ts +181 -0
  79. package/src/services/streaming/ThrottledRenderer.ts +139 -0
  80. package/src/services/streaming/TranscriptBuffer.ts +574 -0
  81. package/src/services/streaming/eventStatusMap.ts +52 -0
  82. package/src/services/streaming/index.ts +46 -0
  83. package/src/services/streaming/renderFormatting.ts +79 -0
  84. package/src/services/streaming/types.ts +234 -0
  85. package/src/skills/SkillLoader.ts +126 -0
  86. package/src/skills/SkillRegistry.ts +366 -0
  87. package/src/skills/index.ts +48 -0
  88. package/src/skills/types.ts +146 -0
  89. package/src/slash-commands/billing.ts +70 -0
  90. package/src/slash-commands/build.ts +413 -0
  91. package/src/slash-commands/builtinCommands.ts +2733 -0
  92. package/src/slash-commands/clone.ts +242 -0
  93. package/src/slash-commands/council.ts +125 -0
  94. package/src/slash-commands/custom/CustomCommandExecutor.ts +143 -0
  95. package/src/slash-commands/custom/CustomCommandLoader.ts +251 -0
  96. package/src/slash-commands/custom/CustomCommandRegistry.ts +144 -0
  97. package/src/slash-commands/custom/index.ts +7 -0
  98. package/src/slash-commands/debate.ts +254 -0
  99. package/src/slash-commands/gmail.ts +105 -0
  100. package/src/slash-commands/index.ts +388 -0
  101. package/src/slash-commands/mcpCommand.ts +205 -0
  102. package/src/slash-commands/types.ts +201 -0
  103. package/src/store/index.ts +76 -0
  104. package/src/store/selectors.ts +246 -0
  105. package/src/store/slices/appSlice.ts +205 -0
  106. package/src/store/slices/commandSlice.ts +115 -0
  107. package/src/store/slices/configSlice.ts +46 -0
  108. package/src/store/slices/focusSlice.ts +64 -0
  109. package/src/store/slices/index.ts +9 -0
  110. package/src/store/slices/sessionSlice.ts +424 -0
  111. package/src/store/streaming-buffer.ts +425 -0
  112. package/src/store/test.ts +296 -0
  113. package/src/store/types.ts +274 -0
  114. package/src/store/vanilla.ts +186 -0
  115. package/src/tools/builtin/bash.ts +236 -0
  116. package/src/tools/builtin/council.ts +105 -0
  117. package/src/tools/builtin/edit.ts +213 -0
  118. package/src/tools/builtin/glob.ts +136 -0
  119. package/src/tools/builtin/grep.ts +263 -0
  120. package/src/tools/builtin/index.ts +61 -0
  121. package/src/tools/builtin/memory.ts +66 -0
  122. package/src/tools/builtin/read.ts +168 -0
  123. package/src/tools/builtin/skill.ts +97 -0
  124. package/src/tools/builtin/snapshot.ts +40 -0
  125. package/src/tools/builtin/task.ts +106 -0
  126. package/src/tools/builtin/write.ts +134 -0
  127. package/src/tools/createTool.ts +221 -0
  128. package/src/tools/execution/ExecutionPipeline.ts +263 -0
  129. package/src/tools/execution/index.ts +40 -0
  130. package/src/tools/execution/stages/CacheStage.ts +131 -0
  131. package/src/tools/execution/stages/ConfirmationStage.ts +203 -0
  132. package/src/tools/execution/stages/DiscoveryStage.ts +46 -0
  133. package/src/tools/execution/stages/ExecutionStage.ts +48 -0
  134. package/src/tools/execution/stages/FormattingStage.ts +44 -0
  135. package/src/tools/execution/stages/HookStage.ts +72 -0
  136. package/src/tools/execution/stages/PermissionStage.ts +287 -0
  137. package/src/tools/execution/stages/PostHookStage.ts +71 -0
  138. package/src/tools/execution/stages/index.ts +12 -0
  139. package/src/tools/execution/test.ts +266 -0
  140. package/src/tools/execution/types.ts +273 -0
  141. package/src/tools/index.ts +81 -0
  142. package/src/tools/registry.ts +304 -0
  143. package/src/tools/schemas.ts +109 -0
  144. package/src/tools/test.ts +220 -0
  145. package/src/tools/types.ts +175 -0
  146. package/src/tools/validation/PermissionChecker.ts +242 -0
  147. package/src/tools/validation/SensitiveFileDetector.ts +210 -0
  148. package/src/tools/validation/index.ts +11 -0
  149. package/src/ui/App.tsx +166 -0
  150. package/src/ui/components/AegisInterface.tsx +484 -0
  151. package/src/ui/components/common/ChatSearch.tsx +150 -0
  152. package/src/ui/components/common/ErrorBoundary.tsx +82 -0
  153. package/src/ui/components/common/ExitMessage.tsx +120 -0
  154. package/src/ui/components/common/LoadingIndicator.tsx +49 -0
  155. package/src/ui/components/common/index.ts +6 -0
  156. package/src/ui/components/dialog/ConfirmationPrompt.tsx +208 -0
  157. package/src/ui/components/dialog/InteractiveSelector.tsx +149 -0
  158. package/src/ui/components/dialog/SetupWizard.tsx +297 -0
  159. package/src/ui/components/dialog/UpdatePrompt.tsx +155 -0
  160. package/src/ui/components/dialog/index.ts +8 -0
  161. package/src/ui/components/index.ts +28 -0
  162. package/src/ui/components/input/CommandSuggestions.tsx +139 -0
  163. package/src/ui/components/input/CustomTextInput.tsx +220 -0
  164. package/src/ui/components/input/InputArea.tsx +361 -0
  165. package/src/ui/components/input/PromptSuggestions.tsx +66 -0
  166. package/src/ui/components/input/index.ts +6 -0
  167. package/src/ui/components/layout/ChatStatusBar.tsx +90 -0
  168. package/src/ui/components/layout/ContextBar.tsx +79 -0
  169. package/src/ui/components/layout/MessageArea.tsx +96 -0
  170. package/src/ui/components/layout/MessageList.tsx +647 -0
  171. package/src/ui/components/layout/MessageSeparator.tsx +26 -0
  172. package/src/ui/components/layout/WelcomeMessage.tsx +93 -0
  173. package/src/ui/components/layout/index.ts +7 -0
  174. package/src/ui/components/markdown/CodeHighlighter.tsx +292 -0
  175. package/src/ui/components/markdown/MessageRenderer.tsx +1211 -0
  176. package/src/ui/components/markdown/index.ts +8 -0
  177. package/src/ui/components/markdown/parser.ts +336 -0
  178. package/src/ui/components/markdown/types.ts +66 -0
  179. package/src/ui/focus/FocusManager.ts +137 -0
  180. package/src/ui/focus/index.ts +13 -0
  181. package/src/ui/focus/types.ts +54 -0
  182. package/src/ui/focus/useFocus.ts +75 -0
  183. package/src/ui/hooks/index.ts +11 -0
  184. package/src/ui/hooks/useAgent.ts +284 -0
  185. package/src/ui/hooks/useCommandHistory.ts +87 -0
  186. package/src/ui/hooks/useCommandProcessor.ts +443 -0
  187. package/src/ui/hooks/useConfirmation.ts +99 -0
  188. package/src/ui/hooks/useCtrlCHandler.ts +100 -0
  189. package/src/ui/hooks/useInputBuffer.ts +122 -0
  190. package/src/ui/hooks/useTerminalSize.ts +68 -0
  191. package/src/ui/hooks/useTerminalWidth.ts +5 -0
  192. package/src/ui/hooks/useWindowedList.ts +118 -0
  193. package/src/ui/render-debugger.ts +621 -0
  194. package/src/ui/test.ts +189 -0
  195. package/src/ui/themes/ThemeManager.ts +332 -0
  196. package/src/ui/themes/aegisTheme.ts +87 -0
  197. package/src/ui/themes/darkTheme.ts +87 -0
  198. package/src/ui/themes/defaultTheme.ts +85 -0
  199. package/src/ui/themes/index.ts +10 -0
  200. package/src/ui/themes/lightTheme.ts +89 -0
  201. package/src/ui/themes/popularThemes.ts +187 -0
  202. package/src/ui/themes/types.ts +130 -0
  203. package/src/utils/clipboard.ts +48 -0
  204. package/src/utils/debug.ts +43 -0
  205. package/src/utils/environment.ts +68 -0
  206. package/src/utils/index.ts +10 -0
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Slash 命令类型定义
3
+ */
4
+
5
+ /**
6
+ *
7
+ */
8
+ export type CommandCategory =
9
+ | 'general' // 通用命
10
+ | 'session' // 会话相
11
+ | 'config' // 配置相
12
+ | 'skills' // Skills 相
13
+ | 'hooks' // Hooks 相
14
+ | 'git' // Git 相
15
+ | 'mcp' // MCP 相
16
+ | 'custom'; // 自定义命
17
+
18
+ /**
19
+ *
20
+ */
21
+ export interface SelectorOption<T = string> {
22
+ value: T;
23
+ label: string;
24
+ description?: string;
25
+ isCurrent?: boolean;
26
+ }
27
+
28
+ /**
29
+ * Slash 命令上下文
30
+ */
31
+ export interface SlashCommandContext {
32
+ /** 当前工作目录 */
33
+ cwd: string;
34
+ /** 会话 ID */
35
+ sessionId?: string;
36
+ /** 用户消息历史 */
37
+ messages?: any[];
38
+ /** ContextManager 实例(用于 /compact 等命令) */
39
+ contextManager?: any;
40
+ /** ChatService 实例(用于 LLM 调用) */
41
+ chatService?: any;
42
+ /** 模型名称 */
43
+ modelName?: string;
44
+ /** 显示选择器的回调 */
45
+ showSelector?: <T>(options: {
46
+ title: string;
47
+ options: SelectorOption<T>[];
48
+ onSelect: (value: T) => void;
49
+ onCancel: () => void;
50
+ }) => void;
51
+ /** 隐藏选择器的回调 */
52
+ hideSelector?: () => void;
53
+ /** 流式内容回调(增量文本) */
54
+ onContentDelta?: (delta: string) => void;
55
+ /** 流式思考回调 */
56
+ onThinkingDelta?: (delta: string) => void;
57
+ /** 工具调用开始回调 */
58
+ onToolCallStart?: (toolCall: { id: string; name: string; input: string }) => void;
59
+ /** 确认处理器(供管道交互式批准工具调用) */
60
+ confirmationHandler?: { requestConfirmation: (details: any) => Promise<any> };
61
+ }
62
+
63
+ /**
64
+ * Slash 命令结果
65
+ */
66
+ export interface SlashCommandResult {
67
+ /** 是否成功 */
68
+ success: boolean;
69
+ /** 结果类型(用于 UI 展示) */
70
+ type?: 'success' | 'error' | 'info' | 'silent' | 'selector';
71
+ /** 内容(Markdown 格式) */
72
+ content?: string;
73
+ /** 消息(简短提示) */
74
+ message?: string;
75
+ /** 错误信息 */
76
+ error?: string;
77
+ /** 是否继续处理(用于某些命令可能需要修改后续流程) */
78
+ shouldContinue?: boolean;
79
+ /** 额外数据 */
80
+ data?: any;
81
+ /** 选择器配置(type 为 'selector' 时使用) */
82
+ selector?: {
83
+ title: string;
84
+ options: SelectorOption[];
85
+ /** 选择后的处理器名称 */
86
+ handler: 'theme' | 'model';
87
+ };
88
+ /** 是否将内容发送给 Agent(自定义命令默认 true) */
89
+ sendToAgent?: boolean;
90
+ }
91
+
92
+ /**
93
+ * Slash 命令定义
94
+ */
95
+ export interface SlashCommand {
96
+ /** 命令名称(不含 /) */
97
+ name: string;
98
+ /** 命令别名 */
99
+ aliases?: string[];
100
+ /** 命令描述 */
101
+ description: string;
102
+ /** 详细描述 */
103
+ fullDescription?: string;
104
+ /** 使用示例 */
105
+ usage?: string;
106
+ /** 命令分类 */
107
+ category?: CommandCategory;
108
+ /** 示例列表 */
109
+ examples?: string[];
110
+ /** 命令处理函数 */
111
+ handler: (args: string, context: SlashCommandContext) => Promise<SlashCommandResult>;
112
+ }
113
+
114
+ /**
115
+ *
116
+ */
117
+ export interface CommandSuggestion {
118
+ /** 完整命令(含 /) */
119
+ command: string;
120
+ /** 命令描述 */
121
+ description: string;
122
+ /** 匹配分数 (0-100) */
123
+ matchScore?: number;
124
+ }
125
+
126
+ /**
127
+ *
128
+ */
129
+ export type SlashCommandRegistry = Record<string, SlashCommand>;
130
+
131
+ // ==================== 自定义命令类
132
+
133
+ /**
134
+ *
135
+ */
136
+ export interface CustomCommandConfig {
137
+ /** 命令描述(AI 调用必需) */
138
+ description?: string;
139
+ /** 参数提示,如 [message] */
140
+ argumentHint?: string;
141
+ /** 限制可用工具列表 */
142
+ allowedTools?: string[];
143
+ /** 指定执行模型 */
144
+ model?: string;
145
+ /** 禁止 AI 调用(默认 false) */
146
+ disableModelInvocation?: boolean;
147
+ }
148
+
149
+ /**
150
+ *
151
+ */
152
+ export type CustomCommandSource = 'user' | 'project';
153
+
154
+ /**
155
+ *
156
+ */
157
+ export type CustomCommandSourceDir = 'claude' | 'aegis';
158
+
159
+ /**
160
+ *
161
+ */
162
+ export interface CustomCommand {
163
+ /** 命令名(不含 /) */
164
+ name: string;
165
+ /** 命名空间(子目录名) */
166
+ namespace?: string;
167
+ /** Frontmatter 配置 */
168
+ config: CustomCommandConfig;
169
+ /** Markdown 正文 */
170
+ content: string;
171
+ /** 文件完整路径 */
172
+ path: string;
173
+ /** 来源类型 */
174
+ source: CustomCommandSource;
175
+ /** 目录类型 */
176
+ sourceDir: CustomCommandSourceDir;
177
+ }
178
+
179
+ /**
180
+ *
181
+ */
182
+ export interface CustomCommandExecutionContext {
183
+ /** 命令参数 */
184
+ args: string[];
185
+ /** 工作目录 */
186
+ workspaceRoot: string;
187
+ /** 中断信号 */
188
+ signal?: AbortSignal;
189
+ }
190
+
191
+ /**
192
+ *
193
+ */
194
+ export interface CustomCommandDiscoveryResult {
195
+ /** 发现的命令列表 */
196
+ commands: CustomCommand[];
197
+ /** 警告信息 */
198
+ warnings: string[];
199
+ /** 扫描的目录 */
200
+ scannedDirs: string[];
201
+ }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Zustand Store 模块导出
3
+ *
4
+ *
5
+ */
6
+
7
+ // 类
8
+ export * from './types.js';
9
+
10
+ // Vanilla Store(非 React 环
11
+ export {
12
+ vanillaStore,
13
+ getState,
14
+ subscribe,
15
+ sessionActions,
16
+ configActions,
17
+ appActions,
18
+ focusActions,
19
+ commandActions,
20
+ getConfig,
21
+ getCurrentModel,
22
+ getPermissionMode,
23
+ ensureStoreInitialized,
24
+ subscribeToState,
25
+ subscribeToTodos,
26
+ subscribeToMessages,
27
+ } from './vanilla.js';
28
+
29
+ // React 选择器
30
+ export {
31
+ useClawdStore,
32
+ // Session
33
+ useSessionId,
34
+ useMessages,
35
+ useIsThinking,
36
+ useIsCompacting,
37
+ useSessionError,
38
+ useCurrentCommand,
39
+ useTokenUsage,
40
+ // Config
41
+ useConfig,
42
+ useTheme,
43
+ usePermissionMode,
44
+ useAllModels,
45
+ useCurrentModel,
46
+ // App
47
+ useInitializationStatus,
48
+ useInitializationError,
49
+ useActiveModal,
50
+ useTodos,
51
+ useAwaitingSecondCtrlC,
52
+ useShowAllThinking,
53
+ useAutoRouterActiveModel,
54
+ useRouterEnabled,
55
+ useWorkflow,
56
+ // Focus
57
+ useCurrentFocus,
58
+ usePreviousFocus,
59
+ // Command
60
+ useIsProcessing,
61
+ usePendingCommands,
62
+ // 派生选择
63
+ useContextRemaining,
64
+ useIsInputDisabled,
65
+ useIsBusy,
66
+ useTodoStats,
67
+ // 细粒度消息选择
68
+ useMessageCount,
69
+ useMessageById,
70
+ useMessageIds,
71
+ useHasStreamingMessage,
72
+ useStreamingMessageId,
73
+ // 组合选择
74
+ useSessionState,
75
+ useAppState,
76
+ } from './selectors.js';
@@ -0,0 +1,246 @@
1
+ /**
2
+ * Store 选择器
3
+ *
4
+ *
5
+ */
6
+
7
+ import { useStore } from 'zustand';
8
+ import { useShallow } from 'zustand/react/shallow';
9
+ import { vanillaStore } from './vanilla.js';
10
+ import type { ClawdStore, SessionMessage, TodoItem, FocusId, WorkflowState } from './types.js';
11
+ import type { ModelConfig, PermissionMode } from '../config/types.js';
12
+
13
+ // ========== 基
14
+
15
+ /**
16
+ * React Hook - 订阅 Clawd Store
17
+ */
18
+ export function useClawdStore<T>(selector: (state: ClawdStore) => T): T {
19
+ return useStore(vanillaStore, selector);
20
+ }
21
+
22
+ // ========== Session 选择
23
+
24
+ export const useSessionId = () =>
25
+ useClawdStore((state) => state.session.sessionId);
26
+
27
+ export const useMessages = () =>
28
+ useClawdStore((state) => state.session.messages);
29
+
30
+ export const useIsThinking = () =>
31
+ useClawdStore((state) => state.session.isThinking);
32
+
33
+ export const useIsCompacting = () =>
34
+ useClawdStore((state) => state.session.isCompacting);
35
+
36
+ export const useSessionError = () =>
37
+ useClawdStore((state) => state.session.error);
38
+
39
+ export const useCurrentCommand = () =>
40
+ useClawdStore((state) => state.session.currentCommand);
41
+
42
+ export const useTokenUsage = () =>
43
+ useClawdStore((state) => state.session.tokenUsage);
44
+
45
+ // ========== Config 选择
46
+
47
+ export const useConfig = () =>
48
+ useClawdStore((state) => state.config.config);
49
+
50
+ export const useTheme = () =>
51
+ useClawdStore((state) => state.config.config?.theme || 'dark');
52
+
53
+ export const usePermissionMode = () =>
54
+ useClawdStore(
55
+ (state) => (state.config.config?.defaultPermissionMode || 'default') as PermissionMode
56
+ );
57
+
58
+ // 常量空引用,避免重渲
59
+ const EMPTY_MODELS: ModelConfig[] = [];
60
+
61
+ export const useAllModels = () =>
62
+ useClawdStore(
63
+ (state) => state.config.config?.models ?? EMPTY_MODELS
64
+ );
65
+
66
+ /**
67
+ *
68
+ */
69
+ export const useCurrentModel = () =>
70
+ useClawdStore((state) => {
71
+ const config = state.config.config;
72
+ if (!config) return undefined;
73
+
74
+ // 优先使
75
+ if (config.currentModelId && config.models) {
76
+ const model = config.models.find((m) => m.id === config.currentModelId);
77
+ if (model) return model;
78
+ }
79
+
80
+ // 回退
81
+ if (config.models && config.models.length > 0) {
82
+ return config.models[0];
83
+ }
84
+
85
+ // 回退
86
+ return config.default;
87
+ });
88
+
89
+ // ========== App 选择
90
+
91
+ export const useInitializationStatus = () =>
92
+ useClawdStore((state) => state.app.initializationStatus);
93
+
94
+ export const useInitializationError = () =>
95
+ useClawdStore((state) => state.app.initializationError);
96
+
97
+ export const useActiveModal = () =>
98
+ useClawdStore((state) => state.app.activeModal);
99
+
100
+ export const useTodos = () =>
101
+ useClawdStore((state) => state.app.todos);
102
+
103
+ export const useAwaitingSecondCtrlC = () =>
104
+ useClawdStore((state) => state.app.awaitingSecondCtrlC);
105
+
106
+ export const useShowAllThinking = () =>
107
+ useClawdStore((state) => state.app.showAllThinking);
108
+
109
+ export const useAutoRouterActiveModel = () =>
110
+ useClawdStore((state) => state.app.autoRouterActiveModel);
111
+
112
+ export const useRouterEnabled = () =>
113
+ useClawdStore((state) => state.config.config?.autoRouter?.enabled ?? false);
114
+
115
+ export const useWorkflow = () =>
116
+ useClawdStore((state) => state.app.workflow);
117
+
118
+ // ========== Focus 选择
119
+
120
+ export const useCurrentFocus = () =>
121
+ useClawdStore((state) => state.focus.currentFocus);
122
+
123
+ export const usePreviousFocus = () =>
124
+ useClawdStore((state) => state.focus.previousFocus);
125
+
126
+ // ========== Command 选择
127
+
128
+ export const useIsProcessing = () =>
129
+ useClawdStore((state) => state.command.isProcessing);
130
+
131
+ export const usePendingCommands = () =>
132
+ useClawdStore((state) => state.command.pendingCommands);
133
+
134
+ // ========== 派生选择
135
+
136
+ /**
137
+ *
138
+ */
139
+ export const useContextRemaining = () =>
140
+ useClawdStore((state) => {
141
+ const { inputTokens, maxContextTokens } = state.session.tokenUsage;
142
+ if (maxContextTokens <= 0) return 100;
143
+ return Math.round(Math.max(0, 100 - (inputTokens / maxContextTokens) * 100));
144
+ });
145
+
146
+ /**
147
+ *
148
+ */
149
+ export const useIsInputDisabled = () =>
150
+ useClawdStore((state) => {
151
+ const isThinking = state.session.isThinking;
152
+ const isReady = state.app.initializationStatus === 'ready';
153
+ const hasModal =
154
+ state.app.activeModal !== 'none' &&
155
+ state.app.activeModal !== 'shortcuts';
156
+ return isThinking || !isReady || hasModal;
157
+ });
158
+
159
+ /**
160
+ *
161
+ */
162
+ export const useIsBusy = () =>
163
+ useClawdStore(
164
+ (state) => state.session.isThinking || state.command.isProcessing
165
+ );
166
+
167
+ /**
168
+ *
169
+ */
170
+ export const useTodoStats = () =>
171
+ useClawdStore(
172
+ useShallow((state) => {
173
+ const todos = state.app.todos;
174
+ return {
175
+ total: todos.length,
176
+ completed: todos.filter((t) => t.status === 'completed').length,
177
+ inProgress: todos.filter((t) => t.status === 'in_progress').length,
178
+ pending: todos.filter((t) => t.status === 'pending').length,
179
+ };
180
+ })
181
+ );
182
+
183
+ // ========== 细粒度消息选择
184
+
185
+ /**
186
+ *
187
+ */
188
+ export const useMessageCount = () =>
189
+ useClawdStore((state) => state.session.messages.length);
190
+
191
+ /**
192
+ *
193
+ */
194
+ export const useMessageById = (id: string) =>
195
+ useClawdStore((state) => state.session.messages.find(m => m.id === id));
196
+
197
+ /**
198
+ *
199
+ */
200
+ export const useMessageIds = () =>
201
+ useClawdStore(
202
+ useShallow((state) => state.session.messages.map(m => m.id))
203
+ );
204
+
205
+ /**
206
+ *
207
+ */
208
+ export const useHasStreamingMessage = () =>
209
+ useClawdStore((state) => state.session.messages.some(m => m.isStreaming));
210
+
211
+ /**
212
+ *
213
+ */
214
+ export const useStreamingMessageId = () =>
215
+ useClawdStore((state) => {
216
+ const streaming = state.session.messages.find(m => m.isStreaming);
217
+ return streaming?.id ?? null;
218
+ });
219
+
220
+ // ========== 组合选择器(使
221
+
222
+ /**
223
+ *
224
+ */
225
+ export const useSessionState = () =>
226
+ useClawdStore(
227
+ useShallow((state) => ({
228
+ sessionId: state.session.sessionId,
229
+ messages: state.session.messages,
230
+ isThinking: state.session.isThinking,
231
+ currentCommand: state.session.currentCommand,
232
+ error: state.session.error,
233
+ }))
234
+ );
235
+
236
+ /**
237
+ *
238
+ */
239
+ export const useAppState = () =>
240
+ useClawdStore(
241
+ useShallow((state) => ({
242
+ initializationStatus: state.app.initializationStatus,
243
+ initializationError: state.app.initializationError,
244
+ activeModal: state.app.activeModal,
245
+ }))
246
+ );
@@ -0,0 +1,205 @@
1
+ /**
2
+ * App Slice - 应用状态管理
3
+ */
4
+
5
+ import type { StateCreator } from 'zustand';
6
+ import type { ClawdStore, AppSlice, InitializationStatus, ActiveModal, TodoItem, WorkflowState } from '../types.js';
7
+ import { initialWorkflowState } from '../types.js';
8
+
9
+ const initialAppState = {
10
+ initializationStatus: 'pending' as InitializationStatus,
11
+ initializationError: null as string | null,
12
+ activeModal: 'none' as ActiveModal,
13
+ todos: [] as TodoItem[],
14
+ awaitingSecondCtrlC: false,
15
+ showAllThinking: false,
16
+ manualModelOverride: false,
17
+ autoRouterActiveModel: null as string | null,
18
+ workflow: { ...initialWorkflowState } as WorkflowState,
19
+ };
20
+
21
+ export const createAppSlice: StateCreator<
22
+ ClawdStore,
23
+ [],
24
+ [],
25
+ AppSlice
26
+ > = (set, get) => ({
27
+ ...initialAppState,
28
+
29
+ actions: {
30
+ /**
31
+ *
32
+ */
33
+ setInitializationStatus: (status: InitializationStatus) => {
34
+ set((state) => ({
35
+ app: { ...state.app, initializationStatus: status },
36
+ }));
37
+ },
38
+
39
+ /**
40
+ *
41
+ */
42
+ setInitializationError: (error: string | null) => {
43
+ set((state) => ({
44
+ app: {
45
+ ...state.app,
46
+ initializationError: error,
47
+ initializationStatus: error ? 'error' : state.app.initializationStatus,
48
+ },
49
+ }));
50
+ },
51
+
52
+ /**
53
+ *
54
+ */
55
+ setActiveModal: (modal: ActiveModal) => {
56
+ set((state) => ({
57
+ app: { ...state.app, activeModal: modal },
58
+ }));
59
+ },
60
+
61
+ /**
62
+ *
63
+ */
64
+ setTodos: (todos: TodoItem[]) => {
65
+ set((state) => ({
66
+ app: { ...state.app, todos },
67
+ }));
68
+ },
69
+
70
+ /**
71
+ *
72
+ */
73
+ addTodo: (todo: TodoItem) => {
74
+ set((state) => ({
75
+ app: {
76
+ ...state.app,
77
+ todos: [...state.app.todos, todo],
78
+ },
79
+ }));
80
+ },
81
+
82
+ /**
83
+ *
84
+ */
85
+ updateTodo: (id: string, updates: Partial<TodoItem>) => {
86
+ set((state) => ({
87
+ app: {
88
+ ...state.app,
89
+ todos: state.app.todos.map((todo) =>
90
+ todo.id === id ? { ...todo, ...updates } : todo
91
+ ),
92
+ },
93
+ }));
94
+ },
95
+
96
+ /**
97
+ *
98
+ */
99
+ removeTodo: (id: string) => {
100
+ set((state) => ({
101
+ app: {
102
+ ...state.app,
103
+ todos: state.app.todos.filter((todo) => todo.id !== id),
104
+ },
105
+ }));
106
+ },
107
+
108
+ /**
109
+ *
110
+ */
111
+ setAwaitingSecondCtrlC: (awaiting: boolean) => {
112
+ set((state) => ({
113
+ app: { ...state.app, awaitingSecondCtrlC: awaiting },
114
+ }));
115
+ },
116
+
117
+ /**
118
+ *
119
+ */
120
+ toggleShowAllThinking: () => {
121
+ set((state) => ({
122
+ app: { ...state.app, showAllThinking: !state.app.showAllThinking },
123
+ }));
124
+ },
125
+
126
+ /**
127
+ *
128
+ */
129
+ setManualModelOverride: (value: boolean) => {
130
+ set((state) => ({
131
+ app: { ...state.app, manualModelOverride: value },
132
+ }));
133
+ },
134
+
135
+ /**
136
+ *
137
+ */
138
+ setAutoRouterActiveModel: (label: string | null) => {
139
+ set((state) => ({
140
+ app: { ...state.app, autoRouterActiveModel: label },
141
+ }));
142
+ },
143
+
144
+ // ── Workflow ──────────────────────────────────────────
145
+ workflow: {
146
+ setWorkflow: (opts: { phase: string; target: string; steps: string[] }) => {
147
+ set((state) => ({
148
+ app: {
149
+ ...state.app,
150
+ workflow: {
151
+ visible: true,
152
+ phase: opts.phase,
153
+ target: opts.target,
154
+ steps: opts.steps.map((label, i) => ({
155
+ label,
156
+ status: i === 0 ? 'active' as const : 'pending' as const,
157
+ })),
158
+ currentStepIndex: 0,
159
+ totalSteps: opts.steps.length,
160
+ },
161
+ },
162
+ }));
163
+ },
164
+
165
+ advanceStep: () => {
166
+ set((state) => {
167
+ const wf = state.app.workflow;
168
+ if (!wf.visible) return state;
169
+ const nextIndex = wf.currentStepIndex + 1;
170
+ if (nextIndex >= wf.totalSteps) {
171
+ return {
172
+ app: {
173
+ ...state.app,
174
+ workflow: { ...initialWorkflowState },
175
+ },
176
+ };
177
+ }
178
+ const updatedSteps = wf.steps.map((s, i) => ({
179
+ ...s,
180
+ status: i < nextIndex ? 'done' as const : i === nextIndex ? 'active' as const : 'pending' as const,
181
+ }));
182
+ return {
183
+ app: {
184
+ ...state.app,
185
+ workflow: {
186
+ ...wf,
187
+ steps: updatedSteps,
188
+ currentStepIndex: nextIndex,
189
+ },
190
+ },
191
+ };
192
+ });
193
+ },
194
+
195
+ clearWorkflow: () => {
196
+ set((state) => ({
197
+ app: {
198
+ ...state.app,
199
+ workflow: { ...initialWorkflowState },
200
+ },
201
+ }));
202
+ },
203
+ },
204
+ },
205
+ });