mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-10 07:19:02 +08:00
refactor: Improve logging and error handling in MCPApiService and ClaudeCodeService
This commit is contained in:
parent
305a454ffd
commit
98ebfd12b3
@ -102,19 +102,14 @@ class MCPApiService extends EventEmitter {
|
|||||||
|
|
||||||
async getServerInfo(id: string): Promise<any> {
|
async getServerInfo(id: string): Promise<any> {
|
||||||
try {
|
try {
|
||||||
logger.silly(`getServerInfo called with id: ${id}`)
|
|
||||||
const server = await this.getServerById(id)
|
const server = await this.getServerById(id)
|
||||||
if (!server) {
|
if (!server) {
|
||||||
logger.warn(`Server with id ${id} not found`)
|
logger.warn(`Server with id ${id} not found`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
logger.silly(`Returning server info for id ${id}`)
|
|
||||||
|
|
||||||
const client = await mcpService.initClient(server)
|
const client = await mcpService.initClient(server)
|
||||||
const tools = await client.listTools()
|
const tools = await client.listTools()
|
||||||
|
|
||||||
logger.silly(`Server with id ${id} info:`, { tools: JSON.stringify(tools.tools) })
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: server.id,
|
id: server.id,
|
||||||
name: server.name,
|
name: server.name,
|
||||||
|
|||||||
@ -270,6 +270,7 @@ export class SessionMessageService extends BaseService {
|
|||||||
.orderBy(desc(sessionMessagesTable.created_at))
|
.orderBy(desc(sessionMessagesTable.created_at))
|
||||||
.limit(1)
|
.limit(1)
|
||||||
|
|
||||||
|
logger.silly('Last agent session ID result:', { agentSessionId: result[0]?.agent_session_id, sessionId })
|
||||||
return result[0]?.agent_session_id || ''
|
return result[0]?.agent_session_id || ''
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('Failed to get last agent session ID', {
|
logger.error('Failed to get last agent session ID', {
|
||||||
|
|||||||
@ -74,6 +74,8 @@ class ClaudeCodeService implements AgentServiceInterface {
|
|||||||
ELECTRON_RUN_AS_NODE: '1'
|
ELECTRON_RUN_AS_NODE: '1'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const errorChunks: string[] = []
|
||||||
|
|
||||||
// Build SDK options from parameters
|
// Build SDK options from parameters
|
||||||
const options: Options = {
|
const options: Options = {
|
||||||
abortController,
|
abortController,
|
||||||
@ -82,7 +84,8 @@ class ClaudeCodeService implements AgentServiceInterface {
|
|||||||
model: modelInfo.modelId,
|
model: modelInfo.modelId,
|
||||||
pathToClaudeCodeExecutable: this.claudeExecutablePath,
|
pathToClaudeCodeExecutable: this.claudeExecutablePath,
|
||||||
stderr: (chunk: string) => {
|
stderr: (chunk: string) => {
|
||||||
logger.info('claude stderr', { chunk })
|
logger.warn('claude stderr', { chunk })
|
||||||
|
errorChunks.push(chunk)
|
||||||
},
|
},
|
||||||
appendSystemPrompt: session.instructions,
|
appendSystemPrompt: session.instructions,
|
||||||
permissionMode: session.configuration?.permission_mode,
|
permissionMode: session.configuration?.permission_mode,
|
||||||
@ -116,11 +119,16 @@ class ClaudeCodeService implements AgentServiceInterface {
|
|||||||
|
|
||||||
logger.silly('Starting Claude Code SDK query', {
|
logger.silly('Starting Claude Code SDK query', {
|
||||||
prompt,
|
prompt,
|
||||||
options
|
cwd: options.cwd,
|
||||||
|
model: options.model,
|
||||||
|
permissionMode: options.permissionMode,
|
||||||
|
maxTurns: options.maxTurns,
|
||||||
|
allowedTools: options.allowedTools,
|
||||||
|
resume: options.resume
|
||||||
})
|
})
|
||||||
|
|
||||||
// Start async processing
|
// Start async processing
|
||||||
this.processSDKQuery(prompt, options, aiStream)
|
this.processSDKQuery(prompt, options, aiStream, errorChunks)
|
||||||
|
|
||||||
return aiStream
|
return aiStream
|
||||||
}
|
}
|
||||||
@ -142,7 +150,12 @@ class ClaudeCodeService implements AgentServiceInterface {
|
|||||||
/**
|
/**
|
||||||
* Process SDK query and emit stream events
|
* Process SDK query and emit stream events
|
||||||
*/
|
*/
|
||||||
private async processSDKQuery(prompt: string, options: Options, stream: ClaudeCodeStream): Promise<void> {
|
private async processSDKQuery(
|
||||||
|
prompt: string,
|
||||||
|
options: Options,
|
||||||
|
stream: ClaudeCodeStream,
|
||||||
|
errorChunks: string[]
|
||||||
|
): Promise<void> {
|
||||||
const jsonOutput: SDKMessage[] = []
|
const jsonOutput: SDKMessage[] = []
|
||||||
let hasCompleted = false
|
let hasCompleted = false
|
||||||
const startTime = Date.now()
|
const startTime = Date.now()
|
||||||
@ -209,17 +222,12 @@ class ClaudeCodeService implements AgentServiceInterface {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Original error handling for non-abort errors
|
errorChunks.push(errorObj instanceof Error ? errorObj.message : String(errorObj))
|
||||||
logger.error('SDK query error:', {
|
const errorMessage = errorChunks.join('\n\n')
|
||||||
error: errorObj instanceof Error ? errorObj.message : String(errorObj),
|
|
||||||
duration,
|
|
||||||
messageCount: jsonOutput.length
|
|
||||||
})
|
|
||||||
|
|
||||||
// Emit error event
|
// Emit error event
|
||||||
stream.emit('data', {
|
stream.emit('data', {
|
||||||
type: 'error',
|
type: 'error',
|
||||||
error: errorObj instanceof Error ? errorObj : new Error(String(errorObj))
|
error: new Error(errorMessage)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user