@assistant-ui/react 0.5.14 → 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
  }
@@ -3605,7 +3626,6 @@ var AUTO_STATUS_COMPLETE = Object.freeze({
3605
3626
  type: "complete",
3606
3627
  reason: "unknown"
3607
3628
  });
3608
- var isAutoStatus = (status) => status === AUTO_STATUS_RUNNING || status === AUTO_STATUS_COMPLETE;
3609
3629
  var getAutoStatus = (isLast, isRunning) => isLast && isRunning ? AUTO_STATUS_RUNNING : AUTO_STATUS_COMPLETE;
3610
3630
 
3611
3631
  // src/runtimes/external-store/ThreadMessageLike.tsx
@@ -3689,9 +3709,8 @@ var useExternalStoreSync = (adapter, updateData) => {
3689
3709
  adapterRef.current.messages.at(-1) === m,
3690
3710
  adapterRef.current.isRunning ?? false
3691
3711
  );
3692
- if (cache && (cache.role !== "assistant" || !isAutoStatus(cache.status) || cache.status.type === autoStatus.type))
3712
+ if (cache && (cache.role !== "assistant" || cache.status === autoStatus))
3693
3713
  return cache;
3694
- if (cache) return cache;
3695
3714
  const newMessage = fromThreadMessageLike(
3696
3715
  converter2(m, idx),
3697
3716
  idx.toString(),
@@ -3704,6 +3723,7 @@ var useExternalStoreSync = (adapter, updateData) => {
3704
3723
  }, [adapter.convertMessage]);
3705
3724
  useEffect11(() => {
3706
3725
  updateData(
3726
+ adapter.isDisabled ?? false,
3707
3727
  adapter.isRunning ?? false,
3708
3728
  converter.convertMessages(adapter.messages, convertCallback)
3709
3729
  );
@@ -3711,8 +3731,9 @@ var useExternalStoreSync = (adapter, updateData) => {
3711
3731
  updateData,
3712
3732
  converter,
3713
3733
  convertCallback,
3714
- adapter.messages,
3715
- adapter.isRunning
3734
+ adapter.isDisabled,
3735
+ adapter.isRunning,
3736
+ adapter.messages
3716
3737
  ]);
3717
3738
  };
3718
3739
 
@@ -3723,6 +3744,9 @@ var hasUpcomingMessage = (isRunning, messages) => {
3723
3744
  var ExternalStoreThreadRuntime = class {
3724
3745
  constructor(store) {
3725
3746
  this.store = store;
3747
+ this.isDisabled = store.isDisabled ?? false;
3748
+ this.isRunning = store.isRunning ?? false;
3749
+ this.messages = store.messages;
3726
3750
  this.useStore = create14(() => ({
3727
3751
  store
3728
3752
  }));
@@ -3740,8 +3764,9 @@ var ExternalStoreThreadRuntime = class {
3740
3764
  copy: this.store.onCopy !== null
3741
3765
  };
3742
3766
  }
3743
- messages = [];
3744
- isRunning = false;
3767
+ messages;
3768
+ isDisabled;
3769
+ isRunning;
3745
3770
  getBranches(messageId) {
3746
3771
  return this.repository.getBranches(messageId);
3747
3772
  }
@@ -3792,7 +3817,7 @@ var ExternalStoreThreadRuntime = class {
3792
3817
  this.useStore.setState({ store: this.store });
3793
3818
  }
3794
3819
  }
3795
- updateData = (isRunning, vm) => {
3820
+ updateData = (isDisabled, isRunning, vm) => {
3796
3821
  for (let i = 0; i < vm.length; i++) {
3797
3822
  const message = vm[i];
3798
3823
  const parent = vm[i - 1];
@@ -3815,6 +3840,7 @@ var ExternalStoreThreadRuntime = class {
3815
3840
  this.assistantOptimisticId ?? vm.at(-1)?.id ?? null
3816
3841
  );
3817
3842
  this.messages = this.repository.getMessages();
3843
+ this.isDisabled = isDisabled;
3818
3844
  this.isRunning = isRunning;
3819
3845
  for (const callback of this._subscriptions) callback();
3820
3846
  };