♻️ refactor(agents): improve error handling and logging in agent services

This commit is contained in:
Vaayne 2025-09-30 17:17:56 +08:00
parent ac1cab60a3
commit 51630f95fd
4 changed files with 32 additions and 11 deletions

View File

@ -59,16 +59,23 @@ export abstract class BaseService {
} }
if (ids && ids.length > 0) { if (ids && ids.length > 0) {
for (const id of ids) { for (const id of ids) {
const server = await mcpApiService.getServerInfo(id) try {
if (server) { const server = await mcpApiService.getServerInfo(id)
server.tools.forEach((tool: MCPTool) => { if (server) {
tools.push({ server.tools.forEach((tool: MCPTool) => {
id: `mcp_${id}_${tool.name}`, tools.push({
name: tool.name, id: `mcp_${id}_${tool.name}`,
type: 'mcp', name: tool.name,
description: tool.description || '', type: 'mcp',
requirePermissions: true description: tool.description || '',
requirePermissions: true
})
}) })
}
} catch (error) {
logger.warn('Failed to list MCP tools', {
id,
error: error as Error
}) })
} }
} }

View File

@ -6,7 +6,7 @@ import type {
ListOptions ListOptions
} from '@types' } from '@types'
import { TextStreamPart } from 'ai' import { TextStreamPart } from 'ai'
import { and, desc, eq } from 'drizzle-orm' import { and, desc, eq, not } from 'drizzle-orm'
import { BaseService } from '../BaseService' import { BaseService } from '../BaseService'
import { sessionMessagesTable } from '../database/schema' import { sessionMessagesTable } from '../database/schema'
@ -276,7 +276,7 @@ export class SessionMessageService extends BaseService {
const result = await this.database const result = await this.database
.select({ agent_session_id: sessionMessagesTable.agent_session_id }) .select({ agent_session_id: sessionMessagesTable.agent_session_id })
.from(sessionMessagesTable) .from(sessionMessagesTable)
.where(eq(sessionMessagesTable.session_id, sessionId)) .where(and(eq(sessionMessagesTable.session_id, sessionId), not(eq(sessionMessagesTable.agent_session_id, ''))))
.orderBy(desc(sessionMessagesTable.created_at)) .orderBy(desc(sessionMessagesTable.created_at))
.limit(1) .limit(1)

View File

@ -211,6 +211,11 @@ class ClaudeCodeService implements AgentServiceInterface {
message, message,
event: JSON.stringify(message.event) event: JSON.stringify(message.event)
}) })
} else {
logger.silly('Claude response', {
message,
event: JSON.stringify(message)
})
} }
// Transform SDKMessage to UIMessageChunks // Transform SDKMessage to UIMessageChunks

View File

@ -591,6 +591,15 @@ function handleSystemMessage(message: Extract<SDKMessage, { type: 'system' }>):
raw: message raw: message
} }
}) })
} else if (message.subtype === 'compact_boundary') {
chunks.push({
type: 'raw',
rawValue: {
type: 'compact',
session_id: message.session_id,
raw: message
}
})
} }
return chunks return chunks
} }