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);