import type { GetEmbeddingsResponse, GetExtensionsResponse, GetI18nResponse, PostAssetsFromWorkflowResponse } from '@comfyorg/ingest-types' import { zGetEmbeddingsResponse, zGetExtensionsResponse, zPostAssetsFromWorkflowResponse } from '@comfyorg/ingest-types/zod' import axios from 'axios' import { get } from 'es-toolkit/compat' import { trimEnd } from 'es-toolkit' import { ref } from 'vue' import defaultClientFeatureFlags from '@/config/clientFeatureFlags.json' with { type: 'json' } import type { ApiAuthProvider } from '@/platform/auth/apiAuthProvider' import { anonymousApiAuthProvider } from '@/platform/auth/apiAuthProvider' import { authCredentialOf, notifyAuthCredential } from '@/platform/auth/authCredential' import { scopeMediaRoute } from '@/platform/auth/session/sessionMediaUrl' import { webSessionRequests } from '@/platform/auth/session/webSessionFetch' import { FETCH_RESPONSE_HEADERS_TIMEOUT_MS } from '@/scripts/apiTimeouts' import { getDevOverride } from '@/utils/devFeatureFlagOverride' import { getSessionOverride } from '@/utils/sessionFeatureFlagOverride' import type { ModelFile, ModelFolderInfo } from '@/platform/assets/schemas/assetSchema' import { useToast } from '@/components/ui/toast/toastStore' import { t } from '@/i18n' import { isCloud } from '@/platform/distribution/types' import { addBreadcrumb } from '@sentry/vue' import { useTelemetry } from '@comfyorg/telemetry' import { reportError } from '@comfyorg/telemetry/reportError' import type { components as ManagerComponents } from '@/workbench/extensions/manager/types/generatedManagerTypes' import type { AgentStatusWsMessage, AssetDownloadWsMessage, AssetExportWsMessage, AssetSeedCompletedWsMessage, ExecutedWsMessage, ExecutingWsMessage, ExecutionCachedWsMessage, ExecutionErrorWsMessage, ExecutionInterruptedWsMessage, ExecutionStartWsMessage, ExecutionSuccessWsMessage, FeatureFlagsWsMessage, LogsRawResponse, LogsWsMessage, NotificationWsMessage, ProgressStateWsMessage, ProgressTextWsMessage, ProgressWsMessage, StatusWsMessage, StatusWsMessageStatus } from '@/platform/remote/comfyui/execution/types' import type { PromptFailureResponse, PromptResponse, SystemStats, UserConfigResponse, UserDataFullInfo } from '@/platform/remote/comfyui/types' import type { PreviewMethod, Settings } from '@/platform/settings/types' import type { TemplateIncludeOnDistributionEnum, WorkflowTemplates } from '@/platform/workflow/templates/types/template' import type { ComfyApiWorkflow, ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema' import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId' import type { JobAssetsResult, JobDetail, JobListItem } from '@/platform/remote/comfyui/jobs/jobTypes' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { AuthCredential, AuthScheme } from '@/types/authTypes' import type { NodeExecutionId } from '@comfyorg/graph-model/nodeIdentification' import { getFetchRouteTemplate } from '@/utils/fetchRouteTemplate' import { retryingSend } from '@/utils/fetchWithRetry' import { NO_RETRY_POLICY } from '@/utils/retryPolicy' import type { RetryPolicy } from '@/utils/retryPolicy' import { fetchHistory, fetchJobAssets, fetchJobDetail, fetchQueue } from '@/platform/remote/comfyui/jobs/fetchJobs' const SERVER_FEATURE_FLAGS_TIMEOUT_MS = 5_000 interface QueuePromptRequestBody { client_id: string prompt: ComfyApiWorkflow partial_execution_targets?: NodeExecutionId[] extra_data: { extra_pnginfo: { workflow: ComfyWorkflowJSON } /** * The auth token for the comfy org account if the user is logged in. * * Backend node can access this token by specifying following input: * ```python @classmethod def INPUT_TYPES(s): return { "hidden": { "auth_token": "AUTH_TOKEN_COMFY_ORG"} } def execute(self, auth_token: str): print(f"Auth token: {auth_token}") * ``` */ auth_token_comfy_org?: string /** * The auth token for the comfy org account if the user is logged in. * * Backend node can access this token by specifying following input: * ```python * def INPUT_TYPES(s): * return { * "hidden": { "api_key": "API_KEY_COMFY_ORG" } * } * * def execute(self, api_key: str): * print(f"API Key: {api_key}") * ``` */ api_key_comfy_org?: string /** * Identifies the client submitting the prompt. Forwarded by the backend * to API nodes' upstream requests via the Comfy-Usage-Source header. */ comfy_usage_source?: string /** * Override the preview method for this prompt execution. * 'default' uses the server's CLI setting. */ preview_method?: PreviewMethod } front?: boolean number?: number } interface FetchApiOptions extends RequestInit { timeoutMs?: number | null onAuthHeader?: (attached: boolean) => void /** Reports which auth path was taken, independent of onAuthHeader's attached/not boolean. */ onAuthScheme?: (scheme: AuthScheme) => void /** Reports the kind of credential sent, to tell a missing token from a wrong one. */ onAuthCredential?: (credential: AuthCredential) => void /** Send once, for callers that already retry on their own backoff. */ skipStatusRetry?: boolean } /** * Options for queuePrompt method */ interface QueuePromptOptions { /** * Optional list of node execution IDs to execute (partial execution). * Each ID represents a node's position in nested subgraphs. * Format: Colon-separated path of node IDs (e.g., "123:456:789") */ partialExecutionTargets?: NodeExecutionId[] /** * Override the preview method for this prompt execution. * 'default' uses the server's CLI setting and is not sent to backend. */ previewMethod?: PreviewMethod } /** Dictionary of Frontend-generated API calls */ interface FrontendApiCalls { graphChanged: ComfyWorkflowJSON /** * Signals that auto-queue should treat the active workflow as changed. * Local change-tracker edits are filtered through the prompt-relevant * projection and evaluated only while Run (on change) is active. For those * edits, position, size, title, collapse, group, viewport, and slot-label * changes do not trigger this signal. * Server-pushed `graphChanged` messages are forwarded unfiltered. * Third-party presentation-only nodes are treated as prompt-relevant. */ autoQueueGraphChanged: never promptQueueing: { requestId: number; batchCount: number; number?: number } promptQueued: { number: number; batchCount: number; requestId?: number } graphCleared: never reconnecting: never reconnected: never } export type PromptQueueingEventPayload = FrontendApiCalls['promptQueueing'] export type PromptQueuedEventPayload = FrontendApiCalls['promptQueued'] /** Dictionary of calls originating from ComfyUI core */ interface BackendApiCalls { progress: ProgressWsMessage executing: ExecutingWsMessage executed: ExecutedWsMessage status: StatusWsMessage notification: NotificationWsMessage execution_start: ExecutionStartWsMessage execution_success: ExecutionSuccessWsMessage execution_error: ExecutionErrorWsMessage execution_interrupted: ExecutionInterruptedWsMessage execution_cached: ExecutionCachedWsMessage logs: LogsWsMessage /** Binary preview/progress data */ b_preview: Blob /** Binary preview with metadata (node_id, job_id) */ b_preview_with_metadata: { blob: Blob nodeId: string parentNodeId: string displayNodeId: string realNodeId: string jobId: string } progress_text: ProgressTextWsMessage progress_state: ProgressStateWsMessage feature_flags: FeatureFlagsWsMessage agent_status: AgentStatusWsMessage asset_download: AssetDownloadWsMessage asset_export: AssetExportWsMessage 'assets.seed.completed': AssetSeedCompletedWsMessage } /** Dictionary of all api calls */ interface ApiCalls extends BackendApiCalls, FrontendApiCalls { 'cm-task-started': ManagerComponents['schemas']['MessageTaskStarted'] 'cm-task-completed': ManagerComponents['schemas']['MessageTaskDone'] } /** Used to create a discriminating union on type value. */ interface ApiMessage { type: T data: ApiCalls[T] } export class UnauthorizedError extends Error {} const CLIENT_ID_STORAGE_KEY = 'clientId' let reportedClientIdStorageFailure = false function withClientIdStorage( access: (storage: Storage) => T ): T | undefined { try { return access(window.sessionStorage) } catch (cause) { if (!reportedClientIdStorageFailure) { reportedClientIdStorageFailure = true reportError(cause, { errorType: 'failure_accessing_client_id_storage', surface: 'platform', level: 'warning' }) } return undefined } } /** Ensures workers get a fair shake. */ type Unionize = T[keyof T] /** * Discriminated union of generic, i.e.: * ```ts * // Convert * type ApiMessageUnion = ApiMessage<'status' | 'executing' | ...> * // To * type ApiMessageUnion = ApiMessage<'status'> | ApiMessage<'executing'> | ... * ``` */ type ApiMessageUnion = Unionize<{ [Key in keyof ApiCalls]: ApiMessage }> /** Wraps all properties in {@link CustomEvent}. */ type AsCustomEvents = { readonly [K in keyof T]: CustomEvent } /** Handles differing event and API signatures. */ type ApiToEventType = { [K in keyof T]: K extends 'status' ? StatusWsMessageStatus : K extends 'executing' ? SerializedNodeId : T[K] } /** Dictionary of types used in the detail for a custom event */ type ApiEventTypes = ApiToEventType /** Dictionary of API events: `[name]: CustomEvent` */ type ApiEvents = AsCustomEvents /** {@link Omit} all properties that evaluate to `never`. */ type NeverNever = { [K in keyof T as T[K] extends never ? never : K]: T[K] } /** {@link Pick} only properties that evaluate to `never`. */ type PickNevers = { [K in keyof T as T[K] extends never ? K : never]: T[K] } /** Keys (names) of API events that _do not_ pass a {@link CustomEvent} `detail` object. */ type SimpleApiEvents = keyof PickNevers /** Keys (names) of API events that pass a {@link CustomEvent} `detail` object. */ type ComplexApiEvents = keyof NeverNever export type GlobalSubgraphData = { name: string info: { node_pack: string category?: string search_aliases?: string[] requiresCustomNodes?: string[] includeOnDistributions?: TemplateIncludeOnDistributionEnum[] } data: string | Promise essentials_category?: string } type WebSessionSend = (url: string, init: RequestInit) => Promise function addHeaderEntry(headers: HeadersInit, key: string, value: string) { if (Array.isArray(headers)) { headers.push([key, value]) } else if (headers instanceof Headers) { headers.set(key, value) } else { headers[key] = value } } /** EventTarget typing has no generic capability. */ export interface ComfyApi extends EventTarget { addEventListener( type: TEvent, callback: ((event: ApiEvents[TEvent]) => void) | EventListenerObject | null, options?: AddEventListenerOptions | boolean ): void removeEventListener( type: TEvent, callback: ((event: ApiEvents[TEvent]) => void) | EventListenerObject | null, options?: EventListenerOptions | boolean ): void } export class PromptExecutionError extends Error { response: PromptFailureResponse status?: number constructor(response: PromptFailureResponse, status?: number) { super('Prompt execution failed') this.response = response this.status = status } override toString() { let message = '' const error = this.response.error if (typeof error === 'string') { message += error } else if (typeof error === 'object' && error !== null) { const errorMessage = 'message' in error ? error.message : undefined const errorDetails = 'details' in error ? error.details : undefined if (typeof errorMessage === 'string') { message += errorMessage if (typeof errorDetails === 'string') { message += ': ' + errorDetails } } } if (!message && typeof this.response.message === 'string') { message += this.response.message } for (const [_, nodeError] of Object.entries( this.response.node_errors ?? [] )) { message += '\n' + nodeError.class_type + ':' for (const errorReason of nodeError.errors) { message += '\n - ' + errorReason.message if (errorReason.details) message += ': ' + errorReason.details } } return message } } interface LaunchContext { host: string base: string initialClientId: string | null } function readLaunchContext(): LaunchContext { return { host: location.host, base: isCloud ? '' : location.pathname.split('/').slice(0, -1).join('/'), initialClientId: withClientIdStorage((storage) => storage.getItem(CLIENT_ID_STORAGE_KEY) ) ?? null } } export class ComfyApi extends EventTarget { private _registered = new Set() /** * Maps an original event listener to its error-guarded wrapper, so that * {@link removeEventListener} can match the wrapper installed by * {@link addEventListener}. Keyed weakly so wrappers are GC'd with listeners. */ private _listenerWrappers = new WeakMap< EventListenerOrEventListenerObject, EventListener >() private launchContext?: LaunchContext /** * The current client id from websocket status updates. */ clientId?: string /** * The current user id. */ user: string socket: WebSocket | null = null /** * Monotonic id bumped by every createSocket() attempt. Because createSocket() * holds `this.socket === null` across its async token fetch, two attempts can * overlap (an identity reset racing the close-handler's reconnect, or two * resets in quick succession). Each attempt captures its generation and, once * its awaits settle, only proceeds if it is still the latest — so the newest * identity always wins and superseded attempts never open a leaked socket. */ private socketGeneration = 0 private socketRetryDelayMs = 1_000 private authProvider: ApiAuthProvider = anonymousApiAuthProvider reportedUnknownMessageTypes = new Set() /** * Get feature flags supported by this frontend client. * Returns a copy to prevent external modification. */ getClientFeatureFlags(): Record { return { ...defaultClientFeatureFlags } } /** * Feature flags received from the backend server. */ serverFeatureFlags = ref>({}) /** * Whether feature-flag negotiation for the current socket has settled: the * server delivered a map, or delivery was abandoned (5s timeout, or the * socket closed first). Not monotonic: each replacement socket resets it to * false, so it can flip repeatedly while a connection is reconnecting. True * does not imply the map is non-empty, and after {@link resetSocket} * {@link serverFeatureFlags} still holds the previous identity's map until * the next `feature_flags` message replaces it. */ serverFeatureFlagsSettled = ref(false) /** * The auth token for the comfy org account if the user is logged in. * This is only used for {@link queuePrompt} now. It is not directly * passed as parameter to the function because some custom nodes are hijacking * {@link queuePrompt} improperly, which causes extra parameters to be lost * in the function call chain. * * Ref: https://cs.comfy.org/search?q=context:global+%22api.queuePrompt+%3D%22&patternType=keyword&sm=0 * * TODO: Move this field to parameter of {@link queuePrompt} once all * custom nodes are patched. */ authToken?: string /** * The API key for the comfy org account if the user logged in via API key. */ apiKey?: string constructor() { super() this.user = '' } /** * Snapshots the page host, base path, and stored client id. Runs on first * use; the bootstrap calls it before routing can change the location. */ captureLaunchContext(): void { this.launchContext ??= readLaunchContext() } private get capturedLaunchContext(): LaunchContext { this.launchContext ??= readLaunchContext() return this.launchContext } get api_host(): string { return this.capturedLaunchContext.host } set api_host(host: string) { this.launchContext = { ...this.capturedLaunchContext, host } } get api_base(): string { return this.capturedLaunchContext.base } set api_base(base: string) { this.launchContext = { ...this.capturedLaunchContext, base } } /** The client id from the initial session storage. */ get initialClientId(): string | null { return this.capturedLaunchContext.initialClientId } set initialClientId(initialClientId: string | null) { this.launchContext = { ...this.capturedLaunchContext, initialClientId } } internalURL(route: string): string { return this.api_base + '/internal' + route } apiURL(route: string): string { const requests = webSessionRequests() return this.unscopedApiURL( requests ? scopeMediaRoute(route, requests.workspaceId()) : route ) } /** For requests whose headers already name the workspace. */ private unscopedApiURL(route: string): string { if (route.startsWith('/api')) return this.api_base + route return this.api_base + '/api' + route } fileURL(route: string): string { return this.api_base + route } /** * Installs the credentials source for every request and socket connection. * Called once by the composition root; requests made before that are * anonymous. */ setAuthProvider(provider: ApiAuthProvider): void { this.authProvider = provider } private async getWebSessionSend( retryPolicy?: RetryPolicy ): Promise { const requests = webSessionRequests() if (!requests) return undefined const scope = await requests.scope() return ( scope && retryingSend((url, init) => requests.send(url, init, scope), retryPolicy) ) } /** Sends a same-origin request on the web session; undefined when this tab is not on it. */ async fetchOnWebSession( url: string, init: RequestInit ): Promise { const send = await this.getWebSessionSend() return send?.(url, init) } /** * Adds today's token header, reporting the scheme that was actually used and * whether a 401 may be re-minted. * * The scheme is returned rather than assumed by the caller because this helper * is the only place that knows whether a header was obtained: it reports * `authHeader !== null` through `onAuthHeader` and attaches nothing when auth * is unavailable. A caller that announced `cloud-auth-header` on entry to this * path would misreport exactly the unauthenticated case PM-1802 is about. */ private async addCloudAuthHeader( headers: HeadersInit, onAuthHeader: FetchApiOptions['onAuthHeader'] ): Promise<{ scheme: AuthScheme credential: AuthCredential unifiedRetryOn401: boolean }> { const authHeader = await this.authProvider.getAuthHeader() onAuthHeader?.(authHeader !== null) if (!authHeader) { return { scheme: 'none', credential: 'none', unifiedRetryOn401: false } } for (const [key, value] of Object.entries(authHeader)) { addHeaderEntry(headers, key, value) } return { scheme: 'cloud-auth-header', credential: authCredentialOf(authHeader), unifiedRetryOn401: await this.authProvider.shouldRetryOn401() } } async fetchApi(route: string, options?: FetchApiOptions) { const { timeoutMs = FETCH_RESPONSE_HEADERS_TIMEOUT_MS, onAuthHeader, onAuthScheme, onAuthCredential, skipStatusRetry, ...requestOptions } = options ?? {} const retryPolicy = skipStatusRetry ? NO_RETRY_POLICY : undefined const headers: HeadersInit = requestOptions.headers ?? {} let unifiedRetryOn401 = false let sendOnWebSession: WebSessionSend | undefined if (isCloud) { await this.authProvider.waitForInitialization() sendOnWebSession = await this.getWebSessionSend(retryPolicy) if (sendOnWebSession) { onAuthHeader?.(true) onAuthScheme?.('web-session') notifyAuthCredential(onAuthCredential, 'session-cookie') } else { const cloudAuth = await this.addCloudAuthHeader(headers, onAuthHeader) unifiedRetryOn401 = cloudAuth.unifiedRetryOn401 onAuthScheme?.(cloudAuth.scheme) notifyAuthCredential(onAuthCredential, cloudAuth.credential) } } else { onAuthHeader?.(false) onAuthScheme?.('none') notifyAuthCredential(onAuthCredential, 'none') } addHeaderEntry(headers, 'Comfy-User', this.user) const timeout = timeoutMs === null ? null : { controller: new AbortController(), duration: timeoutMs } const timeoutId = timeout ? setTimeout(() => { const method = (requestOptions.method ?? 'GET').toUpperCase() const routeTemplate = getFetchRouteTemplate(route) addBreadcrumb({ category: 'fetch', message: `Timeout on ${method} ${routeTemplate}`, level: 'warning', data: { timeout_ms: timeout.duration } }) useTelemetry()?.trackFetchTimeout({ route: routeTemplate, method, timeout_ms: timeout.duration }) timeout.controller.abort( new DOMException('Fetch timeout', 'TimeoutError') ) }, timeout.duration) : undefined const signal = requestOptions.signal && timeout ? AbortSignal.any([requestOptions.signal, timeout.controller.signal]) : (requestOptions.signal ?? timeout?.controller.signal) let retryTimeoutId: ReturnType | undefined const retrySignalLifecycle = timeout ? { clearInitialTimeout: () => { if (timeoutId !== undefined) clearTimeout(timeoutId) }, createSignal: () => { const retryController = new AbortController() retryTimeoutId = setTimeout(() => { const method = (requestOptions.method ?? 'GET').toUpperCase() const routeTemplate = getFetchRouteTemplate(route) addBreadcrumb({ category: 'fetch', message: `Timeout on ${method} ${routeTemplate}`, level: 'warning', data: { timeout_ms: timeout.duration } }) useTelemetry()?.trackFetchTimeout({ route: routeTemplate, method, timeout_ms: timeout.duration }) retryController.abort( new DOMException('Fetch timeout', 'TimeoutError') ) }, timeout.duration) return requestOptions.signal ? AbortSignal.any([requestOptions.signal, retryController.signal]) : retryController.signal } } : undefined const init: RequestInit = { cache: 'no-cache', ...requestOptions, headers, signal } const response = sendOnWebSession ? sendOnWebSession(this.unscopedApiURL(route), init) : this.authProvider.fetch( this.apiURL(route), init, unifiedRetryOn401, retrySignalLifecycle, retryPolicy ) return response.finally(() => { if (timeoutId !== undefined) clearTimeout(timeoutId) if (retryTimeoutId !== undefined) clearTimeout(retryTimeoutId) }) } /** * Wraps an event listener so an exception thrown by it — most often from a * third-party custom node — is caught and logged instead of surfacing as an * unhandled error in global telemetry (which RUM captures as a high-volume, * non-actionable error). Native EventTarget already isolates listeners from * one another; this only changes where the error goes. * * The same original listener always maps to the same wrapper, so * {@link removeEventListener} still matches. Logged at `warn` level on * purpose: RUM collects `console.error` by default, which would re-introduce * the noise this guard removes. */ private wrapListener( callback: EventListenerOrEventListenerObject | null ): EventListenerOrEventListenerObject | null { if (!callback) return callback let wrapped = this._listenerWrappers.get(callback) if (!wrapped) { const logError = (event: Event, error: unknown) => console.warn( `[ComfyApi] Uncaught error in "${event.type}" event listener:`, error ) // Regular function (not arrow) so the listener keeps the native // `this === currentTarget` binding that EventTarget provides. wrapped = function (this: unknown, event: Event) { try { const result: unknown = typeof callback === 'function' ? callback.call(this, event) : callback.handleEvent(event) // Async listeners: route a rejected promise through the same guard so // it does not escape as an unhandled rejection (which RUM also logs). if ( result != null && typeof (result as PromiseLike).then === 'function' ) { void Promise.resolve(result).catch((error: unknown) => logError(event, error) ) } } catch (error) { logError(event, error) } } this._listenerWrappers.set(callback, wrapped) } return wrapped } /** * Looks up the guarded wrapper for a listener without creating one — used on * the remove path so a never-registered callback does not leave a stray * WeakMap entry. Falls back to the original callback (a harmless no-op). */ private getWrappedListener( callback: EventListenerOrEventListenerObject | null ): EventListenerOrEventListenerObject | null { if (!callback) return callback return this._listenerWrappers.get(callback) ?? callback } override addEventListener( type: TEvent, callback: ((event: ApiEvents[TEvent]) => void) | EventListenerObject | null, options?: AddEventListenerOptions | boolean ) { super.addEventListener( type, this.wrapListener(callback as EventListenerOrEventListenerObject | null), options ) this._registered.add(type) } override removeEventListener( type: TEvent, callback: ((event: ApiEvents[TEvent]) => void) | EventListenerObject | null, options?: EventListenerOptions | boolean ): void { super.removeEventListener( type, this.getWrappedListener( callback as EventListenerOrEventListenerObject | null ), options ) } addCustomEventListener( type: string, callback: ((event: CustomEvent) => void) | null, options?: AddEventListenerOptions | boolean ) { super.addEventListener( type, this.wrapListener(callback as EventListener), options ) this._registered.add(type) } removeCustomEventListener( type: string, callback: ((event: CustomEvent) => void) | null, options?: EventListenerOptions | boolean ) { super.removeEventListener( type, this.getWrappedListener(callback as EventListener), options ) } /** * Dispatches a custom event. * Provides type safety for the contravariance issue with EventTarget (last checked TS 5.6). * @param type The type of event to emit * @param detail The detail property used for a custom event ({@link CustomEventInit.detail}) */ dispatchCustomEvent(type: SimpleApiEvents): boolean dispatchCustomEvent( type: T, detail: ApiEventTypes[T] | null ): boolean dispatchCustomEvent( type: T, detail?: ApiEventTypes[T] ): boolean { const event = detail === undefined ? new CustomEvent(type) : new CustomEvent(type, { detail }) return super.dispatchEvent(event) } /** @deprecated Use {@link dispatchCustomEvent}. */ override dispatchEvent(event: never): boolean { return super.dispatchEvent(event) } /** * Poll status for colab and other things that don't support websockets. */ private _pollQueue() { setInterval(async () => { try { const resp = await this.fetchApi('/prompt') const status = (await resp.json()) as StatusWsMessageStatus this.dispatchCustomEvent('status', status) } catch { this.dispatchCustomEvent('status', null) } }, 1000) } /** False when the web session is on and no one is signed in to open it. */ private async addSocketAuth(params: URLSearchParams): Promise { const requests = webSessionRequests() const sessionScope = requests && (await requests.scope()) if (sessionScope?.workspaceId) { params.set('workspace_id', sessionScope.workspaceId) } if (sessionScope) return true const authToken = await this.authProvider.getAuthToken() if (authToken) { params.set('token', authToken) } return !requests || params.has('token') } /** The cloud socket's auth params; null when no one is signed in to open it. */ async socketAuthParams(): Promise { const params = new URLSearchParams() return (await this.addSocketAuth(params)) ? params : null } private async createSocket(isReconnect?: boolean) { if (this.socket) { return } if (!isReconnect) this.socketRetryDelayMs = 1_000 const generation = ++this.socketGeneration let opened = false const existingSession = window.name const params = new URLSearchParams() if (existingSession) { params.set('clientId', existingSession) } if (isCloud && !(await this.addSocketAuth(params))) return const sessionRequests = isCloud && !params.has('token') ? webSessionRequests() : undefined const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws' const baseUrl = `${protocol}://${this.api_host}${this.api_base}/ws` const query = params.toString() const wsUrl = query ? `${baseUrl}?${query}` : baseUrl if (generation !== this.socketGeneration) return const socket = new WebSocket(wsUrl) this.socket = socket this.serverFeatureFlagsSettled.value = false socket.binaryType = 'arraybuffer' const settleTimer = setTimeout(() => { if (this.socket === socket && !this.serverFeatureFlagsSettled.value) { this.serverFeatureFlagsSettled.value = true } }, SERVER_FEATURE_FLAGS_TIMEOUT_MS) socket.addEventListener('open', () => { if (this.socket !== socket) return opened = true this.socketRetryDelayMs = 1_000 socket.send( JSON.stringify({ type: 'feature_flags', data: this.getClientFeatureFlags() }) ) if (isReconnect) { this.dispatchCustomEvent('reconnected') } }) socket.addEventListener('error', () => { if (this.socket !== socket) return socket.close() if (!sessionRequests && !isReconnect && !opened) { this._pollQueue() } }) socket.addEventListener('close', () => { if (this.socket !== socket) return this.serverFeatureFlagsSettled.value = true clearTimeout(settleTimer) const recoverSession = sessionRequests && !opened const delayMs = recoverSession ? this.socketRetryDelayMs : 300 if (recoverSession) { this.socketRetryDelayMs = Math.min(delayMs * 2, 60_000) } setTimeout(async () => { if (this.socket !== socket) return if (recoverSession) { await sessionRequests.refreshSession() if (this.socket !== socket) return } this.socket = null await this.createSocket(true) }, delayMs) if (opened) { this.dispatchCustomEvent('status', null) this.dispatchCustomEvent('reconnecting') } }) socket.addEventListener('message', (event) => { if (this.socket !== socket) return try { if (event.data instanceof ArrayBuffer) { const view = new DataView(event.data) const eventType = view.getUint32(0) switch (eventType) { case 3: { try { const decoder3 = new TextDecoder() const rawData = event.data.slice(4) const rawView = new DataView(rawData) let offset = 0 let promptId: string | undefined if ( this.serverSupportsFeature('supports_progress_text_metadata') ) { const promptIdLength = rawView.getUint32(offset) offset += 4 promptId = decoder3.decode( rawData.slice(offset, offset + promptIdLength) ) offset += promptIdLength } const nodeIdLength = rawView.getUint32(offset) offset += 4 const nodeId = decoder3.decode( rawData.slice(offset, offset + nodeIdLength) ) offset += nodeIdLength const text = decoder3.decode(rawData.slice(offset)) this.dispatchCustomEvent('progress_text', { nodeId, text, ...(promptId !== undefined && { prompt_id: promptId }) }) } catch (e) { console.warn('Failed to parse progress_text binary message', e) } break } case 1: { const imageType = view.getUint32(4) const imageData = event.data.slice(8) const imageMime = imageType === 2 ? 'image/png' : 'image/jpeg' const imageBlob = new Blob([imageData], { type: imageMime }) this.dispatchCustomEvent('b_preview', imageBlob) break } case 4: { const decoder4 = new TextDecoder() const metadataLength = view.getUint32(4) const metadataBytes = event.data.slice(8, 8 + metadataLength) const metadata = JSON.parse(decoder4.decode(metadataBytes)) const imageData4 = event.data.slice(8 + metadataLength) const imageMime4 = metadata.image_type const imageBlob4 = new Blob([imageData4], { type: imageMime4 }) this.dispatchCustomEvent('b_preview_with_metadata', { blob: imageBlob4, nodeId: metadata.node_id, displayNodeId: metadata.display_node_id, parentNodeId: metadata.parent_node_id, realNodeId: metadata.real_node_id, jobId: metadata.prompt_id }) this.dispatchCustomEvent('b_preview', imageBlob4) break } default: console.error( `Unknown binary websocket message of type ${eventType}` ) break } } else { const msg = JSON.parse(event.data) as ApiMessageUnion switch (msg.type) { case 'status': if (msg.data.sid) { const clientId = msg.data.sid this.clientId = clientId window.name = clientId withClientIdStorage((storage) => storage.setItem(CLIENT_ID_STORAGE_KEY, clientId) ) } this.dispatchCustomEvent('status', msg.data.status ?? null) break case 'executing': this.dispatchCustomEvent( 'executing', msg.data.display_node || msg.data.node ) break case 'execution_start': case 'execution_error': case 'execution_interrupted': case 'execution_cached': case 'execution_success': case 'progress': case 'progress_state': case 'executed': case 'promptQueued': case 'logs': case 'b_preview': case 'notification': this.dispatchCustomEvent(msg.type, msg.data) break case 'graphChanged': this.dispatchCustomEvent('graphChanged', msg.data) this.dispatchCustomEvent('autoQueueGraphChanged') break case 'autoQueueGraphChanged': this.dispatchCustomEvent('autoQueueGraphChanged') break case 'feature_flags': this.serverFeatureFlags.value = msg.data this.serverFeatureFlagsSettled.value = true this.dispatchCustomEvent('feature_flags', msg.data) break case 'agent_status': this.serverFeatureFlags.value = { ...this.serverFeatureFlags.value, agent: msg.data } this.dispatchCustomEvent('agent_status', msg.data) break default: if (this._registered.has(msg.type)) { super.dispatchEvent( new CustomEvent(msg.type, { detail: msg.data }) ) } else if (!this.reportedUnknownMessageTypes.has(msg.type)) { this.reportedUnknownMessageTypes.add(msg.type) console.error(`Unknown message type ${msg.type}`) } } } } catch (error) { console.warn('Unhandled message:', event.data, error) } }) } /** * Initialises sockets and realtime updates */ async init() { await this.createSocket() } /** * Tears down the active realtime socket and reconnects, re-authenticating * with the currently active account's token. Invoked on a direct identity * change so a tab cannot keep receiving the previous account's realtime * events over a handshake that was authenticated as that account. */ async resetSocket(): Promise { this.clientId = undefined window.name = '' withClientIdStorage((storage) => storage.removeItem(CLIENT_ID_STORAGE_KEY)) const { agent: _previousAgent, ...features } = this.serverFeatureFlags.value this.serverFeatureFlags.value = features await this.replaceSocket() } /** * Re-handshakes the socket for the same account, keeping its client id, so * a new web session workspace takes effect. Does nothing before init(). */ async reconnectSocket(): Promise { if (this.socketGeneration === 0) return await this.replaceSocket() } private async replaceSocket(): Promise { const previous = this.socket // serverFeatureFlags deliberately keeps the previous map (resetSocket drops // only `agent`): clearing it would downgrade every serverSupportsFeature() // caller until the next delivery. this.serverFeatureFlagsSettled.value = false // Detach before closing so the previous socket's close handler sees it is // no longer the active socket and does not start a competing reconnect. this.socket = null if (previous && previous.readyState !== WebSocket.CLOSED) { try { previous.close() } catch { // Already-terminating socket; nothing to clean up. } } // createSocket() bumps the generation; any overlapping reset or reconnect // that is still awaiting its token will see it lost the race and bail, // leaving this the sole owner of the reconnected socket. await this.createSocket() } /** * Gets a list of extension urls */ async getExtensions(): Promise { const resp = await this.fetchApi('/extensions', { cache: 'no-store' }) return zGetExtensionsResponse.parse(await resp.json()) } /** * Gets the available workflow templates from custom nodes. * @returns A map of custom_node names and associated template workflow names. */ async getWorkflowTemplates(): Promise<{ [customNodesName: string]: string[] }> { const res = await this.fetchApi('/workflow_templates') return await res.json() } /** * Gets the index of core workflow templates. * @param locale Optional locale code (e.g., 'en', 'fr', 'zh') to load localized templates */ async getCoreWorkflowTemplates( locale?: string ): Promise { const fileName = locale && locale !== 'en' ? `index.${locale}.json` : 'index.json' try { const res = await axios.get(this.fileURL(`/templates/${fileName}`)) const contentType = String(res.headers['content-type'] ?? '') return contentType.includes('application/json') ? res.data : [] } catch (error) { // Fallback to default English version if localized version doesn't exist if (locale && locale !== 'en') { console.warn( `Localized templates for '${locale}' not found, falling back to English` ) return this.getCoreWorkflowTemplates() } console.error('Error loading core workflow templates:', error) return [] } } /** * Gets a list of embedding names * @throws When the request fails or the response does not match the schema */ async getEmbeddings(): Promise { const resp = await this.fetchApi('/embeddings', { cache: 'no-store' }) if (!resp.ok) { throw new Error(`Failed to fetch /embeddings: ${resp.status}`) } return zGetEmbeddingsResponse.parse(await resp.json()) } /** * Loads node object definitions for the graph * @returns The node definitions */ private nodeDefsPrefetch?: Promise /** Start fetching `/object_info` so the next `getNodeDefs()` can reuse it. */ prefetchNodeDefs(): void { this.nodeDefsPrefetch ??= this.fetchApi('/object_info', { cache: 'no-store' }) this.nodeDefsPrefetch.catch(() => undefined) } /** The `/object_info` response, the prefetched one when there is one, whatever its HTTP status. */ async fetchNodeDefs(): Promise { const prefetched = this.nodeDefsPrefetch this.nodeDefsPrefetch = undefined const fetchFresh = () => this.fetchApi('/object_info', { cache: 'no-store' }) return await (prefetched?.catch(fetchFresh) ?? fetchFresh()) } async getNodeDefs(): Promise> { return await (await this.fetchNodeDefs()).json() } /** * Queues a prompt to be executed * @param {number} number The index at which to queue the prompt, passing -1 will insert the prompt at the front of the queue * @param {object} data The prompt data to queue * @param {QueuePromptOptions} options Optional execution options * @throws {PromptExecutionError} If the prompt fails to execute */ async queuePrompt( number: number, data: { output: ComfyApiWorkflow; workflow: ComfyWorkflowJSON }, options?: QueuePromptOptions ): Promise { const { output: prompt, workflow } = data const body: QueuePromptRequestBody = { client_id: this.clientId ?? '', // TODO: Unify clientId access prompt, ...(options?.partialExecutionTargets && { partial_execution_targets: options.partialExecutionTargets }), extra_data: { auth_token_comfy_org: this.authToken, api_key_comfy_org: this.apiKey, comfy_usage_source: 'comfyui-frontend', extra_pnginfo: { workflow }, ...(options?.previewMethod && options.previewMethod !== 'default' && { preview_method: options.previewMethod }) } } if (number === -1) { body.front = true } else if (number != 0) { body.number = number } const res = await this.fetchApi('/prompt', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) if (res.status !== 200) { const text = await res.text() let errorResponse try { errorResponse = JSON.parse(text) } catch { errorResponse = { error: { type: 'server_error', message: `${res.status} ${res.statusText}`, details: text } } } throw new PromptExecutionError(errorResponse, res.status) } return await res.json() } /** * Gets the list of assets and models referenced by a prompt that would * need user consent before sharing. */ async getShareableAssets( prompt: ComfyApiWorkflow, options?: { owned?: boolean } ): Promise { const body: Record = { workflow_api_json: prompt } if (options?.owned !== undefined) { body.owned = options.owned } const res = await this.fetchApi('/assets/from-workflow', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }) if (res.status !== 200) { throw new Error(`Failed to fetch shareable assets: ${res.status}`) } const data = await res.json() return zPostAssetsFromWorkflowResponse.parse(data) } /** * Gets a list of model folder keys (eg ['checkpoints', 'loras', ...]) * @returns The list of model folder keys */ async getModelFolders(): Promise { const res = await this.fetchApi(`/experiment/models`) if (res.status === 404) { return [] } const folderBlacklist = ['configs', 'custom_nodes'] return (await res.json()).filter( (folder: ModelFolderInfo) => !folderBlacklist.includes(folder.name) ) } /** * Gets a list of models in the specified folder * @param {string} folder The folder to list models from, such as 'checkpoints' * @returns The list of model filenames within the specified folder */ async getModels(folder: string): Promise { const res = await this.fetchApi(`/experiment/models/${folder}`) if (res.status === 404) { return [] } return await res.json() } /** * Gets the metadata for a model * @param {string} folder The folder containing the model * @param {string} model The model to get metadata for * @returns The metadata for the model */ async viewMetadata(folder: string, model: string) { const res = await this.fetchApi( `/view_metadata/${folder}?filename=${encodeURIComponent(model)}` ) const rawResponse = await res.text() if (!rawResponse) { return null } try { return JSON.parse(rawResponse) } catch (error) { console.error( 'Error viewing metadata', res.status, res.statusText, rawResponse, error ) return null } } /** * Loads a list of items (queue or history) * @param {string} type The type of items to load, queue or history * @returns The items of the specified type grouped by their status */ async getItems(type: 'queue' | 'history') { if (type === 'queue') { return this.getQueue() } return this.getHistory() } /** * Gets the current state of the queue * @returns The currently running and queued items */ async getQueue(options?: { throwOnError?: boolean }): Promise<{ Running: JobListItem[] Pending: JobListItem[] }> { try { return await fetchQueue(this.fetchApi.bind(this), options) } catch (error) { if (options?.throwOnError) throw error console.error('Failed to fetch queue:', error) return { Running: [], Pending: [] } } } /** * Gets the prompt execution history * @returns Prompt history including node outputs */ async getHistory( max_items: number = 200, options?: { offset?: number } ): Promise { try { return await fetchHistory( this.fetchApi.bind(this), max_items, options?.offset ) } catch (error) { console.error(error) return [] } } /** * Gets detailed job info including outputs and workflow * @param jobId The job ID * @returns Full job details or undefined if not found */ async getJobDetail( jobId: string, signal?: AbortSignal ): Promise { return fetchJobDetail(this.fetchApi.bind(this), jobId, signal) } /** * Gets a job's output assets, each resolved to a real asset entity with * per-output node context. Returns an empty list when the endpoint is * unavailable (e.g. non-cloud distributions). * @param jobId The job ID * @returns The job's output assets and whether the list is exhaustive */ async getJobAssets(jobId: string): Promise { return fetchJobAssets(this.fetchApi.bind(this), jobId) } /** * Gets system & device stats * @returns System stats such as python version, OS, per device info */ async getSystemStats(): Promise { const res = await this.fetchApi('/system_stats') return await res.json() } /** * Sends a POST request to the API * @param {*} type The endpoint to post to * @param {*} body Optional POST data */ private async _postItem(type: string, body?: Record) { try { await this.fetchApi('/' + type, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined }) } catch (error) { console.error(error) } } /** * Deletes an item from the specified list * @param {string} type The type of item to delete, queue or history * @param {number} id The id of the item to delete */ async deleteItem(type: string, id: string) { await this._postItem(type, { delete: [id] }) } /** * Clears the specified list * @param {string} type The type of list to clear, queue or history */ async clearItems(type: string) { await this._postItem(type, { clear: true }) } /** * Interrupts the execution of the running job. If runningJobId is provided, * it is included in the payload as a helpful hint to the backend. * @param {string | null} [runningJobId] Optional Running Job ID to interrupt */ async interrupt(runningJobId: string | null) { await this._postItem( 'interrupt', runningJobId ? { prompt_id: runningJobId } : undefined ) } /** * Cancels a single job by id via `POST /api/jobs/{job_id}/cancel` (idempotent: * already-terminal jobs are a no-op). Requires runtime parity — not every * runtime exposes this endpoint yet; do not merge callers before parity lands. * * @param {string} jobId The id of the job to cancel */ async cancelJob(jobId: string) { const res = await this.fetchApi( `/jobs/${encodeURIComponent(jobId)}/cancel`, { method: 'POST' } ) if (!res.ok) { const body = await res.text().catch(() => '') throw new Error( `Failed to cancel job ${jobId}: ${res.status}${body ? ` — ${body}` : ''}` ) } } /** * Cancels multiple jobs in a single request via `POST /api/jobs/cancel` with * body `{ job_ids: [...] }`. Already-terminal jobs are no-ops. Same runtime * parity requirement as {@link cancelJob}. * * @param {string[]} jobIds The ids of the jobs to cancel */ async cancelJobs(jobIds: string[]) { if (!jobIds.length) return const res = await this.fetchApi('/jobs/cancel', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ job_ids: jobIds }) }) if (!res.ok) { const body = await res.text().catch(() => '') throw new Error( `Failed to cancel jobs: ${res.status}${body ? ` — ${body}` : ''}` ) } } /** * Gets user configuration data and where data should be stored */ async getUserConfig(): Promise { return (await this.fetchApi('/users')).json() } /** * Creates a new user * @param { string } username * @returns The fetch response */ createUser(username: string) { return this.fetchApi('/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username }) }) } /** * Gets all setting values for the current user * @returns { Promise } A dictionary of id -> value */ async getSettings( options?: Pick ): Promise { const resp = await this.fetchApi('/settings', options) if (resp.status == 401) { // `statusText` is ALWAYS empty over HTTP/2 — the protocol carries no // reason phrase — and cloud.comfy.org is HTTP/2. Passing it straight // through produced `new UnauthorizedError('')`, which the global // onerror handler reported to Sentry as the untitled group // "Error: No error message": 165,890 events in 14 days across 25,417 // users, all of them unactionable because nothing in the event said // which request had failed. throw new UnauthorizedError( `Failed to load settings: 401 ${resp.statusText || 'Unauthorized'}` ) } return await resp.json() } /** * Gets a setting for the current user * @param { string } id The id of the setting to fetch * @returns { Promise } The setting value */ async getSetting(id: keyof Settings): Promise { return (await this.fetchApi(`/settings/${encodeURIComponent(id)}`)).json() } /** * Stores a dictionary of settings for the current user */ async storeSettings(settings: Partial) { return this.fetchApi(`/settings`, { method: 'POST', body: JSON.stringify(settings) }) } /** * Stores a setting for the current user */ async storeSetting( id: keyof Settings, value: Settings[keyof Settings], options?: Pick ) { return this.fetchApi(`/settings/${encodeURIComponent(id)}`, { method: 'POST', body: JSON.stringify(value), ...options }) } /** * Gets a user data file for the current user */ async getUserData(file: string, options?: RequestInit) { return this.fetchApi(`/userdata/${encodeURIComponent(file)}`, options) } /** * Stores a user data file for the current user * @param { string } file The name of the userdata file to save * @param { unknown } data The data to save to the file * @param { RequestInit & { stringify?: boolean, throwOnError?: boolean } } [options] * @returns { Promise } */ async storeUserData( file: string, data: unknown, options: RequestInit & { overwrite?: boolean stringify?: boolean throwOnError?: boolean full_info?: boolean } = { overwrite: true, stringify: true, throwOnError: true, full_info: false } ): Promise { const overwrite = options.overwrite ?? true const fullInfo = options.full_info ?? false const route = `/userdata/${encodeURIComponent(file)}?overwrite=${overwrite}&full_info=${fullInfo}` const resp = await this.fetchApi(route, { method: 'POST', body: options.stringify ? JSON.stringify(data) : (data as BodyInit), ...options }) if (resp.status !== 200) { const message = `Error storing user data file '${file}': ${resp.status} ${resp.statusText}` // The reported message and route are file-independent: the literal file // name (and `route`, which embeds it) is unbounded cardinality and // would fragment Sentry grouping into one bucket per file instead of // one for this failure mode — exactly what buried PM-1906's alerts. reportError( new Error( `Error storing user data file: ${resp.status} ${resp.statusText}` ), { errorType: 'userdata_store_failure', surface: 'platform', tags: { status: resp.status, overwrite, full_info: fullInfo }, context: { file, route: `/userdata/{file}?overwrite=${overwrite}&full_info=${fullInfo}` } } ) if (options.throwOnError !== false) { throw new Error(message) } } return resp } /** * Deletes a user data file for the current user * @param { string } file The name of the userdata file to delete */ async deleteUserData(file: string) { const resp = await this.fetchApi(`/userdata/${encodeURIComponent(file)}`, { method: 'DELETE' }) return resp } /** * Move a user data file for the current user * @param { string } source The userdata file to move * @param { string } dest The destination for the file */ async moveUserData( source: string, dest: string, options = { overwrite: false } ) { const resp = await this.fetchApi( `/userdata/${encodeURIComponent(source)}/move/${encodeURIComponent(dest)}?overwrite=${options.overwrite}`, { method: 'POST' } ) return resp } async listUserDataFullInfo(dir: string): Promise { const trimmedDir = trimEnd(dir, '/') const resp = await this.fetchApi( `/userdata?dir=${encodeURIComponent(trimmedDir)}&recurse=true&split=false&full_info=true` ) if (resp.status === 404) return [] if (resp.status !== 200) { throw new Error( `Error getting user data list '${trimmedDir}': ${resp.status} ${resp.statusText}` ) } return resp.json() } async getGlobalSubgraphData(id: string): Promise { const resp = await api.fetchApi('/global_subgraphs/' + id) if (resp.status !== 200) { throw new Error( `Failed to fetch global subgraph '${id}': ${resp.status} ${resp.statusText}` ) } const subgraph: unknown = await resp.json() if ( typeof subgraph !== 'object' || subgraph === null || !('data' in subgraph) || typeof subgraph.data !== 'string' || !subgraph.data ) { throw new Error(`Global subgraph '${id}' returned empty data`) } return subgraph.data } async getGlobalSubgraphs(): Promise> { const resp = await api.fetchApi('/global_subgraphs') if (resp.status !== 200) return {} const subgraphs: Record = await resp.json() for (const [k, v] of Object.entries(subgraphs)) { if (!v.data) v.data = this.getGlobalSubgraphData(k) } return subgraphs } async getLogs(): Promise { const url = isCloud ? this.apiURL('/logs') : this.internalURL('/logs') const { data } = await axios.get(url) if (typeof data === 'string') return data return data === undefined ? '' : JSON.stringify(data, null, 2) } async getRawLogs(): Promise { const url = isCloud ? this.apiURL('/logs/raw') : this.internalURL('/logs/raw') return (await axios.get(url)).data } async subscribeLogs(enabled: boolean): Promise { const url = isCloud ? this.apiURL('/logs/subscribe') : this.internalURL('/logs/subscribe') return await axios.patch(url, { enabled, clientId: this.clientId }) } async getFolderPaths(): Promise> { const response = await axios .get(this.internalURL('/folder_paths')) .catch(() => null) if (!response) { return {} // Fallback: no filesystem paths known when API unavailable } return response.data } /* Frees memory by unloading models and optionally freeing execution cache * @param {Object} options - The options object * @param {boolean} options.freeExecutionCache - If true, also frees execution cache */ async freeMemory(options: { freeExecutionCache: boolean }) { const toast = useToast() try { let mode = '' if (options.freeExecutionCache) { mode = '{"unload_models": true, "free_memory": true}' } else { mode = '{"unload_models": true}' } const res = await this.fetchApi(`/free`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: mode }) if (res.status === 200) { toast.success( t( options.freeExecutionCache ? 'toastMessages.modelsAndCacheCleared' : 'toastMessages.modelsUnloaded' ), { duration: 3000 } ) } else { toast.error(t('toastMessages.unloadModelsFailed')) } } catch { toast.error(t('toastMessages.unloadModelsError')) } } /** * Gets the custom nodes i18n data from the server. * * @returns The custom nodes i18n data */ async getCustomNodesI18n(): Promise { return (await axios.get(this.apiURL('/i18n'))).data } /** * Checks if the server supports a specific feature. * @param featureName The name of the feature to check (supports dot notation for nested values) * @returns true if the feature is supported, false otherwise */ serverSupportsFeature(featureName: string): boolean { const sessionOverride = getSessionOverride(featureName) if (sessionOverride !== undefined) return sessionOverride === true const override = getDevOverride(featureName) if (override !== undefined) return override return get(this.serverFeatureFlags.value, featureName) === true } /** * Gets a server feature flag value. * @param featureName The name of the feature to get (supports dot notation for nested values) * @param defaultValue The default value if the feature is not found * @returns The feature value or default */ getServerFeature(featureName: string, defaultValue?: T): T { const sessionOverride = getSessionOverride(featureName) if (sessionOverride !== undefined) return sessionOverride const override = getDevOverride(featureName) if (override !== undefined) return override return get(this.serverFeatureFlags.value, featureName, defaultValue) as T } /** * Gets all server feature flags. * @returns Copy of all server feature flags */ getServerFeatures(): Record { return { ...this.serverFeatureFlags.value } } } export const api = new ComfyApi()