Merge remote-tracking branch 'origin/master' into gitea/new_feature

# Conflicts:
#	open-sse/executors/qoder.js
#	open-sse/handlers/chatCore.js
#	open-sse/handlers/chatCore/sseToJsonHandler.js
#	open-sse/providers/registry/commandcode.js
#	src/app/(dashboard)/dashboard/combos/page.js
#	src/app/api/v1/models/route.js
#	src/lib/db/repos/usageRepo.js
#	src/shared/components/UsageStats.js
This commit is contained in:
2026-09-25 10:25:56 +07:00
154 changed files with 10246 additions and 1241 deletions

View File

@@ -218,6 +218,12 @@ export function encodeField(fieldNum, wireType, value) {
return concatArrays(tagBytes, lengthBytes, dataBytes);
}
if (wireType === WIRE_TYPE.FIXED64) {
const buf = Buffer.alloc(8);
buf.writeDoubleLE(Number(value));
return concatArrays(tagBytes, buf);
}
return new Uint8Array(0);
}
@@ -887,6 +893,211 @@ export function extractTextFromResponse(payload) {
}
}
// ==================== AGENT SERVICE (google.protobuf.Value + MCP) ====================
const PB_VALUE = { NULL: 1, NUMBER: 2, STRING: 3, BOOL: 4, STRUCT: 5, LIST: 6 };
const PB_STRUCT_FIELDS = 1;
const PB_MAP_KEY = 1;
const PB_MAP_VALUE = 2;
const PB_LIST_VALUES = 1;
const MTD_NAME = 1;
const MTD_DESCRIPTION = 2;
const MTD_INPUT_SCHEMA = 3;
const MTD_PROVIDER = 4;
const MTD_TOOL_NAME = 5;
const MCP_TOOLS_TOOL = 1;
const MCP_ARGS_NAME = 1;
const MCP_ARGS_ENTRY = 2;
const MCP_ARGS_CALL_ID = 3;
const MCP_ARGS_TOOL_NAME = 5;
const MCR_SUCCESS = 1;
const MCR_ERROR = 2;
const MCR_TOOL_NOT_FOUND = 5;
const MCS_CONTENT = 1;
const MCS_IS_ERROR = 2;
const MCC_TEXT = 1;
const MCC_IMAGE = 2;
const MTC_TEXT = 1;
const MIC_DATA = 1;
const MIC_MIME = 2;
const MER_MESSAGE = 1;
const TNF_NAME = 1;
function asBytes(value) {
if (!value) return Buffer.alloc(0);
return Buffer.isBuffer(value) ? value : Buffer.from(value);
}
/**
* Encode a JS value as google.protobuf.Value (oneof body, no outer tag).
*/
export function encodeAgentValue(value) {
if (value === null || value === undefined) {
return encodeField(PB_VALUE.NULL, WIRE_TYPE.VARINT, 0);
}
if (typeof value === "boolean") {
return encodeField(PB_VALUE.BOOL, WIRE_TYPE.VARINT, value ? 1 : 0);
}
if (typeof value === "number") {
return encodeField(PB_VALUE.NUMBER, WIRE_TYPE.FIXED64, value);
}
if (typeof value === "string") {
return encodeField(PB_VALUE.STRING, WIRE_TYPE.LEN, value);
}
if (Array.isArray(value)) {
const items = value.map((item) => encodeField(PB_LIST_VALUES, WIRE_TYPE.LEN, encodeAgentValue(item)));
return encodeField(PB_VALUE.LIST, WIRE_TYPE.LEN, concatArrays(...items));
}
if (typeof value === "object") {
const entries = Object.entries(value).map(([key, val]) => encodeField(
PB_STRUCT_FIELDS,
WIRE_TYPE.LEN,
concatArrays(
encodeField(PB_MAP_KEY, WIRE_TYPE.LEN, key),
encodeField(PB_MAP_VALUE, WIRE_TYPE.LEN, encodeAgentValue(val)),
),
));
return encodeField(PB_VALUE.STRUCT, WIRE_TYPE.LEN, concatArrays(...entries));
}
return encodeField(PB_VALUE.STRING, WIRE_TYPE.LEN, String(value));
}
/**
* Decode google.protobuf.Value bytes back to a JS value.
*/
export function decodeAgentValue(bytes) {
const fields = decodeMessage(asBytes(bytes));
if (fields.has(PB_VALUE.NULL)) return null;
if (fields.has(PB_VALUE.BOOL)) return fields.get(PB_VALUE.BOOL)[0].value !== 0;
if (fields.has(PB_VALUE.NUMBER)) {
return asBytes(fields.get(PB_VALUE.NUMBER)[0].value).readDoubleLE(0);
}
if (fields.has(PB_VALUE.STRING)) {
return asBytes(fields.get(PB_VALUE.STRING)[0].value).toString("utf8");
}
if (fields.has(PB_VALUE.STRUCT)) {
const result = {};
for (const entry of decodeMessage(asBytes(fields.get(PB_VALUE.STRUCT)[0].value)).get(PB_STRUCT_FIELDS) || []) {
const pair = decodeMessage(asBytes(entry.value));
const key = asBytes(pair.get(PB_MAP_KEY)?.[0]?.value).toString("utf8");
if (key) result[key] = decodeAgentValue(pair.get(PB_MAP_VALUE)?.[0]?.value);
}
return result;
}
if (fields.has(PB_VALUE.LIST)) {
return (decodeMessage(asBytes(fields.get(PB_VALUE.LIST)[0].value)).get(PB_LIST_VALUES) || [])
.map((item) => decodeAgentValue(item.value));
}
return null;
}
function toolNameAndSchema(tool) {
const fn = tool?.function || tool || {};
return {
name: fn.name || tool?.name || "",
description: fn.description || tool?.description || "",
schema: fn.parameters || tool?.parameters || tool?.inputSchema || tool?.input_schema || {},
};
}
/**
* Encode agent.v1.McpToolDefinition body (name, description, Value schema, provider, tool_name).
*/
export function encodeMcpToolDefinition(tool) {
const { name, description, schema } = toolNameAndSchema(tool);
return concatArrays(
encodeField(MTD_NAME, WIRE_TYPE.LEN, name),
encodeField(MTD_DESCRIPTION, WIRE_TYPE.LEN, description),
encodeField(MTD_INPUT_SCHEMA, WIRE_TYPE.LEN, encodeAgentValue(schema)),
encodeField(MTD_PROVIDER, WIRE_TYPE.LEN, "9router"),
encodeField(MTD_TOOL_NAME, WIRE_TYPE.LEN, name),
);
}
/**
* Encode AgentRunRequest.mcp_tools: repeated McpToolDefinition under field 1.
*/
export function encodeMcpTools(tools = []) {
if (!tools?.length) return new Uint8Array();
return concatArrays(
...tools.map((tool) => encodeField(MCP_TOOLS_TOOL, WIRE_TYPE.LEN, encodeMcpToolDefinition(tool))),
);
}
/**
* Decode agent.v1.McpArgs (name, typed args map, toolCallId, toolName).
*/
export function decodeMcpArgs(bytes) {
const msg = decodeMessage(asBytes(bytes));
const args = {};
for (const entry of msg.get(MCP_ARGS_ENTRY) || []) {
const pair = decodeMessage(asBytes(entry.value));
const key = asBytes(pair.get(PB_MAP_KEY)?.[0]?.value).toString("utf8");
if (key) args[key] = decodeAgentValue(pair.get(PB_MAP_VALUE)?.[0]?.value);
}
const read = (field) => asBytes(msg.get(field)?.[0]?.value).toString("utf8");
return {
name: read(MCP_ARGS_NAME),
toolCallId: read(MCP_ARGS_CALL_ID),
toolName: read(MCP_ARGS_TOOL_NAME),
args,
};
}
function encodeMcpTextItem(text) {
return encodeField(
MCS_CONTENT,
WIRE_TYPE.LEN,
encodeField(MCC_TEXT, WIRE_TYPE.LEN, encodeField(MTC_TEXT, WIRE_TYPE.LEN, text)),
);
}
function encodeMcpImageItem(image) {
const data = image?.data || image || new Uint8Array();
const mimeType = image?.mimeType || "application/octet-stream";
return encodeField(
MCS_CONTENT,
WIRE_TYPE.LEN,
encodeField(
MCC_IMAGE,
WIRE_TYPE.LEN,
concatArrays(
encodeField(MIC_DATA, WIRE_TYPE.LEN, data),
encodeField(MIC_MIME, WIRE_TYPE.LEN, mimeType),
),
),
);
}
export function encodeMcpResultSuccess({ textItems = [], imageItems = [], isError = false } = {}) {
const success = concatArrays(
...textItems.map(encodeMcpTextItem),
...imageItems.map(encodeMcpImageItem),
encodeField(MCS_IS_ERROR, WIRE_TYPE.VARINT, isError ? 1 : 0),
);
return encodeField(MCR_SUCCESS, WIRE_TYPE.LEN, success);
}
export function encodeMcpResultError(message) {
return encodeField(
MCR_ERROR,
WIRE_TYPE.LEN,
encodeField(MER_MESSAGE, WIRE_TYPE.LEN, String(message || "")),
);
}
export function encodeMcpResultToolNotFound(name) {
return encodeField(
MCR_TOOL_NOT_FOUND,
WIRE_TYPE.LEN,
encodeField(TNF_NAME, WIRE_TYPE.LEN, String(name || "")),
);
}
// ==================== EXPORTS ====================
export default {
@@ -900,5 +1111,13 @@ export default {
decodeField,
decodeMessage,
parseConnectRPCFrame,
extractTextFromResponse
extractTextFromResponse,
encodeAgentValue,
decodeAgentValue,
encodeMcpToolDefinition,
encodeMcpTools,
decodeMcpArgs,
encodeMcpResultSuccess,
encodeMcpResultError,
encodeMcpResultToolNotFound,
};

View File

@@ -0,0 +1,232 @@
/**
* Helpers for the OpenCode Zen free-tier client fingerprint.
*
* Live upstream probes show that free-tier requests must include the lowercase
* file-search quartet (bash/glob/grep/read). Agent clients such as Claude Code
* may declare the same tools with different casing, so those case variants must
* be renamed instead of duplicated. The response side restores the caller's
* original spelling so downstream clients still recognise their own tool calls.
*/
/** Canonical names required by the upstream free-tier gate. */
export const OPENCODE_FINGERPRINT_TOOLS = ["bash", "glob", "grep", "read"];
// Request body -> names renamed for that request. transformRequest() mutates the
// same body object that chatCore passed into the executor, so a WeakMap keeps the
// mapping request-local without putting transport metadata on the wire.
const renamedToolNames = new WeakMap();
/** Canonical lowercase name when `name` is a quartet member; "" otherwise. */
export function fingerprintToolKey(name) {
const lower = String(name ?? "").trim().toLowerCase();
return OPENCODE_FINGERPRINT_TOOLS.includes(lower) ? lower : "";
}
/** Read a tool name from either flat ({name}) or chat ({function:{name}}) shape. */
function toolNameOf(tool) {
if (!tool || typeof tool !== "object" || Array.isArray(tool)) return "";
if (typeof tool.name === "string" && tool.name.trim()) return tool.name.trim();
const fn = tool.function;
if (fn && typeof fn === "object" && !Array.isArray(fn) && typeof fn.name === "string") {
return fn.name.trim();
}
return "";
}
/**
* Canonicalise only the fingerprint quartet and remove duplicate quartet
* variants. Non-fingerprint tools are preserved verbatim, including tools whose
* names differ only by case; they are outside OpenCode's fingerprint contract.
*
* @param {Array} tools
* @returns {{ tools: Array, map: Map<string,string> }} map: sent name -> original name
*/
export function concealFingerprintToolNames(tools) {
const map = new Map();
if (!Array.isArray(tools) || tools.length === 0) return { tools, map };
const seenQuartet = new Set();
const out = [];
for (const tool of tools) {
if (!tool || typeof tool !== "object" || Array.isArray(tool)) {
out.push(tool);
continue;
}
const current = toolNameOf(tool);
const key = fingerprintToolKey(current);
if (!key) {
out.push(tool);
continue;
}
// `Bash` + `bash` is rejected upstream as a duplicate. Keep exactly one
// declaration for each quartet member.
if (seenQuartet.has(key)) continue;
seenQuartet.add(key);
if (current !== key) {
map.set(key, current);
const fn = tool.function && typeof tool.function === "object" && !Array.isArray(tool.function)
? tool.function
: null;
out.push(fn ? { ...tool, function: { ...fn, name: key } } : { ...tool, name: key });
} else {
out.push(tool);
}
}
return { tools: out, map };
}
/** Append only genuinely missing quartet declarations. */
export function appendMissingFingerprintTools(tools, flat) {
const list = Array.isArray(tools) ? tools : [];
for (const name of OPENCODE_FINGERPRINT_TOOLS) {
if (list.some((tool) => fingerprintToolKey(toolNameOf(tool)) === name)) continue;
list.push(flat ? {
type: "function",
name,
description: "This tool is currently unavailable and must not be used.",
parameters: { type: "object", properties: {} },
} : {
type: "function",
function: {
name,
description: "This tool is currently unavailable and must not be used.",
parameters: { type: "object", properties: {} },
},
});
}
return list;
}
/** Point an explicit tool_choice at a quartet member after canonicalisation. */
export function retargetToolChoice(body, map) {
if (!body || typeof body !== "object" || !map?.size) return;
const choice = body.tool_choice;
if (!choice || typeof choice !== "object" || Array.isArray(choice)) return;
if (typeof choice.name === "string") {
const key = fingerprintToolKey(choice.name);
if (key && map.has(key)) body.tool_choice = { ...choice, name: key };
return;
}
const fn = choice.function;
if (fn && typeof fn === "object" && !Array.isArray(fn) && typeof fn.name === "string") {
const key = fingerprintToolKey(fn.name);
if (key && map.has(key)) {
body.tool_choice = { ...choice, function: { ...fn, name: key } };
}
}
}
/**
* Full request-side pass: canonicalise quartet case variants, remove duplicate
* quartet declarations, append missing members and preserve the legacy
* tool_choice defaults used by the OpenCode executor.
*
* @param {object} body
* @param {boolean} flat - true for Responses tools ({name}), false for chat tools
* @returns {Map<string,string>} map: sent name -> original name
*/
export function applyFingerprintTools(body, flat) {
if (!body || typeof body !== "object") return new Map();
const hadClientTools = Array.isArray(body.tools) && body.tools.length > 0;
const { tools, map } = concealFingerprintToolNames(body.tools);
body.tools = appendMissingFingerprintTools(tools, flat);
retargetToolChoice(body, map);
// Preserve the existing executor semantics. Responses uses auto when the
// fingerprint helper supplies tools; chat requests with no caller tools use
// none so the injected decoys cannot be selected.
if (!body.tool_choice) {
if (flat) body.tool_choice = "auto";
else if (!hadClientTools) body.tool_choice = "none";
}
recordRenamedToolNames(body, map);
return map;
}
/** Store the rename map for `body`. */
export function recordRenamedToolNames(body, map) {
if (!body || typeof body !== "object" || !map?.size) return;
renamedToolNames.set(body, map);
}
/** Retrieve the rename map for `body`. */
export function takeRenamedToolNames(body) {
if (!body || typeof body !== "object") return null;
return renamedToolNames.get(body) || null;
}
// Response side -------------------------------------------------------------
/** Restore caller tool spellings in supported response/event shapes. */
export function restoreToolNames(payload, map) {
if (!map?.size || !payload) return payload;
if (Array.isArray(payload)) return payload.map((item) => restoreToolNames(item, map));
if (typeof payload !== "object") return payload;
let out = payload;
const put = (key, value) => {
if (out === payload) out = { ...payload };
out[key] = value;
};
// Claude streaming content_block_start event.
if (payload.type === "content_block_start") {
const block = payload.content_block;
if (block?.type === "tool_use" && typeof block.name === "string" && map.has(block.name)) {
put("content_block", { ...block, name: map.get(block.name) });
}
}
// Claude non-streaming message body.
if (Array.isArray(payload.content)) {
put("content", payload.content.map((block) =>
block?.type === "tool_use" && typeof block.name === "string" && map.has(block.name)
? { ...block, name: map.get(block.name) }
: block));
}
// OpenAI Chat Completions, both streaming delta and JSON message shapes.
if (Array.isArray(payload.choices)) {
put("choices", payload.choices.map((choice) => {
let changed = false;
const next = { ...choice };
for (const holder of ["delta", "message"]) {
const value = choice?.[holder];
if (!value || !Array.isArray(value.tool_calls) || value.tool_calls.length === 0) continue;
const calls = value.tool_calls.map((call) => {
const name = call?.function?.name;
if (typeof name === "string" && map.has(name)) {
changed = true;
return { ...call, function: { ...call.function, name: map.get(name) } };
}
return call;
});
next[holder] = { ...value, tool_calls: calls };
}
return changed ? next : choice;
}));
}
// OpenAI Responses final JSON body.
if (Array.isArray(payload.output)) {
put("output", payload.output.map((item) =>
item?.type === "function_call" && typeof item.name === "string" && map.has(item.name)
? { ...item, name: map.get(item.name) }
: item));
}
// OpenAI Responses SSE events such as response.output_item.added/done.
const item = payload.item;
if (item?.type === "function_call" && typeof item.name === "string" && map.has(item.name)) {
put("item", { ...item, name: map.get(item.name) });
}
return out;
}

View File

@@ -215,8 +215,11 @@ export async function proxyAwareFetch(url, options = {}, proxyOptions = null) {
const vercelRelayUrl = normalizeString(proxyOptions?.vercelRelayUrl);
if (vercelRelayUrl) {
const parsed = new URL(targetUrl);
const baseHeaders = options.headers instanceof Headers
? Object.fromEntries(options.headers.entries())
: { ...(options.headers || {}) };
const relayHeaders = {
...options.headers,
...baseHeaders,
"x-relay-target": `${parsed.protocol}//${parsed.host}`,
"x-relay-path": `${parsed.pathname}${parsed.search}`,
};

View File

@@ -60,7 +60,13 @@ export function createSSEStream(options = {}) {
const decoder = new TextDecoder("utf-8", { fatal: false });
const state = mode === STREAM_MODE.TRANSLATE
? { ...initState(sourceFormat), provider, toolNameMap, customToolNames: new Set(customToolNames || []), model, sessionId: credentials?._clientSessionId || null }
? { ...initState(sourceFormat), provider, toolNameMap, customToolNames: new Set(customToolNames || []), model, sessionId: credentials?._clientSessionId || null,
// Which upstream format this stream came from. A response translator can be
// reached either directly (target === its registered source) or as the second
// hop of a pivot, and on the terminal null chunk the pivot drops it — so a
// translator that defers closing events until flush needs to know which case
// it is in. Absent/undefined means "unknown", i.e. do not defer.
targetFormat }
: null;
let totalContentLength = 0;