@yourgpt/llm-sdk 2.1.0 → 2.1.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.
@@ -1,4 +1,4 @@
1
- import { k as LanguageModel, Z as AnthropicProviderConfig, A as AIProvider } from '../../types-C_f95PKp.mjs';
1
+ import { k as LanguageModel, Z as AnthropicProviderConfig, A as AIProvider } from '../../types-D20jKwJW.mjs';
2
2
  import 'zod';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { k as LanguageModel, Z as AnthropicProviderConfig, A as AIProvider } from '../../types-C_f95PKp.js';
1
+ import { k as LanguageModel, Z as AnthropicProviderConfig, A as AIProvider } from '../../types-D20jKwJW.js';
2
2
  import 'zod';
3
3
 
4
4
  /**
@@ -257,34 +257,37 @@ function formatMessagesForAnthropic(messages) {
257
257
  let system = "";
258
258
  const formatted = [];
259
259
  const pendingToolResults = [];
260
- for (const msg of messages) {
261
- if (msg.role === "system") {
262
- system += (system ? "\n" : "") + msg.content;
263
- continue;
264
- }
265
- if (msg.role === "assistant" && pendingToolResults.length > 0) {
260
+ const flushToolResults = () => {
261
+ if (pendingToolResults.length === 0) return;
262
+ const validResults = pendingToolResults.filter((tr) => {
263
+ if (!tr.toolCallId) {
264
+ console.warn("[llm-sdk] Skipping tool result with missing toolCallId");
265
+ return false;
266
+ }
267
+ return true;
268
+ });
269
+ if (validResults.length > 0) {
266
270
  formatted.push({
267
271
  role: "user",
268
- content: pendingToolResults.map((tr) => ({
272
+ content: validResults.map((tr) => ({
269
273
  type: "tool_result",
270
274
  tool_use_id: tr.toolCallId,
271
275
  content: tr.content
272
276
  }))
273
277
  });
274
- pendingToolResults.length = 0;
278
+ }
279
+ pendingToolResults.length = 0;
280
+ };
281
+ for (const msg of messages) {
282
+ if (msg.role === "system") {
283
+ system += (system ? "\n" : "") + msg.content;
284
+ continue;
285
+ }
286
+ if (msg.role === "assistant" && pendingToolResults.length > 0) {
287
+ flushToolResults();
275
288
  }
276
289
  if (msg.role === "user") {
277
- if (pendingToolResults.length > 0) {
278
- formatted.push({
279
- role: "user",
280
- content: pendingToolResults.map((tr) => ({
281
- type: "tool_result",
282
- tool_use_id: tr.toolCallId,
283
- content: tr.content
284
- }))
285
- });
286
- pendingToolResults.length = 0;
287
- }
290
+ flushToolResults();
288
291
  if (typeof msg.content === "string") {
289
292
  formatted.push({ role: "user", content: msg.content });
290
293
  } else {
@@ -333,22 +336,21 @@ function formatMessagesForAnthropic(messages) {
333
336
  formatted.push({ role: "assistant", content });
334
337
  }
335
338
  } else if (msg.role === "tool") {
339
+ const toolCallId = msg.toolCallId ?? msg.tool_call_id ?? msg.toolUseId;
340
+ if (!toolCallId) {
341
+ console.warn(
342
+ "[llm-sdk] Tool message missing toolCallId, skipping:",
343
+ msg
344
+ );
345
+ continue;
346
+ }
336
347
  pendingToolResults.push({
337
- toolCallId: msg.toolCallId,
348
+ toolCallId,
338
349
  content: msg.content
339
350
  });
340
351
  }
341
352
  }
342
- if (pendingToolResults.length > 0) {
343
- formatted.push({
344
- role: "user",
345
- content: pendingToolResults.map((tr) => ({
346
- type: "tool_result",
347
- tool_use_id: tr.toolCallId,
348
- content: tr.content
349
- }))
350
- });
351
- }
353
+ flushToolResults();
352
354
  return { system, messages: formatted };
353
355
  }
354
356
 
@@ -540,6 +542,8 @@ var AnthropicAdapter = class {
540
542
  convertToAnthropicMessages(rawMessages) {
541
543
  const messages = [];
542
544
  const pendingToolResults = [];
545
+ let lastToolCallIds = [];
546
+ let toolResultIndex = 0;
543
547
  for (const msg of rawMessages) {
544
548
  if (msg.role === "system") continue;
545
549
  if (msg.role === "assistant") {
@@ -553,6 +557,8 @@ var AnthropicAdapter = class {
553
557
  }))
554
558
  });
555
559
  pendingToolResults.length = 0;
560
+ lastToolCallIds = [];
561
+ toolResultIndex = 0;
556
562
  }
557
563
  const content = [];
558
564
  if (msg.content && typeof msg.content === "string" && msg.content.trim()) {
@@ -560,6 +566,8 @@ var AnthropicAdapter = class {
560
566
  }
561
567
  const toolCalls = msg.tool_calls;
562
568
  if (toolCalls && toolCalls.length > 0) {
569
+ lastToolCallIds = toolCalls.map((tc) => tc.id);
570
+ toolResultIndex = 0;
563
571
  for (const tc of toolCalls) {
564
572
  let input = {};
565
573
  try {
@@ -578,8 +586,35 @@ var AnthropicAdapter = class {
578
586
  messages.push({ role: "assistant", content });
579
587
  }
580
588
  } else if (msg.role === "tool") {
589
+ let toolCallId = msg.tool_call_id;
590
+ if (!toolCallId && lastToolCallIds.length > 0) {
591
+ toolCallId = lastToolCallIds[toolResultIndex];
592
+ toolResultIndex++;
593
+ console.warn(
594
+ `[llm-sdk] Tool message missing tool_call_id, inferred: ${toolCallId}`
595
+ );
596
+ }
597
+ if (!toolCallId) {
598
+ console.warn(
599
+ "[llm-sdk] Skipping tool message with missing tool_call_id (no inference possible):",
600
+ msg
601
+ );
602
+ continue;
603
+ }
604
+ if (lastToolCallIds.length === 0) {
605
+ console.warn(
606
+ `[llm-sdk] Skipping orphaned tool result (no pending tool_use): ${toolCallId}`
607
+ );
608
+ continue;
609
+ }
610
+ if (!lastToolCallIds.includes(toolCallId)) {
611
+ console.warn(
612
+ `[llm-sdk] Skipping tool result with unexpected tool_call_id: ${toolCallId}`
613
+ );
614
+ continue;
615
+ }
581
616
  pendingToolResults.push({
582
- tool_use_id: msg.tool_call_id,
617
+ tool_use_id: toolCallId,
583
618
  content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
584
619
  });
585
620
  } else if (msg.role === "user") {