@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 +2 -2
- package/package.json +1 -1
- package/src/costs.ts +27 -4
- package/src/manager.ts +9 -3
- package/src/ui/cost-status.ts +2 -2
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
|
-
|
|
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
|
-
|
|
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
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
|
-
|
|
5
|
-
|
|
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 === "
|
|
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
|
|
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
|
|
656
|
+
appendSubagentRunUsage(this.pi, data);
|
|
651
657
|
this.savedCost += live.usage.cost.total;
|
|
652
658
|
this.savedRuns++;
|
|
653
659
|
}
|
package/src/ui/cost-status.ts
CHANGED
|
@@ -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
|
|
11
|
-
*
|
|
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;
|