@assistant-ui/react 0.11.5 → 0.11.7

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 (168) hide show
  1. package/dist/client/AssistantClient.d.ts.map +1 -1
  2. package/dist/client/AssistantClient.js +1 -2
  3. package/dist/client/AssistantClient.js.map +1 -1
  4. package/dist/client/ToolUIClient.d.ts +3 -2
  5. package/dist/client/ToolUIClient.d.ts.map +1 -1
  6. package/dist/client/ToolUIClient.js +1 -5
  7. package/dist/client/ToolUIClient.js.map +1 -1
  8. package/dist/context/providers/AttachmentProvider.js +3 -3
  9. package/dist/context/providers/AttachmentProvider.js.map +1 -1
  10. package/dist/context/providers/MessageProvider.js +2 -2
  11. package/dist/context/providers/MessageProvider.js.map +1 -1
  12. package/dist/context/providers/PartProvider.js +2 -2
  13. package/dist/context/providers/PartProvider.js.map +1 -1
  14. package/dist/context/providers/TextMessagePartProvider.d.ts.map +1 -1
  15. package/dist/context/providers/TextMessagePartProvider.js +6 -7
  16. package/dist/context/providers/TextMessagePartProvider.js.map +1 -1
  17. package/dist/context/providers/ThreadListItemProvider.js +3 -3
  18. package/dist/context/providers/ThreadListItemProvider.js.map +1 -1
  19. package/dist/context/react/AssistantApiContext.d.ts +1 -1
  20. package/dist/context/react/AssistantApiContext.d.ts.map +1 -1
  21. package/dist/context/react/AssistantApiContext.js +4 -3
  22. package/dist/context/react/AssistantApiContext.js.map +1 -1
  23. package/dist/context/react/index.d.ts +1 -0
  24. package/dist/context/react/index.d.ts.map +1 -1
  25. package/dist/context/react/index.js +2 -0
  26. package/dist/context/react/index.js.map +1 -1
  27. package/dist/legacy-runtime/AssistantRuntimeProvider.d.ts.map +1 -1
  28. package/dist/legacy-runtime/AssistantRuntimeProvider.js +1 -5
  29. package/dist/legacy-runtime/AssistantRuntimeProvider.js.map +1 -1
  30. package/dist/legacy-runtime/client/AttachmentRuntimeClient.d.ts +2 -4
  31. package/dist/legacy-runtime/client/AttachmentRuntimeClient.d.ts.map +1 -1
  32. package/dist/legacy-runtime/client/AttachmentRuntimeClient.js +10 -10
  33. package/dist/legacy-runtime/client/AttachmentRuntimeClient.js.map +1 -1
  34. package/dist/legacy-runtime/client/ComposerRuntimeClient.d.ts +3 -2
  35. package/dist/legacy-runtime/client/ComposerRuntimeClient.d.ts.map +1 -1
  36. package/dist/legacy-runtime/client/ComposerRuntimeClient.js +1 -5
  37. package/dist/legacy-runtime/client/ComposerRuntimeClient.js.map +1 -1
  38. package/dist/legacy-runtime/client/MessagePartRuntimeClient.d.ts +1 -1
  39. package/dist/legacy-runtime/client/MessagePartRuntimeClient.d.ts.map +1 -1
  40. package/dist/legacy-runtime/client/MessagePartRuntimeClient.js +10 -10
  41. package/dist/legacy-runtime/client/MessagePartRuntimeClient.js.map +1 -1
  42. package/dist/legacy-runtime/client/MessageRuntimeClient.d.ts +3 -3
  43. package/dist/legacy-runtime/client/MessageRuntimeClient.d.ts.map +1 -1
  44. package/dist/legacy-runtime/client/MessageRuntimeClient.js +31 -31
  45. package/dist/legacy-runtime/client/MessageRuntimeClient.js.map +1 -1
  46. package/dist/legacy-runtime/client/ThreadListItemRuntimeClient.d.ts +2 -2
  47. package/dist/legacy-runtime/client/ThreadListItemRuntimeClient.d.ts.map +1 -1
  48. package/dist/legacy-runtime/client/ThreadListItemRuntimeClient.js +17 -17
  49. package/dist/legacy-runtime/client/ThreadListItemRuntimeClient.js.map +1 -1
  50. package/dist/legacy-runtime/client/ThreadListRuntimeClient.d.ts +3 -2
  51. package/dist/legacy-runtime/client/ThreadListRuntimeClient.d.ts.map +1 -1
  52. package/dist/legacy-runtime/client/ThreadListRuntimeClient.js +1 -5
  53. package/dist/legacy-runtime/client/ThreadListRuntimeClient.js.map +1 -1
  54. package/dist/legacy-runtime/client/ThreadRuntimeClient.d.ts +3 -2
  55. package/dist/legacy-runtime/client/ThreadRuntimeClient.d.ts.map +1 -1
  56. package/dist/legacy-runtime/client/ThreadRuntimeClient.js +1 -5
  57. package/dist/legacy-runtime/client/ThreadRuntimeClient.js.map +1 -1
  58. package/dist/legacy-runtime/runtime/ThreadRuntime.d.ts +8 -1
  59. package/dist/legacy-runtime/runtime/ThreadRuntime.d.ts.map +1 -1
  60. package/dist/legacy-runtime/runtime/ThreadRuntime.js +5 -1
  61. package/dist/legacy-runtime/runtime/ThreadRuntime.js.map +1 -1
  62. package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.d.ts +12 -0
  63. package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.d.ts.map +1 -0
  64. package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.js +52 -0
  65. package/dist/legacy-runtime/runtime-cores/assistant-transport/commandQueue.js.map +1 -0
  66. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts +3 -0
  67. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.d.ts.map +1 -0
  68. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js +6 -0
  69. package/dist/legacy-runtime/runtime-cores/assistant-transport/index.js.map +1 -0
  70. package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.d.ts +11 -0
  71. package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.d.ts.map +1 -0
  72. package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.js +55 -0
  73. package/dist/legacy-runtime/runtime-cores/assistant-transport/runManager.js.map +1 -0
  74. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts +66 -0
  75. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.d.ts.map +1 -0
  76. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.js +1 -0
  77. package/dist/legacy-runtime/runtime-cores/assistant-transport/types.js.map +1 -0
  78. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts +7 -0
  79. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.d.ts.map +1 -0
  80. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +185 -0
  81. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -0
  82. package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.d.ts +3 -0
  83. package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.d.ts.map +1 -0
  84. package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.js +12 -0
  85. package/dist/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.js.map +1 -0
  86. package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.d.ts +4 -0
  87. package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.d.ts.map +1 -0
  88. package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.js +13 -0
  89. package/dist/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.js.map +1 -0
  90. package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts +13 -0
  91. package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.d.ts.map +1 -0
  92. package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js +138 -0
  93. package/dist/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.js.map +1 -0
  94. package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts +13 -0
  95. package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.d.ts.map +1 -0
  96. package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js +32 -0
  97. package/dist/legacy-runtime/runtime-cores/assistant-transport/utils.js.map +1 -0
  98. package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.d.ts +1 -0
  99. package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.d.ts.map +1 -1
  100. package/dist/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.js.map +1 -1
  101. package/dist/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.d.ts +2 -1
  102. package/dist/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.d.ts.map +1 -1
  103. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.d.ts +3 -1
  104. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.d.ts.map +1 -1
  105. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.d.ts +3 -2
  106. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.d.ts.map +1 -1
  107. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js +9 -2
  108. package/dist/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.js.map +1 -1
  109. package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.d.ts.map +1 -1
  110. package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.js +2 -2
  111. package/dist/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.js.map +1 -1
  112. package/dist/legacy-runtime/runtime-cores/index.d.ts +1 -0
  113. package/dist/legacy-runtime/runtime-cores/index.d.ts.map +1 -1
  114. package/dist/legacy-runtime/runtime-cores/index.js +1 -0
  115. package/dist/legacy-runtime/runtime-cores/index.js.map +1 -1
  116. package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.d.ts +1 -0
  117. package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.d.ts.map +1 -1
  118. package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.js +5 -0
  119. package/dist/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.js.map +1 -1
  120. package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.d.ts.map +1 -1
  121. package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.js +3 -0
  122. package/dist/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.js.map +1 -1
  123. package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts +2 -0
  124. package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListHookInstanceManager.d.ts.map +1 -1
  125. package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts +2 -0
  126. package/dist/legacy-runtime/runtime-cores/remote-thread-list/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -1
  127. package/dist/utils/tap-store/tap-api.d.ts +9 -1
  128. package/dist/utils/tap-store/tap-api.d.ts.map +1 -1
  129. package/dist/utils/tap-store/tap-api.js +12 -2
  130. package/dist/utils/tap-store/tap-api.js.map +1 -1
  131. package/package.json +1 -1
  132. package/src/client/AssistantClient.ts +1 -4
  133. package/src/client/ToolUIClient.ts +1 -6
  134. package/src/context/providers/AttachmentProvider.tsx +3 -3
  135. package/src/context/providers/MessageProvider.tsx +2 -2
  136. package/src/context/providers/PartProvider.tsx +2 -2
  137. package/src/context/providers/TextMessagePartProvider.tsx +5 -7
  138. package/src/context/providers/ThreadListItemProvider.tsx +3 -3
  139. package/src/context/react/AssistantApiContext.tsx +7 -2
  140. package/src/context/react/index.ts +2 -0
  141. package/src/legacy-runtime/AssistantRuntimeProvider.tsx +1 -15
  142. package/src/legacy-runtime/client/AttachmentRuntimeClient.ts +10 -11
  143. package/src/legacy-runtime/client/ComposerRuntimeClient.ts +1 -6
  144. package/src/legacy-runtime/client/MessagePartRuntimeClient.ts +13 -14
  145. package/src/legacy-runtime/client/MessageRuntimeClient.ts +35 -36
  146. package/src/legacy-runtime/client/ThreadListItemRuntimeClient.ts +17 -18
  147. package/src/legacy-runtime/client/ThreadListRuntimeClient.ts +1 -6
  148. package/src/legacy-runtime/client/ThreadRuntimeClient.ts +1 -6
  149. package/src/legacy-runtime/runtime/ThreadRuntime.ts +14 -2
  150. package/src/legacy-runtime/runtime-cores/assistant-transport/commandQueue.ts +62 -0
  151. package/src/legacy-runtime/runtime-cores/assistant-transport/index.ts +2 -0
  152. package/src/legacy-runtime/runtime-cores/assistant-transport/runManager.ts +67 -0
  153. package/src/legacy-runtime/runtime-cores/assistant-transport/types.ts +96 -0
  154. package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransport.spec.md +125 -0
  155. package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.tsx +232 -0
  156. package/src/legacy-runtime/runtime-cores/assistant-transport/useConvertedState.ts +18 -0
  157. package/src/legacy-runtime/runtime-cores/assistant-transport/useLatestRef.ts +9 -0
  158. package/src/legacy-runtime/runtime-cores/assistant-transport/useToolInvocations.ts +173 -0
  159. package/src/legacy-runtime/runtime-cores/assistant-transport/utils.ts +42 -0
  160. package/src/legacy-runtime/runtime-cores/core/BaseThreadRuntimeCore.tsx +1 -0
  161. package/src/legacy-runtime/runtime-cores/core/ThreadRuntimeCore.tsx +2 -1
  162. package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreAdapter.tsx +3 -0
  163. package/src/legacy-runtime/runtime-cores/external-store/ExternalStoreThreadRuntimeCore.tsx +13 -2
  164. package/src/legacy-runtime/runtime-cores/external-store/ThreadMessageLike.tsx +2 -5
  165. package/src/legacy-runtime/runtime-cores/index.ts +1 -0
  166. package/src/legacy-runtime/runtime-cores/local/LocalThreadRuntimeCore.tsx +6 -0
  167. package/src/legacy-runtime/runtime-cores/remote-thread-list/EMPTY_THREAD_CORE.tsx +4 -0
  168. package/src/utils/tap-store/tap-api.ts +19 -2
