soothe-cli 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 (107) hide show
  1. soothe_cli/__init__.py +5 -0
  2. soothe_cli/cli/__init__.py +1 -0
  3. soothe_cli/cli/commands/__init__.py +1 -0
  4. soothe_cli/cli/commands/autopilot_cmd.py +410 -0
  5. soothe_cli/cli/commands/config_cmd.py +277 -0
  6. soothe_cli/cli/commands/run_cmd.py +87 -0
  7. soothe_cli/cli/commands/status_cmd.py +121 -0
  8. soothe_cli/cli/commands/subagent_names.py +17 -0
  9. soothe_cli/cli/commands/thread_cmd.py +657 -0
  10. soothe_cli/cli/execution/__init__.py +6 -0
  11. soothe_cli/cli/execution/daemon.py +194 -0
  12. soothe_cli/cli/execution/headless.py +99 -0
  13. soothe_cli/cli/execution/launcher.py +31 -0
  14. soothe_cli/cli/main.py +509 -0
  15. soothe_cli/cli/renderer.py +444 -0
  16. soothe_cli/cli/stream/__init__.py +17 -0
  17. soothe_cli/cli/stream/context.py +138 -0
  18. soothe_cli/cli/stream/display_line.py +83 -0
  19. soothe_cli/cli/stream/formatter.py +412 -0
  20. soothe_cli/cli/stream/pipeline.py +521 -0
  21. soothe_cli/cli/utils.py +46 -0
  22. soothe_cli/config/__init__.py +5 -0
  23. soothe_cli/config/cli_config.py +155 -0
  24. soothe_cli/plan/__init__.py +5 -0
  25. soothe_cli/plan/rich_tree.py +54 -0
  26. soothe_cli/shared/__init__.py +107 -0
  27. soothe_cli/shared/command_router.py +246 -0
  28. soothe_cli/shared/config_loader.py +68 -0
  29. soothe_cli/shared/display_policy.py +413 -0
  30. soothe_cli/shared/essential_events.py +68 -0
  31. soothe_cli/shared/event_processor.py +823 -0
  32. soothe_cli/shared/message_processing.py +393 -0
  33. soothe_cli/shared/presentation_engine.py +173 -0
  34. soothe_cli/shared/processor_state.py +80 -0
  35. soothe_cli/shared/renderer_protocol.py +158 -0
  36. soothe_cli/shared/rendering.py +43 -0
  37. soothe_cli/shared/slash_commands.py +354 -0
  38. soothe_cli/shared/subagent_routing.py +63 -0
  39. soothe_cli/shared/suppression_state.py +188 -0
  40. soothe_cli/shared/tool_formatters/__init__.py +27 -0
  41. soothe_cli/shared/tool_formatters/base.py +109 -0
  42. soothe_cli/shared/tool_formatters/execution.py +297 -0
  43. soothe_cli/shared/tool_formatters/fallback.py +128 -0
  44. soothe_cli/shared/tool_formatters/file_ops.py +299 -0
  45. soothe_cli/shared/tool_formatters/goal_formatter.py +331 -0
  46. soothe_cli/shared/tool_formatters/media.py +291 -0
  47. soothe_cli/shared/tool_formatters/structured.py +202 -0
  48. soothe_cli/shared/tool_formatters/web.py +143 -0
  49. soothe_cli/shared/tool_output_formatter.py +227 -0
  50. soothe_cli/shared/tui_trace_log.py +40 -0
  51. soothe_cli/tui/__init__.py +5 -0
  52. soothe_cli/tui/_ask_user_types.py +50 -0
  53. soothe_cli/tui/_cli_context.py +27 -0
  54. soothe_cli/tui/_env_vars.py +56 -0
  55. soothe_cli/tui/_session_stats.py +114 -0
  56. soothe_cli/tui/_version.py +21 -0
  57. soothe_cli/tui/app.py +4992 -0
  58. soothe_cli/tui/app.tcss +302 -0
  59. soothe_cli/tui/command_registry.py +310 -0
  60. soothe_cli/tui/config.py +2381 -0
  61. soothe_cli/tui/daemon_session.py +233 -0
  62. soothe_cli/tui/file_ops.py +409 -0
  63. soothe_cli/tui/formatting.py +28 -0
  64. soothe_cli/tui/hooks.py +23 -0
  65. soothe_cli/tui/input.py +782 -0
  66. soothe_cli/tui/media_utils.py +471 -0
  67. soothe_cli/tui/model_config.py +518 -0
  68. soothe_cli/tui/output.py +69 -0
  69. soothe_cli/tui/project_utils.py +188 -0
  70. soothe_cli/tui/sessions.py +1248 -0
  71. soothe_cli/tui/skills/__init__.py +5 -0
  72. soothe_cli/tui/skills/invocation.py +74 -0
  73. soothe_cli/tui/skills/load.py +93 -0
  74. soothe_cli/tui/textual_adapter.py +1430 -0
  75. soothe_cli/tui/theme.py +838 -0
  76. soothe_cli/tui/tool_display.py +297 -0
  77. soothe_cli/tui/unicode_security.py +502 -0
  78. soothe_cli/tui/update_check.py +447 -0
  79. soothe_cli/tui/widgets/__init__.py +9 -0
  80. soothe_cli/tui/widgets/_links.py +63 -0
  81. soothe_cli/tui/widgets/approval.py +430 -0
  82. soothe_cli/tui/widgets/ask_user.py +392 -0
  83. soothe_cli/tui/widgets/autocomplete.py +666 -0
  84. soothe_cli/tui/widgets/autopilot_dashboard.py +308 -0
  85. soothe_cli/tui/widgets/autopilot_screen.py +64 -0
  86. soothe_cli/tui/widgets/chat_input.py +1834 -0
  87. soothe_cli/tui/widgets/clipboard.py +128 -0
  88. soothe_cli/tui/widgets/diff.py +240 -0
  89. soothe_cli/tui/widgets/editor.py +140 -0
  90. soothe_cli/tui/widgets/history.py +221 -0
  91. soothe_cli/tui/widgets/loading.py +194 -0
  92. soothe_cli/tui/widgets/mcp_viewer.py +352 -0
  93. soothe_cli/tui/widgets/message_store.py +693 -0
  94. soothe_cli/tui/widgets/messages.py +1720 -0
  95. soothe_cli/tui/widgets/model_selector.py +988 -0
  96. soothe_cli/tui/widgets/notification_settings.py +155 -0
  97. soothe_cli/tui/widgets/status.py +403 -0
  98. soothe_cli/tui/widgets/theme_selector.py +158 -0
  99. soothe_cli/tui/widgets/thread_selector.py +1865 -0
  100. soothe_cli/tui/widgets/tool_renderers.py +148 -0
  101. soothe_cli/tui/widgets/tool_widgets.py +254 -0
  102. soothe_cli/tui/widgets/tools.py +165 -0
  103. soothe_cli/tui/widgets/welcome.py +330 -0
  104. soothe_cli-0.1.0.dist-info/METADATA +100 -0
  105. soothe_cli-0.1.0.dist-info/RECORD +107 -0
  106. soothe_cli-0.1.0.dist-info/WHEEL +4 -0
  107. soothe_cli-0.1.0.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,2381 @@
