diff --git a/src/components/layout/celebi-markdown.test.tsx b/src/components/layout/celebi-markdown.test.tsx index 649ab3c..88f6794 100644 --- a/src/components/layout/celebi-markdown.test.tsx +++ b/src/components/layout/celebi-markdown.test.tsx @@ -4,6 +4,14 @@ import { CelebiMarkdown } from "@/components/layout/celebi-markdown"; test("renders bold text", () => { const { container } = render(() => ); expect(container.querySelector("strong")?.textContent).toBe("bold"); + expect(container.querySelector("strong")?.className).toContain("underline"); +}); + +test("keeps the typing cursor at the end of the visible answer", () => { + const { container } = render(() => ); + const cursor = container.querySelector('[aria-hidden="true"]'); + expect(container.querySelectorAll('[aria-hidden="true"]')).toHaveLength(1); + expect(cursor?.parentElement?.textContent).toContain("last"); }); test("renders inline code", () => { diff --git a/src/components/layout/celebi-markdown.tsx b/src/components/layout/celebi-markdown.tsx index 6532c31..95011b3 100644 --- a/src/components/layout/celebi-markdown.tsx +++ b/src/components/layout/celebi-markdown.tsx @@ -1,4 +1,5 @@ -import { For, Show } from "solid-js"; +import { For, Show, type JSX } from "solid-js"; +import { searchMatchRanges } from "@/lib/search-text"; type Segment = | { kind: "text"; value: string } @@ -100,16 +101,37 @@ function parseBlocks(text: string): Block[] { return blocks; } -function InlineSegments(props: { segments: Segment[] }) { +function highlightedText(value: string, query?: string): JSX.Element { + const ranges = searchMatchRanges(value, query ?? ""); + if (ranges.length === 0) return <>{value}; + + const parts: JSX.Element[] = []; + let cursor = 0; + for (const range of ranges) { + if (range.start > cursor) parts.push(<>{value.slice(cursor, range.start)}); + parts.push( + + {value.slice(range.start, range.end)} + , + ); + cursor = range.end; + } + if (cursor < value.length) parts.push(<>{value.slice(cursor)}); + return <>{parts}; +} + +function InlineSegments(props: { segments: Segment[]; searchQuery?: string }) { return ( {(segment) => ( - {(segment as { value: string }).value}}> + - {(segment as { value: string }).value} + + {highlightedText((segment as { value: string }).value, props.searchQuery)} + - {(segment as { value: string }).value} + {highlightedText((segment as { value: string }).value, props.searchQuery)} - {(segment as { label: string }).label} + {highlightedText((segment as { label: string }).label, props.searchQuery)} @@ -127,30 +149,46 @@ function InlineSegments(props: { segments: Segment[] }) { ); } -export function CelebiMarkdown(props: { text: string }) { +export function CelebiMarkdown(props: { text: string; showCursor?: boolean; searchQuery?: string }) { const blocks = () => parseBlocks(props.text); return (
- {(block) => ( + {(block, index) => { + const lastBlock = () => index() === blocks().length - 1; + return ( - {(item) =>
  • } + {(item, itemIndex) => ( +
  • + + + +
  • + )}
    }>
    -                {(block as { value: string }).value}
    +                {highlightedText((block as { value: string }).value, props.searchQuery)}
    +                
    +                  
                   
    }>

    - + + +

    - )} + ); + }}
    ); diff --git a/src/components/layout/celebi-panel.tsx b/src/components/layout/celebi-panel.tsx index 5739386..a87a03b 100644 --- a/src/components/layout/celebi-panel.tsx +++ b/src/components/layout/celebi-panel.tsx @@ -1,4 +1,4 @@ -import { For, Match, Show, Switch, createEffect, createSignal, on, onCleanup, untrack } from "solid-js"; +import { For, Match, Show, Suspense, Switch, createEffect, createSignal, on, onCleanup, untrack, type JSX } from "solid-js"; import { useNavigate } from "@tanstack/solid-router"; import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; import { formatApiError } from "@/api/client"; @@ -10,10 +10,11 @@ import { CelebiThreadList } from "@/components/layout/celebi-thread-list"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DataTableSearch } from "@/components/ui/data-table-search"; import { EmptyInline } from "@/components/ui/empty-inline"; -import { IconAlert, IconBotSquare, IconChevronDown, IconChevronLeft, IconCopy, IconEdit, IconMessage, IconPlus } from "@/components/ui/icons"; +import { IconAlert, IconBotSquare, IconChevronDown, IconChevronLeft, IconChevronUp, IconCopy, IconEdit, IconMessage, IconPlus, IconSearch, IconX } from "@/components/ui/icons"; +import { PageSpinner } from "@/components/ui/page-spinner"; import { SidePanel } from "@/components/ui/side-panel"; import { cn } from "@/lib/cn"; -import { matchesSearch } from "@/lib/search-text"; +import { matchesSearch, searchMatchRanges } from "@/lib/search-text"; import { usePreferences, useT } from "@/stores/preferences-context"; type PanelMessage = Pick; @@ -42,6 +43,8 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean const [sending, setSending] = createSignal(false); const [copiedId, setCopiedId] = createSignal(); const [searchQuery, setSearchQuery] = createSignal(""); + const [searchOpen, setSearchOpen] = createSignal(false); + const [searchMatchIndex, setSearchMatchIndex] = createSignal(0); const copyMessage = async (message: PanelMessage) => { await navigator.clipboard.writeText(message.content); @@ -134,6 +137,66 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean if (!query) return messages(); return messages().filter((message) => matchesSearch(query, message.content)); }; + const updateSearchQuery = (value: string) => { + setSearchQuery(value); + setSearchMatchIndex(0); + }; + const searchHits = () => { + const query = searchQuery().trim(); + if (!query) return [] as Array<{ messageId: string }>; + return messages().flatMap((message) => + searchMatchRanges(message.content, query).map(() => ({ messageId: message.id })), + ); + }; + const activeSearchIndex = () => { + const total = searchHits().length; + return total > 0 ? Math.min(searchMatchIndex(), total - 1) : 0; + }; + const activeSearchHit = () => searchHits()[activeSearchIndex()]; + const scrollToSearchHit = (hit: { messageId: string } | undefined) => { + if (!hit || !transcript) return; + const messageNode = Array.from(transcript.querySelectorAll("[data-search-message-id]")) + .find((node) => node.dataset.searchMessageId === hit.messageId); + messageNode?.scrollIntoView({ behavior: "smooth", block: "center" }); + }; + const moveSearchMatch = (direction: -1 | 1) => { + const total = searchHits().length; + if (total === 0) return; + const next = (activeSearchIndex() + direction + total) % total; + setSearchMatchIndex(next); + requestAnimationFrame(() => scrollToSearchHit(searchHits()[next])); + }; + const handleSearchKeyDown = (event: KeyboardEvent) => { + if (event.key === "ArrowUp") { + event.preventDefault(); + moveSearchMatch(-1); + } else if (event.key === "ArrowDown") { + event.preventDefault(); + moveSearchMatch(1); + } + }; + const highlightedMessageText = (value: string, query: string): JSX.Element => { + const ranges = searchMatchRanges(value, query); + if (ranges.length === 0) return <>{value}; + const parts: JSX.Element[] = []; + let cursor = 0; + for (const range of ranges) { + if (range.start > cursor) parts.push(<>{value.slice(cursor, range.start)}); + parts.push( + + {value.slice(range.start, range.end)} + , + ); + cursor = range.end; + } + if (cursor < value.length) parts.push(<>{value.slice(cursor)}); + return <>{parts}; + }; + createEffect(() => { + const hit = activeSearchHit(); + if (!hit) return; + requestAnimationFrame(() => scrollToSearchHit(hit)); + }); createEffect(() => { const items = messages(); typedCount(); @@ -176,12 +239,19 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean if (match && threadId() === active) setThreadTitle(match.title); } catch { /* history is non-blocking */ } }; - const openThread = async (thread: ChatbotThread) => { stopPolling(); stopStream(); stopReveal(); setPinned(true); setUnread(false); setView("chat"); setSearchQuery(""); setThreadId(thread.id); setThreadTitle(thread.title); setMessages((await getChatbotThreadMessages(thread.id, { limit: 500 })).items); }; - const createThread = () => { stopPolling(); stopStream(); stopReveal(); setSearchQuery(""); setThreadId(undefined); setThreadTitle(null); setMessages([]); }; + const openThread = async (thread: ChatbotThread) => { stopPolling(); stopStream(); stopReveal(); setPinned(true); setUnread(false); setView("chat"); setSearchOpen(false); updateSearchQuery(""); setThreadId(thread.id); setThreadTitle(thread.title); setMessages((await getChatbotThreadMessages(thread.id, { limit: 500 })).items); }; + const createThread = () => { stopPolling(); stopStream(); stopReveal(); setSearchOpen(false); updateSearchQuery(""); setThreadId(undefined); setThreadTitle(null); setMessages([]); }; + const startNewChat = () => { createThread(); setView("chat"); }; + const openThreads = () => { setSearchOpen(false); setSearchQuery(""); setView("threads"); }; // Rename and delete go through the shared confirm dialog, never the // browser's own prompt()/confirm() boxes. const [renaming, setRenaming] = createSignal(null); const [removing, setRemoving] = createSignal(null); + const renameActiveThread = () => { + const activeId = threadId(); + if (!activeId) return; + setRenaming({ id: activeId, title: threadTitle(), created_at: 0, updated_at: 0 }); + }; const renameThread = async (thread: ChatbotThread, title: string | undefined) => { const updated = await patchChatbotThreadById(thread.id, { title: title?.trim() || null }); if (threadId() === thread.id) setThreadTitle(updated.title); @@ -282,49 +352,120 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean return (
    -
    +
    setView("chat")}> - - {t("common.back")} - +
    + + {t("ai.chats")} + +
    } > - -
    - - {view() === "threads" ? t("ai.chats") : threadId() ? threadTitle() || t("ai.untitledChat") : t("ai.newChat")} - - - +
    +
    + + +
    + + {threadTitle() || t("ai.untitledChat")} + + +
    + { setSearchOpen(false); updateSearchQuery(""); }}> + + + } + > + + +
    +
    - void openThread(thread)} - onRename={setRenaming} - onRemove={setRemoving} - /> + }> + void openThread(thread)} + onRename={setRenaming} + onRemove={setRemoving} + /> +
    {/* Hidden rather than unmounted while the history is open, so going back lands on the same scroll position and a streaming answer keeps writing. */}
    -
    - -
    + +
    + + +
    + + {t("ai.searchCount", { current: searchHits().length > 0 ? activeSearchIndex() + 1 : 0, total: searchHits().length })} + + + +
    +
    +
    +
    {/* The jump control floats over the bottom of the transcript, next to the newest message it leads to, instead of sitting up by search. */}
    @@ -342,10 +483,16 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean
    {(message) => ( -
    +
    -

    {message.content}

    +

    {highlightedMessageText(message.content, searchQuery())}

    {failureMessage(message.error_code)}

    @@ -356,9 +503,8 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean
    - + -