fix(agent): preserve interrupted tool-call turns
Keep tool-call assistant messages valid across provider sanitization and avoid trailing user-only history after model errors. This prevents follow-up requests from sending broken tool chains back to the gateway.
This commit is contained in:
@@ -375,6 +375,14 @@ class LLMProvider(ABC):
|
||||
and role in ("user", "assistant")
|
||||
):
|
||||
prev = merged[-1]
|
||||
if role == "assistant":
|
||||
prev_has_tools = bool(prev.get("tool_calls"))
|
||||
curr_has_tools = bool(msg.get("tool_calls"))
|
||||
if curr_has_tools:
|
||||
merged[-1] = dict(msg)
|
||||
continue
|
||||
if prev_has_tools:
|
||||
continue
|
||||
prev_content = prev.get("content") or ""
|
||||
curr_content = msg.get("content") or ""
|
||||
if isinstance(prev_content, str) and isinstance(curr_content, str):
|
||||
|
||||
@@ -243,6 +243,10 @@ class OpenAICompatProvider(LLMProvider):
|
||||
tc_clean["id"] = map_id(tc_clean.get("id"))
|
||||
normalized.append(tc_clean)
|
||||
clean["tool_calls"] = normalized
|
||||
if clean.get("role") == "assistant":
|
||||
# Some OpenAI-compatible gateways reject assistant messages
|
||||
# that mix non-empty content with tool_calls.
|
||||
clean["content"] = None
|
||||
if "tool_call_id" in clean and clean["tool_call_id"]:
|
||||
clean["tool_call_id"] = map_id(clean["tool_call_id"])
|
||||
return self._enforce_role_alternation(sanitized)
|
||||
|
||||
Reference in New Issue
Block a user