@assistant-ui/react 0.0.29 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. package/dist/AssistantRuntime-BM_jVV3g.d.mts +23 -0
  2. package/dist/AssistantRuntime-CBMSAJqH.d.mts +23 -0
  3. package/dist/AssistantRuntime-C_BvM7ZT.d.ts +23 -0
  4. package/dist/AssistantRuntime-DELpXWfG.d.ts +23 -0
  5. package/dist/ModelConfigTypes-B9UY4zxv.d.mts +55 -0
  6. package/dist/ModelConfigTypes-B9UY4zxv.d.ts +55 -0
  7. package/dist/ModelConfigTypes-BF5HxVrH.d.mts +55 -0
  8. package/dist/ModelConfigTypes-BF5HxVrH.d.ts +55 -0
  9. package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +55 -0
  10. package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +55 -0
  11. package/dist/chunk-3XZUKECF.mjs +207 -0
  12. package/dist/chunk-3XZUKECF.mjs.map +1 -0
  13. package/dist/chunk-4DQ2CIAD.mjs +69 -0
  14. package/dist/chunk-4DQ2CIAD.mjs.map +1 -0
  15. package/dist/chunk-5YONSDN4.mjs +200 -0
  16. package/dist/chunk-5YONSDN4.mjs.map +1 -0
  17. package/dist/chunk-CY4TTHR7.mjs +76 -0
  18. package/dist/chunk-CY4TTHR7.mjs.map +1 -0
  19. package/dist/chunk-DKAWDNW5.mjs +22 -0
  20. package/dist/chunk-DKAWDNW5.mjs.map +1 -0
  21. package/dist/chunk-GQKH2ADD.mjs +165 -0
  22. package/dist/chunk-GQKH2ADD.mjs.map +1 -0
  23. package/dist/chunk-J5LGTIGS.mjs +10 -0
  24. package/dist/chunk-J5LGTIGS.mjs.map +1 -0
  25. package/dist/chunk-X4HBDEFP.mjs +30 -0
  26. package/dist/chunk-X4HBDEFP.mjs.map +1 -0
  27. package/dist/experimental.d.mts +120 -0
  28. package/dist/experimental.d.ts +120 -0
  29. package/dist/experimental.js +407 -0
  30. package/dist/experimental.js.map +1 -0
  31. package/dist/experimental.mjs +144 -0
  32. package/dist/experimental.mjs.map +1 -0
  33. package/dist/index.d.mts +6 -266
  34. package/dist/index.d.ts +6 -266
  35. package/dist/index.js +77 -730
  36. package/dist/index.js.map +1 -1
  37. package/dist/index.mjs +62 -786
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/internal-dlLjX30u.d.mts +75 -0
  40. package/dist/internal-dlLjX30u.d.ts +75 -0
  41. package/dist/internal.d.mts +25 -0
  42. package/dist/internal.d.ts +25 -0
  43. package/dist/internal.js +225 -0
  44. package/dist/internal.js.map +1 -0
  45. package/dist/internal.mjs +12 -0
  46. package/dist/internal.mjs.map +1 -0
  47. package/package.json +23 -3