@@ -7,21 +7,20 @@ export const MessagePartClient = resource(
7
7
  ({ runtime }: { runtime: MessagePartRuntime }) => {
8
8
  const runtimeState = tapSubscribable(runtime);
9
9
 
10
- const api = tapApi<MessagePartClientApi>({
11
- getState: () => runtimeState,
10
+ return tapApi<MessagePartClientApi>(
11
+ {
12
+ getState: () => runtimeState,
12
13
 
13
- addToolResult: (result) => runtime.addToolResult(result),
14
+ addToolResult: (result) => runtime.addToolResult(result),
14
15
 
15
- __internal_getRuntime: () => runtime,
16
- });
17
-
18
- return {
19
- key:
20
- runtimeState.type === "tool-call"
21
- ? "toolCallId-" + runtimeState.toolCallId
22
- : undefined,
23
- state: runtimeState,
24
- api,
25
- };
16
+ __internal_getRuntime: () => runtime,
17
+ },
18
+ {
19
+ key:
20
+ runtimeState.type === "tool-call"
21
+ ? "toolCallId-" + runtimeState.toolCallId
22
+ : undefined,
23
+ },
24
+ );
26
25
  },
27
26
  );
@@ -99,44 +99,43 @@ export const MessageClient = resource(
99
99
  isHoveringState,
100
100
  ]);
