Files
9router/open-sse/executors/qoder.js
luulam 2305e26e25 feat: pre-request token validation, mid-stream error handling, and usage stats improvements
- Qoder: handle mid-stream errors by returning proper error Response instead of embedding in stream
- Qoder: add refreshCredentials() to validate token via quota endpoint before requests
- chatCore: validate and refresh provider tokens before sending chat requests
- chatCore: bail early with 401 on unrecoverable token refresh errors
- Usage stats: track apiKey, comboName, fallbackHistory in request details
- Dashboard: improve Combos, Endpoint, Provider, Usage, and RequestDetails pages
- API keys route: upsert logic with provider_type support
- DB repos: usageRepo query improvements, requestDetailsRepo pagination, apiKeysRepo updates
2026-06-29 10:08:07 +07:00

641 lines
22 KiB
JavaScript

/**
* QoderExecutor — sends OpenAI-format chat requests to Qoder's COSY-signed
* inference endpoint at api3.qoder.sh, then unwraps Qoder's `{statusCodeValue,
* body}` SSE envelope back into plain OpenAI SSE for the rest of the pipeline.
*
* Differences vs the previous placeholder:
* - URL is api3.qoder.sh/algo/api/v2/service/pro/sse/agent_chat_generation
* with `&Encode=1` so we can ship the body through the WAF-bypass
* encoder.
* - Authentication is COSY (RSA + AES + MD5 + ~17 Cosy-* headers), not
* a static HMAC.
* - The request shape Qoder expects is non-trivial (chat_context with
* mirrored modelConfig, business block with stable IDs, system text
* hoisted out of the messages array). All ported from the reference.
* - Model identifier is one of the canonical Qoder keys (auto / ultimate /
* performance / efficient / lite + frontier "*model" ids); the
* translator layer feeds us "qoder/<key>" so we strip the prefix.
* - Per-model `model_config` is fetched live from /algo/api/v2/model/list
* and cached. Sending the wrong block silently downgrades to a
* different model upstream, so a missing entry is a hard error.
*/
import { qoderEncodeBody } from "../shared/qoder/encoding.js";
import { buildCosyHeaders } from "../shared/qoder/cosy.js";
import { v4 as uuidv4 } from "uuid";
import { createHash } from "crypto";
import { BaseExecutor } from "./base.js";
import { PROVIDERS } from "../config/providers.js";
import { proxyAwareFetch } from "../utils/proxyFetch.js";
import { SSE_DONE } from "../utils/sseConstants.js";
import { FETCH_CONNECT_TIMEOUT_MS } from "../config/runtimeConfig.js";
import {
QODER_CHAT_URL_ENCODED,
QODER_MODEL_MAP,
} from "../shared/qoder/constants.js";
import { getQoderModelConfig, resolveQoderModels } from "../services/qoderModels.js";
/**
* Hoist role:"system" messages out of the messages array (Qoder rejects
* system in messages) and flatten any multipart content arrays.
*/
function normalizeMessages(messages) {
if (!Array.isArray(messages) || messages.length === 0) {
return { messages: [], systemText: "" };
}
const systemParts = [];
const out = [];
for (const msg of messages) {
if (!msg || typeof msg !== "object") continue;
const text = extractText(msg.content);
if (msg.role === "system") {
if (text) systemParts.push(text);
continue;
}
const cloned = { ...msg };
cloned.content = text;
out.push(cloned);
}
return { messages: out, systemText: systemParts.join("\n\n") };
}
function extractText(content) {
if (typeof content === "string") return content;
if (content == null) return "";
if (Array.isArray(content)) {
const parts = [];
for (const item of content) {
if (item && typeof item === "object") {
if (item.type === "text" && typeof item.text === "string") {
parts.push(item.text);
} else if (typeof item.text === "string") {
parts.push(item.text);
}
}
}
return parts.join("\n");
}
return String(content);
}
function lastUserText(messages) {
for (let i = messages.length - 1; i >= 0; i--) {
const m = messages[i];
if (m?.role === "user" && typeof m.content === "string") {
return m.content;
}
}
return "";
}
function stableHash(prefix, ...parts) {
const h = createHash("sha256");
h.update(prefix);
for (const p of parts) {
h.update("\0");
h.update(String(p ?? ""));
}
return h.digest("hex").slice(0, 16);
}
function stableChatRecordId(model, messages, tools, maxTokens) {
const h = createHash("sha256");
h.update("qoder-record\0");
h.update(String(model));
for (const m of messages) {
if (!m || typeof m !== "object") continue;
if (m.role) { h.update("\0"); h.update(m.role); }
if (typeof m.content === "string" && m.content) {
h.update("\0"); h.update(m.content);
}
}
if (tools) {
h.update("\0");
try { h.update(JSON.stringify(tools)); } catch {}
}
h.update(`\0mt=${maxTokens}`);
return h.digest("hex").slice(0, 16);
}
function truncate(s, n) {
return s && s.length > n ? `${s.slice(0, n)}...` : s || "";
}
/**
* Parse Qoder-specific mid-stream error codes into user-friendly messages.
* Qoder embeds errors inside SSE envelope body as JSON: {"code":"112","message":"{...}"}
*
* Known codes:
* 112 → quota/billing exceeded (message contains pricingUrl)
* 113 → model not available for current plan
*
* @param {number} statusVal - Upstream status code from envelope
* @param {string} bodyStr - Raw body string from envelope
* @returns {{ statusCode: number, message: string, errorCode: string|null }}
*/
function parseQoderStreamError(statusVal, bodyStr) {
let code = null;
let innerMessage = "";
try {
const inner = JSON.parse(bodyStr);
code = String(inner.code || "");
innerMessage = inner.message || bodyStr;
// Code 112: quota exceeded — inner message is JSON with pricingUrl
if (code === "112") {
let pricingUrl = "";
try {
const msgObj = JSON.parse(innerMessage);
pricingUrl = msgObj.pricingUrl || "";
} catch { /* innerMessage is not JSON */ }
return {
statusCode: statusVal,
message: pricingUrl
? `Qoder quota exceeded. Upgrade your plan at: ${pricingUrl}`
: "Qoder quota exceeded. Please check your plan limits.",
errorCode: code,
};
}
// Code 113: model not available
if (code === "113") {
return {
statusCode: statusVal,
message: `Qoder model not available for your current plan. ${innerMessage}`,
errorCode: code,
};
}
} catch {
// bodyStr is not valid JSON — fall through to generic message
}
// Generic fallback
const statusLabel = statusVal >= 500 ? "server error"
: statusVal === 429 ? "rate limit exceeded"
: statusVal === 403 ? "permission error"
: statusVal === 401 ? "authentication error"
: "request error";
return {
statusCode: statusVal,
message: `Qoder ${statusLabel}: ${truncate(innerMessage || bodyStr, 200)}`,
errorCode: code,
};
}
/**
* Map the OpenAI-style request body into the exact shape Qoder expects.
*/
async function buildQoderRequestBody({ model, body, credentials, log, proxyOptions, signal }) {
const qoderKey = String(model || "").replace(/^qoder\//, "");
// Fetch model config from dynamic API instead of relying on static QODER_MODEL_MAP.
// This allows support for new Qoder models (e.g., qmodel_latest) without code changes.
let modelConfig = await getQoderModelConfig(credentials, qoderKey, { log, proxyOptions, signal });
if (!modelConfig) {
// Try a forced refresh once before giving up — the cache may simply
// not be populated yet on first ever call for this credential.
const refreshed = await resolveQoderModels(credentials, { forceRefresh: true, log, proxyOptions, signal });
const retried = refreshed?.rawConfigs.get(qoderKey);
if (!retried) {
throw new Error(
`qoder: model_config for "${qoderKey}" not yet known (run a model list fetch or check upstream connectivity)`,
);
}
modelConfig = { ...retried, key: qoderKey };
}
const { messages, systemText } = normalizeMessages(body.messages || []);
const tools = body.tools;
const isReasoning = !!modelConfig.is_reasoning;
const maxOutputTokens = Number(modelConfig.max_output_tokens) || 0;
let maxTokens = 32_768;
if (maxOutputTokens > 0) maxTokens = maxOutputTokens;
if (typeof body.max_tokens === "number" && body.max_tokens > 0 && body.max_tokens < maxTokens) {
maxTokens = body.max_tokens;
}
if (typeof body.max_completion_tokens === "number" && body.max_completion_tokens > 0 && body.max_completion_tokens < maxTokens) {
maxTokens = body.max_completion_tokens;
}
const lastUser = lastUserText(messages);
const psd = credentials.providerSpecificData || {};
const sessionId = stableHash("qoder-session", psd.userId, qoderKey);
const recordId = stableChatRecordId(qoderKey, messages, tools, maxTokens);
return {
qoderKey,
payload: {
request_id: uuidv4(),
request_set_id: recordId,
chat_record_id: recordId,
session_id: sessionId,
stream: true,
chat_task: "FREE_INPUT",
is_reply: true,
is_retry: false,
source: 1,
version: "3",
session_type: "qodercli",
agent_id: "agent_common",
task_id: "common",
code_language: "",
chat_prompt: "",
image_urls: null,
aliyun_user_type: "",
system: systemText,
messages,
tools: Array.isArray(tools) ? tools : [],
parameters: { max_tokens: maxTokens },
chat_context: {
chatPrompt: "",
imageUrls: null,
extra: {
context: [],
modelConfig: { key: qoderKey, is_reasoning: isReasoning },
originalContent: lastUser,
},
features: [],
text: lastUser,
},
model_config: modelConfig,
business: {
product: "cli",
version: "1.0.0",
type: "agent",
stage: "start",
id: uuidv4(),
name: truncate(lastUser, 30),
begin_at: Date.now(),
},
},
modelConfig,
};
}
/**
* Wrap the upstream's `{statusCodeValue, body}` SSE envelope into plain
* OpenAI SSE chunks the rest of the chatCore pipeline understands.
*
* Each upstream line looks like:
* data: {"statusCodeValue":200,"body":"{\"choices\":[{\"delta\":{...}}]}"}
* The inner body is an OpenAI streaming chunk (or "[DONE]"). We unwrap it
* and re-emit as `data: <inner>\n\n`. Errors become `data: [DONE]\n\n` plus
* a synthetic OpenAI error chunk.
*/
async function wrapQoderSSE(response, model, midStreamError = {}) {
if (!response.ok || !response.body) return response;
const decoder = new TextDecoder();
const encoder = new TextEncoder();
// Peek at first chunk to detect errors early
const reader = response.body.getReader();
const firstRead = await reader.read();
if (firstRead.done) {
// Empty stream
return new Response("data: [DONE]\n\n", {
status: response.status,
statusText: response.statusText,
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
},
});
}
// Parse first line to check for error
const firstText = decoder.decode(firstRead.value, { stream: true });
const nlIndex = firstText.indexOf("\n");
const firstLine = nlIndex !== -1 ? firstText.slice(0, nlIndex) : firstText;
const trimmed = firstLine.replace(/\r$/, "").trim();
if (trimmed.startsWith("data:")) {
const data = trimmed.slice(5).trimStart();
if (data !== "[DONE]") {
try {
const envelope = JSON.parse(data);
const statusVal = typeof envelope.statusCodeValue === "number" ? envelope.statusCodeValue : 200;
if (statusVal !== 200) {
// Error detected - return error Response to trigger failover
const msg = envelope.body || `upstream status ${statusVal}`;
const errorResponse = new Response(
JSON.stringify({
error: {
message: `qoder error ${statusVal}: ${truncate(msg, 500)}`,
type: "upstream_error",
code: String(statusVal)
}
}),
{
status: statusVal >= 400 && statusVal < 600 ? statusVal : 502,
headers: { "Content-Type": "application/json" }
}
);
reader.cancel();
return errorResponse;
}
} catch (e) {
// Not JSON, continue as normal stream
}
}
}
// No error detected - proceed with normal TransformStream
let buffer = "";
let doneEmitted = false;
const processLine = (line, controller) => {
const trimmed = line.replace(/\r$/, "").trim();
if (!trimmed) return;
if (!trimmed.startsWith("data:")) return;
if (doneEmitted) return;
const data = trimmed.slice(5).trimStart();
if (data === "[DONE]") {
controller.enqueue(encoder.encode(SSE_DONE));
doneEmitted = true;
return;
}
let envelope;
try { envelope = JSON.parse(data); } catch { return; }
const statusVal = typeof envelope.statusCodeValue === "number" ? envelope.statusCodeValue : 200;
const inner = typeof envelope.body === "string" ? envelope.body : "";
if (statusVal !== 200) {
const parsed = parseQoderStreamError(statusVal, inner);
// Store error in shared state so the caller can return a proper error Response
midStreamError.error = { status: parsed.statusCode, message: parsed.message, errorCode: parsed.errorCode };
// End the stream with [DONE]; the caller (QoderExecutor.execute) will detect
// midStreamError and return a non-2xx Response so the API client gets a real error.
controller.enqueue(encoder.encode(SSE_DONE));
doneEmitted = true;
return;
}
if (!inner) return;
if (inner === "[DONE]") {
controller.enqueue(encoder.encode(SSE_DONE));
doneEmitted = true;
return;
}
const sanitized = inner.replace(/\r?\n/g, "");
controller.enqueue(encoder.encode(`data: ${sanitized}\n\n`));
};
const transform = new TransformStream({
transform(chunk, controller) {
buffer += decoder.decode(chunk, { stream: true });
let nl;
while ((nl = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, nl);
buffer = buffer.slice(nl + 1);
processLine(line, controller);
}
},
flush(controller) {
buffer += decoder.decode();
if (buffer.length > 0) {
processLine(buffer, controller);
buffer = "";
}
if (!doneEmitted) {
controller.enqueue(encoder.encode(SSE_DONE));
doneEmitted = true;
}
},
});
// Create a ReadableStream that emits the first chunk + remaining chunks
const combinedStream = new ReadableStream({
start(controller) {
controller.enqueue(firstRead.value);
},
async pull(controller) {
const { done, value } = await reader.read();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
},
cancel() {
reader.cancel();
}
});
const transformed = combinedStream.pipeThrough(transform);
return new Response(transformed, {
status: response.status,
statusText: response.statusText,
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
},
});
}
export class QoderExecutor extends BaseExecutor {
constructor() {
super("qoder", PROVIDERS.qoder);
}
buildUrl() {
return QODER_CHAT_URL_ENCODED;
}
// Override execute entirely — Qoder needs:
// - body built from translated chat completion payload
// - body encoded with QoderEncodeBody before signing
// - COSY headers built from the *encoded* body bytes
// - response stream re-wrapped from {statusCodeValue, body} to OpenAI SSE
async execute({ model, body, stream, credentials, signal, log, proxyOptions = null }) {
const url = this.buildUrl();
const psd = credentials?.providerSpecificData || {};
if (!psd.userId) {
// No user id → no way to sign. Surface a 401 so the dashboard nudges
// the user back to OAuth.
const fakeResp = new Response(
JSON.stringify({ error: { message: "qoder credential is missing userId; reconnect the account" } }),
{ status: 401, headers: { "Content-Type": "application/json" } },
);
return { response: fakeResp, url, headers: {}, transformedBody: body };
}
if (!credentials?.accessToken) {
// Same shape as the userId guard — clean 401 so chatCore reports
// "reconnect" rather than bubbling cosy.js's synchronous throw as 500.
const fakeResp = new Response(
JSON.stringify({ error: { message: "qoder credential is missing accessToken; reconnect the account" } }),
{ status: 401, headers: { "Content-Type": "application/json" } },
);
return { response: fakeResp, url, headers: {}, transformedBody: body };
}
let qoderKey;
let payload;
try {
({ qoderKey, payload } = await buildQoderRequestBody({ model, body, credentials, log, proxyOptions, signal }));
} catch (err) {
const fakeResp = new Response(
JSON.stringify({ error: { message: err.message } }),
{ status: 400, headers: { "Content-Type": "application/json" } },
);
return { response: fakeResp, url, headers: {}, transformedBody: body };
}
const plainBody = Buffer.from(JSON.stringify(payload), "utf8");
const encodedBodyStr = qoderEncodeBody(plainBody);
const encodedBodyBuf = Buffer.from(encodedBodyStr, "latin1");
let cosyHeaders;
try {
cosyHeaders = buildCosyHeaders(
encodedBodyBuf,
url,
{
userId: psd.userId,
authToken: credentials.accessToken,
name: credentials.displayName || "",
email: credentials.email || "",
machineId: psd.machineId || "",
},
);
} catch (err) {
// cosy.js throws synchronously on missing userId/authToken — surface
// as 401 so chatCore prompts re-auth instead of returning a 500.
const fakeResp = new Response(
JSON.stringify({ error: { message: `qoder cosy signing failed: ${err.message}` } }),
{ status: 401, headers: { "Content-Type": "application/json" } },
);
return { response: fakeResp, url, headers: {}, transformedBody: body };
}
const modelSource = (payload.model_config && payload.model_config.source) || "system";
const headers = {
"Content-Type": "application/json",
Accept: "text/event-stream",
"Cache-Control": "no-cache",
"X-Model-Key": qoderKey,
"X-Model-Source": modelSource,
// gzip triggers signature validation on Qoder's CDN; force identity.
"Accept-Encoding": "identity",
...cosyHeaders,
};
// Abort if upstream doesn't return response headers within connect timeout.
const timeoutMs = this.config?.timeoutMs || FETCH_CONNECT_TIMEOUT_MS;
const connectCtrl = new AbortController();
const connectTimer = setTimeout(() => connectCtrl.abort(new Error("fetch connect timeout")), timeoutMs);
const mergedSignal = signal ? AbortSignal.any([signal, connectCtrl.signal]) : connectCtrl.signal;
let response;
try {
response = await proxyAwareFetch(
url,
{ method: "POST", headers, body: encodedBodyBuf, signal: mergedSignal },
proxyOptions,
);
} finally {
clearTimeout(connectTimer);
}
if (!response.ok) {
// Pass error response through unchanged so chatCore can capture it.
return { response, url, headers, transformedBody: payload };
}
const midStreamError = {};
let wrapped = await wrapQoderSSE(response, `qoder/${qoderKey}`, midStreamError);
// If a mid-stream error was detected, return a proper error Response instead of
// a successful one with an error message inside.
if (midStreamError?.error) {
const err = midStreamError.error;
log?.error?.("QODER", `Upstream error ${err.status}: ${err.message}`);
wrapped = new Response(
JSON.stringify({
error: {
message: err.message,
type: "upstream_error",
code: String(err.status),
}
}),
{
status: err.status >= 400 && err.status < 600 ? err.status : 502,
headers: { "Content-Type": "application/json" },
}
);
}
return { response: wrapped, url, headers, transformedBody: payload, midStreamError };
}
// Validate Qoder token by calling the quota endpoint. If it returns 403,
// return a structured unrecoverable error so chatCore bails before sending
// the actual chat request (instead of forwarding a "quota exceeded" success).
async refreshCredentials(credentials, log) {
// Qoder's quota endpoint validates the access token. If it returns 403,
// the token is invalid and the user needs to reconnect.
const oauth = PROVIDERS.qoder?.oauth;
const url = oauth?.quotaUsageUrl || "https://openapi.qoder.sh/api/v2/quota/usage";
const authToken = credentials?.accessToken;
if (!authToken) return null;
try {
const res = await fetch(url, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
signal: AbortSignal.timeout(10_000),
});
if (res.status === 403) {
const errText = await res.text().catch(() => "");
log?.error?.("TOKEN_REFRESH", `Qoder token invalid (403): ${errText}`);
return {
error: "unrecoverable_refresh_error",
code: "invalid_token",
message: "qoder token invalid; reconnect the account",
};
}
// Token is valid — stamp lastRefreshAt so needsRefresh stays quiet
const psd = { ...credentials.providerSpecificData, lastRefreshAt: new Date().toISOString() };
return {
accessToken: authToken,
expiresIn: 86400,
providerSpecificData: psd,
lastRefreshAt: new Date().toISOString(),
};
} catch (err) {
log?.warn?.("TOKEN_REFRESH", `Qoder refresh check failed: ${err.message}`);
return null; // Network blip — let the request proceed, failover handles 403
}
}
// 24h cooldown before re-checking token validity
needsRefresh(credentials) {
if (!credentials?.accessToken) return false;
const psd = credentials?.providerSpecificData || {};
if (!psd.lastRefreshAt) return true;
const elapsed = Date.now() - new Date(psd.lastRefreshAt).getTime();
return elapsed > 24 * 60 * 60 * 1000;
}
}
export default QoderExecutor;
// Internals exposed for unit tests. Not part of the public API — callers
// should import QoderExecutor and use its public methods.
export const __test__ = {
normalizeMessages,
parseQoderStreamError,
wrapQoderSSE,
buildQoderRequestBody,
};