@get-bb/plugin-sdk 0.6.43 → 0.6.52

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.
@@ -1423,10 +1423,12 @@ var threadEventContextWindowUsageSchema = z14.object({
1423
1423
  estimated: z14.boolean(),
1424
1424
  cost: threadUsageCostSchema.optional()
1425
1425
  });
1426
+ var promptCacheTtlMsSchema = z14.number().int().positive();
1426
1427
  var threadEventTokenUsageSchema = z14.object({
1427
1428
  total: threadEventTokenUsageBreakdownSchema,
1428
1429
  last: threadEventTokenUsageBreakdownSchema,
1429
- modelContextWindow: z14.number().nullable()
1430
+ modelContextWindow: z14.number().nullable(),
1431
+ promptCacheTtlMs: promptCacheTtlMsSchema.optional()
1430
1432
  });
1431
1433
  var threadEventWarningCategorySchema = z14.enum([
1432
1434
  "deprecation",
@@ -2327,6 +2329,7 @@ var threadDeltaSchema = z18.discriminatedUnion("kind", [
2327
2329
  total: threadEventTokenUsageBreakdownSchema,
2328
2330
  last: threadEventTokenUsageBreakdownSchema,
2329
2331
  modelContextWindow: z18.number().nullable(),
2332
+ promptCacheTtlMs: promptCacheTtlMsSchema.optional(),
2330
2333
  providerTurnId: providerTurnIdSchema.optional()
2331
2334
  }),
2332
2335
  z18.object({
@@ -2634,6 +2637,7 @@ function asTextDeltaEvent(event) {
2634
2637
  return void 0;
2635
2638
  }
2636
2639
  }
2640
+ var TEXT_DELTA_FLUSH_MS = 50;
2637
2641
  var SEP = THREAD_DELTA_KEY_SEPARATOR;
2638
2642
  function itemKeyString(key) {
2639
2643
  return [
@@ -2663,7 +2667,7 @@ function trimOldestEntries(map, max) {
2663
2667
  function createDeltaAssembler(options) {
2664
2668
  const entropyPrefix = options.entropyPrefix ?? `da${randomUUID().slice(0, 8)}`;
2665
2669
  const progressThrottleMs = options.progressThrottleMs ?? 500;
2666
- const textDeltaFlushMs = options.textDeltaFlushMs ?? 100;
2670
+ const textDeltaFlushMs = options.textDeltaFlushMs ?? TEXT_DELTA_FLUSH_MS;
2667
2671
  const now = options.now ?? Date.now;
2668
2672
  let turnCounter = 0;
2669
2673
  let itemCounter = 0;
@@ -2696,8 +2700,8 @@ function createDeltaAssembler(options) {
2696
2700
  settledItemKeys: /* @__PURE__ */ new Set(),
2697
2701
  progressLastEmitByKey: /* @__PURE__ */ new Map(),
2698
2702
  pendingProgressByKey: /* @__PURE__ */ new Map(),
2699
- textLastEmitByStream: /* @__PURE__ */ new Map(),
2700
- pendingTextByStream: /* @__PURE__ */ new Map()
2703
+ pendingTextByStream: /* @__PURE__ */ new Map(),
2704
+ pendingTextTimer: void 0
2701
2705
  };
2702
2706
  states.set(threadId, created);
2703
2707
  pruneIdleStates();
@@ -2760,59 +2764,67 @@ function createDeltaAssembler(options) {
2760
2764
  }
2761
2765
  }
2762
2766
  function flushPendingText(state, out) {
2763
- if (state.pendingTextByStream.size === 0) {
2764
- return;
2767
+ if (state.pendingTextTimer !== void 0) {
2768
+ clearTimeout(state.pendingTextTimer);
2769
+ state.pendingTextTimer = void 0;
2765
2770
  }
2766
- const nowMs = now();
2767
- for (const [streamKey, pending] of state.pendingTextByStream) {
2771
+ for (const pending of state.pendingTextByStream.values()) {
2768
2772
  out.push({ ...pending.event, delta: pending.text });
2769
- state.textLastEmitByStream.set(streamKey, nowMs);
2770
2773
  }
2771
2774
  state.pendingTextByStream.clear();
2772
- trimOldestEntries(state.textLastEmitByStream, MAX_ID_MAP_ENTRIES);
2773
2775
  }
2774
2776
  function flushElapsedPendingText(state, out) {
2775
2777
  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) {
2778
+ for (const pending of state.pendingTextByStream.values()) {
2779
+ if (nowMs - pending.openedAt >= textDeltaFlushMs) {
2779
2780
  flushPendingText(state, out);
2780
2781
  return;
2781
2782
  }
2782
2783
  }
2783
2784
  }
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);
2785
+ function schedulePendingTextFlush(threadId, state) {
2786
+ const onDeferredEvents = options.onDeferredEvents;
2787
+ if (onDeferredEvents === void 0 || state.pendingTextTimer !== void 0) {
2791
2788
  return;
2792
2789
  }
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);
2790
+ let openedAt = Number.POSITIVE_INFINITY;
2791
+ for (const pending of state.pendingTextByStream.values()) {
2792
+ openedAt = Math.min(openedAt, pending.openedAt);
2793
+ }
2794
+ if (openedAt === Number.POSITIVE_INFINITY) {
2799
2795
  return;
2800
2796
  }
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
- }
2797
+ state.pendingTextTimer = setTimeout(
2798
+ () => {
2799
+ state.pendingTextTimer = void 0;
2800
+ const events = [];
2801
+ flushPendingText(state, events);
2802
+ if (events.length > 0) {
2803
+ onDeferredEvents(threadId, events);
2804
+ }
2805
+ },
2806
+ Math.max(0, openedAt + textDeltaFlushMs - now())
2807
+ );
2808
+ state.pendingTextTimer.unref?.();
2809
+ }
2810
+ function bufferTextDelta(state, event, out) {
2811
+ if (event.type === "item/commandExecution/outputDelta" && event.reset === true) {
2808
2812
  flushPendingText(state, out);
2813
+ out.push(event);
2809
2814
  return;
2810
2815
  }
2816
+ const streamKey = `${event.type}${SEP}${event.itemId}`;
2817
+ const pending = state.pendingTextByStream.get(streamKey);
2811
2818
  if (pending === void 0) {
2812
- state.pendingTextByStream.set(streamKey, { event, text: event.delta });
2813
- return;
2819
+ state.pendingTextByStream.set(streamKey, {
2820
+ event,
2821
+ openedAt: now(),
2822
+ text: event.delta
2823
+ });
2824
+ } else {
2825
+ pending.text += event.delta;
2814
2826
  }
2815
- pending.text += event.delta;
2827
+ flushElapsedPendingText(state, out);
2816
2828
  }
2817
2829
  function rememberSettledKey(state, key) {
2818
2830
  state.settledItemKeys.add(key);
@@ -3967,7 +3979,8 @@ function createDeltaAssembler(options) {
3967
3979
  tokenUsage: {
3968
3980
  total: { ...delta.total },
3969
3981
  last: { ...delta.last },
3970
- modelContextWindow: delta.modelContextWindow
3982
+ modelContextWindow: delta.modelContextWindow,
3983
+ ...delta.promptCacheTtlMs === void 0 ? {} : { promptCacheTtlMs: delta.promptCacheTtlMs }
3971
3984
  }
3972
3985
  });
3973
3986
  return;
@@ -4193,13 +4206,13 @@ function createDeltaAssembler(options) {
4193
4206
  push: (...newEvents) => {
4194
4207
  for (const event of newEvents) {
4195
4208
  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);
4209
+ const state2 = states.get(args.threadId);
4210
+ if (textDelta !== void 0 && state2 !== void 0) {
4211
+ bufferTextDelta(state2, textDelta, events);
4199
4212
  continue;
4200
4213
  }
4201
- if (state !== void 0) {
4202
- flushPendingText(state, events);
4214
+ if (state2 !== void 0) {
4215
+ flushPendingText(state2, events);
4203
4216
  }
4204
4217
  events.push(event);
4205
4218
  }
@@ -4218,21 +4231,19 @@ function createDeltaAssembler(options) {
4218
4231
  }
4219
4232
  for (const delta of args.deltas) {
4220
4233
  if (delta.kind === "session.reset") {
4221
- const state = states.get(args.threadId);
4222
- if (state !== void 0) {
4223
- flushPendingText(state, events);
4234
+ const state2 = states.get(args.threadId);
4235
+ if (state2 !== void 0) {
4236
+ flushPendingText(state2, events);
4224
4237
  }
4225
4238
  states.delete(args.threadId);
4226
4239
  continue;
4227
4240
  }
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
4241
  handleDelta(stateFor(args.threadId), delta, sink);
4235
4242
  }
4243
+ const state = states.get(args.threadId);
4244
+ if (state !== void 0) {
4245
+ schedulePendingTextFlush(args.threadId, state);
4246
+ }
4236
4247
  return events;
4237
4248
  },
4238
4249
  getBbItemId(threadId, providerItemId) {
@@ -6609,6 +6620,17 @@ var REPLAY_CHILD_PATH = fileURLToPath2(
6609
6620
  new URL("./replay-provider-child.mjs", import.meta.url)
6610
6621
  );
6611
6622
  var PARITY_INITIALIZE_ID = "parity-initialize";
6623
+ var BRIDGE_EXIT_STDERR_TAIL_CHARS = 2e3;
6624
+ function describeBridgeExit(child) {
6625
+ if (child.pid === void 0) return "never started";
6626
+ if (child.signalCode !== null) return `signal ${child.signalCode}`;
6627
+ return `code ${String(child.exitCode)}`;
6628
+ }
6629
+ function stderrTail(stderr) {
6630
+ const trimmed = stderr.trim();
6631
+ if (trimmed === "") return "; no stderr";
6632
+ return `; stderr: ${trimmed.slice(-BRIDGE_EXIT_STDERR_TAIL_CHARS)}`;
6633
+ }
6612
6634
  function sleep(ms) {
6613
6635
  return new Promise((resolveSleep) => setTimeout(resolveSleep, ms));
6614
6636
  }
@@ -6632,7 +6654,20 @@ async function acquireReplayStartup() {
6632
6654
  else next();
6633
6655
  };
6634
6656
  }
6657
+ var BRIDGE_STARTUP_ATTEMPTS = 2;
6635
6658
  async function replayRecording(options) {
6659
+ for (let attempt = 1; ; attempt += 1) {
6660
+ const { run, crashedBeforeInitialize } = await replayRecordingAttempt(options);
6661
+ if (!crashedBeforeInitialize || attempt >= BRIDGE_STARTUP_ATTEMPTS) {
6662
+ return run;
6663
+ }
6664
+ process.stderr.write(
6665
+ `parity replay of ${options.recordingDir}: retrying after a bridge startup crash: ${run.stalls.join("; ")}
6666
+ `
6667
+ );
6668
+ }
6669
+ }
6670
+ async function replayRecordingAttempt(options) {
6636
6671
  const timeoutMs = options.timeoutMs ?? 15e3;
6637
6672
  const orderTimeoutMs = options.orderTimeoutMs ?? 5e3;
6638
6673
  const settleMs = options.settleMs ?? 750;
@@ -6749,6 +6784,7 @@ async function replayRecording(options) {
6749
6784
  providerId
6750
6785
  ).events.length : null;
6751
6786
  const answeredIds = /* @__PURE__ */ new Set();
6787
+ const sentRequestIds = [];
6752
6788
  const pendingBridgeRequests = [];
6753
6789
  const recordedAnswers = /* @__PURE__ */ new Map();
6754
6790
  for (const step of steps) {
@@ -6825,15 +6861,33 @@ async function replayRecording(options) {
6825
6861
  stalls.push(`oversized bridge line (${bytes} bytes)`);
6826
6862
  }
6827
6863
  });
6864
+ let reportedBridgeExit = false;
6828
6865
  async function waitFor(label, predicate, limitMs = timeoutMs, reportStall = true) {
6866
+ const diagnostic = () => JSON.stringify({
6867
+ events: events.length,
6868
+ plannedEvents: plannedEventCount,
6869
+ grammarViolationCount: grammarViolations.length,
6870
+ grammarViolations: grammarViolations.slice(-5),
6871
+ unansweredRequests: sentRequestIds.filter(
6872
+ (id) => !answeredIds.has(id)
6873
+ ),
6874
+ exitCode: child.exitCode,
6875
+ signal: child.signalCode,
6876
+ millisecondsSinceOutput: Date.now() - lastOutputAt,
6877
+ stderrTail: stderr.slice(-2e3)
6878
+ });
6829
6879
  const deadline = Date.now() + limitMs;
6830
6880
  while (!predicate()) {
6831
6881
  if (child.exitCode !== null || child.signalCode !== null || child.pid === void 0) {
6832
- stalls.push(`bridge exited while waiting for ${label}`);
6882
+ stalls.push(
6883
+ `bridge exited (${describeBridgeExit(child)}) while waiting for ${label}${reportedBridgeExit ? "" : stderrTail(stderr)}: ${diagnostic()}`
6884
+ );
6885
+ reportedBridgeExit = true;
6833
6886
  return;
6834
6887
  }
6835
6888
  if (Date.now() > deadline) {
6836
- if (reportStall) stalls.push(`timed out waiting for ${label}`);
6889
+ if (reportStall)
6890
+ stalls.push(`timed out waiting for ${label}: ${diagnostic()}`);
6837
6891
  return;
6838
6892
  }
6839
6893
  await sleep(10);
@@ -6858,13 +6912,30 @@ async function replayRecording(options) {
6858
6912
  );
6859
6913
  await waitFor("initialize response", () => answeredIds.has(initializeId));
6860
6914
  const initialized = answeredIds.has(initializeId);
6915
+ const crashedBeforeInitialize = !initialized && (child.pid === void 0 || child.exitCode !== null || child.signalCode !== null);
6861
6916
  if (!initialized) {
6862
6917
  if (child.pid !== void 0 && child.exitCode === null && child.signalCode === null)
6863
6918
  child.kill("SIGKILL");
6864
6919
  await childEnded;
6865
6920
  }
6866
6921
  releaseStartup();
6867
- const sentRequestIds = [];
6922
+ if (crashedBeforeInitialize) {
6923
+ return {
6924
+ run: {
6925
+ providerId,
6926
+ recordingDir: options.recordingDir,
6927
+ lines,
6928
+ lineTimes,
6929
+ lineAfter,
6930
+ events,
6931
+ grammarViolations,
6932
+ stalls,
6933
+ stderr,
6934
+ exitCode: child.exitCode
6935
+ },
6936
+ crashedBeforeInitialize
6937
+ };
6938
+ }
6868
6939
  for (const step of initialized ? steps : []) {
6869
6940
  if (step.message === null || !isRequest(step.message)) {
6870
6941
  if (step.message !== null && !isResponse(step.message)) {
@@ -6955,16 +7026,19 @@ async function replayRecording(options) {
6955
7026
  ]);
6956
7027
  clearTimeout(shutdownTimer);
6957
7028
  return {
6958
- providerId,
6959
- recordingDir: options.recordingDir,
6960
- lines,
6961
- lineTimes,
6962
- lineAfter,
6963
- events,
6964
- grammarViolations,
6965
- stalls,
6966
- stderr,
6967
- exitCode
7029
+ run: {
7030
+ providerId,
7031
+ recordingDir: options.recordingDir,
7032
+ lines,
7033
+ lineTimes,
7034
+ lineAfter,
7035
+ events,
7036
+ grammarViolations,
7037
+ stalls,
7038
+ stderr,
7039
+ exitCode
7040
+ },
7041
+ crashedBeforeInitialize: false
6968
7042
  };
6969
7043
  } finally {
6970
7044
  if (spawnedChild !== void 0 && spawnedChild.pid !== void 0 && spawnedChild.exitCode === null && spawnedChild.signalCode === null) {
@@ -2865,10 +2865,12 @@ var threadEventContextWindowUsageSchema = z18.object({
2865
2865
  estimated: z18.boolean(),
2866
2866
  cost: threadUsageCostSchema.optional()
2867
2867
  });
2868
+ var promptCacheTtlMsSchema = z18.number().int().positive();
2868
2869
  var threadEventTokenUsageSchema = z18.object({
2869
2870
  total: threadEventTokenUsageBreakdownSchema,
2870
2871
  last: threadEventTokenUsageBreakdownSchema,
2871
- modelContextWindow: z18.number().nullable()
2872
+ modelContextWindow: z18.number().nullable(),
2873
+ promptCacheTtlMs: promptCacheTtlMsSchema.optional()
2872
2874
  });
2873
2875
  var threadEventWarningCategorySchema = z18.enum([
2874
2876
  "deprecation",
@@ -11925,6 +11927,7 @@ var threadDeltaSchema = z34.discriminatedUnion("kind", [
11925
11927
  total: threadEventTokenUsageBreakdownSchema,
11926
11928
  last: threadEventTokenUsageBreakdownSchema,
11927
11929
  modelContextWindow: z34.number().nullable(),
11930
+ promptCacheTtlMs: promptCacheTtlMsSchema.optional(),
11928
11931
  providerTurnId: providerTurnIdSchema.optional()
11929
11932
  }),
11930
11933
  z34.object({
@@ -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
  },