@tangle-network/agent-eval 0.124.0 → 0.126.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -35
- package/README.md +270 -189
- package/dist/analyst/index.d.ts +15 -145
- package/dist/analyst/index.js +33 -47
- package/dist/analyst/index.js.map +1 -1
- package/dist/benchmarks/index.d.ts +45 -162
- package/dist/benchmarks/index.js +8 -9
- package/dist/campaign/index.d.ts +3655 -5365
- package/dist/campaign/index.js +21 -95
- package/dist/{chunk-R226UZOI.js → chunk-474LBSOX.js} +2 -2
- package/dist/{chunk-W5B3ZGP3.js → chunk-4B7ZZHPX.js} +8 -6
- package/dist/{chunk-W5B3ZGP3.js.map → chunk-4B7ZZHPX.js.map} +1 -1
- package/dist/{chunk-DT7OXY3C.js → chunk-CM4OILD2.js} +535 -846
- package/dist/chunk-CM4OILD2.js.map +1 -0
- package/dist/{chunk-HM6V7F3M.js → chunk-FO7HEH76.js} +3 -3
- package/dist/chunk-IILEIWGW.js +635 -0
- package/dist/chunk-IILEIWGW.js.map +1 -0
- package/dist/{chunk-EQUK3RFS.js → chunk-J5SQWP6Y.js} +8 -5
- package/dist/chunk-J5SQWP6Y.js.map +1 -0
- package/dist/chunk-KO2PZOGP.js +4637 -0
- package/dist/chunk-KO2PZOGP.js.map +1 -0
- package/dist/{chunk-4Y7AAATF.js → chunk-LKKT3IVV.js} +574 -81
- package/dist/chunk-LKKT3IVV.js.map +1 -0
- package/dist/chunk-M7AH34KV.js +155 -0
- package/dist/chunk-M7AH34KV.js.map +1 -0
- package/dist/chunk-NTOV7RU5.js +7152 -0
- package/dist/chunk-NTOV7RU5.js.map +1 -0
- package/dist/{chunk-QFQZ3U3X.js → chunk-OCFJACJU.js} +2 -2
- package/dist/{chunk-GID26AN4.js → chunk-P22LJ3Y2.js} +4 -6
- package/dist/{chunk-GID26AN4.js.map → chunk-P22LJ3Y2.js.map} +1 -1
- package/dist/{chunk-SJT4OBVL.js → chunk-SDPM6554.js} +3 -3
- package/dist/{chunk-D5JZ7UDZ.js → chunk-UCLVDLCH.js} +136 -50
- package/dist/chunk-UCLVDLCH.js.map +1 -0
- package/dist/chunk-UI4YMIN2.js +105 -0
- package/dist/chunk-UI4YMIN2.js.map +1 -0
- package/dist/chunk-VBQ3CRKH.js +291 -0
- package/dist/chunk-VBQ3CRKH.js.map +1 -0
- package/dist/{chunk-JKDNAOF5.js → chunk-W4L6C2XT.js} +2 -2
- package/dist/{chunk-GRCDRKII.js → chunk-WS3NZZQQ.js} +58 -20
- package/dist/chunk-WS3NZZQQ.js.map +1 -0
- package/dist/cli.js +3 -3
- package/dist/contract/index.d.ts +3221 -3094
- package/dist/contract/index.js +173 -42
- package/dist/contract/index.js.map +1 -1
- package/dist/control.js +2 -3
- package/dist/fuzz.d.ts +14 -1
- package/dist/fuzz.js +1 -1
- package/dist/hosted/index.d.ts +8 -1
- package/dist/index.d.ts +208 -690
- package/dist/index.js +185 -500
- package/dist/index.js.map +1 -1
- package/dist/openapi.json +1 -1
- package/dist/rl.d.ts +5 -100
- package/dist/rl.js +4 -5
- package/dist/rl.js.map +1 -1
- package/dist/rollout/index.d.ts +9 -1
- package/dist/rollout/index.js +6 -6
- package/dist/{run-campaign-I3JXKVAK.js → run-campaign-LVFKZCEU.js} +3 -3
- package/dist/supervisor-run/index.d.ts +156 -4
- package/dist/supervisor-run/index.js +14 -2
- package/dist/traces.js +2 -3
- package/dist/wire/index.d.ts +14 -1
- package/dist/wire/index.js +3 -3
- package/docs/campaign-proposers.md +363 -168
- package/docs/design/loop-taxonomy.md +142 -190
- package/docs/design.md +1 -1
- package/docs/distributed-driver.md +8 -11
- package/docs/feature-guide.md +20 -19
- package/docs/knowledge-readiness.md +2 -5
- package/docs/multi-shot-optimization.md +35 -27
- package/docs/rollout.md +5 -5
- package/package.json +4 -4
- package/dist/chunk-4Y7AAATF.js.map +0 -1
- package/dist/chunk-5PVZVCZB.js +0 -9190
- package/dist/chunk-5PVZVCZB.js.map +0 -1
- package/dist/chunk-A6GT67HT.js +0 -550
- package/dist/chunk-A6GT67HT.js.map +0 -1
- package/dist/chunk-D5JZ7UDZ.js.map +0 -1
- package/dist/chunk-DT7OXY3C.js.map +0 -1
- package/dist/chunk-EQUK3RFS.js.map +0 -1
- package/dist/chunk-GC4ATIKK.js +0 -317
- package/dist/chunk-GC4ATIKK.js.map +0 -1
- package/dist/chunk-GRCDRKII.js.map +0 -1
- package/dist/chunk-LOW3U7JZ.js +0 -328
- package/dist/chunk-LOW3U7JZ.js.map +0 -1
- package/dist/chunk-MGGFVCJ7.js +0 -288
- package/dist/chunk-MGGFVCJ7.js.map +0 -1
- package/dist/chunk-PMITBABE.js +0 -3841
- package/dist/chunk-PMITBABE.js.map +0 -1
- package/dist/chunk-R7ZRE2KV.js +0 -138
- package/dist/chunk-R7ZRE2KV.js.map +0 -1
- /package/dist/{chunk-R226UZOI.js.map → chunk-474LBSOX.js.map} +0 -0
- /package/dist/{chunk-HM6V7F3M.js.map → chunk-FO7HEH76.js.map} +0 -0
- /package/dist/{chunk-QFQZ3U3X.js.map → chunk-OCFJACJU.js.map} +0 -0
- /package/dist/{chunk-SJT4OBVL.js.map → chunk-SDPM6554.js.map} +0 -0
- /package/dist/{chunk-JKDNAOF5.js.map → chunk-W4L6C2XT.js.map} +0 -0
- /package/dist/{run-campaign-I3JXKVAK.js.map → run-campaign-LVFKZCEU.js.map} +0 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// src/rollout/readers/claude-jsonl.ts
|
|
2
|
+
import { readdir, readFile } from "fs/promises";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
var DEFAULT_CLAUDE_PROJECTS_DIR = join(homedir(), ".claude", "projects");
|
|
6
|
+
function claudeProjectSlug(cwd) {
|
|
7
|
+
return cwd.replace(/[^a-zA-Z0-9-]/g, "-");
|
|
8
|
+
}
|
|
9
|
+
async function findClaudeTranscripts(cwd, projectsDir = DEFAULT_CLAUDE_PROJECTS_DIR) {
|
|
10
|
+
const dir = join(projectsDir, claudeProjectSlug(cwd));
|
|
11
|
+
const names = await readdir(dir).catch(() => []);
|
|
12
|
+
return names.filter((n) => n.endsWith(".jsonl")).sort().map((n) => ({ sessionId: n.replace(/\.jsonl$/, ""), path: join(dir, n) }));
|
|
13
|
+
}
|
|
14
|
+
var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
15
|
+
function parseClaudeEntries(raw) {
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const line of raw.split("\n")) {
|
|
18
|
+
if (!line.trim()) continue;
|
|
19
|
+
let entry;
|
|
20
|
+
try {
|
|
21
|
+
const parsed = JSON.parse(line);
|
|
22
|
+
if (!isRecord(parsed)) continue;
|
|
23
|
+
entry = parsed;
|
|
24
|
+
} catch {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (entry.type !== "user" && entry.type !== "assistant") continue;
|
|
28
|
+
const message = entry.message;
|
|
29
|
+
if (!isRecord(message)) continue;
|
|
30
|
+
out.push({
|
|
31
|
+
type: entry.type,
|
|
32
|
+
timestamp: typeof entry.timestamp === "string" ? entry.timestamp : null,
|
|
33
|
+
message,
|
|
34
|
+
toolUseResult: entry.toolUseResult,
|
|
35
|
+
isSidechain: entry.isSidechain === true,
|
|
36
|
+
agentId: typeof entry.agentId === "string" ? entry.agentId : null
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
41
|
+
function blockText(content) {
|
|
42
|
+
if (typeof content === "string") return content;
|
|
43
|
+
if (!Array.isArray(content)) return "";
|
|
44
|
+
return content.filter(
|
|
45
|
+
(b) => isRecord(b) && b.type === "text" && typeof b.text === "string"
|
|
46
|
+
).map((b) => b.text).join("\n");
|
|
47
|
+
}
|
|
48
|
+
async function readClaudeTranscript(path, options = {}) {
|
|
49
|
+
return transcriptFromEntries(parseClaudeEntries(await readFile(path, "utf8")), options);
|
|
50
|
+
}
|
|
51
|
+
function transcriptFromEntries(entries, options = {}) {
|
|
52
|
+
const wantSidechain = options.includeSidechain === true;
|
|
53
|
+
const messages = [];
|
|
54
|
+
const usage = { tokensIn: 0, tokensOut: 0, cacheRead: 0, cacheWrite: 0 };
|
|
55
|
+
let startedAt = null;
|
|
56
|
+
let endedAt = null;
|
|
57
|
+
let model = null;
|
|
58
|
+
let lastAssistantApiId = null;
|
|
59
|
+
let lastAssistantIndex = -1;
|
|
60
|
+
for (const entry of entries) {
|
|
61
|
+
if (entry.isSidechain !== wantSidechain) continue;
|
|
62
|
+
const message = entry.message;
|
|
63
|
+
if (entry.timestamp !== null) {
|
|
64
|
+
if (startedAt === null) startedAt = entry.timestamp;
|
|
65
|
+
endedAt = entry.timestamp;
|
|
66
|
+
}
|
|
67
|
+
if (entry.type === "user") {
|
|
68
|
+
lastAssistantApiId = null;
|
|
69
|
+
lastAssistantIndex = -1;
|
|
70
|
+
const content2 = message.content;
|
|
71
|
+
if (typeof content2 === "string") {
|
|
72
|
+
messages.push({ role: "user", content: content2 });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (!Array.isArray(content2)) continue;
|
|
76
|
+
let userText = "";
|
|
77
|
+
for (const block of content2) {
|
|
78
|
+
if (!isRecord(block)) continue;
|
|
79
|
+
if (block.type === "tool_result" && typeof block.tool_use_id === "string") {
|
|
80
|
+
messages.push({
|
|
81
|
+
role: "tool",
|
|
82
|
+
tool_call_id: block.tool_use_id,
|
|
83
|
+
content: blockText(block.content) || (typeof block.content === "string" ? block.content : "")
|
|
84
|
+
});
|
|
85
|
+
} else if (block.type === "text" && typeof block.text === "string") {
|
|
86
|
+
userText += (userText.length > 0 ? "\n" : "") + block.text;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (userText.length > 0) messages.push({ role: "user", content: userText });
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (typeof message.model === "string") model = message.model;
|
|
93
|
+
const apiId = typeof message.id === "string" ? message.id : null;
|
|
94
|
+
const continuesTurn = apiId !== null && apiId === lastAssistantApiId && lastAssistantIndex >= 0;
|
|
95
|
+
const msgUsage = message.usage;
|
|
96
|
+
if (isRecord(msgUsage) && !continuesTurn) {
|
|
97
|
+
usage.tokensIn += typeof msgUsage.input_tokens === "number" ? msgUsage.input_tokens : 0;
|
|
98
|
+
usage.tokensOut += typeof msgUsage.output_tokens === "number" ? msgUsage.output_tokens : 0;
|
|
99
|
+
usage.cacheRead += typeof msgUsage.cache_read_input_tokens === "number" ? msgUsage.cache_read_input_tokens : 0;
|
|
100
|
+
usage.cacheWrite += typeof msgUsage.cache_creation_input_tokens === "number" ? msgUsage.cache_creation_input_tokens : 0;
|
|
101
|
+
}
|
|
102
|
+
const content = message.content;
|
|
103
|
+
if (!Array.isArray(content)) continue;
|
|
104
|
+
let reasoning = "";
|
|
105
|
+
let text = "";
|
|
106
|
+
const toolCalls = [];
|
|
107
|
+
for (const block of content) {
|
|
108
|
+
if (!isRecord(block)) continue;
|
|
109
|
+
if (block.type === "thinking" && typeof block.thinking === "string" && block.thinking.length > 0) {
|
|
110
|
+
reasoning += (reasoning.length > 0 ? "\n" : "") + block.thinking;
|
|
111
|
+
} else if (block.type === "text" && typeof block.text === "string") {
|
|
112
|
+
text += (text.length > 0 ? "\n" : "") + block.text;
|
|
113
|
+
} else if (block.type === "tool_use" && typeof block.id === "string") {
|
|
114
|
+
toolCalls.push({
|
|
115
|
+
id: block.id,
|
|
116
|
+
type: "function",
|
|
117
|
+
function: {
|
|
118
|
+
name: typeof block.name === "string" ? block.name : "unknown",
|
|
119
|
+
arguments: JSON.stringify(block.input ?? {})
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue;
|
|
125
|
+
if (continuesTurn) {
|
|
126
|
+
const prev = messages[lastAssistantIndex];
|
|
127
|
+
if (text.length > 0) prev.content = prev.content === null ? text : `${prev.content}
|
|
128
|
+
${text}`;
|
|
129
|
+
if (reasoning.length > 0) {
|
|
130
|
+
prev.reasoning_content = prev.reasoning_content === void 0 ? reasoning : `${prev.reasoning_content}
|
|
131
|
+
${reasoning}`;
|
|
132
|
+
}
|
|
133
|
+
if (toolCalls.length > 0) prev.tool_calls = [...prev.tool_calls ?? [], ...toolCalls];
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
messages.push({
|
|
137
|
+
role: "assistant",
|
|
138
|
+
content: text.length > 0 ? text : null,
|
|
139
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
140
|
+
...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
|
|
141
|
+
});
|
|
142
|
+
lastAssistantApiId = apiId;
|
|
143
|
+
lastAssistantIndex = messages.length - 1;
|
|
144
|
+
}
|
|
145
|
+
return { messages, usage, startedAt, endedAt, model };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/rollout/readers/opencode-sqlite.ts
|
|
149
|
+
import { homedir as homedir2 } from "os";
|
|
150
|
+
import { join as join2 } from "path";
|
|
151
|
+
var DEFAULT_OPENCODE_DB = join2(homedir2(), ".local", "share", "opencode", "opencode.db");
|
|
152
|
+
var NODE_SQLITE_SPECIFIER = ["node", "sqlite"].join(":");
|
|
153
|
+
async function openOpencodeDb(path = DEFAULT_OPENCODE_DB) {
|
|
154
|
+
try {
|
|
155
|
+
const { DatabaseSync } = await import(
|
|
156
|
+
/* @vite-ignore */
|
|
157
|
+
NODE_SQLITE_SPECIFIER
|
|
158
|
+
);
|
|
159
|
+
const db = new DatabaseSync(path, { readOnly: true });
|
|
160
|
+
db.prepare("SELECT id FROM session LIMIT 1").get();
|
|
161
|
+
return db;
|
|
162
|
+
} catch {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
var isRecord2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
167
|
+
function parseSessionRow(row) {
|
|
168
|
+
let model = null;
|
|
169
|
+
if (typeof row.model === "string" && row.model.length > 0) {
|
|
170
|
+
try {
|
|
171
|
+
const parsed = JSON.parse(row.model);
|
|
172
|
+
if (isRecord2(parsed)) model = parsed;
|
|
173
|
+
} catch {
|
|
174
|
+
model = null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
id: String(row.id),
|
|
179
|
+
parentId: row.parent_id === null || row.parent_id === void 0 ? null : String(row.parent_id),
|
|
180
|
+
directory: String(row.directory),
|
|
181
|
+
agent: row.agent === null || row.agent === void 0 ? null : String(row.agent),
|
|
182
|
+
model,
|
|
183
|
+
costUsd: Number(row.cost ?? 0),
|
|
184
|
+
tokensInput: Number(row.tokens_input ?? 0),
|
|
185
|
+
tokensOutput: Number(row.tokens_output ?? 0),
|
|
186
|
+
tokensReasoning: Number(row.tokens_reasoning ?? 0),
|
|
187
|
+
tokensCacheRead: Number(row.tokens_cache_read ?? 0),
|
|
188
|
+
tokensCacheWrite: Number(row.tokens_cache_write ?? 0),
|
|
189
|
+
timeCreated: Number(row.time_created ?? 0),
|
|
190
|
+
timeUpdated: Number(row.time_updated ?? 0)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
var SESSION_COLUMNS = "id, parent_id, directory, agent, model, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, time_created, time_updated";
|
|
194
|
+
function findOpencodeSessionsByDirectory(db, directory) {
|
|
195
|
+
const rows = db.prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE directory = ? ORDER BY time_created`).all(directory);
|
|
196
|
+
return rows.map(parseSessionRow);
|
|
197
|
+
}
|
|
198
|
+
function findOpencodeSessionById(db, sessionId) {
|
|
199
|
+
const row = db.prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE id = ?`).get(sessionId);
|
|
200
|
+
return row === void 0 ? null : parseSessionRow(row);
|
|
201
|
+
}
|
|
202
|
+
function toolResultContent(output) {
|
|
203
|
+
if (typeof output === "string") return output;
|
|
204
|
+
if (output === null || output === void 0) return "";
|
|
205
|
+
return JSON.stringify(output);
|
|
206
|
+
}
|
|
207
|
+
function readOpencodeSessionMessages(db, sessionId) {
|
|
208
|
+
const messageRows = db.prepare("SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id").all(sessionId);
|
|
209
|
+
const partsStmt = db.prepare("SELECT data FROM part WHERE message_id = ? ORDER BY id");
|
|
210
|
+
const messages = [];
|
|
211
|
+
for (const messageRow of messageRows) {
|
|
212
|
+
let data;
|
|
213
|
+
try {
|
|
214
|
+
const parsed = JSON.parse(messageRow.data);
|
|
215
|
+
if (!isRecord2(parsed)) continue;
|
|
216
|
+
data = parsed;
|
|
217
|
+
} catch {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const parts = [];
|
|
221
|
+
for (const row of partsStmt.all(messageRow.id)) {
|
|
222
|
+
try {
|
|
223
|
+
const parsed = JSON.parse(row.data);
|
|
224
|
+
if (isRecord2(parsed)) parts.push(parsed);
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (data.role === "user") {
|
|
229
|
+
const text = parts.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("\n");
|
|
230
|
+
messages.push({ role: "user", content: text });
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (data.role !== "assistant") continue;
|
|
234
|
+
const steps = [];
|
|
235
|
+
let current = [];
|
|
236
|
+
for (const part of parts) {
|
|
237
|
+
if (part.type === "step-start") {
|
|
238
|
+
if (current.length > 0) steps.push(current);
|
|
239
|
+
current = [];
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (part.type === "step-finish" || part.type === "snapshot" || part.type === "patch") continue;
|
|
243
|
+
current.push(part);
|
|
244
|
+
}
|
|
245
|
+
if (current.length > 0) steps.push(current);
|
|
246
|
+
for (const step of steps) {
|
|
247
|
+
const reasoning = step.filter((p) => p.type === "reasoning" && typeof p.text === "string" && p.text.length > 0).map((p) => p.text).join("\n");
|
|
248
|
+
const text = step.filter((p) => p.type === "text" && typeof p.text === "string").map((p) => p.text).join("\n");
|
|
249
|
+
const toolParts = step.filter((p) => p.type === "tool" && typeof p.callID === "string");
|
|
250
|
+
const toolCalls = toolParts.map((p) => ({
|
|
251
|
+
id: p.callID,
|
|
252
|
+
type: "function",
|
|
253
|
+
function: {
|
|
254
|
+
name: p.tool ?? "unknown",
|
|
255
|
+
arguments: JSON.stringify(p.state?.input ?? {})
|
|
256
|
+
}
|
|
257
|
+
}));
|
|
258
|
+
if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue;
|
|
259
|
+
messages.push({
|
|
260
|
+
role: "assistant",
|
|
261
|
+
content: text.length > 0 ? text : null,
|
|
262
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
263
|
+
...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
|
|
264
|
+
});
|
|
265
|
+
for (const p of toolParts) {
|
|
266
|
+
messages.push({
|
|
267
|
+
role: "tool",
|
|
268
|
+
tool_call_id: p.callID,
|
|
269
|
+
name: p.tool ?? "unknown",
|
|
270
|
+
content: toolResultContent(p.state?.output)
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return messages;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export {
|
|
279
|
+
DEFAULT_CLAUDE_PROJECTS_DIR,
|
|
280
|
+
claudeProjectSlug,
|
|
281
|
+
findClaudeTranscripts,
|
|
282
|
+
parseClaudeEntries,
|
|
283
|
+
readClaudeTranscript,
|
|
284
|
+
transcriptFromEntries,
|
|
285
|
+
DEFAULT_OPENCODE_DB,
|
|
286
|
+
openOpencodeDb,
|
|
287
|
+
findOpencodeSessionsByDirectory,
|
|
288
|
+
findOpencodeSessionById,
|
|
289
|
+
readOpencodeSessionMessages
|
|
290
|
+
};
|
|
291
|
+
//# sourceMappingURL=chunk-VBQ3CRKH.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rollout/readers/claude-jsonl.ts","../src/rollout/readers/opencode-sqlite.ts"],"sourcesContent":["/**\n * Backfill reader over Claude Code project transcripts\n * (~/.claude/projects/<cwd-slug>/<sessionId>.jsonl) → canonical\n * chat-with-tools messages plus per-session token usage.\n *\n * Transcript lines consumed: type:\"user\" (string content or content blocks —\n * text + tool_result) and type:\"assistant\" (content blocks — thinking, text,\n * tool_use; message.usage carries tokens). Sidechain lines (isSidechain=true,\n * subagent threads) are separate invocations and are excluded from the main\n * transcript. Everything else (queue-operation, attachment, last-prompt…) is\n * transport metadata, not conversation.\n */\n\nimport { readdir, readFile } from 'node:fs/promises'\nimport { homedir } from 'node:os'\nimport { join } from 'node:path'\nimport type { ChatMessage, ChatToolCall } from '../schema'\n\nexport const DEFAULT_CLAUDE_PROJECTS_DIR = join(homedir(), '.claude', 'projects')\n\n/** Claude Code's project-directory slug for a working directory. */\nexport function claudeProjectSlug(cwd: string): string {\n return cwd.replace(/[^a-zA-Z0-9-]/g, '-')\n}\n\nexport interface ClaudeTranscriptRef {\n sessionId: string\n path: string\n}\n\n/** Transcript files recorded for sessions launched from `cwd`. */\nexport async function findClaudeTranscripts(\n cwd: string,\n projectsDir: string = DEFAULT_CLAUDE_PROJECTS_DIR,\n): Promise<ClaudeTranscriptRef[]> {\n const dir = join(projectsDir, claudeProjectSlug(cwd))\n const names = await readdir(dir).catch(() => [])\n return names\n .filter((n) => n.endsWith('.jsonl'))\n .sort()\n .map((n) => ({ sessionId: n.replace(/\\.jsonl$/, ''), path: join(dir, n) }))\n}\n\nexport interface ClaudeUsageTotals {\n tokensIn: number\n tokensOut: number\n cacheRead: number\n cacheWrite: number\n}\n\nexport interface ClaudeTranscript {\n messages: ChatMessage[]\n usage: ClaudeUsageTotals\n /** Timestamp of the first conversation line; null = empty transcript. */\n startedAt: string | null\n endedAt: string | null\n model: string | null\n}\n\nconst isRecord = (v: unknown): v is Record<string, unknown> =>\n typeof v === 'object' && v !== null && !Array.isArray(v)\n\n/**\n * One conversation line of a transcript, still in Claude Code's own shape.\n *\n * This is the single line-level parse of the format. `readClaudeTranscript`\n * projects it to canonical messages + usage; the supervision-tree reader\n * (`src/supervisor-run/claude-code-reader.ts`) projects the SAME entries to\n * spawn/settle/steer instants. Two projections, one parser — a second\n * transcript parser is how the two views silently disagree.\n */\nexport interface ClaudeEntry {\n readonly type: 'user' | 'assistant'\n /** ISO instant of the line; null when the line carried none. */\n readonly timestamp: string | null\n /** The Anthropic message body (`role`, `content`, `model`, `usage`). */\n readonly message: Record<string, unknown>\n /** Claude Code's structured tool result, when the line carries one. */\n readonly toolUseResult: unknown\n /** True on subagent threads — a separate invocation, not this transcript's turn. */\n readonly isSidechain: boolean\n /** Subagent id Claude Code stamps on sidechain lines; null on main-thread lines. */\n readonly agentId: string | null\n}\n\n/** Parse transcript jsonl text into conversation lines. Non-conversation lines are dropped. */\nexport function parseClaudeEntries(raw: string): ClaudeEntry[] {\n const out: ClaudeEntry[] = []\n for (const line of raw.split('\\n')) {\n if (!line.trim()) continue\n let entry: Record<string, unknown>\n try {\n const parsed: unknown = JSON.parse(line)\n if (!isRecord(parsed)) continue\n entry = parsed\n } catch {\n continue\n }\n if (entry.type !== 'user' && entry.type !== 'assistant') continue\n const message = entry.message\n if (!isRecord(message)) continue\n out.push({\n type: entry.type,\n timestamp: typeof entry.timestamp === 'string' ? entry.timestamp : null,\n message,\n toolUseResult: entry.toolUseResult,\n isSidechain: entry.isSidechain === true,\n agentId: typeof entry.agentId === 'string' ? entry.agentId : null,\n })\n }\n return out\n}\n\nexport interface ReadClaudeTranscriptOptions {\n /**\n * Read the sidechain (subagent) thread instead of skipping it. Subagent\n * transcripts under `<session>/subagents/agent-<id>.jsonl` are sidechain\n * lines end to end, so their usage is invisible without this.\n */\n readonly includeSidechain?: boolean\n}\n\nfunction blockText(content: unknown): string {\n if (typeof content === 'string') return content\n if (!Array.isArray(content)) return ''\n return content\n .filter(\n (b): b is Record<string, unknown> =>\n isRecord(b) && b.type === 'text' && typeof b.text === 'string',\n )\n .map((b) => b.text as string)\n .join('\\n')\n}\n\n/** Parse one transcript jsonl into canonical messages + usage totals. */\nexport async function readClaudeTranscript(\n path: string,\n options: ReadClaudeTranscriptOptions = {},\n): Promise<ClaudeTranscript> {\n return transcriptFromEntries(parseClaudeEntries(await readFile(path, 'utf8')), options)\n}\n\n/** The messages+usage projection of already-parsed entries. */\nexport function transcriptFromEntries(\n entries: readonly ClaudeEntry[],\n options: ReadClaudeTranscriptOptions = {},\n): ClaudeTranscript {\n const wantSidechain = options.includeSidechain === true\n const messages: ChatMessage[] = []\n const usage: ClaudeUsageTotals = { tokensIn: 0, tokensOut: 0, cacheRead: 0, cacheWrite: 0 }\n let startedAt: string | null = null\n let endedAt: string | null = null\n let model: string | null = null\n // Claude Code writes one jsonl line PER CONTENT BLOCK of an API message,\n // repeating message.id and usage on each — merge blocks into one canonical\n // assistant turn and count usage once per API message id.\n let lastAssistantApiId: string | null = null\n let lastAssistantIndex = -1\n\n for (const entry of entries) {\n if (entry.isSidechain !== wantSidechain) continue\n const message = entry.message\n if (entry.timestamp !== null) {\n if (startedAt === null) startedAt = entry.timestamp\n endedAt = entry.timestamp\n }\n\n if (entry.type === 'user') {\n lastAssistantApiId = null\n lastAssistantIndex = -1\n const content = message.content\n if (typeof content === 'string') {\n messages.push({ role: 'user', content })\n continue\n }\n if (!Array.isArray(content)) continue\n // A user line may interleave tool_result blocks (answers to the prior\n // assistant tool_use) with plain text; preserve order.\n let userText = ''\n for (const block of content) {\n if (!isRecord(block)) continue\n if (block.type === 'tool_result' && typeof block.tool_use_id === 'string') {\n messages.push({\n role: 'tool',\n tool_call_id: block.tool_use_id,\n content:\n blockText(block.content) || (typeof block.content === 'string' ? block.content : ''),\n })\n } else if (block.type === 'text' && typeof block.text === 'string') {\n userText += (userText.length > 0 ? '\\n' : '') + block.text\n }\n }\n if (userText.length > 0) messages.push({ role: 'user', content: userText })\n continue\n }\n\n // assistant\n if (typeof message.model === 'string') model = message.model\n const apiId = typeof message.id === 'string' ? message.id : null\n const continuesTurn = apiId !== null && apiId === lastAssistantApiId && lastAssistantIndex >= 0\n const msgUsage = message.usage\n if (isRecord(msgUsage) && !continuesTurn) {\n usage.tokensIn += typeof msgUsage.input_tokens === 'number' ? msgUsage.input_tokens : 0\n usage.tokensOut += typeof msgUsage.output_tokens === 'number' ? msgUsage.output_tokens : 0\n usage.cacheRead +=\n typeof msgUsage.cache_read_input_tokens === 'number' ? msgUsage.cache_read_input_tokens : 0\n usage.cacheWrite +=\n typeof msgUsage.cache_creation_input_tokens === 'number'\n ? msgUsage.cache_creation_input_tokens\n : 0\n }\n const content = message.content\n if (!Array.isArray(content)) continue\n let reasoning = ''\n let text = ''\n const toolCalls: ChatToolCall[] = []\n for (const block of content) {\n if (!isRecord(block)) continue\n if (\n block.type === 'thinking' &&\n typeof block.thinking === 'string' &&\n block.thinking.length > 0\n ) {\n reasoning += (reasoning.length > 0 ? '\\n' : '') + block.thinking\n } else if (block.type === 'text' && typeof block.text === 'string') {\n text += (text.length > 0 ? '\\n' : '') + block.text\n } else if (block.type === 'tool_use' && typeof block.id === 'string') {\n toolCalls.push({\n id: block.id,\n type: 'function',\n function: {\n name: typeof block.name === 'string' ? block.name : 'unknown',\n arguments: JSON.stringify(block.input ?? {}),\n },\n })\n }\n }\n if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue\n if (continuesTurn) {\n const prev = messages[lastAssistantIndex]!\n if (text.length > 0) prev.content = prev.content === null ? text : `${prev.content}\\n${text}`\n if (reasoning.length > 0) {\n prev.reasoning_content =\n prev.reasoning_content === undefined\n ? reasoning\n : `${prev.reasoning_content}\\n${reasoning}`\n }\n if (toolCalls.length > 0) prev.tool_calls = [...(prev.tool_calls ?? []), ...toolCalls]\n continue\n }\n messages.push({\n role: 'assistant',\n content: text.length > 0 ? text : null,\n ...(reasoning.length > 0 ? { reasoning_content: reasoning } : {}),\n ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),\n })\n lastAssistantApiId = apiId\n lastAssistantIndex = messages.length - 1\n }\n\n return { messages, usage, startedAt, endedAt, model }\n}\n","/**\n * Read-only backfill reader over the opencode sqlite store\n * (~/.local/share/opencode/opencode.db) → canonical chat-with-tools messages.\n *\n * Schema consumed (observed, 2026-07): `session` rows carry directory /\n * parent_id / agent / model / cost / tokens_*; `message` rows carry a JSON\n * `data` blob ({role, modelID, providerID, tokens, cost, finish}); `part`\n * rows carry the actual content ({type: text|reasoning|tool|step-start|\n * step-finish|snapshot…}). Tool parts hold {callID, state:{input, output,\n * status}} — both the call and its result, which we split into an assistant\n * tool_call plus a role:\"tool\" result message.\n *\n * The store is mutable and can be corrupt (a `.corrupt-bak` sibling ships\n * next to it in the wild), so `openOpencodeDb` returns null instead of\n * throwing — callers record a gap line, never crash the backfill.\n */\n\nimport { homedir } from 'node:os'\nimport { join } from 'node:path'\nimport type { DatabaseSync } from 'node:sqlite'\nimport type { ChatMessage, ChatToolCall } from '../schema'\n\nexport const DEFAULT_OPENCODE_DB = join(homedir(), '.local', 'share', 'opencode', 'opencode.db')\n\nexport interface OpencodeSessionRow {\n id: string\n parentId: string | null\n directory: string\n agent: string | null\n /** Raw session.model JSON: {id, providerID, variant} where present. */\n model: { id?: string; providerID?: string } | null\n costUsd: number\n tokensInput: number\n tokensOutput: number\n tokensReasoning: number\n tokensCacheRead: number\n tokensCacheWrite: number\n timeCreated: number\n timeUpdated: number\n}\n\n// Opaque specifier: esbuild (bundling) and Vite (tests) both rewrite an\n// analyzable dynamic import and strip the `node:` prefix under an es20xx\n// target, which turns this builtin into a bogus \"sqlite\" package lookup.\n// Composing the string at runtime defeats that analysis in both.\nconst NODE_SQLITE_SPECIFIER = ['node', 'sqlite'].join(':')\n\n/** Open the store read-only; null = unavailable/corrupt (caller records a gap). */\nexport async function openOpencodeDb(\n path: string = DEFAULT_OPENCODE_DB,\n): Promise<DatabaseSync | null> {\n try {\n const { DatabaseSync } = (await import(\n /* @vite-ignore */ NODE_SQLITE_SPECIFIER\n )) as typeof import('node:sqlite')\n const db = new DatabaseSync(path, { readOnly: true })\n // Probe: a corrupt store can open() fine and fail on first page read.\n db.prepare('SELECT id FROM session LIMIT 1').get()\n return db\n } catch {\n return null\n }\n}\n\nconst isRecord = (v: unknown): v is Record<string, unknown> =>\n typeof v === 'object' && v !== null && !Array.isArray(v)\n\nfunction parseSessionRow(row: Record<string, unknown>): OpencodeSessionRow {\n let model: OpencodeSessionRow['model'] = null\n if (typeof row.model === 'string' && row.model.length > 0) {\n try {\n const parsed: unknown = JSON.parse(row.model)\n if (isRecord(parsed)) model = parsed as { id?: string; providerID?: string }\n } catch {\n model = null\n }\n }\n return {\n id: String(row.id),\n parentId: row.parent_id === null || row.parent_id === undefined ? null : String(row.parent_id),\n directory: String(row.directory),\n agent: row.agent === null || row.agent === undefined ? null : String(row.agent),\n model,\n costUsd: Number(row.cost ?? 0),\n tokensInput: Number(row.tokens_input ?? 0),\n tokensOutput: Number(row.tokens_output ?? 0),\n tokensReasoning: Number(row.tokens_reasoning ?? 0),\n tokensCacheRead: Number(row.tokens_cache_read ?? 0),\n tokensCacheWrite: Number(row.tokens_cache_write ?? 0),\n timeCreated: Number(row.time_created ?? 0),\n timeUpdated: Number(row.time_updated ?? 0),\n }\n}\n\nconst SESSION_COLUMNS =\n 'id, parent_id, directory, agent, model, cost, tokens_input, tokens_output, tokens_reasoning, tokens_cache_read, tokens_cache_write, time_created, time_updated'\n\n/** Sessions whose cwd is `directory` (the worker-clone join key). */\nexport function findOpencodeSessionsByDirectory(\n db: DatabaseSync,\n directory: string,\n): OpencodeSessionRow[] {\n const rows = db\n .prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE directory = ? ORDER BY time_created`)\n .all(directory) as Array<Record<string, unknown>>\n return rows.map(parseSessionRow)\n}\n\nexport function findOpencodeSessionById(\n db: DatabaseSync,\n sessionId: string,\n): OpencodeSessionRow | null {\n const row = db.prepare(`SELECT ${SESSION_COLUMNS} FROM session WHERE id = ?`).get(sessionId) as\n | Record<string, unknown>\n | undefined\n return row === undefined ? null : parseSessionRow(row)\n}\n\ninterface OpencodePart {\n type?: string\n text?: string\n tool?: string\n callID?: string\n state?: { status?: string; input?: unknown; output?: unknown }\n}\n\nfunction toolResultContent(output: unknown): string {\n if (typeof output === 'string') return output\n if (output === null || output === undefined) return ''\n return JSON.stringify(output)\n}\n\n/**\n * Convert one session's message+part rows into canonical messages.\n * An opencode assistant message row spans several model steps; each step's\n * parts (reasoning → text → tool …) become one assistant message followed by\n * the role:\"tool\" results of its calls, preserving order.\n */\nexport function readOpencodeSessionMessages(db: DatabaseSync, sessionId: string): ChatMessage[] {\n const messageRows = db\n .prepare('SELECT id, data FROM message WHERE session_id = ? ORDER BY time_created, id')\n .all(sessionId) as Array<{ id: string; data: string }>\n const partsStmt = db.prepare('SELECT data FROM part WHERE message_id = ? ORDER BY id')\n\n const messages: ChatMessage[] = []\n for (const messageRow of messageRows) {\n let data: Record<string, unknown>\n try {\n const parsed: unknown = JSON.parse(messageRow.data)\n if (!isRecord(parsed)) continue\n data = parsed\n } catch {\n continue\n }\n const parts: OpencodePart[] = []\n for (const row of partsStmt.all(messageRow.id) as Array<{ data: string }>) {\n try {\n const parsed: unknown = JSON.parse(row.data)\n if (isRecord(parsed)) parts.push(parsed as OpencodePart)\n } catch {\n // Malformed part payload: skip the part, keep the message.\n }\n }\n\n if (data.role === 'user') {\n const text = parts\n .filter((p) => p.type === 'text' && typeof p.text === 'string')\n .map((p) => p.text as string)\n .join('\\n')\n messages.push({ role: 'user', content: text })\n continue\n }\n if (data.role !== 'assistant') continue\n\n // Split the row into steps at step-start boundaries; parts before the\n // first step-start (none observed, but tolerated) form an implicit step.\n const steps: OpencodePart[][] = []\n let current: OpencodePart[] = []\n for (const part of parts) {\n if (part.type === 'step-start') {\n if (current.length > 0) steps.push(current)\n current = []\n continue\n }\n if (part.type === 'step-finish' || part.type === 'snapshot' || part.type === 'patch') continue\n current.push(part)\n }\n if (current.length > 0) steps.push(current)\n\n for (const step of steps) {\n const reasoning = step\n .filter((p) => p.type === 'reasoning' && typeof p.text === 'string' && p.text.length > 0)\n .map((p) => p.text as string)\n .join('\\n')\n const text = step\n .filter((p) => p.type === 'text' && typeof p.text === 'string')\n .map((p) => p.text as string)\n .join('\\n')\n const toolParts = step.filter((p) => p.type === 'tool' && typeof p.callID === 'string')\n const toolCalls: ChatToolCall[] = toolParts.map((p) => ({\n id: p.callID as string,\n type: 'function',\n function: {\n name: p.tool ?? 'unknown',\n arguments: JSON.stringify(p.state?.input ?? {}),\n },\n }))\n if (reasoning.length === 0 && text.length === 0 && toolCalls.length === 0) continue\n messages.push({\n role: 'assistant',\n content: text.length > 0 ? text : null,\n ...(reasoning.length > 0 ? { reasoning_content: reasoning } : {}),\n ...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),\n })\n for (const p of toolParts) {\n messages.push({\n role: 'tool',\n tool_call_id: p.callID as string,\n name: p.tool ?? 'unknown',\n content: toolResultContent(p.state?.output),\n })\n }\n }\n }\n return messages\n}\n"],"mappings":";AAaA,SAAS,SAAS,gBAAgB;AAClC,SAAS,eAAe;AACxB,SAAS,YAAY;AAGd,IAAM,8BAA8B,KAAK,QAAQ,GAAG,WAAW,UAAU;AAGzE,SAAS,kBAAkB,KAAqB;AACrD,SAAO,IAAI,QAAQ,kBAAkB,GAAG;AAC1C;AAQA,eAAsB,sBACpB,KACA,cAAsB,6BACU;AAChC,QAAM,MAAM,KAAK,aAAa,kBAAkB,GAAG,CAAC;AACpD,QAAM,QAAQ,MAAM,QAAQ,GAAG,EAAE,MAAM,MAAM,CAAC,CAAC;AAC/C,SAAO,MACJ,OAAO,CAAC,MAAM,EAAE,SAAS,QAAQ,CAAC,EAClC,KAAK,EACL,IAAI,CAAC,OAAO,EAAE,WAAW,EAAE,QAAQ,YAAY,EAAE,GAAG,MAAM,KAAK,KAAK,CAAC,EAAE,EAA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1
1
|
import {
|
|
2
2
|
fsCampaignStorage,
|
|
3
3
|
runCampaign
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-UCLVDLCH.js";
|
|
5
5
|
import {
|
|
6
6
|
__export
|
|
7
7
|
} from "./chunk-PZ5AY32C.js";
|
|
@@ -763,4 +763,4 @@ export {
|
|
|
763
763
|
retrievalMetricsAtCutoff,
|
|
764
764
|
benchmarks_exports
|
|
765
765
|
};
|
|
766
|
-
//# sourceMappingURL=chunk-
|
|
766
|
+
//# sourceMappingURL=chunk-W4L6C2XT.js.map
|
|
@@ -187,7 +187,11 @@ var CostLedger = class {
|
|
|
187
187
|
}
|
|
188
188
|
/** Wait until every call started by this ledger has produced a durable outcome. */
|
|
189
189
|
async waitForIdle(options = {}) {
|
|
190
|
-
|
|
190
|
+
const hasActiveMatch = () => [...this.activeCallIds].some((callId) => {
|
|
191
|
+
const record = this.records.get(callId);
|
|
192
|
+
return record !== void 0 && matches(record, options.filter);
|
|
193
|
+
});
|
|
194
|
+
if (!hasActiveMatch()) return true;
|
|
191
195
|
const timeoutMs = options.timeoutMs ?? 5e3;
|
|
192
196
|
assertTimeout(timeoutMs, "waitForIdle.timeoutMs");
|
|
193
197
|
return new Promise((resolve) => {
|
|
@@ -197,10 +201,12 @@ var CostLedger = class {
|
|
|
197
201
|
if (timer !== void 0) clearTimeout(timer);
|
|
198
202
|
resolve(settled);
|
|
199
203
|
};
|
|
200
|
-
const onIdle = () =>
|
|
204
|
+
const onIdle = () => {
|
|
205
|
+
if (!hasActiveMatch()) finish(true);
|
|
206
|
+
};
|
|
201
207
|
this.idleWaiters.add(onIdle);
|
|
202
208
|
timer = setTimeout(() => finish(false), timeoutMs);
|
|
203
|
-
|
|
209
|
+
onIdle();
|
|
204
210
|
});
|
|
205
211
|
}
|
|
206
212
|
/** Settle a call left pending by a crashed process after reconciling with the provider. */
|
|
@@ -384,9 +390,7 @@ var CostLedger = class {
|
|
|
384
390
|
}
|
|
385
391
|
releaseActiveCall(callId) {
|
|
386
392
|
this.activeCallIds.delete(callId);
|
|
387
|
-
|
|
388
|
-
for (const resolve of this.idleWaiters) resolve();
|
|
389
|
-
this.idleWaiters.clear();
|
|
393
|
+
for (const resolve of [...this.idleWaiters]) resolve();
|
|
390
394
|
}
|
|
391
395
|
commitOutcome(pending, error, observed) {
|
|
392
396
|
try {
|
|
@@ -510,9 +514,23 @@ function costForUsage(model, usage) {
|
|
|
510
514
|
function costForTokenPricing(pricing, usage) {
|
|
511
515
|
assertTokenCount(usage.inputTokens, "usage.inputTokens");
|
|
512
516
|
assertTokenCount(usage.outputTokens, "usage.outputTokens");
|
|
517
|
+
if (usage.cachedTokens !== void 0) {
|
|
518
|
+
assertTokenCount(usage.cachedTokens, "usage.cachedTokens");
|
|
519
|
+
}
|
|
520
|
+
if (usage.cacheWriteTokens !== void 0) {
|
|
521
|
+
assertTokenCount(usage.cacheWriteTokens, "usage.cacheWriteTokens");
|
|
522
|
+
}
|
|
513
523
|
assertNonNegative(pricing.inputUsdPerMillion, "pricing.inputUsdPerMillion");
|
|
524
|
+
if (pricing.cachedInputUsdPerMillion !== void 0) {
|
|
525
|
+
assertNonNegative(pricing.cachedInputUsdPerMillion, "pricing.cachedInputUsdPerMillion");
|
|
526
|
+
}
|
|
527
|
+
if (pricing.cacheWriteUsdPerMillion !== void 0) {
|
|
528
|
+
assertNonNegative(pricing.cacheWriteUsdPerMillion, "pricing.cacheWriteUsdPerMillion");
|
|
529
|
+
}
|
|
514
530
|
assertNonNegative(pricing.outputUsdPerMillion, "pricing.outputUsdPerMillion");
|
|
515
|
-
|
|
531
|
+
const cachedInputRate = pricing.cachedInputUsdPerMillion ?? pricing.inputUsdPerMillion;
|
|
532
|
+
const cacheWriteRate = pricing.cacheWriteUsdPerMillion ?? pricing.inputUsdPerMillion;
|
|
533
|
+
return usage.inputTokens / 1e6 * pricing.inputUsdPerMillion + (usage.cachedTokens ?? 0) / 1e6 * cachedInputRate + (usage.cacheWriteTokens ?? 0) / 1e6 * cacheWriteRate + usage.outputTokens / 1e6 * pricing.outputUsdPerMillion;
|
|
516
534
|
}
|
|
517
535
|
async function settle(promise, signal) {
|
|
518
536
|
return await new Promise((resolve) => {
|
|
@@ -535,7 +553,16 @@ async function settle(promise, signal) {
|
|
|
535
553
|
function buildReceipt(pending, observed, error) {
|
|
536
554
|
assertUsage(observed);
|
|
537
555
|
assertString(observed.model, "receipt.model");
|
|
538
|
-
|
|
556
|
+
if (observed.actualCostUsd !== void 0 && observed.customTokenPricing !== void 0) {
|
|
557
|
+
throw new ValidationError(
|
|
558
|
+
"CostLedger: a receipt cannot have both actualCostUsd and customTokenPricing"
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
const customPricingSnapshot = observed.customTokenPricing === void 0 ? void 0 : pricingSnapshot(observed.customTokenPricing);
|
|
562
|
+
const estimated = customPricingSnapshot ? {
|
|
563
|
+
costUsd: costFromPricing(observed, customPricingSnapshot),
|
|
564
|
+
costUnknown: false
|
|
565
|
+
} : costForUsage(observed.model, observed);
|
|
539
566
|
const hasActual = observed.actualCostUsd !== void 0;
|
|
540
567
|
if (hasActual && observed.costUnknown === true) {
|
|
541
568
|
throw new ValidationError(
|
|
@@ -545,7 +572,11 @@ function buildReceipt(pending, observed, error) {
|
|
|
545
572
|
if (hasActual) assertNonNegative(observed.actualCostUsd, "actualCostUsd");
|
|
546
573
|
const usageUnknown = observed.usageUnknown === true;
|
|
547
574
|
const costUnknown = observed.costUnknown === true || !hasActual && (usageUnknown || estimated.costUnknown);
|
|
548
|
-
const
|
|
575
|
+
const resolvedModelPricing = !customPricingSnapshot && !hasActual && !costUnknown ? resolveModelPricing(observed.model) : null;
|
|
576
|
+
const receiptPricing = !hasActual && !costUnknown ? customPricingSnapshot ?? (resolvedModelPricing ? {
|
|
577
|
+
inputUsdPerThousand: resolvedModelPricing.input,
|
|
578
|
+
outputUsdPerThousand: resolvedModelPricing.output
|
|
579
|
+
} : void 0) : void 0;
|
|
549
580
|
return parseReceipt(
|
|
550
581
|
{
|
|
551
582
|
status: "settled",
|
|
@@ -562,12 +593,7 @@ function buildReceipt(pending, observed, error) {
|
|
|
562
593
|
costUsd: costUnknown ? 0 : hasActual ? observed.actualCostUsd : estimated.costUsd,
|
|
563
594
|
costUnknown,
|
|
564
595
|
usageUnknown,
|
|
565
|
-
...
|
|
566
|
-
pricing: {
|
|
567
|
-
inputUsdPerThousand: resolvedPricing.input,
|
|
568
|
-
outputUsdPerThousand: resolvedPricing.output
|
|
569
|
-
}
|
|
570
|
-
} : {},
|
|
596
|
+
...receiptPricing ? { pricing: receiptPricing } : {},
|
|
571
597
|
...hasActual ? { actualCostUsd: observed.actualCostUsd } : {},
|
|
572
598
|
...pending.maximumCostUsd === void 0 ? {} : { maximumCostUsd: pending.maximumCostUsd },
|
|
573
599
|
...error ? { error } : {},
|
|
@@ -580,11 +606,12 @@ function buildReceipt(pending, observed, error) {
|
|
|
580
606
|
var NonEmptyString = z.string().refine((value) => value.trim().length > 0, "must be non-empty");
|
|
581
607
|
var TokenCount = z.number().int().nonnegative().finite();
|
|
582
608
|
var NonNegative = z.number().nonnegative().finite();
|
|
583
|
-
var Positive = z.number().positive().finite();
|
|
584
609
|
var Tags = z.record(NonEmptyString, z.string());
|
|
585
610
|
var CostPricingSchema = z.strictObject({
|
|
586
|
-
inputUsdPerThousand:
|
|
587
|
-
|
|
611
|
+
inputUsdPerThousand: NonNegative,
|
|
612
|
+
cachedInputUsdPerThousand: NonNegative.optional(),
|
|
613
|
+
cacheWriteUsdPerThousand: NonNegative.optional(),
|
|
614
|
+
outputUsdPerThousand: NonNegative
|
|
588
615
|
});
|
|
589
616
|
var CostCallBaseShape = {
|
|
590
617
|
callId: NonEmptyString,
|
|
@@ -780,7 +807,18 @@ function cloneReceipt(receipt) {
|
|
|
780
807
|
};
|
|
781
808
|
}
|
|
782
809
|
function costFromPricing(usage, pricing) {
|
|
783
|
-
|
|
810
|
+
const cachedInputRate = pricing.cachedInputUsdPerThousand ?? pricing.inputUsdPerThousand;
|
|
811
|
+
const cacheWriteRate = pricing.cacheWriteUsdPerThousand ?? pricing.inputUsdPerThousand;
|
|
812
|
+
return usage.inputTokens / 1e3 * pricing.inputUsdPerThousand + (usage.cachedTokens ?? 0) / 1e3 * cachedInputRate + (usage.cacheWriteTokens ?? 0) / 1e3 * cacheWriteRate + usage.outputTokens / 1e3 * pricing.outputUsdPerThousand;
|
|
813
|
+
}
|
|
814
|
+
function pricingSnapshot(pricing) {
|
|
815
|
+
costForTokenPricing(pricing, { inputTokens: 0, outputTokens: 0 });
|
|
816
|
+
return {
|
|
817
|
+
inputUsdPerThousand: pricing.inputUsdPerMillion / 1e3,
|
|
818
|
+
...pricing.cachedInputUsdPerMillion === void 0 ? {} : { cachedInputUsdPerThousand: pricing.cachedInputUsdPerMillion / 1e3 },
|
|
819
|
+
...pricing.cacheWriteUsdPerMillion === void 0 ? {} : { cacheWriteUsdPerThousand: pricing.cacheWriteUsdPerMillion / 1e3 },
|
|
820
|
+
outputUsdPerThousand: pricing.outputUsdPerMillion / 1e3
|
|
821
|
+
};
|
|
784
822
|
}
|
|
785
823
|
function emptyRollup(channel) {
|
|
786
824
|
return {
|
|
@@ -888,4 +926,4 @@ export {
|
|
|
888
926
|
costForUsage,
|
|
889
927
|
costForTokenPricing
|
|
890
928
|
};
|
|
891
|
-
//# sourceMappingURL=chunk-
|
|
929
|
+
//# sourceMappingURL=chunk-WS3NZZQQ.js.map
|