@diegopetrucci/pi-fast 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +17 -14
  2. package/index.ts +21 -80
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A pi extension that provides `/fast` and `/ultrafast` toggles for supported direct OpenAI API, OpenAI Codex, and Anthropic Claude models. It selects the provider-specific Fast-mode request shape from the currently selected model.
4
4
 
5
- For eligible direct OpenAI API models it injects:
5
+ For eligible direct OpenAI API-key models it injects:
6
6
 
7
7
  ```json
8
8
  {
@@ -10,6 +10,14 @@ For eligible direct OpenAI API models it injects:
10
10
  }
11
11
  ```
12
12
 
13
+ For eligible direct OpenAI ChatGPT OAuth models it injects:
14
+
15
+ ```json
16
+ {
17
+ "service_tier": "priority"
18
+ }
19
+ ```
20
+
13
21
  For eligible OpenAI Codex OAuth models it injects:
14
22
 
15
23
  ```json
@@ -26,11 +34,7 @@ For eligible Anthropic Claude models it injects:
26
34
  }
27
35
  ```
28
36
 
29
- It also adds the required Anthropic beta header value:
30
-
31
- ```text
32
- anthropic-beta: fast-mode-2026-02-01
33
- ```
37
+ It also appends the required beta `fast-mode-2026-02-01` to the request's `betas` list, which Pi sends as the `anthropic-beta` header. The extension does not set the `anthropic-beta` header itself, so Pi's own betas are kept.
34
38
 
35
39
  ## Eligibility
36
40
 
@@ -39,16 +43,15 @@ Direct OpenAI API Fast mode requires all of the following:
39
43
  - Provider `openai`.
40
44
  - API `openai-responses` or `openai-completions`.
41
45
  - Model `gpt-6.1-sol`, `gpt-6-astra`, `gpt-6-sol`, or `gpt-6-luna`.
42
- - API-key auth or Pi's `openai` ChatGPT OAuth (`Sign in with ChatGPT`).
46
+ - API-key auth sets `service_tier: "fast"`; Pi's `openai` ChatGPT OAuth sets `priority`.
43
47
  - No existing `service_tier` field in the request payload.
44
48
 
45
- The direct OpenAI route stays on OpenAI's direct API contract for both API-key
46
- auth and Pi's `openai` ChatGPT OAuth. Acceptance of `service_tier: "fast"` by
47
- Pi's `openai` ChatGPT OAuth sign-in endpoint has not been live-verified.
48
- This direct route is never forwarded to legacy `openai-codex` or its
49
- subscription-usage endpoint. The documented request shape is support evidence,
50
- not live certification; this change was validated only with mocked eligibility
51
- and request payloads, not live or paid provider calls.
49
+ The direct OpenAI route stays on OpenAI's direct API contract but selects the tier by auth
50
+ type. OAuth uses `priority` because its endpoint rejected `fast`; acceptance or application
51
+ of `priority` on Pi's OAuth endpoint has not been live-verified. This direct route is never
52
+ forwarded to legacy `openai-codex` or its subscription-usage endpoint. The request shape is
53
+ support evidence, not certification; tests use mocked eligibility and payloads only, with
54
+ no live or paid provider calls.
52
55
 
53
56
  ### Ultrafast support (initial, fail-closed)
54
57
 
package/index.ts CHANGED
@@ -12,9 +12,6 @@ const ANTHROPIC_PROVIDER_ID = "anthropic";
12
12
  const ANTHROPIC_API_ID = "anthropic-messages";
13
13
  const ANTHROPIC_FAST_SPEED = "fast";
14
14
  const ANTHROPIC_FAST_BETA = "fast-mode-2026-02-01";
15
- const CLAUDE_CODE_OAUTH_BETAS = ["claude-code-20250219", "oauth-2025-04-20"];
16
- const FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
17
- const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
18
15
  const ANTHROPIC_SUPPORTED_MODELS = new Set(["claude-opus-4-8", "claude-opus-5", "claude-opus-5-5"]);
19
16
 
20
17
  const OPENAI_PROVIDER_ID = "openai-codex";
