@assistant-ui/react 0.1.12 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1,19 +1,24 @@
1
- import {
2
- AssistantContext,
3
- ContentPartContext,
4
- MessageContext,
5
- ThreadContext,
6
- __export,
7
- useAssistantContext,
8
- useComposerContext,
9
- useContentPartContext,
10
- useMessageContext,
11
- useThreadContext
12
- } from "./chunk-KUACYNLE.mjs";
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
13
6
 
14
7
  // src/primitive-hooks/actionBar/useActionBarCopy.tsx
15
8
  import { useCallback } from "react";
16
9
 
10
+ // src/context/react/MessageContext.ts
11
+ import { createContext, useContext } from "react";
12
+ var MessageContext = createContext(null);
13
+ var useMessageContext = () => {
14
+ const context = useContext(MessageContext);
15
+ if (!context)
16
+ throw new Error(
17
+ "This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />."
18
+ );
19
+ return context;
20
+ };
21
+
17
22
  // src/utils/combined/useCombinedStore.ts
18
23
  import { useMemo } from "react";
19
24
 
@@ -90,6 +95,20 @@ var useActionBarEdit = () => {
90
95
 
91
96
  // src/primitive-hooks/actionBar/useActionBarReload.tsx
92
97
  import { useCallback as useCallback3 } from "react";
98
+
99
+ // src/context/react/ThreadContext.ts
100
+ import { createContext as createContext2, useContext as useContext2 } from "react";
101
+ var ThreadContext = createContext2(null);
102
+ var useThreadContext = () => {
103
+ const context = useContext2(ThreadContext);
104
+ if (!context)
105
+ throw new Error(
106
+ "This component must be used within an AssistantRuntimeProvider."
107
+ );
108
+ return context;
109
+ };
110
+
111
+ // src/primitive-hooks/actionBar/useActionBarReload.tsx
93
112
  var useActionBarReload = () => {
94
113
  const { useThread, useThreadActions, useComposer, useViewport } = useThreadContext();
95
114
  const { useMessage } = useMessageContext();
@@ -157,6 +176,309 @@ var useBranchPickerPrevious = () => {
157
176
 
158
177
  // src/primitive-hooks/composer/useComposerCancel.tsx
159
178
  import { useCallback as useCallback6 } from "react";
179
+
180
+ // src/context/providers/AssistantRuntimeProvider.tsx
181
+ import { memo } from "react";
182
+
183
+ // src/context/providers/AssistantProvider.tsx
184
+ import { useEffect as useEffect2, useInsertionEffect as useInsertionEffect2, useRef as useRef2, useState as useState2 } from "react";
185
+
186
+ // src/context/react/AssistantContext.ts
187
+ import { createContext as createContext3, useContext as useContext3 } from "react";
188
+ var AssistantContext = createContext3(
189
+ null
190
+ );
191
+ var useAssistantContext = () => {
192
+ const context = useContext3(AssistantContext);
193
+ if (!context)
194
+ throw new Error(
195
+ "This component must be used within an AssistantRuntimeProvider."
196
+ );
197
+ return context;
198
+ };
199
+
200
+ // src/context/stores/AssistantModelConfig.ts
201
+ import { create } from "zustand";
202
+
203
+ // src/types/ModelConfigTypes.ts
204
+ var mergeModelConfigs = (configSet) => {
205
+ const configs = Array.from(configSet).map((c) => c()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
206
+ return configs.reduce((acc, config) => {
207
+ if (config.system) {
208
+ if (acc.system) {
209
+ acc.system += `
210
+
211
+ ${config.system}`;
212
+ } else {
213
+ acc.system = config.system;
214
+ }
215
+ }
216
+ if (config.tools) {
217
+ for (const [name, tool] of Object.entries(config.tools)) {
218
+ if (acc.tools?.[name]) {
219
+ throw new Error(
220
+ `You tried to define a tool with the name ${name}, but it already exists.`
221
+ );
222
+ }
223
+ if (!acc.tools) acc.tools = {};
224
+ acc.tools[name] = tool;
225
+ }
226
+ }
227
+ return acc;
228
+ }, {});
229
+ };
230
+
231
+ // src/utils/ProxyConfigProvider.ts
232
+ var ProxyConfigProvider = class {
233
+ _providers = /* @__PURE__ */ new Set();
234
+ getModelConfig() {
235
+ return mergeModelConfigs(this._providers);
236
+ }
237
+ registerModelConfigProvider(provider) {
238
+ this._providers.add(provider);
239
+ return () => {
240
+ this._providers.delete(provider);
241
+ };
242
+ }
243
+ };
244
+
245
+ // src/context/stores/AssistantModelConfig.ts
246
+ var makeAssistantModelConfigStore = () => create(() => {
247
+ const proxy = new ProxyConfigProvider();
248
+ return Object.freeze({
249
+ getModelConfig: () => {
250
+ return proxy.getModelConfig();
251
+ },
252
+ registerModelConfigProvider: (provider) => {
253
+ return proxy.registerModelConfigProvider(provider);
254
+ }
255
+ });
256
+ });
257
+
258
+ // src/context/stores/AssistantToolUIs.ts
259
+ import { create as create2 } from "zustand";
260
+ var makeAssistantToolUIsStore = () => create2((set) => {
261
+ const renderers = /* @__PURE__ */ new Map();
262
+ return Object.freeze({
263
+ getToolUI: (name) => {
264
+ const arr = renderers.get(name);
265
+ const last = arr?.at(-1);
266
+ if (last) return last;
267
+ return null;
268
+ },
269
+ setToolUI: (name, render) => {
270
+ let arr = renderers.get(name);
271
+ if (!arr) {
272
+ arr = [];
273
+ renderers.set(name, arr);
274
+ }
275
+ arr.push(render);
276
+ set({});
277
+ return () => {
278
+ const index = arr.indexOf(render);
279
+ if (index !== -1) {
280
+ arr.splice(index, 1);
281
+ }
282
+ if (index === arr.length) {
283
+ set({});
284
+ }
285
+ };
286
+ }
287
+ });
288
+ });
289
+
290
+ // src/context/providers/ThreadProvider.tsx
291
+ import { useEffect, useInsertionEffect, useRef, useState } from "react";
292
+
293
+ // src/context/stores/Composer.ts
294
+ import { create as create3 } from "zustand";
295
+
296
+ // src/context/stores/BaseComposer.ts
297
+ var makeBaseComposer = (set) => ({
298
+ value: "",
299
+ setValue: (value) => {
300
+ set({ value });
301
+ }
302
+ });
303
+
304
+ // src/context/stores/Composer.ts
305
+ var makeComposerStore = (useThread, useThreadActions) => {
306
+ const focusListeners = /* @__PURE__ */ new Set();
307
+ return create3()((set, get, store) => {
308
+ return {
309
+ ...makeBaseComposer(set, get, store),
310
+ isEditing: true,
311
+ send: () => {
312
+ const { setValue, value } = get();
313
+ setValue("");
314
+ useThreadActions.getState().append({
315
+ parentId: useThread.getState().messages.at(-1)?.id ?? null,
316
+ role: "user",
317
+ content: [{ type: "text", text: value }]
318
+ });
319
+ },
320
+ cancel: () => {
321
+ const thread = useThread.getState();
322
+ if (!thread.isRunning) return false;
323
+ useThreadActions.getState().cancelRun();
324
+ return true;
325
+ },
326
+ focus: () => {
327
+ for (const listener of focusListeners) {
328
+ listener();
329
+ }
330
+ },
331
+ onFocus: (listener) => {
332
+ focusListeners.add(listener);
333
+ return () => {
334
+ focusListeners.delete(listener);
335
+ };
336
+ }
337
+ };
338
+ });
339
+ };
340
+
341
+ // src/context/stores/Thread.ts
342
+ import { create as create4 } from "zustand";
343
+ var makeThreadStore = (runtimeRef) => {
344
+ return create4(() => ({
345
+ messages: runtimeRef.current.messages,
346
+ isRunning: runtimeRef.current.isRunning
347
+ }));
348
+ };
349
+
350
+ // src/context/stores/ThreadViewport.tsx
351
+ import { create as create5 } from "zustand";
352
+ var makeThreadViewportStore = () => {
353
+ const scrollToBottomListeners = /* @__PURE__ */ new Set();
354
+ return create5(() => ({
355
+ isAtBottom: true,
356
+ scrollToBottom: () => {
357
+ for (const listener of scrollToBottomListeners) {
358
+ listener();
359
+ }
360
+ },
361
+ onScrollToBottom: (callback) => {
362
+ scrollToBottomListeners.add(callback);
363
+ return () => {
364
+ scrollToBottomListeners.delete(callback);
365
+ };
366
+ }
367
+ }));
368
+ };
369
+
370
+ // src/context/stores/ThreadActions.ts
371
+ import { create as create6 } from "zustand";
372
+ var makeThreadActionStore = (runtimeRef) => {
373
+ return create6(
374
+ () => Object.freeze({
375
+ getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
376
+ switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
377
+ startRun: (parentId) => runtimeRef.current.startRun(parentId),
378
+ append: (message) => runtimeRef.current.append(message),
379
+ cancelRun: () => runtimeRef.current.cancelRun(),
380
+ addToolResult: (toolCallId, result) => runtimeRef.current.addToolResult(toolCallId, result)
381
+ })
382
+ );
383
+ };
384
+
385
+ // src/context/providers/ThreadProvider.tsx
386
+ import { jsx, jsxs } from "react/jsx-runtime";
387
+ var ThreadProvider = ({
388
+ children,
389
+ runtime
390
+ }) => {
391
+ const runtimeRef = useRef(runtime);
392
+ useInsertionEffect(() => {
393
+ runtimeRef.current = runtime;
394
+ });
395
+ const [context] = useState(() => {
396
+ const useThread = makeThreadStore(runtimeRef);
397
+ const useThreadActions = makeThreadActionStore(runtimeRef);
398
+ const useViewport = makeThreadViewportStore();
399
+ const useComposer = makeComposerStore(useThread, useThreadActions);
400
+ return {
401
+ useThread,
402
+ useThreadActions,
403
+ useComposer,
404
+ useViewport
405
+ };
406
+ });
407
+ useEffect(() => {
408
+ const onRuntimeUpdate = () => {
409
+ context.useThread.setState(
410
+ Object.freeze({
411
+ messages: runtimeRef.current.messages,
412
+ isRunning: runtimeRef.current.isRunning
413
+ }),
414
+ true
415
+ );
416
+ };
417
+ onRuntimeUpdate();
418
+ return runtime.subscribe(onRuntimeUpdate);
419
+ }, [context, runtime]);
420
+ const RuntimeSynchronizer = runtime.unstable_synchronizer;
421
+ return /* @__PURE__ */ jsxs(ThreadContext.Provider, { value: context, children: [
422
+ RuntimeSynchronizer && /* @__PURE__ */ jsx(RuntimeSynchronizer, {}),
423
+ children
424
+ ] });
425
+ };
426
+
427
+ // src/context/providers/AssistantProvider.tsx
428
+ import { jsx as jsx2 } from "react/jsx-runtime";
429
+ var AssistantProvider = ({ children, runtime }) => {
430
+ const runtimeRef = useRef2(runtime);
431
+ useInsertionEffect2(() => {
432
+ runtimeRef.current = runtime;
433
+ });
434
+ const [context] = useState2(() => {
435
+ const useModelConfig = makeAssistantModelConfigStore();
436
+ const useToolUIs = makeAssistantToolUIsStore();
437
+ return { useModelConfig, useToolUIs };
438
+ });
439
+ const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
440
+ useEffect2(() => {
441
+ return runtime.registerModelConfigProvider(getModelCOnfig);
442
+ }, [runtime, getModelCOnfig]);
443
+ return /* @__PURE__ */ jsx2(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ jsx2(ThreadProvider, { runtime, children }) });
444
+ };
445
+
446
+ // src/context/providers/AssistantRuntimeProvider.tsx
447
+ import { jsx as jsx3 } from "react/jsx-runtime";
448
+ var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
449
+ return /* @__PURE__ */ jsx3(AssistantProvider, { runtime, children });
450
+ };
451
+ var AssistantRuntimeProvider = memo(AssistantRuntimeProviderImpl);
452
+
453
+ // src/context/react/ComposerContext.ts
454
+ import { useContext as useContext4, useMemo as useMemo2 } from "react";
455
+ var useComposerContext = () => {
456
+ const { useComposer } = useThreadContext();
457
+ const { useEditComposer } = useContext4(MessageContext) ?? {};
458
+ return useMemo2(
459
+ () => ({
460
+ useComposer: useEditComposer ?? useComposer,
461
+ type: useEditComposer ? "edit" : "new"
462
+ }),
463
+ [useEditComposer, useComposer]
464
+ );
465
+ };
466
+
467
+ // src/context/react/ContentPartContext.ts
468
+ import { createContext as createContext4, useContext as useContext5 } from "react";
469
+ var ContentPartContext = createContext4(
470
+ null
471
+ );
472
+ var useContentPartContext = () => {
473
+ const context = useContext5(ContentPartContext);
474
+ if (!context)
475
+ throw new Error(
476
+ "This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >."
477
+ );
478
+ return context;
479
+ };
480
+
481
+ // src/primitive-hooks/composer/useComposerCancel.tsx
160
482
  var useComposerCancel = () => {
161
483
  const { useComposer } = useComposerContext();
162
484
  const disabled = useComposer((c) => !c.isEditing);
@@ -324,7 +646,7 @@ __export(actionBar_exports, {
324
646
  // src/primitives/actionBar/ActionBarRoot.tsx
325
647
  import { Primitive } from "@radix-ui/react-primitive";
326
648
  import { forwardRef } from "react";
327
- import { jsx } from "react/jsx-runtime";
649
+ import { jsx as jsx4 } from "react/jsx-runtime";
328
650
  var useActionBarFloatStatus = ({
329
651
  hideWhenRunning,
330
652
  autohide,
@@ -352,7 +674,7 @@ var ActionBarRoot = forwardRef(({ hideWhenRunning, autohide, autohideFloat, ...r
352
674
  autohideFloat
353
675
  });
354
676
  if (hideAndfloatStatus === "hidden" /* Hidden */) return null;
355
- return /* @__PURE__ */ jsx(
677
+ return /* @__PURE__ */ jsx4(
356
678
  Primitive.div,
357
679
  {
358
680
  ...hideAndfloatStatus === "floating" /* Floating */ ? { "data-floating": "true" } : null,
@@ -367,11 +689,11 @@ ActionBarRoot.displayName = "ActionBarRoot";
367
689
  import { composeEventHandlers } from "@radix-ui/primitive";
368
690
  import { Primitive as Primitive2 } from "@radix-ui/react-primitive";
369
691
  import { forwardRef as forwardRef2 } from "react";
370
- import { jsx as jsx2 } from "react/jsx-runtime";
692
+ import { jsx as jsx5 } from "react/jsx-runtime";
371
693
  var createActionButton = (displayName, useActionButton) => {
372
694
  const ActionButton = forwardRef2((props, forwardedRef) => {
373
695
  const callback = useActionButton(props);
374
- return /* @__PURE__ */ jsx2(
696
+ return /* @__PURE__ */ jsx5(
375
697
  Primitive2.button,
376
698
  {
377
699
  type: "button",
@@ -415,17 +737,17 @@ __export(assistantModal_exports, {
415
737
  });
416
738
 
417
739
  // src/primitives/assistantModal/AssistantModalRoot.tsx
418
- import { useState } from "react";
740
+ import { useState as useState3 } from "react";
419
741
  import * as PopoverPrimitive from "@radix-ui/react-popover";
420
742
  import { composeEventHandlers as composeEventHandlers2 } from "@radix-ui/primitive";
421
743
 
422
744
  // src/utils/hooks/useOnComposerFocus.tsx
423
745
  import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
424
- import { useEffect } from "react";
746
+ import { useEffect as useEffect3 } from "react";
425
747
  var useOnComposerFocus = (callback) => {
426
748
  const callbackRef = useCallbackRef(callback);
427
749
  const { useComposer } = useThreadContext();
428
- useEffect(() => {
750
+ useEffect3(() => {
429
751
  return useComposer.getState().onFocus(() => {
430
752
  callbackRef();
431
753
  });
@@ -433,10 +755,10 @@ var useOnComposerFocus = (callback) => {
433
755
  };
434
756
 
435
757
  // src/primitives/assistantModal/AssistantModalRoot.tsx
436
- import { jsx as jsx3 } from "react/jsx-runtime";
758
+ import { jsx as jsx6 } from "react/jsx-runtime";
437
759
  var usePopoverScope = PopoverPrimitive.createPopoverScope();
438
760
  var useAssistantModalOpenState = (defaultOpen = false) => {
439
- const state = useState(defaultOpen);
761
+ const state = useState3(defaultOpen);
440
762
  const [, setOpen] = state;
441
763
  useOnComposerFocus(() => {
442
764
  setOpen(true);
@@ -452,7 +774,7 @@ var AssistantModalRoot = ({
452
774
  }) => {
453
775
  const scope = usePopoverScope(__scopeAssistantModal);
454
776
  const [modalOpen, setOpen] = useAssistantModalOpenState(defaultOpen);
455
- return /* @__PURE__ */ jsx3(
777
+ return /* @__PURE__ */ jsx6(
456
778
  PopoverPrimitive.Root,
457
779
  {
458
780
  ...scope,
@@ -467,11 +789,11 @@ AssistantModalRoot.displayName = "AssistantModalRoot";
467
789
  // src/primitives/assistantModal/AssistantModalTrigger.tsx
468
790
  import { forwardRef as forwardRef3 } from "react";
469
791
  import * as PopoverPrimitive2 from "@radix-ui/react-popover";
470
- import { jsx as jsx4 } from "react/jsx-runtime";
792
+ import { jsx as jsx7 } from "react/jsx-runtime";
471
793
  var AssistantModalTrigger = forwardRef3(
472
794
  ({ __scopeAssistantModal, ...rest }, ref) => {
473
795
  const scope = usePopoverScope(__scopeAssistantModal);
474
- return /* @__PURE__ */ jsx4(PopoverPrimitive2.Trigger, { ...scope, ...rest, ref });
796
+ return /* @__PURE__ */ jsx7(PopoverPrimitive2.Trigger, { ...scope, ...rest, ref });
475
797
  }
476
798
  );
477
799
  AssistantModalTrigger.displayName = "AssistantModalTrigger";
@@ -480,7 +802,7 @@ AssistantModalTrigger.displayName = "AssistantModalTrigger";
480
802
  import { forwardRef as forwardRef4 } from "react";
481
803
  import * as PopoverPrimitive3 from "@radix-ui/react-popover";
482
804
  import { composeEventHandlers as composeEventHandlers3 } from "@radix-ui/primitive";
483
- import { jsx as jsx5 } from "react/jsx-runtime";
805
+ import { jsx as jsx8 } from "react/jsx-runtime";
484
806
  var AssistantModalContent = forwardRef4(
485
807
  ({
486
808
  __scopeAssistantModal,
@@ -491,7 +813,7 @@ var AssistantModalContent = forwardRef4(
491
813
  ...props
492
814
  }, forwardedRef) => {
493
815
  const scope = usePopoverScope(__scopeAssistantModal);
494
- return /* @__PURE__ */ jsx5(PopoverPrimitive3.Portal, { ...scope, children: /* @__PURE__ */ jsx5(
816
+ return /* @__PURE__ */ jsx8(PopoverPrimitive3.Portal, { ...scope, children: /* @__PURE__ */ jsx8(
495
817
  PopoverPrimitive3.Content,
496
818
  {
497
819
  ...scope,
@@ -532,17 +854,17 @@ var BranchPickerPrevious = createActionButton(
532
854
  );
533
855
 
534
856
  // src/primitives/branchPicker/BranchPickerCount.tsx
535
- import { Fragment, jsx as jsx6 } from "react/jsx-runtime";
857
+ import { Fragment, jsx as jsx9 } from "react/jsx-runtime";
536
858
  var BranchPickerCount = () => {
537
859
  const branchCount = useBranchPickerCount();
538
- return /* @__PURE__ */ jsx6(Fragment, { children: branchCount });
860
+ return /* @__PURE__ */ jsx9(Fragment, { children: branchCount });
539
861
  };
540
862
 
541
863
  // src/primitives/branchPicker/BranchPickerNumber.tsx
542
- import { Fragment as Fragment2, jsx as jsx7 } from "react/jsx-runtime";
864
+ import { Fragment as Fragment2, jsx as jsx10 } from "react/jsx-runtime";
543
865
  var BranchPickerNumber = () => {
544
866
  const branchNumber = useBranchPickerNumber();
545
- return /* @__PURE__ */ jsx7(Fragment2, { children: branchNumber });
867
+ return /* @__PURE__ */ jsx10(Fragment2, { children: branchNumber });
546
868
  };
547
869
 
548
870
  // src/primitives/branchPicker/BranchPickerRoot.tsx
@@ -562,7 +884,7 @@ __export(message_exports, {
562
884
  import { composeEventHandlers as composeEventHandlers4 } from "@radix-ui/primitive";
563
885
  import { Primitive as Primitive3 } from "@radix-ui/react-primitive";
564
886
  import { forwardRef as forwardRef5 } from "react";
565
- import { jsx as jsx8 } from "react/jsx-runtime";
887
+ import { jsx as jsx11 } from "react/jsx-runtime";
566
888
  var MessageRoot = forwardRef5(
567
889
  ({ onMouseEnter, onMouseLeave, ...rest }, ref) => {
568
890
  const { useMessageUtils } = useMessageContext();
@@ -573,7 +895,7 @@ var MessageRoot = forwardRef5(
573
895
  const handleMouseLeave = () => {
574
896
  setIsHovering(false);
575
897
  };
576
- return /* @__PURE__ */ jsx8(
898
+ return /* @__PURE__ */ jsx11(
577
899
  Primitive3.div,
578
900
  {
579
901
  ...rest,
@@ -593,12 +915,12 @@ var MessageIf = ({ children, ...query }) => {
593
915
  };
594
916
 
595
917
  // src/primitives/message/MessageContent.tsx
596
- import { memo } from "react";
918
+ import { memo as memo2 } from "react";
597
919
 
598
920
  // src/context/providers/ContentPartProvider.tsx
599
- import { useEffect as useEffect2, useState as useState2 } from "react";
600
- import { create } from "zustand";
601
- import { jsx as jsx9 } from "react/jsx-runtime";
921
+ import { useEffect as useEffect4, useState as useState4 } from "react";
922
+ import { create as create7 } from "zustand";
923
+ import { jsx as jsx12 } from "react/jsx-runtime";
602
924
  var syncContentPart = ({ message }, useContentPart, partIndex) => {
603
925
  const part = message.content[partIndex];
604
926
  if (!part) return;
@@ -615,14 +937,14 @@ var syncContentPart = ({ message }, useContentPart, partIndex) => {
615
937
  };
616
938
  var useContentPartContext2 = (partIndex) => {
617
939
  const { useMessage } = useMessageContext();
618
- const [context] = useState2(() => {
619
- const useContentPart = create(
940
+ const [context] = useState4(() => {
941
+ const useContentPart = create7(
620
942
  () => ({})
621
943
  );
622
944
  syncContentPart(useMessage.getState(), useContentPart, partIndex);
623
945
  return { useContentPart };
624
946
  });
625
- useEffect2(() => {
947
+ useEffect4(() => {
626
948
  syncContentPart(useMessage.getState(), context.useContentPart, partIndex);
627
949
  return useMessage.subscribe((message) => {
628
950
  syncContentPart(message, context.useContentPart, partIndex);
@@ -635,7 +957,7 @@ var ContentPartProvider = ({
635
957
  children
636
958
  }) => {
637
959
  const context = useContentPartContext2(partIndex);
638
- return /* @__PURE__ */ jsx9(ContentPartContext.Provider, { value: context, children });
960
+ return /* @__PURE__ */ jsx12(ContentPartContext.Provider, { value: context, children });
639
961
  };
640
962
 
641
963
  // src/primitives/contentPart/ContentPartDisplay.tsx
@@ -653,28 +975,28 @@ var ContentPartInProgressIndicator = () => {
653
975
  // src/primitives/contentPart/ContentPartText.tsx
654
976
  import { Primitive as Primitive4 } from "@radix-ui/react-primitive";
655
977
  import { forwardRef as forwardRef6 } from "react";
656
- import { jsx as jsx10 } from "react/jsx-runtime";
978
+ import { jsx as jsx13 } from "react/jsx-runtime";
657
979
  var ContentPartText = forwardRef6((props, forwardedRef) => {
658
980
  const text = useContentPartText();
659
- return /* @__PURE__ */ jsx10(Primitive4.span, { ...props, ref: forwardedRef, children: text });
981
+ return /* @__PURE__ */ jsx13(Primitive4.p, { ...props, ref: forwardedRef, children: text });
660
982
  });
661
983
  ContentPartText.displayName = "ContentPartText";
662
984
 
663
985
  // src/primitives/message/MessageContent.tsx
664
- import { Fragment as Fragment3, jsx as jsx11, jsxs } from "react/jsx-runtime";
986
+ import { Fragment as Fragment3, jsx as jsx14, jsxs as jsxs2 } from "react/jsx-runtime";
665
987
  var defaultComponents = {
666
- Text: () => /* @__PURE__ */ jsxs(Fragment3, { children: [
667
- /* @__PURE__ */ jsx11(ContentPartText, {}),
668
- /* @__PURE__ */ jsx11(ContentPartInProgressIndicator, {})
988
+ Text: () => /* @__PURE__ */ jsxs2(Fragment3, { children: [
989
+ /* @__PURE__ */ jsx14(ContentPartText, { style: { whiteSpace: "pre-line" } }),
990
+ /* @__PURE__ */ jsx14(ContentPartInProgressIndicator, {})
669
991
  ] }),
670
992
  Image: () => null,
671
- UI: () => /* @__PURE__ */ jsx11(ContentPartDisplay, {}),
993
+ UI: () => /* @__PURE__ */ jsx14(ContentPartDisplay, {}),
672
994
  tools: {
673
995
  Fallback: (props) => {
674
996
  const { useToolUIs } = useAssistantContext();
675
997
  const Render = useToolUIs((s) => s.getToolUI(props.part.toolName));
676
998
  if (!Render) return null;
677
- return /* @__PURE__ */ jsx11(Render, { ...props });
999
+ return /* @__PURE__ */ jsx14(Render, { ...props });
678
1000
  }
679
1001
  }
680
1002
  };
@@ -693,15 +1015,15 @@ var MessageContentPartComponent = ({
693
1015
  const type = part.type;
694
1016
  switch (type) {
695
1017
  case "text":
696
- return /* @__PURE__ */ jsx11(Text, { part, status });
1018
+ return /* @__PURE__ */ jsx14(Text, { part, status });
697
1019
  case "image":
698
- return /* @__PURE__ */ jsx11(Image, { part, status });
1020
+ return /* @__PURE__ */ jsx14(Image, { part, status });
699
1021
  case "ui":
700
- return /* @__PURE__ */ jsx11(UI, { part, status });
1022
+ return /* @__PURE__ */ jsx14(UI, { part, status });
701
1023
  case "tool-call": {
702
1024
  const Tool = by_name[part.toolName] || Fallback;
703
1025
  const addResult = (result) => addToolResult(part.toolCallId, result);
704
- return /* @__PURE__ */ jsx11(Tool, { part, status, addResult });
1026
+ return /* @__PURE__ */ jsx14(Tool, { part, status, addResult });
705
1027
  }
706
1028
  default:
707
1029
  throw new Error(`Unknown content part type: ${type}`);
@@ -711,9 +1033,9 @@ var MessageContentPartImpl = ({
711
1033
  partIndex,
712
1034
  components
713
1035
  }) => {
714
- return /* @__PURE__ */ jsx11(ContentPartProvider, { partIndex, children: /* @__PURE__ */ jsx11(MessageContentPartComponent, { components }) });
1036
+ return /* @__PURE__ */ jsx14(ContentPartProvider, { partIndex, children: /* @__PURE__ */ jsx14(MessageContentPartComponent, { components }) });
715
1037
  };
716
- var MessageContentPart = memo(
1038
+ var MessageContentPart = memo2(
717
1039
  MessageContentPartImpl,
718
1040
  (prev, next) => prev.partIndex === next.partIndex && prev.components?.Text === next.components?.Text && prev.components?.Image === next.components?.Image && prev.components?.UI === next.components?.UI && prev.components?.tools === next.components?.tools
719
1041
  );
@@ -722,7 +1044,7 @@ var MessageContent = ({ components }) => {
722
1044
  const contentLength = useMessage((s) => s.message.content.length);
723
1045
  return new Array(contentLength).fill(null).map((_, idx) => {
724
1046
  const partIndex = idx;
725
- return /* @__PURE__ */ jsx11(
1047
+ return /* @__PURE__ */ jsx14(
726
1048
  MessageContentPart,
727
1049
  {
728
1050
  partIndex,
@@ -737,22 +1059,22 @@ var MessageContent = ({ components }) => {
737
1059
  import { Primitive as Primitive5 } from "@radix-ui/react-primitive";
738
1060
  import {
739
1061
  forwardRef as forwardRef7,
740
- useMemo as useMemo2
1062
+ useMemo as useMemo3
741
1063
  } from "react";
742
- import { jsx as jsx12 } from "react/jsx-runtime";
1064
+ import { jsx as jsx15 } from "react/jsx-runtime";
743
1065
  var MessageInProgress = forwardRef7((props, ref) => {
744
1066
  const { useMessageUtils } = useMessageContext();
745
- useMemo2(() => {
746
- useMessageUtils.getState().setInProgressIndicator(/* @__PURE__ */ jsx12(Primitive5.span, { ...props, ref }));
1067
+ useMemo3(() => {
1068
+ useMessageUtils.getState().setInProgressIndicator(/* @__PURE__ */ jsx15(Primitive5.span, { ...props, ref }));
747
1069
  }, [useMessageUtils, props, ref]);
748
1070
  return null;
749
1071
  });
750
1072
  MessageInProgress.displayName = "MessageInProgress";
751
1073
 
752
1074
  // src/primitives/branchPicker/BranchPickerRoot.tsx
753
- import { jsx as jsx13 } from "react/jsx-runtime";
1075
+ import { jsx as jsx16 } from "react/jsx-runtime";
754
1076
  var BranchPickerRoot = forwardRef8(({ hideWhenSingleBranch, ...rest }, ref) => {
755
- return /* @__PURE__ */ jsx13(MessageIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ jsx13(Primitive6.div, { ...rest, ref }) });
1077
+ return /* @__PURE__ */ jsx16(MessageIf, { hasBranches: hideWhenSingleBranch ? true : void 0, children: /* @__PURE__ */ jsx16(Primitive6.div, { ...rest, ref }) });
756
1078
  });
757
1079
  BranchPickerRoot.displayName = "BranchPickerRoot";
758
1080
 
@@ -772,7 +1094,7 @@ import { Primitive as Primitive7 } from "@radix-ui/react-primitive";
772
1094
  import {
773
1095
  forwardRef as forwardRef9
774
1096
  } from "react";
775
- import { jsx as jsx14 } from "react/jsx-runtime";
1097
+ import { jsx as jsx17 } from "react/jsx-runtime";
776
1098
  var ComposerRoot = forwardRef9(
777
1099
  ({ onSubmit, ...rest }, forwardedRef) => {
778
1100
  const send = useComposerSend();
@@ -781,7 +1103,7 @@ var ComposerRoot = forwardRef9(
781
1103
  e.preventDefault();
782
1104
  send();
783
1105
  };
784
- return /* @__PURE__ */ jsx14(
1106
+ return /* @__PURE__ */ jsx17(
785
1107
  Primitive7.form,
786
1108
  {
787
1109
  ...rest,
@@ -800,12 +1122,12 @@ import { Slot } from "@radix-ui/react-slot";
800
1122
  import {
801
1123
  forwardRef as forwardRef10,
802
1124
  useCallback as useCallback10,
803
- useEffect as useEffect3,
804
- useRef
1125
+ useEffect as useEffect5,
1126
+ useRef as useRef3
805
1127
  } from "react";
806
1128
  import TextareaAutosize from "react-textarea-autosize";
807
1129
  import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
808
- import { jsx as jsx15 } from "react/jsx-runtime";
1130
+ import { jsx as jsx18 } from "react/jsx-runtime";
809
1131
  var ComposerInput = forwardRef10(
810
1132
  ({ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
811
1133
  const { useThread } = useThreadContext();
@@ -815,7 +1137,7 @@ var ComposerInput = forwardRef10(
815
1137
  return c.value;
816
1138
  });
817
1139
  const Component = asChild ? Slot : TextareaAutosize;
818
- const textareaRef = useRef(null);
1140
+ const textareaRef = useRef3(null);
819
1141
  const ref = useComposedRefs(forwardedRef, textareaRef);
820
1142
  useEscapeKeydown((e) => {
821
1143
  const composer = useComposer.getState();
@@ -843,13 +1165,13 @@ var ComposerInput = forwardRef10(
843
1165
  textareaRef.current.value.length
844
1166
  );
845
1167
  }, [autoFocusEnabled]);
846
- useEffect3(() => focus(), [focus]);
1168
+ useEffect5(() => focus(), [focus]);
847
1169
  useOnComposerFocus(() => {
848
1170
  if (type === "new") {
849
1171
  focus();
850
1172
  }
851
1173
  });
852
- return /* @__PURE__ */ jsx15(
1174
+ return /* @__PURE__ */ jsx18(
853
1175
  Component,
854
1176
  {
855
1177
  value,
@@ -871,12 +1193,12 @@ ComposerInput.displayName = "ComposerInput";
871
1193
  // src/primitives/composer/ComposerSend.tsx
872
1194
  import { forwardRef as forwardRef11 } from "react";
873
1195
  import { Primitive as Primitive8 } from "@radix-ui/react-primitive";
874
- import { jsx as jsx16 } from "react/jsx-runtime";
1196
+ import { jsx as jsx19 } from "react/jsx-runtime";
875
1197
  var ComposerSend = forwardRef11(
876
1198
  ({ disabled, ...rest }, ref) => {
877
1199
  const { useComposer } = useComposerContext();
878
1200
  const hasValue = useComposer((c) => c.isEditing && c.value.length > 0);
879
- return /* @__PURE__ */ jsx16(
1201
+ return /* @__PURE__ */ jsx19(
880
1202
  Primitive8.button,
881
1203
  {
882
1204
  type: "submit",
@@ -913,10 +1235,10 @@ __export(contentPart_exports, {
913
1235
  // src/primitives/contentPart/ContentPartImage.tsx
914
1236
  import { Primitive as Primitive9 } from "@radix-ui/react-primitive";
915
1237
  import { forwardRef as forwardRef12 } from "react";
916
- import { jsx as jsx17 } from "react/jsx-runtime";
1238
+ import { jsx as jsx20 } from "react/jsx-runtime";
917
1239
  var ContentPartImage = forwardRef12((props, forwardedRef) => {
918
1240
  const image = useContentPartImage();
919
- return /* @__PURE__ */ jsx17(Primitive9.img, { src: image, ...props, ref: forwardedRef });
1241
+ return /* @__PURE__ */ jsx20(Primitive9.img, { src: image, ...props, ref: forwardedRef });
920
1242
  });
921
1243
  ContentPartImage.displayName = "ContentPartImage";
922
1244
 
@@ -935,10 +1257,10 @@ __export(thread_exports, {
935
1257
  // src/primitives/thread/ThreadRoot.tsx
936
1258
  import { Primitive as Primitive10 } from "@radix-ui/react-primitive";
937
1259
  import { forwardRef as forwardRef13 } from "react";
938
- import { jsx as jsx18 } from "react/jsx-runtime";
1260
+ import { jsx as jsx21 } from "react/jsx-runtime";
939
1261
  var ThreadRoot = forwardRef13(
940
1262
  (props, ref) => {
941
- return /* @__PURE__ */ jsx18(Primitive10.div, { ...props, ref });
1263
+ return /* @__PURE__ */ jsx21(Primitive10.div, { ...props, ref });
942
1264
  }
943
1265
  );
944
1266
  ThreadRoot.displayName = "ThreadRoot";
@@ -962,16 +1284,16 @@ import { forwardRef as forwardRef14 } from "react";
962
1284
 
963
1285
  // src/primitive-hooks/thread/useThreadViewportAutoScroll.tsx
964
1286
  import { useComposedRefs as useComposedRefs2 } from "@radix-ui/react-compose-refs";
965
- import { useRef as useRef3 } from "react";
1287
+ import { useRef as useRef5 } from "react";
966
1288
 
967
1289
  // src/utils/hooks/useOnResizeContent.tsx
968
1290
  import { useCallbackRef as useCallbackRef2 } from "@radix-ui/react-use-callback-ref";
969
1291
  import { useCallback as useCallback12 } from "react";
970
1292
 
971
1293
  // src/utils/hooks/useManagedRef.ts
972
- import { useCallback as useCallback11, useRef as useRef2 } from "react";
1294
+ import { useCallback as useCallback11, useRef as useRef4 } from "react";
973
1295
  var useManagedRef = (callback) => {
974
- const cleanupRef = useRef2();
1296
+ const cleanupRef = useRef4();
975
1297
  const ref = useCallback11(
976
1298
  (el) => {
977
1299
  if (cleanupRef.current) {
@@ -1026,11 +1348,11 @@ var useOnResizeContent = (callback) => {
1026
1348
 
1027
1349
  // src/utils/hooks/useOnScrollToBottom.tsx
1028
1350
  import { useCallbackRef as useCallbackRef3 } from "@radix-ui/react-use-callback-ref";
1029
- import { useEffect as useEffect4 } from "react";
1351
+ import { useEffect as useEffect6 } from "react";
1030
1352
  var useOnScrollToBottom = (callback) => {
1031
1353
  const callbackRef = useCallbackRef3(callback);
1032
1354
  const { useViewport } = useThreadContext();
1033
- useEffect4(() => {
1355
+ useEffect6(() => {
1034
1356
  return useViewport.getState().onScrollToBottom(() => {
1035
1357
  callbackRef();
1036
1358
  });
@@ -1041,11 +1363,11 @@ var useOnScrollToBottom = (callback) => {
1041
1363
  var useThreadViewportAutoScroll = ({
1042
1364
  autoScroll = true
1043
1365
  }) => {
1044
- const divRef = useRef3(null);
1366
+ const divRef = useRef5(null);
1045
1367
  const { useViewport } = useThreadContext();
1046
- const firstRenderRef = useRef3(true);
1047
- const lastScrollTop = useRef3(0);
1048
- const isScrollingToBottomRef = useRef3(false);
1368
+ const firstRenderRef = useRef5(true);
1369
+ const lastScrollTop = useRef5(0);
1370
+ const isScrollingToBottomRef = useRef5(false);
1049
1371
  const scrollToBottom = () => {
1050
1372
  const div = divRef.current;
1051
1373
  if (!div || !autoScroll) return;
@@ -1091,39 +1413,29 @@ var useThreadViewportAutoScroll = ({
1091
1413
  };
1092
1414
 
1093
1415
  // src/primitives/thread/ThreadViewport.tsx
1094
- import { jsx as jsx19 } from "react/jsx-runtime";
1416
+ import { jsx as jsx22 } from "react/jsx-runtime";
1095
1417
  var ThreadViewport = forwardRef14(({ autoScroll, onScroll, children, ...rest }, forwardedRef) => {
1096
1418
  const autoScrollRef = useThreadViewportAutoScroll({
1097
1419
  autoScroll
1098
1420
  });
1099
1421
  const ref = useComposedRefs3(forwardedRef, autoScrollRef);
1100
- return /* @__PURE__ */ jsx19(Primitive11.div, { ...rest, ref, children });
1422
+ return /* @__PURE__ */ jsx22(Primitive11.div, { ...rest, ref, children });
1101
1423
  });
1102
1424
  ThreadViewport.displayName = "ThreadViewport";
1103
1425
 
1104
1426
  // src/primitives/thread/ThreadMessages.tsx
1105
- import { memo as memo2 } from "react";
1427
+ import { memo as memo3 } from "react";
1106
1428
 
1107
1429
  // src/context/providers/MessageProvider.tsx
1108
- import { useEffect as useEffect5, useState as useState3 } from "react";
1109
- import { create as create4 } from "zustand";
1110
-
1111
- // src/context/stores/EditComposer.ts
1112
- import { create as create2 } from "zustand";
1113
-
1114
- // src/context/stores/BaseComposer.ts
1115
- var makeBaseComposer = (set) => ({
1116
- value: "",
1117
- setValue: (value) => {
1118
- set({ value });
1119
- }
1120
- });
1430
+ import { useEffect as useEffect7, useState as useState5 } from "react";
1431
+ import { create as create10 } from "zustand";
1121
1432
 
1122
1433
  // src/context/stores/EditComposer.ts
1434
+ import { create as create8 } from "zustand";
1123
1435
  var makeEditComposerStore = ({
1124
1436
  onEdit,
1125
1437
  onSend
1126
- }) => create2()((set, get, store) => ({
1438
+ }) => create8()((set, get, store) => ({
1127
1439
  ...makeBaseComposer(set, get, store),
1128
1440
  isEditing: false,
1129
1441
  edit: () => {
@@ -1143,8 +1455,8 @@ var makeEditComposerStore = ({
1143
1455
  }));
1144
1456
 
1145
1457
  // src/context/stores/MessageUtils.ts
1146
- import { create as create3 } from "zustand";
1147
- var makeMessageUtilsStore = () => create3((set) => ({
1458
+ import { create as create9 } from "zustand";
1459
+ var makeMessageUtilsStore = () => create9((set) => ({
1148
1460
  inProgressIndicator: null,
1149
1461
  setInProgressIndicator: (value) => {
1150
1462
  set({ inProgressIndicator: value });
@@ -1160,7 +1472,7 @@ var makeMessageUtilsStore = () => create3((set) => ({
1160
1472
  }));
1161
1473
 
1162
1474
  // src/context/providers/MessageProvider.tsx
1163
- import { jsx as jsx20 } from "react/jsx-runtime";
1475
+ import { jsx as jsx23 } from "react/jsx-runtime";
1164
1476
  var getIsLast = (thread, message) => {
1165
1477
  return thread.messages[thread.messages.length - 1]?.id === message.id;
1166
1478
  };
@@ -1182,8 +1494,8 @@ var syncMessage = (thread, getBranches, useMessage, messageIndex) => {
1182
1494
  };
1183
1495
  var useMessageContext2 = (messageIndex) => {
1184
1496
  const { useThread, useThreadActions } = useThreadContext();
1185
- const [context] = useState3(() => {
1186
- const useMessage = create4(() => ({}));
1497
+ const [context] = useState5(() => {
1498
+ const useMessage = create10(() => ({}));
1187
1499
  const useMessageUtils = makeMessageUtilsStore();
1188
1500
  const useEditComposer = makeEditComposerStore({
1189
1501
  onEdit: () => {
@@ -1219,7 +1531,7 @@ var useMessageContext2 = (messageIndex) => {
1219
1531
  );
1220
1532
  return { useMessage, useMessageUtils, useEditComposer };
1221
1533
  });
1222
- useEffect5(() => {
1534
+ useEffect7(() => {
1223
1535
  return useThread.subscribe((thread) => {
1224
1536
  syncMessage(
1225
1537
  thread,
@@ -1236,11 +1548,11 @@ var MessageProvider = ({
1236
1548
  children
1237
1549
  }) => {
1238
1550
  const context = useMessageContext2(messageIndex);
1239
- return /* @__PURE__ */ jsx20(MessageContext.Provider, { value: context, children });
1551
+ return /* @__PURE__ */ jsx23(MessageContext.Provider, { value: context, children });
1240
1552
  };
1241
1553
 
1242
1554
  // src/primitives/thread/ThreadMessages.tsx
1243
- import { jsx as jsx21, jsxs as jsxs2 } from "react/jsx-runtime";
1555
+ import { jsx as jsx24, jsxs as jsxs3 } from "react/jsx-runtime";
1244
1556
  var getComponents = (components) => {
1245
1557
  return {
1246
1558
  EditComposer: components.EditComposer ?? components.UserMessage ?? components.Message,
@@ -1253,15 +1565,15 @@ var ThreadMessageImpl = ({
1253
1565
  components
1254
1566
  }) => {
1255
1567
  const { UserMessage, EditComposer, AssistantMessage } = getComponents(components);
1256
- return /* @__PURE__ */ jsxs2(MessageProvider, { messageIndex, children: [
1257
- /* @__PURE__ */ jsxs2(MessageIf, { user: true, children: [
1258
- /* @__PURE__ */ jsx21(ComposerIf, { editing: false, children: /* @__PURE__ */ jsx21(UserMessage, {}) }),
1259
- /* @__PURE__ */ jsx21(ComposerIf, { editing: true, children: /* @__PURE__ */ jsx21(EditComposer, {}) })
1568
+ return /* @__PURE__ */ jsxs3(MessageProvider, { messageIndex, children: [
1569
+ /* @__PURE__ */ jsxs3(MessageIf, { user: true, children: [
1570
+ /* @__PURE__ */ jsx24(ComposerIf, { editing: false, children: /* @__PURE__ */ jsx24(UserMessage, {}) }),
1571
+ /* @__PURE__ */ jsx24(ComposerIf, { editing: true, children: /* @__PURE__ */ jsx24(EditComposer, {}) })
1260
1572
  ] }),
1261
- /* @__PURE__ */ jsx21(MessageIf, { assistant: true, children: /* @__PURE__ */ jsx21(AssistantMessage, {}) })
1573
+ /* @__PURE__ */ jsx24(MessageIf, { assistant: true, children: /* @__PURE__ */ jsx24(AssistantMessage, {}) })
1262
1574
  ] });
1263
1575
  };
1264
- var ThreadMessage = memo2(
1576
+ var ThreadMessage = memo3(
1265
1577
  ThreadMessageImpl,
1266
1578
  (prev, next) => prev.messageIndex === next.messageIndex && prev.components.UserMessage === next.components.UserMessage && prev.components.EditComposer === next.components.EditComposer && prev.components.AssistantMessage === next.components.AssistantMessage
1267
1579
  );
@@ -1271,7 +1583,7 @@ var ThreadMessages = ({ components }) => {
1271
1583
  if (messagesLength === 0) return null;
1272
1584
  return new Array(messagesLength).fill(null).map((_, idx) => {
1273
1585
  const messageIndex = idx;
1274
- return /* @__PURE__ */ jsx21(
1586
+ return /* @__PURE__ */ jsx24(
1275
1587
  ThreadMessage,
1276
1588
  {
1277
1589
  messageIndex,
@@ -1295,35 +1607,7 @@ var ThreadSuggestion = createActionButton(
1295
1607
  );
1296
1608
 
1297
1609
  // src/runtime/local/useLocalRuntime.tsx
1298
- import { useInsertionEffect, useState as useState4 } from "react";
1299
-
1300
- // src/utils/ModelConfigTypes.ts
1301
- var mergeModelConfigs = (configSet) => {
1302
- const configs = Array.from(configSet).map((c) => c()).sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
1303
- return configs.reduce((acc, config) => {
1304
- if (config.system) {
1305
- if (acc.system) {
1306
- acc.system += `
1307
-
1308
- ${config.system}`;
1309
- } else {
1310
- acc.system = config.system;
1311
- }
1312
- }
1313
- if (config.tools) {
1314
- for (const [name, tool] of Object.entries(config.tools)) {
1315
- if (acc.tools?.[name]) {
1316
- throw new Error(
1317
- `You tried to define a tool with the name ${name}, but it already exists.`
1318
- );
1319
- }
1320
- if (!acc.tools) acc.tools = {};
1321
- acc.tools[name] = tool;
1322
- }
1323
- }
1324
- return acc;
1325
- }, {});
1326
- };
1610
+ import { useInsertionEffect as useInsertionEffect3, useState as useState6 } from "react";
1327
1611
 
1328
1612
  // src/runtime/utils/idUtils.tsx
1329
1613
  import { customAlphabet } from "nanoid/non-secure";
@@ -1581,234 +1865,82 @@ var LocalRuntime = class {
1581
1865
 
1582
1866
  // src/runtime/local/useLocalRuntime.tsx
1583
1867
  var useLocalRuntime = (adapter) => {
1584
- const [runtime] = useState4(() => new LocalRuntime(adapter));
1585
- useInsertionEffect(() => {
1868
+ const [runtime] = useState6(() => new LocalRuntime(adapter));
1869
+ useInsertionEffect3(() => {
1586
1870
  runtime.adapter = adapter;
1587
1871
  });
1588
1872
  return runtime;
1589
1873
  };
1590
1874
 
1591
- // src/context/providers/AssistantRuntimeProvider.tsx
1592
- import { memo as memo3 } from "react";
1593
-
1594
- // src/context/providers/AssistantProvider.tsx
1595
- import { useEffect as useEffect7, useInsertionEffect as useInsertionEffect3, useRef as useRef5, useState as useState6 } from "react";
1596
-
1597
- // src/context/stores/AssistantModelConfig.ts
1598
- import { create as create5 } from "zustand";
1599
-
1600
- // src/utils/ProxyConfigProvider.ts
1601
- var ProxyConfigProvider = class {
1602
- _providers = /* @__PURE__ */ new Set();
1603
- getModelConfig() {
1604
- return mergeModelConfigs(this._providers);
1605
- }
1606
- registerModelConfigProvider(provider) {
1607
- this._providers.add(provider);
1608
- return () => {
1609
- this._providers.delete(provider);
1610
- };
1611
- }
1612
- };
1613
-
1614
- // src/context/stores/AssistantModelConfig.ts
1615
- var makeAssistantModelConfigStore = () => create5(() => {
1616
- const proxy = new ProxyConfigProvider();
1617
- return Object.freeze({
1618
- getModelConfig: () => {
1619
- return proxy.getModelConfig();
1620
- },
1621
- registerModelConfigProvider: (provider) => {
1622
- return proxy.registerModelConfigProvider(provider);
1623
- }
1624
- });
1625
- });
1626
-
1627
- // src/context/stores/AssistantToolUIs.ts
1628
- import { create as create6 } from "zustand";
1629
- var makeAssistantToolUIsStore = () => create6((set) => {
1630
- const renderers = /* @__PURE__ */ new Map();
1631
- return Object.freeze({
1632
- getToolUI: (name) => {
1633
- const arr = renderers.get(name);
1634
- const last = arr?.at(-1);
1635
- if (last) return last;
1636
- return null;
1637
- },
1638
- setToolUI: (name, render) => {
1639
- let arr = renderers.get(name);
1640
- if (!arr) {
1641
- arr = [];
1642
- renderers.set(name, arr);
1643
- }
1644
- arr.push(render);
1645
- set({});
1646
- return () => {
1647
- const index = arr.indexOf(render);
1648
- if (index !== -1) {
1649
- arr.splice(index, 1);
1650
- }
1651
- if (index === arr.length) {
1652
- set({});
1653
- }
1654
- };
1655
- }
1656
- });
1657
- });
1658
-
1659
- // src/context/providers/ThreadProvider.tsx
1660
- import { useEffect as useEffect6, useInsertionEffect as useInsertionEffect2, useRef as useRef4, useState as useState5 } from "react";
1661
-
1662
- // src/context/stores/Composer.ts
1663
- import { create as create7 } from "zustand";
1664
- var makeComposerStore = (useThread, useThreadActions) => {
1665
- const focusListeners = /* @__PURE__ */ new Set();
1666
- return create7()((set, get, store) => {
1667
- return {
1668
- ...makeBaseComposer(set, get, store),
1669
- isEditing: true,
1670
- send: () => {
1671
- const { setValue, value } = get();
1672
- setValue("");
1673
- useThreadActions.getState().append({
1674
- parentId: useThread.getState().messages.at(-1)?.id ?? null,
1675
- role: "user",
1676
- content: [{ type: "text", text: value }]
1677
- });
1678
- },
1679
- cancel: () => {
1680
- const thread = useThread.getState();
1681
- if (!thread.isRunning) return false;
1682
- useThreadActions.getState().cancelRun();
1683
- return true;
1684
- },
1685
- focus: () => {
1686
- for (const listener of focusListeners) {
1687
- listener();
1688
- }
1689
- },
1690
- onFocus: (listener) => {
1691
- focusListeners.add(listener);
1692
- return () => {
1693
- focusListeners.delete(listener);
1694
- };
1875
+ // src/model-config/useAssistantTool.tsx
1876
+ import { useEffect as useEffect8 } from "react";
1877
+ var useAssistantTool = (tool) => {
1878
+ const { useModelConfig, useToolUIs } = useAssistantContext();
1879
+ const registerModelConfigProvider = useModelConfig(
1880
+ (s) => s.registerModelConfigProvider
1881
+ );
1882
+ const setToolUI = useToolUIs((s) => s.setToolUI);
1883
+ useEffect8(() => {
1884
+ const { toolName, render, ...rest } = tool;
1885
+ const config = {
1886
+ tools: {
1887
+ [tool.toolName]: rest
1695
1888
  }
1696
1889
  };
1697
- });
1698
- };
1699
-
1700
- // src/context/stores/Thread.ts
1701
- import { create as create8 } from "zustand";
1702
- var makeThreadStore = (runtimeRef) => {
1703
- return create8(() => ({
1704
- messages: runtimeRef.current.messages,
1705
- isRunning: runtimeRef.current.isRunning
1706
- }));
1890
+ const unsub1 = registerModelConfigProvider(() => config);
1891
+ const unsub2 = render ? setToolUI(toolName, render) : void 0;
1892
+ return () => {
1893
+ unsub1();
1894
+ unsub2?.();
1895
+ };
1896
+ }, [registerModelConfigProvider, setToolUI, tool]);
1707
1897
  };
1708
1898
 
1709
- // src/context/stores/ThreadViewport.tsx
1710
- import { create as create9 } from "zustand";
1711
- var makeThreadViewportStore = () => {
1712
- const scrollToBottomListeners = /* @__PURE__ */ new Set();
1713
- return create9(() => ({
1714
- isAtBottom: true,
1715
- scrollToBottom: () => {
1716
- for (const listener of scrollToBottomListeners) {
1717
- listener();
1718
- }
1719
- },
1720
- onScrollToBottom: (callback) => {
1721
- scrollToBottomListeners.add(callback);
1722
- return () => {
1723
- scrollToBottomListeners.delete(callback);
1724
- };
1725
- }
1726
- }));
1899
+ // src/model-config/makeAssistantTool.tsx
1900
+ var makeAssistantTool = (tool) => {
1901
+ const Tool = () => {
1902
+ useAssistantTool(tool);
1903
+ return null;
1904
+ };
1905
+ return Tool;
1906
+ };
1907
+
1908
+ // src/model-config/useAssistantToolUI.tsx
1909
+ import { useEffect as useEffect9 } from "react";
1910
+ var useAssistantToolUI = (tool) => {
1911
+ const { useToolUIs } = useAssistantContext();
1912
+ const setToolUI = useToolUIs((s) => s.setToolUI);
1913
+ useEffect9(() => {
1914
+ if (!tool) return;
1915
+ const { toolName, render } = tool;
1916
+ return setToolUI(toolName, render);
1917
+ }, [setToolUI, tool]);
1918
+ };
1919
+
1920
+ // src/model-config/makeAssistantToolUI.tsx
1921
+ var makeAssistantToolUI = (tool) => {
1922
+ const ToolUI = () => {
1923
+ useAssistantToolUI(tool);
1924
+ return null;
1925
+ };
1926
+ return ToolUI;
1727
1927
  };
1728
1928
 
1729
- // src/context/stores/ThreadActions.ts
1730
- import { create as create10 } from "zustand";
1731
- var makeThreadActionStore = (runtimeRef) => {
1732
- return create10(
1733
- () => Object.freeze({
1734
- getBranches: (messageId) => runtimeRef.current.getBranches(messageId),
1735
- switchToBranch: (branchId) => runtimeRef.current.switchToBranch(branchId),
1736
- startRun: (parentId) => runtimeRef.current.startRun(parentId),
1737
- append: (message) => runtimeRef.current.append(message),
1738
- cancelRun: () => runtimeRef.current.cancelRun(),
1739
- addToolResult: (toolCallId, result) => runtimeRef.current.addToolResult(toolCallId, result)
1740
- })
1929
+ // src/model-config/useAssistantInstructions.tsx
1930
+ import { useEffect as useEffect10 } from "react";
1931
+ var useAssistantInstructions = (instruction) => {
1932
+ const { useModelConfig } = useAssistantContext();
1933
+ const registerModelConfigProvider = useModelConfig(
1934
+ (s) => s.registerModelConfigProvider
1741
1935
  );
1742
- };
1743
-
1744
- // src/context/providers/ThreadProvider.tsx
1745
- import { jsx as jsx22, jsxs as jsxs3 } from "react/jsx-runtime";
1746
- var ThreadProvider = ({
1747
- children,
1748
- runtime
1749
- }) => {
1750
- const runtimeRef = useRef4(runtime);
1751
- useInsertionEffect2(() => {
1752
- runtimeRef.current = runtime;
1753
- });
1754
- const [context] = useState5(() => {
1755
- const useThread = makeThreadStore(runtimeRef);
1756
- const useThreadActions = makeThreadActionStore(runtimeRef);
1757
- const useViewport = makeThreadViewportStore();
1758
- const useComposer = makeComposerStore(useThread, useThreadActions);
1759
- return {
1760
- useThread,
1761
- useThreadActions,
1762
- useComposer,
1763
- useViewport
1764
- };
1765
- });
1766
- useEffect6(() => {
1767
- const onRuntimeUpdate = () => {
1768
- context.useThread.setState(
1769
- Object.freeze({
1770
- messages: runtimeRef.current.messages,
1771
- isRunning: runtimeRef.current.isRunning
1772
- }),
1773
- true
1774
- );
1936
+ useEffect10(() => {
1937
+ const config = {
1938
+ system: instruction
1775
1939
  };
1776
- onRuntimeUpdate();
1777
- return runtime.subscribe(onRuntimeUpdate);
1778
- }, [context, runtime]);
1779
- const RuntimeSynchronizer = runtime.unstable_synchronizer;
1780
- return /* @__PURE__ */ jsxs3(ThreadContext.Provider, { value: context, children: [
1781
- RuntimeSynchronizer && /* @__PURE__ */ jsx22(RuntimeSynchronizer, {}),
1782
- children
1783
- ] });
1784
- };
1785
-
1786
- // src/context/providers/AssistantProvider.tsx
1787
- import { jsx as jsx23 } from "react/jsx-runtime";
1788
- var AssistantProvider = ({ children, runtime }) => {
1789
- const runtimeRef = useRef5(runtime);
1790
- useInsertionEffect3(() => {
1791
- runtimeRef.current = runtime;
1792
- });
1793
- const [context] = useState6(() => {
1794
- const useModelConfig = makeAssistantModelConfigStore();
1795
- const useToolUIs = makeAssistantToolUIsStore();
1796
- return { useModelConfig, useToolUIs };
1797
- });
1798
- const getModelCOnfig = context.useModelConfig((c) => c.getModelConfig);
1799
- useEffect7(() => {
1800
- return runtime.registerModelConfigProvider(getModelCOnfig);
1801
- }, [runtime, getModelCOnfig]);
1802
- return /* @__PURE__ */ jsx23(AssistantContext.Provider, { value: context, children: /* @__PURE__ */ jsx23(ThreadProvider, { runtime, children }) });
1940
+ return registerModelConfigProvider(() => config);
1941
+ }, [registerModelConfigProvider, instruction]);
1803
1942
  };
1804
1943
 
1805
- // src/context/providers/AssistantRuntimeProvider.tsx
1806
- import { jsx as jsx24 } from "react/jsx-runtime";
1807
- var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
1808
- return /* @__PURE__ */ jsx24(AssistantProvider, { runtime, children });
1809
- };
1810
- var AssistantRuntimeProvider = memo3(AssistantRuntimeProviderImpl);
1811
-
1812
1944
  // src/internal.ts
1813
1945
  var internal_exports = {};
1814
1946
  __export(internal_exports, {
@@ -1825,22 +1957,32 @@ export {
1825
1957
  internal_exports as INTERNAL,
1826
1958
  message_exports as MessagePrimitive,
1827
1959
  thread_exports as ThreadPrimitive,
1960
+ makeAssistantTool,
1961
+ makeAssistantToolUI,
1828
1962
  useActionBarCopy,
1829
1963
  useActionBarEdit,
1830
1964
  useActionBarReload,
1965
+ useAssistantContext,
1966
+ useAssistantInstructions,
1967
+ useAssistantTool,
1968
+ useAssistantToolUI,
1831
1969
  useBranchPickerCount,
1832
1970
  useBranchPickerNext,
1833
1971
  useBranchPickerNumber,
1834
1972
  useBranchPickerPrevious,
1835
1973
  useComposerCancel,
1974
+ useComposerContext,
1836
1975
  useComposerIf,
1837
1976
  useComposerSend,
1977
+ useContentPartContext,
1838
1978
  useContentPartDisplay,
1839
1979
  useContentPartImage,
1840
1980
  useContentPartInProgressIndicator,
1841
1981
  useContentPartText,
1842
1982
  useLocalRuntime,
1983
+ useMessageContext,
1843
1984
  useMessageIf,
1985
+ useThreadContext,
1844
1986
  useThreadEmpty,
1845
1987
  useThreadIf,
1846
1988
  useThreadScrollToBottom,