diff options
Diffstat (limited to 'src/Agent.App/Logging/JsonLinesRuntimeObserver.cs')
| -rw-r--r-- | src/Agent.App/Logging/JsonLinesRuntimeObserver.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs new file mode 100644 index 0000000..b7cbd26 --- /dev/null +++ b/src/Agent.App/Logging/JsonLinesRuntimeObserver.cs @@ -0,0 +1,37 @@ +using System.Text.Json; +using Agent.Core.Ollama; +using Agent.Core.Runtime; +using Agent.Core.Tools; + +namespace Agent.App.Logging; + +public sealed class JsonLinesRuntimeObserver(string logDirectory) : IAgentRuntimeObserver +{ + private readonly object _sync = new(); + private readonly string _path = Path.Combine(logDirectory, "llm.jsonl"); + + public void LlmRequest(Guid taskId, int iteration, IReadOnlyList<OllamaMessage> messages) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.request", iteration, + messages = messages.Select(static message => new { message.Role, message.Content, image_count = message.Images?.Count ?? 0, message.ToolName }) + }); + + public void LlmResponse(Guid taskId, int iteration, OllamaResult result) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.response", iteration, + result.Content, input_tokens = result.InputTokens, output_tokens = result.OutputTokens, + tool_calls = result.ToolCalls?.Select(static call => call.Function.Name).ToArray() ?? [] + }); + + public void ToolResult(Guid taskId, string toolName, ToolExecutionResult result) => Write(new + { + timestamp = DateTimeOffset.UtcNow, task_id = taskId, event_name = "llm.tool_result", tool_name = toolName, + result.Success, result.ExternalEffect, result.Output, result.FailureFingerprint + }); + + private void Write(object value) + { + string line = JsonSerializer.Serialize(value); + lock (_sync) File.AppendAllText(_path, line + Environment.NewLine); + } +} |
