Skip to content

Commit 1ffd3bb

Browse files
πŸ“¦ NEW: Handle old format memory config (#134)
* πŸ“¦ NEW: Backward compat for old config * πŸ“¦ NEW: Handle old format config * πŸ‘Œ IMPROVE: Logs
1 parent 51cfc7f commit 1ffd3bb

File tree

3 files changed

+146
-18
lines changed

3 files changed

+146
-18
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import {
2424
updateDeployedCommitHash
2525
} from '@/utils/memory/git-sync/handle-git-sync-memories';
2626
import { handleSingleDocDeploy } from './document';
27+
import {
28+
generateUpgradeInstructions,
29+
isOldMemoryConfigFormat
30+
} from '@/utils/memory/handle-old-memory-config';
2731

2832
export interface Account {
2933
login: string;
@@ -482,6 +486,14 @@ export async function deployMemory({
482486

483487
p.log.step(`Processing documents for memory: ${memoryNameWithoutExt}`);
484488

489+
if (isOldMemoryConfigFormat(memoryObject)) {
490+
p.note(generateUpgradeInstructions(memoryObject));
491+
p.cancel(
492+
'Deployment cancelled. Please update your memory config file to the new format.'
493+
);
494+
process.exit(1);
495+
}
496+
485497
let filesToDeploy: string[] = [];
486498
let filesToDelete: string[] = [];
487499
let memoryDocs: MemoryDocumentI[] = [];
@@ -1184,7 +1196,7 @@ export async function deploySingleMemory({
11841196
});
11851197

11861198
p.outro(`Successfully deployed memory: ${memoryName}`);
1187-
process.exit(1);
1199+
process.exit(0);
11881200
} catch (error) {
11891201
if (error instanceof Error) {
11901202
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* Represents the old memory configuration format for backward compatibility.
3+
*/
4+
export interface OldMemoryConfig {
5+
name: string;
6+
description?: string;
7+
config?: OldConfigObject;
8+
}
9+
10+
interface OldConfigObject {
11+
useGitRepo: boolean;
12+
dirToTrack: string;
13+
extToTrack: string[] | ['*'];
14+
deployedCommitHash?: string;
15+
embeddedCommitHash?: string;
16+
}
17+
18+
/**
19+
* Type guard to check if an object is of type `OldConfigObject`.
20+
*
21+
* @param obj - The object to check.
22+
* @returns `true` if the object is an `OldConfigObject`, otherwise `false`.
23+
*/
24+
function isOldConfigObject(obj: unknown): obj is OldConfigObject {
25+
return (
26+
typeof obj === 'object' &&
27+
obj !== null &&
28+
'useGitRepo' in obj &&
29+
typeof (obj as OldConfigObject).useGitRepo === 'boolean' &&
30+
'dirToTrack' in obj &&
31+
typeof (obj as OldConfigObject).dirToTrack === 'string' &&
32+
'extToTrack' in obj &&
33+
Array.isArray((obj as OldConfigObject).extToTrack)
34+
);
35+
}
36+
37+
/**
38+
* Checks if an object conforms to the old memory configuration format.
39+
*
40+
* @param obj - The object to check.
41+
* @returns `true` if the object is in the old memory configuration format, otherwise `false`.
42+
*/
43+
export function isOldMemoryConfigFormat(obj: unknown): boolean {
44+
if (
45+
typeof obj !== 'object' ||
46+
obj === null ||
47+
!('name' in obj) ||
48+
!('config' in obj)
49+
) {
50+
return false;
51+
}
52+
53+
const typedObj = obj as { name: unknown; config: unknown };
54+
55+
return (
56+
typeof typedObj.name === 'string' &&
57+
(typedObj.config === undefined || isOldConfigObject(typedObj.config))
58+
);
59+
}
60+
61+
/**
62+
* Generates upgrade instructions for converting an old memory configuration to the new format.
63+
*
64+
* @param oldConfig - The old memory configuration.
65+
* @returns A string containing the upgrade instructions.
66+
*/
67+
export function generateUpgradeInstructions(
68+
oldConfig: OldMemoryConfig
69+
): string {
70+
if (!oldConfig.config) {
71+
return 'Invalid memory config.';
72+
}
73+
74+
const newConfigExample = {
75+
name: oldConfig.name,
76+
description: oldConfig.description || 'Your memory description',
77+
git: {
78+
enabled: oldConfig.config.useGitRepo,
79+
include:
80+
oldConfig.config.extToTrack[0] === '*'
81+
? [`${oldConfig.config.dirToTrack}/**/*`]
82+
: oldConfig.config.extToTrack.map(
83+
ext => `${oldConfig.config?.dirToTrack}/**/*${ext}`
84+
),
85+
gitignore: true,
86+
deployedAt: oldConfig.config.deployedCommitHash || '',
87+
embeddedAt: oldConfig.config.embeddedCommitHash || ''
88+
}
89+
};
90+
91+
return `
92+
Your memory config is using an outdated format in baseai/memory/${oldConfig.name}/index.ts. Please update the file to this new format:
93+
94+
${JSON.stringify(newConfigExample, null, 2)}
95+
96+
Key changes:
97+
- Removed nested 'config' object structure
98+
- Git-related fields are now grouped under a 'git' object
99+
- 'useGitRepo' is now 'git.enabled'
100+
- 'dirToTrack' and 'extToTrack' are combined into 'git.include' glob patterns
101+
- 'deployedCommitHash' is now 'git.deployedAt'
102+
- 'embeddedCommitHash' is now 'git.embeddedAt'
103+
- Added new 'git.gitignore' field (defaults to true)
104+
105+
For more information, refer to the documentation: https://baseai.dev/docs/guides/memory-from-git
106+
`;
107+
}

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import fs from 'fs/promises';
22
import path from 'path';
33
import * as p from '@clack/prompts';
44
import { memoryConfigSchema, type MemoryConfigI } from 'types/memory';
5+
import {
6+
generateUpgradeInstructions,
7+
isOldMemoryConfigFormat,
8+
type OldMemoryConfig
9+
} from './handle-old-memory-config';
510

611
function extractConfigObject(fileContents: string): unknown {
712
try {
@@ -38,22 +43,7 @@ function extractConfigObject(fileContents: string): unknown {
3843

3944
// Create a new Function that returns the object literal
4045
const fn = new Function(`return ${memoryObjStr}`);
41-
const memoryObj = fn();
42-
43-
// Extract memory config properties
44-
const configObj: MemoryConfigI = {
45-
name: memoryObj.name,
46-
description: memoryObj.description,
47-
git: {
48-
enabled: memoryObj.git.enabled,
49-
include: memoryObj.git.include,
50-
gitignore: memoryObj.git.gitignore,
51-
deployedAt: memoryObj.git.deployedAt || '',
52-
embeddedAt: memoryObj.git.embeddedAt || ''
53-
}
54-
};
55-
56-
return configObj;
46+
return fn();
5747
} catch (error) {
5848
console.error('Parsing error:', error);
5949
console.error('File contents:', fileContents);
@@ -79,7 +69,26 @@ export default async function loadMemoryConfig(
7969
const fileContents = await fs.readFile(indexFilePath, 'utf-8');
8070
const configObj = extractConfigObject(fileContents);
8171

82-
return memoryConfigSchema.parse(configObj);
72+
// Try to parse with new schema first
73+
try {
74+
return memoryConfigSchema.parse(configObj);
75+
} catch (parseError) {
76+
if (!configObj) throw parseError;
77+
78+
// If parsing fails, check if it's an old format
79+
if (isOldMemoryConfigFormat(configObj)) {
80+
p.note(
81+
generateUpgradeInstructions(configObj as OldMemoryConfig)
82+
);
83+
p.cancel(
84+
'Deployment cancelled. Please update your memory config file to the new format.'
85+
);
86+
process.exit(1);
87+
}
88+
89+
// If it's neither new nor old format, throw the original error
90+
throw parseError;
91+
}
8392
} catch (error) {
8493
if (error instanceof Error) {
8594
p.cancel(`Failed to load memory '${memoryName}': ${error.message}`);

0 commit comments

Comments
Β (0)