@monotykamary/pi-opencode-provider 1.0.3
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/.github/FUNDING.yml +4 -0
- package/AGENTS.md +56 -0
- package/LICENSE +21 -0
- package/README.md +171 -0
- package/custom-models.json +24 -0
- package/index.ts +356 -0
- package/models.json +940 -0
- package/package.json +34 -0
- package/patch.json +193 -0
- package/scripts/update-models.js +190 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
## DO NOT EDIT — Auto-generated Files
|
|
4
|
+
|
|
5
|
+
The following files are **idempotent** and regenerated by `scripts/update-models.js`. Never edit them directly — your changes will be overwritten on the next model sync.
|
|
6
|
+
|
|
7
|
+
| File | Why it's auto-generated |
|
|
8
|
+
|------|------------------------|
|
|
9
|
+
| `models.json` | Built from the provider API. `update-models.js` fetches models, preserves curated data for known IDs, and writes this file. |
|
|
10
|
+
| `README.md` (model table) | The table under `## Available Models` is replaced in-place by `update-models.js` after merging base models → patch → custom models. |
|
|
11
|
+
|
|
12
|
+
## Correct Files to Edit
|
|
13
|
+
|
|
14
|
+
When a model needs overrides, new properties, or corrections, edit the appropriate source file below. These are the **source of truth** that the update script reads but never writes.
|
|
15
|
+
|
|
16
|
+
| File | Purpose |
|
|
17
|
+
|------|---------|
|
|
18
|
+
| `patch.json` | Per-model overrides keyed by model ID. Add reasoning flags, compat settings, pricing corrections, thinking level maps, etc. Applied on top of `models.json` at runtime and for README generation. |
|
|
19
|
+
| `custom-models.json` | Models that don't exist in the provider API (hidden models, router endpoints, cross-provider aliases). Merged after patch. Format: array of full model objects (same schema as `models.json` entries). |
|
|
20
|
+
| `index.ts` | Provider extension code. |
|
|
21
|
+
| `scripts/update-models.js` | The sync script itself (edit only if changing how models are fetched/transformed). |
|
|
22
|
+
|
|
23
|
+
## Data Flow
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
Provider API ──fetch──► models.json ──apply──► patch.json ──merge──► custom-models.json
|
|
27
|
+
│ │ │
|
|
28
|
+
└────────────────────────────┴──────────────────────┘
|
|
29
|
+
│
|
|
30
|
+
README model table
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
1. `models.json` — base data from the provider API (auto-generated, DO NOT EDIT)
|
|
34
|
+
2. `patch.json` — overrides applied on top (EDIT THIS for corrections/enrichments)
|
|
35
|
+
3. `custom-models.json` — additional models not in the API (EDIT THIS for new models)
|
|
36
|
+
4. README table — rendered from the merged result of all three (auto-generated, DO NOT EDIT)
|
|
37
|
+
|
|
38
|
+
## Common Tasks
|
|
39
|
+
|
|
40
|
+
### Add a compat setting or override pricing for an existing model
|
|
41
|
+
→ Edit `patch.json`. Add an entry keyed by the model's `id`.
|
|
42
|
+
|
|
43
|
+
### Add a model not available in the provider API
|
|
44
|
+
→ Edit `custom-models.json`. Add a full model object to the array.
|
|
45
|
+
|
|
46
|
+
### Update models from the provider API
|
|
47
|
+
→ Run `node scripts/update-models.js` (may require an API key env var).
|
|
48
|
+
|
|
49
|
+
### Regenerate the README model table
|
|
50
|
+
→ Run `node scripts/update-models.js` — it updates both `models.json` and the README table.
|
|
51
|
+
|
|
52
|
+
## TL;DR
|
|
53
|
+
|
|
54
|
+
- **Never edit `models.json`** — edit `patch.json` instead.
|
|
55
|
+
- **Never edit the README model table** — run the update script instead.
|
|
56
|
+
- `patch.json` and `custom-models.json` are the source files you should modify.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🌐 pi-opencode-provider
|
|
4
|
+
|
|
5
|
+
**46+ models via [opencode](https://opencode.ai/)**
|
|
6
|
+
|
|
7
|
+
_GPT, Claude, Gemini, GLM, MiniMax, Kimi, DeepSeek — unified API for [pi](https://github.com/earendil-works/pi-coding-agent)._
|
|
8
|
+
|
|
9
|
+
[](https://github.com/earendil-works/pi-coding-agent)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
|
|
12
|
+
</div>
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
|
|
18
|
+
- **50+ AI Models** including GPT-5.x Codex, Claude Opus/Sonnet/Haiku, Gemini 3.x, GLM, MiniMax, Kimi K2.5, and more
|
|
19
|
+
- **Multi-API Support** — uses the correct API protocol per model (Anthropic, OpenAI Responses, OpenAI Completions, Gemini)
|
|
20
|
+
- **Cost Tracking** with per-model pricing for budget management
|
|
21
|
+
- **Reasoning Models** with thinking level maps for proper effort control
|
|
22
|
+
- **Vision Support** for image-capable models
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### Option 1: Using `pi install` (Recommended)
|
|
27
|
+
|
|
28
|
+
Install from npm:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pi install @monotykamary/pi-opencode-provider
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or directly from GitHub:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pi install https://github.com/monotykamary/pi-opencode-provider
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Then set your API key and run pi:
|
|
41
|
+
```bash
|
|
42
|
+
# Recommended: add to auth.json
|
|
43
|
+
# See Authentication section below
|
|
44
|
+
|
|
45
|
+
# Or set as environment variable
|
|
46
|
+
export OPENCODE_API_KEY=your-api-key-here
|
|
47
|
+
|
|
48
|
+
pi
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Option 2: Manual Clone
|
|
52
|
+
|
|
53
|
+
1. Clone this repository:
|
|
54
|
+
```bash
|
|
55
|
+
git clone https://github.com/monotykamary/pi-opencode-provider.git
|
|
56
|
+
cd pi-opencode-provider
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
2. Set your opencode API key:
|
|
60
|
+
```bash
|
|
61
|
+
# Recommended: add to auth.json
|
|
62
|
+
# See Authentication section below
|
|
63
|
+
|
|
64
|
+
# Or set as environment variable
|
|
65
|
+
export OPENCODE_API_KEY=your-api-key-here
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
3. Run pi with the extension:
|
|
69
|
+
```bash
|
|
70
|
+
pi -e /path/to/pi-opencode-provider
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Available Models
|
|
74
|
+
|
|
75
|
+
| Model | API | Type | Context | Max Tokens | Input Cost | Output Cost |
|
|
76
|
+
|-------|-----|------|---------|------------|------------|-------------|
|
|
77
|
+
| Big Pickle | Completions | Text | 200K | 32K | Free | Free |
|
|
78
|
+
| Claude Fable 5 | Anthropic | Text + Image | 1.0M | 128K | $10.00 | $50.00 |
|
|
79
|
+
| Claude Haiku 4.5 | Anthropic | Text + Image | 200K | 64K | $1.00 | $5.00 |
|
|
80
|
+
| Claude Opus 4.1 | Anthropic | Text + Image | 200K | 32K | $15.00 | $75.00 |
|
|
81
|
+
| Claude Opus 4.5 | Anthropic | Text + Image | 200K | 64K | $5.00 | $25.00 |
|
|
82
|
+
| Claude Opus 4.6 | Anthropic | Text + Image | 1.0M | 128K | $5.00 | $25.00 |
|
|
83
|
+
| Claude Opus 4.7 | Anthropic | Text + Image | 1.0M | 128K | $5.00 | $25.00 |
|
|
84
|
+
| Claude Opus 4.8 | Anthropic | Text + Image | 1.0M | 128K | $5.00 | $25.00 |
|
|
85
|
+
| Claude Sonnet 4 | Anthropic | Text + Image | 1.0M | 64K | $3.00 | $15.00 |
|
|
86
|
+
| Claude Sonnet 4.5 | Anthropic | Text + Image | 1.0M | 64K | $3.00 | $15.00 |
|
|
87
|
+
| Claude Sonnet 4.6 | Anthropic | Text + Image | 1.0M | 64K | $3.00 | $15.00 |
|
|
88
|
+
| Claude Sonnet 5 | Anthropic | Text + Image | 1.0M | 128K | $2.00 | $10.00 |
|
|
89
|
+
| DeepSeek V4 Flash | Completions | Text | 1.0M | 384K | $0.14 | $0.28 |
|
|
90
|
+
| DeepSeek V4 Flash Free | Completions | Text | 200K | 128K | Free | Free |
|
|
91
|
+
| DeepSeek V4 Pro | Completions | Text | 1.0M | 384K | $1.74 | $3.84 |
|
|
92
|
+
| Gemini 3 Flash | Gemini | Text + Image | 1.0M | 66K | $0.50 | $3.00 |
|
|
93
|
+
| Gemini 3.1 Pro Preview | Gemini | Text + Image | 1.0M | 66K | $2.00 | $12.00 |
|
|
94
|
+
| Gemini 3.5 Flash | Gemini | Text + Image | 1.0M | 66K | $1.50 | $9.00 |
|
|
95
|
+
| GLM-5 | Completions | Text | 205K | 131K | $1.00 | $3.20 |
|
|
96
|
+
| GLM-5.1 | Completions | Text | 205K | 131K | $1.40 | $4.40 |
|
|
97
|
+
| GLM-5.2 | Completions | Text | 1.0M | 131K | $1.40 | $4.40 |
|
|
98
|
+
| GPT-5 | Responses | Text + Image | 400K | 128K | $1.07 | $8.50 |
|
|
99
|
+
| GPT-5 Codex | Responses | Text + Image | 400K | 128K | $1.07 | $8.50 |
|
|
100
|
+
| GPT-5 Nano | Responses | Text + Image | 400K | 128K | $0.05 | $0.40 |
|
|
101
|
+
| GPT-5.1 | Responses | Text + Image | 400K | 128K | $1.07 | $8.50 |
|
|
102
|
+
| GPT-5.1 Codex | Responses | Text + Image | 400K | 128K | $1.07 | $8.50 |
|
|
103
|
+
| GPT-5.1 Codex Max | Responses | Text + Image | 400K | 128K | $1.25 | $10.00 |
|
|
104
|
+
| GPT-5.1 Codex Mini | Responses | Text + Image | 400K | 128K | $0.25 | $2.00 |
|
|
105
|
+
| GPT-5.2 | Responses | Text + Image | 400K | 128K | $1.75 | $14.00 |
|
|
106
|
+
| GPT-5.2 Codex | Responses | Text + Image | 400K | 128K | $1.75 | $14.00 |
|
|
107
|
+
| GPT-5.3 Codex | Responses | Text + Image | 400K | 128K | $1.75 | $14.00 |
|
|
108
|
+
| GPT-5.3 Codex Spark | Responses | Text | 128K | 128K | $1.75 | $14.00 |
|
|
109
|
+
| GPT-5.4 | Responses | Text + Image | 1.1M | 128K | $2.50 | $15.00 |
|
|
110
|
+
| GPT-5.4 Mini | Responses | Text + Image | 400K | 128K | $0.75 | $4.50 |
|
|
111
|
+
| GPT-5.4 Nano | Responses | Text + Image | 400K | 128K | $0.20 | $1.25 |
|
|
112
|
+
| GPT-5.4 Pro | Responses | Text + Image | 1.1M | 128K | $30.00 | $180.00 |
|
|
113
|
+
| GPT-5.5 | Responses | Text + Image | 1.1M | 128K | $5.00 | $30.00 |
|
|
114
|
+
| GPT-5.5 Pro | Responses | Text + Image | 1.1M | 128K | $30.00 | $180.00 |
|
|
115
|
+
| Grok Build 0.1 | Completions | Text + Image | 256K | 256K | $1.00 | $2.00 |
|
|
116
|
+
| Kimi K2.5 | Completions | Text + Image | 262K | 66K | $0.60 | $3.00 |
|
|
117
|
+
| Kimi K2.6 | Completions | Text + Image | 262K | 66K | $0.95 | $4.00 |
|
|
118
|
+
| Kimi K2.7 Code | Completions | Text + Image | 262K | 262K | $0.95 | $4.00 |
|
|
119
|
+
| MiMo V2.5 Free | Completions | Text + Image | 200K | 32K | Free | Free |
|
|
120
|
+
| MiniMax-M2.5 | Completions | Text | 205K | 131K | $0.30 | $1.20 |
|
|
121
|
+
| MiniMax-M2.7 | Completions | Text | 205K | 131K | $0.30 | $1.20 |
|
|
122
|
+
| MiniMax-M3 | Completions | Text + Image | 512K | 128K | $0.30 | $1.20 |
|
|
123
|
+
| Nemotron 3 Ultra Free | Completions | Text | 1.0M | 128K | Free | Free |
|
|
124
|
+
| North Mini Code Free | Completions | Text | 256K | 64K | Free | Free |
|
|
125
|
+
| Qwen3.5 Plus | Anthropic | Text + Image | 262K | 66K | $0.20 | $1.20 |
|
|
126
|
+
| Qwen3.6 Plus | Anthropic | Text + Image | 262K | 66K | $0.50 | $3.00 |
|
|
127
|
+
*Costs are per million tokens. Prices subject to change - check [opencode.ai](https://opencode.ai) for current pricing.*
|
|
128
|
+
|
|
129
|
+
## Usage
|
|
130
|
+
|
|
131
|
+
After loading the extension, use the `/model` command in pi to select your preferred model:
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
/model
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Then select "opencode" as the provider and choose from the available models.
|
|
138
|
+
|
|
139
|
+
## Authentication
|
|
140
|
+
|
|
141
|
+
The opencode API key can be configured in multiple ways (resolved in this order):
|
|
142
|
+
|
|
143
|
+
1. **`auth.json`** (recommended) — Add to `~/.pi/agent/auth.json`:
|
|
144
|
+
```json
|
|
145
|
+
{ "opencode": { "type": "api_key", "key": "your-api-key" } }
|
|
146
|
+
```
|
|
147
|
+
The `key` field supports literal values, env var names, and shell commands (prefix with `!`). See [pi's auth file docs](https://github.com/badlogic/pi-mono) for details.
|
|
148
|
+
2. **Runtime override** — Use the `--api-key` CLI flag
|
|
149
|
+
3. **Environment variable** — Set `OPENCODE_API_KEY`
|
|
150
|
+
|
|
151
|
+
## Environment Variables
|
|
152
|
+
|
|
153
|
+
| Variable | Required | Description |
|
|
154
|
+
|----------|----------|-------------|
|
|
155
|
+
| `OPENCODE_API_KEY` | No | Your opencode.ai API key (fallback if not in auth.json) |
|
|
156
|
+
|
|
157
|
+
## Configuration
|
|
158
|
+
|
|
159
|
+
Add to your pi configuration for automatic loading:
|
|
160
|
+
|
|
161
|
+
```json
|
|
162
|
+
{
|
|
163
|
+
"extensions": [
|
|
164
|
+
"/path/to/pi-opencode-provider"
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "gemini-3-pro",
|
|
4
|
+
"name": "Gemini 3 Pro",
|
|
5
|
+
"api": "google-generative-ai",
|
|
6
|
+
"baseUrl": "https://opencode.ai/zen/v1",
|
|
7
|
+
"reasoning": true,
|
|
8
|
+
"input": ["text", "image"],
|
|
9
|
+
"cost": { "input": 1.25, "output": 10, "cacheRead": 0.125, "cacheWrite": 0 },
|
|
10
|
+
"contextWindow": 1048576,
|
|
11
|
+
"maxTokens": 65536
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"id": "grok-code",
|
|
15
|
+
"name": "Grok Code",
|
|
16
|
+
"api": "openai-completions",
|
|
17
|
+
"baseUrl": "https://opencode.ai/zen/v1",
|
|
18
|
+
"reasoning": true,
|
|
19
|
+
"input": ["text", "image"],
|
|
20
|
+
"cost": { "input": 1, "output": 2, "cacheRead": 0.2, "cacheWrite": 0 },
|
|
21
|
+
"contextWindow": 256000,
|
|
22
|
+
"maxTokens": 256000
|
|
23
|
+
}
|
|
24
|
+
]
|
package/index.ts
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* opencode Provider Extension
|
|
3
|
+
*
|
|
4
|
+
* Registers opencode as a custom provider with multi-API support.
|
|
5
|
+
* Models use anthropic-messages, openai-responses, openai-completions,
|
|
6
|
+
* or google-generative-ai as appropriate, with per-model baseUrl/api.
|
|
7
|
+
*
|
|
8
|
+
* Model resolution strategy: Stale-While-Revalidate
|
|
9
|
+
* 1. Serve stale immediately: disk cache → embedded models.json (zero-latency)
|
|
10
|
+
* 2. Revalidate in background: live API /models → merge with embedded → cache → hot-swap
|
|
11
|
+
* 3. patch.json + custom-models.json applied on top of whichever source won
|
|
12
|
+
*
|
|
13
|
+
* Merge order: [live|cache|embedded] → apply patch.json → merge custom-models.json
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* # Set your API key
|
|
17
|
+
* export OPENCODE_API_KEY=your-api-key
|
|
18
|
+
*
|
|
19
|
+
* # Run pi with the extension
|
|
20
|
+
* pi -e /path/to/pi-opencode-provider
|
|
21
|
+
*
|
|
22
|
+
* Then use /model to select from available models
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { getAgentDir, type ExtensionAPI, type ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
26
|
+
import modelsData from "./models.json" with { type: "json" };
|
|
27
|
+
import customModelsData from "./custom-models.json" with { type: "json" };
|
|
28
|
+
import patchData from "./patch.json" with { type: "json" };
|
|
29
|
+
import fs from "fs";
|
|
30
|
+
import path from "path";
|
|
31
|
+
|
|
32
|
+
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
type Api = "openai-completions" | "openai-responses" | "anthropic-messages" | "google-generative-ai";
|
|
35
|
+
|
|
36
|
+
interface ThinkingLevelMapValue {
|
|
37
|
+
[level: string]: string | null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface JsonModel {
|
|
41
|
+
id: string;
|
|
42
|
+
name: string;
|
|
43
|
+
api?: Api;
|
|
44
|
+
baseUrl?: string;
|
|
45
|
+
reasoning: boolean;
|
|
46
|
+
thinkingLevelMap?: ThinkingLevelMapValue;
|
|
47
|
+
input: ("text" | "image")[];
|
|
48
|
+
cost: {
|
|
49
|
+
input: number;
|
|
50
|
+
output: number;
|
|
51
|
+
cacheRead: number;
|
|
52
|
+
cacheWrite: number;
|
|
53
|
+
};
|
|
54
|
+
contextWindow: number;
|
|
55
|
+
maxTokens: number;
|
|
56
|
+
compat?: Record<string, unknown>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface PatchEntry {
|
|
60
|
+
name?: string;
|
|
61
|
+
api?: Api;
|
|
62
|
+
baseUrl?: string;
|
|
63
|
+
reasoning?: boolean;
|
|
64
|
+
thinkingLevelMap?: ThinkingLevelMapValue;
|
|
65
|
+
input?: ("text" | "image")[];
|
|
66
|
+
cost?: {
|
|
67
|
+
input?: number;
|
|
68
|
+
output?: number;
|
|
69
|
+
cacheRead?: number;
|
|
70
|
+
cacheWrite?: number;
|
|
71
|
+
};
|
|
72
|
+
contextWindow?: number;
|
|
73
|
+
maxTokens?: number;
|
|
74
|
+
compat?: Record<string, unknown>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type PatchData = Record<string, PatchEntry>;
|
|
78
|
+
|
|
79
|
+
// ─── Patch Application ────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
function applyPatch(model: JsonModel, patch: PatchEntry): JsonModel {
|
|
82
|
+
const result = { ...model };
|
|
83
|
+
|
|
84
|
+
if (patch.name !== undefined) result.name = patch.name;
|
|
85
|
+
if (patch.api !== undefined) result.api = patch.api;
|
|
86
|
+
if (patch.baseUrl !== undefined) result.baseUrl = patch.baseUrl;
|
|
87
|
+
if (patch.reasoning !== undefined) result.reasoning = patch.reasoning;
|
|
88
|
+
if (patch.thinkingLevelMap !== undefined) result.thinkingLevelMap = patch.thinkingLevelMap;
|
|
89
|
+
if (patch.input !== undefined) result.input = patch.input;
|
|
90
|
+
if (patch.contextWindow !== undefined) result.contextWindow = patch.contextWindow;
|
|
91
|
+
if (patch.maxTokens !== undefined) result.maxTokens = patch.maxTokens;
|
|
92
|
+
|
|
93
|
+
if (patch.cost) {
|
|
94
|
+
result.cost = {
|
|
95
|
+
input: patch.cost.input ?? result.cost.input,
|
|
96
|
+
output: patch.cost.output ?? result.cost.output,
|
|
97
|
+
cacheRead: patch.cost.cacheRead ?? result.cost.cacheRead,
|
|
98
|
+
cacheWrite: patch.cost.cacheWrite ?? result.cost.cacheWrite,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
if (patch.compat) {
|
|
102
|
+
result.compat = { ...(result.compat || {}), ...patch.compat };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (!result.reasoning && result.compat?.thinkingFormat) {
|
|
106
|
+
delete result.compat.thinkingFormat;
|
|
107
|
+
}
|
|
108
|
+
if (result.compat && Object.keys(result.compat).length === 0) {
|
|
109
|
+
delete result.compat;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Full pipeline: base models → patch → custom → result */
|
|
116
|
+
function buildModels(base: JsonModel[], custom: JsonModel[], patch: PatchData): JsonModel[] {
|
|
117
|
+
const modelMap = new Map<string, JsonModel>();
|
|
118
|
+
|
|
119
|
+
for (const model of base) {
|
|
120
|
+
modelMap.set(model.id, model);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const [id, patchEntry] of Object.entries(patch)) {
|
|
124
|
+
const existing = modelMap.get(id);
|
|
125
|
+
if (existing) {
|
|
126
|
+
modelMap.set(id, applyPatch(existing, patchEntry));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
for (const model of custom) {
|
|
131
|
+
const existing = modelMap.get(model.id);
|
|
132
|
+
const patchEntry = patch[model.id];
|
|
133
|
+
if (existing && patchEntry) {
|
|
134
|
+
modelMap.set(model.id, applyPatch(model, patchEntry));
|
|
135
|
+
} else if (existing) {
|
|
136
|
+
modelMap.set(model.id, model);
|
|
137
|
+
} else if (patchEntry) {
|
|
138
|
+
modelMap.set(model.id, applyPatch(model, patchEntry));
|
|
139
|
+
} else {
|
|
140
|
+
modelMap.set(model.id, model);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return Array.from(modelMap.values());
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ─── Stale-While-Revalidate Model Sync ────────────────────────────────────────
|
|
148
|
+
|
|
149
|
+
const PROVIDER_ID = "opencode";
|
|
150
|
+
const BASE_URL = "https://opencode.ai/zen/v1";
|
|
151
|
+
const MODELS_URL = `${BASE_URL}/models`;
|
|
152
|
+
const CACHE_DIR = path.join(getAgentDir(), "cache");
|
|
153
|
+
const CACHE_PATH = path.join(CACHE_DIR, `${PROVIDER_ID}-models.json`);
|
|
154
|
+
const LIVE_FETCH_TIMEOUT_MS = 8000;
|
|
155
|
+
|
|
156
|
+
/** Map models.dev provider.npm to pi API type and base URL. */
|
|
157
|
+
const NPM_TO_API: Record<string, { api: Api; baseUrl: string }> = {
|
|
158
|
+
"@ai-sdk/anthropic": { api: "anthropic-messages", baseUrl: "https://opencode.ai/zen" },
|
|
159
|
+
"@ai-sdk/openai": { api: "openai-responses", baseUrl: "https://opencode.ai/zen/v1" },
|
|
160
|
+
"@ai-sdk/google": { api: "google-generative-ai", baseUrl: "https://opencode.ai/zen/v1" },
|
|
161
|
+
};
|
|
162
|
+
const DEFAULT_API: { api: Api; baseUrl: string } = { api: "openai-completions", baseUrl: "https://opencode.ai/zen/v1" };
|
|
163
|
+
|
|
164
|
+
/** Transform a model from the opencode /v1/models API. Returns minimal data. */
|
|
165
|
+
function transformApiModel(apiModel: any): JsonModel {
|
|
166
|
+
const npm = apiModel.provider?.npm;
|
|
167
|
+
const { api, baseUrl } = (npm && NPM_TO_API[npm]) || DEFAULT_API;
|
|
168
|
+
const rawInput = apiModel.modalities?.input || ["text"];
|
|
169
|
+
// Pi Model type only supports "text" and "image"
|
|
170
|
+
const input: ("text" | "image")[] = rawInput.filter((m: string) => m === "text" || m === "image") as ("text" | "image")[];
|
|
171
|
+
if (!input.includes("text")) input.unshift("text");
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
id: apiModel.id,
|
|
175
|
+
name: apiModel.name || apiModel.id,
|
|
176
|
+
api,
|
|
177
|
+
baseUrl,
|
|
178
|
+
reasoning: apiModel.reasoning || false,
|
|
179
|
+
input,
|
|
180
|
+
cost: {
|
|
181
|
+
input: apiModel.cost?.input || 0,
|
|
182
|
+
output: apiModel.cost?.output || 0,
|
|
183
|
+
cacheRead: apiModel.cost?.cache_read || 0,
|
|
184
|
+
cacheWrite: apiModel.cost?.cache_write || 0,
|
|
185
|
+
},
|
|
186
|
+
contextWindow: apiModel.limit?.context || apiModel.context_length || 131072,
|
|
187
|
+
maxTokens: apiModel.limit?.output || 0,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function fetchLiveModels(apiKey: string, signal?: AbortSignal): Promise<JsonModel[] | null> {
|
|
192
|
+
try {
|
|
193
|
+
const response = await fetch(MODELS_URL, {
|
|
194
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
195
|
+
signal: signal ? AbortSignal.any([AbortSignal.timeout(LIVE_FETCH_TIMEOUT_MS), signal]) : AbortSignal.timeout(LIVE_FETCH_TIMEOUT_MS),
|
|
196
|
+
});
|
|
197
|
+
if (!response.ok) return null;
|
|
198
|
+
const data = await response.json();
|
|
199
|
+
const apiModels = Array.isArray(data) ? data : (data.data || []);
|
|
200
|
+
if (!Array.isArray(apiModels) || apiModels.length === 0) return null;
|
|
201
|
+
return apiModels.map(transformApiModel);
|
|
202
|
+
} catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function loadCachedModels(): JsonModel[] | null {
|
|
208
|
+
try {
|
|
209
|
+
const data = JSON.parse(fs.readFileSync(CACHE_PATH, "utf8"));
|
|
210
|
+
return Array.isArray(data) ? data : null;
|
|
211
|
+
} catch {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function cacheModels(models: JsonModel[]): void {
|
|
217
|
+
try {
|
|
218
|
+
fs.mkdirSync(CACHE_DIR, { recursive: true });
|
|
219
|
+
fs.writeFileSync(CACHE_PATH, JSON.stringify(models, null, 2) + "\n");
|
|
220
|
+
} catch {
|
|
221
|
+
// Cache write failure is non-fatal
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function mergeWithEmbedded(liveModels: JsonModel[], embeddedModels: JsonModel[]): JsonModel[] {
|
|
226
|
+
const embeddedMap = new Map(embeddedModels.map(m => [m.id, m]));
|
|
227
|
+
const seen = new Set<string>();
|
|
228
|
+
const result: JsonModel[] = [];
|
|
229
|
+
for (const liveModel of liveModels) {
|
|
230
|
+
const embedded = embeddedMap.get(liveModel.id);
|
|
231
|
+
seen.add(liveModel.id);
|
|
232
|
+
if (embedded) {
|
|
233
|
+
// Self-heal: live API pricing is authoritative field-by-field. Prefer the
|
|
234
|
+
// live cost when the API reports it (non-zero); fall back to embedded when
|
|
235
|
+
// the API is silent (0) so curated cacheRead/cacheWrite isn't clobbered and
|
|
236
|
+
// providers whose /models endpoint exposes no pricing keep their curated
|
|
237
|
+
// cost. Curation (reasoning/input/compat/name) still wins via ...embedded.
|
|
238
|
+
result.push({
|
|
239
|
+
...liveModel,
|
|
240
|
+
...embedded,
|
|
241
|
+
cost: {
|
|
242
|
+
input: liveModel.cost.input || embedded.cost.input,
|
|
243
|
+
output: liveModel.cost.output || embedded.cost.output,
|
|
244
|
+
cacheRead: liveModel.cost.cacheRead || embedded.cost.cacheRead,
|
|
245
|
+
cacheWrite: liveModel.cost.cacheWrite || embedded.cost.cacheWrite,
|
|
246
|
+
},
|
|
247
|
+
contextWindow: liveModel.contextWindow || embedded.contextWindow,
|
|
248
|
+
});
|
|
249
|
+
} else {
|
|
250
|
+
result.push(liveModel);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// Append any embedded models that the live API didn't return
|
|
254
|
+
for (const em of embeddedModels) {
|
|
255
|
+
if (!seen.has(em.id)) {
|
|
256
|
+
result.push(em);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function loadStaleModels(embeddedModels: JsonModel[]): JsonModel[] {
|
|
263
|
+
const cached = loadCachedModels();
|
|
264
|
+
if (!cached || cached.length === 0) return embeddedModels;
|
|
265
|
+
|
|
266
|
+
// Merge embedded models that are missing from cache (newly added models)
|
|
267
|
+
const cachedMap = new Map(cached.map(m => [m.id, m]));
|
|
268
|
+
for (const em of embeddedModels) {
|
|
269
|
+
if (!cachedMap.has(em.id)) {
|
|
270
|
+
cached.push(em);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return cached;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function revalidateModels(apiKey: string | undefined, embeddedModels: JsonModel[], signal?: AbortSignal): Promise<JsonModel[] | null> {
|
|
277
|
+
if (!apiKey) return null;
|
|
278
|
+
const liveModels = await fetchLiveModels(apiKey, signal);
|
|
279
|
+
if (!liveModels || liveModels.length === 0) return null;
|
|
280
|
+
const merged = mergeWithEmbedded(liveModels, embeddedModels);
|
|
281
|
+
cacheModels(merged);
|
|
282
|
+
return merged;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ─── API Key Resolution (via ModelRegistry) ────────────────────────────────────
|
|
286
|
+
|
|
287
|
+
let cachedApiKey: string | undefined;
|
|
288
|
+
let revalidateAbort: AbortController | null = null;
|
|
289
|
+
|
|
290
|
+
async function resolveApiKey(modelRegistry: ModelRegistry): Promise<void> {
|
|
291
|
+
cachedApiKey = await modelRegistry.getApiKeyForProvider("opencode") ?? undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// ─── Extension Entry Point ────────────────────────────────────────────────────
|
|
295
|
+
|
|
296
|
+
export default function (pi: ExtensionAPI) {
|
|
297
|
+
const embeddedModels = modelsData as JsonModel[];
|
|
298
|
+
const customModels = customModelsData as JsonModel[];
|
|
299
|
+
const patches = patchData as PatchData;
|
|
300
|
+
|
|
301
|
+
const staleBase = loadStaleModels(embeddedModels);
|
|
302
|
+
const staleModels = buildModels(staleBase, customModels, patches);
|
|
303
|
+
|
|
304
|
+
pi.registerProvider("opencode", {
|
|
305
|
+
baseUrl: BASE_URL,
|
|
306
|
+
apiKey: "$OPENCODE_API_KEY",
|
|
307
|
+
api: "openai-completions",
|
|
308
|
+
models: staleModels.map(m => ({
|
|
309
|
+
id: m.id,
|
|
310
|
+
name: m.name,
|
|
311
|
+
api: m.api || "openai-completions",
|
|
312
|
+
baseUrl: m.baseUrl,
|
|
313
|
+
reasoning: m.reasoning,
|
|
314
|
+
thinkingLevelMap: m.thinkingLevelMap,
|
|
315
|
+
input: m.input,
|
|
316
|
+
cost: m.cost,
|
|
317
|
+
contextWindow: m.contextWindow,
|
|
318
|
+
maxTokens: m.maxTokens,
|
|
319
|
+
compat: m.compat,
|
|
320
|
+
})),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
324
|
+
revalidateAbort?.abort();
|
|
325
|
+
revalidateAbort = new AbortController();
|
|
326
|
+
const signal = revalidateAbort.signal;
|
|
327
|
+
resolveApiKey(ctx.modelRegistry).then(() => {
|
|
328
|
+
revalidateModels(cachedApiKey, embeddedModels, signal).then((freshBase) => {
|
|
329
|
+
if (freshBase && !signal.aborted) {
|
|
330
|
+
pi.registerProvider("opencode", {
|
|
331
|
+
baseUrl: BASE_URL,
|
|
332
|
+
apiKey: "$OPENCODE_API_KEY",
|
|
333
|
+
api: "openai-completions",
|
|
334
|
+
models: buildModels(freshBase, customModels, patches).map(m => ({
|
|
335
|
+
id: m.id,
|
|
336
|
+
name: m.name,
|
|
337
|
+
api: m.api || "openai-completions",
|
|
338
|
+
baseUrl: m.baseUrl,
|
|
339
|
+
reasoning: m.reasoning,
|
|
340
|
+
thinkingLevelMap: m.thinkingLevelMap,
|
|
341
|
+
input: m.input,
|
|
342
|
+
cost: m.cost,
|
|
343
|
+
contextWindow: m.contextWindow,
|
|
344
|
+
maxTokens: m.maxTokens,
|
|
345
|
+
compat: m.compat,
|
|
346
|
+
})),
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
pi.on("session_shutdown", () => {
|
|
354
|
+
revalidateAbort?.abort();
|
|
355
|
+
});
|
|
356
|
+
}
|