diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index b1bfd2f..b80c5d0 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -203,14 +203,7 @@ class AgentLoop: await on_progress(self._tool_hint(response.tool_calls), tool_hint=True) tool_call_dicts = [ - { - "id": tc.id, - "type": "function", - "function": { - "name": tc.name, - "arguments": json.dumps(tc.arguments, ensure_ascii=False) - } - } + tc.to_openai_tool_call() for tc in response.tool_calls ] messages = self.context.add_assistant_message( diff --git a/nanobot/agent/subagent.py b/nanobot/agent/subagent.py index 21b8b32..eb3b3b0 100644 --- a/nanobot/agent/subagent.py +++ b/nanobot/agent/subagent.py @@ -126,14 +126,7 @@ class SubagentManager: if response.has_tool_calls: tool_call_dicts = [ - { - "id": tc.id, - "type": "function", - "function": { - "name": tc.name, - "arguments": json.dumps(tc.arguments, ensure_ascii=False), - }, - } + tc.to_openai_tool_call() for tc in response.tool_calls ] messages.append(build_assistant_message( @@ -222,7 +215,7 @@ Stay focused on the assigned task. Your final response will be reported back to parts.append(f"## Skills\n\nRead SKILL.md with read_file to use a skill.\n\n{skills_summary}") return "\n\n".join(parts) - + async def cancel_by_session(self, session_key: str) -> int: """Cancel all subagents for the given session. Returns count cancelled.""" tasks = [self._running_tasks[tid] for tid in self._session_tasks.get(session_key, []) diff --git a/nanobot/providers/base.py b/nanobot/providers/base.py index d4ea60d..15a10ff 100644 --- a/nanobot/providers/base.py +++ b/nanobot/providers/base.py @@ -1,6 +1,7 @@ """Base LLM provider interface.""" import asyncio +import json from abc import ABC, abstractmethod from dataclasses import dataclass, field from typing import Any @@ -14,6 +15,24 @@ class ToolCallRequest: id: str name: str arguments: dict[str, Any] + provider_specific_fields: dict[str, Any] | None = None + function_provider_specific_fields: dict[str, Any] | None = None + + def to_openai_tool_call(self) -> dict[str, Any]: + """Serialize to an OpenAI-style tool_call payload.""" + tool_call = { + "id": self.id, + "type": "function", + "function": { + "name": self.name, + "arguments": json.dumps(self.arguments, ensure_ascii=False), + }, + } + if self.provider_specific_fields: + tool_call["provider_specific_fields"] = self.provider_specific_fields + if self.function_provider_specific_fields: + tool_call["function"]["provider_specific_fields"] = self.function_provider_specific_fields + return tool_call @dataclass diff --git a/nanobot/providers/litellm_provider.py b/nanobot/providers/litellm_provider.py index cb67635..af91c2f 100644 --- a/nanobot/providers/litellm_provider.py +++ b/nanobot/providers/litellm_provider.py @@ -309,10 +309,17 @@ class LiteLLMProvider(LLMProvider): if isinstance(args, str): args = json_repair.loads(args) + provider_specific_fields = getattr(tc, "provider_specific_fields", None) or None + function_provider_specific_fields = ( + getattr(tc.function, "provider_specific_fields", None) or None + ) + tool_calls.append(ToolCallRequest( id=_short_tool_id(), name=tc.function.name, arguments=args, + provider_specific_fields=provider_specific_fields, + function_provider_specific_fields=function_provider_specific_fields, )) usage = {} diff --git a/tests/test_gemini_thought_signature.py b/tests/test_gemini_thought_signature.py new file mode 100644 index 0000000..bc4132c --- /dev/null +++ b/tests/test_gemini_thought_signature.py @@ -0,0 +1,53 @@ +from types import SimpleNamespace + +from nanobot.providers.base import ToolCallRequest +from nanobot.providers.litellm_provider import LiteLLMProvider + + +def test_litellm_parse_response_preserves_tool_call_provider_fields() -> None: + provider = LiteLLMProvider(default_model="gemini/gemini-3-flash") + + response = SimpleNamespace( + choices=[ + SimpleNamespace( + finish_reason="tool_calls", + message=SimpleNamespace( + content=None, + tool_calls=[ + SimpleNamespace( + id="call_123", + function=SimpleNamespace( + name="read_file", + arguments='{"path":"todo.md"}', + provider_specific_fields={"inner": "value"}, + ), + provider_specific_fields={"thought_signature": "signed-token"}, + ) + ], + ), + ) + ], + usage=None, + ) + + parsed = provider._parse_response(response) + + assert len(parsed.tool_calls) == 1 + assert parsed.tool_calls[0].provider_specific_fields == {"thought_signature": "signed-token"} + assert parsed.tool_calls[0].function_provider_specific_fields == {"inner": "value"} + + +def test_tool_call_request_serializes_provider_fields() -> None: + tool_call = ToolCallRequest( + id="abc123xyz", + name="read_file", + arguments={"path": "todo.md"}, + provider_specific_fields={"thought_signature": "signed-token"}, + function_provider_specific_fields={"inner": "value"}, + ) + + message = tool_call.to_openai_tool_call() + + assert message["provider_specific_fields"] == {"thought_signature": "signed-token"} + assert message["function"]["provider_specific_fields"] == {"inner": "value"} + assert message["function"]["arguments"] == '{"path": "todo.md"}'