c0de-agent 0.0.0 → 1.1.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 (336) 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 +30 -0
  18. package/dist/cli/deps.js +70 -0
  19. package/dist/cli/index.d.ts +8 -0
  20. package/dist/cli/index.js +155 -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 +100 -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 +85 -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 +35 -0
  54. package/dist/core/loop.js +915 -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 +113 -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 +116 -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 +97 -0
  72. package/dist/core/types.js +1 -0
  73. package/dist/core/worktree.d.ts +24 -0
  74. package/dist/core/worktree.js +70 -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 +195 -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 +69 -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 +34 -0
  160. package/dist/server/agent-manager.js +62 -0
  161. package/dist/server/app.d.ts +5 -0
  162. package/dist/server/app.js +78 -0
  163. package/dist/server/context.d.ts +20 -0
  164. package/dist/server/context.js +49 -0
  165. package/dist/server/dev.d.ts +14 -0
  166. package/dist/server/dev.js +148 -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 +32 -0
  178. package/dist/server/permission/store.js +56 -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 +318 -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 +204 -0
  203. package/dist/server/routes/terminal.d.ts +5 -0
  204. package/dist/server/routes/terminal.js +66 -0
  205. package/dist/server/routes/tool.d.ts +4 -0
  206. package/dist/server/routes/tool.js +28 -0
  207. package/dist/server/routes/update.d.ts +14 -0
  208. package/dist/server/routes/update.js +49 -0
  209. package/dist/server/server.d.ts +56 -0
  210. package/dist/server/server.js +269 -0
  211. package/dist/server/terminal/pty-manager.d.ts +53 -0
  212. package/dist/server/terminal/pty-manager.js +160 -0
  213. package/dist/server/types.d.ts +76 -0
  214. package/dist/server/types.js +2 -0
  215. package/dist/server/util/safe-path.d.ts +7 -0
  216. package/dist/server/util/safe-path.js +7 -0
  217. package/dist/session/archive.d.ts +15 -0
  218. package/dist/session/archive.js +129 -0
  219. package/dist/session/branch.d.ts +9 -0
  220. package/dist/session/branch.js +63 -0
  221. package/dist/session/compaction.d.ts +33 -0
  222. package/dist/session/compaction.js +261 -0
  223. package/dist/session/context.d.ts +16 -0
  224. package/dist/session/context.js +173 -0
  225. package/dist/session/index.d.ts +10 -0
  226. package/dist/session/index.js +10 -0
  227. package/dist/session/message.d.ts +22 -0
  228. package/dist/session/message.js +145 -0
  229. package/dist/session/session.d.ts +37 -0
  230. package/dist/session/session.js +144 -0
  231. package/dist/session/shake.d.ts +66 -0
  232. package/dist/session/shake.js +304 -0
  233. package/dist/session/snapshot.d.ts +11 -0
  234. package/dist/session/snapshot.js +60 -0
  235. package/dist/session/squash.d.ts +9 -0
  236. package/dist/session/squash.js +88 -0
  237. package/dist/session/token.d.ts +10 -0
  238. package/dist/session/token.js +33 -0
  239. package/dist/session/types.d.ts +122 -0
  240. package/dist/session/types.js +1 -0
  241. package/dist/shared/index.d.ts +2 -0
  242. package/dist/shared/index.js +1 -0
  243. package/dist/shared/types/agent.d.ts +229 -0
  244. package/dist/shared/types/agent.js +1 -0
  245. package/dist/shared/types/base.d.ts +32 -0
  246. package/dist/shared/types/base.js +1 -0
  247. package/dist/shared/types/config.d.ts +114 -0
  248. package/dist/shared/types/config.js +1 -0
  249. package/dist/shared/types/index.d.ts +6 -0
  250. package/dist/shared/types/index.js +1 -0
  251. package/dist/shared/types/llm.d.ts +123 -0
  252. package/dist/shared/types/llm.js +1 -0
  253. package/dist/shared/types/message.d.ts +71 -0
  254. package/dist/shared/types/message.js +1 -0
  255. package/dist/shared/types/tool.d.ts +137 -0
  256. package/dist/shared/types/tool.js +1 -0
  257. package/dist/shared/utils/path.d.ts +15 -0
  258. package/dist/shared/utils/path.js +23 -0
  259. package/dist/shared/utils.d.ts +5 -0
  260. package/dist/shared/utils.js +10 -0
  261. package/dist/tools/builtin/bash.d.ts +12 -0
  262. package/dist/tools/builtin/bash.js +121 -0
  263. package/dist/tools/builtin/dap.d.ts +23 -0
  264. package/dist/tools/builtin/dap.js +231 -0
  265. package/dist/tools/builtin/edit.d.ts +12 -0
  266. package/dist/tools/builtin/edit.js +151 -0
  267. package/dist/tools/builtin/glob.d.ts +11 -0
  268. package/dist/tools/builtin/glob.js +120 -0
  269. package/dist/tools/builtin/grep.d.ts +6 -0
  270. package/dist/tools/builtin/grep.js +141 -0
  271. package/dist/tools/builtin/read.d.ts +6 -0
  272. package/dist/tools/builtin/read.js +73 -0
  273. package/dist/tools/builtin/resolvers.d.ts +12 -0
  274. package/dist/tools/builtin/resolvers.js +79 -0
  275. package/dist/tools/builtin/task.d.ts +9 -0
  276. package/dist/tools/builtin/task.js +100 -0
  277. package/dist/tools/builtin/write.d.ts +6 -0
  278. package/dist/tools/builtin/write.js +36 -0
  279. package/dist/tools/builtin/yield.d.ts +9 -0
  280. package/dist/tools/builtin/yield.js +40 -0
  281. package/dist/tools/executor.d.ts +9 -0
  282. package/dist/tools/executor.js +54 -0
  283. package/dist/tools/hashline/index.d.ts +2 -0
  284. package/dist/tools/hashline/index.js +3 -0
  285. package/dist/tools/hashline/patch.d.ts +48 -0
  286. package/dist/tools/hashline/patch.js +201 -0
  287. package/dist/tools/index.d.ts +30 -0
  288. package/dist/tools/index.js +55 -0
  289. package/dist/tools/permission.d.ts +13 -0
  290. package/dist/tools/permission.js +39 -0
  291. package/dist/tools/registry.d.ts +15 -0
  292. package/dist/tools/registry.js +54 -0
  293. package/dist/tools/resolver.d.ts +15 -0
  294. package/dist/tools/resolver.js +47 -0
  295. package/dist/tools/truncate.d.ts +8 -0
  296. package/dist/tools/truncate.js +50 -0
  297. package/dist/tools/types.d.ts +101 -0
  298. package/dist/tools/types.js +1 -0
  299. package/dist/tools/validate.d.ts +8 -0
  300. package/dist/tools/validate.js +130 -0
  301. package/dist/tools/websearch/fetch.d.ts +11 -0
  302. package/dist/tools/websearch/fetch.js +21 -0
  303. package/dist/tools/websearch/index.d.ts +19 -0
  304. package/dist/tools/websearch/index.js +78 -0
  305. package/dist/tools/websearch/providers/brave.d.ts +5 -0
  306. package/dist/tools/websearch/providers/brave.js +53 -0
  307. package/dist/tools/websearch/providers/duckduckgo.d.ts +3 -0
  308. package/dist/tools/websearch/providers/duckduckgo.js +75 -0
  309. package/dist/tools/websearch/providers/tavily.d.ts +11 -0
  310. package/dist/tools/websearch/providers/tavily.js +89 -0
  311. package/dist/tools/websearch/types.d.ts +51 -0
  312. package/dist/tools/websearch/types.js +18 -0
  313. package/dist/tools/websearch/websearch.d.ts +10 -0
  314. package/dist/tools/websearch/websearch.js +48 -0
  315. package/dist/update/index.d.ts +10 -0
  316. package/dist/update/index.js +6 -0
  317. package/dist/update/ipc.d.ts +15 -0
  318. package/dist/update/ipc.js +41 -0
  319. package/dist/update/scheduler.d.ts +34 -0
  320. package/dist/update/scheduler.js +57 -0
  321. package/dist/update/snapshot.d.ts +39 -0
  322. package/dist/update/snapshot.js +99 -0
  323. package/dist/update/updater.d.ts +48 -0
  324. package/dist/update/updater.js +83 -0
  325. package/dist/update/version.d.ts +18 -0
  326. package/dist/update/version.js +49 -0
  327. package/drizzle/0000_magical_nightshade.sql +55 -0
  328. package/drizzle/0001_chemical_doctor_faustus.sql +13 -0
  329. package/drizzle/0002_amusing_micromacro.sql +12 -0
  330. package/drizzle/0003_regular_purifiers.sql +2 -0
  331. package/drizzle/meta/0000_snapshot.json +448 -0
  332. package/drizzle/meta/0001_snapshot.json +539 -0
  333. package/drizzle/meta/0002_snapshot.json +633 -0
  334. package/drizzle/meta/0003_snapshot.json +645 -0
  335. package/drizzle/meta/_journal.json +34 -0
  336. package/package.json +108 -8
