This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
import { listMemories, getProfileSummary } from "./ai-memory";
|
||||
import { MEMORY_CATEGORIES, type MemoryCategory } from "@adventureos/shared";
|
||||
import { extractMemoryCandidates } from "./memory-extraction";
|
||||
import { buildMemoryActionReply, performChatMemoryAction } from "./chat-memory-actions";
|
||||
|
||||
export async function listChatSessions(userId: string) {
|
||||
return db
|
||||
@@ -67,6 +68,9 @@ export async function sendChatMessage(userId: string, sessionId: string, content
|
||||
})
|
||||
.returning();
|
||||
|
||||
const memoryAction = await performChatMemoryAction(userId, sessionId, userMsg, messages);
|
||||
const authoritativeMemoryReply = buildMemoryActionReply(memoryAction);
|
||||
|
||||
const availability = await getAiAvailability(userId);
|
||||
const ctx = await buildMentorContext(userId, {
|
||||
userMessage: content,
|
||||
@@ -89,7 +93,9 @@ export async function sendChatMessage(userId: string, sessionId: string, content
|
||||
let offline = false;
|
||||
const memoryIdsUsed = ctx.memoryIds;
|
||||
|
||||
if (!availability.canUse || !availability.online) {
|
||||
if (authoritativeMemoryReply) {
|
||||
reply = authoritativeMemoryReply;
|
||||
} else if (!availability.canUse || !availability.online) {
|
||||
offline = true;
|
||||
reply = buildOfflineReply(content, ctx);
|
||||
} else {
|
||||
@@ -102,6 +108,7 @@ export async function sendChatMessage(userId: string, sessionId: string, content
|
||||
userMessage: content,
|
||||
contextBlock,
|
||||
history,
|
||||
memoryAction: JSON.stringify(memoryAction),
|
||||
});
|
||||
|
||||
reply =
|
||||
@@ -116,21 +123,33 @@ export async function sendChatMessage(userId: string, sessionId: string, content
|
||||
sessionId,
|
||||
role: "assistant",
|
||||
content: reply,
|
||||
metadata: { memoryIdsUsed, offline },
|
||||
metadata: { memoryIdsUsed, offline, memoryAction },
|
||||
})
|
||||
.returning();
|
||||
|
||||
const memorySuggestions = await extractMemoryCandidates(userId, "chat", content, {
|
||||
type: "ai_chat",
|
||||
id: userMsg.id,
|
||||
});
|
||||
const memorySuggestions =
|
||||
memoryAction.intent === "ordinary_message"
|
||||
? await extractMemoryCandidates(userId, "chat", content, {
|
||||
type: "ai_chat",
|
||||
id: userMsg.id,
|
||||
})
|
||||
: memoryAction.type === "memory_suggestion_created"
|
||||
? [
|
||||
{
|
||||
id: memoryAction.id,
|
||||
title: memoryAction.title,
|
||||
category: memoryAction.category,
|
||||
status: memoryAction.status,
|
||||
},
|
||||
]
|
||||
: [];
|
||||
|
||||
await db
|
||||
.update(aiChatSessions)
|
||||
.set({ updatedAt: new Date(), title: session.title === "New conversation" ? truncateTitle(content) : session.title })
|
||||
.where(eq(aiChatSessions.id, sessionId));
|
||||
|
||||
return { message: assistantMsg, reply, memoryIdsUsed, offline, memorySuggestions };
|
||||
return { message: assistantMsg, reply, memoryIdsUsed, offline, memorySuggestions, memoryAction };
|
||||
}
|
||||
|
||||
function truncateTitle(text: string): string {
|
||||
|
||||
Reference in New Issue
Block a user