Line data Source code
1 : /**
2 : * @fileoverview Jobs API types - Backend job API format
3 : * @module platform/remote/comfyui/jobs/jobTypes
4 : *
5 : * These types represent the jobs API format returned by the backend.
6 : * Jobs API provides a memory-optimized alternative to history API.
7 : */
8 :
9 : import { zJobEntry, zJobExecutionMetaEntry } from '@comfyorg/ingest-types/zod'
10 : import { z } from 'zod'
11 :
12 : import { resultItemType } from './resultItemTypeSchema'
13 : import { zTaskOutput } from './executionTypes'
14 :
15 : export { zJobExecutionMetaEntry }
16 :
17 2356 : const zJobStatus = z.enum([
18 : 'pending',
19 : 'in_progress',
20 : 'completed',
21 : 'failed',
22 : 'cancelled'
23 : ])
24 :
25 2356 : const zPreviewOutput = z
26 : .object({
27 : filename: z.string().optional(),
28 : subfolder: z.string().optional(),
29 : type: resultItemType.optional(),
30 : nodeId: z.string(),
31 : mediaType: z.string(),
32 : content: z.string().optional(),
33 : display_name: z.string().optional()
34 : })
35 : .passthrough()
36 :
37 : /**
38 : * Execution error from Jobs API.
39 : * Similar to ExecutionErrorWsMessage but with optional prompt_id/timestamp/executed
40 : * since these may not be present in stored errors or infrastructure-generated errors.
41 : */
42 2356 : const zExecutionError = z
43 : .object({
44 : prompt_id: z.string().optional(),
45 : timestamp: z.number().optional(),
46 : node_id: z.string(),
47 : node_type: z.string(),
48 : executed: z.array(z.string()).optional(),
49 : exception_message: z.string(),
50 : exception_type: z.string(),
51 : traceback: z.array(z.string()),
52 : current_inputs: z.unknown().optional(),
53 : current_outputs: z.unknown().optional()
54 : })
55 : .passthrough()
56 :
57 : export type ExecutionError = z.infer<typeof zExecutionError>
58 :
59 : /**
60 : * Raw job from API - uses passthrough to allow extra fields
61 : */
62 2356 : const zRawJobListItem = z
63 : .object({
64 : id: z.string(),
65 : status: zJobStatus,
66 : create_time: z.number(),
67 : execution_start_time: z.number().nullable().optional(),
68 : execution_end_time: z.number().nullable().optional(),
69 : preview_output: zPreviewOutput.nullable().optional(),
70 : // Sourced from the generated `@comfyorg/ingest-types` JobEntry schema
71 : // (outputs_count/previewable_outputs_count), widened to nullable since
72 : // local ComfyUI's /api/jobs sends explicit nulls where Cloud omits.
73 : // .int() inherited from zJobEntry; intentionally stricter than the previous z.number()
74 : outputs_count: zJobEntry.shape.outputs_count.nullable(),
75 : previewable_outputs_count:
76 : zJobEntry.shape.previewable_outputs_count.nullable(),
77 : execution_error: zExecutionError.nullable().optional(),
78 : workflow_id: z.string().nullable().optional(),
79 : priority: z.number().optional()
80 : })
81 : .passthrough()
82 :
83 : /**
84 : * Job detail - returned by GET /api/jobs/{job_id} (detail endpoint)
85 : * Includes full workflow and outputs for re-execution and downloads
86 : */
87 2356 : export const zJobDetail = zRawJobListItem
88 : .extend({
89 : workflow: z.unknown().optional(),
90 : outputs: zTaskOutput.optional(),
91 : update_time: z.number().optional(),
92 : execution_status: z.unknown().optional(),
93 : execution_meta: z.unknown().optional()
94 : })
95 : .passthrough()
96 :
97 2356 : const zPaginationInfo = z.object({
98 : offset: z.number(),
99 : limit: z.number(),
100 : total: z.number(),
101 : has_more: z.boolean()
102 : })
103 :
104 2356 : export const zJobsListResponse = z.object({
105 : jobs: z.array(zRawJobListItem),
106 : pagination: zPaginationInfo
107 : })
108 :
109 : /**
110 : * A single output asset produced by a job, enriched with per-output node
111 : * context (`node_id`, `output_key`, `output_index`) correlated from the job's
112 : * execution outputs by content hash. Node-context fields are null when the
113 : * asset cannot be matched to an output entry.
114 : * Returned by GET /api/jobs/{job_id}/assets.
115 : */
116 2356 : const zJobOutputAsset = z
117 : .object({
118 : id: z.string(),
119 : name: z.string(),
120 : hash: z.string().nullable().optional(),
121 : preview_url: z.string().nullable().optional(),
122 : mime_type: z.string().nullable().optional(),
123 : size: z.number().nullable().optional(),
124 : node_id: z.string().nullable().optional(),
125 : output_key: z.string().nullable().optional(),
126 : output_index: z.number().nullable().optional(),
127 : created_at: z.string().nullable().optional()
128 : })
129 : .passthrough()
130 :
131 : /**
132 : * Paginated list of a single job's output assets. The envelope is lenient
133 : * where `zJobsListResponse` is strict: the cloud serialiser drops null fields
134 : * (`exclude_none=True`) and this endpoint's pagination convention is
135 : * unconfirmed, so a missing bookkeeping field must degrade to "no more pages"
136 : * rather than reject the payload and silently disable enrichment.
137 : */
138 2356 : export const zJobAssetsResponse = z.object({
139 : job_id: z.string().optional(),
140 : assets: z.array(zJobOutputAsset),
141 : pagination: z
142 : .object({
143 : offset: z.number().nullable().optional(),
144 : limit: z.number().nullable().optional(),
145 : total: z.number().nullable().optional(),
146 : has_more: z.boolean().nullable().optional()
147 : })
148 : .optional()
149 : })
150 :
151 : /** Schema for workflow container structure in job detail responses */
152 2356 : export const zWorkflowContainer = z.object({
153 : prompt: z.unknown().optional(),
154 : extra_data: z
155 : .object({
156 : extra_pnginfo: z
157 : .object({
158 : workflow: z.unknown().optional()
159 : })
160 : .nullish()
161 : })
162 : .nullish()
163 : })
164 :
165 : export type JobStatus = z.infer<typeof zJobStatus>
166 : export type RawJobListItem = z.infer<typeof zRawJobListItem>
167 : /** Job list item with priority always set (server-provided or synthetic) */
168 : export type JobListItem = RawJobListItem & { priority: number }
169 : export type JobDetail = z.infer<typeof zJobDetail>
170 : export type JobOutputAsset = z.infer<typeof zJobOutputAsset>
171 :
172 : /**
173 : * A job's output assets plus whether the list is known to be exhaustive.
174 : * `complete` is false when a page failed, the server reported more pages it
175 : * did not deliver, or the page cap was hit — callers use it to avoid caching a
176 : * truncated list as if it were the whole set.
177 : */
178 : export interface JobAssetsResult {
179 : assets: JobOutputAsset[]
180 : complete: boolean
181 : }
182 :
183 : /** Task type used in the API (queue vs history endpoints) */
184 : export type APITaskType = 'queue' | 'history'
185 :
186 : /** Internal task type derived from job status for UI display */
187 : export type TaskType = 'Running' | 'Pending' | 'History'
|