@assistant-ui/react-ai-sdk 1.3.28 → 1.3.30

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/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="@assistant-ui/core/store" />
2
1
  /// <reference types="@assistant-ui/core/react" />
3
2
  import { frontendTools } from "./frontendTools.js";
4
3
  import { useAISDKRuntime } from "./ui/use-chat/useAISDKRuntime.js";
@@ -1 +1 @@
1
- {"version":3,"file":"useAISDKRuntime.d.ts","names":[],"sources":["../../../src/ui/use-chat/useAISDKRuntime.ts"],"mappings":";;;;KA0CY,6BAAA,uBACS,SAAA,GAAY,SAAA,EAE/B,OAAA,EAAS,aAAA,KACN,eAAA,CAAgB,UAAA;AAAA,KAYT,mBAAA;EACV,QAAA,IACK,WAAA,CAAY,oBAAA;IACX,OAAA,GAAU,oBAAA;EAAA;EAGhB,eAAA,GAAkB,6BAAA;EArBa;;;;;;;;EA8B/B,4BAAA;EA5BS;;;;;AACoB;AAY/B;EAuBE,QAAA,GAAW,oBAAA;;;;;;;EAOX,WAAA,YAAuB,gBAAA;AAAA;AAAA,cAGZ,eAAA,sBAAsC,SAAA,GAAY,SAAA,EAC7D,WAAA,EAAa,UAAA,QAAkB,OAAA,CAAQ,UAAA;EACvC,QAAA;EAAA,eAAA,EAAA,qBAAA;EAAA,4BAAA;EAAA,QAAA;EAAA;AAAA,IAMG,mBAAA,KAAwB,gBAAA"}
1
+ {"version":3,"file":"useAISDKRuntime.d.ts","names":[],"sources":["../../../src/ui/use-chat/useAISDKRuntime.ts"],"mappings":";;;;KAyCY,6BAAA,uBACS,SAAA,GAAY,SAAA,EAE/B,OAAA,EAAS,aAAA,KACN,eAAA,CAAgB,UAAA;AAAA,KAYT,mBAAA;EACV,QAAA,IACK,WAAA,CAAY,oBAAA;IACX,OAAA,GAAU,oBAAA;EAAA;EAGhB,eAAA,GAAkB,6BAAA;EArBa;;;;;;;;EA8B/B,4BAAA;EA5BS;;;;;AACoB;AAY/B;EAuBE,QAAA,GAAW,oBAAA;;;;;;;EAOX,WAAA,YAAuB,gBAAA;AAAA;AAAA,cAGZ,eAAA,sBAAsC,SAAA,GAAY,SAAA,EAC7D,WAAA,EAAa,UAAA,QAAkB,OAAA,CAAQ,UAAA;EACvC,QAAA;EAAA,eAAA,EAAA,qBAAA;EAAA,4BAAA;EAAA,QAAA;EAAA;AAAA,IAMG,mBAAA,KAAwB,gBAAA"}
@@ -10,7 +10,7 @@ import { toExportedMessageRepository, useExternalHistory } from "./useExternalHi
10
10
  import { useStreamingTiming } from "./useStreamingTiming.js";
11
11
  import { generateId, isToolUIPart } from "ai";
12
12
  import { useMemo, useRef, useState } from "react";
13
- import { useExternalStoreRuntime, useRuntimeAdapters, useToolInvocations } from "@assistant-ui/core/react";
13
+ import { useExternalStoreRuntime, useRuntimeAdapters } from "@assistant-ui/core/react";
14
14
  import { getExternalStoreMessages } from "@assistant-ui/core";
15
15
  //#region src/ui/use-chat/useAISDKRuntime.ts
