ai-tui-shell 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. ai_tui_shell/__init__.py +14 -0
  2. ai_tui_shell/__main__.py +14 -0
  3. ai_tui_shell/ai/__init__.py +237 -0
  4. ai_tui_shell/ai/anthropic_provider.py +403 -0
  5. ai_tui_shell/ai/conversation_store.py +238 -0
  6. ai_tui_shell/ai/models.py +120 -0
  7. ai_tui_shell/ai/ollama_provider.py +251 -0
  8. ai_tui_shell/ai/openai_provider.py +335 -0
  9. ai_tui_shell/ai/retry.py +262 -0
  10. ai_tui_shell/ai/stub_provider.py +162 -0
  11. ai_tui_shell/ai/token_budget.py +140 -0
  12. ai_tui_shell/ai/tools.py +500 -0
  13. ai_tui_shell/app.py +681 -0
  14. ai_tui_shell/batch/__init__.py +37 -0
  15. ai_tui_shell/batch/aggregator.py +198 -0
  16. ai_tui_shell/batch/executor.py +456 -0
  17. ai_tui_shell/batch/library.py +157 -0
  18. ai_tui_shell/batch/models.py +341 -0
  19. ai_tui_shell/batch/picker.py +155 -0
  20. ai_tui_shell/batch/placeholders.py +275 -0
  21. ai_tui_shell/batch/repository.py +90 -0
  22. ai_tui_shell/cli.py +765 -0
  23. ai_tui_shell/config/__init__.py +14 -0
  24. ai_tui_shell/config/db.py +204 -0
  25. ai_tui_shell/config/migrations.py +186 -0
  26. ai_tui_shell/config/paths.py +80 -0
  27. ai_tui_shell/config/secrets.py +272 -0
  28. ai_tui_shell/config/settings.py +248 -0
  29. ai_tui_shell/core/__init__.py +12 -0
  30. ai_tui_shell/core/event_bus.py +307 -0
  31. ai_tui_shell/core/logging_setup.py +165 -0
  32. ai_tui_shell/core/proxy_env.py +47 -0
  33. ai_tui_shell/crypto/__init__.py +11 -0
  34. ai_tui_shell/crypto/audit_chain.py +168 -0
  35. ai_tui_shell/domain/ai.py +349 -0
  36. ai_tui_shell/domain/skill.py +170 -0
  37. ai_tui_shell/hosts/__init__.py +14 -0
  38. ai_tui_shell/hosts/manager.py +94 -0
  39. ai_tui_shell/hosts/models.py +125 -0
  40. ai_tui_shell/hosts/repository.py +87 -0
  41. ai_tui_shell/policy/__init__.py +31 -0
  42. ai_tui_shell/policy/approval_cache.py +144 -0
  43. ai_tui_shell/policy/ast_check.py +242 -0
  44. ai_tui_shell/policy/audit.py +115 -0
  45. ai_tui_shell/policy/audit_middleware.py +314 -0
  46. ai_tui_shell/policy/engine.py +353 -0
  47. ai_tui_shell/policy/llm_classifier.py +221 -0
  48. ai_tui_shell/policy/models.py +200 -0
  49. ai_tui_shell/policy/modes.py +117 -0
  50. ai_tui_shell/policy/static_rules.py +172 -0
  51. ai_tui_shell/services/__init__.py +9 -0
  52. ai_tui_shell/services/ai_service.py +523 -0
  53. ai_tui_shell/services/policy_service.py +226 -0
  54. ai_tui_shell/sftp/__init__.py +12 -0
  55. ai_tui_shell/sftp/transfer_queue.py +339 -0
  56. ai_tui_shell/skills/__init__.py +18 -0
  57. ai_tui_shell/skills/builtin.py +83 -0
  58. ai_tui_shell/skills/installer.py +129 -0
  59. ai_tui_shell/skills/library.py +210 -0
  60. ai_tui_shell/skills/manager.py +115 -0
  61. ai_tui_shell/ssh/__init__.py +15 -0
  62. ai_tui_shell/ssh/auth.py +131 -0
  63. ai_tui_shell/ssh/client.py +221 -0
  64. ai_tui_shell/ssh/keepalive.py +123 -0
  65. ai_tui_shell/ssh/known_hosts.py +138 -0
  66. ai_tui_shell/ssh/pty_session.py +516 -0
  67. ai_tui_shell/ssh/sftp_session.py +780 -0
  68. ai_tui_shell/terminal/__init__.py +10 -0
  69. ai_tui_shell/terminal/screen.py +252 -0
  70. ai_tui_shell/ui/__init__.py +5 -0
  71. ai_tui_shell/ui/components.py +400 -0
  72. ai_tui_shell/ui/css/app.tcss +34 -0
  73. ai_tui_shell/ui/css/terminal.tcss +18 -0
  74. ai_tui_shell/ui/design.py +371 -0
  75. ai_tui_shell/ui/keybindings.py +46 -0
  76. ai_tui_shell/ui/rabbit_art.py +928 -0
  77. ai_tui_shell/ui/screens/__init__.py +5 -0
  78. ai_tui_shell/ui/screens/ai_panel.py +352 -0
  79. ai_tui_shell/ui/screens/approval_prompt.py +212 -0
  80. ai_tui_shell/ui/screens/batch_modal.py +665 -0
  81. ai_tui_shell/ui/screens/host_manager.py +651 -0
  82. ai_tui_shell/ui/screens/main_screen.py +1300 -0
  83. ai_tui_shell/ui/screens/settings_screen.py +945 -0
  84. ai_tui_shell/ui/screens/sftp_browser.py +415 -0
  85. ai_tui_shell/ui/screens/skill_manager.py +232 -0
  86. ai_tui_shell/ui/text.py +222 -0
  87. ai_tui_shell/ui/theme.py +132 -0
  88. ai_tui_shell/ui/widgets/__init__.py +5 -0
  89. ai_tui_shell/ui/widgets/ai_sidebar.py +402 -0
  90. ai_tui_shell/ui/widgets/panel_resize_handle.py +148 -0
  91. ai_tui_shell/ui/widgets/rabbit_stage.py +497 -0
  92. ai_tui_shell/ui/widgets/sftp_tree.py +227 -0
  93. ai_tui_shell/ui/widgets/terminal_view.py +365 -0
  94. ai_tui_shell/ui/widgets/transfer_queue.py +138 -0
  95. ai_tui_shell/version.py +12 -0
  96. ai_tui_shell-0.1.0.dist-info/METADATA +180 -0
  97. ai_tui_shell-0.1.0.dist-info/RECORD +101 -0
  98. ai_tui_shell-0.1.0.dist-info/WHEEL +4 -0
  99. ai_tui_shell-0.1.0.dist-info/entry_points.txt +3 -0
  100. ai_tui_shell-0.1.0.dist-info/licenses/LICENSE +21 -0
  101. ai_tui_shell-0.1.0.dist-info/licenses/NOTICE +27 -0
