2026-08-22 04:28:13 +03:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
2026-09-11 21:26:29 +03:00
|
|
|
|
import { CheckIcon, CornerDownLeftIcon, SquareIcon, XIcon } from "lucide-react";
|
2026-08-22 04:28:13 +03:00
|
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-22 05:02:15 +03:00
|
|
|
|
* The chat composer — content row ONLY (the persistent rounded shell lives in
|
|
|
|
|
|
* the chat view's overlay, so the shell can later morph chat → question
|
|
|
|
|
|
* without remounting).
|
2026-08-22 04:28:13 +03:00
|
|
|
|
*
|
2026-08-22 05:02:15 +03:00
|
|
|
|
* Corner-radius math (concentric with the shell): shell rounded-2xl (16px) −
|
|
|
|
|
|
* row padding p-1.5 (6px) = button rounded-[10px] = 25% of the 40px button.
|
2026-08-22 04:28:13 +03:00
|
|
|
|
*/
|
2026-09-19 13:16:34 +03:00
|
|
|
|
/** What one Enter press does, in precedence order (the plan-verdict
|
|
|
|
|
|
* behavior is user-decided 2026-09-19, docs/probe-teach-split.md §4):
|
|
|
|
|
|
* stop a busy stream; ACCEPT when the plan verdict pends — typed text
|
|
|
|
|
|
* rides the acceptance as the note (the green button's exact semantics;
|
|
|
|
|
|
* an empty box accepts too, so Enter can never strand the verdict);
|
|
|
|
|
|
* otherwise send the message. Pure so the static-render harness can pin
|
|
|
|
|
|
* the decision (key events can't be simulated in renderToStaticMarkup). */
|
|
|
|
|
|
export function enterAction(state: { busy: boolean; verdictPending: boolean; canSend: boolean }): "stop" | "accept" | "send" | "none" {
|
|
|
|
|
|
if (state.busy) return "stop";
|
|
|
|
|
|
if (state.verdictPending) return "accept";
|
|
|
|
|
|
if (state.canSend) return "send";
|
|
|
|
|
|
return "none";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-22 04:28:13 +03:00
|
|
|
|
export function Composer({
|
|
|
|
|
|
busy,
|
|
|
|
|
|
value,
|
|
|
|
|
|
onChange,
|
|
|
|
|
|
onSend,
|
|
|
|
|
|
onStop,
|
|
|
|
|
|
canSend,
|
|
|
|
|
|
autoFocus,
|
2026-09-11 21:26:29 +03:00
|
|
|
|
verdict,
|
2026-09-17 19:31:25 +03:00
|
|
|
|
onEscape,
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}: {
|
|
|
|
|
|
busy: boolean;
|
|
|
|
|
|
value: string;
|
|
|
|
|
|
onChange: (v: string) => void;
|
|
|
|
|
|
onSend: () => void;
|
|
|
|
|
|
onStop: () => void;
|
|
|
|
|
|
canSend: boolean;
|
|
|
|
|
|
autoFocus?: boolean;
|
2026-09-11 21:26:29 +03:00
|
|
|
|
/** The plan verdict gate: while the presented plan awaits the learner's
|
|
|
|
|
|
* word, the send square becomes two stacked buttons — green accept,
|
|
|
|
|
|
* red decline. Typed text rides the verdict as his note. */
|
|
|
|
|
|
verdict?: { onAccept: () => void; onDecline: () => void };
|
2026-09-17 19:31:25 +03:00
|
|
|
|
/** Escape while a dictation runs stops it (STT wave, 2026-09-17). No-op
|
|
|
|
|
|
* when absent — Esc otherwise means nothing in the composer. */
|
|
|
|
|
|
onEscape?: () => void;
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<form
|
|
|
|
|
|
data-testid="composer"
|
2026-08-22 05:02:15 +03:00
|
|
|
|
className="flex items-end gap-1.5 p-1.5"
|
2026-08-22 04:28:13 +03:00
|
|
|
|
onSubmit={(e) => {
|
|
|
|
|
|
e.preventDefault();
|
2026-09-19 13:16:34 +03:00
|
|
|
|
const action = enterAction({ busy, verdictPending: Boolean(verdict), canSend });
|
|
|
|
|
|
if (action === "stop") onStop();
|
|
|
|
|
|
else if (action === "accept") verdict?.onAccept();
|
|
|
|
|
|
else if (action === "send") onSend();
|
2026-08-22 04:28:13 +03:00
|
|
|
|
}}
|
|
|
|
|
|
>
|
|
|
|
|
|
<textarea
|
|
|
|
|
|
value={value}
|
|
|
|
|
|
onChange={(e) => onChange(e.target.value)}
|
2026-08-22 05:02:15 +03:00
|
|
|
|
onKeyDown={(e) => {
|
2026-09-17 19:31:25 +03:00
|
|
|
|
// Esc ends dictation before anything else Esc might someday mean
|
|
|
|
|
|
// here (the dictate decisions: click-again or Esc to stop).
|
2026-09-17 20:47:23 +03:00
|
|
|
|
// stopPropagation claims the Esc (keyboard wave A): the
|
|
|
|
|
|
// window-level chain must not also abort a turn under us.
|
2026-09-17 19:31:25 +03:00
|
|
|
|
if (e.key === "Escape" && onEscape) {
|
|
|
|
|
|
e.preventDefault();
|
2026-09-17 20:47:23 +03:00
|
|
|
|
e.stopPropagation();
|
2026-09-17 19:31:25 +03:00
|
|
|
|
onEscape();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-09-09 02:25:27 +03:00
|
|
|
|
// Chat convention: Enter sends, Shift+Enter is a newline.
|
|
|
|
|
|
// ⌘/Ctrl+Enter also sends — the universal submit shortcut
|
|
|
|
|
|
// across every input in the app (thinking field, answers).
|
2026-09-19 13:16:34 +03:00
|
|
|
|
// While the plan verdict pends, Enter ACCEPTS the plan instead
|
|
|
|
|
|
// (enterAction — user-decided 2026-09-19).
|
2026-09-09 02:25:27 +03:00
|
|
|
|
if (e.key === "Enter" && (e.metaKey || e.ctrlKey || !e.shiftKey)) {
|
2026-08-22 05:02:15 +03:00
|
|
|
|
e.preventDefault();
|
2026-09-19 13:16:34 +03:00
|
|
|
|
const action = enterAction({ busy, verdictPending: Boolean(verdict), canSend });
|
|
|
|
|
|
if (action === "stop") onStop();
|
|
|
|
|
|
else if (action === "accept") verdict?.onAccept();
|
|
|
|
|
|
else if (action === "send") onSend();
|
|
|
|
|
|
}
|
2026-08-22 05:02:15 +03:00
|
|
|
|
}}
|
|
|
|
|
|
placeholder="Ask, answer, or steer the lesson…"
|
2026-08-22 04:28:13 +03:00
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
|
|
rows={1}
|
2026-09-17 13:15:35 +03:00
|
|
|
|
className="field-sizing-content lh-input max-h-48 min-h-10 flex-1 resize-none bg-transparent px-2.5 py-2 outline-none placeholder:text-muted-foreground"
|
2026-08-22 04:28:13 +03:00
|
|
|
|
/>
|
2026-09-11 21:26:29 +03:00
|
|
|
|
{verdict && !busy ? (
|
|
|
|
|
|
// The plan verdict (docs/probe-teach-split.md §4): accept
|
|
|
|
|
|
// sanctions teaching (phase plan→teach); decline sends the
|
2026-09-19 13:16:34 +03:00
|
|
|
|
// revision request. Enter accepts too (user-decided 2026-09-19 —
|
|
|
|
|
|
// it formerly sent a plain message, which left the verdict
|
|
|
|
|
|
// pending on the next message; a side question now rides the
|
|
|
|
|
|
// acceptance as its note instead).
|
2026-09-11 21:26:29 +03:00
|
|
|
|
<div className="flex shrink-0 flex-col justify-end gap-1" data-testid="plan-verdict-buttons">
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={verdict.onAccept}
|
2026-09-12 23:48:27 +03:00
|
|
|
|
className="flex h-[19px] items-center justify-center gap-1 rounded-md bg-emerald-600 px-3 text-xs font-medium text-white transition-colors hover:bg-emerald-500"
|
2026-09-11 21:26:29 +03:00
|
|
|
|
>
|
|
|
|
|
|
<CheckIcon className="size-3" /> Accept plan
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={verdict.onDecline}
|
2026-09-12 23:48:27 +03:00
|
|
|
|
className="flex h-[19px] items-center justify-center gap-1 rounded-md bg-destructive px-3 text-xs font-medium text-destructive-foreground transition-colors hover:opacity-90"
|
2026-09-11 21:26:29 +03:00
|
|
|
|
>
|
|
|
|
|
|
<XIcon className="size-3" /> Decline
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="submit"
|
|
|
|
|
|
aria-label={busy ? "Stop" : "Send"}
|
|
|
|
|
|
disabled={!busy && !canSend}
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"flex size-10 shrink-0 items-center justify-center rounded-[10px] transition-colors",
|
|
|
|
|
|
busy
|
|
|
|
|
|
? "bg-secondary text-foreground hover:bg-accent"
|
|
|
|
|
|
: "bg-primary text-primary-foreground hover:opacity-90 disabled:opacity-40",
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{busy ? <SquareIcon className="size-4" /> : <CornerDownLeftIcon className="size-5" />}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
2026-08-22 04:28:13 +03:00
|
|
|
|
</form>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|