lemon.js
13.1 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env node
const { parseArgs } = require('util');
const http = require('http');
const API_BASE = process.env.LEMON_API_BASE || 'http://localhost:3010/api';
function request(method, path, data) {
return new Promise((resolve, reject) => {
const dataStr = data ? JSON.stringify(data) : '';
const headers = {};
if (dataStr || method !== 'GET') {
headers['Content-Type'] = 'application/json';
headers['Content-Length'] = Buffer.byteLength(dataStr);
}
// agent-chat.js 挂了 requireAuthOrInternal(38 号文档),这个 CLI 跑在 agent 自己的
// claude CLI 子进程里,没有登录 cookie;LEMON_INTERNAL_TOKEN 是服务端进程 spawn 子进程时
// 顺带传下来的(server/middleware/jwt-auth.js),带上就能过 internal 校验。
if (process.env.LEMON_INTERNAL_TOKEN) {
headers['x-internal-token'] = process.env.LEMON_INTERNAL_TOKEN;
}
const req = http.request(API_BASE + path, {
method,
headers
}, (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' }, desc: { type: 'string' }
}});
const res = await request('POST', `/company/${values.id}/hire`, { role: values.role, type: values.type, name: values.name || values.role, description: values.desc });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'company fire') {
const { values } = parseArgs({ args: args.slice(2), options: {
id: { type: 'string' }, role: { type: 'string' }, archive: { type: 'boolean', default: true }
}});
const res = await request('POST', `/company/${values.id}/fire`, { role: values.role, archive: values.archive });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'company templates') {
const res = await request('GET', '/company/templates');
console.log('\n📦 可用 Agent 模板列表:\n');
if (Array.isArray(res.data)) {
res.data.forEach(t => {
console.log(` • ${t.id || t.name} — ${t.description || '(无描述)'}`);
});
} else {
console.log(JSON.stringify(res.data, null, 2));
}
console.log('');
}
else if (command === 'company list') {
const res = await request('GET', '/company/list');
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'okr create' || 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', '/okr', { title: values.title, description: values.desc, owner: values.owner });
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'okr add-node' || 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', `/okr/${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 }, parent: { type: 'string' }
}});
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;
if (values.parent) payload.parentTaskId = values.parent;
const res = await request('POST', '/agent/task/dispatch', payload);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'okr list' || command === 'goal list') {
const res = await request('GET', '/okr');
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'okr status' || command === 'goal status') {
const { values } = parseArgs({ args: args.slice(2), options: { goal: { type: 'string' } }});
const res = await request('GET', `/okr/${values.goal}/progress`);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal update') {
const { values } = parseArgs({ args: args.slice(2), options: {
goal: { type: 'string' }, status: { type: 'string' }, progress: { type: 'string' }
}});
const payload = {};
if (values.status) payload.status = values.status;
if (values.progress) payload.progress = parseInt(values.progress, 10);
const res = await request('PUT', `/okr/${values.goal}`, payload);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'goal update-node') {
const { values } = parseArgs({ args: args.slice(2), options: {
goal: { type: 'string' }, node: { type: 'string' }, status: { type: 'string' }, owner: { type: 'string' }
}});
const payload = {};
if (values.status) payload.status = values.status;
if (values.owner) payload.owner = values.owner;
const res = await request('PUT', `/okr/${values.goal}/nodes/${values.node}`, payload);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'task report') {
const { values } = parseArgs({ args: args.slice(2), options: {
taskId: { type: 'string' }, from: { type: 'string' }, to: { type: 'string' },
status: { type: 'string', default: 'done' }, summary: { type: 'string', default: '' }
}});
const res = await request('POST', '/agent/task/report', {
taskId: values.taskId, from: values.from, to: values.to,
status: values.status, summary: values.summary
});
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'task reject') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }, runId: { type: 'string' },
stepIndex: { type: 'string' }, reason: { type: 'string', default: '' },
reviewedBy: { type: 'string', default: 'coo' }
}});
const res = await request('POST', '/agent/task/reject', {
companyId: values.companyId, runId: values.runId,
stepIndex: parseInt(values.stepIndex, 10),
reason: values.reason, reviewedBy: values.reviewedBy
});
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'task transition') {
const { values } = parseArgs({ args: args.slice(2), options: {
agentId: { type: 'string' }, phase: { type: 'string' }, artifact: { type: 'string', default: '' }
}});
const res = await request('POST', `/agent/${values.agentId}/task/transition`, {
phase: values.phase, artifact: values.artifact
});
console.log(JSON.stringify(res.data, null, 2));
// 42 号文档 Phase 4:额外打一次 graph-runs 的 phase-note 端点,把这次阶段流转
// 落进 timeline.ndjson(大多数调用方——legacy 会话式 agent——这个端点会安静
// no-op,因为 --artifact 不是 work graph 节点产物目录的形状;不影响上面那次
// 调用的结果,出错也不影响本命令的退出码)。
try {
await request('POST', '/graph-runs/phase-note', {
artifact: values.artifact, phase: values.phase, agentId: values.agentId
});
} catch { /* best-effort,静默丢弃 */ }
}
// 43 号文档 Phase 3:itemized 任务列出它物化出的子任务(coder-validator 逐条验收前先拿列表)
else if (command === 'task subtasks') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }, taskId: { type: 'string' }
}});
const res = await request('GET', `/company/${values.companyId}/tasks?parentTaskId=${encodeURIComponent(values.taskId)}`);
console.log(JSON.stringify(res.data, null, 2));
}
// 43 号文档 Phase 3:逐条上报一个子任务的验收结果(每判完一条验收项调一次)
else if (command === 'task subtask-result') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }, taskId: { type: 'string' },
status: { type: 'string' }, summary: { type: 'string', default: '' }
}});
const res = await request('POST', `/company/${values.companyId}/tasks/${values.taskId}/subtask-result`, {
status: values.status, summary: values.summary
});
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'pipeline create') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }, file: { type: 'string' }
}});
const fs = require('fs');
if (!fs.existsSync(values.file)) {
console.error('File not found:', values.file);
process.exit(1);
}
const data = JSON.parse(fs.readFileSync(values.file, 'utf-8'));
const res = await request('POST', `/company/${values.companyId}/pipelines`, data);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'pipeline list') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }
}});
const res = await request('GET', `/company/${values.companyId}/pipelines`);
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'human request') {
const { values } = parseArgs({ args: args.slice(2), options: {
from: { type: 'string' }, type: { type: 'string', default: 'api_key' },
key: { type: 'string' }, reason: { type: 'string' }
}});
const res = await request('POST', '/agent/human-request', {
from: values.from, type: values.type, key: values.key, reason: values.reason
});
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'schedule add') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }, time: { type: 'string' },
agent: { type: 'string' }, task: { type: 'string' },
type: { type: 'string', default: 'task' }
}});
const res = await request('POST', `/company/${values.companyId}/schedule/slots`, {
time: values.time, agent: values.agent,
task: values.task, type: values.type
});
console.log(JSON.stringify(res.data, null, 2));
}
else if (command === 'schedule list') {
const { values } = parseArgs({ args: args.slice(2), options: {
companyId: { type: 'string' }
}});
const res = await request('GET', `/company/${values.companyId}/schedule`);
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();