From 0d5e66a9aeca9dd454df46fbdd1f12d69ba9b5a2 Mon Sep 17 00:00:00 2001 From: suruiqiang Date: Thu, 23 Jan 2025 18:24:38 +0800 Subject: [PATCH] not insert mcpSystemPrompt if not ENABLE_MCP --- app/store/chat.ts | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index 5c95ac02c..e152c7522 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -35,7 +35,7 @@ import { ModelConfig, ModelType, useAppConfig } from "./config"; import { useAccessStore } from "./access"; import { collectModelsWithDefaultModel } from "../utils/model"; import { createEmptyMask, Mask } from "./mask"; -import { executeMcpAction, getAllTools } from "../mcp/actions"; +import { executeMcpAction, getAllTools, isMcpEnabled } from "../mcp/actions"; import { extractMcpJson, isMcpJson } from "../mcp/utils"; const localStorage = safeLocalStorage(); @@ -245,7 +245,7 @@ export const useChatStore = createPersistStore( newSession.topic = currentSession.topic; // 深拷贝消息 - newSession.messages = currentSession.messages.map(msg => ({ + newSession.messages = currentSession.messages.map((msg) => ({ ...msg, id: nanoid(), // 生成新的消息 ID })); @@ -551,27 +551,32 @@ export const useChatStore = createPersistStore( (session.mask.modelConfig.model.startsWith("gpt-") || session.mask.modelConfig.model.startsWith("chatgpt-")); - const mcpSystemPrompt = await getMcpSystemPrompt(); + const mcpEnabled = await isMcpEnabled(); + const mcpSystemPrompt = mcpEnabled ? await getMcpSystemPrompt() : ""; var systemPrompts: ChatMessage[] = []; - systemPrompts = shouldInjectSystemPrompts - ? [ - createMessage({ - role: "system", - content: - fillTemplateWith("", { - ...modelConfig, - template: DEFAULT_SYSTEM_TEMPLATE, - }) + mcpSystemPrompt, - }), - ] - : [ - createMessage({ - role: "system", - content: mcpSystemPrompt, - }), - ]; + if (shouldInjectSystemPrompts) { + systemPrompts = [ + createMessage({ + role: "system", + content: + fillTemplateWith("", { + ...modelConfig, + template: DEFAULT_SYSTEM_TEMPLATE, + }) + mcpSystemPrompt, + }), + ]; + } else if (mcpEnabled) { + systemPrompts = [ + createMessage({ + role: "system", + content: mcpSystemPrompt, + }), + ]; + } + + if (shouldInjectSystemPrompts || mcpEnabled) { console.log( "[Global System Prompt] ", systemPrompts.at(0)?.content ?? "empty", @@ -816,6 +821,8 @@ export const useChatStore = createPersistStore( /** check if the message contains MCP JSON and execute the MCP action */ checkMcpJson(message: ChatMessage) { + const mcpEnabled = isMcpEnabled(); + if (!mcpEnabled) return; const content = getMessageTextContent(message); if (isMcpJson(content)) { try {