mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 01:47:19 +01:00
The model button, the bring-your-own-key sheet and the link under a failed
turn ("Get a pass") were built in Heidi. They are the chat's experience, so
they belong here: ModelChip for the header, ModelSheet driven by the
product's vendor allowlist and key check, and ChatFailure.action/retry so a
transport can say what the reader can do next and when Retry cannot help.
Claude-Session: https://claude.ai/code/session_019MDtmteJuDGFouA1itJ5tM
Co-authored-by: Claude <noreply@anthropic.com>
171 lines
5.9 KiB
JavaScript
171 lines
5.9 KiB
JavaScript
// The model button, the model sheet and the way on under a failed turn:
|
|
// what a product offering "bring your own key" or an allowance used to build
|
|
// for itself (Heidi, 2026-11-26).
|
|
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 {
|
|
ChatMessage,
|
|
ChatTurnError,
|
|
ModelChip,
|
|
ModelSheet,
|
|
DEFAULT_MODEL_SHEET_LABELS,
|
|
chatTurnFailure,
|
|
} from "../dist/react/index.js";
|
|
|
|
const vendors = [
|
|
{
|
|
id: "openrouter",
|
|
label: "OpenRouter",
|
|
keyUrl: "https://openrouter.ai/keys",
|
|
keyHint: "sk-or-…",
|
|
modelExample: "vendor/model",
|
|
},
|
|
{ id: "groq", label: "Groq", keyUrl: "https://console.groq.com/keys" },
|
|
];
|
|
const sheet = (props) =>
|
|
renderToStaticMarkup(
|
|
h(ModelSheet, {
|
|
open: true,
|
|
onClose() {},
|
|
vendors,
|
|
current: null,
|
|
check: async () => ({ ok: false }),
|
|
onSave() {},
|
|
onClear() {},
|
|
...props,
|
|
}),
|
|
);
|
|
|
|
test("the model button is there without a key, says whose model answers, and names the reader's own", () => {
|
|
const free = renderToStaticMarkup(h(ModelChip, { model: null, label: "Model", onOpen() {} }));
|
|
assert.match(free, /<button[^>]*data-model-chip="free"/);
|
|
assert.match(free, />Model</);
|
|
const own = renderToStaticMarkup(
|
|
h(ModelChip, { model: "vendor/strong-one", label: "Model", onOpen() {} }),
|
|
);
|
|
assert.match(own, /data-model-chip="own"/);
|
|
assert.match(own, /aria-label="Model: vendor\/strong-one"/);
|
|
assert.match(own, /class="ck-model-dot"/);
|
|
});
|
|
|
|
test("the sheet lists the product's vendors, links where to get a key, and names the free models", () => {
|
|
const html = sheet({ freeModels: ["free/one", "free/two"] });
|
|
assert.match(html, /<dialog[^>]*class="ck-sheet"[^>]*aria-label="Connect your own model"/);
|
|
assert.match(html, /<option value="openrouter"[^>]*>OpenRouter<\/option>/);
|
|
assert.match(html, /<option value="groq"[^>]*>Groq<\/option>/);
|
|
assert.match(
|
|
html,
|
|
/href="https:\/\/openrouter\.ai\/keys"[^>]*target="_blank"[^>]*rel="noreferrer noopener"/,
|
|
);
|
|
assert.match(html, /data-model-free[\s\S]*free\/one[\s\S]*free\/two/);
|
|
// A key is never shown as text: a password field, no autocomplete, no spellcheck.
|
|
assert.match(
|
|
html,
|
|
/<input id="ck-model-key"[^>]*type="password"[^>]*autoComplete="off"[^>]*spellCheck="false"/,
|
|
);
|
|
assert.match(html, /placeholder="sk-or-…"/);
|
|
});
|
|
|
|
test("with a model of the reader's own, the sheet says so and offers to remove it, and the free list is not shown", () => {
|
|
const html = sheet({
|
|
current: { vendor: "groq", key: "gsk_saved_key_value", model: "vendor/mine" },
|
|
freeModels: ["free/one"],
|
|
});
|
|
assert.match(html, /Connected with vendor\/mine/);
|
|
assert.match(html, />Remove key</);
|
|
assert.equal(html.includes("data-model-free"), false);
|
|
// The saved key is in the field, never printed as text on the page.
|
|
assert.equal(
|
|
html.replace(/value="gsk_saved_key_value"/, "").includes("gsk_saved_key_value"),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("labels are the product's, and every one has a default", () => {
|
|
for (const [k, v] of Object.entries(DEFAULT_MODEL_SHEET_LABELS))
|
|
assert.ok(typeof v === "string" && v.trim(), `${k} has no default`);
|
|
const html = sheet({ labels: { title: "Eigenes Modell verbinden", vendor: "Anbieter" } });
|
|
assert.match(html, /aria-label="Eigenes Modell verbinden"/);
|
|
assert.match(html, />Anbieter</);
|
|
});
|
|
|
|
test("a failed turn carries its way on, and Retry only where trying again can help", () => {
|
|
const failure = chatTurnFailure(
|
|
new ChatTurnError("Today's free questions are used up.", {
|
|
kind: "limit",
|
|
action: { label: "Get a pass", href: "/en/pass" },
|
|
retry: false,
|
|
}),
|
|
);
|
|
assert.deepEqual(failure, {
|
|
message: "Today's free questions are used up.",
|
|
kind: "limit",
|
|
action: { label: "Get a pass", href: "/en/pass" },
|
|
retry: false,
|
|
});
|
|
|
|
const html = renderToStaticMarkup(
|
|
h(ChatMessage, {
|
|
message: { id: "a", role: "assistant", content: "", failed: true, failure },
|
|
onRetry() {},
|
|
}),
|
|
);
|
|
assert.match(html, /<a class="ck-action ck-failure-action" href="\/en\/pass">Get a pass<\/a>/);
|
|
assert.equal(html.includes("<button"), false, "Retry is offered where it cannot help");
|
|
|
|
// Without an action and without retry:false, Retry is there as before.
|
|
const plain = renderToStaticMarkup(
|
|
h(ChatMessage, {
|
|
message: {
|
|
id: "b",
|
|
role: "assistant",
|
|
content: "",
|
|
failed: true,
|
|
failure: { message: "No." },
|
|
},
|
|
onRetry() {},
|
|
}),
|
|
);
|
|
assert.match(plain, /class="ck-action"/);
|
|
assert.equal(plain.includes("ck-failure-action"), false);
|
|
});
|
|
|
|
test("an action that is not a plain web link is dropped: a transport cannot put script under a turn", () => {
|
|
for (const href of [
|
|
"javascript:alert(1)",
|
|
"//evil.example/x",
|
|
"data:text/html,x",
|
|
"http://plain.example",
|
|
"",
|
|
]) {
|
|
const failure = chatTurnFailure(new ChatTurnError("x", { action: { label: "Go", href } }));
|
|
assert.equal(failure.action, undefined, `${href} was kept`);
|
|
}
|
|
assert.deepEqual(
|
|
chatTurnFailure(
|
|
new ChatTurnError("x", { action: { label: "Go", href: "https://example.org/a" } }),
|
|
).action,
|
|
{
|
|
label: "Go",
|
|
href: "https://example.org/a",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("the stylesheet gives the new controls full-size targets and a 16px field", () => {
|
|
const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8");
|
|
// The way on is a .ck-action (a full-size target) with an outline.
|
|
assert.match(css, /\.ck-failure-action\s*\{[^}]*border:/);
|
|
for (const cls of ["ck-model-input", "ck-model-use", "ck-model-remove", "ck-model-link"]) {
|
|
assert.match(
|
|
css,
|
|
new RegExp(`\\.${cls}\\s*\\{[^}]*min-height:\\s*var\\(--ck-target\\)`),
|
|
`${cls} is smaller than a finger`,
|
|
);
|
|
}
|
|
assert.match(css, /\.ck-model-input\s*\{[^}]*font-size:\s*max\(16px/);
|
|
});
|