@arhen/pi-senja 1.2.5 → 1.2.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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +82 -70
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 a paid speed preference per OpenAI provider and model.
|
|
16
|
+
- **Fast / Ultrafast mode** — `/fast on|off` and `/ultrafast on|off` set a paid speed preference per OpenAI provider and model. With `openai-codex` and ChatGPT sign-in, Senja checks the account catalog and retries a saved tier after a temporary lookup failure on the next turn or model selection. With the public `openai` Responses provider, Senja checks known model support and OpenAI handles account eligibility. The footer shows the selected preference as `fast` or `ultrafast`. Preferences persist under `<agent-dir>/fast-mode/`. An explicit `samplingParams.service_tier` takes precedence.
|
|
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
|
@@ -38,8 +38,9 @@ interface RenderHandle {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
type SpeedTier = "fast" | "ultrafast";
|
|
41
|
+
type TierIssue = "unsupported" | "unverified";
|
|
41
42
|
|
|
42
|
-
//
|
|
43
|
+
// The public model list has no speed-tier metadata for either sign-in method.
|
|
43
44
|
const SPEED_TIER_SUPPORT: Record<SpeedTier, ReadonlySet<string>> = {
|
|
44
45
|
fast: new Set([
|
|
45
46
|
"gpt-5.5",
|
|
@@ -59,39 +60,50 @@ const SPEED_TIER_SUPPORT: Record<SpeedTier, ReadonlySet<string>> = {
|
|
|
59
60
|
|
|
60
61
|
const wireTier = (tier: SpeedTier) => (tier === "fast" ? "priority" : "ultrafast");
|
|
61
62
|
|
|
62
|
-
async function
|
|
63
|
+
async function getTierIssue(
|
|
64
|
+
ctx: ExtensionContext,
|
|
65
|
+
model: NonNullable<ExtensionContext["model"]>,
|
|
66
|
+
tier: SpeedTier,
|
|
67
|
+
): Promise<TierIssue | undefined> {
|
|
63
68
|
const codex = model.provider === "openai-codex";
|
|
64
69
|
const base = codex ? "https://chatgpt.com/backend-api" : "https://api.openai.com/v1";
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
if (model.baseUrl?.replace(/\/$/, "") !== base) return "unsupported";
|
|
71
|
+
if (!codex || !ctx.modelRegistry.isUsingOAuth(model)) {
|
|
72
|
+
return SPEED_TIER_SUPPORT[tier].has(model.id) ? undefined : "unsupported";
|
|
73
|
+
}
|
|
69
74
|
try {
|
|
70
75
|
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
71
|
-
if (!auth.ok || !auth.apiKey
|
|
76
|
+
if (!auth.ok || !auth.apiKey) return "unverified";
|
|
77
|
+
if (auth.baseUrl && auth.baseUrl.replace(/\/$/, "") !== base) return "unsupported";
|
|
72
78
|
const headers = new Headers({ Authorization: `Bearer ${auth.apiKey}` });
|
|
73
79
|
const account = auth.headers?.["chatgpt-account-id"];
|
|
74
80
|
if (account) headers.set("ChatGPT-Account-ID", account);
|
|
75
|
-
const response = await fetch(
|
|
81
|
+
const response = await fetch(`${base}/codex/models?client_version=0.166.0`, {
|
|
76
82
|
headers,
|
|
77
83
|
redirect: "error",
|
|
78
84
|
signal: AbortSignal.timeout(5000),
|
|
79
85
|
});
|
|
80
|
-
if (!response.ok) return
|
|
86
|
+
if (!response.ok) return "unverified";
|
|
81
87
|
const catalog = (await response.json()) as { models?: unknown } | null;
|
|
82
|
-
if (!Array.isArray(catalog?.models)) return
|
|
88
|
+
if (!Array.isArray(catalog?.models)) return "unverified";
|
|
83
89
|
const entry = catalog.models.find((m: { slug?: string } | null) => m?.slug === model.id);
|
|
84
|
-
if (!entry) return unsupported;
|
|
90
|
+
if (!entry) return "unsupported";
|
|
85
91
|
const tiers = Array.isArray(entry.service_tiers)
|
|
86
92
|
? entry.service_tiers.map((t: { id?: string } | null) => t?.id)
|
|
87
93
|
: entry.additional_speed_tiers;
|
|
88
|
-
if (!Array.isArray(tiers)) return
|
|
89
|
-
return tiers.includes(wireTier(tier)) || tiers.includes(tier) ? undefined : unsupported;
|
|
94
|
+
if (!Array.isArray(tiers)) return "unverified";
|
|
95
|
+
return tiers.includes(wireTier(tier)) || tiers.includes(tier) ? undefined : "unsupported";
|
|
90
96
|
} catch {
|
|
91
|
-
return
|
|
97
|
+
return "unverified";
|
|
92
98
|
}
|
|
93
99
|
}
|
|
94
100
|
|
|
101
|
+
function tierIssueMessage(issue: TierIssue, key: string, tier: SpeedTier): string {
|
|
102
|
+
return issue === "unsupported"
|
|
103
|
+
? `${tier}: not supported for ${key}`
|
|
104
|
+
: `Cannot verify ${tier} for ${key}; not enabled.`;
|
|
105
|
+
}
|
|
106
|
+
|
|
95
107
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
96
108
|
|
|
97
109
|
/** Replace $HOME with ~, mirroring Pi's built-in footer logic. */
|
|
@@ -356,7 +368,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
356
368
|
let lastDoneIn: number | undefined;
|
|
357
369
|
let modeWorkingMessage: string | undefined;
|
|
358
370
|
const speedTiers = new Map<string, SpeedTier | null>();
|
|
359
|
-
const
|
|
371
|
+
const unverifiedTiers = new Set<string>();
|
|
360
372
|
|
|
361
373
|
const unsubscribeMode = pi.events.on("pi-mode:working-message", (message) => {
|
|
362
374
|
if (message === undefined || typeof message === "string") modeWorkingMessage = message || undefined;
|
|
@@ -375,27 +387,49 @@ export default function (pi: ExtensionAPI) {
|
|
|
375
387
|
return join(speedDir, createHash("sha256").update(key).digest("hex"));
|
|
376
388
|
}
|
|
377
389
|
|
|
378
|
-
|
|
390
|
+
function readSpeedTier(key: string): SpeedTier | null {
|
|
391
|
+
try {
|
|
392
|
+
const saved = readFileSync(speedFile(key), "utf8").trim();
|
|
393
|
+
return saved === "fast" || saved === "ultrafast" ? saved : null;
|
|
394
|
+
} catch {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function applySpeedTier(key: string, tier: SpeedTier | null): void {
|
|
400
|
+
const previous = speedTiers.get(key) ?? null;
|
|
401
|
+
speedTiers.set(key, tier);
|
|
402
|
+
if (previous !== tier) pi.events.emit("pi-senja:speed-tier-change", key);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async function restoreSpeedTier(ctx: ExtensionContext, retry = false) {
|
|
379
406
|
const model = ctx.model;
|
|
380
407
|
const key = speedKey(model);
|
|
381
408
|
if (!key || !model || speedTiers.has(key)) return;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
409
|
+
const wasUnverified = unverifiedTiers.has(key);
|
|
410
|
+
if (wasUnverified && !retry) return;
|
|
411
|
+
const saved = readSpeedTier(key);
|
|
412
|
+
if (!saved) {
|
|
413
|
+
unverifiedTiers.delete(key);
|
|
414
|
+
applySpeedTier(key, null);
|
|
387
415
|
return;
|
|
388
416
|
}
|
|
389
|
-
|
|
390
|
-
const warning = await canRequestTier(ctx, model, saved);
|
|
417
|
+
const issue = await getTierIssue(ctx, model, saved);
|
|
391
418
|
if (speedTiers.has(key)) return;
|
|
392
|
-
|
|
393
|
-
|
|
419
|
+
if (issue === "unverified") {
|
|
420
|
+
unverifiedTiers.add(key);
|
|
421
|
+
} else {
|
|
422
|
+
unverifiedTiers.delete(key);
|
|
423
|
+
applySpeedTier(key, issue ? null : saved);
|
|
424
|
+
}
|
|
425
|
+
if (issue && (issue !== "unverified" || !wasUnverified) && ctx.hasUI) {
|
|
426
|
+
ctx.ui.notify(tierIssueMessage(issue, key, saved), "warning");
|
|
427
|
+
}
|
|
394
428
|
}
|
|
395
429
|
|
|
396
430
|
function setSpeedTier(key: string, tier: SpeedTier | null): void {
|
|
397
|
-
|
|
398
|
-
|
|
431
|
+
applySpeedTier(key, tier);
|
|
432
|
+
unverifiedTiers.delete(key);
|
|
399
433
|
if (tier) {
|
|
400
434
|
mkdirSync(speedDir, { recursive: true });
|
|
401
435
|
writeFileSync(speedFile(key), `${tier}\n`);
|
|
@@ -414,7 +448,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
414
448
|
// Re-render when state that the footer displays changes.
|
|
415
449
|
pi.on("thinking_level_select", requestFooterRender);
|
|
416
450
|
pi.on("model_select", async (_event, ctx) => {
|
|
417
|
-
await restoreSpeedTier(ctx);
|
|
451
|
+
await restoreSpeedTier(ctx, true);
|
|
418
452
|
requestFooterRender();
|
|
419
453
|
});
|
|
420
454
|
|
|
@@ -424,46 +458,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
424
458
|
const model = ctx.model;
|
|
425
459
|
const key = speedKey(model);
|
|
426
460
|
const payload = event.payload as { model?: string; service_tier?: unknown } | undefined;
|
|
427
|
-
if (!key || !payload || payload.model !== model
|
|
461
|
+
if (!key || !model || !payload || payload.model !== model.id) return;
|
|
428
462
|
await restoreSpeedTier(ctx);
|
|
429
463
|
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);
|
|
464
|
+
if (!tier || payload.service_tier !== undefined) return;
|
|
434
465
|
return { ...payload, service_tier: wireTier(tier) };
|
|
435
466
|
});
|
|
436
467
|
|
|
437
|
-
function finishSpeedRequest(provider: string, model: string, served: unknown, ctx: ExtensionContext) {
|
|
438
|
-
const key = `${provider}/${model}`;
|
|
439
|
-
const tier = pendingReports.get(key);
|
|
440
|
-
if (!tier) return;
|
|
441
|
-
pendingReports.delete(key);
|
|
442
|
-
if (!ctx.hasUI || served === wireTier(tier) || served === tier) return;
|
|
443
|
-
const message =
|
|
444
|
-
typeof served === "string" && ["default", "priority", "fast", "ultrafast", "flex", "scale"].includes(served)
|
|
445
|
-
? `${tier} miss: served ${served} (${sanitizeStatus(model)})`
|
|
446
|
-
: `${tier}: tier not confirmed`;
|
|
447
|
-
if (ctx.mode === "tui") ctx.ui.notify(ctx.ui.theme.fg("warning", message), "info");
|
|
448
|
-
else ctx.ui.notify(message, "warning");
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
pi.on("provider_stream_event", (event, ctx) => {
|
|
452
|
-
const data = event.data as { type?: string; response?: { service_tier?: unknown; status?: string } } | null;
|
|
453
|
-
if (data?.type !== "response.completed" && data?.type !== "response.done") return;
|
|
454
|
-
if (data.response?.status && data.response.status !== "completed") return;
|
|
455
|
-
finishSpeedRequest(event.provider, event.model, data.response?.service_tier, ctx);
|
|
456
|
-
});
|
|
457
|
-
|
|
458
|
-
pi.on("message_end", ({ message }, ctx) => {
|
|
459
|
-
if (message.role !== "assistant") return;
|
|
460
|
-
if (message.stopReason === "stop" || message.stopReason === "toolUse") {
|
|
461
|
-
finishSpeedRequest(message.provider, message.model, undefined, ctx);
|
|
462
|
-
} else {
|
|
463
|
-
pendingReports.delete(`${message.provider}/${message.model}`);
|
|
464
|
-
}
|
|
465
|
-
});
|
|
466
|
-
|
|
467
468
|
// Track how long the current prompt has been running.
|
|
468
469
|
pi.on("agent_start", (_event, ctx) => {
|
|
469
470
|
clearWorkingTimers();
|
|
@@ -487,6 +488,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
487
488
|
workingTimer = setInterval(tick, 250);
|
|
488
489
|
});
|
|
489
490
|
|
|
491
|
+
pi.on("agent_start", async (_event, ctx) => {
|
|
492
|
+
const key = speedKey(ctx.model);
|
|
493
|
+
if (!key || !unverifiedTiers.has(key)) return;
|
|
494
|
+
await restoreSpeedTier(ctx, true);
|
|
495
|
+
requestFooterRender();
|
|
496
|
+
});
|
|
497
|
+
|
|
490
498
|
// Report final duration and clean up timers.
|
|
491
499
|
pi.on("agent_end", (_event, ctx) => {
|
|
492
500
|
clearWorkingTimers();
|
|
@@ -722,18 +730,22 @@ export default function (pi: ExtensionAPI) {
|
|
|
722
730
|
return;
|
|
723
731
|
}
|
|
724
732
|
if (arg === "on") {
|
|
725
|
-
const
|
|
726
|
-
if (
|
|
733
|
+
const issue = await getTierIssue(ctx, model, tier);
|
|
734
|
+
if (issue) {
|
|
727
735
|
if (speedTiers.get(key) === tier) {
|
|
728
|
-
|
|
729
|
-
|
|
736
|
+
applySpeedTier(key, null);
|
|
737
|
+
if (issue === "unverified") {
|
|
738
|
+
speedTiers.delete(key);
|
|
739
|
+
unverifiedTiers.add(key);
|
|
740
|
+
}
|
|
730
741
|
requestFooterRender();
|
|
731
742
|
}
|
|
732
|
-
if (ctx.hasUI) ctx.ui.notify(
|
|
743
|
+
if (ctx.hasUI) ctx.ui.notify(tierIssueMessage(issue, key, tier), "warning");
|
|
733
744
|
return;
|
|
734
745
|
}
|
|
735
746
|
}
|
|
736
|
-
|
|
747
|
+
const configuredTier = arg === "off" ? (speedTier ?? readSpeedTier(key)) : tier;
|
|
748
|
+
if (arg === "on" || !configuredTier || configuredTier === tier) {
|
|
737
749
|
speedTier = arg === "on" ? tier : null;
|
|
738
750
|
try {
|
|
739
751
|
setSpeedTier(key, speedTier);
|
|
@@ -756,7 +768,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
756
768
|
|
|
757
769
|
pi.on("session_start", async (event, ctx) => {
|
|
758
770
|
speedTiers.clear();
|
|
759
|
-
|
|
771
|
+
unverifiedTiers.clear();
|
|
760
772
|
await restoreSpeedTier(ctx);
|
|
761
773
|
clearWorkingTimers();
|
|
762
774
|
workingSince = undefined;
|