File size: 44,305 Bytes
415a17d
55b1a24
4c208f2
213c234
415a17d
 
55b1a24
edc4c90
213c234
8db63ba
fd1786e
5fe1a3d
415a17d
55b1a24
415a17d
782c122
24561f7
55b1a24
415a17d
 
 
55b1a24
 
415a17d
55b1a24
415a17d
 
 
 
e223b2b
415a17d
 
e223b2b
415a17d
27fe49f
213c234
 
 
 
 
 
 
 
 
55b1a24
213c234
55b1a24
 
 
213c234
 
 
 
 
 
 
 
 
 
 
 
 
 
415a17d
24561f7
415a17d
 
 
 
 
 
213c234
 
 
 
 
 
 
 
 
 
415a17d
 
 
 
 
 
782c122
415a17d
 
 
782c122
 
 
 
 
d9c705e
 
 
782c122
 
 
 
 
415a17d
 
5fe1a3d
55b1a24
600cde8
415a17d
 
 
e6edab1
 
 
 
 
 
 
 
 
 
 
 
 
 
415a17d
 
 
 
 
e6edab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415a17d
 
 
 
 
 
 
e6edab1
381170a
 
 
782c122
381170a
 
782c122
381170a
e6edab1
782c122
 
e6edab1
782c122
e6edab1
 
 
415a17d
 
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9774b79
782c122
 
 
9774b79
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
415a17d
782c122
 
415a17d
782c122
 
415a17d
 
782c122
55b1a24
415a17d
08f60f4
 
 
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6edab1
9774b79
782c122
 
 
9774b79
782c122
 
 
 
 
 
544b046
782c122
e6edab1
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08f60f4
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6edab1
782c122
 
 
 
 
 
 
 
 
415a17d
782c122
 
 
 
415a17d
782c122
 
415a17d
 
782c122
 
2531fcd
55b1a24
2531fcd
 
 
 
 
 
 
e6edab1
 
2531fcd
e6edab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edc4c90
 
d9c705e
edc4c90
55b1a24
d9c705e
 
 
 
 
edc4c90
d9c705e
edc4c90
d9c705e
55b1a24
d9c705e
 
 
 
 
e6edab1
 
 
 
 
415a17d
 
 
d9c705e
 
 
782c122
 
d9c705e
 
b097ab2
 
08f60f4
782c122
 
fd1786e
08f60f4
782c122
 
fd1786e
ac7c05c
 
782c122
 
 
 
 
55b1a24
08f60f4
782c122
fd1786e
ac7c05c
 
 
 
 
b097ab2
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
fd1786e
ac7c05c
 
 
 
 
 
 
 
 
7a039f4
 
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08f60f4
 
 
ac7c05c
08f60f4
782c122
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c208f2
 
 
 
 
ac7c05c
 
 
 
 
 
 
 
1ee5994
 
 
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
08f60f4
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a039f4
 
 
 
 
 
 
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df84ef5
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
08f60f4
 
 
ac7c05c
 
 
 
 
 
 
 
 
 
 
 
08f60f4
 
 
782c122
d9c705e
 
782c122
 
 
 
 
 
 
9774b79
782c122
 
 
9774b79
782c122
 
 
 
 
 
 
 
 
 
 
 
 
d9c705e
 
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8ca9dc
 
782c122
d8ca9dc
782c122
 
d8ca9dc
 
 
 
 
782c122
d8ca9dc
 
 
 
 
782c122
 
 
 
 
 
 
 
 
 
 
 
 
 
d9c705e
 
 
 
 
 
 
13cae40
 
 
d9c705e
 
 
 
081e575
 
 
 
 
d8ca9dc
081e575
 
 
 
 
 
 
 
d8ca9dc
 
 
081e575
 
 
 
 
 
 
 
 
3c39754
 
d8ca9dc
 
13cae40
 
ac7c05c
 
3c39754
ac7c05c
 
 
3c39754
 
ac7c05c
 
 
782c122
8e9e5e5
ac7c05c
 
 
 
13cae40
 
 
ac7c05c
 
c076000
df84ef5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac7c05c
 
df84ef5
 
3c9abfc
ac7c05c
 
 
 
 
 
 
 
 
3c9abfc
 
55b1a24
 
d9c705e
c076000
d9c705e
 
 
 
 
 
 
 
 
55b1a24
 
600cde8
 
 
 
 
544b046
55b1a24
544b046
55b1a24
 
 
 
600cde8
55b1a24
600cde8
55b1a24
 
600cde8
 
544b046
55b1a24
 
 
 
 
 
 
544b046
600cde8
55b1a24
8db63ba
23a36f6
5fe1a3d
600cde8
55b1a24
 
 
 
 
 
544b046
600cde8
 
 
 
415a17d
 
 
 
 
55b1a24
 
415a17d
55b1a24
415a17d
 
 
 
 
 
55b1a24
415a17d
 
 
 
 
 
 
 
 
 
 
55b1a24
415a17d
 
 
 
 
782c122
 
081e575
d9c705e
 
782c122
 
