using System.Diagnostics;
using Microsoft.Extensions.Logging;
namespace Service.AgentFramework;
///
/// Agent 基类 - 提供通用的工作流编排能力
///
public abstract class BaseAgent
{
protected readonly IToolRegistry _toolRegistry;
protected readonly ILogger _logger;
protected readonly List _steps = new();
protected readonly Dictionary _metadata = new();
// 定义 ActivitySource 供 DevUI 捕获
private static readonly ActivitySource _activitySource = new("Microsoft.Agents.Workflows");
protected BaseAgent(
IToolRegistry toolRegistry,
ILogger logger)
{
_toolRegistry = toolRegistry;
_logger = logger;
}
///
/// 记录执行步骤
///
protected void RecordStep(
string name,
string description,
object? output = null,
long durationMs = 0)
{
var step = new ExecutionStep
{
Name = name,
Description = description,
Status = "Completed",
Output = output,
DurationMs = durationMs
};
_steps.Add(step);
// 使用 Activity 进行埋点,将被 DevUI 自动捕获
using var activity = _activitySource.StartActivity(name);
activity?.SetTag("agent.step.description", description);
if (output != null) activity?.SetTag("agent.step.output", output.ToString());
}
///
/// 记录失败的步骤
///
protected void RecordFailedStep(
string name,
string description,
string error,
long durationMs = 0)
{
var step = new ExecutionStep
{
Name = name,
Description = description,
Status = "Failed",
Error = error,
DurationMs = durationMs
};
_steps.Add(step);
using var activity = _activitySource.StartActivity($"{name} (Failed)");
activity?.SetTag("agent.step.error", error);
_logger.LogError("[Agent步骤失败] {StepName}: {Error}", name, error);
}
///
/// 设置元数据
///
protected void SetMetadata(string key, object? value)
{
_metadata[key] = value;
}
///
/// 获取执行日志
///
protected List GetExecutionLog()
{
return _steps.ToList();
}
///
/// 生成多轮总结
///
protected virtual async Task GenerateSummaryAsync(
string[] phases,
Dictionary phaseResults)
{
var summaryParts = new List();
// 简单的总结生成逻辑
// 实际项目中可以集成 AI 生成更复杂的总结
foreach (var phase in phases)
{
if (phaseResults.TryGetValue(phase, out var result))
{
summaryParts.Add($"{phase}:已完成");
}
}
return await Task.FromResult(string.Join(";", summaryParts) + "。");
}
///
/// 调用 Tool(简化接口)
///
protected async Task CallToolAsync(
string toolName,
string stepName,
string stepDescription)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
try
{
_logger.LogInformation("开始执行 Tool: {ToolName}", toolName);
var result = await _toolRegistry.InvokeToolAsync(toolName);
sw.Stop();
RecordStep(stepName, stepDescription, result, sw.ElapsedMilliseconds);
return result;
}
catch (Exception ex)
{
sw.Stop();
RecordFailedStep(stepName, stepDescription, ex.Message, sw.ElapsedMilliseconds);
throw;
}
}
///
/// 调用带参数的 Tool
///
protected async Task CallToolAsync(
string toolName,
TParam param,
string stepName,
string stepDescription)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
try
{
_logger.LogInformation("开始执行 Tool: {ToolName},参数: {Param}", toolName, param);
var result = await _toolRegistry.InvokeToolAsync(toolName, param);
sw.Stop();
RecordStep(stepName, stepDescription, result, sw.ElapsedMilliseconds);
return result;
}
catch (Exception ex)
{
sw.Stop();
RecordFailedStep(stepName, stepDescription, ex.Message, sw.ElapsedMilliseconds);
throw;
}
}
///
/// 调用带多参数的 Tool
///
protected async Task CallToolAsync(
string toolName,
TParam1 param1,
TParam2 param2,
string stepName,
string stepDescription)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
try
{
_logger.LogInformation("开始执行 Tool: {ToolName},参数: {Param1}, {Param2}", toolName, param1, param2);
var result = await _toolRegistry.InvokeToolAsync(
toolName, param1, param2);
sw.Stop();
RecordStep(stepName, stepDescription, result, sw.ElapsedMilliseconds);
return result;
}
catch (Exception ex)
{
sw.Stop();
RecordFailedStep(stepName, stepDescription, ex.Message, sw.ElapsedMilliseconds);
throw;
}
}
///
/// 获取 Agent 执行结果
///
protected AgentResult CreateResult(
T data,
string summary,
bool success = true,
string? error = null)
{
return new AgentResult
{
Data = data,
Summary = summary,
Steps = _steps,
Metadata = _metadata,
Success = success,
Error = error
};
}
}