@chenacc/dsh-agentbridge 0.1.1 → 0.2.1

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.
Files changed (3) hide show
  1. package/README.md +18 -6
  2. package/lib/index.js +122 -7
  3. package/package.json +4 -1
package/README.md CHANGED
@@ -7,16 +7,26 @@ AgentBridge 的 DSH Desktop 集成插件。在某个项目会话里执行 `/phon
7
7
 
8
8
  ## 前置条件(重要)
9
9
 
10
- 这个插件**只是一个桥接层**,它自己不是服务端。它需要本机运行 AgentBridge 主程序(默认 `http://127.0.0.1:8787`):
10
+ 这个插件**只是一个桥接层**,真正的服务端是本机运行的 AgentBridge 桥(默认 `http://127.0.0.1:8787`)。
11
+
12
+ 从 0.2.0 起,桥作为依赖随插件一起安装,**DSH Desktop 启动时插件会自己在后台把桥拉起来**,所以正常情况下你什么都不用敲,重启桌面后直接 `/phone` 就行。
13
+
14
+ 只有自动启动失败时(沙箱限制、端口被占、装在非常规位置)才需要手动起桥:
11
15
 
12
16
  ```powershell
13
- git clone https://github.com/chenacc1/agentbridge.git
14
- cd agentbridge
15
- npm install
16
- npm start # 或双击仓库里的「启动 AgentBridge.cmd」
17
+ npx @chenacc/agentbridge
17
18
  ```
18
19
 
19
- 没有这一步,`/phone` 会返回"无法连接 AgentBridge",插件不会自己下载或启动任何服务。
20
+ 想长期用、或要改配置(项目白名单、远程通道、通知推送):
21
+
22
+ ```powershell
23
+ npm install -g @chenacc/agentbridge
24
+ agentbridge phone --agent claude
25
+ ```
26
+
27
+ 也可以从仓库跑:`git clone` → `npm install` → `npm start`。
28
+
29
+ 桥起不来时 `/phone` 会把上面这些命令直接印在会话里,不会让你对着一个报错发呆。
20
30
 
21
31
  要求:DSH Desktop 已安装(存在 `%USERPROFILE%\.dsh\profiles\desktop\package.json`),Node.js 22+。
22
32
 
@@ -51,6 +61,8 @@ pnpm add @chenacc/dsh-agentbridge@latest
51
61
  | 变量 | 默认值 | 含义 |
52
62
  |---|---|---|
53
63
  | `AGENTBRIDGE_ORIGIN` | `http://127.0.0.1:8787` | 插件连接的 AgentBridge 地址;桥换了端口或走了隧道时设置它 |
64
+ | `AGENTBRIDGE_DATA_DIR` | `~/.agentbridge`(自动启动时) | 桥的会话、配对与日志目录,不会写进 `~/.dsh` |
65
+ | `AGENTBRIDGE_NO_AUTOSTART` | 未设置 | 设为 `1` 关掉"插件加载时自动起桥",只保留 `/phone` 里的手动提示 |
54
66
 
55
67
  变量要设在**真正启动 DSH Desktop 的环境里**(插件读 `process.env`),不是设在跑 bridge 的终端里。
56
68
 
package/lib/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import { randomUUID } from "node:crypto";
2
+ import { existsSync } from "node:fs";
3
+ import { homedir } from "node:os";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
2
6
 
3
7
  const name = "agentbridge-dsh-phone";
4
8
  // `agents` is not optional: creating a conversation from the phone asks the agent
@@ -63,18 +67,123 @@ function bridgeSetupText(origin) {
63
67
  return [
64
68
  `无法连接 AgentBridge 桥(${origin})。插件本身已经加载,缺的是电脑上这个本地服务。`,
65
69
  "",
66
- "在电脑上开一个终端,第一次使用时依次执行:",
67
- "1) git clone https://github.com/chenacc1/agentbridge.git",
68
- "2) cd agentbridge && npm install",
69
- "3) npm start",
70
+ "手动起桥只要一条命令:",
71
+ "npx @chenacc/agentbridge",
70
72
  "",
71
- "已经下载过这个仓库的话,进到那个目录执行 npm start 就行。",
72
- `桥启动后终端里会打印 ${origin},那时再敲一次 /phone。`,
73
+ "想长期使用就装成全局命令:",
74
+ "npm install -g @chenacc/agentbridge",
75
+ "agentbridge phone --agent claude",
73
76
  "",
74
- "桥不在默认端口的话,先设置环境变量 AGENTBRIDGE_ORIGIN 指向实际地址,再重启 DSH Desktop。",
77
+ `桥启动后再敲一次 /phone。桥不在默认端口的话,先设置环境变量 AGENTBRIDGE_ORIGIN,再重启 DSH Desktop。`,
75
78
  ].join("\n");
76
79
  }
77
80
 
