| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Identifies the Langfuse object a score is attached to. Langfuse supports scoring a trace, a |
| | | 5 | | /// specific observation within a trace, or a whole session (a score may also target a dataset run, |
| | | 6 | | /// which the experiment path handles separately). |
| | | 7 | | /// </summary> |
| | | 8 | | internal readonly record struct LangfuseScoreTarget |
| | | 9 | | { |
| | | 10 | | private LangfuseScoreTarget(string? traceId, string? observationId, string? sessionId, string contextId) |
| | | 11 | | { |
| | 11 | 12 | | TraceId = traceId; |
| | 11 | 13 | | ObservationId = observationId; |
| | 11 | 14 | | SessionId = sessionId; |
| | 11 | 15 | | ContextId = contextId; |
| | 11 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary>Gets the trace id, or <see langword="null"/> for a session-only score.</summary> |
| | 13 | 19 | | public string? TraceId { get; } |
| | | 20 | | |
| | | 21 | | /// <summary>Gets the observation id, or <see langword="null"/>.</summary> |
| | 11 | 22 | | public string? ObservationId { get; } |
| | | 23 | | |
| | | 24 | | /// <summary>Gets the session id, or <see langword="null"/>.</summary> |
| | 11 | 25 | | public string? SessionId { get; } |
| | | 26 | | |
| | | 27 | | /// <summary>Gets a human-readable id used in diagnostics when a score upload fails.</summary> |
| | 0 | 28 | | public string ContextId { get; } |
| | | 29 | | |
| | | 30 | | /// <summary>Targets an entire trace.</summary> |
| | | 31 | | /// <param name="traceId">The trace id.</param> |
| | | 32 | | /// <returns>A trace-level target.</returns> |
| | | 33 | | public static LangfuseScoreTarget Trace(string traceId) |
| | | 34 | | { |
| | 8 | 35 | | ArgumentException.ThrowIfNullOrWhiteSpace(traceId); |
| | 8 | 36 | | return new LangfuseScoreTarget(traceId, observationId: null, sessionId: null, traceId); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary>Targets a specific observation within a trace.</summary> |
| | | 40 | | /// <param name="traceId">The owning trace id.</param> |
| | | 41 | | /// <param name="observationId">The observation id.</param> |
| | | 42 | | /// <returns>An observation-level target.</returns> |
| | | 43 | | public static LangfuseScoreTarget Observation(string traceId, string observationId) |
| | | 44 | | { |
| | 1 | 45 | | ArgumentException.ThrowIfNullOrWhiteSpace(traceId); |
| | 1 | 46 | | ArgumentException.ThrowIfNullOrWhiteSpace(observationId); |
| | 1 | 47 | | return new LangfuseScoreTarget(traceId, observationId, sessionId: null, observationId); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary>Targets a whole session.</summary> |
| | | 51 | | /// <param name="sessionId">The session id.</param> |
| | | 52 | | /// <returns>A session-level target.</returns> |
| | | 53 | | public static LangfuseScoreTarget Session(string sessionId) |
| | | 54 | | { |
| | 2 | 55 | | ArgumentException.ThrowIfNullOrWhiteSpace(sessionId); |
| | 2 | 56 | | return new LangfuseScoreTarget(traceId: null, observationId: null, sessionId, sessionId); |
| | | 57 | | } |
| | | 58 | | } |