@get-bb/plugin-sdk 0.6.42 → 0.6.49

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.
@@ -15965,6 +15965,22 @@ async function revokeCursorSessionMcpServer(approval) {
15965
15965
 
15966
15966
  // ../provider-bridge-acp/src/bridge/bridge.ts
15967
15967
  var sessionsByBbThreadId = /* @__PURE__ */ new Map();
15968
+ var agentConnections = /* @__PURE__ */ new Set();
15969
+ var shuttingDown = false;
15970
+ function createBridgeAgentConnection(options) {
15971
+ if (shuttingDown) {
15972
+ throw new Error("ACP bridge is shutting down");
15973
+ }
15974
+ const connection = createAcpAgentConnection({
15975
+ ...options,
15976
+ onExit: (info) => {
15977
+ agentConnections.delete(connection);
15978
+ options.onExit(info);
15979
+ }
15980
+ });
15981
+ agentConnections.add(connection);
15982
+ return connection;
15983
+ }
15968
15984
  var bbThreadIdByProviderThreadId = /* @__PURE__ */ new Map();
15969
15985
  var pendingRuntimeRequests = /* @__PURE__ */ new Map();
15970
15986
  var runtimeRequestIdCounter = 0;
@@ -16412,7 +16428,7 @@ async function loadSessionDiscoveredModels(agent, reasoningProbePriorityModelIds
16412
16428
  ...withoutBridgeRuntimeEnv(process.env),
16413
16429
  ...agent.envVars ?? {}
16414
16430
  };
16415
- const connection = createAcpAgentConnection({
16431
+ const connection = createBridgeAgentConnection({
16416
16432
  command: agent.command,
16417
16433
  args: agent.args,
16418
16434
  cwd: agent.cwd ?? process.cwd(),
@@ -17105,7 +17121,7 @@ async function startAgentSession(request) {
17105
17121
  ...params.envVars,
17106
17122
  ...dialect.id === "opencode" ? { OPENCODE_CLIENT: "acp", OPENCODE_ENABLE_QUESTION_TOOL: "false" } : {}
17107
17123
  };
17108
- const connection = createAcpAgentConnection({
17124
+ const connection = createBridgeAgentConnection({
17109
17125
  command: params.agent.command,
17110
17126
  args: launch.args,
17111
17127
  cwd: params.cwd,
@@ -17808,7 +17824,12 @@ async function handleModelList(id, params, dialectId) {
17808
17824
  );
17809
17825
  return;
17810
17826
  }
17811
- const sessionDiscoveredModels = params.listCommand === void 0 && params.agent ? await loadSessionDiscoveredModels(
17827
+ if (params.listCommand) {
17828
+ throw new Error(
17829
+ "ACP model list command failed to provide a model catalog."
17830
+ );
17831
+ }
17832
+ const sessionDiscoveredModels = params.agent ? await loadSessionDiscoveredModels(
17812
17833
  params.agent,
17813
17834
  params.reasoningProbePriorityModelIds,
17814
17835
  params.parameterizedModelPicker
@@ -18111,11 +18132,21 @@ function handleParsedMessage(parsed) {
18111
18132
  }
18112
18133
  var handleLine = createBridgeLineHandler({ handleParsedMessage });
18113
18134
  async function stopAllSessions() {
18114
- await Promise.all(
18115
- Array.from(sessionsByBbThreadId.values()).map(
18116
- (session) => stopSession(session)
18117
- )
18118
- );
18135
+ const results = await Promise.allSettled([
18136
+ ...Array.from(
18137
+ sessionsByBbThreadId.values(),
18138
+ (session) => releaseSession(session)
18139
+ ),
18140
+ ...Array.from(agentConnections, (connection) => connection.kill())
18141
+ ]);
18142
+ for (const result of results) {
18143
+ if (result.status === "rejected") {
18144
+ process.stderr.write(
18145
+ `acp bridge: shutdown cleanup failed: ${String(result.reason)}
18146
+ `
18147
+ );
18148
+ }
18149
+ }
18119
18150
  const dynamicToolBridge = dynamicToolBridgePromise ? await dynamicToolBridgePromise.catch(() => null) : null;
18120
18151
  await new Promise((resolveClose) => {
18121
18152
  if (!dynamicToolBridge) {
@@ -18125,16 +18156,22 @@ async function stopAllSessions() {
18125
18156
  dynamicToolBridge.server.close(() => resolveClose());
18126
18157
  });
18127
18158
  }
18159
+ function shutdownBridge() {
18160
+ if (shuttingDown) return;
18161
+ shuttingDown = true;
18162
+ void stopAllSessions().catch((error) => {
18163
+ process.stderr.write(`acp bridge: shutdown failed: ${String(error)}
18164
+ `);
18165
+ }).finally(() => process.exit(0));
18166
+ }
18128
18167
  if (process.argv.includes("--mcp-stdio")) {
18129
18168
  runAcpDynamicToolMcpServer();
18130
18169
  }
18131
18170
  var experimental_providerBridge = experimental_defineProviderBridge({
18132
18171
  handleLine,
18133
- onClose: () => {
18134
- void stopAllSessions().finally(() => {
18135
- process.exit(0);
18136
- });
18137
- }
18172
+ onClose: shutdownBridge,
18173
+ onSigterm: shutdownBridge,
18174
+ onSigint: shutdownBridge
18138
18175
  });
18139
18176
 
18140
18177
  // ../provider-bridge-acp/src/probe.ts
@@ -2634,6 +2634,7 @@ function asTextDeltaEvent(event) {
2634
2634
  return void 0;
2635
2635
  }
2636
2636
  }
2637
+ var TEXT_DELTA_FLUSH_MS = 50;
2637
2638
  var SEP = THREAD_DELTA_KEY_SEPARATOR;
2638
2639
  function itemKeyString(key) {
2639
2640
  return [
@@ -2663,7 +2664,7 @@ function trimOldestEntries(map, max) {
2663
2664
  function createDeltaAssembler(options) {
2664
2665
  const entropyPrefix = options.entropyPrefix ?? `da${randomUUID().slice(0, 8)}`;
2665
2666
  const progressThrottleMs = options.progressThrottleMs ?? 500;
2666
- const textDeltaFlushMs = options.textDeltaFlushMs ?? 100;
2667
+ const textDeltaFlushMs = options.textDeltaFlushMs ?? TEXT_DELTA_FLUSH_MS;
2667
2668
  const now = options.now ?? Date.now;
2668
2669
  let turnCounter = 0;
2669
2670
  let itemCounter = 0;
@@ -2696,8 +2697,8 @@ function createDeltaAssembler(options) {
2696
2697
  settledItemKeys: /* @__PURE__ */ new Set(),
2697
2698
  progressLastEmitByKey: /* @__PURE__ */ new Map(),
2698
2699
  pendingProgressByKey: /* @__PURE__ */ new Map(),
2699
- textLastEmitByStream: /* @__PURE__ */ new Map(),
2700
- pendingTextByStream: /* @__PURE__ */ new Map()
2700
+ pendingTextByStream: /* @__PURE__ */ new Map(),
2701
+ pendingTextTimer: void 0
2701
2702
  };
2702
2703
  states.set(threadId, created);
2703
2704
  pruneIdleStates();
@@ -2760,59 +2761,67 @@ function createDeltaAssembler(options) {
2760
2761
  }
2761
2762
  }
2762
2763
  function flushPendingText(state, out) {
2763
- if (state.pendingTextByStream.size === 0) {
2764
- return;
2764
+ if (state.pendingTextTimer !== void 0) {
2765
+ clearTimeout(state.pendingTextTimer);
2766
+ state.pendingTextTimer = void 0;
2765
2767
  }
2766
- const nowMs = now();
2767
- for (const [streamKey, pending] of state.pendingTextByStream) {
2768
+ for (const pending of state.pendingTextByStream.values()) {
2768
2769
  out.push({ ...pending.event, delta: pending.text });
2769
- state.textLastEmitByStream.set(streamKey, nowMs);
2770
2770
  }
2771
2771
  state.pendingTextByStream.clear();
2772
- trimOldestEntries(state.textLastEmitByStream, MAX_ID_MAP_ENTRIES);
2773
2772
  }
2774
2773
  function flushElapsedPendingText(state, out) {
2775
2774
  const nowMs = now();
2776
- for (const streamKey of state.pendingTextByStream.keys()) {
2777
- const last = state.textLastEmitByStream.get(streamKey);
2778
- if (last === void 0 || nowMs - last >= textDeltaFlushMs) {
2775
+ for (const pending of state.pendingTextByStream.values()) {
2776
+ if (nowMs - pending.openedAt >= textDeltaFlushMs) {
2779
2777
  flushPendingText(state, out);
2780
2778
  return;
2781
2779
  }
2782
2780
  }
2783
2781
  }
2784
- function bufferTextDelta(state, event, out) {
2785
- const streamKey = `${event.type}${SEP}${event.itemId}`;
2786
- if (event.type === "item/commandExecution/outputDelta" && event.reset === true) {
2787
- flushPendingText(state, out);
2788
- out.push(event);
2789
- state.textLastEmitByStream.set(streamKey, now());
2790
- trimOldestEntries(state.textLastEmitByStream, MAX_ID_MAP_ENTRIES);
2782
+ function schedulePendingTextFlush(threadId, state) {
2783
+ const onDeferredEvents = options.onDeferredEvents;
2784
+ if (onDeferredEvents === void 0 || state.pendingTextTimer !== void 0) {
2791
2785
  return;
2792
2786
  }
2793
- const last = state.textLastEmitByStream.get(streamKey);
2794
- if (last === void 0) {
2795
- flushPendingText(state, out);
2796
- out.push(event);
2797
- state.textLastEmitByStream.set(streamKey, now());
2798
- trimOldestEntries(state.textLastEmitByStream, MAX_ID_MAP_ENTRIES);
2787
+ let openedAt = Number.POSITIVE_INFINITY;
2788
+ for (const pending of state.pendingTextByStream.values()) {
2789
+ openedAt = Math.min(openedAt, pending.openedAt);
2790
+ }
2791
+ if (openedAt === Number.POSITIVE_INFINITY) {
2799
2792
  return;
2800
2793
  }
2801
- const pending = state.pendingTextByStream.get(streamKey);
2802
- if (now() - last >= textDeltaFlushMs) {
2803
- if (pending === void 0) {
2804
- state.pendingTextByStream.set(streamKey, { event, text: event.delta });
2805
- } else {
2806
- pending.text += event.delta;
2807
- }
2794
+ state.pendingTextTimer = setTimeout(
2795
+ () => {
2796
+ state.pendingTextTimer = void 0;
2797
+ const events = [];
2798
+ flushPendingText(state, events);
2799
+ if (events.length > 0) {
2800
+ onDeferredEvents(threadId, events);
2801
+ }
2802
+ },
2803
+ Math.max(0, openedAt + textDeltaFlushMs - now())
2804
+ );
2805
+ state.pendingTextTimer.unref?.();
2806
+ }
2807
+ function bufferTextDelta(state, event, out) {
2808
+ if (event.type === "item/commandExecution/outputDelta" && event.reset === true) {
2808
2809
  flushPendingText(state, out);
2810
+ out.push(event);
2809
2811
  return;
2810
2812
  }
2813
+ const streamKey = `${event.type}${SEP}${event.itemId}`;
2814
+ const pending = state.pendingTextByStream.get(streamKey);
2811
2815
  if (pending === void 0) {
2812
- state.pendingTextByStream.set(streamKey, { event, text: event.delta });
2813
- return;
2816
+ state.pendingTextByStream.set(streamKey, {
2817
+ event,
2818
+ openedAt: now(),
2819
+ text: event.delta
2820
+ });
2821
+ } else {
2822
+ pending.text += event.delta;
2814
2823
  }
2815
- pending.text += event.delta;
2824
+ flushElapsedPendingText(state, out);
2816
2825
  }
2817
2826
  function rememberSettledKey(state, key) {
2818
2827
  state.settledItemKeys.add(key);
@@ -4193,13 +4202,13 @@ function createDeltaAssembler(options) {
4193
4202
  push: (...newEvents) => {
4194
4203
  for (const event of newEvents) {
4195
4204
  const textDelta = textDeltaFlushMs > 0 ? asTextDeltaEvent(event) : void 0;
4196
- const state = states.get(args.threadId);
4197
- if (textDelta !== void 0 && state !== void 0) {
4198
- bufferTextDelta(state, textDelta, events);
4205
+ const state2 = states.get(args.threadId);
4206
+ if (textDelta !== void 0 && state2 !== void 0) {
4207
+ bufferTextDelta(state2, textDelta, events);
4199
4208
  continue;
4200
4209
  }
4201
- if (state !== void 0) {
4202
- flushPendingText(state, events);
4210
+ if (state2 !== void 0) {
4211
+ flushPendingText(state2, events);
4203
4212
  }
4204
4213
  events.push(event);
4205
4214
  }
@@ -4218,21 +4227,19 @@ function createDeltaAssembler(options) {
4218
4227
  }
4219
4228
  for (const delta of args.deltas) {
4220
4229
  if (delta.kind === "session.reset") {
4221
- const state = states.get(args.threadId);
4222
- if (state !== void 0) {
4223
- flushPendingText(state, events);
4230
+ const state2 = states.get(args.threadId);
4231
+ if (state2 !== void 0) {
4232
+ flushPendingText(state2, events);
4224
4233
  }
4225
4234
  states.delete(args.threadId);
4226
4235
  continue;
4227
4236
  }
4228
- if (delta.kind === "item.textClose" || delta.kind === "item.close" || delta.kind === "session.ended") {
4229
- const state = states.get(args.threadId);
4230
- if (state !== void 0) {
4231
- flushPendingText(state, events);
4232
- }
4233
- }
4234
4237
  handleDelta(stateFor(args.threadId), delta, sink);
4235
4238
  }
4239
+ const state = states.get(args.threadId);
4240
+ if (state !== void 0) {
4241
+ schedulePendingTextFlush(args.threadId, state);
4242
+ }
4236
4243
  return events;
4237
4244
  },
4238
4245
  getBbItemId(threadId, providerItemId) {
@@ -6609,6 +6616,17 @@ var REPLAY_CHILD_PATH = fileURLToPath2(
6609
6616
  new URL("./replay-provider-child.mjs", import.meta.url)
6610
6617
  );
6611
6618
  var PARITY_INITIALIZE_ID = "parity-initialize";
6619
+ var BRIDGE_EXIT_STDERR_TAIL_CHARS = 2e3;
6620
+ function describeBridgeExit(child) {
6621
+ if (child.pid === void 0) return "never started";
6622
+ if (child.signalCode !== null) return `signal ${child.signalCode}`;
6623
+ return `code ${String(child.exitCode)}`;
6624
+ }
6625
+ function stderrTail(stderr) {
6626
+ const trimmed = stderr.trim();
6627
+ if (trimmed === "") return "; no stderr";
6628
+ return `; stderr: ${trimmed.slice(-BRIDGE_EXIT_STDERR_TAIL_CHARS)}`;
6629
+ }
6612
6630
  function sleep(ms) {
6613
6631
  return new Promise((resolveSleep) => setTimeout(resolveSleep, ms));
6614
6632
  }
@@ -6632,7 +6650,20 @@ async function acquireReplayStartup() {
6632
6650
  else next();
6633
6651
  };
6634
6652
  }
6653
+ var BRIDGE_STARTUP_ATTEMPTS = 2;
6635
6654
  async function replayRecording(options) {
6655
+ for (let attempt = 1; ; attempt += 1) {
6656
+ const { run, crashedBeforeInitialize } = await replayRecordingAttempt(options);
6657
+ if (!crashedBeforeInitialize || attempt >= BRIDGE_STARTUP_ATTEMPTS) {
6658
+ return run;
6659
+ }
6660
+ process.stderr.write(
6661
+ `parity replay of ${options.recordingDir}: retrying after a bridge startup crash: ${run.stalls.join("; ")}
6662
+ `
6663
+ );
6664
+ }
6665
+ }
6666
+ async function replayRecordingAttempt(options) {
6636
6667
  const timeoutMs = options.timeoutMs ?? 15e3;
6637
6668
  const orderTimeoutMs = options.orderTimeoutMs ?? 5e3;
6638
6669
  const settleMs = options.settleMs ?? 750;
@@ -6749,6 +6780,7 @@ async function replayRecording(options) {
6749
6780
  providerId
6750
6781
  ).events.length : null;
6751
6782
  const answeredIds = /* @__PURE__ */ new Set();
6783
+ const sentRequestIds = [];
6752
6784
  const pendingBridgeRequests = [];
6753
6785
  const recordedAnswers = /* @__PURE__ */ new Map();
6754
6786
  for (const step of steps) {
@@ -6825,15 +6857,33 @@ async function replayRecording(options) {
6825
6857
  stalls.push(`oversized bridge line (${bytes} bytes)`);
6826
6858
  }
6827
6859
  });
6860
+ let reportedBridgeExit = false;
6828
6861
  async function waitFor(label, predicate, limitMs = timeoutMs, reportStall = true) {
6862
+ const diagnostic = () => JSON.stringify({
6863
+ events: events.length,
6864
+ plannedEvents: plannedEventCount,
6865
+ grammarViolationCount: grammarViolations.length,
6866
+ grammarViolations: grammarViolations.slice(-5),
6867
+ unansweredRequests: sentRequestIds.filter(
6868
+ (id) => !answeredIds.has(id)
6869
+ ),
6870
+ exitCode: child.exitCode,
6871
+ signal: child.signalCode,
6872
+ millisecondsSinceOutput: Date.now() - lastOutputAt,
6873
+ stderrTail: stderr.slice(-2e3)
6874
+ });
6829
6875
  const deadline = Date.now() + limitMs;
6830
6876
  while (!predicate()) {
6831
6877
  if (child.exitCode !== null || child.signalCode !== null || child.pid === void 0) {
6832
- stalls.push(`bridge exited while waiting for ${label}`);
6878
+ stalls.push(
6879
+ `bridge exited (${describeBridgeExit(child)}) while waiting for ${label}${reportedBridgeExit ? "" : stderrTail(stderr)}: ${diagnostic()}`
6880
+ );
6881
+ reportedBridgeExit = true;
6833
6882
  return;
6834
6883
  }
6835
6884
  if (Date.now() > deadline) {
6836
- if (reportStall) stalls.push(`timed out waiting for ${label}`);
6885
+ if (reportStall)
6886
+ stalls.push(`timed out waiting for ${label}: ${diagnostic()}`);
6837
6887
  return;
6838
6888
  }
6839
6889
  await sleep(10);
@@ -6858,13 +6908,30 @@ async function replayRecording(options) {
6858
6908
  );
6859
6909
  await waitFor("initialize response", () => answeredIds.has(initializeId));
6860
6910
  const initialized = answeredIds.has(initializeId);
6911
+ const crashedBeforeInitialize = !initialized && (child.pid === void 0 || child.exitCode !== null || child.signalCode !== null);
6861
6912
  if (!initialized) {
6862
6913
  if (child.pid !== void 0 && child.exitCode === null && child.signalCode === null)
6863
6914
  child.kill("SIGKILL");
6864
6915
  await childEnded;
6865
6916
  }
6866
6917
  releaseStartup();
6867
- const sentRequestIds = [];
6918
+ if (crashedBeforeInitialize) {
6919
+ return {
6920
+ run: {
6921
+ providerId,
6922
+ recordingDir: options.recordingDir,
6923
+ lines,
6924
+ lineTimes,
6925
+ lineAfter,
6926
+ events,
6927
+ grammarViolations,
6928
+ stalls,
6929
+ stderr,
6930
+ exitCode: child.exitCode
6931
+ },
6932
+ crashedBeforeInitialize
6933
+ };
6934
+ }
6868
6935
  for (const step of initialized ? steps : []) {
6869
6936
  if (step.message === null || !isRequest(step.message)) {
6870
6937
  if (step.message !== null && !isResponse(step.message)) {
@@ -6955,16 +7022,19 @@ async function replayRecording(options) {
6955
7022
  ]);
6956
7023
  clearTimeout(shutdownTimer);
6957
7024
  return {
6958
- providerId,
6959
- recordingDir: options.recordingDir,
6960
- lines,
6961
- lineTimes,
6962
- lineAfter,
6963
- events,
6964
- grammarViolations,
6965
- stalls,
6966
- stderr,
6967
- exitCode
7025
+ run: {
7026
+ providerId,
7027
+ recordingDir: options.recordingDir,
7028
+ lines,
7029
+ lineTimes,
7030
+ lineAfter,
7031
+ events,
7032
+ grammarViolations,
7033
+ stalls,
7034
+ stderr,
7035
+ exitCode
7036
+ },
7037
+ crashedBeforeInitialize: false
6968
7038
  };
6969
7039
  } finally {
6970
7040
  if (spawnedChild !== void 0 && spawnedChild.pid !== void 0 && spawnedChild.exitCode === null && spawnedChild.signalCode === null) {
@@ -281,16 +281,19 @@ function readCursor(stateDir) {
281
281
  let text;
282
282
  try {
283
283
  text = readFileSync(join(stateDir, "cursor"), "utf8").trim();
284
- } catch {
285
- return null;
284
+ } catch (error) {
285
+ return error?.code === "ENOENT" ? null : false;
286
286
  }
287
- if (text === "end" || text === "") return null;
287
+ if (text === "end") return null;
288
+ if (!/^\d+ \d+$/u.test(text)) return false;
288
289
  const [run, seq] = text.split(" ").map(Number);
290
+ if (!Number.isSafeInteger(run) || !Number.isSafeInteger(seq)) return false;
289
291
  return { run, seq };
290
292
  }
291
293
 
292
294
  function cursorAllows(cursor, entry) {
293
295
  if (cursor === null) return true;
296
+ if (cursor === false) return false;
294
297
  return entry.run < cursor.run || (entry.run === cursor.run && entry.seq < cursor.seq);
295
298
  }
296
299
 
@@ -960,6 +960,7 @@ function normalizeExperimentalFileOpenOptions(value) {
960
960
  // src/thread-action-groups.ts
961
961
  var experimental_THREAD_ACTION_GROUPS = {
962
962
  open: "1_open",
963
+ state: "1_state",
963
964
  organize: "2_organize",
964
965
  settings: "3_settings",
965
966
  lifecycle: "4_lifecycle"
@@ -2517,6 +2518,9 @@ var testPluginSdkApp = {
2517
2518
  experimental_copyToClipboard(content) {
2518
2519
  return activeClipboard?.(content) ?? Promise.resolve(true);
2519
2520
  },
2521
+ experimental_saveFile() {
2522
+ return Promise.resolve("started");
2523
+ },
2520
2524
  experimental_useSidebarThreadActions() {
2521
2525
  return useSlotEnv("experimental_useSidebarThreadActions").sidebarActions;
2522
2526
  },