Skip to content

Commit

Permalink
📦 NEW: useGit config
Browse files Browse the repository at this point in the history
  • Loading branch information
saqibameen committed Nov 14, 2024
1 parent 363ab95 commit c58b503
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {MemoryI} from '@baseai/core';
import path from 'path';

const memoryCodeFiles = (): MemoryI => ({
name: 'code-files',
description: 'Memory that contains project files',
config: {
useGitRepo: false,
dirToTrack: path.posix.join('.'),
extToTrack: ['*'],
},
useGit: false,
include: ['documents/**/*'],
});

export default memoryCodeFiles;
7 changes: 2 additions & 5 deletions examples/astro/baseai/memory/chat-with-docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import type {MemoryI} from '@baseai/core';
const buidMemory = (): MemoryI => ({
name: 'chat-with-docs',
description: 'Chat with given docs',
config: {
useGitRepo: false,
include: '.',
extensions: ['*'],
},
useGit: false,
include: ['documents/**/*'],
});

export default buidMemory;
7 changes: 2 additions & 5 deletions examples/nextjs/baseai/memory/chat-with-docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {MemoryI} from '@baseai/core';
const buidMemory = (): MemoryI => ({
name: 'chat-with-docs',
description: 'Chat with given docs',
config: {
useGitRepo: false,
include: '.',
extensions: ['*'],
},
useGit: false,
include: ['documents/**/*'],
});

export default buidMemory;
14 changes: 0 additions & 14 deletions examples/nodejs/baseai/memory/ai-agent-memory/index.ts

This file was deleted.

7 changes: 2 additions & 5 deletions examples/nodejs/baseai/memory/chat-with-docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {MemoryI} from '@baseai/core';
const buildMemory = (): MemoryI => ({
name: 'chat-with-docs',
description: 'Chat with docs',
config: {
useGitRepo: false,
include: '.',
extensions: ['*'],
},
useGit: false,
include: ['documents/**/*'],
});

export default buildMemory;
14 changes: 6 additions & 8 deletions examples/nodejs/baseai/memory/chat-with-repo/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { MemoryI } from '@baseai/core';
import {MemoryI} from '@baseai/core';

const memoryChatWithRepo = (): MemoryI => ({
name: 'chat-with-repo',
description: '',
config: {
useGitRepo: true,
include: './examples',
extensions: ['*'],
}
name: 'chat-with-repo',
description: '',
useGit: true,
include: ['examples/**/*'],
gitignore: true,
});

export default memoryChatWithRepo;
4 changes: 2 additions & 2 deletions examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",
"license": "UNLICENSED",
"dependencies": {
"@baseai/core": "workspace:*",
"@baseai/core": "^0.9.25",
"dotenv": "^16.4.5"
},
"devDependencies": {
"baseai": "workspace:*",
"baseai": "^0.9.25",
"tsx": "^4.19.0"
}
}
7 changes: 2 additions & 5 deletions examples/remix/baseai/memory/chat-with-docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import {MemoryI} from '@baseai/core';
const buidMemory = (): MemoryI => ({
name: 'chat-with-docs',
description: 'Chat with given docs',
config: {
useGitRepo: false,
include: '.',
extensions: ['*'],
},
useGit: false,
include: ['documents/**/*'],
});

