bonecode 1.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 (575) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -50
  3. package/bone/output/agent/.dockerignore +7 -7
  4. package/bone/output/agent/.env.example +36 -36
  5. package/bone/output/agent/.github/workflows/ci.yaml +58 -58
  6. package/bone/output/agent/AgentDomain.bone.map +349 -349
  7. package/bone/output/agent/AgentDomain.postman_collection.json +957 -957
  8. package/bone/output/agent/Dockerfile +22 -22
  9. package/bone/output/agent/README.md +47 -47
  10. package/bone/output/agent/admin/index.html +739 -739
  11. package/bone/output/agent/docker-compose.yaml +22 -22
  12. package/bone/output/agent/k8s/deployment.yaml +75 -75
  13. package/bone/output/agent/migrations/agent.sql +36 -36
  14. package/bone/output/agent/migrations/agent_instance.sql +36 -36
  15. package/bone/output/agent/migrations/audit_log.sql +18 -18
  16. package/bone/output/agent/migrations/build_step.sql +34 -34
  17. package/bone/output/agent/migrations/event_outbox.sql +31 -31
  18. package/bone/output/agent/migrations/plan.sql +30 -30
  19. package/bone/output/agent/migrations/task.sql +30 -30
  20. package/bone/output/agent/migrations/tool_call.sql +33 -33
  21. package/bone/output/agent/openapi.yaml +1116 -1116
  22. package/bone/output/agent/package.json +35 -35
  23. package/bone/output/agent/schema.graphql +233 -233
  24. package/bone/output/agent/sdk/client.ts +231 -231
  25. package/bone/output/agent/src/algorithms.ts +2 -2
  26. package/bone/output/agent/src/audit.ts +44 -44
  27. package/bone/output/agent/src/auth.ts +57 -57
  28. package/bone/output/agent/src/cron.ts +12 -12
  29. package/bone/output/agent/src/db.ts +31 -31
  30. package/bone/output/agent/src/debug.ts +66 -66
  31. package/bone/output/agent/src/events.ts +243 -243
  32. package/bone/output/agent/src/extensions.ts +54 -54
  33. package/bone/output/agent/src/failure_rules.ts +322 -322
  34. package/bone/output/agent/src/flows.ts +168 -168
  35. package/bone/output/agent/src/health.ts +43 -43
  36. package/bone/output/agent/src/index.ts +99 -99
  37. package/bone/output/agent/src/logger.ts +69 -66
  38. package/bone/output/agent/src/metrics.ts +75 -75
  39. package/bone/output/agent/src/migrate.ts +351 -351
  40. package/bone/output/agent/src/migration_diff.ts +108 -108
  41. package/bone/output/agent/src/notify.ts +125 -125
  42. package/bone/output/agent/src/routes/plan.ts +91 -91
  43. package/bone/output/agent/src/routes/task.ts +105 -105
  44. package/bone/output/agent/src/routes/tool_call.ts +166 -166
  45. package/bone/output/agent/src/schemas.ts +384 -384
  46. package/bone/output/agent/src/state_machines/agent_instance.ts +24 -24
  47. package/bone/output/agent/src/state_machines/build_step.ts +22 -22
  48. package/bone/output/agent/src/state_machines/plan.ts +22 -22
  49. package/bone/output/agent/src/state_machines/task.ts +22 -22
  50. package/bone/output/agent/src/state_machines/tool_call.ts +22 -22
  51. package/bone/output/agent/src/tests.ts +361 -361
  52. package/bone/output/agent/src/websocket.ts +200 -200
  53. package/bone/output/agent/tsconfig.json +24 -24
  54. package/bone/output/rag/.dockerignore +7 -7
  55. package/bone/output/rag/.env.example +36 -36
  56. package/bone/output/rag/.github/workflows/ci.yaml +58 -58
  57. package/bone/output/rag/Dockerfile +22 -22
  58. package/bone/output/rag/RAGDomain.bone.map +286 -286
  59. package/bone/output/rag/RAGDomain.postman_collection.json +922 -922
  60. package/bone/output/rag/README.md +47 -47
  61. package/bone/output/rag/admin/index.html +817 -817
  62. package/bone/output/rag/docker-compose.yaml +22 -22
  63. package/bone/output/rag/k8s/deployment.yaml +75 -75
  64. package/bone/output/rag/migrations/audit_log.sql +18 -18
  65. package/bone/output/rag/migrations/code_chunk.sql +34 -34
  66. package/bone/output/rag/migrations/code_file.sql +33 -33
  67. package/bone/output/rag/migrations/event_outbox.sql +31 -31
  68. package/bone/output/rag/migrations/indexing_job.sql +33 -33
  69. package/bone/output/rag/migrations/knowledge_base.sql +35 -35
  70. package/bone/output/rag/migrations/memory_entry.sql +34 -34
  71. package/bone/output/rag/openapi.yaml +1097 -1097
  72. package/bone/output/rag/package.json +35 -35
  73. package/bone/output/rag/schema.graphql +245 -245
  74. package/bone/output/rag/sdk/client.ts +234 -234
  75. package/bone/output/rag/src/algorithms.ts +2 -2
  76. package/bone/output/rag/src/audit.ts +37 -37
  77. package/bone/output/rag/src/auth.ts +57 -57
  78. package/bone/output/rag/src/cron.ts +12 -12
  79. package/bone/output/rag/src/db.ts +31 -31
  80. package/bone/output/rag/src/debug.ts +66 -66
  81. package/bone/output/rag/src/events.ts +243 -243
  82. package/bone/output/rag/src/extensions.ts +350 -350
  83. package/bone/output/rag/src/failure_rules.ts +314 -314
  84. package/bone/output/rag/src/flows.ts +239 -239
  85. package/bone/output/rag/src/health.ts +43 -43
  86. package/bone/output/rag/src/index.ts +94 -94
  87. package/bone/output/rag/src/logger.ts +69 -66
  88. package/bone/output/rag/src/metrics.ts +75 -75
  89. package/bone/output/rag/src/migrate.ts +363 -363
  90. package/bone/output/rag/src/migration_diff.ts +108 -108
  91. package/bone/output/rag/src/notify.ts +99 -99
  92. package/bone/output/rag/src/routes/code_chunk.ts +75 -75
  93. package/bone/output/rag/src/routes/code_file.ts +101 -101
  94. package/bone/output/rag/src/routes/indexing_job.ts +87 -87
  95. package/bone/output/rag/src/routes/knowledge_base.ts +230 -230
  96. package/bone/output/rag/src/routes/memory_entry.ts +87 -87
  97. package/bone/output/rag/src/schemas.ts +394 -394
  98. package/bone/output/rag/src/state_machines/code_file.ts +23 -23
  99. package/bone/output/rag/src/state_machines/indexing_job.ts +22 -22
  100. package/bone/output/rag/src/state_machines/knowledge_base.ts +23 -23
  101. package/bone/output/rag/src/state_machines/memory_entry.ts +20 -20
  102. package/bone/output/rag/src/tests.ts +339 -339
  103. package/bone/output/rag/tsconfig.json +24 -24
  104. package/bone/output/session/.dockerignore +7 -7
  105. package/bone/output/session/.env.example +36 -36
  106. package/bone/output/session/.github/workflows/ci.yaml +58 -58
  107. package/bone/output/session/Dockerfile +22 -22
  108. package/bone/output/session/README.md +47 -47
  109. package/bone/output/session/SessionDomain.bone.map +349 -349
  110. package/bone/output/session/SessionDomain.postman_collection.json +957 -957
  111. package/bone/output/session/admin/index.html +666 -666
  112. package/bone/output/session/docker-compose.yaml +22 -22
  113. package/bone/output/session/k8s/deployment.yaml +75 -75
  114. package/bone/output/session/migrations/audit_log.sql +18 -18
  115. package/bone/output/session/migrations/event_outbox.sql +31 -31
  116. package/bone/output/session/migrations/message.sql +31 -31
  117. package/bone/output/session/migrations/part.sql +28 -28
  118. package/bone/output/session/migrations/permission.sql +28 -28
  119. package/bone/output/session/migrations/project.sql +28 -28
  120. package/bone/output/session/migrations/session.sql +38 -38
  121. package/bone/output/session/openapi.yaml +1101 -1101
  122. package/bone/output/session/package.json +35 -35
  123. package/bone/output/session/schema.graphql +222 -222
  124. package/bone/output/session/sdk/client.ts +225 -225
  125. package/bone/output/session/src/algorithms.ts +2 -2
  126. package/bone/output/session/src/audit.ts +44 -44
  127. package/bone/output/session/src/auth.ts +57 -57
  128. package/bone/output/session/src/cron.ts +12 -12
  129. package/bone/output/session/src/db.ts +31 -31
  130. package/bone/output/session/src/debug.ts +66 -66
  131. package/bone/output/session/src/events.ts +270 -270
  132. package/bone/output/session/src/extensions.ts +215 -215
  133. package/bone/output/session/src/failure_rules.ts +283 -283
  134. package/bone/output/session/src/flows.ts +168 -168
  135. package/bone/output/session/src/health.ts +43 -43
  136. package/bone/output/session/src/index.ts +99 -99
  137. package/bone/output/session/src/logger.ts +67 -66
  138. package/bone/output/session/src/metrics.ts +75 -75
  139. package/bone/output/session/src/migrate.ts +331 -331
  140. package/bone/output/session/src/migration_diff.ts +108 -108
  141. package/bone/output/session/src/notify.ts +112 -112
  142. package/bone/output/session/src/routes/message.ts +93 -93
  143. package/bone/output/session/src/routes/part.ts +79 -79
  144. package/bone/output/session/src/routes/permission.ts +79 -79
  145. package/bone/output/session/src/routes/project.ts +79 -79
  146. package/bone/output/session/src/routes/session.ts +294 -294
  147. package/bone/output/session/src/schemas.ts +357 -357
  148. package/bone/output/session/src/state_machines/session.ts +23 -23
  149. package/bone/output/session/src/tests.ts +325 -325
  150. package/bone/output/session/src/websocket.ts +223 -200
  151. package/bone/output/session/tsconfig.json +24 -24
  152. package/bone/output/workspace/.dockerignore +7 -7
  153. package/bone/output/workspace/.env.example +36 -36
  154. package/bone/output/workspace/.github/workflows/ci.yaml +58 -58
  155. package/bone/output/workspace/Dockerfile +22 -22
  156. package/bone/output/workspace/README.md +45 -45
  157. package/bone/output/workspace/WorkspaceDomain.bone.map +188 -188
  158. package/bone/output/workspace/WorkspaceDomain.postman_collection.json +620 -620
  159. package/bone/output/workspace/admin/index.html +484 -484
  160. package/bone/output/workspace/docker-compose.yaml +22 -22
  161. package/bone/output/workspace/k8s/deployment.yaml +75 -75
  162. package/bone/output/workspace/migrations/audit_log.sql +18 -18
  163. package/bone/output/workspace/migrations/codebase.sql +34 -34
  164. package/bone/output/workspace/migrations/event_outbox.sql +31 -31
  165. package/bone/output/workspace/migrations/snapshot.sql +32 -32
  166. package/bone/output/workspace/migrations/workspace.sql +33 -33
  167. package/bone/output/workspace/openapi.yaml +721 -721
  168. package/bone/output/workspace/package.json +35 -35
  169. package/bone/output/workspace/schema.graphql +153 -153
  170. package/bone/output/workspace/sdk/client.ts +155 -155
  171. package/bone/output/workspace/src/algorithms.ts +2 -2
  172. package/bone/output/workspace/src/audit.ts +37 -37
  173. package/bone/output/workspace/src/auth.ts +57 -57
  174. package/bone/output/workspace/src/cron.ts +12 -12
  175. package/bone/output/workspace/src/db.ts +31 -31
  176. package/bone/output/workspace/src/debug.ts +66 -66
  177. package/bone/output/workspace/src/events.ts +243 -243
  178. package/bone/output/workspace/src/extensions.ts +44 -44
  179. package/bone/output/workspace/src/failure_rules.ts +152 -152
  180. package/bone/output/workspace/src/health.ts +43 -43
  181. package/bone/output/workspace/src/index.ts +88 -88
  182. package/bone/output/workspace/src/logger.ts +69 -66
  183. package/bone/output/workspace/src/metrics.ts +75 -75
  184. package/bone/output/workspace/src/migrate.ts +219 -219
  185. package/bone/output/workspace/src/migration_diff.ts +108 -108
  186. package/bone/output/workspace/src/notify.ts +73 -73
  187. package/bone/output/workspace/src/routes/codebase.ts +87 -87
  188. package/bone/output/workspace/src/routes/snapshot.ts +127 -127
  189. package/bone/output/workspace/src/routes/workspace.ts +190 -190
  190. package/bone/output/workspace/src/schemas.ts +231 -231
  191. package/bone/output/workspace/src/state_machines/codebase.ts +21 -21
  192. package/bone/output/workspace/src/state_machines/snapshot.ts +20 -20
  193. package/bone/output/workspace/src/state_machines/workspace.ts +21 -21
  194. package/bone/output/workspace/src/tests.ts +248 -248
  195. package/bone/output/workspace/tsconfig.json +24 -24
  196. package/compat/opencode_adapter.ts +94 -17
  197. package/package.json +15 -2
  198. package/src/cli.ts +66 -107
  199. package/src/db_adapter.ts +354 -0
  200. package/src/engine/account/account.sql.ts +39 -39
  201. package/src/engine/account/account.ts +456 -456
  202. package/src/engine/account/repo.ts +166 -166
  203. package/src/engine/account/schema.ts +99 -99
  204. package/src/engine/account/url.ts +8 -8
  205. package/src/engine/acp/README.md +174 -174
  206. package/src/engine/acp/agent.ts +1968 -1968
  207. package/src/engine/acp/runtime.ts +22 -22
  208. package/src/engine/acp/session.ts +122 -122
  209. package/src/engine/acp/types.ts +24 -24
  210. package/src/engine/agent/agent.ts +463 -463
  211. package/src/engine/agent/generate.txt +75 -75
  212. package/src/engine/agent/prompt/compaction.txt +9 -9
  213. package/src/engine/agent/prompt/explore.txt +18 -18
  214. package/src/engine/agent/prompt/scout.txt +36 -36
  215. package/src/engine/agent/prompt/summary.txt +11 -11
  216. package/src/engine/agent/prompt/title.txt +44 -44
  217. package/src/engine/agent/subagent-permissions.ts +34 -34
  218. package/src/engine/auth/index.ts +96 -96
  219. package/src/engine/background/background/job.ts +200 -200
  220. package/src/engine/background/job.ts +200 -200
  221. package/src/engine/bus/bus-event.ts +45 -45
  222. package/src/engine/bus/global.ts +22 -22
  223. package/src/engine/bus/index.ts +203 -203
  224. package/src/engine/command/command/index.ts +181 -181
  225. package/src/engine/command/command/template/initialize.txt +66 -66
  226. package/src/engine/command/command/template/review.txt +101 -101
  227. package/src/engine/command/index.ts +181 -181
  228. package/src/engine/command/template/initialize.txt +66 -66
  229. package/src/engine/command/template/review.txt +101 -101
  230. package/src/engine/config/agent.ts +172 -172
  231. package/src/engine/config/attachment.ts +25 -25
  232. package/src/engine/config/command.ts +62 -62
  233. package/src/engine/config/config.ts +833 -833
  234. package/src/engine/config/console-state.ts +14 -14
  235. package/src/engine/config/entry-name.ts +16 -16
  236. package/src/engine/config/error.ts +23 -23
  237. package/src/engine/config/formatter.ts +13 -13
  238. package/src/engine/config/layout.ts +6 -6
  239. package/src/engine/config/lsp.ts +43 -43
  240. package/src/engine/config/managed.ts +71 -71
  241. package/src/engine/config/markdown.ts +96 -96
  242. package/src/engine/config/mcp.ts +56 -56
  243. package/src/engine/config/model-id.ts +5 -5
  244. package/src/engine/config/parse.ts +79 -79
  245. package/src/engine/config/paths.ts +45 -45
  246. package/src/engine/config/permission.ts +58 -58
  247. package/src/engine/config/plugin.ts +84 -84
  248. package/src/engine/config/provider.ts +111 -111
  249. package/src/engine/config/reference.ts +23 -23
  250. package/src/engine/config/server.ts +19 -19
  251. package/src/engine/config/skills.ts +14 -14
  252. package/src/engine/config/variable.ts +90 -90
  253. package/src/engine/control-plane/adapters/index.ts +41 -41
  254. package/src/engine/control-plane/adapters/worktree.ts +96 -96
  255. package/src/engine/control-plane/dev/README.md +19 -19
  256. package/src/engine/control-plane/dev/debug-workspace-plugin.ts +73 -73
  257. package/src/engine/control-plane/schema.ts +14 -14
  258. package/src/engine/control-plane/types.ts +59 -59
  259. package/src/engine/control-plane/util.ts +39 -39
  260. package/src/engine/control-plane/workspace-adapter-runtime.ts +51 -51
  261. package/src/engine/control-plane/workspace-context.ts +26 -26
  262. package/src/engine/control-plane/workspace.sql.ts +20 -20
  263. package/src/engine/control-plane/workspace.ts +1072 -1072
  264. package/src/engine/data-migration.ts +161 -161
  265. package/src/engine/effect/app-runtime.ts +143 -143
  266. package/src/engine/effect/bootstrap-runtime.ts +29 -29
  267. package/src/engine/effect/bridge.ts +84 -84
  268. package/src/engine/effect/config-service.ts +67 -67
  269. package/src/engine/effect/instance-ref.ts +11 -11
  270. package/src/engine/effect/instance-registry.ts +12 -12
  271. package/src/engine/effect/instance-state.ts +72 -72
  272. package/src/engine/effect/promise.ts +17 -17
  273. package/src/engine/effect/run-service.ts +47 -47
  274. package/src/engine/effect/runner.ts +217 -217
  275. package/src/engine/effect/runtime-flags.ts +74 -74
  276. package/src/engine/effect/service-use.ts +38 -38
  277. package/src/engine/env/index.ts +37 -37
  278. package/src/engine/event-v2-bridge.ts +89 -89
  279. package/src/engine/file/file/ignore.ts +81 -81
  280. package/src/engine/file/file/index.ts +651 -651
  281. package/src/engine/file/file/protected.ts +59 -59
  282. package/src/engine/file/file/ripgrep.ts +481 -481
  283. package/src/engine/file/file/watcher.ts +167 -167
  284. package/src/engine/file/ignore.ts +81 -81
  285. package/src/engine/file/index.ts +651 -651
  286. package/src/engine/file/protected.ts +59 -59
  287. package/src/engine/file/ripgrep.ts +481 -481
  288. package/src/engine/file/watcher.ts +167 -167
  289. package/src/engine/format/format/formatter.ts +404 -404
  290. package/src/engine/format/format/index.ts +209 -209
  291. package/src/engine/format/formatter.ts +404 -404
  292. package/src/engine/format/index.ts +209 -209
  293. package/src/engine/git/git/index.ts +347 -347
  294. package/src/engine/git/index.ts +347 -347
  295. package/src/engine/id/id.ts +80 -80
  296. package/src/engine/ide/index.ts +70 -70
  297. package/src/engine/image/image/image.ts +176 -176
  298. package/src/engine/image/image.ts +176 -176
  299. package/src/engine/index.ts +251 -251
  300. package/src/engine/installation/index.ts +327 -327
  301. package/src/engine/lsp/client.ts +707 -707
  302. package/src/engine/lsp/diagnostic.ts +29 -29
  303. package/src/engine/lsp/language.ts +121 -121
  304. package/src/engine/lsp/launch.ts +21 -21
  305. package/src/engine/lsp/lsp/client.ts +707 -707
  306. package/src/engine/lsp/lsp/diagnostic.ts +29 -29
  307. package/src/engine/lsp/lsp/language.ts +121 -121
  308. package/src/engine/lsp/lsp/launch.ts +21 -21
  309. package/src/engine/lsp/lsp/lsp.ts +507 -507
  310. package/src/engine/lsp/lsp/server.ts +2064 -2064
  311. package/src/engine/lsp/lsp.ts +507 -507
  312. package/src/engine/lsp/server.ts +2064 -2064
  313. package/src/engine/mcp/auth.ts +146 -146
  314. package/src/engine/mcp/index.ts +958 -958
  315. package/src/engine/mcp/mcp/auth.ts +146 -146
  316. package/src/engine/mcp/mcp/index.ts +958 -958
  317. package/src/engine/mcp/mcp/oauth-callback.ts +232 -232
  318. package/src/engine/mcp/mcp/oauth-provider.ts +214 -214
  319. package/src/engine/mcp/oauth-callback.ts +232 -232
  320. package/src/engine/mcp/oauth-provider.ts +214 -214
  321. package/src/engine/node.ts +6 -6
  322. package/src/engine/patch/index.ts +689 -689
  323. package/src/engine/patch/patch/index.ts +689 -689
  324. package/src/engine/permission/arity.ts +163 -163
  325. package/src/engine/permission/evaluate.ts +15 -15
  326. package/src/engine/permission/index.ts +306 -306
  327. package/src/engine/permission/permission/arity.ts +163 -163
  328. package/src/engine/permission/permission/evaluate.ts +15 -15
  329. package/src/engine/permission/permission/index.ts +306 -306
  330. package/src/engine/permission/permission/schema.ts +13 -13
  331. package/src/engine/permission/schema.ts +13 -13
  332. package/src/engine/plugin/azure.ts +26 -26
  333. package/src/engine/plugin/cloudflare.ts +76 -76
  334. package/src/engine/plugin/codex.ts +622 -622
  335. package/src/engine/plugin/digitalocean.ts +411 -411
  336. package/src/engine/plugin/github-copilot/copilot.ts +394 -394
  337. package/src/engine/plugin/github-copilot/models.ts +196 -196
  338. package/src/engine/plugin/index.ts +295 -295
  339. package/src/engine/plugin/install.ts +439 -439
  340. package/src/engine/plugin/loader.ts +216 -216
  341. package/src/engine/plugin/meta.ts +188 -188
  342. package/src/engine/plugin/shared.ts +323 -323
  343. package/src/engine/project/bootstrap-service.ts +9 -9
  344. package/src/engine/project/bootstrap.ts +75 -75
  345. package/src/engine/project/instance-context.ts +24 -24
  346. package/src/engine/project/instance-layer.ts +11 -11
  347. package/src/engine/project/instance-runtime.ts +16 -16
  348. package/src/engine/project/instance-store.ts +193 -193
  349. package/src/engine/project/project.sql.ts +17 -17
  350. package/src/engine/project/project.ts +537 -537
  351. package/src/engine/project/schema.ts +13 -13
  352. package/src/engine/project/vcs.ts +405 -405
  353. package/src/engine/provider/auth.ts +225 -225
  354. package/src/engine/provider/error.ts +204 -204
  355. package/src/engine/provider/model-status.ts +8 -8
  356. package/src/engine/provider/provider.ts +1843 -1843
  357. package/src/engine/provider/schema.ts +30 -30
  358. package/src/engine/provider/transform.ts +1376 -1376
  359. package/src/engine/pty/index.ts +365 -365
  360. package/src/engine/pty/input.ts +24 -24
  361. package/src/engine/pty/pty/index.ts +365 -365
  362. package/src/engine/pty/pty/input.ts +24 -24
  363. package/src/engine/pty/pty/pty.bun.ts +26 -26
  364. package/src/engine/pty/pty/pty.node.ts +27 -27
  365. package/src/engine/pty/pty/pty.ts +25 -25
  366. package/src/engine/pty/pty/schema.ts +14 -14
  367. package/src/engine/pty/pty/ticket.ts +68 -68
  368. package/src/engine/pty/pty.bun.ts +26 -26
  369. package/src/engine/pty/pty.node.ts +27 -27
  370. package/src/engine/pty/pty.ts +25 -25
  371. package/src/engine/pty/schema.ts +14 -14
  372. package/src/engine/pty/ticket.ts +68 -68
  373. package/src/engine/question/index.ts +213 -213
  374. package/src/engine/question/question/index.ts +213 -213
  375. package/src/engine/question/question/schema.ts +10 -10
  376. package/src/engine/question/schema.ts +10 -10
  377. package/src/engine/reference/reference/reference.ts +241 -241
  378. package/src/engine/reference/reference/repository-cache.ts +147 -147
  379. package/src/engine/reference/reference.ts +241 -241
  380. package/src/engine/reference/repository-cache.ts +147 -147
  381. package/src/engine/session/compaction.ts +651 -651
  382. package/src/engine/session/instruction.ts +238 -238
  383. package/src/engine/session/llm.ts +459 -459
  384. package/src/engine/session/message-error.ts +14 -14
  385. package/src/engine/session/message-v2.ts +1202 -1202
  386. package/src/engine/session/message.ts +146 -146
  387. package/src/engine/session/overflow.ts +32 -32
  388. package/src/engine/session/processor.ts +823 -823
  389. package/src/engine/session/prompt/anthropic.txt +105 -105
  390. package/src/engine/session/prompt/beast.txt +147 -147
  391. package/src/engine/session/prompt/build-switch.txt +5 -5
  392. package/src/engine/session/prompt/codex.txt +79 -79
  393. package/src/engine/session/prompt/copilot-gpt-5.txt +143 -143
  394. package/src/engine/session/prompt/default.txt +105 -105
  395. package/src/engine/session/prompt/gemini.txt +155 -155
  396. package/src/engine/session/prompt/gpt.txt +107 -107
  397. package/src/engine/session/prompt/kimi.txt +95 -95
  398. package/src/engine/session/prompt/max-steps.txt +15 -15
  399. package/src/engine/session/prompt/plan-reminder-anthropic.txt +67 -67
  400. package/src/engine/session/prompt/plan.txt +26 -26
  401. package/src/engine/session/prompt/trinity.txt +97 -97
  402. package/src/engine/session/prompt.ts +66 -9
  403. package/src/engine/session/retry.ts +200 -200
  404. package/src/engine/session/revert.ts +162 -162
  405. package/src/engine/session/run-state.ts +153 -153
  406. package/src/engine/session/schema.ts +26 -26
  407. package/src/engine/session/session.sql.ts +137 -137
  408. package/src/engine/session/session.ts +1011 -1011
  409. package/src/engine/session/status.ts +94 -94
  410. package/src/engine/session/summary.ts +164 -164
  411. package/src/engine/session/system.ts +84 -84
  412. package/src/engine/session/todo.ts +81 -81
  413. package/src/engine/share/session.ts +61 -61
  414. package/src/engine/share/share-next.ts +376 -376
  415. package/src/engine/share/share.sql.ts +13 -13
  416. package/src/engine/shell/shell/shell.ts +215 -215
  417. package/src/engine/shell/shell.ts +215 -215
  418. package/src/engine/skill/discovery.ts +116 -116
  419. package/src/engine/skill/index.ts +336 -336
  420. package/src/engine/skill/prompt/customize-opencode.md +377 -377
  421. package/src/engine/skill/skill/discovery.ts +116 -116
  422. package/src/engine/skill/skill/index.ts +336 -336
  423. package/src/engine/skill/skill/prompt/customize-opencode.md +377 -377
  424. package/src/engine/snapshot/index.ts +762 -762
  425. package/src/engine/snapshot/snapshot/index.ts +762 -762
  426. package/src/engine/sync/README.md +179 -179
  427. package/src/engine/sync/event.sql.ts +17 -17
  428. package/src/engine/sync/index.ts +410 -410
  429. package/src/engine/sync/schema.ts +11 -11
  430. package/src/engine/temporary.ts +33 -33
  431. package/src/engine/tool/apply_patch.ts +313 -313
  432. package/src/engine/tool/apply_patch.txt +33 -33
  433. package/src/engine/tool/edit.ts +711 -711
  434. package/src/engine/tool/edit.txt +10 -10
  435. package/src/engine/tool/external-directory.ts +49 -49
  436. package/src/engine/tool/glob.ts +103 -103
  437. package/src/engine/tool/glob.txt +6 -6
  438. package/src/engine/tool/grep.ts +156 -156
  439. package/src/engine/tool/grep.txt +8 -8
  440. package/src/engine/tool/invalid.ts +21 -21
  441. package/src/engine/tool/json-schema.ts +164 -164
  442. package/src/engine/tool/lsp.ts +113 -113
  443. package/src/engine/tool/lsp.txt +24 -24
  444. package/src/engine/tool/mcp-websearch.ts +96 -96
  445. package/src/engine/tool/plan-enter.txt +14 -14
  446. package/src/engine/tool/plan-exit.txt +13 -13
  447. package/src/engine/tool/plan.ts +78 -78
  448. package/src/engine/tool/question.ts +44 -44
  449. package/src/engine/tool/question.txt +10 -10
  450. package/src/engine/tool/read.ts +337 -337
  451. package/src/engine/tool/read.txt +14 -14
  452. package/src/engine/tool/registry.ts +472 -472
  453. package/src/engine/tool/repo_clone.ts +80 -80
  454. package/src/engine/tool/repo_clone.txt +5 -5
  455. package/src/engine/tool/repo_overview.ts +279 -279
  456. package/src/engine/tool/repo_overview.txt +4 -4
  457. package/src/engine/tool/schema.ts +14 -14
  458. package/src/engine/tool/shell/id.ts +19 -19
  459. package/src/engine/tool/shell/prompt.ts +295 -295
  460. package/src/engine/tool/shell/shell.txt +77 -77
  461. package/src/engine/tool/shell.ts +647 -647
  462. package/src/engine/tool/skill.ts +75 -75
  463. package/src/engine/tool/skill.txt +5 -5
  464. package/src/engine/tool/task.ts +337 -337
  465. package/src/engine/tool/task.txt +58 -58
  466. package/src/engine/tool/task_status.ts +179 -179
  467. package/src/engine/tool/task_status.txt +13 -13
  468. package/src/engine/tool/todo.ts +57 -57
  469. package/src/engine/tool/todowrite.txt +167 -167
  470. package/src/engine/tool/tool/apply_patch.ts +313 -313
  471. package/src/engine/tool/tool/apply_patch.txt +33 -33
  472. package/src/engine/tool/tool/edit.ts +711 -711
  473. package/src/engine/tool/tool/edit.txt +10 -10
  474. package/src/engine/tool/tool/external-directory.ts +49 -49
  475. package/src/engine/tool/tool/glob.ts +103 -103
  476. package/src/engine/tool/tool/glob.txt +6 -6
  477. package/src/engine/tool/tool/grep.ts +156 -156
  478. package/src/engine/tool/tool/grep.txt +8 -8
  479. package/src/engine/tool/tool/invalid.ts +21 -21
  480. package/src/engine/tool/tool/json-schema.ts +164 -164
  481. package/src/engine/tool/tool/lsp.ts +113 -113
  482. package/src/engine/tool/tool/lsp.txt +24 -24
  483. package/src/engine/tool/tool/mcp-websearch.ts +96 -96
  484. package/src/engine/tool/tool/plan-enter.txt +14 -14
  485. package/src/engine/tool/tool/plan-exit.txt +13 -13
  486. package/src/engine/tool/tool/plan.ts +78 -78
  487. package/src/engine/tool/tool/question.ts +44 -44
  488. package/src/engine/tool/tool/question.txt +10 -10
  489. package/src/engine/tool/tool/read.ts +337 -337
  490. package/src/engine/tool/tool/read.txt +14 -14
  491. package/src/engine/tool/tool/registry.ts +472 -472
  492. package/src/engine/tool/tool/repo_clone.ts +80 -80
  493. package/src/engine/tool/tool/repo_clone.txt +5 -5
  494. package/src/engine/tool/tool/repo_overview.ts +279 -279
  495. package/src/engine/tool/tool/repo_overview.txt +4 -4
  496. package/src/engine/tool/tool/schema.ts +14 -14
  497. package/src/engine/tool/tool/shell/id.ts +19 -19
  498. package/src/engine/tool/tool/shell/prompt.ts +295 -295
  499. package/src/engine/tool/tool/shell/shell.txt +77 -77
  500. package/src/engine/tool/tool/shell.ts +647 -647
  501. package/src/engine/tool/tool/skill.ts +75 -75
  502. package/src/engine/tool/tool/skill.txt +5 -5
  503. package/src/engine/tool/tool/task.ts +337 -337
  504. package/src/engine/tool/tool/task.txt +58 -58
  505. package/src/engine/tool/tool/task_status.ts +179 -179
  506. package/src/engine/tool/tool/task_status.txt +13 -13
  507. package/src/engine/tool/tool/todo.ts +57 -57
  508. package/src/engine/tool/tool/todowrite.txt +167 -167
  509. package/src/engine/tool/tool/tool.ts +164 -164
  510. package/src/engine/tool/tool/truncate.ts +160 -160
  511. package/src/engine/tool/tool/truncation-dir.ts +4 -4
  512. package/src/engine/tool/tool/webfetch.ts +192 -192
  513. package/src/engine/tool/tool/webfetch.txt +13 -13
  514. package/src/engine/tool/tool/websearch.ts +143 -143
  515. package/src/engine/tool/tool/websearch.txt +14 -14
  516. package/src/engine/tool/tool/write.ts +104 -104
  517. package/src/engine/tool/tool/write.txt +8 -8
  518. package/src/engine/tool/tool.ts +164 -164
  519. package/src/engine/tool/truncate.ts +160 -160
  520. package/src/engine/tool/truncation-dir.ts +4 -4
  521. package/src/engine/tool/webfetch.ts +192 -192
  522. package/src/engine/tool/webfetch.txt +13 -13
  523. package/src/engine/tool/websearch.ts +143 -143
  524. package/src/engine/tool/websearch.txt +14 -14
  525. package/src/engine/tool/write.ts +104 -104
  526. package/src/engine/tool/write.txt +8 -8
  527. package/src/engine/util/archive.ts +17 -17
  528. package/src/engine/util/bom.ts +31 -31
  529. package/src/engine/util/data-url.ts +9 -9
  530. package/src/engine/util/defer.ts +10 -10
  531. package/src/engine/util/effect-http-client.ts +11 -11
  532. package/src/engine/util/error.ts +88 -88
  533. package/src/engine/util/filesystem.ts +252 -252
  534. package/src/engine/util/format.ts +20 -20
  535. package/src/engine/util/iife.ts +3 -3
  536. package/src/engine/util/lazy.ts +20 -20
  537. package/src/engine/util/local-context.ts +25 -25
  538. package/src/engine/util/locale.ts +86 -86
  539. package/src/engine/util/media.ts +26 -26
  540. package/src/engine/util/process.ts +176 -176
  541. package/src/engine/util/queue.ts +32 -32
  542. package/src/engine/util/record.ts +3 -3
  543. package/src/engine/util/repository.ts +158 -158
  544. package/src/engine/util/rpc.ts +66 -66
  545. package/src/engine/util/signal.ts +12 -12
  546. package/src/engine/util/timeout.ts +13 -13
  547. package/src/engine/util/token.ts +7 -7
  548. package/src/engine/util/util/archive.ts +17 -17
  549. package/src/engine/util/util/bom.ts +31 -31
  550. package/src/engine/util/util/data-url.ts +9 -9
  551. package/src/engine/util/util/defer.ts +10 -10
  552. package/src/engine/util/util/effect-http-client.ts +11 -11
  553. package/src/engine/util/util/error.ts +88 -88
  554. package/src/engine/util/util/filesystem.ts +252 -252
  555. package/src/engine/util/util/format.ts +20 -20
  556. package/src/engine/util/util/iife.ts +3 -3
  557. package/src/engine/util/util/lazy.ts +20 -20
  558. package/src/engine/util/util/local-context.ts +25 -25
  559. package/src/engine/util/util/locale.ts +86 -86
  560. package/src/engine/util/util/media.ts +26 -26
  561. package/src/engine/util/util/process.ts +176 -176
  562. package/src/engine/util/util/queue.ts +32 -32
  563. package/src/engine/util/util/record.ts +3 -3
  564. package/src/engine/util/util/repository.ts +158 -158
  565. package/src/engine/util/util/rpc.ts +66 -66
  566. package/src/engine/util/util/signal.ts +12 -12
  567. package/src/engine/util/util/timeout.ts +13 -13
  568. package/src/engine/util/util/token.ts +7 -7
  569. package/src/engine/util/util/which.ts +14 -14
  570. package/src/engine/util/util/wildcard.ts +59 -59
  571. package/src/engine/util/which.ts +14 -14
  572. package/src/engine/util/wildcard.ts +59 -59
  573. package/src/engine/worktree/index.ts +621 -621
  574. package/src/server.ts +121 -158
  575. package/src/tui.ts +485 -502
