@yourgpt/copilot-sdk 1.2.0 → 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
  *
@@ -1199,6 +1243,16 @@ var ChatWithTools = class {
1199
1243
  result: r.success ? r.result : { success: false, error: r.error }
1200
1244
  }));
1201
1245
  await this.chat.continueWithToolResults(toolResults);
1246
+ } else if (this.agentLoop.maxIterationsReached && toolCallInfos.length > 0) {
1247
+ this.debug("Max iterations reached, adding blocked tool results");
1248
+ const blockedResults = toolCallInfos.map((tc) => ({
1249
+ toolCallId: tc.id,
1250
+ result: {
1251
+ success: false,
1252
+ error: "Tool execution blocked: maximum iterations reached"
1253
+ }
1254
+ }));
1255
+ await this.chat.continueWithToolResults(blockedResults);
1202
1256
  }
1203
1257
  } catch (error) {
1204
1258
  this.debug("Error executing tools:", error);
@@ -1566,8 +1620,6 @@ function useCopilot() {
1566
1620
  function CopilotProvider({
1567
1621
  children,
1568
1622
  runtimeUrl,
1569
- config,
1570
- cloud,
1571
1623
  systemPrompt,
1572
1624
  tools: toolsConfig,
1573
1625
  threadId,
@@ -1575,7 +1627,8 @@ function CopilotProvider({
1575
1627
  onMessagesChange,
1576
1628
  onError,
1577
1629
  streaming,
1578
- debug = false
1630
+ debug = false,
1631
+ maxIterations
1579
1632
  }) {
1580
1633
  const debugLog = useCallback(
1581
1634
  (...args) => {
@@ -1607,12 +1660,12 @@ function CopilotProvider({
1607
1660
  chatRef.current = new ReactChatWithTools(
1608
1661
  {
1609
1662
  runtimeUrl,
1610
- llm: config,
1611
1663
  systemPrompt,
1612
1664
  threadId,
1613
1665
  initialMessages: uiInitialMessages,
1614
1666
  streaming,
1615
- debug
1667
+ debug,
1668
+ maxIterations
1616
1669
  },
1617
1670
  {
1618
1671
  onToolExecutionsChange: (executions) => {
@@ -3525,5 +3578,5 @@ function useChat(config) {
3525
3578
  }
3526
3579
 
3527
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 };
3528
- //# sourceMappingURL=chunk-HPNLS3PQ.js.map
3529
- //# sourceMappingURL=chunk-HPNLS3PQ.js.map
3581
+ //# sourceMappingURL=chunk-34LUNTCZ.js.map
3582
+ //# sourceMappingURL=chunk-34LUNTCZ.js.map