@assistant-ui/react 0.4.1 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -907,21 +907,30 @@ var ActionBarPrimitiveRoot = (0, import_react25.forwardRef)(({ hideWhenRunning,
907
907
  ActionBarPrimitiveRoot.displayName = "ActionBarPrimitive.Root";
908
908
 
909
909
  // src/utils/createActionButton.tsx
910
- var import_primitive = require("@radix-ui/primitive");
911
- var import_react_primitive2 = require("@radix-ui/react-primitive");
912
910
  var import_react26 = require("react");
911
+ var import_react_primitive2 = require("@radix-ui/react-primitive");
912
+ var import_primitive = require("@radix-ui/primitive");
913
913
  var import_jsx_runtime5 = require("react/jsx-runtime");
914
- var createActionButton = (displayName, useActionButton) => {
914
+ var createActionButton = (displayName, useActionButton, forwardProps = []) => {
915
915
  const ActionButton = (0, import_react26.forwardRef)((props, forwardedRef) => {
916
- const callback = useActionButton(props);
916
+ const forwardedProps = {};
917
+ const primitiveProps = {};
918
+ Object.keys(props).forEach((key) => {
919
+ if (forwardProps.includes(key)) {
920
+ forwardedProps[key] = props[key];
921
+ } else {
922
+ primitiveProps[key] = props[key];
923
+ }
924
+ });
925
+ const callback = useActionButton(forwardedProps);
917
926
  return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
918
927
  import_react_primitive2.Primitive.button,
919
928
  {
920
929
  type: "button",
921
930
  disabled: !callback,
922
- ...props,
931
+ ...primitiveProps,
923
932
  ref: forwardedRef,
924
- onClick: (0, import_primitive.composeEventHandlers)(props.onClick, () => {
933
+ onClick: (0, import_primitive.composeEventHandlers)(primitiveProps.onClick, () => {
925
934
  callback?.();
926
935
  })
927
936
  }
@@ -934,7 +943,8 @@ var createActionButton = (displayName, useActionButton) => {
934
943
  // src/primitives/actionBar/ActionBarCopy.tsx
935
944
  var ActionBarPrimitiveCopy = createActionButton(
936
945
  "ActionBarPrimitive.Copy",
937
- useActionBarCopy
946
+ useActionBarCopy,
947
+ ["copiedDuration"]
938
948
  );
939
949
 
940
950
  // src/primitives/actionBar/ActionBarReload.tsx
@@ -1288,6 +1298,9 @@ var TextStreamAnimator = class {
1288
1298
  };
1289
1299
  };
1290
1300
  var useSmooth = (text, smooth = false) => {
1301
+ const { useMessage } = useMessageContext();
1302
+ const id = useMessage((m) => m.message.id);
1303
+ const idRef = (0, import_react35.useRef)(id);
1291
1304
  const [displayedText, setDisplayedText] = (0, import_react35.useState)(text);
1292
1305
  const [animatorRef] = (0, import_react35.useState)(
1293
1306
  new TextStreamAnimator(text, setDisplayedText)
@@ -1297,7 +1310,8 @@ var useSmooth = (text, smooth = false) => {
1297
1310
  animatorRef.stop();
1298
1311
  return;
1299
1312
  }
1300
- if (!text.startsWith(animatorRef.targetText)) {
1313
+ if (idRef.current !== id || !text.startsWith(animatorRef.targetText)) {
1314
+ idRef.current = id;
1301
1315
  setDisplayedText(text);
1302
1316
  animatorRef.currentText = text;
1303
1317
  animatorRef.targetText = text;
@@ -1306,7 +1320,7 @@ var useSmooth = (text, smooth = false) => {
1306
1320
  }
1307
1321
  animatorRef.targetText = text;
1308
1322
  animatorRef.start();
1309
- }, [animatorRef, smooth, text]);
1323
+ }, [animatorRef, id, smooth, text]);
1310
1324
  (0, import_react35.useEffect)(() => {
1311
1325
  return () => {
1312
1326
  animatorRef.stop();
@@ -1708,14 +1722,11 @@ var useThreadViewportAutoScroll = ({
1708
1722
  }) => {
1709
1723
  const divRef = (0, import_react46.useRef)(null);
1710
1724
  const { useViewport } = useThreadContext();
1711
- const firstRenderRef = (0, import_react46.useRef)(true);
1712
1725
  const lastScrollTop = (0, import_react46.useRef)(0);
1713
1726
  const isScrollingToBottomRef = (0, import_react46.useRef)(false);
1714
- const scrollToBottom = () => {
1727
+ const scrollToBottom = (behavior) => {
1715
1728
  const div = divRef.current;
1716
1729
  if (!div || !autoScroll) return;
1717
- const behavior = firstRenderRef.current ? "instant" : "auto";
1718
- firstRenderRef.current = false;
1719
1730
  isScrollingToBottomRef.current = true;
1720
1731
  div.scrollTo({ top: div.scrollHeight, behavior });
1721
1732
  };
@@ -1726,7 +1737,9 @@ var useThreadViewportAutoScroll = ({
1726
1737
  const newIsAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight;
1727
1738
  if (!newIsAtBottom && lastScrollTop.current < div.scrollTop) {
1728
1739
  } else {
1729
- isScrollingToBottomRef.current = newIsAtBottom;
1740
+ if (newIsAtBottom) {
1741
+ isScrollingToBottomRef.current = false;
1742
+ }
1730
1743
  if (newIsAtBottom !== isAtBottom) {
1731
1744
  useViewport.setState({
1732
1745
  isAtBottom: newIsAtBottom
@@ -1736,11 +1749,10 @@ var useThreadViewportAutoScroll = ({
1736
1749
  lastScrollTop.current = div.scrollTop;
1737
1750
  };
1738
1751
  const resizeRef = useOnResizeContent(() => {
1739
- if (!isScrollingToBottomRef.current && !useViewport.getState().isAtBottom && !firstRenderRef.current) {
1740
- handleScroll();
1741
- } else {
1742
- scrollToBottom();
1752
+ if (isScrollingToBottomRef.current || useViewport.getState().isAtBottom) {
1753
+ scrollToBottom("instant");
1743
1754
  }
1755
+ handleScroll();
1744
1756
  });
1745
1757
  const scrollRef = useManagedRef((el) => {
1746
1758
  el.addEventListener("scroll", handleScroll);
@@ -1750,7 +1762,7 @@ var useThreadViewportAutoScroll = ({
1750
1762
  });
1751
1763
  const autoScrollRef = (0, import_react_compose_refs3.useComposedRefs)(resizeRef, scrollRef, divRef);
1752
1764
  useOnScrollToBottom(() => {
1753
- scrollToBottom();
1765
+ scrollToBottom("auto");
1754
1766
  });
1755
1767
  return autoScrollRef;
1756
1768
  };
@@ -1951,7 +1963,8 @@ var ThreadPrimitiveScrollToBottom = createActionButton(
1951
1963
  // src/primitives/thread/ThreadSuggestion.tsx
1952
1964
  var ThreadPrimitiveSuggestion = createActionButton(
1953
1965
  "ThreadPrimitive.Suggestion",
1954
- useThreadSuggestion
1966
+ useThreadSuggestion,
1967
+ ["prompt", "autoSend", "method"]
1955
1968
  );
1956
1969
 
1957
1970
  // src/runtimes/local/useLocalRuntime.tsx
@@ -2344,7 +2357,7 @@ function assistantDecoderStream() {
2344
2357
  let currentToolCall;
2345
2358
  return new TransformStream({
2346
2359
  transform(chunk, controller) {
2347
- const [code, valueJson] = chunk.split(":");
2360
+ const [code, valueJson] = parseStreamPart(chunk);
2348
2361
  const value = JSON.parse(valueJson);
2349
2362
  if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2350
2363
  controller.enqueue({
@@ -2403,6 +2416,14 @@ function assistantDecoderStream() {
2403
2416
  }
2404
2417
  });
2405
2418
  }
2419
+ var parseStreamPart = (part) => {
2420
+ const index = part.indexOf(":");
2421
+ if (index === -1) throw new Error("Invalid stream part");
2422
+ return [
2423
+ part.slice(0, index),
2424
+ part.slice(index + 1)
2425
+ ];
2426
+ };
2406
2427
 
2407
2428
  // src/runtimes/edge/streams/chunkByLineStream.ts
2408
2429
  function chunkByLineStream() {