From dc71125eb7c323aeeca0e96be1be2bedc8b41844 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Wed, 6 Aug 2025 03:11:44 +0000 Subject: [PATCH] fix: include assistant messages in codex-mini-latest conversation context - Updated convertMessagesToInput to include both user and assistant messages - Added role prefixes (User:/Assistant:) to maintain conversation context - Added test for multi-turn conversations to ensure proper handling - This fixes the issue where assistant responses were excluded in multi-turn conversations --- .../providers/__tests__/openai-native.spec.ts | 31 ++++++++++++++++++- src/api/providers/openai-native.ts | 23 ++++++++------ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/api/providers/__tests__/openai-native.spec.ts b/src/api/providers/__tests__/openai-native.spec.ts index 02b749023d..863691315f 100644 --- a/src/api/providers/__tests__/openai-native.spec.ts +++ b/src/api/providers/__tests__/openai-native.spec.ts @@ -483,7 +483,7 @@ describe("OpenAiNativeHandler", () => { expect(mockResponsesStream).toHaveBeenCalledWith({ model: "codex-mini-latest", instructions: systemPrompt, - input: "Hello!", + input: "User: Hello!", }) const textChunks = chunks.filter((chunk) => chunk.type === "text") @@ -492,6 +492,35 @@ describe("OpenAiNativeHandler", () => { expect(textChunks[1].text).toBe(" world") }) + it("should handle multi-turn conversations with assistant messages", async () => { + const multiTurnMessages: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: "What is 2+2?", + }, + { + role: "assistant", + content: "2+2 equals 4.", + }, + { + role: "user", + content: "What about 3+3?", + }, + ] + + const responseStream = handler.createMessage(systemPrompt, multiTurnMessages) + const chunks: any[] = [] + for await (const chunk of responseStream) { + chunks.push(chunk) + } + + expect(mockResponsesStream).toHaveBeenCalledWith({ + model: "codex-mini-latest", + instructions: systemPrompt, + input: "User: What is 2+2?\n\nAssistant: 2+2 equals 4.\n\nUser: What about 3+3?", + }) + }) + it("should handle non-streaming completion via v1/responses", async () => { const result = await handler.completePrompt("Test prompt") diff --git a/src/api/providers/openai-native.ts b/src/api/providers/openai-native.ts index 45470f1407..b0df288ad1 100644 --- a/src/api/providers/openai-native.ts +++ b/src/api/providers/openai-native.ts @@ -173,15 +173,20 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio private convertMessagesToInput(messages: Anthropic.Messages.MessageParam[]): string { return messages .map((msg) => { - if (msg.role === "user") { - if (typeof msg.content === "string") { - return msg.content - } else if (Array.isArray(msg.content)) { - return msg.content - .filter((part) => part.type === "text") - .map((part) => part.text) - .join("\n") - } + let content = "" + + if (typeof msg.content === "string") { + content = msg.content + } else if (Array.isArray(msg.content)) { + content = msg.content + .filter((part) => part.type === "text") + .map((part) => part.text) + .join("\n") + } + + // Include role prefix to maintain conversation context + if (content) { + return msg.role === "user" ? `User: ${content}` : `Assistant: ${content}` } return "" })