From 032cf5190492169fc4ea5283d5044b875678b244 Mon Sep 17 00:00:00 2001 From: Simon Shi Date: Tue, 16 Jun 2026 13:38:55 +0800 Subject: [PATCH] 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 --- package-lock.json | 44 ++++++++++++++++++++++++++++++-------------- package.json | 5 +---- src/oauth.ts | 36 +++++++++++++++++------------------- src/stream.ts | 9 ++++++++- 4 files changed, 56 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86283e7..2e16ca4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,15 +8,12 @@ "name": "pi-provider-qoder", "version": "0.1.0", "license": "MIT", - "dependencies": { - "esbuild": "^0.25.0", - "js-tiktoken": "^1.0.21" - }, "devDependencies": { "@biomejs/biome": "2.4.2", "@earendil-works/pi-ai": "^0.75.5", "@earendil-works/pi-coding-agent": "^0.75.5", "@earendil-works/pi-tui": "^0.75.5", + "esbuild": "^0.25.0", "typescript": "^5.7.0" } }, @@ -1259,7 +1256,7 @@ "typebox": "1.1.38" }, "bin": { - "pi-ai": "dist/cli.js" + "pi-ai": "./dist/cli.js" }, "engines": { "node": ">=22.19.0" @@ -2671,6 +2668,7 @@ "cpu": [ "ppc64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2687,6 +2685,7 @@ "cpu": [ "arm" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2703,6 +2702,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2719,6 +2719,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2735,6 +2736,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2751,6 +2753,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2767,6 +2770,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2783,6 +2787,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2799,6 +2804,7 @@ "cpu": [ "arm" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2815,6 +2821,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2831,6 +2838,7 @@ "cpu": [ "ia32" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2847,6 +2855,7 @@ "cpu": [ "loong64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2863,6 +2872,7 @@ "cpu": [ "mips64el" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2879,6 +2889,7 @@ "cpu": [ "ppc64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2895,6 +2906,7 @@ "cpu": [ "riscv64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2911,6 +2923,7 @@ "cpu": [ "s390x" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2927,6 +2940,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2943,6 +2957,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2959,6 +2974,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2975,6 +2991,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -2991,6 +3008,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3007,6 +3025,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3023,6 +3042,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3039,6 +3059,7 @@ "cpu": [ "arm64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3055,6 +3076,7 @@ "cpu": [ "ia32" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3071,6 +3093,7 @@ "cpu": [ "x64" ], + "dev": true, "license": "MIT", "optional": true, "os": [ @@ -3376,6 +3399,7 @@ "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "dev": true, "funding": [ { "type": "github", @@ -3458,6 +3482,7 @@ "version": "0.25.12", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.12.tgz", "integrity": "sha512-bbPBYYrtZbkt6Os6FiTLCTFxvq4tt3JKall1vRwshA3fdVztsLAatFaZobhkBC8/BrPetoa0oksYoKXoG4ryJg==", + "dev": true, "hasInstallScript": true, "license": "MIT", "bin": { @@ -3677,15 +3702,6 @@ "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": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-bigint/-/json-bigint-1.0.0.tgz", diff --git a/package.json b/package.json index 816364c..df53898 100644 --- a/package.json +++ b/package.json @@ -32,15 +32,12 @@ "./dist/index.js" ] }, - "dependencies": { - "esbuild": "^0.25.0", - "js-tiktoken": "^1.0.21" - }, "devDependencies": { "@biomejs/biome": "2.4.2", "@earendil-works/pi-ai": "^0.75.5", "@earendil-works/pi-coding-agent": "^0.75.5", "@earendil-works/pi-tui": "^0.75.5", + "esbuild": "^0.25.0", "typescript": "^5.7.0" } } diff --git a/src/oauth.ts b/src/oauth.ts index e22db60..bfecc1e 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -13,20 +13,22 @@ export interface QoderCredentials extends OAuthCredentials { 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 { - mkdirSync(dirname(CREDENTIALS_CACHE_FILE), { recursive: true }); - writeFileSync(CREDENTIALS_CACHE_FILE, JSON.stringify(creds, null, 2), "utf-8"); - } catch {} -} - -export function getCachedCredentials(accessToken: string): QoderCredentials | null { - if (existsSync(CREDENTIALS_CACHE_FILE)) { +/** + * Read the Qoder identity (userID/email/name/machineID) from pi's own auth + * store. pi persists the full OAuthCredentials there on login/refresh and keeps + * it up to date, so there is no need to maintain a separate credentials cache. + * + * 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(AUTH_FILE)) { try { - const creds = JSON.parse(readFileSync(CREDENTIALS_CACHE_FILE, "utf-8")); - if (creds && creds.access === accessToken) { + const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8")); + const creds = auth?.qoder; + if (creds && creds.userID) { return creds as QoderCredentials; } } catch {} @@ -68,7 +70,7 @@ export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise {}); @@ -80,10 +82,9 @@ export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise {}); } catch {} @@ -147,7 +148,7 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise< machineID, }; - saveCachedCredentials(refreshed as QoderCredentials); + // pi persists the refreshed credentials in auth.json itself. // Cache models in background updateQoderModelsCache( newAccess, @@ -165,8 +166,5 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise< ...credentials, expires: Date.now() + 60 * 60 * 1000, // extend for 1 hour }; - try { - saveCachedCredentials(refreshedFallback as QoderCredentials); - } catch {} return refreshedFallback; } diff --git a/src/stream.ts b/src/stream.ts index bec3b8e..fc97acc 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -9,7 +9,7 @@ import type { } from "@earendil-works/pi-ai"; import * as PiAi from "@earendil-works/pi-ai"; import { buildAuthHeaders, getMachineId } from "./cosy.js"; -import { getCachedModelConfig } from "./models.js"; +import { getCachedModelConfig, isCacheStale, updateQoderModelsCache } from "./models.js"; import { getCachedCredentials } from "./oauth.js"; import { qoderEncodeBody } from "./qoder-encoding.js"; import { ThinkingTagParser } from "./thinking-parser.js"; @@ -90,6 +90,13 @@ 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,