import type { SymbolRegistrySnapshot } from "../symbol-registry.js"; import type { AgentSessionState } from "../session-state.js"; import type { AgentStateStore, StateBackend } from "./define-state.js"; import { sessionKvKey, symbolsKvKey } from "./fs-state-adapter.js"; type KvClient = { get(key: string): Promise; set(key: string, value: unknown): Promise; keys(pattern: string): Promise; }; async function loadKv(): Promise { const mod = await import("@vercel/kv"); return mod.kv as KvClient; } export class KvStateAdapter implements AgentStateStore { constructor( private readonly agentRoot: string, private readonly tenantScope: string, ) { void this.agentRoot; } backend(): StateBackend { return "kv"; } async get(intent: string): Promise { const kv = await loadKv(); return kv.get(sessionKvKey(this.tenantScope, intent)); } async put(state: AgentSessionState): Promise { const kv = await loadKv(); await kv.set(sessionKvKey(this.tenantScope, state.intent), state); } async listIntents(): Promise { const kv = await loadKv(); const keys = await kv.keys(`plasm:${this.tenantScope}:session:*`); const intents: string[] = []; for (const key of keys) { const state = await kv.get(key); if (state?.intent) intents.push(state.intent); } return intents; } async getSymbolRegistry(tenantId: string): Promise { const kv = await loadKv(); return kv.get(symbolsKvKey(tenantId)); } async putSymbolRegistry( tenantId: string, snapshot: SymbolRegistrySnapshot, ): Promise { const kv = await loadKv(); await kv.set(symbolsKvKey(tenantId), snapshot); } }