This commit is contained in:
锦恢 2025-06-17 23:29:23 +08:00
parent 50990688be
commit 94935e6b9c
2 changed files with 60 additions and 24 deletions

View File

@ -1,10 +1,47 @@
import type { InputSchema } from "@/hook/type";
import type { ToolItem } from "@/hook/type";
export function toolSchemaToPromptDescription(tool: InputSchema) {
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`;
});
}
export function getXmlWrapperPrompt(tools: any) {
// 有参数的工具
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: 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 <Start HERE> and <End HERE> 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

View File

@ -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
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;