Skip to content

Commit e083f75

Browse files
committed
feat(chat): allow generateMessageId in uiMessageStreamOptions, auto-pass originalMessages
1 parent 0f27792 commit e083f75

File tree

1 file changed

+16
-6
lines changed
  • packages/trigger-sdk/src/v3

1 file changed

+16
-6
lines changed

packages/trigger-sdk/src/v3/ai.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,14 +1756,20 @@ export type PipeChatOptions = {
17561756
* Set static defaults via `uiMessageStreamOptions` on `chat.agent()`, or
17571757
* override per-turn via `chat.setUIMessageStreamOptions()`.
17581758
*
1759-
* `onFinish`, `originalMessages`, and `generateMessageId` are omitted because
1760-
* they are managed internally for response capture and message accumulation.
1759+
* `onFinish` is omitted because it is managed internally for response capture.
17611760
* Use `streamText`'s `onFinish` for custom finish handling, or drop down to
17621761
* raw task mode with `chat.pipe()` for full control.
1762+
*
1763+
* `originalMessages` is omitted because it is automatically set from the
1764+
* accumulated conversation history, ensuring message IDs are reused across
1765+
* turns (e.g. for tool approval continuations).
1766+
*
1767+
* `generateMessageId` can be set to control ID generation for response
1768+
* messages (e.g. UUID-v7). If not set, the AI SDK's default `generateId` is used.
17631769
*/
17641770
export type ChatUIMessageStreamOptions<TUIM extends UIMessage = UIMessage> = Omit<
17651771
UIMessageStreamOptions<TUIM>,
1766-
"onFinish" | "originalMessages" | "generateMessageId"
1772+
"onFinish" | "originalMessages"
17671773
>;
17681774

17691775
/**
@@ -2429,9 +2435,10 @@ export type ChatAgentOptions<
24292435
* inside `run()` or lifecycle hooks. Per-turn values are merged on top
24302436
* of these defaults (per-turn wins on conflicts).
24312437
*
2432-
* `onFinish`, `originalMessages`, and `generateMessageId` are managed
2433-
* internally and cannot be overridden here. Use `streamText`'s `onFinish`
2434-
* for custom finish handling, or drop to raw task mode for full control.
2438+
* `onFinish` and `originalMessages` are managed internally and cannot be
2439+
* overridden here. Use `streamText`'s `onFinish` for custom finish
2440+
* handling. `generateMessageId` can be set to control response message
2441+
* ID generation (e.g. UUID-v7).
24352442
*
24362443
* @example
24372444
* ```ts
@@ -3174,10 +3181,13 @@ function chatAgent<
31743181
// Auto-pipe if the run function returned a StreamTextResult or similar,
31753182
// but only if pipeChat() wasn't already called manually during this turn.
31763183
// We call toUIMessageStream ourselves to attach onFinish for response capture.
3184+
// Pass originalMessages so the AI SDK reuses message IDs across turns
3185+
// (e.g. for tool approval continuations / HITL flows).
31773186
if ((locals.get(chatPipeCountKey) ?? 0) === 0 && isUIMessageStreamable(runResult)) {
31783187
onFinishAttached = true;
31793188
const uiStream = runResult.toUIMessageStream({
31803189
...resolveUIMessageStreamOptions(),
3190+
originalMessages: accumulatedUIMessages,
31813191
onFinish: ({ responseMessage }: { responseMessage: UIMessage }) => {
31823192
capturedResponseMessage = responseMessage as TUIMessage;
31833193
resolveOnFinish!();

0 commit comments

Comments
 (0)