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