@assistant-ui/core 0.3.15 → 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 (455) 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 +8 -2
  5. package/dist/internal.js +9 -3
  6. package/dist/model-context/frame/host.d.ts.map +1 -1
  7. package/dist/model-context/frame/host.js +3 -2
  8. package/dist/model-context/frame/host.js.map +1 -1
  9. package/dist/model-context/frame/provider.d.ts +5 -0
  10. package/dist/model-context/frame/provider.d.ts.map +1 -1
  11. package/dist/model-context/frame/provider.js +141 -28
  12. package/dist/model-context/frame/provider.js.map +1 -1
  13. package/dist/model-context/interactable-composer-metadata.d.ts +1 -1
  14. package/dist/model-context/interactable-composer-metadata.js +1 -1
  15. package/dist/model-context/interactable-composer-metadata.js.map +1 -1
  16. package/dist/model-context/registry.d.ts +2 -2
  17. package/dist/model-context/registry.d.ts.map +1 -1
  18. package/dist/model-context/registry.js +9 -28
  19. package/dist/model-context/registry.js.map +1 -1
  20. package/dist/model-context/tool-call-text.d.ts +26 -0
  21. package/dist/model-context/tool-call-text.d.ts.map +1 -0
  22. package/dist/model-context/tool-call-text.js +18 -0
  23. package/dist/model-context/tool-call-text.js.map +1 -0
  24. package/dist/react/RuntimeAdapter.js +1 -1
  25. package/dist/react/adapters/LocalStorageThreadListAdapter.d.ts.map +1 -1
  26. package/dist/react/adapters/LocalStorageThreadListAdapter.js +1 -1
  27. package/dist/react/adapters/LocalStorageThreadListAdapter.js.map +1 -1
  28. package/dist/react/client/AdaptedRemoteThread.js +1 -1
  29. package/dist/react/client/InMemoryThreadList.d.ts.map +1 -1
  30. package/dist/react/client/InMemoryThreadList.js +25 -21
  31. package/dist/react/client/InMemoryThreadList.js.map +1 -1
  32. package/dist/react/client/Interactables.d.ts.map +1 -1
  33. package/dist/react/client/Interactables.js +60 -178
  34. package/dist/react/client/Interactables.js.map +1 -1
  35. package/dist/react/client/RemoteThreadList.d.ts +15 -0
  36. package/dist/react/client/RemoteThreadList.d.ts.map +1 -1
  37. package/dist/react/client/RemoteThreadList.js +263 -194
  38. package/dist/react/client/RemoteThreadList.js.map +1 -1
  39. package/dist/react/client/Tools.d.ts.map +1 -1
  40. package/dist/react/client/Tools.js +6 -2
  41. package/dist/react/client/Tools.js.map +1 -1
  42. package/dist/react/index.d.ts +4 -2
  43. package/dist/react/index.js +2 -1
  44. package/dist/react/interactables-legacy/Interactables.d.ts.map +1 -1
  45. package/dist/react/interactables-legacy/Interactables.js +60 -178
  46. package/dist/react/interactables-legacy/Interactables.js.map +1 -1
  47. package/dist/react/interactables-shared/useInteractablePersistenceQueue.d.ts +24 -0
  48. package/dist/react/interactables-shared/useInteractablePersistenceQueue.d.ts.map +1 -0
  49. package/dist/react/interactables-shared/useInteractablePersistenceQueue.js +138 -0
  50. package/dist/react/interactables-shared/useInteractablePersistenceQueue.js.map +1 -0
  51. package/dist/react/model-context/toolbox.d.ts +2 -14
  52. package/dist/react/model-context/toolbox.d.ts.map +1 -1
  53. package/dist/react/model-context/toolbox.js +1 -13
  54. package/dist/react/model-context/toolbox.js.map +1 -1
  55. package/dist/react/model-context/useAssistantContext.js +37 -7
  56. package/dist/react/model-context/useAssistantContext.js.map +1 -1
  57. package/dist/react/model-context/useAuiToolOverrides.js +31 -5
  58. package/dist/react/model-context/useAuiToolOverrides.js.map +1 -1
  59. package/dist/react/model-context/useInlineRender.d.ts.map +1 -1
  60. package/dist/react/model-context/useInlineRender.js +10 -36
  61. package/dist/react/model-context/useInlineRender.js.map +1 -1
  62. package/dist/react/primitive-hooks/useMessageError.d.ts.map +1 -1
  63. package/dist/react/primitive-hooks/useMessageError.js +2 -8
  64. package/dist/react/primitive-hooks/useMessageError.js.map +1 -1
  65. package/dist/react/primitive-hooks/useThreadListLoadMore.d.ts.map +1 -1
  66. package/dist/react/primitive-hooks/useThreadListLoadMore.js +2 -4
  67. package/dist/react/primitive-hooks/useThreadListLoadMore.js.map +1 -1
  68. package/dist/react/primitives/composer/ComposerAttachments.d.ts.map +1 -1
  69. package/dist/react/primitives/composer/ComposerAttachments.js +4 -3
  70. package/dist/react/primitives/composer/ComposerAttachments.js.map +1 -1
  71. package/dist/react/primitives/composer/ComposerIf.js.map +1 -1
  72. package/dist/react/primitives/composer/ComposerQueue.d.ts +2 -1
  73. package/dist/react/primitives/composer/ComposerQueue.d.ts.map +1 -1
  74. package/dist/react/primitives/composer/ComposerQueue.js +4 -3
  75. package/dist/react/primitives/composer/ComposerQueue.js.map +1 -1
  76. package/dist/react/primitives/message/MessageAttachments.d.ts.map +1 -1
  77. package/dist/react/primitives/message/MessageAttachments.js +7 -6
  78. package/dist/react/primitives/message/MessageAttachments.js.map +1 -1
  79. package/dist/react/primitives/message/MessageGroupedParts.js +2 -2
  80. package/dist/react/primitives/message/MessageGroupedParts.js.map +1 -1
  81. package/dist/react/primitives/message/MessageParts.js +3 -3
  82. package/dist/react/primitives/message/MessageParts.js.map +1 -1
  83. package/dist/react/primitives/threadList/ThreadListItems.d.ts.map +1 -1
  84. package/dist/react/primitives/threadList/ThreadListItems.js +5 -4
  85. package/dist/react/primitives/threadList/ThreadListItems.js.map +1 -1
  86. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +7 -5
  87. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
  88. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js +44 -22
  89. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js.map +1 -1
  90. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +7 -6
  91. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
  92. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +102 -117
  93. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -1
  94. package/dist/react/runtimes/RemoteThreadResource.d.ts +1 -6
  95. package/dist/react/runtimes/RemoteThreadResource.d.ts.map +1 -1
  96. package/dist/react/runtimes/RemoteThreadResource.js +3 -3
  97. package/dist/react/runtimes/RemoteThreadResource.js.map +1 -1
  98. package/dist/react/runtimes/assistant-transport/runManager.d.ts.map +1 -1
  99. package/dist/react/runtimes/assistant-transport/runManager.js +2 -12
  100. package/dist/react/runtimes/assistant-transport/runManager.js.map +1 -1
  101. package/dist/react/runtimes/assistant-transport/useLatestRef.js +2 -2
  102. package/dist/react/runtimes/assistant-transport/useLatestRef.js.map +1 -1
  103. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts +3 -10
  104. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -1
  105. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js +43 -85
  106. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -1
  107. package/dist/react/runtimes/cloud/auiV0.d.ts +5 -1
  108. package/dist/react/runtimes/cloud/auiV0.d.ts.map +1 -1
  109. package/dist/react/runtimes/cloud/auiV0.js.map +1 -1
  110. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.d.ts +29 -0
  111. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.d.ts.map +1 -0
  112. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.js +159 -0
  113. package/dist/react/runtimes/cloud/createCloudThreadListAdapter.js.map +1 -0
  114. package/dist/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.d.ts +12 -0
  115. package/dist/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.d.ts.map +1 -0
  116. package/dist/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.js +10 -0
  117. package/dist/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.js.map +1 -0
  118. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts +1 -9
  119. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts.map +1 -1
  120. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js +16 -153
  121. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js.map +1 -1
  122. package/dist/react/runtimes/cloud/useCloudThreadListRuntime.js.map +1 -1
  123. package/dist/react/runtimes/createMessageConverter.d.ts +2 -1
  124. package/dist/react/runtimes/createMessageConverter.d.ts.map +1 -1
  125. package/dist/react/runtimes/external-message-converter.d.ts +6 -25
  126. package/dist/react/runtimes/external-message-converter.d.ts.map +1 -1
  127. package/dist/react/runtimes/external-message-converter.js +33 -233
  128. package/dist/react/runtimes/external-message-converter.js.map +1 -1
  129. package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts.map +1 -1
  130. package/dist/react/runtimes/useRemoteThreadListRuntime.js +14 -6
  131. package/dist/react/runtimes/useRemoteThreadListRuntime.js.map +1 -1
  132. package/dist/react/types/MessagePartComponentTypes.d.ts +7 -3
  133. package/dist/react/types/MessagePartComponentTypes.d.ts.map +1 -1
  134. package/dist/react/types/scopes/tools.d.ts +2 -0
  135. package/dist/react/types/scopes/tools.d.ts.map +1 -1
  136. package/dist/runtime/api/composer-runtime.d.ts +1 -1
  137. package/dist/runtime/api/composer-runtime.js.map +1 -1
  138. package/dist/runtime/api/message-part-runtime.d.ts +2 -2
  139. package/dist/runtime/api/message-part-runtime.d.ts.map +1 -1
  140. package/dist/runtime/api/message-part-runtime.js +1 -1
  141. package/dist/runtime/api/message-part-runtime.js.map +1 -1
  142. package/dist/runtime/api/thread-list-runtime.d.ts +9 -1
  143. package/dist/runtime/api/thread-list-runtime.d.ts.map +1 -1
  144. package/dist/runtime/api/thread-list-runtime.js +5 -0
  145. package/dist/runtime/api/thread-list-runtime.js.map +1 -1
  146. package/dist/runtime/api/thread-runtime.d.ts +2 -3
  147. package/dist/runtime/api/thread-runtime.d.ts.map +1 -1
  148. package/dist/runtime/base/base-composer-runtime-core.d.ts +1 -1
  149. package/dist/runtime/base/base-composer-runtime-core.d.ts.map +1 -1
  150. package/dist/runtime/base/base-composer-runtime-core.js +14 -29
  151. package/dist/runtime/base/base-composer-runtime-core.js.map +1 -1
  152. package/dist/runtime/base/base-thread-runtime-core.d.ts +4 -6
  153. package/dist/runtime/base/base-thread-runtime-core.d.ts.map +1 -1
  154. package/dist/runtime/base/base-thread-runtime-core.js +5 -11
  155. package/dist/runtime/base/base-thread-runtime-core.js.map +1 -1
  156. package/dist/runtime/base/default-thread-composer-runtime-core.d.ts +1 -1
  157. package/dist/runtime/base/default-thread-composer-runtime-core.d.ts.map +1 -1
  158. package/dist/runtime/base/default-thread-composer-runtime-core.js +1 -1
  159. package/dist/runtime/base/default-thread-composer-runtime-core.js.map +1 -1
  160. package/dist/runtime/interfaces/composer-runtime-core.d.ts +1 -1
  161. package/dist/runtime/interfaces/thread-list-runtime-core.d.ts +14 -2
  162. package/dist/runtime/interfaces/thread-list-runtime-core.d.ts.map +1 -1
  163. package/dist/runtime/interfaces/thread-runtime-core.d.ts +16 -2
  164. package/dist/runtime/interfaces/thread-runtime-core.d.ts.map +1 -1
  165. package/dist/runtime/internal.d.ts +2 -1
  166. package/dist/runtime/internal.js +2 -1
  167. package/dist/runtime/queue/external-thread-queue-adapter.d.ts +1 -1
  168. package/dist/runtime/queue/message-queue.d.ts.map +1 -1
  169. package/dist/runtime/queue/message-queue.js +1 -1
  170. package/dist/runtime/queue/message-queue.js.map +1 -1
  171. package/dist/runtime/queue/queue-item.d.ts +12 -0
  172. package/dist/runtime/queue/queue-item.d.ts.map +1 -0
  173. package/dist/runtime/queue/queue-item.js +6 -0
  174. package/dist/runtime/queue/queue-item.js.map +1 -0
  175. package/dist/runtime/utils/abortable-thread-load.d.ts +15 -0
  176. package/dist/runtime/utils/abortable-thread-load.d.ts.map +1 -0
  177. package/dist/runtime/utils/abortable-thread-load.js +35 -0
  178. package/dist/runtime/utils/abortable-thread-load.js.map +1 -0
  179. package/dist/runtime/utils/attachment-add-operations.d.ts +18 -0
  180. package/dist/runtime/utils/attachment-add-operations.d.ts.map +1 -0
  181. package/dist/runtime/utils/attachment-add-operations.js +43 -0
  182. package/dist/runtime/utils/attachment-add-operations.js.map +1 -0
  183. package/dist/runtime/utils/auto-status.d.ts +7 -2
  184. package/dist/runtime/utils/auto-status.d.ts.map +1 -1
  185. package/dist/runtime/utils/auto-status.js +13 -3
  186. package/dist/runtime/utils/auto-status.js.map +1 -1
  187. package/dist/runtime/utils/external-message-conversion.d.ts +48 -0
  188. package/dist/runtime/utils/external-message-conversion.d.ts.map +1 -0
  189. package/dist/runtime/utils/external-message-conversion.js +222 -0
  190. package/dist/runtime/utils/external-message-conversion.js.map +1 -0
  191. package/dist/runtime/utils/external-store-message.d.ts +2 -1
  192. package/dist/runtime/utils/external-store-message.d.ts.map +1 -1
  193. package/dist/runtime/utils/external-store-message.js +2 -1
  194. package/dist/runtime/utils/external-store-message.js.map +1 -1
  195. package/dist/runtime/utils/message-repository-session.d.ts +30 -0
  196. package/dist/runtime/utils/message-repository-session.d.ts.map +1 -0
  197. package/dist/runtime/utils/message-repository-session.js +130 -0
  198. package/dist/runtime/utils/message-repository-session.js.map +1 -0
  199. package/dist/runtime/utils/message-repository.d.ts.map +1 -1
  200. package/dist/runtime/utils/message-repository.js +3 -4
  201. package/dist/runtime/utils/message-repository.js.map +1 -1
  202. package/dist/runtime/utils/pending-tool-calls.d.ts +20 -0
  203. package/dist/runtime/utils/pending-tool-calls.d.ts.map +1 -0
  204. package/dist/runtime/utils/pending-tool-calls.js +22 -0
  205. package/dist/runtime/utils/pending-tool-calls.js.map +1 -0
  206. package/dist/runtime/utils/resolveToolApprovalResponse.d.ts +6 -3
  207. package/dist/runtime/utils/resolveToolApprovalResponse.d.ts.map +1 -1
  208. package/dist/runtime/utils/resolveToolApprovalResponse.js +12 -3
  209. package/dist/runtime/utils/resolveToolApprovalResponse.js.map +1 -1
  210. package/dist/runtime/utils/thread-message-like.d.ts +3 -10
  211. package/dist/runtime/utils/thread-message-like.d.ts.map +1 -1
  212. package/dist/runtime/utils/thread-message-like.js.map +1 -1
  213. package/dist/runtime/utils/tool-call-tree.d.ts +39 -0
  214. package/dist/runtime/utils/tool-call-tree.d.ts.map +1 -0
  215. package/dist/runtime/utils/tool-call-tree.js +85 -0
  216. package/dist/runtime/utils/tool-call-tree.js.map +1 -0
  217. package/dist/runtimes/external-store/external-store-adapter.d.ts +29 -2
  218. package/dist/runtimes/external-store/external-store-adapter.d.ts.map +1 -1
  219. package/dist/runtimes/external-store/external-store-thread-list-runtime-core.d.ts +2 -5
  220. package/dist/runtimes/external-store/external-store-thread-list-runtime-core.d.ts.map +1 -1
  221. package/dist/runtimes/external-store/external-store-thread-list-runtime-core.js +3 -10
  222. package/dist/runtimes/external-store/external-store-thread-list-runtime-core.js.map +1 -1
  223. package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts +21 -2
  224. package/dist/runtimes/external-store/external-store-thread-runtime-core.d.ts.map +1 -1
  225. package/dist/runtimes/external-store/external-store-thread-runtime-core.js +154 -46
  226. package/dist/runtimes/external-store/external-store-thread-runtime-core.js.map +1 -1
  227. package/dist/runtimes/inert/InertThreadRuntimeCore.d.ts +1 -1
  228. package/dist/runtimes/inert/InertThreadRuntimeCore.d.ts.map +1 -1
  229. package/dist/runtimes/inert/InertThreadRuntimeCore.js.map +1 -1
  230. package/dist/runtimes/local/local-thread-runtime-core.d.ts +4 -2
  231. package/dist/runtimes/local/local-thread-runtime-core.d.ts.map +1 -1
  232. package/dist/runtimes/local/local-thread-runtime-core.js +23 -3
  233. package/dist/runtimes/local/local-thread-runtime-core.js.map +1 -1
  234. package/dist/runtimes/remote-thread-list/remote-thread-state.d.ts +12 -1
  235. package/dist/runtimes/remote-thread-list/remote-thread-state.d.ts.map +1 -1
  236. package/dist/runtimes/remote-thread-list/remote-thread-state.js +107 -14
  237. package/dist/runtimes/remote-thread-list/remote-thread-state.js.map +1 -1
  238. package/dist/runtimes/remote-thread-list/title.d.ts +9 -0
  239. package/dist/runtimes/remote-thread-list/title.d.ts.map +1 -0
  240. package/dist/runtimes/remote-thread-list/title.js +6 -0
  241. package/dist/runtimes/remote-thread-list/title.js.map +1 -0
  242. package/dist/runtimes/remote-thread-list/types.d.ts +6 -5
  243. package/dist/runtimes/remote-thread-list/types.d.ts.map +1 -1
  244. package/dist/runtimes/tool-invocations/ToolInvocationTracker.d.ts +31 -8
  245. package/dist/runtimes/tool-invocations/ToolInvocationTracker.d.ts.map +1 -1
  246. package/dist/runtimes/tool-invocations/ToolInvocationTracker.js +98 -59
  247. package/dist/runtimes/tool-invocations/ToolInvocationTracker.js.map +1 -1
  248. package/dist/store/clients/external-thread.d.ts +1 -1
  249. package/dist/store/clients/external-thread.d.ts.map +1 -1
  250. package/dist/store/clients/external-thread.js +85 -103
  251. package/dist/store/clients/external-thread.js.map +1 -1
  252. package/dist/store/clients/single-thread-list.d.ts.map +1 -1
  253. package/dist/store/clients/single-thread-list.js +2 -2
  254. package/dist/store/clients/single-thread-list.js.map +1 -1
  255. package/dist/store/clients/suggestions.d.ts.map +1 -1
  256. package/dist/store/clients/suggestions.js +33 -12
  257. package/dist/store/clients/suggestions.js.map +1 -1
  258. package/dist/store/clients/thread-message-client.js +1 -1
  259. package/dist/store/clients/thread-selection-events.js +1 -1
  260. package/dist/store/index.d.ts +6 -3
  261. package/dist/store/index.js +2 -1
  262. package/dist/store/internal.d.ts +2 -2
  263. package/dist/store/internal.js +2 -2
  264. package/dist/store/primitive-predicates.d.ts +4 -1
  265. package/dist/store/primitive-predicates.d.ts.map +1 -1
  266. package/dist/store/primitive-predicates.js +9 -1
  267. package/dist/store/primitive-predicates.js.map +1 -1
  268. package/dist/store/runtime-clients/composer-runtime-client.js +1 -1
  269. package/dist/store/runtime-clients/message-runtime-client.js +1 -1
  270. package/dist/store/runtime-clients/thread-list-item-runtime-client.js +1 -1
  271. package/dist/store/runtime-clients/thread-list-runtime-client.d.ts.map +1 -1
  272. package/dist/store/runtime-clients/thread-list-runtime-client.js +124 -105
  273. package/dist/store/runtime-clients/thread-list-runtime-client.js.map +1 -1
  274. package/dist/store/runtime-clients/thread-runtime-client.js +1 -1
  275. package/dist/store/runtime-clients/useSubscribable.d.ts +3 -1
  276. package/dist/store/runtime-clients/useSubscribable.d.ts.map +1 -1
  277. package/dist/store/runtime-clients/useSubscribable.js +1 -1
  278. package/dist/store/runtime-clients/useSubscribable.js.map +1 -1
  279. package/dist/store/scope-registration.d.ts +1 -1
  280. package/dist/store/scopes/composer.d.ts +2 -1
  281. package/dist/store/scopes/composer.d.ts.map +1 -1
  282. package/dist/store/scopes/message.d.ts +1 -1
  283. package/dist/store/scopes/part.d.ts +6 -3
  284. package/dist/store/scopes/part.d.ts.map +1 -1
  285. package/dist/store/scopes/queue-item.d.ts +2 -9
  286. package/dist/store/scopes/queue-item.d.ts.map +1 -1
  287. package/dist/store/scopes/queue-item.js +0 -6
  288. package/dist/subscribable/subscribable.d.ts +8 -1
  289. package/dist/subscribable/subscribable.d.ts.map +1 -1
  290. package/dist/subscribable/subscribable.js +24 -16
  291. package/dist/subscribable/subscribable.js.map +1 -1
  292. package/dist/types/message.d.ts +47 -4
  293. package/dist/types/message.d.ts.map +1 -1
  294. package/dist/types/message.js +7 -1
  295. package/dist/types/message.js.map +1 -1
  296. package/dist/utils/data-url.d.ts +14 -1
  297. package/dist/utils/data-url.d.ts.map +1 -1
  298. package/dist/utils/data-url.js +13 -1
  299. package/dist/utils/data-url.js.map +1 -1
  300. package/dist/utils/invoke-user-callback.d.ts +5 -0
  301. package/dist/utils/invoke-user-callback.d.ts.map +1 -0
  302. package/dist/utils/invoke-user-callback.js +19 -0
  303. package/dist/utils/invoke-user-callback.js.map +1 -0
  304. package/dist/utils/normalizePartStatus.js.map +1 -1
  305. package/package.json +12 -17
  306. package/src/index.ts +6 -1
  307. package/src/internal.ts +20 -0
  308. package/src/model-context/frame/host.test.ts +91 -7
  309. package/src/model-context/frame/host.ts +7 -2
  310. package/src/model-context/frame/provider.test.ts +484 -13
  311. package/src/model-context/frame/provider.ts +205 -35
  312. package/src/model-context/interactable-composer-metadata.ts +1 -1
  313. package/src/model-context/registry.test.ts +44 -0
  314. package/src/model-context/registry.ts +13 -35
  315. package/src/model-context/tool-call-text.test.ts +67 -0
  316. package/src/model-context/tool-call-text.ts +67 -0
  317. package/src/react/AssistantRuntimeProvider.test.tsx +66 -2
  318. package/src/react/adapters/LocalStorageThreadListAdapter.tsx +1 -3
  319. package/src/react/client/InMemoryThreadList.test.tsx +49 -0
  320. package/src/react/client/InMemoryThreadList.ts +7 -1
  321. package/src/react/client/Interactables.ts +19 -178
  322. package/src/react/client/RemoteThreadList.background.test.ts +384 -0
  323. package/src/react/client/RemoteThreadList.concurrent.test.tsx +274 -0
  324. package/src/react/client/RemoteThreadList.test.ts +369 -1
  325. package/src/react/client/RemoteThreadList.ts +320 -142
  326. package/src/react/client/Tools.ts +7 -1
  327. package/src/react/index.ts +4 -0
  328. package/src/react/interactables-legacy/Interactables.ts +20 -177
  329. package/src/react/interactables-shared/useInteractablePersistenceQueue.test.tsx +161 -0
  330. package/src/react/interactables-shared/useInteractablePersistenceQueue.ts +206 -0
  331. package/src/react/model-context/modelContextHooks.test.tsx +131 -0
  332. package/src/react/model-context/toolbox.test.ts +9 -9
  333. package/src/react/model-context/toolbox.ts +9 -36
  334. package/src/react/model-context/useAssistantContext.ts +4 -2
  335. package/src/react/model-context/useAuiToolOverrides.ts +4 -2
  336. package/src/react/model-context/useInlineRender.test.tsx +48 -0
  337. package/src/react/model-context/useInlineRender.ts +14 -10
  338. package/src/react/primitive-hooks/useMessageError.ts +2 -19
  339. package/src/react/primitive-hooks/useThreadListLoadMore.ts +2 -3
  340. package/src/react/primitives/composer/ComposerAttachments.test.tsx +76 -0
  341. package/src/react/primitives/composer/ComposerAttachments.tsx +9 -4
  342. package/src/react/primitives/composer/ComposerQueue.test.tsx +72 -0
  343. package/src/react/primitives/composer/ComposerQueue.tsx +7 -4
  344. package/src/react/primitives/message/MessageAttachments.test.tsx +107 -37
  345. package/src/react/primitives/message/MessageAttachments.tsx +10 -7
  346. package/src/react/primitives/message/MessageGroupedParts.tsx +2 -2
  347. package/src/react/primitives/message/MessageParts.tsx +3 -3
  348. package/src/react/primitives/threadList/ThreadListItems.test.tsx +99 -0
  349. package/src/react/primitives/threadList/ThreadListItems.tsx +8 -5
  350. package/src/react/runtimes/RemoteThreadListHookInstanceManager.running.test.tsx +94 -1
  351. package/src/react/runtimes/RemoteThreadListHookInstanceManager.tsx +72 -31
  352. package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.switch-delete.test.ts +169 -0
  353. package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +140 -124
  354. package/src/react/runtimes/RemoteThreadResource.ts +1 -4
  355. package/src/react/runtimes/assistant-transport/runManager.ts +4 -17
  356. package/src/react/runtimes/assistant-transport/transport-scheduling.test.ts +51 -2
  357. package/src/react/runtimes/assistant-transport/useLatestRef.ts +2 -2
  358. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.test.ts +45 -0
  359. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.test.tsx +157 -1
  360. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.ts +82 -176
  361. package/src/react/runtimes/cloud/auiV0.ts +5 -0
  362. package/src/react/runtimes/cloud/createCloudThreadListAdapter.test.ts +88 -0
  363. package/src/react/runtimes/cloud/createCloudThreadListAdapter.ts +236 -0
  364. package/src/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.test.ts +37 -0
  365. package/src/react/runtimes/cloud/createCloudThreadListAdapterCreateFallback.ts +18 -0
  366. package/src/react/runtimes/cloud/useCloudThreadListAdapter.test.tsx +90 -1
  367. package/src/react/runtimes/cloud/useCloudThreadListAdapter.tsx +33 -238
  368. package/src/react/runtimes/external-message-converter.test.ts +0 -14
  369. package/src/react/runtimes/external-message-converter.test.tsx +77 -0
  370. package/src/react/runtimes/external-message-converter.ts +59 -481
  371. package/src/react/runtimes/useRemoteThreadListRuntime.concurrent.test.tsx +216 -0
  372. package/src/react/runtimes/useRemoteThreadListRuntime.ts +24 -6
  373. package/src/react/types/MessagePartComponentTypes.ts +7 -3
  374. package/src/react/types/scopes/tools.ts +2 -0
  375. package/src/runtime/api/composer-runtime.ts +1 -1
  376. package/src/runtime/api/message-part-runtime.ts +3 -3
  377. package/src/runtime/api/message-runtime.test.ts +28 -1
  378. package/src/runtime/api/thread-list-runtime.ts +25 -1
  379. package/src/runtime/base/base-composer-runtime-core.ts +20 -40
  380. package/src/runtime/base/base-thread-runtime-core.test.ts +110 -3
  381. package/src/runtime/base/base-thread-runtime-core.ts +16 -15
  382. package/src/runtime/base/default-thread-composer-runtime-core.ts +1 -4
  383. package/src/runtime/interfaces/composer-runtime-core.ts +1 -1
  384. package/src/runtime/interfaces/thread-list-runtime-core.ts +20 -1
  385. package/src/runtime/interfaces/thread-runtime-core.ts +18 -2
  386. package/src/runtime/internal.ts +5 -0
  387. package/src/runtime/queue/external-thread-queue-adapter.ts +1 -1
  388. package/src/runtime/queue/message-queue.ts +1 -4
  389. package/src/runtime/queue/queue-item.ts +10 -0
  390. package/src/runtime/utils/abortable-thread-load.test.ts +87 -0
  391. package/src/runtime/utils/abortable-thread-load.ts +53 -0
  392. package/src/runtime/utils/attachment-add-operations.ts +59 -0
  393. package/src/runtime/utils/auto-status.test.ts +57 -0
  394. package/src/runtime/utils/auto-status.ts +46 -1
  395. package/src/runtime/utils/external-message-conversion.test.ts +210 -0
  396. package/src/runtime/utils/external-message-conversion.ts +510 -0
  397. package/src/runtime/utils/external-store-message.ts +2 -0
  398. package/src/runtime/utils/message-repository-session.test.ts +195 -0
  399. package/src/runtime/utils/message-repository-session.ts +176 -0
  400. package/src/runtime/utils/message-repository.ts +7 -4
  401. package/src/runtime/utils/pending-tool-calls.test.ts +57 -0
  402. package/src/runtime/utils/pending-tool-calls.ts +35 -0
  403. package/src/runtime/utils/resolveToolApprovalResponse.test.ts +73 -0
  404. package/src/runtime/utils/resolveToolApprovalResponse.ts +27 -6
  405. package/src/runtime/utils/thread-message-like.ts +4 -10
  406. package/src/runtime/utils/tool-call-tree.test.ts +191 -0
  407. package/src/runtime/utils/tool-call-tree.ts +97 -0
  408. package/src/runtimes/external-store/external-store-adapter.ts +38 -2
  409. package/src/runtimes/external-store/external-store-thread-list-runtime-core.ts +6 -14
  410. package/src/runtimes/external-store/external-store-thread-runtime-core.ts +231 -61
  411. package/src/runtimes/inert/InertThreadRuntimeCore.ts +1 -1
  412. package/src/runtimes/local/local-thread-runtime-core.test.ts +201 -4
  413. package/src/runtimes/local/local-thread-runtime-core.ts +53 -4
  414. package/src/runtimes/remote-thread-list/remote-thread-state.test.ts +149 -0
  415. package/src/runtimes/remote-thread-list/remote-thread-state.ts +178 -16
  416. package/src/runtimes/remote-thread-list/title.ts +3 -0
  417. package/src/runtimes/remote-thread-list/types.ts +6 -5
  418. package/src/runtimes/tool-invocations/EDGE_CASES.md +125 -3
  419. package/src/runtimes/tool-invocations/ToolInvocationTracker.test.ts +856 -102
  420. package/src/runtimes/tool-invocations/ToolInvocationTracker.ts +145 -89
  421. package/src/store/clients/external-thread.ts +30 -69
  422. package/src/store/clients/single-thread-list.ts +4 -1
  423. package/src/store/clients/suggestions.test.ts +185 -0
  424. package/src/store/clients/suggestions.ts +42 -35
  425. package/src/store/index.ts +4 -0
  426. package/src/store/internal.ts +2 -0
  427. package/src/store/primitive-predicates.ts +35 -0
  428. package/src/store/runtime-clients/thread-list-runtime-client.ts +16 -2
  429. package/src/store/runtime-clients/useSubscribable.ts +8 -2
  430. package/src/store/scopes/message.ts +1 -1
  431. package/src/store/scopes/part.ts +6 -3
  432. package/src/store/scopes/queue-item.ts +2 -9
  433. package/src/subscribable/subscribable.test.ts +50 -0
  434. package/src/subscribable/subscribable.ts +33 -21
  435. package/src/tests/MessageRepository.test.ts +77 -0
  436. package/src/tests/RemoteThreadListThreadListRuntimeCore-load-race.test.ts +305 -0
  437. package/src/tests/RemoteThreadListThreadListRuntimeCore-loadMore.test.ts +78 -0
  438. package/src/tests/RemoteThreadListThreadListRuntimeCore-reload.test.ts +52 -0
  439. package/src/tests/RemoteThreadListThreadListRuntimeCore-title-race.test.ts +158 -0
  440. package/src/tests/external-store-optimistic-id.test.ts +79 -0
  441. package/src/tests/external-store-repository-instance.test.ts +166 -0
  442. package/src/tests/external-store-thread-runtime-core-adapter.test.ts +186 -16
  443. package/src/tests/external-store-thread-runtime-core.test.ts +642 -0
  444. package/src/tests/external-thread-attachments.test.tsx +258 -1
  445. package/src/tests/remote-thread-list-adapter-switch.test.ts +66 -1
  446. package/src/tests/single-thread-list-item.test.tsx +47 -0
  447. package/src/tests/thread-message-like.test.ts +39 -1
  448. package/src/tests/thread-switch-events.test.tsx +397 -2
  449. package/src/types/message.ts +58 -5
  450. package/src/utils/data-url.test.ts +52 -1
  451. package/src/utils/data-url.ts +21 -0
  452. package/src/utils/invoke-user-callback.test.ts +82 -0
  453. package/src/utils/invoke-user-callback.ts +31 -0
  454. package/src/utils/normalizePartStatus.ts +1 -1
  455. package/dist/store/scopes/queue-item.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { notifySubscribers } from "../../subscribable/subscribable.js";
