import Link from "next/link"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import type { MessageKey, TranslateFn } from "@/i18n/i18n-context"; import type { ChatScopeStorageMode } from "@/lib/chat-scope-entry"; import { buildScopeChatHref, buildScopeFeatureHref, type ScopeWorkspaceStatus, } from "@/lib/scope-workspace"; import type { StorageMode } from "@/lib/memory-storage-mode"; import type { WikiBuildChanges, WikiBuildQuality, WikiBuildStatus, } from "@/types/wiki"; import type { FileMemoryScope } from "@/types/file-memory"; export interface ScopeWorkspaceViewModel { memoryStoreName: string; storageMode: StorageMode; chatStorageMode?: ChatScopeStorageMode; scope: FileMemoryScope; status: ScopeWorkspaceStatus; memory: { exists?: boolean; loading: boolean; error?: string }; dream: { skillCount: number; profileCount: number; partial: boolean; error?: string; }; wiki: { pageCount?: number; quality?: WikiBuildQuality; changes?: WikiBuildChanges; latestStatus?: WikiBuildStatus; error?: string; }; chat: { memoryEnabled: true; wikiEnabled: boolean; usesFallback: boolean }; } const REVIEWABLE_STATUSES = new Set([ "draft", "stale", "failed_with_fallback", "blocked", ]); const STATUS_VARIANT: Record< ScopeWorkspaceStatus, "secondary" | "success" | "warning" | "danger" > = { loading: "secondary", draft: "secondary", building: "warning", ready: "success", stale: "warning", failed_with_fallback: "warning", blocked: "danger", }; const STATUS_DESCRIPTION_KEY: Record = { loading: "memory.scopeWorkspace.statusDescription.loading", draft: "memory.scopeWorkspace.statusDescription.draft", building: "memory.scopeWorkspace.statusDescription.building", ready: "memory.scopeWorkspace.statusDescription.ready", stale: "memory.scopeWorkspace.statusDescription.stale", failed_with_fallback: "memory.scopeWorkspace.statusDescription.failed_with_fallback", blocked: "memory.scopeWorkspace.statusDescription.blocked", }; function ScopeLinks({ view, segment, label, }: { view: ScopeWorkspaceViewModel; segment: "units" | "search" | "dream" | "wiki"; label: string; }) { return ( ); } type ScopeWorkspaceHeaderView = Pick< ScopeWorkspaceViewModel, | "memoryStoreName" | "storageMode" | "chatStorageMode" | "scope" | "status" | "memory" | "chat" >; function canOpenStructuredChat( view: ScopeWorkspaceHeaderView, ): view is ScopeWorkspaceHeaderView & { chatStorageMode: ChatScopeStorageMode; } { return ( view.chatStorageMode !== undefined && !view.memory.loading && !view.memory.error ); } export function ScopeWorkspaceHeader({ view, t, }: { view: ScopeWorkspaceHeaderView; t: TranslateFn; }) { const reviewHref = buildScopeFeatureHref( view.memoryStoreName, "wiki", view.scope, { tab: "build", }, ); const scopeText = [ view.scope.appId, view.scope.tenantId, view.scope.agentId, view.scope.runId, ].join(" / "); const statusDescriptionKey: MessageKey = view.status === "building" && !view.chat.wikiEnabled ? "memory.scopeWorkspace.statusDescription.firstBuilding" : STATUS_DESCRIPTION_KEY[view.status]; return (

{t("memory.scopeWorkspace.title")}

{t(`memory.scopeWorkspace.status.${view.status}`)}

{t("memory.scopeWorkspace.description")}

{t("memory.scopeWorkspace.store")}
{view.memoryStoreName}
{t("memory.scopeWorkspace.scope")}
{scopeText}

{t(statusDescriptionKey)}

{REVIEWABLE_STATUSES.has(view.status) ? ( ) : null} {canOpenStructuredChat(view) ? ( ) : null}
); } export function ScopeWorkspaceMemoryCard({ view, t, }: { view: ScopeWorkspaceViewModel; t: TranslateFn; }) { return ( {t("memory.scopeWorkspace.memoryTitle")} {t("memory.scopeWorkspace.memoryDescription")} {view.memory.loading ? (

{t("memory.scopeWorkspace.loading")}

) : null} {!view.memory.loading && !view.memory.error ? (

{view.memory.exists ? t("memory.scopeWorkspace.exists") : t("memory.scopeWorkspace.notFound")}

) : null} {view.memory.error ? (

{view.memory.error}

) : null}
); } export function ScopeWorkspaceDreamCard({ view, t, }: { view: ScopeWorkspaceViewModel; t: TranslateFn; }) { return ( {t("memory.scopeWorkspace.dreamTitle")} {t("memory.scopeWorkspace.dreamDescription")}

{t("memory.scopeWorkspace.skillCount", { count: view.dream.skillCount, })}

{t("memory.scopeWorkspace.profileCount", { count: view.dream.profileCount, })}

{view.dream.partial ? (

{t("memory.scopeWorkspace.partial")}

) : null} {view.dream.error ? (

{view.dream.error}

) : null}
); } export function ScopeWorkspaceWikiCard({ view, t, }: { view: ScopeWorkspaceViewModel; t: TranslateFn; }) { const quality = view.wiki.quality; const changes = view.wiki.changes; return ( {t("memory.scopeWorkspace.wikiTitle")} {t("memory.scopeWorkspace.wikiDescription")} {view.wiki.pageCount !== undefined ? (

{t("memory.scopeWorkspace.pageCount", { count: view.wiki.pageCount, })}

) : null} {view.wiki.latestStatus ? (

{t("memory.scopeWorkspace.latestStage", { status: view.wiki.latestStatus, })}

) : null} {quality ? ( <>

{t("memory.scopeWorkspace.qualitySourceCoverage", { percent: Math.round(quality.sourceCoverage * 100), })}

{t("memory.scopeWorkspace.qualityGroundedRate", { percent: Math.round(quality.groundedPageRate * 100), })}

) : null} {changes ? ( <>

{t("memory.scopeWorkspace.changesAdded", { count: changes.added, })}

{t("memory.scopeWorkspace.changesModified", { count: changes.modified, })}

{t("memory.scopeWorkspace.changesDeleted", { count: changes.deleted, })}

{t("memory.scopeWorkspace.changesAffectedPages", { count: changes.affectedPages, })}

) : null} {view.status === "stale" ? (

{t("memory.scopeWorkspace.staleGuidance")}

) : null} {view.status === "failed_with_fallback" ? (

{t("memory.scopeWorkspace.fallbackGuidance")}

) : null} {view.status === "building" ? (

{view.chat.wikiEnabled ? t("memory.scopeWorkspace.buildingGuidance") : t("memory.scopeWorkspace.firstBuildGuidance")}

) : null} {view.status === "draft" ? (

{t("memory.scopeWorkspace.draftGuidance")}

) : null} {view.wiki.error ? (

{view.wiki.error}

) : null}
); } export function ScopeWorkspaceChatCard({ view, t, }: { view: ScopeWorkspaceViewModel; t: TranslateFn; }) { return ( {t("memory.scopeWorkspace.chatTitle")} {t("memory.scopeWorkspace.chatDescription")}

{t("memory.scopeWorkspace.chatMemoryEnabled")}

{view.chat.wikiEnabled ? t("memory.scopeWorkspace.chatWikiEnabled") : t("memory.scopeWorkspace.chatWikiDisabled")}

{view.chat.usesFallback ? (

{t("memory.scopeWorkspace.chatFallback")}

) : null}
{canOpenStructuredChat(view) ? ( ) : null}
); } export function ScopeWorkspaceCards({ view, t, }: { view: ScopeWorkspaceViewModel; t: TranslateFn; }) { return (
); }