@tangle-network/agent-app 0.60.229 → 0.60.231

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -236,24 +236,12 @@ function assistantRowIdForTurn(turnKey) {
236
236
  return `assistant:${turnKey}`;
237
237
  }
238
238
 
239
- // src/chat-routes/turn-routes.ts
240
- import {
241
- deriveExecutionId,
242
- handleChatTurn
243
- } from "@tangle-network/agent-runtime/durable";
244
- function failureReasonOf(data) {
245
- if (!data) return void 0;
246
- const message = data.message ?? data.error ?? data.reason;
247
- if (typeof message === "string" && message.length > 0) return message;
248
- return void 0;
249
- }
250
- function failureCodeOf(data) {
251
- const code = data?.code;
252
- return typeof code === "string" && code.length > 0 ? code : void 0;
253
- }
239
+ // src/chat-routes/turn-route-contracts.ts
254
240
  var INTENT_LINE_TIMEOUT_MS = 1500;
255
241
  var INTENT_LINE_HISTORY_MESSAGES = 6;
256
242
  var INTENT_LINE_MAX_CHARS = 240;
243
+
244
+ // src/chat-routes/turn-admission.ts
257
245
  function errorResponse(err) {
258
246
  return Response.json({ code: err.code, error: err.message }, { status: err.status });
259
247
  }
@@ -301,6 +289,21 @@ function userPartsWithFiles(userParts, fileParts, mentions) {
301
289
  ...mentions.map((mention) => ({ ...mentionInputToPart(mention) }))
302
290
  ]);
303
291
  }
292
+
293
+ // src/chat-routes/turn-execution.ts
294
+ import { deriveExecutionId, handleChatTurn } from "@tangle-network/agent-runtime/durable";
295
+
296
+ // src/chat-routes/turn-event-stream.ts
297
+ function failureReasonOf(data) {
298
+ if (!data) return void 0;
299
+ const message = data.message ?? data.error ?? data.reason;
300
+ if (typeof message === "string" && message.length > 0) return message;
301
+ return void 0;
302
+ }
303
+ function failureCodeOf(data) {
304
+ const code = data?.code;
305
+ return typeof code === "string" && code.length > 0 ? code : void 0;
306
+ }
304
307
  function isVisibleText(event) {
305
308
  const value = event;
306
309
  if (event.type === "text") return typeof value.text === "string" ? value.text.trim().length > 0 : true;
@@ -364,227 +367,136 @@ async function* withStreamHeartbeat(source, intervalMs, makeEvent) {
364
367
  await iterator.return?.();
365
368
  }
366
369
  }