16
16
  const toUIMessage = (createMessage, fallbackRole) => ({
@@ -47,31 +47,11 @@ const useAISDKRuntime = (chatHelpers, { adapters, toCreateMessage: customToCreat
47
47
  const [runtimeRef] = useState(() => ({ get current() {
48
48
  return runtime;
49
49
  } }));
50
- const toolInvocations = useToolInvocations({
51
- state: {
52
- messages,
53
- isRunning
54
- },
55
- getTools: () => runtimeRef.current.thread.getModelContext().tools,
56
- onResult: (command) => {
57
- if (command.type === "add-tool-result") {
58
- const output = command.modelContent !== void 0 ? wrapModelContentEnvelope(command.result, command.modelContent) : command.result;
59
- chatHelpers.addToolResult({
60
- tool: command.toolName,
61
- toolCallId: command.toolCallId,
62
- output,
63
- options: { metadata: lastRunConfigRef.current }
64
- });
65
- }
66
- },
67
- setToolStatuses
68
- });
69
50
  const isLoading = useExternalHistory(runtimeRef, adapters?.history ?? contextAdapters?.history, AISDKMessageConverter.toThreadMessages, aiSDKV6FormatAdapter, (messages) => {
70
51
  chatHelpers.setMessages(messages);
71
52
  });
72
53
  const completePendingToolCalls = async () => {
73
54
  if (!cancelPendingToolCallsOnSend) return;
74
- await toolInvocations.abort();
75
55
  chatHelpers.setMessages((messages) => {
76
56
  const lastMessage = messages.at(-1);
77
57
  if (lastMessage?.role !== "assistant") return messages;
@@ -96,6 +76,8 @@ const useAISDKRuntime = (chatHelpers, { adapters, toCreateMessage: customToCreat
96
76
  const runtime = useExternalStoreRuntime({
97
77
  isRunning,
98
78
  messages,
79
+ unstable_enableToolInvocations: true,
80
+ setToolStatuses,
99
81
  setMessages: (messages) => chatHelpers.setMessages(messages.map(getVercelAIMessages).filter(Boolean).flat()),
100
82
  onImport: (messages) => chatHelpers.setMessages(messages.map(getVercelAIMessages).filter(Boolean).flat()),
101
83
  onExportExternalState: () => {
@@ -124,7 +106,6 @@ const useAISDKRuntime = (chatHelpers, { adapters, toCreateMessage: customToCreat
124
106
  },
125
107
  onCancel: async () => {
126
108
  chatHelpers.stop();
127
- await toolInvocations.abort();
128
109
  },
129
110
  onNew: async (message) => {
130
111
  const createMessage = (customToCreateMessage ?? toCreateMessage)(message);
@@ -152,24 +133,25 @@ const useAISDKRuntime = (chatHelpers, { adapters, toCreateMessage: customToCreat
152
133
  chatHelpers.setMessages(newMessages);
153
134
  await chatHelpers.regenerate({ metadata: config.runConfig });
154
135
  },
155
- onAddToolResult: ({ toolCallId, result, isError }) => {
136
+ onAddToolResult: ({ toolCallId, toolName, result, isError, modelContent }) => {
156
137
  const options = { metadata: lastRunConfigRef.current };
157
138
  if (isError) chatHelpers.addToolOutput({
158
139
  state: "output-error",
159
- tool: toolCallId,
140
+ tool: toolName ?? toolCallId,
160
141
  toolCallId,
161
142
  errorText: typeof result === "string" ? result : JSON.stringify(result),
162
143
  options
163
144
  });
164
- else chatHelpers.addToolOutput({
165
- state: "output-available",
166
- tool: toolCallId,
167
- toolCallId,
168
- output: result,
169
- options
170
- });
145
+ else {
146
+ const output = modelContent !== void 0 ? wrapModelContentEnvelope(result, modelContent) : result;
147
+ chatHelpers.addToolResult({
148
+ tool: toolName,
149
+ toolCallId,
150
+ output,
151
+ options
152
+ });
153
+ }
171
154
  },
172
- onResumeToolCall: (options) => toolInvocations.resume(options.toolCallId, options.payload),
173
155
  onRespondToToolApproval: ({ approvalId, approved, reason }) => {
174
156
  chatHelpers.addToolApprovalResponse({
175
157
  id: approvalId,
@@ -1 +1 @@
1
- {"version":3,"file":"useAISDKRuntime.js","names":[],"sources":["../../../src/ui/use-chat/useAISDKRuntime.ts"],"sourcesContent":["\"use client\";\n\nimport { useState, useMemo, useRef } from \"react\";\nimport type { UIMessage, useChat, CreateUIMessage } from \"@ai-sdk/react\";\nimport { isToolUIPart, generateId } from \"ai\";\nimport {\n useExternalStoreRuntime,\n useRuntimeAdapters,\n useToolInvocations,\n type ToolExecutionStatus,\n} from \"@assistant-ui/core/react\";\nimport type {\n ExternalStoreAdapter,\n ThreadHistoryAdapter,\n AssistantRuntime,\n ThreadMessage,\n ThreadSuggestion,\n MessageFormatAdapter,\n MessageFormatItem,\n MessageFormatRepository,\n AppendMessage,\n RunConfig,\n McpAppMetadata,\n} from \"@assistant-ui/core\";\nimport { getExternalStoreMessages } from \"@assistant-ui/core\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport { sliceMessagesUntil } from \"../utils/sliceMessagesUntil\";\nimport { toCreateMessage } from \"../utils/toCreateMessage\";\nimport { vercelAttachmentAdapter } from \"../utils/vercelAttachmentAdapter\";\nimport { getVercelAIMessages } from \"../getVercelAIMessages\";\nimport { AISDKMessageConverter } from \"../utils/convertMessage\";\nimport { wrapModelContentEnvelope } from \"../../modelContentEnvelope\";\nimport {\n type AISDKStorageFormat,\n aiSDKV6FormatAdapter,\n} from \"../adapters/aiSDKFormatAdapter\";\nimport {\n useExternalHistory,\n toExportedMessageRepository,\n} from \"./useExternalHistory\";\nimport { useStreamingTiming } from \"./useStreamingTiming\";\n\nexport type CustomToCreateMessageFunction = <\n UI_MESSAGE extends UIMessage = UIMessage,\n>(\n message: AppendMessage,\n) => CreateUIMessage<UI_MESSAGE>;\n\nconst toUIMessage = <UI_MESSAGE extends UIMessage>(\n createMessage: CreateUIMessage<UI_MESSAGE>,\n fallbackRole: UI_MESSAGE[\"role\"],\n): UI_MESSAGE =>\n ({\n ...createMessage,\n id: createMessage.id ?? generateId(),\n role: createMessage.role ?? fallbackRole,\n }) as UI_MESSAGE;\n\nexport type AISDKRuntimeAdapter = {\n adapters?:\n | (NonNullable<ExternalStoreAdapter[\"adapters\"]> & {\n history?: ThreadHistoryAdapter | undefined;\n })\n | undefined;\n toCreateMessage?: CustomToCreateMessageFunction;\n /**\n * Whether to automatically cancel pending interactive tool calls when the user sends a new message.\n *\n * When enabled (default), the pending tool calls will be marked as failed with an error message\n * indicating the user cancelled the tool call by sending a new message.\n *\n * @default true\n */\n cancelPendingToolCallsOnSend?: boolean | undefined;\n /**\n * Called when `runtime.thread.resumeRun(config)` is invoked.\n *\n * When omitted, `resumeRun` throws `\"Runtime does not support resuming runs.\"`.\n * Provide this to bridge resume invocations into a custom replay channel\n * (for example, an SSE reconnect endpoint keyed by turn id).\n */\n onResume?: ExternalStoreAdapter[\"onResume\"];\n /**\n * Follow up suggestions to surface on the thread. Use this to drive\n * dynamic suggestions from application state, tool results, or backend\n * responses; flows into `thread.suggestions` and is rendered by\n * components that read it (such as the shadcn `ThreadFollowupSuggestions`).\n */\n suggestions?: readonly ThreadSuggestion[] | undefined;\n};\n\nexport const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(\n chatHelpers: ReturnType<typeof useChat<UI_MESSAGE>>,\n {\n adapters,\n toCreateMessage: customToCreateMessage,\n cancelPendingToolCallsOnSend = true,\n onResume,\n suggestions,\n }: AISDKRuntimeAdapter = {},\n) => {\n const contextAdapters = useRuntimeAdapters();\n const [toolStatuses, setToolStatuses] = useState<\n Record<string, ToolExecutionStatus>\n >({});\n const toolArgsKeyOrderCacheRef = useRef<Map<string, Map<string, string[]>>>(\n new Map(),\n );\n const toolLastInputCacheRef = useRef<Map<string, ReadonlyJSONObject>>(\n new Map(),\n );\n const mcpAppMetadataCacheRef = useRef<Map<string, McpAppMetadata>>(new Map());\n const lastRunConfigRef = useRef<RunConfig | undefined>(undefined);\n\n const hasExecutingTools = Object.values(toolStatuses).some(\n (s) => s?.type === \"executing\",\n );\n const isRunning =\n chatHelpers.status === \"submitted\" ||\n chatHelpers.status === \"streaming\" ||\n hasExecutingTools;\n\n const messageTiming = useStreamingTiming(chatHelpers.messages, isRunning);\n\n const messages = AISDKMessageConverter.useThreadMessages({\n isRunning,\n messages: chatHelpers.messages,\n metadata: useMemo(\n () => ({\n toolStatuses,\n messageTiming,\n toolArgsKeyOrderCache: toolArgsKeyOrderCacheRef.current,\n toolLastInputCache: toolLastInputCacheRef.current,\n mcpAppMetadataCache: mcpAppMetadataCacheRef.current,\n ...(chatHelpers.error && { error: chatHelpers.error.message }),\n }),\n [toolStatuses, messageTiming, chatHelpers.error],\n ),\n });\n\n const [runtimeRef] = useState(() => ({\n get current(): AssistantRuntime {\n return runtime;\n },\n }));\n\n const toolInvocations = useToolInvocations({\n state: {\n messages,\n isRunning,\n },\n getTools: () => runtimeRef.current.thread.getModelContext().tools,\n onResult: (command) => {\n if (command.type === \"add-tool-result\") {\n const output =\n command.modelContent !== undefined\n ? wrapModelContentEnvelope(command.result, command.modelContent)\n : command.result;\n chatHelpers.addToolResult({\n tool: command.toolName,\n toolCallId: command.toolCallId,\n output,\n options: { metadata: lastRunConfigRef.current },\n });\n }\n },\n setToolStatuses,\n });\n\n const isLoading = useExternalHistory(\n runtimeRef,\n adapters?.history ?? contextAdapters?.history,\n AISDKMessageConverter.toThreadMessages as (\n messages: UI_MESSAGE[],\n ) => ThreadMessage[],\n aiSDKV6FormatAdapter as MessageFormatAdapter<\n UI_MESSAGE,\n AISDKStorageFormat\n >,\n (messages) => {\n chatHelpers.setMessages(messages);\n },\n );\n\n const completePendingToolCalls = async () => {\n if (!cancelPendingToolCallsOnSend) return;\n\n await toolInvocations.abort();\n\n // Mark any tool without a result as cancelled (uses setMessages to avoid triggering sendAutomaticallyWhen)\n chatHelpers.setMessages((messages) => {\n const lastMessage = messages.at(-1);\n if (lastMessage?.role !== \"assistant\") return messages;\n\n let hasChanges = false;\n const parts = lastMessage.parts?.map((part) => {\n if (!isToolUIPart(part)) return part;\n if (part.state === \"output-available\" || part.state === \"output-error\")\n return part;\n\n hasChanges = true;\n return {\n ...part,\n state: \"output-error\" as const,\n errorText: \"User cancelled tool call by sending a new message.\",\n };\n });\n\n if (!hasChanges) return messages;\n return [...messages.slice(0, -1), { ...lastMessage, parts }];\n });\n };\n\n const runtime = useExternalStoreRuntime({\n isRunning,\n messages,\n setMessages: (messages) =>\n chatHelpers.setMessages(\n messages\n .map(getVercelAIMessages<UI_MESSAGE>)\n .filter(Boolean)\n .flat(),\n ),\n onImport: (messages) =>\n chatHelpers.setMessages(\n messages\n .map(getVercelAIMessages<UI_MESSAGE>)\n .filter(Boolean)\n .flat(),\n ),\n onExportExternalState: (): MessageFormatRepository<UI_MESSAGE> => {\n const exported = runtimeRef.current.thread.export();\n\n const expandedMessages: MessageFormatItem<UI_MESSAGE>[] = [];\n const lastInnerIdMap = new Map<string, string>();\n\n for (const item of exported.messages) {\n const innerMessages = getExternalStoreMessages<UI_MESSAGE>(\n item.message,\n );\n let parentId =\n item.parentId != null\n ? (lastInnerIdMap.get(item.parentId) ?? item.parentId)\n : null;\n for (const innerMessage of innerMessages) {\n expandedMessages.push({ parentId, message: innerMessage });\n parentId = aiSDKV6FormatAdapter.getId(innerMessage as UIMessage);\n }\n if (innerMessages.length > 0) {\n lastInnerIdMap.set(\n item.message.id,\n aiSDKV6FormatAdapter.getId(\n innerMessages[innerMessages.length - 1]! as UIMessage,\n ),\n );\n }\n }\n\n const result: MessageFormatRepository<UI_MESSAGE> = {\n messages: expandedMessages,\n };\n\n if (exported.headId != null) {\n result.headId = lastInnerIdMap.get(exported.headId) ?? exported.headId;\n }\n\n return result;\n },\n onLoadExternalState: (repo: MessageFormatRepository<UI_MESSAGE>) => {\n // Convert MessageFormatRepository to ExportedMessageRepository\n const exportedRepo = toExportedMessageRepository(\n AISDKMessageConverter.toThreadMessages,\n repo,\n );\n\n // Import into the thread's MessageRepository\n runtimeRef.current.thread.import(exportedRepo);\n },\n onCancel: async () => {\n chatHelpers.stop();\n await toolInvocations.abort();\n },\n onNew: async (message) => {\n const createMessage = (\n customToCreateMessage ?? toCreateMessage\n )<UI_MESSAGE>(message);\n\n if (!(message.startRun ?? message.role === \"user\")) {\n chatHelpers.setMessages((current) => [\n ...current,\n toUIMessage<UI_MESSAGE>(createMessage, message.role),\n ]);\n return;\n }\n\n lastRunConfigRef.current = message.runConfig;\n await completePendingToolCalls();\n await chatHelpers.sendMessage(createMessage, {\n metadata: message.runConfig,\n });\n },\n onEdit: async (message) => {\n const createMessage = (\n customToCreateMessage ?? toCreateMessage\n )<UI_MESSAGE>(message);\n\n if (!(message.startRun ?? message.role === \"user\")) {\n chatHelpers.setMessages((current) => [\n ...sliceMessagesUntil(current, message.parentId),\n toUIMessage<UI_MESSAGE>(createMessage, message.role),\n ]);\n return;\n }\n\n lastRunConfigRef.current = message.runConfig;\n chatHelpers.setMessages((current) =>\n sliceMessagesUntil(current, message.parentId),\n );\n await chatHelpers.sendMessage(createMessage, {\n metadata: message.runConfig,\n });\n },\n onReload: async (parentId: string | null, config) => {\n lastRunConfigRef.current = config.runConfig;\n const newMessages = sliceMessagesUntil(chatHelpers.messages, parentId);\n chatHelpers.setMessages(newMessages);\n\n await chatHelpers.regenerate({ metadata: config.runConfig });\n },\n onAddToolResult: ({ toolCallId, result, isError }) => {\n const options = { metadata: lastRunConfigRef.current };\n if (isError) {\n chatHelpers.addToolOutput({\n state: \"output-error\",\n tool: toolCallId,\n toolCallId,\n errorText:\n typeof result === \"string\" ? result : JSON.stringify(result),\n options,\n });\n } else {\n chatHelpers.addToolOutput({\n state: \"output-available\",\n tool: toolCallId,\n toolCallId,\n output: result,\n options,\n });\n }\n },\n onResumeToolCall: (options) =>\n toolInvocations.resume(options.toolCallId, options.payload),\n onRespondToToolApproval: ({ approvalId, approved, reason }) => {\n void chatHelpers.addToolApprovalResponse({\n id: approvalId,\n approved,\n ...(reason != null && { reason }),\n options: { metadata: lastRunConfigRef.current },\n });\n },\n ...(onResume && { onResume }),\n ...(suggestions && { suggestions }),\n adapters: {\n attachments: vercelAttachmentAdapter,\n ...contextAdapters,\n ...adapters,\n },\n isLoading,\n });\n\n return runtime;\n};\n"],"mappings":";;;;;;;;;;;;;;;AAgDA,MAAM,eACJ,eACA,kBAEC;CACC,GAAG;CACH,IAAI,cAAc,MAAM,WAAW;CACnC,MAAM,cAAc,QAAQ;AAC9B;AAmCF,MAAa,mBACX,aACA,EACE,UACA,iBAAiB,uBACjB,+BAA+B,MAC/B,UACA,gBACuB,CAAC,MACvB;CACH,MAAM,kBAAkB,mBAAmB;CAC3C,MAAM,CAAC,cAAc,mBAAmB,SAEtC,CAAC,CAAC;CACJ,MAAM,2BAA2B,uBAC/B,IAAI,IAAI,CACV;CACA,MAAM,wBAAwB,uBAC5B,IAAI,IAAI,CACV;CACA,MAAM,yBAAyB,uBAAoC,IAAI,IAAI,CAAC;CAC5E,MAAM,mBAAmB,OAA8B,KAAA,CAAS;CAEhE,MAAM,oBAAoB,OAAO,OAAO,YAAY,EAAE,MACnD,MAAM,GAAG,SAAS,WACrB;CACA,MAAM,YACJ,YAAY,WAAW,eACvB,YAAY,WAAW,eACvB;CAEF,MAAM,gBAAgB,mBAAmB,YAAY,UAAU,SAAS;CAExE,MAAM,WAAW,sBAAsB,kBAAkB;EACvD;EACA,UAAU,YAAY;EACtB,UAAU,eACD;GACL;GACA;GACA,uBAAuB,yBAAyB;GAChD,oBAAoB,sBAAsB;GAC1C,qBAAqB,uBAAuB;GAC5C,GAAI,YAAY,SAAS,EAAE,OAAO,YAAY,MAAM,QAAQ;EAC9D,IACA;GAAC;GAAc;GAAe,YAAY;EA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EACA;CACF,CAAC;CAED,OAAO;AACT"}
1
+ {"version":3,"file":"useAISDKRuntime.js","names":[],"sources":["../../../src/ui/use-chat/useAISDKRuntime.ts"],"sourcesContent":["\"use client\";\n\nimport { useMemo, useRef, useState } from \"react\";\nimport type { UIMessage, useChat, CreateUIMessage } from \"@ai-sdk/react\";\nimport { isToolUIPart, generateId } from \"ai\";\nimport {\n useExternalStoreRuntime,\n useRuntimeAdapters,\n} from \"@assistant-ui/core/react\";\nimport type { ToolExecutionStatus } from \"@assistant-ui/core\";\nimport type {\n ExternalStoreAdapter,\n ThreadHistoryAdapter,\n AssistantRuntime,\n ThreadMessage,\n ThreadSuggestion,\n MessageFormatAdapter,\n MessageFormatItem,\n MessageFormatRepository,\n AppendMessage,\n RunConfig,\n McpAppMetadata,\n} from \"@assistant-ui/core\";\nimport { getExternalStoreMessages } from \"@assistant-ui/core\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\nimport { sliceMessagesUntil } from \"../utils/sliceMessagesUntil\";\nimport { toCreateMessage } from \"../utils/toCreateMessage\";\nimport { vercelAttachmentAdapter } from \"../utils/vercelAttachmentAdapter\";\nimport { getVercelAIMessages } from \"../getVercelAIMessages\";\nimport { AISDKMessageConverter } from \"../utils/convertMessage\";\nimport { wrapModelContentEnvelope } from \"../../modelContentEnvelope\";\nimport {\n type AISDKStorageFormat,\n aiSDKV6FormatAdapter,\n} from \"../adapters/aiSDKFormatAdapter\";\nimport {\n useExternalHistory,\n toExportedMessageRepository,\n} from \"./useExternalHistory\";\nimport { useStreamingTiming } from \"./useStreamingTiming\";\n\nexport type CustomToCreateMessageFunction = <\n UI_MESSAGE extends UIMessage = UIMessage,\n>(\n message: AppendMessage,\n) => CreateUIMessage<UI_MESSAGE>;\n\nconst toUIMessage = <UI_MESSAGE extends UIMessage>(\n createMessage: CreateUIMessage<UI_MESSAGE>,\n fallbackRole: UI_MESSAGE[\"role\"],\n): UI_MESSAGE =>\n ({\n ...createMessage,\n id: createMessage.id ?? generateId(),\n role: createMessage.role ?? fallbackRole,\n }) as UI_MESSAGE;\n\nexport type AISDKRuntimeAdapter = {\n adapters?:\n | (NonNullable<ExternalStoreAdapter[\"adapters\"]> & {\n history?: ThreadHistoryAdapter | undefined;\n })\n | undefined;\n toCreateMessage?: CustomToCreateMessageFunction;\n /**\n * Whether to automatically cancel pending interactive tool calls when the user sends a new message.\n *\n * When enabled (default), the pending tool calls will be marked as failed with an error message\n * indicating the user cancelled the tool call by sending a new message.\n *\n * @default true\n */\n cancelPendingToolCallsOnSend?: boolean | undefined;\n /**\n * Called when `runtime.thread.resumeRun(config)` is invoked.\n *\n * When omitted, `resumeRun` throws `\"Runtime does not support resuming runs.\"`.\n * Provide this to bridge resume invocations into a custom replay channel\n * (for example, an SSE reconnect endpoint keyed by turn id).\n */\n onResume?: ExternalStoreAdapter[\"onResume\"];\n /**\n * Follow up suggestions to surface on the thread. Use this to drive\n * dynamic suggestions from application state, tool results, or backend\n * responses; flows into `thread.suggestions` and is rendered by\n * components that read it (such as the shadcn `ThreadFollowupSuggestions`).\n */\n suggestions?: readonly ThreadSuggestion[] | undefined;\n};\n\nexport const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(\n chatHelpers: ReturnType<typeof useChat<UI_MESSAGE>>,\n {\n adapters,\n toCreateMessage: customToCreateMessage,\n cancelPendingToolCallsOnSend = true,\n onResume,\n suggestions,\n }: AISDKRuntimeAdapter = {},\n) => {\n const contextAdapters = useRuntimeAdapters();\n const [toolStatuses, setToolStatuses] = useState<\n Record<string, ToolExecutionStatus>\n >({});\n const toolArgsKeyOrderCacheRef = useRef<Map<string, Map<string, string[]>>>(\n new Map(),\n );\n const toolLastInputCacheRef = useRef<Map<string, ReadonlyJSONObject>>(\n new Map(),\n );\n const mcpAppMetadataCacheRef = useRef<Map<string, McpAppMetadata>>(new Map());\n const lastRunConfigRef = useRef<RunConfig | undefined>(undefined);\n\n const hasExecutingTools = Object.values(toolStatuses).some(\n (s) => s?.type === \"executing\",\n );\n const isRunning =\n chatHelpers.status === \"submitted\" ||\n chatHelpers.status === \"streaming\" ||\n hasExecutingTools;\n\n const messageTiming = useStreamingTiming(chatHelpers.messages, isRunning);\n\n const messages = AISDKMessageConverter.useThreadMessages({\n isRunning,\n messages: chatHelpers.messages,\n metadata: useMemo(\n () => ({\n toolStatuses,\n messageTiming,\n toolArgsKeyOrderCache: toolArgsKeyOrderCacheRef.current,\n toolLastInputCache: toolLastInputCacheRef.current,\n mcpAppMetadataCache: mcpAppMetadataCacheRef.current,\n ...(chatHelpers.error && { error: chatHelpers.error.message }),\n }),\n [toolStatuses, messageTiming, chatHelpers.error],\n ),\n });\n\n const [runtimeRef] = useState(() => ({\n get current(): AssistantRuntime {\n return runtime;\n },\n }));\n\n const isLoading = useExternalHistory(\n runtimeRef,\n adapters?.history ?? contextAdapters?.history,\n AISDKMessageConverter.toThreadMessages as (\n messages: UI_MESSAGE[],\n ) => ThreadMessage[],\n aiSDKV6FormatAdapter as MessageFormatAdapter<\n UI_MESSAGE,\n AISDKStorageFormat\n >,\n (messages) => {\n chatHelpers.setMessages(messages);\n },\n );\n\n const completePendingToolCalls = async () => {\n if (!cancelPendingToolCallsOnSend) return;\n\n // The runtime auto-aborts in-flight tool invocations when a new run\n // is dispatched (append() / startRun()). All we need to do here is\n // mark any tool without a result as cancelled in the UI message list.\n\n // Mark any tool without a result as cancelled (uses setMessages to avoid triggering sendAutomaticallyWhen)\n chatHelpers.setMessages((messages) => {\n const lastMessage = messages.at(-1);\n if (lastMessage?.role !== \"assistant\") return messages;\n\n let hasChanges = false;\n const parts = lastMessage.parts?.map((part) => {\n if (!isToolUIPart(part)) return part;\n if (part.state === \"output-available\" || part.state === \"output-error\")\n return part;\n\n hasChanges = true;\n return {\n ...part,\n state: \"output-error\" as const,\n errorText: \"User cancelled tool call by sending a new message.\",\n };\n });\n\n if (!hasChanges) return messages;\n return [...messages.slice(0, -1), { ...lastMessage, parts }];\n });\n };\n\n const runtime = useExternalStoreRuntime({\n isRunning,\n messages,\n unstable_enableToolInvocations: true,\n setToolStatuses,\n setMessages: (messages) =>\n chatHelpers.setMessages(\n messages\n .map(getVercelAIMessages<UI_MESSAGE>)\n .filter(Boolean)\n .flat(),\n ),\n onImport: (messages) =>\n chatHelpers.setMessages(\n messages\n .map(getVercelAIMessages<UI_MESSAGE>)\n .filter(Boolean)\n .flat(),\n ),\n onExportExternalState: (): MessageFormatRepository<UI_MESSAGE> => {\n const exported = runtimeRef.current.thread.export();\n\n const expandedMessages: MessageFormatItem<UI_MESSAGE>[] = [];\n const lastInnerIdMap = new Map<string, string>();\n\n for (const item of exported.messages) {\n const innerMessages = getExternalStoreMessages<UI_MESSAGE>(\n item.message,\n );\n let parentId =\n item.parentId != null\n ? (lastInnerIdMap.get(item.parentId) ?? item.parentId)\n : null;\n for (const innerMessage of innerMessages) {\n expandedMessages.push({ parentId, message: innerMessage });\n parentId = aiSDKV6FormatAdapter.getId(innerMessage as UIMessage);\n }\n if (innerMessages.length > 0) {\n lastInnerIdMap.set(\n item.message.id,\n aiSDKV6FormatAdapter.getId(\n innerMessages[innerMessages.length - 1]! as UIMessage,\n ),\n );\n }\n }\n\n const result: MessageFormatRepository<UI_MESSAGE> = {\n messages: expandedMessages,\n };\n\n if (exported.headId != null) {\n result.headId = lastInnerIdMap.get(exported.headId) ?? exported.headId;\n }\n\n return result;\n },\n onLoadExternalState: (repo: MessageFormatRepository<UI_MESSAGE>) => {\n // Convert MessageFormatRepository to ExportedMessageRepository\n const exportedRepo = toExportedMessageRepository(\n AISDKMessageConverter.toThreadMessages,\n repo,\n );\n\n // Import into the thread's MessageRepository\n runtimeRef.current.thread.import(exportedRepo);\n },\n onCancel: async () => {\n chatHelpers.stop();\n },\n onNew: async (message) => {\n const createMessage = (\n customToCreateMessage ?? toCreateMessage\n )<UI_MESSAGE>(message);\n\n if (!(message.startRun ?? message.role === \"user\")) {\n chatHelpers.setMessages((current) => [\n ...current,\n toUIMessage<UI_MESSAGE>(createMessage, message.role),\n ]);\n return;\n }\n\n lastRunConfigRef.current = message.runConfig;\n await completePendingToolCalls();\n await chatHelpers.sendMessage(createMessage, {\n metadata: message.runConfig,\n });\n },\n onEdit: async (message) => {\n const createMessage = (\n customToCreateMessage ?? toCreateMessage\n )<UI_MESSAGE>(message);\n\n if (!(message.startRun ?? message.role === \"user\")) {\n chatHelpers.setMessages((current) => [\n ...sliceMessagesUntil(current, message.parentId),\n toUIMessage<UI_MESSAGE>(createMessage, message.role),\n ]);\n return;\n }\n\n lastRunConfigRef.current = message.runConfig;\n chatHelpers.setMessages((current) =>\n sliceMessagesUntil(current, message.parentId),\n );\n await chatHelpers.sendMessage(createMessage, {\n metadata: message.runConfig,\n });\n },\n onReload: async (parentId: string | null, config) => {\n lastRunConfigRef.current = config.runConfig;\n const newMessages = sliceMessagesUntil(chatHelpers.messages, parentId);\n chatHelpers.setMessages(newMessages);\n\n await chatHelpers.regenerate({ metadata: config.runConfig });\n },\n onAddToolResult: ({\n toolCallId,\n toolName,\n result,\n isError,\n modelContent,\n }) => {\n const options = { metadata: lastRunConfigRef.current };\n if (isError) {\n chatHelpers.addToolOutput({\n state: \"output-error\",\n tool: toolName ?? toolCallId,\n toolCallId,\n errorText:\n typeof result === \"string\" ? result : JSON.stringify(result),\n options,\n });\n } else {\n const output =\n modelContent !== undefined\n ? wrapModelContentEnvelope(result, modelContent)\n : result;\n chatHelpers.addToolResult({\n tool: toolName,\n toolCallId,\n output,\n options,\n });\n }\n },\n onRespondToToolApproval: ({ approvalId, approved, reason }) => {\n void chatHelpers.addToolApprovalResponse({\n id: approvalId,\n approved,\n ...(reason != null && { reason }),\n options: { metadata: lastRunConfigRef.current },\n });\n },\n ...(onResume && { onResume }),\n ...(suggestions && { suggestions }),\n adapters: {\n attachments: vercelAttachmentAdapter,\n ...contextAdapters,\n ...adapters,\n },\n isLoading,\n });\n\n return runtime;\n};\n"],"mappings":";;;;;;;;;;;;;;;AA+CA,MAAM,eACJ,eACA,kBAEC;CACC,GA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AC;QACI;IACL,MAAM,SACJ,iBAAiB,KAAA,IACb,yBAAyB,QAAQ,YAAY,IAC7C;IACN,YAAY,cAAc;KACxB,MAAM;KACN;KACA;KACA;IACF,CAAC;GACH;EACF;EACA,0BAA0B,EAAE,YAAY,UAAU,aAAa;GAC7D,YAAiB,wBAAwB;IACvC,IAAI;IACJ;IACA,GAAI,UAAU,QAAQ,EAAE,OAAO;IAC/B,SAAS,EAAE,UAAU,iBAAiB,QAAQ;GAChD,CAAC;EACH;EACA,GAAI,YAAY,EAAE,SAAS;EAC3B,GAAI,eAAe,EAAE,YAAY;EACjC,UAAU;GACR,aAAa;GACb,GAAG;GACH,GAAG;EACL;EACA;CACF,CAAC;CAED,OAAO;AACT"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/react-ai-sdk",
3
- "version": "1.3.28",
3
+ "version": "1.3.30",
4
4
  "description": "Vercel AI SDK adapter for assistant-ui",
5
5
  "keywords": [
6
6
  "ai-sdk",
@@ -30,10 +30,10 @@
30
30
  ],
31
31
  "sideEffects": false,
32
32
  "dependencies": {
33
- "@ai-sdk/react": "^3.0.190",
34
- "@assistant-ui/core": "^0.2.6",
33
+ "@ai-sdk/react": "^3.0.193",
34
+ "@assistant-ui/core": "^0.2.7",
35
35
  "@assistant-ui/store": "^0.2.12",
36
- "ai": "^6.0.188",
36
+ "ai": "^6.0.191",
37
37
  "assistant-cloud": "*"
38
38
  },
39
39
  "peerDependencies": {
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="@assistant-ui/core/store" />
2
1
  /// <reference types="@assistant-ui/core/react" />
3
2
 
4
3
  export { useAISDKRuntime } from "./ui/use-chat/useAISDKRuntime";
@@ -1,14 +1,13 @@
1
1
  "use client";
2
2
 
3
- import { useState, useMemo, useRef } from "react";
3
+ import { useMemo, useRef, useState } from "react";
4
4
  import type { UIMessage, useChat, CreateUIMessage } from "@ai-sdk/react";
5
5
  import { isToolUIPart, generateId } from "ai";
6
6
  import {
7
7
  useExternalStoreRuntime,
8
8
  useRuntimeAdapters,
9
- useToolInvocations,
10
- type ToolExecutionStatus,
11
9
  } from "@assistant-ui/core/react";
10
+ import type { ToolExecutionStatus } from "@assistant-ui/core";
12
11
  import type {
13
12
  ExternalStoreAdapter,
14
13
  ThreadHistoryAdapter,
@@ -144,29 +143,6 @@ export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
144
143
  },
145
144
  }));
146
145
 
147
- const toolInvocations = useToolInvocations({
148
- state: {
149
- messages,
150
- isRunning,
151
- },
152
- getTools: () => runtimeRef.current.thread.getModelContext().tools,
153
- onResult: (command) => {
154
- if (command.type === "add-tool-result") {
155
- const output =
156
- command.modelContent !== undefined
157
- ? wrapModelContentEnvelope(command.result, command.modelContent)
158
- : command.result;
159
- chatHelpers.addToolResult({
160
- tool: command.toolName,
161
- toolCallId: command.toolCallId,
162
- output,
163
- options: { metadata: lastRunConfigRef.current },
164
- });
165
- }
166
- },
167
- setToolStatuses,
168
- });
169
-
170
146
  const isLoading = useExternalHistory(
171
147
  runtimeRef,
172
148
  adapters?.history ?? contextAdapters?.history,
@@ -185,7 +161,9 @@ export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
185
161
  const completePendingToolCalls = async () => {
186
162
  if (!cancelPendingToolCallsOnSend) return;
187
163
 
188
- await toolInvocations.abort();
164
+ // The runtime auto-aborts in-flight tool invocations when a new run
165
+ // is dispatched (append() / startRun()). All we need to do here is
166
+ // mark any tool without a result as cancelled in the UI message list.
189
167
 
190
168
  // Mark any tool without a result as cancelled (uses setMessages to avoid triggering sendAutomaticallyWhen)
191
169
  chatHelpers.setMessages((messages) => {
@@ -214,6 +192,8 @@ export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
214
192
  const runtime = useExternalStoreRuntime({
215
193
  isRunning,
216
194
  messages,
195
+ unstable_enableToolInvocations: true,
196
+ setToolStatuses,
217
197
  setMessages: (messages) =>
218
198
  chatHelpers.setMessages(
219
199
  messages
@@ -278,7 +258,6 @@ export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
278
258
  },
279
259
  onCancel: async () => {
280
260
  chatHelpers.stop();
281
- await toolInvocations.abort();
282
261
  },
283
262
  onNew: async (message) => {
284
263
  const createMessage = (
@@ -327,29 +306,36 @@ export const useAISDKRuntime = <UI_MESSAGE extends UIMessage = UIMessage>(
327
306
 
328
307
  await chatHelpers.regenerate({ metadata: config.runConfig });
329
308
  },
330
- onAddToolResult: ({ toolCallId, result, isError }) => {
309
+ onAddToolResult: ({
310
+ toolCallId,
311
+ toolName,
312
+ result,
313
+ isError,
314
+ modelContent,
315
+ }) => {
331
316
  const options = { metadata: lastRunConfigRef.current };
332
317
  if (isError) {
333
318
  chatHelpers.addToolOutput({
334
319
  state: "output-error",
335
- tool: toolCallId,
320
+ tool: toolName ?? toolCallId,
336
321
  toolCallId,
337
322
  errorText:
338
323
  typeof result === "string" ? result : JSON.stringify(result),
339
324
  options,
340
325
  });
341
326
  } else {
342
- chatHelpers.addToolOutput({
343
- state: "output-available",
344
- tool: toolCallId,
327
+ const output =
328
+ modelContent !== undefined
329
+ ? wrapModelContentEnvelope(result, modelContent)
330
+ : result;
331
+ chatHelpers.addToolResult({
332
+ tool: toolName,
345
333
  toolCallId,
346
- output: result,
334
+ output,
347
335
  options,
348
336
  });
349
337
  }
350
338
  },
351
- onResumeToolCall: (options) =>
352
- toolInvocations.resume(options.toolCallId, options.payload),
353
339
  onRespondToToolApproval: ({ approvalId, approved, reason }) => {
354
340
  void chatHelpers.addToolApprovalResponse({
355
341
  id: approvalId,
@@ -1,6 +1,9 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import type { ReadonlyJSONObject } from "assistant-stream/utils";
3
- import { AISDKMessageConverter } from "./convertMessage";
3
+ import {
4
+ AISDKMessageConverter,
5
+ type AISDKMessageConverterMetadata,
6
+ } from "./convertMessage";
4
7
 
5
8
  describe("AISDKMessageConverter", () => {
6
9
  it("converts user files into attachments and keeps text content", () => {
@@ -338,7 +341,7 @@ describe("AISDKMessageConverter", () => {
338
341
  });
339
342
 
340
343
  it("keeps observed key order from streaming snapshots for final tool args", () => {
341
- const metadata = {
344
+ const metadata: AISDKMessageConverterMetadata = {
342
345
  toolArgsKeyOrderCache: new Map<string, Map<string, string[]>>(),
343
346
  };
344
347
 
@@ -410,7 +413,7 @@ describe("AISDKMessageConverter", () => {
410
413
  });
411
414
 
412
415
  it("merges duplicate toolCallId across assistant snapshots", () => {
413
- const metadata = {
416
+ const metadata: AISDKMessageConverterMetadata = {
414
417
  toolArgsKeyOrderCache: new Map<string, Map<string, string[]>>(),
415
418
  };
416
419
 
@@ -463,7 +466,7 @@ describe("AISDKMessageConverter", () => {
463
466
  });
464
467
 
465
468
  it("preserves last good input when AI SDK briefly emits null input", () => {
466
- const metadata = {
469
+ const metadata: AISDKMessageConverterMetadata = {
467
470
  toolArgsKeyOrderCache: new Map<string, Map<string, string[]>>(),
468
471
  toolLastInputCache: new Map<string, ReadonlyJSONObject>(),
469
472
  };
@@ -506,7 +509,7 @@ describe("AISDKMessageConverter", () => {
506
509
  });
507
510
 
508
511
  it("preserves last good input across terminal state transitions", () => {
509
- const metadata = {
512
+ const metadata: AISDKMessageConverterMetadata = {
510
513
  toolArgsKeyOrderCache: new Map<string, Map<string, string[]>>(),
511
514
  toolLastInputCache: new Map<string, ReadonlyJSONObject>(),
512
515
  };
@@ -690,7 +693,7 @@ describe("AISDKMessageConverter", () => {
690
693
  });
691
694
 
692
695
  it("memoizes MCP app metadata across conversions by resourceUri", () => {
693
- const metadata = {
696
+ const metadata: AISDKMessageConverterMetadata = {
694
697
  mcpAppMetadataCache: new Map(),
695
698
  };
696
699