refactor: drop redundant credentials cache and unused deps

- stream: passively rebuild stale/missing models cache on request
- oauth: read identity from pi's auth.json instead of self-managed cache
- deps: remove unused js-tiktoken; move esbuild to devDependencies
This commit is contained in:
Simon Shi
2026-06-16 13:38:55 +08:00
parent c9413961d2
commit 032cf51904
4 changed files with 56 additions and 38 deletions
+30 -14
View File
@@ -8,15 +8,12 @@
"name": "pi-provider-qoder", "name": "pi-provider-qoder",
"version": "0.1.0", "version": "0.1.0",
"license": "MIT", "license": "MIT",
"dependencies": {
"esbuild": "^0.25.0",
"js-tiktoken": "^1.0.21"
},
"devDependencies": { "devDependencies": {
"@biomejs/biome": "2.4.2", "@biomejs/biome": "2.4.2",
"@earendil-works/pi-ai": "^0.75.5", "@earendil-works/pi-ai": "^0.75.5",
"@earendil-works/pi-coding-agent": "^0.75.5", "@earendil-works/pi-coding-agent": "^0.75.5",
"@earendil-works/pi-tui": "^0.75.5", "@earendil-works/pi-tui": "^0.75.5",
"esbuild": "^0.25.0",
"typescript": "^5.7.0" "typescript": "^5.7.0"
} }
}, },
@@ -1259,7 +1256,7 @@
"typebox": "1.1.38" "typebox": "1.1.38"
}, },
"bin": { "bin": {
"pi-ai": "dist/cli.js" "pi-ai": "./dist/cli.js"
}, },
"engines": { "engines": {
"node": ">=22.19.0" "node": ">=22.19.0"
@@ -2671,6 +2668,7 @@
"cpu": [ "cpu": [
"ppc64" "ppc64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2687,6 +2685,7 @@
"cpu": [ "cpu": [
"arm" "arm"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2703,6 +2702,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2719,6 +2719,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2735,6 +2736,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2751,6 +2753,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2767,6 +2770,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2783,6 +2787,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2799,6 +2804,7 @@
"cpu": [ "cpu": [
"arm" "arm"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2815,6 +2821,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2831,6 +2838,7 @@
"cpu": [ "cpu": [
"ia32" "ia32"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2847,6 +2855,7 @@
"cpu": [ "cpu": [
"loong64" "loong64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2863,6 +2872,7 @@
"cpu": [ "cpu": [
"mips64el" "mips64el"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2879,6 +2889,7 @@
"cpu": [ "cpu": [
"ppc64" "ppc64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2895,6 +2906,7 @@
"cpu": [ "cpu": [
"riscv64" "riscv64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2911,6 +2923,7 @@
"cpu": [ "cpu": [
"s390x" "s390x"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2927,6 +2940,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2943,6 +2957,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2959,6 +2974,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2975,6 +2991,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -2991,6 +3008,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3007,6 +3025,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3023,6 +3042,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3039,6 +3059,7 @@
"cpu": [ "cpu": [
"arm64" "arm64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3055,6 +3076,7 @@
"cpu": [ "cpu": [
"ia32" "ia32"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3071,6 +3093,7 @@
"cpu": [ "cpu": [
"x64" "x64"
], ],
"dev": true,
"license": "MIT", "license": "MIT",
"optional": true, "optional": true,
"os": [ "os": [
@@ -3376,6 +3399,7 @@
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"dev": true,
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@@ -3458,6 +3482,7 @@
"version": "0.25.12", "version": "0.25.12",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.12.tgz", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.12.tgz",
"integrity": "sha512-bbPBYYrtZbkt6Os6FiTLCTFxvq4tt3JKall1vRwshA3fdVztsLAatFaZobhkBC8/BrPetoa0oksYoKXoG4ryJg==", "integrity": "sha512-bbPBYYrtZbkt6Os6FiTLCTFxvq4tt3JKall1vRwshA3fdVztsLAatFaZobhkBC8/BrPetoa0oksYoKXoG4ryJg==",
"dev": true,
"hasInstallScript": true, "hasInstallScript": true,
"license": "MIT", "license": "MIT",
"bin": { "bin": {
@@ -3677,15 +3702,6 @@
"node": ">= 14" "node": ">= 14"
} }
}, },
"node_modules/js-tiktoken": {
"version": "1.0.21",
"resolved": "https://registry.npmjs.org/js-tiktoken/-/js-tiktoken-1.0.21.tgz",
"integrity": "sha512-biOj/6M5qdgx5TKjDnFT1ymSpM5tbd3ylwDtrQvFQSu0Z7bBYko2dF+W/aUkXUPuk6IVpRxk/3Q2sHOzGlS36g==",
"license": "MIT",
"dependencies": {
"base64-js": "^1.5.1"
}
},
"node_modules/json-bigint": { "node_modules/json-bigint": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz",
+1 -4
View File
@@ -32,15 +32,12 @@
"./dist/index.js" "./dist/index.js"
] ]
}, },
"dependencies": {
"esbuild": "^0.25.0",
"js-tiktoken": "^1.0.21"
},
"devDependencies": { "devDependencies": {
"@biomejs/biome": "2.4.2", "@biomejs/biome": "2.4.2",
"@earendil-works/pi-ai": "^0.75.5", "@earendil-works/pi-ai": "^0.75.5",
"@earendil-works/pi-coding-agent": "^0.75.5", "@earendil-works/pi-coding-agent": "^0.75.5",
"@earendil-works/pi-tui": "^0.75.5", "@earendil-works/pi-tui": "^0.75.5",
"esbuild": "^0.25.0",
"typescript": "^5.7.0" "typescript": "^5.7.0"
} }
} }
+17 -19
View File
@@ -13,20 +13,22 @@ export interface QoderCredentials extends OAuthCredentials {
machineID: string; machineID: string;
} }
const CREDENTIALS_CACHE_FILE = join(homedir(), ".pi", "agent", "qoder-credentials-cache.json"); const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json");
export function saveCachedCredentials(creds: QoderCredentials) { /**
try { * Read the Qoder identity (userID/email/name/machineID) from pi's own auth
mkdirSync(dirname(CREDENTIALS_CACHE_FILE), { recursive: true }); * store. pi persists the full OAuthCredentials there on login/refresh and keeps
writeFileSync(CREDENTIALS_CACHE_FILE, JSON.stringify(creds, null, 2), "utf-8"); * it up to date, so there is no need to maintain a separate credentials cache.
} catch {} *
} * Note: the auth.json path/shape is a pi internal convention, not a public API.
* This is best-effort and falls back to null so callers can use placeholders.
export function getCachedCredentials(accessToken: string): QoderCredentials | null { */
if (existsSync(CREDENTIALS_CACHE_FILE)) { export function getCachedCredentials(_accessToken: string): QoderCredentials | null {
if (existsSync(AUTH_FILE)) {
try { try {
const creds = JSON.parse(readFileSync(CREDENTIALS_CACHE_FILE, "utf-8")); const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8"));
if (creds && creds.access === accessToken) { const creds = auth?.qoder;
if (creds && creds.userID) {
return creds as QoderCredentials; return creds as QoderCredentials;
} }
} catch {} } catch {}
@@ -68,7 +70,7 @@ export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise<OAuthC
machineID, machineID,
}; };
saveCachedCredentials(creds); // pi persists these credentials in auth.json itself; no separate cache needed.
// Cache models in background // Cache models in background
updateQoderModelsCache(pat, userID, name, email).catch(() => {}); updateQoderModelsCache(pat, userID, name, email).catch(() => {});
@@ -80,10 +82,9 @@ export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise<OAuthC
// 2. Interactive login // 2. Interactive login
const creds = await interactiveLogin(callbacks); const creds = await interactiveLogin(callbacks);
// Cache models in background and save credentials // Cache models in background. pi persists the credentials in auth.json itself.
try { try {
const qCreds = creds as QoderCredentials; const qCreds = creds as QoderCredentials;
saveCachedCredentials(qCreds);
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email).catch(() => {}); updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email).catch(() => {});
} catch {} } catch {}
@@ -147,7 +148,7 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
machineID, machineID,
}; };
saveCachedCredentials(refreshed as QoderCredentials); // pi persists the refreshed credentials in auth.json itself.
// Cache models in background // Cache models in background
updateQoderModelsCache( updateQoderModelsCache(
newAccess, newAccess,
@@ -165,8 +166,5 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
...credentials, ...credentials,
expires: Date.now() + 60 * 60 * 1000, // extend for 1 hour expires: Date.now() + 60 * 60 * 1000, // extend for 1 hour
}; };
try {
saveCachedCredentials(refreshedFallback as QoderCredentials);
} catch {}
return refreshedFallback; return refreshedFallback;
} }
+8 -1
View File
@@ -9,7 +9,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 } from "./models.js"; import { getCachedModelConfig, isCacheStale, updateQoderModelsCache } 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";
@@ -90,6 +90,13 @@ 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,