import { parseExactScope } from "@/lib/scope-workspace"; import type { FileMemoryScope } from "@/types/file-memory"; export type ChatScopeStorageMode = "ots" | "file+ots"; export type ChatScopeWikiPolicy = "auto" | "off"; export interface ChatScopeEntry { memoryStoreName: string; storageMode: ChatScopeStorageMode; scope: FileMemoryScope; wiki: ChatScopeWikiPolicy; } export function resolveChatScopeStorageMode( value: unknown, ): ChatScopeStorageMode | undefined { if (value === "ots") return "ots"; if (value === "file+ots" || value === "oss+ots") return "file+ots"; return undefined; } export function parseChatScopeEntry( params: URLSearchParams, ): ChatScopeEntry | null { const memoryStoreName = params.get("memoryStoreName")?.trim() ?? ""; const storageMode = params.get("storageMode"); const scope = parseExactScope(params); const wiki = params.get("wiki"); if ( !memoryStoreName || (storageMode !== "ots" && storageMode !== "file+ots") || !scope || (wiki !== "auto" && wiki !== "off") ) { return null; } return { memoryStoreName, storageMode, scope, wiki }; } export type ChatScopeHydrationDecision = { action: "hydrate"; enableWiki: boolean } | { action: "ignore" }; export function decideChatScopeHydration(input: { entry: ChatScopeEntry; activeSessionId: string | null; messageCount: number; wikiReady: boolean; handled: boolean; }): ChatScopeHydrationDecision { if (input.handled || input.activeSessionId || input.messageCount > 0) { return { action: "ignore" }; } return { action: "hydrate", enableWiki: input.entry.wiki === "auto" && input.wikiReady, }; }