100 lines
2.9 KiB
TypeScript
100 lines
2.9 KiB
TypeScript
import type mongoose from "mongoose";
|
|
import type { GarnetClient } from "@common/garnet";
|
|
import type { LlmClient } from "@llm/llm-client";
|
|
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 { LlmReranker } from "./reranker.js";
|
|
import type {
|
|
LongTermMemory,
|
|
MemoryService,
|
|
SearchOptions,
|
|
SessionMemory,
|
|
WorkingMemory,
|
|
} from "./memory-types.js";
|
|
|
|
interface CreateMemoryServiceOptions {
|
|
garnetClient: GarnetClient;
|
|
llmClient?: Pick<LlmClient, "chatCompletion"> | undefined;
|
|
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(),
|
|
options.llmClient ? new LlmReranker(options.llmClient) : undefined,
|
|
)
|
|
: new NoopLongTermMemory(options.garnetClient);
|
|
|
|
return new DefaultMemoryService(session, longTerm);
|
|
}
|