@@ -0,0 +1,14 @@
1
+ """AI TUI Shell — AI-integrated secure SSH terminal emulator.
2
+
3
+ Top-level package marker. Importing submodules should not pull in heavy
4
+ dependencies (textual, asyncssh, fastapi) until they're actually needed;
5
+ use ``ai_tui_shell.app`` for the Textual entry point.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ __version__ = "0.1.0"
11
+ __app_name__ = "ai-tui-shell"
12
+
13
+ # Re-export public API lazily; nothing heavy at import time.
14
+ __all__ = ["__app_name__", "__version__"]
@@ -0,0 +1,14 @@
1
+ """Entry point for ``python -m ai_tui_shell``.
2
+
3
+ Delegates to :func:`ai_tui_shell.cli.main` so behavior is identical whether
4
+ invoked via the script entry point or ``python -m``.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import sys
10
+
11
+ from ai_tui_shell.cli import main
12
+
13
+ if __name__ == "__main__":
14
+ sys.exit(main())
@@ -0,0 +1,237 @@
1
+ """AI provider abstraction layer (M12).
2
+
3
+ Everything provider-specific (Anthropic, OpenAI, Ollama) is contained in
4
+ submodules; the rest of the codebase sees only :class:`AIProvider` and
5
+ the dataclasses in :mod:`ai_tui_shell.domain.ai`. New providers slot in by
6
+ adding a module that implements :class:`AIProvider` and registering it
7
+ in :func:`build_provider`.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import logging
13
+ from collections.abc import AsyncIterator
14
+ from typing import Any
15
+
16
+ from ai_tui_shell.config.settings import AISettings
17
+ from ai_tui_shell.domain.ai import (
18
+ ChatChunk,
19
+ ChatRequest,
20
+ ChatResponse,
21
+ )
22
+
23
+ _logger = logging.getLogger(__name__)
24
+
25
+
26
+ class AIError(RuntimeError):
27
+ """Base class for provider failures.
28
+
29
+ Subclasses:
30
+
31
+ * :class:`AIConfigError` — missing/invalid configuration. Not retried.
32
+ * :class:`AIRateLimitError` — provider asked us to back off. Retried.
33
+ * :class:`AITransientError` — network/5xx. Retried.
34
+ * :class:`AIResponseError` — provider returned an unparseable payload.
35
+ """
36
+
37
+ def __init__(self, message: str, *, retry_after_s: float | None = None) -> None:
38
+ super().__init__(message)
39
+ self.retry_after_s = retry_after_s
40
+
41
+
42
+ class AIConfigError(AIError):
43
+ """Missing API key, bad model, etc. Don't retry."""
44
+
45
+
46
+ class AIRateLimitError(AIError):
47
+ """Provider asked us to slow down (429)."""
48
+
49
+
50
+ class AITransientError(AIError):
51
+ """Network glitch or 5xx. Retry with backoff."""
52
+
53
+
54
+ class AIResponseError(AIError):
55
+ """Provider returned a body we couldn't parse. Don't retry."""
56
+
57
+
58
+ class AIProvider:
59
+ """Abstract provider interface.
60
+
61
+ Concrete providers (Anthropic, OpenAI, Ollama, Stub) override the
62
+ internal ``_stream`` and ``_complete`` methods. The public API:
63
+
64
+ * :meth:`complete` — one-shot, returns a full :class:`ChatResponse`.
65
+ * :meth:`stream` — yields :class:`ChatChunk` then a final sentinel.
66
+
67
+ Both methods apply the same retry policy (see :mod:`ai_tui_shell.ai.retry`)
68
+ and the same context-window trimming (see :mod:`ai_tui_shell.ai.token_budget`).
69
+ """
70
+
71
+ name: str = "abstract"
72
+
73
+ def __init__(self, *, model: str, timeout_s: float = 30.0, max_retries: int = 3) -> None:
74
+ if not model:
75
+ raise AIConfigError("model name is required")
76
+ self.model = model
77
+ self.timeout_s = float(timeout_s)
78
+ self.max_retries = int(max_retries)
79
+
80
+ # -- public API ---------------------------------------------------------
81
+
82
+ async def complete(self, request: ChatRequest) -> ChatResponse:
83
+ """Run a single non-streaming completion."""
84
+ from ai_tui_shell.ai.retry import with_retry
85
+ from ai_tui_shell.ai.token_budget import trim_history
86
+
87
+ trimmed = trim_history(request)
88
+ return await with_retry(
89
+ lambda: self._complete(trimmed),
90
+ max_retries=self.max_retries,
91
+ timeout_s=self.timeout_s,
92
+ provider_name=self.name,
93
+ )
94
+
95
+ async def stream(self, request: ChatRequest) -> AsyncIterator[ChatChunk]:
96
+ """Yield :class:`ChatChunk` instances as the model produces them.
97
+
98
+ The final chunk has ``finish_reason`` set; the iterator never
99
+ yields the synthetic final assistant message (call
100
+ :meth:`complete` if you need the aggregated response).
101
+ """
102
+ from ai_tui_shell.ai.retry import with_retry_stream
103
+ from ai_tui_shell.ai.token_budget import trim_history
104
+
105
+ trimmed = trim_history(request)
106
+ async for chunk in with_retry_stream(
107
+ lambda: self._stream(trimmed),
108
+ max_retries=self.max_retries,
109
+ timeout_s=self.timeout_s,
110
+ provider_name=self.name,
111
+ ):
112
+ yield chunk
113
+
114
+ # -- internal hooks (override) ------------------------------------------
115
+
116
+ async def _complete(self, request: ChatRequest) -> ChatResponse:
117
+ raise NotImplementedError
118
+
119
+ async def _stream(self, request: ChatRequest) -> AsyncIterator[ChatChunk]:
120
+ raise NotImplementedError
121
+ yield # pragma: no cover # makes this an async generator
122
+
123
+ # -- diagnostics --------------------------------------------------------
124
+
125
+ def describe(self) -> dict[str, Any]:
126
+ """Short description used by the settings screen and audit logs."""
127
+ return {"name": self.name, "model": self.model, "timeout_s": self.timeout_s}
128
+
129
+
130
+ # ---------------------------------------------------------------------------
131
+ # Provider factory
132
+ # ---------------------------------------------------------------------------
133
+
134
+
135
+ def build_provider(
136
+ settings: AISettings,
137
+ *,
138
+ api_key: str | None = None,
139
+ force: str | None = None,
140
+ ) -> AIProvider:
141
+ """Return an :class:`AIProvider` instance per ``settings.provider``.
142
+
143
+ Falls back to :class:`StubProvider` when no real key is available so
144
+ that CI / unit tests can exercise the chat path without a network.
145
+ """
146
+ import os
147
+
148
+ provider_name = (force or settings.provider).lower()
149
+ key = api_key if api_key is not None else os.environ.get("SHELLFORGE_AI_API_KEY")
150
+
151
+ # The endpoint override comes from the settings; an empty value
152
+ # means "provider default".
153
+ base_url: str | None = settings.base_url.strip() or None
154
+
155
+ if provider_name == "anthropic":
156
+ from ai_tui_shell.ai.anthropic_provider import AnthropicChatProvider
157
+
158
+ if not key:
159
+ from ai_tui_shell.ai.stub_provider import StubProvider
160
+
161
+ _logger.warning("no anthropic API key (keychain / SHELLFORGE_AI_API_KEY); using stub provider")
162
+ return StubProvider(model=settings.model)
163
+ return AnthropicChatProvider(
164
+ model=settings.model,
165
+ api_key=key,
166
+ timeout_s=max(settings.classify_timeout_seconds * 5, 30.0),
167
+ max_retries=3,
168
+ base_url=base_url,
169
+ )
170
+ if provider_name == "openai":
171
+ from ai_tui_shell.ai.openai_provider import OpenAIChatProvider
172
+
173
+ if not key:
174
+ from ai_tui_shell.ai.stub_provider import StubProvider
175
+
176
+ _logger.warning("no openai API key (keychain / SHELLFORGE_AI_API_KEY); using stub provider")
177
+ return StubProvider(model=settings.model)
178
+ return OpenAIChatProvider(
179
+ model=settings.model,
180
+ api_key=key,
181
+ timeout_s=max(settings.classify_timeout_seconds * 5, 30.0),
182
+ max_retries=3,
183
+ base_url=base_url,
184
+ )
185
+ if provider_name == "ollama":
186
+ from ai_tui_shell.ai.ollama_provider import DEFAULT_BASE_URL as DEFAULT_OLLAMA_BASE_URL
187
+ from ai_tui_shell.ai.ollama_provider import OllamaChatProvider
188
+
189
+ return OllamaChatProvider(
190
+ model=settings.model,
191
+ timeout_s=max(settings.classify_timeout_seconds * 5, 30.0),
192
+ max_retries=2,
193
+ base_url=base_url or DEFAULT_OLLAMA_BASE_URL,
194
+ )
195
+ # Unknown provider or explicit stub: use stub.
196
+ from ai_tui_shell.ai.stub_provider import StubProvider
197
+
198
+ _logger.warning("unknown provider %r; falling back to stub", provider_name)
199
+ return StubProvider(model=settings.model)
200
+
201
+
202
+ def supported_params(func: Any) -> frozenset[str]:
203
+ """Return the keyword names ``func`` accepts.
204
+
205
+ SDKs drop parameters between major versions (Anthropic removed
206
+ ``temperature`` in 2.x); sending one raises ``TypeError`` and breaks
207
+ every request. Providers filter their payload with this.
208
+
209
+ Returns an empty set when the signature cannot be inspected, in
210
+ which case the caller should send the payload unchanged.
211
+ """
212
+ import inspect
213
+
214
+ try:
215
+ signature = inspect.signature(func)
216
+ except (TypeError, ValueError): # pragma: no cover — exotic SDK builds
217
+ return frozenset()
218
+ params = {
219
+ name
220
+ for name, param in signature.parameters.items()
221
+ if param.kind is not inspect.Parameter.VAR_KEYWORD
222
+ }
223
+ if not params:
224
+ return frozenset()
225
+ return frozenset(params)
226
+
227
+
228
+ __all__ = [
229
+ "AIConfigError",
230
+ "AIError",
231
+ "AIProvider",
232
+ "AIRateLimitError",
233
+ "AIResponseError",
234
+ "AITransientError",
235
+ "build_provider",
236
+ "supported_params",
237
+ ]
@@ -0,0 +1,403 @@
1
+ """Anthropic Messages API provider (FR-4.1, FR-4.7, FR-4.8).
2
+
3
+ Translates :class:`ai_tui_shell.domain.ai.ChatRequest` into the
4
+ ``anthropic.AsyncAnthropic.messages`` shape and decodes the response
5
+ back into our wire types. The SDK is imported lazily so unit tests
6
+ that never exercise this module don't pay the import cost.
7
+
8
+ Key translation notes:
9
+
10
+ * System messages are sent as the top-level ``system`` field, never
11
+ inside ``messages`` (Anthropic rejects that).
12
+ * Tool definitions use ``input_schema`` for parameters.
13
+ * Streaming uses the SDK's ``messages.stream`` async context manager
14
+ and converts ``MessageStartEvent`` / ``ContentBlockStartEvent`` /
15
+ ``ContentBlockDeltaEvent`` / ``MessageStopEvent`` to :class:`ChatChunk`.
16
+ * The SDK raises ``APIStatusError`` for 4xx/5xx; we map those into our
17
+ error hierarchy via :func:`ai_tui_shell.ai.retry._classify`.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import json
23
+ import logging
24
+ import time
25
+ from collections.abc import AsyncIterator
26
+ from functools import lru_cache
27
+ from typing import Any
28
+
29
+ from ai_tui_shell.ai import (
30
+ AIConfigError,
31
+ AIProvider,
32
+ AIRateLimitError,
33
+ AITransientError,
34
+ supported_params,
35
+ )
36
+ from ai_tui_shell.domain.ai import (
37
+ ChatChunk,
38
+ ChatRequest,
39
+ ChatResponse,
40
+ FinishReason,
41
+ Message,
42
+ Role,
43
+ ToolInvocation,
44
+ )
45
+
46
+ _logger = logging.getLogger(__name__)
47
+
48
+
49
+ # Map Anthropic ``stop_reason`` values to our :class:`FinishReason`.
50
+ _STOP_REASON_MAP = {
51
+ "end_turn": FinishReason.STOP,
52
+ "stop_sequence": FinishReason.STOP,
53
+ "max_tokens": FinishReason.LENGTH,
54
+ "tool_use": FinishReason.TOOL_USE,
55
+ }
56
+
57
+ # Extended-thinking token budgets per reasoning effort level.
58
+ _THINKING_BUDGETS = {"low": 2048, "medium": 8192, "high": 16384}
59
+
60
+
61
+ @lru_cache(maxsize=1)
62
+ def _sdk_supported_params() -> tuple[frozenset[str], frozenset[str]]:
63
+ """The keywords ``messages.create`` and ``messages.stream`` accept.
64
+
65
+ Empty sets when the SDK is not installed — the payload is then sent
66
+ unchanged.
67
+ """
68
+ try:
69
+ from anthropic.resources.messages.messages import AsyncMessages
70
+ except Exception: # pragma: no cover — SDK not installed
71
+ return frozenset(), frozenset()
72
+ return supported_params(AsyncMessages.create), supported_params(AsyncMessages.stream)
73
+
74
+
75
+ class AnthropicChatProvider(AIProvider):
76
+ """Anthropic Messages API implementation."""
77
+
78
+ name = "anthropic"
79
+
80
+ def __init__(
81
+ self,
82
+ *,
83
+ model: str,
84
+ api_key: str,
85
+ timeout_s: float = 30.0,
86
+ max_retries: int = 3,
87
+ base_url: str | None = None,
88
+ ) -> None:
89
+ if not api_key:
90
+ raise AIConfigError("Anthropic API key is required")
91
+ super().__init__(model=model, timeout_s=timeout_s, max_retries=max_retries)
92
+ self._api_key = api_key
93
+ self._base_url = base_url
94
+ self._client: Any | None = None
95
+
96
+ # -- public API ---------------------------------------------------------
97
+
98
+ async def _complete(self, request: ChatRequest) -> ChatResponse:
99
+ client = self._get_client()
100
+ started = time.monotonic()
101
+ payload = self._build_payload(request, stream=False)
102
+ try:
103
+ response = await client.messages.create(**payload)
104
+ except Exception as exc:
105
+ raise self._translate_exception(exc) from exc
106
+ latency_ms = int((time.monotonic() - started) * 1000)
107
+ message = self._parse_response(response)
108
+ return ChatResponse(
109
+ message=message,
110
+ finish_reason=_STOP_REASON_MAP.get(
111
+ getattr(response, "stop_reason", None) or "",
112
+ FinishReason.STOP,
113
+ ),
114
+ input_tokens=int(getattr(response.usage, "input_tokens", 0) or 0),
115
+ output_tokens=int(getattr(response.usage, "output_tokens", 0) or 0),
116
+ raw=_safe_raw(response),
117
+ latency_ms=latency_ms,
118
+ )
119
+
120
+ async def _stream(self, request: ChatRequest) -> AsyncIterator[ChatChunk]:
121
+ client = self._get_client()
122
+ payload = self._build_payload(request, stream=True)
123
+ try:
124
+ async with client.messages.stream(**payload) as stream:
125
+ async for event in stream:
126
+ chunk = self._translate_event(event)
127
+ if chunk is not None:
128
+ yield chunk
129
+ # The SDK merges the streamed JSON fragments of a tool
130
+ # call; the events alone never carry its arguments.
131
+ final = await stream.get_final_message()
132
+ except Exception as exc:
133
+ raise self._translate_exception(exc) from exc
134
+ for block in getattr(final, "content", []) or []:
135
+ if getattr(block, "type", None) != "tool_use":
136
+ continue
137
+ yield ChatChunk(
138
+ delta="",
139
+ tool_invocation=ToolInvocation(
140
+ call_id=str(getattr(block, "id", "")),
141
+ name=str(getattr(block, "name", "")),
142
+ arguments=dict(getattr(block, "input", {}) or {}),
143
+ ),
144
+ )
145
+ thinking = [
146
+ _thinking_block_dict(block)
147
+ for block in getattr(final, "content", []) or []
148
+ if getattr(block, "type", None) in {"thinking", "redacted_thinking"}
149
+ ]
150
+ if thinking:
151
+ yield ChatChunk(delta="", thinking_blocks=thinking)
152
+
153
+ # -- payload translation ------------------------------------------------
154
+
155
+ def _build_payload(self, request: ChatRequest, *, stream: bool) -> dict[str, Any]:
156
+ thinking_enabled = request.reasoning_effort != "off"
157
+ messages: list[dict[str, Any]] = []
158
+ for msg in request.messages:
159
+ if msg.role is Role.SYSTEM:
160
+ # System goes into the top-level field, not messages.
161
+ continue
162
+ if msg.role is Role.TOOL:
163
+ # Tool results become ``tool_result`` content blocks on a
164
+ # synthetic ``user`` turn — Anthropic requires the tool
165
+ # result block to be wrapped in a user message.
166
+ messages.append(
167
+ {
168
+ "role": "user",
169
+ "content": [
170
+ {
171
+ "type": "tool_result",
172
+ "tool_use_id": r.call_id,
173
+ "content": r.content,
174
+ "is_error": r.error,
175
+ }
176
+ for r in msg.tool_results
177
+ ],
178
+ }
179
+ )
180
+ continue
181
+ if msg.role is Role.ASSISTANT:
182
+ blocks: list[dict[str, Any]] = []
183
+ # Extended thinking requires the previous turn's thinking
184
+ # blocks to be replayed verbatim before text / tool_use.
185
+ if thinking_enabled:
186
+ blocks.extend(msg.thinking_blocks)
187
+ if msg.content:
188
+ blocks.append({"type": "text", "text": msg.content})
189
+ for inv in msg.tool_invocations:
190
+ blocks.append(
191
+ {
192
+ "type": "tool_use",
193
+ "id": inv.call_id,
194
+ "name": inv.name,
195
+ "input": inv.arguments,
196
+ }
197
+ )
198
+ if not blocks:
199
+ blocks.append({"type": "text", "text": ""})
200
+ messages.append({"role": "assistant", "content": blocks})
201
+ continue
202
+ # USER
203
+ messages.append({"role": "user", "content": msg.content})
204
+
205
+ payload: dict[str, Any] = {
206
+ "model": request.model or self.model,
207
+ "max_tokens": request.max_tokens,
208
+ "temperature": request.temperature,
209
+ "messages": messages,
210
+ }
211
+ if request.system:
212
+ payload["system"] = request.system
213
+ if request.tools:
214
+ payload["tools"] = [
215
+ {
216
+ "name": t.name,
217
+ "description": t.description,
218
+ "input_schema": t.parameters or {"type": "object", "properties": {}},
219
+ }
220
+ for t in request.tools
221
+ ]
222
+ if request.metadata:
223
+ payload["metadata"] = dict(request.metadata)
224
+ if thinking_enabled:
225
+ budget = _THINKING_BUDGETS[request.reasoning_effort]
226
+ payload["thinking"] = {"type": "enabled", "budget_tokens": budget}
227
+ # The budget must fit inside max_tokens, and thinking forces
228
+ # temperature to 1 (the 2.x SDK drops the key entirely).
229
+ payload["max_tokens"] = max(payload["max_tokens"], budget + 2048)
230
+ payload["temperature"] = 1.0
231
+ return self._filter_payload(payload, streaming=stream)
232
+
233
+ def _filter_payload(self, payload: dict[str, Any], *, streaming: bool) -> dict[str, Any]:
234
+ """Drop keys the installed SDK does not accept.
235
+
236
+ The SDK is free to remove parameters between major versions
237
+ (``temperature`` is gone in 2.x); sending one raises a
238
+ ``TypeError`` that would otherwise break every request. The two
239
+ call sites take different arguments, so the filter is chosen
240
+ per path.
241
+ """
242
+ create_params, stream_params = _sdk_supported_params()
243
+ supported = stream_params if streaming else create_params
244
+ if not supported:
245
+ return payload
246
+ dropped = [key for key in payload if key not in supported]
247
+ if dropped:
248
+ _logger.debug("dropping unsupported Anthropic params: %s", dropped)
249
+ return {key: value for key, value in payload.items() if key in supported}
250
+
251
+ def _parse_response(self, response: Any) -> Message:
252
+ """Build a :class:`Message` from a non-streaming response."""
253
+ text_parts: list[str] = []
254
+ tool_invocations: list[ToolInvocation] = []
255
+ thinking_blocks: list[dict[str, Any]] = []
256
+ for block in getattr(response, "content", []) or []:
257
+ btype = getattr(block, "type", None)
258
+ if btype == "text":
259
+ text_parts.append(getattr(block, "text", "") or "")
260
+ elif btype == "tool_use":
261
+ tool_invocations.append(
262
+ ToolInvocation(
263
+ call_id=str(getattr(block, "id", "")),
264
+ name=str(getattr(block, "name", "")),
265
+ arguments=dict(getattr(block, "input", {}) or {}),
266
+ )
267
+ )
268
+ elif btype in {"thinking", "redacted_thinking"}:
269
+ thinking_blocks.append(_thinking_block_dict(block))
270
+ return Message(
271
+ role=Role.ASSISTANT,
272
+ content="".join(text_parts),
273
+ tool_invocations=tool_invocations,
274
+ thinking_blocks=thinking_blocks,
275
+ )
276
+
277
+ def _translate_event(self, event: Any) -> ChatChunk | None:
278
+ """Translate a single Anthropic streaming event to :class:`ChatChunk`."""
279
+ etype = getattr(event, "type", None) or getattr(event, "event", None)
280
+ if etype == "content_block_start":
281
+ # Tool calls are reported from the SDK's final message —
282
+ # the start event carries no arguments.
283
+ return None
284
+ if etype == "content_block_delta":
285
+ delta = getattr(event, "delta", None)
286
+ if delta is None:
287
+ return None
288
+ if getattr(delta, "type", None) == "text_delta":
289
+ return ChatChunk(delta=str(getattr(delta, "text", "") or ""))
290
+ if getattr(delta, "type", None) == "input_json_delta":
291
+ # Partial JSON for a tool invocation; merge happens in
292
+ # the caller-side accumulator (out of scope here).
293
+ return None
294
+ return None
295
+ if etype == "message_delta":
296
+ stop_reason = getattr(event, "delta", None)
297
+ reason = (
298
+ _STOP_REASON_MAP.get(
299
+ getattr(stop_reason, "stop_reason", None) or "",
300
+ FinishReason.STOP,
301
+ )
302
+ if stop_reason is not None
303
+ else FinishReason.STOP
304
+ )
305
+ return ChatChunk(delta="", finish_reason=reason)
306
+ if etype == "message_stop":
307
+ return None
308
+ if etype == "message_start":
309
+ return None
310
+ return None
311
+
312
+ # -- client plumbing ---------------------------------------------------
313
+
314
+ def _get_client(self) -> Any:
315
+ if self._client is None:
316
+ try:
317
+ from anthropic import AsyncAnthropic
318
+ except ImportError as exc:
319
+ raise AIConfigError(f"anthropic SDK not installed: {exc}") from exc
320
+ kwargs: dict[str, Any] = {"api_key": self._api_key, "timeout": self.timeout_s}
321
+ if self._base_url:
322
+ kwargs["base_url"] = self._base_url
323
+ self._client = AsyncAnthropic(**kwargs)
324
+ return self._client
325
+
326
+ @staticmethod
327
+ def _translate_exception(exc: BaseException) -> BaseException:
328
+ """Map SDK exceptions to our hierarchy."""
329
+ name = exc.__class__.__name__.lower()
330
+ if "ratelimit" in name:
331
+ retry_after = getattr(exc, "retry_after", None)
332
+ return AIRateLimitError(
333
+ str(exc), retry_after_s=float(retry_after) if retry_after else None
334
+ )
335
+ if "timeout" in name or "connection" in name:
336
+ return AITransientError(str(exc))
337
+ if "auth" in name or "permission" in name:
338
+ return AIConfigError(str(exc))
339
+ if "status" in name:
340
+ status = getattr(exc, "status_code", None)
341
+ if status in {429}:
342
+ return AIRateLimitError(str(exc))
343
+ if status and 500 <= int(status) <= 599:
344
+ return AITransientError(str(exc))
345
+ return AIConfigError(str(exc))
346
+ return exc
347
+
348
+
349
+ def _thinking_block_dict(block: Any) -> dict[str, Any]:
350
+ """Snapshot an SDK thinking block as a plain replayable dict."""
351
+ if getattr(block, "type", None) == "redacted_thinking":
352
+ return {"type": "redacted_thinking", "data": getattr(block, "data", "") or ""}
353
+ return {
354
+ "type": "thinking",
355
+ "thinking": getattr(block, "thinking", "") or "",
356
+ "signature": getattr(block, "signature", "") or "",
357
+ }
358
+
359
+
360
+ def _safe_raw(response: Any) -> dict[str, Any]:
361
+ """Render the response into a JSON-safe dict (capped)."""
362
+ try:
363
+ text = json.dumps(_to_jsonable(response), default=str)
364
+ if len(text) > 64 * 1024:
365
+ return {"_truncated": True, "size": len(text)}
366
+ result: Any = json.loads(text)
367
+ return result if isinstance(result, dict) else {"_unparseable": True}
368
+ except Exception: # pragma: no cover — defensive
369
+ return {"_unparseable": True}
370
+
371
+
372
+ def _to_jsonable(obj: Any) -> Any:
373
+ """Recursive Pydantic / dataclass / SDK object → JSON-safe."""
374
+ if obj is None or isinstance(obj, (bool, int, float, str)):
375
+ return obj
376
+ if isinstance(obj, (list, tuple, set)):
377
+ return [_to_jsonable(x) for x in obj]
378
+ if isinstance(obj, dict):
379
+ return {str(k): _to_jsonable(v) for k, v in obj.items()}
380
+ if hasattr(obj, "model_dump"):
381
+ try:
382
+ return obj.model_dump()
383
+ except Exception: # pragma: no cover # noqa: S110
384
+ pass
385
+ if hasattr(obj, "__dict__"):
386
+ return {k: _to_jsonable(v) for k, v in obj.__dict__.items()}
387
+ return repr(obj)
388
+
389
+
390
+ # Sanity check at import time: ensure the SDK is importable in dev envs;
391
+ # we don't want a typo'd symbol to surface at first request time.
392
+ def _ensure_sdk_available() -> None:
393
+ try:
394
+ import anthropic # noqa: F401
395
+ except ImportError:
396
+ # The actual error is raised in ``_get_client``; nothing to do here.
397
+ pass
398
+
399
+
400
+ _ensure_sdk_available()
401
+
402
+
403
+ __all__ = ["AnthropicChatProvider"]