@get-bb/plugin-sdk 0.6.157 → 0.6.169

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.
Files changed (30) hide show
  1. package/README.md +1 -1
  2. package/bundled-types/bb-plugin-sdk-app.d.ts +1010 -353
  3. package/bundled-types/bb-plugin-sdk-environment-provider.d.ts +3 -0
  4. package/bundled-types/bb-plugin-sdk-host.d.ts +4 -1
  5. package/bundled-types/bb-plugin-sdk-internal-host-policy.d.ts +9 -6
  6. package/bundled-types/bb-plugin-sdk-internal-plugin-app-collector.d.ts +10 -4
  7. package/bundled-types/bb-plugin-sdk-internal-thread-event-gate.d.ts +1001 -0
  8. package/bundled-types/bb-plugin-sdk-internal-thread-status.d.ts +2 -2
  9. package/bundled-types/bb-plugin-sdk-provider-bridge-acp-next.d.ts +56 -6
  10. package/bundled-types/bb-plugin-sdk-provider-bridge-acp.d.ts +56 -6
  11. package/bundled-types/bb-plugin-sdk-provider-bridge.d.ts +45 -8
  12. package/bundled-types/bb-plugin-sdk-testing-app.d.ts +7 -3
  13. package/bundled-types/bb-plugin-sdk-testing.d.ts +14 -19
  14. package/bundled-types/bb-plugin-sdk.d.ts +1043 -436
  15. package/dist/app.js +8 -0
  16. package/dist/host.js +27 -3
  17. package/dist/index.js +19 -1
  18. package/dist/internal/host-policy.js +34 -13
  19. package/dist/internal/plugin-app-collector.js +35 -3
  20. package/dist/internal/rpc-query-host.js +1 -0
  21. package/dist/internal/thread-event-gate.js +95 -0
  22. package/dist/process.js +7 -1
  23. package/dist/provider-bridge-acp-next.js +503 -144
  24. package/dist/provider-bridge-acp.js +503 -144
  25. package/dist/provider-bridge-testing.js +12 -4
  26. package/dist/provider-bridge.js +115 -34
  27. package/dist/replay-provider-child.mjs +158 -31
  28. package/dist/testing/app.js +647 -351
  29. package/dist/testing/index.js +184 -77
  30. package/package.json +8 -1
@@ -329,6 +329,23 @@ var PLUGIN_INTERACTION_MAX_TITLE_LENGTH = 160;
329
329
  var PLUGIN_INTERACTION_MAX_PAYLOAD_BYTES = 64 * 1024;
330
330
  var PLUGIN_INTERACTION_MAX_TIMEOUT_MS = 7 * 24 * 60 * 60 * 1e3;
331
331
 
332
+ // ../text-utils/src/utf16.ts
333
+ function isHighSurrogateAt(value, index) {
334
+ const code = value.charCodeAt(index);
335
+ return code >= 55296 && code <= 56319;
336
+ }
337
+ function isLowSurrogateAt(value, index) {
338
+ const code = value.charCodeAt(index);
339
+ return code >= 56320 && code <= 57343;
340
+ }
341
+ function sliceUtf16Head(value, maxChars) {
342
+ let end = Math.min(value.length, Math.max(0, maxChars));
343
+ if (isHighSurrogateAt(value, end - 1) && isLowSurrogateAt(value, end)) {
344
+ end -= 1;
345
+ }
346
+ return value.slice(0, end);
347
+ }
348
+
332
349
  // ../domain/src/plugin-icon.ts
