@cortexkit/opencode-magic-context 0.49.34 → 0.49.35

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 (220) hide show
  1. package/dist/auto-search-worker.js +11 -6
  2. package/dist/chunk-embedding-write-worker.js +16 -14
  3. package/dist/{compaction-marker-candidates-31h86ph0.js → compaction-marker-candidates-g0p67ced.js} +3 -2
  4. package/dist/compaction-marker-candidates-worker.js +1 -1
  5. package/dist/{compartment-chunk-embedding-5q5gj67r.js → compartment-chunk-embedding-y4136are.js} +8 -7
  6. package/dist/{conflict-warning-hook-xgskea1v.js → conflict-warning-hook-hhw8z5ak.js} +9 -8
  7. package/dist/{context-p3xrarzt.js → context-fk8e3h9p.js} +1400 -701
  8. package/dist/{derive-budgets-pqybnx09.js → derive-budgets-htj16n4x.js} +3 -2
  9. package/dist/embedding-backfill-q6n72sy0.js +27 -0
  10. package/dist/embedding-worker-process.js +2 -1
  11. package/dist/embedding-worker.js +5 -4
  12. package/dist/expand-attachments-h33jbgyz.js +21 -0
  13. package/dist/features/magic-context/auto-search-diagnostics.d.ts +21 -0
  14. package/dist/features/magic-context/auto-search-diagnostics.d.ts.map +1 -0
  15. package/dist/features/magic-context/compartment-chunk-embedding.d.ts +1 -1
  16. package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
  17. package/dist/features/magic-context/search.d.ts +4 -0
  18. package/dist/features/magic-context/search.d.ts.map +1 -1
  19. package/dist/features/magic-context/store-generation-rebase-labels.d.ts +6 -0
  20. package/dist/features/magic-context/store-generation-rebase-labels.d.ts.map +1 -0
  21. package/dist/features/magic-context/store-generation-rebase.d.ts.map +1 -1
  22. package/dist/{history-owner-36wp3mkb.js → history-owner-0z620wgz.js} +3 -2
  23. package/dist/hooks/magic-context/apply-operations.d.ts +20 -3
  24. package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
  25. package/dist/hooks/magic-context/auto-search-observation.d.ts +13 -0
  26. package/dist/hooks/magic-context/auto-search-observation.d.ts.map +1 -0
  27. package/dist/hooks/magic-context/auto-search-runner.d.ts.map +1 -1
  28. package/dist/hooks/magic-context/auto-search-worker-client.d.ts +5 -1
  29. package/dist/hooks/magic-context/auto-search-worker-client.d.ts.map +1 -1
  30. package/dist/hooks/magic-context/compartment-commit-hashes.d.ts +4 -0
  31. package/dist/hooks/magic-context/compartment-commit-hashes.d.ts.map +1 -0
  32. package/dist/hooks/magic-context/compartment-runner-validation.d.ts +1 -0
  33. package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
  34. package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
  35. package/dist/hooks/magic-context/final-wire-completeness.d.ts +15 -0
  36. package/dist/hooks/magic-context/final-wire-completeness.d.ts.map +1 -0
  37. package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
  38. package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts +1 -0
  39. package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
  40. package/dist/hooks/magic-context/read-session-chunk.d.ts +11 -0
  41. package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
  42. package/dist/hooks/magic-context/read-session-formatting.d.ts +2 -0
  43. package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
  44. package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
  45. package/dist/hooks/magic-context/transform.d.ts.map +1 -1
  46. package/dist/{index-as2amvn3.js → index-0c7p8mq5.js} +6 -531
  47. package/dist/{index-51rdvtkz.js → index-1rhgxqs8.js} +2 -2
  48. package/dist/index-2amn2s73.js +123 -0
  49. package/dist/{index-c7phnbvk.js → index-2p37txes.js} +1 -1
  50. package/dist/index-2qbtqg1w.js +529 -0
  51. package/dist/{index-waktfbjy.js → index-2v6nvy3k.js} +4 -4
  52. package/dist/{index-ftr84kkv.js → index-3d3872c6.js} +7 -5
  53. package/dist/{index-0677w9sd.js → index-3j9t1v4n.js} +55 -21
  54. package/dist/{index-atekmqef.js → index-4bgk5ztz.js} +363 -5499
  55. package/dist/{index-b88dwwrh.js → index-4xyjr62e.js} +1 -1
  56. package/dist/index-66a50f3e.js +777 -0
  57. package/dist/{index-04z754rd.js → index-87ctnzz9.js} +33 -5
  58. package/dist/{index-xrnsygfq.js → index-9wnbnx8f.js} +1 -1
  59. package/dist/{index-97spytxq.js → index-b6xz3qbe.js} +246 -420
  60. package/dist/{index-7g9dr8gy.js → index-brg2w6qc.js} +113 -133
  61. package/dist/{index-spwjbkc7.js → index-dnqqqc5f.js} +1 -1
  62. package/dist/index-dy7w6wm4.js +194 -0
  63. package/dist/{index-m1x9ghk5.js → index-ezv6dz3y.js} +37 -7
  64. package/dist/index-f297z1n8.js +4557 -0
  65. package/dist/{index-qgh1qwjj.js → index-f9b3vtt1.js} +1 -1
  66. package/dist/{index-v15ne3a3.js → index-faxb7mck.js} +2 -2
  67. package/dist/{index-rrv26qjz.js → index-fw42ms3m.js} +1 -1
  68. package/dist/{index-t5pck3ss.js → index-fxbk7gk3.js} +1 -1
  69. package/dist/{index-wfybj1dw.js → index-g4t7850m.js} +2 -2
  70. package/dist/{index-fm36bjv4.js → index-gyaa159j.js} +18 -276
  71. package/dist/{index-4pnsgvfk.js → index-hacf2tyx.js} +1 -1
  72. package/dist/{index-s1n8dd2n.js → index-jkanybgq.js} +2 -2
  73. package/dist/{index-2743x98y.js → index-jn0ddrhp.js} +29 -138
  74. package/dist/{index-h7tgn49j.js → index-jrfre9ah.js} +5 -222
  75. package/dist/{index-kfyb3k6n.js → index-jz5bk7xd.js} +2 -2
  76. package/dist/{index-np3rgfw4.js → index-ksv0a31x.js} +3 -3
  77. package/dist/{index-7kkphb6c.js → index-nj360hcs.js} +4 -4
  78. package/dist/{index-gkee6p4g.js → index-p56dszfp.js} +3 -3
  79. package/dist/{index-9gpw8hrm.js → index-pq0dpwrc.js} +4 -4
  80. package/dist/{index-rfxzazzy.js → index-pswsyf1w.js} +4 -4
  81. package/dist/{index-y1qwzvxn.js → index-pztqab6k.js} +2 -2
  82. package/dist/{index-rverk2vz.js → index-qncw9hey.js} +1 -1
  83. package/dist/index-qx8j6kds.js +270 -0
  84. package/dist/{index-kq5w68dp.js → index-txs8tce6.js} +1 -1
  85. package/dist/index-vzhhr1ht.js +532 -0
  86. package/dist/index-wh9fxds5.js +265 -0
  87. package/dist/{index-hbx7m8he.js → index-x2mrq5xv.js} +170 -48
  88. package/dist/{index-07vskjn8.js → index-xwwsbsd3.js} +15 -13
  89. package/dist/index-z4p0ry94.js +222 -0
  90. package/dist/index-z9zygwse.js +89 -0
  91. package/dist/{index-v7287tzq.js → index-zhqwda72.js} +19 -8
  92. package/dist/index.js +83 -72
  93. package/dist/{logger-94kew5wm.js → logger-qwnbcfj9.js} +2 -1
  94. package/dist/{message-fts-rowid-map-va36knh4.js → message-fts-rowid-map-xg7hr0mk.js} +5 -4
  95. package/dist/{message-index-bounded-qh91fdr3.js → message-index-bounded-narw7bnz.js} +12 -11
  96. package/dist/{message-time-backfill-4qva0b4f.js → message-time-backfill-39bm498k.js} +3 -2
  97. package/dist/migration-worker.js +13 -11
  98. package/dist/{project-embedding-registry-6yex0r9f.js → project-embedding-registry-tdz7985n.js} +13 -11
  99. package/dist/raw-ordinal-warmup-worker.js +12 -7
  100. package/dist/{read-session-chunk-kaghtr75.js → read-session-chunk-vmg1zjg0.js} +15 -10
  101. package/dist/{read-session-raw-b2sd67v4.js → read-session-raw-mncmzbm9.js} +3 -2
  102. package/dist/recomp-orchestrator-qyyhnc9b.js +68 -0
  103. package/dist/render-c5p0k846.js +43 -0
  104. package/dist/{safe-notification-target-mkr9yzvr.js → safe-notification-target-wztd7bgk.js} +3 -2
  105. package/dist/{search-archive-1kw2rp19.js → search-archive-0ny7p3bw.js} +13 -11
  106. package/dist/{search-p684j8ww.js → search-f6hwvcxk.js} +26 -23
  107. package/dist/{send-session-notification-vz86nt79.js → send-session-notification-0gwcz3fw.js} +7 -6
  108. package/dist/{storage-tags-6hvkk01c.js → storage-tags-zx1023km.js} +5 -4
  109. package/dist/store-620rjzt9.js +21 -0
  110. package/dist/store-reader-yh19h38c.js +14 -0
  111. package/dist/tools/ctx-expand/render.d.ts +5 -3
  112. package/dist/tools/ctx-expand/render.d.ts.map +1 -1
  113. package/dist/tools/ctx-expand/search-archive.d.ts.map +1 -1
  114. package/dist/tools/ctx-expand/tools.d.ts +2 -0
  115. package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
  116. package/dist/tools/ctx-expand/v2-archive-reader.d.ts +33 -0
  117. package/dist/tools/ctx-expand/v2-archive-reader.d.ts.map +1 -0
  118. package/dist/{transform-postprocess-phase-ph38mhrs.js → transform-postprocess-phase-807ff6t2.js} +36 -30
  119. package/dist/v2/auto-search-worker.js +11 -6
  120. package/dist/v2/chunk-embedding-write-worker.js +16 -14
  121. package/dist/v2/{compaction-marker-candidates-m4vmye3w.js → compaction-marker-candidates-t1aq6gsj.js} +3 -2
  122. package/dist/v2/compaction-marker-candidates-worker.js +1 -1
  123. package/dist/v2/{compartment-chunk-embedding-rc31e6md.js → compartment-chunk-embedding-ncsk0r9e.js} +8 -7
  124. package/dist/v2/{context-s60hcatx.js → context-znqfkap4.js} +1725 -1095
  125. package/dist/v2/{derive-budgets-knf763tt.js → derive-budgets-sa0v7bz0.js} +4 -3
  126. package/dist/v2/embedding-backfill-11j425e0.js +26 -0
  127. package/dist/v2/embedding-worker-process.js +2 -1
  128. package/dist/v2/embedding-worker.js +5 -4
  129. package/dist/v2/expand-attachments-h33jbgyz.js +21 -0
  130. package/dist/v2/fold/idle.d.ts +15 -0
  131. package/dist/v2/fold/idle.d.ts.map +1 -0
  132. package/dist/v2/fold/native-replay-v3.d.ts +68 -0
  133. package/dist/v2/fold/native-replay-v3.d.ts.map +1 -0
  134. package/dist/v2/fold/native-replay.d.ts +58 -2
  135. package/dist/v2/fold/native-replay.d.ts.map +1 -1
  136. package/dist/v2/fold/policy.d.ts +2 -1
  137. package/dist/v2/fold/policy.d.ts.map +1 -1
  138. package/dist/v2/{history-owner-ad0crnrn.js → history-owner-fpka43cn.js} +3 -2
  139. package/dist/v2/hooks/context.d.ts.map +1 -1
  140. package/dist/v2/hooks/expand-attachments.d.ts +8 -0
  141. package/dist/v2/hooks/expand-attachments.d.ts.map +1 -0
  142. package/dist/v2/hooks/overflow-recovery.d.ts +45 -0
  143. package/dist/v2/hooks/overflow-recovery.d.ts.map +1 -0
  144. package/dist/v2/hooks/store.d.ts +7 -2
  145. package/dist/v2/hooks/store.d.ts.map +1 -1
  146. package/dist/v2/{logger-p34mjtja.js → logger-4cj6d03m.js} +2 -1
  147. package/dist/v2/{message-fts-rowid-map-78awh03v.js → message-fts-rowid-map-rmk571mb.js} +5 -4
  148. package/dist/v2/{message-index-bounded-4htdr1j6.js → message-index-bounded-620bppw7.js} +12 -11
  149. package/dist/v2/{message-time-backfill-zvthw6xb.js → message-time-backfill-0c4h7sg6.js} +3 -2
  150. package/dist/v2/migration-worker.js +13 -11
  151. package/dist/v2/ordinal-range.fixture.d.ts +7 -0
  152. package/dist/v2/ordinal-range.fixture.d.ts.map +1 -0
  153. package/dist/v2/{project-embedding-registry-1x5qe2fy.js → project-embedding-registry-bm5fe1a8.js} +13 -11
  154. package/dist/v2/raw-ordinal-warmup-worker.js +12 -7
  155. package/dist/v2/{read-session-chunk-1fa9qzwb.js → read-session-chunk-qfwv1k71.js} +15 -10
  156. package/dist/v2/{read-session-raw-5x088amc.js → read-session-raw-q294t8xs.js} +3 -2
  157. package/dist/v2/recomp-orchestrator-fcnjref7.js +67 -0
  158. package/dist/v2/render-98a1qv0b.js +42 -0
  159. package/dist/v2/{safe-notification-target-khnzg57c.js → safe-notification-target-gphhast7.js} +3 -2
  160. package/dist/v2/{search-archive-xfp51s0g.js → search-archive-r0y1bafr.js} +13 -11
  161. package/dist/v2/{search-wf8rqxhm.js → search-z1zqzsmh.js} +25 -22
  162. package/dist/v2/{send-session-notification-x5nzy34v.js → send-session-notification-fst6hkg7.js} +7 -6
  163. package/dist/v2/{server-qvy4qavp.js → server-1xesqm5h.js} +113 -133
  164. package/dist/v2/server-2amn2s73.js +123 -0
  165. package/dist/v2/{server-6g98c227.js → server-3schkz95.js} +4 -4
  166. package/dist/v2/server-468mya5v.js +209 -0
  167. package/dist/v2/server-55v5hpth.js +270 -0
  168. package/dist/v2/server-5jp38p98.js +532 -0
  169. package/dist/v2/{server-qjvk4a9t.js → server-69m30b21.js} +1 -1
  170. package/dist/v2/{server-jw5qe9y8.js → server-7ccxt4nq.js} +14 -216
  171. package/dist/v2/{server-3mz2qh5a.js → server-969cdpmg.js} +1 -1
  172. package/dist/v2/{server-835q39kd.js → server-9k0mjef7.js} +2 -2
  173. package/dist/v2/{server-qc915kw2.js → server-bez34e60.js} +4 -4
  174. package/dist/v2/{server-nxas8v1d.js → server-bpvsvtph.js} +3 -3
  175. package/dist/v2/{server-ab8017gw.js → server-bxpz2dcn.js} +29 -138
  176. package/dist/v2/{server-bsgd4dk1.js → server-dcws5831.js} +2 -2
  177. package/dist/v2/server-ef1mskas.js +777 -0
  178. package/dist/v2/{server-1bw1wcbp.js → server-f0j864ma.js} +1 -1
  179. package/dist/v2/{server-z2wft9zv.js → server-fqj6ddpr.js} +402 -5497
  180. package/dist/v2/{server-grakwy1v.js → server-hnknwaz8.js} +37 -7
  181. package/dist/v2/{server-h1byrhwx.js → server-hsf0qsn5.js} +19 -8
  182. package/dist/v2/{server-6495nmn9.js → server-jb6tjyeb.js} +5 -222
  183. package/dist/v2/{server-2aasmpsp.js → server-kkxzwa6w.js} +4 -4
  184. package/dist/v2/{server-a7x00tw8.js → server-mfn1yf9d.js} +1 -1
  185. package/dist/v2/{server-tkdabx8q.js → server-q0z2q2dx.js} +5 -530
  186. package/dist/v2/{server-ajxhrgjv.js → server-qa74bj91.js} +77 -17
  187. package/dist/v2/server-qf2kzsh2.js +4513 -0
  188. package/dist/v2/{server-gyy4k6mf.js → server-qmmkhz2z.js} +1 -1
  189. package/dist/v2/{server-29qp8nz4.js → server-qxzbmtqt.js} +1 -1
  190. package/dist/v2/{server-m1pfg2d4.js → server-r3chnxs5.js} +4 -4
  191. package/dist/v2/{server-vr4szghm.js → server-rktnzs89.js} +2 -2
  192. package/dist/v2/{server-6vd2n183.js → server-s9xgyyj9.js} +1 -1
  193. package/dist/v2/{server-53eh1r8r.js → server-sd8hvzte.js} +2 -2
  194. package/dist/v2/{server-k3t06fxr.js → server-sefxj8k1.js} +1 -1
  195. package/dist/v2/{server-v77xa5wj.js → server-tpt7bwq8.js} +1 -1
  196. package/dist/v2/{server-awr8qa4c.js → server-ws5exe6q.js} +2 -2
  197. package/dist/v2/server-x4m6xcn4.js +194 -0
  198. package/dist/v2/{server-806rzhs6.js → server-xbtw0p9t.js} +1 -1
  199. package/dist/v2/{server-cqjvxyrp.js → server-xy7w7hrd.js} +2 -2
  200. package/dist/v2/server-ypf1ap0v.js +529 -0
  201. package/dist/v2/server-z4p0ry94.js +222 -0
  202. package/dist/v2/server-z9zygwse.js +89 -0
  203. package/dist/v2/{server-ab1g0wn9.js → server-zzgtgqmd.js} +2 -2
  204. package/dist/v2/server.js +6 -5
  205. package/dist/v2/{storage-tags-18nbaanf.js → storage-tags-19s0ebf0.js} +5 -4
  206. package/dist/v2/store-reader-cgx47zkp.js +14 -0
  207. package/dist/v2/store-reader.d.ts +9 -2
  208. package/dist/v2/store-reader.d.ts.map +1 -1
  209. package/dist/v2/store-vdm8v3jd.js +21 -0
  210. package/dist/v2/v2-archive-reader-t2c0eypn.js +11 -0
  211. package/dist/v2/{wrapup-orchestrator-wxf7n5k6.js → wrapup-orchestrator-aqpqsveg.js} +46 -39
  212. package/dist/v2-archive-reader-tm2eayp2.js +11 -0
  213. package/dist/wrapup-orchestrator-k5yp19a6.js +55 -0
  214. package/package.json +1 -1
  215. package/src/shared/sqlite-transaction-mode-guard.test.ts +2 -0
  216. package/dist/embedding-backfill-0jgtqxen.js +0 -25
  217. package/dist/recomp-orchestrator-0y983w7h.js +0 -62
  218. package/dist/v2/embedding-backfill-f43hd8y6.js +0 -24
  219. package/dist/v2/recomp-orchestrator-7q9nvxte.js +0 -61
  220. package/dist/wrapup-orchestrator-ys2nrkkb.js +0 -49
