feat: add CommandCode provider support
This commit is contained in:
@@ -11,7 +11,8 @@ export const FORMATS = {
|
||||
ANTIGRAVITY: "antigravity",
|
||||
KIRO: "kiro",
|
||||
CURSOR: "cursor",
|
||||
OLLAMA: "ollama"
|
||||
OLLAMA: "ollama",
|
||||
COMMANDCODE: "commandcode"
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,7 @@ function ensureInitialized() {
|
||||
require("./request/openai-to-kiro.js");
|
||||
require("./request/openai-to-cursor.js");
|
||||
require("./request/openai-to-ollama.js");
|
||||
require("./request/openai-to-commandcode.js");
|
||||
|
||||
// Response translators
|
||||
require("./response/claude-to-openai.js");
|
||||
@@ -50,6 +51,7 @@ function ensureInitialized() {
|
||||
require("./response/kiro-to-openai.js");
|
||||
require("./response/cursor-to-openai.js");
|
||||
require("./response/ollama-to-openai.js");
|
||||
require("./response/commandcode-to-openai.js");
|
||||
}
|
||||
|
||||
// Strip specific content types from messages (explicit opt-in via strip[] in PROVIDER_MODELS)
|
||||
|
||||
81
open-sse/translator/request/openai-to-commandcode.js
Normal file
81
open-sse/translator/request/openai-to-commandcode.js
Normal file
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
import { randomUUID } from "crypto";
|
||||
|
||||
function toContentBlocks(content) {
|
||||
if (content == null) return [{ type: "text", text: "" }];
|
||||
if (typeof content === "string") return [{ type: "text", text: content }];
|
||||
if (Array.isArray(content)) {
|
||||
const blocks = [];
|
||||
for (const part of content) {
|
||||
if (typeof part === "string") {
|
||||
blocks.push({ type: "text", text: part });
|
||||
} else if (part && typeof part === "object") {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
return blocks.length ? blocks : [{ type: "text", text: "" }];
|
||||
}
|
||||
return [{ type: "text", text: String(content) }];
|
||||
}
|
||||
|
||||
function convertMessages(messages = []) {
|
||||
return messages.map((m) => {
|
||||
const role = m.role === "tool" ? "user" : (m.role || "user");
|
||||
return { role, content: toContentBlocks(m.content) };
|
||||
});
|
||||
}
|
||||
|
||||
export function openaiToCommandCode(model, body, stream /* , credentials */) {
|
||||
const params = {
|
||||
model,
|
||||
messages: convertMessages(body.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 (body.top_p != null) params.top_p = body.top_p;
|
||||
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
|
||||
return {
|
||||
threadId: randomUUID(),
|
||||
memory: "",
|
||||
config: {
|
||||
workingDir: process.cwd(),
|
||||
date: today,
|
||||
environment: process.platform,
|
||||
structure: [],
|
||||
isGitRepo: false,
|
||||
currentBranch: "",
|
||||
mainBranch: "",
|
||||
gitStatus: "",
|
||||
recentCommits: [],
|
||||
},
|
||||
params,
|
||||
};
|
||||
}
|
||||
|
||||
register(FORMATS.OPENAI, FORMATS.COMMANDCODE, openaiToCommandCode, null);
|
||||
194
open-sse/translator/response/commandcode-to-openai.js
Normal file
194
open-sse/translator/response/commandcode-to-openai.js
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* CommandCode → OpenAI response translator
|
||||
*
|
||||
* CommandCode upstream emits NDJSON-style AI SDK v5 stream events:
|
||||
* {"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-call","toolCallId","toolName","input"}
|
||||
* {"type":"finish-step","finishReason","usage": {...}, ...}
|
||||
* {"type":"finish",...}
|
||||
*
|
||||
* Each upstream "event" arrives as one JSON object per line — we receive it as a string chunk
|
||||
* already split per line by the upstream SSE/JSON-line reader in 9router.
|
||||
*/
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
|
||||
function ensureState(state, model) {
|
||||
if (!state.responseId) {
|
||||
state.responseId = `chatcmpl-${Date.now()}`;
|
||||
state.created = Math.floor(Date.now() / 1000);
|
||||
state.model = state.model || model || "commandcode";
|
||||
state.chunkIndex = 0;
|
||||
state.toolIndex = 0;
|
||||
state.toolIndexById = new Map();
|
||||
state.openTools = new Set();
|
||||
state.openText = false;
|
||||
state.finishReason = null;
|
||||
state.usage = null;
|
||||
}
|
||||
}
|
||||
|
||||
function makeChunk(state, delta, finishReason = null) {
|
||||
return {
|
||||
id: state.responseId,
|
||||
object: "chat.completion.chunk",
|
||||
created: state.created,
|
||||
model: state.model,
|
||||
choices: [{ index: 0, delta, finish_reason: finishReason }],
|
||||
};
|
||||
}
|
||||
|
||||
function mapFinishReason(reason) {
|
||||
switch (reason) {
|
||||
case "stop": return "stop";
|
||||
case "length": return "length";
|
||||
case "tool-calls":
|
||||
case "tool_use": return "tool_calls";
|
||||
case "content-filter": return "content_filter";
|
||||
case "error": return "stop";
|
||||
default: return reason || "stop";
|
||||
}
|
||||
}
|
||||
|
||||
export function convertCommandCodeToOpenAI(chunk, state) {
|
||||
if (!chunk) return null;
|
||||
|
||||
// Already-OpenAI chunk: pass through
|
||||
if (chunk && typeof chunk === "object" && chunk.object === "chat.completion.chunk") {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
// Parse string lines coming out of upstream
|
||||
let event = chunk;
|
||||
if (typeof chunk === "string") {
|
||||
const line = chunk.trim();
|
||||
if (!line) return null;
|
||||
// Tolerate raw "data: {...}" framing if the upstream wrapper inserts it
|
||||
const json = line.startsWith("data:") ? line.slice(5).trim() : line;
|
||||
if (!json || json === "[DONE]") return null;
|
||||
try {
|
||||
event = JSON.parse(json);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
if (!event || typeof event !== "object" || !event.type) return null;
|
||||
|
||||
ensureState(state, event.model);
|
||||
const out = [];
|
||||
|
||||
switch (event.type) {
|
||||
case "text-delta": {
|
||||
const text = event.text || event.delta || "";
|
||||
if (!text) break;
|
||||
const delta = state.chunkIndex === 0 ? { role: "assistant", content: text } : { content: text };
|
||||
state.chunkIndex++;
|
||||
state.openText = true;
|
||||
out.push(makeChunk(state, delta));
|
||||
break;
|
||||
}
|
||||
case "reasoning-delta": {
|
||||
const text = event.text || "";
|
||||
if (!text) break;
|
||||
// Map reasoning to OpenAI "reasoning_content" field (used by deepseek-reasoner-style clients).
|
||||
const delta = state.chunkIndex === 0
|
||||
? { role: "assistant", reasoning_content: text }
|
||||
: { reasoning_content: text };
|
||||
state.chunkIndex++;
|
||||
out.push(makeChunk(state, delta));
|
||||
break;
|
||||
}
|
||||
case "tool-input-start": {
|
||||
const id = event.toolCallId || `call_${Date.now()}_${state.toolIndex}`;
|
||||
let idx = state.toolIndexById.get(id);
|
||||
if (idx == null) {
|
||||
idx = state.toolIndex++;
|
||||
state.toolIndexById.set(id, idx);
|
||||
}
|
||||
state.openTools.add(id);
|
||||
const delta = {
|
||||
...(state.chunkIndex === 0 ? { role: "assistant" } : {}),
|
||||
tool_calls: [{
|
||||
index: idx,
|
||||
id,
|
||||
type: "function",
|
||||
function: { name: event.toolName || "", arguments: "" },
|
||||
}],
|
||||
};
|
||||
state.chunkIndex++;
|
||||
out.push(makeChunk(state, delta));
|
||||
break;
|
||||
}
|
||||
case "tool-input-delta": {
|
||||
const 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 || "" },
|
||||
}],
|
||||
};
|
||||
out.push(makeChunk(state, delta));
|
||||
break;
|
||||
}
|
||||
case "tool-call": {
|
||||
// Final consolidated tool call — only emit if we never saw tool-input-* deltas.
|
||||
const id = event.toolCallId;
|
||||
if (state.toolIndexById.has(id)) break;
|
||||
const idx = state.toolIndex++;
|
||||
state.toolIndexById.set(id, idx);
|
||||
const argsStr = typeof event.input === "string" ? event.input : JSON.stringify(event.input ?? {});
|
||||
const delta = {
|
||||
...(state.chunkIndex === 0 ? { role: "assistant" } : {}),
|
||||
tool_calls: [{
|
||||
index: idx,
|
||||
id,
|
||||
type: "function",
|
||||
function: { name: event.toolName || "", arguments: argsStr },
|
||||
}],
|
||||
};
|
||||
state.chunkIndex++;
|
||||
out.push(makeChunk(state, delta));
|
||||
break;
|
||||
}
|
||||
case "finish-step": {
|
||||
state.finishReason = mapFinishReason(event.finishReason);
|
||||
if (event.usage) state.usage = event.usage;
|
||||
break;
|
||||
}
|
||||
case "finish": {
|
||||
const finishReason = state.finishReason || mapFinishReason(event.finishReason || "stop");
|
||||
const finalChunk = makeChunk(state, {}, finishReason);
|
||||
const totalUsage = event.totalUsage || state.usage;
|
||||
if (totalUsage) {
|
||||
finalChunk.usage = {
|
||||
prompt_tokens: totalUsage.inputTokens ?? 0,
|
||||
completion_tokens: totalUsage.outputTokens ?? 0,
|
||||
total_tokens: totalUsage.totalTokens ?? ((totalUsage.inputTokens ?? 0) + (totalUsage.outputTokens ?? 0)),
|
||||
};
|
||||
}
|
||||
out.push(finalChunk);
|
||||
break;
|
||||
}
|
||||
case "error": {
|
||||
state.finishReason = "stop";
|
||||
out.push(makeChunk(state, { content: `\n\n[CommandCode error: ${event.error || event.message || "unknown"}]` }));
|
||||
out.push(makeChunk(state, {}, "stop"));
|
||||
break;
|
||||
}
|
||||
// Silently ignore: start, start-step, reasoning-start, reasoning-end, text-start, text-end,
|
||||
// provider-metadata, message-metadata, etc. They carry no client-visible content.
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return out.length ? out : null;
|
||||
}
|
||||
|
||||
register(FORMATS.COMMANDCODE, FORMATS.OPENAI, null, convertCommandCodeToOpenAI);
|
||||
Reference in New Issue
Block a user