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

+1 -1
View File
@@ -5,5 +5,5 @@ import { ModerationSkill } from "./skill.js";
export { ModerationSkill } from "./skill.js";
export function createModerationSkill(deps: SkillDeps): Skill {
return new ModerationSkill(deps.llmClient);
return new ModerationSkill(deps.llmClient, deps.memoryService);
}
+41 -2
View File
@@ -5,9 +5,12 @@ import type {
} from "@agent/types";
import type { LlmClient } from "@llm/llm-client";
import type { ChatMessage } from "@llm/types";
import type { MemoryService, RetrievedDocument } from "../../memory/memory-types.js";
import { getRequestLogger } from "@obs/context";
import type { Skill } from "@skills/skill";
import type { SkillCapabilityCard } from "@skills/skill-loader";
import { lookupRuleTool } from "@tools/moderation/lookup-rule";
import { getRagEnabled } from "../../config.js";
import { classifyModerationIntent, type ModerationIntent } from "./intents.js";
import { MODERATION_PROMPT_DATA } from "./prompt-data.js";
import { createModerationToolRegistry } from "./tools.js";
@@ -137,18 +140,50 @@ function resolveRuleTopic(message: string): RuleTopic | null {
async function lookupReferenceText(
intent: ModerationIntent,
message: string,
memoryService?: MemoryService,
): Promise<string | undefined> {
if (intent.kind !== "rule-question") {
return undefined;
}
if (memoryService && getRagEnabled()) {
try {
const docs = await memoryService.longTerm.search(message, {
category: "rule",
skill: "moderation",
topK: 3,
});
if (docs.length > 0) {
return formatRetrievedReference(docs);
}
} catch (error) {
getRequestLogger().warn(
{ err: error },
"[ModerationSkill] Knowledge retrieval failed; using local rules",
);
}
}
const topic = resolveRuleTopic(message);
if (!topic) {
return undefined;
}
const result = await lookupRuleTool.execute({ topic }, {});
const result = await lookupRuleTool.execute(
{ topic },
{ memoryService },
);
return result.text;
}
function formatRetrievedReference(docs: RetrievedDocument[]): string {
return docs
.map((doc, index) => {
const score = Number.isFinite(doc.score) ? doc.score.toFixed(3) : "0.000";
return [`#${index + 1} score=${score}`, doc.text.trim()].join("\n");
})
.join("\n\n");
}
function templateForIntent(intent: ModerationIntent): string {
switch (intent.kind) {
case "appeal":
@@ -169,7 +204,10 @@ export class ModerationSkill implements Skill {
readonly priority = 50;
private readonly toolRegistry = createModerationToolRegistry();
constructor(private readonly llmClient: LlmClient) {}
constructor(
private readonly llmClient: LlmClient,
private readonly memoryService?: MemoryService | undefined,
) {}
canHandle(request: AgentRequest): boolean {
return request.skill === this.name;
@@ -187,6 +225,7 @@ export class ModerationSkill implements Skill {
const referenceText = await lookupReferenceText(
classified.intent,
request.userMessage ?? "",
this.memoryService,
);
const metadata = {
intent: classified.intent.kind,
+1
View File
@@ -15,5 +15,6 @@ export function createOnboardingSkill(deps: SkillDeps): Skill {
deps.roomContextService,
deps.shadowEvalBus,
deps.natsClient,
deps.memoryService,
);
}
+22
View File
@@ -123,6 +123,27 @@ function formatUserProfile(profile: UserProfile | null | undefined): string {
].join('\n');
}
function formatRetrievedKnowledge(context: PromptContext): string[] {
const docs = context.retrievedKnowledge ?? [];
if (docs.length === 0) {
return [];
}
return [
'',
'[reference]',
docs
.map((doc, index) => {
const score = Number.isFinite(doc.score) ? doc.score.toFixed(3) : '0.000';
return [
`#${index + 1} (${doc.category}, score=${score})`,
doc.text.trim(),
].join('\n');
})
.join('\n\n'),
];
}
export function personaLayer(): string {
return ONBOARDING_PROMPT_DATA.persona;
}
@@ -201,5 +222,6 @@ export function contextLayer(context: PromptContext): string {
formatUserProfile(context.userProfile),
]
: []),
...formatRetrievedKnowledge(context),
].join('\n');
}
@@ -2,12 +2,14 @@ import type { AgentRequest, ConversationMessage } from '@agent/types';
import type { GarnetClient } from '@common/garnet';
import { getConversationSummaryKey } from '@conv/conversation-store';
import type { ChatMessage } from '@llm/types';
import type { MemoryService } from '../../../memory/memory-types.js';
import { getRequestLogger } from '@obs/context';
import type { RoomContextService } from '@room/room-context-service';
import { meetsTrustTier } from '../../../eval/governance/trust.js';
import {
getAgentTrustMinTier,
getAgentUserMemoryEnabled,
getRagEnabled,
getPromptVariantId,
} from '../../../config.js';
import {
@@ -22,6 +24,7 @@ import type { OnboardingSession, PromptContext } from '../types.js';
interface MessageBuilderDeps {
blackboardStore: OnboardingBlackboardStore;
garnetClient: GarnetClient;
memoryService?: MemoryService | undefined;
roomContextService?: RoomContextService | undefined;
userMemoryStore: UserMemoryStore;
}
@@ -67,6 +70,32 @@ async function loadConversationSummary(
return (await garnetClient.getString(key)) ?? undefined;
}
function shouldRetrieve(derived: DerivedState): boolean {
return (
getRagEnabled() &&
(
derived.intent.kind === 'help-how' ||
derived.intent.kind === 'help-where' ||
derived.intent.kind === 'nickname-question' ||
derived.intent.kind === 'discord-question' ||
derived.intent.kind === 'age-question'
)
);
}
function buildRetrievalQuery(
request: AgentRequest,
derived: DerivedState,
): string {
return [
request.userMessage?.trim(),
derived.intent.kind,
derived.phase.kind,
]
.filter(Boolean)
.join('\n');
}
export async function buildLlmMessages(
deps: MessageBuilderDeps,
input: MessageBuilderInput,
@@ -85,7 +114,34 @@ export async function buildLlmMessages(
return undefined;
})
: Promise.resolve(undefined);
const userProfilePromise = getAgentUserMemoryEnabled()
const workingMemoryPromise = deps.memoryService
? deps.memoryService
.assembleWorkingMemory({
skill: 'onboarding',
chatroomId: request.chatroomId,
userId: request.userId,
includeBlackboard: true,
includeProfile: getAgentUserMemoryEnabled(),
retrieval: shouldRetrieve(derived)
? {
query: buildRetrievalQuery(request, derived),
options: {
category: 'onboarding-step',
skill: 'onboarding',
topK: 2,
},
}
: undefined,
})
.catch((error) => {
logger.warn(
{ err: error },
'[OnboardingSkill] Failed to assemble working memory',
);
return null;
})
: Promise.resolve(null);
const legacyUserProfilePromise = getAgentUserMemoryEnabled()
? deps.userMemoryStore.load(request.userId).catch((error) => {
logger.warn(
{ err: error },
@@ -94,20 +150,40 @@ export async function buildLlmMessages(
return null;
})
: Promise.resolve(null);
const [blackboard, summary, roomContext, userProfile] = await Promise.all([
deps.blackboardStore.load(request.chatroomId, request.userId),
loadConversationSummary(deps.garnetClient, request.chatroomId, request.userId),
const [
workingMemory,
legacyBlackboard,
legacySummary,
roomContext,
legacyUserProfile,
] = await Promise.all([
workingMemoryPromise,
deps.memoryService
? Promise.resolve(undefined)
: deps.blackboardStore.load(request.chatroomId, request.userId),
deps.memoryService
? Promise.resolve(undefined)
: loadConversationSummary(deps.garnetClient, request.chatroomId, request.userId),
roomContextPromise,
userProfilePromise,
deps.memoryService ? Promise.resolve(null) : legacyUserProfilePromise,
]);
const blackboard = workingMemory?.blackboard ?? legacyBlackboard;
if (!blackboard) {
throw new Error('Failed to load onboarding blackboard');
}
const context: PromptContext = {
blackboard,
conversationHistory,
conversationHistory:
workingMemory && workingMemory.conversationHistory.length > 0
? workingMemory.conversationHistory
: conversationHistory,
roomContext,
summary,
retrievedKnowledge: workingMemory?.retrievedKnowledge,
summary: workingMemory?.summary ?? legacySummary,
userMessage: buildPromptUserMessage(request, derived),
userProfile,
userProfile: workingMemory?.userProfile ?? legacyUserProfile,
};
const builder = PromptBuilder.forDerived(derived, session, context);
+12 -2
View File
@@ -1,6 +1,7 @@
import type { AgentRequest, AgentResponse, ConversationMessage } from '@agent/types';
import type { GarnetClient } from '@common/garnet';
import type { NatsClient } from '@common/nats';
import type { MemoryService } from '../../memory/memory-types.js';
import { getContext, getRequestLogger } from '@obs/context';
import type { LlmClient } from '@llm/llm-client';
import type { RoomContextService } from '@room/room-context-service';
@@ -112,10 +113,17 @@ export class OnboardingSkill implements Skill {
private readonly roomContextService?: RoomContextService,
shadowEvalPublisher?: ShadowEvalPublisher,
private readonly natsClient?: Pick<NatsClient, 'request'>,
private readonly memoryService?: MemoryService,
) {
this.classifier = new OnboardingIntentClassifier(llmClient);
this.blackboardStore = new OnboardingBlackboardStore(garnetClient);
this.userMemoryStore = new UserMemoryStore(garnetClient);
this.blackboardStore = new OnboardingBlackboardStore(
garnetClient,
memoryService?.session,
);
this.userMemoryStore = new UserMemoryStore(
garnetClient,
memoryService?.longTerm,
);
this.orchestrator = new OnboardingOrchestrator(llmClient);
this.shadowEvalPublisher = shadowEvalPublisher ?? new ShadowEvalBus(garnetClient);
this.toolRegistry = createOnboardingToolRegistry();
@@ -381,6 +389,7 @@ export class OnboardingSkill implements Skill {
{
blackboardStore: this.blackboardStore,
garnetClient: this.garnetClient,
memoryService: this.memoryService,
roomContextService: this.roomContextService,
userMemoryStore: this.userMemoryStore,
},
@@ -474,6 +483,7 @@ export class OnboardingSkill implements Skill {
userId: request.userId,
session,
garnetClient: this.garnetClient,
memoryService: this.memoryService,
roomContextService: this.roomContextService,
toolRegistry: this.toolRegistry,
};
+24 -1
View File
@@ -1,4 +1,5 @@
import type { GarnetClient } from "@common/garnet";
import type { SessionMemory } from "../../../memory/memory-types.js";
import { getBlackboardTtlSeconds } from "../../../config.js";
import type { Blackboard } from "../types.js";
@@ -37,9 +38,21 @@ function mergeBlackboard(
}
export class OnboardingBlackboardStore {
constructor(private readonly garnetClient: GarnetClient) {}
constructor(
private readonly garnetClient: GarnetClient,
private readonly sessionMemory?: SessionMemory | undefined,
) {}
async load(chatroomId: string, userId: string): Promise<Blackboard> {
if (this.sessionMemory) {
const saved = await this.sessionMemory.getBlackboard<Blackboard>(
"onboarding",
chatroomId,
userId,
);
return mergeBlackboard(saved, {});
}
const key = getBlackboardKey(chatroomId, userId);
const saved = await this.garnetClient.getJson<Blackboard>(key);
return mergeBlackboard(saved, {});
@@ -50,6 +63,16 @@ export class OnboardingBlackboardStore {
userId: string,
value: Blackboard,
): Promise<void> {
if (this.sessionMemory) {
await this.sessionMemory.setBlackboard(
"onboarding",
chatroomId,
userId,
value,
);
return;
}
const key = getBlackboardKey(chatroomId, userId);
await this.garnetClient.setJson(
key,
+14 -2
View File
@@ -4,6 +4,7 @@ import {
anonymizeText,
} from "@eval/anonymize";
import { getAgentUserMemoryTtlDays } from "../../../config.js";
import type { LongTermMemory } from "../../../memory/memory-types.js";
import type { DerivedState, Intent } from "./onboarding-state.js";
export type PreferredStyle = "concise" | "neutral" | "warm";
@@ -86,9 +87,16 @@ function emptyProfile(userId: string, now = nowIso()): UserProfile {
}
export class UserMemoryStore {
constructor(private readonly garnetClient: UserMemoryGarnetClient) {}
constructor(
private readonly garnetClient: UserMemoryGarnetClient,
private readonly longTermMemory?: LongTermMemory | undefined,
) {}
async load(userId: string): Promise<UserProfile | null> {
if (this.longTermMemory) {
return this.longTermMemory.loadUserProfile(userId);
}
return this.garnetClient.getJson<UserProfile>(profileKey(userId));
}
@@ -125,7 +133,11 @@ export class UserMemoryStore {
userId: current.userId,
};
await this.garnetClient.setJson(profileKey(userId), next, ttlSeconds());
if (this.longTermMemory) {
await this.longTermMemory.updateUserProfile(userId, next);
} else {
await this.garnetClient.setJson(profileKey(userId), next, ttlSeconds());
}
return next;
}
}
+2
View File
@@ -1,4 +1,5 @@
import type { ConversationMessage } from "@agent/types";
import type { RetrievedDocument } from "../../memory/memory-types.js";
import type { RoomMessage } from "@room/types";
import type { UserProfile } from "./state/user-memory.js";
@@ -27,6 +28,7 @@ export interface PromptContext {
blackboard: Blackboard;
conversationHistory: ConversationMessage[];
roomContext?: RoomMessage[] | undefined;
retrievedKnowledge?: RetrievedDocument[] | undefined;
summary?: string | undefined;
userMessage: string;
userProfile?: UserProfile | null | undefined;
+2
View File
@@ -2,6 +2,7 @@ import type { GarnetClient } from "@common/garnet";
import type { NatsClient } from "@common/nats";
import type { ShadowEvalBus } from "@eval/shadow-bus";
import type { LlmClient } from "@llm/llm-client";
import type { MemoryService } from "../memory/memory-types.js";
import type { RoomContextService } from "@room/room-context-service";
import type { Skill } from "./skill.js";
import type { SkillRegistry } from "./skill-loader.js";
@@ -9,6 +10,7 @@ import type { SkillRegistry } from "./skill-loader.js";
export interface SkillDeps {
garnetClient: GarnetClient;
llmClient: LlmClient;
memoryService?: MemoryService | undefined;
natsClient: NatsClient;
roomContextService?: RoomContextService | undefined;
shadowEvalBus: ShadowEvalBus;