(item: unknown)
| 157 | } |
| 158 | |
| 159 | function extractTextFromMessageItem(item: unknown): string { |
| 160 | if (!isRecord(item)) { |
| 161 | return '' |
| 162 | } |
| 163 | |
| 164 | if (typeof item.content === 'string') { |
| 165 | return item.content |
| 166 | } |
| 167 | |
| 168 | if (!Array.isArray(item.content)) { |
| 169 | return '' |
| 170 | } |
| 171 | |
| 172 | const textParts: string[] = [] |
| 173 | for (const part of item.content) { |
| 174 | if (!isRecord(part)) { |
| 175 | continue |
| 176 | } |
| 177 | |
| 178 | if ((part.type === 'output_text' || part.type === 'text') && typeof part.text === 'string') { |
| 179 | textParts.push(part.text) |
| 180 | continue |
| 181 | } |
| 182 | |
| 183 | if (part.type === 'output_json') { |
| 184 | if (typeof part.text === 'string') { |
| 185 | textParts.push(part.text) |
| 186 | } else if (part.json !== undefined) { |
| 187 | textParts.push(JSON.stringify(part.json)) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return textParts.join('') |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Extracts plain text from Responses API output items. |
no test coverage detected