@assistant-ui/core 0.3.16 → 0.3.17

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.
Files changed (305) hide show
  1. package/dist/index.d.ts +2 -2
  2. package/dist/index.js +2 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/internal.d.ts +2 -1
  5. package/dist/internal.js +2 -1
  6. package/dist/model-context/frame/provider.d.ts +3 -0
  7. package/dist/model-context/frame/provider.d.ts.map +1 -1
  8. package/dist/model-context/frame/provider.js +71 -21
  9. package/dist/model-context/frame/provider.js.map +1 -1
  10. package/dist/model-context/tool-call-text.d.ts +26 -0
  11. package/dist/model-context/tool-call-text.d.ts.map +1 -0
  12. package/dist/model-context/tool-call-text.js +18 -0
  13. package/dist/model-context/tool-call-text.js.map +1 -0
  14. package/dist/react/RuntimeAdapter.js +1 -1
  15. package/dist/react/adapters/LocalStorageThreadListAdapter.d.ts.map +1 -1
  16. package/dist/react/adapters/LocalStorageThreadListAdapter.js +1 -1
  17. package/dist/react/adapters/LocalStorageThreadListAdapter.js.map +1 -1
  18. package/dist/react/client/AdaptedRemoteThread.js +1 -1
  19. package/dist/react/client/InMemoryThreadList.js +1 -1
  20. package/dist/react/client/Interactables.js +1 -1
  21. package/dist/react/client/RemoteThreadList.d.ts +15 -0
  22. package/dist/react/client/RemoteThreadList.d.ts.map +1 -1
  23. package/dist/react/client/RemoteThreadList.js +240 -138
  24. package/dist/react/client/RemoteThreadList.js.map +1 -1
  25. package/dist/react/client/Tools.d.ts.map +1 -1
  26. package/dist/react/client/Tools.js +6 -2
  27. package/dist/react/client/Tools.js.map +1 -1
  28. package/dist/react/index.d.ts +2 -1
  29. package/dist/react/index.js +2 -1
  30. package/dist/react/interactables-legacy/Interactables.js +4 -4
  31. package/dist/react/interactables-legacy/Interactables.js.map +1 -1
  32. package/dist/react/model-context/toolbox.d.ts +2 -14
  33. package/dist/react/model-context/toolbox.d.ts.map +1 -1
  34. package/dist/react/model-context/toolbox.js +1 -13
  35. package/dist/react/model-context/toolbox.js.map +1 -1
  36. package/dist/react/model-context/useAssistantContext.js +37 -7
  37. package/dist/react/model-context/useAssistantContext.js.map +1 -1
  38. package/dist/react/model-context/useAuiToolOverrides.js +31 -5
  39. package/dist/react/model-context/useAuiToolOverrides.js.map +1 -1
  40. package/dist/react/model-context/useInlineRender.d.ts.map +1 -1
  41. package/dist/react/model-context/useInlineRender.js +10 -36
  42. package/dist/react/model-context/useInlineRender.js.map +1 -1
  43. package/dist/react/primitive-hooks/useMessageError.d.ts.map +1 -1
  44. package/dist/react/primitive-hooks/useMessageError.js +2 -8
  45. package/dist/react/primitive-hooks/useMessageError.js.map +1 -1
  46. package/dist/react/primitive-hooks/useThreadListLoadMore.d.ts.map +1 -1
  47. package/dist/react/primitive-hooks/useThreadListLoadMore.js +2 -4
  48. package/dist/react/primitive-hooks/useThreadListLoadMore.js.map +1 -1
  49. package/dist/react/primitives/composer/ComposerAttachments.d.ts.map +1 -1
  50. package/dist/react/primitives/composer/ComposerAttachments.js +4 -3
  51. package/dist/react/primitives/composer/ComposerAttachments.js.map +1 -1
  52. package/dist/react/primitives/composer/ComposerQueue.d.ts.map +1 -1
  53. package/dist/react/primitives/composer/ComposerQueue.js +4 -3
  54. package/dist/react/primitives/composer/ComposerQueue.js.map +1 -1
  55. package/dist/react/primitives/message/MessageAttachments.d.ts.map +1 -1
  56. package/dist/react/primitives/message/MessageAttachments.js +7 -6
  57. package/dist/react/primitives/message/MessageAttachments.js.map +1 -1
  58. package/dist/react/primitives/message/MessageGroupedParts.js +2 -2
  59. package/dist/react/primitives/message/MessageGroupedParts.js.map +1 -1
  60. package/dist/react/primitives/message/MessageParts.js +3 -3
  61. package/dist/react/primitives/message/MessageParts.js.map +1 -1
  62. package/dist/react/primitives/threadList/ThreadListItems.d.ts.map +1 -1
  63. package/dist/react/primitives/threadList/ThreadListItems.js +5 -4
  64. package/dist/react/primitives/threadList/ThreadListItems.js.map +1 -1
  65. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +6 -4
  66. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
  67. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js +44 -22
  68. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js.map +1 -1
  69. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +6 -5
  70. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
  71. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +79 -62
  72. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -1
  73. package/dist/react/runtimes/RemoteThreadResource.d.ts +1 -6
  74. package/dist/react/runtimes/RemoteThreadResource.d.ts.map +1 -1
  75. package/dist/react/runtimes/RemoteThreadResource.js +3 -3
  76. package/dist/react/runtimes/RemoteThreadResource.js.map +1 -1
  77. package/dist/react/runtimes/assistant-transport/useLatestRef.js +2 -2
  78. package/dist/react/runtimes/assistant-transport/useLatestRef.js.map +1 -1
  79. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -1
  80. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js +23 -18
  81. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -1
  82. package/dist/react/runtimes/cloud/auiV0.d.ts +5 -1
  83. package/dist/react/runtimes/cloud/auiV0.d.ts.map +1 -1
  84. package/dist/react/runtimes/cloud/auiV0.js.map +1 -1
  85. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.d.ts +29 -0
  86. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.d.ts.map +1 -0
  87. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.js +159 -0
  88. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.js.map +1 -0
  89. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts +1 -9
  90. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts.map +1 -1
  91. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js +16 -153
  92. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js.map +1 -1
  93. package/dist/react/runtimes/external-message-converter.js +1 -1
  94. package/dist/react/runtimes/external-message-converter.js.map +1 -1
  95. package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts.map +1 -1
  96. package/dist/react/runtimes/useRemoteThreadListRuntime.js +14 -6
  97. package/dist/react/runtimes/useRemoteThreadListRuntime.js.map +1 -1
  98. package/dist/react/types/MessagePartComponentTypes.d.ts +7 -3
  99. package/dist/react/types/MessagePartComponentTypes.d.ts.map +1 -1
  100. package/dist/react/types/scopes/tools.d.ts +2 -0
  101. package/dist/react/types/scopes/tools.d.ts.map +1 -1
  102. package/dist/runtime/api/message-part-runtime.d.ts +2 -2
  103. package/dist/runtime/api/message-part-runtime.d.ts.map +1 -1
  104. package/dist/runtime/api/message-part-runtime.js +1 -1
  105. package/dist/runtime/api/message-part-runtime.js.map +1 -1
  106. package/dist/runtime/api/thread-list-runtime.d.ts +9 -1
  107. package/dist/runtime/api/thread-list-runtime.d.ts.map +1 -1
  108. package/dist/runtime/api/thread-list-runtime.js +5 -0
  109. package/dist/runtime/api/thread-list-runtime.js.map +1 -1
  110. package/dist/runtime/api/thread-runtime.d.ts +1 -1
  111. package/dist/runtime/base/base-thread-runtime-core.d.ts +2 -2
  112. package/dist/runtime/base/base-thread-runtime-core.d.ts.map +1 -1
  113. package/dist/runtime/base/base-thread-runtime-core.js +2 -2
  114. package/dist/runtime/base/base-thread-runtime-core.js.map +1 -1
  115. package/dist/runtime/interfaces/thread-list-runtime-core.d.ts +14 -2
  116. package/dist/runtime/interfaces/thread-list-runtime-core.d.ts.map +1 -1
  117. package/dist/runtime/interfaces/thread-runtime-core.d.ts +15 -1
  118. package/dist/runtime/interfaces/thread-runtime-core.d.ts.map +1 -1
  119. package/dist/runtime/utils/auto-status.d.ts +1 -1
  120. package/dist/runtime/utils/auto-status.d.ts.map +1 -1
  121. package/dist/runtime/utils/auto-status.js +6 -2
  122. package/dist/runtime/utils/auto-status.js.map +1 -1
  123. package/dist/runtime/utils/external-message-conversion.d.ts +3 -2
  124. package/dist/runtime/utils/external-message-conversion.d.ts.map +1 -1
  125. package/dist/runtime/utils/external-message-conversion.js +4 -3
  126. package/dist/runtime/utils/external-message-conversion.js.map +1 -1
  127. package/dist/runtime/utils/resolveToolApprovalResponse.d.ts +6 -3
  128. package/dist/runtime/utils/resolveToolApprovalResponse.d.ts.map +1 -1
  129. package/dist/runtime/utils/resolveToolApprovalResponse.js +12 -3
  130. package/dist/runtime/utils/resolveToolApprovalResponse.js.map +1 -1
  131. package/dist/runtime/utils/thread-message-like.d.ts +2 -10
  132. package/dist/runtime/utils/thread-message-like.d.ts.map +1 -1
  133. package/dist/runtime/utils/thread-message-like.js.map +1 -1
  134. package/dist/runtime/utils/tool-call-tree.d.ts +39 -0
  135. package/dist/runtime/utils/tool-call-tree.d.ts.map +1 -0
  136. package/dist/runtime/utils/tool-call-tree.js +85 -0
  137. package/dist/runtime/utils/tool-call-tree.js.map +1 -0
  138. package/dist/runtimes/external-store/external-store-adapter.d.ts +29 -2
  139. package/dist/runtimes/external-store/external-store-adapter.d.ts.map +1 -1
  140. package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts +2 -1
  141. package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts.map +1 -1
  142. package/dist/runtimes/external-store/external-store-thread-runtime-core.js +34 -28
  143. package/dist/runtimes/external-store/external-store-thread-runtime-core.js.map +1 -1
  144. package/dist/runtimes/inert/InertThreadRuntimeCore.d.ts +1 -1
  145. package/dist/runtimes/inert/InertThreadRuntimeCore.d.ts.map +1 -1
  146. package/dist/runtimes/inert/InertThreadRuntimeCore.js.map +1 -1
  147. package/dist/runtimes/local/local-thread-runtime-core.d.ts +1 -1
  148. package/dist/runtimes/local/local-thread-runtime-core.d.ts.map +1 -1
  149. package/dist/runtimes/local/local-thread-runtime-core.js +3 -1
  150. package/dist/runtimes/local/local-thread-runtime-core.js.map +1 -1
  151. package/dist/runtimes/remote-thread-list/remote-thread-state.d.ts +1 -1
  152. package/dist/runtimes/remote-thread-list/remote-thread-state.d.ts.map +1 -1
  153. package/dist/runtimes/remote-thread-list/remote-thread-state.js +30 -13
  154. package/dist/runtimes/remote-thread-list/remote-thread-state.js.map +1 -1
  155. package/dist/runtimes/remote-thread-list/title.d.ts +9 -0
  156. package/dist/runtimes/remote-thread-list/title.d.ts.map +1 -0
  157. package/dist/runtimes/remote-thread-list/title.js +6 -0
  158. package/dist/runtimes/remote-thread-list/title.js.map +1 -0
  159. package/dist/runtimes/remote-thread-list/types.d.ts +6 -5
  160. package/dist/runtimes/remote-thread-list/types.d.ts.map +1 -1
  161. package/dist/runtimes/tool-invocations/ToolInvocationTracker.d.ts +31 -8
  162. package/dist/runtimes/tool-invocations/ToolInvocationTracker.d.ts.map +1 -1
  163. package/dist/runtimes/tool-invocations/ToolInvocationTracker.js +97 -60
  164. package/dist/runtimes/tool-invocations/ToolInvocationTracker.js.map +1 -1
  165. package/dist/store/clients/external-thread.d.ts +1 -1
  166. package/dist/store/clients/external-thread.d.ts.map +1 -1
  167. package/dist/store/clients/external-thread.js +11 -4
  168. package/dist/store/clients/external-thread.js.map +1 -1
  169. package/dist/store/clients/single-thread-list.js +1 -1
  170. package/dist/store/clients/suggestions.d.ts.map +1 -1
  171. package/dist/store/clients/suggestions.js +18 -6
  172. package/dist/store/clients/suggestions.js.map +1 -1
  173. package/dist/store/clients/thread-message-client.js +1 -1
  174. package/dist/store/clients/thread-selection-events.js +1 -1
  175. package/dist/store/index.d.ts +2 -1
  176. package/dist/store/index.js +2 -1
  177. package/dist/store/internal.d.ts +2 -2
  178. package/dist/store/internal.js +2 -2
  179. package/dist/store/primitive-predicates.d.ts +4 -1
  180. package/dist/store/primitive-predicates.d.ts.map +1 -1
  181. package/dist/store/primitive-predicates.js +9 -1
  182. package/dist/store/primitive-predicates.js.map +1 -1
  183. package/dist/store/runtime-clients/composer-runtime-client.js +1 -1
  184. package/dist/store/runtime-clients/message-runtime-client.js +1 -1
  185. package/dist/store/runtime-clients/thread-list-item-runtime-client.js +1 -1
  186. package/dist/store/runtime-clients/thread-list-runtime-client.d.ts.map +1 -1
  187. package/dist/store/runtime-clients/thread-list-runtime-client.js +124 -105
  188. package/dist/store/runtime-clients/thread-list-runtime-client.js.map +1 -1
  189. package/dist/store/runtime-clients/thread-runtime-client.js +1 -1
  190. package/dist/store/runtime-clients/useSubscribable.d.ts +3 -1
  191. package/dist/store/runtime-clients/useSubscribable.d.ts.map +1 -1
  192. package/dist/store/runtime-clients/useSubscribable.js +1 -1
  193. package/dist/store/runtime-clients/useSubscribable.js.map +1 -1
  194. package/dist/store/scopes/part.d.ts +6 -3
  195. package/dist/store/scopes/part.d.ts.map +1 -1
  196. package/dist/subscribable/subscribable.d.ts +8 -1
  197. package/dist/subscribable/subscribable.d.ts.map +1 -1
  198. package/dist/subscribable/subscribable.js +24 -16
  199. package/dist/subscribable/subscribable.js.map +1 -1
  200. package/dist/types/message.d.ts +40 -1
  201. package/dist/types/message.d.ts.map +1 -1
  202. package/dist/types/message.js +7 -1
  203. package/dist/types/message.js.map +1 -1
  204. package/package.json +12 -17
  205. package/src/index.ts +6 -1
  206. package/src/internal.ts +8 -0
  207. package/src/model-context/frame/provider.test.ts +217 -1
  208. package/src/model-context/frame/provider.ts +111 -23
  209. package/src/model-context/tool-call-text.test.ts +67 -0
  210. package/src/model-context/tool-call-text.ts +67 -0
  211. package/src/react/AssistantRuntimeProvider.test.tsx +66 -2
  212. package/src/react/adapters/LocalStorageThreadListAdapter.tsx +1 -3
  213. package/src/react/client/RemoteThreadList.background.test.ts +384 -0
  214. package/src/react/client/RemoteThreadList.concurrent.test.tsx +274 -0
  215. package/src/react/client/RemoteThreadList.test.ts +273 -1
  216. package/src/react/client/RemoteThreadList.ts +293 -83
  217. package/src/react/client/Tools.ts +7 -1
  218. package/src/react/index.ts +4 -0
  219. package/src/react/model-context/modelContextHooks.test.tsx +131 -0
  220. package/src/react/model-context/toolbox.test.ts +9 -9
  221. package/src/react/model-context/toolbox.ts +9 -36
  222. package/src/react/model-context/useAssistantContext.ts +4 -2
  223. package/src/react/model-context/useAuiToolOverrides.ts +4 -2
  224. package/src/react/model-context/useInlineRender.test.tsx +48 -0
  225. package/src/react/model-context/useInlineRender.ts +14 -10
  226. package/src/react/primitive-hooks/useMessageError.ts +2 -19
  227. package/src/react/primitive-hooks/useThreadListLoadMore.ts +2 -3
  228. package/src/react/primitives/composer/ComposerAttachments.test.tsx +76 -0
  229. package/src/react/primitives/composer/ComposerAttachments.tsx +9 -4
  230. package/src/react/primitives/composer/ComposerQueue.test.tsx +72 -0
  231. package/src/react/primitives/composer/ComposerQueue.tsx +7 -4
  232. package/src/react/primitives/message/MessageAttachments.test.tsx +107 -37
  233. package/src/react/primitives/message/MessageAttachments.tsx +10 -7
  234. package/src/react/primitives/message/MessageGroupedParts.tsx +2 -2
  235. package/src/react/primitives/message/MessageParts.tsx +3 -3
  236. package/src/react/primitives/threadList/ThreadListItems.test.tsx +99 -0
  237. package/src/react/primitives/threadList/ThreadListItems.tsx +8 -5
  238. package/src/react/runtimes/RemoteThreadListHookInstanceManager.running.test.tsx +94 -1
  239. package/src/react/runtimes/RemoteThreadListHookInstanceManager.tsx +72 -31
  240. package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.switch-delete.test.ts +169 -0
  241. package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +114 -68
  242. package/src/react/runtimes/RemoteThreadResource.ts +1 -4
  243. package/src/react/runtimes/assistant-transport/transport-scheduling.test.ts +51 -2
  244. package/src/react/runtimes/assistant-transport/useLatestRef.ts +2 -2
  245. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.test.ts +45 -0
  246. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.test.tsx +65 -1
  247. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.ts +42 -35
  248. package/src/react/runtimes/cloud/auiV0.ts +5 -0
  249. package/src/react/runtimes/cloud/createCloudThreadListAdapter.test.ts +88 -0
  250. package/src/react/runtimes/cloud/createCloudThreadListAdapter.ts +236 -0
  251. package/src/react/runtimes/cloud/useCloudThreadListAdapter.test.tsx +90 -1
  252. package/src/react/runtimes/cloud/useCloudThreadListAdapter.tsx +33 -238
  253. package/src/react/runtimes/external-message-converter.ts +1 -0
  254. package/src/react/runtimes/useRemoteThreadListRuntime.concurrent.test.tsx +216 -0
  255. package/src/react/runtimes/useRemoteThreadListRuntime.ts +24 -6
  256. package/src/react/types/MessagePartComponentTypes.ts +7 -3
  257. package/src/react/types/scopes/tools.ts +2 -0
  258. package/src/runtime/api/message-part-runtime.ts +3 -3
  259. package/src/runtime/api/thread-list-runtime.ts +25 -1
  260. package/src/runtime/base/base-thread-runtime-core.test.ts +94 -3
  261. package/src/runtime/base/base-thread-runtime-core.ts +10 -4
  262. package/src/runtime/interfaces/thread-list-runtime-core.ts +20 -1
  263. package/src/runtime/interfaces/thread-runtime-core.ts +17 -1
  264. package/src/runtime/utils/auto-status.test.ts +57 -0
  265. package/src/runtime/utils/auto-status.ts +13 -1
  266. package/src/runtime/utils/external-message-conversion.test.ts +97 -0
  267. package/src/runtime/utils/external-message-conversion.ts +17 -1
  268. package/src/runtime/utils/resolveToolApprovalResponse.test.ts +73 -0
  269. package/src/runtime/utils/resolveToolApprovalResponse.ts +27 -6
  270. package/src/runtime/utils/thread-message-like.ts +2 -10
  271. package/src/runtime/utils/tool-call-tree.test.ts +191 -0
  272. package/src/runtime/utils/tool-call-tree.ts +97 -0
  273. package/src/runtimes/external-store/external-store-adapter.ts +38 -2
  274. package/src/runtimes/external-store/external-store-thread-runtime-core.ts +56 -33
  275. package/src/runtimes/inert/InertThreadRuntimeCore.ts +1 -1
  276. package/src/runtimes/local/local-thread-runtime-core.test.ts +29 -3
  277. package/src/runtimes/local/local-thread-runtime-core.ts +5 -1
  278. package/src/runtimes/remote-thread-list/remote-thread-state.test.ts +112 -0
  279. package/src/runtimes/remote-thread-list/remote-thread-state.ts +55 -16
  280. package/src/runtimes/remote-thread-list/title.ts +3 -0
  281. package/src/runtimes/remote-thread-list/types.ts +6 -5
  282. package/src/runtimes/tool-invocations/EDGE_CASES.md +110 -5
  283. package/src/runtimes/tool-invocations/ToolInvocationTracker.test.ts +750 -136
  284. package/src/runtimes/tool-invocations/ToolInvocationTracker.ts +144 -91
  285. package/src/store/clients/external-thread.ts +14 -8
  286. package/src/store/clients/suggestions.test.ts +155 -1
  287. package/src/store/clients/suggestions.ts +42 -34
  288. package/src/store/index.ts +4 -0
  289. package/src/store/internal.ts +2 -0
  290. package/src/store/primitive-predicates.ts +35 -0
  291. package/src/store/runtime-clients/thread-list-runtime-client.ts +16 -2
  292. package/src/store/runtime-clients/useSubscribable.ts +8 -2
  293. package/src/store/scopes/part.ts +6 -3
  294. package/src/subscribable/subscribable.test.ts +50 -0
  295. package/src/subscribable/subscribable.ts +33 -21
  296. package/src/tests/RemoteThreadListThreadListRuntimeCore-load-race.test.ts +111 -0
  297. package/src/tests/RemoteThreadListThreadListRuntimeCore-loadMore.test.ts +78 -0
  298. package/src/tests/RemoteThreadListThreadListRuntimeCore-reload.test.ts +52 -0
  299. package/src/tests/external-store-optimistic-id.test.ts +79 -0
  300. package/src/tests/external-store-repository-instance.test.ts +166 -0
  301. package/src/tests/external-store-thread-runtime-core-adapter.test.ts +22 -16
  302. package/src/tests/external-thread-attachments.test.tsx +85 -1
  303. package/src/tests/remote-thread-list-adapter-switch.test.ts +66 -1
  304. package/src/tests/thread-switch-events.test.tsx +397 -2
  305. package/src/types/message.ts +50 -2