@@ -1,1202 +1,1202 @@
1
- import { BusEvent } from "@/bus/bus-event"
2
- import { SessionID, MessageID, PartID } from "./schema"
3
- import { NamedError } from "@opencode-ai/core/util/error"
4
- import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
5
- import { LSP } from "@/lsp/lsp"
6
- import { Snapshot } from "@/snapshot"
7
- import { SyncEvent } from "../sync"
8
- import { Database } from "@/storage/db"
9
- import { NotFoundError } from "@/storage/storage"
10
- import { and } from "drizzle-orm"
11
- import { desc } from "drizzle-orm"
12
- import { eq } from "drizzle-orm"
13
- import { inArray } from "drizzle-orm"
14
- import { lt } from "drizzle-orm"
15
- import { or } from "drizzle-orm"
16
- import { MessageTable, PartTable, SessionTable } from "./session.sql"
17
- import * as ProviderError from "@/provider/error"
18
- import { iife } from "@/util/iife"
19
- import { errorMessage } from "@/util/error"
20
- import { isMedia } from "@/util/media"
21
- import type { SystemError } from "bun"
22
- import type { Provider } from "@/provider/provider"
23
- import { ModelID, ProviderID } from "@/provider/schema"
24
- import { Effect, Schema, Types } from "effect"
25
- import { NonNegativeInt } from "@opencode-ai/core/schema"
26
- import * as EffectLogger from "@opencode-ai/core/effect/logger"
27
- import { MessageError } from "./message-error"
28
- import { AuthError, OutputLengthError } from "./message-error"
29
- export { AuthError, OutputLengthError } from "./message-error"
30
-
31
- /** Error shape thrown by Bun's fetch() when gzip/br decompression fails mid-stream */
32
- interface FetchDecompressionError extends Error {
33
- code: "ZlibError"
34
- errno: number
35
- path: string
36
- }
37
-
38
- export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached media from tool result:"
39
- export { isMedia }
40
-
41
- export const AbortedError = NamedError.create("MessageAbortedError", { message: Schema.String })
42
- export const StructuredOutputError = NamedError.create("StructuredOutputError", {
43
- message: Schema.String,
44
- retries: NonNegativeInt,
45
- })
46
- export const APIError = NamedError.create("APIError", {
47
- message: Schema.String,
48
- statusCode: Schema.optional(NonNegativeInt),
49
- isRetryable: Schema.Boolean,
50
- responseHeaders: Schema.optional(Schema.Record(Schema.String, Schema.String)),
51
- responseBody: Schema.optional(Schema.String),
52
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
53
- })
54
- export type APIError = Schema.Schema.Type<typeof APIError.Schema>
55
- export const ContextOverflowError = NamedError.create("ContextOverflowError", {
56
- message: Schema.String,
57
- responseBody: Schema.optional(Schema.String),
58
- })
59
-
60
- export class OutputFormatText extends Schema.Class<OutputFormatText>("OutputFormatText")({
61
- type: Schema.Literal("text"),
62
- }) {}
63
-
64
- export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
65
- type: Schema.Literal("json_schema"),
66
- schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
67
- retryCount: NonNegativeInt.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
68
- }) {}
69
-
70
- export const Format = Schema.Union([OutputFormatText, OutputFormatJsonSchema]).annotate({
71
- discriminator: "type",
72
- identifier: "OutputFormat",
73
- })
74
- export type OutputFormat = Schema.Schema.Type<typeof Format>
75
-
76
- const partBase = {
77
- id: PartID,
78
- sessionID: SessionID,
79
- messageID: MessageID,
80
- }
81
-
82
- export const SnapshotPart = Schema.Struct({
83
- ...partBase,
84
- type: Schema.Literal("snapshot"),
85
- snapshot: Schema.String,
86
- }).annotate({ identifier: "SnapshotPart" })
87
- export type SnapshotPart = Types.DeepMutable<Schema.Schema.Type<typeof SnapshotPart>>
88
-
89
- export const PatchPart = Schema.Struct({
90
- ...partBase,
91
- type: Schema.Literal("patch"),
92
- hash: Schema.String,
93
- files: Schema.Array(Schema.String),
94
- }).annotate({ identifier: "PatchPart" })
95
- export type PatchPart = Types.DeepMutable<Schema.Schema.Type<typeof PatchPart>>
96
-
97
- export const TextPart = Schema.Struct({
98
- ...partBase,
99
- type: Schema.Literal("text"),
100
- text: Schema.String,
101
- synthetic: Schema.optional(Schema.Boolean),
102
- ignored: Schema.optional(Schema.Boolean),
103
- time: Schema.optional(
104
- Schema.Struct({
105
- start: NonNegativeInt,
106
- end: Schema.optional(NonNegativeInt),
107
- }),
108
- ),
109
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
110
- }).annotate({ identifier: "TextPart" })
111
- export type TextPart = Types.DeepMutable<Schema.Schema.Type<typeof TextPart>>
112
-
113
- export const ReasoningPart = Schema.Struct({
114
- ...partBase,
115
- type: Schema.Literal("reasoning"),
116
- text: Schema.String,
117
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
118
- time: Schema.Struct({
119
- start: NonNegativeInt,
120
- end: Schema.optional(NonNegativeInt),
121
- }),
122
- }).annotate({ identifier: "ReasoningPart" })
123
- export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof ReasoningPart>>
124
-
125
- const filePartSourceBase = {
126
- text: Schema.Struct({
127
- value: Schema.String,
128
- start: Schema.Finite,
129
- end: Schema.Finite,
130
- }).annotate({ identifier: "FilePartSourceText" }),
131
- }
132
-
133
- export const FileSource = Schema.Struct({
134
- ...filePartSourceBase,
135
- type: Schema.Literal("file"),
136
- path: Schema.String,
137
- }).annotate({ identifier: "FileSource" })
138
-
139
- export const SymbolSource = Schema.Struct({
140
- ...filePartSourceBase,
141
- type: Schema.Literal("symbol"),
142
- path: Schema.String,
143
- range: LSP.Range,
144
- name: Schema.String,
145
- kind: NonNegativeInt,
146
- }).annotate({ identifier: "SymbolSource" })
147
-
148
- export const ResourceSource = Schema.Struct({
149
- ...filePartSourceBase,
150
- type: Schema.Literal("resource"),
151
- clientName: Schema.String,
152
- uri: Schema.String,
153
- }).annotate({ identifier: "ResourceSource" })
154
-
155
- export const FilePartSource = Schema.Union([FileSource, SymbolSource, ResourceSource]).annotate({
156
- discriminator: "type",
157
- identifier: "FilePartSource",
158
- })
159
-
160
- export const FilePart = Schema.Struct({
161
- ...partBase,
162
- type: Schema.Literal("file"),
163
- mime: Schema.String,
164
- filename: Schema.optional(Schema.String),
165
- url: Schema.String,
166
- source: Schema.optional(FilePartSource),
167
- }).annotate({ identifier: "FilePart" })
168
- export type FilePart = Types.DeepMutable<Schema.Schema.Type<typeof FilePart>>
169
-
170
- export const AgentPart = Schema.Struct({
171
- ...partBase,
172
- type: Schema.Literal("agent"),
173
- name: Schema.String,
174
- source: Schema.optional(
175
- Schema.Struct({
176
- value: Schema.String,
177
- start: NonNegativeInt,
178
- end: NonNegativeInt,
179
- }),
180
- ),
181
- }).annotate({ identifier: "AgentPart" })
182
- export type AgentPart = Types.DeepMutable<Schema.Schema.Type<typeof AgentPart>>
183
-
184
- export const CompactionPart = Schema.Struct({
185
- ...partBase,
186
- type: Schema.Literal("compaction"),
187
- auto: Schema.Boolean,
188
- overflow: Schema.optional(Schema.Boolean),
189
- tail_start_id: Schema.optional(MessageID),
190
- }).annotate({ identifier: "CompactionPart" })
191
- export type CompactionPart = Types.DeepMutable<Schema.Schema.Type<typeof CompactionPart>>
192
-
193
- export const SubtaskPart = Schema.Struct({
194
- ...partBase,
195
- type: Schema.Literal("subtask"),
196
- prompt: Schema.String,
197
- description: Schema.String,
198
- agent: Schema.String,
199
- model: Schema.optional(
200
- Schema.Struct({
201
- providerID: ProviderID,
202
- modelID: ModelID,
203
- }),
204
- ),
205
- command: Schema.optional(Schema.String),
206
- }).annotate({ identifier: "SubtaskPart" })
207
- export type SubtaskPart = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPart>>
208
-
209
- export const RetryPart = Schema.Struct({
210
- ...partBase,
211
- type: Schema.Literal("retry"),
212
- attempt: NonNegativeInt,
213
- error: APIError.EffectSchema,
214
- time: Schema.Struct({
215
- created: NonNegativeInt,
216
- }),
217
- }).annotate({ identifier: "RetryPart" })
218
- export type RetryPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof RetryPart>>, "error"> & {
219
- error: APIError
220
- }
221
-
222
- export const StepStartPart = Schema.Struct({
223
- ...partBase,
224
- type: Schema.Literal("step-start"),
225
- snapshot: Schema.optional(Schema.String),
226
- }).annotate({ identifier: "StepStartPart" })
227
- export type StepStartPart = Types.DeepMutable<Schema.Schema.Type<typeof StepStartPart>>
228
-
229
- export const StepFinishPart = Schema.Struct({
230
- ...partBase,
231
- type: Schema.Literal("step-finish"),
232
- reason: Schema.String,
233
- snapshot: Schema.optional(Schema.String),
234
- cost: Schema.Finite,
235
- tokens: Schema.Struct({
236
- total: Schema.optional(Schema.Finite),
237
- input: Schema.Finite,
238
- output: Schema.Finite,
239
- reasoning: Schema.Finite,
240
- cache: Schema.Struct({
241
- read: Schema.Finite,
242
- write: Schema.Finite,
243
- }),
244
- }),
245
- }).annotate({ identifier: "StepFinishPart" })
246
- export type StepFinishPart = Types.DeepMutable<Schema.Schema.Type<typeof StepFinishPart>>
247
-
248
- export const ToolStatePending = Schema.Struct({
249
- status: Schema.Literal("pending"),
250
- input: Schema.Record(Schema.String, Schema.Any),
251
- raw: Schema.String,
252
- }).annotate({ identifier: "ToolStatePending" })
253
- export type ToolStatePending = Types.DeepMutable<Schema.Schema.Type<typeof ToolStatePending>>
254
-
255
- export const ToolStateRunning = Schema.Struct({
256
- status: Schema.Literal("running"),
257
- input: Schema.Record(Schema.String, Schema.Any),
258
- title: Schema.optional(Schema.String),
259
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
260
- time: Schema.Struct({
261
- start: NonNegativeInt,
262
- }),
263
- }).annotate({ identifier: "ToolStateRunning" })
264
- export type ToolStateRunning = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateRunning>>
265
-
266
- export const ToolStateCompleted = Schema.Struct({
267
- status: Schema.Literal("completed"),
268
- input: Schema.Record(Schema.String, Schema.Any),
269
- output: Schema.String,
270
- title: Schema.String,
271
- metadata: Schema.Record(Schema.String, Schema.Any),
272
- time: Schema.Struct({
273
- start: NonNegativeInt,
274
- end: NonNegativeInt,
275
- compacted: Schema.optional(NonNegativeInt),
276
- }),
277
- attachments: Schema.optional(Schema.Array(FilePart)),
278
- }).annotate({ identifier: "ToolStateCompleted" })
279
- export type ToolStateCompleted = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateCompleted>>
280
-
281
- function truncateToolOutput(text: string, maxChars?: number) {
282
- if (!maxChars || text.length <= maxChars) return text
283
- const omitted = text.length - maxChars
284
- return `${text.slice(0, maxChars)}\n[Tool output truncated for compaction: omitted ${omitted} chars]`
285
- }
286
-
287
- export const ToolStateError = Schema.Struct({
288
- status: Schema.Literal("error"),
289
- input: Schema.Record(Schema.String, Schema.Any),
290
- error: Schema.String,
291
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
292
- time: Schema.Struct({
293
- start: NonNegativeInt,
294
- end: NonNegativeInt,
295
- }),
296
- }).annotate({ identifier: "ToolStateError" })
297
- export type ToolStateError = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateError>>
298
-
299
- export const ToolState = Schema.Union([
300
- ToolStatePending,
301
- ToolStateRunning,
302
- ToolStateCompleted,
303
- ToolStateError,
304
- ]).annotate({
305
- discriminator: "status",
306
- identifier: "ToolState",
307
- })
308
- export type ToolState = ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError
309
-
310
- export const ToolPart = Schema.Struct({
311
- ...partBase,
312
- type: Schema.Literal("tool"),
313
- callID: Schema.String,
314
- tool: Schema.String,
315
- state: ToolState,
316
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
317
- }).annotate({ identifier: "ToolPart" })
318
- export type ToolPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof ToolPart>>, "state"> & {
319
- state: ToolState
320
- }
321
-
322
- const messageBase = {
323
- id: MessageID,
324
- sessionID: SessionID,
325
- }
326
-
327
- export const User = Schema.Struct({
328
- ...messageBase,
329
- role: Schema.Literal("user"),
330
- time: Schema.Struct({
331
- created: NonNegativeInt,
332
- }),
333
- format: Schema.optional(Format),
334
- summary: Schema.optional(
335
- Schema.Struct({
336
- title: Schema.optional(Schema.String),
337
- body: Schema.optional(Schema.String),
338
- diffs: Schema.Array(Snapshot.FileDiff),
339
- }),
340
- ),
341
- agent: Schema.String,
342
- model: Schema.Struct({
343
- providerID: ProviderID,
344
- modelID: ModelID,
345
- variant: Schema.optional(Schema.String),
346
- }),
347
- system: Schema.optional(Schema.String),
348
- tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
349
- }).annotate({ identifier: "UserMessage" })
350
- export type User = Types.DeepMutable<Schema.Schema.Type<typeof User>>
351
-
352
- export const Part = Schema.Union([
353
- TextPart,
354
- SubtaskPart,
355
- ReasoningPart,
356
- FilePart,
357
- ToolPart,
358
- StepStartPart,
359
- StepFinishPart,
360
- SnapshotPart,
361
- PatchPart,
362
- AgentPart,
363
- RetryPart,
364
- CompactionPart,
365
- ]).annotate({ discriminator: "type", identifier: "Part" })
366
- export type Part =
367
- | TextPart
368
- | SubtaskPart
369
- | ReasoningPart
370
- | FilePart
371
- | ToolPart
372
- | StepStartPart
373
- | StepFinishPart
374
- | SnapshotPart
375
- | PatchPart
376
- | AgentPart
377
- | RetryPart
378
- | CompactionPart
379
-
380
- const AssistantErrorSchema = Schema.Union([
381
- ...MessageError.Shared,
382
- AbortedError.EffectSchema,
383
- StructuredOutputError.EffectSchema,
384
- ContextOverflowError.EffectSchema,
385
- APIError.EffectSchema,
386
- ]).annotate({ discriminator: "name" })
387
- type AssistantError = Schema.Schema.Type<typeof AssistantErrorSchema>
388
-
389
- // ── Prompt input schemas ─────────────────────────────────────────────────────
390
- //
391
- // Consumers of `SessionPrompt.PromptInput.parts` send part drafts without the
392
- // ambient IDs (`messageID`, `sessionID`) that live on stored parts, and may
393
- // omit `id` to let the server allocate one. These Schema-Struct variants
394
- // carry that shape so prompt decoding can accept drafts without stored IDs.
395
-
396
- export const TextPartInput = Schema.Struct({
397
- id: Schema.optional(PartID),
398
- type: Schema.Literal("text"),
399
- text: Schema.String,
400
- synthetic: Schema.optional(Schema.Boolean),
401
- ignored: Schema.optional(Schema.Boolean),
402
- time: Schema.optional(
403
- Schema.Struct({
404
- start: NonNegativeInt,
405
- end: Schema.optional(NonNegativeInt),
406
- }),
407
- ),
408
- metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
409
- }).annotate({ identifier: "TextPartInput" })
410
- export type TextPartInput = Types.DeepMutable<Schema.Schema.Type<typeof TextPartInput>>
411
-
412
- export const FilePartInput = Schema.Struct({
413
- id: Schema.optional(PartID),
414
- type: Schema.Literal("file"),
415
- mime: Schema.String,
416
- filename: Schema.optional(Schema.String),
417
- url: Schema.String,
418
- source: Schema.optional(FilePartSource),
419
- }).annotate({ identifier: "FilePartInput" })
420
- export type FilePartInput = Types.DeepMutable<Schema.Schema.Type<typeof FilePartInput>>
421
-
422
- export const AgentPartInput = Schema.Struct({
423
- id: Schema.optional(PartID),
424
- type: Schema.Literal("agent"),
425
- name: Schema.String,
426
- source: Schema.optional(
427
- Schema.Struct({
428
- value: Schema.String,
429
- start: NonNegativeInt,
430
- end: NonNegativeInt,
431
- }),
432
- ),
433
- }).annotate({ identifier: "AgentPartInput" })
434
- export type AgentPartInput = Types.DeepMutable<Schema.Schema.Type<typeof AgentPartInput>>
435
-
436
- export const SubtaskPartInput = Schema.Struct({
437
- id: Schema.optional(PartID),
438
- type: Schema.Literal("subtask"),
439
- prompt: Schema.String,
440
- description: Schema.String,
441
- agent: Schema.String,
442
- model: Schema.optional(
443
- Schema.Struct({
444
- providerID: ProviderID,
445
- modelID: ModelID,
446
- }),
447
- ),
448
- command: Schema.optional(Schema.String),
449
- }).annotate({ identifier: "SubtaskPartInput" })
450
- export type SubtaskPartInput = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPartInput>>
451
-
452
- export const Assistant = Schema.Struct({
453
- ...messageBase,
454
- role: Schema.Literal("assistant"),
455
- time: Schema.Struct({
456
- created: NonNegativeInt,
457
- completed: Schema.optional(NonNegativeInt),
458
- }),
459
- error: Schema.optional(AssistantErrorSchema),
460
- parentID: MessageID,
461
- modelID: ModelID,
462
- providerID: ProviderID,
463
- /**
464
- * @deprecated
465
- */
466
- mode: Schema.String,
467
- agent: Schema.String,
468
- path: Schema.Struct({
469
- cwd: Schema.String,
470
- root: Schema.String,
471
- }),
472
- summary: Schema.optional(Schema.Boolean),
473
- cost: Schema.Finite,
474
- tokens: Schema.Struct({
475
- total: Schema.optional(Schema.Finite),
476
- input: Schema.Finite,
477
- output: Schema.Finite,
478
- reasoning: Schema.Finite,
479
- cache: Schema.Struct({
480
- read: Schema.Finite,
481
- write: Schema.Finite,
482
- }),
483
- }),
484
- structured: Schema.optional(Schema.Any),
485
- variant: Schema.optional(Schema.String),
486
- finish: Schema.optional(Schema.String),
487
- }).annotate({ identifier: "AssistantMessage" })
488
- export type Assistant = Omit<Types.DeepMutable<Schema.Schema.Type<typeof Assistant>>, "error"> & {
489
- error?: AssistantError
490
- }
491
-
492
- export const Info = Schema.Union([User, Assistant]).annotate({ discriminator: "role", identifier: "Message" })
493
- export type Info = User | Assistant
494
-
495
- const UpdatedEventSchema = Schema.Struct({
496
- sessionID: SessionID,
497
- info: Info,
498
- })
499
-
500
- const RemovedEventSchema = Schema.Struct({
501
- sessionID: SessionID,
502
- messageID: MessageID,
503
- })
504
-
505
- const PartUpdatedEventSchema = Schema.Struct({
506
- sessionID: SessionID,
507
- part: Part,
508
- time: NonNegativeInt,
509
- })
510
-
511
- const PartRemovedEventSchema = Schema.Struct({
512
- sessionID: SessionID,
513
- messageID: MessageID,
514
- partID: PartID,
515
- })
516
-
517
- export const Event = {
518
- Updated: SyncEvent.define({
519
- type: "message.updated",
520
- version: 1,
521
- aggregate: "sessionID",
522
- schema: UpdatedEventSchema,
523
- }),
524
- Removed: SyncEvent.define({
525
- type: "message.removed",
526
- version: 1,
527
- aggregate: "sessionID",
528
- schema: RemovedEventSchema,
529
- }),
530
- PartUpdated: SyncEvent.define({
531
- type: "message.part.updated",
532
- version: 1,
533
- aggregate: "sessionID",
534
- schema: PartUpdatedEventSchema,
535
- }),
536
- PartDelta: BusEvent.define(
537
- "message.part.delta",
538
- Schema.Struct({
539
- sessionID: SessionID,
540
- messageID: MessageID,
541
- partID: PartID,
542
- field: Schema.String,
543
- delta: Schema.String,
544
- }),
545
- ),
546
- PartRemoved: SyncEvent.define({
547
- type: "message.part.removed",
548
- version: 1,
549
- aggregate: "sessionID",
550
- schema: PartRemovedEventSchema,
551
- }),
552
- }
553
-
554
- export const WithParts = Schema.Struct({
555
- info: Info,
556
- parts: Schema.Array(Part),
557
- })
558
- export type WithParts = {
559
- info: Info
560
- parts: Part[]
561
- }
562
-
563
- const Cursor = Schema.Struct({
564
- id: MessageID,
565
- time: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
566
- })
567
- type Cursor = typeof Cursor.Type
568
-
569
- const decodeCursor = Schema.decodeUnknownSync(Cursor)
570
-
571
- export const cursor = {
572
- encode(input: Cursor) {
573
- return Buffer.from(JSON.stringify(input)).toString("base64url")
574
- },
575
- decode(input: string) {
576
- return decodeCursor(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
577
- },
578
- }
579
-
580
- const info = (row: typeof MessageTable.$inferSelect) =>
581
- ({
582
- ...row.data,
583
- id: row.id,
584
- sessionID: row.session_id,
585
- }) as Info
586
-
587
- const part = (row: typeof PartTable.$inferSelect) =>
588
- ({
589
- ...row.data,
590
- id: row.id,
591
- sessionID: row.session_id,
592
- messageID: row.message_id,
593
- }) as Part
594
-
595
- const older = (row: Cursor) =>
596
- or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
597
-
598
- function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
599
- const ids = rows.map((row) => row.id)
600
- const partByMessage = new Map<string, Part[]>()
601
- if (ids.length > 0) {
602
- const partRows = Database.use((db) =>
603
- db
604
- .select()
605
- .from(PartTable)
606
- .where(inArray(PartTable.message_id, ids))
607
- .orderBy(PartTable.message_id, PartTable.id)
608
- .all(),
609
- )
610
- for (const row of partRows) {
611
- const next = part(row)
612
- const list = partByMessage.get(row.message_id)
613
- if (list) list.push(next)
614
- else partByMessage.set(row.message_id, [next])
615
- }
616
- }
617
-
618
- return rows.map((row) => ({
619
- info: info(row),
620
- parts: partByMessage.get(row.id) ?? [],
621
- }))
622
- }
623
-
624
- function providerMeta(metadata: Record<string, any> | undefined) {
625
- if (!metadata) return undefined
626
- const { providerExecuted: _, ...rest } = metadata
627
- return Object.keys(rest).length > 0 ? rest : undefined
628
- }
629
-
630
- export const toModelMessagesEffect = Effect.fnUntraced(function* (
631
- input: WithParts[],
632
- model: Provider.Model,
633
- options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
634
- ) {
635
- const result: UIMessage[] = []
636
- const toolNames = new Set<string>()
637
- // Track media from tool results that need to be injected as user messages
638
- // for providers that don't support that media type in tool results.
639
- //
640
- // OpenAI-compatible APIs only support string content in tool results, so we need
641
- // to extract media and inject as user messages. Some SDKs only support a subset
642
- // of media in tool results; e.g. Bedrock supports images but not PDFs there.
643
- //
644
- // Only apply this workaround if the model actually supports that media input -
645
- // otherwise unsupportedParts() will turn it into a user-visible error.
646
- const supportsMediaInToolResult = (attachment: { mime: string }) => {
647
- if (model.api.npm === "@ai-sdk/anthropic") return true
648
- if (model.api.npm === "@ai-sdk/openai") return true
649
- if (model.api.npm === "@ai-sdk/amazon-bedrock") return attachment.mime.startsWith("image/")
650
- if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
651
- if (model.api.npm === "@ai-sdk/google") {
652
- const id = model.api.id.toLowerCase()
653
- return id.includes("gemini-3") && !id.includes("gemini-2")
654
- }
655
- return false
656
- }
657
-
658
- const toModelOutput = (options: { toolCallId: string; input: unknown; output: unknown }) => {
659
- const output = options.output
660
- if (typeof output === "string") {
661
- return { type: "text", value: output }
662
- }
663
-
664
- if (typeof output === "object") {
665
- const outputObject = output as {
666
- text: string
667
- attachments?: Array<{ mime: string; url: string }>
668
- }
669
- const attachments = (outputObject.attachments ?? []).filter((attachment) => {
670
- return attachment.url.startsWith("data:") && attachment.url.includes(",")
671
- })
672
-
673
- return {
674
- type: "content",
675
- value: [
676
- ...(outputObject.text ? [{ type: "text", text: outputObject.text }] : []),
677
- ...attachments.map((attachment) => ({
678
- type: "media",
679
- mediaType: attachment.mime,
680
- data: iife(() => {
681
- const commaIndex = attachment.url.indexOf(",")
682
- return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
683
- }),
684
- })),
685
- ],
686
- }
687
- }
688
-
689
- return { type: "json", value: output as never }
690
- }
691
-
692
- for (const msg of input) {
693
- if (msg.parts.length === 0) continue
694
-
695
- if (msg.info.role === "user") {
696
- const userMessage: UIMessage = {
697
- id: msg.info.id,
698
- role: "user",
699
- parts: [],
700
- }
701
- for (const part of msg.parts) {
702
- // User message parts should never be empty
703
- if (part.type === "text" && !part.ignored && part.text !== "")
704
- userMessage.parts.push({
705
- type: "text",
706
- text: part.text,
707
- })
708
- // text/plain and directory files are converted into text parts, ignore them
709
- if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") {
710
- if (options?.stripMedia && isMedia(part.mime)) {
711
- userMessage.parts.push({
712
- type: "text",
713
- text: `[Attached ${part.mime}: ${part.filename ?? "file"}]`,
714
- })
715
- } else {
716
- userMessage.parts.push({
717
- type: "file",
718
- url: part.url,
719
- mediaType: part.mime,
720
- filename: part.filename,
721
- })
722
- }
723
- }
724
-
725
- if (part.type === "compaction") {
726
- userMessage.parts.push({
727
- type: "text",
728
- text: "What did we do so far?",
729
- })
730
- }
731
- if (part.type === "subtask") {
732
- userMessage.parts.push({
733
- type: "text",
734
- text: "The following tool was executed by the user",
735
- })
736
- }
737
- }
738
- if (userMessage.parts.length > 0) result.push(userMessage)
739
- }
740
-
741
- if (msg.info.role === "assistant") {
742
- const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
743
- const media: Array<{ mime: string; url: string; filename?: string }> = []
744
-
745
- if (
746
- msg.info.error &&
747
- !(
748
- AbortedError.isInstance(msg.info.error) &&
749
- msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
750
- )
751
- ) {
752
- continue
753
- }
754
- const assistantMessage: UIMessage = {
755
- id: msg.info.id,
756
- role: "assistant",
757
- parts: [],
758
- }
759
- // Anthropic adaptive thinking can persist assistant turns like:
760
- // step-start, reasoning(signature), text(""), step-start,
761
- // reasoning(signature). The empty text part is a structural separator,
762
- // but it does not carry the signature metadata itself. Dropping it shifts
763
- // signed thinking positions after step-start splitting/provider regrouping;
764
- // keeping it as "" is filtered by the AI SDK and rejected by Anthropic.
765
- // It is unclear whether this shape originates in our stream processing,
766
- // a proxy, or a lower-level library, but preserving a non-empty separator
767
- // here is the only safe replay point we have.
768
- // Use a single space so the separator survives replay without changing
769
- // the neighboring signed reasoning blocks.
770
- const hasSignedReasoning = msg.parts.some((part) => {
771
- if (part.type !== "reasoning") return false
772
- return part.metadata?.anthropic?.signature != null
773
- })
774
- for (const part of msg.parts) {
775
- if (part.type === "text") {
776
- const text = part.text === "" && hasSignedReasoning ? " " : part.text
777
- assistantMessage.parts.push({
778
- type: "text",
779
- text,
780
- ...(differentModel ? {} : { providerMetadata: part.metadata }),
781
- })
782
- }
783
- if (part.type === "step-start")
784
- assistantMessage.parts.push({
785
- type: "step-start",
786
- })
787
- if (part.type === "tool") {
788
- toolNames.add(part.tool)
789
- if (part.state.status === "completed") {
790
- const outputText = part.state.time.compacted
791
- ? "[Old tool result content cleared]"
792
- : truncateToolOutput(part.state.output, options?.toolOutputMaxChars)
793
- const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? [])
794
-
795
- // For providers that don't support media in tool results, extract media files
796
- // (images, PDFs) to be sent as a separate user message
797
- const mediaAttachments = attachments.filter((a) => isMedia(a.mime))
798
- const extractedMedia = mediaAttachments.filter((a) => !supportsMediaInToolResult(a))
799
- if (extractedMedia.length > 0) {
800
- media.push(...extractedMedia)
801
- }
802
- const finalAttachments = attachments.filter((a) => !isMedia(a.mime) || supportsMediaInToolResult(a))
803
-
804
- const output =
805
- finalAttachments.length > 0
806
- ? {
807
- text: outputText,
808
- attachments: finalAttachments,
809
- }
810
- : outputText
811
-
812
- assistantMessage.parts.push({
813
- type: ("tool-" + part.tool) as `tool-${string}`,
814
- state: "output-available",
815
- toolCallId: part.callID,
816
- input: part.state.input,
817
- output,
818
- ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
819
- ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
820
- })
821
- }
822
- if (part.state.status === "error") {
823
- const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined
824
- if (typeof output === "string") {
825
- assistantMessage.parts.push({
826
- type: ("tool-" + part.tool) as `tool-${string}`,
827
- state: "output-available",
828
- toolCallId: part.callID,
829
- input: part.state.input,
830
- output,
831
- ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
832
- ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
833
- })
834
- } else {
835
- assistantMessage.parts.push({
836
- type: ("tool-" + part.tool) as `tool-${string}`,
837
- state: "output-error",
838
- toolCallId: part.callID,
839
- input: part.state.input,
840
- errorText: part.state.error,
841
- ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
842
- ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
843
- })
844
- }
845
- }
846
- // Handle pending/running tool calls to prevent dangling tool_use blocks
847
- // Anthropic/Claude APIs require every tool_use to have a corresponding tool_result
848
- if (part.state.status === "pending" || part.state.status === "running")
849
- assistantMessage.parts.push({
850
- type: ("tool-" + part.tool) as `tool-${string}`,
851
- state: "output-error",
852
- toolCallId: part.callID,
853
- input: part.state.input,
854
- errorText: "[Tool execution was interrupted]",
855
- ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
856
- ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
857
- })
858
- }
859
- if (part.type === "reasoning") {
860
- if (differentModel) {
861
- if (part.text.trim().length > 0)
862
- assistantMessage.parts.push({
863
- type: "text",
864
- text: part.text,
865
- })
866
- continue
867
- }
868
- assistantMessage.parts.push({
869
- type: "reasoning",
870
- text: part.text,
871
- providerMetadata: part.metadata,
872
- })
873
- }
874
- }
875
- if (assistantMessage.parts.length > 0) {
876
- result.push(assistantMessage)
877
- // Inject pending media as a user message for providers that don't support
878
- // media (images, PDFs) in tool results
879
- if (media.length > 0) {
880
- result.push({
881
- id: MessageID.ascending(),
882
- role: "user",
883
- parts: [
884
- {
885
- type: "text" as const,
886
- text: SYNTHETIC_ATTACHMENT_PROMPT,
887
- },
888
- ...media.map((attachment) => ({
889
- type: "file" as const,
890
- url: attachment.url,
891
- mediaType: attachment.mime,
892
- filename: attachment.filename,
893
- })),
894
- ],
895
- })
896
- }
897
- }
898
- }
899
- }
900
-
901
- const tools = Object.fromEntries(Array.from(toolNames).map((toolName) => [toolName, { toModelOutput }]))
902
-
903
- return yield* Effect.promise(() =>
904
- convertToModelMessages(
905
- result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")),
906
- {
907
- //@ts-expect-error (convertToModelMessages expects a ToolSet but only actually needs tools[name]?.toModelOutput)
908
- tools,
909
- },
910
- ),
911
- )
912
- })
913
-
914
- export function toModelMessages(
915
- input: WithParts[],
916
- model: Provider.Model,
917
- options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
918
- ): Promise<ModelMessage[]> {
919
- return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
920
- }
921
-
922
- export const page = Effect.fn("MessageV2.page")(function* (input: {
923
- sessionID: SessionID
924
- limit: number
925
- before?: string
926
- }) {
927
- const before = input.before ? cursor.decode(input.before) : undefined
928
- const where = before
929
- ? and(eq(MessageTable.session_id, input.sessionID), older(before))
930
- : eq(MessageTable.session_id, input.sessionID)
931
- const rows = Database.use((db) =>
932
- db
933
- .select()
934
- .from(MessageTable)
935
- .where(where)
936
- .orderBy(desc(MessageTable.time_created), desc(MessageTable.id))
937
- .limit(input.limit + 1)
938
- .all(),
939
- )
940
- if (rows.length === 0) {
941
- const row = Database.use((db) =>
942
- db.select({ id: SessionTable.id }).from(SessionTable).where(eq(SessionTable.id, input.sessionID)).get(),
943
- )
944
- if (!row) return yield* new NotFoundError({ message: `Session not found: ${input.sessionID}` })
945
- return {
946
- items: [] as WithParts[],
947
- more: false,
948
- }
949
- }
950
-
951
- const more = rows.length > input.limit
952
- const slice = more ? rows.slice(0, input.limit) : rows
953
- const items = hydrate(slice)
954
- items.reverse()
955
- const tail = slice.at(-1)
956
- return {
957
- items,
958
- more,
959
- cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
960
- }
961
- })
962
-
963
- export function* stream(sessionID: SessionID) {
964
- const size = 50
965
- let before: string | undefined
966
- while (true) {
967
- const next = Effect.runSync(
968
- page({ sessionID, limit: size, before }).pipe(
969
- Effect.catchIf(NotFoundError.isInstance, () =>
970
- Effect.succeed({ items: [] as WithParts[], more: false, cursor: undefined }),
971
- ),
972
- ),
973
- )
974
- if (next.items.length === 0) break
975
- for (let i = next.items.length - 1; i >= 0; i--) {
976
- yield next.items[i]
977
- }
978
- if (!next.more || !next.cursor) break
979
- before = next.cursor
980
- }
981
- }
982
-
983
- export function parts(message_id: MessageID) {
984
- const rows = Database.use((db) =>
985
- db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
986
- )
987
- return rows.map(
988
- (row) =>
989
- ({
990
- ...row.data,
991
- id: row.id,
992
- sessionID: row.session_id,
993
- messageID: row.message_id,
994
- }) as Part,
995
- )
996
- }
997
-
998
- export const get = Effect.fn("MessageV2.get")(function* (input: { sessionID: SessionID; messageID: MessageID }) {
999
- const row = Database.use((db) =>
1000
- db
1001
- .select()
1002
- .from(MessageTable)
1003
- .where(and(eq(MessageTable.id, input.messageID), eq(MessageTable.session_id, input.sessionID)))
1004
- .get(),
1005
- )
1006
- if (!row) return yield* new NotFoundError({ message: `Message not found: ${input.messageID}` })
1007
- return {
1008
- info: info(row),
1009
- parts: parts(input.messageID),
1010
- }
1011
- })
1012
-
1013
- export function filterCompacted(msgs: Iterable<WithParts>) {
1014
- const result = [] as WithParts[]
1015
- const completed = new Set<string>()
1016
- let retain: MessageID | undefined
1017
- for (const msg of msgs) {
1018
- result.push(msg)
1019
- if (retain) {
1020
- if (msg.info.id === retain) break
1021
- continue
1022
- }
1023
- if (msg.info.role === "user" && completed.has(msg.info.id)) {
1024
- const part = msg.parts.find((item): item is CompactionPart => item.type === "compaction")
1025
- if (!part) continue
1026
- if (!part.tail_start_id) break
1027
- retain = part.tail_start_id
1028
- if (msg.info.id === retain) break
1029
- continue
1030
- }
1031
- if (msg.info.role === "user" && completed.has(msg.info.id) && msg.parts.some((part) => part.type === "compaction"))
1032
- break
1033
- if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish && !msg.info.error)
1034
- completed.add(msg.info.parentID)
1035
- }
1036
- result.reverse()
1037
- const compactionIndex = result.findLastIndex(
1038
- (msg) =>
1039
- msg.info.role === "user" &&
1040
- msg.parts.some((item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined),
1041
- )
1042
- const compaction = result[compactionIndex]
1043
- const part = compaction?.parts.find(
1044
- (item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined,
1045
- )
1046
- const summaryIndex = compaction
1047
- ? result.findIndex(
1048
- (msg, index) =>
1049
- index > compactionIndex &&
1050
- msg.info.role === "assistant" &&
1051
- msg.info.summary &&
1052
- msg.info.parentID === compaction.info.id,
1053
- )
1054
- : -1
1055
- const tailIndex = part?.tail_start_id ? result.findIndex((msg) => msg.info.id === part.tail_start_id) : -1
1056
- if (tailIndex >= 0 && tailIndex < compactionIndex && summaryIndex > compactionIndex) {
1057
- return [
1058
- ...result.slice(compactionIndex, summaryIndex + 1),
1059
- ...result.slice(tailIndex, compactionIndex),
1060
- ...result.slice(summaryIndex + 1),
1061
- ]
1062
- }
1063
- return result
1064
- }
1065
-
1066
- export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
1067
- return filterCompacted(stream(sessionID))
1068
- })
1069
-
1070
- // filterCompacted reorders messages for model consumption
1071
- // ([compaction-user, summary, ...retained tail..., continue-user]), so array
1072
- // position is not chronological. Derive each binding by max id (MessageID
1073
- // is monotonic via MessageID.ascending) so a pre-compaction overflowing tail
1074
- // assistant doesn't get mistaken for the most recent turn. tasks are
1075
- // compaction/subtask parts attached to user messages newer than the latest
1076
- // finished assistant — i.e. unprocessed work.
1077
- export function latest(msgs: WithParts[]) {
1078
- let user: User | undefined
1079
- let assistant: Assistant | undefined
1080
- let finished: Assistant | undefined
1081
- for (const msg of msgs) {
1082
- const info = msg.info
1083
- if (info.role === "user" && (!user || info.id > user.id)) user = info
1084
- if (info.role === "assistant" && (!assistant || info.id > assistant.id)) assistant = info
1085
- if (info.role === "assistant" && info.finish && (!finished || info.id > finished.id)) finished = info
1086
- }
1087
- const tasks = msgs.flatMap((m) =>
1088
- finished && m.info.id <= finished.id
1089
- ? []
1090
- : m.parts.filter((p): p is CompactionPart | SubtaskPart => p.type === "compaction" || p.type === "subtask"),
1091
- )
1092
- return { user, assistant, finished, tasks }
1093
- }
1094
-
1095
- export function fromError(
1096
- e: unknown,
1097
- ctx: { providerID: ProviderID; aborted?: boolean },
1098
- ): NonNullable<Assistant["error"]> {
1099
- switch (true) {
1100
- case e instanceof DOMException && e.name === "AbortError":
1101
- return new AbortedError(
1102
- { message: e.message },
1103
- {
1104
- cause: e,
1105
- },
1106
- ).toObject()
1107
- case OutputLengthError.isInstance(e):
1108
- return e
1109
- case LoadAPIKeyError.isInstance(e):
1110
- return new AuthError(
1111
- {
1112
- providerID: ctx.providerID,
1113
- message: e.message,
1114
- },
1115
- { cause: e },
1116
- ).toObject()
1117
- case (e as SystemError)?.code === "ECONNRESET":
1118
- return new APIError(
1119
- {
1120
- message: "Connection reset by server",
1121
- isRetryable: true,
1122
- metadata: {
1123
- code: (e as SystemError).code ?? "",
1124
- syscall: (e as SystemError).syscall ?? "",
1125
- message: (e as SystemError).message ?? "",
1126
- },
1127
- },
1128
- { cause: e },
1129
- ).toObject()
1130
- case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
1131
- if (ctx.aborted) {
1132
- return new AbortedError({ message: e.message }, { cause: e }).toObject()
1133
- }
1134
- return new APIError(
1135
- {
1136
- message: "Response decompression failed",
1137
- isRetryable: true,
1138
- metadata: {
1139
- code: (e as FetchDecompressionError).code,
1140
- message: e.message,
1141
- },
1142
- },
1143
- { cause: e },
1144
- ).toObject()
1145
- case APICallError.isInstance(e):
1146
- const parsed = ProviderError.parseAPICallError({
1147
- providerID: ctx.providerID,
1148
- error: e,
1149
- })
1150
- if (parsed.type === "context_overflow") {
1151
- return new ContextOverflowError(
1152
- {
1153
- message: parsed.message,
1154
- responseBody: parsed.responseBody,
1155
- },
1156
- { cause: e },
1157
- ).toObject()
1158
- }
1159
-
1160
- return new APIError(
1161
- {
1162
- message: parsed.message,
1163
- statusCode: parsed.statusCode,
1164
- isRetryable: parsed.isRetryable,
1165
- responseHeaders: parsed.responseHeaders,
1166
- responseBody: parsed.responseBody,
1167
- metadata: parsed.metadata,
1168
- },
1169
- { cause: e },
1170
- ).toObject()
1171
- case e instanceof Error:
1172
- return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
1173
- default:
1174
- try {
1175
- const parsed = ProviderError.parseStreamError(e)
1176
- if (parsed) {
1177
- if (parsed.type === "context_overflow") {
1178
- return new ContextOverflowError(
1179
- {
1180
- message: parsed.message,
1181
- responseBody: parsed.responseBody,
1182
- },
1183
- { cause: e },
1184
- ).toObject()
1185
- }
1186
- return new APIError(
1187
- {
1188
- message: parsed.message,
1189
- isRetryable: parsed.isRetryable,
1190
- responseBody: parsed.responseBody,
1191
- },
1192
- {
1193
- cause: e,
1194
- },
1195
- ).toObject()
1196
- }
1197
- } catch {}
1198
- return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
1199
- }
1200
- }
1201
-
1202
- export * as MessageV2 from "./message-v2"
1
+ import { BusEvent } from "@/bus/bus-event"
2
+ import { SessionID, MessageID, PartID } from "./schema"
3
+ import { NamedError } from "@opencode-ai/core/util/error"
4
+ import { APICallError, convertToModelMessages, LoadAPIKeyError, type ModelMessage, type UIMessage } from "ai"
5
+ import { LSP } from "@/lsp/lsp"
6
+ import { Snapshot } from "@/snapshot"
7
+ import { SyncEvent } from "../sync"
8
+ import { Database } from "@/storage/db"
9
+ import { NotFoundError } from "@/storage/storage"
10
+ import { and } from "drizzle-orm"
11
+ import { desc } from "drizzle-orm"
12
+ import { eq } from "drizzle-orm"
13
+ import { inArray } from "drizzle-orm"
14
+ import { lt } from "drizzle-orm"
15
+ import { or } from "drizzle-orm"
16
+ import { MessageTable, PartTable, SessionTable } from "./session.sql"
17
+ import * as ProviderError from "@/provider/error"
18
+ import { iife } from "@/util/iife"
19
+ import { errorMessage } from "@/util/error"
20
+ import { isMedia } from "@/util/media"
21
+ import type { SystemError } from "bun"
22
+ import type { Provider } from "@/provider/provider"
23
+ import { ModelID, ProviderID } from "@/provider/schema"
24
+ import { Effect, Schema, Types } from "effect"
25
+ import { NonNegativeInt } from "@opencode-ai/core/schema"
26
+ import * as EffectLogger from "@opencode-ai/core/effect/logger"
27
+ import { MessageError } from "./message-error"
28
+ import { AuthError, OutputLengthError } from "./message-error"
29
+ export { AuthError, OutputLengthError } from "./message-error"
30
+
31
+ /** Error shape thrown by Bun's fetch() when gzip/br decompression fails mid-stream */
32
+ interface FetchDecompressionError extends Error {
33
+ code: "ZlibError"
34
+ errno: number
35
+ path: string
36
+ }
37
+
38
+ export const SYNTHETIC_ATTACHMENT_PROMPT = "Attached media from tool result:"
39
+ export { isMedia }
40
+
41
+ export const AbortedError = NamedError.create("MessageAbortedError", { message: Schema.String })
42
+ export const StructuredOutputError = NamedError.create("StructuredOutputError", {
43
+ message: Schema.String,
44
+ retries: NonNegativeInt,
45
+ })
46
+ export const APIError = NamedError.create("APIError", {
47
+ message: Schema.String,
48
+ statusCode: Schema.optional(NonNegativeInt),
49
+ isRetryable: Schema.Boolean,
50
+ responseHeaders: Schema.optional(Schema.Record(Schema.String, Schema.String)),
51
+ responseBody: Schema.optional(Schema.String),
52
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
53
+ })
54
+ export type APIError = Schema.Schema.Type<typeof APIError.Schema>
55
+ export const ContextOverflowError = NamedError.create("ContextOverflowError", {
56
+ message: Schema.String,
57
+ responseBody: Schema.optional(Schema.String),
58
+ })
59
+
60
+ export class OutputFormatText extends Schema.Class<OutputFormatText>("OutputFormatText")({
61
+ type: Schema.Literal("text"),
62
+ }) {}
63
+
64
+ export class OutputFormatJsonSchema extends Schema.Class<OutputFormatJsonSchema>("OutputFormatJsonSchema")({
65
+ type: Schema.Literal("json_schema"),
66
+ schema: Schema.Record(Schema.String, Schema.Any).annotate({ identifier: "JSONSchema" }),
67
+ retryCount: NonNegativeInt.pipe(Schema.optional, Schema.withDecodingDefault(Effect.succeed(2))),
68
+ }) {}
69
+
70
+ export const Format = Schema.Union([OutputFormatText, OutputFormatJsonSchema]).annotate({
71
+ discriminator: "type",
72
+ identifier: "OutputFormat",
73
+ })
74
+ export type OutputFormat = Schema.Schema.Type<typeof Format>
75
+
76
+ const partBase = {
77
+ id: PartID,
78
+ sessionID: SessionID,
79
+ messageID: MessageID,
80
+ }
81
+
82
+ export const SnapshotPart = Schema.Struct({
83
+ ...partBase,
84
+ type: Schema.Literal("snapshot"),
85
+ snapshot: Schema.String,
86
+ }).annotate({ identifier: "SnapshotPart" })
87
+ export type SnapshotPart = Types.DeepMutable<Schema.Schema.Type<typeof SnapshotPart>>
88
+
89
+ export const PatchPart = Schema.Struct({
90
+ ...partBase,
91
+ type: Schema.Literal("patch"),
92
+ hash: Schema.String,
93
+ files: Schema.Array(Schema.String),
94
+ }).annotate({ identifier: "PatchPart" })
95
+ export type PatchPart = Types.DeepMutable<Schema.Schema.Type<typeof PatchPart>>
96
+
97
+ export const TextPart = Schema.Struct({
98
+ ...partBase,
99
+ type: Schema.Literal("text"),
100
+ text: Schema.String,
101
+ synthetic: Schema.optional(Schema.Boolean),
102
+ ignored: Schema.optional(Schema.Boolean),
103
+ time: Schema.optional(
104
+ Schema.Struct({
105
+ start: NonNegativeInt,
106
+ end: Schema.optional(NonNegativeInt),
107
+ }),
108
+ ),
109
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
110
+ }).annotate({ identifier: "TextPart" })
111
+ export type TextPart = Types.DeepMutable<Schema.Schema.Type<typeof TextPart>>
112
+
113
+ export const ReasoningPart = Schema.Struct({
114
+ ...partBase,
115
+ type: Schema.Literal("reasoning"),
116
+ text: Schema.String,
117
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
118
+ time: Schema.Struct({
119
+ start: NonNegativeInt,
120
+ end: Schema.optional(NonNegativeInt),
121
+ }),
122
+ }).annotate({ identifier: "ReasoningPart" })
123
+ export type ReasoningPart = Types.DeepMutable<Schema.Schema.Type<typeof ReasoningPart>>
124
+
125
+ const filePartSourceBase = {
126
+ text: Schema.Struct({
127
+ value: Schema.String,
128
+ start: Schema.Finite,
129
+ end: Schema.Finite,
130
+ }).annotate({ identifier: "FilePartSourceText" }),
131
+ }
132
+
133
+ export const FileSource = Schema.Struct({
134
+ ...filePartSourceBase,
135
+ type: Schema.Literal("file"),
136
+ path: Schema.String,
137
+ }).annotate({ identifier: "FileSource" })
138
+
139
+ export const SymbolSource = Schema.Struct({
140
+ ...filePartSourceBase,
141
+ type: Schema.Literal("symbol"),
142
+ path: Schema.String,
143
+ range: LSP.Range,
144
+ name: Schema.String,
145
+ kind: NonNegativeInt,
146
+ }).annotate({ identifier: "SymbolSource" })
147
+
148
+ export const ResourceSource = Schema.Struct({
149
+ ...filePartSourceBase,
150
+ type: Schema.Literal("resource"),
151
+ clientName: Schema.String,
152
+ uri: Schema.String,
153
+ }).annotate({ identifier: "ResourceSource" })
154
+
155
+ export const FilePartSource = Schema.Union([FileSource, SymbolSource, ResourceSource]).annotate({
156
+ discriminator: "type",
157
+ identifier: "FilePartSource",
158
+ })
159
+
160
+ export const FilePart = Schema.Struct({
161
+ ...partBase,
162
+ type: Schema.Literal("file"),
163
+ mime: Schema.String,
164
+ filename: Schema.optional(Schema.String),
165
+ url: Schema.String,
166
+ source: Schema.optional(FilePartSource),
167
+ }).annotate({ identifier: "FilePart" })
168
+ export type FilePart = Types.DeepMutable<Schema.Schema.Type<typeof FilePart>>
169
+
170
+ export const AgentPart = Schema.Struct({
171
+ ...partBase,
172
+ type: Schema.Literal("agent"),
173
+ name: Schema.String,
174
+ source: Schema.optional(
175
+ Schema.Struct({
176
+ value: Schema.String,
177
+ start: NonNegativeInt,
178
+ end: NonNegativeInt,
179
+ }),
180
+ ),
181
+ }).annotate({ identifier: "AgentPart" })
182
+ export type AgentPart = Types.DeepMutable<Schema.Schema.Type<typeof AgentPart>>
183
+
184
+ export const CompactionPart = Schema.Struct({
185
+ ...partBase,
186
+ type: Schema.Literal("compaction"),
187
+ auto: Schema.Boolean,
188
+ overflow: Schema.optional(Schema.Boolean),
189
+ tail_start_id: Schema.optional(MessageID),
190
+ }).annotate({ identifier: "CompactionPart" })
191
+ export type CompactionPart = Types.DeepMutable<Schema.Schema.Type<typeof CompactionPart>>
192
+
193
+ export const SubtaskPart = Schema.Struct({
194
+ ...partBase,
195
+ type: Schema.Literal("subtask"),
196
+ prompt: Schema.String,
197
+ description: Schema.String,
198
+ agent: Schema.String,
199
+ model: Schema.optional(
200
+ Schema.Struct({
201
+ providerID: ProviderID,
202
+ modelID: ModelID,
203
+ }),
204
+ ),
205
+ command: Schema.optional(Schema.String),
206
+ }).annotate({ identifier: "SubtaskPart" })
207
+ export type SubtaskPart = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPart>>
208
+
209
+ export const RetryPart = Schema.Struct({
210
+ ...partBase,
211
+ type: Schema.Literal("retry"),
212
+ attempt: NonNegativeInt,
213
+ error: APIError.EffectSchema,
214
+ time: Schema.Struct({
215
+ created: NonNegativeInt,
216
+ }),
217
+ }).annotate({ identifier: "RetryPart" })
218
+ export type RetryPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof RetryPart>>, "error"> & {
219
+ error: APIError
220
+ }
221
+
222
+ export const StepStartPart = Schema.Struct({
223
+ ...partBase,
224
+ type: Schema.Literal("step-start"),
225
+ snapshot: Schema.optional(Schema.String),
226
+ }).annotate({ identifier: "StepStartPart" })
227
+ export type StepStartPart = Types.DeepMutable<Schema.Schema.Type<typeof StepStartPart>>
228
+
229
+ export const StepFinishPart = Schema.Struct({
230
+ ...partBase,
231
+ type: Schema.Literal("step-finish"),
232
+ reason: Schema.String,
233
+ snapshot: Schema.optional(Schema.String),
234
+ cost: Schema.Finite,
235
+ tokens: Schema.Struct({
236
+ total: Schema.optional(Schema.Finite),
237
+ input: Schema.Finite,
238
+ output: Schema.Finite,
239
+ reasoning: Schema.Finite,
240
+ cache: Schema.Struct({
241
+ read: Schema.Finite,
242
+ write: Schema.Finite,
243
+ }),
244
+ }),
245
+ }).annotate({ identifier: "StepFinishPart" })
246
+ export type StepFinishPart = Types.DeepMutable<Schema.Schema.Type<typeof StepFinishPart>>
247
+
248
+ export const ToolStatePending = Schema.Struct({
249
+ status: Schema.Literal("pending"),
250
+ input: Schema.Record(Schema.String, Schema.Any),
251
+ raw: Schema.String,
252
+ }).annotate({ identifier: "ToolStatePending" })
253
+ export type ToolStatePending = Types.DeepMutable<Schema.Schema.Type<typeof ToolStatePending>>
254
+
255
+ export const ToolStateRunning = Schema.Struct({
256
+ status: Schema.Literal("running"),
257
+ input: Schema.Record(Schema.String, Schema.Any),
258
+ title: Schema.optional(Schema.String),
259
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
260
+ time: Schema.Struct({
261
+ start: NonNegativeInt,
262
+ }),
263
+ }).annotate({ identifier: "ToolStateRunning" })
264
+ export type ToolStateRunning = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateRunning>>
265
+
266
+ export const ToolStateCompleted = Schema.Struct({
267
+ status: Schema.Literal("completed"),
268
+ input: Schema.Record(Schema.String, Schema.Any),
269
+ output: Schema.String,
270
+ title: Schema.String,
271
+ metadata: Schema.Record(Schema.String, Schema.Any),
272
+ time: Schema.Struct({
273
+ start: NonNegativeInt,
274
+ end: NonNegativeInt,
275
+ compacted: Schema.optional(NonNegativeInt),
276
+ }),
277
+ attachments: Schema.optional(Schema.Array(FilePart)),
278
+ }).annotate({ identifier: "ToolStateCompleted" })
279
+ export type ToolStateCompleted = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateCompleted>>
280
+
281
+ function truncateToolOutput(text: string, maxChars?: number) {
282
+ if (!maxChars || text.length <= maxChars) return text
283
+ const omitted = text.length - maxChars
284
+ return `${text.slice(0, maxChars)}\n[Tool output truncated for compaction: omitted ${omitted} chars]`
285
+ }
286
+
287
+ export const ToolStateError = Schema.Struct({
288
+ status: Schema.Literal("error"),
289
+ input: Schema.Record(Schema.String, Schema.Any),
290
+ error: Schema.String,
291
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
292
+ time: Schema.Struct({
293
+ start: NonNegativeInt,
294
+ end: NonNegativeInt,
295
+ }),
296
+ }).annotate({ identifier: "ToolStateError" })
297
+ export type ToolStateError = Types.DeepMutable<Schema.Schema.Type<typeof ToolStateError>>
298
+
299
+ export const ToolState = Schema.Union([
300
+ ToolStatePending,
301
+ ToolStateRunning,
302
+ ToolStateCompleted,
303
+ ToolStateError,
304
+ ]).annotate({
305
+ discriminator: "status",
306
+ identifier: "ToolState",
307
+ })
308
+ export type ToolState = ToolStatePending | ToolStateRunning | ToolStateCompleted | ToolStateError
309
+
310
+ export const ToolPart = Schema.Struct({
311
+ ...partBase,
312
+ type: Schema.Literal("tool"),
313
+ callID: Schema.String,
314
+ tool: Schema.String,
315
+ state: ToolState,
316
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
317
+ }).annotate({ identifier: "ToolPart" })
318
+ export type ToolPart = Omit<Types.DeepMutable<Schema.Schema.Type<typeof ToolPart>>, "state"> & {
319
+ state: ToolState
320
+ }
321
+
322
+ const messageBase = {
323
+ id: MessageID,
324
+ sessionID: SessionID,
325
+ }
326
+
327
+ export const User = Schema.Struct({
328
+ ...messageBase,
329
+ role: Schema.Literal("user"),
330
+ time: Schema.Struct({
331
+ created: NonNegativeInt,
332
+ }),
333
+ format: Schema.optional(Format),
334
+ summary: Schema.optional(
335
+ Schema.Struct({
336
+ title: Schema.optional(Schema.String),
337
+ body: Schema.optional(Schema.String),
338
+ diffs: Schema.Array(Snapshot.FileDiff),
339
+ }),
340
+ ),
341
+ agent: Schema.String,
342
+ model: Schema.Struct({
343
+ providerID: ProviderID,
344
+ modelID: ModelID,
345
+ variant: Schema.optional(Schema.String),
346
+ }),
347
+ system: Schema.optional(Schema.String),
348
+ tools: Schema.optional(Schema.Record(Schema.String, Schema.Boolean)),
349
+ }).annotate({ identifier: "UserMessage" })
350
+ export type User = Types.DeepMutable<Schema.Schema.Type<typeof User>>
351
+
352
+ export const Part = Schema.Union([
353
+ TextPart,
354
+ SubtaskPart,
355
+ ReasoningPart,
356
+ FilePart,
357
+ ToolPart,
358
+ StepStartPart,
359
+ StepFinishPart,
360
+ SnapshotPart,
361
+ PatchPart,
362
+ AgentPart,
363
+ RetryPart,
364
+ CompactionPart,
365
+ ]).annotate({ discriminator: "type", identifier: "Part" })
366
+ export type Part =
367
+ | TextPart
368
+ | SubtaskPart
369
+ | ReasoningPart
370
+ | FilePart
371
+ | ToolPart
372
+ | StepStartPart
373
+ | StepFinishPart
374
+ | SnapshotPart
375
+ | PatchPart
376
+ | AgentPart
377
+ | RetryPart
378
+ | CompactionPart
379
+
380
+ const AssistantErrorSchema = Schema.Union([
381
+ ...MessageError.Shared,
382
+ AbortedError.EffectSchema,
383
+ StructuredOutputError.EffectSchema,
384
+ ContextOverflowError.EffectSchema,
385
+ APIError.EffectSchema,
386
+ ]).annotate({ discriminator: "name" })
387
+ type AssistantError = Schema.Schema.Type<typeof AssistantErrorSchema>
388
+
389
+ // ── Prompt input schemas ─────────────────────────────────────────────────────
390
+ //
391
+ // Consumers of `SessionPrompt.PromptInput.parts` send part drafts without the
392
+ // ambient IDs (`messageID`, `sessionID`) that live on stored parts, and may
393
+ // omit `id` to let the server allocate one. These Schema-Struct variants
394
+ // carry that shape so prompt decoding can accept drafts without stored IDs.
395
+
396
+ export const TextPartInput = Schema.Struct({
397
+ id: Schema.optional(PartID),
398
+ type: Schema.Literal("text"),
399
+ text: Schema.String,
400
+ synthetic: Schema.optional(Schema.Boolean),
401
+ ignored: Schema.optional(Schema.Boolean),
402
+ time: Schema.optional(
403
+ Schema.Struct({
404
+ start: NonNegativeInt,
405
+ end: Schema.optional(NonNegativeInt),
406
+ }),
407
+ ),
408
+ metadata: Schema.optional(Schema.Record(Schema.String, Schema.Any)),
409
+ }).annotate({ identifier: "TextPartInput" })
410
+ export type TextPartInput = Types.DeepMutable<Schema.Schema.Type<typeof TextPartInput>>
411
+
412
+ export const FilePartInput = Schema.Struct({
413
+ id: Schema.optional(PartID),
414
+ type: Schema.Literal("file"),
415
+ mime: Schema.String,
416
+ filename: Schema.optional(Schema.String),
417
+ url: Schema.String,
418
+ source: Schema.optional(FilePartSource),
419
+ }).annotate({ identifier: "FilePartInput" })
420
+ export type FilePartInput = Types.DeepMutable<Schema.Schema.Type<typeof FilePartInput>>
421
+
422
+ export const AgentPartInput = Schema.Struct({
423
+ id: Schema.optional(PartID),
424
+ type: Schema.Literal("agent"),
425
+ name: Schema.String,
426
+ source: Schema.optional(
427
+ Schema.Struct({
428
+ value: Schema.String,
429
+ start: NonNegativeInt,
430
+ end: NonNegativeInt,
431
+ }),
432
+ ),
433
+ }).annotate({ identifier: "AgentPartInput" })
434
+ export type AgentPartInput = Types.DeepMutable<Schema.Schema.Type<typeof AgentPartInput>>
435
+
436
+ export const SubtaskPartInput = Schema.Struct({
437
+ id: Schema.optional(PartID),
438
+ type: Schema.Literal("subtask"),
439
+ prompt: Schema.String,
440
+ description: Schema.String,
441
+ agent: Schema.String,
442
+ model: Schema.optional(
443
+ Schema.Struct({
444
+ providerID: ProviderID,
445
+ modelID: ModelID,
446
+ }),
447
+ ),
448
+ command: Schema.optional(Schema.String),
449
+ }).annotate({ identifier: "SubtaskPartInput" })
450
+ export type SubtaskPartInput = Types.DeepMutable<Schema.Schema.Type<typeof SubtaskPartInput>>
451
+
452
+ export const Assistant = Schema.Struct({
453
+ ...messageBase,
454
+ role: Schema.Literal("assistant"),
455
+ time: Schema.Struct({
456
+ created: NonNegativeInt,
457
+ completed: Schema.optional(NonNegativeInt),
458
+ }),
459
+ error: Schema.optional(AssistantErrorSchema),
460
+ parentID: MessageID,
461
+ modelID: ModelID,
462
+ providerID: ProviderID,
463
+ /**
464
+ * @deprecated
465
+ */
466
+ mode: Schema.String,
467
+ agent: Schema.String,
468
+ path: Schema.Struct({
469
+ cwd: Schema.String,
470
+ root: Schema.String,
471
+ }),
472
+ summary: Schema.optional(Schema.Boolean),
473
+ cost: Schema.Finite,
474
+ tokens: Schema.Struct({
475
+ total: Schema.optional(Schema.Finite),
476
+ input: Schema.Finite,
477
+ output: Schema.Finite,
478
+ reasoning: Schema.Finite,
479
+ cache: Schema.Struct({
480
+ read: Schema.Finite,
481
+ write: Schema.Finite,
482
+ }),
483
+ }),
484
+ structured: Schema.optional(Schema.Any),
485
+ variant: Schema.optional(Schema.String),
486
+ finish: Schema.optional(Schema.String),
487
+ }).annotate({ identifier: "AssistantMessage" })
488
+ export type Assistant = Omit<Types.DeepMutable<Schema.Schema.Type<typeof Assistant>>, "error"> & {
489
+ error?: AssistantError
490
+ }
491
+
492
+ export const Info = Schema.Union([User, Assistant]).annotate({ discriminator: "role", identifier: "Message" })
493
+ export type Info = User | Assistant
494
+
495
+ const UpdatedEventSchema = Schema.Struct({
496
+ sessionID: SessionID,
497
+ info: Info,
498
+ })
499
+
500
+ const RemovedEventSchema = Schema.Struct({
501
+ sessionID: SessionID,
502
+ messageID: MessageID,
503
+ })
504
+
505
+ const PartUpdatedEventSchema = Schema.Struct({
506
+ sessionID: SessionID,
507
+ part: Part,
508
+ time: NonNegativeInt,
509
+ })
510
+
511
+ const PartRemovedEventSchema = Schema.Struct({
512
+ sessionID: SessionID,
513
+ messageID: MessageID,
514
+ partID: PartID,
515
+ })
516
+
517
+ export const Event = {
518
+ Updated: SyncEvent.define({
519
+ type: "message.updated",
520
+ version: 1,
521
+ aggregate: "sessionID",
522
+ schema: UpdatedEventSchema,
523
+ }),
524
+ Removed: SyncEvent.define({
525
+ type: "message.removed",
526
+ version: 1,
527
+ aggregate: "sessionID",
528
+ schema: RemovedEventSchema,
529
+ }),
530
+ PartUpdated: SyncEvent.define({
531
+ type: "message.part.updated",
532
+ version: 1,
533
+ aggregate: "sessionID",
534
+ schema: PartUpdatedEventSchema,
535
+ }),
536
+ PartDelta: BusEvent.define(
537
+ "message.part.delta",
538
+ Schema.Struct({
539
+ sessionID: SessionID,
540
+ messageID: MessageID,
541
+ partID: PartID,
542
+ field: Schema.String,
543
+ delta: Schema.String,
544
+ }),
545
+ ),
546
+ PartRemoved: SyncEvent.define({
547
+ type: "message.part.removed",
548
+ version: 1,
549
+ aggregate: "sessionID",
550
+ schema: PartRemovedEventSchema,
551
+ }),
552
+ }
553
+
554
+ export const WithParts = Schema.Struct({
555
+ info: Info,
556
+ parts: Schema.Array(Part),
557
+ })
558
+ export type WithParts = {
559
+ info: Info
560
+ parts: Part[]
561
+ }
562
+
563
+ const Cursor = Schema.Struct({
564
+ id: MessageID,
565
+ time: Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)),
566
+ })
567
+ type Cursor = typeof Cursor.Type
568
+
569
+ const decodeCursor = Schema.decodeUnknownSync(Cursor)
570
+
571
+ export const cursor = {
572
+ encode(input: Cursor) {
573
+ return Buffer.from(JSON.stringify(input)).toString("base64url")
574
+ },
575
+ decode(input: string) {
576
+ return decodeCursor(JSON.parse(Buffer.from(input, "base64url").toString("utf8")))
577
+ },
578
+ }
579
+
580
+ const info = (row: typeof MessageTable.$inferSelect) =>
581
+ ({
582
+ ...row.data,
583
+ id: row.id,
584
+ sessionID: row.session_id,
585
+ }) as Info
586
+
587
+ const part = (row: typeof PartTable.$inferSelect) =>
588
+ ({
589
+ ...row.data,
590
+ id: row.id,
591
+ sessionID: row.session_id,
592
+ messageID: row.message_id,
593
+ }) as Part
594
+
595
+ const older = (row: Cursor) =>
596
+ or(lt(MessageTable.time_created, row.time), and(eq(MessageTable.time_created, row.time), lt(MessageTable.id, row.id)))
597
+
598
+ function hydrate(rows: (typeof MessageTable.$inferSelect)[]) {
599
+ const ids = rows.map((row) => row.id)
600
+ const partByMessage = new Map<string, Part[]>()
601
+ if (ids.length > 0) {
602
+ const partRows = Database.use((db) =>
603
+ db
604
+ .select()
605
+ .from(PartTable)
606
+ .where(inArray(PartTable.message_id, ids))
607
+ .orderBy(PartTable.message_id, PartTable.id)
608
+ .all(),
609
+ )
610
+ for (const row of partRows) {
611
+ const next = part(row)
612
+ const list = partByMessage.get(row.message_id)
613
+ if (list) list.push(next)
614
+ else partByMessage.set(row.message_id, [next])
615
+ }
616
+ }
617
+
618
+ return rows.map((row) => ({
619
+ info: info(row),
620
+ parts: partByMessage.get(row.id) ?? [],
621
+ }))
622
+ }
623
+
624
+ function providerMeta(metadata: Record<string, any> | undefined) {
625
+ if (!metadata) return undefined
626
+ const { providerExecuted: _, ...rest } = metadata
627
+ return Object.keys(rest).length > 0 ? rest : undefined
628
+ }
629
+
630
+ export const toModelMessagesEffect = Effect.fnUntraced(function* (
631
+ input: WithParts[],
632
+ model: Provider.Model,
633
+ options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
634
+ ) {
635
+ const result: UIMessage[] = []
636
+ const toolNames = new Set<string>()
637
+ // Track media from tool results that need to be injected as user messages
638
+ // for providers that don't support that media type in tool results.
639
+ //
640
+ // OpenAI-compatible APIs only support string content in tool results, so we need
641
+ // to extract media and inject as user messages. Some SDKs only support a subset
642
+ // of media in tool results; e.g. Bedrock supports images but not PDFs there.
643
+ //
644
+ // Only apply this workaround if the model actually supports that media input -
645
+ // otherwise unsupportedParts() will turn it into a user-visible error.
646
+ const supportsMediaInToolResult = (attachment: { mime: string }) => {
647
+ if (model.api.npm === "@ai-sdk/anthropic") return true
648
+ if (model.api.npm === "@ai-sdk/openai") return true
649
+ if (model.api.npm === "@ai-sdk/amazon-bedrock") return attachment.mime.startsWith("image/")
650
+ if (model.api.npm === "@ai-sdk/google-vertex/anthropic") return true
651
+ if (model.api.npm === "@ai-sdk/google") {
652
+ const id = model.api.id.toLowerCase()
653
+ return id.includes("gemini-3") && !id.includes("gemini-2")
654
+ }
655
+ return false
656
+ }
657
+
658
+ const toModelOutput = (options: { toolCallId: string; input: unknown; output: unknown }) => {
659
+ const output = options.output
660
+ if (typeof output === "string") {
661
+ return { type: "text", value: output }
662
+ }
663
+
664
+ if (typeof output === "object") {
665
+ const outputObject = output as {
666
+ text: string
667
+ attachments?: Array<{ mime: string; url: string }>
668
+ }
669
+ const attachments = (outputObject.attachments ?? []).filter((attachment) => {
670
+ return attachment.url.startsWith("data:") && attachment.url.includes(",")
671
+ })
672
+
673
+ return {
674
+ type: "content",
675
+ value: [
676
+ ...(outputObject.text ? [{ type: "text", text: outputObject.text }] : []),
677
+ ...attachments.map((attachment) => ({
678
+ type: "media",
679
+ mediaType: attachment.mime,
680
+ data: iife(() => {
681
+ const commaIndex = attachment.url.indexOf(",")
682
+ return commaIndex === -1 ? attachment.url : attachment.url.slice(commaIndex + 1)
683
+ }),
684
+ })),
685
+ ],
686
+ }
687
+ }
688
+
689
+ return { type: "json", value: output as never }
690
+ }
691
+
692
+ for (const msg of input) {
693
+ if (msg.parts.length === 0) continue
694
+
695
+ if (msg.info.role === "user") {
696
+ const userMessage: UIMessage = {
697
+ id: msg.info.id,
698
+ role: "user",
699
+ parts: [],
700
+ }
701
+ for (const part of msg.parts) {
702
+ // User message parts should never be empty
703
+ if (part.type === "text" && !part.ignored && part.text !== "")
704
+ userMessage.parts.push({
705
+ type: "text",
706
+ text: part.text,
707
+ })
708
+ // text/plain and directory files are converted into text parts, ignore them
709
+ if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") {
710
+ if (options?.stripMedia && isMedia(part.mime)) {
711
+ userMessage.parts.push({
712
+ type: "text",
713
+ text: `[Attached ${part.mime}: ${part.filename ?? "file"}]`,
714
+ })
715
+ } else {
716
+ userMessage.parts.push({
717
+ type: "file",
718
+ url: part.url,
719
+ mediaType: part.mime,
720
+ filename: part.filename,
721
+ })
722
+ }
723
+ }
724
+
725
+ if (part.type === "compaction") {
726
+ userMessage.parts.push({
727
+ type: "text",
728
+ text: "What did we do so far?",
729
+ })
730
+ }
731
+ if (part.type === "subtask") {
732
+ userMessage.parts.push({
733
+ type: "text",
734
+ text: "The following tool was executed by the user",
735
+ })
736
+ }
737
+ }
738
+ if (userMessage.parts.length > 0) result.push(userMessage)
739
+ }
740
+
741
+ if (msg.info.role === "assistant") {
742
+ const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
743
+ const media: Array<{ mime: string; url: string; filename?: string }> = []
744
+
745
+ if (
746
+ msg.info.error &&
747
+ !(
748
+ AbortedError.isInstance(msg.info.error) &&
749
+ msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
750
+ )
751
+ ) {
752
+ continue
753
+ }
754
+ const assistantMessage: UIMessage = {
755
+ id: msg.info.id,
756
+ role: "assistant",
757
+ parts: [],
758
+ }
759
+ // Anthropic adaptive thinking can persist assistant turns like:
760
+ // step-start, reasoning(signature), text(""), step-start,
761
+ // reasoning(signature). The empty text part is a structural separator,
762
+ // but it does not carry the signature metadata itself. Dropping it shifts
763
+ // signed thinking positions after step-start splitting/provider regrouping;
764
+ // keeping it as "" is filtered by the AI SDK and rejected by Anthropic.
765
+ // It is unclear whether this shape originates in our stream processing,
766
+ // a proxy, or a lower-level library, but preserving a non-empty separator
767
+ // here is the only safe replay point we have.
768
+ // Use a single space so the separator survives replay without changing
769
+ // the neighboring signed reasoning blocks.
770
+ const hasSignedReasoning = msg.parts.some((part) => {
771
+ if (part.type !== "reasoning") return false
772
+ return part.metadata?.anthropic?.signature != null
773
+ })
774
+ for (const part of msg.parts) {
775
+ if (part.type === "text") {
776
+ const text = part.text === "" && hasSignedReasoning ? " " : part.text
777
+ assistantMessage.parts.push({
778
+ type: "text",
779
+ text,
780
+ ...(differentModel ? {} : { providerMetadata: part.metadata }),
781
+ })
782
+ }
783
+ if (part.type === "step-start")
784
+ assistantMessage.parts.push({
785
+ type: "step-start",
786
+ })
787
+ if (part.type === "tool") {
788
+ toolNames.add(part.tool)
789
+ if (part.state.status === "completed") {
790
+ const outputText = part.state.time.compacted
791
+ ? "[Old tool result content cleared]"
792
+ : truncateToolOutput(part.state.output, options?.toolOutputMaxChars)
793
+ const attachments = part.state.time.compacted || options?.stripMedia ? [] : (part.state.attachments ?? [])
794
+
795
+ // For providers that don't support media in tool results, extract media files
796
+ // (images, PDFs) to be sent as a separate user message
797
+ const mediaAttachments = attachments.filter((a) => isMedia(a.mime))
798
+ const extractedMedia = mediaAttachments.filter((a) => !supportsMediaInToolResult(a))
799
+ if (extractedMedia.length > 0) {
800
+ media.push(...extractedMedia)
801
+ }
802
+ const finalAttachments = attachments.filter((a) => !isMedia(a.mime) || supportsMediaInToolResult(a))
803
+
804
+ const output =
805
+ finalAttachments.length > 0
806
+ ? {
807
+ text: outputText,
808
+ attachments: finalAttachments,
809
+ }
810
+ : outputText
811
+
812
+ assistantMessage.parts.push({
813
+ type: ("tool-" + part.tool) as `tool-${string}`,
814
+ state: "output-available",
815
+ toolCallId: part.callID,
816
+ input: part.state.input,
817
+ output,
818
+ ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
819
+ ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
820
+ })
821
+ }
822
+ if (part.state.status === "error") {
823
+ const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined
824
+ if (typeof output === "string") {
825
+ assistantMessage.parts.push({
826
+ type: ("tool-" + part.tool) as `tool-${string}`,
827
+ state: "output-available",
828
+ toolCallId: part.callID,
829
+ input: part.state.input,
830
+ output,
831
+ ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
832
+ ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
833
+ })
834
+ } else {
835
+ assistantMessage.parts.push({
836
+ type: ("tool-" + part.tool) as `tool-${string}`,
837
+ state: "output-error",
838
+ toolCallId: part.callID,
839
+ input: part.state.input,
840
+ errorText: part.state.error,
841
+ ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
842
+ ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
843
+ })
844
+ }
845
+ }
846
+ // Handle pending/running tool calls to prevent dangling tool_use blocks
847
+ // Anthropic/Claude APIs require every tool_use to have a corresponding tool_result
848
+ if (part.state.status === "pending" || part.state.status === "running")
849
+ assistantMessage.parts.push({
850
+ type: ("tool-" + part.tool) as `tool-${string}`,
851
+ state: "output-error",
852
+ toolCallId: part.callID,
853
+ input: part.state.input,
854
+ errorText: "[Tool execution was interrupted]",
855
+ ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}),
856
+ ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }),
857
+ })
858
+ }
859
+ if (part.type === "reasoning") {
860
+ if (differentModel) {
861
+ if (part.text.trim().length > 0)
862
+ assistantMessage.parts.push({
863
+ type: "text",
864
+ text: part.text,
865
+ })
866
+ continue
867
+ }
868
+ assistantMessage.parts.push({
869
+ type: "reasoning",
870
+ text: part.text,
871
+ providerMetadata: part.metadata,
872
+ })
873
+ }
874
+ }
875
+ if (assistantMessage.parts.length > 0) {
876
+ result.push(assistantMessage)
877
+ // Inject pending media as a user message for providers that don't support
878
+ // media (images, PDFs) in tool results
879
+ if (media.length > 0) {
880
+ result.push({
881
+ id: MessageID.ascending(),
882
+ role: "user",
883
+ parts: [
884
+ {
885
+ type: "text" as const,
886
+ text: SYNTHETIC_ATTACHMENT_PROMPT,
887
+ },
888
+ ...media.map((attachment) => ({
889
+ type: "file" as const,
890
+ url: attachment.url,
891
+ mediaType: attachment.mime,
892
+ filename: attachment.filename,
893
+ })),
894
+ ],
895
+ })
896
+ }
897
+ }
898
+ }
899
+ }
900
+
901
+ const tools = Object.fromEntries(Array.from(toolNames).map((toolName) => [toolName, { toModelOutput }]))
902
+
903
+ return yield* Effect.promise(() =>
904
+ convertToModelMessages(
905
+ result.filter((msg) => msg.parts.some((part) => part.type !== "step-start")),
906
+ {
907
+ //@ts-expect-error (convertToModelMessages expects a ToolSet but only actually needs tools[name]?.toModelOutput)
908
+ tools,
909
+ },
910
+ ),
911
+ )
912
+ })
913
+
914
+ export function toModelMessages(
915
+ input: WithParts[],
916
+ model: Provider.Model,
917
+ options?: { stripMedia?: boolean; toolOutputMaxChars?: number },
918
+ ): Promise<ModelMessage[]> {
919
+ return Effect.runPromise(toModelMessagesEffect(input, model, options).pipe(Effect.provide(EffectLogger.layer)))
920
+ }
921
+
922
+ export const page = Effect.fn("MessageV2.page")(function* (input: {
923
+ sessionID: SessionID
924
+ limit: number
925
+ before?: string
926
+ }) {
927
+ const before = input.before ? cursor.decode(input.before) : undefined
928
+ const where = before
929
+ ? and(eq(MessageTable.session_id, input.sessionID), older(before))
930
+ : eq(MessageTable.session_id, input.sessionID)
931
+ const rows = Database.use((db) =>
932
+ db
933
+ .select()
934
+ .from(MessageTable)
935
+ .where(where)
936
+ .orderBy(desc(MessageTable.time_created), desc(MessageTable.id))
937
+ .limit(input.limit + 1)
938
+ .all(),
939
+ )
940
+ if (rows.length === 0) {
941
+ const row = Database.use((db) =>
942
+ db.select({ id: SessionTable.id }).from(SessionTable).where(eq(SessionTable.id, input.sessionID)).get(),
943
+ )
944
+ if (!row) return yield* new NotFoundError({ message: `Session not found: ${input.sessionID}` })
945
+ return {
946
+ items: [] as WithParts[],
947
+ more: false,
948
+ }
949
+ }
950
+
951
+ const more = rows.length > input.limit
952
+ const slice = more ? rows.slice(0, input.limit) : rows
953
+ const items = hydrate(slice)
954
+ items.reverse()
955
+ const tail = slice.at(-1)
956
+ return {
957
+ items,
958
+ more,
959
+ cursor: more && tail ? cursor.encode({ id: tail.id, time: tail.time_created }) : undefined,
960
+ }
961
+ })
962
+
963
+ export function* stream(sessionID: SessionID) {
964
+ const size = 50
965
+ let before: string | undefined
966
+ while (true) {
967
+ const next = Effect.runSync(
968
+ page({ sessionID, limit: size, before }).pipe(
969
+ Effect.catchIf(NotFoundError.isInstance, () =>
970
+ Effect.succeed({ items: [] as WithParts[], more: false, cursor: undefined }),
971
+ ),
972
+ ),
973
+ )
974
+ if (next.items.length === 0) break
975
+ for (let i = next.items.length - 1; i >= 0; i--) {
976
+ yield next.items[i]
977
+ }
978
+ if (!next.more || !next.cursor) break
979
+ before = next.cursor
980
+ }
981
+ }
982
+
983
+ export function parts(message_id: MessageID) {
984
+ const rows = Database.use((db) =>
985
+ db.select().from(PartTable).where(eq(PartTable.message_id, message_id)).orderBy(PartTable.id).all(),
986
+ )
987
+ return rows.map(
988
+ (row) =>
989
+ ({
990
+ ...row.data,
991
+ id: row.id,
992
+ sessionID: row.session_id,
993
+ messageID: row.message_id,
994
+ }) as Part,
995
+ )
996
+ }
997
+
998
+ export const get = Effect.fn("MessageV2.get")(function* (input: { sessionID: SessionID; messageID: MessageID }) {
999
+ const row = Database.use((db) =>
1000
+ db
1001
+ .select()
1002
+ .from(MessageTable)
1003
+ .where(and(eq(MessageTable.id, input.messageID), eq(MessageTable.session_id, input.sessionID)))
1004
+ .get(),
1005
+ )
1006
+ if (!row) return yield* new NotFoundError({ message: `Message not found: ${input.messageID}` })
1007
+ return {
1008
+ info: info(row),
1009
+ parts: parts(input.messageID),
1010
+ }
1011
+ })
1012
+
1013
+ export function filterCompacted(msgs: Iterable<WithParts>) {
1014
+ const result = [] as WithParts[]
1015
+ const completed = new Set<string>()
1016
+ let retain: MessageID | undefined
1017
+ for (const msg of msgs) {
1018
+ result.push(msg)
1019
+ if (retain) {
1020
+ if (msg.info.id === retain) break
1021
+ continue
1022
+ }
1023
+ if (msg.info.role === "user" && completed.has(msg.info.id)) {
1024
+ const part = msg.parts.find((item): item is CompactionPart => item.type === "compaction")
1025
+ if (!part) continue
1026
+ if (!part.tail_start_id) break
1027
+ retain = part.tail_start_id
1028
+ if (msg.info.id === retain) break
1029
+ continue
1030
+ }
1031
+ if (msg.info.role === "user" && completed.has(msg.info.id) && msg.parts.some((part) => part.type === "compaction"))
1032
+ break
1033
+ if (msg.info.role === "assistant" && msg.info.summary && msg.info.finish && !msg.info.error)
1034
+ completed.add(msg.info.parentID)
1035
+ }
1036
+ result.reverse()
1037
+ const compactionIndex = result.findLastIndex(
1038
+ (msg) =>
1039
+ msg.info.role === "user" &&
1040
+ msg.parts.some((item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined),
1041
+ )
1042
+ const compaction = result[compactionIndex]
1043
+ const part = compaction?.parts.find(
1044
+ (item): item is CompactionPart => item.type === "compaction" && item.tail_start_id !== undefined,
1045
+ )
1046
+ const summaryIndex = compaction
1047
+ ? result.findIndex(
1048
+ (msg, index) =>
1049
+ index > compactionIndex &&
1050
+ msg.info.role === "assistant" &&
1051
+ msg.info.summary &&
1052
+ msg.info.parentID === compaction.info.id,
1053
+ )
1054
+ : -1
1055
+ const tailIndex = part?.tail_start_id ? result.findIndex((msg) => msg.info.id === part.tail_start_id) : -1
1056
+ if (tailIndex >= 0 && tailIndex < compactionIndex && summaryIndex > compactionIndex) {
1057
+ return [
1058
+ ...result.slice(compactionIndex, summaryIndex + 1),
1059
+ ...result.slice(tailIndex, compactionIndex),
1060
+ ...result.slice(summaryIndex + 1),
1061
+ ]
1062
+ }
1063
+ return result
1064
+ }
1065
+
1066
+ export const filterCompactedEffect = Effect.fnUntraced(function* (sessionID: SessionID) {
1067
+ return filterCompacted(stream(sessionID))
1068
+ })
1069
+
1070
+ // filterCompacted reorders messages for model consumption
1071
+ // ([compaction-user, summary, ...retained tail..., continue-user]), so array
1072
+ // position is not chronological. Derive each binding by max id (MessageID
1073
+ // is monotonic via MessageID.ascending) so a pre-compaction overflowing tail
1074
+ // assistant doesn't get mistaken for the most recent turn. tasks are
1075
+ // compaction/subtask parts attached to user messages newer than the latest
1076
+ // finished assistant — i.e. unprocessed work.
1077
+ export function latest(msgs: WithParts[]) {
1078
+ let user: User | undefined
1079
+ let assistant: Assistant | undefined
1080
+ let finished: Assistant | undefined
1081
+ for (const msg of msgs) {
1082
+ const info = msg.info
1083
+ if (info.role === "user" && (!user || info.id > user.id)) user = info
1084
+ if (info.role === "assistant" && (!assistant || info.id > assistant.id)) assistant = info
1085
+ if (info.role === "assistant" && info.finish && (!finished || info.id > finished.id)) finished = info
1086
+ }
1087
+ const tasks = msgs.flatMap((m) =>
1088
+ finished && m.info.id <= finished.id
1089
+ ? []
1090
+ : m.parts.filter((p): p is CompactionPart | SubtaskPart => p.type === "compaction" || p.type === "subtask"),
1091
+ )
1092
+ return { user, assistant, finished, tasks }
1093
+ }
1094
+
1095
+ export function fromError(
1096
+ e: unknown,
1097
+ ctx: { providerID: ProviderID; aborted?: boolean },
1098
+ ): NonNullable<Assistant["error"]> {
1099
+ switch (true) {
1100
+ case e instanceof DOMException && e.name === "AbortError":
1101
+ return new AbortedError(
1102
+ { message: e.message },
1103
+ {
1104
+ cause: e,
1105
+ },
1106
+ ).toObject()
1107
+ case OutputLengthError.isInstance(e):
1108
+ return e
1109
+ case LoadAPIKeyError.isInstance(e):
1110
+ return new AuthError(
1111
+ {
1112
+ providerID: ctx.providerID,
1113
+ message: e.message,
1114
+ },
1115
+ { cause: e },
1116
+ ).toObject()
1117
+ case (e as SystemError)?.code === "ECONNRESET":
1118
+ return new APIError(
1119
+ {
1120
+ message: "Connection reset by server",
1121
+ isRetryable: true,
1122
+ metadata: {
1123
+ code: (e as SystemError).code ?? "",
1124
+ syscall: (e as SystemError).syscall ?? "",
1125
+ message: (e as SystemError).message ?? "",
1126
+ },
1127
+ },
1128
+ { cause: e },
1129
+ ).toObject()
1130
+ case e instanceof Error && (e as FetchDecompressionError).code === "ZlibError":
1131
+ if (ctx.aborted) {
1132
+ return new AbortedError({ message: e.message }, { cause: e }).toObject()
1133
+ }
1134
+ return new APIError(
1135
+ {
1136
+ message: "Response decompression failed",
1137
+ isRetryable: true,
1138
+ metadata: {
1139
+ code: (e as FetchDecompressionError).code,
1140
+ message: e.message,
1141
+ },
1142
+ },
1143
+ { cause: e },
1144
+ ).toObject()
1145
+ case APICallError.isInstance(e):
1146
+ const parsed = ProviderError.parseAPICallError({
1147
+ providerID: ctx.providerID,
1148
+ error: e,
1149
+ })
1150
+ if (parsed.type === "context_overflow") {
1151
+ return new ContextOverflowError(
1152
+ {
1153
+ message: parsed.message,
1154
+ responseBody: parsed.responseBody,
1155
+ },
1156
+ { cause: e },
1157
+ ).toObject()
1158
+ }
1159
+
1160
+ return new APIError(
1161
+ {
1162
+ message: parsed.message,
1163
+ statusCode: parsed.statusCode,
1164
+ isRetryable: parsed.isRetryable,
1165
+ responseHeaders: parsed.responseHeaders,
1166
+ responseBody: parsed.responseBody,
1167
+ metadata: parsed.metadata,
1168
+ },
1169
+ { cause: e },
1170
+ ).toObject()
1171
+ case e instanceof Error:
1172
+ return new NamedError.Unknown({ message: errorMessage(e) }, { cause: e }).toObject()
1173
+ default:
1174
+ try {
1175
+ const parsed = ProviderError.parseStreamError(e)
1176
+ if (parsed) {
1177
+ if (parsed.type === "context_overflow") {
1178
+ return new ContextOverflowError(
1179
+ {
1180
+ message: parsed.message,
1181
+ responseBody: parsed.responseBody,
1182
+ },
1183
+ { cause: e },
1184
+ ).toObject()
1185
+ }
1186
+ return new APIError(
1187
+ {
1188
+ message: parsed.message,
1189
+ isRetryable: parsed.isRetryable,
1190
+ responseBody: parsed.responseBody,
1191
+ },
1192
+ {
1193
+ cause: e,
1194
+ },
1195
+ ).toObject()
1196
+ }
1197
+ } catch {}
1198
+ return new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e }).toObject()
1199
+ }
1200
+ }
1201
+
1202
+ export * as MessageV2 from "./message-v2"