@trigger.dev/sdk 4.8.2 → 4.8.4
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/ai-shared.d.ts +3 -2
- package/dist/commonjs/v3/ai-shared.js +219 -28
- package/dist/commonjs/v3/ai-shared.js.map +1 -1
- package/dist/commonjs/v3/ai.d.ts +4 -0
- package/dist/commonjs/v3/ai.js +97 -87
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/auth.d.ts +11 -4
- package/dist/commonjs/v3/auth.js +9 -8
- package/dist/commonjs/v3/auth.js.map +1 -1
- package/dist/commonjs/v3/chat-server.js +3 -1
- package/dist/commonjs/v3/chat-server.js.map +1 -1
- package/dist/commonjs/v3/chat.js +26 -6
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/index.d.ts +1 -1
- package/dist/commonjs/v3/index.js +2 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/oidc/aws.d.ts +24 -0
- package/dist/commonjs/v3/oidc/aws.js +46 -0
- package/dist/commonjs/v3/oidc/aws.js.map +1 -0
- package/dist/commonjs/v3/oidc/infisical.d.ts +16 -0
- package/dist/commonjs/v3/oidc/infisical.js +43 -0
- package/dist/commonjs/v3/oidc/infisical.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai-shared.d.ts +3 -2
- package/dist/esm/v3/ai-shared.js +214 -28
- package/dist/esm/v3/ai-shared.js.map +1 -1
- package/dist/esm/v3/ai.d.ts +4 -0
- package/dist/esm/v3/ai.js +98 -88
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/auth.d.ts +11 -4
- package/dist/esm/v3/auth.js +9 -8
- package/dist/esm/v3/auth.js.map +1 -1
- package/dist/esm/v3/chat-server.js +4 -2
- package/dist/esm/v3/chat-server.js.map +1 -1
- package/dist/esm/v3/chat.js +26 -6
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/index.d.ts +1 -1
- package/dist/esm/v3/index.js +1 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/oidc/aws.d.ts +24 -0
- package/dist/esm/v3/oidc/aws.js +43 -0
- package/dist/esm/v3/oidc/aws.js.map +1 -0
- package/dist/esm/v3/oidc/infisical.d.ts +16 -0
- package/dist/esm/v3/oidc/infisical.js +40 -0
- package/dist/esm/v3/oidc/infisical.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/docs/ai-chat/backend.mdx +2 -2
- package/docs/ai-chat/custom-agents.mdx +8 -1
- package/docs/ai-chat/frontend.mdx +1 -1
- package/docs/ai-chat/lifecycle-hooks.mdx +4 -0
- package/docs/ai-chat/migrating-from-hydrate-messages.mdx +1 -1
- package/docs/ai-chat/transcript-storage.mdx +3 -1
- package/docs/apikeys/custom-access.mdx +209 -0
- package/docs/apikeys.mdx +54 -13
- package/docs/cli-login-commands.mdx +7 -1
- package/docs/cli-preview-archive.mdx +8 -0
- package/docs/cli-webhooks-commands.mdx +115 -0
- package/docs/config/config-file.mdx +2 -2
- package/docs/deployment/overview.mdx +2 -0
- package/docs/deployment/trusted-publishing.mdx +140 -0
- package/docs/github-actions.mdx +2 -2
- package/docs/github-integration.mdx +21 -0
- package/docs/oidc/anthropic.mdx +187 -0
- package/docs/oidc/aws.mdx +219 -0
- package/docs/oidc/custom.mdx +48 -0
- package/docs/oidc/infisical.mdx +59 -0
- package/docs/oidc.mdx +75 -0
- package/docs/self-hosting/env/webapp.mdx +7 -0
- package/docs/webhooks/connect.mdx +12 -0
- package/docs/webhooks/public-beta.mdx +70 -0
- package/package.json +37 -2
|
@@ -84,8 +84,9 @@ export type InferChatUIMessageFromTools<TTools extends ToolSet> = UIMessage<unkn
|
|
|
84
84
|
* onto the existing entry; pushing again would duplicate the row
|
|
85
85
|
* in the chain you return, and the duplicate slim copy would hit
|
|
86
86
|
* `toModelMessages` with no `input`. Returns `false`.
|
|
87
|
-
* - **Incoming id not in `stored`** (
|
|
88
|
-
* on a new turn): push. Returns `true`.
|
|
87
|
+
* - **Incoming user message whose id is not in `stored`** (a fresh user
|
|
88
|
+
* message on a new turn): push. Returns `true`. Any other new message is
|
|
89
|
+
* not something a client can add to the chain, so it is never pushed.
|
|
89
90
|
*
|
|
90
91
|
* Mutates `stored` in place. The caller persists `stored`, not the
|
|
91
92
|
* return value.
|
|
@@ -19,6 +19,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
19
19
|
exports.MAX_EOF_RESUBSCRIBES = exports.BACKGROUND_INJECTION_TYPE = exports.PENDING_MESSAGE_INJECTED_TYPE = void 0;
|
|
20
20
|
exports.upsertIncomingMessage = upsertIncomingMessage;
|
|
21
21
|
exports.slimSubmitMessageForWire = slimSubmitMessageForWire;
|
|
22
|
+
exports.sanitizeClientMessage = sanitizeClientMessage;
|
|
23
|
+
exports.sanitizeClientHistory = sanitizeClientHistory;
|
|
24
|
+
exports.sanitizeClientWirePayload = sanitizeClientWirePayload;
|
|
25
|
+
exports.resolveClientToolResolution = resolveClientToolResolution;
|
|
26
|
+
exports.dropUnbackedToolResolutions = dropUnbackedToolResolutions;
|
|
22
27
|
exports.chatRunTags = chatRunTags;
|
|
23
28
|
exports.waitBeforeEofResubscribe = waitBeforeEofResubscribe;
|
|
24
29
|
/**
|
|
@@ -48,8 +53,9 @@ exports.BACKGROUND_INJECTION_TYPE = "data-background-injected";
|
|
|
48
53
|
* onto the existing entry; pushing again would duplicate the row
|
|
49
54
|
* in the chain you return, and the duplicate slim copy would hit
|
|
50
55
|
* `toModelMessages` with no `input`. Returns `false`.
|
|
51
|
-
* - **Incoming id not in `stored`** (
|
|
52
|
-
* on a new turn): push. Returns `true`.
|
|
56
|
+
* - **Incoming user message whose id is not in `stored`** (a fresh user
|
|
57
|
+
* message on a new turn): push. Returns `true`. Any other new message is
|
|
58
|
+
* not something a client can add to the chain, so it is never pushed.
|
|
53
59
|
*
|
|
54
60
|
* Mutates `stored` in place. The caller persists `stored`, not the
|
|
55
61
|
* return value.
|
|
@@ -78,6 +84,8 @@ function upsertIncomingMessage(stored, event) {
|
|
|
78
84
|
const newMsg = event.incomingMessages[event.incomingMessages.length - 1];
|
|
79
85
|
if (!newMsg)
|
|
80
86
|
return false;
|
|
87
|
+
if (newMsg.role !== "user")
|
|
88
|
+
return false;
|
|
81
89
|
if (newMsg.id) {
|
|
82
90
|
const existingIdx = stored.findIndex((m) => m.id === newMsg.id);
|
|
83
91
|
if (existingIdx !== -1)
|
|
@@ -139,37 +147,220 @@ function slimSubmitMessageForWire(message) {
|
|
|
139
147
|
const advancedToolParts = parts.filter((p) => p && typeof p === "object" && isToolPartType(p.type) && isWireAdvanceableToolState(p.state));
|
|
140
148
|
if (advancedToolParts.length === 0)
|
|
141
149
|
return message;
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
150
|
+
return {
|
|
151
|
+
id: message.id,
|
|
152
|
+
role: message.role,
|
|
153
|
+
parts: advancedToolParts.map(slimAdvancedToolPart),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function slimAdvancedToolPart(p) {
|
|
157
|
+
const base = {
|
|
158
|
+
type: p.type,
|
|
159
|
+
toolCallId: p.toolCallId,
|
|
160
|
+
state: p.state,
|
|
161
|
+
};
|
|
162
|
+
if (p.type === "dynamic-tool" && typeof p.toolName === "string") {
|
|
163
|
+
base.toolName = p.toolName;
|
|
164
|
+
}
|
|
165
|
+
if (p.state === "output-available") {
|
|
166
|
+
base.output = p.output;
|
|
167
|
+
if (p.approval !== undefined)
|
|
168
|
+
base.approval = p.approval;
|
|
169
|
+
}
|
|
170
|
+
else if (p.state === "output-error") {
|
|
171
|
+
if (p.errorText !== undefined)
|
|
172
|
+
base.errorText = p.errorText;
|
|
173
|
+
if (p.approval !== undefined)
|
|
174
|
+
base.approval = p.approval;
|
|
175
|
+
}
|
|
176
|
+
else if (p.state === "approval-responded" || p.state === "output-denied") {
|
|
177
|
+
if (p.approval !== undefined)
|
|
178
|
+
base.approval = p.approval;
|
|
179
|
+
}
|
|
180
|
+
return base;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Reduce a message that arrived from a chat client to what a client can legitimately send.
|
|
184
|
+
*
|
|
185
|
+
* A client sends user messages, and assistant messages only to resolve a tool call the agent made
|
|
186
|
+
* (`addToolApprovalResponse` / `addToolOutput`), in the slim shape `slimSubmitMessageForWire` produces.
|
|
187
|
+
* So a user message loses any tool parts, an assistant message keeps only its resolved tool parts and
|
|
188
|
+
* only the fields the agent overlays (never `input`), and any other message is dropped. A tool part with
|
|
189
|
+
* no `input` is only ever applied as a resolution of a call already in the agent's history; see
|
|
190
|
+
* {@link resolveClientToolResolution}.
|
|
191
|
+
*
|
|
192
|
+
* Returns `undefined` when nothing a client may send is left.
|
|
193
|
+
*
|
|
194
|
+
* @internal
|
|
195
|
+
*/
|
|
196
|
+
function sanitizeClientMessage(message) {
|
|
197
|
+
if (!message || typeof message !== "object")
|
|
198
|
+
return undefined;
|
|
199
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
200
|
+
if (message.role === "user") {
|
|
201
|
+
const kept = parts.filter((p) => !(p && typeof p === "object" && isToolPartType(p.type)));
|
|
202
|
+
return kept.length === parts.length ? message : { ...message, parts: kept };
|
|
203
|
+
}
|
|
204
|
+
if (message.role !== "assistant")
|
|
205
|
+
return undefined;
|
|
206
|
+
const advanced = parts.filter((p) => p &&
|
|
207
|
+
typeof p === "object" &&
|
|
208
|
+
isToolPartType(p.type) &&
|
|
209
|
+
typeof p.toolCallId === "string" &&
|
|
210
|
+
isWireAdvanceableToolState(p.state));
|
|
211
|
+
if (advanced.length === 0)
|
|
212
|
+
return undefined;
|
|
213
|
+
return {
|
|
214
|
+
id: message.id,
|
|
215
|
+
role: message.role,
|
|
216
|
+
parts: advanced.map(slimAdvancedToolPart),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Reduce first-turn history a chat client sent to `chat.headStart`. A new chat can open with an
|
|
221
|
+
* assistant greeting, so assistant messages keep their text; nothing else from the client can carry a
|
|
222
|
+
* tool call, reasoning or a system prompt into the conversation.
|
|
223
|
+
*
|
|
224
|
+
* @internal
|
|
225
|
+
*/
|
|
226
|
+
function sanitizeClientHistory(messages) {
|
|
227
|
+
const out = [];
|
|
228
|
+
for (const message of messages) {
|
|
229
|
+
if (!message || typeof message !== "object")
|
|
230
|
+
continue;
|
|
231
|
+
if (message.role === "user") {
|
|
232
|
+
const sanitized = sanitizeClientMessage(message);
|
|
233
|
+
if (sanitized)
|
|
234
|
+
out.push(sanitized);
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
if (message.role !== "assistant")
|
|
238
|
+
continue;
|
|
239
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
240
|
+
const text = parts.filter((p) => p && typeof p === "object" && p.type === "text");
|
|
241
|
+
if (text.length > 0) {
|
|
242
|
+
out.push({ ...message, parts: text });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Sanitize every client-supplied message on a wire payload. See {@link sanitizeClientMessage}.
|
|
249
|
+
*
|
|
250
|
+
* @internal
|
|
251
|
+
*/
|
|
252
|
+
function sanitizeClientWirePayload(payload) {
|
|
253
|
+
if (!payload || typeof payload !== "object")
|
|
254
|
+
return payload;
|
|
255
|
+
let next = payload;
|
|
256
|
+
if (payload.message !== undefined) {
|
|
257
|
+
const message = sanitizeClientMessage(payload.message);
|
|
258
|
+
if (message !== payload.message) {
|
|
259
|
+
next = { ...next, message };
|
|
260
|
+
if (message === undefined)
|
|
261
|
+
delete next.message;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (Array.isArray(payload.headStartMessages)) {
|
|
265
|
+
next = {
|
|
266
|
+
...next,
|
|
267
|
+
headStartMessages: sanitizeClientHistory(payload.headStartMessages),
|
|
147
268
|
};
|
|
148
|
-
|
|
149
|
-
|
|
269
|
+
}
|
|
270
|
+
return next;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Match a client's tool resolution against the history the agent holds.
|
|
274
|
+
*
|
|
275
|
+
* Each resolved tool part has to answer a call already in `history`: an approval response needs the
|
|
276
|
+
* stored call to be awaiting approval with the same approval id, and a tool output needs the stored call
|
|
277
|
+
* to be awaiting its result. The returned message carries the id of the stored message holding those
|
|
278
|
+
* calls, so it merges onto it whatever id the client gave it. Parts that answer nothing are dropped, and
|
|
279
|
+
* so is the message when none are left.
|
|
280
|
+
*
|
|
281
|
+
* A message with no input-less resolved tool parts is returned unchanged.
|
|
282
|
+
*
|
|
283
|
+
* @internal
|
|
284
|
+
*/
|
|
285
|
+
function resolveClientToolResolution(message, history) {
|
|
286
|
+
if (message.role !== "assistant")
|
|
287
|
+
return message;
|
|
288
|
+
const parts = Array.isArray(message.parts) ? message.parts : [];
|
|
289
|
+
const resolutions = parts.filter(isClientToolResolution);
|
|
290
|
+
if (resolutions.length === 0)
|
|
291
|
+
return message;
|
|
292
|
+
let targetId;
|
|
293
|
+
const kept = [];
|
|
294
|
+
for (const part of resolutions) {
|
|
295
|
+
const found = findStoredToolPart(history, part.toolCallId);
|
|
296
|
+
if (!found)
|
|
297
|
+
continue;
|
|
298
|
+
if (targetId !== undefined && found.messageId !== targetId)
|
|
299
|
+
continue;
|
|
300
|
+
if (!isAllowedResolution(found.part, part))
|
|
301
|
+
continue;
|
|
302
|
+
targetId = found.messageId;
|
|
303
|
+
kept.push(part);
|
|
304
|
+
}
|
|
305
|
+
if (targetId === undefined || kept.length === 0)
|
|
306
|
+
return undefined;
|
|
307
|
+
return { id: targetId, role: message.role, parts: kept };
|
|
308
|
+
}
|
|
309
|
+
function isClientToolResolution(p) {
|
|
310
|
+
return (!!p &&
|
|
311
|
+
typeof p === "object" &&
|
|
312
|
+
isToolPartType(p.type) &&
|
|
313
|
+
typeof p.toolCallId === "string" &&
|
|
314
|
+
isWireAdvanceableToolState(p.state) &&
|
|
315
|
+
p.input === undefined);
|
|
316
|
+
}
|
|
317
|
+
function findStoredToolPart(history, toolCallId) {
|
|
318
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
319
|
+
const stored = history[i];
|
|
320
|
+
if (!stored || stored.role !== "assistant")
|
|
321
|
+
continue;
|
|
322
|
+
for (const part of (stored.parts ?? [])) {
|
|
323
|
+
if (part && isToolPartType(part.type) && part.toolCallId === toolCallId) {
|
|
324
|
+
return { messageId: stored.id, part };
|
|
325
|
+
}
|
|
150
326
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
327
|
+
}
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
function isAllowedResolution(stored, incoming) {
|
|
331
|
+
if (incoming.state === "approval-responded" || incoming.state === "output-denied") {
|
|
332
|
+
return (stored.state === "approval-requested" &&
|
|
333
|
+
typeof stored.approval?.id === "string" &&
|
|
334
|
+
stored.approval.id === incoming.approval?.id);
|
|
335
|
+
}
|
|
336
|
+
return stored.state === "input-available" || stored.state === "approval-responded";
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Remove answered tool parts that carry neither `input` nor `rawInput` from a history the agent did
|
|
340
|
+
* not build itself, such as the chain a `hydrateMessages` or `loadContext` hook returns. Every call the
|
|
341
|
+
* agent made carries one of the two, so such a part is a client's resolution that was copied into the
|
|
342
|
+
* chain without answering a stored call, and an approved one would otherwise run its tool. Assistant
|
|
343
|
+
* messages left with no parts are dropped.
|
|
344
|
+
*
|
|
345
|
+
* @internal
|
|
346
|
+
*/
|
|
347
|
+
function dropUnbackedToolResolutions(messages) {
|
|
348
|
+
const out = [];
|
|
349
|
+
for (const message of messages) {
|
|
350
|
+
if (!message || message.role !== "assistant" || !Array.isArray(message.parts)) {
|
|
351
|
+
out.push(message);
|
|
352
|
+
continue;
|
|
155
353
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
base.approval = p.approval;
|
|
354
|
+
const parts = message.parts;
|
|
355
|
+
const kept = parts.filter((p) => !(isClientToolResolution(p) && p.rawInput === undefined));
|
|
356
|
+
if (kept.length === parts.length) {
|
|
357
|
+
out.push(message);
|
|
161
358
|
}
|
|
162
|
-
else if (
|
|
163
|
-
|
|
164
|
-
base.approval = p.approval;
|
|
359
|
+
else if (kept.length > 0) {
|
|
360
|
+
out.push({ ...message, parts: kept });
|
|
165
361
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return {
|
|
169
|
-
id: message.id,
|
|
170
|
-
role: message.role,
|
|
171
|
-
parts: slimParts,
|
|
172
|
-
};
|
|
362
|
+
}
|
|
363
|
+
return out;
|
|
173
364
|
}
|
|
174
365
|
/** Run tags accept at most 10 entries of at most 128 characters each. */
|
|
175
366
|
const MAX_RUN_TAGS = 10;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG
|
|
1
|
+
{"version":3,"file":"ai-shared.js","sourceRoot":"","sources":["../../../src/v3/ai-shared.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;;;;;;;;;;AAKH;;;GAGG;AACU,QAAA,6BAA6B,GAAG,+BAAwC,CAAC;AAEtF;;;;GAIG;AACU,QAAA,yBAAyB,GAAG,0BAAmC,CAAC;AA0D7E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,+BACE,MAAc,EACd,KAGC;IAED,IAAI,KAAK,CAAC,OAAO,KAAK,gBAAgB;QAAE,OAAO,KAAK,CAAC;IACrD,IAAI,KAAK,CAAC,gBAAgB,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACtD,MAAM,MAAM,GAAG,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,gBAAgB,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E,KAAK,CAAC,CAAC;QAC1D,KAAK,GAAG,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC;QAChE,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QACvC,IAAI,MAAM,CAAC,OAAO;YAAE,IAAI,EAAE,CAAC;IAC7B,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/commonjs/v3/ai.d.ts
CHANGED
|
@@ -3579,6 +3579,10 @@ declare class ChatMessageAccumulator {
|
|
|
3579
3579
|
/**
|
|
3580
3580
|
* Add incoming messages from the transport payload.
|
|
3581
3581
|
* Returns the full accumulated model messages for `streamText`.
|
|
3582
|
+
*
|
|
3583
|
+
* A tool approval or tool output from the client (a tool part with no `input`) is merged onto the
|
|
3584
|
+
* stored call it answers, found in the accumulator or earlier in `messages`. One that answers no
|
|
3585
|
+
* pending call is dropped.
|
|
3582
3586
|
*/
|
|
3583
3587
|
addIncoming(messages: UIMessage[], trigger: string, turn: number): Promise<ModelMessage[]>;
|
|
3584
3588
|
/**
|
package/dist/commonjs/v3/ai.js
CHANGED
|
@@ -439,8 +439,10 @@ async function replaySessionInTailWithChannels(sessionId, options) {
|
|
|
439
439
|
const payload = chunk.payload;
|
|
440
440
|
if (!payload || payload.trigger !== "submit-message")
|
|
441
441
|
continue;
|
|
442
|
-
const message = payload.message
|
|
443
|
-
|
|
442
|
+
const message = payload.message && typeof payload.message === "object"
|
|
443
|
+
? (0, ai_shared_js_1.sanitizeClientMessage)(payload.message)
|
|
444
|
+
: undefined;
|
|
445
|
+
if (!message) {
|
|
444
446
|
if (payload.channelEvent && typeof payload.channelEvent === "object") {
|
|
445
447
|
channelEvents.push({
|
|
446
448
|
channelEvent: payload.channelEvent,
|
|
@@ -1825,7 +1827,7 @@ async function installChatInputRouter(chatId, options) {
|
|
|
1825
1827
|
// re-deliver below it and a replayable route would re-queue it.
|
|
1826
1828
|
if (floor !== undefined && record.seqNum <= floor)
|
|
1827
1829
|
return true;
|
|
1828
|
-
router.ingest(record);
|
|
1830
|
+
router.ingest(sanitizeChatInputRecord(record));
|
|
1829
1831
|
return true;
|
|
1830
1832
|
});
|
|
1831
1833
|
entry.attached = true;
|
|
@@ -1853,6 +1855,21 @@ function chatInputRouterEntry(chatId) {
|
|
|
1853
1855
|
};
|
|
1854
1856
|
return currentChatInputRouter;
|
|
1855
1857
|
}
|
|
1858
|
+
/**
|
|
1859
|
+
* Reduce the client-supplied message on a `kind: "message"` record to what a client may send, before
|
|
1860
|
+
* any consumer sees it. See `sanitizeClientMessage`.
|
|
1861
|
+
* @internal
|
|
1862
|
+
*/
|
|
1863
|
+
function sanitizeChatInputRecord(record) {
|
|
1864
|
+
const chunk = record.data;
|
|
1865
|
+
if (!chunk || typeof chunk !== "object" || chunk.kind !== "message")
|
|
1866
|
+
return record;
|
|
1867
|
+
const payload = chunk.payload;
|
|
1868
|
+
if (!payload || typeof payload !== "object")
|
|
1869
|
+
return record;
|
|
1870
|
+
const sanitized = (0, ai_shared_js_1.sanitizeClientWirePayload)(payload);
|
|
1871
|
+
return sanitized === payload ? record : { ...record, data: { ...chunk, payload: sanitized } };
|
|
1872
|
+
}
|
|
1856
1873
|
/** Drop the router so the next boot attaches a fresh one. @internal */
|
|
1857
1874
|
function __resetChatInputRouterForTests() {
|
|
1858
1875
|
currentChatInputRouter = undefined;
|
|
@@ -2050,54 +2067,6 @@ const chatChannelDownloadKey = locals_js_1.locals.create("chat.channelDownload")
|
|
|
2050
2067
|
const chatUIStreamStaticKey = locals_js_1.locals.create("chat.uiMessageStreamOptions.static");
|
|
2051
2068
|
/** Per-turn UIMessageStream options, set via chat.setUIMessageStreamOptions(). @internal */
|
|
2052
2069
|
const chatUIStreamPerTurnKey = locals_js_1.locals.create("chat.uiMessageStreamOptions.perTurn");
|
|
2053
|
-
/**
|
|
2054
|
-
* Run-scoped `toolCallId → assistant messageId` map. Records the head
|
|
2055
|
-
* assistant id whenever the accumulator absorbs an assistant message
|
|
2056
|
-
* containing tool parts. Used as a fallback in the id-merge for
|
|
2057
|
-
* incoming tool-answer messages — if the AI SDK regenerates the
|
|
2058
|
-
* assistant id on a HITL `addToolOutput` resume, we look up the
|
|
2059
|
-
* original head id by `toolCallId` and rewrite it before the merge.
|
|
2060
|
-
*
|
|
2061
|
-
* Customer-side workaround for the same case is documented in Arena
|
|
2062
|
-
* AI's chat-agent task; lifting it into the SDK so customers don't
|
|
2063
|
-
* have to. See TRI-9137.
|
|
2064
|
-
* @internal
|
|
2065
|
-
*/
|
|
2066
|
-
const chatToolCallToMessageIdKey = locals_js_1.locals.create("chat.toolCallToMessageId");
|
|
2067
|
-
function recordToolCallIdsFromMessage(message) {
|
|
2068
|
-
if (!message || message.role !== "assistant" || !message.id)
|
|
2069
|
-
return;
|
|
2070
|
-
let map = locals_js_1.locals.get(chatToolCallToMessageIdKey);
|
|
2071
|
-
if (!map) {
|
|
2072
|
-
map = new Map();
|
|
2073
|
-
locals_js_1.locals.set(chatToolCallToMessageIdKey, map);
|
|
2074
|
-
}
|
|
2075
|
-
for (const part of message.parts ?? []) {
|
|
2076
|
-
if (typeof part !== "object" || part == null)
|
|
2077
|
-
continue;
|
|
2078
|
-
const toolCallId = part.toolCallId;
|
|
2079
|
-
if (typeof toolCallId === "string" && toolCallId.length > 0) {
|
|
2080
|
-
map.set(toolCallId, message.id);
|
|
2081
|
-
}
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
function rewriteIncomingIdViaToolCallMap(incoming) {
|
|
2085
|
-
const map = locals_js_1.locals.get(chatToolCallToMessageIdKey);
|
|
2086
|
-
if (!map || map.size === 0)
|
|
2087
|
-
return incoming;
|
|
2088
|
-
for (const part of incoming.parts ?? []) {
|
|
2089
|
-
if (typeof part !== "object" || part == null)
|
|
2090
|
-
continue;
|
|
2091
|
-
const toolCallId = part.toolCallId;
|
|
2092
|
-
if (typeof toolCallId !== "string" || toolCallId.length === 0)
|
|
2093
|
-
continue;
|
|
2094
|
-
const headId = map.get(toolCallId);
|
|
2095
|
-
if (headId && headId !== incoming.id) {
|
|
2096
|
-
return { ...incoming, id: headId };
|
|
2097
|
-
}
|
|
2098
|
-
}
|
|
2099
|
-
return incoming;
|
|
2100
|
-
}
|
|
2101
2070
|
function emptyUsage() {
|
|
2102
2071
|
return {
|
|
2103
2072
|
inputTokens: undefined,
|
|
@@ -2390,6 +2359,31 @@ function mergeIncomingIntoHydrated(hydrated, incoming) {
|
|
|
2390
2359
|
return hydrated;
|
|
2391
2360
|
return { ...hydrated, parts: mergedParts };
|
|
2392
2361
|
}
|
|
2362
|
+
/**
|
|
2363
|
+
* Keep the incoming messages a client may add this turn: user messages, and tool resolutions that
|
|
2364
|
+
* answer a call in `history`, re-addressed to the stored message holding it. Anything else is dropped.
|
|
2365
|
+
*
|
|
2366
|
+
* @internal
|
|
2367
|
+
*/
|
|
2368
|
+
function resolveIncomingAgainstHistory(incoming, history) {
|
|
2369
|
+
const out = [];
|
|
2370
|
+
for (const message of incoming) {
|
|
2371
|
+
const resolved = message.role === "user"
|
|
2372
|
+
? message
|
|
2373
|
+
: message.role === "assistant"
|
|
2374
|
+
? (0, ai_shared_js_1.resolveClientToolResolution)(message, history)
|
|
2375
|
+
: undefined;
|
|
2376
|
+
if (resolved) {
|
|
2377
|
+
out.push(resolved);
|
|
2378
|
+
continue;
|
|
2379
|
+
}
|
|
2380
|
+
v3_1.logger.warn("chat.agent: dropped an incoming message that answers no pending tool call", {
|
|
2381
|
+
messageId: message.id,
|
|
2382
|
+
role: message.role,
|
|
2383
|
+
});
|
|
2384
|
+
}
|
|
2385
|
+
return out;
|
|
2386
|
+
}
|
|
2393
2387
|
/**
|
|
2394
2388
|
* Mirror of `slimSubmitMessageForWire`'s predicate. Kept here so the
|
|
2395
2389
|
* agent runtime doesn't have to import from `ai-shared.ts` for a
|
|
@@ -4825,6 +4819,7 @@ function chatCustomAgent(options) {
|
|
|
4825
4819
|
triggerSource: "agent",
|
|
4826
4820
|
agentConfig: { type: "ai-sdk-chat" },
|
|
4827
4821
|
run: async (payload, runOptions) => {
|
|
4822
|
+
payload = (0, ai_shared_js_1.sanitizeClientWirePayload)(payload);
|
|
4828
4823
|
// Bind the run to its backing Session so module-level helpers
|
|
4829
4824
|
// (chat.messages, chat.stream, chat.createStopSignal, chat.createSession)
|
|
4830
4825
|
// resolve to this chat's `.in` / `.out` channels. Address
|
|
@@ -4970,6 +4965,7 @@ function chatAgent(options) {
|
|
|
4970
4965
|
triggerSource: "agent",
|
|
4971
4966
|
agentConfig: { type: "ai-sdk-chat" },
|
|
4972
4967
|
run: async (payload, { signal: runSignal, ctx }) => {
|
|
4968
|
+
payload = (0, ai_shared_js_1.sanitizeClientWirePayload)(payload);
|
|
4973
4969
|
locals_js_1.locals.set(chatAgentRunContextKey, ctx);
|
|
4974
4970
|
locals_js_1.locals.set(chatCustomAgentRunKey, false);
|
|
4975
4971
|
// On AI SDK 7, register the `@ai-sdk/otel` integration (once per process)
|
|
@@ -5604,6 +5600,7 @@ function chatAgent(options) {
|
|
|
5604
5600
|
const oldestTailSeq = Math.min(...replayedInTail.map((r) => r.seqNum), ...replayedChannelInTail.map((r) => r.seqNum));
|
|
5605
5601
|
const partialFollowsBrowserMessage = partialAssistant !== undefined &&
|
|
5606
5602
|
inFlightUsers.length > 1 &&
|
|
5603
|
+
inFlightUsers[0].role === "user" &&
|
|
5607
5604
|
(replayedChannelInTail.length === 0 || replayedInTail[0]?.seqNum === oldestTailSeq);
|
|
5608
5605
|
if (hookChain !== undefined) {
|
|
5609
5606
|
seedChain = hookChain;
|
|
@@ -6245,7 +6242,9 @@ function chatAgent(options) {
|
|
|
6245
6242
|
const { metadata: wireMetadata, message: incomingMessage, headStartMessages: _hsm, channelEvent: wireChannelEvent, ...restWire } = currentWirePayload;
|
|
6246
6243
|
void _hsm;
|
|
6247
6244
|
channelWireEvent = wireChannelEvent;
|
|
6248
|
-
let effectiveIncomingMessage = incomingMessage
|
|
6245
|
+
let effectiveIncomingMessage = incomingMessage === undefined
|
|
6246
|
+
? undefined
|
|
6247
|
+
: (0, ai_shared_js_1.sanitizeClientMessage)(incomingMessage);
|
|
6249
6248
|
const clientData = (parseClientData ? await parseClientData(wireMetadata) : wireMetadata);
|
|
6250
6249
|
turnClientData = clientData;
|
|
6251
6250
|
turnIsActionTurn = false;
|
|
@@ -6703,7 +6702,7 @@ function chatAgent(options) {
|
|
|
6703
6702
|
// with the new user message in `.message`, or no message
|
|
6704
6703
|
// at all (regenerate / preload / close / handover-prepare).
|
|
6705
6704
|
const lastUIMessage = (steeredChannel?.message ?? msg.message);
|
|
6706
|
-
if (lastUIMessage) {
|
|
6705
|
+
if (lastUIMessage && lastUIMessage.role === "user") {
|
|
6707
6706
|
if (pmConfig.onReceived) {
|
|
6708
6707
|
try {
|
|
6709
6708
|
await pmConfig.onReceived({
|
|
@@ -6911,6 +6910,9 @@ function chatAgent(options) {
|
|
|
6911
6910
|
payload.headStartMessages.length > 0) {
|
|
6912
6911
|
cleanedUIMessages = payload.headStartMessages;
|
|
6913
6912
|
}
|
|
6913
|
+
if (!loadContextHook) {
|
|
6914
|
+
cleanedUIMessages = resolveIncomingAgainstHistory(cleanedUIMessages, accumulatedUIMessages);
|
|
6915
|
+
}
|
|
6914
6916
|
// Validate/transform UIMessages before conversion — catches malformed
|
|
6915
6917
|
// messages from storage or untrusted input before they reach the model.
|
|
6916
6918
|
// Slim wire: triggers like `regenerate-message` carry no incoming
|
|
@@ -6980,8 +6982,10 @@ function chatAgent(options) {
|
|
|
6980
6982
|
// copy to fit the .in/append cap (or drop fields they
|
|
6981
6983
|
// re-source from their own DB) get the hydrated copy
|
|
6982
6984
|
// through unchanged.
|
|
6983
|
-
const
|
|
6984
|
-
|
|
6985
|
+
const hydratedChain = (0, ai_shared_js_1.dropUnbackedToolResolutions)(hydrated);
|
|
6986
|
+
const resolvedIncoming = resolveIncomingAgainstHistory(cleanedUIMessages, hydratedChain);
|
|
6987
|
+
const merged = [...hydratedChain];
|
|
6988
|
+
for (const incoming of resolvedIncoming) {
|
|
6985
6989
|
if (!incoming.id)
|
|
6986
6990
|
continue;
|
|
6987
6991
|
const idx = merged.findIndex((m) => m.id === incoming.id);
|
|
@@ -7008,8 +7012,8 @@ function chatAgent(options) {
|
|
|
7008
7012
|
// the same "push only on append" semantic via its
|
|
7009
7013
|
// own append-vs-replace path.
|
|
7010
7014
|
if (currentWirePayload.trigger === "submit-message" &&
|
|
7011
|
-
|
|
7012
|
-
const lastUI =
|
|
7015
|
+
resolvedIncoming.length > 0) {
|
|
7016
|
+
const lastUI = resolvedIncoming[resolvedIncoming.length - 1];
|
|
7013
7017
|
const matchedExisting = lastUI.id !== undefined && previouslyKnownMessageIds.has(lastUI.id);
|
|
7014
7018
|
if (!matchedExisting) {
|
|
7015
7019
|
turnNewUIMessages.push(lastUI);
|
|
@@ -7052,21 +7056,10 @@ function chatAgent(options) {
|
|
|
7052
7056
|
// in the accumulator — we merge the resolved tool-part
|
|
7053
7057
|
// resolutions onto the existing entry, keeping text,
|
|
7054
7058
|
// reasoning, and tool `input` from the prior snapshot.
|
|
7055
|
-
// The fallback for HITL `addToolOutput` continuations
|
|
7056
|
-
// where AI SDK regenerates the id (TRI-9137) still
|
|
7057
|
-
// applies via `rewriteIncomingIdViaToolCallMap`.
|
|
7058
7059
|
let replaced = false;
|
|
7059
7060
|
const replacedPairs = [];
|
|
7060
|
-
for (const
|
|
7061
|
-
|
|
7062
|
-
let idx = accumulatedUIMessages.findIndex((m) => m.id === incoming.id);
|
|
7063
|
-
if (idx === -1) {
|
|
7064
|
-
const rewritten = rewriteIncomingIdViaToolCallMap(incoming);
|
|
7065
|
-
if (rewritten.id !== incoming.id) {
|
|
7066
|
-
incoming = rewritten;
|
|
7067
|
-
idx = accumulatedUIMessages.findIndex((m) => m.id === incoming.id);
|
|
7068
|
-
}
|
|
7069
|
-
}
|
|
7061
|
+
for (const incoming of cleanedUIMessages) {
|
|
7062
|
+
const idx = accumulatedUIMessages.findIndex((m) => m.id === incoming.id);
|
|
7070
7063
|
if (idx !== -1) {
|
|
7071
7064
|
const previous = accumulatedUIMessages[idx];
|
|
7072
7065
|
accumulatedUIMessages[idx] = mergeIncomingIntoHydrated(previous, incoming);
|
|
@@ -7077,7 +7070,6 @@ function chatAgent(options) {
|
|
|
7077
7070
|
accumulatedUIMessages.push(incoming);
|
|
7078
7071
|
turnNewUIMessages.push(incoming);
|
|
7079
7072
|
}
|
|
7080
|
-
recordToolCallIdsFromMessage(incoming);
|
|
7081
7073
|
}
|
|
7082
7074
|
if (replaced) {
|
|
7083
7075
|
let inPlace = true;
|
|
@@ -7785,12 +7777,6 @@ function chatAgent(options) {
|
|
|
7785
7777
|
}
|
|
7786
7778
|
turnNewUIMessages.push(capturedResponseMessage);
|
|
7787
7779
|
locals_js_1.locals.set(chatCurrentUIMessagesKey, accumulatedUIMessages);
|
|
7788
|
-
// Record toolCallId → head messageId so a HITL
|
|
7789
|
-
// continuation next turn can recover the head id
|
|
7790
|
-
// even if the AI SDK regenerates it. See
|
|
7791
|
-
// `chatToolCallToMessageIdKey` for the full
|
|
7792
|
-
// rationale (TRI-9137).
|
|
7793
|
-
recordToolCallIdsFromMessage(capturedResponseMessage);
|
|
7794
7780
|
try {
|
|
7795
7781
|
const responseForModel = (0, compactionResponse_js_1.responseAfterCompaction)(capturedResponseMessage, responseCompaction?.baseResponseStepCount, originalResponse);
|
|
7796
7782
|
// Preserve the complete persistence response, including same-ID
|
|
@@ -9944,18 +9930,42 @@ class ChatMessageAccumulator {
|
|
|
9944
9930
|
/**
|
|
9945
9931
|
* Add incoming messages from the transport payload.
|
|
9946
9932
|
* Returns the full accumulated model messages for `streamText`.
|
|
9933
|
+
*
|
|
9934
|
+
* A tool approval or tool output from the client (a tool part with no `input`) is merged onto the
|
|
9935
|
+
* stored call it answers, found in the accumulator or earlier in `messages`. One that answers no
|
|
9936
|
+
* pending call is dropped.
|
|
9947
9937
|
*/
|
|
9948
9938
|
async addIncoming(messages, trigger, turn) {
|
|
9949
|
-
const
|
|
9950
|
-
const
|
|
9951
|
-
|
|
9952
|
-
|
|
9953
|
-
|
|
9954
|
-
|
|
9955
|
-
|
|
9956
|
-
|
|
9957
|
-
|
|
9939
|
+
const replace = turn === 0 || trigger === "regenerate-message";
|
|
9940
|
+
const ui = replace ? [] : [...this.uiMessages];
|
|
9941
|
+
const lane = replace ? [] : [...this.modelMessages];
|
|
9942
|
+
let appendedModelCount = 0;
|
|
9943
|
+
let rebuild = replace;
|
|
9944
|
+
for (const raw of messages) {
|
|
9945
|
+
const message = raw.role === "assistant" ? cleanupAbortedParts(raw) : raw;
|
|
9946
|
+
const resolved = (0, ai_shared_js_1.resolveClientToolResolution)(message, ui);
|
|
9947
|
+
if (!resolved) {
|
|
9948
|
+
v3_1.logger.warn("chat.MessageAccumulator: dropped an incoming message that answers no pending tool call", { messageId: message.id });
|
|
9949
|
+
continue;
|
|
9950
|
+
}
|
|
9951
|
+
if (resolved !== message) {
|
|
9952
|
+
const idx = ui.findIndex((m) => m.id === resolved.id);
|
|
9953
|
+
const previous = ui[idx];
|
|
9954
|
+
ui[idx] = mergeIncomingIntoHydrated(previous, resolved);
|
|
9955
|
+
if (!rebuild && !(await replaceModelRun(lane, previous, ui[idx], appendedModelCount))) {
|
|
9956
|
+
rebuild = true;
|
|
9957
|
+
}
|
|
9958
|
+
continue;
|
|
9959
|
+
}
|
|
9960
|
+
ui.push(message);
|
|
9961
|
+
if (!rebuild) {
|
|
9962
|
+
const converted = await toModelMessages([message]);
|
|
9963
|
+
lane.push(...converted);
|
|
9964
|
+
appendedModelCount += converted.length;
|
|
9965
|
+
}
|
|
9958
9966
|
}
|
|
9967
|
+
this.uiMessages = ui;
|
|
9968
|
+
this.modelMessages = rebuild ? await toModelMessages(ui) : lane;
|
|
9959
9969
|
return this.modelMessages;
|
|
9960
9970
|
}
|
|
9961
9971
|
/**
|
|
@@ -10477,7 +10487,7 @@ function createChatSession(payload, options) {
|
|
|
10477
10487
|
*/
|
|
10478
10488
|
const handleSteeringMessage = async (msg, seqNum, isActive = () => true) => {
|
|
10479
10489
|
const lastUIMessage = msg.message;
|
|
10480
|
-
if (lastUIMessage) {
|
|
10490
|
+
if (lastUIMessage && lastUIMessage.role === "user") {
|
|
10481
10491
|
if (sessionPendingMessages?.onReceived) {
|
|
10482
10492
|
try {
|
|
10483
10493
|
await sessionPendingMessages.onReceived({
|