@cliptude/cli 0.1.19 → 0.1.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,314 @@
1
+ /* Cliptude MCP App bridge: JSON-RPC 2.0 over postMessage with the host (MCP Apps, SEP-1865).
2
+ * Spec: https://modelcontextprotocol.io/specification/draft/extensions/apps
3
+ * View -> Host requests: ui/initialize, ui/message, ui/update-model-context, ui/open-link, tools/call
4
+ * View -> Host notifications: ui/notifications/initialized, ui/notifications/size-changed
5
+ * Host -> View notifications: ui/notifications/tool-input, ui/notifications/tool-result,
6
+ * ui/notifications/tool-cancelled, ui/notifications/host-context-changed
7
+ * Host -> View requests: ui/resource-teardown, ping
8
+ * Inlined into every widget by cliptude_mcp/apps.py, so the served HTML has no external script. */
9
+ var Cliptude = (function () {
10
+ "use strict";
11
+ var PROTOCOL_VERSION = "2026-01-26";
12
+ var nextId = 1;
13
+ var pending = {};
14
+ var handlers = { input: null, result: null, cancelled: null, context: null };
15
+ var state = { hostContext: {}, hostCapabilities: {}, toolInput: null, connected: false };
16
+
17
+ function post(message) {
18
+ if (window.parent && window.parent !== window) window.parent.postMessage(message, "*");
19
+ }
20
+
21
+ function request(method, params) {
22
+ var id = nextId++;
23
+ return new Promise(function (resolve, reject) {
24
+ pending[id] = { resolve: resolve, reject: reject };
25
+ post({ jsonrpc: "2.0", id: id, method: method, params: params || {} });
26
+ setTimeout(function () {
27
+ if (pending[id]) {
28
+ delete pending[id];
29
+ reject(new Error(method + " timed out"));
30
+ }
31
+ }, 60000);
32
+ });
33
+ }
34
+
35
+ function notify(method, params) {
36
+ post({ jsonrpc: "2.0", method: method, params: params || {} });
37
+ }
38
+
39
+ function respond(id, result) {
40
+ post({ jsonrpc: "2.0", id: id, result: result || {} });
41
+ }
42
+
43
+ function applyContext(ctx) {
44
+ if (!ctx) return;
45
+ var root = document.documentElement;
46
+ if (ctx.theme === "dark" || ctx.theme === "light") {
47
+ root.setAttribute("data-theme", ctx.theme);
48
+ root.style.colorScheme = ctx.theme;
49
+ }
50
+ var styles = ctx.styles || {};
51
+ var vars = styles.variables || {};
52
+ Object.keys(vars).forEach(function (key) {
53
+ if (vars[key] != null && key.indexOf("--") === 0) root.style.setProperty(key, vars[key]);
54
+ });
55
+ if (styles.css && styles.css.fonts && !document.getElementById("host-fonts")) {
56
+ var tag = document.createElement("style");
57
+ tag.id = "host-fonts";
58
+ tag.textContent = styles.css.fonts;
59
+ document.head.appendChild(tag);
60
+ }
61
+ var dims = ctx.containerDimensions || {};
62
+ if (dims.maxHeight) root.style.maxHeight = dims.maxHeight + "px";
63
+ if (handlers.context) handlers.context(state.hostContext);
64
+ }
65
+
66
+ function merge(target, source) {
67
+ Object.keys(source || {}).forEach(function (key) { target[key] = source[key]; });
68
+ return target;
69
+ }
70
+
71
+ window.addEventListener("message", function (event) {
72
+ if (event.source !== window.parent) return;
73
+ var msg = event.data;
74
+ if (!msg || msg.jsonrpc !== "2.0") return;
75
+ if (msg.method === undefined && msg.id !== undefined && msg.id !== null) {
76
+ var waiter = pending[msg.id];
77
+ if (!waiter) return;
78
+ delete pending[msg.id];
79
+ if (msg.error) waiter.reject(new Error(msg.error.message || "Host error"));
80
+ else waiter.resolve(msg.result || {});
81
+ return;
82
+ }
83
+ var params = msg.params || {};
84
+ if (msg.id !== undefined && msg.id !== null) {
85
+ // Requests from the host.
86
+ if (msg.method === "ui/resource-teardown" || msg.method === "ping") {
87
+ Media.stop();
88
+ respond(msg.id, {});
89
+ } else {
90
+ post({ jsonrpc: "2.0", id: msg.id, error: { code: -32601, message: "Method not found" } });
91
+ }
92
+ return;
93
+ }
94
+ switch (msg.method) {
95
+ case "ui/notifications/tool-input":
96
+ state.toolInput = params.arguments || {};
97
+ if (handlers.input) handlers.input(state.toolInput);
98
+ break;
99
+ case "ui/notifications/tool-result":
100
+ if (handlers.result) handlers.result(resultData(params), params);
101
+ break;
102
+ case "ui/notifications/tool-cancelled":
103
+ if (handlers.cancelled) handlers.cancelled(params.reason || "");
104
+ break;
105
+ case "ui/notifications/host-context-changed":
106
+ merge(state.hostContext, params);
107
+ applyContext(params);
108
+ break;
109
+ }
110
+ });
111
+
112
+ /** The tool's data: structuredContent, else the JSON in its first text block. */
113
+ function resultData(result) {
114
+ if (!result) return null;
115
+ if (result.structuredContent && typeof result.structuredContent === "object") return result.structuredContent;
116
+ var blocks = result.content || [];
117
+ for (var i = 0; i < blocks.length; i++) {
118
+ if (blocks[i] && blocks[i].type === "text") {
119
+ try { return JSON.parse(blocks[i].text); } catch (e) { return { error: blocks[i].text }; }
120
+ }
121
+ }
122
+ return null;
123
+ }
124
+
125
+ var lastSize = "";
126
+ function reportSize() {
127
+ var el = document.documentElement;
128
+ var width = Math.ceil(el.scrollWidth);
129
+ var height = Math.ceil(document.body ? document.body.scrollHeight : el.scrollHeight);
130
+ var key = width + "x" + height;
131
+ if (key === lastSize) return;
132
+ lastSize = key;
133
+ notify("ui/notifications/size-changed", { width: width, height: height });
134
+ }
135
+
136
+ function connect(options) {
137
+ handlers.input = options.onInput || null;
138
+ handlers.result = options.onResult || null;
139
+ handlers.cancelled = options.onCancelled || null;
140
+ handlers.context = options.onContext || null;
141
+ if (!window.parent || window.parent === window) return Promise.resolve(false);
142
+ return request("ui/initialize", {
143
+ appInfo: { name: options.name, version: "1.0.0" },
144
+ appCapabilities: { availableDisplayModes: ["inline"] },
145
+ protocolVersion: PROTOCOL_VERSION
146
+ }).then(function (result) {
147
+ state.hostContext = result.hostContext || {};
148
+ state.hostCapabilities = result.hostCapabilities || {};
149
+ state.connected = true;
150
+ applyContext(state.hostContext);
151
+ notify("ui/notifications/initialized", {});
152
+ if (window.ResizeObserver) new ResizeObserver(reportSize).observe(document.body);
153
+ reportSize();
154
+ return true;
155
+ });
156
+ }
157
+
158
+ /** Post a user message into the conversation (ui/message); falls back to model context. */
159
+ function sendMessage(text) {
160
+ return request("ui/message", { role: "user", content: [{ type: "text", text: text }] }).then(function (res) {
161
+ if (res && res.isError) throw new Error("The host did not accept the message");
162
+ return res;
163
+ });
164
+ }
165
+
166
+ function updateModelContext(text, structured) {
167
+ var params = { content: [{ type: "text", text: text }] };
168
+ if (structured) params.structuredContent = structured;
169
+ return request("ui/update-model-context", params);
170
+ }
171
+
172
+ function openLink(url) {
173
+ return request("ui/open-link", { url: url });
174
+ }
175
+
176
+ function callTool(name, args) {
177
+ return request("tools/call", { name: name, arguments: args || {} }).then(resultData);
178
+ }
179
+
180
+ function canCallTools() {
181
+ return !!state.hostCapabilities.serverTools;
182
+ }
183
+
184
+ return {
185
+ connect: connect,
186
+ sendMessage: sendMessage,
187
+ updateModelContext: updateModelContext,
188
+ openLink: openLink,
189
+ callTool: callTool,
190
+ canCallTools: canCallTools,
191
+ reportSize: reportSize,
192
+ state: state
193
+ };
194
+ })();
195
+
196
+ /* One preview playing at a time across the widget (audio or video). */
197
+ var Media = (function () {
198
+ "use strict";
199
+ var current = null;
200
+ var onChange = null;
201
+ function stop() {
202
+ if (current) {
203
+ try { current.el.pause(); } catch (e) {}
204
+ var was = current;
205
+ current = null;
206
+ if (onChange) onChange(was.key, false);
207
+ }
208
+ }
209
+ function toggleAudio(key, url) {
210
+ if (current && current.key === key) { stop(); return false; }
211
+ stop();
212
+ var audio = new Audio(url);
213
+ audio.preload = "none";
214
+ current = { key: key, el: audio };
215
+ audio.addEventListener("ended", function () { if (current && current.el === audio) stop(); });
216
+ audio.addEventListener("error", function () {
217
+ if (current && current.el === audio) { stop(); UI.toast("That preview could not be played."); }
218
+ });
219
+ var played = audio.play();
220
+ if (played && played.catch) played.catch(function () {
221
+ if (current && current.el === audio) { stop(); UI.toast("That preview could not be played."); }
222
+ });
223
+ if (onChange) onChange(key, true);
224
+ return true;
225
+ }
226
+ function claimVideo(key, video) {
227
+ if (current && current.el !== video) stop();
228
+ current = { key: key, el: video };
229
+ if (onChange) onChange(key, true);
230
+ }
231
+ function release(video) {
232
+ if (current && current.el === video) { current = null; }
233
+ }
234
+ return {
235
+ stop: stop, toggleAudio: toggleAudio, claimVideo: claimVideo, release: release,
236
+ onChange: function (fn) { onChange = fn; },
237
+ playing: function () { return current ? current.key : null; }
238
+ };
239
+ })();
240
+
241
+ /* Small DOM helpers shared by the widgets. */
242
+ var UI = (function () {
243
+ "use strict";
244
+ function h(tag, attrs, children) {
245
+ var el = document.createElement(tag);
246
+ Object.keys(attrs || {}).forEach(function (key) {
247
+ var value = attrs[key];
248
+ if (value === null || value === undefined || value === false) return;
249
+ if (key === "class") el.className = value;
250
+ else if (key === "text") el.textContent = value;
251
+ else if (key.indexOf("on") === 0 && typeof value === "function") el.addEventListener(key.slice(2), value);
252
+ else if (key === "style" && typeof value === "object") Object.keys(value).forEach(function (k) { el.style[k] = value[k]; });
253
+ else el.setAttribute(key, value === true ? "" : value);
254
+ });
255
+ (children || []).forEach(function (child) {
256
+ if (child === null || child === undefined || child === false) return;
257
+ el.appendChild(typeof child === "string" ? document.createTextNode(child) : child);
258
+ });
259
+ return el;
260
+ }
261
+ function clear(el) { while (el.firstChild) el.removeChild(el.firstChild); return el; }
262
+ var toastTimer = null;
263
+ function toast(text) {
264
+ var el = document.getElementById("toast");
265
+ if (!el) return;
266
+ el.textContent = text;
267
+ el.classList.add("show");
268
+ clearTimeout(toastTimer);
269
+ toastTimer = setTimeout(function () { el.classList.remove("show"); }, 3200);
270
+ }
271
+ /** Send a choice to the chat. If the host refuses ui/message, keep it in model context and say so. */
272
+ function choose(text, structured) {
273
+ return Cliptude.sendMessage(text).then(function () {
274
+ toast("Sent to the chat.");
275
+ return true;
276
+ }).catch(function () {
277
+ return Cliptude.updateModelContext(text, structured).then(function () {
278
+ toast("Saved. Tell Claude to continue and it will use your choice.");
279
+ return true;
280
+ }).catch(function () {
281
+ toast("Copy this into the chat: " + text);
282
+ return false;
283
+ });
284
+ });
285
+ }
286
+ function status(el, text, kind) {
287
+ clear(el).appendChild(h("div", { class: "status " + (kind || ""), role: kind === "error" ? "alert" : "status" }, [text]));
288
+ }
289
+ function initials(name) {
290
+ var letters = String(name || "").replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter(Boolean);
291
+ return (letters.slice(0, 2).map(function (p) { return p[0]; }).join("") || "?").toUpperCase();
292
+ }
293
+ function img(src, alt, fallbackText) {
294
+ var box = h("span", { class: "thumb" });
295
+ if (src) {
296
+ var image = h("img", { src: src, alt: alt || "", loading: "lazy", decoding: "async", referrerpolicy: "no-referrer" });
297
+ image.addEventListener("error", function () {
298
+ box.replaceChild(h("span", { class: "thumb-fallback", "aria-hidden": "true" }, [fallbackText || ""]), image);
299
+ }, { once: true });
300
+ box.appendChild(image);
301
+ } else {
302
+ box.appendChild(h("span", { class: "thumb-fallback", "aria-hidden": "true" }, [fallbackText || ""]));
303
+ }
304
+ return box;
305
+ }
306
+ function errorText(data) {
307
+ if (!data) return "Nothing came back from Cliptude.";
308
+ if (typeof data.error === "string" && data.error) {
309
+ return data.error.indexOf("<") >= 0 ? "Cliptude returned an error" + (data.status ? " (" + data.status + ")" : "") + "." : data.error;
310
+ }
311
+ return "";
312
+ }
313
+ return { h: h, clear: clear, toast: toast, choose: choose, status: status, initials: initials, img: img, errorText: errorText };
314
+ })();
@@ -0,0 +1,108 @@
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">
6
+ <title>Choose a character</title>
7
+ <style>
8
+ /*@BASE_CSS@*/
9
+ .thumb img { object-fit: contain; background: #fff; }
10
+ .card .desc { color: var(--color-text-secondary); font-size: var(--font-text-xs-size); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
11
+ </style>
12
+ </head>
13
+ <body>
14
+ <main class="app" aria-labelledby="title">
15
+ <div class="head">
16
+ <h1 id="title">Choose a Vector Explainer character</h1>
17
+ <span class="sub" id="count"></span>
18
+ </div>
19
+ <div class="toolbar">
20
+ <label class="sr-only" for="filter">Filter characters</label>
21
+ <input type="search" id="filter" placeholder="Filter characters" autocomplete="off">
22
+ </div>
23
+ <div id="content"><div class="status" role="status">Loading characters…</div></div>
24
+ <div class="selection" id="selection">
25
+ <span class="what" id="selected-text">Pick a character, or go without one.</span>
26
+ <span class="toolbar">
27
+ <button id="none" type="button">No character</button>
28
+ <button class="primary" id="use" type="button" disabled>Use this character</button>
29
+ </span>
30
+ </div>
31
+ </main>
32
+ <div id="toast" role="status" aria-live="polite"></div>
33
+ <script>
34
+ /*@BRIDGE_JS@*/
35
+ (function () {
36
+ "use strict";
37
+ var h = UI.h;
38
+ var data = { builtin: [], uploads: [] };
39
+ var selected = null;
40
+ var content = document.getElementById("content");
41
+
42
+ function matches(c, q) {
43
+ return !q || [c.name, c.descriptor, c.description, c.id].join(" ").toLowerCase().indexOf(q) >= 0;
44
+ }
45
+
46
+ function card(c) {
47
+ return h("button", {
48
+ type: "button", class: "card", role: "listitem", "aria-pressed": String(!!selected && selected.id === c.id),
49
+ "aria-label": c.name + (c.descriptor ? ", " + c.descriptor : ""),
50
+ onclick: function () { select(c); }
51
+ }, [
52
+ UI.img(c.preview_url, "", UI.initials(c.name)),
53
+ h("span", { class: "body" }, [
54
+ h("span", { class: "name" }, [c.name || "Character"]),
55
+ c.descriptor ? h("span", { class: "chips" }, [h("span", { class: "chip" }, [c.descriptor])]) : null,
56
+ c.description ? h("span", { class: "desc" }, [c.description]) : null
57
+ ])
58
+ ]);
59
+ }
60
+
61
+ function section(title, rows) {
62
+ if (!rows.length) return null;
63
+ var grid = h("div", { class: "grid", role: "list", "aria-label": title });
64
+ rows.forEach(function (c) { grid.appendChild(card(c)); });
65
+ return h("section", { class: "list" }, [h("h2", { class: "section-title" }, [title]), grid]);
66
+ }
67
+
68
+ function render() {
69
+ var q = document.getElementById("filter").value.trim().toLowerCase();
70
+ var mine = (data.uploads || []).filter(function (c) { return matches(c, q); });
71
+ var cast = (data.builtin || []).filter(function (c) { return matches(c, q); });
72
+ document.getElementById("count").textContent =
73
+ (data.builtin || []).length + " built-in" + ((data.uploads || []).length ? " · " + data.uploads.length + " yours" : "");
74
+ if (!mine.length && !cast.length) { UI.status(content, q ? "No character matches that filter." : "No characters yet."); return; }
75
+ UI.clear(content).appendChild(h("div", { class: "list" }, [section("Your characters", mine), section("Built-in cast", cast)]));
76
+ }
77
+
78
+ function select(c) {
79
+ selected = c;
80
+ render();
81
+ document.getElementById("use").disabled = false;
82
+ document.getElementById("selected-text").textContent = "“" + c.name + "” · " + c.id;
83
+ }
84
+
85
+ document.getElementById("filter").addEventListener("input", render);
86
+ document.getElementById("use").addEventListener("click", function () {
87
+ if (!selected) return;
88
+ UI.choose("Use the Vector Explainer character “" + selected.name + "” (vector_character_id " + selected.id + ").",
89
+ { vector_character_id: selected.id, vector_character_name: selected.name });
90
+ });
91
+ document.getElementById("none").addEventListener("click", function () {
92
+ UI.choose("Make the Vector Explainer video with no character (leave vector_character_id unset).", { vector_character_id: null });
93
+ });
94
+
95
+ Cliptude.connect({
96
+ name: "cliptude-character-picker",
97
+ onResult: function (result) {
98
+ var err = UI.errorText(result);
99
+ if (err) { UI.status(content, err, "error"); return; }
100
+ data = result || data;
101
+ render();
102
+ },
103
+ onCancelled: function () { UI.status(content, "Cancelled."); }
104
+ }).catch(function () { UI.status(content, "This view could not connect to the chat.", "error"); });
105
+ })();
106
+ </script>
107
+ </body>
108
+ </html>
@@ -0,0 +1,230 @@
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">
6
+ <title>Choose a setting</title>
7
+ <style>
8
+ /*@BASE_CSS@*/
9
+ .row { cursor: pointer; }
10
+ .desc-clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; color: var(--color-text-secondary); font-size: var(--font-text-xs-size); }
11
+ .caption-stage { aspect-ratio: 16 / 9; display: flex; align-items: flex-end; justify-content: center; padding: 12px;
12
+ background: linear-gradient(160deg, #3b4a5c, #10161f); }
13
+ .caption-stage span { font-size: 18px; text-align: center; line-height: 1.2; }
14
+ .theme-stage { aspect-ratio: 16 / 9; display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 700;
15
+ background: var(--color-background-tertiary); color: var(--color-text-tertiary); }
16
+ .video-toggle { position: absolute; right: 6px; bottom: 6px; background: rgba(0,0,0,.6); color: #fff; border: 0; }
17
+ .play-overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
18
+ .play-overlay span { background: rgba(0,0,0,.55); color: #fff; border-radius: 99px; padding: 6px 12px; font-size: 12px; }
19
+ .current { border-style: dashed; }
20
+ </style>
21
+ </head>
22
+ <body>
23
+ <main class="app" aria-labelledby="title">
24
+ <div class="head">
25
+ <h1 id="title">Choose a setting</h1>
26
+ <span class="sub" id="count"></span>
27
+ </div>
28
+ <p class="sub" id="description" hidden></p>
29
+ <div class="toolbar" id="toolbar" hidden>
30
+ <label class="sr-only" for="filter">Filter</label>
31
+ <input type="search" id="filter" placeholder="Filter" autocomplete="off">
32
+ </div>
33
+ <div id="content"><div class="status" role="status">Loading choices…</div></div>
34
+ <div class="selection" id="selection" hidden>
35
+ <span class="what" id="selected-text"></span>
36
+ <button class="primary" id="use" type="button">Use this</button>
37
+ </div>
38
+ </main>
39
+ <div id="toast" role="status" aria-live="polite"></div>
40
+ <script>
41
+ /*@BRIDGE_JS@*/
42
+ (function () {
43
+ "use strict";
44
+ var h = UI.h;
45
+ var PLAY = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M8 5v14l11-7z"/></svg>';
46
+ var PAUSE = '<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 5h4v14H6zm8 0h4v14h-4z"/></svg>';
47
+ var NOUN = { music: "music", image: "background", style: "style", caption: "caption style", theme: "theme", text: "option" };
48
+ var info = { field: "", kind: "text", choices: [] };
49
+ var selected = null;
50
+ var muted = true;
51
+ var content = document.getElementById("content");
52
+
53
+ function noun() { return NOUN[info.kind] || "option"; }
54
+ function isCurrent(c) { return info.current !== undefined && info.current !== null && String(info.current) === String(c.value); }
55
+
56
+ function visible() {
57
+ var q = document.getElementById("filter").value.trim().toLowerCase();
58
+ return (info.choices || []).filter(function (c) {
59
+ return !q || [c.value, c.label, c.hint, c.blurb, c.group].join(" ").toLowerCase().indexOf(q) >= 0;
60
+ });
61
+ }
62
+
63
+ function select(c) {
64
+ selected = c;
65
+ render();
66
+ document.getElementById("selection").hidden = false;
67
+ document.getElementById("use").textContent = c.none ? "Use no music" : "Use this " + noun();
68
+ document.getElementById("selected-text").textContent = c.none ? "No background music"
69
+ : "“" + (c.label || c.value) + "” · " + info.field + "=" + c.value;
70
+ }
71
+
72
+ function messageFor(c) {
73
+ if (c.none) return "Use no background music (background_music_mode=none).";
74
+ var label = c.label && c.label !== c.value ? " (“" + c.label + "”)" : "";
75
+ var where = info.usage ? " " + info.usage : "";
76
+ return "Use " + info.field + "=" + c.value + label + "." + where;
77
+ }
78
+
79
+ function selectable(el, c) {
80
+ el.setAttribute("aria-pressed", String(!!selected && selected.value === c.value && !!selected.none === !!c.none));
81
+ el.addEventListener("click", function () { select(c); });
82
+ if (el.tagName !== "BUTTON") {
83
+ el.tabIndex = 0;
84
+ el.addEventListener("keydown", function (e) { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); select(c); } });
85
+ }
86
+ if (isCurrent(c)) el.classList.add("current");
87
+ return el;
88
+ }
89
+
90
+ /* ---- music: list with play/pause ---- */
91
+ function musicRow(c) {
92
+ var key = "m:" + c.value;
93
+ var playing = Media.playing() === key;
94
+ var play = null;
95
+ if (c.preview_url) {
96
+ play = h("button", { type: "button", class: "icon", "aria-pressed": String(playing),
97
+ "aria-label": (playing ? "Pause " : "Play ") + (c.label || c.value),
98
+ onclick: function (e) { e.stopPropagation(); Media.toggleAudio(key, c.preview_url); } }, []);
99
+ play.innerHTML = playing ? PAUSE : PLAY;
100
+ }
101
+ var chips = [];
102
+ if (c.group) chips.push(h("span", { class: "chip" }, [c.group === "theme" ? "Documentary bed" : c.group === "library" ? "Library" : c.group]));
103
+ if (isCurrent(c)) chips.push(h("span", { class: "chip info" }, ["Default"]));
104
+ return selectable(h("div", { class: "row", role: "listitem" }, [
105
+ play,
106
+ h("div", { class: "main" }, [
107
+ h("span", { class: "name" }, [c.none ? "No music" : (c.label || c.value)]),
108
+ c.hint ? h("span", { class: "desc" }, [c.hint]) : null,
109
+ c.none ? h("span", { class: "desc" }, ["Narration only, no background track."]) :
110
+ (c.label && c.label !== c.value ? h("span", { class: "muted" }, [c.value]) : null),
111
+ chips.length ? h("span", { class: "chips" }, chips) : null
112
+ ])
113
+ ]), c);
114
+ }
115
+
116
+ /* ---- style video previews: poster, click to play, one at a time ---- */
117
+ function videoThumb(c) {
118
+ var box = h("span", { class: "thumb r16x9" });
119
+ var key = "v:" + c.value;
120
+ var video = null;
121
+ if (c.poster_url) box.appendChild(h("img", { src: c.poster_url, alt: "", loading: "lazy" }));
122
+ var overlay = h("span", { class: "play-overlay" }, [h("span", {}, ["▶ Preview"])]);
123
+ box.appendChild(overlay);
124
+ var sound = h("button", { type: "button", class: "icon video-toggle", "aria-label": muted ? "Unmute preview" : "Mute preview",
125
+ hidden: true, onclick: function (e) {
126
+ e.stopPropagation();
127
+ muted = !muted;
128
+ if (video) video.muted = muted;
129
+ sound.textContent = muted ? "🔇" : "🔊";
130
+ sound.setAttribute("aria-label", muted ? "Unmute preview" : "Mute preview");
131
+ } }, [muted ? "🔇" : "🔊"]);
132
+ box.appendChild(sound);
133
+ function start() {
134
+ if (!video) {
135
+ video = h("video", { src: c.preview_video_url, poster: c.poster_url || null, playsinline: true, loop: true, preload: "none" });
136
+ video.muted = muted;
137
+ video.addEventListener("error", function () { overlay.hidden = false; UI.toast("That preview could not be played."); });
138
+ box.insertBefore(video, box.firstChild);
139
+ var still = box.querySelector("img"); if (still) still.remove();
140
+ }
141
+ Media.claimVideo(key, video);
142
+ overlay.hidden = true;
143
+ sound.hidden = false;
144
+ var p = video.play(); if (p && p.catch) p.catch(function () { overlay.hidden = false; });
145
+ }
146
+ function pause() { if (video) { video.pause(); Media.release(video); } overlay.hidden = false; }
147
+ box.addEventListener("mouseenter", function () { if (Cliptude.state.hostContext.deviceCapabilities && Cliptude.state.hostContext.deviceCapabilities.hover === false) return; start(); });
148
+ box.addEventListener("mouseleave", pause);
149
+ box.addEventListener("click", function () { if (video && !video.paused) pause(); else start(); });
150
+ return box;
151
+ }
152
+
153
+ function captionStage(c) {
154
+ var css = c.caption_css || {};
155
+ return h("span", { class: "caption-stage", "aria-hidden": "true" }, [h("span", { style: {
156
+ fontFamily: css.font_family || "inherit", color: css.color || "#fff", textShadow: css.text_shadow || "none",
157
+ background: css.background || "transparent", padding: css.padding || "0", fontWeight: css.font_weight || "bold",
158
+ fontStyle: css.font_style || "normal"
159
+ } }, [c.sample_text || "YOUR CAPTION"])]);
160
+ }
161
+
162
+ function card(c) {
163
+ var media = null;
164
+ if (c.preview_video_url) media = videoThumb(c);
165
+ else if (c.image_url) { media = UI.img(c.image_url, "", ""); media.classList.add("r16x9"); }
166
+ else if (c.caption_css) media = captionStage(c);
167
+ else if (c.swatches && c.swatches.length) {
168
+ media = h("span", { class: "theme-stage", "aria-hidden": "true", style: { background: c.swatches[2] || c.swatches[0] } },
169
+ [h("span", { style: { color: c.swatches[0], borderBottom: "4px solid " + (c.swatches[1] || c.swatches[0]) } }, [c.label || c.value])]);
170
+ }
171
+ var chips = [];
172
+ if (c.group === "upload") chips.push(h("span", { class: "chip info" }, ["Your upload"]));
173
+ if (isCurrent(c)) chips.push(h("span", { class: "chip info" }, ["Default"]));
174
+ if (c.hint) chips.push(h("span", { class: "chip" }, [c.hint]));
175
+ return selectable(h("div", { class: "card", role: "listitem", "aria-label": c.label || c.value }, [
176
+ media,
177
+ h("span", { class: "body" }, [
178
+ h("span", { class: "name" }, [c.label || c.value]),
179
+ c.swatches && c.swatches.length ? h("span", { class: "swatches", "aria-label": "Colours " + c.swatches.join(", ") },
180
+ c.swatches.map(function (col) { return h("span", { class: "swatch", title: col, style: { background: col } }); })) : null,
181
+ c.blurb ? h("span", { class: "desc-clamp" }, [c.blurb]) : null,
182
+ chips.length ? h("span", { class: "chips" }, chips) : null,
183
+ c.label && c.label !== c.value ? h("span", { class: "muted" }, [c.value]) : null
184
+ ])
185
+ ]), c);
186
+ }
187
+
188
+ function render() {
189
+ var rows = visible();
190
+ document.getElementById("count").textContent = (info.choices || []).length + " choices";
191
+ if (!(info.choices || []).length) { UI.status(content, "No choices for this setting."); return; }
192
+ if (!rows.length) { UI.status(content, "Nothing matches that filter."); return; }
193
+ var box;
194
+ if (info.kind === "music") {
195
+ box = h("div", { class: "list", role: "list" });
196
+ if (!document.getElementById("filter").value) box.appendChild(musicRow({ value: "", none: true }));
197
+ rows.forEach(function (c) { box.appendChild(musicRow(c)); });
198
+ } else {
199
+ box = h("div", { class: info.kind === "image" ? "grid" : "grid wide", role: "list" });
200
+ rows.forEach(function (c) { box.appendChild(card(c)); });
201
+ }
202
+ UI.clear(content).appendChild(box);
203
+ }
204
+
205
+ Media.onChange(function (key) { if (String(key || "").indexOf("m:") === 0) render(); });
206
+ document.getElementById("filter").addEventListener("input", render);
207
+ document.getElementById("use").addEventListener("click", function () {
208
+ if (!selected) return;
209
+ var structured = {}; structured[info.field] = selected.none ? null : selected.value;
210
+ if (selected.none) structured = { background_music_mode: "none" };
211
+ UI.choose(messageFor(selected), structured);
212
+ });
213
+
214
+ Cliptude.connect({
215
+ name: "cliptude-choice-picker",
216
+ onResult: function (data) {
217
+ var err = UI.errorText(data);
218
+ if (err) { UI.status(content, err, "error"); return; }
219
+ info = data;
220
+ document.getElementById("title").textContent = data.label || ("Choose " + data.field);
221
+ if (data.description) { var d = document.getElementById("description"); d.textContent = data.description; d.hidden = false; }
222
+ document.getElementById("toolbar").hidden = (data.choices || []).length < 8;
223
+ render();
224
+ },
225
+ onCancelled: function () { UI.status(content, "Cancelled."); }
226
+ }).catch(function () { UI.status(content, "This view could not connect to the chat.", "error"); });
227
+ })();
228
+ </script>
229
+ </body>
230
+ </html>