lemon.js
5.05 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
const { parseArgs } = require('util');
const http = require('http');
const API_BASE = 'http://localhost:3000/api';
function request(method, path, data) {
return new Promise((resolve, reject) => {
const dataStr = data ? JSON.stringify(data) : '';
const req = http.request(API_BASE + path, {
method,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(dataStr)
}
}, (res) => {
let body = '';
res.on('data', c => body += c);
res.on('end', () => {
try {
const parsed = JSON.parse(body);
if (res.statusCode >= 400) {
resolve({
status: res.statusCode,
data: `❌ [CRITICAL ERROR]: API returned ${res.statusCode}.\nDetails: ${JSON.stringify(parsed)}\n⚡ ACTION REQUIRED: You MUST use the <thought> tag to analyze WHY this failed, correct your arguments, and invoke the tool again. Do NOT give up and do NOT show this raw error to the user.`
});
} else {
resolve({ status: res.statusCode, data: parsed });
}
} catch {
if (res.statusCode >= 400) {
resolve({
status: res.statusCode,
data: `❌ [CRITICAL ERROR]: API returned ${res.statusCode}.\nBody: ${body}\n⚡ ACTION REQUIRED: You MUST use the <thought> tag to analyze WHY this failed, correct your arguments, and invoke the tool again. Do NOT give up and do NOT show this raw error to the user.`
});
} else {
resolve({ status: res.statusCode, data: body });
}
}
});
});
req.on('error', reject);
req.write(dataStr);
req.end();
});
}
async function run() {
const args = process.argv.slice(2);
const command = args[0] + ' ' + args[1];
try {
if (command === 'company create') {
const { values } = parseArgs({ args: args.slice(2), options: {
id: { type: 'string' }, name: { type: 'string' }, desc: { type: 'string' }
}});
const res = await request('POST', '/company/create', { id: values.id, name: values.name, description: values.desc });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'company hire') {
const { values } = parseArgs({ args: args.slice(2), options: {
id: { type: 'string' }, role: { type: 'string' }, type: { type: 'string', default: 'cxo' }, name: { type: 'string' }
}});
const res = await request('POST', `/company/${values.id}/hire`, { role: values.role, type: values.type, name: values.name || values.role });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'company list') {
const res = await request('GET', '/company/list');
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal create') {
const { values } = parseArgs({ args: args.slice(2), options: {
title: { type: 'string' }, desc: { type: 'string' }, owner: { type: 'string' }
}});
const res = await request('POST', '/goals', { title: values.title, description: values.desc, owner: values.owner });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal add-node') {
const { values } = parseArgs({ args: args.slice(2), options: {
goal: { type: 'string' }, parent: { type: 'string' }, title: { type: 'string' }, owner: { type: 'string' }, priority: { type: 'string', default: 'P1' }
}});
const res = await request('POST', `/goals/${values.goal}/nodes`, { parentNodeId: values.parent === 'null' ? null : values.parent, title: values.title, owner: values.owner, priority: values.priority });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'task dispatch') {
const { values } = parseArgs({ args: args.slice(2), options: {
from: { type: 'string' }, to: { type: 'string' }, task: { type: 'string' }, priority: { type: 'string', default: 'P1' },
goal: { type: 'string' }, node: { type: 'string' }, auto: { type: 'boolean', default: true }
}});
const payload = { from: values.from, to: values.to, task: values.task, priority: values.priority, autoExecute: values.auto };
if (values.goal) payload.goalId = values.goal;
if (values.node) payload.nodeId = values.node;
const res = await request('POST', '/agent/task/dispatch', payload);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal list') {
const res = await request('GET', '/goals');
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal status') {
const { values } = parseArgs({ args: args.slice(2), options: { goal: { type: 'string' } }});
const res = await request('GET', `/goals/${values.goal}/progress`);
console.log(JSON.stringify(res.data, null, 2));
}
else {
console.error('Unknown command:', command);
process.exit(1);
}
} catch (err) {
console.error('CRITICAL CLI ERROR:', err.message);
process.exit(1);
}
}
run();