reactbridge-sdk 0.2.12 → 0.2.13

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/index.js CHANGED
@@ -516,26 +516,52 @@ function useReactBridge({ onIntentDetected, currentContext, onError, onSpeechSta
516
516
  // Step 2: If there's a tool call, execute it
517
517
  if (response.toolCall && onIntentDetected) {
518
518
  try {
519
- const toolResult = yield onIntentDetected(response.toolCall);
520
- // Step 3: Send tool result back to orchestrator
521
- if (lastRequestRef.current && response.sessionId) {
522
- const resultResponse = yield api.sendToolResult(response.sessionId, toolResult, lastRequestRef.current);
523
- // Add final assistant message with tool result context
524
- const finalMessage = {
525
- id: `assistant-final-${Date.now()}`,
526
- role: "assistant",
527
- content: resultResponse.message,
528
- timestamp: new Date(),
529
- };
530
- setMessages((prev) => [...prev, finalMessage]);
531
- // Trigger TTS for final response only if input came from voice
532
- if (fromVoiceInput) {
533
- ttsProvider.speak(resultResponse.message);
519
+ // Process tool calls in a loop to support chained/sequential tool execution
520
+ let currentToolCall = response.toolCall;
521
+ let currentResponse = response;
522
+ let toolCallCount = 0;
523
+ const maxToolCalls = 50; // Prevent infinite loops
524
+ while (currentToolCall && toolCallCount < maxToolCalls) {
525
+ toolCallCount++;
526
+ // Execute the current tool
527
+ const toolResult = yield onIntentDetected(currentToolCall);
528
+ // Step 3: Send tool result back to orchestrator
529
+ if (lastRequestRef.current && currentResponse.sessionId) {
530
+ const resultResponse = yield api.sendToolResult(currentResponse.sessionId, toolResult, lastRequestRef.current);
531
+ // Add assistant message with tool result context
532
+ const finalMessage = {
533
+ id: `assistant-final-${Date.now()}-${toolCallCount}`,
534
+ role: "assistant",
535
+ content: resultResponse.message,
536
+ timestamp: new Date(),
537
+ toolCall: resultResponse.toolCall,
538
+ };
539
+ setMessages((prev) => [...prev, finalMessage]);
540
+ // Trigger TTS for final response only if input came from voice (first iteration only)
541
+ if (fromVoiceInput && toolCallCount === 1) {
542
+ ttsProvider.speak(resultResponse.message);
543
+ }
544
+ if (onAgentResponse && toolCallCount === 1) {
545
+ onAgentResponse(resultResponse.message);
546
+ }
547
+ // Check if there's another tool call in the response (chained execution)
548
+ if (resultResponse.toolCall) {
549
+ currentToolCall = resultResponse.toolCall;
550
+ currentResponse = resultResponse;
551
+ }
552
+ else {
553
+ // No more tool calls, exit loop
554
+ currentToolCall = null;
555
+ }
534
556
  }
535
- if (onAgentResponse) {
536
- onAgentResponse(resultResponse.message);
557
+ else {
558
+ // Cannot send result, exit loop
559
+ currentToolCall = null;
537
560
  }
538
561
  }
562
+ if (toolCallCount >= maxToolCalls) {
563
+ console.warn("Tool call loop exceeded maximum iterations. Possible infinite loop detected.");
564
+ }
539
565
  }
540
566
  catch (toolError) {
541
567
  const errorMessage = toolError instanceof Error