|
| 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 | +} |
0 commit comments