321 lines
11 KiB
Vue
321 lines
11 KiB
Vue
<script setup lang="ts">
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { computed, nextTick, ref, watch } from "vue";
|
|
|
|
import { Clock, Mic, Pencil, X } from "@lucide/vue";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
import { cn } from "@/lib/utils";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { errors } from "@/copy/errors";
|
|
import { describeError } from "@/lib/api";
|
|
import { isExactCommand, replaceToken } from "./commands";
|
|
import ChatSlashPalette from "./ChatSlashPalette.vue";
|
|
import ChatSuggestions from "./ChatSuggestions.vue";
|
|
import {
|
|
closeCommandMenu,
|
|
commandMatches,
|
|
commandSelection,
|
|
commandSpecs,
|
|
addTaskTag,
|
|
clearPageTag,
|
|
pageTag,
|
|
placeholderHint,
|
|
messages,
|
|
composerText,
|
|
editQueued,
|
|
menuTrigger,
|
|
removeTaskTag,
|
|
retryTasks,
|
|
taskForKey,
|
|
taskLoadError,
|
|
taskTags,
|
|
isCommandMenuOpen,
|
|
isSending,
|
|
moveCommandSelection,
|
|
queuedMessages,
|
|
removeQueued,
|
|
sendMessage,
|
|
selectorData,
|
|
sendVoice,
|
|
stopTurn,
|
|
syncCommandMenu,
|
|
} from "./state";
|
|
|
|
/**
|
|
* The composer: the text, the palette that completes a slash command, and the
|
|
* two actions. It stays writeable and focused while a turn is in flight --
|
|
* stopping is not the only thing an operator may want to do next -- so Stop is
|
|
* an addition to the row, never a replacement for Send.
|
|
*
|
|
* The textarea is stripped back to bare text inside the shell that carries the
|
|
* border and the fill: the field's own chrome would draw a second box inside
|
|
* the first. `bg-transparent!` beats the field's dark fill, which the dark
|
|
* variant would otherwise re-apply over the shell. It grows with what is typed
|
|
* and stops at `max-h-40`, so a long draft scrolls rather than eating the
|
|
* transcript above it.
|
|
*/
|
|
// Flat drops the field's own border and fill when the card around it already
|
|
// draws them, so the collapsed dock reads as one surface rather than a box in a box.
|
|
const props = defineProps<{ open: boolean; flat?: boolean }>();
|
|
|
|
// Voice: recorded in the browser, transcribed by the server like a Telegram
|
|
// voice note. The mic shows only while the composer is empty.
|
|
const canRecord = computed(
|
|
() => !!selectorData.value.voice_available && typeof MediaRecorder !== "undefined" && !!navigator.mediaDevices?.getUserMedia,
|
|
);
|
|
const recording = ref(false);
|
|
const recordError = ref("");
|
|
let recorder: MediaRecorder | null = null;
|
|
async function toggleRecording() {
|
|
if (recorder) {
|
|
recorder.stop();
|
|
return;
|
|
}
|
|
recordError.value = "";
|
|
let stream: MediaStream;
|
|
try {
|
|
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
} catch (err) {
|
|
recordError.value = errors.chat.composer.microphoneUnavailable(describeError(err, errors.common.unknown));
|
|
return;
|
|
}
|
|
const chunks: Blob[] = [];
|
|
recorder = new MediaRecorder(stream);
|
|
recorder.ondataavailable = (e) => chunks.push(e.data);
|
|
recorder.onstop = () => {
|
|
stream.getTracks().forEach((t) => t.stop());
|
|
const clip = new Blob(chunks, { type: recorder?.mimeType || "audio/webm" });
|
|
recorder = null;
|
|
recording.value = false;
|
|
if (clip.size) void sendVoice(clip);
|
|
};
|
|
recorder.start();
|
|
recording.value = true;
|
|
}
|
|
|
|
const field = ref<{ $el: HTMLTextAreaElement } | null>(null);
|
|
|
|
function focus(): void {
|
|
field.value?.$el?.focus();
|
|
}
|
|
|
|
defineExpose({ focus });
|
|
|
|
// The panel stays mounted so the session survives navigation, so opening it is
|
|
// a prop change rather than a mount: focus the composer on that edge.
|
|
watch(
|
|
() => props.open,
|
|
(open) => {
|
|
if (open) void nextTick(focus);
|
|
},
|
|
);
|
|
|
|
// A finished turn leaves the caret in the composer, where the next message is
|
|
// typed, rather than on the button that sent the last one.
|
|
watch(isSending, (sending, wasSending) => {
|
|
if (wasSending && !sending) void nextTick(focus);
|
|
});
|
|
|
|
function onInput(event: Event): void {
|
|
const input = event.target as HTMLTextAreaElement;
|
|
composerText.value = input.value;
|
|
syncCommandMenu(input.value, input.selectionStart ?? input.value.length);
|
|
}
|
|
|
|
function choose(index: number): void {
|
|
const item = commandMatches.value[index];
|
|
const input = field.value?.$el;
|
|
if (!item || !input) return;
|
|
const caret = input.selectionStart ?? input.value.length;
|
|
// Completing replaces the token the caret sits in, which is decided by the
|
|
// same rule that found the matches. A task leaves no text behind: it becomes
|
|
// a tag above the field.
|
|
let edit: { text: string; cursor: number };
|
|
if (menuTrigger.value === "#") {
|
|
const task = taskForKey(item.key);
|
|
if (!task) return;
|
|
addTaskTag(task);
|
|
edit = replaceToken(input.value, caret, "");
|
|
} else {
|
|
edit = replaceToken(input.value, caret, item.label);
|
|
}
|
|
composerText.value = edit.text;
|
|
closeCommandMenu();
|
|
void nextTick(() => {
|
|
input.focus();
|
|
input.setSelectionRange(edit.cursor, edit.cursor);
|
|
});
|
|
}
|
|
|
|
function onKeydown(event: KeyboardEvent): void {
|
|
// Safari may end composition before delivering its confirming Enter.
|
|
if (event.key === "Enter" && (event.isComposing || event.keyCode === 229)) return;
|
|
const plain = !event.shiftKey && !event.ctrlKey && !event.metaKey;
|
|
if (isCommandMenuOpen.value) {
|
|
// A command typed in full is sent rather than completed, so Enter never
|
|
// re-enters the command the palette was offering.
|
|
if (
|
|
event.key === "Enter" &&
|
|
plain &&
|
|
menuTrigger.value === "/" &&
|
|
isExactCommand(commandSpecs.value, composerText.value)
|
|
) {
|
|
event.preventDefault();
|
|
closeCommandMenu();
|
|
void sendMessage();
|
|
return;
|
|
}
|
|
if (event.key === "ArrowDown") {
|
|
event.preventDefault();
|
|
moveCommandSelection(1);
|
|
return;
|
|
}
|
|
if (event.key === "ArrowUp") {
|
|
event.preventDefault();
|
|
moveCommandSelection(-1);
|
|
return;
|
|
}
|
|
if (
|
|
commandMatches.value.length &&
|
|
((event.key === "Enter" && plain) || (event.key === "Tab" && !event.shiftKey))
|
|
) {
|
|
event.preventDefault();
|
|
choose(commandSelection.value);
|
|
return;
|
|
}
|
|
if (event.key === "Escape") {
|
|
// Consumed here: dismissing the palette must not also close the panel.
|
|
event.preventDefault();
|
|
closeCommandMenu();
|
|
return;
|
|
}
|
|
}
|
|
if (event.key === "Enter" && !event.shiftKey) {
|
|
event.preventDefault();
|
|
void sendMessage();
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div :class="flat ? 'p-1.5' : 'border-t p-3'">
|
|
<ul v-if="queuedMessages.length" aria-label="Queued messages" class="mb-2 flex flex-col gap-1">
|
|
<li
|
|
v-for="(queued, index) in queuedMessages"
|
|
:key="index"
|
|
class="flex items-center gap-1 rounded-md border border-dashed border-border px-2 py-1 text-label text-fg-muted"
|
|
>
|
|
<Clock class="size-3.5 flex-none" aria-hidden="true" />
|
|
<span class="min-w-0 flex-1 truncate">{{ queued }}</span>
|
|
<Tooltip>
|
|
<TooltipTrigger as-child>
|
|
<Button variant="ghost" size="icon" class="size-7" aria-label="Edit queued message" @click="editQueued(index)"
|
|
><Pencil
|
|
/></Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Edit</TooltipContent>
|
|
</Tooltip>
|
|
<Tooltip>
|
|
<TooltipTrigger as-child>
|
|
<Button variant="ghost" size="icon" class="size-7" aria-label="Remove queued message" @click="removeQueued(index)"
|
|
><X
|
|
/></Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Remove</TooltipContent>
|
|
</Tooltip>
|
|
</li>
|
|
</ul>
|
|
<ul v-if="!flat && (pageTag || taskTags.length)" aria-label="Message context" class="mb-2 flex flex-wrap gap-1">
|
|
<li
|
|
v-if="pageTag"
|
|
class="flex max-w-full items-center gap-1 rounded-md border border-border bg-muted px-2 py-0.5 text-label"
|
|
>
|
|
<span class="truncate">{{ pageTag.label }}</span>
|
|
<Button variant="ghost" size="icon" class="size-5" aria-label="Remove page context" @click="clearPageTag"
|
|
><X
|
|
/></Button>
|
|
</li>
|
|
<li
|
|
v-for="tag in taskTags"
|
|
:key="tag.id"
|
|
class="flex max-w-full items-center gap-1 rounded-md border border-border bg-muted px-2 py-0.5 text-label"
|
|
>
|
|
<span class="truncate">#{{ tag.id }}{{ tag.title ? ` ${tag.title}` : "" }}</span>
|
|
<Button variant="ghost" size="icon" class="size-5" :aria-label="`Remove task ${tag.id}`" @click="removeTaskTag(tag.id)"
|
|
><X
|
|
/></Button>
|
|
</li>
|
|
</ul>
|
|
<!-- An empty conversation lists the prompts itself; once it has messages
|
|
they sit beside the field so they stay reachable without scrolling up.
|
|
The minimised bar is the field alone; the page context still goes with
|
|
the message. -->
|
|
<ChatSuggestions v-if="!flat && messages.length" />
|
|
<div
|
|
:class="
|
|
cn(
|
|
'relative flex gap-2 rounded-xl border transition-colors duration-200',
|
|
// Minimised, the field is one line: the controls centre on it.
|
|
flat ? 'items-center border-transparent px-1.5 py-1' : 'items-end border-border-strong bg-muted px-3 py-2.5',
|
|
)
|
|
"
|
|
>
|
|
<div v-if="$slots.start" :class="cn('flex flex-none items-center', !flat && 'mb-1.5')">
|
|
<slot name="start" />
|
|
</div>
|
|
<Textarea
|
|
ref="field"
|
|
:model-value="composerText"
|
|
rows="1"
|
|
:placeholder="placeholderHint || 'Message Archie'"
|
|
aria-label="Message Archie"
|
|
role="combobox"
|
|
aria-controls="slash-palette"
|
|
:aria-expanded="isCommandMenuOpen ? 'true' : 'false'"
|
|
:aria-activedescendant="isCommandMenuOpen && commandMatches.length ? `slash-palette-option-${commandSelection}` : undefined"
|
|
:class="cn('max-h-40 flex-1 resize-none rounded-none border-0 bg-transparent! px-0 text-label pointer-coarse:text-base shadow-none focus-visible:ring-0', flat ? 'min-h-8 py-1.5 leading-5' : 'min-h-11 py-1')"
|
|
@input="onInput"
|
|
@keydown="onKeydown"
|
|
/>
|
|
|
|
<ChatSlashPalette
|
|
v-if="isCommandMenuOpen"
|
|
:matches="commandMatches"
|
|
:selection="commandSelection"
|
|
:label="menuTrigger === '#' ? 'Tasks' : 'Commands'"
|
|
:error="menuTrigger === '#' ? taskLoadError : ''"
|
|
@choose="choose"
|
|
@retry="retryTasks"
|
|
/>
|
|
|
|
<!-- Centred on the field's first line, then kept at the bottom as it grows. -->
|
|
<div :class="cn('flex flex-none items-center gap-2', !flat && 'mb-1.5')">
|
|
<slot name="actions" />
|
|
<!-- Only while a turn is arriving: a Stop with nothing to stop is a
|
|
button that lies about what the panel is doing. -->
|
|
<Button v-if="isSending" variant="outline" size="sm" @click="stopTurn"
|
|
>Stop</Button
|
|
>
|
|
<Button
|
|
v-if="canRecord && !composerText.trim()"
|
|
size="sm"
|
|
:variant="recording ? 'destructive' : 'outline'"
|
|
:disabled="isSending && !recording"
|
|
:aria-label="recording ? 'Stop recording and send' : 'Record a voice message'"
|
|
@click="toggleRecording"
|
|
><Mic data-icon="inline-start" />{{ recording ? "Send" : "" }}</Button
|
|
>
|
|
<p v-if="recordError" class="px-1 text-caption text-danger" role="alert">{{ recordError }}</p>
|
|
<Button
|
|
size="sm"
|
|
:variant="composerText.trim() ? 'default' : 'secondary'"
|
|
:disabled="!composerText.trim()"
|
|
@click="sendMessage()"
|
|
>{{ isSending ? "Queue" : "Send" }}</Button
|
|
>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|