File size: 27,772 Bytes
4bfb33c
 
 
 
1627169
4bfb33c
 
 
 
1627169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bfb33c
 
 
 
 
 
1627169
 
 
 
 
 
 
 
4bfb33c
1627169
 
 
4bfb33c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1627169
 
4bfb33c
1627169
 
 
4bfb33c
 
1627169
 
 
 
 
4bfb33c
 
 
 
 
1627169
4bfb33c
1627169
 
 
 
4bfb33c
 
1627169
4bfb33c
1627169
 
 
 
4bfb33c
 
1627169
4bfb33c
 
 
 
 
1627169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134f09f
 
1627169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134f09f
 
4bfb33c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1627169
 
 
 
 
4bfb33c
1627169
4bfb33c
1627169
 
 
 
4bfb33c
1627169
 
4bfb33c
1627169
 
4bfb33c
1627169
 
4bfb33c
1627169
 
4bfb33c
1627169
 
 
4bfb33c
1627169
 
4bfb33c
1627169
 
 
 
4bfb33c
1627169
 
4bfb33c
1627169
 
 
 
 
4bfb33c
1627169
 
 
 
 
 
 
4bfb33c
 
 
 
1627169
4bfb33c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1627169
 
4bfb33c
 
 
1627169
 
 
 
 
 
 
 
 
 
