wop commited on
Commit
6404fca
·
verified ·
1 Parent(s): dd73a3e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +374 -250
index.html CHANGED
@@ -1,258 +1,382 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>Amber Chat Visualizer — Dark Theme</title>
7
- <script src="https://cdn.tailwindcss.com"></script>
8
- <style>
9
- /* Dark Amber Palette */
10
- :root {
11
- --bg-dark: #1a1a1a; /* deep gray background */
12
- --bg-panel: #2a2a2a; /* panels slightly lighter */
13
- --amber-light: #f0c04a; /* soft amber highlight */
14
- --amber-muted: #d9b54a; /* for subtle accents */
15
- --text-light: #e0e0e0; /* main text */
16
- --text-muted: #b0a074; /* secondary text */
17
- --border: rgba(240,192,74,0.15);
18
- --scroll-thumb: rgba(240,192,74,0.25);
19
- --bubble-user: rgba(240,192,74,0.15);
20
- --bubble-assistant: rgba(255,255,255,0.05);
21
- --bubble-system: rgba(240,192,74,0.08);
22
- }
23
-
24
- body {
25
- background-color: var(--bg-dark);
26
- color: var(--text-light);
27
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
28
- }
29
-
30
- .custom-scrollbar::-webkit-scrollbar { width: 6px; }
31
- .custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
32
- .custom-scrollbar::-webkit-scrollbar-thumb {
33
- background: var(--scroll-thumb);
34
- border-radius: 10px;
35
- }
36
-
37
- .editable:focus {
38
- outline: none;
39
- background: rgba(240,192,74,0.05);
40
- border-bottom: 1px solid var(--amber-light);
41
- }
42
-
43
- .main-container { height: calc(100vh - 100px); }
44
- @media (max-width:768px){
45
- .main-container { flex-direction: column; overflow-y: auto; }
46
- .panel { height: 500px; width: 100% !important; flex-shrink: 0; }
47
- }
48
-
49
- header, footer {
50
- background: rgba(30,30,30,0.9);
51
- border-color: var(--border);
52
- backdrop-filter: blur(6px);
53
- }
54
-
55
- .panel {
56
- background: var(--bg-panel);
57
- border-color: var(--border);
58
- }
59
-
60
- /* Chat bubbles */
61
- .bubble-user {
62
- background: var(--bubble-user);
63
- color: var(--text-light);
64
- border-radius: 16px;
65
- padding: 0.75rem 1.25rem;
66
- max-width: 90%;
67
- box-shadow: 0 4px 12px rgba(0,0,0,0.2);
68
- }
69
- .bubble-assistant {
70
- background: var(--bubble-assistant);
71
- color: var(--text-light);
72
- border-radius: 16px;
73
- padding: 0.75rem 1.25rem;
74
- max-width: 90%;
75
- box-shadow: 0 4px 12px rgba(0,0,0,0.1);
76
- }
77
- .bubble-system {
78
- background: var(--bubble-system);
79
- color: var(--amber-muted);
80
- font-style: italic;
81
- border-left: 3px solid var(--amber-muted);
82
- padding-left: 12px;
83
- border-radius: 6px;
84
- }
85
-
86
- /* Thought boxes */
87
- .thought-box {
88
- background: rgba(240,192,74,0.05);
89
- border: 1px solid var(--border);
90
- padding: 8px;
91
- border-radius: 8px;
92
- }
93
- details > summary {
94
- cursor: pointer;
95
- padding: 4px;
96
- font-size: 10px;
97
- font-weight: bold;
98
- color: var(--amber-light);
99
- list-style: none;
100
- }
101
- details > summary::-webkit-details-marker { display: none; }
102
-
103
- </style>
104
  </head>
