@rehpic/vcli 0.1.0-beta.57.1 → 0.1.0-beta.59.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
@@ -1468,6 +1468,7 @@ function getGitInfo(cwd) {
1468
1468
  // src/bridge-service.ts
1469
1469
  var CONFIG_DIR = process.env.VECTOR_HOME?.trim() || join2(homedir3(), ".vector");
1470
1470
  var BRIDGE_CONFIG_FILE = join2(CONFIG_DIR, "bridge.json");
1471
+ var DEVICE_KEY_FILE = join2(CONFIG_DIR, "device-key");
1471
1472
  var PID_FILE = join2(CONFIG_DIR, "bridge.pid");
1472
1473
  var LIVE_ACTIVITIES_CACHE = join2(CONFIG_DIR, "live-activities.json");
1473
1474
  var LAUNCHAGENT_DIR = join2(homedir3(), "Library", "LaunchAgents");
@@ -1492,6 +1493,7 @@ function loadBridgeConfig() {
1492
1493
  function saveBridgeConfig(config) {
1493
1494
  if (!existsSync2(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
1494
1495
  writeFileSync(BRIDGE_CONFIG_FILE, JSON.stringify(config, null, 2));
1496
+ persistDeviceKey(config.deviceKey);
1495
1497
  }
1496
1498
  function writeLiveActivitiesCache(activities) {
1497
1499
  if (!existsSync2(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
@@ -1829,8 +1831,8 @@ var BridgeService = class {
1829
1831
  }
1830
1832
  }
1831
1833
  };
1832
- async function setupBridgeDevice(client, convexUrl, userId) {
1833
- const deviceKey = `${hostname()}-${randomUUID().slice(0, 8)}`;
1834
+ async function setupBridgeDevice(client, convexUrl) {
1835
+ const deviceKey = getStableDeviceKey();
1834
1836
  const displayName = `${process.env.USER ?? "user"}'s ${platform() === "darwin" ? "Mac" : "machine"}`;
1835
1837
  const result = await client.mutation(
1836
1838
  api.agentBridge.mutations.registerBridgeDevice,
@@ -1848,7 +1850,7 @@ async function setupBridgeDevice(client, convexUrl, userId) {
1848
1850
  deviceId: result.deviceId,
1849
1851
  deviceKey,
1850
1852
  deviceSecret: result.deviceSecret,
1851
- userId,
1853
+ userId: result.userId,
1852
1854
  displayName,
1853
1855
  convexUrl,
1854
1856
  registeredAt: (/* @__PURE__ */ new Date()).toISOString()
@@ -1856,6 +1858,28 @@ async function setupBridgeDevice(client, convexUrl, userId) {
1856
1858
  saveBridgeConfig(config);
1857
1859
  return config;
1858
1860
  }
1861
+ function getStableDeviceKey() {
1862
+ const existingConfig = loadBridgeConfig();
1863
+ const existingKey = existingConfig?.deviceKey?.trim();
1864
+ if (existingKey) {
1865
+ persistDeviceKey(existingKey);
1866
+ return existingKey;
1867
+ }
1868
+ if (existsSync2(DEVICE_KEY_FILE)) {
1869
+ const savedKey = readFileSync2(DEVICE_KEY_FILE, "utf-8").trim();
1870
+ if (savedKey) {
1871
+ return savedKey;
1872
+ }
1873
+ }
1874
+ const generatedKey = `${hostname()}-${randomUUID().slice(0, 8)}`;
1875
+ persistDeviceKey(generatedKey);
1876
+ return generatedKey;
1877
+ }
1878
+ function persistDeviceKey(deviceKey) {
1879
+ if (!existsSync2(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
1880
+ writeFileSync(DEVICE_KEY_FILE, `${deviceKey}
1881
+ `);
1882
+ }
1859
1883
  function buildLaunchPrompt(issueKey, issueTitle, workspacePath) {
1860
1884
  return [
1861
1885
  `You are working on Vector issue ${issueKey}: ${issueTitle}.`,
@@ -2511,6 +2535,39 @@ async function getClient(command) {
2511
2535
  );
2512
2536
  return { client, runtime, session };
2513
2537
  }
2538
+ async function ensureBridgeConfig(command) {
2539
+ let config = loadBridgeConfig();
2540
+ try {
2541
+ const runtime = await getRuntime(command);
2542
+ const session = requireSession(runtime);
2543
+ const client = await createConvexClient(
2544
+ session,
2545
+ runtime.appUrl,
2546
+ runtime.convexUrl
2547
+ );
2548
+ const user = await runQuery(client, api.users.currentUser);
2549
+ if (!user) {
2550
+ throw new Error("Not logged in. Run `vcli auth login` first.");
2551
+ }
2552
+ const backendDevice = config ? await runQuery(client, api.agentBridge.queries.getDevice, {
2553
+ deviceId: config.deviceId
2554
+ }) : null;
2555
+ const needsRegistration = !config || config.userId !== user._id || config.convexUrl !== runtime.convexUrl || !backendDevice;
2556
+ if (needsRegistration) {
2557
+ config = await setupBridgeDevice(client, runtime.convexUrl);
2558
+ }
2559
+ if (!config) {
2560
+ throw new Error("Bridge device is not configured.");
2561
+ }
2562
+ saveBridgeConfig(config);
2563
+ return config;
2564
+ } catch (error) {
2565
+ if (config) {
2566
+ return config;
2567
+ }
2568
+ throw error;
2569
+ }
2570
+ }
2514
2571
  async function resolveMemberId(client, orgSlug, ref) {
2515
2572
  const members = await runQuery(
2516
2573
  client,
@@ -4246,24 +4303,9 @@ serviceCommand.command("start").description("Start the bridge service via Launch
4246
4303
  }
4247
4304
  const { spinner } = await import("@clack/prompts");
4248
4305
  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
- }
4306
+ s.start("Ensuring device registration...");
4307
+ const config = await ensureBridgeConfig(command);
4308
+ s.stop(`Device ready: ${config.displayName}`);
4267
4309
  if (osPlatform() === "darwin") {
4268
4310
  s.start("Starting bridge service...");
4269
4311
  const vcliPath = process.argv[1] ?? "vcli";
@@ -4279,19 +4321,7 @@ serviceCommand.command("start").description("Start the bridge service via Launch
4279
4321
  }
4280
4322
  });
4281
4323
  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
- }
4324
+ const config = await ensureBridgeConfig(command);
4295
4325
  if (osPlatform() === "darwin") {
4296
4326
  await launchMenuBar();
4297
4327
  }
@@ -4404,24 +4434,9 @@ serviceCommand.command("install").description("Install the bridge as a system se
4404
4434
  }
4405
4435
  const { spinner } = await import("@clack/prompts");
4406
4436
  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
- }
4437
+ s.start("Ensuring device registration...");
4438
+ const config = await ensureBridgeConfig(command);
4439
+ s.stop(`Device ready: ${config.displayName}`);
4425
4440
  s.start("Installing LaunchAgent...");
4426
4441
  const vcliPath = process.argv[1] ?? "vcli";
4427
4442
  installLaunchAgent(vcliPath);
@@ -4469,18 +4484,9 @@ serviceCommand.command("disable").description("Disable bridge from starting at l
4469
4484
  });
4470
4485
  var bridgeCommand = program.command("bridge").description("Start/stop the local agent bridge");
4471
4486
  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);
4487
+ const existingConfig = loadBridgeConfig();
4488
+ const config = await ensureBridgeConfig(command);
4489
+ if (!existingConfig || existingConfig.deviceId !== config.deviceId || existingConfig.userId !== config.userId) {
4484
4490
  console.log(
4485
4491
  `Device registered: ${config.displayName} (${config.deviceId})`
4486
4492
  );