@luxass/pi-subagents 0.2.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
@@ -93,6 +93,6 @@ If you switch branches with `/tree`, a background result arrives only on a branc
93
93
 
94
94
  ## Costs
95
95
 
96
- The cost in Pi's footer counts only the main session. The footer's status line shows what agents cost in the session, for example `agents $0.123`. It appears when an agent has used a model, and it includes agents that are still running.
96
+ With Pi 1.8 or later, new agent runs count toward the cost in Pi's footer and the totals in `/session`. On earlier versions, the extension saves usage separately. The footer also shows an agent subtotal, for example `agents $0.123`, that includes agents still running.
97
97
 
98
- When a run ends, its token usage and cost are saved in the session as a `subagents-usage` entry. A run stopped by `/reload` or by quitting Pi saves what it used up to then. [`@luxass/pi-cost-explorer`](../pi-cost-explorer/README.md) adds these entries to `/cost` as the source "Agents".
98
+ The extension saves token usage and cost when a run ends, including when `/reload` or quitting Pi interrupts it. [`@luxass/pi-cost-explorer`](../pi-cost-explorer/README.md) shows both new and older agent usage under "Agents".
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luxass/pi-subagents",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Delegate tasks to named Pi agents and follow their conversations.",
5
5
  "keywords": [
6
6
  "pi",
package/src/costs.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type { Usage } from "@earendil-works/pi-ai";
2
+ import type { ExtensionAPI, SessionEntry } from "@earendil-works/pi-coding-agent";
2
3
 
3
- /**
4
- * Custom entry type that stores what one agent run used, in the main session. It is written once
5
- * per run, when the run ends. Its data is a `SubagentRunUsage`.
6
- */
4
+ /** Identifies this extension's native usage entries in the main session. */
5
+ export const SUBAGENT_USAGE_KIND = "pi-subagents";
6
+
7
+ /** Custom entry type for one run's usage on hosts without `pi.appendUsage`. */
7
8
  export const SUBAGENT_USAGE_ENTRY = "subagents-usage";
8
9
 
9
10
  /** What one agent run used. A run that used no tokens saves none. */
@@ -20,6 +21,28 @@ export interface SubagentRunUsage {
20
21
  usage: Usage;
21
22
  }
22
23
 
24
+ type SubagentUsageRecord = Pick<
25
+ Extract<SessionEntry, { type: "usage" }>,
26
+ "kind" | "provider" | "model" | "usage" | "note"
27
+ >;
28
+
29
+ /** Writes one form of usage per run so readers never count a run twice. */
30
+ export function appendSubagentRunUsage(pi: ExtensionAPI, run: SubagentRunUsage): void {
31
+ // The workspace types predate Pi 1.8. Detect the API on the running host.
32
+ const host = pi as ExtensionAPI & { appendUsage?: (record: SubagentUsageRecord) => void };
33
+ if (typeof host.appendUsage === "function") {
34
+ host.appendUsage({
35
+ kind: SUBAGENT_USAGE_KIND,
36
+ provider: run.provider,
37
+ model: run.model,
38
+ usage: run.usage,
39
+ note: run.name,
40
+ });
41
+ } else {
42
+ pi.appendEntry(SUBAGENT_USAGE_ENTRY, run);
43
+ }
44
+ }
45
+
23
46
  /** A usage total that starts at zero. */
24
47
  export function emptySubagentUsage(): Usage {
25
48
  return {
package/src/manager.ts CHANGED
@@ -16,9 +16,11 @@ import {
16
16
  import { isSubagentName, type SubagentDefinition } from "./agents.ts";
17
17
  import {
18
18
  addSubagentUsage,
19
+ appendSubagentRunUsage,
19
20
  emptySubagentUsage,
20
21
  hasSubagentUsage,
21
22
  SUBAGENT_USAGE_ENTRY,
23
+ SUBAGENT_USAGE_KIND,
22
24
  type SubagentRunUsage,
23
25
  } from "./costs.ts";
24
26
  import { subagentMessageText } from "./message-text.ts";
@@ -241,7 +243,10 @@ export class SubagentManager {
241
243
  */
242
244
  restore(): void {
243
245
  for (const entry of this.ctx.sessionManager.getEntries()) {
244
- if (entry.type === "custom" && entry.customType === SUBAGENT_USAGE_ENTRY) {
246
+ if (entry.type === "usage" && entry.kind === SUBAGENT_USAGE_KIND) {
247
+ this.savedCost += entry.usage.cost.total;
248
+ this.savedRuns++;
249
+ } else if (entry.type === "custom" && entry.customType === SUBAGENT_USAGE_ENTRY) {
245
250
  this.savedCost += (entry.data as SubagentRunUsage).usage.cost.total;
246
251
  this.savedRuns++;
247
252
  }
@@ -645,9 +650,10 @@ export class SubagentManager {
645
650
  run: record.run,
646
651
  provider: live.policy.model.provider,
647
652
  model: live.policy.model.id,
648
- usage: live.usage,
653
+ // A child can still report usage while it is being disposed during shutdown.
654
+ usage: { ...live.usage, cost: { ...live.usage.cost } },
649
655
  };
650
- this.pi.appendEntry(SUBAGENT_USAGE_ENTRY, data);
656
+ appendSubagentRunUsage(this.pi, data);
651
657
  this.savedCost += live.usage.cost.total;
652
658
  this.savedRuns++;
653
659
  }
@@ -7,8 +7,8 @@ export const SUBAGENT_COST_STATUS = "subagents";
7
7
 
8
8
  /**
9
9
  * Shows what this session's agents cost in the footer's status line, for example `agents $0.123`.
10
- * Pi's own cost total counts only the main session, and an extension cannot add to it, so agent
11
- * costs get their own number. It appears once an agent has used a model and follows running agents.
10
+ * This subtotal follows running agents. Pi's own total only includes agent runs saved through
11
+ * `pi.appendUsage`.
12
12
  */
13
13
  export function installSubagentCostStatus(ctx: ExtensionContext, manager: SubagentManager): void {
14
14
  let shown: string | undefined;