Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(mothership): stream retry state machine, progressive re-rendering#4287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
33 changes: 26 additions & 7 deletions
33 apps/sim/app/workspace/[workspaceId]/home/components/mothership-chat/mothership-chat.tsx
This file contains hidden or 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 hidden or 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,6 +1,6 @@ | ||
| 'use client' | ||
| import { useEffect, useRef, useState } from 'react' | ||
| import { useEffect, useLayoutEffect, useRef, useState } from 'react' | ||
| interface ProgressiveListOptions { | ||
| /** Number of items to render in the initial batch (most recent items) */ | ||
| @@ -14,15 +14,31 @@ const DEFAULTS = { | ||
| batchSize: 5, | ||
| } satisfies Required<ProgressiveListOptions> | ||
| interface ProgressiveListState { | ||
| key: string | ||
| count: number | ||
| caughtUp: boolean | ||
| } | ||
| function createInitialState( | ||
| key: string, | ||
| itemCount: number, | ||
| initialBatch: number | ||
| ): ProgressiveListState { | ||
| const count = Math.min(itemCount, initialBatch) | ||
| return { | ||
| key, | ||
| count, | ||
| caughtUp: itemCount > 0 && count >= itemCount, | ||
| } | ||
| } | ||
| /** | ||
| * Progressively renders a list of items so that first paint is fast. | ||
| * | ||
| * On mount (or when `key` changes), only the most recent `initialBatch` | ||
| * items are rendered. The rest are added in `batchSize` increments via | ||
| * `requestAnimationFrame` so the browser never blocks on a large DOM mount. | ||
| * | ||
| * Once staging completes for a given key it never re-stages -- new items | ||
| * appended to the list are rendered immediately. | ||
| * `requestAnimationFrame`. | ||
| * | ||
| * @param items Full list of items to render. | ||
| * @param key A session/conversation identifier. When it changes, | ||
| @@ -35,67 +51,83 @@ export function useProgressiveList<T>( | ||
| key: string, | ||
| options?: ProgressiveListOptions | ||
| ): { staged: T[]; isStaging: boolean } { | ||
| const initialBatch = options?.initialBatch ?? DEFAULTS.initialBatch | ||
| const batchSize = options?.batchSize ?? DEFAULTS.batchSize | ||
| const initialBatch = Math.max(0, options?.initialBatch ?? DEFAULTS.initialBatch) | ||
| const batchSize = Math.max(1, options?.batchSize ?? DEFAULTS.batchSize) | ||
| const [state, setState] = useState(() => createInitialState(key, items.length, initialBatch)) | ||
| const latestItemCountRef = useRef(items.length) | ||
| useLayoutEffect(() => { | ||
| latestItemCountRef.current = items.length | ||
| }, [items.length]) | ||
| const completedKeysRef = useRef(new Set<string>()) | ||
| const prevKeyRef = useRef(key) | ||
| const stagingCountRef = useRef(initialBatch) | ||
| const [count, setCount] = useState(() => { | ||
| if (items.length <= initialBatch) return items.length | ||
| return initialBatch | ||
| }) | ||
| const renderState = | ||
| state.key === key && (state.count > 0 || items.length === 0 || state.caughtUp) | ||
| ? state | ||
| : createInitialState(key, items.length, initialBatch) | ||
| useEffect(() => { | ||
| if (completedKeysRef.current.has(key)) { | ||
| setCount(items.length) | ||
| return | ||
| } | ||
| setState((prev) => { | ||
| if (prev.key !== key) { | ||
| return createInitialState(key, items.length, initialBatch) | ||
| } | ||
| if (items.length <= initialBatch) { | ||
| setCount(items.length) | ||
| completedKeysRef.current.add(key) | ||
| return | ||
| } | ||
| if (items.length === 0) { | ||
| if (prev.count === 0 && !prev.caughtUp) { | ||
| return prev | ||
| } | ||
| return { key, count: 0, caughtUp: false } | ||
| } | ||
| let current = Math.max(stagingCountRef.current, initialBatch) | ||
| setCount(current) | ||
| if (prev.caughtUp) { | ||
| if (prev.count === items.length) { | ||
| return prev | ||
| } | ||
| return { key, count: items.length, caughtUp: true } | ||
| } | ||
| let frame: number | undefined | ||
| const minimumCount = Math.min(items.length, initialBatch) | ||
| if (prev.count >= minimumCount && prev.count <= items.length) { | ||
| return prev | ||
| } | ||
| const step = () => { | ||
| const total = items.length | ||
| current = Math.min(total, current + batchSize) | ||
| stagingCountRef.current = current | ||
| setCount(current) | ||
| if (current >= total) { | ||
| completedKeysRef.current.add(key) | ||
| frame = undefined | ||
| return | ||
| const count = Math.min(items.length, Math.max(prev.count, minimumCount)) | ||
| return { | ||
| key, | ||
| count, | ||
| caughtUp: count >= items.length, | ||
| } | ||
| frame = requestAnimationFrame(step) | ||
| }) | ||
| }, [key, items.length, initialBatch]) | ||
| useEffect(() => { | ||
| if (state.key !== key || state.caughtUp || state.count >= items.length) { | ||
| return | ||
| } | ||
| frame = requestAnimationFrame(step) | ||
| const frame = requestAnimationFrame(() => { | ||
| setState((prev) => { | ||
| if (prev.key !== key || prev.caughtUp) { | ||
| return prev | ||
| } | ||
| return () => { | ||
| if (frame !== undefined) cancelAnimationFrame(frame) | ||
| } | ||
| }, [key, items.length, initialBatch, batchSize]) | ||
| const itemCount = latestItemCountRef.current | ||
| const count = Math.min(itemCount, prev.count + batchSize) | ||
| return { | ||
| key, | ||
| count, | ||
| caughtUp: count >= itemCount, | ||
| } | ||
| }) | ||
| }) | ||
| let effectiveCount = count | ||
| if (prevKeyRef.current !== key) { | ||
| effectiveCount = items.length <= initialBatch ? items.length : initialBatch | ||
| stagingCountRef.current = initialBatch | ||
| } | ||
| prevKeyRef.current = key | ||
| const isCompleted = completedKeysRef.current.has(key) | ||
| const isStaging = !isCompleted && effectiveCount < items.length | ||
| const staged = | ||
| isCompleted || effectiveCount >= items.length | ||
| ? items | ||
| : items.slice(Math.max(0, items.length - effectiveCount)) | ||
| return () => cancelAnimationFrame(frame) | ||
| }, [state.key, state.count, state.caughtUp, key, items.length, batchSize]) | ||
icecrasher321 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const effectiveCount = renderState.caughtUp | ||
| ? items.length | ||
| : Math.min(renderState.count, items.length) | ||
| const staged = items.slice(Math.max(0, items.length - effectiveCount)) | ||
| const isStaging = effectiveCount < items.length | ||
| return { staged, isStaging } | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.