feat(ai): Day 4 — 策略 Prompt 优化 + Tool 调用日志
- System Prompt 增加 10 个 Tool 的使用时机指引,Agent 自动选择最合适的 Tool - 优先使用 get_health_insights 作为首次对话开场工具 - AgentRunResult 新增 tool_calls: Vec<ToolCallLog>,记录每次调用名称/耗时/成功状态 - ToolCallLog 将在 Phase 2 写入 ai_tool_call_logs 表
This commit is contained in:
@@ -6,6 +6,14 @@ use crate::dto::{ChatMessage, ChatMessageRole};
|
||||
use crate::error::AiResult;
|
||||
use crate::provider::AiProvider;
|
||||
|
||||
/// 单次 Tool 调用日志
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ToolCallLog {
|
||||
pub tool_name: String,
|
||||
pub duration_ms: u64,
|
||||
pub success: bool,
|
||||
}
|
||||
|
||||
/// Agent 运行时参数
|
||||
pub struct AgentRunParams {
|
||||
pub model: String,
|
||||
@@ -40,6 +48,7 @@ pub struct AgentRunResult {
|
||||
pub total_input_tokens: u32,
|
||||
pub total_output_tokens: u32,
|
||||
pub iterations: usize,
|
||||
pub tool_calls: Vec<ToolCallLog>,
|
||||
}
|
||||
|
||||
impl AgentOrchestrator {
|
||||
@@ -66,6 +75,7 @@ impl AgentOrchestrator {
|
||||
let mut iterations = 0;
|
||||
let mut total_input_tokens = 0u32;
|
||||
let mut total_output_tokens = 0u32;
|
||||
let mut tool_call_logs: Vec<ToolCallLog> = Vec::new();
|
||||
|
||||
loop {
|
||||
iterations += 1;
|
||||
@@ -96,6 +106,7 @@ impl AgentOrchestrator {
|
||||
total_input_tokens,
|
||||
total_output_tokens,
|
||||
iterations,
|
||||
tool_calls: tool_call_logs,
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -143,23 +154,30 @@ impl AgentOrchestrator {
|
||||
|
||||
// 执行每个 Tool Call(受沙箱 allowed_tools 约束)
|
||||
for tc in &tool_calls {
|
||||
let tool_result = match self.tool_registry.get(&tc.name) {
|
||||
let start = std::time::Instant::now();
|
||||
let (tool_result, success) = match self.tool_registry.get(&tc.name) {
|
||||
Some(tool) => {
|
||||
// 沙箱过滤:如果 allowed_tools 存在且不包含此 Tool,拒绝执行
|
||||
if let Some(allowed) = allowed_tools {
|
||||
if !allowed.contains(tc.name.as_str()) {
|
||||
format!("Tool '{}' 在当前角色下不可用", tc.name)
|
||||
(format!("Tool '{}' 在当前角色下不可用", tc.name), false)
|
||||
} else {
|
||||
let result = tool.execute(ctx, tc.arguments.clone()).await;
|
||||
result.output
|
||||
(result.output, true)
|
||||
}
|
||||
} else {
|
||||
let result = tool.execute(ctx, tc.arguments.clone()).await;
|
||||
result.output
|
||||
(result.output, true)
|
||||
}
|
||||
}
|
||||
None => format!("未知 Tool: {}", tc.name),
|
||||
None => (format!("未知 Tool: {}", tc.name), false),
|
||||
};
|
||||
let duration = start.elapsed();
|
||||
|
||||
tool_call_logs.push(ToolCallLog {
|
||||
tool_name: tc.name.clone(),
|
||||
duration_ms: duration.as_millis() as u64,
|
||||
success,
|
||||
});
|
||||
|
||||
messages.push(ChatMessage {
|
||||
role: ChatMessageRole::Tool,
|
||||
|
||||
Reference in New Issue
Block a user