Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/adapter/openai-to-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -59,17 +80,17 @@ export function messagesToPrompt(messages: OpenAIChatRequest["messages"]): strin
switch (msg.role) {
case "system":
// System messages become context instructions
parts.push(`<system>\n${msg.content}\n</system>\n`);
parts.push(`<system>\n${extractContent(msg.content)}\n</system>\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(`<previous_response>\n${msg.content}\n</previous_response>\n`);
parts.push(`<previous_response>\n${extractContent(msg.content)}\n</previous_response>\n`);
break;
}
}
Expand Down