mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 21:28:27 +01:00
* fix(scroll): back at the bottom by hand, the jump button goes (0.22.1) A finger rocks back a pixel or two as it lifts; that last touchmove read as scrolling up and paused following at the bottom, so the jump button stayed over the reply chips with nowhere to jump to (OrangeCat's Diplodoctor chat on Android, 2026-11-01). A touch that ends at the very bottom, or a downward scroll that arrives there, resumes following. Browser test on ChatScreen at 390px with real touch: fails on 0.22.0, passes now; a real tap on the button still lands at the latest message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtms1H9g9uu7AdDgJzLQ * feat(replies): when an answer ends on a question, the chips answer it Under four questions about a sore finger, Diplodoctor's chips were "I have more info" and "Consult a doctor": a tap answered nothing. On its live model, 2 of 9 chip sets answered the question; 7 of 9 with this sentence. In REPLIES_INSTRUCTION, every fleet chat gets it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtms1H9g9uu7AdDgJzLQ --------- Co-authored-by: Claude <noreply@anthropic.com>
199 lines
8.1 KiB
JavaScript
199 lines
8.1 KiB
JavaScript
// Suggested replies: the format is one contract between a model and every
|
|
// chat in the fleet, so the parser is tested on what models actually write.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createElement as h } from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
import { extractReplies, REPLIES_INSTRUCTION, REPLIES_FENCE } from "../dist/index.js";
|
|
import { ChatThread, ChatReplies } from "../dist/react/index.js";
|
|
|
|
const answer =
|
|
'Want me to set it up?\n\n```quick_replies\n["Yes, do it", "Show me first", "Not now"]\n```';
|
|
|
|
test("replies: the block is taken out of the answer and returned as buttons", () => {
|
|
const { text, replies } = extractReplies(answer);
|
|
assert.equal(text, "Want me to set it up?");
|
|
assert.deepEqual(replies, ["Yes, do it", "Show me first", "Not now"]);
|
|
});
|
|
|
|
test("replies: an answer without a block is untouched", () => {
|
|
assert.deepEqual(extractReplies("Done."), { text: "Done.", replies: [] });
|
|
});
|
|
|
|
test("replies: while streaming, a half-written block is hidden, never shown as JSON", () => {
|
|
for (const partial of [
|
|
"Want me to set it up?\n```quick_re",
|
|
'Want me to set it up?\n```quick_replies\n["Yes, do',
|
|
"Want me to set it up?\n```q",
|
|
]) {
|
|
assert.equal(extractReplies(partial).text, "Want me to set it up?", partial);
|
|
}
|
|
});
|
|
|
|
test("replies: an ordinary code block is never mistaken for one", () => {
|
|
const code = "Run this:\n```sh\npnpm i\n```";
|
|
assert.equal(extractReplies(code).text, code);
|
|
assert.equal(extractReplies("Run this:\n```").text, "Run this:\n```");
|
|
});
|
|
|
|
test("replies: capped, deduplicated, and sentences dropped", () => {
|
|
const long = "x".repeat(80);
|
|
const { replies } = extractReplies(
|
|
'```quick_replies\n["A", "a", "B", "' + long + '", "C", "D", "E", 3]\n```',
|
|
);
|
|
assert.deepEqual(replies, ["A", "B", "C", "D"]);
|
|
});
|
|
|
|
test("replies: one per line is accepted when the model skips the JSON", () => {
|
|
const { replies } = extractReplies('```quick_replies\n- Yes\n- "No"\n```');
|
|
assert.deepEqual(replies, ["Yes", "No"]);
|
|
});
|
|
|
|
test("replies: a bare JSON array the model ends on, with no fence, is the replies", () => {
|
|
const text =
|
|
'Under Swiss law a dismissal during sick leave is not automatically void.\n\n["Ask if notice was respected", "Learn about abusive dismissal", "Consult a lawyer"]';
|
|
assert.deepEqual(extractReplies(text), {
|
|
text: "Under Swiss law a dismissal during sick leave is not automatically void.",
|
|
replies: ["Ask if notice was respected", "Learn about abusive dismissal", "Consult a lawyer"],
|
|
});
|
|
// Not while streaming: the array may still be growing.
|
|
assert.deepEqual(extractReplies(text, { streaming: true }).replies, []);
|
|
// An array that is part of the answer (code, data) stays text.
|
|
const code = "The config is a list:\n\n[1, 2, 3]";
|
|
assert.deepEqual(extractReplies(code), { text: code, replies: [] });
|
|
});
|
|
|
|
test("replies: malformed JSON yields no buttons and no raw text", () => {
|
|
const { text, replies } = extractReplies('Ok.\n```quick_replies\n{"a": 1}\n```');
|
|
assert.equal(text, "Ok.");
|
|
assert.deepEqual(replies, []);
|
|
});
|
|
|
|
const BLOCK = '\n\n```quick_replies\n["Yes, do it", "Not now"]\n```';
|
|
|
|
test("replies: a heading the model writes over the replies is dropped, in any language", () => {
|
|
for (const label of [
|
|
"**Quick follow-up options**",
|
|
"### Suggested replies",
|
|
"**Mögliche Rückfragen:**",
|
|
"_Mögliche Rückfragen:_",
|
|
"## Быстрые ответы",
|
|
"**Варианты ответа**:",
|
|
]) {
|
|
const { text, replies } = extractReplies("Here is the answer.\n\n" + label + BLOCK);
|
|
assert.equal(text, "Here is the answer.", label);
|
|
assert.equal(replies.length, 2, label);
|
|
}
|
|
});
|
|
|
|
test("replies: a rule with a heading above the replies is dropped too", () => {
|
|
for (const tail of ["---\n\n**Quick replies**", "**Quick replies**\n\n---", "---"]) {
|
|
assert.equal(extractReplies("Answer.\n\n" + tail + BLOCK).text, "Answer.", tail);
|
|
}
|
|
});
|
|
|
|
test("replies: a bold sentence that concludes the answer is kept", () => {
|
|
for (const kept of [
|
|
"**China holds 99 %.**",
|
|
"**This is a real conclusion with more than six words**",
|
|
]) {
|
|
const answerText = "Gallium is refined, not mined.\n\n" + kept;
|
|
assert.equal(extractReplies(answerText + BLOCK).text, answerText, kept);
|
|
}
|
|
});
|
|
|
|
test("replies: a heading in the middle of an answer, or with no replies, is kept", () => {
|
|
const mid = "## Plan\n\nStep one.";
|
|
assert.equal(extractReplies(mid + BLOCK).text, mid);
|
|
assert.equal(extractReplies("Intro.\n\n**Quick replies**").text, "Intro.\n\n**Quick replies**");
|
|
});
|
|
|
|
test("replies: while streaming, the heading over the replies never shows", () => {
|
|
for (const partial of [
|
|
"Answer.\n\n**Quick fo",
|
|
"Answer.\n\n**Quick follow-up options**",
|
|
"Answer.\n\n**Quick follow-up options**\n\n",
|
|
"Answer.\n\n**Quick follow-up options**\n\n```",
|
|
"Answer.\n\n**Quick follow-up options**\n\n```quick_rep",
|
|
'Answer.\n\n---\n\n### Mögliche Rückfragen\n\n```quick_replies\n["Ja',
|
|
]) {
|
|
assert.equal(extractReplies(partial, { streaming: true }).text, "Answer.", partial);
|
|
}
|
|
// A real section shows as soon as its first words arrive.
|
|
const section = "Answer.\n\n## Plan\n\nStep one is";
|
|
assert.equal(extractReplies(section, { streaming: true }).text, section);
|
|
});
|
|
|
|
test("replies: the instruction forbids a heading over the replies", () => {
|
|
assert.match(REPLIES_INSTRUCTION, /Do not write any heading or label before it/);
|
|
});
|
|
|
|
test("replies: the instruction makes the model lead — every answer, best reply first", () => {
|
|
assert.match(REPLIES_INSTRUCTION, /End every answer/);
|
|
assert.match(REPLIES_INSTRUCTION, /FIRST/);
|
|
// A question asked is answered by the chips (Diplodoctor, 2026-11-01:
|
|
// "I have more info" under four questions answered none of them).
|
|
assert.match(
|
|
REPLIES_INSTRUCTION,
|
|
/ends on a question to them, the replies are short answers to that question/,
|
|
);
|
|
});
|
|
|
|
test("replies: the instruction teaches the same fence the parser reads", () => {
|
|
assert.match(REPLIES_INSTRUCTION, new RegExp("```" + REPLIES_FENCE));
|
|
const example = REPLIES_INSTRUCTION.slice(REPLIES_INSTRUCTION.indexOf("```"));
|
|
assert.ok(extractReplies("x\n" + example.split("\nRules")[0]).replies.length >= 2);
|
|
});
|
|
|
|
test("thread: replies show under the LATEST answer only, as buttons, with the block stripped", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [
|
|
{ id: "1", role: "assistant", content: 'Old.\n```quick_replies\n["Stale"]\n```' },
|
|
{ id: "2", role: "user", content: "hi" },
|
|
{ id: "3", role: "assistant", content: answer },
|
|
],
|
|
onReply: () => {},
|
|
}),
|
|
);
|
|
assert.doesNotMatch(html, /quick_replies/);
|
|
assert.doesNotMatch(html, /Stale/, "an older answer's replies are gone");
|
|
assert.equal((html.match(/class="ck-reply(?: ck-reply-lead)?"/g) ?? []).length, 3);
|
|
assert.equal(
|
|
(html.match(/class="ck-reply ck-reply-lead"/g) ?? []).length,
|
|
1,
|
|
"the first reply is the lead — the recommended next step",
|
|
);
|
|
assert.match(html, /role="group" aria-label="Suggested replies"/);
|
|
});
|
|
|
|
test("thread: no buttons while a turn is in flight, and none without onReply", () => {
|
|
const messages = [{ id: "1", role: "assistant", content: answer }];
|
|
const live = renderToStaticMarkup(
|
|
h(ChatThread, { messages, onReply: () => {}, live: { text: "Thinking" } }),
|
|
);
|
|
assert.doesNotMatch(live, /ck-reply"/);
|
|
const none = renderToStaticMarkup(h(ChatThread, { messages }));
|
|
assert.doesNotMatch(none, /ck-reply"/);
|
|
assert.doesNotMatch(
|
|
none,
|
|
/quick_replies/,
|
|
"the block is stripped even when no app asked for buttons",
|
|
);
|
|
});
|
|
|
|
test("thread: explicit replies win over a block in the content", () => {
|
|
const html = renderToStaticMarkup(
|
|
h(ChatThread, {
|
|
messages: [{ id: "1", role: "assistant", content: answer, replies: ["Mine"] }],
|
|
onReply: () => {},
|
|
}),
|
|
);
|
|
assert.match(html, />Mine</);
|
|
assert.doesNotMatch(html, />Not now</);
|
|
});
|
|
|
|
test("ChatReplies: renders nothing for no replies (no empty group to announce)", () => {
|
|
assert.equal(renderToStaticMarkup(h(ChatReplies, { replies: [], onPick: () => {} })), "");
|
|
});
|