feat: 添加工具执行时长字段,增强消息处理功能

This commit is contained in:
oudecheng 2026-06-02 16:52:47 +08:00
parent 5273a7b335
commit 4d6d989247
5 changed files with 24 additions and 3 deletions

View File

@ -308,6 +308,7 @@ fn filter_images_by_age_and_count(
tool_call_id: message.tool_call_id.clone(), tool_call_id: message.tool_call_id.clone(),
tool_name: message.tool_name.clone(), tool_name: message.tool_name.clone(),
tool_state: message.tool_state.clone(), tool_state: message.tool_state.clone(),
tool_duration_ms: message.tool_duration_ms,
tool_calls: message.tool_calls.clone(), tool_calls: message.tool_calls.clone(),
}); });
} }
@ -1025,7 +1026,8 @@ impl AgentLoop {
} else { } else {
ToolMessageState::Completed ToolMessageState::Completed
}, },
); )
.with_tool_duration(result.duration.as_millis() as u64);
messages.push(tool_message.clone()); messages.push(tool_message.clone());
emitted_messages.push(tool_message.clone()); emitted_messages.push(tool_message.clone());
let duration_ms = Some(result.duration.as_millis() as u64); let duration_ms = Some(result.duration.as_millis() as u64);
@ -1041,7 +1043,8 @@ impl AgentLoop {
} else { } else {
ToolMessageState::Completed ToolMessageState::Completed
}, },
); )
.with_tool_duration(result.duration.as_millis() as u64);
messages.push(tool_message.clone()); messages.push(tool_message.clone());
emitted_messages.push(tool_message.clone()); emitted_messages.push(tool_message.clone());
let duration_ms = Some(result.duration.as_millis() as u64); let duration_ms = Some(result.duration.as_millis() as u64);

View File

@ -62,6 +62,8 @@ pub struct ChatMessage {
pub tool_name: Option<String>, pub tool_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub tool_state: Option<ToolMessageState>, pub tool_state: Option<ToolMessageState>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tool_duration_ms: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>, pub tool_calls: Option<Vec<ToolCall>>,
} }
@ -78,6 +80,7 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: None, tool_call_id: None,
tool_name: None, tool_name: None,
tool_duration_ms: None,
tool_state: None, tool_state: None,
tool_calls: None, tool_calls: None,
} }
@ -94,6 +97,7 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: None, tool_call_id: None,
tool_name: None, tool_name: None,
tool_duration_ms: None,
tool_state: None, tool_state: None,
tool_calls: None, tool_calls: None,
} }
@ -110,6 +114,7 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: None, tool_call_id: None,
tool_name: None, tool_name: None,
tool_duration_ms: None,
tool_state: None, tool_state: None,
tool_calls: None, tool_calls: None,
} }
@ -138,6 +143,7 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: None, tool_call_id: None,
tool_name: None, tool_name: None,
tool_duration_ms: None,
tool_state: None, tool_state: None,
tool_calls: Some(tool_calls), tool_calls: Some(tool_calls),
} }
@ -171,6 +177,7 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: None, tool_call_id: None,
tool_name: None, tool_name: None,
tool_duration_ms: None,
tool_state: None, tool_state: None,
tool_calls: None, tool_calls: None,
} }
@ -205,11 +212,17 @@ impl ChatMessage {
reasoning_content: None, reasoning_content: None,
tool_call_id: Some(tool_call_id.into()), tool_call_id: Some(tool_call_id.into()),
tool_name: Some(tool_name.into()), tool_name: Some(tool_name.into()),
tool_duration_ms: None,
tool_state: Some(tool_state), tool_state: Some(tool_state),
tool_calls: None, tool_calls: None,
} }
} }
pub fn with_tool_duration(mut self, ms: u64) -> Self {
self.tool_duration_ms = Some(ms);
self
}
pub fn has_system_context(&self, expected: &str) -> bool { pub fn has_system_context(&self, expected: &str) -> bool {
self.system_context.as_deref() == Some(expected) self.system_context.as_deref() == Some(expected)
} }

View File

@ -662,7 +662,7 @@ fn chat_message_to_ws_outbound(msg: &crate::bus::ChatMessage) -> Option<WsOutbou
content: msg.content.clone(), content: msg.content.clone(),
role: msg.role.clone(), role: msg.role.clone(),
subagent_task_id: None, subagent_task_id: None,
duration_ms: None, duration_ms: msg.tool_duration_ms,
}), }),
ToolMessageState::PendingUserAction => Some(WsOutbound::ToolPending { ToolMessageState::PendingUserAction => Some(WsOutbound::ToolPending {
id: msg.id.clone(), id: msg.id.clone(),

View File

@ -1455,6 +1455,7 @@ impl SessionStore {
tool_call_id: row.get(7)?, tool_call_id: row.get(7)?,
tool_name: row.get(8)?, tool_name: row.get(8)?,
tool_state: None, tool_state: None,
tool_duration_ms: None,
tool_calls, tool_calls,
}) })
})?; })?;
@ -1780,6 +1781,7 @@ fn clone_message_for_compaction(message: &ChatMessage, timestamp: i64) -> ChatMe
tool_call_id: message.tool_call_id.clone(), tool_call_id: message.tool_call_id.clone(),
tool_name: message.tool_name.clone(), tool_name: message.tool_name.clone(),
tool_state: message.tool_state.clone(), tool_state: message.tool_state.clone(),
tool_duration_ms: message.tool_duration_ms,
tool_calls: message.tool_calls.clone(), tool_calls: message.tool_calls.clone(),
} }
} }
@ -1836,6 +1838,7 @@ fn load_messages_between(
tool_call_id: row.get(7)?, tool_call_id: row.get(7)?,
tool_name: row.get(8)?, tool_name: row.get(8)?,
tool_state: None, tool_state: None,
tool_duration_ms: None,
tool_calls, tool_calls,
}) })
}, },
@ -1896,6 +1899,7 @@ fn load_messages_after(
tool_call_id: row.get(7)?, tool_call_id: row.get(7)?,
tool_name: row.get(8)?, tool_name: row.get(8)?,
tool_state: None, tool_state: None,
tool_duration_ms: None,
tool_calls, tool_calls,
}) })
})?; })?;

View File

@ -346,6 +346,7 @@ export function useChat(): UseChatReturn {
toolName: msg.tool_name, toolName: msg.tool_name,
toolCallId: msg.tool_call_id, toolCallId: msg.tool_call_id,
subagentTaskId: msg.subagent_task_id, subagentTaskId: msg.subagent_task_id,
durationMs: msg.duration_ms,
}, },
]) ])
break break