forked from TrafficGuard/typedai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
81 lines (68 loc) · 2.35 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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 */
scriptName: string;
initialPrompt: string;
resumeAgentId: string | undefined;
}
export function parseProcessArgs(): CliOptions {
const scriptPath = process.argv[1];
let scriptName = scriptPath.split(path.sep).at(-1);
scriptName = scriptName.substring(0, scriptName.length - 3);
return parseUserCliArgs(scriptName, process.argv.slice(2));
}
/**
*
* @param scriptName
* @param scriptArgs copy of the script arguments
*/
export function parseUserCliArgs(scriptName: string, scriptArgs: string[]): CliOptions {
// strip out filesystem arg if it exists
const fsArgIndex = scriptArgs.findIndex((arg) => arg.startsWith('--fs='));
if (fsArgIndex > -1) {
scriptArgs.splice(fsArgIndex, 1);
}
let resumeLastRun = false;
let i = 0;
for (; i < scriptArgs.length; i++) {
if (scriptArgs[i] === '-r') {
resumeLastRun = true;
} else {
break;
}
}
let initialPrompt = scriptArgs.slice(i).join(' ');
logger.info(initialPrompt);
// If not prompt provided then load from file
if (!initialPrompt.trim()) {
if (existsSync(`src/cli/${scriptName}-in`)) initialPrompt = readFileSync(`src/cli/${scriptName}-in`, 'utf-8');
}
logger.info(initialPrompt);
const resumeAgentId = resumeLastRun ? getLastRunAgentId(scriptName) : undefined;
return { scriptName, resumeAgentId, initialPrompt };
}
export function saveAgentId(scriptName: string, agentId: string): void {
const dirPath = join(systemDir(), 'cli');
mkdirSync(dirPath, { recursive: true });
writeFileSync(join(dirPath, `${scriptName}.lastRun`), agentId);
}
export function getLastRunAgentId(scriptName: string): string | undefined {
const filePath = join(systemDir(), 'cli', `${scriptName}.lastRun`);
if (existsSync(filePath)) {
return readFileSync(filePath, 'utf-8').trim();
}
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),
);