333
350
  function isPluginOwnedIconPath(icon) {
334
351
  return icon.startsWith("./");
@@ -2268,7 +2285,9 @@ function runPluginStorageMigrations(database, statements) {
2268
2285
  if (!migrationColumns.some((column) => column.name === "statement_hash")) {
2269
2286
  database.exec("ALTER TABLE _bb_migrations ADD COLUMN statement_hash TEXT");
2270
2287
  }
2271
- const rows = database.prepare("SELECT id, statement_hash FROM _bb_migrations ORDER BY id").all();
2288
+ const rows = database.prepare(
2289
+ "SELECT id, statement_hash FROM _bb_migrations ORDER BY id"
2290
+ ).all();
2272
2291
  const applied = /* @__PURE__ */ new Map();
2273
2292
  for (const row of rows) applied.set(row.id, row.statement_hash);
2274
2293
  const statementHashes = statements.map(migrationStatementHash);
@@ -2631,20 +2650,19 @@ function normalizeInteractionRequest(request) {
2631
2650
  `ui.requestInput title must be 1-${PLUGIN_INTERACTION_MAX_TITLE_LENGTH} characters`
2632
2651
  );
2633
2652
  }
2634
- let payload;
2653
+ let json;
2635
2654
  try {
2636
- const json = JSON.stringify(request.payload);
2637
- if (json === void 0) throw new Error();
2638
- if (Buffer.byteLength(json, "utf8") > PLUGIN_INTERACTION_MAX_PAYLOAD_BYTES) {
2639
- throw new Error("ui.requestInput payload exceeds 64 KiB");
2640
- }
2641
- payload = JSON.parse(json);
2642
- } catch (error) {
2643
- if (error instanceof Error && error.message.includes("64 KiB")) {
2644
- throw error;
2645
- }
2655
+ json = JSON.stringify(request.payload);
2656
+ } catch {
2646
2657
  throw new Error("ui.requestInput payload must be JSON-serializable");
2647
2658
  }
2659
+ if (json === void 0) {
2660
+ throw new Error("ui.requestInput payload must be JSON-serializable");
2661
+ }
2662
+ if (Buffer.byteLength(json, "utf8") > PLUGIN_INTERACTION_MAX_PAYLOAD_BYTES) {
2663
+ throw new Error("ui.requestInput payload exceeds 64 KiB");
2664
+ }
2665
+ const payload = JSON.parse(json);
2648
2666
  const timeoutMs = request.timeoutMs ?? 10 * 60 * 1e3;
2649
2667
  if (!Number.isInteger(timeoutMs) || timeoutMs <= 0 || timeoutMs > PLUGIN_INTERACTION_MAX_TIMEOUT_MS) {
2650
2668
  throw new Error(
@@ -2821,7 +2839,10 @@ function normalizePluginAgentConfiguration(args) {
2821
2839
  if (output.instructions !== void 0 && typeof output.instructions !== "string") {
2822
2840
  throw new Error("configure() output.instructions must be a string");
2823
2841
  }
2824
- const instructions = typeof output.instructions === "string" && output.instructions.trim().length > 0 ? output.instructions.slice(0, PLUGIN_DYNAMIC_INSTRUCTIONS_MAX_CHARS) : null;
2842
+ const instructions = typeof output.instructions === "string" && output.instructions.trim().length > 0 ? sliceUtf16Head(
2843
+ output.instructions,
2844
+ PLUGIN_DYNAMIC_INSTRUCTIONS_MAX_CHARS
2845
+ ) : null;
2825
2846
  const toolSelections = normalizePluginAgentToolSelections({
2826
2847
  knownIds: args.knownToolIds,
2827
2848
  pluginId: args.pluginId,
@@ -3041,6 +3062,10 @@ function createFakeSdk(options) {
3041
3062
  `bb.sdk.${path} is not stubbed \u2014 pass an implementation via createFakePluginHost({ sdk: { ... } }) or harness.sdk.stub("${path}", fn)`
3042
3063
  );
3043
3064
  }
3065
+ const track = options.trackThreadCreation;
3066
+ if (track !== void 0 && (path === "threads.spawn" || path === "threads.fork")) {
3067
+ return track(async () => stub(...args));
3068
+ }
3044
3069
  return stub(...args);
3045
3070
  }
3046
3071
  const nodes = /* @__PURE__ */ new Map();
@@ -3072,6 +3097,98 @@ function createFakeSdk(options) {
3072
3097
  return { sdk: node(""), harness };
3073
3098
  }
3074
3099
 
3100
+ // src/internal/thread-event-gate.ts
3101
+ function fromThread(thread) {
3102
+ return { threadId: thread.id, createdAt: thread.createdAt };
3103
+ }
3104
+ function fromThreadId(threadId) {
3105
+ return { threadId, createdAt: null };
3106
+ }
3107
+ var THREAD_OF_EVENT = {
3108
+ "experimental_environment.removed": () => null,
3109
+ "experimental_thread.events": ({ thread }) => fromThread(thread),
3110
+ "experimental_thread.parentChanged": ({ thread }) => fromThread(thread),
3111
+ "experimental_thread.read": ({ thread }) => fromThread(thread),
3112
+ "experimental_terminal.input": () => null,
3113
+ "experimental_host.deleted": () => null,
3114
+ "thread.created": ({ thread }) => fromThread(thread),
3115
+ "thread.active": ({ thread }) => fromThread(thread),
3116
+ "thread.idle": ({ thread }) => fromThread(thread),
3117
+ "thread.failed": ({ thread }) => fromThread(thread),
3118
+ "thread.archived": ({ thread }) => fromThread(thread),
3119
+ "thread.unarchived": ({ thread }) => fromThread(thread),
3120
+ "thread.deleted": ({ thread }) => fromThread(thread),
3121
+ "interaction.pending": ({ thread }) => fromThread(thread),
3122
+ "message.queued": ({ entry }) => fromThreadId(entry.threadId),
3123
+ "message.dispatched": ({ entry }) => fromThreadId(entry.threadId),
3124
+ "message.cancelled": ({ entry }) => fromThreadId(entry.threadId),
3125
+ "turn.failed": ({ threadId }) => fromThreadId(threadId),
3126
+ "experimental_thread.settled": ({ thread }) => fromThread(thread)
3127
+ };
3128
+ function threadOfEvent(event, payload) {
3129
+ const extract = THREAD_OF_EVENT[event];
3130
+ return extract(payload);
3131
+ }
3132
+ function createThreadEventGate(options = {}) {
3133
+ const now = options.now ?? Date.now;
3134
+ const schedule = options.schedule ?? ((run) => void setImmediate(run));
3135
+ const callsStartedAt = /* @__PURE__ */ new Map();
3136
+ let held = [];
3137
+ function createdDuringCall(createdAt) {
3138
+ if (createdAt === null) return false;
3139
+ for (const startedAt of callsStartedAt.values()) {
3140
+ if (startedAt <= createdAt) return true;
3141
+ }
3142
+ return false;
3143
+ }
3144
+ function release() {
3145
+ const pending = held;
3146
+ held = [];
3147
+ const stillHeld = /* @__PURE__ */ new Set();
3148
+ for (const entry of pending) {
3149
+ if (stillHeld.has(entry.threadId) || createdDuringCall(entry.createdAt)) {
3150
+ stillHeld.add(entry.threadId);
3151
+ held.push(entry);
3152
+ continue;
3153
+ }
3154
+ entry.deliver();
3155
+ }
3156
+ }
3157
+ return {
3158
+ async track(call) {
3159
+ const token = /* @__PURE__ */ Symbol("thread-creating-call");
3160
+ callsStartedAt.set(token, now());
3161
+ try {
3162
+ return await call();
3163
+ } finally {
3164
+ callsStartedAt.delete(token);
3165
+ schedule(release);
3166
+ }
3167
+ },
3168
+ deliver(subject, deliver) {
3169
+ if (subject === null) {
3170
+ deliver();
3171
+ return;
3172
+ }
3173
+ const threadId = subject.threadId;
3174
+ if (held.some((entry) => entry.threadId === threadId)) {
3175
+ held.push({ threadId, createdAt: null, deliver });
3176
+ return;
3177
+ }
3178
+ if (callsStartedAt.size === 0) {
3179
+ deliver();
3180
+ return;
3181
+ }
3182
+ const createdAt = subject.createdAt();
3183
+ if (!createdDuringCall(createdAt)) {
3184
+ deliver();
3185
+ return;
3186
+ }
3187
+ held.push({ threadId, createdAt, deliver });
3188
+ }
3189
+ };
3190
+ }
3191
+
3075
3192
  // src/testing/fake-plugin-host.ts
3076
3193
  var PluginContextStaleError = class extends Error {
3077
3194
  constructor(pluginId) {
@@ -3110,7 +3227,6 @@ function jsonRoundTrip(value, what) {
3110
3227
  function throwRpcError(error) {
3111
3228
  throw new experimental_PluginRpcCallError(error);
3112
3229
  }
3113
- var FAKE_TUNNEL_INGRESS_FIRST_PORT = 41e3;
3114
3230
  var fakeHostDisposers = /* @__PURE__ */ new WeakMap();
3115
3231
  function createFakePluginHost(options = {}) {
3116
3232
  return createFakePluginHostInternal(options);
@@ -3503,37 +3619,13 @@ function createFakePluginHostInternal(options, sharedState) {
3503
3619
  get experimental_dataDir() {
3504
3620
  assertLive();
3505
3621
  return dataDir;
3506
- },
3507
- async internal_DO_NOT_USE_openTunnelIngress(ingressOptions) {
3508
- assertLive();
3509
- const audience = ingressOptions?.audience;
3510
- if (typeof audience !== "string" || audience.length === 0) {
3511
- throw new Error(
3512
- "bb.server.internal_DO_NOT_USE_openTunnelIngress needs a non-empty audience"
3513
- );
3514
- }
3515
- const shared = tunnelIngresses.find(
3516
- (entry2) => entry2.audience === audience && !entry2.closed
3517
- );
3518
- const entry = {
3519
- audience,
3520
- baseUrl: shared?.baseUrl ?? `http://127.0.0.1:${FAKE_TUNNEL_INGRESS_FIRST_PORT + tunnelIngresses.length}`,
3521
- closed: false
3522
- };
3523
- tunnelIngresses.push(entry);
3524
- const close = async () => {
3525
- entry.closed = true;
3526
- const index = disposeHooks.indexOf(close);
3527
- if (index !== -1) disposeHooks.splice(index, 1);
3528
- };
3529
- disposeHooks.push(close);
3530
- return { baseUrl: entry.baseUrl, close };
3531
3622
  }
3532
3623
  };
3533
- const tunnelIngresses = [];
3624
+ const threadEventGate = createThreadEventGate();
3534
3625
  const { sdk, harness: sdkHarness } = createFakeSdk({
3535
3626
  pluginId,
3536
- overrides: options.sdk
3627
+ overrides: options.sdk,
3628
+ trackThreadCreation: (call) => threadEventGate.track(call)
3537
3629
  });
3538
3630
  const threadEventHandlers = {
3539
3631
  "experimental_thread.events": [],
@@ -3553,8 +3645,52 @@ function createFakePluginHostInternal(options, sharedState) {
3553
3645
  "message.dispatched": [],
3554
3646
  "turn.failed": [],
3555
3647
  "message.cancelled": [],
3648
+ "experimental_thread.settled": [],
3556
3649
  "thread.unarchived": []
3557
3650
  };
3651
+ function deliverThreadEvent(event, payload) {
3652
+ const thread = threadOfEvent(event, payload);
3653
+ return new Promise((resolve2) => {
3654
+ threadEventGate.deliver(
3655
+ thread === null ? null : { threadId: thread.threadId, createdAt: () => thread.createdAt },
3656
+ () => {
3657
+ void (async () => {
3658
+ const errors = [];
3659
+ for (const handler of [...threadEventHandlers[event]]) {
3660
+ try {
3661
+ await handler(payload);
3662
+ } catch (error) {
3663
+ errors.push(error);
3664
+ emitLog(
3665
+ "warn",
3666
+ `${event} handler failed: ${errorMessage(error)}`
3667
+ );
3668
+ }
3669
+ }
3670
+ resolve2({ errors });
3671
+ })();
3672
+ }
3673
+ );
3674
+ });
3675
+ }
3676
+ function settledEventFor(event, payload) {
3677
+ const delivered = {
3678
+ [event]: payload
3679
+ };
3680
+ if (delivered["thread.idle"] !== void 0) {
3681
+ return { outcome: "completed", ...delivered["thread.idle"] };
3682
+ }
3683
+ if (delivered["thread.failed"] !== void 0) {
3684
+ return { outcome: "failed", ...delivered["thread.failed"] };
3685
+ }
3686
+ if (delivered["thread.archived"] !== void 0) {
3687
+ return { outcome: "archived", ...delivered["thread.archived"] };
3688
+ }
3689
+ if (delivered["thread.deleted"] !== void 0) {
3690
+ return { outcome: "deleted", ...delivered["thread.deleted"] };
3691
+ }
3692
+ return null;
3693
+ }
3558
3694
  const hooks = {
3559
3695
  "message.dispatch": null
3560
3696
  };
@@ -3603,9 +3739,6 @@ function createFakePluginHostInternal(options, sharedState) {
3603
3739
  });
3604
3740
  }
3605
3741
  const sharedPortDeclarations = [];
3606
- let nextTunnelConfig = null;
3607
- let nextTunnelStatuses = [];
3608
- const nextTunnelListeners = /* @__PURE__ */ new Set();
3609
3742
  const hostRpcCalls = [];
3610
3743
  const hostWorkerExitSubscriptions = [];
3611
3744
  const hostSignalSubscriptions = [];
@@ -3737,27 +3870,10 @@ function createFakePluginHostInternal(options, sharedState) {
3737
3870
  } else {
3738
3871
  sharedPortDeclarations[existingIndex] = replacement;
3739
3872
  }
3740
- },
3741
- internal_DO_NOT_USE_setNextTunnels(config) {
3742
- assertLive();
3743
- nextTunnelConfig = config === null ? null : { domain: config.domain, server: { ...config.server } };
3744
- },
3745
- internal_DO_NOT_USE_nextTunnelStatuses() {
3746
- assertLive();
3747
- return structuredClone(nextTunnelStatuses);
3748
- },
3749
- internal_DO_NOT_USE_onNextTunnelStatusChange(listener) {
3750
- assertLive();
3751
- nextTunnelListeners.add(listener);
3752
- return () => {
3753
- nextTunnelListeners.delete(listener);
3754
- };
3755
3873
  }
3756
3874
  };
3757
3875
  disposeHooks.push(() => {
3758
3876
  sharedPortDeclarations.length = 0;
3759
- nextTunnelConfig = null;
3760
- nextTunnelListeners.clear();
3761
3877
  });
3762
3878
  const events = {
3763
3879
  on(event, handler) {
@@ -3983,15 +4099,7 @@ function createFakePluginHostInternal(options, sharedState) {
3983
4099
  get recheckCount() {
3984
4100
  return requestedDrains;
3985
4101
  },
3986
- internal_DO_NOT_USE_tunnelIngresses: tunnelIngresses,
3987
4102
  sharedPortDeclarations,
3988
- get internal_DO_NOT_USE_nextTunnelConfig() {
3989
- return nextTunnelConfig;
3990
- },
3991
- internal_DO_NOT_USE_setNextTunnelStatuses(statuses) {
3992
- nextTunnelStatuses = structuredClone([...statuses]);
3993
- for (const listener of [...nextTunnelListeners]) listener();
3994
- },
3995
4103
  experimental_hostRpcCalls: hostRpcCalls,
3996
4104
  sdk: sdkHarness,
3997
4105
  registrations: {
@@ -4037,6 +4145,7 @@ function createFakePluginHostInternal(options, sharedState) {
4037
4145
  "message.dispatched": threadEventHandlers["message.dispatched"].length,
4038
4146
  "turn.failed": threadEventHandlers["turn.failed"].length,
4039
4147
  "message.cancelled": threadEventHandlers["message.cancelled"].length,
4148
+ "experimental_thread.settled": threadEventHandlers["experimental_thread.settled"].length,
4040
4149
  "thread.unarchived": threadEventHandlers["thread.unarchived"].length
4041
4150
  };
4042
4151
  },
@@ -4429,14 +4538,12 @@ function createFakePluginHostInternal(options, sharedState) {
4429
4538
  await schedule.fn();
4430
4539
  },
4431
4540
  async emitThreadEvent(event, payload) {
4432
- const errors = [];
4433
- for (const handler of [...threadEventHandlers[event]]) {
4434
- try {
4435
- await handler(payload);
4436
- } catch (error) {
4437
- errors.push(error);
4438
- emitLog("warn", `${event} handler failed: ${errorMessage(error)}`);
4439
- }
4541
+ const { errors } = await deliverThreadEvent(event, payload);
4542
+ const settled = settledEventFor(event, payload);
4543
+ if (settled !== null) {
4544
+ errors.push(
4545
+ ...(await deliverThreadEvent("experimental_thread.settled", settled)).errors
4546
+ );
4440
4547
  }
4441
4548
  return { errors };
4442
4549
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@get-bb/plugin-sdk",
3
- "version": "0.6.157",
3
+ "version": "0.6.169",
4
4
  "license": "MIT",
5
5
  "homepage": "https://github.com/get-bb/bb#readme",
6
6
  "bugs": {
@@ -99,6 +99,12 @@
99
99
  "import": "./dist/internal/thread-status.js",
100
100
  "default": "./dist/internal/thread-status.js"
101
101
  },
102
+ "./internal/thread-event-gate": {
103
+ "source": "./src/internal/thread-event-gate.ts",
104
+ "types": "./bundled-types/bb-plugin-sdk-internal-thread-event-gate.d.ts",
105
+ "import": "./dist/internal/thread-event-gate.js",
106
+ "default": "./dist/internal/thread-event-gate.js"
107
+ },
102
108
  "./internal/file-navigation-validation": {
103
109
  "source": "./src/internal/file-navigation-validation.ts",
104
110
  "types": "./bundled-types/bb-plugin-sdk-internal-file-navigation-validation.d.ts",
@@ -180,6 +186,7 @@
180
186
  "@bb/server-contract": "workspace:*",
181
187
  "@bb/skill-index": "workspace:*",
182
188
  "@bb/test-helpers": "workspace:*",
189
+ "@bb/text-utils": "workspace:*",
183
190
  "@bb/tsconfig": "workspace:*",
184
191
  "@testing-library/react": "^16.3.3",
185
192
  "@types/better-sqlite3": "^7.6.13",