@trigger.dev/sdk 0.0.0-chat-prerelease-20260401212829 → 0.0.0-chat-prerelease-20260402124815
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/chat-client.d.ts +116 -146
- package/dist/commonjs/v3/chat-client.js +181 -325
- package/dist/commonjs/v3/chat-client.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +1 -1
- package/dist/commonjs/v3/chat.js +3 -4
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/chat-client.d.ts +116 -146
- package/dist/esm/v3/chat-client.js +179 -322
- package/dist/esm/v3/chat-client.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +1 -1
- package/dist/esm/v3/chat.js +2 -2
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -2
|
@@ -1,31 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Server-side
|
|
3
|
-
* Works inside tasks (agent-to-agent), server handlers, and standalone scripts.
|
|
2
|
+
* Server-side API for chatting with Trigger.dev agents.
|
|
4
3
|
*
|
|
5
4
|
* @example
|
|
6
5
|
* ```ts
|
|
7
|
-
* import {
|
|
8
|
-
* import type { prReviewChat } from "./trigger/pr-review";
|
|
6
|
+
* import { AgentChat } from "@trigger.dev/sdk/chat";
|
|
9
7
|
*
|
|
10
|
-
* const
|
|
11
|
-
*
|
|
12
|
-
* clientData: { userId: "user_123"
|
|
8
|
+
* const chat = new AgentChat<typeof myAgent>({
|
|
9
|
+
* agent: "my-agent",
|
|
10
|
+
* clientData: { userId: "user_123" },
|
|
13
11
|
* });
|
|
14
12
|
*
|
|
15
|
-
* const
|
|
16
|
-
* await
|
|
17
|
-
*
|
|
18
|
-
* // Stream with typed chunks
|
|
19
|
-
* const turn = await conv.send("Review PR #1");
|
|
20
|
-
* for await (const chunk of turn) {
|
|
21
|
-
* if (chunk.type === "text-delta") process.stdout.write(chunk.textDelta);
|
|
22
|
-
* }
|
|
23
|
-
*
|
|
24
|
-
* // Or just get the text
|
|
25
|
-
* const fix = await conv.textResponse("Fix the bug");
|
|
26
|
-
* console.log(conv.messages); // ← typed UIMessage[]
|
|
27
|
-
*
|
|
28
|
-
* await conv.close();
|
|
13
|
+
* const stream = await chat.sendMessage("Review PR #1");
|
|
14
|
+
* const text = await stream.text();
|
|
15
|
+
* await chat.close();
|
|
29
16
|
* ```
|
|
30
17
|
*/
|
|
31
18
|
import type { Task } from "@trigger.dev/core/v3";
|
|
@@ -35,16 +22,43 @@ import type { ChatTaskWirePayload } from "./ai.js";
|
|
|
35
22
|
export type InferChatClientData<T> = T extends Task<any, ChatTaskWirePayload<any, infer TMetadata>, any> ? unknown extends TMetadata ? Record<string, unknown> : TMetadata : Record<string, unknown>;
|
|
36
23
|
/** Extract the UIMessage type from a chat agent task. */
|
|
37
24
|
export type InferChatUIMessage<T> = T extends Task<any, ChatTaskWirePayload<infer TUIMessage, any>, any> ? TUIMessage : UIMessage;
|
|
25
|
+
/** Persistable session state — store this to resume across requests. */
|
|
38
26
|
export type ChatSession = {
|
|
39
27
|
runId: string;
|
|
40
|
-
publicAccessToken: string;
|
|
41
28
|
lastEventId?: string;
|
|
42
29
|
};
|
|
43
|
-
export type
|
|
44
|
-
/** The task ID to trigger. */
|
|
45
|
-
|
|
46
|
-
/**
|
|
30
|
+
export type AgentChatOptions<TAgent = unknown> = {
|
|
31
|
+
/** The agent task ID to trigger. */
|
|
32
|
+
agent: string;
|
|
33
|
+
/**
|
|
34
|
+
* Conversation ID. Used for tagging runs and correlating messages.
|
|
35
|
+
* @default crypto.randomUUID()
|
|
36
|
+
*/
|
|
37
|
+
id?: string;
|
|
38
|
+
/** Client data included in every request. Typed from the agent's clientDataSchema. */
|
|
47
39
|
clientData?: InferChatClientData<TAgent>;
|
|
40
|
+
/**
|
|
41
|
+
* Restore a previous session. Pass the runId (and optionally lastEventId)
|
|
42
|
+
* from a previous request to resume the same agent run.
|
|
43
|
+
*/
|
|
44
|
+
session?: ChatSession;
|
|
45
|
+
/**
|
|
46
|
+
* Called when a new run is triggered (first message or preload).
|
|
47
|
+
* Use this to persist the session for later resumption.
|
|
48
|
+
*/
|
|
49
|
+
onTriggered?: (event: {
|
|
50
|
+
runId: string;
|
|
51
|
+
chatId: string;
|
|
52
|
+
}) => void | Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Called when a turn completes (the agent's response stream ends).
|
|
55
|
+
* Use this to persist the lastEventId for stream resumption.
|
|
56
|
+
*/
|
|
57
|
+
onTurnComplete?: (event: {
|
|
58
|
+
runId: string;
|
|
59
|
+
chatId: string;
|
|
60
|
+
lastEventId?: string;
|
|
61
|
+
}) => void | Promise<void>;
|
|
48
62
|
/** Stream key for output stream. @default "chat" */
|
|
49
63
|
streamKey?: string;
|
|
50
64
|
/** SSE timeout in seconds. @default 120 */
|
|
@@ -58,173 +72,129 @@ export type ChatClientOptions<TAgent = unknown> = {
|
|
|
58
72
|
priority?: number;
|
|
59
73
|
};
|
|
60
74
|
};
|
|
61
|
-
type UIMessageLike = {
|
|
62
|
-
id: string;
|
|
63
|
-
role: string;
|
|
64
|
-
parts?: unknown[];
|
|
65
|
-
[key: string]: unknown;
|
|
66
|
-
};
|
|
67
75
|
/** Parsed tool call from the stream. */
|
|
68
76
|
export type ChatToolCall = {
|
|
69
77
|
toolName: string;
|
|
70
78
|
toolCallId: string;
|
|
71
|
-
|
|
79
|
+
input: unknown;
|
|
72
80
|
};
|
|
73
81
|
/** Parsed tool result from the stream. */
|
|
74
82
|
export type ChatToolResult = {
|
|
75
|
-
toolName: string;
|
|
76
83
|
toolCallId: string;
|
|
77
|
-
|
|
84
|
+
output: unknown;
|
|
78
85
|
};
|
|
79
|
-
/**
|
|
86
|
+
/** Accumulated result after a stream completes. */
|
|
80
87
|
export type ChatStreamResult = {
|
|
81
88
|
text: string;
|
|
82
89
|
toolCalls: ChatToolCall[];
|
|
83
90
|
toolResults: ChatToolResult[];
|
|
84
91
|
};
|
|
85
92
|
/**
|
|
86
|
-
*
|
|
87
|
-
* Yields `UIMessageChunk` from the AI SDK.
|
|
93
|
+
* A single turn's response stream from an agent.
|
|
88
94
|
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* for await (const chunk of stream) {
|
|
95
|
-
* if (chunk.type === "text-delta") process.stdout.write(chunk.textDelta);
|
|
96
|
-
* if (chunk.type === "tool-call") console.log(chunk.toolName, chunk.args);
|
|
97
|
-
* }
|
|
98
|
-
*
|
|
99
|
-
* // Or consume and get the structured result
|
|
100
|
-
* const { text, toolCalls, toolResults } = await stream.result();
|
|
101
|
-
* ```
|
|
95
|
+
* Pick one consumption mode:
|
|
96
|
+
* - `for await (const chunk of stream)` — typed UIMessageChunk iteration
|
|
97
|
+
* - `await stream.result()` — accumulated `{ text, toolCalls, toolResults }`
|
|
98
|
+
* - `await stream.text()` — just the text
|
|
99
|
+
* - `yield* stream.messages()` — sub-agent pattern (yields UIMessage snapshots)
|
|
102
100
|
*/
|
|
103
101
|
export declare class ChatStream {
|
|
104
|
-
private readonly
|
|
105
|
-
private teed;
|
|
102
|
+
private readonly _stream;
|
|
106
103
|
private resultPromise;
|
|
107
104
|
constructor(stream: ReadableStream<UIMessageChunk>);
|
|
108
|
-
/**
|
|
105
|
+
/** The raw ReadableStream for direct use with AI SDK utilities. */
|
|
106
|
+
get stream(): ReadableStream<UIMessageChunk>;
|
|
109
107
|
[Symbol.asyncIterator](): AsyncIterableIterator<UIMessageChunk>;
|
|
110
108
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
109
|
+
* Yields accumulated UIMessage snapshots for the sub-agent tool pattern.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const stream = await chat.sendMessage("Research this topic");
|
|
114
|
+
* yield* stream.messages();
|
|
115
|
+
* ```
|
|
113
116
|
*/
|
|
117
|
+
messages(): AsyncGenerator<UIMessage, void, unknown>;
|
|
118
|
+
/** Consume the stream and return the accumulated result. */
|
|
114
119
|
result(): Promise<ChatStreamResult>;
|
|
115
|
-
/**
|
|
120
|
+
/** Consume the stream and return just the text. */
|
|
116
121
|
text(): Promise<string>;
|
|
117
122
|
private consumeStream;
|
|
118
123
|
}
|
|
119
124
|
/**
|
|
120
|
-
*
|
|
121
|
-
* Manages the turn lifecycle, accumulates messages, and returns typed streams.
|
|
125
|
+
* A chat conversation with a Trigger.dev agent.
|
|
122
126
|
*
|
|
123
127
|
* @example
|
|
124
128
|
* ```ts
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* const { text, toolCalls } = await turn.result();
|
|
129
|
+
* // Simple usage
|
|
130
|
+
* const chat = new AgentChat<typeof myAgent>({ agent: "my-agent" });
|
|
131
|
+
* const text = await (await chat.sendMessage("Hello")).text();
|
|
132
|
+
* await chat.close();
|
|
130
133
|
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
private readonly chatId;
|
|
140
|
-
private readonly clientData?;
|
|
141
|
-
private _messages;
|
|
142
|
-
constructor(client: ChatClient<TAgent>, chatId: string, clientData?: InferChatClientData<TAgent> | undefined);
|
|
143
|
-
/** The accumulated conversation messages (user + assistant). */
|
|
144
|
-
get messages(): readonly InferChatUIMessage<TAgent>[];
|
|
145
|
-
/** Preload the agent — initialize before the first message. */
|
|
146
|
-
preload(options?: {
|
|
147
|
-
idleTimeoutInSeconds?: number;
|
|
148
|
-
}): Promise<ChatSession>;
|
|
149
|
-
/**
|
|
150
|
-
* Send a text message and return a typed ChatStream for the response.
|
|
151
|
-
* The user message and assistant response are accumulated in `messages`.
|
|
152
|
-
*/
|
|
153
|
-
send(text: string, options?: {
|
|
154
|
-
abortSignal?: AbortSignal;
|
|
155
|
-
}): Promise<ChatStream>;
|
|
156
|
-
/** Send a text message and return the full text response. */
|
|
157
|
-
textResponse(text: string): Promise<string>;
|
|
158
|
-
/** Send a text message and return the full structured result. */
|
|
159
|
-
fullResponse(text: string): Promise<ChatStreamResult>;
|
|
160
|
-
/** Send a steering/pending message during an active stream. */
|
|
161
|
-
steer(text: string): Promise<boolean>;
|
|
162
|
-
/** Stop the current generation. */
|
|
163
|
-
stop(): Promise<void>;
|
|
164
|
-
/** Close the conversation — agent exits gracefully. */
|
|
165
|
-
close(): Promise<boolean>;
|
|
166
|
-
/** Get the current session. */
|
|
167
|
-
get session(): ChatSession | undefined;
|
|
168
|
-
private accumulateResponse;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Server-side chat client for interacting with Trigger.dev chat agents.
|
|
172
|
-
*
|
|
173
|
-
* Pass the agent type for full type inference:
|
|
174
|
-
* ```ts
|
|
175
|
-
* const client = new ChatClient<typeof myAgent>({ task: "my-agent" });
|
|
134
|
+
* // Stateless request handler — persist and restore session
|
|
135
|
+
* const chat = new AgentChat<typeof myAgent>({
|
|
136
|
+
* agent: "my-agent",
|
|
137
|
+
* id: chatId,
|
|
138
|
+
* session: { runId: savedRunId, lastEventId: savedLastEventId },
|
|
139
|
+
* onTriggered: ({ runId }) => db.save(chatId, { runId }),
|
|
140
|
+
* onTurnComplete: ({ lastEventId }) => db.update(chatId, { lastEventId }),
|
|
141
|
+
* });
|
|
176
142
|
* ```
|
|
177
143
|
*/
|
|
178
|
-
export declare class
|
|
144
|
+
export declare class AgentChat<TAgent = unknown> {
|
|
179
145
|
private readonly taskId;
|
|
146
|
+
private readonly chatId;
|
|
180
147
|
private readonly streamKey;
|
|
181
148
|
private readonly streamTimeoutSeconds;
|
|
182
|
-
private readonly
|
|
149
|
+
private readonly clientData;
|
|
183
150
|
private readonly triggerOptions;
|
|
184
|
-
private
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}): ChatConversation<TAgent>;
|
|
151
|
+
private readonly onTriggered;
|
|
152
|
+
private readonly onTurnComplete;
|
|
153
|
+
private session;
|
|
154
|
+
constructor(options: AgentChatOptions<TAgent>);
|
|
155
|
+
/** The conversation ID. */
|
|
156
|
+
get id(): string;
|
|
157
|
+
/** The current run session, if active. */
|
|
158
|
+
get run(): ChatSession | undefined;
|
|
193
159
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
160
|
+
* Preload the agent — start the run before the first message.
|
|
161
|
+
* The agent's `onPreload` hook fires immediately.
|
|
162
|
+
* No-op if already has a session.
|
|
196
163
|
*/
|
|
197
|
-
preload(
|
|
198
|
-
clientData?: InferChatClientData<TAgent>;
|
|
164
|
+
preload(options?: {
|
|
199
165
|
idleTimeoutInSeconds?: number;
|
|
200
166
|
}): Promise<ChatSession>;
|
|
201
167
|
/**
|
|
202
|
-
* Send
|
|
203
|
-
*
|
|
204
|
-
*
|
|
168
|
+
* Send a text message and get the response stream.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* const stream = await chat.sendMessage("Review PR #1");
|
|
173
|
+
* const text = await stream.text();
|
|
174
|
+
* ```
|
|
205
175
|
*/
|
|
206
|
-
|
|
176
|
+
sendMessage(text: string, options?: {
|
|
177
|
+
abortSignal?: AbortSignal;
|
|
178
|
+
}): Promise<ChatStream>;
|
|
179
|
+
/** Send raw UIMessage-like objects. Use `sendMessage()` for simple text. */
|
|
180
|
+
sendRaw(messages: Array<{
|
|
181
|
+
id: string;
|
|
182
|
+
role: string;
|
|
183
|
+
parts?: unknown[];
|
|
184
|
+
[key: string]: unknown;
|
|
185
|
+
}>, options?: {
|
|
207
186
|
trigger?: "submit-message" | "regenerate-message";
|
|
208
|
-
clientData?: InferChatClientData<TAgent>;
|
|
209
187
|
abortSignal?: AbortSignal;
|
|
210
188
|
}): Promise<ReadableStream<UIMessageChunk>>;
|
|
211
|
-
/**
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
/** Reconnect to an existing session's response stream. */
|
|
221
|
-
reconnect(chatId: string, abortSignal?: AbortSignal): Promise<ReadableStream<UIMessageChunk> | null>;
|
|
222
|
-
getSession(chatId: string): ChatSession | undefined;
|
|
223
|
-
setSession(chatId: string, session: ChatSession): void;
|
|
224
|
-
deleteSession(chatId: string): void;
|
|
225
|
-
private mergeClientData;
|
|
226
|
-
private createStreamApiClient;
|
|
189
|
+
/** Send a steering message during an active stream. */
|
|
190
|
+
steer(text: string): Promise<boolean>;
|
|
191
|
+
/** Stop the current generation (agent stays alive for next turn). */
|
|
192
|
+
stop(): Promise<void>;
|
|
193
|
+
/** Close the conversation — agent exits its loop gracefully. */
|
|
194
|
+
close(): Promise<boolean>;
|
|
195
|
+
/** Reconnect to the response stream (e.g. after a disconnect). */
|
|
196
|
+
reconnect(abortSignal?: AbortSignal): Promise<ReadableStream<UIMessageChunk> | null>;
|
|
197
|
+
private createApiClient;
|
|
227
198
|
private triggerNewRun;
|
|
228
199
|
private subscribeToStream;
|
|
229
200
|
}
|
|
230
|
-
export {};
|