import { createAgentCodeRunAdminConfigService } from '../agent-code-run-admin-config.mjs'; import { createManagedChatIntentRouter } from '../chat-intent-router.mjs'; import { createConversationMemoryService } from '../conversation-memory.mjs'; import { createDirectChatService } from '../direct-chat-service.mjs'; import { createEpisodicMemoryService } from '../episodic-memory.mjs'; import { createMemoryV2AdminConfigService } from '../memory-v2-admin-config.mjs'; import { createManagedMemoryV2Runtime } from '../memory-v2-runtime.mjs'; import { createSessionSnapshotService } from '../session-snapshot.mjs'; import { createSessionStreamStore } from '../session-stream-store.mjs'; import { isSessionStreamReplayEnabled } from '../session-stream.mjs'; import { createSkillRuntimeAdminConfigService } from '../skill-runtime-admin-config.mjs'; import { createSystemDisclosurePolicyService } from '../system-disclosure-policy.mjs'; import { createWechatScheduleLlmConfigService } from '../wechat-schedule-llm-config.mjs'; import { createWechatIntentRouterConfigService } from '../wechat-intent-router-config.mjs'; import { createManagedWechatIntentRouter } from '../wechat-intent-router.mjs'; export async function bootstrapPortalMemorySessionServices({ pool, h5Root, env = process.env, llmProviderService, userAuth, sessionAccess, logger = console, createMemoryV2AdminConfigServiceFn = createMemoryV2AdminConfigService, createSkillRuntimeAdminConfigServiceFn = createSkillRuntimeAdminConfigService, createSystemDisclosurePolicyServiceFn = createSystemDisclosurePolicyService, createAgentCodeRunAdminConfigServiceFn = createAgentCodeRunAdminConfigService, createWechatScheduleLlmConfigServiceFn = createWechatScheduleLlmConfigService, createWechatIntentRouterConfigServiceFn = createWechatIntentRouterConfigService, createManagedWechatIntentRouterFn = createManagedWechatIntentRouter, createConversationMemoryServiceFn = createConversationMemoryService, createManagedMemoryV2RuntimeFn = createManagedMemoryV2Runtime, createEpisodicMemoryServiceFn = createEpisodicMemoryService, createSessionSnapshotServiceFn = createSessionSnapshotService, isSessionStreamReplayEnabledFn = isSessionStreamReplayEnabled, createSessionStreamStoreFn = createSessionStreamStore, createDirectChatServiceFn = createDirectChatService, createManagedChatIntentRouterFn = createManagedChatIntentRouter, } = {}) { if ( !pool || !h5Root || !llmProviderService || !userAuth || !sessionAccess ) { throw new Error( 'bootstrapPortalMemorySessionServices requires memory and session dependencies', ); } const memoryV2ConfigService = createMemoryV2AdminConfigServiceFn(pool); const skillRuntimeConfigService = createSkillRuntimeAdminConfigServiceFn(pool, { h5Root, }); const systemDisclosurePolicyService = createSystemDisclosurePolicyServiceFn(pool); await systemDisclosurePolicyService.initialize(); const agentCodeRunPolicyService = createAgentCodeRunAdminConfigServiceFn(pool, { env }); const wechatScheduleLlmConfigService = createWechatScheduleLlmConfigServiceFn(pool); const wechatIntentRouterConfigService = createWechatIntentRouterConfigServiceFn(pool, { env }); const wechatIntentRouter = createManagedWechatIntentRouterFn({ llmProviderService, configService: wechatIntentRouterConfigService, env, logger, }); const getEffectiveEnv = async () => { const state = await memoryV2ConfigService .getRuntimeState() .catch(() => null); return { ...env, ...(state?.overrides ?? {}), }; }; const conversationMemoryService = createConversationMemoryServiceFn(pool, { llmProviderService, getEffectiveEnv, }); const memoryV2 = await createManagedMemoryV2RuntimeFn({ legacyMemoryService: conversationMemoryService, configService: memoryV2ConfigService, mysqlPool: pool, }); const episodicMemoryService = createEpisodicMemoryServiceFn(pool, { getEffectiveEnv, logger, }); await episodicMemoryService .ensureSchema() .catch((error) => { logger.warn( '[episodic-memory] schema setup degraded; snapshot fallback remains available:', error instanceof Error ? error.message : error, ); }); const sessionSnapshotService = createSessionSnapshotServiceFn(pool, { conversationMemoryService, memoryV2, episodicMemoryService, }); const sessionStreamStore = isSessionStreamReplayEnabledFn() ? createSessionStreamStoreFn({ pool }) : null; const directChatService = createDirectChatServiceFn({ userAuth, sessionAccess, llmProviderService, sessionSnapshotService, memoryV2, conversationMemoryService, episodicMemoryService, }); const chatIntentRouter = createManagedChatIntentRouterFn({ llmProviderService, memoryV2, conversationMemoryService, episodicMemoryService, configService: memoryV2ConfigService, }); return { memoryV2ConfigService, skillRuntimeConfigService, systemDisclosurePolicyService, agentCodeRunPolicyService, wechatScheduleLlmConfigService, wechatIntentRouterConfigService, wechatIntentRouter, conversationMemoryService, memoryV2, episodicMemoryService, sessionSnapshotService, sessionStreamStore, directChatService, chatIntentRouter, getEffectiveEnv, }; }