refactor: optimize model cache updates by moving to session_start hook & bump to 0.2.1
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pi-provider-qoder",
|
"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",
|
"description": "Pi extension for Qoder AI — with OAuth authentication, COSY signatures and WAF bypass",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
+20
-2
@@ -1,7 +1,7 @@
|
|||||||
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
||||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
||||||
import { getCachedModels, staticModels } from "./models.js";
|
import { getCachedModels, isCacheStale, staticModels, updateQoderModelsCache } from "./models.js";
|
||||||
import { loginQoder, refreshQoderToken } from "./oauth.js";
|
import { getCachedCredentials, loginQoder, refreshQoderToken } from "./oauth.js";
|
||||||
import { streamQoder } from "./stream.js";
|
import { streamQoder } from "./stream.js";
|
||||||
import { fetchQoderUsage } from "./usage.js";
|
import { fetchQoderUsage } from "./usage.js";
|
||||||
|
|
||||||
@@ -12,6 +12,24 @@ type OAuthConfigWithUsage = NonNullable<ProviderConfig["oauth"]> & {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function (pi: ExtensionAPI) {
|
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 = {
|
const oauth: OAuthConfigWithUsage = {
|
||||||
name: "Qoder (Browser OAuth / PAT)",
|
name: "Qoder (Browser OAuth / PAT)",
|
||||||
login: loginQoder,
|
login: loginQoder,
|
||||||
|
|||||||
+1
-8
@@ -12,7 +12,7 @@ import type {
|
|||||||
} from "@earendil-works/pi-ai";
|
} from "@earendil-works/pi-ai";
|
||||||
import * as PiAi from "@earendil-works/pi-ai";
|
import * as PiAi from "@earendil-works/pi-ai";
|
||||||
import { buildAuthHeaders, getMachineId } from "./cosy.js";
|
import { buildAuthHeaders, getMachineId } from "./cosy.js";
|
||||||
import { getCachedModelConfig, isCacheStale, updateQoderModelsCache } from "./models.js";
|
import { getCachedModelConfig } from "./models.js";
|
||||||
import { getCachedCredentials } from "./oauth.js";
|
import { getCachedCredentials } from "./oauth.js";
|
||||||
import { qoderEncodeBody } from "./qoder-encoding.js";
|
import { qoderEncodeBody } from "./qoder-encoding.js";
|
||||||
import { ThinkingTagParser } from "./thinking-parser.js";
|
import { ThinkingTagParser } from "./thinking-parser.js";
|
||||||
@@ -107,13 +107,6 @@ export function streamQoder(
|
|||||||
const email = cachedCreds?.email || "user@qoder.com";
|
const email = cachedCreds?.email || "user@qoder.com";
|
||||||
const machineID = cachedCreds?.machineID || getMachineId();
|
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 qoderModel = model.id;
|
||||||
const modelConfig = getCachedModelConfig(qoderModel) || {
|
const modelConfig = getCachedModelConfig(qoderModel) || {
|
||||||
key: qoderModel,
|
key: qoderModel,
|
||||||
|
|||||||
Reference in New Issue
Block a user