Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optimize CLI script #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ By default, it writes the output to `scrape.md`. Alternatively you can provide a
`npm run query <question>` runs the codebase query agent at *src/swe/discovery/codebaseQuery.ts* which can answer ad hoc
questions about a codebase/folder contents.

### optimize

`npm run optimize` runs the optimizeProjectStructure function configured in `src/cli/cli.ts`

Without arguments, the script will optimize the project structure based on predefined rules. You can also provide specific options to customize the optimization process.

For example, you could run:
```bash
npm run optimize -- --rules=custom-rules.json
```

This will use the custom rules defined in `custom-rules.json` to optimize the project structure.

## Development

Expand Down
17 changes: 17 additions & 0 deletions src/cli/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { unlinkSync } from 'node:fs';
import { expect } from 'chai';
import { systemDir } from '../appVars';
import { parseUserCliArgs, saveAgentId } from './cli';
import { optimizeProjectStructure } from '#swe/codeEditingAgent';

describe('parseProcessArgs', () => {
beforeEach(() => {
Expand Down Expand Up @@ -40,3 +41,19 @@ describe('parseProcessArgs', () => {
expect(result.initialPrompt).to.be.empty;
});
});

describe('optimizeProjectStructure', () => {
it('should optimize project structure with default rules', async () => {
const result = await optimizeProjectStructure({});
expect(result).to.be.undefined;
});

it('should optimize project structure with custom rules', async () => {
const customRules = JSON.stringify([
{ type: 'move', from: 'src/old-folder', to: 'src/new-folder' },
{ type: 'delete', target: 'src/temp-file.ts' },
]);
const result = await optimizeProjectStructure({ rules: customRules });
expect(result).to.be.undefined;
});
});
11 changes: 11 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import path, { join } from 'path';
import { logger } from '#o11y/logger';
import { systemDir } from '../appVars';
import { optimizeProjectStructure } from '#swe/codeEditingAgent';

export interface CliOptions {
/** Name of the executed .ts file without the extension */
Expand Down Expand Up @@ -68,3 +69,13 @@ export function getLastRunAgentId(scriptName: string): string | undefined {
logger.warn(`No agent to resume for ${scriptName} script`);
return undefined;
}

export async function main() {
const { initialPrompt } = parseProcessArgs();
await optimizeProjectStructure(initialPrompt);
}

main().then(
() => console.log('Optimization complete'),
(e) => console.error(e),
);
49 changes: 49 additions & 0 deletions src/swe/codeEditingAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,53 @@ Then respond in following format:
const response: any = await llms().medium.generateJson(prompt, { id: 'extractFilenames' });
return response.files;
}

/**
* Optimizes the project structure based on predefined rules or custom rules provided by the user.
* @param options The options for optimizing the project structure, including custom rules if any.
*/
@func()
async optimizeProjectStructure(options: { rules?: string }): Promise<void> {
const fs: FileSystemService = getFileSystem();
const rules = options.rules ? JSON.parse(await fs.readFile(options.rules)) : this.getDefaultOptimizationRules();

for (const rule of rules) {
// Apply each optimization rule to the project structure
await this.applyOptimizationRule(rule);
}

logger.info('Project structure optimization complete.');
}

/**
* Returns the default optimization rules for the project structure.
*/
private getDefaultOptimizationRules(): any[] {
return [
// Add default optimization rules here
{ type: 'move', from: 'src/old-folder', to: 'src/new-folder' },
{ type: 'delete', target: 'src/temp-file.ts' },
// Add more rules as needed
];
}

/**
* Applies a single optimization rule to the project structure.
* @param rule The optimization rule to apply.
*/
private async applyOptimizationRule(rule: any): Promise<void> {
const fs: FileSystemService = getFileSystem();

switch (rule.type) {
case 'move':
await fs.move(rule.from, rule.to);
break;
case 'delete':
await fs.delete(rule.target);
break;
// Add more rule types as needed
default:
logger.warn(`Unknown optimization rule type: ${rule.type}`);
}
}
}