comfyui_api/models/
history.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use super::{Image, Prompt};
6
7/// Struct containing task results from the ComfyUI API `history` endpoint.
8#[derive(Deserialize, Debug)]
9#[serde(transparent)]
10pub struct History {
11    /// Completed tasks indexed by their uuid.
12    pub tasks: HashMap<uuid::Uuid, Task>,
13}
14
15/// Struct representing a single task result from the ComfyUI API `history` endpoint.
16#[derive(Serialize, Deserialize, Debug)]
17pub struct Task {
18    /// Outputs from the task.
19    pub outputs: Outputs,
20    /// Information about prompt execution.
21    pub prompt: PromptResult,
22}
23
24/// Struct representing outputs from a task.
25#[derive(Serialize, Deserialize, Debug)]
26#[serde(transparent)]
27pub struct Outputs {
28    /// Outputs from the task indexed by node.
29    pub nodes: HashMap<String, NodeOutputOrUnknown>,
30}
31
32/// Enumertion of all possible output types from a node.
33#[derive(Serialize, Deserialize, Debug)]
34#[serde(untagged)]
35pub enum NodeOutputOrUnknown {
36    /// Enum variant representing image outputs from a node.
37    NodeOutput(NodeOutput),
38    /// Struct capturing unknown outputs.
39    Unknown(serde_json::Value),
40}
41
42/// Struct representing image outputs from a node.
43#[derive(Serialize, Deserialize, Debug)]
44pub struct NodeOutput {
45    /// Images from the node.
46    pub images: Vec<Image>,
47}
48
49/// Struct representing a prompt result.
50#[derive(Serialize, Deserialize, Debug)]
51#[serde(from = "(u64, uuid::Uuid, Prompt, ExtraData, OutputsToExecute)")]
52pub struct PromptResult {
53    /// The task number.
54    pub num: u64,
55    /// The task uuid.
56    pub id: uuid::Uuid,
57    /// The prompt that was executed.
58    pub prompt: Prompt,
59    /// Extra data about execution.
60    pub extra_data: ExtraData,
61    /// Outputs executed for this prompt.
62    pub outputs_to_execute: OutputsToExecute,
63}
64
65impl From<(u64, uuid::Uuid, Prompt, ExtraData, OutputsToExecute)> for PromptResult {
66    fn from(
67        (num, id, prompt, extra_data, outputs_to_execute): (
68            u64,
69            uuid::Uuid,
70            Prompt,
71            ExtraData,
72            OutputsToExecute,
73        ),
74    ) -> Self {
75        Self {
76            num,
77            id,
78            prompt,
79            extra_data,
80            outputs_to_execute,
81        }
82    }
83}
84
85impl From<PromptResult> for (u64, uuid::Uuid, Prompt, ExtraData, OutputsToExecute) {
86    fn from(
87        PromptResult {
88            num,
89            id,
90            prompt,
91            extra_data,
92            outputs_to_execute,
93        }: PromptResult,
94    ) -> Self {
95        (num, id, prompt, extra_data, outputs_to_execute)
96    }
97}
98
99/// Struct representing extra data about prompt execution.
100#[derive(Serialize, Deserialize, Debug, Clone)]
101pub struct ExtraData {
102    /// The client id that performed the request.
103    pub client_id: uuid::Uuid,
104}
105
106/// Struct representing outputs to execute for a prompt.
107#[derive(Serialize, Deserialize, Debug, Clone)]
108#[serde(transparent)]
109pub struct OutputsToExecute {
110    /// List of nodes which have outputs.
111    pub nodes: Vec<String>,
112}