Line data Source code
1 : import type { PromptWorkflowMetadata } from '@comfyorg/comfyui-api-types'
2 : import type { PromptInfo } from '@comfyorg/ingest-types'
3 : import { z } from 'zod'
4 : import { zNodeId } from '@comfyorg/graph-model/workflowSchema'
5 : import type { TaskStatus } from './taskSchema'
6 : import { resultItemType } from './resultItemTypeSchema'
7 :
8 : export type JobId = string
9 :
10 : /**
11 : * Core's OpenAPI defines no websocket frame schemas. It documents that
12 : * `workflow_metadata` is echoed onto every JSON frame carrying a `prompt_id`,
13 : * so this field is the request contract read back rather than a separate one.
14 : *
15 : * What transfers from the spec is the map's VALUE type, not the map: core types
16 : * `workflow_metadata` as an arbitrary string map, and the frame carries that
17 : * map's contents merged into the frame object. So a `Pick` or `Partial` of the
18 : * map would describe the wrong shape here even though it would compile.
19 : *
20 : * The generated package is bundled into the published declarations by
21 : * `tsconfig.types.json`, so this reference is inlined rather than emitted as an
22 : * import of a private package that an external consumer could not resolve.
23 : */
24 : type StampedWorkflowId = { workflow_id?: PromptWorkflowMetadata[string] }
25 :
26 2356 : export const zResultItem = z.object({
27 : filename: z.string().optional(),
28 : subfolder: z.string().optional(),
29 : type: resultItemType.optional(),
30 : display_name: z.string().optional(),
31 : id: z.string().optional(),
32 : preview_id: z.string().optional()
33 : })
34 : export type ResultItem = z.infer<typeof zResultItem>
35 : // Uses .passthrough() because custom nodes can output arbitrary keys.
36 : // See docs/adr/NODE-OUTPUTS-0007-output-passthrough-for-extensible-nodes.md
37 2356 : const zOutputs = z
38 : .object({
39 : audio: z.array(zResultItem).optional(),
40 : images: z.array(zResultItem).optional(),
41 : video: z.array(zResultItem).optional(),
42 : animated: z.array(z.boolean()).optional(),
43 : text: z.union([z.string(), z.array(z.string())]).optional()
44 : })
45 : .passthrough()
46 :
47 : export type NodeExecutionOutput = z.infer<typeof zOutputs>
48 :
49 : export type NodeOutputWith<T extends Record<string, unknown>> =
50 : NodeExecutionOutput & T
51 :
52 : type NodeId = z.infer<typeof zNodeId>
53 :
54 : export type StatusWsMessageStatus = PromptInfo
55 : export interface StatusWsMessage {
56 : status?: StatusWsMessageStatus | null
57 : sid?: string | null
58 : }
59 : export interface ProgressWsMessage extends StampedWorkflowId {
60 : value: number
61 : max: number
62 : prompt_id: JobId
63 : node: NodeId
64 : }
65 : export interface NodeProgressState {
66 : value: number
67 : max: number
68 : state: 'pending' | 'running' | 'finished' | 'error'
69 : node_id: NodeId
70 : prompt_id: JobId
71 : display_node_id?: NodeId
72 : parent_node_id?: NodeId
73 : real_node_id?: NodeId
74 : }
75 : export interface ProgressStateWsMessage extends StampedWorkflowId {
76 : prompt_id: JobId
77 : nodes: Record<NodeId, NodeProgressState>
78 : }
79 : export interface ExecutingWsMessage extends StampedWorkflowId {
80 : node: NodeId | null
81 : display_node?: NodeId
82 : prompt_id: JobId
83 : }
84 : export interface ExecutedWsMessage extends ExecutingWsMessage {
85 : node: NodeId
86 : display_node: NodeId
87 : output: NodeExecutionOutput
88 : merge?: boolean
89 : }
90 : interface ExecutionWsMessageBase extends StampedWorkflowId {
91 : prompt_id: JobId
92 : timestamp: number
93 : }
94 : export type ExecutionStartWsMessage = ExecutionWsMessageBase
95 : export type ExecutionSuccessWsMessage = ExecutionWsMessageBase
96 : export interface ExecutionCachedWsMessage extends ExecutionWsMessageBase {
97 : nodes: NodeId[]
98 : }
99 : export interface ExecutionInterruptedWsMessage extends ExecutionWsMessageBase {
100 : node_id: NodeId
101 : node_type: string
102 : executed: NodeId[]
103 : }
104 : export interface ExecutionErrorWsMessage extends ExecutionWsMessageBase {
105 : node_id?: NodeId | null
106 : node_type: string
107 : executed: NodeId[]
108 : exception_message: string
109 : exception_type: string
110 : traceback: string[]
111 : current_inputs?: unknown
112 : current_outputs?: unknown
113 : }
114 : export interface ProgressTextWsMessage {
115 : nodeId: NodeId
116 : text: string
117 : prompt_id?: string
118 : }
119 : export interface NotificationWsMessage {
120 : value: string
121 : id?: string
122 : }
123 : export interface TerminalSize {
124 : cols: number
125 : row: number
126 : }
127 : export interface LogEntry {
128 : t: string
129 : m: string
130 : }
131 : export interface LogsWsMessage {
132 : size?: TerminalSize
133 : entries: LogEntry[]
134 : }
135 : export interface LogsRawResponse {
136 : size: TerminalSize
137 : entries: LogEntry[]
138 : }
139 : export type FeatureFlagsWsMessage = Record<string, unknown>
140 : /** ComfyUI's `agent` server feature, pushed again on every change. */
141 : export interface AgentStatusWsMessage {
142 : state: 'starting' | 'ready' | 'failed'
143 : reason?: string | null
144 : }
145 : export interface AssetDownloadWsMessage {
146 : task_id: string
147 : asset_name: string
148 : bytes_total: number
149 : bytes_downloaded: number
150 : progress: number
151 : status: TaskStatus
152 : asset_id?: string
153 : error?: string
154 : }
155 : export interface AssetExportWsMessage {
156 : task_id: string
157 : export_name?: string
158 : assets_total: number
159 : assets_attempted: number
160 : assets_failed: number
161 : bytes_total: number
162 : bytes_processed: number
163 : progress: number
164 : status: TaskStatus
165 : error?: string
166 : }
167 : export interface AssetSeedCompletedWsMessage {
168 : created: number
169 : elapsed: number
170 : enriched: number
171 : phase: 'fast' | 'enrich' | 'full'
172 : skipped: number
173 : total: number
174 : }
175 :
176 2356 : export const zTaskOutput = z.record(zNodeId, zOutputs)
177 : export type TaskOutput = z.infer<typeof zTaskOutput>
|