@online-tools/work-agent-runner 0.0.5-alpha-1 → 0.1.0-alpha-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +65 -0
- package/README.md +289 -116
- package/bin/main.mjs +1948 -436
- package/lib/check.mjs +391 -0
- package/lib/claude-cli.mjs +8 -0
- package/lib/claude-install.mjs +221 -0
- package/lib/config.mjs +48 -4
- package/lib/connection.mjs +87 -4
- package/lib/control.mjs +330 -0
- package/lib/diagnose.mjs +82 -0
- package/lib/doctor.mjs +19 -3
- package/lib/errors.mjs +43 -0
- package/lib/jobs.mjs +428 -91
- package/lib/log.mjs +100 -7
- package/lib/maintenance.mjs +394 -0
- package/lib/sandbox.mjs +55 -6
- package/lib/service.mjs +42 -6
- package/lib/setup-state.mjs +119 -0
- package/lib/setup.mjs +4 -1
- package/lib/state.mjs +145 -0
- package/lib/uninstall.mjs +122 -0
- package/lib/views.mjs +260 -0
- package/lib/work-agent.mjs +27 -6
- package/package.json +2 -2
package/lib/views.mjs
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// What `status`, `jobs` and `paths` print (docs/design.md 5.1, 3.22): pure
|
|
2
|
+
// functions from the data the instance (or its state files) reports to lines
|
|
3
|
+
// of text, so the commands stay thin and the wording is tested in one place.
|
|
4
|
+
//
|
|
5
|
+
// Only node: built-ins and files in this directory.
|
|
6
|
+
|
|
7
|
+
import { ALL_FEATURES, FEATURE_NAMES } from './check.mjs';
|
|
8
|
+
import { FEATURE_LABELS } from './jobs.mjs';
|
|
9
|
+
|
|
10
|
+
const GB = 1024 ** 3;
|
|
11
|
+
|
|
12
|
+
const PHASE_LABELS = {
|
|
13
|
+
received: '收到',
|
|
14
|
+
accepted: '已接单',
|
|
15
|
+
waiting: '等待中',
|
|
16
|
+
preparing: '准备中',
|
|
17
|
+
running: '运行中',
|
|
18
|
+
finishing: '收尾中',
|
|
19
|
+
cleaning: '清理中',
|
|
20
|
+
done: '结束',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const RESULT_LABELS = {
|
|
24
|
+
ok: '✓ 成功',
|
|
25
|
+
failed: '✗ 失败',
|
|
26
|
+
cancelled: '· 取消',
|
|
27
|
+
timeout: '✗ 超时',
|
|
28
|
+
not_accepted: '· 未接单',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const REASON_LABELS = {
|
|
32
|
+
starting_checks: '启动检查进行中',
|
|
33
|
+
clone_missing: 'work-agent 仓库不可用',
|
|
34
|
+
pool_update: '正在更新知识仓',
|
|
35
|
+
disk_low: '工作目录磁盘空间不足',
|
|
36
|
+
unconfirmed: '网页上还没有确认这台机器',
|
|
37
|
+
outdated: '程序版本过低',
|
|
38
|
+
upgrade: '正在升级程序',
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Characters a terminal draws two columns wide (CJK, fullwidth forms): columns
|
|
42
|
+
// line up by what is drawn, not by how many characters there are.
|
|
43
|
+
const WIDE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uA000-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6]/u;
|
|
44
|
+
|
|
45
|
+
/** How many terminal columns `text` takes. */
|
|
46
|
+
export function displayWidth(text) {
|
|
47
|
+
let width = 0;
|
|
48
|
+
for (const char of String(text)) width += WIDE.test(char) ? 2 : 1;
|
|
49
|
+
return width;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `text` and spaces up to `width` columns (none when it is that wide already). */
|
|
53
|
+
export function padDisplay(text, width) {
|
|
54
|
+
return `${text}${' '.repeat(Math.max(0, width - displayWidth(text)))}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// status: labels in a column of this width ("近 24 小时" is the widest).
|
|
58
|
+
const LABEL_WIDTH = 12;
|
|
59
|
+
const INDENT = ' '.repeat(LABEL_WIDTH);
|
|
60
|
+
|
|
61
|
+
export function featureLabel(feature) {
|
|
62
|
+
return FEATURE_LABELS[feature] || feature || '(未知)';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** "12 分 31 秒", "3 小时 4 分", "5 秒". */
|
|
66
|
+
export function formatDuration(ms) {
|
|
67
|
+
const seconds = Math.max(0, Math.round(ms / 1000));
|
|
68
|
+
if (seconds < 60) return `${seconds} 秒`;
|
|
69
|
+
const minutes = Math.floor(seconds / 60);
|
|
70
|
+
if (minutes < 60) return `${minutes} 分 ${seconds % 60} 秒`;
|
|
71
|
+
const hours = Math.floor(minutes / 60);
|
|
72
|
+
if (hours < 48) return `${hours} 小时 ${minutes % 60} 分`;
|
|
73
|
+
return `${Math.floor(hours / 24)} 天 ${hours % 24} 小时`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** "12-01 10:12:03" in local time ('' for no time). */
|
|
77
|
+
export function formatTime(value) {
|
|
78
|
+
const time = Date.parse(value || '');
|
|
79
|
+
if (!Number.isFinite(time)) return '';
|
|
80
|
+
const date = new Date(time);
|
|
81
|
+
const pad = (number) => String(number).padStart(2, '0');
|
|
82
|
+
return `${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** "6 秒前", "3 分钟前", "2 小时前", "3 天前"; "3 小时后" for times ahead. */
|
|
86
|
+
export function relative(value, now = Date.now()) {
|
|
87
|
+
const time = Date.parse(value || '');
|
|
88
|
+
if (!Number.isFinite(time)) return '';
|
|
89
|
+
const diff = now - time;
|
|
90
|
+
const ahead = diff < 0;
|
|
91
|
+
const abs = Math.abs(diff);
|
|
92
|
+
let text;
|
|
93
|
+
if (abs < 60_000) text = `${Math.max(1, Math.round(abs / 1000))} 秒`;
|
|
94
|
+
else if (abs < 3_600_000) text = `${Math.round(abs / 60_000)} 分钟`;
|
|
95
|
+
else if (abs < 86_400_000) text = `${Math.round(abs / 3_600_000)} 小时`;
|
|
96
|
+
else text = `${Math.round(abs / 86_400_000)} 天`;
|
|
97
|
+
return ahead ? `${text}后` : `${text}前`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function gigabytes(bytes) {
|
|
101
|
+
return `${(bytes / GB).toFixed(bytes >= 100 * GB ? 0 : 1)} GB`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** A short run id for tables: skill_1764…a3f1. */
|
|
105
|
+
export function shortRunId(runId) {
|
|
106
|
+
const id = String(runId || '');
|
|
107
|
+
return id.length > 20 ? `${id.slice(0, 10)}…${id.slice(-4)}` : id;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The live part of `status`: from the instance's snapshot (status.json shape).
|
|
112
|
+
* `stale`: read from the file, because no instance answered — `silentPid`: one
|
|
113
|
+
* runs but did not answer (`silentReason`), else none runs.
|
|
114
|
+
*/
|
|
115
|
+
export function statusLines(snapshot, { now = Date.now(), stale = false, silentPid = 0, silentReason = '' } = {}) {
|
|
116
|
+
const lines = [];
|
|
117
|
+
const say = (label, text) => lines.push(`${padDisplay(label, LABEL_WIDTH)}${text}`);
|
|
118
|
+
const instance = snapshot.instance || {};
|
|
119
|
+
if (stale && silentPid) {
|
|
120
|
+
lines.push(`程序在运行(pid ${silentPid}),但没有回应管理接口${silentReason ? `(${silentReason})` : ''}。以下是记录里的状态(${relative(snapshot.updatedAt, now)}):`);
|
|
121
|
+
} else if (stale) {
|
|
122
|
+
lines.push(`程序没有在运行。以下是它最后一次记录的状态(${relative(snapshot.updatedAt, now)}):`);
|
|
123
|
+
} else {
|
|
124
|
+
const how = { systemd: '后台服务(systemd)', launchd: '后台服务(launchd)' }[instance.service] || '前台';
|
|
125
|
+
lines.push(`work-agent-runner ${instance.version || ''} · ${how}运行中(pid ${instance.pid},已运行 ${formatDuration(now - Date.parse(instance.startedAt || now))})`);
|
|
126
|
+
}
|
|
127
|
+
const connection = snapshot.connection || {};
|
|
128
|
+
const machine = snapshot.machine || {};
|
|
129
|
+
if (connection.connected) {
|
|
130
|
+
const confirmed = machine.confirmed === false ? ',网页上还没有确认' : '';
|
|
131
|
+
say('连接', `✓ 已连接 ${connection.server || ''} · 最近心跳 ${relative(connection.lastHeartbeatAt, now) || '—'} · 登记为「${machine.name || ''}」${confirmed}${machine.role === 'server' ? ' · 服务器执行机器' : ''}`);
|
|
132
|
+
} else {
|
|
133
|
+
say('连接', `✗ 未连接 ${connection.server || ''}${connection.lastError ? `:${connection.lastError}` : ''}`);
|
|
134
|
+
}
|
|
135
|
+
const state = snapshot.state || {};
|
|
136
|
+
if (state.accepting) say('接单', '✓ 接单中');
|
|
137
|
+
else say('接单', `✗ 暂不接单:${REASON_LABELS[state.reason] || state.reason || '未连接'}`);
|
|
138
|
+
const jobs = Array.isArray(snapshot.jobs) ? snapshot.jobs : [];
|
|
139
|
+
if (!jobs.length) say('任务', '没有运行中的任务');
|
|
140
|
+
else {
|
|
141
|
+
say('任务', `${jobs.length} 个`);
|
|
142
|
+
for (const job of jobs) {
|
|
143
|
+
const timeout = job.timeoutAt ? `,${relative(job.timeoutAt, now)}超时` : '';
|
|
144
|
+
lines.push(`${INDENT}${shortRunId(job.runId)} ${featureLabel(job.feature)} ${PHASE_LABELS[job.phase] || job.phase} ${formatDuration(now - Date.parse(job.phaseSince || now))}${timeout}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const maintenance = snapshot.maintenance || {};
|
|
148
|
+
const repo = maintenance.repo || {};
|
|
149
|
+
const pool = maintenance.pool || {};
|
|
150
|
+
const repoLast = repo.lastRunAt ? ` · ${relative(repo.lastRunAt, now)}${repo.result === 'ok' ? '刷新成功' : `刷新失败(${repo.error})`}` : '';
|
|
151
|
+
say('维护', `work-agent ${repo.commit ? repo.commit.slice(0, 7) : '不可用'}${repoLast}${repo.nextAt ? ` · 下次 ${formatTime(repo.nextAt)}` : ''}`);
|
|
152
|
+
if (pool.repos) {
|
|
153
|
+
const poolLast = pool.running ? ' · 正在更新' : pool.lastRunAt ? ` · ${relative(pool.lastRunAt, now)}${pool.result === 'ok' ? '更新成功' : `更新失败(${pool.error})`}` : pool.updatedAt ? ` · ${relative(pool.updatedAt, now)}更新` : '';
|
|
154
|
+
lines.push(`${INDENT}知识仓 ${pool.repos} 个仓${poolLast}${pool.requested ? ' · 已请求更新' : ''}${pool.nextAt ? ` · 下次 ${formatTime(pool.nextAt)}` : ''}`);
|
|
155
|
+
} else {
|
|
156
|
+
lines.push(`${INDENT}知识仓未初始化(work-agent-runner setup 可以下载)`);
|
|
157
|
+
}
|
|
158
|
+
const caps = snapshot.capabilities || {};
|
|
159
|
+
const sandbox = caps.sandbox || {};
|
|
160
|
+
const checked = maintenance.checks?.lastRunAt ? `(${relative(maintenance.checks.lastRunAt, now)}检测)` : '';
|
|
161
|
+
say('环境', `Claude Code ${caps.claude || '不可用'} · ${sandbox.ok ? '沙箱可用' : `沙箱不可用:${sandbox.reason || '尚未检测'}`}${checked}`);
|
|
162
|
+
const resources = snapshot.resources || {};
|
|
163
|
+
const parts = [];
|
|
164
|
+
if (resources.workRootFree) parts.push(`工作目录可用 ${gigabytes(resources.workRootFree)}`);
|
|
165
|
+
if (resources.memTotal) parts.push(`内存可用 ${gigabytes(resources.memAvailable || 0)} / ${gigabytes(resources.memTotal)}`);
|
|
166
|
+
if (Array.isArray(resources.loadAvg)) parts.push(`负载 ${resources.loadAvg[0]}`);
|
|
167
|
+
if (parts.length) say('资源', parts.join(' · '));
|
|
168
|
+
const counts = snapshot.last24h;
|
|
169
|
+
if (counts) {
|
|
170
|
+
const total = counts.ok + counts.failed + counts.cancelled + counts.timeout;
|
|
171
|
+
const last = counts.lastError?.code ? ` 最近错误 ${counts.lastError.code}(${relative(counts.lastError.at, now)})` : '';
|
|
172
|
+
say('近 24 小时', `任务 ${total}:成功 ${counts.ok} · 失败 ${counts.failed} · 取消 ${counts.cancelled} · 超时 ${counts.timeout}${last}`);
|
|
173
|
+
}
|
|
174
|
+
for (const problem of snapshot.degraded || []) say('问题', `${problem.code} ${problem.message}`);
|
|
175
|
+
return lines;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** `jobs`: running ones first, then the history (newest first). */
|
|
179
|
+
export function jobsLines({ running = [], history = [] }, { now = Date.now(), failedOnly = false } = {}) {
|
|
180
|
+
const lines = [];
|
|
181
|
+
if (running.length) {
|
|
182
|
+
lines.push(`运行中(${running.length}):`);
|
|
183
|
+
// The whole id: it is what `jobs show` / `jobs cancel` take.
|
|
184
|
+
for (const job of running) {
|
|
185
|
+
lines.push(` ${job.runId} ${featureLabel(job.feature)} ${PHASE_LABELS[job.phase] || job.phase} ${formatDuration(now - Date.parse(job.phaseSince || now))}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!history.length) {
|
|
189
|
+
lines.push(failedOnly ? '没有失败的任务记录' : '还没有任务记录');
|
|
190
|
+
return lines;
|
|
191
|
+
}
|
|
192
|
+
lines.push(`${failedOnly ? '失败' : '最近'}的任务(${history.length} 条):`);
|
|
193
|
+
for (const record of history) {
|
|
194
|
+
const at = record.at || {};
|
|
195
|
+
const took = at.received && at.done ? formatDuration(Date.parse(at.done) - Date.parse(at.received)) : '';
|
|
196
|
+
const why = record.result !== 'ok' && record.error?.message ? ` ${record.error.code ? `${record.error.code} ` : ''}${record.error.message}` : '';
|
|
197
|
+
lines.push(` ${formatTime(at.done)} ${padDisplay(RESULT_LABELS[record.result] || record.result, 8)} ${padDisplay(took, 12)} ${featureLabel(record.feature)} ${record.runId}${why}`);
|
|
198
|
+
}
|
|
199
|
+
return lines;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** `jobs show <runId>`: one record in full — finished (history), or running (a snapshot entry: no result). */
|
|
203
|
+
export function jobDetailLines(record, { now = Date.now() } = {}) {
|
|
204
|
+
const at = record.at || {};
|
|
205
|
+
const lines = [`任务 ${record.runId}`, ` 功能:${featureLabel(record.feature)}${record.interactive ? '(交互执行)' : ''}`];
|
|
206
|
+
if (!record.result) {
|
|
207
|
+
const timeout = record.timeoutAt ? `,${relative(record.timeoutAt, now)}超时` : '';
|
|
208
|
+
lines.push(` 状态:${PHASE_LABELS[record.phase] || record.phase || '运行中'}(${formatDuration(now - Date.parse(record.phaseSince || now))})${timeout}`);
|
|
209
|
+
} else {
|
|
210
|
+
lines.push(` 结果:${RESULT_LABELS[record.result] || record.result}${record.error?.message ? `:${record.error.code ? `${record.error.code} ` : ''}${record.error.message}` : ''}`);
|
|
211
|
+
}
|
|
212
|
+
if (record.exit) lines.push(` 退出:${record.exit.timedOut ? '超时,' : ''}退出码 ${record.exit.code ?? '—'}${record.exit.signal ? `,信号 ${record.exit.signal}` : ''}`);
|
|
213
|
+
if (record.workAgentCommit) lines.push(` work-agent:${record.workAgentCommit.slice(0, 7)}`);
|
|
214
|
+
if (record.isolation) lines.push(` WORK_AGENT_HOME:${record.isolation === 'per-run' ? '任务独立' : '共享'}`);
|
|
215
|
+
if (record.outputBytes) lines.push(` 输出:${(record.outputBytes / 1024).toFixed(1)} KB`);
|
|
216
|
+
if (record.waitedMs) lines.push(` 开始前等待:${formatDuration(record.waitedMs)}`);
|
|
217
|
+
lines.push(' 过程:');
|
|
218
|
+
const order = ['received', 'accepted', 'waiting', 'preparing', 'running', 'finishing', 'cleaning', 'done'];
|
|
219
|
+
let previous = 0;
|
|
220
|
+
for (const phase of order) {
|
|
221
|
+
if (!at[phase]) continue;
|
|
222
|
+
const time = Date.parse(at[phase]);
|
|
223
|
+
lines.push(` ${formatTime(at[phase])} ${PHASE_LABELS[phase]}${previous ? `(+${formatDuration(time - previous)})` : ''}`);
|
|
224
|
+
previous = time;
|
|
225
|
+
}
|
|
226
|
+
return lines;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const CHECK_MARKS = { ok: '✓', warn: '⚠', fail: '✗', info: '·', skip: '-' };
|
|
230
|
+
|
|
231
|
+
/** `check`: the report of lib/check.mjs, group by group, then the verdict. */
|
|
232
|
+
export function checkLines(report) {
|
|
233
|
+
const lines = [];
|
|
234
|
+
const width = Math.max(0, ...report.groups.flatMap((group) => group.items.map((entry) => displayWidth(entry.title)))) + 2;
|
|
235
|
+
const indent = ' '.repeat(4 + width);
|
|
236
|
+
for (const group of report.groups) {
|
|
237
|
+
lines.push(group.title);
|
|
238
|
+
for (const entry of group.items) {
|
|
239
|
+
lines.push(` ${CHECK_MARKS[entry.status] || '·'} ${padDisplay(entry.title, width)}${entry.detail}${entry.code ? `(${entry.code})` : ''}`);
|
|
240
|
+
for (const note of entry.notes || []) lines.push(`${indent}${note}`);
|
|
241
|
+
if (entry.status === 'fail' && entry.affects?.length) {
|
|
242
|
+
const all = ALL_FEATURES.every((feature) => entry.affects.includes(feature));
|
|
243
|
+
lines.push(`${indent}影响:${all ? '所有功能' : entry.affects.map((feature) => FEATURE_NAMES[feature] || feature).join('、')}`);
|
|
244
|
+
}
|
|
245
|
+
if (entry.status !== 'ok' && entry.fix) lines.push(`${indent}处理:${entry.fix}`);
|
|
246
|
+
if (entry.status !== 'ok' && entry.docs) lines.push(`${indent}说明:${entry.docs}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
lines.push('', `结论:${report.summary}`);
|
|
250
|
+
return lines;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** `paths`: [{ label, path, note, free, check }] → aligned lines. */
|
|
254
|
+
export function pathsLines(rows) {
|
|
255
|
+
const width = Math.max(...rows.map((row) => displayWidth(row.label))) + 2;
|
|
256
|
+
return rows.map((row) => {
|
|
257
|
+
const extras = [row.note, row.free ? `可用 ${gigabytes(row.free)}` : '', row.check].filter(Boolean).join(' · ');
|
|
258
|
+
return `${padDisplay(row.label, width)}${row.path || '—'}${extras ? ` (${extras})` : ''}`;
|
|
259
|
+
});
|
|
260
|
+
}
|
package/lib/work-agent.mjs
CHANGED
|
@@ -116,8 +116,10 @@ function gitEnv(env) {
|
|
|
116
116
|
// repos into the Skill tree (work-agent has submodules inside .claude/skills).
|
|
117
117
|
const GIT_CONFIG = ['-c', 'credential.helper=', '-c', 'submodule.recurse=false'];
|
|
118
118
|
|
|
119
|
+
/** Why git failed, from its stderr: the first fatal: / error: line (git's last line is often advice). */
|
|
119
120
|
function lastLine(text) {
|
|
120
|
-
|
|
121
|
+
const lines = String(text || '').trim().split('\n').map((line) => line.trim()).filter(Boolean);
|
|
122
|
+
return (lines.find((line) => /^(fatal|error):/i.test(line)) || lines.pop() || '').replace(/^(fatal|error):\s*/i, '');
|
|
121
123
|
}
|
|
122
124
|
|
|
123
125
|
/** Quick local queries (rev-parse, log, status): synchronous. */
|
|
@@ -288,14 +290,17 @@ export class WorkAgent {
|
|
|
288
290
|
* Runs `fn` alone among clone operations: after anything queued here, and
|
|
289
291
|
* holding work-agent.lock, for which it waits up to `waitMs`.
|
|
290
292
|
*/
|
|
291
|
-
withClone(fn, { waitMs = this.lockWaitMs } = {}) {
|
|
293
|
+
withClone(fn, { waitMs = this.lockWaitMs, onWait = null } = {}) {
|
|
292
294
|
const next = this.cloneQueue.then(async () => {
|
|
293
295
|
fs.mkdirSync(path.dirname(this.paths.repoLockFile), { recursive: true, mode: 0o700 });
|
|
294
296
|
let lock;
|
|
295
297
|
try {
|
|
296
298
|
lock = await acquireLockWaiting(this.paths.repoLockFile, {
|
|
297
299
|
timeoutMs: waitMs,
|
|
298
|
-
onWait: (pid) =>
|
|
300
|
+
onWait: (pid) => {
|
|
301
|
+
this.note('info', `work-agent 仓库正被另一个进程使用(pid ${pid}),等待它结束……`);
|
|
302
|
+
onWait?.(pid);
|
|
303
|
+
},
|
|
299
304
|
});
|
|
300
305
|
} catch (error) {
|
|
301
306
|
if (error instanceof LockedError) {
|
|
@@ -650,7 +655,7 @@ export class WorkAgent {
|
|
|
650
655
|
* for reading and running, not for pushing; so is any symlink that leads out
|
|
651
656
|
* of the repository. Returns the commit that was copied.
|
|
652
657
|
*/
|
|
653
|
-
copyRepoInto(targetDir, targets) {
|
|
658
|
+
copyRepoInto(targetDir, targets, { onWait = null } = {}) {
|
|
654
659
|
return this.withClone(() => {
|
|
655
660
|
const state = this.state();
|
|
656
661
|
if (!state.ok) throw new Error(state.reason);
|
|
@@ -660,7 +665,7 @@ export class WorkAgent {
|
|
|
660
665
|
});
|
|
661
666
|
rewriteSkillPaths(path.join(targetDir, SKILLS_SUBDIR), targets);
|
|
662
667
|
return { commit: state.commit };
|
|
663
|
-
});
|
|
668
|
+
}, { onWait });
|
|
664
669
|
}
|
|
665
670
|
|
|
666
671
|
/** `python3 -I <clone>/scripts/work-agent.py <args>` with the workspace it manages. */
|
|
@@ -672,10 +677,12 @@ export class WorkAgent {
|
|
|
672
677
|
reject(new Error('程序正在退出'));
|
|
673
678
|
return;
|
|
674
679
|
}
|
|
680
|
+
// Line by line to `say`: the program's log, and the terminal that asked
|
|
681
|
+
// (directly, or through the control interface).
|
|
675
682
|
const child = spawn('python3', ['-I', script, ...args], {
|
|
676
683
|
cwd: this.paths.repoDir,
|
|
677
684
|
detached: true,
|
|
678
|
-
stdio: ['ignore', 'inherit', 'inherit'],
|
|
685
|
+
stdio: say ? ['ignore', 'pipe', 'pipe'] : ['ignore', 'inherit', 'inherit'],
|
|
679
686
|
env: {
|
|
680
687
|
...gitEnv(this.env),
|
|
681
688
|
WORK_AGENT_HOME: this.paths.workAgentHome,
|
|
@@ -686,6 +693,20 @@ export class WorkAgent {
|
|
|
686
693
|
},
|
|
687
694
|
});
|
|
688
695
|
this.child = child;
|
|
696
|
+
if (say) {
|
|
697
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
698
|
+
let rest = '';
|
|
699
|
+
stream.setEncoding('utf8');
|
|
700
|
+
stream.on('data', (text) => {
|
|
701
|
+
const parts = (rest + text).split(/\r?\n|\r/);
|
|
702
|
+
rest = parts.pop();
|
|
703
|
+
for (const line of parts) if (line.trim()) say(line);
|
|
704
|
+
});
|
|
705
|
+
stream.on('end', () => {
|
|
706
|
+
if (rest.trim()) say(rest);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
}
|
|
689
710
|
const timer = setTimeout(() => {
|
|
690
711
|
say?.(`已超过 ${Math.round(timeoutMs / 60_000)} 分钟,正在停止`);
|
|
691
712
|
killGroup(child, 'SIGTERM');
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@online-tools/work-agent-runner",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0-alpha-2",
|
|
4
4
|
"description": "GitCode 在线工具的 Agent 执行机器:在自己的机器上运行 work-agent 的 Skill(PR 描述、代码检视)",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": { "work-agent-runner": "bin/work-agent-runner.cjs" },
|
|
8
|
-
"files": ["bin/", "lib/", "README.md", "LICENSE"],
|
|
8
|
+
"files": ["bin/", "lib/", "README.md", "CHANGELOG.md", "LICENSE"],
|
|
9
9
|
"engines": { "node": ">=10.12.0" },
|
|
10
10
|
"os": ["linux", "darwin"],
|
|
11
11
|
"publishConfig": { "access": "public" }
|