@@ -0,0 +1,229 @@
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
+ /**
177
+ * 会话压缩成功后发出(spec: plugin-hooks `session:compact`)。
178
+ * 实际发生压缩(runCompaction 返回 compacted=true)时才 yield;
179
+ * nothing_to_compact 不发。archiveId/summary 仅在真实压缩时存在。
180
+ */
181
+ | {
182
+ _tag: 'compaction_done';
183
+ summary: string;
184
+ archiveId?: string;
185
+ compactedCount: number;
186
+ keptCount: number;
187
+ } | {
188
+ _tag: 'done';
189
+ };
190
+ /**
191
+ * Mutable agent state.
192
+ * Per the data+functions paradigm, the context object may be modified in place.
193
+ */
194
+ type AgentState = {
195
+ id: string;
196
+ session: Session;
197
+ messages: Message[];
198
+ tools: ToolDef[];
199
+ config: AgentConfig;
200
+ status: AgentStatus;
201
+ abortController: AbortController;
202
+ steeringQueue: string[];
203
+ segments: LLMSegment[];
204
+ /** compaction 等事件要求下一轮强制开新段时设置;loop 消费后清除。 */
205
+ pendingSegmentTrigger?: SegmentTrigger;
206
+ tokenBudget: TokenBudget;
207
+ /** estimateTokens 的校准系数(由 calibrateEstimate 按真实 usage EMA 更新,默认 1.0)。 */
208
+ calibrationFactor: number;
209
+ compactionModel?: {
210
+ provider: string;
211
+ model: string;
212
+ };
213
+ /**
214
+ * 压缩死锁标记:自动压缩成功后仍超阈值(如 keepRecentTokens 本身已超限)时置真,
215
+ * 暂停后续自动压缩以防每轮重复触发(无限循环)。收到新用户消息(agentLoop 重入)时重置。
216
+ */
217
+ compactionDeadEnd?: boolean;
218
+ /**
219
+ * 压缩退化监测器:压缩成功后初始化,监测接下来若干轮 assistant 回复。
220
+ * 若连续产生空回复(无实质文本且无 tool_call),发出非致命警告(不中断循环),
221
+ * 提示 agent 可能在"沉默退化"。remaining 耗尽即清除;新一轮用户输入
222
+ * (agentLoop 重入)时不会自动重置——它由压缩成功单独建立。
223
+ */
224
+ postCompactionMonitor?: {
225
+ remaining: number;
226
+ noTextStreak: number;
227
+ };
228
+ };
229
+ 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,114 @@
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
+ /** 中轮压缩(mid-run compaction):单个 turn 内工具执行后、下一次 LLM 请求前
20
+ * 按阈值静默压缩。与 turn-end 自动压缩独立——以本开关为闸门复用 shouldCompact
21
+ * 的阈值逻辑(不受 `enabled` 影响),默认关闭(保守开启)。 */
22
+ midTurnEnabled?: boolean;
23
+ /** 压缩摘要使用的模型覆盖。未设置时回退到会话主模型。
24
+ * 摘要任务对推理能力要求低,可指定便宜/快速模型以降低成本。 */
25
+ compactionModel?: {
26
+ provider: string;
27
+ model: string;
28
+ };
29
+ };
30
+ /** Tool-mode auto-selection configuration (spec §16.5). */
31
+ type ToolMetricsConfig = {
32
+ enabled: boolean;
33
+ /** Minimum success ratio to prefer a mode (e.g. 0.8 = 80%). */
34
+ threshold: number;
35
+ /** Minimum sample count before trusting a mode's success ratio. */
36
+ minSamples: number;
37
+ };
38
+ /** Web 搜索配置(见 docs/superpowers/specs/2026-06-30-websearch-tool-design.md)。 */
39
+ type WebSearchConfig = {
40
+ /** 后端选择。'auto'(默认)→ 按 key 可用性:tavily > brave > duckduckgo。 */
41
+ provider: 'auto' | 'duckduckgo' | 'tavily' | 'brave';
42
+ /** Tavily key;也可由环境变量 TAVILY_API_KEY 提供(环境变量优先)。 */
43
+ tavilyApiKey?: string;
44
+ /** Brave key;也可由环境变量 BRAVE_API_KEY 提供(环境变量优先)。 */
45
+ braveApiKey?: string;
46
+ };
47
+ /** 多 agent 配置(spec: multi-agent-design §4.12)。 */
48
+ type AgentsConfig = {
49
+ /** agent markdown 目录(相对项目根),默认 '.c0de/agents'。 */
50
+ dir: string;
51
+ /** 并行子 agent 数上限,默认 3。 */
52
+ subagentConcurrency: number;
53
+ };
54
+ /** 权限配置:控制工具执行授权的默认行为。 */
55
+ type PermissionConfig = {
56
+ /** 启动时的默认授权模式:'default' 逐个确认(默认);'auto' 自动放行 ask 工具(YOLO 自动授权)。 */
57
+ defaultMode: 'default' | 'auto';
58
+ };
59
+ /** 自动升级配置(spec §18)。控制后台 npm registry 检查与无感知热更新行为。 */
60
+ type UpdateConfig = {
61
+ /** 启用后台定期版本检查(默认 true)。 */
62
+ enabled: boolean;
63
+ /** 检查间隔(毫秒),默认 1 小时。 */
64
+ intervalMs: number;
65
+ /** 启动后首次检查的延迟(毫秒),默认 10 秒。 */
66
+ initialDelayMs: number;
67
+ /** 发现新版本后自动应用(无感知),false 则仅前端提示手动确认(默认 false)。 */
68
+ autoApply: boolean;
69
+ };
70
+ /** Server security configuration (spec §24.2). */
71
+ type SecurityConfig = {
72
+ /** 启用 Bearer token 认证(远程访问场景)。本地开发默认关闭。 */
73
+ authEnabled: boolean;
74
+ /** Bearer token;authEnabled 为 true 时必填。 */
75
+ token?: string;
76
+ /** 额外允许的 CORS origin(本地回环始终允许)。 */
77
+ allowedOrigins: string[];
78
+ };
79
+ /** Global application configuration. */
80
+ type Config = {
81
+ providers: ProviderConfig[];
82
+ defaultProvider: string;
83
+ defaultModel: string;
84
+ roleRouting: Record<string, {
85
+ provider: string;
86
+ model: string;
87
+ }>;
88
+ fallback: {
89
+ enabled: boolean;
90
+ maxRetries: number;
91
+ retryDelay: number;
92
+ };
93
+ compaction: CompactionConfig;
94
+ tools: {
95
+ enabled: string[];
96
+ disabled: string[];
97
+ };
98
+ plugins: {
99
+ enabled: string[];
100
+ };
101
+ mcpServers: MCPServerConfig[];
102
+ slashCommands: {
103
+ enabled: string[];
104
+ };
105
+ toolMetrics: ToolMetricsConfig;
106
+ security: SecurityConfig;
107
+ websearch: WebSearchConfig;
108
+ agents: AgentsConfig;
109
+ permission: PermissionConfig;
110
+ update: UpdateConfig;
111
+ theme: 'light' | 'dark' | 'system';
112
+ locale: string;
113
+ };
114
+ 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,123 @@
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
+ classification?: 'context-overflow';
121
+ };
122
+ };
123
+ 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 {};