1
+ """Configuration, constants, and model creation for the CLI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import importlib
6
+ import json
7
+ import logging
8
+ import os
9
+ import re
10
+ import shlex
11
+ import sys
12
+ import threading
13
+ from dataclasses import dataclass
14
+ from enum import StrEnum
15
+ from importlib.metadata import PackageNotFoundError, distribution
16
+ from pathlib import Path
17
+ from typing import TYPE_CHECKING, Any
18
+ from urllib.parse import unquote, urlparse
19
+
20
+ from soothe_sdk import SOOTHE_HOME
21
+
22
+ from soothe_cli.tui._version import __version__
23
+
24
+ logger = logging.getLogger(__name__)
25
+
26
+ # ---------------------------------------------------------------------------
27
+ # Lazy bootstrap: dotenv loading, LANGSMITH_PROJECT override, and start-path
28
+ # detection are deferred until first access of `settings` (via module
29
+ # `__getattr__`). This avoids disk I/O and path traversal during import for
30
+ # callers that never touch `settings` (e.g. `Soothe --help`).
31
+ # ---------------------------------------------------------------------------
32
+
33
+ _bootstrap_done = False
34
+ """Whether `_ensure_bootstrap()` has executed."""
35
+
36
+ _bootstrap_lock = threading.Lock()
37
+ """Guards `_ensure_bootstrap()` against concurrent access from the main thread
38
+ and the prewarm worker thread."""
39
+
40
+ _singleton_lock = threading.Lock()
41
+ """Guards lazy singleton construction in `_get_console` / `_get_settings`."""
42
+
43
+ _bootstrap_start_path: Path | None = None
44
+ """Working directory captured at bootstrap time for dotenv and project discovery."""
45
+
46
+ _original_langsmith_project: str | None = None
47
+ """Caller's `LANGSMITH_PROJECT` value before the CLI overrides it for agent traces.
48
+
49
+ Captured inside `_ensure_bootstrap()` after dotenv loading but before the
50
+ `LANGSMITH_PROJECT` override, so `.env`-only values are visible.
51
+ """
52
+
53
+
54
+ def _find_dotenv_from_start_path(start_path: Path) -> Path | None:
55
+ """Find the nearest `.env` file from an explicit start path upward.
56
+
57
+ Args:
58
+ start_path: Directory to start searching from.
59
+
60
+ Returns:
61
+ Path to the nearest `.env` file, or `None` if not found.
62
+ """
63
+ current = start_path.expanduser().resolve()
64
+ for parent in [current, *list(current.parents)]:
65
+ candidate = parent / ".env"
66
+ try:
67
+ if candidate.is_file():
68
+ return candidate
69
+ except OSError:
70
+ logger.warning("Could not inspect .env candidate %s", candidate)
71
+ continue
72
+ return None
73
+
74
+
75
+ # Global user-level .env (SOOTHE_HOME/.env); sentinel when Path.home() fails.
76
+ try:
77
+ _GLOBAL_DOTENV_PATH = Path(SOOTHE_HOME) / ".env"
78
+ except RuntimeError:
79
+ _GLOBAL_DOTENV_PATH = Path("/nonexistent/.soothe/.env")
80
+
81
+
82
+ def _load_dotenv(*, start_path: Path | None = None) -> bool:
83
+ """Load environment variables from project and global `.env` files.
84
+
85
+ Loads in order (first write wins, `override=False`):
86
+
87
+ 1. Project/CWD `.env` — project-specific values
88
+ 2. `SOOTHE_HOME/.env` — global user defaults
89
+
90
+ Both layers use `override=False` (the python-dotenv default) so that
91
+ shell-exported variables always take precedence over dotenv files.
92
+ Because project loads first, the effective precedence is:
93
+
94
+ ```text
95
+ shell env (incl. inline `VAR=x`) > project `.env` > global `.env`
96
+ ```
97
+
98
+ !!! note
99
+
100
+ To scope credentials to the CLI without colliding with
101
+ identically-named shell exports, use the `SOOTHE_` env-var
102
+ prefix (see `resolve_env_var` in `soothe.model_config`).
103
+
104
+ Args:
105
+ start_path: Directory to use for project `.env` discovery.
106
+
107
+ Returns:
108
+ `True` when at least one dotenv file was loaded, `False` otherwise.
109
+ """
110
+ import dotenv
111
+
112
+ loaded = False
113
+
114
+ # 1. Project/CWD .env — loads first so project values are set before the
115
+ # global file, which can only fill in vars not already present.
116
+ dotenv_path: Path | str | None = None
117
+ try:
118
+ if start_path is None:
119
+ loaded = dotenv.load_dotenv(override=False) or loaded
120
+ else:
121
+ dotenv_path = _find_dotenv_from_start_path(start_path)
122
+ if dotenv_path is not None:
123
+ loaded = dotenv.load_dotenv(dotenv_path=dotenv_path, override=False) or loaded
124
+ except (OSError, ValueError):
125
+ logger.warning(
126
+ "Could not read project dotenv at %s; project env vars will not be loaded",
127
+ dotenv_path or start_path or "cwd",
128
+ exc_info=True,
129
+ )
130
+
131
+ # 2. Global (SOOTHE_HOME/.env) — fills in any vars not already set by
132
+ # the shell or the project dotenv.
133
+ # try/except wraps both is_file() and load_dotenv() to cover the TOCTOU
134
+ # window where the file can vanish between stat and open.
135
+ try:
136
+ if _GLOBAL_DOTENV_PATH.is_file() and dotenv.load_dotenv(
137
+ dotenv_path=_GLOBAL_DOTENV_PATH, override=False
138
+ ):
139
+ loaded = True
140
+ logger.debug("Loaded global dotenv: %s", _GLOBAL_DOTENV_PATH)
141
+ except (OSError, ValueError):
142
+ logger.warning(
143
+ "Could not read global dotenv at %s; global defaults will not be applied",
144
+ _GLOBAL_DOTENV_PATH,
145
+ exc_info=True,
146
+ )
147
+
148
+ return loaded
149
+
150
+
151
+ def _ensure_bootstrap() -> None:
152
+ """Run one-time bootstrap: dotenv loading and `LANGSMITH_PROJECT` override.
153
+
154
+ Idempotent and thread-safe — subsequent calls are no-ops. Called
155
+ automatically by `_get_settings()` when `settings` is first accessed.
156
+
157
+ The flag is set in `finally` so that partial failures (e.g. a
158
+ malformed `.env`) still mark bootstrap as done — preventing infinite retry
159
+ loops. Exceptions are caught and logged at ERROR level; the CLI proceeds
160
+ with the environment as-is.
161
+ """
162
+ global _bootstrap_done, _bootstrap_start_path, _original_langsmith_project # noqa: PLW0603
163
+
164
+ if _bootstrap_done:
165
+ return
166
+
167
+ with _bootstrap_lock:
168
+ if _bootstrap_done: # double-check after acquiring lock
169
+ return
170
+
171
+ try:
172
+ from soothe_cli.tui.project_utils import (
173
+ get_server_project_context as _get_server_project_context,
174
+ )
175
+
176
+ ctx = _get_server_project_context()
177
+ _bootstrap_start_path = ctx.user_cwd if ctx else None
178
+ _load_dotenv(start_path=_bootstrap_start_path)
179
+
180
+ # Capture AFTER dotenv loading so .env-only values are visible,
181
+ # but BEFORE the override below replaces it.
182
+ _original_langsmith_project = os.environ.get("LANGSMITH_PROJECT")
183
+
184
+ # CRITICAL: Override LANGSMITH_PROJECT to route agent traces to a
185
+ # separate project. LangSmith reads LANGSMITH_PROJECT at invocation
186
+ # time, so we override it here and preserve the user's original
187
+ # value for shell commands.
188
+ from soothe_cli.tui._env_vars import LANGSMITH_PROJECT
189
+
190
+ soothe_project = os.environ.get(LANGSMITH_PROJECT)
191
+ if soothe_project:
192
+ os.environ["LANGSMITH_PROJECT"] = soothe_project
193
+
194
+ # Propagate prefixed LangSmith env vars to canonical names.
195
+ # The CLI resolves prefixed vars via resolve_env_var(), but the
196
+ # LangSmith SDK reads os.environ directly and has no knowledge
197
+ # of the SOOTHE_ prefix. Setting canonical vars here
198
+ # bridges that gap.
199
+ from soothe_cli.tui.model_config import _ENV_PREFIX
200
+
201
+ for canonical in (
202
+ "LANGSMITH_API_KEY",
203
+ "LANGCHAIN_API_KEY",
204
+ "LANGSMITH_TRACING",
205
+ "LANGCHAIN_TRACING_V2",
206
+ ):
207
+ prefixed = f"{_ENV_PREFIX}{canonical}"
208
+ if prefixed not in os.environ:
209
+ continue
210
+ prefixed_val = os.environ[prefixed]
211
+ if canonical not in os.environ:
212
+ # Propagate (including empty string for explicit disable).
213
+ os.environ[canonical] = prefixed_val
214
+ elif os.environ[canonical] != prefixed_val:
215
+ os.environ[canonical] = prefixed_val
216
+ logger.warning(
217
+ "Both %s and %s are set with different values; using %s. Unset %s to silence this warning.",
218
+ canonical,
219
+ prefixed,
220
+ prefixed,
221
+ canonical,
222
+ )
223
+ except Exception:
224
+ logger.exception(
225
+ "Bootstrap failed; .env values and LANGSMITH_PROJECT override "
226
+ "may be missing. The CLI will proceed with environment as-is.",
227
+ )
228
+ finally:
229
+ _bootstrap_done = True
230
+
231
+
232
+ if TYPE_CHECKING:
233
+ from langchain_core.language_models import BaseChatModel
234
+ from langchain_core.runnables import RunnableConfig
235
+ from rich.console import Console
236
+
237
+ # Static type stubs for lazy module attributes resolved by __getattr__.
238
+ # At runtime these are created on first access by _get_settings() /
239
+ # _get_console() and cached in globals().
240
+ settings: Settings
241
+ console: Console
242
+
243
+ MODE_PREFIXES: dict[str, str] = {
244
+ "shell": "!",
245
+ "command": "/",
246
+ }
247
+ """Maps each non-normal mode to its trigger character."""
248
+
249
+ MODE_DISPLAY_GLYPHS: dict[str, str] = {
250
+ "shell": "$",
251
+ "command": "/",
252
+ }
253
+ """Maps each non-normal mode to its display glyph shown in the prompt/UI."""
254
+
255
+ if MODE_PREFIXES.keys() != MODE_DISPLAY_GLYPHS.keys():
256
+ _only_prefixes = MODE_PREFIXES.keys() - MODE_DISPLAY_GLYPHS.keys()
257
+ _only_glyphs = MODE_DISPLAY_GLYPHS.keys() - MODE_PREFIXES.keys()
258
+ msg = (
259
+ "MODE_PREFIXES and MODE_DISPLAY_GLYPHS have mismatched keys: "
260
+ f"only in PREFIXES={_only_prefixes}, only in GLYPHS={_only_glyphs}"
261
+ )
262
+ raise ValueError(msg)
263
+
264
+ PREFIX_TO_MODE: dict[str, str] = {v: k for k, v in MODE_PREFIXES.items()}
265
+ """Reverse lookup: trigger character -> mode name."""
266
+
267
+
268
+ class CharsetMode(StrEnum):
269
+ """Character set mode for TUI display."""
270
+
271
+ UNICODE = "unicode"
272
+ """Always use Unicode glyphs (e.g. `⏺`, `✓`, `…`)."""
273
+
274
+ ASCII = "ascii"
275
+ """Always use ASCII-safe fallbacks (e.g. `(*)`, `[OK]`, `...`)."""
276
+
277
+ AUTO = "auto"
278
+ """Detect charset support at runtime and pick Unicode or ASCII."""
279
+
280
+
281
+ @dataclass(frozen=True)
282
+ class Glyphs:
283
+ """Character glyphs for TUI display."""
284
+
285
+ tool_prefix: str # ⏺ vs (*)
286
+ ellipsis: str # … vs ...
287
+ checkmark: str # ✓ vs [OK]
288
+ error: str # ✗ vs [X]
289
+ circle_empty: str # ○ vs [ ]
290
+ circle_filled: str # ● vs [*]
291
+ output_prefix: str # ⎿ vs L
292
+ spinner_frames: tuple[str, ...] # Braille vs ASCII spinner
293
+ pause: str # ⏸ vs ||
294
+ newline: str # ⏎ vs \\n
295
+ warning: str # ⚠ vs [!]
296
+ question: str # ? vs [?]
297
+ arrow_up: str # up arrow vs ^
298
+ arrow_down: str # down arrow vs v
299
+ bullet: str # bullet vs -
300
+ cursor: str # cursor vs >
301
+
302
+ # Box-drawing characters
303
+ box_vertical: str # │ vs |
304
+ box_horizontal: str # ─ vs -
305
+ box_double_horizontal: str # ═ vs =
306
+
307
+ # Diff-specific
308
+ gutter_bar: str # ▌ vs |
309
+
310
+ # Status bar
311
+ git_branch: str # "↗" vs "git:"
312
+
313
+
314
+ UNICODE_GLYPHS = Glyphs(
315
+ tool_prefix="⏺",
316
+ ellipsis="…",
317
+ checkmark="✓",
318
+ error="✗",
319
+ circle_empty="○",
320
+ circle_filled="●",
321
+ output_prefix="⎿",
322
+ spinner_frames=("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"),
323
+ pause="⏸",
324
+ newline="⏎",
325
+ warning="⚠",
326
+ question="?",
327
+ arrow_up="↑",
328
+ arrow_down="↓",
329
+ bullet="•",
330
+ cursor="›", # noqa: RUF001 # Intentional Unicode glyph
331
+ # Box-drawing characters
332
+ box_vertical="│",
333
+ box_horizontal="─",
334
+ box_double_horizontal="═",
335
+ gutter_bar="▌",
336
+ git_branch="↗",
337
+ )
338
+ """Glyph set for terminals with full Unicode support."""
339
+
340
+ ASCII_GLYPHS = Glyphs(
341
+ tool_prefix="(*)",
342
+ ellipsis="...",
343
+ checkmark="[OK]",
344
+ error="[X]",
345
+ circle_empty="[ ]",
346
+ circle_filled="[*]",
347
+ output_prefix="L",
348
+ spinner_frames=("(-)", "(\\)", "(|)", "(/)"),
349
+ pause="||",
350
+ newline="\\n",
351
+ warning="[!]",
352
+ question="[?]",
353
+ arrow_up="^",
354
+ arrow_down="v",
355
+ bullet="-",
356
+ cursor=">",
357
+ # Box-drawing characters
358
+ box_vertical="|",
359
+ box_horizontal="-",
360
+ box_double_horizontal="=",
361
+ gutter_bar="|",
362
+ git_branch="git:",
363
+ )
364
+ """Glyph set for terminals limited to 7-bit ASCII."""
365
+
366
+ _glyphs_cache: Glyphs | None = None
367
+ """Module-level cache for detected glyphs."""
368
+
369
+ _editable_cache: tuple[bool, str | None] | None = None
370
+ """Module-level cache for editable install info: (is_editable, source_path)."""
371
+
372
+ _langsmith_url_cache: tuple[str, str] | None = None
373
+ """Module-level cache for successful LangSmith project URL lookups."""
374
+
375
+ _LANGSMITH_URL_LOOKUP_TIMEOUT_SECONDS = 2.0
376
+ """Max seconds to wait for LangSmith project URL lookup.
377
+
378
+ Kept short so tracing metadata can never stall CLI flows.
379
+ """
380
+
381
+
382
+ def _resolve_editable_info() -> tuple[bool, str | None]:
383
+ """Parse PEP 610 `direct_url.json` once and cache both results.
384
+
385
+ Returns:
386
+ Tuple of (is_editable, contracted_source_path). The path is
387
+ `~`-contracted when it falls under the user's home directory, or
388
+ `None` when the install is non-editable or the path is unavailable.
389
+ """
390
+ global _editable_cache # noqa: PLW0603 # Module-level cache requires global statement
391
+ if _editable_cache is not None:
392
+ return _editable_cache
393
+
394
+ editable = False
395
+ path: str | None = None
396
+
397
+ try:
398
+ dist = distribution("Soothe")
399
+ raw = dist.read_text("direct_url.json")
400
+ if raw:
401
+ data = json.loads(raw)
402
+ editable = data.get("dir_info", {}).get("editable", False)
403
+ if editable:
404
+ url = data.get("url", "")
405
+ if url.startswith("file://"):
406
+ path = unquote(urlparse(url).path)
407
+ home = str(Path.home())
408
+ if path.startswith(home):
409
+ path = "~" + path[len(home) :]
410
+ except (PackageNotFoundError, FileNotFoundError, json.JSONDecodeError, TypeError):
411
+ logger.debug(
412
+ "Failed to read editable install info from PEP 610 metadata",
413
+ exc_info=True,
414
+ )
415
+
416
+ _editable_cache = (editable, path)
417
+ return _editable_cache
418
+
419
+
420
+ def _is_editable_install() -> bool:
421
+ """Check if Soothe is installed in editable mode.
422
+
423
+ Uses PEP 610 `direct_url.json` metadata to detect editable installs.
424
+
425
+ Returns:
426
+ `True` if installed in editable mode, `False` otherwise.
427
+ """
428
+ return _resolve_editable_info()[0]
429
+
430
+
431
+ def _get_editable_install_path() -> str | None:
432
+ """Return the `~`-contracted source directory for an editable install.
433
+
434
+ Returns `None` for non-editable installs or when the path cannot be
435
+ determined.
436
+ """
437
+ return _resolve_editable_info()[1]
438
+
439
+
440
+ def _detect_charset_mode() -> CharsetMode:
441
+ """Auto-detect terminal charset capabilities.
442
+
443
+ Returns:
444
+ The detected CharsetMode based on environment and terminal encoding.
445
+ """
446
+ env_mode = os.environ.get("UI_CHARSET_MODE", "auto").lower()
447
+ if env_mode == "unicode":
448
+ return CharsetMode.UNICODE
449
+ if env_mode == "ascii":
450
+ return CharsetMode.ASCII
451
+
452
+ # Auto: check stdout encoding and LANG
453
+ encoding = getattr(sys.stdout, "encoding", "") or ""
454
+ if "utf" in encoding.lower():
455
+ return CharsetMode.UNICODE
456
+ lang = os.environ.get("LANG", "") or os.environ.get("LC_ALL", "")
457
+ if "utf" in lang.lower():
458
+ return CharsetMode.UNICODE
459
+ return CharsetMode.ASCII
460
+
461
+
462
+ def get_glyphs() -> Glyphs:
463
+ """Get the glyph set for the current charset mode.
464
+
465
+ Returns:
466
+ The appropriate Glyphs instance based on charset mode detection.
467
+ """
468
+ global _glyphs_cache # noqa: PLW0603 # Module-level cache requires global statement
469
+ if _glyphs_cache is not None:
470
+ return _glyphs_cache
471
+
472
+ mode = _detect_charset_mode()
473
+ _glyphs_cache = ASCII_GLYPHS if mode == CharsetMode.ASCII else UNICODE_GLYPHS
474
+ return _glyphs_cache
475
+
476
+
477
+ def reset_glyphs_cache() -> None:
478
+ """Reset the glyphs cache (for testing)."""
479
+ global _glyphs_cache # noqa: PLW0603 # Module-level cache requires global statement
480
+ _glyphs_cache = None
481
+
482
+
483
+ def is_ascii_mode() -> bool:
484
+ """Check whether the terminal is in ASCII charset mode.
485
+
486
+ Convenience wrapper so widgets can branch on charset without importing
487
+ both `_detect_charset_mode` and `CharsetMode`.
488
+
489
+ Returns:
490
+ `True` when the detected charset mode is ASCII.
491
+ """
492
+ return _detect_charset_mode() == CharsetMode.ASCII
493
+
494
+
495
+ def newline_shortcut() -> str:
496
+ """Return the platform-native label for the newline keyboard shortcut.
497
+
498
+ macOS labels the modifier "Option" while other platforms use Ctrl+J
499
+ as the most reliable cross-terminal shortcut.
500
+
501
+ Returns:
502
+ A human-readable shortcut string, e.g. `'Option+Enter'` or `'Ctrl+J'`.
503
+ """
504
+ return "Option+Enter" if sys.platform == "darwin" else "Ctrl+J"
505
+
506
+
507
+ _UNICODE_BANNER = f"""
508
+ ███████╗ ██████╗ ██████╗ ████████╗██╗ ██╗███████╗
509
+ ██╔════╝██╔═══██╗██╔═══██╗╚══██╔══╝██║ ██║██╔════╝
510
+ ███████╗██║ ██║██║ ██║ ██║ ███████║█████╗
511
+ ╚════██║██║ ██║██║ ██║ ██║ ██╔══██║██╔══╝
512
+ ███████║╚██████╔╝╚██████╔╝ ██║ ██║ ██║███████╗
513
+ ╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝
514
+
515
+ v{__version__}
516
+ """
517
+ _ASCII_BANNER = f"""
518
+ _______ _______ _______ _______ _______
519
+ / ___ \\/ ___ \\/ ___ \\/ ___ \\/ ___ \
520
+ | | | || | | || | | || | | || | | |
521
+ | |___| || |___| || |___| || |___| || |___| |
522
+ \\_______/\\_______/\\_______/\\_______/\\_______/
523
+ v{__version__}
524
+ """
525
+
526
+
527
+ def get_banner() -> str:
528
+ """Get the appropriate banner for the current charset mode.
529
+
530
+ Returns:
531
+ The text art banner string (Unicode or ASCII based on charset mode).
532
+
533
+ Includes "(local)" suffix when installed in editable mode.
534
+ """
535
+ if _detect_charset_mode() == CharsetMode.ASCII:
536
+ banner = _ASCII_BANNER
537
+ else:
538
+ banner = _UNICODE_BANNER
539
+
540
+ if _is_editable_install():
541
+ banner = banner.replace(f"v{__version__}", f"v{__version__} (local)")
542
+
543
+ return banner
544
+
545
+
546
+ MAX_ARG_LENGTH = 150
547
+ """Character limit for tool argument values in the UI.
548
+
549
+ Longer values are truncated with an ellipsis by `truncate_value`
550
+ in `tool_display`.
551
+ """
552
+
553
+ config: RunnableConfig = {
554
+ "recursion_limit": 1000,
555
+ }
556
+ """Default LangGraph runnable config.
557
+
558
+ Sets `recursion_limit` to 1000 to accommodate deeply nested agent graphs without
559
+ hitting the default LangGraph ceiling.
560
+ """
561
+
562
+ _git_branch_cache: dict[str, str | None] = {}
563
+ """Per-cwd cache of resolved git branch names.
564
+
565
+ Avoids repeated `git rev-parse` subprocess calls within the same session. Keyed
566
+ by `str(Path.cwd())`; `None` values indicate the directory is not inside a git
567
+ repository.
568
+ """
569
+
570
+
571
+ def _get_git_branch() -> str | None:
572
+ """Return the current git branch name, or `None` if not in a repo."""
573
+ import subprocess # noqa: S404
574
+
575
+ try:
576
+ cwd = str(Path.cwd())
577
+ except OSError:
578
+ logger.debug("Could not determine cwd for git branch lookup", exc_info=True)
579
+ return None
580
+ if cwd in _git_branch_cache:
581
+ return _git_branch_cache[cwd]
582
+
583
+ try:
584
+ result = subprocess.run(
585
+ ["git", "rev-parse", "--abbrev-ref", "HEAD"], # noqa: S607
586
+ capture_output=True,
587
+ text=True,
588
+ timeout=2,
589
+ check=False,
590
+ )
591
+ if result.returncode == 0:
592
+ branch = result.stdout.strip() or None
593
+ _git_branch_cache[cwd] = branch
594
+ return branch
595
+ except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
596
+ logger.debug("Could not determine git branch", exc_info=True)
597
+ _git_branch_cache[cwd] = None
598
+ return None
599
+
600
+
601
+ def build_stream_config(
602
+ thread_id: str,
603
+ assistant_id: str | None,
604
+ *,
605
+ sandbox_type: str | None = None,
606
+ ) -> RunnableConfig:
607
+ """Build the LangGraph stream config dict.
608
+
609
+ Injects CLI and SDK versions into `metadata["versions"]` so LangSmith traces
610
+ can be correlated with specific releases.
611
+
612
+ Why the CLI sets *both* versions:
613
+
614
+ * `create_deep_agent` bakes `versions: {"Soothe": "X.Y.Z"}` into the
615
+ compiled graph via `with_config`. At stream time, LangGraph merges
616
+ the graph config with the runtime config passed here. Because the
617
+ metadata merge is shallow (effectively `{**graph_meta, **runtime_meta}`
618
+ for top-level keys), both configs containing a `versions` key means
619
+ the runtime dict **replaces** the graph dict entirely — the SDK
620
+ version would be lost.
621
+ * Including the SDK version here ensures it survives the merge.
622
+
623
+ Includes `ls_integration` metadata so LangSmith traces originating from the CLI
624
+ are distinguishable from bare SDK usage.
625
+
626
+ Args:
627
+ thread_id: The CLI session thread identifier.
628
+ assistant_id: The agent/assistant identifier, if any.
629
+ sandbox_type: Sandbox provider name for trace metadata, or `None` if no
630
+ sandbox is active.
631
+
632
+ Returns:
633
+ Config dict with `configurable` and `metadata` keys.
634
+ """
635
+ import contextlib
636
+ import importlib.metadata as importlib_metadata
637
+ from datetime import UTC, datetime
638
+
639
+ try:
640
+ cwd = str(Path.cwd())
641
+ except OSError:
642
+ logger.warning("Could not determine working directory", exc_info=True)
643
+ cwd = ""
644
+
645
+ # Include SDK version alongside CLI version — see docstring for why.
646
+ versions: dict[str, str] = {"Soothe": __version__}
647
+ with contextlib.suppress(importlib_metadata.PackageNotFoundError):
648
+ versions["Soothe"] = importlib_metadata.version("Soothe")
649
+
650
+ metadata: dict[str, Any] = {
651
+ "versions": versions,
652
+ "ls_integration": "Soothe",
653
+ }
654
+ from soothe_cli.tui._env_vars import USER_ID
655
+
656
+ user_id = os.environ.get(USER_ID)
657
+ if user_id:
658
+ metadata["user_id"] = user_id
659
+ if cwd:
660
+ metadata["cwd"] = cwd
661
+ if assistant_id:
662
+ metadata.update(
663
+ {
664
+ "assistant_id": assistant_id,
665
+ "agent_name": assistant_id,
666
+ "updated_at": datetime.now(UTC).isoformat(),
667
+ }
668
+ )
669
+ branch = _get_git_branch()
670
+ if branch:
671
+ metadata["git_branch"] = branch
672
+ if sandbox_type and sandbox_type != "none":
673
+ metadata["sandbox_type"] = sandbox_type
674
+ return {
675
+ "configurable": {"thread_id": thread_id},
676
+ "metadata": metadata,
677
+ }
678
+
679
+
680
+ class _ShellAllowAll(list): # noqa: FURB189 # sentinel type, not a general-purpose list subclass
681
+ """Sentinel subclass for unrestricted shell access.
682
+
683
+ Using a dedicated type instead of a plain list lets consumers use
684
+ `isinstance` checks, which survive serialization/copy unlike identity
685
+ checks (`is`).
686
+ """
687
+
688
+
689
+ SHELL_ALLOW_ALL: list[str] = _ShellAllowAll(["__ALL__"])
690
+ """Sentinel value returned by `parse_shell_allow_list` for `--shell-allow-list=all`."""
691
+
692
+
693
+ def parse_shell_allow_list(allow_list_str: str | None) -> list[str] | None:
694
+ """Parse shell allow-list from string.
695
+
696
+ Args:
697
+ allow_list_str: Comma-separated list of commands, `'recommended'` for
698
+ safe defaults, or `'all'` to allow any command.
699
+
700
+ `'all'` must be the sole value — it is not recognized inside a
701
+ comma-separated list (unlike `'recommended'`).
702
+
703
+ Can also include `'recommended'` in the list to merge with custom
704
+ commands.
705
+
706
+ Returns:
707
+ List of allowed commands, `SHELL_ALLOW_ALL` if `'all'` was specified,
708
+ or `None` if no allow-list configured.
709
+
710
+ Raises:
711
+ ValueError: If `'all'` is combined with other commands.
712
+ """
713
+ if not allow_list_str:
714
+ return None
715
+
716
+ # Special value 'all' allows any shell command
717
+ if allow_list_str.strip().lower() == "all":
718
+ return SHELL_ALLOW_ALL
719
+
720
+ # Special value 'recommended' uses our curated safe list
721
+ if allow_list_str.strip().lower() == "recommended":
722
+ return list(RECOMMENDED_SAFE_SHELL_COMMANDS)
723
+
724
+ # Split by comma and strip whitespace
725
+ commands = [cmd.strip() for cmd in allow_list_str.split(",") if cmd.strip()]
726
+
727
+ # Reject ambiguous input: 'all' mixed with other commands
728
+ if any(cmd.lower() == "all" for cmd in commands):
729
+ msg = (
730
+ "Cannot combine 'all' with other commands in --shell-allow-list. "
731
+ "Use '--shell-allow-list all' alone to allow any command."
732
+ )
733
+ raise ValueError(msg)
734
+
735
+ # If "recommended" is in the list, merge with recommended commands
736
+ result = []
737
+ for cmd in commands:
738
+ if cmd.lower() == "recommended":
739
+ result.extend(RECOMMENDED_SAFE_SHELL_COMMANDS)
740
+ else:
741
+ result.append(cmd)
742
+
743
+ # Remove duplicates while preserving order
744
+ seen: set[str] = set()
745
+ unique: list[str] = []
746
+ for cmd in result:
747
+ if cmd not in seen:
748
+ seen.add(cmd)
749
+ unique.append(cmd)
750
+ return unique
751
+
752
+
753
+ def _read_config_yaml_skills_dirs() -> list[str] | None:
754
+ """Read `[skills].extra_allowed_dirs` from `SOOTHE_HOME/config/config.yml`.
755
+
756
+ Returns:
757
+ List of path strings, or `None` if the key is absent or the file
758
+ cannot be read.
759
+ """
760
+ import yaml
761
+
762
+ from soothe_cli.tui.model_config import DEFAULT_CONFIG_PATH
763
+
764
+ try:
765
+ with DEFAULT_CONFIG_PATH.open("r") as f:
766
+ data = yaml.safe_load(f)
767
+ except FileNotFoundError:
768
+ return None
769
+ except (PermissionError, OSError, yaml.YAMLError):
770
+ logger.warning(
771
+ "Could not read skills config from %s",
772
+ DEFAULT_CONFIG_PATH,
773
+ exc_info=True,
774
+ )
775
+ return None
776
+
777
+ skills_section = data.get("skills", {}) if data else {}
778
+ dirs = skills_section.get("extra_allowed_dirs")
779
+ if isinstance(dirs, list):
780
+ return dirs
781
+ return None
782
+
783
+
784
+ def _parse_extra_skills_dirs(
785
+ env_raw: str | None,
786
+ config_yaml_dirs: list[str] | None = None,
787
+ ) -> list[Path] | None:
788
+ """Merge extra skill directories from env var and config.yml.
789
+
790
+ Extra skills directories extend the containment allowlist used by
791
+ `load_skill_content` to validate that a resolved skill path lives inside a
792
+ trusted root. They do **not** add new skill discovery locations — skills are
793
+ still discovered only from the standard directories. This exists so that
794
+ symlinks inside standard skill directories can legitimately point to targets
795
+ in user-specified locations without being rejected by the path
796
+ containment check.
797
+
798
+ The env var (`SOOTHE_EXTRA_SKILLS_DIRS`, colon-separated) takes
799
+ precedence: when set, `config.yml` values are ignored.
800
+
801
+ Args:
802
+ env_raw: Value of `SOOTHE_EXTRA_SKILLS_DIRS` (colon-separated), or
803
+ `None` if unset.
804
+ config_yaml_dirs: List of path strings from
805
+ `[skills].extra_allowed_dirs` in `SOOTHE_HOME/config/config.yml`.
806
+
807
+ Returns:
808
+ List of resolved `Path` objects, or `None` if not configured.
809
+ """
810
+ # Env var takes precedence when set
811
+ if env_raw:
812
+ dirs = [Path(p.strip()).expanduser().resolve() for p in env_raw.split(":") if p.strip()]
813
+ return dirs or None
814
+
815
+ if config_yaml_dirs:
816
+ dirs = [
817
+ Path(p).expanduser().resolve()
818
+ for p in config_yaml_dirs
819
+ if isinstance(p, str) and p.strip()
820
+ ]
821
+ return dirs or None
822
+
823
+ return None
824
+
825
+
826
+ @dataclass
827
+ class Settings:
828
+ """Global settings and environment detection for Soothe.
829
+
830
+ This class is initialized once at startup and provides access to:
831
+ - Available models and API keys
832
+ - Current project information
833
+ - Tool availability (e.g., Tavily)
834
+ - File system paths
835
+ """
836
+
837
+ openai_api_key: str | None
838
+ """OpenAI API key if available."""
839
+
840
+ anthropic_api_key: str | None
841
+ """Anthropic API key if available."""
842
+
843
+ google_api_key: str | None
844
+ """Google API key if available."""
845
+
846
+ nvidia_api_key: str | None
847
+ """NVIDIA API key if available."""
848
+
849
+ tavily_api_key: str | None
850
+ """Tavily API key if available."""
851
+
852
+ google_cloud_project: str | None
853
+ """Google Cloud project ID for VertexAI authentication."""
854
+
855
+ soothe_langchain_project: str | None
856
+ """LangSmith project name for Soothe agent tracing."""
857
+
858
+ user_langchain_project: str | None
859
+ """Original `LANGSMITH_PROJECT` from environment (for user code)."""
860
+
861
+ model_name: str | None = None
862
+ """Currently active model name, set after model creation."""
863
+
864
+ model_provider: str | None = None
865
+ """Provider identifier (e.g., `openai`, `anthropic`, `google_genai`)."""
866
+
867
+ model_context_limit: int | None = None
868
+ """Maximum input token count from the model profile."""
869
+
870
+ model_unsupported_modalities: frozenset[str] = frozenset()
871
+ """Input modalities not indicated as supported by the model profile."""
872
+
873
+ project_root: Path | None = None
874
+ """Current project root directory, or `None` if not in a git project."""
875
+
876
+ shell_allow_list: list[str] | None = None
877
+ """Shell commands that don't require user approval."""
878
+
879
+ extra_skills_dirs: list[Path] | None = None
880
+ """Extra directories added to the skill path containment allowlist.
881
+
882
+ These do NOT add new skill discovery locations — skills are still only
883
+ discovered from the standard directories. They exist so that symlinks inside
884
+ standard skill directories can point to targets in these additional
885
+ locations without being rejected by the containment check
886
+ in `load_skill_content`.
887
+
888
+ Set via `SOOTHE_EXTRA_SKILLS_DIRS` env var (colon-separated) or
889
+ `[skills].extra_allowed_dirs` in `SOOTHE_HOME/config/config.yml`.
890
+ """
891
+
892
+ @classmethod
893
+ def from_environment(cls, *, start_path: Path | None = None) -> Settings:
894
+ """Create settings by detecting the current environment.
895
+
896
+ Args:
897
+ start_path: Directory to start project detection from (defaults to cwd)
898
+
899
+ Returns:
900
+ Settings instance with detected configuration
901
+ """
902
+ # Detect API keys (normalize empty strings to None).
903
+ from soothe_cli.tui.model_config import resolve_env_var
904
+
905
+ openai_key = resolve_env_var("OPENAI_API_KEY")
906
+ anthropic_key = resolve_env_var("ANTHROPIC_API_KEY")
907
+ google_key = resolve_env_var("GOOGLE_API_KEY")
908
+ nvidia_key = resolve_env_var("NVIDIA_API_KEY")
909
+ tavily_key = resolve_env_var("TAVILY_API_KEY")
910
+ google_cloud_project = resolve_env_var("GOOGLE_CLOUD_PROJECT")
911
+
912
+ # Detect LangSmith configuration
913
+ # SOOTHE_CLI_LANGSMITH_PROJECT: Project for Soothe agent tracing
914
+ # user_langchain_project: User's ORIGINAL LANGSMITH_PROJECT (before override)
915
+ # When accessed via the module-level `settings` singleton,
916
+ # _ensure_bootstrap() has already run and may have overridden
917
+ # LANGSMITH_PROJECT. We use the saved original value, not the
918
+ # current os.environ value. Direct callers should ensure
919
+ # bootstrap has run if they depend on the override.
920
+ from soothe_cli.tui._env_vars import (
921
+ EXTRA_SKILLS_DIRS,
922
+ LANGSMITH_PROJECT,
923
+ SHELL_ALLOW_LIST,
924
+ )
925
+
926
+ soothe_langchain_project = resolve_env_var(LANGSMITH_PROJECT)
927
+ user_langchain_project = _original_langsmith_project # Use saved original!
928
+
929
+ # Detect project
930
+ from soothe_cli.tui.project_utils import find_project_root
931
+
932
+ project_root = find_project_root(start_path)
933
+
934
+ # Parse shell command allow-list from environment
935
+ # Format: comma-separated list of commands (e.g., "ls,cat,grep,pwd")
936
+
937
+ shell_allow_list_str = os.environ.get(SHELL_ALLOW_LIST)
938
+ shell_allow_list = parse_shell_allow_list(shell_allow_list_str)
939
+
940
+ # Parse extra skill containment roots from env var or config.yml.
941
+ # These extend the path allowlist for load_skill_content but do not
942
+ # add new skill discovery locations.
943
+ extra_skills_dirs = _parse_extra_skills_dirs(
944
+ os.environ.get(EXTRA_SKILLS_DIRS),
945
+ _read_config_yaml_skills_dirs(),
946
+ )
947
+
948
+ return cls(
949
+ openai_api_key=openai_key,
950
+ anthropic_api_key=anthropic_key,
951
+ google_api_key=google_key,
952
+ nvidia_api_key=nvidia_key,
953
+ tavily_api_key=tavily_key,
954
+ google_cloud_project=google_cloud_project,
955
+ soothe_langchain_project=soothe_langchain_project,
956
+ user_langchain_project=user_langchain_project,
957
+ project_root=project_root,
958
+ shell_allow_list=shell_allow_list,
959
+ extra_skills_dirs=extra_skills_dirs,
960
+ )
961
+
962
+ def reload_from_environment(self, *, start_path: Path | None = None) -> list[str]:
963
+ """Reload selected settings from environment variables and project files.
964
+
965
+ This refreshes only fields that are expected to change at runtime
966
+ (API keys, Google Cloud project, project root, shell allow-list, and
967
+ LangSmith tracing project).
968
+
969
+ Runtime model state (`model_name`, `model_provider`,
970
+ `model_context_limit`) and the original user LangSmith project
971
+ (`user_langchain_project`) are intentionally preserved -- they are
972
+ not in `reloadable_fields` and are never touched by this method.
973
+
974
+ !!! note
975
+
976
+ `.env` files are loaded with `override=False`, so shell-exported
977
+ variables always take precedence. To override a shell-exported key
978
+ from `.env`, use the `SOOTHE_` prefix (e.g.
979
+ `SOOTHE_OPENAI_API_KEY`).
980
+
981
+ Args:
982
+ start_path: Directory to start project detection from (defaults to cwd).
983
+
984
+ Returns:
985
+ A list of human-readable change descriptions.
986
+ """
987
+ _load_dotenv(start_path=start_path)
988
+
989
+ api_key_fields = {
990
+ "openai_api_key",
991
+ "anthropic_api_key",
992
+ "google_api_key",
993
+ "nvidia_api_key",
994
+ "tavily_api_key",
995
+ }
996
+ """Fields that hold API keys — used to mask values in change reports
997
+ so secrets are not logged as plaintext."""
998
+
999
+ reloadable_fields = (
1000
+ "openai_api_key",
1001
+ "anthropic_api_key",
1002
+ "google_api_key",
1003
+ "nvidia_api_key",
1004
+ "tavily_api_key",
1005
+ "google_cloud_project",
1006
+ "soothe_langchain_project",
1007
+ "project_root",
1008
+ "shell_allow_list",
1009
+ "extra_skills_dirs",
1010
+ )
1011
+ """Fields refreshed on `/reload`.
1012
+
1013
+ Runtime model state (`model_name`, `model_provider`, `model_context_limit`)
1014
+ and the original user LangSmith project are intentionally excluded —
1015
+ they are set once and should not change across reloads.
1016
+ """
1017
+
1018
+ previous = {field: getattr(self, field) for field in reloadable_fields}
1019
+
1020
+ from soothe_cli.tui._env_vars import (
1021
+ EXTRA_SKILLS_DIRS,
1022
+ LANGSMITH_PROJECT,
1023
+ SHELL_ALLOW_LIST,
1024
+ )
1025
+
1026
+ try:
1027
+ shell_allow_list = parse_shell_allow_list(os.environ.get(SHELL_ALLOW_LIST))
1028
+ except ValueError:
1029
+ logger.warning(
1030
+ "Invalid %s during reload; keeping previous value",
1031
+ SHELL_ALLOW_LIST,
1032
+ )
1033
+ shell_allow_list = previous["shell_allow_list"]
1034
+
1035
+ try:
1036
+ from soothe_cli.tui.project_utils import find_project_root
1037
+
1038
+ project_root = find_project_root(start_path)
1039
+ except OSError:
1040
+ logger.warning("Could not detect project root during reload; keeping previous value")
1041
+ project_root = previous["project_root"]
1042
+
1043
+ from soothe_cli.tui.model_config import resolve_env_var
1044
+
1045
+ refreshed = {
1046
+ "openai_api_key": resolve_env_var("OPENAI_API_KEY"),
1047
+ "anthropic_api_key": resolve_env_var("ANTHROPIC_API_KEY"),
1048
+ "google_api_key": resolve_env_var("GOOGLE_API_KEY"),
1049
+ "nvidia_api_key": resolve_env_var("NVIDIA_API_KEY"),
1050
+ "tavily_api_key": resolve_env_var("TAVILY_API_KEY"),
1051
+ "google_cloud_project": resolve_env_var("GOOGLE_CLOUD_PROJECT"),
1052
+ "soothe_langchain_project": resolve_env_var(LANGSMITH_PROJECT),
1053
+ "project_root": project_root,
1054
+ "shell_allow_list": shell_allow_list,
1055
+ "extra_skills_dirs": _parse_extra_skills_dirs(
1056
+ os.environ.get(EXTRA_SKILLS_DIRS),
1057
+ _read_config_yaml_skills_dirs(),
1058
+ ),
1059
+ }
1060
+
1061
+ for field, value in refreshed.items():
1062
+ setattr(self, field, value)
1063
+
1064
+ # Sync the LANGSMITH_PROJECT env var so LangSmith tracing picks up
1065
+ # the change
1066
+ new_project = refreshed["soothe_langchain_project"]
1067
+ if new_project:
1068
+ os.environ["LANGSMITH_PROJECT"] = new_project
1069
+ elif previous["soothe_langchain_project"]:
1070
+ # Override was previously active but new value is unset; restore.
1071
+ if _original_langsmith_project:
1072
+ os.environ["LANGSMITH_PROJECT"] = _original_langsmith_project
1073
+ else:
1074
+ os.environ.pop("LANGSMITH_PROJECT", None)
1075
+
1076
+ def _display(field: str, value: object) -> str:
1077
+ if field in api_key_fields:
1078
+ return "set" if value else "unset"
1079
+ return str(value)
1080
+
1081
+ changes: list[str] = []
1082
+ for field in reloadable_fields:
1083
+ old_value = previous[field]
1084
+ new_value = refreshed[field]
1085
+ if old_value != new_value:
1086
+ changes.append(
1087
+ f"{field}: {_display(field, old_value)} -> {_display(field, new_value)}"
1088
+ )
1089
+ return changes
1090
+
1091
+ @property
1092
+ def has_openai(self) -> bool:
1093
+ """Check if OpenAI API key is configured."""
1094
+ return self.openai_api_key is not None
1095
+
1096
+ @property
1097
+ def has_anthropic(self) -> bool:
1098
+ """Check if Anthropic API key is configured."""
1099
+ return self.anthropic_api_key is not None
1100
+
1101
+ @property
1102
+ def has_google(self) -> bool:
1103
+ """Check if Google API key is configured."""
1104
+ return self.google_api_key is not None
1105
+
1106
+ @property
1107
+ def has_nvidia(self) -> bool:
1108
+ """Check if NVIDIA API key is configured."""
1109
+ return self.nvidia_api_key is not None
1110
+
1111
+ @property
1112
+ def has_vertex_ai(self) -> bool:
1113
+ """Check if VertexAI is available (Google Cloud project set, no API key).
1114
+
1115
+ VertexAI uses Application Default Credentials (ADC) for authentication,
1116
+ so if GOOGLE_CLOUD_PROJECT is set and GOOGLE_API_KEY is not, we assume
1117
+ VertexAI.
1118
+ """
1119
+ return self.google_cloud_project is not None and self.google_api_key is None
1120
+
1121
+ @property
1122
+ def has_tavily(self) -> bool:
1123
+ """Check if Tavily API key is configured."""
1124
+ return self.tavily_api_key is not None
1125
+
1126
+ @property
1127
+ def user_soothe_dir(self) -> Path:
1128
+ """Get the base user-level Soothe directory (SOOTHE_HOME).
1129
+
1130
+ Returns:
1131
+ Path to SOOTHE_HOME
1132
+ """
1133
+ return Path(SOOTHE_HOME)
1134
+
1135
+ @staticmethod
1136
+ def get_user_agent_md_path(agent_name: str) -> Path:
1137
+ """Get user-level AGENTS.md path for a specific agent.
1138
+
1139
+ Returns path regardless of whether the file exists.
1140
+
1141
+ Args:
1142
+ agent_name: Name of the agent
1143
+
1144
+ Returns:
1145
+ Path to ~/SOOTHE_HOME/{agent_name}/AGENTS.md
1146
+ """
1147
+ return Path(SOOTHE_HOME) / agent_name / "AGENTS.md"
1148
+
1149
+ def get_project_agent_md_path(self) -> list[Path]:
1150
+ """Get project-level AGENTS.md paths.
1151
+
1152
+ Checks both `{project_root}/.soothe/AGENTS.md` and
1153
+ `{project_root}/AGENTS.md`, returning all that exist. If both are
1154
+ present, both are loaded and their instructions are combined, with
1155
+ `.soothe/AGENTS.md` first.
1156
+
1157
+ Returns:
1158
+ Existing AGENTS.md paths.
1159
+
1160
+ Empty if neither file exists or not in a project, one entry if
1161
+ only one is present, or two entries if both locations have the
1162
+ file.
1163
+ """
1164
+ if not self.project_root:
1165
+ return []
1166
+ from soothe_cli.tui.project_utils import find_project_agent_md
1167
+
1168
+ return find_project_agent_md(self.project_root)
1169
+
1170
+ @staticmethod
1171
+ def _is_valid_agent_name(agent_name: str) -> bool:
1172
+ """Validate to prevent invalid filesystem paths and security issues.
1173
+
1174
+ Returns:
1175
+ True if the agent name is valid, False otherwise.
1176
+ """
1177
+ if not agent_name or not agent_name.strip():
1178
+ return False
1179
+ # Allow only alphanumeric, hyphens, underscores, and whitespace
1180
+ return bool(re.match(r"^[a-zA-Z0-9_\-\s]+$", agent_name))
1181
+
1182
+ def get_agent_dir(self, agent_name: str) -> Path:
1183
+ """Get the global agent directory path.
1184
+
1185
+ Args:
1186
+ agent_name: Name of the agent
1187
+
1188
+ Returns:
1189
+ Path to ~/SOOTHE_HOME/{agent_name}
1190
+
1191
+ Raises:
1192
+ ValueError: If the agent name contains invalid characters.
1193
+ """
1194
+ if not self._is_valid_agent_name(agent_name):
1195
+ msg = (
1196
+ f"Invalid agent name: {agent_name!r}. Agent names can only "
1197
+ "contain letters, numbers, hyphens, underscores, and spaces."
1198
+ )
1199
+ raise ValueError(msg)
1200
+ return Path(SOOTHE_HOME) / agent_name
1201
+
1202
+ def ensure_agent_dir(self, agent_name: str) -> Path:
1203
+ """Ensure the global agent directory exists and return its path.
1204
+
1205
+ Args:
1206
+ agent_name: Name of the agent
1207
+
1208
+ Returns:
1209
+ Path to ~/SOOTHE_HOME/{agent_name}
1210
+
1211
+ Raises:
1212
+ ValueError: If the agent name contains invalid characters.
1213
+ """
1214
+ if not self._is_valid_agent_name(agent_name):
1215
+ msg = (
1216
+ f"Invalid agent name: {agent_name!r}. Agent names can only "
1217
+ "contain letters, numbers, hyphens, underscores, and spaces."
1218
+ )
1219
+ raise ValueError(msg)
1220
+ agent_dir = self.get_agent_dir(agent_name)
1221
+ agent_dir.mkdir(parents=True, exist_ok=True)
1222
+ return agent_dir
1223
+
1224
+ def get_user_skills_dir(self, agent_name: str) -> Path:
1225
+ """Get user-level skills directory path for a specific agent.
1226
+
1227
+ Args:
1228
+ agent_name: Name of the agent
1229
+
1230
+ Returns:
1231
+ Path to ~/SOOTHE_HOME/{agent_name}/skills/
1232
+ """
1233
+ return self.get_agent_dir(agent_name) / "skills"
1234
+
1235
+ def ensure_user_skills_dir(self, agent_name: str) -> Path:
1236
+ """Ensure user-level skills directory exists and return its path.
1237
+
1238
+ Args:
1239
+ agent_name: Name of the agent
1240
+
1241
+ Returns:
1242
+ Path to ~/SOOTHE_HOME/{agent_name}/skills/
1243
+ """
1244
+ skills_dir = self.get_user_skills_dir(agent_name)
1245
+ skills_dir.mkdir(parents=True, exist_ok=True)
1246
+ return skills_dir
1247
+
1248
+ def get_project_skills_dir(self) -> Path | None:
1249
+ """Get project-level skills directory path.
1250
+
1251
+ Returns:
1252
+ Path to {project_root}/.soothe/skills/, or None if not in a project
1253
+ """
1254
+ if not self.project_root:
1255
+ return None
1256
+ return self.project_root / ".soothe" / "skills"
1257
+
1258
+ def ensure_project_skills_dir(self) -> Path | None:
1259
+ """Ensure project-level skills directory exists and return its path.
1260
+
1261
+ Returns:
1262
+ Path to {project_root}/.soothe/skills/, or None if not in a project
1263
+ """
1264
+ if not self.project_root:
1265
+ return None
1266
+ skills_dir = self.get_project_skills_dir()
1267
+ if skills_dir is None:
1268
+ return None
1269
+ skills_dir.mkdir(parents=True, exist_ok=True)
1270
+ return skills_dir
1271
+
1272
+ def get_user_agents_dir(self, agent_name: str) -> Path:
1273
+ """Get user-level agents directory path for custom subagent definitions.
1274
+
1275
+ Args:
1276
+ agent_name: Name of the CLI agent (e.g., "Soothe")
1277
+
1278
+ Returns:
1279
+ Path to ~/SOOTHE_HOME/{agent_name}/agents/
1280
+ """
1281
+ return self.get_agent_dir(agent_name) / "agents"
1282
+
1283
+ def get_project_agents_dir(self) -> Path | None:
1284
+ """Get project-level agents directory path for custom subagent definitions.
1285
+
1286
+ Returns:
1287
+ Path to {project_root}/.soothe/agents/, or None if not in a project
1288
+ """
1289
+ if not self.project_root:
1290
+ return None
1291
+ return self.project_root / ".soothe" / "agents"
1292
+
1293
+ @property
1294
+ def user_agents_dir(self) -> Path:
1295
+ """Get the base user-level `.agents` directory (`~/.agents`).
1296
+
1297
+ Returns:
1298
+ Path to `~/.agents`
1299
+ """
1300
+ return Path.home() / ".agents"
1301
+
1302
+ def get_user_agent_skills_dir(self) -> Path:
1303
+ """Get user-level `~/.agents/skills/` directory.
1304
+
1305
+ This is a generic alias path for skills that is tool-agnostic.
1306
+
1307
+ Returns:
1308
+ Path to `~/.agents/skills/`
1309
+ """
1310
+ return self.user_agents_dir / "skills"
1311
+
1312
+ def get_project_agent_skills_dir(self) -> Path | None:
1313
+ """Get project-level `.agents/skills/` directory.
1314
+
1315
+ This is a generic alias path for skills that is tool-agnostic.
1316
+
1317
+ Returns:
1318
+ Path to `{project_root}/.agents/skills/`, or `None` if not in a project
1319
+ """
1320
+ if not self.project_root:
1321
+ return None
1322
+ return self.project_root / ".agents" / "skills"
1323
+
1324
+ @staticmethod
1325
+ def get_user_claude_skills_dir() -> Path:
1326
+ """Get user-level `~/.claude/skills/` directory (experimental).
1327
+
1328
+ Convenience bridge for cross-tool skill sharing with Claude Code.
1329
+ This is experimental and may be removed.
1330
+
1331
+ Returns:
1332
+ Path to `~/.claude/skills/`
1333
+ """
1334
+ return Path.home() / ".claude" / "skills"
1335
+
1336
+ def get_project_claude_skills_dir(self) -> Path | None:
1337
+ """Get project-level `.claude/skills/` directory (experimental).
1338
+
1339
+ Convenience bridge for cross-tool skill sharing with Claude Code.
1340
+ This is experimental and may be removed.
1341
+
1342
+ Returns:
1343
+ Path to `{project_root}/.claude/skills/`, or `None` if not in a project.
1344
+ """
1345
+ if not self.project_root:
1346
+ return None
1347
+ return self.project_root / ".claude" / "skills"
1348
+
1349
+ @staticmethod
1350
+ def get_built_in_skills_dir() -> Path:
1351
+ """Get the directory containing built-in skills that ship with the CLI.
1352
+
1353
+ Returns:
1354
+ Path to the `built_in_skills/` directory within the package.
1355
+ """
1356
+ return Path(__file__).parent / "built_in_skills"
1357
+
1358
+ def get_extra_skills_dirs(self) -> list[Path]:
1359
+ """Get user-configured extra skill directories.
1360
+
1361
+ Set via `SOOTHE_EXTRA_SKILLS_DIRS` (colon-separated paths) or
1362
+ `[skills].extra_allowed_dirs` in `SOOTHE_HOME/config/config.yml`.
1363
+
1364
+ Returns:
1365
+ List of extra skill directory paths, or empty list if not configured.
1366
+ """
1367
+ return self.extra_skills_dirs or []
1368
+
1369
+
1370
+ class SessionState:
1371
+ """Mutable session state shared across the app, adapter, and agent.
1372
+
1373
+ Tracks runtime flags like auto-approve that can be toggled during a
1374
+ session via keybindings or the HITL approval menu's "Auto-approve all"
1375
+ option.
1376
+
1377
+ The `auto_approve` flag controls whether tool calls (shell execution, file
1378
+ writes/edits, web search, URL fetch) require user confirmation before running.
1379
+ """
1380
+
1381
+ def __init__(self, auto_approve: bool = False, no_splash: bool = False) -> None:
1382
+ """Initialize session state with optional flags.
1383
+
1384
+ Args:
1385
+ auto_approve: Whether to auto-approve tool calls without
1386
+ prompting.
1387
+
1388
+ Can be toggled at runtime via Shift+Tab or the HITL
1389
+ approval menu.
1390
+ no_splash: Whether to skip displaying the splash screen on startup.
1391
+ """
1392
+ self.auto_approve = auto_approve
1393
+ self.no_splash = no_splash
1394
+ self.exit_hint_until: float | None = None
1395
+ self.exit_hint_handle = None
1396
+ from soothe_cli.tui.sessions import generate_thread_id
1397
+
1398
+ self.thread_id = generate_thread_id()
1399
+
1400
+ def toggle_auto_approve(self) -> bool:
1401
+ """Toggle auto-approve and return the new state.
1402
+
1403
+ Called by the Shift+Tab keybinding in the Textual app.
1404
+
1405
+ When auto-approve is on, all tool calls execute without prompting.
1406
+
1407
+ Returns:
1408
+ The new `auto_approve` state after toggling.
1409
+ """
1410
+ self.auto_approve = not self.auto_approve
1411
+ return self.auto_approve
1412
+
1413
+
1414
+ SHELL_TOOL_NAMES: frozenset[str] = frozenset({"bash", "shell", "execute"})
1415
+ """Tool names recognized as shell/command-execution tools.
1416
+
1417
+ Only `'execute'` is registered by the SDK and CLI backends in practice.
1418
+ `'bash'` and `'shell'` are legacy names carried over and kept as
1419
+ backwards-compatible aliases.
1420
+ """
1421
+
1422
+ DANGEROUS_SHELL_PATTERNS = (
1423
+ "$(", # Command substitution
1424
+ "`", # Backtick command substitution
1425
+ "$'", # ANSI-C quoting (can encode dangerous chars via escape sequences)
1426
+ "\n", # Newline (command injection)
1427
+ "\r", # Carriage return (command injection)
1428
+ "\t", # Tab (can be used for injection in some shells)
1429
+ "<(", # Process substitution (input)
1430
+ ">(", # Process substitution (output)
1431
+ "<<<", # Here-string
1432
+ "<<", # Here-doc (can embed commands)
1433
+ ">>", # Append redirect
1434
+ ">", # Output redirect
1435
+ "<", # Input redirect
1436
+ "${", # Variable expansion with braces (can run commands via ${var:-$(cmd)})
1437
+ )
1438
+ """Literal substrings that indicate shell injection risk.
1439
+
1440
+ Used by `contains_dangerous_patterns` to reject commands that embed arbitrary
1441
+ execution via redirects, substitution operators, or control characters — even
1442
+ when the base command is on the allow-list.
1443
+ """
1444
+
1445
+ RECOMMENDED_SAFE_SHELL_COMMANDS = (
1446
+ # Directory listing
1447
+ "ls",
1448
+ "dir",
1449
+ # File content viewing (read-only)
1450
+ "cat",
1451
+ "head",
1452
+ "tail",
1453
+ # Text searching (read-only)
1454
+ "grep",
1455
+ "wc",
1456
+ "strings",
1457
+ # Text processing (read-only, no shell execution)
1458
+ "cut",
1459
+ "tr",
1460
+ "diff",
1461
+ "md5sum",
1462
+ "sha256sum",
1463
+ # Path utilities
1464
+ "pwd",
1465
+ "which",
1466
+ # System info (read-only)
1467
+ "uname",
1468
+ "hostname",
1469
+ "whoami",
1470
+ "id",
1471
+ "groups",
1472
+ "uptime",
1473
+ "nproc",
1474
+ "lscpu",
1475
+ "lsmem",
1476
+ # Process viewing (read-only)
1477
+ "ps",
1478
+ )
1479
+ """Read-only commands auto-approved in non-interactive mode.
1480
+
1481
+ Only includes readers and formatters — shells, editors, interpreters, package
1482
+ managers, network tools, archivers, and anything on GTFOBins/LOOBins is
1483
+ intentionally excluded. File-write and injection vectors are blocked separately
1484
+ by `DANGEROUS_SHELL_PATTERNS`.
1485
+ """
1486
+
1487
+
1488
+ def contains_dangerous_patterns(command: str) -> bool:
1489
+ """Check if a command contains dangerous shell patterns.
1490
+
1491
+ These patterns can be used to bypass allow-list validation by embedding
1492
+ arbitrary commands within seemingly safe commands. The check includes
1493
+ both literal substring patterns (redirects, substitution operators, etc.)
1494
+ and regex patterns for bare variable expansion (`$VAR`) and the background
1495
+ operator (`&`).
1496
+
1497
+ Args:
1498
+ command: The shell command to check.
1499
+
1500
+ Returns:
1501
+ True if dangerous patterns are found, False otherwise.
1502
+ """
1503
+ if any(pattern in command for pattern in DANGEROUS_SHELL_PATTERNS):
1504
+ return True
1505
+
1506
+ # Bare variable expansion ($VAR without braces) can leak sensitive paths.
1507
+ # We already block ${ and $( above; this catches plain $HOME, $IFS, etc.
1508
+ if re.search(r"\$[A-Za-z_]", command):
1509
+ return True
1510
+
1511
+ # Standalone & (background execution) changes the execution model and
1512
+ # should not be allowed. We check for & that is NOT part of &&.
1513
+ return bool(re.search(r"(?<![&])&(?![&])", command))
1514
+
1515
+
1516
+ def is_shell_command_allowed(command: str, allow_list: list[str] | None) -> bool:
1517
+ """Check if a shell command is in the allow-list.
1518
+
1519
+ The allow-list matches against the first token of the command (the executable
1520
+ name). This allows read-only commands like ls, cat, grep, etc. to be
1521
+ auto-approved.
1522
+
1523
+ When `allow_list` is the `SHELL_ALLOW_ALL` sentinel, all non-empty commands
1524
+ are approved unconditionally — dangerous pattern checks are skipped.
1525
+
1526
+ SECURITY: For regular allow-lists, this function rejects commands containing
1527
+ dangerous shell patterns (command substitution, redirects, process
1528
+ substitution, etc.) BEFORE parsing, to prevent injection attacks that could
1529
+ bypass the allow-list.
1530
+
1531
+ Args:
1532
+ command: The full shell command to check.
1533
+ allow_list: List of allowed command names (e.g., `["ls", "cat", "grep"]`),
1534
+ the `SHELL_ALLOW_ALL` sentinel to allow any command, or `None`.
1535
+
1536
+ Returns:
1537
+ `True` if the command is allowed, `False` otherwise.
1538
+ """
1539
+ if not allow_list or not command or not command.strip():
1540
+ return False
1541
+
1542
+ # SHELL_ALLOW_ALL sentinel — skip pattern and token checks
1543
+ if isinstance(allow_list, _ShellAllowAll):
1544
+ return True
1545
+
1546
+ # SECURITY: Check for dangerous patterns BEFORE any parsing
1547
+ # This prevents injection attacks like: ls "$(rm -rf /)"
1548
+ if contains_dangerous_patterns(command):
1549
+ return False
1550
+
1551
+ allow_set = set(allow_list)
1552
+
1553
+ # Extract the first command token
1554
+ # Handle pipes and other shell operators by checking each command in the pipeline
1555
+ # Split by compound operators first (&&, ||), then single-char operators (|, ;).
1556
+ # Note: standalone & (background) is blocked by contains_dangerous_patterns above.
1557
+ segments = re.split(r"&&|\|\||[|;]", command)
1558
+
1559
+ # Track if we found at least one valid command
1560
+ found_command = False
1561
+
1562
+ for raw_segment in segments:
1563
+ segment = raw_segment.strip()
1564
+ if not segment:
1565
+ continue
1566
+
1567
+ try:
1568
+ # Try to parse as shell command to extract the executable name
1569
+ tokens = shlex.split(segment)
1570
+ if tokens:
1571
+ found_command = True
1572
+ cmd_name = tokens[0]
1573
+ # Check if this command is in the allow set
1574
+ if cmd_name not in allow_set:
1575
+ return False
1576
+ except ValueError:
1577
+ # If we can't parse it, be conservative and require approval
1578
+ return False
1579
+
1580
+ # All segments are allowed (and we found at least one command)
1581
+ return found_command
1582
+
1583
+
1584
+ def get_langsmith_project_name() -> str | None:
1585
+ """Resolve the LangSmith project name if tracing is configured.
1586
+
1587
+ Checks for the required API key and tracing environment variables.
1588
+ When both are present, resolves the project name with priority:
1589
+ `settings.soothe_langchain_project` (from
1590
+ `SOOTHE_CLI_LANGSMITH_PROJECT`), then `LANGSMITH_PROJECT` from the
1591
+ environment (note: this may already have been overridden at bootstrap time
1592
+ to match `SOOTHE_CLI_LANGSMITH_PROJECT`), then `'Soothe'`.
1593
+
1594
+ Returns:
1595
+ Project name string when LangSmith tracing is active, None otherwise.
1596
+ """
1597
+ from soothe_cli.tui.model_config import resolve_env_var
1598
+
1599
+ langsmith_key = resolve_env_var("LANGSMITH_API_KEY") or resolve_env_var("LANGCHAIN_API_KEY")
1600
+ langsmith_tracing = resolve_env_var("LANGSMITH_TRACING") or resolve_env_var(
1601
+ "LANGCHAIN_TRACING_V2"
1602
+ )
1603
+ if not (langsmith_key and langsmith_tracing):
1604
+ return None
1605
+
1606
+ return (
1607
+ _get_settings().soothe_langchain_project or os.environ.get("LANGSMITH_PROJECT") or "Soothe"
1608
+ )
1609
+
1610
+
1611
+ def fetch_langsmith_project_url(project_name: str) -> str | None:
1612
+ """Fetch the LangSmith project URL via the LangSmith client.
1613
+
1614
+ Successful results are cached at module level so repeated calls do not
1615
+ make additional network requests.
1616
+
1617
+ The network call runs in a daemon thread with a hard timeout of
1618
+ `_LANGSMITH_URL_LOOKUP_TIMEOUT_SECONDS`, so this function blocks the
1619
+ calling thread for at most that duration even if LangSmith is unreachable.
1620
+
1621
+ Returns None (with a debug log) on any failure: missing `langsmith` package,
1622
+ network errors, invalid project names, client initialization issues,
1623
+ or timeouts.
1624
+
1625
+ Args:
1626
+ project_name: LangSmith project name to look up.
1627
+
1628
+ Returns:
1629
+ Project URL string if found, None otherwise.
1630
+ """
1631
+ global _langsmith_url_cache # noqa: PLW0603 # Module-level cache requires global statement
1632
+
1633
+ if _langsmith_url_cache is not None:
1634
+ cached_name, cached_url = _langsmith_url_cache
1635
+ if cached_name == project_name:
1636
+ return cached_url
1637
+ # Different project name — fall through to fetch.
1638
+
1639
+ try:
1640
+ from langsmith import Client
1641
+ except ImportError:
1642
+ logger.debug(
1643
+ "Could not fetch LangSmith project URL for '%s'",
1644
+ project_name,
1645
+ exc_info=True,
1646
+ )
1647
+ return None
1648
+
1649
+ result: str | None = None
1650
+ lookup_error: Exception | None = None
1651
+ done = threading.Event()
1652
+
1653
+ def _lookup_url() -> None:
1654
+ nonlocal result, lookup_error
1655
+ try:
1656
+ from soothe_cli.tui.model_config import resolve_env_var
1657
+
1658
+ # Explicit api_key because Client() reads os.environ directly
1659
+ # and doesn't know about the SOOTHE_ prefix.
1660
+ api_key = resolve_env_var("LANGSMITH_API_KEY") or resolve_env_var("LANGCHAIN_API_KEY")
1661
+ project = Client(api_key=api_key).read_project(project_name=project_name)
1662
+ result = project.url or None
1663
+ except Exception as exc: # noqa: BLE001 # LangSmith SDK error types are not stable
1664
+ lookup_error = exc
1665
+ finally:
1666
+ done.set()
1667
+
1668
+ thread = threading.Thread(target=_lookup_url, daemon=True)
1669
+ thread.start()
1670
+
1671
+ if not done.wait(_LANGSMITH_URL_LOOKUP_TIMEOUT_SECONDS):
1672
+ logger.debug(
1673
+ "Timed out fetching LangSmith project URL for '%s' after %.1fs",
1674
+ project_name,
1675
+ _LANGSMITH_URL_LOOKUP_TIMEOUT_SECONDS,
1676
+ )
1677
+ return None
1678
+
1679
+ if lookup_error is not None:
1680
+ logger.debug(
1681
+ "Could not fetch LangSmith project URL for '%s'",
1682
+ project_name,
1683
+ exc_info=(
1684
+ type(lookup_error),
1685
+ lookup_error,
1686
+ lookup_error.__traceback__,
1687
+ ),
1688
+ )
1689
+ return None
1690
+
1691
+ if result is not None:
1692
+ _langsmith_url_cache = (project_name, result)
1693
+ return result
1694
+
1695
+
1696
+ def build_langsmith_thread_url(thread_id: str) -> str | None:
1697
+ """Build a full LangSmith thread URL if tracing is configured.
1698
+
1699
+ Combines `get_langsmith_project_name` and `fetch_langsmith_project_url`
1700
+ into a single convenience helper.
1701
+
1702
+ Args:
1703
+ thread_id: Thread identifier to build the URL for.
1704
+
1705
+ Returns:
1706
+ Full thread URL string, or `None` if unavailable (LangSmith is not
1707
+ configured or the project URL cannot be resolved.)
1708
+ """
1709
+ project_name = get_langsmith_project_name()
1710
+ if not project_name:
1711
+ return None
1712
+
1713
+ project_url = fetch_langsmith_project_url(project_name)
1714
+ if not project_url:
1715
+ return None
1716
+
1717
+ return f"{project_url.rstrip('/')}/t/{thread_id}?utm_source=Soothe"
1718
+
1719
+
1720
+ def reset_langsmith_url_cache() -> None:
1721
+ """Reset the LangSmith URL cache (for testing)."""
1722
+ global _langsmith_url_cache # noqa: PLW0603 # Module-level cache requires global statement
1723
+ _langsmith_url_cache = None
1724
+
1725
+
1726
+ def get_default_coding_instructions() -> str:
1727
+ """Get the default coding agent instructions.
1728
+
1729
+ These are the immutable base instructions that cannot be modified by the agent.
1730
+ Long-term memory (AGENTS.md) is handled separately by the middleware.
1731
+
1732
+ Returns:
1733
+ The default agent instructions as a string.
1734
+ """
1735
+ default_prompt_path = Path(__file__).parent / "default_agent_prompt.md"
1736
+ return default_prompt_path.read_text()
1737
+
1738
+
1739
+ def detect_provider(model_name: str) -> str | None:
1740
+ """Auto-detect provider from model name.
1741
+
1742
+ Intentionally duplicates a subset of LangChain's
1743
+ `_attempt_infer_model_provider` because we need to resolve the provider
1744
+ **before** calling `init_chat_model` in order to:
1745
+
1746
+ 1. Build provider-specific kwargs (API base URLs, headers, etc.) that are
1747
+ passed *into* `init_chat_model`.
1748
+ 2. Validate credentials early to surface user-friendly errors.
1749
+
1750
+ Args:
1751
+ model_name: Model name to detect provider from.
1752
+
1753
+ Returns:
1754
+ Provider name (openai, anthropic, google_genai, google_vertexai,
1755
+ nvidia) or `None` if the provider cannot be determined from the
1756
+ name alone.
1757
+ """
1758
+ model_lower = model_name.lower()
1759
+
1760
+ if model_lower.startswith(("gpt-", "o1", "o3", "o4", "chatgpt")):
1761
+ return "openai"
1762
+
1763
+ if model_lower.startswith("claude"):
1764
+ s = _get_settings()
1765
+ if not s.has_anthropic and s.has_vertex_ai:
1766
+ return "google_vertexai"
1767
+ return "anthropic"
1768
+
1769
+ if model_lower.startswith("gemini"):
1770
+ s = _get_settings()
1771
+ if s.has_vertex_ai and not s.has_google:
1772
+ return "google_vertexai"
1773
+ return "google_genai"
1774
+
1775
+ if model_lower.startswith(("nemotron", "nvidia/")):
1776
+ return "nvidia"
1777
+
1778
+ return None
1779
+
1780
+
1781
+ def _get_default_model_spec() -> str:
1782
+ """Get default model specification based on available credentials.
1783
+
1784
+ Checks in order:
1785
+
1786
+ 1. `[models].default` in config file (user's intentional preference).
1787
+ 2. `[models].recent` in config file (last `/model` switch).
1788
+ 3. Auto-detection based on available API credentials.
1789
+
1790
+ Returns:
1791
+ Model specification in provider:model format.
1792
+
1793
+ Raises:
1794
+ ModelConfigError: If no credentials are configured.
1795
+ """
1796
+ from soothe_cli.tui.model_config import ModelConfig, ModelConfigError
1797
+
1798
+ config = ModelConfig.load()
1799
+ if config.default_model:
1800
+ return config.default_model
1801
+
1802
+ if config.recent_model:
1803
+ return config.recent_model
1804
+
1805
+ s = _get_settings()
1806
+ if s.has_openai:
1807
+ return "openai:gpt-5.2"
1808
+ if s.has_anthropic:
1809
+ return "anthropic:claude-sonnet-4-6"
1810
+ if s.has_google:
1811
+ return "google_genai:gemini-3.1-pro-preview"
1812
+ if s.has_vertex_ai:
1813
+ return "google_vertexai:gemini-3.1-pro-preview"
1814
+ if s.has_nvidia:
1815
+ return "nvidia:nvidia/nemotron-3-super-120b-a12b"
1816
+
1817
+ msg = (
1818
+ "No credentials configured. Please set one of: "
1819
+ "ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY, "
1820
+ "GOOGLE_CLOUD_PROJECT, or NVIDIA_API_KEY"
1821
+ )
1822
+ raise ModelConfigError(msg)
1823
+
1824
+
1825
+ _OPENROUTER_APP_URL = "https://pypi.org/project/Soothe/"
1826
+ """Default `app_url` (maps to `HTTP-Referer`) for OpenRouter attribution.
1827
+
1828
+ See https://openrouter.ai/docs/app-attribution for details.
1829
+ """
1830
+
1831
+ _OPENROUTER_APP_TITLE = "Deep Agents CLI"
1832
+ """Default `app_title` (maps to `X-Title`) for OpenRouter attribution."""
1833
+
1834
+ _OPENROUTER_APP_CATEGORIES: list[str] = ["cli-agent"]
1835
+ """Default `app_categories` (maps to `X-OpenRouter-Categories`) for OpenRouter."""
1836
+
1837
+
1838
+ def _apply_openrouter_defaults(kwargs: dict[str, Any]) -> None:
1839
+ """Inject default OpenRouter attribution kwargs.
1840
+
1841
+ Sets `app_url`, `app_title`, and `app_categories` via `setdefault` so
1842
+ that user-supplied values in config take precedence. These map to the
1843
+ `HTTP-Referer`, `X-Title`, and `X-OpenRouter-Categories` headers that
1844
+ `ChatOpenRouter` sends for app attribution
1845
+ (see https://openrouter.ai/docs/app-attribution).
1846
+
1847
+ Users can override either value provider-wide or per-model in
1848
+ `SOOTHE_HOME/config/config.yml`:
1849
+
1850
+ ```yaml
1851
+ # Provider-wide
1852
+ [models.providers.openrouter.params]
1853
+ app_url = "https://myapp.com"
1854
+ app_title = "My App"
1855
+
1856
+ # Per-model (shallow-merges on top of provider-wide)
1857
+ [models.providers.openrouter.params."openai/gpt-oss-120b"]
1858
+ app_title = "My App (GPT)"
1859
+ ```
1860
+
1861
+ Args:
1862
+ kwargs: Mutable kwargs dict to update in place.
1863
+ """
1864
+ kwargs.setdefault("app_url", _OPENROUTER_APP_URL)
1865
+ kwargs.setdefault("app_title", _OPENROUTER_APP_TITLE)
1866
+ kwargs.setdefault("app_categories", _OPENROUTER_APP_CATEGORIES)
1867
+
1868
+
1869
+ def _get_provider_kwargs(provider: str, *, model_name: str | None = None) -> dict[str, Any]:
1870
+ """Get provider-specific kwargs from the config file.
1871
+
1872
+ Reads `base_url`, `api_key_env`, and the `params` table from the user's
1873
+ `config.yml` for the given provider.
1874
+
1875
+ When `model_name` is provided, per-model overrides from the `params`
1876
+ sub-table are shallow-merged on top.
1877
+
1878
+ Args:
1879
+ provider: Provider name (e.g., openai, anthropic, fireworks, ollama).
1880
+ model_name: Optional model name for per-model overrides.
1881
+
1882
+ Returns:
1883
+ Dictionary of provider-specific kwargs.
1884
+ """
1885
+ from soothe_cli.tui.model_config import ModelConfig
1886
+
1887
+ config = ModelConfig.load()
1888
+ result: dict[str, Any] = config.get_kwargs(provider, model_name=model_name)
1889
+ base_url = config.get_base_url(provider)
1890
+ if base_url:
1891
+ result["base_url"] = base_url
1892
+ from soothe_cli.tui.model_config import PROVIDER_API_KEY_ENV, resolve_env_var
1893
+
1894
+ api_key_env = config.get_api_key_env(provider)
1895
+ if not api_key_env:
1896
+ api_key_env = PROVIDER_API_KEY_ENV.get(provider)
1897
+ if api_key_env:
1898
+ logger.debug(
1899
+ "No api_key_env in config.yml for '%s'; using hardcoded provider env var",
1900
+ provider,
1901
+ )
1902
+ if api_key_env:
1903
+ api_key = resolve_env_var(api_key_env)
1904
+ if api_key:
1905
+ result["api_key"] = api_key
1906
+
1907
+ if provider == "openrouter":
1908
+ _apply_openrouter_defaults(result)
1909
+
1910
+ return result
1911
+
1912
+
1913
+ def _create_model_from_class(
1914
+ class_path: str,
1915
+ model_name: str,
1916
+ provider: str,
1917
+ kwargs: dict[str, Any],
1918
+ ) -> BaseChatModel:
1919
+ """Import and instantiate a custom `BaseChatModel` class.
1920
+
1921
+ Args:
1922
+ class_path: Fully-qualified class in `module.path:ClassName` format.
1923
+ model_name: Model identifier to pass as `model` kwarg.
1924
+ provider: Provider name (for error messages).
1925
+ kwargs: Additional keyword arguments for the constructor.
1926
+
1927
+ Returns:
1928
+ Instantiated `BaseChatModel`.
1929
+
1930
+ Raises:
1931
+ ModelConfigError: If the class cannot be imported, is not a
1932
+ `BaseChatModel` subclass, or fails to instantiate.
1933
+ """
1934
+ from langchain_core.language_models import (
1935
+ BaseChatModel as _BaseChatModel, # Runtime import; module level is typing only
1936
+ )
1937
+
1938
+ from soothe_cli.tui.model_config import ModelConfigError
1939
+
1940
+ if ":" not in class_path:
1941
+ msg = f"Invalid class_path '{class_path}' for provider '{provider}': must be in module.path:ClassName format"
1942
+ raise ModelConfigError(msg)
1943
+
1944
+ module_path, class_name = class_path.rsplit(":", 1)
1945
+
1946
+ try:
1947
+ module = importlib.import_module(module_path)
1948
+ except ImportError as e:
1949
+ msg = f"Could not import module '{module_path}' for provider '{provider}': {e}"
1950
+ raise ModelConfigError(msg) from e
1951
+
1952
+ cls = getattr(module, class_name, None)
1953
+ if cls is None:
1954
+ msg = f"Class '{class_name}' not found in module '{module_path}' for provider '{provider}'"
1955
+ raise ModelConfigError(msg)
1956
+
1957
+ if not (isinstance(cls, type) and issubclass(cls, _BaseChatModel)):
1958
+ msg = f"'{class_path}' is not a BaseChatModel subclass (got {type(cls).__name__})"
1959
+ raise ModelConfigError(msg)
1960
+
1961
+ try:
1962
+ return cls(model=model_name, **kwargs)
1963
+ except Exception as e:
1964
+ msg = f"Failed to instantiate '{class_path}' for '{provider}:{model_name}': {e}"
1965
+ raise ModelConfigError(msg) from e
1966
+
1967
+
1968
+ def _create_model_via_init(
1969
+ model_name: str,
1970
+ provider: str,
1971
+ kwargs: dict[str, Any],
1972
+ ) -> BaseChatModel:
1973
+ """Create a model using langchain's `init_chat_model`.
1974
+
1975
+ Args:
1976
+ model_name: Model identifier.
1977
+ provider: Provider name (may be empty for auto-detection).
1978
+ kwargs: Additional keyword arguments.
1979
+
1980
+ Returns:
1981
+ Instantiated `BaseChatModel`.
1982
+
1983
+ Raises:
1984
+ ModelConfigError: On import, value, or runtime errors.
1985
+ """
1986
+ from langchain.chat_models import init_chat_model
1987
+
1988
+ from soothe_cli.tui.model_config import ModelConfigError
1989
+
1990
+ try:
1991
+ if provider:
1992
+ return init_chat_model(model_name, model_provider=provider, **kwargs)
1993
+ return init_chat_model(model_name, **kwargs)
1994
+ except ImportError as e:
1995
+ import importlib.util
1996
+
1997
+ package_map = {
1998
+ "anthropic": "langchain-anthropic",
1999
+ "openai": "langchain-openai",
2000
+ "google_genai": "langchain-google-genai",
2001
+ "google_vertexai": "langchain-google-vertexai",
2002
+ "nvidia": "langchain-nvidia-ai-endpoints",
2003
+ }
2004
+ package = package_map.get(provider, f"langchain-{provider}")
2005
+ # Convert pip package name to Python module name for import check.
2006
+ module_name = package.replace("-", "_")
2007
+ try:
2008
+ spec_found = importlib.util.find_spec(module_name) is not None
2009
+ except (ImportError, ValueError):
2010
+ spec_found = False
2011
+ if spec_found:
2012
+ # Package is installed but an internal import failed — surface
2013
+ # the real error instead of the misleading "missing package" hint.
2014
+ msg = f"Provider package '{package}' is installed but failed to import for provider '{provider}': {e}"
2015
+ else:
2016
+ msg = f"Missing package for provider '{provider}'. Install: pip install {package}"
2017
+ raise ModelConfigError(msg) from e
2018
+ except (ValueError, TypeError) as e:
2019
+ spec = f"{provider}:{model_name}" if provider else model_name
2020
+ msg = f"Invalid model configuration for '{spec}': {e}"
2021
+ raise ModelConfigError(msg) from e
2022
+ except Exception as e: # provider SDK auth/network errors
2023
+ spec = f"{provider}:{model_name}" if provider else model_name
2024
+ msg = f"Failed to initialize model '{spec}': {e}"
2025
+ raise ModelConfigError(msg) from e
2026
+
2027
+
2028
+ @dataclass(frozen=True)
2029
+ class ModelResult:
2030
+ """Result of creating a chat model, bundling the model with its metadata.
2031
+
2032
+ This separates model creation from settings mutation so callers can decide
2033
+ when to commit the metadata to global settings.
2034
+
2035
+ Attributes:
2036
+ model: The instantiated chat model.
2037
+ model_name: Resolved model name.
2038
+ provider: Resolved provider name.
2039
+ context_limit: Max input tokens from the model profile, or `None`.
2040
+ unsupported_modalities: Input modalities not indicated as supported by
2041
+ the model profile (e.g. `{"audio", "video"}`).
2042
+ """
2043
+
2044
+ model: BaseChatModel
2045
+ model_name: str
2046
+ provider: str
2047
+ context_limit: int | None = None
2048
+ unsupported_modalities: frozenset[str] = frozenset()
2049
+
2050
+ def apply_to_settings(self) -> None:
2051
+ """Commit this result's metadata to global `settings`."""
2052
+ s = _get_settings()
2053
+ s.model_name = self.model_name
2054
+ s.model_provider = self.provider
2055
+ s.model_context_limit = self.context_limit
2056
+ s.model_unsupported_modalities = self.unsupported_modalities
2057
+
2058
+
2059
+ def _apply_profile_overrides(
2060
+ model: BaseChatModel,
2061
+ overrides: dict[str, Any],
2062
+ model_name: str,
2063
+ *,
2064
+ label: str,
2065
+ raise_on_failure: bool = False,
2066
+ ) -> None:
2067
+ """Merge `overrides` into `model.profile`.
2068
+
2069
+ If the model already has a dict profile, overrides are layered on top
2070
+ so existing keys (e.g., `tool_calling`) are preserved unchanged.
2071
+
2072
+ Args:
2073
+ model: The chat model whose profile will be updated.
2074
+ overrides: Key/value pairs to merge into the profile.
2075
+ model_name: Model name used in log/error messages.
2076
+ label: Human-readable source label for messages
2077
+ (e.g., `"config.yml"`, `"CLI --profile-override"`).
2078
+ raise_on_failure: When `True`, raise `ModelConfigError` instead
2079
+ of logging a warning if assignment fails.
2080
+
2081
+ Raises:
2082
+ ModelConfigError: If `raise_on_failure` is `True` and the model
2083
+ rejects profile assignment.
2084
+ """
2085
+ from soothe_cli.tui.model_config import ModelConfigError
2086
+
2087
+ logger.debug("Applying %s profile overrides: %s", label, overrides)
2088
+ profile = getattr(model, "profile", None)
2089
+ merged = {**profile, **overrides} if isinstance(profile, dict) else overrides
2090
+ try:
2091
+ model.profile = merged # type: ignore[union-attr]
2092
+ except (AttributeError, TypeError, ValueError) as exc:
2093
+ if raise_on_failure:
2094
+ msg = f"Could not apply {label} to model '{model_name}': {exc}. The model may not support profile assignment."
2095
+ raise ModelConfigError(msg) from exc
2096
+ logger.warning(
2097
+ "Could not apply %s profile overrides to model '%s': %s. Overrides will be ignored.",
2098
+ label,
2099
+ model_name,
2100
+ exc,
2101
+ )
2102
+
2103
+
2104
+ def create_model(
2105
+ model_spec: str | None = None,
2106
+ *,
2107
+ extra_kwargs: dict[str, Any] | None = None,
2108
+ profile_overrides: dict[str, Any] | None = None,
2109
+ ) -> ModelResult:
2110
+ """Create a chat model.
2111
+
2112
+ Uses `init_chat_model` for standard providers, or imports a custom
2113
+ `BaseChatModel` subclass when the provider has a `class_path` in config.
2114
+
2115
+ Supports `provider:model` format (e.g., `'anthropic:claude-sonnet-4-5'`)
2116
+ for explicit provider selection, or bare model names for auto-detection.
2117
+
2118
+ Args:
2119
+ model_spec: Model specification in `provider:model` format (e.g.,
2120
+ `'anthropic:claude-sonnet-4-5'`, `'openai:gpt-4o'`) or just the model
2121
+ name for auto-detection (e.g., `'claude-sonnet-4-5'`).
2122
+
2123
+ If not provided, uses environment-based defaults.
2124
+ extra_kwargs: Additional kwargs to pass to the model constructor.
2125
+
2126
+ These take highest priority, overriding values from the config file.
2127
+ profile_overrides: Extra profile fields from `--profile-override`.
2128
+
2129
+ Merged on top of config file profile overrides (CLI wins).
2130
+
2131
+ Returns:
2132
+ A `ModelResult` containing the model and its metadata.
2133
+
2134
+ Raises:
2135
+ ModelConfigError: If provider cannot be determined from the model name,
2136
+ required provider package is not installed, or no credentials are
2137
+ configured.
2138
+
2139
+ Examples:
2140
+ >>> model = create_model("anthropic:claude-sonnet-4-5")
2141
+ >>> model = create_model("openai:gpt-4o")
2142
+ >>> model = create_model("gpt-4o") # Auto-detects openai
2143
+ >>> model = create_model() # Uses environment defaults
2144
+ """
2145
+ from soothe_cli.tui.model_config import (
2146
+ IMPLICIT_AUTH_PROVIDERS,
2147
+ ModelConfig,
2148
+ ModelConfigError,
2149
+ ModelSpec,
2150
+ get_credential_env_var,
2151
+ has_provider_credentials,
2152
+ )
2153
+
2154
+ if not model_spec:
2155
+ model_spec = _get_default_model_spec()
2156
+
2157
+ # Parse provider:model syntax
2158
+ provider: str
2159
+ model_name: str
2160
+ parsed = ModelSpec.try_parse(model_spec)
2161
+ if parsed:
2162
+ # Explicit provider:model (e.g., "anthropic:claude-sonnet-4-5")
2163
+ provider, model_name = parsed.provider, parsed.model
2164
+ elif ":" in model_spec:
2165
+ # Contains colon but ModelSpec rejected it (empty provider or model)
2166
+ _, _, after = model_spec.partition(":")
2167
+ if after:
2168
+ # Leading colon (e.g., ":claude-opus-4-6") — treat as bare model name
2169
+ model_name = after
2170
+ provider = detect_provider(model_name) or ""
2171
+ else:
2172
+ msg = (
2173
+ f"Invalid model spec '{model_spec}': model name is required "
2174
+ "(e.g., 'anthropic:claude-sonnet-4-5' or 'claude-sonnet-4-5')"
2175
+ )
2176
+ raise ModelConfigError(msg)
2177
+ else:
2178
+ # Bare model name — auto-detect provider or let init_chat_model infer
2179
+ model_name = model_spec
2180
+ provider = detect_provider(model_spec) or ""
2181
+
2182
+ # Early credential check — fail fast with an actionable message instead of
2183
+ # letting the provider SDK raise an opaque auth error on first invocation.
2184
+ # Providers that support implicit auth (e.g., Vertex AI ADC) are excluded
2185
+ # because their env-var mapping is not a reliable indicator.
2186
+ if provider and provider not in IMPLICIT_AUTH_PROVIDERS:
2187
+ cred_status = has_provider_credentials(provider)
2188
+ if cred_status is False:
2189
+ env_var = get_credential_env_var(provider) or f"<{provider} API key>"
2190
+ msg = f"No credentials found for provider '{provider}'. Please set the {env_var} environment variable."
2191
+ raise ModelConfigError(msg)
2192
+
2193
+ # Provider-specific kwargs (with per-model overrides)
2194
+ kwargs = _get_provider_kwargs(provider, model_name=model_name)
2195
+
2196
+ # CLI --model-params take highest priority
2197
+ if extra_kwargs:
2198
+ kwargs.update(extra_kwargs)
2199
+
2200
+ # Check if this provider uses a custom BaseChatModel class
2201
+ config = ModelConfig.load()
2202
+ class_path = config.get_class_path(provider) if provider else None
2203
+
2204
+ if class_path:
2205
+ model = _create_model_from_class(class_path, model_name, provider, kwargs)
2206
+ else:
2207
+ model = _create_model_via_init(model_name, provider, kwargs)
2208
+
2209
+ resolved_provider = provider or getattr(model, "_model_provider", provider)
2210
+
2211
+ # Apply profile overrides from config.yml (e.g., max_input_tokens)
2212
+ if provider:
2213
+ config_profile_overrides = config.get_profile_overrides(provider, model_name=model_name)
2214
+ if config_profile_overrides:
2215
+ _apply_profile_overrides(
2216
+ model,
2217
+ config_profile_overrides,
2218
+ model_name,
2219
+ label=f"config.yml (provider '{provider}')",
2220
+ )
2221
+
2222
+ # CLI --profile-override takes highest priority (on top of config.yml)
2223
+ if profile_overrides:
2224
+ _apply_profile_overrides(
2225
+ model,
2226
+ profile_overrides,
2227
+ model_name,
2228
+ label="CLI --profile-override",
2229
+ raise_on_failure=True,
2230
+ )
2231
+
2232
+ # Extract context limit and modality support from model profile
2233
+ context_limit: int | None = None
2234
+ unsupported_modalities: frozenset[str] = frozenset()
2235
+ profile = getattr(model, "profile", None)
2236
+ if isinstance(profile, dict):
2237
+ if isinstance(profile.get("max_input_tokens"), int):
2238
+ context_limit = profile["max_input_tokens"]
2239
+
2240
+ modality_keys = {
2241
+ "image_inputs": "image",
2242
+ "audio_inputs": "audio",
2243
+ "video_inputs": "video",
2244
+ "pdf_inputs": "pdf",
2245
+ }
2246
+ unsupported_modalities = frozenset(
2247
+ label for key, label in modality_keys.items() if profile.get(key) is False
2248
+ )
2249
+
2250
+ return ModelResult(
2251
+ model=model,
2252
+ model_name=model_name,
2253
+ provider=resolved_provider,
2254
+ context_limit=context_limit,
2255
+ unsupported_modalities=unsupported_modalities,
2256
+ )
2257
+
2258
+
2259
+ def validate_model_capabilities(model: BaseChatModel, model_name: str) -> None:
2260
+ """Validate that the model has required capabilities for `Soothe`.
2261
+
2262
+ Checks the model's profile (if available) to ensure it supports tool calling, which
2263
+ is required for agent functionality. Issues warnings for models without profiles or
2264
+ with limited context windows.
2265
+
2266
+ Args:
2267
+ model: The instantiated model to validate.
2268
+ model_name: Model name for error/warning messages.
2269
+
2270
+ Note:
2271
+ This validation is best-effort. Models without profiles will pass with
2272
+ a warning. Calls `sys.exit(1)` if the model's profile explicitly
2273
+ indicates `tool_calling=False`.
2274
+ """
2275
+ console = _get_console()
2276
+ profile = getattr(model, "profile", None)
2277
+
2278
+ if profile is None:
2279
+ # Model doesn't have profile data - warn but allow
2280
+ console.print(
2281
+ f"[dim][yellow]Note:[/yellow] No capability profile for "
2282
+ f"'{model_name}'. Cannot verify tool calling support.[/dim]"
2283
+ )
2284
+ return
2285
+
2286
+ if not isinstance(profile, dict):
2287
+ return
2288
+
2289
+ # Check required capability: tool_calling
2290
+ tool_calling = profile.get("tool_calling")
2291
+ if tool_calling is False:
2292
+ console.print(
2293
+ f"[bold red]Error:[/bold red] Model '{model_name}' does not support tool calling."
2294
+ )
2295
+ console.print(
2296
+ "\nDeep Agents requires tool calling for agent functionality. "
2297
+ "Please choose a model that supports tool calling."
2298
+ )
2299
+ console.print("\nSee MODELS.md for supported models.")
2300
+ sys.exit(1)
2301
+
2302
+ # Warn about potentially limited context (< 8k tokens)
2303
+ max_input_tokens = profile.get("max_input_tokens")
2304
+ if max_input_tokens and max_input_tokens < 8000: # noqa: PLR2004 # Model context window default
2305
+ console.print(
2306
+ f"[dim][yellow]Warning:[/yellow] Model '{model_name}' has limited context "
2307
+ f"({max_input_tokens:,} tokens). Agent performance may be affected.[/dim]"
2308
+ )
2309
+
2310
+
2311
+ def _get_console() -> Console:
2312
+ """Return the lazily-initialized global `Console` instance.
2313
+
2314
+ Defers the `rich.console` import until console output is actually
2315
+ needed. The result is cached in `globals()["console"]`.
2316
+
2317
+ Returns:
2318
+ The global Rich `Console` singleton.
2319
+ """
2320
+ cached = globals().get("console")
2321
+ if cached is not None:
2322
+ return cached
2323
+ with _singleton_lock:
2324
+ cached = globals().get("console")
2325
+ if cached is not None:
2326
+ return cached
2327
+ from rich.console import Console
2328
+
2329
+ inst = Console(highlight=False)
2330
+ globals()["console"] = inst
2331
+ return inst
2332
+
2333
+
2334
+ def _get_settings() -> Settings:
2335
+ """Return the lazily-initialized global `Settings` instance.
2336
+
2337
+ Ensures bootstrap has run before constructing settings. The result is cached
2338
+ in `globals()["settings"]` so subsequent access — including
2339
+ `from config import settings` in other modules — resolves instantly.
2340
+
2341
+ Returns:
2342
+ The global `Settings` singleton.
2343
+ """
2344
+ cached = globals().get("settings")
2345
+ if cached is not None:
2346
+ return cached
2347
+ with _singleton_lock:
2348
+ cached = globals().get("settings")
2349
+ if cached is not None:
2350
+ return cached
2351
+ _ensure_bootstrap()
2352
+ try:
2353
+ inst = Settings.from_environment(start_path=_bootstrap_start_path)
2354
+ except Exception:
2355
+ logger.exception(
2356
+ "Failed to initialize settings from environment (start_path=%s)",
2357
+ _bootstrap_start_path,
2358
+ )
2359
+ raise
2360
+ globals()["settings"] = inst
2361
+ return inst
2362
+
2363
+
2364
+ def __getattr__(name: str) -> Settings | Console:
2365
+ """Lazy module attributes for `settings` and `console`.
2366
+
2367
+ Defers heavy initialization until first access. Subsequent accesses hit
2368
+ the module-level attribute directly (no `__getattr__` overhead).
2369
+
2370
+ Returns:
2371
+ The requested lazy singleton.
2372
+
2373
+ Raises:
2374
+ AttributeError: If *name* is not a lazily-provided attribute.
2375
+ """
2376
+ if name == "settings":
2377
+ return _get_settings()
2378
+ if name == "console":
2379
+ return _get_console()
2380
+ msg = f"module {__name__!r} has no attribute {name!r}"
2381
+ raise AttributeError(msg)