1
- import { getMessageTextContent , trimTopic } from "../utils" ;
1
+ import {
2
+ getMessageTextContent ,
3
+ isDalle3 ,
4
+ safeLocalStorage ,
5
+ trimTopic ,
6
+ } from "../utils" ;
2
7
3
8
import { indexedDBStorage } from "@/app/utils/indexedDB-storage" ;
4
9
import { nanoid } from "nanoid" ;
@@ -14,14 +19,13 @@ import {
14
19
DEFAULT_INPUT_TEMPLATE ,
15
20
DEFAULT_MODELS ,
16
21
DEFAULT_SYSTEM_TEMPLATE ,
22
+ GEMINI_SUMMARIZE_MODEL ,
17
23
KnowledgeCutOffDate ,
24
+ ServiceProvider ,
18
25
StoreKey ,
19
26
SUMMARIZE_MODEL ,
20
- GEMINI_SUMMARIZE_MODEL ,
21
- ServiceProvider ,
22
27
} from "../constant" ;
23
28
import Locale , { getLang } from "../locales" ;
24
- import { isDalle3 , safeLocalStorage } from "../utils" ;
25
29
import { prettyObject } from "../utils/format" ;
26
30
import { createPersistStore } from "../utils/store" ;
27
31
import { estimateTokenLength } from "../utils/token" ;
@@ -55,6 +59,7 @@ export type ChatMessage = RequestMessage & {
55
59
model ?: ModelType ;
56
60
tools ?: ChatMessageTool [ ] ;
57
61
audio_url ?: string ;
62
+ isMcpResponse ?: boolean ;
58
63
} ;
59
64
60
65
export function createMessage ( override : Partial < ChatMessage > ) : ChatMessage {
@@ -368,20 +373,22 @@ export const useChatStore = createPersistStore(
368
373
get ( ) . summarizeSession ( false , targetSession ) ;
369
374
} ,
370
375
371
- async onUserInput ( content : string , attachImages ?: string [ ] ) {
376
+ async onUserInput (
377
+ content : string ,
378
+ attachImages ?: string [ ] ,
379
+ isMcpResponse ?: boolean ,
380
+ ) {
372
381
const session = get ( ) . currentSession ( ) ;
373
382
const modelConfig = session . mask . modelConfig ;
374
383
375
- const userContent = fillTemplateWith ( content , modelConfig ) ;
376
- console . log ( "[User Input] after template: " , userContent ) ;
377
-
378
- let mContent : string | MultimodalContent [ ] = userContent ;
384
+ // MCP Response no need to fill template
385
+ let mContent : string | MultimodalContent [ ] = isMcpResponse
386
+ ? content
387
+ : fillTemplateWith ( content , modelConfig ) ;
379
388
380
- if ( attachImages && attachImages . length > 0 ) {
389
+ if ( ! isMcpResponse && attachImages && attachImages . length > 0 ) {
381
390
mContent = [
382
- ...( userContent
383
- ? [ { type : "text" as const , text : userContent } ]
384
- : [ ] ) ,
391
+ ...( content ? [ { type : "text" as const , text : content } ] : [ ] ) ,
385
392
...attachImages . map ( ( url ) => ( {
386
393
type : "image_url" as const ,
387
394
image_url : { url } ,
@@ -392,6 +399,7 @@ export const useChatStore = createPersistStore(
392
399
let userMessage : ChatMessage = createMessage ( {
393
400
role : "user" ,
394
401
content : mContent ,
402
+ isMcpResponse,
395
403
} ) ;
396
404
397
405
const botMessage : ChatMessage = createMessage ( {
@@ -770,9 +778,10 @@ export const useChatStore = createPersistStore(
770
778
lastInput,
771
779
} ) ;
772
780
} ,
781
+
782
+ /** check if the message contains MCP JSON and execute the MCP action */
773
783
checkMcpJson ( message : ChatMessage ) {
774
- const content =
775
- typeof message . content === "string" ? message . content : "" ;
784
+ const content = getMessageTextContent ( message ) ;
776
785
if ( isMcpJson ( content ) ) {
777
786
try {
778
787
const mcpRequest = extractMcpJson ( content ) ;
@@ -782,11 +791,14 @@ export const useChatStore = createPersistStore(
782
791
executeMcpAction ( mcpRequest . clientId , mcpRequest . mcp )
783
792
. then ( ( result ) => {
784
793
console . log ( "[MCP Response]" , result ) ;
785
- // 直接使用onUserInput发送结果
786
- get ( ) . onUserInput (
794
+ const mcpResponse =
787
795
typeof result === "object"
788
796
? JSON . stringify ( result )
789
- : String ( result ) ,
797
+ : String ( result ) ;
798
+ get ( ) . onUserInput (
799
+ `\`\`\`json:mcp:${ mcpRequest . clientId } \n${ mcpResponse } \n\`\`\`` ,
800
+ [ ] ,
801
+ true ,
790
802
) ;
791
803
} )
792
804
. catch ( ( error ) => showToast ( String ( error ) ) ) ;
0 commit comments