319 lines
13 KiB
Vue
319 lines
13 KiB
Vue
<script setup lang="ts">
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { ChevronDown, ChevronUp } from "@lucide/vue";
|
|
import { useEventListener, useMediaQuery, useStorage, useWindowSize } from "@vueuse/core";
|
|
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
|
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useLiveResource } from "@/stores/session";
|
|
import { cn } from "@/lib/utils";
|
|
import ChatBar from "./ChatBar.vue";
|
|
import ChatComposer from "./ChatComposer.vue";
|
|
import ChatTranscript from "./ChatTranscript.vue";
|
|
import { composerText, escapeOwnedByLayer, isSending, reloadTranscript, useChat } from "./state";
|
|
import { setDockOpen, unseenCount } from "./unseen";
|
|
|
|
/**
|
|
* Chat is a bar docked at the bottom of every page, not a route: the operator
|
|
* asks Archie about the page they are already on, so navigating must not reset
|
|
* it, and it must not replace the work that prompted the question. The agent
|
|
* is told which page that is.
|
|
*
|
|
* It is the composer alone until the operator focuses it or a turn starts, then
|
|
* the conversation opens above it. Hiding the conversation leaves the bar. It is
|
|
* deliberately not a modal: no scrim, no scroll lock, no focus trap.
|
|
*
|
|
* On a desktop-width screen the expanded conversation can be pinned to the left
|
|
* or right edge, where the page reflows beside it instead of sitting under it.
|
|
* Dragging the header to an edge pins it; dragging it away, or double-clicking
|
|
* the header, unpins it. The side and width are remembered per browser.
|
|
*
|
|
* On a phone the bar rides above the on-screen keyboard, which iOS does not
|
|
* resize the page for, and swiping down on the conversation header hides it.
|
|
*
|
|
* It stays mounted, so the session and any turn in flight survive a navigation.
|
|
* It is mounted outside the app shell: `.shell`'s backdrop-filter makes it a
|
|
* containing block for `position: fixed`, so a bar nested inside would anchor to
|
|
* the shell and ride off the bottom of a long page.
|
|
*/
|
|
useChat();
|
|
|
|
// A task this conversation started reports its outcome into it. The dock stays
|
|
// mounted while collapsed, so a reply that lands then is still read and counted.
|
|
useLiveResource("chat", () => void reloadTranscript());
|
|
|
|
const expanded = ref(false);
|
|
watch(expanded, setDockOpen, { immediate: true });
|
|
const MIN_WIDTH = 320;
|
|
const EDGE = 64;
|
|
type Side = "left" | "right";
|
|
const saved = useStorage<{ side: Side | null; width: number }>(
|
|
"archie.chat.dock",
|
|
{ side: null, width: 420 },
|
|
localStorage,
|
|
{ mergeDefaults: true },
|
|
);
|
|
const desktop = useMediaQuery("(min-width: 768px)");
|
|
const { width: viewport } = useWindowSize();
|
|
const width = computed(() =>
|
|
Math.round(Math.min(Math.max(saved.value.width, MIN_WIDTH), Math.max(MIN_WIDTH, Math.min(720, viewport.value - 480)))),
|
|
);
|
|
const side = computed(() => (desktop.value && expanded.value ? saved.value.side : null));
|
|
// The part of the layout viewport the on-screen keyboard covers, and the height
|
|
// left above it. Fixed elements stay anchored to the layout viewport, so without
|
|
// this the bar sits under the keyboard.
|
|
const keyboard = ref(0);
|
|
const visibleHeight = ref(window.innerHeight);
|
|
function followViewport(): void {
|
|
const vv = window.visualViewport;
|
|
keyboard.value = vv ? Math.max(0, Math.round(window.innerHeight - vv.height - vv.offsetTop)) : 0;
|
|
visibleHeight.value = vv ? vv.height : window.innerHeight;
|
|
}
|
|
useEventListener(() => window.visualViewport, ["resize", "scroll"], followViewport);
|
|
const phoneBottom = computed(() =>
|
|
keyboard.value > 0 ? `${keyboard.value + 8}px` : "max(0.5rem, env(safe-area-inset-bottom))",
|
|
);
|
|
const composer = ref<InstanceType<typeof ChatComposer> | null>(null);
|
|
|
|
// Nothing outside the dock closes it: every navigation control is outside it,
|
|
// so a click-outside dismissal turned navigation into a collapse and read as a
|
|
// lost conversation. Hide and Escape are the only ways to collapse.
|
|
|
|
watch(isSending, (sending) => {
|
|
if (sending) expanded.value = true;
|
|
});
|
|
|
|
function onKeydown(event: KeyboardEvent): void {
|
|
// A focused control (the composer dismissing its palette, or the topbar
|
|
// search) already handled Escape via preventDefault; collapsing too would
|
|
// make that menu impossible to dismiss on its own. A popover portalled out
|
|
// of the bar owns Escape by being open, which the flag reports.
|
|
if (event.defaultPrevented || event.key !== "Escape" || escapeOwnedByLayer()) return;
|
|
expanded.value = false;
|
|
}
|
|
watch(expanded, (open) => {
|
|
if (open) document.addEventListener("keydown", onKeydown);
|
|
else document.removeEventListener("keydown", onKeydown);
|
|
});
|
|
onUnmounted(() => document.removeEventListener("keydown", onKeydown));
|
|
watch(expanded, () => nextTick(reserve));
|
|
|
|
// The page reserves the collapsed dock's height, read from the rendered bar so
|
|
// the last line of any page can scroll clear of it.
|
|
const dock = ref<HTMLElement | null>(null);
|
|
let observer: ResizeObserver | null = null;
|
|
function reserve(): void {
|
|
if (expanded.value || !dock.value) return;
|
|
const clear = window.innerHeight - dock.value.getBoundingClientRect().top;
|
|
document.documentElement.style.setProperty("--chat-dock-clear", `${Math.ceil(clear)}px`);
|
|
}
|
|
onMounted(() => {
|
|
observer = new ResizeObserver(reserve);
|
|
if (dock.value) observer.observe(dock.value);
|
|
window.addEventListener("resize", reserve);
|
|
});
|
|
onUnmounted(() => {
|
|
observer?.disconnect();
|
|
window.removeEventListener("resize", reserve);
|
|
document.documentElement.style.removeProperty("--chat-dock-clear");
|
|
});
|
|
|
|
// The page makes room for a pinned dock through these two variables.
|
|
watch([side, width], ([s, w]) => {
|
|
const root = document.documentElement.style;
|
|
root.setProperty("--chat-pin-l", s === "left" ? `${w}px` : "0px");
|
|
root.setProperty("--chat-pin-r", s === "right" ? `${w}px` : "0px");
|
|
}, { immediate: true });
|
|
onUnmounted(() => {
|
|
document.documentElement.style.removeProperty("--chat-pin-l");
|
|
document.documentElement.style.removeProperty("--chat-pin-r");
|
|
});
|
|
|
|
function edgeAt(x: number): Side | null {
|
|
if (x <= EDGE) return "left";
|
|
if (x >= window.innerWidth - EDGE) return "right";
|
|
return null;
|
|
}
|
|
|
|
// Pointer travel before a press on the header becomes a drag, so a click or a
|
|
// double-click on it never moves the dock.
|
|
const INTERACTIVE = "button, a, input, select, textarea, [role=combobox]";
|
|
const DRAG_START = 4;
|
|
const offset = ref<{ x: number; y: number } | null>(null);
|
|
const target = ref<Side | null>(null);
|
|
function startDrag(event: PointerEvent): void {
|
|
if (event.button !== 0 || (event.target as HTMLElement).closest(INTERACTIVE)) return;
|
|
if (!desktop.value) return startSwipe(event);
|
|
const grip = event.currentTarget as HTMLElement;
|
|
const { clientX: x0, clientY: y0 } = event;
|
|
grip.setPointerCapture(event.pointerId);
|
|
const move = (e: PointerEvent): void => {
|
|
const dx = e.clientX - x0;
|
|
const dy = e.clientY - y0;
|
|
if (!offset.value && Math.hypot(dx, dy) < DRAG_START) return;
|
|
offset.value = { x: dx, y: dy };
|
|
target.value = edgeAt(e.clientX);
|
|
};
|
|
const end = (e: PointerEvent): void => {
|
|
grip.removeEventListener("pointermove", move);
|
|
grip.removeEventListener("pointerup", end);
|
|
grip.removeEventListener("pointercancel", end);
|
|
if (offset.value && e.type === "pointerup") saved.value.side = edgeAt(e.clientX);
|
|
offset.value = null;
|
|
target.value = null;
|
|
};
|
|
grip.addEventListener("pointermove", move);
|
|
grip.addEventListener("pointerup", end);
|
|
grip.addEventListener("pointercancel", end);
|
|
}
|
|
// A downward flick on the header hides the conversation. It must be mostly
|
|
// vertical so a sideways slip does not collapse it.
|
|
const SWIPE = 48;
|
|
function startSwipe(event: PointerEvent): void {
|
|
const grip = event.currentTarget as HTMLElement;
|
|
const { clientX: x0, clientY: y0 } = event;
|
|
const end = (e: PointerEvent): void => {
|
|
grip.removeEventListener("pointerup", end);
|
|
grip.removeEventListener("pointercancel", end);
|
|
const dy = e.clientY - y0;
|
|
if (e.type === "pointerup" && dy >= SWIPE && dy > Math.abs(e.clientX - x0) * 1.5) {
|
|
// The keyboard is for the composer, which is no longer in front of it.
|
|
(document.activeElement as HTMLElement | null)?.blur();
|
|
expanded.value = false;
|
|
}
|
|
};
|
|
grip.addEventListener("pointerup", end);
|
|
grip.addEventListener("pointercancel", end);
|
|
}
|
|
function togglePin(event: MouseEvent): void {
|
|
if (!desktop.value || (event.target as HTMLElement).closest(INTERACTIVE)) return;
|
|
saved.value.side = saved.value.side ? null : "right";
|
|
}
|
|
|
|
function resize(event: PointerEvent): void {
|
|
const handle = event.currentTarget as HTMLElement;
|
|
handle.setPointerCapture(event.pointerId);
|
|
const move = (e: PointerEvent): void => {
|
|
saved.value.width = side.value === "right" ? window.innerWidth - e.clientX : e.clientX;
|
|
};
|
|
const end = (): void => {
|
|
handle.removeEventListener("pointermove", move);
|
|
handle.removeEventListener("pointerup", end);
|
|
handle.removeEventListener("pointercancel", end);
|
|
};
|
|
handle.addEventListener("pointermove", move);
|
|
handle.addEventListener("pointerup", end);
|
|
handle.addEventListener("pointercancel", end);
|
|
}
|
|
function resizeByKey(event: KeyboardEvent): void {
|
|
const grow = event.key === (side.value === "right" ? "ArrowLeft" : "ArrowRight");
|
|
const shrink = event.key === (side.value === "right" ? "ArrowRight" : "ArrowLeft");
|
|
if (!grow && !shrink) return;
|
|
event.preventDefault();
|
|
saved.value.width = width.value + (grow ? 16 : -16);
|
|
}
|
|
|
|
function usePrompt(text: string): void {
|
|
composerText.value = text;
|
|
composer.value?.focus();
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div
|
|
v-if="target"
|
|
aria-hidden="true"
|
|
:class="cn('pointer-events-none fixed inset-y-0 z-40 border-primary/40 bg-primary/10', target === 'left' ? 'left-0 border-r-2' : 'right-0 border-l-2')"
|
|
:style="{ width: `${width}px` }"
|
|
/>
|
|
<div
|
|
:class="cn(
|
|
'pointer-events-none fixed z-40',
|
|
side ? cn('inset-y-0', side === 'left' ? 'left-0' : 'right-0') : 'inset-x-0 bottom-4 flex justify-center px-3',
|
|
)"
|
|
:style="side ? { width: `${width}px` } : desktop ? undefined : { bottom: phoneBottom }"
|
|
>
|
|
<aside
|
|
ref="dock"
|
|
aria-label="Chat with Archie"
|
|
:class="cn(
|
|
'touch-hit pointer-events-auto relative flex flex-col border-border-strong bg-popover shadow-[var(--shadow-md)]',
|
|
side
|
|
? cn('h-full w-full', side === 'left' ? 'border-r' : 'border-l')
|
|
: 'w-full max-w-xl rounded-lg border',
|
|
offset && 'z-50 opacity-90',
|
|
)"
|
|
:style="offset ? { transform: `translate(${offset.x}px, ${offset.y}px)` } : undefined"
|
|
>
|
|
<div
|
|
v-if="side"
|
|
role="separator"
|
|
aria-orientation="vertical"
|
|
aria-label="Resize chat"
|
|
tabindex="0"
|
|
:aria-valuenow="width"
|
|
:aria-valuemin="MIN_WIDTH"
|
|
class="absolute inset-y-0 z-10 w-1.5 cursor-col-resize hover:bg-primary/30 focus-visible:bg-primary/30 focus-visible:outline-none"
|
|
:class="side === 'left' ? '-right-0.5' : '-left-0.5'"
|
|
@pointerdown.prevent="resize"
|
|
@keydown="resizeByKey"
|
|
/>
|
|
<div
|
|
v-if="expanded"
|
|
:class="desktop ? 'cursor-grab select-none active:cursor-grabbing' : 'touch-none'"
|
|
@pointerdown="startDrag"
|
|
@dblclick="togglePin"
|
|
>
|
|
<div class="flex justify-center pt-1.5" aria-hidden="true">
|
|
<span class="h-1 w-8 rounded-full bg-border-strong" />
|
|
</div>
|
|
<ChatBar>
|
|
<Tooltip>
|
|
<TooltipTrigger as-child>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
class="flex-none"
|
|
aria-label="Hide conversation"
|
|
@click="expanded = false"
|
|
>
|
|
<ChevronDown />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Hide conversation</TooltipContent>
|
|
</Tooltip>
|
|
</ChatBar>
|
|
</div>
|
|
<div
|
|
v-if="expanded"
|
|
:class="cn('flex min-h-0 flex-col', side ? 'flex-1' : 'h-[min(28rem,55dvh)]')"
|
|
:style="side || desktop ? undefined : { height: `min(28rem, ${Math.round(visibleHeight * 0.55)}px)` }"
|
|
>
|
|
<ChatTranscript @prompt="usePrompt" />
|
|
</div>
|
|
<div @focusin="expanded = true">
|
|
<ChatComposer ref="composer" :open="false" :flat="!expanded">
|
|
<template v-if="!expanded" #actions>
|
|
<Tooltip>
|
|
<TooltipTrigger as-child>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
:class="cn(unseenCount && 'w-auto gap-1 px-2')"
|
|
:aria-label="unseenCount ? `Show conversation, ${unseenCount} new` : 'Show conversation'"
|
|
@click="expanded = true"
|
|
>
|
|
<ChevronUp />
|
|
<Badge v-if="unseenCount" variant="default" aria-hidden="true">{{ unseenCount }}</Badge>
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Show conversation</TooltipContent>
|
|
</Tooltip>
|
|
</template>
|
|
</ChatComposer>
|
|
</div>
|
|
</aside>
|
|
</div>
|
|
</template>
|