105
- <body class="flex flex-col h-screen overflow-hidden">
106
-
107
- <header class="flex items-center justify-between px-6 py-4 border-b shadow-sm">
108
- <div class="flex items-center space-x-3">
109
- <div class="p-2 rounded-lg bg-amber-light/20">
110
- <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 text-amber-light" fill="none" viewBox="0 0 24 24" stroke="currentColor">
111
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"/>
112
- </svg>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  </div>
114
- <h1 class="text-xl font-bold tracking-tight" style="color:var(--amber-light)">Amber Chat <span class="text-amber-muted font-light">Visualizer</span></h1>
115
- </div>
116
- <div class="flex items-center gap-4 text-xs font-mono text-amber-muted">
117
- <span class="hidden sm:inline">JSON + VISUAL SYNC</span>
118
- <button onclick="copyJson()" class="p-2 hover:bg-amber-light/10 rounded-full transition-colors">
119
- <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
120
- <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2"/>
121
- </svg>
122
- </button>
123
- </div>
124
- </header>
125
-
126
- <main class="main-container flex-1 flex overflow-hidden">
127
- <section class="panel w-1/2 flex flex-col border-r">
128
- <div class="px-4 py-2 bg-transparent border-b flex justify-between items-center">
129
- <span class="text-[10px] font-mono uppercase tracking-widest text-amber-muted">source.json</span>
130
- <span id="json-error" class="text-[10px] text-red-500 hidden">Invalid JSON</span>
131
  </div>
132
- <textarea id="json-editor"
133
- class="flex-1 p-6 bg-transparent text-amber-light font-mono text-sm outline-none resize-none custom-scrollbar" spellcheck="false"></textarea>
134
- </section>
135
-
136
- <section id="chat-preview" class="panel w-1/2 overflow-y-auto p-6 md:p-12 custom-scrollbar">
137
- <!-- bubbles injected -->
138
- </section>
139
- </main>
140
-
141
- <footer class="h-10 flex items-center justify-between px-6 border-t text-[10px] uppercase tracking-widest text-amber-muted">
142
- <div>Click text to edit visually</div>
143
- <div>Dark Amber Mode</div>
144
- </footer>
145
-
146
- <script>
147
- const jsonEditor = document.getElementById('json-editor');
148
- const chatPreview = document.getElementById('chat-preview');
149
- const errorLabel = document.getElementById('json-error');
150
-
151
- let data = {
152
- "messages":[
153
- { "role":"system","content":"You are a helpful assistant."},
154
- { "role":"user","content":"Tell me about this dark theme."},
155
- { "role":"assistant","content":"<think>The user wants a calmer dark version.</think>This is a dark amber theme: deep charcoal backgrounds, soft amber highlights, low-contrast bubbles for comfortable nighttime reading."}
156
- ]
157
- };
158
-
159
- jsonEditor.value = JSON.stringify(data,null,2);
160
- renderChat();
161
-
162
- jsonEditor.addEventListener('input', (e)=>{
163
- try{
164
- const parsed = JSON.parse(e.target.value);
165
- if(parsed.messages && Array.isArray(parsed.messages)){
166
- data=parsed; renderChat(); errorLabel.classList.add('hidden');
167
- }
168
- }catch(err){ errorLabel.classList.remove('hidden'); }
169
- });
170
-
171
- function renderChat(){
172
- chatPreview.innerHTML='';
173
- data.messages.forEach((msg,i)=>{ chatPreview.appendChild(createBubble(msg,i)); });
174
- }
175
-
176
- function createBubble(msg,index){
177
- const isUser=msg.role.toLowerCase()==='user';
178
- const isAssistant=msg.role.toLowerCase()==='assistant';
179
- const isSystem=msg.role.toLowerCase()==='system';
180
-
181
- const container=document.createElement('div');
182
- container.className=`flex flex-col mb-6 w-full ${isUser?'items-end':'items-start'}`;
183
-
184
- const label=document.createElement('div');
185
- label.contentEditable=true;
186
- label.className='text-[10px] font-semibold uppercase tracking-widest mb-1 px-1 outline-none';
187
- label.style.color=isUser?'var(--amber-light)':'var(--amber-muted)';
188
- label.innerText=msg.role;
189
- label.onblur=(e)=>updateData(index,'role',e.target.innerText);
190
-
191
- const body=document.createElement('div');
192
- let bubbleClass='';
193
- if(isUser) bubbleClass='bubble-user';
194
- else if(isSystem) bubbleClass='bubble-system';
195
- else bubbleClass='bubble-assistant';
196
- body.className=bubbleClass;
197
-
198
- const content=msg.content||"";
199
- const thinkMatch=content.match(/<think>([\s\S]*?)<\/think>/);
200
- const thoughtText=thinkMatch?thinkMatch[1]:"";
201
- const cleanText=content.replace(/<think>[\s\S]*?<\/think>/,'').trim();
202
-
203
- if(isAssistant||thoughtText){
204
- const details=document.createElement('details');
205
- details.open=!!thoughtText;
206
- details.className='thought-box';
207
- const summary=document.createElement('summary');
208
- summary.innerHTML='<span class="mr-2">▶</span> THOUGHT PROCESS';
209
- const thoughtDiv=document.createElement('div');
210
- thoughtDiv.contentEditable=true;
211
- thoughtDiv.className='p-2 text-xs';
212
- thoughtDiv.innerText=thoughtText;
213
- thoughtDiv.onblur=(e)=>updateData(index,'thought',e.target.innerText);
214
- details.appendChild(summary);
215
- details.appendChild(thoughtDiv);
216
- body.appendChild(details);
217
- }
218
-
219
- const mainText=document.createElement('div');
220
- mainText.contentEditable=true;
221
- mainText.className='outline-none whitespace-pre-wrap leading-relaxed';
222
- mainText.innerText=cleanText;
223
- mainText.onblur=(e)=>updateData(index,'content',e.target.innerText);
224
-
225
- body.appendChild(mainText);
226
- container.appendChild(label);
227
- container.appendChild(body);
228
- return container;
229
- }
230
-
231
- function updateData(index,field,value){
232
- const msg=data.messages[index];
233
- if(field==='role') msg.role=value;
234
- else if(field==='thought'){
235
- const current=msg.content||"";
236
- const clean=current.replace(/<think>[\s\S]*?<\/think>/,'').trim();
237
- msg.content=`<think>${value}</think>${clean}`;
238
- }
239
- else if(field==='content'){
240
- const current=msg.content||"";
241
- const thinkMatch=current.match(/<think>([\s\S]*?)<\/think>/);
242
- const thinkPart=thinkMatch?`<think>${thinkMatch[1]}</think>`:'';
243
- msg.content=thinkPart+value;
244
- }
245
- jsonEditor.value=JSON.stringify(data,null,2);
246
- }
247
-
248
- function copyJson(){
249
- jsonEditor.select();
250
- document.execCommand('copy');
251
- const btn=document.querySelector('button[onclick="copyJson()"]');
252
- const orig=btn.innerHTML;
253
- btn.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>';
254
- setTimeout(()=>btn.innerHTML=orig,1400);
255
- }
256
- </script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
  </body>
