pi-service-tier 0.1.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/LICENSE +21 -0
- package/README.md +81 -0
- package/package.json +48 -0
- package/screenshot.png +0 -0
- package/service-tier.ts +271 -0
- package/shared.ts +361 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthias Vallentin
|
|
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,81 @@
|
|
|
1
|
+
# ⚡ pi-service-tier
|
|
2
|
+
|
|
3
|
+
A [pi](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent)
|
|
4
|
+
extension that applies provider service tiers.
|
|
5
|
+
|
|
6
|
+
## 📦 Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pi install npm:pi-service-tier
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
If you use [pi-fancy-footer](https://github.com/mavam/pi-fancy-footer), you'll
|
|
13
|
+
get a service tier widget in the footer:
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
## ✨ What it does
|
|
18
|
+
|
|
19
|
+
- Adds service tier parameters to supported provider requests when a tier is
|
|
20
|
+
configured
|
|
21
|
+
- Adds `/fast` to toggle the current model provider between fast mode and off
|
|
22
|
+
- Adds `/service-tier` to configure all supported providers from an interactive
|
|
23
|
+
modal
|
|
24
|
+
- Adds an optional service tier widget when `pi-fancy-footer` is installed
|
|
25
|
+
|
|
26
|
+
## 🚀 Commands
|
|
27
|
+
|
|
28
|
+
- `/fast`: toggles the current model provider between its fast tier and off. The
|
|
29
|
+
supported providers all use `priority` as the fast tier.
|
|
30
|
+
|
|
31
|
+
- `/service-tier`: opens an interactive editor. The current model provider
|
|
32
|
+
appears first, followed by the remaining supported providers. Press Enter or
|
|
33
|
+
Space to cycle through `off` and the provider-specific tiers.
|
|
34
|
+
|
|
35
|
+
## ⚙️ Configure
|
|
36
|
+
|
|
37
|
+
Run `/service-tier` or create `~/.pi/agent/service-tier.json`:
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"openai": "priority",
|
|
42
|
+
"openai-codex": "flex",
|
|
43
|
+
"anthropic": "priority",
|
|
44
|
+
"google": "priority",
|
|
45
|
+
"google-vertex": "flex"
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Supported providers
|
|
50
|
+
|
|
51
|
+
| Provider | Tiers | Fast tier |
|
|
52
|
+
| --------------- | ---------------------- | ---------- |
|
|
53
|
+
| `openai` | `flex`, `priority` | `priority` |
|
|
54
|
+
| `openai-codex` | `flex`, `priority` | `priority` |
|
|
55
|
+
| `anthropic` | `priority`, `standard` | `priority` |
|
|
56
|
+
| `google` | `flex`, `priority` | `priority` |
|
|
57
|
+
| `google-vertex` | `flex`, `priority` | `priority` |
|
|
58
|
+
|
|
59
|
+
To turn a provider off, omit its key. Only the values listed above are accepted.
|
|
60
|
+
Batch APIs are separate asynchronous APIs and are not configured by this
|
|
61
|
+
extension.
|
|
62
|
+
|
|
63
|
+
## 🧩 Footer widget
|
|
64
|
+
|
|
65
|
+
When [pi-fancy-footer](https://github.com/mavam/pi-fancy-footer) is installed,
|
|
66
|
+
the widget appears only when the active model uses a supported provider/API pair
|
|
67
|
+
and that provider has a configured tier.
|
|
68
|
+
|
|
69
|
+
The widget id is `pi-service-tier.service-tier`.
|
|
70
|
+
|
|
71
|
+
## 📝 TODO
|
|
72
|
+
|
|
73
|
+
- Account for service-tier pricing in pi usage metrics. The extension currently
|
|
74
|
+
injects the tier into the provider request payload, but pi's OpenAI Codex cost
|
|
75
|
+
calculation reads the requested tier from provider options. Until pi exposes a
|
|
76
|
+
first-class extension path for that option, displayed usage costs can omit
|
|
77
|
+
flex or priority multipliers.
|
|
78
|
+
|
|
79
|
+
## 📄 License
|
|
80
|
+
|
|
81
|
+
[MIT](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-service-tier",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Provider service-tier controls for pi",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"@mariozechner/pi-coding-agent": "*",
|
|
8
|
+
"@mariozechner/pi-tui": "*",
|
|
9
|
+
"typebox": "*"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"service-tier.ts",
|
|
13
|
+
"shared.ts",
|
|
14
|
+
"README.md",
|
|
15
|
+
"screenshot.png",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"pi-package",
|
|
20
|
+
"pi",
|
|
21
|
+
"pi-extension",
|
|
22
|
+
"service-tier",
|
|
23
|
+
"openai",
|
|
24
|
+
"codex",
|
|
25
|
+
"footer",
|
|
26
|
+
"statusline"
|
|
27
|
+
],
|
|
28
|
+
"author": "Matthias Vallentin",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/mavam/pi-service-tier.git"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=24.0.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"test": "node --test --experimental-strip-types *.test.ts"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"pi": {
|
|
44
|
+
"extensions": [
|
|
45
|
+
"./service-tier.ts"
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
}
|
package/screenshot.png
ADDED
|
Binary file
|
package/service-tier.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSettingsListTheme,
|
|
3
|
+
type ExtensionAPI,
|
|
4
|
+
type ExtensionCommandContext,
|
|
5
|
+
type ExtensionContext,
|
|
6
|
+
} from "@mariozechner/pi-coding-agent";
|
|
7
|
+
import {
|
|
8
|
+
Container,
|
|
9
|
+
SettingsList,
|
|
10
|
+
Text,
|
|
11
|
+
type SettingItem,
|
|
12
|
+
} from "@mariozechner/pi-tui";
|
|
13
|
+
import {
|
|
14
|
+
DEFAULT_SERVICE_TIER_CONFIG,
|
|
15
|
+
SERVICE_TIER_PROVIDER_DEFINITIONS,
|
|
16
|
+
applyServiceTierToPayload,
|
|
17
|
+
createServiceTierSections,
|
|
18
|
+
getServiceTierConfigPath,
|
|
19
|
+
isServiceTierProvider,
|
|
20
|
+
loadServiceTierConfig,
|
|
21
|
+
resolveEffectiveServiceTier,
|
|
22
|
+
setProviderServiceTier,
|
|
23
|
+
toggleFastServiceTier,
|
|
24
|
+
writeServiceTierConfigSnapshot,
|
|
25
|
+
type ServiceTierName,
|
|
26
|
+
type ServiceTierConfigSnapshot,
|
|
27
|
+
} from "./shared.ts";
|
|
28
|
+
|
|
29
|
+
export const SERVICE_TIER_WIDGET_ID = "pi-service-tier.service-tier";
|
|
30
|
+
|
|
31
|
+
const FANCY_FOOTER_DISCOVER_WIDGETS_EVENT =
|
|
32
|
+
"pi-fancy-footer:discover-widgets";
|
|
33
|
+
const FANCY_FOOTER_REQUEST_WIDGET_REFRESH_EVENT =
|
|
34
|
+
"pi-fancy-footer:request-widget-refresh";
|
|
35
|
+
|
|
36
|
+
function contributeFancyFooterWidget(
|
|
37
|
+
pi: ExtensionAPI,
|
|
38
|
+
widget: Record<string, unknown>,
|
|
39
|
+
): void {
|
|
40
|
+
pi.events.on(FANCY_FOOTER_DISCOVER_WIDGETS_EVENT, (payload) => {
|
|
41
|
+
const request = payload as
|
|
42
|
+
| { registerWidget?: (widget: Record<string, unknown>) => void }
|
|
43
|
+
| undefined;
|
|
44
|
+
if (typeof request?.registerWidget !== "function") return;
|
|
45
|
+
request.registerWidget(widget);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function requestFancyFooterRefresh(pi: ExtensionAPI): void {
|
|
50
|
+
pi.events.emit(FANCY_FOOTER_REQUEST_WIDGET_REFRESH_EVENT, {});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function warnOnce(message: string, lastWarning: string): string {
|
|
54
|
+
if (message !== lastWarning) console.warn(`pi-service-tier: ${message}`);
|
|
55
|
+
return message;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function formatModel(model: ExtensionContext["model"]): string {
|
|
59
|
+
if (!model) return "the current model";
|
|
60
|
+
return `${model.provider}/${model.id}`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function notifyConfigWriteError(ctx: ExtensionCommandContext, error: unknown): void {
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
ctx.ui.notify(`Failed to save service tier config: ${message}`, "error");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function createServiceTierSettingItems(
|
|
69
|
+
config: ServiceTierConfigSnapshot,
|
|
70
|
+
model: ExtensionContext["model"],
|
|
71
|
+
): SettingItem[] {
|
|
72
|
+
return createServiceTierSections(config, model).flatMap((section) =>
|
|
73
|
+
section.items.map((item) => ({
|
|
74
|
+
id: item.id,
|
|
75
|
+
label: section.id === "current" ? `Current: ${item.label}` : item.label,
|
|
76
|
+
description: item.description,
|
|
77
|
+
currentValue: item.currentValue,
|
|
78
|
+
values: [...item.values],
|
|
79
|
+
})),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default function (pi: ExtensionAPI) {
|
|
84
|
+
let currentServiceTier: ServiceTierName | "" = "";
|
|
85
|
+
let lastConfigWarning = "";
|
|
86
|
+
|
|
87
|
+
const loadConfigOrDefault = (): ServiceTierConfigSnapshot => {
|
|
88
|
+
try {
|
|
89
|
+
const config = loadServiceTierConfig();
|
|
90
|
+
lastConfigWarning = "";
|
|
91
|
+
return config;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
94
|
+
lastConfigWarning = warnOnce(message, lastConfigWarning);
|
|
95
|
+
return { ...DEFAULT_SERVICE_TIER_CONFIG };
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const loadConfigForCommand = (
|
|
100
|
+
ctx: ExtensionCommandContext,
|
|
101
|
+
): ServiceTierConfigSnapshot | undefined => {
|
|
102
|
+
try {
|
|
103
|
+
const config = loadServiceTierConfig();
|
|
104
|
+
lastConfigWarning = "";
|
|
105
|
+
return config;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
108
|
+
lastConfigWarning = warnOnce(message, lastConfigWarning);
|
|
109
|
+
ctx.ui.notify(`Invalid service tier config: ${message}`, "error");
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const refreshServiceTier = (
|
|
115
|
+
ctx: ExtensionContext,
|
|
116
|
+
config = loadConfigOrDefault(),
|
|
117
|
+
forceRefresh = false,
|
|
118
|
+
): ServiceTierName | "" => {
|
|
119
|
+
const nextServiceTier = resolveEffectiveServiceTier(config, ctx.model);
|
|
120
|
+
if (currentServiceTier === nextServiceTier && !forceRefresh) {
|
|
121
|
+
return currentServiceTier;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
currentServiceTier = nextServiceTier;
|
|
125
|
+
requestFancyFooterRefresh(pi);
|
|
126
|
+
return currentServiceTier;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const writeAndRefresh = (
|
|
130
|
+
ctx: ExtensionCommandContext,
|
|
131
|
+
config: ServiceTierConfigSnapshot,
|
|
132
|
+
): boolean => {
|
|
133
|
+
try {
|
|
134
|
+
writeServiceTierConfigSnapshot(config);
|
|
135
|
+
refreshServiceTier(ctx, config, true);
|
|
136
|
+
return true;
|
|
137
|
+
} catch (error) {
|
|
138
|
+
notifyConfigWriteError(ctx, error);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
contributeFancyFooterWidget(pi, {
|
|
144
|
+
id: SERVICE_TIER_WIDGET_ID,
|
|
145
|
+
label: "Service tier",
|
|
146
|
+
description: "Shows the configured provider service tier when active.",
|
|
147
|
+
defaults: {
|
|
148
|
+
row: 1,
|
|
149
|
+
position: 8,
|
|
150
|
+
align: "right",
|
|
151
|
+
fill: "none",
|
|
152
|
+
},
|
|
153
|
+
icon: {
|
|
154
|
+
nerd: "",
|
|
155
|
+
emoji: "⚡",
|
|
156
|
+
unicode: "⚡",
|
|
157
|
+
ascii: "!",
|
|
158
|
+
},
|
|
159
|
+
visible: () => currentServiceTier !== "",
|
|
160
|
+
renderText: () => currentServiceTier,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
pi.registerCommand("fast", {
|
|
164
|
+
description: "Toggle fast service tier for the current model provider.",
|
|
165
|
+
handler: async (_args, ctx) => {
|
|
166
|
+
const config = loadConfigForCommand(ctx);
|
|
167
|
+
if (!config) return;
|
|
168
|
+
|
|
169
|
+
const result = toggleFastServiceTier(config, ctx.model);
|
|
170
|
+
if (!result) {
|
|
171
|
+
ctx.ui.notify(
|
|
172
|
+
`Service tier is not supported for ${formatModel(ctx.model)}.`,
|
|
173
|
+
"warning",
|
|
174
|
+
);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!writeAndRefresh(ctx, result.config)) return;
|
|
179
|
+
|
|
180
|
+
const providerLabel =
|
|
181
|
+
SERVICE_TIER_PROVIDER_DEFINITIONS[result.provider].label;
|
|
182
|
+
ctx.ui.notify(
|
|
183
|
+
`${providerLabel} service tier: ${result.serviceTier || "off"}`,
|
|
184
|
+
"info",
|
|
185
|
+
);
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
pi.registerCommand("service-tier", {
|
|
190
|
+
description: "Configure provider service tiers.",
|
|
191
|
+
handler: async (_args, ctx) => {
|
|
192
|
+
if (!ctx.hasUI) {
|
|
193
|
+
ctx.ui.notify("/service-tier requires interactive UI mode", "warning");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const config = loadConfigForCommand(ctx);
|
|
198
|
+
if (!config) return;
|
|
199
|
+
|
|
200
|
+
await ctx.ui.custom((tui, theme, _keybindings, done) => {
|
|
201
|
+
let currentConfig = config;
|
|
202
|
+
const items = createServiceTierSettingItems(currentConfig, ctx.model);
|
|
203
|
+
const container = new Container();
|
|
204
|
+
container.addChild(
|
|
205
|
+
new Text(
|
|
206
|
+
`${theme.fg("accent", theme.bold("Service Tier"))}\n${theme.fg(
|
|
207
|
+
"dim",
|
|
208
|
+
getServiceTierConfigPath(),
|
|
209
|
+
)}`,
|
|
210
|
+
0,
|
|
211
|
+
0,
|
|
212
|
+
),
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
let settingsList: SettingsList;
|
|
216
|
+
settingsList = new SettingsList(
|
|
217
|
+
items,
|
|
218
|
+
Math.min(items.length, 12),
|
|
219
|
+
getSettingsListTheme(),
|
|
220
|
+
(id, newValue) => {
|
|
221
|
+
if (!isServiceTierProvider(id)) return;
|
|
222
|
+
|
|
223
|
+
const previousValue = currentConfig[id] ?? "off";
|
|
224
|
+
const nextConfig = setProviderServiceTier(
|
|
225
|
+
currentConfig,
|
|
226
|
+
id,
|
|
227
|
+
newValue === "off" ? "" : (newValue as ServiceTierName),
|
|
228
|
+
);
|
|
229
|
+
if (!writeAndRefresh(ctx, nextConfig)) {
|
|
230
|
+
settingsList.updateValue(id, previousValue);
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
currentConfig = nextConfig;
|
|
235
|
+
},
|
|
236
|
+
() => done(undefined),
|
|
237
|
+
);
|
|
238
|
+
container.addChild(settingsList);
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
render: (width: number) => container.render(width),
|
|
242
|
+
invalidate: () => container.invalidate(),
|
|
243
|
+
handleInput(data: string) {
|
|
244
|
+
settingsList.handleInput(data);
|
|
245
|
+
tui.requestRender();
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
253
|
+
refreshServiceTier(ctx);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
pi.on("model_select", async (_event, ctx) => {
|
|
257
|
+
refreshServiceTier(ctx);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
pi.on("before_provider_request", async (event, ctx) => {
|
|
261
|
+
const config = loadConfigOrDefault();
|
|
262
|
+
refreshServiceTier(ctx, config);
|
|
263
|
+
return applyServiceTierToPayload(event.payload, config, ctx.model);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
pi.on("session_shutdown", async () => {
|
|
267
|
+
if (!currentServiceTier) return;
|
|
268
|
+
currentServiceTier = "";
|
|
269
|
+
requestFancyFooterRefresh(pi);
|
|
270
|
+
});
|
|
271
|
+
}
|
package/shared.ts
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { Compile } from "typebox/compile";
|
|
6
|
+
|
|
7
|
+
export const SERVICE_TIER_CONFIG_FILE = "service-tier.json";
|
|
8
|
+
|
|
9
|
+
export const SERVICE_TIER_PROVIDERS = [
|
|
10
|
+
"openai",
|
|
11
|
+
"openai-codex",
|
|
12
|
+
"anthropic",
|
|
13
|
+
"google",
|
|
14
|
+
"google-vertex",
|
|
15
|
+
] as const;
|
|
16
|
+
export type ServiceTierProvider = (typeof SERVICE_TIER_PROVIDERS)[number];
|
|
17
|
+
|
|
18
|
+
export type ServiceTierName = "flex" | "priority" | "standard";
|
|
19
|
+
export type ServiceTierConfigSnapshot = Partial<
|
|
20
|
+
Record<ServiceTierProvider, ServiceTierName>
|
|
21
|
+
>;
|
|
22
|
+
|
|
23
|
+
export interface ServiceTier {
|
|
24
|
+
name: ServiceTierName;
|
|
25
|
+
value: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const DEFAULT_SERVICE_TIER_CONFIG: ServiceTierConfigSnapshot = {};
|
|
29
|
+
|
|
30
|
+
export interface ServiceTierProviderDefinition {
|
|
31
|
+
label: string;
|
|
32
|
+
api: string;
|
|
33
|
+
tiers: readonly ServiceTier[];
|
|
34
|
+
fastTier?: ServiceTierName;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function tier(name: ServiceTierName, value = name): ServiceTier {
|
|
38
|
+
return { name, value };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const SERVICE_TIER_PROVIDER_DEFINITIONS: Record<
|
|
42
|
+
ServiceTierProvider,
|
|
43
|
+
ServiceTierProviderDefinition
|
|
44
|
+
> = {
|
|
45
|
+
openai: {
|
|
46
|
+
label: "OpenAI",
|
|
47
|
+
api: "openai-responses",
|
|
48
|
+
tiers: [tier("flex"), tier("priority")],
|
|
49
|
+
fastTier: "priority",
|
|
50
|
+
},
|
|
51
|
+
"openai-codex": {
|
|
52
|
+
label: "OpenAI Codex",
|
|
53
|
+
api: "openai-codex-responses",
|
|
54
|
+
tiers: [tier("flex"), tier("priority")],
|
|
55
|
+
fastTier: "priority",
|
|
56
|
+
},
|
|
57
|
+
anthropic: {
|
|
58
|
+
label: "Anthropic",
|
|
59
|
+
api: "anthropic-messages",
|
|
60
|
+
tiers: [tier("priority", "auto"), tier("standard", "standard_only")],
|
|
61
|
+
fastTier: "priority",
|
|
62
|
+
},
|
|
63
|
+
google: {
|
|
64
|
+
label: "Google Gemini",
|
|
65
|
+
api: "google-generative-ai",
|
|
66
|
+
tiers: [tier("flex"), tier("priority")],
|
|
67
|
+
fastTier: "priority",
|
|
68
|
+
},
|
|
69
|
+
"google-vertex": {
|
|
70
|
+
label: "Google Vertex AI",
|
|
71
|
+
api: "google-vertex",
|
|
72
|
+
tiers: [tier("flex"), tier("priority")],
|
|
73
|
+
fastTier: "priority",
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
function serviceTierNames(provider: ServiceTierProvider): ServiceTierName[] {
|
|
78
|
+
return SERVICE_TIER_PROVIDER_DEFINITIONS[provider].tiers.map(
|
|
79
|
+
(serviceTier) => serviceTier.name,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function literalUnion(values: readonly ServiceTierName[]) {
|
|
84
|
+
return Type.Union(values.map((value) => Type.Literal(value)));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const serviceTierConfigSchema = Type.Object(
|
|
88
|
+
{
|
|
89
|
+
openai: Type.Optional(literalUnion(serviceTierNames("openai"))),
|
|
90
|
+
"openai-codex": Type.Optional(
|
|
91
|
+
literalUnion(serviceTierNames("openai-codex")),
|
|
92
|
+
),
|
|
93
|
+
anthropic: Type.Optional(literalUnion(serviceTierNames("anthropic"))),
|
|
94
|
+
google: Type.Optional(literalUnion(serviceTierNames("google"))),
|
|
95
|
+
"google-vertex": Type.Optional(
|
|
96
|
+
literalUnion(serviceTierNames("google-vertex")),
|
|
97
|
+
),
|
|
98
|
+
},
|
|
99
|
+
{ additionalProperties: false },
|
|
100
|
+
);
|
|
101
|
+
const validateServiceTierConfig = Compile(serviceTierConfigSchema);
|
|
102
|
+
|
|
103
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
104
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function formatValidationErrors(filePath: string, value: unknown): string {
|
|
108
|
+
const errors = Array.from(validateServiceTierConfig.Errors(value));
|
|
109
|
+
const unknownKeys = isRecord(value)
|
|
110
|
+
? Object.keys(value).filter(
|
|
111
|
+
(key) =>
|
|
112
|
+
!(SERVICE_TIER_PROVIDERS as readonly string[]).includes(key),
|
|
113
|
+
)
|
|
114
|
+
: [];
|
|
115
|
+
if (unknownKeys.length > 0) {
|
|
116
|
+
return `Invalid ${filePath}: unknown setting ${unknownKeys.map((key) => `"${key}"`).join(", ")}`;
|
|
117
|
+
}
|
|
118
|
+
return `Invalid ${filePath}: ${errors
|
|
119
|
+
.map((error) => `${error.path || "/"} ${error.message}`)
|
|
120
|
+
.join(", ")}`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function getDefaultAgentDir(): string {
|
|
124
|
+
return process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function getServiceTierConfigPath(
|
|
128
|
+
agentDir = getDefaultAgentDir(),
|
|
129
|
+
): string {
|
|
130
|
+
return join(agentDir, SERVICE_TIER_CONFIG_FILE);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function isServiceTierProvider(
|
|
134
|
+
value: unknown,
|
|
135
|
+
): value is ServiceTierProvider {
|
|
136
|
+
return (
|
|
137
|
+
typeof value === "string" &&
|
|
138
|
+
(SERVICE_TIER_PROVIDERS as readonly string[]).includes(value)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function parseServiceTierConfigValue(
|
|
143
|
+
filePath: string,
|
|
144
|
+
value: unknown,
|
|
145
|
+
): ServiceTierConfigSnapshot {
|
|
146
|
+
if (value === undefined) return { ...DEFAULT_SERVICE_TIER_CONFIG };
|
|
147
|
+
if (!validateServiceTierConfig.Check(value)) {
|
|
148
|
+
throw new Error(formatValidationErrors(filePath, value));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const config = value as ServiceTierConfigSnapshot;
|
|
152
|
+
const result: ServiceTierConfigSnapshot = {};
|
|
153
|
+
for (const provider of SERVICE_TIER_PROVIDERS) {
|
|
154
|
+
const serviceTier = config[provider];
|
|
155
|
+
if (serviceTier !== undefined) result[provider] = serviceTier;
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function loadServiceTierConfig(
|
|
161
|
+
configPath = getServiceTierConfigPath(),
|
|
162
|
+
): ServiceTierConfigSnapshot {
|
|
163
|
+
if (!existsSync(configPath)) return { ...DEFAULT_SERVICE_TIER_CONFIG };
|
|
164
|
+
|
|
165
|
+
let parsed: unknown;
|
|
166
|
+
try {
|
|
167
|
+
parsed = JSON.parse(readFileSync(configPath, "utf8"));
|
|
168
|
+
} catch (error) {
|
|
169
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
170
|
+
throw new Error(`Failed to parse ${configPath}: ${message}`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return parseServiceTierConfigValue(configPath, parsed);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function writeServiceTierConfigSnapshot(
|
|
177
|
+
config: ServiceTierConfigSnapshot,
|
|
178
|
+
configPath = getServiceTierConfigPath(),
|
|
179
|
+
): void {
|
|
180
|
+
mkdirSync(dirname(configPath), { recursive: true });
|
|
181
|
+
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function modelSupportsServiceTier(
|
|
185
|
+
model: { api?: unknown; provider?: unknown } | undefined,
|
|
186
|
+
): boolean {
|
|
187
|
+
return getServiceTierProviderForModel(model) !== undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function getServiceTierProviderForModel(
|
|
191
|
+
model: { api?: unknown; provider?: unknown } | undefined,
|
|
192
|
+
): ServiceTierProvider | undefined {
|
|
193
|
+
if (!model || !isServiceTierProvider(model.provider)) return undefined;
|
|
194
|
+
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[model.provider];
|
|
195
|
+
return model.api === definition.api ? model.provider : undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function getConfiguredServiceTier(
|
|
199
|
+
config: ServiceTierConfigSnapshot,
|
|
200
|
+
provider: ServiceTierProvider | undefined,
|
|
201
|
+
): ServiceTierName | "" {
|
|
202
|
+
return provider ? (config[provider] ?? "") : "";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function resolveEffectiveServiceTier(
|
|
206
|
+
config: ServiceTierConfigSnapshot,
|
|
207
|
+
model: { api?: unknown; provider?: unknown } | undefined,
|
|
208
|
+
): ServiceTierName | "" {
|
|
209
|
+
return getConfiguredServiceTier(config, getServiceTierProviderForModel(model));
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function getTier(
|
|
213
|
+
provider: ServiceTierProvider,
|
|
214
|
+
serviceTierName: ServiceTierName,
|
|
215
|
+
): ServiceTier | undefined {
|
|
216
|
+
return SERVICE_TIER_PROVIDER_DEFINITIONS[provider].tiers.find(
|
|
217
|
+
(serviceTier) => serviceTier.name === serviceTierName,
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function applyPayloadServiceTier(
|
|
222
|
+
payload: Record<string, unknown>,
|
|
223
|
+
provider: ServiceTierProvider,
|
|
224
|
+
value: string,
|
|
225
|
+
): Record<string, unknown> {
|
|
226
|
+
if (provider === "google" || provider === "google-vertex") {
|
|
227
|
+
return {
|
|
228
|
+
...payload,
|
|
229
|
+
config: {
|
|
230
|
+
...(isRecord(payload.config) ? payload.config : {}),
|
|
231
|
+
serviceTier: value,
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return { ...payload, service_tier: value };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function applyServiceTierToPayload(
|
|
240
|
+
payload: unknown,
|
|
241
|
+
config: ServiceTierConfigSnapshot,
|
|
242
|
+
model: { api?: unknown; provider?: unknown } | undefined,
|
|
243
|
+
): unknown | undefined {
|
|
244
|
+
const provider = getServiceTierProviderForModel(model);
|
|
245
|
+
const serviceTierName = getConfiguredServiceTier(config, provider);
|
|
246
|
+
if (!provider || !serviceTierName || !isRecord(payload)) return undefined;
|
|
247
|
+
|
|
248
|
+
const serviceTier = getTier(provider, serviceTierName);
|
|
249
|
+
if (!serviceTier) return undefined;
|
|
250
|
+
|
|
251
|
+
return applyPayloadServiceTier(payload, provider, serviceTier.value);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface FastToggleResult {
|
|
255
|
+
config: ServiceTierConfigSnapshot;
|
|
256
|
+
provider: ServiceTierProvider;
|
|
257
|
+
serviceTier: ServiceTierName | "";
|
|
258
|
+
fast: boolean;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function toggleFastServiceTier(
|
|
262
|
+
config: ServiceTierConfigSnapshot,
|
|
263
|
+
model: { api?: unknown; provider?: unknown } | undefined,
|
|
264
|
+
): FastToggleResult | undefined {
|
|
265
|
+
const provider = getServiceTierProviderForModel(model);
|
|
266
|
+
if (!provider) return undefined;
|
|
267
|
+
|
|
268
|
+
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[provider];
|
|
269
|
+
if (!definition.fastTier) return undefined;
|
|
270
|
+
|
|
271
|
+
const current = getConfiguredServiceTier(config, provider);
|
|
272
|
+
const fast = current !== definition.fastTier;
|
|
273
|
+
const serviceTier = fast ? definition.fastTier : "";
|
|
274
|
+
return {
|
|
275
|
+
config: setProviderServiceTier(config, provider, serviceTier),
|
|
276
|
+
provider,
|
|
277
|
+
serviceTier,
|
|
278
|
+
fast,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function setProviderServiceTier(
|
|
283
|
+
config: ServiceTierConfigSnapshot,
|
|
284
|
+
provider: ServiceTierProvider,
|
|
285
|
+
value: ServiceTierName | "",
|
|
286
|
+
): ServiceTierConfigSnapshot {
|
|
287
|
+
const next = { ...config };
|
|
288
|
+
if (value === "") delete next[provider];
|
|
289
|
+
else next[provider] = value;
|
|
290
|
+
return next;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export type ServiceTierSectionId = "current" | "providers";
|
|
294
|
+
|
|
295
|
+
export interface ServiceTierSectionItem {
|
|
296
|
+
id: ServiceTierProvider;
|
|
297
|
+
label: string;
|
|
298
|
+
currentValue: ServiceTierName | "off";
|
|
299
|
+
values: readonly (ServiceTierName | "off")[];
|
|
300
|
+
description: string;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export interface ServiceTierSection {
|
|
304
|
+
id: ServiceTierSectionId;
|
|
305
|
+
title: string;
|
|
306
|
+
items: ServiceTierSectionItem[];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function buildSectionItem(
|
|
310
|
+
provider: ServiceTierProvider,
|
|
311
|
+
config: ServiceTierConfigSnapshot,
|
|
312
|
+
currentModel?: { id?: unknown; name?: unknown },
|
|
313
|
+
): ServiceTierSectionItem {
|
|
314
|
+
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[provider];
|
|
315
|
+
const modelLabel =
|
|
316
|
+
typeof currentModel?.name === "string" && currentModel.name
|
|
317
|
+
? currentModel.name
|
|
318
|
+
: typeof currentModel?.id === "string" && currentModel.id
|
|
319
|
+
? currentModel.id
|
|
320
|
+
: "";
|
|
321
|
+
const label = modelLabel
|
|
322
|
+
? `${definition.label} (${modelLabel})`
|
|
323
|
+
: definition.label;
|
|
324
|
+
const configuredValue = getConfiguredServiceTier(config, provider);
|
|
325
|
+
const currentValue = configuredValue || "off";
|
|
326
|
+
const tierNames = definition.tiers.map((serviceTier) => serviceTier.name);
|
|
327
|
+
return {
|
|
328
|
+
id: provider,
|
|
329
|
+
label,
|
|
330
|
+
currentValue,
|
|
331
|
+
values: ["off", ...tierNames],
|
|
332
|
+
description: `${definition.label} supports ${tierNames.join(", ")}.`,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export function createServiceTierSections(
|
|
337
|
+
config: ServiceTierConfigSnapshot,
|
|
338
|
+
model: { api?: unknown; provider?: unknown; id?: unknown; name?: unknown } | undefined,
|
|
339
|
+
): ServiceTierSection[] {
|
|
340
|
+
const currentProvider = getServiceTierProviderForModel(model);
|
|
341
|
+
const otherProviders = SERVICE_TIER_PROVIDERS.filter(
|
|
342
|
+
(provider) => provider !== currentProvider,
|
|
343
|
+
);
|
|
344
|
+
const sections: ServiceTierSection[] = [];
|
|
345
|
+
|
|
346
|
+
if (currentProvider) {
|
|
347
|
+
sections.push({
|
|
348
|
+
id: "current",
|
|
349
|
+
title: "Current Model",
|
|
350
|
+
items: [buildSectionItem(currentProvider, config, model)],
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
sections.push({
|
|
355
|
+
id: "providers",
|
|
356
|
+
title: currentProvider ? "Other Providers" : "Providers",
|
|
357
|
+
items: otherProviders.map((provider) => buildSectionItem(provider, config)),
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
return sections;
|
|
361
|
+
}
|