-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAiChatAdapter.cs
More file actions
566 lines (508 loc) · 25.6 KB
/
Copy pathOpenAiChatAdapter.cs
File metadata and controls
566 lines (508 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Nodes;
using AdaptiveApi.Core.Abstractions;
using AdaptiveApi.Core.Model;
using AdaptiveApi.Core.Pipeline;
using AdaptiveApi.Core.Plugins;
using AdaptiveApi.Core.Proxy;
using AdaptiveApi.Core.Routing;
using AdaptiveApi.Core.Rules;
using AdaptiveApi.Core.Streaming;
using AdaptiveApi.Plugins.SDK.Hooks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace AdaptiveApi.Providers.OpenAI;
public sealed class OpenAiChatAdapter : IProviderAdapter
{
private readonly IHttpClientFactory _httpFactory;
private readonly ITranslatorRouter _translatorRouter;
private readonly IRuleResolver _ruleResolver;
private readonly IAuditSink _audit;
private readonly IPiiRedactor _piiRedactor;
private readonly ITranslationCache _translationCache;
private readonly IPluginHookDispatcher _hooks;
private readonly ILogger<OpenAiChatAdapter> _log;
public OpenAiChatAdapter(
IHttpClientFactory httpFactory,
ITranslatorRouter translatorRouter,
IRuleResolver ruleResolver,
IAuditSink audit,
IPiiRedactor piiRedactor,
ITranslationCache translationCache,
IPluginHookDispatcher hooks,
ILogger<OpenAiChatAdapter> log)
{
_httpFactory = httpFactory;
_translatorRouter = translatorRouter;
_ruleResolver = ruleResolver;
_audit = audit;
_piiRedactor = piiRedactor;
_translationCache = translationCache;
_hooks = hooks;
_log = log;
}
public string ProviderId => "openai.chat";
public RouteKind RouteKind => RouteKind.OpenAiChat;
public bool Matches(HttpRequest request)
{
var path = request.Path.Value ?? string.Empty;
return request.Method == HttpMethods.Post
&& (path.EndsWith("/chat/completions", StringComparison.Ordinal)
|| path.EndsWith("/responses", StringComparison.Ordinal)
|| path.EndsWith("/embeddings", StringComparison.Ordinal));
}
public async Task HandleAsync(HttpContext context, RouteConfig route, CancellationToken ct)
{
var sw = Stopwatch.StartNew();
var timings = new PipelineTimings();
var inbound = context.Request;
var upstreamPath = ExtractUpstreamPath(inbound.Path.Value!);
var upstreamUri = new Uri(route.UpstreamBaseUrl, upstreamPath + inbound.QueryString);
var (bodyBytes, streaming) = await ReadBodyAsync(inbound, ct);
var effective = ApplyHeaderOverrides(route, inbound);
var translationActive = effective.Direction != DirectionMode.Off
&& effective.UserLanguage.Value != effective.LlmLanguage.Value;
// Debug mode is off unless the caller explicitly asks for it. In debug mode
// the adapter captures the pre/post-translation bodies and the raw translator
// I/O, then attaches them to the response so callers can show the full chain.
// Never turn this on for production clients — payloads can include user input.
var debugMode = DebugRequested(inbound);
var debug = debugMode ? new DebugRecorder() : null;
if (debug is not null && bodyBytes.Length > 0)
debug.SetRequestPre(System.Text.Encoding.UTF8.GetString(bodyBytes));
var planSw = Stopwatch.StartNew();
var rules = translationActive
? await _ruleResolver.ResolveAsync(effective, ct)
: EmptyRules(effective);
planSw.Stop();
timings.Record("plan", planSw.Elapsed, "resolve rules + planner");
var hookCtx = new PipelineHookContext(
HttpContext: context,
RouteId: effective.RouteId,
TenantId: effective.TenantId,
ProviderId: ProviderId,
UserLanguage: effective.UserLanguage.Value,
LlmLanguage: effective.LlmLanguage.Value,
Direction: effective.Direction.ToString(),
IsStreaming: streaming,
Properties: new Dictionary<string, object?>(StringComparer.Ordinal));
// Hook 1/6 — before request translation
var hookResult = await _hooks.RunBeforeRequestTranslationAsync(hookCtx, bodyBytes, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
if (hookResult.ModifiedBody is { } prereqBody) bodyBytes = prereqBody;
var forwardBody = bodyBytes;
var requestChars = 0;
// Request translation runs whether or not the response is streamed — the two
// are orthogonal. Streaming only changes how the RESPONSE is emitted.
if (translationActive && bodyBytes.Length > 0
&& effective.Direction is DirectionMode.Bidirectional or DirectionMode.RequestOnly)
{
var translateSw = Stopwatch.StartNew();
(forwardBody, requestChars) = await TranslateJsonAsync(bodyBytes, rules.RequestAllowlist,
source: effective.UserLanguage, target: effective.LlmLanguage, effective, rules, rules.RequestStyle, ct,
debug: debug, debugDirection: "translate-request");
translateSw.Stop();
timings.Record("translate-request", translateSw.Elapsed,
$"{effective.UserLanguage.Value} to {effective.LlmLanguage.Value}");
}
if (debug is not null && forwardBody.Length > 0)
debug.SetRequestPost(System.Text.Encoding.UTF8.GetString(forwardBody));
// Hook 2/6 — after request translation
hookResult = await _hooks.RunAfterRequestTranslationAsync(hookCtx, forwardBody, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
if (hookResult.ModifiedBody is { } postreqBody) forwardBody = postreqBody;
using var upstreamReq = new HttpRequestMessage(new HttpMethod(inbound.Method), upstreamUri);
if (forwardBody.Length > 0)
{
upstreamReq.Content = new ByteArrayContent(forwardBody);
upstreamReq.Content.Headers.TryAddWithoutValidation(
"Content-Type", inbound.ContentType ?? "application/json");
}
HeaderForwarder.CopyInboundToUpstream(inbound.Headers, upstreamReq, upstreamReq.Content);
// Hook 3/6 — before AI call
hookResult = await _hooks.RunBeforeAiAsync(hookCtx, upstreamReq, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
var http = _httpFactory.CreateClient("openai-upstream");
var upstreamSw = Stopwatch.StartNew();
using var upstreamResp = await http.SendAsync(upstreamReq, HttpCompletionOption.ResponseHeadersRead, ct);
upstreamSw.Stop();
timings.Record("upstream", upstreamSw.Elapsed, "OpenAI round-trip");
// Hook 4/6 — after AI call
hookResult = await _hooks.RunAfterAiAsync(hookCtx, upstreamResp, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
context.Response.StatusCode = (int)upstreamResp.StatusCode;
context.Response.Headers.Remove("transfer-encoding");
var responseTranslationActive = translationActive
&& upstreamResp.IsSuccessStatusCode
&& (effective.Direction is DirectionMode.Bidirectional or DirectionMode.ResponseOnly);
var integrityFailures = 0;
var responseChars = 0;
if (streaming && responseTranslationActive)
{
HeaderForwarder.CopyUpstreamToClient(upstreamResp, context.Response.Headers);
context.Response.Headers["Content-Type"] = "text/event-stream";
context.Response.Headers.Remove("Content-Length");
// Emit Server-Timing before the SSE body starts streaming. Response-translate
// timings can't be captured under streaming; the client measures total stream
// wall-time instead.
timings.WriteTo(context);
// Stream translators lack the debug-pipeline hook that the JSON path uses.
// Capture the raw upstream stream bytes (capped) + the translated output
// bytes we emit to the client, so debug clients see both sides.
var strategy = StreamStrategyFor(inbound);
var respTransSw = Stopwatch.StartNew();
StreamTranslatorMetrics m;
if (debug is not null)
{
var upstreamCapture = new MemoryStream();
var clientCapture = new MemoryStream();
await using (upstreamCapture)
await using (clientCapture)
await using (var sourceTap = await upstreamResp.Content.ReadAsStreamAsync(ct))
{
using var sourceTee = new TappingStream(sourceTap, upstreamCapture, cap: 128 * 1024);
using var sinkTee = new TappingStream(context.Response.Body, clientCapture, cap: 128 * 1024);
var translator = _translatorRouter.Resolve(effective);
if (strategy == "progressive")
{
var progressive = new ProgressiveStreamTranslator(translator, systemContext: rules.SystemContext);
m = await progressive.TranslateAsync(sourceTee, sinkTee,
source: effective.LlmLanguage, target: effective.UserLanguage, ct);
}
else
{
var streamer = new OpenAiStreamTranslator(translator, systemContext: rules.SystemContext);
m = await streamer.TranslateAsync(sourceTee, sinkTee,
source: effective.LlmLanguage, target: effective.UserLanguage, ct);
}
debug.SetUpstreamResponse(System.Text.Encoding.UTF8.GetString(upstreamCapture.ToArray()));
debug.SetFinalResponse(System.Text.Encoding.UTF8.GetString(clientCapture.ToArray()));
}
}
else
{
await using var upstreamStream = await upstreamResp.Content.ReadAsStreamAsync(ct);
var translator = _translatorRouter.Resolve(effective);
if (strategy == "progressive")
{
var progressive = new ProgressiveStreamTranslator(translator, systemContext: rules.SystemContext);
m = await progressive.TranslateAsync(upstreamStream, context.Response.Body,
source: effective.LlmLanguage, target: effective.UserLanguage, ct);
}
else
{
var streamer = new OpenAiStreamTranslator(translator, systemContext: rules.SystemContext);
m = await streamer.TranslateAsync(upstreamStream, context.Response.Body,
source: effective.LlmLanguage, target: effective.UserLanguage, ct);
}
}
respTransSw.Stop();
timings.Record("translate-response-stream", respTransSw.Elapsed,
$"{strategy} stream, {m.CharsTranslated} chars");
integrityFailures = m.IntegrityFailures;
responseChars = m.CharsTranslated;
// Emit a trailing SSE event with the final timings so downstream clients
// can reconstruct the full pipeline timeline even though Server-Timing
// had to be flushed before the body started.
var trailingJson = BuildTrailingTimingsJson(timings, strategy, m);
var trailingBytes = System.Text.Encoding.UTF8.GetBytes(
$"event: x-adaptiveapi-timing\ndata: {trailingJson}\n\n");
await context.Response.Body.WriteAsync(trailingBytes, ct);
await context.Response.Body.FlushAsync(ct);
// Mirror debug traces as their own trailing SSE event so clients get the
// translator I/O and body samples without needing a second channel.
if (debug is not null)
{
var debugJson = BuildDebugEventJson(debug);
var debugBytes = System.Text.Encoding.UTF8.GetBytes(
$"event: x-adaptiveapi-debug\ndata: {debugJson}\n\n");
await context.Response.Body.WriteAsync(debugBytes, ct);
await context.Response.Body.FlushAsync(ct);
}
}
else if (!responseTranslationActive)
{
timings.WriteTo(context);
HeaderForwarder.CopyUpstreamToClient(upstreamResp, context.Response.Headers);
await using var upstreamStream = await upstreamResp.Content.ReadAsStreamAsync(ct);
await upstreamStream.CopyToAsync(context.Response.Body, 8192, ct);
}
else
{
var respBytes = await upstreamResp.Content.ReadAsByteArrayAsync(ct);
if (debug is not null)
debug.SetUpstreamResponse(System.Text.Encoding.UTF8.GetString(respBytes));
// Hook 5/6 — before response translation
hookResult = await _hooks.RunBeforeResponseTranslationAsync(hookCtx, respBytes, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
if (hookResult.ModifiedBody is { } preRespBody) respBytes = preRespBody;
var respTransSw = Stopwatch.StartNew();
var (translatedResp, chars) = await TranslateJsonAsync(respBytes, rules.ResponseAllowlist,
source: effective.LlmLanguage, target: effective.UserLanguage, effective, rules, rules.ResponseStyle, ct,
debug: debug, debugDirection: "translate-response");
respTransSw.Stop();
timings.Record("translate-response", respTransSw.Elapsed,
$"{effective.LlmLanguage.Value} to {effective.UserLanguage.Value}");
responseChars = chars;
// Hook 6/6 — after response translation
hookResult = await _hooks.RunAfterResponseTranslationAsync(hookCtx, translatedResp, ct);
if (await ApplyShortCircuitAsync(context, hookResult, ct)) return;
if (hookResult.ModifiedBody is { } postRespBody) translatedResp = postRespBody;
HeaderForwarder.CopyUpstreamToClient(upstreamResp, context.Response.Headers);
// When debug is on, inject `_debug` into the response JSON so the demo can
// display the translator I/O alongside the message. This mutates the OpenAI
// response shape — that's acceptable in debug mode since the caller opted in.
byte[] finalBytes;
if (debug is not null)
{
finalBytes = InjectDebugIntoJsonBody(translatedResp, debug);
}
else
{
finalBytes = translatedResp;
}
if (debug is not null)
debug.SetFinalResponse(System.Text.Encoding.UTF8.GetString(finalBytes));
context.Response.Headers["Content-Length"] = finalBytes.Length.ToString();
timings.WriteTo(context);
await context.Response.Body.WriteAsync(finalBytes, ct);
}
sw.Stop();
await _audit.RecordAsync(new AuditRecord(
TenantId: effective.TenantId,
RouteId: effective.RouteId,
Method: inbound.Method,
Path: inbound.Path.Value ?? string.Empty,
Status: (int)upstreamResp.StatusCode,
UserLanguage: effective.UserLanguage.Value,
LlmLanguage: effective.LlmLanguage.Value,
Direction: effective.Direction.ToString(),
TranslatorId: effective.TranslatorId,
GlossaryId: effective.GlossaryId,
RequestStyleRuleId: effective.RequestStyleRuleId,
ResponseStyleRuleId: effective.ResponseStyleRuleId,
RequestChars: requestChars,
ResponseChars: responseChars,
IntegrityFailures: integrityFailures,
DurationMs: sw.ElapsedMilliseconds), ct);
}
private static async Task<bool> ApplyShortCircuitAsync(HttpContext context, HookResult result, CancellationToken ct)
{
if (result.ContinuePipeline) return false;
context.Response.StatusCode = result.ShortCircuitStatus ?? StatusCodes.Status403Forbidden;
if (!string.IsNullOrEmpty(result.ShortCircuitContentType))
context.Response.Headers["Content-Type"] = result.ShortCircuitContentType;
if (result.ShortCircuitBody is { Length: > 0 } body)
await context.Response.Body.WriteAsync(body, ct);
return true;
}
private async Task<(byte[] Bytes, int Chars)> TranslateJsonAsync(
byte[] bytes, Allowlist allowlist, LanguageCode source, LanguageCode target,
RouteConfig route, ResolvedRules rules, StyleBinding style, CancellationToken ct,
DebugRecorder? debug = null, string debugDirection = "translate")
{
JsonNode? root;
try { root = JsonNode.Parse(bytes); }
catch (JsonException) { return (bytes, 0); }
if (root is null) return (bytes, 0);
var translator = _translatorRouter.Resolve(route);
var pipeline = new TranslationPipeline(translator, _piiRedactor, _translationCache);
var pipelineOptions = new PipelineOptions
{
Source = source,
Target = target,
GlossaryId = rules.DeeplGlossaryId,
StyleRuleId = style.DeeplStyleId,
CustomInstructions = rules.SystemInstructionsFor(style, source.Value, target.Value),
DoNotTranslateTerms = rules.DoNotTranslateFor(source.Value, target.Value),
Formality = rules.Formality,
RedactPii = rules.RedactPii,
PiiDetectors = rules.PiiDetectors,
Context = rules.SystemContext,
TranslationMemoryId = route.TranslationMemoryId,
TranslationMemoryThreshold = route.TranslationMemoryThreshold,
Debug = debug,
DebugDirection = debugDirection,
};
var stats = await pipeline.TranslateInPlaceAsync(root, allowlist, pipelineOptions, ct);
await ToolCallTranslator.TranslateAsync(
root, ToolCallTranslator.RootShape.OpenAiChat,
translator, source, target, rules.ToolArgsDenylist, ct);
var outBytes = System.Text.Encoding.UTF8.GetBytes(root.ToJsonString());
return (outBytes, bytes.Length); // chars approximates bytes for sizing audit
}
private static ResolvedRules EmptyRules(RouteConfig route) => new(
Glossary: Array.Empty<GlossaryTerm>(),
DeeplGlossaryId: null,
RequestStyle: StyleBinding.Empty,
ResponseStyle: StyleBinding.Empty,
RequestAllowlist: AllowlistCatalog.Request(route.Kind),
ResponseAllowlist: AllowlistCatalog.Response(route.Kind),
ToolArgsDenylist: ToolArgsDenylist.Default,
Formality: Formality.Default);
private static RouteConfig ApplyHeaderOverrides(RouteConfig route, HttpRequest req)
{
var (userOverride, llmOverride) = LangHeader.Parse(req.Headers);
var userLang = userOverride is not null ? new LanguageCode(userOverride) : route.UserLanguage;
var llmLang = llmOverride is not null ? new LanguageCode(llmOverride) : route.LlmLanguage;
var mode = req.Headers.TryGetValue("X-AdaptiveApi-Mode", out var m)
? Enum.TryParse<DirectionMode>(m.ToString(), true, out var parsed) ? parsed : route.Direction
: route.Direction;
var translator = req.Headers.TryGetValue("X-AdaptiveApi-Translator", out var t)
? t.ToString() : route.TranslatorId;
var glossary = req.Headers.TryGetValue("X-AdaptiveApi-Glossary", out var g) ? g.ToString() : route.GlossaryId;
// Style-rule overrides are direction-specific. The legacy
// `X-AdaptiveApi-Style-Rule` header (which set both directions) was
// removed — callers must use the per-direction headers.
var requestStyle = req.Headers.TryGetValue("X-AdaptiveApi-Request-Style-Rule", out var rsr)
? rsr.ToString()
: route.RequestStyleRuleId;
var responseStyle = req.Headers.TryGetValue("X-AdaptiveApi-Response-Style-Rule", out var rspsr)
? rspsr.ToString()
: route.ResponseStyleRuleId;
var tmId = req.Headers.TryGetValue("X-AdaptiveApi-Translation-Memory", out var tm)
? tm.ToString() : route.TranslationMemoryId;
int? tmThreshold = req.Headers.TryGetValue("X-AdaptiveApi-Tm-Threshold", out var tmt)
&& int.TryParse(tmt.ToString(), out var parsedThreshold)
? parsedThreshold : route.TranslationMemoryThreshold;
if (LangHeader.AnyOverrideSet(req.Headers) && mode == DirectionMode.Off)
mode = DirectionMode.Bidirectional;
return route with
{
UserLanguage = userLang,
LlmLanguage = llmLang,
Direction = mode,
TranslatorId = translator,
GlossaryId = glossary,
RequestStyleRuleId = requestStyle,
ResponseStyleRuleId = responseStyle,
TranslationMemoryId = string.IsNullOrEmpty(tmId) ? null : tmId,
TranslationMemoryThreshold = tmThreshold,
};
}
private static async Task<(byte[] Body, bool Streaming)> ReadBodyAsync(HttpRequest req, CancellationToken ct)
{
if (req.ContentLength is null or 0 && !req.Headers.ContainsKey("Transfer-Encoding"))
return (Array.Empty<byte>(), false);
using var ms = new MemoryStream();
await req.Body.CopyToAsync(ms, ct);
var bytes = ms.ToArray();
var streaming = IsStreamRequested(bytes);
return (bytes, streaming);
}
private static bool IsStreamRequested(byte[] bytes)
{
if (bytes.Length == 0) return false;
try
{
using var doc = JsonDocument.Parse(bytes);
return doc.RootElement.TryGetProperty("stream", out var s)
&& s.ValueKind == JsonValueKind.True;
}
catch (JsonException)
{
return false;
}
}
/// Picks a streaming strategy. Request header `X-AdaptiveApi-Stream-Strategy: progressive`
/// flips to the lower-latency progressive translator. Default stays on the
/// sentence-boundary translator from M3.
private static string StreamStrategyFor(HttpRequest req)
{
if (req.Headers.TryGetValue("X-AdaptiveApi-Stream-Strategy", out var v))
return v.ToString().Trim().ToLowerInvariant();
return "sentence";
}
private static bool DebugRequested(HttpRequest req)
{
if (!req.Headers.TryGetValue("X-AdaptiveApi-Debug", out var v)) return false;
var asked = v.ToString().Trim().ToLowerInvariant();
return asked == "payloads" || asked == "1" || asked == "true" || asked == "on";
}
private static string BuildDebugEventJson(DebugRecorder debug)
{
var payload = BuildDebugNode(debug);
return payload.ToJsonString();
}
private static byte[] InjectDebugIntoJsonBody(byte[] body, DebugRecorder debug)
{
JsonNode? root;
try { root = JsonNode.Parse(body); }
catch (JsonException) { return body; }
if (root is not JsonObject obj) return body;
obj["_debug"] = BuildDebugNode(debug);
return System.Text.Encoding.UTF8.GetBytes(obj.ToJsonString());
}
private static JsonObject BuildDebugNode(DebugRecorder debug)
{
var traces = new JsonArray();
foreach (var t in debug.Translations)
{
var pairs = new JsonArray();
foreach (var p in t.Pairs)
{
pairs.Add(new JsonObject
{
["source"] = p.Source,
["target"] = p.Target,
});
}
traces.Add(new JsonObject
{
["direction"] = t.Direction,
["sourceLanguage"] = t.SourceLanguage,
["targetLanguage"] = t.TargetLanguage,
["pairs"] = pairs,
});
}
return new JsonObject
{
["bodies"] = new JsonObject
{
["requestPreTranslation"] = debug.RequestBodyPreTranslation,
["requestPostTranslation"] = debug.RequestBodyPostTranslation,
["upstreamResponse"] = debug.UpstreamResponseBody,
["finalResponse"] = debug.FinalResponseBody,
},
["translatorCalls"] = traces,
};
}
/// Serialises the full pipeline timings plus final stream stats so the trailing
/// `x-adaptiveapi-timing` SSE event carries the same information Server-Timing
/// would have, for cases where the client is parsing SSE anyway.
private static string BuildTrailingTimingsJson(
PipelineTimings timings, string strategy, StreamTranslatorMetrics m)
{
var entries = new JsonArray();
foreach (var e in timings.Entries)
{
entries.Add(new JsonObject
{
["name"] = e.Name,
["durationMs"] = e.DurationMs,
["desc"] = e.Description,
});
}
var payload = new JsonObject
{
["entries"] = entries,
["stream"] = new JsonObject
{
["strategy"] = strategy,
["eventsIn"] = m.EventsIn,
["eventsOut"] = m.EventsOut,
["charsTranslated"] = m.CharsTranslated,
["integrityFailures"] = m.IntegrityFailures,
},
};
return payload.ToJsonString();
}
private static string ExtractUpstreamPath(string inboundPath)
{
var segments = inboundPath.Split('/', StringSplitOptions.RemoveEmptyEntries);
if (segments.Length < 3 || segments[0] != "v1")
throw new InvalidOperationException($"unexpected inbound path shape: {inboundPath}");
return "/v1/" + string.Join('/', segments[2..]);
}
}