-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstart.js
More file actions
executable file
·88 lines (73 loc) · 2.23 KB
/
Copy pathstart.js
File metadata and controls
executable file
·88 lines (73 loc) · 2.23 KB
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
82
83
84
85
86
87
88
#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const concolor = require('concolor');
const { directoryExists, exists } = require('metautil');
const color = concolor({
warn: 'b,yellow',
error: 'b,red',
});
const CONFIG_PATH = path.join(__dirname, 'config.js');
const TEMPLATE_PATH = path.join(__dirname, 'agent', 'config.template.js');
const ensureConfig = () => {
if (fs.existsSync(CONFIG_PATH)) return false;
fs.copyFileSync(TEMPLATE_PATH, CONFIG_PATH);
return true;
};
const createdConfig = ensureConfig();
const { errorText } = require('./agent/agent.js');
const { openWorkspace } = require('./agent/workspace.js');
const { startIde } = require('./ide/ide.js');
const config = require('./config.js');
const DEFAULT_MAX_STEPS = 30;
const USAGE_FILE = path.join(__dirname, 'agent', 'usage.md');
const USAGE = fs.readFileSync(USAGE_FILE, 'utf8').trim();
const resolveApiKey = () => {
const raw = config.API_KEY || '';
return raw.trim();
};
const resolveRoot = async (arg) => {
const root = path.resolve(arg || process.cwd());
const found = await exists(root);
if (!found) throw new Error(`Not found: ${root}`);
const isDir = await directoryExists(root);
if (!isDir) throw new Error(`Not a directory: ${root}`);
return root;
};
const missingKey = () => {
if (createdConfig) console.log(color.warn('Created config.js\n'));
console.log(color.error('API_KEY is not set.\n'));
console.log(USAGE);
};
const launchIde = async (root) => {
if (!process.stdin.isTTY || !process.stdout.isTTY) {
console.log(color.error('IDE needs an interactive terminal.'));
process.exitCode = 1;
return;
}
await openWorkspace(root);
await startIde({
maxSteps: DEFAULT_MAX_STEPS,
model: config.MODEL,
});
};
const main = async () => {
if (!resolveApiKey()) {
missingKey();
process.exitCode = 1;
return;
}
const extra = process.argv.slice(2);
if (extra.length > 1) {
console.log(color.error('Usage: node start.js [project-root]'));
process.exitCode = 1;
return;
}
const root = await resolveRoot(extra[0]);
await launchIde(root);
};
main().catch((error) => {
console.error(color.error(`\nFatal: ${errorText(error)}`));
process.exitCode = 1;
});