-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtool.py
More file actions
2850 lines (2653 loc) · 118 KB
/
tool.py
File metadata and controls
2850 lines (2653 loc) · 118 KB
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
996
997
998
999
1000
"""
title: Inline Visualizer v2
author: Classic298
version: 2.1.1
required_open_webui_version: 0.9.2
description: Renders interactive HTML/SVG visualizations inline in chat. Requires "iframe Sandbox Allow Same Origin" to be enabled in Open WebUI Settings -> Interface. For design instructions, the model should call view_skill("visualize").
"""
import re
from typing import Literal
# Build marker embedded into the rendered iframe so the running
# version can be verified at runtime (search DevTools for
# `data-iv-build` on <html>). Bump on every protocol-level change
# so stale cached iframes can be spotted immediately.
_IV_BUILD = "2.1.0"
from fastapi.responses import HTMLResponse
from pydantic import BaseModel, Field
# ---------------------------------------------------------------------------
# Injected CSS — Theme variables (light default, dark via data-theme)
# ---------------------------------------------------------------------------
THEME_CSS = """
:root {
--color-text-primary: #1F2937;
--color-text-secondary: #6B7280;
--color-text-tertiary: #9CA3AF;
--color-text-info: #2563EB;
--color-text-success: #059669;
--color-text-warning: #D97706;
--color-text-danger: #DC2626;
--color-bg-primary: #FFFFFF;
--color-bg-secondary: #F9FAFB;
--color-bg-tertiary: #F3F4F6;
--color-border-tertiary: rgba(0,0,0,0.15);
--color-border-secondary: rgba(0,0,0,0.3);
--color-border-primary: rgba(0,0,0,0.4);
--font-sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
--font-mono: 'SF Mono', Menlo, Consolas, monospace;
--radius-md: 8px;
--radius-lg: 12px;
--radius-xl: 16px;
/* --- Color ramp variables (light) --- */
--ramp-purple-fill:#EEEDFE; --ramp-purple-stroke:#534AB7; --ramp-purple-th:#3C3489; --ramp-purple-ts:#534AB7;
--ramp-teal-fill:#E1F5EE; --ramp-teal-stroke:#0F6E56; --ramp-teal-th:#085041; --ramp-teal-ts:#0F6E56;
--ramp-coral-fill:#FAECE7; --ramp-coral-stroke:#993C1D; --ramp-coral-th:#712B13; --ramp-coral-ts:#993C1D;
--ramp-pink-fill:#FBEAF0; --ramp-pink-stroke:#993556; --ramp-pink-th:#72243E; --ramp-pink-ts:#993556;
--ramp-gray-fill:#F1EFE8; --ramp-gray-stroke:#5F5E5A; --ramp-gray-th:#444441; --ramp-gray-ts:#5F5E5A;
--ramp-blue-fill:#E6F1FB; --ramp-blue-stroke:#185FA5; --ramp-blue-th:#0C447C; --ramp-blue-ts:#185FA5;
--ramp-green-fill:#EAF3DE; --ramp-green-stroke:#3B6D11; --ramp-green-th:#27500A; --ramp-green-ts:#3B6D11;
--ramp-amber-fill:#FAEEDA; --ramp-amber-stroke:#854F0B; --ramp-amber-th:#633806; --ramp-amber-ts:#854F0B;
--ramp-red-fill:#FCEBEB; --ramp-red-stroke:#A32D2D; --ramp-red-th:#791F1F; --ramp-red-ts:#A32D2D;
/* --- Common aliases (catch hallucinated variable names) --- */
/* Text */
--fg: var(--color-text-primary);
--text: var(--color-text-primary);
--foreground: var(--color-text-primary);
--text-primary: var(--color-text-primary);
--text-color: var(--color-text-primary);
--color-text: var(--color-text-primary);
--color-foreground: var(--color-text-primary);
--body-color: var(--color-text-primary);
--muted: var(--color-text-secondary);
--muted-foreground: var(--color-text-secondary);
--text-muted: var(--color-text-secondary);
--text-secondary: var(--color-text-secondary);
--secondary: var(--color-text-secondary);
--subtle: var(--color-text-tertiary);
--text-tertiary: var(--color-text-tertiary);
/* Backgrounds */
--bg: var(--color-bg-primary);
--background: var(--color-bg-primary);
--bg-primary: var(--color-bg-primary);
--body-bg: var(--color-bg-primary);
--color-bg: var(--color-bg-primary);
--surface: var(--color-bg-secondary);
--surface-1: var(--color-bg-secondary);
--surface-2: var(--color-bg-tertiary);
--card: var(--color-bg-secondary);
--card-bg: var(--color-bg-secondary);
--card-foreground: var(--color-text-primary);
--card-background: var(--color-bg-secondary);
--popover: var(--color-bg-secondary);
--popover-foreground: var(--color-text-primary);
--hover: rgba(0,0,0,0.04);
/* Borders */
--border: var(--color-border-tertiary);
--border-color: var(--color-border-tertiary);
--divider: var(--color-border-tertiary);
--separator: var(--color-border-tertiary);
--input: var(--color-border-tertiary);
--ring: var(--color-border-secondary);
/* Accent / Primary (AI uses --accent as brand color, not surface) */
--primary: #6c2eb9;
--primary-foreground: #ffffff;
--accent: #6c2eb9;
--accent-foreground: #ffffff;
/* Themed select chevron (light) — used by the pre-styled <select> */
--select-arrow: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M3 4.5l3 3 3-3' fill='none' stroke='%236B7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
}
:root[data-theme="dark"] {
--color-text-primary: #E5E7EB;
--color-text-secondary: #9CA3AF;
--color-text-tertiary: #6B7280;
--color-text-info: #60A5FA;
--color-text-success: #34D399;
--color-text-warning: #FBBF24;
--color-text-danger: #F87171;
--color-bg-primary: #1A1A1A;
--color-bg-secondary: #262626;
--color-bg-tertiary: #111111;
--color-border-tertiary: rgba(255,255,255,0.15);
--color-border-secondary: rgba(255,255,255,0.3);
--color-border-primary: rgba(255,255,255,0.4);
--ramp-purple-fill:#3C3489; --ramp-purple-stroke:#AFA9EC; --ramp-purple-th:#CECBF6; --ramp-purple-ts:#AFA9EC;
--ramp-teal-fill:#085041; --ramp-teal-stroke:#5DCAA5; --ramp-teal-th:#9FE1CB; --ramp-teal-ts:#5DCAA5;
--ramp-coral-fill:#712B13; --ramp-coral-stroke:#F0997B; --ramp-coral-th:#F5C4B3; --ramp-coral-ts:#F0997B;
--ramp-pink-fill:#72243E; --ramp-pink-stroke:#ED93B1; --ramp-pink-th:#F4C0D1; --ramp-pink-ts:#ED93B1;
--ramp-gray-fill:#444441; --ramp-gray-stroke:#B4B2A9; --ramp-gray-th:#D3D1C7; --ramp-gray-ts:#B4B2A9;
--ramp-blue-fill:#0C447C; --ramp-blue-stroke:#85B7EB; --ramp-blue-th:#B5D4F4; --ramp-blue-ts:#85B7EB;
--ramp-green-fill:#27500A; --ramp-green-stroke:#97C459; --ramp-green-th:#C0DD97; --ramp-green-ts:#97C459;
--ramp-amber-fill:#633806; --ramp-amber-stroke:#EF9F27; --ramp-amber-th:#FAC775; --ramp-amber-ts:#EF9F27;
--ramp-red-fill:#791F1F; --ramp-red-stroke:#F09595; --ramp-red-th:#F7C1C1; --ramp-red-ts:#F09595;
/* --- Common aliases (dark overrides) --- */
--text: var(--color-text-primary);
--foreground: var(--color-text-primary);
--text-primary: var(--color-text-primary);
--text-color: var(--color-text-primary);
--color-text: var(--color-text-primary);
--body-color: var(--color-text-primary);
--muted: var(--color-text-secondary);
--muted-foreground: var(--color-text-secondary);
--text-muted: var(--color-text-secondary);
--text-secondary: var(--color-text-secondary);
--secondary: var(--color-text-secondary);
--subtle: var(--color-text-tertiary);
--text-tertiary: var(--color-text-tertiary);
--bg: var(--color-bg-primary);
--background: var(--color-bg-primary);
--bg-primary: var(--color-bg-primary);
--body-bg: var(--color-bg-primary);
--color-bg: var(--color-bg-primary);
--surface: var(--color-bg-secondary);
--surface-1: var(--color-bg-secondary);
--surface-2: var(--color-bg-tertiary);
--card: var(--color-bg-secondary);
--card-bg: var(--color-bg-secondary);
--card-foreground: var(--color-text-primary);
--card-background: var(--color-bg-secondary);
--popover: var(--color-bg-secondary);
--popover-foreground: var(--color-text-primary);
--hover: rgba(255,255,255,0.06);
--border: var(--color-border-tertiary);
--border-color: var(--color-border-tertiary);
--divider: var(--color-border-tertiary);
--separator: var(--color-border-tertiary);
--input: var(--color-border-tertiary);
--ring: var(--color-border-secondary);
--primary: #a78bfa;
--primary-foreground: #1A1A1A;
--accent: #a78bfa;
--accent-foreground: #ffffff;
--select-arrow: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path d='M3 4.5l3 3 3-3' fill='none' stroke='%239CA3AF' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
}
/* --- Named accent palette ---
* Apply data-accent="<name>" on <html> for global, on any element
* for local override. The variants reuse the existing color-ramp
* stroke colors so charts and forms share visual vocabulary
* (teal here = teal in a chart). Each variant works in both
* light and dark themes — --accent picks up the ramp's per-theme
* stroke automatically; --accent-foreground flips dark in dark
* mode so text stays legible on pastel accents.
*/
[data-accent="purple"] { --accent: var(--ramp-purple-stroke); --accent-foreground: #ffffff; }
[data-accent="teal"] { --accent: var(--ramp-teal-stroke); --accent-foreground: #ffffff; }
[data-accent="coral"] { --accent: var(--ramp-coral-stroke); --accent-foreground: #ffffff; }
[data-accent="pink"] { --accent: var(--ramp-pink-stroke); --accent-foreground: #ffffff; }
[data-accent="gray"] { --accent: var(--ramp-gray-stroke); --accent-foreground: #ffffff; }
[data-accent="blue"] { --accent: var(--ramp-blue-stroke); --accent-foreground: #ffffff; }
[data-accent="green"] { --accent: var(--ramp-green-stroke); --accent-foreground: #ffffff; }
[data-accent="amber"] { --accent: var(--ramp-amber-stroke); --accent-foreground: #ffffff; }
[data-accent="red"] { --accent: var(--ramp-red-stroke); --accent-foreground: #ffffff; }
[data-theme="dark"] [data-accent],
[data-theme="dark"][data-accent] {
--accent-foreground: #1A1A1A;
}
"""
# ---------------------------------------------------------------------------
# Injected CSS — SVG utility classes + color ramp selectors
# ---------------------------------------------------------------------------
SVG_CLASSES = """
/* --- Text --- */
.t { font: 400 14px/1.4 var(--font-sans); fill: var(--color-text-primary); }
.ts { font: 400 12px/1.4 var(--font-sans); fill: var(--color-text-secondary); }
.th { font: 500 14px/1.4 var(--font-sans); fill: var(--color-text-primary); }
/* --- Shapes --- */
.box { fill: var(--color-bg-secondary); stroke: var(--color-border-tertiary); stroke-width: 0.5; }
.node { cursor: pointer; }
.node:hover { opacity: 0.85; }
.arr { stroke: var(--color-border-secondary); stroke-width: 1.5; fill: none; }
.leader { stroke: var(--color-text-tertiary); stroke-width: 0.5; stroke-dasharray: 3 2; fill: none; }
/* --- Color ramp selectors (fill/stroke adapt via CSS vars) --- */
.c-purple>rect,.c-purple>circle,.c-purple>ellipse{fill:var(--ramp-purple-fill);stroke:var(--ramp-purple-stroke);stroke-width:.5}
.c-purple>.th{fill:var(--ramp-purple-th)!important} .c-purple>.ts{fill:var(--ramp-purple-ts)!important}
.c-teal>rect,.c-teal>circle,.c-teal>ellipse{fill:var(--ramp-teal-fill);stroke:var(--ramp-teal-stroke);stroke-width:.5}
.c-teal>.th{fill:var(--ramp-teal-th)!important} .c-teal>.ts{fill:var(--ramp-teal-ts)!important}
.c-coral>rect,.c-coral>circle,.c-coral>ellipse{fill:var(--ramp-coral-fill);stroke:var(--ramp-coral-stroke);stroke-width:.5}
.c-coral>.th{fill:var(--ramp-coral-th)!important} .c-coral>.ts{fill:var(--ramp-coral-ts)!important}
.c-pink>rect,.c-pink>circle,.c-pink>ellipse{fill:var(--ramp-pink-fill);stroke:var(--ramp-pink-stroke);stroke-width:.5}
.c-pink>.th{fill:var(--ramp-pink-th)!important} .c-pink>.ts{fill:var(--ramp-pink-ts)!important}
.c-gray>rect,.c-gray>circle,.c-gray>ellipse{fill:var(--ramp-gray-fill);stroke:var(--ramp-gray-stroke);stroke-width:.5}
.c-gray>.th{fill:var(--ramp-gray-th)!important} .c-gray>.ts{fill:var(--ramp-gray-ts)!important}
.c-blue>rect,.c-blue>circle,.c-blue>ellipse{fill:var(--ramp-blue-fill);stroke:var(--ramp-blue-stroke);stroke-width:.5}
.c-blue>.th{fill:var(--ramp-blue-th)!important} .c-blue>.ts{fill:var(--ramp-blue-ts)!important}
.c-green>rect,.c-green>circle,.c-green>ellipse{fill:var(--ramp-green-fill);stroke:var(--ramp-green-stroke);stroke-width:.5}
.c-green>.th{fill:var(--ramp-green-th)!important} .c-green>.ts{fill:var(--ramp-green-ts)!important}
.c-amber>rect,.c-amber>circle,.c-amber>ellipse{fill:var(--ramp-amber-fill);stroke:var(--ramp-amber-stroke);stroke-width:.5}
.c-amber>.th{fill:var(--ramp-amber-th)!important} .c-amber>.ts{fill:var(--ramp-amber-ts)!important}
.c-red>rect,.c-red>circle,.c-red>ellipse{fill:var(--ramp-red-fill);stroke:var(--ramp-red-stroke);stroke-width:.5}
.c-red>.th{fill:var(--ramp-red-th)!important} .c-red>.ts{fill:var(--ramp-red-ts)!important}
"""
# ---------------------------------------------------------------------------
# Injected CSS — Base resets & interactive element styles
# ---------------------------------------------------------------------------
BASE_STYLES = """
* { box-sizing: border-box; margin: 0; font-family: var(--font-sans); }
html, body { overflow: hidden; }
body { background: transparent; color: var(--color-text-primary); line-height: 1.5; padding: 8px; }
svg { overflow: visible; }
svg text { fill: var(--color-text-primary); }
h1 { font-size: 22px; font-weight: 500; color: var(--color-text-primary); margin-bottom: 12px; }
h2 { font-size: 18px; font-weight: 500; color: var(--color-text-primary); margin-bottom: 8px; }
h3 { font-size: 16px; font-weight: 500; color: var(--color-text-primary); margin-bottom: 6px; }
p { font-size: 14px; color: var(--color-text-secondary); margin-bottom: 8px; }
/* --- Pre-styled form elements ---
* Each rule is gated with :not([class]):not([style]) so the model
* opts in by emitting bare HTML. Adding either attribute is treated
* as opting out — the default suppresses and the model styles from
* scratch. Keeps token cost low for vanilla cases without locking
* the design space.
*/
button:not([class]):not([style]) {
background: transparent; border: 0.5px solid var(--color-border-secondary);
border-radius: var(--radius-md); padding: 6px 14px; font-size: 13px;
color: var(--color-text-primary); cursor: pointer; font-family: var(--font-sans);
}
button:not([class]):not([style]):hover { background: var(--color-bg-secondary); }
input[type="text"]:not([class]):not([style]),
input[type="number"]:not([class]):not([style]),
input[type="email"]:not([class]):not([style]),
input[type="search"]:not([class]):not([style]),
input[type="password"]:not([class]):not([style]),
input[type="tel"]:not([class]):not([style]),
input[type="url"]:not([class]):not([style]),
input[type="date"]:not([class]):not([style]),
input[type="time"]:not([class]):not([style]),
input[type="datetime-local"]:not([class]):not([style]) {
background: var(--color-bg-primary);
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md); padding: 6px 10px; font-size: 13px;
color: var(--color-text-primary); font-family: var(--font-sans);
outline: none; transition: border-color 0.15s ease;
}
input[type="text"]:not([class]):not([style]):focus,
input[type="number"]:not([class]):not([style]):focus,
input[type="email"]:not([class]):not([style]):focus,
input[type="search"]:not([class]):not([style]):focus,
input[type="password"]:not([class]):not([style]):focus,
input[type="tel"]:not([class]):not([style]):focus,
input[type="url"]:not([class]):not([style]):focus,
input[type="date"]:not([class]):not([style]):focus,
input[type="time"]:not([class]):not([style]):focus,
input[type="datetime-local"]:not([class]):not([style]):focus {
border-color: var(--color-border-primary);
}
/* Drop the type=number spinner — clashes with the field's borders. */
input[type="number"]:not([class]):not([style]) {
-moz-appearance: textfield; appearance: textfield;
}
input[type="number"]:not([class]):not([style])::-webkit-outer-spin-button,
input[type="number"]:not([class]):not([style])::-webkit-inner-spin-button {
-webkit-appearance: none; margin: 0;
}
textarea:not([class]) {
background: var(--color-bg-primary);
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md); padding: 8px 10px; font-size: 13px;
color: var(--color-text-primary); font-family: var(--font-sans);
outline: none; resize: vertical; min-height: 60px;
transition: border-color 0.15s ease;
}
textarea:not([class]):focus { border-color: var(--color-border-primary); }
/* accent-color always applies, regardless of class/style — it's a
* tint property the model is highly unlikely to set themselves, and
* letting it ride keeps palette switches consistent even when the
* model adds inline width/max-width styling to the slider. */
input[type="range"], input[type="checkbox"], input[type="radio"] {
accent-color: var(--accent);
}
input[type="range"]:not([class]):not([style]) { width: 100%; }
input[type="checkbox"]:not([class]):not([style]),
input[type="radio"]:not([class]):not([style]) {
/* accent-color comes from the always-on rule above. */
cursor: pointer;
}
select:not([class]):not([style]) {
appearance: none; -webkit-appearance: none; -moz-appearance: none;
background-color: var(--color-bg-secondary);
background-image: var(--select-arrow);
background-repeat: no-repeat;
background-position: right 10px center;
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md); padding: 6px 28px 6px 10px;
font-size: 13px; color: var(--color-text-primary); font-family: var(--font-sans);
outline: none; cursor: pointer;
}
select:not([class]):not([style]):focus { border-color: var(--color-border-primary); }
label:not([class]):not([style]) {
font-size: 13px; color: var(--color-text-primary); cursor: pointer;
}
fieldset:not([class]):not([style]) {
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md); padding: 12px;
}
legend:not([class]):not([style]) {
font-size: 12px; color: var(--color-text-secondary); padding: 0 6px;
}
/* Validation error border — standard a11y attribute, no class needed. */
input[aria-invalid="true"]:not([class]):not([style]),
textarea[aria-invalid="true"]:not([class]),
select[aria-invalid="true"]:not([class]):not([style]) {
border-color: var(--color-text-danger);
}
/* Keyboard-only focus rings (accent outline). Mouse focus stays subtle. */
button:not([class]):not([style]):focus-visible,
input:not([class]):not([style]):focus-visible,
textarea:not([class]):focus-visible,
select:not([class]):not([style]):focus-visible {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
code {
font-family: var(--font-mono); font-size: 13px; background: var(--color-bg-tertiary);
padding: 2px 6px; border-radius: 4px;
}
/* <kbd> — keyboard-key pill (cmd/ctrl/k style). */
kbd:not([class]):not([style]) {
font-family: var(--font-mono); font-size: 12px;
background: var(--color-bg-secondary);
border: 0.5px solid var(--color-border-tertiary);
border-radius: 4px; padding: 1px 6px;
color: var(--color-text-primary);
}
/* <hr> — flat divider matching the rest of the borders. */
hr:not([class]):not([style]) {
border: none;
border-top: 0.5px solid var(--color-border-tertiary);
margin: 1.5rem 0;
}
/* <details> / <summary> — themed disclosure with a bigger chevron.
* Container is an invisible rounded "wrapper" — the visible card-
* shape is the summary header itself. This way if a model adds its
* own summary background/border, the result is still single-card,
* not nested. Chevron is sized to be clearly visible. */
details:not([class]):not([style]) {
margin: 12px 0;
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md);
overflow: hidden;
}
details:not([class]):not([style]) > summary {
cursor: pointer; list-style: none; user-select: none;
font-weight: 500; color: var(--color-text-primary);
background: var(--color-bg-secondary);
padding: 10px 14px 10px 34px;
position: relative;
transition: background-color 0.15s ease;
}
details:not([class]):not([style]) > summary:hover {
background: var(--color-bg-tertiary);
}
details:not([class]):not([style]) > summary::-webkit-details-marker { display: none; }
details:not([class]):not([style]) > summary::marker { content: ''; }
details:not([class]):not([style]) > summary::before {
content: '\\25B8'; /* ▸ */
position: absolute; left: 12px; top: 50%;
transform: translateY(-50%);
transition: transform 0.15s ease;
color: var(--color-text-secondary);
font-size: 18px;
line-height: 1;
}
details[open]:not([class]):not([style]) > summary::before {
transform: translateY(-50%) rotate(90deg);
}
details[open]:not([class]):not([style]) > summary {
border-bottom: 0.5px solid var(--color-border-tertiary);
}
/* Margin (not padding) so children with their own bg inset properly. */
details:not([class]):not([style]) > *:not(summary) {
margin: 12px 14px;
}
blockquote:not([class]):not([style]) {
border-left: 4px solid var(--accent);
background: var(--color-bg-secondary);
padding: 12px 18px;
margin: 16px 0;
color: var(--color-text-secondary);
border-radius: var(--radius-md);
}
blockquote:not([class]):not([style]) > :last-child { margin-bottom: 0; }
blockquote:not([class]):not([style]) > :first-child { margin-top: 0; }
/* <table> — flat data table, theme-matched borders, header pill,
* row hover, last-row borderless, no zebra (kept calm). For numeric
* columns, add align="right" or class="num" to <th>/<td>. */
table:not([class]):not([style]) {
width: 100%;
border-collapse: collapse;
margin: 12px 0;
font-size: 13px;
color: var(--color-text-primary);
font-family: var(--font-sans);
}
table:not([class]):not([style]) caption {
text-align: left;
font-size: 13px;
font-weight: 500;
color: var(--color-text-secondary);
padding: 0 0 8px;
caption-side: top;
}
table:not([class]):not([style]) th {
text-align: left;
padding: 8px 12px;
/* Reset all sides so a model's `border:` shorthand can't leak through. */
border: none;
border-bottom: 0.5px solid var(--color-border-secondary);
font-weight: 500;
font-size: 11px;
color: var(--color-text-secondary);
text-transform: uppercase;
letter-spacing: 0.04em;
background: var(--color-bg-secondary);
white-space: nowrap;
}
table:not([class]):not([style]) td {
padding: 10px 12px;
border: none;
border-bottom: 0.5px solid var(--color-border-tertiary);
vertical-align: top;
}
table:not([class]):not([style]) tr:last-child > td {
border-bottom: none;
}
table:not([class]):not([style]) tbody tr {
transition: background-color 0.1s ease;
}
table:not([class]):not([style]) tbody tr:hover {
background: var(--color-bg-secondary);
}
/* Numeric columns: opt-in via align="right" or class="num" on cells. */
table:not([class]):not([style]) td[align="right"],
table:not([class]):not([style]) th[align="right"],
table:not([class]):not([style]) td.num,
table:not([class]):not([style]) th.num {
text-align: right;
font-variant-numeric: tabular-nums;
}
mark:not([class]):not([style]) {
background: var(--ramp-amber-fill);
color: var(--ramp-amber-th);
padding: 0 4px;
border-radius: 3px;
}
/* <dl> three modes — default stacked, data-layout="grid", data-layout="inline".
* data-layout is the explicit opt-in, so [data-layout] rules skip the class/style gate. */
dl:not([class]):not([style]) { margin: 12px 0; }
dl:not([class]):not([style]) > dt {
font-weight: 500;
color: var(--color-text-primary);
font-size: 14px;
margin-top: 12px;
}
dl:not([class]):not([style]) > dt:first-child { margin-top: 0; }
dl:not([class]):not([style]) > dd {
margin: 4px 0 0;
font-size: 13px;
color: var(--color-text-secondary);
}
/* `display: contents` on the optional wrapping div + dual selectors below
* tolerates both flat <dt><dd>… and <div><dt><dd></div>… markup. */
dl[data-layout="grid"] {
display: grid;
grid-template-columns: max-content 1fr;
gap: 8px 16px;
align-items: baseline;
padding: 12px 16px;
border: 0.5px solid var(--color-border-tertiary);
border-radius: var(--radius-md);
background: var(--color-bg-secondary);
margin: 12px 0;
}
dl[data-layout="grid"] > div { display: contents; }
dl[data-layout="grid"] > dt,
dl[data-layout="grid"] > div > dt {
font-weight: 400;
color: var(--color-text-secondary);
font-size: 13px;
margin: 0;
}
dl[data-layout="grid"] > dd,
dl[data-layout="grid"] > div > dd {
margin: 0;
text-align: right;
color: var(--color-text-primary);
font-weight: 500;
font-size: 13px;
}
/* data-layout="inline" — pill row. Each <dt>/<dd> pair wrapped in <div>.
* Same opt-in-via-attribute logic as grid above — no :not() gate. */
dl[data-layout="inline"] {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin: 12px 0;
}
dl[data-layout="inline"] > div {
display: inline-flex;
align-items: baseline;
gap: 4px;
padding: 4px 10px;
border: 0.5px solid var(--color-border-tertiary);
border-radius: 999px;
font-size: 12px;
background: var(--color-bg-secondary);
}
dl[data-layout="inline"] > div > dt {
margin: 0;
font-weight: 400;
color: var(--color-text-secondary);
font-size: 12px;
}
dl[data-layout="inline"] > div > dt::after {
content: ":";
margin-right: 2px;
}
dl[data-layout="inline"] > div > dd {
margin: 0;
font-weight: 500;
color: var(--color-text-primary);
font-size: 12px;
}
#iv-dl-wrap{position:fixed;top:4px;right:4px;z-index:9999}
#iv-dl-btn{width:26px;height:26px;padding:0;display:flex;align-items:center;justify-content:center;
opacity:0.3;border-color:var(--color-border-tertiary);background:var(--color-bg-primary)}
#iv-dl-btn:hover{opacity:0.9;background:var(--color-bg-secondary)}
#iv-dl-btn svg{width:14px;height:14px;stroke:var(--color-text-secondary);fill:none;
stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round}
/* --- Print ---
* overflow:hidden on html/body clips content in print (needed on screen
* for iframe sizing). Chart.js canvas scaling is handled by JS beforeprint
* handler in BODY_SCRIPTS — it directly mutates inline styles that CSS
* cannot reliably override in Chrome's print engine.
*/
@media print {
@page { margin: 12mm; }
html, body { overflow: visible !important; height: auto !important;
background: #fff !important; }
body { padding: 4px !important; }
#iv-dl-wrap { display: none !important; }
* { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
"""
# ---------------------------------------------------------------------------
# Injected JavaScript — theme detection (head), height reporting & bridges (body)
# ---------------------------------------------------------------------------
# Theme script runs in <head> before user content so CSS vars are resolved
# when model scripts read them at parse time.
#
# !! SRCDOC SAFETY !! Do NOT write the literal tokens <!-- , --> ,
# <![CDATA[ , ]]> , <script> or </script> ANYWHERE in this body —
# not even inside JS comments. The iframe srcdoc's HTML5 tokenizer
# treats them as parser state changes regardless of JS context, and
# silently breaks the IIFE (see _assert_srcdoc_safe near the bottom
# of this file for the runtime guard).
THEME_DETECTION_SCRIPT = """
<script>
(function() {
function detectTheme(root) {
return root.classList.contains('dark')
|| root.getAttribute('data-theme') === 'dark'
|| getComputedStyle(root).colorScheme === 'dark';
}
function applyTheme(isDark) {
var theme = isDark ? 'dark' : 'light';
if (document.documentElement.getAttribute('data-theme') === theme) return;
document.documentElement.setAttribute('data-theme', theme);
if (window.Chart && Chart.instances) {
var s = getComputedStyle(document.documentElement);
var tc = s.getPropertyValue('--color-text-secondary').trim();
var gc = s.getPropertyValue('--color-border-tertiary').trim();
Chart.defaults.color = tc;
Chart.defaults.borderColor = gc;
Object.values(Chart.instances).forEach(function(chart) {
Object.values(chart.options.scales || {}).forEach(function(scale) {
if (scale.ticks) scale.ticks.color = tc;
if (scale.grid) scale.grid.color = gc;
});
var leg = (chart.options.plugins || {}).legend;
if (leg && leg.labels) leg.labels.color = tc;
chart.update();
});
}
}
try {
var p = parent.document.documentElement;
applyTheme(detectTheme(p));
new MutationObserver(function() {
applyTheme(detectTheme(p));
}).observe(p, { attributes: true, attributeFilter: ['class', 'data-theme', 'style'] });
} catch(e) {
// No same-origin access — fall back to OS preference.
var mq = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
if (mq) {
applyTheme(mq.matches);
mq.addEventListener('change', function(e) { applyTheme(e.matches); });
}
}
})();
</script>
"""
# !! SRCDOC SAFETY !! Do NOT write the literal tokens <!-- , --> ,
# <![CDATA[ , ]]> , <script> or </script> ANYWHERE in this body —
# not even inside JS comments. See THEME_DETECTION_SCRIPT for full rationale.
BODY_SCRIPTS = """
<script>
// --- Height reporting ---
var _rh_last = 0; // last reported height
var _rh_consecutive = 0; // consecutive small-growth reports
var _rh_raf = 0; // rAF id for debouncing ResizeObserver
function reportHeight() {
var b = document.body;
// Measure SVG overflow before the body collapse below — getBBox
// needs normal layout.
var svgOverflow = 0;
document.querySelectorAll('svg[viewBox]').forEach(function(svg) {
try {
var bbox = svg.getBBox();
var vb = svg.viewBox.baseVal;
if (vb && vb.width > 0 && vb.height > 0) {
var overflow = bbox.y + bbox.height - (vb.y + vb.height);
if (overflow > 0) {
var scale = svg.getBoundingClientRect().width / vb.width;
svgOverflow += Math.ceil(overflow * scale);
}
}
} catch(e) {}
});
// Force height:auto on body + direct children — vh in an auto-sized
// iframe tracks iframe height, creating a feedback loop.
var savedBody = b.style.cssText;
b.style.setProperty('height', 'auto', 'important');
b.style.setProperty('overflow', 'visible', 'important');
b.style.setProperty('display', 'block', 'important');
var saved = [];
Array.from(b.children).forEach(function(el) {
if (el.nodeType !== 1) return;
saved.push({ el: el, css: el.style.cssText });
el.style.setProperty('height', 'auto', 'important');
el.style.setProperty('max-height', 'none', 'important');
el.style.setProperty('min-height', '0', 'important');
el.style.setProperty('overflow', 'visible', 'important');
});
// Collapse any descendant with viewport-unit dimensions — 100vh
// resolves to our own reported height, so leaving it intact
// creates a feedback loop where body grows each cycle.
var savedVh = [];
try {
var vhUsers = b.querySelectorAll(
'[style*="vh"], [style*="vw"], [style*="vmin"], [style*="vmax"]'
);
for (var k = 0; k < vhUsers.length; k++) {
var ve = vhUsers[k];
savedVh.push({ el: ve, css: ve.style.cssText });
ve.style.setProperty('min-height', '0', 'important');
ve.style.setProperty('max-height', 'none', 'important');
ve.style.setProperty('height', 'auto', 'important');
}
} catch(e) {}
var h = b.scrollHeight + svgOverflow;
b.style.cssText = savedBody;
saved.forEach(function(s) { s.el.style.cssText = s.css; });
for (var v = 0; v < savedVh.length; v++) {
savedVh[v].el.style.cssText = savedVh[v].css;
}
// Loop guard: 3+ consecutive small monotonic increases → stop.
var delta = h - _rh_last;
if (_rh_last > 0 && delta > 0 && delta < 50) {
_rh_consecutive++;
if (_rh_consecutive >= 3) return;
} else {
_rh_consecutive = 0;
}
_rh_last = h;
parent.postMessage({ type: 'iframe:height', height: h }, '*');
}
window.addEventListener('load', reportHeight);
window.addEventListener('resize', reportHeight);
// rAF-debounced ResizeObserver avoids tight synchronous loops.
new ResizeObserver(function() {
cancelAnimationFrame(_rh_raf);
_rh_raf = requestAnimationFrame(reportHeight);
}).observe(document.body);
// <details> toggle — ResizeObserver misses this in some browsers.
document.addEventListener('toggle', function() {
_rh_consecutive = 0;
setTimeout(reportHeight, 50);
}, true);
// Dynamic content swaps (innerHTML assignments, SPA-style updates).
var _rh_mutRaf = 0;
new MutationObserver(function() {
_rh_consecutive = 0;
cancelAnimationFrame(_rh_mutRaf);
_rh_mutRaf = requestAnimationFrame(reportHeight);
}).observe(document.body, { childList: true, subtree: true });
// Click covers custom expand/collapse via style.display / class swaps.
document.addEventListener('click', function() {
_rh_consecutive = 0;
cancelAnimationFrame(_rh_mutRaf);
_rh_mutRaf = requestAnimationFrame(reportHeight);
}, true);
// --- Post-render fixes (theme defaults, overlap prevention) ---
window.addEventListener('load', function() {
// Chart.js theme defaults + legend overflow prevention
if (window.Chart) {
var s = getComputedStyle(document.documentElement);
var textColor = s.getPropertyValue('--color-text-secondary').trim();
var gridColor = s.getPropertyValue('--color-border-tertiary').trim();
Chart.defaults.color = textColor;
Chart.defaults.borderColor = gridColor;
Chart.defaults.plugins.legend.labels.color = textColor;
Chart.defaults.plugins.legend.maxHeight = 120;
Chart.defaults.plugins.legend.labels.boxWidth = 12;
Chart.defaults.plugins.legend.labels.font = { size: 11 };
Object.values(Chart.instances || {}).forEach(function(chart) {
var leg = chart.options.plugins && chart.options.plugins.legend;
if (leg) {
leg.maxHeight = leg.maxHeight || 120;
if (leg.labels) {
leg.labels.boxWidth = leg.labels.boxWidth || 12;
}
}
chart.update();
});
}
// De-overlap SVG axis labels only — add data-no-stagger on a <svg>
// to opt out.
document.querySelectorAll('svg').forEach(function(svg) {
if (svg.hasAttribute('data-no-stagger')) return;
var texts = Array.from(svg.querySelectorAll('text'));
if (texts.length < 4) return;
var items = [];
texts.forEach(function(t) {
var r = t.getBoundingClientRect();
if (r.width < 1) return;
items.push({ el: t, rect: r, cx: r.left + r.width / 2, cy: r.top + r.height / 2 });
});
if (items.length < 4) return;
// Only touch texts in a narrow y-band (axis labels). Diagrams with
// texts spread across the canvas are left alone.
var minY = Infinity, maxY = -Infinity;
items.forEach(function(it) {
if (it.cy < minY) minY = it.cy;
if (it.cy > maxY) maxY = it.cy;
});
var ySpan = maxY - minY;
if (ySpan < 1) return;
// Pick the densest y-band (likely the axis row).
var bandSize = 30;
var bestBand = [], bestCount = 0;
items.forEach(function(anchor) {
var band = items.filter(function(it) { return Math.abs(it.cy - anchor.cy) < bandSize; });
if (band.length > bestCount) { bestCount = band.length; bestBand = band; }
});
if (bestBand.length < 3 || bestBand.length === items.length && ySpan > 60) return;
var groups = [];
bestBand.forEach(function(it) {
for (var i = 0; i < groups.length; i++) {
if (Math.abs(groups[i].cx - it.cx) < 15) {
groups[i].items.push(it);
return;
}
}
groups.push({ cx: it.cx, items: [it] });
});
if (groups.length < 3) return;
groups.sort(function(a, b) { return a.cx - b.cx; });
var needsStagger = false;
for (var i = 0; i < groups.length - 1; i++) {
var maxR = 0, minL = Infinity;
groups[i].items.forEach(function(it) { if (it.rect.right > maxR) maxR = it.rect.right; });
groups[i+1].items.forEach(function(it) { if (it.rect.left < minL) minL = it.rect.left; });
if (maxR > minL - 2) { needsStagger = true; break; }
}
if (needsStagger) {
for (var i = 1; i < groups.length; i += 2) {
groups[i].items.forEach(function(it) {
var cy = parseFloat(it.el.getAttribute('y') || 0);
it.el.setAttribute('y', String(cy + 18));
});
}
}
});
setTimeout(reportHeight, 100);
});
// --- sendPrompt bridge (requires iframe Sandbox Allow Same Origin) ---
function sendPrompt(text) {
try {
// Open WebUI's native prompt-submit postMessage — queues if the
// model is mid-generation.
parent.postMessage({ type: 'input:prompt:submit', text: text }, '*');
} catch(e) { /* iframe sandbox restriction */ }
}
// --- Open link in parent window ---
function openLink(url) {
try { parent.window.open(url, '_blank'); }
catch(e) { window.open(url, '_blank'); }
}
// --- navigator.vibrate silencer ---
// Chrome spams `[Intervention] Blocked call to navigator.vibrate…` on
// every call without a prior user gesture. Replace with a no-op so the
// block path never runs.
try {
if (typeof navigator !== 'undefined' && navigator.vibrate) {
navigator.vibrate = function() { return false; };
}
} catch(e) {}
// --- Toast bridge ---
// Floating auto-dismissing top-right banner. kind = success/info/warn/error.
function toast(msg, kind) {
kind = kind || 'success';
var color = kind === 'error' ? 'var(--color-text-danger)'
: kind === 'info' ? 'var(--color-text-info)'
: kind === 'warn' ? 'var(--color-text-warning)'
: 'var(--color-text-success)';
var wrap = document.getElementById('iv-toast-wrap');
if (!wrap) {
wrap = document.createElement('div');
wrap.id = 'iv-toast-wrap';
wrap.style.cssText =
'position:fixed;top:4px;right:38px;z-index:9998;' +
'display:flex;flex-direction:column;gap:4px;pointer-events:none;' +
'max-width:280px;';
document.body.appendChild(wrap);
}
var el = document.createElement('div');
el.style.cssText =
'padding:6px 12px;border-radius:var(--radius-md);' +
'background:var(--color-bg-secondary);' +
'border:0.5px solid var(--color-border-tertiary);' +
'color:' + color + ';font-size:12px;line-height:1.4;' +
'font-family:var(--font-sans);font-weight:500;' +
'opacity:0;transform:translateY(-4px);transition:all 0.2s ease;' +
'pointer-events:auto;white-space:nowrap;' +
'overflow:hidden;text-overflow:ellipsis;';
el.textContent = String(msg == null ? '' : msg);
wrap.appendChild(el);
requestAnimationFrame(function() {
el.style.opacity = '1';
el.style.transform = 'none';
});
setTimeout(function() {
el.style.opacity = '0';
el.style.transform = 'translateY(-4px)';
setTimeout(function() { if (el.parentNode) el.parentNode.removeChild(el); }, 220);
}, 2200);
}
// --- copyText bridge ---
// Async Clipboard API with execCommand fallback (Open WebUI's iframe
// sandbox lacks allow-clipboard-write). Toast fires unconditionally —
// execCommand can silently fail and swallowing feedback leaves the user
// confused. silent=true suppresses the toast.
function copyText(text, silent) {
var s = String(text == null ? '' : text);
var label = (typeof _ivCopiedStr !== 'undefined' &&
(_ivCopiedStr[_ivLang] || _ivCopiedStr.en)) || 'Copied';
function fire() { if (!silent) try { toast(label, 'success'); } catch(e) {} }
function legacy() {
try {
var ta = document.createElement('textarea');
ta.value = s;
ta.setAttribute('readonly', '');
ta.style.cssText =
'position:fixed;left:-9999px;top:-9999px;opacity:0;';
document.body.appendChild(ta);
ta.focus();
ta.select();
try { ta.setSelectionRange(0, s.length); } catch(e) {}
try { document.execCommand('copy'); } catch(e) {}
ta.remove();
} catch(e) {}
fire();
}
try {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(s).then(fire, legacy);
return;
}
} catch(e) {}
legacy();
}
// --- saveState / loadState bridges ---
// parent.localStorage proxy scoped to the assistant message id — state
// persists across reloads but never leaks between chats / messages.
// Silent no-op if localStorage / parent is unreachable.
function _ivStatePrefix() {
try {
var f = window.frameElement;
var msg = f && f.closest && f.closest('[id^="message-"]');
return 'iv-state:' + ((msg && msg.id) || 'global') + ':';
} catch(e) { return 'iv-state:global:'; }
}
function saveState(key, value) {
try {
parent.localStorage.setItem(
_ivStatePrefix() + String(key),
JSON.stringify(value === undefined ? null : value)
);
} catch(e) {}
}
function loadState(key, fallback) {
try {
var v = parent.localStorage.getItem(_ivStatePrefix() + String(key));
if (v == null) return fallback === undefined ? null : fallback;
return JSON.parse(v);
} catch(e) { return fallback === undefined ? null : fallback; }
}
/*__CHIME_BLOCK__*/
// --- Print fix for Chart.js canvases ---
// Chart.js writes explicit pixel widths as inline styles that CSS
// max-width can't override in Chrome's print engine. Mutate inline
// styles before print, restore after.
(function() {
window.addEventListener('beforeprint', function() {
document.querySelectorAll('canvas').forEach(function(c) {
c.setAttribute('data-print-style', c.style.cssText);