@rehpic/vcli 0.1.0-beta.56.1 → 0.1.0-beta.58.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.
package/dist/index.js CHANGED
@@ -1829,7 +1829,7 @@ var BridgeService = class {
1829
1829
  }
1830
1830
  }
1831
1831
  };
1832
- async function setupBridgeDevice(client, convexUrl, userId) {
1832
+ async function setupBridgeDevice(client, convexUrl) {
1833
1833
  const deviceKey = `${hostname()}-${randomUUID().slice(0, 8)}`;
1834
1834
  const displayName = `${process.env.USER ?? "user"}'s ${platform() === "darwin" ? "Mac" : "machine"}`;
1835
1835
  const result = await client.mutation(
@@ -1848,7 +1848,7 @@ async function setupBridgeDevice(client, convexUrl, userId) {
1848
1848
  deviceId: result.deviceId,
1849
1849
  deviceKey,
1850
1850
  deviceSecret: result.deviceSecret,
1851
- userId,
1851
+ userId: result.userId,
1852
1852
  displayName,
1853
1853
  convexUrl,
1854
1854
  registeredAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -2511,6 +2511,39 @@ async function getClient(command) {
2511
2511
  );
2512
2512
  return { client, runtime, session };
2513
2513
  }
2514
+ async function ensureBridgeConfig(command) {
2515
+ let config = loadBridgeConfig();
2516
+ try {
2517
+ const runtime = await getRuntime(command);
2518
+ const session = requireSession(runtime);
2519
+ const authUserId = decodeSessionClaims(session).userId;
2520
+ const client = await createConvexClient(
2521
+ session,
2522
+ runtime.appUrl,
2523
+ runtime.convexUrl
2524
+ );
2525
+ const user = await runQuery(client, api.users.currentUser);
2526
+ if (!user) {
2527
+ throw new Error("Not logged in. Run `vcli auth login` first.");
2528
+ }
2529
+ const backendDevice = config ? await runQuery(client, api.agentBridge.queries.getDevice, {
2530
+ deviceId: config.deviceId
2531
+ }) : null;
2532
+ const needsRegistration = !config || (authUserId ? config.userId !== authUserId : false) || config.convexUrl !== runtime.convexUrl || !backendDevice;
2533
+ if (needsRegistration) {
2534
+ config = await setupBridgeDevice(client, runtime.convexUrl);
2535
+ }
2536
+ if (!config) {
2537
+ throw new Error("Bridge device is not configured.");
2538
+ }
2539
+ return config;
2540
+ } catch (error) {
2541
+ if (config) {
2542
+ return config;
2543
+ }
2544
+ throw error;
2545
+ }
2546
+ }
2514
2547
  async function resolveMemberId(client, orgSlug, ref) {
2515
2548
  const members = await runQuery(
2516
2549
  client,
@@ -4246,24 +4279,9 @@ serviceCommand.command("start").description("Start the bridge service via Launch
4246
4279
  }
4247
4280
  const { spinner } = await import("@clack/prompts");
4248
4281
  const s = spinner();
4249
- let config = loadBridgeConfig();
4250
- if (!config) {
4251
- s.start("Registering device...");
4252
- const runtime = await getRuntime(command);
4253
- const session = requireSession(runtime);
4254
- const client = await createConvexClient(
4255
- session,
4256
- runtime.appUrl,
4257
- runtime.convexUrl
4258
- );
4259
- const user = await runQuery(client, api.users.currentUser);
4260
- if (!user) {
4261
- s.stop("Failed");
4262
- throw new Error("Not logged in. Run `vcli auth login` first.");
4263
- }
4264
- config = await setupBridgeDevice(client, runtime.convexUrl, user._id);
4265
- s.stop(`Device registered: ${config.displayName}`);
4266
- }
4282
+ s.start("Ensuring device registration...");
4283
+ const config = await ensureBridgeConfig(command);
4284
+ s.stop(`Device ready: ${config.displayName}`);
4267
4285
  if (osPlatform() === "darwin") {
4268
4286
  s.start("Starting bridge service...");
4269
4287
  const vcliPath = process.argv[1] ?? "vcli";
@@ -4279,19 +4297,7 @@ serviceCommand.command("start").description("Start the bridge service via Launch
4279
4297
  }
4280
4298
  });
4281
4299
  serviceCommand.command("run").description("Run the bridge service in the foreground (used by LaunchAgent)").action(async (_options, command) => {
4282
- let config = loadBridgeConfig();
4283
- if (!config) {
4284
- const runtime = await getRuntime(command);
4285
- const session = requireSession(runtime);
4286
- const client = await createConvexClient(
4287
- session,
4288
- runtime.appUrl,
4289
- runtime.convexUrl
4290
- );
4291
- const user = await runQuery(client, api.users.currentUser);
4292
- if (!user) throw new Error("Not logged in. Run `vcli auth login` first.");
4293
- config = await setupBridgeDevice(client, runtime.convexUrl, user._id);
4294
- }
4300
+ const config = await ensureBridgeConfig(command);
4295
4301
  if (osPlatform() === "darwin") {
4296
4302
  await launchMenuBar();
4297
4303
  }
@@ -4404,24 +4410,9 @@ serviceCommand.command("install").description("Install the bridge as a system se
4404
4410
  }
4405
4411
  const { spinner } = await import("@clack/prompts");
4406
4412
  const s = spinner();
4407
- let config = loadBridgeConfig();
4408
- if (!config) {
4409
- s.start("Registering device...");
4410
- const runtime = await getRuntime(command);
4411
- const session = requireSession(runtime);
4412
- const client = await createConvexClient(
4413
- session,
4414
- runtime.appUrl,
4415
- runtime.convexUrl
4416
- );
4417
- const user = await runQuery(client, api.users.currentUser);
4418
- if (!user) {
4419
- s.stop("Failed");
4420
- throw new Error("Not logged in. Run `vcli auth login` first.");
4421
- }
4422
- config = await setupBridgeDevice(client, runtime.convexUrl, user._id);
4423
- s.stop(`Device registered: ${config.displayName}`);
4424
- }
4413
+ s.start("Ensuring device registration...");
4414
+ const config = await ensureBridgeConfig(command);
4415
+ s.stop(`Device ready: ${config.displayName}`);
4425
4416
  s.start("Installing LaunchAgent...");
4426
4417
  const vcliPath = process.argv[1] ?? "vcli";
4427
4418
  installLaunchAgent(vcliPath);
@@ -4469,18 +4460,9 @@ serviceCommand.command("disable").description("Disable bridge from starting at l
4469
4460
  });
4470
4461
  var bridgeCommand = program.command("bridge").description("Start/stop the local agent bridge");
4471
4462
  bridgeCommand.command("start").description("Register device, install service, and start the bridge").action(async (_options, command) => {
4472
- let config = loadBridgeConfig();
4473
- if (!config) {
4474
- const runtime = await getRuntime(command);
4475
- const session = requireSession(runtime);
4476
- const client = await createConvexClient(
4477
- session,
4478
- runtime.appUrl,
4479
- runtime.convexUrl
4480
- );
4481
- const user = await runQuery(client, api.users.currentUser);
4482
- if (!user) throw new Error("Not logged in. Run `vcli auth login` first.");
4483
- config = await setupBridgeDevice(client, runtime.convexUrl, user._id);
4463
+ const existingConfig = loadBridgeConfig();
4464
+ const config = await ensureBridgeConfig(command);
4465
+ if (!existingConfig || existingConfig.deviceId !== config.deviceId || existingConfig.userId !== config.userId) {
4484
4466
  console.log(
4485
4467
  `Device registered: ${config.displayName} (${config.deviceId})`
4486
4468
  );