pi-agent-python-sdk 0.2.0__tar.gz → 0.2.2__tar.gz

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.
Files changed (20) hide show
  1. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/CHANGELOG.md +32 -0
  2. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/PKG-INFO +177 -77
  3. pi_agent_python_sdk-0.2.2/README.md +326 -0
  4. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/pyproject.toml +3 -3
  5. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_events.py +4 -2
  6. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_launch.py +1 -1
  7. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_observation.py +28 -6
  8. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_runs.py +4 -4
  9. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_transport.py +11 -2
  10. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/client.py +27 -8
  11. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/sync.py +10 -0
  12. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/types.py +41 -3
  13. pi_agent_python_sdk-0.2.0/README.md +0 -226
  14. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/.gitignore +0 -0
  15. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/LICENSE +0 -0
  16. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/__init__.py +0 -0
  17. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_collections.py +0 -0
  18. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/_usage.py +0 -0
  19. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/errors.py +0 -0
  20. {pi_agent_python_sdk-0.2.0 → pi_agent_python_sdk-0.2.2}/src/pi_agent/py.typed +0 -0
@@ -2,6 +2,38 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.2.2
6
+
7
+ ### Changed
8
+
9
+ - Update the tested Pi runtime to 0.86.1, retaining 0.85.1 as the minimum.
10
+
11
+ ## 0.2.1
12
+
13
+ ### Added
14
+
15
+ - Stop process observations without closing Pi, then drain buffered records;
16
+ report scoped stop, discard, overflow, and process termination separately.
17
+
18
+ ### Fixed
19
+
20
+ - Drain the ready queue in each blocking iterator transfer to avoid finite-burst
21
+ overflow caused by repeated partial transfers. Queue limits remain unchanged;
22
+ the additional batch is bounded by the same count and byte budgets.
23
+ - Allow sequential async stream iterator reuse while retaining concurrent-reader
24
+ and result-drain protections.
25
+ - Keep a healthy client usable after locally rejected, unsent input, without
26
+ relaxing ownership or cleanup for commands that may have reached Pi.
27
+
28
+ ### Changed
29
+
30
+ - Update the tested Pi runtime to 0.86.0, retaining 0.85.1 as the minimum.
31
+ - Clarify Python interface choice, async event consumption, and structured-output
32
+ boundaries; demonstrate thinking, tool events, and unknown metadata.
33
+ - Describe system transcript messages, session usage entries, compaction system
34
+ snapshots, and model prompt-cache metadata in the wire annotations. Preserve
35
+ legacy optional fields and raw metadata; Pi still owns transcript and cache behavior.
36
+
5
37
  ## 0.2.0
6
38
 
7
39
  ### Breaking changes
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pi-agent-python-sdk
3
- Version: 0.2.0
3
+ Version: 0.2.2
4
4
  Summary: Typed async and synchronous Python clients for Pi's agent RPC runtime
5
5
  Project-URL: Homepage, https://github.com/cheenulabs/pi-agent-python-sdk
6
- Project-URL: Documentation, https://github.com/cheenulabs/pi-agent-python-sdk/tree/v0.2.0/docs
6
+ Project-URL: Documentation, https://github.com/cheenulabs/pi-agent-python-sdk/tree/v0.2.2/docs
7
7
  Project-URL: Issues, https://github.com/cheenulabs/pi-agent-python-sdk/issues
8
- Project-URL: Changelog, https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/CHANGELOG.md
8
+ Project-URL: Changelog, https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/CHANGELOG.md
9
9
  Author: cheenulabs
10
10
  License-Expression: MIT
11
11
  License-File: LICENSE
@@ -23,40 +23,69 @@ Description-Content-Type: text/markdown
23
23
 
24
24
  # Pi Agent Python SDK
25
25
 