4bfb33c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134f09f
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RISU File Tools</title>
    <script src="./pako.min.js"></script>
    <script src="./msgpackr.min.js"></script>
    <style>
        /* 保持之前的所有样式不变 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.6;
            padding: 1rem;
            background-color: #f5f5f5;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 1rem;
        }

        .tool-section {
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 1.5rem;
            margin-bottom: 2rem;
        }

        h1 {
            font-size: 1.5rem;
            color: #333;
            margin-bottom: 1rem;
        }

        h2 {
            font-size: 1.2rem;
            color: #444;
            margin: 1rem 0;
        }

        .input-group {
            margin-bottom: 1rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            color: #555;
        }

        textarea {
            width: 100%;
            min-height: 150px;
            padding: 0.8rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            font-family: monospace;
            margin-bottom: 1rem;
        }

        .button-group {
            display: flex;
            gap: 0.5rem;
            flex-wrap: wrap;
            margin: 1rem 0;
        }

        button {
            padding: 0.6rem 1.2rem;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            transition: background-color 0.2s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .file-input-wrapper {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1rem;
            flex-wrap: wrap;
        }

        input[type="file"] {
            max-width: 100%;
        }

        .output {
            margin-top: 1.5rem;
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 4px;
        }

        pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            padding: 1rem;
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 0.9rem;
            overflow-x: auto;
        }

        @media screen and (max-width: 768px) {
            .container {
                padding: 0.5rem;
            }

            .tool-section {
                padding: 1rem;
            }

            h1 {
                font-size: 1.3rem;
            }

            h2 {
                font-size: 1.1rem;
            }

            button {
                width: 100%;
                margin-bottom: 0.5rem;
            }

            .file-input-wrapper {
                flex-direction: column;
                align-items: stretch;
            }

            input[type="file"] {
                width: 100%;
            }

            textarea {
                min-height: 120px;
            }
        }

        .loading {
            display: none;
            text-align: center;
            margin: 1rem 0;
        }

        .loading.active {
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="tool-section">
            <h1>ST to Risu Converter</h1>
            <div class="input-group">
                <label for="stInput">Paste ST JSON here:</label>
                <div class="file-input-wrapper">
                    <button id="stfileSelectButton">Choose File</button>
                    <input type="file" id="stfileInput" accept=".txt,.json" style="display:none;" />
                </div>
                <textarea id="stInput" placeholder="Paste ST JSON content..."></textarea>
            </div>

            <div class="input-group">
                <label for="risuDefault">Paste Risu Default JSON here:</label>
                <textarea id="risuDefault" placeholder="Paste Risu Default JSON content...">{
  "name": "Default",
  "apiType": "instructgpt35",
  "openAIKey": "",
  "mainPrompt": "",
  "jailbreak": "",
  "globalNote": "",
  "temperature": 100,
  "maxContext": 16000,
  "maxResponse": 1000,
  "frequencyPenalty": 0,
  "PresensePenalty": 0,
  "formatingOrder": [
    "main",
    "description",
    "personaPrompt",
    "chats",
    "lastChat",
    "jailbreak",
    "lorebook",
    "globalNote",
    "authorNote"
  ],
  "aiModel": "reverse_proxy",
  "subModel": "reverse_proxy",
  "currentPluginProvider": "",
  "textgenWebUIStreamURL": "",
  "textgenWebUIBlockingURL": "",
  "forceReplaceUrl": "",
  "forceReplaceUrl2": "",
  "promptPreprocess": false,
  "bias": [],
  "koboldURL": "http://localho.st:5001/api/v1",
  "proxyKey": "",
  "ooba": {
    "max_new_tokens": 180,
    "do_sample": true,
    "temperature": 0.7,
    "top_p": 0.9,
    "typical_p": 1,
    "repetition_penalty": 1.15,
    "encoder_repetition_penalty": 1,
    "top_k": 20,
    "min_length": 0,
    "no_repeat_ngram_size": 0,
    "num_beams": 1,
    "penalty_alpha": 0,
    "length_penalty": 1,
    "early_stopping": false,
    "seed": -1,
    "add_bos_token": true,
    "truncation_length": 4096,
    "ban_eos_token": false,
    "skip_special_tokens": true,
    "top_a": 0,
    "tfs": 1,
    "epsilon_cutoff": 0,
    "eta_cutoff": 0,
    "formating": {
      "header": "Below is an instruction that describes a task. Write a response that appropriately completes the request.",
      "systemPrefix": "### Instruction:",
      "userPrefix": "### Input:",
      "assistantPrefix": "### Response:",
      "seperator": "",
      "useName": false
    }
  },
  "ainconfig": {
    "top_p": 0.7,
    "rep_pen": 1.0625,
    "top_a": 0.08,
    "rep_pen_slope": 1.7,
    "rep_pen_range": 1024,
    "typical_p": 1,
    "badwords": "",
    "stoptokens": "",
    "top_k": 140
  },
  "proxyRequestModel": "claude-3-5-sonnet-20240620",
  "openrouterRequestModel": "anthropic/claude-2",
  "NAISettings": {
    "topK": 12,
    "topP": 0.85,
    "topA": 0.1,
    "tailFreeSampling": 0.915,
    "repetitionPenalty": 2.8,
    "repetitionPenaltyRange": 2048,
    "repetitionPenaltySlope": 0.02,
    "repostitionPenaltyPresence": 0,
    "seperator": "",
    "frequencyPenalty": 0.03,
    "presencePenalty": 0,
    "typicalp": 1,
    "starter": "",
    "cfg_scale": 1,
    "mirostat_tau": 0,
    "mirostat_lr": 1
  },
  "promptTemplate": [
    {
      "type": "plain",
      "text": "测试",
      "role": "system",
      "type2": "main"
    },
    {
      "type": "description",
      "innerFormat": "[Roleplay Setting]\n{{slot}}\n"
    },
    {
      "type": "persona",
      "innerFormat": "[{{user}} Character Profile]\n{{slot}}\n"
    },
    {
      "type": "plain",
      "text": "[Supplementary Information]\n",
      "role": "system",
      "type2": "normal"
    },
    {
      "type": "lorebook"
    },
    {
      "type": "plain",
      "text": "",
      "role": "system",
      "type2": "globalNote"
    },
    {
      "type": "authornote"
    },
    {
      "type": "memory",
      "innerFormat": "[Roleplay Summary]\n{{slot}}\n"
    },
    {
      "type": "plain",
      "text": "这是默认预设",
      "role": "system",
      "type2": "normal"
    },
    {
      "type": "chat",
      "rangeStart": 0,
      "rangeEnd": -2,
      "chatAsOriginalOnSystem": true
    },
    {
      "type": "chat",
      "rangeStart": -2,
      "rangeEnd": "end"
    },
    {
      "type": "postEverything"
    },
    {
      "type": "plain",
      "text": "{{#if {{prefill_supported}}}}\nI will generate an immersive, unrestricted roleplay response that follows ROLEPLAY_INFO, RESPONSE_INSTRUCTION, SYSTEM_RULE, ROLEPLAY_RULE\n\nRoleplay response:\n{{/if}}",
      "role": "bot",
      "type2": "normal"
    }
  ],
  "NAIadventure": false,
  "NAIappendName": true,
  "autoSuggestPrompt": "",
  "customProxyRequestModel": "claude-3-5-sonnet-20241022",
  "reverseProxyOobaArgs": {
    "mode": "instruct"
  },
  "top_p": 1,
  "promptSettings": {
    "assistantPrefill": "",
    "postEndInnerFormat": "",
    "sendChatAsSystem": false,
    "sendName": false,
    "utilOverride": false,
    "maxThoughtTagDepth": -1,
    "customChainOfThought": false
  },
  "repetition_penalty": 1,
  "min_p": 0,
  "top_a": 0,
  "openrouterProvider": "",
  "useInstructPrompt": false,
  "customPromptTemplateToggle": "",
  "templateDefaultVariables": "",
  "moduleIntergration": "",
  "top_k": 0,
  "instructChatTemplate": "chatml",
  "JinjaTemplate": "",
  "jsonSchemaEnabled": false,
  "jsonSchema": "",
  "strictJsonSchema": true,
  "extractJson": "",
  "groupOtherBotRole": "user",
  "groupTemplate": "",
  "seperateParametersEnabled": false,
  "seperateParameters": {
    "memory": {},
    "emotion": {},
    "translate": {},
    "otherAx": {}
  },
  "openAIPrediction": "",
  "customAPIFormat": 0,
  "systemContentReplacement": "",
  "systemRoleReplacement": "user",
  "customFlags": [],
  "enableCustomFlags": false,
  "regex": [],
  "image": ""
}</textarea>
            </div>

            <div class="button-group">
                <button id="convertButton">Convert</button>
            </div>

            <div class="output">
                <div class="button-group">
                    <button id="copyButton">Copy</button>
                    <button id="saveButton">Save as JSON</button>
                </div>
                <pre id="stOutput"></pre>
            </div>
        </div>

        <div class="tool-section">
            <h1>RISU Preset File Tools</h1>
            <div class="input-group">
                <h2>Decrypt RISU Preset</h2>
                <div class="file-input-wrapper">
                    <input type="file" id="fileInput" accept=".risupreset,.risup"/>
                    <button id="recoverButton">Recover Preset</button>
                </div>
            </div>

            <div class="input-group">
                <h2>Encrypt to RISU Preset</h2>
                <div class="file-input-wrapper">
                    <input type="file" id="jsonInput" accept=".json"/>
                    <button id="encryptButton">Create .risup</button>
                </div>
            </div>

            <div class="loading" id="loadingIndicator">Processing...</div>
            <pre id="output"></pre>
        </div>
    </div>

    <script>
        //st文件选择
        document.getElementById("stfileSelectButton").addEventListener("click", function() {
            // 触发文件选择框
            document.getElementById("stfileInput").click();
        });

        // 处理文件选择和读取内容
        document.getElementById("stfileInput").addEventListener("change", function(event) {
            const file = event.target.files[0]; // 获取选择的第一个文件

            if (file) {
                const reader = new FileReader();

                // 文件读取成功后的回调
                reader.onload = function(e) {
                    const fileContent = e.target.result;
                    document.getElementById("stInput").value = fileContent; // 填充到textarea中
                };

                // 读取文件内容
                reader.readAsText(file); // 读取文件为文本
            }
        });
    
        // Copy button functionality
        document.getElementById("copyButton").addEventListener("click", function() {
            const outputText = document.getElementById("stOutput").textContent;
            navigator.clipboard.writeText(outputText).then(function() {
                alert("Copied to clipboard!");
            }).catch(function(err) {
                alert("Failed to copy text: " + err);
            });
        });
// 接着前面的script标签内容继续添加代码:

        // Save as JSON button functionality
        document.getElementById("saveButton").addEventListener("click", function() {
            const outputText = document.getElementById("stOutput").textContent;

            // Ensure the output is valid JSON
            try {
                const jsonObject = JSON.parse(outputText); // Validate JSON format
                const blob = new Blob([JSON.stringify(jsonObject, null, 2)], { type: 'application/json' });
                const link = document.createElement('a');
                link.href = URL.createObjectURL(blob);
                link.download = 'converted_risu.json'; // File name
                link.click();
            } catch (error) {
                alert("The output is not valid JSON.");
            }
        });
    
        // ST to Risu Converter Logic
        document.getElementById('convertButton').addEventListener('click', function convert() {
            try {
                const stInput = JSON.parse(document.getElementById('stInput').value);
                const risuDefault = JSON.parse(document.getElementById('risuDefault').value);

                if (!stInput || !risuDefault) {
                    throw new Error('Invalid JSON input.');
                }

                const promptOrder = stInput.prompt_order.find(order => order.character_id === 100001);
                if (!promptOrder) {
                    throw new Error('Character ID 100001 not found in ST prompt_order.');
                }

                const stPrompts = stInput.prompts;
                const risuPrompts = risuDefault.promptTemplate;

                const specialTypeOrder = ["description", "persona", "lorebook", "globalNote", "authornote", "memory", "chat"];
                const specialTemplates = [];

                specialTypeOrder.forEach(type => {
                    const templates = risuPrompts.filter(p => p.type === type || p.type2 === type);
                    if (templates.length > 0) {
                        specialTemplates.push(...templates);
                    } else {
                        if (type === "chat") {
                            const chatTemplates = risuPrompts.filter(p => p.type === "chat");
                            if (chatTemplates.length < 2) {
                                specialTemplates.push(...chatTemplates);
                            }
                        } else {
                            throw new Error(`Missing required special type: ${type}`);
                        }
                    }
                });

                const convertedPrompts = [];
                const stPromptMap = new Map(stPrompts.map(p => [p.identifier, p]));

                let mainPrompt;

                promptOrder.order.forEach(({ identifier, enabled }) => {
                    if (!enabled) return;

                    if (identifier === "main") {
                        const mainPromptData = stPromptMap.get(identifier);
                        if (mainPromptData) {
                            const template = risuPrompts.find(p => p.type2 === "main");
                            if (template) {
                                mainPrompt = {
                                    ...template,
                                    text: mainPromptData.content || "未命名"
                                };
                            }
                        }
                        return;
                    }

                    if (["charDescription", "dialogueExamples", "charPersonality", "scenario", "worldInfoBefore", "worldInfoAfter"].includes(identifier)) {
                        return;
                    }

                    const stPrompt = stPromptMap.get(identifier);
                    if (!stPrompt) {
                        console.warn(`Prompt with identifier ${identifier} not found, skipping.`);
                        return;
                    }

                    const name = stPrompt.name || "未命名";
                    convertedPrompts.push({
                        type: "plain",
                        text: stPrompt.content || "\n",
                        role: stPrompt.role,
                        type2: "normal",
                        name
                    });
                });

                const charDescriptionIndex = promptOrder.order.findIndex(o => o.identifier === "charDescription");
                if (charDescriptionIndex === -1) {
                    throw new Error('charDescription not found in prompt_order.');
                }

                const filteredSpecialTemplates = [
                    risuPrompts.find(p => p.type === "description"),
                    mainPrompt,
                    ...specialTemplates.filter(p => p.type !== "description" && p.type2 !== "main")
                ];

                const result = [
                    ...convertedPrompts.slice(0, charDescriptionIndex),
                    ...filteredSpecialTemplates,
                    ...convertedPrompts.slice(charDescriptionIndex)
                ].filter(Boolean);

                risuDefault.promptTemplate = result;

                document.getElementById('stOutput').textContent = JSON.stringify(risuDefault, null, 2);
            } catch (error) {
                document.getElementById('stOutput').textContent = `Error: ${error.message}`;
            }
        });

        // RISU Preset File Tools Logic
        async function initWasm() {
            try {
                const response = await fetch('./rpack_bg.wasm');
                if (!response.ok) throw new Error('Failed to fetch Wasm module');
                const wasmModule = await response.arrayBuffer();
                if (!WebAssembly.validate(wasmModule)) throw new Error('Invalid WebAssembly module');
                const { instance } = await WebAssembly.instantiate(wasmModule);
                return instance.exports;
            } catch (error) {
                console.error('Error initializing Wasm module:', error);
                throw error;
            }
        }

        function getUint8ArrayMemory0(wasmInstance) {
            return new Uint8Array(wasmInstance.memory.buffer);
        }

        function passArray8ToWasm0(arg, malloc, wasmInstance) {
            const ptr = malloc(arg.length * 1, 1) >>> 0;
            getUint8ArrayMemory0(wasmInstance).set(arg, ptr / 1);
            WASM_VECTOR_LEN = arg.length;
            return ptr;
        }

        let WASM_VECTOR_LEN = 0;

        function getDataViewMemory0(wasmInstance) {
            return new DataView(wasmInstance.memory.buffer);
        }

        function getArrayU8FromWasm0(ptr, len, wasmInstance) {
            return getUint8ArrayMemory0(wasmInstance).subarray(ptr / 1, ptr / 1 + len);
        }

        async function decodeRPack(datas, wasmInstance) {
            try {
                const retptr = wasmInstance.__wbindgen_add_to_stack_pointer(-16);
                const ptr0 = passArray8ToWasm0(datas, wasmInstance.__wbindgen_malloc, wasmInstance);
                const len0 = WASM_VECTOR_LEN;
                wasmInstance.decode(retptr, ptr0, len0);
                var r0 = getDataViewMemory0(wasmInstance).getInt32(retptr + 4 * 0, true);
                var r1 = getDataViewMemory0(wasmInstance).getInt32(retptr + 4 * 1, true);
                var v2 = getArrayU8FromWasm0(r0, r1, wasmInstance).slice();
                wasmInstance.__wbindgen_free(r0, r1 * 1, 1);
                return v2;
            } finally {
                wasmInstance.__wbindgen_add_to_stack_pointer(16);
            }
        }

        async function encodeRPack(datas, wasmInstance) {
            try {
                const retptr = wasmInstance.__wbindgen_add_to_stack_pointer(-16);
                const ptr0 = passArray8ToWasm0(datas, wasmInstance.__wbindgen_malloc, wasmInstance);
                const len0 = WASM_VECTOR_LEN;
                wasmInstance.encode(retptr, ptr0, len0);
                var r0 = getDataViewMemory0(wasmInstance).getInt32(retptr + 4 * 0, true);
                var r1 = getDataViewMemory0(wasmInstance).getInt32(retptr + 4 * 1, true);
                var v2 = getArrayU8FromWasm0(r0, r1, wasmInstance).slice();
                wasmInstance.__wbindgen_free(r0, r1 * 1, 1);
                return v2;
            } finally {
                wasmInstance.__wbindgen_add_to_stack_pointer(16);
            }
        }

        async function decryptBuffer(data, key) {
            const encoder = new TextEncoder();
            const keyData = encoder.encode(key);
            const hash = await window.crypto.subtle.digest("SHA-256", keyData);
            const cryptoKey = await window.crypto.subtle.importKey(
                "raw",
                hash,
                "AES-GCM",
                false,
                ["decrypt"]
            );
            return await window.crypto.subtle.decrypt(
                {
                    name: "AES-GCM",
                    iv: new Uint8Array(12)
                },
                cryptoKey,
                data
            );
        }

        async function encryptBuffer(data, key) {
            const encoder = new TextEncoder();
            const keyData = encoder.encode(key);
            const hash = await window.crypto.subtle.digest("SHA-256", keyData);
            const cryptoKey = await window.crypto.subtle.importKey(
                "raw",
                hash,
                "AES-GCM",
                false,
                ["encrypt"]
            );
            return await window.crypto.subtle.encrypt(
                {
                    name: "AES-GCM",
                    iv: new Uint8Array(12)
                },
                cryptoKey,
                data
            );
        }

        async function recoverPresetFromFile(file) {
            try {
                showLoading();
                const arrayBuffer = await file.arrayBuffer();
                const uint8Array = new Uint8Array(arrayBuffer);

                let finalData;

                if (file.name.endsWith('.risup')) {
                    const wasmInstance = await initWasm();
                    const decodedData = await decodeRPack(uint8Array, wasmInstance);
                    console.log("1. RPack decoded:", decodedData);

                    const decompressedData = pako.inflate(decodedData);
                    console.log("2. Decompressed:", decompressedData);

                    const firstDecode = msgpackr.decode(decompressedData);
                    console.log("3. First msgpack decode:", firstDecode);

                    const decryptedData = await decryptBuffer(firstDecode.preset, 'risupreset');
                    console.log("4. Decrypted data:", new Uint8Array(decryptedData));

                    finalData = msgpackr.decode(new Uint8Array(decryptedData));
                    console.log("5. Final decoded data:", finalData);

                } else if (file.name.endsWith('.risupreset')) {
                    const decompressedData = pako.inflate(uint8Array);
                    console.log("1. Decompressed:", decompressedData);

                    const firstDecode = msgpackr.decode(decompressedData);
                    console.log("2. First msgpack decode:", firstDecode);

                    const encryptedPreset = firstDecode.preset ?? firstDecode.pres;
                    if (!encryptedPreset) {
                        throw new Error("Missing `preset` or `pres` field in .risupreset file.");
                    }

                    const decryptedData = await decryptBuffer(encryptedPreset, 'risupreset');
                    console.log("3. Decrypted data:", new Uint8Array(decryptedData));

                    finalData = msgpackr.decode(new Uint8Array(decryptedData));
                    console.log("4. Final decoded data:", finalData);
                } else {
                    throw new Error('Unsupported file format');
                }

                document.getElementById('output').textContent = JSON.stringify(finalData, null, 2);
            } catch (error) {
                console.error('Error recovering preset:', error);
                document.getElementById('output').textContent = `Error: ${error.message}\n\nStack: ${error.stack}`;
            } finally {
                hideLoading();
            }
        }

        async function createPresetFile(file) {
            try {
                showLoading();
                const text = await file.text();
                const jsonData = JSON.parse(text);

                const firstEncode = msgpackr.encode(jsonData);
                console.log("1. First msgpack encode:", firstEncode);

                const encryptedData = await encryptBuffer(firstEncode, 'risupreset');
                console.log("2. Encrypted data:", new Uint8Array(encryptedData));

                const wrapper = {
                    presetVersion: 2,
                    type: "preset",
                    preset: new Uint8Array(encryptedData)
                };
                const secondEncode = msgpackr.encode(wrapper);
                console.log("3. Second msgpack encode:", secondEncode);

                const compressedData = pako.deflate(secondEncode);
                console.log("4. Compressed:", compressedData);

                const wasmInstance = await initWasm();
                const rpackData = await encodeRPack(compressedData, wasmInstance);
                console.log("5. RPack encoded:", rpackData);

                const blob = new Blob([rpackData], { type: 'application/octet-stream' });
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = file.name.replace('.json', '.risup');
                a.click();
                URL.revokeObjectURL(url);

                document.getElementById('output').textContent = "Preset file created successfully!";
            } catch (error) {
                console.error('Error creating preset:', error);
                document.getElementById('output').textContent = `Error: ${error.message}\n\nStack: ${error.stack}`;
            } finally {
                hideLoading();
            }
        }

        // 添加loading状态控制
        function showLoading() {
            document.getElementById('loadingIndicator').classList.add('active');
        }

        function hideLoading() {
            document.getElementById('loadingIndicator').classList.remove('active');
        }

        // Attach event listeners for the file buttons
        document.getElementById('recoverButton').addEventListener('click', () => {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            if (file) {
                recoverPresetFromFile(file);
            } else {
                document.getElementById('output').textContent = 'Please select a file first.';
            }
        });

        document.getElementById('encryptButton').addEventListener('click', () => {
            const fileInput = document.getElementById('jsonInput');
            const file = fileInput.files[0];
            if (file) {
                createPresetFile(file);
            } else {
                document.getElementById('output').textContent = 'Please select a JSON file first.';
            }
        });
    </script>
</body>
</html>