@assistant-ui/react 0.5.15 → 0.5.16

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -209,6 +209,7 @@ var makeComposerStore = (useThreadMessages, useThreadActions) => {
209
209
  import { create as create4 } from "zustand";
210
210
  var makeThreadStore = (runtimeRef) => {
211
211
  return create4(() => ({
212
+ isDisabled: runtimeRef.getState().isDisabled,
212
213
  isRunning: runtimeRef.getState().isRunning
213
214
  }));
214
215
  };
@@ -307,10 +308,12 @@ var ThreadProvider = ({
307
308
  useCallback2(
308
309
  (thread) => {
309
310
  const onThreadUpdate = () => {
310
- if (thread.isRunning !== context.useThread.getState().isRunning) {
311
+ const threadState = context.useThread.getState();
312
+ if (thread.isRunning !== threadState.isRunning || thread.isDisabled !== threadState.isDisabled) {
311
313
  context.useThread.setState(
312
314
  Object.freeze({
313
- isRunning: thread.isRunning
315
+ isRunning: thread.isRunning,
316
+ isDisabled: thread.isDisabled
314
317
  }),
315
318
  true
316
319
  );
@@ -621,7 +624,7 @@ var useActionBarReload = () => {
621
624
  const { useMessage } = useMessageContext();
622
625
  const disabled = useCombinedStore(
623
626
  [useThread, useMessage],
624
- (t, m) => t.isRunning || m.message.role !== "assistant"
627
+ (t, m) => t.isRunning || t.isDisabled || m.message.role !== "assistant"
625
628
  );
626
629
  const callback = useCallback7(() => {
627
630
  const { parentId } = useMessage.getState();
@@ -707,9 +710,16 @@ var useComposerIf = (props) => {
707
710
  // src/primitive-hooks/composer/useComposerSend.tsx
708
711
  import { useCallback as useCallback11 } from "react";
709
712
  var useComposerSend = () => {
710
- const { useViewport, useComposer: useNewComposer } = useThreadContext();
713
+ const {
714
+ useThread,
715
+ useViewport,
716
+ useComposer: useNewComposer
717
+ } = useThreadContext();
711
718
  const { useComposer } = useComposerContext();
712
- const disabled = useComposer((c) => !c.isEditing || c.value.length === 0);
719
+ const disabled = useCombinedStore(
720
+ [useThread, useComposer],
721
+ (t, c) => t.isDisabled || t.isRunning || !c.isEditing || c.value.length === 0
722
+ );
713
723
  const callback = useCallback11(() => {
714
724
  const composerState = useComposer.getState();
715
725
  if (!composerState.isEditing) return;
@@ -788,6 +798,8 @@ var useThreadIf = (props) => {
788
798
  if (props.empty === false && messages.length === 0) return false;
789
799
  if (props.running === true && !thread.isRunning) return false;
790
800
  if (props.running === false && thread.isRunning) return false;
801
+ if (props.disabled === true && thread.isDisabled) return false;
802
+ if (props.disabled === false && thread.isDisabled) return false;
791
803
  return true;
792
804
  }
793
805
  );
@@ -819,7 +831,7 @@ var useThreadSuggestion = ({
819
831
  }) => {
820
832
  const { useThread, useComposer } = useThreadContext();
821
833
  const append = useAppendMessage();
822
- const disabled = useThread((t) => t.isRunning);
834
+ const disabled = useThread((t) => t.isDisabled);
823
835
  const callback = useCallback13(() => {
824
836
  const thread = useThread.getState();
825
837
  const composer = useComposer.getState();
@@ -1512,7 +1524,14 @@ import TextareaAutosize from "react-textarea-autosize";
1512
1524
  import { useEscapeKeydown } from "@radix-ui/react-use-escape-keydown";
1513
1525
  import { jsx as jsx19 } from "react/jsx-runtime";
1514
1526
  var ComposerPrimitiveInput = forwardRef11(
1515
- ({ autoFocus = false, asChild, disabled, onChange, onKeyDown, ...rest }, forwardedRef) => {
1527
+ ({
1528
+ autoFocus = false,
1529
+ asChild,
1530
+ disabled: disabledProp,
1531
+ onChange,
1532
+ onKeyDown,
1533
+ ...rest
1534
+ }, forwardedRef) => {
1516
1535
  const { useThread } = useThreadContext();
1517
1536
  const { useComposer, type } = useComposerContext();
1518
1537
  const value = useComposer((c) => {
@@ -1520,6 +1539,7 @@ var ComposerPrimitiveInput = forwardRef11(
1520
1539
  return c.value;
1521
1540
  });
1522
1541
  const Component = asChild ? Slot : TextareaAutosize;
1542
+ const isDisabled = useThread((t) => t.isDisabled) ?? disabledProp ?? false;
1523
1543
  const textareaRef = useRef4(null);
1524
1544
  const ref = useComposedRefs2(forwardedRef, textareaRef);
1525
1545
  useEscapeKeydown((e) => {
@@ -1530,7 +1550,7 @@ var ComposerPrimitiveInput = forwardRef11(
1530
1550
  }
1531
1551
  });
1532
1552
  const handleKeyPress = (e) => {
1533
- if (disabled) return;
1553
+ if (isDisabled) return;
1534
1554
  if (e.key === "Enter" && e.shiftKey === false) {
1535
1555
  const isRunning = useThread.getState().isRunning;
1536
1556
  if (!isRunning) {
@@ -1539,7 +1559,7 @@ var ComposerPrimitiveInput = forwardRef11(
1539
1559
  }
1540
1560
  }
1541
1561
  };
1542
- const autoFocusEnabled = autoFocus && !disabled;
1562
+ const autoFocusEnabled = autoFocus && !isDisabled;
1543
1563
  const focus = useCallback15(() => {
1544
1564
  const textarea = textareaRef.current;
1545
1565
  if (!textarea || !autoFocusEnabled) return;
@@ -1562,7 +1582,7 @@ var ComposerPrimitiveInput = forwardRef11(
1562
1582
  value,
1563
1583
  ...rest,
1564
1584
  ref,
1565
- disabled,
1585
+ disabled: isDisabled,
1566
1586
  onChange: composeEventHandlers5(onChange, (e) => {
1567
1587
  const composerState = useComposer.getState();
1568
1588
  if (!composerState.isEditing) return;
@@ -3392,6 +3412,7 @@ var LocalThreadRuntime = class {
3392
3412
  abortController = null;
3393
3413
  repository = new MessageRepository();
3394
3414
  capabilities = CAPABILITIES;
3415
+ isDisabled = false;
3395
3416
  get messages() {
3396
3417
  return this.repository.getMessages();
3397
3418
  }
@@ -3702,6 +3723,7 @@ var useExternalStoreSync = (adapter, updateData) => {
3702
3723
  }, [adapter.convertMessage]);
3703
3724
  useEffect11(() => {
3704
3725
  updateData(
3726
+ adapter.isDisabled ?? false,
3705
3727
  adapter.isRunning ?? false,
3706
3728
  converter.convertMessages(adapter.messages, convertCallback)
3707
3729
  );
@@ -3709,8 +3731,9 @@ var useExternalStoreSync = (adapter, updateData) => {
3709
3731
  updateData,
3710
3732
  converter,
3711
3733
  convertCallback,
3712
- adapter.messages,
3713
- adapter.isRunning
3734
+ adapter.isDisabled,
3735
+ adapter.isRunning,
3736
+ adapter.messages
3714
3737
  ]);
3715
3738
  };
3716
3739
 
@@ -3721,6 +3744,9 @@ var hasUpcomingMessage = (isRunning, messages) => {
3721
3744
  var ExternalStoreThreadRuntime = class {
3722
3745
  constructor(store) {
3723
3746
  this.store = store;
3747
+ this.isDisabled = store.isDisabled ?? false;
3748
+ this.isRunning = store.isRunning ?? false;
3749
+ this.messages = store.messages;
3724
3750
  this.useStore = create14(() => ({
3725
3751
  store
3726
3752
  }));
@@ -3738,8 +3764,9 @@ var ExternalStoreThreadRuntime = class {
3738
3764
  copy: this.store.onCopy !== null
3739
3765
  };
3740
3766
  }
3741
- messages = [];
3742
- isRunning = false;
3767
+ messages;
3768
+ isDisabled;
3769
+ isRunning;
3743
3770
  getBranches(messageId) {
3744
3771
  return this.repository.getBranches(messageId);
3745
3772
  }
@@ -3790,7 +3817,7 @@ var ExternalStoreThreadRuntime = class {
3790
3817
  this.useStore.setState({ store: this.store });
3791
3818
  }
3792
3819
  }
3793
- updateData = (isRunning, vm) => {
3820
+ updateData = (isDisabled, isRunning, vm) => {
3794
3821
  for (let i = 0; i < vm.length; i++) {
3795
3822
  const message = vm[i];
3796
3823
  const parent = vm[i - 1];
@@ -3813,6 +3840,7 @@ var ExternalStoreThreadRuntime = class {
3813
3840
  this.assistantOptimisticId ?? vm.at(-1)?.id ?? null
3814
3841
  );
3815
3842
  this.messages = this.repository.getMessages();
3843
+ this.isDisabled = isDisabled;
3816
3844
  this.isRunning = isRunning;
3817
3845
  for (const callback of this._subscriptions) callback();
3818
3846
  };