@ulearning/ai-sdk-adapter 0.0.0-stage → 1.0.0-next.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.
- package/LICENSE +21 -0
- package/README.md +29 -2
- package/dist/adapters/aiSDKFormatAdapter.d.ts +6 -0
- package/dist/adapters/aiSDKFormatAdapter.d.ts.map +1 -0
- package/dist/adapters/aiSDKFormatAdapter.js +17 -0
- package/dist/adapters/aiSDKFormatAdapter.js.map +1 -0
- package/dist/adapters/vercelAttachmentAdapter.d.ts +3 -0
- package/dist/adapters/vercelAttachmentAdapter.d.ts.map +1 -0
- package/dist/adapters/vercelAttachmentAdapter.js +36 -0
- package/dist/adapters/vercelAttachmentAdapter.js.map +1 -0
- package/dist/aiSDKExtras.d.ts +13 -0
- package/dist/aiSDKExtras.d.ts.map +1 -0
- package/dist/aiSDKExtras.js +6 -0
- package/dist/aiSDKExtras.js.map +1 -0
- package/dist/converters/convertMessage.d.ts +44 -0
- package/dist/converters/convertMessage.d.ts.map +1 -0
- package/dist/converters/convertMessage.js +465 -0
- package/dist/converters/convertMessage.js.map +1 -0
- package/dist/converters/toCreateMessage.d.ts +4 -0
- package/dist/converters/toCreateMessage.d.ts.map +1 -0
- package/dist/converters/toCreateMessage.js +65 -0
- package/dist/converters/toCreateMessage.js.map +1 -0
- package/dist/converters/toolApprovalAnswers.d.ts +3 -0
- package/dist/converters/toolApprovalAnswers.d.ts.map +1 -0
- package/dist/converters/toolApprovalAnswers.js +16 -0
- package/dist/converters/toolApprovalAnswers.js.map +1 -0
- package/dist/hooks.d.ts +17 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +19 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/model-context/injectInteractableContext.d.ts +37 -0
- package/dist/model-context/injectInteractableContext.d.ts.map +1 -0
- package/dist/model-context/injectInteractableContext.js +54 -0
- package/dist/model-context/injectInteractableContext.js.map +1 -0
- package/dist/model-context/injectQuoteContext.d.ts +25 -0
- package/dist/model-context/injectQuoteContext.d.ts.map +1 -0
- package/dist/model-context/injectQuoteContext.js +51 -0
- package/dist/model-context/injectQuoteContext.js.map +1 -0
- package/dist/runtime/AISDKChat.d.ts +13 -0
- package/dist/runtime/AISDKChat.d.ts.map +1 -0
- package/dist/runtime/AISDKChat.js +34 -0
- package/dist/runtime/AISDKChat.js.map +1 -0
- package/dist/runtime/AISDKThreads.d.ts +24 -0
- package/dist/runtime/AISDKThreads.d.ts.map +1 -0
- package/dist/runtime/AISDKThreads.js +92 -0
- package/dist/runtime/AISDKThreads.js.map +1 -0
- package/dist/runtime/DynamicChatTransport.d.ts +50 -0
- package/dist/runtime/DynamicChatTransport.d.ts.map +1 -0
- package/dist/runtime/DynamicChatTransport.js +153 -0
- package/dist/runtime/DynamicChatTransport.js.map +1 -0
- package/dist/runtime/getResumableAdapter.d.ts +5 -0
- package/dist/runtime/getResumableAdapter.d.ts.map +1 -0
- package/dist/runtime/getResumableAdapter.js +11 -0
- package/dist/runtime/getResumableAdapter.js.map +1 -0
- package/dist/runtime/toolHistoryCodec.d.ts +20 -0
- package/dist/runtime/toolHistoryCodec.d.ts.map +1 -0
- package/dist/runtime/toolHistoryCodec.js +122 -0
- package/dist/runtime/toolHistoryCodec.js.map +1 -0
- package/dist/runtime/useAISDKRuntime.d.ts +95 -0
- package/dist/runtime/useAISDKRuntime.d.ts.map +1 -0
- package/dist/runtime/useAISDKRuntime.js +754 -0
- package/dist/runtime/useAISDKRuntime.js.map +1 -0
- package/dist/runtime/useChatRuntime.d.ts +17 -0
- package/dist/runtime/useChatRuntime.d.ts.map +1 -0
- package/dist/runtime/useChatRuntime.js +50 -0
- package/dist/runtime/useChatRuntime.js.map +1 -0
- package/dist/runtime/useChatSnapshot.d.ts +6 -0
- package/dist/runtime/useChatSnapshot.d.ts.map +1 -0
- package/dist/runtime/useChatSnapshot.js +62 -0
- package/dist/runtime/useChatSnapshot.js.map +1 -0
- package/dist/runtime/useChatThread.d.ts +121 -0
- package/dist/runtime/useChatThread.d.ts.map +1 -0
- package/dist/runtime/useChatThread.js +255 -0
- package/dist/runtime/useChatThread.js.map +1 -0
- package/dist/runtime/useDynamicChatTransport.d.ts +4 -0
- package/dist/runtime/useDynamicChatTransport.d.ts.map +1 -0
- package/dist/runtime/useDynamicChatTransport.js +26 -0
- package/dist/runtime/useDynamicChatTransport.js.map +1 -0
- package/dist/runtime/useExternalHistory.d.ts +12 -0
- package/dist/runtime/useExternalHistory.d.ts.map +1 -0
- package/dist/runtime/useExternalHistory.js +556 -0
- package/dist/runtime/useExternalHistory.js.map +1 -0
- package/dist/runtime/useMessageQueue.d.ts +14 -0
- package/dist/runtime/useMessageQueue.d.ts.map +1 -0
- package/dist/runtime/useMessageQueue.js +142 -0
- package/dist/runtime/useMessageQueue.js.map +1 -0
- package/dist/runtime/useResourceCleanup.d.ts +2 -0
- package/dist/runtime/useResourceCleanup.d.ts.map +1 -0
- package/dist/runtime/useResourceCleanup.js +47 -0
- package/dist/runtime/useResourceCleanup.js.map +1 -0
- package/dist/runtime/useStreamingTiming.d.ts +12 -0
- package/dist/runtime/useStreamingTiming.d.ts.map +1 -0
- package/dist/runtime/useStreamingTiming.js +36 -0
- package/dist/runtime/useStreamingTiming.js.map +1 -0
- package/dist/tools/generativeTools.d.ts +71 -0
- package/dist/tools/generativeTools.d.ts.map +1 -0
- package/dist/tools/generativeTools.js +258 -0
- package/dist/tools/generativeTools.js.map +1 -0
- package/dist/tools/mcp-stdio.node.d.ts +2 -0
- package/dist/tools/mcp-stdio.node.d.ts.map +1 -0
- package/dist/tools/mcp-stdio.node.js +1 -0
- package/dist/tools/mcp-stdio.unsupported.d.ts +3 -0
- package/dist/tools/mcp-stdio.unsupported.d.ts.map +1 -0
- package/dist/tools/mcp-stdio.unsupported.js +10 -0
- package/dist/tools/mcp-stdio.unsupported.js.map +1 -0
- package/dist/transport/AssistantChatTransport.d.ts +20 -0
- package/dist/transport/AssistantChatTransport.d.ts.map +1 -0
- package/dist/transport/AssistantChatTransport.js +151 -0
- package/dist/transport/AssistantChatTransport.js.map +1 -0
- package/dist/transport/resumable.d.ts +36 -0
- package/dist/transport/resumable.d.ts.map +1 -0
- package/dist/transport/resumable.js +112 -0
- package/dist/transport/resumable.js.map +1 -0
- package/dist/usage.d.ts +24 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +121 -0
- package/dist/usage.js.map +1 -0
- package/dist/utils/getVercelAIMessages.d.ts +4 -0
- package/dist/utils/getVercelAIMessages.d.ts.map +1 -0
- package/dist/utils/getVercelAIMessages.js +8 -0
- package/dist/utils/getVercelAIMessages.js.map +1 -0
- package/dist/utils/sliceMessagesUntil.d.ts +3 -0
- package/dist/utils/sliceMessagesUntil.d.ts.map +1 -0
- package/dist/utils/sliceMessagesUntil.js +10 -0
- package/dist/utils/sliceMessagesUntil.js.map +1 -0
- package/package.json +96 -3
- package/src/__tests__/standalone.e2e.ts +118 -0
- package/src/adapters/aiSDKFormatAdapter.test.ts +36 -0
- package/src/adapters/aiSDKFormatAdapter.ts +18 -0
- package/src/adapters/vercelAttachmentAdapter.test.ts +52 -0
- package/src/adapters/vercelAttachmentAdapter.ts +36 -0
- package/src/aiSDKExtras.ts +31 -0
- package/src/converters/convertMessage.metadata-key.test.tsx +182 -0
- package/src/converters/convertMessage.test.ts +2428 -0
- package/src/converters/convertMessage.tool-args-status.test.tsx +185 -0
- package/src/converters/convertMessage.ts +919 -0
- package/src/converters/toCreateMessage.test.ts +716 -0
- package/src/converters/toCreateMessage.ts +110 -0
- package/src/converters/toolApprovalAnswers.ts +27 -0
- package/src/hooks.test.ts +168 -0
- package/src/hooks.ts +26 -0
- package/src/index.ts +29 -0
- package/src/model-context/injectInteractableContext.test.ts +167 -0
- package/src/model-context/injectInteractableContext.ts +64 -0
- package/src/model-context/injectQuoteContext.test.ts +33 -0
- package/src/model-context/injectQuoteContext.ts +63 -0
- package/src/runtime/AISDKChat.artifacts.test.ts +48 -0
- package/src/runtime/AISDKChat.integration.test.tsx +148 -0
- package/src/runtime/AISDKChat.test.ts +241 -0
- package/src/runtime/AISDKChat.ts +43 -0
- package/src/runtime/AISDKThreads.test.ts +811 -0
- package/src/runtime/AISDKThreads.ts +194 -0
- package/src/runtime/DynamicChatTransport.test.ts +228 -0
- package/src/runtime/DynamicChatTransport.ts +281 -0
- package/src/runtime/__tests__/controlled-transport.ts +70 -0
- package/src/runtime/__tests__/tap.ts +80 -0
- package/src/runtime/__tests__/vue.ts +46 -0
- package/src/runtime/getResumableAdapter.ts +16 -0
- package/src/runtime/toolHistoryCodec.test.ts +187 -0
- package/src/runtime/toolHistoryCodec.ts +245 -0
- package/src/runtime/useAISDKRuntime.approval.integration.test.tsx +1956 -0
- package/src/runtime/useAISDKRuntime.approval.test.tsx +580 -0
- package/src/runtime/useAISDKRuntime.denied-tool.test.tsx +122 -0
- package/src/runtime/useAISDKRuntime.fast-refresh.test.tsx +302 -0
- package/src/runtime/useAISDKRuntime.metadata-cache.test.tsx +157 -0
- package/src/runtime/useAISDKRuntime.queue.test.tsx +707 -0
- package/src/runtime/useAISDKRuntime.reload.test.tsx +217 -0
- package/src/runtime/useAISDKRuntime.test.ts +2345 -0
- package/src/runtime/useAISDKRuntime.tool-output.test.tsx +113 -0
- package/src/runtime/useAISDKRuntime.ts +1503 -0
- package/src/runtime/useAISDKRuntime.voice.test.tsx +319 -0
- package/src/runtime/useChatOnTap.test.ts +60 -0
- package/src/runtime/useChatRuntime.fast-refresh.test.tsx +83 -0
- package/src/runtime/useChatRuntime.integration.test.tsx +661 -0
- package/src/runtime/useChatRuntime.local-storage.test.tsx +114 -0
- package/src/runtime/useChatRuntime.native.test.ts +147 -0
- package/src/runtime/useChatRuntime.run-telemetry.test.tsx +226 -0
- package/src/runtime/useChatRuntime.test.ts +619 -0
- package/src/runtime/useChatRuntime.ts +80 -0
- package/src/runtime/useChatSnapshot.test.ts +60 -0
- package/src/runtime/useChatSnapshot.ts +86 -0
- package/src/runtime/useChatThread.binding.test.tsx +140 -0
- package/src/runtime/useChatThread.test.ts +993 -0
- package/src/runtime/useChatThread.transport.test.tsx +149 -0
- package/src/runtime/useChatThread.ts +533 -0
- package/src/runtime/useDynamicChatTransport.ts +26 -0
- package/src/runtime/useExternalHistory.test.ts +4187 -0
- package/src/runtime/useExternalHistory.ts +902 -0
- package/src/runtime/useMessageQueue.effects.test.tsx +88 -0
- package/src/runtime/useMessageQueue.test.tsx +256 -0
- package/src/runtime/useMessageQueue.ts +189 -0
- package/src/runtime/useResourceCleanup.ts +58 -0
- package/src/runtime/useStreamingTiming.test.tsx +96 -0
- package/src/runtime/useStreamingTiming.ts +69 -0
- package/src/tools/generativeTools.test.ts +1058 -0
- package/src/tools/generativeTools.ts +491 -0
- package/src/tools/mcp-stdio.node.ts +1 -0
- package/src/tools/mcp-stdio.unsupported.ts +12 -0
- package/src/transport/AssistantChatTransport.test.ts +631 -0
- package/src/transport/AssistantChatTransport.ts +236 -0
- package/src/transport/resumable.test.ts +262 -0
- package/src/transport/resumable.ts +162 -0
- package/src/usage.test.ts +204 -0
- package/src/usage.ts +179 -0
- package/src/utils/getVercelAIMessages.ts +11 -0
- package/src/utils/sliceMessagesUntil.test.ts +51 -0
- package/src/utils/sliceMessagesUntil.ts +16 -0
|
@@ -0,0 +1,1503 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useInsertionEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
useSyncExternalStore,
|
|
11
|
+
} from "@ulearning/ai-tap";
|
|
12
|
+
import type { UIMessage, CreateUIMessage } from "ai";
|
|
13
|
+
import { isToolUIPart, generateId, getToolName } from "ai";
|
|
14
|
+
import {
|
|
15
|
+
useExternalStoreRuntime,
|
|
16
|
+
type JoinStrategy,
|
|
17
|
+
} from "@ulearning/ai-core/vue";
|
|
18
|
+
import { useReplaySafeEffect } from "@ulearning/ai-store/internal";
|
|
19
|
+
import type {
|
|
20
|
+
SuggestionAdapter,
|
|
21
|
+
ThreadSuggestion,
|
|
22
|
+
ToolExecutionStatus,
|
|
23
|
+
} from "@ulearning/ai-core";
|
|
24
|
+
import type {
|
|
25
|
+
ExternalStoreAdapter,
|
|
26
|
+
ExternalStoreSharedOptions,
|
|
27
|
+
ThreadHistoryAdapter,
|
|
28
|
+
AssistantRuntime,
|
|
29
|
+
ThreadMessage,
|
|
30
|
+
MessageFormatAdapter,
|
|
31
|
+
MessageFormatItem,
|
|
32
|
+
MessageFormatRepository,
|
|
33
|
+
AppendMessage,
|
|
34
|
+
RunConfig,
|
|
35
|
+
McpAppMetadata,
|
|
36
|
+
RespondToToolApprovalOptions,
|
|
37
|
+
Unstable_ToolInteractionLog,
|
|
38
|
+
} from "@ulearning/ai-core";
|
|
39
|
+
import {
|
|
40
|
+
getExternalStoreMessages,
|
|
41
|
+
pickExternalStoreSharedOptions,
|
|
42
|
+
} from "@ulearning/ai-core";
|
|
43
|
+
import {
|
|
44
|
+
useRuntimeResource,
|
|
45
|
+
useRuntimeAdaptersValue,
|
|
46
|
+
useExternalMessageConverterValue,
|
|
47
|
+
useStreamingTimingValue,
|
|
48
|
+
} from "@ulearning/ai-core/vue/internal";
|
|
49
|
+
import {
|
|
50
|
+
appendToolInteraction,
|
|
51
|
+
consumeSuggestionResult,
|
|
52
|
+
MessageRepository,
|
|
53
|
+
} from "@ulearning/ai-core/internal";
|
|
54
|
+
import type { ReadonlyJSONObject } from "@ulearning/ai-stream/utils";
|
|
55
|
+
import type { AssistantError } from "@ulearning/ai-core";
|
|
56
|
+
import { sliceMessagesUntil } from "../utils/sliceMessagesUntil";
|
|
57
|
+
import { toCreateMessage } from "../converters/toCreateMessage";
|
|
58
|
+
import { vercelAttachmentAdapter } from "../adapters/vercelAttachmentAdapter";
|
|
59
|
+
import { getVercelAIMessages } from "../utils/getVercelAIMessages";
|
|
60
|
+
import {
|
|
61
|
+
AISDKMessageConverter,
|
|
62
|
+
convertAISDKMessage,
|
|
63
|
+
createAISDKMetadataKey,
|
|
64
|
+
type AISDKMessageConverterMetadata,
|
|
65
|
+
} from "../converters/convertMessage";
|
|
66
|
+
import { wrapModelContentEnvelope } from "@ulearning/ai-stream/internal";
|
|
67
|
+
import {
|
|
68
|
+
type AISDKStorageFormat,
|
|
69
|
+
aiSDKV6FormatAdapter,
|
|
70
|
+
} from "../adapters/aiSDKFormatAdapter";
|
|
71
|
+
import {
|
|
72
|
+
useExternalHistory,
|
|
73
|
+
toExportedMessageRepository,
|
|
74
|
+
} from "./useExternalHistory";
|
|
75
|
+
import { aiSdkStreamingTimingAccessors } from "./useStreamingTiming";
|
|
76
|
+
import { isResourceExecution } from "@ulearning/ai-tap/internal";
|
|
77
|
+
import { toValue, type MaybeRefOrGetter } from "vue";
|
|
78
|
+
import { useMessageQueue } from "./useMessageQueue";
|
|
79
|
+
import { aiSDKExtras, type AISDKChatHelpers } from "../aiSDKExtras";
|
|
80
|
+
|
|
81
|
+
export type CustomToCreateMessageFunction = <
|
|
82
|
+
UI_MESSAGE extends UIMessage = UIMessage,
|
|
83
|
+
>(
|
|
84
|
+
message: AppendMessage,
|
|
85
|
+
) => CreateUIMessage<UI_MESSAGE>;
|
|
86
|
+
|
|
87
|
+
const toUIMessage = <UI_MESSAGE extends UIMessage>(
|
|
88
|
+
createMessage: CreateUIMessage<UI_MESSAGE>,
|
|
89
|
+
fallbackRole: UI_MESSAGE["role"],
|
|
90
|
+
): UI_MESSAGE =>
|
|
91
|
+
({
|
|
92
|
+
...createMessage,
|
|
93
|
+
id: createMessage.id ?? generateId(),
|
|
94
|
+
role: createMessage.role ?? fallbackRole,
|
|
95
|
+
}) as UI_MESSAGE;
|
|
96
|
+
|
|
97
|
+
const toVoiceTranscriptUIMessage = <UI_MESSAGE extends UIMessage>(
|
|
98
|
+
message: ThreadMessage,
|
|
99
|
+
): UI_MESSAGE =>
|
|
100
|
+
({
|
|
101
|
+
id: message.id,
|
|
102
|
+
role: message.role,
|
|
103
|
+
parts: message.content
|
|
104
|
+
.filter((part) => part.type === "text")
|
|
105
|
+
.map((part) => ({ type: "text", text: part.text })),
|
|
106
|
+
metadata: {
|
|
107
|
+
...(message.metadata.modality && { modality: message.metadata.modality }),
|
|
108
|
+
...(Object.keys(message.metadata.custom).length > 0 && {
|
|
109
|
+
custom: message.metadata.custom,
|
|
110
|
+
}),
|
|
111
|
+
},
|
|
112
|
+
}) as UI_MESSAGE;
|
|
113
|
+
|
|
114
|
+
export type AISDKRuntimeAdapter<UI_MESSAGE extends UIMessage = UIMessage> =
|
|
115
|
+
ExternalStoreSharedOptions & {
|
|
116
|
+
adapters?:
|
|
117
|
+
| (NonNullable<ExternalStoreAdapter["adapters"]> & {
|
|
118
|
+
history?: ThreadHistoryAdapter | undefined;
|
|
119
|
+
suggestion?: SuggestionAdapter | undefined;
|
|
120
|
+
})
|
|
121
|
+
| undefined;
|
|
122
|
+
toCreateMessage?: CustomToCreateMessageFunction;
|
|
123
|
+
/** @deprecated Experimental since 2026-08-28. Not scheduled for removal; the API may change in any release. */
|
|
124
|
+
unstable_messageRepositoryInstance?: MessageRepository | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The object a host answer belongs to, normally the `Chat` the runtime
|
|
127
|
+
* renders. A host answer never reaches the `useChat` messages, so without
|
|
128
|
+
* an owner it lives only as long as this runtime. With an owner, it
|
|
129
|
+
* survives runtime remounts over the same chat.
|
|
130
|
+
*
|
|
131
|
+
* @deprecated Experimental since 2026-09-29. Not scheduled for removal; the API may change in any release.
|
|
132
|
+
*/
|
|
133
|
+
unstable_hostApprovalOwner?: object | undefined;
|
|
134
|
+
/**
|
|
135
|
+
* Whether to automatically cancel pending interactive tool calls when the user sends a new message.
|
|
136
|
+
*
|
|
137
|
+
* When enabled (default), the pending tool calls will be marked as failed with an error message
|
|
138
|
+
* indicating the user cancelled the tool call by sending a new message.
|
|
139
|
+
*
|
|
140
|
+
* @default true
|
|
141
|
+
*/
|
|
142
|
+
cancelPendingToolCallsOnSend?: boolean | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* Holds messages sent during a run in `composer.queue` until the run
|
|
145
|
+
* settles. Steered messages dispatch before queued follow-ups.
|
|
146
|
+
* @deprecated Experimental since 2026-10-15. Not scheduled for removal; the API may change in any release.
|
|
147
|
+
*/
|
|
148
|
+
unstable_enableMessageQueue?: boolean | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* Called when `runtime.thread.resumeRun(config)` is invoked.
|
|
151
|
+
*
|
|
152
|
+
* When omitted, `resumeRun` throws `"Runtime does not support resuming runs."`.
|
|
153
|
+
* Provide this to bridge resume invocations into a custom replay channel
|
|
154
|
+
* (for example, an SSE reconnect endpoint keyed by turn id).
|
|
155
|
+
*/
|
|
156
|
+
onResume?: ExternalStoreAdapter["onResume"];
|
|
157
|
+
canResume?: ExternalStoreAdapter["canResume"];
|
|
158
|
+
/**
|
|
159
|
+
* Called when `runtime.thread.resumeToolCall(options)` is invoked for a tool call the in-process tracker does not own.
|
|
160
|
+
*
|
|
161
|
+
* When omitted, `resumeToolCall` throws `"Tool call ${toolCallId} is not waiting for resume."`.
|
|
162
|
+
* Provide this to bridge resume-tool-call invocations into a custom handler.
|
|
163
|
+
*/
|
|
164
|
+
onResumeToolCall?: ExternalStoreAdapter["onResumeToolCall"];
|
|
165
|
+
/**
|
|
166
|
+
* Answers tool approval requests through a host-owned channel instead of the AI SDK's `addToolApprovalResponse`.
|
|
167
|
+
*
|
|
168
|
+
* Called for every approval request in the thread with the complete response, including option and free-form answers. Hand requests the host does not own to `respondViaAISDK`, which is what runs when this option is omitted. The answer applies to the approval when the handler starts and is removed if it throws. It is never written into the `useChat` messages, so `sendAutomaticallyWhen` cannot forward it. With a history adapter, the answer is stored with its message once the handler resolves and returns on reload. With `unstable_hostApprovalOwner`, in-memory answers survive runtime remounts over the same chat; without an owner or history adapter, they last only as long as the runtime. Bring the resumed run back into the chat, for example with `resumeStream`, and have the endpoint refuse a second resume.
|
|
169
|
+
*
|
|
170
|
+
* While a handler is set, an approval's `display`, `allowFreeform`, `dismissible` and `options` reach the renderer, because the handler can receive answers the AI SDK cannot carry. A stream declares them through the `approvalDescriptor` of its `tool-approval-request` chunk, the one approval field the AI SDK keeps opaque; the converter reads the request and answer fields from that descriptor when the approval itself lacks them.
|
|
171
|
+
*/
|
|
172
|
+
onRespondToToolApproval?:
|
|
173
|
+
| ((
|
|
174
|
+
response: RespondToToolApprovalOptions,
|
|
175
|
+
context: {
|
|
176
|
+
toolCallId: string;
|
|
177
|
+
toolName: string;
|
|
178
|
+
/** Sends this response through the AI SDK's `addToolApprovalResponse`, which carries only `approved` and `reason`. */
|
|
179
|
+
respondViaAISDK: () => Promise<void>;
|
|
180
|
+
},
|
|
181
|
+
) => Promise<void> | void)
|
|
182
|
+
| undefined;
|
|
183
|
+
/**
|
|
184
|
+
* How consecutive assistant messages are rendered.
|
|
185
|
+
*
|
|
186
|
+
* `"concat-content"` (the default) merges them into a single thread message.
|
|
187
|
+
* `"none"` keeps each assistant message as its own thread message, which is
|
|
188
|
+
* useful when a backend persists proactive or consecutive assistant messages
|
|
189
|
+
* as separate entries.
|
|
190
|
+
*/
|
|
191
|
+
joinStrategy?: JoinStrategy | undefined;
|
|
192
|
+
/**
|
|
193
|
+
* A branch-aware AI SDK message tree seeded once when `useChat` is empty.
|
|
194
|
+
* After that seed, live updates come only from `useChat`. A later empty
|
|
195
|
+
* chat or a new object identity does not reload the tree.
|
|
196
|
+
*/
|
|
197
|
+
messageRepository?: MessageFormatRepository<UI_MESSAGE>;
|
|
198
|
+
/**
|
|
199
|
+
* Called after an explicit `switchToBranch` (for example a BranchPicker
|
|
200
|
+
* click). Complements `setMessages` and does not enable switching by itself.
|
|
201
|
+
*
|
|
202
|
+
* @deprecated Experimental since 2026-06-23. Not scheduled for removal; the API may change in any release.
|
|
203
|
+
*/
|
|
204
|
+
unstable_onBranchChange?: ExternalStoreAdapter["unstable_onBranchChange"];
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const EMPTY_SUGGESTIONS: readonly ThreadSuggestion[] = [];
|
|
208
|
+
|
|
209
|
+
const useGeneratedSuggestions = (
|
|
210
|
+
suggestionAdapter: SuggestionAdapter | undefined,
|
|
211
|
+
messages: readonly ThreadMessage[],
|
|
212
|
+
isRunning: boolean,
|
|
213
|
+
): readonly ThreadSuggestion[] => {
|
|
214
|
+
const [suggestions, setSuggestions] =
|
|
215
|
+
useState<readonly ThreadSuggestion[]>(EMPTY_SUGGESTIONS);
|
|
216
|
+
const controllerRef = useRef<AbortController | null>(null);
|
|
217
|
+
const wasRunningRef = useRef(false);
|
|
218
|
+
const messagesRef = useRef(messages);
|
|
219
|
+
useInsertionEffect(() => {
|
|
220
|
+
messagesRef.current = messages;
|
|
221
|
+
}, [messages]);
|
|
222
|
+
const adapterKey = suggestionAdapter?.key;
|
|
223
|
+
const adapterRef = useRef(suggestionAdapter);
|
|
224
|
+
const lastAdapterKeyRef = useRef(adapterKey);
|
|
225
|
+
const adapterReplacedRef = useRef(false);
|
|
226
|
+
useInsertionEffect(() => {
|
|
227
|
+
const previousAdapter = adapterRef.current;
|
|
228
|
+
adapterRef.current = suggestionAdapter;
|
|
229
|
+
const adapterReplaced =
|
|
230
|
+
previousAdapter !== undefined &&
|
|
231
|
+
suggestionAdapter !== undefined &&
|
|
232
|
+
lastAdapterKeyRef.current !== adapterKey;
|
|
233
|
+
adapterReplacedRef.current ||= adapterReplaced;
|
|
234
|
+
if (suggestionAdapter === undefined || adapterReplaced) {
|
|
235
|
+
controllerRef.current?.abort();
|
|
236
|
+
}
|
|
237
|
+
lastAdapterKeyRef.current = adapterKey;
|
|
238
|
+
}, [adapterKey, suggestionAdapter]);
|
|
239
|
+
const hasAdapter = suggestionAdapter !== undefined;
|
|
240
|
+
const adapterPresentRef = useRef(hasAdapter);
|
|
241
|
+
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
const clearSuggestions = () => {
|
|
244
|
+
controllerRef.current?.abort();
|
|
245
|
+
controllerRef.current = null;
|
|
246
|
+
setSuggestions((prev) => (prev.length === 0 ? prev : EMPTY_SUGGESTIONS));
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const adapter = adapterRef.current;
|
|
250
|
+
const adapterRestored = hasAdapter && !adapterPresentRef.current;
|
|
251
|
+
adapterPresentRef.current = hasAdapter;
|
|
252
|
+
const adapterReplaced = adapterReplacedRef.current;
|
|
253
|
+
adapterReplacedRef.current = false;
|
|
254
|
+
if (!adapter) {
|
|
255
|
+
clearSuggestions();
|
|
256
|
+
wasRunningRef.current = isRunning;
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (adapterReplaced) clearSuggestions();
|
|
261
|
+
|
|
262
|
+
if (isRunning) {
|
|
263
|
+
if (!wasRunningRef.current) {
|
|
264
|
+
clearSuggestions();
|
|
265
|
+
}
|
|
266
|
+
wasRunningRef.current = true;
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (!wasRunningRef.current && !adapterReplaced && !adapterRestored) return;
|
|
271
|
+
wasRunningRef.current = false;
|
|
272
|
+
|
|
273
|
+
const currentMessages = messagesRef.current;
|
|
274
|
+
const last = currentMessages.at(-1);
|
|
275
|
+
if (last?.role !== "assistant") return;
|
|
276
|
+
if (last.status?.type === "requires-action") return;
|
|
277
|
+
|
|
278
|
+
const controller = new AbortController();
|
|
279
|
+
controllerRef.current = controller;
|
|
280
|
+
const { signal } = controller;
|
|
281
|
+
|
|
282
|
+
void (async () => {
|
|
283
|
+
try {
|
|
284
|
+
const promiseOrGenerator = adapter.generate({
|
|
285
|
+
messages: currentMessages,
|
|
286
|
+
signal,
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
await consumeSuggestionResult(promiseOrGenerator, {
|
|
290
|
+
signal,
|
|
291
|
+
onUpdate: (nextSuggestions) => {
|
|
292
|
+
if (controllerRef.current !== controller) return;
|
|
293
|
+
setSuggestions(nextSuggestions);
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
} catch {}
|
|
297
|
+
})();
|
|
298
|
+
}, [adapterKey, hasAdapter, isRunning]);
|
|
299
|
+
|
|
300
|
+
useReplaySafeEffect(() => {
|
|
301
|
+
return () => {
|
|
302
|
+
controllerRef.current?.abort();
|
|
303
|
+
};
|
|
304
|
+
}, []);
|
|
305
|
+
|
|
306
|
+
return suggestions;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const NO_CANCELLED_MESSAGE_IDS: ReadonlySet<string> = new Set();
|
|
310
|
+
|
|
311
|
+
const NO_SUPERSEDED_APPROVAL_PROJECTION = Object.freeze({
|
|
312
|
+
approvalIds: Object.freeze(new Set<string>()),
|
|
313
|
+
statusMessageIds: Object.freeze(new Set<string>()),
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const NO_TOOL_APPROVAL_RESPONSES: ReadonlyMap<
|
|
317
|
+
string,
|
|
318
|
+
RespondToToolApprovalOptions
|
|
319
|
+
> = new Map();
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* A host answer is deliberately kept out of the `useChat` messages, so nothing
|
|
323
|
+
* in the chat records it. Held in runtime state it would die with the runtime,
|
|
324
|
+
* and a runtime mounted again over the same chat would show the request open
|
|
325
|
+
* and take a second answer. Keyed on the chat instead, the answer lives as
|
|
326
|
+
* long as the chat it belongs to, and is collected with it.
|
|
327
|
+
*/
|
|
328
|
+
type OwnedApproval = {
|
|
329
|
+
response: RespondToToolApprovalOptions;
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
type ChatRuntimeState = {
|
|
333
|
+
approvals: Map<string, OwnedApproval>;
|
|
334
|
+
cancelledMessageIds: ReadonlySet<string>;
|
|
335
|
+
cancelledToolCallIds: Set<string>;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const chatRuntimeStates = new WeakMap<object, ChatRuntimeState>();
|
|
339
|
+
|
|
340
|
+
const getChatRuntimeState = (owner: object | undefined) => {
|
|
341
|
+
if (owner === undefined) return undefined;
|
|
342
|
+
let state = chatRuntimeStates.get(owner);
|
|
343
|
+
if (state === undefined) {
|
|
344
|
+
state = {
|
|
345
|
+
approvals: new Map(),
|
|
346
|
+
cancelledMessageIds: NO_CANCELLED_MESSAGE_IDS,
|
|
347
|
+
cancelledToolCallIds: new Set(),
|
|
348
|
+
};
|
|
349
|
+
chatRuntimeStates.set(owner, state);
|
|
350
|
+
}
|
|
351
|
+
return state;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
const toApprovalResponses = (
|
|
355
|
+
owned: ReadonlyMap<string, OwnedApproval> | undefined,
|
|
356
|
+
): ReadonlyMap<string, RespondToToolApprovalOptions> =>
|
|
357
|
+
owned && owned.size > 0
|
|
358
|
+
? new Map([...owned].map(([id, entry]) => [id, entry.response]))
|
|
359
|
+
: NO_TOOL_APPROVAL_RESPONSES;
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* The answers live on the owner, but each mounted runtime renders them from
|
|
363
|
+
* its own state, so a write has to be announced: the runtime that performed it
|
|
364
|
+
* may already be unmounted (a rollback resolving after a remount), and another
|
|
365
|
+
* runtime may be mounted over the same owner.
|
|
366
|
+
*/
|
|
367
|
+
const chatRuntimeListeners = new WeakMap<object, Set<() => void>>();
|
|
368
|
+
|
|
369
|
+
const subscribeToChatRuntimeState = (owner: object, listener: () => void) => {
|
|
370
|
+
const listeners = chatRuntimeListeners.get(owner) ?? new Set();
|
|
371
|
+
chatRuntimeListeners.set(owner, listeners);
|
|
372
|
+
listeners.add(listener);
|
|
373
|
+
return () => {
|
|
374
|
+
listeners.delete(listener);
|
|
375
|
+
};
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
const notifyChatRuntimeState = (owner: object) => {
|
|
379
|
+
for (const listener of [...(chatRuntimeListeners.get(owner) ?? [])]) {
|
|
380
|
+
listener();
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const getSupersededApprovalProjection = <UI_MESSAGE extends UIMessage>(
|
|
385
|
+
messages: readonly UI_MESSAGE[],
|
|
386
|
+
hostApprovalIds: ReadonlySet<string>,
|
|
387
|
+
joinStrategy: JoinStrategy | undefined,
|
|
388
|
+
isRunning: boolean,
|
|
389
|
+
) => {
|
|
390
|
+
const approvalIds = new Set<string>();
|
|
391
|
+
const statusMessageIds = new Set<string>();
|
|
392
|
+
const lastIndex = messages.length - 1;
|
|
393
|
+
let lastAssistant: UI_MESSAGE | undefined;
|
|
394
|
+
let hasSupersededApproval = false;
|
|
395
|
+
let previousWasVoice = false;
|
|
396
|
+
|
|
397
|
+
const flush = () => {
|
|
398
|
+
const hasOpenToolPart =
|
|
399
|
+
lastAssistant === messages[lastIndex] &&
|
|
400
|
+
lastAssistant?.parts?.some((part) => {
|
|
401
|
+
if (
|
|
402
|
+
typeof part?.type !== "string" ||
|
|
403
|
+
!isToolUIPart(part) ||
|
|
404
|
+
part.state === "output-available" ||
|
|
405
|
+
part.state === "output-error" ||
|
|
406
|
+
part.state === "output-denied"
|
|
407
|
+
)
|
|
408
|
+
return false;
|
|
409
|
+
|
|
410
|
+
const approval = (
|
|
411
|
+
part as {
|
|
412
|
+
approval?: {
|
|
413
|
+
resolution?: unknown;
|
|
414
|
+
descriptor?: unknown;
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
).approval;
|
|
418
|
+
const resolution =
|
|
419
|
+
approval?.resolution ??
|
|
420
|
+
(approval?.descriptor as { resolution?: unknown } | undefined)
|
|
421
|
+
?.resolution;
|
|
422
|
+
return resolution !== "cancelled" && resolution !== "expired";
|
|
423
|
+
});
|
|
424
|
+
if (
|
|
425
|
+
lastAssistant &&
|
|
426
|
+
hasSupersededApproval &&
|
|
427
|
+
!hasOpenToolPart &&
|
|
428
|
+
!(isRunning && lastAssistant === messages[lastIndex])
|
|
429
|
+
) {
|
|
430
|
+
statusMessageIds.add(lastAssistant.id);
|
|
431
|
+
}
|
|
432
|
+
lastAssistant = undefined;
|
|
433
|
+
hasSupersededApproval = false;
|
|
434
|
+
previousWasVoice = false;
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
for (let index = 0; index < messages.length; index++) {
|
|
438
|
+
const message = messages[index]!;
|
|
439
|
+
if (message.role !== "assistant") {
|
|
440
|
+
flush();
|
|
441
|
+
} else {
|
|
442
|
+
const isVoice =
|
|
443
|
+
(message.metadata as { modality?: unknown } | undefined)?.modality ===
|
|
444
|
+
"voice";
|
|
445
|
+
if (isVoice || previousWasVoice || joinStrategy === "none") flush();
|
|
446
|
+
lastAssistant = message;
|
|
447
|
+
previousWasVoice = isVoice;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
for (const part of message.parts ?? []) {
|
|
451
|
+
if (
|
|
452
|
+
typeof part?.type !== "string" ||
|
|
453
|
+
!isToolUIPart(part) ||
|
|
454
|
+
part.state !== "approval-requested"
|
|
455
|
+
)
|
|
456
|
+
continue;
|
|
457
|
+
|
|
458
|
+
const approval = part.approval;
|
|
459
|
+
if (!approval) continue;
|
|
460
|
+
|
|
461
|
+
const approvalId = approval.id;
|
|
462
|
+
if (hostApprovalIds.has(approvalId)) continue;
|
|
463
|
+
|
|
464
|
+
const resolution =
|
|
465
|
+
(approval as { resolution?: unknown }).resolution ??
|
|
466
|
+
(approval.descriptor as { resolution?: unknown } | null | undefined)
|
|
467
|
+
?.resolution;
|
|
468
|
+
if (resolution === "cancelled" || resolution === "expired") continue;
|
|
469
|
+
|
|
470
|
+
if (index !== lastIndex) {
|
|
471
|
+
approvalIds.add(approvalId);
|
|
472
|
+
if (message.role === "assistant") hasSupersededApproval = true;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
flush();
|
|
477
|
+
|
|
478
|
+
if (approvalIds.size === 0 && statusMessageIds.size === 0)
|
|
479
|
+
return NO_SUPERSEDED_APPROVAL_PROJECTION;
|
|
480
|
+
|
|
481
|
+
return { approvalIds, statusMessageIds };
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const findRawToolMessageIndex = <UI_MESSAGE extends UIMessage>(
|
|
485
|
+
messages: readonly UI_MESSAGE[],
|
|
486
|
+
messageId: string,
|
|
487
|
+
toolCallId: string,
|
|
488
|
+
joinStrategy: JoinStrategy | undefined,
|
|
489
|
+
) => {
|
|
490
|
+
const containsToolCall = (message: UI_MESSAGE) =>
|
|
491
|
+
message.parts?.some(
|
|
492
|
+
(part) =>
|
|
493
|
+
typeof part?.type === "string" &&
|
|
494
|
+
isToolUIPart(part) &&
|
|
495
|
+
part.toolCallId === toolCallId,
|
|
496
|
+
) === true;
|
|
497
|
+
|
|
498
|
+
const messageIndex = messages.findIndex(
|
|
499
|
+
(message) => message.id === messageId,
|
|
500
|
+
);
|
|
501
|
+
if (messageIndex === -1) return -1;
|
|
502
|
+
if (containsToolCall(messages[messageIndex]!)) return messageIndex;
|
|
503
|
+
if (joinStrategy === "none" || messages[messageIndex]?.role !== "assistant")
|
|
504
|
+
return -1;
|
|
505
|
+
|
|
506
|
+
let start = messageIndex;
|
|
507
|
+
while (start > 0 && messages[start - 1]?.role === "assistant") start--;
|
|
508
|
+
|
|
509
|
+
let end = messageIndex;
|
|
510
|
+
while (end + 1 < messages.length && messages[end + 1]?.role === "assistant")
|
|
511
|
+
end++;
|
|
512
|
+
|
|
513
|
+
for (let index = start; index <= end; index++) {
|
|
514
|
+
if (containsToolCall(messages[index]!)) return index;
|
|
515
|
+
}
|
|
516
|
+
return -1;
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const toChatError = (error: Error): AssistantError => {
|
|
520
|
+
const { code, statusCode } = error as {
|
|
521
|
+
code?: unknown;
|
|
522
|
+
statusCode?: unknown;
|
|
523
|
+
};
|
|
524
|
+
return {
|
|
525
|
+
code:
|
|
526
|
+
typeof code === "string"
|
|
527
|
+
? code
|
|
528
|
+
: error.name !== "Error"
|
|
529
|
+
? error.name
|
|
530
|
+
: "unknown",
|
|
531
|
+
message: error.message,
|
|
532
|
+
...(typeof statusCode === "number" && { statusCode }),
|
|
533
|
+
};
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const useAISDKRuntimeResource = <UI_MESSAGE extends UIMessage = UIMessage>(
|
|
537
|
+
chatHelpers: AISDKChatHelpers<UI_MESSAGE>,
|
|
538
|
+
adapter: AISDKRuntimeAdapter<UI_MESSAGE> = {},
|
|
539
|
+
) => {
|
|
540
|
+
const {
|
|
541
|
+
adapters,
|
|
542
|
+
toCreateMessage: customToCreateMessage,
|
|
543
|
+
cancelPendingToolCallsOnSend = true,
|
|
544
|
+
onResume,
|
|
545
|
+
canResume,
|
|
546
|
+
onResumeToolCall,
|
|
547
|
+
onRespondToToolApproval: customOnRespondToToolApproval,
|
|
548
|
+
joinStrategy,
|
|
549
|
+
messageRepository,
|
|
550
|
+
unstable_onBranchChange,
|
|
551
|
+
unstable_enableMessageQueue,
|
|
552
|
+
} = adapter;
|
|
553
|
+
const suggestionAdapter = adapters?.suggestion;
|
|
554
|
+
const contextAdapters = useRuntimeAdaptersValue();
|
|
555
|
+
const [queueError, setQueueError] = useState<Error | undefined>();
|
|
556
|
+
const runtimeError = unstable_enableMessageQueue
|
|
557
|
+
? (queueError ?? chatHelpers.error)
|
|
558
|
+
: chatHelpers.error;
|
|
559
|
+
const [toolStatuses, setToolStatuses] = useState<
|
|
560
|
+
Record<string, ToolExecutionStatus>
|
|
561
|
+
>({});
|
|
562
|
+
const [cancelledMessages, setCancelledMessages] = useState<{
|
|
563
|
+
chatId: string;
|
|
564
|
+
ids: ReadonlySet<string>;
|
|
565
|
+
} | null>(null);
|
|
566
|
+
// Set by hosts that own the chat across runtime lifetimes, so a host answer
|
|
567
|
+
// outlives a remount over that same chat.
|
|
568
|
+
const approvalOwner = adapter.unstable_hostApprovalOwner;
|
|
569
|
+
const chatRuntimeState = getChatRuntimeState(approvalOwner);
|
|
570
|
+
const ownedApprovals = chatRuntimeState?.approvals;
|
|
571
|
+
const localCancelledToolCallIdsRef = useRef(new Set<string>());
|
|
572
|
+
const cancelledToolCallIds =
|
|
573
|
+
chatRuntimeState?.cancelledToolCallIds ??
|
|
574
|
+
localCancelledToolCallIdsRef.current;
|
|
575
|
+
const ownedCancelledMessageIds = useSyncExternalStore(
|
|
576
|
+
useCallback(
|
|
577
|
+
(listener) =>
|
|
578
|
+
approvalOwner
|
|
579
|
+
? subscribeToChatRuntimeState(approvalOwner, listener)
|
|
580
|
+
: () => {},
|
|
581
|
+
[approvalOwner],
|
|
582
|
+
),
|
|
583
|
+
useCallback(
|
|
584
|
+
() => chatRuntimeState?.cancelledMessageIds ?? NO_CANCELLED_MESSAGE_IDS,
|
|
585
|
+
[chatRuntimeState],
|
|
586
|
+
),
|
|
587
|
+
() => NO_CANCELLED_MESSAGE_IDS,
|
|
588
|
+
);
|
|
589
|
+
const [toolApprovalResponses, setToolApprovalResponses] = useState<
|
|
590
|
+
ReadonlyMap<string, RespondToToolApprovalOptions>
|
|
591
|
+
>(() => toApprovalResponses(ownedApprovals));
|
|
592
|
+
const [toolArtifacts, setToolArtifacts] = useState<
|
|
593
|
+
ReadonlyMap<string, unknown>
|
|
594
|
+
>(() => new Map());
|
|
595
|
+
const [toolInteractions, setToolInteractions] = useState<
|
|
596
|
+
ReadonlyMap<string, Unstable_ToolInteractionLog>
|
|
597
|
+
>(() => new Map());
|
|
598
|
+
const toolApprovalResponsesRef = useRef(
|
|
599
|
+
new Map(toApprovalResponses(ownedApprovals)),
|
|
600
|
+
);
|
|
601
|
+
const ownedApprovalIdsRef = useRef(new Set(ownedApprovals?.keys()));
|
|
602
|
+
const hostApprovalIdsRef = useRef(new Set<string>(ownedApprovals?.keys()));
|
|
603
|
+
|
|
604
|
+
// The owner's record is shared, so this runtime re-reads it whenever it is
|
|
605
|
+
// written rather than only at mount: the write may come from a runtime that
|
|
606
|
+
// has since unmounted, or from another runtime mounted over the same owner.
|
|
607
|
+
useEffect(() => {
|
|
608
|
+
if (!approvalOwner || !ownedApprovals) return undefined;
|
|
609
|
+
const sync = () => {
|
|
610
|
+
for (const id of ownedApprovalIdsRef.current)
|
|
611
|
+
toolApprovalResponsesRef.current.delete(id);
|
|
612
|
+
for (const [id, entry] of ownedApprovals)
|
|
613
|
+
toolApprovalResponsesRef.current.set(id, entry.response);
|
|
614
|
+
ownedApprovalIdsRef.current = new Set(ownedApprovals.keys());
|
|
615
|
+
hostApprovalIdsRef.current = new Set(
|
|
616
|
+
toolApprovalResponsesRef.current.keys(),
|
|
617
|
+
);
|
|
618
|
+
setToolApprovalResponses((prev) => {
|
|
619
|
+
const next = new Map(toolApprovalResponsesRef.current);
|
|
620
|
+
const unchanged =
|
|
621
|
+
prev.size === next.size &&
|
|
622
|
+
[...next].every(([id, response]) => prev.get(id) === response);
|
|
623
|
+
return unchanged ? prev : next;
|
|
624
|
+
});
|
|
625
|
+
};
|
|
626
|
+
const unsubscribe = subscribeToChatRuntimeState(approvalOwner, sync);
|
|
627
|
+
// The state was seeded during render, so a write landing between then and
|
|
628
|
+
// this subscription would otherwise never be seen.
|
|
629
|
+
sync();
|
|
630
|
+
return unsubscribe;
|
|
631
|
+
}, [approvalOwner, ownedApprovals]);
|
|
632
|
+
|
|
633
|
+
// A runtime kept mounted across a change of owner must not carry the
|
|
634
|
+
// previous chat's answers: a reused approval id would render as already
|
|
635
|
+
// answered and reject a genuine response.
|
|
636
|
+
const lastApprovalOwnerRef = useRef(approvalOwner);
|
|
637
|
+
if (lastApprovalOwnerRef.current !== approvalOwner) {
|
|
638
|
+
lastApprovalOwnerRef.current = approvalOwner;
|
|
639
|
+
hostApprovalIdsRef.current = new Set<string>(ownedApprovals?.keys());
|
|
640
|
+
toolApprovalResponsesRef.current = new Map(
|
|
641
|
+
toApprovalResponses(ownedApprovals),
|
|
642
|
+
);
|
|
643
|
+
ownedApprovalIdsRef.current = new Set(ownedApprovals?.keys());
|
|
644
|
+
setToolApprovalResponses(new Map(toolApprovalResponsesRef.current));
|
|
645
|
+
}
|
|
646
|
+
const toolArgsKeyOrderCacheRef = useRef<Map<string, Map<string, string[]>>>(
|
|
647
|
+
new Map(),
|
|
648
|
+
);
|
|
649
|
+
const [createdAtCaches] = useState(
|
|
650
|
+
() => new Map<string, Map<string, Date>>(),
|
|
651
|
+
);
|
|
652
|
+
let createdAtCache = createdAtCaches.get(chatHelpers.id);
|
|
653
|
+
if (!createdAtCache) {
|
|
654
|
+
createdAtCache = new Map();
|
|
655
|
+
createdAtCaches.set(chatHelpers.id, createdAtCache);
|
|
656
|
+
}
|
|
657
|
+
const toolLastInputCacheRef = useRef<Map<string, ReadonlyJSONObject>>(
|
|
658
|
+
new Map(),
|
|
659
|
+
);
|
|
660
|
+
const toolArgsTextCacheRef = useRef<
|
|
661
|
+
NonNullable<AISDKMessageConverterMetadata["toolArgsTextCache"]>
|
|
662
|
+
>(new WeakMap());
|
|
663
|
+
const mcpAppMetadataCacheRef = useRef<Map<string, McpAppMetadata>>(new Map());
|
|
664
|
+
const toolArtifactsRef = useRef<Map<string, unknown>>(new Map());
|
|
665
|
+
const toolInteractionsRef = useRef<Map<string, Unstable_ToolInteractionLog>>(
|
|
666
|
+
new Map(),
|
|
667
|
+
);
|
|
668
|
+
const lastRunConfigRef = useRef<RunConfig | undefined>(undefined);
|
|
669
|
+
const restoreToolArtifacts = useCallback((next: Map<string, unknown>) => {
|
|
670
|
+
toolArtifactsRef.current = next;
|
|
671
|
+
setToolArtifacts(next);
|
|
672
|
+
}, []);
|
|
673
|
+
const restoreToolInteractions = useCallback(
|
|
674
|
+
(next: Map<string, Unstable_ToolInteractionLog>) => {
|
|
675
|
+
toolInteractionsRef.current = next;
|
|
676
|
+
setToolInteractions(next);
|
|
677
|
+
},
|
|
678
|
+
[],
|
|
679
|
+
);
|
|
680
|
+
const getToolArtifacts = useCallback(() => toolArtifactsRef.current, []);
|
|
681
|
+
const getToolInteractions = useCallback(
|
|
682
|
+
() => toolInteractionsRef.current,
|
|
683
|
+
[],
|
|
684
|
+
);
|
|
685
|
+
|
|
686
|
+
const hasExecutingTools = Object.values(toolStatuses).some(
|
|
687
|
+
(s) => s?.type === "executing",
|
|
688
|
+
);
|
|
689
|
+
const providerIsRunning =
|
|
690
|
+
chatHelpers.status === "submitted" || chatHelpers.status === "streaming";
|
|
691
|
+
const isRunning = providerIsRunning || hasExecutingTools;
|
|
692
|
+
const wasProviderRunningRef = useRef(providerIsRunning);
|
|
693
|
+
|
|
694
|
+
const messageTiming = useStreamingTimingValue(
|
|
695
|
+
chatHelpers.messages,
|
|
696
|
+
isRunning,
|
|
697
|
+
aiSdkStreamingTimingAccessors,
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
// Flag the streaming message optimistic: its id can be swapped for a server
|
|
701
|
+
// id mid-run, and the repository then drops the orphaned pre-swap id (#4037).
|
|
702
|
+
const lastMessage = chatHelpers.messages.at(-1);
|
|
703
|
+
const optimisticMessageId =
|
|
704
|
+
isRunning && lastMessage?.role === "assistant" ? lastMessage.id : undefined;
|
|
705
|
+
|
|
706
|
+
const cancelledMessageIds = chatRuntimeState
|
|
707
|
+
? ownedCancelledMessageIds
|
|
708
|
+
: cancelledMessages?.chatId === chatHelpers.id
|
|
709
|
+
? cancelledMessages.ids
|
|
710
|
+
: NO_CANCELLED_MESSAGE_IDS;
|
|
711
|
+
const supportsRichToolApprovalResponses =
|
|
712
|
+
customOnRespondToToolApproval != null;
|
|
713
|
+
const supersededApprovalProjectionRef = useRef(
|
|
714
|
+
NO_SUPERSEDED_APPROVAL_PROJECTION,
|
|
715
|
+
);
|
|
716
|
+
const supersededApprovalProjection = useMemo(
|
|
717
|
+
() => {
|
|
718
|
+
const projection = getSupersededApprovalProjection(
|
|
719
|
+
chatHelpers.messages,
|
|
720
|
+
hostApprovalIdsRef.current,
|
|
721
|
+
joinStrategy,
|
|
722
|
+
isRunning,
|
|
723
|
+
);
|
|
724
|
+
const previous = supersededApprovalProjectionRef.current;
|
|
725
|
+
if (
|
|
726
|
+
projection.approvalIds.size === previous.approvalIds.size &&
|
|
727
|
+
projection.statusMessageIds.size === previous.statusMessageIds.size &&
|
|
728
|
+
[...projection.approvalIds].every((id) =>
|
|
729
|
+
previous.approvalIds.has(id),
|
|
730
|
+
) &&
|
|
731
|
+
[...projection.statusMessageIds].every((id) =>
|
|
732
|
+
previous.statusMessageIds.has(id),
|
|
733
|
+
)
|
|
734
|
+
)
|
|
735
|
+
return previous;
|
|
736
|
+
|
|
737
|
+
supersededApprovalProjectionRef.current = projection;
|
|
738
|
+
return projection;
|
|
739
|
+
},
|
|
740
|
+
// oxlint-disable-next-line react/exhaustive-deps -- hostApprovalIdsRef changes alongside toolApprovalResponses, which invalidates the projection
|
|
741
|
+
[chatHelpers.messages, joinStrategy, isRunning, toolApprovalResponses],
|
|
742
|
+
);
|
|
743
|
+
|
|
744
|
+
const toThreadMessages = useCallback(
|
|
745
|
+
(sourceMessages: UI_MESSAGE[]) => {
|
|
746
|
+
const metadata: AISDKMessageConverterMetadata = {
|
|
747
|
+
createdAtCache,
|
|
748
|
+
supportsRichToolApprovalResponses,
|
|
749
|
+
toolArtifacts: toolArtifactsRef.current,
|
|
750
|
+
toolInteractions: toolInteractionsRef.current,
|
|
751
|
+
toolApprovalResponses: toolApprovalResponsesRef.current,
|
|
752
|
+
};
|
|
753
|
+
return AISDKMessageConverter.toThreadMessages(
|
|
754
|
+
sourceMessages,
|
|
755
|
+
false,
|
|
756
|
+
metadata,
|
|
757
|
+
);
|
|
758
|
+
},
|
|
759
|
+
[createdAtCache, supportsRichToolApprovalResponses],
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const retractCancellation = useCallback(
|
|
763
|
+
(chatId: string, messageId: string) => {
|
|
764
|
+
if (chatRuntimeState !== undefined && approvalOwner !== undefined) {
|
|
765
|
+
if (!chatRuntimeState.cancelledMessageIds.has(messageId)) return;
|
|
766
|
+
const ids = new Set(chatRuntimeState.cancelledMessageIds);
|
|
767
|
+
ids.delete(messageId);
|
|
768
|
+
chatRuntimeState.cancelledMessageIds = ids;
|
|
769
|
+
notifyChatRuntimeState(approvalOwner);
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
setCancelledMessages((prev) => {
|
|
773
|
+
if (prev?.chatId !== chatId || !prev.ids.has(messageId)) return prev;
|
|
774
|
+
const ids = new Set(prev.ids);
|
|
775
|
+
ids.delete(messageId);
|
|
776
|
+
return { chatId, ids };
|
|
777
|
+
});
|
|
778
|
+
},
|
|
779
|
+
[approvalOwner, chatRuntimeState],
|
|
780
|
+
);
|
|
781
|
+
|
|
782
|
+
// A provider run that resumes the stopped response retracts its cancellation;
|
|
783
|
+
// a run that starts a new response leaves the stopped one marked.
|
|
784
|
+
const resumedMessageId =
|
|
785
|
+
providerIsRunning && lastMessage?.role === "assistant"
|
|
786
|
+
? lastMessage.id
|
|
787
|
+
: undefined;
|
|
788
|
+
|
|
789
|
+
useEffect(() => {
|
|
790
|
+
const wasProviderRunning = wasProviderRunningRef.current;
|
|
791
|
+
wasProviderRunningRef.current = providerIsRunning;
|
|
792
|
+
if (wasProviderRunning || !resumedMessageId) return;
|
|
793
|
+
retractCancellation(chatHelpers.id, resumedMessageId);
|
|
794
|
+
}, [
|
|
795
|
+
providerIsRunning,
|
|
796
|
+
resumedMessageId,
|
|
797
|
+
chatHelpers.id,
|
|
798
|
+
retractCancellation,
|
|
799
|
+
]);
|
|
800
|
+
|
|
801
|
+
const [getConverterMetadataKey] = useState(createAISDKMetadataKey);
|
|
802
|
+
const messages = useExternalMessageConverterValue({
|
|
803
|
+
callback: convertAISDKMessage,
|
|
804
|
+
isRunning,
|
|
805
|
+
messages: chatHelpers.messages,
|
|
806
|
+
joinStrategy,
|
|
807
|
+
metadata: useMemo<AISDKMessageConverterMetadata>(
|
|
808
|
+
() => ({
|
|
809
|
+
createdAtCache,
|
|
810
|
+
toolStatuses,
|
|
811
|
+
messageTiming,
|
|
812
|
+
toolArgsKeyOrderCache: toolArgsKeyOrderCacheRef.current,
|
|
813
|
+
toolArgsTextCache: toolArgsTextCacheRef.current,
|
|
814
|
+
toolLastInputCache: toolLastInputCacheRef.current,
|
|
815
|
+
mcpAppMetadataCache: mcpAppMetadataCacheRef.current,
|
|
816
|
+
toolArtifacts,
|
|
817
|
+
toolInteractions,
|
|
818
|
+
supportsRichToolApprovalResponses,
|
|
819
|
+
cancelledToolApprovalIds: supersededApprovalProjection.approvalIds,
|
|
820
|
+
cancelledStatusMessageIds:
|
|
821
|
+
supersededApprovalProjection.statusMessageIds,
|
|
822
|
+
...(optimisticMessageId && { optimisticMessageId }),
|
|
823
|
+
...(chatHelpers.error && {
|
|
824
|
+
error: toChatError(chatHelpers.error),
|
|
825
|
+
}),
|
|
826
|
+
...(cancelledMessageIds.size > 0 && { cancelledMessageIds }),
|
|
827
|
+
...(toolApprovalResponses.size > 0 && { toolApprovalResponses }),
|
|
828
|
+
}),
|
|
829
|
+
[
|
|
830
|
+
createdAtCache,
|
|
831
|
+
toolStatuses,
|
|
832
|
+
messageTiming,
|
|
833
|
+
optimisticMessageId,
|
|
834
|
+
chatHelpers.error,
|
|
835
|
+
cancelledMessageIds,
|
|
836
|
+
toolApprovalResponses,
|
|
837
|
+
supportsRichToolApprovalResponses,
|
|
838
|
+
supersededApprovalProjection,
|
|
839
|
+
toolArtifacts,
|
|
840
|
+
toolInteractions,
|
|
841
|
+
],
|
|
842
|
+
),
|
|
843
|
+
getMetadataKey: getConverterMetadataKey,
|
|
844
|
+
});
|
|
845
|
+
|
|
846
|
+
const exportedMessageRepository = useMemo(() => {
|
|
847
|
+
if (!messageRepository) return undefined;
|
|
848
|
+
const converted = toExportedMessageRepository(
|
|
849
|
+
toThreadMessages,
|
|
850
|
+
messageRepository,
|
|
851
|
+
);
|
|
852
|
+
return converted.messages.length > 0 ? converted : undefined;
|
|
853
|
+
}, [messageRepository, toThreadMessages]);
|
|
854
|
+
|
|
855
|
+
const generatedSuggestions = useGeneratedSuggestions(
|
|
856
|
+
suggestionAdapter,
|
|
857
|
+
messages,
|
|
858
|
+
isRunning,
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
const [runtimeRef] = useState(() => ({
|
|
862
|
+
get current(): AssistantRuntime {
|
|
863
|
+
return runtime;
|
|
864
|
+
},
|
|
865
|
+
}));
|
|
866
|
+
|
|
867
|
+
const {
|
|
868
|
+
isLoading,
|
|
869
|
+
deleteMessage: deleteHistoryMessage,
|
|
870
|
+
markRunCancelled,
|
|
871
|
+
persistToolInteractions,
|
|
872
|
+
persistToolApprovalResponses,
|
|
873
|
+
} = useExternalHistory(
|
|
874
|
+
runtimeRef,
|
|
875
|
+
adapters?.history ?? contextAdapters?.history,
|
|
876
|
+
toThreadMessages,
|
|
877
|
+
aiSDKV6FormatAdapter as MessageFormatAdapter<
|
|
878
|
+
UI_MESSAGE,
|
|
879
|
+
AISDKStorageFormat
|
|
880
|
+
>,
|
|
881
|
+
(messages) => {
|
|
882
|
+
chatHelpers.setMessages(messages);
|
|
883
|
+
},
|
|
884
|
+
getToolArtifacts,
|
|
885
|
+
restoreToolArtifacts,
|
|
886
|
+
getToolInteractions,
|
|
887
|
+
restoreToolInteractions,
|
|
888
|
+
toolApprovalResponsesRef.current,
|
|
889
|
+
() => {
|
|
890
|
+
for (const [id, entry] of ownedApprovals ?? []) {
|
|
891
|
+
toolApprovalResponsesRef.current.set(id, entry.response);
|
|
892
|
+
}
|
|
893
|
+
hostApprovalIdsRef.current = new Set(
|
|
894
|
+
toolApprovalResponsesRef.current.keys(),
|
|
895
|
+
);
|
|
896
|
+
setToolApprovalResponses(new Map(toolApprovalResponsesRef.current));
|
|
897
|
+
},
|
|
898
|
+
);
|
|
899
|
+
|
|
900
|
+
const {
|
|
901
|
+
id: chatId,
|
|
902
|
+
messages: chatMessages,
|
|
903
|
+
status: chatStatus,
|
|
904
|
+
} = chatHelpers;
|
|
905
|
+
const extras = useMemo(
|
|
906
|
+
() =>
|
|
907
|
+
aiSDKExtras.provide({
|
|
908
|
+
chat: chatHelpers as unknown as AISDKChatHelpers<UIMessage>,
|
|
909
|
+
error: runtimeError,
|
|
910
|
+
}),
|
|
911
|
+
// oxlint-disable-next-line react/exhaustive-deps -- keyed on the chat's identity and reactive snapshots; useChat re-mints the helpers object every render while its remaining fields are instance-bound methods, and a render-stable extras identity is what lets the external-store core dedupe adapter updates
|
|
912
|
+
[chatId, chatMessages, chatStatus, runtimeError],
|
|
913
|
+
);
|
|
914
|
+
|
|
915
|
+
const completePendingToolCalls = async () => {
|
|
916
|
+
if (!cancelPendingToolCallsOnSend) return;
|
|
917
|
+
|
|
918
|
+
// The runtime auto-aborts in-flight tool invocations when a new run
|
|
919
|
+
// is dispatched (append() / startRun()), so this only has to mark the
|
|
920
|
+
// abandoned tools cancelled in the UI message list. Every non-terminal
|
|
921
|
+
// tool call qualifies wherever it sits: the run that produced it is over,
|
|
922
|
+
// and a staged `startRun: false` message can sit between it and the tail.
|
|
923
|
+
// Uses setMessages to avoid triggering sendAutomaticallyWhen.
|
|
924
|
+
chatHelpers.setMessages((messages) => {
|
|
925
|
+
let hasChanges = false;
|
|
926
|
+
|
|
927
|
+
const next = messages.map((message) => {
|
|
928
|
+
if (message.role !== "assistant") return message;
|
|
929
|
+
|
|
930
|
+
let messageChanged = false;
|
|
931
|
+
const parts = message.parts?.map((part) => {
|
|
932
|
+
if (typeof part?.type !== "string" || !isToolUIPart(part))
|
|
933
|
+
return part;
|
|
934
|
+
if (
|
|
935
|
+
part.state === "output-available" ||
|
|
936
|
+
part.state === "output-error" ||
|
|
937
|
+
part.state === "output-denied"
|
|
938
|
+
)
|
|
939
|
+
return part;
|
|
940
|
+
|
|
941
|
+
messageChanged = true;
|
|
942
|
+
const { approval: _approval, ...rest } = part;
|
|
943
|
+
return {
|
|
944
|
+
...rest,
|
|
945
|
+
state: "output-error" as const,
|
|
946
|
+
errorText: "User cancelled tool call by sending a new message.",
|
|
947
|
+
};
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
if (!messageChanged) return message;
|
|
951
|
+
hasChanges = true;
|
|
952
|
+
return { ...message, parts };
|
|
953
|
+
});
|
|
954
|
+
|
|
955
|
+
if (!hasChanges) return messages;
|
|
956
|
+
return next;
|
|
957
|
+
});
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
const respondViaAISDK = ({
|
|
961
|
+
approvalId,
|
|
962
|
+
approved,
|
|
963
|
+
reason,
|
|
964
|
+
}: RespondToToolApprovalOptions) =>
|
|
965
|
+
Promise.resolve(
|
|
966
|
+
chatHelpers.addToolApprovalResponse({
|
|
967
|
+
id: approvalId,
|
|
968
|
+
approved,
|
|
969
|
+
...(reason != null && { reason }),
|
|
970
|
+
options: { metadata: lastRunConfigRef.current },
|
|
971
|
+
}),
|
|
972
|
+
);
|
|
973
|
+
|
|
974
|
+
const respondViaHost = async (
|
|
975
|
+
onRespond: NonNullable<AISDKRuntimeAdapter["onRespondToToolApproval"]>,
|
|
976
|
+
response: RespondToToolApprovalOptions,
|
|
977
|
+
) => {
|
|
978
|
+
const { approvalId } = response;
|
|
979
|
+
const requested = chatHelpers.messages
|
|
980
|
+
.flatMap((message) =>
|
|
981
|
+
message.parts.flatMap((part) =>
|
|
982
|
+
typeof part?.type === "string" && isToolUIPart(part)
|
|
983
|
+
? [{ messageId: message.id, part }]
|
|
984
|
+
: [],
|
|
985
|
+
),
|
|
986
|
+
)
|
|
987
|
+
.find(
|
|
988
|
+
({ part }) =>
|
|
989
|
+
part.state === "approval-requested" &&
|
|
990
|
+
part.approval?.id === approvalId,
|
|
991
|
+
);
|
|
992
|
+
if (!requested || hostApprovalIdsRef.current.has(approvalId))
|
|
993
|
+
throw new Error(
|
|
994
|
+
`Tool approval ${approvalId} is not waiting for a response.`,
|
|
995
|
+
);
|
|
996
|
+
|
|
997
|
+
// A host answer stays out of the useChat messages, where sendAutomaticallyWhen would forward it to the chat route.
|
|
998
|
+
// The owner can change while a response is in flight, and the id set is a
|
|
999
|
+
// ref that is reseeded when it does. Both writes are therefore scoped to
|
|
1000
|
+
// the record this response started under, so a rollback never reaches a
|
|
1001
|
+
// different chat's state.
|
|
1002
|
+
const startedWith = ownedApprovals;
|
|
1003
|
+
const startedOwner = approvalOwner;
|
|
1004
|
+
const startedResponses = toolApprovalResponsesRef.current;
|
|
1005
|
+
// Whether this response is currently applied, tracked here rather than
|
|
1006
|
+
// read back from the id ref: that ref follows the chat on screen and is
|
|
1007
|
+
// reseeded when the owner changes, so it cannot answer for this response.
|
|
1008
|
+
let isApplied = false;
|
|
1009
|
+
const applyResponse = (applied: boolean) => {
|
|
1010
|
+
isApplied = applied;
|
|
1011
|
+
// The captured record is always corrected, so a rollback reaches the
|
|
1012
|
+
// chat the response belongs to even after the owner moved on.
|
|
1013
|
+
if (applied)
|
|
1014
|
+
startedWith?.set(approvalId, {
|
|
1015
|
+
response,
|
|
1016
|
+
});
|
|
1017
|
+
else startedWith?.delete(approvalId);
|
|
1018
|
+
|
|
1019
|
+
if (startedOwner) {
|
|
1020
|
+
// Every runtime mounted over that owner re-reads the record, including
|
|
1021
|
+
// one mounted after this response started.
|
|
1022
|
+
notifyChatRuntimeState(startedOwner);
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
if (lastApprovalOwnerRef.current !== startedOwner) return;
|
|
1027
|
+
if (applied) hostApprovalIdsRef.current.add(approvalId);
|
|
1028
|
+
else hostApprovalIdsRef.current.delete(approvalId);
|
|
1029
|
+
if (applied) toolApprovalResponsesRef.current.set(approvalId, response);
|
|
1030
|
+
else toolApprovalResponsesRef.current.delete(approvalId);
|
|
1031
|
+
setToolApprovalResponses(new Map(toolApprovalResponsesRef.current));
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
applyResponse(true);
|
|
1035
|
+
try {
|
|
1036
|
+
await onRespond(response, {
|
|
1037
|
+
toolCallId: requested.part.toolCallId,
|
|
1038
|
+
toolName: getToolName(requested.part),
|
|
1039
|
+
respondViaAISDK: async () => {
|
|
1040
|
+
try {
|
|
1041
|
+
await respondViaAISDK(response);
|
|
1042
|
+
} finally {
|
|
1043
|
+
applyResponse(false);
|
|
1044
|
+
}
|
|
1045
|
+
},
|
|
1046
|
+
});
|
|
1047
|
+
} catch (error) {
|
|
1048
|
+
if (isApplied) applyResponse(false);
|
|
1049
|
+
throw error;
|
|
1050
|
+
}
|
|
1051
|
+
const remainsApplied =
|
|
1052
|
+
startedWith?.has(approvalId) ?? startedResponses.has(approvalId);
|
|
1053
|
+
if (isApplied && remainsApplied) {
|
|
1054
|
+
await persistToolApprovalResponses(requested.messageId);
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
|
|
1058
|
+
// A thread message can join several AI SDK messages, so a slice through it
|
|
1059
|
+
// has to end at the last of them rather than at the id the thread exposes.
|
|
1060
|
+
const lastAISDKMessageId = (threadMessageId: string | null) => {
|
|
1061
|
+
if (threadMessageId == null) return null;
|
|
1062
|
+
const threadMessage = runtimeRef.current.thread
|
|
1063
|
+
.getState()
|
|
1064
|
+
.messages.find((message) => message.id === threadMessageId);
|
|
1065
|
+
return (
|
|
1066
|
+
(threadMessage &&
|
|
1067
|
+
getVercelAIMessages<UI_MESSAGE>(threadMessage).at(-1)?.id) ??
|
|
1068
|
+
threadMessageId
|
|
1069
|
+
);
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
const cancelRun = async () => {
|
|
1073
|
+
markRunCancelled();
|
|
1074
|
+
for (const toolCallId of Object.keys(toolStatuses)) {
|
|
1075
|
+
cancelledToolCallIds.add(toolCallId);
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
const message = chatHelpers.messages.at(-1);
|
|
1079
|
+
const cancelledId =
|
|
1080
|
+
isRunning && message?.role === "assistant" ? message.id : undefined;
|
|
1081
|
+
if (cancelledId) {
|
|
1082
|
+
const liveIds = new Set(chatHelpers.messages.map((m) => m.id));
|
|
1083
|
+
if (chatRuntimeState !== undefined && approvalOwner !== undefined) {
|
|
1084
|
+
chatRuntimeState.cancelledMessageIds = new Set([
|
|
1085
|
+
...[...chatRuntimeState.cancelledMessageIds].filter((id) =>
|
|
1086
|
+
liveIds.has(id),
|
|
1087
|
+
),
|
|
1088
|
+
cancelledId,
|
|
1089
|
+
]);
|
|
1090
|
+
notifyChatRuntimeState(approvalOwner);
|
|
1091
|
+
} else {
|
|
1092
|
+
setCancelledMessages((prev) => {
|
|
1093
|
+
const kept =
|
|
1094
|
+
prev?.chatId === chatHelpers.id
|
|
1095
|
+
? [...prev.ids].filter((id) => liveIds.has(id))
|
|
1096
|
+
: [];
|
|
1097
|
+
return {
|
|
1098
|
+
chatId: chatHelpers.id,
|
|
1099
|
+
ids: new Set([...kept, cancelledId]),
|
|
1100
|
+
};
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
try {
|
|
1105
|
+
await chatHelpers.stop();
|
|
1106
|
+
} catch (error) {
|
|
1107
|
+
if (!(error instanceof Error && error.name === "AbortError")) {
|
|
1108
|
+
if (cancelledId) retractCancellation(chatHelpers.id, cancelledId);
|
|
1109
|
+
throw error;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
const sendNew = async (message: AppendMessage) => {
|
|
1115
|
+
setQueueError(undefined);
|
|
1116
|
+
const createMessage = (
|
|
1117
|
+
customToCreateMessage ?? toCreateMessage
|
|
1118
|
+
)<UI_MESSAGE>(message);
|
|
1119
|
+
|
|
1120
|
+
if (!(message.startRun ?? message.role === "user")) {
|
|
1121
|
+
chatHelpers.setMessages((current) => [
|
|
1122
|
+
...current,
|
|
1123
|
+
toUIMessage<UI_MESSAGE>(createMessage, message.role),
|
|
1124
|
+
]);
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
lastRunConfigRef.current = message.runConfig;
|
|
1129
|
+
await completePendingToolCalls();
|
|
1130
|
+
await chatHelpers.sendMessage(createMessage, {
|
|
1131
|
+
metadata: message.runConfig,
|
|
1132
|
+
});
|
|
1133
|
+
};
|
|
1134
|
+
|
|
1135
|
+
const messageQueue = useMessageQueue({
|
|
1136
|
+
onError: (error) => {
|
|
1137
|
+
if (error?.name === "AbortError") setQueueError(undefined);
|
|
1138
|
+
else setQueueError(error);
|
|
1139
|
+
},
|
|
1140
|
+
enabled: unstable_enableMessageQueue === true,
|
|
1141
|
+
isRunning,
|
|
1142
|
+
isSendDisabled: adapter.isSendDisabled === true,
|
|
1143
|
+
send: sendNew,
|
|
1144
|
+
cancel: cancelRun,
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
const hasSeededRepositoryRef = useRef(false);
|
|
1148
|
+
const shouldFeedRepository =
|
|
1149
|
+
exportedMessageRepository != null &&
|
|
1150
|
+
!hasSeededRepositoryRef.current &&
|
|
1151
|
+
messages.length === 0;
|
|
1152
|
+
|
|
1153
|
+
const runtime = useExternalStoreRuntime({
|
|
1154
|
+
unstable_persistsHistory: true,
|
|
1155
|
+
isRunning: providerIsRunning,
|
|
1156
|
+
...(shouldFeedRepository
|
|
1157
|
+
? { messageRepository: exportedMessageRepository }
|
|
1158
|
+
: { messages }),
|
|
1159
|
+
unstable_enableToolInvocations: true,
|
|
1160
|
+
setToolStatuses,
|
|
1161
|
+
setMessages: (messages) =>
|
|
1162
|
+
chatHelpers.setMessages(
|
|
1163
|
+
messages
|
|
1164
|
+
.map(getVercelAIMessages<UI_MESSAGE>)
|
|
1165
|
+
.filter(Boolean)
|
|
1166
|
+
.flat(),
|
|
1167
|
+
),
|
|
1168
|
+
onImport: (messages) =>
|
|
1169
|
+
chatHelpers.setMessages(
|
|
1170
|
+
messages
|
|
1171
|
+
.map(getVercelAIMessages<UI_MESSAGE>)
|
|
1172
|
+
.filter(Boolean)
|
|
1173
|
+
.flat(),
|
|
1174
|
+
),
|
|
1175
|
+
onVoiceTranscript: (message: ThreadMessage) =>
|
|
1176
|
+
chatHelpers.setMessages((current) => [
|
|
1177
|
+
...current,
|
|
1178
|
+
toVoiceTranscriptUIMessage<UI_MESSAGE>(message),
|
|
1179
|
+
]),
|
|
1180
|
+
onExportExternalState: (): MessageFormatRepository<UI_MESSAGE> => {
|
|
1181
|
+
const exported = runtimeRef.current.thread.export();
|
|
1182
|
+
|
|
1183
|
+
const expandedMessages: MessageFormatItem<UI_MESSAGE>[] = [];
|
|
1184
|
+
const lastInnerIdMap = new Map<string, string>();
|
|
1185
|
+
|
|
1186
|
+
for (const item of exported.messages) {
|
|
1187
|
+
const innerMessages = getExternalStoreMessages<UI_MESSAGE>(
|
|
1188
|
+
item.message,
|
|
1189
|
+
);
|
|
1190
|
+
let parentId =
|
|
1191
|
+
item.parentId != null
|
|
1192
|
+
? (lastInnerIdMap.get(item.parentId) ?? item.parentId)
|
|
1193
|
+
: null;
|
|
1194
|
+
for (const innerMessage of innerMessages) {
|
|
1195
|
+
expandedMessages.push({ parentId, message: innerMessage });
|
|
1196
|
+
parentId = aiSDKV6FormatAdapter.getId(innerMessage as UIMessage);
|
|
1197
|
+
}
|
|
1198
|
+
if (innerMessages.length > 0) {
|
|
1199
|
+
lastInnerIdMap.set(
|
|
1200
|
+
item.message.id,
|
|
1201
|
+
aiSDKV6FormatAdapter.getId(
|
|
1202
|
+
innerMessages[innerMessages.length - 1]! as UIMessage,
|
|
1203
|
+
),
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
const result: MessageFormatRepository<UI_MESSAGE> = {
|
|
1209
|
+
messages: expandedMessages,
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
if (exported.headId != null) {
|
|
1213
|
+
result.headId = lastInnerIdMap.get(exported.headId) ?? exported.headId;
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
return result;
|
|
1217
|
+
},
|
|
1218
|
+
onLoadExternalState: (repo: MessageFormatRepository<UI_MESSAGE>) => {
|
|
1219
|
+
// Convert MessageFormatRepository to ExportedMessageRepository
|
|
1220
|
+
const exportedRepo = toExportedMessageRepository(toThreadMessages, repo);
|
|
1221
|
+
|
|
1222
|
+
// Import into the thread's MessageRepository
|
|
1223
|
+
runtimeRef.current.thread.import(exportedRepo);
|
|
1224
|
+
},
|
|
1225
|
+
onCancel: messageQueue.cancel,
|
|
1226
|
+
onNew: sendNew,
|
|
1227
|
+
...(messageQueue.adapter && { queue: messageQueue.adapter }),
|
|
1228
|
+
onEdit: async (message) => {
|
|
1229
|
+
messageQueue.clear();
|
|
1230
|
+
const createMessage = (
|
|
1231
|
+
customToCreateMessage ?? toCreateMessage
|
|
1232
|
+
)<UI_MESSAGE>(message);
|
|
1233
|
+
|
|
1234
|
+
const parentId = lastAISDKMessageId(message.parentId);
|
|
1235
|
+
if (!(message.startRun ?? message.role === "user")) {
|
|
1236
|
+
chatHelpers.setMessages((current) => [
|
|
1237
|
+
...sliceMessagesUntil(current, parentId),
|
|
1238
|
+
toUIMessage<UI_MESSAGE>(createMessage, message.role),
|
|
1239
|
+
]);
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
lastRunConfigRef.current = message.runConfig;
|
|
1244
|
+
chatHelpers.setMessages((current) =>
|
|
1245
|
+
sliceMessagesUntil(current, parentId),
|
|
1246
|
+
);
|
|
1247
|
+
await chatHelpers.sendMessage(createMessage, {
|
|
1248
|
+
metadata: message.runConfig,
|
|
1249
|
+
});
|
|
1250
|
+
},
|
|
1251
|
+
onDelete: async (messageId) => {
|
|
1252
|
+
const threadMessages = runtimeRef.current.thread.getState().messages;
|
|
1253
|
+
const messageIndex = threadMessages.findIndex(
|
|
1254
|
+
(message) => message.id === messageId,
|
|
1255
|
+
);
|
|
1256
|
+
if (messageIndex === -1) return;
|
|
1257
|
+
|
|
1258
|
+
await deleteHistoryMessage(messageId);
|
|
1259
|
+
|
|
1260
|
+
let removedToolArtifact = false;
|
|
1261
|
+
let removedToolInteractions = false;
|
|
1262
|
+
let removedToolApprovalResponse = false;
|
|
1263
|
+
let removedHostApprovalId = false;
|
|
1264
|
+
const nextToolArtifacts = new Map(toolArtifactsRef.current);
|
|
1265
|
+
const nextToolInteractions = new Map(toolInteractionsRef.current);
|
|
1266
|
+
for (const part of threadMessages[messageIndex]!.content) {
|
|
1267
|
+
if (part.type === "tool-call") {
|
|
1268
|
+
removedToolArtifact =
|
|
1269
|
+
nextToolArtifacts.delete(part.toolCallId) || removedToolArtifact;
|
|
1270
|
+
removedToolInteractions =
|
|
1271
|
+
nextToolInteractions.delete(part.toolCallId) ||
|
|
1272
|
+
removedToolInteractions;
|
|
1273
|
+
if (part.approval) {
|
|
1274
|
+
ownedApprovals?.delete(part.approval.id);
|
|
1275
|
+
removedToolApprovalResponse =
|
|
1276
|
+
toolApprovalResponsesRef.current.delete(part.approval.id) ||
|
|
1277
|
+
removedToolApprovalResponse;
|
|
1278
|
+
removedHostApprovalId =
|
|
1279
|
+
hostApprovalIdsRef.current.delete(part.approval.id) ||
|
|
1280
|
+
removedHostApprovalId;
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
if (removedToolArtifact) {
|
|
1285
|
+
toolArtifactsRef.current = nextToolArtifacts;
|
|
1286
|
+
setToolArtifacts(nextToolArtifacts);
|
|
1287
|
+
}
|
|
1288
|
+
if (removedToolInteractions) {
|
|
1289
|
+
toolInteractionsRef.current = nextToolInteractions;
|
|
1290
|
+
setToolInteractions(nextToolInteractions);
|
|
1291
|
+
}
|
|
1292
|
+
if (removedToolApprovalResponse || removedHostApprovalId) {
|
|
1293
|
+
if (approvalOwner) notifyChatRuntimeState(approvalOwner);
|
|
1294
|
+
setToolApprovalResponses(new Map(toolApprovalResponsesRef.current));
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
const deleteIds = new Set(
|
|
1298
|
+
getExternalStoreMessages<UI_MESSAGE>(threadMessages[messageIndex]!).map(
|
|
1299
|
+
(message) => message.id,
|
|
1300
|
+
),
|
|
1301
|
+
);
|
|
1302
|
+
chatHelpers.setMessages((current) =>
|
|
1303
|
+
current.filter((message) => !deleteIds.has(message.id)),
|
|
1304
|
+
);
|
|
1305
|
+
},
|
|
1306
|
+
onReload: async (parentId: string | null, config) => {
|
|
1307
|
+
messageQueue.clear();
|
|
1308
|
+
lastRunConfigRef.current = config.runConfig;
|
|
1309
|
+
const newMessages = sliceMessagesUntil(
|
|
1310
|
+
chatHelpers.messages,
|
|
1311
|
+
lastAISDKMessageId(parentId),
|
|
1312
|
+
);
|
|
1313
|
+
// regenerate drops a trailing assistant message, so the first message of
|
|
1314
|
+
// the reloaded response stays for it to drop instead of the parent.
|
|
1315
|
+
const reloaded = chatHelpers.messages[newMessages.length];
|
|
1316
|
+
if (reloaded?.role === "assistant") newMessages.push(reloaded);
|
|
1317
|
+
chatHelpers.setMessages(newMessages);
|
|
1318
|
+
|
|
1319
|
+
await chatHelpers.regenerate({ metadata: config.runConfig });
|
|
1320
|
+
},
|
|
1321
|
+
onAddToolResult: ({
|
|
1322
|
+
messageId,
|
|
1323
|
+
toolCallId,
|
|
1324
|
+
toolName,
|
|
1325
|
+
result,
|
|
1326
|
+
isError,
|
|
1327
|
+
artifact,
|
|
1328
|
+
modelContent,
|
|
1329
|
+
}) => {
|
|
1330
|
+
const wasCancelled = cancelledToolCallIds.delete(toolCallId);
|
|
1331
|
+
if (artifact !== undefined) {
|
|
1332
|
+
const next = new Map(toolArtifactsRef.current);
|
|
1333
|
+
next.set(toolCallId, artifact);
|
|
1334
|
+
toolArtifactsRef.current = next;
|
|
1335
|
+
setToolArtifacts(next);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
const targetIndex = findRawToolMessageIndex(
|
|
1339
|
+
chatHelpers.messages,
|
|
1340
|
+
messageId,
|
|
1341
|
+
toolCallId,
|
|
1342
|
+
joinStrategy,
|
|
1343
|
+
);
|
|
1344
|
+
|
|
1345
|
+
const errorText =
|
|
1346
|
+
typeof result === "string" ? result : JSON.stringify(result);
|
|
1347
|
+
const output =
|
|
1348
|
+
!isError && modelContent !== undefined
|
|
1349
|
+
? wrapModelContentEnvelope(result, modelContent)
|
|
1350
|
+
: result;
|
|
1351
|
+
|
|
1352
|
+
if (
|
|
1353
|
+
targetIndex >= 0 &&
|
|
1354
|
+
(targetIndex !== chatHelpers.messages.length - 1 || wasCancelled)
|
|
1355
|
+
) {
|
|
1356
|
+
const target = chatHelpers.messages[targetIndex]!;
|
|
1357
|
+
const targetPart = target.parts.find(
|
|
1358
|
+
(part) =>
|
|
1359
|
+
typeof part?.type === "string" &&
|
|
1360
|
+
isToolUIPart(part) &&
|
|
1361
|
+
part.toolCallId === toolCallId,
|
|
1362
|
+
) as { state?: string; preliminary?: boolean } | undefined;
|
|
1363
|
+
// An earlier message's or a cancelled call's settled output may already have reached the model, as the error a cancelling send writes does.
|
|
1364
|
+
if (
|
|
1365
|
+
targetPart?.state === "output-error" ||
|
|
1366
|
+
targetPart?.state === "output-denied" ||
|
|
1367
|
+
(targetPart?.state === "output-available" && !targetPart.preliminary)
|
|
1368
|
+
)
|
|
1369
|
+
return Promise.resolve();
|
|
1370
|
+
|
|
1371
|
+
const targetMessageId = target.id;
|
|
1372
|
+
chatHelpers.setMessages((current) =>
|
|
1373
|
+
current.map((message) => {
|
|
1374
|
+
if (message.id !== targetMessageId) return message;
|
|
1375
|
+
|
|
1376
|
+
return {
|
|
1377
|
+
...message,
|
|
1378
|
+
parts: message.parts.map((part) => {
|
|
1379
|
+
if (
|
|
1380
|
+
typeof part?.type !== "string" ||
|
|
1381
|
+
!isToolUIPart(part) ||
|
|
1382
|
+
part.toolCallId !== toolCallId
|
|
1383
|
+
)
|
|
1384
|
+
return part;
|
|
1385
|
+
|
|
1386
|
+
const { preliminary: _preliminary, ...finalPart } =
|
|
1387
|
+
part as typeof part & { preliminary?: boolean };
|
|
1388
|
+
return {
|
|
1389
|
+
...finalPart,
|
|
1390
|
+
state: isError
|
|
1391
|
+
? ("output-error" as const)
|
|
1392
|
+
: ("output-available" as const),
|
|
1393
|
+
output: isError ? undefined : output,
|
|
1394
|
+
errorText: isError ? errorText : undefined,
|
|
1395
|
+
} as typeof part;
|
|
1396
|
+
}),
|
|
1397
|
+
};
|
|
1398
|
+
}),
|
|
1399
|
+
);
|
|
1400
|
+
return Promise.resolve();
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
if (wasCancelled) return Promise.resolve();
|
|
1404
|
+
|
|
1405
|
+
const options = { metadata: lastRunConfigRef.current };
|
|
1406
|
+
if (isError) {
|
|
1407
|
+
return Promise.resolve(
|
|
1408
|
+
chatHelpers.addToolOutput({
|
|
1409
|
+
state: "output-error",
|
|
1410
|
+
tool: toolName ?? toolCallId,
|
|
1411
|
+
toolCallId,
|
|
1412
|
+
errorText,
|
|
1413
|
+
options,
|
|
1414
|
+
}),
|
|
1415
|
+
);
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
return Promise.resolve(
|
|
1419
|
+
chatHelpers.addToolOutput({
|
|
1420
|
+
tool: toolName,
|
|
1421
|
+
toolCallId,
|
|
1422
|
+
output,
|
|
1423
|
+
options,
|
|
1424
|
+
}),
|
|
1425
|
+
);
|
|
1426
|
+
},
|
|
1427
|
+
onRespondToToolApproval: (response) => {
|
|
1428
|
+
if (supersededApprovalProjection.approvalIds.has(response.approvalId))
|
|
1429
|
+
return Promise.reject(
|
|
1430
|
+
new Error(
|
|
1431
|
+
`Tool approval ${response.approvalId} is not waiting for a response.`,
|
|
1432
|
+
),
|
|
1433
|
+
);
|
|
1434
|
+
return customOnRespondToToolApproval
|
|
1435
|
+
? respondViaHost(customOnRespondToToolApproval, response)
|
|
1436
|
+
: respondViaAISDK(response);
|
|
1437
|
+
},
|
|
1438
|
+
unstable_onRecordToolInteraction: ({
|
|
1439
|
+
messageId,
|
|
1440
|
+
toolCallId,
|
|
1441
|
+
interaction,
|
|
1442
|
+
}) => {
|
|
1443
|
+
const next = new Map(toolInteractionsRef.current);
|
|
1444
|
+
next.set(
|
|
1445
|
+
toolCallId,
|
|
1446
|
+
appendToolInteraction(next.get(toolCallId), interaction),
|
|
1447
|
+
);
|
|
1448
|
+
toolInteractionsRef.current = next;
|
|
1449
|
+
setToolInteractions(next);
|
|
1450
|
+
return persistToolInteractions(messageId);
|
|
1451
|
+
},
|
|
1452
|
+
...pickExternalStoreSharedOptions(adapter),
|
|
1453
|
+
...(adapter.unstable_messageRepositoryInstance && {
|
|
1454
|
+
unstable_messageRepositoryInstance:
|
|
1455
|
+
adapter.unstable_messageRepositoryInstance,
|
|
1456
|
+
}),
|
|
1457
|
+
...(suggestionAdapter ? { suggestions: generatedSuggestions } : {}),
|
|
1458
|
+
...(onResume && { onResume }),
|
|
1459
|
+
...(canResume !== undefined && { canResume }),
|
|
1460
|
+
...(onResumeToolCall && { onResumeToolCall }),
|
|
1461
|
+
...(unstable_onBranchChange && { unstable_onBranchChange }),
|
|
1462
|
+
adapters: {
|
|
1463
|
+
attachments: vercelAttachmentAdapter,
|
|
1464
|
+
...contextAdapters,
|
|
1465
|
+
...adapters,
|
|
1466
|
+
},
|
|
1467
|
+
extras,
|
|
1468
|
+
isLoading,
|
|
1469
|
+
});
|
|
1470
|
+
|
|
1471
|
+
const setMessagesRef = useRef(chatHelpers.setMessages);
|
|
1472
|
+
useInsertionEffect(() => {
|
|
1473
|
+
setMessagesRef.current = chatHelpers.setMessages;
|
|
1474
|
+
}, [chatHelpers.setMessages]);
|
|
1475
|
+
|
|
1476
|
+
useEffect(() => {
|
|
1477
|
+
if (hasSeededRepositoryRef.current) return;
|
|
1478
|
+
if (!exportedMessageRepository) return;
|
|
1479
|
+
if (chatHelpers.messages.length > 0) {
|
|
1480
|
+
hasSeededRepositoryRef.current = true;
|
|
1481
|
+
return;
|
|
1482
|
+
}
|
|
1483
|
+
const tempRepo = new MessageRepository();
|
|
1484
|
+
tempRepo.import(exportedMessageRepository);
|
|
1485
|
+
setMessagesRef.current(
|
|
1486
|
+
tempRepo.getMessages().flatMap(getExternalStoreMessages<UI_MESSAGE>),
|
|
1487
|
+
);
|
|
1488
|
+
hasSeededRepositoryRef.current = true;
|
|
1489
|
+
}, [exportedMessageRepository, chatHelpers.messages.length]);
|
|
1490
|
+
return runtime;
|
|
1491
|
+
};
|
|
1492
|
+
|
|
1493
|
+
/** Vue setup owns the resource; reactive helper/adapter getters stay live. */
|
|
1494
|
+
export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
|
|
1495
|
+
helpers: MaybeRefOrGetter<AISDKChatHelpers<UI_MESSAGE>>,
|
|
1496
|
+
adapter: MaybeRefOrGetter<AISDKRuntimeAdapter<UI_MESSAGE>> = {},
|
|
1497
|
+
): AssistantRuntime => {
|
|
1498
|
+
if (isResourceExecution())
|
|
1499
|
+
return useAISDKRuntimeResource(toValue(helpers), toValue(adapter));
|
|
1500
|
+
return useRuntimeResource(() =>
|
|
1501
|
+
useAISDKRuntimeResource(toValue(helpers), toValue(adapter)),
|
|
1502
|
+
);
|
|
1503
|
+
};
|