@assistant-ui/react 0.7.9 → 0.7.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. package/dist/api/AssistantRuntime.d.ts +4 -5
  2. package/dist/api/AssistantRuntime.d.ts.map +1 -1
  3. package/dist/api/AssistantRuntime.js +16 -19
  4. package/dist/api/AssistantRuntime.js.map +1 -1
  5. package/dist/api/AssistantRuntime.mjs +16 -19
  6. package/dist/api/AssistantRuntime.mjs.map +1 -1
  7. package/dist/api/ThreadListItemRuntime.d.ts +11 -2
  8. package/dist/api/ThreadListItemRuntime.d.ts.map +1 -1
  9. package/dist/api/ThreadListItemRuntime.js +16 -5
  10. package/dist/api/ThreadListItemRuntime.js.map +1 -1
  11. package/dist/api/ThreadListItemRuntime.mjs +16 -5
  12. package/dist/api/ThreadListItemRuntime.mjs.map +1 -1
  13. package/dist/api/ThreadListRuntime.d.ts +3 -0
  14. package/dist/api/ThreadListRuntime.d.ts.map +1 -1
  15. package/dist/api/ThreadListRuntime.js +27 -8
  16. package/dist/api/ThreadListRuntime.js.map +1 -1
  17. package/dist/api/ThreadListRuntime.mjs +27 -8
  18. package/dist/api/ThreadListRuntime.mjs.map +1 -1
  19. package/dist/api/ThreadRuntime.d.ts +10 -7
  20. package/dist/api/ThreadRuntime.d.ts.map +1 -1
  21. package/dist/api/ThreadRuntime.js +16 -6
  22. package/dist/api/ThreadRuntime.js.map +1 -1
  23. package/dist/api/ThreadRuntime.mjs +16 -6
  24. package/dist/api/ThreadRuntime.mjs.map +1 -1
  25. package/dist/context/providers/AssistantRuntimeProvider.d.ts.map +1 -1
  26. package/dist/context/providers/AssistantRuntimeProvider.js +8 -1
  27. package/dist/context/providers/AssistantRuntimeProvider.js.map +1 -1
  28. package/dist/context/providers/AssistantRuntimeProvider.mjs +8 -1
  29. package/dist/context/providers/AssistantRuntimeProvider.mjs.map +1 -1
  30. package/dist/context/providers/ThreadRuntimeProvider.d.ts +2 -0
  31. package/dist/context/providers/ThreadRuntimeProvider.d.ts.map +1 -1
  32. package/dist/context/providers/ThreadRuntimeProvider.js +3 -2
  33. package/dist/context/providers/ThreadRuntimeProvider.js.map +1 -1
  34. package/dist/context/providers/ThreadRuntimeProvider.mjs +3 -2
  35. package/dist/context/providers/ThreadRuntimeProvider.mjs.map +1 -1
  36. package/dist/internal.d.ts +1 -2
  37. package/dist/internal.d.ts.map +1 -1
  38. package/dist/internal.js +0 -3
  39. package/dist/internal.js.map +1 -1
  40. package/dist/internal.mjs +0 -2
  41. package/dist/internal.mjs.map +1 -1
  42. package/dist/primitives/composer/ComposerInput.d.ts.map +1 -1
  43. package/dist/primitives/composer/ComposerInput.js +3 -1
  44. package/dist/primitives/composer/ComposerInput.js.map +1 -1
  45. package/dist/primitives/composer/ComposerInput.mjs +3 -1
  46. package/dist/primitives/composer/ComposerInput.mjs.map +1 -1
  47. package/dist/runtimes/core/BaseThreadRuntimeCore.d.ts +2 -4
  48. package/dist/runtimes/core/BaseThreadRuntimeCore.d.ts.map +1 -1
  49. package/dist/runtimes/core/BaseThreadRuntimeCore.js +1 -5
  50. package/dist/runtimes/core/BaseThreadRuntimeCore.js.map +1 -1
  51. package/dist/runtimes/core/BaseThreadRuntimeCore.mjs +1 -5
  52. package/dist/runtimes/core/BaseThreadRuntimeCore.mjs.map +1 -1
  53. package/dist/runtimes/core/ThreadListRuntimeCore.d.ts +14 -6
  54. package/dist/runtimes/core/ThreadListRuntimeCore.d.ts.map +1 -1
  55. package/dist/runtimes/core/ThreadListRuntimeCore.js.map +1 -1
  56. package/dist/runtimes/core/ThreadRuntimeCore.d.ts +1 -15
  57. package/dist/runtimes/core/ThreadRuntimeCore.d.ts.map +1 -1
  58. package/dist/runtimes/core/ThreadRuntimeCore.js.map +1 -1
  59. package/dist/runtimes/core/index.d.ts +1 -1
  60. package/dist/runtimes/core/index.d.ts.map +1 -1
  61. package/dist/runtimes/core/index.js.map +1 -1
  62. package/dist/runtimes/external-store/ExternalStoreAdapter.d.ts +20 -3
  63. package/dist/runtimes/external-store/ExternalStoreAdapter.d.ts.map +1 -1
  64. package/dist/runtimes/external-store/ExternalStoreAdapter.js.map +1 -1
  65. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.d.ts +0 -1
  66. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.d.ts.map +1 -1
  67. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.js +3 -10
  68. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.js.map +1 -1
  69. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.mjs +3 -10
  70. package/dist/runtimes/external-store/ExternalStoreRuntimeCore.mjs.map +1 -1
  71. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.d.ts +9 -11
  72. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.d.ts.map +1 -1
  73. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.js +19 -27
  74. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.js.map +1 -1
  75. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.mjs +19 -27
  76. package/dist/runtimes/external-store/ExternalStoreThreadListRuntimeCore.mjs.map +1 -1
  77. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.d.ts +2 -2
  78. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.d.ts.map +1 -1
  79. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.js +4 -7
  80. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.js.map +1 -1
  81. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.mjs +4 -7
  82. package/dist/runtimes/external-store/ExternalStoreThreadRuntimeCore.mjs.map +1 -1
  83. package/dist/runtimes/local/LocalRuntimeCore.d.ts +0 -1
  84. package/dist/runtimes/local/LocalRuntimeCore.d.ts.map +1 -1
  85. package/dist/runtimes/local/LocalRuntimeCore.js +3 -12
  86. package/dist/runtimes/local/LocalRuntimeCore.js.map +1 -1
  87. package/dist/runtimes/local/LocalRuntimeCore.mjs +3 -12
  88. package/dist/runtimes/local/LocalRuntimeCore.mjs.map +1 -1
  89. package/dist/runtimes/local/LocalThreadListRuntimeCore.d.ts +17 -15
  90. package/dist/runtimes/local/LocalThreadListRuntimeCore.d.ts.map +1 -1
  91. package/dist/runtimes/local/LocalThreadListRuntimeCore.js +94 -92
  92. package/dist/runtimes/local/LocalThreadListRuntimeCore.js.map +1 -1
  93. package/dist/runtimes/local/LocalThreadListRuntimeCore.mjs +94 -92
  94. package/dist/runtimes/local/LocalThreadListRuntimeCore.mjs.map +1 -1
  95. package/dist/runtimes/local/LocalThreadRuntimeCore.d.ts +4 -3
  96. package/dist/runtimes/local/LocalThreadRuntimeCore.d.ts.map +1 -1
  97. package/dist/runtimes/local/LocalThreadRuntimeCore.js +11 -10
  98. package/dist/runtimes/local/LocalThreadRuntimeCore.js.map +1 -1
  99. package/dist/runtimes/local/LocalThreadRuntimeCore.mjs +11 -10
  100. package/dist/runtimes/local/LocalThreadRuntimeCore.mjs.map +1 -1
  101. package/dist/runtimes/local/useLocalRuntime.d.ts.map +1 -1
  102. package/dist/runtimes/local/useLocalRuntime.js +4 -7
  103. package/dist/runtimes/local/useLocalRuntime.js.map +1 -1
  104. package/dist/runtimes/local/useLocalRuntime.mjs +4 -7
  105. package/dist/runtimes/local/useLocalRuntime.mjs.map +1 -1
  106. package/package.json +1 -1
  107. package/src/api/AssistantRuntime.ts +21 -25
  108. package/src/api/ThreadListItemRuntime.ts +36 -7
  109. package/src/api/ThreadListRuntime.ts +31 -9
  110. package/src/api/ThreadRuntime.ts +38 -10
  111. package/src/context/providers/AssistantRuntimeProvider.tsx +4 -1
  112. package/src/context/providers/ThreadRuntimeProvider.tsx +9 -2
  113. package/src/internal.ts +1 -1
  114. package/src/primitives/composer/ComposerInput.tsx +3 -1
  115. package/src/runtimes/core/BaseThreadRuntimeCore.tsx +1 -9
  116. package/src/runtimes/core/ThreadListRuntimeCore.tsx +16 -7
  117. package/src/runtimes/core/ThreadRuntimeCore.tsx +1 -19
  118. package/src/runtimes/core/index.ts +0 -1
  119. package/src/runtimes/external-store/ExternalStoreAdapter.tsx +20 -3
  120. package/src/runtimes/external-store/ExternalStoreRuntimeCore.tsx +5 -12
  121. package/src/runtimes/external-store/ExternalStoreThreadListRuntimeCore.tsx +22 -35
  122. package/src/runtimes/external-store/ExternalStoreThreadRuntimeCore.tsx +3 -7
  123. package/src/runtimes/local/LocalRuntimeCore.tsx +7 -14
  124. package/src/runtimes/local/LocalThreadListRuntimeCore.tsx +110 -103
  125. package/src/runtimes/local/LocalThreadRuntimeCore.tsx +11 -10
  126. package/src/runtimes/local/useLocalRuntime.tsx +5 -12
  127. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.d.ts +0 -19
  128. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.d.ts.map +0 -1
  129. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.js +0 -87
  130. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.js.map +0 -1
  131. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.mjs +0 -62
  132. package/dist/runtimes/local/LocalThreadMetadataRuntimeCore.mjs.map +0 -1
  133. package/src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx +0 -79
