@get-bb/plugin-sdk 0.4.108 → 0.5.8

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.
@@ -4224,6 +4224,23 @@ function mimeTypeFromExtension(filePath) {
4224
4224
  }
4225
4225
  }
4226
4226
 
4227
+ // ../text-utils/src/utf16.ts
4228
+ function isHighSurrogateAt(value, index) {
4229
+ const code = value.charCodeAt(index);
4230
+ return code >= 55296 && code <= 56319;
4231
+ }
4232
+ function isLowSurrogateAt(value, index) {
4233
+ const code = value.charCodeAt(index);
4234
+ return code >= 56320 && code <= 57343;
4235
+ }
4236
+ function sliceUtf16Head(value, maxChars) {
4237
+ let end = Math.min(value.length, Math.max(0, maxChars));
4238
+ if (isHighSurrogateAt(value, end - 1) && isLowSurrogateAt(value, end)) {
4239
+ end -= 1;
4240
+ }
4241
+ return value.slice(0, end);
4242
+ }
4243
+
4227
4244
  // ../provider-bridge-protocol/src/bridge-kit/presentation.ts
4228
4245
  var PRESENTATION_TITLE_MAX_LENGTH = 160;
4229
4246
  function presentationTitle(text) {
@@ -4231,7 +4248,7 @@ function presentationTitle(text) {
4231
4248
  if (firstLine.length === 0) {
4232
4249
  return void 0;
4233
4250
  }
4234
- return firstLine.length > PRESENTATION_TITLE_MAX_LENGTH ? `${firstLine.slice(0, PRESENTATION_TITLE_MAX_LENGTH - 1)}\u2026` : firstLine;
4251
+ return firstLine.length > PRESENTATION_TITLE_MAX_LENGTH ? `${sliceUtf16Head(firstLine, PRESENTATION_TITLE_MAX_LENGTH - 1)}\u2026` : firstLine;
4235
4252
  }
4236
4253
  function withTitle(presentation, title) {
4237
4254
  return title === void 0 ? presentation : { ...presentation, title };
@@ -5764,6 +5781,55 @@ var GROK_ACP_DIALECT = {
5764
5781
  toolIdentity: grokToolIdentity,
5765
5782
  classifyToolCall: grokClassifyToolCall
5766
5783
  };
5784
+ var grokModelMetaSchema = z40.object({
5785
+ totalContextTokens: z40.number().int().positive()
5786
+ }).passthrough();
5787
+ var grokPromptMetaSchema = z40.object({
5788
+ inputTokens: z40.number().int().nonnegative().optional(),
5789
+ totalTokens: z40.number().int().nonnegative().optional(),
5790
+ usage: z40.object({
5791
+ inputTokens: z40.number().int().nonnegative().optional(),
5792
+ totalTokens: z40.number().int().nonnegative().optional()
5793
+ }).passthrough().optional()
5794
+ }).passthrough();
5795
+ var grokPromptResultSchema = z40.object({
5796
+ _meta: grokPromptMetaSchema.optional()
5797
+ }).passthrough();
5798
+ var grokSessionModelSchema = z40.object({
5799
+ modelId: z40.string(),
5800
+ _meta: grokModelMetaSchema.optional()
5801
+ }).passthrough();
5802
+ var grokSessionModelsSchema = z40.object({
5803
+ currentModelId: z40.string().optional(),
5804
+ availableModels: z40.array(grokSessionModelSchema).optional()
5805
+ }).passthrough();
5806
+ function grokContextWindowSizeFromSessionModels(models) {
5807
+ const parsed = grokSessionModelsSchema.safeParse(models);
5808
+ if (!parsed.success) {
5809
+ return void 0;
5810
+ }
5811
+ const available = parsed.data.availableModels ?? [];
5812
+ const currentId = parsed.data.currentModelId;
5813
+ const ordered = [
5814
+ ...currentId === void 0 ? [] : available.filter((model) => model.modelId === currentId),
5815
+ ...available
5816
+ ];
5817
+ for (const model of ordered) {
5818
+ if (model._meta !== void 0) {
5819
+ return model._meta.totalContextTokens;
5820
+ }
5821
+ }
5822
+ return void 0;
5823
+ }
5824
+ function grokContextUsageFromPromptResult(result) {
5825
+ const parsed = grokPromptResultSchema.safeParse(result);
5826
+ if (!parsed.success) {
5827
+ return void 0;
5828
+ }
5829
+ const meta = parsed.data._meta;
5830
+ const used = meta?.usage?.inputTokens ?? meta?.inputTokens ?? meta?.usage?.totalTokens ?? meta?.totalTokens;
5831
+ return used === void 0 ? void 0 : { used };
5832
+ }
5767
5833
  var CURSOR_TASK_TOOL = "task";
5768
5834
  var cursorTaskRawInputSchema = z40.object({ _toolName: z40.string().optional() }).passthrough();
5769
5835
  var CURSOR_TASK_METHOD = "cursor/task";
@@ -8334,6 +8400,29 @@ function sendThreadDeltas(threadId, deltas) {
8334
8400
  deltas: [...deltas]
8335
8401
  });
8336
8402
  }
