mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-28 05:11:24 +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
|
||||
}
|
||||
})
|
||||
} else {
|
||||
} else if (final.webSearchResults.length) {
|
||||
const providerName = Object.keys(providerMetadata || {})[0]
|
||||
// console.log('providerName', providerName)
|
||||
switch (providerName) {
|
||||
case WebSearchSource.OPENAI:
|
||||
this.onChunk({
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
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 { SdkMessageParam, SdkRawOutput, SdkToolCall } from '@renderer/types/sdk'
|
||||
import {
|
||||
@ -230,7 +230,7 @@ async function executeToolCalls(
|
||||
model: Model,
|
||||
topicId?: string
|
||||
): Promise<{ toolResults: SdkMessageParam[]; confirmedToolCalls: SdkToolCall[] }> {
|
||||
const mcpToolResponses: ToolCallResponse[] = toolCalls
|
||||
const mcpToolResponses: MCPToolResponse[] = toolCalls
|
||||
.map((toolCall) => {
|
||||
const mcpTool = ctx.apiClientInstance.convertSdkToolCallToMcp(toolCall, mcpTools)
|
||||
if (!mcpTool) {
|
||||
@ -238,7 +238,7 @@ async function executeToolCalls(
|
||||
}
|
||||
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) {
|
||||
logger.warn(`No valid MCP tool responses to execute`)
|
||||
|
||||
@ -53,6 +53,7 @@ const PrepareToolWrapper = styled.span`
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
font-size: 14px;
|
||||
padding: 5px;
|
||||
padding-left: 0;
|
||||
`
|
||||
|
||||
@ -60,6 +61,7 @@ const MessageWebSearchToolTitleTextWrapper = styled(Text)`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
padding: 5px;
|
||||
`
|
||||
|
||||
// const MessageWebSearchToolBodyUlWrapper = styled.ul`
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import {
|
||||
ExternalToolResult,
|
||||
KnowledgeReference,
|
||||
MCPTool,
|
||||
MCPToolResponse,
|
||||
NormalToolResponse,
|
||||
ToolUseResponse,
|
||||
@ -294,7 +295,7 @@ export interface ExternalToolCompleteChunk {
|
||||
export interface MCPToolCreatedChunk {
|
||||
type: ChunkType.MCP_TOOL_CREATED
|
||||
tool_calls?: SdkToolCall[] // 工具调用
|
||||
tool_use_responses?: ToolUseResponse[] // 工具使用响应
|
||||
tool_use_responses?: (Omit<ToolUseResponse, 'tool'> & { tool: MCPTool })[] // 工具使用响应
|
||||
}
|
||||
|
||||
export interface MCPToolPendingChunk {
|
||||
|
||||
@ -883,6 +883,7 @@ export interface MCPToolResponse extends Omit<ToolUseResponse | ToolCallResponse
|
||||
|
||||
export interface NormalToolResponse extends Omit<ToolCallResponse, 'tool'> {
|
||||
tool: BaseTool
|
||||
toolCallId: string
|
||||
}
|
||||
|
||||
export interface MCPToolResultContent {
|
||||
|
||||
@ -328,7 +328,11 @@ export function isToolAutoApproved(tool: BaseTool, server?: MCPServer): boolean
|
||||
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) {
|
||||
return []
|
||||
}
|
||||
@ -346,7 +350,7 @@ export function parseToolUse(content: string, mcpTools: MCPTool[], startIdx: num
|
||||
|
||||
const toolUsePattern =
|
||||
/<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 idx = startIdx
|
||||
// Find all tool use blocks
|
||||
|
||||
Loading…
Reference in New Issue
Block a user