@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/eval.js CHANGED
@@ -11203,6 +11203,77 @@ function parseHermesParams(inner) {
11203
11203
  }
11204
11204
  return sanitizeToolInput(input, { trim: true }).value;
11205
11205
  }
11206
+ var STREAM_MARKER = "<function=";
11207
+ var DEFAULT_STREAM_BUFFER_CAP = 8192;
11208
+ var isStreamWs = (c) => c === " " || c === " " || c === "\n" || c === "\r";
11209
+ function streamToolCallBufferState(held, allowedToolNames, cap = DEFAULT_STREAM_BUFFER_CAP) {
11210
+ if (allowedToolNames.size === 0) return "impossible";
11211
+ const t = held.trimStart();
11212
+ if (t.length < STREAM_MARKER.length) {
11213
+ return STREAM_MARKER.startsWith(t) ? "possible" : "impossible";
11214
+ }
11215
+ if (!t.startsWith(STREAM_MARKER)) return "impossible";
11216
+ const parsed = parseStreamMarkerName(t);
11217
+ if (parsed === "building") return "possible";
11218
+ if (parsed === "invalid") return "impossible";
11219
+ const nameOk = parsed.complete ? allowedToolNames.has(parsed.name) : someToolNameStartsWith(allowedToolNames, parsed.name);
11220
+ if (!nameOk) return "impossible";
11221
+ return held.length > cap ? "impossible" : "possible";
11222
+ }
11223
+ function parseStreamMarkerName(t) {
11224
+ let cursor = STREAM_MARKER.length;
11225
+ while (cursor < t.length && isStreamWs(t[cursor])) cursor += 1;
11226
+ const nameStart = cursor;
11227
+ while (cursor < t.length && t[cursor] !== ">" && !isStreamWs(t[cursor])) cursor += 1;
11228
+ const name = t.slice(nameStart, cursor);
11229
+ if (name.length === 0) return cursor >= t.length ? "building" : "invalid";
11230
+ return { name, complete: cursor < t.length && t[cursor] === ">" };
11231
+ }
11232
+ function someToolNameStartsWith(allowedToolNames, prefix) {
11233
+ for (const name of allowedToolNames) {
11234
+ if (name.startsWith(prefix)) return true;
11235
+ }
11236
+ return false;
11237
+ }
11238
+ function firstPossibleMarkerStart(held, allowedToolNames) {
11239
+ for (let i = held.indexOf("<"); i !== -1; i = held.indexOf("<", i + 1)) {
11240
+ if (streamToolCallBufferState(held.slice(i), allowedToolNames) === "possible") return i;
11241
+ }
11242
+ return -1;
11243
+ }
11244
+ var StreamSuppressionBuffer = class {
11245
+ constructor(allowedToolNames) {
11246
+ this.allowedToolNames = allowedToolNames;
11247
+ }
11248
+ allowedToolNames;
11249
+ #held = "";
11250
+ /** Feed a content delta; returns the text to emit as a `text_delta` now, or `undefined` to hold. */
11251
+ push(content) {
11252
+ this.#held += content;
11253
+ if (streamToolCallBufferState(this.#held, this.allowedToolNames) === "possible")
11254
+ return void 0;
11255
+ const holdStart = firstPossibleMarkerStart(this.#held, this.allowedToolNames);
11256
+ if (holdStart > 0) {
11257
+ const flush2 = this.#held.slice(0, holdStart);
11258
+ this.#held = this.#held.slice(holdStart);
11259
+ return flush2;
11260
+ }
11261
+ const flush = this.#held;
11262
+ this.#held = "";
11263
+ return flush;
11264
+ }
11265
+ /** Drain the held buffer at stream end. `hasNativeCalls` mirrors `finish()`'s size-guard: when
11266
+ * native `tool_calls` exist, `finish()` won't strip the leaked block, so stream the held text WHOLE
11267
+ * (keeping `accumulatedText == finish.text`); otherwise strip the recoverable blocks. Idempotent. */
11268
+ drain(hasNativeCalls) {
11269
+ if (this.#held.length === 0) return void 0;
11270
+ const held = this.#held;
11271
+ this.#held = "";
11272
+ if (hasNativeCalls) return held;
11273
+ const residual = extractHermesToolCalls(held, () => "held", this.allowedToolNames).residualText;
11274
+ return residual.length > 0 ? residual : void 0;
11275
+ }
11276
+ };
11206
11277
 
11207
11278
  // src/internal/llm/openai.ts
11208
11279
  var OpenAIClient = class {
@@ -11301,6 +11372,8 @@ var OpenAIClient = class {
11301
11372
  const events = accumulator.consume(chunk);
11302
11373
  for (const event of events) yield event;
11303
11374
  }
11375
+ const drainEvent = accumulator.finalizeHeldText();
11376
+ if (drainEvent !== void 0) yield drainEvent;
11304
11377
  return accumulator.finish();
11305
11378
  }
11306
11379
  };
@@ -11316,6 +11389,7 @@ var OpenAIStreamAccumulator = class {
11316
11389
  this.extractFromContent = extractFromContent;
11317
11390
  this.providerName = providerName;
11318
11391
  this.allowedToolNames = allowedToolNames;
11392
+ this.suppress = extractFromContent && allowedToolNames !== void 0 && allowedToolNames.size > 0 ? new StreamSuppressionBuffer(allowedToolNames) : void 0;
11319
11393
  }
11320
11394
  extractFromContent;
11321
11395
  providerName;
@@ -11328,18 +11402,30 @@ var OpenAIStreamAccumulator = class {
11328
11402
  cacheWriteTokens;
11329
11403
  reasoningTokens;
11330
11404
  toolCalls = /* @__PURE__ */ new Map();
11405
+ /** R7: present only when recovery is enabled AND the request declares tools — holds suspected
11406
+ * leaked-dialect content back from the `text_delta` stream. `undefined` ⇒ stream immediately. */
11407
+ suppress;
11331
11408
  consume(chunk) {
11332
11409
  const events = [];
11333
11410
  this.applyUsage(chunk.usage);
11334
11411
  for (const choice of chunk.choices ?? []) {
11335
- const reasoningEvent = this.applyReasoningDelta(
11336
- choice.delta?.reasoning ?? choice.delta?.reasoning_content
11337
- );
11338
- if (reasoningEvent !== void 0) events.push(reasoningEvent);
11339
- const textEvent = this.applyContentDelta(choice.delta?.content);
11340
- if (textEvent !== void 0) events.push(textEvent);
11341
- this.mergeToolCallDeltas(choice.delta?.tool_calls);
11342
- this.applyFinishReason(choice.finish_reason);
11412
+ events.push(...this.applyChoice(choice));
11413
+ }
11414
+ return events;
11415
+ }
11416
+ applyChoice(choice) {
11417
+ const events = [];
11418
+ const reasoningEvent = this.applyReasoningDelta(
11419
+ choice.delta?.reasoning ?? choice.delta?.reasoning_content
11420
+ );
11421
+ if (reasoningEvent !== void 0) events.push(reasoningEvent);
11422
+ const textEvent = this.applyContentDelta(choice.delta?.content);
11423
+ if (textEvent !== void 0) events.push(textEvent);
11424
+ this.mergeToolCallDeltas(choice.delta?.tool_calls);
11425
+ this.applyFinishReason(choice.finish_reason);
11426
+ if (choice.finish_reason !== void 0 && choice.finish_reason !== null) {
11427
+ const flushEvent = this.finalizeHeldText();
11428
+ if (flushEvent !== void 0) events.push(flushEvent);
11343
11429
  }
11344
11430
  return events;
11345
11431
  }
@@ -11364,7 +11450,17 @@ var OpenAIStreamAccumulator = class {
11364
11450
  applyContentDelta(content) {
11365
11451
  if (typeof content !== "string" || content.length === 0) return void 0;
11366
11452
  this.text += content;
11367
- return { type: "text_delta", text: content };
11453
+ if (this.suppress === void 0) return { type: "text_delta", text: content };
11454
+ const emit = this.suppress.push(content);
11455
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
11456
+ }
11457
+ /** R7 held-buffer finalizer, called at the `finish_reason` chunk (in `applyChoice`) AND after the
11458
+ * SSE loop in `stream()` — so a stream that omits a `finish_reason` terminal never silently drops
11459
+ * held text. `toolCalls.size > 0` (native calls present) makes `finish()` skip recovery, so the
11460
+ * buffer streams the held text whole. Idempotent once drained. */
11461
+ finalizeHeldText() {
11462
+ const emit = this.suppress?.drain(this.toolCalls.size > 0);
11463
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
11368
11464
  }
11369
11465
  mergeToolCallDeltas(deltas) {
11370
11466
  for (const call of deltas ?? []) {