@trigger.dev/sdk 4.4.5 → 4.5.0-rc.0

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.
Files changed (161) hide show
  1. package/dist/commonjs/v3/agentSkillsRuntime.d.ts +28 -0
  2. package/dist/commonjs/v3/agentSkillsRuntime.js +163 -0
  3. package/dist/commonjs/v3/agentSkillsRuntime.js.map +1 -0
  4. package/dist/commonjs/v3/ai-shared.d.ts +173 -0
  5. package/dist/commonjs/v3/ai-shared.js +25 -0
  6. package/dist/commonjs/v3/ai-shared.js.map +1 -0
  7. package/dist/commonjs/v3/ai.d.ts +2823 -5
  8. package/dist/commonjs/v3/ai.js +6197 -13
  9. package/dist/commonjs/v3/ai.js.map +1 -1
  10. package/dist/commonjs/v3/auth.d.ts +9 -0
  11. package/dist/commonjs/v3/auth.js.map +1 -1
  12. package/dist/commonjs/v3/chat-client.d.ts +301 -0
  13. package/dist/commonjs/v3/chat-client.js +624 -0
  14. package/dist/commonjs/v3/chat-client.js.map +1 -0
  15. package/dist/commonjs/v3/chat-react.d.ts +155 -0
  16. package/dist/commonjs/v3/chat-react.js +330 -0
  17. package/dist/commonjs/v3/chat-react.js.map +1 -0
  18. package/dist/commonjs/v3/chat-server.d.ts +206 -0
  19. package/dist/commonjs/v3/chat-server.js +737 -0
  20. package/dist/commonjs/v3/chat-server.js.map +1 -0
  21. package/dist/commonjs/v3/chat-server.test.d.ts +1 -0
  22. package/dist/commonjs/v3/chat-server.test.js +518 -0
  23. package/dist/commonjs/v3/chat-server.test.js.map +1 -0
  24. package/dist/commonjs/v3/chat-tab-coordinator.d.ts +65 -0
  25. package/dist/commonjs/v3/chat-tab-coordinator.js +235 -0
  26. package/dist/commonjs/v3/chat-tab-coordinator.js.map +1 -0
  27. package/dist/commonjs/v3/chat-tab-coordinator.test.d.ts +1 -0
  28. package/dist/commonjs/v3/chat-tab-coordinator.test.js +140 -0
  29. package/dist/commonjs/v3/chat-tab-coordinator.test.js.map +1 -0
  30. package/dist/commonjs/v3/chat.d.ts +437 -0
  31. package/dist/commonjs/v3/chat.js +968 -0
  32. package/dist/commonjs/v3/chat.js.map +1 -0
  33. package/dist/commonjs/v3/chat.test.d.ts +1 -0
  34. package/dist/commonjs/v3/chat.test.js +1180 -0
  35. package/dist/commonjs/v3/chat.test.js.map +1 -0
  36. package/dist/commonjs/v3/createStartSessionAction.test.d.ts +1 -0
  37. package/dist/commonjs/v3/createStartSessionAction.test.js +113 -0
  38. package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -0
  39. package/dist/commonjs/v3/deployments.d.ts +26 -0
  40. package/dist/commonjs/v3/deployments.js +37 -0
  41. package/dist/commonjs/v3/deployments.js.map +1 -0
  42. package/dist/commonjs/v3/index.d.ts +6 -3
  43. package/dist/commonjs/v3/index.js +7 -1
  44. package/dist/commonjs/v3/index.js.map +1 -1
  45. package/dist/commonjs/v3/runs.d.ts +22 -7
  46. package/dist/commonjs/v3/runs.js +1 -0
  47. package/dist/commonjs/v3/runs.js.map +1 -1
  48. package/dist/commonjs/v3/sessions.d.ts +228 -0
  49. package/dist/commonjs/v3/sessions.js +664 -0
  50. package/dist/commonjs/v3/sessions.js.map +1 -0
  51. package/dist/commonjs/v3/sessions.test.d.ts +1 -0
  52. package/dist/commonjs/v3/sessions.test.js +154 -0
  53. package/dist/commonjs/v3/sessions.test.js.map +1 -0
  54. package/dist/commonjs/v3/shared.d.ts +24 -2
  55. package/dist/commonjs/v3/shared.js +189 -1
  56. package/dist/commonjs/v3/shared.js.map +1 -1
  57. package/dist/commonjs/v3/skill.d.ts +99 -0
  58. package/dist/commonjs/v3/skill.js +155 -0
  59. package/dist/commonjs/v3/skill.js.map +1 -0
  60. package/dist/commonjs/v3/skills.d.ts +2 -0
  61. package/dist/commonjs/v3/skills.js +6 -0
  62. package/dist/commonjs/v3/skills.js.map +1 -0
  63. package/dist/commonjs/v3/streams.js +127 -19
  64. package/dist/commonjs/v3/streams.js.map +1 -1
  65. package/dist/commonjs/v3/tasks.d.ts +2 -1
  66. package/dist/commonjs/v3/tasks.js +1 -0
  67. package/dist/commonjs/v3/tasks.js.map +1 -1
  68. package/dist/commonjs/v3/test/index.d.ts +3 -0
  69. package/dist/commonjs/v3/test/index.js +18 -0
  70. package/dist/commonjs/v3/test/index.js.map +1 -0
  71. package/dist/commonjs/v3/test/mock-chat-agent.d.ts +259 -0
  72. package/dist/commonjs/v3/test/mock-chat-agent.js +468 -0
  73. package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -0
  74. package/dist/commonjs/v3/test/setup-catalog.d.ts +1 -0
  75. package/dist/commonjs/v3/test/setup-catalog.js +18 -0
  76. package/dist/commonjs/v3/test/setup-catalog.js.map +1 -0
  77. package/dist/commonjs/v3/test/test-session-handle.d.ts +53 -0
  78. package/dist/commonjs/v3/test/test-session-handle.js +256 -0
  79. package/dist/commonjs/v3/test/test-session-handle.js.map +1 -0
  80. package/dist/commonjs/version.js +1 -1
  81. package/dist/esm/v3/agentSkillsRuntime.d.ts +28 -0
  82. package/dist/esm/v3/agentSkillsRuntime.js +136 -0
  83. package/dist/esm/v3/agentSkillsRuntime.js.map +1 -0
  84. package/dist/esm/v3/ai-shared.d.ts +173 -0
  85. package/dist/esm/v3/ai-shared.js +22 -0
  86. package/dist/esm/v3/ai-shared.js.map +1 -0
  87. package/dist/esm/v3/ai.d.ts +2823 -5
  88. package/dist/esm/v3/ai.js +6187 -14
  89. package/dist/esm/v3/ai.js.map +1 -1
  90. package/dist/esm/v3/auth.d.ts +9 -0
  91. package/dist/esm/v3/auth.js.map +1 -1
  92. package/dist/esm/v3/chat-client.d.ts +301 -0
  93. package/dist/esm/v3/chat-client.js +619 -0
  94. package/dist/esm/v3/chat-client.js.map +1 -0
  95. package/dist/esm/v3/chat-react.d.ts +155 -0
  96. package/dist/esm/v3/chat-react.js +325 -0
  97. package/dist/esm/v3/chat-react.js.map +1 -0
  98. package/dist/esm/v3/chat-server.d.ts +206 -0
  99. package/dist/esm/v3/chat-server.js +734 -0
  100. package/dist/esm/v3/chat-server.js.map +1 -0
  101. package/dist/esm/v3/chat-server.test.d.ts +1 -0
  102. package/dist/esm/v3/chat-server.test.js +516 -0
  103. package/dist/esm/v3/chat-server.test.js.map +1 -0
  104. package/dist/esm/v3/chat-tab-coordinator.d.ts +65 -0
  105. package/dist/esm/v3/chat-tab-coordinator.js +231 -0
  106. package/dist/esm/v3/chat-tab-coordinator.js.map +1 -0
  107. package/dist/esm/v3/chat-tab-coordinator.test.d.ts +1 -0
  108. package/dist/esm/v3/chat-tab-coordinator.test.js +138 -0
  109. package/dist/esm/v3/chat-tab-coordinator.test.js.map +1 -0
  110. package/dist/esm/v3/chat.d.ts +437 -0
  111. package/dist/esm/v3/chat.js +961 -0
  112. package/dist/esm/v3/chat.js.map +1 -0
  113. package/dist/esm/v3/chat.test.d.ts +1 -0
  114. package/dist/esm/v3/chat.test.js +1178 -0
  115. package/dist/esm/v3/chat.test.js.map +1 -0
  116. package/dist/esm/v3/createStartSessionAction.test.d.ts +1 -0
  117. package/dist/esm/v3/createStartSessionAction.test.js +111 -0
  118. package/dist/esm/v3/createStartSessionAction.test.js.map +1 -0
  119. package/dist/esm/v3/deployments.d.ts +26 -0
  120. package/dist/esm/v3/deployments.js +34 -0
  121. package/dist/esm/v3/deployments.js.map +1 -0
  122. package/dist/esm/v3/index.d.ts +6 -3
  123. package/dist/esm/v3/index.js +4 -1
  124. package/dist/esm/v3/index.js.map +1 -1
  125. package/dist/esm/v3/runs.d.ts +15 -0
  126. package/dist/esm/v3/runs.js +1 -0
  127. package/dist/esm/v3/runs.js.map +1 -1
  128. package/dist/esm/v3/sessions.d.ts +228 -0
  129. package/dist/esm/v3/sessions.js +656 -0
  130. package/dist/esm/v3/sessions.js.map +1 -0
  131. package/dist/esm/v3/sessions.test.d.ts +1 -0
  132. package/dist/esm/v3/sessions.test.js +152 -0
  133. package/dist/esm/v3/sessions.test.js.map +1 -0
  134. package/dist/esm/v3/shared.d.ts +24 -2
  135. package/dist/esm/v3/shared.js +188 -1
  136. package/dist/esm/v3/shared.js.map +1 -1
  137. package/dist/esm/v3/skill.d.ts +99 -0
  138. package/dist/esm/v3/skill.js +128 -0
  139. package/dist/esm/v3/skill.js.map +1 -0
  140. package/dist/esm/v3/skills.d.ts +2 -0
  141. package/dist/esm/v3/skills.js +2 -0
  142. package/dist/esm/v3/skills.js.map +1 -0
  143. package/dist/esm/v3/streams.js +127 -20
  144. package/dist/esm/v3/streams.js.map +1 -1
  145. package/dist/esm/v3/tasks.d.ts +2 -1
  146. package/dist/esm/v3/tasks.js +2 -1
  147. package/dist/esm/v3/tasks.js.map +1 -1
  148. package/dist/esm/v3/test/index.d.ts +3 -0
  149. package/dist/esm/v3/test/index.js +13 -0
  150. package/dist/esm/v3/test/index.js.map +1 -0
  151. package/dist/esm/v3/test/mock-chat-agent.d.ts +259 -0
  152. package/dist/esm/v3/test/mock-chat-agent.js +465 -0
  153. package/dist/esm/v3/test/mock-chat-agent.js.map +1 -0
  154. package/dist/esm/v3/test/setup-catalog.d.ts +1 -0
  155. package/dist/esm/v3/test/setup-catalog.js +16 -0
  156. package/dist/esm/v3/test/setup-catalog.js.map +1 -0
  157. package/dist/esm/v3/test/test-session-handle.d.ts +53 -0
  158. package/dist/esm/v3/test/test-session-handle.js +251 -0
  159. package/dist/esm/v3/test/test-session-handle.js.map +1 -0
  160. package/dist/esm/version.js +1 -1
  161. package/package.json +87 -6
