From b67e94b9c1c4a5aa84c217877314bbcf93a64082 Mon Sep 17 00:00:00 2001 From: David Stone Date: Tue, 17 Feb 2026 01:07:04 -0700 Subject: [PATCH] Fix: Handle array-style content blocks in messagesToPrompt OpenAI API supports both string and array content formats for messages. When content is an array of objects (e.g. [{"type": "text", "text": "..."}]), JavaScript's template literal interpolation converts it to "[object Object]" instead of the actual text content. This adds an extractContent() helper that: - Returns string content as-is - Extracts and joins text from array-style content blocks - Falls back to String() for any other type All msg.content references in messagesToPrompt are updated to use extractContent(msg.content) for the system, user, and assistant cases. Co-Authored-By: Claude Opus 4.6 --- src/adapter/openai-to-cli.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/adapter/openai-to-cli.ts b/src/adapter/openai-to-cli.ts index c8ecaa1..89a80da 100644 --- a/src/adapter/openai-to-cli.ts +++ b/src/adapter/openai-to-cli.ts @@ -46,6 +46,27 @@ export function extractModel(model: string): ClaudeModel { return "opus"; } +/** + * Extract text content from OpenAI message content. + * + * OpenAI API supports both string content and array-style content blocks + * (e.g. [{"type": "text", "text": "..."}]). When content is an array, + * naive string interpolation produces "[object Object]". This helper + * extracts the actual text from both formats. + */ +function extractContent(content: string | Array<{type: string; text?: string}>): string { + if (typeof content === "string") { + return content; + } + if (Array.isArray(content)) { + return content + .filter((block) => block.type === "text") + .map((block) => block.text ?? "") + .join("\n"); + } + return String(content ?? ""); +} + /** * Convert OpenAI messages array to a single prompt string for Claude CLI * @@ -59,17 +80,17 @@ export function messagesToPrompt(messages: OpenAIChatRequest["messages"]): strin switch (msg.role) { case "system": // System messages become context instructions - parts.push(`\n${msg.content}\n\n`); + parts.push(`\n${extractContent(msg.content)}\n\n`); break; case "user": // User messages are the main prompt - parts.push(msg.content); + parts.push(extractContent(msg.content)); break; case "assistant": // Previous assistant responses for context - parts.push(`\n${msg.content}\n\n`); + parts.push(`\n${extractContent(msg.content)}\n\n`); break; } }