@@ -3,206 +3,213 @@ import { ThreadListRuntimeCore } from "../core/ThreadListRuntimeCore";
3
3
  import { ExportedMessageRepository } from "../utils/MessageRepository";
4
4
  import { generateId } from "../../utils/idUtils";
5
5
  import { LocalThreadRuntimeCore } from "./LocalThreadRuntimeCore";
6
- import { ThreadMetadata } from "../core/ThreadRuntimeCore";
7
6
 
8
7
  export type ThreadListAdapter = {
9
8
  subscribe(callback: () => void): Unsubscribe;
10
9
  };
11
10
 
12
11
  export type LocalThreadData = {
13
- runtime: LocalThreadRuntimeCore;
14
- lastState: ThreadMetadata["state"];
15
- dispose: Unsubscribe;
12
+ readonly runtime: LocalThreadRuntimeCore;
13
+ readonly state: "new" | "regular" | "archived";
14
+ readonly threadId: string;
15
+ readonly title?: string | undefined;
16
+
17
+ dispose(): void;
16
18
  };
17
19
 
18
20
  export type LocalThreadFactory = (
19
- threadId: string,
20
21
  data: ExportedMessageRepository,
21
22
  ) => LocalThreadRuntimeCore;
22
23
 
23
24
  export class LocalThreadListRuntimeCore implements ThreadListRuntimeCore {
24
25
  private _threadData = new Map<string, LocalThreadData>();
25
- private _threads: readonly string[] = [];
26
- private _archivedThreads: readonly string[] = [];
27
- private _newThread: string | undefined;
26
+ private _threadIds: readonly string[] = [];
27
+ private _archivedThreadIds: readonly string[] = [];
28
+ private _newThreadId: string | undefined;
28
29
 
29
- public get newThread() {
30
- return this._newThread;
30
+ public get newThreadId() {
31
+ return this._newThreadId;
31
32
  }
32
33
 
33
- public get threads() {
34
- return this._threads;
34
+ public get threadIds() {
35
+ return this._threadIds;
35
36
  }
36
37
 
37
- public get archivedThreads() {
38
- return this._archivedThreads;
38
+ public get archivedThreadIds() {
39
+ return this._archivedThreadIds;
39
40
  }
40
41
 
41
- private _mainThread!: LocalThreadRuntimeCore;
42
+ private _mainThreadId!: string;
42
43
 
43
- public get mainThread(): LocalThreadRuntimeCore {
44
- return this._mainThread;
44
+ public get mainThreadId(): string {
45
+ return this._mainThreadId;
45
46
  }
46
47
 
47
48
  constructor(private _threadFactory: LocalThreadFactory) {
48
49
  this.switchToNewThread();
49
50
  }
50
51
 
51
- public getThreadMetadataById(threadId: string) {
52
- return this._threadData.get(threadId)?.runtime.metadata;
52
+ public getMainThreadRuntimeCore() {
53
+ const result = this._threadData.get(this._mainThreadId)?.runtime;
54
+ if (!result)
55
+ throw new Error("Main thread not found. This is a bug in assistant-ui.");
56
+ return result;
57
+ }
58
+
59
+ public getItemById(threadId: string) {
60
+ return this._threadData.get(threadId);
53
61
  }
54
62
 
55
- public switchToThread(threadId: string): Promise<void> {
56
- if (this._mainThread?.metadata.threadId === threadId)
57
- return Promise.resolve();
63
+ public async switchToThread(threadId: string): Promise<void> {
64
+ if (this._mainThreadId === threadId) return;
58
65
 
59
66
  const data = this._threadData.get(threadId);
60
67
  if (!data) throw new Error("Thread not found");
61
68
 
62
- if (data.runtime.metadata.state === "archived") this.unarchive(threadId);
69
+ if (data.state === "archived") await this.unarchive(threadId);
63
70
 
64
- this._mainThread?._notifyEventSubscribers("switched-away");
65
- this._mainThread = data.runtime;
71
+ this._mainThreadId = data.threadId;
66
72
  this._notifySubscribers();
67
-
68
- data.runtime._notifyEventSubscribers("switched-to");
69
- return Promise.resolve();
70
73
  }
71
74
 
72
75
  public switchToNewThread(): Promise<void> {
73
- if (this._newThread === undefined) {
76
+ if (this._newThreadId === undefined) {
74
77
  let threadId: string;
75
78
  do {
76
79
  threadId = generateId();
77
80
  } while (this._threadData.has(threadId));
78
81
 
79
- const runtime = this._threadFactory(threadId, { messages: [] });
80
- const dispose = runtime.metadata.subscribe(() => {
81
- this._syncState(threadId, runtime.metadata);
82
- threadId = runtime.metadata.threadId;
82
+ const runtime = this._threadFactory({ messages: [] });
83
+ const dispose = runtime.unstable_on("initialize", () => {
84
+ const data = this._threadData.get(threadId);
85
+ if (!data) throw new Error("Thread not found");
86
+
87
+ this._stateOp(threadId, "regular");
83
88
  });
84
- this._threadData.set(threadId, { runtime, lastState: "new", dispose });
85
- this._newThread = threadId;
89
+ this._threadData.set(threadId, {
90
+ runtime,
91
+ state: "new",
92
+ threadId,
93
+ dispose,
94
+ });
95
+ this._newThreadId = threadId;
86
96
  }
87
97
 
88
- this.switchToThread(this._newThread);
98
+ this.switchToThread(this._newThreadId);
89
99
  return Promise.resolve();
90
100
  }
91
101
 
92
- private async _syncState(
93
- lastThreadId: string,
94
- { state, threadId }: ThreadMetadata,
102
+ private async _stateOp(
103
+ threadId: string,
104
+ newState: "regular" | "archived" | "deleted",
95
105
  ) {
96
- const data = this._threadData.get(lastThreadId);
106
+ const data = this._threadData.get(threadId);
97
107
  if (!data) throw new Error("Thread not found");
98
- const lastState = data.lastState;
99
- if (lastState === state && lastThreadId === threadId) return;
100
108
 
101
- if (lastThreadId !== threadId) {
102
- this._threadData.delete(lastThreadId);
103
- if (lastState === "new") {
104
- this._newThread = threadId;
105
- }
106
- if (lastState === "regular") {
107
- this._threads = this._threads.map((t) =>
108
- t === lastThreadId ? threadId : t,
109
- );
110
- }
111
- if (lastState === "archived") {
112
- this._archivedThreads = this._archivedThreads.map((t) =>
113
- t === lastThreadId ? threadId : t,
109
+ const { state: lastState } = data;
110
+ if (lastState === newState) return;
111
+
112
+ // lastState
113
+ switch (lastState) {
114
+ case "new":
115
+ this._newThreadId = undefined;
116
+ break;
117
+ case "regular":
118
+ this._threadIds = this._threadIds.filter((t) => t !== threadId);
119
+ break;
120
+ case "archived":
121
+ this._archivedThreadIds = this._archivedThreadIds.filter(
122
+ (t) => t !== threadId,
114
123
  );
115
- }
116
- }
124
+ break;
117
125
 
118
- if (lastState !== state) {
119
- if (lastState === "new") {
120
- this._newThread = undefined;
126
+ default: {
127
+ const _exhaustiveCheck: never = lastState;
128
+ throw new Error(`Unsupported state: ${_exhaustiveCheck}`);
121
129
  }
130
+ }
122
131
 
123
- if (lastState === "regular") {
124
- this._threads = this._threads.filter((t) => t !== threadId);
125
- }
132
+ // newState
133
+ switch (newState) {
134
+ case "regular":
135
+ this._threadIds = [...this._threadIds, data.threadId];
136
+ break;
126
137
 
127
- if (lastState === "archived") {
128
- this._archivedThreads = this._archivedThreads.filter(
129
- (t) => t !== threadId,
130
- );
131
- }
138
+ case "archived":
139
+ this._archivedThreadIds = [...this._archivedThreadIds, data.threadId];
140
+ break;
132
141
 
133
- if (state === "new") {
134
- if (this._newThread) {
135
- this.delete(this._newThread);
136
- }
137
- this._newThread = threadId;
138
- }
139
- if (state === "archived") {
140
- this._archivedThreads = [
141
- ...this._archivedThreads,
142
- data.runtime.metadata.threadId,
143
- ];
144
- }
145
- if (state === "regular") {
146
- this._threads = [...this._threads, data.runtime.metadata.threadId];
147
- }
148
- if (state === "deleted") {
149
- data.dispose();
142
+ case "deleted":
150
143
  this._threadData.delete(threadId);
151
- }
144
+ data.dispose();
145
+ break;
152
146
 
153
- data.lastState = state;
147
+ default: {
148
+ const _exhaustiveCheck: never = newState;
149
+ throw new Error(`Unsupported state: ${_exhaustiveCheck}`);
150
+ }
154
151
  }
155
152
 
156
- this._notifySubscribers();
153
+ if (newState !== "deleted") {
154
+ this._threadData.set(threadId, {
155
+ ...data,
156
+ state: newState,
157
+ });
158
+ }
157
159
 
158
160
  if (
159
- threadId === this._mainThread.metadata.threadId &&
160
- (state === "archived" || state === "deleted")
161
+ threadId === this._mainThreadId &&
162
+ (newState === "archived" || newState === "deleted")
161
163
  ) {
162
- const lastThread = this._threads[0];
163
- if (lastThread) {
164
- await this.switchToThread(lastThread);
164
+ const lastThreadId = this._threadIds[0];
165
+ if (lastThreadId) {
166
+ await this.switchToThread(lastThreadId);
165
167
  } else {
166
168
  await this.switchToNewThread();
167
169
  }
170
+ } else {
171
+ this._notifySubscribers();
168
172
  }
169
173
  }
170
174
 
171
175
  public rename(threadId: string, newTitle: string): Promise<void> {
172
176
  const data = this._threadData.get(threadId);
173
177
  if (!data) throw new Error("Thread not found");
174
- data.runtime.metadata.rename(newTitle);
175
178
 
179
+ this._threadData.set(threadId, {
180
+ ...data,
181
+ title: newTitle,
182
+ });
183
+ this._notifySubscribers();
176
184
  return Promise.resolve();
177
185
  }
178
186
 
179
187
  public archive(threadId: string): Promise<void> {
180
188
  const data = this._threadData.get(threadId);
181
189
  if (!data) throw new Error("Thread not found");
182
- if (data.lastState !== "regular")
183
- throw new Error("Thread is not yet created or archived");
184
- data.runtime.metadata.archive();
190
+ if (data.state !== "regular")
191
+ throw new Error("Thread is not yet initialized or already archived");
185
192
 
193
+ this._stateOp(threadId, "archived");
186
194
  return Promise.resolve();
187
195
  }
188
196
 
189
197
  public unarchive(threadId: string): Promise<void> {
190
198
  const data = this._threadData.get(threadId);
191
199
  if (!data) throw new Error("Thread not found");
192
- if (data.lastState !== "archived")
193
- throw new Error("Thread is not archived");
194
- data.runtime.metadata.unarchive();
200
+ if (data.state !== "archived") throw new Error("Thread is not archived");
195
201
 
202
+ this._stateOp(threadId, "regular");
196
203
  return Promise.resolve();
197
204
  }
198
205
 
199
206
  public delete(threadId: string): Promise<void> {
200
207
  const data = this._threadData.get(threadId);
201
208
  if (!data) throw new Error("Thread not found");
202
- if (data.lastState !== "regular" && data.lastState !== "archived")
203
- throw new Error("Thread is not yet created or already deleted");
204
- data.runtime.metadata.delete();
209
+ if (data.state !== "regular" && data.state !== "archived")
210
+ throw new Error("Thread is not yet initalized");
205
211
 
212
+ this._stateOp(threadId, "deleted");
206
213
  return Promise.resolve();
207
214
  }
208
215
 
@@ -14,12 +14,13 @@ import {
14
14
  ThreadRuntimeCore,
15
15
  } from "../core/ThreadRuntimeCore";
16
16
  import { BaseThreadRuntimeCore } from "../core/BaseThreadRuntimeCore";
17
- import { LocalThreadMetadataRuntimeCore } from "./LocalThreadMetadataRuntimeCore";
18
17
 
19
18
  export class LocalThreadRuntimeCore
20
19
  extends BaseThreadRuntimeCore
21
20
  implements ThreadRuntimeCore
22
21
  {
22
+ private _isInitialized = false;
23
+
23
24
  public readonly capabilities = {
24
25
  switchToBranch: true,
25
26
  edit: true,
@@ -42,11 +43,10 @@ export class LocalThreadRuntimeCore
42
43
 
43
44
  constructor(
44
45
  configProvider: ModelConfigProvider,
45
- threadId: string,
46
46
  options: LocalRuntimeOptionsBase,
47
47
  ) {
48
- super(configProvider, new LocalThreadMetadataRuntimeCore(threadId));
49
- this.setOptions(options);
48
+ super(configProvider);
49
+ this.__internal_setOptions(options);
50
50
  }
51
51
 
52
52
  private _options!: LocalRuntimeOptionsBase;
@@ -55,7 +55,7 @@ export class LocalThreadRuntimeCore
55
55
  return undefined;
56
56
  }
57
57
 
58
- public setOptions(options: LocalRuntimeOptionsBase) {
58
+ public __internal_setOptions(options: LocalRuntimeOptionsBase) {
59
59
  if (this._options === options) return;
60
60
 
61
61
  this._options = options;
@@ -83,14 +83,15 @@ export class LocalThreadRuntimeCore
83
83
  if (hasUpdates) this._notifySubscribers();
84
84
  }
85
85
 
86
- private _transitionAwayFromNewState() {
87
- if (this.metadata.state === "new") {
88
- this.metadata.create();
86
+ private _ensureInitialized() {
87
+ if (!this._isInitialized) {
88
+ this._isInitialized = true;
89
+ this._notifyEventSubscribers("initialize");
89
90
  }
90
91
  }
91
92
 
92
93
  public async append(message: AppendMessage): Promise<void> {
93
- this._transitionAwayFromNewState();
94
+ this._ensureInitialized();
94
95
 
95
96
  const newMessage = fromCoreMessage(message, {
96
97
  attachments: message.attachments,
@@ -107,7 +108,7 @@ export class LocalThreadRuntimeCore
107
108
  }
108
109
 
109
110
  public async startRun(parentId: string | null): Promise<void> {
110
- this._transitionAwayFromNewState();
111
+ this._ensureInitialized();
111
112
 
112
113
  this.repository.resetHead(parentId);
113
114
 
@@ -9,29 +9,22 @@ import {
9
9
  AssistantRuntimeImpl,
10
10
  } from "../../api/AssistantRuntime";
11
11
  import { ThreadRuntimeImpl } from "../../internal";
12
- import { ThreadRuntime } from "../../api/ThreadRuntime";
13
12
 
14
13
  export type LocalRuntime = AssistantRuntime & {
15
14
  reset: (options?: Parameters<LocalRuntimeCore["reset"]>[0]) => void;
16
15
  };
17
16
 
18
17
  class LocalRuntimeImpl extends AssistantRuntimeImpl implements LocalRuntime {
19
- private constructor(
20
- private core: LocalRuntimeCore,
21
- thread: ThreadRuntime,
22
- ) {
23
- super(core, thread);
18
+ private constructor(private core: LocalRuntimeCore) {
19
+ super(core, ThreadRuntimeImpl);
24
20
  }
25
21
 
26
22
  public reset(options?: Parameters<LocalRuntimeCore["reset"]>[0]) {
27
23
  this.core.reset(options);
28
24
  }
29
25
 
30
- public static override create(_core: LocalRuntimeCore) {
31
- return new LocalRuntimeImpl(
32
- _core,
33
- AssistantRuntimeImpl.createMainThreadRuntime(_core, ThreadRuntimeImpl),
34
- ) as LocalRuntime;
26
+ public static override create(_core: LocalRuntimeCore): LocalRuntime {
27
+ return new LocalRuntimeImpl(_core);
35
28
  }
36
29
  }
37
30
 
@@ -50,7 +43,7 @@ export const useLocalRuntime = (
50
43
  const [runtime] = useState(() => new LocalRuntimeCore(opt, initialMessages));
51
44
 
52
45
  useEffect(() => {
53
- runtime.setOptions(opt);
46
+ runtime.threadList.getMainThreadRuntimeCore().__internal_setOptions(opt);
54
47
  });
55
48
 
56
49
  return useMemo(() => LocalRuntimeImpl.create(runtime), [runtime]);
@@ -1,19 +0,0 @@
1
- import { ThreadMetadataRuntimeCore } from "../core/ThreadRuntimeCore";
2
- export declare class LocalThreadMetadataRuntimeCore implements ThreadMetadataRuntimeCore {
3
- private _threadId;
4
- private _state;
5
- private _title;
6
- private _subscribers;
7
- constructor(_threadId: string);
8
- get threadId(): string;
9
- get state(): "archived" | "regular" | "new" | "deleted";
10
- get title(): string | undefined;
11
- rename(newTitle: string): Promise<void>;
12
- archive(): Promise<void>;
13
- unarchive(): Promise<void>;
14
- delete(): Promise<void>;
15
- create(title?: string): Promise<void>;
16
- subscribe(callback: () => void): () => boolean;
17
- private _notifySubscribers;
18
- }
19
- //# sourceMappingURL=LocalThreadMetadataRuntimeCore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LocalThreadMetadataRuntimeCore.d.ts","sourceRoot":"","sources":["../../../src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,qBAAa,8BACX,YAAW,yBAAyB;IAMxB,OAAO,CAAC,SAAS;IAJ7B,OAAO,CAAC,MAAM,CAAqD;IACnE,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,YAAY,CAAyB;gBAEzB,SAAS,EAAE,MAAM;IAErC,IAAW,QAAQ,WAElB;IAED,IAAW,KAAK,+CAEf;IAED,IAAW,KAAK,uBAEf;IAEM,MAAM,CAAC,QAAQ,EAAE,MAAM;IAOvB,OAAO;IAWP,SAAS;IAST,MAAM;IAUN,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM;IAUrB,SAAS,CAAC,QAAQ,EAAE,MAAM,IAAI;IAKrC,OAAO,CAAC,kBAAkB;CAG3B"}
@@ -1,87 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx
21
- var LocalThreadMetadataRuntimeCore_exports = {};
22
- __export(LocalThreadMetadataRuntimeCore_exports, {
23
- LocalThreadMetadataRuntimeCore: () => LocalThreadMetadataRuntimeCore
24
- });
25
- module.exports = __toCommonJS(LocalThreadMetadataRuntimeCore_exports);
26
- var LocalThreadMetadataRuntimeCore = class {
27
- constructor(_threadId) {
28
- this._threadId = _threadId;
29
- }
30
- _state = "new";
31
- _title;
32
- _subscribers = /* @__PURE__ */ new Set();
33
- get threadId() {
34
- return this._threadId;
35
- }
36
- get state() {
37
- return this._state;
38
- }
39
- get title() {
40
- return this._title;
41
- }
42
- rename(newTitle) {
43
- this._title = newTitle;
44
- this._notifySubscribers();
45
- return Promise.resolve();
46
- }
47
- archive() {
48
- if (this._state === "new") throw new Error("Thread not created");
49
- if (this._state === "archived") throw new Error("Thread already archived");
50
- if (this._state === "deleted") throw new Error("Thread deleted");
51
- this._state = "archived";
52
- this._notifySubscribers();
53
- return Promise.resolve();
54
- }
55
- unarchive() {
56
- if (this._state !== "archived") throw new Error("Thread not archived");
57
- this._state = "regular";
58
- this._notifySubscribers();
59
- return Promise.resolve();
60
- }
61
- delete() {
62
- if (this._state === "new") throw new Error("Thread not created");
63
- if (this._state === "deleted") throw new Error("Thread already deleted");
64
- this._state = "deleted";
65
- this._notifySubscribers();
66
- return Promise.resolve();
67
- }
68
- create(title) {
69
- if (this.state !== "new") throw new Error("Thread not new");
70
- this._state = "regular";
71
- this._title = title ?? this._title;
72
- this._notifySubscribers();
73
- return Promise.resolve();
74
- }
75
- subscribe(callback) {
76
- this._subscribers.add(callback);
77
- return () => this._subscribers.delete(callback);
78
- }
79
- _notifySubscribers() {
80
- for (const callback of this._subscribers) callback();
81
- }
82
- };
83
- // Annotate the CommonJS export names for ESM import in node:
84
- 0 && (module.exports = {
85
- LocalThreadMetadataRuntimeCore
86
- });
87
- //# sourceMappingURL=LocalThreadMetadataRuntimeCore.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx"],"sourcesContent":["import { ThreadMetadataRuntimeCore } from \"../core/ThreadRuntimeCore\";\n\nexport class LocalThreadMetadataRuntimeCore\n implements ThreadMetadataRuntimeCore\n{\n private _state: \"regular\" | \"archived\" | \"deleted\" | \"new\" = \"new\";\n private _title: string | undefined;\n private _subscribers = new Set<() => void>();\n\n constructor(private _threadId: string) {}\n\n public get threadId() {\n return this._threadId;\n }\n\n public get state() {\n return this._state;\n }\n\n public get title() {\n return this._title;\n }\n\n public rename(newTitle: string) {\n this._title = newTitle;\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public archive() {\n if (this._state === \"new\") throw new Error(\"Thread not created\");\n if (this._state === \"archived\") throw new Error(\"Thread already archived\");\n if (this._state === \"deleted\") throw new Error(\"Thread deleted\");\n\n this._state = \"archived\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public unarchive() {\n if (this._state !== \"archived\") throw new Error(\"Thread not archived\");\n\n this._state = \"regular\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public delete() {\n if (this._state === \"new\") throw new Error(\"Thread not created\");\n if (this._state === \"deleted\") throw new Error(\"Thread already deleted\");\n\n this._state = \"deleted\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public create(title?: string) {\n if (this.state !== \"new\") throw new Error(\"Thread not new\");\n\n this._state = \"regular\";\n this._title = title ?? this._title;\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public subscribe(callback: () => void) {\n this._subscribers.add(callback);\n return () => this._subscribers.delete(callback);\n }\n\n private _notifySubscribers() {\n for (const callback of this._subscribers) callback();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEO,IAAM,iCAAN,MAEP;AAAA,EAKE,YAAoB,WAAmB;AAAnB;AAAA,EAAoB;AAAA,EAJhC,SAAqD;AAAA,EACrD;AAAA,EACA,eAAe,oBAAI,IAAgB;AAAA,EAI3C,IAAW,WAAW;AACpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,OAAO,UAAkB;AAC9B,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,UAAU;AACf,QAAI,KAAK,WAAW,MAAO,OAAM,IAAI,MAAM,oBAAoB;AAC/D,QAAI,KAAK,WAAW,WAAY,OAAM,IAAI,MAAM,yBAAyB;AACzE,QAAI,KAAK,WAAW,UAAW,OAAM,IAAI,MAAM,gBAAgB;AAE/D,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,YAAY;AACjB,QAAI,KAAK,WAAW,WAAY,OAAM,IAAI,MAAM,qBAAqB;AAErE,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,SAAS;AACd,QAAI,KAAK,WAAW,MAAO,OAAM,IAAI,MAAM,oBAAoB;AAC/D,QAAI,KAAK,WAAW,UAAW,OAAM,IAAI,MAAM,wBAAwB;AAEvE,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,OAAO,OAAgB;AAC5B,QAAI,KAAK,UAAU,MAAO,OAAM,IAAI,MAAM,gBAAgB;AAE1D,SAAK,SAAS;AACd,SAAK,SAAS,SAAS,KAAK;AAC5B,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,UAAU,UAAsB;AACrC,SAAK,aAAa,IAAI,QAAQ;AAC9B,WAAO,MAAM,KAAK,aAAa,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEQ,qBAAqB;AAC3B,eAAW,YAAY,KAAK,aAAc,UAAS;AAAA,EACrD;AACF;","names":[]}
@@ -1,62 +0,0 @@
1
- // src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx
2
- var LocalThreadMetadataRuntimeCore = class {
3
- constructor(_threadId) {
4
- this._threadId = _threadId;
5
- }
6
- _state = "new";
7
- _title;
8
- _subscribers = /* @__PURE__ */ new Set();
9
- get threadId() {
10
- return this._threadId;
11
- }
12
- get state() {
13
- return this._state;
14
- }
15
- get title() {
16
- return this._title;
17
- }
18
- rename(newTitle) {
19
- this._title = newTitle;
20
- this._notifySubscribers();
21
- return Promise.resolve();
22
- }
23
- archive() {
24
- if (this._state === "new") throw new Error("Thread not created");
25
- if (this._state === "archived") throw new Error("Thread already archived");
26
- if (this._state === "deleted") throw new Error("Thread deleted");
27
- this._state = "archived";
28
- this._notifySubscribers();
29
- return Promise.resolve();
30
- }
31
- unarchive() {
32
- if (this._state !== "archived") throw new Error("Thread not archived");
33
- this._state = "regular";
34
- this._notifySubscribers();
35
- return Promise.resolve();
36
- }
37
- delete() {
38
- if (this._state === "new") throw new Error("Thread not created");
39
- if (this._state === "deleted") throw new Error("Thread already deleted");
40
- this._state = "deleted";
41
- this._notifySubscribers();
42
- return Promise.resolve();
43
- }
44
- create(title) {
45
- if (this.state !== "new") throw new Error("Thread not new");
46
- this._state = "regular";
47
- this._title = title ?? this._title;
48
- this._notifySubscribers();
49
- return Promise.resolve();
50
- }
51
- subscribe(callback) {
52
- this._subscribers.add(callback);
53
- return () => this._subscribers.delete(callback);
54
- }
55
- _notifySubscribers() {
56
- for (const callback of this._subscribers) callback();
57
- }
58
- };
59
- export {
60
- LocalThreadMetadataRuntimeCore
61
- };
62
- //# sourceMappingURL=LocalThreadMetadataRuntimeCore.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/runtimes/local/LocalThreadMetadataRuntimeCore.tsx"],"sourcesContent":["import { ThreadMetadataRuntimeCore } from \"../core/ThreadRuntimeCore\";\n\nexport class LocalThreadMetadataRuntimeCore\n implements ThreadMetadataRuntimeCore\n{\n private _state: \"regular\" | \"archived\" | \"deleted\" | \"new\" = \"new\";\n private _title: string | undefined;\n private _subscribers = new Set<() => void>();\n\n constructor(private _threadId: string) {}\n\n public get threadId() {\n return this._threadId;\n }\n\n public get state() {\n return this._state;\n }\n\n public get title() {\n return this._title;\n }\n\n public rename(newTitle: string) {\n this._title = newTitle;\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public archive() {\n if (this._state === \"new\") throw new Error(\"Thread not created\");\n if (this._state === \"archived\") throw new Error(\"Thread already archived\");\n if (this._state === \"deleted\") throw new Error(\"Thread deleted\");\n\n this._state = \"archived\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public unarchive() {\n if (this._state !== \"archived\") throw new Error(\"Thread not archived\");\n\n this._state = \"regular\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public delete() {\n if (this._state === \"new\") throw new Error(\"Thread not created\");\n if (this._state === \"deleted\") throw new Error(\"Thread already deleted\");\n\n this._state = \"deleted\";\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public create(title?: string) {\n if (this.state !== \"new\") throw new Error(\"Thread not new\");\n\n this._state = \"regular\";\n this._title = title ?? this._title;\n this._notifySubscribers();\n\n return Promise.resolve();\n }\n\n public subscribe(callback: () => void) {\n this._subscribers.add(callback);\n return () => this._subscribers.delete(callback);\n }\n\n private _notifySubscribers() {\n for (const callback of this._subscribers) callback();\n }\n}\n"],"mappings":";AAEO,IAAM,iCAAN,MAEP;AAAA,EAKE,YAAoB,WAAmB;AAAnB;AAAA,EAAoB;AAAA,EAJhC,SAAqD;AAAA,EACrD;AAAA,EACA,eAAe,oBAAI,IAAgB;AAAA,EAI3C,IAAW,WAAW;AACpB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK;AAAA,EACd;AAAA,EAEO,OAAO,UAAkB;AAC9B,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,UAAU;AACf,QAAI,KAAK,WAAW,MAAO,OAAM,IAAI,MAAM,oBAAoB;AAC/D,QAAI,KAAK,WAAW,WAAY,OAAM,IAAI,MAAM,yBAAyB;AACzE,QAAI,KAAK,WAAW,UAAW,OAAM,IAAI,MAAM,gBAAgB;AAE/D,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,YAAY;AACjB,QAAI,KAAK,WAAW,WAAY,OAAM,IAAI,MAAM,qBAAqB;AAErE,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,SAAS;AACd,QAAI,KAAK,WAAW,MAAO,OAAM,IAAI,MAAM,oBAAoB;AAC/D,QAAI,KAAK,WAAW,UAAW,OAAM,IAAI,MAAM,wBAAwB;AAEvE,SAAK,SAAS;AACd,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,OAAO,OAAgB;AAC5B,QAAI,KAAK,UAAU,MAAO,OAAM,IAAI,MAAM,gBAAgB;AAE1D,SAAK,SAAS;AACd,SAAK,SAAS,SAAS,KAAK;AAC5B,SAAK,mBAAmB;AAExB,WAAO,QAAQ,QAAQ;AAAA,EACzB;AAAA,EAEO,UAAU,UAAsB;AACrC,SAAK,aAAa,IAAI,QAAQ;AAC9B,WAAO,MAAM,KAAK,aAAa,OAAO,QAAQ;AAAA,EAChD;AAAA,EAEQ,qBAAqB;AAC3B,eAAW,YAAY,KAAK,aAAc,UAAS;AAAA,EACrD;AACF;","names":[]}