81
+ const BRIDGE_PACKAGE = "@chenacc/agentbridge";
82
+ // Long enough for a detached server to bind its port on a slow disk, short
83
+ // enough that /phone still reads as a command rather than a hang.
84
+ const AUTOSTART_TIMEOUT_MS = 8_000;
85
+
86
+ /**
87
+ * Where the bridge's server entry lives on this machine, if it is installed.
88
+ *
89
+ * The plugin ships `@chenacc/agentbridge` as a dependency, so the normal answer
90
+ * comes from module resolution next to this file. `AGENTBRIDGE_BRIDGE_ENTRY`
91
+ * exists for tests and for unusual installs.
92
+ *
93
+ * The resolved manifest must live under a `node_modules` directory: without that
94
+ * check, Node's package self-reference resolves this repository's own root
95
+ * package as `@chenacc/agentbridge`, so a development checkout would start a
96
+ * second bridge from the source tree and steal port 8787 from the developer.
97
+ */
98
+ function bridgeServerEntry() {
99
+ if (process.env.AGENTBRIDGE_BRIDGE_ENTRY) return process.env.AGENTBRIDGE_BRIDGE_ENTRY;
100
+ try {
101
+ const resolved = fileURLToPath(import.meta.resolve(`${BRIDGE_PACKAGE}/package.json`));
102
+ if (!resolved.split(path.sep).includes("node_modules")) return null;
103
+ return path.join(path.dirname(resolved), "src", "server.js");
104
+ } catch {
105
+ return null;
106
+ }
107
+ }
108
+
109
+ async function bridgeIsReady(origin) {
110
+ try {
111
+ const response = await fetch(`${origin}/api/health`, { signal: AbortSignal.timeout(700) });
112
+ if (!response.ok) return false;
113
+ const body = await response.json().catch(() => null);
114
+ return body?.ok === true;
115
+ } catch {
116
+ return false;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Whether this process may start a bridge at all.
122
+ *
123
+ * A unit test must never spawn a server: node:test sets NODE_TEST_CONTEXT, and a
124
+ * bridge started there would bind a real port and outlive the run. An explicit
125
+ * AGENTBRIDGE_BRIDGE_ENTRY is the single way to opt back in, which is what the
126
+ * autostart test relies on.
127
+ */
128
+ function autoStartAllowed() {
129
+ if (process.env.AGENTBRIDGE_NO_AUTOSTART === "1") return false;
130
+ if (process.env.AGENTBRIDGE_BRIDGE_ENTRY) return true;
131
+ return process.env.NODE_TEST_CONTEXT === undefined;
132
+ }
133
+
134
+ /**
135
+ * Start the local bridge and wait until it answers.
136
+ *
137
+ * Returns true only once the health check passes, so the caller retries the real
138
+ * registration instead of trusting that a spawn means a working server. Every
139
+ * failure path is silent: the caller falls back to the manual setup text.
140
+ */
141
+ async function startBridge(origin) {
142
+ if (await bridgeIsReady(origin)) return true;
143
+ if (!autoStartAllowed()) return false;
144
+ const entry = bridgeServerEntry();
145
+ if (!entry || !existsSync(entry)) return false;
146
+
147
+ let port;
148
+ try {
149
+ port = new URL(origin).port || "8787";
150
+ } catch {
151
+ return false;
152
+ }
153
+
154
+ try {
155
+ // Loaded lazily: the plugin normally runs with a bridge already up, so the
156
+ // child-process machinery is dead weight on the common path.
157
+ const { spawn } = await import("node:child_process");
158
+ const child = spawn(process.execPath, [entry], {
159
+ // State gets its own directory: the bridge writes sessions, pairings and
160
+ // logs under AGENTBRIDGE_DATA_DIR, and dropping that inside the DSH data
161
+ // directory would mix two products' state in one place. The working
162
+ // directory stays the user's home so the bridge's default project is not
163
+ // some internal folder.
164
+ cwd: homedir(),
165
+ detached: true,
166
+ stdio: "ignore",
167
+ windowsHide: true,
168
+ env: {
169
+ ...process.env,
170
+ AGENTBRIDGE_PORT: process.env.AGENTBRIDGE_PORT || port,
171
+ AGENTBRIDGE_DATA_DIR: process.env.AGENTBRIDGE_DATA_DIR || path.join(homedir(), ".agentbridge"),
172
+ },
173
+ });
174
+ child.unref();
175
+ } catch {
176
+ return false;
177
+ }
178
+
179
+ const deadline = Date.now() + AUTOSTART_TIMEOUT_MS;
180
+ while (Date.now() < deadline) {
181
+ await new Promise((resolve) => setTimeout(resolve, 150));
182
+ if (await bridgeIsReady(origin)) return true;
183
+ }
184
+ return false;
185
+ }
186
+
78
187
  function clipped(value, limit) {
79
188
  return typeof value === "string" ? value.slice(0, limit) : "";
80
189
  }
@@ -857,6 +966,12 @@ function apply(ctx) {
857
966
  }
858
967
  },
859
968
  });
969
+
970
+ // Bring the bridge up in the background as soon as the plugin loads, so by the
971
+ // time the user types /phone the local service is already there. Deliberately
972
+ // not awaited: a slow spawn must never delay desktop startup, and every failure
973
+ // is silent — /phone still prints the manual steps when nothing came up.
974
+ void startBridge(String(process.env.AGENTBRIDGE_ORIGIN || DEFAULT_ORIGIN).replace(/\/$/, "")).catch(() => {});
860
975
  }
861
976
 
862
977
  export { apply, inject, name };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chenacc/dsh-agentbridge",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "AgentBridge for DSH Desktop: run /phone in a conversation to pair a phone console with that exact live Agent.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -43,6 +43,9 @@
43
43
  "patch": "./cordis.patch.yml"
44
44
  }
45
45
  },
46
+ "dependencies": {
47
+ "@chenacc/agentbridge": "^0.1.0"
48
+ },
46
49
  "peerDependencies": {
47
50
  "@deepseek-ai/dsh-commands": "^0.1.0-rc.7",
48
51
  "@deepseek-ai/cordis": "^4.0.1"