@assistant-ui/core 0.1.2 → 0.1.3

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 (104) hide show
  1. package/dist/model-context/frame/provider.d.ts.map +1 -1
  2. package/dist/model-context/frame/provider.js +2 -4
  3. package/dist/model-context/frame/provider.js.map +1 -1
  4. package/dist/react/index.d.ts +2 -1
  5. package/dist/react/index.d.ts.map +1 -1
  6. package/dist/react/index.js +2 -1
  7. package/dist/react/index.js.map +1 -1
  8. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts +97 -0
  9. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.d.ts.map +1 -0
  10. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js +111 -0
  11. package/dist/react/runtimes/RemoteThreadListHookInstanceManager.js.map +1 -0
  12. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts +115 -0
  13. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.d.ts.map +1 -0
  14. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js +444 -0
  15. package/dist/react/runtimes/RemoteThreadListThreadListRuntimeCore.js.map +1 -0
  16. package/dist/react/runtimes/RuntimeAdapterProvider.d.ts +18 -0
  17. package/dist/react/runtimes/RuntimeAdapterProvider.d.ts.map +1 -0
  18. package/dist/react/runtimes/RuntimeAdapterProvider.js +14 -0
  19. package/dist/react/runtimes/RuntimeAdapterProvider.js.map +1 -0
  20. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts +5 -0
  21. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.d.ts.map +1 -0
  22. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js +528 -0
  23. package/dist/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.js.map +1 -0
  24. package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.d.ts +15 -0
  25. package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.d.ts.map +1 -0
  26. package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.js +83 -0
  27. package/dist/react/runtimes/cloud/CloudFileAttachmentAdapter.js.map +1 -0
  28. package/dist/react/runtimes/cloud/auiV0.d.ts +62 -0
  29. package/dist/react/runtimes/cloud/auiV0.d.ts.map +1 -0
  30. package/dist/react/runtimes/cloud/auiV0.js +74 -0
  31. package/dist/react/runtimes/cloud/auiV0.js.map +1 -0
  32. package/dist/react/runtimes/cloud/index.d.ts +4 -0
  33. package/dist/react/runtimes/cloud/index.d.ts.map +1 -0
  34. package/dist/react/runtimes/cloud/index.js +4 -0
  35. package/dist/react/runtimes/cloud/index.js.map +1 -0
  36. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts +13 -0
  37. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.d.ts.map +1 -0
  38. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js +102 -0
  39. package/dist/react/runtimes/cloud/useCloudThreadListAdapter.js.map +1 -0
  40. package/dist/react/runtimes/createMessageConverter.d.ts +17 -0
  41. package/dist/react/runtimes/createMessageConverter.d.ts.map +1 -0
  42. package/dist/react/runtimes/createMessageConverter.js +50 -0
  43. package/dist/react/runtimes/createMessageConverter.js.map +1 -0
  44. package/dist/react/runtimes/external-message-converter.d.ts +34 -0
  45. package/dist/react/runtimes/external-message-converter.d.ts.map +1 -0
  46. package/dist/react/runtimes/external-message-converter.js +309 -0
  47. package/dist/react/runtimes/external-message-converter.js.map +1 -0
  48. package/dist/react/runtimes/index.d.ts +10 -0
  49. package/dist/react/runtimes/index.d.ts.map +1 -0
  50. package/dist/react/runtimes/index.js +10 -0
  51. package/dist/react/runtimes/index.js.map +1 -0
  52. package/dist/react/runtimes/useExternalStoreRuntime.d.ts +4 -0
  53. package/dist/react/runtimes/useExternalStoreRuntime.d.ts.map +1 -0
  54. package/dist/react/runtimes/useExternalStoreRuntime.js +19 -0
  55. package/dist/react/runtimes/useExternalStoreRuntime.js.map +1 -0
  56. package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts +4 -0
  57. package/dist/react/runtimes/useRemoteThreadListRuntime.d.ts.map +1 -0
  58. package/dist/react/runtimes/useRemoteThreadListRuntime.js +48 -0
  59. package/dist/react/runtimes/useRemoteThreadListRuntime.js.map +1 -0
  60. package/dist/react/runtimes/useToolInvocations.d.ts +38 -0
  61. package/dist/react/runtimes/useToolInvocations.d.ts.map +1 -0
  62. package/dist/react/runtimes/useToolInvocations.js +411 -0
  63. package/dist/react/runtimes/useToolInvocations.js.map +1 -0
  64. package/dist/react/types/store-augmentation.d.ts +0 -1
  65. package/dist/react/types/store-augmentation.d.ts.map +1 -1
  66. package/dist/react/types/store-augmentation.js +1 -1
  67. package/dist/react/types/store-augmentation.js.map +1 -1
  68. package/dist/runtime/base/base-composer-runtime-core.d.ts.map +1 -1
  69. package/dist/runtime/base/base-composer-runtime-core.js +2 -0
  70. package/dist/runtime/base/base-composer-runtime-core.js.map +1 -1
  71. package/dist/store/index.d.ts +1 -1
  72. package/dist/store/index.d.ts.map +1 -1
  73. package/dist/store/index.js +1 -2
  74. package/dist/store/index.js.map +1 -1
  75. package/dist/utils/json/is-json-equal.d.ts +2 -0
  76. package/dist/utils/json/is-json-equal.d.ts.map +1 -0
  77. package/dist/utils/json/is-json-equal.js +31 -0
  78. package/dist/utils/json/is-json-equal.js.map +1 -0
  79. package/dist/utils/json/is-json.d.ts +6 -0
  80. package/dist/utils/json/is-json.d.ts.map +1 -0
  81. package/dist/utils/json/is-json.js +33 -0
  82. package/dist/utils/json/is-json.js.map +1 -0
  83. package/package.json +10 -9
  84. package/src/model-context/frame/provider.ts +2 -6
  85. package/src/react/index.ts +2 -1
  86. package/src/react/runtimes/RemoteThreadListHookInstanceManager.tsx +176 -0
  87. package/src/react/runtimes/RemoteThreadListThreadListRuntimeCore.tsx +534 -0
  88. package/src/react/runtimes/RuntimeAdapterProvider.tsx +40 -0
  89. package/src/react/runtimes/cloud/AssistantCloudThreadHistoryAdapter.ts +785 -0
  90. package/src/react/runtimes/cloud/CloudFileAttachmentAdapter.ts +101 -0
  91. package/src/react/runtimes/cloud/auiV0.ts +160 -0
  92. package/src/react/runtimes/cloud/index.ts +3 -0
  93. package/src/react/runtimes/cloud/useCloudThreadListAdapter.tsx +152 -0
  94. package/src/react/runtimes/createMessageConverter.ts +77 -0
  95. package/src/react/runtimes/external-message-converter.ts +487 -0
  96. package/src/react/runtimes/index.ts +30 -0
  97. package/src/react/runtimes/useExternalStoreRuntime.ts +27 -0
  98. package/src/react/runtimes/useRemoteThreadListRuntime.ts +76 -0
  99. package/src/react/runtimes/useToolInvocations.ts +594 -0
  100. package/src/react/types/store-augmentation.ts +0 -2
  101. package/src/runtime/base/base-composer-runtime-core.ts +2 -0
  102. package/src/store/index.ts +1 -2
  103. package/src/utils/json/is-json-equal.ts +48 -0
  104. package/src/utils/json/is-json.ts +58 -0
