"use client"; import { useMemoryStore } from "@/hooks/use-memory-store"; import { useMemoryStoreScopes } from "@/hooks/use-memory-store-scopes"; import { useWiki } from "@/hooks/use-wikis"; import { useI18n } from "@/i18n/i18n-context"; import { parseKnownStorageMode, type StorageMode, } from "@/lib/memory-storage-mode"; import { deriveScopeWorkspaceStatus } from "@/lib/scope-workspace"; import { ApiClientError } from "@/services/api-client"; import type { MemoryScope } from "@/types/memory"; import type { FileMemoryScope } from "@/types/file-memory"; import { ScopeWorkspaceHeader, type ScopeWorkspaceViewModel, } from "./scope-workspace-cards"; function sameScope(left: MemoryScope, right: FileMemoryScope): boolean { return ( left.appId === right.appId && left.tenantId === right.tenantId && left.agentId === right.agentId && left.runId === right.runId ); } function isPendingOrLoading(query: { isPending?: boolean; isLoading?: boolean; }): boolean { return Boolean(query.isPending || query.isLoading); } function errorText(error: unknown, fallback: string): string | undefined { if (!error) return undefined; if (error instanceof ApiClientError) { return [error.displayMessage, error.requestId].filter(Boolean).join(" ยท "); } return fallback; } export function ScopeWorkspace({ memoryStoreName, scope, }: { memoryStoreName: string; scope: FileMemoryScope; }) { const store = useMemoryStore(memoryStoreName); if (isPendingOrLoading(store)) { return ; } if (store.error) { return ; } const storageMode = parseKnownStorageMode(store.data?.storageMode); if ( storageMode === null || storageMode === "filemem" || storageMode === "managed-file" ) { return ; } return ( ); } function ScopeWorkspaceGateState({ state, }: { state: "loading" | "error" | "unsupported"; }) { const { t } = useI18n(); const messageKey = state === "loading" ? "memory.scopeWorkspace.loading" : state === "error" ? "memory.scopeWorkspace.loadFailed" : "memory.scopeWorkspace.unsupported"; return (
{t(messageKey)}
); } function LoadedScopeWorkspace({ memoryStoreName, scope, storageMode, }: { memoryStoreName: string; scope: FileMemoryScope; storageMode: Exclude; }) { const { t } = useI18n(); const scopeInventory = useMemoryStoreScopes(memoryStoreName, scope, { limit: 1, }); const wiki = useWiki(memoryStoreName, scope); const safeError = t("memory.scopeWorkspace.loadFailed"); const memoryError = errorText(scopeInventory.error, safeError); const status = deriveScopeWorkspaceStatus({ loading: isPendingOrLoading(scopeInventory) || isPendingOrLoading(wiki), error: wiki.error, wiki: wiki.data, }); const exists = scopeInventory.data?.items.some((item) => sameScope(item, scope), ); const wikiEnabled = wiki.data?.status === "ready"; const view: Pick< ScopeWorkspaceViewModel, | "memoryStoreName" | "storageMode" | "chatStorageMode" | "scope" | "status" | "memory" | "chat" > = { memoryStoreName, storageMode, chatStorageMode: storageMode, scope, status, memory: { exists, loading: isPendingOrLoading(scopeInventory), error: memoryError, }, chat: { memoryEnabled: true, wikiEnabled, usesFallback: status === "failed_with_fallback", }, }; return (
); }