diff --git a/src/__tests__/catalog-cache.test.ts b/src/__tests__/catalog-cache.test.ts index f06c5dc..b3240db 100644 --- a/src/__tests__/catalog-cache.test.ts +++ b/src/__tests__/catalog-cache.test.ts @@ -2,21 +2,27 @@ import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { getCachedModelConfig, getCachedModels, updateQoderModelsCache } from "../catalog.js"; +import { clearQoderModelsMemCache, getCachedModelConfig, getCachedModels, updateQoderModelsCache } from "../catalog.js"; import { loadLiveFixture, responseFromFixture } from "./live-fixture.js"; +function testHome(): string { + return process.env.HOME || process.env.USERPROFILE || homedir(); +} + const CACHE_PATHS = { - global: join(homedir(), ".pi", "agent", "qoder-models-cache.json"), - cn: join(homedir(), ".pi", "agent", "qoder-cn-models-cache.json"), + global: join(testHome(), ".pi", "agent", "qoder-models-cache.json"), + cn: join(testHome(), ".pi", "agent", "qoder-cn-models-cache.json"), }; let originalCaches: Record; beforeEach(() => { + clearQoderModelsMemCache(); originalCaches = { global: existsSync(CACHE_PATHS.global) ? readFileSync(CACHE_PATHS.global, "utf8") : undefined, cn: existsSync(CACHE_PATHS.cn) ? readFileSync(CACHE_PATHS.cn, "utf8") : undefined, }; for (const path of Object.values(CACHE_PATHS)) rmSync(path, { force: true }); + clearQoderModelsMemCache(); }); afterEach(() => { @@ -27,6 +33,7 @@ afterEach(() => { if (original === undefined) rmSync(path, { force: true }); else writeFileSync(path, original, "utf8"); } + clearQoderModelsMemCache(); }); describe("Qoder model cache", () => { @@ -145,6 +152,7 @@ describe("Qoder model cache", () => { }), "utf8", ); + clearQoderModelsMemCache(); expect(getCachedModels("global").map((model) => model.id)).toEqual(["Ultimate"]); }); @@ -191,4 +199,26 @@ describe("Qoder model cache", () => { const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8")); expect(cache.models[0].contextWindow).toBe(200000); }); + + it("serves getCachedModelConfig from memory after the cache file is removed", async () => { + vi.stubGlobal( + "fetch", + vi.fn().mockResolvedValue({ + ok: true, + json: () => + Promise.resolve({ + chat: [{ key: "lite", enable: true, display_name: "Lite" }], + }), + }), + ); + + await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global"); + expect(getCachedModelConfig("Lite", "global")?.key).toBe("lite"); + + // Hot-path mem cache must keep serving without touching disk again. + rmSync(CACHE_PATHS.global, { force: true }); + expect(existsSync(CACHE_PATHS.global)).toBe(false); + expect(getCachedModelConfig("Lite", "global")?.key).toBe("lite"); + expect(getCachedModels("global").map((m) => m.id)).toEqual(["Lite"]); + }); }); diff --git a/src/__tests__/cosy.test.ts b/src/__tests__/cosy.test.ts index 4b00f23..d02c45e 100644 --- a/src/__tests__/cosy.test.ts +++ b/src/__tests__/cosy.test.ts @@ -1,4 +1,5 @@ -import { describe, expect, it } from "vitest"; +import crypto from "node:crypto"; +import { describe, expect, it, vi } from "vitest"; import { buildAuthHeaders } from "../cosy.js"; describe("COSY client identity", () => { @@ -18,4 +19,40 @@ describe("COSY client identity", () => { const payload = JSON.parse(Buffer.from(payloadB64, "base64").toString("utf8")) as { cosyVersion: string }; expect(payload.cosyVersion).toBe("1.1.38"); }); + + it("matches the legacy full-string Cosy signature for a known body Buffer", () => { + vi.spyOn(crypto, "randomUUID") + .mockReturnValueOnce("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee") // aes key source + .mockReturnValueOnce("11111111-2222-3333-4444-555555555555") // requestId + .mockReturnValueOnce("99999999-8888-7777-6666-555555555555"); // X-Request-Id + + const body = Buffer.from('{"hello":"world","n":1}', "utf8"); + const url = "https://api3.qoder.sh/algo/api/v1/chat"; + const headers = buildAuthHeaders(body, url, { + userID: "user-1", + authToken: "token-1", + name: "Test", + email: "test@example.com", + machineID: "machine-1", + }); + + const auth = headers.Authorization; + const match = auth.match(/^Bearer COSY\.([^.]+)\.([0-9a-f]+)$/); + expect(match).toBeTruthy(); + const payloadB64 = match?.[1] ?? ""; + const sig = match?.[2] ?? ""; + const cosyKey = headers["Cosy-Key"]; + const timestamp = headers["Cosy-Date"]; + const sigPath = headers["Cosy-Sigpath"]; + + const legacyInput = `${payloadB64}\n${cosyKey}\n${timestamp}\n${body.toString("utf8")}\n${sigPath}`; + const legacySig = crypto.createHash("md5").update(legacyInput).digest("hex"); + expect(sig).toBe(legacySig); + + const legacyBodyHash = crypto.createHash("md5").update(body).digest("hex"); + expect(headers["Cosy-Bodyhash"]).toBe(legacyBodyHash); + expect(headers["Cosy-Bodylength"]).toBe(String(body.length)); + + vi.restoreAllMocks(); + }); }); diff --git a/src/__tests__/encoding.test.ts b/src/__tests__/encoding.test.ts index 014f75f..ea759f4 100644 --- a/src/__tests__/encoding.test.ts +++ b/src/__tests__/encoding.test.ts @@ -1,70 +1,106 @@ import { describe, expect, it } from "vitest"; import { qoderEncodeBody } from "../protocol/encoding.js"; +/** Reference encoder matching the pre-Buffer string implementation (byte-identical). */ +function qoderEncodeBodyLegacyString(plaintext: string | Buffer): string { + const qoderCustomAlphabet = "_doRTgHZBKcGVjlvpC,@aFSx#DPuNJme&i*MzLOEn)sUrthbf%Y^w.(kIQyXqWA!"; + const qoderStdAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + const encodeTable = new Uint8Array(256); + for (let i = 0; i < encodeTable.length; i++) encodeTable[i] = i; + for (let i = 0; i < qoderStdAlphabet.length; i++) { + encodeTable[qoderStdAlphabet.charCodeAt(i)] = qoderCustomAlphabet.charCodeAt(i); + } + encodeTable["=".charCodeAt(0)] = "$".charCodeAt(0); + + const std = Buffer.isBuffer(plaintext) ? plaintext.toString("base64") : Buffer.from(plaintext).toString("base64"); + const src = Buffer.from(std, "ascii"); + const n = src.length; + const a = Math.floor(n / 3); + const out = Buffer.allocUnsafe(n); + let dst = 0; + for (let i = n - a; i < n; i++) out[dst++] = encodeTable[src[i]]; + for (let i = a; i < n - a; i++) out[dst++] = encodeTable[src[i]]; + for (let i = 0; i < a; i++) out[dst++] = encodeTable[src[i]]; + return out.toString("ascii"); +} + describe("qoderEncodeBody", () => { it("encodes a simple string", () => { const result = qoderEncodeBody("hello"); - expect(result).toBeTruthy(); - expect(typeof result).toBe("string"); - // Should not contain standard base64 padding char '=' - expect(result).not.toContain("="); + expect(Buffer.isBuffer(result)).toBe(true); + expect(result.toString("ascii")).toBeTruthy(); + expect(result.toString("ascii")).not.toContain("="); }); it("encodes a Buffer", () => { const buf = Buffer.from("hello world"); const result = qoderEncodeBody(buf); - expect(result).toBeTruthy(); - expect(result).not.toContain("="); + expect(result.toString("ascii")).toBeTruthy(); + expect(result.toString("ascii")).not.toContain("="); }); it("produces deterministic output", () => { const a = qoderEncodeBody("test input"); const b = qoderEncodeBody("test input"); - expect(a).toBe(b); + expect(a.equals(b)).toBe(true); }); it("produces different output for different inputs", () => { const a = qoderEncodeBody("input A"); const b = qoderEncodeBody("input B"); - expect(a).not.toBe(b); + expect(a.equals(b)).toBe(false); }); it("handles empty string", () => { const result = qoderEncodeBody(""); - expect(result).toBe(""); + expect(result.length).toBe(0); }); it("handles empty Buffer", () => { const result = qoderEncodeBody(Buffer.alloc(0)); - expect(result).toBe(""); + expect(result.length).toBe(0); }); it("replaces '=' padding with '$'", () => { - // Base64 of "a" is "YQ==" which has padding — our encoding should use $ - const result = qoderEncodeBody("a"); + const result = qoderEncodeBody("a").toString("ascii"); expect(result).not.toContain("="); expect(result).toContain("$"); }); it("uses custom alphabet (not standard base64)", () => { - const result = qoderEncodeBody("The quick brown fox"); - // Standard base64 would use A-Za-z0-9+/= - // Our encoding uses a custom alphabet, so the output should differ + const result = qoderEncodeBody("The quick brown fox").toString("ascii"); const stdBase64 = Buffer.from("The quick brown fox").toString("base64"); expect(result).not.toBe(stdBase64); }); it("handles binary content", () => { const binary = Buffer.from([0x00, 0xff, 0x80, 0x7f, 0x01]); - const result = qoderEncodeBody(binary); + const result = qoderEncodeBody(binary).toString("ascii"); expect(result).toBeTruthy(); expect(result).not.toContain("="); }); it("handles JSON content", () => { const json = JSON.stringify({ key: "value", num: 42 }); - const result = qoderEncodeBody(json); + const result = qoderEncodeBody(json).toString("ascii"); expect(result).toBeTruthy(); expect(result).not.toContain("="); }); + + it("is byte-identical to the legacy string encoder for several inputs", () => { + const samples: Array = [ + "", + "a", + "hello", + "The quick brown fox", + JSON.stringify({ messages: [{ role: "user", content: "hi" }], n: 1 }), + Buffer.from([0x00, 0xff, 0x80, 0x7f, 0x01]), + Buffer.alloc(256, 0x42), + ]; + for (const sample of samples) { + const next = qoderEncodeBody(sample).toString("ascii"); + const legacy = qoderEncodeBodyLegacyString(sample); + expect(next).toBe(legacy); + } + }); }); diff --git a/src/__tests__/oauth.test.ts b/src/__tests__/oauth.test.ts index 8685bd5..ef3870a 100644 --- a/src/__tests__/oauth.test.ts +++ b/src/__tests__/oauth.test.ts @@ -2,12 +2,17 @@ import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { autoLoginQoderFromEnvironment, getCachedCredentials, getQoderPatForMode } from "../auth/oauth.js"; +import { + autoLoginQoderFromEnvironment, + clearQoderAuthMemCache, + getCachedCredentials, + getQoderPatForMode, +} from "../auth/oauth.js"; import { credentialsFromPat } from "../auth/pat.js"; import { updateQoderModelsCache } from "../catalog.js"; import { loadLiveFixture } from "./live-fixture.js"; -const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json"); +const AUTH_FILE = join(process.env.HOME || process.env.USERPROFILE || homedir(), ".pi", "agent", "auth.json"); vi.mock("../auth/pat.js", () => ({ credentialsFromPat: vi.fn().mockResolvedValue({ @@ -39,6 +44,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => { beforeEach(() => { vi.clearAllMocks(); process.env = { ...originalEnv }; + clearQoderAuthMemCache(); originalAuth = existsSync(AUTH_FILE) ? readFileSync(AUTH_FILE, "utf8") : undefined; }); @@ -46,6 +52,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => { process.env = originalEnv; if (originalAuth === undefined) rmSync(AUTH_FILE, { force: true }); else writeFileSync(AUTH_FILE, originalAuth, "utf8"); + clearQoderAuthMemCache(); }); it("extracts PAT correctly from env for global and CN mode", () => { diff --git a/src/auth/oauth.ts b/src/auth/oauth.ts index 448722e..b5326f7 100644 --- a/src/auth/oauth.ts +++ b/src/auth/oauth.ts @@ -16,8 +16,6 @@ export interface QoderCredentials extends OAuthCredentials { machineID: string; } -const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json"); - /** * `AuthStorage` is not part of every pi-coding-agent release's public exports, * so it is read off the module namespace instead of imported by name: a missing @@ -29,6 +27,45 @@ const AuthStorage = ( } ).AuthStorage; +const identityCache = new Map(); + +function getHomeDir(): string { + return process.env.HOME || process.env.USERPROFILE || homedir(); +} + +function getAuthFilePath(): string { + return join(getHomeDir(), ".pi", "agent", "auth.json"); +} + +/** Memoized parse of auth.json; invalidated on save. undefined = not loaded. */ +let authFileMem: { path: string; data: Record } | null | undefined; + +/** Clear process-memory auth caches (used by tests that mutate auth.json). */ +export function clearQoderAuthMemCache(): void { + authFileMem = undefined; + identityCache.clear(); +} + +function readAuthFileCached(): Record | null { + const authPath = getAuthFilePath(); + if (authFileMem !== undefined) { + if (authFileMem === null) return null; + if (authFileMem.path === authPath) return authFileMem.data; + } + if (!existsSync(authPath)) { + authFileMem = null; + return null; + } + try { + const data = JSON.parse(readFileSync(authPath, "utf-8")) as Record; + authFileMem = { path: authPath, data }; + return data; + } catch { + authFileMem = null; + return null; + } +} + /** Return the PAT exposed through the environment for a provider mode. */ export function getQoderPatForMode(mode: QoderMode): string { for (const envName of getQoderRegionConfig(mode).patEnvNames) { @@ -40,18 +77,20 @@ export function getQoderPatForMode(mode: QoderMode): string { function saveCredentialsToAuthFile(providerID: string, credentials: OAuthCredentials): void { try { - const dir = dirname(AUTH_FILE); + const authPath = getAuthFilePath(); + const dir = dirname(authPath); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true, mode: 0o700 }); } - let auth: Record = {}; - if (existsSync(AUTH_FILE)) { - try { - auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8")); - } catch {} - } + const existing = readAuthFileCached(); + const auth: Record = existing ? { ...existing } : {}; auth[providerID] = { type: "oauth", ...credentials }; - writeFileSync(AUTH_FILE, JSON.stringify(auth, null, 2), { encoding: "utf-8", mode: 0o600 }); + writeFileSync(authPath, JSON.stringify(auth, null, 2), { encoding: "utf-8", mode: 0o600 }); + authFileMem = { path: authPath, data: auth }; + const q = credentials as QoderCredentials; + if (q.access && q.userID) { + identityCache.set(`${providerID}:${q.access}`, q); + } } catch (err) { console.error(`[pi-provider-qoder] Failed to write auth storage for ${providerID}:`, err); } @@ -94,20 +133,18 @@ export async function autoLoginQoderFromEnvironment(providerID: string, mode: Qo * This is best-effort and falls back to null so callers can use placeholders. */ export function getCachedCredentials(_accessToken: string, providerID = "qoder"): QoderCredentials | null { - if (existsSync(AUTH_FILE)) { - try { - const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8")); - const creds = auth?.[providerID] || (providerID === "qoder" ? auth?.qoder : null); - if (creds?.userID || creds?.access) { - return creds as QoderCredentials; - } - } catch {} + const auth = readAuthFileCached(); + if (!auth) return null; + const creds = (auth[providerID] || (providerID === "qoder" ? auth.qoder : null)) as QoderCredentials | null; + if (creds?.userID || creds?.access) { + if (creds.access && creds.userID) { + identityCache.set(`${providerID}:${creds.access}`, creds); + } + return creds; } return null; } -const identityCache = new Map(); - /** * Resolve the Qoder identity (userID/email/name/machineID) for a chat request. * OMP (17.x) persists login credentials in its own agent.db, not in @@ -122,13 +159,16 @@ export async function resolveQoderIdentity( mode: QoderMode, ): Promise { const region = getQoderRegionConfig(mode); - const cached = getCachedCredentials(accessToken, providerID); - if (cached?.userID) return cached; - const cacheKey = `${providerID}:${accessToken}`; const mem = identityCache.get(cacheKey); if (mem?.userID) return mem; + const cached = getCachedCredentials(accessToken, providerID); + if (cached?.userID) { + identityCache.set(cacheKey, cached); + return cached; + } + const info = await fetchUserInfo(accessToken, mode); const machineID = getMachineId(); const creds: QoderCredentials = { diff --git a/src/catalog.ts b/src/catalog.ts index 38c2b03..8c0b3e0 100644 --- a/src/catalog.ts +++ b/src/catalog.ts @@ -64,8 +64,54 @@ export interface QoderModelDef { description?: string; } +function getHomeDir(): string { + // Prefer process.env.HOME so vitest setup can isolate caches. Node 26+ caches + // os.homedir() from process start, ignoring later HOME changes. + return process.env.HOME || process.env.USERPROFILE || homedir(); +} + function getQoderCachePath(mode: QoderMode): string { - return join(homedir(), ".pi", "agent", getQoderRegionConfig(mode).modelCacheFile); + return join(getHomeDir(), ".pi", "agent", getQoderRegionConfig(mode).modelCacheFile); +} + +interface ParsedModelCache { + updatedAt?: number; + models?: QoderModelDef[]; + configs?: Record; +} + +/** In-memory cache keyed by absolute cache path (HOME-safe across tests). */ +const modelCacheMem = new Map(); + +/** Clear process-memory model caches (also used by tests that mutate cache files). */ +export function clearQoderModelsMemCache(): void { + modelCacheMem.clear(); +} + +function readParsedModelCache(mode: QoderMode): ParsedModelCache | null { + const cachePath = getQoderCachePath(mode); + if (modelCacheMem.has(cachePath)) { + return modelCacheMem.get(cachePath) ?? null; + } + if (!existsSync(cachePath)) { + modelCacheMem.set(cachePath, null); + return null; + } + try { + const data = JSON.parse(readFileSync(cachePath, "utf8")) as ParsedModelCache; + modelCacheMem.set(cachePath, data); + return data; + } catch { + modelCacheMem.set(cachePath, null); + return null; + } +} + +function writeParsedModelCache(mode: QoderMode, data: ParsedModelCache): void { + const cachePath = getQoderCachePath(mode); + mkdirSync(dirname(cachePath), { recursive: true }); + writeFileSync(cachePath, JSON.stringify(data, null, 2), "utf-8"); + modelCacheMem.set(cachePath, data); } /** @@ -480,52 +526,42 @@ function buildThinkingLevelMap(entry: QoderModelEntry): ThinkingLevelMap | undef } export function getCachedModels(mode: QoderMode): QoderModelDef[] { - const cachePath = getQoderCachePath(mode); - if (existsSync(cachePath)) { - try { - const data = JSON.parse(readFileSync(cachePath, "utf8")); - if (data && Array.isArray(data.models)) { - const models = data.models.map((model: QoderModelDef) => { - const config = data.configs?.[model.id] as QoderModelEntry | undefined; - const display = config?.display_name; - const staticModel = (mode === "cn" ? staticCnModels : staticModels).find( - (seed) => seed.upstreamKey === model.id, - ); - if (display) return { ...model, id: toQoderModelId(display), name: display }; - if (staticModel) return { ...model, id: staticModel.id, name: staticModel.name }; - return model.name ? { ...model, id: toQoderModelId(model.name) } : model; - }); - // Older releases injected `auto` without a corresponding service config. - // Keep an explicitly enabled service model, but drop the legacy fallback. - if (data.configs && typeof data.configs === "object" && !data.configs.auto) { - return models.filter((model: QoderModelDef) => model.id.toLowerCase() !== "auto"); - } - return models; - } - } catch {} + const data = readParsedModelCache(mode); + if (data && Array.isArray(data.models)) { + const models = data.models.map((model: QoderModelDef) => { + const config = data.configs?.[model.id] as QoderModelEntry | undefined; + const display = config?.display_name; + const staticModel = (mode === "cn" ? staticCnModels : staticModels).find((seed) => seed.upstreamKey === model.id); + if (display) return { ...model, id: toQoderModelId(display), name: display }; + if (staticModel) return { ...model, id: staticModel.id, name: staticModel.name }; + return model.name ? { ...model, id: toQoderModelId(model.name) } : model; + }); + // Older releases injected `auto` without a corresponding service config. + // Keep an explicitly enabled service model, but drop the legacy fallback. + if (data.configs && typeof data.configs === "object" && !data.configs.auto) { + return models.filter((model: QoderModelDef) => model.id.toLowerCase() !== "auto"); + } + return models; } return mode === "cn" ? staticCnModels : staticModels; } export function getCachedModelConfig(modelId: string, mode: QoderMode): QoderModelEntry | null { - const cachePath = getQoderCachePath(mode); - if (existsSync(cachePath)) { - try { - const data = JSON.parse(readFileSync(cachePath, "utf8")); - const direct = data?.configs?.[modelId] as QoderModelEntry | undefined; - if (direct && toQoderModelId(direct.display_name) === modelId) { - return withMaxContextAsDefault(direct); - } + const data = readParsedModelCache(mode); + if (data) { + const direct = data.configs?.[modelId] as QoderModelEntry | undefined; + if (direct && toQoderModelId(direct.display_name) === modelId) { + return withMaxContextAsDefault(direct); + } - // Read old cache shapes without preserving their raw-key aliases. - const legacyEntry = Object.values(data?.configs || {}).find( - (entry) => - entry && typeof entry === "object" && toQoderModelId((entry as QoderModelEntry).display_name) === modelId, - ) as QoderModelEntry | undefined; - if (legacyEntry) { - return withMaxContextAsDefault(legacyEntry); - } - } catch {} + // Read old cache shapes without preserving their raw-key aliases. + const legacyEntry = Object.values(data.configs || {}).find( + (entry) => + entry && typeof entry === "object" && toQoderModelId((entry as QoderModelEntry).display_name) === modelId, + ) as QoderModelEntry | undefined; + if (legacyEntry) { + return withMaxContextAsDefault(legacyEntry); + } } const staticModel = (mode === "cn" ? staticCnModels : staticModels).find((model) => model.id === modelId); @@ -577,16 +613,10 @@ function withMaxContextAsDefault(entry: QoderModelEntry): QoderModelEntry { } export function isCacheStale(mode: QoderMode): boolean { - const cachePath = getQoderCachePath(mode); - if (!existsSync(cachePath)) return true; - try { - const data = JSON.parse(readFileSync(cachePath, "utf8")); - if (!data || typeof data.updatedAt !== "number") return true; - // Stale if older than 1 hour - return Date.now() - data.updatedAt > 3600_000; - } catch { - return true; - } + const data = readParsedModelCache(mode); + if (!data || typeof data.updatedAt !== "number") return true; + // Stale if older than 1 hour + return Date.now() - data.updatedAt > 3600_000; } export async function updateQoderModelsCache( @@ -670,8 +700,6 @@ export async function updateQoderModelsCache( configs, }; - const cachePath = getQoderCachePath(mode); - mkdirSync(dirname(cachePath), { recursive: true }); - writeFileSync(cachePath, JSON.stringify(cacheData, null, 2), "utf-8"); + writeParsedModelCache(mode, cacheData); } catch {} } diff --git a/src/cosy.ts b/src/cosy.ts index ed3d53f..8949295 100644 --- a/src/cosy.ts +++ b/src/cosy.ts @@ -76,8 +76,13 @@ function computeSigPath(urlStr: string): string { return sigPath; } +function getHomeDir(): string { + return process.env.HOME || process.env.USERPROFILE || homedir(); +} + export function getMachineId(): string { - const paths = [join(homedir(), ".qoder", ".auth", "machine_id"), join(homedir(), ".pi", "agent", "qoder-machine-id")]; + const home = getHomeDir(); + const paths = [join(home, ".qoder", ".auth", "machine_id"), join(home, ".pi", "agent", "qoder-machine-id")]; for (const p of paths) { if (existsSync(p)) { try { @@ -133,15 +138,25 @@ export function buildAuthHeaders( const payloadB64 = Buffer.from(JSON.stringify(cosyPayload)).toString("base64"); const sigPath = computeSigPath(requestURL); - const bodyStr = body ? (Buffer.isBuffer(body) ? body.toString("utf8") : body) : ""; - const sigInput = `${payloadB64}\n${cosyKey}\n${timestamp}\n${bodyStr}\n${sigPath}`; - const sig = crypto.createHash("md5").update(sigInput).digest("hex"); - - const bodyHash = crypto + // Feed MD5 in the same byte order as the legacy template string: + // payloadB64 + "\n" + cosyKey + "\n" + timestamp + "\n" + bodyBytes + "\n" + sigPath + // without concatenating the full body into a string. + const bodyBytes = body ? (Buffer.isBuffer(body) ? body : Buffer.from(body)) : Buffer.alloc(0); + const sig = crypto .createHash("md5") - .update(body || "") + .update(payloadB64) + .update("\n") + .update(cosyKey) + .update("\n") + .update(timestamp) + .update("\n") + .update(bodyBytes) + .update("\n") + .update(sigPath) .digest("hex"); - const bodyLen = body ? (Buffer.isBuffer(body) ? body.length : Buffer.from(body).length).toString() : "0"; + + const bodyHash = crypto.createHash("md5").update(bodyBytes).digest("hex"); + const bodyLen = bodyBytes.length.toString(); const machineID = creds.machineID || getMachineId(); diff --git a/src/protocol/encoding.ts b/src/protocol/encoding.ts index daa2a26..ed83f63 100644 --- a/src/protocol/encoding.ts +++ b/src/protocol/encoding.ts @@ -15,11 +15,14 @@ for (let i = 0; i < qoderStdAlphabet.length; i++) { // Qoder uses "$" instead of standard Base64 "=" padding. encodeTable["=".charCodeAt(0)] = "$".charCodeAt(0); -export function qoderEncodeBody(plaintext: string | Buffer): string { +/** + * Encode a request body with Qoder's custom Base64 alphabet and block reorder. + * Returns a Buffer of ASCII bytes identical to the previous string encoding. + */ +export function qoderEncodeBody(plaintext: string | Buffer): Buffer { const std = Buffer.isBuffer(plaintext) ? plaintext.toString("base64") : Buffer.from(plaintext).toString("base64"); - const src = Buffer.from(std, "ascii"); - const n = src.length; + const n = std.length; const a = Math.floor(n / 3); const out = Buffer.allocUnsafe(n); @@ -28,19 +31,19 @@ export function qoderEncodeBody(plaintext: string | Buffer): string { // Equivalent to: // std.slice(n - a) + std.slice(a, n - a) + std.slice(0, a) // - // Translate directly into the output buffer to avoid building a large - // rearranged string and repeatedly concatenating one-character strings. + // Translate directly into the output buffer via charCodeAt to avoid an + // intermediate Buffer.from(std, "ascii") copy of the base64 string. for (let i = n - a; i < n; i++) { - out[dst++] = encodeTable[src[i]]; + out[dst++] = encodeTable[std.charCodeAt(i)]; } for (let i = a; i < n - a; i++) { - out[dst++] = encodeTable[src[i]]; + out[dst++] = encodeTable[std.charCodeAt(i)]; } for (let i = 0; i < a; i++) { - out[dst++] = encodeTable[src[i]]; + out[dst++] = encodeTable[std.charCodeAt(i)]; } - return out.toString("ascii"); + return out; } diff --git a/src/protocol/stream.ts b/src/protocol/stream.ts index 2dfadc8..a285abf 100644 --- a/src/protocol/stream.ts +++ b/src/protocol/stream.ts @@ -268,8 +268,7 @@ export function streamQoder( }; const bodyBytes = Buffer.from(JSON.stringify(reqBody)); - const encodedBody = qoderEncodeBody(bodyBytes); - const encodedBytes = Buffer.from(encodedBody, "utf8"); + const encodedBytes = qoderEncodeBody(bodyBytes); const chatURL = getQoderChatURL(providerMode); @@ -294,7 +293,7 @@ export function streamQoder( "X-Model-Source": modelSource, ...headers, }, - body: encodedBytes, + body: encodedBytes as unknown as BodyInit, signal: options?.signal, }); @@ -307,6 +306,7 @@ export function streamQoder( if (!reader) throw new Error("No response body"); const decoder = new TextDecoder(); let buffer = ""; + let bufferStart = 0; let contentBlockIndex = -1; let thinkingBlockIndex = -1; @@ -328,14 +328,20 @@ export function streamQoder( const { done, value } = await reader.read(); if (done) break; + // Drop consumed prefix before appending so we do not keep growing a + // dead head of the string across chunks. + if (bufferStart > 0) { + buffer = buffer.substring(bufferStart); + bufferStart = 0; + } buffer += decoder.decode(value, { stream: true }); while (true) { - const lineEnd = buffer.indexOf("\n"); + const lineEnd = buffer.indexOf("\n", bufferStart); if (lineEnd === -1) break; - const line = buffer.substring(0, lineEnd).trim(); - buffer = buffer.substring(lineEnd + 1); + const line = buffer.substring(bufferStart, lineEnd).trim(); + bufferStart = lineEnd + 1; if (!line.startsWith("data:")) continue; diff --git a/src/protocol/thinking.ts b/src/protocol/thinking.ts index a939d01..58e08a9 100644 --- a/src/protocol/thinking.ts +++ b/src/protocol/thinking.ts @@ -12,6 +12,8 @@ export const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [ { open: "", close: "" }, ]; +const ALL_THINKING_TAGS: string[] = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]); + function getTrailingPossibleTagPrefixLength(text: string, tag: string): number { const maxPrefixLength = Math.min(text.length, tag.length - 1); for (let len = maxPrefixLength; len > 0; len--) { @@ -154,8 +156,7 @@ export class ThinkingTagParser { // No complete tag yet. Hold back any trailing prefix that could be the // start of an opener OR a closer, so a tag split across stream deltas is // not partially emitted as text. - const allTags = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]); - const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, allTags); + const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, ALL_THINKING_TAGS); const safeLen = this.textBuffer.length - trailingPrefixLength; if (safeLen > 0) { this.emitText(this.textBuffer.slice(0, safeLen));