c0de-agent 0.0.0 → 1.0.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.
Files changed (330) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +101 -0
  3. package/dist/cli/commands/acp.d.ts +19 -0
  4. package/dist/cli/commands/acp.js +50 -0
  5. package/dist/cli/commands/chat.d.ts +13 -0
  6. package/dist/cli/commands/chat.js +26 -0
  7. package/dist/cli/commands/config.d.ts +17 -0
  8. package/dist/cli/commands/config.js +77 -0
  9. package/dist/cli/commands/init.d.ts +9 -0
  10. package/dist/cli/commands/init.js +14 -0
  11. package/dist/cli/commands/plugin.d.ts +11 -0
  12. package/dist/cli/commands/plugin.js +31 -0
  13. package/dist/cli/commands/serve.d.ts +18 -0
  14. package/dist/cli/commands/serve.js +35 -0
  15. package/dist/cli/commands/update.d.ts +24 -0
  16. package/dist/cli/commands/update.js +40 -0
  17. package/dist/cli/deps.d.ts +19 -0
  18. package/dist/cli/deps.js +57 -0
  19. package/dist/cli/index.d.ts +8 -0
  20. package/dist/cli/index.js +149 -0
  21. package/dist/cli/modes/acp.d.ts +18 -0
  22. package/dist/cli/modes/acp.js +57 -0
  23. package/dist/cli/modes/print.d.ts +16 -0
  24. package/dist/cli/modes/print.js +30 -0
  25. package/dist/cli/parser.d.ts +19 -0
  26. package/dist/cli/parser.js +28 -0
  27. package/dist/cli/utils/output.d.ts +11 -0
  28. package/dist/cli/utils/output.js +23 -0
  29. package/dist/cli/utils/prompt.d.ts +12 -0
  30. package/dist/cli/utils/prompt.js +18 -0
  31. package/dist/core/agent.d.ts +12 -0
  32. package/dist/core/agent.js +95 -0
  33. package/dist/core/agents/builtin.d.ts +6 -0
  34. package/dist/core/agents/builtin.js +76 -0
  35. package/dist/core/agents/discovery.d.ts +6 -0
  36. package/dist/core/agents/discovery.js +96 -0
  37. package/dist/core/agents/index.d.ts +6 -0
  38. package/dist/core/agents/index.js +4 -0
  39. package/dist/core/agents/parallel.d.ts +13 -0
  40. package/dist/core/agents/parallel.js +37 -0
  41. package/dist/core/agents/registry.d.ts +4 -0
  42. package/dist/core/agents/registry.js +23 -0
  43. package/dist/core/agents/types.d.ts +37 -0
  44. package/dist/core/agents/types.js +1 -0
  45. package/dist/core/compact.d.ts +11 -0
  46. package/dist/core/compact.js +31 -0
  47. package/dist/core/config.d.ts +7 -0
  48. package/dist/core/config.js +84 -0
  49. package/dist/core/context.d.ts +23 -0
  50. package/dist/core/context.js +93 -0
  51. package/dist/core/index.d.ts +13 -0
  52. package/dist/core/index.js +11 -0
  53. package/dist/core/loop.d.ts +25 -0
  54. package/dist/core/loop.js +651 -0
  55. package/dist/core/metrics.d.ts +27 -0
  56. package/dist/core/metrics.js +94 -0
  57. package/dist/core/prompt-registry.d.ts +21 -0
  58. package/dist/core/prompt-registry.js +200 -0
  59. package/dist/core/prompt.d.ts +11 -0
  60. package/dist/core/prompt.js +13 -0
  61. package/dist/core/secret.d.ts +12 -0
  62. package/dist/core/secret.js +51 -0
  63. package/dist/core/slash.d.ts +15 -0
  64. package/dist/core/slash.js +114 -0
  65. package/dist/core/steering.d.ts +8 -0
  66. package/dist/core/steering.js +15 -0
  67. package/dist/core/title.d.ts +42 -0
  68. package/dist/core/title.js +115 -0
  69. package/dist/core/tool-exec.d.ts +20 -0
  70. package/dist/core/tool-exec.js +64 -0
  71. package/dist/core/types.d.ts +95 -0
  72. package/dist/core/types.js +1 -0
  73. package/dist/core/worktree.d.ts +24 -0
  74. package/dist/core/worktree.js +68 -0
  75. package/dist/dap/index.d.ts +6 -0
  76. package/dist/dap/index.js +4 -0
  77. package/dist/dap/protocol.d.ts +40 -0
  78. package/dist/dap/protocol.js +132 -0
  79. package/dist/dap/session.d.ts +23 -0
  80. package/dist/dap/session.js +130 -0
  81. package/dist/dap/transport.d.ts +8 -0
  82. package/dist/dap/transport.js +30 -0
  83. package/dist/dap/types.d.ts +50 -0
  84. package/dist/dap/types.js +2 -0
  85. package/dist/db/client.d.ts +22 -0
  86. package/dist/db/client.js +39 -0
  87. package/dist/db/index.d.ts +6 -0
  88. package/dist/db/index.js +4 -0
  89. package/dist/db/migrate.d.ts +9 -0
  90. package/dist/db/migrate.js +15 -0
  91. package/dist/db/schema.d.ts +965 -0
  92. package/dist/db/schema.js +112 -0
  93. package/dist/db/types.d.ts +11 -0
  94. package/dist/db/types.js +1 -0
  95. package/dist/llm/index.d.ts +13 -0
  96. package/dist/llm/index.js +8 -0
  97. package/dist/llm/protocols/openai-compat.d.ts +139 -0
  98. package/dist/llm/protocols/openai-compat.js +214 -0
  99. package/dist/llm/protocols/utils/index.d.ts +2 -0
  100. package/dist/llm/protocols/utils/index.js +2 -0
  101. package/dist/llm/protocols/utils/lifecycle.d.ts +23 -0
  102. package/dist/llm/protocols/utils/lifecycle.js +46 -0
  103. package/dist/llm/protocols/utils/tool-stream.d.ts +46 -0
  104. package/dist/llm/protocols/utils/tool-stream.js +94 -0
  105. package/dist/llm/provider-error.d.ts +5 -0
  106. package/dist/llm/provider-error.js +33 -0
  107. package/dist/llm/provider.d.ts +32 -0
  108. package/dist/llm/provider.js +191 -0
  109. package/dist/llm/registry.d.ts +44 -0
  110. package/dist/llm/registry.js +107 -0
  111. package/dist/llm/retry.d.ts +37 -0
  112. package/dist/llm/retry.js +82 -0
  113. package/dist/llm/routing.d.ts +36 -0
  114. package/dist/llm/routing.js +53 -0
  115. package/dist/llm/schema/errors.d.ts +88 -0
  116. package/dist/llm/schema/errors.js +33 -0
  117. package/dist/llm/schema/events.d.ts +135 -0
  118. package/dist/llm/schema/events.js +35 -0
  119. package/dist/llm/schema/ids.d.ts +22 -0
  120. package/dist/llm/schema/ids.js +3 -0
  121. package/dist/llm/schema/index.d.ts +5 -0
  122. package/dist/llm/schema/index.js +5 -0
  123. package/dist/llm/schema/messages.d.ts +97 -0
  124. package/dist/llm/schema/messages.js +18 -0
  125. package/dist/llm/schema/options.d.ts +54 -0
  126. package/dist/llm/schema/options.js +54 -0
  127. package/dist/llm/token.d.ts +2 -0
  128. package/dist/llm/token.js +13 -0
  129. package/dist/llm/transport.d.ts +31 -0
  130. package/dist/llm/transport.js +165 -0
  131. package/dist/mcp/index.d.ts +1 -0
  132. package/dist/mcp/index.js +3 -0
  133. package/dist/plugins/builtin.d.ts +6 -0
  134. package/dist/plugins/builtin.js +54 -0
  135. package/dist/plugins/hooks.d.ts +3 -0
  136. package/dist/plugins/hooks.js +79 -0
  137. package/dist/plugins/index.d.ts +9 -0
  138. package/dist/plugins/index.js +8 -0
  139. package/dist/plugins/init.d.ts +23 -0
  140. package/dist/plugins/init.js +30 -0
  141. package/dist/plugins/lifecycle.d.ts +6 -0
  142. package/dist/plugins/lifecycle.js +74 -0
  143. package/dist/plugins/loader.d.ts +16 -0
  144. package/dist/plugins/loader.js +68 -0
  145. package/dist/plugins/logger.d.ts +4 -0
  146. package/dist/plugins/logger.js +31 -0
  147. package/dist/plugins/registry.d.ts +7 -0
  148. package/dist/plugins/registry.js +24 -0
  149. package/dist/plugins/types.d.ts +113 -0
  150. package/dist/plugins/types.js +1 -0
  151. package/dist/project/detect.d.ts +4 -0
  152. package/dist/project/detect.js +131 -0
  153. package/dist/project/index.d.ts +4 -0
  154. package/dist/project/index.js +2 -0
  155. package/dist/project/project.d.ts +23 -0
  156. package/dist/project/project.js +87 -0
  157. package/dist/project/resolve.d.ts +8 -0
  158. package/dist/project/resolve.js +81 -0
  159. package/dist/server/agent-manager.d.ts +32 -0
  160. package/dist/server/agent-manager.js +54 -0
  161. package/dist/server/app.d.ts +5 -0
  162. package/dist/server/app.js +75 -0
  163. package/dist/server/context.d.ts +20 -0
  164. package/dist/server/context.js +47 -0
  165. package/dist/server/dev.d.ts +11 -0
  166. package/dist/server/dev.js +83 -0
  167. package/dist/server/index.d.ts +11 -0
  168. package/dist/server/index.js +7 -0
  169. package/dist/server/middleware/auth.d.ts +15 -0
  170. package/dist/server/middleware/auth.js +29 -0
  171. package/dist/server/middleware/cors.d.ts +16 -0
  172. package/dist/server/middleware/cors.js +44 -0
  173. package/dist/server/middleware/error.d.ts +15 -0
  174. package/dist/server/middleware/error.js +11 -0
  175. package/dist/server/permission/interactive.d.ts +24 -0
  176. package/dist/server/permission/interactive.js +56 -0
  177. package/dist/server/permission/store.d.ts +30 -0
  178. package/dist/server/permission/store.js +47 -0
  179. package/dist/server/routes/agent.d.ts +4 -0
  180. package/dist/server/routes/agent.js +19 -0
  181. package/dist/server/routes/catalog.d.ts +42 -0
  182. package/dist/server/routes/catalog.js +194 -0
  183. package/dist/server/routes/chat.d.ts +8 -0
  184. package/dist/server/routes/chat.js +282 -0
  185. package/dist/server/routes/commands.d.ts +5 -0
  186. package/dist/server/routes/commands.js +17 -0
  187. package/dist/server/routes/config.d.ts +4 -0
  188. package/dist/server/routes/config.js +27 -0
  189. package/dist/server/routes/files.d.ts +4 -0
  190. package/dist/server/routes/files.js +183 -0
  191. package/dist/server/routes/filesystem.d.ts +29 -0
  192. package/dist/server/routes/filesystem.js +108 -0
  193. package/dist/server/routes/health.d.ts +3 -0
  194. package/dist/server/routes/health.js +13 -0
  195. package/dist/server/routes/permissions.d.ts +5 -0
  196. package/dist/server/routes/permissions.js +23 -0
  197. package/dist/server/routes/project.d.ts +4 -0
  198. package/dist/server/routes/project.js +51 -0
  199. package/dist/server/routes/provider.d.ts +16 -0
  200. package/dist/server/routes/provider.js +66 -0
  201. package/dist/server/routes/session.d.ts +4 -0
  202. package/dist/server/routes/session.js +130 -0
  203. package/dist/server/routes/tool.d.ts +4 -0
  204. package/dist/server/routes/tool.js +28 -0
  205. package/dist/server/routes/update.d.ts +14 -0
  206. package/dist/server/routes/update.js +49 -0
  207. package/dist/server/server.d.ts +43 -0
  208. package/dist/server/server.js +197 -0
  209. package/dist/server/types.d.ts +73 -0
  210. package/dist/server/types.js +2 -0
  211. package/dist/server/util/safe-path.d.ts +7 -0
  212. package/dist/server/util/safe-path.js +7 -0
  213. package/dist/session/archive.d.ts +15 -0
  214. package/dist/session/archive.js +129 -0
  215. package/dist/session/branch.d.ts +9 -0
  216. package/dist/session/branch.js +63 -0
  217. package/dist/session/compaction.d.ts +27 -0
  218. package/dist/session/compaction.js +192 -0
  219. package/dist/session/context.d.ts +16 -0
  220. package/dist/session/context.js +173 -0
  221. package/dist/session/index.d.ts +10 -0
  222. package/dist/session/index.js +10 -0
  223. package/dist/session/message.d.ts +22 -0
  224. package/dist/session/message.js +145 -0
  225. package/dist/session/session.d.ts +37 -0
  226. package/dist/session/session.js +144 -0
  227. package/dist/session/snapshot.d.ts +11 -0
  228. package/dist/session/snapshot.js +60 -0
  229. package/dist/session/squash.d.ts +9 -0
  230. package/dist/session/squash.js +88 -0
  231. package/dist/session/token.d.ts +10 -0
  232. package/dist/session/token.js +33 -0
  233. package/dist/session/types.d.ts +122 -0
  234. package/dist/session/types.js +1 -0
  235. package/dist/shared/index.d.ts +2 -0
  236. package/dist/shared/index.js +1 -0
  237. package/dist/shared/types/agent.d.ts +202 -0
  238. package/dist/shared/types/agent.js +1 -0
  239. package/dist/shared/types/base.d.ts +32 -0
  240. package/dist/shared/types/base.js +1 -0
  241. package/dist/shared/types/config.d.ts +104 -0
  242. package/dist/shared/types/config.js +1 -0
  243. package/dist/shared/types/index.d.ts +6 -0
  244. package/dist/shared/types/index.js +1 -0
  245. package/dist/shared/types/llm.d.ts +122 -0
  246. package/dist/shared/types/llm.js +1 -0
  247. package/dist/shared/types/message.d.ts +71 -0
  248. package/dist/shared/types/message.js +1 -0
  249. package/dist/shared/types/tool.d.ts +134 -0
  250. package/dist/shared/types/tool.js +1 -0
  251. package/dist/shared/utils/path.d.ts +15 -0
  252. package/dist/shared/utils/path.js +23 -0
  253. package/dist/shared/utils.d.ts +5 -0
  254. package/dist/shared/utils.js +10 -0
  255. package/dist/tools/builtin/bash.d.ts +12 -0
  256. package/dist/tools/builtin/bash.js +121 -0
  257. package/dist/tools/builtin/dap.d.ts +23 -0
  258. package/dist/tools/builtin/dap.js +231 -0
  259. package/dist/tools/builtin/edit.d.ts +12 -0
  260. package/dist/tools/builtin/edit.js +151 -0
  261. package/dist/tools/builtin/glob.d.ts +11 -0
  262. package/dist/tools/builtin/glob.js +120 -0
  263. package/dist/tools/builtin/grep.d.ts +6 -0
  264. package/dist/tools/builtin/grep.js +141 -0
  265. package/dist/tools/builtin/read.d.ts +6 -0
  266. package/dist/tools/builtin/read.js +73 -0
  267. package/dist/tools/builtin/resolvers.d.ts +12 -0
  268. package/dist/tools/builtin/resolvers.js +79 -0
  269. package/dist/tools/builtin/task.d.ts +9 -0
  270. package/dist/tools/builtin/task.js +100 -0
  271. package/dist/tools/builtin/write.d.ts +6 -0
  272. package/dist/tools/builtin/write.js +36 -0
  273. package/dist/tools/builtin/yield.d.ts +9 -0
  274. package/dist/tools/builtin/yield.js +40 -0
  275. package/dist/tools/executor.d.ts +9 -0
  276. package/dist/tools/executor.js +54 -0
  277. package/dist/tools/hashline/index.d.ts +2 -0
  278. package/dist/tools/hashline/index.js +3 -0
  279. package/dist/tools/hashline/patch.d.ts +48 -0
  280. package/dist/tools/hashline/patch.js +201 -0
  281. package/dist/tools/index.d.ts +30 -0
  282. package/dist/tools/index.js +55 -0
  283. package/dist/tools/permission.d.ts +13 -0
  284. package/dist/tools/permission.js +39 -0
  285. package/dist/tools/registry.d.ts +15 -0
  286. package/dist/tools/registry.js +54 -0
  287. package/dist/tools/resolver.d.ts +15 -0
  288. package/dist/tools/resolver.js +47 -0
  289. package/dist/tools/truncate.d.ts +8 -0
  290. package/dist/tools/truncate.js +50 -0
  291. package/dist/tools/types.d.ts +101 -0
  292. package/dist/tools/types.js +1 -0
  293. package/dist/tools/validate.d.ts +8 -0
  294. package/dist/tools/validate.js +130 -0
  295. package/dist/tools/websearch/fetch.d.ts +11 -0
  296. package/dist/tools/websearch/fetch.js +21 -0
  297. package/dist/tools/websearch/index.d.ts +19 -0
  298. package/dist/tools/websearch/index.js +78 -0
  299. package/dist/tools/websearch/providers/brave.d.ts +5 -0
  300. package/dist/tools/websearch/providers/brave.js +53 -0
  301. package/dist/tools/websearch/providers/duckduckgo.d.ts +3 -0
  302. package/dist/tools/websearch/providers/duckduckgo.js +75 -0
  303. package/dist/tools/websearch/providers/tavily.d.ts +11 -0
  304. package/dist/tools/websearch/providers/tavily.js +89 -0
  305. package/dist/tools/websearch/types.d.ts +51 -0
  306. package/dist/tools/websearch/types.js +18 -0
  307. package/dist/tools/websearch/websearch.d.ts +10 -0
  308. package/dist/tools/websearch/websearch.js +48 -0
  309. package/dist/update/index.d.ts +10 -0
  310. package/dist/update/index.js +6 -0
  311. package/dist/update/ipc.d.ts +15 -0
  312. package/dist/update/ipc.js +41 -0
  313. package/dist/update/scheduler.d.ts +34 -0
  314. package/dist/update/scheduler.js +57 -0
  315. package/dist/update/snapshot.d.ts +39 -0
  316. package/dist/update/snapshot.js +99 -0
  317. package/dist/update/updater.d.ts +48 -0
  318. package/dist/update/updater.js +83 -0
  319. package/dist/update/version.d.ts +18 -0
  320. package/dist/update/version.js +49 -0
  321. package/drizzle/0000_magical_nightshade.sql +55 -0
  322. package/drizzle/0001_chemical_doctor_faustus.sql +13 -0
  323. package/drizzle/0002_amusing_micromacro.sql +12 -0
  324. package/drizzle/0003_regular_purifiers.sql +2 -0
  325. package/drizzle/meta/0000_snapshot.json +448 -0
  326. package/drizzle/meta/0001_snapshot.json +539 -0
  327. package/drizzle/meta/0002_snapshot.json +633 -0
  328. package/drizzle/meta/0003_snapshot.json +645 -0
  329. package/drizzle/meta/_journal.json +34 -0
  330. package/package.json +100 -8
