@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.
- package/dist/auto-search-worker.js +11 -6
- package/dist/chunk-embedding-write-worker.js +16 -14
- package/dist/{compaction-marker-candidates-31h86ph0.js → compaction-marker-candidates-g0p67ced.js} +3 -2
- package/dist/compaction-marker-candidates-worker.js +1 -1
- package/dist/{compartment-chunk-embedding-5q5gj67r.js → compartment-chunk-embedding-y4136are.js} +8 -7
- package/dist/{conflict-warning-hook-xgskea1v.js → conflict-warning-hook-hhw8z5ak.js} +9 -8
- package/dist/{context-p3xrarzt.js → context-fk8e3h9p.js} +1400 -701
- package/dist/{derive-budgets-pqybnx09.js → derive-budgets-htj16n4x.js} +3 -2
- package/dist/embedding-backfill-q6n72sy0.js +27 -0
- package/dist/embedding-worker-process.js +2 -1
- package/dist/embedding-worker.js +5 -4
- package/dist/expand-attachments-h33jbgyz.js +21 -0
- package/dist/features/magic-context/auto-search-diagnostics.d.ts +21 -0
- package/dist/features/magic-context/auto-search-diagnostics.d.ts.map +1 -0
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/search.d.ts +4 -0
- package/dist/features/magic-context/search.d.ts.map +1 -1
- package/dist/features/magic-context/store-generation-rebase-labels.d.ts +6 -0
- package/dist/features/magic-context/store-generation-rebase-labels.d.ts.map +1 -0
- package/dist/features/magic-context/store-generation-rebase.d.ts.map +1 -1
- package/dist/{history-owner-36wp3mkb.js → history-owner-0z620wgz.js} +3 -2
- package/dist/hooks/magic-context/apply-operations.d.ts +20 -3
- package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
- package/dist/hooks/magic-context/auto-search-observation.d.ts +13 -0
- package/dist/hooks/magic-context/auto-search-observation.d.ts.map +1 -0
- package/dist/hooks/magic-context/auto-search-runner.d.ts.map +1 -1
- package/dist/hooks/magic-context/auto-search-worker-client.d.ts +5 -1
- package/dist/hooks/magic-context/auto-search-worker-client.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-commit-hashes.d.ts +4 -0
- package/dist/hooks/magic-context/compartment-commit-hashes.d.ts.map +1 -0
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts +1 -0
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-completeness.d.ts +15 -0
- package/dist/hooks/magic-context/final-wire-completeness.d.ts.map +1 -0
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts +1 -0
- package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-chunk.d.ts +11 -0
- package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-formatting.d.ts +2 -0
- package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-as2amvn3.js → index-0c7p8mq5.js} +6 -531
- package/dist/{index-51rdvtkz.js → index-1rhgxqs8.js} +2 -2
- package/dist/index-2amn2s73.js +123 -0
- package/dist/{index-c7phnbvk.js → index-2p37txes.js} +1 -1
- package/dist/index-2qbtqg1w.js +529 -0
- package/dist/{index-waktfbjy.js → index-2v6nvy3k.js} +4 -4
- package/dist/{index-ftr84kkv.js → index-3d3872c6.js} +7 -5
- package/dist/{index-0677w9sd.js → index-3j9t1v4n.js} +55 -21
- package/dist/{index-atekmqef.js → index-4bgk5ztz.js} +363 -5499
- package/dist/{index-b88dwwrh.js → index-4xyjr62e.js} +1 -1
- package/dist/index-66a50f3e.js +777 -0
- package/dist/{index-04z754rd.js → index-87ctnzz9.js} +33 -5
- package/dist/{index-xrnsygfq.js → index-9wnbnx8f.js} +1 -1
- package/dist/{index-97spytxq.js → index-b6xz3qbe.js} +246 -420
- package/dist/{index-7g9dr8gy.js → index-brg2w6qc.js} +113 -133
- package/dist/{index-spwjbkc7.js → index-dnqqqc5f.js} +1 -1
- package/dist/index-dy7w6wm4.js +194 -0
- package/dist/{index-m1x9ghk5.js → index-ezv6dz3y.js} +37 -7
- package/dist/index-f297z1n8.js +4557 -0
- package/dist/{index-qgh1qwjj.js → index-f9b3vtt1.js} +1 -1
- package/dist/{index-v15ne3a3.js → index-faxb7mck.js} +2 -2
- package/dist/{index-rrv26qjz.js → index-fw42ms3m.js} +1 -1
- package/dist/{index-t5pck3ss.js → index-fxbk7gk3.js} +1 -1
- package/dist/{index-wfybj1dw.js → index-g4t7850m.js} +2 -2
- package/dist/{index-fm36bjv4.js → index-gyaa159j.js} +18 -276
- package/dist/{index-4pnsgvfk.js → index-hacf2tyx.js} +1 -1
- package/dist/{index-s1n8dd2n.js → index-jkanybgq.js} +2 -2
- package/dist/{index-2743x98y.js → index-jn0ddrhp.js} +29 -138
- package/dist/{index-h7tgn49j.js → index-jrfre9ah.js} +5 -222
- package/dist/{index-kfyb3k6n.js → index-jz5bk7xd.js} +2 -2
- package/dist/{index-np3rgfw4.js → index-ksv0a31x.js} +3 -3
- package/dist/{index-7kkphb6c.js → index-nj360hcs.js} +4 -4
- package/dist/{index-gkee6p4g.js → index-p56dszfp.js} +3 -3
- package/dist/{index-9gpw8hrm.js → index-pq0dpwrc.js} +4 -4
- package/dist/{index-rfxzazzy.js → index-pswsyf1w.js} +4 -4
- package/dist/{index-y1qwzvxn.js → index-pztqab6k.js} +2 -2
- package/dist/{index-rverk2vz.js → index-qncw9hey.js} +1 -1
- package/dist/index-qx8j6kds.js +270 -0
- package/dist/{index-kq5w68dp.js → index-txs8tce6.js} +1 -1
- package/dist/index-vzhhr1ht.js +532 -0
- package/dist/index-wh9fxds5.js +265 -0
- package/dist/{index-hbx7m8he.js → index-x2mrq5xv.js} +170 -48
- package/dist/{index-07vskjn8.js → index-xwwsbsd3.js} +15 -13
- package/dist/index-z4p0ry94.js +222 -0
- package/dist/index-z9zygwse.js +89 -0
- package/dist/{index-v7287tzq.js → index-zhqwda72.js} +19 -8
- package/dist/index.js +83 -72
- package/dist/{logger-94kew5wm.js → logger-qwnbcfj9.js} +2 -1
- package/dist/{message-fts-rowid-map-va36knh4.js → message-fts-rowid-map-xg7hr0mk.js} +5 -4
- package/dist/{message-index-bounded-qh91fdr3.js → message-index-bounded-narw7bnz.js} +12 -11
- package/dist/{message-time-backfill-4qva0b4f.js → message-time-backfill-39bm498k.js} +3 -2
- package/dist/migration-worker.js +13 -11
- package/dist/{project-embedding-registry-6yex0r9f.js → project-embedding-registry-tdz7985n.js} +13 -11
- package/dist/raw-ordinal-warmup-worker.js +12 -7
- package/dist/{read-session-chunk-kaghtr75.js → read-session-chunk-vmg1zjg0.js} +15 -10
- package/dist/{read-session-raw-b2sd67v4.js → read-session-raw-mncmzbm9.js} +3 -2
- package/dist/recomp-orchestrator-qyyhnc9b.js +68 -0
- package/dist/render-c5p0k846.js +43 -0
- package/dist/{safe-notification-target-mkr9yzvr.js → safe-notification-target-wztd7bgk.js} +3 -2
- package/dist/{search-archive-1kw2rp19.js → search-archive-0ny7p3bw.js} +13 -11
- package/dist/{search-p684j8ww.js → search-f6hwvcxk.js} +26 -23
- package/dist/{send-session-notification-vz86nt79.js → send-session-notification-0gwcz3fw.js} +7 -6
- package/dist/{storage-tags-6hvkk01c.js → storage-tags-zx1023km.js} +5 -4
- package/dist/store-620rjzt9.js +21 -0
- package/dist/store-reader-yh19h38c.js +14 -0
- package/dist/tools/ctx-expand/render.d.ts +5 -3
- package/dist/tools/ctx-expand/render.d.ts.map +1 -1
- package/dist/tools/ctx-expand/search-archive.d.ts.map +1 -1
- package/dist/tools/ctx-expand/tools.d.ts +2 -0
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/tools/ctx-expand/v2-archive-reader.d.ts +33 -0
- package/dist/tools/ctx-expand/v2-archive-reader.d.ts.map +1 -0
- package/dist/{transform-postprocess-phase-ph38mhrs.js → transform-postprocess-phase-807ff6t2.js} +36 -30
- package/dist/v2/auto-search-worker.js +11 -6
- package/dist/v2/chunk-embedding-write-worker.js +16 -14
- package/dist/v2/{compaction-marker-candidates-m4vmye3w.js → compaction-marker-candidates-t1aq6gsj.js} +3 -2
- package/dist/v2/compaction-marker-candidates-worker.js +1 -1
- package/dist/v2/{compartment-chunk-embedding-rc31e6md.js → compartment-chunk-embedding-ncsk0r9e.js} +8 -7
- package/dist/v2/{context-s60hcatx.js → context-znqfkap4.js} +1725 -1095
- package/dist/v2/{derive-budgets-knf763tt.js → derive-budgets-sa0v7bz0.js} +4 -3
- package/dist/v2/embedding-backfill-11j425e0.js +26 -0
- package/dist/v2/embedding-worker-process.js +2 -1
- package/dist/v2/embedding-worker.js +5 -4
- package/dist/v2/expand-attachments-h33jbgyz.js +21 -0
- package/dist/v2/fold/idle.d.ts +15 -0
- package/dist/v2/fold/idle.d.ts.map +1 -0
- package/dist/v2/fold/native-replay-v3.d.ts +68 -0
- package/dist/v2/fold/native-replay-v3.d.ts.map +1 -0
- package/dist/v2/fold/native-replay.d.ts +58 -2
- package/dist/v2/fold/native-replay.d.ts.map +1 -1
- package/dist/v2/fold/policy.d.ts +2 -1
- package/dist/v2/fold/policy.d.ts.map +1 -1
- package/dist/v2/{history-owner-ad0crnrn.js → history-owner-fpka43cn.js} +3 -2
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/expand-attachments.d.ts +8 -0
- package/dist/v2/hooks/expand-attachments.d.ts.map +1 -0
- package/dist/v2/hooks/overflow-recovery.d.ts +45 -0
- package/dist/v2/hooks/overflow-recovery.d.ts.map +1 -0
- package/dist/v2/hooks/store.d.ts +7 -2
- package/dist/v2/hooks/store.d.ts.map +1 -1
- package/dist/v2/{logger-p34mjtja.js → logger-4cj6d03m.js} +2 -1
- package/dist/v2/{message-fts-rowid-map-78awh03v.js → message-fts-rowid-map-rmk571mb.js} +5 -4
- package/dist/v2/{message-index-bounded-4htdr1j6.js → message-index-bounded-620bppw7.js} +12 -11
- package/dist/v2/{message-time-backfill-zvthw6xb.js → message-time-backfill-0c4h7sg6.js} +3 -2
- package/dist/v2/migration-worker.js +13 -11
- package/dist/v2/ordinal-range.fixture.d.ts +7 -0
- package/dist/v2/ordinal-range.fixture.d.ts.map +1 -0
- package/dist/v2/{project-embedding-registry-1x5qe2fy.js → project-embedding-registry-bm5fe1a8.js} +13 -11
- package/dist/v2/raw-ordinal-warmup-worker.js +12 -7
- package/dist/v2/{read-session-chunk-1fa9qzwb.js → read-session-chunk-qfwv1k71.js} +15 -10
- package/dist/v2/{read-session-raw-5x088amc.js → read-session-raw-q294t8xs.js} +3 -2
- package/dist/v2/recomp-orchestrator-fcnjref7.js +67 -0
- package/dist/v2/render-98a1qv0b.js +42 -0
- package/dist/v2/{safe-notification-target-khnzg57c.js → safe-notification-target-gphhast7.js} +3 -2
- package/dist/v2/{search-archive-xfp51s0g.js → search-archive-r0y1bafr.js} +13 -11
- package/dist/v2/{search-wf8rqxhm.js → search-z1zqzsmh.js} +25 -22
- package/dist/v2/{send-session-notification-x5nzy34v.js → send-session-notification-fst6hkg7.js} +7 -6
- package/dist/v2/{server-qvy4qavp.js → server-1xesqm5h.js} +113 -133
- package/dist/v2/server-2amn2s73.js +123 -0
- package/dist/v2/{server-6g98c227.js → server-3schkz95.js} +4 -4
- package/dist/v2/server-468mya5v.js +209 -0
- package/dist/v2/server-55v5hpth.js +270 -0
- package/dist/v2/server-5jp38p98.js +532 -0
- package/dist/v2/{server-qjvk4a9t.js → server-69m30b21.js} +1 -1
- package/dist/v2/{server-jw5qe9y8.js → server-7ccxt4nq.js} +14 -216
- package/dist/v2/{server-3mz2qh5a.js → server-969cdpmg.js} +1 -1
- package/dist/v2/{server-835q39kd.js → server-9k0mjef7.js} +2 -2
- package/dist/v2/{server-qc915kw2.js → server-bez34e60.js} +4 -4
- package/dist/v2/{server-nxas8v1d.js → server-bpvsvtph.js} +3 -3
- package/dist/v2/{server-ab8017gw.js → server-bxpz2dcn.js} +29 -138
- package/dist/v2/{server-bsgd4dk1.js → server-dcws5831.js} +2 -2
- package/dist/v2/server-ef1mskas.js +777 -0
- package/dist/v2/{server-1bw1wcbp.js → server-f0j864ma.js} +1 -1
- package/dist/v2/{server-z2wft9zv.js → server-fqj6ddpr.js} +402 -5497
- package/dist/v2/{server-grakwy1v.js → server-hnknwaz8.js} +37 -7
- package/dist/v2/{server-h1byrhwx.js → server-hsf0qsn5.js} +19 -8
- package/dist/v2/{server-6495nmn9.js → server-jb6tjyeb.js} +5 -222
- package/dist/v2/{server-2aasmpsp.js → server-kkxzwa6w.js} +4 -4
- package/dist/v2/{server-a7x00tw8.js → server-mfn1yf9d.js} +1 -1
- package/dist/v2/{server-tkdabx8q.js → server-q0z2q2dx.js} +5 -530
- package/dist/v2/{server-ajxhrgjv.js → server-qa74bj91.js} +77 -17
- package/dist/v2/server-qf2kzsh2.js +4513 -0
- package/dist/v2/{server-gyy4k6mf.js → server-qmmkhz2z.js} +1 -1
- package/dist/v2/{server-29qp8nz4.js → server-qxzbmtqt.js} +1 -1
- package/dist/v2/{server-m1pfg2d4.js → server-r3chnxs5.js} +4 -4
- package/dist/v2/{server-vr4szghm.js → server-rktnzs89.js} +2 -2
- package/dist/v2/{server-6vd2n183.js → server-s9xgyyj9.js} +1 -1
- package/dist/v2/{server-53eh1r8r.js → server-sd8hvzte.js} +2 -2
- package/dist/v2/{server-k3t06fxr.js → server-sefxj8k1.js} +1 -1
- package/dist/v2/{server-v77xa5wj.js → server-tpt7bwq8.js} +1 -1
- package/dist/v2/{server-awr8qa4c.js → server-ws5exe6q.js} +2 -2
- package/dist/v2/server-x4m6xcn4.js +194 -0
- package/dist/v2/{server-806rzhs6.js → server-xbtw0p9t.js} +1 -1
- package/dist/v2/{server-cqjvxyrp.js → server-xy7w7hrd.js} +2 -2
- package/dist/v2/server-ypf1ap0v.js +529 -0
- package/dist/v2/server-z4p0ry94.js +222 -0
- package/dist/v2/server-z9zygwse.js +89 -0
- package/dist/v2/{server-ab1g0wn9.js → server-zzgtgqmd.js} +2 -2
- package/dist/v2/server.js +6 -5
- package/dist/v2/{storage-tags-18nbaanf.js → storage-tags-19s0ebf0.js} +5 -4
- package/dist/v2/store-reader-cgx47zkp.js +14 -0
- package/dist/v2/store-reader.d.ts +9 -2
- package/dist/v2/store-reader.d.ts.map +1 -1
- package/dist/v2/store-vdm8v3jd.js +21 -0
- package/dist/v2/v2-archive-reader-t2c0eypn.js +11 -0
- package/dist/v2/{wrapup-orchestrator-wxf7n5k6.js → wrapup-orchestrator-aqpqsveg.js} +46 -39
- package/dist/v2-archive-reader-tm2eayp2.js +11 -0
- package/dist/wrapup-orchestrator-k5yp19a6.js +55 -0
- package/package.json +1 -1
- package/src/shared/sqlite-transaction-mode-guard.test.ts +2 -0
- package/dist/embedding-backfill-0jgtqxen.js +0 -25
- package/dist/recomp-orchestrator-0y983w7h.js +0 -62
- package/dist/v2/embedding-backfill-f43hd8y6.js +0 -24
- package/dist/v2/recomp-orchestrator-7q9nvxte.js +0 -61
- package/dist/wrapup-orchestrator-ys2nrkkb.js +0 -49
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import {
|
|
2
|
+
formatFailClosedBlockingSummary
|
|
3
|
+
} from "./server-468mya5v.js";
|
|
4
|
+
import {
|
|
5
|
+
RAW_MESSAGE_TYPES2
|
|
6
|
+
} from "./server-ef1mskas.js";
|
|
7
|
+
import {
|
|
8
|
+
markHostUnservedRow
|
|
9
|
+
} from "./server-z9zygwse.js";
|
|
10
|
+
|
|
11
|
+
// src/v2/tool-result.ts
|
|
12
|
+
function isToolError(part) {
|
|
13
|
+
const result = part.result;
|
|
14
|
+
return part.resultType === "error" || result?.type === "error" || result !== null && typeof result === "object" && "error" in result;
|
|
15
|
+
}
|
|
16
|
+
function toolErrorText(result) {
|
|
17
|
+
const value = result.value === undefined ? result : result.value;
|
|
18
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
19
|
+
}
|
|
20
|
+
function toolStateContent(state) {
|
|
21
|
+
if (typeof state.output === "string")
|
|
22
|
+
return state.output;
|
|
23
|
+
if (state.status === "error")
|
|
24
|
+
return toolErrorText({ error: state.error, content: state.content ?? [] });
|
|
25
|
+
if (typeof state.content === "string")
|
|
26
|
+
return state.content;
|
|
27
|
+
if (!Array.isArray(state.content))
|
|
28
|
+
return "";
|
|
29
|
+
return state.content.map((part) => {
|
|
30
|
+
if (typeof part === "string")
|
|
31
|
+
return part;
|
|
32
|
+
if (!part || typeof part !== "object")
|
|
33
|
+
return "";
|
|
34
|
+
const value = part;
|
|
35
|
+
if (typeof value.text === "string")
|
|
36
|
+
return value.text;
|
|
37
|
+
if (typeof value.value === "string")
|
|
38
|
+
return value.value;
|
|
39
|
+
return "";
|
|
40
|
+
}).filter(Boolean).join(`
|
|
41
|
+
`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/v2/fold/restore.ts
|
|
45
|
+
import { fileURLToPath } from "node:url";
|
|
46
|
+
|
|
47
|
+
// src/v2/hooks/storage-notice.ts
|
|
48
|
+
var STORAGE_NOTICE_PREFIX = "[Magic Context storage notice] ";
|
|
49
|
+
function formatStorageRefusalNotice(reason) {
|
|
50
|
+
return `${STORAGE_NOTICE_PREFIX}This turn was not sent to the model. ${formatFailClosedBlockingSummary(reason)}`;
|
|
51
|
+
}
|
|
52
|
+
function formatStorageRecoveryNotice(hiddenWorkRecovered) {
|
|
53
|
+
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.`;
|
|
54
|
+
}
|
|
55
|
+
function isStorageNoticeText(text) {
|
|
56
|
+
return typeof text === "string" && text.startsWith(STORAGE_NOTICE_PREFIX);
|
|
57
|
+
}
|
|
58
|
+
function hasStorageNoticeShape(message) {
|
|
59
|
+
if (message.role !== "user" || typeof message.id !== "string")
|
|
60
|
+
return false;
|
|
61
|
+
if (message.content.length !== 1)
|
|
62
|
+
return false;
|
|
63
|
+
const part = message.content[0];
|
|
64
|
+
return part?.type === "text" && isStorageNoticeText(part.text);
|
|
65
|
+
}
|
|
66
|
+
async function dropStorageNotices(messages, isAdmittedSynthetic) {
|
|
67
|
+
const drop = new Set;
|
|
68
|
+
for (const message of messages) {
|
|
69
|
+
if (!hasStorageNoticeShape(message))
|
|
70
|
+
continue;
|
|
71
|
+
if (await isAdmittedSynthetic(message.id))
|
|
72
|
+
drop.add(message);
|
|
73
|
+
}
|
|
74
|
+
if (drop.size === 0)
|
|
75
|
+
return 0;
|
|
76
|
+
const kept = messages.filter((message) => !drop.has(message));
|
|
77
|
+
messages.splice(0, messages.length, ...kept);
|
|
78
|
+
return drop.size;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/v2/fold/restore.ts
|
|
82
|
+
function unavailableAttachmentNote(name, mediaType) {
|
|
83
|
+
return `[Attachment ${name ? `"${name}" ` : ""}(${mediaType}) is not available in this restored history]`;
|
|
84
|
+
}
|
|
85
|
+
var imageMimes = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
|
|
86
|
+
function attachmentParts(files, rowID, media) {
|
|
87
|
+
const seen = new Map;
|
|
88
|
+
return files.flatMap((file) => {
|
|
89
|
+
if (imageMimes.has(file.mime) && file.source.type === "inline" && file.mention?.text) {
|
|
90
|
+
const key = JSON.stringify([
|
|
91
|
+
file.mime,
|
|
92
|
+
file.name ?? null,
|
|
93
|
+
file.description ?? null,
|
|
94
|
+
file.mention.text
|
|
95
|
+
]);
|
|
96
|
+
const payloads = seen.get(key) ?? new Set;
|
|
97
|
+
if (payloads.has(file.data))
|
|
98
|
+
return [];
|
|
99
|
+
payloads.add(file.data);
|
|
100
|
+
seen.set(key, payloads);
|
|
101
|
+
}
|
|
102
|
+
const uri = file.source.type === "uri" ? file.source.uri : undefined;
|
|
103
|
+
const location = uri?.startsWith("file:") ? fileURLToPath(uri) : undefined;
|
|
104
|
+
if (file.mime === "text/plain" || file.mime === "application/x-directory") {
|
|
105
|
+
const directory = file.mime === "application/x-directory";
|
|
106
|
+
return [
|
|
107
|
+
{
|
|
108
|
+
type: "text",
|
|
109
|
+
text: `
|
|
110
|
+
|
|
111
|
+
${[
|
|
112
|
+
directory ? `Attached directory: ${location ?? file.name ?? uri ?? "directory"}` : `Attached file: ${file.name ?? uri ?? "inline attachment"}`,
|
|
113
|
+
file.description === undefined ? undefined : `Description: ${file.description}`,
|
|
114
|
+
directory && !file.data.length ? undefined : "",
|
|
115
|
+
directory && !file.data.length ? undefined : Buffer.from(file.data, "base64").toString("utf8")
|
|
116
|
+
].filter((line) => line !== undefined).join(`
|
|
117
|
+
`)}`,
|
|
118
|
+
metadata: {
|
|
119
|
+
attachment: {
|
|
120
|
+
source: file.source,
|
|
121
|
+
name: file.name,
|
|
122
|
+
description: file.description
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
if (!imageMimes.has(file.mime) && file.mime !== "application/pdf")
|
|
129
|
+
return [];
|
|
130
|
+
const metadata = file.description === undefined ? undefined : { description: file.description };
|
|
131
|
+
const located = location ? [{ type: "text", text: `Attached file: ${location}` }] : [];
|
|
132
|
+
if (!media)
|
|
133
|
+
return [
|
|
134
|
+
...located,
|
|
135
|
+
{
|
|
136
|
+
type: "media",
|
|
137
|
+
mediaType: file.mime,
|
|
138
|
+
data: file.data,
|
|
139
|
+
filename: file.name,
|
|
140
|
+
metadata
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
const asset = media.asset(file.data, file.mime);
|
|
144
|
+
if (typeof asset === "string") {
|
|
145
|
+
media.unavailable({ rowID, name: file.name, mediaType: file.mime, reason: asset });
|
|
146
|
+
return [
|
|
147
|
+
...located,
|
|
148
|
+
{ type: "text", text: unavailableAttachmentNote(file.name, file.mime) }
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
return [...located, { type: "media", media: asset, filename: file.name, metadata }];
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function restoreRow(row, model, media) {
|
|
155
|
+
const data = row.data;
|
|
156
|
+
const make = (role, content, metadata = data.metadata) => ({
|
|
157
|
+
id: row.id,
|
|
158
|
+
role,
|
|
159
|
+
content,
|
|
160
|
+
...metadata === undefined ? {} : { metadata }
|
|
161
|
+
});
|
|
162
|
+
if (row.type === "user") {
|
|
163
|
+
const skills = data.skills ?? [];
|
|
164
|
+
const content = [
|
|
165
|
+
...skills.flatMap((skill) => skill.text === undefined ? [] : [{ type: "text", text: skill.text }]),
|
|
166
|
+
...data.text ? [{ type: "text", text: data.text }] : [],
|
|
167
|
+
...attachmentParts(data.files ?? [], row.id, media)
|
|
168
|
+
];
|
|
169
|
+
return content.length ? [
|
|
170
|
+
make("user", content, {
|
|
171
|
+
...data.metadata ?? {},
|
|
172
|
+
...data.agents?.length ? { agents: data.agents } : {}
|
|
173
|
+
})
|
|
174
|
+
] : [];
|
|
175
|
+
}
|
|
176
|
+
if (row.type === "system")
|
|
177
|
+
return [{ role: "system", content: [{ type: "text", text: data.text ?? "" }] }];
|
|
178
|
+
if (row.type === "synthetic" && isStorageNoticeText(data.text))
|
|
179
|
+
return [];
|
|
180
|
+
if (["synthetic", "skill"].includes(row.type))
|
|
181
|
+
return [make("user", [{ type: "text", text: data.text ?? "" }])];
|
|
182
|
+
if (row.type === "location-switched")
|
|
183
|
+
return [
|
|
184
|
+
make("user", [
|
|
185
|
+
{
|
|
186
|
+
type: "text",
|
|
187
|
+
text: `The working directory has been changed to ${data.location.directory}.`
|
|
188
|
+
}
|
|
189
|
+
])
|
|
190
|
+
];
|
|
191
|
+
if (row.type === "shell")
|
|
192
|
+
return data.metadata?.background === true ? [] : [
|
|
193
|
+
make("user", [
|
|
194
|
+
{
|
|
195
|
+
type: "text",
|
|
196
|
+
text: `The following shell command was executed by the user:
|
|
197
|
+
|
|
198
|
+
Command:
|
|
199
|
+
${data.command}
|
|
200
|
+
|
|
201
|
+
Output:
|
|
202
|
+
${data.output?.output ?? ""}`
|
|
203
|
+
}
|
|
204
|
+
])
|
|
205
|
+
];
|
|
206
|
+
if (row.type !== "assistant")
|
|
207
|
+
return [];
|
|
208
|
+
const previousModel = data.model;
|
|
209
|
+
const sameProvider = previousModel?.providerID === model.providerID;
|
|
210
|
+
const sameModel = sameProvider && previousModel?.id === model.id;
|
|
211
|
+
const reuse = sameModel && data.error === undefined;
|
|
212
|
+
const metadata = (state) => state === undefined ? undefined : { [model.providerID]: state };
|
|
213
|
+
const reasoningInterrupted = data.content?.some((part) => part.type === "reasoning" && part.time !== undefined && part.time.completed === undefined);
|
|
214
|
+
const content = [];
|
|
215
|
+
const results = [];
|
|
216
|
+
for (const part of data.content ?? []) {
|
|
217
|
+
let native = "native" in part ? part.native : part.type === "tool" ? part.providerState : part.state;
|
|
218
|
+
if (part.type === "tool" && part.providerResultState !== null && typeof part.providerResultState === "object" && !Array.isArray(part.providerResultState))
|
|
219
|
+
native = {
|
|
220
|
+
...native !== null && typeof native === "object" && !Array.isArray(native) ? native : {},
|
|
221
|
+
...part.providerResultState
|
|
222
|
+
};
|
|
223
|
+
if (part.type === "text") {
|
|
224
|
+
if (part.text !== "")
|
|
225
|
+
content.push({
|
|
226
|
+
type: "text",
|
|
227
|
+
text: part.text,
|
|
228
|
+
providerMetadata: reuse ? metadata(native) : undefined
|
|
229
|
+
});
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (part.type === "reasoning") {
|
|
233
|
+
if (part.text !== "" || reuse && !reasoningInterrupted && native !== undefined)
|
|
234
|
+
content.push({
|
|
235
|
+
type: data.error === undefined && !reasoningInterrupted ? "reasoning" : "text",
|
|
236
|
+
text: part.text,
|
|
237
|
+
providerMetadata: reuse && !reasoningInterrupted ? metadata(native) : undefined
|
|
238
|
+
});
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const state = part.state;
|
|
242
|
+
const completed = state.status === "completed" || state.status === "error";
|
|
243
|
+
const toolReuse = reuse || sameModel && part.executed === true && completed;
|
|
244
|
+
let input = state.input;
|
|
245
|
+
if (state.status === "streaming" && typeof input === "string") {
|
|
246
|
+
try {
|
|
247
|
+
input = JSON.parse(input);
|
|
248
|
+
} catch {}
|
|
249
|
+
}
|
|
250
|
+
content.push({
|
|
251
|
+
type: "tool-call",
|
|
252
|
+
id: part.id,
|
|
253
|
+
name: part.name,
|
|
254
|
+
input,
|
|
255
|
+
providerExecuted: part.executed,
|
|
256
|
+
providerMetadata: toolReuse ? metadata(native) : undefined
|
|
257
|
+
});
|
|
258
|
+
if (!completed)
|
|
259
|
+
continue;
|
|
260
|
+
const toolContent = state.content ?? [];
|
|
261
|
+
const single = toolContent.length === 1 ? toolContent[0] : undefined;
|
|
262
|
+
const result = {
|
|
263
|
+
type: "tool-result",
|
|
264
|
+
id: part.id,
|
|
265
|
+
name: part.name,
|
|
266
|
+
providerExecuted: part.executed,
|
|
267
|
+
providerMetadata: (part.executed === true ? sameProvider : reuse) ? metadata(native) : undefined,
|
|
268
|
+
result: state.status === "error" ? { error: state.error, content: toolContent } : single?.type === "text" ? { type: "text", value: single.text } : { type: "content", value: toolContent },
|
|
269
|
+
...state.status === "error" ? { resultType: "error" } : {}
|
|
270
|
+
};
|
|
271
|
+
if (part.executed === true)
|
|
272
|
+
content.push(result);
|
|
273
|
+
else
|
|
274
|
+
results.push({ role: "tool", content: [result] });
|
|
275
|
+
}
|
|
276
|
+
return [...content.length ? [make("assistant", content)] : [], ...results];
|
|
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 { isToolError, toolErrorText, toolStateContent, STORAGE_NOTICE_PREFIX, formatStorageRefusalNotice, formatStorageRecoveryNotice, hasStorageNoticeShape, dropStorageNotices, restoreRow, servedBoundaryRow2, resolveV2BoundaryUserMessage2, readAllV2RawMessagesForConversion2, readV2RawMessagePagesForConversion2, createV2RawMessageReader2, createV2RawMessageProvider2 };
|