26
- [![PyPI](https://img.shields.io/pypi/v/pi-agent-python-sdk?style=flat-square)][pypi]
26
+ [![PyPI](https://img.shields.io/pypi/v/pi-agent-python-sdk.svg?style=flat-square)][pypi]
27
27
  [![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-3776AB?style=flat-square)][metadata]
28
28
  [![MIT license](https://img.shields.io/badge/license-MIT-blue?style=flat-square)][license]
29
29
 
30
- **Run Pi from Python. Stream responses, continue conversations, and control sessions.**
30
+ **Use your installed Pi coding agent as a Python API. Stream responses,
31
+ continue conversations, and control sessions from Python.**
31
32
 
32
- A community Python SDK for [Pi coding agent](https://github.com/earendil-works/pi).
33
- Use synchronous or asynchronous clients with typed access to Pi's RPC commands
34
- and no third-party Python runtime dependencies. Pi runs as a subprocess and keeps
35
- its usual models, authentication, tools, extensions, skills, and configuration.
33
+ [Pi](https://github.com/earendil-works/pi) is an open-source coding agent that
34
+ runs in your terminal. This unofficial community SDK launches the Pi CLI in RPC
35
+ mode and exposes prompts, streaming events, and sessions through typed
36
+ synchronous and asynchronous Python APIs.
36
37
 
37
- > Requires Python **3.11+** and a separate Pi installation. The tested protocol
38
- > baseline is **Pi 0.85.1**; see [compatibility][compatibility] for version and
39
- > platform scope.
38
+ ```python
39
+ from pi_agent import PiClient
40
+
41
+ with PiClient() as pi:
42
+ print(pi.run("Explain the current project without changing files.").text)
43
+ ```
44
+
45
+ Requires an installed, configured Pi CLI — see [Prerequisites](#prerequisites).
46
+
47
+ [Prerequisites](#prerequisites) · [Quick start](#quick-start) ·
48
+ [Streaming](#streaming) · [Async](#async-usage) ·
49
+ [Configuration](#configuration) · [Thinking](#models-and-thinking) ·
50
+ [RPC](#rpc-access) · [Documentation](#documentation) ·
51
+ [Troubleshooting](#troubleshooting) · [Support](#support)
52
+
53
+ ## What you can do
40
54
 
41
- [Quick start](#quick-start) · [Streaming](#streaming) · [Async](#async-usage) ·
42
- [Configuration](#configuration) · [RPC](#rpc-access) · [Documentation](#documentation)
55
+ - **Run prompts from Python** — get a final answer, or stream text, thinking, and
56
+ tool events as they happen.
57
+ - **Keep conversations going** — send follow-ups on the same client, or resume,
58
+ fork, and clone sessions.
59
+ - **Control the agent** — switch models, set thinking level, steer mid-run, and
60
+ compact context.
61
+ - **Reuse your Pi setup** — same configuration, tools, and extensions as the CLI,
62
+ with sync or async clients.
63
+
64
+ ## Prerequisites
65
+
66
+ | Requirement | Notes |
67
+ | --- | --- |
68
+ | Python **3.11+** | Package metadata requires 3.11 or newer |
69
+ | Node.js **22.19.0+** | Required by Pi 0.86.1 |
70
+ | Pi **0.85.1+** | Minimum accepted version; **0.86.1** is the tested baseline |
71
+ | Model provider | Required; configure with the Pi CLI (see Quick start) |
43
72
 
44
73
  ## Quick start
45
74
 
46
- Install Pi with Node.js **22.19.0 or newer**:
75
+ Install Pi with Node.js **22.19.0 or newer** (pin matches the tested baseline):
47
76
 
48
77
  ```sh
49
- npm install -g @earendil-works/pi-coding-agent@0.85.1
78
+ npm install -g @earendil-works/pi-coding-agent@0.86.1
50
79
  pi --version
51
80
  ```
52
81
 
53
- Run `pi` once to configure your provider and model using Pi's normal setup.
54
- The SDK uses that configuration when it starts Pi.
82
+ Run `pi` once to configure your provider and model. The SDK uses that
83
+ configuration when it starts Pi.
55
84
 
56
85
  Install the Python package from [PyPI][pypi]:
57
86
 
58
87
  ```sh
59
- python -m pip install pi-agent-python-sdk==0.2.0
88
+ python -m pip install pi-agent-python-sdk
60
89
  ```
61
90
 
62
91
  The distribution is named `pi-agent-python-sdk`; import it as
@@ -71,8 +100,8 @@ with PiClient() as pi:
71
100
  ```
72
101
 
73
102
  The context manager starts and closes Pi. `run()` waits for the conversation to
74
- settle, including retries and queued follow-ups. The result includes finalized
75
- messages, session identity, elapsed time, and observed assistant usage.
103
+ settle. The result includes finalized messages, session identity, elapsed time,
104
+ and observed assistant usage.
76
105
 
77
106
  Continue the conversation with another call on the same client:
78
107
 
@@ -87,15 +116,6 @@ with PiClient() as pi:
87
116
  Pi keeps the conversation context. Each result contains only the messages and
88
117
  answer from that call; a call with no assistant output has empty text.
89
118
 
90
- ## What you can do
91
-
92
- - **Run and stream:** get a final answer or consume text, thinking, and tool events.
93
- - **Keep a conversation:** send follow-up prompts, resume sessions, fork, or clone.
94
- - **Control Pi:** select models, adjust thinking, steer work, compact context, and
95
- call all 33 RPC commands in the pinned baseline.
96
- - **Integrate with your application:** use sync or async clients, typed results,
97
- raw event dictionaries, and extension UI callbacks.
98
-
99
119
  ## Streaming
100
120
 
101
121
  ```python
@@ -110,9 +130,15 @@ with PiClient() as pi:
110
130
  print(f"\nStop reason: {result.stop_reason}")
111
131
  ```
112
132
 
133
+ <details>
134
+ <summary>Streaming details</summary>
135
+
113
136
  `event.raw` contains the full Pi event, including fields the SDK does not yet
114
- recognize. Keep the stream inside its context: leaving early cleans up unfinished work. See [errors and cancellation][errors] for
115
- handling timeouts and partial results.
137
+ recognize. Keep the stream inside its context: leaving early cleans up unfinished
138
+ work. See [errors and cancellation][errors] for handling timeouts and partial
139
+ results.
140
+
141
+ </details>
116
142
 
117
143
  ## Async usage
118
144
 
@@ -135,8 +161,13 @@ if __name__ == "__main__":
135
161
  asyncio.run(main())
136
162
  ```
137
163
 
138
- Async streaming uses `async with pi.stream(...)`, `async for event in stream`,
139
- and `await stream.result()`. See the [complete streaming example][stream-example].
164
+ <details>
165
+ <summary>Async streaming</summary>
166
+
167
+ Use `async with pi.stream(...)`, `async for event in stream`, and
168
+ `await stream.result()`. See the [complete streaming example][stream-example].
169
+
170
+ </details>
140
171
 
141
172
  ## Configuration
142
173
 
@@ -150,14 +181,70 @@ with PiClient(cwd=".", no_session=True) as pi:
150
181
  ```
151
182
 
152
183
  Leave `no_session` unset to preserve Pi's normal session persistence. Use
153
- `provider=` and `model=` to override Pi's configured model, or `session=` to open
154
- an existing session. See [constructor options][constructor-options]
155
- for environment overrides, executable paths, and deadlines.
184
+ `session=` to open an existing session. For model overrides, see
185
+ [Models and thinking](#models-and-thinking).
186
+
187
+ <details>
188
+ <summary>More configuration</summary>
189
+
190
+ See [constructor options][constructor-options] for environment overrides,
191
+ executable paths, and deadlines.
156
192
 
157
193
  Extensions are installed and configured through Pi. To load your own extension,
158
- pass `extra_args=["--extension", "/absolute/path/to/your-extension.ts"]` to either
159
- client. See [using your own extensions][extensions]
160
- for details and [extension UI][ui-example] for an interactive example.
194
+ pass `extra_args=["--extension", "/absolute/path/to/your-extension.ts"]` to
195
+ either client. See [using your own extensions][extensions] for details and
196
+ [extension UI][ui-example] for an interactive example.
197
+
198
+ </details>
199
+
200
+ ## Models and thinking
201
+
202
+ By default the SDK uses whatever model you already configured in the Pi CLI.
203
+ Override it when you construct the client, or switch later on the same client.
204
+ Set the model and thinking level **before** `run()` or `stream()`.
205
+
206
+ Select a model at construction:
207
+
208
+ ```python
209
+ from pi_agent import PiClient
210
+
211
+ with PiClient(provider="openai", model="gpt-5") as pi:
212
+ print(pi.run("Summarise this repo without changing files.").text)
213
+ ```
214
+
215
+ Thinking levels depend on the current model. Query them, set one, then confirm
216
+ what Pi actually applied:
217
+
218
+ ```python
219
+ from pi_agent import PiClient
220
+
221
+ with PiClient() as pi:
222
+ levels = pi.get_available_thinking_levels()
223
+ print("available:", levels)
224
+ if "high" in levels:
225
+ pi.set_thinking_level("high")
226
+ print("effective:", pi.get_state()["thinkingLevel"])
227
+ print(pi.run("Analyse this project's architecture without changing files.").text)
228
+ ```
229
+
230
+ Replace the example provider/model ids with ones from your Pi install
231
+ (`pi --list-models` or `get_available_models()`).
232
+
233
+ <details>
234
+ <summary>List models and switch after start</summary>
235
+
236
+ ```python
237
+ from pi_agent import PiClient
238
+
239
+ with PiClient() as pi:
240
+ for model in pi.get_available_models():
241
+ print(model["provider"], model["id"])
242
+ selected = pi.set_model("openai", "gpt-5")
243
+ print("using", selected["provider"], selected["id"])
244
+ print(pi.run("Summarise this repo without changing files.").text)
245
+ ```
246
+
247
+ </details>
161
248
 
162
249
  ## RPC access
163
250
 
@@ -170,7 +257,18 @@ Your Python application
170
257
  Models · tools · extensions · sessions
171
258
  ```
172
259
 
173
- Choose the interface that fits the work:
260
+ Inspect session state without starting a model run:
261
+
262
+ ```python
263
+ from pi_agent import PiClient
264
+
265
+ with PiClient() as pi:
266
+ state = pi.get_state()
267
+ print(state["sessionId"])
268
+ ```
269
+
270
+ <details>
271
+ <summary>Which interface to use</summary>
174
272
 
175
273
  | You need | Use |
176
274
  | --- | --- |
@@ -182,29 +280,20 @@ Choose the interface that fits the work:
182
280
  | A specific Pi operation | `get_state()`, `set_model()`, `fork()`, and other command methods |
183
281
  | A raw command response envelope | `request()` |
184
282
 
185
- For example, inspect session state without starting a model run:
186
-
187
- ```python
188
- from pi_agent import PiClient
189
-
190
- with PiClient() as pi:
191
- state = pi.get_state()
192
- print(state["sessionId"])
193
- ```
194
-
195
283
  Python arguments use `snake_case`; wire dictionaries retain Pi's `camelCase`
196
284
  fields. `prompt()` acknowledges submission, which may be handled entirely by an
197
285
  extension. It does not wait for a completed answer.
198
286
 
199
- Use one client per conversation and wait for each `run()` or stream to finish
200
- before starting the next. After using `prompt()` for your own event handling,
201
- use a fresh client for `run()` or `stream()` so results cannot include delayed
202
- events from earlier work. The SDK launches a new process and cannot attach to an existing
203
- Pi terminal session.
287
+ Each client starts its own Pi subprocess. Use one client per conversation and
288
+ wait for each `run()` or stream to finish before starting the next. After using
289
+ `prompt()` for your own event handling, use a fresh client for `run()` or
290
+ `stream()` so results cannot include delayed events from earlier work.
204
291
 
205
292
  See [RPC structure][rpc] for the protocol mapping and module layout, and
206
293
  the [command reference][commands] for every method.
207
294
 
295
+ </details>
296
+
208
297
  ## Documentation
209
298
 
210
299
  | Guide | Contents |
@@ -215,35 +304,46 @@ the [command reference][commands] for every method.
215
304
  | [RPC structure][rpc] | How the Python client maps to Pi's protocol |
216
305
  | [Compatibility][compatibility] | Runtime versions and platform validation |
217
306
  | [Examples][examples] | Runnable sync, async, streaming, sessions, steering, and UI examples |
218
- | [Release notes][release] | Changes included in 0.2.0 |
307
+ | [Release notes][release] | Changes included in 0.2.2 |
219
308
 
220
309
  Examples use your configured Pi and may make provider calls. Development checks
221
310
  use an isolated local test provider; see [CONTRIBUTING.md][contributing] for
222
311
  setup and validation commands.
223
312
 
224
- Maintainers: [protocol discovery][discovery] ·
225
- [maintenance][maintenance] · [release checklist][releasing].
313
+ ## Troubleshooting
314
+
315
+ | Symptom | What to check |
316
+ | --- | --- |
317
+ | `pi` not found / launch fails | Ensure the Pi CLI is on `PATH`, or pass `executable=` to the client |
318
+ | Version rejected | Upgrade to Pi **0.85.1+**; prefer the tested baseline **0.86.1** |
319
+ | No model / auth errors | Run `pi` once and complete provider setup |
320
+ | Unexpected file or command changes | Review Pi tool/extension settings before automation |
321
+
322
+ ## Support
323
+
324
+ - [Open an issue][issues] for bugs, questions, or compatibility reports.
325
+ - See [CONTRIBUTING.md][contributing] for local setup and pull requests.
326
+ - For security reports, open a [private security advisory][security].
226
327
 
227
328
  ## License
228
329
 
229
330
  [MIT][license]
230
331
 
231
332
  [pypi]: https://pypi.org/project/pi-agent-python-sdk/
232
- [release]: https://github.com/cheenulabs/pi-agent-python-sdk/releases/tag/v0.2.0
233
- [metadata]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/pyproject.toml
234
- [license]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/LICENSE
235
- [compatibility]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/compatibility.md
236
- [errors]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/errors.md
237
- [stream-example]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/examples/stream.py
238
- [constructor-options]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/api.md#constructor-options
239
- [extensions]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/usage.md#using-your-own-extensions
240
- [ui-example]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/examples/ui.py
241
- [rpc]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/rpc.md
242
- [commands]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/api.md#all-33-rpc-commands
243
- [usage]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/usage.md
244
- [api]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/api.md
245
- [examples]: https://github.com/cheenulabs/pi-agent-python-sdk/tree/v0.2.0/examples
246
- [contributing]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/CONTRIBUTING.md
247
- [discovery]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/discovery.md
248
- [maintenance]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/maintenance.md
249
- [releasing]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.0/docs/releasing.md
333
+ [release]: https://github.com/cheenulabs/pi-agent-python-sdk/releases/tag/v0.2.2
334
+ [metadata]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/pyproject.toml
335
+ [license]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/LICENSE
336
+ [compatibility]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/compatibility.md
337
+ [errors]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/errors.md
338
+ [stream-example]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/examples/stream.py
339
+ [constructor-options]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/api.md#constructor-options
340
+ [extensions]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/usage.md#using-your-own-extensions
341
+ [ui-example]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/examples/ui.py
342
+ [rpc]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/rpc.md
343
+ [commands]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/api.md#all-33-rpc-commands
344
+ [usage]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/usage.md
345
+ [api]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/docs/api.md
346
+ [examples]: https://github.com/cheenulabs/pi-agent-python-sdk/tree/v0.2.2/examples
347
+ [contributing]: https://github.com/cheenulabs/pi-agent-python-sdk/blob/v0.2.2/CONTRIBUTING.md
348
+ [issues]: https://github.com/cheenulabs/pi-agent-python-sdk/issues
349
+ [security]: https://github.com/cheenulabs/pi-agent-python-sdk/security/advisories/new