Previously a message composed while the AI agent was streaming a reply was silently dropped (the composer early-returned on isStreaming). Now such messages are queued FIFO and sent automatically once the current turn finishes cleanly. - chat-input: submit() enqueues while streaming (via new onQueue prop) and sends otherwise; during streaming show a queue Send button (when text is present) alongside the Stop button; the textarea stays usable. - chat-thread: per-conversation queue in local state (mirrored in a ref); flush the next message in onFinish ONLY on a clean finish - ai@6 useChat fires onFinish from a finally on Stop/disconnect/error too, where the queue must be preserved. Pending messages render as removable chips above the composer. Queue is cleared on chat switch (parent remount) and survives in-place new-chat id adoption. - queue-helpers: pure FIFO helpers (enqueue/dequeue/removeQueuedById) + tests. - i18n: add en-US/ru-RU keys (Queue message, Remove queued message, Send when the agent finishes). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
121 lines
3.8 KiB
TypeScript
121 lines
3.8 KiB
TypeScript
import { KeyboardEvent } from "react";
|
|
import { ActionIcon, Group, Textarea, Tooltip } from "@mantine/core";
|
|
import { IconPlayerStopFilled, IconSend } from "@tabler/icons-react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useAtom, useAtomValue } from "jotai";
|
|
import { aiChatDraftAtom } from "@/features/ai-chat/atoms/ai-chat-atom.ts";
|
|
import { workspaceAtom } from "@/features/user/atoms/current-user-atom";
|
|
import { MicButton } from "@/features/dictation/components/mic-button";
|
|
|
|
interface ChatInputProps {
|
|
onSend: (text: string) => void;
|
|
/** Called instead of `onSend` while a turn is streaming: the text is queued
|
|
* and sent automatically once the current turn finishes. */
|
|
onQueue: (text: string) => void;
|
|
onStop: () => void;
|
|
isStreaming: boolean;
|
|
disabled?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Message composer. Enter submits, Shift+Enter inserts a newline. While the
|
|
* agent is streaming, submitting QUEUES the message (via `onQueue`) instead of
|
|
* dropping it — it is sent automatically once the current turn finishes; the
|
|
* Stop button (calls `stop()`) is also shown. The textarea stays usable so the
|
|
* user can draft / queue the next turn while the agent is busy.
|
|
*/
|
|
export default function ChatInput({
|
|
onSend,
|
|
onQueue,
|
|
onStop,
|
|
isStreaming,
|
|
disabled,
|
|
}: ChatInputProps) {
|
|
const { t } = useTranslation();
|
|
const [value, setValue] = useAtom(aiChatDraftAtom);
|
|
const workspace = useAtomValue(workspaceAtom);
|
|
const isDictationEnabled = workspace?.settings?.ai?.dictation === true;
|
|
|
|
const submit = (): void => {
|
|
const text = value.trim();
|
|
if (!text || disabled) return;
|
|
if (isStreaming) onQueue(text);
|
|
else onSend(text);
|
|
setValue("");
|
|
};
|
|
|
|
const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>): void => {
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
submit();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Group gap="xs" align="flex-end" wrap="nowrap">
|
|
<Textarea
|
|
style={{ flex: 1 }}
|
|
placeholder={t("Ask the AI agent…")}
|
|
value={value}
|
|
onChange={(e) => setValue(e.currentTarget.value)}
|
|
onKeyDown={handleKeyDown}
|
|
autosize
|
|
minRows={1}
|
|
maxRows={6}
|
|
disabled={disabled}
|
|
// Focus the composer whenever this input mounts. ChatThread is remounted
|
|
// via `key` on every chat appearance (window open, "New chat", chat
|
|
// switch), so a fresh chat lands with the cursor ready in the field.
|
|
autoFocus
|
|
/>
|
|
{isDictationEnabled && (
|
|
<MicButton
|
|
size="lg"
|
|
streaming
|
|
disabled={isStreaming || disabled}
|
|
onText={(text) => setValue((v) => (v ? `${v} ${text}` : text))}
|
|
/>
|
|
)}
|
|
{isStreaming ? (
|
|
<Group gap="xs" wrap="nowrap">
|
|
{value.trim().length > 0 && (
|
|
<Tooltip label={t("Send when the agent finishes")} withArrow>
|
|
<ActionIcon
|
|
size="lg"
|
|
variant="filled"
|
|
onClick={submit}
|
|
aria-label={t("Queue message")}
|
|
>
|
|
<IconSend size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
)}
|
|
<Tooltip label={t("Stop")} withArrow>
|
|
<ActionIcon
|
|
size="lg"
|
|
color="red"
|
|
variant="light"
|
|
onClick={onStop}
|
|
aria-label={t("Stop")}
|
|
>
|
|
<IconPlayerStopFilled size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
</Group>
|
|
) : (
|
|
<Tooltip label={t("Send")} withArrow>
|
|
<ActionIcon
|
|
size="lg"
|
|
variant="filled"
|
|
onClick={submit}
|
|
disabled={disabled || value.trim().length === 0}
|
|
aria-label={t("Send")}
|
|
>
|
|
<IconSend size={18} />
|
|
</ActionIcon>
|
|
</Tooltip>
|
|
)}
|
|
</Group>
|
|
);
|
|
}
|