71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
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>,
|
|
): 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<Blackboard> {
|
|
const key = getBlackboardKey(chatroomId, userId);
|
|
const saved = await this.garnetClient.getJson<Blackboard>(key);
|
|
return mergeBlackboard(saved, {});
|
|
}
|
|
|
|
async save(
|
|
chatroomId: string,
|
|
userId: string,
|
|
value: Blackboard,
|
|
): Promise<void> {
|
|
const key = getBlackboardKey(chatroomId, userId);
|
|
await this.garnetClient.setJson(
|
|
key,
|
|
value,
|
|
getBlackboardTtlSeconds(),
|
|
);
|
|
}
|
|
|
|
async update(
|
|
chatroomId: string,
|
|
userId: string,
|
|
updater: (current: Blackboard) => Blackboard,
|
|
): Promise<Blackboard> {
|
|
const current = await this.load(chatroomId, userId);
|
|
const next = updater(current);
|
|
await this.save(chatroomId, userId, next);
|
|
return next;
|
|
}
|
|
}
|