import type { GarnetClient } from "@common/garnet"; import { getBlackboardTtlSeconds } from "../../../config.js"; import type { Blackboard } from "../types.js"; function getBlackboardKey(chatroomId: string, userId: string): string { return `lolympus-agent:bb:${chatroomId}:${userId}`; } function createDefaultBlackboard(): Blackboard { return { flaggedIssues: [], openQuestions: [], recentNicknameAttempts: [], toneMemory: {}, }; } function mergeBlackboard( current: Blackboard | null, next: Partial, ): Blackboard { const base = current ?? createDefaultBlackboard(); return { flaggedIssues: next.flaggedIssues ?? base.flaggedIssues, lastIntent: next.lastIntent ?? base.lastIntent, lastIntentAt: next.lastIntentAt ?? base.lastIntentAt, openQuestions: next.openQuestions ?? base.openQuestions, recentNicknameAttempts: next.recentNicknameAttempts ?? base.recentNicknameAttempts, toneMemory: { ...base.toneMemory, ...next.toneMemory, }, }; } export class OnboardingBlackboardStore { constructor(private readonly garnetClient: GarnetClient) {} async load(chatroomId: string, userId: string): Promise { const key = getBlackboardKey(chatroomId, userId); const saved = await this.garnetClient.getJson(key); return mergeBlackboard(saved, {}); } async save( chatroomId: string, userId: string, value: Blackboard, ): Promise { const key = getBlackboardKey(chatroomId, userId); await this.garnetClient.setJson( key, value, getBlackboardTtlSeconds(), ); } async update( chatroomId: string, userId: string, updater: (current: Blackboard) => Blackboard, ): Promise { const current = await this.load(chatroomId, userId); const next = updater(current); await this.save(chatroomId, userId, next); return next; } }