@chenacc/dsh-agentbridge 0.2.1 → 0.2.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/lib/index.js +48 -13
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
3
4
|
import { homedir } from "node:os";
|
|
4
5
|
import path from "node:path";
|
|
5
6
|
import { fileURLToPath } from "node:url";
|
|
@@ -90,6 +91,11 @@ const AUTOSTART_TIMEOUT_MS = 8_000;
|
|
|
90
91
|
* comes from module resolution next to this file. `AGENTBRIDGE_BRIDGE_ENTRY`
|
|
91
92
|
* exists for tests and for unusual installs.
|
|
92
93
|
*
|
|
94
|
+
* Resolution goes through `createRequire` first on purpose: `import.meta.resolve`
|
|
95
|
+
* only exists from Node 20.6, and the Electron runtime DSH Desktop loads plugins
|
|
96
|
+
* in can be older than that — an unresolvable answer there fails silently and
|
|
97
|
+
* looks exactly like "no bridge installed".
|
|
98
|
+
*
|
|
93
99
|
* The resolved manifest must live under a `node_modules` directory: without that
|
|
94
100
|
* check, Node's package self-reference resolves this repository's own root
|
|
95
101
|
* package as `@chenacc/agentbridge`, so a development checkout would start a
|
|
@@ -97,13 +103,27 @@ const AUTOSTART_TIMEOUT_MS = 8_000;
|
|
|
97
103
|
*/
|
|
98
104
|
function bridgeServerEntry() {
|
|
99
105
|
if (process.env.AGENTBRIDGE_BRIDGE_ENTRY) return process.env.AGENTBRIDGE_BRIDGE_ENTRY;
|
|
106
|
+
|
|
107
|
+
const candidates = [];
|
|
100
108
|
try {
|
|
101
|
-
|
|
102
|
-
if (!resolved.split(path.sep).includes("node_modules")) return null;
|
|
103
|
-
return path.join(path.dirname(resolved), "src", "server.js");
|
|
109
|
+
candidates.push(createRequire(import.meta.url).resolve(`${BRIDGE_PACKAGE}/package.json`));
|
|
104
110
|
} catch {
|
|
105
|
-
|
|
111
|
+
// Not installed next to the plugin: fall through to the ESM resolver.
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
if (typeof import.meta.resolve === "function") {
|
|
115
|
+
candidates.push(fileURLToPath(import.meta.resolve(`${BRIDGE_PACKAGE}/package.json`)));
|
|
116
|
+
}
|
|
117
|
+
} catch {
|
|
118
|
+
// No ESM resolver either: the dependency is simply absent.
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const manifest of candidates) {
|
|
122
|
+
if (!manifest.split(path.sep).includes("node_modules")) continue;
|
|
123
|
+
const entry = path.join(path.dirname(manifest), "src", "server.js");
|
|
124
|
+
if (existsSync(entry)) return entry;
|
|
106
125
|
}
|
|
126
|
+
return null;
|
|
107
127
|
}
|
|
108
128
|
|
|
109
129
|
async function bridgeIsReady(origin) {
|
|
@@ -135,19 +155,28 @@ function autoStartAllowed() {
|
|
|
135
155
|
* Start the local bridge and wait until it answers.
|
|
136
156
|
*
|
|
137
157
|
* 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.
|
|
139
|
-
*
|
|
158
|
+
* registration instead of trusting that a spawn means a working server. Failures
|
|
159
|
+
* stay quiet for the user (they get the manual steps either way) but each one is
|
|
160
|
+
* reported through `log`, because "nothing happened" is the hardest thing to
|
|
161
|
+
* diagnose from the outside.
|
|
140
162
|
*/
|
|
141
|
-
async function startBridge(origin) {
|
|
163
|
+
async function startBridge(origin, log = () => {}) {
|
|
142
164
|
if (await bridgeIsReady(origin)) return true;
|
|
143
|
-
if (!autoStartAllowed())
|
|
165
|
+
if (!autoStartAllowed()) {
|
|
166
|
+
log("auto-start disabled for this process");
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
144
169
|
const entry = bridgeServerEntry();
|
|
145
|
-
if (!entry || !existsSync(entry))
|
|
170
|
+
if (!entry || !existsSync(entry)) {
|
|
171
|
+
log(`auto-start skipped: ${BRIDGE_PACKAGE} is not resolvable from the plugin install`);
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
146
174
|
|
|
147
175
|
let port;
|
|
148
176
|
try {
|
|
149
177
|
port = new URL(origin).port || "8787";
|
|
150
178
|
} catch {
|
|
179
|
+
log(`auto-start skipped: ${origin} is not a usable origin`);
|
|
151
180
|
return false;
|
|
152
181
|
}
|
|
153
182
|
|
|
@@ -172,7 +201,8 @@ async function startBridge(origin) {
|
|
|
172
201
|
},
|
|
173
202
|
});
|
|
174
203
|
child.unref();
|
|
175
|
-
} catch {
|
|
204
|
+
} catch (error) {
|
|
205
|
+
log(`auto-start failed to spawn ${entry}: ${textFromError(error)}`);
|
|
176
206
|
return false;
|
|
177
207
|
}
|
|
178
208
|
|
|
@@ -181,6 +211,7 @@ async function startBridge(origin) {
|
|
|
181
211
|
await new Promise((resolve) => setTimeout(resolve, 150));
|
|
182
212
|
if (await bridgeIsReady(origin)) return true;
|
|
183
213
|
}
|
|
214
|
+
log(`auto-start timed out: spawned ${entry} but ${origin} never answered`);
|
|
184
215
|
return false;
|
|
185
216
|
}
|
|
186
217
|
|
|
@@ -969,9 +1000,13 @@ function apply(ctx) {
|
|
|
969
1000
|
|
|
970
1001
|
// Bring the bridge up in the background as soon as the plugin loads, so by the
|
|
971
1002
|
// time the user types /phone the local service is already there. Deliberately
|
|
972
|
-
// not awaited: a slow spawn must never delay desktop startup, and
|
|
973
|
-
//
|
|
974
|
-
|
|
1003
|
+
// not awaited: a slow spawn must never delay desktop startup, and the user
|
|
1004
|
+
// still gets the manual steps from /phone when nothing came up. The reason is
|
|
1005
|
+
// logged instead of swallowed, because "nothing happened" is undiagnosable.
|
|
1006
|
+
void startBridge(
|
|
1007
|
+
String(process.env.AGENTBRIDGE_ORIGIN || DEFAULT_ORIGIN).replace(/\/$/, ""),
|
|
1008
|
+
(message) => ctx.logger?.warn?.(`[agentbridge-dsh-phone] ${message}`),
|
|
1009
|
+
).catch((error) => ctx.logger?.warn?.(`[agentbridge-dsh-phone] auto-start crashed: ${textFromError(error)}`));
|
|
975
1010
|
}
|
|
976
1011
|
|
|
977
1012
|
export { apply, inject, name };
|
package/package.json
CHANGED