101
101
 
102
- const api = tapApi<MessageClientApi>({
103
- getState: () => state,
104
-
105
- composer: composer.api,
106
-
107
- reload: (config) => runtime.reload(config),
108
- speak: () => runtime.speak(),
109
- stopSpeaking: () => runtime.stopSpeaking(),
110
- submitFeedback: (feedback) => runtime.submitFeedback(feedback),
111
- switchToBranch: (options) => runtime.switchToBranch(options),
112
- getCopyText: () => runtime.unstable_getCopyText(),
113
-
114
- part: (selector) => {
115
- if ("index" in selector) {
116
- return parts.api({ index: selector.index });
117
- } else {
118
- return parts.api({ key: "toolCallId-" + selector.toolCallId });
119
- }
120
- },
121
-
122
- attachment: (selector) => {
123
- if ("id" in selector) {
124
- return attachments.api({ key: selector.id });
125
- } else {
126
- return attachments.api(selector);
127
- }
128
- },
102
+ return tapApi<MessageClientApi>(
103
+ {
104
+ getState: () => state,
105
+
106
+ composer: composer.api,
107
+
108
+ reload: (config) => runtime.reload(config),
109
+ speak: () => runtime.speak(),
110
+ stopSpeaking: () => runtime.stopSpeaking(),
111
+ submitFeedback: (feedback) => runtime.submitFeedback(feedback),
112
+ switchToBranch: (options) => runtime.switchToBranch(options),
113
+ getCopyText: () => runtime.unstable_getCopyText(),
114
+
115
+ part: (selector) => {
116
+ if ("index" in selector) {
117
+ return parts.api({ index: selector.index });
118
+ } else {
119
+ return parts.api({ key: "toolCallId-" + selector.toolCallId });
120
+ }
121
+ },
129
122
 
130
- setIsCopied,
131
- setIsHovering,
123
+ attachment: (selector) => {
124
+ if ("id" in selector) {
125
+ return attachments.api({ key: selector.id });
126
+ } else {
127
+ return attachments.api(selector);
128
+ }
129
+ },
132
130
 
133
- __internal_getRuntime: () => runtime,
134
- });
131
+ setIsCopied,
132
+ setIsHovering,
135
133
 
136
- return {
137
- key: runtimeState.id,
138
- state,
139
- api,
140
- };
134
+ __internal_getRuntime: () => runtime,
135
+ },
136
+ {
137
+ key: runtimeState.id,
138
+ },
139
+ );
141
140
  },
