@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.d.mts +8 -3
- package/dist/index.d.ts +8 -3
- package/dist/index.js +44 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -245,6 +245,7 @@ declare class LocalThreadRuntime implements ThreadRuntime {
|
|
245
245
|
cancel: true;
|
246
246
|
copy: true;
|
247
247
|
}>;
|
248
|
+
readonly isDisabled = false;
|
248
249
|
get messages(): ThreadMessage[];
|
249
250
|
get isRunning(): boolean;
|
250
251
|
constructor(configProvider: ModelConfigProvider, adapter: ChatModelAdapter, options?: LocalRuntimeOptions | undefined);
|
@@ -564,8 +565,9 @@ type ExternalStoreMessageConverterAdapter<T> = {
|
|
564
565
|
convertMessage: ExternalStoreMessageConverter<T>;
|
565
566
|
};
|
566
567
|
type ExternalStoreAdapterBase<T> = {
|
567
|
-
threadId?: string;
|
568
|
-
|
568
|
+
threadId?: string | undefined;
|
569
|
+
isDisabled?: boolean | undefined;
|
570
|
+
isRunning?: boolean | undefined;
|
569
571
|
messages: T[];
|
570
572
|
setMessages?: ((messages: T[]) => void) | undefined;
|
571
573
|
onNew: (message: AppendMessage) => Promise<void>;
|
@@ -656,7 +658,8 @@ declare class ExternalStoreThreadRuntime implements ReactThreadRuntime {
|
|
656
658
|
cancel: boolean;
|
657
659
|
copy: boolean;
|
658
660
|
};
|
659
|
-
messages:
|
661
|
+
messages: any[];
|
662
|
+
isDisabled: boolean;
|
660
663
|
isRunning: boolean;
|
661
664
|
constructor(store: ExternalStoreAdapter<any>);
|
662
665
|
getBranches(messageId: string): string[];
|
@@ -760,6 +763,7 @@ declare function useAssistantContext(options: {
|
|
760
763
|
|
761
764
|
type ThreadState = Readonly<{
|
762
765
|
isRunning: boolean;
|
766
|
+
isDisabled: boolean;
|
763
767
|
}>;
|
764
768
|
|
765
769
|
type ThreadViewportState = Readonly<{
|
@@ -932,6 +936,7 @@ declare const useThreadEmpty: () => boolean;
|
|
932
936
|
type ThreadIfFilters = {
|
933
937
|
empty: boolean | undefined;
|
934
938
|
running: boolean | undefined;
|
939
|
+
disabled: boolean | undefined;
|
935
940
|
};
|
936
941
|
type UseThreadIfProps = RequireAtLeastOne<ThreadIfFilters>;
|
937
942
|
declare const useThreadIf: (props: UseThreadIfProps) => boolean;
|
package/dist/index.d.ts
CHANGED
@@ -245,6 +245,7 @@ declare class LocalThreadRuntime implements ThreadRuntime {
|
|
245
245
|
cancel: true;
|
246
246
|
copy: true;
|
247
247
|
}>;
|
248
|
+
readonly isDisabled = false;
|
248
249
|
get messages(): ThreadMessage[];
|
249
250
|
get isRunning(): boolean;
|
250
251
|
constructor(configProvider: ModelConfigProvider, adapter: ChatModelAdapter, options?: LocalRuntimeOptions | undefined);
|
@@ -564,8 +565,9 @@ type ExternalStoreMessageConverterAdapter<T> = {
|
|
564
565
|
convertMessage: ExternalStoreMessageConverter<T>;
|
565
566
|
};
|
566
567
|
type ExternalStoreAdapterBase<T> = {
|
567
|
-
threadId?: string;
|
568
|
-
|
568
|
+
threadId?: string | undefined;
|
569
|
+
isDisabled?: boolean | undefined;
|
570
|
+
isRunning?: boolean | undefined;
|
569
571
|
messages: T[];
|
570
572
|
setMessages?: ((messages: T[]) => void) | undefined;
|
571
573
|
onNew: (message: AppendMessage) => Promise<void>;
|
@@ -656,7 +658,8 @@ declare class ExternalStoreThreadRuntime implements ReactThreadRuntime {
|
|
656
658
|
cancel: boolean;
|
657
659
|
copy: boolean;
|
658
660
|
};
|
659
|
-
messages:
|
661
|
+
messages: any[];
|
662
|
+
isDisabled: boolean;
|
660
663
|
isRunning: boolean;
|
661
664
|
constructor(store: ExternalStoreAdapter<any>);
|
662
665
|
getBranches(messageId: string): string[];
|
@@ -760,6 +763,7 @@ declare function useAssistantContext(options: {
|
|
760
763
|
|
761
764
|
type ThreadState = Readonly<{
|
762
765
|
isRunning: boolean;
|
766
|
+
isDisabled: boolean;
|
763
767
|
}>;
|
764
768
|
|
765
769
|
type ThreadViewportState = Readonly<{
|
@@ -932,6 +936,7 @@ declare const useThreadEmpty: () => boolean;
|
|
932
936
|
type ThreadIfFilters = {
|
933
937
|
empty: boolean | undefined;
|
934
938
|
running: boolean | undefined;
|
939
|
+
disabled: boolean | undefined;
|
935
940
|
};
|
936
941
|
type UseThreadIfProps = RequireAtLeastOne<ThreadIfFilters>;
|
937
942
|
declare const useThreadIf: (props: UseThreadIfProps) => boolean;
|
package/dist/index.js
CHANGED
@@ -304,6 +304,7 @@ var makeComposerStore = (useThreadMessages, useThreadActions) => {
|
|
304
304
|
var import_zustand4 = require("zustand");
|
305
305
|
var makeThreadStore = (runtimeRef) => {
|
306
306
|
return (0, import_zustand4.create)(() => ({
|
307
|
+
isDisabled: runtimeRef.getState().isDisabled,
|
307
308
|
isRunning: runtimeRef.getState().isRunning
|
308
309
|
}));
|
309
310
|
};
|
@@ -402,10 +403,12 @@ var ThreadProvider = ({
|
|
402
403
|
(0, import_react4.useCallback)(
|
403
404
|
(thread) => {
|
404
405
|
const onThreadUpdate = () => {
|
405
|
-
|
406
|
+
const threadState = context.useThread.getState();
|
407
|
+
if (thread.isRunning !== threadState.isRunning || thread.isDisabled !== threadState.isDisabled) {
|
406
408
|
context.useThread.setState(
|
407
409
|
Object.freeze({
|
408
|
-
isRunning: thread.isRunning
|
410
|
+
isRunning: thread.isRunning,
|
411
|
+
isDisabled: thread.isDisabled
|
409
412
|
}),
|
410
413
|
true
|
411
414
|
);
|
@@ -716,7 +719,7 @@ var useActionBarReload = () => {
|
|
716
719
|
const { useMessage } = useMessageContext();
|
717
720
|
const disabled = useCombinedStore(
|
718
721
|
[useThread, useMessage],
|
719
|
-
(t, m) => t.isRunning || m.message.role !== "assistant"
|
722
|
+
(t, m) => t.isRunning || t.isDisabled || m.message.role !== "assistant"
|
720
723
|
);
|
721
724
|
const callback = (0, import_react19.useCallback)(() => {
|
722
725
|
const { parentId } = useMessage.getState();
|
@@ -802,9 +805,16 @@ var useComposerIf = (props) => {
|
|
802
805
|
// src/primitive-hooks/composer/useComposerSend.tsx
|
803
806
|
var import_react23 = require("react");
|
804
807
|
var useComposerSend = () => {
|
805
|
-
const {
|
808
|
+
const {
|
809
|
+
useThread,
|
810
|
+
useViewport,
|
811
|
+
useComposer: useNewComposer
|
812
|
+
} = useThreadContext();
|
806
813
|
const { useComposer } = useComposerContext();
|
807
|
-
const disabled =
|
814
|
+
const disabled = useCombinedStore(
|
815
|
+
[useThread, useComposer],
|
816
|
+
(t, c) => t.isDisabled || t.isRunning || !c.isEditing || c.value.length === 0
|
817
|
+
);
|
808
818
|
const callback = (0, import_react23.useCallback)(() => {
|
809
819
|
const composerState = useComposer.getState();
|
810
820
|
if (!composerState.isEditing) return;
|
@@ -883,6 +893,8 @@ var useThreadIf = (props) => {
|
|
883
893
|
if (props.empty === false && messages.length === 0) return false;
|
884
894
|
if (props.running === true && !thread.isRunning) return false;
|
885
895
|
if (props.running === false && thread.isRunning) return false;
|
896
|
+
if (props.disabled === true && thread.isDisabled) return false;
|
897
|
+
if (props.disabled === false && thread.isDisabled) return false;
|
886
898
|
return true;
|
887
899
|
}
|
888
900
|
);
|
@@ -914,7 +926,7 @@ var useThreadSuggestion = ({
|
|
914
926
|
}) => {
|
915
927
|
const { useThread, useComposer } = useThreadContext();
|
916
928
|
const append = useAppendMessage();
|
917
|
-
const disabled = useThread((t) => t.
|
929
|
+
const disabled = useThread((t) => t.isDisabled);
|
918
930
|
const callback = (0, import_react25.useCallback)(() => {
|
919
931
|
const thread = useThread.getState();
|
920
932
|
const composer = useComposer.getState();
|
@@ -1597,7 +1609,14 @@ var import_react_textarea_autosize = __toESM(require("react-textarea-autosize"))
|
|
1597
1609
|
var import_react_use_escape_keydown = require("@radix-ui/react-use-escape-keydown");
|
1598
1610
|
var import_jsx_runtime19 = require("react/jsx-runtime");
|
1599
1611
|
var ComposerPrimitiveInput = (0, import_react41.forwardRef)(
|
1600
|
-
({
|
1612
|
+
({
|
1613
|
+
autoFocus = false,
|
1614
|
+
asChild,
|
1615
|
+
disabled: disabledProp,
|
1616
|
+
onChange,
|
1617
|
+
onKeyDown,
|
1618
|
+
...rest
|
1619
|
+
}, forwardedRef) => {
|
1601
1620
|
const { useThread } = useThreadContext();
|
1602
1621
|
const { useComposer, type } = useComposerContext();
|
1603
1622
|
const value = useComposer((c) => {
|
@@ -1605,6 +1624,7 @@ var ComposerPrimitiveInput = (0, import_react41.forwardRef)(
|
|
1605
1624
|
return c.value;
|
1606
1625
|
});
|
1607
1626
|
const Component = asChild ? import_react_slot.Slot : import_react_textarea_autosize.default;
|
1627
|
+
const isDisabled = useThread((t) => t.isDisabled) ?? disabledProp ?? false;
|
1608
1628
|
const textareaRef = (0, import_react41.useRef)(null);
|
1609
1629
|
const ref = (0, import_react_compose_refs2.useComposedRefs)(forwardedRef, textareaRef);
|
1610
1630
|
(0, import_react_use_escape_keydown.useEscapeKeydown)((e) => {
|
@@ -1615,7 +1635,7 @@ var ComposerPrimitiveInput = (0, import_react41.forwardRef)(
|
|
1615
1635
|
}
|
1616
1636
|
});
|
1617
1637
|
const handleKeyPress = (e) => {
|
1618
|
-
if (
|
1638
|
+
if (isDisabled) return;
|
1619
1639
|
if (e.key === "Enter" && e.shiftKey === false) {
|
1620
1640
|
const isRunning = useThread.getState().isRunning;
|
1621
1641
|
if (!isRunning) {
|
@@ -1624,7 +1644,7 @@ var ComposerPrimitiveInput = (0, import_react41.forwardRef)(
|
|
1624
1644
|
}
|
1625
1645
|
}
|
1626
1646
|
};
|
1627
|
-
const autoFocusEnabled = autoFocus && !
|
1647
|
+
const autoFocusEnabled = autoFocus && !isDisabled;
|
1628
1648
|
const focus = (0, import_react41.useCallback)(() => {
|
1629
1649
|
const textarea = textareaRef.current;
|
1630
1650
|
if (!textarea || !autoFocusEnabled) return;
|
@@ -1647,7 +1667,7 @@ var ComposerPrimitiveInput = (0, import_react41.forwardRef)(
|
|
1647
1667
|
value,
|
1648
1668
|
...rest,
|
1649
1669
|
ref,
|
1650
|
-
disabled,
|
1670
|
+
disabled: isDisabled,
|
1651
1671
|
onChange: (0, import_primitive5.composeEventHandlers)(onChange, (e) => {
|
1652
1672
|
const composerState = useComposer.getState();
|
1653
1673
|
if (!composerState.isEditing) return;
|
@@ -3475,6 +3495,7 @@ var LocalThreadRuntime = class {
|
|
3475
3495
|
abortController = null;
|
3476
3496
|
repository = new MessageRepository();
|
3477
3497
|
capabilities = CAPABILITIES;
|
3498
|
+
isDisabled = false;
|
3478
3499
|
get messages() {
|
3479
3500
|
return this.repository.getMessages();
|
3480
3501
|
}
|
@@ -3688,7 +3709,6 @@ var AUTO_STATUS_COMPLETE = Object.freeze({
|
|
3688
3709
|
type: "complete",
|
3689
3710
|
reason: "unknown"
|
3690
3711
|
});
|
3691
|
-
var isAutoStatus = (status) => status === AUTO_STATUS_RUNNING || status === AUTO_STATUS_COMPLETE;
|
3692
3712
|
var getAutoStatus = (isLast, isRunning) => isLast && isRunning ? AUTO_STATUS_RUNNING : AUTO_STATUS_COMPLETE;
|
3693
3713
|
|
3694
3714
|
// src/runtimes/external-store/ThreadMessageLike.tsx
|
@@ -3772,9 +3792,8 @@ var useExternalStoreSync = (adapter, updateData) => {
|
|
3772
3792
|
adapterRef.current.messages.at(-1) === m,
|
3773
3793
|
adapterRef.current.isRunning ?? false
|
3774
3794
|
);
|
3775
|
-
if (cache && (cache.role !== "assistant" ||
|
3795
|
+
if (cache && (cache.role !== "assistant" || cache.status === autoStatus))
|
3776
3796
|
return cache;
|
3777
|
-
if (cache) return cache;
|
3778
3797
|
const newMessage = fromThreadMessageLike(
|
3779
3798
|
converter2(m, idx),
|
3780
3799
|
idx.toString(),
|
@@ -3787,6 +3806,7 @@ var useExternalStoreSync = (adapter, updateData) => {
|
|
3787
3806
|
}, [adapter.convertMessage]);
|
3788
3807
|
(0, import_react55.useEffect)(() => {
|
3789
3808
|
updateData(
|
3809
|
+
adapter.isDisabled ?? false,
|
3790
3810
|
adapter.isRunning ?? false,
|
3791
3811
|
converter.convertMessages(adapter.messages, convertCallback)
|
3792
3812
|
);
|
@@ -3794,8 +3814,9 @@ var useExternalStoreSync = (adapter, updateData) => {
|
|
3794
3814
|
updateData,
|
3795
3815
|
converter,
|
3796
3816
|
convertCallback,
|
3797
|
-
adapter.
|
3798
|
-
adapter.isRunning
|
3817
|
+
adapter.isDisabled,
|
3818
|
+
adapter.isRunning,
|
3819
|
+
adapter.messages
|
3799
3820
|
]);
|
3800
3821
|
};
|
3801
3822
|
|
@@ -3806,6 +3827,9 @@ var hasUpcomingMessage = (isRunning, messages) => {
|
|
3806
3827
|
var ExternalStoreThreadRuntime = class {
|
3807
3828
|
constructor(store) {
|
3808
3829
|
this.store = store;
|
3830
|
+
this.isDisabled = store.isDisabled ?? false;
|
3831
|
+
this.isRunning = store.isRunning ?? false;
|
3832
|
+
this.messages = store.messages;
|
3809
3833
|
this.useStore = (0, import_zustand14.create)(() => ({
|
3810
3834
|
store
|
3811
3835
|
}));
|
@@ -3823,8 +3847,9 @@ var ExternalStoreThreadRuntime = class {
|
|
3823
3847
|
copy: this.store.onCopy !== null
|
3824
3848
|
};
|
3825
3849
|
}
|
3826
|
-
messages
|
3827
|
-
|
3850
|
+
messages;
|
3851
|
+
isDisabled;
|
3852
|
+
isRunning;
|
3828
3853
|
getBranches(messageId) {
|
3829
3854
|
return this.repository.getBranches(messageId);
|
3830
3855
|
}
|
@@ -3875,7 +3900,7 @@ var ExternalStoreThreadRuntime = class {
|
|
3875
3900
|
this.useStore.setState({ store: this.store });
|
3876
3901
|
}
|
3877
3902
|
}
|
3878
|
-
updateData = (isRunning, vm) => {
|
3903
|
+
updateData = (isDisabled, isRunning, vm) => {
|
3879
3904
|
for (let i = 0; i < vm.length; i++) {
|
3880
3905
|
const message = vm[i];
|
3881
3906
|
const parent = vm[i - 1];
|
@@ -3898,6 +3923,7 @@ var ExternalStoreThreadRuntime = class {
|
|
3898
3923
|
this.assistantOptimisticId ?? vm.at(-1)?.id ?? null
|
3899
3924
|
);
|
3900
3925
|
this.messages = this.repository.getMessages();
|
3926
|
+
this.isDisabled = isDisabled;
|
3901
3927
|
this.isRunning = isRunning;
|
3902
3928
|
for (const callback of this._subscriptions) callback();
|
3903
3929
|
};
|