pi-service-tier 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -23
- package/config.ts +41 -0
- package/domain.ts +40 -14
- package/package.json +1 -1
- package/service-tier.ts +116 -42
package/README.md
CHANGED
|
@@ -13,44 +13,74 @@ pi install npm:pi-service-tier
|
|
|
13
13
|
|
|
14
14
|
- Adds service tier parameters to supported provider requests when a tier is
|
|
15
15
|
configured
|
|
16
|
-
- Adds `/fast` to toggle the current model provider between
|
|
17
|
-
for this session only
|
|
16
|
+
- Adds `/fast` and `/ultrafast` to toggle the current model provider between
|
|
17
|
+
the selected speed and off for this session only
|
|
18
18
|
- Adds `/service-tier` to configure global defaults for all supported providers
|
|
19
19
|
from an interactive modal
|
|
20
20
|
- Adds an optional service tier widget when `pi-fancy-footer` is installed
|
|
21
21
|
|
|
22
22
|
## 🚀 Commands
|
|
23
23
|
|
|
24
|
-
- `/fast`: toggles the current model provider between its fast tier and off
|
|
24
|
+
- `/fast`: toggles the current model's provider between its fast tier and off
|
|
25
25
|
**in this session**, without changing other sessions or your global defaults.
|
|
26
26
|
The supported providers all use `priority` as the fast tier.
|
|
27
27
|
|
|
28
|
+
- `/ultrafast`: toggles between `ultrafast` and off **in this session** for
|
|
29
|
+
`openai` and `openai-codex` (including additional accounts on their official
|
|
30
|
+
endpoints). It uses the same provider override as `/fast`, so selecting a
|
|
31
|
+
different speed replaces the active tier.
|
|
32
|
+
|
|
28
33
|
- `/service-tier`: opens an interactive editor for **global defaults**. The
|
|
29
|
-
current model provider appears first, followed by the remaining supported
|
|
34
|
+
current model's provider appears first, followed by the remaining supported
|
|
30
35
|
providers. Press Enter or Space to cycle through `off` and the provider-specific
|
|
31
36
|
tiers. Session overrides take precedence over these defaults.
|
|
32
37
|
|
|
33
38
|
### Session scope
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
mode off. Each provider has
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
To speed up a session, run `/fast` or `/ultrafast`. Run the same command again to
|
|
41
|
+
turn that mode off. Each supported provider has one override (shared with any
|
|
42
|
+
additional accounts for that provider), so switching models preserves your
|
|
43
|
+
choices. Turning either mode off disables this extension's tier injection, even
|
|
44
|
+
if a global tier is configured. The command leaves other request parameters
|
|
45
|
+
unchanged.
|
|
46
|
+
|
|
47
|
+
| Current tier | `/fast` | `/ultrafast` |
|
|
48
|
+
| ------------- | ---------- | ------------ |
|
|
49
|
+
| Off or `flex` | `priority` | `ultrafast` |
|
|
50
|
+
| `priority` | Off | `ultrafast` |
|
|
51
|
+
| `ultrafast` | `priority` | Off |
|
|
52
|
+
|
|
53
|
+
Both commands remain listed on all providers. Running them on an unsupported
|
|
54
|
+
model produces a warning without changing settings.
|
|
40
55
|
|
|
41
56
|
Overrides are saved with the session and restored on resume or extension reload.
|
|
42
57
|
They follow the active conversation branch: `/tree` restores the choices at the
|
|
43
58
|
selected point, and `/fork` or `/clone` inherits the choices on the copied branch.
|
|
44
59
|
Later toggles in a fork do not affect its parent. `/new` starts without overrides
|
|
45
|
-
and uses your
|
|
60
|
+
and uses your process defaults (`--service-tier` if passed, otherwise your global
|
|
61
|
+
defaults).
|
|
62
|
+
|
|
63
|
+
### Ultrafast availability
|
|
64
|
+
|
|
65
|
+
The extension sends `service_tier: "ultrafast"` for models using the `openai`
|
|
66
|
+
Responses API or `openai-codex`. OpenAI determines model and account eligibility.
|
|
67
|
+
Pi reports provider errors without automatically selecting another tier.
|
|
68
|
+
|
|
69
|
+
[OpenAI's API documentation] describes current model availability, rate limits,
|
|
70
|
+
and pricing. [Codex subscription eligibility] differs from API-key access and
|
|
71
|
+
may require workspace permissions.
|
|
72
|
+
|
|
73
|
+
[OpenAI's API documentation]: https://developers.openai.com/api/docs/guides/ultrafast-mode
|
|
74
|
+
[Codex subscription eligibility]: https://learn.chatgpt.com/docs/agent-configuration/speed
|
|
46
75
|
|
|
47
76
|
### Child agents
|
|
48
77
|
|
|
49
78
|
With [pi-agents](https://github.com/mavam/pi-agents) 0.36.0 or later, which
|
|
50
79
|
provides [agent extension hooks](https://github.com/mavam/pi-agents/blob/main/docs/hooks.md),
|
|
51
80
|
new agents capture your effective service-tier settings, including session
|
|
52
|
-
`/fast` overrides. Each child applies the settings for its own
|
|
53
|
-
when it uses a different model from the
|
|
81
|
+
`/fast` and `/ultrafast` overrides. Each child applies the settings for its own
|
|
82
|
+
provider, even when it uses a different model or additional account from the
|
|
83
|
+
parent.
|
|
54
84
|
|
|
55
85
|
For example, enable `/fast`, then ask Pi to spawn an agent. That agent keeps
|
|
56
86
|
its captured settings if you toggle `/fast` again. Graph nodes capture when
|
|
@@ -76,27 +106,63 @@ Run `/service-tier` or create `~/.pi/agent/service-tier.json` to set global defa
|
|
|
76
106
|
}
|
|
77
107
|
```
|
|
78
108
|
|
|
109
|
+
### Per-process defaults
|
|
110
|
+
|
|
111
|
+
`--service-tier` replaces `~/.pi/agent/service-tier.json` for one Pi process,
|
|
112
|
+
for example in a scripted `pi -p` run or a subprocess launcher:
|
|
113
|
+
|
|
114
|
+
```sh
|
|
115
|
+
pi --service-tier=off
|
|
116
|
+
pi --service-tier=openai-codex:priority,anthropic:standard
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
`off` disables every provider. Otherwise, list `provider:tier` pairs; any
|
|
120
|
+
omitted provider stays off instead of inheriting from `service-tier.json`.
|
|
121
|
+
Session `/fast` and `/ultrafast` overrides still take precedence and are saved
|
|
122
|
+
without the flag's values, so reopening the session without `--service-tier`
|
|
123
|
+
uses `service-tier.json` again. Agents created with `pi-agents` capture these
|
|
124
|
+
process defaults too. An invalid value warns once, disables all tiers for the
|
|
125
|
+
process, and blocks `/fast` and `/ultrafast` toggles.
|
|
126
|
+
|
|
79
127
|
### Supported providers
|
|
80
128
|
|
|
81
|
-
| Provider | Tiers
|
|
82
|
-
| --------------- |
|
|
83
|
-
| `openai` | `flex`, `priority`
|
|
84
|
-
| `openai-codex` | `flex`, `priority`
|
|
85
|
-
| `anthropic` | `priority`, `standard`
|
|
86
|
-
| `google` | `flex`, `priority`
|
|
87
|
-
| `google-vertex` | `flex`, `priority`
|
|
129
|
+
| Provider | Tiers | Fast tier |
|
|
130
|
+
| --------------- | ------------------------------- | ---------- |
|
|
131
|
+
| `openai` | `flex`, `priority`, `ultrafast` | `priority` |
|
|
132
|
+
| `openai-codex` | `flex`, `priority`, `ultrafast` | `priority` |
|
|
133
|
+
| `anthropic` | `priority`, `standard` | `priority` |
|
|
134
|
+
| `google` | `flex`, `priority` | `priority` |
|
|
135
|
+
| `google-vertex` | `flex`, `priority` | `priority` |
|
|
88
136
|
|
|
89
137
|
To turn a provider off by default, omit its key. Only the values listed above are
|
|
90
|
-
accepted. Providers without a session override continue to pick up global changes
|
|
138
|
+
accepted. Providers without a session override continue to pick up global changes
|
|
139
|
+
unless the process was started with `--service-tier`.
|
|
91
140
|
Batch APIs are separate asynchronous APIs and are not configured by this
|
|
92
141
|
extension.
|
|
93
142
|
|
|
143
|
+
### Additional accounts
|
|
144
|
+
|
|
145
|
+
Accounts registered under another provider name, such as
|
|
146
|
+
[pi-multi-pass](https://github.com/hjanuschka/pi-multi-pass)'s `anthropic-2` or
|
|
147
|
+
`openai-codex-2`, share the service-tier settings of the built-in provider whose
|
|
148
|
+
API and official endpoint they use. `/fast`, `/ultrafast`, `/service-tier`,
|
|
149
|
+
child agents, and the footer widget all share that provider's setting rather
|
|
150
|
+
than storing a separate entry per account.
|
|
151
|
+
|
|
152
|
+
Account matching requires the exact API and `baseUrl` of Pi's built-in provider
|
|
153
|
+
(for example `https://api.anthropic.com` with no trailing slash or port, or
|
|
154
|
+
`https://{location}-aiplatform.googleapis.com` for `google-vertex`). Other
|
|
155
|
+
spellings and third-party proxies speaking a supported API are not matched and
|
|
156
|
+
receive no service tier.
|
|
157
|
+
|
|
94
158
|
## 🧩 Footer widget
|
|
95
159
|
|
|
96
160
|
When [pi-fancy-footer](https://github.com/mavam/pi-fancy-footer) is installed,
|
|
97
|
-
the widget appears only when the active model
|
|
161
|
+
the widget appears only when the active model resolves to a supported provider
|
|
98
162
|
and that provider has an effective tier after applying session overrides. It shows
|
|
99
|
-
a single `⚡`
|
|
163
|
+
`⚡⚡` for ultrafast or a single `⚡` for another active tier, without the tier
|
|
164
|
+
name. The bolts indicate the requested tier; they do not confirm which tier the
|
|
165
|
+
provider used.
|
|
100
166
|
|
|
101
167
|
The widget id is `pi-service-tier.service-tier`. It uses the current
|
|
102
168
|
`pi-fancy-footer` event protocol, with row `1`, position `8`, right alignment,
|
|
@@ -110,7 +176,9 @@ when the footer announces that it is ready.
|
|
|
110
176
|
injects the tier into the provider request payload, but pi's OpenAI Codex cost
|
|
111
177
|
calculation reads the requested tier from provider options. Until pi exposes a
|
|
112
178
|
first-class extension path for that option, displayed usage costs can omit
|
|
113
|
-
flex or priority multipliers.
|
|
179
|
+
flex or priority multipliers. Ultrafast pricing is also not represented by
|
|
180
|
+
Pi's current pricing logic. Do not use displayed costs to estimate ultrafast
|
|
181
|
+
charges.
|
|
114
182
|
|
|
115
183
|
## 📄 License
|
|
116
184
|
|
package/config.ts
CHANGED
|
@@ -9,6 +9,8 @@ import {
|
|
|
9
9
|
SERVICE_TIER_PROVIDERS,
|
|
10
10
|
SERVICE_TIER_PROVIDER_DEFINITIONS,
|
|
11
11
|
isRecord,
|
|
12
|
+
isServiceTierName,
|
|
13
|
+
isServiceTierProvider,
|
|
12
14
|
type ServiceTierConfigSnapshot,
|
|
13
15
|
type ServiceTierName,
|
|
14
16
|
type ServiceTierProvider,
|
|
@@ -109,3 +111,42 @@ export function writeServiceTierConfigSnapshot(
|
|
|
109
111
|
mkdirSync(dirname(configPath), { recursive: true });
|
|
110
112
|
writeFileSync(configPath, `${JSON.stringify(config, null, 2)}\n`);
|
|
111
113
|
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Parse `--service-tier`: `off` leaves every provider off; otherwise a
|
|
117
|
+
* comma-separated list of `provider:tier` pairs replacing global defaults.
|
|
118
|
+
*/
|
|
119
|
+
export function parseServiceTierFlag(value: string): ServiceTierConfigSnapshot {
|
|
120
|
+
const trimmed = value.trim();
|
|
121
|
+
if (trimmed === "off") return { ...DEFAULT_SERVICE_TIER_CONFIG };
|
|
122
|
+
|
|
123
|
+
const config: ServiceTierConfigSnapshot = {};
|
|
124
|
+
for (const rawItem of trimmed.split(",")) {
|
|
125
|
+
const item = rawItem.trim();
|
|
126
|
+
const parts = item.split(":");
|
|
127
|
+
if (parts.length !== 2) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
`--service-tier: expected off or provider:tier pairs, got "${item}"`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const provider = parts[0].trim();
|
|
133
|
+
const serviceTier = parts[1].trim();
|
|
134
|
+
if (!isServiceTierProvider(provider)) {
|
|
135
|
+
throw new Error(`--service-tier: unknown provider "${provider}"`);
|
|
136
|
+
}
|
|
137
|
+
if (provider in config) {
|
|
138
|
+
throw new Error(`--service-tier: ${provider} is set more than once`);
|
|
139
|
+
}
|
|
140
|
+
if (!isServiceTierName(provider, serviceTier)) {
|
|
141
|
+
const hint =
|
|
142
|
+
serviceTier === "off"
|
|
143
|
+
? " (omit providers to leave them off, or use --service-tier=off)"
|
|
144
|
+
: "";
|
|
145
|
+
throw new Error(
|
|
146
|
+
`--service-tier: ${provider} has no service tier "${serviceTier}"${hint}`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
config[provider] = serviceTier;
|
|
150
|
+
}
|
|
151
|
+
return config;
|
|
152
|
+
}
|
package/domain.ts
CHANGED
|
@@ -7,7 +7,8 @@ export const SERVICE_TIER_PROVIDERS = [
|
|
|
7
7
|
] as const;
|
|
8
8
|
export type ServiceTierProvider = (typeof SERVICE_TIER_PROVIDERS)[number];
|
|
9
9
|
|
|
10
|
-
export type ServiceTierName = "flex" | "priority" | "standard";
|
|
10
|
+
export type ServiceTierName = "flex" | "priority" | "standard" | "ultrafast";
|
|
11
|
+
export type ServiceTierSpeed = "fast" | "ultrafast";
|
|
11
12
|
/** An omitted provider leaves the provider's default behavior unchanged. */
|
|
12
13
|
export type ServiceTierConfigSnapshot = Partial<
|
|
13
14
|
Record<ServiceTierProvider, ServiceTierName>
|
|
@@ -24,6 +25,7 @@ export type ServiceTierSettings = Readonly<
|
|
|
24
25
|
export interface ServiceTierModel {
|
|
25
26
|
readonly provider?: unknown;
|
|
26
27
|
readonly api?: unknown;
|
|
28
|
+
readonly baseUrl?: unknown;
|
|
27
29
|
readonly id?: unknown;
|
|
28
30
|
readonly name?: unknown;
|
|
29
31
|
}
|
|
@@ -38,8 +40,11 @@ export const DEFAULT_SERVICE_TIER_CONFIG: ServiceTierConfigSnapshot = {};
|
|
|
38
40
|
export interface ServiceTierProviderDefinition {
|
|
39
41
|
label: string;
|
|
40
42
|
api: string;
|
|
43
|
+
/** Exact built-in endpoint; matched literally with `api` for account aliases. */
|
|
44
|
+
baseUrl: string;
|
|
41
45
|
tiers: readonly ServiceTier[];
|
|
42
46
|
fastTier?: ServiceTierName;
|
|
47
|
+
ultrafastTier?: ServiceTierName;
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
function tier(name: ServiceTierName, value: string = name): ServiceTier {
|
|
@@ -53,30 +58,37 @@ export const SERVICE_TIER_PROVIDER_DEFINITIONS: Record<
|
|
|
53
58
|
openai: {
|
|
54
59
|
label: "OpenAI",
|
|
55
60
|
api: "openai-responses",
|
|
56
|
-
|
|
61
|
+
baseUrl: "https://api.openai.com/v1",
|
|
62
|
+
tiers: [tier("flex"), tier("priority"), tier("ultrafast")],
|
|
57
63
|
fastTier: "priority",
|
|
64
|
+
ultrafastTier: "ultrafast",
|
|
58
65
|
},
|
|
59
66
|
"openai-codex": {
|
|
60
67
|
label: "OpenAI Codex",
|
|
61
68
|
api: "openai-codex-responses",
|
|
62
|
-
|
|
69
|
+
baseUrl: "https://chatgpt.com/backend-api",
|
|
70
|
+
tiers: [tier("flex"), tier("priority"), tier("ultrafast")],
|
|
63
71
|
fastTier: "priority",
|
|
72
|
+
ultrafastTier: "ultrafast",
|
|
64
73
|
},
|
|
65
74
|
anthropic: {
|
|
66
75
|
label: "Anthropic",
|
|
67
76
|
api: "anthropic-messages",
|
|
77
|
+
baseUrl: "https://api.anthropic.com",
|
|
68
78
|
tiers: [tier("priority", "auto"), tier("standard", "standard_only")],
|
|
69
79
|
fastTier: "priority",
|
|
70
80
|
},
|
|
71
81
|
google: {
|
|
72
82
|
label: "Google Gemini",
|
|
73
83
|
api: "google-generative-ai",
|
|
84
|
+
baseUrl: "https://generativelanguage.googleapis.com/v1beta",
|
|
74
85
|
tiers: [tier("flex"), tier("priority")],
|
|
75
86
|
fastTier: "priority",
|
|
76
87
|
},
|
|
77
88
|
"google-vertex": {
|
|
78
89
|
label: "Google Vertex AI",
|
|
79
90
|
api: "google-vertex",
|
|
91
|
+
baseUrl: "https://{location}-aiplatform.googleapis.com",
|
|
80
92
|
tiers: [tier("flex"), tier("priority")],
|
|
81
93
|
fastTier: "priority",
|
|
82
94
|
},
|
|
@@ -113,9 +125,19 @@ export function modelSupportsServiceTier(
|
|
|
113
125
|
export function getServiceTierProviderForModel(
|
|
114
126
|
model: ServiceTierModel | undefined,
|
|
115
127
|
): ServiceTierProvider | undefined {
|
|
116
|
-
if (!model
|
|
117
|
-
|
|
118
|
-
|
|
128
|
+
if (!model) return undefined;
|
|
129
|
+
if (isServiceTierProvider(model.provider)) {
|
|
130
|
+
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[model.provider];
|
|
131
|
+
return model.api === definition.api ? model.provider : undefined;
|
|
132
|
+
}
|
|
133
|
+
// Match additional accounts (e.g. `anthropic-2`) by exact API and built-in endpoint.
|
|
134
|
+
return SERVICE_TIER_PROVIDERS.find((provider) => {
|
|
135
|
+
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[provider];
|
|
136
|
+
return (
|
|
137
|
+
model.api === definition.api &&
|
|
138
|
+
model.baseUrl === definition.baseUrl
|
|
139
|
+
);
|
|
140
|
+
});
|
|
119
141
|
}
|
|
120
142
|
|
|
121
143
|
export function getConfiguredServiceTier(
|
|
@@ -174,29 +196,33 @@ export function applyServiceTierToPayload(
|
|
|
174
196
|
return applyPayloadServiceTier(payload, provider, serviceTier.value);
|
|
175
197
|
}
|
|
176
198
|
|
|
177
|
-
export interface
|
|
199
|
+
export interface ServiceTierToggleResult {
|
|
178
200
|
provider: ServiceTierProvider;
|
|
179
201
|
serviceTier: ServiceTierSelection;
|
|
180
|
-
|
|
202
|
+
enabled: boolean;
|
|
181
203
|
}
|
|
182
204
|
|
|
183
|
-
export function
|
|
205
|
+
export function toggleServiceTierSpeed(
|
|
184
206
|
config: ServiceTierSettings,
|
|
185
207
|
model: ServiceTierModel | undefined,
|
|
186
|
-
|
|
208
|
+
speed: ServiceTierSpeed = "fast",
|
|
209
|
+
): ServiceTierToggleResult | undefined {
|
|
187
210
|
const provider = getServiceTierProviderForModel(model);
|
|
188
211
|
if (!provider) return undefined;
|
|
189
212
|
|
|
190
213
|
const definition = SERVICE_TIER_PROVIDER_DEFINITIONS[provider];
|
|
191
|
-
|
|
214
|
+
const targetTier = speed === "ultrafast"
|
|
215
|
+
? definition.ultrafastTier
|
|
216
|
+
: definition.fastTier;
|
|
217
|
+
if (!targetTier) return undefined;
|
|
192
218
|
|
|
193
219
|
const current = getConfiguredServiceTier(config, provider);
|
|
194
|
-
const
|
|
195
|
-
const serviceTier =
|
|
220
|
+
const enabled = current !== targetTier;
|
|
221
|
+
const serviceTier = enabled ? targetTier : "";
|
|
196
222
|
return {
|
|
197
223
|
provider,
|
|
198
224
|
serviceTier,
|
|
199
|
-
|
|
225
|
+
enabled,
|
|
200
226
|
};
|
|
201
227
|
}
|
|
202
228
|
|
package/package.json
CHANGED
package/service-tier.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
isServiceTierProvider,
|
|
18
18
|
resolveEffectiveServiceTier,
|
|
19
19
|
setProviderServiceTier,
|
|
20
|
-
|
|
20
|
+
toggleServiceTierSpeed,
|
|
21
21
|
type ServiceTierSelection,
|
|
22
22
|
type ServiceTierSettings,
|
|
23
23
|
type ServiceTierConfigSnapshot,
|
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
import {
|
|
27
27
|
getServiceTierConfigPath,
|
|
28
28
|
loadServiceTierConfig,
|
|
29
|
+
parseServiceTierFlag,
|
|
29
30
|
writeServiceTierConfigSnapshot,
|
|
30
31
|
} from "./config.ts";
|
|
31
32
|
import { createServiceTierSections } from "./settings.ts";
|
|
@@ -55,6 +56,11 @@ function formatModel(model: ExtensionContext["model"]): string {
|
|
|
55
56
|
return `${model.provider}/${model.id}`;
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
function formatServiceTier(serviceTier: ServiceTierSelection): string {
|
|
60
|
+
if (!serviceTier) return "";
|
|
61
|
+
return serviceTier === "ultrafast" ? "⚡⚡" : "⚡";
|
|
62
|
+
}
|
|
63
|
+
|
|
58
64
|
function notifyConfigWriteError(ctx: ExtensionCommandContext, error: unknown): void {
|
|
59
65
|
const message = error instanceof Error ? error.message : String(error);
|
|
60
66
|
ctx.ui.notify(`Failed to save service tier config: ${message}`, "error");
|
|
@@ -87,8 +93,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
87
93
|
widget: {
|
|
88
94
|
id: SERVICE_TIER_WIDGET_ID,
|
|
89
95
|
label: "Service tier",
|
|
90
|
-
description: "Shows
|
|
91
|
-
content: { type: "text", text: currentServiceTier
|
|
96
|
+
description: "Shows two bolts for ultrafast or one for another active tier.",
|
|
97
|
+
content: { type: "text", text: formatServiceTier(currentServiceTier) },
|
|
92
98
|
icon: false,
|
|
93
99
|
layout: {
|
|
94
100
|
row: 1,
|
|
@@ -131,8 +137,55 @@ export default function (pi: ExtensionAPI) {
|
|
|
131
137
|
}
|
|
132
138
|
};
|
|
133
139
|
|
|
140
|
+
pi.registerFlag("service-tier", {
|
|
141
|
+
type: "string",
|
|
142
|
+
description:
|
|
143
|
+
"Replace global service tier defaults for this process: off, or provider:tier pairs such as openai-codex:priority,anthropic:standard.",
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
type ProcessDefaultsFlag =
|
|
147
|
+
| { active: false }
|
|
148
|
+
| { active: true; config: ServiceTierConfigSnapshot }
|
|
149
|
+
| { active: true; error: string };
|
|
150
|
+
|
|
151
|
+
// Flag values arrive after extension loading; resolve once on first use.
|
|
152
|
+
let processFlag: ProcessDefaultsFlag | undefined;
|
|
153
|
+
let flagWarned = false;
|
|
154
|
+
const getProcessFlag = (): ProcessDefaultsFlag => {
|
|
155
|
+
if (processFlag) return processFlag;
|
|
156
|
+
const value = pi.getFlag("service-tier");
|
|
157
|
+
if (typeof value !== "string") {
|
|
158
|
+
processFlag = { active: false };
|
|
159
|
+
return processFlag;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
processFlag = { active: true, config: parseServiceTierFlag(value) };
|
|
163
|
+
} catch (error) {
|
|
164
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
165
|
+
processFlag = { active: true, error: message };
|
|
166
|
+
}
|
|
167
|
+
return processFlag;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const warnInvalidFlagOnce = (message: string): void => {
|
|
171
|
+
if (flagWarned) return;
|
|
172
|
+
console.warn(`pi-service-tier: ${message}`);
|
|
173
|
+
flagWarned = true;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
/** Process defaults: `--service-tier` when set, otherwise `service-tier.json`. */
|
|
177
|
+
const loadProcessDefaults = (): ServiceTierConfigSnapshot => {
|
|
178
|
+
const flag = getProcessFlag();
|
|
179
|
+
if (!flag.active) return loadConfigOrDefault();
|
|
180
|
+
if ("error" in flag) {
|
|
181
|
+
warnInvalidFlagOnce(flag.error);
|
|
182
|
+
return { ...DEFAULT_SERVICE_TIER_CONFIG };
|
|
183
|
+
}
|
|
184
|
+
return flag.config;
|
|
185
|
+
};
|
|
186
|
+
|
|
134
187
|
// Register during loading, before either extension starts its session.
|
|
135
|
-
registerAgentHooks(pi.events,
|
|
188
|
+
registerAgentHooks(pi.events, loadProcessDefaults);
|
|
136
189
|
|
|
137
190
|
const loadConfigForCommand = (
|
|
138
191
|
ctx: ExtensionCommandContext,
|
|
@@ -149,8 +202,21 @@ export default function (pi: ExtensionAPI) {
|
|
|
149
202
|
}
|
|
150
203
|
};
|
|
151
204
|
|
|
205
|
+
const loadDefaultsForToggle = (
|
|
206
|
+
ctx: ExtensionCommandContext,
|
|
207
|
+
): ServiceTierConfigSnapshot | undefined => {
|
|
208
|
+
const flag = getProcessFlag();
|
|
209
|
+
if (!flag.active) return loadConfigForCommand(ctx);
|
|
210
|
+
if ("error" in flag) {
|
|
211
|
+
warnInvalidFlagOnce(flag.error);
|
|
212
|
+
ctx.ui.notify(`Invalid service tier flag: ${flag.error}`, "error");
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
return flag.config;
|
|
216
|
+
};
|
|
217
|
+
|
|
152
218
|
const effectiveSettings = (
|
|
153
|
-
defaults =
|
|
219
|
+
defaults = loadProcessDefaults(),
|
|
154
220
|
): ServiceTierSettings => mergeServiceTierSettings(defaults, sessionOverrides);
|
|
155
221
|
|
|
156
222
|
const refreshServiceTier = (
|
|
@@ -174,7 +240,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
174
240
|
): boolean => {
|
|
175
241
|
try {
|
|
176
242
|
writeServiceTierConfigSnapshot(config);
|
|
177
|
-
|
|
243
|
+
const defaults = getProcessFlag().active ? loadProcessDefaults() : config;
|
|
244
|
+
refreshServiceTier(ctx, effectiveSettings(defaults), true);
|
|
178
245
|
return true;
|
|
179
246
|
} catch (error) {
|
|
180
247
|
notifyConfigWriteError(ctx, error);
|
|
@@ -182,43 +249,46 @@ export default function (pi: ExtensionAPI) {
|
|
|
182
249
|
}
|
|
183
250
|
};
|
|
184
251
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
252
|
+
for (const speed of ["fast", "ultrafast"] as const) {
|
|
253
|
+
pi.registerCommand(speed, {
|
|
254
|
+
description: `Toggle ${speed} service tier for the current provider in this session.`,
|
|
255
|
+
handler: async (_args, ctx) => {
|
|
256
|
+
const defaults = loadDefaultsForToggle(ctx);
|
|
257
|
+
if (!defaults) return;
|
|
258
|
+
|
|
259
|
+
const result = toggleServiceTierSpeed(effectiveSettings(defaults), ctx.model, speed);
|
|
260
|
+
if (!result) {
|
|
261
|
+
const label = speed === "ultrafast" ? "Ultrafast service tier" : "Service tier";
|
|
262
|
+
ctx.ui.notify(
|
|
263
|
+
`${label} is not supported for ${formatModel(ctx.model)}.`,
|
|
264
|
+
"warning",
|
|
265
|
+
);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const state: ServiceTierSessionState = {
|
|
270
|
+
version: 1,
|
|
271
|
+
overrides: { ...sessionOverrides, [result.provider]: result.serviceTier },
|
|
272
|
+
};
|
|
273
|
+
try {
|
|
274
|
+
pi.appendEntry(SERVICE_TIER_SESSION_ENTRY, state);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
277
|
+
ctx.ui.notify(`Failed to save session service tier: ${message}`, "error");
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
sessionOverrides = state.overrides;
|
|
281
|
+
refreshServiceTier(ctx, effectiveSettings(defaults), true);
|
|
282
|
+
|
|
283
|
+
const providerLabel =
|
|
284
|
+
SERVICE_TIER_PROVIDER_DEFINITIONS[result.provider].label;
|
|
193
285
|
ctx.ui.notify(
|
|
194
|
-
|
|
195
|
-
"
|
|
286
|
+
`${providerLabel} service tier for this session: ${result.serviceTier || "off"}`,
|
|
287
|
+
"info",
|
|
196
288
|
);
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const state: ServiceTierSessionState = {
|
|
201
|
-
version: 1,
|
|
202
|
-
overrides: { ...sessionOverrides, [result.provider]: result.serviceTier },
|
|
203
|
-
};
|
|
204
|
-
try {
|
|
205
|
-
pi.appendEntry(SERVICE_TIER_SESSION_ENTRY, state);
|
|
206
|
-
} catch (error) {
|
|
207
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
208
|
-
ctx.ui.notify(`Failed to save session service tier: ${message}`, "error");
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
sessionOverrides = state.overrides;
|
|
212
|
-
refreshServiceTier(ctx, effectiveSettings(config), true);
|
|
213
|
-
|
|
214
|
-
const providerLabel =
|
|
215
|
-
SERVICE_TIER_PROVIDER_DEFINITIONS[result.provider].label;
|
|
216
|
-
ctx.ui.notify(
|
|
217
|
-
`${providerLabel} service tier for this session: ${result.serviceTier || "off"}`,
|
|
218
|
-
"info",
|
|
219
|
-
);
|
|
220
|
-
},
|
|
221
|
-
});
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
}
|
|
222
292
|
|
|
223
293
|
pi.registerCommand("service-tier", {
|
|
224
294
|
description: "Configure global provider service tier defaults.",
|
|
@@ -231,6 +301,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
231
301
|
const config = loadConfigForCommand(ctx);
|
|
232
302
|
if (!config) return;
|
|
233
303
|
|
|
304
|
+
const subtitle = getProcessFlag().active
|
|
305
|
+
? `${getServiceTierConfigPath()}\n--service-tier replaces global defaults in this process.\nSession /fast and /ultrafast overrides take precedence.`
|
|
306
|
+
: `${getServiceTierConfigPath()}\nSession /fast and /ultrafast overrides take precedence.`;
|
|
307
|
+
|
|
234
308
|
await ctx.ui.custom((tui, theme, _keybindings, done) => {
|
|
235
309
|
let currentConfig = config;
|
|
236
310
|
const items = createServiceTierSettingItems(currentConfig, ctx.model);
|
|
@@ -239,7 +313,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
239
313
|
new Text(
|
|
240
314
|
`${theme.fg("accent", theme.bold("Global service tier defaults"))}\n${theme.fg(
|
|
241
315
|
"dim",
|
|
242
|
-
|
|
316
|
+
subtitle,
|
|
243
317
|
)}`,
|
|
244
318
|
0,
|
|
245
319
|
0,
|