comfyui_client/meta.rs
1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::{collections::HashMap, fmt::Debug};
4
5/// Contains information about a prompt, including its execution details.
6#[derive(Serialize, Deserialize, Debug)]
7pub struct PromptInfo {
8 /// Execution information related to the prompt.
9 pub exec_info: ExecInfo,
10}
11
12/// Contains execution details such as the remaining queue length.
13#[derive(Serialize, Deserialize, Debug)]
14pub struct ExecInfo {
15 /// The number of remaining tasks in the execution queue.
16 pub queue_remaining: usize,
17}
18
19/// Represents file information including filename, subfolder, and file type.
20#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
21pub struct FileInfo {
22 /// The name of the file.
23 #[serde(alias = "name")]
24 pub filename: String,
25 /// The subfolder where the file is located.
26 pub subfolder: String,
27 /// The type of the file.
28 pub r#type: String,
29}
30
31/// Represents a prompt with an identifier, a number, and potential node errors.
32#[derive(Serialize, Deserialize, Debug)]
33pub struct Prompt {
34 /// Unique identifier for the prompt.
35 pub prompt_id: String,
36 /// A numeric identifier for the prompt.
37 pub number: usize,
38 /// A mapping of node identifiers to error details in JSON format.
39 pub node_errors: HashMap<String, Value>,
40}
41
42/// Represents the history of outputs for a prompt.
43#[derive(Serialize, Deserialize, Debug)]
44pub struct History {
45 /// A mapping of output identifiers to their corresponding images.
46 pub outputs: HashMap<String, Images>,
47}
48
49/// Contains an optional list of image file information.
50#[derive(Serialize, Deserialize, Debug)]
51pub struct Images {
52 /// A vector of file information objects, if available.
53 pub images: Option<Vec<FileInfo>>,
54}
55
56/// Represents events emitted by the system.
57///
58/// The enum variants correspond to different event types. The `Unknown` variant
59/// holds raw JSON data for unrecognized events.
60#[derive(Serialize, Deserialize, Debug)]
61#[serde(tag = "type", content = "data")]
62#[serde(rename_all = "snake_case")]
63pub enum Event {
64 /// A status event containing execution information.
65 Status(StatusEvent),
66 /// A progress event indicating current progress.
67 Progress(ProgressEvent),
68 /// An event indicating that a node has completed execution along with its
69 /// output.
70 Executed(ExecutedEvent),
71 /// An event indicating that a node is currently executing.
72 Executing(ExecutingEvent),
73 /// An event signaling the start of execution for a prompt.
74 ExecutionStart(ExecutionStartEvent),
75 /// An event signaling that an error occurred during execution.
76 ExecutionError(ExecutionErrorEvent),
77 /// An event indicating that the execution results were retrieved from the
78 /// cache.
79 ExecutionCached(ExecutionCachedEvent),
80 /// An event indicating that the execution was interrupted.
81 ExecutionInterrupted(ExecutionInterruptedEvent),
82 /// An unknown event type that encapsulates raw JSON data.
83 #[serde(skip)]
84 Unknown(Value),
85}
86
87/// Event payload for a status event, containing execution information.
88#[derive(Serialize, Deserialize, Debug)]
89pub struct StatusEvent {
90 /// Execution information associated with the event.
91 pub status: ExecInfo,
92}
93
94/// Event payload for a progress update, including current value and maximum
95/// value.
96#[derive(Serialize, Deserialize, Debug)]
97pub struct ProgressEvent {
98 /// The current progress value.
99 pub value: usize,
100 /// The maximum progress value.
101 pub max: usize,
102}
103
104/// Represents the output of an executed node.
105#[derive(Serialize, Deserialize, Debug)]
106pub struct Output {
107 /// A list of image file information objects.
108 pub images: Vec<FileInfo>,
109}
110
111/// Event payload for a completed execution, including the node identifier,
112/// prompt ID, and output data.
113#[derive(Serialize, Deserialize, Debug)]
114pub struct ExecutedEvent {
115 /// Identifier of the node that completed execution.
116 pub node: String,
117 /// The prompt ID associated with the execution.
118 pub prompt_id: String,
119 /// The output generated by the executed node.
120 pub output: Output,
121}
122
123/// Event payload for an execution in progress, including the node identifier
124/// and prompt ID.
125#[derive(Serialize, Deserialize, Debug)]
126pub struct ExecutingEvent {
127 /// Identifier of the node currently executing.
128 pub node: String,
129 /// The prompt ID associated with the execution.
130 pub prompt_id: String,
131}
132
133/// Event payload indicating that the execution has started.
134#[derive(Serialize, Deserialize, Debug)]
135pub struct ExecutionStartEvent {
136 /// The prompt ID for which the execution has started.
137 pub prompt_id: String,
138}
139
140/// Event payload for an execution error, containing details about the error and
141/// its context.
142#[derive(Serialize, Deserialize, Debug)]
143pub struct ExecutionErrorEvent {
144 /// The prompt ID associated with the error.
145 pub prompt_id: String,
146 /// The identifier of the node where the error occurred.
147 pub node_id: String,
148 /// The type of the node where the error occurred.
149 pub node_type: String,
150 /// A list of node identifiers that were executed before the error.
151 pub executed: Vec<String>,
152 /// The error message from the exception.
153 pub exception_message: String,
154 /// The type of the exception.
155 pub exception_type: String,
156 /// A traceback of the error as a list of strings.
157 pub traceback: Vec<String>,
158 /// The current input values at the time of the error.
159 pub current_inputs: HashMap<String, Value>,
160 /// The current output values at the time of the error.
161 pub current_outputs: HashMap<String, Value>,
162}
163
164/// Event payload indicating that the execution result was obtained from the
165/// cache.
166#[derive(Serialize, Deserialize, Debug)]
167pub struct ExecutionCachedEvent {
168 /// A list of node identifiers that were cached.
169 pub nodes: Vec<String>,
170 /// The prompt ID associated with the cached execution.
171 pub prompt_id: String,
172}
173
174/// Event payload for an interrupted execution, containing details about the
175/// interruption.
176#[derive(Serialize, Deserialize, Debug)]
177pub struct ExecutionInterruptedEvent {
178 /// The prompt ID associated with the interruption.
179 pub prompt_id: String,
180 /// The identifier of the node where the execution was interrupted.
181 pub node_id: String,
182 /// The type of the node that was interrupted.
183 pub node_type: String,
184 /// A list of node identifiers that were executed before the interruption.
185 pub executed: Vec<String>,
186}
187
188#[cfg(test)]
189mod tests {
190 use super::*;
191 use serde_json::json;
192
193 /// Tests serialization of different event types.
194 #[test]
195 fn test_serialize_event() {
196 let ev = Event::Status(StatusEvent {
197 status: ExecInfo { queue_remaining: 0 },
198 });
199 let value = serde_json::to_value(&ev).unwrap();
200 assert_eq!(
201 value,
202 json!({
203 "type": "status",
204 "data": {
205 "status": {
206 "queue_remaining": 0,
207 }
208 }
209 })
210 );
211
212 let ev = Event::ExecutionStart(ExecutionStartEvent {
213 prompt_id: "xxxxxx".to_string(),
214 });
215 let value = serde_json::to_value(&ev).unwrap();
216 assert_eq!(
217 value,
218 json!({
219 "type": "execution_start",
220 "data": {
221 "prompt_id": "xxxxxx",
222 }
223 })
224 );
225 }
226}