@yourgpt/copilot-sdk 1.2.1 → 1.2.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.
@@ -457,6 +457,7 @@ var AbstractChat = class {
457
457
  async sendMessage(content, attachments) {
458
458
  this.debug("sendMessage", { content, attachments });
459
459
  try {
460
+ this.resolveUnresolvedToolCalls();
460
461
  const userMessage = createUserMessage(content, attachments);
461
462
  this.state.pushMessage(userMessage);
462
463
  this.state.status = "submitted";
@@ -469,6 +470,49 @@ var AbstractChat = class {
469
470
  this.handleError(error);
470
471
  }
471
472
  }
473
+ /**
474
+ * Resolve any tool_calls that don't have corresponding tool_results.
475
+ * This prevents Anthropic API errors when tool_use has no tool_result.
476
+ * Can happen when max iterations is reached or tool execution is interrupted.
477
+ */
478
+ resolveUnresolvedToolCalls() {
479
+ const messages = this.state.messages;
480
+ const allToolCallIds = /* @__PURE__ */ new Set();
481
+ const resolvedIds = /* @__PURE__ */ new Set();
482
+ for (const msg of messages) {
483
+ if (msg.role === "assistant" && msg.toolCalls?.length) {
484
+ for (const tc of msg.toolCalls) {
485
+ allToolCallIds.add(tc.id);
486
+ }
487
+ }
488
+ if (msg.role === "tool" && msg.toolCallId) {
489
+ resolvedIds.add(msg.toolCallId);
490
+ }
491
+ }
492
+ const unresolvedIds = [...allToolCallIds].filter(
493
+ (id) => !resolvedIds.has(id)
494
+ );
495
+ if (unresolvedIds.length > 0) {
496
+ this.debug(
497
+ "resolveUnresolvedToolCalls",
498
+ `Adding ${unresolvedIds.length} missing tool results`
499
+ );
500
+ for (const toolCallId of unresolvedIds) {
501
+ const toolMessage = {
502
+ id: generateMessageId(),
503
+ role: "tool",
504
+ content: JSON.stringify({
505
+ success: false,
506
+ error: "Tool execution was interrupted. Please try again."
507
+ }),
508
+ toolCallId,
509
+ createdAt: /* @__PURE__ */ new Date()
510
+ };
511
+ this.state.pushMessage(toolMessage);
512
+ }
513
+ this.callbacks.onMessagesChange?.(this.state.messages);
514
+ }
515
+ }
472
516
  /**
473
517
  * Continue with tool results
474
518
  *
@@ -3534,5 +3578,5 @@ function useChat(config) {
3534
3578
  }
3535
3579
 
3536
3580
  export { AbstractAgentLoop, AbstractChat, CopilotProvider, ReactChat, ReactChatState, ReactThreadManager, ReactThreadManagerState, createPermissionStorage, createReactChat, createReactChatState, createReactThreadManager, createReactThreadManagerState, createSessionPermissionCache, formatKnowledgeResultsForAI, initialAgentLoopState, searchKnowledgeBase, useAIAction, useAIActions, useAIContext, useAIContexts, useAITools, useAgent, useCapabilities, useChat, useCopilot, useDevLogger, useFeatureSupport, useKnowledgeBase, useSuggestions, useSupportedMediaTypes, useThreadManager, useTool, useToolExecutor, useToolWithSchema, useTools, useToolsWithSchema };
3537
- //# sourceMappingURL=chunk-A43DHFKL.js.map
3538
- //# sourceMappingURL=chunk-A43DHFKL.js.map
3581
+ //# sourceMappingURL=chunk-34LUNTCZ.js.map
3582
+ //# sourceMappingURL=chunk-34LUNTCZ.js.map