@lobehub/lobehub 2.0.0-next.341 → 2.0.0-next.343
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -0
- package/changelog/v1.json +17 -0
- package/package.json +1 -1
- package/packages/database/src/models/__tests__/userMemories.test.ts +62 -5
- package/packages/database/src/models/agentCronJob.ts +9 -9
- package/packages/database/src/models/userMemory/__tests__/identity.test.ts +5 -5
- package/packages/database/src/models/userMemory/experience.ts +91 -1
- package/packages/database/src/models/userMemory/identity.ts +93 -2
- package/packages/database/src/models/userMemory/model.ts +27 -8
- package/packages/types/src/userMemory/experience.ts +25 -0
- package/packages/types/src/userMemory/identity.ts +27 -0
- package/packages/types/src/userMemory/index.ts +1 -0
- package/packages/types/src/userMemory/shared.ts +30 -0
- package/src/app/[variants]/(main)/group/profile/features/Header/index.tsx +3 -4
- package/src/app/[variants]/(main)/home/features/InputArea/SkillInstallBanner.tsx +7 -8
- package/src/app/[variants]/(main)/memory/(home)/features/Persona/PersonaDetail.tsx +58 -0
- package/src/app/[variants]/(main)/memory/(home)/features/Persona/PersonaHeader.tsx +22 -0
- package/src/app/[variants]/(main)/memory/(home)/features/Persona/PersonaSummary.tsx +43 -0
- package/src/app/[variants]/(main)/memory/(home)/features/Persona/index.tsx +53 -0
- package/src/app/[variants]/(main)/memory/(home)/features/RoleTagCloud/index.tsx +2 -2
- package/src/app/[variants]/(main)/memory/(home)/index.tsx +15 -3
- package/src/app/[variants]/(main)/memory/experiences/features/List/GridView/ExperienceCard.tsx +3 -3
- package/src/app/[variants]/(main)/memory/experiences/features/List/GridView/index.tsx +3 -3
- package/src/app/[variants]/(main)/memory/experiences/features/List/TimelineView/ExperienceCard.tsx +3 -3
- package/src/app/[variants]/(main)/memory/experiences/features/List/TimelineView/index.tsx +3 -3
- package/src/app/[variants]/(main)/memory/features/SourceLink.tsx +2 -11
- package/src/app/[variants]/(main)/memory/features/TimeLineView/TimeLineCard.tsx +2 -9
- package/src/app/[variants]/(main)/memory/identities/features/IdentityRightPanel.tsx +1 -1
- package/src/app/[variants]/(main)/memory/identities/features/List/GridView/IdentityCard.tsx +5 -4
- package/src/app/[variants]/(main)/memory/identities/features/List/GridView/index.tsx +3 -3
- package/src/app/[variants]/(main)/memory/identities/features/List/TimelineView/IdentityCard.tsx +6 -6
- package/src/app/[variants]/(main)/memory/identities/features/List/TimelineView/index.tsx +6 -4
- package/src/app/[variants]/(main)/settings/profile/index.tsx +8 -8
- package/src/app/[variants]/(main)/settings/provider/features/ProviderConfig/index.tsx +0 -1
- package/src/app/[variants]/(main)/settings/skill/features/Actions.tsx +0 -1
- package/src/app/[variants]/(main)/settings/skill/features/KlavisSkillItem.tsx +9 -10
- package/src/app/[variants]/(main)/settings/skill/features/LobehubSkillItem.tsx +9 -10
- package/src/app/[variants]/(main)/settings/skill/features/McpSkillItem.tsx +4 -5
- package/src/app/[variants]/(main)/settings/skill/features/SkillList.tsx +4 -5
- package/src/app/[variants]/share/t/[id]/SharedMessageList.tsx +1 -4
- package/src/app/[variants]/share/t/[id]/_layout/index.tsx +47 -121
- package/src/app/[variants]/share/t/[id]/_layout/style.ts +59 -0
- package/src/app/[variants]/share/t/[id]/features/Portal/index.tsx +4 -5
- package/src/app/[variants]/share/t/[id]/index.tsx +30 -37
- package/src/components/404/index.tsx +15 -9
- package/src/components/DragUpload/index.tsx +15 -16
- package/src/features/EditorCanvas/DocumentIdMode.tsx +1 -2
- package/src/features/IntegrationDetailModal/index.tsx +11 -12
- package/src/features/PageEditor/Copilot/Toolbar.tsx +1 -1
- package/src/features/ResourceManager/index.tsx +13 -6
- package/src/features/ShareModal/ShareImage/Preview.tsx +19 -28
- package/src/features/ShareModal/ShareImage/style.ts +4 -2
- package/src/features/ShareModal/index.tsx +5 -1
- package/src/features/ShareModal/style.ts +1 -0
- package/src/features/ShareModal/useContainerStyles.ts +1 -1
- package/src/features/SharePopover/index.tsx +16 -9
- package/src/features/SharePopover/style.ts +2 -2
- package/src/features/SkillStore/CommunityList/Item.tsx +2 -2
- package/src/features/SkillStore/LobeHubList/Item.tsx +2 -2
- package/src/features/SkillStore/LobeHubList/index.tsx +2 -3
- package/src/features/SkillStore/style.ts +4 -4
- package/src/layout/GlobalProvider/ServerVersionOutdatedAlert.tsx +28 -20
- package/src/server/routers/lambda/userMemories.ts +61 -5
- package/src/server/routers/lambda/userMemory.ts +5 -1
- package/src/services/chat/index.ts +2 -2
- package/src/services/userMemory/index.ts +25 -1
- package/src/store/chat/slices/aiChat/actions/streamingExecutor.ts +0 -1
- package/src/store/userMemory/initialState.ts +22 -52
- package/src/store/userMemory/slices/context/action.ts +1 -1
- package/src/store/userMemory/slices/context/index.ts +1 -0
- package/src/store/userMemory/slices/context/initialState.ts +22 -0
- package/src/store/userMemory/slices/experience/action.ts +10 -22
- package/src/store/userMemory/slices/experience/index.ts +1 -0
- package/src/store/userMemory/slices/experience/initialState.ts +22 -0
- package/src/store/userMemory/slices/home/action.ts +17 -0
- package/src/store/userMemory/slices/identity/action.ts +36 -24
- package/src/store/userMemory/slices/identity/initialState.ts +7 -4
- package/src/store/userMemory/slices/preference/action.ts +1 -1
- package/src/store/userMemory/slices/preference/index.ts +1 -0
- package/src/store/userMemory/slices/preference/initialState.ts +22 -0
- package/src/styles/antdOverride.ts +9 -0
- package/src/server/routers/lambda/userMemories/index.ts +0 -13
- package/src/server/routers/lambda/userMemories/reembed.ts +0 -440
- package/src/server/routers/lambda/userMemories/search.ts +0 -117
- package/src/server/routers/lambda/userMemories/shared.ts +0 -63
- package/src/server/routers/lambda/userMemories/tools.ts +0 -410
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import { type LobeChatDatabase } from '@lobechat/database';
|
|
2
|
-
import { type z } from 'zod';
|
|
3
|
-
|
|
4
|
-
import { DEFAULT_FILE_EMBEDDING_MODEL_ITEM } from '@/const/settings/knowledge';
|
|
5
|
-
import { type UserMemoryModel } from '@/database/models/userMemory';
|
|
6
|
-
import { getServerDefaultFilesConfig } from '@/server/globalConfig';
|
|
7
|
-
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
|
|
8
|
-
import { type SearchMemoryResult, searchMemorySchema } from '@/types/userMemory';
|
|
9
|
-
|
|
10
|
-
import { EMBEDDING_VECTOR_DIMENSION, memoryProcedure, router } from './shared';
|
|
11
|
-
|
|
12
|
-
type MemorySearchContext = {
|
|
13
|
-
memoryModel: UserMemoryModel;
|
|
14
|
-
serverDB: LobeChatDatabase;
|
|
15
|
-
userId: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type MemorySearchResult = Awaited<ReturnType<UserMemoryModel['searchWithEmbedding']>>;
|
|
19
|
-
|
|
20
|
-
const EMPTY_SEARCH_RESULT: SearchMemoryResult = {
|
|
21
|
-
contexts: [],
|
|
22
|
-
experiences: [],
|
|
23
|
-
preferences: [],
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const mapMemorySearchResult = (layeredResults: MemorySearchResult): SearchMemoryResult => {
|
|
27
|
-
return {
|
|
28
|
-
contexts: layeredResults.contexts.map((context) => ({
|
|
29
|
-
accessedAt: context.accessedAt,
|
|
30
|
-
associatedObjects: context.associatedObjects,
|
|
31
|
-
associatedSubjects: context.associatedSubjects,
|
|
32
|
-
createdAt: context.createdAt,
|
|
33
|
-
currentStatus: context.currentStatus,
|
|
34
|
-
description: context.description,
|
|
35
|
-
id: context.id,
|
|
36
|
-
metadata: context.metadata,
|
|
37
|
-
scoreImpact: context.scoreImpact,
|
|
38
|
-
scoreUrgency: context.scoreUrgency,
|
|
39
|
-
tags: context.tags,
|
|
40
|
-
title: context.title,
|
|
41
|
-
type: context.type,
|
|
42
|
-
updatedAt: context.updatedAt,
|
|
43
|
-
userMemoryIds: Array.isArray(context.userMemoryIds)
|
|
44
|
-
? (context.userMemoryIds as string[])
|
|
45
|
-
: null,
|
|
46
|
-
})),
|
|
47
|
-
experiences: layeredResults.experiences.map((experience) => ({
|
|
48
|
-
accessedAt: experience.accessedAt,
|
|
49
|
-
action: experience.action,
|
|
50
|
-
createdAt: experience.createdAt,
|
|
51
|
-
id: experience.id,
|
|
52
|
-
keyLearning: experience.keyLearning,
|
|
53
|
-
metadata: experience.metadata,
|
|
54
|
-
possibleOutcome: experience.possibleOutcome,
|
|
55
|
-
reasoning: experience.reasoning,
|
|
56
|
-
scoreConfidence: experience.scoreConfidence,
|
|
57
|
-
situation: experience.situation,
|
|
58
|
-
tags: experience.tags,
|
|
59
|
-
type: experience.type,
|
|
60
|
-
updatedAt: experience.updatedAt,
|
|
61
|
-
userMemoryId: experience.userMemoryId,
|
|
62
|
-
})),
|
|
63
|
-
preferences: layeredResults.preferences.map((preference) => ({
|
|
64
|
-
accessedAt: preference.accessedAt,
|
|
65
|
-
conclusionDirectives: preference.conclusionDirectives,
|
|
66
|
-
createdAt: preference.createdAt,
|
|
67
|
-
id: preference.id,
|
|
68
|
-
metadata: preference.metadata,
|
|
69
|
-
scorePriority: preference.scorePriority,
|
|
70
|
-
suggestions: preference.suggestions,
|
|
71
|
-
tags: preference.tags,
|
|
72
|
-
type: preference.type,
|
|
73
|
-
updatedAt: preference.updatedAt,
|
|
74
|
-
userMemoryId: preference.userMemoryId,
|
|
75
|
-
})),
|
|
76
|
-
} satisfies SearchMemoryResult;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export const searchUserMemories = async (
|
|
80
|
-
ctx: MemorySearchContext,
|
|
81
|
-
input: z.infer<typeof searchMemorySchema>,
|
|
82
|
-
): Promise<SearchMemoryResult> => {
|
|
83
|
-
const { provider, model: embeddingModel } =
|
|
84
|
-
getServerDefaultFilesConfig().embeddingModel || DEFAULT_FILE_EMBEDDING_MODEL_ITEM;
|
|
85
|
-
// Read user's provider config from database
|
|
86
|
-
const agentRuntime = await initModelRuntimeFromDB(ctx.serverDB, ctx.userId, provider);
|
|
87
|
-
|
|
88
|
-
const queryEmbeddings = await agentRuntime.embeddings({
|
|
89
|
-
dimensions: EMBEDDING_VECTOR_DIMENSION,
|
|
90
|
-
input: input.query,
|
|
91
|
-
model: embeddingModel,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const limits = {
|
|
95
|
-
contexts: input.topK?.contexts,
|
|
96
|
-
experiences: input.topK?.experiences,
|
|
97
|
-
preferences: input.topK?.preferences,
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const layeredResults = await ctx.memoryModel.searchWithEmbedding({
|
|
101
|
-
embedding: queryEmbeddings?.[0],
|
|
102
|
-
limits,
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
return mapMemorySearchResult(layeredResults);
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const searchRouter = router({
|
|
109
|
-
searchMemory: memoryProcedure.input(searchMemorySchema).query(async ({ input, ctx }) => {
|
|
110
|
-
try {
|
|
111
|
-
return await searchUserMemories(ctx, input);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
console.error('Failed to retrieve memories:', error);
|
|
114
|
-
return EMPTY_SEARCH_RESULT;
|
|
115
|
-
}
|
|
116
|
-
}),
|
|
117
|
-
});
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { type LobeChatDatabase } from '@lobechat/database';
|
|
2
|
-
import { type SQL, and } from 'drizzle-orm';
|
|
3
|
-
|
|
4
|
-
import { DEFAULT_FILE_EMBEDDING_MODEL_ITEM } from '@/const/settings/knowledge';
|
|
5
|
-
import { UserMemoryModel } from '@/database/models/userMemory';
|
|
6
|
-
import { authedProcedure } from '@/libs/trpc/lambda';
|
|
7
|
-
import { keyVaults, serverDatabase } from '@/libs/trpc/lambda/middleware';
|
|
8
|
-
import { getServerDefaultFilesConfig } from '@/server/globalConfig';
|
|
9
|
-
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
|
|
10
|
-
|
|
11
|
-
export const EMBEDDING_VECTOR_DIMENSION = 1024;
|
|
12
|
-
|
|
13
|
-
export const memoryProcedure = authedProcedure
|
|
14
|
-
.use(serverDatabase)
|
|
15
|
-
.use(keyVaults)
|
|
16
|
-
.use(async (opts) => {
|
|
17
|
-
const { ctx } = opts;
|
|
18
|
-
return opts.next({
|
|
19
|
-
ctx: {
|
|
20
|
-
memoryModel: new UserMemoryModel(ctx.serverDB, ctx.userId),
|
|
21
|
-
},
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
export const getEmbeddingRuntime = async (serverDB: LobeChatDatabase, userId: string) => {
|
|
26
|
-
const { provider, model: embeddingModel } =
|
|
27
|
-
getServerDefaultFilesConfig().embeddingModel || DEFAULT_FILE_EMBEDDING_MODEL_ITEM;
|
|
28
|
-
// Read user's provider config from database
|
|
29
|
-
const agentRuntime = await initModelRuntimeFromDB(serverDB, userId, provider);
|
|
30
|
-
|
|
31
|
-
return { agentRuntime, embeddingModel };
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export const createEmbedder = (agentRuntime: any, embeddingModel: string) => {
|
|
35
|
-
return async (value?: string | null): Promise<number[] | undefined> => {
|
|
36
|
-
if (!value || value.trim().length === 0) return undefined;
|
|
37
|
-
|
|
38
|
-
const embeddings = await agentRuntime.embeddings({
|
|
39
|
-
dimensions: EMBEDDING_VECTOR_DIMENSION,
|
|
40
|
-
input: value,
|
|
41
|
-
model: embeddingModel,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
return embeddings?.[0];
|
|
45
|
-
};
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export const combineConditions = (conditions: Array<SQL | undefined>): SQL | undefined => {
|
|
49
|
-
const filtered = conditions.filter((condition): condition is SQL => condition !== undefined);
|
|
50
|
-
if (filtered.length === 0) return undefined;
|
|
51
|
-
if (filtered.length === 1) return filtered[0];
|
|
52
|
-
|
|
53
|
-
return and(...filtered);
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
export const normalizeEmbeddable = (value?: string | null): string | undefined => {
|
|
57
|
-
if (typeof value !== 'string') return undefined;
|
|
58
|
-
const trimmed = value.trim();
|
|
59
|
-
|
|
60
|
-
return trimmed.length > 0 ? trimmed : undefined;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export { router } from '@/libs/trpc/lambda';
|
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AddIdentityActionSchema,
|
|
3
|
-
ContextMemoryItemSchema,
|
|
4
|
-
ExperienceMemoryItemSchema,
|
|
5
|
-
PreferenceMemoryItemSchema,
|
|
6
|
-
RemoveIdentityActionSchema,
|
|
7
|
-
UpdateIdentityActionSchema,
|
|
8
|
-
} from '@lobechat/memory-user-memory';
|
|
9
|
-
import { LayersEnum } from '@lobechat/types';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
type IdentityEntryBasePayload,
|
|
13
|
-
type IdentityEntryPayload,
|
|
14
|
-
UserMemoryModel,
|
|
15
|
-
} from '@/database/models/userMemory';
|
|
16
|
-
import { searchMemorySchema } from '@/types/userMemory';
|
|
17
|
-
|
|
18
|
-
import { searchUserMemories } from './search';
|
|
19
|
-
import { createEmbedder, getEmbeddingRuntime, memoryProcedure, router } from './shared';
|
|
20
|
-
|
|
21
|
-
export const toolsRouter = router({
|
|
22
|
-
addContextMemory: memoryProcedure
|
|
23
|
-
.input(ContextMemoryItemSchema)
|
|
24
|
-
.mutation(async ({ input, ctx }) => {
|
|
25
|
-
try {
|
|
26
|
-
const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(
|
|
27
|
-
ctx.serverDB,
|
|
28
|
-
ctx.userId,
|
|
29
|
-
);
|
|
30
|
-
const embed = createEmbedder(agentRuntime, embeddingModel);
|
|
31
|
-
|
|
32
|
-
const summaryEmbedding = await embed(input.summary);
|
|
33
|
-
const detailsEmbedding = await embed(input.details);
|
|
34
|
-
const contextDescriptionEmbedding = await embed(input.withContext.description);
|
|
35
|
-
|
|
36
|
-
const { context, memory } = await ctx.memoryModel.createContextMemory({
|
|
37
|
-
context: {
|
|
38
|
-
associatedObjects:
|
|
39
|
-
UserMemoryModel.parseAssociatedObjects(input.withContext.associatedObjects) ?? null,
|
|
40
|
-
associatedSubjects:
|
|
41
|
-
UserMemoryModel.parseAssociatedSubjects(input.withContext.associatedSubjects) ?? null,
|
|
42
|
-
currentStatus: input.withContext.currentStatus ?? null,
|
|
43
|
-
description: input.withContext.description ?? null,
|
|
44
|
-
descriptionVector: contextDescriptionEmbedding ?? null,
|
|
45
|
-
metadata: {},
|
|
46
|
-
scoreImpact: input.withContext.scoreImpact ?? null,
|
|
47
|
-
scoreUrgency: input.withContext.scoreUrgency ?? null,
|
|
48
|
-
tags: input.tags ?? [],
|
|
49
|
-
title: input.withContext.title ?? null,
|
|
50
|
-
type: input.withContext.type ?? null,
|
|
51
|
-
},
|
|
52
|
-
details: input.details || '',
|
|
53
|
-
detailsEmbedding,
|
|
54
|
-
memoryCategory: input.memoryCategory,
|
|
55
|
-
memoryLayer: LayersEnum.Context,
|
|
56
|
-
memoryType: input.memoryType,
|
|
57
|
-
summary: input.summary,
|
|
58
|
-
summaryEmbedding,
|
|
59
|
-
title: input.title,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
return {
|
|
63
|
-
contextId: context.id,
|
|
64
|
-
memoryId: memory.id,
|
|
65
|
-
message: 'Memory saved successfully',
|
|
66
|
-
success: true,
|
|
67
|
-
};
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error('Failed to save memory:', error);
|
|
70
|
-
return {
|
|
71
|
-
message: `Failed to save memory: ${(error as Error).message}`,
|
|
72
|
-
success: false,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}),
|
|
76
|
-
|
|
77
|
-
addExperienceMemory: memoryProcedure
|
|
78
|
-
.input(ExperienceMemoryItemSchema)
|
|
79
|
-
.mutation(async ({ input, ctx }) => {
|
|
80
|
-
try {
|
|
81
|
-
const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(
|
|
82
|
-
ctx.serverDB,
|
|
83
|
-
ctx.userId,
|
|
84
|
-
);
|
|
85
|
-
const embed = createEmbedder(agentRuntime, embeddingModel);
|
|
86
|
-
|
|
87
|
-
const summaryEmbedding = await embed(input.summary);
|
|
88
|
-
const detailsEmbedding = await embed(input.details);
|
|
89
|
-
const situationVector = await embed(input.withExperience.situation);
|
|
90
|
-
const actionVector = await embed(input.withExperience.action);
|
|
91
|
-
const keyLearningVector = await embed(input.withExperience.keyLearning);
|
|
92
|
-
|
|
93
|
-
const { experience, memory } = await ctx.memoryModel.createExperienceMemory({
|
|
94
|
-
details: input.details || '',
|
|
95
|
-
detailsEmbedding,
|
|
96
|
-
experience: {
|
|
97
|
-
action: input.withExperience.action ?? null,
|
|
98
|
-
actionVector: actionVector ?? null,
|
|
99
|
-
keyLearning: input.withExperience.keyLearning ?? null,
|
|
100
|
-
keyLearningVector: keyLearningVector ?? null,
|
|
101
|
-
metadata: {},
|
|
102
|
-
possibleOutcome: input.withExperience.possibleOutcome ?? null,
|
|
103
|
-
reasoning: input.withExperience.reasoning ?? null,
|
|
104
|
-
scoreConfidence: input.withExperience.scoreConfidence ?? null,
|
|
105
|
-
situation: input.withExperience.situation ?? null,
|
|
106
|
-
situationVector: situationVector ?? null,
|
|
107
|
-
tags: input.tags ?? [],
|
|
108
|
-
type: input.memoryType,
|
|
109
|
-
},
|
|
110
|
-
memoryCategory: input.memoryCategory,
|
|
111
|
-
memoryLayer: LayersEnum.Experience,
|
|
112
|
-
memoryType: input.memoryType,
|
|
113
|
-
summary: input.summary,
|
|
114
|
-
summaryEmbedding,
|
|
115
|
-
title: input.title,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
experienceId: experience.id,
|
|
120
|
-
memoryId: memory.id,
|
|
121
|
-
message: 'Memory saved successfully',
|
|
122
|
-
success: true,
|
|
123
|
-
};
|
|
124
|
-
} catch (error) {
|
|
125
|
-
console.error('Failed to save memory:', error);
|
|
126
|
-
return {
|
|
127
|
-
message: `Failed to save memory: ${(error as Error).message}`,
|
|
128
|
-
success: false,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
}),
|
|
132
|
-
|
|
133
|
-
addIdentityMemory: memoryProcedure
|
|
134
|
-
.input(AddIdentityActionSchema)
|
|
135
|
-
.mutation(async ({ input, ctx }) => {
|
|
136
|
-
try {
|
|
137
|
-
const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(
|
|
138
|
-
ctx.serverDB,
|
|
139
|
-
ctx.userId,
|
|
140
|
-
);
|
|
141
|
-
const embed = createEmbedder(agentRuntime, embeddingModel);
|
|
142
|
-
|
|
143
|
-
const summaryEmbedding = await embed(input.summary);
|
|
144
|
-
const detailsEmbedding = await embed(input.details);
|
|
145
|
-
const descriptionEmbedding = await embed(input.withIdentity.description);
|
|
146
|
-
|
|
147
|
-
const identityMetadata: Record<string, unknown> = {};
|
|
148
|
-
if (
|
|
149
|
-
input.withIdentity.scoreConfidence !== null &&
|
|
150
|
-
input.withIdentity.scoreConfidence !== undefined
|
|
151
|
-
) {
|
|
152
|
-
identityMetadata.scoreConfidence = input.withIdentity.scoreConfidence;
|
|
153
|
-
}
|
|
154
|
-
if (
|
|
155
|
-
input.withIdentity.sourceEvidence !== null &&
|
|
156
|
-
input.withIdentity.sourceEvidence !== undefined
|
|
157
|
-
) {
|
|
158
|
-
identityMetadata.sourceEvidence = input.withIdentity.sourceEvidence;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const { identityId, userMemoryId } = await ctx.memoryModel.addIdentityEntry({
|
|
162
|
-
base: {
|
|
163
|
-
details: input.details,
|
|
164
|
-
detailsVector1024: detailsEmbedding ?? null,
|
|
165
|
-
memoryCategory: input.memoryCategory,
|
|
166
|
-
memoryLayer: LayersEnum.Identity,
|
|
167
|
-
memoryType: input.memoryType,
|
|
168
|
-
metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,
|
|
169
|
-
summary: input.summary,
|
|
170
|
-
summaryVector1024: summaryEmbedding ?? null,
|
|
171
|
-
tags: input.tags,
|
|
172
|
-
title: input.title,
|
|
173
|
-
},
|
|
174
|
-
identity: {
|
|
175
|
-
description: input.withIdentity.description,
|
|
176
|
-
descriptionVector: descriptionEmbedding ?? null,
|
|
177
|
-
episodicDate: input.withIdentity.episodicDate,
|
|
178
|
-
metadata: Object.keys(identityMetadata).length > 0 ? identityMetadata : undefined,
|
|
179
|
-
relationship: input.withIdentity.relationship,
|
|
180
|
-
role: input.withIdentity.role,
|
|
181
|
-
tags: input.tags,
|
|
182
|
-
type: input.withIdentity.type,
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
return {
|
|
187
|
-
identityId,
|
|
188
|
-
memoryId: userMemoryId,
|
|
189
|
-
message: 'Identity memory saved successfully',
|
|
190
|
-
success: true,
|
|
191
|
-
};
|
|
192
|
-
} catch (error) {
|
|
193
|
-
console.error('Failed to save identity memory:', error);
|
|
194
|
-
return {
|
|
195
|
-
message: `Failed to save identity memory: ${(error as Error).message}`,
|
|
196
|
-
success: false,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
}),
|
|
200
|
-
|
|
201
|
-
addPreferenceMemory: memoryProcedure
|
|
202
|
-
.input(PreferenceMemoryItemSchema)
|
|
203
|
-
.mutation(async ({ input, ctx }) => {
|
|
204
|
-
try {
|
|
205
|
-
const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(
|
|
206
|
-
ctx.serverDB,
|
|
207
|
-
ctx.userId,
|
|
208
|
-
);
|
|
209
|
-
const embed = createEmbedder(agentRuntime, embeddingModel);
|
|
210
|
-
|
|
211
|
-
const summaryEmbedding = await embed(input.summary);
|
|
212
|
-
const detailsEmbedding = await embed(input.details);
|
|
213
|
-
const conclusionVector = await embed(input.withPreference.conclusionDirectives);
|
|
214
|
-
|
|
215
|
-
const suggestionsText =
|
|
216
|
-
input.withPreference?.suggestions?.length && input.withPreference?.suggestions?.length > 0
|
|
217
|
-
? input.withPreference?.suggestions?.join('\n')
|
|
218
|
-
: null;
|
|
219
|
-
|
|
220
|
-
const metadata = {
|
|
221
|
-
appContext: input.withPreference.appContext,
|
|
222
|
-
extractedScopes: input.withPreference.extractedScopes,
|
|
223
|
-
originContext: input.withPreference.originContext,
|
|
224
|
-
} satisfies Record<string, unknown>;
|
|
225
|
-
|
|
226
|
-
const { memory, preference } = await ctx.memoryModel.createPreferenceMemory({
|
|
227
|
-
details: input.details || '',
|
|
228
|
-
detailsEmbedding,
|
|
229
|
-
memoryCategory: input.memoryCategory,
|
|
230
|
-
memoryLayer: LayersEnum.Preference,
|
|
231
|
-
memoryType: input.memoryType,
|
|
232
|
-
preference: {
|
|
233
|
-
conclusionDirectives: input.withPreference.conclusionDirectives || '',
|
|
234
|
-
conclusionDirectivesVector: conclusionVector ?? null,
|
|
235
|
-
metadata,
|
|
236
|
-
scorePriority: input.withPreference.scorePriority ?? null,
|
|
237
|
-
suggestions: suggestionsText,
|
|
238
|
-
tags: input.tags,
|
|
239
|
-
type: input.memoryType,
|
|
240
|
-
},
|
|
241
|
-
summary: input.summary,
|
|
242
|
-
summaryEmbedding,
|
|
243
|
-
title: input.title,
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
return {
|
|
247
|
-
memoryId: memory.id,
|
|
248
|
-
message: 'Memory saved successfully',
|
|
249
|
-
preferenceId: preference.id,
|
|
250
|
-
success: true,
|
|
251
|
-
};
|
|
252
|
-
} catch (error) {
|
|
253
|
-
console.error('Failed to save memory:', error);
|
|
254
|
-
return {
|
|
255
|
-
message: `Failed to save memory: ${(error as Error).message}`,
|
|
256
|
-
success: false,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
}),
|
|
260
|
-
|
|
261
|
-
removeIdentityMemory: memoryProcedure
|
|
262
|
-
.input(RemoveIdentityActionSchema)
|
|
263
|
-
.mutation(async ({ input, ctx }) => {
|
|
264
|
-
try {
|
|
265
|
-
const removed = await ctx.memoryModel.removeIdentityEntry(input.id);
|
|
266
|
-
|
|
267
|
-
if (!removed) {
|
|
268
|
-
return {
|
|
269
|
-
message: 'Identity memory not found',
|
|
270
|
-
success: false,
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return {
|
|
275
|
-
identityId: input.id,
|
|
276
|
-
message: 'Identity memory removed successfully',
|
|
277
|
-
reason: input.reason,
|
|
278
|
-
success: true,
|
|
279
|
-
};
|
|
280
|
-
} catch (error) {
|
|
281
|
-
console.error('Failed to remove identity memory:', error);
|
|
282
|
-
return {
|
|
283
|
-
message: `Failed to remove identity memory: ${(error as Error).message}`,
|
|
284
|
-
success: false,
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
}),
|
|
288
|
-
|
|
289
|
-
searchMemory: memoryProcedure.input(searchMemorySchema).query(async ({ input, ctx }) => {
|
|
290
|
-
try {
|
|
291
|
-
return await searchUserMemories(ctx, input);
|
|
292
|
-
} catch (error) {
|
|
293
|
-
console.error('Failed to retrieve memories:', error);
|
|
294
|
-
return { contexts: [], experiences: [], preferences: [] };
|
|
295
|
-
}
|
|
296
|
-
}),
|
|
297
|
-
|
|
298
|
-
updateIdentityMemory: memoryProcedure
|
|
299
|
-
.input(UpdateIdentityActionSchema)
|
|
300
|
-
.mutation(async ({ input, ctx }) => {
|
|
301
|
-
try {
|
|
302
|
-
const { agentRuntime, embeddingModel } = await getEmbeddingRuntime(
|
|
303
|
-
ctx.serverDB,
|
|
304
|
-
ctx.userId,
|
|
305
|
-
);
|
|
306
|
-
const embed = createEmbedder(agentRuntime, embeddingModel);
|
|
307
|
-
|
|
308
|
-
let summaryVector1024: number[] | null | undefined;
|
|
309
|
-
if (input.set.summary !== undefined) {
|
|
310
|
-
const vector = await embed(input.set.summary);
|
|
311
|
-
summaryVector1024 = vector ?? null;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
let detailsVector1024: number[] | null | undefined;
|
|
315
|
-
if (input.set.details !== undefined) {
|
|
316
|
-
const vector = await embed(input.set.details);
|
|
317
|
-
detailsVector1024 = vector ?? null;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
let descriptionVector: number[] | null | undefined;
|
|
321
|
-
if (input.set.withIdentity.description !== undefined) {
|
|
322
|
-
const vector = await embed(input.set.withIdentity.description);
|
|
323
|
-
descriptionVector = vector ?? null;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
const metadataUpdates: Record<string, unknown> = {};
|
|
327
|
-
if (Object.hasOwn(input.set.withIdentity, 'scoreConfidence')) {
|
|
328
|
-
metadataUpdates.scoreConfidence = input.set.withIdentity.scoreConfidence ?? null;
|
|
329
|
-
}
|
|
330
|
-
if (Object.hasOwn(input.set.withIdentity, 'sourceEvidence')) {
|
|
331
|
-
metadataUpdates.sourceEvidence = input.set.withIdentity.sourceEvidence ?? null;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
const identityPayload: Partial<IdentityEntryPayload> = {};
|
|
335
|
-
if (input.set.withIdentity.description !== undefined) {
|
|
336
|
-
identityPayload.description = input.set.withIdentity.description;
|
|
337
|
-
identityPayload.descriptionVector = descriptionVector;
|
|
338
|
-
}
|
|
339
|
-
if (input.set.withIdentity.episodicDate !== undefined) {
|
|
340
|
-
identityPayload.episodicDate = input.set.withIdentity.episodicDate;
|
|
341
|
-
}
|
|
342
|
-
if (input.set.withIdentity.relationship !== undefined) {
|
|
343
|
-
identityPayload.relationship = input.set.withIdentity.relationship;
|
|
344
|
-
}
|
|
345
|
-
if (input.set.withIdentity.role !== undefined) {
|
|
346
|
-
identityPayload.role = input.set.withIdentity.role;
|
|
347
|
-
}
|
|
348
|
-
if (input.set.tags !== undefined) {
|
|
349
|
-
identityPayload.tags = input.set.tags;
|
|
350
|
-
}
|
|
351
|
-
if (input.set.withIdentity.type !== undefined) {
|
|
352
|
-
identityPayload.type = input.set.withIdentity.type;
|
|
353
|
-
}
|
|
354
|
-
if (Object.keys(metadataUpdates).length > 0) {
|
|
355
|
-
identityPayload.metadata = metadataUpdates;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const basePayload: Partial<IdentityEntryBasePayload> = {};
|
|
359
|
-
if (input.set.details !== undefined) {
|
|
360
|
-
basePayload.details = input.set.details;
|
|
361
|
-
basePayload.detailsVector1024 = detailsVector1024;
|
|
362
|
-
}
|
|
363
|
-
if (input.set.memoryCategory !== undefined) {
|
|
364
|
-
basePayload.memoryCategory = input.set.memoryCategory;
|
|
365
|
-
}
|
|
366
|
-
if (input.set.memoryType !== undefined) {
|
|
367
|
-
basePayload.memoryType = input.set.memoryType;
|
|
368
|
-
}
|
|
369
|
-
if (input.set.summary !== undefined) {
|
|
370
|
-
basePayload.summary = input.set.summary;
|
|
371
|
-
basePayload.summaryVector1024 = summaryVector1024;
|
|
372
|
-
}
|
|
373
|
-
if (input.set.tags !== undefined) {
|
|
374
|
-
basePayload.tags = input.set.tags;
|
|
375
|
-
}
|
|
376
|
-
if (input.set.title !== undefined) {
|
|
377
|
-
basePayload.title = input.set.title;
|
|
378
|
-
}
|
|
379
|
-
if (Object.keys(metadataUpdates).length > 0) {
|
|
380
|
-
basePayload.metadata = metadataUpdates;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const updated = await ctx.memoryModel.updateIdentityEntry({
|
|
384
|
-
base: Object.keys(basePayload).length > 0 ? basePayload : undefined,
|
|
385
|
-
identity: Object.keys(identityPayload).length > 0 ? identityPayload : undefined,
|
|
386
|
-
identityId: input.id,
|
|
387
|
-
mergeStrategy: input.mergeStrategy,
|
|
388
|
-
});
|
|
389
|
-
|
|
390
|
-
if (!updated) {
|
|
391
|
-
return {
|
|
392
|
-
message: 'Identity memory not found',
|
|
393
|
-
success: false,
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
return {
|
|
398
|
-
identityId: input.id,
|
|
399
|
-
message: 'Identity memory updated successfully',
|
|
400
|
-
success: true,
|
|
401
|
-
};
|
|
402
|
-
} catch (error) {
|
|
403
|
-
console.error('Failed to update identity memory:', error);
|
|
404
|
-
return {
|
|
405
|
-
message: `Failed to update identity memory: ${(error as Error).message}`,
|
|
406
|
-
success: false,
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
}),
|
|
410
|
-
});
|