@luxass/pi-cost-explorer 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +49 -2
- package/package.json +46 -3
- package/src/analysis.ts +329 -0
- package/src/dialog.ts +784 -0
- package/src/format.ts +131 -0
- package/src/index.ts +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-PRESENT Lucas Nørgård
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,50 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @luxass/pi-cost-explorer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
See where a [Pi](https://pi.dev) session spent its money. The cost explorer breaks the session down by model, prompt, tool, and request in a tabbed dialog.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pi install npm:@luxass/pi-cost-explorer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
To try the extension from this checkout:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pi -e ./packages/pi-cost-explorer
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Use
|
|
18
|
+
|
|
19
|
+
Run `/cost` in Pi's terminal interface. The dialog has five tabs:
|
|
20
|
+
|
|
21
|
+
- **Overview** shows the total cost, token counts, and the cost split between input, output, cache reads, and cache writes. It also shows the cache hit rate, averages, a cost-over-time sparkline, cost by source, and the most expensive model, prompt, and tool.
|
|
22
|
+
- **Models** shows requests, tokens, cache hit rate, and cost for each provider and model.
|
|
23
|
+
- **Turns** lists each prompt you sent with the cost of everything it caused. Press Enter to see the prompt's breakdown and each usage record.
|
|
24
|
+
- **Tools** shows calls, errors, run time, and cost for each tool.
|
|
25
|
+
- **Requests** lists every priced event in time order. Press Enter to see its full token and cost breakdown.
|
|
26
|
+
|
|
27
|
+
The totals use the same sources as Pi's `/session` command: model responses, usage that tools report for their own model calls, background usage such as cache warming, and compaction and branch summaries.
|
|
28
|
+
|
|
29
|
+
A Pi session branches when you go back to an earlier message and continue from there. These session branches are not Git branches. By default the dialog covers every session branch, and rows from other branches are dimmed. Press `b` to show only the current branch. While the dialog is open, it refreshes after each turn.
|
|
30
|
+
|
|
31
|
+
| Controls | Action |
|
|
32
|
+
| ---------------------------- | -------------------------------------------------------- |
|
|
33
|
+
| Left, Right, Tab, Shift+Tab | Switch tabs. |
|
|
34
|
+
| `1`–`5` | Jump to a tab. |
|
|
35
|
+
| Up, Down, Page Up, Page Down | Move the selection or scroll. |
|
|
36
|
+
| Home, End | Go to the first or last row. |
|
|
37
|
+
| Enter | Open the selected prompt or request. |
|
|
38
|
+
| Esc | Go back from a detail screen, or close. |
|
|
39
|
+
| `s` | Switch between the two sort orders of a list. |
|
|
40
|
+
| `b` | Switch between all session branches and the current one. |
|
|
41
|
+
| `r` | Refresh. |
|
|
42
|
+
| `q` | Close. |
|
|
43
|
+
| Click or mouse wheel | Switch tabs, select or open rows, or scroll. |
|
|
44
|
+
|
|
45
|
+
Mouse input works in fullscreen mode only.
|
|
46
|
+
|
|
47
|
+
### Limits
|
|
48
|
+
|
|
49
|
+
- Costs come from the prices in Pi's model configuration. If a model has no price, its requests show $0 and the Overview shows a warning.
|
|
50
|
+
- The **≈ Calling** column in Tools is an estimate. One request can call several tools, so it splits the request's cost evenly between them. The **Nested** column is exact: it is usage that a tool reports for its own model calls, for example subagents.
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luxass/pi-cost-explorer",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Explore a Pi session's cost by model, prompt, tool, and request in a tabbed dialog.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cost",
|
|
7
|
+
"pi",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"pi-package",
|
|
10
|
+
"tokens",
|
|
11
|
+
"usage"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/luxass/agent-extensions",
|
|
17
|
+
"directory": "packages/pi-cost-explorer"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@earendil-works/pi-coding-agent": "1.4.0",
|
|
30
|
+
"@earendil-works/pi-tui": "1.4.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
34
|
+
"@earendil-works/pi-tui": "*"
|
|
35
|
+
},
|
|
36
|
+
"pi": {
|
|
37
|
+
"image": "https://raw.githubusercontent.com/luxass/agent-extensions/main/packages/pi-cost-explorer/docs/cover.png",
|
|
38
|
+
"extensions": [
|
|
39
|
+
"./src/index.ts"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"lint": "oxlint .",
|
|
44
|
+
"lint:fix": "oxlint --fix .",
|
|
45
|
+
"fmt": "oxfmt .",
|
|
46
|
+
"fmt:check": "oxfmt --check .",
|
|
47
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
48
|
+
}
|
|
6
49
|
}
|
package/src/analysis.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import type { SessionEntry, SessionMessageEntry } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
type UsageEntry = Extract<SessionEntry, { type: "usage" }>;
|
|
4
|
+
type AgentMessage = SessionMessageEntry["message"];
|
|
5
|
+
type AssistantMessage = Extract<AgentMessage, { role: "assistant" }>;
|
|
6
|
+
type UserMessage = Extract<AgentMessage, { role: "user" }>;
|
|
7
|
+
|
|
8
|
+
export type Usage = UsageEntry["usage"];
|
|
9
|
+
|
|
10
|
+
/** Where a usage record came from. Matches the sources Pi adds to its own session totals. */
|
|
11
|
+
export type UsageSource = "assistant" | "tool" | "usage" | "compaction" | "branch_summary";
|
|
12
|
+
|
|
13
|
+
export const SOURCE_LABELS: Record<UsageSource, string> = {
|
|
14
|
+
assistant: "Model requests",
|
|
15
|
+
tool: "Nested tool usage",
|
|
16
|
+
usage: "Background usage",
|
|
17
|
+
compaction: "Compaction summaries",
|
|
18
|
+
branch_summary: "Branch summaries",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Token counts and costs added up over a group of usage records. The fields match Pi's
|
|
23
|
+
* `Usage`, so one record's usage is already a set of totals.
|
|
24
|
+
*/
|
|
25
|
+
export type UsageTotals = Pick<Usage, TokenKind | "cost"> & { count: number };
|
|
26
|
+
|
|
27
|
+
const TOKEN_KINDS = ["input", "output", "cacheRead", "cacheWrite"] as const;
|
|
28
|
+
type TokenKind = (typeof TOKEN_KINDS)[number];
|
|
29
|
+
|
|
30
|
+
/** Token usage and cost from one session entry, such as a model response. */
|
|
31
|
+
export interface UsageRecord {
|
|
32
|
+
entryId: string;
|
|
33
|
+
source: UsageSource;
|
|
34
|
+
/** Model id, tool name, or usage kind. */
|
|
35
|
+
label: string;
|
|
36
|
+
provider?: string;
|
|
37
|
+
model?: string;
|
|
38
|
+
timestamp: number;
|
|
39
|
+
usage: Usage;
|
|
40
|
+
/** Entry id of the user prompt this record belongs to. */
|
|
41
|
+
turnId?: string;
|
|
42
|
+
/** Tool names requested by an assistant message. */
|
|
43
|
+
toolCalls: string[];
|
|
44
|
+
stopReason?: string;
|
|
45
|
+
onBranch: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface Turn {
|
|
49
|
+
id: string;
|
|
50
|
+
/** 1-based position among all prompts, in the order they were sent. */
|
|
51
|
+
index: number;
|
|
52
|
+
prompt: string;
|
|
53
|
+
timestamp: number;
|
|
54
|
+
records: UsageRecord[];
|
|
55
|
+
totals: UsageTotals;
|
|
56
|
+
toolCalls: number;
|
|
57
|
+
onBranch: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ModelStat {
|
|
61
|
+
key: string;
|
|
62
|
+
provider?: string;
|
|
63
|
+
model: string;
|
|
64
|
+
totals: UsageTotals;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ToolStat {
|
|
68
|
+
name: string;
|
|
69
|
+
calls: number;
|
|
70
|
+
errors: number;
|
|
71
|
+
durationMs: number;
|
|
72
|
+
/** Usage the tool reported for its own nested model calls. Exact. */
|
|
73
|
+
nested: UsageTotals;
|
|
74
|
+
/** Even split of each calling request's cost across its tool calls. An estimate. */
|
|
75
|
+
share: number;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface Analysis {
|
|
79
|
+
total: UsageTotals;
|
|
80
|
+
bySource: Map<UsageSource, UsageTotals>;
|
|
81
|
+
records: UsageRecord[];
|
|
82
|
+
turns: Turn[];
|
|
83
|
+
models: ModelStat[];
|
|
84
|
+
tools: ToolStat[];
|
|
85
|
+
/** Requests that used tokens but report zero cost, usually because pricing is unknown. */
|
|
86
|
+
unpriced: number;
|
|
87
|
+
/** Cost of requests that ended with an error or were aborted. */
|
|
88
|
+
failedCost: number;
|
|
89
|
+
failedCount: number;
|
|
90
|
+
/** Cost recorded outside the current session branch. Zero when only that branch is analyzed. */
|
|
91
|
+
offBranchCost: number;
|
|
92
|
+
firstTimestamp?: number;
|
|
93
|
+
lastTimestamp?: number;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Starts a new set of totals at zero. Every grouping in `analyze()` uses one. */
|
|
97
|
+
export function createUsageTotals(): UsageTotals {
|
|
98
|
+
return {
|
|
99
|
+
input: 0,
|
|
100
|
+
output: 0,
|
|
101
|
+
cacheRead: 0,
|
|
102
|
+
cacheWrite: 0,
|
|
103
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
104
|
+
count: 0,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function addUsage(totals: UsageTotals, usage: Usage): void {
|
|
109
|
+
for (const kind of TOKEN_KINDS) {
|
|
110
|
+
totals[kind] += usage[kind];
|
|
111
|
+
totals.cost[kind] += usage.cost[kind];
|
|
112
|
+
}
|
|
113
|
+
totals.cost.total += usage.cost.total;
|
|
114
|
+
totals.count += 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Sum of the four token kinds, as Pi counts it in `/session`. */
|
|
118
|
+
export function tokenCount(totals: UsageTotals): number {
|
|
119
|
+
return TOKEN_KINDS.reduce((sum, kind) => sum + totals[kind], 0);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Share of prompt tokens served from the cache. */
|
|
123
|
+
export function cacheHitRate(totals: UsageTotals): number | undefined {
|
|
124
|
+
const prompt = totals.input + totals.cacheRead + totals.cacheWrite;
|
|
125
|
+
return prompt > 0 ? totals.cacheRead / prompt : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function parseTimestamp(value: string): number {
|
|
129
|
+
const parsed = Date.parse(value);
|
|
130
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function promptText(message: UserMessage): string {
|
|
134
|
+
const raw =
|
|
135
|
+
typeof message.content === "string"
|
|
136
|
+
? message.content
|
|
137
|
+
: message.content.map((part) => (part.type === "text" ? part.text : "[image]")).join(" ");
|
|
138
|
+
const collapsed = raw.replace(/\s+/g, " ").trim();
|
|
139
|
+
return collapsed || "(empty prompt)";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isPriced(usage: Usage): boolean {
|
|
143
|
+
const tokens = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
144
|
+
return tokens === 0 || usage.cost.total > 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function toolCallNames(message: AssistantMessage): string[] {
|
|
148
|
+
if (!Array.isArray(message.content)) return [];
|
|
149
|
+
return message.content.flatMap((part) => (part.type === "toolCall" ? [part.name] : []));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Builds every view of the session from its entries.
|
|
154
|
+
*
|
|
155
|
+
* Accounting follows Pi's `/session` totals: assistant messages, tool results with
|
|
156
|
+
* nested usage, usage entries, and compaction or branch summaries that report usage.
|
|
157
|
+
*/
|
|
158
|
+
export function analyze(
|
|
159
|
+
entries: readonly SessionEntry[],
|
|
160
|
+
branchIds: ReadonlySet<string>,
|
|
161
|
+
): Analysis {
|
|
162
|
+
const total = createUsageTotals();
|
|
163
|
+
const bySource = new Map<UsageSource, UsageTotals>();
|
|
164
|
+
const records: UsageRecord[] = [];
|
|
165
|
+
const turns = new Map<string, Turn>();
|
|
166
|
+
const models = new Map<string, ModelStat>();
|
|
167
|
+
const tools = new Map<string, ToolStat>();
|
|
168
|
+
// Entries are append-only, so a parent always comes before its children.
|
|
169
|
+
const turnOf = new Map<string, string | undefined>();
|
|
170
|
+
|
|
171
|
+
let unpriced = 0;
|
|
172
|
+
let failedCost = 0;
|
|
173
|
+
let failedCount = 0;
|
|
174
|
+
let offBranchCost = 0;
|
|
175
|
+
|
|
176
|
+
const toolStat = (name: string): ToolStat => {
|
|
177
|
+
let stat = tools.get(name);
|
|
178
|
+
if (!stat) {
|
|
179
|
+
stat = { name, calls: 0, errors: 0, durationMs: 0, nested: createUsageTotals(), share: 0 };
|
|
180
|
+
tools.set(name, stat);
|
|
181
|
+
}
|
|
182
|
+
return stat;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const addRecord = (record: UsageRecord): void => {
|
|
186
|
+
records.push(record);
|
|
187
|
+
addUsage(total, record.usage);
|
|
188
|
+
|
|
189
|
+
let sourceTotals = bySource.get(record.source);
|
|
190
|
+
if (!sourceTotals) {
|
|
191
|
+
sourceTotals = createUsageTotals();
|
|
192
|
+
bySource.set(record.source, sourceTotals);
|
|
193
|
+
}
|
|
194
|
+
addUsage(sourceTotals, record.usage);
|
|
195
|
+
|
|
196
|
+
const modelKey = record.model
|
|
197
|
+
? `${record.provider ?? "unknown"}/${record.model}`
|
|
198
|
+
: `(${SOURCE_LABELS[record.source].toLowerCase()})`;
|
|
199
|
+
let modelStat = models.get(modelKey);
|
|
200
|
+
if (!modelStat) {
|
|
201
|
+
modelStat = {
|
|
202
|
+
key: modelKey,
|
|
203
|
+
provider: record.provider,
|
|
204
|
+
model: record.model ?? SOURCE_LABELS[record.source],
|
|
205
|
+
totals: createUsageTotals(),
|
|
206
|
+
};
|
|
207
|
+
models.set(modelKey, modelStat);
|
|
208
|
+
}
|
|
209
|
+
addUsage(modelStat.totals, record.usage);
|
|
210
|
+
|
|
211
|
+
if (!isPriced(record.usage)) unpriced += 1;
|
|
212
|
+
if (!record.onBranch) offBranchCost += record.usage.cost.total;
|
|
213
|
+
|
|
214
|
+
const turn = record.turnId ? turns.get(record.turnId) : undefined;
|
|
215
|
+
if (turn) {
|
|
216
|
+
turn.records.push(record);
|
|
217
|
+
addUsage(turn.totals, record.usage);
|
|
218
|
+
turn.toolCalls += record.toolCalls.length;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
for (const entry of entries) {
|
|
223
|
+
const parentTurn = entry.parentId ? turnOf.get(entry.parentId) : undefined;
|
|
224
|
+
const timestamp = parseTimestamp(entry.timestamp);
|
|
225
|
+
const onBranch = branchIds.has(entry.id);
|
|
226
|
+
|
|
227
|
+
if (entry.type !== "message") {
|
|
228
|
+
turnOf.set(entry.id, parentTurn);
|
|
229
|
+
const base = { entryId: entry.id, timestamp, turnId: parentTurn, toolCalls: [], onBranch };
|
|
230
|
+
if (entry.type === "usage") {
|
|
231
|
+
addRecord({
|
|
232
|
+
...base,
|
|
233
|
+
source: "usage",
|
|
234
|
+
label: entry.kind,
|
|
235
|
+
provider: entry.provider,
|
|
236
|
+
model: entry.model,
|
|
237
|
+
usage: entry.usage,
|
|
238
|
+
});
|
|
239
|
+
} else if ((entry.type === "compaction" || entry.type === "branch_summary") && entry.usage) {
|
|
240
|
+
addRecord({ ...base, source: entry.type, label: entry.type, usage: entry.usage });
|
|
241
|
+
}
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const message = entry.message;
|
|
246
|
+
if (message.role === "user") {
|
|
247
|
+
turnOf.set(entry.id, entry.id);
|
|
248
|
+
turns.set(entry.id, {
|
|
249
|
+
id: entry.id,
|
|
250
|
+
index: turns.size + 1,
|
|
251
|
+
prompt: promptText(message),
|
|
252
|
+
timestamp,
|
|
253
|
+
records: [],
|
|
254
|
+
totals: createUsageTotals(),
|
|
255
|
+
toolCalls: 0,
|
|
256
|
+
onBranch,
|
|
257
|
+
});
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
turnOf.set(entry.id, parentTurn);
|
|
262
|
+
|
|
263
|
+
if (message.role === "assistant") {
|
|
264
|
+
const calls = toolCallNames(message);
|
|
265
|
+
addRecord({
|
|
266
|
+
entryId: entry.id,
|
|
267
|
+
source: "assistant",
|
|
268
|
+
label: message.model,
|
|
269
|
+
provider: message.provider,
|
|
270
|
+
model: message.model,
|
|
271
|
+
timestamp: message.timestamp || timestamp,
|
|
272
|
+
usage: message.usage,
|
|
273
|
+
turnId: parentTurn,
|
|
274
|
+
toolCalls: calls,
|
|
275
|
+
stopReason: message.stopReason,
|
|
276
|
+
onBranch,
|
|
277
|
+
});
|
|
278
|
+
if (message.stopReason === "error" || message.stopReason === "aborted") {
|
|
279
|
+
failedCost += message.usage.cost.total;
|
|
280
|
+
failedCount += 1;
|
|
281
|
+
}
|
|
282
|
+
const share = calls.length > 0 ? message.usage.cost.total / calls.length : 0;
|
|
283
|
+
for (const name of calls) {
|
|
284
|
+
const stat = toolStat(name);
|
|
285
|
+
stat.calls += 1;
|
|
286
|
+
stat.share += share;
|
|
287
|
+
}
|
|
288
|
+
} else if (message.role === "toolResult") {
|
|
289
|
+
const stat = toolStat(message.toolName);
|
|
290
|
+
if (message.isError) stat.errors += 1;
|
|
291
|
+
stat.durationMs += message.durationMs ?? 0;
|
|
292
|
+
if (message.usage) {
|
|
293
|
+
addUsage(stat.nested, message.usage);
|
|
294
|
+
addRecord({
|
|
295
|
+
entryId: entry.id,
|
|
296
|
+
source: "tool",
|
|
297
|
+
label: message.toolName,
|
|
298
|
+
timestamp: message.timestamp || timestamp,
|
|
299
|
+
usage: message.usage,
|
|
300
|
+
turnId: parentTurn,
|
|
301
|
+
toolCalls: [],
|
|
302
|
+
onBranch,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const ordered = records.toSorted((left, right) => left.timestamp - right.timestamp);
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
total,
|
|
312
|
+
bySource,
|
|
313
|
+
records: ordered,
|
|
314
|
+
turns: [...turns.values()],
|
|
315
|
+
models: [...models.values()].toSorted(
|
|
316
|
+
(left, right) => right.totals.cost.total - left.totals.cost.total,
|
|
317
|
+
),
|
|
318
|
+
tools: [...tools.values()].toSorted(
|
|
319
|
+
(left, right) =>
|
|
320
|
+
right.share + right.nested.cost.total - (left.share + left.nested.cost.total),
|
|
321
|
+
),
|
|
322
|
+
unpriced,
|
|
323
|
+
failedCost,
|
|
324
|
+
failedCount,
|
|
325
|
+
offBranchCost,
|
|
326
|
+
firstTimestamp: ordered[0]?.timestamp,
|
|
327
|
+
lastTimestamp: ordered.at(-1)?.timestamp,
|
|
328
|
+
};
|
|
329
|
+
}
|