Skip to content

Commit 837fa6d

Browse files
authored
πŸ“¦ NEW: Dynamically set document metadata (#137)
* πŸ“¦ NEW: Dynamically set document metadata * πŸ‘Œ IMPROVE: Code * πŸ› FIX: Reference errors
1 parent c1dda8f commit 837fa6d

File tree

5 files changed

+85
-10
lines changed

5 files changed

+85
-10
lines changed

β€Žexamples/nodejs/baseai/memory/ai-agent-memory/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const memoryAiAgentMemory = (): MemoryI => ({
1010
deployedAt: '',
1111
embeddedAt: '',
1212
},
13+
documents: {
14+
meta: doc => {
15+
// generate a URL for each document
16+
const url = `https://example.com/${doc.path}`;
17+
return {
18+
url,
19+
name: doc.name,
20+
};
21+
},
22+
},
1323
});
1424

1525
export default memoryAiAgentMemory;

β€Žpackages/baseai/src/deploy/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,8 @@ export async function uploadDocumentsToMemory({
689689
const signedUrl = await getSignedUploadUrl({
690690
documentName: doc.name,
691691
memoryName: name,
692-
account
692+
account,
693+
meta: doc.meta
693694
});
694695

695696
const uploadResponse = await uploadDocument(signedUrl, doc.blob);
@@ -919,11 +920,13 @@ export async function listMemoryDocuments({
919920
async function getSignedUploadUrl({
920921
documentName,
921922
memoryName,
922-
account
923+
account,
924+
meta
923925
}: {
924926
documentName: string;
925927
memoryName: string;
926928
account: Account;
929+
meta: Record<string, string>;
927930
}): Promise<string> {
928931
const { uploadDocument } = getMemoryApiUrls({
929932
memoryName
@@ -942,6 +945,7 @@ async function getSignedUploadUrl({
942945
Authorization: `Bearer ${account.apiKey}`
943946
},
944947
body: JSON.stringify({
948+
meta,
945949
memoryName,
946950
ownerLogin,
947951
fileName: documentName

β€Žpackages/baseai/src/utils/memory/load-memory-files.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { allSupportedExtensions, MEMORYSETS } from './constants';
55
import { getDocumentContent } from './get-document-content';
66
import { formatDocSize } from './lib';
77
import loadMemoryConfig from './load-memory-config';
8-
import { memoryConfigSchema, type MemoryConfigI } from 'types/memory';
8+
import {
9+
memoryConfigSchema,
10+
type DocumentConfigI,
11+
type MemoryConfigI
12+
} from 'types/memory';
913
import { execSync } from 'child_process';
1014
import fg from 'fast-glob';
1115

@@ -14,6 +18,8 @@ export interface MemoryDocumentI {
1418
size: string;
1519
content: string;
1620
blob: Blob;
21+
path: string;
22+
meta: Record<string, string>;
1723
}
1824

1925
export const loadMemoryFiles = async (
@@ -24,10 +30,11 @@ export const loadMemoryFiles = async (
2430

2531
// useDocumentsDir
2632
const useDocumentsDir = !memoryConfig || !memoryConfig.git.enabled;
33+
const documentConfig = memoryConfig?.documents;
2734

2835
// Load files from documents directory.
2936
if (useDocumentsDir) {
30-
return await loadMemoryFilesFromDocsDir(memoryName);
37+
return await loadMemoryFilesFromDocsDir({ memoryName, documentConfig });
3138
}
3239

3340
// Load files from the repo.
@@ -121,12 +128,21 @@ export const loadMemoryFilesFromCustomDir = async ({
121128
return null;
122129
}
123130

124-
return {
131+
const memoryFile = {
132+
path: filePath,
125133
name: path.basename(filePath.replace(/\//g, '-')),
126134
size: formatDocSize(fileContentBlob.size),
127135
content: await getDocumentContent(fileContentBlob),
128136
blob: fileContentBlob
129137
};
138+
139+
let meta = {};
140+
141+
if (memoryConfig?.documents?.meta) {
142+
meta = memoryConfig.documents.meta(memoryFile) || {};
143+
}
144+
145+
return { ...memoryFile, meta };
130146
})
131147
);
132148

@@ -159,9 +175,13 @@ export const loadMemoryFilesFromCustomDir = async ({
159175
* - Have unsupported file extensions.
160176
* 5. Returns an array of `MemoryDocumentI` objects representing the valid memory files.
161177
*/
162-
export const loadMemoryFilesFromDocsDir = async (
163-
memoryName: string
164-
): Promise<MemoryDocumentI[]> => {
178+
export const loadMemoryFilesFromDocsDir = async ({
179+
memoryName,
180+
documentConfig
181+
}: {
182+
memoryName: string;
183+
documentConfig?: DocumentConfigI;
184+
}): Promise<MemoryDocumentI[]> => {
165185
const memoryDir = path.join(process.cwd(), 'baseai', 'memory', memoryName);
166186
const memoryFilesPath = path.join(memoryDir, 'documents');
167187

@@ -214,12 +234,21 @@ export const loadMemoryFilesFromDocsDir = async (
214234
return null;
215235
}
216236

217-
return {
237+
const memoryFile = {
218238
name: file,
239+
path: filePath,
219240
size: formatDocSize(fileContentBlob.size),
220241
content: await getDocumentContent(fileContentBlob),
221242
blob: fileContentBlob
222243
};
244+
245+
let meta = {};
246+
247+
if (documentConfig?.meta) {
248+
meta = documentConfig.meta(memoryFile) || {};
249+
}
250+
251+
return { ...memoryFile, meta };
223252
})
224253
);
225254

β€Žpackages/baseai/types/memory.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,32 @@ export const gitConfigSchema = z.object({
2727
embeddedAt: z.string().trim().optional().default('')
2828
});
2929

30+
export const documentSchema = z.object({
31+
meta: z
32+
.function()
33+
.args(
34+
z.object({
35+
name: z.string(),
36+
size: z.string(),
37+
content: z.string(),
38+
blob: z.instanceof(Blob),
39+
path: z.string(),
40+
})
41+
)
42+
.returns(z.record(z.string()))
43+
.optional()
44+
});
45+
3046
export const memoryConfigSchema = z.object({
3147
name: z.string(),
3248
description: z.string().optional(),
33-
git: gitConfigSchema
49+
git: gitConfigSchema,
50+
documents: documentSchema.optional()
3451
});
3552

3653
export type GitConfigI = z.infer<typeof gitConfigSchema>;
3754

3855
export type MemoryConfigI = z.infer<typeof memoryConfigSchema>;
56+
export type DocumentConfigI = z.infer<typeof documentSchema>;
3957

4058
export type MemoryI = MemoryConfigI;

β€Žpackages/core/types/memory.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,22 @@ export interface GitConfig {
66
embeddedAt?: string;
77
}
88

9+
export interface MemoryDocumentI {
10+
name: string;
11+
size: string;
12+
content: string;
13+
blob: Blob;
14+
path: string;
15+
originalName: string;
16+
}
17+
18+
export interface Document {
19+
meta?: (documnet: MemoryDocumentI) => Record<string, string>;
20+
}
21+
922
export interface Memory {
1023
name: string;
1124
description?: string;
1225
git: GitConfig;
26+
documents?: Document;
1327
}

0 commit comments

Comments
Β (0)