-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
123 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@assistant-ui/react-markdown": patch | ||
"@assistant-ui/react-ui": patch | ||
"@assistant-ui/react": patch | ||
--- | ||
|
||
feat: smooth text streaming |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import { useContentPartContext } from "@assistant-ui/react"; | ||
import { INTERNAL, useContentPartText } from "@assistant-ui/react"; | ||
import type { FC } from "react"; | ||
import ReactMarkdown, { type Options } from "react-markdown"; | ||
|
||
export type MarkdownTextPrimitiveProps = Omit<Options, "children">; | ||
const { useSmooth } = INTERNAL; | ||
|
||
export type MarkdownTextPrimitiveProps = Omit<Options, "children"> & { | ||
smooth?: boolean; | ||
}; | ||
|
||
export const MarkdownTextPrimitive: FC<MarkdownTextPrimitiveProps> = ( | ||
options, | ||
) => { | ||
const { useContentPart } = useContentPartContext(); | ||
const text = useContentPart((c) => { | ||
if (c.part.type !== "text") | ||
throw new Error( | ||
"This component can only be used inside text content parts.", | ||
); | ||
|
||
return c.part.text; | ||
}); | ||
return <ReactMarkdown {...options}>{text}</ReactMarkdown>; | ||
const { | ||
part: { text }, | ||
} = useContentPartText(); | ||
const smoothText = useSmooth(text, options.smooth); | ||
return <ReactMarkdown {...options}>{smoothText}</ReactMarkdown>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { ProxyConfigProvider } from "./utils/ProxyConfigProvider"; | ||
export { MessageRepository } from "./runtime/utils/MessageRepository"; | ||
export { BaseAssistantRuntime } from "./runtime/core/BaseAssistantRuntime"; | ||
export { useSmooth } from "./utils/hooks/useSmooth"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
packages/react/src/primitives/contentPart/ContentPartText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
class TextStreamAnimator { | ||
private animationFrameId: number | null = null; | ||
private lastUpdateTime: number = Date.now(); | ||
private decayFactor: number = 0.99; | ||
|
||
private _targetText: string = ""; | ||
get targetText() { | ||
return this._targetText; | ||
} | ||
set targetText(targetText: string) { | ||
this._targetText = targetText; | ||
if (this.animationFrameId === null) { | ||
this.animate(); | ||
} | ||
} | ||
|
||
constructor( | ||
private setText: (callback: (prevText: string) => string) => void, | ||
) {} | ||
|
||
stop() { | ||
if (this.animationFrameId !== null) { | ||
cancelAnimationFrame(this.animationFrameId); | ||
this.animationFrameId = null; | ||
} | ||
} | ||
|
||
private animate = () => { | ||
const currentTime = Date.now(); | ||
const deltaTime = currentTime - this.lastUpdateTime; | ||
this.lastUpdateTime = currentTime; | ||
|
||
this.setText((currentText) => { | ||
const targetText = this._targetText; | ||
|
||
if (currentText === targetText) { | ||
this.animationFrameId = null; | ||
return currentText; | ||
} | ||
|
||
const remainingChars = targetText.length - currentText.length; | ||
const charsToAdd = Math.max( | ||
1, | ||
Math.floor( | ||
remainingChars * (1 - Math.pow(this.decayFactor, deltaTime)), | ||
), | ||
); | ||
const newText = targetText.slice(0, currentText.length + charsToAdd); | ||
this.animationFrameId = requestAnimationFrame(this.animate); | ||
return newText; | ||
}); | ||
}; | ||
} | ||
|
||
export const useSmooth = (text: string, smooth: boolean = false) => { | ||
const [displayedText, setDisplayedText] = useState(text); | ||
const [animatorRef] = useState<TextStreamAnimator>( | ||
new TextStreamAnimator(setDisplayedText), | ||
); | ||
|
||
useEffect(() => { | ||
if (!smooth) { | ||
animatorRef.stop(); | ||
return; | ||
} | ||
|
||
if (!text.startsWith(animatorRef.targetText)) { | ||
setDisplayedText(text); | ||
animatorRef.stop(); | ||
return; | ||
} | ||
|
||
animatorRef.targetText = text; | ||
}, [animatorRef, smooth, text]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
animatorRef.stop(); | ||
}; | ||
}, [animatorRef]); | ||
|
||
return smooth ? displayedText : text; | ||
}; |