Files
chatkit/dist/react/Thread.js
T
CatoandClaude 04dd6f86b8 feat: under every answer — Copy, Listen, Share; sources as a sheet; replies that lead (0.10.0) (#20)
The pieces of the ChatGPT app the owner put beside ours (2026-10-21), done
once here so Loki, OrangeCat and every chat in the fleet get them:

- `AnswerActions`: the row under an answer. Copy; Listen, reading the answer
  as prose (`plainTextForSpeech`, from loki) with the browser's voice or the
  `speaker` an app passes (loki's studio voice), with a small player — Stop
  and the time so far — while it reads; Share through the system sheet
  where one exists. `ChatThread` renders it under every answer.
- Sources on a touch screen are ONE "Sources · n" button that opens a sheet
  with every record (label, detail, link); chips stay on a mouse. `Sheet`
  (the attach sheet's <dialog>, extracted) and `SourcesRow` are exported.
- `AttachMenu` takes `extra` rows under Camera · Photos · Files, and then
  opens the sheet on a mouse too — an app's "one of my things" has no file
  dialog to live in.
- The replies instruction makes the model lead: 2–4 replies on every answer,
  the recommended next step first, drawn as the lead (`ck-reply-lead`).
- An empty box is one row on the narrowest phone: the placeholder is cut at
  the edge, never wrapped into a clipped second line.

Browser checks cover the player, the sources sheet on a touch screen and
chips on a mouse, and the one-row placeholder at 320px.


Claude-Session: https://claude.ai/code/session_018WZnBnaLP6RhwFkLxRTKgE

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-21 06:21:21 +02:00

79 lines
6.7 KiB
JavaScript

"use client";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { Markdown, defaultRenderLink } from "./Markdown.js";
import { useStickToBottom } from "./hooks.js";
import { extractReplies } from "../replies.js";
import { AnswerActions } from "./AnswerActions.js";
import { IconArrowDown, IconRetry, IconStop } from "./icons.js";
export const DEFAULT_THREAD_LABELS = {
copy: "Copy",
copied: "Copied",
listen: "Listen",
share: "Share",
retry: "Try again",
stop: "Stop",
working: "Working on it",
stopped: "Stopped. Send it again to retry.",
jump: "Jump to the latest message",
failed: "That did not go through.",
loading: "Loading conversation",
replies: "Suggested replies",
};
/**
* One turn. The asymmetry is deliberate: the person's own words get a pill —
* a short aside they already know — and the answer gets the full column with
* no box, because it is the thing being READ. Copy is on every answer; Retry
* only on the last one (retrying an older turn would fork the thread).
*/
export function ChatMessage({ message, onRetry, renderLink = defaultRenderLink, showSpeaker = true, footer, last = false, onReply, speaker, share, labels = DEFAULT_THREAD_LABELS, }) {
if (message.role === "user") {
return (_jsx("div", { className: "ck-turn ck-turn-user", children: _jsx("div", { className: "ck-bubble", children: message.content }) }));
}
const parsed = extractReplies(message.content);
const replies = message.replies ?? parsed.replies;
const cls = ["ck-turn", "ck-turn-answer"];
if (message.speaker?.id)
cls.push(`ck-from-${message.speaker.id}`);
// A failed turn and the latest answer never hide their buttons: Retry after
// a failure is the one thing the reader needs, and a hover it cannot do on
// a phone must not stand between them.
if (last || message.failed)
cls.push("ck-turn-pinned");
return (_jsxs("div", { className: cls.join(" "), children: [showSpeaker && message.speaker && _jsx("span", { className: "ck-speaker", children: message.speaker.name }), message.failed ? (_jsx("p", { className: "ck-failed", role: "status", children: message.content || labels.failed })) : (_jsx(Markdown, { text: parsed.text, citations: message.citations, renderLink: renderLink })), footer, message.failed ? (onRetry && (_jsx("div", { className: "ck-turn-actions", children: _jsxs("button", { type: "button", className: "ck-action", onClick: onRetry, children: [_jsx(IconRetry, {}), _jsx("span", { children: labels.retry })] }) }))) : (_jsx(AnswerActions, { text: parsed.text, onRetry: onRetry, speaker: speaker, share: share, labels: labels })), last && onReply && !message.failed && replies.length > 0 && (_jsx(ChatReplies, { replies: replies, onPick: onReply, label: labels.replies }))] }));
}
/**
* The replies under an answer. Exported for apps that render their own
* thread, so the buttons look and behave the same in every product.
*/
export function ChatReplies({ replies, onPick, label = DEFAULT_THREAD_LABELS.replies, disabled = false, }) {
if (replies.length === 0)
return null;
return (_jsx("div", { className: "ck-replies", role: "group", "aria-label": label, children: replies.map((r, i) => (_jsx("button", { type: "button", className: i === 0 ? "ck-reply ck-reply-lead" : "ck-reply", disabled: disabled, onClick: () => onPick(r), children: r }, r))) }));
}
/**
* The conversation. Follows new tokens only while the reader is at the
* bottom, with a button back down once they are not; a turn in flight can be
* stopped from where it is being written.
*/
export function ChatThread({ messages, live = null, loading = false, stopped = false, onStop, onRetry, onReply, renderLink = defaultRenderLink, renderFooter, showSpeakers, speaker, share, empty, children, labels: labelOverrides, }) {
const labels = { ...DEFAULT_THREAD_LABELS, ...labelOverrides };
const { scrollRef, endRef, following, onScroll, jumpToBottom } = useStickToBottom([messages.length, live?.text, live?.status, stopped], messages.length);
if (loading) {
return (_jsx("div", { className: "ck-thread-loading", role: "status", children: labels.loading }));
}
if (messages.length === 0 && !live && !stopped)
return _jsx(_Fragment, { children: empty ?? null });
const speakers = new Set([...messages.map((m) => m.speaker?.name), live?.speaker?.name].filter(Boolean));
const named = showSpeakers ?? speakers.size > 1;
const lastAnswer = [...messages].reverse().find((m) => m.role === "assistant");
return (_jsxs("div", { className: "ck-thread-wrap", children: [_jsx("div", { ref: scrollRef, className: "ck-thread", onScroll: onScroll, children: _jsxs("div", { className: "ck-thread-inner", children: [messages.map((m) => (_jsx(ChatMessage, { message: m, onRetry: onRetry && m.id === lastAnswer?.id && !live ? onRetry : undefined, renderLink: renderLink, showSpeaker: named, footer: renderFooter?.(m), last: m.id === lastAnswer?.id, onReply: live || stopped ? undefined : onReply, speaker: speaker, share: share, labels: labels }, m.id))), live && (_jsxs("div", { className: "ck-turn ck-turn-answer", children: [named && live.speaker && _jsx("span", { className: "ck-speaker", children: live.speaker.name }), live.text ? (_jsx(Markdown, { text: extractReplies(live.text).text, renderLink: renderLink })) : (_jsxs("p", { className: "ck-live", role: "status", "aria-live": "polite", children: [_jsxs("span", { className: "ck-dots", "aria-hidden": true, children: [_jsx("span", {}), _jsx("span", {}), _jsx("span", {})] }), live.status ?? labels.working] })), onStop && (_jsxs("button", { type: "button", className: "ck-stop", onClick: onStop, children: [_jsx(IconStop, {}), labels.stop] }))] })), stopped && !live && (_jsx("p", { className: "ck-stopped", role: "status", children: labels.stopped })), children, _jsx("div", { ref: endRef })] }) }), !following && (_jsx("button", { type: "button", className: "ck-jump", onClick: jumpToBottom, "aria-label": labels.jump, title: labels.jump, children: _jsx(IconArrowDown, {}) }))] }));
}
/** The empty state that starts a conversation. It disappears once one has. */
export function ChatStarters({ starters, onPick, title, }) {
return (_jsxs("div", { className: "ck-starters", children: [title && _jsx("div", { className: "ck-starters-title", children: title }), _jsx("div", { className: "ck-starters-list", children: starters.map((s, i) => {
const label = typeof s === "string" ? s : s.label;
const prompt = typeof s === "string" ? s : (s.prompt ?? s.label);
return (_jsx("button", { type: "button", className: "ck-starter", onClick: () => onPick(prompt), children: label }, i));
}) })] }));
}
//# sourceMappingURL=Thread.js.map