refactor: Convert GenerationJobManager methods to async for improved performance

- Updated methods in GenerationJobManager and InMemoryJobStore to be asynchronous, enhancing the handling of job creation, retrieval, and management.
- Adjusted the ResumableAgentController and related routes to await job operations, ensuring proper flow and error handling.
- Increased timeout duration in ResumableAgentController's startGeneration function to 3500ms for better subscriber readiness management.
This commit is contained in:
Danny Avila 2025-12-12 02:53:40 -05:00
parent f6bdc0970a
commit 7842bcc6e0
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
5 changed files with 104 additions and 107 deletions

View file

@ -63,7 +63,7 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
}
});
const job = GenerationJobManager.createJob(streamId, userId, reqConversationId);
const job = await GenerationJobManager.createJob(streamId, userId, reqConversationId);
req._resumableStreamId = streamId;
// Track if partial response was already saved to avoid duplicates
@ -83,7 +83,7 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
return;
}
const resumeState = GenerationJobManager.getResumeState(streamId);
const resumeState = await GenerationJobManager.getResumeState(streamId);
if (!resumeState?.userMessage) {
logger.debug('[ResumableAgentController] No user message to save partial response for');
return;
@ -166,7 +166,7 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
// Start background generation - wait for subscriber with timeout fallback
const startGeneration = async () => {
try {
await Promise.race([job.readyPromise, new Promise((resolve) => setTimeout(resolve, 2500))]);
await Promise.race([job.readyPromise, new Promise((resolve) => setTimeout(resolve, 3500))]);
} catch (waitError) {
logger.warn(
`[ResumableAgentController] Error waiting for subscriber: ${waitError.message}`,