367
- function createChatTurnRoutes(options) {
368
- const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
369
- const draftStore = options.store;
370
- if (options.incrementalPersistence && !storeSupportsDraftPersistence(draftStore)) {
371
- throw new Error(
372
- "incrementalPersistence requires a store with updateMessage() \u2014 `/chat-store`'s createChatStore has it; a product store must implement it or omit the option"
373
- );
374
- }
375
- const draftTuning = options.incrementalPersistence === false || !storeSupportsDraftPersistence(draftStore) ? null : options.incrementalPersistence ?? {};
376
- const progressMessages = { ...TURN_ROUTE_PHASE_MESSAGES, ...options.progressMessages };
377
- const traceOptions = options.turnTrace === false ? void 0 : options.turnTrace;
378
- const traceSink = options.turnTrace === false ? void 0 : traceOptions?.sink ?? createIntelligenceTurnTraceSink({ project: options.projectId });
379
- function startIntentLine(admitted) {
380
- const intentLine = options.intentLine;
381
- if (!intentLine) return void 0;
382
- const { parsed, auth } = admitted;
383
- const threadId = parsed.payload.threadId;
384
- const controller = new AbortController();
385
- let timer;
386
- const deadline = new Promise((resolve) => {
387
- timer = setTimeout(() => {
388
- controller.abort();
389
- resolve(null);
390
- }, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
391
- });
392
- const generated = (async () => {
393
- const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
394
- const text = await intentLine.generate({
395
- content: parsed.content,
396
- history,
397
- tenantId: auth.tenantId,
398
- userId: auth.userId,
399
- threadId,
400
- context: auth.context,
401
- signal: controller.signal
402
- });
403
- const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
404
- return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
405
- })().catch((err) => {
406
- if (!controller.signal.aborted) {
407
- log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
370
+ function concatStreams(streams) {
371
+ let index = 0;
372
+ let reader = null;
373
+ return new ReadableStream({
374
+ async pull(controller) {
375
+ for (; ; ) {
376
+ if (!reader) {
377
+ const next = streams[index++];
378
+ if (!next) {
379
+ controller.close();
380
+ return;
381
+ }
382
+ reader = next.getReader();
383
+ }
384
+ const { done, value } = await reader.read();
385
+ if (done) {
386
+ reader = null;
387
+ continue;
388
+ }
389
+ controller.enqueue(value);
390
+ return;
408
391
  }
409
- return null;
410
- });
411
- return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
412
- }
413
- async function turn(request, ctx) {
414
- const admitted = await admitTurn(request, createTurnTrace({ d1QueryCounter: currentD1QueryCounter() }));
415
- if (!admitted.ok) return admitted.response;
416
- if (request.headers.get(TURN_PROGRESS_HEADER) !== TURN_PROGRESS_FIRST) return runTurn(request, ctx, admitted);
417
- return progressFirstTurn(request, ctx, admitted);
418
- }
419
- async function admitTurn(request, trace) {
420
- const parse = trace.start("admission.parse", "admission");
421
- const [rawBody, badBody] = await parseJsonObjectBody(request);
422
- if (badBody) return { ok: false, response: badBody };
423
- let parsed;
424
- try {
425
- parsed = validateTurnBody(rawBody, options.maxInlinePartBytes);
426
- } catch (err) {
427
- if (err instanceof ChatTurnInputError) return { ok: false, response: errorResponse(err) };
428
- throw err;
392
+ },
393
+ async cancel(reason) {
394
+ await reader?.cancel(reason);
395
+ for (const stream of streams.slice(index)) await stream.cancel(reason);
429
396
  }
430
- parse.end();
431
- const auth = await trace.measure("admission.authorize", "admission", (span) => options.authorize({ request, intent: "turn", body: parsed.payload, trace, span }));
432
- if (!auth.ok) return { ok: false, response: auth.response };
433
- const namedModel = typeof parsed.payload.model === "string" ? parsed.payload.model.trim() : "";
434
- const policy = options.modelPolicy;
435
- if (policy && namedModel) {
436
- const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(
437
- namedModel,
438
- policy.catalog,
439
- policy.filter,
440
- policy.context
441
- ));
442
- if (!decision.allowed) {
443
- return { ok: false, response: Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 }) };
444
- }
397
+ });
398
+ }
399
+ async function* abortOnSignal(source, signal) {
400
+ const iterator = source[Symbol.asyncIterator]();
401
+ try {
402
+ for (; ; ) {
403
+ const next = await nextWithAbort(iterator, signal);
404
+ if (next.done) return;
405
+ yield next.value;
445
406
  }
446
- return { ok: true, parsed, auth, trace };
407
+ } finally {
408
+ await closeIteratorAfterAbort(iterator);
447
409
  }
448
- function progressFirstTurn(request, ctx, admitted) {
449
- const receivedAt = Date.now();
450
- const encoder = new TextEncoder();
451
- const { readable, writable } = new TransformStream();
452
- const writer = writable.getWriter();
453
- let clientGone = false;
454
- const write = async (chunk) => {
455
- if (clientGone) return;
456
- try {
457
- await writer.write(chunk);
458
- } catch {
459
- clientGone = true;
460
- }
461
- };
462
- const writeEvent = (event) => write(encoder.encode(`${JSON.stringify(event)}
463
- `));
464
- const progress = (phase, message) => {
465
- void writeEvent(buildTurnPhaseEvent(phase, message, { sinceRequestMs: Date.now() - receivedAt }));
410
+ }
411
+ function nextWithAbort(iterator, signal) {
412
+ if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
413
+ return new Promise((resolve, reject) => {
414
+ let settled = false;
415
+ const cleanup = () => signal.removeEventListener("abort", onAbort);
416
+ const onAbort = () => {
417
+ if (settled) return;
418
+ settled = true;
419
+ cleanup();
420
+ reject(new Error("chat turn cancelled"));
466
421
  };
467
- progress("accepted", progressMessages.accepted);
468
- const intent = startIntentLine(admitted);
469
- let piping = false;
470
- let atLineStart = true;
471
- let pendingIntent = null;
472
- if (intent) {
473
- void intent.then((text) => {
474
- if (!text || clientGone) return;
475
- const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
476
- if (!piping || atLineStart) void writeEvent(event);
477
- else pendingIntent = event;
478
- });
422
+ signal.addEventListener("abort", onAbort, { once: true });
423
+ if (signal.aborted) {
424
+ onAbort();
425
+ return;
479
426
  }
480
- const pump = (async () => {
481
- let inner;
427
+ Promise.resolve().then(() => iterator.next()).then(
428
+ (result) => {
429
+ if (settled) return;
430
+ settled = true;
431
+ cleanup();
432
+ resolve(result);
433
+ },
434
+ (error) => {
435
+ if (settled) return;
436
+ settled = true;
437
+ cleanup();
438
+ reject(error);
439
+ }
440
+ );
441
+ });
442
+ }
443
+ var ABORT_CLEANUP_TIMEOUT_MS = 50;
444
+ async function closeIteratorAfterAbort(iterator) {
445
+ let closing;
446
+ try {
447
+ const result = iterator.return?.();
448
+ if (result) closing = Promise.resolve(result);
449
+ } catch {
450
+ return;
451
+ }
452
+ if (!closing) return;
453
+ let timeout;
454
+ try {
455
+ await Promise.race([
456
+ Promise.resolve(closing).catch(() => void 0),
457
+ new Promise((resolve) => {
458
+ timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
459
+ })
460
+ ]);
461
+ } finally {
462
+ if (timeout !== void 0) clearTimeout(timeout);
463
+ }
464
+ }
465
+
466
+ // src/chat-routes/turn-execution.ts
467
+ function modelAttributionFields(attribution) {
468
+ return {
469
+ ...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
470
+ ...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
471
+ ...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
472
+ ...attribution?.servedSource ? { servedSource: attribution.servedSource } : {}
473
+ };
474
+ }
475
+ function createAdmittedTurnRunner({
476
+ options,
477
+ draftStore,
478
+ draftTuning,
479
+ progressMessages,
480
+ traceOptions,
481
+ traceSink,
482
+ startIntentLine,
483
+ log
484
+ }) {
485
+ return async function runAdmittedTurn(request, ctx, admitted, early, progress, startedIntent) {
486
+ const { parsed, auth, trace } = admitted;
487
+ const intentLine = startedIntent ?? startIntentLine(admitted);
488
+ const { payload, content, fileParts, mentions, turnId } = parsed;
489
+ const { tenantId, userId, context } = auth;
490
+ if (options.turnLock?.warm) {
491
+ const warmSpan = trace.start("admission.lock_warm", "admission");
492
+ const warmFailed = (err) => {
493
+ warmSpan.end(void 0, err);
494
+ log("[chat-routes] turnLock.warm failed", { error: err instanceof Error ? err.message : String(err) });
495
+ };
482
496
  try {
483
- inner = await runTurn(request, ctx, admitted, progress, intent);
497
+ void Promise.resolve(options.turnLock.warm({ tenantId, userId, threadId: payload.threadId, context })).then(() => warmSpan.end(), warmFailed);
484
498
  } catch (err) {
485
- log("[chat-routes] turn failed before it streamed", { error: err instanceof Error ? err.message : String(err) });
486
- inner = Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
487
- }
488
- if (!inner.body || !inner.headers.get("x-turn-id")) {
489
- await writeEvent(await turnResponseEventFor(inner));
490
- } else {
491
- const reader = inner.body.getReader();
492
- piping = true;
493
- try {
494
- for (; ; ) {
495
- const { done, value } = await reader.read();
496
- if (done) break;
497
- atLineStart = value.length > 0 && value[value.length - 1] === 10;
498
- await write(value);
499
- if (atLineStart && pendingIntent) {
500
- const event = pendingIntent;
501
- pendingIntent = null;
502
- await writeEvent(event);
503
- }
504
- if (clientGone) {
505
- await reader.cancel().catch(() => {
506
- });
507
- break;
508
- }
509
- }
510
- } catch (err) {
511
- log("[chat-routes] progress-first pipe failed", { error: err instanceof Error ? err.message : String(err) });
512
- }
513
- }
514
- if (!clientGone) await writer.close().catch(() => {
515
- });
516
- })();
517
- if (ctx?.waitUntil) ctx.waitUntil(pump);
518
- else void pump.catch(() => {
519
- });
520
- return new Response(readable, {
521
- headers: {
522
- "Content-Type": "application/x-ndjson",
523
- "Cache-Control": "no-cache",
524
- [TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST
525
- }
526
- });
527
- }
528
- function startAfterAuthorize(request, admitted) {
529
- let release;
530
- const { parsed, auth, trace } = admitted;
531
- if (options.afterAuthorize) {
532
- try {
533
- const started = options.afterAuthorize({
534
- request,
535
- body: parsed.payload,
536
- tenantId: auth.tenantId,
537
- userId: auth.userId,
538
- threadId: parsed.payload.threadId,
539
- context: auth.context,
540
- trace
541
- });
542
- if (typeof started === "function") release = started;
543
- } catch (err) {
544
- log("[chat-routes] afterAuthorize failed; the turn goes on without it", { error: err instanceof Error ? err.message : String(err) });
545
- }
546
- }
547
- return {
548
- handOff: () => {
549
- release = void 0;
550
- },
551
- release: async () => {
552
- const free = release;
553
- release = void 0;
554
- if (!free) return;
555
- try {
556
- await free();
557
- } catch (err) {
558
- log("[chat-routes] afterAuthorize release failed", { error: err instanceof Error ? err.message : String(err) });
559
- }
560
- }
561
- };
562
- }
563
- async function runTurn(request, ctx, admitted, progress, startedIntent) {
564
- const early = startAfterAuthorize(request, admitted);
565
- try {
566
- return await runAdmittedTurn(request, ctx, admitted, early, progress, startedIntent);
567
- } finally {
568
- const freed = early.release();
569
- if (ctx?.waitUntil) ctx.waitUntil(freed);
570
- else void freed;
571
- }
572
- }
573
- async function runAdmittedTurn(request, ctx, admitted, early, progress, startedIntent) {
574
- const { parsed, auth, trace } = admitted;
575
- const intentLine = startedIntent ?? startIntentLine(admitted);
576
- const { payload, content, fileParts, mentions, turnId } = parsed;
577
- const { tenantId, userId, context } = auth;
578
- if (options.turnLock?.warm) {
579
- const warmSpan = trace.start("admission.lock_warm", "admission");
580
- const warmFailed = (err) => {
581
- warmSpan.end(void 0, err);
582
- log("[chat-routes] turnLock.warm failed", { error: err instanceof Error ? err.message : String(err) });
583
- };
584
- try {
585
- void Promise.resolve(options.turnLock.warm({ tenantId, userId, threadId: payload.threadId, context })).then(() => warmSpan.end(), warmFailed);
586
- } catch (err) {
587
- warmFailed(err);
499
+ warmFailed(err);
588
500
  }
589
501
  }
590
502
  const prepareTurn = options.prepareTurn;
@@ -785,10 +697,7 @@ function createChatTurnRoutes(options) {
785
697
  assistantMessageId: assistantRowId(),
786
698
  ...gatedTurn ? { gated: true } : {},
787
699
  ...producer?.model ? { model: producer.model } : {},
788
- ...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
789
- ...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
790
- ...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
791
- ...attribution?.servedSource ? { servedSource: attribution.servedSource } : {},
700
+ ...modelAttributionFields(attribution),
792
701
  ...failoverInfo ? { modelFailover: failoverInfo } : {}
793
702
  };
794
703
  if (!gatedTurn) settledTurn = completed;
@@ -1018,10 +927,7 @@ function createChatTurnRoutes(options) {
1018
927
  content: finalText,
1019
928
  ...parts && parts.length > 0 ? { parts } : {},
1020
929
  ...producer?.model ? { model: producer.model } : {},
1021
- ...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
1022
- ...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
1023
- ...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
1024
- ...attribution?.servedSource ? { servedSource: attribution.servedSource } : {},
930
+ ...modelAttributionFields(attribution),
1025
931
  ...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
1026
932
  ...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {},
1027
933
  ...usage.reasoningTokens !== void 0 ? { reasoningTokens: usage.reasoningTokens } : {},
@@ -1061,10 +967,7 @@ function createChatTurnRoutes(options) {
1061
967
  ...runFailed ? { failureReason: failureReasonOf(lastFailureData) } : {},
1062
968
  ...runFailed && failureCodeOf(lastFailureData) ? { failureCode: failureCodeOf(lastFailureData) } : {},
1063
969
  ...producer?.model ? { model: producer.model } : {},
1064
- ...attribution?.requestedModel ? { requestedModel: attribution.requestedModel } : {},
1065
- ...attribution?.servedModel ? { servedModel: attribution.servedModel } : {},
1066
- ...attribution?.servedProvider ? { servedProvider: attribution.servedProvider } : {},
1067
- ...attribution?.servedSource ? { servedSource: attribution.servedSource } : {},
970
+ ...modelAttributionFields(attribution),
1068
971
  ...failoverInfo ? { modelFailover: failoverInfo } : {}
1069
972
  }));
1070
973
  }
@@ -1143,6 +1046,225 @@ function createChatTurnRoutes(options) {
1143
1046
  exportTraceInBackground();
1144
1047
  throw err;
1145
1048
  }
1049
+ };
1050
+ }
1051
+
1052
+ // src/chat-routes/turn-routes.ts
1053
+ function createChatTurnRoutes(options) {
1054
+ const log = options.log ?? ((message, meta) => console.error(message, meta ?? ""));
1055
+ const draftStore = options.store;
1056
+ if (options.incrementalPersistence && !storeSupportsDraftPersistence(draftStore)) {
1057
+ throw new Error(
1058
+ "incrementalPersistence requires a store with updateMessage() \u2014 `/chat-store`'s createChatStore has it; a product store must implement it or omit the option"
1059
+ );
1060
+ }
1061
+ const draftTuning = options.incrementalPersistence === false || !storeSupportsDraftPersistence(draftStore) ? null : options.incrementalPersistence ?? {};
1062
+ const progressMessages = { ...TURN_ROUTE_PHASE_MESSAGES, ...options.progressMessages };
1063
+ const traceOptions = options.turnTrace === false ? void 0 : options.turnTrace;
1064
+ const traceSink = options.turnTrace === false ? void 0 : traceOptions?.sink ?? createIntelligenceTurnTraceSink({ project: options.projectId });
1065
+ function startIntentLine(admitted) {
1066
+ const intentLine = options.intentLine;
1067
+ if (!intentLine) return void 0;
1068
+ const { parsed, auth } = admitted;
1069
+ const threadId = parsed.payload.threadId;
1070
+ const controller = new AbortController();
1071
+ let timer;
1072
+ const deadline = new Promise((resolve) => {
1073
+ timer = setTimeout(() => {
1074
+ controller.abort();
1075
+ resolve(null);
1076
+ }, intentLine.timeoutMs ?? INTENT_LINE_TIMEOUT_MS);
1077
+ });
1078
+ const generated = (async () => {
1079
+ const history = (await options.store.listMessages(threadId)).slice(-INTENT_LINE_HISTORY_MESSAGES).map((message) => ({ role: message.role, content: message.content }));
1080
+ const text = await intentLine.generate({
1081
+ content: parsed.content,
1082
+ history,
1083
+ tenantId: auth.tenantId,
1084
+ userId: auth.userId,
1085
+ threadId,
1086
+ context: auth.context,
1087
+ signal: controller.signal
1088
+ });
1089
+ const line = typeof text === "string" ? text.trim().replace(/\s+/g, " ") : "";
1090
+ return line ? line.slice(0, INTENT_LINE_MAX_CHARS) : null;
1091
+ })().catch((err) => {
1092
+ if (!controller.signal.aborted) {
1093
+ log("[chat-routes] intent line failed; showing none", { error: err instanceof Error ? err.message : String(err) });
1094
+ }
1095
+ return null;
1096
+ });
1097
+ return Promise.race([generated, deadline]).finally(() => clearTimeout(timer));
1098
+ }
1099
+ const runAdmittedTurn = createAdmittedTurnRunner({
1100
+ options,
1101
+ draftStore,
1102
+ draftTuning,
1103
+ progressMessages,
1104
+ traceOptions,
1105
+ traceSink,
1106
+ startIntentLine,
1107
+ log
1108
+ });
1109
+ async function turn(request, ctx) {
1110
+ const admitted = await admitTurn(request, createTurnTrace({ d1QueryCounter: currentD1QueryCounter() }));
1111
+ if (!admitted.ok) return admitted.response;
1112
+ if (request.headers.get(TURN_PROGRESS_HEADER) !== TURN_PROGRESS_FIRST) return runTurn(request, ctx, admitted);
1113
+ return progressFirstTurn(request, ctx, admitted);
1114
+ }
1115
+ async function admitTurn(request, trace) {
1116
+ const parse = trace.start("admission.parse", "admission");
1117
+ const [rawBody, badBody] = await parseJsonObjectBody(request);
1118
+ if (badBody) return { ok: false, response: badBody };
1119
+ let parsed;
1120
+ try {
1121
+ parsed = validateTurnBody(rawBody, options.maxInlinePartBytes);
1122
+ } catch (err) {
1123
+ if (err instanceof ChatTurnInputError) return { ok: false, response: errorResponse(err) };
1124
+ throw err;
1125
+ }
1126
+ parse.end();
1127
+ const auth = await trace.measure("admission.authorize", "admission", (span) => options.authorize({ request, intent: "turn", body: parsed.payload, trace, span }));
1128
+ if (!auth.ok) return { ok: false, response: auth.response };
1129
+ const namedModel = typeof parsed.payload.model === "string" ? parsed.payload.model.trim() : "";
1130
+ const policy = options.modelPolicy;
1131
+ if (policy && namedModel) {
1132
+ const decision = await trace.measure("admission.model_policy", "admission", () => checkModelPolicy(
1133
+ namedModel,
1134
+ policy.catalog,
1135
+ policy.filter,
1136
+ policy.context
1137
+ ));
1138
+ if (!decision.allowed) {
1139
+ return { ok: false, response: Response.json({ code: "model_policy_denied", error: decision.reason }, { status: 403 }) };
1140
+ }
1141
+ }
1142
+ return { ok: true, parsed, auth, trace };
1143
+ }
1144
+ function progressFirstTurn(request, ctx, admitted) {
1145
+ const receivedAt = Date.now();
1146
+ const encoder = new TextEncoder();
1147
+ const { readable, writable } = new TransformStream();
1148
+ const writer = writable.getWriter();
1149
+ let clientGone = false;
1150
+ const write = async (chunk) => {
1151
+ if (clientGone) return;
1152
+ try {
1153
+ await writer.write(chunk);
1154
+ } catch {
1155
+ clientGone = true;
1156
+ }
1157
+ };
1158
+ const writeEvent = (event) => write(encoder.encode(`${JSON.stringify(event)}
1159
+ `));
1160
+ const progress = (phase, message) => {
1161
+ void writeEvent(buildTurnPhaseEvent(phase, message, { sinceRequestMs: Date.now() - receivedAt }));
1162
+ };
1163
+ progress("accepted", progressMessages.accepted);
1164
+ const intent = startIntentLine(admitted);
1165
+ let piping = false;
1166
+ let atLineStart = true;
1167
+ let pendingIntent = null;
1168
+ if (intent) {
1169
+ void intent.then((text) => {
1170
+ if (!text || clientGone) return;
1171
+ const event = { type: TURN_INTENT_EVENT, data: { text, sinceRequestMs: Date.now() - receivedAt } };
1172
+ if (!piping || atLineStart) void writeEvent(event);
1173
+ else pendingIntent = event;
1174
+ });
1175
+ }
1176
+ const pump = (async () => {
1177
+ let inner;
1178
+ try {
1179
+ inner = await runTurn(request, ctx, admitted, progress, intent);
1180
+ } catch (err) {
1181
+ log("[chat-routes] turn failed before it streamed", { error: err instanceof Error ? err.message : String(err) });
1182
+ inner = Response.json({ error: "The turn failed before it started. Try again." }, { status: 500 });
1183
+ }
1184
+ if (!inner.body || !inner.headers.get("x-turn-id")) {
1185
+ await writeEvent(await turnResponseEventFor(inner));
1186
+ } else {
1187
+ const reader = inner.body.getReader();
1188
+ piping = true;
1189
+ try {
1190
+ for (; ; ) {
1191
+ const { done, value } = await reader.read();
1192
+ if (done) break;
1193
+ atLineStart = value.length > 0 && value[value.length - 1] === 10;
1194
+ await write(value);
1195
+ if (atLineStart && pendingIntent) {
1196
+ const event = pendingIntent;
1197
+ pendingIntent = null;
1198
+ await writeEvent(event);
1199
+ }
1200
+ if (clientGone) {
1201
+ await reader.cancel().catch(() => {
1202
+ });
1203
+ break;
1204
+ }
1205
+ }
1206
+ } catch (err) {
1207
+ log("[chat-routes] progress-first pipe failed", { error: err instanceof Error ? err.message : String(err) });
1208
+ }
1209
+ }
1210
+ if (!clientGone) await writer.close().catch(() => {
1211
+ });
1212
+ })();
1213
+ if (ctx?.waitUntil) ctx.waitUntil(pump);
1214
+ else void pump.catch(() => {
1215
+ });
1216
+ return new Response(readable, {
1217
+ headers: {
1218
+ "Content-Type": "application/x-ndjson",
1219
+ "Cache-Control": "no-cache",
1220
+ [TURN_PROGRESS_HEADER]: TURN_PROGRESS_FIRST
1221
+ }
1222
+ });
1223
+ }
1224
+ function startAfterAuthorize(request, admitted) {
1225
+ let release;
1226
+ const { parsed, auth, trace } = admitted;
1227
+ if (options.afterAuthorize) {
1228
+ try {
1229
+ const started = options.afterAuthorize({
1230
+ request,
1231
+ body: parsed.payload,
1232
+ tenantId: auth.tenantId,
1233
+ userId: auth.userId,
1234
+ threadId: parsed.payload.threadId,
1235
+ context: auth.context,
1236
+ trace
1237
+ });
1238
+ if (typeof started === "function") release = started;
1239
+ } catch (err) {
1240
+ log("[chat-routes] afterAuthorize failed; the turn goes on without it", { error: err instanceof Error ? err.message : String(err) });
1241
+ }
1242
+ }
1243
+ return {
1244
+ handOff: () => {
1245
+ release = void 0;
1246
+ },
1247
+ release: async () => {
1248
+ const free = release;
1249
+ release = void 0;
1250
+ if (!free) return;
1251
+ try {
1252
+ await free();
1253
+ } catch (err) {
1254
+ log("[chat-routes] afterAuthorize release failed", { error: err instanceof Error ? err.message : String(err) });
1255
+ }
1256
+ }
1257
+ };
1258
+ }
1259
+ async function runTurn(request, ctx, admitted, progress, startedIntent) {
1260
+ const early = startAfterAuthorize(request, admitted);
1261
+ try {
1262
+ return await runAdmittedTurn(request, ctx, admitted, early, progress, startedIntent);
1263
+ } finally {
1264
+ const freed = early.release();
1265
+ if (ctx?.waitUntil) ctx.waitUntil(freed);
1266
+ else void freed;
1267
+ }
1146
1268
  }
1147
1269
  async function replay(request, params) {
1148
1270
  const turnId = params.turnId?.trim();
@@ -1195,101 +1317,6 @@ function createChatTurnRoutes(options) {
1195
1317
  interactions: options.interactions ? createInteractionAnswerRoute(options.interactions) : null
1196
1318
  };
1197
1319
  }
1198
- function concatStreams(streams) {
1199
- let index = 0;
1200
- let reader = null;
1201
- return new ReadableStream({
1202
- async pull(controller) {
1203
- for (; ; ) {
1204
- if (!reader) {
1205
- const next = streams[index++];
1206
- if (!next) {
1207
- controller.close();
1208
- return;
1209
- }
1210
- reader = next.getReader();
1211
- }
1212
- const { done, value } = await reader.read();
1213
- if (done) {
1214
- reader = null;
1215
- continue;
1216
- }
1217
- controller.enqueue(value);
1218
- return;
1219
- }
1220
- },
1221
- async cancel(reason) {
1222
- await reader?.cancel(reason);
1223
- for (const stream of streams.slice(index)) await stream.cancel(reason);
1224
- }
1225
- });
1226
- }
1227
- async function* abortOnSignal(source, signal) {
1228
- const iterator = source[Symbol.asyncIterator]();
1229
- try {
1230
- for (; ; ) {
1231
- const next = await nextWithAbort(iterator, signal);
1232
- if (next.done) return;
1233
- yield next.value;
1234
- }
1235
- } finally {
1236
- await closeIteratorAfterAbort(iterator);
1237
- }
1238
- }
1239
- function nextWithAbort(iterator, signal) {
1240
- if (signal.aborted) return Promise.reject(new Error("chat turn cancelled"));
1241
- return new Promise((resolve, reject) => {
1242
- let settled = false;
1243
- const cleanup = () => signal.removeEventListener("abort", onAbort);
1244
- const onAbort = () => {
1245
- if (settled) return;
1246
- settled = true;
1247
- cleanup();
1248
- reject(new Error("chat turn cancelled"));
1249
- };
1250
- signal.addEventListener("abort", onAbort, { once: true });
1251
- if (signal.aborted) {
1252
- onAbort();
1253
- return;
1254
- }
1255
- Promise.resolve().then(() => iterator.next()).then(
1256
- (result) => {
1257
- if (settled) return;
1258
- settled = true;
1259
- cleanup();
1260
- resolve(result);
1261
- },
1262
- (error) => {
1263
- if (settled) return;
1264
- settled = true;
1265
- cleanup();
1266
- reject(error);
1267
- }
1268
- );
1269
- });
1270
- }
1271
- var ABORT_CLEANUP_TIMEOUT_MS = 50;
1272
- async function closeIteratorAfterAbort(iterator) {
1273
- let closing;
1274
- try {
1275
- const result = iterator.return?.();
1276
- if (result) closing = Promise.resolve(result);
1277
- } catch {
1278
- return;
1279
- }
1280
- if (!closing) return;
1281
- let timeout;
1282
- try {
1283
- await Promise.race([
1284
- Promise.resolve(closing).catch(() => void 0),
1285
- new Promise((resolve) => {
1286
- timeout = setTimeout(resolve, ABORT_CLEANUP_TIMEOUT_MS);
1287
- })
1288
- ]);
1289
- } finally {
1290
- if (timeout !== void 0) clearTimeout(timeout);
1291
- }
1292
- }
1293
1320
 
1294
1321
  // src/chat-routes/gateway-adapter.ts
1295
1322
  async function* streamChatRouteAsSandboxEvents(options) {
@@ -1485,4 +1512,4 @@ export {
1485
1512
  createChatTurnRoutes,
1486
1513
  streamChatRouteAsSandboxEvents
1487
1514
  };
1488
- //# sourceMappingURL=chunk-PTB2B2OO.js.map
1515
+ //# sourceMappingURL=chunk-RMPPWNW7.js.map