@luxass/pi-cost-explorer 0.0.0 → 0.2.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/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 also include what its agents cost, as the source "Agents". Each agent run is one row, with its own model. Pi's `/session` command and the cost in Pi's footer do not include agents, so the total here is higher than theirs by that amount. The Overview says how much went to agents.
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.0.0",
3
+ "version": "0.2.0",
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": "*",
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,30 @@ function toolCallNames(message: AssistantMessage): string[] {
149
159
  return message.content.flatMap((part) => (part.type === "toolCall" ? [part.name] : []));
150
160
  }
151
161
 
162
+ /**
163
+ * Custom entry type that @luxass/pi-subagents writes once for each agent run. The agent's model
164
+ * requests are saved in the agent's own session, so this entry is how they reach this session.
165
+ */
166
+ const SUBAGENT_USAGE_ENTRY_TYPE = "subagents-usage";
167
+
168
+ /**
169
+ * The fields of a `subagents-usage` entry that the cost explorer reads. pi-subagents owns the
170
+ * full shape as `SubagentRunUsage`, and it saves Pi's own usage record in `usage`.
171
+ */
172
+ interface SubagentUsageEntryData {
173
+ /** The agent's task name. */
174
+ name: string;
175
+ provider: string;
176
+ model: string;
177
+ usage: Usage;
178
+ }
179
+
152
180
  /**
153
181
  * Builds every view of the session from its entries.
154
182
  *
155
183
  * Accounting follows Pi's `/session` totals: assistant messages, tool results with
156
184
  * nested usage, usage entries, and compaction or branch summaries that report usage.
185
+ * Agent runs saved by @luxass/pi-subagents are added on top, as their own source.
157
186
  */
158
187
  export function analyze(
159
188
  entries: readonly SessionEntry[],
@@ -238,6 +267,16 @@ export function analyze(
238
267
  });
239
268
  } else if ((entry.type === "compaction" || entry.type === "branch_summary") && entry.usage) {
240
269
  addRecord({ ...base, source: entry.type, label: entry.type, usage: entry.usage });
270
+ } else if (entry.type === "custom" && entry.customType === SUBAGENT_USAGE_ENTRY_TYPE) {
271
+ const run = entry.data as SubagentUsageEntryData;
272
+ addRecord({
273
+ ...base,
274
+ source: "agent",
275
+ label: run.name,
276
+ provider: run.provider,
277
+ model: run.model,
278
+ usage: run.usage,
279
+ });
241
280
  }
242
281
  continue;
243
282
  }
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