| | | 1 | | namespace AsiBackbone.Core.Actors; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides a default framework-neutral actor context implementation. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed record AsiBackboneActorContext : IAsiBackboneActorContext |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// The stable identifier used for unknown actors. |
| | | 10 | | /// </summary> |
| | | 11 | | public const string UnknownActorId = "unknown"; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// The stable identifier used for system actors. |
| | | 15 | | /// </summary> |
| | | 16 | | public const string SystemActorId = "system"; |
| | | 17 | | |
| | 183 | 18 | | private AsiBackboneActorContext( |
| | 183 | 19 | | string actorId, |
| | 183 | 20 | | AsiBackboneActorType actorType, |
| | 183 | 21 | | string? displayName, |
| | 183 | 22 | | bool isKnown, |
| | 183 | 23 | | bool isAuthenticated) |
| | | 24 | | { |
| | 183 | 25 | | ActorId = NormalizeActorId(actorId); |
| | 180 | 26 | | ActorType = actorType; |
| | 180 | 27 | | DisplayName = NormalizeDisplayName(displayName); |
| | 180 | 28 | | IsKnown = isKnown; |
| | 180 | 29 | | IsAuthenticated = isAuthenticated; |
| | 180 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 257 | 33 | | public string ActorId { get; } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | 257 | 36 | | public AsiBackboneActorType ActorType { get; } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 247 | 39 | | public string? DisplayName { get; } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 28 | 42 | | public bool IsKnown { get; } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 34 | 45 | | public bool IsAuthenticated { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets a shared actor context for unknown or unauthenticated operations. |
| | | 49 | | /// </summary> |
| | 14 | 50 | | public static AsiBackboneActorContext Unknown { get; } = |
| | 4 | 51 | | new(UnknownActorId, AsiBackboneActorType.Unknown, null, isKnown: false, isAuthenticated: false); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets a shared actor context for trusted system operations. |
| | | 55 | | /// </summary> |
| | 64 | 56 | | public static AsiBackboneActorContext System { get; } = |
| | 4 | 57 | | new(SystemActorId, AsiBackboneActorType.System, "System", isKnown: true, isAuthenticated: true); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Creates an actor context for a human participant. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="actorId">The stable actor identifier.</param> |
| | | 63 | | /// <param name="displayName">The optional display name.</param> |
| | | 64 | | /// <param name="isAuthenticated">Whether the host has authenticated the actor.</param> |
| | | 65 | | /// <returns>A human actor context.</returns> |
| | | 66 | | public static AsiBackboneActorContext Human( |
| | | 67 | | string actorId, |
| | | 68 | | string? displayName = null, |
| | | 69 | | bool isAuthenticated = true) |
| | | 70 | | { |
| | 130 | 71 | | return new AsiBackboneActorContext( |
| | 130 | 72 | | actorId, |
| | 130 | 73 | | AsiBackboneActorType.Human, |
| | 130 | 74 | | displayName, |
| | 130 | 75 | | isKnown: true, |
| | 130 | 76 | | isAuthenticated: isAuthenticated); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Creates an actor context for a service participant. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="actorId">The stable service actor identifier.</param> |
| | | 83 | | /// <param name="displayName">The optional display name.</param> |
| | | 84 | | /// <returns>A service actor context.</returns> |
| | | 85 | | public static AsiBackboneActorContext Service(string actorId, string? displayName = null) |
| | | 86 | | { |
| | 39 | 87 | | return new AsiBackboneActorContext( |
| | 39 | 88 | | actorId, |
| | 39 | 89 | | AsiBackboneActorType.Service, |
| | 39 | 90 | | displayName, |
| | 39 | 91 | | isKnown: true, |
| | 39 | 92 | | isAuthenticated: true); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Creates an actor context for a delegated or autonomous software agent. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="actorId">The stable agent actor identifier.</param> |
| | | 99 | | /// <param name="displayName">The optional display name.</param> |
| | | 100 | | /// <param name="isAuthenticated">Whether the host has authenticated the agent.</param> |
| | | 101 | | /// <returns>An agent actor context.</returns> |
| | | 102 | | public static AsiBackboneActorContext Agent( |
| | | 103 | | string actorId, |
| | | 104 | | string? displayName = null, |
| | | 105 | | bool isAuthenticated = true) |
| | | 106 | | { |
| | 6 | 107 | | return new AsiBackboneActorContext( |
| | 6 | 108 | | actorId, |
| | 6 | 109 | | AsiBackboneActorType.Agent, |
| | 6 | 110 | | displayName, |
| | 6 | 111 | | isKnown: true, |
| | 6 | 112 | | isAuthenticated: isAuthenticated); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | private static string NormalizeActorId(string actorId) |
| | | 116 | | { |
| | 183 | 117 | | ArgumentException.ThrowIfNullOrWhiteSpace(actorId); |
| | | 118 | | |
| | 180 | 119 | | return actorId.Trim(); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private static string? NormalizeDisplayName(string? displayName) |
| | | 123 | | { |
| | 180 | 124 | | string? normalizedDisplayName = displayName?.Trim(); |
| | | 125 | | |
| | 180 | 126 | | return string.IsNullOrWhiteSpace(normalizedDisplayName) |
| | 180 | 127 | | ? null |
| | 180 | 128 | | : normalizedDisplayName; |
| | | 129 | | } |
| | | 130 | | } |