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