@@ -0,0 +1,120 @@
1
+ import { h as ModelConfigProvider, b as ThreadMessage, M as ModelConfig, e as AssistantContentPart, d as AppendMessage } from './ModelConfigTypes-B9UY4zxv.mjs';
2
+ export { I as ImageContentPart, a as ToolCallContentPart, U as UIContentPart } from './ModelConfigTypes-B9UY4zxv.mjs';
3
+ import { U as Unsubscribe, a as ThreadState, A as AssistantRuntime } from './AssistantRuntime-CBMSAJqH.mjs';
4
+ import { ReactNode } from 'react';
5
+ import { UseBoundStore, StoreApi } from 'zustand';
6
+ import 'zod';
7
+
8
+ type AssistantModelConfigState = {
9
+ getModelConfig: ModelConfigProvider;
10
+ registerModelConfigProvider: (provider: ModelConfigProvider) => () => void;
11
+ };
12
+
13
+ type ContentPartState = Readonly<{
14
+ status: "in_progress" | "done" | "error";
15
+ part: ThreadMessage["content"][number];
16
+ }>;
17
+
18
+ type MessageState = Readonly<{
19
+ message: Readonly<ThreadMessage>;
20
+ parentId: string | null;
21
+ branches: readonly string[];
22
+ isLast: boolean;
23
+ inProgressIndicator: ReactNode | null;
24
+ setInProgressIndicator: (value: ReactNode | null) => void;
25
+ isCopied: boolean;
26
+ setIsCopied: (value: boolean) => void;
27
+ isHovering: boolean;
28
+ setIsHovering: (value: boolean) => void;
29
+ }>;
30
+
31
+ type BaseComposerState = Readonly<{
32
+ value: string;
33
+ setValue: (value: string) => void;
34
+ }>;
35
+
36
+ type EditComposerState = BaseComposerState & Readonly<{
37
+ isEditing: boolean;
38
+ edit: () => void;
39
+ send: () => void;
40
+ cancel: () => boolean;
41
+ }>;
42
+
43
+ type ComposerState = BaseComposerState & Readonly<{
44
+ isEditing: true;
45
+ send: () => void;
46
+ cancel: () => boolean;
47
+ }>;
48
+
49
+ type ThreadViewportState = {
50
+ isAtBottom: boolean;
51
+ scrollToBottom: () => void;
52
+ onScrollToBottom: (callback: () => void) => Unsubscribe;
53
+ };
54
+
55
+ type AssistantContextValue = {
56
+ useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;
57
+ };
58
+ declare const useAssistantContext: () => AssistantContextValue;
59
+
60
+ type ThreadContextValue = {
61
+ useThread: UseBoundStore<StoreApi<ThreadState>>;
62
+ useComposer: UseBoundStore<StoreApi<ComposerState>>;
63
+ useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
64
+ };
65
+ declare const useThreadContext: () => ThreadContextValue;
66
+
67
+ type ComposerContextValue = {
68
+ useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
69
+ type: "edit" | "new";
70
+ };
71
+ declare const useComposerContext: () => ComposerContextValue;
72
+
73
+ type MessageContextValue = {
74
+ useMessage: UseBoundStore<StoreApi<MessageState>>;
75
+ useComposer: UseBoundStore<StoreApi<EditComposerState>>;
76
+ };
77
+ declare const useMessageContext: () => MessageContextValue;
78
+
79
+ type ContentPartContextValue = {
80
+ useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
81
+ };
82
+ declare const useContentPartContext: () => ContentPartContextValue;
83
+
84
+ type ChatModelRunResult = {
85
+ content: AssistantContentPart[];
86
+ };
87
+ type ChatModelRunOptions = {
88
+ messages: ThreadMessage[];
89
+ abortSignal: AbortSignal;
90
+ config: ModelConfig;
91
+ onUpdate: (result: ChatModelRunResult) => void;
92
+ };
93
+ type ChatModelAdapter = {
94
+ run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
95
+ };
96
+
97
+ declare class LocalRuntime implements AssistantRuntime {
98
+ adapter: ChatModelAdapter;
99
+ private _subscriptions;
100
+ private _configProviders;
101
+ private abortController;
102
+ private repository;
103
+ get messages(): ThreadMessage[];
104
+ get isRunning(): boolean;
105
+ constructor(adapter: ChatModelAdapter);
106
+ getBranches(messageId: string): string[];
107
+ switchToBranch(branchId: string): void;
108
+ append(message: AppendMessage): Promise<void>;
109
+ startRun(parentId: string | null): Promise<void>;
110
+ cancelRun(): void;
111
+ private notifySubscribers;
112
+ subscribe(callback: () => void): Unsubscribe;
113
+ registerModelConfigProvider(provider: ModelConfigProvider): () => boolean;
114
+ }
115
+
116
+ declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
117
+
118
+ declare const useAssistantInstructions: (instruction: string) => void;
119
+
120
+ export { type AssistantContextValue, type AssistantModelConfigState, type ChatModelAdapter, type ChatModelRunOptions, type ComposerContextValue, type ComposerState, type ContentPartContextValue, type ContentPartState, type EditComposerState, type MessageContextValue, type MessageState, ModelConfigProvider, type ThreadContextValue, ThreadState, type ThreadViewportState, useAssistantContext, useAssistantInstructions, useComposerContext, useContentPartContext, useLocalRuntime, useMessageContext, useThreadContext };
@@ -0,0 +1,120 @@
1
+ import { h as ModelConfigProvider, b as ThreadMessage, M as ModelConfig, e as AssistantContentPart, d as AppendMessage } from './ModelConfigTypes-B9UY4zxv.js';
2
+ export { I as ImageContentPart, a as ToolCallContentPart, U as UIContentPart } from './ModelConfigTypes-B9UY4zxv.js';
3
+ import { U as Unsubscribe, a as ThreadState, A as AssistantRuntime } from './AssistantRuntime-C_BvM7ZT.js';
4
+ import { ReactNode } from 'react';
5
+ import { UseBoundStore, StoreApi } from 'zustand';
6
+ import 'zod';
7
+
8
+ type AssistantModelConfigState = {
9
+ getModelConfig: ModelConfigProvider;
10
+ registerModelConfigProvider: (provider: ModelConfigProvider) => () => void;
11
+ };
12
+
13
+ type ContentPartState = Readonly<{
14
+ status: "in_progress" | "done" | "error";
15
+ part: ThreadMessage["content"][number];
16
+ }>;
17
+
18
+ type MessageState = Readonly<{
19
+ message: Readonly<ThreadMessage>;
20
+ parentId: string | null;
21
+ branches: readonly string[];
22
+ isLast: boolean;
23
+ inProgressIndicator: ReactNode | null;
24
+ setInProgressIndicator: (value: ReactNode | null) => void;
25
+ isCopied: boolean;
26
+ setIsCopied: (value: boolean) => void;
27
+ isHovering: boolean;
28
+ setIsHovering: (value: boolean) => void;
29
+ }>;
30
+
31
+ type BaseComposerState = Readonly<{
32
+ value: string;
33
+ setValue: (value: string) => void;
34
+ }>;
35
+
36
+ type EditComposerState = BaseComposerState & Readonly<{
37
+ isEditing: boolean;
38
+ edit: () => void;
39
+ send: () => void;
40
+ cancel: () => boolean;
41
+ }>;
42
+
43
+ type ComposerState = BaseComposerState & Readonly<{
44
+ isEditing: true;
45
+ send: () => void;
46
+ cancel: () => boolean;
47
+ }>;
48
+
49
+ type ThreadViewportState = {
50
+ isAtBottom: boolean;
51
+ scrollToBottom: () => void;
52
+ onScrollToBottom: (callback: () => void) => Unsubscribe;
53
+ };
54
+
55
+ type AssistantContextValue = {
56
+ useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;
57
+ };
58
+ declare const useAssistantContext: () => AssistantContextValue;
59
+
60
+ type ThreadContextValue = {
61
+ useThread: UseBoundStore<StoreApi<ThreadState>>;
62
+ useComposer: UseBoundStore<StoreApi<ComposerState>>;
63
+ useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
64
+ };
65
+ declare const useThreadContext: () => ThreadContextValue;
66
+
67
+ type ComposerContextValue = {
68
+ useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
69
+ type: "edit" | "new";
70
+ };
71
+ declare const useComposerContext: () => ComposerContextValue;
72
+
73
+ type MessageContextValue = {
74
+ useMessage: UseBoundStore<StoreApi<MessageState>>;
75
+ useComposer: UseBoundStore<StoreApi<EditComposerState>>;
76
+ };
77
+ declare const useMessageContext: () => MessageContextValue;
78
+
79
+ type ContentPartContextValue = {
80
+ useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
81
+ };
82
+ declare const useContentPartContext: () => ContentPartContextValue;
83
+
84
+ type ChatModelRunResult = {
85
+ content: AssistantContentPart[];
86
+ };
87
+ type ChatModelRunOptions = {
88
+ messages: ThreadMessage[];
89
+ abortSignal: AbortSignal;
90
+ config: ModelConfig;
91
+ onUpdate: (result: ChatModelRunResult) => void;
92
+ };
93
+ type ChatModelAdapter = {
94
+ run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
95
+ };
96
+
97
+ declare class LocalRuntime implements AssistantRuntime {
98
+ adapter: ChatModelAdapter;
99
+ private _subscriptions;
100
+ private _configProviders;
101
+ private abortController;
102
+ private repository;
103
+ get messages(): ThreadMessage[];
104
+ get isRunning(): boolean;
105
+ constructor(adapter: ChatModelAdapter);
106
+ getBranches(messageId: string): string[];
107
+ switchToBranch(branchId: string): void;
108
+ append(message: AppendMessage): Promise<void>;
109
+ startRun(parentId: string | null): Promise<void>;
110
+ cancelRun(): void;
111
+ private notifySubscribers;
112
+ subscribe(callback: () => void): Unsubscribe;
113
+ registerModelConfigProvider(provider: ModelConfigProvider): () => boolean;
114
+ }
115
+
116
+ declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
117
+
118
+ declare const useAssistantInstructions: (instruction: string) => void;
119
+
120
+ export { type AssistantContextValue, type AssistantModelConfigState, type ChatModelAdapter, type ChatModelRunOptions, type ComposerContextValue, type ComposerState, type ContentPartContextValue, type ContentPartState, type EditComposerState, type MessageContextValue, type MessageState, ModelConfigProvider, type ThreadContextValue, ThreadState, type ThreadViewportState, useAssistantContext, useAssistantInstructions, useComposerContext, useContentPartContext, useLocalRuntime, useMessageContext, useThreadContext };
@@ -0,0 +1,407 @@
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/experimental.ts
21
+ var experimental_exports = {};
22
+ __export(experimental_exports, {
23
+ useAssistantContext: () => useAssistantContext,
24
+ useAssistantInstructions: () => useAssistantInstructions,
25
+ useComposerContext: () => useComposerContext,
26
+ useContentPartContext: () => useContentPartContext,
27
+ useLocalRuntime: () => useLocalRuntime,
28
+ useMessageContext: () => useMessageContext,
29
+ useThreadContext: () => useThreadContext
30
+ });
31
+ module.exports = __toCommonJS(experimental_exports);
32
+
33
+ // src/runtime/local/useLocalRuntime.tsx
34
+ var import_react = require("react");
35
+
36
+ // src/utils/ModelConfigTypes.ts
37
+ var mergeModelConfigs = (configs) => {
38
+ return configs.reduce((acc, config) => {
39
+ if (config.system) {
40
+ if (acc.system) {
41
+ acc.system += `
42
+
43
+ ${config.system}`;
44
+ } else {
45
+ acc.system = config.system;
46
+ }
47
+ }
48
+ if (config.tools) {
49
+ acc.tools = { ...acc.tools, ...config.tools };
50
+ }
51
+ return acc;
52
+ }, {});
53
+ };
54
+
55
+ // src/runtime/utils/idUtils.tsx
56
+ var import_non_secure = require("nanoid/non-secure");
57
+ var generateId = (0, import_non_secure.customAlphabet)(
58
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
59
+ 7
60
+ );
61
+ var optimisticPrefix = "__optimistic__";
62
+ var generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;
63
+
64
+ // src/runtime/utils/MessageRepository.tsx
65
+ var findHead = (message) => {
66
+ if (message.next) return findHead(message.next);
67
+ return message;
68
+ };
69
+ var MessageRepository = class {
70
+ messages = /* @__PURE__ */ new Map();
71
+ // message_id -> item
72
+ head = null;
73
+ root = {
74
+ children: []
75
+ };
76
+ performOp(newParent, child, operation) {
77
+ const parentOrRoot = child.prev ?? this.root;
78
+ const newParentOrRoot = newParent ?? this.root;
79
+ if (operation === "relink" && parentOrRoot === newParentOrRoot) return;
80
+ if (operation !== "link") {
81
+ parentOrRoot.children = parentOrRoot.children.filter(
82
+ (m) => m !== child.current.id
83
+ );
84
+ if (child.prev?.next === child) {
85
+ const fallbackId = child.prev.children.at(-1);
86
+ const fallback = fallbackId ? this.messages.get(fallbackId) : null;
87
+ if (fallback === void 0) {
88
+ throw new Error(
89
+ "MessageRepository(performOp/cut): Fallback sibling message not found. This is likely an internal bug in assistant-ui."
90
+ );
91
+ }
92
+ child.prev.next = fallback;
93
+ }
94
+ }
95
+ if (operation !== "cut") {
96
+ newParentOrRoot.children = [
97
+ ...newParentOrRoot.children,
98
+ child.current.id
99
+ ];
100
+ if (newParent && (findHead(child) === this.head || newParent.next === null)) {
101
+ newParent.next = child;
102
+ }
103
+ child.prev = newParent;
104
+ }
105
+ }
106
+ getMessages() {
107
+ const messages = new Array(this.head?.level ?? 0);
108
+ for (let current = this.head; current; current = current.prev) {
109
+ messages[current.level] = current.current;
110
+ }
111
+ return messages;
112
+ }
113
+ addOrUpdateMessage(parentId, message) {
114
+ const existingItem = this.messages.get(message.id);
115
+ const prev = parentId ? this.messages.get(parentId) : null;
116
+ if (prev === void 0)
117
+ throw new Error(
118
+ "MessageRepository(addOrUpdateMessage): Parent message not found. This is likely an internal bug in assistant-ui."
119
+ );
120
+ if (existingItem) {
121
+ existingItem.current = message;
122
+ this.performOp(prev, existingItem, "relink");
123
+ return;
124
+ }
125
+ const newItem = {
126
+ prev,
127
+ current: message,
128
+ next: null,
129
+ children: [],
130
+ level: prev ? prev.level + 1 : 0
131
+ };
132
+ this.messages.set(message.id, newItem);
133
+ this.performOp(prev, newItem, "link");
134
+ if (this.head === prev) {
135
+ this.head = newItem;
136
+ }
137
+ }
138
+ appendOptimisticMessage(parentId, message) {
139
+ let optimisticId;
140
+ do {
141
+ optimisticId = generateOptimisticId();
142
+ } while (this.messages.has(optimisticId));
143
+ this.addOrUpdateMessage(parentId, {
144
+ ...message,
145
+ id: optimisticId,
146
+ createdAt: /* @__PURE__ */ new Date(),
147
+ ...message.role === "assistant" ? { status: "in_progress" } : void 0
148
+ });
149
+ return optimisticId;
150
+ }
151
+ deleteMessage(messageId, replacementId) {
152
+ const message = this.messages.get(messageId);
153
+ if (!message)
154
+ throw new Error(
155
+ "MessageRepository(deleteMessage): Optimistic message not found. This is likely an internal bug in assistant-ui."
156
+ );
157
+ const replacement = replacementId === void 0 ? message.prev : replacementId === null ? null : this.messages.get(replacementId);
158
+ if (replacement === void 0)
159
+ throw new Error(
160
+ "MessageRepository(deleteMessage): Replacement not found. This is likely an internal bug in assistant-ui."
161
+ );
162
+ for (const child of message.children) {
163
+ const childMessage = this.messages.get(child);
164
+ if (!childMessage)
165
+ throw new Error(
166
+ "MessageRepository(deleteMessage): Child message not found. This is likely an internal bug in assistant-ui."
167
+ );
168
+ this.performOp(replacement, childMessage, "relink");
169
+ }
170
+ this.performOp(null, message, "cut");
171
+ this.messages.delete(messageId);
172
+ if (this.head === message) {
173
+ this.head = replacement ? findHead(replacement) : null;
174
+ }
175
+ }
176
+ getBranches(messageId) {
177
+ const message = this.messages.get(messageId);
178
+ if (!message)
179
+ throw new Error(
180
+ "MessageRepository(getBranches): Message not found. This is likely an internal bug in assistant-ui."
181
+ );
182
+ const { children } = message.prev ?? this.root;
183
+ return children;
184
+ }
185
+ switchToBranch(messageId) {
186
+ const message = this.messages.get(messageId);
187
+ if (!message)
188
+ throw new Error(
189
+ "MessageRepository(switchToBranch): Branch not found. This is likely an internal bug in assistant-ui."
190
+ );
191
+ if (message.prev) {
192
+ message.prev.next = message;
193
+ }
194
+ this.head = findHead(message);
195
+ }
196
+ resetHead(messageId) {
197
+ if (messageId === null) {
198
+ this.head = null;
199
+ return;
200
+ }
201
+ const message = this.messages.get(messageId);
202
+ if (!message)
203
+ throw new Error(
204
+ "MessageRepository(resetHead): Branch not found. This is likely an internal bug in assistant-ui."
205
+ );
206
+ this.head = message;
207
+ for (let current = message; current; current = current.prev) {
208
+ if (current.prev) {
209
+ current.prev.next = current;
210
+ }
211
+ }
212
+ }
213
+ };
214
+
215
+ // src/runtime/local/LocalRuntime.tsx
216
+ var LocalRuntime = class {
217
+ constructor(adapter) {
218
+ this.adapter = adapter;
219
+ }
220
+ _subscriptions = /* @__PURE__ */ new Set();
221
+ _configProviders = /* @__PURE__ */ new Set();
222
+ abortController = null;
223
+ repository = new MessageRepository();
224
+ get messages() {
225
+ return this.repository.getMessages();
226
+ }
227
+ get isRunning() {
228
+ return this.abortController != null;
229
+ }
230
+ getBranches(messageId) {
231
+ return this.repository.getBranches(messageId);
232
+ }
233
+ switchToBranch(branchId) {
234
+ this.repository.switchToBranch(branchId);
235
+ this.notifySubscribers();
236
+ }
237
+ async append(message) {
238
+ const userMessageId = generateId();
239
+ const userMessage = {
240
+ id: userMessageId,
241
+ role: "user",
242
+ content: message.content,
243
+ createdAt: /* @__PURE__ */ new Date()
244
+ };
245
+ this.repository.addOrUpdateMessage(message.parentId, userMessage);
246
+ await this.startRun(userMessageId);
247
+ }
248
+ async startRun(parentId) {
249
+ const id = generateId();
250
+ this.repository.resetHead(parentId);
251
+ const messages = this.repository.getMessages();
252
+ const message = {
253
+ id,
254
+ role: "assistant",
255
+ status: "in_progress",
256
+ content: [{ type: "text", text: "" }],
257
+ createdAt: /* @__PURE__ */ new Date()
258
+ };
259
+ this.repository.addOrUpdateMessage(parentId, { ...message });
260
+ this.abortController?.abort();
261
+ this.abortController = new AbortController();
262
+ this.notifySubscribers();
263
+ try {
264
+ const updateHandler = ({ content }) => {
265
+ message.content = content;
266
+ this.repository.addOrUpdateMessage(parentId, { ...message });
267
+ this.notifySubscribers();
268
+ };
269
+ const result = await this.adapter.run({
270
+ messages,
271
+ abortSignal: this.abortController.signal,
272
+ config: mergeModelConfigs([...this._configProviders].map((p) => p())),
273
+ onUpdate: updateHandler
274
+ });
275
+ updateHandler(result);
276
+ message.status = "done";
277
+ this.repository.addOrUpdateMessage(parentId, { ...message });
278
+ } catch (e) {
279
+ message.status = "error";
280
+ this.repository.addOrUpdateMessage(parentId, { ...message });
281
+ console.error(e);
282
+ } finally {
283
+ this.abortController = null;
284
+ this.notifySubscribers();
285
+ }
286
+ }
287
+ cancelRun() {
288
+ if (!this.abortController) return;
289
+ this.abortController.abort();
290
+ this.abortController = null;
291
+ this.notifySubscribers();
292
+ }
293
+ notifySubscribers() {
294
+ for (const callback of this._subscriptions) callback();
295
+ }
296
+ subscribe(callback) {
297
+ this._subscriptions.add(callback);
298
+ return () => this._subscriptions.delete(callback);
299
+ }
300
+ registerModelConfigProvider(provider) {
301
+ this._configProviders.add(provider);
302
+ return () => this._configProviders.delete(provider);
303
+ }
304
+ };
305
+
306
+ // src/runtime/local/useLocalRuntime.tsx
307
+ var useLocalRuntime = (adapter) => {
308
+ const [runtime] = (0, import_react.useState)(() => new LocalRuntime(adapter));
309
+ (0, import_react.useInsertionEffect)(() => {
310
+ runtime.adapter = adapter;
311
+ });
312
+ return runtime;
313
+ };
314
+
315
+ // src/context/AssistantContext.ts
316
+ var import_react2 = require("react");
317
+ var AssistantContext = (0, import_react2.createContext)(
318
+ null
319
+ );
320
+ var useAssistantContext = () => {
321
+ const context = (0, import_react2.useContext)(AssistantContext);
322
+ if (!context)
323
+ throw new Error(
324
+ "This component must be used within an AssistantRuntimeProvider."
325
+ );
326
+ return context;
327
+ };
328
+
329
+ // src/context/ThreadContext.ts
330
+ var import_react3 = require("react");
331
+ var ThreadContext = (0, import_react3.createContext)(null);
332
+ var useThreadContext = () => {
333
+ const context = (0, import_react3.useContext)(ThreadContext);
334
+ if (!context)
335
+ throw new Error("This component must be used within an AssistantRuntimeProvider.");
336
+ return context;
337
+ };
338
+
339
+ // src/context/ComposerContext.ts
340
+ var import_react5 = require("react");
341
+
342
+ // src/context/MessageContext.ts
343
+ var import_react4 = require("react");
344
+ var MessageContext = (0, import_react4.createContext)(null);
345
+ var useMessageContext = () => {
346
+ const context = (0, import_react4.useContext)(MessageContext);
347
+ if (!context)
348
+ throw new Error(
349
+ "This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />."
350
+ );
351
+ return context;
352
+ };
353
+
354
+ // src/context/ComposerContext.ts
355
+ var useComposerContext = () => {
356
+ const { useComposer } = useThreadContext();
357
+ const { useComposer: useEditComposer } = (0, import_react5.useContext)(MessageContext) ?? {};
358
+ return (0, import_react5.useMemo)(
359
+ () => ({
360
+ useComposer: useEditComposer ?? useComposer,
361
+ type: useEditComposer ? "edit" : "new"
362
+ }),
363
+ [useEditComposer, useComposer]
364
+ );
365
+ };
366
+
367
+ // src/context/ContentPartContext.ts
368
+ var import_react6 = require("react");
369
+ var ContentPartContext = (0, import_react6.createContext)(
370
+ null
371
+ );
372
+ var useContentPartContext = () => {
373
+ const context = (0, import_react6.useContext)(ContentPartContext);
374
+ if (!context)
375
+ throw new Error(
376
+ "This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >."
377
+ );
378
+ return context;
379
+ };
380
+
381
+ // src/model-config/useAssistantInstructions.tsx
382
+ var import_react7 = require("react");
383
+ var useAssistantInstructions = (instruction) => {
384
+ const { useModelConfig } = useAssistantContext();
385
+ const addContextProvider = useModelConfig(
386
+ (s) => s.registerModelConfigProvider
387
+ );
388
+ (0, import_react7.useEffect)(
389
+ () => addContextProvider(() => {
390
+ return {
391
+ system: instruction
392
+ };
393
+ }),
394
+ [addContextProvider, instruction]
395
+ );
396
+ };
397
+ // Annotate the CommonJS export names for ESM import in node:
398
+ 0 && (module.exports = {
399
+ useAssistantContext,
400
+ useAssistantInstructions,
401
+ useComposerContext,
402
+ useContentPartContext,
403
+ useLocalRuntime,
404
+ useMessageContext,
405
+ useThreadContext
406
+ });
407
+ //# sourceMappingURL=experimental.js.map