| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Context; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Default implementation of <see cref="IAgentExecutionContext"/>. Immutable record that |
| | | 5 | | /// carries user identity, orchestration ID, an optional workspace, and an extensible |
| | | 6 | | /// property bag. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <param name="UserId">The user identity for the current orchestration.</param> |
| | | 9 | | /// <param name="OrchestrationId">Correlation ID for the current orchestration run.</param> |
| | | 10 | | /// <param name="Workspace">Optional workspace for agent file operations. Stored in |
| | | 11 | | /// <see cref="IAgentExecutionContext.Properties"/> under the <see cref="Workspace.IWorkspace"/> |
| | | 12 | | /// type key so it is accessible via <see cref="AgentExecutionContextExtensions.GetWorkspace"/>.</param> |
| | | 13 | | /// <param name="Properties">Extensible property bag for consumer-specific data.</param> |
| | | 14 | | [DoNotAutoRegister] |
| | 42 | 15 | | public sealed record AgentExecutionContext( |
| | 8 | 16 | | string UserId, |
| | 3 | 17 | | string OrchestrationId, |
| | 17 | 18 | | IReadOnlyDictionary<string, object>? Properties = null, |
| | 62 | 19 | | Workspace.IWorkspace? Workspace = null) : IAgentExecutionContext |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | IReadOnlyDictionary<string, object> IAgentExecutionContext.Properties => |
| | 17 | 23 | | BuildProperties(); |
| | | 24 | | |
| | | 25 | | private IReadOnlyDictionary<string, object> BuildProperties() |
| | | 26 | | { |
| | 17 | 27 | | if (Workspace is null) |
| | 14 | 28 | | return Properties ?? EmptyProperties.Instance; |
| | | 29 | | |
| | | 30 | | // Merge the workspace into the property bag so GetWorkspace() works |
| | | 31 | | // regardless of whether the consumer uses this default implementation |
| | | 32 | | // or a custom one. |
| | 3 | 33 | | var merged = Properties is not null |
| | 3 | 34 | | ? new Dictionary<string, object>(Properties) |
| | 3 | 35 | | : new Dictionary<string, object>(); |
| | | 36 | | |
| | 3 | 37 | | merged[typeof(Workspace.IWorkspace).FullName!] = Workspace; |
| | 3 | 38 | | return merged; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static class EmptyProperties |
| | | 42 | | { |
| | 1 | 43 | | internal static readonly IReadOnlyDictionary<string, object> Instance = |
| | 1 | 44 | | new Dictionary<string, object>(); |
| | | 45 | | } |
| | | 46 | | } |