@@ -0,0 +1,534 @@
1
+ import type { ThreadListRuntimeCore } from "../../runtime/interfaces/thread-list-runtime-core";
2
+ import { generateId } from "../../utils/id";
3
+ import { BaseSubscribable } from "../../subscribable";
4
+ import { OptimisticState } from "../../runtimes/remote-thread-list/optimistic-state";
5
+ import { EMPTY_THREAD_CORE } from "../../runtimes/remote-thread-list/empty-thread-core";
6
+ import type {
7
+ RemoteThreadData,
8
+ THREAD_MAPPING_ID,
9
+ RemoteThreadState,
10
+ } from "../../runtimes/remote-thread-list/remote-thread-state";
11
+ import {
12
+ createThreadMappingId,
13
+ getThreadData,
14
+ updateStatusReducer,
15
+ } from "../../runtimes/remote-thread-list/remote-thread-state";
16
+ import type { RemoteThreadListOptions } from "../../runtimes/remote-thread-list/types";
17
+ import { RemoteThreadListHookInstanceManager } from "./RemoteThreadListHookInstanceManager";
18
+ import {
19
+ ComponentType,
20
+ FC,
21
+ Fragment,
22
+ PropsWithChildren,
23
+ useEffect,
24
+ useId,
25
+ } from "react";
26
+ import { create } from "zustand";
27
+ import { AssistantMessageStream } from "assistant-stream";
28
+ import type { ModelContextProvider } from "../../model-context/types";
29
+ import { RuntimeAdapterProvider } from "./RuntimeAdapterProvider";
30
+
31
+ export class RemoteThreadListThreadListRuntimeCore
32
+ extends BaseSubscribable
33
+ implements ThreadListRuntimeCore
34
+ {
35
+ private _options!: RemoteThreadListOptions;
36
+ private readonly _hookManager: RemoteThreadListHookInstanceManager;
37
+
38
+ private _loadThreadsPromise: Promise<void> | undefined;
39
+
40
+ private _mainThreadId!: string;
41
+ private readonly _state = new OptimisticState<RemoteThreadState>({
42
+ isLoading: false,
43
+ newThreadId: undefined,
44
+ threadIds: [],
45
+ archivedThreadIds: [],
46
+ threadIdMap: {},
47
+ threadData: {},
48
+ });
49
+
50
+ public get threadItems() {
51
+ return this._state.value.threadData;
52
+ }
53
+
54
+ public getLoadThreadsPromise() {
55
+ // TODO this needs to be cached in case this promise is loaded during suspense
56
+ if (!this._loadThreadsPromise) {
57
+ this._loadThreadsPromise = this._state
58
+ .optimisticUpdate({
59
+ execute: () => this._options.adapter.list(),
60
+ loading: (state) => {
61
+ return {
62
+ ...state,
63
+ isLoading: true,
64
+ };
65
+ },
66
+ then: (state, l) => {
67
+ const newThreadIds = [];
68
+ const newArchivedThreadIds = [];
69
+ const newThreadIdMap = {} as Record<string, THREAD_MAPPING_ID>;
70
+ const newThreadData = {} as Record<
71
+ THREAD_MAPPING_ID,
72
+ RemoteThreadData
73
+ >;
74
+
75
+ for (const thread of l.threads) {
76
+ switch (thread.status) {
77
+ case "regular":
78
+ newThreadIds.push(thread.remoteId);
79
+ break;
80
+ case "archived":
81
+ newArchivedThreadIds.push(thread.remoteId);
82
+ break;
83
+ default: {
84
+ const _exhaustiveCheck: never = thread.status;
85
+ throw new Error(`Unsupported state: ${_exhaustiveCheck}`);
86
+ }
87
+ }
88
+
89
+ const mappingId = createThreadMappingId(thread.remoteId);
90
+ newThreadIdMap[thread.remoteId] = mappingId;
91
+ newThreadData[mappingId] = {
92
+ id: thread.remoteId,
93
+ remoteId: thread.remoteId,
94
+ externalId: thread.externalId,
95
+ status: thread.status,
96
+ title: thread.title,
97
+ initializeTask: Promise.resolve({
98
+ remoteId: thread.remoteId,
99
+ externalId: thread.externalId,
100
+ }),
101
+ };
102
+ }
103
+
104
+ return {
105
+ ...state,
106
+ threadIds: newThreadIds,
107
+ archivedThreadIds: newArchivedThreadIds,
108
+ threadIdMap: {
109
+ ...state.threadIdMap,
110
+ ...newThreadIdMap,
111
+ },
112
+ threadData: {
113
+ ...state.threadData,
114
+ ...newThreadData,
115
+ },
116
+ };
117
+ },
118
+ })
119
+ .then(() => {});
120
+ }
121
+
122
+ return this._loadThreadsPromise;
123
+ }
124
+
125
+ private readonly contextProvider: ModelContextProvider;
126
+
127
+ constructor(
128
+ options: RemoteThreadListOptions,
129
+ contextProvider: ModelContextProvider,
130
+ ) {
131
+ super();
132
+ this.contextProvider = contextProvider;
133
+
134
+ this._state.subscribe(() => this._notifySubscribers());
135
+ this._hookManager = new RemoteThreadListHookInstanceManager(
136
+ options.runtimeHook,
137
+ this,
138
+ );
139
+ this.useProvider = create(() => ({
140
+ Provider: (options.adapter.unstable_Provider ??
141
+ Fragment) as ComponentType<PropsWithChildren>,
142
+ }));
143
+ this.__internal_setOptions(options);
144
+ this.switchToNewThread();
145
+ }
146
+
147
+ private useProvider;
148
+
149
+ public __internal_setOptions(options: RemoteThreadListOptions) {
150
+ if (this._options === options) return;
151
+
152
+ this._options = options;
153
+
154
+ const Provider = (options.adapter.unstable_Provider ??
155
+ Fragment) as ComponentType<PropsWithChildren>;
156
+ if (Provider !== this.useProvider.getState().Provider) {
157
+ this.useProvider.setState({ Provider }, true);
158
+ }
159
+
160
+ this._hookManager.setRuntimeHook(options.runtimeHook);
161
+ }
162
+
163
+ public __internal_load() {
164
+ this.getLoadThreadsPromise(); // begin loading on initial bind
165
+ }
166
+
167
+ public get isLoading() {
168
+ return this._state.value.isLoading;
169
+ }
170
+
171
+ public get threadIds() {
172
+ return this._state.value.threadIds;
173
+ }
174
+
175
+ public get archivedThreadIds() {
176
+ return this._state.value.archivedThreadIds;
177
+ }
178
+
179
+ public get newThreadId() {
180
+ return this._state.value.newThreadId;
181
+ }
182
+
183
+ public get mainThreadId(): string {
184
+ return this._mainThreadId;
185
+ }
186
+
187
+ public getMainThreadRuntimeCore() {
188
+ const result = this._hookManager.getThreadRuntimeCore(this._mainThreadId);
189
+ if (!result) return EMPTY_THREAD_CORE;
190
+ return result;
191
+ }
192
+
193
+ public getThreadRuntimeCore(threadIdOrRemoteId: string) {
194
+ const data = this.getItemById(threadIdOrRemoteId);
195
+ if (!data) throw new Error("Thread not found");
196
+
197
+ const result = this._hookManager.getThreadRuntimeCore(data.id);
198
+ if (!result) throw new Error("Thread not found");
199
+ return result;
200
+ }
201
+
202
+ public getItemById(threadIdOrRemoteId: string) {
203
+ return getThreadData(this._state.value, threadIdOrRemoteId);
204
+ }
205
+
206
+ public async switchToThread(threadIdOrRemoteId: string): Promise<void> {
207
+ let data = this.getItemById(threadIdOrRemoteId);
208
+
209
+ if (!data) {
210
+ const remoteMetadata =
211
+ await this._options.adapter.fetch(threadIdOrRemoteId);
212
+ const state = this._state.value;
213
+ const mappingId = createThreadMappingId(remoteMetadata.remoteId);
214
+
215
+ const newThreadData = {
216
+ ...state.threadData,
217
+ [mappingId]: {
218
+ id: mappingId,
219
+ initializeTask: Promise.resolve({
220
+ remoteId: remoteMetadata.remoteId,
221
+ externalId: remoteMetadata.externalId,
222
+ }),
223
+ remoteId: remoteMetadata.remoteId,
224
+ externalId: remoteMetadata.externalId,
225
+ status: remoteMetadata.status,
226
+ title: remoteMetadata.title,
227
+ } as RemoteThreadData,
228
+ };
229
+
230
+ const newThreadIdMap = {
231
+ ...state.threadIdMap,
232
+ [remoteMetadata.remoteId]: mappingId,
233
+ };
234
+
235
+ const newThreadIds =
236
+ remoteMetadata.status === "regular"
237
+ ? [...state.threadIds, remoteMetadata.remoteId]
238
+ : state.threadIds;
239
+
240
+ const newArchivedThreadIds =
241
+ remoteMetadata.status === "archived"
242
+ ? [...state.archivedThreadIds, remoteMetadata.remoteId]
243
+ : state.archivedThreadIds;
244
+
245
+ this._state.update({
246
+ ...state,
247
+ threadIds: newThreadIds,
248
+ archivedThreadIds: newArchivedThreadIds,
249
+ threadIdMap: newThreadIdMap,
250
+ threadData: newThreadData,
251
+ });
252
+
253
+ data = this.getItemById(threadIdOrRemoteId);
254
+ }
255
+
256
+ if (!data) throw new Error("Thread not found");
257
+ if (this._mainThreadId === data.id) return;
258
+
259
+ const task = this._hookManager.startThreadRuntime(data.id);
260
+ if (this.mainThreadId !== undefined) {
261
+ await task;
262
+ } else {
263
+ task.then(() => this._notifySubscribers());
264
+ }
265
+
266
+ if (data.status === "archived") await this.unarchive(data.id);
267
+ this._mainThreadId = data.id;
268
+
269
+ this._notifySubscribers();
270
+ }
271
+
272
+ public async switchToNewThread(): Promise<void> {
273
+ // an initialization transaction is in progress, wait for it to settle
274
+ while (
275
+ this._state.baseValue.newThreadId !== undefined &&
276
+ this._state.value.newThreadId === undefined
277
+ ) {
278
+ await this._state.waitForUpdate();
279
+ }
280
+
281
+ const state = this._state.value;
282
+ let id: string | undefined = this._state.value.newThreadId;
283
+ if (id === undefined) {
284
+ do {
285
+ id = `__LOCALID_${generateId()}`;
286
+ } while (state.threadIdMap[id]);
287
+
288
+ const mappingId = createThreadMappingId(id);
289
+ this._state.update({
290
+ ...state,
291
+ newThreadId: id,
292
+ threadIdMap: {
293
+ ...state.threadIdMap,
294
+ [id]: mappingId,
295
+ },
296
+ threadData: {
297
+ ...state.threadData,
298
+ [mappingId]: {
299
+ status: "new",
300
+ id,
301
+ remoteId: undefined,
302
+ externalId: undefined,
303
+ title: undefined,
304
+ } satisfies RemoteThreadData,
305
+ },
306
+ });
307
+ }
308
+
309
+ return this.switchToThread(id);
310
+ }
311
+
312
+ public initialize = async (threadId: string) => {
313
+ if (this._state.value.newThreadId !== threadId) {
314
+ const data = this.getItemById(threadId);
315
+ if (!data) throw new Error("Thread not found");
316
+ if (data.status === "new") throw new Error("Unexpected new state");
317
+ return data.initializeTask;
318
+ }
319
+
320
+ return this._state.optimisticUpdate({
321
+ execute: () => {
322
+ return this._options.adapter.initialize(threadId);
323
+ },
324
+ optimistic: (state) => {
325
+ return updateStatusReducer(state, threadId, "regular");
326
+ },
327
+ loading: (state, task) => {
328
+ const mappingId = createThreadMappingId(threadId);
329
+ return {
330
+ ...state,
331
+ threadData: {
332
+ ...state.threadData,
333
+ [mappingId]: {
334
+ ...state.threadData[mappingId],
335
+ initializeTask: task,
336
+ },
337
+ },
338
+ };
339
+ },
340
+ then: (state, { remoteId, externalId }) => {
341
+ const data = getThreadData(state, threadId);
342
+ if (!data) return state;
343
+
344
+ const mappingId = createThreadMappingId(threadId);
345
+ return {
346
+ ...state,
347
+ threadIdMap: {
348
+ ...state.threadIdMap,
349
+ [remoteId]: mappingId,
350
+ },
351
+ threadData: {
352
+ ...state.threadData,
353
+ [mappingId]: {
354
+ ...data,
355
+ initializeTask: Promise.resolve({ remoteId, externalId }),
356
+ remoteId,
357
+ externalId,
358
+ },
359
+ },
360
+ };
361
+ },
362
+ });
363
+ };
364
+
365
+ public generateTitle = async (threadId: string) => {
366
+ const data = this.getItemById(threadId);
367
+ if (!data) throw new Error("Thread not found");
368
+ if (data.status === "new") throw new Error("Thread is not yet initialized");
369
+
370
+ const { remoteId } = await data.initializeTask;
371
+
372
+ const runtimeCore = this._hookManager.getThreadRuntimeCore(data.id);
373
+ if (!runtimeCore) return; // thread is no longer running
374
+
375
+ const messages = runtimeCore.messages;
376
+ const stream = await this._options.adapter.generateTitle(
377
+ remoteId,
378
+ messages,
379
+ );
380
+ const messageStream = AssistantMessageStream.fromAssistantStream(stream);
381
+ for await (const result of messageStream) {
382
+ const newTitle = result.parts.filter((c) => c.type === "text")[0]?.text;
383
+ const state = this._state.baseValue;
384
+ const currentData = getThreadData(state, data.id);
385
+ if (!currentData) continue;
386
+ this._state.update({
387
+ ...state,
388
+ threadData: {
389
+ ...state.threadData,
390
+ [currentData.id]: {
391
+ ...currentData,
392
+ title: newTitle,
393
+ },
394
+ },
395
+ });
396
+ }
397
+ };
398
+
399
+ public rename(threadIdOrRemoteId: string, newTitle: string): Promise<void> {
400
+ const data = this.getItemById(threadIdOrRemoteId);
401
+ if (!data) throw new Error("Thread not found");
402
+ if (data.status === "new") throw new Error("Thread is not yet initialized");
403
+
404
+ return this._state.optimisticUpdate({
405
+ execute: async () => {
406
+ const { remoteId } = await data.initializeTask;
407
+ return this._options.adapter.rename(remoteId, newTitle);
408
+ },
409
+ optimistic: (state) => {
410
+ const data = getThreadData(state, threadIdOrRemoteId);
411
+ if (!data) return state;
412
+
413
+ return {
414
+ ...state,
415
+ threadData: {
416
+ ...state.threadData,
417
+ [data.id]: {
418
+ ...data,
419
+ title: newTitle,
420
+ },
421
+ },
422
+ };
423
+ },
424
+ });
425
+ }
426
+
427
+ private async _ensureThreadIsNotMain(threadId: string) {
428
+ if (threadId === this.newThreadId)
429
+ throw new Error("Cannot ensure new thread is not main");
430
+
431
+ if (threadId === this._mainThreadId) {
432
+ await this.switchToNewThread();
433
+ }
434
+ }
435
+
436
+ public async archive(threadIdOrRemoteId: string) {
437
+ const data = this.getItemById(threadIdOrRemoteId);
438
+ if (!data) throw new Error("Thread not found");
439
+ if (data.status !== "regular")
440
+ throw new Error("Thread is not yet initialized or already archived");
441
+
442
+ await this._ensureThreadIsNotMain(data.id);
443
+
444
+ return this._state.optimisticUpdate({
445
+ execute: async () => {
446
+ const { remoteId } = await data.initializeTask;
447
+ return this._options.adapter.archive(remoteId);
448
+ },
449
+ optimistic: (state) => {
450
+ return updateStatusReducer(state, data.id, "archived");
451
+ },
452
+ });
453
+ }
454
+
455
+ public unarchive(threadIdOrRemoteId: string): Promise<void> {
456
+ const data = this.getItemById(threadIdOrRemoteId);
457
+ if (!data) throw new Error("Thread not found");
458
+ if (data.status !== "archived") throw new Error("Thread is not archived");
459
+
460
+ return this._state.optimisticUpdate({
461
+ execute: async () => {
462
+ try {
463
+ const { remoteId } = await data.initializeTask;
464
+ return await this._options.adapter.unarchive(remoteId);
465
+ } catch (error) {
466
+ await this._ensureThreadIsNotMain(data.id);
467
+ throw error;
468
+ }
469
+ },
470
+ optimistic: (state) => {
471
+ return updateStatusReducer(state, data.id, "regular");
472
+ },
473
+ });
474
+ }
475
+
476
+ public async delete(threadIdOrRemoteId: string) {
477
+ const data = this.getItemById(threadIdOrRemoteId);
478
+ if (!data) throw new Error("Thread not found");
479
+ if (data.status !== "regular" && data.status !== "archived")
480
+ throw new Error("Thread is not yet initialized");
481
+
482
+ await this._ensureThreadIsNotMain(data.id);
483
+
484
+ return this._state.optimisticUpdate({
485
+ execute: async () => {
486
+ const { remoteId } = await data.initializeTask;
487
+ return await this._options.adapter.delete(remoteId);
488
+ },
489
+ optimistic: (state) => {
490
+ return updateStatusReducer(state, data.id, "deleted");
491
+ },
492
+ });
493
+ }
494
+
495
+ public async detach(threadIdOrRemoteId: string): Promise<void> {
496
+ const data = this.getItemById(threadIdOrRemoteId);
497
+ if (!data) throw new Error("Thread not found");
498
+ if (data.status !== "regular" && data.status !== "archived")
499
+ throw new Error("Thread is not yet initialized");
500
+
501
+ await this._ensureThreadIsNotMain(data.id);
502
+ this._hookManager.stopThreadRuntime(data.id);
503
+ }
504
+
505
+ private useBoundIds = create<string[]>(() => []);
506
+
507
+ public __internal_RenderComponent: FC = () => {
508
+ const id = useId();
509
+ useEffect(() => {
510
+ this.useBoundIds.setState((s) => [...s, id], true);
511
+ return () => {
512
+ this.useBoundIds.setState((s) => s.filter((i) => i !== id), true);
513
+ };
514
+ }, [id]);
515
+
516
+ const boundIds = this.useBoundIds();
517
+ const { Provider } = this.useProvider();
518
+
519
+ const adapters = {
520
+ modelContext: this.contextProvider,
521
+ };
522
+
523
+ return (
524
+ (boundIds.length === 0 || boundIds[0] === id) && (
525
+ // only render if the component is the first one mounted
526
+ <RuntimeAdapterProvider adapters={adapters}>
527
+ <this._hookManager.__internal_RenderThreadRuntimes
528
+ provider={Provider}
529
+ />
530
+ </RuntimeAdapterProvider>
531
+ )
532
+ );
533
+ };
534
+ }
@@ -0,0 +1,40 @@
1
+ import { createContext, FC, ReactNode, useContext } from "react";
2
+ import type { ThreadHistoryAdapter } from "../../adapters/thread-history";
3
+ import type { AttachmentAdapter } from "../../adapters/attachment";
4
+ import type { ModelContextProvider } from "../../model-context/types";
5
+
6
+ export type RuntimeAdapters = {
7
+ modelContext?: ModelContextProvider | undefined;
8
+ history?: ThreadHistoryAdapter | undefined;
9
+ attachments?: AttachmentAdapter | undefined;
10
+ };
11
+
12
+ const RuntimeAdaptersContext = createContext<RuntimeAdapters | null>(null);
13
+
14
+ export namespace RuntimeAdapterProvider {
15
+ export type Props = {
16
+ adapters: RuntimeAdapters;
17
+ children: ReactNode;
18
+ };
19
+ }
20
+
21
+ export const RuntimeAdapterProvider: FC<RuntimeAdapterProvider.Props> = ({
22
+ adapters,
23
+ children,
24
+ }) => {
25
+ const context = useContext(RuntimeAdaptersContext);
26
+ return (
27
+ <RuntimeAdaptersContext.Provider
28
+ value={{
29
+ ...context,
30
+ ...adapters,
31
+ }}
32
+ >
33
+ {children}
34
+ </RuntimeAdaptersContext.Provider>
35
+ );
36
+ };
37
+
38
+ export const useRuntimeAdapters = () => {
39
+ return useContext(RuntimeAdaptersContext);
40
+ };