Files
archie/ui/src/chat/ChatSessionActions.vue
T

189 lines
5.7 KiB
Vue

<script setup lang="ts">
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import {
Copy,
Ellipsis,
GitBranch,
Pencil,
RotateCw,
Shrink,
Undo2,
} from "@lucide/vue";
import { ref } from "vue";
import { toast } from "vue-sonner";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import {
Popover,
PopoverAnchor,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { errors } from "@/copy/errors";
import { describeError } from "@/lib/api";
import ChatSettings from "./ChatSettings.vue";
import {
currentSession,
messages,
runSessionCommand,
useEscapeLayer,
} from "./state";
/**
* The menu behind the bar's one "more" button: the commands that act on the
* conversation itself rather than on what is being said (retry the last turn,
* undo it, rename the conversation, branch it, or look at what compressing it
* would drop), then the model it runs on.
*
* Each one is the same command the server accepts from any other channel, so
* this is a set of buttons over the slash surface rather than a second way to
* do the work. The popover closes as the command goes, because its answer comes
* back into the transcript and the status line, not into the menu.
*/
const open = ref(false);
useEscapeLayer(open);
function run(command: string): void {
open.value = false;
void runSessionCommand(command);
}
// Reply text is already Markdown, so the transcript is the messages under a
// heading per speaker. Empty turns (tool-only replies) have nothing to paste.
async function copyMarkdown(): Promise<void> {
open.value = false;
const text = messages.value
.map((m) => {
const body = (m.text ?? m.Text ?? "").trim();
if (!body) return "";
return `### ${(m.from ?? m.From) === "web" ? "You" : "Archie"}\n\n${body}`;
})
.filter(Boolean)
.join("\n\n");
if (!text) {
toast.error(errors.chat.session.nothingToCopy);
return;
}
try {
await navigator.clipboard.writeText(text);
} catch (err) {
toast.error(errors.chat.session.copyFailed(describeError(err, errors.chat.session.clipboardUnavailable)));
return;
}
toast.success("Copied as Markdown");
}
// Rename and branch ask for a value in a labelled dialog, not window.prompt.
const promptOpen = ref(false);
// The dialog owns Escape while open, so closing it leaves the dock open.
useEscapeLayer(promptOpen);
const promptCommand = ref("");
const promptMessage = ref("");
const promptValue = ref("");
function prompt(command: string, message: string): void {
open.value = false;
promptCommand.value = command;
promptMessage.value = message;
promptValue.value = "";
promptOpen.value = true;
}
function submitPrompt(): void {
const value = promptValue.value.trim();
promptOpen.value = false;
if (value) void runSessionCommand(`${promptCommand.value} ${value}`);
}
</script>
<template>
<Popover v-model:open="open">
<!-- The anchor sits outside the tooltip chain on purpose: nested inside
TooltipTrigger, the popper anchor ref resolves to a component object
rather than the button, the content is never positioned, and the menu
stays parked off-screen. -->
<PopoverAnchor as-child>
<Tooltip>
<TooltipTrigger as-child>
<PopoverTrigger as-child>
<Button
variant="ghost"
size="icon-sm"
class="flex-none"
aria-label="More"
>
<Ellipsis />
</Button>
</PopoverTrigger>
</TooltipTrigger>
<TooltipContent>More</TooltipContent>
</Tooltip>
</PopoverAnchor>
<PopoverContent align="end" class="grid w-64 gap-0.5 p-1">
<template v-if="currentSession">
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="run('/retry')"
><RotateCw data-icon="inline-start" />Retry</Button
>
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="run('/undo')"
><Undo2 data-icon="inline-start" />Undo</Button
>
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="prompt('/title', 'Session title')"
><Pencil data-icon="inline-start" />Rename</Button
>
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="prompt('/branch', 'Branch name (optional)')"
><GitBranch data-icon="inline-start" />Branch</Button
>
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="run('/compress --preview')"
><Shrink data-icon="inline-start" />Preview compression</Button
>
<Button
variant="ghost"
size="sm"
class="justify-start"
@click="copyMarkdown"
><Copy data-icon="inline-start" />Copy as Markdown</Button
>
<Separator class="my-1" />
</template>
<ChatSettings />
</PopoverContent>
</Popover>
<Dialog :open="promptOpen" @update:open="(v: boolean) => (promptOpen = v)">
<DialogContent>
<DialogHeader><DialogTitle>{{ promptMessage }}</DialogTitle></DialogHeader>
<Input v-model="promptValue" :aria-label="promptMessage" @keydown.enter.prevent="submitPrompt" />
<DialogFooter>
<Button variant="ghost" @click="promptOpen = false">Cancel</Button>
<Button :disabled="!promptValue.trim()" @click="submitPrompt">OK</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</template>