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,130 @@
1
+ import { generateId } from '../shared/index.js';
2
+ import { createDAPClient } from './protocol.js';
3
+ // ── 基于已建立 client 的原子操作(薄封装,对齐 DAP command) ──
4
+ async function dapInitialize(client, adapterID) {
5
+ return client.request('initialize', {
6
+ clientID: 'c0de-agent',
7
+ adapterID,
8
+ linesStartAt1: true,
9
+ columnsStartAt1: true,
10
+ pathFormat: 'path',
11
+ });
12
+ }
13
+ async function dapLaunch(client, config) {
14
+ const cmd = config.request === 'attach' ? 'attach' : 'launch';
15
+ await client.request(cmd, {
16
+ program: config.program,
17
+ args: config.args,
18
+ cwd: config.cwd,
19
+ stopOnEntry: false,
20
+ ...config.launchArgs,
21
+ });
22
+ }
23
+ async function dapSetBreakpoints(client, file, bps) {
24
+ return client.request('setBreakpoints', {
25
+ source: { path: file },
26
+ breakpoints: bps.map((b) => ({ line: b.line, condition: b.condition })),
27
+ });
28
+ }
29
+ async function dapContinue(client, threadId) {
30
+ return client.request('continue', { threadId });
31
+ }
32
+ async function dapStep(client, threadId, kind) {
33
+ const command = kind === 'in' ? 'stepIn' : kind === 'out' ? 'stepOut' : 'next';
34
+ return client.request(command, { threadId });
35
+ }
36
+ async function dapStackTrace(client, threadId) {
37
+ const body = (await client.request('stackTrace', { threadId }));
38
+ return body?.stackFrames ?? [];
39
+ }
40
+ /** 按帧取变量:先 scopes 得到 variablesReference,再 variables 汇总。 */
41
+ async function dapVariables(client, frameId) {
42
+ const scopesBody = (await client.request('scopes', { frameId }));
43
+ const refs = scopesBody?.scopes ?? [];
44
+ const out = [];
45
+ for (const s of refs) {
46
+ if (s.variablesReference === undefined)
47
+ continue;
48
+ const vBody = (await client.request('variables', {
49
+ variablesReference: s.variablesReference,
50
+ }));
51
+ out.push(...(vBody?.variables ?? []));
52
+ }
53
+ return out;
54
+ }
55
+ async function dapEvaluate(client, frameId, expression) {
56
+ const body = (await client.request('evaluate', { expression, frameId, context: 'repl' }));
57
+ return body?.result ?? '';
58
+ }
59
+ function createDebugSessionManager() {
60
+ const sessions = new Map();
61
+ const require = (sessionId) => {
62
+ const s = sessions.get(sessionId);
63
+ if (!s)
64
+ throw new Error(`DAP session "${sessionId}" not found`);
65
+ return s;
66
+ };
67
+ return {
68
+ async start(spawn, config) {
69
+ const transport = spawn(config);
70
+ const client = createDAPClient(transport);
71
+ const session = {
72
+ id: generateId(),
73
+ adapter: config.adapter,
74
+ program: config.program,
75
+ state: 'running',
76
+ };
77
+ let lastThread = null;
78
+ client.on('stopped', (body) => {
79
+ session.state = 'paused';
80
+ const tid = body?.threadId;
81
+ if (typeof tid === 'number')
82
+ lastThread = tid;
83
+ });
84
+ client.on('terminated', () => {
85
+ session.state = 'stopped';
86
+ });
87
+ await dapInitialize(client, config.adapter);
88
+ await dapLaunch(client, config);
89
+ sessions.set(session.id, { session, client, config });
90
+ return { sessionId: session.id, threadId: lastThread };
91
+ },
92
+ setBreakpoint(sessionId, bp) {
93
+ return dapSetBreakpoints(require(sessionId).client, bp.file, [bp]);
94
+ },
95
+ continue(sessionId, threadId) {
96
+ return dapContinue(require(sessionId).client, threadId);
97
+ },
98
+ step(sessionId, threadId, kind) {
99
+ return dapStep(require(sessionId).client, threadId, kind);
100
+ },
101
+ stack(sessionId, threadId) {
102
+ return dapStackTrace(require(sessionId).client, threadId);
103
+ },
104
+ variables(sessionId, frameId) {
105
+ return dapVariables(require(sessionId).client, frameId);
106
+ },
107
+ evaluate(sessionId, frameId, expression) {
108
+ return dapEvaluate(require(sessionId).client, frameId, expression);
109
+ },
110
+ async stop(sessionId) {
111
+ const s = sessions.get(sessionId);
112
+ if (!s)
113
+ return;
114
+ s.session.state = 'stopped';
115
+ // disconnect 适配器可能已退出;吞错。
116
+ try {
117
+ await s.client.request('disconnect', {});
118
+ }
119
+ catch {
120
+ /* adapter gone */
121
+ }
122
+ s.client.dispose();
123
+ sessions.delete(sessionId);
124
+ },
125
+ getSession(sessionId) {
126
+ return sessions.get(sessionId)?.session;
127
+ },
128
+ };
129
+ }
130
+ export { createDebugSessionManager, dapVariables };
@@ -0,0 +1,8 @@
1
+ import { type ChildProcessWithoutNullStreams } from 'node:child_process';
2
+ import type { DAPTransport } from './protocol.js';
3
+ /** 启动一个调试适配器子进程,将其 stdin/stdout 包装为 DAPTransport。 */
4
+ declare function createProcessTransport(command: string, args?: string[]): {
5
+ transport: DAPTransport;
6
+ child: ChildProcessWithoutNullStreams;
7
+ };
8
+ export { createProcessTransport };
@@ -0,0 +1,30 @@
1
+ import { spawn } from 'node:child_process';
2
+ /** 启动一个调试适配器子进程,将其 stdin/stdout 包装为 DAPTransport。 */
3
+ function createProcessTransport(command, args = []) {
4
+ const child = spawn(command, args, { stdio: ['pipe', 'pipe', 'pipe'] });
5
+ const dataHandlers = [];
6
+ const closeHandlers = [];
7
+ child.stdout.setEncoding('utf8');
8
+ child.stdout.on('data', (chunk) => {
9
+ for (const h of dataHandlers)
10
+ h(chunk);
11
+ });
12
+ child.on('close', () => {
13
+ for (const h of closeHandlers)
14
+ h();
15
+ });
16
+ const transport = {
17
+ write: (chunk) => {
18
+ if (!child.stdin.destroyed)
19
+ child.stdin.write(chunk);
20
+ },
21
+ onData: (h) => dataHandlers.push(h),
22
+ onClose: (h) => closeHandlers.push(h),
23
+ close: () => {
24
+ if (!child.killed)
25
+ child.kill();
26
+ },
27
+ };
28
+ return { transport, child };
29
+ }
30
+ export { createProcessTransport };
@@ -0,0 +1,50 @@
1
+ /** 启动调试会话的配置。 */
2
+ type DAPConfig = {
3
+ /** 调试适配器标识(如 'node', 'python'),用于 initialize.adapterID。 */
4
+ adapter: string;
5
+ /** 被调试程序路径。 */
6
+ program: string;
7
+ /** attach 或 launch(默认 launch)。 */
8
+ request?: 'launch' | 'attach';
9
+ args?: string[];
10
+ cwd?: string;
11
+ /** 额外的 launch/attach 参数(适配器特定)。 */
12
+ launchArgs?: Record<string, unknown>;
13
+ };
14
+ /** 调试会话句柄。 */
15
+ type DAPSession = {
16
+ id: string;
17
+ adapter: string;
18
+ program: string;
19
+ state: 'running' | 'paused' | 'stopped';
20
+ };
21
+ /** 断点。 */
22
+ type Breakpoint = {
23
+ file: string;
24
+ line: number;
25
+ condition?: string;
26
+ };
27
+ /** 栈帧。 */
28
+ type StackFrame = {
29
+ id: number;
30
+ name: string;
31
+ file: string;
32
+ line: number;
33
+ column?: number;
34
+ };
35
+ /** 变量。 */
36
+ type Variable = {
37
+ name: string;
38
+ value: string;
39
+ type?: string;
40
+ variablesReference?: number;
41
+ };
42
+ /** 工具用的简化配置(spec §21 debug_start 输入)。 */
43
+ type DebugStartInput = {
44
+ adapter: string;
45
+ program: string;
46
+ args?: string[];
47
+ cwd?: string;
48
+ stopOnEntry?: boolean;
49
+ };
50
+ export type { Breakpoint, DAPConfig, DAPSession, DebugStartInput, StackFrame, Variable };
@@ -0,0 +1,2 @@
1
+ // DAP 类型(spec §21)。零依赖的最小子集。
2
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { PgliteDatabase } from 'drizzle-orm/pglite';
2
+ import * as schema from './schema.js';
3
+ import type { DBConfig } from './types.js';
4
+ /**
5
+ * Unified database handle.
6
+ * `db` is the Drizzle ORM instance with schema bound.
7
+ * `close()` cleans up the connection.
8
+ */
9
+ type DB = {
10
+ db: PgliteDatabase<typeof schema>;
11
+ close(): Promise<void>;
12
+ };
13
+ /**
14
+ * Create a database connection.
15
+ *
16
+ * - PGLite mode: in-process WASM Postgres, no server needed.
17
+ * Pass `dataDir` for persistent storage, omit for in-memory (tests).
18
+ * - PostgreSQL mode: connects to a real PG server via connection string.
19
+ */
20
+ declare function createDB(config: DBConfig): Promise<DB>;
21
+ export type { DB };
22
+ export { createDB };
@@ -0,0 +1,39 @@
1
+ import { drizzle as drizzlePg } from 'drizzle-orm/node-postgres';
2
+ import { drizzle } from 'drizzle-orm/pglite';
3
+ import { Pool } from 'pg';
4
+ import * as schema from './schema.js';
5
+ /**
6
+ * Create a database connection.
7
+ *
8
+ * - PGLite mode: in-process WASM Postgres, no server needed.
9
+ * Pass `dataDir` for persistent storage, omit for in-memory (tests).
10
+ * - PostgreSQL mode: connects to a real PG server via connection string.
11
+ */
12
+ async function createDB(config) {
13
+ if (config.driver === 'pglite') {
14
+ const connection = config.dataDir && config.dataDir !== ':memory:' ? { dataDir: config.dataDir } : undefined;
15
+ const db = drizzle({ schema, connection });
16
+ // $client is the underlying PGlite (WASM Postgres) instance drizzle created
17
+ // from the connection option. Without closing it, each instance leaks
18
+ // ~100MB+ of WASM memory — which OOM-kills vitest worker forks when many
19
+ // test files run in the same process.
20
+ const client = db.$client;
21
+ return {
22
+ db,
23
+ async close() {
24
+ await client.close();
25
+ },
26
+ };
27
+ }
28
+ // PostgreSQL mode — cast to PgliteDatabase for a unified interface.
29
+ // The underlying Drizzle ORM query API is identical across drivers.
30
+ const pool = new Pool({ connectionString: config.connectionString });
31
+ const db = drizzlePg(pool, { schema });
32
+ return {
33
+ db,
34
+ async close() {
35
+ await pool.end();
36
+ },
37
+ };
38
+ }
39
+ export { createDB };
@@ -0,0 +1,6 @@
1
+ export type { DB } from './client.js';
2
+ export { createDB } from './client.js';
3
+ export { migrateDB } from './migrate.js';
4
+ export type { CompactionArchiveInsert, CompactionArchiveRow, FileSnapshotInsert, FileSnapshotRow, SessionEntryInsert, SessionEntryRow, SessionInsert, SessionRow, } from './schema.js';
5
+ export { compactionArchives, fileSnapshots, sessionEntries, sessions, } from './schema.js';
6
+ export type { DBConfig, DBDriver } from './types.js';
@@ -0,0 +1,4 @@
1
+ // DB package: Drizzle schema, PGLite/PostgreSQL client, migrations.
2
+ export { createDB } from './client.js';
3
+ export { migrateDB } from './migrate.js';
4
+ export { compactionArchives, fileSnapshots, sessionEntries, sessions, } from './schema.js';
@@ -0,0 +1,9 @@
1
+ import type { DB } from './client.js';
2
+ /**
3
+ * Run all pending database migrations.
4
+ * Migration SQL files live in `drizzle/` at the project root.
5
+ *
6
+ * Must be called after `createDB()` and before any queries.
7
+ */
8
+ declare function migrateDB(handle: DB): Promise<void>;
9
+ export { migrateDB };
@@ -0,0 +1,15 @@
1
+ import { resolve } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { migrate as drizzleMigrate } from 'drizzle-orm/pglite/migrator';
4
+ /**
5
+ * Run all pending database migrations.
6
+ * Migration SQL files live in `drizzle/` at the project root.
7
+ *
8
+ * Must be called after `createDB()` and before any queries.
9
+ */
10
+ async function migrateDB(handle) {
11
+ const currentDir = fileURLToPath(new URL('.', import.meta.url));
12
+ const migrationsFolder = resolve(currentDir, '..', '..', 'drizzle');
13
+ await drizzleMigrate(handle.db, { migrationsFolder });
14
+ }
15
+ export { migrateDB };