@beyondwork/wb-cli 0.1.20 → 0.1.22

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.1.21
4
+
5
+ - Update types
6
+
3
7
  ## v0.1.20
4
8
 
5
9
  - Fix "Get workblock from workspace" hanging by adding request timeouts and better progress/log/error reporting.
package/out/extension.js CHANGED
@@ -52,16 +52,18 @@ let diagnosticCollection;
52
52
  let outputChannel;
53
53
  let streamsByDir;
54
54
  let hostsByStream;
55
+ let branchKeysByStream;
56
+ let streamsByBranchKey;
55
57
  let syncStreams;
56
- let streamsByHost;
57
58
  let latestWorkblocks;
59
+ let currentUsersByHost;
58
60
  let outputLogger;
59
- async function onWorkblockChange(streamId, workblock, treeDataProvider, host) {
60
- console.log("onWorkblockChange", streamId, workblock?.state);
61
+ async function onWorkblockChange(key, workblock, treeDataProvider) {
62
+ console.log("onWorkblockChange", key, workblock?.state);
61
63
  if (!workblock) {
62
64
  return;
63
65
  }
64
- const dir = streamsByDir[streamId];
66
+ const dir = streamsByDir[key];
65
67
  if (!dir) {
66
68
  return;
67
69
  }
@@ -71,7 +73,98 @@ async function onWorkblockChange(streamId, workblock, treeDataProvider, host) {
71
73
  }
72
74
  const uWb = (0, lib_1.trimWorkblock)(workblock);
73
75
  const uLocalWb = (0, lib_1.trimWorkblock)(localWb);
74
- treeDataProvider.setSyncStatus(host, streamId, JSON.stringify(uWb) !== JSON.stringify(uLocalWb));
76
+ treeDataProvider.setSyncStatus(key, JSON.stringify(uWb) !== JSON.stringify(uLocalWb));
77
+ }
78
+ function parseBranchKey(key) {
79
+ const [host, workblockId, branch] = key.split("|").map(decodeURIComponent);
80
+ if (!host || !workblockId || !branch) {
81
+ throw new Error(`Invalid branch binding key: ${key}`);
82
+ }
83
+ return { host, workblockId, branch };
84
+ }
85
+ async function refreshWorkblocksForHost(host, client, treeDataProvider, currentUserId) {
86
+ const workblocks = await client.listWorkblockDefinitions();
87
+ const branchesByWorkblock = {};
88
+ for (const workblock of workblocks) {
89
+ branchesByWorkblock[workblock.id] = workblock.branches ?? [];
90
+ }
91
+ const involvedWorkblocks = workblocks.filter((workblock) => {
92
+ if (workblock.user_id === currentUserId || workblock.branch_user === currentUserId) {
93
+ return true;
94
+ }
95
+ return userBranchesFor(branchesByWorkblock[workblock.id] ?? [], currentUserId).length > 0;
96
+ });
97
+ treeDataProvider.setWorkblocksForHost(host, involvedWorkblocks);
98
+ for (const workblock of involvedWorkblocks) {
99
+ const ownsWorkblock = workblock.user_id === currentUserId || workblock.branch_user === currentUserId;
100
+ const branches = filterBranchesForUser(branchesByWorkblock[workblock.id] ?? [], currentUserId, ownsWorkblock);
101
+ treeDataProvider.setBranchesForWorkblock(host, workblock.id, branches, false, false);
102
+ }
103
+ treeDataProvider.refresh();
104
+ }
105
+ function filterBranchesForUser(branches, currentUserId, ownsWorkblock) {
106
+ const visibleBranches = (0, tree_data_provider_1.filterVisibleBranches)(branches);
107
+ const userBranches = neuiUserBranchesFor(branches, currentUserId);
108
+ if (userBranches.length > 0) {
109
+ return userBranches;
110
+ }
111
+ return ownsWorkblock ? visibleBranches.filter((branch) => branch.name === "main") : [];
112
+ }
113
+ function userBranchesFor(branches, currentUserId) {
114
+ return branches
115
+ .filter((branch) => branch.name.startsWith("features/") || branch.name.startsWith("users/"))
116
+ .filter((branch) => !branch.name.includes("@thread/"))
117
+ .filter((branch) => branchOwnerId(branch) === currentUserId);
118
+ }
119
+ function neuiUserBranchesFor(branches, currentUserId) {
120
+ const featureBranches = new Set(branches
121
+ .filter((branch) => branch.name.startsWith("features/"))
122
+ .filter((branch) => !branch.name.includes("@thread/"))
123
+ .filter((branch) => !branchOwnerId(branch))
124
+ .map((branch) => getFeatureRef(branch.name)));
125
+ return branches
126
+ .filter((branch) => branch.name.startsWith("features/"))
127
+ .filter((branch) => !branch.name.includes("@thread/"))
128
+ .filter((branch) => branchOwnerId(branch) === currentUserId)
129
+ .filter((branch) => featureBranches.has(getFeatureRef(branch.name)));
130
+ }
131
+ function getFeatureRef(branchName) {
132
+ const withoutThread = branchName.replace(/@thread\/[0-9a-f-]+$/, "");
133
+ const userBranchIdx = withoutThread.lastIndexOf("@users/");
134
+ if (userBranchIdx !== -1) {
135
+ return withoutThread.slice(0, userBranchIdx);
136
+ }
137
+ return withoutThread;
138
+ }
139
+ function branchOwnerId(branch) {
140
+ const withoutThread = branch.name.replace(/@thread\/[0-9a-f-]+$/, "");
141
+ const userBranchIdx = withoutThread.lastIndexOf("@users/");
142
+ if (userBranchIdx !== -1) {
143
+ return withoutThread.slice(userBranchIdx + "@users/".length);
144
+ }
145
+ if (withoutThread.startsWith("users/")) {
146
+ return withoutThread.slice("users/".length);
147
+ }
148
+ if (branch.owner_id && branch.owner_id !== "00000000-0000-0000-0000-000000000000") {
149
+ return branch.owner_id;
150
+ }
151
+ return undefined;
152
+ }
153
+ async function resolveBranchStream(host, workblockId, branch) {
154
+ const client = clientsByHost[host];
155
+ if (!client) {
156
+ throw new Error(`Not connected to ${host}`);
157
+ }
158
+ const stream = await client.getEditStream(workblockId);
159
+ await client.switchStreamBranch(stream.id, branch);
160
+ const key = (0, tree_data_provider_1.branchKey)(host, workblockId, branch);
161
+ hostsByStream[stream.id] = host;
162
+ branchKeysByStream[stream.id] = key;
163
+ streamsByBranchKey[key] = stream.id;
164
+ return stream;
165
+ }
166
+ async function resolveBranchItemStream(item) {
167
+ return resolveBranchStream(item.connection.host, item.workblock.id, item.branch.name);
75
168
  }
76
169
  async function connectToHost(host, context, treeDataProvider) {
77
170
  if (clientsByHost[host]) {
@@ -83,63 +176,74 @@ async function connectToHost(host, context, treeDataProvider) {
83
176
  throw new Error(`Failed to retrieve token for ${host}.`);
84
177
  }
85
178
  const newClient = new client_1.BeyondWorkClient();
179
+ newClient.addCurrentUserHandler((user) => {
180
+ currentUsersByHost[host] = user;
181
+ refreshWorkblocksForHost(host, newClient, treeDataProvider, user.id)
182
+ .then(() => autoBindBranches(host, context, treeDataProvider))
183
+ .catch((e) => {
184
+ outputChannel.appendLine(`${e}`);
185
+ vscode.window.showErrorMessage(`Beyond Work: ${e}`);
186
+ });
187
+ });
86
188
  newClient.addStreamHandler(async (message) => {
87
- treeDataProvider.setStreamsForHost(host, message);
88
189
  treeDataProvider.updateConnectionStatus(host, "connected");
89
- streamsByHost[host] = message;
90
- hostsByStream = {};
91
190
  for (const stream of message) {
92
191
  hostsByStream[stream.id] = host;
93
192
  }
94
193
  try {
95
- // Attempt automatic binding of streams to directories based on manifest.yaml id matches
96
- await autoBindStreams(message, context, treeDataProvider);
194
+ const currentUser = currentUsersByHost[host];
195
+ if (!currentUser) {
196
+ return;
197
+ }
198
+ await refreshWorkblocksForHost(host, newClient, treeDataProvider, currentUser.id);
199
+ await autoBindBranches(host, context, treeDataProvider);
97
200
  }
98
201
  catch (e) {
99
202
  outputChannel.appendLine(`${e}`);
100
203
  vscode.window.showErrorMessage(`Beyond Work: ${e}`);
101
204
  }
102
- Promise.all(message.map(async (stream) => {
103
- return await newClient.getWorkblock(stream.id);
104
- }))
105
- .then((workblocks) => {
106
- workblocks.map((wb, idx) => {
107
- onWorkblockChange(message[idx].id, wb, treeDataProvider, host);
108
- if (wb) {
109
- latestWorkblocks[host + message[idx].id] = wb;
110
- }
111
- });
112
- console.log("got workblocks", workblocks);
113
- })
114
- .catch((e) => {
115
- const msg = e?.message ?? String(e);
116
- outputChannel.appendLine(`[connect] Failed to prefetch workblocks: ${msg}`);
117
- });
118
205
  });
119
206
  newClient.addEventHandler(async (message) => {
120
207
  for (const msg of message) {
121
208
  if (msg.type == "workblock") {
122
- if (syncStreams[msg.stream_id] !== undefined &&
123
- msg.work_block?.work_block) {
124
- await (0, lib_1.storeWorkblockFiles)(syncStreams[msg.stream_id], msg.work_block?.work_block, newClient);
209
+ const streamId = msg.stream_id;
210
+ let key = streamId ? branchKeysByStream[streamId] : undefined;
211
+ if (!key && msg.work_block?.workblock_id && msg.work_block?.branch) {
212
+ key = (0, tree_data_provider_1.branchKey)(host, msg.work_block.workblock_id, msg.work_block.branch);
213
+ }
214
+ if (!key) {
215
+ continue;
216
+ }
217
+ let workblock = msg.work_block?.work_block;
218
+ if (!workblock &&
219
+ streamId &&
220
+ msg.work_block?.workblock_id &&
221
+ msg.work_block?.commit_ref) {
222
+ try {
223
+ workblock = await newClient.getWorkblock(streamId);
224
+ }
225
+ catch (e) {
226
+ const err = e?.message ?? String(e);
227
+ outputChannel.appendLine(`[events] Failed to resolve workblock for ${streamId}: ${err}`);
228
+ }
229
+ }
230
+ if (syncStreams[key] !== undefined && workblock) {
231
+ await (0, lib_1.storeWorkblockFiles)(syncStreams[key], workblock, newClient);
232
+ }
233
+ await onWorkblockChange(key, workblock, treeDataProvider);
234
+ if (workblock) {
235
+ latestWorkblocks[key] = workblock;
125
236
  }
126
- await onWorkblockChange(msg.stream_id, msg.work_block?.work_block, treeDataProvider, host);
127
- latestWorkblocks[host + msg.stream_id] = msg.work_block?.work_block;
128
237
  }
129
238
  if (msg.type == "stream") {
130
- if (msg.stream?.stream?.editing) {
131
- hostsByStream[msg.stream?.stream?.id] = host;
132
- streamsByHost[host].push({
133
- id: msg.stream?.stream?.id,
134
- editing: msg.stream?.stream?.editing,
135
- title: msg.stream?.stream?.title,
136
- });
239
+ const stream = msg.stream?.stream;
240
+ if (stream?.id) {
241
+ hostsByStream[stream.id] = host;
137
242
  }
138
- else {
139
- streamsByHost[host] = streamsByHost[host].filter((s) => s.id !== msg.stream?.stream?.id);
243
+ const currentUser = currentUsersByHost[host];
244
+ if (currentUser) {
245
+ await refreshWorkblocksForHost(host, newClient, treeDataProvider, currentUser.id);
140
246
  }
141
- streamsByHost[host] = streamsByHost[host].sort((a, b) => a.title.localeCompare(b.title));
142
- treeDataProvider.setStreamsForHost(host, streamsByHost[host]);
143
247
  }
144
248
  }
145
249
  });
@@ -163,8 +267,10 @@ function activate(context) {
163
267
  streamsByDir = context.globalState.get("bw_streams_dir") ?? {};
164
268
  syncStreams = context.globalState.get("bw_sync_streams") ?? {};
165
269
  hostsByStream = {};
166
- streamsByHost = {};
270
+ branchKeysByStream = {};
271
+ streamsByBranchKey = {};
167
272
  latestWorkblocks = {};
273
+ currentUsersByHost = {};
168
274
  const treeDataProvider = new tree_data_provider_1.BeyondWorkTreeDataProvider(context, streamsByDir, syncStreams);
169
275
  const treeView = vscode.window.createTreeView("beyondworkConnections", {
170
276
  treeDataProvider: treeDataProvider,
@@ -178,19 +284,26 @@ function activate(context) {
178
284
  if (ignorePatterns.some((pattern) => uri.path.includes(pattern))) {
179
285
  return;
180
286
  }
181
- for (const [streamId, dir] of Object.entries(streamsByDir)) {
287
+ for (const [key, dir] of Object.entries(streamsByDir)) {
288
+ if (!key.includes("|")) {
289
+ continue;
290
+ }
182
291
  if (uri.fsPath.startsWith(dir)) {
183
- const host = hostsByStream[streamId];
184
- if (host) {
185
- if (syncStreams[streamId] !== undefined && sync) {
186
- await setWorkblock(dir, host, streamId).then(() => {
187
- treeDataProvider.setSyncStatus(host, streamId, false);
292
+ try {
293
+ const { host, workblockId, branch } = parseBranchKey(key);
294
+ if (syncStreams[key] !== undefined && sync) {
295
+ const stream = await resolveBranchStream(host, workblockId, branch);
296
+ await setWorkblock(dir, host, stream.id).then(() => {
297
+ treeDataProvider.setSyncStatus(key, false);
188
298
  });
189
299
  }
190
300
  else {
191
- await onWorkblockChange(streamId, latestWorkblocks[host + streamId], treeDataProvider, host);
301
+ await onWorkblockChange(key, latestWorkblocks[key], treeDataProvider);
192
302
  }
193
303
  }
304
+ catch (e) {
305
+ outputChannel.appendLine(`[sync] ${e?.message ?? e}`);
306
+ }
194
307
  }
195
308
  }
196
309
  };
@@ -298,29 +411,22 @@ function activate(context) {
298
411
  try {
299
412
  const stream = await client.storeWorkblock(wb);
300
413
  outputChannel.appendLine(`Created stream ${stream.id} (${stream.title})`);
301
- // Ensure host mapping structures exist
302
- if (!streamsByHost[host]) {
303
- streamsByHost[host] = [];
304
- }
305
414
  hostsByStream[stream.id] = host;
306
- // Insert new stream into list if not present
307
- if (!streamsByHost[host].some((s) => s.id === stream.id)) {
308
- streamsByHost[host].push({
309
- id: stream.id,
310
- editing: stream.editing ?? true,
311
- title: stream.title ?? wb.name,
312
- });
313
- streamsByHost[host] = streamsByHost[host].sort((a, b) => a.title.localeCompare(b.title));
314
- }
415
+ const branch = stream.branch ?? "main";
416
+ const key = (0, tree_data_provider_1.branchKey)(host, wb.id, branch);
417
+ branchKeysByStream[stream.id] = key;
418
+ streamsByBranchKey[key] = stream.id;
315
419
  // Bind directory to stream
316
- streamsByDir[stream.id] = baseDir[0].fsPath;
420
+ streamsByDir[key] = baseDir[0].fsPath;
317
421
  context.globalState.update("bw_streams_dir", streamsByDir);
318
422
  // Store latest workblock for change tracking
319
- latestWorkblocks[host + stream.id] = wb;
423
+ latestWorkblocks[key] = wb;
320
424
  // Update tree view
321
- treeDataProvider.setStreamsForHost(host, streamsByHost[host]);
322
- treeDataProvider.refresh();
323
- vscode.window.showInformationMessage(`Created and bound new stream '${stream.title}'`);
425
+ const currentUser = currentUsersByHost[host];
426
+ if (currentUser) {
427
+ await refreshWorkblocksForHost(host, client, treeDataProvider, currentUser.id);
428
+ }
429
+ vscode.window.showInformationMessage(`Created and bound '${wb.name}' on ${branch}`);
324
430
  }
325
431
  catch (e) {
326
432
  const msg = e?.message ?? String(e);
@@ -328,50 +434,47 @@ function activate(context) {
328
434
  vscode.window.showErrorMessage(`Failed to create stream: ${msg}`);
329
435
  }
330
436
  });
331
- const bindStream = vscode.commands.registerCommand("beyondwork.beyondwork.bindStream", async (stream, dir) => {
332
- outputChannel.appendLine(`Binding stream ${stream.id} ${dir}`);
437
+ const bindStream = vscode.commands.registerCommand("beyondwork.beyondwork.bindStream", async (item) => {
438
+ outputChannel.appendLine(`Binding ${item.workblock.name} ${item.branch.name} ${item.dir}`);
333
439
  const opts = {
334
440
  canSelectFolders: true,
335
441
  canSelectFiles: false,
336
442
  canSelectMany: false,
337
443
  openLabel: "Select folder for the workblock",
338
444
  };
339
- if (dir) {
340
- opts.defaultUri = vscode.Uri.file(dir);
445
+ if (item.dir) {
446
+ opts.defaultUri = vscode.Uri.file(item.dir);
341
447
  }
342
448
  const baseDir = await vscode.window.showOpenDialog(opts);
343
449
  if (!baseDir?.length) {
344
450
  return;
345
451
  }
346
- streamsByDir[stream.id] = baseDir[0].fsPath;
452
+ streamsByDir[item.key] = baseDir[0].fsPath;
347
453
  context.globalState.update("bw_streams_dir", streamsByDir);
348
454
  treeDataProvider.refresh();
349
- // Immediately fetch remote workblock into selected directory (enhanced behavior)
350
455
  try {
351
- const host = hostsByStream[stream.id];
352
- if (host) {
353
- const client = clientsByHost[host];
354
- const wb = await client.getWorkblock(stream.id);
355
- if (wb) {
356
- await (0, lib_1.storeWorkblockFiles)(baseDir[0].fsPath, wb, client);
357
- latestWorkblocks[host + stream.id] = wb;
358
- await onWorkblockChange(stream.id, wb, treeDataProvider, host);
359
- outputChannel.appendLine(`Fetched remote workblock for ${stream.id} into ${baseDir[0].fsPath}`);
360
- }
361
- else {
362
- outputChannel.appendLine(`No remote workblock present for ${stream.id}`);
363
- }
456
+ const stream = await resolveBranchItemStream(item);
457
+ const client = clientsByHost[item.connection.host];
458
+ const wb = await client.getWorkblock(stream.id);
459
+ if (wb) {
460
+ await (0, lib_1.storeWorkblockFiles)(baseDir[0].fsPath, wb, client);
461
+ latestWorkblocks[item.key] = wb;
462
+ await onWorkblockChange(item.key, wb, treeDataProvider);
463
+ outputChannel.appendLine(`Fetched ${item.workblock.name} (${item.branch.name}) into ${baseDir[0].fsPath}`);
464
+ }
465
+ else {
466
+ outputChannel.appendLine(`No remote workblock present for ${item.workblock.name} (${item.branch.name})`);
364
467
  }
365
468
  }
366
469
  catch (e) {
367
- outputChannel.appendLine(`Failed to fetch remote workblock for ${stream.id}: ${e?.message ?? e}`);
470
+ outputChannel.appendLine(`Failed to fetch remote workblock for ${item.workblock.name}: ${e?.message ?? e}`);
368
471
  }
369
472
  });
370
473
  const getWB = vscode.commands.registerCommand("beyondwork.beyondwork.get", async (item) => {
371
474
  outputChannel.show(true);
372
- outputChannel.appendLine(`Getting workblock streamId=${item.stream.id} host=${item.connection.host}`);
475
+ outputChannel.appendLine(`Getting workblock workblockId=${item.workblock.id} branch=${item.branch.name} host=${item.connection.host}`);
373
476
  if (!item.dir) {
374
- vscode.window.showErrorMessage("Please bind a directory to the stream first");
477
+ vscode.window.showErrorMessage("Please bind a directory to the branch first");
375
478
  return;
376
479
  }
377
480
  const dir = item.dir;
@@ -383,7 +486,7 @@ function activate(context) {
383
486
  try {
384
487
  await vscode.window.withProgress({
385
488
  location: vscode.ProgressLocation.Notification,
386
- title: `Beyond Work: Getting workblock (${item.stream.title})`,
489
+ title: `Beyond Work: Getting ${item.workblock.name} (${item.branch.name})`,
387
490
  cancellable: true,
388
491
  }, async (_progress, token) => {
389
492
  let cancellationSub;
@@ -391,16 +494,19 @@ function activate(context) {
391
494
  cancellationSub = token.onCancellationRequested(() => reject(new Error("Cancelled")));
392
495
  });
393
496
  try {
497
+ const stream = await resolveBranchItemStream(item);
394
498
  const wb = await Promise.race([
395
- client.getWorkblock(item.stream.id),
499
+ client.getWorkblock(stream.id),
396
500
  cancellation,
397
501
  ]);
398
- outputChannel.appendLine(`Got workblock streamId=${item.stream.id}: ${wb ? wb.id : "none"}`);
502
+ outputChannel.appendLine(`Got workblock ${item.workblock.id} (${item.branch.name}): ${wb ? wb.id : "none"}`);
399
503
  if (!wb) {
400
504
  vscode.window.showErrorMessage("No workblock in workspace");
401
505
  return;
402
506
  }
403
507
  await (0, lib_1.storeWorkblockFiles)(dir, wb, client);
508
+ latestWorkblocks[item.key] = wb;
509
+ await onWorkblockChange(item.key, wb, treeDataProvider);
404
510
  }
405
511
  finally {
406
512
  cancellationSub?.dispose();
@@ -409,21 +515,24 @@ function activate(context) {
409
515
  }
410
516
  catch (e) {
411
517
  const msg = e?.message ?? String(e);
412
- outputChannel.appendLine(`Failed to get workblock streamId=${item.stream.id}: ${msg}`);
518
+ outputChannel.appendLine(`Failed to get workblock ${item.workblock.id} (${item.branch.name}): ${msg}`);
413
519
  vscode.window.showErrorMessage(`Failed to get workblock: ${msg}`);
414
520
  }
415
521
  });
416
522
  const setWB = vscode.commands.registerCommand("beyondwork.beyondwork.set", async (item) => {
417
- outputChannel.appendLine(`Setting workblock ${item.stream}`);
523
+ outputChannel.appendLine(`Setting workblock ${item.workblock.id} (${item.branch.name})`);
418
524
  if (!item.dir) {
419
- vscode.window.showErrorMessage("Please bind a directory to the stream first");
525
+ vscode.window.showErrorMessage("Please bind a directory to the branch first");
420
526
  return;
421
527
  }
422
- await setWorkblock(item.dir, item.connection.host, item.stream.id);
528
+ const stream = await resolveBranchItemStream(item);
529
+ await setWorkblock(item.dir, item.connection.host, stream.id);
530
+ treeDataProvider.setSyncStatus(item.key, false);
423
531
  });
424
532
  const compareChanges = async (item) => {
425
533
  const wb = await (0, lib_1.assembleWorkblock)(item.dir, outputLogger);
426
- const stored = await clientsByHost[item.connection.host].getWorkblock(item.stream.id);
534
+ const stream = await resolveBranchItemStream(item);
535
+ const stored = await clientsByHost[item.connection.host].getWorkblock(stream.id);
427
536
  const file1 = vscode.Uri.parse("bw-manifest:local.yaml");
428
537
  const file2 = vscode.Uri.parse("bw-manifest:stored.yaml");
429
538
  if (wb) {
@@ -435,16 +544,16 @@ function activate(context) {
435
544
  vscode.commands.executeCommand("vscode.diff", file1, file2, "Local vs stored workblock");
436
545
  };
437
546
  const streamOptions = vscode.commands.registerCommand("beyondwork.beyondwork.showStreamOptions", async (item) => {
438
- outputChannel.appendLine(`Stream options ${item}`);
547
+ outputChannel.appendLine(`Branch options ${item.workblock.id} (${item.branch.name})`);
439
548
  const dirOpt = item.dir
440
549
  ? `Change directory from ${item.dir}`
441
- : "Choose a directory for the workspace";
550
+ : "Choose a directory for the branch";
442
551
  const options = [dirOpt];
443
552
  if (item.dir) {
444
553
  options.push("Remove directory binding");
445
- options.push("Get workblock from workspace");
446
- options.push("Store workblock to workspace");
447
- if (syncStreams[item.stream.id] === undefined) {
554
+ options.push("Get workblock branch");
555
+ options.push("Store workblock branch");
556
+ if (syncStreams[item.key] === undefined) {
448
557
  options.push("Enable sync");
449
558
  }
450
559
  else {
@@ -462,21 +571,21 @@ function activate(context) {
462
571
  if (selection) {
463
572
  switch (selection) {
464
573
  case dirOpt:
465
- vscode.commands.executeCommand("beyondwork.beyondwork.bindStream", item.stream, item.dir);
574
+ vscode.commands.executeCommand("beyondwork.beyondwork.bindStream", item);
466
575
  break;
467
- case "Get workblock from workspace":
576
+ case "Get workblock branch":
468
577
  vscode.commands.executeCommand("beyondwork.beyondwork.get", item);
469
578
  break;
470
- case "Store workblock to workspace":
579
+ case "Store workblock branch":
471
580
  vscode.commands.executeCommand("beyondwork.beyondwork.set", item);
472
581
  break;
473
582
  case "Enable sync":
474
- syncStreams[item.stream.id] = item.dir;
583
+ syncStreams[item.key] = item.dir;
475
584
  context.globalState.update("bw_sync_streams", syncStreams);
476
585
  treeDataProvider.refresh();
477
586
  break;
478
587
  case "Disable sync":
479
- delete syncStreams[item.stream.id];
588
+ delete syncStreams[item.key];
480
589
  context.globalState.update("bw_sync_streams", syncStreams);
481
590
  treeDataProvider.refresh();
482
591
  break;
@@ -484,8 +593,8 @@ function activate(context) {
484
593
  compareChanges(item);
485
594
  break;
486
595
  case "Remove directory binding":
487
- delete streamsByDir[item.stream.id];
488
- delete syncStreams[item.stream.id];
596
+ delete streamsByDir[item.key];
597
+ delete syncStreams[item.key];
489
598
  context.globalState.update("bw_streams_dir", streamsByDir);
490
599
  treeDataProvider.refresh();
491
600
  break;
@@ -494,11 +603,12 @@ function activate(context) {
494
603
  });
495
604
  });
496
605
  const syncSelected = vscode.commands.registerCommand("beyondwork.beyondwork.storeSelected", async () => {
497
- const items = treeView.selection.filter((item) => item.contextValue === "beyondwork.stream");
606
+ const items = treeView.selection.filter((item) => item.contextValue === "beyondwork.branch");
498
607
  for (const item of items) {
499
- const si = item;
500
- if (si.dir) {
501
- await setWorkblock(si.dir, si.connection.host, si.stream.id);
608
+ const branch = item;
609
+ if (branch.dir) {
610
+ const stream = await resolveBranchItemStream(branch);
611
+ await setWorkblock(branch.dir, branch.connection.host, stream.id);
502
612
  }
503
613
  }
504
614
  });
@@ -529,7 +639,7 @@ Work blocks are expected to be placed in a directory that contains nothing but w
529
639
  First step is to create a new connection. Click the \`+\` button in the BEYOND WORK sidebar, or use CMD-SHIFT-P and type [Beyond Work: Add Connection](command:beyondwork.beyondwork.addConnection).
530
640
  Then, enter the hostname of the Beyond Work instance you want to connect to. This is the same hostname you use to access the web UI.
531
641
 
532
- Then, click on the connection in the BEYOND WORK sidebar. This will prompt for an API key. You can get this by running \`!tokencreate\` in any workspace.
642
+ Then, click on the connection in the BEYOND WORK sidebar. This will prompt for an API key. You can create one by going to your Profile (top right) -> Settings -> API Tokens.
533
643
 
534
644
  The connection should now have a blue checkmark. Under it, workspaces with an open editor will be shown. Each of them can be bound to a directory by clicking on them
535
645
  and choosing \`Choose a directory\`.
@@ -667,9 +777,7 @@ async function setWorkblock(dir, host, streamId) {
667
777
  outputChannel.appendLine(`Set workblock ${streamId}`);
668
778
  }
669
779
  }
670
- // Automatically bind streams to directories by scanning for manifest.yaml files containing matching id
671
- async function autoBindStreams(streams, context, treeDataProvider) {
672
- // Collect all manifest.yaml files in open workspace folders
780
+ async function autoBindBranches(host, context, treeDataProvider) {
673
781
  const manifestUris = await vscode.workspace.findFiles("**/manifest.yaml", "**/{node_modules,.git}/**");
674
782
  if (!manifestUris.length) {
675
783
  return;
@@ -695,17 +803,23 @@ async function autoBindStreams(streams, context, treeDataProvider) {
695
803
  // ignore individual read errors
696
804
  }
697
805
  }
806
+ const workblocksByHost = context.globalState.get("bw_workblocks", {});
807
+ const branchesByWorkblock = context.globalState.get("bw_workblock_branches", {});
808
+ const workblocks = workblocksByHost[host] ?? [];
698
809
  let updated = false;
699
810
  const existingDirs = new Set(Object.values(streamsByDir));
700
- for (const s of streams) {
701
- if (!s.editing || !s.current_work_block) {
702
- continue;
703
- }
704
- if (!streamsByDir[s.id] && manifestById[s.id]) {
705
- const candidateDir = manifestById[s.current_work_block];
706
- // Skip if directory already bound to a different stream to avoid accidental collisions
811
+ for (const workblock of workblocks) {
812
+ if (manifestById[workblock.id]) {
813
+ const candidateDir = manifestById[workblock.id];
814
+ const workblockBranchKey = (0, tree_data_provider_1.branchKey)(host, workblock.id, "");
815
+ const branches = branchesByWorkblock[workblockBranchKey] ?? workblock.branches ?? [];
816
+ const branch = branches.find((candidate) => candidate.name === "main") ?? branches[0];
817
+ if (!branch) {
818
+ continue;
819
+ }
820
+ const key = (0, tree_data_provider_1.branchKey)(host, workblock.id, branch.name);
707
821
  if (!existingDirs.has(candidateDir)) {
708
- streamsByDir[s.id] = candidateDir;
822
+ streamsByDir[key] = candidateDir;
709
823
  existingDirs.add(candidateDir);
710
824
  updated = true;
711
825
  }