Merge Memory V2 runtime facade
This commit is contained in:
+62
-18
@@ -823,6 +823,7 @@ export function createTkmindProxy({
|
||||
subscriptionService,
|
||||
sessionSnapshotService,
|
||||
conversationMemoryService,
|
||||
memoryV2,
|
||||
}) {
|
||||
const targets = apiTargets?.length ? apiTargets : apiTarget ? [apiTarget] : [];
|
||||
const primaryTarget = targets[0] ?? apiTarget ?? '';
|
||||
@@ -1025,10 +1026,48 @@ export function createTkmindProxy({
|
||||
aiderTimeoutMs: Number(process.env.MEMIND_AIDER_TIMEOUT_MS ?? 600_000),
|
||||
openhandsTimeoutMs: Number(process.env.MEMIND_OPENHANDS_TIMEOUT_MS ?? 900_000),
|
||||
},
|
||||
memory: memoryV2?.getStatus
|
||||
? await Promise.resolve(memoryV2.getStatus())
|
||||
: {
|
||||
enabled: Boolean(conversationMemoryService?.isEnabled?.()),
|
||||
backend: 'legacy',
|
||||
selectedBackend: conversationMemoryService ? 'legacy-conversation-memory' : null,
|
||||
profileEnabled: false,
|
||||
eventLogEnabled: Boolean(conversationMemoryService?.isEnabled?.()),
|
||||
vectorEnabled: false,
|
||||
failOpen: true,
|
||||
backends: [
|
||||
{
|
||||
name: 'legacy-conversation-memory',
|
||||
available: Boolean(conversationMemoryService),
|
||||
supports: {
|
||||
resolve: Boolean(conversationMemoryService?.listMemories),
|
||||
write: Boolean(conversationMemoryService?.saveAndAnalyze),
|
||||
compact: Boolean(conversationMemoryService?.analyzeUser),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
targets: targetStatuses,
|
||||
};
|
||||
}
|
||||
|
||||
async function resolveUserMemories(userId, { sessionId = null, query = null, limit = 40 } = {}) {
|
||||
if (!userId) return [];
|
||||
if (memoryV2?.resolve) {
|
||||
const resolved = await memoryV2.resolve({
|
||||
userId,
|
||||
sessionId,
|
||||
query,
|
||||
limit,
|
||||
}).catch(() => null);
|
||||
return Array.isArray(resolved?.memories) ? resolved.memories : [];
|
||||
}
|
||||
return conversationMemoryService?.listMemories
|
||||
? conversationMemoryService.listMemories(userId, { limit }).catch(() => [])
|
||||
: [];
|
||||
}
|
||||
|
||||
async function startSessionForUser(
|
||||
userId,
|
||||
{
|
||||
@@ -1075,9 +1114,7 @@ export function createTkmindProxy({
|
||||
}
|
||||
}
|
||||
const publishLayout = await userAuth.getUserPublishLayout(userId);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService.listMemories(userId, { limit: 40 }).catch(() => [])
|
||||
: [];
|
||||
const userMemories = await resolveUserMemories(userId, { sessionId: session.id, limit: 40 });
|
||||
await reconcileAgentSession(
|
||||
(pathname, init) => apiFetch(startTarget, apiSecret, pathname, init),
|
||||
session.id,
|
||||
@@ -1178,15 +1215,21 @@ export function createTkmindProxy({
|
||||
return userAuth.getAgentSessionPolicy(userId);
|
||||
}
|
||||
|
||||
async function reconcileSessionPolicyForUser(userId, sessionId, { toolMode = 'chat' } = {}) {
|
||||
async function reconcileSessionPolicyForUser(
|
||||
userId,
|
||||
sessionId,
|
||||
{ toolMode = 'chat', query = null } = {},
|
||||
) {
|
||||
if (!userId || !sessionId) return;
|
||||
const target = await resolveTarget(sessionId);
|
||||
const workingDir = await userAuth.resolveWorkingDir(userId);
|
||||
const sessionPolicy = await getSessionPolicyForToolMode(userId, toolMode);
|
||||
const publishLayout = await userAuth.getUserPublishLayout(userId);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService.listMemories(userId, { limit: 40 }).catch(() => [])
|
||||
: [];
|
||||
const userMemories = await resolveUserMemories(userId, {
|
||||
sessionId,
|
||||
query,
|
||||
limit: 40,
|
||||
});
|
||||
await reconcileAgentSession(
|
||||
(pathname, init) => apiFetch(target, apiSecret, pathname, init),
|
||||
sessionId,
|
||||
@@ -1226,7 +1269,10 @@ export function createTkmindProxy({
|
||||
throw err;
|
||||
}
|
||||
|
||||
await reconcileSessionPolicyForUser(userId, sessionId, { toolMode });
|
||||
await reconcileSessionPolicyForUser(userId, sessionId, {
|
||||
toolMode,
|
||||
query: firstUserText(userMessage),
|
||||
});
|
||||
await applySessionLlmProvider(sessionId);
|
||||
|
||||
const user = await userAuth.getUserById(userId);
|
||||
@@ -1358,11 +1404,10 @@ export function createTkmindProxy({
|
||||
}
|
||||
}
|
||||
const publishLayout = await userAuth.getUserPublishLayout(req.currentUser.id);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService
|
||||
.listMemories(req.currentUser.id, { limit: 40 })
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const userMemories = await resolveUserMemories(req.currentUser.id, {
|
||||
sessionId: session.id,
|
||||
limit: 40,
|
||||
});
|
||||
const api = (pathname, init) => apiFetch(startTarget, apiSecret, pathname, init);
|
||||
try {
|
||||
await reconcileAgentSession(api, session.id, {
|
||||
@@ -1430,11 +1475,10 @@ export function createTkmindProxy({
|
||||
const workingDir = await userAuth.resolveWorkingDir(req.currentUser.id);
|
||||
const sessionPolicy = await userAuth.getAgentSessionPolicy(req.currentUser.id);
|
||||
const publishLayout = await userAuth.getUserPublishLayout(req.currentUser.id);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService
|
||||
.listMemories(req.currentUser.id, { limit: 40 })
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const userMemories = await resolveUserMemories(req.currentUser.id, {
|
||||
sessionId,
|
||||
limit: 40,
|
||||
});
|
||||
try {
|
||||
await reconcileAgentSession(
|
||||
(pathname, init) => apiFetch(resumeTarget, apiSecret, pathname, init),
|
||||
|
||||
Reference in New Issue
Block a user