@get-bb/plugin-sdk 0.6.150 → 0.6.164

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.
@@ -81,7 +81,9 @@ function parseArgs(argv) {
81
81
  }
82
82
  }
83
83
  if (!args.recording || !args.dialect || !args.state) {
84
- throw new Error("usage: --recording <dir> --dialect <json-rpc|claude-cli|pi-rpc> --state <dir>");
84
+ throw new Error(
85
+ "usage: --recording <dir> --dialect <json-rpc|claude-cli|pi-rpc> --state <dir>",
86
+ );
85
87
  }
86
88
  return args;
87
89
  }
@@ -93,7 +95,10 @@ function readLane(dir, direction) {
93
95
  .split("\n")
94
96
  .filter((line) => line.length > 0)
95
97
  .map((line) => JSON.parse(line))
96
- .map((entry) => ({ ...entry, run: typeof entry.run === "number" ? entry.run : 0 }));
98
+ .map((entry) => ({
99
+ ...entry,
100
+ run: typeof entry.run === "number" ? entry.run : 0,
101
+ }));
97
102
  }
98
103
 
99
104
  // ---------------------------------------------------------------------------
@@ -103,7 +108,8 @@ function readLane(dir, direction) {
103
108
  const DIALECTS = {
104
109
  "json-rpc": {
105
110
  classify(message) {
106
- const hasId = typeof message.id === "string" || typeof message.id === "number";
111
+ const hasId =
112
+ typeof message.id === "string" || typeof message.id === "number";
107
113
  if (hasId && typeof message.method === "string") {
108
114
  return { kind: "request", id: message.id, key: message.method };
109
115
  }
@@ -135,7 +141,11 @@ const DIALECTS = {
135
141
  };
136
142
  }
137
143
  if (message.type === "control_response") {
138
- return { kind: "response", id: message.response?.request_id, key: "control_response" };
144
+ return {
145
+ kind: "response",
146
+ id: message.response?.request_id,
147
+ key: "control_response",
148
+ };
139
149
  }
140
150
  return {
141
151
  kind: "notification",
@@ -143,7 +153,10 @@ const DIALECTS = {
143
153
  };
144
154
  },
145
155
  isInitialize(classified) {
146
- return classified.kind === "request" && classified.key === "control_request:initialize";
156
+ return (
157
+ classified.kind === "request" &&
158
+ classified.key === "control_request:initialize"
159
+ );
147
160
  },
148
161
  withResponseId(message, id) {
149
162
  return { ...message, response: { ...message.response, request_id: id } };
@@ -183,9 +196,18 @@ const DIALECTS = {
183
196
  };
184
197
  }
185
198
  if (channel.kind === "tool-result" || channel.kind === "reply") {
186
- return { kind: "response", id: channel.id, key: "channel:response", channel: true };
199
+ return {
200
+ kind: "response",
201
+ id: channel.id,
202
+ key: "channel:response",
203
+ channel: true,
204
+ };
187
205
  }
188
- return { kind: "notification", key: `channel:${channel.kind ?? "?"}`, channel: true };
206
+ return {
207
+ kind: "notification",
208
+ key: `channel:${channel.kind ?? "?"}`,
209
+ channel: true,
210
+ };
189
211
  }
190
212
  if (message.type === "response") {
191
213
  return { kind: "response", id: message.id, key: "response" };
@@ -239,8 +261,14 @@ function buildSegments(entries, dialect) {
239
261
  let current = null;
240
262
  for (const entry of entries) {
241
263
  const message = parseLine(entry.line);
242
- const classified = message === null ? { kind: "raw", key: "raw" } : dialect.classify(message);
243
- const startsSegment = entry.dir === "bridge→provider" && message !== null && dialect.isInitialize(classified);
264
+ const classified =
265
+ message === null
266
+ ? { kind: "raw", key: "raw" }
267
+ : dialect.classify(message);
268
+ const startsSegment =
269
+ entry.dir === "bridge→provider" &&
270
+ message !== null &&
271
+ dialect.isInitialize(classified);
244
272
  if (current === null || startsSegment) {
245
273
  current = [];
246
274
  segments.push(current);
@@ -294,7 +322,10 @@ function readCursor(stateDir) {
294
322
  function cursorAllows(cursor, entry) {
295
323
  if (cursor === null) return true;
296
324
  if (cursor === false) return false;
297
- return entry.run < cursor.run || (entry.run === cursor.run && entry.seq < cursor.seq);
325
+ return (
326
+ entry.run < cursor.run ||
327
+ (entry.run === cursor.run && entry.seq < cursor.seq)
328
+ );
298
329
  }
299
330
 
300
331
  function firstSessionDefiningKey(script) {
@@ -365,7 +396,7 @@ function connectChannel(channel) {
365
396
  // Player
366
397
  // ---------------------------------------------------------------------------
367
398
 
368
- function servePiCatalog(entries) {
399
+ function recordedPiModels(entries) {
369
400
  const models = new Map();
370
401
  let state = {};
371
402
  for (const entry of entries) {
@@ -389,6 +420,17 @@ function servePiCatalog(entries) {
389
420
  }
390
421
  }
391
422
  }
423
+ return { models, state };
424
+ }
425
+
426
+ const PI_MODEL_SELECTION_KEYS = new Set([
427
+ "get_available_models",
428
+ "set_model",
429
+ "set_thinking_level",
430
+ ]);
431
+
432
+ function servePiCatalog(entries) {
433
+ const { models, state } = recordedPiModels(entries);
392
434
  const channel = connectChannel(DIALECTS["pi-rpc"].channel);
393
435
  readNewlineDelimitedLines(channel, (line) => {
394
436
  const message = parseLine(line);
@@ -465,9 +507,12 @@ function main() {
465
507
  process.argv.includes("--no-session-persistence");
466
508
  const segmentIndex = diagnosticFork ? null : claimSegmentIndex(args.state);
467
509
  let script = segmentIndex === null ? [] : (segments[segmentIndex] ?? []);
468
- const log = (text) => process.stderr.write(`[replay-child #${segmentIndex}] ${text}\n`);
510
+ const log = (text) =>
511
+ process.stderr.write(`[replay-child #${segmentIndex}] ${text}\n`);
469
512
  if (script.length === 0) {
470
- log(`no recorded segment ${segmentIndex} (recording has ${segments.length}); answering generically`);
513
+ log(
514
+ `no recorded segment ${segmentIndex} (recording has ${segments.length}); answering generically`,
515
+ );
471
516
  }
472
517
  const segmentSessionKey = firstSessionDefiningKey(script);
473
518
  let sawSessionDefiningRequest = false;
@@ -486,11 +531,18 @@ function main() {
486
531
  let piState = null;
487
532
  let piStreaming = false;
488
533
  let piPromptProbePending = false;
534
+ let piSelectionProbePending = false;
535
+ const piModels =
536
+ args.dialect === "pi-rpc" ? recordedPiModels(entries).models : new Map();
489
537
 
490
538
  const channel = dialect.channel ?? null;
491
539
  const channelSocket = channel ? connectChannel(channel) : null;
492
540
  function emit(message) {
493
- if (channel && typeof message[channel.key] === "object" && message[channel.key] !== null) {
541
+ if (
542
+ channel &&
543
+ typeof message[channel.key] === "object" &&
544
+ message[channel.key] !== null
545
+ ) {
494
546
  channelSocket.write(`${JSON.stringify(message[channel.key])}\n`);
495
547
  return;
496
548
  }
@@ -517,7 +569,12 @@ function main() {
517
569
  }
518
570
  if (classified.kind === "response") {
519
571
  const liveId = liveIdByRecordedId.get(String(classified.id));
520
- emit(dialect.withResponseId(message, liveId === undefined ? classified.id : liveId));
572
+ emit(
573
+ dialect.withResponseId(
574
+ message,
575
+ liveId === undefined ? classified.id : liveId,
576
+ ),
577
+ );
521
578
  return;
522
579
  }
523
580
  if (
@@ -528,7 +585,10 @@ function main() {
528
585
  ) {
529
586
  emit({
530
587
  ...message,
531
- request: { ...message.request, callback_id: hookIds.get(message.request.callback_id) },
588
+ request: {
589
+ ...message.request,
590
+ callback_id: hookIds.get(message.request.callback_id),
591
+ },
532
592
  });
533
593
  return;
534
594
  }
@@ -635,7 +695,10 @@ function main() {
635
695
  }
636
696
  if (step.classified.kind === "request") {
637
697
  liveIdByRecordedId.set(String(step.classified.id), live.classified.id);
638
- if (dialect.isInitialize(step.classified) && args.dialect === "claude-cli") {
698
+ if (
699
+ dialect.isInitialize(step.classified) &&
700
+ args.dialect === "claude-cli"
701
+ ) {
639
702
  hookIds = hookCallbackIdMap(step.message, live.message);
640
703
  }
641
704
  }
@@ -666,11 +729,14 @@ function main() {
666
729
  : step.classified.key === live.classified.key);
667
730
  if (!same) continue;
668
731
  const cursor = readCursor(args.state);
669
- const gated = script.slice(position, index).some((between) =>
670
- between.dir === "provider→bridge" &&
671
- between.classified.kind !== "response" &&
672
- !cursorAllows(cursor, between),
673
- );
732
+ const gated = script
733
+ .slice(position, index)
734
+ .some(
735
+ (between) =>
736
+ between.dir === "provider→bridge" &&
737
+ between.classified.kind !== "response" &&
738
+ !cursorAllows(cursor, between),
739
+ );
674
740
  if (gated) {
675
741
  lookaheadTimer = setTimeout(lookAhead, CURSOR_POLL_MS);
676
742
  return;
@@ -692,7 +758,9 @@ function main() {
692
758
  emitRecorded(between);
693
759
  }
694
760
  }
695
- log(`bridge skipped recorded ${skipped.join(", ")}; resuming at ${live.classified.key}`);
761
+ log(
762
+ `bridge skipped recorded ${skipped.join(", ")}; resuming at ${live.classified.key}`,
763
+ );
696
764
  position = index;
697
765
  advance();
698
766
  armStall();
@@ -703,10 +771,14 @@ function main() {
703
771
 
704
772
  function answerGenerically(live, reason) {
705
773
  if (live.classified.kind === "request") {
706
- log(`${reason}: answering ${live.classified.key} (${String(live.classified.id)}) generically`);
774
+ log(
775
+ `${reason}: answering ${live.classified.key} (${String(live.classified.id)}) generically`,
776
+ );
707
777
  emit(dialect.genericResponse(live.classified.id, live.classified));
708
778
  } else {
709
- log(`${reason}: dropping unmatched ${live.classified.kind} ${live.classified.key}`);
779
+ log(
780
+ `${reason}: dropping unmatched ${live.classified.kind} ${live.classified.key}`,
781
+ );
710
782
  }
711
783
  }
712
784
 
@@ -716,7 +788,9 @@ function main() {
716
788
  const expected = script[position];
717
789
  log(
718
790
  `stalled for ${STALL_MS}ms at step ${position}/${script.length}` +
719
- (expected ? ` (expecting ${expected.dir} ${expected.classified.key})` : ""),
791
+ (expected
792
+ ? ` (expecting ${expected.dir} ${expected.classified.key})`
793
+ : ""),
720
794
  );
721
795
  for (const live of pendingLive.splice(0)) {
722
796
  answerGenerically(live, "stall");
@@ -728,7 +802,8 @@ function main() {
728
802
  if (stallTimer !== null) clearTimeout(stallTimer);
729
803
  if (lookaheadTimer !== null) clearTimeout(lookaheadTimer);
730
804
  stallTimer = pendingLive.length > 0 ? setTimeout(onStall, STALL_MS) : null;
731
- lookaheadTimer = pendingLive.length > 0 ? setTimeout(lookAhead, LOOKAHEAD_MS) : null;
805
+ lookaheadTimer =
806
+ pendingLive.length > 0 ? setTimeout(lookAhead, LOOKAHEAD_MS) : null;
732
807
  }
733
808
 
734
809
  /**
@@ -747,6 +822,46 @@ function main() {
747
822
  }
748
823
  }
749
824
 
825
+ function nextBridgeExpectationKey() {
826
+ return script.slice(position).find((step) => step.dir === "bridge→provider")
827
+ ?.classified.key;
828
+ }
829
+
830
+ function answerPiModelSelection(live) {
831
+ const { id, key } = live.classified;
832
+ const request = live.message;
833
+ if (key === "get_available_models") {
834
+ emit({
835
+ id,
836
+ type: "response",
837
+ command: key,
838
+ success: true,
839
+ data: { models: [...piModels.values()] },
840
+ });
841
+ return;
842
+ }
843
+ if (key === "set_model") {
844
+ const model = piModels.get(`${request.provider}/${request.modelId}`);
845
+ if (model === undefined) {
846
+ emit({
847
+ id,
848
+ type: "response",
849
+ command: key,
850
+ success: false,
851
+ error: `Model not found: ${request.provider}/${request.modelId}`,
852
+ });
853
+ return;
854
+ }
855
+ piState = { ...(piState ?? {}), model };
856
+ piSelectionProbePending = true;
857
+ emit({ id, type: "response", command: key, success: true, data: model });
858
+ return;
859
+ }
860
+ piState = { ...(piState ?? {}), thinkingLevel: request.level };
861
+ piSelectionProbePending = true;
862
+ emit({ id, type: "response", command: key, success: true });
863
+ }
864
+
750
865
  function onLiveMessage(message) {
751
866
  const live = { message, classified: dialect.classify(message) };
752
867
  if (
@@ -755,20 +870,32 @@ function main() {
755
870
  SESSION_DEFINING_KEY.test(live.classified.key)
756
871
  ) {
757
872
  sawSessionDefiningRequest = true;
758
- if (segmentSessionKey !== null && live.classified.key !== segmentSessionKey) {
873
+ if (
874
+ segmentSessionKey !== null &&
875
+ live.classified.key !== segmentSessionKey
876
+ ) {
759
877
  releaseSegment(live);
760
878
  }
761
879
  }
880
+ if (
881
+ args.dialect === "pi-rpc" &&
882
+ live.classified.kind === "request" &&
883
+ PI_MODEL_SELECTION_KEYS.has(live.classified.key) &&
884
+ nextBridgeExpectationKey() !== live.classified.key
885
+ ) {
886
+ answerPiModelSelection(live);
887
+ return;
888
+ }
762
889
  if (
763
890
  args.dialect === "pi-rpc" &&
764
891
  live.classified.kind === "request" &&
765
892
  live.classified.key === "get_state" &&
766
- piPromptProbePending &&
893
+ (piPromptProbePending || piSelectionProbePending) &&
767
894
  piState !== null &&
768
- script.slice(position).find((step) => step.dir === "bridge→provider")
769
- ?.classified.key !== "get_state"
895
+ nextBridgeExpectationKey() !== "get_state"
770
896
  ) {
771
897
  piPromptProbePending = false;
898
+ piSelectionProbePending = false;
772
899
  emit({
773
900
  id: live.classified.id,
774
901
  type: "response",