perf: cut hot-path body copies, Cosy hashing, and per-request disk reads
This commit is contained in:
@@ -2,21 +2,27 @@ import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|||||||
import { homedir } from "node:os";
|
import { homedir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
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";
|
import { loadLiveFixture, responseFromFixture } from "./live-fixture.js";
|
||||||
|
|
||||||
|
function testHome(): string {
|
||||||
|
return process.env.HOME || process.env.USERPROFILE || homedir();
|
||||||
|
}
|
||||||
|
|
||||||
const CACHE_PATHS = {
|
const CACHE_PATHS = {
|
||||||
global: join(homedir(), ".pi", "agent", "qoder-models-cache.json"),
|
global: join(testHome(), ".pi", "agent", "qoder-models-cache.json"),
|
||||||
cn: join(homedir(), ".pi", "agent", "qoder-cn-models-cache.json"),
|
cn: join(testHome(), ".pi", "agent", "qoder-cn-models-cache.json"),
|
||||||
};
|
};
|
||||||
let originalCaches: Record<keyof typeof CACHE_PATHS, string | undefined>;
|
let originalCaches: Record<keyof typeof CACHE_PATHS, string | undefined>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
clearQoderModelsMemCache();
|
||||||
originalCaches = {
|
originalCaches = {
|
||||||
global: existsSync(CACHE_PATHS.global) ? readFileSync(CACHE_PATHS.global, "utf8") : undefined,
|
global: existsSync(CACHE_PATHS.global) ? readFileSync(CACHE_PATHS.global, "utf8") : undefined,
|
||||||
cn: existsSync(CACHE_PATHS.cn) ? readFileSync(CACHE_PATHS.cn, "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 });
|
for (const path of Object.values(CACHE_PATHS)) rmSync(path, { force: true });
|
||||||
|
clearQoderModelsMemCache();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -27,6 +33,7 @@ afterEach(() => {
|
|||||||
if (original === undefined) rmSync(path, { force: true });
|
if (original === undefined) rmSync(path, { force: true });
|
||||||
else writeFileSync(path, original, "utf8");
|
else writeFileSync(path, original, "utf8");
|
||||||
}
|
}
|
||||||
|
clearQoderModelsMemCache();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Qoder model cache", () => {
|
describe("Qoder model cache", () => {
|
||||||
@@ -145,6 +152,7 @@ describe("Qoder model cache", () => {
|
|||||||
}),
|
}),
|
||||||
"utf8",
|
"utf8",
|
||||||
);
|
);
|
||||||
|
clearQoderModelsMemCache();
|
||||||
|
|
||||||
expect(getCachedModels("global").map((model) => model.id)).toEqual(["Ultimate"]);
|
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"));
|
const cache = JSON.parse(readFileSync(CACHE_PATHS.global, "utf8"));
|
||||||
expect(cache.models[0].contextWindow).toBe(200000);
|
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"]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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";
|
import { buildAuthHeaders } from "../cosy.js";
|
||||||
|
|
||||||
describe("COSY client identity", () => {
|
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 };
|
const payload = JSON.parse(Buffer.from(payloadB64, "base64").toString("utf8")) as { cosyVersion: string };
|
||||||
expect(payload.cosyVersion).toBe("1.1.38");
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,70 +1,106 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { qoderEncodeBody } from "../protocol/encoding.js";
|
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", () => {
|
describe("qoderEncodeBody", () => {
|
||||||
it("encodes a simple string", () => {
|
it("encodes a simple string", () => {
|
||||||
const result = qoderEncodeBody("hello");
|
const result = qoderEncodeBody("hello");
|
||||||
expect(result).toBeTruthy();
|
expect(Buffer.isBuffer(result)).toBe(true);
|
||||||
expect(typeof result).toBe("string");
|
expect(result.toString("ascii")).toBeTruthy();
|
||||||
// Should not contain standard base64 padding char '='
|
expect(result.toString("ascii")).not.toContain("=");
|
||||||
expect(result).not.toContain("=");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("encodes a Buffer", () => {
|
it("encodes a Buffer", () => {
|
||||||
const buf = Buffer.from("hello world");
|
const buf = Buffer.from("hello world");
|
||||||
const result = qoderEncodeBody(buf);
|
const result = qoderEncodeBody(buf);
|
||||||
expect(result).toBeTruthy();
|
expect(result.toString("ascii")).toBeTruthy();
|
||||||
expect(result).not.toContain("=");
|
expect(result.toString("ascii")).not.toContain("=");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("produces deterministic output", () => {
|
it("produces deterministic output", () => {
|
||||||
const a = qoderEncodeBody("test input");
|
const a = qoderEncodeBody("test input");
|
||||||
const b = 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", () => {
|
it("produces different output for different inputs", () => {
|
||||||
const a = qoderEncodeBody("input A");
|
const a = qoderEncodeBody("input A");
|
||||||
const b = qoderEncodeBody("input B");
|
const b = qoderEncodeBody("input B");
|
||||||
expect(a).not.toBe(b);
|
expect(a.equals(b)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles empty string", () => {
|
it("handles empty string", () => {
|
||||||
const result = qoderEncodeBody("");
|
const result = qoderEncodeBody("");
|
||||||
expect(result).toBe("");
|
expect(result.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles empty Buffer", () => {
|
it("handles empty Buffer", () => {
|
||||||
const result = qoderEncodeBody(Buffer.alloc(0));
|
const result = qoderEncodeBody(Buffer.alloc(0));
|
||||||
expect(result).toBe("");
|
expect(result.length).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("replaces '=' padding with '$'", () => {
|
it("replaces '=' padding with '$'", () => {
|
||||||
// Base64 of "a" is "YQ==" which has padding — our encoding should use $
|
const result = qoderEncodeBody("a").toString("ascii");
|
||||||
const result = qoderEncodeBody("a");
|
|
||||||
expect(result).not.toContain("=");
|
expect(result).not.toContain("=");
|
||||||
expect(result).toContain("$");
|
expect(result).toContain("$");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses custom alphabet (not standard base64)", () => {
|
it("uses custom alphabet (not standard base64)", () => {
|
||||||
const result = qoderEncodeBody("The quick brown fox");
|
const result = qoderEncodeBody("The quick brown fox").toString("ascii");
|
||||||
// Standard base64 would use A-Za-z0-9+/=
|
|
||||||
// Our encoding uses a custom alphabet, so the output should differ
|
|
||||||
const stdBase64 = Buffer.from("The quick brown fox").toString("base64");
|
const stdBase64 = Buffer.from("The quick brown fox").toString("base64");
|
||||||
expect(result).not.toBe(stdBase64);
|
expect(result).not.toBe(stdBase64);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles binary content", () => {
|
it("handles binary content", () => {
|
||||||
const binary = Buffer.from([0x00, 0xff, 0x80, 0x7f, 0x01]);
|
const binary = Buffer.from([0x00, 0xff, 0x80, 0x7f, 0x01]);
|
||||||
const result = qoderEncodeBody(binary);
|
const result = qoderEncodeBody(binary).toString("ascii");
|
||||||
expect(result).toBeTruthy();
|
expect(result).toBeTruthy();
|
||||||
expect(result).not.toContain("=");
|
expect(result).not.toContain("=");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles JSON content", () => {
|
it("handles JSON content", () => {
|
||||||
const json = JSON.stringify({ key: "value", num: 42 });
|
const json = JSON.stringify({ key: "value", num: 42 });
|
||||||
const result = qoderEncodeBody(json);
|
const result = qoderEncodeBody(json).toString("ascii");
|
||||||
expect(result).toBeTruthy();
|
expect(result).toBeTruthy();
|
||||||
expect(result).not.toContain("=");
|
expect(result).not.toContain("=");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("is byte-identical to the legacy string encoder for several inputs", () => {
|
||||||
|
const samples: Array<string | Buffer> = [
|
||||||
|
"",
|
||||||
|
"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);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,12 +2,17 @@ import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|||||||
import { homedir } from "node:os";
|
import { homedir } from "node:os";
|
||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
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 { credentialsFromPat } from "../auth/pat.js";
|
||||||
import { updateQoderModelsCache } from "../catalog.js";
|
import { updateQoderModelsCache } from "../catalog.js";
|
||||||
import { loadLiveFixture } from "./live-fixture.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", () => ({
|
vi.mock("../auth/pat.js", () => ({
|
||||||
credentialsFromPat: vi.fn().mockResolvedValue({
|
credentialsFromPat: vi.fn().mockResolvedValue({
|
||||||
@@ -39,6 +44,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
process.env = { ...originalEnv };
|
process.env = { ...originalEnv };
|
||||||
|
clearQoderAuthMemCache();
|
||||||
originalAuth = existsSync(AUTH_FILE) ? readFileSync(AUTH_FILE, "utf8") : undefined;
|
originalAuth = existsSync(AUTH_FILE) ? readFileSync(AUTH_FILE, "utf8") : undefined;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -46,6 +52,7 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
|
|||||||
process.env = originalEnv;
|
process.env = originalEnv;
|
||||||
if (originalAuth === undefined) rmSync(AUTH_FILE, { force: true });
|
if (originalAuth === undefined) rmSync(AUTH_FILE, { force: true });
|
||||||
else writeFileSync(AUTH_FILE, originalAuth, "utf8");
|
else writeFileSync(AUTH_FILE, originalAuth, "utf8");
|
||||||
|
clearQoderAuthMemCache();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("extracts PAT correctly from env for global and CN mode", () => {
|
it("extracts PAT correctly from env for global and CN mode", () => {
|
||||||
|
|||||||
+63
-23
@@ -16,8 +16,6 @@ export interface QoderCredentials extends OAuthCredentials {
|
|||||||
machineID: string;
|
machineID: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `AuthStorage` is not part of every pi-coding-agent release's public exports,
|
* `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
|
* so it is read off the module namespace instead of imported by name: a missing
|
||||||
@@ -29,6 +27,45 @@ const AuthStorage = (
|
|||||||
}
|
}
|
||||||
).AuthStorage;
|
).AuthStorage;
|
||||||
|
|
||||||
|
const identityCache = new Map<string, QoderCredentials>();
|
||||||
|
|
||||||
|
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<string, unknown> } | 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<string, unknown> | 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<string, unknown>;
|
||||||
|
authFileMem = { path: authPath, data };
|
||||||
|
return data;
|
||||||
|
} catch {
|
||||||
|
authFileMem = null;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Return the PAT exposed through the environment for a provider mode. */
|
/** Return the PAT exposed through the environment for a provider mode. */
|
||||||
export function getQoderPatForMode(mode: QoderMode): string {
|
export function getQoderPatForMode(mode: QoderMode): string {
|
||||||
for (const envName of getQoderRegionConfig(mode).patEnvNames) {
|
for (const envName of getQoderRegionConfig(mode).patEnvNames) {
|
||||||
@@ -40,18 +77,20 @@ export function getQoderPatForMode(mode: QoderMode): string {
|
|||||||
|
|
||||||
function saveCredentialsToAuthFile(providerID: string, credentials: OAuthCredentials): void {
|
function saveCredentialsToAuthFile(providerID: string, credentials: OAuthCredentials): void {
|
||||||
try {
|
try {
|
||||||
const dir = dirname(AUTH_FILE);
|
const authPath = getAuthFilePath();
|
||||||
|
const dir = dirname(authPath);
|
||||||
if (!existsSync(dir)) {
|
if (!existsSync(dir)) {
|
||||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||||
}
|
}
|
||||||
let auth: Record<string, unknown> = {};
|
const existing = readAuthFileCached();
|
||||||
if (existsSync(AUTH_FILE)) {
|
const auth: Record<string, unknown> = existing ? { ...existing } : {};
|
||||||
try {
|
|
||||||
auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8"));
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
auth[providerID] = { type: "oauth", ...credentials };
|
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) {
|
} catch (err) {
|
||||||
console.error(`[pi-provider-qoder] Failed to write auth storage for ${providerID}:`, 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.
|
* This is best-effort and falls back to null so callers can use placeholders.
|
||||||
*/
|
*/
|
||||||
export function getCachedCredentials(_accessToken: string, providerID = "qoder"): QoderCredentials | null {
|
export function getCachedCredentials(_accessToken: string, providerID = "qoder"): QoderCredentials | null {
|
||||||
if (existsSync(AUTH_FILE)) {
|
const auth = readAuthFileCached();
|
||||||
try {
|
if (!auth) return null;
|
||||||
const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8"));
|
const creds = (auth[providerID] || (providerID === "qoder" ? auth.qoder : null)) as QoderCredentials | null;
|
||||||
const creds = auth?.[providerID] || (providerID === "qoder" ? auth?.qoder : null);
|
if (creds?.userID || creds?.access) {
|
||||||
if (creds?.userID || creds?.access) {
|
if (creds.access && creds.userID) {
|
||||||
return creds as QoderCredentials;
|
identityCache.set(`${providerID}:${creds.access}`, creds);
|
||||||
}
|
}
|
||||||
} catch {}
|
return creds;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const identityCache = new Map<string, QoderCredentials>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve the Qoder identity (userID/email/name/machineID) for a chat request.
|
* 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
|
* OMP (17.x) persists login credentials in its own agent.db, not in
|
||||||
@@ -122,13 +159,16 @@ export async function resolveQoderIdentity(
|
|||||||
mode: QoderMode,
|
mode: QoderMode,
|
||||||
): Promise<QoderCredentials> {
|
): Promise<QoderCredentials> {
|
||||||
const region = getQoderRegionConfig(mode);
|
const region = getQoderRegionConfig(mode);
|
||||||
const cached = getCachedCredentials(accessToken, providerID);
|
|
||||||
if (cached?.userID) return cached;
|
|
||||||
|
|
||||||
const cacheKey = `${providerID}:${accessToken}`;
|
const cacheKey = `${providerID}:${accessToken}`;
|
||||||
const mem = identityCache.get(cacheKey);
|
const mem = identityCache.get(cacheKey);
|
||||||
if (mem?.userID) return mem;
|
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 info = await fetchUserInfo(accessToken, mode);
|
||||||
const machineID = getMachineId();
|
const machineID = getMachineId();
|
||||||
const creds: QoderCredentials = {
|
const creds: QoderCredentials = {
|
||||||
|
|||||||
+82
-54
@@ -64,8 +64,54 @@ export interface QoderModelDef {
|
|||||||
description?: string;
|
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 {
|
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<string, QoderModelEntry>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** In-memory cache keyed by absolute cache path (HOME-safe across tests). */
|
||||||
|
const modelCacheMem = new Map<string, ParsedModelCache | null>();
|
||||||
|
|
||||||
|
/** 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[] {
|
export function getCachedModels(mode: QoderMode): QoderModelDef[] {
|
||||||
const cachePath = getQoderCachePath(mode);
|
const data = readParsedModelCache(mode);
|
||||||
if (existsSync(cachePath)) {
|
if (data && Array.isArray(data.models)) {
|
||||||
try {
|
const models = data.models.map((model: QoderModelDef) => {
|
||||||
const data = JSON.parse(readFileSync(cachePath, "utf8"));
|
const config = data.configs?.[model.id] as QoderModelEntry | undefined;
|
||||||
if (data && Array.isArray(data.models)) {
|
const display = config?.display_name;
|
||||||
const models = data.models.map((model: QoderModelDef) => {
|
const staticModel = (mode === "cn" ? staticCnModels : staticModels).find((seed) => seed.upstreamKey === model.id);
|
||||||
const config = data.configs?.[model.id] as QoderModelEntry | undefined;
|
if (display) return { ...model, id: toQoderModelId(display), name: display };
|
||||||
const display = config?.display_name;
|
if (staticModel) return { ...model, id: staticModel.id, name: staticModel.name };
|
||||||
const staticModel = (mode === "cn" ? staticCnModels : staticModels).find(
|
return model.name ? { ...model, id: toQoderModelId(model.name) } : model;
|
||||||
(seed) => seed.upstreamKey === model.id,
|
});
|
||||||
);
|
// Older releases injected `auto` without a corresponding service config.
|
||||||
if (display) return { ...model, id: toQoderModelId(display), name: display };
|
// Keep an explicitly enabled service model, but drop the legacy fallback.
|
||||||
if (staticModel) return { ...model, id: staticModel.id, name: staticModel.name };
|
if (data.configs && typeof data.configs === "object" && !data.configs.auto) {
|
||||||
return model.name ? { ...model, id: toQoderModelId(model.name) } : model;
|
return models.filter((model: QoderModelDef) => model.id.toLowerCase() !== "auto");
|
||||||
});
|
}
|
||||||
// Older releases injected `auto` without a corresponding service config.
|
return models;
|
||||||
// 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 {}
|
|
||||||
}
|
}
|
||||||
return mode === "cn" ? staticCnModels : staticModels;
|
return mode === "cn" ? staticCnModels : staticModels;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCachedModelConfig(modelId: string, mode: QoderMode): QoderModelEntry | null {
|
export function getCachedModelConfig(modelId: string, mode: QoderMode): QoderModelEntry | null {
|
||||||
const cachePath = getQoderCachePath(mode);
|
const data = readParsedModelCache(mode);
|
||||||
if (existsSync(cachePath)) {
|
if (data) {
|
||||||
try {
|
const direct = data.configs?.[modelId] as QoderModelEntry | undefined;
|
||||||
const data = JSON.parse(readFileSync(cachePath, "utf8"));
|
if (direct && toQoderModelId(direct.display_name) === modelId) {
|
||||||
const direct = data?.configs?.[modelId] as QoderModelEntry | undefined;
|
return withMaxContextAsDefault(direct);
|
||||||
if (direct && toQoderModelId(direct.display_name) === modelId) {
|
}
|
||||||
return withMaxContextAsDefault(direct);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read old cache shapes without preserving their raw-key aliases.
|
// Read old cache shapes without preserving their raw-key aliases.
|
||||||
const legacyEntry = Object.values(data?.configs || {}).find(
|
const legacyEntry = Object.values(data.configs || {}).find(
|
||||||
(entry) =>
|
(entry) =>
|
||||||
entry && typeof entry === "object" && toQoderModelId((entry as QoderModelEntry).display_name) === modelId,
|
entry && typeof entry === "object" && toQoderModelId((entry as QoderModelEntry).display_name) === modelId,
|
||||||
) as QoderModelEntry | undefined;
|
) as QoderModelEntry | undefined;
|
||||||
if (legacyEntry) {
|
if (legacyEntry) {
|
||||||
return withMaxContextAsDefault(legacyEntry);
|
return withMaxContextAsDefault(legacyEntry);
|
||||||
}
|
}
|
||||||
} catch {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const staticModel = (mode === "cn" ? staticCnModels : staticModels).find((model) => model.id === modelId);
|
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 {
|
export function isCacheStale(mode: QoderMode): boolean {
|
||||||
const cachePath = getQoderCachePath(mode);
|
const data = readParsedModelCache(mode);
|
||||||
if (!existsSync(cachePath)) return true;
|
if (!data || typeof data.updatedAt !== "number") return true;
|
||||||
try {
|
// Stale if older than 1 hour
|
||||||
const data = JSON.parse(readFileSync(cachePath, "utf8"));
|
return Date.now() - data.updatedAt > 3600_000;
|
||||||
if (!data || typeof data.updatedAt !== "number") return true;
|
|
||||||
// Stale if older than 1 hour
|
|
||||||
return Date.now() - data.updatedAt > 3600_000;
|
|
||||||
} catch {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateQoderModelsCache(
|
export async function updateQoderModelsCache(
|
||||||
@@ -670,8 +700,6 @@ export async function updateQoderModelsCache(
|
|||||||
configs,
|
configs,
|
||||||
};
|
};
|
||||||
|
|
||||||
const cachePath = getQoderCachePath(mode);
|
writeParsedModelCache(mode, cacheData);
|
||||||
mkdirSync(dirname(cachePath), { recursive: true });
|
|
||||||
writeFileSync(cachePath, JSON.stringify(cacheData, null, 2), "utf-8");
|
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-8
@@ -76,8 +76,13 @@ function computeSigPath(urlStr: string): string {
|
|||||||
return sigPath;
|
return sigPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getHomeDir(): string {
|
||||||
|
return process.env.HOME || process.env.USERPROFILE || homedir();
|
||||||
|
}
|
||||||
|
|
||||||
export function getMachineId(): string {
|
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) {
|
for (const p of paths) {
|
||||||
if (existsSync(p)) {
|
if (existsSync(p)) {
|
||||||
try {
|
try {
|
||||||
@@ -133,15 +138,25 @@ export function buildAuthHeaders(
|
|||||||
const payloadB64 = Buffer.from(JSON.stringify(cosyPayload)).toString("base64");
|
const payloadB64 = Buffer.from(JSON.stringify(cosyPayload)).toString("base64");
|
||||||
const sigPath = computeSigPath(requestURL);
|
const sigPath = computeSigPath(requestURL);
|
||||||
|
|
||||||
const bodyStr = body ? (Buffer.isBuffer(body) ? body.toString("utf8") : body) : "";
|
// Feed MD5 in the same byte order as the legacy template string:
|
||||||
const sigInput = `${payloadB64}\n${cosyKey}\n${timestamp}\n${bodyStr}\n${sigPath}`;
|
// payloadB64 + "\n" + cosyKey + "\n" + timestamp + "\n" + bodyBytes + "\n" + sigPath
|
||||||
const sig = crypto.createHash("md5").update(sigInput).digest("hex");
|
// without concatenating the full body into a string.
|
||||||
|
const bodyBytes = body ? (Buffer.isBuffer(body) ? body : Buffer.from(body)) : Buffer.alloc(0);
|
||||||
const bodyHash = crypto
|
const sig = crypto
|
||||||
.createHash("md5")
|
.createHash("md5")
|
||||||
.update(body || "")
|
.update(payloadB64)
|
||||||
|
.update("\n")
|
||||||
|
.update(cosyKey)
|
||||||
|
.update("\n")
|
||||||
|
.update(timestamp)
|
||||||
|
.update("\n")
|
||||||
|
.update(bodyBytes)
|
||||||
|
.update("\n")
|
||||||
|
.update(sigPath)
|
||||||
.digest("hex");
|
.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();
|
const machineID = creds.machineID || getMachineId();
|
||||||
|
|
||||||
|
|||||||
@@ -15,11 +15,14 @@ for (let i = 0; i < qoderStdAlphabet.length; i++) {
|
|||||||
// Qoder uses "$" instead of standard Base64 "=" padding.
|
// Qoder uses "$" instead of standard Base64 "=" padding.
|
||||||
encodeTable["=".charCodeAt(0)] = "$".charCodeAt(0);
|
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 std = Buffer.isBuffer(plaintext) ? plaintext.toString("base64") : Buffer.from(plaintext).toString("base64");
|
||||||
|
|
||||||
const src = Buffer.from(std, "ascii");
|
const n = std.length;
|
||||||
const n = src.length;
|
|
||||||
const a = Math.floor(n / 3);
|
const a = Math.floor(n / 3);
|
||||||
|
|
||||||
const out = Buffer.allocUnsafe(n);
|
const out = Buffer.allocUnsafe(n);
|
||||||
@@ -28,19 +31,19 @@ export function qoderEncodeBody(plaintext: string | Buffer): string {
|
|||||||
// Equivalent to:
|
// Equivalent to:
|
||||||
// std.slice(n - a) + std.slice(a, n - a) + std.slice(0, a)
|
// std.slice(n - a) + std.slice(a, n - a) + std.slice(0, a)
|
||||||
//
|
//
|
||||||
// Translate directly into the output buffer to avoid building a large
|
// Translate directly into the output buffer via charCodeAt to avoid an
|
||||||
// rearranged string and repeatedly concatenating one-character strings.
|
// intermediate Buffer.from(std, "ascii") copy of the base64 string.
|
||||||
for (let i = n - a; i < n; i++) {
|
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++) {
|
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++) {
|
for (let i = 0; i < a; i++) {
|
||||||
out[dst++] = encodeTable[src[i]];
|
out[dst++] = encodeTable[std.charCodeAt(i)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.toString("ascii");
|
return out;
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-6
@@ -268,8 +268,7 @@ export function streamQoder(
|
|||||||
};
|
};
|
||||||
|
|
||||||
const bodyBytes = Buffer.from(JSON.stringify(reqBody));
|
const bodyBytes = Buffer.from(JSON.stringify(reqBody));
|
||||||
const encodedBody = qoderEncodeBody(bodyBytes);
|
const encodedBytes = qoderEncodeBody(bodyBytes);
|
||||||
const encodedBytes = Buffer.from(encodedBody, "utf8");
|
|
||||||
|
|
||||||
const chatURL = getQoderChatURL(providerMode);
|
const chatURL = getQoderChatURL(providerMode);
|
||||||
|
|
||||||
@@ -294,7 +293,7 @@ export function streamQoder(
|
|||||||
"X-Model-Source": modelSource,
|
"X-Model-Source": modelSource,
|
||||||
...headers,
|
...headers,
|
||||||
},
|
},
|
||||||
body: encodedBytes,
|
body: encodedBytes as unknown as BodyInit,
|
||||||
signal: options?.signal,
|
signal: options?.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -307,6 +306,7 @@ export function streamQoder(
|
|||||||
if (!reader) throw new Error("No response body");
|
if (!reader) throw new Error("No response body");
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let buffer = "";
|
let buffer = "";
|
||||||
|
let bufferStart = 0;
|
||||||
|
|
||||||
let contentBlockIndex = -1;
|
let contentBlockIndex = -1;
|
||||||
let thinkingBlockIndex = -1;
|
let thinkingBlockIndex = -1;
|
||||||
@@ -328,14 +328,20 @@ export function streamQoder(
|
|||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
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 });
|
buffer += decoder.decode(value, { stream: true });
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const lineEnd = buffer.indexOf("\n");
|
const lineEnd = buffer.indexOf("\n", bufferStart);
|
||||||
if (lineEnd === -1) break;
|
if (lineEnd === -1) break;
|
||||||
|
|
||||||
const line = buffer.substring(0, lineEnd).trim();
|
const line = buffer.substring(bufferStart, lineEnd).trim();
|
||||||
buffer = buffer.substring(lineEnd + 1);
|
bufferStart = lineEnd + 1;
|
||||||
|
|
||||||
if (!line.startsWith("data:")) continue;
|
if (!line.startsWith("data:")) continue;
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ export const THINKING_TAG_VARIANTS: Array<{ open: string; close: string }> = [
|
|||||||
{ open: "<thought>", close: "</thought>" },
|
{ open: "<thought>", close: "</thought>" },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const ALL_THINKING_TAGS: string[] = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]);
|
||||||
|
|
||||||
function getTrailingPossibleTagPrefixLength(text: string, tag: string): number {
|
function getTrailingPossibleTagPrefixLength(text: string, tag: string): number {
|
||||||
const maxPrefixLength = Math.min(text.length, tag.length - 1);
|
const maxPrefixLength = Math.min(text.length, tag.length - 1);
|
||||||
for (let len = maxPrefixLength; len > 0; len--) {
|
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
|
// 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
|
// start of an opener OR a closer, so a tag split across stream deltas is
|
||||||
// not partially emitted as text.
|
// not partially emitted as text.
|
||||||
const allTags = THINKING_TAG_VARIANTS.flatMap((variant) => [variant.open, variant.close]);
|
const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, ALL_THINKING_TAGS);
|
||||||
const trailingPrefixLength = getMaxTrailingPossibleTagPrefixLength(this.textBuffer, allTags);
|
|
||||||
const safeLen = this.textBuffer.length - trailingPrefixLength;
|
const safeLen = this.textBuffer.length - trailingPrefixLength;
|
||||||
if (safeLen > 0) {
|
if (safeLen > 0) {
|
||||||
this.emitText(this.textBuffer.slice(0, safeLen));
|
this.emitText(this.textBuffer.slice(0, safeLen));
|
||||||
|
|||||||
Reference in New Issue
Block a user