@@ -1,4 +1,5 @@
1
1
  import { isJSONValueEqual } from "../../utils/json/is-json-equal.js";
2
+ import { walkToolCallTree } from "../../runtime/utils/tool-call-tree.js";
2
3
  import { ToolResponse, createAssistantStreamController, unstable_toolResultStream } from "assistant-stream";
3
4
  import { AssistantMetaTransformStream } from "assistant-stream/utils";
4
5
  //#region src/runtimes/tool-invocations/ToolInvocationTracker.ts
@@ -50,9 +51,17 @@ const getToolExecutionId = (value) => value[TOOL_EXECUTION_ID];
50
51
  var ToolInvocationTracker = class {
51
52
  _getTools;
52
53
  _callbacks;
54
+ _isClientToolCall;
53
55
  _entries = /* @__PURE__ */ new Map();
54
56
  _humanInput = /* @__PURE__ */ new Map();
55
57
  _executing = /* @__PURE__ */ new Set();
58
+ /**
59
+ * Tool calls whose turn ended before they reached the executor. Held here
60
+ * rather than on the entry because an entry is rebuilt whenever a snapshot
61
+ * re-creates the call, and this is the one reason to skip that no later
62
+ * snapshot carries.
63
+ */
64
+ _discardedToolCallIds = /* @__PURE__ */ new Set();
56
65
  _settledResolvers = [];
57
66
  _statuses = /* @__PURE__ */ new Map();
58
67
  _ac = new AbortController();
@@ -64,16 +73,19 @@ var ToolInvocationTracker = class {
64
73
  /**
65
74
  * Set when the assistant-stream pipeline has died (errored out via
66
75
  * `.pipeTo(...).catch(...)`). The next `setState` re-initializes the
67
- * pipeline and demotes all active entries to restored so they survive
68
- * across the restart without re-firing `streamCall` (preserves the
69
- * "exactly once" contract). Capped at a single auto-restart per session
70
- * — repeated failures keep the tracker dead with a more visible error.
76
+ * pipeline and demotes each active entry that reached the executor to
77
+ * restored, so it survives the restart without re-firing `streamCall`.
78
+ * A restart is an execution boundary like `reset()`: an entry that had
79
+ * not reached it starts over and fires once there (F.4). Capped at a
80
+ * single auto-restart per session — repeated failures keep the tracker
81
+ * dead with a more visible error.
71
82
  */
72
83
  _pipelineDead = false;
73
84
  _pipelineRestartUsed = false;
74
- constructor(getTools, callbacks) {
85
+ constructor(getTools, callbacks, isClientToolCall) {
75
86
  this._getTools = getTools;
76
87
  this._callbacks = callbacks;
88
+ this._isClientToolCall = isClientToolCall;
77
89
  this._initPipeline();
78
90
  }
79
91
  /**
@@ -141,6 +153,7 @@ var ToolInvocationTracker = class {
141
153
  try {
142
154
  this._pendingRestore = true;
143
155
  this._entries.clear();
156
+ this._discardedToolCallIds.clear();
144
157
  this._lastSnapshot = null;
145
158
  this.abort();
146
159
  if (this._statuses.size > 0) {
@@ -154,8 +167,13 @@ var ToolInvocationTracker = class {
154
167
  /**
155
168
  * Abort any in-flight `execute()` invocations. Resolves once all of them
156
169
  * have settled (or immediately if none are running).
170
+ *
171
+ * `discardPending` additionally kills the calls that never reached the
172
+ * executor, for a caller ending the turn rather than interrupting it. The
173
+ * signal cannot reach those: they are waiting on the run to settle (A.10),
174
+ * and the settled snapshot arrives after this installs a fresh controller.
157
175
  */
158
- abort() {
176
+ abort(options) {
159
177
  try {
160
178
  this._humanInput.forEach(({ reject }) => {
161
179
  try {
@@ -163,6 +181,12 @@ var ToolInvocationTracker = class {
163
181
  } catch {}
164
182
  });
165
183
  this._humanInput.clear();
184
+ if (options?.discardPending) for (const [toolCallId, entry] of this._entries) {
185
+ if (!entry.controller) continue;
186
+ if (entry.argsComplete || entry.hasResult) continue;
187
+ this._discardedToolCallIds.add(toolCallId);
188
+ entry.skipExecute = true;
189
+ }
166
190
  this._ac.abort();
167
191
  this._ac = new AbortController();
168
192
  if (this._executing.size === 0) return Promise.resolve();
@@ -265,6 +289,7 @@ var ToolInvocationTracker = class {
265
289
  if (!this._captureExecution(toolCallId, executionId)) return;
266
290
  if (this._entries.get(toolCallId).skipExecute) return;
267
291
  this._executing.add(executionId);
292
+ if (this._humanInput.get(toolCallId)?.executionId === executionId) return;
268
293
  this._setStatus(toolCallId, { type: "executing" });
269
294
  }
270
295
  _onExecutionEnd(toolCallId, executionId) {
@@ -330,16 +355,26 @@ var ToolInvocationTracker = class {
330
355
  this._statuses = next;
331
356
  this._invokeOnStatusesChange();
332
357
  }
333
- _hasExecutableTool(toolName) {
334
- const tool = this._getTools()?.[toolName];
335
- return tool?.execute !== void 0 || tool?.streamCall !== void 0;
358
+ _warnProviderOwnedSkip(toolName, toolCallId) {
359
+ if (process.env.NODE_ENV === "production") return;
360
+ if (this._getTools()?.[toolName]?.execute === void 0) return;
361
+ console.warn("[ToolInvocationTracker] the runtime reports this tool call as provider-owned, so the registered execute is skipped; the provider has to hand the call to the client for it to run (see EDGE_CASES.md A.9)", {
362
+ toolCallId,
363
+ toolName
364
+ });
336
365
  }
337
- _shouldCloseArgsStream({ toolName, argsText, hasResult }) {
366
+ /**
367
+ * Closing the args stream hands the call to the client executor, so it may
368
+ * only happen once the provider can no longer speak about that call. The run
369
+ * ending is the only universal signal for that; an adapter that reports the
370
+ * call as client-owned has said it earlier, per call.
371
+ */
372
+ _shouldCloseArgsStream({ argsText, hasResult, clientOwned }) {
338
373
  if (hasResult) return true;
339
- if (!this._hasExecutableTool(toolName)) return !this._isRunning && isArgsTextComplete(argsText);
340
- return isArgsTextComplete(argsText);
374
+ if (!isArgsTextComplete(argsText)) return false;
375
+ return clientOwned || !this._isRunning;
341
376
  }
342
- _startActiveEntry(toolCallId, toolName, skipExecute) {
377
+ _startActiveEntry(toolCallId, toolName, skipExecute, clientOwned) {
343
378
  const entry = {
344
379
  toolName,
345
380
  controller: this._controller.addToolCallPart({
@@ -349,7 +384,8 @@ var ToolInvocationTracker = class {
349
384
  argsText: "",
350
385
  hasResult: false,
351
386
  skipExecute,
352
- argsComplete: false
387
+ argsComplete: false,
388
+ clientOwned
353
389
  };
354
390
  this._entries.set(toolCallId, entry);
355
391
  return entry;
@@ -364,6 +400,10 @@ var ToolInvocationTracker = class {
364
400
  _demoteEntriesToRestored() {
365
401
  for (const [toolCallId, entry] of this._entries) {
366
402
  if (!entry.controller) continue;
403
+ if (!entry.argsComplete && !entry.hasResult) {
404
+ this._entries.delete(toolCallId);
405
+ continue;
406
+ }
367
407
  this._entries.set(toolCallId, {
368
408
  toolName: entry.toolName,
369
409
  argsText: entry.argsText,
@@ -391,9 +431,9 @@ var ToolInvocationTracker = class {
391
431
  } else if (!content.argsText.startsWith(entry.argsText)) {
392
432
  if (isArgsTextComplete(entry.argsText) && isArgsTextComplete(content.argsText) && isEquivalentCompleteArgsText(entry.argsText, content.argsText)) {
393
433
  const shouldClose = this._shouldCloseArgsStream({
394
- toolName: content.toolName,
395
434
  argsText: content.argsText,
396
- hasResult
435
+ hasResult,
436
+ clientOwned: entry.clientOwned
397
437
  });
398
438
  if (shouldClose) entry.controller.argsText.close();
399
439
  entry.argsText = content.argsText;
@@ -412,9 +452,9 @@ var ToolInvocationTracker = class {
412
452
  const delta = content.argsText.slice(entry.argsText.length);
413
453
  entry.controller.argsText.append(delta);
414
454
  const shouldClose = this._shouldCloseArgsStream({
415
- toolName: content.toolName,
416
455
  argsText: content.argsText,
417
- hasResult
456
+ hasResult,
457
+ clientOwned: entry.clientOwned
418
458
  });
419
459
  if (shouldClose) entry.controller.argsText.close();
420
460
  entry.argsText = content.argsText;
@@ -423,9 +463,9 @@ var ToolInvocationTracker = class {
423
463
  }
424
464
  if (!entry.argsComplete && entry.controller) {
425
465
  if (this._shouldCloseArgsStream({
426
- toolName: content.toolName,
427
466
  argsText: entry.argsText,
428
- hasResult
467
+ hasResult,
468
+ clientOwned: entry.clientOwned
429
469
  })) {
430
470
  entry.controller.argsText.close();
431
471
  entry.argsComplete = true;
@@ -434,46 +474,43 @@ var ToolInvocationTracker = class {
434
474
  }
435
475
  _processMessages(messages) {
436
476
  const isRestore = this._pendingRestore;
437
- for (const message of messages) {
438
- if (!message || !Array.isArray(message.content)) continue;
439
- for (const content of message.content) {
440
- if (!content || content.type !== "tool-call") continue;
441
- const existing = this._entries.get(content.toolCallId);
442
- if (isRestore) {
443
- if (!existing?.controller) this._entries.set(content.toolCallId, {
444
- toolName: content.toolName,
445
- argsText: content.argsText,
446
- hasResult: content.result !== void 0
447
- });
448
- if (content.messages) this._processMessages(content.messages);
449
- continue;
450
- }
451
- let entry = existing;
452
- if (entry && !entry.controller) {
453
- if (!(content.argsText !== entry.argsText || content.result !== void 0 !== entry.hasResult)) {
454
- if (content.messages) this._processMessages(content.messages);
455
- continue;
456
- }
457
- this._entries.delete(content.toolCallId);
458
- entry = void 0;
459
- }
460
- if (!entry) entry = this._startActiveEntry(content.toolCallId, content.toolName, content.result !== void 0);
461
- if (content.approval !== void 0) entry.skipExecute = true;
462
- this._processArgsText(entry, content);
463
- if (content.result !== void 0 && !entry.hasResult) {
464
- const { controller: activeController } = entry;
465
- if (!activeController) continue;
466
- entry.hasResult = true;
467
- entry.argsComplete = true;
468
- activeController.setResponse(new ToolResponse({
469
- result: content.result,
470
- artifact: content.artifact,
471
- isError: content.isError,
472
- ...content.modelContent !== void 0 ? { modelContent: content.modelContent } : {}
473
- }));
474
- activeController.close();
475
- }
476
- if (content.messages) this._processMessages(content.messages);
477
+ for (const { part: content } of walkToolCallTree(messages)) {
478
+ const existing = this._entries.get(content.toolCallId);
479
+ if (isRestore) {
480
+ if (!existing?.controller) this._entries.set(content.toolCallId, {
481
+ toolName: content.toolName,
482
+ argsText: content.argsText,
483
+ hasResult: content.result !== void 0
484
+ });
485
+ continue;
486
+ }
487
+ let entry = existing;
488
+ if (content.result !== void 0) this._discardedToolCallIds.delete(content.toolCallId);
489
+ if (entry && !entry.controller) {
490
+ if (!(content.argsText !== entry.argsText || content.result !== void 0 !== entry.hasResult)) continue;
491
+ this._entries.delete(content.toolCallId);
492
+ entry = void 0;
493
+ }
494
+ if (!entry) {
495
+ const ownership = this._isClientToolCall?.(content);
496
+ const providerOwned = content.result === void 0 && ownership === false;
497
+ if (providerOwned) this._warnProviderOwnedSkip(content.toolName, content.toolCallId);
498
+ entry = this._startActiveEntry(content.toolCallId, content.toolName, content.result !== void 0 || providerOwned || this._discardedToolCallIds.has(content.toolCallId), ownership === true);
499
+ }
500
+ if (content.approval !== void 0) entry.skipExecute = true;
501
+ this._processArgsText(entry, content);
502
+ if (content.result !== void 0 && !entry.hasResult) {
503
+ const { controller: activeController } = entry;
504
+ if (!activeController) continue;
505
+ entry.hasResult = true;
506
+ entry.argsComplete = true;
507
+ activeController.setResponse(new ToolResponse({
508
+ result: content.result,
509
+ artifact: content.artifact,
510
+ isError: content.isError,
511
+ ...content.modelContent !== void 0 ? { modelContent: content.modelContent } : {}
512
+ }));
513
+ activeController.close();
477
514
  }
478
515
  }
479
516
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ToolInvocationTracker.js","names":["process","env","NODE_ENV","createAssistantStreamController","ToolCallStreamController","ToolResponse","unstable_toolResultStream","Tool","ToolModelContentPart","ToolResultStreamOptions","AssistantMetaTransformStream","ReadonlyJSONValue","isJSONValueEqual","ThreadMessage","TOOL_EXECUTION_ID","Symbol","for","ToolExecutionStatus","type","payload","AddToolResultCommand","toolCallId","toolName","result","isError","artifact","modelContent","ToolCallEntry","argsText","hasResult","executionId","skipExecute","controller","argsComplete","SettledResolver","executionIds","ReadonlySet","resolve","isArgsTextComplete","JSON","parse","parseArgsText","undefined","isEquivalentCompleteArgsText","previous","next","previousValue","nextValue","getToolExecutionId","value","Record","PropertyKey","ToolInvocationTracker","_getTools","_callbacks","Callbacks","_entries","Map","_humanInput","reject","reason","_executing","Set","_settledResolvers","_statuses","_ac","AbortController","_pendingRestore","_lastSnapshot","Snapshot","_isRunning","_controller","ReturnType","_pipelineDead","_pipelineRestartUsed","constructor","getTools","callbacks","_initPipeline","stream","human","_onHumanInput","transform","_getWrappedTools","signal","onExecutionStart","id","_name","_onExecutionStart","onExecutionEnd","_onExecutionEnd","pipeThrough","pipeTo","WritableStream","write","chunk","_handleResultChunk","err","console","error","catch","setState","snapshot","_demoteEntriesToRestored","clear","messages","isRunning","isLoading","restoreFromLoading","previousIsRunning","_processMessages","reset","abort","size","_invokeOnStatusesChange","Promise","forEach","Error","push","resume","handlers","get","delete","_setStatus","getStatuses","ReadonlyMap","tools","Object","fromEntries","entries","map","name","tool","execute","streamCall","wrappedTool","args","context","Parameters","entry","_captureExecution","reader","set","add","_deleteStatus","pending","some","has","length","meta","_invokeOnResult","command","onResult","onStatusesChange","status","_hasExecutableTool","_shouldCloseArgsStream","_startActiveEntry","toolCallController","addToolCallPart","_processArgsText","content","shouldWriteArgsText","warn","startsWith","shouldClose","close","delta","slice","append","isRestore","message","Array","isArray","existing","signatureChanged","approval","activeController","setResponse","ExecutionStatus","statuses"],"sources":["../../../src/runtimes/tool-invocations/ToolInvocationTracker.ts"],"sourcesContent":["declare const process: { env: { NODE_ENV?: string } };\n\nimport {\n createAssistantStreamController,\n type ToolCallStreamController,\n ToolResponse,\n unstable_toolResultStream,\n type Tool,\n type ToolModelContentPart,\n type ToolResultStreamOptions,\n} from \"assistant-stream\";\nimport {\n AssistantMetaTransformStream,\n type ReadonlyJSONValue,\n} from \"assistant-stream/utils\";\nimport { isJSONValueEqual } from \"../../utils/json/is-json-equal\";\nimport type { ThreadMessage } from \"../../types/message\";\n\nconst TOOL_EXECUTION_ID = Symbol.for(\"assistant-stream.tool-execution-id\");\n\n/**\n * Streaming execution state for a frontend tool.\n */\nexport type ToolExecutionStatus =\n | { type: \"executing\" }\n | {\n type: \"interrupt\";\n payload: { type: \"human\"; payload: unknown };\n };\n\nexport type AddToolResultCommand = {\n readonly type: \"add-tool-result\";\n readonly toolCallId: string;\n readonly toolName: string;\n readonly result: ReadonlyJSONValue;\n readonly isError: boolean;\n readonly artifact?: ReadonlyJSONValue;\n readonly modelContent?: readonly ToolModelContentPart[];\n};\n\ntype ToolCallEntry = {\n toolName: string;\n argsText: string;\n hasResult: boolean;\n executionId?: symbol;\n skipExecute?: boolean;\n} & (\n | {\n /** Restored phase — observed during a history-load snapshot. */\n controller?: undefined;\n argsComplete?: undefined;\n }\n | {\n /** Active phase — chunks are flowing through `controller`. */\n controller: ToolCallStreamController;\n argsComplete: boolean;\n }\n);\n\ntype SettledResolver = {\n executionIds: ReadonlySet<symbol>;\n resolve: () => void;\n};\n\nconst isArgsTextComplete = (argsText: string) => {\n try {\n JSON.parse(argsText);\n return true;\n } catch {\n return false;\n }\n};\n\nconst parseArgsText = (argsText: string) => {\n try {\n return JSON.parse(argsText);\n } catch {\n return undefined;\n }\n};\n\nconst isEquivalentCompleteArgsText = (previous: string, next: string) => {\n const previousValue = parseArgsText(previous);\n const nextValue = parseArgsText(next);\n if (previousValue === undefined || nextValue === undefined) return false;\n return isJSONValueEqual(previousValue, nextValue);\n};\n\nconst getToolExecutionId = (value: object): symbol | undefined =>\n (value as Record<PropertyKey, unknown>)[TOOL_EXECUTION_ID] as\n | symbol\n | undefined;\n\n/**\n * Plain-class port of the former `useToolInvocations` React hook. Owns the\n * assistant-stream pipeline that drives client-side `streamCall` / `execute`\n * for tool-call parts surfaced by a thread runtime, plus the per-tool-call\n * status map that consumers render against.\n *\n * **Contract**: `streamCall` (and `execute`) fires exactly once per logical\n * `toolCallId`. Args mutations after first completion, result replacement,\n * and result clearing are *not* surfaced through additional `streamCall`\n * invocations — by design — so hosts cannot observe spurious re-fires of\n * side effects. The follow-up `reader.events()` API will expose those\n * post-completion transitions to consumers that opt in.\n *\n * State-transition safety: every public method that observes runtime state\n * (`setState`, `reset`, `abort`, `resume`) wraps its work in try/catch and\n * logs to `console.error` rather than throwing. The tracker is built into\n * the hot message-processing path, so a malformed snapshot must never crash\n * the host runtime. See ./EDGE_CASES.md for the known non-trivial state\n * transitions and what each does today.\n *\n * @deprecated Internal — for framework bindings; not a public API. May change without notice.\n */\nexport class ToolInvocationTracker {\n private readonly _getTools: () => Record<string, Tool> | undefined;\n private readonly _callbacks: ToolInvocationTracker.Callbacks;\n\n private readonly _entries = new Map<string, ToolCallEntry>();\n private readonly _humanInput = new Map<\n string,\n {\n executionId: symbol;\n resolve: (payload: unknown) => void;\n reject: (reason: unknown) => void;\n }\n >();\n private readonly _executing = new Set<symbol>();\n private readonly _settledResolvers: SettledResolver[] = [];\n\n private _statuses = new Map<string, ToolExecutionStatus>();\n\n private _ac: AbortController = new AbortController();\n private _pendingRestore = true;\n\n /** Cached last snapshot, used to skip processing on identical re-renders. */\n private _lastSnapshot: ToolInvocationTracker.Snapshot | null = null;\n private _isRunning = false;\n\n private _controller!: ReturnType<typeof createAssistantStreamController>[1];\n\n /**\n * Set when the assistant-stream pipeline has died (errored out via\n * `.pipeTo(...).catch(...)`). The next `setState` re-initializes the\n * pipeline and demotes all active entries to restored so they survive\n * across the restart without re-firing `streamCall` (preserves the\n * \"exactly once\" contract). Capped at a single auto-restart per session\n * — repeated failures keep the tracker dead with a more visible error.\n */\n private _pipelineDead = false;\n private _pipelineRestartUsed = false;\n\n constructor(\n getTools: () => Record<string, Tool> | undefined,\n callbacks: ToolInvocationTracker.Callbacks,\n ) {\n this._getTools = getTools;\n this._callbacks = callbacks;\n\n this._initPipeline();\n }\n\n /**\n * Build the assistant-stream pipeline. Called once from the constructor\n * and at most once again if `_pipelineDead` is set (see F.4 in\n * EDGE_CASES.md).\n */\n private _initPipeline(): void {\n const [stream, controller] = createAssistantStreamController();\n this._controller = controller;\n\n const human = (\n toolCallId: string,\n payload: unknown,\n executionId?: symbol,\n ) => this._onHumanInput(toolCallId, payload, executionId);\n const transform = unstable_toolResultStream(\n () => this._getWrappedTools(),\n () => this._ac.signal,\n human,\n {\n onExecutionStart: (id: string, _name: string, executionId?: symbol) =>\n this._onExecutionStart(id, executionId),\n onExecutionEnd: (id: string, _name: string, executionId?: symbol) =>\n this._onExecutionEnd(id, executionId),\n } as ToolResultStreamOptions,\n );\n\n stream\n .pipeThrough(transform)\n .pipeThrough(new AssistantMetaTransformStream())\n .pipeTo(\n new WritableStream({\n write: (chunk) => {\n try {\n if (chunk.type !== \"result\") return;\n this._handleResultChunk(chunk);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] result chunk handling failed\",\n err,\n );\n }\n },\n }),\n )\n .catch((err) => {\n console.error(\n \"[ToolInvocationTracker] stream pipeline failed; will attempt single restart on next setState\",\n err,\n );\n this._pipelineDead = true;\n });\n }\n\n // ───────────────────────── public API ─────────────────────────\n\n /**\n * Feed the next observed snapshot into the tracker. Called from the host\n * runtime whenever its message list / running state changes.\n */\n public setState(snapshot: ToolInvocationTracker.Snapshot): void {\n try {\n // Recover from a dead pipeline before processing anything. We demote\n // all active entries to \"restored\" so the rebuilt pipeline does not\n // re-fire `streamCall` for tool calls that already fired pre-death;\n // preserves the \"exactly once per toolCallId\" contract.\n if (this._pipelineDead) {\n if (this._pipelineRestartUsed) {\n // Already retried once and failed again. Stay dead.\n return;\n }\n this._pipelineRestartUsed = true;\n this._pipelineDead = false;\n this._demoteEntriesToRestored();\n this._executing.clear();\n this._ac = new AbortController();\n this._initPipeline();\n // Fall through and process the snapshot against the fresh pipeline.\n }\n\n // Identical snapshot — skip processing entirely. Note: external-store\n // runtimes rebuild the messages array on every adapter update, so this\n // fast-path rarely triggers there; it's primarily for the React-hook\n // shim where state references are stable.\n if (\n this._lastSnapshot &&\n this._lastSnapshot.messages === snapshot.messages &&\n this._lastSnapshot.isRunning === snapshot.isRunning &&\n this._lastSnapshot.isLoading === snapshot.isLoading\n ) {\n return;\n }\n\n // While the host is still loading initial state, treat every snapshot\n // as historical: tool calls are recorded so the next live snapshot can\n // diff against them, but `streamCall` / `execute` do not fire.\n const restoreFromLoading = snapshot.isLoading === true;\n if (restoreFromLoading) {\n this._pendingRestore = true;\n }\n\n // E.4 / AF3 — only mark `_lastSnapshot`/`_isRunning` as observed after\n // processing succeeds. If `_processMessages` throws, the next snapshot\n // (even if identical) gets re-processed against the recovered state.\n const previousIsRunning = this._isRunning;\n this._isRunning = snapshot.isRunning;\n try {\n this._processMessages(snapshot.messages);\n } catch (err) {\n this._isRunning = previousIsRunning;\n throw err;\n }\n this._lastSnapshot = snapshot;\n this._pendingRestore = false;\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] setState failed; snapshot dropped\",\n err,\n );\n }\n }\n\n /**\n * Reset the tracker so the next observed snapshot is treated as historical.\n * Clears entries and aborts any in-flight executions. Used by callers like\n * `importExternalState` to mark a freshly loaded state as restored.\n */\n public reset(): void {\n try {\n this._pendingRestore = true;\n this._entries.clear();\n this._lastSnapshot = null;\n void this.abort();\n // Statuses are cleared synchronously: discarded executions may never\n // settle (aborting hands the signal to the tool, it does not force\n // settlement), and a late settlement of one sibling must not republish\n // the others. `_deleteStatus` no-ops for cleared ids, so post-reset\n // settlements stay silent.\n if (this._statuses.size > 0) {\n this._statuses = new Map();\n this._invokeOnStatusesChange();\n }\n } catch (err) {\n console.error(\"[ToolInvocationTracker] reset failed\", err);\n }\n }\n\n /**\n * Abort any in-flight `execute()` invocations. Resolves once all of them\n * have settled (or immediately if none are running).\n */\n public abort(): Promise<void> {\n try {\n this._humanInput.forEach(({ reject }) => {\n try {\n reject(new Error(\"Tool execution aborted\"));\n } catch {\n // host rejection handler threw — already in the abort path,\n // swallow so we continue cleaning up.\n }\n });\n this._humanInput.clear();\n\n this._ac.abort();\n this._ac = new AbortController();\n\n if (this._executing.size === 0) {\n return Promise.resolve();\n }\n const executionIds = new Set(this._executing);\n return new Promise<void>((resolve) => {\n this._settledResolvers.push({ executionIds, resolve });\n });\n } catch (err) {\n console.error(\"[ToolInvocationTracker] abort failed\", err);\n return Promise.resolve();\n }\n }\n\n /**\n * Resolve a pending human-input request for the given tool call. Returns\n * `true` if a pending request was resumed, `false` if the tracker has no\n * outstanding request for that id (the caller should fall back to its own\n * dispatch path).\n */\n public resume(toolCallId: string, payload: unknown): boolean {\n try {\n const handlers = this._humanInput.get(toolCallId);\n if (!handlers) return false;\n this._humanInput.delete(toolCallId);\n this._setStatus(toolCallId, { type: \"executing\" });\n handlers.resolve(payload);\n return true;\n } catch (err) {\n console.error(\"[ToolInvocationTracker] resume failed\", err);\n return false;\n }\n }\n\n /**\n * Returns the current tool execution status map. The returned `Map` is\n * the tracker's internal store — do not mutate it. Treat the reference\n * as a snapshot that may be replaced wholesale on the next status\n * transition.\n */\n public getStatuses(): ReadonlyMap<string, ToolExecutionStatus> {\n return this._statuses;\n }\n\n // ───────────────────── internal: tool wrapping ─────────────────────\n\n private _getWrappedTools(): Record<string, Tool> | undefined {\n const tools = this._getTools();\n if (!tools) return undefined;\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, tool]) => {\n const execute = tool.execute;\n const streamCall = tool.streamCall;\n if (execute === undefined && streamCall === undefined)\n return [name, tool];\n\n const wrappedTool = {\n ...tool,\n ...(execute !== undefined && {\n execute: (...[args, context]: Parameters<typeof execute>) => {\n const executionId = getToolExecutionId(context);\n const entry = this._captureExecution(\n context.toolCallId,\n executionId,\n );\n if (!entry || entry.skipExecute) {\n return new Promise(() => {}) as never;\n }\n return execute(args, context);\n },\n }),\n ...(streamCall !== undefined && {\n streamCall: (\n ...[reader, context]: Parameters<typeof streamCall>\n ) => {\n const executionId = getToolExecutionId(context);\n const entry = this._captureExecution(\n context.toolCallId,\n executionId,\n );\n if (!entry) return;\n return streamCall(reader, context);\n },\n }),\n } as Tool;\n return [name, wrappedTool];\n }),\n ) as Record<string, Tool>;\n }\n\n private _captureExecution(\n toolCallId: string,\n executionId: symbol | undefined,\n ): ToolCallEntry | undefined {\n if (executionId === undefined) return undefined;\n const entry = this._entries.get(toolCallId);\n if (!entry?.controller) return undefined;\n if (entry.executionId === undefined) entry.executionId = executionId;\n return entry.executionId === executionId ? entry : undefined;\n }\n\n // ──────────────── internal: execution lifecycle callbacks ────────────────\n\n private _onHumanInput(\n toolCallId: string,\n payload: unknown,\n executionId?: symbol,\n ): Promise<unknown> {\n return new Promise<unknown>((resolve, reject) => {\n // A discarded execution must not resurrect a status entry or park an\n // unanswerable request after its id has been reused.\n const entry = this._entries.get(toolCallId);\n if (!entry?.controller || entry.executionId !== executionId) {\n reject(new Error(\"Tool execution aborted\"));\n return;\n }\n const previous = this._humanInput.get(toolCallId);\n if (previous) {\n try {\n previous.reject(\n new Error(\"Human input request was superseded by a new request\"),\n );\n } catch {\n // host rejection handler threw; ignore and proceed\n }\n }\n this._humanInput.set(toolCallId, {\n executionId: executionId!,\n resolve,\n reject,\n });\n this._setStatus(toolCallId, {\n type: \"interrupt\",\n payload: { type: \"human\", payload },\n });\n });\n }\n\n private _onExecutionStart(\n toolCallId: string,\n executionId: symbol | undefined,\n ): void {\n if (!this._captureExecution(toolCallId, executionId)) return;\n const entry = this._entries.get(toolCallId)!;\n if (entry.skipExecute) return;\n\n this._executing.add(executionId!);\n this._setStatus(toolCallId, { type: \"executing\" });\n }\n\n private _onExecutionEnd(\n toolCallId: string,\n executionId: symbol | undefined,\n ): void {\n if (executionId === undefined || !this._executing.delete(executionId))\n return;\n\n const entry = this._entries.get(toolCallId);\n if (entry?.executionId === executionId) this._deleteStatus(toolCallId);\n\n const pending: SettledResolver[] = [];\n this._settledResolvers.forEach(({ executionIds, resolve }) => {\n if ([...executionIds].some((id) => this._executing.has(id))) {\n pending.push({ executionIds, resolve });\n return;\n }\n try {\n resolve();\n } catch {\n // ignore — settled-resolver consumer threw\n }\n });\n this._settledResolvers.length = 0;\n this._settledResolvers.push(...pending);\n }\n\n private _handleResultChunk(chunk: {\n type: \"result\";\n result: ReadonlyJSONValue;\n isError: boolean;\n artifact?: ReadonlyJSONValue;\n modelContent?: readonly ToolModelContentPart[];\n meta: { toolCallId: string; toolName: string };\n }): void {\n const toolCallId = chunk.meta.toolCallId;\n const executionId = getToolExecutionId(chunk);\n const entry = this._entries.get(toolCallId);\n\n if (!entry || entry.executionId !== executionId) return;\n\n // The host already set the result (via the live snapshot's\n // `setResponse` path). Suppress the executor's redundant emit.\n if (entry?.hasResult) return;\n if (entry.skipExecute) return;\n\n this._invokeOnResult({\n type: \"add-tool-result\",\n toolCallId,\n toolName: chunk.meta.toolName,\n result: chunk.result,\n isError: chunk.isError,\n ...(chunk.artifact !== undefined && { artifact: chunk.artifact }),\n ...(chunk.modelContent !== undefined && {\n modelContent: chunk.modelContent,\n }),\n });\n }\n\n // ──────────────── internal: callback invocation (AF1/AF2) ────────────────\n\n private _invokeOnResult(command: AddToolResultCommand): void {\n try {\n this._callbacks.onResult(command);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] onResult callback threw; result dropped\",\n err,\n );\n }\n }\n\n private _invokeOnStatusesChange(): void {\n try {\n this._callbacks.onStatusesChange(this._statuses);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] onStatusesChange callback threw; status change not propagated\",\n err,\n );\n }\n }\n\n // ──────────────── internal: status map mutations ────────────────\n\n private _setStatus(toolCallId: string, status: ToolExecutionStatus): void {\n const next = new Map(this._statuses);\n next.set(toolCallId, status);\n this._statuses = next;\n this._invokeOnStatusesChange();\n }\n\n private _deleteStatus(toolCallId: string): void {\n if (!this._statuses.has(toolCallId)) return;\n const next = new Map(this._statuses);\n next.delete(toolCallId);\n this._statuses = next;\n this._invokeOnStatusesChange();\n }\n\n // ──────────────── internal: snapshot processing ────────────────\n\n private _hasExecutableTool(toolName: string): boolean {\n const tool = this._getTools()?.[toolName];\n return tool?.execute !== undefined || tool?.streamCall !== undefined;\n }\n\n private _shouldCloseArgsStream({\n toolName,\n argsText,\n hasResult,\n }: {\n toolName: string;\n argsText: string;\n hasResult: boolean;\n }): boolean {\n if (hasResult) return true;\n if (!this._hasExecutableTool(toolName)) {\n return !this._isRunning && isArgsTextComplete(argsText);\n }\n return isArgsTextComplete(argsText);\n }\n\n private _startActiveEntry(\n toolCallId: string,\n toolName: string,\n skipExecute: boolean,\n ): ToolCallEntry {\n const toolCallController = this._controller.addToolCallPart({\n toolName,\n toolCallId,\n });\n const entry: ToolCallEntry = {\n toolName,\n controller: toolCallController,\n argsText: \"\",\n hasResult: false,\n skipExecute,\n argsComplete: false,\n };\n this._entries.set(toolCallId, entry);\n return entry;\n }\n\n /**\n * Demote every active entry back to the restored phase. Used by the\n * pipeline-restart path so that, after a fresh pipeline is built, the\n * next observed snapshot does not re-fire `streamCall` for tool calls\n * that already fired pre-death. Args / hasResult tracking is preserved\n * so signature comparisons still work.\n */\n private _demoteEntriesToRestored(): void {\n for (const [toolCallId, entry] of this._entries) {\n if (!entry.controller) continue;\n this._entries.set(toolCallId, {\n toolName: entry.toolName,\n argsText: entry.argsText,\n hasResult: entry.hasResult,\n });\n }\n }\n\n private _processArgsText(\n entry: ToolCallEntry,\n content: {\n toolCallId: string;\n toolName: string;\n argsText: string;\n result?: unknown;\n },\n ): void {\n if (!entry.controller) return;\n const hasResult = content.result !== undefined;\n\n if (content.argsText !== entry.argsText) {\n let shouldWriteArgsText = true;\n\n if (entry.argsComplete) {\n if (isEquivalentCompleteArgsText(entry.argsText, content.argsText)) {\n // A.3 — key reorder. Track new text, no re-fire needed.\n entry.argsText = content.argsText;\n shouldWriteArgsText = false;\n } else {\n // A.4 — args changed after first completion. Under the\n // \"exactly once per toolCallId\" contract we do not restart the\n // stream. The host's existing `streamCall` keeps its original\n // args view; the snapshot's new text is recorded for diffing\n // but not surfaced. Events API in a follow-up will expose this\n // to consumers that opt in.\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[ToolInvocationTracker] argsText changed after first completion; not re-firing streamCall (see EDGE_CASES.md A.4)\",\n {\n previous: entry.argsText,\n next: content.argsText,\n toolCallId: content.toolCallId,\n },\n );\n }\n shouldWriteArgsText = false;\n }\n } else if (!content.argsText.startsWith(entry.argsText)) {\n if (\n isArgsTextComplete(entry.argsText) &&\n isArgsTextComplete(content.argsText) &&\n isEquivalentCompleteArgsText(entry.argsText, content.argsText)\n ) {\n const shouldClose = this._shouldCloseArgsStream({\n toolName: content.toolName,\n argsText: content.argsText,\n hasResult,\n });\n if (shouldClose) entry.controller.argsText.close();\n entry.argsText = content.argsText;\n entry.argsComplete = shouldClose;\n shouldWriteArgsText = false;\n } else {\n // A.2 — args regressed mid-stream. Under the \"exactly once\"\n // contract we do not restart. The controller keeps whatever\n // prefix we already streamed; subsequent prefix-respecting\n // updates can still flow against it. Snapshots that never\n // re-converge to a prefix will leave the controller's args\n // view stale relative to the snapshot. Events API in a\n // follow-up will expose this to consumers that opt in.\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[ToolInvocationTracker] argsText regressed mid-stream; not restarting (see EDGE_CASES.md A.2)\",\n {\n previous: entry.argsText,\n next: content.argsText,\n toolCallId: content.toolCallId,\n },\n );\n }\n shouldWriteArgsText = false;\n }\n }\n\n if (shouldWriteArgsText && entry.controller) {\n const delta = content.argsText.slice(entry.argsText.length);\n entry.controller.argsText.append(delta);\n const shouldClose = this._shouldCloseArgsStream({\n toolName: content.toolName,\n argsText: content.argsText,\n hasResult,\n });\n if (shouldClose) entry.controller.argsText.close();\n entry.argsText = content.argsText;\n entry.argsComplete = shouldClose;\n }\n }\n\n if (!entry.argsComplete && entry.controller) {\n // ToolExecutionStream parses the streamed prefix on close, so the close\n // gates on the streamed content; a divergent snapshot (A.2) can be\n // complete while the controller still holds an incomplete stale prefix.\n const shouldClose = this._shouldCloseArgsStream({\n toolName: content.toolName,\n argsText: entry.argsText,\n hasResult,\n });\n if (shouldClose) {\n entry.controller.argsText.close();\n entry.argsComplete = true;\n }\n }\n }\n\n private _processMessages(messages: readonly ThreadMessage[]): void {\n const isRestore = this._pendingRestore;\n\n for (const message of messages) {\n if (!message || !Array.isArray((message as ThreadMessage).content)) {\n continue;\n }\n for (const content of message.content as readonly ThreadMessage[\"content\"][number][]) {\n if (!content || content.type !== \"tool-call\") continue;\n\n const existing = this._entries.get(content.toolCallId);\n\n if (isRestore) {\n // Don't overwrite an already-active entry (e.g. live tool-call\n // observed before this restore snapshot landed). Restore can\n // only seed entries the runtime has never seen.\n if (!existing?.controller) {\n this._entries.set(content.toolCallId, {\n toolName: content.toolName,\n argsText: content.argsText,\n hasResult: content.result !== undefined,\n });\n }\n if (content.messages) this._processMessages(content.messages);\n continue;\n }\n\n // Live snapshot.\n let entry = existing;\n\n if (entry && !entry.controller) {\n // Restored entry observed in a live snapshot. Promote if its\n // signature has changed; otherwise treat as still-historical.\n const signatureChanged =\n content.argsText !== entry.argsText ||\n (content.result !== undefined) !== entry.hasResult;\n if (!signatureChanged) {\n if (content.messages) this._processMessages(content.messages);\n continue;\n }\n this._entries.delete(content.toolCallId);\n entry = undefined;\n }\n\n if (!entry) {\n entry = this._startActiveEntry(\n content.toolCallId,\n content.toolName,\n content.result !== undefined,\n );\n }\n\n if (content.approval !== undefined) entry.skipExecute = true;\n\n this._processArgsText(entry, content);\n\n if (content.result !== undefined && !entry.hasResult) {\n // `entry` is in active phase from this point — either just\n // created by `_startActiveEntry`, or pre-existing with a live\n // controller. Narrow once instead of asserting at every use.\n const { controller: activeController } = entry;\n if (!activeController) continue;\n entry.hasResult = true;\n entry.argsComplete = true;\n activeController.setResponse(\n new ToolResponse({\n result: content.result as ReadonlyJSONValue,\n artifact: content.artifact as ReadonlyJSONValue | undefined,\n isError: content.isError,\n ...(content.modelContent !== undefined\n ? { modelContent: content.modelContent }\n : {}),\n }),\n );\n activeController.close();\n }\n\n if (content.messages) this._processMessages(content.messages);\n }\n }\n }\n}\n\nexport namespace ToolInvocationTracker {\n export type ExecutionStatus = ToolExecutionStatus;\n\n export type Snapshot = {\n readonly messages: readonly ThreadMessage[];\n /** Whether the producing runtime is currently streaming new output. */\n readonly isRunning: boolean;\n /**\n * Whether the producing runtime is still loading historical state.\n * When `true`, every snapshot is treated as historical (no `streamCall` /\n * `execute` fires). When `false`, processing resumes as live.\n */\n readonly isLoading?: boolean;\n };\n\n export type Callbacks = {\n /**\n * Invoked when a client-side `execute()` returns a result and the runtime\n * needs to feed it back into the conversation.\n */\n onResult: (command: AddToolResultCommand) => void;\n /**\n * Invoked whenever the per-tool-call status map changes (executing /\n * interrupt / cleared). The callback receives a fresh map; mutating the\n * argument is not supported.\n */\n onStatusesChange: (\n statuses: ReadonlyMap<string, ToolExecutionStatus>,\n ) => void;\n };\n}\n"],"mappings":";;;;AAkBA,MAAMc,oBAAoBC,OAAOC,IAAI,oCAAoC;AA8CzE,MAAMsB,sBAAsBV,aAAqB;CAC/C,IAAI;EACFW,KAAKC,MAAMZ,QAAQ;EACnB,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;AAEA,MAAMa,iBAAiBb,aAAqB;CAC1C,IAAI;EACF,OAAOW,KAAKC,MAAMZ,QAAQ;CAC5B,QAAQ;EACN;CACF;AACF;AAEA,MAAMe,gCAAgCC,UAAkBC,SAAiB;CACvE,MAAMC,gBAAgBL,cAAcG,QAAQ;CAC5C,MAAMG,YAAYN,cAAc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IyI,QAAQvI,iBAAiBgB,KAAAA,IACzB,EAAEhB,cAAcuI,QAAQvI,aAAa,IACrC,CAAC;KACP,CAAC,CACH;KACAuJ,iBAAiBX,MAAM;IACzB;IAEA,IAAIL,QAAQxD,UAAU,KAAKK,iBAAiBmD,QAAQxD,QAAQ;GAC9D;EACF;CACF;AACF"}
1
+ {"version":3,"file":"ToolInvocationTracker.js","names":["process","env","NODE_ENV","createAssistantStreamController","ToolCallStreamController","ToolResponse","unstable_toolResultStream","Tool","ToolModelContentPart","ToolResultStreamOptions","AssistantMetaTransformStream","ReadonlyJSONValue","isJSONValueEqual","ThreadMessage","ToolCallMessagePart","walkToolCallTree","TOOL_EXECUTION_ID","Symbol","for","ToolExecutionStatus","type","payload","AddToolResultCommand","toolCallId","toolName","result","isError","artifact","modelContent","ToolCallEntry","argsText","hasResult","executionId","skipExecute","controller","argsComplete","clientOwned","SettledResolver","executionIds","ReadonlySet","resolve","isArgsTextComplete","JSON","parse","parseArgsText","undefined","isEquivalentCompleteArgsText","previous","next","previousValue","nextValue","getToolExecutionId","value","Record","PropertyKey","ToolInvocationTracker","_getTools","_callbacks","Callbacks","_isClientToolCall","toolCall","_entries","Map","_humanInput","reject","reason","_executing","Set","_discardedToolCallIds","_settledResolvers","_statuses","_ac","AbortController","_pendingRestore","_lastSnapshot","Snapshot","_isRunning","_controller","ReturnType","_pipelineDead","_pipelineRestartUsed","constructor","getTools","callbacks","isClientToolCall","_initPipeline","stream","human","_onHumanInput","transform","_getWrappedTools","signal","onExecutionStart","id","_name","_onExecutionStart","onExecutionEnd","_onExecutionEnd","pipeThrough","pipeTo","WritableStream","write","chunk","_handleResultChunk","err","console","error","catch","setState","snapshot","_demoteEntriesToRestored","clear","messages","isRunning","isLoading","restoreFromLoading","previousIsRunning","_processMessages","reset","abort","size","_invokeOnStatusesChange","options","discardPending","Promise","forEach","Error","entry","add","push","resume","handlers","get","delete","_setStatus","getStatuses","ReadonlyMap","tools","Object","fromEntries","entries","map","name","tool","execute","streamCall","wrappedTool","args","context","Parameters","_captureExecution","reader","set","_deleteStatus","pending","some","has","length","meta","_invokeOnResult","command","onResult","onStatusesChange","status","_warnProviderOwnedSkip","warn","_shouldCloseArgsStream","_startActiveEntry","toolCallController","addToolCallPart","_processArgsText","content","shouldWriteArgsText","startsWith","shouldClose","close","delta","slice","append","isRestore","part","existing","signatureChanged","ownership","providerOwned","approval","activeController","setResponse","ExecutionStatus","statuses"],"sources":["../../../src/runtimes/tool-invocations/ToolInvocationTracker.ts"],"sourcesContent":["declare const process: { env: { NODE_ENV?: string } };\n\nimport {\n createAssistantStreamController,\n type ToolCallStreamController,\n ToolResponse,\n unstable_toolResultStream,\n type Tool,\n type ToolModelContentPart,\n type ToolResultStreamOptions,\n} from \"assistant-stream\";\nimport {\n AssistantMetaTransformStream,\n type ReadonlyJSONValue,\n} from \"assistant-stream/utils\";\nimport { isJSONValueEqual } from \"../../utils/json/is-json-equal\";\nimport type { ThreadMessage, ToolCallMessagePart } from \"../../types/message\";\nimport { walkToolCallTree } from \"../../runtime/utils/tool-call-tree\";\n\nconst TOOL_EXECUTION_ID = Symbol.for(\"assistant-stream.tool-execution-id\");\n\n/**\n * Streaming execution state for a frontend tool.\n */\nexport type ToolExecutionStatus =\n | { type: \"executing\" }\n | {\n type: \"interrupt\";\n payload: { type: \"human\"; payload: unknown };\n };\n\nexport type AddToolResultCommand = {\n readonly type: \"add-tool-result\";\n readonly toolCallId: string;\n readonly toolName: string;\n readonly result: ReadonlyJSONValue;\n readonly isError: boolean;\n readonly artifact?: ReadonlyJSONValue;\n readonly modelContent?: readonly ToolModelContentPart[];\n};\n\ntype ToolCallEntry = {\n toolName: string;\n argsText: string;\n hasResult: boolean;\n executionId?: symbol;\n skipExecute?: boolean;\n} & (\n | {\n /** Restored phase — observed during a history-load snapshot. */\n controller?: undefined;\n argsComplete?: undefined;\n }\n | {\n /** Active phase — chunks are flowing through `controller`. */\n controller: ToolCallStreamController;\n argsComplete: boolean;\n clientOwned: boolean;\n }\n);\n\ntype SettledResolver = {\n executionIds: ReadonlySet<symbol>;\n resolve: () => void;\n};\n\nconst isArgsTextComplete = (argsText: string) => {\n try {\n JSON.parse(argsText);\n return true;\n } catch {\n return false;\n }\n};\n\nconst parseArgsText = (argsText: string) => {\n try {\n return JSON.parse(argsText);\n } catch {\n return undefined;\n }\n};\n\nconst isEquivalentCompleteArgsText = (previous: string, next: string) => {\n const previousValue = parseArgsText(previous);\n const nextValue = parseArgsText(next);\n if (previousValue === undefined || nextValue === undefined) return false;\n return isJSONValueEqual(previousValue, nextValue);\n};\n\nconst getToolExecutionId = (value: object): symbol | undefined =>\n (value as Record<PropertyKey, unknown>)[TOOL_EXECUTION_ID] as\n | symbol\n | undefined;\n\n/**\n * Plain-class port of the former `useToolInvocations` React hook. Owns the\n * assistant-stream pipeline that drives client-side `streamCall` / `execute`\n * for tool-call parts surfaced by a thread runtime, plus the per-tool-call\n * status map that consumers render against.\n *\n * **Contract**: `streamCall` (and `execute`) fires exactly once per logical\n * `toolCallId`. Args mutations after first completion, result replacement,\n * and result clearing are *not* surfaced through additional `streamCall`\n * invocations — by design — so hosts cannot observe spurious re-fires of\n * side effects. The follow-up `reader.events()` API will expose those\n * post-completion transitions to consumers that opt in.\n *\n * State-transition safety: every public method that observes runtime state\n * (`setState`, `reset`, `abort`, `resume`) wraps its work in try/catch and\n * logs to `console.error` rather than throwing. The tracker is built into\n * the hot message-processing path, so a malformed snapshot must never crash\n * the host runtime. See ./EDGE_CASES.md for the known non-trivial state\n * transitions and what each does today.\n *\n * @deprecated Internal — for framework bindings; not a public API. May change without notice.\n */\nexport class ToolInvocationTracker {\n private readonly _getTools: () => Record<string, Tool> | undefined;\n private readonly _callbacks: ToolInvocationTracker.Callbacks;\n private readonly _isClientToolCall:\n | ((toolCall: ToolCallMessagePart) => boolean | undefined)\n | undefined;\n\n private readonly _entries = new Map<string, ToolCallEntry>();\n private readonly _humanInput = new Map<\n string,\n {\n executionId: symbol;\n resolve: (payload: unknown) => void;\n reject: (reason: unknown) => void;\n }\n >();\n private readonly _executing = new Set<symbol>();\n /**\n * Tool calls whose turn ended before they reached the executor. Held here\n * rather than on the entry because an entry is rebuilt whenever a snapshot\n * re-creates the call, and this is the one reason to skip that no later\n * snapshot carries.\n */\n private readonly _discardedToolCallIds = new Set<string>();\n private readonly _settledResolvers: SettledResolver[] = [];\n\n private _statuses = new Map<string, ToolExecutionStatus>();\n\n private _ac: AbortController = new AbortController();\n private _pendingRestore = true;\n\n /** Cached last snapshot, used to skip processing on identical re-renders. */\n private _lastSnapshot: ToolInvocationTracker.Snapshot | null = null;\n private _isRunning = false;\n\n private _controller!: ReturnType<typeof createAssistantStreamController>[1];\n\n /**\n * Set when the assistant-stream pipeline has died (errored out via\n * `.pipeTo(...).catch(...)`). The next `setState` re-initializes the\n * pipeline and demotes each active entry that reached the executor to\n * restored, so it survives the restart without re-firing `streamCall`.\n * A restart is an execution boundary like `reset()`: an entry that had\n * not reached it starts over and fires once there (F.4). Capped at a\n * single auto-restart per session — repeated failures keep the tracker\n * dead with a more visible error.\n */\n private _pipelineDead = false;\n private _pipelineRestartUsed = false;\n\n constructor(\n getTools: () => Record<string, Tool> | undefined,\n callbacks: ToolInvocationTracker.Callbacks,\n isClientToolCall?: (toolCall: ToolCallMessagePart) => boolean | undefined,\n ) {\n this._getTools = getTools;\n this._callbacks = callbacks;\n this._isClientToolCall = isClientToolCall;\n\n this._initPipeline();\n }\n\n /**\n * Build the assistant-stream pipeline. Called once from the constructor\n * and at most once again if `_pipelineDead` is set (see F.4 in\n * EDGE_CASES.md).\n */\n private _initPipeline(): void {\n const [stream, controller] = createAssistantStreamController();\n this._controller = controller;\n\n const human = (\n toolCallId: string,\n payload: unknown,\n executionId?: symbol,\n ) => this._onHumanInput(toolCallId, payload, executionId);\n const transform = unstable_toolResultStream(\n () => this._getWrappedTools(),\n () => this._ac.signal,\n human,\n {\n onExecutionStart: (id: string, _name: string, executionId?: symbol) =>\n this._onExecutionStart(id, executionId),\n onExecutionEnd: (id: string, _name: string, executionId?: symbol) =>\n this._onExecutionEnd(id, executionId),\n } as ToolResultStreamOptions,\n );\n\n stream\n .pipeThrough(transform)\n .pipeThrough(new AssistantMetaTransformStream())\n .pipeTo(\n new WritableStream({\n write: (chunk) => {\n try {\n if (chunk.type !== \"result\") return;\n this._handleResultChunk(chunk);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] result chunk handling failed\",\n err,\n );\n }\n },\n }),\n )\n .catch((err) => {\n console.error(\n \"[ToolInvocationTracker] stream pipeline failed; will attempt single restart on next setState\",\n err,\n );\n this._pipelineDead = true;\n });\n }\n\n // ───────────────────────── public API ─────────────────────────\n\n /**\n * Feed the next observed snapshot into the tracker. Called from the host\n * runtime whenever its message list / running state changes.\n */\n public setState(snapshot: ToolInvocationTracker.Snapshot): void {\n try {\n // Recover from a dead pipeline before processing anything. Entries\n // that reached the executor are demoted to \"restored\" so the rebuilt\n // pipeline does not re-fire `streamCall` for them; the rest start over\n // across the boundary the restart opens (F.4).\n if (this._pipelineDead) {\n if (this._pipelineRestartUsed) {\n // Already retried once and failed again. Stay dead.\n return;\n }\n this._pipelineRestartUsed = true;\n this._pipelineDead = false;\n this._demoteEntriesToRestored();\n this._executing.clear();\n this._ac = new AbortController();\n this._initPipeline();\n // Fall through and process the snapshot against the fresh pipeline.\n }\n\n // Identical snapshot — skip processing entirely. Note: external-store\n // runtimes rebuild the messages array on every adapter update, so this\n // fast-path rarely triggers there; it's primarily for the React-hook\n // shim where state references are stable.\n if (\n this._lastSnapshot &&\n this._lastSnapshot.messages === snapshot.messages &&\n this._lastSnapshot.isRunning === snapshot.isRunning &&\n this._lastSnapshot.isLoading === snapshot.isLoading\n ) {\n return;\n }\n\n // While the host is still loading initial state, treat every snapshot\n // as historical: tool calls are recorded so the next live snapshot can\n // diff against them, but `streamCall` / `execute` do not fire.\n const restoreFromLoading = snapshot.isLoading === true;\n if (restoreFromLoading) {\n this._pendingRestore = true;\n }\n\n // E.4 / AF3 — only mark `_lastSnapshot`/`_isRunning` as observed after\n // processing succeeds. If `_processMessages` throws, the next snapshot\n // (even if identical) gets re-processed against the recovered state.\n const previousIsRunning = this._isRunning;\n this._isRunning = snapshot.isRunning;\n try {\n this._processMessages(snapshot.messages);\n } catch (err) {\n this._isRunning = previousIsRunning;\n throw err;\n }\n this._lastSnapshot = snapshot;\n this._pendingRestore = false;\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] setState failed; snapshot dropped\",\n err,\n );\n }\n }\n\n /**\n * Reset the tracker so the next observed snapshot is treated as historical.\n * Clears entries and aborts any in-flight executions. Used by callers like\n * `importExternalState` to mark a freshly loaded state as restored.\n */\n public reset(): void {\n try {\n this._pendingRestore = true;\n this._entries.clear();\n this._discardedToolCallIds.clear();\n this._lastSnapshot = null;\n void this.abort();\n // Statuses are cleared synchronously: discarded executions may never\n // settle (aborting hands the signal to the tool, it does not force\n // settlement), and a late settlement of one sibling must not republish\n // the others. `_deleteStatus` no-ops for cleared ids, so post-reset\n // settlements stay silent.\n if (this._statuses.size > 0) {\n this._statuses = new Map();\n this._invokeOnStatusesChange();\n }\n } catch (err) {\n console.error(\"[ToolInvocationTracker] reset failed\", err);\n }\n }\n\n /**\n * Abort any in-flight `execute()` invocations. Resolves once all of them\n * have settled (or immediately if none are running).\n *\n * `discardPending` additionally kills the calls that never reached the\n * executor, for a caller ending the turn rather than interrupting it. The\n * signal cannot reach those: they are waiting on the run to settle (A.10),\n * and the settled snapshot arrives after this installs a fresh controller.\n */\n public abort(options?: { discardPending?: boolean }): Promise<void> {\n try {\n this._humanInput.forEach(({ reject }) => {\n try {\n reject(new Error(\"Tool execution aborted\"));\n } catch {\n // host rejection handler threw — already in the abort path,\n // swallow so we continue cleaning up.\n }\n });\n this._humanInput.clear();\n\n if (options?.discardPending) {\n for (const [toolCallId, entry] of this._entries) {\n if (!entry.controller) continue;\n if (entry.argsComplete || entry.hasResult) continue;\n this._discardedToolCallIds.add(toolCallId);\n entry.skipExecute = true;\n }\n }\n\n this._ac.abort();\n this._ac = new AbortController();\n\n if (this._executing.size === 0) {\n return Promise.resolve();\n }\n const executionIds = new Set(this._executing);\n return new Promise<void>((resolve) => {\n this._settledResolvers.push({ executionIds, resolve });\n });\n } catch (err) {\n console.error(\"[ToolInvocationTracker] abort failed\", err);\n return Promise.resolve();\n }\n }\n\n /**\n * Resolve a pending human-input request for the given tool call. Returns\n * `true` if a pending request was resumed, `false` if the tracker has no\n * outstanding request for that id (the caller should fall back to its own\n * dispatch path).\n */\n public resume(toolCallId: string, payload: unknown): boolean {\n try {\n const handlers = this._humanInput.get(toolCallId);\n if (!handlers) return false;\n this._humanInput.delete(toolCallId);\n this._setStatus(toolCallId, { type: \"executing\" });\n handlers.resolve(payload);\n return true;\n } catch (err) {\n console.error(\"[ToolInvocationTracker] resume failed\", err);\n return false;\n }\n }\n\n /**\n * Returns the current tool execution status map. The returned `Map` is\n * the tracker's internal store — do not mutate it. Treat the reference\n * as a snapshot that may be replaced wholesale on the next status\n * transition.\n */\n public getStatuses(): ReadonlyMap<string, ToolExecutionStatus> {\n return this._statuses;\n }\n\n // ───────────────────── internal: tool wrapping ─────────────────────\n\n private _getWrappedTools(): Record<string, Tool> | undefined {\n const tools = this._getTools();\n if (!tools) return undefined;\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, tool]) => {\n const execute = tool.execute;\n const streamCall = tool.streamCall;\n if (execute === undefined && streamCall === undefined)\n return [name, tool];\n\n const wrappedTool = {\n ...tool,\n ...(execute !== undefined && {\n execute: (...[args, context]: Parameters<typeof execute>) => {\n const executionId = getToolExecutionId(context);\n const entry = this._captureExecution(\n context.toolCallId,\n executionId,\n );\n if (!entry || entry.skipExecute) {\n return new Promise(() => {}) as never;\n }\n return execute(args, context);\n },\n }),\n ...(streamCall !== undefined && {\n streamCall: (\n ...[reader, context]: Parameters<typeof streamCall>\n ) => {\n const executionId = getToolExecutionId(context);\n const entry = this._captureExecution(\n context.toolCallId,\n executionId,\n );\n if (!entry) return;\n return streamCall(reader, context);\n },\n }),\n } as Tool;\n return [name, wrappedTool];\n }),\n ) as Record<string, Tool>;\n }\n\n private _captureExecution(\n toolCallId: string,\n executionId: symbol | undefined,\n ): ToolCallEntry | undefined {\n if (executionId === undefined) return undefined;\n const entry = this._entries.get(toolCallId);\n if (!entry?.controller) return undefined;\n if (entry.executionId === undefined) entry.executionId = executionId;\n return entry.executionId === executionId ? entry : undefined;\n }\n\n // ──────────────── internal: execution lifecycle callbacks ────────────────\n\n private _onHumanInput(\n toolCallId: string,\n payload: unknown,\n executionId?: symbol,\n ): Promise<unknown> {\n return new Promise<unknown>((resolve, reject) => {\n // A discarded execution must not resurrect a status entry or park an\n // unanswerable request after its id has been reused.\n const entry = this._entries.get(toolCallId);\n if (!entry?.controller || entry.executionId !== executionId) {\n reject(new Error(\"Tool execution aborted\"));\n return;\n }\n const previous = this._humanInput.get(toolCallId);\n if (previous) {\n try {\n previous.reject(\n new Error(\"Human input request was superseded by a new request\"),\n );\n } catch {\n // host rejection handler threw; ignore and proceed\n }\n }\n this._humanInput.set(toolCallId, {\n executionId: executionId!,\n resolve,\n reject,\n });\n this._setStatus(toolCallId, {\n type: \"interrupt\",\n payload: { type: \"human\", payload },\n });\n });\n }\n\n private _onExecutionStart(\n toolCallId: string,\n executionId: symbol | undefined,\n ): void {\n if (!this._captureExecution(toolCallId, executionId)) return;\n const entry = this._entries.get(toolCallId)!;\n if (entry.skipExecute) return;\n\n this._executing.add(executionId!);\n // execute can park human() before onExecutionStart; preserve this execution's interrupt.\n if (this._humanInput.get(toolCallId)?.executionId === executionId) return;\n this._setStatus(toolCallId, { type: \"executing\" });\n }\n\n private _onExecutionEnd(\n toolCallId: string,\n executionId: symbol | undefined,\n ): void {\n if (executionId === undefined || !this._executing.delete(executionId))\n return;\n\n const entry = this._entries.get(toolCallId);\n if (entry?.executionId === executionId) this._deleteStatus(toolCallId);\n\n const pending: SettledResolver[] = [];\n this._settledResolvers.forEach(({ executionIds, resolve }) => {\n if ([...executionIds].some((id) => this._executing.has(id))) {\n pending.push({ executionIds, resolve });\n return;\n }\n try {\n resolve();\n } catch {\n // ignore — settled-resolver consumer threw\n }\n });\n this._settledResolvers.length = 0;\n this._settledResolvers.push(...pending);\n }\n\n private _handleResultChunk(chunk: {\n type: \"result\";\n result: ReadonlyJSONValue;\n isError: boolean;\n artifact?: ReadonlyJSONValue;\n modelContent?: readonly ToolModelContentPart[];\n meta: { toolCallId: string; toolName: string };\n }): void {\n const toolCallId = chunk.meta.toolCallId;\n const executionId = getToolExecutionId(chunk);\n const entry = this._entries.get(toolCallId);\n\n if (!entry || entry.executionId !== executionId) return;\n\n // The host already set the result (via the live snapshot's\n // `setResponse` path). Suppress the executor's redundant emit.\n if (entry?.hasResult) return;\n if (entry.skipExecute) return;\n\n this._invokeOnResult({\n type: \"add-tool-result\",\n toolCallId,\n toolName: chunk.meta.toolName,\n result: chunk.result,\n isError: chunk.isError,\n ...(chunk.artifact !== undefined && { artifact: chunk.artifact }),\n ...(chunk.modelContent !== undefined && {\n modelContent: chunk.modelContent,\n }),\n });\n }\n\n // ──────────────── internal: callback invocation (AF1/AF2) ────────────────\n\n private _invokeOnResult(command: AddToolResultCommand): void {\n try {\n this._callbacks.onResult(command);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] onResult callback threw; result dropped\",\n err,\n );\n }\n }\n\n private _invokeOnStatusesChange(): void {\n try {\n this._callbacks.onStatusesChange(this._statuses);\n } catch (err) {\n console.error(\n \"[ToolInvocationTracker] onStatusesChange callback threw; status change not propagated\",\n err,\n );\n }\n }\n\n // ──────────────── internal: status map mutations ────────────────\n\n private _setStatus(toolCallId: string, status: ToolExecutionStatus): void {\n const next = new Map(this._statuses);\n next.set(toolCallId, status);\n this._statuses = next;\n this._invokeOnStatusesChange();\n }\n\n private _deleteStatus(toolCallId: string): void {\n if (!this._statuses.has(toolCallId)) return;\n const next = new Map(this._statuses);\n next.delete(toolCallId);\n this._statuses = next;\n this._invokeOnStatusesChange();\n }\n\n // ──────────────── internal: snapshot processing ────────────────\n\n private _warnProviderOwnedSkip(toolName: string, toolCallId: string): void {\n if (process.env.NODE_ENV === \"production\") return;\n if (this._getTools()?.[toolName]?.execute === undefined) return;\n console.warn(\n \"[ToolInvocationTracker] the runtime reports this tool call as provider-owned, so the registered execute is skipped; the provider has to hand the call to the client for it to run (see EDGE_CASES.md A.9)\",\n { toolCallId, toolName },\n );\n }\n\n /**\n * Closing the args stream hands the call to the client executor, so it may\n * only happen once the provider can no longer speak about that call. The run\n * ending is the only universal signal for that; an adapter that reports the\n * call as client-owned has said it earlier, per call.\n */\n private _shouldCloseArgsStream({\n argsText,\n hasResult,\n clientOwned,\n }: {\n argsText: string;\n hasResult: boolean;\n clientOwned: boolean;\n }): boolean {\n if (hasResult) return true;\n if (!isArgsTextComplete(argsText)) return false;\n return clientOwned || !this._isRunning;\n }\n\n private _startActiveEntry(\n toolCallId: string,\n toolName: string,\n skipExecute: boolean,\n clientOwned: boolean,\n ): ToolCallEntry {\n const toolCallController = this._controller.addToolCallPart({\n toolName,\n toolCallId,\n });\n const entry: ToolCallEntry = {\n toolName,\n controller: toolCallController,\n argsText: \"\",\n hasResult: false,\n skipExecute,\n argsComplete: false,\n clientOwned,\n };\n this._entries.set(toolCallId, entry);\n return entry;\n }\n\n /**\n * Demote every active entry back to the restored phase. Used by the\n * pipeline-restart path so that, after a fresh pipeline is built, the\n * next observed snapshot does not re-fire `streamCall` for tool calls\n * that already fired pre-death. Args / hasResult tracking is preserved\n * so signature comparisons still work.\n */\n private _demoteEntriesToRestored(): void {\n for (const [toolCallId, entry] of this._entries) {\n if (!entry.controller) continue;\n if (!entry.argsComplete && !entry.hasResult) {\n // The call never reached the executor. A restored entry is promoted\n // only when its signature changes, and a call waiting on the run to\n // settle already holds its final args, so demoting it would strand it\n // unexecuted. Dropping it lets the next snapshot start it over; a call\n // whose turn was discarded is held by `_discardedToolCallIds`, not by\n // the entry, so starting over does not revive it.\n this._entries.delete(toolCallId);\n continue;\n }\n this._entries.set(toolCallId, {\n toolName: entry.toolName,\n argsText: entry.argsText,\n hasResult: entry.hasResult,\n });\n }\n }\n\n private _processArgsText(\n entry: ToolCallEntry,\n content: {\n toolCallId: string;\n toolName: string;\n argsText: string;\n result?: unknown;\n },\n ): void {\n if (!entry.controller) return;\n const hasResult = content.result !== undefined;\n\n if (content.argsText !== entry.argsText) {\n let shouldWriteArgsText = true;\n\n if (entry.argsComplete) {\n if (isEquivalentCompleteArgsText(entry.argsText, content.argsText)) {\n // A.3 — key reorder. Track new text, no re-fire needed.\n entry.argsText = content.argsText;\n shouldWriteArgsText = false;\n } else {\n // A.4 — args changed after first completion. Under the\n // \"exactly once per toolCallId\" contract we do not restart the\n // stream. The host's existing `streamCall` keeps its original\n // args view; the snapshot's new text is recorded for diffing\n // but not surfaced. Events API in a follow-up will expose this\n // to consumers that opt in.\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[ToolInvocationTracker] argsText changed after first completion; not re-firing streamCall (see EDGE_CASES.md A.4)\",\n {\n previous: entry.argsText,\n next: content.argsText,\n toolCallId: content.toolCallId,\n },\n );\n }\n shouldWriteArgsText = false;\n }\n } else if (!content.argsText.startsWith(entry.argsText)) {\n if (\n isArgsTextComplete(entry.argsText) &&\n isArgsTextComplete(content.argsText) &&\n isEquivalentCompleteArgsText(entry.argsText, content.argsText)\n ) {\n const shouldClose = this._shouldCloseArgsStream({\n argsText: content.argsText,\n hasResult,\n clientOwned: entry.clientOwned,\n });\n if (shouldClose) entry.controller.argsText.close();\n entry.argsText = content.argsText;\n entry.argsComplete = shouldClose;\n shouldWriteArgsText = false;\n } else {\n // A.2 — args regressed mid-stream. Under the \"exactly once\"\n // contract we do not restart. The controller keeps whatever\n // prefix we already streamed; subsequent prefix-respecting\n // updates can still flow against it. Snapshots that never\n // re-converge to a prefix will leave the controller's args\n // view stale relative to the snapshot. Events API in a\n // follow-up will expose this to consumers that opt in.\n if (process.env.NODE_ENV !== \"production\") {\n console.warn(\n \"[ToolInvocationTracker] argsText regressed mid-stream; not restarting (see EDGE_CASES.md A.2)\",\n {\n previous: entry.argsText,\n next: content.argsText,\n toolCallId: content.toolCallId,\n },\n );\n }\n shouldWriteArgsText = false;\n }\n }\n\n if (shouldWriteArgsText && entry.controller) {\n const delta = content.argsText.slice(entry.argsText.length);\n entry.controller.argsText.append(delta);\n const shouldClose = this._shouldCloseArgsStream({\n argsText: content.argsText,\n hasResult,\n clientOwned: entry.clientOwned,\n });\n if (shouldClose) entry.controller.argsText.close();\n entry.argsText = content.argsText;\n entry.argsComplete = shouldClose;\n }\n }\n\n if (!entry.argsComplete && entry.controller) {\n // ToolExecutionStream parses the streamed prefix on close, so the close\n // gates on the streamed content; a divergent snapshot (A.2) can be\n // complete while the controller still holds an incomplete stale prefix.\n const shouldClose = this._shouldCloseArgsStream({\n argsText: entry.argsText,\n hasResult,\n clientOwned: entry.clientOwned,\n });\n if (shouldClose) {\n entry.controller.argsText.close();\n entry.argsComplete = true;\n }\n }\n }\n\n private _processMessages(messages: readonly ThreadMessage[]): void {\n const isRestore = this._pendingRestore;\n\n for (const { part: content } of walkToolCallTree(messages)) {\n const existing = this._entries.get(content.toolCallId);\n\n if (isRestore) {\n // Don't overwrite an already-active entry (e.g. live tool-call\n // observed before this restore snapshot landed). Restore can\n // only seed entries the runtime has never seen.\n if (!existing?.controller) {\n this._entries.set(content.toolCallId, {\n toolName: content.toolName,\n argsText: content.argsText,\n hasResult: content.result !== undefined,\n });\n }\n continue;\n }\n\n // Live snapshot.\n let entry = existing;\n\n // A discarded id is remembered only until the call is answered, which\n // bounds the set to the open calls of a discarded turn.\n if (content.result !== undefined)\n this._discardedToolCallIds.delete(content.toolCallId);\n\n if (entry && !entry.controller) {\n // Restored entry observed in a live snapshot. Promote if its\n // signature has changed; otherwise treat as still-historical.\n const signatureChanged =\n content.argsText !== entry.argsText ||\n (content.result !== undefined) !== entry.hasResult;\n if (!signatureChanged) continue;\n this._entries.delete(content.toolCallId);\n entry = undefined;\n }\n\n if (!entry) {\n const ownership = this._isClientToolCall?.(content);\n const providerOwned =\n content.result === undefined && ownership === false;\n if (providerOwned)\n this._warnProviderOwnedSkip(content.toolName, content.toolCallId);\n entry = this._startActiveEntry(\n content.toolCallId,\n content.toolName,\n content.result !== undefined ||\n providerOwned ||\n this._discardedToolCallIds.has(content.toolCallId),\n ownership === true,\n );\n }\n\n if (content.approval !== undefined) entry.skipExecute = true;\n\n this._processArgsText(entry, content);\n\n if (content.result !== undefined && !entry.hasResult) {\n // `entry` is in active phase from this point — either just\n // created by `_startActiveEntry`, or pre-existing with a live\n // controller. Narrow once instead of asserting at every use.\n const { controller: activeController } = entry;\n if (!activeController) continue;\n entry.hasResult = true;\n entry.argsComplete = true;\n activeController.setResponse(\n new ToolResponse({\n result: content.result as ReadonlyJSONValue,\n artifact: content.artifact as ReadonlyJSONValue | undefined,\n isError: content.isError,\n ...(content.modelContent !== undefined\n ? { modelContent: content.modelContent }\n : {}),\n }),\n );\n activeController.close();\n }\n }\n }\n}\n\nexport namespace ToolInvocationTracker {\n export type ExecutionStatus = ToolExecutionStatus;\n\n export type Snapshot = {\n readonly messages: readonly ThreadMessage[];\n /** Whether the producing runtime is currently streaming new output. */\n readonly isRunning: boolean;\n /**\n * Whether the producing runtime is still loading historical state.\n * When `true`, every snapshot is treated as historical (no `streamCall` /\n * `execute` fires). When `false`, processing resumes as live.\n */\n readonly isLoading?: boolean;\n };\n\n export type Callbacks = {\n /**\n * Invoked when a client-side `execute()` returns a result and the runtime\n * needs to feed it back into the conversation.\n */\n onResult: (command: AddToolResultCommand) => void;\n /**\n * Invoked whenever the per-tool-call status map changes (executing /\n * interrupt / cleared). The callback receives a fresh map; mutating the\n * argument is not supported.\n */\n onStatusesChange: (\n statuses: ReadonlyMap<string, ToolExecutionStatus>,\n ) => void;\n };\n}\n"],"mappings":";;;;;AAmBA,MAAMgB,oBAAoBC,OAAOC,IAAI,oCAAoC;AA+CzE,MAAMuB,sBAAsBX,aAAqB;CAC/C,IA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IAAIwG,SAAS,CAACA,MAAM7F,YAAY;IAM9B,IAAI,EAFFyI,QAAQ7I,aAAaiG,MAAMjG,YAC1B6I,QAAQlJ,WAAWoB,KAAAA,MAAekF,MAAMhG,YACpB;IACvB,KAAK8B,SAASwE,OAAOsC,QAAQpJ,UAAU;IACvCwG,QAAQlF,KAAAA;GACV;GAEA,IAAI,CAACkF,OAAO;IACV,MAAMwD,YAAY,KAAK5H,oBAAoBgH,OAAO;IAClD,MAAMa,gBACJb,QAAQlJ,WAAWoB,KAAAA,KAAa0I,cAAc;IAChD,IAAIC,eACF,KAAKpB,uBAAuBO,QAAQnJ,UAAUmJ,QAAQpJ,UAAU;IAClEwG,QAAQ,KAAKwC,kBACXI,QAAQpJ,YACRoJ,QAAQnJ,UACRmJ,QAAQlJ,WAAWoB,KAAAA,KACjB2I,iBACA,KAAKpH,sBAAsBwF,IAAIe,QAAQpJ,UAAU,GACnDgK,cAAc,IAChB;GACF;GAEA,IAAIZ,QAAQc,aAAa5I,KAAAA,GAAWkF,MAAM9F,cAAc;GAExD,KAAKyI,iBAAiB3C,OAAO4C,OAAO;GAEpC,IAAIA,QAAQlJ,WAAWoB,KAAAA,KAAa,CAACkF,MAAMhG,WAAW;IAIpD,MAAM,EAAEG,YAAYwJ,qBAAqB3D;IACzC,IAAI,CAAC2D,kBAAkB;IACvB3D,MAAMhG,YAAY;IAClBgG,MAAM5F,eAAe;IACrBuJ,iBAAiBC,YACf,IAAItL,aAAa;KACfoB,QAAQkJ,QAAQlJ;KAChBE,UAAUgJ,QAAQhJ;KAClBD,SAASiJ,QAAQjJ;KACjB,GAAIiJ,QAAQ/I,iBAAiBiB,KAAAA,IACzB,EAAEjB,cAAc+I,QAAQ/I,aAAa,IACrC,CAAC;IACP,CAAC,CACH;IACA8J,iBAAiBX,MAAM;GACzB;EACF;CACF;AACF"}
@@ -53,7 +53,7 @@ type ExternalThreadProps = {
53
53
  /** Branch adapter for runtimes that track sibling variants of messages. */
54
54
  branches?: ExternalThreadBranchAdapter;
55
55
  /** Callback for tool approval decisions. Absent: responding to an approval throws a capability error. */
56
- onRespondToToolApproval?: (options: RespondToToolApprovalOptions) => void;
56
+ onRespondToToolApproval?: (options: RespondToToolApprovalOptions) => void | Promise<void>;
57
57
  };
58
58
  declare const ExternalThread: import("@assistant-ui/tap").Resource<ClientOutput<"thread">, [ExternalThreadProps]>;
59
59
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"external-thread.d.ts","names":[],"sources":["../../../src/store/clients/external-thread.ts"],"mappings":";;;;;;;;;;KA6DY,wBAAwB;EAClC;;KAgBU;EACV,mBAAmB;EACnB;EACA;EACA,QAAQ;EACR;;;;;;;EAOA;;;;;EAKA,SAAS,SAAS;EAClB,UAAU,SAAS;EACnB,YAAY;EACZ;EACA;EACA;;;;;;;EAOA,yBAAyB;EACzB,oBAAoB,SAAS;;EAE7B,qBAAqB,SAAS;EAC9B,wBAAwB;EACxB,oBAAoB;EACpB,kBAAkB;EAClB,gBAAgB;;EAEhB,QAAQ;;EAER,WAAW;;EAEX,2BAA2B,SAAS;;cAohCzB,4CAAc,SAAA,yBAAA"}
1
+ {"version":3,"file":"external-thread.d.ts","names":[],"sources":["../../../src/store/clients/external-thread.ts"],"mappings":";;;;;;;;;;KA8DY,wBAAwB;EAClC;;KAgBU;EACV,mBAAmB;EACnB;EACA;EACA,QAAQ;EACR;;;;;;;EAOA;;;;;EAKA,SAAS,SAAS;EAClB,UAAU,SAAS;EACnB,YAAY;EACZ;EACA;EACA;;;;;;;EAOA,yBAAyB;EACzB,oBAAoB,SAAS;;EAE7B,qBAAqB,SAAS;EAC9B,wBAAwB;EACxB,oBAAoB;EACpB,kBAAkB;EAClB,gBAAgB;;EAEhB,QAAQ;;EAER,WAAW;;EAEX,2BACE,SAAS,wCACC;;cAuhCD,4CAAc,SAAA,yBAAA"}
@@ -1,3 +1,4 @@
1
+ import { generateId } from "../../utils/id.js";
1
2
  import { fileMatchesAccept } from "../../adapters/attachment.js";
2
3
  import { getThreadMessageText } from "../../utils/text.js";
3
4
  import { toMessagePartStatus } from "../../utils/normalizePartStatus.js";
@@ -9,11 +10,11 @@ import { ModelContext } from "./model-context-client.js";
9
10
  import { DataRenderers } from "../../react/client/DataRenderers.js";
10
11
  import { Tools } from "../../react/client/Tools.js";
11
12
  import { SingleThreadList } from "./single-thread-list.js";
13
+ import { Derived, attachTransformScopes, useClientLookup, useClientResource } from "@assistant-ui/store/client";
12
14
  import { ToolResponse } from "assistant-stream";
13
15
  import { c } from "@assistant-ui/tap/react-shim/compiler-runtime";
14
16
  import { useEffect, useRef, useState } from "@assistant-ui/tap/react-shim";
15
17
  import { resource, withKey } from "@assistant-ui/tap";
16
- import { Derived, attachTransformScopes, useClientLookup, useClientResource } from "@assistant-ui/store/client";
17
18
  //#region src/store/clients/external-thread.ts
18
19
  const EMPTY_QUEUE_ITEMS = [];
19
20
  const EMPTY_BRANCH_IDS = [];
@@ -322,7 +323,13 @@ const usePartResource = (t0) => {
322
323
  if (!onRespondToToolApproval) throw new Error("Runtime does not support tool approvals.");
323
324
  if (part.type !== "tool-call") throw new Error("Tried to respond to tool approval on non-tool message part");
324
325
  if (!part.approval || part.approval.approved !== void 0 || part.approval.resolution !== void 0) throw new Error("Tool call has no pending approval");
325
- onRespondToToolApproval(resolveToolApprovalResponse(part.approval, response_0));
326
+ const options = resolveToolApprovalResponse(part.approval, response_0);
327
+ try {
328
+ return Promise.resolve(onRespondToToolApproval(options));
329
+ } catch (t7) {
330
+ const error = t7;
331
+ return Promise.reject(error);
332
+ }
326
333
  };
327
334
  $[12] = onRespondToToolApproval;
328
335
  $[13] = part;
@@ -587,7 +594,7 @@ const useComposerClientResource = (t0) => {
587
594
  }
588
595
  } else if (!isCreateAttachment(fileOrAttachment)) {
589
596
  const newAttachment = {
590
- id: Math.random().toString(36).substring(7),
597
+ id: generateId(),
591
598
  type: "file",
592
599
  name: fileOrAttachment.name,
593
600
  contentType: fileOrAttachment.type,
@@ -598,7 +605,7 @@ const useComposerClientResource = (t0) => {
598
605
  setAttachments((prev_1) => [...prev_1, newAttachment]);
599
606
  } else {
600
607
  const newAttachment_0 = {
601
- id: fileOrAttachment.id ?? Math.random().toString(36).substring(7),
608
+ id: fileOrAttachment.id ?? generateId(),
602
609
  type: fileOrAttachment.type ?? "document",
603
610
  name: fileOrAttachment.name,
604
611
  contentType: fileOrAttachment.contentType,