@agentbean/daemon 0.1.22 → 0.1.24

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.
@@ -76,6 +76,26 @@ function saveCache(payload) {
76
76
  logger.warn({ err: err?.message }, 'failed to save scan cache');
77
77
  }
78
78
  }
79
+ export function createDeviceSocketOptions(input) {
80
+ return {
81
+ auth: {
82
+ token: input.token,
83
+ deviceId: input.deviceId,
84
+ networkId: input.networkId,
85
+ agents: input.agents,
86
+ systemInfo: input.systemInfo,
87
+ daemonVersion: input.systemInfo.daemonVersion,
88
+ protocolVersion: 1,
89
+ capabilities: {
90
+ customAgentDispatch: true,
91
+ },
92
+ },
93
+ reconnection: true,
94
+ reconnectionDelay: 1_000,
95
+ reconnectionDelayMax: 10_000,
96
+ timeout: 20_000,
97
+ };
98
+ }
79
99
  async function scanAll() {
80
100
  const [runtimes, agentos, local] = await Promise.all([
81
101
  scanRuntimes(),
@@ -197,23 +217,13 @@ export function createDeviceDaemon(cfg, agents) {
197
217
  return {
198
218
  async start() {
199
219
  const agentUrl = cfg.server.url.endsWith('/agent') ? cfg.server.url : cfg.server.url + '/agent';
200
- socket = io(agentUrl, {
201
- auth: {
202
- token: cfg.server.token,
203
- deviceId: cfg.deviceId,
204
- networkId: cfg.networkId,
205
- agents: publicAgents,
206
- systemInfo,
207
- daemonVersion: systemInfo.daemonVersion,
208
- protocolVersion: 1,
209
- capabilities: {
210
- customAgentDispatch: true,
211
- },
212
- },
213
- transports: ['websocket'],
214
- reconnection: true,
215
- reconnectionDelay: 1_000,
216
- });
220
+ socket = io(agentUrl, createDeviceSocketOptions({
221
+ token: cfg.server.token,
222
+ deviceId: cfg.deviceId,
223
+ networkId: cfg.networkId,
224
+ agents: publicAgents,
225
+ systemInfo,
226
+ }));
217
227
  socket.on('connect', () => {
218
228
  const reconnecting = !firstConnect;
219
229
  firstConnect = false;
@@ -249,6 +259,15 @@ export function createDeviceDaemon(cfg, agents) {
249
259
  socket.on('connect_error', (err) => {
250
260
  logger.error({ err: err.message }, 'connect_error');
251
261
  });
262
+ socket.io.on('reconnect_attempt', (attempt) => {
263
+ logger.info({ attempt }, 'device daemon reconnect attempt');
264
+ });
265
+ socket.io.on('reconnect', (attempt) => {
266
+ logger.info({ attempt }, 'device daemon reconnected');
267
+ });
268
+ socket.io.on('reconnect_error', (err) => {
269
+ logger.warn({ err: errorMessage(err) }, 'device daemon reconnect failed');
270
+ });
252
271
  socket.on('dispatch', (req) => {
253
272
  let agent = agents.get(req.agentId);
254
273
  if (!agent && req.customAgent) {
package/dist/index.js CHANGED
@@ -141,6 +141,7 @@ Options:
141
141
  }
142
142
  let serverUrl = values['server-url'] ?? process.env.AGENT_BEAN_SERVER_URL;
143
143
  let token = values['token'] ?? process.env.AGENT_BEAN_AGENT_TOKEN;
144
+ let savedAuth = null;
144
145
  let networkId = values['network-id'] ?? 'default';
145
146
  if (values.invite) {
146
147
  if (!serverUrl) {
@@ -153,11 +154,10 @@ Options:
153
154
  networkId = auth.networkId ?? networkId;
154
155
  }
155
156
  else if (!token) {
156
- const saved = loadAuth();
157
- if (saved) {
158
- serverUrl = serverUrl ?? saved.serverUrl;
159
- token = saved.token;
160
- networkId = saved.networkId ?? networkId;
157
+ savedAuth = loadAuth();
158
+ if (savedAuth) {
159
+ serverUrl = serverUrl ?? savedAuth.serverUrl;
160
+ token = savedAuth.token;
161
161
  }
162
162
  }
163
163
  if (!serverUrl || !token) {
@@ -165,6 +165,18 @@ Options:
165
165
  console.error('Usage: agentbean-daemon --server-url <url> --token <token>');
166
166
  process.exit(1);
167
167
  }
168
+ const tokenNetworkId = networkIdFromToken(token);
169
+ if (values['network-id'] && tokenNetworkId && values['network-id'] !== tokenNetworkId) {
170
+ console.error('Error: --network-id does not match the provided token.');
171
+ console.error('Use the team ID embedded in the token, or omit --network-id and let the daemon detect it.');
172
+ process.exit(1);
173
+ }
174
+ networkId = resolveCliNetworkId({
175
+ explicitNetworkId: values['network-id'],
176
+ token,
177
+ savedNetworkId: savedAuth?.networkId,
178
+ fallbackNetworkId: networkId,
179
+ });
168
180
  const deviceId = values['device-id'] ?? await getDeviceId();
169
181
  logger.info({ serverUrl, deviceId, networkId }, 'CLI mode: auto-discovering agents');
170
182
  const agents = await discoverAgents(deviceId);
@@ -187,6 +199,20 @@ function normalizeAgentUrl(serverUrl) {
187
199
  const base = normalizeBaseUrl(serverUrl);
188
200
  return `${base}/agent`;
189
201
  }
202
+ export function networkIdFromToken(token) {
203
+ const parts = String(token ?? '').split(':');
204
+ if (parts.length !== 3)
205
+ return undefined;
206
+ const networkId = parts[1]?.trim();
207
+ return networkId || undefined;
208
+ }
209
+ export function resolveCliNetworkId(input) {
210
+ return input.explicitNetworkId
211
+ ?? networkIdFromToken(input.token)
212
+ ?? input.savedNetworkId
213
+ ?? input.fallbackNetworkId
214
+ ?? 'default';
215
+ }
190
216
  export const INVITE_CONNECTION_TIMEOUT_MS = 20_000;
191
217
  export function createInviteSocketOptions() {
192
218
  return {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@agentbean/daemon",
3
3
  "private": false,
4
- "version": "0.1.22",
4
+ "version": "0.1.24",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "bin": {