@mariozechner/pi-coding-agent 0.49.3 → 0.50.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/CHANGELOG.md +110 -1
- package/README.md +310 -1230
- package/dist/cli/args.d.ts +5 -0
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +57 -23
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/config-selector.d.ts +14 -0
- package/dist/cli/config-selector.d.ts.map +1 -0
- package/dist/cli/config-selector.js +31 -0
- package/dist/cli/config-selector.js.map +1 -0
- package/dist/cli/session-picker.d.ts.map +1 -1
- package/dist/cli/session-picker.js +1 -1
- package/dist/cli/session-picker.js.map +1 -1
- package/dist/core/agent-session.d.ts +60 -37
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +272 -69
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +8 -18
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +39 -55
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +2 -1
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/diagnostics.d.ts +15 -0
- package/dist/core/diagnostics.d.ts.map +1 -0
- package/dist/core/diagnostics.js +2 -0
- package/dist/core/diagnostics.js.map +1 -0
- package/dist/core/export-html/template.css +9 -0
- package/dist/core/export-html/template.js +6 -4
- package/dist/core/extensions/index.d.ts +1 -1
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +10 -1
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +9 -3
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +39 -12
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +112 -1
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts +9 -2
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +13 -0
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/model-registry.d.ts +42 -2
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +154 -44
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +3 -2
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/package-manager.d.ts +130 -0
- package/dist/core/package-manager.d.ts.map +1 -0
- package/dist/core/package-manager.js +1177 -0
- package/dist/core/package-manager.js.map +1 -0
- package/dist/core/prompt-templates.d.ts +6 -0
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +114 -54
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts +160 -0
- package/dist/core/resource-loader.d.ts.map +1 -0
- package/dist/core/resource-loader.js +604 -0
- package/dist/core/resource-loader.js.map +1 -0
- package/dist/core/sdk.d.ts +14 -105
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +52 -304
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +45 -1
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/settings-manager.d.ts +34 -16
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +104 -25
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/skills.d.ts +18 -10
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +126 -93
- package/dist/core/skills.js.map +1 -1
- package/dist/core/system-prompt.d.ts +3 -27
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +16 -103
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +2 -1
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +4 -4
- package/dist/core/tools/read.js.map +1 -1
- package/dist/index.d.ts +12 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -6
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +209 -97
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts +5 -1
- package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
- package/dist/modes/interactive/components/bordered-loader.js +29 -9
- package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
- package/dist/modes/interactive/components/config-selector.d.ts +71 -0
- package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
- package/dist/modes/interactive/components/config-selector.js +468 -0
- package/dist/modes/interactive/components/config-selector.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +4 -0
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +3 -4
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/components/session-selector.d.ts +18 -1
- package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/session-selector.js +195 -87
- package/dist/modes/interactive/components/session-selector.js.map +1 -1
- package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
- package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
- package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +5 -5
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +42 -2
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +538 -204
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/dark.json +1 -1
- package/dist/modes/interactive/theme/light.json +1 -1
- package/dist/modes/interactive/theme/theme-schema.json +8 -1
- package/dist/modes/interactive/theme/theme.d.ts +8 -1
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +72 -25
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +7 -74
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +17 -82
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/utils/git.d.ts +2 -0
- package/dist/utils/git.d.ts.map +1 -0
- package/dist/utils/git.js +6 -0
- package/dist/utils/git.js.map +1 -0
- package/dist/utils/shell.d.ts +1 -0
- package/dist/utils/shell.d.ts.map +1 -1
- package/dist/utils/shell.js +14 -1
- package/dist/utils/shell.js.map +1 -1
- package/dist/utils/sleep.d.ts +5 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +17 -0
- package/dist/utils/sleep.js.map +1 -0
- package/docs/compaction.md +23 -21
- package/docs/custom-provider.md +538 -0
- package/docs/development.md +69 -0
- package/docs/extensions.md +182 -118
- package/docs/images/doom-extension.png +0 -0
- package/docs/images/interactive-mode.png +0 -0
- package/docs/images/tree-view.png +0 -0
- package/docs/json.md +79 -0
- package/docs/keybindings.md +162 -0
- package/docs/models.md +193 -0
- package/docs/packages.md +168 -0
- package/docs/prompt-templates.md +67 -0
- package/docs/providers.md +147 -0
- package/docs/sdk.md +111 -178
- package/docs/session.md +167 -16
- package/docs/settings.md +216 -0
- package/docs/shell-aliases.md +13 -0
- package/docs/skills.md +111 -202
- package/docs/terminal-setup.md +65 -0
- package/docs/themes.md +295 -0
- package/docs/tui.md +36 -5
- package/docs/windows.md +17 -0
- package/examples/README.md +1 -0
- package/examples/extensions/README.md +22 -2
- package/examples/extensions/bookmark.ts +50 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
- package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
- package/examples/extensions/custom-provider-anthropic/package.json +19 -0
- package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
- package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
- package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
- package/examples/extensions/doom-overlay/doom/build.sh +1 -1
- package/examples/extensions/event-bus.ts +43 -0
- package/examples/extensions/message-renderer.ts +59 -0
- package/examples/extensions/session-name.ts +27 -0
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/02-custom-model.ts +3 -3
- package/examples/sdk/03-custom-prompt.ts +20 -9
- package/examples/sdk/04-skills.ts +26 -27
- package/examples/sdk/06-extensions.ts +15 -6
- package/examples/sdk/07-context-files.ts +22 -18
- package/examples/sdk/08-prompt-templates.ts +19 -14
- package/examples/sdk/09-api-keys-and-oauth.ts +5 -12
- package/examples/sdk/10-settings.ts +3 -3
- package/examples/sdk/12-full-control.ts +16 -7
- package/examples/sdk/README.md +24 -30
- package/package.json +4 -4
- package/docs/theme.md +0 -617
- package/examples/extensions/chalk-logger.ts +0 -26
package/docs/compaction.md
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
LLMs have limited context windows. When conversations grow too long, pi uses compaction to summarize older content while preserving recent work. This page covers both auto-compaction and branch summarization.
|
|
4
4
|
|
|
5
|
-
**Source files
|
|
6
|
-
- [`src/core/compaction/compaction.ts`](
|
|
7
|
-
- [`src/core/compaction/branch-summarization.ts`](
|
|
8
|
-
- [`src/core/compaction/utils.ts`](
|
|
9
|
-
- [`src/core/session-manager.ts`](
|
|
10
|
-
- [`src/core/
|
|
5
|
+
**Source files** ([pi-mono](https://github.com/badlogic/pi-mono)):
|
|
6
|
+
- [`packages/coding-agent/src/core/compaction/compaction.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/compaction.ts) - Auto-compaction logic
|
|
7
|
+
- [`packages/coding-agent/src/core/compaction/branch-summarization.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/branch-summarization.ts) - Branch summarization
|
|
8
|
+
- [`packages/coding-agent/src/core/compaction/utils.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/utils.ts) - Shared utilities (file tracking, serialization)
|
|
9
|
+
- [`packages/coding-agent/src/core/session-manager.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/session-manager.ts) - Entry types (`CompactionEntry`, `BranchSummaryEntry`)
|
|
10
|
+
- [`packages/coding-agent/src/core/extensions/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/extensions/types.ts) - Extension event types
|
|
11
|
+
|
|
12
|
+
For TypeScript definitions in your project, inspect `node_modules/@mariozechner/pi-coding-agent/dist/`.
|
|
11
13
|
|
|
12
14
|
## Overview
|
|
13
15
|
|
|
@@ -108,13 +110,13 @@ Valid cut points are:
|
|
|
108
110
|
- User messages
|
|
109
111
|
- Assistant messages
|
|
110
112
|
- BashExecution messages
|
|
111
|
-
-
|
|
113
|
+
- Custom messages (custom_message, branch_summary)
|
|
112
114
|
|
|
113
115
|
Never cut at tool results (they must stay with their tool call).
|
|
114
116
|
|
|
115
117
|
### CompactionEntry Structure
|
|
116
118
|
|
|
117
|
-
Defined in [`
|
|
119
|
+
Defined in [`session-manager.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/session-manager.ts):
|
|
118
120
|
|
|
119
121
|
```typescript
|
|
120
122
|
interface CompactionEntry<T = unknown> {
|
|
@@ -125,8 +127,8 @@ interface CompactionEntry<T = unknown> {
|
|
|
125
127
|
summary: string;
|
|
126
128
|
firstKeptEntryId: string;
|
|
127
129
|
tokensBefore: number;
|
|
128
|
-
fromHook?: boolean; // true if
|
|
129
|
-
details?: T; //
|
|
130
|
+
fromHook?: boolean; // true if provided by extension (legacy field name)
|
|
131
|
+
details?: T; // implementation-specific data
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
// Default compaction uses this for details (from compaction.ts):
|
|
@@ -136,9 +138,9 @@ interface CompactionDetails {
|
|
|
136
138
|
}
|
|
137
139
|
```
|
|
138
140
|
|
|
139
|
-
|
|
141
|
+
Extensions can store any JSON-serializable data in `details`. The default compaction tracks file operations, but custom extension implementations can use their own structure.
|
|
140
142
|
|
|
141
|
-
See [`prepareCompaction()`](
|
|
143
|
+
See [`prepareCompaction()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/compaction.ts) and [`compact()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/compaction.ts) for the implementation.
|
|
142
144
|
|
|
143
145
|
## Branch Summarization
|
|
144
146
|
|
|
@@ -181,7 +183,7 @@ This means file tracking accumulates across multiple compactions or nested branc
|
|
|
181
183
|
|
|
182
184
|
### BranchSummaryEntry Structure
|
|
183
185
|
|
|
184
|
-
Defined in [`
|
|
186
|
+
Defined in [`session-manager.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/session-manager.ts):
|
|
185
187
|
|
|
186
188
|
```typescript
|
|
187
189
|
interface BranchSummaryEntry<T = unknown> {
|
|
@@ -191,8 +193,8 @@ interface BranchSummaryEntry<T = unknown> {
|
|
|
191
193
|
timestamp: number;
|
|
192
194
|
summary: string;
|
|
193
195
|
fromId: string; // Entry we navigated from
|
|
194
|
-
fromHook?: boolean; // true if
|
|
195
|
-
details?: T; //
|
|
196
|
+
fromHook?: boolean; // true if provided by extension (legacy field name)
|
|
197
|
+
details?: T; // implementation-specific data
|
|
196
198
|
}
|
|
197
199
|
|
|
198
200
|
// Default branch summarization uses this for details (from branch-summarization.ts):
|
|
@@ -202,9 +204,9 @@ interface BranchSummaryDetails {
|
|
|
202
204
|
}
|
|
203
205
|
```
|
|
204
206
|
|
|
205
|
-
Same as compaction,
|
|
207
|
+
Same as compaction, extensions can store custom data in `details`.
|
|
206
208
|
|
|
207
|
-
See [`collectEntriesForBranchSummary()`](
|
|
209
|
+
See [`collectEntriesForBranchSummary()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/branch-summarization.ts), [`prepareBranchEntries()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/branch-summarization.ts), and [`generateBranchSummary()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/branch-summarization.ts) for the implementation.
|
|
208
210
|
|
|
209
211
|
## Summary Format
|
|
210
212
|
|
|
@@ -248,7 +250,7 @@ path/to/changed.ts
|
|
|
248
250
|
|
|
249
251
|
### Message Serialization
|
|
250
252
|
|
|
251
|
-
Before summarization, messages are serialized to text via [`serializeConversation()`](
|
|
253
|
+
Before summarization, messages are serialized to text via [`serializeConversation()`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/compaction/utils.ts):
|
|
252
254
|
|
|
253
255
|
```
|
|
254
256
|
[User]: What they said
|
|
@@ -260,9 +262,9 @@ Before summarization, messages are serialized to text via [`serializeConversatio
|
|
|
260
262
|
|
|
261
263
|
This prevents the model from treating it as a conversation to continue.
|
|
262
264
|
|
|
263
|
-
## Custom Summarization via
|
|
265
|
+
## Custom Summarization via Extensions
|
|
264
266
|
|
|
265
|
-
|
|
267
|
+
Extensions can intercept and customize both compaction and branch summarization. See [`extensions/types.ts`](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/core/extensions/types.ts) for event type definitions.
|
|
266
268
|
|
|
267
269
|
### session_before_compact
|
|
268
270
|
|
|
@@ -332,7 +334,7 @@ pi.on("session_before_compact", async (event, ctx) => {
|
|
|
332
334
|
});
|
|
333
335
|
```
|
|
334
336
|
|
|
335
|
-
See [
|
|
337
|
+
See [custom-compaction.ts](../examples/extensions/custom-compaction.ts) for a complete example using a different model.
|
|
336
338
|
|
|
337
339
|
### session_before_tree
|
|
338
340
|
|
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
# Custom Providers
|
|
2
|
+
|
|
3
|
+
Extensions can register custom model providers via `pi.registerProvider()`. This enables:
|
|
4
|
+
|
|
5
|
+
- **Proxies** - Route requests through corporate proxies or API gateways
|
|
6
|
+
- **Custom endpoints** - Use self-hosted or private model deployments
|
|
7
|
+
- **OAuth/SSO** - Add authentication flows for enterprise providers
|
|
8
|
+
- **Custom APIs** - Implement streaming for non-standard LLM APIs
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Quick Reference](#quick-reference)
|
|
13
|
+
- [Override Existing Provider](#override-existing-provider)
|
|
14
|
+
- [Register New Provider](#register-new-provider)
|
|
15
|
+
- [OAuth Support](#oauth-support)
|
|
16
|
+
- [Custom Streaming API](#custom-streaming-api)
|
|
17
|
+
- [Testing Your Implementation](#testing-your-implementation)
|
|
18
|
+
- [Config Reference](#config-reference)
|
|
19
|
+
- [Model Definition Reference](#model-definition-reference)
|
|
20
|
+
|
|
21
|
+
## Quick Reference
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
25
|
+
|
|
26
|
+
export default function (pi: ExtensionAPI) {
|
|
27
|
+
// Override baseUrl for existing provider
|
|
28
|
+
pi.registerProvider("anthropic", {
|
|
29
|
+
baseUrl: "https://proxy.example.com"
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Register new provider with models
|
|
33
|
+
pi.registerProvider("my-provider", {
|
|
34
|
+
baseUrl: "https://api.example.com",
|
|
35
|
+
apiKey: "MY_API_KEY",
|
|
36
|
+
api: "openai-completions",
|
|
37
|
+
models: [
|
|
38
|
+
{
|
|
39
|
+
id: "my-model",
|
|
40
|
+
name: "My Model",
|
|
41
|
+
reasoning: false,
|
|
42
|
+
input: ["text", "image"],
|
|
43
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
44
|
+
contextWindow: 128000,
|
|
45
|
+
maxTokens: 4096
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Override Existing Provider
|
|
53
|
+
|
|
54
|
+
The simplest use case: redirect an existing provider through a proxy.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// All Anthropic requests now go through your proxy
|
|
58
|
+
pi.registerProvider("anthropic", {
|
|
59
|
+
baseUrl: "https://proxy.example.com"
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Add custom headers to OpenAI requests
|
|
63
|
+
pi.registerProvider("openai", {
|
|
64
|
+
headers: {
|
|
65
|
+
"X-Custom-Header": "value"
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Both baseUrl and headers
|
|
70
|
+
pi.registerProvider("google", {
|
|
71
|
+
baseUrl: "https://ai-gateway.corp.com/google",
|
|
72
|
+
headers: {
|
|
73
|
+
"X-Corp-Auth": "CORP_AUTH_TOKEN" // env var or literal
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
When only `baseUrl` and/or `headers` are provided (no `models`), all existing models for that provider are preserved with the new endpoint.
|
|
79
|
+
|
|
80
|
+
## Register New Provider
|
|
81
|
+
|
|
82
|
+
To add a completely new provider, specify `models` along with the required configuration.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
pi.registerProvider("my-llm", {
|
|
86
|
+
baseUrl: "https://api.my-llm.com/v1",
|
|
87
|
+
apiKey: "MY_LLM_API_KEY", // env var name or literal value
|
|
88
|
+
api: "openai-completions", // which streaming API to use
|
|
89
|
+
models: [
|
|
90
|
+
{
|
|
91
|
+
id: "my-llm-large",
|
|
92
|
+
name: "My LLM Large",
|
|
93
|
+
reasoning: true, // supports extended thinking
|
|
94
|
+
input: ["text", "image"],
|
|
95
|
+
cost: {
|
|
96
|
+
input: 3.0, // $/million tokens
|
|
97
|
+
output: 15.0,
|
|
98
|
+
cacheRead: 0.3,
|
|
99
|
+
cacheWrite: 3.75
|
|
100
|
+
},
|
|
101
|
+
contextWindow: 200000,
|
|
102
|
+
maxTokens: 16384
|
|
103
|
+
}
|
|
104
|
+
]
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
When `models` is provided, it **replaces** all existing models for that provider.
|
|
109
|
+
|
|
110
|
+
### API Types
|
|
111
|
+
|
|
112
|
+
The `api` field determines which streaming implementation is used:
|
|
113
|
+
|
|
114
|
+
| API | Use for |
|
|
115
|
+
|-----|---------|
|
|
116
|
+
| `anthropic-messages` | Anthropic Claude API and compatibles |
|
|
117
|
+
| `openai-completions` | OpenAI Chat Completions API and compatibles |
|
|
118
|
+
| `openai-responses` | OpenAI Responses API |
|
|
119
|
+
| `azure-openai-responses` | Azure OpenAI Responses API |
|
|
120
|
+
| `openai-codex-responses` | OpenAI Codex Responses API |
|
|
121
|
+
| `google-generative-ai` | Google Generative AI API |
|
|
122
|
+
| `google-gemini-cli` | Google Cloud Code Assist API |
|
|
123
|
+
| `google-vertex` | Google Vertex AI API |
|
|
124
|
+
| `bedrock-converse-stream` | Amazon Bedrock Converse API |
|
|
125
|
+
|
|
126
|
+
Most OpenAI-compatible providers work with `openai-completions`. Use `compat` for quirks:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
models: [{
|
|
130
|
+
id: "custom-model",
|
|
131
|
+
// ...
|
|
132
|
+
compat: {
|
|
133
|
+
supportsDeveloperRole: false, // use "system" instead of "developer"
|
|
134
|
+
supportsReasoningEffort: false, // disable reasoning_effort param
|
|
135
|
+
maxTokensField: "max_tokens", // instead of "max_completion_tokens"
|
|
136
|
+
requiresToolResultName: true, // tool results need name field
|
|
137
|
+
requiresMistralToolIds: true // tool IDs must be 9 alphanumeric chars
|
|
138
|
+
}
|
|
139
|
+
}]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Auth Header
|
|
143
|
+
|
|
144
|
+
If your provider expects `Authorization: Bearer <key>` but doesn't use a standard API, set `authHeader: true`:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
pi.registerProvider("custom-api", {
|
|
148
|
+
baseUrl: "https://api.example.com",
|
|
149
|
+
apiKey: "MY_API_KEY",
|
|
150
|
+
authHeader: true, // adds Authorization: Bearer header
|
|
151
|
+
api: "openai-completions",
|
|
152
|
+
models: [...]
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## OAuth Support
|
|
157
|
+
|
|
158
|
+
Add OAuth/SSO authentication that integrates with `/login`:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
import type { OAuthCredentials, OAuthLoginCallbacks } from "@mariozechner/pi-ai";
|
|
162
|
+
|
|
163
|
+
pi.registerProvider("corporate-ai", {
|
|
164
|
+
baseUrl: "https://ai.corp.com/v1",
|
|
165
|
+
api: "openai-responses",
|
|
166
|
+
models: [...],
|
|
167
|
+
oauth: {
|
|
168
|
+
name: "Corporate AI (SSO)",
|
|
169
|
+
|
|
170
|
+
async login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
171
|
+
// Option 1: Browser-based OAuth
|
|
172
|
+
callbacks.onAuth({ url: "https://sso.corp.com/authorize?..." });
|
|
173
|
+
|
|
174
|
+
// Option 2: Device code flow
|
|
175
|
+
callbacks.onDeviceCode({
|
|
176
|
+
userCode: "ABCD-1234",
|
|
177
|
+
verificationUri: "https://sso.corp.com/device"
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Option 3: Prompt for token/code
|
|
181
|
+
const code = await callbacks.onPrompt({ message: "Enter SSO code:" });
|
|
182
|
+
|
|
183
|
+
// Exchange for tokens (your implementation)
|
|
184
|
+
const tokens = await exchangeCodeForTokens(code);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
refresh: tokens.refreshToken,
|
|
188
|
+
access: tokens.accessToken,
|
|
189
|
+
expires: Date.now() + tokens.expiresIn * 1000
|
|
190
|
+
};
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
async refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
|
194
|
+
const tokens = await refreshAccessToken(credentials.refresh);
|
|
195
|
+
return {
|
|
196
|
+
refresh: tokens.refreshToken ?? credentials.refresh,
|
|
197
|
+
access: tokens.accessToken,
|
|
198
|
+
expires: Date.now() + tokens.expiresIn * 1000
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
|
|
202
|
+
getApiKey(credentials: OAuthCredentials): string {
|
|
203
|
+
return credentials.access;
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
// Optional: modify models based on user's subscription
|
|
207
|
+
modifyModels(models, credentials) {
|
|
208
|
+
const region = decodeRegionFromToken(credentials.access);
|
|
209
|
+
return models.map(m => ({
|
|
210
|
+
...m,
|
|
211
|
+
baseUrl: `https://${region}.ai.corp.com/v1`
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
After registration, users can authenticate via `/login corporate-ai`.
|
|
219
|
+
|
|
220
|
+
### OAuthLoginCallbacks
|
|
221
|
+
|
|
222
|
+
The `callbacks` object provides three ways to authenticate:
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
interface OAuthLoginCallbacks {
|
|
226
|
+
// Open URL in browser (for OAuth redirects)
|
|
227
|
+
onAuth(params: { url: string }): void;
|
|
228
|
+
|
|
229
|
+
// Show device code (for device authorization flow)
|
|
230
|
+
onDeviceCode(params: { userCode: string; verificationUri: string }): void;
|
|
231
|
+
|
|
232
|
+
// Prompt user for input (for manual token entry)
|
|
233
|
+
onPrompt(params: { message: string }): Promise<string>;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### OAuthCredentials
|
|
238
|
+
|
|
239
|
+
Credentials are persisted in `~/.pi/agent/auth.json`:
|
|
240
|
+
|
|
241
|
+
```typescript
|
|
242
|
+
interface OAuthCredentials {
|
|
243
|
+
refresh: string; // Refresh token (for refreshToken())
|
|
244
|
+
access: string; // Access token (returned by getApiKey())
|
|
245
|
+
expires: number; // Expiration timestamp in milliseconds
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Custom Streaming API
|
|
250
|
+
|
|
251
|
+
For providers with non-standard APIs, implement `streamSimple`. Study the existing provider implementations before writing your own:
|
|
252
|
+
|
|
253
|
+
**Reference implementations:**
|
|
254
|
+
- [anthropic.ts](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/providers/anthropic.ts) - Anthropic Messages API
|
|
255
|
+
- [openai-completions.ts](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/providers/openai-completions.ts) - OpenAI Chat Completions
|
|
256
|
+
- [openai-responses.ts](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/providers/openai-responses.ts) - OpenAI Responses API
|
|
257
|
+
- [google.ts](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/providers/google.ts) - Google Generative AI
|
|
258
|
+
- [amazon-bedrock.ts](https://github.com/badlogic/pi-mono/blob/main/packages/ai/src/providers/amazon-bedrock.ts) - AWS Bedrock
|
|
259
|
+
|
|
260
|
+
### Stream Pattern
|
|
261
|
+
|
|
262
|
+
All providers follow the same pattern:
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
import {
|
|
266
|
+
type AssistantMessage,
|
|
267
|
+
type AssistantMessageEventStream,
|
|
268
|
+
type Context,
|
|
269
|
+
type Model,
|
|
270
|
+
type SimpleStreamOptions,
|
|
271
|
+
calculateCost,
|
|
272
|
+
createAssistantMessageEventStream,
|
|
273
|
+
} from "@mariozechner/pi-ai";
|
|
274
|
+
|
|
275
|
+
function streamMyProvider(
|
|
276
|
+
model: Model<any>,
|
|
277
|
+
context: Context,
|
|
278
|
+
options?: SimpleStreamOptions
|
|
279
|
+
): AssistantMessageEventStream {
|
|
280
|
+
const stream = createAssistantMessageEventStream();
|
|
281
|
+
|
|
282
|
+
(async () => {
|
|
283
|
+
// Initialize output message
|
|
284
|
+
const output: AssistantMessage = {
|
|
285
|
+
role: "assistant",
|
|
286
|
+
content: [],
|
|
287
|
+
api: model.api,
|
|
288
|
+
provider: model.provider,
|
|
289
|
+
model: model.id,
|
|
290
|
+
usage: {
|
|
291
|
+
input: 0,
|
|
292
|
+
output: 0,
|
|
293
|
+
cacheRead: 0,
|
|
294
|
+
cacheWrite: 0,
|
|
295
|
+
totalTokens: 0,
|
|
296
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
297
|
+
},
|
|
298
|
+
stopReason: "stop",
|
|
299
|
+
timestamp: Date.now(),
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
try {
|
|
303
|
+
// Push start event
|
|
304
|
+
stream.push({ type: "start", partial: output });
|
|
305
|
+
|
|
306
|
+
// Make API request and process response...
|
|
307
|
+
// Push content events as they arrive...
|
|
308
|
+
|
|
309
|
+
// Push done event
|
|
310
|
+
stream.push({
|
|
311
|
+
type: "done",
|
|
312
|
+
reason: output.stopReason as "stop" | "length" | "toolUse",
|
|
313
|
+
message: output
|
|
314
|
+
});
|
|
315
|
+
stream.end();
|
|
316
|
+
} catch (error) {
|
|
317
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
318
|
+
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
319
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
320
|
+
stream.end();
|
|
321
|
+
}
|
|
322
|
+
})();
|
|
323
|
+
|
|
324
|
+
return stream;
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Event Types
|
|
329
|
+
|
|
330
|
+
Push events via `stream.push()` in this order:
|
|
331
|
+
|
|
332
|
+
1. `{ type: "start", partial: output }` - Stream started
|
|
333
|
+
|
|
334
|
+
2. Content events (repeatable, track `contentIndex` for each block):
|
|
335
|
+
- `{ type: "text_start", contentIndex, partial }` - Text block started
|
|
336
|
+
- `{ type: "text_delta", contentIndex, delta, partial }` - Text chunk
|
|
337
|
+
- `{ type: "text_end", contentIndex, content, partial }` - Text block ended
|
|
338
|
+
- `{ type: "thinking_start", contentIndex, partial }` - Thinking started
|
|
339
|
+
- `{ type: "thinking_delta", contentIndex, delta, partial }` - Thinking chunk
|
|
340
|
+
- `{ type: "thinking_end", contentIndex, content, partial }` - Thinking ended
|
|
341
|
+
- `{ type: "toolcall_start", contentIndex, partial }` - Tool call started
|
|
342
|
+
- `{ type: "toolcall_delta", contentIndex, delta, partial }` - Tool call JSON chunk
|
|
343
|
+
- `{ type: "toolcall_end", contentIndex, toolCall, partial }` - Tool call ended
|
|
344
|
+
|
|
345
|
+
3. `{ type: "done", reason, message }` or `{ type: "error", reason, error }` - Stream ended
|
|
346
|
+
|
|
347
|
+
The `partial` field in each event contains the current `AssistantMessage` state. Update `output.content` as you receive data, then include `output` as the `partial`.
|
|
348
|
+
|
|
349
|
+
### Content Blocks
|
|
350
|
+
|
|
351
|
+
Add content blocks to `output.content` as they arrive:
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
// Text block
|
|
355
|
+
output.content.push({ type: "text", text: "" });
|
|
356
|
+
stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
|
|
357
|
+
|
|
358
|
+
// As text arrives
|
|
359
|
+
const block = output.content[contentIndex];
|
|
360
|
+
if (block.type === "text") {
|
|
361
|
+
block.text += delta;
|
|
362
|
+
stream.push({ type: "text_delta", contentIndex, delta, partial: output });
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// When block completes
|
|
366
|
+
stream.push({ type: "text_end", contentIndex, content: block.text, partial: output });
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
### Tool Calls
|
|
370
|
+
|
|
371
|
+
Tool calls require accumulating JSON and parsing:
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
// Start tool call
|
|
375
|
+
output.content.push({
|
|
376
|
+
type: "toolCall",
|
|
377
|
+
id: toolCallId,
|
|
378
|
+
name: toolName,
|
|
379
|
+
arguments: {}
|
|
380
|
+
});
|
|
381
|
+
stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
382
|
+
|
|
383
|
+
// Accumulate JSON
|
|
384
|
+
let partialJson = "";
|
|
385
|
+
partialJson += jsonDelta;
|
|
386
|
+
try {
|
|
387
|
+
block.arguments = JSON.parse(partialJson);
|
|
388
|
+
} catch {}
|
|
389
|
+
stream.push({ type: "toolcall_delta", contentIndex, delta: jsonDelta, partial: output });
|
|
390
|
+
|
|
391
|
+
// Complete
|
|
392
|
+
stream.push({
|
|
393
|
+
type: "toolcall_end",
|
|
394
|
+
contentIndex,
|
|
395
|
+
toolCall: { type: "toolCall", id, name, arguments: block.arguments },
|
|
396
|
+
partial: output
|
|
397
|
+
});
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Usage and Cost
|
|
401
|
+
|
|
402
|
+
Update usage from API response and calculate cost:
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
output.usage.input = response.usage.input_tokens;
|
|
406
|
+
output.usage.output = response.usage.output_tokens;
|
|
407
|
+
output.usage.cacheRead = response.usage.cache_read_tokens ?? 0;
|
|
408
|
+
output.usage.cacheWrite = response.usage.cache_write_tokens ?? 0;
|
|
409
|
+
output.usage.totalTokens = output.usage.input + output.usage.output +
|
|
410
|
+
output.usage.cacheRead + output.usage.cacheWrite;
|
|
411
|
+
calculateCost(model, output.usage);
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
### Registration
|
|
415
|
+
|
|
416
|
+
Register your stream function:
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
pi.registerProvider("my-provider", {
|
|
420
|
+
baseUrl: "https://api.example.com",
|
|
421
|
+
apiKey: "MY_API_KEY",
|
|
422
|
+
api: "my-custom-api",
|
|
423
|
+
models: [...],
|
|
424
|
+
streamSimple: streamMyProvider
|
|
425
|
+
});
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
## Testing Your Implementation
|
|
429
|
+
|
|
430
|
+
Test your provider against the same test suites used by built-in providers. Copy and adapt these test files from [packages/ai/test/](https://github.com/badlogic/pi-mono/tree/main/packages/ai/test):
|
|
431
|
+
|
|
432
|
+
| Test | Purpose |
|
|
433
|
+
|------|---------|
|
|
434
|
+
| `stream.test.ts` | Basic streaming, text output |
|
|
435
|
+
| `tokens.test.ts` | Token counting and usage |
|
|
436
|
+
| `abort.test.ts` | AbortSignal handling |
|
|
437
|
+
| `empty.test.ts` | Empty/minimal responses |
|
|
438
|
+
| `context-overflow.test.ts` | Context window limits |
|
|
439
|
+
| `image-limits.test.ts` | Image input handling |
|
|
440
|
+
| `unicode-surrogate.test.ts` | Unicode edge cases |
|
|
441
|
+
| `tool-call-without-result.test.ts` | Tool call edge cases |
|
|
442
|
+
| `image-tool-result.test.ts` | Images in tool results |
|
|
443
|
+
| `total-tokens.test.ts` | Total token calculation |
|
|
444
|
+
| `cross-provider-handoff.test.ts` | Context handoff between providers |
|
|
445
|
+
|
|
446
|
+
Run tests with your provider/model pairs to verify compatibility.
|
|
447
|
+
|
|
448
|
+
## Config Reference
|
|
449
|
+
|
|
450
|
+
```typescript
|
|
451
|
+
interface ProviderConfig {
|
|
452
|
+
/** API endpoint URL. Required when defining models. */
|
|
453
|
+
baseUrl?: string;
|
|
454
|
+
|
|
455
|
+
/** API key or environment variable name. Required when defining models (unless oauth). */
|
|
456
|
+
apiKey?: string;
|
|
457
|
+
|
|
458
|
+
/** API type for streaming. Required at provider or model level when defining models. */
|
|
459
|
+
api?: Api;
|
|
460
|
+
|
|
461
|
+
/** Custom streaming implementation for non-standard APIs. */
|
|
462
|
+
streamSimple?: (
|
|
463
|
+
model: Model<Api>,
|
|
464
|
+
context: Context,
|
|
465
|
+
options?: SimpleStreamOptions
|
|
466
|
+
) => AssistantMessageEventStream;
|
|
467
|
+
|
|
468
|
+
/** Custom headers to include in requests. Values can be env var names. */
|
|
469
|
+
headers?: Record<string, string>;
|
|
470
|
+
|
|
471
|
+
/** If true, adds Authorization: Bearer header with the resolved API key. */
|
|
472
|
+
authHeader?: boolean;
|
|
473
|
+
|
|
474
|
+
/** Models to register. If provided, replaces all existing models for this provider. */
|
|
475
|
+
models?: ProviderModelConfig[];
|
|
476
|
+
|
|
477
|
+
/** OAuth provider for /login support. */
|
|
478
|
+
oauth?: {
|
|
479
|
+
name: string;
|
|
480
|
+
login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials>;
|
|
481
|
+
refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials>;
|
|
482
|
+
getApiKey(credentials: OAuthCredentials): string;
|
|
483
|
+
modifyModels?(models: Model<Api>[], credentials: OAuthCredentials): Model<Api>[];
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
## Model Definition Reference
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
interface ProviderModelConfig {
|
|
492
|
+
/** Model ID (e.g., "claude-sonnet-4-20250514"). */
|
|
493
|
+
id: string;
|
|
494
|
+
|
|
495
|
+
/** Display name (e.g., "Claude 4 Sonnet"). */
|
|
496
|
+
name: string;
|
|
497
|
+
|
|
498
|
+
/** API type override for this specific model. */
|
|
499
|
+
api?: Api;
|
|
500
|
+
|
|
501
|
+
/** Whether the model supports extended thinking. */
|
|
502
|
+
reasoning: boolean;
|
|
503
|
+
|
|
504
|
+
/** Supported input types. */
|
|
505
|
+
input: ("text" | "image")[];
|
|
506
|
+
|
|
507
|
+
/** Cost per million tokens (for usage tracking). */
|
|
508
|
+
cost: {
|
|
509
|
+
input: number;
|
|
510
|
+
output: number;
|
|
511
|
+
cacheRead: number;
|
|
512
|
+
cacheWrite: number;
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
/** Maximum context window size in tokens. */
|
|
516
|
+
contextWindow: number;
|
|
517
|
+
|
|
518
|
+
/** Maximum output tokens. */
|
|
519
|
+
maxTokens: number;
|
|
520
|
+
|
|
521
|
+
/** Custom headers for this specific model. */
|
|
522
|
+
headers?: Record<string, string>;
|
|
523
|
+
|
|
524
|
+
/** OpenAI compatibility settings for openai-completions API. */
|
|
525
|
+
compat?: {
|
|
526
|
+
supportsStore?: boolean;
|
|
527
|
+
supportsDeveloperRole?: boolean;
|
|
528
|
+
supportsReasoningEffort?: boolean;
|
|
529
|
+
supportsUsageInStreaming?: boolean;
|
|
530
|
+
maxTokensField?: "max_completion_tokens" | "max_tokens";
|
|
531
|
+
requiresToolResultName?: boolean;
|
|
532
|
+
requiresAssistantAfterToolResult?: boolean;
|
|
533
|
+
requiresThinkingAsText?: boolean;
|
|
534
|
+
requiresMistralToolIds?: boolean;
|
|
535
|
+
thinkingFormat?: "openai" | "zai";
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
```
|