feat(opencode-go): add muse-spark-1.3-contributor and fix parallel tool calls on Responses paths (#3819)
- Add muse-spark-1.3-contributor as responses-only model on OpenCode Go with dedicated executor - Key Responses→chat streaming tool calls by item_id to prevent parallel tool calls merging into index 0 - Standardize tool coercions and call_id clamping in Responses API translation
This commit is contained in:
@@ -23,6 +23,46 @@ export function normalizeResponsesInput(input) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Strict Responses upstreams reject overlong call_ids with InputValidationError (#393).
|
||||
export const MAX_RESPONSES_CALL_ID_LEN = 64;
|
||||
|
||||
export function clampResponsesCallId(id) {
|
||||
if (typeof id !== "string" || !id) return `call_${Date.now()}`;
|
||||
return id.length > MAX_RESPONSES_CALL_ID_LEN ? id.substring(0, MAX_RESPONSES_CALL_ID_LEN) : id;
|
||||
}
|
||||
|
||||
// Single-stringify: objects → JSON once; valid JSON strings pass through untouched;
|
||||
// anything else (partial fragments, empty) falls back to "{}" instead of
|
||||
// double-encoding and tripping upstream InputValidationError.
|
||||
export function coerceResponsesArguments(value) {
|
||||
if (value === undefined || value === null || value === "") return "{}";
|
||||
if (typeof value !== "string") {
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch {
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
try {
|
||||
JSON.parse(value);
|
||||
return value;
|
||||
} catch {
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
|
||||
// function_call_output.output must be a string — never null/object.
|
||||
export function coerceResponsesOutput(value) {
|
||||
if (typeof value === "string") return value;
|
||||
if (value === undefined || value === null) return "";
|
||||
if (Array.isArray(value)) return value.map((c) => c?.text ?? JSON.stringify(c)).join("");
|
||||
try {
|
||||
return JSON.stringify(value);
|
||||
} catch {
|
||||
return String(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert OpenAI Responses API format to standard chat completions format
|
||||
* Responses API uses: { input: [...], instructions: "..." }
|
||||
|
||||
@@ -6,12 +6,15 @@
|
||||
*/
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
import { normalizeResponsesInput } from "../formats/responsesApi.js";
|
||||
import {
|
||||
normalizeResponsesInput,
|
||||
clampResponsesCallId,
|
||||
coerceResponsesArguments,
|
||||
coerceResponsesOutput,
|
||||
} from "../formats/responsesApi.js";
|
||||
import { ROLE, OPENAI_BLOCK, RESPONSES_ITEM } from "../schema/index.js";
|
||||
|
||||
// Responses API enforces max 64 chars on call_id (#393)
|
||||
const MAX_CALL_ID_LEN = 64;
|
||||
const clampCallId = (id) => (typeof id === "string" && id.length > MAX_CALL_ID_LEN ? id.substring(0, MAX_CALL_ID_LEN) : id);
|
||||
const MAX_TOOL_NAME_LEN = 128;
|
||||
|
||||
/**
|
||||
* Convert OpenAI Responses API request to OpenAI Chat Completions format
|
||||
@@ -249,6 +252,23 @@ export function openaiResponsesToOpenAIRequest(model, body, stream, credentials)
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract plain text from a system/developer message for Responses instructions.
|
||||
* Array content (text parts) is joined; anything else falls back to "" rather
|
||||
* than leaking "[object Object]" upstream.
|
||||
*/
|
||||
function extractInstructionsText(content) {
|
||||
if (typeof content === "string") return content;
|
||||
if (Array.isArray(content)) {
|
||||
return content.map((c) => {
|
||||
if (typeof c?.text === "string") return c.text;
|
||||
if (typeof c?.content === "string") return c.content;
|
||||
return "";
|
||||
}).filter(Boolean).join("\n");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure object schema always has properties field (required by Codex Responses API)
|
||||
*/
|
||||
@@ -327,7 +347,7 @@ export function openaiToOpenAIResponsesRequest(model, body, stream, credentials)
|
||||
// Use the first instruction-bearing message as instructions.
|
||||
// OpenAI recommends role="developer" for GPT-5/Codex as the system-level prompt.
|
||||
if (!hasSystemMessage) {
|
||||
result.instructions = typeof msg.content === "string" ? msg.content : "";
|
||||
result.instructions = extractInstructionsText(msg.content);
|
||||
hasSystemMessage = true;
|
||||
}
|
||||
continue; // Skip instruction messages in input
|
||||
@@ -378,26 +398,24 @@ export function openaiToOpenAIResponsesRequest(model, body, stream, credentials)
|
||||
// Convert tool calls
|
||||
if (msg.role === ROLE.ASSISTANT && msg.tool_calls) {
|
||||
for (const tc of msg.tool_calls) {
|
||||
// Skip nameless calls — strict Responses upstreams reject them (#444)
|
||||
const name = typeof tc.function?.name === "string" ? tc.function.name.trim() : "";
|
||||
if (!name) continue;
|
||||
result.input.push({
|
||||
type: RESPONSES_ITEM.FUNCTION_CALL,
|
||||
call_id: clampCallId(tc.id),
|
||||
name: tc.function?.name || "_unknown",
|
||||
arguments: tc.function?.arguments || "{}"
|
||||
call_id: clampResponsesCallId(tc.id),
|
||||
name: name.slice(0, MAX_TOOL_NAME_LEN),
|
||||
arguments: coerceResponsesArguments(tc.function?.arguments)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Convert tool results - output must be a string for Responses API
|
||||
if (msg.role === ROLE.TOOL) {
|
||||
const output = typeof msg.content === "string"
|
||||
? msg.content
|
||||
: Array.isArray(msg.content)
|
||||
? msg.content.map(c => c.text || JSON.stringify(c)).join("")
|
||||
: JSON.stringify(msg.content);
|
||||
result.input.push({
|
||||
type: RESPONSES_ITEM.FUNCTION_CALL_OUTPUT,
|
||||
call_id: clampCallId(msg.tool_call_id),
|
||||
output
|
||||
call_id: clampResponsesCallId(msg.tool_call_id),
|
||||
output: coerceResponsesOutput(msg.content)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -411,16 +429,19 @@ export function openaiToOpenAIResponsesRequest(model, body, stream, credentials)
|
||||
if (body.tools && Array.isArray(body.tools)) {
|
||||
result.tools = body.tools.map(tool => {
|
||||
if (tool.type === OPENAI_BLOCK.FUNCTION) {
|
||||
// Strict upstreams reject nameless/overlong tool declarations
|
||||
const name = typeof tool.function?.name === "string" ? tool.function.name.trim() : "";
|
||||
if (!name) return null;
|
||||
return {
|
||||
type: OPENAI_BLOCK.FUNCTION,
|
||||
name: tool.function.name,
|
||||
name: name.slice(0, MAX_TOOL_NAME_LEN),
|
||||
description: String(tool.function.description || ""),
|
||||
parameters: normalizeToolParameters(tool.function.parameters),
|
||||
strict: tool.function.strict
|
||||
};
|
||||
}
|
||||
return tool;
|
||||
});
|
||||
}).filter(Boolean);
|
||||
}
|
||||
|
||||
// Pass through other relevant fields
|
||||
|
||||
@@ -446,6 +446,13 @@ export function openaiResponsesToOpenAIResponse(chunk, state) {
|
||||
state.created = Math.floor(Date.now() / 1000);
|
||||
state.toolCallIndex = 0;
|
||||
state.currentToolCallId = null;
|
||||
// item_id → chat tool_calls index. Deltas carry item_id; keying on it (not
|
||||
// stream position) keeps parallel calls separate when upstream emits all
|
||||
// output_item.added events before any done/delta. Lazily created so callers
|
||||
// that build their own state object (stream.js) need no changes.
|
||||
state.respToolChatIndex ??= new Map();
|
||||
// Indices that already received argument deltas (guards done-with-args).
|
||||
state.respToolArgsEmitted ??= new Set();
|
||||
}
|
||||
|
||||
// Text content delta
|
||||
@@ -464,16 +471,29 @@ export function openaiResponsesToOpenAIResponse(chunk, state) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Function call started (standard function_call or custom_tool_call)
|
||||
// Function call started (standard function_call or custom_tool_call).
|
||||
// Index is assigned here (not on done): attributing deltas by stream position
|
||||
// merges parallel calls into index 0 whenever upstream emits all addeds
|
||||
// before dones — the client then concatenates N JSON payloads into one
|
||||
// tool input and fails validation. The server item id is the correlator.
|
||||
if (eventType === "response.output_item.added" && (data.item?.type === RESPONSES_ITEM.FUNCTION_CALL || data.item?.type === "custom_tool_call")) {
|
||||
const item = data.item;
|
||||
state.currentToolCallId = item.call_id || fallbackToolCallId();
|
||||
state.respToolChatIndex ??= new Map();
|
||||
const key = item.id || data.item_id || state.currentToolCallId;
|
||||
let idx;
|
||||
if (key && state.respToolChatIndex.has(key)) {
|
||||
idx = state.respToolChatIndex.get(key); // duplicate added (retry) — reuse
|
||||
} else {
|
||||
idx = state.toolCallIndex++;
|
||||
if (key) state.respToolChatIndex.set(key, idx);
|
||||
}
|
||||
|
||||
return buildChunk(
|
||||
{ id: state.chatId, created: state.created, model: state.model || MODEL_FALLBACK },
|
||||
{
|
||||
tool_calls: [{
|
||||
index: state.toolCallIndex,
|
||||
index: idx,
|
||||
id: state.currentToolCallId,
|
||||
type: OPENAI_BLOCK.FUNCTION,
|
||||
function: { name: item.name || "", arguments: "" }
|
||||
@@ -482,20 +502,39 @@ export function openaiResponsesToOpenAIResponse(chunk, state) {
|
||||
);
|
||||
}
|
||||
|
||||
// Function call arguments delta (standard or custom_tool_call variant)
|
||||
// Function call arguments delta (standard or custom_tool_call variant).
|
||||
// Routed by item_id so interleaved parallel fragments stay on their own call.
|
||||
if (eventType === "response.function_call_arguments.delta" || eventType === "response.custom_tool_call_input.delta") {
|
||||
const argsDelta = data.delta || "";
|
||||
if (!argsDelta) return null;
|
||||
|
||||
const known = data.item_id ? state.respToolChatIndex?.get(data.item_id) : undefined;
|
||||
const idx = known ?? Math.max(0, (state.toolCallIndex || 1) - 1);
|
||||
state.respToolArgsEmitted ??= new Set();
|
||||
state.respToolArgsEmitted.add(idx);
|
||||
return buildChunk(
|
||||
{ id: state.chatId, created: state.created, model: state.model || MODEL_FALLBACK },
|
||||
{ tool_calls: [{ index: state.toolCallIndex, function: { arguments: argsDelta } }] }
|
||||
{ tool_calls: [{ index: idx, function: { arguments: argsDelta } }] }
|
||||
);
|
||||
}
|
||||
|
||||
// Function call done (standard or custom_tool_call variant)
|
||||
// Function call done (standard or custom_tool_call variant).
|
||||
// Index was assigned at added-time; nothing to advance. Some upstreams send
|
||||
// complete arguments only here (no deltas) — emit them once in that case.
|
||||
if (eventType === "response.output_item.done" && (data.item?.type === RESPONSES_ITEM.FUNCTION_CALL || data.item?.type === "custom_tool_call")) {
|
||||
state.toolCallIndex++;
|
||||
const key = data.item?.id || data.item_id;
|
||||
const idx = (key && state.respToolChatIndex?.get(key)) ?? Math.max(0, (state.toolCallIndex || 1) - 1);
|
||||
const fullArgs = data.item?.arguments;
|
||||
if (typeof fullArgs === "string" && fullArgs) {
|
||||
state.respToolArgsEmitted ??= new Set();
|
||||
if (!state.respToolArgsEmitted.has(idx)) {
|
||||
state.respToolArgsEmitted.add(idx);
|
||||
return buildChunk(
|
||||
{ id: state.chatId, created: state.created, model: state.model || MODEL_FALLBACK },
|
||||
{ tool_calls: [{ index: idx, function: { arguments: fullArgs } }] }
|
||||
);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user