feat: Enhance event duration tracking and UI components in chat
- Added duration tracking for agent steps and tool calls, improving event data accuracy. - Updated AgentStreamDisplay and ToolResultRenderer components to include fallback headers and improved output styling. - Refactored duration handling in the agent engine and session management to ensure consistent duration reporting. - Enhanced UI elements for better user experience, including scrollable output areas and improved tooltip visibility.
This commit is contained in:
@@ -164,19 +164,6 @@ func (e *AgentEngine) executeLoop(
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
|
||||
// Emit step event
|
||||
e.eventBus.Emit(ctx, event.Event{
|
||||
ID: generateEventID("step"),
|
||||
Type: event.EventAgentStep,
|
||||
SessionID: sessionID,
|
||||
Data: event.AgentStepData{
|
||||
Iteration: state.CurrentRound,
|
||||
Thought: response.Content,
|
||||
ToolCalls: step.ToolCalls,
|
||||
Duration: time.Since(roundStart).Milliseconds(),
|
||||
},
|
||||
})
|
||||
|
||||
// 2. Check finish reason - if stop and no tool calls, agent is done
|
||||
if response.FinishReason == "stop" && len(response.ToolCalls) == 0 {
|
||||
logger.Infof(ctx, "[Agent][Round-%d] Agent finished - no more tool calls needed", state.CurrentRound+1)
|
||||
@@ -195,6 +182,7 @@ func (e *AgentEngine) executeLoop(
|
||||
Done: true,
|
||||
},
|
||||
})
|
||||
step.Duration = time.Since(roundStart).Milliseconds()
|
||||
logger.Infof(ctx, "[Agent][Round-%d] Duration: %dms", state.CurrentRound+1, time.Since(roundStart).Milliseconds())
|
||||
break
|
||||
}
|
||||
@@ -345,6 +333,7 @@ func (e *AgentEngine) executeLoop(
|
||||
}
|
||||
}
|
||||
|
||||
step.Duration = time.Since(roundStart).Milliseconds()
|
||||
state.RoundSteps = append(state.RoundSteps, step)
|
||||
// 4. Observe: Add tool results to messages and write to context
|
||||
messages = e.appendToolResults(ctx, messages, step)
|
||||
|
||||
@@ -29,12 +29,12 @@ func NewMCPTool(service *types.MCPService, mcpTool *types.MCPTool, mcpManager *m
|
||||
}
|
||||
|
||||
// Name returns the unique name for this tool
|
||||
// Format: mcp_{service_name}_{tool_name}
|
||||
// Format: mcp.{service_name}.{tool_name}
|
||||
func (t *MCPTool) Name() string {
|
||||
// Sanitize service name and tool name to create a valid identifier
|
||||
serviceName := sanitizeName(t.service.Name)
|
||||
toolName := sanitizeName(t.mcpTool.Name)
|
||||
return fmt.Sprintf("mcp_%s_%s", serviceName, toolName)
|
||||
return fmt.Sprintf("mcp.%s.%s", serviceName, toolName)
|
||||
}
|
||||
|
||||
// Description returns the tool description
|
||||
|
||||
@@ -123,9 +123,15 @@ func (h *AgentStreamHandler) handleToolCall(ctx context.Context, evt event.Event
|
||||
return nil
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
// Track start time for this tool call (use tool_call_id as key)
|
||||
h.eventStartTimes[data.ToolCallID] = time.Now()
|
||||
h.mu.Unlock()
|
||||
|
||||
metadata := map[string]interface{}{
|
||||
"tool_name": data.ToolName,
|
||||
"arguments": data.Arguments,
|
||||
"tool_name": data.ToolName,
|
||||
"arguments": data.Arguments,
|
||||
"tool_call_id": data.ToolCallID,
|
||||
}
|
||||
|
||||
// Append event to stream
|
||||
@@ -150,6 +156,18 @@ func (h *AgentStreamHandler) handleToolResult(ctx context.Context, evt event.Eve
|
||||
return nil
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
// Calculate duration from start time if available, otherwise use provided duration
|
||||
var durationMs int64
|
||||
if startTime, exists := h.eventStartTimes[data.ToolCallID]; exists {
|
||||
durationMs = time.Since(startTime).Milliseconds()
|
||||
delete(h.eventStartTimes, data.ToolCallID)
|
||||
} else if data.Duration > 0 {
|
||||
// Fallback to provided duration if start time not tracked
|
||||
durationMs = data.Duration
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
// Send SSE response (both success and failure)
|
||||
responseType := types.ResponseTypeToolResult
|
||||
content := data.Output
|
||||
@@ -162,11 +180,12 @@ func (h *AgentStreamHandler) handleToolResult(ctx context.Context, evt event.Eve
|
||||
|
||||
// Build metadata including tool result data for rich frontend rendering
|
||||
metadata := map[string]interface{}{
|
||||
"tool_name": data.ToolName,
|
||||
"success": data.Success,
|
||||
"output": data.Output,
|
||||
"error": data.Error,
|
||||
"duration": data.Duration,
|
||||
"tool_name": data.ToolName,
|
||||
"success": data.Success,
|
||||
"output": data.Output,
|
||||
"error": data.Error,
|
||||
"duration_ms": durationMs,
|
||||
"tool_call_id": data.ToolCallID,
|
||||
}
|
||||
|
||||
// Merge tool result data (contains display_type, formatted results, etc.)
|
||||
@@ -264,8 +283,30 @@ func (h *AgentStreamHandler) handleFinalAnswer(ctx context.Context, evt event.Ev
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
// Track start time on first chunk
|
||||
if _, exists := h.eventStartTimes[evt.ID]; !exists {
|
||||
h.eventStartTimes[evt.ID] = time.Now()
|
||||
}
|
||||
|
||||
// Accumulate final answer locally for assistant message (database)
|
||||
h.finalAnswer += data.Content
|
||||
|
||||
// Calculate duration if done
|
||||
var metadata map[string]interface{}
|
||||
if data.Done {
|
||||
startTime := h.eventStartTimes[evt.ID]
|
||||
duration := time.Since(startTime)
|
||||
metadata = map[string]interface{}{
|
||||
"event_id": evt.ID,
|
||||
"duration_ms": duration.Milliseconds(),
|
||||
"completed_at": time.Now().Unix(),
|
||||
}
|
||||
delete(h.eventStartTimes, evt.ID)
|
||||
} else {
|
||||
metadata = map[string]interface{}{
|
||||
"event_id": evt.ID,
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
// Append this chunk to stream (frontend will accumulate by event ID)
|
||||
@@ -275,6 +316,7 @@ func (h *AgentStreamHandler) handleFinalAnswer(ctx context.Context, evt event.Ev
|
||||
Content: data.Content, // Just this chunk
|
||||
Done: data.Done,
|
||||
Timestamp: time.Now(),
|
||||
Data: metadata,
|
||||
}); err != nil {
|
||||
logger.GetLogger(h.ctx).Error("Append answer event to stream failed", "error", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user