Skip to content

🤯 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.

🔨 chore(memory-user-memory): support source ids in extraction schemas (#14778)

Neko4630785870c7
diff --git a/packages/memory-user-memory/src/extractors/identity.test.ts b/packages/memory-user-memory/src/extractors/identity.test.ts
index 90603627e05..1ef9822457e 100644
--- a/packages/memory-user-memory/src/extractors/identity.test.ts
+++ b/packages/memory-user-memory/src/extractors/identity.test.ts
@@ -58,6 +58,7 @@ describe('IdentityExtractor', () => {
relationship: 'self',
role: 'developer',
scoreConfidence: 0.8,
+ sourceIds: ['message-1'],
sourceEvidence: null,
type: 'personal',
},
diff --git a/packages/memory-user-memory/src/prompts/layers/activity.ts b/packages/memory-user-memory/src/prompts/layers/activity.ts
index a6ebd1c45d2..a10ac80df61 100644
--- a/packages/memory-user-memory/src/prompts/layers/activity.ts
+++ b/packages/memory-user-memory/src/prompts/layers/activity.ts
@@ -35,7 +35,8 @@ export const activityPrompt = [
'## Output Format',
'\n',
'Return structured JSON data according to the provided schema:',
- '- Basic fields: title, summary, details, memoryLayer, memoryType, memoryCategory, tags',
+ '- Basic fields: title, summary, details, memoryLayer, memoryType, memoryCategory, sourceIds, tags',
+ '- If messages contain source metadata with stable message ids, set sourceIds to the exact ids that support this memory. Use [] when no source id is available.',
'- Activity-specific fields in withActivity: type (enum), status (planned/completed/cancelled/ongoing/on_hold/pending),',
' optional timezone, startsAt, endsAt (ISO 8601), associatedSubjects (self-aware entities: people, pets, groups, orgs), associatedObjects',
' (non-living items like transportation, devices),',
diff --git a/packages/memory-user-memory/src/prompts/layers/context.ts b/packages/memory-user-memory/src/prompts/layers/context.ts
index b2095e4acf5..d7fc46eef6d 100644
--- a/packages/memory-user-memory/src/prompts/layers/context.ts
+++ b/packages/memory-user-memory/src/prompts/layers/context.ts
@@ -44,7 +44,8 @@ export const contextPrompt = [
'Return structured JSON data according to the provided schema. A strict schema',
'validates the output and includes:',
'\n',
- '- Basic fields: title, summary, details, memoryLayer, memoryType, memoryCategory',
+ '- Basic fields: title, summary, details, memoryLayer, memoryType, memoryCategory, sourceIds',
+ '- If messages contain source metadata with stable message ids, set sourceIds to the exact ids that support this memory. Use [] when no source id is available.',
'- Context-specific fields in withContext: title, description, extractedLabels,',
' associatedSubjects (array of object), associatedObjects (array of object),',
' currentStatus, type, scoreImpact, scoreUrgency.',
diff --git a/packages/memory-user-memory/src/prompts/layers/experience.ts b/packages/memory-user-memory/src/prompts/layers/experience.ts
index 22a659ee626..6a0640d4d66 100644
--- a/packages/memory-user-memory/src/prompts/layers/experience.ts
+++ b/packages/memory-user-memory/src/prompts/layers/experience.ts
@@ -47,7 +47,8 @@ export const experiencePrompt = [
'pass validation against a strict schema including:',
'\n',
'- Basic fields: title, summary, details, memoryLayer, memoryType, memoryCategory,',
- ' tags',
+ ' sourceIds, tags',
+ '- If messages contain source metadata with stable message ids, set sourceIds to the exact ids that support this memory. Use [] when no source id is available.',
'- Experience-specific fields in withExperience: labels, situation, reasoning,',
' possibleOutcome, action, keyLearning, scoreConfidence,',
' problemSolvingScore, knowledgeValueScore',
diff --git a/packages/memory-user-memory/src/prompts/layers/identity.ts b/packages/memory-user-memory/src/prompts/layers/identity.ts
index e101d6af39e..45543b3ebcc 100644
--- a/packages/memory-user-memory/src/prompts/layers/identity.ts
+++ b/packages/memory-user-memory/src/prompts/layers/identity.ts
@@ -106,7 +106,8 @@ export const identityPrompt = [
' there are no actions of that type.',
'- Each `add` item must include a rich `description`, `type`, and optional fields',
' like `relationship`, `role`, `episodicDate`, `extractedLabels`,',
- ' `scoreConfidence`, and `sourceEvidence`.',
+ ' `scoreConfidence`, `sourceIds`, and `sourceEvidence`.',
+ '- If messages contain source metadata with stable message ids, set sourceIds to the exact ids that support each add or update action. Use [] when no source id is available.',
'- Each `update` item must include `id`, `mergeStrategy`, and a `set` object with',
' changed fields.',
'- Each `remove` item must include `id` and `reason`.',
diff --git a/packages/memory-user-memory/src/prompts/layers/preference.ts b/packages/memory-user-memory/src/prompts/layers/preference.ts
index e36b79ed777..869509c723b 100644
--- a/packages/memory-user-memory/src/prompts/layers/preference.ts
+++ b/packages/memory-user-memory/src/prompts/layers/preference.ts
@@ -53,7 +53,8 @@ export const preferencePrompt = [
'pass validation against a strict schema including:',
'\n',
'- Basic fields: title, summary, details, memoryLayer, memoryType,',
- ' memoryCategory',
+ ' memoryCategory, sourceIds',
+ '- If messages contain source metadata with stable message ids, set sourceIds to the exact ids that support this memory. Use [] when no source id is available.',
'- Preference-specific fields in withPreference: extractedLabels,',
' extractedScopes (array of strings), originContext (trigger, scenario, actor,',
' applicableWhen, notApplicableWhen), appContext (app, surface, feature,',
diff --git a/packages/memory-user-memory/src/schemas/activity.ts b/packages/memory-user-memory/src/schemas/activity.ts
index 36d6a485b42..7c647e8c45d 100644
--- a/packages/memory-user-memory/src/schemas/activity.ts
+++ b/packages/memory-user-memory/src/schemas/activity.ts
@@ -40,6 +40,7 @@ export const ActivityMemoryItemSchema = z.object({
details: z.string(),
memoryCategory: z.string(),
memoryType: MemoryTypeSchema,
+ sourceIds: z.array(z.string()).optional().nullable(),
summary: z.string(),
tags: z.array(z.string()),
title: z.string(),
@@ -77,6 +78,7 @@ export const ActivityMemorySchema: GenerateObjectSchema = {
'Talked through renewal scope, confirmed timeline flexibility, and captured follow-ups.',
memoryCategory: 'work',
memoryType: 'activity',
+ sourceIds: ['message-3'],
summary: 'Client Q2 renewal meeting with Alice (ACME)',
tags: ['meeting', 'client', 'renewal'],
title: 'ACME Q2 renewal meeting',
@@ -142,6 +144,12 @@ export const ActivityMemorySchema: GenerateObjectSchema = {
description: 'Concise overview of this activity.',
type: 'string',
},
+ sourceIds: {
+ description:
+ 'Stable source message ids that support this activity. Use [] when unavailable.',
+ items: { type: 'string' },
+ type: ['array', 'null'],
+ },
tags: {
description: 'Model-generated tags summarizing key facets of the activity.',
items: { type: 'string' },
@@ -322,6 +330,7 @@ export const ActivityMemorySchema: GenerateObjectSchema = {
'details',
'memoryType',
'memoryCategory',
+ 'sourceIds',
'tags',
'withActivity',
],
diff --git a/packages/memory-user-memory/src/schemas/context.ts b/packages/memory-user-memory/src/schemas/context.ts
index 5fe74404f8d..ddf44b7ade8 100644
--- a/packages/memory-user-memory/src/schemas/context.ts
+++ b/packages/memory-user-memory/src/schemas/context.ts
@@ -79,6 +79,10 @@ export const ContextMemoryItemSchema = z.object({
details: z.string().describe('Optional detailed information'),
memoryCategory: z.string().describe('Memory category'),
memoryType: MemoryTypeSchema.describe('Memory type'),
+ sourceIds: z
+ .array(z.string())
+ .nullable()
+ .describe('Stable source message ids that support this memory'),
summary: z.string().describe('Concise overview of this specific memory'),
tags: z.array(z.string()).describe('User defined tags that summarize the context facets'),
title: z.string().describe('Brief descriptive title'),
diff --git a/packages/memory-user-memory/src/schemas/experience.ts b/packages/memory-user-memory/src/schemas/experience.ts
index 206e4ab6aae..06613fa01fd 100644
--- a/packages/memory-user-memory/src/schemas/experience.ts
+++ b/packages/memory-user-memory/src/schemas/experience.ts
@@ -37,6 +37,10 @@ export const ExperienceMemoryItemSchema = z.object({
details: z.string().describe('Optional detailed information'),
memoryCategory: z.string().describe('Memory category'),
memoryType: MemoryTypeSchema.describe('Memory type'),
+ sourceIds: z
+ .array(z.string())
+ .nullable()
+ .describe('Stable source message ids that support this memory'),
summary: z.string().describe('Concise overview of this specific memory'),
tags: z.array(z.string()).describe('Model generated tags that summarize the experience facets'),
title: z.string().describe('Brief descriptive title'),
diff --git a/packages/memory-user-memory/src/schemas/identity.ts b/packages/memory-user-memory/src/schemas/identity.ts
index a567a2f75b3..96c961af1ad 100644
--- a/packages/memory-user-memory/src/schemas/identity.ts
+++ b/packages/memory-user-memory/src/schemas/identity.ts
@@ -59,6 +59,10 @@ export const AddIdentityActionSchema = z
.string()
.describe('Role explicitly mentioned for this identity entry (e.g., "platform engineer", "caregiver"); keep neutral and only use when evidence exists'),
scoreConfidence: z.number(),
+ sourceIds: z
+ .array(z.string())
+ .nullable()
+ .describe('Stable source message ids that support this identity'),
sourceEvidence: z.union([z.string(), z.null()]),
type: IdentityTypeEnum,
})
@@ -109,6 +113,10 @@ export const UpdateIdentityActionSchema = z
.describe('Role explicitly mentioned for this identity entry (e.g., "platform engineer", "caregiver"); keep existing when not updated; use null for omitting the field')
.nullable(),
scoreConfidence: z.number().nullable(),
+ sourceIds: z
+ .array(z.string())
+ .nullable()
+ .describe('Stable source message ids that support this identity update'),
sourceEvidence: z.string().nullable(),
// TODO: OpenAI requires `required` fields to be always present, while enum fields cannot be null
type: z
diff --git a/packages/memory-user-memory/src/schemas/preference.ts b/packages/memory-user-memory/src/schemas/preference.ts
index 65389d822b4..cb69e3fb29c 100644
--- a/packages/memory-user-memory/src/schemas/preference.ts
+++ b/packages/memory-user-memory/src/schemas/preference.ts
@@ -66,6 +66,10 @@ export const PreferenceMemoryItemSchema = z.object({
details: z.string().describe('Optional detailed information'),
memoryCategory: z.string().describe('Memory category'),
memoryType: MemoryTypeSchema.describe('Memory type'),
+ sourceIds: z
+ .array(z.string())
+ .nullable()
+ .describe('Stable source message ids that support this memory'),
summary: z.string().describe('Concise overview of this specific memory'),
tags: z.array(z.string()).describe('Model generated tags that summarize the preference facets'),
title: z.string().describe('Brief descriptive title'),