-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathai.ts
More file actions
9999 lines (9365 loc) · 390 KB
/
Copy pathai.ts
File metadata and controls
9999 lines (9365 loc) · 390 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
accessoryAttributes,
AnyTask,
apiClientManager,
controlSubtype,
getSchemaParseFn,
headerValue,
InputStreamOncePromise,
type InputStreamOnceOptions,
type InputStreamWaitOptions,
type InputStreamWaitWithIdleTimeoutOptions,
isSchemaZodEsque,
logger,
type MachinePresetName,
ManualWaitpointPromise,
OutOfMemoryError,
sessionStreams,
type PipeStreamResult,
type RealtimeDefinedInputStream,
type RealtimeDefinedStream,
type ReadStreamOptions,
SemanticInternalAttributes,
type SendInputStreamOptions,
Task,
taskContext,
type AppendStreamOptions,
type InputStreamOnceResult,
type inferSchemaIn,
type inferSchemaOut,
type PipeStreamOptions,
type TaskIdentifier,
type TaskOptions,
type TaskSchema,
type TaskRunContext,
type TaskWithSchema,
SESSION_IN_EVENT_ID_HEADER,
TRIGGER_CONTROL_SUBTYPE,
generateJWT,
type WriterStreamOptions,
} from "@trigger.dev/core/v3";
import type {
FinishReason,
LanguageModelUsage,
ModelMessage,
Tool,
ToolSet,
UIMessage,
UIMessageChunk,
UIMessageStreamOptions,
} from "ai";
import type { ChatSnapshotV1, StreamWriteResult } from "@trigger.dev/core/v3";
// Runtime VALUES go through the ESM/CJS shim so the CJS build can `require`
// ESM-only `ai@7` (see ../imports/ai-runtime.ts).
import {
convertToModelMessages,
dynamicTool,
generateId as generateMessageId,
getToolName,
isToolUIPart,
jsonSchema,
readUIMessageStream,
tool as aiTool,
zodSchema,
} from "../imports/ai-runtime.js";
import type { JSONSchema7, Schema } from "ai";
// `ToolCallOptions` is defined locally rather than imported from `ai`: v7
// renamed/removed that export (it's `ToolExecutionOptions<CONTEXT>` now), so a
// direct import breaks on v7. This structural shape is wider than both majors'
// and reads the user-context field under both names (`experimental_context` on
// v6, `context` on v7).
type ToolCallOptions = {
toolCallId: string;
messages?: ModelMessage[];
abortSignal?: AbortSignal;
experimental_context?: unknown;
context?: unknown;
};
import { type Attributes, trace } from "@opentelemetry/api";
import { auth } from "./auth.js";
import { locals } from "./locals.js";
import { metadata } from "./metadata.js";
import type { ResolvedPrompt } from "./prompt.js";
import type { ResolvedSkill } from "./skill.js";
// Bash-skill runtime lives in `./agentSkillsRuntime.ts` (exposed as
// the `@trigger.dev/sdk/ai/skills-runtime` subpath). It's a normal
// static import — `ai.ts` is server-only by reachability now that
// browser-side primitives (PENDING_MESSAGE_INJECTED_TYPE and the
// chat-task wire types) live in `./ai-shared.ts`. Any browser bundle
// that wants those primitives imports `./ai-shared.js` directly and
// never touches `ai.ts`'s module graph, so the `node:*` builtins
// pulled in transitively here never reach a client chunk.
import { runBashInSkill, readFileInSkill } from "./agentSkillsRuntime.js";
import { streams, markChatAgentRunForStreamsWarning } from "./streams.js";
import {
sessions,
type SessionHandle,
type SessionInputChannel,
type SessionOutputChannel,
type SessionPipeStreamOptions,
type SessionSubscribeOptions,
} from "./sessions.js";
import { createTask } from "./shared.js";
import { ensureAiSdkTelemetry } from "./aiAutoTelemetry.js";
import { resourceCatalog, type SessionTriggerConfig } from "@trigger.dev/core/v3";
import { tracer } from "./tracer.js";
/** Re-export for typing `ctx` in `chat.agent` hooks without importing `@trigger.dev/core`. */
export type { TaskRunContext } from "@trigger.dev/core/v3";
const METADATA_KEY = "tool.execute.options";
/**
* Wrapper around `convertToModelMessages` that always passes
* `ignoreIncompleteToolCalls: true` to prevent failures from
* stopped/aborted conversations with partial tool parts.
*/
function toModelMessages(messages: UIMessage[]): Promise<ModelMessage[]> {
// Pass the resolved per-turn `tools` (if any) so the AI SDK can look up each
// tool's `toModelOutput` and re-apply it to prior-turn tool results. Without
// `tools` it falls back to JSON-stringifying the raw output (TRI-10149). The
// conditional spread keeps the options object byte-identical to the no-tools
// path when nothing was declared.
const tools = locals.get(chatResolvedToolsKey);
return convertToModelMessages(messages, {
ignoreIncompleteToolCalls: true,
...(tools ? { tools } : {}),
});
}
export type ToolCallExecutionOptions = {
toolCallId: string;
experimental_context?: unknown;
/** v7 name for the user context (`experimental_context` on v6). */
context?: unknown;
/** Chat context — only present when the tool runs inside a chat.agent turn. */
chatId?: string;
turn?: number;
continuation?: boolean;
clientData?: unknown;
/** Serialized chat.local values from the parent run. @internal */
chatLocals?: Record<string, unknown>;
};
/** Chat context stored in locals during each chat.agent turn for auto-detection. */
type ChatTurnContext<TClientData = unknown> = {
chatId: string;
turn: number;
continuation: boolean;
clientData?: TClientData;
};
const chatTurnContextKey = locals.create<ChatTurnContext>("chat.turnContext");
/**
* Per-run slot holding the Session handle that backs this chat's `.in` /
* `.out` channels. Populated at the top of `chatAgent`'s run function from
* `payload.sessionId`; read by every module-level helper (`chatStream`,
* `messagesInput`, `stopInput`) so the chat.agent internals can remain
* the same module-level shape they were when the I/O was run-scoped.
* @internal
*/
const chatSessionHandleKey = locals.create<SessionHandle>("chat.sessionHandle");
/**
* S2 seq_num of the most recent `turn-complete` control record written by
* this worker. Read by `writeTurnCompleteChunk` to know what to trim back
* to when the next turn finishes, keeping `session.out` bounded to ~one
* turn at steady state.
*
* Seeded at boot from `ChatSnapshotV1.lastOutEventId` (which is exactly
* the previous turn-complete's seq_num). Wrapped in a mutable holder so
* `writeTurnCompleteChunk` can advance it without going through a setter.
* @internal
*/
const lastTurnCompleteSeqNumKey = locals.create<{ value: number | undefined }>(
"chat.lastTurnCompleteSeqNum"
);
/**
* Scan `session.out` for the latest `turn-complete` control record and
* return its `session-in-event-id` header value — the committed-consume
* cursor on `.in` as of that turn-complete. Used at worker boot to seed
* the `.in` subscription so already-processed user messages don't get
* replayed from S2.
*
* Implementation streams the SSE endpoint and listens for `turn-complete`
* via the transport's `onControl` callback; the data-chunk for-await is
* just there to drive the stream. The scan is O(1 turn) because
* `session.out` is bounded to roughly one turn at steady state — every
* successful turn-complete is followed by an S2 trim back to the
* previous one (see `writeTurnCompleteChunk`).
*
* Returns `undefined` if no `turn-complete` carrying the header has been
* written yet — first-turn-ever, first turn post-OOM-with-no-prior-runs,
* or a `turn-complete` written before this header existed (cross-version
* boot). Callers fall back to subscribing `.in` from seq 0 in that case;
* the slim-wire merge handles any dedup against snapshot-restored
* messages.
* @internal
*/
async function findLatestSessionInCursor(
chatId: string
): Promise<number | undefined> {
const apiClient = apiClientManager.clientOrThrow();
let latestCursor: number | undefined;
const stream = await apiClient.subscribeToSessionStream<unknown>(chatId, "out", {
// 5s rather than 1s: S2 trim is eventually-consistent (10-60s
// window), so a worker booting just after a trim could still see
// pre-trim records and need a bit longer to drain them all before
// the SSE long-poll closes. Without enough headroom the scan would
// fall back to `undefined`, the `.in` cursor wouldn't be seeded,
// and the next subscribe would replay messages already processed.
timeoutInSeconds: 5,
onControl: (event) => {
if (event.subtype !== TRIGGER_CONTROL_SUBTYPE.TURN_COMPLETE) return;
const raw = headerValue(event.headers, SESSION_IN_EVENT_ID_HEADER);
if (!raw) return;
const parsed = Number.parseInt(raw, 10);
if (Number.isFinite(parsed)) latestCursor = parsed;
},
});
// Drain the stream so the underlying SSE reader runs to completion. We
// don't accumulate chunks; `onControl` fires inline as turn-complete
// records arrive.
for await (const _ of stream) {
// intentionally empty
}
return latestCursor;
}
/**
* Versioned blob written to S3 after every turn completes (when no
* `hydrateMessages` hook is registered). Read at run boot to seed the
* accumulator with prior conversation state, replacing the old wire-borne
* full-history seed.
*
* The shape is shared with the Sessions dashboard (which reads the same
* blob to render the full conversation transcript) via
* `@trigger.dev/core/v3`. Customer code shouldn't reach in here — the
* SDK transports surface the messages through the standard `messages`
* accumulator.
*
* @internal
*/
export type { ChatSnapshotV1 } from "@trigger.dev/core/v3";
/**
* Test-only override hook — `mockChatAgent` installs a fake to return
* synthetic snapshots without hitting S3. Mirrors the `__set*ImplForTests`
* pattern in `sessions.ts`. Not part of the public API.
* @internal
*/
type ReadChatSnapshotImpl = <TUIMessage extends UIMessage>(
sessionId: string
) => Promise<ChatSnapshotV1<TUIMessage> | undefined> | ChatSnapshotV1<TUIMessage> | undefined;
let readChatSnapshotImpl: ReadChatSnapshotImpl | undefined;
export function __setReadChatSnapshotImplForTests(impl: ReadChatSnapshotImpl | undefined): void {
readChatSnapshotImpl = impl;
}
/**
* Test-only override hook — see `__setReadChatSnapshotImplForTests`. The
* mock harness records writes for assertion via this setter. Not public.
* @internal
*/
type WriteChatSnapshotImpl = <TUIMessage extends UIMessage>(
sessionId: string,
snapshot: ChatSnapshotV1<TUIMessage>
) => Promise<void> | void;
let writeChatSnapshotImpl: WriteChatSnapshotImpl | undefined;
export function __setWriteChatSnapshotImplForTests(impl: WriteChatSnapshotImpl | undefined): void {
writeChatSnapshotImpl = impl;
}
/**
* Read the persisted snapshot for a session. Returns `undefined` on:
* - missing object (404 from the presigned GET — fresh session, never
* persisted)
* - presign failure (network/auth issue)
* - malformed JSON
* - version mismatch (forward-compat — older runtimes ignore newer blobs)
*
* Always swallows errors via `logger.warn`. The agent boot loop must stay
* available even if S3 hiccups; the worst case is replaying more of
* `session.out` than strictly necessary.
* @internal
*/
async function readChatSnapshot<TUIMessage extends UIMessage>(
sessionId: string
): Promise<ChatSnapshotV1<TUIMessage> | undefined> {
if (readChatSnapshotImpl) {
return (await readChatSnapshotImpl<TUIMessage>(sessionId)) ?? undefined;
}
const apiClient = apiClientManager.clientOrThrow();
let presignedUrl: string;
try {
const resp = await apiClient.getChatSnapshotUrl(sessionId);
presignedUrl = resp.presignedUrl;
} catch (error) {
logger.warn("chat.agent: snapshot presign (read) failed; continuing without snapshot", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
return undefined;
}
let response: Response;
try {
response = await fetch(presignedUrl, { method: "GET" });
} catch (error) {
logger.warn("chat.agent: snapshot fetch failed; continuing without snapshot", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
return undefined;
}
if (response.status === 404) {
// First-ever boot for this session — no snapshot yet. Caller falls
// through to replay-only.
return undefined;
}
if (!response.ok) {
logger.warn("chat.agent: snapshot fetch returned non-OK; continuing without snapshot", {
status: response.status,
sessionId,
});
return undefined;
}
let parsed: unknown;
try {
parsed = await response.json();
} catch (error) {
logger.warn("chat.agent: snapshot JSON parse failed; continuing without snapshot", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
return undefined;
}
if (!parsed || typeof parsed !== "object") return undefined;
const candidate = parsed as Partial<ChatSnapshotV1<TUIMessage>>;
if (candidate.version !== 1 || !Array.isArray(candidate.messages)) {
logger.warn("chat.agent: snapshot version/shape mismatch; ignoring", {
version: candidate.version,
sessionId,
});
return undefined;
}
return candidate as ChatSnapshotV1<TUIMessage>;
}
/**
* Persist the snapshot for a session. Awaited by callers immediately after
* `onTurnComplete` — the agent may suspend right after this point, and
* fire-and-forget promises don't reliably complete on suspend.
*
* Errors are swallowed via `logger.warn`. A failed write means the next
* boot replays slightly more of `session.out` (back to the previous
* snapshot's cursor) instead of failing — the conversation stays
* coherent, only the boot path does marginally more work.
* @internal
*/
async function writeChatSnapshot<TUIMessage extends UIMessage>(
sessionId: string,
snapshot: ChatSnapshotV1<TUIMessage>
): Promise<void> {
if (writeChatSnapshotImpl) {
await writeChatSnapshotImpl<TUIMessage>(sessionId, snapshot);
return;
}
const apiClient = apiClientManager.clientOrThrow();
let presignedUrl: string;
try {
const resp = await apiClient.createChatSnapshotUploadUrl(sessionId);
presignedUrl = resp.presignedUrl;
} catch (error) {
logger.warn("chat.agent: snapshot presign (write) failed; next run will replay further", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
return;
}
let response: Response;
try {
response = await fetch(presignedUrl, {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify(snapshot),
});
} catch (error) {
logger.warn("chat.agent: snapshot upload failed; next run will replay further", {
error: error instanceof Error ? error.message : String(error),
sessionId,
});
return;
}
if (!response.ok) {
logger.warn("chat.agent: snapshot upload returned non-OK; next run will replay further", {
status: response.status,
sessionId,
});
}
}
/**
* Test-only entry point that bypasses `__setReadChatSnapshotImplForTests`
* and reaches the real `apiClient.getPayloadUrl` + `fetch` + JSON-parse path.
* Used by `chat-snapshot.test.ts` to verify 404 / 500 / malformed JSON /
* version-mismatch / network-error behavior end-to-end. Tests mock global
* `fetch` and the api-client config; this wrapper lets them drive the
* production code without the override hook short-circuiting.
*
* Not part of the public API. The `__` prefix and `ForTests` suffix mirror
* the override-hook setters above.
* @internal
*/
export async function __readChatSnapshotProductionPathForTests<TUIMessage extends UIMessage>(
sessionId: string
): Promise<ChatSnapshotV1<TUIMessage> | undefined> {
const saved = readChatSnapshotImpl;
readChatSnapshotImpl = undefined;
try {
return await readChatSnapshot<TUIMessage>(sessionId);
} finally {
readChatSnapshotImpl = saved;
}
}
/**
* Test-only entry point that bypasses `__setWriteChatSnapshotImplForTests`
* and reaches the real `apiClient.createUploadPayloadUrl` + `fetch` PUT
* path. Pairs with `__readChatSnapshotProductionPathForTests` — see that
* function's note for the rationale.
*
* Not part of the public API.
* @internal
*/
export async function __writeChatSnapshotProductionPathForTests<TUIMessage extends UIMessage>(
sessionId: string,
snapshot: ChatSnapshotV1<TUIMessage>
): Promise<void> {
const saved = writeChatSnapshotImpl;
writeChatSnapshotImpl = undefined;
try {
await writeChatSnapshot<TUIMessage>(sessionId, snapshot);
} finally {
writeChatSnapshotImpl = saved;
}
}
/**
* Merge two `UIMessage[]` lists by `id`, with the second list winning on
* collision. Used at run boot to combine the snapshot's persisted history
* with the replayed `session.out` tail — replay produces the freshest
* representation of any assistant message that landed after the snapshot's
* cursor, so it should overwrite the older copy from the snapshot.
*
* Order: items unique to `a` keep their original positions; items unique to
* `b` are appended at the end in their `b` order; collisions take `b`'s
* value but keep the position they had in `a`.
*
* @internal
*/
function mergeByIdReplaceWins<TUIMessage extends UIMessage>(
a: TUIMessage[],
b: TUIMessage[]
): TUIMessage[] {
if (b.length === 0) return [...a];
if (a.length === 0) return [...b];
const indexById = new Map<string, number>();
for (let i = 0; i < a.length; i++) {
const id = a[i]!.id;
if (typeof id === "string" && id.length > 0) indexById.set(id, i);
}
const result = [...a];
for (const next of b) {
const id = next.id;
if (typeof id === "string" && id.length > 0 && indexById.has(id)) {
result[indexById.get(id)!] = next;
} else {
const newIdx = result.length;
result.push(next);
if (typeof id === "string" && id.length > 0) indexById.set(id, newIdx);
}
}
return result;
}
/**
* Test-only entry point for `mergeByIdReplaceWins`. The merge helper is the
* one piece of slim-wire boot logic that's purely functional, so it earns a
* direct unit test that exercises empty inputs, id collisions, no-id append,
* order preservation, and the replay-wins-on-collision invariant. Mirrors
* the `__*ProductionPathForTests` pattern used for the snapshot/replay
* helpers above.
*
* Not part of the public API.
* @internal
*/
export function __mergeByIdReplaceWinsForTests<TUIMessage extends UIMessage>(
a: TUIMessage[],
b: TUIMessage[]
): TUIMessage[] {
return mergeByIdReplaceWins<TUIMessage>(a, b);
}
/**
* Test-only override hook — `mockChatAgent` installs a fake replay that
* returns a synthetic `UIMessage[]` so unit tests can drive the boot loop
* without an SSE subscription. Mirrors the snapshot setters above. Not
* part of the public API.
* @internal
*/
type ReplaySessionOutTailResult<TUIMessage extends UIMessage> = {
/** Messages whose `finish` chunk landed before the run died. Safe to seed the chain. */
settled: TUIMessage[];
/**
* The trailing assistant message whose `finish` chunk never arrived —
* an orphan from a cancel / crash / OOM. `cleanupAbortedParts` has
* already stripped streaming-in-progress fragments. `undefined` if
* the tail ended cleanly (every segment closed).
*/
partial: TUIMessage | undefined;
/**
* The trailing assistant message BEFORE `cleanupAbortedParts` ran. Same
* `undefined` semantics as `partial`. Use this when you need to inspect
* tool parts the cleanup would strip (e.g. `input-available` /
* `input-streaming` orphans surfaced via `pendingToolCalls`).
*/
partialRaw: TUIMessage | undefined;
};
type ReplaySessionOutTailImpl = <TUIMessage extends UIMessage>(
sessionId: string,
options?: { lastEventId?: string }
) => Promise<ReplaySessionOutTailResult<TUIMessage>>;
let replaySessionOutTailImpl: ReplaySessionOutTailImpl | undefined;
export function __setReplaySessionOutTailImplForTests(
impl: ReplaySessionOutTailImpl | undefined
): void {
replaySessionOutTailImpl = impl;
}
/**
* Drain `session.out` from `lastEventId` (or the start) and reduce the
* remaining `UIMessageChunk`s back into `UIMessage[]`. Used at run boot to
* catch any chunks that landed AFTER the last persisted snapshot — typically
* the chunks from the turn whose `onTurnComplete` ran but whose snapshot
* write didn't make it to S3 before the run crashed / suspended.
*
* Implementation:
* 1. `apiClient.readSessionStreamRecords` — non-SSE, `wait=0` drain.
* Returns immediately with whatever records exist after the cursor.
* The previous SSE-subscribe path paid a fixed ~1s long-poll tax on
* every fresh chat (timeout duration on empty streams) — unacceptable
* for the first-message TTFC budget.
* 2. Filter out the agent's control chunks (`type: "trigger:*"`) — they
* ride on the same stream as the user-visible UIMessageChunks.
* 3. Split chunks at `start`/`finish` boundaries so each segment is a
* single message, then feed each segment through the AI SDK's
* `readUIMessageStream` reducer (the same one `useChat` uses on the
* browser side) and grab the final emitted snapshot.
* 4. The trailing message — if it never received a `finish` chunk —
* goes through `cleanupAbortedParts` so partial in-flight parts
* don't leak into the next turn's accumulator. Drop it entirely
* if cleanup empties it.
*
* Errors are propagated to the caller (the boot loop wraps in try/catch and
* `logger.warn`s); we don't swallow here so test code can observe failures
* directly.
* @internal
*/
async function replaySessionOutTail<TUIMessage extends UIMessage>(
sessionId: string,
options?: { lastEventId?: string }
): Promise<ReplaySessionOutTailResult<TUIMessage>> {
if (replaySessionOutTailImpl) {
return await replaySessionOutTailImpl<TUIMessage>(sessionId, options);
}
const apiClient = apiClientManager.clientOrThrow();
const response = await apiClient.readSessionStreamRecords(sessionId, "out", {
afterEventId: options?.lastEventId,
});
const collected: UIMessageChunk[] = [];
for (const record of response.records) {
// `data` is the chunk object as written by the SDK's session-out
// writer (an AI SDK `UIMessageChunk` or a Trigger control object).
// The route forwards it as-is — no JSON envelope to unwrap here.
// Defensive shape checks below tolerate malformed records by
// skipping them instead of throwing.
const chunk: unknown = record.data;
if (!chunk || typeof chunk !== "object") continue;
const type = (chunk as { type?: unknown }).type;
if (typeof type !== "string") continue;
// Drop agent control chunks (`trigger:turn-complete`, `trigger:upgrade-required`,
// session-state telemetry, etc.). They ride the same stream but aren't part
// of the UIMessageChunk discriminated union and would confuse the reducer.
if (type.startsWith("trigger:")) continue;
collected.push(chunk as UIMessageChunk);
}
if (collected.length === 0) return { settled: [], partial: undefined, partialRaw: undefined };
// Split chunks into per-message segments. A `start` chunk demarcates the
// beginning of an assistant message; chunks before any `start` (rare —
// but possible if the stream begins mid-message after a resume) get
// bundled into a leading "implicit" segment so we don't drop them silently.
type Segment = { chunks: UIMessageChunk[]; closed: boolean };
const segments: Segment[] = [];
let current: Segment | undefined;
for (const chunk of collected) {
if (chunk.type === "start") {
current = { chunks: [chunk], closed: false };
segments.push(current);
continue;
}
if (!current) {
// Chunk arrived before any `start`. Synthesize a segment so the reducer
// has something to work with — `readUIMessageStream` tolerates a missing
// `start` because we pass `message: undefined`.
current = { chunks: [], closed: false };
segments.push(current);
}
current.chunks.push(chunk);
if (chunk.type === "finish") {
current.closed = true;
current = undefined;
}
}
const settled: TUIMessage[] = [];
let partial: TUIMessage | undefined;
let partialRaw: TUIMessage | undefined;
for (let i = 0; i < segments.length; i++) {
const seg = segments[i]!;
const isTrailing = i === segments.length - 1 && !seg.closed;
const segmentStream = new ReadableStream<UIMessageChunk>({
start(controller) {
for (const c of seg.chunks) controller.enqueue(c);
controller.close();
},
});
let last: UIMessage | undefined;
try {
for await (const snapshot of readUIMessageStream({ stream: segmentStream })) {
last = snapshot;
}
} catch (error) {
// Reducer error — the segment is malformed. Skip it and keep going so a
// single corrupt chunk doesn't sink the entire replay.
logger.warn("chat.agent: replay reducer failed for segment; skipping", {
sessionId,
segmentIndex: i,
error: error instanceof Error ? error.message : String(error),
});
continue;
}
if (!last) continue;
if (isTrailing) {
const cleaned = cleanupAbortedParts(last as TUIMessage);
if (cleaned.parts.length === 0) continue;
partial = cleaned;
// Keep the raw pre-cleanup message too — recovery boot extracts
// `pendingToolCalls` from it, since `cleanupAbortedParts` strips
// exactly the input-streaming / input-available tool parts that
// we want to surface.
partialRaw = last as TUIMessage;
} else {
settled.push(last as TUIMessage);
}
}
return { settled, partial, partialRaw };
}
/**
* Test-only entry point that bypasses `__setReplaySessionOutTailImplForTests`
* and reaches the real `apiClient.subscribeToSessionStream` + chunk-segment
* splitter + `readUIMessageStream` reducer. Pairs with the snapshot
* production-path wrappers above. Lets `replay-session-out.test.ts` drive
* synthetic chunk sequences through the real reducer to lock down chunk-
* stream → `UIMessage[]` correctness — if the AI SDK's chunk semantics
* shift in a future version, the test catches it before customers do.
*
* Tests should mock `apiClient.subscribeToSessionStream` (e.g. via
* `vi.spyOn(apiClient, ...)`) to feed a `ReadableStream<UIMessageChunk>`.
*
* Not part of the public API.
* @internal
*/
export async function __replaySessionOutTailProductionPathForTests<
TUIMessage extends UIMessage,
>(
sessionId: string,
options?: { lastEventId?: string }
): Promise<TUIMessage[]> {
const saved = replaySessionOutTailImpl;
replaySessionOutTailImpl = undefined;
try {
const { settled, partial } = await replaySessionOutTail<TUIMessage>(sessionId, options);
return partial !== undefined ? [...settled, partial] : settled;
} finally {
replaySessionOutTailImpl = saved;
}
}
/**
* Test-only override hook for `replaySessionInTail`. Mirrors
* `__setReplaySessionOutTailImplForTests` so unit tests can drive the boot
* loop's chain-reconstruction logic without an HTTP round-trip.
* @internal
*/
type ReplaySessionInTailImpl = <TUIMessage extends UIMessage>(
sessionId: string,
options?: { lastEventId?: string }
) => Promise<{ message: TUIMessage; metadata: unknown; seqNum: number }[]>;
let replaySessionInTailImpl: ReplaySessionInTailImpl | undefined;
export function __setReplaySessionInTailImplForTests(
impl: ReplaySessionInTailImpl | undefined
): void {
replaySessionInTailImpl = impl;
}
/**
* Drain `session.in` from `lastEventId` (or the start) and surface the user
* messages that landed past the cursor. Mirror of `replaySessionOutTail` —
* both reads run at continuation boot so the SDK can reconstruct
* conversational order across a dead run that never wrote `onTurnComplete`.
*
* `session.in` carries the {@link ChatInputChunk} tagged union:
* - `kind: "message"` — a `ChatTaskWirePayload` envelope for a new user
* message (`trigger: "submit-message"`) or a regeneration. Only the
* submit-message records carry a `payload.message`; regenerations,
* preload / close / action / handover-prepare have no message.
* - `kind: "stop"` — mid-turn cancellation signal. Not a message.
* - `kind: "handover"` / `kind: "handover-skip"` — head-start signals.
* Not user messages.
*
* This function filters to the first variant and returns the embedded
* `UIMessage`s in seq_num order, paired with their seq_num so the caller
* can advance the session.in cursor past them.
*
* Errors are propagated to the caller (the boot loop wraps in try/catch
* and `logger.warn`s); we don't swallow here so test code can observe
* failures directly.
* @internal
*/
async function replaySessionInTail<TUIMessage extends UIMessage>(
sessionId: string,
options?: { lastEventId?: string }
): Promise<{ message: TUIMessage; metadata: unknown; seqNum: number }[]> {
if (replaySessionInTailImpl) {
return await replaySessionInTailImpl<TUIMessage>(sessionId, options);
}
const apiClient = apiClientManager.clientOrThrow();
const response = await apiClient.readSessionStreamRecords(sessionId, "in", {
afterEventId: options?.lastEventId,
});
const out: { message: TUIMessage; metadata: unknown; seqNum: number }[] = [];
for (const record of response.records) {
// session.in writers POST `JSON.stringify(chunk)` directly; the
// webapp wraps that in `{ data: <string>, id }` and stores it on
// S2. The records endpoint hands `data` back as the original
// string — unlike session.out (where the writer puts a chunk
// OBJECT into the envelope and the route forwards it as an
// object). Defensive: handle both shapes so future writer changes
// on either side don't silently lose records.
let chunk: unknown = record.data;
if (typeof chunk === "string") {
try {
chunk = JSON.parse(chunk);
} catch {
continue;
}
}
if (!chunk || typeof chunk !== "object") continue;
const kind = (chunk as { kind?: unknown }).kind;
if (kind !== "message") continue;
const payload = (
chunk as {
payload?: { trigger?: unknown; message?: unknown; metadata?: unknown };
}
).payload;
if (!payload || payload.trigger !== "submit-message") continue;
const message = payload.message;
if (!message || typeof message !== "object") continue;
out.push({
message: message as TUIMessage,
metadata: payload.metadata,
seqNum: record.seqNum,
});
}
return out;
}
/**
* Test-only entry point that bypasses
* `__setReplaySessionInTailImplForTests` and reaches the real
* `apiClient.readSessionStreamRecords` + filter pipeline. Mirrors
* `__replaySessionOutTailProductionPathForTests`.
* @internal
*/
export async function __replaySessionInTailProductionPathForTests<
TUIMessage extends UIMessage,
>(
sessionId: string,
options?: { lastEventId?: string }
): Promise<{ message: TUIMessage; metadata: unknown; seqNum: number }[]> {
const saved = replaySessionInTailImpl;
replaySessionInTailImpl = undefined;
try {
return await replaySessionInTail<TUIMessage>(sessionId, options);
} finally {
replaySessionInTailImpl = saved;
}
}
/**
* Resolve the Session handle for the current chat.agent run.
*
* Two contexts populate this:
* 1. Inside a `chat.agent` run — `locals.chatSessionHandleKey` is set
* at boot (lines 4665 / 4760).
* 2. Inside an `ai.toolExecute` subtask — the tool wrapper threads the
* parent's `chatId` through tool metadata under `METADATA_KEY`. We
* lazily open the session from that chatId here so subtasks can use
* `chat.stream.writer({ target: "root" })` to write back to the
* parent's chat session without any wiring.
*
* Throws if neither is available.
* @internal
*/
function getChatSession(): SessionHandle {
let handle = locals.get(chatSessionHandleKey);
if (handle) return handle;
// Fallback: subtask context. The parent threaded chatId via tool metadata.
const toolMeta = metadata.get(METADATA_KEY) as ToolCallExecutionOptions | undefined;
if (toolMeta?.chatId) {
handle = sessions.open(toolMeta.chatId);
locals.set(chatSessionHandleKey, handle);
return handle;
}
throw new Error(
"chat.agent session handle is not initialized. This indicates a chat.agent helper was used outside of a chat.agent run, or the transport did not send a sessionId."
);
}
/**
* Stamp `gen_ai.conversation.id` on the active span at chat-run boot.
* The run-level span is already alive when the run callback fires, so
* `TaskContextSpanProcessor.onStart` (which stamps subsequent spans
* automatically) won't catch it — set explicitly here.
*/
function stampConversationIdOnActiveSpan(
conversationId: string | undefined,
span = trace.getActiveSpan()
): void {
if (!span || !conversationId) return;
span.setAttribute(SemanticInternalAttributes.GEN_AI_CONVERSATION_ID, conversationId);
}
type ToolResultContent = Array<
| {
type: "text";
text: string;
}
| {
type: "image";
data: string;
mimeType?: string;
}
>;
export type ToolOptions<TResult> = {
experimental_toToolResultContent?: (result: TResult) => ToolResultContent;
};
/** Satisfies AI SDK `ToolSet` index signature alongside concrete `Tool` input/output types. */
type ToolSetCompatible<T extends Tool<any, any>> = T & NonNullable<ToolSet[string]>;
function assertTaskUsableAsTool(task: AnyTask): void {
if (("schema" in task && !task.schema) || ("jsonSchema" in task && !task.jsonSchema)) {
throw new Error(
"Cannot convert this task to to a tool because the task has no schema. Make sure to either use schemaTask or a task with an input jsonSchema."
);
}
}
/**
* Shared implementation: run a task as a tool invocation (`triggerAndSubscribe` + tool metadata).
* Used by {@link toolExecute} and the deprecated `ai.tool()` wrapper.
*/
function createTaskToolExecuteHandler<
TIdentifier extends string,
TTaskSchema extends TaskSchema | undefined = undefined,
TInput = void,
TOutput = unknown,
>(
task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput> | Task<TIdentifier, TInput, TOutput>
): (input: unknown, toolOpts: ToolCallOptions | undefined) => Promise<TOutput> {
assertTaskUsableAsTool(task);
return async function taskToolExecuteHandler(
input: unknown,
toolOpts: ToolCallOptions | undefined
): Promise<TOutput> {
const toolMeta: ToolCallExecutionOptions = {
toolCallId: toolOpts?.toolCallId ?? "",
};
// v6 passes user context as `experimental_context`, v7 as `context`. Read
// whichever is set and stamp both so subtasks reading either name work.
const toolContext = toolOpts?.context ?? toolOpts?.experimental_context;
if (toolContext !== undefined) {
try {
const serialized = JSON.parse(JSON.stringify(toolContext));
toolMeta.experimental_context = serialized;
toolMeta.context = serialized;
} catch {
/* non-serializable */
}
}
const chatCtx = locals.get(chatTurnContextKey);
if (chatCtx) {
toolMeta.chatId = chatCtx.chatId;
toolMeta.turn = chatCtx.turn;
toolMeta.continuation = chatCtx.continuation;
toolMeta.clientData = chatCtx.clientData;
}
const chatLocals: Record<string, unknown> = {};
for (const entry of chatLocalRegistry) {
const value = locals.get(entry.key);
if (value !== undefined) {
chatLocals[entry.id] = value;
}
}
if (Object.keys(chatLocals).length > 0) {
toolMeta.chatLocals = chatLocals;
}
return await task
.triggerAndSubscribe(input as inferSchemaIn<TTaskSchema>, {
metadata: {
[METADATA_KEY]: toolMeta as any,
},
tags: toolOpts?.toolCallId ? [`toolCallId:${toolOpts.toolCallId}`] : undefined,
signal: toolOpts?.abortSignal,
})
.unwrap();
};
}
/**
* Returns an `execute` function for the AI SDK `tool()` helper (or any compatible tool definition).
* Preferred API for task-backed tools: the same Trigger wiring as the deprecated `ai.tool()`
* (`triggerAndSubscribe`, tool-call metadata, chat context, `chat.local` serialization) without
* building the tool object. You supply `description`, `inputSchema`, and any AI-SDK-only options
* (e.g. `experimental_toToolResultContent`) on `tool()` yourself.
*
* @example
* ```ts
* import { tool } from "ai";
* import { z } from "zod";
* import { ai } from "@trigger.dev/sdk/ai";
* import { myTask } from "./trigger/myTask";
*
* export const myTool = tool({
* description: myTask.description ?? "",
* inputSchema: z.object({ id: z.string() }),
* execute: ai.toolExecute(myTask),
* });
* ```
*/
function toolExecute<TIdentifier extends string, TInput = void, TOutput = unknown>(
task: Task<TIdentifier, TInput, TOutput>
): (input: TInput, toolOpts: ToolCallOptions) => Promise<TOutput>;
function toolExecute<
TIdentifier extends string,
TTaskSchema extends TaskSchema | undefined = undefined,
TOutput = unknown,
>(
task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput>
): (input: inferSchemaIn<TTaskSchema>, toolOpts: ToolCallOptions) => Promise<TOutput>;
function toolExecute<
TIdentifier extends string,
TTaskSchema extends TaskSchema | undefined = undefined,
TInput = void,
TOutput = unknown,
>(
task: TaskWithSchema<TIdentifier, TTaskSchema, TOutput> | Task<TIdentifier, TInput, TOutput>
): (
input: TTaskSchema extends TaskSchema ? inferSchemaIn<TTaskSchema> : TInput,
toolOpts: ToolCallOptions
) => Promise<TOutput> {
return createTaskToolExecuteHandler(task) as (
input: TTaskSchema extends TaskSchema ? inferSchemaIn<TTaskSchema> : TInput,
toolOpts: ToolCallOptions