forked from CherryHQ/cherry-studio-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcpTool.ts
More file actions
868 lines (774 loc) · 23.3 KB
/
mcpTool.ts
File metadata and controls
868 lines (774 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
import type { ContentBlockParam, MessageParam, ToolUnion, ToolUseBlock } from '@anthropic-ai/sdk/resources'
import type { Content, FunctionCall, Part, Tool } from '@google/genai'
import { Type as GeminiSchemaType } from '@google/genai'
import type OpenAI from 'openai'
import type {
ChatCompletionContentPart,
ChatCompletionMessageParam,
ChatCompletionMessageToolCall,
ChatCompletionTool
} from 'openai/resources'
import { isFunctionCallingModel, isVisionModel } from '@/config/models'
import { loggerService } from '@/services/LoggerService'
import type { Assistant, Model } from '@/types/assistant'
import type { MCPToolCompleteChunk, MCPToolInProgressChunk, MCPToolPendingChunk } from '@/types/chunk'
import { ChunkType } from '@/types/chunk'
import type { MCPCallToolResponse, MCPServer, MCPToolResponse, ToolUseResponse } from '@/types/mcp'
import type { AwsBedrockSdkMessageParam, AwsBedrockSdkTool, AwsBedrockSdkToolCall } from '@/types/sdk'
import type { MCPTool } from '@/types/tool'
import { isToolUseModeFunction } from './assistants'
import { filterProperties, processSchemaForO3 } from './mcpSchema'
const logger = loggerService.withContext('Utils:MCPTools')
export function mcpToolsToOpenAIResponseTools(mcpTools: MCPTool[]): OpenAI.Responses.Tool[] {
return mcpTools.map(tool => {
const parameters = processSchemaForO3(tool.inputSchema)
return {
type: 'function',
name: tool.id,
parameters: {
type: 'object' as const,
...parameters
},
strict: true
} satisfies OpenAI.Responses.Tool
})
}
export function mcpToolsToOpenAIChatTools(mcpTools: MCPTool[]): ChatCompletionTool[] {
return mcpTools.map(tool => {
const parameters = processSchemaForO3(tool.inputSchema)
return {
type: 'function',
function: {
name: tool.id,
description: tool.description,
parameters: {
type: 'object' as const,
...parameters
},
strict: true
}
} as ChatCompletionTool
})
}
export function openAIToolsToMcpTool(
mcpTools: MCPTool[],
toolCall: OpenAI.Responses.ResponseFunctionToolCall | ChatCompletionMessageToolCall
): MCPTool | undefined {
let toolName = ''
try {
if ('name' in toolCall) {
toolName = toolCall.name
} else if (toolCall.type === 'function' && 'function' in toolCall) {
toolName = toolCall.function.name
} else {
throw new Error('Unknown tool call type')
}
} catch (error) {
logger.error(`Error parsing tool call: ${toolCall}`, error as Error)
// window.message.error(t('chat.mcp.error.parse_tool_call', { toolCall: toolCall }))
return undefined
}
const tools = mcpTools.filter(mcpTool => {
return mcpTool.id === toolName || mcpTool.name === toolName
})
if (tools.length > 1) {
logger.warn(`Multiple MCP Tools found for tool call: ${toolName}`)
// window.message.warning(t('chat.mcp.warning.multiple_tools', { tool: tools[0].name }))
}
if (tools.length === 0) {
logger.warn(`No MCP Tool found for tool call: ${toolName}`)
// window.message.warning(t('chat.mcp.warning.no_tool', { tool: toolName }))
return undefined
}
return tools[0]
}
export async function callBuiltInTool(toolResponse: MCPToolResponse): Promise<MCPCallToolResponse | undefined> {
logger.info(`[BuiltIn] Calling Built-in Tool: ${toolResponse.tool.name}`, toolResponse.tool)
if (toolResponse.tool.name === 'think') {
const thought = toolResponse.arguments?.thought
return {
isError: false,
content: [
{
type: 'text',
text: (thought as string) || ''
}
]
}
}
return undefined
}
export async function callMCPTool(
_toolResponse: MCPToolResponse,
_topicId?: string,
_modelName?: string
): Promise<MCPCallToolResponse> {
throw new Error('Not implemented')
// logger.info(`Calling Tool: ${toolResponse.tool.serverName} ${toolResponse.tool.name}`, toolResponse.tool)
// try {
// const server = getMcpServerByTool(toolResponse.tool)
// if (!server) {
// throw new Error(`Server not found: ${toolResponse.tool.serverName}`)
// }
// const resp = await window.api.mcp.callTool(
// {
// server,
// name: toolResponse.tool.name,
// args: toolResponse.arguments,
// callId: toolResponse.id
// },
// topicId ? currentSpan(topicId, modelName)?.spanContext() : undefined
// )
// if (toolResponse.tool.serverName === BuiltinMCPServerNames.mcpAutoInstall) {
// if (resp.data) {
// const mcpServer: MCPServer = {
// id: `f${nanoid()}`,
// name: resp.data.name,
// description: resp.data.description,
// baseUrl: resp.data.baseUrl,
// command: resp.data.command,
// args: resp.data.args,
// env: resp.data.env,
// registryUrl: '',
// isActive: false,
// provider: 'CherryAI'
// }
// store.dispatch(addMCPServer(mcpServer))
// }
// }
// logger.info(`Tool called: ${toolResponse.tool.serverName} ${toolResponse.tool.name}`, resp)
// return resp
// } catch (e) {
// logger.error(`Error calling Tool: ${toolResponse.tool.serverName} ${toolResponse.tool.name}`, e as Error)
// return Promise.resolve({
// isError: true,
// content: [
// {
// type: 'text',
// text: `Error calling tool ${toolResponse.tool.name}: ${e instanceof Error ? e.stack || e.message || 'No error details available' : JSON.stringify(e)}`
// }
// ]
// })
// }
}
export function mcpToolsToAnthropicTools(mcpTools: MCPTool[]): ToolUnion[] {
return mcpTools.map(tool => {
const t: ToolUnion = {
name: tool.id,
description: tool.description,
// @ts-ignore ignore type as it it unknown
input_schema: tool.inputSchema
}
return t
})
}
export function anthropicToolUseToMcpTool(mcpTools: MCPTool[] | undefined, toolUse: ToolUseBlock): MCPTool | undefined {
if (!mcpTools) return undefined
const tools = mcpTools.filter(tool => tool.id === toolUse.name)
if (tools.length === 0) {
logger.warn(`No MCP Tool found for tool call: ${toolUse.name}`)
// window.message.warning(t('chat.mcp.warning.no_tool', { tool: toolUse.name }))
return undefined
}
if (tools.length > 1) {
logger.warn(`Multiple MCP Tools found for tool call: ${toolUse.name}`)
// window.message.warning(t('chat.mcp.warning.multiple_tools', { tool: tools[0].name }))
}
return tools[0]
}
/**
* @param mcpTools
* @returns
*/
export function mcpToolsToGeminiTools(mcpTools: MCPTool[]): Tool[] {
return [
{
functionDeclarations: mcpTools?.map(tool => {
const filteredSchema = filterProperties(tool.inputSchema)
return {
name: tool.id,
description: tool.description,
parameters: {
type: GeminiSchemaType.OBJECT,
properties: filteredSchema.properties,
required: tool.inputSchema.required
}
}
})
}
]
}
export function geminiFunctionCallToMcpTool(
mcpTools: MCPTool[] | undefined,
toolCall: FunctionCall | undefined
): MCPTool | undefined {
if (!toolCall) return undefined
if (!mcpTools) return undefined
const toolName = toolCall.name || toolCall.id
if (!toolName) return undefined
const tools = mcpTools.filter(tool => tool.id.includes(toolName) || tool.name.includes(toolName))
if (tools.length > 1) {
logger.warn(`Multiple MCP Tools found for tool call: ${toolName}`)
// window.message.warning(t('chat.mcp.warning.multiple_tools', { tool: tools[0].name }))
}
if (tools.length === 0) {
logger.warn(`No MCP Tool found for tool call: ${toolName}`)
// window.message.warning(t('chat.mcp.warning.no_tool', { tool: toolName }))
return undefined
}
return tools[0]
}
export function upsertMCPToolResponse(
results: MCPToolResponse[],
resp: MCPToolResponse,
onChunk: (chunk: MCPToolPendingChunk | MCPToolInProgressChunk | MCPToolCompleteChunk) => void
) {
const index = results.findIndex(ret => ret.id === resp.id)
let result = resp
if (index !== -1) {
const cur = {
...results[index],
response: resp.response,
arguments: resp.arguments,
status: resp.status
}
results[index] = cur
result = cur
} else {
results.push(resp)
}
switch (resp.status) {
case 'pending':
onChunk({
type: ChunkType.MCP_TOOL_PENDING,
responses: [result]
})
break
case 'invoking':
onChunk({
type: ChunkType.MCP_TOOL_IN_PROGRESS,
responses: [result]
})
break
case 'cancelled':
case 'done':
onChunk({
type: ChunkType.MCP_TOOL_COMPLETE,
responses: [result]
})
break
default:
break
}
}
export function filterMCPTools(
mcpTools: MCPTool[] | undefined,
enabledServers: MCPServer[] | undefined
): MCPTool[] | undefined {
if (mcpTools) {
if (enabledServers) {
mcpTools = mcpTools.filter(t => enabledServers.some(m => m.name === t.serverName))
} else {
mcpTools = []
}
}
return mcpTools
}
export function getMcpServerByTool(_tool: MCPTool) {
throw new Error('Function not implemented.')
// const servers = store.getState().mcp.servers
// return servers.find(s => s.id === tool.serverId)
}
export function isToolAutoApproved(tool: MCPTool, server?: MCPServer): boolean {
if (tool.isBuiltIn) {
return true
}
const effectiveServer = server ?? getMcpServerByTool(tool)
return effectiveServer ? !effectiveServer.disabledAutoApproveTools?.includes(tool.name) : false
}
export function parseToolUse(
content: string,
mcpTools: MCPTool[],
startIdx: number = 0
): (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] {
if (!content || !mcpTools || mcpTools.length === 0) {
return []
}
// 支持两种格式:
// 1. 完整的 <tool_use></tool_use> 标签包围的内容
// 2. 只有内部内容(从 TagExtractor 提取出来的)
let contentToProcess = content
// 如果内容不包含 <tool_use> 标签,说明是从 TagExtractor 提取的内部内容,需要包装
if (!content.includes('<tool_use>')) {
contentToProcess = `<tool_use>\n${content}\n</tool_use>`
}
const toolUsePattern =
/<tool_use>([\s\S]*?)<name>([\s\S]*?)<\/name>([\s\S]*?)<arguments>([\s\S]*?)<\/arguments>([\s\S]*?)<\/tool_use>/g
const tools: (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] = []
let match
let idx = startIdx
// Find all tool use blocks
while ((match = toolUsePattern.exec(contentToProcess)) !== null) {
// const fullMatch = match[0]
const toolName = match[2].trim()
const toolArgs = match[4].trim()
// Try to parse the arguments as JSON
let parsedArgs
try {
parsedArgs = JSON.parse(toolArgs)
} catch {
// If parsing fails, use the string as is
parsedArgs = toolArgs
}
// Logger.log(`Parsed arguments for tool "${toolName}":`, parsedArgs)
const mcpTool = mcpTools.find(tool => tool.id === toolName || tool.name === toolName)
if (!mcpTool) {
logger.error(`Tool "${toolName}" not found in MCP tools`)
// window.message.error(i18n.t('settings.mcp.errors.toolNotFound', { name: toolName }))
continue
}
// Add to tools array
tools.push({
id: `${toolName}-${idx++}`, // Unique ID for each tool use
toolUseId: mcpTool.id,
tool: mcpTool,
arguments: parsedArgs,
status: 'pending'
})
// Remove the tool use block from the content
// content = content.replace(fullMatch, '')
}
return tools
}
export function mcpToolCallResponseToOpenAICompatibleMessage(
mcpToolResponse: MCPToolResponse,
resp: MCPCallToolResponse,
isVisionModel: boolean = false,
noSupportArrayContent: boolean = false
): ChatCompletionMessageParam {
const message = {
role: 'user'
} as ChatCompletionMessageParam
if (resp.isError) {
message.content = JSON.stringify(resp.content)
} else if (noSupportArrayContent) {
let content: string = `Here is the result of mcp tool use \`${mcpToolResponse.tool.name}\`:\n`
if (isVisionModel) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
content += (item.text || 'no content') + '\n'
break
case 'image':
// NOTE: 假设兼容模式下支持解析base64图片,虽然我觉得应该不支持
content += `Here is a image result: data:${item.mimeType};base64,${item.data}\n`
break
case 'audio':
// NOTE: 假设兼容模式下支持解析base64音频,虽然我觉得应该不支持
content += `Here is a audio result: data:${item.mimeType};base64,${item.data}\n`
break
default:
content += `Here is a unsupported result type: ${item.type}\n`
break
}
}
} else {
content += JSON.stringify(resp.content)
content += '\n'
}
message.content = content
} else {
const content: ChatCompletionContentPart[] = [
{
type: 'text',
text: `Here is the result of mcp tool use \`${mcpToolResponse.tool.name}\`:`
}
]
if (isVisionModel) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
content.push({
type: 'text',
text: item.text || 'no content'
})
break
case 'image':
content.push({
type: 'image_url',
image_url: {
url: `data:${item.mimeType};base64,${item.data}`,
detail: 'auto'
}
})
break
case 'audio':
content.push({
type: 'input_audio',
input_audio: {
data: `data:${item.mimeType};base64,${item.data}`,
format: 'mp3'
}
})
break
default:
content.push({
type: 'text',
text: `Unsupported type: ${item.type}`
})
break
}
}
} else {
content.push({
type: 'text',
text: JSON.stringify(resp.content)
})
}
message.content = content
}
return message
}
export function mcpToolCallResponseToOpenAIMessage(
mcpToolResponse: MCPToolResponse,
resp: MCPCallToolResponse,
isVisionModel: boolean = false
): OpenAI.Responses.EasyInputMessage {
const message = {
role: 'user'
} as OpenAI.Responses.EasyInputMessage
if (resp.isError) {
message.content = JSON.stringify(resp.content)
} else {
const content: OpenAI.Responses.ResponseInputContent[] = [
{
type: 'input_text',
text: `Here is the result of mcp tool use \`${mcpToolResponse.tool.name}\`:`
}
]
if (isVisionModel) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
content.push({
type: 'input_text',
text: item.text || 'no content'
})
break
case 'image':
content.push({
type: 'input_image',
image_url: `data:${item.mimeType};base64,${item.data}`,
detail: 'auto'
})
break
default:
content.push({
type: 'input_text',
text: `Unsupported type: ${item.type}`
})
break
}
}
} else {
content.push({
type: 'input_text',
text: JSON.stringify(resp.content)
})
}
message.content = content
}
return message
}
export function mcpToolCallResponseToAnthropicMessage(
mcpToolResponse: MCPToolResponse,
resp: MCPCallToolResponse,
model: Model
): MessageParam {
const message = {
role: 'user'
} as MessageParam
if (resp.isError) {
message.content = JSON.stringify(resp.content)
} else {
const content: ContentBlockParam[] = [
{
type: 'text',
text: `Here is the result of mcp tool use \`${mcpToolResponse.tool.name}\`:`
}
]
if (isVisionModel(model)) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
content.push({
type: 'text',
text: item.text || 'no content'
})
break
case 'image':
if (
item.mimeType === 'image/png' ||
item.mimeType === 'image/jpeg' ||
item.mimeType === 'image/webp' ||
item.mimeType === 'image/gif'
) {
content.push({
type: 'image',
source: {
type: 'base64',
data: `data:${item.mimeType};base64,${item.data}`,
media_type: item.mimeType
}
})
} else {
content.push({
type: 'text',
text: `Unsupported image type: ${item.mimeType}`
})
}
break
default:
content.push({
type: 'text',
text: `Unsupported type: ${item.type}`
})
break
}
}
} else {
content.push({
type: 'text',
text: JSON.stringify(resp.content)
})
}
message.content = content
}
return message
}
export function mcpToolCallResponseToGeminiMessage(
mcpToolResponse: MCPToolResponse,
resp: MCPCallToolResponse,
isVisionModel: boolean = false
): Content {
const message = {
role: 'user'
} as Content
if (resp.isError) {
message.parts = [
{
text: JSON.stringify(resp.content)
}
]
} else {
const parts: Part[] = [
{
text: `Here is the result of mcp tool use \`${mcpToolResponse.tool.name}\`:`
}
]
if (isVisionModel) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
parts.push({
text: item.text || 'no content'
})
break
case 'image':
if (!item.data) {
parts.push({
text: 'No image data provided'
})
} else {
parts.push({
inlineData: {
data: item.data,
mimeType: item.mimeType || 'image/png'
}
})
}
break
default:
parts.push({
text: `Unsupported type: ${item.type}`
})
break
}
}
} else {
parts.push({
text: JSON.stringify(resp.content)
})
}
message.parts = parts
}
return message
}
export function mcpToolsToAwsBedrockTools(mcpTools: MCPTool[]): AwsBedrockSdkTool[] {
return mcpTools.map(tool => ({
toolSpec: {
name: tool.id,
description: tool.description,
inputSchema: {
json: {
type: 'object',
properties: tool.inputSchema?.properties
? Object.fromEntries(
Object.entries(tool.inputSchema.properties).map(([key, value]) => [
key,
{
type:
typeof value === 'object' && value !== null && 'type' in value ? (value as any).type : 'string',
description:
typeof value === 'object' && value !== null && 'description' in value
? (value as any).description
: undefined
}
])
)
: {},
required: tool.inputSchema?.required || []
}
}
}
}))
}
export function awsBedrockToolUseToMcpTool(
mcpTools: MCPTool[] | undefined,
toolCall: AwsBedrockSdkToolCall
): MCPTool | undefined {
if (!toolCall) return undefined
if (!mcpTools) return undefined
const tool = mcpTools.find(tool => tool.id === toolCall.name || tool.name === toolCall.name)
if (!tool) {
return undefined
}
return tool
}
export function mcpToolCallResponseToAwsBedrockMessage(
mcpToolResponse: MCPToolResponse,
resp: MCPCallToolResponse,
model: Model
): AwsBedrockSdkMessageParam {
const message: AwsBedrockSdkMessageParam = {
role: 'user',
content: []
}
const toolUseId =
'toolUseId' in mcpToolResponse && mcpToolResponse.toolUseId
? mcpToolResponse.toolUseId
: 'toolCallId' in mcpToolResponse && mcpToolResponse.toolCallId
? mcpToolResponse.toolCallId
: 'unknown-tool-id'
if (resp.isError) {
message.content = [
{
toolResult: {
toolUseId: toolUseId,
content: [
{
text: `Error: ${JSON.stringify(resp.content)}`
}
],
status: 'error'
}
}
]
} else {
const toolResultContent: {
json?: any
text?: string
image?: {
format: 'png' | 'jpeg' | 'gif' | 'webp'
source: {
bytes?: Uint8Array
s3Location?: {
uri: string
bucketOwner?: string
}
}
}
}[] = []
if (isVisionModel(model)) {
for (const item of resp.content) {
switch (item.type) {
case 'text':
toolResultContent.push({
text: item.text || 'no content'
})
break
case 'image':
if (item.data && item.mimeType) {
// const awsImage = convertBase64ImageToAwsBedrockFormat(item.data, item.mimeType)
const awsImage = null
if (awsImage) {
toolResultContent.push({ image: awsImage })
} else {
toolResultContent.push({
text: `[Image received: ${item.mimeType}, size: ${item.data?.length || 0} bytes]`
})
}
} else {
toolResultContent.push({
text: '[Image received but no data available]'
})
}
break
default:
toolResultContent.push({
text: `Unsupported content type: ${item.type}`
})
break
}
}
} else {
// 对于非视觉模型,将所有内容合并为文本
const textContent = resp.content
.map(item => {
if (item.type === 'text') {
return item.text
} else {
// 对于非文本内容,尝试转换为JSON格式
try {
return JSON.stringify(item)
} catch {
return `[${item.type} content]`
}
}
})
.join('\n')
toolResultContent.push({
text: textContent || 'Tool execution completed with no output'
})
}
message.content = [
{
toolResult: {
toolUseId: toolUseId,
content: toolResultContent,
status: 'success'
}
}
]
}
return message
}
/**
* 是否启用工具使用(function call)
* @param assistant
* @returns 是否启用工具使用
*/
export function isSupportedToolUse(assistant: Assistant) {
if (assistant.model) {
return isFunctionCallingModel(assistant.model) && isToolUseModeFunction(assistant)
}
return false
}
/**
* 是否使用提示词工具使用
* @param assistant
* @returns 是否使用提示词工具使用
*/
export function isPromptToolUse(assistant: Assistant) {
return assistant.settings?.toolUseMode === 'prompt'
}