import type { MemoryScope } from "@/types/memory"; import type { FileMemoryScope } from "@/types/file-memory"; import type { Wiki, WikiBuildStatus } from "@/types/wiki"; import type { ChatScopeStorageMode, ChatScopeWikiPolicy, } from "@/lib/chat-scope-entry"; export type ScopeFeatureSegment = "scope" | "units" | "search" | "dream" | "wiki"; export type ScopeWorkspaceStatus = | "loading" | "draft" | "building" | "ready" | "stale" | "failed_with_fallback" | "blocked"; const NON_TERMINAL = new Set([ "pending", "processing", "accepting_output", "sealing", "validating", "indexing", "publishing", ]); export function parseExactScope( params: URLSearchParams, ): FileMemoryScope | null { const values = ["appId", "tenantId", "agentId", "runId"].map( (key) => params.get(key)?.trim() ?? "", ); if (values.some((value) => !value || value === "*")) return null; const [appId, tenantId, agentId, runId] = values; return { appId, tenantId, agentId, runId }; } export function toExactScope(scope: MemoryScope): FileMemoryScope | null { return parseExactScope( new URLSearchParams({ appId: scope.appId ?? "", tenantId: scope.tenantId ?? "", agentId: scope.agentId ?? "", runId: scope.runId ?? "", }), ); } function scopeParams( scope: FileMemoryScope, extra: Record = {}, ) { const params = new URLSearchParams({ appId: scope.appId, tenantId: scope.tenantId, agentId: scope.agentId, runId: scope.runId, }); for (const [key, value] of Object.entries(extra)) { if (!["appId", "tenantId", "agentId", "runId"].includes(key)) { params.set(key, value); } } return params; } export function buildScopeFeatureHref( store: string, segment: ScopeFeatureSegment, scope: FileMemoryScope, extra: Record = {}, ): string { return `/memory/${encodeURIComponent(store)}/${segment}?${scopeParams(scope, extra)}`; } export function buildScopeWorkspaceHref(store: string, scope: FileMemoryScope) { return buildScopeFeatureHref(store, "scope", scope); } export function buildScopeChatHref( memoryStoreName: string, storageMode: ChatScopeStorageMode, scope: FileMemoryScope, wiki: ChatScopeWikiPolicy, ): string { if (storageMode !== "ots" && storageMode !== "file+ots") { throw new Error("Unsupported Chat Scope storage mode"); } const params = new URLSearchParams({ memoryStoreName, storageMode, appId: scope.appId, tenantId: scope.tenantId, agentId: scope.agentId, runId: scope.runId, wiki, }); return `/chat?${params}`; } export function deriveScopeWorkspaceStatus(input: { loading?: boolean; error?: unknown; wiki?: Pick & { latestBuild?: { status: WikiBuildStatus }; }; }): ScopeWorkspaceStatus { if (input.loading) return "loading"; if (input.error || !input.wiki) return "blocked"; const latestStatus = input.wiki.latestBuild?.status; if (latestStatus && NON_TERMINAL.has(latestStatus)) return "building"; if (latestStatus === "failed" || latestStatus === "conflict") { return input.wiki.status === "ready" ? "failed_with_fallback" : "blocked"; } if (input.wiki.status !== "ready") return "draft"; if (input.wiki.hasPendingChanges) { return "stale"; } return "ready"; }