@arhen/pi-senja 1.2.1 → 1.2.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/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +122 -40
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ Forked from [pi-haiku](https://github.com/nnocte/pi-haiku) **0.2.0** by **nocte*
|
|
|
13
13
|
- **Quiet tool text** — tool titles and output both use the foreground beige (`fg0`) rather than loud accents, keeping panels calm against their tint.
|
|
14
14
|
- **Editor-style code colors** — pink keywords, olive strings, aqua function names, amber types, warm beige identifiers and punctuation, readable muted comments. Applies to code in tool calls/results and Markdown code blocks through Pi's syntax roles.
|
|
15
15
|
- **A live timer** while Pi works, plus elapsed-time completion message and notification. Preserves the active mode label and colour from `@arhen/pi-add-mode` (`review is working... 8s`).
|
|
16
|
-
- **Fast / Ultrafast mode** — `/fast on|off` and `/ultrafast on|off` set
|
|
16
|
+
- **Fast / Ultrafast mode** — `/fast on|off` and `/ultrafast on|off` set a paid speed preference per OpenAI provider and model. For ChatGPT sign-in, Senja checks the account’s model catalog before enabling or restoring a tier. If it is unavailable or cannot be verified, Senja warns and does not apply it. API-key mode checks known model support, not account entitlement. The footer shows only `fast` or `ultrafast`. If a completed Senja request reports a different tier or none, Senja warns once and turns the mode off for that model. Failed or canceled requests do not change the preference. Off shows no chip and adds no tier. Preferences persist under `<agent-dir>/fast-mode/`; explicit `samplingParams.service_tier` still wins.
|
|
17
17
|
- **A fresh start** — clear the visible screen on initial terminal startup without clearing scrollback.
|
|
18
18
|
|
|
19
19
|
Enabled by default in terminal UI mode. Print, JSON, SDK, and RPC modes do not install terminal components, switch themes, clear the screen, or start UI timers. `/senja` in RPC mode reports that terminal UI is required.
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -39,10 +39,7 @@ interface RenderHandle {
|
|
|
39
39
|
|
|
40
40
|
type SpeedTier = "fast" | "ultrafast";
|
|
41
41
|
|
|
42
|
-
//
|
|
43
|
-
// so unknown models still get the tier; they just warn here. Fast list from OpenAI's
|
|
44
|
-
// model catalog (codex 0.166.0, service_tiers/priority); Ultrafast list from OpenAI's
|
|
45
|
-
// Ultrafast mode docs. Extend as OpenAI ships new tiers/models.
|
|
42
|
+
// API-key model lists do not advertise speed tiers. OAuth uses the account catalog instead.
|
|
46
43
|
const SPEED_TIER_SUPPORT: Record<SpeedTier, ReadonlySet<string>> = {
|
|
47
44
|
fast: new Set([
|
|
48
45
|
"gpt-5.5",
|
|
@@ -60,6 +57,41 @@ const SPEED_TIER_SUPPORT: Record<SpeedTier, ReadonlySet<string>> = {
|
|
|
60
57
|
ultrafast: new Set(["gpt-6.1-sol", "gpt-6.2-astra", "gpt-6.2-sol", "gpt-6-astra"]),
|
|
61
58
|
};
|
|
62
59
|
|
|
60
|
+
const wireTier = (tier: SpeedTier) => (tier === "fast" ? "priority" : "ultrafast");
|
|
61
|
+
|
|
62
|
+
async function canRequestTier(ctx: ExtensionContext, model: NonNullable<ExtensionContext["model"]>, tier: SpeedTier) {
|
|
63
|
+
const codex = model.provider === "openai-codex";
|
|
64
|
+
const base = codex ? "https://chatgpt.com/backend-api" : "https://api.openai.com/v1";
|
|
65
|
+
const failure = `Cannot verify ${tier} for ${model.provider}/${model.id}; not enabled.`;
|
|
66
|
+
const unsupported = `${tier} is not available for ${model.provider}/${model.id}; not enabled.`;
|
|
67
|
+
if (model.baseUrl?.replace(/\/$/, "") !== base) return failure;
|
|
68
|
+
if (!ctx.modelRegistry.isUsingOAuth(model)) return SPEED_TIER_SUPPORT[tier].has(model.id) ? undefined : unsupported;
|
|
69
|
+
try {
|
|
70
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
71
|
+
if (!auth.ok || !auth.apiKey || (auth.baseUrl && auth.baseUrl.replace(/\/$/, "") !== base)) return failure;
|
|
72
|
+
const headers = new Headers({ Authorization: `Bearer ${auth.apiKey}` });
|
|
73
|
+
const account = auth.headers?.["chatgpt-account-id"];
|
|
74
|
+
if (account) headers.set("ChatGPT-Account-ID", account);
|
|
75
|
+
const response = await fetch(codex ? `${base}/codex/models?client_version=0.166.0` : `${base}/models`, {
|
|
76
|
+
headers,
|
|
77
|
+
redirect: "error",
|
|
78
|
+
signal: AbortSignal.timeout(5000),
|
|
79
|
+
});
|
|
80
|
+
if (!response.ok) return failure;
|
|
81
|
+
const catalog = (await response.json()) as { models?: unknown } | null;
|
|
82
|
+
if (!Array.isArray(catalog?.models)) return failure;
|
|
83
|
+
const entry = catalog.models.find((m: { slug?: string } | null) => m?.slug === model.id);
|
|
84
|
+
if (!entry) return unsupported;
|
|
85
|
+
const tiers = Array.isArray(entry.service_tiers)
|
|
86
|
+
? entry.service_tiers.map((t: { id?: string } | null) => t?.id)
|
|
87
|
+
: entry.additional_speed_tiers;
|
|
88
|
+
if (!Array.isArray(tiers)) return failure;
|
|
89
|
+
return tiers.includes(wireTier(tier)) || tiers.includes(tier) ? undefined : unsupported;
|
|
90
|
+
} catch {
|
|
91
|
+
return failure;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
63
95
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
64
96
|
|
|
65
97
|
/** Replace $HOME with ~, mirroring Pi's built-in footer logic. */
|
|
@@ -324,6 +356,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
324
356
|
let lastDoneIn: number | undefined;
|
|
325
357
|
let modeWorkingMessage: string | undefined;
|
|
326
358
|
const speedTiers = new Map<string, SpeedTier | null>();
|
|
359
|
+
const pendingReports = new Map<string, SpeedTier>();
|
|
327
360
|
|
|
328
361
|
const unsubscribeMode = pi.events.on("pi-mode:working-message", (message) => {
|
|
329
362
|
if (message === undefined || typeof message === "string") modeWorkingMessage = message || undefined;
|
|
@@ -342,23 +375,27 @@ export default function (pi: ExtensionAPI) {
|
|
|
342
375
|
return join(speedDir, createHash("sha256").update(key).digest("hex"));
|
|
343
376
|
}
|
|
344
377
|
|
|
345
|
-
function
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
speedTiers.set(key, tier);
|
|
378
|
+
async function restoreSpeedTier(ctx: ExtensionContext) {
|
|
379
|
+
const model = ctx.model;
|
|
380
|
+
const key = speedKey(model);
|
|
381
|
+
if (!key || !model || speedTiers.has(key)) return;
|
|
382
|
+
let saved: string;
|
|
383
|
+
try {
|
|
384
|
+
saved = readFileSync(speedFile(key), "utf8").trim();
|
|
385
|
+
} catch {
|
|
386
|
+
speedTiers.set(key, null);
|
|
387
|
+
return;
|
|
356
388
|
}
|
|
357
|
-
|
|
389
|
+
if (saved !== "fast" && saved !== "ultrafast") return;
|
|
390
|
+
const warning = await canRequestTier(ctx, model, saved);
|
|
391
|
+
if (speedTiers.has(key)) return;
|
|
392
|
+
speedTiers.set(key, warning ? null : saved);
|
|
393
|
+
if (warning && ctx.hasUI) ctx.ui.notify(warning, "warning");
|
|
358
394
|
}
|
|
359
395
|
|
|
360
396
|
function setSpeedTier(key: string, tier: SpeedTier | null): void {
|
|
361
397
|
speedTiers.set(key, tier);
|
|
398
|
+
pendingReports.delete(key);
|
|
362
399
|
if (tier) {
|
|
363
400
|
mkdirSync(speedDir, { recursive: true });
|
|
364
401
|
writeFileSync(speedFile(key), `${tier}\n`);
|
|
@@ -376,17 +413,56 @@ export default function (pi: ExtensionAPI) {
|
|
|
376
413
|
|
|
377
414
|
// Re-render when state that the footer displays changes.
|
|
378
415
|
pi.on("thinking_level_select", requestFooterRender);
|
|
379
|
-
pi.on("model_select",
|
|
416
|
+
pi.on("model_select", async (_event, ctx) => {
|
|
417
|
+
await restoreSpeedTier(ctx);
|
|
418
|
+
requestFooterRender();
|
|
419
|
+
});
|
|
380
420
|
|
|
381
421
|
// Inject the active speed tier into OpenAI requests. /fast maps to service_tier
|
|
382
422
|
// "priority": the ChatGPT backend rejects the literal "fast" value.
|
|
383
|
-
pi.on("before_provider_request", (event, ctx) => {
|
|
423
|
+
pi.on("before_provider_request", async (event, ctx) => {
|
|
384
424
|
const model = ctx.model;
|
|
385
|
-
const
|
|
386
|
-
if (!model || !speedTier) return;
|
|
425
|
+
const key = speedKey(model);
|
|
387
426
|
const payload = event.payload as { model?: string; service_tier?: unknown } | undefined;
|
|
388
|
-
if (!payload || payload.model !== model
|
|
389
|
-
|
|
427
|
+
if (!key || !payload || payload.model !== model?.id) return;
|
|
428
|
+
await restoreSpeedTier(ctx);
|
|
429
|
+
const tier = speedTiers.get(key);
|
|
430
|
+
if (!tier) return;
|
|
431
|
+
pendingReports.delete(key);
|
|
432
|
+
if (payload.service_tier !== undefined) return;
|
|
433
|
+
pendingReports.set(key, tier);
|
|
434
|
+
return { ...payload, service_tier: wireTier(tier) };
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
function finishSpeedRequest(key: string, served: unknown, ctx: ExtensionContext) {
|
|
438
|
+
const tier = pendingReports.get(key);
|
|
439
|
+
if (!tier) return;
|
|
440
|
+
pendingReports.delete(key);
|
|
441
|
+
if (served === wireTier(tier) || served === tier) return;
|
|
442
|
+
const reason =
|
|
443
|
+
typeof served === "string" && /^[a-z0-9_-]{1,32}$/i.test(served) ? `reported ${served}` : "did not report a tier";
|
|
444
|
+
let warning = `OpenAI ${reason}. Turned off ${tier} for ${key}.`;
|
|
445
|
+
try {
|
|
446
|
+
setSpeedTier(key, null);
|
|
447
|
+
} catch {
|
|
448
|
+
warning += " Could not save this change.";
|
|
449
|
+
}
|
|
450
|
+
requestFooterRender();
|
|
451
|
+
if (ctx.hasUI) ctx.ui.notify(warning, "warning");
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
pi.on("provider_stream_event", (event, ctx) => {
|
|
455
|
+
const data = event.data as { type?: string; response?: { service_tier?: unknown; status?: string } } | null;
|
|
456
|
+
if (data?.type !== "response.completed" && data?.type !== "response.done") return;
|
|
457
|
+
if (data.response?.status && data.response.status !== "completed") return;
|
|
458
|
+
finishSpeedRequest(`${event.provider}/${event.model}`, data.response?.service_tier, ctx);
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
pi.on("message_end", ({ message }, ctx) => {
|
|
462
|
+
if (message.role !== "assistant") return;
|
|
463
|
+
const key = `${message.provider}/${message.model}`;
|
|
464
|
+
if (message.stopReason === "stop" || message.stopReason === "toolUse") finishSpeedRequest(key, undefined, ctx);
|
|
465
|
+
else pendingReports.delete(key);
|
|
390
466
|
});
|
|
391
467
|
|
|
392
468
|
// Track how long the current prompt has been running.
|
|
@@ -512,9 +588,10 @@ export default function (pi: ExtensionAPI) {
|
|
|
512
588
|
const level = pi.getThinkingLevel();
|
|
513
589
|
modelBlock += ` ${theme.fg("dim", GLYPH.square)} ${theme.fg(effortColor(level), level)}`;
|
|
514
590
|
}
|
|
515
|
-
const
|
|
516
|
-
|
|
517
|
-
|
|
591
|
+
const key = speedKey(ctx.model);
|
|
592
|
+
const tier = key && speedTiers.get(key);
|
|
593
|
+
if (tier) {
|
|
594
|
+
modelBlock += ` ${theme.fg("dim", GLYPH.square)} ${theme.fg("warning", tier)}`;
|
|
518
595
|
}
|
|
519
596
|
|
|
520
597
|
const line1 = alignRight(locationBlock, modelBlock, width, theme);
|
|
@@ -635,17 +712,29 @@ export default function (pi: ExtensionAPI) {
|
|
|
635
712
|
handler: async (args, ctx) => {
|
|
636
713
|
const model = ctx.model;
|
|
637
714
|
const key = speedKey(model);
|
|
638
|
-
let speedTier =
|
|
715
|
+
let speedTier = key ? speedTiers.get(key) : null;
|
|
639
716
|
const arg = args.trim().toLowerCase();
|
|
640
717
|
if (arg !== "on" && arg !== "off") {
|
|
641
|
-
if (ctx.hasUI) ctx.ui.notify(speedTier
|
|
718
|
+
if (ctx.hasUI) ctx.ui.notify(speedTier ?? "Fast and Ultrafast are off", "info");
|
|
642
719
|
return;
|
|
643
720
|
}
|
|
644
721
|
if (!model || !key) {
|
|
645
722
|
if (ctx.hasUI) ctx.ui.notify("Speed tiers require an OpenAI model", "warning");
|
|
646
723
|
return;
|
|
647
724
|
}
|
|
648
|
-
if (arg === "on"
|
|
725
|
+
if (arg === "on") {
|
|
726
|
+
const warning = await canRequestTier(ctx, model, tier);
|
|
727
|
+
if (warning) {
|
|
728
|
+
if (speedTiers.get(key) === tier) {
|
|
729
|
+
speedTiers.set(key, null);
|
|
730
|
+
pendingReports.delete(key);
|
|
731
|
+
requestFooterRender();
|
|
732
|
+
}
|
|
733
|
+
if (ctx.hasUI) ctx.ui.notify(warning, "warning");
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (arg === "on" || !speedTier || speedTier === tier) {
|
|
649
738
|
speedTier = arg === "on" ? tier : null;
|
|
650
739
|
try {
|
|
651
740
|
setSpeedTier(key, speedTier);
|
|
@@ -659,24 +748,17 @@ export default function (pi: ExtensionAPI) {
|
|
|
659
748
|
ctx.ui.notify(`${tier} mode off`, "info");
|
|
660
749
|
return;
|
|
661
750
|
}
|
|
662
|
-
|
|
663
|
-
ctx.ui.notify(
|
|
664
|
-
`${speedTier} mode on — ${model.id} is not on the known ${speedTier} support list; OpenAI may reject or ignore the request`,
|
|
665
|
-
"warning",
|
|
666
|
-
);
|
|
667
|
-
} else {
|
|
668
|
-
ctx.ui.notify(
|
|
669
|
-
`${speedTier} mode on — service_tier: ${speedTier === "fast" ? "priority" : "ultrafast"}`,
|
|
670
|
-
"info",
|
|
671
|
-
);
|
|
672
|
-
}
|
|
751
|
+
ctx.ui.notify(speedTier, "info");
|
|
673
752
|
},
|
|
674
753
|
});
|
|
675
754
|
|
|
676
755
|
pi.registerCommand("fast", speedTierCommand("fast"));
|
|
677
756
|
pi.registerCommand("ultrafast", speedTierCommand("ultrafast"));
|
|
678
757
|
|
|
679
|
-
pi.on("session_start", (event, ctx) => {
|
|
758
|
+
pi.on("session_start", async (event, ctx) => {
|
|
759
|
+
speedTiers.clear();
|
|
760
|
+
pendingReports.clear();
|
|
761
|
+
await restoreSpeedTier(ctx);
|
|
680
762
|
clearWorkingTimers();
|
|
681
763
|
workingSince = undefined;
|
|
682
764
|
lastDoneIn = undefined;
|