"use client"; import { useEffect, useMemo, useReducer, useState, type FormEvent, } from "react"; import { ArrowLeft, BookOpen, ChevronDown, ChevronRight, FileText, Folder, FolderOpen, Search, GitBranch, ListTree, } from "lucide-react"; import { Markdown } from "@/components/qa/markdown"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, } from "@/components/ui/dialog"; import { WikiGraphView } from "./wiki-graph"; import { WikiSourceViewer } from "./wiki-source-viewer"; import { resolveWikiSourcePath } from "@/lib/wiki-graph"; import { useSearchWikiPages, useWikiPage, useWikiPages, useWikiSources, useWikiSource, } from "@/hooks/use-wikis"; import { useI18n } from "@/i18n/i18n-context"; import { buildWikiPageTree, expandWikiPageAncestors, flattenWikiPageTree, mergeWikiPages, toggleWikiDirectory, } from "@/lib/wiki-page-tree"; import { ApiClientError } from "@/services/api-client"; import type { WikiPage, WikiScope } from "@/types/wiki"; import { cn } from "@/lib/utils"; interface WikiPagePanelProps { memoryStoreName: string; scope: WikiScope; enabled: boolean; selectedPagePath?: string; onOpenPage: (path: string) => void; } export interface WikiSearchState { draft: string; submitted: string; } export const INITIAL_WIKI_SEARCH_STATE: WikiSearchState = { draft: "", submitted: "", }; export type WikiSearchAction = { type: "draft"; value: string } | { type: "submit" }; export function reduceWikiSearchState( state: WikiSearchState, action: WikiSearchAction, ): WikiSearchState { if (action.type === "draft") return { ...state, draft: action.value }; return { ...state, submitted: state.draft.trim() }; } export function openWikiSearchResult( page: Pick, onOpenPage: (path: string) => void, ): void { onOpenPage(page.path); } export { WikiSourcePreview } from "./source-preview-content"; export function WikiPagesPanel(props: WikiPagePanelProps) { const { t } = useI18n(); const [collapsed, setCollapsed] = useState>(new Set()); const [view, setView] = useState<"graph" | "document">( props.selectedPagePath ? "document" : "graph", ); const [previewHistory, setPreviewHistory] = useState< Array<{ kind: "page" | "source"; path: string }> >([]); const preview = previewHistory.at(-1); const sourcePath = preview?.kind === "source" ? preview.path : undefined; function openPreview(kind: "page" | "source", path: string) { setPreviewHistory((history) => { const current = history.at(-1); return current?.kind === kind && current.path === path ? history : [...history, { kind, path }]; }); } const openSourcePreview = (path: string) => openPreview("source", path); const openPagePreview = (path: string) => openPreview("page", path); useEffect(() => { setPreviewHistory([]); }, [ props.memoryStoreName, props.scope.appId, props.scope.tenantId, props.scope.agentId, ]); const previewPage = useWikiPage( props.memoryStoreName, props.scope, props.enabled && preview?.kind === "page" ? { path: preview.path, includeReflectionInfo: false } : undefined, ); const sourceList = useWikiSources(props.memoryStoreName, props.scope, { limit: 100, enabled: props.enabled, }); const sources = useMemo( () => sourceList.data?.pages.flatMap((page) => page.sources) ?? [], [sourceList.data], ); const source = useWikiSource(props.memoryStoreName, props.scope, sourcePath); const list = useWikiPages(props.memoryStoreName, props.scope, { limit: 100, enabled: props.enabled, }); const pages = useMemo( () => mergeWikiPages(...(list.data?.pages.map((page) => page.pages) ?? [])), [list.data], ); const tree = useMemo(() => buildWikiPageTree(pages), [pages]); const rows = useMemo( () => flattenWikiPageTree(tree, collapsed), [collapsed, tree], ); const exactPage = useWikiPage( props.memoryStoreName, props.scope, props.enabled && props.selectedPagePath ? { path: props.selectedPagePath, includeReflectionInfo: false, } : undefined, ); useEffect(() => { const selectedPagePath = props.selectedPagePath; if (!selectedPagePath) return; setCollapsed((current) => expandWikiPageAncestors(current, selectedPagePath), ); }, [props.selectedPagePath]); if (!props.enabled) { return (

{t("wiki.detail.noReadableBuild")}

); } return (

{t("wiki.graph.loaded", { count: pages.length })}

{view === "graph" ? ( <> {list.isLoading ? (

{t("common.loading")}

) : ( )}
{list.hasNextPage ? ( ) : null} {sourceList.hasNextPage ? ( ) : null}
{list.error ? ( <> ) : null} {sourceList.error ? : null} ) : (
{exactPage.isLoading ? (

{t("common.loading")}

) : null} {exactPage.error ? : null} {exactPage.data ? ( ) : !props.selectedPagePath && !exactPage.isLoading ? (

{t("wiki.pages.selectHint")}

) : null}
)} { if (!open) setPreviewHistory([]); }} > { if (document.fullscreenElement) { event.preventDefault(); void document.exitFullscreen().catch(() => {}); } }} >
{previewHistory.length > 1 ? ( ) : null} {preview?.kind === "page" ? ( ) : ( )}
{t( preview?.kind === "page" ? "wiki.graph.page" : "wiki.graph.openSource", )} {preview?.path}
{preview?.kind === "page" ? ( previewPage.isLoading ? (

{t("common.loading")}

) : previewPage.error ? (
) : previewPage.data ? (
) : null ) : source.isLoading ? (

{t("common.loading")}

) : source.error ? ( ) : source.data ? ( <> {pages.some((page) => page.sources?.some( (ref) => resolveWikiSourcePath(ref, sources) === sourcePath, ), ) ? (
{t("wiki.preview.referencedBy")} {pages .filter((page) => page.sources?.some( (ref) => resolveWikiSourcePath(ref, sources) === sourcePath, ), ) .map((page) => ( ))}
) : null} ) : null}
); } export function WikiSearchPanel( props: Omit, ) { const { t } = useI18n(); const [searchState, dispatchSearch] = useReducer( reduceWikiSearchState, INITIAL_WIKI_SEARCH_STATE, ); const search = useSearchWikiPages(props.memoryStoreName, props.scope, { query: searchState.submitted, limit: 50, enabled: props.enabled && searchState.submitted.length > 0, includeReflectionInfo: false, }); function handleSubmit(event: FormEvent) { event.preventDefault(); if (!props.enabled) return; dispatchSearch({ type: "submit" }); } return (
dispatchSearch({ type: "draft", value: event.target.value }) } placeholder={t("wiki.search.placeholder")} />
{!props.enabled ? (

{t("wiki.detail.noReadableBuild")}

) : null} {props.enabled && search.error ? ( ) : null}
    {props.enabled ? search.data?.pages.map((page) => (
  1. )) : null}
{props.enabled && searchState.submitted && !search.isFetching && search.data?.pages.length === 0 ? (

{t("wiki.search.empty")}

) : null}
); } function WikiPageReader({ page, onOpenPage, onOpenSource, sourcePaths, pagePaths, }: { page: WikiPage; onOpenPage: (path: string) => void; onOpenSource: (path: string) => void; sourcePaths: { path: string }[]; pagePaths: { path: string }[]; }) { const { t } = useI18n(); return (
{ if ( event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey ) return; const link = event.target instanceof Element ? event.target.closest("a") : null; const href = link?.getAttribute("href"); if (!href || /^(?:[a-z][a-z\d+.-]*:|\/\/|#)/i.test(href)) return; let path: string; try { path = decodeURIComponent( new URL(href, `https://wiki.invalid${page.path}`).pathname, ); } catch { return; } const linkedPage = pagePaths.find((item) => item.path === path); if (linkedPage) { event.preventDefault(); onOpenPage(linkedPage.path); return; } const source = resolveWikiSourcePath(path, sourcePaths); if (source) { event.preventDefault(); onOpenSource(source); } }} >

{page.title || page.path}

{page.path}

{page.type ? {page.type} : null}
{page.description ? (

{page.description}

) : null} {page.content ?? ""} resolveWikiSourcePath(ref, sourcePaths)} />

{t("wiki.pages.linksHint")}

); } function PageMetadata({ title, values, onOpen, resolvePath, }: { title: string; values?: string[]; onOpen?: (path: string) => void; resolvePath?: (ref: string) => string | undefined; }) { if (!values?.length) return null; return (

{title}

{values.map((value) => onOpen && (!resolvePath || resolvePath(value)) ? ( ) : ( {value} ), )}
); } function WikiError({ error }: { error: Error }) { const { t } = useI18n(); const apiError = error instanceof ApiClientError ? error : undefined; return (

{apiError?.displayMessage ?? error.message}

{apiError?.requestId ? (

{t("wiki.detail.requestId")}: {apiError.requestId}

) : null} {apiError?.details?.upstreamRequestId ? (

{t("wiki.detail.upstreamRequestId")}:{" "} {apiError.details.upstreamRequestId}

) : null}
); }