Skip to content

Commit daf3d87

Browse files
committed
fix(agent): enhance error handling in Agent class and improve event data structure
1 parent 66e797b commit daf3d87

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

packages/core/src/agent/index.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,11 @@ export class Agent<TProvider extends { llm: LLMProvider<unknown> }> {
559559
}
560560

561561
// Create the event data, including the serialized userContext
562-
const eventData: Partial<StandardEventData> & { userContext?: Record<string, unknown> } = {
562+
const eventData: Partial<StandardEventData> & {
563+
userContext?: Record<string, unknown>;
564+
} = {
563565
affectedNodeId,
564-
status,
566+
status: status as any,
565567
timestamp: new Date().toISOString(),
566568
sourceAgentId: this.id,
567569
...data,
@@ -637,9 +639,11 @@ export class Agent<TProvider extends { llm: LLMProvider<unknown> }> {
637639
}
638640
}
639641

640-
const eventData: Partial<StandardEventData> & { userContext?: Record<string, unknown> } = {
642+
const eventData: Partial<StandardEventData> & {
643+
userContext?: Record<string, unknown>;
644+
} = {
641645
affectedNodeId: toolNodeId,
642-
status: status as EventStatus,
646+
status: status as any,
643647
timestamp: new Date().toISOString(),
644648
input,
645649
output,
@@ -1115,6 +1119,36 @@ export class Agent<TProvider extends { llm: LLMProvider<unknown> }> {
11151119
}
11161120
},
11171121
onError: async (error: unknown) => {
1122+
if (error instanceof Error && "toolCallId" in error) {
1123+
const eventUpdater = operationContext.eventUpdaters.get(error.toolCallId as string);
1124+
1125+
if (eventUpdater) {
1126+
// Create a unique node ID for the tool
1127+
const toolNodeId = `tool_${(error as any).toolName}_${this.id}`;
1128+
1129+
// Update the tracked event with completion status
1130+
eventUpdater({
1131+
data: {
1132+
affectedNodeId: toolNodeId,
1133+
error: error.message,
1134+
errorMessage: error.message,
1135+
status: "error",
1136+
updatedAt: new Date().toISOString(),
1137+
output: error.message,
1138+
},
1139+
});
1140+
1141+
// Remove the updater from the map
1142+
operationContext.eventUpdaters.delete(error.toolCallId as string);
1143+
1144+
// Call onToolEnd hook if a tool with this name exists
1145+
const tool = this.toolManager.getToolByName((error as any).toolName);
1146+
if (tool) {
1147+
await this.hooks.onToolEnd?.(this, tool, error, operationContext);
1148+
}
1149+
}
1150+
}
1151+
11181152
// Changed 'any' to 'unknown'
11191153
// Clear the updaters map
11201154
operationContext.eventUpdaters.clear();

0 commit comments

Comments
 (0)