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,430 @@
1
+ """Approval widget for HITL - using standard Textual patterns."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Any, ClassVar
6
+
7
+ from textual.binding import Binding, BindingType
8
+ from textual.containers import Container, Vertical, VerticalScroll
9
+ from textual.content import Content
10
+ from textual.message import Message
11
+ from textual.widgets import Static
12
+
13
+ if TYPE_CHECKING:
14
+ import asyncio
15
+
16
+ from textual import events
17
+ from textual.app import ComposeResult
18
+
19
+ from soothe_cli.tui import theme
20
+ from soothe_cli.tui.config import (
21
+ SHELL_TOOL_NAMES,
22
+ get_glyphs,
23
+ is_ascii_mode,
24
+ )
25
+ from soothe_cli.tui.unicode_security import (
26
+ check_url_safety,
27
+ detect_dangerous_unicode,
28
+ format_warning_detail,
29
+ iter_string_values,
30
+ looks_like_url_key,
31
+ render_with_unicode_markers,
32
+ strip_dangerous_unicode,
33
+ summarize_issues,
34
+ )
35
+ from soothe_cli.tui.widgets.tool_renderers import get_renderer
36
+
37
+ # Max length for truncated shell command display
38
+ _SHELL_COMMAND_TRUNCATE_LENGTH: int = 120
39
+ _WARNING_PREVIEW_LIMIT: int = 3
40
+ _WARNING_TEXT_TRUNCATE_LENGTH: int = 220
41
+
42
+
43
+ class ApprovalMenu(Container):
44
+ """Approval menu using standard Textual patterns.
45
+
46
+ Key design decisions (following mistral-vibe reference):
47
+ - Container base class with compose()
48
+ - BINDINGS for key handling (not on_key)
49
+ - can_focus_children = False to prevent focus theft
50
+ - Simple Static widgets for options
51
+ - Standard message posting
52
+ - Tool-specific widgets via renderer pattern
53
+ """
54
+
55
+ can_focus = True
56
+ can_focus_children = False
57
+
58
+ # CSS is in app.tcss - no DEFAULT_CSS needed
59
+
60
+ BINDINGS: ClassVar[list[BindingType]] = [
61
+ Binding("up", "move_up", "Up", show=False),
62
+ Binding("k", "move_up", "Up", show=False),
63
+ Binding("down", "move_down", "Down", show=False),
64
+ Binding("j", "move_down", "Down", show=False),
65
+ Binding("enter", "select", "Select", show=False),
66
+ Binding("1", "select_approve", "Approve", show=False),
67
+ Binding("y", "select_approve", "Approve", show=False),
68
+ Binding("2", "select_auto", "Auto-approve", show=False),
69
+ Binding("a", "select_auto", "Auto-approve", show=False),
70
+ Binding("3", "select_reject", "Reject", show=False),
71
+ Binding("n", "select_reject", "Reject", show=False),
72
+ Binding("e", "toggle_expand", "Expand command", show=False),
73
+ ]
74
+
75
+ class Decided(Message):
76
+ """Message sent when user makes a decision."""
77
+
78
+ def __init__(self, decision: dict[str, str]) -> None:
79
+ """Initialize a Decided message with the user's decision.
80
+
81
+ Args:
82
+ decision: Dictionary containing the decision type (e.g., 'approve',
83
+ 'reject', or 'auto_approve_all').
84
+ """
85
+ super().__init__()
86
+ self.decision = decision
87
+
88
+ # Tools that don't need detailed info display (already shown in tool call)
89
+ _MINIMAL_TOOLS: ClassVar[frozenset[str]] = SHELL_TOOL_NAMES
90
+
91
+ def __init__(
92
+ self,
93
+ action_requests: list[dict[str, Any]] | dict[str, Any],
94
+ _assistant_id: str | None = None,
95
+ id: str | None = None, # noqa: A002 # Textual widget constructor uses `id` parameter
96
+ **kwargs: Any,
97
+ ) -> None:
98
+ """Initialize the ApprovalMenu widget.
99
+
100
+ Args:
101
+ action_requests: A single action request dictionary or a list of action
102
+ request dictionaries requiring approval. Each dictionary should
103
+ contain 'name' (tool name) and 'args' (tool arguments).
104
+ _assistant_id: Optional assistant ID (currently unused, reserved for
105
+ future use).
106
+ id: Optional widget ID. Defaults to 'approval-menu'.
107
+ **kwargs: Additional keyword arguments passed to the Container base class.
108
+ """
109
+ super().__init__(id=id or "approval-menu", classes="approval-menu", **kwargs)
110
+ # Support both single request (legacy) and list of requests (batch)
111
+ if isinstance(action_requests, dict):
112
+ self._action_requests = [action_requests]
113
+ else:
114
+ self._action_requests = action_requests
115
+
116
+ # For display purposes, get tool names
117
+ self._tool_names = [r.get("name", "unknown") for r in self._action_requests]
118
+ self._selected = 0
119
+ self._future: asyncio.Future[dict[str, str]] | None = None
120
+ self._option_widgets: list[Static] = []
121
+ self._tool_info_container: Vertical | None = None
122
+ # Minimal display if ALL tools are bash/shell
123
+ self._is_minimal = all(name in self._MINIMAL_TOOLS for name in self._tool_names)
124
+ # For expandable shell commands
125
+ self._command_expanded = False
126
+ self._command_widget: Static | None = None
127
+ self._has_expandable_command = self._check_expandable_command()
128
+ self._security_warnings = self._collect_security_warnings()
129
+
130
+ def set_future(self, future: asyncio.Future[dict[str, str]]) -> None:
131
+ """Set the future to resolve when user decides."""
132
+ self._future = future
133
+
134
+ def _check_expandable_command(self) -> bool:
135
+ """Check if there's a shell command that can be expanded.
136
+
137
+ Returns:
138
+ Whether the single action request is an expandable shell command.
139
+ """
140
+ if len(self._action_requests) != 1:
141
+ return False
142
+ req = self._action_requests[0]
143
+ if req.get("name", "") not in SHELL_TOOL_NAMES:
144
+ return False
145
+ command = str(req.get("args", {}).get("command", ""))
146
+ return len(command) > _SHELL_COMMAND_TRUNCATE_LENGTH
147
+
148
+ def _get_command_display(self, *, expanded: bool) -> Content:
149
+ """Get the command display content (truncated or full).
150
+
151
+ Args:
152
+ expanded: Whether to show the full command or truncated version.
153
+
154
+ Returns:
155
+ Styled Content for the command display.
156
+
157
+ Raises:
158
+ RuntimeError: If called with empty action_requests.
159
+ """
160
+ if not self._action_requests:
161
+ msg = "_get_command_display called with empty action_requests"
162
+ raise RuntimeError(msg)
163
+ req = self._action_requests[0]
164
+ command_raw = str(req.get("args", {}).get("command", ""))
165
+ command = strip_dangerous_unicode(command_raw)
166
+ issues = detect_dangerous_unicode(command_raw)
167
+
168
+ if expanded or len(command) <= _SHELL_COMMAND_TRUNCATE_LENGTH:
169
+ command_display = command
170
+ else:
171
+ command_display = command[:_SHELL_COMMAND_TRUNCATE_LENGTH] + get_glyphs().ellipsis
172
+
173
+ if not expanded and len(command) > _SHELL_COMMAND_TRUNCATE_LENGTH:
174
+ display = Content.from_markup(
175
+ "[bold]$cmd[/bold] [dim](press 'e' to expand)[/dim]",
176
+ cmd=command_display,
177
+ )
178
+ else:
179
+ display = Content.from_markup("[bold]$cmd[/bold]", cmd=command_display)
180
+
181
+ if not issues:
182
+ return display
183
+
184
+ raw_with_markers = render_with_unicode_markers(command_raw)
185
+ if not expanded and len(raw_with_markers) > _WARNING_TEXT_TRUNCATE_LENGTH:
186
+ raw_with_markers = (
187
+ raw_with_markers[:_WARNING_TEXT_TRUNCATE_LENGTH] + get_glyphs().ellipsis
188
+ )
189
+
190
+ return Content.assemble(
191
+ display,
192
+ Content.from_markup(
193
+ "\n[yellow]Warning:[/yellow] hidden chars detected ($summary)\n[dim]raw: $raw[/dim]",
194
+ summary=summarize_issues(issues),
195
+ raw=raw_with_markers,
196
+ ),
197
+ )
198
+
199
+ def compose(self) -> ComposeResult:
200
+ """Compose the widget with Static children.
201
+
202
+ Layout: Tool info first (what's being approved), then options at bottom.
203
+ For bash/shell, skip tool info since it's already shown in tool call.
204
+
205
+ Yields:
206
+ Widgets for title, tool info, options, and help text.
207
+ """
208
+ # Title - show count if multiple tools
209
+ count = len(self._action_requests)
210
+ if count == 1:
211
+ title = Content.from_markup(">>> $name Requires Approval <<<", name=self._tool_names[0])
212
+ else:
213
+ title = Content(f">>> {count} Tool Calls Require Approval <<<")
214
+ yield Static(title, classes="approval-title")
215
+
216
+ if self._security_warnings:
217
+ parts: list[Content] = [
218
+ Content.from_markup("[yellow]Warning:[/yellow] Potentially deceptive text"),
219
+ ]
220
+ parts.extend(
221
+ Content.from_markup("\n[dim]- $w[/dim]", w=warning)
222
+ for warning in self._security_warnings[:_WARNING_PREVIEW_LIMIT]
223
+ )
224
+ if len(self._security_warnings) > _WARNING_PREVIEW_LIMIT:
225
+ remaining = len(self._security_warnings) - _WARNING_PREVIEW_LIMIT
226
+ parts.append(Content.styled(f"\n- +{remaining} more warning(s)", "dim"))
227
+ yield Static(
228
+ Content.assemble(*parts),
229
+ classes="approval-security-warning",
230
+ )
231
+
232
+ # For shell commands, show the command (expandable if long)
233
+ if self._is_minimal and len(self._action_requests) == 1:
234
+ self._command_widget = Static(
235
+ self._get_command_display(expanded=self._command_expanded),
236
+ classes="approval-command",
237
+ )
238
+ yield self._command_widget
239
+
240
+ # Tool info - only for non-minimal tools (diffs, writes show actual content)
241
+ if not self._is_minimal:
242
+ with VerticalScroll(classes="tool-info-scroll"):
243
+ self._tool_info_container = Vertical(classes="tool-info-container")
244
+ yield self._tool_info_container
245
+
246
+ # Separator between tool details and options
247
+ glyphs = get_glyphs()
248
+ yield Static(glyphs.box_horizontal * 40, classes="approval-separator")
249
+
250
+ # Options container at bottom
251
+ with Container(classes="approval-options-container"):
252
+ # Options - create 3 Static widgets
253
+ for i in range(3): # noqa: B007 # Loop variable unused - iterating for count only
254
+ widget = Static("", classes="approval-option")
255
+ self._option_widgets.append(widget)
256
+ yield widget
257
+
258
+ # Help text at the very bottom
259
+ glyphs = get_glyphs()
260
+ help_text = (
261
+ f"{glyphs.arrow_up}/{glyphs.arrow_down} navigate {glyphs.bullet} "
262
+ f"Enter select {glyphs.bullet} y/a/n quick keys {glyphs.bullet} Esc reject"
263
+ )
264
+ if self._has_expandable_command:
265
+ help_text += f" {glyphs.bullet} e expand"
266
+ yield Static(help_text, classes="approval-help")
267
+
268
+ async def on_mount(self) -> None:
269
+ """Focus self on mount and update tool info."""
270
+ if is_ascii_mode():
271
+ colors = theme.get_theme_colors(self)
272
+ self.styles.border = ("ascii", colors.warning)
273
+
274
+ if not self._is_minimal:
275
+ await self._update_tool_info()
276
+ self._update_options()
277
+ self.focus()
278
+
279
+ async def _update_tool_info(self) -> None:
280
+ """Mount the tool-specific approval widgets for all tools."""
281
+ if not self._tool_info_container:
282
+ return
283
+
284
+ # Clear existing content
285
+ await self._tool_info_container.remove_children()
286
+
287
+ # Mount info for each tool
288
+ for i, action_request in enumerate(self._action_requests):
289
+ tool_name = action_request.get("name", "unknown")
290
+ tool_args = action_request.get("args", {})
291
+
292
+ # Add tool header if multiple tools
293
+ if len(self._action_requests) > 1:
294
+ header = Static(
295
+ Content.from_markup(
296
+ "[bold]$num. $name[/bold]",
297
+ num=i + 1,
298
+ name=tool_name,
299
+ )
300
+ )
301
+ await self._tool_info_container.mount(header)
302
+
303
+ # Show description if present
304
+ description = action_request.get("description")
305
+ if description:
306
+ desc_widget = Static(
307
+ Content.from_markup("[dim]$desc[/dim]", desc=description),
308
+ classes="approval-description",
309
+ )
310
+ await self._tool_info_container.mount(desc_widget)
311
+
312
+ # Get the appropriate renderer for this tool
313
+ renderer = get_renderer(tool_name)
314
+ widget_class, data = renderer.get_approval_widget(tool_args)
315
+ approval_widget = widget_class(data)
316
+ await self._tool_info_container.mount(approval_widget)
317
+
318
+ def _update_options(self) -> None:
319
+ """Update option widgets based on selection."""
320
+ count = len(self._action_requests)
321
+ if count == 1:
322
+ options = [
323
+ "1. Approve (y)",
324
+ "2. Auto-approve for this thread (a)",
325
+ "3. Reject (n)",
326
+ ]
327
+ else:
328
+ options = [
329
+ f"1. Approve all {count} (y)",
330
+ "2. Auto-approve for this thread (a)",
331
+ f"3. Reject all {count} (n)",
332
+ ]
333
+
334
+ for i, (text, widget) in enumerate(zip(options, self._option_widgets, strict=True)):
335
+ cursor = f"{get_glyphs().cursor} " if i == self._selected else " "
336
+ widget.update(f"{cursor}{text}")
337
+
338
+ # Update classes
339
+ widget.remove_class("approval-option-selected")
340
+ if i == self._selected:
341
+ widget.add_class("approval-option-selected")
342
+
343
+ def action_move_up(self) -> None:
344
+ """Move selection up."""
345
+ self._selected = (self._selected - 1) % 3
346
+ self._update_options()
347
+
348
+ def action_move_down(self) -> None:
349
+ """Move selection down."""
350
+ self._selected = (self._selected + 1) % 3
351
+ self._update_options()
352
+
353
+ def action_select(self) -> None:
354
+ """Select current option."""
355
+ self._handle_selection(self._selected)
356
+
357
+ def action_select_approve(self) -> None:
358
+ """Select approve option."""
359
+ self._selected = 0
360
+ self._update_options()
361
+ self._handle_selection(0)
362
+
363
+ def action_select_auto(self) -> None:
364
+ """Select auto-approve option."""
365
+ self._selected = 1
366
+ self._update_options()
367
+ self._handle_selection(1)
368
+
369
+ def action_select_reject(self) -> None:
370
+ """Select reject option."""
371
+ self._selected = 2
372
+ self._update_options()
373
+ self._handle_selection(2)
374
+
375
+ def action_toggle_expand(self) -> None:
376
+ """Toggle shell command expansion."""
377
+ if not self._has_expandable_command or not self._command_widget:
378
+ return
379
+ self._command_expanded = not self._command_expanded
380
+ self._command_widget.update(self._get_command_display(expanded=self._command_expanded))
381
+
382
+ def _handle_selection(self, option: int) -> None:
383
+ """Handle the selected option."""
384
+ decision_map = {
385
+ 0: "approve",
386
+ 1: "auto_approve_all",
387
+ 2: "reject",
388
+ }
389
+ decision = {"type": decision_map[option]}
390
+
391
+ # Resolve the future
392
+ if self._future and not self._future.done():
393
+ self._future.set_result(decision)
394
+
395
+ # Post message
396
+ self.post_message(self.Decided(decision))
397
+
398
+ def _collect_security_warnings(self) -> list[str]:
399
+ """Collect warning strings for suspicious Unicode and URL values.
400
+
401
+ Recursively inspects all nested string values in action arguments.
402
+
403
+ Returns:
404
+ Warning strings for the current action request batch.
405
+ """
406
+ warnings: list[str] = []
407
+ for action_request in self._action_requests:
408
+ tool_name = str(action_request.get("name", "unknown"))
409
+ args = action_request.get("args", {})
410
+ if not isinstance(args, dict):
411
+ continue
412
+ for arg_path, text in iter_string_values(args):
413
+ issues = detect_dangerous_unicode(text)
414
+ if issues:
415
+ warnings.append(
416
+ f"{tool_name}.{arg_path}: hidden Unicode ({summarize_issues(issues)})"
417
+ )
418
+ if looks_like_url_key(arg_path):
419
+ result = check_url_safety(text)
420
+ if result.safe:
421
+ continue
422
+ detail = format_warning_detail(result.warnings)
423
+ if result.decoded_domain:
424
+ detail = f"{detail}; decoded host: {result.decoded_domain}"
425
+ warnings.append(f"{tool_name}.{arg_path}: {detail}")
426
+ return warnings
427
+
428
+ def on_blur(self, event: events.Blur) -> None: # noqa: ARG002 # Textual event handler signature
429
+ """Re-focus on blur to keep focus trapped until decision is made."""
430
+ self.call_after_refresh(self.focus)