refactor: optimize model cache updates by moving to session_start hook & bump to 0.2.1

This commit is contained in:
Simon Shi
2026-06-16 13:39:24 +08:00
parent 8fb093bac0
commit 26be3e0e17
3 changed files with 22 additions and 11 deletions
+20 -2
View File
@@ -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<ProviderConfig["oauth"]> & {
};
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,
+1 -8
View File
@@ -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,