142
141
  );
@@ -38,23 +38,22 @@ export const ThreadListItemClient = resource(
38
38
  };
39
39
  }, [runtime, events]);
40
40
 
41
- const api = tapApi<ThreadListItemClientApi>({
42
- getState: () => runtimeState,
43
- switchTo: runtime.switchTo,
44
- rename: runtime.rename,
45
- archive: runtime.archive,
46
- unarchive: runtime.unarchive,
47
- delete: runtime.delete,
48
- generateTitle: runtime.generateTitle,
49
- initialize: runtime.initialize,
50
- detach: runtime.detach,
51
- __internal_getRuntime: () => runtime,
52
- });
53
-
54
- return {
55
- state: runtimeState,
56
- api,
57
- key: runtimeState.id,
58
- };
41
+ return tapApi<ThreadListItemClientApi>(
42
+ {
43
+ getState: () => runtimeState,
44
+ switchTo: runtime.switchTo,
45
+ rename: runtime.rename,
46
+ archive: runtime.archive,
47
+ unarchive: runtime.unarchive,
48
+ delete: runtime.delete,
49
+ generateTitle: runtime.generateTitle,
50
+ initialize: runtime.initialize,
51
+ detach: runtime.detach,
52
+ __internal_getRuntime: () => runtime,
53
+ },
54
+ {
55
+ key: runtimeState.id,
56
+ },
57
+ );
59
58
  },
60
59
  );
@@ -53,7 +53,7 @@ export const ThreadListClient = resource(
53
53
  };
54
54
  }, [runtimeState, threadItems.state, main.state]);
55
55
 
56
- const api = tapApi<ThreadListClientApi>({
56
+ return tapApi<ThreadListClientApi>({
57
57
  getState: () => state,
58
58
 
59
59
  thread: () => main.api,
@@ -81,10 +81,5 @@ export const ThreadListClient = resource(
81
81
  runtime.switchToNewThread();
82
82
  },
83
83
  });
84
-
85
- return {
86
- state,
87
- api,
88
- };
89
84
  },
90
85
  );
