| | | 1 | | using System.Text; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Resolves the Langfuse OTLP/HTTP ingestion endpoints and authentication headers from a |
| | | 7 | | /// <see cref="LangfuseOptions"/> instance. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Langfuse exposes an OpenTelemetry-compatible endpoint at <c>/api/public/otel</c> and |
| | | 11 | | /// authenticates with HTTP Basic auth over the base64 of <c>publicKey:secretKey</c>. Only |
| | | 12 | | /// OTLP over HTTP is supported (gRPC is not), so the .NET exporter is always configured for |
| | | 13 | | /// the <c>HttpProtobuf</c> protocol against the signal-specific paths produced here. |
| | | 14 | | /// </remarks> |
| | | 15 | | internal sealed class LangfuseEndpoints |
| | | 16 | | { |
| | | 17 | | private const string OtelBasePath = "api/public/otel"; |
| | | 18 | | |
| | 7 | 19 | | private LangfuseEndpoints( |
| | 7 | 20 | | Uri baseUrl, |
| | 7 | 21 | | Uri tracesEndpoint, |
| | 7 | 22 | | Uri metricsEndpoint, |
| | 7 | 23 | | Uri scoresEndpoint, |
| | 7 | 24 | | string authorizationHeaderValue, |
| | 7 | 25 | | string headers) |
| | | 26 | | { |
| | 7 | 27 | | BaseUrl = baseUrl; |
| | 7 | 28 | | TracesEndpoint = tracesEndpoint; |
| | 7 | 29 | | MetricsEndpoint = metricsEndpoint; |
| | 7 | 30 | | ScoresEndpoint = scoresEndpoint; |
| | 7 | 31 | | AuthorizationHeaderValue = authorizationHeaderValue; |
| | 7 | 32 | | Headers = headers; |
| | 7 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the resolved Langfuse base URL with a trailing slash (for example |
| | | 37 | | /// <c>https://cloud.langfuse.com/</c>). Public REST API paths such as |
| | | 38 | | /// <c>api/public/dataset-run-items</c> are resolved relative to this. |
| | | 39 | | /// </summary> |
| | 1 | 40 | | public Uri BaseUrl { get; } |
| | | 41 | | |
| | | 42 | | /// <summary>Gets the full OTLP/HTTP traces endpoint (<c>.../api/public/otel/v1/traces</c>).</summary> |
| | 6 | 43 | | public Uri TracesEndpoint { get; } |
| | | 44 | | |
| | | 45 | | /// <summary>Gets the full OTLP/HTTP metrics endpoint (<c>.../api/public/otel/v1/metrics</c>).</summary> |
| | 1 | 46 | | public Uri MetricsEndpoint { get; } |
| | | 47 | | |
| | | 48 | | /// <summary>Gets the Langfuse public Scores API endpoint (<c>.../api/public/scores</c>).</summary> |
| | 3 | 49 | | public Uri ScoresEndpoint { get; } |
| | | 50 | | |
| | | 51 | | /// <summary>Gets the <c>Authorization</c> header value (<c>Basic <base64></c>).</summary> |
| | 3 | 52 | | public string AuthorizationHeaderValue { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the comma-separated header string consumed by the OTLP exporter, carrying the |
| | | 56 | | /// <c>Authorization</c> header and the Langfuse ingestion-version header. |
| | | 57 | | /// </summary> |
| | 3 | 58 | | public string Headers { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Resolves the ingestion endpoints and headers for the supplied options. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="options">A configured options instance.</param> |
| | | 64 | | /// <returns>The resolved <see cref="LangfuseEndpoints"/>.</returns> |
| | | 65 | | /// <exception cref="ArgumentNullException"><paramref name="options"/> is <see langword="null"/>.</exception> |
| | | 66 | | /// <exception cref="InvalidOperationException"> |
| | | 67 | | /// <paramref name="options"/> is missing the public or secret key, or its |
| | | 68 | | /// <see cref="LangfuseOptions.Host"/>/<see cref="LangfuseOptions.Region"/> cannot be resolved |
| | | 69 | | /// to an absolute HTTP(S) URL. |
| | | 70 | | /// </exception> |
| | | 71 | | public static LangfuseEndpoints Resolve(LangfuseOptions options) |
| | | 72 | | { |
| | 10 | 73 | | ArgumentNullException.ThrowIfNull(options); |
| | | 74 | | |
| | 10 | 75 | | if (string.IsNullOrWhiteSpace(options.PublicKey) || string.IsNullOrWhiteSpace(options.SecretKey)) |
| | | 76 | | { |
| | 1 | 77 | | throw new InvalidOperationException( |
| | 1 | 78 | | "Langfuse public and secret keys are required to resolve ingestion endpoints. " + |
| | 1 | 79 | | "Check LangfuseOptions.IsConfigured before resolving endpoints."); |
| | | 80 | | } |
| | | 81 | | |
| | 9 | 82 | | var baseUrl = ResolveBaseUrl(options); |
| | | 83 | | |
| | 7 | 84 | | var auth = Convert.ToBase64String( |
| | 7 | 85 | | Encoding.UTF8.GetBytes($"{options.PublicKey}:{options.SecretKey}")); |
| | 7 | 86 | | var authorizationHeaderValue = $"Basic {auth}"; |
| | 7 | 87 | | var headers = $"Authorization={authorizationHeaderValue},x-langfuse-ingestion-version=4"; |
| | | 88 | | |
| | 7 | 89 | | return new LangfuseEndpoints( |
| | 7 | 90 | | baseUrl: baseUrl, |
| | 7 | 91 | | tracesEndpoint: new Uri(baseUrl, $"{OtelBasePath}/v1/traces"), |
| | 7 | 92 | | metricsEndpoint: new Uri(baseUrl, $"{OtelBasePath}/v1/metrics"), |
| | 7 | 93 | | scoresEndpoint: new Uri(baseUrl, "api/public/scores"), |
| | 7 | 94 | | authorizationHeaderValue: authorizationHeaderValue, |
| | 7 | 95 | | headers: headers); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | private static Uri ResolveBaseUrl(LangfuseOptions options) |
| | | 99 | | { |
| | | 100 | | string raw; |
| | 9 | 101 | | if (!string.IsNullOrWhiteSpace(options.Host)) |
| | | 102 | | { |
| | 3 | 103 | | raw = options.Host; |
| | | 104 | | } |
| | 6 | 105 | | else if (options.Region is { } region) |
| | | 106 | | { |
| | 5 | 107 | | raw = RegionBaseUrl(region); |
| | | 108 | | } |
| | | 109 | | else |
| | | 110 | | { |
| | 1 | 111 | | throw new InvalidOperationException( |
| | 1 | 112 | | "Langfuse export target is not set. Provide a Host (self-hosted) or a Region " + |
| | 1 | 113 | | "(Langfuse Cloud). Check LangfuseOptions.IsConfigured before resolving endpoints."); |
| | | 114 | | } |
| | | 115 | | |
| | 8 | 116 | | if (!Uri.TryCreate(EnsureTrailingSlash(raw), UriKind.Absolute, out var uri) |
| | 8 | 117 | | || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) |
| | | 118 | | { |
| | 1 | 119 | | throw new InvalidOperationException( |
| | 1 | 120 | | $"Langfuse host '{raw}' is not a valid absolute HTTP(S) URL."); |
| | | 121 | | } |
| | | 122 | | |
| | 7 | 123 | | return uri; |
| | | 124 | | } |
| | | 125 | | |
| | 5 | 126 | | private static string RegionBaseUrl(LangfuseRegion region) => region switch |
| | 5 | 127 | | { |
| | 2 | 128 | | LangfuseRegion.Eu => "https://cloud.langfuse.com", |
| | 1 | 129 | | LangfuseRegion.Us => "https://us.cloud.langfuse.com", |
| | 1 | 130 | | LangfuseRegion.Jp => "https://jp.cloud.langfuse.com", |
| | 1 | 131 | | LangfuseRegion.Hipaa => "https://hipaa.cloud.langfuse.com", |
| | 0 | 132 | | _ => throw new InvalidOperationException($"Unknown Langfuse region '{region}'."), |
| | 5 | 133 | | }; |
| | | 134 | | |
| | | 135 | | private static string EnsureTrailingSlash(string url) => |
| | 8 | 136 | | url.EndsWith('/') ? url : url + "/"; |
| | | 137 | | } |