@diegopetrucci/pi-fast 0.1.6 → 0.1.7

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 +43 -4
  2. package/index.ts +190 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # fast
2
2
 
3
- A pi extension that provides one `/fast` toggle 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.
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
5
  For eligible direct OpenAI API models it injects:
6
6
 
@@ -51,6 +51,30 @@ request shape is support evidence, not live certification; this change was
51
51
  validated only with mocked eligibility and request payloads, not live or paid
52
52
  provider calls.
53
53
 
54
+ ### Ultrafast support (initial, fail-closed)
55
+
56
+ Ultrafast is intentionally narrower than ordinary Fast mode. It requires all of
57
+ the following on every activation and provider request:
58
+
59
+ - Provider `openai`.
60
+ - API `openai-responses`.
61
+ - Model `gpt-6-astra`.
62
+ - Direct API-key auth, not Codex OAuth or direct OpenAI OAuth.
63
+ - Base URL exactly `https://api.openai.com/v1` (a single trailing slash is also accepted).
64
+
65
+ It does not support Chat Completions, preview models, regional or proxy endpoints,
66
+ or Anthropic Ultrafast. Eligible payloads receive the requested
67
+ `service_tier: "ultrafast"` only when they do not already contain a
68
+ `service_tier` field. Existing values and mismatched/malformed payloads are left
69
+ untouched. The extension reports a requested tier; it does not claim that OpenAI
70
+ accepted or applied it, and it never silently falls back to ordinary Fast.
71
+
72
+ OpenAI documents this request shape in the [Ultrafast API guide](https://developers.openai.com/api/docs/guides/ultrafast-mode)
73
+ and lists the account/model pricing in its [API pricing](https://developers.openai.com/api/docs/pricing).
74
+ Actual Ultrafast API pricing is **6x Standard**, while Pi's current cost display
75
+ omits that premium. Treat the activation warning as a deliberate paid-usage
76
+ warning.
77
+
54
78
  OpenAI Codex Fast mode requires all of the following:
55
79
 
56
80
  - Provider `openai-codex`.
@@ -91,11 +115,26 @@ Anthropic Fast mode supports both API-key and Claude Code OAuth access when the
91
115
 
92
116
  ```text
93
117
  /fast
118
+ /ultrafast
119
+ /fast ultrafast
94
120
  ```
95
121
 
96
- Run `/fast` to toggle Fast mode for the current session/runtime. The enabled state follows model changes: switching between eligible OpenAI and Anthropic models automatically changes the request format without resetting the toggle. The footer shows `fast` only while the selected model is eligible and Fast mode is enabled.
97
-
98
- The extension defaults to off so installing the full collection does not accidentally spend Fast-mode credits.
122
+ Run `/fast` to toggle ordinary Fast mode for the current session/runtime. The
123
+ enabled state follows model changes: switching between eligible OpenAI and
124
+ Anthropic models automatically changes the request format without resetting the
125
+ toggle. The footer shows `fast` only while the selected model is eligible and
126
+ Fast mode is enabled.
127
+
128
+ `/ultrafast` and `/fast ultrafast` are equivalent toggles for the session-only
129
+ Ultrafast override. Ultrafast defaults off, is never enabled by global or project
130
+ configuration, and can be undone with either command. Running ordinary `/fast`
131
+ while Ultrafast is selected turns Ultrafast off rather than silently switching to
132
+ another paid tier. Switching to an unsupported model or endpoint disables
133
+ Ultrafast and does not re-enable it when `gpt-6-astra` is selected again. The
134
+ footer uses `ultrafast` while an eligible request is selected; status text says
135
+ that the tier is requested, not remotely applied.
136
+
137
+ The extension defaults to off so installing the full collection does not accidentally spend Fast-mode credits. Ultrafast verification is mocked-only: tests use local eligibility checks, fake payloads, and a fake fetch through Pi's `openai-responses` `streamSimple`; they make no live or paid provider requests.
99
138
 
100
139
  ## Config
101
140
 
package/index.ts CHANGED
@@ -34,6 +34,10 @@ const OPENAI_API_PROVIDER_ID = "openai";
34
34
  const OPENAI_API_IDS = new Set(["openai-responses", "openai-completions"]);
35
35
  const OPENAI_API_FAST_SERVICE_TIER = "fast";
36
36
  const OPENAI_API_SUPPORTED_MODELS = new Set(["gpt-6.1-sol", "gpt-6-astra", "gpt-6-sol", "gpt-6-luna"]);
37
+ const OPENAI_ULTRAFAST_MODEL = "gpt-6-astra";
38
+ const OPENAI_ULTRAFAST_API_ID = "openai-responses";
39
+ const OPENAI_ULTRAFAST_SERVICE_TIER = "ultrafast";
40
+ const OPENAI_CANONICAL_BASE_URL = "https://api.openai.com/v1";
37
41
 
38
42
  const DEFAULT_CONFIG: FastConfig = {
39
43
  enabled: false,
@@ -53,6 +57,8 @@ type FastConfig = {
53
57
  type SessionState = {
54
58
  config: FastConfig;
55
59
  override: FastOverride;
60
+ /** Session-only Ultrafast selection; never loaded from configuration. */
61
+ ultrafast: boolean;
56
62
  lastInjectedAt?: number;
57
63
  lastInjectedModel?: string;
58
64
  };
@@ -239,6 +245,73 @@ function getEligibility(ctx: ExtensionContext): Eligibility {
239
245
  };
240
246
  }
241
247
 
248
+ function isCanonicalOpenAIEndpoint(baseUrl: unknown): boolean {
249
+ return baseUrl === OPENAI_CANONICAL_BASE_URL || baseUrl === `${OPENAI_CANONICAL_BASE_URL}/`;
250
+ }
251
+
252
+ function getUltrafastEligibility(ctx: ExtensionContext): Eligibility {
253
+ const model = ctx.model;
254
+ if (!model) {
255
+ return { eligible: false, modelKey: "no-model", reason: "no model is selected" };
256
+ }
257
+
258
+ const key = `${model.provider}/${model.id}`;
259
+ if (model.provider !== OPENAI_API_PROVIDER_ID) {
260
+ return {
261
+ eligible: false,
262
+ modelKey: key,
263
+ reason: "Ultrafast mode requires the direct OpenAI provider",
264
+ };
265
+ }
266
+
267
+ if (model.api !== OPENAI_ULTRAFAST_API_ID) {
268
+ return {
269
+ eligible: false,
270
+ modelKey: key,
271
+ reason: `Ultrafast mode requires API ${OPENAI_ULTRAFAST_API_ID}`,
272
+ };
273
+ }
274
+
275
+ if (model.id !== OPENAI_ULTRAFAST_MODEL) {
276
+ return {
277
+ eligible: false,
278
+ modelKey: key,
279
+ reason: `Ultrafast mode is only enabled for ${OPENAI_ULTRAFAST_MODEL}`,
280
+ };
281
+ }
282
+
283
+ if (!isCanonicalOpenAIEndpoint(model.baseUrl)) {
284
+ return {
285
+ eligible: false,
286
+ modelKey: key,
287
+ reason: `Ultrafast mode requires the canonical endpoint ${OPENAI_CANONICAL_BASE_URL}`,
288
+ };
289
+ }
290
+
291
+ try {
292
+ if (ctx.modelRegistry.isUsingOAuth(model)) {
293
+ return {
294
+ eligible: false,
295
+ modelKey: key,
296
+ reason: "Ultrafast mode requires direct OpenAI API-key auth; OAuth is unsupported",
297
+ };
298
+ }
299
+ } catch {
300
+ return {
301
+ eligible: false,
302
+ modelKey: key,
303
+ reason: "Ultrafast mode requires direct OpenAI API-key auth; auth could not be verified",
304
+ };
305
+ }
306
+
307
+ return {
308
+ eligible: true,
309
+ modelKey: key,
310
+ provider: "openai",
311
+ serviceTier: OPENAI_ULTRAFAST_SERVICE_TIER,
312
+ };
313
+ }
314
+
242
315
  function updateStatus(ctx: ExtensionContext, state: SessionState): void {
243
316
  if (!ctx.hasUI) return;
244
317
  if (!state.config.showStatus) {
@@ -246,6 +319,12 @@ function updateStatus(ctx: ExtensionContext, state: SessionState): void {
246
319
  return;
247
320
  }
248
321
 
322
+ if (state.ultrafast) {
323
+ const eligibility = getUltrafastEligibility(ctx);
324
+ ctx.ui.setStatus(EXTENSION_ID, eligibility.eligible ? "ultrafast" : undefined);
325
+ return;
326
+ }
327
+
249
328
  const eligibility = getEligibility(ctx);
250
329
  ctx.ui.setStatus(
251
330
  EXTENSION_ID,
@@ -254,13 +333,22 @@ function updateStatus(ctx: ExtensionContext, state: SessionState): void {
254
333
  }
255
334
 
256
335
  function getStatusMessage(ctx: ExtensionContext, state: SessionState): string {
257
- const enabled = isFastEnabled(state);
258
- const eligibility = getEligibility(ctx);
259
- const active = enabled && eligibility.eligible;
260
336
  const injected = state.lastInjectedAt
261
337
  ? ` Last injected for ${state.lastInjectedModel ?? "unknown model"} ${Math.max(0, Math.round((Date.now() - state.lastInjectedAt) / 1000))}s ago.`
262
338
  : "";
263
339
 
340
+ if (state.ultrafast) {
341
+ const eligibility = getUltrafastEligibility(ctx);
342
+ if (eligibility.eligible) {
343
+ return `Ultrafast mode is on (session only) and eligible for ${eligibility.modelKey}; requested service_tier=${OPENAI_ULTRAFAST_SERVICE_TIER}. Actual API pricing is 6x Standard, but Pi's current cost display omits this premium.${injected}`;
344
+ }
345
+ return `Ultrafast mode is on (session only), but not eligible for ${eligibility.modelKey}: ${eligibility.reason}.${injected}`;
346
+ }
347
+
348
+ const enabled = isFastEnabled(state);
349
+ const eligibility = getEligibility(ctx);
350
+ const active = enabled && eligibility.eligible;
351
+
264
352
  if (active) {
265
353
  const wireSetting =
266
354
  eligibility.provider === "anthropic"
@@ -316,7 +404,7 @@ async function injectAnthropicFastHeader(
316
404
  state: SessionState,
317
405
  hasActiveTools: boolean,
318
406
  ): Promise<void> {
319
- if (!isFastEnabled(state)) return;
407
+ if (state.ultrafast || !isFastEnabled(state)) return;
320
408
  const eligibility = getEligibility(ctx);
321
409
  if (!eligibility.eligible || eligibility.provider !== "anthropic") return;
322
410
 
@@ -337,12 +425,29 @@ async function injectAnthropicFastHeader(
337
425
  headers[outputKey] = next.join(",");
338
426
  }
339
427
 
428
+ function injectUltrafastPayload(
429
+ payload: unknown,
430
+ ctx: ExtensionContext,
431
+ state: SessionState,
432
+ ): PayloadRecord | undefined {
433
+ if (!state.ultrafast) return undefined;
434
+ const eligibility = getUltrafastEligibility(ctx);
435
+ if (!eligibility.eligible || !eligibility.serviceTier) return undefined;
436
+ if (!isPayloadRecord(payload)) return undefined;
437
+ if (payload.model !== ctx.model?.id) return undefined;
438
+ if ("service_tier" in payload) return undefined;
439
+
440
+ state.lastInjectedAt = Date.now();
441
+ state.lastInjectedModel = eligibility.modelKey;
442
+ return { ...payload, service_tier: eligibility.serviceTier };
443
+ }
444
+
340
445
  function injectFastPayload(
341
446
  payload: unknown,
342
447
  ctx: ExtensionContext,
343
448
  state: SessionState,
344
449
  ): PayloadRecord | undefined {
345
- if (!isFastEnabled(state)) return undefined;
450
+ if (state.ultrafast || !isFastEnabled(state)) return undefined;
346
451
  const eligibility = getEligibility(ctx);
347
452
  if (!eligibility.eligible || !eligibility.provider) return undefined;
348
453
  if (!isPayloadRecord(payload)) return undefined;
@@ -362,6 +467,34 @@ function injectFastPayload(
362
467
  return { ...payload, service_tier: eligibility.serviceTier };
363
468
  }
364
469
 
470
+ function disableUltrafast(ctx: ExtensionContext, state: SessionState, reason?: string): void {
471
+ state.ultrafast = false;
472
+ // Do not fall back to ordinary Fast when a selected Ultrafast model becomes ineligible.
473
+ state.override = "off";
474
+ updateStatus(ctx, state);
475
+ if (reason) {
476
+ ctx.ui.notify(`Ultrafast mode disabled: ${reason}. It remains off until manually enabled.`, "warning");
477
+ }
478
+ }
479
+
480
+ function toggleUltrafast(ctx: ExtensionContext, state: SessionState): void {
481
+ if (state.ultrafast) {
482
+ disableUltrafast(ctx, state);
483
+ ctx.ui.notify("Ultrafast mode is off (session only); regular Fast remains off until /fast is toggled.", "info");
484
+ return;
485
+ }
486
+
487
+ const eligibility = getUltrafastEligibility(ctx);
488
+ if (!eligibility.eligible) {
489
+ ctx.ui.notify(`Cannot enable Ultrafast mode for ${eligibility.modelKey}: ${eligibility.reason}.`, "error");
490
+ return;
491
+ }
492
+
493
+ state.ultrafast = true;
494
+ updateStatus(ctx, state);
495
+ ctx.ui.notify(getStatusMessage(ctx, state), "info");
496
+ }
497
+
365
498
  export default function fastExtension(pi: ExtensionAPI) {
366
499
  const states = new WeakMap<object, SessionState>();
367
500
 
@@ -371,6 +504,7 @@ export default function fastExtension(pi: ExtensionAPI) {
371
504
  state = {
372
505
  config: loadConfig(ctx),
373
506
  override: "auto",
507
+ ultrafast: false,
374
508
  };
375
509
  states.set(ctx.sessionManager, state);
376
510
  }
@@ -381,13 +515,26 @@ export default function fastExtension(pi: ExtensionAPI) {
381
515
  const state: SessionState = {
382
516
  config: loadConfig(ctx),
383
517
  override: "auto",
518
+ ultrafast: false,
384
519
  };
385
520
  states.set(ctx.sessionManager, state);
386
521
  updateStatus(ctx, state);
387
522
  });
388
523
 
389
524
  pi.on("model_select", (_event, ctx) => {
390
- updateStatus(ctx, getState(ctx));
525
+ const state = getState(ctx);
526
+ if (state.ultrafast) {
527
+ const eligibility = getUltrafastEligibility(ctx);
528
+ if (!eligibility.eligible) {
529
+ disableUltrafast(
530
+ ctx,
531
+ state,
532
+ `the selected model ${eligibility.modelKey} is unsupported (${eligibility.reason})`,
533
+ );
534
+ return;
535
+ }
536
+ }
537
+ updateStatus(ctx, state);
391
538
  });
392
539
 
393
540
  pi.on("before_provider_headers", async (event, ctx) => {
@@ -397,11 +544,38 @@ export default function fastExtension(pi: ExtensionAPI) {
397
544
 
398
545
  pi.on("before_provider_request", (event, ctx) => {
399
546
  const state = getState(ctx);
547
+ if (state.ultrafast) {
548
+ const eligibility = getUltrafastEligibility(ctx);
549
+ if (!eligibility.eligible) {
550
+ disableUltrafast(
551
+ ctx,
552
+ state,
553
+ `the current model ${eligibility.modelKey} is unsupported (${eligibility.reason})`,
554
+ );
555
+ return undefined;
556
+ }
557
+ const nextPayload = injectUltrafastPayload(event.payload, ctx, state);
558
+ updateStatus(ctx, state);
559
+ return nextPayload;
560
+ }
561
+
400
562
  const nextPayload = injectFastPayload(event.payload, ctx, state);
401
563
  updateStatus(ctx, state);
402
564
  return nextPayload;
403
565
  });
404
566
 
567
+ pi.registerCommand("ultrafast", {
568
+ description: "Toggle session-only Ultrafast mode for direct OpenAI API-key GPT-6 Astra",
569
+ getArgumentCompletions: () => null,
570
+ handler: async (args, ctx) => {
571
+ if (args.trim()) {
572
+ ctx.ui.notify("Usage: /ultrafast", "warning");
573
+ return;
574
+ }
575
+ toggleUltrafast(ctx, getState(ctx));
576
+ },
577
+ });
578
+
405
579
  pi.registerCommand("fast", {
406
580
  description: "Toggle Fast mode for supported direct OpenAI, OpenAI Codex, and Anthropic Claude models",
407
581
  getArgumentCompletions: () => null,
@@ -409,14 +583,23 @@ export default function fastExtension(pi: ExtensionAPI) {
409
583
  const state = getState(ctx);
410
584
  const action = args.trim();
411
585
 
586
+ if (action === "ultrafast") {
587
+ toggleUltrafast(ctx, state);
588
+ return;
589
+ }
590
+
412
591
  if (!action) {
592
+ if (state.ultrafast) {
593
+ toggleUltrafast(ctx, state);
594
+ return;
595
+ }
413
596
  state.override = isFastEnabled(state) ? "off" : "on";
414
597
  updateStatus(ctx, state);
415
598
  ctx.ui.notify(getStatusMessage(ctx, state), "info");
416
599
  return;
417
600
  }
418
601
 
419
- ctx.ui.notify("Usage: /fast", "warning");
602
+ ctx.ui.notify("Usage: /fast [ultrafast]", "warning");
420
603
  },
421
604
  });
422
605
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopetrucci/pi-fast",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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",