415a17d
ab8a147
415a17d
 
 
 
 
 
 
55b1a24
415a17d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
<script lang="ts">
  import type { PicletGeneratorProps, PicletWorkflowState, CaptionType, CaptionLength, PicletStats } from '$lib/types';
  import { Nature } from '$lib/types';
  import type { PicletInstance } from '$lib/db/schema';
  import UploadStep from './UploadStep.svelte';
  import WorkflowProgress from './WorkflowProgress.svelte';
  import PicletResult from './PicletResult.svelte';
  import { removeBackground } from '$lib/utils/professionalImageProcessing';
  import { extractPicletMetadata } from '$lib/services/picletMetadata';
  import { savePicletInstance, generatedDataToPicletInstance } from '$lib/db/piclets';
  import { PicletType, TYPE_DATA } from '$lib/types/picletTypes';
  import { EncounterService } from '$lib/db/encounterService';
  
  interface Props extends PicletGeneratorProps {}
  
  let { joyCaptionClient, zephyrClient, fluxClient, qwenClient }: Props = $props();
  
  let state: PicletWorkflowState = $state({
    currentStep: 'upload',
    userImage: null,
    imageCaption: null,
    picletConcept: null,
    picletStats: null,
    imagePrompt: null,
    picletImage: null,
    error: null,
    isProcessing: false
  });
  
  const IMAGE_GENERATION_PROMPT = (concept: string) => `Extract ONLY the visual appearance from this monster concept and describe it in one concise sentence:
"${concept}"

Focus on: colors, body shape, eyes, limbs, mouth, and key visual features. Omit backstory, abilities, and non-visual details.`;
  

  async function importPiclet(picletData: PicletInstance) {
    state.isProcessing = true;
    state.currentStep = 'complete';
    
    try {
      // Save the imported piclet
      const savedId = await savePicletInstance(picletData);
      
      // Create a success state similar to generation
      state.picletImage = {
        imageUrl: picletData.imageUrl,
        imageData: picletData.imageData,
        seed: 0,
        prompt: 'Imported piclet'
      };
      
      // Show import success
      state.isProcessing = false;
      alert(`Successfully imported ${picletData.nickname || picletData.typeId}!`);
      
      // Reset to allow another import/generation
      setTimeout(() => reset(), 2000);
    } catch (error) {
      state.error = `Failed to import piclet: ${error}`;
      state.isProcessing = false;
    }
  }

  async function handleImageSelected(file: File) {
    if (!joyCaptionClient || !fluxClient) {
      state.error = "Services not connected. Please wait...";
      return;
    }
    
    state.userImage = file;
    state.error = null;
    
    // Check if this is a piclet card with metadata
    const picletData = await extractPicletMetadata(file);
    if (picletData) {
      // Import existing piclet
      await importPiclet(picletData);
    } else {
      // Generate new piclet
      startWorkflow();
    }
  }
  
  async function startWorkflow() {
    state.isProcessing = true;
    
    try {
      // Step 1: Generate detailed object description with joy-caption
      await captionImage();
      await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for state update
      
      // Step 2: Generate free-form monster concept with qwen3
      await generateConcept();
      await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for state update
      
      // Step 3: Generate structured monster stats based on both caption and concept
      await generateStats();
      await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for state update
      
      // Step 4: Generate image prompt with qwen3
      await generateImagePrompt();
      await new Promise(resolve => setTimeout(resolve, 100)); // Small delay for state update
      
      // Step 5: Generate monster image
      await generateMonsterImage();
      
      // Step 6: Auto-save the piclet as uncaught
      await autoSavePiclet();
      
      state.currentStep = 'complete';
    } catch (err) {
      console.error('Workflow error:', err);
      
      // Check for GPU quota error
      if (err && typeof err === 'object' && 'message' in err) {
        const errorMessage = String(err.message);
        if (errorMessage.includes('exceeded your GPU quota') || errorMessage.includes('GPU quota')) {
          state.error = 'GPU quota exceeded! You need to sign in with Hugging Face for free GPU time, or upgrade to Hugging Face Pro for more quota.';
        } else {
          state.error = errorMessage;
        }
      } else if (err instanceof Error) {
        state.error = err.message;
      } else {
        state.error = 'An unknown error occurred';
      }
    } finally {
      state.isProcessing = false;
    }
  }
  
  function handleAPIError(error: any): never {
    console.error('API Error:', error);
    
    // Check if it's a GPU quota error
    if (error && typeof error === 'object' && 'message' in error) {
      const errorMessage = String(error.message);
      if (errorMessage.includes('exceeded your GPU quota') || errorMessage.includes('GPU quota')) {
        throw new Error('GPU quota exceeded! You need to sign in with Hugging Face for free GPU time, or upgrade to Hugging Face Pro for more quota.');
      }
      throw new Error(errorMessage);
    }
    
    // Check if error has a different structure (like the status object from the logs)
    if (error && typeof error === 'object' && 'type' in error && error.type === 'status') {
      const statusError = error as any;
      if (statusError.message && statusError.message.includes('GPU quota')) {
        throw new Error('GPU quota exceeded! You need to sign in with Hugging Face for free GPU time, or upgrade to Hugging Face Pro for more quota.');
      }
      throw new Error(statusError.message || 'API request failed');
    }
    
    throw error;
  }
  
  async function captionImage() {
    state.currentStep = 'captioning';
    
    if (!joyCaptionClient || !state.userImage) {
      throw new Error('Caption service not available or no image provided');
    }
    
    try {
      const output = await joyCaptionClient.predict("/stream_chat", [
        state.userImage,  // input_image
        "Descriptive",  // caption_type
        "long",  // caption_length
        [],  // extra_options
        "",  // name_input
        ""  // custom_prompt (empty for default descriptive captioning)
      ]);
      
      const [, caption] = output.data;
      // Store the detailed object description
      state.imageCaption = caption;
      console.log('Detailed object description generated:', caption);
    } catch (error) {
      handleAPIError(error);
    }
  }
  
  async function generateConcept() {
    state.currentStep = 'conceptualizing';
    
    if (!qwenClient || !state.imageCaption) {
      throw new Error('Qwen service not available or no image caption provided');
    }
    
    const conceptPrompt = `Based on this detailed object description, create a PokΓ©mon-style monster that transforms the object into an imaginative creature. The monster should clearly be inspired by the object's appearance but reimagined as a living monster.

Object description: "${state.imageCaption}"

Guidelines:
- Take the object's key visual elements (colors, shapes, materials) incorporating all of them into a single creature design
- Add eyes (can be glowing, mechanical, multiple, etc.) positioned where they make sense
- Include limbs (legs, arms, wings, tentacles) that grow from or replace parts of the object
- Add a mouth, beak, or feeding apparatus if appropriate
- Add creature elements like tail, fins, claws, horns, etc where fitting

Format your response exactly as follows:

# Object Rarity
{Assess how rare the object is based on real-world availability and value. Rare objects give strong monsters while common objects give weak ones. Use: common, uncommon, rare, or legendary}

# Monster Name
{Creative name that hints at the original object}

## Monster Visual Description
{Detailed physical description showing how the object becomes a creature. Ensure the creature uses all the unique attributes of the object. Include colors, shapes, materials, eyes, limbs, mouth, and distinctive features. This section should be comprehensive as it will be used for both stats generation and image creation.}`;
    
    try {
      // Create the required state structure based on qwen.html
      const defaultState = {
        "conversation_contexts": {},
        "conversations": [],
        "conversation_id": "",
      };
      
      // Create default settings based on qwen.html with minimal thinking tokens
      const defaultSettings = {
        "model": "qwen3-235b-a22b",
        "sys_prompt": "You are a creative monster designer specializing in transforming everyday objects into imaginative PokΓ©mon-style creatures. Follow the exact format provided and create detailed, engaging descriptions that bring these monsters to life.",
        "thinking_budget": 3
      };
      
      // Create thinking button state
      const thinkingBtnState = {
        "enable_thinking": true
      };
      
      console.log('Generating monster concept with qwen3...');
      
      // Call the add_message function (fn_index 13)
      const output = await qwenClient.predict(13, [
        conceptPrompt,          // input_value
        defaultSettings,        // settings_form_value  
        thinkingBtnState,       // thinking_btn_state_value
        defaultState            // state_value
      ]);
      
      console.log('Qwen3 concept response:', output);
      
      // Extract the response text from the output
      let responseText = "";
      if (output && output.data && Array.isArray(output.data)) {
        // The chatbot response is at index 5 in the outputs array
        const chatbotUpdate = output.data[5];
        
        if (chatbotUpdate && chatbotUpdate.value && Array.isArray(chatbotUpdate.value)) {
          const chatHistory = chatbotUpdate.value;
          
          if (chatHistory.length > 0) {
            // Get the last message (assistant's response)
            const lastMessage = chatHistory[chatHistory.length - 1];
            
            if (lastMessage && lastMessage.content && Array.isArray(lastMessage.content)) {
              // Extract text content from the message
              const textContents = lastMessage.content
                .filter((item: any) => item.type === "text")
                .map((item: any) => item.content)
                .join("\n");
              responseText = textContents || "Response received but no text content found";
            } else if (lastMessage && lastMessage.role === "assistant") {
              // Fallback - if content structure is different
              responseText = JSON.stringify(lastMessage, null, 2);
            }
          }
        }
      }
      
      if (!responseText || responseText.trim() === '') {
        throw new Error('Failed to generate monster concept');
      }
      
      state.picletConcept = responseText;
      console.log('Monster concept generated:', responseText);
    } catch (error) {
      handleAPIError(error);
    }
  }
  
  async function generateImagePrompt() {
    state.currentStep = 'promptCrafting';
    
    if (!qwenClient || !state.picletConcept || !state.imageCaption) {
      throw new Error('Qwen service not available or no concept/caption available for prompt generation');
    }
    
    // Extract the Monster Visual Description from the structured concept
    const visualDescMatch = state.picletConcept.match(/## Monster Visual Description\s*\n([\s\S]*?)(?=##|$)/);
    
    if (visualDescMatch && visualDescMatch[1]) {
      state.imagePrompt = visualDescMatch[1].trim();
      console.log('Extracted visual description for image generation:', state.imagePrompt);
      return; // Skip qwen3 call since we have the description
    }
    
    // Fallback: if format parsing fails, use qwen3 to extract visual description
    const imagePromptPrompt = `Based on this monster concept, extract ONLY the visual description for image generation:

MONSTER CONCEPT:
"""
${state.picletConcept}
"""

Create a concise visual description (1-3 sentences, max 100 words). Focus only on colors, shapes, materials, eyes, limbs, mouth, and distinctive features. Omit all non-visual information like abilities and backstory.`;
    
    try {
      // Create the required state structure based on qwen.html
      const defaultState = {
        "conversation_contexts": {},
        "conversations": [],
        "conversation_id": "",
      };
      
      // Create default settings based on qwen.html with minimal thinking tokens
      const defaultSettings = {
        "model": "qwen3-235b-a22b",
        "sys_prompt": "You are an expert at creating concise visual descriptions for image generation. Extract ONLY visual appearance details and describe them in 1-2 sentences (max 50 words). Focus on colors, shape, eyes, limbs, and distinctive features. Omit all non-visual information like abilities, personality, or backstory.",
        "thinking_budget": 3
      };
      
      // Create thinking button state
      const thinkingBtnState = {
        "enable_thinking": true
      };
      
      console.log('Generating image prompt with qwen3...');
      
      // Call the add_message function (fn_index 13)
      const output = await qwenClient.predict(13, [
        imagePromptPrompt,      // input_value
        defaultSettings,        // settings_form_value  
        thinkingBtnState,       // thinking_btn_state_value
        defaultState            // state_value
      ]);
      
      console.log('Qwen3 image prompt response:', output);
      
      // Extract the response text from the output using the same pattern as generateConcept
      let responseText = "";
      if (output && output.data && Array.isArray(output.data)) {
        // The chatbot response is at index 5 in the outputs array
        const chatbotUpdate = output.data[5];
        
        if (chatbotUpdate && chatbotUpdate.value && Array.isArray(chatbotUpdate.value)) {
          const chatHistory = chatbotUpdate.value;
          
          if (chatHistory.length > 0) {
            // Get the last message (assistant's response)
            const lastMessage = chatHistory[chatHistory.length - 1];
            
            if (lastMessage && lastMessage.content && Array.isArray(lastMessage.content)) {
              // Extract text content from the message
              const textContents = lastMessage.content
                .filter((item: any) => item.type === "text")
                .map((item: any) => item.content)
                .join("\n");
              responseText = textContents || "Response received but no text content found";
            } else if (lastMessage && lastMessage.role === "assistant") {
              // Fallback - if content structure is different
              responseText = JSON.stringify(lastMessage, null, 2);
            }
          }
        }
      }
      
      if (!responseText || responseText.trim() === '') {
        throw new Error('Failed to generate image prompt');
      }
      
      state.imagePrompt = responseText.trim();
      console.log('Image prompt generated:', state.imagePrompt);
    } catch (error) {
      handleAPIError(error);
    }
  }
  
  async function generateMonsterImage() {
    state.currentStep = 'generating';
    
    if (!fluxClient || !state.imagePrompt || !state.picletStats) {
      throw new Error('Image generation service not available or no prompt/stats');
    }
    
    // The image prompt should already be generated by generateImagePrompt() in the workflow
    
    // Get tier for image quality enhancement
    const tier = state.picletStats.tier || 'medium';
    const tierDescriptions = {
      low: 'simple and basic design',
      medium: 'detailed and well-crafted design', 
      high: 'highly detailed and impressive design with special effects',
      legendary: 'extremely detailed and majestic design with dramatic lighting and aura effects'
    };
    
    try {
      const output = await fluxClient.predict("/infer", [
        `${state.imagePrompt}\nNow generate a PokΓ©mon-Anime-style image of the monster in an idle pose with a white background. This is a ${tier} tier monster with ${tierDescriptions[tier as keyof typeof tierDescriptions]}. The monster should not be attacking or in motion. The full monster must be visible within the frame.`,
        0,      // seed
        true,   // randomizeSeed
        1024,   // width
        1024,   // height
        4       // steps
      ]);
      
      const [image, usedSeed] = output.data;
      let url: string | undefined;
      
      if (typeof image === "string") url = image;
      else if (image && image.url) url = image.url;
      else if (image && image.path) url = image.path;
      
      if (url) {
        // Process the image to remove background using professional AI method
        console.log('Processing image for background removal...');
        try {
          const transparentBase64 = await removeBackground(url);
          state.picletImage = {
            imageUrl: url,
            imageData: transparentBase64,
            seed: usedSeed,
            prompt: state.imagePrompt
          };
          console.log('Background removal completed successfully');
        } catch (processError) {
          console.error('Failed to process image for background removal:', processError);
          // Fallback to original image
          state.picletImage = {
            imageUrl: url,
            seed: usedSeed,
            prompt: state.imagePrompt
          };
        }
      } else {
        throw new Error('Failed to generate monster image');
      }
    } catch (error) {
      handleAPIError(error);
    }
  }
  
  async function generateStats() {
    state.currentStep = 'statsGenerating';
    
    if (!qwenClient || !state.picletConcept || !state.imageCaption) {
      throw new Error('Qwen service not available or no concept/caption available for stats generation');
    }
    
    // Default tier (will be set from the generated stats)
    let tier: 'low' | 'medium' | 'high' | 'legendary' = 'medium';
    
    // Extract monster name and rarity from the structured concept
    const monsterNameMatch = state.picletConcept.match(/# Monster Name\s*\n([\s\S]*?)(?=^##|$)/m);
    const monsterName = monsterNameMatch ? monsterNameMatch[1].trim() : 'Unknown Monster';
    
    const rarityMatch = state.picletConcept.match(/# Object Rarity\s*\n([\s\S]*?)(?=^#)/m);
    const objectRarity = rarityMatch ? rarityMatch[1].trim().toLowerCase() : 'common';
    
    // Create comprehensive battle-ready monster prompt
    const statsPrompt = `Based on this detailed object description and monster concept, create a complete battle-ready monster for the Pictuary Battle System:

ORIGINAL OBJECT DESCRIPTION:
"${state.imageCaption}"

MONSTER CONCEPT:
"${state.picletConcept}"

The object rarity has been assessed as: ${objectRarity}

## BATTLE SYSTEM OVERVIEW
This monster will be used in a turn-based battle system with composable effects. You must create:
1. **Base Stats**: Core combat statistics  
2. **Special Ability**: Passive trait with triggers and effects
3. **Movepool**: 4 battle moves with complex effect combinations

## TYPE SYSTEM
Choose the primary type (and optional secondary type) based on the object:
β€’ **beast**: Vertebrate wildlife β€” mammals, birds, reptiles. Raw physicality, instincts
β€’ **bug**: Arthropods β€” butterflies, beetles, mantises. Agile swarms, precision strikes  
β€’ **aquatic**: Life that swims, dives, sloshes β€” fish, octopus, sentient puddles. Tides, pressure
β€’ **flora**: Plants and fungi β€” blooming or decaying. Growth, spores, vines, seasonal shifts
β€’ **mineral**: Stones, crystals, metals β€” earth's depths. Durability, reflective armor, seismic shocks
β€’ **space**: Stars, moon, cosmic objects β€” not of this world. Celestial energy, gravitational effects
β€’ **machina**: Engineered devices β€” gadgets to machinery. Gears, circuits, drones, power surges
β€’ **structure**: Buildings, bridges, monuments β€” architectural titans. Fortification, terrain shaping
β€’ **culture**: Art, fashion, toys, symbols β€” creative expressions. Buffs, debuffs, illusion, stories
β€’ **cuisine**: Dishes, drinks, culinary art β€” flavors and aromas. Temperature, restorative effects

## EFFECT SYSTEM
All abilities and moves use these **atomic building blocks**:

### **Effect Types:**
1. **damage**: Deal damage (weak/normal/strong/extreme) with formulas (standard/recoil/drain/fixed/percentage)
2. **modifyStats**: Change stats (increase/decrease/greatly_increase/greatly_decrease) for hp/attack/defense/speed/accuracy  
3. **applyStatus**: Apply status effects (burn/freeze/paralyze/poison/sleep/confuse) with chance percentage
4. **heal**: Restore HP (small/medium/large/full) or by percentage/fixed amounts
5. **manipulatePP**: Drain, restore, or disable PP from moves
6. **fieldEffect**: Persistent battlefield modifications (reflect/lightScreen for defense, spikes for entry damage, mist for stat protection, healingField for regeneration, etc.)
7. **counter**: Reflect damage
8. **priority**: Modify move priority (-5 to +5)
9. **removeStatus**: Cure specific status conditions
10. **mechanicOverride**: Modify core game mechanics (immunity, type changes, etc.)

### **Targets:**
- **self**: The move user
- **opponent**: The target opponent  
- **all**: All combatants
- **allies**: Allied creatures (team battles)
- **field**: Entire battlefield

### **Conditions:**
- **always**: Effect always applies
- **onHit**: Only if move hits successfully  
- **afterUse**: After move execution regardless of hit/miss
- **onCritical**: Only on critical hits
- **ifLowHp**: If user's HP < 25%
- **ifHighHp**: If user's HP > 75%

### **Move Flags:**
Moves can have flags affecting interactions:
- **contact**: Makes physical contact (triggers contact abilities)
- **explosive**: Explosive move (affected by explosion-related abilities)
- **draining**: Drains HP from target
- **priority**: Natural priority move (+1 to +5)
- **sacrifice**: Involves self-sacrifice or major cost
- **reckless**: High power with drawbacks

## SPECIAL ABILITY TRIGGERS
Special abilities activate on specific events:
- **onSwitchIn**: When entering battle
- **onDamageTaken**: When this monster takes damage
- **onContactDamage**: When hit by a contact move
- **endOfTurn**: At the end of each turn
- **onLowHP**: When HP drops below 25%
- **onStatusInflicted**: When a status is applied

## BALANCING GUIDELINES
**Stat Ranges by Rarity:**
- **common**: 45-80 total stats, individual stats 10-25
- **uncommon**: 80-120 total stats, individual stats 15-35  
- **rare**: 120-160 total stats, individual stats 25-45
- **legendary**: 160-200+ total stats, individual stats 35-50

**Design Philosophy:**
- **Risk-Reward**: Powerful moves must have meaningful drawbacks
- **Type Synergy**: Moves should match the monster's type and concept
- **Strategic Depth**: Abilities should create interesting decision points
- **No Strictly Better**: Every powerful effect has a cost or condition

The output should be formatted as a JSON instance that conforms to the schema below.

\`\`\`json
{
  "type": "object",
  "properties": {
    "name": {"type": "string", "description": "Creative name for the monster that hints at the original object"},
    "description": {"type": "string", "description": "Flavor text describing the monster (2-3 sentences)"},
    "tier": {"type": "string", "enum": ["low", "medium", "high", "legendary"], "description": "Power tier based on rarity: common=low, uncommon=medium, rare=high, legendary=legendary"},
    "primaryType": {"type": "string", "enum": ["beast", "bug", "aquatic", "flora", "mineral", "space", "machina", "structure", "culture", "cuisine"], "description": "Primary type based on object characteristics"},
    "secondaryType": {"type": ["string", "null"], "enum": ["beast", "bug", "aquatic", "flora", "mineral", "space", "machina", "structure", "culture", "cuisine", null], "description": "Optional secondary type for dual-type monsters"},
    "baseStats": {
      "type": "object",
      "properties": {
        "hp": {"type": "integer", "minimum": 10, "maximum": 50, "description": "Hit points"},
        "attack": {"type": "integer", "minimum": 10, "maximum": 50, "description": "Attack power"},
        "defense": {"type": "integer", "minimum": 10, "maximum": 50, "description": "Defensive capability"},
        "speed": {"type": "integer", "minimum": 10, "maximum": 50, "description": "Speed and agility"}
      },
      "required": ["hp", "attack", "defense", "speed"],
      "additionalProperties": false
    },
    "nature": {
      "type": "string", 
      "enum": ["hardy", "docile", "serious", "bashful", "quirky", "lonely", "brave", "adamant", "naughty", "bold", "relaxed", "impish", "lax", "timid", "hasty", "jolly", "naive", "modest", "mild", "quiet", "gentle", "sassy", "careful", "calm", "reckless"],
      "description": "Personality trait affecting behavior and battle style"
    },
    "specialAbility": {
      "type": "object",
      "properties": {
        "name": {"type": "string", "description": "Name of the special ability"},
        "description": {"type": "string", "description": "Description of what the ability does"},
        "triggers": {
          "type": "array",
          "items": {"$ref": "#/definitions/Trigger"},
          "minItems": 1,
          "maxItems": 1,
          "description": "Single trigger effect for the special ability"
        }
      },
      "required": ["name", "description"],
      "additionalProperties": false
    },
    "movepool": {
      "type": "array",
      "items": {"$ref": "#/definitions/Move"},
      "minItems": 4,
      "maxItems": 4,
      "description": "Exactly 4 battle moves"
    }
  },
  "required": ["name", "description", "tier", "primaryType", "baseStats", "nature", "specialAbility", "movepool"],
  "additionalProperties": false,
  "definitions": {
    "Effect": {
      "type": "object",
      "properties": {
        "type": {"type": "string", "enum": ["damage", "modifyStats", "applyStatus", "heal", "manipulatePP", "fieldEffect", "counter", "priority", "removeStatus", "mechanicOverride"]},
        "target": {"type": "string", "enum": ["self", "opponent", "allies", "all", "attacker", "field", "playerSide", "opponentSide"]},
        "condition": {"type": "string", "enum": ["always", "onHit", "afterUse", "onCritical", "ifLowHp", "ifHighHp", "thisTurn", "nextTurn", "restOfBattle"]}
      },
      "required": ["type"],
      "allOf": [
        {"if": {"properties": {"type": {"const": "damage"}}}, "then": {"properties": {"amount": {"enum": ["weak", "normal", "strong", "extreme"]}, "formula": {"enum": ["standard", "recoil", "drain", "fixed", "percentage"]}, "value": {"type": "number"}}}},
        {"if": {"properties": {"type": {"const": "modifyStats"}}}, "then": {"properties": {"stats": {"type": "object", "properties": {"hp": {"enum": ["increase", "decrease", "greatly_increase", "greatly_decrease"]}, "attack": {"enum": ["increase", "decrease", "greatly_increase", "greatly_decrease"]}, "defense": {"enum": ["increase", "decrease", "greatly_increase", "greatly_decrease"]}, "speed": {"enum": ["increase", "decrease", "greatly_increase", "greatly_decrease"]}, "accuracy": {"enum": ["increase", "decrease", "greatly_increase", "greatly_decrease"]}}}}}},
        {"if": {"properties": {"type": {"const": "applyStatus"}}}, "then": {"properties": {"status": {"enum": ["burn", "freeze", "paralyze", "poison", "sleep", "confuse"]}, "chance": {"type": "number", "minimum": 1, "maximum": 100}}}},
        {"if": {"properties": {"type": {"const": "heal"}}}, "then": {"properties": {"amount": {"enum": ["small", "medium", "large", "full"]}, "formula": {"enum": ["percentage", "fixed"]}, "value": {"type": "number"}}}},
        {"if": {"properties": {"type": {"const": "fieldEffect"}}}, "then": {"properties": {"effect": {"enum": ["reflect", "lightScreen", "spikes", "healingMist", "toxicSpikes"]}, "stackable": {"type": "boolean"}}}},
        {"if": {"properties": {"type": {"const": "manipulatePP"}}}, "then": {"properties": {"action": {"enum": ["drain", "restore", "disable"]}, "amount": {"enum": ["small", "medium", "large"]}}}},
        {"if": {"properties": {"type": {"const": "counter"}}}, "then": {"properties": {"strength": {"enum": ["weak", "normal", "strong"]}}}},
        {"if": {"properties": {"type": {"const": "priority"}}}, "then": {"properties": {"value": {"type": "integer", "minimum": -5, "maximum": 5}}}},
        {"if": {"properties": {"type": {"const": "removeStatus"}}}, "then": {"properties": {"status": {"enum": ["burn", "freeze", "paralyze", "poison", "sleep", "confuse"]}}}},
        {"if": {"properties": {"type": {"const": "mechanicOverride"}}}, "then": {"properties": {"mechanic": {"enum": ["criticalHits", "statusImmunity", "damageReflection", "damageAbsorption", "damageCalculation", "damageMultiplier", "healingInversion", "healingBlocked", "priorityOverride", "accuracyBypass", "typeImmunity", "typeChange", "contactDamage", "drainInversion", "weatherImmunity", "flagImmunity", "flagWeakness", "flagResistance", "statModification", "targetRedirection", "extraTurn"]}, "value": {}}}}
      ]
    },
    "Trigger": {
      "type": "object",
      "properties": {
        "event": {"type": "string", "enum": ["onSwitchIn", "onDamageTaken", "onContactDamage", "endOfTurn", "onLowHP", "onStatusInflicted", "beforeMoveUse", "afterMoveUse"]},
        "condition": {"type": "string", "enum": ["always", "ifLowHp", "ifHighHp", "ifStatus:burn", "ifStatus:freeze", "ifStatus:paralyze", "ifStatus:poison", "ifStatus:sleep", "ifStatus:confuse"]},
        "effects": {"type": "array", "items": {"$ref": "#/definitions/Effect"}, "minItems": 1}
      },
      "required": ["event", "effects"]
    },
    "Move": {
      "type": "object",
      "properties": {
        "name": {"type": "string", "description": "Name of the move"},
        "description": {"type": "string", "description": "Description of what the move does"},
        "type": {"type": "string", "enum": ["beast", "bug", "aquatic", "flora", "mineral", "space", "machina", "structure", "culture", "cuisine", "normal"], "description": "Move type for STAB and effectiveness"},
        "power": {"type": "integer", "minimum": 0, "maximum": 250, "description": "Base power (0 for status moves)"},
        "accuracy": {"type": "integer", "minimum": 30, "maximum": 100, "description": "Hit chance percentage"},
        "pp": {"type": "integer", "minimum": 1, "maximum": 40, "description": "Power points (uses per battle)"},
        "priority": {"type": "integer", "minimum": -5, "maximum": 5, "description": "Priority bracket"},
        "flags": {"type": "array", "items": {"enum": ["contact", "explosive", "draining", "priority", "sacrifice", "reckless", "bite", "punch", "sound", "ground"]}, "description": "Move characteristics"},
        "effects": {"type": "array", "items": {"$ref": "#/definitions/Effect"}, "minItems": 1, "description": "What the move does"}
      },
      "required": ["name", "type", "power", "accuracy", "pp", "priority", "flags", "effects"],
      "additionalProperties": false
    }
  }
}
\`\`\`

**STAT GUIDELINES:**
Base the tier and stats on the object rarity:
- **common β†’ low tier**: hp/attack/defense/speed should be 10-25 (total ~40-80)
- **uncommon β†’ medium tier**: hp/attack/defense/speed should be 15-35 (total ~80-120)
- **rare β†’ high tier**: hp/attack/defense/speed should be 25-45 (total ~120-160)
- **legendary β†’ legendary tier**: hp/attack/defense/speed should be 35-50 (total ~160-200)

**MOVE DESIGN EXAMPLES:**
- **Basic Attack**: {"type": "damage", "target": "opponent", "amount": "normal"}
- **Status Move**: {"type": "applyStatus", "target": "opponent", "status": "burn", "chance": 30}
- **Self-Buff**: {"type": "modifyStats", "target": "self", "stats": {"attack": "increase"}}
- **Risk-Reward**: High power + {"type": "damage", "target": "self", "formula": "recoil", "value": 0.25}

Write your response within \`\`\`json\`\`\``;
    
    console.log('Generating monster stats with qwen3');
    
    try {
      // Create the required state structure based on qwen.html
      const defaultState = {
        "conversation_contexts": {},
        "conversations": [],
        "conversation_id": "",
      };
      
      // Create default settings based on qwen.html with minimal thinking tokens
      const defaultSettings = {
        "model": "qwen3-235b-a22b",
        "sys_prompt": "You are a game designer specializing in monster stats and abilities. You must ONLY output valid JSON that matches the provided schema exactly. Do not include any text before or after the JSON. Do not include null values in your JSON response. Your entire response should be wrapped in a ```json``` code block.",
        "thinking_budget": 3
      };
      
      // Create thinking button state
      const thinkingBtnState = {
        "enable_thinking": true
      };
      
      // Call the add_message function (fn_index 13)
      const output = await qwenClient.predict(13, [
        statsPrompt,            // input_value
        defaultSettings,        // settings_form_value  
        thinkingBtnState,       // thinking_btn_state_value
        defaultState            // state_value
      ]);
      
      console.log('Qwen3 stats response:', output);
      
      // Extract the response text from the output using the same pattern as generateConcept
      let responseText = "";
      if (output && output.data && Array.isArray(output.data)) {
        // The chatbot response is at index 5 in the outputs array
        const chatbotUpdate = output.data[5];
        
        if (chatbotUpdate && chatbotUpdate.value && Array.isArray(chatbotUpdate.value)) {
          const chatHistory = chatbotUpdate.value;
          
          if (chatHistory.length > 0) {
            // Get the last message (assistant's response)
            const lastMessage = chatHistory[chatHistory.length - 1];
            
            console.log('Full message structure:', JSON.stringify(lastMessage, null, 2));
            
            if (lastMessage && lastMessage.content && Array.isArray(lastMessage.content)) {
              // Extract ALL text content from the message more robustly
              const textContents = lastMessage.content
                .filter((item: any) => item.type === "text")
                .map((item: any) => {
                  console.log('Content item:', item);
                  return item.content || '';
                })
                .join("");  // Join without separator to avoid breaking JSON
              responseText = textContents || "Response received but no text content found";
              console.log('Extracted text length:', responseText.length);
              console.log('Extracted text preview:', responseText.substring(0, 200) + '...');
            } else if (lastMessage && typeof lastMessage === 'string') {
              // Handle case where the message is a plain string
              responseText = lastMessage;
            } else if (lastMessage && lastMessage.role === "assistant") {
              // Fallback - if content structure is different
              responseText = JSON.stringify(lastMessage, null, 2);
            }
          }
        }
      }
      
      if (!responseText || responseText.trim() === '') {
        throw new Error('Failed to generate monster stats');
      }
      
      console.log('Stats output:', responseText);
      let jsonString = responseText;
      
      // Extract JSON from the response (remove markdown if present)
      let cleanJson = jsonString;
      if (jsonString.includes('```')) {
        const matches = jsonString.match(/```(?:json)?\s*([\s\S]*?)```/);
        if (matches) {
          cleanJson = matches[1];
        } else {
          // If no closing ```, just remove the opening ```json
          cleanJson = jsonString.replace(/^```(?:json)?\s*/, '').replace(/```\s*$/, '');
        }
      }
      
      try {
        // Extract JSON by properly balancing braces instead of using regex
        const startIndex = cleanJson.indexOf('{');
        if (startIndex !== -1) {
          let braceCount = 0;
          let endIndex = -1;
          
          // Find the matching closing brace by counting
          for (let i = startIndex; i < cleanJson.length; i++) {
            if (cleanJson[i] === '{') braceCount++;
            if (cleanJson[i] === '}') {
              braceCount--;
              if (braceCount === 0) {
                endIndex = i;
                break;
              }
            }
          }
          
          if (endIndex !== -1) {
            cleanJson = cleanJson.substring(startIndex, endIndex + 1);
            console.log('Balanced JSON extracted, length:', cleanJson.length);
          } else {
            throw new Error('JSON appears to be truncated - unable to balance braces');
          }
        } else {
          throw new Error('No JSON object found in response');
        }
        
        console.log('Final JSON to parse (length: ' + cleanJson.length + '):', cleanJson.substring(0, 500) + '...');
        
        const parsedStats = JSON.parse(cleanJson.trim());
        
        // Validate the battle-ready monster structure
        console.log('Parsed battle monster:', parsedStats);
        
        // Ensure required fields exist
        if (!parsedStats.name || !parsedStats.baseStats || !parsedStats.specialAbility || !parsedStats.movepool) {
          throw new Error('Generated monster is missing required battle system fields');
        }
        
        // Validate movepool has exactly 4 moves
        if (!Array.isArray(parsedStats.movepool) || parsedStats.movepool.length !== 4) {
          throw new Error('Monster movepool must contain exactly 4 moves');
        }
        
        // Ensure all moves have required effect arrays
        for (const move of parsedStats.movepool) {
          if (!move.effects || !Array.isArray(move.effects) || move.effects.length === 0) {
            throw new Error(`Move "${move.name}" is missing effects array`);
          }
        }
        
        // Use tier from the JSON response
        tier = parsedStats.tier || 'medium';
        
        // Clean asterisks from JSON-parsed name (qwen3 often adds them for markdown bold)
        if (parsedStats.name) {
          parsedStats.name = parsedStats.name.replace(/\*/g, '');
        }
        
        // Clean asterisks from special ability name
        if (parsedStats.specialAbility?.name) {
          parsedStats.specialAbility.name = parsedStats.specialAbility.name.replace(/\*/g, '');
        }
        
        // Clean asterisks from move names
        if (parsedStats.movepool) {
          for (const move of parsedStats.movepool) {
            if (move.name) {
              move.name = move.name.replace(/\*/g, '');
            }
          }
        }
        
        // Ensure the name from structured concept is used if available
        if (monsterName && monsterName !== 'Unknown Monster') {
          // Remove asterisk characters used for markdown bold formatting
          parsedStats.name = monsterName.replace(/\*/g, '');
        }
        
        // Ensure baseStats are numbers within reasonable ranges
        if (parsedStats.baseStats) {
          const statFields = ['hp', 'attack', 'defense', 'speed'];
          for (const field of statFields) {
            if (parsedStats.baseStats[field] !== undefined) {
              parsedStats.baseStats[field] = Math.max(10, Math.min(50, parseInt(parsedStats.baseStats[field])));
            }
          }
        }
        
        const stats: PicletStats = parsedStats;
        state.picletStats = stats;
        console.log('Monster stats generated:', stats);
        console.log('Monster stats JSON:', JSON.stringify(stats, null, 2));
      } catch (parseError) {
        console.error('Failed to parse JSON:', parseError, 'Raw output:', cleanJson);
        throw new Error('Failed to parse monster stats JSON');
      }
    } catch (error) {
      handleAPIError(error);
    }
  }
  
  async function autoSavePiclet() {
    if (!state.picletImage || !state.imageCaption || !state.picletConcept || !state.imagePrompt || !state.picletStats) {
      console.error('Cannot auto-save: missing required data');
      return;
    }
    
    try {
      // Create a clean copy of stats to ensure it's serializable
      const cleanStats = JSON.parse(JSON.stringify(state.picletStats));
      
      const picletData = {
        name: state.picletStats.name,
        imageUrl: state.picletImage.imageUrl,
        imageData: state.picletImage.imageData,
        imageCaption: state.imageCaption,
        concept: state.picletConcept,
        imagePrompt: state.imagePrompt,
        stats: cleanStats,
        createdAt: new Date()
      };
      
      // Check for any non-serializable data
      console.log('Checking piclet data for serializability:');
      console.log('- name type:', typeof picletData.name);
      console.log('- imageUrl type:', typeof picletData.imageUrl);
      console.log('- imageData type:', typeof picletData.imageData, picletData.imageData ? `length: ${picletData.imageData.length}` : 'null/undefined');
      console.log('- imageCaption type:', typeof picletData.imageCaption);
      console.log('- concept type:', typeof picletData.concept);
      console.log('- imagePrompt type:', typeof picletData.imagePrompt);
      console.log('- stats:', cleanStats);
      
      // Convert to PicletInstance format and save
      const picletInstance = await generatedDataToPicletInstance(picletData);
      const picletId = await savePicletInstance(picletInstance);
      console.log('Piclet auto-saved as uncaught with ID:', picletId);
    } catch (err) {
      console.error('Failed to auto-save piclet:', err);
      console.error('Piclet data that failed to save:', {
        name: state.picletStats?.name,
        hasImageUrl: !!state.picletImage?.imageUrl,
        hasImageData: !!state.picletImage?.imageData,
        hasStats: !!state.picletStats
      });
      // Don't throw - we don't want to interrupt the workflow
    }
  }
  
  function reset() {
    state = {
      currentStep: 'upload',
      userImage: null,
      imageCaption: null,
      picletConcept: null,
      picletStats: null,
      imagePrompt: null,
      picletImage: null,
      error: null,
      isProcessing: false
    };
  }
</script>

<div class="piclet-generator">
  
  {#if state.currentStep !== 'upload'}
    <WorkflowProgress currentStep={state.currentStep} error={state.error} />
  {/if}
  
  {#if state.currentStep === 'upload'}
    <UploadStep 
      onImageSelected={handleImageSelected}
      isProcessing={state.isProcessing}
    />
  {:else if state.currentStep === 'complete'}
    <PicletResult workflowState={state} onReset={reset} />
  {:else}
    <div class="processing-container">
      <div class="spinner"></div>
      <p class="processing-text">
        {#if state.currentStep === 'captioning'}
          Analyzing your image...
        {:else if state.currentStep === 'conceptualizing'}
          Creating Piclet concept...
        {:else if state.currentStep === 'statsGenerating'}
          Generating battle stats...
        {:else if state.currentStep === 'promptCrafting'}
          Creating image prompt...
        {:else if state.currentStep === 'generating'}
          Generating your Piclet...
        {/if}
      </p>
    </div>
  {/if}
</div>

<style>
  .piclet-generator {
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
  }
  
  
  .processing-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 3rem 1rem;
  }
  
  .spinner {
    width: 60px;
    height: 60px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 2rem;
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  
  .processing-text {
    font-size: 1.2rem;
    color: #333;
    margin-bottom: 2rem;
  }
  
</style>