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,297 @@
1
+ """Formatting utilities for tool call display in the CLI.
2
+
3
+ This module handles rendering tool calls and tool messages for the TUI.
4
+
5
+ Imported at module level by `textual_adapter` (itself deferred from the startup
6
+ path). Heavy SDK dependencies (e.g., `backends`) are deferred to function bodies.
7
+ """
8
+
9
+ import json
10
+ from contextlib import suppress
11
+ from pathlib import Path
12
+ from typing import Any
13
+
14
+ from soothe_cli.tui.config import MAX_ARG_LENGTH, get_glyphs
15
+ from soothe_cli.tui.unicode_security import strip_dangerous_unicode
16
+
17
+ _HIDDEN_CHAR_MARKER = " [hidden chars removed]"
18
+ """Marker appended to display values that had dangerous Unicode stripped, so
19
+ users know the value was modified for safety."""
20
+
21
+
22
+ def _format_timeout(seconds: int) -> str:
23
+ """Format timeout in human-readable units (e.g., 300 -> '5m', 3600 -> '1h').
24
+
25
+ Args:
26
+ seconds: The timeout value in seconds to format.
27
+
28
+ Returns:
29
+ Human-readable timeout string (e.g., '5m', '1h', '300s').
30
+ """
31
+ if seconds < 60: # noqa: PLR2004 # Time unit boundary
32
+ return f"{seconds}s"
33
+ if seconds < 3600 and seconds % 60 == 0: # noqa: PLR2004 # Time unit boundaries
34
+ return f"{seconds // 60}m"
35
+ if seconds % 3600 == 0:
36
+ return f"{seconds // 3600}h"
37
+ # For odd values, just show seconds
38
+ return f"{seconds}s"
39
+
40
+
41
+ def _coerce_timeout_seconds(timeout: int | str | None) -> int | None:
42
+ """Normalize timeout values to seconds for display.
43
+
44
+ Accepts integer values and numeric strings. Returns `None` for invalid
45
+ values so display formatting never raises.
46
+
47
+ Args:
48
+ timeout: Raw timeout value from tool arguments.
49
+
50
+ Returns:
51
+ Integer timeout in seconds, or `None` if unavailable/invalid.
52
+ """
53
+ if type(timeout) is int:
54
+ return timeout
55
+ if isinstance(timeout, str):
56
+ stripped = timeout.strip()
57
+ if not stripped:
58
+ return None
59
+ try:
60
+ return int(stripped)
61
+ except ValueError:
62
+ return None
63
+ return None
64
+
65
+
66
+ def truncate_value(value: str, max_length: int = MAX_ARG_LENGTH) -> str:
67
+ """Truncate a string value if it exceeds max_length.
68
+
69
+ Returns:
70
+ Truncated string with ellipsis suffix if exceeded, otherwise original.
71
+ """
72
+ if len(value) > max_length:
73
+ return value[:max_length] + get_glyphs().ellipsis
74
+ return value
75
+
76
+
77
+ def _sanitize_display_value(value: object, *, max_length: int = MAX_ARG_LENGTH) -> str:
78
+ """Sanitize a value for safe, compact terminal display.
79
+
80
+ Hidden/deceptive Unicode controls are stripped. When stripping occurs, a
81
+ marker is appended so users know the value changed for display safety.
82
+
83
+ Args:
84
+ value: Any value to display.
85
+ max_length: Maximum display length before truncation.
86
+
87
+ Returns:
88
+ Sanitized display string.
89
+ """
90
+ raw = str(value)
91
+ sanitized = strip_dangerous_unicode(raw)
92
+ display = truncate_value(sanitized, max_length)
93
+ if sanitized != raw:
94
+ return display + _HIDDEN_CHAR_MARKER
95
+ return display
96
+
97
+
98
+ def format_tool_display(tool_name: str, tool_args: dict) -> str:
99
+ """Format tool calls for display with tool-specific smart formatting.
100
+
101
+ Shows the most relevant information for each tool type rather than all arguments.
102
+
103
+ Args:
104
+ tool_name: Name of the tool being called
105
+ tool_args: Dictionary of tool arguments
106
+
107
+ Returns:
108
+ Formatted string for display (e.g., "(*) read_file(config.py)" in ASCII mode)
109
+
110
+ Examples:
111
+ read_file(path="/long/path/file.py") → "<prefix> read_file(file.py)"
112
+ web_search(query="how to code") → '<prefix> web_search("how to code")'
113
+ execute(command="pip install foo") → '<prefix> execute("pip install foo")'
114
+ """
115
+ prefix = get_glyphs().tool_prefix
116
+
117
+ def abbreviate_path(path_str: str, max_length: int = 60) -> str:
118
+ """Abbreviate a file path intelligently - show basename or relative path.
119
+
120
+ Returns:
121
+ Shortened path string suitable for display.
122
+ """
123
+ try:
124
+ path = Path(path_str)
125
+
126
+ # If it's just a filename (no directory parts), return as-is
127
+ if len(path.parts) == 1:
128
+ return path_str
129
+
130
+ # Try to get relative path from current working directory
131
+ with suppress(
132
+ ValueError, # ValueError: path is not relative to cwd
133
+ OSError, # OSError: filesystem errors when resolving paths
134
+ ):
135
+ rel_path = path.relative_to(Path.cwd())
136
+ rel_str = str(rel_path)
137
+ # Use relative if it's shorter and not too long
138
+ if len(rel_str) < len(path_str) and len(rel_str) <= max_length:
139
+ return rel_str
140
+
141
+ # If absolute path is reasonable length, use it
142
+ if len(path_str) <= max_length:
143
+ return path_str
144
+ except Exception: # noqa: BLE001 # Fallback to original string on any path resolution error
145
+ return truncate_value(path_str, max_length)
146
+ else:
147
+ # Otherwise, just show basename (filename only)
148
+ return path.name
149
+
150
+ # Tool-specific formatting - show the most important argument(s)
151
+ if tool_name in {"read_file", "write_file", "edit_file"}:
152
+ # File operations: show the primary file path argument (file_path or path)
153
+ path_value = tool_args.get("file_path")
154
+ if path_value is None:
155
+ path_value = tool_args.get("path")
156
+ if path_value is not None:
157
+ path_raw = strip_dangerous_unicode(str(path_value))
158
+ path = abbreviate_path(path_raw)
159
+ if path_raw != str(path_value):
160
+ path += _HIDDEN_CHAR_MARKER
161
+ return f"{prefix} {tool_name}({path})"
162
+
163
+ elif tool_name == "web_search":
164
+ # Web search: show the query string
165
+ if "query" in tool_args:
166
+ query = _sanitize_display_value(tool_args["query"], max_length=100)
167
+ return f'{prefix} {tool_name}("{query}")'
168
+
169
+ elif tool_name == "grep":
170
+ # Grep: show the search pattern
171
+ if "pattern" in tool_args:
172
+ pattern = _sanitize_display_value(tool_args["pattern"], max_length=70)
173
+ return f'{prefix} {tool_name}("{pattern}")'
174
+
175
+ elif tool_name == "execute":
176
+ # Execute: show the command, and timeout only if non-default
177
+ if "command" in tool_args:
178
+ command = _sanitize_display_value(tool_args["command"], max_length=120)
179
+ timeout = _coerce_timeout_seconds(tool_args.get("timeout"))
180
+ from soothe_sdk import DEFAULT_EXECUTE_TIMEOUT
181
+
182
+ if timeout is not None and timeout != DEFAULT_EXECUTE_TIMEOUT:
183
+ timeout_str = _format_timeout(timeout)
184
+ return f'{prefix} {tool_name}("{command}", timeout={timeout_str})'
185
+ return f'{prefix} {tool_name}("{command}")'
186
+
187
+ elif tool_name == "ls":
188
+ # ls: show directory, or empty if current directory
189
+ if tool_args.get("path"):
190
+ path_raw = strip_dangerous_unicode(str(tool_args["path"]))
191
+ path = abbreviate_path(path_raw)
192
+ if path_raw != str(tool_args["path"]):
193
+ path += _HIDDEN_CHAR_MARKER
194
+ return f"{prefix} {tool_name}({path})"
195
+ return f"{prefix} {tool_name}()"
196
+
197
+ elif tool_name == "glob":
198
+ # Glob: show the pattern
199
+ if "pattern" in tool_args:
200
+ pattern = _sanitize_display_value(tool_args["pattern"], max_length=80)
201
+ return f'{prefix} {tool_name}("{pattern}")'
202
+
203
+ elif tool_name == "fetch_url":
204
+ # Fetch URL: show the URL being fetched
205
+ if "url" in tool_args:
206
+ url = _sanitize_display_value(tool_args["url"], max_length=80)
207
+ return f'{prefix} {tool_name}("{url}")'
208
+
209
+ elif tool_name == "task":
210
+ # Task: show subagent type badge
211
+ agent_type = tool_args.get("subagent_type", "")
212
+ if agent_type:
213
+ agent_type = _sanitize_display_value(agent_type, max_length=40)
214
+ return f"{prefix} {tool_name} [{agent_type}]"
215
+ return f"{prefix} {tool_name}"
216
+
217
+ elif tool_name == "ask_user":
218
+ if "questions" in tool_args and isinstance(tool_args["questions"], list):
219
+ count = len(tool_args["questions"])
220
+ label = "question" if count == 1 else "questions"
221
+ return f"{prefix} {tool_name}({count} {label})"
222
+
223
+ elif tool_name == "compact_conversation":
224
+ return f"{prefix} {tool_name}()"
225
+
226
+ elif tool_name == "write_todos":
227
+ if "todos" in tool_args and isinstance(tool_args["todos"], list):
228
+ count = len(tool_args["todos"])
229
+ return f"{prefix} {tool_name}({count} items)"
230
+
231
+ # Fallback: generic formatting for unknown tools
232
+ # Show all arguments in key=value format
233
+ args_str = ", ".join(
234
+ f"{_sanitize_display_value(k, max_length=30)}={_sanitize_display_value(v, max_length=50)}"
235
+ for k, v in tool_args.items()
236
+ )
237
+ return f"{prefix} {tool_name}({args_str})"
238
+
239
+
240
+ def _format_content_block(block: dict) -> str:
241
+ """Format a single content block dict for display.
242
+
243
+ Replaces large binary payloads (e.g. base64 image/video data) with a
244
+ human-readable placeholder so they don't flood the terminal.
245
+
246
+ Args:
247
+ block: An `ImageContentBlock`, `VideoContentBlock`, or `FileContentBlock`
248
+ dictionary.
249
+
250
+ Returns:
251
+ A display-friendly string for the block.
252
+ """
253
+ if block.get("type") == "image" and isinstance(block.get("base64"), str):
254
+ b64 = block["base64"]
255
+ size_kb = len(b64) * 3 // 4 // 1024 # approximate decoded size
256
+ mime = block.get("mime_type", "image")
257
+ return f"[Image: {mime}, ~{size_kb}KB]"
258
+ if block.get("type") == "video" and isinstance(block.get("base64"), str):
259
+ b64 = block["base64"]
260
+ size_kb = len(b64) * 3 // 4 // 1024 # approximate decoded size
261
+ mime = block.get("mime_type", "video")
262
+ return f"[Video: {mime}, ~{size_kb}KB]"
263
+ if block.get("type") == "file" and isinstance(block.get("base64"), str):
264
+ b64 = block["base64"]
265
+ size_kb = len(b64) * 3 // 4 // 1024 # approximate decoded size
266
+ mime = block.get("mime_type", "file")
267
+ return f"[File: {mime}, ~{size_kb}KB]"
268
+ try:
269
+ # Preserve non-ASCII characters (CJK, emoji, etc.) instead of \uXXXX escapes
270
+ return json.dumps(block, ensure_ascii=False)
271
+ except (TypeError, ValueError):
272
+ return str(block)
273
+
274
+
275
+ def format_tool_message_content(content: Any) -> str: # noqa: ANN401 # Content can be str, list, or dict
276
+ """Convert `ToolMessage` content into a printable string.
277
+
278
+ Returns:
279
+ Formatted string representation of the tool message content.
280
+ """
281
+ if content is None:
282
+ return ""
283
+ if isinstance(content, list):
284
+ parts = []
285
+ for item in content:
286
+ if isinstance(item, str):
287
+ parts.append(item)
288
+ elif isinstance(item, dict):
289
+ parts.append(_format_content_block(item))
290
+ else:
291
+ try:
292
+ # Preserve non-ASCII characters (CJK, emoji, etc.)
293
+ parts.append(json.dumps(item, ensure_ascii=False))
294
+ except (TypeError, ValueError):
295
+ parts.append(str(item))
296
+ return "\n".join(parts)
297
+ return str(content)