diff options
Diffstat (limited to 'src/agentux/providers/base.py')
| -rw-r--r-- | src/agentux/providers/base.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/src/agentux/providers/base.py b/src/agentux/providers/base.py index f20980b..671a79c 100644 --- a/src/agentux/providers/base.py +++ b/src/agentux/providers/base.py @@ -8,17 +8,24 @@ matter of implementing this interface -- the agent loop does not change. from __future__ import annotations from abc import ABC, abstractmethod -from typing import AsyncIterator, List, Optional +from typing import AsyncIterator, Awaitable, Callable, List, Optional from ..config import Config from ..models import Message, ToolDefinition +#: Receives text deltas as they arrive. Async so providers can await the +#: consumer (a terminal write) without blocking the read loop. +TextCallback = Callable[[str], Awaitable[None]] + class BaseProvider(ABC): """A model backend Agentux can drive.""" def __init__(self, config: Config) -> None: self.config = config + #: Token usage reported by the most recent response, or ``None``. + #: Agents read this to show real numbers instead of estimates. + self.last_usage: Optional[Dict[str, int]] = None @abstractmethod async def chat( @@ -67,5 +74,23 @@ class BaseProvider(ABC): if message.content: yield message.content + async def chat_streaming( + self, + messages: List[Message], + tools: Optional[List[ToolDefinition]] = None, + on_text: Optional[TextCallback] = None, + ) -> Message: + """Chat, streaming text deltas to ``on_text`` as they arrive. + + Returns a complete :class:`Message`, so a step that turns out to be a + tool call costs the same single request as a step that is not. That is + the whole point: streaming a step and *then* discovering it was a tool + call means paying for the request twice. + """ + message = await self.chat(messages, tools) + if on_text and message.content: + await on_text(message.content) + return message + -__all__ = ["BaseProvider"] +__all__ = ["BaseProvider", "TextCallback"] |
