fix: address round-2 review findings

- apply(): only swap the current model when a Nous model was filtered
  out; a session on another provider is left alone, and the notice names
  the model actually in use instead of the first catalog hit.
- Device-code poll floors the interval at 1s and treats a non-numeric
  interval as absent, matching pi-ai's own poller; interval: 0 from the
  server no longer becomes a tight loop.
- Drop the provider's own context.publish: createProvider already
  persists and publishes the fetchModels return value with checkedAt;
  the extra publish was a second, timestamp-less write each refresh.
- tokenRequest tolerates a literal null error body (data.error would
  have thrown TypeError, masking the HTTP status).
- tsconfig: remove allowImportingTsExtensions and baseUrl, both inert.
- .editorconfig covers .mjs.
- Tests: 14 (was 8). tokenRequest is exported and its HTML/null error
  paths are pinned; the no-credential test now sets NOUS_API_KEY so it
  fails if an env fallback returns; the slash commands are driven
  through a recording pi (switch/notice semantics, and persist-before-
  mutate on a failed flag write). Stub returns a private mkdtemp dir.
- CHANGELOG: /nous-catalog entry states the switch semantics.
This commit is contained in:
AlexKucera committed 2026-11-25 10:09:06 +01:00
1 parent 00d50e10de
commit b9065d409c
7 files changed
+117 -26

No files matched your search

+15 -10
View File
@@ -78,7 +78,7 @@ class OAuthError extends Error {
}
}
async function tokenRequest(body: Record<string, string>, signal?: AbortSignal): Promise<TokenResponse> {
export async function tokenRequest(body: Record<string, string>, signal?: AbortSignal): Promise<TokenResponse> {
const response = await fetch(`${PORTAL}/api/oauth/token`, {
method: "POST",
headers: formHeaders,
@@ -90,7 +90,7 @@ async function tokenRequest(body: Record<string, string>, signal?: AbortSignal):
// Gateways and the Vercel checkpoint return HTML; never let JSON.parse mask the HTTP status.
let data: { error?: string; error_description?: string } = {};
try {
data = JSON.parse(text);
data = JSON.parse(text) ?? {}; // a literal `null` body must not turn `data.error` into a TypeError
} catch {
// non-JSON body: report the status and a short excerpt only
}
@@ -132,7 +132,8 @@ async function login(interaction: ProviderAuthInteraction): Promise<OAuthCredent
await interaction.prompt({ type: "text", message: "Press Enter to open the Nous Portal sign-in page." });
const deadline = Date.now() + device.expires_in * 1000;
let interval = device.interval ?? 5;
// Floor at 1 s like pi-ai's own poller: a missing, zero or non-numeric interval must not become a tight loop.
let interval = Math.max(Number(device.interval) || 5, 1);
while (Date.now() < deadline) {
try {
return toCredential(
@@ -271,9 +272,14 @@ export default function nousPortal(pi: ExtensionAPI) {
catalog = next;
await ctx.modelRegistry.refresh({ providers: [PROVIDER_ID] });
const shown = ctx.modelRegistry.getAvailable().filter((m) => m.provider === PROVIDER_ID && inCatalog(m));
// Keep the session usable if the current model just got filtered out.
if (shown.length && !shown.some((m) => m.id === ctx.model?.id)) await pi.setModel(shown[0]);
ctx.ui.notify(`Nous catalog: ${next} (${shown.length} models)${shown[0] ? " — using " + shown[0].id : ""}`);
// Keep the session usable only when a Nous model just got filtered out; a session on another provider
// is left alone, and the notice names the model actually in use, not the first hit.
const onNous = ctx.model?.provider === PROVIDER_ID;
const current = onNous ? shown.find((m) => m.id === ctx.model?.id) : undefined;
const switched = shown.length && onNous && !current ? shown[0] : undefined;
if (switched) await pi.setModel(switched);
const inUse = current ?? switched;
ctx.ui.notify(`Nous catalog: ${next} (${shown.length} models)${inUse ? " — using " + inUse.id : ""}`);
};
const cycle: Record<Catalog, Catalog> = { full: "free", free: "promo", promo: "full" };
pi.registerCommand("nous-catalog", {
@@ -299,10 +305,9 @@ export default function nousPortal(pi: ExtensionAPI) {
},
async fetchModels(context: RefreshModelsContext) {
const fetched = await fetchModels(context.credential, context.signal);
if (fetched.length) {
models = fetched;
await context.publish({ persist: { models: fetched } });
}
// Empty live result → keep the last good list. createProvider persists and publishes whatever is
// returned (pi-ai models.js refreshModels), so no publish here: that was a second, timestamp-less write.
if (fetched.length) models = fetched;
return models;
},
}),