@ljoukov/llm 4.0.10 → 4.0.12

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/index.cjs CHANGED
@@ -4752,12 +4752,12 @@ function buildGeminiToolOutputMediaPart(item) {
4752
4752
  if (!parsed) {
4753
4753
  return null;
4754
4754
  }
4755
- return (0, import_genai2.createFunctionResponsePartFromBase64)(parsed.dataBase64, parsed.mimeType);
4755
+ return (0, import_genai2.createPartFromBase64)(parsed.dataBase64, parsed.mimeType);
4756
4756
  }
4757
4757
  if (item.type === "input_file") {
4758
4758
  const dataUrl = typeof item.file_url === "string" ? parseDataUrlPayload(item.file_url) : null;
4759
4759
  if (dataUrl) {
4760
- const part = (0, import_genai2.createFunctionResponsePartFromBase64)(dataUrl.dataBase64, dataUrl.mimeType);
4760
+ const part = (0, import_genai2.createPartFromBase64)(dataUrl.dataBase64, dataUrl.mimeType);
4761
4761
  const displayName = item.filename?.trim();
4762
4762
  if (displayName && part.inlineData) {
4763
4763
  part.inlineData.displayName = displayName;
@@ -4766,7 +4766,7 @@ function buildGeminiToolOutputMediaPart(item) {
4766
4766
  }
4767
4767
  const inferredMimeType = inferToolOutputMimeTypeFromFilename(item.filename);
4768
4768
  if (typeof item.file_data === "string" && item.file_data.trim().length > 0 && inferredMimeType) {
4769
- const part = (0, import_genai2.createFunctionResponsePartFromBase64)(item.file_data, inferredMimeType);
4769
+ const part = (0, import_genai2.createPartFromBase64)(item.file_data, inferredMimeType);
4770
4770
  const displayName = item.filename?.trim();
4771
4771
  if (displayName && part.inlineData) {
4772
4772
  part.inlineData.displayName = displayName;
@@ -4774,7 +4774,7 @@ function buildGeminiToolOutputMediaPart(item) {
4774
4774
  return part;
4775
4775
  }
4776
4776
  if (typeof item.file_url === "string" && item.file_url.trim().length > 0 && inferredMimeType) {
4777
- const part = (0, import_genai2.createFunctionResponsePartFromUri)(item.file_url, inferredMimeType);
4777
+ const part = (0, import_genai2.createPartFromUri)(item.file_url, inferredMimeType);
4778
4778
  const displayName = item.filename?.trim();
4779
4779
  if (displayName && part.fileData) {
4780
4780
  part.fileData.displayName = displayName;
@@ -4808,41 +4808,35 @@ function toGeminiToolOutputPlaceholder(item) {
4808
4808
  media: dataUrl || typeof item.file_data === "string" && item.file_data.trim().length > 0 ? "attached-inline-data" : typeof item.file_url === "string" && item.file_url.trim().length > 0 ? "attached-file-data" : void 0
4809
4809
  };
4810
4810
  }
4811
- function buildGeminiFunctionResponsePart(options) {
4811
+ function buildGeminiFunctionResponseParts(options) {
4812
4812
  const outputItems = toGeminiToolOutputItems(options.outputPayload);
4813
4813
  if (!outputItems) {
4814
4814
  const responsePayload2 = isPlainRecord(options.outputPayload) ? sanitiseLogValue(options.outputPayload) : { output: sanitiseLogValue(options.outputPayload) };
4815
4815
  if (options.callId) {
4816
- return (0, import_genai2.createPartFromFunctionResponse)(options.callId, options.toolName, responsePayload2);
4816
+ return [(0, import_genai2.createPartFromFunctionResponse)(options.callId, options.toolName, responsePayload2)];
4817
4817
  }
4818
- return {
4819
- functionResponse: {
4820
- name: options.toolName,
4821
- response: responsePayload2
4818
+ return [
4819
+ {
4820
+ functionResponse: {
4821
+ name: options.toolName,
4822
+ response: responsePayload2
4823
+ }
4822
4824
  }
4823
- };
4825
+ ];
4824
4826
  }
4825
4827
  const responseOutput = outputItems.map((item) => toGeminiToolOutputPlaceholder(item));
4826
- const responseMediaParts = outputItems.flatMap((item) => {
4828
+ const responseParts = outputItems.flatMap((item) => {
4827
4829
  const mediaPart = buildGeminiToolOutputMediaPart(item);
4828
4830
  return mediaPart ? [mediaPart] : [];
4829
4831
  });
4830
4832
  const responsePayload = { output: responseOutput };
4831
- if (options.callId) {
4832
- return (0, import_genai2.createPartFromFunctionResponse)(
4833
- options.callId,
4834
- options.toolName,
4835
- responsePayload,
4836
- responseMediaParts
4837
- );
4838
- }
4839
- return {
4833
+ const functionResponsePart = options.callId ? (0, import_genai2.createPartFromFunctionResponse)(options.callId, options.toolName, responsePayload) : {
4840
4834
  functionResponse: {
4841
4835
  name: options.toolName,
4842
- response: { output: responseOutput },
4843
- ...responseMediaParts.length > 0 ? { parts: responseMediaParts } : {}
4836
+ response: responsePayload
4844
4837
  }
4845
4838
  };
4839
+ return [functionResponsePart, ...responseParts];
4846
4840
  }
4847
4841
  function parseOpenAiToolArguments(raw) {
4848
4842
  const trimmed = raw.trim();
@@ -7442,8 +7436,6 @@ async function runToolLoop(request) {
7442
7436
  let thoughtsText2 = "";
7443
7437
  const modelParts = [];
7444
7438
  const functionCalls = [];
7445
- const seenFunctionCallIds = /* @__PURE__ */ new Set();
7446
- const seenFunctionCallKeys = /* @__PURE__ */ new Set();
7447
7439
  let latestUsageMetadata;
7448
7440
  let resolvedModelVersion;
7449
7441
  for await (const chunk of stream) {
@@ -7460,34 +7452,13 @@ async function runToolLoop(request) {
7460
7452
  continue;
7461
7453
  }
7462
7454
  const primary = candidates[0];
7463
- const parts = primary?.content?.parts;
7464
- if (!parts || parts.length === 0) {
7455
+ const parts = primary?.content?.parts ?? [];
7456
+ const chunkFunctionCalls = chunk.functionCalls ?? [];
7457
+ if (parts.length === 0 && chunkFunctionCalls.length === 0) {
7465
7458
  continue;
7466
7459
  }
7467
7460
  for (const part of parts) {
7468
7461
  modelParts.push(part);
7469
- const call = part.functionCall;
7470
- if (call) {
7471
- const id = typeof call.id === "string" ? call.id : "";
7472
- const shouldAdd = (() => {
7473
- if (id.length > 0) {
7474
- if (seenFunctionCallIds.has(id)) {
7475
- return false;
7476
- }
7477
- seenFunctionCallIds.add(id);
7478
- return true;
7479
- }
7480
- const key = JSON.stringify({ name: call.name ?? "", args: call.args ?? null });
7481
- if (seenFunctionCallKeys.has(key)) {
7482
- return false;
7483
- }
7484
- seenFunctionCallKeys.add(key);
7485
- return true;
7486
- })();
7487
- if (shouldAdd) {
7488
- functionCalls.push(call);
7489
- }
7490
- }
7491
7462
  if (typeof part.text === "string" && part.text.length > 0) {
7492
7463
  if (part.thought) {
7493
7464
  thoughtsText2 += part.text;
@@ -7500,6 +7471,15 @@ async function runToolLoop(request) {
7500
7471
  }
7501
7472
  }
7502
7473
  }
7474
+ if (chunkFunctionCalls.length > 0) {
7475
+ functionCalls.push(...chunkFunctionCalls);
7476
+ continue;
7477
+ }
7478
+ for (const part of parts) {
7479
+ if (part.functionCall) {
7480
+ functionCalls.push(part.functionCall);
7481
+ }
7482
+ }
7503
7483
  }
7504
7484
  return {
7505
7485
  responseText: responseText2,
@@ -7670,7 +7650,7 @@ async function runToolLoop(request) {
7670
7650
  durationMs: result.durationMs
7671
7651
  });
7672
7652
  responseParts.push(
7673
- buildGeminiFunctionResponsePart({
7653
+ ...buildGeminiFunctionResponseParts({
7674
7654
  toolName: entry.toolName,
7675
7655
  callId: entry.call.id,
7676
7656
  outputPayload