@lobehub/lobehub 2.0.0-next.340 → 2.0.0-next.342

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.
@@ -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
- });