@dtelecom/agents-js 0.2.2 → 0.3.0

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.
@@ -366,6 +366,9 @@ var OpenRouterLLM = class {
366
366
  prov.require_parameters = true;
367
367
  body.provider = prov;
368
368
  }
369
+ if (options?.tools?.length) {
370
+ body.tools = options.tools;
371
+ }
369
372
  log2.debug(`LLM request: model=${this.model}, messages=${messages.length}`);
370
373
  const response = await fetch(OPENROUTER_URL, {
371
374
  method: "POST",
@@ -390,6 +393,7 @@ var OpenRouterLLM = class {
390
393
  let jsonBuffer = "";
391
394
  let segmentsYielded = false;
392
395
  let lastUsage;
396
+ const pendingToolCalls = /* @__PURE__ */ new Map();
393
397
  let inSegmentsArray = false;
394
398
  let objectStart = -1;
395
399
  let braceDepth = 0;
@@ -475,6 +479,20 @@ var OpenRouterLLM = class {
475
479
  yield { type: "token", token: delta.content };
476
480
  }
477
481
  }
482
+ if (delta?.tool_calls) {
483
+ for (const tc of delta.tool_calls) {
484
+ const idx = tc.index;
485
+ if (tc.id) {
486
+ pendingToolCalls.set(idx, { id: tc.id, name: tc.function?.name ?? "", arguments: tc.function?.arguments ?? "" });
487
+ } else {
488
+ const existing = pendingToolCalls.get(idx);
489
+ if (existing) {
490
+ if (tc.function?.name) existing.name += tc.function.name;
491
+ if (tc.function?.arguments) existing.arguments += tc.function.arguments;
492
+ }
493
+ }
494
+ }
495
+ }
478
496
  if (parsed.usage) {
479
497
  lastUsage = {
480
498
  promptTokens: parsed.usage.prompt_tokens,
@@ -491,6 +509,9 @@ var OpenRouterLLM = class {
491
509
  if (structured && !segmentsYielded && jsonBuffer.length > 0) {
492
510
  log2.warn(`LLM returned no segments. Raw JSON: "${jsonBuffer.slice(0, 300)}"`);
493
511
  }
512
+ for (const [, tc] of pendingToolCalls) {
513
+ yield { type: "tool_call", toolCall: tc };
514
+ }
494
515
  yield { type: "done", ...lastUsage ? { usage: lastUsage } : {} };
495
516
  }
496
517
  };
@@ -547,6 +568,9 @@ var OpenAILLM = class {
547
568
  if (this.responseFormat && !options?.plainText) {
548
569
  body.response_format = this.responseFormat;
549
570
  }
571
+ if (options?.tools?.length) {
572
+ body.tools = options.tools;
573
+ }
550
574
  log3.debug(`LLM request: model=${this.model}, messages=${messages.length}`);
551
575
  const response = await fetch(OPENAI_URL, {
552
576
  method: "POST",
@@ -571,6 +595,7 @@ var OpenAILLM = class {
571
595
  let jsonBuffer = "";
572
596
  let segmentsYielded = false;
573
597
  let lastUsage;
598
+ const pendingToolCalls = /* @__PURE__ */ new Map();
574
599
  let inSegmentsArray = false;
575
600
  let objectStart = -1;
576
601
  let braceDepth = 0;
@@ -664,6 +689,20 @@ var OpenAILLM = class {
664
689
  yield { type: "token", token: delta.content };
665
690
  }
666
691
  }
692
+ if (delta?.tool_calls) {
693
+ for (const tc of delta.tool_calls) {
694
+ const idx = tc.index;
695
+ if (tc.id) {
696
+ pendingToolCalls.set(idx, { id: tc.id, name: tc.function?.name ?? "", arguments: tc.function?.arguments ?? "" });
697
+ } else {
698
+ const existing = pendingToolCalls.get(idx);
699
+ if (existing) {
700
+ if (tc.function?.name) existing.name += tc.function.name;
701
+ if (tc.function?.arguments) existing.arguments += tc.function.arguments;
702
+ }
703
+ }
704
+ }
705
+ }
667
706
  if (parsed.usage) {
668
707
  lastUsage = {
669
708
  promptTokens: parsed.usage.prompt_tokens,
@@ -680,6 +719,9 @@ var OpenAILLM = class {
680
719
  if (structured && !segmentsYielded && jsonBuffer.length > 0) {
681
720
  log3.warn(`LLM returned no segments. Raw JSON: "${jsonBuffer.slice(0, 300)}"`);
682
721
  }
722
+ for (const [, tc] of pendingToolCalls) {
723
+ yield { type: "tool_call", toolCall: tc };
724
+ }
683
725
  yield { type: "done", ...lastUsage ? { usage: lastUsage } : {} };
684
726
  }
685
727
  };