@@ -0,0 +1,532 @@
1
+ import {
2
+ formatFailClosedBlockingSummary
3
+ } from "./index-wh9fxds5.js";
4
+ import {
5
+ RAW_MESSAGE_TYPES2
6
+ } from "./index-66a50f3e.js";
7
+ import {
8
+ markHostUnservedRow
9
+ } from "./index-z9zygwse.js";
10
+
11
+ // src/v2/fold/restore.ts
12
+ import { fileURLToPath } from "node:url";
13
+
14
+ // src/v2/hooks/storage-notice.ts
15
+ var STORAGE_NOTICE_PREFIX = "[Magic Context storage notice] ";
16
+ function formatStorageRefusalNotice(reason) {
17
+ return `${STORAGE_NOTICE_PREFIX}This turn was not sent to the model. ${formatFailClosedBlockingSummary(reason)}`;
18
+ }
19
+ function formatStorageRecoveryNotice(hiddenWorkRecovered) {
20
+ return hiddenWorkRecovered ? `${STORAGE_NOTICE_PREFIX}Magic Context storage is available again and turns are sent to the model. The historian and dreamer are running again. Restart OpenCode to enable Magic Context's tools.` : `${STORAGE_NOTICE_PREFIX}Magic Context storage is available again and turns are sent to the model. Restart OpenCode to enable Magic Context's tools, historian and dreamer.`;
21
+ }
22
+ function isStorageNoticeText(text) {
23
+ return typeof text === "string" && text.startsWith(STORAGE_NOTICE_PREFIX);
24
+ }
25
+ function hasStorageNoticeShape(message) {
26
+ if (message.role !== "user" || typeof message.id !== "string")
27
+ return false;
28
+ if (message.content.length !== 1)
29
+ return false;
30
+ const part = message.content[0];
31
+ return part?.type === "text" && isStorageNoticeText(part.text);
32
+ }
33
+ async function dropStorageNotices(messages, isAdmittedSynthetic) {
34
+ const drop = new Set;
35
+ for (const message of messages) {
36
+ if (!hasStorageNoticeShape(message))
37
+ continue;
38
+ if (await isAdmittedSynthetic(message.id))
39
+ drop.add(message);
40
+ }
41
+ if (drop.size === 0)
42
+ return 0;
43
+ const kept = messages.filter((message) => !drop.has(message));
44
+ messages.splice(0, messages.length, ...kept);
45
+ return drop.size;
46
+ }
47
+
48
+ // src/v2/fold/restore.ts
49
+ function unavailableAttachmentNote(name, mediaType) {
50
+ return `[Attachment ${name ? `"${name}" ` : ""}(${mediaType}) is not available in this restored history]`;
51
+ }
52
+ var imageMimes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
53
+ function attachmentParts(files, rowID, media) {
54
+ const seen = new Map;
55
+ return files.flatMap((file) => {
56
+ if (imageMimes.has(file.mime) && file.source.type === "inline" && file.mention?.text) {
57
+ const key = JSON.stringify([
58
+ file.mime,
59
+ file.name ?? null,
60
+ file.description ?? null,
61
+ file.mention.text
62
+ ]);
63
+ const payloads = seen.get(key) ?? new Set;
64
+ if (payloads.has(file.data))
65
+ return [];
66
+ payloads.add(file.data);
67
+ seen.set(key, payloads);
68
+ }
69
+ const uri = file.source.type === "uri" ? file.source.uri : undefined;
70
+ const location = uri?.startsWith("file:") ? fileURLToPath(uri) : undefined;
71
+ if (file.mime === "text/plain" || file.mime === "application/x-directory") {
72
+ const directory = file.mime === "application/x-directory";
73
+ return [
74
+ {
75
+ type: "text",
76
+ text: `
77
+
78
+ ${[
79
+ directory ? `Attached directory: ${location ?? file.name ?? uri ?? "directory"}` : `Attached file: ${file.name ?? uri ?? "inline attachment"}`,
80
+ file.description === undefined ? undefined : `Description: ${file.description}`,
81
+ directory && !file.data.length ? undefined : "",
82
+ directory && !file.data.length ? undefined : Buffer.from(file.data, "base64").toString("utf8")
83
+ ].filter((line) => line !== undefined).join(`
84
+ `)}`,
85
+ metadata: {
86
+ attachment: {
87
+ source: file.source,
88
+ name: file.name,
89
+ description: file.description
90
+ }
91
+ }
92
+ }
93
+ ];
94
+ }
95
+ if (!imageMimes.has(file.mime) && file.mime !== "application/pdf")
96
+ return [];
97
+ const metadata = file.description === undefined ? undefined : { description: file.description };
98
+ const located = location ? [{ type: "text", text: `Attached file: ${location}` }] : [];
99
+ if (!media)
100
+ return [
101
+ ...located,
102
+ {
103
+ type: "media",
104
+ mediaType: file.mime,
105
+ data: file.data,
106
+ filename: file.name,
107
+ metadata
108
+ }
109
+ ];
110
+ const asset = media.asset(file.data, file.mime);
111
+ if (typeof asset === "string") {
112
+ media.unavailable({ rowID, name: file.name, mediaType: file.mime, reason: asset });
113
+ return [
114
+ ...located,
115
+ { type: "text", text: unavailableAttachmentNote(file.name, file.mime) }
116
+ ];
117
+ }
118
+ return [...located, { type: "media", media: asset, filename: file.name, metadata }];
119
+ });
120
+ }
121
+ function restoreRow(row, model, media) {
122
+ const data = row.data;
123
+ const make = (role, content, metadata = data.metadata) => ({
124
+ id: row.id,
125
+ role,
126
+ content,
127
+ ...metadata === undefined ? {} : { metadata }
128
+ });
129
+ if (row.type === "user") {
130
+ const skills = data.skills ?? [];
131
+ const content = [
132
+ ...skills.flatMap((skill) => skill.text === undefined ? [] : [{ type: "text", text: skill.text }]),
133
+ ...data.text ? [{ type: "text", text: data.text }] : [],
134
+ ...attachmentParts(data.files ?? [], row.id, media)
135
+ ];
136
+ return content.length ? [
137
+ make("user", content, {
138
+ ...data.metadata ?? {},
139
+ ...data.agents?.length ? { agents: data.agents } : {}
140
+ })
141
+ ] : [];
142
+ }
143
+ if (row.type === "system")
144
+ return [{ role: "system", content: [{ type: "text", text: data.text ?? "" }] }];
145
+ if (row.type === "synthetic" && isStorageNoticeText(data.text))
146
+ return [];
147
+ if (["synthetic", "skill"].includes(row.type))
148
+ return [make("user", [{ type: "text", text: data.text ?? "" }])];
149
+ if (row.type === "location-switched")
150
+ return [
151
+ make("user", [
152
+ {
153
+ type: "text",
154
+ text: `The working directory has been changed to ${data.location.directory}.`
155
+ }
156
+ ])
157
+ ];
158
+ if (row.type === "shell")
159
+ return data.metadata?.background === true ? [] : [
160
+ make("user", [
161
+ {
162
+ type: "text",
163
+ text: `The following shell command was executed by the user:
164
+
165
+ Command:
166
+ ${data.command}
167
+
168
+ Output:
169
+ ${data.output?.output ?? ""}`
170
+ }
171
+ ])
172
+ ];
173
+ if (row.type !== "assistant")
174
+ return [];
175
+ const previousModel = data.model;
176
+ const sameProvider = previousModel?.providerID === model.providerID;
177
+ const sameModel = sameProvider && previousModel?.id === model.id;
178
+ const reuse = sameModel && data.error === undefined;
179
+ const metadata = (state) => state === undefined ? undefined : { [model.providerID]: state };
180
+ const reasoningInterrupted = data.content?.some((part) => part.type === "reasoning" && part.time !== undefined && part.time.completed === undefined);
181
+ const content = [];
182
+ const results = [];
183
+ for (const part of data.content ?? []) {
184
+ let native = "native" in part ? part.native : part.type === "tool" ? part.providerState : part.state;
185
+ if (part.type === "tool" && part.providerResultState !== null && typeof part.providerResultState === "object" && !Array.isArray(part.providerResultState))
186
+ native = {
187
+ ...native !== null && typeof native === "object" && !Array.isArray(native) ? native : {},
188
+ ...part.providerResultState
189
+ };
190
+ if (part.type === "text") {
191
+ if (part.text !== "")
192
+ content.push({
193
+ type: "text",
194
+ text: part.text,
195
+ providerMetadata: reuse ? metadata(native) : undefined
196
+ });
197
+ continue;
198
+ }
199
+ if (part.type === "reasoning") {
200
+ if (part.text !== "" || reuse && !reasoningInterrupted && native !== undefined)
201
+ content.push({
202
+ type: data.error === undefined && !reasoningInterrupted ? "reasoning" : "text",
203
+ text: part.text,
204
+ providerMetadata: reuse && !reasoningInterrupted ? metadata(native) : undefined
205
+ });
206
+ continue;
207
+ }
208
+ const state = part.state;
209
+ const completed = state.status === "completed" || state.status === "error";
210
+ const toolReuse = reuse || sameModel && part.executed === true && completed;
211
+ let input = state.input;
212
+ if (state.status === "streaming" && typeof input === "string") {
213
+ try {
214
+ input = JSON.parse(input);
215
+ } catch {}
216
+ }
217
+ content.push({
218
+ type: "tool-call",
219
+ id: part.id,
220
+ name: part.name,
221
+ input,
222
+ providerExecuted: part.executed,
223
+ providerMetadata: toolReuse ? metadata(native) : undefined
224
+ });
225
+ if (!completed)
226
+ continue;
227
+ const toolContent = state.content ?? [];
228
+ const single = toolContent.length === 1 ? toolContent[0] : undefined;
229
+ const result = {
230
+ type: "tool-result",
231
+ id: part.id,
232
+ name: part.name,
233
+ providerExecuted: part.executed,
234
+ providerMetadata: (part.executed === true ? sameProvider : reuse) ? metadata(native) : undefined,
235
+ result: state.status === "error" ? { error: state.error, content: toolContent } : single?.type === "text" ? { type: "text", value: single.text } : { type: "content", value: toolContent },
236
+ ...state.status === "error" ? { resultType: "error" } : {}
237
+ };
238
+ if (part.executed === true)
239
+ content.push(result);
240
+ else
241
+ results.push({ role: "tool", content: [result] });
242
+ }
243
+ return [...content.length ? [make("assistant", content)] : [], ...results];
244
+ }
245
+
246
+ // src/v2/tool-result.ts
247
+ function isToolError(part) {
248
+ const result = part.result;
249
+ return part.resultType === "error" || result?.type === "error" || result !== null && typeof result === "object" && "error" in result;
250
+ }
251
+ function toolErrorText(result) {
252
+ const value = result.value === undefined ? result : result.value;
253
+ return typeof value === "string" ? value : JSON.stringify(value);
254
+ }
255
+ function toolStateContent(state) {
256
+ if (typeof state.output === "string")
257
+ return state.output;
258
+ if (state.status === "error")
259
+ return toolErrorText({ error: state.error, content: state.content ?? [] });
260
+ if (typeof state.content === "string")
261
+ return state.content;
262
+ if (!Array.isArray(state.content))
263
+ return "";
264
+ return state.content.map((part) => {
265
+ if (typeof part === "string")
266
+ return part;
267
+ if (!part || typeof part !== "object")
268
+ return "";
269
+ const value = part;
270
+ if (typeof value.text === "string")
271
+ return value.text;
272
+ if (typeof value.value === "string")
273
+ return value.value;
274
+ return "";
275
+ }).filter(Boolean).join(`
276
+ `);
277
+ }
278
+
279
+ // src/v2/hooks/store.ts
280
+ var rawMessageTypes = new Set(RAW_MESSAGE_TYPES2);
281
+ var isRawRow = (row) => rawMessageTypes.has(row.type);
282
+ var NO_MODEL = { providerID: "", id: "" };
283
+ function hostServesRowById(row) {
284
+ return isRawRow(row) && restoreRow(row, NO_MODEL).some((message) => message.id === row.id);
285
+ }
286
+ function projectRawMessages(rows, ordinalFor) {
287
+ return rows.filter(isRawRow).map((row, index) => {
288
+ const message = {
289
+ id: row.id,
290
+ ordinal: ordinalFor(row, index),
291
+ role: row.type === "assistant" ? "assistant" : row.type === "system" ? "system" : "user",
292
+ createdAt: row.time_created ?? row.data.time?.created,
293
+ parts: row.type === "assistant" ? (row.data.content ?? []).map((part) => {
294
+ if (part.type !== "tool")
295
+ return { ...part };
296
+ const state = part.state;
297
+ const content = state.content;
298
+ return {
299
+ type: "tool",
300
+ tool: part.name,
301
+ callID: part.id,
302
+ state: {
303
+ ...state,
304
+ output: state.status === "error" ? toolStateContent(state) : content?.filter((p) => p.type === "text").map((p) => p.text).join(`
305
+ `) ?? ""
306
+ }
307
+ };
308
+ }) : [{ type: "text", text: row.data.text ?? "" }]
309
+ };
310
+ Object.defineProperty(message, "storeType", { value: row.type, enumerable: false });
311
+ if (!hostServesRowById(row))
312
+ markHostUnservedRow(message);
313
+ return message;
314
+ });
315
+ }
316
+ function rawMessages(rows, history = rows) {
317
+ const ordinals = new Map(history.filter(isRawRow).map((row, index) => [row.id, index + 1]));
318
+ return projectRawMessages(rows, (row) => {
319
+ return ordinals.get(row.id) ?? 0;
320
+ });
321
+ }
322
+ function rawMessagePage(rows, afterOrdinal) {
323
+ return projectRawMessages(rows, (_row, index) => afterOrdinal + index + 1);
324
+ }
325
+ var SERVED_BOUNDARY_PAGE = 50;
326
+ function servedBoundaryRow2(reader, sessionID, messageID) {
327
+ const boundary = reader.messageById(sessionID, messageID);
328
+ if (!boundary)
329
+ return null;
330
+ if (hostServesRowById(boundary))
331
+ return boundary;
332
+ let through = boundary.seq - 1;
333
+ for (;; ) {
334
+ const rows = reader.rawRowsThrough(sessionID, through, SERVED_BOUNDARY_PAGE);
335
+ const served = rows.find(hostServesRowById);
336
+ if (served)
337
+ return served;
338
+ const oldest = rows.at(-1);
339
+ if (rows.length < SERVED_BOUNDARY_PAGE || !oldest)
340
+ return null;
341
+ through = oldest.seq - 1;
342
+ }
343
+ }
344
+ function resolveV2BoundaryUserMessage2(reader, sessionID, endMessageID) {
345
+ const end = reader.messageById(sessionID, endMessageID);
346
+ if (!end || !isRawRow(end))
347
+ return null;
348
+ let through = end.seq;
349
+ for (;; ) {
350
+ const rows = reader.rawRowsThrough(sessionID, through, SERVED_BOUNDARY_PAGE);
351
+ const user = rows.find((row) => row.type !== "assistant");
352
+ if (user)
353
+ return rawMessagePage([user], 0)[0] ?? null;
354
+ const oldest = rows.at(-1);
355
+ if (rows.length < SERVED_BOUNDARY_PAGE || !oldest)
356
+ return null;
357
+ through = oldest.seq - 1;
358
+ }
359
+ }
360
+ function readAllV2RawMessagesForConversion2(openReader, sessionID) {
361
+ const reader = openReader();
362
+ try {
363
+ return rawMessages(reader.history(sessionID));
364
+ } finally {
365
+ reader.close();
366
+ }
367
+ }
368
+ var CONVERSION_PAGE_ROWS = 1000;
369
+ function* readV2RawMessagePagesForConversion2(openReader, sessionID, pageRows = CONVERSION_PAGE_ROWS) {
370
+ const reader = openReader();
371
+ try {
372
+ let after = -1;
373
+ let ordinal = 0;
374
+ for (;; ) {
375
+ const page = reader.page(sessionID, { after, limit: pageRows });
376
+ const messages = rawMessagePage(page.rows, ordinal);
377
+ ordinal += messages.length;
378
+ if (messages.length > 0)
379
+ yield messages;
380
+ if (page.rows.length < pageRows || page.cursor === undefined)
381
+ return;
382
+ after = page.cursor;
383
+ }
384
+ } finally {
385
+ reader.close();
386
+ }
387
+ }
388
+ function createV2RawMessageReader2(openReader, storePath, releaseReader = (reader) => reader.close()) {
389
+ const servedBoundaryCache = new Map;
390
+ return {
391
+ storePath,
392
+ getRevision: (sessionID) => {
393
+ const reader = openReader();
394
+ try {
395
+ return reader.latestSequence(sessionID);
396
+ } finally {
397
+ releaseReader(reader);
398
+ }
399
+ },
400
+ getCountAfterRevision: (sessionID, after, through) => {
401
+ const reader = openReader();
402
+ try {
403
+ return reader.messageCountAfterSequence(sessionID, after, through);
404
+ } finally {
405
+ releaseReader(reader);
406
+ }
407
+ },
408
+ readPage: (sessionID, afterOrdinal, limit, finalWatermark, after) => {
409
+ const reader = openReader();
410
+ try {
411
+ return rawMessagePage(reader.messagePage(sessionID, afterOrdinal, limit, finalWatermark, after), afterOrdinal);
412
+ } finally {
413
+ releaseReader(reader);
414
+ }
415
+ },
416
+ findById: (sessionID, messageID) => {
417
+ const reader = openReader();
418
+ try {
419
+ const ordinal = reader.messageOrdinalById(sessionID, messageID);
420
+ const row = reader.messageById(sessionID, messageID);
421
+ if (ordinal === null || row === null)
422
+ return null;
423
+ return rawMessagePage([row], ordinal - 1)[0] ?? null;
424
+ } finally {
425
+ releaseReader(reader);
426
+ }
427
+ },
428
+ findPartsById: (sessionID, messageID) => {
429
+ const reader = openReader();
430
+ try {
431
+ const row = reader.messageById(sessionID, messageID);
432
+ return row ? rawMessagePage([row], 0)[0] ?? null : null;
433
+ } finally {
434
+ releaseReader(reader);
435
+ }
436
+ },
437
+ hasById: (sessionID, messageID) => {
438
+ const reader = openReader();
439
+ try {
440
+ return reader.messageExistsById(sessionID, messageID);
441
+ } finally {
442
+ releaseReader(reader);
443
+ }
444
+ },
445
+ ordinalOf: (sessionID, messageID) => {
446
+ const reader = openReader();
447
+ try {
448
+ return reader.messageOrdinalById(sessionID, messageID);
449
+ } finally {
450
+ releaseReader(reader);
451
+ }
452
+ },
453
+ orderOf: (sessionID, messageID) => {
454
+ const reader = openReader();
455
+ try {
456
+ return reader.sequenceForId(sessionID, messageID) ?? null;
457
+ } finally {
458
+ releaseReader(reader);
459
+ }
460
+ },
461
+ ordinalMapForRange: (sessionID, fromOrdinal, toOrdinal) => {
462
+ const reader = openReader();
463
+ try {
464
+ return reader.messageIdOrdinals(sessionID, fromOrdinal, toOrdinal);
465
+ } finally {
466
+ releaseReader(reader);
467
+ }
468
+ },
469
+ readOrdinalPage: (sessionID, after, limit) => {
470
+ const reader = openReader();
471
+ try {
472
+ return reader.messageOrdinalPage(sessionID, after, limit);
473
+ } finally {
474
+ releaseReader(reader);
475
+ }
476
+ },
477
+ getCount: (sessionID) => {
478
+ const reader = openReader();
479
+ try {
480
+ return reader.messageCount(sessionID);
481
+ } finally {
482
+ releaseReader(reader);
483
+ }
484
+ },
485
+ getStoredCount: (sessionID) => {
486
+ const reader = openReader();
487
+ try {
488
+ return reader.storedMessageCount(sessionID);
489
+ } finally {
490
+ releaseReader(reader);
491
+ }
492
+ },
493
+ servedBoundaryIdOf: (sessionID, messageID) => {
494
+ const key = `${sessionID}\x00${messageID}`;
495
+ if (servedBoundaryCache.has(key))
496
+ return servedBoundaryCache.get(key) ?? null;
497
+ const reader = openReader();
498
+ try {
499
+ const served = servedBoundaryRow2(reader, sessionID, messageID)?.id ?? null;
500
+ if (servedBoundaryCache.size >= 1000)
501
+ servedBoundaryCache.clear();
502
+ if (served !== null)
503
+ servedBoundaryCache.set(key, served);
504
+ return served;
505
+ } finally {
506
+ releaseReader(reader);
507
+ }
508
+ }
509
+ };
510
+ }
511
+ function createV2RawMessageProvider2(reader, sessionID) {
512
+ return {
513
+ ...reader.storePath ? { workerSource: { kind: "opencode2", path: reader.storePath } } : {},
514
+ readMessagePage: (afterOrdinal, limit, finalWatermark, after) => reader.readPage(sessionID, afterOrdinal, limit, finalWatermark, after),
515
+ readMessageById: (messageID) => reader.findById(sessionID, messageID),
516
+ readMessagePartsById: (messageID) => reader.findPartsById(sessionID, messageID),
517
+ hasMessageById: (messageID) => reader.hasById(sessionID, messageID),
518
+ readMessageOrdinalById: (messageID) => reader.ordinalOf(sessionID, messageID),
519
+ ...reader.orderOf ? {
520
+ readMessageOrderById: (messageID) => reader.orderOf?.(sessionID, messageID) ?? null
521
+ } : {},
522
+ readMessageIdOrdinalsForRange: (fromOrdinal, toOrdinal) => reader.ordinalMapForRange(sessionID, fromOrdinal, toOrdinal),
523
+ readMessageOrdinalPage: (after, limit) => reader.readOrdinalPage(sessionID, after, limit),
524
+ getMessageCount: () => reader.getCount(sessionID),
525
+ getStoredMessageCount: () => reader.getStoredCount(sessionID),
526
+ ...reader.servedBoundaryIdOf ? {
527
+ readServedBoundaryId: (messageID) => reader.servedBoundaryIdOf?.(sessionID, messageID) ?? null
528
+ } : {}
529
+ };
530
+ }
531
+
532
+ export { STORAGE_NOTICE_PREFIX, formatStorageRefusalNotice, formatStorageRecoveryNotice, hasStorageNoticeShape, dropStorageNotices, restoreRow, isToolError, toolErrorText, toolStateContent, servedBoundaryRow2, resolveV2BoundaryUserMessage2, readAllV2RawMessagesForConversion2, readV2RawMessagePagesForConversion2, createV2RawMessageReader2, createV2RawMessageProvider2 };