@cloudflare/ai-chat 0.8.0 → 0.8.1

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/react.js CHANGED
@@ -902,6 +902,7 @@ function useAgentChat(options) {
902
902
  markInitialMessagesSeeded();
903
903
  setMessages([]);
904
904
  setClientToolResults(/* @__PURE__ */ new Map());
905
+ setPendingOnToolCallIds(/* @__PURE__ */ new Set());
905
906
  resetToolContinuation();
906
907
  processedToolCalls.current.clear();
907
908
  localResponseMessageIdsRef.current.clear();
@@ -933,6 +934,15 @@ function useAgentChat(options) {
933
934
  })();
934
935
  const pendingConfirmationsRef = useRef(pendingConfirmations);
935
936
  pendingConfirmationsRef.current = pendingConfirmations;
937
+ const [pendingOnToolCallIds, setPendingOnToolCallIds] = useState(() => /* @__PURE__ */ new Set());
938
+ const finishOnToolCall = useCallback((toolCallId) => {
939
+ setPendingOnToolCallIds((prev) => {
940
+ if (!prev.has(toolCallId)) return prev;
941
+ const next = new Set(prev);
942
+ next.delete(toolCallId);
943
+ return next;
944
+ });
945
+ }, []);
936
946
  useEffect(() => {
937
947
  if (!experimental_automaticToolResolution) return;
938
948
  if (isResolvingToolsRef.current) return;
@@ -1035,6 +1045,12 @@ function useAgentChat(options) {
1035
1045
  const toolCallId = part.toolCallId;
1036
1046
  const toolName = getToolName(part);
1037
1047
  processedToolCalls.current.add(toolCallId);
1048
+ setPendingOnToolCallIds((prev) => {
1049
+ if (prev.has(toolCallId)) return prev;
1050
+ const next = new Set(prev);
1051
+ next.add(toolCallId);
1052
+ return next;
1053
+ });
1038
1054
  const addToolOutput = (opts) => {
1039
1055
  sendToolOutputToServer(opts.toolCallId, toolName, opts.output, opts.state, opts.errorText);
1040
1056
  addToolResult({
@@ -1043,19 +1059,29 @@ function useAgentChat(options) {
1043
1059
  output: opts.state === "output-error" ? opts.errorText ?? "Tool execution denied by user" : opts.output
1044
1060
  });
1045
1061
  };
1046
- currentOnToolCall({
1047
- toolCall: {
1048
- toolCallId,
1049
- toolName,
1050
- input: part.input
1051
- },
1052
- addToolOutput
1062
+ let result;
1063
+ try {
1064
+ result = currentOnToolCall({
1065
+ toolCall: {
1066
+ toolCallId,
1067
+ toolName,
1068
+ input: part.input
1069
+ },
1070
+ addToolOutput
1071
+ });
1072
+ } catch (error) {
1073
+ finishOnToolCall(toolCallId);
1074
+ throw error;
1075
+ }
1076
+ Promise.resolve(result).finally(() => {
1077
+ finishOnToolCall(toolCallId);
1053
1078
  });
1054
1079
  }
1055
1080
  }, [
1056
1081
  chatMessages,
1057
1082
  sendToolOutputToServer,
1058
- addToolResult
1083
+ addToolResult,
1084
+ finishOnToolCall
1059
1085
  ]);
1060
1086
  const streamStateRef = useRef({ status: "idle" });
1061
1087
  const [isServerStreaming, setIsServerStreaming] = useState(false);
@@ -1308,15 +1334,15 @@ function useAgentChat(options) {
1308
1334
  }, [sendToolOutputToServer, addToolResult]);
1309
1335
  const lastAssistantMessage = messagesWithToolResults[messagesWithToolResults.length - 1];
1310
1336
  const hasPendingClientToolCalls = (() => {
1311
- const hasOnToolCall = !!onToolCall;
1312
- if (!hasOnToolCall && !tools) return false;
1337
+ if (pendingOnToolCallIds.size === 0 && !tools) return false;
1313
1338
  if (!lastAssistantMessage || lastAssistantMessage.role !== "assistant") return false;
1314
1339
  for (const part of lastAssistantMessage.parts) {
1315
1340
  if (!isToolUIPart(part)) continue;
1316
1341
  if (part.state !== "input-available") continue;
1317
1342
  const toolName = getToolName(part);
1318
1343
  if (toolsRequiringConfirmation.includes(toolName)) continue;
1319
- if (hasOnToolCall || tools?.[toolName]?.execute) return true;
1344
+ if (pendingOnToolCallIds.has(part.toolCallId)) return true;
1345
+ if (tools?.[toolName]?.execute) return true;
1320
1346
  }
1321
1347
  return false;
1322
1348
  })();