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

95 lines
3.2 KiB
Vue

<script setup lang="ts">
import { MessageSquare } from "@lucide/vue";
import { computed, onUnmounted } from "vue";
import { useRoute } from "vue-router";
import { Button } from "@/components/ui/button";
import { chatModelReady, messages, placeholderHint, sendMessage } from "./state";
import { suggestionsForRoute } from "./suggestions";
/**
* The suggested prompts for the page the operator is on. The empty
* conversation lists them (`list`); beside the composer they are a row of
* chips that scrolls sideways. None show until a chat model is configured. A click
* sends the prompt as its own message (sendMessage's textOverride path sends at
* once while no turn runs) rather than filling the field, and resting on one
* previews it as the composer's placeholder.
*/
defineProps<{ list?: boolean }>();
const route = useRoute();
// No model, no prompts: one sent now could only fail.
// A prompt already asked in this conversation is not suggested again.
const prompts = computed(() => {
if (!chatModelReady.value) return [];
const asked = new Set(messages.value.map((m) => m.text?.trim()));
return suggestionsForRoute(route).filter((p) => !asked.has(p));
});
function send(prompt: string): void {
placeholderHint.value = "";
void sendMessage({ textOverride: prompt });
}
function preview(prompt: string): void {
placeholderHint.value = prompt;
}
// A vertical wheel scrolls the row sideways; a trackpad's sideways swipe
// already does.
function scrollSideways(event: WheelEvent): void {
if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
const row = event.currentTarget as HTMLElement;
if (row.scrollWidth <= row.clientWidth) return;
event.preventDefault();
row.scrollLeft += event.deltaY;
}
function clear(): void {
placeholderHint.value = "";
}
onUnmounted(clear);
</script>
<template>
<ul v-if="prompts.length && list" class="flex flex-col gap-1 text-left" aria-label="Suggested prompts">
<li v-for="prompt in prompts" :key="prompt">
<Button
variant="ghost"
class="h-auto w-full justify-start gap-3 py-1.5 whitespace-normal text-left font-normal"
@click="send(prompt)"
@pointerenter="preview(prompt)"
@pointerleave="clear"
@focus="preview(prompt)"
@blur="clear"
>
<span class="flex size-7 flex-none items-center justify-center rounded-md border" aria-hidden="true">
<MessageSquare class="size-3.5" />
</span>
{{ prompt }}
</Button>
</li>
</ul>
<!-- One row that scrolls sideways, so the prompts never push the
transcript up by more than a line. -->
<div
v-else-if="prompts.length"
class="-mx-3 mb-2 flex snap-x snap-mandatory scroll-px-3 gap-1.5 overflow-x-auto px-3 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
role="group"
aria-label="Suggested prompts"
@wheel="scrollSideways"
>
<Button
v-for="prompt in prompts"
:key="prompt"
variant="outline"
size="sm"
class="flex-none snap-start whitespace-nowrap"
@click="send(prompt)"
@pointerenter="preview(prompt)"
@pointerleave="clear"
@focus="preview(prompt)"
@blur="clear"
>
{{ prompt }}
</Button>
</div>
</template>