@assistant-ui/react 0.11.34 → 0.11.36

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 (216) hide show
  1. package/dist/augmentations.d.ts +9 -0
  2. package/dist/augmentations.d.ts.map +1 -1
  3. package/dist/client/AssistantClient.d.ts +13 -57
  4. package/dist/client/AssistantClient.d.ts.map +1 -1
  5. package/dist/client/AssistantClient.js +49 -26
  6. package/dist/client/AssistantClient.js.map +1 -1
  7. package/dist/client/ModelContext.d.ts +8 -0
  8. package/dist/client/ModelContext.d.ts.map +1 -0
  9. package/dist/client/ModelContext.js +21 -0
  10. package/dist/client/ModelContext.js.map +1 -0
  11. package/dist/client/ModelContextClient.d.ts +7 -0
  12. package/dist/client/ModelContextClient.d.ts.map +1 -0
  13. package/dist/client/ModelContextClient.js +18 -0
  14. package/dist/client/ModelContextClient.js.map +1 -0
  15. package/dist/client/ToolUIClient.d.ts.map +1 -1
  16. package/dist/client/ToolUIClient.js +45 -3
  17. package/dist/client/ToolUIClient.js.map +1 -1
  18. package/dist/client/ToolUIContext.d.ts +4 -0
  19. package/dist/client/ToolUIContext.d.ts.map +1 -0
  20. package/dist/client/ToolUIContext.js +20 -0
  21. package/dist/client/ToolUIContext.js.map +1 -0
  22. package/dist/client/Tools.d.ts +10 -0
  23. package/dist/client/Tools.d.ts.map +1 -0
  24. package/dist/client/Tools.js +55 -0
  25. package/dist/client/Tools.js.map +1 -0
  26. package/dist/client/types/ModelContext.d.ts +12 -0
  27. package/dist/client/types/ModelContext.d.ts.map +1 -0
  28. package/dist/client/types/ModelContext.js +1 -0
  29. package/dist/client/types/ModelContext.js.map +1 -0
  30. package/dist/client/types/ThreadList.d.ts.map +1 -1
  31. package/dist/client/types/ToolUI.d.ts +12 -1
  32. package/dist/client/types/ToolUI.d.ts.map +1 -1
  33. package/dist/client/types/Tools.d.ts +9 -0
  34. package/dist/client/types/Tools.d.ts.map +1 -0
  35. package/dist/client/types/Tools.js +1 -0
  36. package/dist/client/types/Tools.js.map +1 -0
  37. package/dist/context/providers/AttachmentByIndexProvider.d.ts.map +1 -1
  38. package/dist/context/providers/AttachmentByIndexProvider.js +20 -24
  39. package/dist/context/providers/AttachmentByIndexProvider.js.map +1 -1
  40. package/dist/context/providers/MessageByIndexProvider.d.ts.map +1 -1
  41. package/dist/context/providers/MessageByIndexProvider.js +27 -29
  42. package/dist/context/providers/MessageByIndexProvider.js.map +1 -1
  43. package/dist/context/providers/MessageProvider.d.ts.map +1 -1
  44. package/dist/context/providers/MessageProvider.js +11 -13
  45. package/dist/context/providers/MessageProvider.js.map +1 -1
  46. package/dist/context/providers/PartByIndexProvider.d.ts.map +1 -1
  47. package/dist/context/providers/PartByIndexProvider.js +11 -13
  48. package/dist/context/providers/PartByIndexProvider.js.map +1 -1
  49. package/dist/context/providers/TextMessagePartProvider.d.ts.map +1 -1
  50. package/dist/context/providers/TextMessagePartProvider.js +11 -13
  51. package/dist/context/providers/TextMessagePartProvider.js.map +1 -1
  52. package/dist/context/providers/ThreadListItemProvider.d.ts.map +1 -1
  53. package/dist/context/providers/ThreadListItemProvider.js +41 -45
  54. package/dist/context/providers/ThreadListItemProvider.js.map +1 -1
  55. package/dist/context/react/AssistantApiContext.d.ts +33 -4
  56. package/dist/context/react/AssistantApiContext.d.ts.map +1 -1
  57. package/dist/context/react/AssistantApiContext.js +35 -17
  58. package/dist/context/react/AssistantApiContext.js.map +1 -1
  59. package/dist/context/react/index.d.ts +1 -1
  60. package/dist/context/react/index.d.ts.map +1 -1
  61. package/dist/context/react/index.js +5 -1
  62. package/dist/context/react/index.js.map +1 -1
  63. package/dist/legacy-runtime/AssistantRuntimeProvider.d.ts.map +1 -1
  64. package/dist/legacy-runtime/AssistantRuntimeProvider.js +7 -9
  65. package/dist/legacy-runtime/AssistantRuntimeProvider.js.map +1 -1
  66. package/dist/legacy-runtime/RuntimeAdapter.d.ts +7 -0
  67. package/dist/legacy-runtime/RuntimeAdapter.d.ts.map +1 -0
  68. package/dist/legacy-runtime/RuntimeAdapter.js +20 -0
  69. package/dist/legacy-runtime/RuntimeAdapter.js.map +1 -0
  70. package/dist/legacy-runtime/client/ThreadListRuntimeClient.d.ts +2 -0
  71. package/dist/legacy-runtime/client/ThreadListRuntimeClient.d.ts.map +1 -1
  72. package/dist/legacy-runtime/client/ThreadListRuntimeClient.js +6 -2
  73. package/dist/legacy-runtime/client/ThreadListRuntimeClient.js.map +1 -1
  74. package/dist/legacy-runtime/hooks/AssistantContext.d.ts +14 -1
  75. package/dist/legacy-runtime/hooks/AssistantContext.d.ts.map +1 -1
  76. package/dist/legacy-runtime/hooks/AssistantContext.js +1 -1
  77. package/dist/legacy-runtime/hooks/AssistantContext.js.map +1 -1
  78. package/dist/legacy-runtime/hooks/AttachmentContext.d.ts +6 -0
  79. package/dist/legacy-runtime/hooks/AttachmentContext.d.ts.map +1 -1
  80. package/dist/legacy-runtime/hooks/AttachmentContext.js.map +1 -1
  81. package/dist/legacy-runtime/hooks/ComposerContext.d.ts +41 -2
  82. package/dist/legacy-runtime/hooks/ComposerContext.d.ts.map +1 -1
  83. package/dist/legacy-runtime/hooks/ComposerContext.js.map +1 -1
  84. package/dist/legacy-runtime/hooks/MessageContext.d.ts +36 -2
  85. package/dist/legacy-runtime/hooks/MessageContext.d.ts.map +1 -1
  86. package/dist/legacy-runtime/hooks/MessageContext.js.map +1 -1
  87. package/dist/legacy-runtime/hooks/MessagePartContext.d.ts +6 -0
  88. package/dist/legacy-runtime/hooks/MessagePartContext.d.ts.map +1 -1
  89. package/dist/legacy-runtime/hooks/MessagePartContext.js.map +1 -1
  90. package/dist/legacy-runtime/hooks/ThreadContext.d.ts +26 -1
  91. package/dist/legacy-runtime/hooks/ThreadContext.d.ts.map +1 -1
  92. package/dist/legacy-runtime/hooks/ThreadContext.js.map +1 -1
  93. package/dist/legacy-runtime/hooks/ThreadListItemContext.d.ts +6 -0
  94. package/dist/legacy-runtime/hooks/ThreadListItemContext.d.ts.map +1 -1
  95. package/dist/legacy-runtime/hooks/ThreadListItemContext.js.map +1 -1
  96. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts +2 -2
  97. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts.map +1 -1
  98. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js +4 -2
  99. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js.map +1 -1
  100. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts +2 -0
  101. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts.map +1 -1
  102. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts +3 -0
  103. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts.map +1 -1
  104. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +14 -4
  105. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -1
  106. package/dist/legacy-runtime/runtime-cores/core/AssistantRuntimeCore.d.ts +1 -0
  107. package/dist/legacy-runtime/runtime-cores/core/AssistantRuntimeCore.d.ts.map +1 -1
  108. package/dist/legacy-runtime/runtime-cores/core/BaseAssistantRuntimeCore.d.ts +1 -0
  109. package/dist/legacy-runtime/runtime-cores/core/BaseAssistantRuntimeCore.d.ts.map +1 -1
  110. package/dist/legacy-runtime/runtime-cores/core/BaseAssistantRuntimeCore.js +3 -0
  111. package/dist/legacy-runtime/runtime-cores/core/BaseAssistantRuntimeCore.js.map +1 -1
  112. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadListRuntimeCore.d.ts +4 -1
  113. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadListRuntimeCore.d.ts.map +1 -1
  114. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadListRuntimeCore.js +2 -2
  115. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadListRuntimeCore.js.map +1 -1
  116. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.d.ts.map +1 -1
  117. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js +7 -1
  118. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js.map +1 -1
  119. package/dist/legacy-runtime/runtime-cores/external-store/auto-status.d.ts +2 -12
  120. package/dist/legacy-runtime/runtime-cores/external-store/auto-status.d.ts.map +1 -1
  121. package/dist/legacy-runtime/runtime-cores/external-store/auto-status.js +45 -15
  122. package/dist/legacy-runtime/runtime-cores/external-store/auto-status.js.map +1 -1
  123. package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.d.ts +2 -0
  124. package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.d.ts.map +1 -1
  125. package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js +4 -2
  126. package/dist/legacy-runtime/runtime-cores/external-store/external-message-converter.js.map +1 -1
  127. package/dist/legacy-runtime/runtime-cores/local/LocalThreadListRuntimeCore.d.ts +4 -1
  128. package/dist/legacy-runtime/runtime-cores/local/LocalThreadListRuntimeCore.d.ts.map +1 -1
  129. package/dist/legacy-runtime/runtime-cores/local/LocalThreadListRuntimeCore.js +2 -2
  130. package/dist/legacy-runtime/runtime-cores/local/LocalThreadListRuntimeCore.js.map +1 -1
  131. package/dist/legacy-runtime/runtime-cores/utils/MessageRepository.js +1 -1
  132. package/dist/legacy-runtime/runtime-cores/utils/MessageRepository.js.map +1 -1
  133. package/dist/model-context/index.d.ts +2 -0
  134. package/dist/model-context/index.d.ts.map +1 -1
  135. package/dist/model-context/index.js +6 -0
  136. package/dist/model-context/index.js.map +1 -1
  137. package/dist/model-context/makeAssistantVisible.js +1 -1
  138. package/dist/model-context/makeAssistantVisible.js.map +1 -1
  139. package/dist/model-context/toolbox.d.ts +28 -0
  140. package/dist/model-context/toolbox.d.ts.map +1 -0
  141. package/dist/model-context/toolbox.js +15 -0
  142. package/dist/model-context/toolbox.js.map +1 -0
  143. package/dist/model-context/useAssistantInstructions.js +1 -1
  144. package/dist/model-context/useAssistantInstructions.js.map +1 -1
  145. package/dist/model-context/useAssistantTool.js +1 -1
  146. package/dist/model-context/useAssistantTool.js.map +1 -1
  147. package/dist/primitives/message/MessageParts.d.ts.map +1 -1
  148. package/dist/primitives/message/MessageParts.js +2 -2
  149. package/dist/primitives/message/MessageParts.js.map +1 -1
  150. package/dist/primitives/message/MessagePartsGrouped.d.ts.map +1 -1
  151. package/dist/primitives/message/MessagePartsGrouped.js +2 -2
  152. package/dist/primitives/message/MessagePartsGrouped.js.map +1 -1
  153. package/dist/tests/setup.js +8 -11
  154. package/dist/tests/setup.js.map +1 -1
  155. package/dist/utils/tap-store/derived-scopes.d.ts +81 -0
  156. package/dist/utils/tap-store/derived-scopes.d.ts.map +1 -0
  157. package/dist/utils/tap-store/derived-scopes.js +67 -0
  158. package/dist/utils/tap-store/derived-scopes.js.map +1 -0
  159. package/dist/utils/tap-store/index.d.ts +1 -0
  160. package/dist/utils/tap-store/index.d.ts.map +1 -1
  161. package/dist/utils/tap-store/index.js +4 -0
  162. package/dist/utils/tap-store/index.js.map +1 -1
  163. package/dist/utils/tap-store/tap-api.js +1 -1
  164. package/dist/utils/tap-store/tap-api.js.map +1 -1
  165. package/package.json +9 -9
  166. package/src/augmentations.ts +12 -0
  167. package/src/client/AssistantClient.ts +64 -38
  168. package/src/client/ModelContext.ts +28 -0
  169. package/src/client/ModelContextClient.ts +16 -0
  170. package/src/client/ToolUIClient.ts +50 -3
  171. package/src/client/ToolUIContext.ts +22 -0
  172. package/src/client/Tools.ts +68 -0
  173. package/src/client/types/ModelContext.ts +15 -0
  174. package/src/client/types/ThreadList.ts +4 -0
  175. package/src/client/types/ToolUI.ts +10 -1
  176. package/src/client/types/Tools.ts +11 -0
  177. package/src/context/providers/AttachmentByIndexProvider.tsx +21 -25
  178. package/src/context/providers/MessageByIndexProvider.tsx +31 -33
  179. package/src/context/providers/MessageProvider.tsx +12 -14
  180. package/src/context/providers/PartByIndexProvider.tsx +12 -14
  181. package/src/context/providers/TextMessagePartProvider.tsx +12 -14
  182. package/src/context/providers/ThreadListItemProvider.tsx +42 -46
  183. package/src/context/react/AssistantApiContext.tsx +69 -29
  184. package/src/context/react/index.ts +5 -1
  185. package/src/legacy-runtime/AssistantRuntimeProvider.tsx +7 -9
  186. package/src/legacy-runtime/RuntimeAdapter.ts +19 -0
  187. package/src/legacy-runtime/client/ThreadListRuntimeClient.ts +10 -1
  188. package/src/legacy-runtime/hooks/AssistantContext.ts +16 -2
  189. package/src/legacy-runtime/hooks/AttachmentContext.ts +6 -0
  190. package/src/legacy-runtime/hooks/ComposerContext.ts +41 -2
  191. package/src/legacy-runtime/hooks/MessageContext.ts +37 -2
  192. package/src/legacy-runtime/hooks/MessagePartContext.ts +6 -0
  193. package/src/legacy-runtime/hooks/ThreadContext.ts +27 -1
  194. package/src/legacy-runtime/hooks/ThreadListItemContext.ts +6 -0
  195. package/src/legacy-runtime/runtime-cores/assistant-transport/index.ts +2 -0
  196. package/src/legacy-runtime/runtime-cores/assistant-transport/types.ts +3 -0
  197. package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.tsx +35 -14
  198. package/src/legacy-runtime/runtime-cores/core/AssistantRuntimeCore.tsx +1 -0
  199. package/src/legacy-runtime/runtime-cores/core/BaseAssistantRuntimeCore.tsx +4 -0
  200. package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadListRuntimeCore.tsx +4 -2
  201. package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.tsx +7 -1
  202. package/src/legacy-runtime/runtime-cores/external-store/auto-status.tsx +49 -16
  203. package/src/legacy-runtime/runtime-cores/external-store/external-message-converter.tsx +4 -0
  204. package/src/legacy-runtime/runtime-cores/local/LocalThreadListRuntimeCore.tsx +4 -2
  205. package/src/legacy-runtime/runtime-cores/utils/MessageRepository.tsx +1 -1
  206. package/src/model-context/index.ts +9 -0
  207. package/src/model-context/makeAssistantVisible.tsx +1 -1
  208. package/src/model-context/toolbox.tsx +37 -0
  209. package/src/model-context/useAssistantInstructions.tsx +1 -1
  210. package/src/model-context/useAssistantTool.tsx +1 -1
  211. package/src/primitives/message/MessageParts.tsx +3 -2
  212. package/src/primitives/message/MessagePartsGrouped.tsx +3 -2
  213. package/src/tests/setup.ts +2 -8
  214. package/src/utils/tap-store/derived-scopes.ts +185 -0
  215. package/src/utils/tap-store/index.ts +9 -0
  216. package/src/utils/tap-store/tap-api.ts +1 -1