@@ -111,7 +111,7 @@ export const ThreadClient = resource(
111
111
  };
112
112
  }, [runtimeState, messages, composer.state]);
113
113
 
114
- const api = tapApi<ThreadClientApi>({
114
+ return tapApi<ThreadClientApi>({
115
115
  getState: () => state,
116
116
 
117
117
  composer: composer.api,
@@ -136,10 +136,5 @@ export const ThreadClient = resource(
136
136
 
137
137
  __internal_getRuntime: () => runtime,
138
138
  });
139
-
140
- return {
141
- state,
142
- api,
143
- };
144
139
  },
145
140
  );
@@ -43,7 +43,7 @@ export type CreateStartRunConfig = {
43
43
  };
44
44
 
45
45
  export type CreateResumeRunConfig = CreateStartRunConfig & {
46
- stream: (
46
+ stream?: (
47
47
  options: ChatModelRunOptions,
48
48
  ) => AsyncGenerator<ChatModelRunResult, void, unknown>;
49
49
  };
@@ -53,7 +53,7 @@ const toResumeRunConfig = (message: CreateResumeRunConfig): ResumeRunConfig => {
53
53
  parentId: message.parentId ?? null,
54
54
  sourceId: message.sourceId ?? null,
55
55
  runConfig: message.runConfig ?? {},
56
- stream: message.stream,
56
+ ...(message.stream ? { stream: message.stream } : {}),
57
57
  };
58
58
  };
59
59
 
@@ -256,6 +256,12 @@ export type ThreadRuntime = {
256
256
  **/
257
257
  unstable_resumeRun(config: CreateResumeRunConfig): void;
258
258
 
259
+ /**
260
+ * Load external state into the thread.
261
+ * @param state The state to load into the thread
262
+ */
263
+ unstable_loadExternalState(state: any): void;
264
+
259
265
  subscribe(callback: () => void): Unsubscribe;
260
266
  cancelRun(): void;
261
267
  getModelContext(): ModelContext;
@@ -346,6 +352,8 @@ export class ThreadRuntimeImpl implements ThreadRuntime {
346
352
  protected __internal_bindMethods() {
347
353
  this.append = this.append.bind(this);
348
354
  this.unstable_resumeRun = this.unstable_resumeRun.bind(this);
355
+ this.unstable_loadExternalState =
356
+ this.unstable_loadExternalState.bind(this);
349
357
  this.startRun = this.startRun.bind(this);
350
358
  this.cancelRun = this.cancelRun.bind(this);
351
359
  this.stopSpeaking = this.stopSpeaking.bind(this);
@@ -399,6 +407,10 @@ export class ThreadRuntimeImpl implements ThreadRuntime {
399
407
  return this._threadBinding.getState().resumeRun(toResumeRunConfig(config));
400
408
  }
401
409
 
410
+ public unstable_loadExternalState(state: any) {
411
+ this._threadBinding.getState().unstable_loadExternalState(state);
412
+ }
413
+
402
414
  public cancelRun() {
403
415
  this._threadBinding.getState().cancelRun();
404
416
  }
@@ -0,0 +1,62 @@
1
+ import { useState, useCallback, useRef, useEffect } from "react";
2
+ import {
3
+ AssistantTransportCommand,
4
+ CommandQueueState,
5
+ QueuedCommand,
6
+ } from "./types";
7
+
8
+ export const createInitialQueueState = (): CommandQueueState => ({
9
+ queued: [],
10
+ inTransit: [],
11
+ });
12
+
13
+ export const useCommandQueue = (opts: { onQueue: () => void }) => {
14
+ const onQueueRef = useRef(opts.onQueue);
15
+ useEffect(() => {
16
+ onQueueRef.current = opts.onQueue;
17
+ });
18
+
19
+ const [, rerender] = useState(0);
20
+ const queueStateRef = useRef<CommandQueueState>(createInitialQueueState());
21
+
22
+ const enqueue = (command: AssistantTransportCommand) => {
23
+ queueStateRef.current = {
24
+ queued: [...queueStateRef.current.queued, command],
25
+ inTransit: queueStateRef.current.inTransit,
26
+ };
27
+ rerender((prev) => prev + 1);
28
+
29
+ onQueueRef.current();
30
+ };
31
+
32
+ const flush = (): QueuedCommand[] => {
33
+ if (queueStateRef.current.queued.length === 0) return [];
34
+
35
+ const queued = queueStateRef.current.queued;
36
+ queueStateRef.current = {
37
+ queued: [],
38
+ inTransit: [...queueStateRef.current.inTransit, ...queued],
39
+ };
40
+ rerender((prev) => prev + 1);
41
+
42
+ return queued;
43
+ };
44
+
45
+ const markDelivered = () => {
46
+ queueStateRef.current = { ...queueStateRef.current, inTransit: [] };
47
+ rerender((prev) => prev + 1);
48
+ };
49
+
50
+ const reset = useCallback(() => {
51
+ queueStateRef.current = createInitialQueueState();
52
+ rerender((prev) => prev + 1);
53
+ }, []);
54
+
55
+ return {
56
+ state: queueStateRef.current,
57
+ enqueue,
58
+ flush,
59
+ markDelivered,
60
+ reset,
61
+ };
62
+ };
@@ -0,0 +1,2 @@
1
+ export { useAssistantTransportRuntime } from "./useAssistantTransportRuntime";
2
+ export type { AssistantTransportConnectionMetadata } from "./types";
@@ -0,0 +1,67 @@
1
+ import { useCallback, useRef, useState } from "react";
2
+ import { useLatestRef } from "./useLatestRef";
3
+
4
+ export type RunManager = Readonly<{
5
+ isRunning: boolean;
6
+ schedule: () => void;
7
+ cancel: () => void;
8
+ }>;
9
+
10
+ export function useRunManager(config: {
11
+ onRun: (signal: AbortSignal) => Promise<void>;
12
+ onFinish?: (() => void) | undefined;
13
+ onError?: ((error: Error) => void) | undefined;
14
+ }): RunManager {
15
+ const [isRunning, setIsRunning] = useState(false);
16
+ const stateRef = useRef({
17
+ pending: false,
18
+ abortController: null as AbortController | null,
19
+ });
20
+ const onRunRef = useLatestRef(config.onRun);
21
+ const onFinishRef = useLatestRef(config.onFinish);
22
+ const onErrorRef = useLatestRef(config.onError);
23
+
24
+ const startRun = useCallback(() => {
25
+ setIsRunning(true);
26
+ stateRef.current.pending = false;
27
+ const ac = new AbortController();
28
+ stateRef.current.abortController = ac;
29
+
30
+ queueMicrotask(async () => {
31
+ try {
32
+ await onRunRef.current(ac.signal);
33
+ } catch (error) {
34
+ stateRef.current.pending = false;
35
+ onErrorRef.current?.(error as Error);
36
+ } finally {
37
+ onFinishRef.current?.();
38
+ if (stateRef.current.pending) {
39
+ startRun();
40
+ } else {
41
+ setIsRunning(false);
42
+ stateRef.current.abortController = null;
43
+ }
44
+ }
45
+ });
46
+ }, [onRunRef, onFinishRef]);
47
+
48
+ const schedule = useCallback(() => {
49
+ if (stateRef.current.abortController) {
50
+ // Coalesce multiple schedules while running into a single follow-up run.
51
+ stateRef.current.pending = true;
52
+ return;
53
+ }
54
+ startRun();
55
+ }, [startRun]);
56
+
57
+ const cancel = useCallback(() => {
58
+ stateRef.current.pending = false;
59
+ stateRef.current.abortController?.abort();
60
+ }, []);
61
+
62
+ return {
63
+ isRunning,
64
+ schedule,
65
+ cancel,
66
+ };
67
+ }
@@ -0,0 +1,96 @@
1
+ import { ReadonlyJSONValue } from "assistant-stream/utils";
2
+ import { ThreadMessage } from "../../../types";
3
+ import { AttachmentAdapter, ThreadHistoryAdapter } from "..";
4
+
5
+ // Message part types
6
+ export type TextPart = {
7
+ readonly type: "text";
8
+ readonly text: string;
9
+ };
10
+
11
+ export type ImagePart = {
12
+ readonly type: "image";
13
+ readonly image: string;
14
+ };
15
+
16
+ export type UserMessagePart = TextPart | ImagePart;
17
+
18
+ // Command types
19
+ export type AddMessageCommand = {
20
+ readonly type: "add-message";
21
+ readonly message: {
22
+ readonly role: "user";
23
+ readonly parts: readonly UserMessagePart[];
24
+ };
25
+ };
26
+
27
+ export type AddToolResultCommand = {
28
+ readonly type: "add-tool-result";
29
+ readonly toolCallId: string;
30
+ readonly toolName: string;
31
+ readonly result: ReadonlyJSONValue;
32
+ readonly isError: boolean;
33
+ readonly artifact?: ReadonlyJSONValue;
34
+ };
35
+
36
+ export type AssistantTransportCommand =
37
+ | AddMessageCommand
38
+ | AddToolResultCommand;
39
+
40
+ // State types
41
+ export type AssistantTransportState = {
42
+ readonly messages: readonly ThreadMessage[];
43
+ readonly isRunning: boolean;
44
+ };
45
+
46
+ export type AssistantTransportConnectionMetadata = {
47
+ pendingCommands: AssistantTransportCommand[];
48
+ isSending: boolean;
49
+ };
50
+
51
+ export type AssistantTransportStateConverter<T> = (
52
+ state: T,
53
+ connectionMetadata: AssistantTransportConnectionMetadata,
54
+ ) => AssistantTransportState;
55
+
56
+ // Queue types
57
+ export type CommandQueueState = {
58
+ queued: AssistantTransportCommand[];
59
+ inTransit: AssistantTransportCommand[];
60
+ };
61
+
62
+ // For now, queued items are plain commands (runConfig not supported)
63
+ export type QueuedCommand = AssistantTransportCommand;
64
+
65
+ // Task types
66
+
67
+ // Options types
68
+ export type HeadersValue = Record<string, string> | Headers;
69
+
70
+ export type AssistantTransportOptions<T> = {
71
+ initialState: T;
72
+ api: string;
73
+ resumeApi?: string;
74
+ converter: AssistantTransportStateConverter<T>;
75
+ headers: HeadersValue | (() => Promise<HeadersValue>);
76
+ body?: object;
77
+ onResponse?: (response: Response) => void;
78
+ onFinish?: () => void;
79
+ onError?: (
80
+ error: Error,
81
+ params: {
82
+ commands: AssistantTransportCommand[];
83
+ updateState: (updater: (state: T) => T) => void;
84
+ },
85
+ ) => void;
86
+ onCancel?: (params: {
87
+ commands: AssistantTransportCommand[];
88
+ updateState: (updater: (state: T) => T) => void;
89
+ }) => void;
90
+ adapters?: {
91
+ attachments?: AttachmentAdapter | undefined;
92
+ history?: ThreadHistoryAdapter | undefined;
93
+ };
94
+ };
95
+
96
+ // (no task or stream-specific types needed in this module)
@@ -0,0 +1,125 @@
1
+ useAssistantTransport
2
+
3
+ Overview
4
+
5
+ - Similar API as `useDataStreamRuntime`.
6
+ - Built on an external-store runtime; the external store issues "commands".
7
+ - Exactly one run is active at a time (single-flight).
8
+ - Runs take queued commands as input and consume an assistant stream that yields state snapshots.
9
+ - Every run flushes the entire command queue; a single run processes all pending commands.
10
+
11
+ Command Scheduling
12
+
13
+ - When commands are enqueued:
14
+ - If a run is in progress: do not start another; mark that a follow-up run is pending.
15
+ - When the current run ends: if commands were scheduled during the run, start a new run and publish them.
16
+ - If no run is in progress: start a run immediately and flush commands to the server.
17
+ - Scheduling uses `queueMicrotask` to coalesce multiple synchronous enqueues into a single run start.
18
+
19
+ Command Queue
20
+
21
+ `useCommandQueue({ onQueue() { runManager.schedule(); } })`
22
+
23
+ - `enqueue(cmd)`: Adds a command to the queue. Calls `onQueue` when transitioning from empty → non-empty (coalesced via `queueMicrotask`).
24
+ - `flush(): Command[]`: Returns all queued commands, moves them into `inTransit`, and clears the queue.
25
+ - Internal state tracks `inTransit: Command[]` and `queued: Command[]`.
26
+
27
+ Run Manager
28
+
29
+ `useRunManager({
30
+ async onRun(signal) {
31
+ const commands = commandQueue.flush();
32
+ setInTransitCommands(commands);
33
+
34
+ try {
35
+ const response = await fetch(backendUrl, { signal });
36
+ const stream = response.body
37
+ .pipeThrough(new DataStreamDecoder())
38
+ .pipeThrough(
39
+ new AssistantMessageAccumulator({
40
+ initialMessage: createInitialMessage({
41
+ unstable_state: (state.state as ReadonlyJSONValue) ?? null,
42
+ }),
43
+ }),
44
+ );
45
+
46
+ for await (const snapshot of stream) {
47
+ // Clear in-transit commands after the first response chunk.
48
+ // Use a stable empty array to avoid unnecessary re-renders.
49
+ setInTransitCommands(EMPTY_ARRAY);
50
+ setSnapshot(snapshot);
51
+ }
52
+ } catch (error) {
53
+ // Do not restore commands. Surface error to onError for state update.
54
+ callbacks.onError?.({
55
+ error,
56
+ commands: getCurrentInTransitCommands(),
57
+ updateState(updater) {
58
+ setSnapshot((prev) => updater(prev));
59
+ },
60
+ });
61
+ }
62
+
63
+ },
64
+ })`
65
+
66
+ - `schedule()`: Starts immediately if idle, or schedules at most one follow-up run to start right after the current run.
67
+ - `cancel()`: Aborts the active run via `signal` and clears any scheduled follow-up run. Does not restore commands.
68
+ - `isRunning: boolean`: Indicates whether a run is currently active (internal to scheduling).
69
+ UI-facing `isRunning` is controlled by the converter output (see Converter).
70
+ - On cancellation, invoke `callbacks.onCancel?.({ commands, updateState })` where `commands` contains all pending work at the time of cancel: `[...inTransitCommands, ...queuedCommands]`. Note: after the first snapshot arrives, `inTransitCommands` are cleared to `[]`, so cancels after first byte will not include them.
71
+ - RunConfig is not supported for now; any provided run configuration is ignored.
72
+
73
+ Converter
74
+
75
+ `useConverter({
76
+ converter,
77
+ agentState,
78
+ queuedCommands,
79
+ inTransitCommands,
80
+ })`
81
+
82
+ - Reactive pattern: do not imperatively set converted state. Maintain an `agentState` snapshot variable (updated via stream), and compute the converted UI state with a memoized converter.
83
+ - Example: `const pending = [...inTransitCommands, ...queuedCommands]; const converted = useMemo(() => converter(agentState, { pendingCommands: pending, isSending }), [agentState, pending, isSending])`
84
+ - `isSending` should be sourced from the run manager’s `isRunning` flag.
85
+ - Returns `AssistantTransportState` with `{ messages, isRunning }` derived from inputs via `converter`.
86
+ - The converter controls UI `isRunning`. Typical mapping: `isRunning = isSending`. Advanced policies are allowed (e.g., extend running while reconciling, or suppress during background tool results).
87
+ - Assistant stream deltas are applied by `AssistantMessageAccumulator`, which emits immutable full-state snapshots; no additional delta handling is required in the converter.
88
+
89
+ Tool Invocations
90
+
91
+ `useToolInvocations({
92
+ messages,
93
+ onResult(result) { commandQueue.enqueue(result); },
94
+ })`
95
+
96
+ - Uses a ToolCall differ to diff tool calls across successive snapshots (e.g., ToolCallDiffer).
97
+ - When a tool call’s argsText transitions from incomplete → complete and `result` is undefined, synthesize a tool-execution event and enqueue an `add-tool-result` command via `onResult`.
98
+ - `onResult` is for frontend function calling (client-side tool calls producing results to enqueue).
99
+ - No return value.
100
+
101
+ External Store Runtime Bridge
102
+
103
+ `useExternalStoreRuntime({
104
+ isRunning,
105
+ messages,
106
+ onNew(command) { commandQueue.enqueue(command); },
107
+ onCancel() { runManager.cancel(); },
108
+ onAddToolResult(result) { commandQueue.enqueue(result); },
109
+ })`
110
+
111
+ - `onAddToolResult` typically reflects userland-triggered results (e.g., human/tool calling) coming from the external store runtime.
112
+
113
+ Notes
114
+
115
+ - Use a stable `EMPTY_ARRAY` when clearing in-transit commands to minimize re-renders via referential equality.
116
+ - "Assistant stream" refers to the incremental response stream that yields state snapshots.
117
+
118
+ Callbacks
119
+
120
+ - `onError({ error, commands, updateState })`: invoked on network/stream errors. Commands are not restored; use `updateState(state => newState)` to reflect the error in state and/or messages. `commands` reflects the current in-transit commands at the moment of error (often `[]` after the first snapshot).
121
+ - `onCancel({ commands, updateState })`: invoked after a cancellation. Commands are not restored. `commands` contains all pending work at cancel time (`inTransitCommands` plus queued). Use `updateState` to reflect cancellation in state and/or messages. The last received snapshot remains committed.
122
+
123
+ Return Value
124
+
125
+ - `useAssistantTransport` returns the runtime object from `useExternalStoreRuntime` (e.g., `{ isRunning, messages, ... }`), rather than a custom wrapper shape.