(content: any)
| 1815 | ): { text: string; images: AIImage[] } => { |
| 1816 | const parseContent = (content: any): { text: string; images: AIImage[] } => { |
| 1817 | if (typeof content === "string") { |
| 1818 | return { text: content || "AI 回复为空", images: [] }; |
| 1819 | } |
| 1820 | |
| 1821 | const parts = Array.isArray(content) |
| 1822 | ? content |
| 1823 | : content && typeof content === "object" |
| 1824 | ? [content] |
| 1825 | : []; |
| 1826 | |
| 1827 | if (parts.length === 0) return { text: "AI 回复为空", images: [] }; |
| 1828 | |
| 1829 | const textSegments: string[] = []; |
| 1830 | const images: AIImage[] = []; |
| 1831 | for (const part of parts) { |
| 1832 | if ( |
| 1833 | (part.type === "text" || part.type === "output_text") && |
| 1834 | typeof part.text === "string" |
| 1835 | ) { |
| 1836 | textSegments.push(part.text); |
| 1837 | } |
| 1838 | if (part.type === "image_url" && part.image_url?.url) { |
| 1839 | const dataUrl = parseDataUrl(part.image_url.url); |
| 1840 | if (dataUrl) |
| 1841 | images.push({ data: dataUrl.data, mimeType: dataUrl.mimeType }); |
| 1842 | else images.push({ url: part.image_url.url, mimeType: "image/jpeg" }); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | return { |
| 1847 | text: textSegments.join("\n").trim() || "AI 回复为空", |
| 1848 | images, |
| 1849 | }; |
| 1850 | }; |
| 1851 | |
| 1852 | const message = data?.choices?.[0]?.message; |
| 1853 | if (!message) return { text: "AI 回复为空", images: [] }; |
| 1854 | return parseContent(message.content); |
no test coverage detected