2025-12-03 21:48:04 -05:00
|
|
|
import { logger } from '@librechat/data-schemas';
|
2025-12-12 01:10:08 -05:00
|
|
|
import type { StandardGraph } from '@librechat/agents';
|
2025-12-14 20:59:41 -05:00
|
|
|
import type { Agents } from 'librechat-data-provider';
|
2025-12-12 02:53:40 -05:00
|
|
|
import type {
|
|
|
|
|
SerializableJobData,
|
|
|
|
|
IEventTransport,
|
2025-12-13 17:04:42 -05:00
|
|
|
AbortResult,
|
2025-12-12 02:53:40 -05:00
|
|
|
IJobStore,
|
|
|
|
|
} from './interfaces/IJobStore';
|
2025-12-12 01:10:08 -05:00
|
|
|
import type * as t from '~/types';
|
2025-12-12 02:16:24 -05:00
|
|
|
import { InMemoryEventTransport } from './implementations/InMemoryEventTransport';
|
|
|
|
|
import { InMemoryJobStore } from './implementations/InMemoryJobStore';
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Configuration options for GenerationJobManager
|
|
|
|
|
*/
|
|
|
|
|
export interface GenerationJobManagerOptions {
|
|
|
|
|
jobStore?: IJobStore;
|
|
|
|
|
eventTransport?: IEventTransport;
|
2025-12-15 09:16:06 -05:00
|
|
|
/**
|
|
|
|
|
* If true, cleans up event transport immediately when job completes.
|
|
|
|
|
* If false, keeps EventEmitters until periodic cleanup for late reconnections.
|
|
|
|
|
* Default: true (immediate cleanup to save memory)
|
|
|
|
|
*/
|
|
|
|
|
cleanupOnComplete?: boolean;
|
2025-12-14 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
/**
|
|
|
|
|
* Runtime state for active jobs - not serializable, kept in-memory per instance.
|
|
|
|
|
* Contains AbortController, ready promise, and other non-serializable state.
|
2025-12-12 02:36:44 -05:00
|
|
|
*
|
|
|
|
|
* @property abortController - Controller to abort the generation
|
|
|
|
|
* @property readyPromise - Resolves when first real subscriber connects (used to sync generation start)
|
|
|
|
|
* @property resolveReady - Function to resolve readyPromise
|
|
|
|
|
* @property finalEvent - Cached final event for late subscribers
|
|
|
|
|
* @property syncSent - Whether sync event was sent (reset when all subscribers leave)
|
|
|
|
|
* @property allSubscribersLeftHandlers - Internal handlers for disconnect events.
|
|
|
|
|
* These are stored separately from eventTransport subscribers to avoid being counted
|
|
|
|
|
* in subscriber count. This is critical: if these were registered via subscribe(),
|
|
|
|
|
* they would count as subscribers, causing isFirstSubscriber() to return false
|
|
|
|
|
* when the real client connects, which would prevent readyPromise from resolving.
|
2025-12-12 02:16:24 -05:00
|
|
|
*/
|
|
|
|
|
interface RuntimeJobState {
|
|
|
|
|
abortController: AbortController;
|
|
|
|
|
readyPromise: Promise<void>;
|
|
|
|
|
resolveReady: () => void;
|
|
|
|
|
finalEvent?: t.ServerSentEvent;
|
|
|
|
|
syncSent: boolean;
|
2025-12-12 02:36:44 -05:00
|
|
|
allSubscribersLeftHandlers?: Array<(...args: unknown[]) => void>;
|
2025-12-12 02:16:24 -05:00
|
|
|
}
|
2025-12-03 21:48:04 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manages generation jobs for resumable LLM streams.
|
2025-12-12 02:40:34 -05:00
|
|
|
*
|
2025-12-14 20:59:41 -05:00
|
|
|
* Architecture: Composes two pluggable services via dependency injection:
|
|
|
|
|
* - jobStore: Job metadata + content state (InMemory → Redis for horizontal scaling)
|
2025-12-12 02:40:34 -05:00
|
|
|
* - eventTransport: Pub/sub events (InMemory → Redis Pub/Sub for horizontal scaling)
|
2025-12-14 20:59:41 -05:00
|
|
|
*
|
|
|
|
|
* Content state is tied to jobs:
|
|
|
|
|
* - In-memory: jobStore holds WeakRef to graph for live content/run steps access
|
|
|
|
|
* - Redis: jobStore persists chunks, reconstructs content on demand
|
2025-12-12 02:40:34 -05:00
|
|
|
*
|
2025-12-12 02:53:40 -05:00
|
|
|
* All storage methods are async to support both in-memory and external stores (Redis, etc.).
|
2025-12-12 02:40:34 -05:00
|
|
|
*
|
2025-12-12 02:53:40 -05:00
|
|
|
* @example Redis injection:
|
2025-12-12 02:40:34 -05:00
|
|
|
* ```ts
|
|
|
|
|
* const manager = new GenerationJobManagerClass({
|
|
|
|
|
* jobStore: new RedisJobStore(redisClient),
|
|
|
|
|
* eventTransport: new RedisPubSubTransport(redisClient),
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
|
|
|
|
class GenerationJobManagerClass {
|
2025-12-14 20:59:41 -05:00
|
|
|
/** Job metadata + content state storage - swappable for Redis, etc. */
|
2025-12-12 02:53:40 -05:00
|
|
|
private jobStore: IJobStore;
|
2025-12-12 02:40:34 -05:00
|
|
|
/** Event pub/sub transport - swappable for Redis Pub/Sub, etc. */
|
2025-12-12 02:53:40 -05:00
|
|
|
private eventTransport: IEventTransport;
|
2025-12-12 02:16:24 -05:00
|
|
|
|
|
|
|
|
/** Runtime state - always in-memory, not serializable */
|
|
|
|
|
private runtimeState = new Map<string, RuntimeJobState>();
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
private cleanupInterval: NodeJS.Timeout | null = null;
|
2025-12-12 02:16:24 -05:00
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
/** Whether we're using Redis stores */
|
|
|
|
|
private _isRedis = false;
|
|
|
|
|
|
2025-12-15 09:16:06 -05:00
|
|
|
/** Whether to cleanup event transport immediately on job completion */
|
|
|
|
|
private _cleanupOnComplete = true;
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
constructor(options?: GenerationJobManagerOptions) {
|
|
|
|
|
this.jobStore =
|
2025-12-15 09:16:06 -05:00
|
|
|
options?.jobStore ?? new InMemoryJobStore({ ttlAfterComplete: 0, maxJobs: 1000 });
|
2025-12-14 23:45:08 -05:00
|
|
|
this.eventTransport = options?.eventTransport ?? new InMemoryEventTransport();
|
2025-12-15 09:16:06 -05:00
|
|
|
this._cleanupOnComplete = options?.cleanupOnComplete ?? true;
|
2025-12-12 02:16:24 -05:00
|
|
|
}
|
2025-12-03 21:48:04 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the job manager with periodic cleanup.
|
2025-12-14 23:45:08 -05:00
|
|
|
* Call this once at application startup.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
|
|
|
|
initialize(): void {
|
|
|
|
|
if (this.cleanupInterval) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
this.jobStore.initialize();
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
this.cleanupInterval = setInterval(() => {
|
|
|
|
|
this.cleanup();
|
|
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
|
|
if (this.cleanupInterval.unref) {
|
|
|
|
|
this.cleanupInterval.unref();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
logger.debug('[GenerationJobManager] Initialized');
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Configure the manager with custom stores.
|
|
|
|
|
* Call this BEFORE initialize() to use Redis or other stores.
|
|
|
|
|
*
|
|
|
|
|
* @example Using Redis
|
|
|
|
|
* ```ts
|
|
|
|
|
* import { createStreamServicesFromCache } from '~/stream/createStreamServices';
|
|
|
|
|
* import { cacheConfig, ioredisClient } from '~/cache';
|
|
|
|
|
*
|
|
|
|
|
* const services = createStreamServicesFromCache({ cacheConfig, ioredisClient });
|
|
|
|
|
* GenerationJobManager.configure(services);
|
|
|
|
|
* GenerationJobManager.initialize();
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
configure(services: {
|
|
|
|
|
jobStore: IJobStore;
|
|
|
|
|
eventTransport: IEventTransport;
|
|
|
|
|
isRedis?: boolean;
|
2025-12-15 09:16:06 -05:00
|
|
|
cleanupOnComplete?: boolean;
|
2025-12-14 23:45:08 -05:00
|
|
|
}): void {
|
|
|
|
|
if (this.cleanupInterval) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
'[GenerationJobManager] Reconfiguring after initialization - destroying existing services',
|
|
|
|
|
);
|
|
|
|
|
this.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.jobStore = services.jobStore;
|
|
|
|
|
this.eventTransport = services.eventTransport;
|
|
|
|
|
this._isRedis = services.isRedis ?? false;
|
2025-12-15 09:16:06 -05:00
|
|
|
this._cleanupOnComplete = services.cleanupOnComplete ?? true;
|
2025-12-14 23:45:08 -05:00
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
`[GenerationJobManager] Configured with ${this._isRedis ? 'Redis' : 'in-memory'} stores`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if using Redis stores.
|
|
|
|
|
*/
|
|
|
|
|
get isRedis(): boolean {
|
|
|
|
|
return this._isRedis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the job store instance (for advanced use cases).
|
|
|
|
|
*/
|
|
|
|
|
getJobStore(): IJobStore {
|
|
|
|
|
return this.jobStore;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
/**
|
|
|
|
|
* Create a new generation job.
|
2025-12-12 02:36:44 -05:00
|
|
|
*
|
|
|
|
|
* This sets up:
|
|
|
|
|
* 1. Serializable job data in the job store
|
|
|
|
|
* 2. Runtime state including readyPromise (resolves when first SSE client connects)
|
|
|
|
|
* 3. allSubscribersLeft callback for handling client disconnections
|
|
|
|
|
*
|
|
|
|
|
* The readyPromise mechanism ensures generation doesn't start before the client
|
|
|
|
|
* is ready to receive events. The controller awaits this promise (with a short timeout)
|
|
|
|
|
* before starting LLM generation.
|
|
|
|
|
*
|
|
|
|
|
* @param streamId - Unique identifier for this stream
|
|
|
|
|
* @param userId - User who initiated the request
|
|
|
|
|
* @param conversationId - Optional conversation ID for lookup
|
2025-12-12 02:53:40 -05:00
|
|
|
* @returns A facade object for the GenerationJob
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async createJob(
|
|
|
|
|
streamId: string,
|
|
|
|
|
userId: string,
|
|
|
|
|
conversationId?: string,
|
|
|
|
|
): Promise<t.GenerationJob> {
|
|
|
|
|
const jobData = await this.jobStore.createJob(streamId, userId, conversationId);
|
2025-12-03 21:48:04 -05:00
|
|
|
|
2025-12-12 02:36:44 -05:00
|
|
|
/**
|
|
|
|
|
* Create runtime state with readyPromise.
|
|
|
|
|
* readyPromise is resolved in subscribe() when isFirstSubscriber() returns true.
|
|
|
|
|
* This synchronizes generation start with client connection.
|
|
|
|
|
*/
|
2025-12-03 21:48:04 -05:00
|
|
|
let resolveReady: () => void;
|
|
|
|
|
const readyPromise = new Promise<void>((resolve) => {
|
|
|
|
|
resolveReady = resolve;
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime: RuntimeJobState = {
|
2025-12-03 21:48:04 -05:00
|
|
|
abortController: new AbortController(),
|
|
|
|
|
readyPromise,
|
|
|
|
|
resolveReady: resolveReady!,
|
2025-12-12 02:16:24 -05:00
|
|
|
syncSent: false,
|
2025-12-03 21:48:04 -05:00
|
|
|
};
|
2025-12-12 02:16:24 -05:00
|
|
|
this.runtimeState.set(streamId, runtime);
|
2025-12-03 21:48:04 -05:00
|
|
|
|
2025-12-12 02:36:44 -05:00
|
|
|
/**
|
|
|
|
|
* Set up all-subscribers-left callback.
|
|
|
|
|
* When all SSE clients disconnect, this:
|
|
|
|
|
* 1. Resets syncSent so reconnecting clients get sync event
|
|
|
|
|
* 2. Calls any registered allSubscribersLeft handlers (e.g., to save partial responses)
|
|
|
|
|
*/
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.onAllSubscribersLeft(streamId, () => {
|
|
|
|
|
const currentRuntime = this.runtimeState.get(streamId);
|
|
|
|
|
if (currentRuntime) {
|
|
|
|
|
currentRuntime.syncSent = false;
|
2025-12-12 02:36:44 -05:00
|
|
|
// Call registered handlers (from job.emitter.on('allSubscribersLeft', ...))
|
|
|
|
|
if (currentRuntime.allSubscribersLeftHandlers) {
|
2025-12-15 09:35:19 -05:00
|
|
|
this.jobStore
|
|
|
|
|
.getContentParts(streamId)
|
|
|
|
|
.then((content) => {
|
|
|
|
|
const parts = content ?? [];
|
|
|
|
|
for (const handler of currentRuntime.allSubscribersLeftHandlers ?? []) {
|
|
|
|
|
try {
|
|
|
|
|
handler(parts);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
logger.error(`[GenerationJobManager] Error in allSubscribersLeft handler:`, err);
|
|
|
|
|
}
|
2025-12-14 23:45:08 -05:00
|
|
|
}
|
2025-12-15 09:35:19 -05:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
logger.error(
|
|
|
|
|
`[GenerationJobManager] Failed to get content parts for allSubscribersLeft handlers:`,
|
|
|
|
|
err,
|
|
|
|
|
);
|
|
|
|
|
});
|
2025-12-12 02:36:44 -05:00
|
|
|
}
|
2025-12-12 02:16:24 -05:00
|
|
|
}
|
|
|
|
|
});
|
2025-12-03 21:48:04 -05:00
|
|
|
|
|
|
|
|
logger.debug(`[GenerationJobManager] Created job: ${streamId}`);
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
// Return facade for backwards compatibility
|
|
|
|
|
return this.buildJobFacade(streamId, jobData, runtime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:40:34 -05:00
|
|
|
* Build a GenerationJob facade from composed services.
|
|
|
|
|
*
|
|
|
|
|
* This facade provides a unified API (job.emitter, job.abortController, etc.)
|
|
|
|
|
* while internally delegating to the injected services (jobStore, eventTransport,
|
|
|
|
|
* contentState). This allows swapping implementations (e.g., Redis) without
|
|
|
|
|
* changing consumer code.
|
2025-12-12 02:36:44 -05:00
|
|
|
*
|
|
|
|
|
* IMPORTANT: The emitterProxy.on('allSubscribersLeft') handler registration
|
|
|
|
|
* does NOT use eventTransport.subscribe(). This is intentional:
|
|
|
|
|
*
|
|
|
|
|
* If we used subscribe() for internal handlers, those handlers would count
|
|
|
|
|
* as subscribers. When the real SSE client connects, isFirstSubscriber()
|
|
|
|
|
* would return false (because internal handler was "first"), and readyPromise
|
|
|
|
|
* would never resolve - causing a 5-second timeout delay before generation starts.
|
|
|
|
|
*
|
|
|
|
|
* Instead, allSubscribersLeft handlers are stored in runtime.allSubscribersLeftHandlers
|
|
|
|
|
* and called directly from the onAllSubscribersLeft callback in createJob().
|
|
|
|
|
*
|
|
|
|
|
* @param streamId - The stream identifier
|
|
|
|
|
* @param jobData - Serializable job metadata from job store
|
|
|
|
|
* @param runtime - Non-serializable runtime state (abort controller, promises, etc.)
|
|
|
|
|
* @returns A GenerationJob facade object
|
2025-12-12 02:16:24 -05:00
|
|
|
*/
|
|
|
|
|
private buildJobFacade(
|
|
|
|
|
streamId: string,
|
|
|
|
|
jobData: SerializableJobData,
|
|
|
|
|
runtime: RuntimeJobState,
|
|
|
|
|
): t.GenerationJob {
|
2025-12-12 02:36:44 -05:00
|
|
|
/**
|
|
|
|
|
* Proxy emitter that delegates to eventTransport for most operations.
|
|
|
|
|
* Exception: allSubscribersLeft handlers are stored separately to avoid
|
|
|
|
|
* incrementing subscriber count (see class JSDoc above).
|
|
|
|
|
*/
|
2025-12-12 02:16:24 -05:00
|
|
|
const emitterProxy = {
|
|
|
|
|
on: (event: string, handler: (...args: unknown[]) => void) => {
|
|
|
|
|
if (event === 'allSubscribersLeft') {
|
2025-12-12 02:36:44 -05:00
|
|
|
// Store handler for internal callback - don't use subscribe() to avoid counting as a subscriber
|
|
|
|
|
if (!runtime.allSubscribersLeftHandlers) {
|
|
|
|
|
runtime.allSubscribersLeftHandlers = [];
|
|
|
|
|
}
|
|
|
|
|
runtime.allSubscribersLeftHandlers.push(handler);
|
2025-12-12 02:16:24 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
emit: () => {
|
|
|
|
|
/* handled via eventTransport */
|
|
|
|
|
},
|
|
|
|
|
listenerCount: () => this.eventTransport.getSubscriberCount(streamId),
|
|
|
|
|
setMaxListeners: () => {
|
|
|
|
|
/* no-op for proxy */
|
|
|
|
|
},
|
|
|
|
|
removeAllListeners: () => this.eventTransport.cleanup(streamId),
|
|
|
|
|
off: () => {
|
|
|
|
|
/* handled via unsubscribe */
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
streamId,
|
|
|
|
|
emitter: emitterProxy as unknown as t.GenerationJob['emitter'],
|
|
|
|
|
status: jobData.status as t.GenerationJobStatus,
|
|
|
|
|
createdAt: jobData.createdAt,
|
|
|
|
|
completedAt: jobData.completedAt,
|
|
|
|
|
abortController: runtime.abortController,
|
|
|
|
|
error: jobData.error,
|
|
|
|
|
metadata: {
|
|
|
|
|
userId: jobData.userId,
|
|
|
|
|
conversationId: jobData.conversationId,
|
|
|
|
|
userMessage: jobData.userMessage,
|
|
|
|
|
responseMessageId: jobData.responseMessageId,
|
|
|
|
|
sender: jobData.sender,
|
|
|
|
|
},
|
|
|
|
|
readyPromise: runtime.readyPromise,
|
|
|
|
|
resolveReady: runtime.resolveReady,
|
|
|
|
|
finalEvent: runtime.finalEvent,
|
|
|
|
|
syncSent: runtime.syncSent,
|
|
|
|
|
};
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a job by streamId.
|
|
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getJob(streamId: string): Promise<t.GenerationJob | undefined> {
|
|
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
if (!jobData || !runtime) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return this.buildJobFacade(streamId, jobData, runtime);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a job exists.
|
|
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async hasJob(streamId: string): Promise<boolean> {
|
|
|
|
|
return this.jobStore.hasJob(streamId);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get job status.
|
|
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getJobStatus(streamId: string): Promise<t.GenerationJobStatus | undefined> {
|
|
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
return jobData?.status as t.GenerationJobStatus | undefined;
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mark job as complete.
|
2025-12-15 09:44:27 -05:00
|
|
|
* If cleanupOnComplete is true (default), immediately cleans up job resources.
|
|
|
|
|
* Note: eventTransport is NOT cleaned up here to allow the final event to be
|
|
|
|
|
* fully transmitted. It will be cleaned up when subscribers disconnect or
|
|
|
|
|
* by the periodic cleanup job.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:16:24 -05:00
|
|
|
async completeJob(streamId: string, error?: string): Promise<void> {
|
2025-12-15 09:16:06 -05:00
|
|
|
// Clear content state and run step buffer (Redis only)
|
2025-12-14 20:59:41 -05:00
|
|
|
this.jobStore.clearContentState(streamId);
|
2025-12-15 09:16:06 -05:00
|
|
|
this.runStepBuffers?.delete(streamId);
|
|
|
|
|
|
|
|
|
|
// Immediate cleanup if configured (default: true)
|
|
|
|
|
if (this._cleanupOnComplete) {
|
|
|
|
|
this.runtimeState.delete(streamId);
|
2025-12-15 09:44:27 -05:00
|
|
|
// Don't cleanup eventTransport here - let the done event fully transmit first.
|
|
|
|
|
// EventTransport will be cleaned up when subscribers disconnect or by periodic cleanup.
|
2025-12-15 09:16:06 -05:00
|
|
|
await this.jobStore.deleteJob(streamId);
|
|
|
|
|
} else {
|
|
|
|
|
// Only update status if keeping the job around
|
|
|
|
|
await this.jobStore.updateJob(streamId, {
|
|
|
|
|
status: error ? 'error' : 'complete',
|
|
|
|
|
completedAt: Date.now(),
|
|
|
|
|
error,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-03 21:48:04 -05:00
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
logger.debug(`[GenerationJobManager] Job completed: ${streamId}`);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abort a job (user-initiated).
|
2025-12-13 17:04:42 -05:00
|
|
|
* Returns all data needed for token spending and message saving.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-13 17:04:42 -05:00
|
|
|
async abortJob(streamId: string): Promise<AbortResult> {
|
2025-12-12 02:53:40 -05:00
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
|
|
|
|
|
if (!jobData) {
|
2025-12-11 09:52:15 -05:00
|
|
|
logger.warn(`[GenerationJobManager] Cannot abort - job not found: ${streamId}`);
|
2025-12-13 17:04:42 -05:00
|
|
|
return { success: false, jobData: null, content: [], text: '', finalEvent: null };
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
if (runtime) {
|
|
|
|
|
runtime.abortController.abort();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 09:16:06 -05:00
|
|
|
// Get content before clearing state
|
2025-12-14 23:45:08 -05:00
|
|
|
const content = (await this.jobStore.getContentParts(streamId)) ?? [];
|
2025-12-13 17:04:42 -05:00
|
|
|
const text = this.extractTextFromContent(content);
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
// Create final event for abort
|
|
|
|
|
const userMessageId = jobData.userMessage?.messageId;
|
|
|
|
|
|
|
|
|
|
const abortFinalEvent: t.ServerSentEvent = {
|
2025-12-11 09:52:15 -05:00
|
|
|
final: true,
|
2025-12-12 02:16:24 -05:00
|
|
|
conversation: { conversationId: jobData.conversationId },
|
2025-12-11 09:52:15 -05:00
|
|
|
title: 'New Chat',
|
2025-12-12 02:16:24 -05:00
|
|
|
requestMessage: jobData.userMessage
|
2025-12-11 09:52:15 -05:00
|
|
|
? {
|
2025-12-11 21:19:43 -05:00
|
|
|
messageId: userMessageId,
|
2025-12-12 02:16:24 -05:00
|
|
|
parentMessageId: jobData.userMessage.parentMessageId,
|
|
|
|
|
conversationId: jobData.conversationId,
|
|
|
|
|
text: jobData.userMessage.text ?? '',
|
2025-12-11 21:19:43 -05:00
|
|
|
isCreatedByUser: true,
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
: null,
|
|
|
|
|
responseMessage: {
|
2025-12-12 02:16:24 -05:00
|
|
|
messageId: jobData.responseMessageId ?? `${userMessageId ?? 'aborted'}_`,
|
|
|
|
|
parentMessageId: userMessageId,
|
|
|
|
|
conversationId: jobData.conversationId,
|
|
|
|
|
content,
|
2025-12-13 17:04:42 -05:00
|
|
|
text,
|
2025-12-12 02:16:24 -05:00
|
|
|
sender: jobData.sender ?? 'AI',
|
2025-12-11 09:52:15 -05:00
|
|
|
unfinished: true,
|
2025-12-11 21:19:43 -05:00
|
|
|
error: false,
|
|
|
|
|
isCreatedByUser: false,
|
2025-12-11 09:52:15 -05:00
|
|
|
},
|
|
|
|
|
aborted: true,
|
2025-12-12 01:10:08 -05:00
|
|
|
} as unknown as t.ServerSentEvent;
|
2025-12-11 09:52:15 -05:00
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
if (runtime) {
|
|
|
|
|
runtime.finalEvent = abortFinalEvent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.eventTransport.emitDone(streamId, abortFinalEvent);
|
2025-12-14 20:59:41 -05:00
|
|
|
this.jobStore.clearContentState(streamId);
|
2025-12-15 09:16:06 -05:00
|
|
|
this.runStepBuffers?.delete(streamId);
|
|
|
|
|
|
|
|
|
|
// Immediate cleanup if configured (default: true)
|
|
|
|
|
if (this._cleanupOnComplete) {
|
|
|
|
|
this.runtimeState.delete(streamId);
|
2025-12-15 09:44:27 -05:00
|
|
|
// Don't cleanup eventTransport here - let the abort event fully transmit first.
|
2025-12-15 09:16:06 -05:00
|
|
|
await this.jobStore.deleteJob(streamId);
|
|
|
|
|
} else {
|
|
|
|
|
// Only update status if keeping the job around
|
|
|
|
|
await this.jobStore.updateJob(streamId, {
|
|
|
|
|
status: 'aborted',
|
|
|
|
|
completedAt: Date.now(),
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-03 21:48:04 -05:00
|
|
|
|
|
|
|
|
logger.debug(`[GenerationJobManager] Job aborted: ${streamId}`);
|
2025-12-13 17:04:42 -05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
jobData,
|
|
|
|
|
content,
|
|
|
|
|
text,
|
|
|
|
|
finalEvent: abortFinalEvent,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract plain text from content parts array.
|
|
|
|
|
*/
|
|
|
|
|
private extractTextFromContent(content: Agents.MessageContentComplex[]): string {
|
|
|
|
|
return content
|
|
|
|
|
.map((part) => {
|
|
|
|
|
if ('text' in part && typeof part.text === 'string') {
|
|
|
|
|
return part.text;
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
})
|
|
|
|
|
.join('')
|
|
|
|
|
.trim();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Subscribe to a job's event stream.
|
2025-12-12 02:36:44 -05:00
|
|
|
*
|
|
|
|
|
* This is called when an SSE client connects to /chat/stream/:streamId.
|
|
|
|
|
* On first subscription, it resolves readyPromise to signal that generation can start.
|
|
|
|
|
*
|
|
|
|
|
* The subscriber count is critical for the readyPromise mechanism:
|
|
|
|
|
* - isFirstSubscriber() returns true when subscriber count is exactly 1
|
|
|
|
|
* - This happens when the first REAL client connects (not internal handlers)
|
|
|
|
|
* - Internal allSubscribersLeft handlers are stored separately to avoid being counted
|
|
|
|
|
*
|
|
|
|
|
* @param streamId - The stream to subscribe to
|
|
|
|
|
* @param onChunk - Handler for chunk events (streamed tokens, run steps, etc.)
|
|
|
|
|
* @param onDone - Handler for completion event (includes final message)
|
|
|
|
|
* @param onError - Handler for error events
|
|
|
|
|
* @returns Subscription object with unsubscribe function, or null if job not found
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async subscribe(
|
2025-12-03 21:48:04 -05:00
|
|
|
streamId: string,
|
2025-12-12 01:10:08 -05:00
|
|
|
onChunk: t.ChunkHandler,
|
|
|
|
|
onDone?: t.DoneHandler,
|
|
|
|
|
onError?: t.ErrorHandler,
|
2025-12-12 02:53:40 -05:00
|
|
|
): Promise<{ unsubscribe: t.UnsubscribeFn } | null> {
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
if (!runtime) {
|
2025-12-03 21:48:04 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:53:40 -05:00
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
|
|
|
|
|
// If job already complete, send final event
|
2025-12-04 08:57:13 -05:00
|
|
|
setImmediate(() => {
|
2025-12-12 02:16:24 -05:00
|
|
|
if (
|
|
|
|
|
runtime.finalEvent &&
|
|
|
|
|
jobData &&
|
|
|
|
|
['complete', 'error', 'aborted'].includes(jobData.status)
|
|
|
|
|
) {
|
|
|
|
|
onDone?.(runtime.finalEvent);
|
2025-12-04 08:57:13 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
const subscription = this.eventTransport.subscribe(streamId, {
|
|
|
|
|
onChunk: (event) => {
|
|
|
|
|
const e = event as t.ServerSentEvent;
|
|
|
|
|
// Filter out internal events
|
|
|
|
|
if (!(e as Record<string, unknown>)._internal) {
|
|
|
|
|
onChunk(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onDone: (event) => onDone?.(event as t.ServerSentEvent),
|
|
|
|
|
onError,
|
|
|
|
|
});
|
2025-12-03 21:48:04 -05:00
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
// Signal ready on first subscriber
|
2025-12-12 02:36:44 -05:00
|
|
|
const isFirst = this.eventTransport.isFirstSubscriber(streamId);
|
|
|
|
|
logger.debug(
|
|
|
|
|
`[GenerationJobManager] subscribe check: streamId=${streamId}, isFirst=${isFirst}`,
|
|
|
|
|
);
|
|
|
|
|
if (isFirst) {
|
2025-12-12 02:16:24 -05:00
|
|
|
runtime.resolveReady();
|
2025-12-12 02:36:44 -05:00
|
|
|
logger.debug(
|
|
|
|
|
`[GenerationJobManager] First subscriber ready, resolving promise for ${streamId}`,
|
|
|
|
|
);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
return subscription;
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Emit a chunk event to all subscribers.
|
2025-12-12 02:53:40 -05:00
|
|
|
* Uses runtime state check for performance (avoids async job store lookup per token).
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 01:10:08 -05:00
|
|
|
emitChunk(streamId: string, event: t.ServerSentEvent): void {
|
2025-12-12 02:53:40 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
if (!runtime || runtime.abortController.signal.aborted) {
|
2025-12-03 21:48:04 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2025-12-04 08:57:13 -05:00
|
|
|
|
2025-12-11 09:52:15 -05:00
|
|
|
// Track user message from created event
|
2025-12-12 02:16:24 -05:00
|
|
|
this.trackUserMessage(streamId, event);
|
2025-12-04 08:57:13 -05:00
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
// For Redis mode, persist chunk for later reconstruction
|
|
|
|
|
if (this._isRedis) {
|
|
|
|
|
// The SSE event structure is { event: string, data: unknown, ... }
|
|
|
|
|
// The aggregator expects { event: string, data: unknown } where data is the payload
|
|
|
|
|
const eventObj = event as Record<string, unknown>;
|
|
|
|
|
const eventType = eventObj.event as string | undefined;
|
|
|
|
|
const eventData = eventObj.data;
|
|
|
|
|
|
|
|
|
|
if (eventType && eventData !== undefined) {
|
|
|
|
|
// Store in format expected by aggregateContent: { event, data }
|
|
|
|
|
this.jobStore.appendChunk(streamId, { event: eventType, data: eventData }).catch((err) => {
|
|
|
|
|
logger.error(`[GenerationJobManager] Failed to append chunk:`, err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// For run step events, also save to run steps key for quick retrieval
|
|
|
|
|
if (eventType === 'on_run_step' || eventType === 'on_run_step_completed') {
|
|
|
|
|
this.saveRunStepFromEvent(streamId, eventData as Record<string, unknown>);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.emitChunk(streamId, event);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Extract and save run step from event data.
|
|
|
|
|
* The data is already the run step object from the event payload.
|
|
|
|
|
*/
|
|
|
|
|
private saveRunStepFromEvent(streamId: string, data: Record<string, unknown>): void {
|
|
|
|
|
// The data IS the run step object
|
|
|
|
|
const runStep = data as Agents.RunStep;
|
|
|
|
|
if (!runStep.id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fire and forget - accumulate run steps
|
|
|
|
|
this.accumulateRunStep(streamId, runStep);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-15 09:16:06 -05:00
|
|
|
* Accumulate run steps for a stream (Redis mode only).
|
2025-12-14 23:45:08 -05:00
|
|
|
* Uses a simple in-memory buffer that gets flushed to Redis.
|
2025-12-15 09:16:06 -05:00
|
|
|
* Not used in in-memory mode - run steps come from live graph via WeakRef.
|
2025-12-14 23:45:08 -05:00
|
|
|
*/
|
2025-12-15 09:16:06 -05:00
|
|
|
private runStepBuffers: Map<string, Agents.RunStep[]> | null = null;
|
2025-12-14 23:45:08 -05:00
|
|
|
|
|
|
|
|
private accumulateRunStep(streamId: string, runStep: Agents.RunStep): void {
|
2025-12-15 09:16:06 -05:00
|
|
|
// Lazy initialization - only create map when first used (Redis mode)
|
|
|
|
|
if (!this.runStepBuffers) {
|
|
|
|
|
this.runStepBuffers = new Map();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
let buffer = this.runStepBuffers.get(streamId);
|
|
|
|
|
if (!buffer) {
|
|
|
|
|
buffer = [];
|
|
|
|
|
this.runStepBuffers.set(streamId, buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update or add run step
|
|
|
|
|
const existingIdx = buffer.findIndex((rs) => rs.id === runStep.id);
|
|
|
|
|
if (existingIdx >= 0) {
|
|
|
|
|
buffer[existingIdx] = runStep;
|
|
|
|
|
} else {
|
|
|
|
|
buffer.push(runStep);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 09:16:06 -05:00
|
|
|
// Save to Redis
|
2025-12-14 23:45:08 -05:00
|
|
|
if (this.jobStore.saveRunSteps) {
|
|
|
|
|
this.jobStore.saveRunSteps(streamId, buffer).catch((err) => {
|
|
|
|
|
logger.error(`[GenerationJobManager] Failed to save run steps:`, err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 09:52:15 -05:00
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Track user message from created event.
|
2025-12-11 09:52:15 -05:00
|
|
|
*/
|
2025-12-12 02:16:24 -05:00
|
|
|
private trackUserMessage(streamId: string, event: t.ServerSentEvent): void {
|
2025-12-11 09:52:15 -05:00
|
|
|
const data = event as Record<string, unknown>;
|
|
|
|
|
if (!data.created || !data.message) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = data.message as Record<string, unknown>;
|
2025-12-12 02:16:24 -05:00
|
|
|
const updates: Partial<SerializableJobData> = {
|
|
|
|
|
userMessage: {
|
|
|
|
|
messageId: message.messageId as string,
|
|
|
|
|
parentMessageId: message.parentMessageId as string | undefined,
|
|
|
|
|
conversationId: message.conversationId as string | undefined,
|
|
|
|
|
text: message.text as string | undefined,
|
|
|
|
|
},
|
2025-12-11 09:52:15 -05:00
|
|
|
};
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
if (message.conversationId) {
|
|
|
|
|
updates.conversationId = message.conversationId as string;
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
this.jobStore.updateJob(streamId, updates);
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Update job metadata.
|
2025-12-11 09:52:15 -05:00
|
|
|
*/
|
2025-12-15 09:16:06 -05:00
|
|
|
async updateMetadata(
|
|
|
|
|
streamId: string,
|
|
|
|
|
metadata: Partial<t.GenerationJobMetadata>,
|
|
|
|
|
): Promise<void> {
|
2025-12-12 02:16:24 -05:00
|
|
|
const updates: Partial<SerializableJobData> = {};
|
|
|
|
|
if (metadata.responseMessageId) {
|
|
|
|
|
updates.responseMessageId = metadata.responseMessageId;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.sender) {
|
|
|
|
|
updates.sender = metadata.sender;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.conversationId) {
|
|
|
|
|
updates.conversationId = metadata.conversationId;
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
2025-12-12 02:16:24 -05:00
|
|
|
if (metadata.userMessage) {
|
|
|
|
|
updates.userMessage = metadata.userMessage;
|
|
|
|
|
}
|
2025-12-13 17:04:42 -05:00
|
|
|
if (metadata.endpoint) {
|
|
|
|
|
updates.endpoint = metadata.endpoint;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.iconURL) {
|
|
|
|
|
updates.iconURL = metadata.iconURL;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.model) {
|
|
|
|
|
updates.model = metadata.model;
|
|
|
|
|
}
|
|
|
|
|
if (metadata.promptTokens !== undefined) {
|
|
|
|
|
updates.promptTokens = metadata.promptTokens;
|
|
|
|
|
}
|
2025-12-15 09:16:06 -05:00
|
|
|
await this.jobStore.updateJob(streamId, updates);
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-12 01:10:08 -05:00
|
|
|
/**
|
|
|
|
|
* Set reference to the graph's contentParts array.
|
|
|
|
|
*/
|
|
|
|
|
setContentParts(streamId: string, contentParts: Agents.MessageContentComplex[]): void {
|
2025-12-12 02:53:40 -05:00
|
|
|
// Use runtime state check for performance (sync check)
|
|
|
|
|
if (!this.runtimeState.has(streamId)) {
|
2025-12-12 01:10:08 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2025-12-14 20:59:41 -05:00
|
|
|
this.jobStore.setContentParts(streamId, contentParts);
|
2025-12-12 01:10:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set reference to the graph instance.
|
|
|
|
|
*/
|
|
|
|
|
setGraph(streamId: string, graph: StandardGraph): void {
|
2025-12-12 02:53:40 -05:00
|
|
|
// Use runtime state check for performance (sync check)
|
|
|
|
|
if (!this.runtimeState.has(streamId)) {
|
2025-12-12 01:10:08 -05:00
|
|
|
return;
|
|
|
|
|
}
|
2025-12-14 20:59:41 -05:00
|
|
|
this.jobStore.setGraph(streamId, graph);
|
2025-12-12 01:10:08 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-11 09:52:15 -05:00
|
|
|
/**
|
|
|
|
|
* Get resume state for reconnecting clients.
|
|
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getResumeState(streamId: string): Promise<t.ResumeState | null> {
|
|
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
if (!jobData) {
|
2025-12-11 09:52:15 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
const aggregatedContent = (await this.jobStore.getContentParts(streamId)) ?? [];
|
|
|
|
|
const runSteps = await this.jobStore.getRunSteps(streamId);
|
2025-12-12 01:10:08 -05:00
|
|
|
|
|
|
|
|
logger.debug(`[GenerationJobManager] getResumeState:`, {
|
|
|
|
|
streamId,
|
|
|
|
|
runStepsLength: runSteps.length,
|
2025-12-15 01:21:44 -05:00
|
|
|
aggregatedContentLength: aggregatedContent.length,
|
2025-12-12 01:10:08 -05:00
|
|
|
});
|
|
|
|
|
|
2025-12-11 09:52:15 -05:00
|
|
|
return {
|
2025-12-12 01:10:08 -05:00
|
|
|
runSteps,
|
|
|
|
|
aggregatedContent,
|
2025-12-12 02:16:24 -05:00
|
|
|
userMessage: jobData.userMessage,
|
|
|
|
|
responseMessageId: jobData.responseMessageId,
|
|
|
|
|
conversationId: jobData.conversationId,
|
|
|
|
|
sender: jobData.sender,
|
2025-12-11 09:52:15 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Mark that sync has been sent.
|
2025-12-11 09:52:15 -05:00
|
|
|
*/
|
|
|
|
|
markSyncSent(streamId: string): void {
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.syncSent = true;
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Check if sync has been sent.
|
2025-12-11 09:52:15 -05:00
|
|
|
*/
|
|
|
|
|
wasSyncSent(streamId: string): boolean {
|
2025-12-12 02:16:24 -05:00
|
|
|
return this.runtimeState.get(streamId)?.syncSent ?? false;
|
2025-12-11 09:52:15 -05:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Emit a done event.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 01:10:08 -05:00
|
|
|
emitDone(streamId: string, event: t.ServerSentEvent): void {
|
2025-12-12 02:16:24 -05:00
|
|
|
const runtime = this.runtimeState.get(streamId);
|
|
|
|
|
if (runtime) {
|
|
|
|
|
runtime.finalEvent = event;
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.emitDone(streamId, event);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Emit an error event.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
|
|
|
|
emitError(streamId: string, error: string): void {
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.emitError(streamId, error);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Cleanup expired jobs.
|
2025-12-15 09:16:06 -05:00
|
|
|
* Also cleans up any orphaned runtime state, buffers, and event transport entries.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:16:24 -05:00
|
|
|
private async cleanup(): Promise<void> {
|
|
|
|
|
const count = await this.jobStore.cleanup();
|
|
|
|
|
|
|
|
|
|
// Cleanup runtime state for deleted jobs
|
|
|
|
|
for (const streamId of this.runtimeState.keys()) {
|
2025-12-12 02:53:40 -05:00
|
|
|
if (!(await this.jobStore.hasJob(streamId))) {
|
2025-12-12 02:16:24 -05:00
|
|
|
this.runtimeState.delete(streamId);
|
2025-12-15 09:16:06 -05:00
|
|
|
this.runStepBuffers?.delete(streamId);
|
2025-12-14 20:59:41 -05:00
|
|
|
this.jobStore.clearContentState(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.cleanup(streamId);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 09:16:06 -05:00
|
|
|
// Also check runStepBuffers for any orphaned entries (Redis mode only)
|
|
|
|
|
if (this.runStepBuffers) {
|
|
|
|
|
for (const streamId of this.runStepBuffers.keys()) {
|
|
|
|
|
if (!(await this.jobStore.hasJob(streamId))) {
|
|
|
|
|
this.runStepBuffers.delete(streamId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-15 09:44:27 -05:00
|
|
|
// Check eventTransport for orphaned streams (e.g., connections dropped without clean close)
|
|
|
|
|
// These are streams that exist in eventTransport but have no corresponding job
|
|
|
|
|
for (const streamId of this.eventTransport.getTrackedStreamIds()) {
|
|
|
|
|
if (!(await this.jobStore.hasJob(streamId)) && !this.runtimeState.has(streamId)) {
|
|
|
|
|
this.eventTransport.cleanup(streamId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-12 02:16:24 -05:00
|
|
|
if (count > 0) {
|
|
|
|
|
logger.debug(`[GenerationJobManager] Cleaned up ${count} expired jobs`);
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 08:57:13 -05:00
|
|
|
/**
|
|
|
|
|
* Get stream info for status endpoint.
|
|
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getStreamInfo(streamId: string): Promise<{
|
2025-12-04 08:57:13 -05:00
|
|
|
active: boolean;
|
2025-12-12 01:10:08 -05:00
|
|
|
status: t.GenerationJobStatus;
|
|
|
|
|
aggregatedContent?: Agents.MessageContentComplex[];
|
2025-12-04 08:57:13 -05:00
|
|
|
createdAt: number;
|
2025-12-12 02:53:40 -05:00
|
|
|
} | null> {
|
|
|
|
|
const jobData = await this.jobStore.getJob(streamId);
|
2025-12-12 02:16:24 -05:00
|
|
|
if (!jobData) {
|
2025-12-04 08:57:13 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:45:08 -05:00
|
|
|
const aggregatedContent = (await this.jobStore.getContentParts(streamId)) ?? [];
|
|
|
|
|
|
2025-12-04 08:57:13 -05:00
|
|
|
return {
|
2025-12-12 02:16:24 -05:00
|
|
|
active: jobData.status === 'running',
|
|
|
|
|
status: jobData.status as t.GenerationJobStatus,
|
2025-12-14 23:45:08 -05:00
|
|
|
aggregatedContent,
|
2025-12-12 02:16:24 -05:00
|
|
|
createdAt: jobData.createdAt,
|
2025-12-04 08:57:13 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Get total job count.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getJobCount(): Promise<number> {
|
2025-12-12 02:16:24 -05:00
|
|
|
return this.jobStore.getJobCount();
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Get job count by status.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async getJobCountByStatus(): Promise<Record<t.GenerationJobStatus, number>> {
|
|
|
|
|
const [running, complete, error, aborted] = await Promise.all([
|
|
|
|
|
this.jobStore.getJobCountByStatus('running'),
|
|
|
|
|
this.jobStore.getJobCountByStatus('complete'),
|
|
|
|
|
this.jobStore.getJobCountByStatus('error'),
|
|
|
|
|
this.jobStore.getJobCountByStatus('aborted'),
|
|
|
|
|
]);
|
|
|
|
|
return { running, complete, error, aborted };
|
2025-12-03 21:48:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-12 02:16:24 -05:00
|
|
|
* Destroy the manager.
|
2025-12-15 09:16:06 -05:00
|
|
|
* Cleans up all resources including runtime state, buffers, and stores.
|
2025-12-03 21:48:04 -05:00
|
|
|
*/
|
2025-12-12 02:53:40 -05:00
|
|
|
async destroy(): Promise<void> {
|
2025-12-03 21:48:04 -05:00
|
|
|
if (this.cleanupInterval) {
|
|
|
|
|
clearInterval(this.cleanupInterval);
|
|
|
|
|
this.cleanupInterval = null;
|
|
|
|
|
}
|
2025-12-12 02:16:24 -05:00
|
|
|
|
2025-12-12 02:53:40 -05:00
|
|
|
await this.jobStore.destroy();
|
2025-12-12 02:16:24 -05:00
|
|
|
this.eventTransport.destroy();
|
|
|
|
|
this.runtimeState.clear();
|
2025-12-15 09:16:06 -05:00
|
|
|
this.runStepBuffers?.clear();
|
2025-12-12 02:16:24 -05:00
|
|
|
|
2025-12-03 21:48:04 -05:00
|
|
|
logger.debug('[GenerationJobManager] Destroyed');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const GenerationJobManager = new GenerationJobManagerClass();
|
|
|
|
|
export { GenerationJobManagerClass };
|