From ed4889134aa77d912d7e0fc0d69f7def0dd772fc Mon Sep 17 00:00:00 2001 From: dev Date: Mon, 5 Oct 2026 16:40:10 +0530 Subject: Working agent before enhancements: 205 tests passing --- src/agentux/models.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/agentux/models.py (limited to 'src/agentux/models.py') diff --git a/src/agentux/models.py b/src/agentux/models.py new file mode 100644 index 0000000..afb17cd --- /dev/null +++ b/src/agentux/models.py @@ -0,0 +1,81 @@ +"""Provider-neutral data types shared by the agent, providers, and tools. + +These deliberately contain no HTTP or provider-specific concepts. A provider +translates its own wire format into these and the agent loop only ever reads +them, which is what keeps the provider layer replaceable. +""" + +from __future__ import annotations + +from typing import Any, Dict, List, Literal, Optional + +from pydantic import BaseModel, Field + +Role = Literal["system", "user", "assistant", "tool"] + + +class ToolCall(BaseModel): + """A structured request from the model to run one tool. + + This is only ever populated from a provider's native function-calling + mechanism -- never from text the model printed. + """ + + id: str + name: str + arguments: Dict[str, Any] = Field(default_factory=dict) + + +class Message(BaseModel): + """One turn of the conversation.""" + + role: Role + content: Optional[str] = None + tool_calls: Optional[List[ToolCall]] = None + tool_call_id: Optional[str] = None + + def to_dict(self) -> Dict[str, Any]: + """Serialize to the OpenAI chat-completions wire shape.""" + payload: Dict[str, Any] = {"role": self.role} + if self.content is not None: + payload["content"] = self.content + if self.tool_calls: + payload["tool_calls"] = [ + { + "id": call.id, + "type": "function", + "function": { + "name": call.name, + "arguments": _dumps(call.arguments), + }, + } + for call in self.tool_calls + ] + if self.tool_call_id: + payload["tool_call_id"] = self.tool_call_id + return payload + + +class ToolDefinition(BaseModel): + """A tool advertised to the model. + + ``parameters`` is a JSON Schema object describing the accepted arguments. + """ + + name: str + description: str + parameters: Dict[str, Any] + + +class ToolResult(BaseModel): + """The outcome of running a tool, ready to be sent back to the model.""" + + tool_call_id: str + output: str + is_error: bool = False + + +def _dumps(value: Any) -> str: + import json + + return json.dumps(value) -- cgit v1.2.3