Spaces:
Running
Running
Update index.html
Browse files- 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>
|
| 7 |
-
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
-
<style>
|
| 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 |
-
</style>
|
| 104 |
</head>
|
| 105 |
-
<body class="flex flex-col h-screen
|
| 106 |
-
|
| 107 |
-
<header
|
| 108 |
-
<
|
| 109 |
-
<div class="
|
| 110 |
-
<
|
| 111 |
-
<
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
</div>
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 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 |
-
|
| 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 |
-
jsonEditor
|
| 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 |
</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>
|