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,83 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ const DEFAULT_PACKAGE = 'c0de-agent';
6
+ /** 默认自更新:npm install -g <pkg>。 */
7
+ function defaultInstall(pkg) {
8
+ return new Promise((resolve, reject) => {
9
+ const child = spawn('npm', ['install', '-g', pkg], { stdio: 'ignore' });
10
+ child.on('error', reject);
11
+ child.on('exit', (code) => {
12
+ if (code === 0)
13
+ resolve();
14
+ else
15
+ reject(new Error(`npm install -g ${pkg} exited with ${code}`));
16
+ });
17
+ });
18
+ }
19
+ /** 默认启动新实例:detached spawn,argv 全部透传给新进程。 */
20
+ function defaultSpawn(_snapshotPath, argv) {
21
+ return new Promise((resolve, reject) => {
22
+ try {
23
+ const child = spawn(process.argv0, [process.argv[1] ?? '', ...argv], {
24
+ detached: true,
25
+ stdio: 'ignore',
26
+ });
27
+ child.unref();
28
+ resolve();
29
+ }
30
+ catch (error) {
31
+ reject(error instanceof Error ? error : new Error(String(error)));
32
+ }
33
+ });
34
+ }
35
+ /**
36
+ * 执行热更新(spec §18.1-18.2 流程):
37
+ * 1. 序列化当前会话状态到快照文件
38
+ * 2. 全局自更新(npm install -g)
39
+ * 3. detached 启动新版本实例,传入快照路径(新实例 restore + 端口接管)
40
+ *
41
+ * 注:旧实例的 graceful shutdown / 端口让渡由新实例启动后通过 IPC 协调
42
+ * (见 ipc.ts),本函数只负责"序列化 + 安装 + 接力"。若提供 handoffPort,
43
+ * argv 会带上 --handoff-port,新实例据此 requestHandoff 通知旧实例退出。
44
+ */
45
+ async function performHotUpdate(snapshot, opts = {}) {
46
+ const pkg = opts.packageName ?? DEFAULT_PACKAGE;
47
+ const dir = await mkdtemp(join(tmpdir(), 'c0de-update-'));
48
+ const snapshotPath = opts.snapshotPath ?? join(dir, 'snapshot.json');
49
+ await writeFile(snapshotPath, JSON.stringify(snapshot), 'utf8');
50
+ const install = opts.installFn ?? defaultInstall;
51
+ try {
52
+ await install(pkg);
53
+ }
54
+ catch (error) {
55
+ return {
56
+ _tag: 'install_failed',
57
+ error: error instanceof Error ? error.message : String(error),
58
+ snapshotPath,
59
+ };
60
+ }
61
+ const restoreFlag = opts.restoreFlag ?? '--restore';
62
+ const argv = [restoreFlag, snapshotPath];
63
+ if (opts.handoffPort !== undefined) {
64
+ argv.push('--handoff-port', String(opts.handoffPort));
65
+ }
66
+ const spawnNew = opts.spawnNewInstanceFn ?? defaultSpawn;
67
+ try {
68
+ await spawnNew(snapshotPath, argv);
69
+ }
70
+ catch (error) {
71
+ return {
72
+ _tag: 'spawn_failed',
73
+ error: error instanceof Error ? error.message : String(error),
74
+ snapshotPath,
75
+ };
76
+ }
77
+ return { _tag: 'success', snapshotPath };
78
+ }
79
+ /** 清理快照临时目录(热更新完成或放弃后调用)。 */
80
+ async function cleanupSnapshot(snapshotPath) {
81
+ await rm(join(snapshotPath, '..'), { recursive: true, force: true }).catch(() => { });
82
+ }
83
+ export { cleanupSnapshot, performHotUpdate };
@@ -0,0 +1,18 @@
1
+ type UpdateCheckResult = {
2
+ hasUpdate: boolean;
3
+ currentVersion: string;
4
+ latestVersion: string;
5
+ releaseNotes?: string;
6
+ };
7
+ type CheckOptions = {
8
+ fetchImpl?: typeof fetch;
9
+ packageName?: string;
10
+ currentVersion?: string;
11
+ registryUrl?: string;
12
+ };
13
+ /** 语义化版本比较:a < b → -1,a == b → 0,a > b → 1。忽略前导 v 和 prerelease。 */
14
+ declare function compareSemver(a: string, b: string): number;
15
+ /** 查询 npm registry 判断是否有新版本。网络/解析失败时返回 hasUpdate:false,绝不抛错。 */
16
+ declare function checkForUpdate(opts?: CheckOptions): Promise<UpdateCheckResult>;
17
+ export type { CheckOptions, UpdateCheckResult };
18
+ export { checkForUpdate, compareSemver };
@@ -0,0 +1,49 @@
1
+ // 版本检查(spec §18.1):查询 npm registry 比对当前版本。
2
+ const DEFAULT_PACKAGE = 'c0de-agent';
3
+ const DEFAULT_VERSION = '0.1.0';
4
+ const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
5
+ /** 取 semver 的数值核心(去前导 v 与 prerelease)。 */
6
+ function semverCore(v) {
7
+ return (v.replace(/^v/, '').split('-')[0] ?? '').split('.').map(Number);
8
+ }
9
+ /** 语义化版本比较:a < b → -1,a == b → 0,a > b → 1。忽略前导 v 和 prerelease。 */
10
+ function compareSemver(a, b) {
11
+ const pa = semverCore(a);
12
+ const pb = semverCore(b);
13
+ for (let i = 0; i < 3; i++) {
14
+ const x = pa[i] ?? 0;
15
+ const y = pb[i] ?? 0;
16
+ if (x < y)
17
+ return -1;
18
+ if (x > y)
19
+ return 1;
20
+ }
21
+ return 0;
22
+ }
23
+ /** 查询 npm registry 判断是否有新版本。网络/解析失败时返回 hasUpdate:false,绝不抛错。 */
24
+ async function checkForUpdate(opts = {}) {
25
+ const pkg = opts.packageName ?? DEFAULT_PACKAGE;
26
+ const current = opts.currentVersion ?? DEFAULT_VERSION;
27
+ const registry = opts.registryUrl ?? DEFAULT_REGISTRY;
28
+ const fetchImpl = opts.fetchImpl ?? fetch;
29
+ const result = {
30
+ hasUpdate: false,
31
+ currentVersion: current,
32
+ latestVersion: current,
33
+ };
34
+ try {
35
+ const res = await fetchImpl(`${registry}/${pkg}/latest`);
36
+ if (!res.ok)
37
+ return result;
38
+ const data = (await res.json());
39
+ if (!data.version)
40
+ return result;
41
+ result.latestVersion = data.version;
42
+ result.hasUpdate = compareSemver(current, data.version) < 0;
43
+ }
44
+ catch {
45
+ // 离线/网络错误:保守地报告无更新
46
+ }
47
+ return result;
48
+ }
49
+ export { checkForUpdate, compareSemver };
@@ -0,0 +1,55 @@
1
+ CREATE TABLE "compaction_archives" (
2
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
3
+ "session_id" uuid NOT NULL,
4
+ "compaction_id" uuid NOT NULL,
5
+ "archive_type" text NOT NULL,
6
+ "original_entries" jsonb NOT NULL,
7
+ "file_snapshots" jsonb DEFAULT '[]'::jsonb,
8
+ "summary" text NOT NULL,
9
+ "token_count" integer,
10
+ "searchable_text" text,
11
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
12
+ );
13
+ --> statement-breakpoint
14
+ CREATE TABLE "file_snapshots" (
15
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
16
+ "session_id" uuid NOT NULL,
17
+ "entry_id" uuid,
18
+ "file_path" text NOT NULL,
19
+ "content" text NOT NULL,
20
+ "content_hash" text NOT NULL,
21
+ "token_count" integer DEFAULT 0,
22
+ "version" integer DEFAULT 1,
23
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
24
+ );
25
+ --> statement-breakpoint
26
+ CREATE TABLE "session_entries" (
27
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
28
+ "session_id" uuid NOT NULL,
29
+ "tag" text NOT NULL,
30
+ "role" text,
31
+ "content" jsonb NOT NULL,
32
+ "tool_name" text,
33
+ "token_count" integer DEFAULT 0,
34
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
35
+ );
36
+ --> statement-breakpoint
37
+ CREATE TABLE "sessions" (
38
+ "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
39
+ "title" text NOT NULL,
40
+ "parent_id" uuid,
41
+ "branch_point" integer,
42
+ "metadata" jsonb DEFAULT '{}'::jsonb NOT NULL,
43
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
44
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
45
+ );
46
+ --> statement-breakpoint
47
+ ALTER TABLE "compaction_archives" ADD CONSTRAINT "compaction_archives_session_id_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."sessions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
48
+ ALTER TABLE "file_snapshots" ADD CONSTRAINT "file_snapshots_session_id_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."sessions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
49
+ ALTER TABLE "session_entries" ADD CONSTRAINT "session_entries_session_id_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."sessions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
50
+ ALTER TABLE "sessions" ADD CONSTRAINT "sessions_parent_id_sessions_id_fk" FOREIGN KEY ("parent_id") REFERENCES "public"."sessions"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
51
+ CREATE INDEX "idx_archives_session" ON "compaction_archives" USING btree ("session_id");--> statement-breakpoint
52
+ CREATE INDEX "idx_snapshots_session_path" ON "file_snapshots" USING btree ("session_id","file_path");--> statement-breakpoint
53
+ CREATE UNIQUE INDEX "idx_snapshots_latest" ON "file_snapshots" USING btree ("session_id","file_path","version");--> statement-breakpoint
54
+ CREATE INDEX "idx_entries_session" ON "session_entries" USING btree ("session_id","created_at");--> statement-breakpoint
55
+ CREATE INDEX "idx_sessions_parent" ON "sessions" USING btree ("parent_id");
@@ -0,0 +1,13 @@
1
+ CREATE TABLE "projects" (
2
+ "id" text PRIMARY KEY NOT NULL,
3
+ "worktree" text NOT NULL,
4
+ "vcs" text,
5
+ "name" text,
6
+ "git_remote" text,
7
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
8
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
9
+ );
10
+ --> statement-breakpoint
11
+ ALTER TABLE "sessions" ADD COLUMN "project_id" text;--> statement-breakpoint
12
+ ALTER TABLE "sessions" ADD CONSTRAINT "sessions_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
13
+ CREATE INDEX "idx_sessions_project" ON "sessions" USING btree ("project_id");
@@ -0,0 +1,12 @@
1
+ CREATE TABLE "tool_metrics" (
2
+ "model" text NOT NULL,
3
+ "tool" text NOT NULL,
4
+ "mode" text NOT NULL,
5
+ "attempts" integer DEFAULT 0 NOT NULL,
6
+ "successes" integer DEFAULT 0 NOT NULL,
7
+ "failures" integer DEFAULT 0 NOT NULL,
8
+ "avg_latency_ms" real DEFAULT 0 NOT NULL,
9
+ "last_used" timestamp with time zone DEFAULT now() NOT NULL
10
+ );
11
+ --> statement-breakpoint
12
+ CREATE UNIQUE INDEX "uq_tool_metrics_model_tool_mode" ON "tool_metrics" USING btree ("model","tool","mode");
@@ -0,0 +1,2 @@
1
+ ALTER TABLE "sessions" ADD COLUMN "agent_type" text;--> statement-breakpoint
2
+ ALTER TABLE "sessions" ADD COLUMN "worktree_path" text;
@@ -0,0 +1,448 @@
1
+ {
2
+ "id": "50d91e70-a998-4243-a3b3-b60f79e7bbd2",
3
+ "prevId": "00000000-0000-0000-0000-000000000000",
4
+ "version": "7",
5
+ "dialect": "postgresql",
6
+ "tables": {
7
+ "public.compaction_archives": {
8
+ "name": "compaction_archives",
9
+ "schema": "",
10
+ "columns": {
11
+ "id": {
12
+ "name": "id",
13
+ "type": "uuid",
14
+ "primaryKey": true,
15
+ "notNull": true,
16
+ "default": "gen_random_uuid()"
17
+ },
18
+ "session_id": {
19
+ "name": "session_id",
20
+ "type": "uuid",
21
+ "primaryKey": false,
22
+ "notNull": true
23
+ },
24
+ "compaction_id": {
25
+ "name": "compaction_id",
26
+ "type": "uuid",
27
+ "primaryKey": false,
28
+ "notNull": true
29
+ },
30
+ "archive_type": {
31
+ "name": "archive_type",
32
+ "type": "text",
33
+ "primaryKey": false,
34
+ "notNull": true
35
+ },
36
+ "original_entries": {
37
+ "name": "original_entries",
38
+ "type": "jsonb",
39
+ "primaryKey": false,
40
+ "notNull": true
41
+ },
42
+ "file_snapshots": {
43
+ "name": "file_snapshots",
44
+ "type": "jsonb",
45
+ "primaryKey": false,
46
+ "notNull": false,
47
+ "default": "'[]'::jsonb"
48
+ },
49
+ "summary": {
50
+ "name": "summary",
51
+ "type": "text",
52
+ "primaryKey": false,
53
+ "notNull": true
54
+ },
55
+ "token_count": {
56
+ "name": "token_count",
57
+ "type": "integer",
58
+ "primaryKey": false,
59
+ "notNull": false
60
+ },
61
+ "searchable_text": {
62
+ "name": "searchable_text",
63
+ "type": "text",
64
+ "primaryKey": false,
65
+ "notNull": false
66
+ },
67
+ "created_at": {
68
+ "name": "created_at",
69
+ "type": "timestamp with time zone",
70
+ "primaryKey": false,
71
+ "notNull": true,
72
+ "default": "now()"
73
+ }
74
+ },
75
+ "indexes": {
76
+ "idx_archives_session": {
77
+ "name": "idx_archives_session",
78
+ "columns": [
79
+ {
80
+ "expression": "session_id",
81
+ "isExpression": false,
82
+ "asc": true,
83
+ "nulls": "last"
84
+ }
85
+ ],
86
+ "isUnique": false,
87
+ "concurrently": false,
88
+ "method": "btree",
89
+ "with": {}
90
+ }
91
+ },
92
+ "foreignKeys": {
93
+ "compaction_archives_session_id_sessions_id_fk": {
94
+ "name": "compaction_archives_session_id_sessions_id_fk",
95
+ "tableFrom": "compaction_archives",
96
+ "tableTo": "sessions",
97
+ "columnsFrom": [
98
+ "session_id"
99
+ ],
100
+ "columnsTo": [
101
+ "id"
102
+ ],
103
+ "onDelete": "cascade",
104
+ "onUpdate": "no action"
105
+ }
106
+ },
107
+ "compositePrimaryKeys": {},
108
+ "uniqueConstraints": {},
109
+ "policies": {},
110
+ "checkConstraints": {},
111
+ "isRLSEnabled": false
112
+ },
113
+ "public.file_snapshots": {
114
+ "name": "file_snapshots",
115
+ "schema": "",
116
+ "columns": {
117
+ "id": {
118
+ "name": "id",
119
+ "type": "uuid",
120
+ "primaryKey": true,
121
+ "notNull": true,
122
+ "default": "gen_random_uuid()"
123
+ },
124
+ "session_id": {
125
+ "name": "session_id",
126
+ "type": "uuid",
127
+ "primaryKey": false,
128
+ "notNull": true
129
+ },
130
+ "entry_id": {
131
+ "name": "entry_id",
132
+ "type": "uuid",
133
+ "primaryKey": false,
134
+ "notNull": false
135
+ },
136
+ "file_path": {
137
+ "name": "file_path",
138
+ "type": "text",
139
+ "primaryKey": false,
140
+ "notNull": true
141
+ },
142
+ "content": {
143
+ "name": "content",
144
+ "type": "text",
145
+ "primaryKey": false,
146
+ "notNull": true
147
+ },
148
+ "content_hash": {
149
+ "name": "content_hash",
150
+ "type": "text",
151
+ "primaryKey": false,
152
+ "notNull": true
153
+ },
154
+ "token_count": {
155
+ "name": "token_count",
156
+ "type": "integer",
157
+ "primaryKey": false,
158
+ "notNull": false,
159
+ "default": 0
160
+ },
161
+ "version": {
162
+ "name": "version",
163
+ "type": "integer",
164
+ "primaryKey": false,
165
+ "notNull": false,
166
+ "default": 1
167
+ },
168
+ "created_at": {
169
+ "name": "created_at",
170
+ "type": "timestamp with time zone",
171
+ "primaryKey": false,
172
+ "notNull": true,
173
+ "default": "now()"
174
+ }
175
+ },
176
+ "indexes": {
177
+ "idx_snapshots_session_path": {
178
+ "name": "idx_snapshots_session_path",
179
+ "columns": [
180
+ {
181
+ "expression": "session_id",
182
+ "isExpression": false,
183
+ "asc": true,
184
+ "nulls": "last"
185
+ },
186
+ {
187
+ "expression": "file_path",
188
+ "isExpression": false,
189
+ "asc": true,
190
+ "nulls": "last"
191
+ }
192
+ ],
193
+ "isUnique": false,
194
+ "concurrently": false,
195
+ "method": "btree",
196
+ "with": {}
197
+ },
198
+ "idx_snapshots_latest": {
199
+ "name": "idx_snapshots_latest",
200
+ "columns": [
201
+ {
202
+ "expression": "session_id",
203
+ "isExpression": false,
204
+ "asc": true,
205
+ "nulls": "last"
206
+ },
207
+ {
208
+ "expression": "file_path",
209
+ "isExpression": false,
210
+ "asc": true,
211
+ "nulls": "last"
212
+ },
213
+ {
214
+ "expression": "version",
215
+ "isExpression": false,
216
+ "asc": true,
217
+ "nulls": "last"
218
+ }
219
+ ],
220
+ "isUnique": true,
221
+ "concurrently": false,
222
+ "method": "btree",
223
+ "with": {}
224
+ }
225
+ },
226
+ "foreignKeys": {
227
+ "file_snapshots_session_id_sessions_id_fk": {
228
+ "name": "file_snapshots_session_id_sessions_id_fk",
229
+ "tableFrom": "file_snapshots",
230
+ "tableTo": "sessions",
231
+ "columnsFrom": [
232
+ "session_id"
233
+ ],
234
+ "columnsTo": [
235
+ "id"
236
+ ],
237
+ "onDelete": "cascade",
238
+ "onUpdate": "no action"
239
+ }
240
+ },
241
+ "compositePrimaryKeys": {},
242
+ "uniqueConstraints": {},
243
+ "policies": {},
244
+ "checkConstraints": {},
245
+ "isRLSEnabled": false
246
+ },
247
+ "public.session_entries": {
248
+ "name": "session_entries",
249
+ "schema": "",
250
+ "columns": {
251
+ "id": {
252
+ "name": "id",
253
+ "type": "uuid",
254
+ "primaryKey": true,
255
+ "notNull": true,
256
+ "default": "gen_random_uuid()"
257
+ },
258
+ "session_id": {
259
+ "name": "session_id",
260
+ "type": "uuid",
261
+ "primaryKey": false,
262
+ "notNull": true
263
+ },
264
+ "tag": {
265
+ "name": "tag",
266
+ "type": "text",
267
+ "primaryKey": false,
268
+ "notNull": true
269
+ },
270
+ "role": {
271
+ "name": "role",
272
+ "type": "text",
273
+ "primaryKey": false,
274
+ "notNull": false
275
+ },
276
+ "content": {
277
+ "name": "content",
278
+ "type": "jsonb",
279
+ "primaryKey": false,
280
+ "notNull": true
281
+ },
282
+ "tool_name": {
283
+ "name": "tool_name",
284
+ "type": "text",
285
+ "primaryKey": false,
286
+ "notNull": false
287
+ },
288
+ "token_count": {
289
+ "name": "token_count",
290
+ "type": "integer",
291
+ "primaryKey": false,
292
+ "notNull": false,
293
+ "default": 0
294
+ },
295
+ "created_at": {
296
+ "name": "created_at",
297
+ "type": "timestamp with time zone",
298
+ "primaryKey": false,
299
+ "notNull": true,
300
+ "default": "now()"
301
+ }
302
+ },
303
+ "indexes": {
304
+ "idx_entries_session": {
305
+ "name": "idx_entries_session",
306
+ "columns": [
307
+ {
308
+ "expression": "session_id",
309
+ "isExpression": false,
310
+ "asc": true,
311
+ "nulls": "last"
312
+ },
313
+ {
314
+ "expression": "created_at",
315
+ "isExpression": false,
316
+ "asc": true,
317
+ "nulls": "last"
318
+ }
319
+ ],
320
+ "isUnique": false,
321
+ "concurrently": false,
322
+ "method": "btree",
323
+ "with": {}
324
+ }
325
+ },
326
+ "foreignKeys": {
327
+ "session_entries_session_id_sessions_id_fk": {
328
+ "name": "session_entries_session_id_sessions_id_fk",
329
+ "tableFrom": "session_entries",
330
+ "tableTo": "sessions",
331
+ "columnsFrom": [
332
+ "session_id"
333
+ ],
334
+ "columnsTo": [
335
+ "id"
336
+ ],
337
+ "onDelete": "cascade",
338
+ "onUpdate": "no action"
339
+ }
340
+ },
341
+ "compositePrimaryKeys": {},
342
+ "uniqueConstraints": {},
343
+ "policies": {},
344
+ "checkConstraints": {},
345
+ "isRLSEnabled": false
346
+ },
347
+ "public.sessions": {
348
+ "name": "sessions",
349
+ "schema": "",
350
+ "columns": {
351
+ "id": {
352
+ "name": "id",
353
+ "type": "uuid",
354
+ "primaryKey": true,
355
+ "notNull": true,
356
+ "default": "gen_random_uuid()"
357
+ },
358
+ "title": {
359
+ "name": "title",
360
+ "type": "text",
361
+ "primaryKey": false,
362
+ "notNull": true
363
+ },
364
+ "parent_id": {
365
+ "name": "parent_id",
366
+ "type": "uuid",
367
+ "primaryKey": false,
368
+ "notNull": false
369
+ },
370
+ "branch_point": {
371
+ "name": "branch_point",
372
+ "type": "integer",
373
+ "primaryKey": false,
374
+ "notNull": false
375
+ },
376
+ "metadata": {
377
+ "name": "metadata",
378
+ "type": "jsonb",
379
+ "primaryKey": false,
380
+ "notNull": true,
381
+ "default": "'{}'::jsonb"
382
+ },
383
+ "created_at": {
384
+ "name": "created_at",
385
+ "type": "timestamp with time zone",
386
+ "primaryKey": false,
387
+ "notNull": true,
388
+ "default": "now()"
389
+ },
390
+ "updated_at": {
391
+ "name": "updated_at",
392
+ "type": "timestamp with time zone",
393
+ "primaryKey": false,
394
+ "notNull": true,
395
+ "default": "now()"
396
+ }
397
+ },
398
+ "indexes": {
399
+ "idx_sessions_parent": {
400
+ "name": "idx_sessions_parent",
401
+ "columns": [
402
+ {
403
+ "expression": "parent_id",
404
+ "isExpression": false,
405
+ "asc": true,
406
+ "nulls": "last"
407
+ }
408
+ ],
409
+ "isUnique": false,
410
+ "concurrently": false,
411
+ "method": "btree",
412
+ "with": {}
413
+ }
414
+ },
415
+ "foreignKeys": {
416
+ "sessions_parent_id_sessions_id_fk": {
417
+ "name": "sessions_parent_id_sessions_id_fk",
418
+ "tableFrom": "sessions",
419
+ "tableTo": "sessions",
420
+ "columnsFrom": [
421
+ "parent_id"
422
+ ],
423
+ "columnsTo": [
424
+ "id"
425
+ ],
426
+ "onDelete": "no action",
427
+ "onUpdate": "no action"
428
+ }
429
+ },
430
+ "compositePrimaryKeys": {},
431
+ "uniqueConstraints": {},
432
+ "policies": {},
433
+ "checkConstraints": {},
434
+ "isRLSEnabled": false
435
+ }
436
+ },
437
+ "enums": {},
438
+ "schemas": {},
439
+ "sequences": {},
440
+ "roles": {},
441
+ "policies": {},
442
+ "views": {},
443
+ "_meta": {
444
+ "columns": {},
445
+ "schemas": {},
446
+ "tables": {}
447
+ }
448
+ }