@@ -79,11 +76,6 @@ type RecursivePartial<T> = {
79
76
 
80
77
  type PayloadRecord = Record<string, unknown>;
81
78
 
82
- type AnthropicHeaderCompat = {
83
- forceAdaptiveThinking?: boolean;
84
- supportsEagerToolInputStreaming?: boolean;
85
- };
86
-
87
79
  type Eligibility = {
88
80
  eligible: boolean;
89
81
  modelKey: string;
@@ -155,7 +147,7 @@ function describeMode(state: SessionState): string {
155
147
  return state.config.enabled ? "on (config default)" : "off (config default)";
156
148
  }
157
149
 
158
- function getEligibility(ctx: ExtensionContext): Eligibility {
150
+ function getEligibility(ctx: ExtensionContext, includeServiceTier = true): Eligibility {
159
151
  const model = ctx.model;
160
152
  if (!model) {
161
153
  return { eligible: false, modelKey: "no-model", reason: "no model is selected" };
@@ -237,7 +229,11 @@ function getEligibility(ctx: ExtensionContext): Eligibility {
237
229
  eligible: true,
238
230
  modelKey: key,
239
231
  provider: "openai",
240
- serviceTier: OPENAI_API_FAST_SERVICE_TIER,
232
+ serviceTier: includeServiceTier
233
+ ? ctx.modelRegistry.isUsingOAuth(model)
234
+ ? OPENAI_FAST_SERVICE_TIER
235
+ : OPENAI_API_FAST_SERVICE_TIER
236
+ : undefined,
241
237
  };
242
238
  }
243
239
 
@@ -328,7 +324,7 @@ function updateStatus(ctx: ExtensionContext, state: SessionState): void {
328
324
  return;
329
325
  }
330
326
 
331
- const eligibility = getEligibility(ctx);
327
+ const eligibility = getEligibility(ctx, false);
332
328
  ctx.ui.setStatus(EXTENSION_ID, isFastEnabled(state) && eligibility.eligible ? "fast" : undefined);
333
329
  }
334
330
 
@@ -346,7 +342,7 @@ function getStatusMessage(ctx: ExtensionContext, state: SessionState): string {
346
342
  }
347
343
 
348
344
  const enabled = isFastEnabled(state);
349
- const eligibility = getEligibility(ctx);
345
+ const eligibility = getEligibility(ctx, enabled);
350
346
  const active = enabled && eligibility.eligible;
351
347
 
352
348
  if (active) {
@@ -364,67 +360,17 @@ function getStatusMessage(ctx: ExtensionContext, state: SessionState): string {
364
360
  return `Fast mode is ${describeMode(state)}. Current model: ${eligibility.modelKey}.${injected}`;
365
361
  }
366
362
 
367
- function splitBetaHeader(value: string | null | undefined): string[] {
368
- return (value ?? "")
369
- .split(",")
370
- .map((part) => part.trim())
371
- .filter(Boolean);
372
- }
373
-
374
- async function isAnthropicOAuth(ctx: ExtensionContext): Promise<boolean> {
375
- try {
376
- const resolution = await ctx.modelRegistry.getProviderAuth(ANTHROPIC_PROVIDER_ID);
377
- const apiKey = resolution?.auth.apiKey;
378
- if (typeof apiKey === "string") return apiKey.includes("sk-ant-oat");
379
- } catch {
380
- // Fall back to the synchronous registry snapshot below.
381
- }
382
-
383
- return ctx.model ? ctx.modelRegistry.isUsingOAuth(ctx.model) : false;
384
- }
385
-
386
- function getAnthropicProviderBetas(ctx: ExtensionContext, hasActiveTools: boolean): string[] {
387
- const model = ctx.model;
388
- if (!model) return [];
389
- const compat = model.compat as AnthropicHeaderCompat | undefined;
390
-
391
- const betas: string[] = [];
392
- if (hasActiveTools && compat?.supportsEagerToolInputStreaming === false) {
393
- betas.push(FINE_GRAINED_TOOL_STREAMING_BETA);
394
- }
395
- if (compat?.forceAdaptiveThinking !== true) {
396
- betas.push(INTERLEAVED_THINKING_BETA);
397
- }
398
- return betas;
399
- }
400
-
401
- async function injectAnthropicFastHeader(
402
- headers: Record<string, string | null>,
403
- ctx: ExtensionContext,
404
- state: SessionState,
405
- hasActiveTools: boolean,
406
- ): Promise<void> {
407
- if (state.ultrafast || !isFastEnabled(state)) return;
408
- const eligibility = getEligibility(ctx);
409
- if (!eligibility.eligible || eligibility.provider !== "anthropic") return;
410
-
411
- const matchingKeys = Object.keys(headers).filter(
412
- (name) => name.toLowerCase() === "anthropic-beta",
413
- );
414
- const existing = matchingKeys.flatMap((name) => splitBetaHeader(headers[name]));
415
- const matchingModelEntries = Object.entries(ctx.model?.headers ?? {}).filter(
416
- ([name]) => name.toLowerCase() === "anthropic-beta",
417
- );
418
- const modelBetas = matchingModelEntries.flatMap(([, value]) => splitBetaHeader(value));
419
- const providerBetas = getAnthropicProviderBetas(ctx, hasActiveTools);
420
- const oauthBetas = (await isAnthropicOAuth(ctx)) ? CLAUDE_CODE_OAUTH_BETAS : [];
421
- const next = Array.from(
422
- new Set([...modelBetas, ...existing, ...oauthBetas, ...providerBetas, ANTHROPIC_FAST_BETA]),
423
- );
424
-
425
- for (const key of matchingKeys) delete headers[key];
426
- const outputKey = matchingModelEntries.at(-1)?.[0] ?? "anthropic-beta";
427
- headers[outputKey] = next.join(",");
363
+ /**
364
+ * Adds the Fast beta to the request's `betas` list instead of rewriting the
365
+ * `anthropic-beta` header. Pi derives the final beta list from the model and
366
+ * request (mid-conversation effort, server-side fallback, inline tools, OAuth,
367
+ * interleaved thinking, ...) and treats an explicit `anthropic-beta` header as
368
+ * the complete list, so setting that header here would drop any beta this
369
+ * extension does not know about.
370
+ */
371
+ function withAnthropicFastBeta(betas: unknown[] | undefined): unknown[] {
372
+ const existing = betas ?? [];
373
+ return existing.includes(ANTHROPIC_FAST_BETA) ? existing : [...existing, ANTHROPIC_FAST_BETA];
428
374
  }
429
375
 
430
376
  function injectUltrafastPayload(
@@ -457,9 +403,10 @@ function injectFastPayload(
457
403
 
458
404
  if (eligibility.provider === "anthropic") {
459
405
  if ("speed" in payload) return undefined;
406
+ if (payload.betas !== undefined && !Array.isArray(payload.betas)) return undefined;
460
407
  state.lastInjectedAt = Date.now();
461
408
  state.lastInjectedModel = eligibility.modelKey;
462
- return { ...payload, speed: ANTHROPIC_FAST_SPEED };
409
+ return { ...payload, speed: ANTHROPIC_FAST_SPEED, betas: withAnthropicFastBeta(payload.betas) };
463
410
  }
464
411
 
465
412
  if ("service_tier" in payload) return undefined;
@@ -548,12 +495,6 @@ export default function fastExtension(pi: ExtensionAPI) {
548
495
  updateStatus(ctx, state);
549
496
  });
550
497
 
551
- pi.on("before_provider_headers", async (event, ctx) => {
552
- const hasActiveTools =
553
- typeof pi.getActiveTools === "function" && pi.getActiveTools().length > 0;
554
- await injectAnthropicFastHeader(event.headers, ctx, getState(ctx), hasActiveTools);
555
- });
556
-
557
498
  pi.on("before_provider_request", (event, ctx) => {
558
499
  const state = getState(ctx);
559
500
  if (state.ultrafast) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopetrucci/pi-fast",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "A pi extension that enables Fast mode for supported direct OpenAI API, OpenAI Codex, and Anthropic Claude models based on the currently selected model.",
5
5
  "keywords": [
6
6
  "pi-package",