LCOV - code coverage report
Current view: top level - src/services - jobOutputCache.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 69.4 % 49 34
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : /**
       2              :  * @fileoverview Job output cache for caching and managing job data
       3              :  * @module services/jobOutputCache
       4              :  *
       5              :  * Centralizes job output and detail caching with LRU eviction.
       6              :  * Provides helpers for working with previewable outputs and workflows.
       7              :  */
       8              : 
       9              : import QuickLRU from '@alloc/quick-lru'
      10              : 
      11              : import type { JobDetail, JobOutputAsset } from '@comfyorg/api-client/jobTypes'
      12              : import {
      13              :   extractApiPrompt,
      14              :   extractWorkflow
      15              : } from '@comfyorg/api-client/fetchJobs'
      16              : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
      17              : import type { TaskOutput } from '@comfyorg/api-client/executionTypes'
      18              : import { api } from '@/scripts/api'
      19              : import type { AugmentedResultItem } from '@/utils/resultItem'
      20              : import { findResultIndexByUrl } from '@/utils/resultItemUrl'
      21              : import { filterPreviewableResults } from '@/utils/resultItem'
      22              : import { parseTaskOutput } from '@/stores/resultItemParsing'
      23              : 
      24              : /** The part of `TaskItemImpl` this cache reads; `queueStore` imports this module. */
      25              : interface OutputsTask {
      26              :   readonly jobId: string
      27              :   readonly outputsCount: number | undefined
      28              :   readonly previewableOutputs: readonly AugmentedResultItem[]
      29              :   loadFullOutputs(): Promise<OutputsTask>
      30              : }
      31              : 
      32         2356 : const MAX_TASK_CACHE_SIZE = 50
      33         2356 : const MAX_JOB_DETAIL_CACHE_SIZE = 50
      34         2356 : const MAX_JOB_ASSETS_CACHE_SIZE = 50
      35              : 
      36         2356 : const taskCache = new QuickLRU<string, OutputsTask>({
      37              :   maxSize: MAX_TASK_CACHE_SIZE
      38              : })
      39         2356 : const jobDetailCache = new QuickLRU<string, JobDetail>({
      40              :   maxSize: MAX_JOB_DETAIL_CACHE_SIZE
      41              : })
      42         2356 : const jobAssetsCache = new QuickLRU<string, JobOutputAsset[]>({
      43              :   maxSize: MAX_JOB_ASSETS_CACHE_SIZE
      44              : })
      45         2356 : const inFlightJobDetails = new Map<string, Promise<JobDetail | undefined>>()
      46              : const inFlightJobAssets = new Map<string, Promise<JobOutputAsset[]>>()
      47              : 
      48              : // Track latest request to dedupe stale responses
      49         2356 : let latestTaskRequestId: string | null = null
      50              : 
      51              : // ===== Task Output Caching =====
      52              : 
      53            0 : export function findActiveIndex(
      54              :   items: readonly AugmentedResultItem[],
      55              :   url?: string
      56              : ): number {
      57            0 :   return findResultIndexByUrl(items, url)
      58              : }
      59              : 
      60              : /**
      61              :  * Gets previewable outputs for a task, with lazy loading, caching, and request deduping.
      62              :  * Returns null if a newer request superseded this one while loading.
      63              :  */
      64            1 : export async function getOutputsForTask(
      65              :   task: OutputsTask
      66              : ): Promise<AugmentedResultItem[] | null> {
      67            1 :   const requestId = task.jobId
      68            1 :   latestTaskRequestId = requestId
      69              : 
      70              :   const outputsCount = task.outputsCount ?? 0
      71              :   const needsLazyLoad = outputsCount > 1
      72              : 
      73            1 :   if (!needsLazyLoad) {
      74            1 :     return [...task.previewableOutputs]
      75              :   }
      76              : 
      77            0 :   const cached = taskCache.get(requestId)
      78            0 :   if (cached) {
      79            0 :     return [...cached.previewableOutputs]
      80              :   }
      81              : 
      82            0 :   try {
      83            0 :     const loadedTask = await task.loadFullOutputs()
      84              : 
      85              :     // Check if request was superseded while loading
      86            0 :     if (latestTaskRequestId !== requestId) {
      87            0 :       return null
      88              :     }
      89              : 
      90            0 :     taskCache.set(requestId, loadedTask)
      91            0 :     return [...loadedTask.previewableOutputs]
      92              :   } catch (error) {
      93            0 :     if (latestTaskRequestId !== requestId) return null
      94            0 :     console.warn('Failed to load full outputs, using preview:', error)
      95            0 :     return [...task.previewableOutputs]
      96              :   }
      97              : }
      98              : 
      99         1357 : function getPreviewableOutputs(outputs?: TaskOutput): AugmentedResultItem[] {
     100         1357 :   if (!outputs) return []
     101         1320 :   return filterPreviewableResults(parseTaskOutput(outputs))
     102              : }
     103              : 
     104         1357 : export function getPreviewableOutputsFromJobDetail(
     105              :   jobDetail?: JobDetail
     106              : ): AugmentedResultItem[] {
     107         1357 :   return getPreviewableOutputs(jobDetail?.outputs)
     108              : }
     109              : 
     110              : // ===== Job Detail Caching =====
     111              : 
     112         1428 : export async function getJobDetail(
     113              :   jobId: string
     114              : ): Promise<JobDetail | undefined> {
     115         1428 :   const cached = jobDetailCache.get(jobId)
     116         1428 :   if (cached) return cached
     117              : 
     118          742 :   const inFlight = inFlightJobDetails.get(jobId)
     119         1428 :   if (inFlight) return inFlight
     120              : 
     121          708 :   const request = api
     122              :     .getJobDetail(jobId)
     123              :     .then((detail) => {
     124          704 :       if (detail) jobDetailCache.set(jobId, detail)
     125          704 :       return detail
     126              :     })
     127              :     .catch((error) => {
     128            0 :       console.warn('Failed to fetch job detail:', error)
     129              :       return undefined
     130              :     })
     131              :     .finally(() => inFlightJobDetails.delete(jobId))
     132          708 :   inFlightJobDetails.set(jobId, request)
     133          708 :   return request
     134              : }
     135              : 
     136              : /**
     137              :  * Gets a job's output assets with LRU caching and in-flight request dedupe,
     138              :  * so N concurrent resolutions of the same job issue one network request.
     139              :  * Only complete, non-empty results are cached: an empty list usually means the
     140              :  * endpoint is unavailable or the assets are not yet persisted, and a truncated
     141              :  * one means a page failed mid-pagination. Caching either would pin an
     142              :  * under-resolved set until LRU eviction, long after the endpoint recovered.
     143              :  */
     144              : export async function getJobAssets(jobId: string): Promise<JobOutputAsset[]> {
     145              :   const cached = jobAssetsCache.get(jobId)
     146              :   if (cached) return cached
     147              : 
     148              :   const inFlight = inFlightJobAssets.get(jobId)
     149              :   if (inFlight) return inFlight
     150              : 
     151              :   const request = api
     152              :     .getJobAssets(jobId)
     153              :     .then(({ assets, complete }) => {
     154              :       if (complete && assets.length) jobAssetsCache.set(jobId, assets)
     155              :       return assets
     156              :     })
     157              :     .finally(() => inFlightJobAssets.delete(jobId))
     158              :   inFlightJobAssets.set(jobId, request)
     159              :   return request
     160              : }
     161              : 
     162            4 : export async function getJobWorkflow(
     163              :   jobId: string
     164              : ): Promise<ComfyWorkflowJSON | undefined> {
     165            4 :   const detail = await getJobDetail(jobId)
     166            4 :   return await extractWorkflow(detail)
     167              : }
     168              : 
     169              : /**
     170              :  * The API-format graph a job stores, available only for jobs that embed no
     171              :  * editor workflow. Returned unvalidated — see `extractApiPrompt`.
     172              :  */
     173            1 : export async function getJobApiPrompt(jobId: string): Promise<unknown> {
     174            1 :   const detail = await getJobDetail(jobId)
     175            1 :   return extractApiPrompt(detail)
     176              : }
        

Generated by: LCOV version 2.0-1