* Convert Anthropic MessageParam[] to a list of {role, content} objects * suitable for OpenAI-compatible chat.completions APIs.
( messages: MessageParam[], )
| 129 | * suitable for OpenAI-compatible chat.completions APIs. |
| 130 | */ |
| 131 | function messageParamsToOpenAIRoleContent( |
| 132 | messages: MessageParam[], |
| 133 | ): Array<{ role: 'user' | 'assistant'; content: string }> { |
| 134 | const result: Array<{ role: 'user' | 'assistant'; content: string }> = [] |
| 135 | for (const m of messages) { |
| 136 | if (m.role !== 'user' && m.role !== 'assistant') continue |
| 137 | const text = |
| 138 | typeof m.content === 'string' |
| 139 | ? m.content |
| 140 | : Array.isArray(m.content) |
| 141 | ? m.content |
| 142 | .filter( |
| 143 | (b): b is { type: 'text'; text: string } => b.type === 'text', |
| 144 | ) |
| 145 | .map(b => b.text) |
| 146 | .join('\n') |
| 147 | : '' |
| 148 | if (text) { |
| 149 | result.push({ role: m.role as 'user' | 'assistant', content: text }) |
| 150 | } |
| 151 | } |
| 152 | return result |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Lightweight API wrapper for "side queries" outside the main conversation loop. |
no test coverage detected