@theokit/sdk 2.15.1 → 2.15.2

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.
package/dist/a2a/index.js CHANGED
@@ -10362,12 +10362,83 @@ function parseHermesParams(inner) {
10362
10362
  }
10363
10363
  return sanitizeToolInput(input, { trim: true }).value;
10364
10364
  }
10365
- var HERMES_BLOCK, HERMES_PARAM;
10365
+ function streamToolCallBufferState(held, allowedToolNames, cap = DEFAULT_STREAM_BUFFER_CAP) {
10366
+ if (allowedToolNames.size === 0) return "impossible";
10367
+ const t = held.trimStart();
10368
+ if (t.length < STREAM_MARKER.length) {
10369
+ return STREAM_MARKER.startsWith(t) ? "possible" : "impossible";
10370
+ }
10371
+ if (!t.startsWith(STREAM_MARKER)) return "impossible";
10372
+ const parsed = parseStreamMarkerName(t);
10373
+ if (parsed === "building") return "possible";
10374
+ if (parsed === "invalid") return "impossible";
10375
+ const nameOk = parsed.complete ? allowedToolNames.has(parsed.name) : someToolNameStartsWith(allowedToolNames, parsed.name);
10376
+ if (!nameOk) return "impossible";
10377
+ return held.length > cap ? "impossible" : "possible";
10378
+ }
10379
+ function parseStreamMarkerName(t) {
10380
+ let cursor = STREAM_MARKER.length;
10381
+ while (cursor < t.length && isStreamWs(t[cursor])) cursor += 1;
10382
+ const nameStart = cursor;
10383
+ while (cursor < t.length && t[cursor] !== ">" && !isStreamWs(t[cursor])) cursor += 1;
10384
+ const name = t.slice(nameStart, cursor);
10385
+ if (name.length === 0) return cursor >= t.length ? "building" : "invalid";
10386
+ return { name, complete: cursor < t.length && t[cursor] === ">" };
10387
+ }
10388
+ function someToolNameStartsWith(allowedToolNames, prefix) {
10389
+ for (const name of allowedToolNames) {
10390
+ if (name.startsWith(prefix)) return true;
10391
+ }
10392
+ return false;
10393
+ }
10394
+ function firstPossibleMarkerStart(held, allowedToolNames) {
10395
+ for (let i = held.indexOf("<"); i !== -1; i = held.indexOf("<", i + 1)) {
10396
+ if (streamToolCallBufferState(held.slice(i), allowedToolNames) === "possible") return i;
10397
+ }
10398
+ return -1;
10399
+ }
10400
+ var HERMES_BLOCK, HERMES_PARAM, STREAM_MARKER, DEFAULT_STREAM_BUFFER_CAP, isStreamWs, StreamSuppressionBuffer;
10366
10401
  var init_hermes_tool_extract = __esm({
10367
10402
  "src/internal/llm/hermes-tool-extract.ts"() {
10368
10403
  init_sanitize_tool_input();
10369
10404
  HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
10370
10405
  HERMES_PARAM = /<parameter=\s*([^>\s]+)\s*>([\s\S]*?)<\/parameter>/g;
10406
+ STREAM_MARKER = "<function=";
10407
+ DEFAULT_STREAM_BUFFER_CAP = 8192;
10408
+ isStreamWs = (c) => c === " " || c === " " || c === "\n" || c === "\r";
10409
+ StreamSuppressionBuffer = class {
10410
+ constructor(allowedToolNames) {
10411
+ this.allowedToolNames = allowedToolNames;
10412
+ }
10413
+ allowedToolNames;
10414
+ #held = "";
10415
+ /** Feed a content delta; returns the text to emit as a `text_delta` now, or `undefined` to hold. */
10416
+ push(content) {
10417
+ this.#held += content;
10418
+ if (streamToolCallBufferState(this.#held, this.allowedToolNames) === "possible")
10419
+ return void 0;
10420
+ const holdStart = firstPossibleMarkerStart(this.#held, this.allowedToolNames);
10421
+ if (holdStart > 0) {
10422
+ const flush2 = this.#held.slice(0, holdStart);
10423
+ this.#held = this.#held.slice(holdStart);
10424
+ return flush2;
10425
+ }
10426
+ const flush = this.#held;
10427
+ this.#held = "";
10428
+ return flush;
10429
+ }
10430
+ /** Drain the held buffer at stream end. `hasNativeCalls` mirrors `finish()`'s size-guard: when
10431
+ * native `tool_calls` exist, `finish()` won't strip the leaked block, so stream the held text WHOLE
10432
+ * (keeping `accumulatedText == finish.text`); otherwise strip the recoverable blocks. Idempotent. */
10433
+ drain(hasNativeCalls) {
10434
+ if (this.#held.length === 0) return void 0;
10435
+ const held = this.#held;
10436
+ this.#held = "";
10437
+ if (hasNativeCalls) return held;
10438
+ const residual = extractHermesToolCalls(held, () => "held", this.allowedToolNames).residualText;
10439
+ return residual.length > 0 ? residual : void 0;
10440
+ }
10441
+ };
10371
10442
  }
10372
10443
  });
10373
10444
 
@@ -10581,6 +10652,8 @@ var init_openai2 = __esm({
10581
10652
  const events = accumulator.consume(chunk);
10582
10653
  for (const event of events) yield event;
10583
10654
  }
10655
+ const drainEvent = accumulator.finalizeHeldText();
10656
+ if (drainEvent !== void 0) yield drainEvent;
10584
10657
  return accumulator.finish();
10585
10658
  }
10586
10659
  };
@@ -10596,6 +10669,7 @@ var init_openai2 = __esm({
10596
10669
  this.extractFromContent = extractFromContent;
10597
10670
  this.providerName = providerName;
10598
10671
  this.allowedToolNames = allowedToolNames;
10672
+ this.suppress = extractFromContent && allowedToolNames !== void 0 && allowedToolNames.size > 0 ? new StreamSuppressionBuffer(allowedToolNames) : void 0;
10599
10673
  }
10600
10674
  extractFromContent;
10601
10675
  providerName;
@@ -10608,18 +10682,30 @@ var init_openai2 = __esm({
10608
10682
  cacheWriteTokens;
10609
10683
  reasoningTokens;
10610
10684
  toolCalls = /* @__PURE__ */ new Map();
10685
+ /** R7: present only when recovery is enabled AND the request declares tools — holds suspected
10686
+ * leaked-dialect content back from the `text_delta` stream. `undefined` ⇒ stream immediately. */
10687
+ suppress;
10611
10688
  consume(chunk) {
10612
10689
  const events = [];
10613
10690
  this.applyUsage(chunk.usage);
10614
10691
  for (const choice of chunk.choices ?? []) {
10615
- const reasoningEvent = this.applyReasoningDelta(
10616
- choice.delta?.reasoning ?? choice.delta?.reasoning_content
10617
- );
10618
- if (reasoningEvent !== void 0) events.push(reasoningEvent);
10619
- const textEvent = this.applyContentDelta(choice.delta?.content);
10620
- if (textEvent !== void 0) events.push(textEvent);
10621
- this.mergeToolCallDeltas(choice.delta?.tool_calls);
10622
- this.applyFinishReason(choice.finish_reason);
10692
+ events.push(...this.applyChoice(choice));
10693
+ }
10694
+ return events;
10695
+ }
10696
+ applyChoice(choice) {
10697
+ const events = [];
10698
+ const reasoningEvent = this.applyReasoningDelta(
10699
+ choice.delta?.reasoning ?? choice.delta?.reasoning_content
10700
+ );
10701
+ if (reasoningEvent !== void 0) events.push(reasoningEvent);
10702
+ const textEvent = this.applyContentDelta(choice.delta?.content);
10703
+ if (textEvent !== void 0) events.push(textEvent);
10704
+ this.mergeToolCallDeltas(choice.delta?.tool_calls);
10705
+ this.applyFinishReason(choice.finish_reason);
10706
+ if (choice.finish_reason !== void 0 && choice.finish_reason !== null) {
10707
+ const flushEvent = this.finalizeHeldText();
10708
+ if (flushEvent !== void 0) events.push(flushEvent);
10623
10709
  }
10624
10710
  return events;
10625
10711
  }
@@ -10644,7 +10730,17 @@ var init_openai2 = __esm({
10644
10730
  applyContentDelta(content) {
10645
10731
  if (typeof content !== "string" || content.length === 0) return void 0;
10646
10732
  this.text += content;
10647
- return { type: "text_delta", text: content };
10733
+ if (this.suppress === void 0) return { type: "text_delta", text: content };
10734
+ const emit = this.suppress.push(content);
10735
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
10736
+ }
10737
+ /** R7 held-buffer finalizer, called at the `finish_reason` chunk (in `applyChoice`) AND after the
10738
+ * SSE loop in `stream()` — so a stream that omits a `finish_reason` terminal never silently drops
10739
+ * held text. `toolCalls.size > 0` (native calls present) makes `finish()` skip recovery, so the
10740
+ * buffer streams the held text whole. Idempotent once drained. */
10741
+ finalizeHeldText() {
10742
+ const emit = this.suppress?.drain(this.toolCalls.size > 0);
10743
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
10648
10744
  }
10649
10745
  mergeToolCallDeltas(deltas) {
10650
10746
  for (const call of deltas ?? []) {