pi-petrroll 0.1.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
@@ -5,12 +5,13 @@ Small, focused extensions for [Pi](https://pi.dev). MIT licensed.
5
5
  | Extension | Command | Agent tool |
6
6
  | --- | --- | --- |
7
7
  | [Change directory](#change-directory) | `/cd <path>` | `change_directory` |
8
+ | [External costs](#external-costs) | — | `add_cost` |
8
9
 
9
- One extension, one command, one tool. No skills, shell wrappers, or replacement built-in tools.
10
+ Independent, focused extensions. No skills, shell wrappers, or replacement built-in tools.
10
11
 
11
12
  ## Install
12
13
 
13
- Requires **interactive Pi 0.85.1 or newer** (`@earendil-works/pi-coding-agent`); tested against 0.85.1. Earlier Pi versions may not rebuild cwd-bound services when switching sessions.
14
+ Requires **Pi 0.85.1 or newer** (`@earendil-works/pi-coding-agent`); tested against 0.85.1. Change directory requires interactive mode; external cost recording works in all modes. Earlier Pi versions may not rebuild cwd-bound services when switching sessions.
14
15
 
15
16
  ```sh
16
17
  pi install npm:pi-petrroll
@@ -30,7 +31,23 @@ For local development, use this checkout instead of the npm or GitHub install:
30
31
  pi install /absolute/path/to/pi-petrroll
31
32
  ```
32
33
 
33
- Choose one installation source, not both. Use `pi config` to enable/disable individual extensions as this collection grows.
34
+ Choose one installation source, not multiple.
35
+
36
+ ### Enable only what you need
37
+
38
+ One package does **not** mean all extensions must be enabled. Run `pi config` to toggle each extension independently, or `pi config -l` for project-specific choices. Then `/reload` running sessions.
39
+
40
+ All extensions load by default. To load only `cd`, including after future updates, use this package entry in `~/.pi/agent/settings.json`:
41
+
42
+ ```json
43
+ {
44
+ "packages": [
45
+ { "source": "npm:pi-petrroll", "extensions": ["extensions/cd/index.ts"] }
46
+ ]
47
+ }
48
+ ```
49
+
50
+ Disabling `cd` disables both `/cd` and its `change_directory` tool. Disabling `cost` disables `add_cost`. To load only cost recording, use `"extensions": ["extensions/cost/index.ts"]` in the package entry above.
34
51
 
35
52
  ## Change directory
36
53
 
@@ -84,6 +101,27 @@ Manual `/cd` switches without starting another model response; the agent tool re
84
101
  - The copied history can contain references to the old project. A visible context message records the change, and the new system prompt contains the destination's instructions.
85
102
  - Switching reloads project extensions according to Pi's trust policy. Install only code you trust.
86
103
 
104
+ ## External costs
105
+
106
+ After launching a subagent with the ordinary `bash` tool, Pi can call `add_cost` to include the subagent's reported cost in the **current session's native cost total**:
107
+
108
+ ```json
109
+ {
110
+ "amount": 0.0375,
111
+ "reason": "Bash-launched review subagent, run review-123"
112
+ }
113
+ ```
114
+
115
+ - `amount` is an additional, finite, non-negative cost in **USD**, not cents. Zero is allowed.
116
+ - `reason` is a required, non-blank description (up to 1000 characters), saved with the tool result for an audit trail.
117
+ - Costs appear in Pi's footer, `/session`, and RPC session statistics. Pi stores them as tool-result usage, so they survive reload/resume and compaction without inventing token counts or changing the parent model's usage.
118
+ - Works in interactive, print, JSON, and RPC modes, including in-memory sessions. `--no-session` costs exist only for that process.
119
+ - This is accounting only: the tool does not launch anything, inspect subagent output, estimate pricing, or charge money. The agent must obtain the cost from the external run first.
120
+
121
+ **Every successful call is additive; there is no deduplication.** Report each external cost once. For a continued subagent, report only the new, unrecorded cost—not its cumulative lifetime total. Do not report parent-session model costs or nested usage already returned by another tool. If the external cost is unknown, do not guess.
122
+
123
+ Pi's totals include all entries in the session file, including abandoned branches. Forked or copied sessions inherit costs present in their copied history; a new empty session starts at zero. No session files or Pi internals are patched.
124
+
87
125
  ## Development
88
126
 
89
127
  ```sh
@@ -92,15 +130,17 @@ npm run check
92
130
  npm test
93
131
  ```
94
132
 
95
- Tests cover path handling, copy/branch preservation, cancellation, aborts, concurrency, and a real Pi runtime handoff with a fake model stream. The runtime test verifies that tool results are copied, `read` and `bash` use the new cwd, destination `AGENTS.md` replaces source context, and the agent continues. Tests do not call a model API or use your credentials.
133
+ Tests cover cost validation, native cost accumulation, persistence/resume, compaction, path handling, copy/branch preservation, cancellation, aborts, concurrency, and a real Pi runtime handoff with a fake model stream. The runtime test verifies that tool results are copied, `read` and `bash` use the new cwd, destination `AGENTS.md` replaces source context, and the agent continues. Tests do not call a model API or use your credentials.
96
134
 
97
135
  ```text
98
136
  extensions/
99
137
  cd/
100
138
  index.ts
101
- # Add another-extension/index.ts here.
139
+ cost/
140
+ index.ts
102
141
  tests/
103
142
  cd.test.ts
143
+ cost.test.ts
104
144
  runtime.test.ts
105
145
  ```
106
146
 
@@ -0,0 +1,52 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { Type, type Static } from "typebox";
3
+
4
+ export const addCostParameters = Type.Object({
5
+ amount: Type.Number({ minimum: 0, description: "Additional cost in USD (not cents or a cumulative session total)." }),
6
+ reason: Type.String({ minLength: 1, maxLength: 1000, description: "What incurred this cost, e.g. the bash-launched subagent task or run ID." }),
7
+ });
8
+
9
+ export type AddCostInput = Static<typeof addCostParameters>;
10
+
11
+ export default function (pi: ExtensionAPI) {
12
+ pi.registerTool({
13
+ name: "add_cost",
14
+ label: "Add Cost",
15
+ description:
16
+ "Add an external cost in USD to the current Pi session's native cost total. " +
17
+ "Use after external work such as a subagent launched via bash reports its cost. " +
18
+ "Every successful call adds the amount again; report only the unrecorded cost, never a cumulative total or costs Pi already accounts for. " +
19
+ "This records accounting only: it does not run a subagent or charge money.",
20
+ promptSnippet: "Record external costs in the current session total",
21
+ promptGuidelines: [
22
+ "Use add_cost after bash-launched subagents or other external work report a cost in USD. Do not guess costs or double-count costs already recorded by Pi or a previous add_cost call.",
23
+ ],
24
+ parameters: addCostParameters,
25
+ async execute(_toolCallId, { amount, reason }, signal) {
26
+ signal?.throwIfAborted();
27
+ // Validate here too: tool_call hooks can mutate arguments after schema validation.
28
+ if (typeof amount !== "number" || !Number.isFinite(amount) || amount < 0) {
29
+ throw new Error("amount must be a finite, non-negative cost in USD.");
30
+ }
31
+ if (typeof reason !== "string" || !reason.trim() || reason.length > 1000) {
32
+ throw new Error("reason must be non-blank and at most 1000 characters.");
33
+ }
34
+ const description = reason.trim();
35
+ return {
36
+ content: [{ type: "text", text: `Added $${amount} USD to this session's cost: ${description}` }],
37
+ details: { amount, currency: "USD", reason: description },
38
+ // Pi persists tool-result usage and includes it in footer, /session, and
39
+ // RPC totals, including after compaction. No fabricated token counts or
40
+ // attribution to the parent model's input/output pricing categories.
41
+ usage: {
42
+ input: 0,
43
+ output: 0,
44
+ cacheRead: 0,
45
+ cacheWrite: 0,
46
+ totalTokens: 0,
47
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: amount },
48
+ },
49
+ };
50
+ },
51
+ });
52
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pi-petrroll",
3
- "version": "0.1.0",
4
- "description": "Small, focused Pi extensions by petrroll: change projects without leaving your session.",
3
+ "version": "0.2.0",
4
+ "description": "Small, focused Pi extensions by petrroll: switch projects and track external costs in your session.",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": "Petr Houška (petrroll)",