-
Notifications
You must be signed in to change notification settings - Fork 49
fix: handle array content format in OpenAI messages #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |||||||||||||
| * Converts OpenAI chat request format to Claude CLI input | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| import type { OpenAIChatRequest } from "../types/openai.js"; | ||||||||||||||
| import type { | ||||||||||||||
| OpenAIChatRequest, | ||||||||||||||
| OpenAIContentPart, | ||||||||||||||
| } from "../types/openai.js"; | ||||||||||||||
|
|
||||||||||||||
| export type ClaudeModel = "opus" | "sonnet" | "haiku"; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -46,6 +49,32 @@ export function extractModel(model: string): ClaudeModel { | |||||||||||||
| return "opus"; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Extract text from message content. | ||||||||||||||
| * | ||||||||||||||
| * OpenAI API allows content to be either a plain string or an array of | ||||||||||||||
| * content parts (e.g. [{type: "text", text: "..."}]). This function | ||||||||||||||
| * normalises both forms into a single string. | ||||||||||||||
| */ | ||||||||||||||
| export function extractContent( | ||||||||||||||
| content: string | OpenAIContentPart[], | ||||||||||||||
| ): string { | ||||||||||||||
| if (typeof content === "string") return content; | ||||||||||||||
|
|
||||||||||||||
| if (Array.isArray(content)) { | ||||||||||||||
| return content | ||||||||||||||
| .map((part) => { | ||||||||||||||
| if (typeof part === "string") return part; | ||||||||||||||
| if (part && typeof part === "object") return part.text ?? ""; | ||||||||||||||
| return ""; | ||||||||||||||
| }) | ||||||||||||||
|
Comment on lines
+66
to
+70
|
||||||||||||||
| .map((part) => { | |
| if (typeof part === "string") return part; | |
| if (part && typeof part === "object") return part.text ?? ""; | |
| return ""; | |
| }) | |
| .map((part) => part.text ?? "") |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,9 +3,19 @@ | |||||||||||||||||||||||||||||
| * Used for Clawdbot integration | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * A single content part in a multi-part message. | ||||||||||||||||||||||||||||||
| * See: https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages | ||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||
| export interface OpenAIContentPart { | ||||||||||||||||||||||||||||||
| type: "text" | "image_url"; | ||||||||||||||||||||||||||||||
| text?: string; | ||||||||||||||||||||||||||||||
| image_url?: { url: string; detail?: string }; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+14
|
||||||||||||||||||||||||||||||
| export interface OpenAIContentPart { | |
| type: "text" | "image_url"; | |
| text?: string; | |
| image_url?: { url: string; detail?: string }; | |
| } | |
| export type OpenAIContentPart = | |
| | { | |
| type: "text"; | |
| text: string; | |
| } | |
| | { | |
| type: "image_url"; | |
| image_url: { url: string; detail?: string }; | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function silently ignores image_url content parts by only extracting the text field. When a message contains image content (type: "image_url"), this data is lost without any indication to the user. Consider either logging a warning when image content is encountered, or documenting this limitation in the function's docstring, since Claude CLI may not support image inputs.