From cf3a3a57ceb2e0d7de3c050edf45b585e58e3bf8 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Sat, 26 Apr 2025 15:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=A0=BC=E5=BC=8F=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/main-panel/chat/normalise.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 renderer/src/components/main-panel/chat/normalise.ts diff --git a/renderer/src/components/main-panel/chat/normalise.ts b/renderer/src/components/main-panel/chat/normalise.ts new file mode 100644 index 0000000..8998113 --- /dev/null +++ b/renderer/src/components/main-panel/chat/normalise.ts @@ -0,0 +1,54 @@ +import { ChatMessage, ToolMessage } from "./chat"; + +const OPENAI_SUPPORT_MEDIA = new Set(['text', 'image_url', 'video_url']); + +function hasInvalidType(toolMessage: ToolMessage) { + for (const content of toolMessage.content) { + if (OPENAI_SUPPORT_MEDIA.has(content.type)) { + continue; + } + return true; + } + return false; +} + +function normaliseToolMessage(rest: ToolMessage) { + if (!hasInvalidType(rest)) { + return rest; + } + + const newRest = JSON.parse(JSON.stringify(rest)); + + // 过滤一下 userMessages,现在的大部分模型只支持 text, image_url and video_url 这三种类型的数据 + for (const content of newRest.content) { + if (OPENAI_SUPPORT_MEDIA.has(content.type)) { + continue; + } + + + + } + + return rest; +} + +/** + * @description 标准化发送信息,将自定义的项目进行剔除 + * 对于图像信息进行特殊处理 + * @param userMessages + */ +export async function normaliseChatMessage(userMessages: ChatMessage[]) { + const normalisedMessages = []; + for (const msg of userMessages) { + if (msg.role === 'tool') { + const normMessage = normaliseToolMessage(msg); + const { extraInfo, name, ...rest } = normMessage; + normalisedMessages.push(rest); + } else { + const { extraInfo, name, ...rest } = msg; + normalisedMessages.push(rest); + } + } + + return normalisedMessages; +} \ No newline at end of file