@theokit/sdk 2.15.0 → 2.15.1

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
@@ -11178,11 +11178,16 @@ function sanitizeToolInput(input, options) {
11178
11178
  // src/internal/llm/hermes-tool-extract.ts
11179
11179
  var HERMES_BLOCK = /<function=\s*([^>\s]+)\s*>([\s\S]*?)<\/tool_call>/g;
11180
11180
  var HERMES_PARAM = /<parameter=\s*([^>\s]+)\s*>([\s\S]*?)<\/parameter>/g;
11181
- function extractHermesToolCalls(content, makeId) {
11181
+ function extractHermesToolCalls(content, makeId, allowedToolNames) {
11182
+ const isPromoted = (name) => name.length > 0 && (allowedToolNames === void 0 || allowedToolNames.has(name));
11182
11183
  const toolCalls = [];
11184
+ const droppedNames = [];
11183
11185
  for (const block of content.matchAll(HERMES_BLOCK)) {
11184
11186
  const name = (block[1] ?? "").trim();
11185
- if (name.length === 0) continue;
11187
+ if (!isPromoted(name)) {
11188
+ if (name.length > 0 && allowedToolNames !== void 0) droppedNames.push(name);
11189
+ continue;
11190
+ }
11186
11191
  toolCalls.push({
11187
11192
  type: "tool_use",
11188
11193
  id: makeId(),
@@ -11190,8 +11195,11 @@ function extractHermesToolCalls(content, makeId) {
11190
11195
  input: parseHermesParams(block[2] ?? "")
11191
11196
  });
11192
11197
  }
11193
- const residualText = toolCalls.length === 0 ? content : content.replace(HERMES_BLOCK, "").trim();
11194
- return { toolCalls, residualText };
11198
+ const residualText = toolCalls.length === 0 ? content : content.replace(
11199
+ HERMES_BLOCK,
11200
+ (full, rawName) => isPromoted((rawName ?? "").trim()) ? "" : full
11201
+ ).trim();
11202
+ return { toolCalls, residualText, droppedNames };
11195
11203
  }
11196
11204
  function parseHermesParams(inner) {
11197
11205
  const input = {};
@@ -11275,7 +11283,10 @@ var OpenAIClient = class {
11275
11283
  }
11276
11284
  const accumulator = new OpenAIStreamAccumulator(
11277
11285
  this.options.extractToolCallsFromContent ?? false,
11278
- providerId
11286
+ providerId,
11287
+ // R5: request-scoped allowlist — leaked recovery only promotes a block whose name is a tool the
11288
+ // model was actually given. Empty set (no tools) recovers nothing.
11289
+ new Set(request.tools?.map((tool) => tool.name) ?? [])
11279
11290
  );
11280
11291
  for await (const record of parseSseStream(response.body, signal)) {
11281
11292
  if (record.data === "[DONE]") break;
@@ -11305,13 +11316,18 @@ var OpenAIStreamAccumulator = class {
11305
11316
  /**
11306
11317
  * @param extractFromContent opt-in leaked-dialect safe-parse (theokit#58). Default false.
11307
11318
  * @param providerName provider id, used only to label the recovery log line.
11319
+ * @param allowedToolNames R5 request-scoped allowlist — built from `request.tools` at `stream()`;
11320
+ * leaked recovery in `finish()` only promotes a block whose name is in this set. `undefined`
11321
+ * (direct construction) recovers all (back-compat); an empty set recovers nothing.
11308
11322
  */
11309
- constructor(extractFromContent = false, providerName = "openai") {
11323
+ constructor(extractFromContent = false, providerName = "openai", allowedToolNames) {
11310
11324
  this.extractFromContent = extractFromContent;
11311
11325
  this.providerName = providerName;
11326
+ this.allowedToolNames = allowedToolNames;
11312
11327
  }
11313
11328
  extractFromContent;
11314
11329
  providerName;
11330
+ allowedToolNames;
11315
11331
  text = "";
11316
11332
  stopReason = "end_turn";
11317
11333
  inputTokens;
@@ -11382,7 +11398,8 @@ var OpenAIStreamAccumulator = class {
11382
11398
  if (this.extractFromContent && toolCalls.length === 0) {
11383
11399
  const recovered = extractHermesToolCalls(
11384
11400
  this.text,
11385
- () => `hermes-${globalThis.crypto.randomUUID()}`
11401
+ () => `hermes-${globalThis.crypto.randomUUID()}`,
11402
+ this.allowedToolNames
11386
11403
  );
11387
11404
  if (recovered.toolCalls.length > 0) {
11388
11405
  toolCalls.push(...recovered.toolCalls);
@@ -11390,6 +11407,12 @@ var OpenAIStreamAccumulator = class {
11390
11407
  stopReason = "tool_use";
11391
11408
  process.stderr.write(
11392
11409
  `[theokit-sdk] recovered ${recovered.toolCalls.length} leaked tool call(s) from assistant content (provider="${this.providerName}", names=${recovered.toolCalls.map((c) => c.name).join(",")})
11410
+ `
11411
+ );
11412
+ }
11413
+ if (recovered.droppedNames.length > 0) {
11414
+ process.stderr.write(
11415
+ `[theokit-sdk] dropped ${recovered.droppedNames.length} leaked block(s) whose name is not a tool in the request (provider="${this.providerName}", names=${recovered.droppedNames.join(",")})
11393
11416
  `
11394
11417
  );
11395
11418
  }