@luxass/pi-cost-explorer 0.1.0 → 0.2.1

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 CHANGED
@@ -26,6 +26,8 @@ Run `/cost` in Pi's terminal interface. The dialog has five tabs:
26
26
 
27
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
28
 
29
+ If you use `@luxass/pi-subagents`, the totals include its runs under "Agents". Each run has one row with its model. Runs saved with `pi.appendUsage()` also count in Pi's footer and `/session`. Runs saved in the older format count in `/cost` but not in Pi's totals.
30
+
29
31
  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
32
 
31
33
  | Controls | Action |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luxass/pi-cost-explorer",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Explore a Pi session's cost by model, prompt, tool, and request in a tabbed dialog.",
5
5
  "keywords": [
6
6
  "cost",
@@ -26,8 +26,8 @@
26
26
  "access": "public"
27
27
  },
28
28
  "devDependencies": {
29
- "@earendil-works/pi-coding-agent": "1.4.0",
30
- "@earendil-works/pi-tui": "1.4.0"
29
+ "@earendil-works/pi-coding-agent": "1.6.0",
30
+ "@earendil-works/pi-tui": "1.6.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@earendil-works/pi-coding-agent": "*",
@@ -44,6 +44,7 @@
44
44
  "lint:fix": "oxlint --fix .",
45
45
  "fmt": "oxfmt .",
46
46
  "fmt:check": "oxfmt --check .",
47
- "typecheck": "tsc -p tsconfig.json"
47
+ "typecheck": "tsc -p tsconfig.json",
48
+ "test": "node --test \"test/**/*.test.ts\""
48
49
  }
49
50
  }
package/src/analysis.ts CHANGED
@@ -7,13 +7,23 @@ type UserMessage = Extract<AgentMessage, { role: "user" }>;
7
7
 
8
8
  export type Usage = UsageEntry["usage"];
9
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";
10
+ /**
11
+ * Where a usage record came from. All but `agent` are the sources Pi adds to its own session
12
+ * totals. `agent` is an agent run saved by @luxass/pi-subagents, which Pi's totals leave out.
13
+ */
14
+ export type UsageSource =
15
+ | "assistant"
16
+ | "tool"
17
+ | "usage"
18
+ | "agent"
19
+ | "compaction"
20
+ | "branch_summary";
12
21
 
13
22
  export const SOURCE_LABELS: Record<UsageSource, string> = {
14
23
  assistant: "Model requests",
15
24
  tool: "Nested tool usage",
16
25
  usage: "Background usage",
26
+ agent: "Agents",
17
27
  compaction: "Compaction summaries",
18
28
  branch_summary: "Branch summaries",
19
29
  };
@@ -31,7 +41,7 @@ type TokenKind = (typeof TOKEN_KINDS)[number];
31
41
  export interface UsageRecord {
32
42
  entryId: string;
33
43
  source: UsageSource;
34
- /** Model id, tool name, or usage kind. */
44
+ /** Model id, tool name, usage kind, or agent task name. */
35
45
  label: string;
36
46
  provider?: string;
37
47
  model?: string;
@@ -149,11 +159,31 @@ function toolCallNames(message: AssistantMessage): string[] {
149
159
  return message.content.flatMap((part) => (part.type === "toolCall" ? [part.name] : []));
150
160
  }
151
161
 
162
+ /**
163
+ * Native usage kind and legacy custom entry type written by @luxass/pi-subagents.
164
+ * Each run uses one format, depending on which Pi APIs are available.
165
+ */
166
+ const SUBAGENT_USAGE_KIND = "pi-subagents";
167
+ const SUBAGENT_USAGE_ENTRY_TYPE = "subagents-usage";
168
+
169
+ /**
170
+ * The fields of a `subagents-usage` entry that the cost explorer reads. pi-subagents owns the
171
+ * full shape as `SubagentRunUsage`, and it saves Pi's own usage record in `usage`.
172
+ */
173
+ interface SubagentUsageEntryData {
174
+ /** The agent's task name. */
175
+ name: string;
176
+ provider: string;
177
+ model: string;
178
+ usage: Usage;
179
+ }
180
+
152
181
  /**
153
182
  * Builds every view of the session from its entries.
154
183
  *
155
184
  * Accounting follows Pi's `/session` totals: assistant messages, tool results with
156
185
  * nested usage, usage entries, and compaction or branch summaries that report usage.
186
+ * Agent runs from both native usage and legacy custom entries appear under Agents.
157
187
  */
158
188
  export function analyze(
159
189
  entries: readonly SessionEntry[],
@@ -228,16 +258,27 @@ export function analyze(
228
258
  turnOf.set(entry.id, parentTurn);
229
259
  const base = { entryId: entry.id, timestamp, turnId: parentTurn, toolCalls: [], onBranch };
230
260
  if (entry.type === "usage") {
261
+ const isSubagent = entry.kind === SUBAGENT_USAGE_KIND;
231
262
  addRecord({
232
263
  ...base,
233
- source: "usage",
234
- label: entry.kind,
264
+ source: isSubagent ? "agent" : "usage",
265
+ label: isSubagent ? (entry.note ?? entry.kind) : entry.kind,
235
266
  provider: entry.provider,
236
267
  model: entry.model,
237
268
  usage: entry.usage,
238
269
  });
239
270
  } else if ((entry.type === "compaction" || entry.type === "branch_summary") && entry.usage) {
240
271
  addRecord({ ...base, source: entry.type, label: entry.type, usage: entry.usage });
272
+ } else if (entry.type === "custom" && entry.customType === SUBAGENT_USAGE_ENTRY_TYPE) {
273
+ const run = entry.data as SubagentUsageEntryData;
274
+ addRecord({
275
+ ...base,
276
+ source: "agent",
277
+ label: run.name,
278
+ provider: run.provider,
279
+ model: run.model,
280
+ usage: run.usage,
281
+ });
241
282
  }
242
283
  continue;
243
284
  }
package/src/dialog.ts CHANGED
@@ -480,7 +480,10 @@ function overviewScreen({ analysis, theme, width, onlyCurrentBranch }: ScreenCon
480
480
  }
481
481
 
482
482
  const warning = theme.fg("warning", "!");
483
+ const agents = analysis.bySource.get("agent");
483
484
  const notes = [
485
+ agents !== undefined &&
486
+ `${theme.fg("muted", "·")} ${money(agents.cost.total)} went to ${plural(agents.count, "agent run")}. The cost in Pi's footer does not include agents.`,
484
487
  analysis.unpriced > 0 &&
485
488
  `${warning} ${plural(analysis.unpriced, "usage record")} with tokens but no cost. Pi has no price for that model, so totals are too low.`,
486
489
  analysis.failedCount > 0 &&
@@ -754,6 +757,7 @@ function recordLabel(record: UsageRecord): string {
754
757
  if (record.source === "assistant") return record.label;
755
758
  if (record.source === "tool") return `↳ ${record.label} (tool)`;
756
759
  if (record.source === "usage") return `${record.label} · ${record.model ?? "unknown"}`;
760
+ if (record.source === "agent") return `↳ ${record.label} (agent)`;
757
761
  return SOURCE_LABELS[record.source];
758
762
  }
759
763