-
Notifications
You must be signed in to change notification settings - Fork 92
chore: support history #52
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,115 @@ export default function App({ | |
| // Whether an interrupt has been requested for the current stream | ||
| const [interruptRequested, setInterruptRequested] = useState(false); | ||
|
|
||
| // Track message history with pagination | ||
| const [messages, setMessages] = useState<LettaMessageUnion[]>(messageHistory); | ||
| const [isLoadingHistory, setIsLoadingHistory] = useState(false); | ||
| const [hasMoreHistory, setHasMoreHistory] = useState(true); | ||
| const [historyInitialized, setHistoryInitialized] = useState(false); | ||
| const [isLoadingInitialHistory, setIsLoadingInitialHistory] = useState(false); | ||
|
|
||
| // Fetch full message history on mount | ||
| useEffect(() => { | ||
| async function fetchInitialHistory() { | ||
| if (historyInitialized || agentId === "loading") { | ||
| return; | ||
| } | ||
|
|
||
| setHistoryInitialized(true); | ||
| setIsLoadingInitialHistory(true); | ||
|
|
||
| try { | ||
| const client = await getClient(); | ||
|
|
||
| // Fetch more messages initially to have a good buffer for fast scrolling | ||
| const INITIAL_LIMIT = 200; | ||
| const messagesPage = await client.agents.messages.list(agentId, { | ||
| limit: INITIAL_LIMIT, | ||
| }); | ||
|
|
||
| setMessages(messagesPage.items); | ||
|
|
||
| // If we got fewer than requested, we have all the history | ||
| setHasMoreHistory(messagesPage.items.length >= INITIAL_LIMIT); | ||
| } catch (error) { | ||
| console.error("[History Init] Error fetching initial history:", error); | ||
| // Fall back to the passed-in messageHistory | ||
| setMessages(messageHistory); | ||
| setHasMoreHistory(messageHistory.length > 0); | ||
| } finally { | ||
| setIsLoadingInitialHistory(false); | ||
| } | ||
| } | ||
|
|
||
| fetchInitialHistory(); | ||
| }, [agentId, historyInitialized, messageHistory]); | ||
|
|
||
| // Sync messageHistory updates (from new messages being sent) | ||
| useEffect(() => { | ||
| // Only update if the latest message in messageHistory is newer than in messages | ||
| if ( | ||
| historyInitialized && | ||
| messageHistory.length > 0 && | ||
| ( | ||
| messages.length === 0 || | ||
| (messageHistory[messageHistory.length - 1]?.id !== messages[messages.length - 1]?.id) | ||
| ) | ||
| ) { | ||
| setMessages(messageHistory); | ||
| } | ||
| }, [messageHistory, messages, historyInitialized]); | ||
|
|
||
| // Fetch earlier messages for history pagination | ||
| const fetchEarlierMessages = useCallback(async () => { | ||
| if (isLoadingHistory || !hasMoreHistory || agentId === "loading") { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| setIsLoadingHistory(true); | ||
| const client = await getClient(); | ||
|
|
||
| // Get the ID of the earliest message we have | ||
| const earliestMessage = messages[0]; | ||
| if (!earliestMessage) { | ||
| setHasMoreHistory(false); | ||
| return; | ||
| } | ||
|
|
||
| // Fetch PAGE_SIZE + 1 to determine if there are more messages beyond this batch | ||
| const PAGE_SIZE = 50; | ||
|
||
| const messagesPage = await client.agents.messages.list(agentId, { | ||
| before: earliestMessage.id, | ||
| limit: PAGE_SIZE + 1, | ||
| }); | ||
|
|
||
| const olderMessages = messagesPage.items; | ||
|
|
||
| if (olderMessages.length === 0) { | ||
| setHasMoreHistory(false); | ||
| } else { | ||
| // Check if there are more messages beyond this batch | ||
| const hasMore = olderMessages.length > PAGE_SIZE; | ||
|
|
||
| // Only take PAGE_SIZE messages (drop the +1 indicator) | ||
| const messagesToAdd = hasMore | ||
| ? olderMessages.slice(0, PAGE_SIZE) | ||
| : olderMessages; | ||
|
|
||
| // Prepend older messages to the list | ||
| setMessages((prev) => [...messagesToAdd, ...prev]); | ||
|
|
||
| // Update hasMore flag | ||
| setHasMoreHistory(hasMore); | ||
| } | ||
| } catch (error) { | ||
| console.error("Error fetching earlier messages:", error); | ||
| setHasMoreHistory(false); | ||
| } finally { | ||
| setIsLoadingHistory(false); | ||
| } | ||
| }, [isLoadingHistory, hasMoreHistory, agentId, messages]); | ||
|
|
||
| // Whether a command is running (disables input but no streaming UI) | ||
| const [commandRunning, setCommandRunning] = useState(false); | ||
|
|
||
|
|
@@ -1364,6 +1473,11 @@ export default function App({ | |
| onPermissionModeChange={setUiPermissionMode} | ||
| onExit={handleExit} | ||
| onInterrupt={handleInterrupt} | ||
| messageHistory={messages} | ||
| onFetchEarlierMessages={fetchEarlierMessages} | ||
| isLoadingHistory={isLoadingHistory} | ||
| hasMoreHistory={hasMoreHistory} | ||
| isLoadingInitialHistory={isLoadingInitialHistory} | ||
| interruptRequested={interruptRequested} | ||
| /> | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constant
INITIAL_LIMIT = 200is defined inside the function. Consider moving it to the file level for consistency withPAGE_SIZEand to avoid redefinition on each render.