8403
+ function rememberGrokContextWindow(session, models) {
8404
+ if (session.dialect.id !== "grok") {
8405
+ return;
8406
+ }
8407
+ const size = grokContextWindowSizeFromSessionModels(models);
8408
+ if (size !== void 0) {
8409
+ session.grokContextWindowSize = size;
8410
+ }
8411
+ }
8412
+ function emitGrokContextWindow(session, used) {
8413
+ if (session.dialect.id !== "grok" || session.grokContextWindowSize === void 0) {
8414
+ return;
8415
+ }
8416
+ sendThreadDeltas(session.bbThreadId, [
8417
+ {
8418
+ kind: "contextWindow",
8419
+ used,
8420
+ size: session.grokContextWindowSize,
8421
+ estimated: false,
8422
+ attach: "open"
8423
+ }
8424
+ ]);
8425
+ }
8337
8426
  function emitForSession(session, method, params) {
8338
8427
  sendThreadDeltas(
8339
8428
  session.bbThreadId,
@@ -9403,6 +9492,7 @@ async function startAgentSession(request) {
9403
9492
  loading: false,
9404
9493
  loadingSessionId: void 0,
9405
9494
  pendingLoadUsageUpdate: void 0,
9495
+ grokContextWindowSize: void 0,
9406
9496
  stopping: false,
9407
9497
  turnSettled: void 0,
9408
9498
  pendingPermissions: /* @__PURE__ */ new Set(),
@@ -9449,6 +9539,7 @@ async function startAgentSession(request) {
9449
9539
  let sessionId;
9450
9540
  let loadedConfigOptions;
9451
9541
  let loadedModels;
9542
+ let createdFreshSession = false;
9452
9543
  if (request.kind === "fork") {
9453
9544
  const forkedSession = await connection.request({
9454
9545
  method: "session/fork",
@@ -9467,6 +9558,7 @@ async function startAgentSession(request) {
9467
9558
  sessionId = forkedSession.sessionId;
9468
9559
  loadedConfigOptions = forkedSession.configOptions;
9469
9560
  loadedModels = forkedSession.models;
9561
+ rememberGrokContextWindow(session, forkedSession.models);
9470
9562
  } else if (request.kind === "resume" && supportsLoadSession) {
9471
9563
  session.loading = true;
9472
9564
  session.loadingSessionId = request.resumeProviderThreadId;
@@ -9483,6 +9575,7 @@ async function startAgentSession(request) {
9483
9575
  });
9484
9576
  loadedConfigOptions = configState?.configOptions;
9485
9577
  loadedModels = configState?.models;
9578
+ rememberGrokContextWindow(session, configState?.models);
9486
9579
  sessionId = request.resumeProviderThreadId;
9487
9580
  } catch {
9488
9581
  sessionId = void 0;
@@ -9501,6 +9594,8 @@ async function startAgentSession(request) {
9501
9594
  resultSchema: acpSessionNewResultSchema
9502
9595
  });
9503
9596
  sessionId = newSession.sessionId;
9597
+ createdFreshSession = true;
9598
+ rememberGrokContextWindow(session, newSession.models);
9504
9599
  await selectAcpNativeModel({
9505
9600
  connection,
9506
9601
  sessionId,
@@ -9548,6 +9643,9 @@ async function startAgentSession(request) {
9548
9643
  sessionRestorable: session.supportsLoadSession
9549
9644
  });
9550
9645
  sendThreadDeltas(bbThreadId, [{ kind: "session.reset" }]);
9646
+ if (createdFreshSession) {
9647
+ emitGrokContextWindow(session, 0);
9648
+ }
9551
9649
  session.deferStartEmit = void 0;
9552
9650
  for (const deferred of deferredEmits) {
9553
9651
  if (deferred.sessionId !== void 0 && deferred.sessionId !== sessionId) {
@@ -9700,6 +9798,10 @@ function runTurn(session, firstInput) {
9700
9798
  }
9701
9799
  const result = await promptResult;
9702
9800
  stopReason = result.stopReason;
9801
+ const grokUsage = grokContextUsageFromPromptResult(result);
9802
+ if (grokUsage !== void 0) {
9803
+ emitGrokContextWindow(session, grokUsage.used);
9804
+ }
9703
9805
  } catch (error) {
9704
9806
  session.promptRequestPending = false;
9705
9807
  dropTurnInput(pending, "ACP turn failed before the prompt was sent");
@@ -9749,6 +9851,10 @@ function startCompaction(session, pending) {
9749
9851
  });
9750
9852
  acceptTurnInput(session, pending);
9751
9853
  session.turnSettled = promptResult.then((result) => {
9854
+ const grokUsage = grokContextUsageFromPromptResult(result);
9855
+ if (grokUsage !== void 0) {
9856
+ emitGrokContextWindow(session, grokUsage.used);
9857
+ }
9752
9858
  finish(
9753
9859
  result.stopReason === "end_turn" ? compactionOutcomeForEndTurn(
9754
9860
  session.dialect,
@@ -4282,6 +4282,23 @@ function shouldAutoDenyInteractiveRequest(policy) {
4282
4282
  return policy.permissionEscalation === "deny";
4283
4283
  }
4284
4284
 
4285
+ // ../text-utils/src/utf16.ts
4286
+ function isHighSurrogateAt(value, index) {
4287
+ const code = value.charCodeAt(index);
4288
+ return code >= 55296 && code <= 56319;
4289
+ }
4290
+ function isLowSurrogateAt(value, index) {
4291
+ const code = value.charCodeAt(index);
4292
+ return code >= 56320 && code <= 57343;
4293
+ }
4294
+ function sliceUtf16Head(value, maxChars) {
4295
+ let end = Math.min(value.length, Math.max(0, maxChars));
4296
+ if (isHighSurrogateAt(value, end - 1) && isLowSurrogateAt(value, end)) {
4297
+ end -= 1;
4298
+ }
4299
+ return value.slice(0, end);
4300
+ }
4301
+
4285
4302
  // ../provider-bridge-protocol/src/bridge-kit/presentation.ts
4286
4303
  var PRESENTATION_TITLE_MAX_LENGTH = 160;
4287
4304
  function presentationTitle(text) {
@@ -4289,10 +4306,10 @@ function presentationTitle(text) {
4289
4306
  if (firstLine2.length === 0) {
4290
4307
  return void 0;
4291
4308
  }
4292
- return firstLine2.length > PRESENTATION_TITLE_MAX_LENGTH ? `${firstLine2.slice(0, PRESENTATION_TITLE_MAX_LENGTH - 1)}\u2026` : firstLine2;
4309
+ return firstLine2.length > PRESENTATION_TITLE_MAX_LENGTH ? `${sliceUtf16Head(firstLine2, PRESENTATION_TITLE_MAX_LENGTH - 1)}\u2026` : firstLine2;
4293
4310
  }
4294
4311
  function presentationDetail(text) {
4295
- return text.length > THREAD_EVENT_ITEM_PRESENTATION_DETAIL_MAX_LENGTH ? `${text.slice(0, THREAD_EVENT_ITEM_PRESENTATION_DETAIL_MAX_LENGTH - 1)}\u2026` : text;
4312
+ return text.length > THREAD_EVENT_ITEM_PRESENTATION_DETAIL_MAX_LENGTH ? `${sliceUtf16Head(text, THREAD_EVENT_ITEM_PRESENTATION_DETAIL_MAX_LENGTH - 1)}\u2026` : text;
4296
4313
  }
4297
4314
  function withTitle(presentation, title) {
4298
4315
  return title === void 0 ? presentation : { ...presentation, title };