mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 03:47:19 +01:00
* feat: chatkit v0.1 — one chat for every product The fleet had 13 chats in 10 repos, each written fresh, each missing what another had fixed. This extracts loki /loki (composer, thread, messages, markdown, attachments) and adds heidi's microphone and orangecat's keyboard fix: - Composer: auto-growing 16px textarea, Enter/Shift+Enter/IME, Send and Stop in one slot, attachments with paste, mic always present, every label translatable, app-specific controls in slots. - useDictation: browser recogniser first; missing or SILENT (4 s watchdog, bounded permission wait) -> record and transcribe on the server; a dead recogniser remembered 30 days; every failure shown in words. - ChatThread/ChatMessage: stick-to-bottom + jump button, speaker names once two agents speak, Copy on answers, Retry on the last one, failed turns visible. - Markdown with no HTML path; javascript: links stay text. - styles.css: --ck-* variables defaulting to @bitbaum/design-tokens. - verify = format, lint, types, build, 18 unit/render tests and 21 browser checks (390px + desktop, light + dark, a sentence through a fake mic past a silent recogniser). Browser checks mutation-tested (14px input, 60 s watchdog) — both fail as they should. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(styles): --ck-* defaults at zero specificity on :root, so an app's own values win Declared on the chat's elements they overrode any app setting --ck-* at the root — the opposite of what the README promised. Found preparing loki, whose colour variables are full OKLCH colours under the design-token names and so must map them. New browser check: a :root --ck-accent reaches the chat. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
98 lines
3.4 KiB
JavaScript
98 lines
3.4 KiB
JavaScript
// The components render on the server (Next RSC/SSR) and carry the parts the
|
|
// standard requires. Rendered to a string: a chat that cannot even render
|
|
// without a browser is a chat that breaks every Next page it is put on.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import { createElement as h } from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
import { Composer, ChatThread, ChatStarters, Markdown } from "../dist/react/index.js";
|
|
|
|
test("composer: textarea, send, and 16px guaranteed by the stylesheet", () => {
|
|
const html = renderToStaticMarkup(h(Composer, { onSend: () => {}, placeholder: "Ask" }));
|
|
assert.match(html, /<textarea[^>]*class="ck-input"/);
|
|
assert.match(html, /aria-label="Send"/);
|
|
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
|
|
assert.match(
|
|
css,
|
|
/\.ck-input\s*\{[^}]*font-size:\s*max\(16px/,
|
|
"input must be >= 16px (iOS zoom)",
|
|
);
|
|
assert.match(css, /\.ck-md\s*\{[^}]*font-size:\s*var\(--ck-text\)/);
|
|
assert.match(css, /--ck-text:\s*1rem/);
|
|
});
|
|
|
|
test("composer: labels are translatable (heidi is not in English)", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(Composer, { onSend: () => {}, placeholder: "Frag", labels: { send: "Senden" } }),
|
|
);
|
|
assert.match(html, /aria-label="Senden"/);
|
|
});
|
|
|
|
test("thread: names who speaks once two agents answer, and offers retry on the last answer", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [
|
|
{ id: "1", role: "user", content: "I want to earn" },
|
|
{
|
|
id: "2",
|
|
role: "assistant",
|
|
content: "Use **OrangeCat**.",
|
|
speaker: { name: "Cat", id: "cat" },
|
|
},
|
|
{
|
|
id: "3",
|
|
role: "assistant",
|
|
content: "I can build it.",
|
|
speaker: { name: "Loki", id: "loki" },
|
|
},
|
|
],
|
|
onRetry: () => {},
|
|
}),
|
|
);
|
|
assert.match(html, /ck-speaker">Cat</);
|
|
assert.match(html, /ck-speaker">Loki</);
|
|
assert.match(html, /<strong>OrangeCat<\/strong>/);
|
|
assert.equal((html.match(/Try again/g) ?? []).length, 1, "retry only on the last answer");
|
|
assert.match(html, /ck-from-cat/);
|
|
});
|
|
|
|
test("thread: a failed turn says so, with retry — never silence", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [
|
|
{ id: "1", role: "user", content: "hi" },
|
|
{ id: "2", role: "assistant", content: "", failed: true },
|
|
],
|
|
onRetry: () => {},
|
|
}),
|
|
);
|
|
assert.match(html, /ck-failed/);
|
|
assert.match(html, /ck-turn-pinned/, "a failed turn never hides its Retry behind a hover");
|
|
assert.match(html, /Try again/);
|
|
});
|
|
|
|
test("thread: empty shows the starters, live shows stop", () => {
|
|
const empty = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [],
|
|
empty: h(ChatStarters, { starters: ["What can you build?"], onPick: () => {} }),
|
|
}),
|
|
);
|
|
assert.match(empty, /What can you build\?/);
|
|
const live = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [{ id: "1", role: "user", content: "x" }],
|
|
live: {},
|
|
onStop: () => {},
|
|
}),
|
|
);
|
|
assert.match(live, /ck-stop/);
|
|
});
|
|
|
|
test("markdown never renders model text as HTML", () => {
|
|
const html = renderToStaticMarkup(h(Markdown, { text: "<img src=x onerror=alert(1)> **ok**" }));
|
|
assert.ok(!html.includes("<img"), "raw HTML must be escaped");
|
|
assert.match(html, /<img/);
|
|
});
|