studio-worker 0.4.5

Pull-based image-generation worker for the minis.gg studio.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Shared wire-format mirrors of the studio API.
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

// ---------------------------------------------------------------------------
// Task kinds — every job claimed by the worker is one of these.
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Hash)]
#[serde(rename_all = "snake_case")]
pub enum TaskKind {
    Image,
    Llm,
    AudioStt,
    AudioTts,
    Video,
}

impl TaskKind {
    pub const ALL: [TaskKind; 5] = [
        TaskKind::Image,
        TaskKind::Llm,
        TaskKind::AudioStt,
        TaskKind::AudioTts,
        TaskKind::Video,
    ];

    pub fn as_str(&self) -> &'static str {
        match self {
            TaskKind::Image => "image",
            TaskKind::Llm => "llm",
            TaskKind::AudioStt => "audio_stt",
            TaskKind::AudioTts => "audio_tts",
            TaskKind::Video => "video",
        }
    }
}

// ---------------------------------------------------------------------------
// Per-kind task parameters
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageParams {
    pub prompt: String,
    /// Negative prompt — what the model should steer AWAY from.
    /// Optional: when `None`, `sd-cli` is invoked without
    /// `--negative-prompt`.  Wire-format key: `negativePrompt`.
    #[serde(default)]
    pub negative_prompt: Option<String>,
    /// HTTPS URL to a base image for image-to-image generation.
    /// When set, the worker downloads the bytes to a local tempfile
    /// and invokes `sd-cli --init-img <path>`.  Required for any
    /// task profile that declares the `initImage` capability.
    #[serde(default)]
    pub init_image_url: Option<String>,
    /// HTTPS URL to a black/white inpaint mask (white = the region the
    /// model may repaint). When set alongside `init_image_url`, the
    /// worker downloads it and invokes `sd-cli --mask <path>` so only
    /// the masked region changes. Wire-format key: `maskUrl`.
    #[serde(default)]
    pub mask_url: Option<String>,
    /// HTTPS URL to a reference image for instruction-edit models
    /// (e.g. Qwen-Image-Edit / Flux Kontext).  When set, the worker
    /// downloads it and invokes `sd-cli -r <path>` (reference mode)
    /// instead of the `--init-img`/`--strength`/`--mask` img2img path:
    /// the model regenerates the whole image from the reference per the
    /// instruction prompt; per-region clipping happens in the studio
    /// composite. Wire-format key: `refImageUrl`.
    #[serde(default)]
    pub ref_image_url: Option<String>,
    /// Denoise / noise-strength for i2i (0.0 = keep init image
    /// unchanged, 1.0 = full re-noise).  Maps to `sd-cli --strength`.
    #[serde(default)]
    pub denoise: Option<f32>,
    /// Classifier-free guidance scale.  Per-job override; falls back
    /// to `ModelSource.cliDefaults.cfgScale` when `None`.
    #[serde(default)]
    pub cfg_scale: Option<f32>,
    /// Sampler choice (`euler`, `euler_a`, `dpm++2m`, ...).  Per-job
    /// override; falls back to `ModelSource.cliDefaults.samplingMethod`.
    #[serde(default)]
    pub sampling_method: Option<String>,
    #[serde(default = "default_image_dim")]
    pub width: u32,
    #[serde(default = "default_image_dim")]
    pub height: u32,
    #[serde(default = "default_steps")]
    pub steps: u32,
    #[serde(default)]
    pub seed: Option<u64>,
    #[serde(default = "default_image_ext")]
    pub ext: String,
}

