sim
sim copied to clipboard
fix(chat): save tool calls
Summary
Save tool calls in messages array correctly.
Type of Change
- [x] Bug fix
Testing
Tested manually.
Checklist
- [x] Code follows project style guidelines
- [x] Self-reviewed my changes
- [x] Tests added/updated and passing
- [x] No new warnings introduced
- [x] I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)
The latest updates on your projects. Learn more about Vercel for GitHub.
Greptile Overview
Greptile Summary
This PR implements proper persistence of tool calls to conversation memory in OpenAI-compatible format. The changes enable the system to save and restore complete conversation turns including tool invocations and their results.
Key Changes:
- Added
buildMessagesForMemory()in agent handler to format tool calls as assistant messages withtool_callsarray and separate tool role messages for results - Implemented
sanitizeMessagesForProvider()in utils to remove orphaned tool messages/calls that lack proper pairing - Updated memory sliding window logic to group messages into complete turns (user + tool calls/results + assistant) to prevent breaking tool call pairs mid-conversation
- Modified all 13 provider implementations to store tool call IDs and raw arguments (
rawArgumentsfield) for accurate persistence - Fixed voice interface race condition by properly checking
isCallEndedRefthroughout all effects and callbacks
Additional Fix:
- Resolved voice mode issue where recognition would restart after call ended due to improper ref usage
Confidence Score: 4/5
- Safe to merge with minor JSON parsing risk in Anthropic provider
- The implementation is well-structured and handles tool call persistence correctly across all providers. The sliding window logic properly groups messages into turns to maintain tool call/result pairs. However, there's one unhandled
JSON.parse()call in the Anthropic provider that could throw on malformed input (line 110-112). This is a known issue from previous review threads but should be addressed to prevent runtime errors. - Pay close attention to
apps/sim/providers/anthropic/index.tsfor the JSON parsing issue
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| apps/sim/app/chat/components/voice-interface/voice-interface.tsx | 5/5 | Fixed race condition by properly using isCallEndRef to prevent voice recognition restart after call ends |
| apps/sim/executor/handlers/agent/agent-handler.ts | 4/5 | Added buildMessagesForMemory() to persist tool calls and results in OpenAI-compatible format with stable IDs |
| apps/sim/executor/handlers/agent/memory.ts | 5/5 | Improved sliding window to group messages into complete turns (user + tool calls/results + assistant) to prevent breaking tool call pairs |
| apps/sim/providers/anthropic/index.ts | 3/5 | Added support for OpenAI-compatible tool message format and preserves original tool use IDs; JSON.parse() needs error handling |
| apps/sim/providers/utils.ts | 5/5 | Added sanitizeMessagesForProvider() to remove orphaned tool messages/calls and ensure proper pairing |
Sequence Diagram
sequenceDiagram
participant User
participant Agent
participant Provider
participant Memory
participant Tool
User->>Agent: Send message with tools enabled
Agent->>Memory: Load conversation history
Memory-->>Agent: Return messages (grouped by turns)
Agent->>Provider: Send request with sanitized messages
Note over Provider: sanitizeMessagesForProvider()<br/>removes orphaned tool calls/results
Provider->>Provider: Generate response with tool_calls
Provider-->>Agent: Response with tool_calls array
Agent->>Agent: buildMessagesForMemory()
Note over Agent: Create assistant message<br/>with tool_calls array
Agent->>Memory: Persist assistant message with tool_calls
loop For each tool call
Agent->>Tool: Execute tool with arguments
Tool-->>Agent: Tool result
Agent->>Agent: Create tool role message
Note over Agent: Include tool_call_id and name
Agent->>Memory: Persist tool result message
end
Agent->>Provider: Continue with tool results
Provider->>Provider: Generate final response
Provider-->>Agent: Final assistant message
Agent->>Agent: buildMessagesForMemory()
Agent->>Memory: Persist final assistant message
Agent-->>User: Return complete response
Done in separate PR. https://github.com/simstudioai/sim/pull/2515