@assistant-ui/react 0.5.24 → 0.5.26

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -159,11 +159,23 @@ var makeComposerStore = (useThreadMessages, useThreadActions) => {
159
159
 
160
160
  // src/context/stores/Thread.ts
161
161
  import { create as create4 } from "zustand";
162
+ var getThreadStateFromRuntime = (runtime) => {
163
+ const lastMessage = runtime.messages.at(-1);
164
+ if (lastMessage?.role !== "assistant")
165
+ return Object.freeze({
166
+ isDisabled: runtime.isDisabled,
167
+ isRunning: false,
168
+ unstable_canAppendNew: !runtime.isDisabled
169
+ });
170
+ return Object.freeze({
171
+ isDisabled: runtime.isDisabled,
172
+ isRunning: lastMessage.status.type === "running",
173
+ unstable_canAppendNew: !runtime.isDisabled && lastMessage.status.type !== "running" && lastMessage.status.type !== "requires-action"
174
+ });
175
+ };
162
176
  var makeThreadStore = (runtimeRef) => {
163
- return create4(() => ({
164
- isDisabled: runtimeRef.getState().isDisabled,
165
- isRunning: runtimeRef.getState().isRunning
166
- }));
177
+ const runtime = runtimeRef.getState();
178
+ return create4(() => getThreadStateFromRuntime(runtime));
167
179
  };
168
180
 
169
181
  // src/context/stores/ThreadViewport.tsx
@@ -260,13 +272,11 @@ var ThreadProvider = ({
260
272
  useCallback2(
261
273
  (thread) => {
262
274
  const onThreadUpdate = () => {
263
- const threadState = context.useThread.getState();
264
- if (thread.isRunning !== threadState.isRunning || thread.isDisabled !== threadState.isDisabled) {
275
+ const oldState = context.useThread.getState();
276
+ const state = getThreadStateFromRuntime(thread);
277
+ if (oldState.isDisabled !== state.isDisabled || oldState.isRunning !== state.isRunning || oldState.unstable_canAppendNew !== state.unstable_canAppendNew) {
265
278
  context.useThread.setState(
266
- Object.freeze({
267
- isRunning: thread.isRunning,
268
- isDisabled: thread.isDisabled
269
- }),
279
+ getThreadStateFromRuntime(thread),
270
280
  true
271
281
  );
272
282
  }
@@ -520,8 +530,8 @@ var useCombinedStore = (stores, selector) => {
520
530
  return useCombined(selector);
521
531
  };
522
532
 
523
- // src/utils/getMessageText.tsx
524
- var getMessageText = (message) => {
533
+ // src/utils/getThreadMessageText.tsx
534
+ var getThreadMessageText = (message) => {
525
535
  const textParts = message.content.filter(
526
536
  (part) => part.type === "text"
527
537
  );
@@ -543,7 +553,7 @@ var useActionBarCopy = ({
543
553
  const { message } = useMessage.getState();
544
554
  const { setIsCopied } = useMessageUtils.getState();
545
555
  const { isEditing, value: composerValue } = useEditComposer.getState();
546
- const valueToCopy = isEditing ? composerValue : getMessageText(message);
556
+ const valueToCopy = isEditing ? composerValue : getThreadMessageText(message);
547
557
  navigator.clipboard.writeText(valueToCopy).then(() => {
548
558
  setIsCopied(true);
549
559
  setTimeout(() => setIsCopied(false), copiedDuration);
@@ -670,7 +680,7 @@ var useComposerSend = () => {
670
680
  const { useComposer } = useComposerContext();
671
681
  const disabled = useCombinedStore(
672
682
  [useThread, useComposer],
673
- (t, c) => t.isDisabled || t.isRunning || !c.isEditing || c.value.length === 0
683
+ (t, c) => !t.unstable_canAppendNew || !c.isEditing || c.value.length === 0
674
684
  );
675
685
  const callback = useCallback11(() => {
676
686
  const composerState = useComposer.getState();
@@ -1568,8 +1578,8 @@ var ComposerPrimitiveInput = forwardRef12(
1568
1578
  const handleKeyPress = (e) => {
1569
1579
  if (isDisabled) return;
1570
1580
  if (e.key === "Enter" && e.shiftKey === false) {
1571
- const isRunning = useThread.getState().isRunning;
1572
- if (!isRunning) {
1581
+ const { unstable_canAppendNew } = useThread.getState();
1582
+ if (unstable_canAppendNew) {
1573
1583
  e.preventDefault();
1574
1584
  textareaRef.current?.closest("form")?.requestSubmit();
1575
1585
  }
@@ -1902,7 +1912,7 @@ var useMessageContext2 = (messageIndex) => {
1902
1912
  throw new Error(
1903
1913
  "Tried to edit a non-user message. Editing is only supported for user messages. This is likely an internal bug in assistant-ui."
1904
1914
  );
1905
- const text = getMessageText(message);
1915
+ const text = getThreadMessageText(message);
1906
1916
  return text;
1907
1917
  },
1908
1918
  onSend: (text) => {
@@ -2709,9 +2719,6 @@ var LocalThreadRuntime = class {
2709
2719
  get messages() {
2710
2720
  return this.repository.getMessages();
2711
2721
  }
2712
- get isRunning() {
2713
- return this.abortController != null;
2714
- }
2715
2722
  getBranches(messageId) {
2716
2723
  return this.repository.getBranches(messageId);
2717
2724
  }
@@ -2800,8 +2807,6 @@ var LocalThreadRuntime = class {
2800
2807
  updateMessage({
2801
2808
  status: { type: "complete", reason: "unknown" }
2802
2809
  });
2803
- } else {
2804
- this.notifySubscribers();
2805
2810
  }
2806
2811
  } catch (e) {
2807
2812
  this.abortController = null;
@@ -3016,7 +3021,6 @@ var ExternalStoreThreadRuntime = class {
3016
3021
  }
3017
3022
  messages = [];
3018
3023
  isDisabled = false;
3019
- isRunning = false;
3020
3024
  converter = new ThreadMessageConverter();
3021
3025
  _store;
3022
3026
  constructor(store) {
@@ -3075,7 +3079,6 @@ var ExternalStoreThreadRuntime = class {
3075
3079
  );
3076
3080
  this.messages = this.repository.getMessages();
3077
3081
  this.isDisabled = isDisabled;
3078
- this.isRunning = isRunning;
3079
3082
  for (const callback of this._subscriptions) callback();
3080
3083
  }
3081
3084
  getBranches(messageId) {