fn default_image_dim() -> u32 {
    512
}
fn default_steps() -> u32 {
    20
}
fn default_image_ext() -> String {
    "webp".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub role: String,
    pub content: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LlmParams {
    pub messages: Vec<ChatMessage>,
    /// System prompt prepended to the conversation.  Engines that
    /// don't accept a separate system role inline it as the first
    /// chat turn.
    #[serde(default)]
    pub system: Option<String>,
    #[serde(default = "default_max_tokens")]
    pub max_tokens: u32,
    #[serde(default = "default_temperature")]
    pub temperature: f32,
    #[serde(default)]
    pub top_p: Option<f32>,
    #[serde(default)]
    pub stop: Option<Vec<String>>,
    /// Strict-JSON output schema.  Engine-specific; passed through
    /// verbatim to the backend (e.g. llama.cpp's `--grammar` JSON).
    #[serde(default)]
    pub json_schema: Option<serde_json::Value>,
    /// Reasoning effort hint.  Currently honoured by Gemini-style
    /// backends only; ignored elsewhere.
    #[serde(default)]
    pub reasoning: Option<String>,
}

fn default_max_tokens() -> u32 {
    512
}
fn default_temperature() -> f32 {
    0.7
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioSttParams {
    /// HTTPS URL to fetch the audio bytes from (e.g. R2 signed URL).
    pub input_url: String,
    #[serde(default)]
    pub language: Option<String>,
    /// Translate the audio to English (whisper `--translate`).
    #[serde(default)]
    pub translate: Option<bool>,
    /// Initial prompt to bias the transcription.
    #[serde(default)]
    pub prompt: Option<String>,
    /// Voice-activity detection.
    #[serde(default)]
    pub vad: Option<bool>,
    /// Timestamp granularity: `"segment"` or `"word"`.
    #[serde(default)]
    pub timestamps: Option<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioTtsParams {
    pub text: String,
    #[serde(default = "default_voice")]
    pub voice: String,
    /// Playback speed multiplier (1.0 = natural pace).
    #[serde(default)]
    pub speed: Option<f32>,
    /// Spoken-language hint (e.g. `"en"`, `"nl"`).
    #[serde(default)]
    pub language: Option<String>,
    #[serde(default = "default_audio_ext")]
    pub ext: String,
}

fn default_voice() -> String {
    "default".into()
}
fn default_audio_ext() -> String {
    "wav".into()
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VideoParams {
    pub prompt: String,
    #[serde(default)]
    pub negative_prompt: Option<String>,
    /// HTTPS URL to a base frame for image-to-video models.
    #[serde(default)]
    pub init_image_url: Option<String>,
    #[serde(default = "default_video_seconds")]
    pub seconds: f32,
    /// Frame rate; defaults to backend-specific value when `None`.
    #[serde(default)]
    pub fps: Option<u32>,
    #[serde(default = "default_image_dim")]
    pub width: u32,
    #[serde(default = "default_image_dim")]
    pub height: u32,
    #[serde(default = "default_video_ext")]
    pub ext: String,
}

fn default_video_seconds() -> f32 {
    2.0
}
fn default_video_ext() -> String {
    "mp4".into()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Task {
    Image(ImageParams),
    Llm(LlmParams),
    AudioStt(AudioSttParams),
    AudioTts(AudioTtsParams),
    Video(VideoParams),
}

impl Task {
    pub fn kind(&self) -> TaskKind {
        match self {
            Task::Image(_) => TaskKind::Image,
            Task::Llm(_) => TaskKind::Llm,
            Task::AudioStt(_) => TaskKind::AudioStt,
            Task::AudioTts(_) => TaskKind::AudioTts,
            Task::Video(_) => TaskKind::Video,
        }
    }
}

// ---------------------------------------------------------------------------
// Per-kind task results
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub enum TaskResult {
    /// Binary image (webp/png/...) with the chosen extension.
    Image { bytes: Vec<u8>, ext: String },
    /// JSON response from an LLM call (free-form to mirror common APIs).
    Llm { json: serde_json::Value },
    /// JSON transcript.
    AudioStt { json: serde_json::Value },
    /// Binary audio (wav/mp3/...) with the chosen extension.
    AudioTts { bytes: Vec<u8>, ext: String },
    /// Binary video (mp4/webm/...) with the chosen extension.
    Video { bytes: Vec<u8>, ext: String },
}

impl TaskResult {
    pub fn kind(&self) -> TaskKind {
        match self {
            TaskResult::Image { .. } => TaskKind::Image,
            TaskResult::Llm { .. } => TaskKind::Llm,
            TaskResult::AudioStt { .. } => TaskKind::AudioStt,
            TaskResult::AudioTts { .. } => TaskKind::AudioTts,
            TaskResult::Video { .. } => TaskKind::Video,
        }
    }
}

// ---------------------------------------------------------------------------
// Worker capabilities + registration
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerCapabilities {
    #[serde(rename = "machineName")]
    pub machine_name: String,
    pub username: String,
    #[serde(rename = "agentVersion")]
    pub agent_version: String,
    pub engine: String,
    #[serde(rename = "vramTotalGb")]
    pub vram_total_gb: f32,
    #[serde(rename = "vramThresholdGb")]
    pub vram_threshold_gb: f32,
    #[serde(rename = "autoEnabled")]
    pub auto_enabled: bool,
    #[serde(rename = "autoStart")]
    pub auto_start: bool,
    /// Flat list of models, kept for backward compat with the existing studio
    /// API that doesn't know about kinds yet.  Equivalent to
    /// `supported_models_per_kind[Image]`.
    #[serde(rename = "supportedModels")]
    pub supported_models: Vec<String>,
    /// New: task kinds this worker can serve.
    #[serde(rename = "taskKinds", default)]
    pub task_kinds: Vec<TaskKind>,
    /// New: per-kind supported model ids.
    #[serde(rename = "supportedModelsPerKind", default)]
    pub supported_models_per_kind: BTreeMap<TaskKind, Vec<String>>,
}

// ---------------------------------------------------------------------------
// Auto-register wire format — the only registration path.
//
// Worker POSTs `/workers/register-request` with hostname / username /
// VRAM / supported models, gets a `requestId` back, and polls
// `/workers/register-requests/:id` until the operator approves or
// rejects from the studio dashboard.
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
pub struct AutoRegisterRequest {
    /// Per-install UUID stable across worker restarts on the same
    /// machine.  The studio uses it to dedup re-submissions.
    #[serde(rename = "installId")]
    pub install_id: String,
    /// SHA-256 hex of the worker-side `registration_secret`.  The
    /// worker keeps the secret locally and presents it as a Bearer
    /// token when polling for status; only the hash leaves the box.
    #[serde(rename = "registrationSecretHash")]
    pub registration_secret_hash: String,
    /// Full capability snapshot — hostname, username, engine, VRAM,
    /// supported models so the operator can decide.
    pub capabilities: WorkerCapabilities,
    /// `studio-worker/<version>` so the operator sees stale clients.
    #[serde(rename = "userAgent")]
    pub user_agent: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct AutoRegisterRequestResponse {
    #[serde(rename = "requestId")]
    pub request_id: String,
    /// Always `"pending"` on first response.  Idempotent dedup may
    /// return the existing requestId for the same
    /// `(installId, sourceIp)` tuple — status is still "pending".
    pub status: String,
}

/// Tagged union returned by `GET /workers/register-requests/:id`.
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case", tag = "status")]
pub enum RegisterStatus {
    Pending,
    Approved {
        #[serde(rename = "workerId")]
        worker_id: String,
        #[serde(rename = "authToken")]
        auth_token: String,
    },
    Rejected {
        #[serde(default)]
        reason: String,
    },
}

#[derive(Debug, Clone, Serialize)]
pub struct HeartbeatRequest {
    pub capabilities: WorkerCapabilities,
    #[serde(rename = "currentJobId", skip_serializing_if = "Option::is_none")]
    pub current_job_id: Option<String>,
}

// ---------------------------------------------------------------------------
// ModelSource — download spec the studio attaches to every offer so
// the worker can fetch + run any model without per-model knowledge.
// Mirrors `WorkerModelSource` on the TS side.
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ModelFileRole {
    DiffusionModel,
    TextEncoder,
    /// Vision tower / mmproj for a multimodal text encoder (e.g.
    /// Qwen2.5-VL ViT). Maps to `sd-cli --llm_vision`; required by
    /// instruction-edit models that condition on the reference image.
    TextEncoderVision,
    Vae,
    Lora,
    Model,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ModelEngine {
    SdCpp,
    LlamaCpp,
    /// ONNX Runtime image engine (pykeio/ort).  Serves the LaMa
    /// object-removal model used by Find-the-Differences removals.
    Onnx,
    Synthetic,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelFile {
    pub role: ModelFileRole,
    pub url: String,
    pub filename: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub approx_bytes: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelCliDefaults {
    pub cfg_scale: f32,
    pub steps: u32,
    pub width: u32,
    pub height: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sampling_method: Option<String>,
    /// `--flow-shift` for Flow models (SD3.x / WAN / Qwen-Image). Model-level constant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flow_shift: Option<f32>,
    /// `--qwen-image-zero-cond-t` — mandatory for Qwen-Image edit quality.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub zero_cond_t: Option<bool>,
    /// `--offload-to-cpu` — keep weights in RAM, stream to VRAM, so a large model fits a small card.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub offload_to_cpu: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelSource {
    pub engine: ModelEngine,
    pub files: Vec<ModelFile>,
    pub cli_defaults: ModelCliDefaults,
}

// ---------------------------------------------------------------------------
// JobClaim — every offer carries `task` + `model_source` populated by the
// studio's registry resolver.  No legacy `prompt + ext` shadow fields.
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JobClaim {
    pub job_id: String,
    #[allow(dead_code)]
    pub game_id: String,
    pub asset_name: String,
    pub model: String,
    pub vram_gb_estimate: f32,
    /// Structured task payload.  Required — the studio refuses to
    /// promote a job without one.  Worker treats a missing `task`
    /// as a protocol_violation.
    pub task: Task,
    /// Download + engine + CLI defaults the studio resolved from its
    /// model registry.  Required — `synthetic` is just another engine
    /// option, not a fallback for missing rows.
    pub model_source: ModelSource,
}

#[derive(Debug, Clone, Serialize)]
pub struct FailRequest {
    pub error: String,
    pub retryable: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "ui", derive(PartialEq, Eq))]
pub struct LogEntry {
    pub ts: String,
    pub level: String,
    pub category: String,
    pub message: String,
    #[serde(rename = "jobId", default, skip_serializing_if = "Option::is_none")]
    pub job_id: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogBatch {
    pub entries: Vec<LogEntry>,
}

// ---------------------------------------------------------------------------
// Release feed (auto-update)
// ---------------------------------------------------------------------------

/// Subset of the GitHub Releases API we care about.
#[derive(Debug, Clone, Deserialize)]
pub struct GithubRelease {
    pub tag_name: String,
    #[serde(default)]
    pub prerelease: bool,
    #[serde(default)]
    pub draft: bool,
    #[serde(default)]
    pub assets: Vec<GithubReleaseAsset>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GithubReleaseAsset {
    pub name: String,
    pub browser_download_url: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    fn synthetic_model_source_json() -> serde_json::Value {
        serde_json::json!({
            "engine": "synthetic",
            "files": [],
            "cliDefaults": {
                "cfgScale": 1.0,
                "steps": 8,
                "width": 1024,
                "height": 1024,
            },
        })
    }

    #[test]
    fn job_claim_requires_task_and_model_source() {
        // Without `task` or `modelSource` the offer must fail to
        // deserialise — no silent fallback to an Option/Image default.
        let bare = serde_json::json!({
            "jobId": "j-1",
            "gameId": "g-1",
            "assetName": "g-1/creatures/x",
            "model": "synthetic-image",
            "vramGbEstimate": 1.0,
        });
        assert!(
            serde_json::from_value::<JobClaim>(bare).is_err(),
            "JobClaim must reject missing task + modelSource"
        );
    }

    #[test]
    fn job_claim_with_explicit_llm_task() {
        let json = serde_json::json!({
            "jobId": "j-2",
            "gameId": "g-1",
            "assetName": "g-1/conversations/x",
            "model": "llama-3.1-8b",
            "vramGbEstimate": 8.0,
            "task": {
                "kind": "llm",
                "messages": [{"role": "user", "content": "hi"}],
                "maxTokens": 32,
                "temperature": 0.5,
            },
            "modelSource": synthetic_model_source_json(),
        });
        let claim: JobClaim = serde_json::from_value(json).unwrap();
        match claim.task {
            Task::Llm(p) => {
                assert_eq!(p.messages.len(), 1);
                assert_eq!(p.max_tokens, 32);
            }
            other => panic!("expected llm, got {:?}", other),
        }
    }

    #[test]
    fn job_claim_with_explicit_image_task() {
        let json = serde_json::json!({
            "jobId": "j-3",
            "gameId": "g-1",
            "assetName": "g-1/creatures/y",
            "model": "synthetic-image",
            "vramGbEstimate": 8.0,
            "task": {
                "kind": "image",
                "prompt": "a koi",
                "width": 1024,
                "height": 1024,
                "steps": 30,
                "ext": "png",
            },
            "modelSource": synthetic_model_source_json(),
        });
        let claim: JobClaim = serde_json::from_value(json).unwrap();
        match claim.task {
            Task::Image(p) => {
                assert_eq!(p.prompt, "a koi");
                assert_eq!(p.width, 1024);
                assert_eq!(p.ext, "png");
            }
            other => panic!("expected image, got {:?}", other),
        }
    }

    #[test]
    fn image_params_round_trips_with_new_fields() {
        let json = serde_json::json!({
            "kind": "image",
            "prompt": "a stone golem",
            "negativePrompt": "text, watermark, low quality",
            "initImageUrl": "https://example.invalid/t2-golem-stone/latest.webp",
            "denoise": 0.55,
            "cfgScale": 7.5,
            "samplingMethod": "dpm++2m",
            "width": 768,
            "height": 512,
            "steps": 30,
            "seed": 1234,
            "ext": "webp",
        });
        let task: Task = serde_json::from_value(json).unwrap();
        match task {
            Task::Image(p) => {
                assert_eq!(p.prompt, "a stone golem");
                assert_eq!(
                    p.negative_prompt.as_deref(),
                    Some("text, watermark, low quality")
                );
                assert_eq!(
                    p.init_image_url.as_deref(),
                    Some("https://example.invalid/t2-golem-stone/latest.webp")
                );
                assert!((p.denoise.unwrap() - 0.55).abs() < 1e-6);
                assert!((p.cfg_scale.unwrap() - 7.5).abs() < 1e-6);
                assert_eq!(p.sampling_method.as_deref(), Some("dpm++2m"));
                assert_eq!(p.width, 768);
                assert_eq!(p.height, 512);
                assert_eq!(p.steps, 30);
                assert_eq!(p.seed, Some(1234));
            }
            other => panic!("expected image, got {:?}", other),
        }
    }

    #[test]
    fn image_params_defaults_when_optional_fields_absent() {
        let json = serde_json::json!({
            "kind": "image",
            "prompt": "a fox"
        });
        let task: Task = serde_json::from_value(json).unwrap();
        match task {
            Task::Image(p) => {
                assert_eq!(p.prompt, "a fox");
                assert!(p.negative_prompt.is_none());
                assert!(p.init_image_url.is_none());
                assert!(p.denoise.is_none());
                assert!(p.cfg_scale.is_none());
                assert!(p.sampling_method.is_none());
                assert_eq!(p.width, 512);
                assert_eq!(p.height, 512);
                assert_eq!(p.steps, 20);
                assert_eq!(p.ext, "webp");
            }
            other => panic!("expected image, got {:?}", other),
        }
    }

    #[test]
    fn task_kinds_round_trip_via_json() {
        for kind in TaskKind::ALL {
            let s = serde_json::to_string(&kind).unwrap();
            let back: TaskKind = serde_json::from_str(&s).unwrap();
            assert_eq!(kind, back);
        }
    }
}