init
This commit is contained in:
commit
7fa293c9e8
59 files changed
+15526
No files matched your search
@@ -0,0 +1,93 @@
|
||||
import type { ConversationMessage } from "@agent/types";
|
||||
import type { GarnetClient } from "@common/garnet";
|
||||
import {
|
||||
getBackgroundStreamMaxLen,
|
||||
getConversationTtlSeconds,
|
||||
getConversationSummaryTtlSeconds,
|
||||
getMaxConversationMessages,
|
||||
} from "../config.js";
|
||||
|
||||
const BACKGROUND_STREAM_KEY = "lolympus-agent:background";
|
||||
|
||||
export function getConversationKey(chatroomId: string, userId: string): string {
|
||||
return `lolympus-agent:conv:${chatroomId}:${userId}`;
|
||||
}
|
||||
|
||||
export function getConversationSummaryKey(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
): string {
|
||||
return `lolympus-agent:summary:${chatroomId}:${userId}`;
|
||||
}
|
||||
|
||||
export class ConversationStore {
|
||||
constructor(private readonly garnetClient: GarnetClient) {}
|
||||
|
||||
async getHistory(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
): Promise<ConversationMessage[]> {
|
||||
const key = getConversationKey(chatroomId, userId);
|
||||
const messages =
|
||||
await this.garnetClient.getJson<ConversationMessage[]>(key);
|
||||
return messages ?? [];
|
||||
}
|
||||
|
||||
async appendMessage(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
message: ConversationMessage,
|
||||
): Promise<void> {
|
||||
const key = getConversationKey(chatroomId, userId);
|
||||
const messages = await this.getHistory(chatroomId, userId);
|
||||
|
||||
messages.push(message);
|
||||
|
||||
const maxMessages = getMaxConversationMessages();
|
||||
const trimmed =
|
||||
messages.length > maxMessages
|
||||
? messages.slice(messages.length - maxMessages)
|
||||
: messages;
|
||||
|
||||
await this.garnetClient.setJson(key, trimmed, getConversationTtlSeconds());
|
||||
|
||||
if (trimmed.length > 5) {
|
||||
await this.garnetClient.appendStream(
|
||||
BACKGROUND_STREAM_KEY,
|
||||
{
|
||||
type: "summarize",
|
||||
chatroomId,
|
||||
userId,
|
||||
},
|
||||
{
|
||||
maxLen: getBackgroundStreamMaxLen(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async getSummary(chatroomId: string, userId: string): Promise<string | null> {
|
||||
const key = getConversationSummaryKey(chatroomId, userId);
|
||||
return this.garnetClient.getString(key);
|
||||
}
|
||||
|
||||
async setSummary(
|
||||
chatroomId: string,
|
||||
userId: string,
|
||||
summary: string,
|
||||
): Promise<void> {
|
||||
const key = getConversationSummaryKey(chatroomId, userId);
|
||||
await this.garnetClient.setString(
|
||||
key,
|
||||
summary,
|
||||
getConversationSummaryTtlSeconds(),
|
||||
);
|
||||
}
|
||||
|
||||
async clear(chatroomId: string, userId: string): Promise<void> {
|
||||
const key = getConversationKey(chatroomId, userId);
|
||||
const summaryKey = getConversationSummaryKey(chatroomId, userId);
|
||||
await this.garnetClient.delete(key);
|
||||
await this.garnetClient.delete(summaryKey);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export { ConversationStore } from "./conversation-store.js";
|
||||
export { summarizeConversation } from "./summarizer.js";
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { ConversationMessage } from "@agent/types";
|
||||
import type { LlmClient } from "@llm/llm-client";
|
||||
|
||||
function formatHistory(history: ConversationMessage[]): string {
|
||||
return history.map((message) => `${message.role}: ${message.content}`).join("\n");
|
||||
}
|
||||
|
||||
export async function summarizeConversation(
|
||||
llmClient: LlmClient,
|
||||
history: ConversationMessage[],
|
||||
existingSummary?: string | undefined,
|
||||
): Promise<string> {
|
||||
if (!llmClient.hasEndpoints() || history.length === 0) {
|
||||
return existingSummary ?? "";
|
||||
}
|
||||
|
||||
const response = await llmClient.chatCompletion(
|
||||
[
|
||||
{
|
||||
role: "system",
|
||||
content:
|
||||
"다음 대화를 3문장 이내의 한국어 메모로 요약해. 온보딩 진행상태, 아직 남은 질문, 톤 관련 단서만 남겨.",
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
existingSummary ? `[기존 요약]\n${existingSummary}` : "",
|
||||
"[최근 대화]",
|
||||
formatHistory(history),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n\n"),
|
||||
},
|
||||
],
|
||||
{
|
||||
maxTokens: 256,
|
||||
temperature: 0.2,
|
||||
},
|
||||
);
|
||||
|
||||
return response?.content.trim() ?? existingSummary ?? "";
|
||||
}
|
||||
Reference in new issue
Block a user