@@ -0,0 +1,202 @@
1
+ import type { ChatTool } from './llm.js';
2
+ import type { Message, Session } from './message.js';
3
+ import type { ToolDef, ToolResult } from './tool.js';
4
+ /** Agent error variants. Discriminated by `_tag`. */
5
+ type AgentError = {
6
+ _tag: 'aborted';
7
+ } | {
8
+ _tag: 'max_turns';
9
+ maxTurns: number;
10
+ } | {
11
+ _tag: 'unexpected';
12
+ message: string;
13
+ } | {
14
+ _tag: 'provider';
15
+ message: string;
16
+ retryable: boolean;
17
+ } | {
18
+ _tag: 'tool';
19
+ toolName: string;
20
+ message: string;
21
+ };
22
+ /** A tool call awaiting user permission. */
23
+ type PendingToolCall = {
24
+ id: string;
25
+ tool: string;
26
+ input: unknown;
27
+ };
28
+ /** Agent run-time status. Discriminated by `_tag`. */
29
+ type AgentStatus = {
30
+ _tag: 'idle';
31
+ } | {
32
+ _tag: 'running';
33
+ currentTool?: string;
34
+ turnCount: number;
35
+ } | {
36
+ _tag: 'paused';
37
+ pauseReason: string;
38
+ pendingToolCall?: PendingToolCall;
39
+ } | {
40
+ _tag: 'stopped';
41
+ reason: 'completed' | 'aborted' | 'error';
42
+ error?: AgentError;
43
+ } | {
44
+ _tag: 'interrupted';
45
+ };
46
+ /** Configuration for creating an agent. */
47
+ type AgentConfig = {
48
+ provider: string;
49
+ model: string;
50
+ maxTokens?: number;
51
+ temperature?: number;
52
+ systemPrompt?: string;
53
+ tools: string[];
54
+ plugins: string[];
55
+ maxTurns?: number;
56
+ /** primary agent 名(段记录用,spec: agent-frontend-switching §4.2)。 */
57
+ agentName?: string;
58
+ /** primary agent 的 role prompt(仅覆盖 role section,保留动态上下文)。 */
59
+ agentRolePrompt?: string;
60
+ };
61
+ /**
62
+ * Token budget for context window management.
63
+ * 分配(spec §3.6 策略1):reserved(system prompt+工具描述) 20%,
64
+ * historyBudget(历史消息) 60%,剩余 available-historyBudget 预留给当前轮次 20%。
65
+ */
66
+ type TokenBudget = {
67
+ total: number;
68
+ reserved: number;
69
+ available: number;
70
+ historyBudget: number;
71
+ used: number;
72
+ keepRecent: number;
73
+ };
74
+ /** 段内单次 LLM 调用的轻量记录(不含 messages/systemPrompt/tools——那些是段级或派生数据)。 */
75
+ type LLMCall = {
76
+ id: string;
77
+ timestamp: number;
78
+ usage: {
79
+ input: number;
80
+ output: number;
81
+ cacheRead?: number;
82
+ };
83
+ latency: {
84
+ firstToken: number;
85
+ total: number;
86
+ };
87
+ cost: number;
88
+ thinking?: string;
89
+ /** 模型回复文本(替代完整 responseChunks;前端只用文本拼接)。 */
90
+ responseText: string;
91
+ /** 非正常停止原因(length/content_filter),正常完成为 undefined。 */
92
+ finishReason?: string;
93
+ };
94
+ /** 触发新段的原因。 */
95
+ type SegmentTrigger = 'initial' | 'model_change' | 'tools_change' | 'system_prompt_change' | 'compaction' | 'user_confirmed';
96
+ /**
97
+ * 一段共享相同前缀(systemPrompt + tools + provider + model)的连续 LLM 调用。
98
+ * 段首存一次前缀快照,段内 calls 只记轻量增量,消除每轮重复存储完整 messages 的 O(N²) 冗余。
99
+ */
100
+ type LLMSegment = {
101
+ id: string;
102
+ /** hash(systemPrompt + 规格化 tools)。变化 = 前缀失效 = cache miss。 */
103
+ fingerprint: string;
104
+ provider: string;
105
+ model: string;
106
+ /** 段首快照:本段生命周期内恒定的系统提示词。 */
107
+ systemPrompt: string;
108
+ /** 段首快照:本段启用的工具规格。 */
109
+ tools: ChatTool[];
110
+ startedAt: number;
111
+ trigger: SegmentTrigger;
112
+ /** 段首 primary agent 名(段检测比较用,spec: agent-frontend-switching §4.3)。 */
113
+ agentName?: string;
114
+ /** 模型上下文窗口(来自 registry capabilities),用于总结面板使用率。段内恒定。 */
115
+ contextWindow?: number;
116
+ calls: LLMCall[];
117
+ };
118
+ /** Events emitted by the agent loop. Discriminated by `_tag`. */
119
+ type AgentEvent = {
120
+ _tag: 'status_change';
121
+ status: AgentStatus;
122
+ } | {
123
+ _tag: 'text_delta';
124
+ text: string;
125
+ } | {
126
+ _tag: 'tool_call_start';
127
+ id: string;
128
+ tool: string;
129
+ input: unknown;
130
+ } | {
131
+ _tag: 'tool_call_progress';
132
+ id: string;
133
+ progress: string;
134
+ } | {
135
+ _tag: 'tool_call_end';
136
+ id: string;
137
+ result: ToolResult;
138
+ } | {
139
+ _tag: 'thinking';
140
+ text: string;
141
+ } | {
142
+ _tag: 'usage';
143
+ input: number;
144
+ output: number;
145
+ cacheRead?: number;
146
+ } | {
147
+ _tag: 'permission_required';
148
+ toolCallId: string;
149
+ tool: string;
150
+ input: unknown;
151
+ } | {
152
+ _tag: 'error';
153
+ error: AgentError;
154
+ }
155
+ /** 通知前端:本轮 LLM 调用详情已持久化,应刷新调用详情面板。轻量通知,不带 payload。 */
156
+ | {
157
+ _tag: 'llm_detail';
158
+ } | {
159
+ _tag: 'subagent_start';
160
+ childId: string;
161
+ agentType: string;
162
+ description: string;
163
+ background: boolean;
164
+ } | {
165
+ _tag: 'subagent_progress';
166
+ childId: string;
167
+ toolName?: string;
168
+ status: 'running' | 'completed' | 'failed';
169
+ } | {
170
+ _tag: 'subagent_end';
171
+ childId: string;
172
+ agentType: string;
173
+ success: boolean;
174
+ output?: string;
175
+ } | {
176
+ _tag: 'done';
177
+ };
178
+ /**
179
+ * Mutable agent state.
180
+ * Per the data+functions paradigm, the context object may be modified in place.
181
+ */
182
+ type AgentState = {
183
+ id: string;
184
+ session: Session;
185
+ messages: Message[];
186
+ tools: ToolDef[];
187
+ config: AgentConfig;
188
+ status: AgentStatus;
189
+ abortController: AbortController;
190
+ steeringQueue: string[];
191
+ segments: LLMSegment[];
192
+ /** compaction 等事件要求下一轮强制开新段时设置;loop 消费后清除。 */
193
+ pendingSegmentTrigger?: SegmentTrigger;
194
+ tokenBudget: TokenBudget;
195
+ /** estimateTokens 的校准系数(由 calibrateEstimate 按真实 usage EMA 更新,默认 1.0)。 */
196
+ calibrationFactor: number;
197
+ compactionModel?: {
198
+ provider: string;
199
+ model: string;
200
+ };
201
+ };
202
+ export type { AgentConfig, AgentError, AgentEvent, AgentState, AgentStatus, LLMCall, LLMSegment, PendingToolCall, SegmentTrigger, TokenBudget, };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ /**
2
+ * JSON Schema type (draft-07 compatible subset).
3
+ * Used for tool parameter schemas and config schemas.
4
+ */
5
+ type JSONSchema = {
6
+ type?: string;
7
+ properties?: Record<string, JSONSchema>;
8
+ required?: string[];
9
+ items?: JSONSchema | JSONSchema[];
10
+ description?: string;
11
+ enum?: unknown[];
12
+ additionalProperties?: boolean | JSONSchema;
13
+ allOf?: JSONSchema[];
14
+ anyOf?: JSONSchema[];
15
+ oneOf?: JSONSchema[];
16
+ $ref?: string;
17
+ default?: unknown;
18
+ examples?: unknown[];
19
+ [key: string]: unknown;
20
+ };
21
+ /** Role of a message in a conversation. */
22
+ type MessageRole = 'user' | 'assistant' | 'system' | 'tool';
23
+ /**
24
+ * Lightweight session reference.
25
+ * Used by ToolContext and other cross-package types to avoid
26
+ * importing the full Session type from the session package.
27
+ */
28
+ type SessionRef = {
29
+ id: string;
30
+ cwd: string;
31
+ };
32
+ export type { JSONSchema, MessageRole, SessionRef };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,104 @@
1
+ import type { ProviderConfig } from './llm.js';
2
+ /** MCP server configuration. */
3
+ type MCPServerConfig = {
4
+ name: string;
5
+ transport: 'stdio' | 'sse' | 'http';
6
+ command?: string;
7
+ args?: string[];
8
+ url?: string;
9
+ };
10
+ /** Context compaction configuration. */
11
+ type CompactionConfig = {
12
+ enabled: boolean;
13
+ /** Token usage ratio that triggers compaction (e.g. 0.8 = 80%). */
14
+ threshold: number;
15
+ /** Token space to reserve after compaction. */
16
+ reserveTokens: number;
17
+ /** Token budget for retaining recent messages verbatim. */
18
+ keepRecentTokens: number;
19
+ };
20
+ /** Tool-mode auto-selection configuration (spec §16.5). */
21
+ type ToolMetricsConfig = {
22
+ enabled: boolean;
23
+ /** Minimum success ratio to prefer a mode (e.g. 0.8 = 80%). */
24
+ threshold: number;
25
+ /** Minimum sample count before trusting a mode's success ratio. */
26
+ minSamples: number;
27
+ };
28
+ /** Web 搜索配置(见 docs/superpowers/specs/2026-06-30-websearch-tool-design.md)。 */
29
+ type WebSearchConfig = {
30
+ /** 后端选择。'auto'(默认)→ 按 key 可用性:tavily > brave > duckduckgo。 */
31
+ provider: 'auto' | 'duckduckgo' | 'tavily' | 'brave';
32
+ /** Tavily key;也可由环境变量 TAVILY_API_KEY 提供(环境变量优先)。 */
33
+ tavilyApiKey?: string;
34
+ /** Brave key;也可由环境变量 BRAVE_API_KEY 提供(环境变量优先)。 */
35
+ braveApiKey?: string;
36
+ };
37
+ /** 多 agent 配置(spec: multi-agent-design §4.12)。 */
38
+ type AgentsConfig = {
39
+ /** agent markdown 目录(相对项目根),默认 '.c0de/agents'。 */
40
+ dir: string;
41
+ /** 并行子 agent 数上限,默认 3。 */
42
+ subagentConcurrency: number;
43
+ };
44
+ /** 权限配置:控制工具执行授权的默认行为。 */
45
+ type PermissionConfig = {
46
+ /** 启动时的默认授权模式:'default' 逐个确认(默认);'auto' 自动放行 ask 工具(YOLO 自动授权)。 */
47
+ defaultMode: 'default' | 'auto';
48
+ };
49
+ /** 自动升级配置(spec §18)。控制后台 npm registry 检查与无感知热更新行为。 */
50
+ type UpdateConfig = {
51
+ /** 启用后台定期版本检查(默认 true)。 */
52
+ enabled: boolean;
53
+ /** 检查间隔(毫秒),默认 1 小时。 */
54
+ intervalMs: number;
55
+ /** 启动后首次检查的延迟(毫秒),默认 10 秒。 */
56
+ initialDelayMs: number;
57
+ /** 发现新版本后自动应用(无感知),false 则仅前端提示手动确认(默认 false)。 */
58
+ autoApply: boolean;
59
+ };
60
+ /** Server security configuration (spec §24.2). */
61
+ type SecurityConfig = {
62
+ /** 启用 Bearer token 认证(远程访问场景)。本地开发默认关闭。 */
63
+ authEnabled: boolean;
64
+ /** Bearer token;authEnabled 为 true 时必填。 */
65
+ token?: string;
66
+ /** 额外允许的 CORS origin(本地回环始终允许)。 */
67
+ allowedOrigins: string[];
68
+ };
69
+ /** Global application configuration. */
70
+ type Config = {
71
+ providers: ProviderConfig[];
72
+ defaultProvider: string;
73
+ defaultModel: string;
74
+ roleRouting: Record<string, {
75
+ provider: string;
76
+ model: string;
77
+ }>;
78
+ fallback: {
79
+ enabled: boolean;
80
+ maxRetries: number;
81
+ retryDelay: number;
82
+ };
83
+ compaction: CompactionConfig;
84
+ tools: {
85
+ enabled: string[];
86
+ disabled: string[];
87
+ };
88
+ plugins: {
89
+ enabled: string[];
90
+ };
91
+ mcpServers: MCPServerConfig[];
92
+ slashCommands: {
93
+ enabled: string[];
94
+ };
95
+ toolMetrics: ToolMetricsConfig;
96
+ security: SecurityConfig;
97
+ websearch: WebSearchConfig;
98
+ agents: AgentsConfig;
99
+ permission: PermissionConfig;
100
+ update: UpdateConfig;
101
+ theme: 'light' | 'dark' | 'system';
102
+ locale: string;
103
+ };
104
+ export type { AgentsConfig, CompactionConfig, Config, MCPServerConfig, PermissionConfig, SecurityConfig, ToolMetricsConfig, UpdateConfig, WebSearchConfig, };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ export type * from './agent.js';
2
+ export type * from './base.js';
3
+ export type * from './config.js';
4
+ export type * from './llm.js';
5
+ export type * from './message.js';
6
+ export type * from './tool.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,122 @@
1
+ import type { JSONSchema } from './base.js';
2
+ /** Supported LLM provider protocols. */
3
+ type ProviderProtocol = 'openai' | 'anthropic' | 'google' | 'openai-compat';
4
+ /** Why a model response ended. Protocol-agnostic; mirrored by each provider. */
5
+ type FinishReason = 'stop' | 'length' | 'tool-calls' | 'content-filter' | 'error' | 'unknown';
6
+ /** Provider configuration entry. */
7
+ type ProviderConfig = {
8
+ name: string;
9
+ protocol: ProviderProtocol;
10
+ apiKey: string;
11
+ baseURL?: string;
12
+ models?: Record<string, ModelOverride>;
13
+ };
14
+ /** Per-model configuration overrides. */
15
+ type ModelOverride = {
16
+ /** 是否在会话模型选择器中启用。省略或 true 为启用;false 为禁用(仍保留记录)。 */
17
+ enabled?: boolean;
18
+ contextWindow?: number;
19
+ maxOutput?: number;
20
+ supportsTools?: boolean;
21
+ supportsVision?: boolean;
22
+ supportsThinking?: boolean;
23
+ costPer1kInput?: number;
24
+ costPer1kOutput?: number;
25
+ };
26
+ /** Static model capability descriptor. */
27
+ type ModelCapabilities = {
28
+ contextWindow: number;
29
+ maxOutput: number;
30
+ supportsTools: boolean;
31
+ supportsVision: boolean;
32
+ supportsThinking: boolean;
33
+ costPer1kInput: number;
34
+ costPer1kOutput: number;
35
+ };
36
+ /** Role tag for multi-model routing. */
37
+ type ModelRole = {
38
+ readonly _tag: 'default';
39
+ } | {
40
+ readonly _tag: 'smol';
41
+ } | {
42
+ readonly _tag: 'slow';
43
+ } | {
44
+ readonly _tag: 'plan';
45
+ } | {
46
+ readonly _tag: 'commit';
47
+ };
48
+ /** Content part for multimodal messages (text or image). */
49
+ type ContentPart = {
50
+ type: 'text';
51
+ text: string;
52
+ } | {
53
+ type: 'image';
54
+ mediaType: string;
55
+ data: string;
56
+ };
57
+ /** Protocol-level chat message sent to the LLM provider. */
58
+ type ChatMessage = {
59
+ role: 'system' | 'user' | 'assistant' | 'tool';
60
+ content: string | ContentPart[];
61
+ toolCallId?: string;
62
+ toolCalls?: {
63
+ id: string;
64
+ name: string;
65
+ arguments: string;
66
+ }[];
67
+ };
68
+ /** Tool definition as sent to the LLM provider. */
69
+ type ChatTool = {
70
+ name: string;
71
+ description: string;
72
+ parameters: JSONSchema;
73
+ };
74
+ /** Request to the LLM provider. */
75
+ type ChatRequest = {
76
+ model: string;
77
+ messages: ChatMessage[];
78
+ tools?: ChatTool[];
79
+ stream: true;
80
+ maxTokens?: number;
81
+ temperature?: number;
82
+ system?: string;
83
+ };
84
+ /**
85
+ * Streaming chunk from the LLM provider.
86
+ * Tool calls stream in three phases: start → delta(s) → end.
87
+ * The LLM package normalizes all provider formats to this union.
88
+ */
89
+ type StreamChunk = {
90
+ _tag: 'text';
91
+ text: string;
92
+ } | {
93
+ _tag: 'tool_call_start';
94
+ id: string;
95
+ name: string;
96
+ } | {
97
+ _tag: 'tool_call_delta';
98
+ id: string;
99
+ argumentsDelta: string;
100
+ } | {
101
+ _tag: 'tool_call_end';
102
+ id: string;
103
+ argumentsFinal?: string;
104
+ } | {
105
+ _tag: 'thinking';
106
+ text: string;
107
+ } | {
108
+ _tag: 'usage';
109
+ inputTokens: number;
110
+ outputTokens: number;
111
+ cacheRead?: number;
112
+ } | {
113
+ _tag: 'done';
114
+ finishReason?: FinishReason;
115
+ } | {
116
+ _tag: 'error';
117
+ error: {
118
+ message: string;
119
+ retryable?: boolean;
120
+ };
121
+ };
122
+ export type { ChatMessage, ChatRequest, ChatTool, ContentPart, FinishReason, ModelCapabilities, ModelOverride, ModelRole, ProviderConfig, ProviderProtocol, StreamChunk, };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,71 @@
1
+ import type { LLMSegment } from './agent.js';
2
+ import type { MessageRole } from './base.js';
3
+ import type { ToolResult } from './tool.js';
4
+ /** Content variants within a single message. Discriminated by `_tag`. */
5
+ type MessageContent = {
6
+ _tag: 'text';
7
+ text: string;
8
+ } | {
9
+ _tag: 'tool_call';
10
+ id: string;
11
+ tool: string;
12
+ input: unknown;
13
+ } | {
14
+ _tag: 'tool_result';
15
+ id: string;
16
+ tool: string;
17
+ output: ToolResult;
18
+ } | {
19
+ _tag: 'thinking';
20
+ text: string;
21
+ } | {
22
+ _tag: 'steering';
23
+ text: string;
24
+ } | {
25
+ _tag: 'image';
26
+ mediaType: string;
27
+ data: string;
28
+ };
29
+ /** A single message in a session. Content is always an array of parts. */
30
+ type Message = {
31
+ id: string;
32
+ sessionId: string;
33
+ role: MessageRole;
34
+ content: MessageContent[];
35
+ tokenCount: number;
36
+ createdAt: number;
37
+ };
38
+ /** 上次 agent run 的持久化状态(服务重启后检测中断用)。 */
39
+ type LastRun = {
40
+ status: 'running' | 'completed';
41
+ agentName?: string;
42
+ provider?: string;
43
+ model?: string;
44
+ startedAt: number;
45
+ };
46
+ /** Session metadata for branching and compaction tracking. */
47
+ type SessionMetadata = {
48
+ mainThreadId?: string;
49
+ squashCount?: number;
50
+ fileSnapshots?: string[];
51
+ /** 本会话分段增量 LLM 调用记录,用于调用详情面板展示。 */
52
+ segments?: LLMSegment[];
53
+ /** 上次 agent run 状态;status='running' 且进程无活跃 run → 被中断。 */
54
+ lastRun?: LastRun;
55
+ };
56
+ /** A conversation session (may have a parent for branching). */
57
+ type Session = {
58
+ id: string;
59
+ title: string;
60
+ parentId: string | null;
61
+ projectId: string | null;
62
+ branchPoint: number | null;
63
+ metadata: SessionMetadata;
64
+ /** 子 session 用的 agent 类型名(null=主 session)。 */
65
+ agentType: string | null;
66
+ /** 隔离 worktree 路径(null=共享父 cwd)。 */
67
+ worktreePath: string | null;
68
+ createdAt: number;
69
+ updatedAt: number;
70
+ };
71
+ export type { LastRun, Message, MessageContent, Session, SessionMetadata };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,134 @@
1
+ import type { JSONSchema, SessionRef } from './base.js';
2
+ /** Permission level for tool execution. */
3
+ type ToolPermission = 'auto' | 'ask' | 'deny';
4
+ /** Result of a tool execution. Discriminated by `_tag`. */
5
+ type ToolResult = {
6
+ _tag: 'success';
7
+ output: string;
8
+ metadata?: Record<string, unknown>;
9
+ } | {
10
+ _tag: 'error';
11
+ error: string;
12
+ } | {
13
+ _tag: 'permission_required';
14
+ reason: string;
15
+ } | {
16
+ _tag: 'truncated';
17
+ output: string;
18
+ truncated: boolean;
19
+ totalLines: number;
20
+ };
21
+ /** Context passed to a URL resolver (skill://, agent://, pr://, …). */
22
+ type URLResolveContext = {
23
+ cwd: string;
24
+ session: SessionRef;
25
+ };
26
+ /** A resolver for one URL scheme (e.g. `skill`, `agent`, `pr`). */
27
+ type URLResolver = {
28
+ scheme: string;
29
+ resolve: (url: string, ctx: URLResolveContext) => Promise<string>;
30
+ };
31
+ /** Mutable registry of URL resolvers, keyed by scheme. */
32
+ type URLRegistry = {
33
+ resolvers: Map<string, URLResolver>;
34
+ };
35
+ /** Result of resolving a URL. */
36
+ type ResolveResult = {
37
+ _tag: 'ok';
38
+ content: string;
39
+ } | {
40
+ _tag: 'error';
41
+ error: string;
42
+ };
43
+ /** Transport shape for a debug adapter process stdio (DAP). Host-injected. */
44
+ type DebugTransport = {
45
+ write: (chunk: string | Uint8Array) => void;
46
+ onData: (handler: (chunk: Uint8Array | string) => void) => void;
47
+ onClose: (handler: () => void) => void;
48
+ close: () => void;
49
+ };
50
+ /** Context passed to every tool executor. */
51
+ type ToolContext = {
52
+ cwd: string;
53
+ session: SessionRef;
54
+ abort: AbortSignal;
55
+ mode?: string;
56
+ /** URL resolver registry; present when the host wires up internal URL schemes. */
57
+ urlRegistry?: URLRegistry;
58
+ /** Spawn a sub-agent and return its final text output (dependency-reversal hook
59
+ * for the `task` tool; avoids tools→core circular import). */
60
+ runSubAgent?: (input: SubAgentRequest) => Promise<SubAgentResult>;
61
+ /** Spawn a debug adapter and return its stdio transport (dependency-reversal
62
+ * hook for the `debug_*` tools; host wires real child_process spawn). */
63
+ debugSpawn?: (config: unknown) => DebugTransport;
64
+ /** 子 agent 专用:yield 工具调用时收集结构化结果(runSubAgent 注入)。 */
65
+ collectYield?: (data: unknown) => void;
66
+ };
67
+ /** 单个并行子任务项(批量模式)。 */
68
+ type TaskItem = {
69
+ description?: string;
70
+ /** 角色细分(注入子 prompt)。 */
71
+ role?: string;
72
+ assignment: string;
73
+ };
74
+ /** Request to run a sub-agent (the `task` tool's payload to the host). */
75
+ type SubAgentRequest = {
76
+ /** agent 类型名(必填)。 */
77
+ agentType: string;
78
+ prompt: string;
79
+ description?: string;
80
+ /** 批量模式的角色细分。 */
81
+ role?: string;
82
+ /** 批量模式的共享上下文。 */
83
+ context?: string;
84
+ model?: string;
85
+ /** 后台异步运行(默认 false)。 */
86
+ background?: boolean;
87
+ };
88
+ /** Result of a sub-agent run. */
89
+ type SubAgentResult = {
90
+ _tag: 'success';
91
+ output: string;
92
+ sessionId: string;
93
+ data?: unknown;
94
+ patchPath?: string;
95
+ } | {
96
+ _tag: 'error';
97
+ error: string;
98
+ sessionId?: string;
99
+ } | {
100
+ _tag: 'running';
101
+ jobId: string;
102
+ sessionId: string;
103
+ };
104
+ /** Function signature for tool execution. */
105
+ type ToolExecutor = (input: unknown, ctx: ToolContext) => Promise<ToolResult>;
106
+ /** Optional execution mode for tools with multiple implementations. */
107
+ type ToolMode = {
108
+ name: string;
109
+ description: string;
110
+ isAvailable: (ctx: ToolContext) => boolean;
111
+ };
112
+ /** Complete tool definition registered with the tool registry. */
113
+ type ToolDef = {
114
+ name: string;
115
+ description: string;
116
+ parameters: JSONSchema;
117
+ permission: ToolPermission;
118
+ execute: ToolExecutor;
119
+ timeout?: number;
120
+ modes?: ToolMode[];
121
+ };
122
+ /** Per-(model, tool, mode) success/latency record used to auto-select the
123
+ * best tool mode (spec §16.5). Persisted in the `tool_metrics` table. */
124
+ type ModelToolMetrics = {
125
+ model: string;
126
+ tool: string;
127
+ mode: string;
128
+ attempts: number;
129
+ successes: number;
130
+ failures: number;
131
+ avgLatencyMs: number;
132
+ lastUsed: number;
133
+ };
134
+ export type { DebugTransport, ModelToolMetrics, ResolveResult, SubAgentRequest, SubAgentResult, TaskItem, ToolContext, ToolDef, ToolExecutor, ToolMode, ToolPermission, ToolResult, URLRegistry, URLResolveContext, URLResolver, };
@@ -0,0 +1 @@
1
+ export {};