fix: route PAT login through pi LoginDialog callbacks
The custom ctx.ui.custom login menu stole focus from pi's built-in LoginDialog, so onPrompt never received keystrokes and PAT login silently failed with 'No API key found'. Drive login via the provided onPrompt/onAuth/onProgress callbacks instead, and drop login-ui.ts and the now-unused pi-tui dependency. Also clears remaining lint warnings by removing any-casts in models/oauth/stream/transform.
This commit is contained in:
@@ -85,7 +85,6 @@ src/
|
||||
├── index.ts # Extension registration
|
||||
├── cosy.ts # COSY Signature and Machine ID resolver
|
||||
├── login.ts # OAuth Device Flow + PAT login sequence
|
||||
├── login-ui.ts # Custom TUI components for login
|
||||
├── pat.ts # PAT → job-token exchange + identity resolution
|
||||
├── models.ts # Model definitions and Dynamic Config Cache
|
||||
├── oauth.ts # PAT / OAuth callback orchestrator
|
||||
|
||||
Generated
-41
@@ -12,7 +12,6 @@
|
||||
"@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"
|
||||
}
|
||||
@@ -2647,20 +2646,6 @@
|
||||
"zod": "^3.25.28 || ^4"
|
||||
}
|
||||
},
|
||||
"node_modules/@earendil-works/pi-tui": {
|
||||
"version": "0.75.5",
|
||||
"resolved": "https://registry.npmjs.org/@earendil-works/pi-tui/-/pi-tui-0.75.5.tgz",
|
||||
"integrity": "sha512-LkXUM1/49pvzzeI39Y5wjBMlgafcCf67HCLhB9Z7yuXHy4XgT+VqxWcZVW5hBdhQsHZd0znjJotfGH1BzxMfiA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"get-east-asian-width": "1.6.0",
|
||||
"marked": "15.0.12"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22.19.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/aix-ppc64": {
|
||||
"version": "0.25.12",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.12.tgz",
|
||||
@@ -3633,19 +3618,6 @@
|
||||
"node": ">=18"
|
||||
}
|
||||
},
|
||||
"node_modules/get-east-asian-width": {
|
||||
"version": "1.6.0",
|
||||
"resolved": "https://registry.npmjs.org/get-east-asian-width/-/get-east-asian-width-1.6.0.tgz",
|
||||
"integrity": "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/google-auth-library": {
|
||||
"version": "10.7.0",
|
||||
"resolved": "https://registry.npmjs.org/google-auth-library/-/google-auth-library-10.7.0.tgz",
|
||||
@@ -3756,19 +3728,6 @@
|
||||
"dev": true,
|
||||
"license": "Apache-2.0"
|
||||
},
|
||||
"node_modules/marked": {
|
||||
"version": "15.0.12",
|
||||
"resolved": "https://registry.npmjs.org/marked/-/marked-15.0.12.tgz",
|
||||
"integrity": "sha512-8dD6FusOQSrpv9Z1rdNMdlSgQOIP880DHqnohobOmYLElGEqAL/JvxvuxZO16r4HtjTlfPRDC1hbvxC9dPN2nA==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"bin": {
|
||||
"marked": "bin/marked.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 18"
|
||||
}
|
||||
},
|
||||
"node_modules/ms": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
|
||||
|
||||
+1
-2
@@ -19,7 +19,7 @@
|
||||
"README.md"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "./node_modules/esbuild/bin/esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --external:@earendil-works/pi-ai --external:@earendil-works/pi-coding-agent --external:@earendil-works/pi-tui",
|
||||
"build": "./node_modules/esbuild/bin/esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js --external:@earendil-works/pi-ai --external:@earendil-works/pi-coding-agent",
|
||||
"check": "node node_modules/typescript/bin/tsc --noEmit",
|
||||
"lint": "node node_modules/@biomejs/biome/bin/biome check .",
|
||||
"lint:fix": "node node_modules/@biomejs/biome/bin/biome check --write .",
|
||||
@@ -36,7 +36,6 @@
|
||||
"@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"
|
||||
},
|
||||
|
||||
+28
-26
@@ -1,39 +1,41 @@
|
||||
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import { setExtensionContext } from "./login-ui.js";
|
||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
||||
import { getCachedModels, staticModels } from "./models.js";
|
||||
import { loginQoder, refreshQoderToken } from "./oauth.js";
|
||||
import { streamQoder } from "./stream.js";
|
||||
import { fetchQoderUsage } from "./usage.js";
|
||||
|
||||
// pi supports a `fetchUsage` hook on the oauth config at runtime, but it is not
|
||||
// part of the published ProviderConfig type. Declare the extension locally.
|
||||
type OAuthConfigWithUsage = NonNullable<ProviderConfig["oauth"]> & {
|
||||
fetchUsage: typeof fetchQoderUsage;
|
||||
};
|
||||
|
||||
export default function (pi: ExtensionAPI) {
|
||||
// Capture ctx for custom TUI login components
|
||||
pi.on("session_start", async (_event, ctx) => {
|
||||
setExtensionContext(ctx);
|
||||
});
|
||||
const oauth: OAuthConfigWithUsage = {
|
||||
name: "Qoder (Browser OAuth / PAT)",
|
||||
login: loginQoder,
|
||||
refreshToken: refreshQoderToken,
|
||||
getApiKey: (cred: OAuthCredentials) => cred.access,
|
||||
modifyModels: (models: Model<Api>[], _cred: OAuthCredentials) => {
|
||||
const cached = getCachedModels();
|
||||
const nonQoder = models.filter((m: Model<Api>) => m.provider !== "qoder");
|
||||
const modelsToUse = cached.length > 0 ? cached : staticModels;
|
||||
const modifiedQoder = modelsToUse.map((m) => ({
|
||||
...m,
|
||||
baseUrl: "https://api3.qoder.sh/",
|
||||
})) as Model<Api>[];
|
||||
|
||||
return [...nonQoder, ...modifiedQoder];
|
||||
},
|
||||
fetchUsage: fetchQoderUsage,
|
||||
};
|
||||
|
||||
pi.registerProvider("qoder", {
|
||||
baseUrl: "https://api3.qoder.sh/",
|
||||
api: "qoder-api" as any,
|
||||
models: getCachedModels(),
|
||||
oauth: {
|
||||
name: "Qoder (Browser OAuth / PAT)",
|
||||
login: loginQoder,
|
||||
refreshToken: refreshQoderToken,
|
||||
getApiKey: (cred: OAuthCredentials) => cred.access,
|
||||
modifyModels: (models: Model<Api>[], cred: OAuthCredentials) => {
|
||||
const cached = getCachedModels();
|
||||
const nonQoder = models.filter((m: Model<Api>) => m.provider !== "qoder");
|
||||
const modelsToUse = cached.length > 0 ? cached : staticModels;
|
||||
const modifiedQoder = modelsToUse.map((m: Model<Api>) => ({
|
||||
...m,
|
||||
baseUrl: "https://api3.qoder.sh/",
|
||||
}));
|
||||
|
||||
return [...nonQoder, ...modifiedQoder];
|
||||
},
|
||||
fetchUsage: fetchQoderUsage,
|
||||
} as any,
|
||||
api: "qoder-api" as Api,
|
||||
models: getCachedModels() as unknown as ProviderConfig["models"],
|
||||
oauth: oauth as ProviderConfig["oauth"],
|
||||
streamSimple: streamQoder,
|
||||
});
|
||||
}
|
||||
|
||||
-158
@@ -1,158 +0,0 @@
|
||||
import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
||||
import { DynamicBorder, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
||||
import { Container, type SelectItem, SelectList, Text } from "@earendil-works/pi-tui";
|
||||
|
||||
export type LoginChoice = { method: "web" } | { method: "pat" } | null; // null = cancelled
|
||||
|
||||
let _ctx: ExtensionContext | undefined;
|
||||
|
||||
export function setExtensionContext(ctx: ExtensionContext) {
|
||||
_ctx = ctx;
|
||||
}
|
||||
|
||||
export function hasExtensionContext(): boolean {
|
||||
return _ctx !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the login selection UI using pi's native TUI components.
|
||||
* Returns the user's choice or null if cancelled.
|
||||
*/
|
||||
export async function showLoginUI(): Promise<LoginChoice> {
|
||||
if (!_ctx) return null;
|
||||
const ctx = _ctx;
|
||||
|
||||
return ctx.ui.custom<LoginChoice>((tui, theme, _kb, done) => {
|
||||
const mainItems: SelectItem[] = [
|
||||
{ value: "web", label: "Browser Login", description: "Sign in via browser (OAuth device flow)" },
|
||||
{ value: "pat", label: "Use API Key (PAT)", description: "Paste a Qoder Personal Access Token (pt-...)" },
|
||||
];
|
||||
|
||||
const container = new Container();
|
||||
const border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
||||
const title = new Text(theme.fg("accent", theme.bold("Qoder Login")), 1, 0);
|
||||
const hint = new Text(theme.fg("dim", "↑↓ navigate • enter select • esc cancel"), 1, 0);
|
||||
const borderBottom = new DynamicBorder((s: string) => theme.fg("accent", s));
|
||||
|
||||
const selectList = new SelectList(mainItems, mainItems.length, {
|
||||
selectedPrefix: (t: string) => theme.fg("accent", t),
|
||||
selectedText: (t: string) => theme.fg("accent", t),
|
||||
description: (t: string) => theme.fg("muted", t),
|
||||
scrollInfo: (t: string) => theme.fg("dim", t),
|
||||
noMatch: (t: string) => theme.fg("warning", t),
|
||||
});
|
||||
|
||||
selectList.onSelect = (item) => {
|
||||
done({ method: item.value as "web" | "pat" });
|
||||
};
|
||||
selectList.onCancel = () => done(null);
|
||||
|
||||
container.addChild(border);
|
||||
container.addChild(title);
|
||||
container.addChild(selectList);
|
||||
container.addChild(hint);
|
||||
container.addChild(borderBottom);
|
||||
|
||||
tui.requestRender();
|
||||
|
||||
return {
|
||||
render(width: number) {
|
||||
return container.render(width);
|
||||
},
|
||||
invalidate() {
|
||||
container.invalidate();
|
||||
},
|
||||
handleInput(data: string) {
|
||||
selectList.handleInput(data);
|
||||
tui.requestRender();
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a waiting UI wrapper with an Escape return loop logic.
|
||||
* The user can press Escape or 'q' to abort the current login flow immediately.
|
||||
*/
|
||||
export async function showWaitingUI(
|
||||
outerCallbacks: OAuthLoginCallbacks,
|
||||
runAuth: (mergedCallbacks: OAuthLoginCallbacks) => Promise<OAuthCredentials>,
|
||||
): Promise<OAuthCredentials | null> {
|
||||
if (!_ctx) {
|
||||
return runAuth(outerCallbacks);
|
||||
}
|
||||
const ctx = _ctx;
|
||||
|
||||
return ctx.ui.custom<OAuthCredentials | null>((tui, theme, _kb, done) => {
|
||||
const container = new Container();
|
||||
const border = new DynamicBorder((s: string) => theme.fg("accent", s));
|
||||
const title = new Text(theme.fg("accent", theme.bold("Qoder Login - Authorization")), 1, 0);
|
||||
const borderBottom = new DynamicBorder((s: string) => theme.fg("accent", s));
|
||||
|
||||
const statusText = new Text("Initiating login flow...", 1, 0);
|
||||
const urlText = new Text("", 1, 0);
|
||||
const instructionsText = new Text("", 1, 0);
|
||||
const hint = new Text(theme.fg("dim", "esc cancel / back"), 1, 0);
|
||||
|
||||
container.addChild(border);
|
||||
container.addChild(title);
|
||||
container.addChild(statusText);
|
||||
container.addChild(urlText);
|
||||
container.addChild(instructionsText);
|
||||
container.addChild(hint);
|
||||
container.addChild(borderBottom);
|
||||
|
||||
const abortCtrl = new AbortController();
|
||||
let onAuthCalled = false;
|
||||
|
||||
const mergedCallbacks: OAuthLoginCallbacks = {
|
||||
...outerCallbacks,
|
||||
onProgress: (msg: string) => {
|
||||
outerCallbacks.onProgress?.(msg);
|
||||
statusText.setText(msg);
|
||||
tui.requestRender();
|
||||
},
|
||||
onAuth: (info: { url: string; instructions?: string }) => {
|
||||
if (!onAuthCalled) {
|
||||
onAuthCalled = true;
|
||||
outerCallbacks.onAuth?.(info);
|
||||
}
|
||||
urlText.setText(`URL: ${info.url}`);
|
||||
instructionsText.setText(info.instructions || "");
|
||||
tui.requestRender();
|
||||
},
|
||||
signal: abortCtrl.signal,
|
||||
};
|
||||
|
||||
runAuth(mergedCallbacks).then(
|
||||
(creds) => {
|
||||
done(creds);
|
||||
},
|
||||
(err) => {
|
||||
if (abortCtrl.signal.aborted) {
|
||||
done(null);
|
||||
} else {
|
||||
statusText.setText(theme.fg("warning", `Error: ${err.message || err}`));
|
||||
tui.requestRender();
|
||||
setTimeout(() => done(null), 3000);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
render(width: number) {
|
||||
return container.render(width);
|
||||
},
|
||||
invalidate() {
|
||||
container.invalidate();
|
||||
},
|
||||
handleInput(data: string) {
|
||||
// Escape key (standalone 0x1B) or 'q' to cancel
|
||||
if ((data.length === 1 && data.charCodeAt(0) === 0x1b) || data === "q") {
|
||||
abortCtrl.abort();
|
||||
done(null);
|
||||
}
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
+8
-32
@@ -1,7 +1,6 @@
|
||||
import crypto from "node:crypto";
|
||||
import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
||||
import { getMachineId } from "./cosy.js";
|
||||
import { hasExtensionContext, showLoginUI, showWaitingUI } from "./login-ui.js";
|
||||
import { credentialsFromPat } from "./pat.js";
|
||||
|
||||
type PromptFn = (p: { message: string; placeholder?: string; allowEmpty?: boolean }) => Promise<string>;
|
||||
@@ -38,29 +37,10 @@ function parseExpiresAt(s?: string, expiresInSeconds?: number): number {
|
||||
}
|
||||
|
||||
export async function interactiveLogin(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
||||
if (hasExtensionContext()) {
|
||||
const choice = await showLoginUI();
|
||||
if (!choice) {
|
||||
throw new Error("Login cancelled");
|
||||
}
|
||||
|
||||
if (choice.method === "pat") {
|
||||
return patLogin(callbacks);
|
||||
}
|
||||
|
||||
const runAuth = async (mergedCallbacks: OAuthLoginCallbacks) => {
|
||||
return runDeviceFlow(mergedCallbacks);
|
||||
};
|
||||
|
||||
const creds = await showWaitingUI(callbacks, runAuth);
|
||||
if (!creds) {
|
||||
throw new Error("Login cancelled");
|
||||
}
|
||||
return creds;
|
||||
}
|
||||
|
||||
// Fallback for terminal prompt (no extension TUI context).
|
||||
// Offer PAT entry first since it works in non-interactive-ish setups.
|
||||
// pi drives this via its built-in LoginDialog, which wires onPrompt/onAuth/
|
||||
// onProgress to a focused input. We must use those callbacks directly rather
|
||||
// than opening our own ctx.ui.custom surface (which would steal focus and
|
||||
// leave onPrompt unable to receive keystrokes).
|
||||
const prompt = getPrompt(callbacks);
|
||||
const pat = await prompt({
|
||||
message: "Paste a Qoder Personal Access Token (pt-...), or leave empty for browser login",
|
||||
@@ -72,11 +52,6 @@ export async function interactiveLogin(callbacks: OAuthLoginCallbacks): Promise<
|
||||
return patLogin(callbacks, pat.trim());
|
||||
}
|
||||
|
||||
await prompt({
|
||||
message: "Press Enter to start browser login for Qoder",
|
||||
placeholder: "press enter",
|
||||
allowEmpty: true,
|
||||
});
|
||||
if (getSignal(callbacks)?.aborted) throw new Error("Login cancelled");
|
||||
return runDeviceFlow(callbacks);
|
||||
}
|
||||
@@ -208,9 +183,10 @@ async function runDeviceFlow(callbacks: OAuthLoginCallbacks): Promise<OAuthCrede
|
||||
email,
|
||||
name,
|
||||
machineID,
|
||||
} as any;
|
||||
} catch (e: any) {
|
||||
if (e.name === "AbortError" || getSignal(callbacks)?.aborted) {
|
||||
} as OAuthCredentials;
|
||||
} catch (e: unknown) {
|
||||
const err = e as { name?: string };
|
||||
if (err.name === "AbortError" || getSignal(callbacks)?.aborted) {
|
||||
throw new Error("Login cancelled");
|
||||
}
|
||||
throw e;
|
||||
|
||||
+22
-7
@@ -7,6 +7,21 @@ const CACHE_PATH = join(homedir(), ".pi", "agent", "qoder-models-cache.json");
|
||||
|
||||
export const ZERO_COST = Object.freeze({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
||||
|
||||
/** Shape of a single entry returned by the Qoder /model/list endpoint. */
|
||||
export interface QoderModelEntry {
|
||||
key?: string;
|
||||
enable?: boolean;
|
||||
display_name?: string;
|
||||
max_input_tokens?: number;
|
||||
max_output_tokens?: number;
|
||||
context_config?: Record<string, { token_count?: number }>;
|
||||
is_vl?: boolean;
|
||||
is_reasoning?: boolean;
|
||||
thinking_config?: { enabled?: { efforts?: unknown } };
|
||||
source?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
export interface QoderModelDef {
|
||||
id: string;
|
||||
name: string;
|
||||
@@ -192,12 +207,12 @@ export function getCachedModels(): QoderModelDef[] {
|
||||
return staticModels;
|
||||
}
|
||||
|
||||
export function getCachedModelConfig(modelKey: string): Record<string, any> | null {
|
||||
export function getCachedModelConfig(modelKey: string): QoderModelEntry | null {
|
||||
if (existsSync(CACHE_PATH)) {
|
||||
try {
|
||||
const data = JSON.parse(readFileSync(CACHE_PATH, "utf8"));
|
||||
if (data && data.configs && data.configs[modelKey]) {
|
||||
return data.configs[modelKey];
|
||||
if (data?.configs?.[modelKey]) {
|
||||
return data.configs[modelKey] as QoderModelEntry;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
@@ -243,12 +258,12 @@ export async function updateQoderModelsCache(
|
||||
return;
|
||||
}
|
||||
|
||||
const resData = (await response.json()) as { chat?: any[] };
|
||||
const resData = (await response.json()) as { chat?: QoderModelEntry[] };
|
||||
const chatModels = resData.chat || [];
|
||||
if (chatModels.length === 0) return;
|
||||
|
||||
const newModels: QoderModelDef[] = [];
|
||||
const configs: Record<string, any> = {};
|
||||
const configs: Record<string, QoderModelEntry> = {};
|
||||
|
||||
for (const entry of chatModels) {
|
||||
const key = entry.key;
|
||||
@@ -258,8 +273,8 @@ export async function updateQoderModelsCache(
|
||||
let ctxLen = entry.max_input_tokens || 180000;
|
||||
if (entry.context_config && typeof entry.context_config === "object") {
|
||||
for (const configVal of Object.values(entry.context_config)) {
|
||||
if (configVal && typeof configVal === "object" && typeof (configVal as any).token_count === "number") {
|
||||
const tc = (configVal as any).token_count;
|
||||
if (configVal && typeof configVal === "object" && typeof configVal.token_count === "number") {
|
||||
const tc = configVal.token_count;
|
||||
if (tc > ctxLen) {
|
||||
ctxLen = tc;
|
||||
}
|
||||
|
||||
+7
-9
@@ -29,7 +29,7 @@ export function getCachedCredentials(_accessToken: string): QoderCredentials | n
|
||||
try {
|
||||
const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8"));
|
||||
const creds = auth?.qoder;
|
||||
if (creds && creds.userID) {
|
||||
if (creds?.userID) {
|
||||
return creds as QoderCredentials;
|
||||
}
|
||||
} catch {}
|
||||
@@ -91,6 +91,9 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
|
||||
const refreshToken = parts[0] || "";
|
||||
const userID = parts[1] || "";
|
||||
const machineID = parts[2] || getMachineId();
|
||||
const prev = credentials as Partial<QoderCredentials>;
|
||||
const prevName = prev.name || "";
|
||||
const prevEmail = prev.email || "";
|
||||
|
||||
const refreshURL = "https://center.qoder.sh/algo/api/v3/user/refresh_token";
|
||||
try {
|
||||
@@ -130,19 +133,14 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
|
||||
access: newAccess,
|
||||
expires: expireMs - 5 * 60 * 1000,
|
||||
userID,
|
||||
email: (credentials as any).email || "",
|
||||
name: (credentials as any).name || "",
|
||||
email: prevEmail,
|
||||
name: prevName,
|
||||
machineID,
|
||||
};
|
||||
|
||||
// pi persists the refreshed credentials in auth.json itself.
|
||||
// Cache models in background
|
||||
updateQoderModelsCache(
|
||||
newAccess,
|
||||
userID,
|
||||
(credentials as any).name || "",
|
||||
(credentials as any).email || "",
|
||||
).catch(() => {});
|
||||
updateQoderModelsCache(newAccess, userID, prevName, prevEmail).catch(() => {});
|
||||
|
||||
return refreshed;
|
||||
}
|
||||
|
||||
+36
-19
@@ -6,6 +6,9 @@ import type {
|
||||
Context,
|
||||
Model,
|
||||
SimpleStreamOptions,
|
||||
TextContent,
|
||||
ThinkingContent,
|
||||
ToolCall,
|
||||
} from "@earendil-works/pi-ai";
|
||||
import * as PiAi from "@earendil-works/pi-ai";
|
||||
import { buildAuthHeaders, getMachineId } from "./cosy.js";
|
||||
@@ -15,6 +18,15 @@ import { qoderEncodeBody } from "./qoder-encoding.js";
|
||||
import { ThinkingTagParser } from "./thinking-parser.js";
|
||||
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
||||
|
||||
interface ToolCallState {
|
||||
arguments: string;
|
||||
id: string;
|
||||
name: string;
|
||||
emittedStart?: boolean;
|
||||
emittedEnd?: boolean;
|
||||
contentIndex: number;
|
||||
}
|
||||
|
||||
function stableHash(prefix: string, ...inputs: string[]): string {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update(prefix);
|
||||
@@ -25,17 +37,22 @@ function stableHash(prefix: string, ...inputs: string[]): string {
|
||||
return hash.digest("hex").slice(0, 16);
|
||||
}
|
||||
|
||||
function stableChatRecordID(model: string, messages: any[], tools: any, maxTokens: number): string {
|
||||
function stableChatRecordID(
|
||||
model: string,
|
||||
messages: Array<{ role?: string; content?: unknown }>,
|
||||
tools: unknown,
|
||||
maxTokens: number,
|
||||
): string {
|
||||
const hash = crypto.createHash("sha256");
|
||||
hash.update("qoder-record");
|
||||
hash.update("\0");
|
||||
hash.update(model);
|
||||
for (const msg of messages) {
|
||||
if (msg && msg.role) {
|
||||
if (msg?.role) {
|
||||
hash.update("\0");
|
||||
hash.update(msg.role);
|
||||
}
|
||||
if (msg && msg.content) {
|
||||
if (msg?.content) {
|
||||
hash.update("\0");
|
||||
hash.update(typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content));
|
||||
}
|
||||
@@ -124,7 +141,7 @@ export function streamQoder(
|
||||
typeof content === "string"
|
||||
? content
|
||||
: Array.isArray(content)
|
||||
? content.map((c) => c.text || "").join("")
|
||||
? content.map((c) => ("text" in c ? c.text : "")).join("")
|
||||
: "";
|
||||
break;
|
||||
}
|
||||
@@ -143,7 +160,7 @@ export function streamQoder(
|
||||
const toolsRaw = context.tools && context.tools.length > 0 ? transformTools(context.tools) : undefined;
|
||||
const recordID = stableChatRecordID(qoderModel, normalizedMessages, toolsRaw, maxTokens);
|
||||
|
||||
const reqBody: Record<string, any> = {
|
||||
const reqBody: Record<string, unknown> = {
|
||||
request_id: crypto.randomUUID(),
|
||||
request_set_id: recordID,
|
||||
chat_record_id: recordID,
|
||||
@@ -235,9 +252,9 @@ export function streamQoder(
|
||||
|
||||
let contentBlockIndex = -1;
|
||||
let thinkingBlockIndex = -1;
|
||||
const toolCallsState: any[] = [];
|
||||
const toolCallsState: ToolCallState[] = [];
|
||||
|
||||
const thinkingEnabled = (options?.reasoning as any) !== false && (options?.reasoning as any) !== "off";
|
||||
const thinkingEnabled = (options?.reasoning as unknown) !== false && (options?.reasoning as unknown) !== "off";
|
||||
const thinkingParser = thinkingEnabled ? new ThinkingTagParser(output, stream) : null;
|
||||
|
||||
stream.push({ type: "start", partial: output });
|
||||
@@ -281,10 +298,10 @@ export function streamQoder(
|
||||
if (delta.reasoning_content) {
|
||||
if (thinkingBlockIndex === -1) {
|
||||
thinkingBlockIndex = output.content.length;
|
||||
output.content.push({ type: "thinking", thinking: "" } as any);
|
||||
output.content.push({ type: "thinking", thinking: "" });
|
||||
stream.push({ type: "thinking_start", contentIndex: thinkingBlockIndex, partial: output });
|
||||
}
|
||||
const block = output.content[thinkingBlockIndex] as any;
|
||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||
block.thinking += delta.reasoning_content;
|
||||
stream.push({
|
||||
type: "thinking_delta",
|
||||
@@ -298,7 +315,7 @@ export function streamQoder(
|
||||
if (delta.content) {
|
||||
// End API thinking block if active
|
||||
if (thinkingBlockIndex !== -1) {
|
||||
const block = output.content[thinkingBlockIndex] as any;
|
||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||
stream.push({
|
||||
type: "thinking_end",
|
||||
contentIndex: thinkingBlockIndex,
|
||||
@@ -313,10 +330,10 @@ export function streamQoder(
|
||||
} else {
|
||||
if (contentBlockIndex === -1) {
|
||||
contentBlockIndex = output.content.length;
|
||||
output.content.push({ type: "text", text: "" } as any);
|
||||
output.content.push({ type: "text", text: "" });
|
||||
stream.push({ type: "text_start", contentIndex: contentBlockIndex, partial: output });
|
||||
}
|
||||
const block = output.content[contentBlockIndex] as any;
|
||||
const block = output.content[contentBlockIndex] as TextContent;
|
||||
block.text += delta.content;
|
||||
stream.push({
|
||||
type: "text_delta",
|
||||
@@ -332,7 +349,7 @@ export function streamQoder(
|
||||
for (const tc of delta.tool_calls) {
|
||||
const idx = tc.index ?? 0;
|
||||
if (!toolCallsState[idx]) {
|
||||
toolCallsState[idx] = { arguments: "" };
|
||||
toolCallsState[idx] = { arguments: "", id: "", name: "", contentIndex: 0 };
|
||||
}
|
||||
const state = toolCallsState[idx];
|
||||
if (tc.id) state.id = tc.id;
|
||||
@@ -344,8 +361,8 @@ export function streamQoder(
|
||||
if (state.emittedStart === undefined) {
|
||||
state.emittedStart = true;
|
||||
state.contentIndex = output.content.length;
|
||||
const block = { type: "toolCall", id: state.id, name: state.name, arguments: {} };
|
||||
output.content.push(block as any);
|
||||
const block: ToolCall = { type: "toolCall", id: state.id, name: state.name, arguments: {} };
|
||||
output.content.push(block);
|
||||
stream.push({ type: "toolcall_start", contentIndex: state.contentIndex, partial: output });
|
||||
}
|
||||
stream.push({
|
||||
@@ -372,7 +389,7 @@ export function streamQoder(
|
||||
}
|
||||
|
||||
if (thinkingBlockIndex !== -1) {
|
||||
const block = output.content[thinkingBlockIndex] as any;
|
||||
const block = output.content[thinkingBlockIndex] as ThinkingContent;
|
||||
stream.push({
|
||||
type: "thinking_end",
|
||||
contentIndex: thinkingBlockIndex,
|
||||
@@ -382,13 +399,13 @@ export function streamQoder(
|
||||
}
|
||||
|
||||
for (const state of toolCallsState) {
|
||||
if (state && state.emittedStart && !state.emittedEnd) {
|
||||
if (state?.emittedStart && !state.emittedEnd) {
|
||||
state.emittedEnd = true;
|
||||
let args = {};
|
||||
try {
|
||||
args = JSON.parse(state.arguments || "{}");
|
||||
} catch {}
|
||||
const block = output.content[state.contentIndex] as any;
|
||||
const block = output.content[state.contentIndex] as ToolCall;
|
||||
block.arguments = args;
|
||||
stream.push({
|
||||
type: "toolcall_end",
|
||||
@@ -411,7 +428,7 @@ export function streamQoder(
|
||||
}
|
||||
stream.push({ type: "done", reason: output.stopReason as "stop" | "toolUse", message: output });
|
||||
stream.end();
|
||||
} catch (e: any) {
|
||||
} catch (e: unknown) {
|
||||
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
||||
output.errorMessage = e instanceof Error ? e.message : String(e);
|
||||
stream.push({ type: "error", reason: output.stopReason, error: output });
|
||||
|
||||
+37
-8
@@ -9,6 +9,35 @@ import type {
|
||||
ToolResultMessage,
|
||||
} from "@earendil-works/pi-ai";
|
||||
|
||||
/** OpenAI-style tool definition sent to the Qoder API. */
|
||||
interface QoderTool {
|
||||
type: "function";
|
||||
function: {
|
||||
name: string;
|
||||
description?: string;
|
||||
parameters?: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
/** OpenAI-style tool call within an assistant message. */
|
||||
interface QoderToolCall {
|
||||
id?: string;
|
||||
type: "function";
|
||||
function: { name?: string; arguments: string };
|
||||
}
|
||||
|
||||
type QoderTextPart = { type: "text"; text: string };
|
||||
type QoderImagePart = { type: "image_url"; image_url: { url: string } };
|
||||
type QoderContent = string | Array<QoderTextPart | QoderImagePart>;
|
||||
|
||||
/** OpenAI-style message sent to the Qoder API. */
|
||||
interface QoderMessage {
|
||||
role: "user" | "assistant" | "tool";
|
||||
content: QoderContent | null;
|
||||
tool_calls?: QoderToolCall[];
|
||||
tool_call_id?: string;
|
||||
}
|
||||
|
||||
export function getContentText(msg: Message): string {
|
||||
if (typeof msg.content === "string") return msg.content;
|
||||
if (Array.isArray(msg.content)) {
|
||||
@@ -23,7 +52,7 @@ export function getContentText(msg: Message): string {
|
||||
return "";
|
||||
}
|
||||
|
||||
export function transformTools(tools: Tool[]): any[] {
|
||||
export function transformTools(tools: Tool[]): QoderTool[] {
|
||||
return tools.map((t) => ({
|
||||
type: "function",
|
||||
function: {
|
||||
@@ -34,8 +63,8 @@ export function transformTools(tools: Tool[]): any[] {
|
||||
}));
|
||||
}
|
||||
|
||||
export function transformMessagesForQoder(messages: Message[]): any[] {
|
||||
const normalizedMessages: any[] = [];
|
||||
export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
||||
const normalizedMessages: QoderMessage[] = [];
|
||||
|
||||
for (const msg of messages) {
|
||||
// Skip error or aborted messages
|
||||
@@ -47,14 +76,14 @@ export function transformMessagesForQoder(messages: Message[]): any[] {
|
||||
}
|
||||
|
||||
if (msg.role === "user") {
|
||||
let content: any = "";
|
||||
let content: QoderContent = "";
|
||||
if (typeof msg.content === "string") {
|
||||
content = msg.content;
|
||||
} else if (Array.isArray(msg.content)) {
|
||||
const hasImage = msg.content.some((c) => c.type === "image");
|
||||
if (hasImage) {
|
||||
content = msg.content
|
||||
.map((c) => {
|
||||
.map((c): QoderTextPart | QoderImagePart | null => {
|
||||
if (c.type === "text") {
|
||||
return { type: "text", text: (c as TextContent).text };
|
||||
}
|
||||
@@ -69,7 +98,7 @@ export function transformMessagesForQoder(messages: Message[]): any[] {
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.filter(Boolean);
|
||||
.filter((p): p is QoderTextPart | QoderImagePart => p !== null);
|
||||
} else {
|
||||
content = getContentText(msg);
|
||||
}
|
||||
@@ -81,7 +110,7 @@ export function transformMessagesForQoder(messages: Message[]): any[] {
|
||||
} else if (msg.role === "assistant") {
|
||||
const am = msg as AssistantMessage;
|
||||
let content = "";
|
||||
const toolCalls: any[] = [];
|
||||
const toolCalls: QoderToolCall[] = [];
|
||||
|
||||
if (Array.isArray(am.content)) {
|
||||
for (const block of am.content) {
|
||||
@@ -106,7 +135,7 @@ export function transformMessagesForQoder(messages: Message[]): any[] {
|
||||
content = am.content || "";
|
||||
}
|
||||
|
||||
const mapped: any = {
|
||||
const mapped: QoderMessage = {
|
||||
role: "assistant",
|
||||
content: content || null,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user