Add memory layers and RAG retrieval

This commit is contained in:
dahyeon committed 2026-05-18 22:33:25 -07:00
1 parent 1d0be56842
commit 1a86bded0b
31 files changed
+1592 -58

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
import type mongoose from "mongoose";
import type { GarnetClient } from "@common/garnet";
import type { Blackboard } from "@skills/onboarding/types";
import { createEmbeddingClient } from "./embedding-client.js";
import { GarnetSessionMemory } from "./garnet-session-memory.js";
import {
MongoLongTermMemory,
NoopLongTermMemory,
} from "./mongo-long-term-memory.js";
import type {
LongTermMemory,
MemoryService,
SearchOptions,
SessionMemory,
WorkingMemory,
} from "./memory-types.js";
interface CreateMemoryServiceOptions {
garnetClient: GarnetClient;
mongooseConnection?: mongoose.Connection | undefined;
}
function defaultBlackboard(): Blackboard {
return {
flaggedIssues: [],
openQuestions: [],
recentNicknameAttempts: [],
toneMemory: {},
};
}
export class DefaultMemoryService implements MemoryService {
constructor(
readonly session: SessionMemory,
readonly longTerm: LongTermMemory,
) {}
async assembleWorkingMemory(input: {
skill: string;
chatroomId: string;
userId: string;
includeBlackboard?: boolean;
includeProfile?: boolean;
retrieval?: { query: string; options?: SearchOptions | undefined } | undefined;
}): Promise<WorkingMemory> {
const [
conversationHistory,
summary,
blackboard,
userProfile,
retrievedKnowledge,
] = await Promise.all([
this.session.getConversationHistory(input.chatroomId, input.userId),
this.session.getSummary(input.chatroomId, input.userId),
input.includeBlackboard
? this.session.getBlackboard<Blackboard>(
input.skill,
input.chatroomId,
input.userId,
)
: Promise.resolve(null),
input.includeProfile
? this.longTerm.loadUserProfile(input.userId)
: Promise.resolve(null),
input.retrieval
? this.longTerm.search(input.retrieval.query, input.retrieval.options ?? {})
: Promise.resolve(undefined),
]);
return {
conversationHistory,
...(summary ? { summary } : {}),
...(input.includeBlackboard
? { blackboard: blackboard ?? defaultBlackboard() }
: {}),
...(userProfile ? { userProfile } : {}),
...(retrievedKnowledge ? { retrievedKnowledge } : {}),
};
}
}
export function createMemoryService(
options: CreateMemoryServiceOptions,
): MemoryService {
const session = new GarnetSessionMemory(options.garnetClient);
const longTerm = options.mongooseConnection
? new MongoLongTermMemory(
options.garnetClient,
options.mongooseConnection,
createEmbeddingClient(),
)
: new NoopLongTermMemory(options.garnetClient);
return new DefaultMemoryService(session, longTerm);
}