1
+ import { BaseSubscribable } from "../../subscribable/subscribable.js";
2
2
  import { invalidateThreadRuntime } from "../../runtime/utils/thread-runtime-lifecycle.js";
3
3
  //#region src/runtimes/external-store/external-store-thread-list-runtime-core.ts
4
4
  const EMPTY_ARRAY = Object.freeze([]);
@@ -12,7 +12,7 @@ const DEFAULT_THREAD = Object.freeze({
12
12
  });
13
13
  const RESOLVED_PROMISE = Promise.resolve();
14
14
  const DEFAULT_THREAD_DATA = Object.freeze({ [DEFAULT_THREAD_ID]: DEFAULT_THREAD });
15
- var ExternalStoreThreadListRuntimeCore = class {
15
+ var ExternalStoreThreadListRuntimeCore = class extends BaseSubscribable {
16
16
  _mainThreadId = DEFAULT_THREAD_ID;
17
17
  _threads = DEFAULT_THREADS;
18
18
  _archivedThreads = EMPTY_ARRAY;
@@ -40,6 +40,7 @@ var ExternalStoreThreadListRuntimeCore = class {
40
40
  }
41
41
  threadFactory;
42
42
  constructor(adapter = {}, threadFactory) {
43
+ super();
43
44
  this.threadFactory = threadFactory;
44
45
  this.__internal_setAdapter(adapter, true);
45
46
  }
@@ -145,14 +146,6 @@ var ExternalStoreThreadListRuntimeCore = class {
145
146
  generateTitle() {
146
147
  throw new Error("Method not implemented.");
147
148
  }
148
- _subscriptions = /* @__PURE__ */ new Set();
149
- subscribe(callback) {
150
- this._subscriptions.add(callback);
151
- return () => this._subscriptions.delete(callback);
152
- }
153
- _notifySubscribers() {
154
- notifySubscribers(this._subscriptions);
155
- }
156
149
  };
157
150
  //#endregion
158
151
  export { ExternalStoreThreadListRuntimeCore };
@@ -1 +1 @@
1
- {"version":3,"file":"external-store-thread-list-runtime-core.js","names":["Unsubscribe","ExternalStoreThreadRuntimeCore","ThreadListItemCoreState","ThreadListRuntimeCore","ExternalStoreThreadListAdapter","invalidateThreadRuntime","notifySubscribers","ExternalStoreThreadFactory","EMPTY_ARRAY","Object","freeze","DEFAULT_THREAD_ID","DEFAULT_THREADS","DEFAULT_THREAD","id","remoteId","undefined","externalId","status","RESOLVED_PROMISE","Promise","resolve","DEFAULT_THREAD_DATA","ExternalStoreThreadListRuntimeCore","_mainThreadId","_threads","_archivedThreads","_threadData","Readonly","Record","adapter","isLoading","newThreadId","threadIds","archivedThreadIds","threadItems","getLoadThreadsPromise","_mainThread","mainThreadId","threadFactory","constructor","__internal_setAdapter","getMainThreadRuntimeCore","getThreadRuntimeCore","Error","getItemById","threadId","initialLoad","previousAdapter","newThreads","threads","newArchivedThreads","archivedThreads","previousThreadId","previousThreads","previousArchivedThreads","fromEntries","map","t","_notifySubscribers","reloadMainThread","unstable_refetchThread","switchToThread","_options","unarchive","onSwitchToThread","switchToNewThread","onSwitchToNewThread","rename","newTitle","onRename","updateCustom","custom","onUpdateCustom","detach","archive","onArchive","onUnarchive","delete","onDelete","initialize","generateTitle","_subscriptions","Set","subscribe","callback","add"],"sources":["../../../src/runtimes/external-store/external-store-thread-list-runtime-core.ts"],"sourcesContent":["import type { Unsubscribe } from \"../../types/unsubscribe\";\nimport type { ExternalStoreThreadRuntimeCore } from \"./external-store-thread-runtime-core\";\nimport type {\n ThreadListItemCoreState,\n ThreadListRuntimeCore,\n} from \"../../runtime/interfaces/thread-list-runtime-core\";\nimport type { ExternalStoreThreadListAdapter } from \"./external-store-adapter\";\nimport { invalidateThreadRuntime } from \"../../runtime/utils/thread-runtime-lifecycle\";\nimport { notifySubscribers } from \"../../subscribable/subscribable\";\n\nexport type ExternalStoreThreadFactory = () => ExternalStoreThreadRuntimeCore;\n\nconst EMPTY_ARRAY = Object.freeze([]);\nconst DEFAULT_THREAD_ID = \"DEFAULT_THREAD_ID\";\nconst DEFAULT_THREADS = Object.freeze([DEFAULT_THREAD_ID]);\nconst DEFAULT_THREAD = Object.freeze({\n id: DEFAULT_THREAD_ID,\n remoteId: undefined,\n externalId: undefined,\n status: \"regular\",\n});\nconst RESOLVED_PROMISE = Promise.resolve();\nconst DEFAULT_THREAD_DATA = Object.freeze({\n [DEFAULT_THREAD_ID]: DEFAULT_THREAD,\n});\n\nexport class ExternalStoreThreadListRuntimeCore implements ThreadListRuntimeCore {\n private _mainThreadId: string = DEFAULT_THREAD_ID;\n private _threads: readonly string[] = DEFAULT_THREADS;\n private _archivedThreads: readonly string[] = EMPTY_ARRAY;\n private _threadData: Readonly<Record<string, ThreadListItemCoreState>> =\n DEFAULT_THREAD_DATA;\n private adapter: ExternalStoreThreadListAdapter = {};\n\n public get isLoading() {\n return this.adapter.isLoading ?? false;\n }\n\n public get newThreadId() {\n return undefined;\n }\n\n public get threadIds() {\n return this._threads;\n }\n\n public get archivedThreadIds() {\n return this._archivedThreads;\n }\n\n public get threadItems() {\n return this._threadData;\n }\n\n public getLoadThreadsPromise() {\n return RESOLVED_PROMISE;\n }\n\n private _mainThread!: ExternalStoreThreadRuntimeCore;\n\n public get mainThreadId() {\n return this._mainThreadId;\n }\n\n private threadFactory: ExternalStoreThreadFactory;\n\n constructor(\n adapter: ExternalStoreThreadListAdapter = {},\n threadFactory: ExternalStoreThreadFactory,\n ) {\n this.threadFactory = threadFactory;\n this.__internal_setAdapter(adapter, true);\n }\n\n public getMainThreadRuntimeCore() {\n return this._mainThread;\n }\n\n public getThreadRuntimeCore(): never {\n throw new Error(\"Method not implemented.\");\n }\n\n public getItemById(threadId: string) {\n return this._threadData[threadId];\n }\n\n public __internal_setAdapter(\n adapter: ExternalStoreThreadListAdapter,\n initialLoad = false,\n ) {\n const previousAdapter = this.adapter;\n this.adapter = adapter;\n\n const newThreadId = adapter.threadId ?? DEFAULT_THREAD_ID;\n const newThreads = adapter.threads ?? EMPTY_ARRAY;\n const newArchivedThreads = adapter.archivedThreads ?? EMPTY_ARRAY;\n\n const previousThreadId = previousAdapter.threadId ?? DEFAULT_THREAD_ID;\n const previousThreads = previousAdapter.threads ?? EMPTY_ARRAY;\n const previousArchivedThreads =\n previousAdapter.archivedThreads ?? EMPTY_ARRAY;\n\n if (\n !initialLoad &&\n previousThreadId === newThreadId &&\n previousThreads === newThreads &&\n previousArchivedThreads === newArchivedThreads\n ) {\n return;\n }\n\n if (\n previousThreads !== newThreads ||\n previousArchivedThreads !== newArchivedThreads ||\n previousThreadId !== newThreadId\n ) {\n this._threadData = {\n ...DEFAULT_THREAD_DATA,\n ...Object.fromEntries(\n adapter.threads?.map((t) => [\n t.id,\n {\n ...t,\n remoteId: t.remoteId,\n externalId: t.externalId,\n status: \"regular\",\n },\n ]) ?? [],\n ),\n ...Object.fromEntries(\n adapter.archivedThreads?.map((t) => [\n t.id,\n {\n ...t,\n remoteId: t.remoteId,\n externalId: t.externalId,\n status: \"archived\",\n },\n ]) ?? [],\n ),\n };\n }\n\n if (previousThreads !== newThreads) {\n this._threads = this.adapter.threads?.map((t) => t.id) ?? EMPTY_ARRAY;\n }\n\n if (previousArchivedThreads !== newArchivedThreads) {\n this._archivedThreads =\n this.adapter.archivedThreads?.map((t) => t.id) ?? EMPTY_ARRAY;\n }\n\n // `initialLoad ||`: `_mainThread!` must be assigned on construction.\n if (initialLoad || previousThreadId !== newThreadId) {\n if (!initialLoad) invalidateThreadRuntime(this._mainThread);\n this._mainThreadId = newThreadId;\n this._mainThread = this.threadFactory();\n }\n\n if (!this._threadData[this._mainThreadId]) {\n this._threadData = {\n ...this._threadData,\n [this._mainThreadId]: {\n id: this._mainThreadId,\n remoteId: undefined,\n externalId: undefined,\n status: \"regular\",\n },\n };\n }\n\n this._notifySubscribers();\n }\n\n public async reloadMainThread(): Promise<void> {\n // There is no runtime hook to remount here, so the capability is the only\n // path and an adapter without it has nothing to refetch with.\n if (!this._mainThread.unstable_refetchThread) return;\n // No unsent-thread guard: every entry here is regular or archived, so the\n // \"new\" status the remote thread list has to exclude cannot occur.\n await this._mainThread.unstable_refetchThread();\n }\n\n public async switchToThread(\n threadId: string,\n _options?: { unarchive?: boolean },\n ): Promise<void> {\n if (this._mainThreadId === threadId) return;\n const onSwitchToThread = this.adapter.onSwitchToThread;\n if (!onSwitchToThread)\n throw new Error(\n \"External store adapter does not support switching to thread\",\n );\n await onSwitchToThread(threadId);\n }\n\n public async switchToNewThread(): Promise<void> {\n const onSwitchToNewThread = this.adapter.onSwitchToNewThread;\n if (!onSwitchToNewThread)\n throw new Error(\n \"External store adapter does not support switching to new thread\",\n );\n\n await onSwitchToNewThread();\n }\n\n public async rename(threadId: string, newTitle: string): Promise<void> {\n const onRename = this.adapter.onRename;\n if (!onRename)\n throw new Error(\"External store adapter does not support renaming\");\n\n await onRename(threadId, newTitle);\n }\n\n public async updateCustom(\n threadId: string,\n custom: Record<string, unknown> | undefined,\n ): Promise<void> {\n const onUpdateCustom = this.adapter.onUpdateCustom;\n if (!onUpdateCustom)\n throw new Error(\n \"External store adapter does not support updating custom metadata\",\n );\n\n await onUpdateCustom(threadId, custom);\n }\n\n public async detach(): Promise<void> {\n // no-op\n }\n\n public async archive(threadId: string): Promise<void> {\n const onArchive = this.adapter.onArchive;\n if (!onArchive)\n throw new Error(\"External store adapter does not support archiving\");\n\n await onArchive(threadId);\n }\n\n public async unarchive(threadId: string): Promise<void> {\n const onUnarchive = this.adapter.onUnarchive;\n if (!onUnarchive)\n throw new Error(\"External store adapter does not support unarchiving\");\n\n await onUnarchive(threadId);\n }\n\n public async delete(threadId: string): Promise<void> {\n const onDelete = this.adapter.onDelete;\n if (!onDelete)\n throw new Error(\"External store adapter does not support deleting\");\n\n await onDelete(threadId);\n }\n\n public initialize(\n threadId: string,\n ): Promise<{ remoteId: string; externalId: string | undefined }> {\n return Promise.resolve({ remoteId: threadId, externalId: undefined });\n }\n\n public generateTitle(): never {\n throw new Error(\"Method not implemented.\");\n }\n\n private _subscriptions = new Set<() => void>();\n\n public subscribe(callback: () => void): Unsubscribe {\n this._subscriptions.add(callback);\n return () => this._subscriptions.delete(callback);\n }\n\n private _notifySubscribers() {\n notifySubscribers(this._subscriptions);\n }\n}\n"],"mappings":";;;AAYA,MAAMQ,cAAcC,OAAOC,OAAO,CAAA,CAAE;AACpC,MAAMC,oBAAoB;AAC1B,MAAMC,kBAAkBH,OAAOC,OAAO,CAACC,iBAAiB,CAAC;AACzD,MAAME,iBAAiBJ,OAAOC,OAAO;CACnCI,IAAIH;CACJI,UAAUC,KAAAA;CACVC,YAAYD,KAAAA;CACZE,QAAQ;AACV,CAAC;AACD,MAAMC,mBAAmBC,QAAQC,QAAQ;AACzC,MAAMC,sBAAsBb,OAAOC,OAAO,GACvCC,oBAAoBE,eACvB,CAAC;AAED,IAAaU,qCAAb,MAAiF;CAC/E,gBAAgCZ;CAChC,WAAsCC;CACtC,mBAA8CJ;CAC9C,cACEc;CACF,UAAkD,CAAC;CAEnD,IAAWS,YAAY;EACrB,OAAO,KAAKD,QAAQC,aAAa;CACnC;CAEA,IAAWC,cAAc,CAEzB;CAEA,IAAWC,YAAY;EACrB,OAAO,KAAKR;CACd;CAEA,IAAWS,oBAAoB;EAC7B,OAAO,KAAKR;CACd;CAEA,IAAWS,cAAc;EACvB,OAAO,KAAKR;CACd;C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BAAiB,KAAK3C,QAAQ2C;EACpC,IAAI,CAACA,gBACH,MAAM,IAAI7B,MACR,kEACF;EAEF,MAAM6B,eAAe3B,UAAU0B,MAAM;CACvC;CAEA,MAAaE,SAAwB,CACnC;CAGF,MAAaC,QAAQ7B,UAAiC;EACpD,MAAM8B,YAAY,KAAK9C,QAAQ8C;EAC/B,IAAI,CAACA,WACH,MAAM,IAAIhC,MAAM,mDAAmD;EAErE,MAAMgC,UAAU9B,QAAQ;CAC1B;CAEA,MAAakB,UAAUlB,UAAiC;EACtD,MAAM+B,cAAc,KAAK/C,QAAQ+C;EACjC,IAAI,CAACA,aACH,MAAM,IAAIjC,MAAM,qDAAqD;EAEvE,MAAMiC,YAAY/B,QAAQ;CAC5B;CAEA,MAAagC,OAAOhC,UAAiC;EACnD,MAAMiC,WAAW,KAAKjD,QAAQiD;EAC9B,IAAI,CAACA,UACH,MAAM,IAAInC,MAAM,kDAAkD;EAEpE,MAAMmC,SAASjC,QAAQ;CACzB;CAEA,WACEA,UAC+D;EAC/D,OAAO1B,QAAQC,QAAQ;GAAEN,UAAU+B;GAAU7B,YAAYD,KAAAA;EAAU,CAAC;CACtE;CAEA,gBAA8B;EAC5B,MAAM,IAAI4B,MAAM,yBAAyB;CAC3C;CAEA,iCAAyB,IAAIuC,IAAgB;CAE7C,UAAiBE,UAAmC;EAClD,KAAKH,eAAeI,IAAID,QAAQ;EAChC,aAAa,KAAKH,eAAeJ,OAAOO,QAAQ;CAClD;CAEA,qBAA6B;EAC3B/E,kBAAkB,KAAK4E,cAAc;CACvC;AACF"}
1
+ {"version":3,"file":"external-store-thread-list-runtime-core.js","names":["ExternalStoreThreadRuntimeCore","ThreadListItemCoreState","ThreadListRuntimeCore","ExternalStoreThreadListAdapter","invalidateThreadRuntime","BaseSubscribable","ExternalStoreThreadFactory","EMPTY_ARRAY","Object","freeze","DEFAULT_THREAD_ID","DEFAULT_THREADS","DEFAULT_THREAD","id","remoteId","undefined","externalId","status","RESOLVED_PROMISE","Promise","resolve","DEFAULT_THREAD_DATA","ExternalStoreThreadListRuntimeCore","_mainThreadId","_threads","_archivedThreads","_threadData","Readonly","Record","adapter","isLoading","newThreadId","threadIds","archivedThreadIds","threadItems","getLoadThreadsPromise","_mainThread","mainThreadId","threadFactory","constructor","__internal_setAdapter","getMainThreadRuntimeCore","getThreadRuntimeCore","Error","getItemById","threadId","initialLoad","previousAdapter","newThreads","threads","newArchivedThreads","archivedThreads","previousThreadId","previousThreads","previousArchivedThreads","fromEntries","map","t","_notifySubscribers","reloadMainThread","unstable_refetchThread","switchToThread","_options","unarchive","onSwitchToThread","switchToNewThread","onSwitchToNewThread","rename","newTitle","onRename","updateCustom","custom","onUpdateCustom","detach","archive","onArchive","onUnarchive","delete","onDelete","initialize","generateTitle"],"sources":["../../../src/runtimes/external-store/external-store-thread-list-runtime-core.ts"],"sourcesContent":["import type { ExternalStoreThreadRuntimeCore } from \"./external-store-thread-runtime-core\";\nimport type {\n ThreadListItemCoreState,\n ThreadListRuntimeCore,\n} from \"../../runtime/interfaces/thread-list-runtime-core\";\nimport type { ExternalStoreThreadListAdapter } from \"./external-store-adapter\";\nimport { invalidateThreadRuntime } from \"../../runtime/utils/thread-runtime-lifecycle\";\nimport { BaseSubscribable } from \"../../subscribable/subscribable\";\n\nexport type ExternalStoreThreadFactory = () => ExternalStoreThreadRuntimeCore;\n\nconst EMPTY_ARRAY = Object.freeze([]);\nconst DEFAULT_THREAD_ID = \"DEFAULT_THREAD_ID\";\nconst DEFAULT_THREADS = Object.freeze([DEFAULT_THREAD_ID]);\nconst DEFAULT_THREAD = Object.freeze({\n id: DEFAULT_THREAD_ID,\n remoteId: undefined,\n externalId: undefined,\n status: \"regular\",\n});\nconst RESOLVED_PROMISE = Promise.resolve();\nconst DEFAULT_THREAD_DATA = Object.freeze({\n [DEFAULT_THREAD_ID]: DEFAULT_THREAD,\n});\n\nexport class ExternalStoreThreadListRuntimeCore\n extends BaseSubscribable\n implements ThreadListRuntimeCore\n{\n private _mainThreadId: string = DEFAULT_THREAD_ID;\n private _threads: readonly string[] = DEFAULT_THREADS;\n private _archivedThreads: readonly string[] = EMPTY_ARRAY;\n private _threadData: Readonly<Record<string, ThreadListItemCoreState>> =\n DEFAULT_THREAD_DATA;\n private adapter: ExternalStoreThreadListAdapter = {};\n\n public get isLoading() {\n return this.adapter.isLoading ?? false;\n }\n\n public get newThreadId() {\n return undefined;\n }\n\n public get threadIds() {\n return this._threads;\n }\n\n public get archivedThreadIds() {\n return this._archivedThreads;\n }\n\n public get threadItems() {\n return this._threadData;\n }\n\n public getLoadThreadsPromise() {\n return RESOLVED_PROMISE;\n }\n\n private _mainThread!: ExternalStoreThreadRuntimeCore;\n\n public get mainThreadId() {\n return this._mainThreadId;\n }\n\n private threadFactory: ExternalStoreThreadFactory;\n\n constructor(\n adapter: ExternalStoreThreadListAdapter = {},\n threadFactory: ExternalStoreThreadFactory,\n ) {\n super();\n this.threadFactory = threadFactory;\n this.__internal_setAdapter(adapter, true);\n }\n\n public getMainThreadRuntimeCore() {\n return this._mainThread;\n }\n\n public getThreadRuntimeCore(): never {\n throw new Error(\"Method not implemented.\");\n }\n\n public getItemById(threadId: string) {\n return this._threadData[threadId];\n }\n\n public __internal_setAdapter(\n adapter: ExternalStoreThreadListAdapter,\n initialLoad = false,\n ) {\n const previousAdapter = this.adapter;\n this.adapter = adapter;\n\n const newThreadId = adapter.threadId ?? DEFAULT_THREAD_ID;\n const newThreads = adapter.threads ?? EMPTY_ARRAY;\n const newArchivedThreads = adapter.archivedThreads ?? EMPTY_ARRAY;\n\n const previousThreadId = previousAdapter.threadId ?? DEFAULT_THREAD_ID;\n const previousThreads = previousAdapter.threads ?? EMPTY_ARRAY;\n const previousArchivedThreads =\n previousAdapter.archivedThreads ?? EMPTY_ARRAY;\n\n if (\n !initialLoad &&\n previousThreadId === newThreadId &&\n previousThreads === newThreads &&\n previousArchivedThreads === newArchivedThreads\n ) {\n return;\n }\n\n if (\n previousThreads !== newThreads ||\n previousArchivedThreads !== newArchivedThreads ||\n previousThreadId !== newThreadId\n ) {\n this._threadData = {\n ...DEFAULT_THREAD_DATA,\n ...Object.fromEntries(\n adapter.threads?.map((t) => [\n t.id,\n {\n ...t,\n remoteId: t.remoteId,\n externalId: t.externalId,\n status: \"regular\",\n },\n ]) ?? [],\n ),\n ...Object.fromEntries(\n adapter.archivedThreads?.map((t) => [\n t.id,\n {\n ...t,\n remoteId: t.remoteId,\n externalId: t.externalId,\n status: \"archived\",\n },\n ]) ?? [],\n ),\n };\n }\n\n if (previousThreads !== newThreads) {\n this._threads = this.adapter.threads?.map((t) => t.id) ?? EMPTY_ARRAY;\n }\n\n if (previousArchivedThreads !== newArchivedThreads) {\n this._archivedThreads =\n this.adapter.archivedThreads?.map((t) => t.id) ?? EMPTY_ARRAY;\n }\n\n // `initialLoad ||`: `_mainThread!` must be assigned on construction.\n if (initialLoad || previousThreadId !== newThreadId) {\n if (!initialLoad) invalidateThreadRuntime(this._mainThread);\n this._mainThreadId = newThreadId;\n this._mainThread = this.threadFactory();\n }\n\n if (!this._threadData[this._mainThreadId]) {\n this._threadData = {\n ...this._threadData,\n [this._mainThreadId]: {\n id: this._mainThreadId,\n remoteId: undefined,\n externalId: undefined,\n status: \"regular\",\n },\n };\n }\n\n this._notifySubscribers();\n }\n\n public async reloadMainThread(): Promise<void> {\n // There is no runtime hook to remount here, so the capability is the only\n // path and an adapter without it has nothing to refetch with.\n if (!this._mainThread.unstable_refetchThread) return;\n // No unsent-thread guard: every entry here is regular or archived, so the\n // \"new\" status the remote thread list has to exclude cannot occur.\n await this._mainThread.unstable_refetchThread();\n }\n\n public async switchToThread(\n threadId: string,\n _options?: { unarchive?: boolean },\n ): Promise<void> {\n if (this._mainThreadId === threadId) return;\n const onSwitchToThread = this.adapter.onSwitchToThread;\n if (!onSwitchToThread)\n throw new Error(\n \"External store adapter does not support switching to thread\",\n );\n await onSwitchToThread(threadId);\n }\n\n public async switchToNewThread(): Promise<void> {\n const onSwitchToNewThread = this.adapter.onSwitchToNewThread;\n if (!onSwitchToNewThread)\n throw new Error(\n \"External store adapter does not support switching to new thread\",\n );\n\n await onSwitchToNewThread();\n }\n\n public async rename(threadId: string, newTitle: string): Promise<void> {\n const onRename = this.adapter.onRename;\n if (!onRename)\n throw new Error(\"External store adapter does not support renaming\");\n\n await onRename(threadId, newTitle);\n }\n\n public async updateCustom(\n threadId: string,\n custom: Record<string, unknown> | undefined,\n ): Promise<void> {\n const onUpdateCustom = this.adapter.onUpdateCustom;\n if (!onUpdateCustom)\n throw new Error(\n \"External store adapter does not support updating custom metadata\",\n );\n\n await onUpdateCustom(threadId, custom);\n }\n\n public async detach(): Promise<void> {\n // no-op\n }\n\n public async archive(threadId: string): Promise<void> {\n const onArchive = this.adapter.onArchive;\n if (!onArchive)\n throw new Error(\"External store adapter does not support archiving\");\n\n await onArchive(threadId);\n }\n\n public async unarchive(threadId: string): Promise<void> {\n const onUnarchive = this.adapter.onUnarchive;\n if (!onUnarchive)\n throw new Error(\"External store adapter does not support unarchiving\");\n\n await onUnarchive(threadId);\n }\n\n public async delete(threadId: string): Promise<void> {\n const onDelete = this.adapter.onDelete;\n if (!onDelete)\n throw new Error(\"External store adapter does not support deleting\");\n\n await onDelete(threadId);\n }\n\n public initialize(\n threadId: string,\n ): Promise<{ remoteId: string; externalId: string | undefined }> {\n return Promise.resolve({ remoteId: threadId, externalId: undefined });\n }\n\n public generateTitle(): never {\n throw new Error(\"Method not implemented.\");\n }\n}\n"],"mappings":";;;AAWA,MAAMO,cAAcC,OAAOC,OAAO,CAAA,CAAE;AACpC,MAAMC,oBAAoB;AAC1B,MAAMC,kBAAkBH,OAAOC,OAAO,CAACC,iBAAiB,CAAC;AACzD,MAAME,iBAAiBJ,OAAOC,OAAO;CACnCI,IAAIH;CACJI,UAAUC,KAAAA;CACVC,YAAYD,KAAAA;CACZE,QAAQ;AACV,CAAC;AACD,MAAMC,mBAAmBC,QAAQC,QAAQ;AACzC,MAAMC,sBAAsBb,OAAOC,OA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ider } from "../../model-context/types.js";
7
7
  import { ThreadMessageLike } from "../../runtime/utils/thread-message-like.js";
8
8
  import { ExportedMessageRepository } from "../../runtime/utils/message-repository.js";
9
+ import { QueueItemState } from "../../runtime/queue/queue-item.js";
9
10
  import { QueuePlacement } from "../../runtime/queue/external-thread-queue-adapter.js";
10
- import { QueueItemState } from "../../store/scopes/queue-item.js";
11
11
  import { AddToolResultOptions, RespondToToolApprovalOptions, ResumeRunConfig, ResumeToolCallOptions, RuntimeCapabilities, StartRunConfig, ThreadRuntimeCore, ThreadSuggestion } from "../../runtime/interfaces/thread-runtime-core.js";
12
12
  import { BaseThreadRuntimeCore } from "../../runtime/base/base-thread-runtime-core.js";
13
13
  import { ExternalStoreAdapter, ExternalStoreThreadListAdapter } from "./external-store-adapter.js";
@@ -37,6 +37,8 @@ declare class ExternalStoreThreadRuntimeCore extends BaseThreadRuntimeCore imple
37
37
  suggestions: readonly ThreadSuggestion[];
38
38
  extras: unknown;
39
39
  private _converter;
40
+ private _pendingDeleteEvictions;
41
+ private _optimistic;
40
42
  private _store;
41
43
  private _getInitializePromise?;
42
44
  __internal_setGetInitializePromise(getPromise: () => Promise<unknown> | undefined): void;
@@ -46,9 +48,25 @@ declare class ExternalStoreThreadRuntimeCore extends BaseThreadRuntimeCore imple
46
48
  * snapshot — only when `adapter.unstable_enableToolInvocations === true`.
47
49
  */
48
50
  private _toolInvocations;
51
+ private _toolStatuses;
52
+ private _effectiveIsRunning;
53
+ private _inTrackerUpdate;
54
+ private _pendingRunningRefresh;
55
+ /**
56
+ * Tracker mutations initiated by this class (setState, reset) can publish
57
+ * status changes synchronously. Re-entering the snapshot pipeline from
58
+ * inside them would feed the tracker a stale snapshot and consume the
59
+ * restore arming a reset just installed, so the running refresh is
60
+ * deferred until the mutation returns and stays off the tracker.
61
+ */
62
+ private _runTrackerUpdate;
63
+ private _refreshEffectiveIsRunning;
64
+ private _hasExecutingTools;
65
+ private _getEffectiveIsRunning;
49
66
  beginEdit(messageId: string): void;
50
67
  constructor(contextProvider: ModelContextProvider, store: ExternalStoreAdapter<any>);
51
68
  __internal_setAdapter(store: ExternalStoreAdapter<any>): void;
69
+ private _updateStoreSnapshot;
52
70
  /**
53
71
  * Feed the current message snapshot into the tool-invocations tracker.
54
72
  * Opt-in via `adapter.unstable_enableToolInvocations: true`. The tracker
@@ -81,6 +99,7 @@ declare class ExternalStoreThreadRuntimeCore extends BaseThreadRuntimeCore imple
81
99
  private _notifyBranchChange;
82
100
  append(rawMessage: AppendMessage): Promise<void>;
83
101
  deleteMessage(messageId: string): Promise<void>;
102
+ private _evictDeletedMessage;
84
103
  getQueueItems(): readonly QueueItemState[];
85
104
  getSteerQueueItems(): readonly QueueItemState[];
86
105
  moveQueueItem(queueItemId: string, placement: QueuePlacement): void;
@@ -99,7 +118,7 @@ declare class ExternalStoreThreadRuntimeCore extends BaseThreadRuntimeCore imple
99
118
  private dropEmptyOptimisticHead;
100
119
  addToolResult(options: AddToolResultOptions): void;
101
120
  resumeToolCall(options: ResumeToolCallOptions): void;
102
- respondToToolApproval(options: RespondToToolApprovalOptions): void;
121
+ respondToToolApproval(options: RespondToToolApprovalOptions): Promise<void>;
103
122
  reset(initialMessages?: readonly ThreadMessageLike[]): void;
104
123
  import(data: ExportedMessageRepository): void;
105
124
  private updateMessages;
@@ -1 +1 @@
1
- {"version":3,"file":"external-store-thread-runtime-core.d.ts","names":[],"sources":["../../../src/runtimes/external-store/external-store-thread-runtime-core.ts"],"mappings":";;;;;;;;;;;;;;;;cAwEa,qBACX,oBACA,mBAAmB;cAKR,uCACH,iCACG;UAEH;MAiBG,gBAAY;UAIf;EACD;EACA;MACI;MAIA;YAIQ,6BAA6B;MAI5B,sEAAK,sDAAA;MAId;kBARqC;;;;;;;MAarC,iCAAiC;EAKrC,sBAAsB;EACtB;UAEC;UAEA;UAEA;EAED,mCACL,kBAAkB;UAKZ;;;;;UAMA;EAEQ,UAAU;EAQxB,YAAA,iBAAiB,sBACjB,OAAO;EAMF,sBAAsB,OAAO;;;;;;;UA2M5B;;;;;UAsEA;UACA;;;;;;;UAQA;EAmBQ,eAAe;;;;;;;;;UA6BvB;EAaK,OAAO,YAAY,gBAAgB;EAoEnC,cAAc,oBAAoB;EAoBxC,0BApBwC;EA0BxC,+BANa;EAUb,cAAc,qBAAqB,WAAW;EAI9C,gBAAgB;EAIV,SAAS,QAAQ,iBAAiB;EAYlC,UAAU,QAAQ,kBAAkB;EAO1C;EAOA,oBAAoB;;;;;;EAqBpB;EAKA;UAmFC;EAOD,cAAc,SAAS;EASvB,eAAe,SAAS;EAoBxB,sBAAsB,SAAS;EAStB,MAAM,2BAA2B;EAMjC,OAAO,MAAM;UAQrB"}
1
+ {"version":3,"file":"external-store-thread-runtime-core.d.ts","names":[],"sources":["../../../src/runtimes/external-store/external-store-thread-runtime-core.ts"],"mappings":";;;;;;;;;;;;;;;;cAyEa,qBACX,oBACA,mBAAmB;cAKR,uCACH,iCACG;UAEH;MAiBG,gBAAY;UAIf;EACD;EACA;MACI;MAIA;YAKQ,6BAA6B;MAI5B,sEAAK,sDAAA;MAId;kBARqC;;;;;;;MAarC,iCAAiC;EAKrC,sBAAsB;EACtB;UAEC;UAUA;UAKA;UAEA;UAEA;EAED,mCACL,kBAAkB;UAKZ;;;;;UAMA;UACA;UACA;UACA;UACA;;;;;;;;UASA;UAaA;UAQA;UASA;EAIQ,UAAU;EAQxB,YAAA,iBAAiB,sBACjB,OAAO;EAMF,sBAAsB,OAAO;UAM5B;;;;;;;UA4PA;;;;;UAoFA;UACA;;;;;;;UAQA;EAWQ,eAAe;;;;;;;;;UA8BvB;EAaK,OAAO,YAAY,gBAAgB;EAqEnC,cAAc,oBAAoB;UA0CvC;EAkBD,0BA5DwC;EAkExC,+BANa;EAUb,cAAc,qBAAqB,WAAW;EAI9C,gBAAgB;EAIV,SAAS,QAAQ,iBAAiB;EAclC,UAAU,QAAQ,kBAAkB;EAO1C;EAOA,oBAAoB;;;;;;EAqBpB;EAKA;UAmFC;EAOD,cAAc,SAAS;EASvB,eAAe,SAAS;EAoBxB,sBACL,SAAS,+BACR;EAUa,MAAM,2BAA2B;EAMjC,OAAO,MAAM;UAQrB"}
@@ -1,14 +1,17 @@
1
1
  import { generateId } from "../../utils/id.js";
2
2
  import { getThreadMessageText } from "../../utils/text.js";
3
3
  import { fromThreadMessageLike } from "../../runtime/utils/thread-message-like.js";
4
- import { bindExternalStoreMessage, getExternalStoreMessages } from "../../runtime/utils/external-store-message.js";
5
- import { getAutoStatus, isAutoStatus } from "../../runtime/utils/auto-status.js";
4
+ import { FALLBACK_ID_PREFIX, bindExternalStoreMessage, getExternalStoreMessages } from "../../runtime/utils/external-store-message.js";
5
+ import { getContentAutoStatus, isAutoStatus } from "../../runtime/utils/auto-status.js";
6
6
  import { ExportedMessageRepository, MessageRepository } from "../../runtime/utils/message-repository.js";
7
- import { EMPTY_QUEUE_ITEMS } from "../../store/scopes/queue-item.js";
7
+ import { EMPTY_QUEUE_ITEMS } from "../../runtime/queue/queue-item.js";
8
+ import { walkToolCallTree } from "../../runtime/utils/tool-call-tree.js";
8
9
  import { BaseThreadRuntimeCore } from "../../runtime/base/base-thread-runtime-core.js";
9
10
  import { captureThreadRuntimeGeneration, isThreadRuntimeGenerationCurrent } from "../../runtime/utils/thread-runtime-lifecycle.js";
10
11
  import { ThreadMessageConverter } from "./thread-message-converter.js";
12
+ import { shallowArrayEqual } from "../../runtime/utils/external-message-conversion.js";
11
13
  import { ToolInvocationTracker } from "../tool-invocations/ToolInvocationTracker.js";
14
+ import { shallowEqual } from "@assistant-ui/store/client";
12
15
  //#region src/runtimes/external-store/external-store-thread-runtime-core.ts
13
16
  const EMPTY_ARRAY = Object.freeze([]);
14
17
  const observeAdapterCallback = (name, result) => {
@@ -16,12 +19,6 @@ const observeAdapterCallback = (name, result) => {
16
19
  console.error(`[ExternalStoreThreadRuntimeCore] ${name} callback rejected`, error);
17
20
  });
18
21
  };
19
- const shallowEqual = (a, b) => {
20
- const aKeys = Object.keys(a);
21
- if (aKeys.length !== Object.keys(b).length) return false;
22
- for (const key of aKeys) if (a[key] !== b[key]) return false;
23
- return true;
24
- };
25
22
  const hasUpcomingMessage = (isRunning, messages) => {
26
23
  return isRunning && messages[messages.length - 1]?.role !== "assistant";
27
24
  };
@@ -52,6 +49,7 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
52
49
  return this._store.isLoading ?? false;
53
50
  }
54
51
  get isRunning() {
52
+ if (this._hasExecutingTools(this._store)) return true;
55
53
  return this._store.isRunning;
56
54
  }
57
55
  _getBaseMessages() {
@@ -70,6 +68,8 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
70
68
  suggestions = [];
71
69
  extras = void 0;
72
70
  _converter = new ThreadMessageConverter();
71
+ _pendingDeleteEvictions = /* @__PURE__ */ new Set();
72
+ _optimistic = null;
73
73
  _store;
74
74
  _getInitializePromise;
75
75
  __internal_setGetInitializePromise(getPromise) {
@@ -81,6 +81,45 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
81
81
  * snapshot — only when `adapter.unstable_enableToolInvocations === true`.
82
82
  */
83
83
  _toolInvocations = null;
84
+ _toolStatuses = /* @__PURE__ */ new Map();
85
+ _effectiveIsRunning = false;
86
+ _inTrackerUpdate = false;
87
+ _pendingRunningRefresh = false;
88
+ /**
89
+ * Tracker mutations initiated by this class (setState, reset) can publish
90
+ * status changes synchronously. Re-entering the snapshot pipeline from
91
+ * inside them would feed the tracker a stale snapshot and consume the
92
+ * restore arming a reset just installed, so the running refresh is
93
+ * deferred until the mutation returns and stays off the tracker.
94
+ */
95
+ _runTrackerUpdate(fn) {
96
+ this._inTrackerUpdate = true;
97
+ try {
98
+ fn();
99
+ } finally {
100
+ this._inTrackerUpdate = false;
101
+ }
102
+ if (this._pendingRunningRefresh) {
103
+ this._pendingRunningRefresh = false;
104
+ this._refreshEffectiveIsRunning();
105
+ }
106
+ }
107
+ _refreshEffectiveIsRunning() {
108
+ const isRunning = this._getEffectiveIsRunning(this._store);
109
+ if (this._effectiveIsRunning === isRunning) return;
110
+ this._effectiveIsRunning = isRunning;
111
+ this._notifyEventSubscribers(isRunning ? "runStart" : "runEnd", {});
112
+ this._notifySubscribers();
113
+ }
114
+ _hasExecutingTools(store) {
115
+ if (store.unstable_enableToolInvocations !== true) return false;
116
+ if (this._toolInvocations === null) return false;
117
+ for (const status of this._toolStatuses.values()) if (status.type === "executing") return true;
118
+ return false;
119
+ }
120
+ _getEffectiveIsRunning(store) {
121
+ return (store.isRunning ?? false) || this._hasExecutingTools(store);
122
+ }
84
123
  beginEdit(messageId) {
85
124
  if (!this._store.onEdit) throw new Error("Runtime does not support editing.");
86
125
  super.beginEdit(messageId);
@@ -91,11 +130,21 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
91
130
  }
92
131
  __internal_setAdapter(store) {
93
132
  if (this._store === store) return;
94
- const isRunning = store.isRunning ?? false;
133
+ this._updateStoreSnapshot(store);
134
+ }
135
+ _updateStoreSnapshot(store) {
136
+ const previousIsRunning = this._effectiveIsRunning;
95
137
  this.isDisabled = store.isDisabled ?? false;
96
138
  this.isSendDisabled = store.isSendDisabled ?? false;
97
139
  const oldStore = this._store;
98
140
  this._store = store;
141
+ const isRunning = this._getEffectiveIsRunning(store);
142
+ const repositoryInstance = store.unstable_messageRepositoryInstance;
143
+ const repositoryChanged = repositoryInstance !== void 0 && repositoryInstance !== this.repository;
144
+ if (repositoryChanged) {
145
+ this.repository = repositoryInstance;
146
+ this._pendingDeleteEvictions.clear();
147
+ }
99
148
  if (oldStore?.queue !== store.queue) {
100
149
  this._transformedQueue = void 0;
101
150
  store.queue?.__internal_setDispatchTransform?.((message) => {
@@ -129,26 +178,27 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
129
178
  if (!shallowEqual(this._capabilities, newCapabilities)) this._capabilities = newCapabilities;
130
179
  let messages;
131
180
  if (store.messageRepository) {
132
- if (oldStore && oldStore.isRunning === store.isRunning && oldStore.messageRepository === store.messageRepository) {
181
+ if (oldStore && !repositoryChanged && oldStore.isRunning === store.isRunning && oldStore.messageRepository === store.messageRepository && previousIsRunning === isRunning) {
133
182
  this._notifySubscribers();
134
183
  return;
135
184
  }
136
185
  const incoming = store.messageRepository.messages;
137
186
  const headId = store.messageRepository.headId ?? incoming.at(-1)?.message.id ?? null;
138
- if (oldStore && oldStore.messageRepository === store.messageRepository) {
187
+ if (oldStore && !repositoryChanged && oldStore.messageRepository === store.messageRepository) {
139
188
  this.repository.resetHead(headId);
140
189
  messages = this.repository.getMessages();
141
190
  } else {
142
191
  const incomingIds = new Set(incoming.map(({ message }) => message.id));
143
192
  for (const { message, parentId } of incoming) this.repository.addOrUpdateMessage(parentId, message);
144
193
  for (const { message } of this.repository.export().messages) if (!incomingIds.has(message.id)) this.repository.deleteMessage(message.id);
194
+ this._pendingDeleteEvictions.clear();
145
195
  this.repository.resetHead(headId);
146
196
  messages = this.repository.getMessages();
147
197
  }
148
198
  } else if (store.messages) {
149
199
  if (oldStore) {
150
200
  if (oldStore.convertMessage !== store.convertMessage) this._converter = new ThreadMessageConverter();
151
- else if (oldStore.isRunning === store.isRunning && oldStore.messages === store.messages) {
201
+ else if (!repositoryChanged && oldStore.isRunning === store.isRunning && oldStore.messages === store.messages && previousIsRunning === isRunning) {
152
202
  this._notifySubscribers();
153
203
  return;
154
204
  }
@@ -156,10 +206,20 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
156
206
  messages = !store.convertMessage ? store.messages : this._converter.convertMessages(store.messages, (cache, m, idx) => {
157
207
  if (!store.convertMessage) return m;
158
208
  const isLast = idx === (store.messages?.length ?? 0) - 1;
159
- const autoStatus = getAutoStatus(isLast, isRunning, false, false, void 0);
160
- if (cache && (cache.role !== "assistant" || !isAutoStatus(cache.status) || cache.status === autoStatus)) return cache;
209
+ const fallbackId = `${FALLBACK_ID_PREFIX}${idx}`;
210
+ if (cache && (cache.role !== "assistant" || !isAutoStatus(cache.status) || cache.status === getContentAutoStatus(cache.content, isLast, isRunning))) {
211
+ if (cache.id.startsWith("__external_store_fallback_") && cache.id !== fallbackId) {
212
+ const updated = {
213
+ ...cache,
214
+ id: fallbackId
215
+ };
216
+ bindExternalStoreMessage(updated, m);
217
+ return updated;
218
+ }
219
+ return cache;
220
+ }
161
221
  const messageLike = store.convertMessage(m, idx);
162
- const newMessage = fromThreadMessageLike(messageLike, idx.toString(), autoStatus);
222
+ const newMessage = fromThreadMessageLike(messageLike, fallbackId, getContentAutoStatus(messageLike.content, isLast, isRunning));
163
223
  bindExternalStoreMessage(newMessage, m);
164
224
  return newMessage;
165
225
  });
@@ -180,24 +240,46 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
180
240
  const parent = messages[i - 1];
181
241
  this.repository.addOrUpdateMessage(parent?.id ?? null, message);
182
242
  }
243
+ if (this._pendingDeleteEvictions.size > 0) {
244
+ const incomingIds = new Set(messages.map((m) => m.id));
245
+ for (const id of this._pendingDeleteEvictions) {
246
+ this._pendingDeleteEvictions.delete(id);
247
+ if (incomingIds.has(id)) continue;
248
+ try {
249
+ this.repository.getMessage(id);
250
+ } catch {
251
+ continue;
252
+ }
253
+ this.repository.deleteMessage(id);
254
+ }
255
+ }
183
256
  } else throw new Error("ExternalStoreAdapter must provide either 'messages' or 'messageRepository'");
184
257
  if (messages.length > 0) this.ensureInitialized();
185
- if ((oldStore?.isRunning ?? false) !== (store.isRunning ?? false)) {
186
- if (store.isRunning) this._notifyEventSubscribers("runStart", {});
258
+ this._effectiveIsRunning = isRunning;
259
+ if (previousIsRunning !== isRunning) {
260
+ if (isRunning) this._notifyEventSubscribers("runStart", {});
187
261
  else this._notifyEventSubscribers("runEnd", {});
188
262
  }
189
263
  let optimisticId = null;
190
264
  if (hasUpcomingMessage(isRunning, messages)) {
191
- optimisticId = generateId();
192
- this.repository.addOrUpdateMessage(messages.at(-1)?.id ?? null, fromThreadMessageLike({
265
+ const parentId = messages.at(-1)?.id ?? null;
266
+ if (this._optimistic?.parentId !== parentId) this._optimistic = {
267
+ id: generateId(),
268
+ parentId
269
+ };
270
+ optimisticId = this._optimistic.id;
271
+ this.repository.addOrUpdateMessage(parentId, fromThreadMessageLike({
193
272
  role: "assistant",
194
273
  content: [],
195
274
  metadata: { isOptimistic: true }
196
275
  }, optimisticId, { type: "running" }));
197
276
  }
277
+ if (optimisticId === null) this._optimistic = null;
198
278
  this.repository.resetHead(optimisticId ?? messages.at(-1)?.id ?? null);
199
- this._messages = this.repository.getMessages();
200
- this._driveToolInvocations();
279
+ const messagesSnapshot = this.repository.getMessages();
280
+ if (!this._messages || !shallowArrayEqual(this._messages, messagesSnapshot)) this._messages = messagesSnapshot;
281
+ if (repositoryChanged) this._runTrackerUpdate(() => this._toolInvocations?.reset());
282
+ this._runTrackerUpdate(() => this._driveToolInvocations());
201
283
  this._notifySubscribers();
202
284
  }
203
285
  /**
@@ -211,6 +293,7 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
211
293
  if (this._toolInvocations) {
212
294
  this._toolInvocations.reset();
213
295
  this._toolInvocations = null;
296
+ this._toolStatuses = /* @__PURE__ */ new Map();
214
297
  this._store.setToolStatuses?.({});
215
298
  }
216
299
  return;
@@ -234,12 +317,21 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
234
317
  }
235
318
  },
236
319
  onStatusesChange: (statuses) => {
237
- this._store.setToolStatuses?.(Object.fromEntries(statuses));
320
+ const hadExecutingTools = this._hasExecutingTools(this._store);
321
+ this._toolStatuses = statuses;
322
+ try {
323
+ this._store.setToolStatuses?.(Object.fromEntries(statuses));
324
+ } finally {
325
+ if (hadExecutingTools !== this._hasExecutingTools(this._store)) {
326
+ if (this._inTrackerUpdate) this._pendingRunningRefresh = true;
327
+ else this._updateStoreSnapshot(this._store);
328
+ }
329
+ }
238
330
  }
239
- });
331
+ }, (toolCall) => this._store.unstable_isClientToolCall?.(toolCall));
240
332
  this._toolInvocations.setState({
241
333
  messages: this._messages,
242
- isRunning: this._store.isRunning ?? false,
334
+ isRunning: this._getEffectiveIsRunning(this._store),
243
335
  ...this._store.isLoading !== void 0 && { isLoading: this._store.isLoading }
244
336
  });
245
337
  }
@@ -258,27 +350,18 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
258
350
  _findMessageIdForToolCall(toolCallId) {
259
351
  if (this._messagesForToolCallIndex !== this._messages) {
260
352
  this._toolCallToMessageId.clear();
261
- const visit = (messages) => {
262
- for (const message of messages) {
263
- if (!Array.isArray(message.content)) continue;
264
- for (const part of message.content) {
265
- if (!part || part.type !== "tool-call") continue;
266
- this._toolCallToMessageId.set(part.toolCallId, message.id);
267
- if (part.messages) visit(part.messages);
268
- }
269
- }
270
- };
271
- visit(this._messages);
353
+ for (const { part, messageId } of walkToolCallTree(this._messages)) this._toolCallToMessageId.set(part.toolCallId, messageId);
272
354
  this._messagesForToolCallIndex = this._messages;
273
355
  }
274
356
  return this._toolCallToMessageId.get(toolCallId);
275
357
  }
276
358
  switchToBranch(branchId) {
277
359
  if (!this._store.setMessages) throw new Error("Runtime does not support switching branches.");
278
- if (this._store.isRunning) return;
360
+ if (this._getEffectiveIsRunning(this._store)) return;
279
361
  const onBranchChange = this._store.unstable_onBranchChange;
280
362
  const previousHeadId = onBranchChange ? this.repository.canonicalHeadId : null;
281
363
  this.repository.switchToBranch(branchId);
364
+ this._pendingDeleteEvictions.clear();
282
365
  this.updateMessages(this.repository.getMessages());
283
366
  if (onBranchChange) this._notifyBranchChange(previousHeadId, onBranchChange);
284
367
  }
@@ -307,28 +390,48 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
307
390
  if (!isEdit && this._store.queue) {
308
391
  if (initPromise) await initPromise;
309
392
  if (!isThreadRuntimeGenerationCurrent(this, generation)) return;
310
- if (message.steer ?? this._store.isRunning ?? false) this._store.queue.steer(message);
393
+ if (message.steer ?? this._getEffectiveIsRunning(this._store)) this._store.queue.steer(message);
311
394
  else this._store.queue.enqueue(message);
312
395
  return;
313
396
  }
314
397
  initPromise?.catch(() => {});
315
- if (message.startRun ?? message.role === "user") await this._toolInvocations?.abort();
398
+ if (message.startRun ?? message.role === "user") await this._toolInvocations?.abort({ discardPending: true });
316
399
  if (!isThreadRuntimeGenerationCurrent(this, generation)) return;
317
400
  if (isEdit) {
318
401
  if (!this._store.onEdit) throw new Error("Runtime does not support editing messages.");
402
+ this._pendingDeleteEvictions.clear();
319
403
  await this._store.onEdit(message);
320
404
  } else await this._store.onNew(message);
321
405
  }
322
406
  async deleteMessage(messageId) {
323
407
  if (this._store.onDelete) {
324
- await this._store.onDelete(messageId);
408
+ if (this.repository.getMessages().some((m) => m.id === messageId)) this._pendingDeleteEvictions.add(messageId);
409
+ try {
410
+ await this._store.onDelete(messageId);
411
+ } catch (error) {
412
+ this._pendingDeleteEvictions.delete(messageId);
413
+ throw error;
414
+ }
325
415
  return;
326
416
  }
327
417
  if (!this._store.setMessages) throw new Error("Runtime does not support deleting messages.");
328
- if (this._store.isRunning) await this._toolInvocations?.abort();
418
+ if (this._getEffectiveIsRunning(this._store)) await this._toolInvocations?.abort();
329
419
  const messages = this.repository.getMessages();
330
420
  if (messages.findIndex((m) => m.id === messageId) === -1) throw new Error("Message not found.");
421
+ this._pendingDeleteEvictions.clear();
331
422
  this.updateMessages(messages.filter((message) => message.id !== messageId));
423
+ this._evictDeletedMessage(messageId);
424
+ }
425
+ _evictDeletedMessage(messageId) {
426
+ if (messageId.startsWith("__external_store_fallback_")) return;
427
+ try {
428
+ this.repository.getMessage(messageId);
429
+ } catch {
430
+ return;
431
+ }
432
+ this.repository.deleteMessage(messageId);
433
+ this._messages = this.repository.getMessages();
434
+ this._notifySubscribers();
332
435
  }
333
436
  getQueueItems() {
334
437
  return this._store?.queue?.items ?? EMPTY_QUEUE_ITEMS;
@@ -344,7 +447,8 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
344
447
  }
345
448
  async startRun(config) {
346
449
  if (!this._store.onReload) throw new Error("Runtime does not support reloading messages.");
347
- await this._toolInvocations?.abort();
450
+ this._pendingDeleteEvictions.clear();
451
+ await this._toolInvocations?.abort({ discardPending: true });
348
452
  await this._store.onReload(config.parentId, config);
349
453
  }
350
454
  async resumeRun(config) {
@@ -357,7 +461,7 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
357
461
  }
358
462
  importExternalState(state) {
359
463
  if (!this._store.onLoadExternalState) throw new Error("Runtime does not support importing external states.");
360
- this._toolInvocations?.reset();
464
+ this._runTrackerUpdate(() => this._toolInvocations?.reset());
361
465
  this._store.onLoadExternalState(state);
362
466
  }
363
467
  /**
@@ -366,13 +470,13 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
366
470
  * without run-cancel semantics (`onCancel`, composer draft restoration).
367
471
  */
368
472
  unstable_notifySessionReset() {
369
- this._toolInvocations?.reset();
473
+ this._runTrackerUpdate(() => this._toolInvocations?.reset());
370
474
  this._store.queue?.__internal_notifyCancelled?.();
371
475
  }
372
476
  cancelRun() {
373
477
  if (!this._store.onCancel) throw new Error("Runtime does not support cancelling runs.");
374
478
  const generation = captureThreadRuntimeGeneration(this);
375
- this._toolInvocations?.abort();
479
+ this._toolInvocations?.abort({ discardPending: true });
376
480
  this._store.queue?.__internal_notifyCancelled?.();
377
481
  observeAdapterCallback("onCancel", this._store.onCancel());
378
482
  this.dropEmptyOptimisticHead();
@@ -424,7 +528,11 @@ var ExternalStoreThreadRuntimeCore = class extends BaseThreadRuntimeCore {
424
528
  }
425
529
  respondToToolApproval(options) {
426
530
  if (!this._store.onRespondToToolApproval) throw new Error("Runtime does not support tool approvals.");
427
- observeAdapterCallback("onRespondToToolApproval", this._store.onRespondToToolApproval(options));
531
+ try {
532
+ return Promise.resolve(this._store.onRespondToToolApproval(options));
533
+ } catch (error) {
534
+ return Promise.reject(error);
535
+ }
428
536
  }
429
537
  reset(initialMessages) {
430
538
  const repo = new MessageRepository();