openwork-server 0.18.62 → 0.18.64
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/README.md +2 -2
- package/dist/opencode-plugin-deps-1.18.33.tgz +0 -0
- package/dist/opencode-plugins/approval-loopback-fetch.js +18 -0
- package/dist/opencode-plugins/drive-file-tools.js +20 -5
- package/dist/opencode-plugins/managed-policy-next.js +7782 -1886
- package/dist/opencode-plugins/openwork-agent-permissions-v2.js +7912 -1896
- package/dist/opencode-plugins/openwork-auto-route-v2.js +19657 -0
- package/dist/opencode-plugins/openwork-capabilities-knowledge.js +7800 -1911
- package/dist/opencode-plugins/openwork-chrome-devtools.js +25294 -1914
- package/dist/opencode-plugins/openwork-cloud-skills-v2.js +54 -31
- package/dist/opencode-plugins/openwork-computer-use.js +25259 -1881
- package/dist/opencode-plugins/openwork-context-v2.js +25523 -1891
- package/dist/opencode-plugins/openwork-extensions-preview.js +8804 -2150
- package/dist/opencode-plugins/openwork-gateway-quota-v2.js +7748 -1846
- package/dist/opencode-plugins/openwork-gateway-quota.js +7755 -1853
- package/dist/opencode-plugins/openwork-pdf-attachments.js +8032 -2145
- package/dist/opencode-plugins/openwork-provider-adapters.js +65 -6
- package/dist/opencode-plugins/openwork-provider-sync-v2.js +459 -139
- package/dist/opencode-plugins/openwork-session-groups.js +5 -4
- package/dist/opencode-plugins/openwork-spreadsheets.js +7809 -1922
- package/dist/opencode-plugins/openwork-ui-bridge.js +6 -3
- package/dist/opencode-plugins/openwork-visible-reasoning-v2.js +153 -0
- package/dist/opencode-plugins/provider-safe-tool-schema.js +57 -0
- package/dist/opencode-plugins/session-read-detail.js +475 -0
- package/dist/openwork-server.mjs +21238 -10377
- package/package.json +1 -1
- package/web/assets/{abnfDiagram-VCTEODGH-DJzrT8Ql.js → abnfDiagram-VCTEODGH-j_pTDiqz.js} +1 -1
- package/web/assets/{app-_2KkkZIM.js → app-Bzky4OVl.js} +489 -454
- package/web/assets/app-C32roy8y.css +32 -0
- package/web/assets/{arc-KehB1xN5.js → arc-CaQNh7ZT.js} +1 -1
- package/web/assets/{architectureDiagram-5GKGNRK7-C_GL2639.js → architectureDiagram-5GKGNRK7-wMx5wEbF.js} +1 -1
- package/web/assets/{artifact-code-view-CYn_58gV.js → artifact-code-view-DCMMpJxD.js} +1 -1
- package/web/assets/{artifact-spreadsheet-editor-f6V-tYvi.js → artifact-spreadsheet-editor-DNHOxqPc.js} +1 -1
- package/web/assets/{artifact-text-editor-CiSlW3kl.js → artifact-text-editor-o6WCLEmo.js} +1 -1
- package/web/assets/{blockDiagram-OSKFZWR5-DErH-7gY.js → blockDiagram-OSKFZWR5-BYAM-sb8.js} +1 -1
- package/web/assets/{c4Diagram-7LVT6UL2-aML4z_n9.js → c4Diagram-7LVT6UL2-pOXGVuIa.js} +1 -1
- package/web/assets/channel-DR4AKESO.js +1 -0
- package/web/assets/{chunk-2Q5K7J3B-Dw_IH9rW.js → chunk-2Q5K7J3B-A7RWrtFk.js} +1 -1
- package/web/assets/{chunk-5VM5RSS4-aeJEQ945.js → chunk-5VM5RSS4-BbhM53lO.js} +1 -1
- package/web/assets/{chunk-F27PBJKO-hZwti-Oc.js → chunk-F27PBJKO-Aoti2mfN.js} +1 -1
- package/web/assets/{chunk-HLEWEB6X-JFNzm3Ge.js → chunk-HLEWEB6X-h1a1fijx.js} +1 -1
- package/web/assets/{chunk-JWPE2WC7-B34hvYk7.js → chunk-JWPE2WC7-CtyBiDJt.js} +1 -1
- package/web/assets/{chunk-POPQ4Y6H-CDqgEp-q.js → chunk-POPQ4Y6H-d6pjL5RD.js} +1 -1
- package/web/assets/{chunk-SVP7TREG-B65ta9aQ.js → chunk-SVP7TREG-DnzaR7AG.js} +1 -1
- package/web/assets/{chunk-XXDRQBXY-DjTi6c9m.js → chunk-XXDRQBXY-B9hkBiar.js} +1 -1
- package/web/assets/{chunk-ZLD2IHE6-CbviXFvb.js → chunk-ZLD2IHE6-DzJ1_fxy.js} +1 -1
- package/web/assets/classDiagram-CYGNFDIV-B0y8HxEN.js +1 -0
- package/web/assets/classDiagram-v2-TLXNO2FR-B0y8HxEN.js +1 -0
- package/web/assets/{cose-bilkent-JH36ORCC-Cl9SO5zd.js → cose-bilkent-JH36ORCC-BhvTRFRe.js} +1 -1
- package/web/assets/{cynefin-OW5HDTMX-CAcz2tHt.js → cynefin-OW5HDTMX-fGckFsSU.js} +1 -1
- package/web/assets/{cynefinDiagram-5FMLGOSQ-BJHft4FO.js → cynefinDiagram-5FMLGOSQ-BzW--hKZ.js} +1 -1
- package/web/assets/{dagre-OS7QT2EB-DbdnY1uS.js → dagre-OS7QT2EB-BnhLgL07.js} +1 -1
- package/web/assets/{diagram-S7CK7UJ4-C07WD5Lc.js → diagram-S7CK7UJ4-Cu-md7dS.js} +1 -1
- package/web/assets/{diagram-UQ7AKVKN-D8VCjq9x.js → diagram-UQ7AKVKN-DXQdcUqJ.js} +1 -1
- package/web/assets/{diagram-VSXAHHWV-D1XSfMnK.js → diagram-VSXAHHWV-C0TNG60H.js} +1 -1
- package/web/assets/{diagram-VX7I27RA-3GZe-yrf.js → diagram-VX7I27RA-z9AkHrlD.js} +1 -1
- package/web/assets/{diagram-Z3DM3KII-CI43WcIB.js → diagram-Z3DM3KII-CB0wN1mx.js} +1 -1
- package/web/assets/{ebnfDiagram-PWID7BFC-CTzouade.js → ebnfDiagram-PWID7BFC-k6ofa2S7.js} +1 -1
- package/web/assets/{erDiagram-2YWLMYGG-CNcqsnqx.js → erDiagram-2YWLMYGG-DH0LRcXA.js} +1 -1
- package/web/assets/{flowDiagram-T62WH6J4-pomAqjgF.js → flowDiagram-T62WH6J4-BRVNnCPG.js} +1 -1
- package/web/assets/{ganttDiagram-EL5Y4UJY-C1dNaxxj.js → ganttDiagram-EL5Y4UJY-BnEWMV5l.js} +1 -1
- package/web/assets/{gitGraphDiagram-WWUBYQGX-Ni8GECg1.js → gitGraphDiagram-WWUBYQGX-COrkPeQK.js} +1 -1
- package/web/assets/{infoDiagram-FKFFQAWI-C0kvE7Zu.js → infoDiagram-FKFFQAWI-DZuH1fmc.js} +1 -1
- package/web/assets/{ishikawaDiagram-5VMMS53U-CxmP_irp.js → ishikawaDiagram-5VMMS53U-CaJUJ6Ff.js} +1 -1
- package/web/assets/{journeyDiagram-EYS64GPL-60GkPgH5.js → journeyDiagram-EYS64GPL-BE6Fv_rV.js} +1 -1
- package/web/assets/{kanban-definition-UXKFOSKX-ByxcB0T_.js → kanban-definition-UXKFOSKX-B4bPGzK6.js} +1 -1
- package/web/assets/{layout-BD9ef90s.js → layout-or_42uvc.js} +1 -1
- package/web/assets/{linear-Dp9l0lBr.js → linear-5PXMsdhe.js} +1 -1
- package/web/assets/{mermaid.core-FKm3Akes.js → mermaid.core-DW6D3RPJ.js} +6 -6
- package/web/assets/{mindmap-definition-THT77NOG-i7WJlWHV.js → mindmap-definition-THT77NOG-CyTLW_BU.js} +1 -1
- package/web/assets/{pegDiagram-XKGWAZYB-DfI-LsJq.js → pegDiagram-XKGWAZYB-7JUI9VKo.js} +1 -1
- package/web/assets/{pieDiagram-E7YTZNPT-kcgWYBSb.js → pieDiagram-E7YTZNPT-Bp-lNUXy.js} +1 -1
- package/web/assets/{quadrantDiagram-AXDQQJYC-DVyziZnc.js → quadrantDiagram-AXDQQJYC-BHzbyOLv.js} +1 -1
- package/web/assets/{railroadDiagram-O6MQD6OU-CWgHq9nv.js → railroadDiagram-O6MQD6OU-DUbwyc8J.js} +1 -1
- package/web/assets/{requirementDiagram-IS5BZ75X-B7fgi4lz.js → requirementDiagram-IS5BZ75X-C6vHvRXL.js} +1 -1
- package/web/assets/{sankeyDiagram-P5KCCOFB-D1-pD_qh.js → sankeyDiagram-P5KCCOFB-rhUrZHvo.js} +1 -1
- package/web/assets/{sequenceDiagram-WJ2MYXX4-B6oeRmVj.js → sequenceDiagram-WJ2MYXX4-CbKw45qx.js} +1 -1
- package/web/assets/{sizeCapture-INFHLROL-BTpKGFt5.js → sizeCapture-INFHLROL-BzYRidKe.js} +1 -1
- package/web/assets/{stateDiagram-XQSTLZYL-zXJRxE6h.js → stateDiagram-XQSTLZYL-CY_NqfCu.js} +1 -1
- package/web/assets/stateDiagram-v2-IH3M54BS-ZplxpnyH.js +1 -0
- package/web/assets/{swimlanes-V6O3JKXN-Dc7MXPb3.js → swimlanes-V6O3JKXN-DsuNS06S.js} +1 -1
- package/web/assets/swimlanesDiagram-JKAHXJPX-BgYXaTmf.js +8 -0
- package/web/assets/{timeline-definition-24CTP7MA-DYhgbnjW.js → timeline-definition-24CTP7MA-Bw7piZjw.js} +1 -1
- package/web/assets/{vennDiagram-4TSXK5OY-DRnme7za.js → vennDiagram-4TSXK5OY-3QO9enN9.js} +1 -1
- package/web/assets/{wardleyDiagram-VM6X3IG4-wAoo4GsZ.js → wardleyDiagram-VM6X3IG4-u2wsdzR8.js} +1 -1
- package/web/assets/{workspace-file-tree-DmAwvqtx.js → workspace-file-tree-B0RW5c5J.js} +1 -1
- package/web/assets/{xychartDiagram-S5SC5T6Z-_Z_TOWBn.js → xychartDiagram-S5SC5T6Z-YVqX-MzU.js} +1 -1
- package/web/index.html +2 -2
- package/web/assets/app-CkmqBm_1.css +0 -32
- package/web/assets/channel-7oM0ncHI.js +0 -1
- package/web/assets/classDiagram-CYGNFDIV-BkidFauP.js +0 -1
- package/web/assets/classDiagram-v2-TLXNO2FR-BkidFauP.js +0 -1
- package/web/assets/stateDiagram-v2-IH3M54BS-CmwZ6tXj.js +0 -1
- package/web/assets/swimlanesDiagram-JKAHXJPX-BXoJEx2b.js +0 -8
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `session.read` with `detail: "full"`: every message and content item the
|
|
3
|
+
* engine stores, mapped from OpenCode v2's native message schema
|
|
4
|
+
* (`Session.Message.Info`, `GET /api/session/:id/message`, engine 2.2.0) and,
|
|
5
|
+
* for sessions still on v1, from the equivalent v1 message parts into the
|
|
6
|
+
* same shape.
|
|
7
|
+
*
|
|
8
|
+
* Kept: every message type (user, assistant, synthetic, system, skill, shell,
|
|
9
|
+
* compaction, idle, agent/model/location switches) and every assistant
|
|
10
|
+
* content item (text, reasoning, tool calls with input, output, files, error,
|
|
11
|
+
* metadata and timing), plus model, agent, finish reason, tokens, cost,
|
|
12
|
+
* retries and changed files.
|
|
13
|
+
*
|
|
14
|
+
* Left out on purpose: provider `native` blobs (opaque, provider-specific,
|
|
15
|
+
* may hold signed reasoning), inline base64 file data (the file's name, type
|
|
16
|
+
* and URI are kept) and raw provider response bodies in errors.
|
|
17
|
+
*
|
|
18
|
+
* Long fields are clipped with a visible marker and the message is flagged
|
|
19
|
+
* `truncated`; reading one message by `messageId` raises the limits.
|
|
20
|
+
*
|
|
21
|
+
* Dependency-free on purpose: the bundled engine plugin imports it.
|
|
22
|
+
*/
|
|
23
|
+
function isRecord(value) {
|
|
24
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
function str(value) {
|
|
27
|
+
return typeof value === "string" ? value : undefined;
|
|
28
|
+
}
|
|
29
|
+
function num(value) {
|
|
30
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
function rec(value) {
|
|
33
|
+
return isRecord(value) ? value : undefined;
|
|
34
|
+
}
|
|
35
|
+
function list(value) {
|
|
36
|
+
return Array.isArray(value) ? value : [];
|
|
37
|
+
}
|
|
38
|
+
/** Drops undefined fields so the result stays compact. */
|
|
39
|
+
function compact(value) {
|
|
40
|
+
for (const key of Object.keys(value))
|
|
41
|
+
if (value[key] === undefined)
|
|
42
|
+
delete value[key];
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
export const SESSION_READ_LIST_LIMITS = {
|
|
46
|
+
text: 8_000,
|
|
47
|
+
reasoning: 4_000,
|
|
48
|
+
context: 2_000,
|
|
49
|
+
toolInput: 2_000,
|
|
50
|
+
toolOutput: 4_000,
|
|
51
|
+
toolMetadata: 1_000,
|
|
52
|
+
shellOutput: 4_000,
|
|
53
|
+
compactionSummary: 4_000,
|
|
54
|
+
errorMessage: 1_000,
|
|
55
|
+
toolBudget: 64_000,
|
|
56
|
+
};
|
|
57
|
+
export const SESSION_READ_MESSAGE_LIMITS = {
|
|
58
|
+
text: 100_000,
|
|
59
|
+
reasoning: 50_000,
|
|
60
|
+
context: 50_000,
|
|
61
|
+
toolInput: 50_000,
|
|
62
|
+
toolOutput: 100_000,
|
|
63
|
+
toolMetadata: 20_000,
|
|
64
|
+
shellOutput: 100_000,
|
|
65
|
+
compactionSummary: 50_000,
|
|
66
|
+
errorMessage: 4_000,
|
|
67
|
+
toolBudget: 400_000,
|
|
68
|
+
};
|
|
69
|
+
/** Whole-result character budget for a list read. One message is always returned. */
|
|
70
|
+
export const SESSION_READ_RESULT_BUDGET = 200_000;
|
|
71
|
+
class Clipper {
|
|
72
|
+
limits;
|
|
73
|
+
truncated = false;
|
|
74
|
+
constructor(limits) {
|
|
75
|
+
this.limits = limits;
|
|
76
|
+
}
|
|
77
|
+
text(value, limit) {
|
|
78
|
+
if (value === undefined)
|
|
79
|
+
return undefined;
|
|
80
|
+
if (value.length <= limit)
|
|
81
|
+
return value;
|
|
82
|
+
this.truncated = true;
|
|
83
|
+
return `${value.slice(0, limit)}…[+${value.length - limit} chars; read this message by messageId for more]`;
|
|
84
|
+
}
|
|
85
|
+
/** Small JSON values stay structured; large ones become a clipped JSON string. */
|
|
86
|
+
json(value, limit) {
|
|
87
|
+
if (value === undefined)
|
|
88
|
+
return undefined;
|
|
89
|
+
if (typeof value === "string")
|
|
90
|
+
return this.text(value, limit);
|
|
91
|
+
let encoded;
|
|
92
|
+
try {
|
|
93
|
+
encoded = JSON.stringify(value) ?? "";
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return "[unserializable]";
|
|
97
|
+
}
|
|
98
|
+
if (encoded.length <= limit)
|
|
99
|
+
return value;
|
|
100
|
+
return this.text(encoded, limit);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function richModel(value) {
|
|
104
|
+
const model = rec(value);
|
|
105
|
+
if (!model)
|
|
106
|
+
return undefined;
|
|
107
|
+
const modelId = str(model.id) ?? str(model.modelID);
|
|
108
|
+
const providerId = str(model.providerID);
|
|
109
|
+
if (!modelId || !providerId)
|
|
110
|
+
return undefined;
|
|
111
|
+
return compact({ providerId, modelId, variant: str(model.variant) });
|
|
112
|
+
}
|
|
113
|
+
function richTokens(value) {
|
|
114
|
+
const tokens = rec(value);
|
|
115
|
+
if (!tokens)
|
|
116
|
+
return undefined;
|
|
117
|
+
const cache = rec(tokens.cache);
|
|
118
|
+
return compact({
|
|
119
|
+
input: num(tokens.input),
|
|
120
|
+
output: num(tokens.output),
|
|
121
|
+
reasoning: num(tokens.reasoning),
|
|
122
|
+
cache: cache ? compact({ read: num(cache.read), write: num(cache.write) }) : undefined,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/** v2 `Session.StructuredError`; the provider response body is omitted. */
|
|
126
|
+
function richV2Error(value, clip) {
|
|
127
|
+
const error = rec(value);
|
|
128
|
+
if (!error)
|
|
129
|
+
return undefined;
|
|
130
|
+
return compact({
|
|
131
|
+
type: str(error.type) ?? "UnknownError",
|
|
132
|
+
message: clip.text(str(error.message), clip.limits.errorMessage),
|
|
133
|
+
status: num(error.status),
|
|
134
|
+
retryAfterMs: num(error.retryAfterMs),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
function richTime(value) {
|
|
138
|
+
const time = rec(value);
|
|
139
|
+
return compact({ created: num(time?.created), streamed: num(time?.streamed), completed: num(time?.completed) });
|
|
140
|
+
}
|
|
141
|
+
function childSessionId(tool, metadata) {
|
|
142
|
+
if (!metadata || (tool !== "subagent" && tool !== "task"))
|
|
143
|
+
return undefined;
|
|
144
|
+
return str(metadata.sessionId)?.trim() || str(metadata.sessionID)?.trim() || undefined;
|
|
145
|
+
}
|
|
146
|
+
/** A file reference without its bytes: `data:` URLs carry the whole file, so they become `inline`. */
|
|
147
|
+
function fileRef(name, mime, uri) {
|
|
148
|
+
const inline = uri?.startsWith("data:") === true;
|
|
149
|
+
return compact({ name, mime, uri: inline ? undefined : uri, inline: inline ? true : undefined });
|
|
150
|
+
}
|
|
151
|
+
function nonEmpty(value) {
|
|
152
|
+
return value && Object.keys(value).length > 0 ? value : undefined;
|
|
153
|
+
}
|
|
154
|
+
/** Keeps tool input, output and metadata within the message's tool budget. */
|
|
155
|
+
function budgetTools(content, clip) {
|
|
156
|
+
let budget = clip.limits.toolBudget;
|
|
157
|
+
return content.map((item) => {
|
|
158
|
+
if (item.type !== "tool")
|
|
159
|
+
return item;
|
|
160
|
+
const size = JSON.stringify(item).length;
|
|
161
|
+
if (size <= budget) {
|
|
162
|
+
budget -= size;
|
|
163
|
+
return item;
|
|
164
|
+
}
|
|
165
|
+
budget = 0;
|
|
166
|
+
if (item.input === undefined && item.output === undefined && item.metadata === undefined)
|
|
167
|
+
return item;
|
|
168
|
+
clip.truncated = true;
|
|
169
|
+
const { input: _input, output: _output, metadata: _metadata, ...listed } = item;
|
|
170
|
+
return { ...listed, detailOmitted: true };
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function v2ToolOutput(content) {
|
|
174
|
+
const texts = [];
|
|
175
|
+
const files = [];
|
|
176
|
+
for (const item of list(content)) {
|
|
177
|
+
if (!isRecord(item))
|
|
178
|
+
continue;
|
|
179
|
+
if (item.type === "text" && typeof item.text === "string")
|
|
180
|
+
texts.push(item.text);
|
|
181
|
+
if (item.type === "file")
|
|
182
|
+
files.push(fileRef(str(item.name), str(item.mime), str(item.uri)));
|
|
183
|
+
}
|
|
184
|
+
return { ...(texts.length ? { output: texts.join("\n") } : {}), ...(files.length ? { files } : {}) };
|
|
185
|
+
}
|
|
186
|
+
function v2Tool(item, clip) {
|
|
187
|
+
const id = str(item.id);
|
|
188
|
+
const name = str(item.name);
|
|
189
|
+
const state = rec(item.state);
|
|
190
|
+
if (!id || !name || !state)
|
|
191
|
+
return null;
|
|
192
|
+
const status = str(state.status) ?? "unknown";
|
|
193
|
+
const metadata = rec(state.metadata);
|
|
194
|
+
const { output, files } = v2ToolOutput(state.content);
|
|
195
|
+
const time = rec(item.time);
|
|
196
|
+
const call = {
|
|
197
|
+
type: "tool",
|
|
198
|
+
id,
|
|
199
|
+
name,
|
|
200
|
+
status,
|
|
201
|
+
executed: typeof item.executed === "boolean" ? item.executed : undefined,
|
|
202
|
+
// Streaming input is the raw partial JSON text the model is still writing.
|
|
203
|
+
input: clip.json(state.input, clip.limits.toolInput),
|
|
204
|
+
output: clip.text(output, clip.limits.toolOutput),
|
|
205
|
+
files,
|
|
206
|
+
error: status === "error" ? richV2Error(state.error, clip) : undefined,
|
|
207
|
+
metadata: clip.json(nonEmpty(metadata), clip.limits.toolMetadata),
|
|
208
|
+
childSessionId: childSessionId(name, metadata),
|
|
209
|
+
time: time ? compact({ created: num(time.created), ran: num(time.ran), completed: num(time.completed) }) : undefined,
|
|
210
|
+
};
|
|
211
|
+
return compact(call);
|
|
212
|
+
}
|
|
213
|
+
function v2Content(value, clip) {
|
|
214
|
+
const content = [];
|
|
215
|
+
for (const item of list(value)) {
|
|
216
|
+
if (!isRecord(item))
|
|
217
|
+
continue;
|
|
218
|
+
if (item.type === "text") {
|
|
219
|
+
content.push({ type: "text", text: clip.text(str(item.text) ?? "", clip.limits.text) ?? "" });
|
|
220
|
+
}
|
|
221
|
+
else if (item.type === "reasoning") {
|
|
222
|
+
const time = rec(item.time);
|
|
223
|
+
content.push(compact({
|
|
224
|
+
type: "reasoning",
|
|
225
|
+
text: clip.text(str(item.text) ?? "", clip.limits.reasoning) ?? "",
|
|
226
|
+
time: time ? compact({ created: num(time.created), completed: num(time.completed) }) : undefined,
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
else if (item.type === "tool") {
|
|
230
|
+
const tool = v2Tool(item, clip);
|
|
231
|
+
if (tool)
|
|
232
|
+
content.push(tool);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return budgetTools(content, clip);
|
|
236
|
+
}
|
|
237
|
+
function v2File(value) {
|
|
238
|
+
const file = rec(value);
|
|
239
|
+
if (!file)
|
|
240
|
+
return null;
|
|
241
|
+
const source = rec(file.source);
|
|
242
|
+
if (source?.type === "uri")
|
|
243
|
+
return fileRef(str(file.name), str(file.mime), str(source.uri));
|
|
244
|
+
return compact({ name: str(file.name), mime: str(file.mime), inline: true });
|
|
245
|
+
}
|
|
246
|
+
/** Maps one native v2 `Session.Message.Info`. Unknown future types keep id, type and time. */
|
|
247
|
+
export function richV2Message(row, limits, options = {}) {
|
|
248
|
+
if (!isRecord(row))
|
|
249
|
+
return null;
|
|
250
|
+
const id = str(row.id);
|
|
251
|
+
const type = str(row.type);
|
|
252
|
+
if (!id || !type)
|
|
253
|
+
return null;
|
|
254
|
+
const clip = new Clipper(limits);
|
|
255
|
+
const base = { id, type, time: richTime(row.time) };
|
|
256
|
+
let fields = {};
|
|
257
|
+
switch (type) {
|
|
258
|
+
case "user": {
|
|
259
|
+
const files = list(row.files).map(v2File).filter((file) => file !== null);
|
|
260
|
+
const agents = list(row.agents).flatMap((agent) => (isRecord(agent) && typeof agent.name === "string" ? [agent.name] : []));
|
|
261
|
+
const skills = list(row.skills).flatMap((skill) => isRecord(skill) && typeof skill.name === "string" ? [compact({ id: str(skill.id), name: skill.name })] : []);
|
|
262
|
+
fields = {
|
|
263
|
+
text: clip.text(str(row.text) ?? "", limits.text),
|
|
264
|
+
files: files.length ? files : undefined,
|
|
265
|
+
agents: agents.length ? agents : undefined,
|
|
266
|
+
skills: skills.length ? skills : undefined,
|
|
267
|
+
};
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case "synthetic":
|
|
271
|
+
case "system":
|
|
272
|
+
fields = { text: clip.text(str(row.text) ?? "", limits.context) };
|
|
273
|
+
break;
|
|
274
|
+
case "skill":
|
|
275
|
+
fields = { skill: str(row.skill), name: str(row.name), text: clip.text(str(row.text) ?? "", limits.context) };
|
|
276
|
+
break;
|
|
277
|
+
case "shell": {
|
|
278
|
+
const output = rec(row.output);
|
|
279
|
+
fields = {
|
|
280
|
+
shellId: str(row.shellID),
|
|
281
|
+
command: str(row.command),
|
|
282
|
+
status: str(row.status),
|
|
283
|
+
exit: num(row.exit) ?? str(row.exit),
|
|
284
|
+
output: clip.text(str(output?.output), limits.shellOutput),
|
|
285
|
+
outputTruncatedByEngine: output?.truncated === true ? true : undefined,
|
|
286
|
+
};
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case "assistant": {
|
|
290
|
+
const snapshot = rec(row.snapshot);
|
|
291
|
+
const retry = rec(row.retry);
|
|
292
|
+
const changed = list(snapshot?.files).filter((file) => typeof file === "string");
|
|
293
|
+
fields = {
|
|
294
|
+
agent: str(row.agent),
|
|
295
|
+
model: richModel(row.model),
|
|
296
|
+
finish: str(row.finish),
|
|
297
|
+
cost: num(row.cost),
|
|
298
|
+
tokens: richTokens(row.tokens),
|
|
299
|
+
error: richV2Error(row.error, clip),
|
|
300
|
+
retry: retry ? compact({ attempt: num(retry.attempt), at: num(retry.at), error: richV2Error(retry.error, clip) }) : undefined,
|
|
301
|
+
changedFiles: changed.length ? changed : undefined,
|
|
302
|
+
content: v2Content(row.content, clip),
|
|
303
|
+
};
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case "compaction":
|
|
307
|
+
fields = {
|
|
308
|
+
status: str(row.status),
|
|
309
|
+
reason: str(row.reason),
|
|
310
|
+
summary: clip.text(str(row.summary), limits.compactionSummary),
|
|
311
|
+
model: richModel(row.model),
|
|
312
|
+
error: richV2Error(row.error, clip),
|
|
313
|
+
cost: num(row.cost),
|
|
314
|
+
tokens: richTokens(row.tokens),
|
|
315
|
+
};
|
|
316
|
+
break;
|
|
317
|
+
case "idle":
|
|
318
|
+
fields = { outcome: str(row.outcome), error: richV2Error(row.error, clip) };
|
|
319
|
+
break;
|
|
320
|
+
case "agent-switched":
|
|
321
|
+
fields = { agent: str(row.agent), previous: str(row.previous) };
|
|
322
|
+
break;
|
|
323
|
+
case "model-switched":
|
|
324
|
+
fields = { model: richModel(row.model), previous: richModel(row.previous) };
|
|
325
|
+
break;
|
|
326
|
+
case "location-switched": {
|
|
327
|
+
const previous = rec(row.previous);
|
|
328
|
+
fields = {
|
|
329
|
+
directory: str(rec(row.location)?.directory),
|
|
330
|
+
subpath: str(row.subpath),
|
|
331
|
+
previousDirectory: str(rec(previous?.location)?.directory),
|
|
332
|
+
};
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
default:
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
const metadata = options.includeMetadata ? clip.json(nonEmpty(rec(row.metadata)), limits.toolMetadata) : undefined;
|
|
339
|
+
return compact({ ...base, ...fields, metadata, truncated: clip.truncated ? true : undefined });
|
|
340
|
+
}
|
|
341
|
+
/** v1 errors are `{ name, data: { message } }`. */
|
|
342
|
+
function richV1Error(value, clip) {
|
|
343
|
+
const error = rec(value);
|
|
344
|
+
if (!error)
|
|
345
|
+
return undefined;
|
|
346
|
+
const data = rec(error.data);
|
|
347
|
+
return compact({
|
|
348
|
+
type: str(error.name) ?? "UnknownError",
|
|
349
|
+
message: clip.text(str(data?.message) ?? str(error.message), clip.limits.errorMessage),
|
|
350
|
+
status: num(data?.statusCode),
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
function v1Tool(part, clip) {
|
|
354
|
+
const id = str(part.callID) ?? str(part.id);
|
|
355
|
+
const name = str(part.tool);
|
|
356
|
+
const state = rec(part.state);
|
|
357
|
+
if (!id || !name || !state)
|
|
358
|
+
return null;
|
|
359
|
+
const rawStatus = str(state.status) ?? "unknown";
|
|
360
|
+
const status = rawStatus === "pending" ? "streaming" : rawStatus;
|
|
361
|
+
const metadata = rec(state.metadata);
|
|
362
|
+
const time = rec(state.time);
|
|
363
|
+
const files = list(state.attachments).flatMap((file) => isRecord(file) ? [fileRef(str(file.filename), str(file.mime), str(file.url))] : []);
|
|
364
|
+
const errorText = str(state.error);
|
|
365
|
+
const call = {
|
|
366
|
+
type: "tool",
|
|
367
|
+
id,
|
|
368
|
+
name,
|
|
369
|
+
status,
|
|
370
|
+
input: clip.json(rawStatus === "pending" && str(state.raw) ? state.raw : state.input, clip.limits.toolInput),
|
|
371
|
+
output: clip.text(str(state.output), clip.limits.toolOutput),
|
|
372
|
+
files: files.length ? files : undefined,
|
|
373
|
+
error: errorText !== undefined ? { type: "ToolError", message: clip.text(errorText, clip.limits.errorMessage) } : undefined,
|
|
374
|
+
metadata: clip.json(nonEmpty(metadata), clip.limits.toolMetadata),
|
|
375
|
+
childSessionId: childSessionId(name, metadata),
|
|
376
|
+
time: time ? compact({ ran: num(time.start), completed: num(time.end) }) : undefined,
|
|
377
|
+
};
|
|
378
|
+
return compact(call);
|
|
379
|
+
}
|
|
380
|
+
/** Maps one v1 `{ info, parts }` message into the same shape as v2. */
|
|
381
|
+
export function richV1Message(message, limits, options = {}) {
|
|
382
|
+
if (!isRecord(message))
|
|
383
|
+
return null;
|
|
384
|
+
const info = rec(message.info);
|
|
385
|
+
const id = str(info?.id);
|
|
386
|
+
const role = str(info?.role);
|
|
387
|
+
if (!info || !id || !role)
|
|
388
|
+
return null;
|
|
389
|
+
const clip = new Clipper(limits);
|
|
390
|
+
const parts = list(message.parts).filter(isRecord);
|
|
391
|
+
const base = { id, type: role, time: richTime(info.time) };
|
|
392
|
+
if (role === "user") {
|
|
393
|
+
const text = parts
|
|
394
|
+
.filter((part) => part.type === "text" && part.ignored !== true && typeof part.text === "string")
|
|
395
|
+
.map((part) => str(part.text) ?? "")
|
|
396
|
+
.join("\n\n");
|
|
397
|
+
const files = parts.flatMap((part) => part.type === "file" ? [fileRef(str(part.filename), str(part.mime), str(part.url))] : []);
|
|
398
|
+
const agents = parts.flatMap((part) => (part.type === "agent" && typeof part.name === "string" ? [part.name] : []));
|
|
399
|
+
return compact({
|
|
400
|
+
...base,
|
|
401
|
+
text: clip.text(text, limits.text),
|
|
402
|
+
files: files.length ? files : undefined,
|
|
403
|
+
agents: agents.length ? agents : undefined,
|
|
404
|
+
truncated: clip.truncated ? true : undefined,
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
const content = [];
|
|
408
|
+
const changed = new Set();
|
|
409
|
+
for (const part of parts) {
|
|
410
|
+
if (part.type === "text" && part.ignored !== true) {
|
|
411
|
+
content.push(compact({
|
|
412
|
+
type: "text",
|
|
413
|
+
text: clip.text(str(part.text) ?? "", limits.text) ?? "",
|
|
414
|
+
synthetic: part.synthetic === true ? true : undefined,
|
|
415
|
+
}));
|
|
416
|
+
}
|
|
417
|
+
else if (part.type === "reasoning") {
|
|
418
|
+
const time = rec(part.time);
|
|
419
|
+
content.push(compact({
|
|
420
|
+
type: "reasoning",
|
|
421
|
+
text: clip.text(str(part.text) ?? "", limits.reasoning) ?? "",
|
|
422
|
+
time: time ? compact({ created: num(time.start), completed: num(time.end) }) : undefined,
|
|
423
|
+
}));
|
|
424
|
+
}
|
|
425
|
+
else if (part.type === "tool") {
|
|
426
|
+
const tool = v1Tool(part, clip);
|
|
427
|
+
if (tool)
|
|
428
|
+
content.push(tool);
|
|
429
|
+
}
|
|
430
|
+
else if (part.type === "file") {
|
|
431
|
+
content.push({ type: "file", file: fileRef(str(part.filename), str(part.mime), str(part.url)) });
|
|
432
|
+
}
|
|
433
|
+
else if (part.type === "patch") {
|
|
434
|
+
for (const file of list(part.files))
|
|
435
|
+
if (typeof file === "string")
|
|
436
|
+
changed.add(file);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const model = str(info.modelID) && str(info.providerID)
|
|
440
|
+
? compact({ providerId: str(info.providerID) ?? "", modelId: str(info.modelID) ?? "", variant: str(info.variant) })
|
|
441
|
+
: undefined;
|
|
442
|
+
return compact({
|
|
443
|
+
...base,
|
|
444
|
+
agent: str(info.agent) ?? str(info.mode),
|
|
445
|
+
model,
|
|
446
|
+
finish: str(info.finish),
|
|
447
|
+
cost: num(info.cost),
|
|
448
|
+
tokens: richTokens(info.tokens),
|
|
449
|
+
error: richV1Error(info.error, clip),
|
|
450
|
+
changedFiles: changed.size ? [...changed] : undefined,
|
|
451
|
+
content: budgetTools(content, clip),
|
|
452
|
+
metadata: options.includeMetadata ? clip.json(nonEmpty(rec(info.metadata)), limits.toolMetadata) : undefined,
|
|
453
|
+
truncated: clip.truncated ? true : undefined,
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* The newest (from end) or oldest (from start) messages that fit the result
|
|
458
|
+
* budget, in chronological order. Always keeps at least one message.
|
|
459
|
+
*/
|
|
460
|
+
export function fitSessionReadBudget(messages, from, budget = SESSION_READ_RESULT_BUDGET) {
|
|
461
|
+
const ordered = from === "end" ? [...messages].reverse() : messages;
|
|
462
|
+
const kept = [];
|
|
463
|
+
let used = 0;
|
|
464
|
+
for (const message of ordered) {
|
|
465
|
+
const size = JSON.stringify(message).length;
|
|
466
|
+
if (kept.length > 0 && used + size > budget)
|
|
467
|
+
break;
|
|
468
|
+
kept.push(message);
|
|
469
|
+
used += size;
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
messages: from === "end" ? kept.reverse() : kept,
|
|
473
|
+
omittedForSize: messages.length - kept.length,
|
|
474
|
+
};
|
|
475
|
+
}
|