agent-workbench-connector 0.3.1 → 0.3.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/README.md +14 -3
- package/dist/agent.mjs +521 -92
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,9 +8,20 @@
|
|
|
8
8
|
|
|
9
9
|
推荐在工作台“设备与 Agent → 连接设备”点击“生成并复制部署命令”,在开发机终端粘贴运行。命令使用 Node.js 自带的 `npx` 获取最新连接器,兑换服务端的临时令牌后直接启动,无需密码、配置文件或全局安装。支持 macOS、Linux 和 Windows PowerShell,Google 登录账号也适用。
|
|
10
10
|
|
|
11
|
+
`--connect` 默认启动后台进程,在服务器确认设备上线后返回命令提示符,可以关闭终端。终端会显示连接标识、日志路径及停止命令。查看与停止方式:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npx --yes agent-workbench-connector@latest --status
|
|
15
|
+
npx --yes agent-workbench-connector@latest --stop <连接标识>
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
日志和本机控制信息位于 `~/.workflow-data/connectors/`,仅当前用户可读;控制信息不包含工作台登录凭据或设备会话令牌。停止会取消同步、关闭连接并保留待核对的执行状态,不会自动重发 Agent 指令。重复连接同一设备会明确失败;先正常停止旧连接器,再生成新的部署命令。旧版前台进程仍需 Ctrl+C 退出。后台进程崩溃后可重新生成命令启动,状态查询会标为已停止。后台运行不是系统服务,不会在退出系统登录或电脑重启后自动恢复;24 小时设备会话过期后也需重新生成命令。
|
|
19
|
+
|
|
20
|
+
排查时在部署命令末尾添加 `--foreground`,保持前台输出并用 Ctrl+C 停止;配置文件启动方式仍默认保持前台运行。后台启动失败会显示原因和日志路径,不会自动重试令牌兑换或切换连接方式。本机控制通道仅监听 `127.0.0.1`,通过独立随机密钥校验状态与停止请求,不根据持久化 PID 杀进程。控制端口由设备状态目录确定并由操作系统独占,异常退出会释放;若与其他本机程序端口冲突,会明确失败,需先检查并释放提示的端口。
|
|
21
|
+
|
|
11
22
|
`--connect <页面生成的参数>` 携带工作台地址、一次性临时令牌和有效期,不包含浏览器会话或账号密码。令牌最长 10 分钟有效,重新生成后旧命令失效。命令包含接入凭据,请勿分享;跨设备使用 HTTPS,回环地址可用 HTTP。兑换成功后使用独立的 24 小时受限设备会话,仅保存在进程内存中,不写入文件。退出浏览器不影响已接入设备;账号停用、重置密码或撤销执行权限会使设备会话失效。退出连接器或授权失效后需在网页重新生成命令。
|
|
12
23
|
|
|
13
|
-
兑换不会跟随重定向、自动重试或回退到密码登录。网络失败会提示结果未知并停止,不启动替代连接。该模式不读取默认配置文件,不使用环境中的旧账号密码或令牌,不能与 `--env-file` / `--once`
|
|
24
|
+
兑换不会跟随重定向、自动重试或回退到密码登录。网络失败会提示结果未知并停止,不启动替代连接。该模式不读取默认配置文件,不使用环境中的旧账号密码或令牌,不能与 `--env-file` / `--once` 混用;其他高级环境变量仍保留。设备会话通过父子进程的私有 IPC 通道传递,不放入后台进程的命令参数、环境变量、日志或控制文件。先停止已有连接器再启动。
|
|
14
25
|
|
|
15
26
|
维护者需先发布支持令牌版 `--connect` 的连接器,再部署带临时令牌接口、数据库迁移 008 及新版网页的工作台。未发布时,安装维护者提供的 tgz,使用 `agent-workbench-connector` 替换页面命令的 `npx --yes agent-workbench-connector@latest` 前缀。
|
|
16
27
|
|
|
@@ -25,7 +36,7 @@ agent-workbench-connector --env-file ./agent.env
|
|
|
25
36
|
|
|
26
37
|
也可以把配置保存为 `~/.bugflow/agent.env`,然后直接运行 `agent-workbench-connector`。已有环境变量优先于文件;不自动加载当前目录的 `.env`。令牌登录仍支持 `WORKBENCH_TOKEN`。不要把密码或令牌放入命令行参数。
|
|
27
38
|
|
|
28
|
-
|
|
39
|
+
配置文件方式的进程需要保持前台运行,按 Ctrl+C 正常退出。仅同步模式配置 `WORKBENCH_EXECUTE_CODEX=false` 后,可以使用 `agent-workbench-connector --env-file ./agent.env --once` 单次同步。`agent-workbench-connector --help` 查看参数,`agent-workbench-connector --version` 查看版本。
|
|
29
40
|
|
|
30
41
|
执行模式主动连接工作台 `/api/realtime` 的 WebSocket 设备控制通道,令牌通过首条消息认证。收到任务、审批、停止、目录或 Git 请求变化后立即读取状态,并每 3 秒补查。执行输出、审批请求和执行状态先保存到本地日志,再通过同一条 WebSocket 回报,等待服务端保存确认。断线时会提示、继续补查并自动重连;未确认回报保留待重连重报,命令仍通过原有领取和持久回执处理,不会自动重发已提交的 Agent 指令。历史及图片同步独立每轮等待 30 秒,另有每 30 秒心跳;慢同步或目录选择器等待不会阻塞已有执行的停止与审批。退出时取消通知、在途请求和目录选择器,通过 HTTP 回报最后状态,并保存未结束执行供核对。使用这些能力需先更新工作台,再重新打包、升级并重启连接器,无需新增环境变量或数据库迁移;HTTPS 反向代理须允许 `/api/realtime` 的 WebSocket Upgrade。
|
|
31
42
|
|
|
@@ -45,6 +56,6 @@ agent-workbench-connector --env-file ./agent.env
|
|
|
45
56
|
|
|
46
57
|
日常发布可在 GitHub **Actions → Release Agent → Run workflow** 选择 main 和 `patch` / `minor` / `major`。流程自动更新本包版本、提交并打标签,再启动现有 CI / CD 检查和 npm 发布;最终发布状态查看 CI / CD,而非仅看 Release Agent。main / 标签保护必须允许该机器人的推送,否则继续使用版本 PR 和手动标签,不绕过仓库规则。现有 npm Trusted Publisher 仍是 `ci-cd.yml`,无需更换。
|
|
47
58
|
|
|
48
|
-
在工作台仓库运行 `pnpm agent:pack`,生成 `dist/device-agent/agent-workbench-connector-0.3.
|
|
59
|
+
在工作台仓库运行 `pnpm agent:pack`,生成 `dist/device-agent/agent-workbench-connector-0.3.2.tgz`。包内只包含编译后的连接器、说明、许可证和包元数据;上下文工作区代码已打包,无 `workspace:*` 运行时依赖,ACP SDK 由包管理器安装。源码改动后必须重新打包。
|
|
49
60
|
|
|
50
61
|
CI / CD 在 `agent-vX.Y.Z` 标签推送后执行完整检查,再发布同一次构建的 tgz。标签版本必须与本包版本一致,提交必须已合入 main;PR 和普通 main 提交只打包不发布。发布使用独立的 GitHub `npm` Environment 和 npm Trusted Publisher(`Gausons/auto-workflow`、`ci-cd.yml`、Environment `npm`),首次发布也可配置临时 `NPM_TOKEN` Secret。具体初始化步骤见仓库 README 的“连接器 npm 发布”。本机 npm 登录状态不会传递给 CI;包版本一旦发布不能覆盖。安装或构建本身不会自动发布,也不会要求用户在安装机器上构建。
|
package/dist/agent.mjs
CHANGED
|
@@ -2,16 +2,73 @@
|
|
|
2
2
|
import { link, mkdir, open, readFile, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
3
|
import os, { homedir, hostname } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
6
|
import { parseArgs, parseEnv, promisify } from "node:util";
|
|
7
|
+
import { execFile, spawn } from "node:child_process";
|
|
6
8
|
import { createHash, createHmac, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
|
|
7
9
|
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
8
10
|
import { accessSync, constants, createReadStream, existsSync, realpathSync, statSync } from "node:fs";
|
|
9
11
|
import { createInterface } from "node:readline";
|
|
10
|
-
import {
|
|
12
|
+
import { createConnection, createServer } from "node:net";
|
|
11
13
|
import { EventEmitter } from "node:events";
|
|
12
14
|
import { Readable, Writable } from "node:stream";
|
|
13
|
-
import { pathToFileURL } from "node:url";
|
|
14
15
|
import * as acp from "@agentclientprotocol/sdk";
|
|
16
|
+
//#region ../../shared/deviceDeployment.ts
|
|
17
|
+
function validateDeviceDeployment(value) {
|
|
18
|
+
if (!value || typeof value !== "object") throw new Error("部署参数无效,请重新生成部署命令");
|
|
19
|
+
const data = value;
|
|
20
|
+
if (typeof data.url !== "string" || data.url.length > 2048 || typeof data.deploymentToken !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(data.deploymentToken) || typeof data.expiresAt !== "number" || !Number.isSafeInteger(data.expiresAt)) throw new Error("部署参数无效,请重新生成部署命令");
|
|
21
|
+
const address = new URL(data.url);
|
|
22
|
+
if (!["http:", "https:"].includes(address.protocol) || address.username || address.password || address.pathname !== "/" || address.search || address.hash) throw new Error("工作台访问地址无效");
|
|
23
|
+
if (address.protocol !== "https:" && ![
|
|
24
|
+
"localhost",
|
|
25
|
+
"127.0.0.1",
|
|
26
|
+
"[::1]"
|
|
27
|
+
].includes(address.hostname)) throw new Error("临时令牌仅支持 HTTPS 工作台,本机调试可使用 HTTP");
|
|
28
|
+
return {
|
|
29
|
+
url: address.origin,
|
|
30
|
+
deploymentToken: data.deploymentToken,
|
|
31
|
+
expiresAt: data.expiresAt
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/connect.ts
|
|
36
|
+
function parseDeployment(encoded) {
|
|
37
|
+
if (encoded.length > 8192 || !/^[A-Za-z0-9_-]+$/.test(encoded)) throw new Error("部署参数无效,请重新生成部署命令");
|
|
38
|
+
try {
|
|
39
|
+
return validateDeviceDeployment(JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")));
|
|
40
|
+
} catch {
|
|
41
|
+
throw new Error("部署参数无效,请重新生成部署命令");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async function deploymentEnvironment(encoded, environment, fetchImpl = fetch) {
|
|
45
|
+
const deployment = parseDeployment(encoded);
|
|
46
|
+
if (deployment.expiresAt <= Date.now()) throw new Error("临时令牌已过期,请在网页重新生成部署命令");
|
|
47
|
+
let response;
|
|
48
|
+
try {
|
|
49
|
+
response = await fetchImpl(new URL("/api/auth/device-deployment/exchange", deployment.url), {
|
|
50
|
+
method: "POST",
|
|
51
|
+
redirect: "error",
|
|
52
|
+
headers: { "Content-Type": "application/json" },
|
|
53
|
+
body: JSON.stringify({ deploymentToken: deployment.deploymentToken }),
|
|
54
|
+
signal: AbortSignal.timeout(3e4)
|
|
55
|
+
});
|
|
56
|
+
} catch {
|
|
57
|
+
throw new Error("令牌兑换结果未知,未自动重试或启动设备;请在网页重新生成部署命令");
|
|
58
|
+
}
|
|
59
|
+
const data = await response.json().catch(() => null);
|
|
60
|
+
if (!response.ok) throw new Error(response.status === 401 ? "临时令牌已使用或失效,请在网页重新生成部署命令" : `设备授权失败(${response.status}),请检查账号权限后重新生成部署命令`);
|
|
61
|
+
if (!data || typeof data !== "object" || !("token" in data) || typeof data.token !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(data.token)) throw new Error("令牌兑换响应无效,未启动设备;请重新生成部署命令");
|
|
62
|
+
const result = {
|
|
63
|
+
...environment,
|
|
64
|
+
WORKBENCH_URL: deployment.url,
|
|
65
|
+
WORKBENCH_TOKEN: data.token
|
|
66
|
+
};
|
|
67
|
+
delete result.WORKBENCH_USERNAME;
|
|
68
|
+
delete result.WORKBENCH_PASSWORD;
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
15
72
|
//#region ../../src/tenancy.ts
|
|
16
73
|
function canonicalWorkspace(value) {
|
|
17
74
|
let current = path.resolve(value);
|
|
@@ -4794,7 +4851,8 @@ async function syncDeviceOnce({ request, history, deviceId, name, outputDir, inc
|
|
|
4794
4851
|
received
|
|
4795
4852
|
};
|
|
4796
4853
|
}
|
|
4797
|
-
async function runDeviceConnector({ environment = { ...process.env }, once = false, stateRoot = process.cwd() } = {}) {
|
|
4854
|
+
async function runDeviceConnector({ environment = { ...process.env }, once = false, stateRoot = process.cwd(), signal, onStateDirectory, onReady } = {}) {
|
|
4855
|
+
signal?.throwIfAborted();
|
|
4798
4856
|
const defaults = deviceConnectorDefaults(environment);
|
|
4799
4857
|
if (defaults.execute && once) throw new Error("Agent 执行模式需要保持连接器运行;如需单次同步,请设置 WORKBENCH_EXECUTE_CODEX=false");
|
|
4800
4858
|
if (!environment.WORKBENCH_URL) throw new Error("请设置 WORKBENCH_URL,连接参数见 README「多设备执行」");
|
|
@@ -4830,12 +4888,31 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
4830
4888
|
tenantId,
|
|
4831
4889
|
userId
|
|
4832
4890
|
};
|
|
4833
|
-
const
|
|
4834
|
-
|
|
4891
|
+
const directory = await resolveDeviceStateDirectory(environment, connectionIdentity, stateRoot);
|
|
4892
|
+
signal?.throwIfAborted();
|
|
4893
|
+
await mkdir(directory, {
|
|
4835
4894
|
recursive: true,
|
|
4836
4895
|
mode: 448
|
|
4837
4896
|
});
|
|
4897
|
+
const stateDir = await realpath(directory);
|
|
4838
4898
|
await bindDeviceConnection(stateDir, connectionIdentity);
|
|
4899
|
+
const release = await onStateDirectory?.(stateDir, connectionIdentity);
|
|
4900
|
+
try {
|
|
4901
|
+
await runAuthenticatedConnector({
|
|
4902
|
+
environment,
|
|
4903
|
+
once,
|
|
4904
|
+
signal,
|
|
4905
|
+
onReady,
|
|
4906
|
+
defaults,
|
|
4907
|
+
base,
|
|
4908
|
+
token,
|
|
4909
|
+
stateDir
|
|
4910
|
+
});
|
|
4911
|
+
} finally {
|
|
4912
|
+
await release?.();
|
|
4913
|
+
}
|
|
4914
|
+
}
|
|
4915
|
+
async function runAuthenticatedConnector({ environment, once, signal, onReady, defaults, base, token, stateDir }) {
|
|
4839
4916
|
let deviceId;
|
|
4840
4917
|
try {
|
|
4841
4918
|
deviceId = (await readFile(path.join(stateDir, "id"), "utf8")).trim();
|
|
@@ -4857,6 +4934,7 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
4857
4934
|
environment
|
|
4858
4935
|
});
|
|
4859
4936
|
const stopping = new AbortController();
|
|
4937
|
+
const abort = () => stopping.abort();
|
|
4860
4938
|
let shuttingDown = false, heartbeatAgents = ["codex"];
|
|
4861
4939
|
const request = async (method, body, endpoint = "/api/task-center") => {
|
|
4862
4940
|
const binary = body instanceof Uint8Array;
|
|
@@ -4920,6 +4998,8 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
4920
4998
|
}
|
|
4921
4999
|
}) : null;
|
|
4922
5000
|
const stop = () => stopping.abort();
|
|
5001
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
5002
|
+
if (signal?.aborted) abort();
|
|
4923
5003
|
process.once("SIGINT", stop);
|
|
4924
5004
|
process.once("SIGTERM", stop);
|
|
4925
5005
|
console.log(`设备:${environment.WORKBENCH_DEVICE_NAME || hostname()};模式:${worker ? "远程执行" : "仅同步"};交接包目录:${outputDir}`);
|
|
@@ -4929,7 +5009,9 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
4929
5009
|
stopping.abort();
|
|
4930
5010
|
throw error;
|
|
4931
5011
|
}
|
|
4932
|
-
|
|
5012
|
+
let message = error.message;
|
|
5013
|
+
for (const secret of [token, environment.WORKBENCH_PASSWORD]) if (secret) message = message.replaceAll(secret, "[已隐藏]");
|
|
5014
|
+
console.error(`同步未完成:${message};稍后重试`);
|
|
4933
5015
|
};
|
|
4934
5016
|
const heartbeat = async () => {
|
|
4935
5017
|
await request("POST", {
|
|
@@ -4977,35 +5059,34 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
4977
5059
|
};
|
|
4978
5060
|
const loops = [];
|
|
4979
5061
|
try {
|
|
4980
|
-
if (
|
|
4981
|
-
|
|
4982
|
-
|
|
4983
|
-
|
|
4984
|
-
|
|
4985
|
-
}
|
|
4986
|
-
loops.push(runDeviceControl({
|
|
4987
|
-
signal: stopping.signal,
|
|
4988
|
-
work: () => worker.sync(false),
|
|
4989
|
-
git: () => worker.syncGit(),
|
|
4990
|
-
controls: () => worker.syncControls(),
|
|
4991
|
-
heartbeat,
|
|
4992
|
-
onControlsWake: (wake) => {
|
|
4993
|
-
wakeControls = wake;
|
|
4994
|
-
},
|
|
4995
|
-
onError: reportError,
|
|
4996
|
-
subscribe: (wake, signal) => watchDeviceUpdates({
|
|
4997
|
-
url: new URL("/api/realtime", base),
|
|
4998
|
-
token,
|
|
4999
|
-
deviceId,
|
|
5000
|
-
signal,
|
|
5001
|
-
wake,
|
|
5002
|
-
onFeedback: (send) => {
|
|
5003
|
-
feedback = send;
|
|
5004
|
-
},
|
|
5005
|
-
onError: () => console.warn("设备 WebSocket 暂不可用,继续每 3 秒补查控制请求,执行回报保留待重连")
|
|
5006
|
-
})
|
|
5007
|
-
}));
|
|
5062
|
+
if (!once) {
|
|
5063
|
+
stopping.signal.throwIfAborted();
|
|
5064
|
+
await heartbeat();
|
|
5065
|
+
stopping.signal.throwIfAborted();
|
|
5066
|
+
await onReady?.();
|
|
5008
5067
|
}
|
|
5068
|
+
if (worker) loops.push(runDeviceControl({
|
|
5069
|
+
signal: stopping.signal,
|
|
5070
|
+
work: () => worker.sync(false),
|
|
5071
|
+
git: () => worker.syncGit(),
|
|
5072
|
+
controls: () => worker.syncControls(),
|
|
5073
|
+
heartbeat,
|
|
5074
|
+
onControlsWake: (wake) => {
|
|
5075
|
+
wakeControls = wake;
|
|
5076
|
+
},
|
|
5077
|
+
onError: reportError,
|
|
5078
|
+
subscribe: (wake, signal) => watchDeviceUpdates({
|
|
5079
|
+
url: new URL("/api/realtime", base),
|
|
5080
|
+
token,
|
|
5081
|
+
deviceId,
|
|
5082
|
+
signal,
|
|
5083
|
+
wake,
|
|
5084
|
+
onFeedback: (send) => {
|
|
5085
|
+
feedback = send;
|
|
5086
|
+
},
|
|
5087
|
+
onError: () => console.warn("设备 WebSocket 暂不可用,继续每 3 秒补查控制请求,执行回报保留待重连")
|
|
5088
|
+
})
|
|
5089
|
+
}));
|
|
5009
5090
|
loops.push(historyLoop());
|
|
5010
5091
|
await Promise.all(loops);
|
|
5011
5092
|
} finally {
|
|
@@ -5013,65 +5094,378 @@ async function runDeviceConnector({ environment = { ...process.env }, once = fal
|
|
|
5013
5094
|
await Promise.allSettled(loops);
|
|
5014
5095
|
process.removeListener("SIGINT", stop);
|
|
5015
5096
|
process.removeListener("SIGTERM", stop);
|
|
5097
|
+
signal?.removeEventListener("abort", abort);
|
|
5016
5098
|
shuttingDown = true;
|
|
5017
5099
|
await worker?.shutdown();
|
|
5018
5100
|
}
|
|
5019
5101
|
}
|
|
5020
5102
|
//#endregion
|
|
5021
|
-
//#region
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
if (
|
|
5026
|
-
const
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
"
|
|
5030
|
-
"
|
|
5031
|
-
"
|
|
5032
|
-
].includes(
|
|
5103
|
+
//#region src/control.ts
|
|
5104
|
+
var protocol = "agent-workbench-connector";
|
|
5105
|
+
var connectorDirectory = (root) => path.join(root, ".workflow-data", "connectors");
|
|
5106
|
+
function validRecord(value) {
|
|
5107
|
+
if (!value || typeof value !== "object") return false;
|
|
5108
|
+
const record = value;
|
|
5109
|
+
return typeof record.id === "string" && /^[a-f0-9]{24}$/.test(record.id) && Number.isSafeInteger(record.pid) && Number(record.pid) > 0 && Number.isSafeInteger(record.port) && Number(record.port) >= 1024 && Number(record.port) <= 65535 && typeof record.secret === "string" && /^[A-Za-z0-9_-]{43}$/.test(record.secret) && typeof record.stateDir === "string" && typeof record.origin === "string" && typeof record.tenantId === "string" && typeof record.userId === "string" && [
|
|
5110
|
+
"starting",
|
|
5111
|
+
"running",
|
|
5112
|
+
"stopping",
|
|
5113
|
+
"stopped"
|
|
5114
|
+
].includes(String(record.status)) && (record.logFile === void 0 || typeof record.logFile === "string");
|
|
5115
|
+
}
|
|
5116
|
+
async function readRecord(root, id) {
|
|
5117
|
+
if (!/^[a-f0-9]{24}$/.test(id)) throw new Error("连接标识无效,请先运行 --status");
|
|
5118
|
+
const value = JSON.parse(await readFile(path.join(connectorDirectory(root), `${id}.json`), "utf8"));
|
|
5119
|
+
if (!validRecord(value) || value.id !== id) throw new Error("本机连接器状态文件无效,请核对状态目录");
|
|
5120
|
+
return value;
|
|
5121
|
+
}
|
|
5122
|
+
async function command(record, action) {
|
|
5123
|
+
return new Promise((resolve, reject) => {
|
|
5124
|
+
const socket = createConnection({
|
|
5125
|
+
host: "127.0.0.1",
|
|
5126
|
+
port: record.port
|
|
5127
|
+
});
|
|
5128
|
+
socket.setEncoding("utf8");
|
|
5129
|
+
let input = "";
|
|
5130
|
+
const fail = () => {
|
|
5131
|
+
socket.destroy();
|
|
5132
|
+
reject(/* @__PURE__ */ new Error("连接器控制通道不可用"));
|
|
5133
|
+
};
|
|
5134
|
+
socket.setTimeout(1500, fail);
|
|
5135
|
+
socket.once("error", fail);
|
|
5136
|
+
socket.once("connect", () => socket.write(JSON.stringify({
|
|
5137
|
+
protocol,
|
|
5138
|
+
id: record.id,
|
|
5139
|
+
secret: record.secret,
|
|
5140
|
+
action
|
|
5141
|
+
}) + "\n"));
|
|
5142
|
+
socket.on("data", (chunk) => {
|
|
5143
|
+
input += String(chunk);
|
|
5144
|
+
if (input.length > 16384) {
|
|
5145
|
+
fail();
|
|
5146
|
+
return;
|
|
5147
|
+
}
|
|
5148
|
+
if (!input.includes("\n")) return;
|
|
5149
|
+
try {
|
|
5150
|
+
const response = JSON.parse(input.slice(0, input.indexOf("\n")));
|
|
5151
|
+
if (response.protocol !== protocol || response.status?.id !== record.id || response.status.pid !== record.pid) {
|
|
5152
|
+
fail();
|
|
5153
|
+
return;
|
|
5154
|
+
}
|
|
5155
|
+
socket.destroy();
|
|
5156
|
+
resolve(response.status);
|
|
5157
|
+
} catch {
|
|
5158
|
+
fail();
|
|
5159
|
+
}
|
|
5160
|
+
});
|
|
5161
|
+
socket.once("end", () => {
|
|
5162
|
+
if (!input.includes("\n")) fail();
|
|
5163
|
+
});
|
|
5164
|
+
});
|
|
5165
|
+
}
|
|
5166
|
+
async function listConnectors(root) {
|
|
5167
|
+
let files;
|
|
5168
|
+
try {
|
|
5169
|
+
files = await readdir(connectorDirectory(root));
|
|
5170
|
+
} catch (error) {
|
|
5171
|
+
if (error.code === "ENOENT") return [];
|
|
5172
|
+
throw error;
|
|
5173
|
+
}
|
|
5174
|
+
return Promise.all(files.filter((file) => /^[a-f0-9]{24}\.json$/.test(file)).sort().map(async (file) => {
|
|
5175
|
+
const record = await readRecord(root, file.slice(0, -5));
|
|
5176
|
+
const { secret: _secret, ...saved } = record;
|
|
5177
|
+
try {
|
|
5178
|
+
return await command(record, "status");
|
|
5179
|
+
} catch {
|
|
5180
|
+
return {
|
|
5181
|
+
...saved,
|
|
5182
|
+
status: "stopped"
|
|
5183
|
+
};
|
|
5184
|
+
}
|
|
5185
|
+
}));
|
|
5186
|
+
}
|
|
5187
|
+
async function stopConnector(root, id) {
|
|
5188
|
+
const record = await readRecord(root, id);
|
|
5189
|
+
try {
|
|
5190
|
+
await command(record, "status");
|
|
5191
|
+
} catch {
|
|
5192
|
+
return false;
|
|
5193
|
+
}
|
|
5194
|
+
await command(record, "stop");
|
|
5195
|
+
for (let attempt = 0; attempt < 100; attempt++) {
|
|
5196
|
+
await setTimeout$1(100);
|
|
5197
|
+
try {
|
|
5198
|
+
await command(record, "status");
|
|
5199
|
+
} catch {
|
|
5200
|
+
return true;
|
|
5201
|
+
}
|
|
5202
|
+
}
|
|
5203
|
+
throw new Error("已请求正常停止,连接器仍在清理执行状态;请稍后运行 --status 核对,不要重复启动");
|
|
5204
|
+
}
|
|
5205
|
+
async function openConnectorControl(options) {
|
|
5206
|
+
const id = createHash("sha256").update(options.stateDir).digest("hex").slice(0, 24);
|
|
5207
|
+
const port = 2e4 + Number.parseInt(id.slice(0, 8), 16) % 4e4;
|
|
5208
|
+
const record = {
|
|
5209
|
+
id,
|
|
5210
|
+
pid: process.pid,
|
|
5211
|
+
port,
|
|
5212
|
+
...options.identity,
|
|
5213
|
+
stateDir: options.stateDir,
|
|
5214
|
+
logFile: options.logFile,
|
|
5215
|
+
status: "starting",
|
|
5216
|
+
secret: randomBytes(32).toString("base64url")
|
|
5217
|
+
};
|
|
5218
|
+
const publicStatus = () => {
|
|
5219
|
+
const { secret: _secret, ...status } = record;
|
|
5220
|
+
return status;
|
|
5221
|
+
};
|
|
5222
|
+
const server = createServer((socket) => {
|
|
5223
|
+
socket.setEncoding("utf8");
|
|
5224
|
+
let input = "";
|
|
5225
|
+
socket.setTimeout(1500, () => socket.destroy());
|
|
5226
|
+
socket.on("error", () => {});
|
|
5227
|
+
socket.on("data", (chunk) => {
|
|
5228
|
+
input += String(chunk);
|
|
5229
|
+
if (input.length > 4096) {
|
|
5230
|
+
socket.destroy();
|
|
5231
|
+
return;
|
|
5232
|
+
}
|
|
5233
|
+
if (!input.includes("\n")) return;
|
|
5234
|
+
try {
|
|
5235
|
+
const request = JSON.parse(input.slice(0, input.indexOf("\n")));
|
|
5236
|
+
if (request.protocol !== protocol || request.id !== id || typeof request.secret !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(request.secret) || !timingSafeEqual(Buffer.from(request.secret), Buffer.from(record.secret)) || !["status", "stop"].includes(String(request.action))) {
|
|
5237
|
+
socket.destroy();
|
|
5238
|
+
return;
|
|
5239
|
+
}
|
|
5240
|
+
if (request.action === "stop") {
|
|
5241
|
+
record.status = "stopping";
|
|
5242
|
+
options.stop();
|
|
5243
|
+
}
|
|
5244
|
+
socket.end(JSON.stringify({
|
|
5245
|
+
protocol,
|
|
5246
|
+
status: publicStatus()
|
|
5247
|
+
}) + "\n");
|
|
5248
|
+
socket.removeAllListeners("data");
|
|
5249
|
+
} catch {
|
|
5250
|
+
socket.destroy();
|
|
5251
|
+
}
|
|
5252
|
+
});
|
|
5253
|
+
});
|
|
5254
|
+
await new Promise((resolve, reject) => {
|
|
5255
|
+
server.once("error", reject);
|
|
5256
|
+
server.listen({
|
|
5257
|
+
host: "127.0.0.1",
|
|
5258
|
+
port,
|
|
5259
|
+
exclusive: true
|
|
5260
|
+
}, () => {
|
|
5261
|
+
server.removeListener("error", reject);
|
|
5262
|
+
resolve();
|
|
5263
|
+
});
|
|
5264
|
+
}).catch(async (error) => {
|
|
5265
|
+
if (error.code !== "EADDRINUSE") throw error;
|
|
5266
|
+
let existing;
|
|
5267
|
+
try {
|
|
5268
|
+
existing = await command(await readRecord(options.root, id), "status");
|
|
5269
|
+
} catch {}
|
|
5270
|
+
throw new Error(existing ? `该连接器已在运行(${id},PID ${existing.pid});请先使用 --stop ${id} 正常停止` : `本机连接器控制端口 ${port} 已被占用;请先检查并释放该端口`);
|
|
5271
|
+
});
|
|
5272
|
+
const filename = path.join(connectorDirectory(options.root), `${id}.json`);
|
|
5273
|
+
const save = async () => {
|
|
5274
|
+
await writeFile(filename + ".pending", JSON.stringify(record), { mode: 384 });
|
|
5275
|
+
await rename(filename + ".pending", filename);
|
|
5276
|
+
};
|
|
5277
|
+
const closeServer = () => new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
|
5278
|
+
try {
|
|
5279
|
+
await mkdir(connectorDirectory(options.root), {
|
|
5280
|
+
recursive: true,
|
|
5281
|
+
mode: 448
|
|
5282
|
+
});
|
|
5283
|
+
await save();
|
|
5284
|
+
} catch (error) {
|
|
5285
|
+
await closeServer();
|
|
5286
|
+
throw error;
|
|
5287
|
+
}
|
|
5033
5288
|
return {
|
|
5034
|
-
|
|
5035
|
-
|
|
5036
|
-
|
|
5289
|
+
info: publicStatus,
|
|
5290
|
+
async ready() {
|
|
5291
|
+
record.status = "running";
|
|
5292
|
+
await save();
|
|
5293
|
+
},
|
|
5294
|
+
async close() {
|
|
5295
|
+
record.status = "stopped";
|
|
5296
|
+
try {
|
|
5297
|
+
await save();
|
|
5298
|
+
} finally {
|
|
5299
|
+
await closeServer();
|
|
5300
|
+
}
|
|
5301
|
+
}
|
|
5037
5302
|
};
|
|
5038
5303
|
}
|
|
5039
5304
|
//#endregion
|
|
5040
|
-
//#region src/
|
|
5041
|
-
function
|
|
5042
|
-
|
|
5305
|
+
//#region src/background.ts
|
|
5306
|
+
async function runManagedConnector(environment, options = {}) {
|
|
5307
|
+
const stopping = new AbortController();
|
|
5308
|
+
const cancel = () => stopping.abort();
|
|
5309
|
+
options.signal?.addEventListener("abort", cancel, { once: true });
|
|
5310
|
+
if (options.signal?.aborted) cancel();
|
|
5311
|
+
let control;
|
|
5043
5312
|
try {
|
|
5044
|
-
|
|
5045
|
-
|
|
5046
|
-
|
|
5313
|
+
await runDeviceConnector({
|
|
5314
|
+
environment,
|
|
5315
|
+
stateRoot: homedir(),
|
|
5316
|
+
once: options.once,
|
|
5317
|
+
signal: stopping.signal,
|
|
5318
|
+
async onStateDirectory(stateDir, identity) {
|
|
5319
|
+
control = await openConnectorControl({
|
|
5320
|
+
root: homedir(),
|
|
5321
|
+
stateDir,
|
|
5322
|
+
identity,
|
|
5323
|
+
logFile: options.logFile,
|
|
5324
|
+
stop: cancel
|
|
5325
|
+
});
|
|
5326
|
+
return () => control.close();
|
|
5327
|
+
},
|
|
5328
|
+
async onReady() {
|
|
5329
|
+
await control.ready();
|
|
5330
|
+
options.ready?.(control.info());
|
|
5331
|
+
}
|
|
5332
|
+
});
|
|
5333
|
+
} finally {
|
|
5334
|
+
options.signal?.removeEventListener("abort", cancel);
|
|
5047
5335
|
}
|
|
5048
5336
|
}
|
|
5049
|
-
async function
|
|
5050
|
-
const
|
|
5051
|
-
|
|
5052
|
-
|
|
5337
|
+
async function startBackgroundConnector(environment, entry) {
|
|
5338
|
+
const directory = connectorDirectory(homedir());
|
|
5339
|
+
await mkdir(directory, {
|
|
5340
|
+
recursive: true,
|
|
5341
|
+
mode: 448
|
|
5342
|
+
});
|
|
5343
|
+
const logFile = path.join(directory, `${randomUUID()}.log`);
|
|
5344
|
+
const log = await open(logFile, "wx", 384);
|
|
5345
|
+
const inherited = { ...process.env };
|
|
5346
|
+
for (const name of [
|
|
5347
|
+
"WORKBENCH_TOKEN",
|
|
5348
|
+
"WORKBENCH_USERNAME",
|
|
5349
|
+
"WORKBENCH_PASSWORD"
|
|
5350
|
+
]) delete inherited[name];
|
|
5053
5351
|
try {
|
|
5054
|
-
|
|
5055
|
-
|
|
5056
|
-
|
|
5057
|
-
|
|
5058
|
-
|
|
5059
|
-
|
|
5352
|
+
const child = spawn(process.execPath, [
|
|
5353
|
+
...process.execArgv,
|
|
5354
|
+
entry,
|
|
5355
|
+
"--background-worker"
|
|
5356
|
+
], {
|
|
5357
|
+
detached: true,
|
|
5358
|
+
windowsHide: true,
|
|
5359
|
+
stdio: [
|
|
5360
|
+
"ignore",
|
|
5361
|
+
log.fd,
|
|
5362
|
+
log.fd,
|
|
5363
|
+
"ipc"
|
|
5364
|
+
],
|
|
5365
|
+
env: inherited
|
|
5060
5366
|
});
|
|
5061
|
-
|
|
5062
|
-
|
|
5367
|
+
return await new Promise((resolve, reject) => {
|
|
5368
|
+
let finished = false;
|
|
5369
|
+
const cancel = () => {
|
|
5370
|
+
if (child.connected) child.send({ type: "cancel" }, () => {});
|
|
5371
|
+
};
|
|
5372
|
+
const cleanup = () => {
|
|
5373
|
+
clearTimeout(timer);
|
|
5374
|
+
process.removeListener("SIGINT", cancel);
|
|
5375
|
+
process.removeListener("SIGTERM", cancel);
|
|
5376
|
+
};
|
|
5377
|
+
const fail = (message) => {
|
|
5378
|
+
if (finished) return;
|
|
5379
|
+
finished = true;
|
|
5380
|
+
cleanup();
|
|
5381
|
+
cancel();
|
|
5382
|
+
if (child.connected) child.disconnect();
|
|
5383
|
+
child.unref();
|
|
5384
|
+
reject(/* @__PURE__ */ new Error(`${message};未自动重试。日志:${logFile};请使用 --status 核对后再重新生成命令`));
|
|
5385
|
+
};
|
|
5386
|
+
const timer = setTimeout(() => fail("后台连接器启动超时"), 9e4);
|
|
5387
|
+
process.once("SIGINT", cancel);
|
|
5388
|
+
process.once("SIGTERM", cancel);
|
|
5389
|
+
child.once("error", () => fail("无法启动后台连接器"));
|
|
5390
|
+
child.once("exit", () => fail("后台连接器未完成启动,请查看日志"));
|
|
5391
|
+
child.on("message", (message) => {
|
|
5392
|
+
if (finished || !message || typeof message !== "object" || !("type" in message)) return;
|
|
5393
|
+
if (message.type === "error" && "message" in message && typeof message.message === "string") {
|
|
5394
|
+
fail(message.message);
|
|
5395
|
+
return;
|
|
5396
|
+
}
|
|
5397
|
+
if (message.type !== "ready" || !("info" in message) || !message.info || typeof message.info !== "object") return;
|
|
5398
|
+
finished = true;
|
|
5399
|
+
cleanup();
|
|
5400
|
+
if (child.connected) child.disconnect();
|
|
5401
|
+
child.unref();
|
|
5402
|
+
resolve(message.info);
|
|
5403
|
+
});
|
|
5404
|
+
child.send({
|
|
5405
|
+
type: "start",
|
|
5406
|
+
environment,
|
|
5407
|
+
logFile
|
|
5408
|
+
}, (error) => {
|
|
5409
|
+
if (error) fail("无法传递后台连接配置");
|
|
5410
|
+
});
|
|
5411
|
+
});
|
|
5412
|
+
} finally {
|
|
5413
|
+
await log.close();
|
|
5414
|
+
}
|
|
5415
|
+
}
|
|
5416
|
+
async function runBackgroundWorker() {
|
|
5417
|
+
if (!process.send) throw new Error("后台工作进程只能由连接器启动");
|
|
5418
|
+
const stopping = new AbortController();
|
|
5419
|
+
let ready = false;
|
|
5420
|
+
process.on("disconnect", () => {
|
|
5421
|
+
if (!ready) stopping.abort();
|
|
5422
|
+
});
|
|
5423
|
+
process.on("message", (message) => {
|
|
5424
|
+
if (message && typeof message === "object" && "type" in message && message.type === "cancel") stopping.abort();
|
|
5425
|
+
});
|
|
5426
|
+
const input = await new Promise((resolve, reject) => {
|
|
5427
|
+
const timer = setTimeout(() => reject(/* @__PURE__ */ new Error("未收到后台连接配置")), 1e4);
|
|
5428
|
+
process.once("message", (message) => {
|
|
5429
|
+
clearTimeout(timer);
|
|
5430
|
+
if (!message || typeof message !== "object" || !("type" in message) || message.type !== "start" || !("environment" in message) || !message.environment || typeof message.environment !== "object" || !("logFile" in message) || typeof message.logFile !== "string") {
|
|
5431
|
+
reject(/* @__PURE__ */ new Error("后台连接配置无效"));
|
|
5432
|
+
return;
|
|
5433
|
+
}
|
|
5434
|
+
resolve({
|
|
5435
|
+
environment: message.environment,
|
|
5436
|
+
logFile: message.logFile
|
|
5437
|
+
});
|
|
5438
|
+
});
|
|
5439
|
+
}).catch((error) => {
|
|
5440
|
+
if (process.connected) process.disconnect();
|
|
5441
|
+
throw error;
|
|
5442
|
+
});
|
|
5443
|
+
try {
|
|
5444
|
+
await runManagedConnector(input.environment, {
|
|
5445
|
+
logFile: input.logFile,
|
|
5446
|
+
signal: stopping.signal,
|
|
5447
|
+
ready(info) {
|
|
5448
|
+
ready = true;
|
|
5449
|
+
process.send?.({
|
|
5450
|
+
type: "ready",
|
|
5451
|
+
info
|
|
5452
|
+
}, () => {});
|
|
5453
|
+
}
|
|
5454
|
+
});
|
|
5455
|
+
} catch (error) {
|
|
5456
|
+
let message = error instanceof Error ? error.message : "后台连接器执行失败";
|
|
5457
|
+
for (const key of ["WORKBENCH_TOKEN", "WORKBENCH_PASSWORD"]) if (input.environment[key]) message = message.replaceAll(input.environment[key], "[已隐藏]");
|
|
5458
|
+
console.error(message);
|
|
5459
|
+
if (process.connected) process.send?.({
|
|
5460
|
+
type: "error",
|
|
5461
|
+
message
|
|
5462
|
+
}, () => {
|
|
5463
|
+
if (process.connected) process.disconnect();
|
|
5464
|
+
});
|
|
5465
|
+
process.exitCode = 1;
|
|
5466
|
+
} finally {
|
|
5467
|
+
if (process.connected) process.disconnect();
|
|
5063
5468
|
}
|
|
5064
|
-
const data = await response.json().catch(() => null);
|
|
5065
|
-
if (!response.ok) throw new Error(response.status === 401 ? "临时令牌已使用或失效,请在网页重新生成部署命令" : `设备授权失败(${response.status}),请检查账号权限后重新生成部署命令`);
|
|
5066
|
-
if (!data || typeof data !== "object" || !("token" in data) || typeof data.token !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(data.token)) throw new Error("令牌兑换响应无效,未启动设备;请重新生成部署命令");
|
|
5067
|
-
const result = {
|
|
5068
|
-
...environment,
|
|
5069
|
-
WORKBENCH_URL: deployment.url,
|
|
5070
|
-
WORKBENCH_TOKEN: data.token
|
|
5071
|
-
};
|
|
5072
|
-
delete result.WORKBENCH_USERNAME;
|
|
5073
|
-
delete result.WORKBENCH_PASSWORD;
|
|
5074
|
-
return result;
|
|
5075
5469
|
}
|
|
5076
5470
|
//#endregion
|
|
5077
5471
|
//#region src/cli.ts
|
|
@@ -5087,16 +5481,29 @@ async function main() {
|
|
|
5087
5481
|
},
|
|
5088
5482
|
"env-file": { type: "string" },
|
|
5089
5483
|
once: { type: "boolean" },
|
|
5090
|
-
connect: { type: "string" }
|
|
5484
|
+
connect: { type: "string" },
|
|
5485
|
+
foreground: { type: "boolean" },
|
|
5486
|
+
status: { type: "boolean" },
|
|
5487
|
+
stop: { type: "string" },
|
|
5488
|
+
"background-worker": { type: "boolean" }
|
|
5091
5489
|
} });
|
|
5490
|
+
if (values["background-worker"]) {
|
|
5491
|
+
await runBackgroundWorker();
|
|
5492
|
+
return;
|
|
5493
|
+
}
|
|
5092
5494
|
if (values.help) {
|
|
5093
5495
|
console.log(`Agent Workbench 开发机连接器
|
|
5094
5496
|
|
|
5095
5497
|
用法:agent-workbench-connector [--env-file <配置文件>] [--once]
|
|
5096
5498
|
agent-workbench-connector --connect <页面生成的部署参数>
|
|
5499
|
+
agent-workbench-connector --status
|
|
5500
|
+
agent-workbench-connector --stop <连接标识>
|
|
5097
5501
|
|
|
5098
5502
|
--connect <参数> 一键连接:兑换临时令牌,无需输入账号密码
|
|
5099
|
-
|
|
5503
|
+
默认后台运行,上线后释放终端;不保存设备会话令牌
|
|
5504
|
+
--foreground 一键连接时保持前台运行,Ctrl+C 退出
|
|
5505
|
+
--status 查看本机连接器状态、连接标识及日志路径
|
|
5506
|
+
--stop <连接标识> 正常停止指定连接器,不终止其他连接
|
|
5100
5507
|
--env-file <文件> 加载连接配置,已有环境变量优先
|
|
5101
5508
|
默认读取 ~/.bugflow/agent.env(文件存在时)
|
|
5102
5509
|
--once 单次同步,需 WORKBENCH_EXECUTE_CODEX=false
|
|
@@ -5107,7 +5514,7 @@ async function main() {
|
|
|
5107
5514
|
也可使用 WORKBENCH_TOKEN。默认启用远程执行与历史摘要同步。
|
|
5108
5515
|
默认状态目录:~/.workflow-data/devices/<连接标识>
|
|
5109
5516
|
可用 WORKBENCH_DEVICE_DIR 指定原连接器状态目录,保留设备身份与执行日志。
|
|
5110
|
-
需要本机安装并登录 Agent;Git 分支管理需要 Git
|
|
5517
|
+
需要本机安装并登录 Agent;Git 分支管理需要 Git。配置文件方式保持前台运行,Ctrl+C 退出。`);
|
|
5111
5518
|
return;
|
|
5112
5519
|
}
|
|
5113
5520
|
if (values.version) {
|
|
@@ -5115,17 +5522,43 @@ async function main() {
|
|
|
5115
5522
|
console.log(manifest.version);
|
|
5116
5523
|
return;
|
|
5117
5524
|
}
|
|
5525
|
+
if (values.status || values.stop !== void 0) {
|
|
5526
|
+
if (values.status && values.stop !== void 0 || values.connect !== void 0 || values["env-file"] || values.once || values.foreground) throw new Error("--status / --stop 不能与其他运行选项同时使用");
|
|
5527
|
+
if (values.stop !== void 0) console.log(await stopConnector(homedir(), values.stop) ? "连接器已正常停止。" : "连接器已停止,无需重复停止。");
|
|
5528
|
+
else {
|
|
5529
|
+
const connectors = await listConnectors(homedir());
|
|
5530
|
+
if (!connectors.length) console.log("本机暂无连接器记录。");
|
|
5531
|
+
for (const connector of connectors) {
|
|
5532
|
+
const label = {
|
|
5533
|
+
starting: "启动中",
|
|
5534
|
+
running: "运行中",
|
|
5535
|
+
stopping: "停止中",
|
|
5536
|
+
stopped: "已停止"
|
|
5537
|
+
}[connector.status];
|
|
5538
|
+
console.log(`${connector.id} ${label} PID ${connector.pid} ${connector.origin}`);
|
|
5539
|
+
if (connector.logFile) console.log(`日志:${connector.logFile}`);
|
|
5540
|
+
}
|
|
5541
|
+
}
|
|
5542
|
+
return;
|
|
5543
|
+
}
|
|
5544
|
+
if (values.foreground && values.connect === void 0) throw new Error("--foreground 需与 --connect 同时使用");
|
|
5118
5545
|
if (values.connect !== void 0) {
|
|
5119
5546
|
if (values["env-file"] || values.once) throw new Error("--connect 不能与 --env-file 或 --once 同时使用");
|
|
5120
5547
|
const deployment = parseDeployment(values.connect);
|
|
5121
5548
|
console.log(`正在连接 ${deployment.url},验证临时部署令牌…`);
|
|
5122
|
-
console.log("
|
|
5549
|
+
console.log("默认允许远程执行并同步历史摘要。");
|
|
5123
5550
|
const environment = await deploymentEnvironment(values.connect, process.env);
|
|
5124
|
-
|
|
5125
|
-
|
|
5126
|
-
environment
|
|
5127
|
-
|
|
5128
|
-
|
|
5551
|
+
if (values.foreground) {
|
|
5552
|
+
console.log("授权成功,正在前台连接设备;Ctrl+C 退出。");
|
|
5553
|
+
await runManagedConnector(environment);
|
|
5554
|
+
} else {
|
|
5555
|
+
console.log("授权成功,正在启动后台连接器…");
|
|
5556
|
+
const info = await startBackgroundConnector(environment, fileURLToPath(import.meta.url));
|
|
5557
|
+
console.log(`设备已连接,连接器已转入后台(PID ${info.pid}),可以关闭此终端。`);
|
|
5558
|
+
console.log(`连接标识:${info.id}\n日志:${info.logFile}`);
|
|
5559
|
+
const command = "npx --yes --registry=https://registry.npmjs.org agent-workbench-connector@latest";
|
|
5560
|
+
console.log(`查看状态:${command} --status\n停止连接:${command} --stop ${info.id}`);
|
|
5561
|
+
}
|
|
5129
5562
|
return;
|
|
5130
5563
|
}
|
|
5131
5564
|
const config = values["env-file"] ? path.resolve(values["env-file"]) : path.join(homedir(), ".bugflow", "agent.env");
|
|
@@ -5138,11 +5571,7 @@ async function main() {
|
|
|
5138
5571
|
if (!process.env.WORKBENCH_URL) throw new Error("请在 ~/.bugflow/agent.env 配置 WORKBENCH_URL 和登录账号,或使用 --env-file 指定配置文件;运行 --help 查看帮助");
|
|
5139
5572
|
const environment = { ...process.env };
|
|
5140
5573
|
if (environment.WORKBENCH_DEVICE_DIR) environment.WORKBENCH_DEVICE_DIR = path.resolve(environment.WORKBENCH_DEVICE_DIR);
|
|
5141
|
-
await
|
|
5142
|
-
environment,
|
|
5143
|
-
once: values.once,
|
|
5144
|
-
stateRoot: homedir()
|
|
5145
|
-
});
|
|
5574
|
+
await runManagedConnector(environment, { once: values.once });
|
|
5146
5575
|
}
|
|
5147
5576
|
main().catch((error) => {
|
|
5148
5577
|
console.error(error instanceof Error ? error.message : String(error));
|