{"version":3,"file":"StreamingMessage.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/StreamingMessage/StreamingMessage.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport React, {\n  forwardRef,\n  useEffect,\n  useImperativeHandle,\n  useRef,\n  useState,\n} from 'react';\n\nimport { useStableCallback } from '../../../utils/useStableCallback';\nimport { AIMarkdown } from '../AIMarkdown';\nimport { ThemeScopeProvider, useAIThemeScopeClassName } from '../themeScope';\n\nexport type UseMessageTextStreamingProps = {\n  /** A fixed pace instead of catching up: the time between each step, in milliseconds. */\n  letterIntervalMs?: number;\n  /** A fixed pace instead of catching up: the letters written each step. */\n  renderingLetterCount?: number;\n  text: string;\n};\n\nexport type StreamingMessageRef = { skipAnimation: () => void };\n\n/**\n * However much of the answer has arrived is written out within this, so the text on screen is\n * never far behind the agent: a long chunk is typed fast, a trickle at the pace it comes.\n */\nconst CATCH_UP_MS = 400;\n/** One frame, the step of the catching-up pace. */\nconst FRAME_MS = 16;\n/** The fixed pace's defaults, when only one of its props is given. */\nconst DEFAULT_LETTER_INTERVAL_MS = 30;\nconst DEFAULT_RENDERING_LETTER_COUNT = 2;\n\n/**\n * The part of `text` written so far. Text that grows is typed out from where it was; text\n * there from the start, such as a reply loaded from history, shows at once.\n */\nconst useTypedText = ({\n  letterIntervalMs,\n  renderingLetterCount,\n  text,\n}: UseMessageTextStreamingProps) => {\n  const isFixed = letterIntervalMs !== undefined || renderingLetterCount !== undefined;\n  const [written, setWritten] = useState(text.length);\n  const cursor = useRef(text.length);\n\n  useEffect(() => {\n    if (cursor.current > text.length) {\n      // The text was replaced by a shorter one: there is nothing past its end to keep.\n      cursor.current = text.length;\n      setWritten(text.length);\n    }\n    if (cursor.current >= text.length) return;\n\n    // Catching up sets its pace as text arrives: enough letters a frame to write what is behind\n    // in CATCH_UP_MS, and at least one, kept steady until more text comes.\n    const step = isFixed\n      ? (renderingLetterCount ?? DEFAULT_RENDERING_LETTER_COUNT)\n      : Math.max(1, Math.ceil(((text.length - cursor.current) * FRAME_MS) / CATCH_UP_MS));\n\n    const timer = setInterval(\n      () => {\n        if (cursor.current >= text.length) {\n          clearInterval(timer);\n          return;\n        }\n        cursor.current = Math.min(text.length, cursor.current + step);\n        setWritten(cursor.current);\n      },\n      isFixed ? (letterIntervalMs ?? DEFAULT_LETTER_INTERVAL_MS) : FRAME_MS,\n    );\n    return () => clearInterval(timer);\n  }, [isFixed, letterIntervalMs, renderingLetterCount, text]);\n\n  const skipAnimation = useStableCallback(() => {\n    cursor.current = text.length;\n    setWritten(text.length);\n  });\n\n  return { skipAnimation, typedText: text.slice(0, written) } as const;\n};\n\nexport const StreamingMessage = forwardRef<\n  StreamingMessageRef,\n  UseMessageTextStreamingProps\n>(({ letterIntervalMs, renderingLetterCount, text }, ref) => {\n  const themeScopeClassName = useAIThemeScopeClassName();\n  const { skipAnimation, typedText } = useTypedText({\n    letterIntervalMs,\n    renderingLetterCount,\n    text,\n  });\n\n  useImperativeHandle(ref, () => ({ skipAnimation }), [skipAnimation]);\n\n  return (\n    <ThemeScopeProvider>\n      <div className={clsx(themeScopeClassName, 'str-chat__ai-streaming-message')}>\n        <AIMarkdown>{typedText}</AIMarkdown>\n      </div>\n    </ThemeScopeProvider>\n  );\n});\n\nStreamingMessage.displayName = 'StreamingMessage';\n"],"mappings":";;;;;;;;;;;;AA2BA,IAAM,cAAc;;AAEpB,IAAM,WAAW;;AAEjB,IAAM,6BAA6B;AACnC,IAAM,iCAAiC;;;;;AAMvC,IAAM,gBAAgB,EACpB,kBACA,sBACA,WACkC;CAClC,MAAM,UAAU,qBAAqB,KAAA,KAAa,yBAAyB,KAAA;CAC3E,MAAM,CAAC,SAAS,cAAc,SAAS,KAAK,MAAM;CAClD,MAAM,SAAS,OAAO,KAAK,MAAM;CAEjC,gBAAgB;EACd,IAAI,OAAO,UAAU,KAAK,QAAQ;GAEhC,OAAO,UAAU,KAAK;GACtB,WAAW,KAAK,MAAM;EACxB;EACA,IAAI,OAAO,WAAW,KAA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