Do a better job of not having the frontend lockup if the session doesn't exist

This commit is contained in:
perf3ct 2025-04-13 21:43:58 +00:00
parent 9a68155edc
commit e65c5ddd46
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
3 changed files with 71 additions and 53 deletions

View File

@ -28,9 +28,9 @@ export async function createChatSession(): Promise<string | null> {
*/
export async function checkSessionExists(sessionId: string): Promise<boolean> {
try {
const sessionCheck = await server.get<any>(`llm/sessions/${sessionId}`);
const sessionCheck = await server.getWithSilentNotFound<any>(`llm/sessions/${sessionId}`);
return !!(sessionCheck && sessionCheck.id);
} catch (error) {
} catch (error: any) {
console.log(`Error checking session ${sessionId}:`, error);
return false;
}

View File

@ -328,7 +328,7 @@ export default class LlmChatPanel extends BasicWidget {
// If we successfully restored a session, also fetch the latest session data
try {
const sessionData = await server.get<{
const sessionData = await server.getWithSilentNotFound<{
metadata?: {
model?: string;
provider?: string;
@ -356,6 +356,7 @@ export default class LlmChatPanel extends BasicWidget {
content?: string;
}>;
}>(`llm/sessions/${savedData.sessionId}`);
if (sessionData && sessionData.metadata) {
// Update our metadata with the latest from the server
this.metadata = {
@ -368,9 +369,18 @@ export default class LlmChatPanel extends BasicWidget {
if (sessionData.sources && sessionData.sources.length > 0) {
this.sources = sessionData.sources;
}
} else {
// Session data is missing or incomplete, create a new session
console.log(`Invalid or incomplete session data for ${savedData.sessionId}, creating a new session`);
this.sessionId = null;
await this.createChatSession();
}
} catch (fetchError) {
} catch (fetchError: any) {
// Handle fetch errors (this should now only happen for network issues, not 404s)
console.warn(`Could not fetch latest session data: ${fetchError}`);
console.log(`Creating a new session after fetch error`);
this.sessionId = null;
await this.createChatSession();
}
} else {
console.log(`Saved session ${savedData.sessionId} not found, will create new one`);

View File

@ -1604,7 +1604,15 @@ class RestChatService {
// Check if session exists
const session = sessions.get(sessionId);
if (!session) {
throw new Error(`Session with ID ${sessionId} not found`);
// Instead of throwing an error, return a structured 404 response
// that the frontend can handle gracefully
res.status(404).json({
error: true,
message: `Session with ID ${sessionId} not found`,
code: 'session_not_found',
sessionId
});
return null; // Return null to prevent further processing
}
// Return session with metadata and additional fields