@arhen/pi-senja 1.2.5 → 1.2.6
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 +75 -68
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,43 @@ 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
|
+
async function restoreSpeedTier(ctx: ExtensionContext, retry = false) {
|
|
379
400
|
const model = ctx.model;
|
|
380
401
|
const key = speedKey(model);
|
|
381
402
|
if (!key || !model || speedTiers.has(key)) return;
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
403
|
+
const wasUnverified = unverifiedTiers.has(key);
|
|
404
|
+
if (wasUnverified && !retry) return;
|
|
405
|
+
const saved = readSpeedTier(key);
|
|
406
|
+
if (!saved) {
|
|
407
|
+
unverifiedTiers.delete(key);
|
|
386
408
|
speedTiers.set(key, null);
|
|
387
409
|
return;
|
|
388
410
|
}
|
|
389
|
-
|
|
390
|
-
const warning = await canRequestTier(ctx, model, saved);
|
|
411
|
+
const issue = await getTierIssue(ctx, model, saved);
|
|
391
412
|
if (speedTiers.has(key)) return;
|
|
392
|
-
|
|
393
|
-
|
|
413
|
+
if (issue === "unverified") {
|
|
414
|
+
unverifiedTiers.add(key);
|
|
415
|
+
} else {
|
|
416
|
+
unverifiedTiers.delete(key);
|
|
417
|
+
speedTiers.set(key, issue ? null : saved);
|
|
418
|
+
}
|
|
419
|
+
if (issue && (issue !== "unverified" || !wasUnverified) && ctx.hasUI) {
|
|
420
|
+
ctx.ui.notify(tierIssueMessage(issue, key, saved), "warning");
|
|
421
|
+
}
|
|
394
422
|
}
|
|
395
423
|
|
|
396
424
|
function setSpeedTier(key: string, tier: SpeedTier | null): void {
|
|
397
425
|
speedTiers.set(key, tier);
|
|
398
|
-
|
|
426
|
+
unverifiedTiers.delete(key);
|
|
399
427
|
if (tier) {
|
|
400
428
|
mkdirSync(speedDir, { recursive: true });
|
|
401
429
|
writeFileSync(speedFile(key), `${tier}\n`);
|
|
@@ -414,7 +442,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
414
442
|
// Re-render when state that the footer displays changes.
|
|
415
443
|
pi.on("thinking_level_select", requestFooterRender);
|
|
416
444
|
pi.on("model_select", async (_event, ctx) => {
|
|
417
|
-
await restoreSpeedTier(ctx);
|
|
445
|
+
await restoreSpeedTier(ctx, true);
|
|
418
446
|
requestFooterRender();
|
|
419
447
|
});
|
|
420
448
|
|
|
@@ -424,46 +452,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
424
452
|
const model = ctx.model;
|
|
425
453
|
const key = speedKey(model);
|
|
426
454
|
const payload = event.payload as { model?: string; service_tier?: unknown } | undefined;
|
|
427
|
-
if (!key || !payload || payload.model !== model
|
|
455
|
+
if (!key || !model || !payload || payload.model !== model.id) return;
|
|
428
456
|
await restoreSpeedTier(ctx);
|
|
429
457
|
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);
|
|
458
|
+
if (!tier || payload.service_tier !== undefined) return;
|
|
434
459
|
return { ...payload, service_tier: wireTier(tier) };
|
|
435
460
|
});
|
|
436
461
|
|
|
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
462
|
// Track how long the current prompt has been running.
|
|
468
463
|
pi.on("agent_start", (_event, ctx) => {
|
|
469
464
|
clearWorkingTimers();
|
|
@@ -487,6 +482,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
487
482
|
workingTimer = setInterval(tick, 250);
|
|
488
483
|
});
|
|
489
484
|
|
|
485
|
+
pi.on("agent_start", async (_event, ctx) => {
|
|
486
|
+
const key = speedKey(ctx.model);
|
|
487
|
+
if (!key || !unverifiedTiers.has(key)) return;
|
|
488
|
+
await restoreSpeedTier(ctx, true);
|
|
489
|
+
requestFooterRender();
|
|
490
|
+
});
|
|
491
|
+
|
|
490
492
|
// Report final duration and clean up timers.
|
|
491
493
|
pi.on("agent_end", (_event, ctx) => {
|
|
492
494
|
clearWorkingTimers();
|
|
@@ -722,18 +724,23 @@ export default function (pi: ExtensionAPI) {
|
|
|
722
724
|
return;
|
|
723
725
|
}
|
|
724
726
|
if (arg === "on") {
|
|
725
|
-
const
|
|
726
|
-
if (
|
|
727
|
+
const issue = await getTierIssue(ctx, model, tier);
|
|
728
|
+
if (issue) {
|
|
727
729
|
if (speedTiers.get(key) === tier) {
|
|
728
|
-
|
|
729
|
-
|
|
730
|
+
if (issue === "unverified") {
|
|
731
|
+
speedTiers.delete(key);
|
|
732
|
+
unverifiedTiers.add(key);
|
|
733
|
+
} else {
|
|
734
|
+
speedTiers.set(key, null);
|
|
735
|
+
}
|
|
730
736
|
requestFooterRender();
|
|
731
737
|
}
|
|
732
|
-
if (ctx.hasUI) ctx.ui.notify(
|
|
738
|
+
if (ctx.hasUI) ctx.ui.notify(tierIssueMessage(issue, key, tier), "warning");
|
|
733
739
|
return;
|
|
734
740
|
}
|
|
735
741
|
}
|
|
736
|
-
|
|
742
|
+
const configuredTier = arg === "off" ? (speedTier ?? readSpeedTier(key)) : tier;
|
|
743
|
+
if (arg === "on" || !configuredTier || configuredTier === tier) {
|
|
737
744
|
speedTier = arg === "on" ? tier : null;
|
|
738
745
|
try {
|
|
739
746
|
setSpeedTier(key, speedTier);
|
|
@@ -756,7 +763,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
756
763
|
|
|
757
764
|
pi.on("session_start", async (event, ctx) => {
|
|
758
765
|
speedTiers.clear();
|
|
759
|
-
|
|
766
|
+
unverifiedTiers.clear();
|
|
760
767
|
await restoreSpeedTier(ctx);
|
|
761
768
|
clearWorkingTimers();
|
|
762
769
|
workingSince = undefined;
|