@@ -0,0 +1,206 @@
1
+ /**
2
+ * Server-side helpers for the `chat.agent` head-start flow — a
3
+ * customer's warm process (Next.js route handler, Express, etc.)
4
+ * gets the conversation moving while the heavy chat.agent run boots
5
+ * in parallel. Mid-turn, ownership of the durable stream hands over
6
+ * to the agent.
7
+ *
8
+ * The `chat.headStart({ agentId, run })` entry point returns a
9
+ * Next.js-style POST handler. Inside the customer's `run` callback
10
+ * they call `streamText` themselves, spreading
11
+ * `chat.toStreamTextOptions({ tools })` to inherit handover wiring.
12
+ * The handler runs `streamText` step 1 in the customer's process
13
+ * while the chat.agent run boots in parallel; on `tool-calls` the
14
+ * agent run picks up tool execution and continues, on pure-text the
15
+ * agent run exits clean without an LLM call.
16
+ *
17
+ * Two-layer naming: customer-facing surface is "head start"
18
+ * (describes the *benefit* — fast first-turn TTFC). The internal
19
+ * protocol still uses "handover" (describes the *mechanism* — the
20
+ * conversation hands off mid-turn from the warm process to the
21
+ * agent). Customers see `chat.headStart`, `HeadStartSession`, etc.
22
+ * The wire format and run-loop locals stay on `handover` /
23
+ * `handover-prepare` / `handover-skip`.
24
+ *
25
+ * Cooperative ordering only — handler stops writing to `session.out`
26
+ * before sending the `handover` chunk on `session.in`. No S2 fencing.
27
+ *
28
+ * ⚠️ HARD CONSTRAINT — bundle isolation
29
+ *
30
+ * This module is the customer-facing boundary for the route handler.
31
+ * The whole TTFC win comes from the customer's process being
32
+ * lightweight while the heavy agent run boots in parallel. **The
33
+ * route-handler bundle must not include heavy tool execute deps**:
34
+ * E2B, puppeteer/playwright, native bindings, the trigger SDK
35
+ * runtime, turndown, image processing libs, anything that pulls
36
+ * weight or pulls `node:` builtins.
37
+ *
38
+ * "Schema-only" tools must live in a module that imports only `ai`
39
+ * (for `tool()`) and `zod`. The agent task module imports those
40
+ * schemas and adds execute fns elsewhere — that's where the heavy
41
+ * deps live, and it's never reached by the route handler bundle.
42
+ *
43
+ * Runtime "strip executes" helpers (anything that takes a tool
44
+ * catalog with executes and removes them) DO NOT solve this. The
45
+ * import chain is resolved at bundle/build time, so importing the
46
+ * full catalog drags every dep in regardless of what the SDK does
47
+ * with the value at runtime.
48
+ *
49
+ * IMPORTANT (internal): this module must NOT import from `./ai.ts`.
50
+ * `ai.ts` statically imports `agentSkillsRuntime` (which uses `node:`
51
+ * builtins unfit for some serverless runtimes) and the heavy task
52
+ * runtime. Allowed imports: `./ai-shared.js`, `./chat-client.js`,
53
+ * `@trigger.dev/core/v3` (api client), `ai` (types + lightweight
54
+ * helpers like `stepCountIs` / `convertToModelMessages`).
55
+ */
56
+ import { type ModelMessage, type StreamTextResult, type Tool, type UIMessage, type UIMessageChunk } from "ai";
57
+ export type HeadStartRunArgs<TTools extends Record<string, Tool>> = {
58
+ /** User messages parsed from the incoming request. */
59
+ messages: UIMessage[];
60
+ /** Aborts when the request closes or the SDK times out the handover. */
61
+ signal: AbortSignal;
62
+ /** Helper exposing `toStreamTextOptions(...)` and a session escape hatch. */
63
+ chat: HeadStartChatHelper<TTools>;
64
+ };
65
+ export type HeadStartChatHelper<TTools extends Record<string, Tool>> = {
66
+ /**
67
+ * Spread into the customer's `streamText` call to inherit handover
68
+ * wiring. Returns options for:
69
+ *
70
+ * - `messages` — converted from the wire payload's UIMessages
71
+ * - `tools` — the customer's tool set (typically schema-only — see
72
+ * the bundle-isolation note in this module's header)
73
+ * - `abortSignal` — combined request-lifecycle + idle timeout
74
+ * - `stopWhen` — `stepCountIs(1)`. Step 1 only. The agent run picks
75
+ * up tool execution and step 2+ after the handover signal.
76
+ *
77
+ * Customer adds `model`, `system`, `providerOptions`, etc. on top.
78
+ * The customer keeps full control of the `streamText` call shape;
79
+ * this helper just hands back the options the SDK needs to own.
80
+ *
81
+ * The customer COULD override any of these by re-setting them after
82
+ * the spread, but doing so for `stopWhen` / `messages` /
83
+ * `abortSignal` will break the handover protocol. The intent is
84
+ * that customers spread first, then add only their own keys.
85
+ */
86
+ toStreamTextOptions<TOpts extends Record<string, unknown> = Record<string, unknown>>(opts?: {
87
+ tools?: TTools;
88
+ }): TOpts;
89
+ /** Lower-level escape hatch with manual `out` / `in` / dispatch primitives. */
90
+ session: HeadStartSession;
91
+ };
92
+ export type HeadStartSession = {
93
+ readonly chatId: string;
94
+ /**
95
+ * Tees a UIMessage stream into `session.out` for durability/resume,
96
+ * fire-and-forget. Returns a passthrough that the caller can use as
97
+ * the HTTP response body.
98
+ */
99
+ tee(stream: ReadableStream<UIMessageChunk>): ReadableStream<UIMessageChunk>;
100
+ /**
101
+ * Awaits `result.finishReason` and dispatches `handover` (with the
102
+ * partial assistant ModelMessages) or `handover-skip`.
103
+ */
104
+ handoverWhenDone(result: StreamTextResult<any, any>): Promise<void>;
105
+ /**
106
+ * Sugar over `tee` + `handoverWhenDone` + standard SSE response.
107
+ * Returns a `Response` with `Content-Type: text/event-stream` whose
108
+ * body is the teed stream.
109
+ */
110
+ handoverResponse(result: StreamTextResult<any, any>): Response;
111
+ /**
112
+ * Manually dispatch the `handover` signal on `session.in`.
113
+ *
114
+ * - `isFinal: true` — the partial assistant message IS the response.
115
+ * The agent runs `onChatStart` / `onTurnStart` / `onTurnComplete`
116
+ * against it but skips the LLM call. Use for pure-text replies.
117
+ * - `isFinal: false` — the partial assistant message ends with
118
+ * pending tool calls. The agent executes them and then runs a
119
+ * step-2 LLM call to produce the final response.
120
+ *
121
+ * `messageId` lets the caller carry a stable assistant message id
122
+ * across the handover boundary so the browser merges step 1 and
123
+ * step 2 into the same `UIMessage`.
124
+ */
125
+ handover(args: {
126
+ partialAssistantMessage: ModelMessage[];
127
+ isFinal: boolean;
128
+ messageId?: string;
129
+ }): Promise<void>;
130
+ /** Manually dispatch the `handover-skip` signal on `session.in`. */
131
+ handoverSkip(): Promise<void>;
132
+ };
133
+ export type HeadStartHandlerOptions<TTools extends Record<string, Tool>> = {
134
+ /** The `chat.agent({ id })` of the agent we're handing off to. */
135
+ agentId: string;
136
+ /**
137
+ * Customer's first-turn implementation. Receives `messages`,
138
+ * `signal`, and a `chat` helper. Should call `streamText` with
139
+ * `...chat.toStreamTextOptions({ tools })` and return the
140
+ * `StreamTextResult`.
141
+ */
142
+ run: (args: HeadStartRunArgs<TTools>) => Promise<StreamTextResult<any, any>>;
143
+ /**
144
+ * Seconds the agent run waits for the handover signal before
145
+ * exiting. Defaults to 60.
146
+ */
147
+ idleTimeoutInSeconds?: number;
148
+ };
149
+ export declare const chat: {
150
+ /**
151
+ * Returns a Next.js-style POST handler for the chat.agent
152
+ * head-start flow. Customer mounts it as
153
+ * `export const { POST } = chat.headStart({...})` (or
154
+ * `export const POST = chat.headStart({...})`).
155
+ *
156
+ * Pair with the browser transport's `headStart: "/api/chat"`
157
+ * option so the first message of a brand-new chat lands here
158
+ * before the agent run boots.
159
+ */
160
+ headStart<TTools extends Record<string, Tool>>(opts: HeadStartHandlerOptions<TTools>): (req: Request) => Promise<Response>;
161
+ /**
162
+ * Lower-level primitive for power users who want to call
163
+ * `streamText` themselves outside the `run` callback shape — custom
164
+ * transforms, non-AI-SDK code paths, or manual control over the
165
+ * response. Same wiring `chat.headStart` builds on internally.
166
+ */
167
+ openSession(opts: {
168
+ req: Request;
169
+ agentId: string;
170
+ idleTimeoutInSeconds?: number;
171
+ }): Promise<HeadStartSession>;
172
+ /**
173
+ * Wrap a Web Fetch handler — `(req: Request) => Promise<Response>` —
174
+ * as a Node `http` listener — `(req: IncomingMessage, res: ServerResponse) => Promise<void>`.
175
+ *
176
+ * Use this to mount `chat.headStart` (or any other Web Fetch
177
+ * handler) inside Node-only frameworks like Express, Fastify, Koa,
178
+ * or raw `node:http`. Web-native frameworks (Next.js App Router,
179
+ * Hono, SvelteKit, Remix, Workers, Bun, Deno, etc.) don't need
180
+ * this — they pass `Request` objects directly.
181
+ *
182
+ * Streams the response body chunk-by-chunk to the Node response,
183
+ * so the `chat.headStart` SSE chunks reach the browser as they
184
+ * arrive (no buffering). Aborts the underlying handler if the
185
+ * client closes the connection.
186
+ *
187
+ * Type-only import of `node:http` types — no runtime dep on `node:http`,
188
+ * so this stays safe to bundle into edge / Workers builds (the
189
+ * function just won't be called there).
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * import express from "express";
194
+ * import { chat } from "@trigger.dev/sdk/chat-server";
195
+ *
196
+ * const handler = chat.headStart({
197
+ * agentId: "my-chat",
198
+ * run: async ({ chat: helper }) => streamText({ ... }),
199
+ * });
200
+ *
201
+ * const app = express();
202
+ * app.post("/api/chat", chat.toNodeListener(handler));
203
+ * ```
204
+ */
205
+ toNodeListener: typeof toNodeListener;
206
+ };