( content: unknown, )
| 233 | } |
| 234 | |
| 235 | function buildMessageContent( |
| 236 | content: unknown, |
| 237 | ): string | ResponsesApiContentPart[] | null { |
| 238 | if (typeof content === 'string') return content || null |
| 239 | if (!Array.isArray(content)) { |
| 240 | const text = JSON.stringify(content) |
| 241 | return text || null |
| 242 | } |
| 243 | |
| 244 | const hasImages = content.some( |
| 245 | (part) => part.type === 'image' && typeof part.image === 'string' && part.image, |
| 246 | ) |
| 247 | |
| 248 | if (!hasImages) { |
| 249 | const text = extractTextParts(content) |
| 250 | return text || null |
| 251 | } |
| 252 | |
| 253 | const parts: ResponsesApiContentPart[] = [] |
| 254 | for (const part of content) { |
| 255 | if (part.type === 'text' && typeof part.text === 'string' && part.text) { |
| 256 | parts.push({ type: 'input_text', text: part.text }) |
| 257 | } else if (part.type === 'json') { |
| 258 | const text = typeof part.value === 'string' ? part.value : JSON.stringify(part.value) |
| 259 | if (text) { |
| 260 | parts.push({ type: 'input_text', text }) |
| 261 | } |
| 262 | } else if (part.type === 'image') { |
| 263 | const imageUrl = toImageUrl(part.image, part.mediaType) |
| 264 | if (imageUrl) { |
| 265 | parts.push({ type: 'input_image', image_url: imageUrl }) |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | return parts.length > 0 ? parts : null |
| 271 | } |
| 272 | |
| 273 | function toImageUrl(image: unknown, mediaType?: string): string | null { |
| 274 | if (typeof image !== 'string' || !image) return null |
no test coverage detected