@assistant-ui/react 0.5.57 → 0.5.59

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -774,7 +774,10 @@ var useSmooth = (state, smooth = false) => {
774
774
  const {
775
775
  part: { text }
776
776
  } = state;
777
- const id = useMessage((m) => m.message.id);
777
+ const id = useMessage({
778
+ optional: true,
779
+ selector: (m) => m.message.id
780
+ });
778
781
  const idRef = useRef(id);
779
782
  const [displayedText, setDisplayedText] = useState2(text);
780
783
  const smoothStatusStore = useSmoothStatusStore({ optional: true });
@@ -2345,19 +2348,40 @@ var AssistantRuntimeProviderImpl = ({ children, runtime }) => {
2345
2348
  var AssistantRuntimeProvider = memo(AssistantRuntimeProviderImpl);
2346
2349
 
2347
2350
  // src/context/providers/TextContentPartProvider.tsx
2348
- import { useState as useState9 } from "react";
2351
+ import { useEffect as useEffect5, useState as useState9 } from "react";
2349
2352
  import { create as create11 } from "zustand";
2350
2353
  import { jsx as jsx9 } from "react/jsx-runtime";
2351
- var TextContentPartProvider = ({ children, text }) => {
2354
+ var COMPLETE_STATUS = {
2355
+ type: "complete"
2356
+ };
2357
+ var RUNNING_STATUS = {
2358
+ type: "running"
2359
+ };
2360
+ var TextContentPartProvider = ({ children, text, isRunning }) => {
2352
2361
  const [context] = useState9(() => {
2353
2362
  const useContentPart2 = create11(() => ({
2354
- status: { type: "complete" },
2363
+ status: isRunning ? RUNNING_STATUS : COMPLETE_STATUS,
2355
2364
  part: { type: "text", text }
2356
2365
  }));
2357
2366
  return {
2358
2367
  useContentPart: useContentPart2
2359
2368
  };
2360
2369
  });
2370
+ useEffect5(() => {
2371
+ const state = context.useContentPart.getState();
2372
+ const textUpdated = state.part.text !== text;
2373
+ const targetTextPart = textUpdated ? { type: "text", text } : state.part;
2374
+ const targetStatus = isRunning ? RUNNING_STATUS : COMPLETE_STATUS;
2375
+ const statusUpdated = state.status !== targetStatus;
2376
+ if (!textUpdated && !statusUpdated) return;
2377
+ writableStore(context.useContentPart).setState(
2378
+ {
2379
+ part: targetTextPart,
2380
+ status: targetStatus
2381
+ },
2382
+ true
2383
+ );
2384
+ }, [context, isRunning, text]);
2361
2385
  return /* @__PURE__ */ jsx9(ContentPartContext.Provider, { value: context, children });
2362
2386
  };
2363
2387
 
@@ -2452,11 +2476,11 @@ var useSwitchToNewThread = () => {
2452
2476
  };
2453
2477
 
2454
2478
  // src/model-config/useAssistantTool.tsx
2455
- import { useEffect as useEffect5 } from "react";
2479
+ import { useEffect as useEffect6 } from "react";
2456
2480
  var useAssistantTool = (tool) => {
2457
2481
  const assistantActionsStore = useAssistantActionsStore();
2458
2482
  const toolUIsStore = useToolUIsStore();
2459
- useEffect5(() => {
2483
+ useEffect6(() => {
2460
2484
  const { toolName, render, ...rest } = tool;
2461
2485
  const config = {
2462
2486
  tools: {
@@ -2485,10 +2509,10 @@ var makeAssistantTool = (tool) => {
2485
2509
  };
2486
2510
 
2487
2511
  // src/model-config/useAssistantToolUI.tsx
2488
- import { useEffect as useEffect6 } from "react";
2512
+ import { useEffect as useEffect7 } from "react";
2489
2513
  var useAssistantToolUI = (tool) => {
2490
2514
  const toolUIsStore = useToolUIsStore();
2491
- useEffect6(() => {
2515
+ useEffect7(() => {
2492
2516
  if (!tool) return;
2493
2517
  const { toolName, render } = tool;
2494
2518
  return toolUIsStore.getState().setToolUI(toolName, render);
@@ -2506,10 +2530,10 @@ var makeAssistantToolUI = (tool) => {
2506
2530
  };
2507
2531
 
2508
2532
  // src/model-config/useAssistantInstructions.tsx
2509
- import { useEffect as useEffect7 } from "react";
2533
+ import { useEffect as useEffect8 } from "react";
2510
2534
  var useAssistantInstructions = (instruction) => {
2511
2535
  const actionsStore = useAssistantActionsStore();
2512
- useEffect7(() => {
2536
+ useEffect8(() => {
2513
2537
  const config = {
2514
2538
  system: instruction
2515
2539
  };
@@ -3048,11 +3072,11 @@ import { composeEventHandlers as composeEventHandlers3 } from "@radix-ui/primiti
3048
3072
 
3049
3073
  // src/utils/hooks/useOnComposerFocus.tsx
3050
3074
  import { useCallbackRef as useCallbackRef2 } from "@radix-ui/react-use-callback-ref";
3051
- import { useEffect as useEffect8 } from "react";
3075
+ import { useEffect as useEffect9 } from "react";
3052
3076
  var useOnComposerFocus = (callback) => {
3053
3077
  const callbackRef = useCallbackRef2(callback);
3054
3078
  const threadComposerStore = useThreadComposerStore();
3055
- useEffect8(() => {
3079
+ useEffect9(() => {
3056
3080
  return threadComposerStore.getState().onFocus(() => {
3057
3081
  callbackRef();
3058
3082
  });
@@ -3281,24 +3305,24 @@ MessagePrimitiveIf.displayName = "MessagePrimitive.If";
3281
3305
  import { memo as memo2 } from "react";
3282
3306
 
3283
3307
  // src/context/providers/ContentPartProvider.tsx
3284
- import { useEffect as useEffect9, useState as useState11 } from "react";
3308
+ import { useEffect as useEffect10, useState as useState11 } from "react";
3285
3309
  import { create as create12 } from "zustand";
3286
3310
  import { jsx as jsx20 } from "react/jsx-runtime";
3287
- var COMPLETE_STATUS = {
3311
+ var COMPLETE_STATUS2 = {
3288
3312
  type: "complete"
3289
3313
  };
3290
3314
  var toContentPartStatus = (message, partIndex, part) => {
3291
- if (message.role !== "assistant") return COMPLETE_STATUS;
3315
+ if (message.role !== "assistant") return COMPLETE_STATUS2;
3292
3316
  const isLastPart = partIndex === Math.max(0, message.content.length - 1);
3293
3317
  if (part.type !== "tool-call") {
3294
3318
  if ("reason" in message.status && message.status.reason === "tool-calls" && isLastPart)
3295
3319
  throw new Error(
3296
3320
  "Encountered unexpected requires-action status. This is likely an internal bug in assistant-ui."
3297
3321
  );
3298
- return isLastPart ? message.status : COMPLETE_STATUS;
3322
+ return isLastPart ? message.status : COMPLETE_STATUS2;
3299
3323
  }
3300
3324
  if (!!part.result) {
3301
- return COMPLETE_STATUS;
3325
+ return COMPLETE_STATUS2;
3302
3326
  }
3303
3327
  return message.status;
3304
3328
  };
@@ -3328,7 +3352,7 @@ var useContentPartContext2 = (partIndex) => {
3328
3352
  );
3329
3353
  return { useContentPart: useContentPart2 };
3330
3354
  });
3331
- useEffect9(() => {
3355
+ useEffect10(() => {
3332
3356
  const syncContentPart = (message) => {
3333
3357
  const newState = getContentPartState(
3334
3358
  message,
@@ -3535,7 +3559,7 @@ var {
3535
3559
  } = createContextStoreHook(useMessageAttachmentContext, "useAttachment");
3536
3560
 
3537
3561
  // src/context/providers/MessageAttachmentProvider.tsx
3538
- import { useEffect as useEffect10, useState as useState12 } from "react";
3562
+ import { useEffect as useEffect11, useState as useState12 } from "react";
3539
3563
  import { create as create13 } from "zustand";
3540
3564
  import { jsx as jsx24 } from "react/jsx-runtime";
3541
3565
  var getAttachment = ({ message }, useAttachment2, partIndex) => {
@@ -3557,7 +3581,7 @@ var useMessageAttachmentContext2 = (partIndex) => {
3557
3581
  return { type: "message", useAttachment: useAttachment2 };
3558
3582
  }
3559
3583
  );
3560
- useEffect10(() => {
3584
+ useEffect11(() => {
3561
3585
  const syncAttachment = (messageState) => {
3562
3586
  const newState = getAttachment(
3563
3587
  messageState,
@@ -3677,7 +3701,7 @@ import { Slot } from "@radix-ui/react-slot";
3677
3701
  import {
3678
3702
  forwardRef as forwardRef16,
3679
3703
  useCallback as useCallback17,
3680
- useEffect as useEffect11,
3704
+ useEffect as useEffect12,
3681
3705
  useRef as useRef4
3682
3706
  } from "react";
3683
3707
  import TextareaAutosize from "react-textarea-autosize";
@@ -3730,7 +3754,7 @@ var ComposerPrimitiveInput = forwardRef16(
3730
3754
  textareaRef.current.value.length
3731
3755
  );
3732
3756
  }, [autoFocusEnabled]);
3733
- useEffect11(() => focus(), [focus]);
3757
+ useEffect12(() => focus(), [focus]);
3734
3758
  useOnComposerFocus(() => {
3735
3759
  if (composerStore.getState().type === "thread") {
3736
3760
  focus();
@@ -3778,7 +3802,7 @@ var ComposerPrimitiveAddAttachment = createActionButton(
3778
3802
  import { memo as memo4 } from "react";
3779
3803
 
3780
3804
  // src/context/providers/ComposerAttachmentProvider.tsx
3781
- import { useEffect as useEffect12, useState as useState13 } from "react";
3805
+ import { useEffect as useEffect13, useState as useState13 } from "react";
3782
3806
  import { create as create14 } from "zustand";
3783
3807
  import { jsx as jsx29 } from "react/jsx-runtime";
3784
3808
  var getAttachment2 = ({ attachments }, useAttachment2, partIndex) => {
@@ -3798,7 +3822,7 @@ var useComposerAttachmentContext2 = (partIndex) => {
3798
3822
  return { type: "composer", useAttachment: useAttachment2 };
3799
3823
  }
3800
3824
  );
3801
- useEffect12(() => {
3825
+ useEffect13(() => {
3802
3826
  const syncAttachment = (composer) => {
3803
3827
  const newState = getAttachment2(
3804
3828
  composer,
@@ -3971,11 +3995,11 @@ var useOnResizeContent = (callback) => {
3971
3995
 
3972
3996
  // src/utils/hooks/useOnScrollToBottom.tsx
3973
3997
  import { useCallbackRef as useCallbackRef4 } from "@radix-ui/react-use-callback-ref";
3974
- import { useEffect as useEffect13 } from "react";
3998
+ import { useEffect as useEffect14 } from "react";
3975
3999
  var useOnScrollToBottom = (callback) => {
3976
4000
  const callbackRef = useCallbackRef4(callback);
3977
4001
  const threadViewportStore = useThreadViewportStore();
3978
- useEffect13(() => {
4002
+ useEffect14(() => {
3979
4003
  return threadViewportStore.getState().onScrollToBottom(() => {
3980
4004
  callbackRef();
3981
4005
  });
@@ -4048,7 +4072,7 @@ ThreadPrimitiveViewport.displayName = "ThreadPrimitive.Viewport";
4048
4072
  import { memo as memo5 } from "react";
4049
4073
 
4050
4074
  // src/context/providers/MessageProvider.tsx
4051
- import { useEffect as useEffect14, useState as useState14 } from "react";
4075
+ import { useEffect as useEffect15, useState as useState14 } from "react";
4052
4076
  import { create as create17 } from "zustand";
4053
4077
 
4054
4078
  // src/context/stores/EditComposer.ts
@@ -4174,7 +4198,7 @@ var useMessageContext2 = (messageIndex) => {
4174
4198
  });
4175
4199
  return { useMessage: useMessage2, useMessageUtils: useMessageUtils2, useEditComposer: useEditComposer2 };
4176
4200
  });
4177
- useEffect14(() => {
4201
+ useEffect15(() => {
4178
4202
  const syncMessage = (thread) => {
4179
4203
  const newState = getMessageState(
4180
4204
  thread,