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

+84 -17
View File
@@ -1,4 +1,6 @@
import { z } from "zod";
import { getRagEnabled } from "../../config.js";
import { getRequestLogger } from "../../observability/context.js";
import {
DEADLINE_NEAR_THRESHOLD_MIN,
DISCORD_GUIDE_TEXT,
@@ -10,23 +12,34 @@ import {
import type { Tool } from "../tool.js";
const argsSchema = z.object({
topic: z.enum([
"age",
"tier-codes",
"discord-link",
"heart-rule",
"nickname-format",
"deadline",
]),
topic: z.string().min(1).describe("검색할 온보딩 규칙 주제"),
topK: z.number().int().min(1).max(10).optional(),
});
const resultSchema = z.object({
topic: argsSchema.shape.topic,
text: z.string(),
source: z.literal("constants"),
source: z.enum(["constants", "knowledge_documents"]),
matches: z
.array(
z.object({
id: z.string(),
score: z.number(),
text: z.string(),
}),
)
.optional(),
});
const TOPIC_TEXT: Record<z.infer<typeof argsSchema>["topic"], string> = {
type LegacyTopic =
| "age"
| "tier-codes"
| "discord-link"
| "heart-rule"
| "nickname-format"
| "deadline";
const TOPIC_TEXT: Record<LegacyTopic, string> = {
age: `가입 가능 연령은 ${MIN_AGE}세 이상이에요.`,
"tier-codes": `사용 가능한 티어 코드는 ${TIER_CODES_TEXT}예요.`,
"discord-link": DISCORD_GUIDE_TEXT,
@@ -35,20 +48,74 @@ const TOPIC_TEXT: Record<z.infer<typeof argsSchema>["topic"], string> = {
deadline: `데드라인이 ${DEADLINE_NEAR_THRESHOLD_MIN}분 이하로 남으면 마감이 임박한 상태로 안내해요.`,
};
function resolveLegacyTopic(topic: string): LegacyTopic | null {
if (
topic === "age" ||
topic === "tier-codes" ||
topic === "discord-link" ||
topic === "heart-rule" ||
topic === "nickname-format" ||
topic === "deadline"
) {
return topic;
}
if (/(나이|연령|age)/iu.test(topic)) return "age";
if (/(티어|tier|랭크)/iu.test(topic)) return "tier-codes";
if (/(디스코드|디코|discord)/iu.test(topic)) return "discord-link";
if (/(하트|인증|사진|heart)/iu.test(topic)) return "heart-rule";
if (/(닉네임|형식|이름|nickname)/iu.test(topic)) return "nickname-format";
if (/(마감|데드라인|deadline|시간)/iu.test(topic)) return "deadline";
return null;
}
function legacyLookupCommunityRule(topic: string): z.infer<typeof resultSchema> {
const resolved = resolveLegacyTopic(topic);
return {
topic,
text: resolved
? TOPIC_TEXT[resolved]
: Object.values(TOPIC_TEXT).join("\n"),
source: "constants",
};
}
export const lookupCommunityRuleTool: Tool<
z.infer<typeof argsSchema>,
z.infer<typeof resultSchema>
> = {
name: "lookup-community-rule",
description:
"나이 제한, 닉네임 형식, 티어 코드, 하트 인증 규칙, 디스코드 안내 같은 커뮤니티 규칙을 상수에서 조회한다.",
"나이 제한, 닉네임 형식, 티어 코드, 하트 인증 규칙, 디스코드 안내 같은 커뮤니티 규칙을 조회한다.",
argsSchema,
resultSchema,
execute(args) {
return Promise.resolve({
topic: args.topic,
text: TOPIC_TEXT[args.topic],
source: "constants",
});
async execute(args, ctx) {
if (ctx.memoryService && getRagEnabled()) {
try {
const docs = await ctx.memoryService.longTerm.search(args.topic, {
category: "onboarding-step",
skill: "onboarding",
topK: args.topK ?? 3,
});
if (docs.length > 0) {
return {
topic: args.topic,
text: docs.map((doc) => doc.text).join("\n\n"),
source: "knowledge_documents",
matches: docs.map((doc) => ({
id: doc.id,
score: doc.score,
text: doc.text,
})),
};
}
} catch (error) {
getRequestLogger().warn(
{ err: error, topic: args.topic },
"[lookup-community-rule] Knowledge retrieval failed; using constants",
);
}
}
return legacyLookupCommunityRule(args.topic);
},
};