From 94935e6b9c278d66ddd27dc21752ef9c6acd3cd3 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Tue, 17 Jun 2025 23:29:23 +0800 Subject: [PATCH] update --- .../components/main-panel/chat/core/prompt.ts | 61 +++++++++++++------ .../main-panel/chat/core/task-loop.ts | 23 +++++-- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/renderer/src/components/main-panel/chat/core/prompt.ts b/renderer/src/components/main-panel/chat/core/prompt.ts index 8303bbf..333938e 100644 --- a/renderer/src/components/main-panel/chat/core/prompt.ts +++ b/renderer/src/components/main-panel/chat/core/prompt.ts @@ -1,10 +1,47 @@ -import type { InputSchema } from "@/hook/type"; - -export function toolSchemaToPromptDescription(tool: InputSchema) { +import type { ToolItem } from "@/hook/type"; +export function toolSchemaToPromptDescription(tools: ToolItem[]) { + let prompt = ''; + + // 无参数的工具 + const noParamTools = tools.filter(tool => + !tool.inputSchema.required || tool.inputSchema.required.length === 0 + ); + + if (noParamTools.length > 0) { + noParamTools.forEach(tool => { + prompt += `- ${tool.name}\n`; + prompt += `**Description**: ${tool.description}\n\n`; + }); + } + + // 有参数的工具 + const paramTools = tools.filter(tool => + tool.inputSchema.required && tool.inputSchema.required.length > 0 + ); + + if (paramTools.length > 0) { + paramTools.forEach(tool => { + prompt += `- ${tool.name}\n`; + prompt += `**Description**: ${tool.description}\n`; + prompt += `**Parameters**:\n`; + + Object.entries(tool.inputSchema.properties).forEach(([name, prop]) => { + const required = tool.inputSchema.required?.includes(name) || false; + prompt += `- \`${name}\`: ${prop.description || '无描述'} (${prop.type}) ${required ? '(required)' : ''}\n`; + }); + + prompt += '\n'; + }); + } + + return prompt; } -export function getXmlWrapperPrompt(tools: any) { +export function getXmlWrapperPrompt(tools: ToolItem[]) { + + const toolPrompt = toolSchemaToPromptDescription(tools); + return ` [Start Fresh Session from here] @@ -94,21 +131,7 @@ Answer the user's request using the relevant tool(s), if they are available. Che Do not use and in your output, that is just output format reference to where to start and end your output. ## AVAILABLE TOOLS FOR SUPERASSISTANT -- neo4j-mcp.executeReadOnlyCypherQuery -**Description**: [neo4j-mcp] 执行只读的 Cypher 查询 -**Parameters**: -- \`cypher\`: Cypher 查询语句,必须是只读的 (string) (required) - -- neo4j-mcp.getAllNodeTypes -**Description**: [neo4j-mcp] 获取所有的节点类型 - -- neo4j-mcp.getAllRelationTypes -**Description**: [neo4j-mcp] 获取所有的关系类型 - -- neo4j-mcp.getNodeField -**Description**: [neo4j-mcp] 获取节点的字段 -**Parameters**: -- \`nodeLabel\`: 节点的标签 (string) (required) +${toolPrompt} - list_servers **Description**: List all connected MCP servers and their capabilities diff --git a/renderer/src/components/main-panel/chat/core/task-loop.ts b/renderer/src/components/main-panel/chat/core/task-loop.ts index 0849163..e9f058c 100644 --- a/renderer/src/components/main-panel/chat/core/task-loop.ts +++ b/renderer/src/components/main-panel/chat/core/task-loop.ts @@ -15,7 +15,12 @@ import type { ToolItem } from "@/hook/type"; import chalk from 'chalk'; export type ChatCompletionChunk = OpenAI.Chat.Completions.ChatCompletionChunk; -export type ChatCompletionCreateParamsBase = OpenAI.Chat.Completions.ChatCompletionCreateParams & { id?: string, proxyServer?: string }; +export interface TaskLoopChatOption { + id?: string + proxyServer?: string + enableXmlWrapper?: boolean +} +export type ChatCompletionCreateParamsBase = OpenAI.Chat.Completions.ChatCompletionCreateParams & TaskLoopChatOption; export interface TaskLoopOptions { maxEpochs?: number; maxJsonParseRetry?: number; @@ -148,9 +153,15 @@ export class TaskLoop { const { chunk } = data.msg as { chunk: ChatCompletionChunk }; // 处理增量的 content 和 tool_calls - this.handleChunkDeltaContent(chunk); - this.handleChunkDeltaToolCalls(chunk, toolcallIndexAdapter); - this.handleChunkUsage(chunk); + if (chatData.enableXmlWrapper) { + this.handleChunkDeltaContent(chunk); + // no tool call in enableXmlWrapper + this.handleChunkUsage(chunk); + } else { + this.handleChunkDeltaContent(chunk); + this.handleChunkDeltaToolCalls(chunk, toolcallIndexAdapter); + this.handleChunkUsage(chunk); + } this.consumeChunks(chunk); }, { once: false }); @@ -210,6 +221,7 @@ export class TaskLoop { const tools = getToolSchema(tabStorage.settings.enableTools); const parallelToolCalls = tabStorage.settings.parallelToolCalls; const proxyServer = mcpSetting.proxyServer || ''; + const enableXmlWrapper = tabStorage.settings.enableXmlWrapper; const userMessages = []; @@ -240,7 +252,8 @@ export class TaskLoop { tools, parallelToolCalls, messages: userMessages, - proxyServer + proxyServer, + enableXmlWrapper } as ChatCompletionCreateParamsBase; return chatData;