feat: add wechat publish recovery and conversation memory
This commit is contained in:
+73
-1
@@ -266,7 +266,17 @@ export async function buildVisionPayload({
|
||||
};
|
||||
}
|
||||
|
||||
export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth, llmProviderService, localFetchAsset, subscriptionService, sessionSnapshotService }) {
|
||||
export function createTkmindProxy({
|
||||
apiTarget,
|
||||
apiTargets,
|
||||
apiSecret,
|
||||
userAuth,
|
||||
llmProviderService,
|
||||
localFetchAsset,
|
||||
subscriptionService,
|
||||
sessionSnapshotService,
|
||||
conversationMemoryService,
|
||||
}) {
|
||||
const targets = apiTargets?.length ? apiTargets : apiTarget ? [apiTarget] : [];
|
||||
const primaryTarget = targets[0] ?? apiTarget ?? '';
|
||||
let rrIdx = 0;
|
||||
@@ -345,6 +355,51 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
return primaryTarget;
|
||||
}
|
||||
|
||||
async function startSessionForUser(
|
||||
userId,
|
||||
{
|
||||
workingDir,
|
||||
sessionPolicy = null,
|
||||
recipe = null,
|
||||
} = {},
|
||||
) {
|
||||
const startTarget = await pickTarget();
|
||||
const upstream = await apiFetch(startTarget, apiSecret, '/agent/start', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
...(workingDir ? { working_dir: workingDir } : {}),
|
||||
enable_context_memory: sessionPolicy?.enableContextMemory,
|
||||
...(sessionPolicy?.extensionOverrides
|
||||
? { extension_overrides: sessionPolicy.extensionOverrides }
|
||||
: {}),
|
||||
...(recipe ? { recipe } : {}),
|
||||
}),
|
||||
});
|
||||
const text = await upstream.text();
|
||||
if (!upstream.ok) {
|
||||
throw new Error(text || `创建会话失败 (${upstream.status})`);
|
||||
}
|
||||
const session = JSON.parse(text);
|
||||
if (!session?.id) {
|
||||
throw new Error('创建会话失败:缺少 session id');
|
||||
}
|
||||
await userAuth.registerAgentSession(userId, session.id, startTarget);
|
||||
if (sessionPolicy?.gooseMode) {
|
||||
const modeRes = await apiFetch(startTarget, apiSecret, '/agent/update_session', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
session_id: session.id,
|
||||
goose_mode: sessionPolicy.gooseMode,
|
||||
}),
|
||||
});
|
||||
if (!modeRes.ok) {
|
||||
const modeText = await modeRes.text().catch(() => '');
|
||||
throw new Error(modeText || `设置会话模式失败 (${modeRes.status})`);
|
||||
}
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
async function resolveTarget(sessionId) {
|
||||
if (targets.length <= 1 || !sessionId) return primaryTarget;
|
||||
try {
|
||||
@@ -420,6 +475,9 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
const workingDir = await userAuth.resolveWorkingDir(userId);
|
||||
const sessionPolicy = await userAuth.getAgentSessionPolicy(userId);
|
||||
const publishLayout = await userAuth.getUserPublishLayout(userId);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService.listMemories(userId, { limit: 40 }).catch(() => [])
|
||||
: [];
|
||||
await reconcileAgentSession(
|
||||
(pathname, init) => apiFetch(target, apiSecret, pathname, init),
|
||||
sessionId,
|
||||
@@ -428,6 +486,7 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
sessionPolicy,
|
||||
sandboxConstraints: publishLayout?.constraints ?? null,
|
||||
tolerateInvalidWorkingDir: true,
|
||||
userMemories,
|
||||
userContext: publishLayout
|
||||
? {
|
||||
userId,
|
||||
@@ -521,12 +580,18 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
}
|
||||
}
|
||||
const publishLayout = await userAuth.getUserPublishLayout(req.currentUser.id);
|
||||
const userMemories = conversationMemoryService?.listMemories
|
||||
? await conversationMemoryService
|
||||
.listMemories(req.currentUser.id, { limit: 40 })
|
||||
.catch(() => [])
|
||||
: [];
|
||||
const api = (pathname, init) => apiFetch(startTarget, apiSecret, pathname, init);
|
||||
try {
|
||||
await reconcileAgentSession(api, session.id, {
|
||||
workingDir,
|
||||
sessionPolicy,
|
||||
sandboxConstraints: publishLayout?.constraints ?? null,
|
||||
userMemories,
|
||||
userContext: publishLayout
|
||||
? {
|
||||
userId: req.currentUser.id,
|
||||
@@ -587,6 +652,11 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
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(() => [])
|
||||
: [];
|
||||
try {
|
||||
await reconcileAgentSession(
|
||||
(pathname, init) => apiFetch(resumeTarget, apiSecret, pathname, init),
|
||||
@@ -596,6 +666,7 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
sessionPolicy,
|
||||
sandboxConstraints: publishLayout?.constraints ?? null,
|
||||
tolerateInvalidWorkingDir: true,
|
||||
userMemories,
|
||||
userContext: publishLayout
|
||||
? {
|
||||
userId: req.currentUser.id,
|
||||
@@ -911,6 +982,7 @@ export function createTkmindProxy({ apiTarget, apiTargets, apiSecret, userAuth,
|
||||
proxyFallback,
|
||||
proxySessionEvents,
|
||||
resolveTarget,
|
||||
startSessionForUser,
|
||||
apiFetch: async (pathname, init) => apiFetch(await pickTarget(), apiSecret, pathname, init),
|
||||
apiFetchTo: (target, pathname, init) => apiFetch(target, apiSecret, pathname, init),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user