From 9449bdb190881b6d084eb9ce7a9d966a82ea00de Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Wed, 18 Jun 2025 21:47:02 +0800 Subject: [PATCH] support xml --- .../main-panel/chat/core/handle-tool-calls.ts | 16 +- .../main-panel/chat/core/xml-wrapper.ts | 37 +++++ .../src/components/main-panel/chat/index.vue | 156 +++++++++++++----- .../main-panel/chat/message/toolcall.vue | 2 +- renderer/src/i18n/ar.json | 3 +- renderer/src/i18n/de.json | 3 +- renderer/src/i18n/en.json | 3 +- renderer/src/i18n/fr.json | 3 +- renderer/src/i18n/ja.json | 3 +- renderer/src/i18n/ko.json | 3 +- renderer/src/i18n/ru.json | 3 +- renderer/src/i18n/zh-cn.json | 3 +- renderer/src/i18n/zh-tw.json | 3 +- 13 files changed, 186 insertions(+), 52 deletions(-) diff --git a/renderer/src/components/main-panel/chat/core/handle-tool-calls.ts b/renderer/src/components/main-panel/chat/core/handle-tool-calls.ts index 7ae62ad..d50047c 100644 --- a/renderer/src/components/main-panel/chat/core/handle-tool-calls.ts +++ b/renderer/src/components/main-panel/chat/core/handle-tool-calls.ts @@ -112,15 +112,16 @@ function parseErrorObject(error: any): string { * @param callId2Index ID到索引的映射表 * @returns 映射后的索引值 */ -export function idAsIndexAdapter(toolCall: ToolCall, callId2Index: Map): IToolCallIndex { +export function idAsIndexAdapter(toolCall: ToolCall | string, callId2Index: Map): IToolCallIndex { // grok 采用 id 作为 index,需要将 id 映射到 zero-based 的 index - if (!toolCall.id) { + const id = typeof toolCall === 'string' ? toolCall : toolCall.id; + if (!id) { return 0; } - if (!callId2Index.has(toolCall.id)) { - callId2Index.set(toolCall.id, callId2Index.size); + if (!callId2Index.has(id)) { + callId2Index.set(id, callId2Index.size); } - return callId2Index.get(toolCall.id)!; + return callId2Index.get(id)!; } @@ -162,4 +163,9 @@ export function getToolCallIndexAdapter(llm: BasicLlmDescription, chatData: Chat } return defaultIndexAdapter; +} + +export function getIdAsIndexAdapter() { + const callId2Index = new Map(); + return (toolCall: ToolCall) => idAsIndexAdapter(toolCall, callId2Index); } \ No newline at end of file diff --git a/renderer/src/components/main-panel/chat/core/xml-wrapper.ts b/renderer/src/components/main-panel/chat/core/xml-wrapper.ts index fee8717..935fb9e 100644 --- a/renderer/src/components/main-panel/chat/core/xml-wrapper.ts +++ b/renderer/src/components/main-panel/chat/core/xml-wrapper.ts @@ -3,6 +3,7 @@ import { MessageState, type ToolCall } from '../chat-box/chat'; import { mcpClientAdapter } from '@/views/connect/core'; import { handleToolResponse, type IToolCallIndex, type ToolCallResult } from './handle-tool-calls'; import type { ChatStorage, EnableToolItem } from "../chat-box/chat"; +import type { ToolCallContent } from '@/hook/type'; export interface XmlToolCall { server: string; @@ -225,6 +226,42 @@ export async function getToolCallFromXmlString(xmlString: string): Promise((resolve, reject) => { + parseString(xmlString, (err, result) => { + if (err) reject(err); + else resolve(result); + }); + }); + + if (!result?.function_results?.result) { + return null; + } + + const resultData = result.function_results.result[0]; + const callId = resultData.$.call_id; + + // 提取所有评论文本 + const toolcallContent = [] as ToolCallContent[]; + const content = resultData._; + + toolcallContent.push({ + type: 'text', + text: content + }); + + return { + callId, + toolcallContent + }; + } catch (error) { + console.error('Failed to parse function results:', error); + return null; + } +} + export function toNormaliseToolcall(xmlToolcall: XmlToolCall, toolcallIndexAdapter: (toolCall: ToolCall) => IToolCallIndex): ToolCall { const toolcall = { id: xmlToolcall.callId, diff --git a/renderer/src/components/main-panel/chat/index.vue b/renderer/src/components/main-panel/chat/index.vue index fe6ab81..85e66cf 100644 --- a/renderer/src/components/main-panel/chat/index.vue +++ b/renderer/src/components/main-panel/chat/index.vue @@ -1,10 +1,10 @@