|
| 1 | +using AiDotNet.Agentic.Models; |
| 2 | + |
| 3 | +// Disambiguate from the legacy AiDotNet.PromptEngineering.Templates.ChatMessage (global using). |
| 4 | +using ChatMessage = AiDotNet.Agentic.Models.ChatMessage; |
| 5 | + |
| 6 | +namespace AiDotNet.Agentic.SelfImproving; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A captured record of one agent run — the structured "trajectory" the self-improving layer learns from. |
| 10 | +/// It pairs what the agent did (the full message transcript, the final answer, step count, token usage) with |
| 11 | +/// an optional quality <see cref="Reward"/> assigned later by an evaluator. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// <para> |
| 15 | +/// Trajectories are the raw material for every self-improvement mechanism: continuous evaluation scores them, |
| 16 | +/// learned routers/tool policies train on them, prompt optimizers measure prompts against them, and |
| 17 | +/// reward-filtered trajectories become fine-tuning (LoRA) data. Capturing a run is non-invasive — a |
| 18 | +/// <see cref="TracingAgent{T}"/> wraps any agent and records each run without changing its behavior. |
| 19 | +/// </para> |
| 20 | +/// <para><b>For Beginners:</b> Think of this as the flight recorder for an agent: it saves exactly what was |
| 21 | +/// said and done on a run, plus (once graded) how good the outcome was. Collect many of these and the system |
| 22 | +/// can learn what works and get better over time. |
| 23 | +/// </para> |
| 24 | +/// </remarks> |
| 25 | +public sealed class AgentTrajectory |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// Initializes a new trajectory. |
| 29 | + /// </summary> |
| 30 | + /// <param name="id">The unique id for this trajectory.</param> |
| 31 | + /// <param name="agentName">The name of the agent that produced the run.</param> |
| 32 | + /// <param name="messages">The full conversation transcript the run produced.</param> |
| 33 | + /// <param name="finalText">The agent's final answer.</param> |
| 34 | + /// <param name="iterations">The number of model calls the run took.</param> |
| 35 | + /// <param name="usage">Aggregate token usage, when available.</param> |
| 36 | + /// <param name="reward">An optional quality score (assigned by an evaluator). <c>null</c> until graded.</param> |
| 37 | + /// <param name="metadata">Optional key/value metadata.</param> |
| 38 | + /// <exception cref="ArgumentNullException">Thrown when <paramref name="id"/>, <paramref name="agentName"/>, <paramref name="messages"/>, or <paramref name="finalText"/> is <c>null</c>.</exception> |
| 39 | + public AgentTrajectory( |
| 40 | + string id, |
| 41 | + string agentName, |
| 42 | + IReadOnlyList<ChatMessage> messages, |
| 43 | + string finalText, |
| 44 | + int iterations, |
| 45 | + ChatUsage? usage = null, |
| 46 | + double? reward = null, |
| 47 | + IReadOnlyDictionary<string, string>? metadata = null) |
| 48 | + { |
| 49 | + Guard.NotNullOrWhiteSpace(id); |
| 50 | + Guard.NotNull(agentName); |
| 51 | + Guard.NotNull(messages); |
| 52 | + Guard.NotNull(finalText); |
| 53 | + Id = id; |
| 54 | + AgentName = agentName; |
| 55 | + Messages = messages; |
| 56 | + FinalText = finalText; |
| 57 | + Iterations = iterations; |
| 58 | + Usage = usage; |
| 59 | + Reward = reward; |
| 60 | + Metadata = metadata; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary>Gets the unique id for this trajectory.</summary> |
| 64 | + public string Id { get; } |
| 65 | + |
| 66 | + /// <summary>Gets the name of the agent that produced the run.</summary> |
| 67 | + public string AgentName { get; } |
| 68 | + |
| 69 | + /// <summary>Gets the full conversation transcript the run produced.</summary> |
| 70 | + public IReadOnlyList<ChatMessage> Messages { get; } |
| 71 | + |
| 72 | + /// <summary>Gets the agent's final answer.</summary> |
| 73 | + public string FinalText { get; } |
| 74 | + |
| 75 | + /// <summary>Gets the number of model calls the run took.</summary> |
| 76 | + public int Iterations { get; } |
| 77 | + |
| 78 | + /// <summary>Gets aggregate token usage, or <c>null</c> when not reported.</summary> |
| 79 | + public ChatUsage? Usage { get; } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Gets or sets the quality score for this trajectory, assigned by an evaluator (higher is better), or |
| 83 | + /// <c>null</c> while ungraded. |
| 84 | + /// </summary> |
| 85 | + public double? Reward { get; set; } |
| 86 | + |
| 87 | + /// <summary>Gets optional key/value metadata, or <c>null</c> when none was supplied.</summary> |
| 88 | + public IReadOnlyDictionary<string, string>? Metadata { get; } |
| 89 | +} |
0 commit comments