Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

Merge PR #2251: fix(kiro): harden OpenAI-to-Kiro translator for API compliance (thanks @8mbe)

diegosouzapwd2b413c9ab5e
diff --cc open-sse/translator/request/openai-to-kiro.ts
index a558579f33,0e58e26070..50fc19a9fe
--- a/open-sse/translator/request/openai-to-kiro.ts
+++ b/open-sse/translator/request/openai-to-kiro.ts
@@@ -64,7 -98,7 +100,8 @@@ function convertMessages(messages, tool
userInputMessage: {
content: string;
modelId: string;
+ images?: Array<{ format: string; source: { bytes: string } }>;
+ origin: string;
userInputMessageContext?: {
toolResults?: Array<Record<string, unknown>>;
tools?: Array<Record<string, unknown>>;
@@@ -138,9 -149,8 +176,9 @@@
currentMessage = userMsg;
pendingUserContent = [];
pendingToolResults = [];
+ pendingImages = [];
} else if (currentRole === "assistant") {
- const content = pendingAssistantContent.join("\n\n").trim() || "...";
+ const content = pendingAssistantContent.join("\n\n").trim() || "(empty)";
const assistantMsg = {
assistantResponseMessage: {
content: content,
@@@ -412,7 -389,99 +470,99 @@@
}
}
- return { history: mergedHistory, currentMessage, toolsAttached };
+ // Ensure first message is user. Kiro API requires conversations to start
+ // with a user message (fixes "Improperly formed request" for assistant-first).
+ if (mergedHistory.length > 0 && mergedHistory[0].assistantResponseMessage) {
+ mergedHistory.unshift({
+ userInputMessage: {
+ content: "(empty)",
+ modelId: model,
+ origin: "AI_EDITOR",
+ },
+ });
+ }
+
+ // Ensure assistant exists before toolResults. Kiro API validates that every
+ // toolResults array has a preceding assistantResponseMessage with toolUses.
+ // When the assistant message is missing (truncated conversation), we strip
+ // the orphaned toolResults and convert them to text to preserve context.
+ for (let i = 0; i < mergedHistory.length; i++) {
+ const item = mergedHistory[i];
+ if (!item.userInputMessage?.userInputMessageContext?.toolResults) continue;
+
+ const prev = mergedHistory[i - 1];
+ const hasPrecedingAssistant =
+ prev?.assistantResponseMessage?.toolUses && prev.assistantResponseMessage.toolUses.length > 0;
+
+ if (!hasPrecedingAssistant) {
+ const toolResults = item.userInputMessage.userInputMessageContext.toolResults as Array<{
+ toolUseId?: string;
+ content?: Array<{ text?: string }>;
+ }>;
+ const toolResultTexts = toolResults
+ .map((tr) => {
+ const id = tr.toolUseId || "";
+ const text = tr.content?.map((c) => c.text || "").join("\n") || "";
+ return id ? `[Tool Result (${id})]\n${text}` : `[Tool Result]\n${text}`;
+ })
+ .join("\n\n");
+
+ const originalContent = item.userInputMessage.content || "";
+ item.userInputMessage.content = originalContent
+ ? `${originalContent}\n\n${toolResultTexts}`
+ : toolResultTexts;
+ delete item.userInputMessage.userInputMessageContext.toolResults;
+
+ if (Object.keys(item.userInputMessage.userInputMessageContext).length === 0) {
+ delete item.userInputMessage.userInputMessageContext;
+ }
+ }
+ }
+
+ // Also check currentMessage for orphaned toolResults (not in history)
+ if (currentMessage?.userInputMessage?.userInputMessageContext?.toolResults) {
+ const lastHistory = mergedHistory[mergedHistory.length - 1];
+ const hasPrecedingAssistant =
+ lastHistory?.assistantResponseMessage?.toolUses &&
+ lastHistory.assistantResponseMessage.toolUses.length > 0;
+
+ if (!hasPrecedingAssistant) {
+ const toolResults = currentMessage.userInputMessage.userInputMessageContext
+ .toolResults as Array<{ toolUseId?: string; content?: Array<{ text?: string }> }>;
+ const toolResultTexts = toolResults
+ .map((tr) => {
+ const id = tr.toolUseId || "";
+ const text = tr.content?.map((c) => c.text || "").join("\n") || "";
+ return id ? `[Tool Result (${id})]\n${text}` : `[Tool Result]\n${text}`;
+ })
+ .join("\n\n");
+
+ const originalContent = currentMessage.userInputMessage.content || "";
+ currentMessage.userInputMessage.content = originalContent
+ ? `${originalContent}\n\n${toolResultTexts}`
+ : toolResultTexts;
+ delete currentMessage.userInputMessage.userInputMessageContext.toolResults;
+
+ if (Object.keys(currentMessage.userInputMessage.userInputMessageContext).length === 0) {
+ delete currentMessage.userInputMessage.userInputMessageContext;
+ }
+ }
+ }
+
+ // Ensure alternating roles by inserting synthetic assistant messages
+ // between consecutive user turns that couldn't be merged.
+ const alternatingHistory: typeof mergedHistory = [];
+ for (const item of mergedHistory) {
+ const last = alternatingHistory[alternatingHistory.length - 1];
+ if (item.userInputMessage && last?.userInputMessage) {
+ alternatingHistory.push({
+ assistantResponseMessage: { content: "(empty)" },
+ });
+ }
+ alternatingHistory.push(item);
+ }
+
- return { history: alternatingHistory, currentMessage };
++ return { history: alternatingHistory, currentMessage, toolsAttached };
}
/**
diff --cc tests/unit/translator-openai-to-kiro.test.ts
index 6bccb7b1be,02d62e3612..53284396ff
--- a/tests/unit/translator-openai-to-kiro.test.ts
+++ b/tests/unit/translator-openai-to-kiro.test.ts
@@@ -290,152 -295,142 +295,292 @@@ test("OpenAI -> Kiro merges adjacent us
assert.equal(history[1].assistantResponseMessage?.content, "Answer 1");
});
+test("OpenAI -> Kiro synthesizes tools schema when body.tools is omitted but history has tool_calls", () => {
+ const result = buildKiroPayload(
+ "claude-opus-4.7",
+ {
+ messages: [
+ { role: "user", content: "Start" },
+ {
+ role: "assistant",
+ tool_calls: [
+ {
+ id: "tooluse_1",
+ type: "function",
+ function: { name: "edit", arguments: '{"path":"x"}' },
+ },
+ ],
+ },
+ { role: "tool", tool_call_id: "tooluse_1", content: "ok" },
+ {
+ role: "assistant",
+ tool_calls: [
+ {
+ id: "tooluse_2",
+ type: "function",
+ function: { name: "bash", arguments: '{"cmd":"ls"}' },
+ },
+ ],
+ },
+ { role: "tool", tool_call_id: "tooluse_2", content: "listing" },
+ { role: "user", content: "Continue" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const ctx = result.conversationState.currentMessage.userInputMessage.userInputMessageContext as {
+ tools?: Array<{ toolSpecification: { name: string } }>;
+ };
+ const tools = ctx?.tools;
+ assert.ok(tools, "synthesized tools schema should be attached to currentMessage");
+ const names = tools.map((t) => t.toolSpecification.name).sort();
+ assert.deepEqual(names, ["bash", "edit"]);
+});
+
+test("OpenAI -> Kiro does not override body.tools when caller already provides a schema", () => {
+ const result = buildKiroPayload(
+ "claude-opus-4.7",
+ {
+ messages: [
+ { role: "user", content: "Start" },
+ {
+ role: "assistant",
+ tool_calls: [
+ {
+ id: "tooluse_1",
+ type: "function",
+ function: { name: "read_file", arguments: "{}" },
+ },
+ ],
+ },
+ { role: "tool", tool_call_id: "tooluse_1", content: "ok" },
+ { role: "user", content: "Continue" },
+ ],
+ tools: [
+ {
+ type: "function",
+ function: {
+ name: "read_file",
+ description: "Real description",
+ parameters: { type: "object", properties: { path: { type: "string" } } },
+ },
+ },
+ ],
+ },
+ false,
+ null
+ );
+
+ const ctx = result.conversationState.currentMessage.userInputMessage.userInputMessageContext as {
+ tools?: Array<{ toolSpecification: { name: string; description: string } }>;
+ };
+ const tools = ctx.tools;
+ assert.ok(tools);
+ assert.equal(tools.length, 1);
+ assert.equal(tools[0].toolSpecification.description, "Real description");
+});
+
+test("OpenAI -> Kiro synthesizes tools from Anthropic-style tool_use content blocks", () => {
+ const result = buildKiroPayload(
+ "claude-opus-4.7",
+ {
+ messages: [
+ { role: "user", content: "Start" },
+ {
+ role: "assistant",
+ content: [
+ { type: "text", text: "Calling tools" },
+ { type: "tool_use", id: "tu_1", name: "search", input: { q: "x" } },
+ { type: "tool_use", id: "tu_2", name: "open_file", input: { path: "a" } },
+ ],
+ },
+ {
+ role: "user",
+ content: [
+ { type: "tool_result", tool_use_id: "tu_1", content: [{ type: "text", text: "hit" }] },
+ { type: "tool_result", tool_use_id: "tu_2", content: [{ type: "text", text: "ok" }] },
+ ],
+ },
+ { role: "user", content: "continue" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const ctx = result.conversationState.currentMessage.userInputMessage.userInputMessageContext as {
+ tools?: Array<{ toolSpecification: { name: string } }>;
+ };
+ const tools = ctx?.tools;
+ assert.ok(tools, "tools should be synthesized from tool_use content blocks");
+ const names = tools.map((t) => t.toolSpecification.name).sort();
+ assert.deepEqual(names, ["open_file", "search"]);
+});
+
+test("OpenAI -> Kiro attaches tools to currentMessage when history has no user turn to carry them", () => {
+ const result = buildKiroPayload(
+ "claude-opus-4.7",
+ {
+ messages: [
+ {
+ role: "assistant",
+ tool_calls: [
+ { id: "tc_1", type: "function", function: { name: "edit", arguments: "{}" } },
+ ],
+ },
+ ],
+ },
+ false,
+ null
+ );
+
+ const cm = result.conversationState.currentMessage.userInputMessage;
+ const ctx = cm.userInputMessageContext as {
+ tools?: Array<{ toolSpecification: { name: string } }>;
+ };
+ assert.ok(ctx?.tools, "tools should be attached to currentMessage fallback");
+ assert.equal(ctx.tools!.length, 1);
+ assert.equal(ctx.tools![0].toolSpecification.name, "edit");
+});
++
+ test("OpenAI -> Kiro strips additionalProperties and empty required from tool schemas", () => {
+ const result = buildKiroPayload(
+ "claude-sonnet-4",
+ {
+ messages: [{ role: "user", content: "Hi" }],
+ tools: [
+ {
+ type: "function",
+ function: {
+ name: "test_tool",
+ description: "Test",
+ parameters: {
+ type: "object",
+ properties: {
+ path: { type: "string", additionalProperties: false },
+ nested: {
+ type: "object",
+ properties: { id: { type: "string" } },
+ additionalProperties: true,
+ },
+ },
+ required: [],
+ additionalProperties: false,
+ },
+ },
+ },
+ ],
+ },
+ false,
+ null
+ );
+
+ const schema = result.conversationState.currentMessage.userInputMessage.userInputMessageContext
+ ?.tools?.[0]?.toolSpecification?.inputSchema?.json as any;
+
+ assert.ok(schema, "schema should exist");
+ assert.equal(
+ schema.additionalProperties,
+ undefined,
+ "top-level additionalProperties should be stripped"
+ );
+ assert.equal(schema.required, undefined, "empty required should be omitted");
+ assert.equal(
+ schema.properties.path.additionalProperties,
+ undefined,
+ "nested additionalProperties should be stripped"
+ );
+ assert.equal(
+ schema.properties.nested.additionalProperties,
+ undefined,
+ "deep nested additionalProperties should be stripped"
+ );
+ });
+
+ test("OpenAI -> Kiro merges consecutive assistant messages", () => {
+ const result = buildKiroPayload(
+ "claude-sonnet-4",
+ {
+ messages: [
+ { role: "user", content: "Hello" },
+ { role: "assistant", content: "Part 1" },
+ { role: "assistant", content: "Part 2" },
+ { role: "user", content: "Continue" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const history = result.conversationState.history as any[];
+ assert.equal(history.length, 2, "consecutive assistants should be merged into one");
+ assert.equal(history[0].userInputMessage.content, "Hello");
+ assert.equal(history[1].assistantResponseMessage.content, "Part 1\n\nPart 2");
+ });
+
+ test("OpenAI -> Kiro prepends synthetic user when conversation starts with assistant", () => {
+ const result = buildKiroPayload(
+ "claude-sonnet-4",
+ {
+ messages: [
+ { role: "assistant", content: "Greeting" },
+ { role: "user", content: "Hello" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const history = result.conversationState.history as any[];
+ assert.equal(history.length, 2);
+ assert.equal(history[0].userInputMessage.content, "(empty)");
+ assert.equal(history[0].userInputMessage.origin, "AI_EDITOR");
+ assert.equal(history[1].assistantResponseMessage.content, "Greeting");
+ });
+
+ test("OpenAI -> Kiro converts orphaned tool results to text", () => {
+ const result = buildKiroPayload(
+ "claude-sonnet-4",
+ {
+ messages: [
+ { role: "user", content: "First" },
+ { role: "assistant", content: "Answer" },
+ { role: "tool", tool_call_id: "orphan_1", content: "result data" },
+ { role: "user", content: "Follow-up" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const currentMsg = result.conversationState.currentMessage.userInputMessage;
+ assert.match(currentMsg.content, /Follow-up\n\n\[Tool Result \(orphan_1\)\]\nresult data$/);
+ assert.equal(
+ currentMsg.userInputMessageContext,
+ undefined,
+ "orphaned toolResults should be removed from context"
+ );
+ });
+
+ test("OpenAI -> Kiro includes origin on all history user messages", () => {
+ const result = buildKiroPayload(
+ "claude-sonnet-4",
+ {
+ messages: [
+ { role: "user", content: "A" },
+ { role: "assistant", content: "B" },
+ { role: "user", content: "C" },
+ ],
+ },
+ false,
+ null
+ );
+
+ const history = result.conversationState.history as any[];
+ assert.equal(history[0].userInputMessage.origin, "AI_EDITOR");
+ assert.equal(history[1].assistantResponseMessage.content, "B");
+ // Note: last user message becomes currentMessage, not history
+ assert.equal(history.length, 2);
+ });