@assistant-ui/react 0.4.2 → 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
@@ -1298,6 +1298,9 @@ var TextStreamAnimator = class {
1298
1298
  };
1299
1299
  };
1300
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);
1301
1304
  const [displayedText, setDisplayedText] = (0, import_react35.useState)(text);
1302
1305
  const [animatorRef] = (0, import_react35.useState)(
1303
1306
  new TextStreamAnimator(text, setDisplayedText)
@@ -1307,7 +1310,8 @@ var useSmooth = (text, smooth = false) => {
1307
1310
  animatorRef.stop();
1308
1311
  return;
1309
1312
  }
1310
- if (!text.startsWith(animatorRef.targetText)) {
1313
+ if (idRef.current !== id || !text.startsWith(animatorRef.targetText)) {
1314
+ idRef.current = id;
1311
1315
  setDisplayedText(text);
1312
1316
  animatorRef.currentText = text;
1313
1317
  animatorRef.targetText = text;
@@ -1316,7 +1320,7 @@ var useSmooth = (text, smooth = false) => {
1316
1320
  }
1317
1321
  animatorRef.targetText = text;
1318
1322
  animatorRef.start();
1319
- }, [animatorRef, smooth, text]);
1323
+ }, [animatorRef, id, smooth, text]);
1320
1324
  (0, import_react35.useEffect)(() => {
1321
1325
  return () => {
1322
1326
  animatorRef.stop();
@@ -1718,14 +1722,11 @@ var useThreadViewportAutoScroll = ({
1718
1722
  }) => {
1719
1723
  const divRef = (0, import_react46.useRef)(null);
1720
1724
  const { useViewport } = useThreadContext();
1721
- const firstRenderRef = (0, import_react46.useRef)(true);
1722
1725
  const lastScrollTop = (0, import_react46.useRef)(0);
1723
1726
  const isScrollingToBottomRef = (0, import_react46.useRef)(false);
1724
- const scrollToBottom = () => {
1727
+ const scrollToBottom = (behavior) => {
1725
1728
  const div = divRef.current;
1726
1729
  if (!div || !autoScroll) return;
1727
- const behavior = firstRenderRef.current ? "instant" : "auto";
1728
- firstRenderRef.current = false;
1729
1730
  isScrollingToBottomRef.current = true;
1730
1731
  div.scrollTo({ top: div.scrollHeight, behavior });
1731
1732
  };
@@ -1736,7 +1737,9 @@ var useThreadViewportAutoScroll = ({
1736
1737
  const newIsAtBottom = div.scrollHeight - div.scrollTop <= div.clientHeight;
1737
1738
  if (!newIsAtBottom && lastScrollTop.current < div.scrollTop) {
1738
1739
  } else {
1739
- isScrollingToBottomRef.current = newIsAtBottom;
1740
+ if (newIsAtBottom) {
1741
+ isScrollingToBottomRef.current = false;
1742
+ }
1740
1743
  if (newIsAtBottom !== isAtBottom) {
1741
1744
  useViewport.setState({
1742
1745
  isAtBottom: newIsAtBottom
@@ -1746,11 +1749,10 @@ var useThreadViewportAutoScroll = ({
1746
1749
  lastScrollTop.current = div.scrollTop;
1747
1750
  };
1748
1751
  const resizeRef = useOnResizeContent(() => {
1749
- if (!isScrollingToBottomRef.current && !useViewport.getState().isAtBottom && !firstRenderRef.current) {
1750
- handleScroll();
1751
- } else {
1752
- scrollToBottom();
1752
+ if (isScrollingToBottomRef.current || useViewport.getState().isAtBottom) {
1753
+ scrollToBottom("instant");
1753
1754
  }
1755
+ handleScroll();
1754
1756
  });
1755
1757
  const scrollRef = useManagedRef((el) => {
1756
1758
  el.addEventListener("scroll", handleScroll);
@@ -1760,7 +1762,7 @@ var useThreadViewportAutoScroll = ({
1760
1762
  });
1761
1763
  const autoScrollRef = (0, import_react_compose_refs3.useComposedRefs)(resizeRef, scrollRef, divRef);
1762
1764
  useOnScrollToBottom(() => {
1763
- scrollToBottom();
1765
+ scrollToBottom("auto");
1764
1766
  });
1765
1767
  return autoScrollRef;
1766
1768
  };
@@ -2355,7 +2357,7 @@ function assistantDecoderStream() {
2355
2357
  let currentToolCall;
2356
2358
  return new TransformStream({
2357
2359
  transform(chunk, controller) {
2358
- const [code, valueJson] = chunk.split(":");
2360
+ const [code, valueJson] = parseStreamPart(chunk);
2359
2361
  const value = JSON.parse(valueJson);
2360
2362
  if (currentToolCall && code !== "2" /* ToolCallArgsTextDelta */ && code !== "E" /* Error */) {
2361
2363
  controller.enqueue({
@@ -2414,6 +2416,14 @@ function assistantDecoderStream() {
2414
2416
  }
2415
2417
  });
2416
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
+ };
2417
2427
 
2418
2428
  // src/runtimes/edge/streams/chunkByLineStream.ts
2419
2429
  function chunkByLineStream() {