feat: enhance CommandCode integration with improved message handling

This commit is contained in:
decolua
2026-05-07 23:02:07 +07:00
parent b72a443bd3
commit ad661c1286
5 changed files with 446 additions and 23 deletions

View File

@@ -1,17 +1,32 @@
/**
* OpenAI → CommandCode request translator
*
* CommandCode endpoint expects an envelope:
* { threadId, memory, config, params: { model, messages, stream, max_tokens, temperature, tools? } }
* where `params.messages` are Anthropic-style content blocks ([{type:"text", text}, ...]).
*
* The model id received here is the upstream id (e.g. "deepseek/deepseek-v4-pro") thanks to the
* `provider/model` registration in providerModels.js.
* Upstream `/alpha/generate` schema (verified live with curl 2026-05-07):
* - params.system: STRING at top level (Anthropic-style; system messages NOT allowed in messages[])
* - params.messages[*].role ∈ {"user","assistant","tool"}
* - params.messages[*].content: Array of content blocks (NEVER a string)
* - tool_use blocks (assistant): {type:"tool-call", toolCallId, toolName, input}
* - tool_result blocks (role=user): {type:"tool-result", toolCallId, toolName, output}
* - tools[*]: Anthropic plain {name, description, input_schema}
*/
import { register } from "../index.js";
import { FORMATS } from "../formats.js";
import { randomUUID } from "crypto";
function flattenText(content) {
if (content == null) return "";
if (typeof content === "string") return content;
if (Array.isArray(content)) {
const parts = [];
for (const p of content) {
if (typeof p === "string") parts.push(p);
else if (p && typeof p === "object" && typeof p.text === "string") parts.push(p.text);
}
return parts.join("\n");
}
return String(content);
}
function toContentBlocks(content) {
if (content == null) return [{ type: "text", text: "" }];
if (typeof content === "string") return [{ type: "text", text: content }];
@@ -24,8 +39,6 @@ function toContentBlocks(content) {
if (part.type === "text" && typeof part.text === "string") {
blocks.push({ type: "text", text: part.text });
} else if (part.type === "image_url" || part.type === "image") {
// CommandCode currently rejects multimodal blocks via this gateway;
// collapse to a textual placeholder so the request still validates.
blocks.push({ type: "text", text: "[image omitted]" });
} else if (typeof part.text === "string") {
blocks.push({ type: "text", text: part.text });
@@ -37,25 +50,101 @@ function toContentBlocks(content) {
return [{ type: "text", text: String(content) }];
}
function safeParseJson(s) {
if (s == null) return {};
if (typeof s !== "string") return s;
try { return JSON.parse(s); } catch { return {}; }
}
function convertMessages(messages = []) {
return messages.map((m) => {
const role = m.role === "tool" ? "user" : (m.role || "user");
return { role, content: toContentBlocks(m.content) };
});
const out = [];
const systemTexts = [];
for (const m of messages) {
if (!m) continue;
const role = m.role;
if (role === "system") {
const t = flattenText(m.content);
if (t) systemTexts.push(t);
continue;
}
if (role === "tool") {
const value = typeof m.content === "string" ? m.content : flattenText(m.content);
out.push({
role: "tool",
content: [{
type: "tool-result",
toolCallId: m.tool_call_id || "",
toolName: m.name || "",
output: { type: "text", value },
}],
});
continue;
}
if (role === "assistant") {
const blocks = [];
const text = flattenText(m.content);
if (text) blocks.push({ type: "text", text });
if (Array.isArray(m.tool_calls)) {
for (const tc of m.tool_calls) {
const fn = tc.function || {};
blocks.push({
type: "tool-call",
toolCallId: tc.id || "",
toolName: fn.name || "",
input: safeParseJson(fn.arguments),
});
}
}
out.push({ role: "assistant", content: blocks.length ? blocks : [{ type: "text", text: "" }] });
continue;
}
out.push({ role: "user", content: toContentBlocks(m.content) });
}
return { messages: out, system: systemTexts.join("\n\n") };
}
function convertTools(tools) {
if (!Array.isArray(tools) || tools.length === 0) return undefined;
const result = [];
for (const t of tools) {
if (!t) continue;
if (t.type === "function" && t.function) {
result.push({
name: t.function.name,
description: t.function.description,
input_schema: t.function.parameters || { type: "object" },
});
} else if (t.name && (t.input_schema || t.parameters)) {
result.push({
name: t.name,
description: t.description,
input_schema: t.input_schema || t.parameters,
});
}
}
return result.length ? result : undefined;
}
export function openaiToCommandCode(model, body, stream /* , credentials */) {
const { messages, system } = convertMessages(body.messages);
const params = {
model,
messages: convertMessages(body.messages),
messages,
stream: stream !== false,
max_tokens: body.max_tokens ?? body.max_output_tokens ?? 64000,
temperature: body.temperature ?? 0.3,
};
if (Array.isArray(body.tools) && body.tools.length > 0) {
params.tools = body.tools;
}
if (system) params.system = system;
const tools = convertTools(body.tools);
if (tools) params.tools = tools;
if (body.top_p != null) params.top_p = body.top_p;
const today = new Date().toISOString().slice(0, 10);

View File

@@ -5,8 +5,9 @@
* {"type":"start"} {"type":"start-step", ...}
* {"type":"reasoning-start","id":"..."} {"type":"reasoning-delta","text":"..."}
* {"type":"text-start","id":"..."} {"type":"text-delta","text":"..."}
* {"type":"tool-input-start","toolCallId","toolName"}
* {"type":"tool-input-delta","toolCallId","inputTextDelta"}
* {"type":"tool-input-start","id","toolName"}
* {"type":"tool-input-delta","id","delta"}
* {"type":"tool-input-end","id"}
* {"type":"tool-call","toolCallId","toolName","input"}
* {"type":"finish-step","finishReason","usage": {...}, ...}
* {"type":"finish",...}
@@ -104,7 +105,7 @@ export function convertCommandCodeToOpenAI(chunk, state) {
break;
}
case "tool-input-start": {
const id = event.toolCallId || `call_${Date.now()}_${state.toolIndex}`;
const id = event.id || event.toolCallId || `call_${Date.now()}_${state.toolIndex}`;
let idx = state.toolIndexById.get(id);
if (idx == null) {
idx = state.toolIndex++;
@@ -125,13 +126,13 @@ export function convertCommandCodeToOpenAI(chunk, state) {
break;
}
case "tool-input-delta": {
const id = event.toolCallId;
const id = event.id || event.toolCallId;
const idx = state.toolIndexById.get(id);
if (idx == null) break;
const delta = {
tool_calls: [{
index: idx,
function: { arguments: event.inputTextDelta || event.delta || "" },
function: { arguments: event.delta || event.inputTextDelta || "" },
}],
};
out.push(makeChunk(state, delta));
@@ -178,7 +179,9 @@ export function convertCommandCodeToOpenAI(chunk, state) {
}
case "error": {
state.finishReason = "stop";
out.push(makeChunk(state, { content: `\n\n[CommandCode error: ${event.error || event.message || "unknown"}]` }));
const errVal = event.error ?? event.message ?? "unknown";
const errStr = typeof errVal === "string" ? errVal : JSON.stringify(errVal);
out.push(makeChunk(state, { content: `\n\n[CommandCode error: ${errStr}]` }));
out.push(makeChunk(state, {}, "stop"));
break;
}