258
  </html>
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
  <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ember · token forge</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <style>
9
+ * { box-sizing: border-box; }
10
+ :root {
11
+ --bg-deep: #0f0f0f; --bg-panel: #1a1a1a; --amber-glow: #fbbf24;
12
+ --amber-soft: #d97706; --amber-muted: #b68b40; --text-luminous: #f5f2e8;
13
+ --text-dim: #a89f8e; --border-glow: rgba(251, 191, 36, 0.15);
14
+ --scroll-thumb: rgba(251, 191, 36, 0.3); --bubble-user: rgba(251, 191, 36, 0.12);
15
+ --bubble-assistant: rgba(245, 235, 200, 0.04); --bubble-system: rgba(180, 130, 40, 0.15);
16
+ --thought-bg: rgba(251, 191, 36, 0.06); --success: #a3e635;
17
+ }
18
+ body {
19
+ background: var(--bg-deep); color: var(--text-luminous);
20
+ font-family: 'Inter', system-ui, sans-serif; font-feature-settings: "cv02", "ss01";
21
+ }
22
+ .custom-scrollbar::-webkit-scrollbar { width: 5px; }
23
+ .custom-scrollbar::-webkit-scrollbar-track { background: transparent; }
24
+ .custom-scrollbar::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 20px; }
25
+
26
+ /* panels */
27
+ .panel-glass {
28
+ background: var(--bg-panel); border: 1px solid var(--border-glow);
29
+ border-radius: 20px; box-shadow: 0 20px 35px -15px black;
30
+ }
31
+
32
+ /* bubbles */
33
+ .bubble-user {
34
+ background: var(--bubble-user); border: 1px solid rgba(251,191,36,0.2);
35
+ border-radius: 22px 22px 6px 22px; padding: 0.8rem 1.4rem;
36
+ }
37
+ .bubble-assistant {
38
+ background: var(--bubble-assistant); border: 1px solid rgba(255,235,160,0.08);
39
+ border-radius: 22px 22px 22px 6px; padding: 0.8rem 1.4rem;
40
+ }
41
+ .bubble-system {
42
+ background: var(--bubble-system); border-left: 4px solid var(--amber-soft);
43
+ border-radius: 16px; padding: 0.8rem 1.4rem; color: #e6d7b0;
44
+ }
45
+
46
+ /* thought container */
47
+ .thought-container {
48
+ background: var(--thought-bg); border-radius: 20px;
49
+ border: 1px solid rgba(251,191,36,0.18); margin-bottom: 8px;
50
+ }
51
+ details.thought-container > summary {
52
+ cursor: pointer; list-style: none; padding: 0.4rem 1rem;
53
+ font-size: 0.65rem; font-weight: 600; letter-spacing: 1px;
54
+ color: var(--amber-glow); display: flex; align-items: center;
55
+ }
56
+ details.thought-container > summary::-webkit-details-marker { display: none; }
57
+ details.thought-container summary::before {
58
+ content: "▶"; font-size: 9px; color: var(--amber-glow);
59
+ display: inline-block; transition: 0.15s; margin-right: 6px;
60
+ }
61
+ details.thought-container[open] summary::before { content: "▼"; }
62
+ .thought-body { padding: 0 1rem 0.6rem 1rem; font-size: 0.8rem; color: #d4c29b; }
63
+
64
+ /* editable only for content/thought — role labels now static */
65
+ .editable-text, .editable-thought {
66
+ transition: all 0.1s ease; border-radius: 6px; cursor: text;
67
+ }
68
+ .editable-text:focus, .editable-thought:focus {
69
+ outline: 1px solid var(--amber-glow); background: rgba(251,191,36,0.04);
70
+ box-shadow: 0 0 0 2px rgba(251,191,36,0.2);
71
+ }
72
+
73
+ /* token badges – minimal */
74
+ .token-badge {
75
+ background: rgba(0,0,0,0.5); backdrop-filter: blur(2px);
76
+ border: 1px solid rgba(251,191,36,0.25); color: #d4b27e;
77
+ font-size: 0.6rem; padding: 0.2rem 0.6rem; border-radius: 30px;
78
+ white-space: nowrap; font-family: monospace;
79
+ }
80
+
81
+ /* global token meter */
82
+ .meter-bar {
83
+ height: 6px; background: #2a2a2a; border-radius: 30px; overflow: hidden;
84
+ width: 140px; box-shadow: inset 0 1px 4px black;
85
+ }
86
+ .meter-fill {
87
+ height: 100%; background: linear-gradient(90deg, #fbbf24, #d97706);
88
+ border-radius: 30px; width: 0%; transition: width 0.2s;
89
+ }
90
+
91
+ /* role badges (static now, just visual) */
92
+ .role-badge {
93
+ background: rgba(251,191,36,0.1); padding: 0.2rem 0.8rem;
94
+ border-radius: 30px; font-size: 0.65rem; font-weight: 500;
95
+ border: 1px solid rgba(251,191,36,0.15); color: var(--amber-glow);
96
+ text-transform: uppercase; letter-spacing: 0.04em;
97
+ }
98
+
99
+ .main-container { height: calc(100vh - 118px); gap: 8px; }
100
+ @media (max-width: 700px) { .main-container { flex-direction: column; height: auto; } }
101
+ </style>
 
 
102
  </head>
103
+ <body class="flex flex-col h-screen antialiased p-2">
104
+
105
+ <!-- header: token meter + next turn button -->
106
+ <header class="panel-glass flex items-center justify-between px-5 py-2.5 mx-1 mb-1">
107
+ <div class="flex items-center gap-4">
108
+ <div class="flex items-center gap-2">
109
+ <div class="p-2 rounded-xl bg-amber-500/10">
110
+ <svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-amber-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
111
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.7" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"/>
112
+ </svg>
113
+ </div>
114
+ <h2 class="text-lg font-semibold tracking-tight text-amber-400/90">token<span class="text-amber-600">forge</span></h2>
115
+ </div>
116
+
117
+ <!-- GLOBAL TOKEN COUNTER with editable limit -->
118
+ <div class="flex items-center gap-3 bg-black/30 px-3 py-1.5 rounded-full border border-amber-900/30">
119
+ <span class="text-xs text-amber-400/80">∑ tokens</span>
120
+ <span id="global-total" class="text-sm font-mono text-amber-300">0</span>
121
+ <span class="text-amber-700">/</span>
122
+ <input id="token-limit" type="number" value="32000" min="100" max="1000000" step="100"
123
+ class="w-20 bg-transparent border border-amber-800/50 rounded-md px-1.5 py-0.5 text-xs text-amber-200 font-mono text-right focus:outline-amber-500/50">
124
+ <div class="meter-bar"><div id="meter-fill" class="meter-fill" style="width:0%"></div></div>
125
+ <span id="percent-used" class="text-[0.6rem] text-amber-600/90 w-10">0%</span>
126
+ </div>
127
  </div>
128
+
129
+ <!-- NEXT TURN BUTTON -->
130
+ <div class="flex gap-2">
131
+ <button id="nextTurnBtn" class="flex items-center gap-1.5 bg-amber-600/20 hover:bg-amber-600/30 border border-amber-500/30 rounded-full px-5 py-2 text-xs font-medium text-amber-300 transition-all">
132
+ <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 9l3 3m0 0l-3 3m3-3H8m13 0a9 9 0 11-18 0 9 9 0 0118 0z"/></svg>
133
+ next turn
134
+ </button>
 
 
 
 
 
 
 
 
 
 
135
  </div>
136
+ </header>
137
+
138
+ <!-- main columns -->
139
+ <main class="main-container flex overflow-hidden p-1">
140
+ <!-- LEFT JSON -->
141
+ <section class="panel-glass w-1/2 flex flex-col overflow-hidden">
142
+ <div class="flex items-center justify-between px-5 py-2 border-b border-amber-900/30">
143
+ <span class="text-[0.65rem] font-mono tracking-wider text-amber-600/80">source.json</span>
144
+ <span id="json-error" class="text-red-400/90 text-[0.6rem] bg-red-950/40 px-2 py-0.5 rounded hidden">invalid</span>
145
+ </div>
146
+ <textarea id="json-editor" class="flex-1 p-5 bg-[#0c0c0c] text-amber-100/80 font-mono text-sm outline-none resize-none custom-scrollbar" spellcheck="false"></textarea>
147
+ </section>
148
+
149
+ <!-- RIGHT CHAT PREVIEW + token details inline -->
150
+ <section id="chat-preview" class="panel-glass w-1/2 overflow-y-auto p-5 custom-scrollbar space-y-5 relative">
151
+ <!-- chat bubbles go here -->
152
+ </section>
153
+ </main>
154
+
155
+ <!-- footer: subtle hint -->
156
+ <footer class="flex items-center justify-between px-5 py-1.5 text-[0.55rem] uppercase tracking-widest text-amber-700/60">
157
+ <div>↻ next turn auto‑selects user/assistant • roles static • token count per message (t)</div>
158
+ <div>⊙ token forge</div>
159
+ </footer>
160
+
161
+ <script>
162
+ (function() {
163
+ const jsonEditor = document.getElementById('json-editor');
164
+ const chatPreview = document.getElementById('chat-preview');
165
+ const errorLabel = document.getElementById('json-error');
166
+ const globalTotalSpan = document.getElementById('global-total');
167
+ const tokenLimitInput = document.getElementById('token-limit');
168
+ const meterFill = document.getElementById('meter-fill');
169
+ const percentSpan = document.getElementById('percent-used');
170
+ const nextTurnBtn = document.getElementById('nextTurnBtn');
171
+
172
+ // --- default data with alternating pattern ---
173
+ let data = {
174
+ "messages": [
175
+ { "role": "system", "content": "You are a helpful assistant in dark amber mode." },
176
+ { "role": "user", "content": "what is the token limit feeling?" },
177
+ { "role": "assistant", "content": "<think>they might be testing memory</think>We're at 0% used — you can adjust the limit anytime." }
178
+ ]
179
+ };
180
+
181
+ // simple token estimator (char ~0.25 token, but for demo we use rough)
182
+ function estimateTokens(str) {
183
+ if (!str) return 0;
184
+ // simulate tiktoken-ish: 4 chars per token on average
185
+ return Math.ceil(str.length / 3.8);
186
+ }
187
+
188
+ function computeAllTokens() {
189
+ let total = 0;
190
+ data.messages.forEach(msg => {
191
+ const content = msg.content || '';
192
+ total += estimateTokens(content);
193
+ // also count think part? already inside content. fine.
194
+ });
195
+ return total;
196
+ }
197
+
198
+ function updateGlobalMeter() {
199
+ const total = computeAllTokens();
200
+ const limit = parseInt(tokenLimitInput.value, 10) || 32000;
201
+ const percent = Math.min(100, ((total / limit) * 100).toFixed(1));
202
+ globalTotalSpan.innerText = total;
203
+ meterFill.style.width = percent + '%';
204
+ percentSpan.innerText = percent + '%';
205
+
206
+ // visual warning if >90%
207
+ if (percent > 90) {
208
+ meterFill.style.background = 'linear-gradient(90deg, #f97316, #dc2626)';
209
+ } else {
210
+ meterFill.style.background = 'linear-gradient(90deg, #fbbf24, #d97706)';
211
+ }
212
+ }
213
+
214
+ // per-message token display (tiny badge)
215
+ function createTokenBadge(count) {
216
+ const badge = document.createElement('span');
217
+ badge.className = 'token-badge ml-2';
218
+ badge.innerText = `⚡${count}`;
219
+ return badge;
220
+ }
221
+
222
+ // render everything
223
+ function renderChat() {
224
+ chatPreview.innerHTML = '';
225
+ data.messages.forEach((msg, idx) => {
226
+ const bubble = createBubble(msg, idx);
227
+ chatPreview.appendChild(bubble);
228
+ });
229
+ updateGlobalMeter();
230
+ }
231
+
232
+ function createBubble(msg, index) {
233
+ const role = msg.role || 'assistant';
234
+ const isUser = role === 'user';
235
+ const isAssistant = role === 'assistant';
236
+ const isSystem = role === 'system';
237
+
238
+ const container = document.createElement('div');
239
+ container.className = `flex flex-col w-full ${isUser ? 'items-end' : 'items-start'} mb-5 relative`;
240
+
241
+ // --- static role badge (NO contenteditable) ---
242
+ const labelRow = document.createElement('div');
243
+ labelRow.className = 'flex items-center gap-2 mb-1.5';
244
+
245
+ const roleSpan = document.createElement('span');
246
+ roleSpan.className = 'role-badge';
247
+ roleSpan.innerText = role;
248
+ labelRow.appendChild(roleSpan);
249
+
250
+ // add token badge for message (total tokens of content, includes thought)
251
+ const contentTokens = estimateTokens(msg.content || '');
252
+ const tokenBadge = createTokenBadge(contentTokens);
253
+ labelRow.appendChild(tokenBadge);
254
+
255
+ container.appendChild(labelRow);
256
+
257
+ // --- bubble body ---
258
+ const body = document.createElement('div');
259
+ let bubbleClass = 'bubble-assistant';
260
+ if (isUser) bubbleClass = 'bubble-user';
261
+ else if (isSystem) bubbleClass = 'bubble-system';
262
+ body.className = bubbleClass + ' w-fit max-w-[90%] break-words';
263
+
264
+ // extract thought
265
+ const raw = msg.content || '';
266
+ const thinkRegex = /<think>([\s\S]*?)<\/think>/;
267
+ const thinkMatch = raw.match(thinkRegex);
268
+ const thoughtText = thinkMatch ? thinkMatch[1].trim() : '';
269
+ const cleanText = raw.replace(thinkRegex, '').trim();
270
+
271
+ // thought section (only if assistant, but could appear)
272
+ if (isAssistant || thoughtText) {
273
+ const details = document.createElement('details');
274
+ details.className = 'thought-container';
275
+ if (thoughtText) details.open = true;
276
+
277
+ const summary = document.createElement('summary');
278
+ summary.innerText = ' thought process';
279
+
280
+ const thoughtDiv = document.createElement('div');
281
+ thoughtDiv.className = 'thought-body editable-thought outline-none';
282
+ thoughtDiv.contentEditable = true;
283
+ thoughtDiv.innerText = thoughtText;
284
+ thoughtDiv.onblur = (e) => updateData(index, 'thought', e.target.innerText);
285
+
286
+ details.appendChild(summary);
287
+ details.appendChild(thoughtDiv);
288
+ body.appendChild(details);
289
+ }
290
+
291
+ // main editable content
292
+ const mainDiv = document.createElement('div');
293
+ mainDiv.contentEditable = true;
294
+ mainDiv.className = 'editable-text outline-none whitespace-pre-wrap leading-relaxed text-[0.95rem]';
295
+ mainDiv.innerText = cleanText || '...';
296
+ mainDiv.onblur = (e) => updateData(index, 'content', e.target.innerText);
297
+
298
+ body.appendChild(mainDiv);
299
+ container.appendChild(body);
300
+ return container;
301
+ }
302
+
303
+ function updateData(index, field, value) {
304
+ const msg = data.messages[index];
305
+ if (!msg) return;
306
+
307
+ if (field === 'thought') {
308
+ const current = msg.content || '';
309
+ const clean = current.replace(/<think>[\s\S]*?<\/think>/, '').trim();
310
+ if (value.trim()) {
311
+ msg.content = `<think>${value.trim()}</think> ${clean}`;
312
+ } else {
313
+ msg.content = clean;
314
+ }
315
+ } else if (field === 'content') {
316
+ const current = msg.content || '';
317
+ const thinkMatch = current.match(/<think>([\s\S]*?)<\/think>/);
318
+ const thoughtPart = thinkMatch ? `<think>${thinkMatch[1]}</think>` : '';
319
+ msg.content = thoughtPart + (thoughtPart && value ? ' ' : '') + value;
320
+ }
321
+
322
+ // update json & re-render
323
+ jsonEditor.value = JSON.stringify(data, null, 2);
324
+ renderChat();
325
+ }
326
+
327
+ // listen to JSON edits
328
+ jsonEditor.addEventListener('input', (e) => {
329
+ try {
330
+ const parsed = JSON.parse(e.target.value);
331
+ if (parsed.messages && Array.isArray(parsed.messages)) {
332
+ data = parsed;
333
+ renderChat();
334
+ errorLabel.classList.add('hidden');
335
+ } else throw new Error();
336
+ } catch {
337
+ errorLabel.classList.remove('hidden');
338
+ }
339
+ });
340
+
341
+ // next turn: add user or assistant based on last role
342
+ function addNextTurn() {
343
+ const messages = data.messages;
344
+ if (!messages.length) return;
345
+
346
+ const lastRole = messages[messages.length - 1].role;
347
+ let newRole = 'user';
348
+ if (lastRole === 'user') newRole = 'assistant';
349
+ else if (lastRole === 'assistant') newRole = 'user';
350
+ else newRole = 'user'; // system? fallback to user
351
+
352
+ // default empty content
353
+ const newMsg = { role: newRole, content: '' };
354
+ data.messages.push(newMsg);
355
+ jsonEditor.value = JSON.stringify(data, null, 2);
356
+ renderChat();
357
+
358
+ // auto-scroll to bottom
359
+ setTimeout(() => {
360
+ chatPreview.scrollTo({ top: chatPreview.scrollHeight, behavior: 'smooth' });
361
+ }, 30);
362
+ }
363
+
364
+ nextTurnBtn.addEventListener('click', addNextTurn);
365
+
366
+ // token limit change
367
+ tokenLimitInput.addEventListener('input', updateGlobalMeter);
368
+
369
+ // initialize
370
+ jsonEditor.value = JSON.stringify(data, null, 2);
371
+ renderChat();
372
+
373
+ // optional: copy function (simple)
374
+ window.copyJson = function() {
375
+ jsonEditor.select();
376
+ navigator.clipboard?.writeText(jsonEditor.value);
377
+ // visual feedback can be added but not needed
378
+ };
379
+ })();
380
+ </script>
381
  </body>
382
  </html>