Line data Source code
1 : /**
2 : * @fileoverview Jobs API Fetchers
3 : * @module platform/remote/comfyui/jobs/fetchJobs
4 : *
5 : * Unified jobs API fetcher for history, queue, and job details.
6 : * All distributions use the /jobs endpoint.
7 : */
8 :
9 : import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema'
10 : import { validateComfyWorkflow } from '@comfyorg/graph-model/workflowSchema'
11 : import type { JobId } from './executionTypes'
12 :
13 : import type {
14 : JobAssetsResult,
15 : JobDetail,
16 : JobListItem,
17 : JobOutputAsset,
18 : JobStatus,
19 : RawJobListItem
20 : } from './jobTypes'
21 : import {
22 : zJobAssetsResponse,
23 : zJobDetail,
24 : zJobsListResponse,
25 : zWorkflowContainer
26 : } from './jobTypes'
27 :
28 : interface FetchJobsRawResult {
29 : jobs: RawJobListItem[]
30 : total: number
31 : offset: number
32 : limit: number
33 : hasMore: boolean
34 : }
35 :
36 2356 : class JobsApiError extends Error {}
37 :
38 : interface FetchJobsOptions {
39 : throwOnError?: boolean
40 : }
41 :
42 : export interface FetchHistoryPageResult {
43 : jobs: JobListItem[]
44 : total: number
45 : offset: number
46 : limit: number
47 : hasMore: boolean
48 : }
49 :
50 : /**
51 : * Fetches raw jobs from /jobs endpoint
52 : * @internal
53 : */
54 10278 : async function fetchJobsRaw(
55 : fetchApi: (url: string) => Promise<Response>,
56 : statuses: JobStatus[],
57 : maxItems: number = 200,
58 : offset: number = 0,
59 : options?: FetchJobsOptions
60 : ): Promise<FetchJobsRawResult> {
61 : const statusParam = statuses.join(',')
62 10278 : const url = `/jobs?status=${statusParam}&limit=${maxItems}&offset=${offset}`
63 10278 : try {
64 10278 : const res = await fetchApi(url)
65 10278 : if (!res.ok) {
66 1 : throw new JobsApiError(`Failed to fetch jobs: ${res.status}`)
67 : }
68 10181 : const data = zJobsListResponse.parse(await res.json())
69 10175 : return {
70 : jobs: data.jobs,
71 : total: data.pagination.total,
72 : offset: data.pagination.offset,
73 : limit: data.pagination.limit,
74 : hasMore: data.pagination.has_more
75 : }
76 : } catch (error) {
77 1 : if (options?.throwOnError) throw error
78 0 : console.error('[Jobs API] Error fetching jobs:', error)
79 0 : return { jobs: [], total: 0, offset, limit: maxItems, hasMore: false }
80 : }
81 : }
82 :
83 : // Large offset to ensure running/pending jobs sort above history
84 2356 : const QUEUE_PRIORITY_BASE = 1_000_000
85 :
86 : /**
87 : * Assigns synthetic priority to jobs.
88 : * Only assigns if job doesn't already have a server-provided priority.
89 : */
90 12757 : function assignPriority(
91 : jobs: RawJobListItem[],
92 : basePriority: number
93 : ): JobListItem[] {
94 12757 : return jobs.map((job, index) => ({
95 : ...job,
96 : priority: job.priority ?? basePriority - index
97 : }))
98 : }
99 :
100 : /**
101 : * Fetches history (terminal state jobs: completed, failed, cancelled)
102 : * Assigns synthetic priority starting from total (lower than queue jobs).
103 : */
104 7660 : export async function fetchHistory(
105 : fetchApi: (url: string) => Promise<Response>,
106 : maxItems: number = 200,
107 : offset: number = 0,
108 : options?: FetchJobsOptions
109 : ): Promise<JobListItem[]> {
110 7660 : const { jobs } = await fetchHistoryPage(fetchApi, maxItems, offset, options)
111 7593 : return jobs
112 : }
113 :
114 : /**
115 : * Fetches one page of history with server-provided pagination metadata.
116 : */
117 7660 : export async function fetchHistoryPage(
118 : fetchApi: (url: string) => Promise<Response>,
119 : maxItems: number = 200,
120 : offset: number = 0,
121 : options?: FetchJobsOptions
122 : ): Promise<FetchHistoryPageResult> {
123 7660 : const result = await fetchJobsRaw(
124 : fetchApi,
125 : ['completed', 'failed', 'cancelled'],
126 : maxItems,
127 : offset,
128 : options
129 : )
130 :
131 7593 : return {
132 : jobs: assignPriority(result.jobs, result.total - result.offset),
133 : total: result.total,
134 : offset: result.offset,
135 : limit: result.limit,
136 : hasMore: result.hasMore
137 : }
138 : }
139 :
140 : /**
141 : * Fetches queue (in_progress + pending jobs)
142 : * Pending jobs get highest priority, then running jobs.
143 : */
144 2618 : export async function fetchQueue(
145 : fetchApi: (url: string) => Promise<Response>,
146 : options?: FetchJobsOptions
147 : ): Promise<{ Running: JobListItem[]; Pending: JobListItem[] }> {
148 2618 : const { jobs } = await fetchJobsRaw(
149 : fetchApi,
150 : ['in_progress', 'pending'],
151 : 200,
152 : 0,
153 : options
154 : )
155 :
156 2582 : const running = jobs.filter((j) => j.status === 'in_progress')
157 2582 : const pending = jobs.filter((j) => j.status === 'pending')
158 :
159 : // Pending gets highest priority, then running
160 : // Both are above any history job due to QUEUE_PRIORITY_BASE
161 2582 : return {
162 : Running: assignPriority(running, QUEUE_PRIORITY_BASE + running.length),
163 : Pending: assignPriority(
164 : pending,
165 : QUEUE_PRIORITY_BASE + running.length + pending.length
166 : )
167 : }
168 : }
169 :
170 : /**
171 : * Fetches full job details from /jobs/{job_id}
172 : */
173 724 : export async function fetchJobDetail(
174 : fetchApi: (url: string, init?: RequestInit) => Promise<Response>,
175 : jobId: JobId,
176 : signal?: AbortSignal
177 : ): Promise<JobDetail | undefined> {
178 724 : try {
179 724 : const res = await fetchApi(`/jobs/${encodeURIComponent(jobId)}`, {
180 : signal
181 : })
182 :
183 720 : if (!res.ok) {
184 111 : console.warn(`Job not found for job ${jobId}`)
185 111 : return undefined
186 : }
187 :
188 609 : return zJobDetail.parse(await res.json())
189 : } catch (error) {
190 0 : if (signal?.aborted) return undefined
191 0 : console.error(`Failed to fetch job detail for job ${jobId}:`, error)
192 0 : return undefined
193 : }
194 : }
195 :
196 : // Server caps the page size at 500; a single job's outputs fit well within a
197 : // few pages, so this bound also guards against a runaway pagination loop.
198 2356 : const JOB_ASSETS_PAGE_SIZE = 500
199 2356 : const JOB_ASSETS_MAX_PAGES = 20
200 :
201 : /**
202 : * Fetches all output assets for a job from GET /api/jobs/{job_id}/assets,
203 : * paginating internally. Each asset carries a real asset id plus per-output
204 : * node context (node_id, output_key, output_index) resolved server-side by
205 : * content hash. Degrades to whatever was accumulated on any failure (e.g. the
206 : * endpoint is unavailable on non-cloud distributions) so callers can still
207 : * render; `complete` is false whenever pages are known to be missing, so a
208 : * truncated list is distinguishable from a full one and callers can decline to
209 : * cache it.
210 : */
211 0 : export async function fetchJobAssets(
212 : fetchApi: (url: string) => Promise<Response>,
213 : jobId: JobId
214 : ): Promise<JobAssetsResult> {
215 0 : const assets: JobOutputAsset[] = []
216 0 : let offset = 0
217 :
218 0 : try {
219 0 : for (let page = 0; page < JOB_ASSETS_MAX_PAGES; page++) {
220 : const url = `/jobs/${encodeURIComponent(jobId)}/assets?limit=${JOB_ASSETS_PAGE_SIZE}&offset=${offset}`
221 0 : const res = await fetchApi(url)
222 0 : if (!res.ok) {
223 0 : console.warn(
224 : `[Jobs API] Failed to fetch assets for job ${jobId}: ${res.status}`
225 : )
226 0 : return { assets, complete: false }
227 : }
228 :
229 0 : const data = zJobAssetsResponse.parse(await res.json())
230 0 : assets.push(...data.assets)
231 :
232 : const hasMore = data.pagination?.has_more ?? false
233 0 : if (!hasMore) return { assets, complete: true }
234 :
235 0 : if (data.assets.length === 0) {
236 0 : console.warn(
237 : `[Jobs API] Job ${jobId} assets page reported has_more with an empty page; stopping pagination`
238 : )
239 0 : return { assets, complete: false }
240 : }
241 :
242 0 : offset += data.assets.length
243 : }
244 : } catch (error) {
245 0 : console.error(`Failed to fetch assets for job ${jobId}:`, error)
246 0 : return { assets, complete: false }
247 : }
248 :
249 0 : console.warn(
250 : `[Jobs API] Job ${jobId} assets pagination hit the ${JOB_ASSETS_MAX_PAGES}-page cap; returning a truncated list`
251 : )
252 0 : return { assets, complete: false }
253 : }
254 :
255 : /**
256 : * Extracts and validates workflow from job detail response.
257 : * The workflow is nested at: workflow.extra_data.extra_pnginfo.workflow
258 : *
259 : * Uses Zod validation via validateComfyWorkflow to ensure the workflow
260 : * conforms to the expected schema. Logs validation failures for debugging
261 : * but still returns undefined to allow graceful degradation.
262 : */
263 4 : export async function extractWorkflow(
264 : job: JobDetail | undefined
265 : ): Promise<ComfyWorkflowJSON | undefined> {
266 4 : const parsed = zWorkflowContainer.safeParse(job?.workflow)
267 4 : if (!parsed.success) return undefined
268 :
269 4 : const rawWorkflow = parsed.data.extra_data?.extra_pnginfo?.workflow
270 4 : if (!rawWorkflow) return undefined
271 :
272 : const validated = await validateComfyWorkflow(rawWorkflow, (error) => {
273 0 : console.warn('[extractWorkflow] Workflow validation failed:', error)
274 : })
275 :
276 4 : return validated ?? undefined
277 : }
278 :
279 : /**
280 : * Extracts the API-format graph a job stores at: workflow.prompt
281 : *
282 : * Jobs submitted through the API embed no editor workflow, leaving the
283 : * API-format prompt as the only graph they store. Returns undefined whenever
284 : * an embedded workflow exists so those submissions are always used as-is,
285 : * never re-derived. The prompt itself is returned unvalidated — callers gate
286 : * it with `app.isApiJson` before loading it.
287 : */
288 1 : export function extractApiPrompt(job: JobDetail | undefined): unknown {
289 1 : const parsed = zWorkflowContainer.safeParse(job?.workflow)
290 1 : if (!parsed.success) return undefined
291 :
292 1 : const embeddedWorkflow = parsed.data.extra_data?.extra_pnginfo?.workflow
293 1 : if (embeddedWorkflow !== undefined && embeddedWorkflow !== null) {
294 0 : return undefined
295 : }
296 :
297 1 : return parsed.data.prompt
298 : }
|