| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | using Microsoft.Agents.AI.Workflows; |
| | | 5 | | using Microsoft.Extensions.AI; |
| | | 6 | | |
| | | 7 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 8 | | |
| | | 9 | | using ProgressEvents = NexusLabs.Needlr.AgentFramework.Progress; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.AgentFramework.Workflows.Diagnostics; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Extension methods for running workflows with per-stage diagnostics and real-time |
| | | 15 | | /// progress reporting. |
| | | 16 | | /// </summary> |
| | | 17 | | public static class PipelineRunExtensions |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Executes the workflow with per-stage diagnostics (no progress reporting). |
| | | 21 | | /// </summary> |
| | | 22 | | public static Task<IPipelineRunResult> RunWithDiagnosticsAsync( |
| | | 23 | | this Workflow workflow, |
| | | 24 | | string message, |
| | | 25 | | IAgentDiagnosticsAccessor diagnosticsAccessor, |
| | | 26 | | CancellationToken cancellationToken = default) => |
| | 8 | 27 | | RunWithDiagnosticsAsync(workflow, message, new WorkflowRunOptions |
| | 8 | 28 | | { |
| | 8 | 29 | | DiagnosticsAccessor = diagnosticsAccessor, |
| | 8 | 30 | | CancellationToken = cancellationToken, |
| | 8 | 31 | | }); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Executes the workflow with per-stage diagnostics and real-time progress reporting. |
| | | 35 | | /// </summary> |
| | | 36 | | public static Task<IPipelineRunResult> RunWithDiagnosticsAsync( |
| | | 37 | | this Workflow workflow, |
| | | 38 | | string message, |
| | | 39 | | IAgentDiagnosticsAccessor diagnosticsAccessor, |
| | | 40 | | ProgressEvents.IProgressReporter? progressReporter, |
| | | 41 | | CancellationToken cancellationToken = default) => |
| | 0 | 42 | | RunWithDiagnosticsAsync(workflow, message, new WorkflowRunOptions |
| | 0 | 43 | | { |
| | 0 | 44 | | DiagnosticsAccessor = diagnosticsAccessor, |
| | 0 | 45 | | ProgressReporter = progressReporter, |
| | 0 | 46 | | CancellationToken = cancellationToken, |
| | 0 | 47 | | }); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Executes the workflow with per-stage diagnostics, real-time progress reporting, |
| | | 51 | | /// and per-LLM-call completion draining via the provided collector. |
| | | 52 | | /// </summary> |
| | | 53 | | public static Task<IPipelineRunResult> RunWithDiagnosticsAsync( |
| | | 54 | | this Workflow workflow, |
| | | 55 | | string message, |
| | | 56 | | IAgentDiagnosticsAccessor diagnosticsAccessor, |
| | | 57 | | ProgressEvents.IProgressReporter? progressReporter, |
| | | 58 | | IChatCompletionCollector? completionCollector, |
| | | 59 | | ProgressEvents.IProgressReporterAccessor? progressReporterAccessor = null, |
| | | 60 | | CancellationToken cancellationToken = default) => |
| | 0 | 61 | | RunWithDiagnosticsAsync(workflow, message, new WorkflowRunOptions |
| | 0 | 62 | | { |
| | 0 | 63 | | DiagnosticsAccessor = diagnosticsAccessor, |
| | 0 | 64 | | ProgressReporter = progressReporter, |
| | 0 | 65 | | CompletionCollector = completionCollector, |
| | 0 | 66 | | ProgressReporterAccessor = progressReporterAccessor, |
| | 0 | 67 | | CancellationToken = cancellationToken, |
| | 0 | 68 | | }); |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Executes the workflow with per-stage diagnostics and progress reporting |
| | | 72 | | /// configured via <see cref="WorkflowRunOptions"/>. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="workflow">The workflow to execute.</param> |
| | | 75 | | /// <param name="message">The user message to send.</param> |
| | | 76 | | /// <param name="options">Configuration for diagnostics, progress, and completion collection.</param> |
| | | 77 | | public static async Task<IPipelineRunResult> RunWithDiagnosticsAsync( |
| | | 78 | | this Workflow workflow, |
| | | 79 | | string message, |
| | | 80 | | WorkflowRunOptions options) |
| | | 81 | | { |
| | 9 | 82 | | ArgumentNullException.ThrowIfNull(workflow); |
| | 9 | 83 | | ArgumentException.ThrowIfNullOrEmpty(message); |
| | 9 | 84 | | ArgumentNullException.ThrowIfNull(options); |
| | | 85 | | |
| | 9 | 86 | | var diagnosticsAccessor = options.DiagnosticsAccessor; |
| | 9 | 87 | | var reporter = options.ProgressReporter ?? ProgressEvents.NullProgressReporter.Instance; |
| | 9 | 88 | | var collector = options.CompletionCollector |
| | 9 | 89 | | ?? diagnosticsAccessor.CompletionCollector |
| | 9 | 90 | | ?? NullChatCompletionCollector.Instance; |
| | 9 | 91 | | var toolCollector = diagnosticsAccessor.ToolCallCollector; |
| | 9 | 92 | | var progressReporterAccessor = options.ProgressReporterAccessor; |
| | 9 | 93 | | var cancellationToken = options.CancellationToken; |
| | 9 | 94 | | var pipelineStart = Stopwatch.StartNew(); |
| | 9 | 95 | | var stages = new List<IAgentStageResult>(); |
| | 9 | 96 | | var responses = new Dictionary<string, StringBuilder>(); |
| | 9 | 97 | | var invocations = new List<(string ExecutorId, DateTimeOffset InvokedAt)>(); |
| | 9 | 98 | | string? currentExecutorId = null; |
| | 9 | 99 | | bool succeeded = true; |
| | 9 | 100 | | string? errorMessage = null; |
| | 9 | 101 | | Exception? caughtException = null; |
| | 9 | 102 | | int superStepCount = 0; |
| | | 103 | | |
| | 9 | 104 | | collector.DrainCompletions(); // drain stale |
| | 9 | 105 | | toolCollector?.DrainToolCalls(); // drain stale |
| | | 106 | | |
| | | 107 | | // Set the progress reporter on the AsyncLocal accessor so chat/tool middleware |
| | | 108 | | // can emit LLM call and tool call events in real-time. |
| | 9 | 109 | | var progressScope = progressReporterAccessor?.BeginScope(reporter); |
| | | 110 | | |
| | 9 | 111 | | reporter.Report(new ProgressEvents.WorkflowStartedEvent( |
| | 9 | 112 | | Timestamp: DateTimeOffset.UtcNow, |
| | 9 | 113 | | WorkflowId: reporter.WorkflowId, |
| | 9 | 114 | | AgentId: null, |
| | 9 | 115 | | ParentAgentId: null, |
| | 9 | 116 | | Depth: 0, |
| | 9 | 117 | | SequenceNumber: reporter.NextSequence())); |
| | | 118 | | |
| | | 119 | | try |
| | | 120 | | { |
| | 9 | 121 | | using (diagnosticsAccessor.BeginCapture()) |
| | | 122 | | { |
| | 9 | 123 | | await using var run = await InProcessExecution.RunStreamingAsync( |
| | 9 | 124 | | workflow, |
| | 9 | 125 | | new ChatMessage(ChatRole.User, message), |
| | 9 | 126 | | cancellationToken: cancellationToken); |
| | | 127 | | |
| | 9 | 128 | | await run.TrySendMessageAsync(new TurnToken(emitEvents: true)); |
| | | 129 | | |
| | 9 | 130 | | CancellationTokenRegistration? budgetRegistration = null; |
| | 9 | 131 | | if (cancellationToken.CanBeCanceled) |
| | | 132 | | { |
| | 9 | 133 | | budgetRegistration = cancellationToken.Register(() => |
| | 9 | 134 | | { |
| | 0 | 135 | | _ = run.CancelRunAsync(); |
| | 9 | 136 | | }); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | try |
| | | 140 | | { |
| | 648 | 141 | | await foreach (var evt in run.WatchStreamAsync(cancellationToken)) |
| | | 142 | | { |
| | 315 | 143 | | if (evt is ExecutorInvokedEvent invoked) |
| | | 144 | | { |
| | 98 | 145 | | var invokedId = invoked.ExecutorId ?? "unknown"; |
| | 98 | 146 | | invocations.Add((invokedId, DateTimeOffset.UtcNow)); |
| | | 147 | | |
| | 98 | 148 | | reporter.Report(new ProgressEvents.AgentInvokedEvent( |
| | 98 | 149 | | Timestamp: DateTimeOffset.UtcNow, |
| | 98 | 150 | | WorkflowId: reporter.WorkflowId, |
| | 98 | 151 | | AgentId: invokedId, |
| | 98 | 152 | | ParentAgentId: null, |
| | 98 | 153 | | Depth: 1, |
| | 98 | 154 | | SequenceNumber: reporter.NextSequence(), |
| | 98 | 155 | | AgentName: invokedId)); |
| | 98 | 156 | | continue; |
| | | 157 | | } |
| | | 158 | | |
| | 217 | 159 | | if (evt is ExecutorFailedEvent executorFailed) |
| | | 160 | | { |
| | 1 | 161 | | succeeded = false; |
| | 1 | 162 | | errorMessage = executorFailed.Data?.Message; |
| | 1 | 163 | | reporter.Report(new ProgressEvents.AgentFailedEvent( |
| | 1 | 164 | | Timestamp: DateTimeOffset.UtcNow, |
| | 1 | 165 | | WorkflowId: reporter.WorkflowId, |
| | 1 | 166 | | AgentId: executorFailed.ExecutorId, |
| | 1 | 167 | | ParentAgentId: null, |
| | 1 | 168 | | Depth: 1, |
| | 1 | 169 | | SequenceNumber: reporter.NextSequence(), |
| | 1 | 170 | | AgentName: executorFailed.ExecutorId ?? "unknown", |
| | 1 | 171 | | ErrorMessage: executorFailed.Data?.Message ?? "unknown error")); |
| | 1 | 172 | | continue; |
| | | 173 | | } |
| | | 174 | | |
| | 216 | 175 | | if (evt is WorkflowErrorEvent workflowError) |
| | | 176 | | { |
| | 1 | 177 | | succeeded = false; |
| | 1 | 178 | | errorMessage = workflowError.Exception?.Message; |
| | 1 | 179 | | continue; |
| | | 180 | | } |
| | | 181 | | |
| | 215 | 182 | | if (evt is SuperStepStartedEvent) |
| | | 183 | | { |
| | 41 | 184 | | superStepCount++; |
| | 41 | 185 | | reporter.Report(new ProgressEvents.SuperStepStartedProgressEvent( |
| | 41 | 186 | | Timestamp: DateTimeOffset.UtcNow, |
| | 41 | 187 | | WorkflowId: reporter.WorkflowId, |
| | 41 | 188 | | AgentId: null, |
| | 41 | 189 | | ParentAgentId: null, |
| | 41 | 190 | | Depth: 0, |
| | 41 | 191 | | SequenceNumber: reporter.NextSequence(), |
| | 41 | 192 | | StepNumber: superStepCount)); |
| | 41 | 193 | | continue; |
| | | 194 | | } |
| | | 195 | | |
| | 174 | 196 | | if (evt is SuperStepCompletedEvent) |
| | | 197 | | { |
| | 40 | 198 | | reporter.Report(new ProgressEvents.SuperStepCompletedProgressEvent( |
| | 40 | 199 | | Timestamp: DateTimeOffset.UtcNow, |
| | 40 | 200 | | WorkflowId: reporter.WorkflowId, |
| | 40 | 201 | | AgentId: null, |
| | 40 | 202 | | ParentAgentId: null, |
| | 40 | 203 | | Depth: 0, |
| | 40 | 204 | | SequenceNumber: reporter.NextSequence(), |
| | 40 | 205 | | StepNumber: superStepCount)); |
| | 40 | 206 | | continue; |
| | | 207 | | } |
| | | 208 | | |
| | 134 | 209 | | if (evt is not AgentResponseUpdateEvent update |
| | 134 | 210 | | || update.ExecutorId is null |
| | 134 | 211 | | || update.Data is null) |
| | | 212 | | { |
| | | 213 | | continue; |
| | | 214 | | } |
| | | 215 | | |
| | 20 | 216 | | var text = update.Data.ToString(); |
| | 20 | 217 | | if (string.IsNullOrEmpty(text)) |
| | | 218 | | continue; |
| | | 219 | | |
| | | 220 | | // Emit handoff progress event at turn boundaries |
| | 16 | 221 | | if (currentExecutorId is not null |
| | 16 | 222 | | && currentExecutorId != update.ExecutorId) |
| | | 223 | | { |
| | 8 | 224 | | reporter.Report(new ProgressEvents.AgentHandoffEvent( |
| | 8 | 225 | | Timestamp: DateTimeOffset.UtcNow, |
| | 8 | 226 | | WorkflowId: reporter.WorkflowId, |
| | 8 | 227 | | AgentId: null, |
| | 8 | 228 | | ParentAgentId: null, |
| | 8 | 229 | | Depth: 0, |
| | 8 | 230 | | SequenceNumber: reporter.NextSequence(), |
| | 8 | 231 | | FromAgentId: currentExecutorId, |
| | 8 | 232 | | ToAgentId: update.ExecutorId)); |
| | | 233 | | } |
| | | 234 | | |
| | 16 | 235 | | currentExecutorId = update.ExecutorId; |
| | | 236 | | |
| | 16 | 237 | | if (!responses.TryGetValue(update.ExecutorId, out var sb)) |
| | 16 | 238 | | responses[update.ExecutorId] = sb = new StringBuilder(); |
| | | 239 | | |
| | 16 | 240 | | sb.Append(text); |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | // Drain all completions and partition them across agent stages. |
| | | 244 | | // Event loop timestamps are unreliable (events are buffered), so we use |
| | | 245 | | // completion timestamps (captured at actual LLM call time) for both |
| | | 246 | | // duration calculation and agent attribution. |
| | 9 | 247 | | var allCompletions = collector.DrainCompletions() |
| | 16 | 248 | | .OrderBy(c => c.StartedAt) |
| | 9 | 249 | | .ToList(); |
| | | 250 | | |
| | | 251 | | // Drain tool calls from the collector for the fallback path. |
| | 9 | 252 | | var allToolCalls = toolCollector?.DrainToolCalls() |
| | 0 | 253 | | ?.OrderBy(t => t.StartedAt) |
| | 9 | 254 | | .ToList() |
| | 9 | 255 | | ?? []; |
| | | 256 | | |
| | | 257 | | // Filter invocations to only real agents (have response text or completions |
| | | 258 | | // attributed by name). Skips non-agent executors like "GroupChatHost". |
| | 9 | 259 | | var agentInvocations = invocations |
| | 98 | 260 | | .Where(inv => responses.ContainsKey(inv.ExecutorId)) |
| | 32 | 261 | | .Select(inv => inv.ExecutorId) |
| | 9 | 262 | | .Distinct() |
| | 9 | 263 | | .ToList(); |
| | | 264 | | |
| | | 265 | | // Partition completions by agent using name matching or temporal gaps. |
| | 9 | 266 | | var partitioned = PartitionCompletionsByAgent( |
| | 9 | 267 | | allCompletions, agentInvocations); |
| | | 268 | | |
| | | 269 | | // Partition tool calls by agent using AgentName attribution. |
| | 9 | 270 | | var partitionedToolCalls = PartitionToolCallsByAgent( |
| | 9 | 271 | | allToolCalls, agentInvocations); |
| | | 272 | | |
| | 50 | 273 | | for (int i = 0; i < agentInvocations.Count; i++) |
| | | 274 | | { |
| | 16 | 275 | | var executorId = agentInvocations[i]; |
| | 16 | 276 | | var stageCompletions = i < partitioned.Count |
| | 16 | 277 | | ? partitioned[i] : []; |
| | 16 | 278 | | var stageToolCalls = i < partitionedToolCalls.Count |
| | 16 | 279 | | ? partitionedToolCalls[i] : []; |
| | | 280 | | |
| | 16 | 281 | | var responseText = responses.TryGetValue(executorId, out var respSb) |
| | 16 | 282 | | ? respSb.ToString() |
| | 16 | 283 | | : string.Empty; |
| | | 284 | | |
| | | 285 | | // Duration from completion timestamps (reliable), not event timestamps. |
| | 16 | 286 | | var duration = stageCompletions.Count > 0 |
| | 16 | 287 | | ? stageCompletions[^1].CompletedAt - stageCompletions[0].StartedAt |
| | 16 | 288 | | : TimeSpan.Zero; |
| | 16 | 289 | | var startedAt = stageCompletions.Count > 0 |
| | 16 | 290 | | ? stageCompletions[0].StartedAt |
| | 16 | 291 | | : DateTimeOffset.UtcNow; |
| | | 292 | | |
| | 16 | 293 | | stages.Add(BuildStageResultFromCompletions( |
| | 16 | 294 | | executorId, |
| | 16 | 295 | | responseText, |
| | 16 | 296 | | diagnosticsAccessor, |
| | 16 | 297 | | stageCompletions, |
| | 16 | 298 | | stageToolCalls, |
| | 16 | 299 | | duration, |
| | 16 | 300 | | startedAt)); |
| | | 301 | | |
| | 16 | 302 | | reporter.Report(new ProgressEvents.AgentCompletedEvent( |
| | 16 | 303 | | Timestamp: DateTimeOffset.UtcNow, |
| | 16 | 304 | | WorkflowId: reporter.WorkflowId, |
| | 16 | 305 | | AgentId: executorId, |
| | 16 | 306 | | ParentAgentId: null, |
| | 16 | 307 | | Depth: 1, |
| | 16 | 308 | | SequenceNumber: reporter.NextSequence(), |
| | 16 | 309 | | AgentName: executorId, |
| | 16 | 310 | | Duration: duration, |
| | 16 | 311 | | TotalTokens: stages[^1].Diagnostics?.AggregateTokenUsage.TotalTokens ?? 0)); |
| | | 312 | | } |
| | 9 | 313 | | } |
| | | 314 | | finally |
| | | 315 | | { |
| | 9 | 316 | | budgetRegistration?.Dispose(); |
| | | 317 | | } |
| | 9 | 318 | | } |
| | 9 | 319 | | } |
| | 0 | 320 | | catch (Exception ex) |
| | | 321 | | { |
| | 0 | 322 | | succeeded = false; |
| | 0 | 323 | | errorMessage = ex.ToString(); |
| | 0 | 324 | | caughtException = ex; |
| | | 325 | | |
| | | 326 | | // If we know which agent was running when the exception propagated |
| | | 327 | | // out of the stream, emit an AgentFailedEvent for it so sinks see |
| | | 328 | | // the per-agent failure before the trailing WorkflowCompletedEvent. |
| | 0 | 329 | | if (currentExecutorId is not null) |
| | | 330 | | { |
| | 0 | 331 | | reporter.Report(new ProgressEvents.AgentFailedEvent( |
| | 0 | 332 | | Timestamp: DateTimeOffset.UtcNow, |
| | 0 | 333 | | WorkflowId: reporter.WorkflowId, |
| | 0 | 334 | | AgentId: currentExecutorId, |
| | 0 | 335 | | ParentAgentId: null, |
| | 0 | 336 | | Depth: 1, |
| | 0 | 337 | | SequenceNumber: reporter.NextSequence(), |
| | 0 | 338 | | AgentName: currentExecutorId, |
| | 0 | 339 | | ErrorMessage: ex.Message)); |
| | | 340 | | } |
| | 0 | 341 | | } |
| | | 342 | | |
| | 9 | 343 | | pipelineStart.Stop(); |
| | | 344 | | |
| | 9 | 345 | | reporter.Report(new ProgressEvents.WorkflowCompletedEvent( |
| | 9 | 346 | | Timestamp: DateTimeOffset.UtcNow, |
| | 9 | 347 | | WorkflowId: reporter.WorkflowId, |
| | 9 | 348 | | AgentId: null, |
| | 9 | 349 | | ParentAgentId: null, |
| | 9 | 350 | | Depth: 0, |
| | 9 | 351 | | SequenceNumber: reporter.NextSequence(), |
| | 9 | 352 | | Succeeded: succeeded, |
| | 9 | 353 | | ErrorMessage: errorMessage, |
| | 9 | 354 | | TotalDuration: pipelineStart.Elapsed)); |
| | | 355 | | |
| | 9 | 356 | | progressScope?.Dispose(); |
| | | 357 | | |
| | 9 | 358 | | return new PipelineRunResult( |
| | 9 | 359 | | stages: stages, |
| | 9 | 360 | | totalDuration: pipelineStart.Elapsed, |
| | 9 | 361 | | succeeded: succeeded, |
| | 9 | 362 | | errorMessage: errorMessage, |
| | 9 | 363 | | exception: caughtException); |
| | 9 | 364 | | } |
| | | 365 | | |
| | | 366 | | private static IAgentStageResult BuildStageResultFromCompletions( |
| | | 367 | | string agentName, |
| | | 368 | | string responseText, |
| | | 369 | | IAgentDiagnosticsAccessor diagnosticsAccessor, |
| | | 370 | | IReadOnlyList<ChatCompletionDiagnostics> completions, |
| | | 371 | | IReadOnlyList<ToolCallDiagnostics> toolCalls, |
| | | 372 | | TimeSpan turnDuration, |
| | | 373 | | DateTimeOffset turnStartedAt) |
| | | 374 | | { |
| | 16 | 375 | | var finalResponse = string.IsNullOrEmpty(responseText) |
| | 16 | 376 | | ? null |
| | 16 | 377 | | : new ChatResponse(new ChatMessage(ChatRole.Assistant, responseText)); |
| | | 378 | | |
| | 16 | 379 | | var middlewareDiag = diagnosticsAccessor.LastRunDiagnostics; |
| | 16 | 380 | | if (middlewareDiag is not null) |
| | | 381 | | { |
| | 16 | 382 | | return new AgentStageResult(agentName, finalResponse, middlewareDiag); |
| | | 383 | | } |
| | | 384 | | |
| | 0 | 385 | | var totalTokens = new TokenUsage( |
| | 0 | 386 | | InputTokens: completions.Sum(c => c.Tokens.InputTokens), |
| | 0 | 387 | | OutputTokens: completions.Sum(c => c.Tokens.OutputTokens), |
| | 0 | 388 | | TotalTokens: completions.Sum(c => c.Tokens.TotalTokens), |
| | 0 | 389 | | CachedInputTokens: completions.Sum(c => c.Tokens.CachedInputTokens), |
| | 0 | 390 | | ReasoningTokens: completions.Sum(c => c.Tokens.ReasoningTokens)); |
| | | 391 | | |
| | 0 | 392 | | return new AgentStageResult( |
| | 0 | 393 | | agentName, |
| | 0 | 394 | | finalResponse, |
| | 0 | 395 | | new AgentRunDiagnostics( |
| | 0 | 396 | | AgentName: agentName, |
| | 0 | 397 | | TotalDuration: turnDuration, |
| | 0 | 398 | | AggregateTokenUsage: totalTokens, |
| | 0 | 399 | | ChatCompletions: completions, |
| | 0 | 400 | | ToolCalls: toolCalls, |
| | 0 | 401 | | TotalInputMessages: 0, |
| | 0 | 402 | | TotalOutputMessages: 0, |
| | 0 | 403 | | InputMessages: [], |
| | 0 | 404 | | OutputResponse: null, |
| | 0 | 405 | | Succeeded: true, |
| | 0 | 406 | | ErrorMessage: null, |
| | 0 | 407 | | StartedAt: turnStartedAt, |
| | 0 | 408 | | CompletedAt: DateTimeOffset.UtcNow)); |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Partitions an ordered list of completions into groups, one per agent invocation. |
| | | 413 | | /// First attempts name-based matching. When completions lack agent names, uses temporal |
| | | 414 | | /// gap analysis: finds the N-1 largest gaps between consecutive completions (where N is |
| | | 415 | | /// the agent count) and splits at those boundaries. |
| | | 416 | | /// </summary> |
| | | 417 | | private static IReadOnlyList<IReadOnlyList<ChatCompletionDiagnostics>> PartitionCompletionsByAgent( |
| | | 418 | | List<ChatCompletionDiagnostics> sorted, |
| | | 419 | | IReadOnlyList<string> agentExecutorIds) |
| | | 420 | | { |
| | 9 | 421 | | if (sorted.Count == 0 || agentExecutorIds.Count == 0) |
| | 1 | 422 | | return agentExecutorIds.Select(_ => (IReadOnlyList<ChatCompletionDiagnostics>)[]).ToList(); |
| | | 423 | | |
| | | 424 | | // Try name-based partitioning first. |
| | 8 | 425 | | var byName = new List<IReadOnlyList<ChatCompletionDiagnostics>>(); |
| | 8 | 426 | | bool allMatched = true; |
| | 48 | 427 | | foreach (var executorId in agentExecutorIds) |
| | | 428 | | { |
| | 16 | 429 | | var matched = sorted |
| | 32 | 430 | | .Where(c => c.AgentName is not null |
| | 32 | 431 | | && (executorId.Equals(c.AgentName, StringComparison.Ordinal) |
| | 32 | 432 | | || executorId.StartsWith(c.AgentName + "_", StringComparison.Ordinal))) |
| | 16 | 433 | | .ToList(); |
| | 16 | 434 | | byName.Add(matched); |
| | 16 | 435 | | if (matched.Count == 0) |
| | 0 | 436 | | allMatched = false; |
| | | 437 | | } |
| | | 438 | | |
| | 8 | 439 | | if (allMatched) |
| | 8 | 440 | | return byName; |
| | | 441 | | |
| | | 442 | | // Fall back to round-robin interleaving: in a RoundRobinGroupChatManager, |
| | | 443 | | // agents alternate turns. Completion[i] belongs to agent[i % N]. |
| | 0 | 444 | | var interleaved = agentExecutorIds |
| | 0 | 445 | | .Select(_ => new List<ChatCompletionDiagnostics>()) |
| | 0 | 446 | | .ToList(); |
| | | 447 | | |
| | 0 | 448 | | for (int i = 0; i < sorted.Count; i++) |
| | | 449 | | { |
| | 0 | 450 | | interleaved[i % agentExecutorIds.Count].Add(sorted[i]); |
| | | 451 | | } |
| | | 452 | | |
| | 0 | 453 | | return interleaved.Select(l => (IReadOnlyList<ChatCompletionDiagnostics>)l).ToList(); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | /// <summary> |
| | | 457 | | /// Partitions tool calls by agent using the <see cref="ToolCallDiagnostics.AgentName"/> |
| | | 458 | | /// field. Tool calls without an agent name are distributed to the first agent bucket. |
| | | 459 | | /// </summary> |
| | | 460 | | private static IReadOnlyList<IReadOnlyList<ToolCallDiagnostics>> PartitionToolCallsByAgent( |
| | | 461 | | List<ToolCallDiagnostics> sorted, |
| | | 462 | | IReadOnlyList<string> agentExecutorIds) |
| | | 463 | | { |
| | 9 | 464 | | if (sorted.Count == 0 || agentExecutorIds.Count == 0) |
| | 25 | 465 | | return agentExecutorIds.Select(_ => (IReadOnlyList<ToolCallDiagnostics>)[]).ToList(); |
| | | 466 | | |
| | 0 | 467 | | var buckets = agentExecutorIds |
| | 0 | 468 | | .Select(_ => new List<ToolCallDiagnostics>()) |
| | 0 | 469 | | .ToList(); |
| | | 470 | | |
| | 0 | 471 | | foreach (var tc in sorted) |
| | | 472 | | { |
| | 0 | 473 | | var matched = false; |
| | 0 | 474 | | for (int i = 0; i < agentExecutorIds.Count; i++) |
| | | 475 | | { |
| | 0 | 476 | | if (tc.AgentName is not null |
| | 0 | 477 | | && (agentExecutorIds[i].Equals(tc.AgentName, StringComparison.Ordinal) |
| | 0 | 478 | | || agentExecutorIds[i].StartsWith(tc.AgentName + "_", StringComparison.Ordinal))) |
| | | 479 | | { |
| | 0 | 480 | | buckets[i].Add(tc); |
| | 0 | 481 | | matched = true; |
| | 0 | 482 | | break; |
| | | 483 | | } |
| | | 484 | | } |
| | | 485 | | |
| | 0 | 486 | | if (!matched) |
| | | 487 | | { |
| | 0 | 488 | | buckets[0].Add(tc); |
| | | 489 | | } |
| | | 490 | | } |
| | | 491 | | |
| | 0 | 492 | | return buckets.Select(l => (IReadOnlyList<ToolCallDiagnostics>)l).ToList(); |
| | | 493 | | } |
| | | 494 | | } |