-
Notifications
You must be signed in to change notification settings - Fork 2.8k
basecamp2 - Resume chat session from server with a cache #5088
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { Session } from '../api'; | ||
| import { getApiUrl } from '../config'; | ||
|
|
||
| /** | ||
| * In-memory cache for session data | ||
| * Maps session ID to Session object | ||
| */ | ||
| const sessionCache = new Map<string, Session>(); | ||
|
|
||
| /** | ||
| * In-flight request tracking to prevent duplicate fetches | ||
| * Maps session ID to Promise of Session | ||
| */ | ||
| const inFlightRequests = new Map<string, Promise<Session>>(); | ||
|
|
||
| /** | ||
| * Load a session from the server using the /agent/resume endpoint | ||
| * Implements caching to avoid redundant fetches | ||
| * | ||
| * @param sessionId - The unique identifier for the session | ||
| * @param forceRefresh - If true, bypass cache and fetch fresh data | ||
| * @returns Promise resolving to the Session object | ||
| * @throws Error if the request fails or session not found | ||
| */ | ||
| export async function loadSession(sessionId: string, forceRefresh = false): Promise<Session> { | ||
| if (!forceRefresh && sessionCache.has(sessionId)) { | ||
| return sessionCache.get(sessionId)!; | ||
| } | ||
|
|
||
| if (inFlightRequests.has(sessionId)) { | ||
| return inFlightRequests.get(sessionId)!; | ||
| } | ||
|
|
||
| const fetchPromise = (async () => { | ||
| try { | ||
| const url = getApiUrl('/agent/resume'); | ||
| const secretKey = await window.electron.getSecretKey(); | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'X-Secret-Key': secretKey, | ||
| }, | ||
| body: JSON.stringify({ | ||
| session_id: sessionId, | ||
| }), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const errorText = await response.text().catch(() => 'Unknown error'); | ||
| throw new Error(`Failed to load session: HTTP ${response.status} - ${errorText}`); | ||
| } | ||
|
|
||
| const session: Session = await response.json(); | ||
| sessionCache.set(sessionId, session); | ||
|
|
||
| return session; | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| throw new Error(`Error loading session ${sessionId}: ${error.message}`); | ||
| } | ||
| throw new Error(`Error loading session ${sessionId}: Unknown error`); | ||
| } finally { | ||
| inFlightRequests.delete(sessionId); | ||
| } | ||
| })(); | ||
|
|
||
| inFlightRequests.set(sessionId, fetchPromise); | ||
| return fetchPromise; | ||
| } | ||
|
|
||
| /** | ||
| * Clear a specific session from the cache | ||
| * Useful when a session has been updated and needs to be refetched | ||
| * | ||
| * @param sessionId - The unique identifier for the session to clear | ||
| */ | ||
| export function clearSessionCache(sessionId: string): void { | ||
| sessionCache.delete(sessionId); | ||
| } | ||
|
|
||
| /** | ||
| * Clear all sessions from the cache | ||
| * Useful for logout or when switching contexts | ||
| */ | ||
| export function clearAllSessionCache(): void { | ||
| sessionCache.clear(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a session is currently cached | ||
| * | ||
| * @param sessionId - The unique identifier for the session | ||
| * @returns true if the session is in cache, false otherwise | ||
| */ | ||
| export function isSessionCached(sessionId: string): boolean { | ||
| return sessionCache.has(sessionId); | ||
| } | ||
|
|
||
| /** | ||
| * Get a session from cache without fetching | ||
| * Returns undefined if not cached | ||
| * | ||
| * @param sessionId - The unique identifier for the session | ||
| * @returns The cached Session object or undefined | ||
| */ | ||
| export function getCachedSession(sessionId: string): Session | undefined { | ||
| return sessionCache.get(sessionId); | ||
| } | ||
|
|
||
| /** | ||
| * Preload a session into cache | ||
| * Useful when you already have session data from another source | ||
| * | ||
| * @param session - The Session object to cache | ||
| */ | ||
| export function preloadSession(session: Session): void { | ||
| sessionCache.set(session.id, session); | ||
| } | ||
|
|
||
| /** | ||
| * Get the current cache size | ||
| * Useful for debugging and monitoring | ||
| * | ||
| * @returns The number of sessions currently cached | ||
| */ | ||
| export function getCacheSize(): number { | ||
| return sessionCache.size; | ||
| } |
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.
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.
I originally set chat to
nullbut it started having cascading effects all over the codebase setting the types toChatType | nullso opted for a less disruptive change for now and figured we can come back to it later