From 26be3e0e171950197cde450815660e820caa547b Mon Sep 17 00:00:00 2001 From: Simon Shi Date: Tue, 16 Jun 2026 13:39:24 +0800 Subject: [PATCH] refactor: optimize model cache updates by moving to session_start hook & bump to 0.2.1 --- package.json | 2 +- src/index.ts | 22 ++++++++++++++++++++-- src/stream.ts | 9 +-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 2eaf72c..9a68e15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pi-provider-qoder", - "version": "0.2.0", + "version": "0.2.1", "description": "Pi extension for Qoder AI — with OAuth authentication, COSY signatures and WAF bypass", "type": "module", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index 2d4d8a2..5b1e4b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai"; import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent"; -import { getCachedModels, staticModels } from "./models.js"; -import { loginQoder, refreshQoderToken } from "./oauth.js"; +import { getCachedModels, isCacheStale, staticModels, updateQoderModelsCache } from "./models.js"; +import { getCachedCredentials, loginQoder, refreshQoderToken } from "./oauth.js"; import { streamQoder } from "./stream.js"; import { fetchQoderUsage } from "./usage.js"; @@ -12,6 +12,24 @@ type OAuthConfigWithUsage = NonNullable & { }; export default function (pi: ExtensionAPI) { + // Refresh the models cache once per session at startup if it is missing or + // stale (>1h old), rather than on every message in the stream hot path. + // Login/refresh are the other rebuild triggers; this covers the case where + // the cache was deleted while the token is still valid. + pi.on("session_start", async (_event, ctx) => { + try { + const accessToken = await ctx.modelRegistry.getApiKeyForProvider("qoder"); + if (!accessToken || !isCacheStale()) return; + const creds = getCachedCredentials(accessToken); + const userID = creds?.userID || "qoder-user"; + const name = creds?.name || "Qoder User"; + const email = creds?.email || "user@qoder.com"; + await updateQoderModelsCache(accessToken, userID, name, email); + } catch { + // Best-effort: fall back to the existing cache / static models. + } + }); + const oauth: OAuthConfigWithUsage = { name: "Qoder (Browser OAuth / PAT)", login: loginQoder, diff --git a/src/stream.ts b/src/stream.ts index 4256fa9..c1837c9 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -12,7 +12,7 @@ import type { } from "@earendil-works/pi-ai"; import * as PiAi from "@earendil-works/pi-ai"; import { buildAuthHeaders, getMachineId } from "./cosy.js"; -import { getCachedModelConfig, isCacheStale, updateQoderModelsCache } from "./models.js"; +import { getCachedModelConfig } from "./models.js"; import { getCachedCredentials } from "./oauth.js"; import { qoderEncodeBody } from "./qoder-encoding.js"; import { ThinkingTagParser } from "./thinking-parser.js"; @@ -107,13 +107,6 @@ export function streamQoder( const email = cachedCreds?.email || "user@qoder.com"; const machineID = cachedCreds?.machineID || getMachineId(); - // Passively rebuild the models cache if it's missing or stale. - // Login/refresh are the only other rebuild triggers, so without this a - // deleted cache file never gets recreated while the token stays valid. - if (isCacheStale()) { - updateQoderModelsCache(accessToken, userID, name, email).catch(() => {}); - } - const qoderModel = model.id; const modelConfig = getCachedModelConfig(qoderModel) || { key: qoderModel,