@@ -8,6 +8,7 @@ import {
8
8
  useMemo,
9
9
  useEffect,
10
10
  } from "react";
11
+ import { useResource } from "@assistant-ui/tap/react";
11
12
 
12
13
  import { ToolUIApi, ToolUIState, ToolUIMeta } from "../../client/types/ToolUI";
13
14
  import {
@@ -32,8 +33,6 @@ import {
32
33
  AttachmentClientState,
33
34
  } from "../../client/types/Attachment";
34
35
  import { Unsubscribe } from "@assistant-ui/tap";
35
- import { ModelContextProvider } from "../../model-context";
36
- import { AssistantRuntime } from "../../legacy-runtime/runtime/AssistantRuntime";
37
36
  import {
38
37
  AssistantEvent,
39
38
  AssistantEventCallback,
@@ -50,6 +49,15 @@ import {
50
49
  AssistantClientProps,
51
50
  useAssistantClient,
52
51
  } from "../../client/AssistantClient";
52
+ import { ToolsApi, ToolsMeta } from "../../client/types/Tools";
53
+ import {
54
+ ModelContextApi,
55
+ ModelContextMeta,
56
+ } from "../../client/types/ModelContext";
57
+ import {
58
+ DerivedScopes,
59
+ DerivedScopesInput,
60
+ } from "../../utils/tap-store/derived-scopes";
53
61
 
54
62
  export type AssistantState = {
55
63
  readonly threads: ThreadListClientState;
@@ -63,7 +71,7 @@ export type AssistantState = {
63
71
  readonly attachment: AttachmentClientState;
64
72
  };
65
73
 
66
- type AssistantApiField<
74
+ export type AssistantApiField<
67
75
  TApi,
68
76
  TMeta extends { source: string | null; query: any },
69
77
  > = (() => TApi) & (TMeta | { source: null; query: Record<string, never> });
@@ -115,6 +123,8 @@ type AttachmentMeta = {
115
123
  export type AssistantApi = {
116
124
  threads: AssistantApiField<ThreadListClientApi, ThreadsMeta>;
117
125
  toolUIs: AssistantApiField<ToolUIApi, ToolUIMeta>;
126
+ tools: AssistantApiField<ToolsApi, ToolsMeta>;
127
+ modelContext: AssistantApiField<ModelContextApi, ModelContextMeta>;
118
128
  threadListItem: AssistantApiField<
119
129
  ThreadListItemClientApi,
120
130
  ThreadListItemMeta
@@ -132,11 +142,6 @@ export type AssistantApi = {
132
142
  event: AssistantEventSelector<TEvent>,
133
143
  callback: AssistantEventCallback<TEvent>,
134
144
  ): Unsubscribe;
135
-
136
- // temp
137
- registerModelContextProvider(provider: ModelContextProvider): void;
138
- /** @internal */
139
- __internal_getRuntime?(): AssistantRuntime;
140
145
  };
141
146
 
142
147
  export const createAssistantApiField = <
@@ -170,6 +175,22 @@ const AssistantApiContext = createContext<AssistantApi>({
170
175
  throw new Error("ToolUIs is only available inside <AssistantProvider />");
171
176
  },
172
177
  }),
178
+ tools: createAssistantApiField({
179
+ source: null,
180
+ query: {},
181
+ get: (): never => {
182
+ throw new Error("Tools is only available inside <AssistantProvider />");
183
+ },
184
+ }),
185
+ modelContext: createAssistantApiField({
186
+ source: null,
187
+ query: {},
188
+ get: (): never => {
189
+ throw new Error(
190
+ "ModelContext is only available inside <AssistantProvider />",
191
+ );
192
+ },
193
+ }),
173
194
  threadListItem: createAssistantApiField({
174
195
  source: null,
175
196
  query: {},
@@ -229,25 +250,47 @@ const AssistantApiContext = createContext<AssistantApi>({
229
250
  const { scope } = normalizeEventSelector(selector);
230
251
  throw new Error(`Event scope is not available in this component: ${scope}`);
231
252
  },
232
-
233
- registerModelContextProvider: () => {
234
- throw new Error(
235
- "Registering model context providers is only available inside <AssistantProvider />",
236
- );
237
- },
238
253
  });
239
254
 
240
- const useAssistantApiImpl = (): AssistantApi => {
255
+ export const useAssistantApiImpl = (): AssistantApi => {
241
256
  return useContext(AssistantApiContext);
242
257
  };
243
258
 
259
+ /**
260
+ * Hook to extend the current AssistantApi with additional derived scope fields and special callbacks.
261
+ * This merges the derived fields with the existing API from context.
262
+ * Fields are automatically memoized based on source and query changes.
263
+ * Special callbacks (on, subscribe, flushSync) use the useEffectEvent pattern to always access latest values.
264
+ *
265
+ * @param scopes - Record of field names to DerivedScope resource elements, plus optional special callbacks
266
+ * @returns The merged AssistantApi
267
+ *
268
+ * @example
269
+ * ```tsx
270
+ * const api = useExtendedAssistantApi({
271
+ * message: DerivedScope({
272
+ * source: "root",
273
+ * query: {},
274
+ * get: () => messageApi,
275
+ * }),
276
+ * on: (selector, callback) => {
277
+ * // Custom event filtering logic
278
+ * },
279
+ * });
280
+ * ```
281
+ */
282
+ export const useExtendedAssistantApi = (
283
+ scopes: DerivedScopesInput,
284
+ ): AssistantApi => {
285
+ const baseApi = useAssistantApiImpl();
286
+ const partialApi = useResource(DerivedScopes(scopes));
287
+ return useMemo(() => extendApi(baseApi, partialApi), [baseApi, partialApi]);
288
+ };
289
+
244
290
  const useExtendedAssistantApiImpl = (
245
291
  config: AssistantClientProps,
246
292
  ): AssistantApi => {
247
- const api = useAssistantApiImpl();
248
- const api2 = useAssistantClient(config);
249
- const extendedApi = useMemo(() => extendApi(api, api2), [api, api2]);
250
- return extendedApi;
293
+ return useAssistantClient(config);
251
294
  };
252
295
 
253
296
  export function useAssistantApi(): AssistantApi;
@@ -293,7 +336,7 @@ const mergeFnsWithUnsubscribe = <TArgs extends Array<unknown>>(
293
336
  };
294
337
  };
295
338
 
296
- const extendApi = (
339
+ export const extendApi = (
297
340
  api: AssistantApi,
298
341
  api2: Partial<AssistantApi>,
299
342
  ): AssistantApi => {
@@ -311,18 +354,15 @@ const extendApi = (
311
354
  };
312
355
 
313
356
  export const AssistantProvider: FC<
314
- PropsWithChildren<{ api: Partial<AssistantApi>; devToolsVisible?: boolean }>
315
- > = ({ api: api2, children, devToolsVisible = true }) => {
316
- const api = useAssistantApi();
317
- const extendedApi = useMemo(() => extendApi(api, api2), [api, api2]);
318
-
357
+ PropsWithChildren<{ api: AssistantApi; devToolsVisible?: boolean }>
358
+ > = ({ api, children, devToolsVisible = true }) => {
319
359
  useEffect(() => {
320
- if (!devToolsVisible || !api2.subscribe) return undefined;
321
- return DevToolsProviderApi.register(api2);
322
- }, [api2, devToolsVisible]);
360
+ if (!devToolsVisible || !api.subscribe) return undefined;
361
+ return DevToolsProviderApi.register(api);
362
+ }, [api, devToolsVisible]);
323
363
 
324
364
  return (
325
- <AssistantApiContext.Provider value={extendedApi}>
365
+ <AssistantApiContext.Provider value={api}>
326
366
  {/* TODO temporarily allow accessing viewport state from outside the viewport */}
327
367
  {/* TODO figure out if this behavior should be deprecated, since it is quite hacky */}
328
368
  <ThreadViewportProvider>{children}</ThreadViewportProvider>
@@ -2,7 +2,11 @@
2
2
  // TODO createContextStoreHook does not work well with server-side nextjs bundler
3
3
  // use client necessary here for now
4
4
 
5
- export { useAssistantApi, type AssistantApi } from "./AssistantApiContext";
5
+ export {
6
+ useAssistantApi,
7
+ useExtendedAssistantApi,
8
+ type AssistantApi,
9
+ } from "./AssistantApiContext";
6
10
  export { useAssistantState } from "./hooks/useAssistantState";
7
11
  export { useAssistantEvent } from "./hooks/useAssistantEvent";
8
12
 
@@ -1,11 +1,13 @@
1
1
  "use client";
2
2
 
3
3
  import { FC, memo, PropsWithChildren } from "react";
4
- import { AssistantProvider } from "../context/react/AssistantApiContext";
4
+ import {
5
+ AssistantProvider,
6
+ useAssistantApi,
7
+ } from "../context/react/AssistantApiContext";
5
8
  import { AssistantRuntime } from "./runtime/AssistantRuntime";
6
9
  import { AssistantRuntimeCore } from "./runtime-cores/core/AssistantRuntimeCore";
7
- import { useAssistantClient } from "../client/AssistantClient";
8
- import { ThreadListClient } from "./client/ThreadListRuntimeClient";
10
+ import { RuntimeAdapter } from "./RuntimeAdapter";
9
11
 
10
12
  export namespace AssistantProvider {
11
13
  export type Props = PropsWithChildren<{
@@ -24,12 +26,8 @@ export const AssistantRuntimeProviderImpl: FC<AssistantProvider.Props> = ({
24
26
  children,
25
27
  runtime,
26
28
  }) => {
27
- const api = useAssistantClient({
28
- threads: ThreadListClient({
29
- runtime: runtime.threads,
30
- }),
31
- registerModelContextProvider: runtime.registerModelContextProvider,
32
- __internal_runtime: runtime,
29
+ const api = useAssistantApi({
30
+ threads: RuntimeAdapter(runtime),
33
31
  });
34
32
 
35
33
  const RenderComponent = getRenderComponent(runtime);
@@ -0,0 +1,19 @@
1
+ import { resource, tapEffect, tapInlineResource } from "@assistant-ui/tap";
2
+ import type { AssistantRuntime } from "./runtime/AssistantRuntime";
3
+ import { ThreadListClient } from "./client/ThreadListRuntimeClient";
4
+ import { tapModelContext } from "../client/ModelContext";
5
+
6
+ export const RuntimeAdapter = resource((runtime: AssistantRuntime) => {
7
+ const modelContext = tapModelContext();
8
+
9
+ tapEffect(() => {
10
+ return runtime.registerModelContextProvider(modelContext);
11
+ }, [runtime, modelContext]);
12
+
13
+ return tapInlineResource(
14
+ ThreadListClient({
15
+ runtime: runtime.threads,
16
+ __internal_assistantRuntime: runtime,
17
+ }),
18
+ );
19
+ });
@@ -9,6 +9,7 @@ import {
9
9
  ThreadListClientState,
10
10
  ThreadListClientApi,
11
11
  } from "../../client/types/ThreadList";
12
+ import type { AssistantRuntime } from "../runtime/AssistantRuntime";
12
13
 
13
14
  const ThreadListItemClientById = resource(
14
15
  ({ runtime, id }: { runtime: ThreadListRuntime; id: string }) => {
@@ -25,7 +26,13 @@ const ThreadListItemClientById = resource(
25
26
  );
26
27
 
27
28
  export const ThreadListClient = resource(
28
- ({ runtime }: { runtime: ThreadListRuntime }) => {
29
+ ({
30
+ runtime,
31
+ __internal_assistantRuntime,
32
+ }: {
33
+ runtime: ThreadListRuntime;
34
+ __internal_assistantRuntime: AssistantRuntime;
35
+ }) => {
29
36
  const runtimeState = tapSubscribable(runtime);
30
37
 
31
38
  const main = tapInlineResource(
@@ -80,6 +87,8 @@ export const ThreadListClient = resource(
80
87
  switchToNewThread: () => {
81
88
  runtime.switchToNewThread();
82
89
  },
90
+
91
+ __internal_getAssistantRuntime: () => __internal_assistantRuntime,
83
92
  });
84
93
  },
85
94
  );
@@ -6,6 +6,8 @@ import type { ThreadListRuntime } from "../runtime/ThreadListRuntime";
6
6
  import { createStateHookForRuntime } from "../../context/react/utils/createStateHookForRuntime";
7
7
 
8
8
  /**
9
+ * @deprecated Use `useAssistantApi()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
10
+ *
9
11
  * Hook to access the AssistantRuntime from the current context.
10
12
  *
11
13
  * The AssistantRuntime provides access to the top-level assistant state and actions,
@@ -17,13 +19,21 @@ import { createStateHookForRuntime } from "../../context/react/utils/createState
17
19
  *
18
20
  * @example
19
21
  * ```tsx
22
+ * // Before:
20
23
  * function MyComponent() {
21
24
  * const runtime = useAssistantRuntime();
22
- *
23
25
  * const handleNewThread = () => {
24
26
  * runtime.switchToNewThread();
25
27
  * };
28
+ * return <button onClick={handleNewThread}>New Thread</button>;
29
+ * }
26
30
  *
31
+ * // After:
32
+ * function MyComponent() {
33
+ * const api = useAssistantApi();
34
+ * const handleNewThread = () => {
35
+ * api.threads().switchToNewThread();
36
+ * };
27
37
  * return <button onClick={handleNewThread}>New Thread</button>;
28
38
  * }
29
39
  * ```
@@ -38,7 +48,7 @@ export function useAssistantRuntime(options?: {
38
48
  optional?: boolean | undefined;
39
49
  }) {
40
50
  const api = useAssistantApi();
41
- const runtime = api.__internal_getRuntime?.() ?? null;
51
+ const runtime = api.threads().__internal_getAssistantRuntime?.() ?? null;
42
52
 
43
53
  if (!runtime && !options?.optional) {
44
54
  throw new Error("AssistantRuntime is not available");
@@ -50,4 +60,8 @@ export function useAssistantRuntime(options?: {
50
60
  const useThreadListRuntime = (opt: {
51
61
  optional: boolean | undefined;
52
62
  }): ThreadListRuntime | null => useAssistantRuntime(opt)?.threads ?? null;
63
+
64
+ /**
65
+ * @deprecated Use `useAssistantState(({ threads }) => threads)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
66
+ */
53
67
  export const useThreadList = createStateHookForRuntime(useThreadListRuntime);
@@ -4,6 +4,9 @@ import { AttachmentRuntime } from "../runtime/AttachmentRuntime";
4
4
  import { createStateHookForRuntime } from "../../context/react/utils/createStateHookForRuntime";
5
5
  import { useAssistantApi, useAssistantState } from "../../context/react";
6
6
 
7
+ /**
8
+ * @deprecated Use `useAssistantApi()` with `api.attachment()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
9
+ */
7
10
  export function useAttachmentRuntime(options?: {
8
11
  optional?: false | undefined;
9
12
  }): AttachmentRuntime;
@@ -80,6 +83,9 @@ export function useMessageAttachmentRuntime(options?: {
80
83
  return attachmentRuntime as AttachmentRuntime<"message">;
81
84
  }
82
85
 
86
+ /**
87
+ * @deprecated Use `useAssistantState(({ attachment }) => attachment)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
88
+ */
83
89
  export const useAttachment = createStateHookForRuntime(useAttachmentRuntime);
84
90
 
85
91
  export const useThreadComposerAttachment = createStateHookForRuntime(
@@ -5,6 +5,8 @@ import { ComposerRuntime } from "../runtime/ComposerRuntime";
5
5
  import { createStateHookForRuntime } from "../../context/react/utils/createStateHookForRuntime";
6
6
 
7
7
  /**
8
+ * @deprecated Use `useAssistantApi()` with `api.composer()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
9
+ *
8
10
  * Hook to access the ComposerRuntime from the current context.
9
11
  *
10
12
  * The ComposerRuntime provides access to composer state and actions for message
@@ -18,21 +20,42 @@ import { createStateHookForRuntime } from "../../context/react/utils/createState
18
20
  *
19
21
  * @example
20
22
  * ```tsx
23
+ * // Before:
21
24
  * function ComposerActions() {
22
25
  * const runtime = useComposerRuntime();
23
- *
24
26
  * const handleSend = () => {
25
27
  * if (runtime.getState().canSend) {
26
28
  * runtime.send();
27
29
  * }
28
30
  * };
29
- *
30
31
  * const handleCancel = () => {
31
32
  * if (runtime.getState().canCancel) {
32
33
  * runtime.cancel();
33
34
  * }
34
35
  * };
36
+ * return (
37
+ * <div>
38
+ * <button onClick={handleSend}>Send</button>
39
+ * <button onClick={handleCancel}>Cancel</button>
40
+ * </div>
41
+ * );
42
+ * }
35
43
  *
44
+ * // After:
45
+ * function ComposerActions() {
46
+ * const api = useAssistantApi();
47
+ * const canSend = useAssistantState(({ composer }) => composer.canSend);
48
+ * const canCancel = useAssistantState(({ composer }) => composer.canCancel);
49
+ * const handleSend = () => {
50
+ * if (canSend) {
51
+ * api.composer().send();
52
+ * }
53
+ * };
54
+ * const handleCancel = () => {
55
+ * if (canCancel) {
56
+ * api.composer().cancel();
57
+ * }
58
+ * };
36
59
  * return (
37
60
  * <div>
38
61
  * <button onClick={handleSend}>Send</button>
@@ -64,6 +87,8 @@ export function useComposerRuntime(options?: {
64
87
  }
65
88
 
66
89
  /**
90
+ * @deprecated Use `useAssistantState(({ composer }) => composer)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
91
+ *
67
92
  * Hook to access the current composer state.
68
93
  *
69
94
  * This hook provides reactive access to the composer's state, including text content,
@@ -74,11 +99,25 @@ export function useComposerRuntime(options?: {
74
99
  *
75
100
  * @example
76
101
  * ```tsx
102
+ * // Before:
77
103
  * function ComposerStatus() {
78
104
  * const text = useComposer((state) => state.text);
79
105
  * const canSend = useComposer((state) => state.canSend);
80
106
  * const attachmentCount = useComposer((state) => state.attachments.length);
107
+ * return (
108
+ * <div>
109
+ * Text: {text.length} chars,
110
+ * Attachments: {attachmentCount},
111
+ * Can send: {canSend}
112
+ * </div>
113
+ * );
114
+ * }
81
115
  *
116
+ * // After:
117
+ * function ComposerStatus() {
118
+ * const text = useAssistantState(({ composer }) => composer.text);
119
+ * const canSend = useAssistantState(({ composer }) => composer.canSend);
120
+ * const attachmentCount = useAssistantState(({ composer }) => composer.attachments.length);
82
121
  * return (
83
122
  * <div>
84
123
  * Text: {text.length} chars,
@@ -6,6 +6,8 @@ import { createStateHookForRuntime } from "../../context/react/utils/createState
6
6
  import { EditComposerRuntime } from "../runtime";
7
7
 
8
8
  /**
9
+ * @deprecated Use `useAssistantApi()` with `api.message()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
10
+ *
9
11
  * Hook to access the MessageRuntime from the current context.
10
12
  *
11
13
  * The MessageRuntime provides access to message-level state and actions,
@@ -17,17 +19,32 @@ import { EditComposerRuntime } from "../runtime";
17
19
  *
18
20
  * @example
19
21
  * ```tsx
22
+ * // Before:
20
23
  * function MessageActions() {
21
24
  * const runtime = useMessageRuntime();
22
- *
23
25
  * const handleReload = () => {
24
26
  * runtime.reload();
25
27
  * };
26
- *
27
28
  * const handleEdit = () => {
28
29
  * runtime.startEdit();
29
30
  * };
31
+ * return (
32
+ * <div>
33
+ * <button onClick={handleReload}>Reload</button>
34
+ * <button onClick={handleEdit}>Edit</button>
35
+ * </div>
36
+ * );
37
+ * }
30
38
  *
39
+ * // After:
40
+ * function MessageActions() {
41
+ * const api = useAssistantApi();
42
+ * const handleReload = () => {
43
+ * api.message().reload();
44
+ * };
45
+ * const handleEdit = () => {
46
+ * api.message().startEdit();
47
+ * };
31
48
  * return (
32
49
  * <div>
33
50
  * <button onClick={handleReload}>Reload</button>
@@ -59,6 +76,8 @@ export function useMessageRuntime(options?: {
59
76
  }
60
77
 
61
78
  /**
79
+ * @deprecated Use `useAssistantState(({ message }) => message)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
80
+ *
62
81
  * Hook to access the current message state.
63
82
  *
64
83
  * This hook provides reactive access to the message's state, including content,
@@ -69,11 +88,23 @@ export function useMessageRuntime(options?: {
69
88
  *
70
89
  * @example
71
90
  * ```tsx
91
+ * // Before:
72
92
  * function MessageContent() {
73
93
  * const role = useMessage((state) => state.role);
74
94
  * const content = useMessage((state) => state.content);
75
95
  * const isLoading = useMessage((state) => state.status.type === "running");
96
+ * return (
97
+ * <div className={`message-${role}`}>
98
+ * {isLoading ? "Loading..." : content.map(part => part.text).join("")}
99
+ * </div>
100
+ * );
101
+ * }
76
102
  *
103
+ * // After:
104
+ * function MessageContent() {
105
+ * const role = useAssistantState(({ message }) => message.role);
106
+ * const content = useAssistantState(({ message }) => message.content);
107
+ * const isLoading = useAssistantState(({ message }) => message.status.type === "running");
77
108
  * return (
78
109
  * <div className={`message-${role}`}>
79
110
  * {isLoading ? "Loading..." : content.map(part => part.text).join("")}
@@ -87,6 +118,10 @@ export const useMessage = createStateHookForRuntime(useMessageRuntime);
87
118
  const useEditComposerRuntime = (opt: {
88
119
  optional: boolean | undefined;
89
120
  }): EditComposerRuntime | null => useMessageRuntime(opt)?.composer ?? null;
121
+
122
+ /**
123
+ * @deprecated Use `useAssistantState(({ message }) => message.composer)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
124
+ */
90
125
  export const useEditComposer = createStateHookForRuntime(
91
126
  useEditComposerRuntime,
92
127
  );
@@ -4,6 +4,9 @@ import { MessagePartRuntime } from "../runtime/MessagePartRuntime";
4
4
  import { createStateHookForRuntime } from "../../context/react/utils/createStateHookForRuntime";
5
5
  import { useAssistantApi, useAssistantState } from "../../context/react";
6
6
 
7
+ /**
8
+ * @deprecated Use `useAssistantApi()` with `api.part()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
9
+ */
7
10
  export function useMessagePartRuntime(options?: {
8
11
  optional?: false | undefined;
9
12
  }): MessagePartRuntime;
@@ -23,4 +26,7 @@ export function useMessagePartRuntime(options?: {
23
26
  return runtime;
24
27
  }
25
28
 
29
+ /**
30
+ * @deprecated Use `useAssistantState(({ part }) => part)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
31
+ */
26
32
  export const useMessagePart = createStateHookForRuntime(useMessagePartRuntime);
@@ -12,6 +12,8 @@ import {
12
12
  } from "../../context/react";
13
13
 
14
14
  /**
15
+ * @deprecated Use `useAssistantApi()` with `api.thread()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
16
+ *
15
17
  * Hook to access the ThreadRuntime from the current context.
16
18
  *
17
19
  * The ThreadRuntime provides access to thread-level state and actions,
@@ -23,13 +25,21 @@ import {
23
25
  *
24
26
  * @example
25
27
  * ```tsx
28
+ * // Before:
26
29
  * function MyComponent() {
27
30
  * const runtime = useThreadRuntime();
28
- *
29
31
  * const handleSendMessage = (text: string) => {
30
32
  * runtime.append({ role: "user", content: [{ type: "text", text }] });
31
33
  * };
34
+ * return <button onClick={() => handleSendMessage("Hello!")}>Send</button>;
35
+ * }
32
36
  *
37
+ * // After:
38
+ * function MyComponent() {
39
+ * const api = useAssistantApi();
40
+ * const handleSendMessage = (text: string) => {
41
+ * api.thread().append({ role: "user", content: [{ type: "text", text }] });
42
+ * };
33
43
  * return <button onClick={() => handleSendMessage("Hello!")}>Send</button>;
34
44
  * }
35
45
  * ```
@@ -52,6 +62,8 @@ export function useThreadRuntime(options?: { optional?: boolean | undefined }) {
52
62
  }
53
63
 
54
64
  /**
65
+ * @deprecated Use `useAssistantState(({ thread }) => thread)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
66
+ *
55
67
  * Hook to access the current thread state.
56
68
  *
57
69
  * This hook provides reactive access to the thread's state, including messages,
@@ -62,10 +74,17 @@ export function useThreadRuntime(options?: { optional?: boolean | undefined }) {
62
74
  *
63
75
  * @example
64
76
  * ```tsx
77
+ * // Before:
65
78
  * function ThreadStatus() {
66
79
  * const isRunning = useThread((state) => state.isRunning);
67
80
  * const messageCount = useThread((state) => state.messages.length);
81
+ * return <div>Running: {isRunning}, Messages: {messageCount}</div>;
82
+ * }
68
83
  *
84
+ * // After:
85
+ * function ThreadStatus() {
86
+ * const isRunning = useAssistantState(({ thread }) => thread.isRunning);
87
+ * const messageCount = useAssistantState(({ thread }) => thread.messages.length);
69
88
  * return <div>Running: {isRunning}, Messages: {messageCount}</div>;
70
89
  * }
71
90
  * ```
@@ -75,10 +94,17 @@ export const useThread = createStateHookForRuntime(useThreadRuntime);
75
94
  const useThreadComposerRuntime = (opt: {
76
95
  optional: boolean | undefined;
77
96
  }): ThreadComposerRuntime | null => useThreadRuntime(opt)?.composer ?? null;
97
+
98
+ /**
99
+ * @deprecated Use `useAssistantState(({ thread }) => thread.composer)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
100
+ */
78
101
  export const useThreadComposer = createStateHookForRuntime(
79
102
  useThreadComposerRuntime,
80
103
  );
81
104
 
105
+ /**
106
+ * @deprecated Use `useAssistantState(({ thread }) => thread.modelContext)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
107
+ */
82
108
  export function useThreadModelContext(options?: {
83
109
  optional?: false | undefined;
84
110
  }): ModelContext;
@@ -4,6 +4,9 @@ import { ThreadListItemRuntime } from "../runtime/ThreadListItemRuntime";
4
4
  import { createStateHookForRuntime } from "../../context/react/utils/createStateHookForRuntime";
5
5
  import { useAssistantApi, useAssistantState } from "../../context/react";
6
6
 
7
+ /**
8
+ * @deprecated Use `useAssistantApi()` with `api.threadListItem()` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
9
+ */
7
10
  export function useThreadListItemRuntime(options?: {
8
11
  optional?: false | undefined;
9
12
  }): ThreadListItemRuntime;
@@ -25,6 +28,9 @@ export function useThreadListItemRuntime(options?: {
25
28
  return runtime;
26
29
  }
27
30
 
31
+ /**
32
+ * @deprecated Use `useAssistantState(({ threadListItem }) => threadListItem)` instead. See migration guide: https://docs.assistant-ui.com/docs/migrations/v0-12
33
+ */
28
34
  export const useThreadListItem = createStateHookForRuntime(
29
35
  useThreadListItemRuntime,
30
36
  );
@@ -1,8 +1,10 @@
1
1
  export {
2
2
  useAssistantTransportRuntime,
3
3
  useAssistantTransportSendCommand,
4
+ useAssistantTransportState,
4
5
  } from "./useAssistantTransportRuntime";
5
6
  export type {
6
7
  AssistantTransportConnectionMetadata,
7
8
  AssistantTransportCommand,
9
+ AssistantTransportProtocol,
8
10
  } from "./types";
@@ -79,10 +79,13 @@ export type QueuedCommand = AssistantTransportCommand;
79
79
  // Options types
80
80
  export type HeadersValue = Record<string, string> | Headers;
81
81
 
82
+ export type AssistantTransportProtocol = "data-stream" | "assistant-transport";
83
+
82
84
  export type AssistantTransportOptions<T> = {
83
85
  initialState: T;
84
86
  api: string;
85
87
  resumeApi?: string;
88
+ protocol?: AssistantTransportProtocol;
86
89
  converter: AssistantTransportStateConverter<T>;
87
90
  headers: HeadersValue | (() => Promise<HeadersValue>);
88
91
  body?: object;