mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-10 15:49:29 +08:00
refactor(aiCore): improve type handling and response structures
- Updated AiSdkToChunkAdapter to refine web search result handling. - Modified McpToolChunkMiddleware to ensure consistent type usage for tool responses. - Enhanced type definitions in chunk.ts and index.ts for better clarity and type safety. - Adjusted MessageWebSearch styles for improved UI consistency. - Refactored parseToolUse function to align with updated MCPTool response structures.
This commit is contained in:
parent
0c7e221b4e
commit
2ce9314a10
@ -185,9 +185,8 @@ export class AiSdkToChunkAdapter {
|
|||||||
source: WebSearchSource.GEMINI
|
source: WebSearchSource.GEMINI
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else if (final.webSearchResults.length) {
|
||||||
const providerName = Object.keys(providerMetadata || {})[0]
|
const providerName = Object.keys(providerMetadata || {})[0]
|
||||||
// console.log('providerName', providerName)
|
|
||||||
switch (providerName) {
|
switch (providerName) {
|
||||||
case WebSearchSource.OPENAI:
|
case WebSearchSource.OPENAI:
|
||||||
this.onChunk({
|
this.onChunk({
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import { loggerService } from '@logger'
|
import { loggerService } from '@logger'
|
||||||
import { MCPCallToolResponse, MCPTool, MCPToolResponse, Model, ToolCallResponse } from '@renderer/types'
|
import { MCPCallToolResponse, MCPTool, MCPToolResponse, Model } from '@renderer/types'
|
||||||
import { ChunkType, MCPToolCreatedChunk } from '@renderer/types/chunk'
|
import { ChunkType, MCPToolCreatedChunk } from '@renderer/types/chunk'
|
||||||
import { SdkMessageParam, SdkRawOutput, SdkToolCall } from '@renderer/types/sdk'
|
import { SdkMessageParam, SdkRawOutput, SdkToolCall } from '@renderer/types/sdk'
|
||||||
import {
|
import {
|
||||||
@ -230,7 +230,7 @@ async function executeToolCalls(
|
|||||||
model: Model,
|
model: Model,
|
||||||
topicId?: string
|
topicId?: string
|
||||||
): Promise<{ toolResults: SdkMessageParam[]; confirmedToolCalls: SdkToolCall[] }> {
|
): Promise<{ toolResults: SdkMessageParam[]; confirmedToolCalls: SdkToolCall[] }> {
|
||||||
const mcpToolResponses: ToolCallResponse[] = toolCalls
|
const mcpToolResponses: MCPToolResponse[] = toolCalls
|
||||||
.map((toolCall) => {
|
.map((toolCall) => {
|
||||||
const mcpTool = ctx.apiClientInstance.convertSdkToolCallToMcp(toolCall, mcpTools)
|
const mcpTool = ctx.apiClientInstance.convertSdkToolCallToMcp(toolCall, mcpTools)
|
||||||
if (!mcpTool) {
|
if (!mcpTool) {
|
||||||
@ -238,7 +238,7 @@ async function executeToolCalls(
|
|||||||
}
|
}
|
||||||
return ctx.apiClientInstance.convertSdkToolCallToMcpToolResponse(toolCall, mcpTool)
|
return ctx.apiClientInstance.convertSdkToolCallToMcpToolResponse(toolCall, mcpTool)
|
||||||
})
|
})
|
||||||
.filter((t): t is ToolCallResponse => typeof t !== 'undefined')
|
.filter((t): t is MCPToolResponse => typeof t !== 'undefined')
|
||||||
|
|
||||||
if (mcpToolResponses.length === 0) {
|
if (mcpToolResponses.length === 0) {
|
||||||
logger.warn(`No valid MCP tool responses to execute`)
|
logger.warn(`No valid MCP tool responses to execute`)
|
||||||
|
|||||||
@ -53,6 +53,7 @@ const PrepareToolWrapper = styled.span`
|
|||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
|
padding: 5px;
|
||||||
padding-left: 0;
|
padding-left: 0;
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -60,6 +61,7 @@ const MessageWebSearchToolTitleTextWrapper = styled(Text)`
|
|||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
|
padding: 5px;
|
||||||
`
|
`
|
||||||
|
|
||||||
// const MessageWebSearchToolBodyUlWrapper = styled.ul`
|
// const MessageWebSearchToolBodyUlWrapper = styled.ul`
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
ExternalToolResult,
|
ExternalToolResult,
|
||||||
KnowledgeReference,
|
KnowledgeReference,
|
||||||
|
MCPTool,
|
||||||
MCPToolResponse,
|
MCPToolResponse,
|
||||||
NormalToolResponse,
|
NormalToolResponse,
|
||||||
ToolUseResponse,
|
ToolUseResponse,
|
||||||
@ -294,7 +295,7 @@ export interface ExternalToolCompleteChunk {
|
|||||||
export interface MCPToolCreatedChunk {
|
export interface MCPToolCreatedChunk {
|
||||||
type: ChunkType.MCP_TOOL_CREATED
|
type: ChunkType.MCP_TOOL_CREATED
|
||||||
tool_calls?: SdkToolCall[] // 工具调用
|
tool_calls?: SdkToolCall[] // 工具调用
|
||||||
tool_use_responses?: ToolUseResponse[] // 工具使用响应
|
tool_use_responses?: (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] // 工具使用响应
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MCPToolPendingChunk {
|
export interface MCPToolPendingChunk {
|
||||||
|
|||||||
@ -883,6 +883,7 @@ export interface MCPToolResponse extends Omit<ToolUseResponse | ToolCallResponse
|
|||||||
|
|
||||||
export interface NormalToolResponse extends Omit<ToolCallResponse, 'tool'> {
|
export interface NormalToolResponse extends Omit<ToolCallResponse, 'tool'> {
|
||||||
tool: BaseTool
|
tool: BaseTool
|
||||||
|
toolCallId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MCPToolResultContent {
|
export interface MCPToolResultContent {
|
||||||
|
|||||||
@ -328,7 +328,11 @@ export function isToolAutoApproved(tool: BaseTool, server?: MCPServer): boolean
|
|||||||
return effectiveServer ? !effectiveServer.disabledAutoApproveTools?.includes(mcpTool.name) : false
|
return effectiveServer ? !effectiveServer.disabledAutoApproveTools?.includes(mcpTool.name) : false
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseToolUse(content: string, mcpTools: MCPTool[], startIdx: number = 0): ToolUseResponse[] {
|
export function parseToolUse(
|
||||||
|
content: string,
|
||||||
|
mcpTools: MCPTool[],
|
||||||
|
startIdx: number = 0
|
||||||
|
): (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] {
|
||||||
if (!content || !mcpTools || mcpTools.length === 0) {
|
if (!content || !mcpTools || mcpTools.length === 0) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
@ -346,7 +350,7 @@ export function parseToolUse(content: string, mcpTools: MCPTool[], startIdx: num
|
|||||||
|
|
||||||
const toolUsePattern =
|
const toolUsePattern =
|
||||||
/<tool_use>([\s\S]*?)<name>([\s\S]*?)<\/name>([\s\S]*?)<arguments>([\s\S]*?)<\/arguments>([\s\S]*?)<\/tool_use>/g
|
/<tool_use>([\s\S]*?)<name>([\s\S]*?)<\/name>([\s\S]*?)<arguments>([\s\S]*?)<\/arguments>([\s\S]*?)<\/tool_use>/g
|
||||||
const tools: ToolUseResponse[] = []
|
const tools: (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] = []
|
||||||
let match
|
let match
|
||||||
let idx = startIdx
|
let idx = startIdx
|
||||||
// Find all tool use blocks
|
// Find all tool use blocks
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user