export default buidMemory;
6 changes: 3 additions & 3 deletions packages/baseai/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,14 @@ export async function deployMemory({
let memoryDocs: MemoryDocumentI[] = [];

// Git sync memories
if (memoryObject.config?.useGitRepo) {
if (memoryObject.useGit) {
// Get names of files to deploy, i.e., changed or new files
const {
filesToDeploy: gitFilesToDeploy,
filesToDelete: gitFilesToDelete
} = await handleGitSyncMemories({
memoryName: memoryNameWithoutExt,
config: memoryObject.config,
config: memoryObject,
account
});

Expand Down Expand Up @@ -530,7 +530,7 @@ export async function deployMemory({
documents: memoryDocs,
account,
overwrite,
isGitSync: memoryObject.config?.useGitRepo,
isGitSync: memoryObject.useGit,
docsToDelete: filesToDelete
});
spinner.stop(`Deployment finished memory: ${memoryObject.name}`);
Expand Down
23 changes: 10 additions & 13 deletions packages/baseai/src/memory/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export async function createMemory() {
message: 'Description of the memory',
placeholder: defaultConfig.description
}),
useGitRepo: () =>
useGit: () =>
p.confirm({
message:
'Do you want to create memory from current project git repository?',
Expand All @@ -70,8 +70,7 @@ export async function createMemory() {
const filePath = path.join(memoryDir, 'index.ts');
const dbDir = path.join(process.cwd(), '.baseai', 'db');

if (memoryInfo.useGitRepo) {
// Check if the current directory is a Git repository
if (memoryInfo.useGit) {
try {
await execAsync('git rev-parse --is-inside-work-tree');
} catch (error) {
Expand All @@ -84,15 +83,13 @@ export async function createMemory() {
const ${memoryNameCamelCase} = (): MemoryI => ({
name: '${memoryNameSlugified}',
description: ${JSON.stringify(memoryInfo.description) || ''},
config: {
useGitRepo: ${memoryInfo.useGitRepo},
${
memoryInfo.useGitRepo
? `include: ['**/*'],
gitignore: true,`
: `include: ['${MEMORY_CONSTANTS.documentsDir}/**/*'],`
}
description: ${JSON.stringify(memoryInfo.description || '') || ''},
useGit: ${memoryInfo.useGit},
${
memoryInfo.useGit
? `include: ['**/*'],
gitignore: true,`
: `include: ['${MEMORY_CONSTANTS.documentsDir}/**/*'],`
}
});
Expand All @@ -105,7 +102,7 @@ export default ${memoryNameCamelCase};
await fs.promises.writeFile(filePath, memoryContent);
await fs.promises.mkdir(dbDir, { recursive: true });

if (!memoryInfo.useGitRepo) {
if (!memoryInfo.useGit) {
const memoryDocumentsPath = path.join(
memoryDir,
MEMORY_CONSTANTS.documentsDir
Expand Down
6 changes: 3 additions & 3 deletions packages/baseai/src/memory/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function embedMemory({
let filesToEmbed: string[] = [];
let filesToDelete: string[] = [];

if (memoryConfig?.useGitRepo) {
if (memoryConfig?.useGit) {
const { filesToDeploy, filesToDelete: gitFilesToDelete } =
await handleGitSyncMemories({
memoryName: memoryName,
Expand All @@ -78,7 +78,7 @@ export async function embedMemory({
let embedResult = 'Embeddings updated.';
if (memoryFiles && memoryFiles.length > 0) {
s.message('Generating embeddings...');
const shouldOverwrite = memoryConfig?.useGitRepo ? true : overwrite;
const shouldOverwrite = memoryConfig?.useGit ? true : overwrite;
embedResult = await generateEmbeddings({
memoryFiles,
memoryName,
Expand All @@ -87,7 +87,7 @@ export async function embedMemory({
});
}

if (memoryConfig?.useGitRepo) {
if (memoryConfig?.useGit) {
if (filesToDelete.length > 0) {
await deleteDocumentsFromDB({
memoryName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function handleGitSyncMemories({
p.log.info(
`Please commit these changes before ${isEmbed ? 'embedding' : 'deploying'}. Aborting.`
);
process.exit(1);
// process.exit(1);
}
} catch (error) {
p.log.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,51 @@ export async function saveDeployedCommitHashInMemoryConfig({

// Check if the git config exists
if (fileContents.includes('git:')) {
// Check if deployedAt exists in git config
if (fileContents.match(/git:\s*{[^}]*deployedAt:/)) {
// Update existing deployedAt
fileContents = fileContents.replace(
/(git:\s*{[^}]*deployedAt:\s*['"])([^'"]*)/,
`$1${deployedCommitHash}`
);
} else {
// Add deployedAt to existing git config
// Find the git block content
const gitMatch = fileContents.match(/git:\s*{([^}]*?)}/);
if (gitMatch) {
const existingGitContent = gitMatch[1].trim();
let newGitContent: string;

// If deployedAt exists, update it
if (existingGitContent.includes('deployedAt:')) {
newGitContent = existingGitContent.replace(
/(deployedAt:\s*['"])([^'"]*)(['"])/,
`$1${deployedCommitHash}$3`
);
} else {
// For empty or minimal content, just add deployedAt
if (!existingGitContent || existingGitContent === '') {
newGitContent = `\n\t\tdeployedAt: '${deployedCommitHash}'\n\t`;
} else {
// Add deployedAt to existing content
newGitContent =
existingGitContent.replace(/,\s*$/, '') + // Remove trailing comma if exists
`,\n\t\tdeployedAt: '${deployedCommitHash}'`;
}
}

// Replace the old git block with the new one
fileContents = fileContents.replace(
/(git:\s*{)/,
`$1\n deployedAt: '${deployedCommitHash}',`
/git:\s*{[^}]*?}/,
`git: {${newGitContent}}`
);
}
} else {
// Add entire git config with deployedAt
fileContents = fileContents.replace(
/config:\s*{/,
`config: {\n git: {\n deployedAt: '${deployedCommitHash}'\n },`
// Add new git config block
const insertAfterUseGit = fileContents.replace(
/(useGit:\s*true,?)(\s*\n)/,
`$1\n\tgit: {\n\t\tdeployedAt: '${deployedCommitHash}'\n\t},$2`
);

// Only update if the replacement was successful
if (insertAfterUseGit !== fileContents) {
fileContents = insertAfterUseGit;
} else {
throw new Error(
'Could not find appropriate location to insert git config'
);
}
}

// Write the updated contents back to the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,51 @@ export async function saveEmbeddedCommitHashInMemoryConfig({

// Check if the git config exists
if (fileContents.includes('git:')) {
// Check if embeddedAt exists in git config
if (fileContents.match(/git:\s*{[^}]*embeddedAt:/)) {
// Update existing embeddedAt
fileContents = fileContents.replace(
/(git:\s*{[^}]*embeddedAt:\s*['"])([^'"]*)/,
`$1${embeddedCommitHash}`
);
} else {
// Add embeddedAt to existing git config
// Find the git block content
const gitMatch = fileContents.match(/git:\s*{([^}]*?)}/);
if (gitMatch) {
const existingGitContent = gitMatch[1].trim();
let newGitContent: string;

// If embeddedAt exists, update it
if (existingGitContent.includes('embeddedAt:')) {
newGitContent = existingGitContent.replace(
/(embeddedAt:\s*['"])([^'"]*)(['"])/,
`$1${embeddedCommitHash}$3`
);
} else {
// For empty or minimal content, just add embeddedAt
if (!existingGitContent || existingGitContent === '') {
newGitContent = `\n\t\tembeddedAt: '${embeddedCommitHash}'\n\t`;
} else {
// Add embeddedAt to existing content
newGitContent =
existingGitContent.replace(/,\s*$/, '') + // Remove trailing comma if exists
`,\n\t\tembeddedAt: '${embeddedCommitHash}'`;
}
}

// Replace the old git block with the new one
fileContents = fileContents.replace(
/(git:\s*{)/,
`$1\n embeddedAt: '${embeddedCommitHash}',`
/git:\s*{[^}]*?}/,
`git: {${newGitContent}}`
);
}
} else {
// Add entire git config with embeddedAt
fileContents = fileContents.replace(
/config:\s*{/,
`config: {\n git: {\n embeddedAt: '${embeddedCommitHash}'\n },`
// Add new git config block
const insertAfterUseGit = fileContents.replace(
/(useGit:\s*true,?)(\s*\n)/,
`$1\n\tgit: {\n\t\tembeddedAt: '${embeddedCommitHash}'\n\t},$2`
);

// Only update if the replacement was successful
if (insertAfterUseGit !== fileContents) {
fileContents = insertAfterUseGit;
} else {
throw new Error(
'Could not find appropriate location to insert git config'
);
}
}

// Write the updated contents back to the file
Expand Down
Loading

0 comments on commit c58b503

Please sign in to comment.