Repository navigation
improvement(mothership): load chat transcripts as windows from a stored UI projection #9002
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
38ac6a3
improvement(mothership): load chat transcripts as windows from a stor…
waleedlatif1 9acc8cf
fix(mothership): keep older chat history loading through review findings
waleedlatif1 3deb844
fix(mothership): bound chat find match allocation to the match limit
waleedlatif1 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
24 changes: 24 additions & 0 deletions
24
apps/sim/app/api/mothership/chats/[chatId]/messages/route.ts
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { listMothershipChatOlderMessagesContract } from '@/lib/api/contracts/mothership-chats' | ||
| import { | ||
| defineInternalJsonRoute, | ||
| internalJsonPresenters, | ||
| internalOrchestrationErrorPolicy, | ||
| internalRateLimits, | ||
| internalSessionAuth, | ||
| } from '@/lib/api/server/routes' | ||
| import { readChatOlderMessages } from '@/lib/mothership/chat/application/read-older-messages' | ||
|
|
||
| /** Older messages of the owner's chat, which the chat page loads behind its newest window. */ | ||
| export const GET = defineInternalJsonRoute({ | ||
| contract: listMothershipChatOlderMessagesContract, | ||
| operation: readChatOlderMessages.operation, | ||
| auth: internalSessionAuth, | ||
| rateLimit: internalRateLimits.none({ | ||
| reason: | ||
| 'Pages a transcript the chat detail read used to return whole, which has no separate rate bucket.', | ||
| }), | ||
| errorPolicy: internalOrchestrationErrorPolicy, | ||
| mapInput: ({ params, query }) => ({ chatId: params.chatId, before: query.before }), | ||
| useCase: readChatOlderMessages, | ||
| present: internalJsonPresenters.withSuccess, | ||
| }) |
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
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
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
48 changes: 48 additions & 0 deletions
48
apps/sim/app/workspace/[workspaceId]/home/hooks/use-mothership-chat-older-messages.ts
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 |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { useEffect } from 'react' | ||
| import { createLogger } from '@sim/logger' | ||
| import { toError } from '@sim/utils/errors' | ||
| import { isCancelledError, useQueryClient } from '@tanstack/react-query' | ||
| import { | ||
| loadOlderMothershipChatMessages, | ||
| type MothershipChatHistory, | ||
| mothershipChatKeys, | ||
| } from '@/hooks/queries/mothership-chats' | ||
|
|
||
| const logger = createLogger('MothershipChatOlderMessages') | ||
|
|
||
| /** | ||
| * Loads a chat's older messages behind its newest window, a page at a time, so the whole | ||
| * transcript is in place before the user scrolls back to it. Each page moves the cached | ||
| * `olderCursor`, which loads the next, until the chat's first message arrives. | ||
| * | ||
| * Writing pages into the detail entry, rather than a separate infinite query, keeps every | ||
| * reader of `detail.messages` (send, find, sources, fork) on one transcript. | ||
| */ | ||
| export function useMothershipChatOlderMessages( | ||
| chatId: string | undefined, | ||
| history: Pick<MothershipChatHistory, 'olderCursor' | 'olderMessagesFailed'> | undefined | ||
| ) { | ||
| const queryClient = useQueryClient() | ||
| const olderCursor = history?.olderMessagesFailed ? null : history?.olderCursor | ||
| useEffect(() => { | ||
| if (!chatId || !olderCursor) return | ||
| const queryKey = mothershipChatKeys.olderMessages(chatId, olderCursor) | ||
| loadOlderMothershipChatMessages(queryClient, chatId, olderCursor).catch((error) => { | ||
| if (isCancelledError(error)) return | ||
| logger.warn('Failed to load older chat messages', { | ||
| chatId, | ||
| error: toError(error).message, | ||
| }) | ||
| // The page failed for good: record it so readers waiting on older history (the Sources | ||
| // panel) settle on an error instead of loading forever. | ||
| queryClient.setQueryData<MothershipChatHistory>( | ||
| mothershipChatKeys.detail(chatId), | ||
| (current) => | ||
| current?.olderCursor === olderCursor ? { ...current, olderMessagesFailed: true } : current | ||
| ) | ||
| }) | ||
| return () => { | ||
| void queryClient.cancelQueries({ queryKey, exact: true }) | ||
| } | ||
| }, [queryClient, chatId, olderCursor]) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.