🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
♻️ refactor(server): split the 1433-line userMemories router into sub-domain modules (#21092)
Arvin Xu81b8d63c484c
diff --git a/apps/server/src/routers/lambda/userMemories.ts b/apps/server/src/routers/lambda/userMemories.tsdeleted file mode 100644index 857231a9756..00000000000--- a/apps/server/src/routers/lambda/userMemories.ts+++ /dev/null@@ -1,1433 +0,0 @@-import { BRANDING_PROVIDER, ENABLE_BUSINESS_FEATURES } from '@lobechat/business-const';-import {- DEFAULT_SEARCH_USER_MEMORY_TOP_K,- DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM,-} from '@lobechat/const';-import { type LobeChatDatabase } from '@lobechat/database';-import {- ActivityMemoryItemSchema,- AddIdentityActionSchema,- ContextMemoryItemSchema,- ExperienceMemoryItemSchema,- PreferenceMemoryItemSchema,- RemoveIdentityActionSchema,- UpdateIdentityToolInputSchema,-} from '@lobechat/memory-user-memory';-import type { QueryTaxonomyOptionsResult, SearchMemoryResult } from '@lobechat/types';-import { LayersEnum, queryTaxonomyOptionsSchema, searchMemorySchema } from '@lobechat/types';-import { type SQL } from 'drizzle-orm';-import { and, asc, eq, gte, lte } from 'drizzle-orm';-import pMap from 'p-map';-import { z } from 'zod';--import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';-import {- type IdentityEntryBasePayload,- type IdentityEntryPayload,-} from '@/database/models/userMemory';-import {- normalizeUserMemorySearchQueries,- shouldRunUserMemoryLexicalSearch,- UserMemoryActivityModel,- UserMemoryExperienceModel,- UserMemoryIdentityModel,- UserMemoryModel,-} from '@/database/models/userMemory';-import { FtsSearchCandidateError } from '@/database/repositories/ftsSearch';-import { UserMemoryTopicRepository } from '@/database/repositories/userMemory';-import {- userMemories,- userMemoriesActivities,- userMemoriesContexts,- userMemoriesExperiences,- userMemoriesIdentities,- userMemoriesPreferences,- userSettings,-} from '@/database/schemas';-import { notTrashed } from '@/database/utils/softDelete';-import { authedProcedure, router } from '@/libs/trpc/lambda';-import { serverDatabase } from '@/libs/trpc/lambda/middleware';-import { getServerDefaultFilesConfig } from '@/server/globalConfig';-import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';-import { createFtsSearchRepo } from '@/server/services/ftsSearch';-import {- recordUserMemoryLexicalSearchDecision,- type UserMemoryLexicalSearchSource,-} from '@/server/services/ftsSearch/observability';-import type { UserMemoryEmbeddingRuntime } from '@/server/services/memory/userMemory/embedding';-import { embedUserMemoryTexts } from '@/server/services/memory/userMemory/embedding';-import {- normalizeSearchMemoryParams,- resolveMemorySearchTopK,-} from '@/server/services/memory/userMemory/searchParams';--const EMPTY_SEARCH_RESULT: SearchMemoryResult = {- activities: [],- contexts: [],- experiences: [],- identities: [],- meta: {- appliedFilters: {},- appliedQueries: [],- layers: {- activities: { hasMore: false, returned: 0, total: 0 },- contexts: { hasMore: false, returned: 0, total: 0 },- experiences: { hasMore: false, returned: 0, total: 0 },- identities: { hasMore: false, returned: 0, total: 0 },- preferences: { hasMore: false, returned: 0, total: 0 },- },- },- preferences: [],-};--const EMPTY_TAXONOMY_RESULT: QueryTaxonomyOptionsResult = {- categories: [],- hasMore: {},- labels: [],- relationships: [],- roles: [],- statuses: [],- tags: [],- types: [],-};--type MemorySearchContext = {- memoryModel: UserMemoryModel;- memoryEffort: MemoryEffort;- serverDB: LobeChatDatabase;- userId: string;-};--type MemoryEffort = 'high' | 'low' | 'medium';--const normalizeMemoryEffort = (value: unknown): MemoryEffort => {- if (value === 'low' || value === 'medium' || value === 'high') return value;- return 'medium';-};--const searchUserMemories = async (- ctx: MemorySearchContext,- input: z.infer<typeof searchMemorySchema>,- source: UserMemoryLexicalSearchSource,-): Promise<SearchMemoryResult> => {- const normalizedInput = normalizeSearchMemoryParams(input);- const { provider, model: embeddingModel } =- getServerDefaultFilesConfig().embeddingModel || DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM;- const modelRuntime = await initModelRuntimeFromDB(ctx.serverDB, ctx.userId, provider);- const normalizedQueries = normalizeUserMemorySearchQueries(normalizedInput.queries);-- const queryEmbeddings =- normalizedQueries.length > 0- ? (- await embedUserMemoryTexts({- input: normalizedQueries,- model: embeddingModel,- runtime: modelRuntime,- source: 'lambda:userMemories.search',- userId: ctx.userId,- })- ).filter((embedding): embedding is number[] => Boolean(embedding))- : [];- const lexicalSearch = shouldRunUserMemoryLexicalSearch(normalizedQueries, queryEmbeddings);- recordUserMemoryLexicalSearchDecision({- decision: lexicalSearch ? 'executed' : 'skipped_long_context',- queryCharacters: Array.from(normalizedQueries.join(' ')).length,- source,- });-- const effectiveEffort = normalizeMemoryEffort(normalizedInput.effort ?? ctx.memoryEffort);- const effortConstrainedLimits = resolveMemorySearchTopK(effectiveEffort, normalizedInput);- return ctx.memoryModel.searchMemory(- { ...normalizedInput, queries: normalizedQueries, topK: effortConstrainedLimits },- queryEmbeddings,- ) as Promise<SearchMemoryResult>;-};--const getEmbeddingRuntime = async (serverDB: LobeChatDatabase, userId: string) => {- const { provider, model: embeddingModel } =- getServerDefaultFilesConfig().embeddingModel || DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM;- // Read user's provider config from database- const agentRuntime = await initModelRuntimeFromDB(- serverDB,- userId,- ENABLE_BUSINESS_FEATURES ? BRANDING_PROVIDER : provider,- );-- return { agentRuntime, embeddingModel };-};--const createEmbedder = (- agentRuntime: UserMemoryEmbeddingRuntime,- embeddingModel: string,- userId: string,-) => {- return async (value?: string | null): Promise<number[] | undefined> => {- if (!value || value.trim().length === 0) return undefined;-- const [embedding] = await embedUserMemoryTexts({- input: [value],- model: embeddingModel,- runtime: agentRuntime,- source: 'lambda:userMemories.tool',- userId,- });-- return embedding;- };-};--const REEMBED_TABLE_KEYS = [- 'userMemories',- 'contexts',- 'preferences',- 'identities',- 'experiences',- 'activities',-] as const;-type ReEmbedTableKey = (typeof REEMBED_TABLE_KEYS)[number];--const reEmbedInputSchema = z.object({- concurrency: z.coerce.number().int().min(1).max(50).optional(),- endDate: z.coerce.date().optional(),- limit: z.coerce.number().int().min(1).optional(),- only: z.array(z.enum(REEMBED_TABLE_KEYS)).optional(),- startDate: z.coerce.date().optional(),-});--interface ReEmbedStats {- failed: number;- skipped: number;- succeeded: number;- total: number;-}--const combineConditions = (conditions: Array<SQL | undefined>): SQL | undefined => {- const filtered = conditions.filter((condition): condition is SQL => condition !== undefined);- if (filtered.length === 0) return undefined;- if (filtered.length === 1) return filtered[0];-- return and(...filtered);-};--const normalizeEmbeddable = (value?: string | null): string | undefined => {- if (typeof value !== 'string') return undefined;- const trimmed = value.trim();-- return trimmed.length > 0 ? trimmed : undefined;-};--/** Candidate-provider outages must not be converted into successful empty memory responses. */-const rethrowCandidateSearchError = (error: unknown) => {- if (error instanceof FtsSearchCandidateError) throw error;-};--const memoryProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {- const { ctx } = opts;- const userSettingsRow = await ctx.serverDB.query.userSettings.findFirst({- columns: { memory: true },- where: eq(userSettings.id, ctx.userId),- });- const memoryConfig =- typeof userSettingsRow?.memory === 'object' && userSettingsRow?.memory !== null- ? (userSettingsRow.memory as { effort?: unknown })- : undefined;- const memoryEffort = normalizeMemoryEffort(memoryConfig?.effort);-- return opts.next({- ctx: {- activityModel: new UserMemoryActivityModel(ctx.serverDB, ctx.userId),- experienceModel: new UserMemoryExperienceModel(ctx.serverDB, ctx.userId),- identityModel: new UserMemoryIdentityModel(ctx.serverDB, ctx.userId),- memoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId),- memoryEffort,- },- });-});-const memorySearchProcedure = memoryProcedure.use(async (opts) => {- const { ctx } = opts;- const ftsSearchRepo = await createFtsSearchRepo({- db: ctx.serverDB,- userId: ctx.userId,- usage: 'memory',- });-- return opts.next({- ctx: {- activityModel: new UserMemoryActivityModel(ctx.serverDB, ctx.userId, ftsSearchRepo),- experienceModel: new UserMemoryExperienceModel(ctx.serverDB, ctx.userId, ftsSearchRepo),- identityModel: new UserMemoryIdentityModel(ctx.serverDB, ctx.userId, ftsSearchRepo),- memoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId, ftsSearchRepo),- },- });-});-const memoryWriteProcedure = memoryProcedure.use(withScopedPermission('message:create'));--export const userMemoriesRouter = router({- getMemoryDetail: memoryProcedure- .input(z.object({ id: z.string(), layer: z.nativeEnum(LayersEnum) }))- .query(async ({ ctx, input }) => {- try {- return await ctx.memoryModel.getMemoryDetail(input);- } catch (error) {- console.error('Failed to retrieve memory detail:', error);- return null;- }- }),-- queryActivities: memorySearchProcedure- .input(- z- .object({- order: z.enum(['asc', 'desc']).optional(),- page: z.coerce.number().int().min(1).optional(),- pageSize: z.coerce.number().int().min(1).max(100).optional(),- q: z.string().optional(),- sort: z.enum(['capturedAt', 'startsAt']).optional(),- status: z.array(z.string()).optional(),- tags: z.array(z.string()).optional(),- types: z.array(z.string()).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- const params = input ?? {};- const fallbackPage = params.page ?? 1;- const fallbackPageSize = params.pageSize ?? 20;-- try {- return await ctx.activityModel.queryList(params);- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to query activities:', error);- return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };- }- }),-- queryExperiences: memorySearchProcedure- .input(- z- .object({- order: z.enum(['asc', 'desc']).optional(),- page: z.coerce.number().int().min(1).optional(),- pageSize: z.coerce.number().int().min(1).max(100).optional(),- q: z.string().optional(),- sort: z.enum(['capturedAt', 'scoreConfidence']).optional(),- tags: z.array(z.string()).optional(),- types: z.array(z.string()).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- const params = input ?? {};- const fallbackPage = params.page ?? 1;- const fallbackPageSize = params.pageSize ?? 20;-- try {- return await ctx.experienceModel.queryList(params);- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to query experiences:', error);- return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };- }- }),-- queryIdentities: memorySearchProcedure- .input(- z- .object({- order: z.enum(['asc', 'desc']).optional(),- page: z.coerce.number().int().min(1).optional(),- pageSize: z.coerce.number().int().min(1).max(100).optional(),- q: z.string().optional(),- relationships: z.array(z.string()).optional(),- sort: z.enum(['capturedAt', 'type']).optional(),- tags: z.array(z.string()).optional(),- types: z.array(z.string()).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- const params = input ?? {};- const fallbackPage = params.page ?? 1;- const fallbackPageSize = params.pageSize ?? 20;-- try {- return await ctx.identityModel.queryList(params);- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to query identities:', error);- return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };- }- }),-- queryIdentitiesForInjection: memoryProcedure- .input(z.object({ limit: z.coerce.number().int().min(1).max(100).optional() }).optional())- .query(async ({ ctx, input }) => {- try {- return await ctx.identityModel.queryForInjection(input?.limit ?? 50);- } catch (error) {- console.error('Failed to query identities for injection:', error);- return [];- }- }),-- queryIdentityRoles: memoryProcedure- .input(- z- .object({- page: z.coerce.number().int().min(1).optional(),- size: z.coerce.number().int().min(1).max(100).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- try {- return await ctx.memoryModel.queryIdentityRoles(input ?? {});- } catch (error) {- console.error('Failed to query identity roles:', error);- return { roles: [], tags: [] };- }- }),-- queryMemories: memorySearchProcedure- .input(- z- .object({- categories: z.array(z.string()).optional(),- layer: z.nativeEnum(LayersEnum).optional(),- order: z.enum(['asc', 'desc']).optional(),- page: z.coerce.number().int().min(1).optional(),- pageSize: z.coerce.number().int().min(1).max(100).optional(),- q: z.string().optional(),- sort: z- .enum([- 'capturedAt',- 'scoreConfidence',- 'scoreImpact',- 'scorePriority',- 'scoreUrgency',- 'startsAt',- ])- .optional(),- status: z.array(z.string()).optional(),- tags: z.array(z.string()).optional(),- types: z.array(z.string()).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- const params = input ?? {};- const fallbackPage = params.page ?? 1;- const fallbackPageSize = params.pageSize ?? 20;-- try {- return await ctx.memoryModel.queryMemories({- ...params,- order: params.order ?? 'desc',- sort: params.sort,- });- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to query memories:', error);- return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };- }- }),-- queryTags: memoryProcedure- .input(- z- .object({- layers: z.array(z.nativeEnum(LayersEnum)).optional(),- page: z.coerce.number().int().min(1).optional(),- size: z.coerce.number().int().min(1).max(100).optional(),- })- .optional(),- )- .query(async ({ ctx, input }) => {- try {- return await ctx.memoryModel.queryTags(input ?? {});- } catch (error) {- console.error('Failed to query memory tags:', error);- return [];- }- }),-- queryTaxonomyOptions: memoryProcedure- .input(queryTaxonomyOptionsSchema.optional())- .query(async ({ ctx, input }) => {- try {- return await ctx.memoryModel.queryTaxonomyOptions(input ?? {});- } catch (error) {- console.error('Failed to query memory taxonomy options:', error);- return EMPTY_TAXONOMY_RESULT;- }- }),-- reEmbedMemories: memoryWriteProcedure- .input(reEmbedInputSchema.optional())- .mutation(async ({ ctx, input }) => {- try {- const options = input ?? {};- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const concurrency = options.concurrency ?? 10;- const shouldProcess = (key: ReEmbedTableKey) =>- !options.only || options.only.length === 0 || options.only.includes(key);-- const embedTexts = async (texts: string[]): Promise<number[][]> => {- if (texts.length === 0) return [];-- const response = await embedUserMemoryTexts({- input: texts,- model: embeddingModel,- runtime: agentRuntime,- source: 'lambda:userMemories.reEmbed',- userId: ctx.userId,- });-- if (response.length !== texts.length) {- throw new Error('Embedding response length mismatch');- }-- return response.map((embedding) => {- if (!embedding) throw new Error('Embedding response length mismatch');-- return embedding;- });- };-- const results: Partial<Record<ReEmbedTableKey, ReEmbedStats>> = {};-- const run = async (key: ReEmbedTableKey, handler: () => Promise<ReEmbedStats>) => {- if (!shouldProcess(key)) return;- results[key] = await handler();- };-- // Individual re-embed handlers are appended below.- await run('userMemories', async () => {- const where = combineConditions([- eq(userMemories.userId, ctx.userId),- notTrashed(userMemories.isDeleted),- options.startDate ? gte(userMemories.createdAt, options.startDate) : undefined,- options.endDate ? lte(userMemories.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemories.findMany({- columns: { details: true, id: true, summary: true },- limit: options.limit,- orderBy: [asc(userMemories.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const summaryText = normalizeEmbeddable(row.summary);- const detailsText = normalizeEmbeddable(row.details);-- try {- if (!summaryText && !detailsText) {- await ctx.memoryModel.updateUserMemoryVectors(row.id, {- detailsVector1024: null,- summaryVector1024: null,- });- skipped += 1;- return;- }-- const inputs: string[] = [];- if (summaryText) inputs.push(summaryText);- if (detailsText) inputs.push(detailsText);-- const embeddings = await embedTexts(inputs);- let embedIndex = 0;-- const summaryVector = summaryText ? (embeddings[embedIndex++] ?? null) : null;- const detailsVector = detailsText ? (embeddings[embedIndex++] ?? null) : null;-- await ctx.memoryModel.updateUserMemoryVectors(row.id, {- detailsVector1024: detailsVector,- summaryVector1024: summaryVector,- });-- succeeded += 1;- } catch (err) {- failed += 1;- console.error(- `[memoryRouter.reEmbed] Failed to re-embed user memory ${row.id}`,- err,- );- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- await run('contexts', async () => {- const where = combineConditions([- eq(userMemoriesContexts.userId, ctx.userId),- options.startDate ? gte(userMemoriesContexts.createdAt, options.startDate) : undefined,- options.endDate ? lte(userMemoriesContexts.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemoriesContexts.findMany({- columns: { description: true, id: true },- limit: options.limit,- orderBy: [asc(userMemoriesContexts.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const description = normalizeEmbeddable(row.description);-- try {- if (!description) {- await ctx.memoryModel.updateContextVectors(row.id, {- descriptionVector: null,- });- skipped += 1;- return;- }-- const [embedding] = await embedTexts([description]);-- await ctx.memoryModel.updateContextVectors(row.id, {- descriptionVector: embedding ?? null,- });- succeeded += 1;- } catch (err) {- failed += 1;- console.error(`[memoryRouter.reEmbed] Failed to re-embed context ${row.id}`, err);- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- await run('preferences', async () => {- const where = combineConditions([- eq(userMemoriesPreferences.userId, ctx.userId),- options.startDate- ? gte(userMemoriesPreferences.createdAt, options.startDate)- : undefined,- options.endDate ? lte(userMemoriesPreferences.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemoriesPreferences.findMany({- columns: { conclusionDirectives: true, id: true },- limit: options.limit,- orderBy: [asc(userMemoriesPreferences.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const directives = normalizeEmbeddable(row.conclusionDirectives);-- try {- if (!directives) {- await ctx.memoryModel.updatePreferenceVectors(row.id, {- conclusionDirectivesVector: null,- });- skipped += 1;- return;- }-- const [embedding] = await embedTexts([directives]);- await ctx.memoryModel.updatePreferenceVectors(row.id, {- conclusionDirectivesVector: embedding ?? null,- });- succeeded += 1;- } catch (err) {- failed += 1;- console.error(- `[memoryRouter.reEmbed] Failed to re-embed preference ${row.id}`,- err,- );- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- await run('identities', async () => {- const where = combineConditions([- eq(userMemoriesIdentities.userId, ctx.userId),- options.startDate- ? gte(userMemoriesIdentities.createdAt, options.startDate)- : undefined,- options.endDate ? lte(userMemoriesIdentities.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemoriesIdentities.findMany({- columns: { description: true, id: true },- limit: options.limit,- orderBy: [asc(userMemoriesIdentities.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const description = normalizeEmbeddable(row.description);-- try {- if (!description) {- await ctx.memoryModel.updateIdentityVectors(row.id, {- descriptionVector: null,- });- skipped += 1;- return;- }-- const [embedding] = await embedTexts([description]);- await ctx.memoryModel.updateIdentityVectors(row.id, {- descriptionVector: embedding ?? null,- });- succeeded += 1;- } catch (err) {- failed += 1;- console.error(`[memoryRouter.reEmbed] Failed to re-embed identity ${row.id}`, err);- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- await run('activities', async () => {- const where = combineConditions([- eq(userMemoriesActivities.userId, ctx.userId),- options.startDate- ? gte(userMemoriesActivities.createdAt, options.startDate)- : undefined,- options.endDate ? lte(userMemoriesActivities.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemoriesActivities.findMany({- columns: { feedback: true, id: true, narrative: true },- limit: options.limit,- orderBy: [asc(userMemoriesActivities.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const narrative = normalizeEmbeddable(row.narrative);- const feedback = normalizeEmbeddable(row.feedback);-- try {- if (!narrative && !feedback) {- await ctx.memoryModel.updateActivityVectors(row.id, {- feedbackVector: null,- narrativeVector: null,- });- skipped += 1;- return;- }-- const inputs: string[] = [];- if (narrative) inputs.push(narrative);- if (feedback) inputs.push(feedback);-- const embeddings = await embedTexts(inputs);- let embedIndex = 0;-- const narrativeVector = narrative ? (embeddings[embedIndex++] ?? null) : null;- const feedbackVector = feedback ? (embeddings[embedIndex++] ?? null) : null;-- await ctx.memoryModel.updateActivityVectors(row.id, {- feedbackVector,- narrativeVector,- });- succeeded += 1;- } catch (err) {- failed += 1;- console.error(`[memoryRouter.reEmbed] Failed to re-embed activity ${row.id}`, err);- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- await run('experiences', async () => {- const where = combineConditions([- eq(userMemoriesExperiences.userId, ctx.userId),- options.startDate- ? gte(userMemoriesExperiences.createdAt, options.startDate)- : undefined,- options.endDate ? lte(userMemoriesExperiences.createdAt, options.endDate) : undefined,- ]);-- const rows = await ctx.serverDB.query.userMemoriesExperiences.findMany({- columns: { action: true, id: true, keyLearning: true, situation: true },- limit: options.limit,- orderBy: [asc(userMemoriesExperiences.createdAt)],- where,- });-- let succeeded = 0;- let failed = 0;- let skipped = 0;-- await pMap(- rows,- async (row) => {- const situation = normalizeEmbeddable(row.situation);- const action = normalizeEmbeddable(row.action);- const keyLearning = normalizeEmbeddable(row.keyLearning);-- try {- if (!situation && !action && !keyLearning) {- await ctx.memoryModel.updateExperienceVectors(row.id, {- actionVector: null,- keyLearningVector: null,- situationVector: null,- });- skipped += 1;- return;- }-- const inputs: string[] = [];- if (situation) inputs.push(situation);- if (action) inputs.push(action);- if (keyLearning) inputs.push(keyLearning);-- const embeddings = await embedTexts(inputs);- let embedIndex = 0;-- const situationVector = situation ? (embeddings[embedIndex++] ?? null) : null;- const actionVector = action ? (embeddings[embedIndex++] ?? null) : null;- const keyLearningVector = keyLearning ? (embeddings[embedIndex++] ?? null) : null;-- await ctx.memoryModel.updateExperienceVectors(row.id, {- actionVector,- keyLearningVector,- situationVector,- });- succeeded += 1;- } catch (err) {- failed += 1;- console.error(- `[memoryRouter.reEmbed] Failed to re-embed experience ${row.id}`,- err,- );- }- },- { concurrency },- );-- return {- failed,- skipped,- succeeded,- total: rows.length,- } satisfies ReEmbedStats;- });-- const processedEntries = Object.entries(results) as Array<[ReEmbedTableKey, ReEmbedStats]>;-- if (processedEntries.length === 0) {- return {- message: 'No memory records matched re-embed criteria',- results,- success: true,- };- }-- const aggregate = processedEntries.reduce(- (acc, [, stats]) => {- acc.failed += stats.failed;- acc.skipped += stats.skipped;- acc.succeeded += stats.succeeded;- acc.total += stats.total;-- return acc;- },- { failed: 0, skipped: 0, succeeded: 0, total: 0 },- );-- const message =- aggregate.total === 0- ? 'No memory records required re-embedding'- : `Re-embedded ${aggregate.succeeded} of ${aggregate.total} records`;-- return {- aggregate,- message,- results,- success: true,- };- } catch (error) {- console.error('Failed to re-embed memories:', error);- return {- message: `Failed to re-embed memories: ${(error as Error).message}`,- success: false,- };- }- }),-- /**- * Retrieve memories for a specific topic- * Uses concatenated user messages (first 7000 chars) as the search query- */- retrieveMemoryForTopic: memorySearchProcedure- .input(z.object({ topicId: z.string() }))- .query(async ({ ctx, input }) => {- // Dev-only escape hatch: skip the embedding + memory search triggered by topic- // load / switch, so chat debugging logs aren't drowned in `text-embedding-3-small`- // router-runtime output. Only honored in non-production builds.- if (process.env.NODE_ENV !== 'production' && process.env.DEV_DISABLE_AUTO_MEMORY === '1') {- console.info('[dev] skip retrieveMemoryForTopic (DEV_DISABLE_AUTO_MEMORY=1)');- return EMPTY_SEARCH_RESULT;- }-- try {- // Get concatenated user messages for this topic- const userMemoryTopicRepo = new UserMemoryTopicRepository(- ctx.serverDB,- ctx.userId,- ctx.workspaceId ?? undefined,- );- const query = await userMemoryTopicRepo.getUserMessagesQueryForTopic(input.topicId);-- if (!query) {- // No user messages available, return empty result- return EMPTY_SEARCH_RESULT;- }-- // Search memories using concatenated user messages- const searchParams = {- queries: [query],- topK: DEFAULT_SEARCH_USER_MEMORY_TOP_K,- };-- const result = await searchUserMemories(ctx, searchParams, 'topic_retrieval');- return result;- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to retrieve memory for topic:', error);- return EMPTY_SEARCH_RESULT;- }- }),-- searchMemory: memorySearchProcedure.input(searchMemorySchema).query(async ({ input, ctx }) => {- try {- return await searchUserMemories(ctx, input, 'api');- } catch (error) {- rethrowCandidateSearchError(error);- console.error('Failed to retrieve memories:', error);- return EMPTY_SEARCH_RESULT;- }- }),-- toolAddActivityMemory: memoryWriteProcedure- .input(ActivityMemoryItemSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- const summaryEmbedding = await embed(input.summary);- const detailsEmbedding = await embed(input.details);- const narrativeVector = await embed(input.withActivity.narrative);- const feedbackVector = await embed(input.withActivity.feedback);-- const { activity, memory } = await ctx.memoryModel.createActivityMemory({- activity: {- associatedLocations:- UserMemoryModel.parseAssociatedLocations(input.withActivity.associatedLocations) ??- null,- associatedObjects:- UserMemoryModel.parseAssociatedObjects(input.withActivity.associatedObjects) ?? [],- associatedSubjects:- UserMemoryModel.parseAssociatedSubjects(input.withActivity.associatedSubjects) ?? [],- endsAt: UserMemoryModel.parseDateFromString(input.withActivity.endsAt ?? undefined),- feedback: input.withActivity.feedback ?? null,- feedbackVector: feedbackVector ?? null,- metadata: input.withActivity.metadata ?? null,- narrative: input.withActivity.narrative ?? null,- narrativeVector: narrativeVector ?? null,- notes: input.withActivity.notes ?? null,- startsAt: UserMemoryModel.parseDateFromString(input.withActivity.startsAt ?? undefined),- status: input.withActivity.status ?? 'pending',- tags: input.withActivity.tags ?? input.tags ?? [],- timezone: input.withActivity.timezone ?? null,- type: input.withActivity.type ?? 'other',- },- details: input.details || '',- detailsEmbedding,- memoryCategory: input.memoryCategory,- memoryLayer: LayersEnum.Activity,- memoryType: input.memoryType,- summary: input.summary,- summaryEmbedding,- title: input.title,- });-- return {- activityId: activity.id,- memoryId: memory.id,- message: 'Memory saved successfully',- success: true,- };- } catch (error) {- console.error('Failed to save memory:', error);- return {- message: `Failed to save memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolAddContextMemory: memoryWriteProcedure- .input(ContextMemoryItemSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- const summaryEmbedding = await embed(input.summary);- const detailsEmbedding = await embed(input.details);- const contextDescriptionEmbedding = await embed(input.withContext.description);-- const { context, memory } = await ctx.memoryModel.createContextMemory({- context: {- associatedObjects:- UserMemoryModel.parseAssociatedObjects(input.withContext.associatedObjects) ?? null,- associatedSubjects:- UserMemoryModel.parseAssociatedSubjects(input.withContext.associatedSubjects) ?? null,- currentStatus: input.withContext.currentStatus ?? null,- description: input.withContext.description ?? null,- descriptionVector: contextDescriptionEmbedding ?? null,- metadata: {},- scoreImpact: input.withContext.scoreImpact ?? null,- scoreUrgency: input.withContext.scoreUrgency ?? null,- tags: input.tags ?? [],- title: input.withContext.title ?? null,- type: input.withContext.type ?? null,- },- details: input.details || '',- detailsEmbedding,- memoryCategory: input.memoryCategory,- memoryLayer: LayersEnum.Context,- memoryType: input.memoryType,- summary: input.summary,- summaryEmbedding,- title: input.title,- });-- return {- contextId: context.id,- memoryId: memory.id,- message: 'Memory saved successfully',- success: true,- };- } catch (error) {- console.error('Failed to save memory:', error);- return {- message: `Failed to save memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolAddExperienceMemory: memoryWriteProcedure- .input(ExperienceMemoryItemSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- const summaryEmbedding = await embed(input.summary);- const detailsEmbedding = await embed(input.details);- const situationVector = await embed(input.withExperience.situation);- const actionVector = await embed(input.withExperience.action);- const keyLearningVector = await embed(input.withExperience.keyLearning);-- const { experience, memory } = await ctx.memoryModel.createExperienceMemory({- details: input.details || '',- detailsEmbedding,- experience: {- action: input.withExperience.action ?? null,- actionVector: actionVector ?? null,- keyLearning: input.withExperience.keyLearning ?? null,- keyLearningVector: keyLearningVector ?? null,- metadata: {},- possibleOutcome: input.withExperience.possibleOutcome ?? null,- reasoning: input.withExperience.reasoning ?? null,- scoreConfidence: input.withExperience.scoreConfidence ?? null,- situation: input.withExperience.situation ?? null,- situationVector: situationVector ?? null,- tags: input.tags ?? [],- type: input.memoryType,- },- memoryCategory: input.memoryCategory,- memoryLayer: LayersEnum.Experience,- memoryType: input.memoryType,- summary: input.summary,- summaryEmbedding,- title: input.title,- });-- return {- experienceId: experience.id,- memoryId: memory.id,- message: 'Memory saved successfully',- success: true,- };- } catch (error) {- console.error('Failed to save memory:', error);- return {- message: `Failed to save memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolAddIdentityMemory: memoryWriteProcedure- .input(AddIdentityActionSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- const summaryEmbedding = await embed(input.summary);- const detailsEmbedding = await embed(input.details);- const descriptionEmbedding = await embed(input.withIdentity.description);-- const identityMetadata: Record<string, unknown> = {};- if (- input.withIdentity.scoreConfidence !== null &&- input.withIdentity.scoreConfidence !== undefined- ) {- identityMetadata.scoreConfidence = input.withIdentity.scoreConfidence;- }- if (- input.withIdentity.sourceEvidence !== null &&- input.withIdentity.sourceEvidence !== undefined- ) {- identityMetadata.sourceEvidence = input.withIdentity.sourceEvidence;- }-- const { identityId, userMemoryId } = await ctx.memoryModel.addIdentityEntry({- base: {- details: input.details,- detailsVector1024: detailsEmbedding ?? null,- memoryCategory: input.memoryCategory,- memoryLayer: LayersEnum.Identity,- memoryType: input.memoryType,- metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,- summary: input.summary,- summaryVector1024: summaryEmbedding ?? null,- tags: input.tags,- title: input.title,- },- identity: {- description: input.withIdentity.description,- descriptionVector: descriptionEmbedding ?? null,- episodicDate: input.withIdentity.episodicDate,- metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,- relationship: input.withIdentity.relationship,- role: input.withIdentity.role,- tags: input.tags,- type: input.withIdentity.type,- },- });-- return {- identityId,- memoryId: userMemoryId,- message: 'Identity memory saved successfully',- success: true,- };- } catch (error) {- console.error('Failed to save identity memory:', error);- return {- message: `Failed to save identity memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolAddPreferenceMemory: memoryWriteProcedure- .input(PreferenceMemoryItemSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- const summaryEmbedding = await embed(input.summary);- const detailsEmbedding = await embed(input.details);- const conclusionVector = await embed(input.withPreference.conclusionDirectives);-- const suggestionsText =- input.withPreference?.suggestions?.length && input.withPreference?.suggestions?.length > 0- ? input.withPreference?.suggestions?.join('\n')- : null;-- const metadata = {- appContext: input.withPreference.appContext,- extractedScopes: input.withPreference.extractedScopes,- originContext: input.withPreference.originContext,- } satisfies Record<string, unknown>;-- const { memory, preference } = await ctx.memoryModel.createPreferenceMemory({- details: input.details || '',- detailsEmbedding,- memoryCategory: input.memoryCategory,- memoryLayer: LayersEnum.Preference,- memoryType: input.memoryType,- preference: {- conclusionDirectives: input.withPreference.conclusionDirectives || '',- conclusionDirectivesVector: conclusionVector ?? null,- metadata,- scorePriority: input.withPreference.scorePriority ?? null,- suggestions: suggestionsText,- tags: input.tags,- type: input.memoryType,- },- summary: input.summary,- summaryEmbedding,- title: input.title,- });-- return {- memoryId: memory.id,- message: 'Memory saved successfully',- preferenceId: preference.id,- success: true,- };- } catch (error) {- console.error('Failed to save memory:', error);- return {- message: `Failed to save memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolRemoveIdentityMemory: memoryWriteProcedure- .input(RemoveIdentityActionSchema)- .mutation(async ({ input, ctx }) => {- try {- const removed = await ctx.memoryModel.removeIdentityEntry(input.id);-- if (!removed) {- return {- message: 'Identity memory not found',- success: false,- };- }-- return {- identityId: input.id,- message: 'Identity memory removed successfully',- reason: input.reason,- success: true,- };- } catch (error) {- console.error('Failed to remove identity memory:', error);- return {- message: `Failed to remove identity memory: ${(error as Error).message}`,- success: false,- };- }- }),-- toolSearchMemory: memorySearchProcedure- .input(searchMemorySchema)- .query(async ({ input, ctx }) => {- const result = await searchUserMemories(ctx, input, 'tool');- return result;- }),-- toolUpdateIdentityMemory: memoryWriteProcedure- .input(UpdateIdentityToolInputSchema)- .mutation(async ({ input, ctx }) => {- try {- const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(- ctx.serverDB,- ctx.userId,- );- const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);-- let summaryVector1024: number[] | null | undefined;- if (input.set.summary !== undefined) {- const vector = await embed(input.set.summary);- summaryVector1024 = vector ?? null;- }-- let detailsVector1024: number[] | null | undefined;- if (input.set.details !== undefined) {- const vector = await embed(input.set.details);- detailsVector1024 = vector ?? null;- }-- let descriptionVector: number[] | null | undefined;- if (input.set.withIdentity.description !== undefined) {- const vector = await embed(input.set.withIdentity.description);- descriptionVector = vector ?? null;- }-- const metadataUpdates: Record<string, unknown> = {};- if (Object.hasOwn(input.set.withIdentity, 'scoreConfidence')) {- metadataUpdates.scoreConfidence = input.set.withIdentity.scoreConfidence ?? null;- }- if (Object.hasOwn(input.set.withIdentity, 'sourceEvidence')) {- metadataUpdates.sourceEvidence = input.set.withIdentity.sourceEvidence ?? null;- }-- const identityPayload: Partial<IdentityEntryPayload> = {};- if (input.set.withIdentity.description !== undefined) {- identityPayload.description = input.set.withIdentity.description;- identityPayload.descriptionVector = descriptionVector;- }- if (input.set.withIdentity.episodicDate !== undefined) {- identityPayload.episodicDate = input.set.withIdentity.episodicDate;- }- if (input.set.withIdentity.relationship !== undefined) {- identityPayload.relationship = input.set.withIdentity.relationship;- }- if (input.set.withIdentity.role !== undefined) {- identityPayload.role = input.set.withIdentity.role;- }- if (input.set.tags !== undefined) {- identityPayload.tags = input.set.tags;- }- if (input.set.withIdentity.type !== undefined) {- identityPayload.type = input.set.withIdentity.type;- }- if (Object.keys(metadataUpdates).length > 0) {- identityPayload.metadata = metadataUpdates;- }-- const basePayload: Partial<IdentityEntryBasePayload> = {};- if (input.set.details !== undefined) {- basePayload.details = input.set.details;- basePayload.detailsVector1024 = detailsVector1024;- }- if (input.set.memoryCategory !== undefined) {- basePayload.memoryCategory = input.set.memoryCategory;- }- if (input.set.memoryType !== undefined) {- basePayload.memoryType = input.set.memoryType;- }- if (input.set.summary !== undefined) {- basePayload.summary = input.set.summary;- basePayload.summaryVector1024 = summaryVector1024;- }- if (input.set.tags !== undefined) {- basePayload.tags = input.set.tags;- }- if (input.set.title !== undefined) {- basePayload.title = input.set.title;- }- if (Object.keys(metadataUpdates).length > 0) {- basePayload.metadata = metadataUpdates;- }-- const updated = await ctx.memoryModel.updateIdentityEntry({- base: Object.keys(basePayload).length > 0 ? basePayload : undefined,- identity: Object.keys(identityPayload).length > 0 ? identityPayload : undefined,- identityId: input.id,- mergeStrategy: input.mergeStrategy,- preserveOmittedFields: true,- });-- if (!updated) {- return {- message: 'Identity memory not found',- success: false,- };- }-- return {- identityId: input.id,- message: 'Identity memory updated successfully',- success: true,- };- } catch (error) {- console.error('Failed to update identity memory:', error);- return {- message: `Failed to update identity memory: ${(error as Error).message}`,- success: false,- };- }- }),-});diff --git a/apps/server/src/routers/lambda/userMemories/index.procedures.test.ts b/apps/server/src/routers/lambda/userMemories/index.procedures.test.tsnew file mode 100644index 00000000000..f61b05a6dde--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/index.procedures.test.ts@@ -0,0 +1,63 @@+// @vitest-environment node+import { describe, expect, it } from 'vitest';++import { userMemoriesRouter } from '.';+import { queryRouter } from './query';+import { reEmbedRouter } from './reEmbed';+import { searchRouter } from './search';+import { toolWriteRouter } from './toolWrite';++/**+ * Pins the router's public surface: every procedure path, its kind, and its+ * middleware chain length (which encodes the procedure builder and its input+ * parsers). Splitting the router across files must keep all three.+ */+describe('userMemoriesRouter procedures', () => {+ const procedures = (userMemoriesRouter as any)._def.procedures as Record<string, any>;++ it('exposes exactly the expected procedure paths', () => {+ expect(Object.keys(procedures).sort()).toEqual(expected().map(([name]) => name));+ });++ it.each([+ ['queryRouter', queryRouter],+ ['reEmbedRouter', reEmbedRouter],+ ['searchRouter', searchRouter],+ ['toolWriteRouter', toolWriteRouter],+ ])('%s declares its procedures in alphabetical order', (_name, subRouter) => {+ const names = Object.keys((subRouter as any)._def.procedures);+ expect(names).toEqual([...names].sort());+ });++ it.each(expected())('%s is a %s with %i middlewares', (name, type, middlewares) => {+ expect(procedures[name]._def.type).toBe(type);+ expect(procedures[name]._def.middlewares).toHaveLength(middlewares);+ });+});++function expected(): Array<+ [name: string, type: 'mutation' | 'query' | 'subscription', middlewares: number]+> {+ return [+ ['getMemoryDetail', 'query', 11],+ ['queryActivities', 'query', 12],+ ['queryExperiences', 'query', 12],+ ['queryIdentities', 'query', 12],+ ['queryIdentitiesForInjection', 'query', 11],+ ['queryIdentityRoles', 'query', 11],+ ['queryMemories', 'query', 12],+ ['queryTags', 'query', 11],+ ['queryTaxonomyOptions', 'query', 11],+ ['reEmbedMemories', 'mutation', 12],+ ['retrieveMemoryForTopic', 'query', 12],+ ['searchMemory', 'query', 12],+ ['toolAddActivityMemory', 'mutation', 12],+ ['toolAddContextMemory', 'mutation', 12],+ ['toolAddExperienceMemory', 'mutation', 12],+ ['toolAddIdentityMemory', 'mutation', 12],+ ['toolAddPreferenceMemory', 'mutation', 12],+ ['toolRemoveIdentityMemory', 'mutation', 12],+ ['toolSearchMemory', 'query', 12],+ ['toolUpdateIdentityMemory', 'mutation', 12],+ ];+}diff --git a/apps/server/src/routers/lambda/userMemories.test.ts b/apps/server/src/routers/lambda/userMemories/index.test.tssimilarity index 99%rename from apps/server/src/routers/lambda/userMemories.test.tsrename to apps/server/src/routers/lambda/userMemories/index.test.tsindex 85047c934ea..a49f3faf31c 100644--- a/apps/server/src/routers/lambda/userMemories.test.ts+++ b/apps/server/src/routers/lambda/userMemories/index.test.ts@@ -8,7 +8,7 @@ import { UserMemoryModel } from '@/database/models/userMemory';import { FtsSearchCandidateError } from '@/database/repositories/ftsSearch';import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';-import { userMemoriesRouter } from './userMemories';+import { userMemoriesRouter } from '.';vi.mock('@/database/core/db-adaptor', () => ({getServerDB: vi.fn(),diff --git a/apps/server/src/routers/lambda/userMemories/index.ts b/apps/server/src/routers/lambda/userMemories/index.tsnew file mode 100644index 00000000000..b187cbdd809--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/index.ts@@ -0,0 +1,13 @@+import { mergeRouters } from '@/libs/trpc/lambda';++import { queryRouter } from './query';+import { reEmbedRouter } from './reEmbed';+import { searchRouter } from './search';+import { toolWriteRouter } from './toolWrite';++export const userMemoriesRouter = mergeRouters(+ queryRouter,+ reEmbedRouter,+ searchRouter,+ toolWriteRouter,+);diff --git a/apps/server/src/routers/lambda/userMemories/procedures.ts b/apps/server/src/routers/lambda/userMemories/procedures.tsnew file mode 100644index 00000000000..fc144d6bd37--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/procedures.ts@@ -0,0 +1,63 @@+import { eq } from 'drizzle-orm';++import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';+import {+ UserMemoryActivityModel,+ UserMemoryExperienceModel,+ UserMemoryIdentityModel,+ UserMemoryModel,+} from '@/database/models/userMemory';+import { userSettings } from '@/database/schemas';+import { authedProcedure } from '@/libs/trpc/lambda';+import { serverDatabase } from '@/libs/trpc/lambda/middleware';+import { createFtsSearchRepo } from '@/server/services/ftsSearch';++export type MemoryEffort = 'high' | 'low' | 'medium';++export const normalizeMemoryEffort = (value: unknown): MemoryEffort => {+ if (value === 'low' || value === 'medium' || value === 'high') return value;+ return 'medium';+};++export const memoryProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {+ const { ctx } = opts;+ const userSettingsRow = await ctx.serverDB.query.userSettings.findFirst({+ columns: { memory: true },+ where: eq(userSettings.id, ctx.userId),+ });+ const memoryConfig =+ typeof userSettingsRow?.memory === 'object' && userSettingsRow?.memory !== null+ ? (userSettingsRow.memory as { effort?: unknown })+ : undefined;+ const memoryEffort = normalizeMemoryEffort(memoryConfig?.effort);++ return opts.next({+ ctx: {+ activityModel: new UserMemoryActivityModel(ctx.serverDB, ctx.userId),+ experienceModel: new UserMemoryExperienceModel(ctx.serverDB, ctx.userId),+ identityModel: new UserMemoryIdentityModel(ctx.serverDB, ctx.userId),+ memoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId),+ memoryEffort,+ },+ });+});++export const memorySearchProcedure = memoryProcedure.use(async (opts) => {+ const { ctx } = opts;+ const ftsSearchRepo = await createFtsSearchRepo({+ db: ctx.serverDB,+ userId: ctx.userId,+ usage: 'memory',+ });++ return opts.next({+ ctx: {+ activityModel: new UserMemoryActivityModel(ctx.serverDB, ctx.userId, ftsSearchRepo),+ experienceModel: new UserMemoryExperienceModel(ctx.serverDB, ctx.userId, ftsSearchRepo),+ identityModel: new UserMemoryIdentityModel(ctx.serverDB, ctx.userId, ftsSearchRepo),+ memoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId, ftsSearchRepo),+ },+ });+});++export const memoryWriteProcedure = memoryProcedure.use(withScopedPermission('message:create'));diff --git a/apps/server/src/routers/lambda/userMemories/query.ts b/apps/server/src/routers/lambda/userMemories/query.tsnew file mode 100644index 00000000000..b0eb692bc15--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/query.ts@@ -0,0 +1,213 @@+import type { QueryTaxonomyOptionsResult } from '@lobechat/types';+import { LayersEnum, queryTaxonomyOptionsSchema } from '@lobechat/types';+import { z } from 'zod';++import { router } from '@/libs/trpc/lambda';++import { memoryProcedure, memorySearchProcedure } from './procedures';+import { rethrowCandidateSearchError } from './utils';++const EMPTY_TAXONOMY_RESULT: QueryTaxonomyOptionsResult = {+ categories: [],+ hasMore: {},+ labels: [],+ relationships: [],+ roles: [],+ statuses: [],+ tags: [],+ types: [],+};++export const queryRouter = router({+ getMemoryDetail: memoryProcedure+ .input(z.object({ id: z.string(), layer: z.nativeEnum(LayersEnum) }))+ .query(async ({ ctx, input }) => {+ try {+ return await ctx.memoryModel.getMemoryDetail(input);+ } catch (error) {+ console.error('Failed to retrieve memory detail:', error);+ return null;+ }+ }),+ queryActivities: memorySearchProcedure+ .input(+ z+ .object({+ order: z.enum(['asc', 'desc']).optional(),+ page: z.coerce.number().int().min(1).optional(),+ pageSize: z.coerce.number().int().min(1).max(100).optional(),+ q: z.string().optional(),+ sort: z.enum(['capturedAt', 'startsAt']).optional(),+ status: z.array(z.string()).optional(),+ tags: z.array(z.string()).optional(),+ types: z.array(z.string()).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ const params = input ?? {};+ const fallbackPage = params.page ?? 1;+ const fallbackPageSize = params.pageSize ?? 20;++ try {+ return await ctx.activityModel.queryList(params);+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to query activities:', error);+ return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };+ }+ }),+ queryExperiences: memorySearchProcedure+ .input(+ z+ .object({+ order: z.enum(['asc', 'desc']).optional(),+ page: z.coerce.number().int().min(1).optional(),+ pageSize: z.coerce.number().int().min(1).max(100).optional(),+ q: z.string().optional(),+ sort: z.enum(['capturedAt', 'scoreConfidence']).optional(),+ tags: z.array(z.string()).optional(),+ types: z.array(z.string()).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ const params = input ?? {};+ const fallbackPage = params.page ?? 1;+ const fallbackPageSize = params.pageSize ?? 20;++ try {+ return await ctx.experienceModel.queryList(params);+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to query experiences:', error);+ return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };+ }+ }),+ queryIdentities: memorySearchProcedure+ .input(+ z+ .object({+ order: z.enum(['asc', 'desc']).optional(),+ page: z.coerce.number().int().min(1).optional(),+ pageSize: z.coerce.number().int().min(1).max(100).optional(),+ q: z.string().optional(),+ relationships: z.array(z.string()).optional(),+ sort: z.enum(['capturedAt', 'type']).optional(),+ tags: z.array(z.string()).optional(),+ types: z.array(z.string()).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ const params = input ?? {};+ const fallbackPage = params.page ?? 1;+ const fallbackPageSize = params.pageSize ?? 20;++ try {+ return await ctx.identityModel.queryList(params);+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to query identities:', error);+ return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };+ }+ }),+ queryIdentitiesForInjection: memoryProcedure+ .input(z.object({ limit: z.coerce.number().int().min(1).max(100).optional() }).optional())+ .query(async ({ ctx, input }) => {+ try {+ return await ctx.identityModel.queryForInjection(input?.limit ?? 50);+ } catch (error) {+ console.error('Failed to query identities for injection:', error);+ return [];+ }+ }),+ queryIdentityRoles: memoryProcedure+ .input(+ z+ .object({+ page: z.coerce.number().int().min(1).optional(),+ size: z.coerce.number().int().min(1).max(100).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ try {+ return await ctx.memoryModel.queryIdentityRoles(input ?? {});+ } catch (error) {+ console.error('Failed to query identity roles:', error);+ return { roles: [], tags: [] };+ }+ }),+ queryMemories: memorySearchProcedure+ .input(+ z+ .object({+ categories: z.array(z.string()).optional(),+ layer: z.nativeEnum(LayersEnum).optional(),+ order: z.enum(['asc', 'desc']).optional(),+ page: z.coerce.number().int().min(1).optional(),+ pageSize: z.coerce.number().int().min(1).max(100).optional(),+ q: z.string().optional(),+ sort: z+ .enum([+ 'capturedAt',+ 'scoreConfidence',+ 'scoreImpact',+ 'scorePriority',+ 'scoreUrgency',+ 'startsAt',+ ])+ .optional(),+ status: z.array(z.string()).optional(),+ tags: z.array(z.string()).optional(),+ types: z.array(z.string()).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ const params = input ?? {};+ const fallbackPage = params.page ?? 1;+ const fallbackPageSize = params.pageSize ?? 20;++ try {+ return await ctx.memoryModel.queryMemories({+ ...params,+ order: params.order ?? 'desc',+ sort: params.sort,+ });+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to query memories:', error);+ return { items: [], page: fallbackPage, pageSize: fallbackPageSize, total: 0 };+ }+ }),+ queryTags: memoryProcedure+ .input(+ z+ .object({+ layers: z.array(z.nativeEnum(LayersEnum)).optional(),+ page: z.coerce.number().int().min(1).optional(),+ size: z.coerce.number().int().min(1).max(100).optional(),+ })+ .optional(),+ )+ .query(async ({ ctx, input }) => {+ try {+ return await ctx.memoryModel.queryTags(input ?? {});+ } catch (error) {+ console.error('Failed to query memory tags:', error);+ return [];+ }+ }),+ queryTaxonomyOptions: memoryProcedure+ .input(queryTaxonomyOptionsSchema.optional())+ .query(async ({ ctx, input }) => {+ try {+ return await ctx.memoryModel.queryTaxonomyOptions(input ?? {});+ } catch (error) {+ console.error('Failed to query memory taxonomy options:', error);+ return EMPTY_TAXONOMY_RESULT;+ }+ }),+});diff --git a/apps/server/src/routers/lambda/userMemories/reEmbed.ts b/apps/server/src/routers/lambda/userMemories/reEmbed.tsnew file mode 100644index 00000000000..bea4c6e1add--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/reEmbed.ts@@ -0,0 +1,526 @@+import { type SQL } from 'drizzle-orm';+import { and, asc, eq, gte, lte } from 'drizzle-orm';+import pMap from 'p-map';+import { z } from 'zod';++import {+ userMemories,+ userMemoriesActivities,+ userMemoriesContexts,+ userMemoriesExperiences,+ userMemoriesIdentities,+ userMemoriesPreferences,+} from '@/database/schemas';+import { notTrashed } from '@/database/utils/softDelete';+import { router } from '@/libs/trpc/lambda';+import { embedUserMemoryTexts } from '@/server/services/memory/userMemory/embedding';++import { memoryWriteProcedure } from './procedures';+import { getEmbeddingRuntime } from './utils';++const REEMBED_TABLE_KEYS = [+ 'userMemories',+ 'contexts',+ 'preferences',+ 'identities',+ 'experiences',+ 'activities',+] as const;++type ReEmbedTableKey = (typeof REEMBED_TABLE_KEYS)[number];++const reEmbedInputSchema = z.object({+ concurrency: z.coerce.number().int().min(1).max(50).optional(),+ endDate: z.coerce.date().optional(),+ limit: z.coerce.number().int().min(1).optional(),+ only: z.array(z.enum(REEMBED_TABLE_KEYS)).optional(),+ startDate: z.coerce.date().optional(),+});++interface ReEmbedStats {+ failed: number;+ skipped: number;+ succeeded: number;+ total: number;+}++const combineConditions = (conditions: Array<SQL | undefined>): SQL | undefined => {+ const filtered = conditions.filter((condition): condition is SQL => condition !== undefined);+ if (filtered.length === 0) return undefined;+ if (filtered.length === 1) return filtered[0];++ return and(...filtered);+};++const normalizeEmbeddable = (value?: string | null): string | undefined => {+ if (typeof value !== 'string') return undefined;+ const trimmed = value.trim();++ return trimmed.length > 0 ? trimmed : undefined;+};++export const reEmbedRouter = router({+ reEmbedMemories: memoryWriteProcedure+ .input(reEmbedInputSchema.optional())+ .mutation(async ({ ctx, input }) => {+ try {+ const options = input ?? {};+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const concurrency = options.concurrency ?? 10;+ const shouldProcess = (key: ReEmbedTableKey) =>+ !options.only || options.only.length === 0 || options.only.includes(key);++ const embedTexts = async (texts: string[]): Promise<number[][]> => {+ if (texts.length === 0) return [];++ const response = await embedUserMemoryTexts({+ input: texts,+ model: embeddingModel,+ runtime: agentRuntime,+ source: 'lambda:userMemories.reEmbed',+ userId: ctx.userId,+ });++ if (response.length !== texts.length) {+ throw new Error('Embedding response length mismatch');+ }++ return response.map((embedding) => {+ if (!embedding) throw new Error('Embedding response length mismatch');++ return embedding;+ });+ };++ const results: Partial<Record<ReEmbedTableKey, ReEmbedStats>> = {};++ const run = async (key: ReEmbedTableKey, handler: () => Promise<ReEmbedStats>) => {+ if (!shouldProcess(key)) return;+ results[key] = await handler();+ };++ // Individual re-embed handlers are appended below.+ await run('userMemories', async () => {+ const where = combineConditions([+ eq(userMemories.userId, ctx.userId),+ notTrashed(userMemories.isDeleted),+ options.startDate ? gte(userMemories.createdAt, options.startDate) : undefined,+ options.endDate ? lte(userMemories.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemories.findMany({+ columns: { details: true, id: true, summary: true },+ limit: options.limit,+ orderBy: [asc(userMemories.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const summaryText = normalizeEmbeddable(row.summary);+ const detailsText = normalizeEmbeddable(row.details);++ try {+ if (!summaryText && !detailsText) {+ await ctx.memoryModel.updateUserMemoryVectors(row.id, {+ detailsVector1024: null,+ summaryVector1024: null,+ });+ skipped += 1;+ return;+ }++ const inputs: string[] = [];+ if (summaryText) inputs.push(summaryText);+ if (detailsText) inputs.push(detailsText);++ const embeddings = await embedTexts(inputs);+ let embedIndex = 0;++ const summaryVector = summaryText ? (embeddings[embedIndex++] ?? null) : null;+ const detailsVector = detailsText ? (embeddings[embedIndex++] ?? null) : null;++ await ctx.memoryModel.updateUserMemoryVectors(row.id, {+ detailsVector1024: detailsVector,+ summaryVector1024: summaryVector,+ });++ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(+ `[memoryRouter.reEmbed] Failed to re-embed user memory ${row.id}`,+ err,+ );+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ await run('contexts', async () => {+ const where = combineConditions([+ eq(userMemoriesContexts.userId, ctx.userId),+ options.startDate ? gte(userMemoriesContexts.createdAt, options.startDate) : undefined,+ options.endDate ? lte(userMemoriesContexts.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemoriesContexts.findMany({+ columns: { description: true, id: true },+ limit: options.limit,+ orderBy: [asc(userMemoriesContexts.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const description = normalizeEmbeddable(row.description);++ try {+ if (!description) {+ await ctx.memoryModel.updateContextVectors(row.id, {+ descriptionVector: null,+ });+ skipped += 1;+ return;+ }++ const [embedding] = await embedTexts([description]);++ await ctx.memoryModel.updateContextVectors(row.id, {+ descriptionVector: embedding ?? null,+ });+ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(`[memoryRouter.reEmbed] Failed to re-embed context ${row.id}`, err);+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ await run('preferences', async () => {+ const where = combineConditions([+ eq(userMemoriesPreferences.userId, ctx.userId),+ options.startDate+ ? gte(userMemoriesPreferences.createdAt, options.startDate)+ : undefined,+ options.endDate ? lte(userMemoriesPreferences.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemoriesPreferences.findMany({+ columns: { conclusionDirectives: true, id: true },+ limit: options.limit,+ orderBy: [asc(userMemoriesPreferences.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const directives = normalizeEmbeddable(row.conclusionDirectives);++ try {+ if (!directives) {+ await ctx.memoryModel.updatePreferenceVectors(row.id, {+ conclusionDirectivesVector: null,+ });+ skipped += 1;+ return;+ }++ const [embedding] = await embedTexts([directives]);+ await ctx.memoryModel.updatePreferenceVectors(row.id, {+ conclusionDirectivesVector: embedding ?? null,+ });+ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(+ `[memoryRouter.reEmbed] Failed to re-embed preference ${row.id}`,+ err,+ );+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ await run('identities', async () => {+ const where = combineConditions([+ eq(userMemoriesIdentities.userId, ctx.userId),+ options.startDate+ ? gte(userMemoriesIdentities.createdAt, options.startDate)+ : undefined,+ options.endDate ? lte(userMemoriesIdentities.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemoriesIdentities.findMany({+ columns: { description: true, id: true },+ limit: options.limit,+ orderBy: [asc(userMemoriesIdentities.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const description = normalizeEmbeddable(row.description);++ try {+ if (!description) {+ await ctx.memoryModel.updateIdentityVectors(row.id, {+ descriptionVector: null,+ });+ skipped += 1;+ return;+ }++ const [embedding] = await embedTexts([description]);+ await ctx.memoryModel.updateIdentityVectors(row.id, {+ descriptionVector: embedding ?? null,+ });+ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(`[memoryRouter.reEmbed] Failed to re-embed identity ${row.id}`, err);+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ await run('activities', async () => {+ const where = combineConditions([+ eq(userMemoriesActivities.userId, ctx.userId),+ options.startDate+ ? gte(userMemoriesActivities.createdAt, options.startDate)+ : undefined,+ options.endDate ? lte(userMemoriesActivities.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemoriesActivities.findMany({+ columns: { feedback: true, id: true, narrative: true },+ limit: options.limit,+ orderBy: [asc(userMemoriesActivities.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const narrative = normalizeEmbeddable(row.narrative);+ const feedback = normalizeEmbeddable(row.feedback);++ try {+ if (!narrative && !feedback) {+ await ctx.memoryModel.updateActivityVectors(row.id, {+ feedbackVector: null,+ narrativeVector: null,+ });+ skipped += 1;+ return;+ }++ const inputs: string[] = [];+ if (narrative) inputs.push(narrative);+ if (feedback) inputs.push(feedback);++ const embeddings = await embedTexts(inputs);+ let embedIndex = 0;++ const narrativeVector = narrative ? (embeddings[embedIndex++] ?? null) : null;+ const feedbackVector = feedback ? (embeddings[embedIndex++] ?? null) : null;++ await ctx.memoryModel.updateActivityVectors(row.id, {+ feedbackVector,+ narrativeVector,+ });+ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(`[memoryRouter.reEmbed] Failed to re-embed activity ${row.id}`, err);+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ await run('experiences', async () => {+ const where = combineConditions([+ eq(userMemoriesExperiences.userId, ctx.userId),+ options.startDate+ ? gte(userMemoriesExperiences.createdAt, options.startDate)+ : undefined,+ options.endDate ? lte(userMemoriesExperiences.createdAt, options.endDate) : undefined,+ ]);++ const rows = await ctx.serverDB.query.userMemoriesExperiences.findMany({+ columns: { action: true, id: true, keyLearning: true, situation: true },+ limit: options.limit,+ orderBy: [asc(userMemoriesExperiences.createdAt)],+ where,+ });++ let succeeded = 0;+ let failed = 0;+ let skipped = 0;++ await pMap(+ rows,+ async (row) => {+ const situation = normalizeEmbeddable(row.situation);+ const action = normalizeEmbeddable(row.action);+ const keyLearning = normalizeEmbeddable(row.keyLearning);++ try {+ if (!situation && !action && !keyLearning) {+ await ctx.memoryModel.updateExperienceVectors(row.id, {+ actionVector: null,+ keyLearningVector: null,+ situationVector: null,+ });+ skipped += 1;+ return;+ }++ const inputs: string[] = [];+ if (situation) inputs.push(situation);+ if (action) inputs.push(action);+ if (keyLearning) inputs.push(keyLearning);++ const embeddings = await embedTexts(inputs);+ let embedIndex = 0;++ const situationVector = situation ? (embeddings[embedIndex++] ?? null) : null;+ const actionVector = action ? (embeddings[embedIndex++] ?? null) : null;+ const keyLearningVector = keyLearning ? (embeddings[embedIndex++] ?? null) : null;++ await ctx.memoryModel.updateExperienceVectors(row.id, {+ actionVector,+ keyLearningVector,+ situationVector,+ });+ succeeded += 1;+ } catch (err) {+ failed += 1;+ console.error(+ `[memoryRouter.reEmbed] Failed to re-embed experience ${row.id}`,+ err,+ );+ }+ },+ { concurrency },+ );++ return {+ failed,+ skipped,+ succeeded,+ total: rows.length,+ } satisfies ReEmbedStats;+ });++ const processedEntries = Object.entries(results) as Array<[ReEmbedTableKey, ReEmbedStats]>;++ if (processedEntries.length === 0) {+ return {+ message: 'No memory records matched re-embed criteria',+ results,+ success: true,+ };+ }++ const aggregate = processedEntries.reduce(+ (acc, [, stats]) => {+ acc.failed += stats.failed;+ acc.skipped += stats.skipped;+ acc.succeeded += stats.succeeded;+ acc.total += stats.total;++ return acc;+ },+ { failed: 0, skipped: 0, succeeded: 0, total: 0 },+ );++ const message =+ aggregate.total === 0+ ? 'No memory records required re-embedding'+ : `Re-embedded ${aggregate.succeeded} of ${aggregate.total} records`;++ return {+ aggregate,+ message,+ results,+ success: true,+ };+ } catch (error) {+ console.error('Failed to re-embed memories:', error);+ return {+ message: `Failed to re-embed memories: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+});diff --git a/apps/server/src/routers/lambda/userMemories/search.ts b/apps/server/src/routers/lambda/userMemories/search.tsnew file mode 100644index 00000000000..2bfc01d31b7--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/search.ts@@ -0,0 +1,156 @@+import {+ DEFAULT_SEARCH_USER_MEMORY_TOP_K,+ DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM,+} from '@lobechat/const';+import { type LobeChatDatabase } from '@lobechat/database';+import type { SearchMemoryResult } from '@lobechat/types';+import { searchMemorySchema } from '@lobechat/types';+import { z } from 'zod';++import type { UserMemoryModel } from '@/database/models/userMemory';+import {+ normalizeUserMemorySearchQueries,+ shouldRunUserMemoryLexicalSearch,+} from '@/database/models/userMemory';+import { UserMemoryTopicRepository } from '@/database/repositories/userMemory';+import { router } from '@/libs/trpc/lambda';+import { getServerDefaultFilesConfig } from '@/server/globalConfig';+import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';+import {+ recordUserMemoryLexicalSearchDecision,+ type UserMemoryLexicalSearchSource,+} from '@/server/services/ftsSearch/observability';+import { embedUserMemoryTexts } from '@/server/services/memory/userMemory/embedding';+import {+ normalizeSearchMemoryParams,+ resolveMemorySearchTopK,+} from '@/server/services/memory/userMemory/searchParams';++import type { MemoryEffort } from './procedures';+import { memorySearchProcedure, normalizeMemoryEffort } from './procedures';+import { rethrowCandidateSearchError } from './utils';++const EMPTY_SEARCH_RESULT: SearchMemoryResult = {+ activities: [],+ contexts: [],+ experiences: [],+ identities: [],+ meta: {+ appliedFilters: {},+ appliedQueries: [],+ layers: {+ activities: { hasMore: false, returned: 0, total: 0 },+ contexts: { hasMore: false, returned: 0, total: 0 },+ experiences: { hasMore: false, returned: 0, total: 0 },+ identities: { hasMore: false, returned: 0, total: 0 },+ preferences: { hasMore: false, returned: 0, total: 0 },+ },+ },+ preferences: [],+};++type MemorySearchContext = {+ memoryModel: UserMemoryModel;+ memoryEffort: MemoryEffort;+ serverDB: LobeChatDatabase;+ userId: string;+};++const searchUserMemories = async (+ ctx: MemorySearchContext,+ input: z.infer<typeof searchMemorySchema>,+ source: UserMemoryLexicalSearchSource,+): Promise<SearchMemoryResult> => {+ const normalizedInput = normalizeSearchMemoryParams(input);+ const { provider, model: embeddingModel } =+ getServerDefaultFilesConfig().embeddingModel || DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM;+ const modelRuntime = await initModelRuntimeFromDB(ctx.serverDB, ctx.userId, provider);+ const normalizedQueries = normalizeUserMemorySearchQueries(normalizedInput.queries);++ const queryEmbeddings =+ normalizedQueries.length > 0+ ? (+ await embedUserMemoryTexts({+ input: normalizedQueries,+ model: embeddingModel,+ runtime: modelRuntime,+ source: 'lambda:userMemories.search',+ userId: ctx.userId,+ })+ ).filter((embedding): embedding is number[] => Boolean(embedding))+ : [];+ const lexicalSearch = shouldRunUserMemoryLexicalSearch(normalizedQueries, queryEmbeddings);+ recordUserMemoryLexicalSearchDecision({+ decision: lexicalSearch ? 'executed' : 'skipped_long_context',+ queryCharacters: Array.from(normalizedQueries.join(' ')).length,+ source,+ });++ const effectiveEffort = normalizeMemoryEffort(normalizedInput.effort ?? ctx.memoryEffort);+ const effortConstrainedLimits = resolveMemorySearchTopK(effectiveEffort, normalizedInput);+ return ctx.memoryModel.searchMemory(+ { ...normalizedInput, queries: normalizedQueries, topK: effortConstrainedLimits },+ queryEmbeddings,+ ) as Promise<SearchMemoryResult>;+};++export const searchRouter = router({+ /**+ * Retrieve memories for a specific topic+ * Uses concatenated user messages (first 7000 chars) as the search query+ */+ retrieveMemoryForTopic: memorySearchProcedure+ .input(z.object({ topicId: z.string() }))+ .query(async ({ ctx, input }) => {+ // Dev-only escape hatch: skip the embedding + memory search triggered by topic+ // load / switch, so chat debugging logs aren't drowned in `text-embedding-3-small`+ // router-runtime output. Only honored in non-production builds.+ if (process.env.NODE_ENV !== 'production' && process.env.DEV_DISABLE_AUTO_MEMORY === '1') {+ console.info('[dev] skip retrieveMemoryForTopic (DEV_DISABLE_AUTO_MEMORY=1)');+ return EMPTY_SEARCH_RESULT;+ }++ try {+ // Get concatenated user messages for this topic+ const userMemoryTopicRepo = new UserMemoryTopicRepository(+ ctx.serverDB,+ ctx.userId,+ ctx.workspaceId ?? undefined,+ );+ const query = await userMemoryTopicRepo.getUserMessagesQueryForTopic(input.topicId);++ if (!query) {+ // No user messages available, return empty result+ return EMPTY_SEARCH_RESULT;+ }++ // Search memories using concatenated user messages+ const searchParams = {+ queries: [query],+ topK: DEFAULT_SEARCH_USER_MEMORY_TOP_K,+ };++ const result = await searchUserMemories(ctx, searchParams, 'topic_retrieval');+ return result;+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to retrieve memory for topic:', error);+ return EMPTY_SEARCH_RESULT;+ }+ }),+ searchMemory: memorySearchProcedure.input(searchMemorySchema).query(async ({ input, ctx }) => {+ try {+ return await searchUserMemories(ctx, input, 'api');+ } catch (error) {+ rethrowCandidateSearchError(error);+ console.error('Failed to retrieve memories:', error);+ return EMPTY_SEARCH_RESULT;+ }+ }),+ toolSearchMemory: memorySearchProcedure+ .input(searchMemorySchema)+ .query(async ({ input, ctx }) => {+ const result = await searchUserMemories(ctx, input, 'tool');+ return result;+ }),+});diff --git a/apps/server/src/routers/lambda/userMemories/toolWrite.ts b/apps/server/src/routers/lambda/userMemories/toolWrite.tsnew file mode 100644index 00000000000..3dc6d31ab34--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/toolWrite.ts@@ -0,0 +1,481 @@+import {+ ActivityMemoryItemSchema,+ AddIdentityActionSchema,+ ContextMemoryItemSchema,+ ExperienceMemoryItemSchema,+ PreferenceMemoryItemSchema,+ RemoveIdentityActionSchema,+ UpdateIdentityToolInputSchema,+} from '@lobechat/memory-user-memory';+import { LayersEnum } from '@lobechat/types';++import {+ type IdentityEntryBasePayload,+ type IdentityEntryPayload,+} from '@/database/models/userMemory';+import { UserMemoryModel } from '@/database/models/userMemory';+import { router } from '@/libs/trpc/lambda';+import type { UserMemoryEmbeddingRuntime } from '@/server/services/memory/userMemory/embedding';+import { embedUserMemoryTexts } from '@/server/services/memory/userMemory/embedding';++import { memoryWriteProcedure } from './procedures';+import { getEmbeddingRuntime } from './utils';++const createEmbedder = (+ agentRuntime: UserMemoryEmbeddingRuntime,+ embeddingModel: string,+ userId: string,+) => {+ return async (value?: string | null): Promise<number[] | undefined> => {+ if (!value || value.trim().length === 0) return undefined;++ const [embedding] = await embedUserMemoryTexts({+ input: [value],+ model: embeddingModel,+ runtime: agentRuntime,+ source: 'lambda:userMemories.tool',+ userId,+ });++ return embedding;+ };+};++export const toolWriteRouter = router({+ toolAddActivityMemory: memoryWriteProcedure+ .input(ActivityMemoryItemSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ const summaryEmbedding = await embed(input.summary);+ const detailsEmbedding = await embed(input.details);+ const narrativeVector = await embed(input.withActivity.narrative);+ const feedbackVector = await embed(input.withActivity.feedback);++ const { activity, memory } = await ctx.memoryModel.createActivityMemory({+ activity: {+ associatedLocations:+ UserMemoryModel.parseAssociatedLocations(input.withActivity.associatedLocations) ??+ null,+ associatedObjects:+ UserMemoryModel.parseAssociatedObjects(input.withActivity.associatedObjects) ?? [],+ associatedSubjects:+ UserMemoryModel.parseAssociatedSubjects(input.withActivity.associatedSubjects) ?? [],+ endsAt: UserMemoryModel.parseDateFromString(input.withActivity.endsAt ?? undefined),+ feedback: input.withActivity.feedback ?? null,+ feedbackVector: feedbackVector ?? null,+ metadata: input.withActivity.metadata ?? null,+ narrative: input.withActivity.narrative ?? null,+ narrativeVector: narrativeVector ?? null,+ notes: input.withActivity.notes ?? null,+ startsAt: UserMemoryModel.parseDateFromString(input.withActivity.startsAt ?? undefined),+ status: input.withActivity.status ?? 'pending',+ tags: input.withActivity.tags ?? input.tags ?? [],+ timezone: input.withActivity.timezone ?? null,+ type: input.withActivity.type ?? 'other',+ },+ details: input.details || '',+ detailsEmbedding,+ memoryCategory: input.memoryCategory,+ memoryLayer: LayersEnum.Activity,+ memoryType: input.memoryType,+ summary: input.summary,+ summaryEmbedding,+ title: input.title,+ });++ return {+ activityId: activity.id,+ memoryId: memory.id,+ message: 'Memory saved successfully',+ success: true,+ };+ } catch (error) {+ console.error('Failed to save memory:', error);+ return {+ message: `Failed to save memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolAddContextMemory: memoryWriteProcedure+ .input(ContextMemoryItemSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ const summaryEmbedding = await embed(input.summary);+ const detailsEmbedding = await embed(input.details);+ const contextDescriptionEmbedding = await embed(input.withContext.description);++ const { context, memory } = await ctx.memoryModel.createContextMemory({+ context: {+ associatedObjects:+ UserMemoryModel.parseAssociatedObjects(input.withContext.associatedObjects) ?? null,+ associatedSubjects:+ UserMemoryModel.parseAssociatedSubjects(input.withContext.associatedSubjects) ?? null,+ currentStatus: input.withContext.currentStatus ?? null,+ description: input.withContext.description ?? null,+ descriptionVector: contextDescriptionEmbedding ?? null,+ metadata: {},+ scoreImpact: input.withContext.scoreImpact ?? null,+ scoreUrgency: input.withContext.scoreUrgency ?? null,+ tags: input.tags ?? [],+ title: input.withContext.title ?? null,+ type: input.withContext.type ?? null,+ },+ details: input.details || '',+ detailsEmbedding,+ memoryCategory: input.memoryCategory,+ memoryLayer: LayersEnum.Context,+ memoryType: input.memoryType,+ summary: input.summary,+ summaryEmbedding,+ title: input.title,+ });++ return {+ contextId: context.id,+ memoryId: memory.id,+ message: 'Memory saved successfully',+ success: true,+ };+ } catch (error) {+ console.error('Failed to save memory:', error);+ return {+ message: `Failed to save memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolAddExperienceMemory: memoryWriteProcedure+ .input(ExperienceMemoryItemSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ const summaryEmbedding = await embed(input.summary);+ const detailsEmbedding = await embed(input.details);+ const situationVector = await embed(input.withExperience.situation);+ const actionVector = await embed(input.withExperience.action);+ const keyLearningVector = await embed(input.withExperience.keyLearning);++ const { experience, memory } = await ctx.memoryModel.createExperienceMemory({+ details: input.details || '',+ detailsEmbedding,+ experience: {+ action: input.withExperience.action ?? null,+ actionVector: actionVector ?? null,+ keyLearning: input.withExperience.keyLearning ?? null,+ keyLearningVector: keyLearningVector ?? null,+ metadata: {},+ possibleOutcome: input.withExperience.possibleOutcome ?? null,+ reasoning: input.withExperience.reasoning ?? null,+ scoreConfidence: input.withExperience.scoreConfidence ?? null,+ situation: input.withExperience.situation ?? null,+ situationVector: situationVector ?? null,+ tags: input.tags ?? [],+ type: input.memoryType,+ },+ memoryCategory: input.memoryCategory,+ memoryLayer: LayersEnum.Experience,+ memoryType: input.memoryType,+ summary: input.summary,+ summaryEmbedding,+ title: input.title,+ });++ return {+ experienceId: experience.id,+ memoryId: memory.id,+ message: 'Memory saved successfully',+ success: true,+ };+ } catch (error) {+ console.error('Failed to save memory:', error);+ return {+ message: `Failed to save memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolAddIdentityMemory: memoryWriteProcedure+ .input(AddIdentityActionSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ const summaryEmbedding = await embed(input.summary);+ const detailsEmbedding = await embed(input.details);+ const descriptionEmbedding = await embed(input.withIdentity.description);++ const identityMetadata: Record<string, unknown> = {};+ if (+ input.withIdentity.scoreConfidence !== null &&+ input.withIdentity.scoreConfidence !== undefined+ ) {+ identityMetadata.scoreConfidence = input.withIdentity.scoreConfidence;+ }+ if (+ input.withIdentity.sourceEvidence !== null &&+ input.withIdentity.sourceEvidence !== undefined+ ) {+ identityMetadata.sourceEvidence = input.withIdentity.sourceEvidence;+ }++ const { identityId, userMemoryId } = await ctx.memoryModel.addIdentityEntry({+ base: {+ details: input.details,+ detailsVector1024: detailsEmbedding ?? null,+ memoryCategory: input.memoryCategory,+ memoryLayer: LayersEnum.Identity,+ memoryType: input.memoryType,+ metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,+ summary: input.summary,+ summaryVector1024: summaryEmbedding ?? null,+ tags: input.tags,+ title: input.title,+ },+ identity: {+ description: input.withIdentity.description,+ descriptionVector: descriptionEmbedding ?? null,+ episodicDate: input.withIdentity.episodicDate,+ metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,+ relationship: input.withIdentity.relationship,+ role: input.withIdentity.role,+ tags: input.tags,+ type: input.withIdentity.type,+ },+ });++ return {+ identityId,+ memoryId: userMemoryId,+ message: 'Identity memory saved successfully',+ success: true,+ };+ } catch (error) {+ console.error('Failed to save identity memory:', error);+ return {+ message: `Failed to save identity memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolAddPreferenceMemory: memoryWriteProcedure+ .input(PreferenceMemoryItemSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ const summaryEmbedding = await embed(input.summary);+ const detailsEmbedding = await embed(input.details);+ const conclusionVector = await embed(input.withPreference.conclusionDirectives);++ const suggestionsText =+ input.withPreference?.suggestions?.length && input.withPreference?.suggestions?.length > 0+ ? input.withPreference?.suggestions?.join('\n')+ : null;++ const metadata = {+ appContext: input.withPreference.appContext,+ extractedScopes: input.withPreference.extractedScopes,+ originContext: input.withPreference.originContext,+ } satisfies Record<string, unknown>;++ const { memory, preference } = await ctx.memoryModel.createPreferenceMemory({+ details: input.details || '',+ detailsEmbedding,+ memoryCategory: input.memoryCategory,+ memoryLayer: LayersEnum.Preference,+ memoryType: input.memoryType,+ preference: {+ conclusionDirectives: input.withPreference.conclusionDirectives || '',+ conclusionDirectivesVector: conclusionVector ?? null,+ metadata,+ scorePriority: input.withPreference.scorePriority ?? null,+ suggestions: suggestionsText,+ tags: input.tags,+ type: input.memoryType,+ },+ summary: input.summary,+ summaryEmbedding,+ title: input.title,+ });++ return {+ memoryId: memory.id,+ message: 'Memory saved successfully',+ preferenceId: preference.id,+ success: true,+ };+ } catch (error) {+ console.error('Failed to save memory:', error);+ return {+ message: `Failed to save memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolRemoveIdentityMemory: memoryWriteProcedure+ .input(RemoveIdentityActionSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const removed = await ctx.memoryModel.removeIdentityEntry(input.id);++ if (!removed) {+ return {+ message: 'Identity memory not found',+ success: false,+ };+ }++ return {+ identityId: input.id,+ message: 'Identity memory removed successfully',+ reason: input.reason,+ success: true,+ };+ } catch (error) {+ console.error('Failed to remove identity memory:', error);+ return {+ message: `Failed to remove identity memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+ toolUpdateIdentityMemory: memoryWriteProcedure+ .input(UpdateIdentityToolInputSchema)+ .mutation(async ({ input, ctx }) => {+ try {+ const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(+ ctx.serverDB,+ ctx.userId,+ );+ const embed = createEmbedder(agentRuntime, embeddingModel, ctx.userId);++ let summaryVector1024: number[] | null | undefined;+ if (input.set.summary !== undefined) {+ const vector = await embed(input.set.summary);+ summaryVector1024 = vector ?? null;+ }++ let detailsVector1024: number[] | null | undefined;+ if (input.set.details !== undefined) {+ const vector = await embed(input.set.details);+ detailsVector1024 = vector ?? null;+ }++ let descriptionVector: number[] | null | undefined;+ if (input.set.withIdentity.description !== undefined) {+ const vector = await embed(input.set.withIdentity.description);+ descriptionVector = vector ?? null;+ }++ const metadataUpdates: Record<string, unknown> = {};+ if (Object.hasOwn(input.set.withIdentity, 'scoreConfidence')) {+ metadataUpdates.scoreConfidence = input.set.withIdentity.scoreConfidence ?? null;+ }+ if (Object.hasOwn(input.set.withIdentity, 'sourceEvidence')) {+ metadataUpdates.sourceEvidence = input.set.withIdentity.sourceEvidence ?? null;+ }++ const identityPayload: Partial<IdentityEntryPayload> = {};+ if (input.set.withIdentity.description !== undefined) {+ identityPayload.description = input.set.withIdentity.description;+ identityPayload.descriptionVector = descriptionVector;+ }+ if (input.set.withIdentity.episodicDate !== undefined) {+ identityPayload.episodicDate = input.set.withIdentity.episodicDate;+ }+ if (input.set.withIdentity.relationship !== undefined) {+ identityPayload.relationship = input.set.withIdentity.relationship;+ }+ if (input.set.withIdentity.role !== undefined) {+ identityPayload.role = input.set.withIdentity.role;+ }+ if (input.set.tags !== undefined) {+ identityPayload.tags = input.set.tags;+ }+ if (input.set.withIdentity.type !== undefined) {+ identityPayload.type = input.set.withIdentity.type;+ }+ if (Object.keys(metadataUpdates).length > 0) {+ identityPayload.metadata = metadataUpdates;+ }++ const basePayload: Partial<IdentityEntryBasePayload> = {};+ if (input.set.details !== undefined) {+ basePayload.details = input.set.details;+ basePayload.detailsVector1024 = detailsVector1024;+ }+ if (input.set.memoryCategory !== undefined) {+ basePayload.memoryCategory = input.set.memoryCategory;+ }+ if (input.set.memoryType !== undefined) {+ basePayload.memoryType = input.set.memoryType;+ }+ if (input.set.summary !== undefined) {+ basePayload.summary = input.set.summary;+ basePayload.summaryVector1024 = summaryVector1024;+ }+ if (input.set.tags !== undefined) {+ basePayload.tags = input.set.tags;+ }+ if (input.set.title !== undefined) {+ basePayload.title = input.set.title;+ }+ if (Object.keys(metadataUpdates).length > 0) {+ basePayload.metadata = metadataUpdates;+ }++ const updated = await ctx.memoryModel.updateIdentityEntry({+ base: Object.keys(basePayload).length > 0 ? basePayload : undefined,+ identity: Object.keys(identityPayload).length > 0 ? identityPayload : undefined,+ identityId: input.id,+ mergeStrategy: input.mergeStrategy,+ preserveOmittedFields: true,+ });++ if (!updated) {+ return {+ message: 'Identity memory not found',+ success: false,+ };+ }++ return {+ identityId: input.id,+ message: 'Identity memory updated successfully',+ success: true,+ };+ } catch (error) {+ console.error('Failed to update identity memory:', error);+ return {+ message: `Failed to update identity memory: ${(error as Error).message}`,+ success: false,+ };+ }+ }),+});diff --git a/apps/server/src/routers/lambda/userMemories/utils.ts b/apps/server/src/routers/lambda/userMemories/utils.tsnew file mode 100644index 00000000000..5fb4338ae73--- /dev/null+++ b/apps/server/src/routers/lambda/userMemories/utils.ts@@ -0,0 +1,25 @@+import { BRANDING_PROVIDER, ENABLE_BUSINESS_FEATURES } from '@lobechat/business-const';+import { DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM } from '@lobechat/const';+import { type LobeChatDatabase } from '@lobechat/database';++import { FtsSearchCandidateError } from '@/database/repositories/ftsSearch';+import { getServerDefaultFilesConfig } from '@/server/globalConfig';+import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';++export const getEmbeddingRuntime = async (serverDB: LobeChatDatabase, userId: string) => {+ const { provider, model: embeddingModel } =+ getServerDefaultFilesConfig().embeddingModel || DEFAULT_USER_MEMORY_EMBEDDING_MODEL_ITEM;+ // Read user's provider config from database+ const agentRuntime = await initModelRuntimeFromDB(+ serverDB,+ userId,+ ENABLE_BUSINESS_FEATURES ? BRANDING_PROVIDER : provider,+ );++ return { agentRuntime, embeddingModel };+};++/** Candidate-provider outages must not be converted into successful empty memory responses. */+export const rethrowCandidateSearchError = (error: unknown) => {+ if (error instanceof FtsSearchCandidateError) throw error;+};