@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/cron.cjs CHANGED
@@ -11211,6 +11211,77 @@ function parseHermesParams(inner) {
11211
11211
  }
11212
11212
  return sanitizeToolInput(input, { trim: true }).value;
11213
11213
  }
11214
+ var STREAM_MARKER = "<function=";
11215
+ var DEFAULT_STREAM_BUFFER_CAP = 8192;
11216
+ var isStreamWs = (c) => c === " " || c === " " || c === "\n" || c === "\r";
11217
+ function streamToolCallBufferState(held, allowedToolNames, cap = DEFAULT_STREAM_BUFFER_CAP) {
11218
+ if (allowedToolNames.size === 0) return "impossible";
11219
+ const t = held.trimStart();
11220
+ if (t.length < STREAM_MARKER.length) {
11221
+ return STREAM_MARKER.startsWith(t) ? "possible" : "impossible";
11222
+ }
11223
+ if (!t.startsWith(STREAM_MARKER)) return "impossible";
11224
+ const parsed = parseStreamMarkerName(t);
11225
+ if (parsed === "building") return "possible";
11226
+ if (parsed === "invalid") return "impossible";
11227
+ const nameOk = parsed.complete ? allowedToolNames.has(parsed.name) : someToolNameStartsWith(allowedToolNames, parsed.name);
11228
+ if (!nameOk) return "impossible";
11229
+ return held.length > cap ? "impossible" : "possible";
11230
+ }
11231
+ function parseStreamMarkerName(t) {
11232
+ let cursor = STREAM_MARKER.length;
11233
+ while (cursor < t.length && isStreamWs(t[cursor])) cursor += 1;
11234
+ const nameStart = cursor;
11235
+ while (cursor < t.length && t[cursor] !== ">" && !isStreamWs(t[cursor])) cursor += 1;
11236
+ const name = t.slice(nameStart, cursor);
11237
+ if (name.length === 0) return cursor >= t.length ? "building" : "invalid";
11238
+ return { name, complete: cursor < t.length && t[cursor] === ">" };
11239
+ }
11240
+ function someToolNameStartsWith(allowedToolNames, prefix) {
11241
+ for (const name of allowedToolNames) {
11242
+ if (name.startsWith(prefix)) return true;
11243
+ }
11244
+ return false;
11245
+ }
11246
+ function firstPossibleMarkerStart(held, allowedToolNames) {
11247
+ for (let i = held.indexOf("<"); i !== -1; i = held.indexOf("<", i + 1)) {
11248
+ if (streamToolCallBufferState(held.slice(i), allowedToolNames) === "possible") return i;
11249
+ }
11250
+ return -1;
11251
+ }
11252
+ var StreamSuppressionBuffer = class {
11253
+ constructor(allowedToolNames) {
11254
+ this.allowedToolNames = allowedToolNames;
11255
+ }
11256
+ allowedToolNames;
11257
+ #held = "";
11258
+ /** Feed a content delta; returns the text to emit as a `text_delta` now, or `undefined` to hold. */
11259
+ push(content) {
11260
+ this.#held += content;
11261
+ if (streamToolCallBufferState(this.#held, this.allowedToolNames) === "possible")
11262
+ return void 0;
11263
+ const holdStart = firstPossibleMarkerStart(this.#held, this.allowedToolNames);
11264
+ if (holdStart > 0) {
11265
+ const flush2 = this.#held.slice(0, holdStart);
11266
+ this.#held = this.#held.slice(holdStart);
11267
+ return flush2;
11268
+ }
11269
+ const flush = this.#held;
11270
+ this.#held = "";
11271
+ return flush;
11272
+ }
11273
+ /** Drain the held buffer at stream end. `hasNativeCalls` mirrors `finish()`'s size-guard: when
11274
+ * native `tool_calls` exist, `finish()` won't strip the leaked block, so stream the held text WHOLE
11275
+ * (keeping `accumulatedText == finish.text`); otherwise strip the recoverable blocks. Idempotent. */
11276
+ drain(hasNativeCalls) {
11277
+ if (this.#held.length === 0) return void 0;
11278
+ const held = this.#held;
11279
+ this.#held = "";
11280
+ if (hasNativeCalls) return held;
11281
+ const residual = extractHermesToolCalls(held, () => "held", this.allowedToolNames).residualText;
11282
+ return residual.length > 0 ? residual : void 0;
11283
+ }
11284
+ };
11214
11285
 
11215
11286
  // src/internal/llm/openai.ts
11216
11287
  var OpenAIClient = class {
@@ -11309,6 +11380,8 @@ var OpenAIClient = class {
11309
11380
  const events = accumulator.consume(chunk);
11310
11381
  for (const event of events) yield event;
11311
11382
  }
11383
+ const drainEvent = accumulator.finalizeHeldText();
11384
+ if (drainEvent !== void 0) yield drainEvent;
11312
11385
  return accumulator.finish();
11313
11386
  }
11314
11387
  };
@@ -11324,6 +11397,7 @@ var OpenAIStreamAccumulator = class {
11324
11397
  this.extractFromContent = extractFromContent;
11325
11398
  this.providerName = providerName;
11326
11399
  this.allowedToolNames = allowedToolNames;
11400
+ this.suppress = extractFromContent && allowedToolNames !== void 0 && allowedToolNames.size > 0 ? new StreamSuppressionBuffer(allowedToolNames) : void 0;
11327
11401
  }
11328
11402
  extractFromContent;
11329
11403
  providerName;
@@ -11336,18 +11410,30 @@ var OpenAIStreamAccumulator = class {
11336
11410
  cacheWriteTokens;
11337
11411
  reasoningTokens;
11338
11412
  toolCalls = /* @__PURE__ */ new Map();
11413
+ /** R7: present only when recovery is enabled AND the request declares tools — holds suspected
11414
+ * leaked-dialect content back from the `text_delta` stream. `undefined` ⇒ stream immediately. */
11415
+ suppress;
11339
11416
  consume(chunk) {
11340
11417
  const events = [];
11341
11418
  this.applyUsage(chunk.usage);
11342
11419
  for (const choice of chunk.choices ?? []) {
11343
- const reasoningEvent = this.applyReasoningDelta(
11344
- choice.delta?.reasoning ?? choice.delta?.reasoning_content
11345
- );
11346
- if (reasoningEvent !== void 0) events.push(reasoningEvent);
11347
- const textEvent = this.applyContentDelta(choice.delta?.content);
11348
- if (textEvent !== void 0) events.push(textEvent);
11349
- this.mergeToolCallDeltas(choice.delta?.tool_calls);
11350
- this.applyFinishReason(choice.finish_reason);
11420
+ events.push(...this.applyChoice(choice));
11421
+ }
11422
+ return events;
11423
+ }
11424
+ applyChoice(choice) {
11425
+ const events = [];
11426
+ const reasoningEvent = this.applyReasoningDelta(
11427
+ choice.delta?.reasoning ?? choice.delta?.reasoning_content
11428
+ );
11429
+ if (reasoningEvent !== void 0) events.push(reasoningEvent);
11430
+ const textEvent = this.applyContentDelta(choice.delta?.content);
11431
+ if (textEvent !== void 0) events.push(textEvent);
11432
+ this.mergeToolCallDeltas(choice.delta?.tool_calls);
11433
+ this.applyFinishReason(choice.finish_reason);
11434
+ if (choice.finish_reason !== void 0 && choice.finish_reason !== null) {
11435
+ const flushEvent = this.finalizeHeldText();
11436
+ if (flushEvent !== void 0) events.push(flushEvent);
11351
11437
  }
11352
11438
  return events;
11353
11439
  }
@@ -11372,7 +11458,17 @@ var OpenAIStreamAccumulator = class {
11372
11458
  applyContentDelta(content) {
11373
11459
  if (typeof content !== "string" || content.length === 0) return void 0;
11374
11460
  this.text += content;
11375
- return { type: "text_delta", text: content };
11461
+ if (this.suppress === void 0) return { type: "text_delta", text: content };
11462
+ const emit = this.suppress.push(content);
11463
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
11464
+ }
11465
+ /** R7 held-buffer finalizer, called at the `finish_reason` chunk (in `applyChoice`) AND after the
11466
+ * SSE loop in `stream()` — so a stream that omits a `finish_reason` terminal never silently drops
11467
+ * held text. `toolCalls.size > 0` (native calls present) makes `finish()` skip recovery, so the
11468
+ * buffer streams the held text whole. Idempotent once drained. */
11469
+ finalizeHeldText() {
11470
+ const emit = this.suppress?.drain(this.toolCalls.size > 0);
11471
+ return emit !== void 0 ? { type: "text_delta", text: emit } : void 0;
11376
11472
  }
11377
11473
  mergeToolCallDeltas(deltas) {
11378
11474
  for (const call of deltas ?? []) {