@trigger.dev/sdk 4.4.6 → 4.5.0-rc.1
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.
- package/dist/commonjs/v3/agentSkillsRuntime.d.ts +28 -0
- package/dist/commonjs/v3/agentSkillsRuntime.js +163 -0
- package/dist/commonjs/v3/agentSkillsRuntime.js.map +1 -0
- package/dist/commonjs/v3/ai-shared.d.ts +173 -0
- package/dist/commonjs/v3/ai-shared.js +25 -0
- package/dist/commonjs/v3/ai-shared.js.map +1 -0
- package/dist/commonjs/v3/ai.d.ts +2823 -5
- package/dist/commonjs/v3/ai.js +6197 -13
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/auth.d.ts +9 -0
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/chat-client.d.ts +301 -0
- package/dist/commonjs/v3/chat-client.js +624 -0
- package/dist/commonjs/v3/chat-client.js.map +1 -0
- package/dist/commonjs/v3/chat-react.d.ts +155 -0
- package/dist/commonjs/v3/chat-react.js +330 -0
- package/dist/commonjs/v3/chat-react.js.map +1 -0
- package/dist/commonjs/v3/chat-server.d.ts +206 -0
- package/dist/commonjs/v3/chat-server.js +737 -0
- package/dist/commonjs/v3/chat-server.js.map +1 -0
- package/dist/commonjs/v3/chat-server.test.d.ts +1 -0
- package/dist/commonjs/v3/chat-server.test.js +518 -0
- package/dist/commonjs/v3/chat-server.test.js.map +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.d.ts +65 -0
- package/dist/commonjs/v3/chat-tab-coordinator.js +235 -0
- package/dist/commonjs/v3/chat-tab-coordinator.js.map +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.d.ts +1 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.js +140 -0
- package/dist/commonjs/v3/chat-tab-coordinator.test.js.map +1 -0
- package/dist/commonjs/v3/chat.d.ts +437 -0
- package/dist/commonjs/v3/chat.js +968 -0
- package/dist/commonjs/v3/chat.js.map +1 -0
- package/dist/commonjs/v3/chat.test.d.ts +1 -0
- package/dist/commonjs/v3/chat.test.js +1180 -0
- package/dist/commonjs/v3/chat.test.js.map +1 -0
- package/dist/commonjs/v3/createStartSessionAction.test.d.ts +1 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js +113 -0
- package/dist/commonjs/v3/createStartSessionAction.test.js.map +1 -0
- package/dist/commonjs/v3/deployments.d.ts +26 -0
- package/dist/commonjs/v3/deployments.js +37 -0
- package/dist/commonjs/v3/deployments.js.map +1 -0
- package/dist/commonjs/v3/index.d.ts +6 -3
- package/dist/commonjs/v3/index.js +7 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/runs.d.ts +22 -7
- package/dist/commonjs/v3/runs.js +1 -0
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/v3/sessions.d.ts +228 -0
- package/dist/commonjs/v3/sessions.js +664 -0
- package/dist/commonjs/v3/sessions.js.map +1 -0
- package/dist/commonjs/v3/sessions.test.d.ts +1 -0
- package/dist/commonjs/v3/sessions.test.js +154 -0
- package/dist/commonjs/v3/sessions.test.js.map +1 -0
- package/dist/commonjs/v3/shared.d.ts +24 -2
- package/dist/commonjs/v3/shared.js +189 -1
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/skill.d.ts +99 -0
- package/dist/commonjs/v3/skill.js +155 -0
- package/dist/commonjs/v3/skill.js.map +1 -0
- package/dist/commonjs/v3/skills.d.ts +2 -0
- package/dist/commonjs/v3/skills.js +6 -0
- package/dist/commonjs/v3/skills.js.map +1 -0
- package/dist/commonjs/v3/streams.js +127 -19
- package/dist/commonjs/v3/streams.js.map +1 -1
- package/dist/commonjs/v3/tasks.d.ts +2 -1
- package/dist/commonjs/v3/tasks.js +1 -0
- package/dist/commonjs/v3/tasks.js.map +1 -1
- package/dist/commonjs/v3/test/index.d.ts +3 -0
- package/dist/commonjs/v3/test/index.js +18 -0
- package/dist/commonjs/v3/test/index.js.map +1 -0
- package/dist/commonjs/v3/test/mock-chat-agent.d.ts +259 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js +468 -0
- package/dist/commonjs/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/commonjs/v3/test/setup-catalog.d.ts +1 -0
- package/dist/commonjs/v3/test/setup-catalog.js +18 -0
- package/dist/commonjs/v3/test/setup-catalog.js.map +1 -0
- package/dist/commonjs/v3/test/test-session-handle.d.ts +53 -0
- package/dist/commonjs/v3/test/test-session-handle.js +256 -0
- package/dist/commonjs/v3/test/test-session-handle.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/agentSkillsRuntime.d.ts +28 -0
- package/dist/esm/v3/agentSkillsRuntime.js +136 -0
- package/dist/esm/v3/agentSkillsRuntime.js.map +1 -0
- package/dist/esm/v3/ai-shared.d.ts +173 -0
- package/dist/esm/v3/ai-shared.js +22 -0
- package/dist/esm/v3/ai-shared.js.map +1 -0
- package/dist/esm/v3/ai.d.ts +2823 -5
- package/dist/esm/v3/ai.js +6187 -14
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/auth.d.ts +9 -0
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/chat-client.d.ts +301 -0
- package/dist/esm/v3/chat-client.js +619 -0
- package/dist/esm/v3/chat-client.js.map +1 -0
- package/dist/esm/v3/chat-react.d.ts +155 -0
- package/dist/esm/v3/chat-react.js +325 -0
- package/dist/esm/v3/chat-react.js.map +1 -0
- package/dist/esm/v3/chat-server.d.ts +206 -0
- package/dist/esm/v3/chat-server.js +734 -0
- package/dist/esm/v3/chat-server.js.map +1 -0
- package/dist/esm/v3/chat-server.test.d.ts +1 -0
- package/dist/esm/v3/chat-server.test.js +516 -0
- package/dist/esm/v3/chat-server.test.js.map +1 -0
- package/dist/esm/v3/chat-tab-coordinator.d.ts +65 -0
- package/dist/esm/v3/chat-tab-coordinator.js +231 -0
- package/dist/esm/v3/chat-tab-coordinator.js.map +1 -0
- package/dist/esm/v3/chat-tab-coordinator.test.d.ts +1 -0
- package/dist/esm/v3/chat-tab-coordinator.test.js +138 -0
- package/dist/esm/v3/chat-tab-coordinator.test.js.map +1 -0
- package/dist/esm/v3/chat.d.ts +437 -0
- package/dist/esm/v3/chat.js +961 -0
- package/dist/esm/v3/chat.js.map +1 -0
- package/dist/esm/v3/chat.test.d.ts +1 -0
- package/dist/esm/v3/chat.test.js +1178 -0
- package/dist/esm/v3/chat.test.js.map +1 -0
- package/dist/esm/v3/createStartSessionAction.test.d.ts +1 -0
- package/dist/esm/v3/createStartSessionAction.test.js +111 -0
- package/dist/esm/v3/createStartSessionAction.test.js.map +1 -0
- package/dist/esm/v3/deployments.d.ts +26 -0
- package/dist/esm/v3/deployments.js +34 -0
- package/dist/esm/v3/deployments.js.map +1 -0
- package/dist/esm/v3/index.d.ts +6 -3
- package/dist/esm/v3/index.js +4 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/runs.d.ts +15 -0
- package/dist/esm/v3/runs.js +1 -0
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/v3/sessions.d.ts +228 -0
- package/dist/esm/v3/sessions.js +656 -0
- package/dist/esm/v3/sessions.js.map +1 -0
- package/dist/esm/v3/sessions.test.d.ts +1 -0
- package/dist/esm/v3/sessions.test.js +152 -0
- package/dist/esm/v3/sessions.test.js.map +1 -0
- package/dist/esm/v3/shared.d.ts +24 -2
- package/dist/esm/v3/shared.js +188 -1
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/skill.d.ts +99 -0
- package/dist/esm/v3/skill.js +128 -0
- package/dist/esm/v3/skill.js.map +1 -0
- package/dist/esm/v3/skills.d.ts +2 -0
- package/dist/esm/v3/skills.js +2 -0
- package/dist/esm/v3/skills.js.map +1 -0
- package/dist/esm/v3/streams.js +127 -20
- package/dist/esm/v3/streams.js.map +1 -1
- package/dist/esm/v3/tasks.d.ts +2 -1
- package/dist/esm/v3/tasks.js +2 -1
- package/dist/esm/v3/tasks.js.map +1 -1
- package/dist/esm/v3/test/index.d.ts +3 -0
- package/dist/esm/v3/test/index.js +13 -0
- package/dist/esm/v3/test/index.js.map +1 -0
- package/dist/esm/v3/test/mock-chat-agent.d.ts +259 -0
- package/dist/esm/v3/test/mock-chat-agent.js +465 -0
- package/dist/esm/v3/test/mock-chat-agent.js.map +1 -0
- package/dist/esm/v3/test/setup-catalog.d.ts +1 -0
- package/dist/esm/v3/test/setup-catalog.js +16 -0
- package/dist/esm/v3/test/setup-catalog.js.map +1 -0
- package/dist/esm/v3/test/test-session-handle.d.ts +53 -0
- package/dist/esm/v3/test/test-session-handle.js +251 -0
- package/dist/esm/v3/test/test-session-handle.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/package.json +87 -6
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @trigger.dev/sdk/chat
|
|
3
|
+
*
|
|
4
|
+
* Browser-safe module for AI SDK chat transport integration.
|
|
5
|
+
* Use this on the frontend with the AI SDK's `useChat` hook.
|
|
6
|
+
*
|
|
7
|
+
* For backend helpers (`chatAgent`, `pipeChat`), use `@trigger.dev/sdk/ai` instead.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import { useChat } from "@ai-sdk/react";
|
|
12
|
+
* import { TriggerChatTransport } from "@trigger.dev/sdk/chat";
|
|
13
|
+
*
|
|
14
|
+
* function Chat() {
|
|
15
|
+
* const { messages, sendMessage, status } = useChat({
|
|
16
|
+
* transport: new TriggerChatTransport({
|
|
17
|
+
* task: "my-chat-task",
|
|
18
|
+
* accessToken: async ({ chatId }) => fetchSessionToken(chatId),
|
|
19
|
+
* startSession: async ({ chatId, taskId }) => createChatSession({ chatId, taskId }),
|
|
20
|
+
* }),
|
|
21
|
+
* });
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
import type { ChatTransport, UIMessage, UIMessageChunk, ChatRequestOptions } from "ai";
|
|
26
|
+
/**
|
|
27
|
+
* Discriminator passed to per-endpoint `baseURL` and `fetch` callbacks.
|
|
28
|
+
*
|
|
29
|
+
* - `"in"` — `POST /realtime/v1/sessions/{chatId}/in/append` (user messages,
|
|
30
|
+
* stops, actions).
|
|
31
|
+
* - `"out"` — `GET /realtime/v1/sessions/{chatId}/out` (SSE response stream).
|
|
32
|
+
*
|
|
33
|
+
* Other endpoints (`/api/v1/sessions`, `/api/v1/auth/jwt/claims`) are reached
|
|
34
|
+
* from the server-side `chat.createStartSessionAction` and `accessToken`
|
|
35
|
+
* callback, not the transport — they accept the same callback shape on their
|
|
36
|
+
* own option objects.
|
|
37
|
+
*/
|
|
38
|
+
export type ChatTransportEndpoint = "in" | "out";
|
|
39
|
+
/** Context passed to `baseURL` and `fetch` callbacks. */
|
|
40
|
+
export type ChatTransportEndpointContext = {
|
|
41
|
+
endpoint: ChatTransportEndpoint;
|
|
42
|
+
chatId: string;
|
|
43
|
+
};
|
|
44
|
+
/** Resolver form of `baseURL` — return the base for the given endpoint. */
|
|
45
|
+
export type ChatBaseURLResolver = (ctx: ChatTransportEndpointContext) => string;
|
|
46
|
+
/**
|
|
47
|
+
* Per-request fetch override. Receives the fully-resolved URL and the
|
|
48
|
+
* RequestInit the transport would have used, plus endpoint context for
|
|
49
|
+
* routing decisions. Customers can rewrite the URL, inject headers, or
|
|
50
|
+
* delegate to a custom transport (e.g. a Cloudflare worker fronting
|
|
51
|
+
* `api.trigger.dev`). Must return a `Response` semantically equivalent to
|
|
52
|
+
* what `globalThis.fetch(url, init)` would have returned.
|
|
53
|
+
*/
|
|
54
|
+
export type ChatFetchOverride = (url: string, init: RequestInit, ctx: ChatTransportEndpointContext) => Promise<Response>;
|
|
55
|
+
/**
|
|
56
|
+
* Arguments for the `accessToken` callback. The transport invokes this
|
|
57
|
+
* whenever it needs a fresh session-scoped PAT — initial use, and
|
|
58
|
+
* after a 401 from any session-PAT-authed request.
|
|
59
|
+
*
|
|
60
|
+
* The callback's job is to return a token, not to start a run.
|
|
61
|
+
* Customers whose implementation also creates the session (typical for
|
|
62
|
+
* `chat.createStartSessionAction` server actions) own the trigger
|
|
63
|
+
* payload server-side — they know their own user/context and don't
|
|
64
|
+
* need anything from the browser to populate `basePayload.metadata`.
|
|
65
|
+
*/
|
|
66
|
+
export type AccessTokenParams = {
|
|
67
|
+
/** Conversation id — same value passed to `sendMessage` / `useChat`. */
|
|
68
|
+
chatId: string;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Arguments for the `startSession` callback. The transport invokes this
|
|
72
|
+
* when it needs a session for a chatId — on `transport.preload(chatId)`,
|
|
73
|
+
* on `transport.start(chatId)`, and lazily on the first `sendMessage`
|
|
74
|
+
* for any chatId without a cached session.
|
|
75
|
+
*
|
|
76
|
+
* The callback typically wraps a server action that calls
|
|
77
|
+
* `chat.createStartSessionAction(taskId)({ chatId, clientData })`. That
|
|
78
|
+
* action is idempotent on `(env, externalId)`, so concurrent / repeat
|
|
79
|
+
* calls converge on the same session.
|
|
80
|
+
*
|
|
81
|
+
* The `clientData` field carries the transport's current `clientData`
|
|
82
|
+
* option — same value the transport merges into per-turn `metadata` on
|
|
83
|
+
* each `.in` chunk. Passing it through `startSession` makes the first
|
|
84
|
+
* run's `payload.metadata` (visible in `onPreload` / `onChatStart`)
|
|
85
|
+
* match what subsequent turns see.
|
|
86
|
+
*
|
|
87
|
+
* @typeParam TClientData – Type of the agent's `clientDataSchema` (when
|
|
88
|
+
* the transport is parameterised with `useTriggerChatTransport<typeof agent>`).
|
|
89
|
+
*/
|
|
90
|
+
export type StartSessionParams<TClientData = unknown> = {
|
|
91
|
+
/** The Trigger.dev task ID associated with this transport. */
|
|
92
|
+
taskId: string;
|
|
93
|
+
/** Conversation id — same value passed to `sendMessage` / `useChat`. */
|
|
94
|
+
chatId: string;
|
|
95
|
+
/**
|
|
96
|
+
* The transport's current `clientData`. Pass through to the server
|
|
97
|
+
* action's `basePayload.metadata` so the first run's `payload.metadata`
|
|
98
|
+
* matches per-turn `metadata`.
|
|
99
|
+
*/
|
|
100
|
+
clientData: TClientData;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Result returned from the `startSession` callback. Carries the
|
|
104
|
+
* session-scoped PAT the transport caches and uses for every
|
|
105
|
+
* `.in/append`, `.out` SSE, and `end-and-continue` call afterward.
|
|
106
|
+
*/
|
|
107
|
+
export type StartSessionResult = {
|
|
108
|
+
/** Session-scoped PAT — `read:sessions:{chatId} + write:sessions:{chatId}`. */
|
|
109
|
+
publicAccessToken: string;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Public surface of {@link TriggerChatTransport}'s session state. Everything
|
|
113
|
+
* the customer should persist for resumption across page reloads. The
|
|
114
|
+
* transport addresses by `chatId` everywhere, so this is light: just a PAT,
|
|
115
|
+
* the last SSE event id, and a couple of UX-state flags.
|
|
116
|
+
*/
|
|
117
|
+
export type ChatSessionPersistedState = {
|
|
118
|
+
publicAccessToken: string;
|
|
119
|
+
lastEventId?: string;
|
|
120
|
+
isStreaming?: boolean;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Common options for the {@link TriggerChatTransport}.
|
|
124
|
+
*
|
|
125
|
+
* @typeParam TClientData – Type of the per-call client data merged into
|
|
126
|
+
* the wire payload via `metadata`. When the task uses `clientDataSchema`,
|
|
127
|
+
* pin this to the schema's input type for end-to-end type safety.
|
|
128
|
+
*/
|
|
129
|
+
export type TriggerChatTransportOptions<TClientData = unknown> = {
|
|
130
|
+
/**
|
|
131
|
+
* The Trigger.dev task ID this transport drives. Sessions created by
|
|
132
|
+
* `transport.start(chatId)` are bound to this task — every run the
|
|
133
|
+
* Session schedules invokes it. Threaded into `startSession` so the
|
|
134
|
+
* customer's server action knows which task to bind.
|
|
135
|
+
*/
|
|
136
|
+
task: string;
|
|
137
|
+
/**
|
|
138
|
+
* Returns a fresh session-scoped PAT for an existing chat session.
|
|
139
|
+
* The transport invokes this on a 401/403 from any session-PAT-authed
|
|
140
|
+
* request — pure refresh, never creates a session.
|
|
141
|
+
*
|
|
142
|
+
* Customer implementation typically does
|
|
143
|
+
* `auth.createPublicToken({ scopes: { read: { sessions: chatId },
|
|
144
|
+
* write: { sessions: chatId } } })` server-side and returns the token.
|
|
145
|
+
*
|
|
146
|
+
* Required so the transport can recover from PAT expiry — never
|
|
147
|
+
* leaves the consumer in an unrecoverable state.
|
|
148
|
+
*/
|
|
149
|
+
accessToken: (params: AccessTokenParams) => string | Promise<string>;
|
|
150
|
+
/**
|
|
151
|
+
* Creates (or no-ops on existing) a session for the given chatId, and
|
|
152
|
+
* returns the session-scoped PAT the transport will use afterward.
|
|
153
|
+
*
|
|
154
|
+
* Wraps a server action that calls
|
|
155
|
+
* `chat.createStartSessionAction(taskId)({ chatId, clientData })`.
|
|
156
|
+
* Customer's server controls authorization, the rest of the
|
|
157
|
+
* triggerConfig, and any atomic DB writes paired with session creation.
|
|
158
|
+
*
|
|
159
|
+
* The transport invokes this:
|
|
160
|
+
* - when `transport.start(chatId)` / `transport.preload(chatId)` is called
|
|
161
|
+
* - lazily on the first `sendMessage` for a chatId with no cached PAT
|
|
162
|
+
*
|
|
163
|
+
* Concurrent and repeat calls dedupe via an in-flight promise + the
|
|
164
|
+
* customer-side idempotency on `(env, externalId)`.
|
|
165
|
+
*
|
|
166
|
+
* Optional only when the customer fully manages session lifecycle
|
|
167
|
+
* externally (hydrating `sessions: { ... }` and never calling
|
|
168
|
+
* `start` / `preload`). Most customers should provide it.
|
|
169
|
+
*/
|
|
170
|
+
startSession?: (params: StartSessionParams<TClientData extends Record<string, unknown> ? TClientData : Record<string, unknown>>) => Promise<StartSessionResult>;
|
|
171
|
+
/**
|
|
172
|
+
* Base URL for the Trigger.dev API. Either a single string applied to every
|
|
173
|
+
* endpoint, or a function called per request that picks a base URL from the
|
|
174
|
+
* endpoint discriminator and chat ID. @default "https://api.trigger.dev"
|
|
175
|
+
*
|
|
176
|
+
* @example Route appends through a proxy, SSE direct:
|
|
177
|
+
* ```ts
|
|
178
|
+
* baseURL: ({ endpoint }) =>
|
|
179
|
+
* endpoint === "out" ? "https://api.trigger.dev" : "https://proxy.example.com",
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
baseURL?: string | ChatBaseURLResolver;
|
|
183
|
+
/**
|
|
184
|
+
* Base URL for the SSE stream subscription only (`GET .../sessions/{chatId}/out`).
|
|
185
|
+
* @deprecated Pass a function for `baseURL` instead and branch on
|
|
186
|
+
* `endpoint === "out"`. `streamBaseURL` continues to work for backwards
|
|
187
|
+
* compatibility and wins over `baseURL` for the SSE endpoint when both
|
|
188
|
+
* are set.
|
|
189
|
+
*/
|
|
190
|
+
streamBaseURL?: string;
|
|
191
|
+
/**
|
|
192
|
+
* Optional per-request fetch override. Called with the resolved URL and the
|
|
193
|
+
* RequestInit the transport built, plus endpoint context. Use this to
|
|
194
|
+
* inject custom headers (e.g. distributed tracing), redirect via a proxy,
|
|
195
|
+
* or wrap fetch with retries/logging.
|
|
196
|
+
*
|
|
197
|
+
* @example Add a tracing header to every chat request:
|
|
198
|
+
* ```ts
|
|
199
|
+
* fetch: (url, init, ctx) => {
|
|
200
|
+
* init.headers = new Headers(init.headers);
|
|
201
|
+
* init.headers.set("traceparent", currentTraceparent());
|
|
202
|
+
* return globalThis.fetch(url, init);
|
|
203
|
+
* },
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
fetch?: ChatFetchOverride;
|
|
207
|
+
/** Additional headers included in every API request. */
|
|
208
|
+
headers?: Record<string, string>;
|
|
209
|
+
/**
|
|
210
|
+
* Seconds to wait for the realtime stream to produce data before timing
|
|
211
|
+
* out. @default 120
|
|
212
|
+
*/
|
|
213
|
+
streamTimeoutSeconds?: number;
|
|
214
|
+
/**
|
|
215
|
+
* Default client data merged into every wire `metadata`. Per-call
|
|
216
|
+
* `metadata` overrides transport-level defaults.
|
|
217
|
+
*/
|
|
218
|
+
clientData?: TClientData extends Record<string, unknown> ? TClientData : Record<string, unknown>;
|
|
219
|
+
/**
|
|
220
|
+
* Restore active session state from external storage (e.g. localStorage)
|
|
221
|
+
* after a page refresh. Hydrated entries skip the start round-trip and
|
|
222
|
+
* use their `publicAccessToken` directly. On 401, the transport
|
|
223
|
+
* invokes `accessToken` to refresh.
|
|
224
|
+
*/
|
|
225
|
+
sessions?: Record<string, ChatSessionPersistedState>;
|
|
226
|
+
/**
|
|
227
|
+
* Called whenever a chat session's state changes. Use this to persist
|
|
228
|
+
* state for reconnection after a page refresh — `null` is passed when
|
|
229
|
+
* the session is removed.
|
|
230
|
+
*/
|
|
231
|
+
onSessionChange?: (chatId: string, session: ChatSessionPersistedState | null) => void;
|
|
232
|
+
/**
|
|
233
|
+
* Enable multi-tab coordination. When `true`, only one tab at a time
|
|
234
|
+
* can send messages to a given chatId; other tabs go read-only.
|
|
235
|
+
*
|
|
236
|
+
* No-op when `BroadcastChannel` is unavailable. @default false
|
|
237
|
+
*/
|
|
238
|
+
multiTab?: boolean;
|
|
239
|
+
/**
|
|
240
|
+
* Read-only "watch" mode for observing an existing chat run from the
|
|
241
|
+
* outside (e.g. a dashboard viewer). When `true`, the SSE subscription
|
|
242
|
+
* stays open across `trigger:turn-complete` so consumers see turn 2,
|
|
243
|
+
* 3, … through one long-lived stream. Pair with `sessions` hydration
|
|
244
|
+
* and `reconnectToStream` for the typical viewer flow. @default false
|
|
245
|
+
*/
|
|
246
|
+
watch?: boolean;
|
|
247
|
+
/**
|
|
248
|
+
* Opt-in URL that gives a brand-new chat a head start: instead of
|
|
249
|
+
* waiting for the trigger.dev agent run to dequeue + boot before
|
|
250
|
+
* the first LLM call, the transport POSTs the first user message
|
|
251
|
+
* to a route handler in your warm process (Next.js, etc.) that
|
|
252
|
+
* exports `chat.handover({ agentId, run })` from
|
|
253
|
+
* `@trigger.dev/sdk/chat-server`. That handler runs `streamText`
|
|
254
|
+
* step 1 right away while the agent boots in parallel, then hands
|
|
255
|
+
* off mid-turn for tool execution (or exits clean for pure-text
|
|
256
|
+
* turns).
|
|
257
|
+
*
|
|
258
|
+
* First turn only. Subsequent turns on the same chat bypass this
|
|
259
|
+
* URL and write directly to `session.in` — the same direct-trigger
|
|
260
|
+
* path used when `headStart` is unset. Customers using `headStart`
|
|
261
|
+
* still need `accessToken` and (optionally) `startSession` for
|
|
262
|
+
* those subsequent turns.
|
|
263
|
+
*
|
|
264
|
+
* NOT a stock `useChat` "endpoint" — this is not the canonical
|
|
265
|
+
* request URL for every turn, just the warm first-turn shortcut.
|
|
266
|
+
*
|
|
267
|
+
* In benchmarks, head-starting drops first-turn TTFC roughly in
|
|
268
|
+
* half versus the direct-trigger flow (cold-start agent boot +
|
|
269
|
+
* onTurnStart hook overlap with the LLM TTFB instead of stacking
|
|
270
|
+
* before it).
|
|
271
|
+
*
|
|
272
|
+
* @default undefined (direct-trigger flow on every turn)
|
|
273
|
+
*/
|
|
274
|
+
headStart?: string;
|
|
275
|
+
};
|
|
276
|
+
/**
|
|
277
|
+
* A custom AI SDK `ChatTransport` that runs chat completions as durable
|
|
278
|
+
* Trigger.dev tasks via the Sessions primitive.
|
|
279
|
+
*
|
|
280
|
+
* Lifecycle:
|
|
281
|
+
* 1. Customer pre-creates the session server-side OR calls
|
|
282
|
+
* `transport.start(chatId)` to mint a one-shot start token and
|
|
283
|
+
* `POST /api/v1/sessions` from the browser.
|
|
284
|
+
* 2. The server triggers the first run as part of session create and
|
|
285
|
+
* returns a session-scoped PAT.
|
|
286
|
+
* 3. `sendMessages` appends to `.in` and subscribes to `.out`. When a
|
|
287
|
+
* run dies (idle, cancel, end-and-continue), the server's
|
|
288
|
+
* append-time probe triggers a fresh run for the same session —
|
|
289
|
+
* transport keeps streaming.
|
|
290
|
+
* 4. `stop()` posts a `{kind:"stop"}` chunk; the agent's turn aborts
|
|
291
|
+
* but the run keeps reading `.in` for the next message.
|
|
292
|
+
* 5. PAT expiry: transport invokes `accessToken` to refresh and
|
|
293
|
+
* retries the failing request once.
|
|
294
|
+
*/
|
|
295
|
+
export declare class TriggerChatTransport implements ChatTransport<UIMessage> {
|
|
296
|
+
private readonly taskId;
|
|
297
|
+
private readonly resolveAccessToken;
|
|
298
|
+
private readonly resolveStartSession;
|
|
299
|
+
private readonly resolveBaseURLFn;
|
|
300
|
+
private readonly fetchOverride;
|
|
301
|
+
private readonly extraHeaders;
|
|
302
|
+
private readonly streamTimeoutSeconds;
|
|
303
|
+
private defaultMetadata;
|
|
304
|
+
private readonly watchMode;
|
|
305
|
+
private readonly headStart;
|
|
306
|
+
private coordinator;
|
|
307
|
+
private _onSessionChange;
|
|
308
|
+
private sessions;
|
|
309
|
+
private activeStreams;
|
|
310
|
+
private pendingStarts;
|
|
311
|
+
constructor(options: TriggerChatTransportOptions);
|
|
312
|
+
/**
|
|
313
|
+
* Eagerly create a Session and trigger its first run. Useful as a
|
|
314
|
+
* "the user might be about to send a message — boot the agent now"
|
|
315
|
+
* preload, or to take ownership of the session before any sendMessage.
|
|
316
|
+
*
|
|
317
|
+
* Idempotent: calling `start(chatId)` twice converges to the same
|
|
318
|
+
* session via the `(env, externalId)` upsert. Concurrent calls
|
|
319
|
+
* deduplicate via an in-flight promise.
|
|
320
|
+
*
|
|
321
|
+
* Requires `getStartToken` to be configured. Customers who pre-create
|
|
322
|
+
* sessions server-side don't need to call this.
|
|
323
|
+
*/
|
|
324
|
+
start(chatId: string): Promise<ChatSessionPersistedState>;
|
|
325
|
+
/**
|
|
326
|
+
* Eagerly create the session before the user types. Same semantics as
|
|
327
|
+
* {@link start} — kept as a separate name for the AI SDK Chat hook,
|
|
328
|
+
* which calls `preload` rather than `start`.
|
|
329
|
+
*/
|
|
330
|
+
preload(chatId: string): Promise<void>;
|
|
331
|
+
/**
|
|
332
|
+
* Send a user message via the session's `.in` channel. The server
|
|
333
|
+
* probes `currentRunId`; if terminal/null it triggers a fresh run on
|
|
334
|
+
* the same session before the append lands. The returned
|
|
335
|
+
* `ReadableStream` carries the agent's response chunks via `.out` SSE.
|
|
336
|
+
*/
|
|
337
|
+
sendMessages: (options: {
|
|
338
|
+
trigger: "submit-message" | "regenerate-message";
|
|
339
|
+
chatId: string;
|
|
340
|
+
messageId: string | undefined;
|
|
341
|
+
messages: UIMessage[];
|
|
342
|
+
abortSignal: AbortSignal | undefined;
|
|
343
|
+
} & ChatRequestOptions) => Promise<ReadableStream<UIMessageChunk>>;
|
|
344
|
+
/**
|
|
345
|
+
* First-turn-only path used when `headStart` is configured. POSTs the
|
|
346
|
+
* wire payload to the customer's `chat.handover` route handler and
|
|
347
|
+
* pipes its SSE response back as a UIMessageChunk stream. Hydrates
|
|
348
|
+
* session state from response headers so subsequent turns bypass
|
|
349
|
+
* the endpoint and use the direct `session.in` path.
|
|
350
|
+
*/
|
|
351
|
+
private sendMessagesViaHandover;
|
|
352
|
+
/**
|
|
353
|
+
* Send a steering message during an active stream without disrupting
|
|
354
|
+
* it. The agent's `pendingMessages` config decides whether to inject
|
|
355
|
+
* between tool-call steps or buffer for the next turn.
|
|
356
|
+
*/
|
|
357
|
+
sendPendingMessage: (chatId: string, message: UIMessage, metadata?: Record<string, unknown>) => Promise<boolean>;
|
|
358
|
+
/**
|
|
359
|
+
* Re-establish an SSE subscription to a known session. Used after a
|
|
360
|
+
* page refresh: the customer hydrates `sessions` in the constructor,
|
|
361
|
+
* the AI SDK calls `reconnectToStream` to resume the stream.
|
|
362
|
+
*/
|
|
363
|
+
reconnectToStream: (options: {
|
|
364
|
+
chatId: string;
|
|
365
|
+
abortSignal?: AbortSignal | undefined;
|
|
366
|
+
} & ChatRequestOptions) => Promise<ReadableStream<UIMessageChunk> | null>;
|
|
367
|
+
/**
|
|
368
|
+
* Stop the current generation. Sends `{kind:"stop"}` on `.in`; the
|
|
369
|
+
* agent aborts its `streamText` call but stays alive for the next
|
|
370
|
+
* message.
|
|
371
|
+
*/
|
|
372
|
+
stopGeneration: (chatId: string) => Promise<boolean>;
|
|
373
|
+
/**
|
|
374
|
+
* Send a custom action chunk (for `chat.agent`'s `actionSchema` /
|
|
375
|
+
* `onAction` hook). Actions are not turns — only `hydrateMessages`
|
|
376
|
+
* and `onAction` fire on the agent side. The returned stream
|
|
377
|
+
* carries any model response `onAction` produced (when it returns a
|
|
378
|
+
* `StreamTextResult`); for `void`-returning side-effect-only actions
|
|
379
|
+
* the stream completes immediately with `trigger:turn-complete`.
|
|
380
|
+
*/
|
|
381
|
+
sendAction: (chatId: string, action: unknown) => Promise<ReadableStream<UIMessageChunk>>;
|
|
382
|
+
getSession: (chatId: string) => ChatSessionPersistedState | undefined;
|
|
383
|
+
setSession(chatId: string, session: ChatSessionPersistedState): void;
|
|
384
|
+
setOnSessionChange(callback: ((chatId: string, session: ChatSessionPersistedState | null) => void) | undefined): void;
|
|
385
|
+
/**
|
|
386
|
+
* Update the transport's `clientData`. Used by `useTriggerChatTransport`
|
|
387
|
+
* to keep the latest value reachable from inside `startSession` and
|
|
388
|
+
* the per-turn `metadata` merge without recreating the transport.
|
|
389
|
+
*
|
|
390
|
+
* Reads always go through the live field — closures around the
|
|
391
|
+
* transport see the latest value the next time they fire.
|
|
392
|
+
*/
|
|
393
|
+
setClientData(clientData: Record<string, unknown> | undefined): void;
|
|
394
|
+
isReadOnly(chatId: string): boolean;
|
|
395
|
+
hasClaim(chatId: string): boolean;
|
|
396
|
+
addReadOnlyListener(fn: (chatId: string, isReadOnly: boolean) => void): void;
|
|
397
|
+
removeReadOnlyListener(fn: (chatId: string, isReadOnly: boolean) => void): void;
|
|
398
|
+
broadcastMessages(chatId: string, messages: unknown[]): void;
|
|
399
|
+
addMessagesListener(fn: (chatId: string, messages: unknown[]) => void): void;
|
|
400
|
+
removeMessagesListener(fn: (chatId: string, messages: unknown[]) => void): void;
|
|
401
|
+
dispose(): void;
|
|
402
|
+
private serializeInputChunk;
|
|
403
|
+
private toPersisted;
|
|
404
|
+
private notifySessionChange;
|
|
405
|
+
/**
|
|
406
|
+
* Resolves the session state for a chatId, starting the session if
|
|
407
|
+
* needed (and `getStartToken` is configured). Customers who provide
|
|
408
|
+
* `accessToken` but no `getStartToken` are expected to have created
|
|
409
|
+
* the session server-side; in that case the first `accessToken` call
|
|
410
|
+
* returns a fresh session PAT.
|
|
411
|
+
*/
|
|
412
|
+
private ensureSessionState;
|
|
413
|
+
private doStart;
|
|
414
|
+
/**
|
|
415
|
+
* Run `op` with the session's stored PAT. On 401/403, refresh the PAT
|
|
416
|
+
* via `accessToken` and retry once. Surfaces non-auth errors as-is.
|
|
417
|
+
*/
|
|
418
|
+
private resolveBaseURL;
|
|
419
|
+
private doFetch;
|
|
420
|
+
private appendInputChunk;
|
|
421
|
+
private callWithAuthRetry;
|
|
422
|
+
/**
|
|
423
|
+
* Open an SSE subscription to the session's `.out` stream and pipe
|
|
424
|
+
* UIMessageChunks through to the AI SDK. Trigger control records
|
|
425
|
+
* (`turn-complete`, `upgrade-required` — see `trigger-control` header
|
|
426
|
+
* on `client-protocol.mdx#records-on-session-out`) are routed by
|
|
427
|
+
* header and never reach the consumer. `upgrade-required` is purely
|
|
428
|
+
* telemetry now since the server handles the run swap inline (see
|
|
429
|
+
* `end-and-continue`).
|
|
430
|
+
*/
|
|
431
|
+
private subscribeToSessionStream;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Convenience constructor matching {@link TriggerChatTransport}.
|
|
435
|
+
*/
|
|
436
|
+
export declare function createChatTransport(options: TriggerChatTransportOptions): TriggerChatTransport;
|
|
437
|
+
export { AgentChat, ChatStream, type AgentChatOptions, type ChatSession, type ChatStreamResult, type ChatToolCall, type ChatToolResult, type InferChatClientData, type InferChatUIMessage, } from "./chat-client.js";
|