mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-04 11:49:02 +08:00
fix(OpenAIProvider): prevent atob error with non-base64 image URLs (#6673)
Signed-off-by: Chan Lee <Leetimemp@gmail.com>
This commit is contained in:
parent
413c372e83
commit
b4bd5fbd0c
@ -49,7 +49,9 @@ import {
|
|||||||
} from '@renderer/utils/mcp-tools'
|
} from '@renderer/utils/mcp-tools'
|
||||||
import { findFileBlocks, findImageBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find'
|
import { findFileBlocks, findImageBlocks, getMainTextContent } from '@renderer/utils/messageUtils/find'
|
||||||
import { buildSystemPrompt } from '@renderer/utils/prompt'
|
import { buildSystemPrompt } from '@renderer/utils/prompt'
|
||||||
|
import { Base64 } from 'js-base64'
|
||||||
import { isEmpty, takeRight } from 'lodash'
|
import { isEmpty, takeRight } from 'lodash'
|
||||||
|
import mime from 'mime'
|
||||||
import OpenAI from 'openai'
|
import OpenAI from 'openai'
|
||||||
import { ChatCompletionContentPart, ChatCompletionMessageParam } from 'openai/resources/chat/completions'
|
import { ChatCompletionContentPart, ChatCompletionMessageParam } from 'openai/resources/chat/completions'
|
||||||
import { Stream } from 'openai/streaming'
|
import { Stream } from 'openai/streaming'
|
||||||
@ -1059,16 +1061,13 @@ export abstract class BaseOpenAIProvider extends BaseProvider {
|
|||||||
const assistantFiles = findImageBlocks(lastAssistantMessage)
|
const assistantFiles = findImageBlocks(lastAssistantMessage)
|
||||||
const assistantImages = await Promise.all(
|
const assistantImages = await Promise.all(
|
||||||
assistantFiles.filter(Boolean).map(async (f) => {
|
assistantFiles.filter(Boolean).map(async (f) => {
|
||||||
const base64Data = f?.url?.replace(/^data:image\/\w+;base64,/, '')
|
const match = f?.url?.match(/^data:(image\/\w+);base64,(.+)$/)
|
||||||
if (!base64Data) return null
|
if (!match) return null
|
||||||
const binary = atob(base64Data)
|
const mimeType = match[1]
|
||||||
const bytes = new Uint8Array(binary.length)
|
const extension = mime.getExtension(mimeType) || 'bin'
|
||||||
for (let i = 0; i < binary.length; i++) {
|
const bytes = Base64.toUint8Array(match[2])
|
||||||
bytes[i] = binary.charCodeAt(i)
|
const fileName = `assistant_image.${extension}`
|
||||||
}
|
return await toFile(bytes, fileName, { type: mimeType })
|
||||||
return await toFile(bytes, 'assistant_image.png', {
|
|
||||||
type: 'image/png'
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
images = images.concat(assistantImages.filter(Boolean) as FileLike[])
|
images = images.concat(assistantImages.filter(Boolean) as FileLike[])
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user