talkops-opscloud 0.3.0b1__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.
- opscloud/__init__.py +5 -0
- opscloud/__main__.py +6 -0
- opscloud/_constants.py +53 -0
- opscloud/_debug.py +145 -0
- opscloud/_debug_buffer.py +250 -0
- opscloud/_textual_patches.py +367 -0
- opscloud/_version.py +15 -0
- opscloud/agent/__init__.py +27 -0
- opscloud/agent/config.py +168 -0
- opscloud/agent/factory.py +1103 -0
- opscloud/approval_mode.py +44 -0
- opscloud/backend/__init__.py +49 -0
- opscloud/backend/composite.py +89 -0
- opscloud/backend/local.py +183 -0
- opscloud/backend/registry.py +79 -0
- opscloud/backend/sandbox_config.py +107 -0
- opscloud/backend/sandbox_factory.py +432 -0
- opscloud/built_in_skills/remember/SKILL.md +124 -0
- opscloud/built_in_skills/skill-creator/SKILL.md +348 -0
- opscloud/built_in_skills/skill-creator/scripts/init_skill.py +371 -0
- opscloud/built_in_skills/skill-creator/scripts/quick_validate.py +165 -0
- opscloud/cli/__init__.py +1 -0
- opscloud/cli/commands/__init__.py +15 -0
- opscloud/cli/commands/auth.py +45 -0
- opscloud/cli/commands/doctor.py +100 -0
- opscloud/cli/commands/mcp.py +137 -0
- opscloud/cli/commands/threads.py +51 -0
- opscloud/cli/interactive.py +154 -0
- opscloud/cli/main.py +903 -0
- opscloud/cli/non_interactive.py +425 -0
- opscloud/cli/server_manager.py +58 -0
- opscloud/commands/__init__.py +19 -0
- opscloud/commands/_base.py +110 -0
- opscloud/commands/_guards.py +52 -0
- opscloud/commands/_router.py +146 -0
- opscloud/commands/_types.py +36 -0
- opscloud/commands/core/__init__.py +47 -0
- opscloud/commands/core/auth.py +94 -0
- opscloud/commands/core/bug.py +46 -0
- opscloud/commands/core/clear.py +106 -0
- opscloud/commands/core/cloud.py +185 -0
- opscloud/commands/core/compact.py +128 -0
- opscloud/commands/core/config_cmd.py +120 -0
- opscloud/commands/core/context.py +220 -0
- opscloud/commands/core/cost.py +44 -0
- opscloud/commands/core/doctor.py +535 -0
- opscloud/commands/core/effort.py +108 -0
- opscloud/commands/core/exit_cmd.py +41 -0
- opscloud/commands/core/fast.py +148 -0
- opscloud/commands/core/help_cmd.py +94 -0
- opscloud/commands/core/mcp.py +134 -0
- opscloud/commands/core/model.py +109 -0
- opscloud/commands/core/permissions.py +196 -0
- opscloud/commands/core/plugins.py +92 -0
- opscloud/commands/core/pool.py +108 -0
- opscloud/commands/core/resume.py +97 -0
- opscloud/commands/core/skills.py +178 -0
- opscloud/commands/power/__init__.py +51 -0
- opscloud/commands/power/agents.py +98 -0
- opscloud/commands/power/btw.py +85 -0
- opscloud/commands/power/copy.py +97 -0
- opscloud/commands/power/goal.py +548 -0
- opscloud/commands/power/loop.py +295 -0
- opscloud/commands/power/memory.py +287 -0
- opscloud/commands/power/review.py +152 -0
- opscloud/commands/power/rubric.py +287 -0
- opscloud/commands/power/runtime.py +321 -0
- opscloud/commands/power/skill_creator.py +70 -0
- opscloud/commands/power/skill_invoke.py +165 -0
- opscloud/commands/power/tasks.py +209 -0
- opscloud/commands/power/trace.py +117 -0
- opscloud/commands/power/ui_toggles.py +135 -0
- opscloud/commands/power/version.py +52 -0
- opscloud/commands/registry.py +176 -0
- opscloud/config/__init__.py +114 -0
- opscloud/config/adapters/__init__.py +5 -0
- opscloud/config/adapters/postgres.py +776 -0
- opscloud/config/adapters/sqlite.py +769 -0
- opscloud/config/aws.py +248 -0
- opscloud/config/cloud_profiles.py +437 -0
- opscloud/config/env_vars.py +81 -0
- opscloud/config/langsmith.py +319 -0
- opscloud/config/manifest.py +817 -0
- opscloud/config/metadata.py +205 -0
- opscloud/config/paths.py +604 -0
- opscloud/config/plugins.py +130 -0
- opscloud/config/settings.py +834 -0
- opscloud/config/store.py +453 -0
- opscloud/config/store_factory.py +97 -0
- opscloud/config/toml_config.py +562 -0
- opscloud/editor.py +173 -0
- opscloud/exceptions.py +65 -0
- opscloud/file_ops.py +217 -0
- opscloud/hooks/__init__.py +139 -0
- opscloud/hooks/env.py +44 -0
- opscloud/hooks/interrupt.py +121 -0
- opscloud/hooks/models/__init__.py +109 -0
- opscloud/hooks/models/adapters.py +19 -0
- opscloud/hooks/models/config.py +79 -0
- opscloud/hooks/models/domain.py +434 -0
- opscloud/hooks/models/transport.py +37 -0
- opscloud/hooks/tools.py +231 -0
- opscloud/input.py +48 -0
- opscloud/integrations/__init__.py +80 -0
- opscloud/integrations/base.py +153 -0
- opscloud/integrations/content_filter.py +119 -0
- opscloud/integrations/event_bus.py +480 -0
- opscloud/integrations/hooks.py +238 -0
- opscloud/integrations/identity.py +181 -0
- opscloud/integrations/notifications.py +134 -0
- opscloud/integrations/stream_bridge.py +397 -0
- opscloud/integrations/thread_store.py +81 -0
- opscloud/json_types.py +20 -0
- opscloud/mcp/__init__.py +57 -0
- opscloud/mcp/config.py +203 -0
- opscloud/mcp/discovery.py +229 -0
- opscloud/mcp/mcp_info.py +109 -0
- opscloud/mcp/middleware.py +102 -0
- opscloud/mcp/preload.py +505 -0
- opscloud/mcp/raw_config.py +149 -0
- opscloud/mcp/semantic_profiler.py +305 -0
- opscloud/mcp/session_manager.py +727 -0
- opscloud/mcp/trust.py +105 -0
- opscloud/memory/__init__.py +27 -0
- opscloud/memory/branch.py +61 -0
- opscloud/memory/guard.py +19 -0
- opscloud/memory/onboarding.py +120 -0
- opscloud/memory/registry.py +134 -0
- opscloud/memory/store.py +223 -0
- opscloud/middleware/__init__.py +106 -0
- opscloud/middleware/_repository_bounds.py +459 -0
- opscloud/middleware/ask_user.py +152 -0
- opscloud/middleware/auto_mode.py +60 -0
- opscloud/middleware/auto_mode_hitl.py +2434 -0
- opscloud/middleware/compaction.py +566 -0
- opscloud/middleware/configurable_model.py +233 -0
- opscloud/middleware/cost_tracking.py +1021 -0
- opscloud/middleware/glm_stall_recovery.py +109 -0
- opscloud/middleware/goal_criteria.py +1234 -0
- opscloud/middleware/goal_state_notice.py +712 -0
- opscloud/middleware/goal_tools.py +407 -0
- opscloud/middleware/headless_mcp_guard.py +88 -0
- opscloud/middleware/jev_model_router.py +1018 -0
- opscloud/middleware/local_context.py +460 -0
- opscloud/middleware/mcp_context.py +151 -0
- opscloud/middleware/mcp_middleware.py +146 -0
- opscloud/middleware/memory_guard.py +393 -0
- opscloud/middleware/model_retry.py +205 -0
- opscloud/middleware/registry.py +137 -0
- opscloud/middleware/reliable_rubric.py +709 -0
- opscloud/middleware/resume_state.py +91 -0
- opscloud/middleware/server_hooks.py +1189 -0
- opscloud/middleware/shell_allow_list.py +90 -0
- opscloud/middleware/skills.py +382 -0
- opscloud/middleware/subagent_artifacts.py +84 -0
- opscloud/middleware/subagent_telemetry.py +261 -0
- opscloud/middleware/subagents.py +243 -0
- opscloud/middleware/tool_filter.py +384 -0
- opscloud/middleware/unified_system_message.py +62 -0
- opscloud/model/__init__.py +37 -0
- opscloud/model/config.py +2992 -0
- opscloud/model/factory.py +477 -0
- opscloud/model/pool.py +459 -0
- opscloud/model/reasoning.py +282 -0
- opscloud/offload.py +173 -0
- opscloud/output.py +64 -0
- opscloud/plugins/__init__.py +189 -0
- opscloud/plugins/_json.py +22 -0
- opscloud/plugins/adapters/__init__.py +13 -0
- opscloud/plugins/adapters/agents.py +112 -0
- opscloud/plugins/adapters/commands.py +190 -0
- opscloud/plugins/adapters/mcp.py +363 -0
- opscloud/plugins/adapters/skills.py +132 -0
- opscloud/plugins/commands_cli.py +541 -0
- opscloud/plugins/discovery.py +512 -0
- opscloud/plugins/manifest.py +335 -0
- opscloud/plugins/marketplace.py +893 -0
- opscloud/plugins/models.py +334 -0
- opscloud/plugins/project_plugins.py +317 -0
- opscloud/plugins/protocol.py +31 -0
- opscloud/plugins/store.py +905 -0
- opscloud/plugins/substitution.py +122 -0
- opscloud/project_utils.py +145 -0
- opscloud/prompts/__init__.py +37 -0
- opscloud/prompts/system.py +483 -0
- opscloud/prompts/templates/system_prompt.md +166 -0
- opscloud/prompts/types.py +54 -0
- opscloud/rubrics/__init__.py +42 -0
- opscloud/rubrics/evaluator.py +404 -0
- opscloud/rubrics/evidence_extractor.py +229 -0
- opscloud/rubrics/generator.py +239 -0
- opscloud/rubrics/jev_compiler.py +175 -0
- opscloud/rubrics/jev_grader.py +297 -0
- opscloud/rubrics/middleware.py +8 -0
- opscloud/schema/__init__.py +107 -0
- opscloud/schema/interrupts.py +981 -0
- opscloud/security/__init__.py +91 -0
- opscloud/security/approval_mode.py +528 -0
- opscloud/security/approval_mode_source.py +230 -0
- opscloud/security/cli_ast_evaluator.py +552 -0
- opscloud/security/jev_classifier.py +651 -0
- opscloud/security/shell_safety.py +340 -0
- opscloud/security/unicode_security.py +439 -0
- opscloud/security/url_validation.py +160 -0
- opscloud/server/__init__.py +13 -0
- opscloud/server/_server_config.py +158 -0
- opscloud/server/server.py +242 -0
- opscloud/server/server_graph.py +93 -0
- opscloud/skills/__init__.py +91 -0
- opscloud/skills/commands.py +1099 -0
- opscloud/skills/invocation.py +200 -0
- opscloud/skills/load.py +388 -0
- opscloud/skills/loader.py +23 -0
- opscloud/skills/merge.py +65 -0
- opscloud/skills/registry.py +263 -0
- opscloud/skills/sources.py +174 -0
- opscloud/skills/trust.py +417 -0
- opscloud/state/__init__.py +142 -0
- opscloud/state/base.py +154 -0
- opscloud/state/cloud_context.py +262 -0
- opscloud/state/goal_channels.py +124 -0
- opscloud/state/goal_state_limits.py +175 -0
- opscloud/state/resume_state.py +58 -0
- opscloud/state/service_context.py +281 -0
- opscloud/state/session.py +957 -0
- opscloud/state/state_migration.py +86 -0
- opscloud/subagents/__init__.py +30 -0
- opscloud/subagents/loader.py +331 -0
- opscloud/subagents/subagents_parser.py +117 -0
- opscloud/subagents/types.py +25 -0
- opscloud/tools/__init__.py +36 -0
- opscloud/tools/catalog.py +37 -0
- opscloud/tools/display.py +24 -0
- opscloud/tools/fetch_url.py +176 -0
- opscloud/tools/goal_tools.py +423 -0
- opscloud/tools/registry.py +105 -0
- opscloud/tools/thread.py +32 -0
- opscloud/tools/web_search.py +215 -0
- opscloud/ui/__init__.py +119 -0
- opscloud/ui/app.py +5400 -0
- opscloud/ui/app.tcss +479 -0
- opscloud/ui/clipboard.py +147 -0
- opscloud/ui/command_registry.py +482 -0
- opscloud/ui/message_store.py +152 -0
- opscloud/ui/permission_store.py +360 -0
- opscloud/ui/preferences.py +113 -0
- opscloud/ui/prompt_manager.py +96 -0
- opscloud/ui/remote_client.py +505 -0
- opscloud/ui/textual_adapter.py +1269 -0
- opscloud/ui/theme.py +728 -0
- opscloud/ui/ui_help.py +415 -0
- opscloud/ui/widgets/__init__.py +383 -0
- opscloud/ui/widgets/_ask_user_types.py +81 -0
- opscloud/ui/widgets/_copy_spans.py +52 -0
- opscloud/ui/widgets/_inline_prompt.py +290 -0
- opscloud/ui/widgets/_js_eval_display.py +139 -0
- opscloud/ui/widgets/_links.py +175 -0
- opscloud/ui/widgets/_tool_stream.py +308 -0
- opscloud/ui/widgets/agent_selector.py +297 -0
- opscloud/ui/widgets/approval.py +741 -0
- opscloud/ui/widgets/ask_user.py +539 -0
- opscloud/ui/widgets/auth.py +695 -0
- opscloud/ui/widgets/auth_manager.py +214 -0
- opscloud/ui/widgets/auto_mode_notice.py +146 -0
- opscloud/ui/widgets/autocomplete.py +709 -0
- opscloud/ui/widgets/chat_input.py +805 -0
- opscloud/ui/widgets/cloud_selector.py +528 -0
- opscloud/ui/widgets/config_manager.py +652 -0
- opscloud/ui/widgets/debug_console.py +1269 -0
- opscloud/ui/widgets/devops_renderers.py +184 -0
- opscloud/ui/widgets/diff.py +237 -0
- opscloud/ui/widgets/effort_selector.py +166 -0
- opscloud/ui/widgets/goal_review.py +612 -0
- opscloud/ui/widgets/goal_status.py +57 -0
- opscloud/ui/widgets/infra_panel.py +141 -0
- opscloud/ui/widgets/install_confirm.py +114 -0
- opscloud/ui/widgets/loading.py +170 -0
- opscloud/ui/widgets/mcp_viewer.py +1204 -0
- opscloud/ui/widgets/messages.py +4461 -0
- opscloud/ui/widgets/model_selector.py +863 -0
- opscloud/ui/widgets/notification_center.py +108 -0
- opscloud/ui/widgets/notification_settings.py +119 -0
- opscloud/ui/widgets/operation_card.py +106 -0
- opscloud/ui/widgets/permissions_manager.py +667 -0
- opscloud/ui/widgets/plugin_manager.py +1212 -0
- opscloud/ui/widgets/pool_selector.py +376 -0
- opscloud/ui/widgets/preamble.py +53 -0
- opscloud/ui/widgets/skills_viewer.py +432 -0
- opscloud/ui/widgets/status.py +1037 -0
- opscloud/ui/widgets/subagent_panel.py +927 -0
- opscloud/ui/widgets/theme_selector.py +232 -0
- opscloud/ui/widgets/thread_selector.py +358 -0
- opscloud/ui/widgets/toast.py +31 -0
- opscloud/ui/widgets/tool_display.py +325 -0
- opscloud/ui/widgets/tool_renderers.py +234 -0
- opscloud/ui/widgets/tool_widgets.py +278 -0
- opscloud/ui/widgets/welcome.py +421 -0
- opscloud/ui/widgets/welcome_popup.py +102 -0
- opscloud/utils/__init__.py +24 -0
- opscloud/utils/cost_estimation.py +169 -0
- opscloud/utils/git.py +390 -0
- opscloud/utils/logger.py +571 -0
- opscloud/utils/session_stats.py +115 -0
- opscloud/utils/startup_error.py +37 -0
- talkops_opscloud-0.3.0b1.dist-info/METADATA +449 -0
- talkops_opscloud-0.3.0b1.dist-info/RECORD +309 -0
- talkops_opscloud-0.3.0b1.dist-info/WHEEL +4 -0
- talkops_opscloud-0.3.0b1.dist-info/entry_points.txt +2 -0
- talkops_opscloud-0.3.0b1.dist-info/licenses/LICENSE +175 -0
opscloud/cli/main.py
ADDED
|
@@ -0,0 +1,903 @@
|
|
|
1
|
+
"""Main CLI entry point and argument parser for OpsCloud.
|
|
2
|
+
|
|
3
|
+
Routes between interactive TUI mode (default), subcommands (doctor, auth,
|
|
4
|
+
threads, mcp, plugin, skill), and non-interactive headless mode
|
|
5
|
+
(-p / -n / positional prompt / piped stdin).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
import sys
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any, overload
|
|
16
|
+
|
|
17
|
+
from opscloud._version import __version__
|
|
18
|
+
from opscloud.approval_mode import ApprovalMode
|
|
19
|
+
from opscloud.cli.commands import (
|
|
20
|
+
run_auth_command,
|
|
21
|
+
run_doctor_command,
|
|
22
|
+
run_mcp_command,
|
|
23
|
+
run_threads_command,
|
|
24
|
+
setup_mcp_parser,
|
|
25
|
+
)
|
|
26
|
+
from opscloud.cli.interactive import run_interactive
|
|
27
|
+
from opscloud.cli.non_interactive import run_non_interactive
|
|
28
|
+
from opscloud.output import OutputFormat, add_json_output_arg
|
|
29
|
+
from opscloud.state.goal_state_limits import validate_rubric
|
|
30
|
+
from opscloud.utils.logger import get_logger
|
|
31
|
+
|
|
32
|
+
logger = get_logger(__name__)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ── Argument Types ───────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def positive_int(value: str) -> int:
|
|
39
|
+
"""Validate positive integer arguments (> 0)."""
|
|
40
|
+
try:
|
|
41
|
+
ivalue = int(value)
|
|
42
|
+
except ValueError as exc:
|
|
43
|
+
raise argparse.ArgumentTypeError(f"{value!r} is not a valid integer") from exc
|
|
44
|
+
if ivalue <= 0:
|
|
45
|
+
raise argparse.ArgumentTypeError(f"{value!r} must be greater than 0")
|
|
46
|
+
return ivalue
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def non_negative_int(value: str) -> int:
|
|
50
|
+
"""Validate non-negative integer arguments (>= 0)."""
|
|
51
|
+
try:
|
|
52
|
+
ivalue = int(value)
|
|
53
|
+
except ValueError as exc:
|
|
54
|
+
raise argparse.ArgumentTypeError(f"{value!r} is not a valid integer") from exc
|
|
55
|
+
if ivalue < 0:
|
|
56
|
+
raise argparse.ArgumentTypeError(f"{value!r} must be non-negative")
|
|
57
|
+
return ivalue
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def shell_allow_list_arg(value: str) -> list[str]:
|
|
61
|
+
"""Parse comma-separated list of allowed shell commands."""
|
|
62
|
+
from opscloud.config.settings import parse_shell_allow_list
|
|
63
|
+
|
|
64
|
+
parsed = parse_shell_allow_list(value)
|
|
65
|
+
return parsed or []
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# ── Rubric Resolution ────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@overload
|
|
72
|
+
def _resolve_rubric_text(rubric: None) -> None: ...
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@overload
|
|
76
|
+
def _resolve_rubric_text(rubric: str) -> str: ...
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _resolve_rubric_text(rubric: str | None) -> str | None:
|
|
80
|
+
"""Resolve the rubric from `--rubric` into one string.
|
|
81
|
+
|
|
82
|
+
`--rubric` accepts literal text, or `@path` to read a file. File paths
|
|
83
|
+
may be absolute, relative to the process working directory, or `~`-expanded.
|
|
84
|
+
"""
|
|
85
|
+
if rubric is None:
|
|
86
|
+
return None
|
|
87
|
+
|
|
88
|
+
if rubric.startswith("@"):
|
|
89
|
+
path = rubric[1:]
|
|
90
|
+
try:
|
|
91
|
+
text = Path(path).expanduser().read_text(encoding="utf-8")
|
|
92
|
+
except (OSError, UnicodeError) as exc:
|
|
93
|
+
msg = f"Could not read rubric file {path!r}: {exc}."
|
|
94
|
+
raise ValueError(msg) from exc
|
|
95
|
+
if not text.strip():
|
|
96
|
+
msg = f"Rubric file {path!r} is empty."
|
|
97
|
+
raise ValueError(msg)
|
|
98
|
+
resolved = text.strip()
|
|
99
|
+
validate_rubric(resolved)
|
|
100
|
+
return resolved
|
|
101
|
+
|
|
102
|
+
if not rubric.strip():
|
|
103
|
+
msg = "--rubric must not be empty."
|
|
104
|
+
raise ValueError(msg)
|
|
105
|
+
resolved = rubric.strip()
|
|
106
|
+
validate_rubric(resolved)
|
|
107
|
+
return resolved
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
# ── Stdin detection & piping ─────────────────────────────
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _apply_stdin_pipe(args: argparse.Namespace) -> None:
|
|
114
|
+
"""Read piped stdin and merge it into the parsed CLI arguments."""
|
|
115
|
+
explicit_stdin = getattr(args, "stdin", False)
|
|
116
|
+
if sys.stdin is None:
|
|
117
|
+
if explicit_stdin:
|
|
118
|
+
print("Error: --stdin was passed but stdin is not available.", file=sys.stderr)
|
|
119
|
+
sys.exit(1)
|
|
120
|
+
return
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
is_tty = sys.stdin.isatty()
|
|
124
|
+
except (ValueError, OSError):
|
|
125
|
+
if explicit_stdin:
|
|
126
|
+
print("Error: --stdin was passed but stdin state could not be determined.", file=sys.stderr)
|
|
127
|
+
sys.exit(1)
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
if is_tty:
|
|
131
|
+
if explicit_stdin:
|
|
132
|
+
print(
|
|
133
|
+
"Error: --stdin was passed but stdin is a terminal. Pipe input or pass a prompt directly.",
|
|
134
|
+
file=sys.stderr,
|
|
135
|
+
)
|
|
136
|
+
sys.exit(1)
|
|
137
|
+
return
|
|
138
|
+
|
|
139
|
+
max_stdin_bytes = 10 * 1024 * 1024 # 10 MiB
|
|
140
|
+
try:
|
|
141
|
+
stdin_text = sys.stdin.read(max_stdin_bytes + 1)
|
|
142
|
+
except UnicodeDecodeError:
|
|
143
|
+
print("Error: Could not read piped input — ensure the input is valid text", file=sys.stderr)
|
|
144
|
+
sys.exit(1)
|
|
145
|
+
except (OSError, ValueError) as exc:
|
|
146
|
+
if not explicit_stdin or "pytest" in str(exc).lower():
|
|
147
|
+
return
|
|
148
|
+
print(f"Error: Failed to read piped input: {exc}", file=sys.stderr)
|
|
149
|
+
sys.exit(1)
|
|
150
|
+
|
|
151
|
+
if len(stdin_text) > max_stdin_bytes:
|
|
152
|
+
print(f"Error: Piped input exceeds {max_stdin_bytes // (1024 * 1024)} MiB limit.", file=sys.stderr)
|
|
153
|
+
sys.exit(1)
|
|
154
|
+
|
|
155
|
+
stdin_text = stdin_text.strip()
|
|
156
|
+
if not stdin_text:
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
if getattr(args, "prompt", None):
|
|
160
|
+
args.prompt = f"{stdin_text}\n\n{args.prompt}"
|
|
161
|
+
elif getattr(args, "initial_prompt", None) is not None:
|
|
162
|
+
if args.initial_prompt:
|
|
163
|
+
args.initial_prompt = f"{stdin_text}\n\n{args.initial_prompt}"
|
|
164
|
+
else:
|
|
165
|
+
args.initial_prompt = stdin_text
|
|
166
|
+
elif getattr(args, "initial_skill", None) and not explicit_stdin:
|
|
167
|
+
args.initial_prompt = stdin_text
|
|
168
|
+
else:
|
|
169
|
+
args.prompt = stdin_text
|
|
170
|
+
|
|
171
|
+
# Restore terminal fd for interactive mode if possible
|
|
172
|
+
try:
|
|
173
|
+
tty_fd = os.open("/dev/tty", os.O_RDONLY)
|
|
174
|
+
os.dup2(tty_fd, 0)
|
|
175
|
+
os.close(tty_fd)
|
|
176
|
+
except OSError:
|
|
177
|
+
pass
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# ── Approval Mode Resolution ─────────────────────────────
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _resolve_approval_mode(args: argparse.Namespace) -> ApprovalMode:
|
|
184
|
+
"""Resolve flags into typed ApprovalMode."""
|
|
185
|
+
if getattr(args, "smart", False):
|
|
186
|
+
return ApprovalMode.SMART
|
|
187
|
+
if getattr(args, "auto_approve", False):
|
|
188
|
+
return ApprovalMode.AUTO
|
|
189
|
+
raw = getattr(args, "approval_mode", None)
|
|
190
|
+
if raw:
|
|
191
|
+
if raw.lower() in ("smart", "s", "jev"):
|
|
192
|
+
return ApprovalMode.SMART
|
|
193
|
+
if raw.lower() in ("always", "manual"):
|
|
194
|
+
return ApprovalMode.MANUAL
|
|
195
|
+
if raw.lower() in ("auto", "a"):
|
|
196
|
+
return ApprovalMode.AUTO
|
|
197
|
+
|
|
198
|
+
# Non-interactive / headless execution (-p / --prompt) defaults to AUTO for autonomous execution,
|
|
199
|
+
# while interactive TUI defaults to MANUAL for human-in-the-loop safety.
|
|
200
|
+
if getattr(args, "prompt", None):
|
|
201
|
+
return ApprovalMode.AUTO
|
|
202
|
+
return ApprovalMode.MANUAL
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
# ── Parser Builder ───────────────────────────────────────
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
209
|
+
"""Build the comprehensive OpsCloud command-line argument parser."""
|
|
210
|
+
parser = argparse.ArgumentParser(
|
|
211
|
+
prog="opscloud",
|
|
212
|
+
description="OpsCloud — Autonomous Cloud Operations & DevOps Coding Agent.",
|
|
213
|
+
)
|
|
214
|
+
parser.add_argument(
|
|
215
|
+
"-v",
|
|
216
|
+
"--version",
|
|
217
|
+
action="version",
|
|
218
|
+
version=f"%(prog)s {__version__}",
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# Prompt & Headless flags
|
|
222
|
+
parser.add_argument(
|
|
223
|
+
"-p",
|
|
224
|
+
"--prompt",
|
|
225
|
+
"-n",
|
|
226
|
+
"--non-interactive",
|
|
227
|
+
dest="prompt",
|
|
228
|
+
type=str,
|
|
229
|
+
default=None,
|
|
230
|
+
help="Run non-interactively (headless) with the given prompt.",
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# Interactive Initial Prompts & Seeding
|
|
234
|
+
parser.add_argument(
|
|
235
|
+
"-m",
|
|
236
|
+
"--message",
|
|
237
|
+
"--initial-prompt",
|
|
238
|
+
dest="initial_prompt",
|
|
239
|
+
type=str,
|
|
240
|
+
default=None,
|
|
241
|
+
help="Initial prompt to auto-submit when interactive session starts.",
|
|
242
|
+
)
|
|
243
|
+
parser.add_argument(
|
|
244
|
+
"-s",
|
|
245
|
+
"--skill",
|
|
246
|
+
dest="initial_skill",
|
|
247
|
+
type=str,
|
|
248
|
+
default=None,
|
|
249
|
+
help="Invoke a skill when interactive or headless session starts.",
|
|
250
|
+
)
|
|
251
|
+
parser.add_argument(
|
|
252
|
+
"--startup-cmd",
|
|
253
|
+
dest="startup_cmd",
|
|
254
|
+
type=str,
|
|
255
|
+
default=None,
|
|
256
|
+
help="Shell command executed at startup before the first prompt.",
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
# Output formatting
|
|
260
|
+
parser.add_argument(
|
|
261
|
+
"-q",
|
|
262
|
+
"--quiet",
|
|
263
|
+
action="store_true",
|
|
264
|
+
default=False,
|
|
265
|
+
help="Clean output for piping stdout — only agent's final text goes to stdout. Requires headless mode.",
|
|
266
|
+
)
|
|
267
|
+
parser.add_argument(
|
|
268
|
+
"--no-stream",
|
|
269
|
+
dest="no_stream",
|
|
270
|
+
action="store_true",
|
|
271
|
+
default=False,
|
|
272
|
+
help="Buffer response and write to stdout at once instead of streaming chunks. Requires headless mode.",
|
|
273
|
+
)
|
|
274
|
+
add_json_output_arg(parser, default="text")
|
|
275
|
+
|
|
276
|
+
# CI/CD Execution Limits
|
|
277
|
+
parser.add_argument(
|
|
278
|
+
"--max-turns",
|
|
279
|
+
dest="max_turns",
|
|
280
|
+
type=positive_int,
|
|
281
|
+
default=None,
|
|
282
|
+
help="Maximum agentic turns before stopping (>= 1). Requires headless mode.",
|
|
283
|
+
)
|
|
284
|
+
parser.add_argument(
|
|
285
|
+
"--timeout",
|
|
286
|
+
dest="timeout",
|
|
287
|
+
type=positive_int,
|
|
288
|
+
default=None,
|
|
289
|
+
help="Hard wall-clock timeout in seconds (>= 1). Exits 124 on expiry. Requires headless mode.",
|
|
290
|
+
)
|
|
291
|
+
parser.add_argument(
|
|
292
|
+
"--recursion-limit",
|
|
293
|
+
dest="recursion_limit",
|
|
294
|
+
type=positive_int,
|
|
295
|
+
default=None,
|
|
296
|
+
help="Override main agent's LangGraph recursion_limit (graph step budget).",
|
|
297
|
+
)
|
|
298
|
+
parser.add_argument(
|
|
299
|
+
"--stdin",
|
|
300
|
+
action="store_true",
|
|
301
|
+
default=False,
|
|
302
|
+
help="Read input from stdin explicitly.",
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
# Cloud & Environment Overrides
|
|
306
|
+
parser.add_argument(
|
|
307
|
+
"--read-only",
|
|
308
|
+
"--dry-run",
|
|
309
|
+
dest="read_only",
|
|
310
|
+
action="store_true",
|
|
311
|
+
default=False,
|
|
312
|
+
help="Enforce read-only cloud operations safety mode (blocks all mutating AWS/K8s/Terraform calls).",
|
|
313
|
+
)
|
|
314
|
+
parser.add_argument(
|
|
315
|
+
"--aws-profile",
|
|
316
|
+
dest="aws_profile",
|
|
317
|
+
type=str,
|
|
318
|
+
default=None,
|
|
319
|
+
help="AWS profile override for this session (e.g. staging, prod).",
|
|
320
|
+
)
|
|
321
|
+
parser.add_argument(
|
|
322
|
+
"--aws-region",
|
|
323
|
+
dest="aws_region",
|
|
324
|
+
type=str,
|
|
325
|
+
default=None,
|
|
326
|
+
help="AWS region override for this session (e.g. us-east-1, eu-west-1).",
|
|
327
|
+
)
|
|
328
|
+
parser.add_argument(
|
|
329
|
+
"-S",
|
|
330
|
+
"--shell-allow-list",
|
|
331
|
+
dest="shell_allow_list",
|
|
332
|
+
type=shell_allow_list_arg,
|
|
333
|
+
default=None,
|
|
334
|
+
help="Comma-separated list of allowed shell commands ('recommended', 'all', or comma-separated names).",
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
# Model & Config
|
|
338
|
+
parser.add_argument(
|
|
339
|
+
"-M",
|
|
340
|
+
"--model",
|
|
341
|
+
dest="model",
|
|
342
|
+
type=str,
|
|
343
|
+
default=None,
|
|
344
|
+
help="Model specification to use (e.g. anthropic:claude-3-5-sonnet, openai:gpt-4o, bedrock:anthropic.claude-3-5-sonnet).",
|
|
345
|
+
)
|
|
346
|
+
parser.add_argument(
|
|
347
|
+
"--model-params",
|
|
348
|
+
dest="model_params",
|
|
349
|
+
type=str,
|
|
350
|
+
default=None,
|
|
351
|
+
help="Extra kwargs to pass to model as JSON string (e.g. '{\"temperature\": 0.0}').",
|
|
352
|
+
)
|
|
353
|
+
parser.add_argument(
|
|
354
|
+
"--profile-override",
|
|
355
|
+
dest="profile_override",
|
|
356
|
+
type=str,
|
|
357
|
+
default=None,
|
|
358
|
+
help="Override model profile fields as JSON string.",
|
|
359
|
+
)
|
|
360
|
+
parser.add_argument(
|
|
361
|
+
"--default-model",
|
|
362
|
+
dest="default_model",
|
|
363
|
+
nargs="?",
|
|
364
|
+
const="__SHOW__",
|
|
365
|
+
default=None,
|
|
366
|
+
help="Set or show the default model for future launches.",
|
|
367
|
+
)
|
|
368
|
+
parser.add_argument(
|
|
369
|
+
"--clear-default-model",
|
|
370
|
+
dest="clear_default_model",
|
|
371
|
+
action="store_true",
|
|
372
|
+
default=False,
|
|
373
|
+
help="Clear configured default model.",
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
# Approvals
|
|
377
|
+
approval_group = parser.add_mutually_exclusive_group()
|
|
378
|
+
approval_group.add_argument(
|
|
379
|
+
"--approval-mode",
|
|
380
|
+
dest="approval_mode",
|
|
381
|
+
type=str,
|
|
382
|
+
choices=["manual", "auto", "smart"],
|
|
383
|
+
default=None,
|
|
384
|
+
help="HITL tool approval mode: 'manual', 'auto', or 'smart' (TypeSafe AI Jev).",
|
|
385
|
+
)
|
|
386
|
+
approval_group.add_argument(
|
|
387
|
+
"-y",
|
|
388
|
+
"--auto-approve",
|
|
389
|
+
dest="auto_approve",
|
|
390
|
+
action="store_true",
|
|
391
|
+
default=False,
|
|
392
|
+
help="Enable classic classifier-backed Auto mode.",
|
|
393
|
+
)
|
|
394
|
+
approval_group.add_argument(
|
|
395
|
+
"--smart",
|
|
396
|
+
dest="smart",
|
|
397
|
+
action="store_true",
|
|
398
|
+
default=False,
|
|
399
|
+
help="Enable Jev-powered Smart approval mode (<100ms System One safety gate).",
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
# MCP
|
|
403
|
+
parser.add_argument(
|
|
404
|
+
"--mcp-config",
|
|
405
|
+
dest="mcp_config",
|
|
406
|
+
type=str,
|
|
407
|
+
default=None,
|
|
408
|
+
help="Path to MCP servers JSON configuration file.",
|
|
409
|
+
)
|
|
410
|
+
parser.add_argument(
|
|
411
|
+
"--no-mcp",
|
|
412
|
+
dest="no_mcp",
|
|
413
|
+
action="store_true",
|
|
414
|
+
default=False,
|
|
415
|
+
help="Disable all MCP tool loading.",
|
|
416
|
+
)
|
|
417
|
+
parser.add_argument(
|
|
418
|
+
"--trust-project-mcp",
|
|
419
|
+
dest="trust_project_mcp",
|
|
420
|
+
action="store_true",
|
|
421
|
+
default=False,
|
|
422
|
+
help="Skip interactive confirmation prompts for project-level MCP servers.",
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
# Session & State
|
|
426
|
+
parser.add_argument(
|
|
427
|
+
"-r",
|
|
428
|
+
"--resume",
|
|
429
|
+
"--resume-thread",
|
|
430
|
+
dest="resume_thread",
|
|
431
|
+
type=str,
|
|
432
|
+
default=None,
|
|
433
|
+
help="Resume an existing session thread ID.",
|
|
434
|
+
)
|
|
435
|
+
parser.add_argument(
|
|
436
|
+
"--cwd",
|
|
437
|
+
dest="cwd",
|
|
438
|
+
type=str,
|
|
439
|
+
default=None,
|
|
440
|
+
help="Working directory path for this execution.",
|
|
441
|
+
)
|
|
442
|
+
parser.add_argument(
|
|
443
|
+
"--goal",
|
|
444
|
+
type=str,
|
|
445
|
+
default=None,
|
|
446
|
+
help="Initial goal objective to establish (for interactive TUI mode).",
|
|
447
|
+
)
|
|
448
|
+
parser.add_argument(
|
|
449
|
+
"--rubric",
|
|
450
|
+
type=str,
|
|
451
|
+
default=None,
|
|
452
|
+
help="Acceptance criteria to grade against (literal text or @path); used with headless mode.",
|
|
453
|
+
)
|
|
454
|
+
parser.add_argument(
|
|
455
|
+
"--rubric-model",
|
|
456
|
+
type=str,
|
|
457
|
+
default=None,
|
|
458
|
+
help="Model specification for the rubric grader agent.",
|
|
459
|
+
)
|
|
460
|
+
parser.add_argument(
|
|
461
|
+
"--rubric-max-iterations",
|
|
462
|
+
type=positive_int,
|
|
463
|
+
default=None,
|
|
464
|
+
help="Maximum evaluation iterations for the rubric grader.",
|
|
465
|
+
)
|
|
466
|
+
parser.add_argument(
|
|
467
|
+
"--verbose",
|
|
468
|
+
action="store_true",
|
|
469
|
+
default=False,
|
|
470
|
+
help="Enable verbose logging.",
|
|
471
|
+
)
|
|
472
|
+
parser.add_argument(
|
|
473
|
+
"--log-level",
|
|
474
|
+
choices=["debug", "info", "warning", "error", "critical"],
|
|
475
|
+
type=str.lower,
|
|
476
|
+
default=None,
|
|
477
|
+
help="Set logging level (default: info, or debug with -v).",
|
|
478
|
+
)
|
|
479
|
+
parser.add_argument(
|
|
480
|
+
"--log-dir",
|
|
481
|
+
type=str,
|
|
482
|
+
default=None,
|
|
483
|
+
help="Directory for log files (default: /tmp/opscloud_logs).",
|
|
484
|
+
)
|
|
485
|
+
parser.add_argument(
|
|
486
|
+
"--log-file",
|
|
487
|
+
type=str,
|
|
488
|
+
default=None,
|
|
489
|
+
help="Log file path or filename (default: opscloud.log in log directory).",
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
# Subcommands
|
|
493
|
+
subparsers = parser.add_subparsers(dest="subcommand", help="Operational subcommands")
|
|
494
|
+
doctor_parser = subparsers.add_parser("doctor", help="Run system & cloud environment health checks.")
|
|
495
|
+
add_json_output_arg(doctor_parser)
|
|
496
|
+
|
|
497
|
+
auth_parser = subparsers.add_parser("auth", help="Show AWS STS authentication status.")
|
|
498
|
+
add_json_output_arg(auth_parser)
|
|
499
|
+
|
|
500
|
+
threads_parser = subparsers.add_parser("threads", help="Manage session threads.")
|
|
501
|
+
add_json_output_arg(threads_parser)
|
|
502
|
+
threads_sub = threads_parser.add_subparsers(dest="threads_command")
|
|
503
|
+
threads_list = threads_sub.add_parser("list", aliases=["ls"], help="List saved session threads.")
|
|
504
|
+
add_json_output_arg(threads_list)
|
|
505
|
+
threads_del = threads_sub.add_parser("delete", help="Delete a session thread.")
|
|
506
|
+
threads_del.add_argument("thread_id", help="Thread ID to delete.")
|
|
507
|
+
add_json_output_arg(threads_del)
|
|
508
|
+
|
|
509
|
+
setup_mcp_parser(subparsers)
|
|
510
|
+
|
|
511
|
+
from opscloud.plugins.commands_cli import setup_plugin_parser
|
|
512
|
+
|
|
513
|
+
setup_plugin_parser(subparsers)
|
|
514
|
+
|
|
515
|
+
from opscloud.skills import setup_skills_parser
|
|
516
|
+
|
|
517
|
+
setup_skills_parser(subparsers)
|
|
518
|
+
|
|
519
|
+
return parser
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
# ── Argument Validation ──────────────────────────────────
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
def _validate_args(args: argparse.Namespace) -> int | None:
|
|
526
|
+
"""Validate argument compatibility and mutual exclusions."""
|
|
527
|
+
# Goal and rubric flag validations
|
|
528
|
+
if getattr(args, "goal", None) and getattr(args, "rubric", None):
|
|
529
|
+
print(
|
|
530
|
+
"Error: Cannot specify both --goal and --rubric. Use --goal for interactive sessions, or --rubric with -p for headless verification.",
|
|
531
|
+
file=sys.stderr,
|
|
532
|
+
)
|
|
533
|
+
return 1
|
|
534
|
+
|
|
535
|
+
if getattr(args, "goal", None) and getattr(args, "prompt", None):
|
|
536
|
+
print(
|
|
537
|
+
"Error: Cannot specify both --goal and --rubric/--prompt. Use --goal for interactive sessions, or --rubric with -p for headless verification.",
|
|
538
|
+
file=sys.stderr,
|
|
539
|
+
)
|
|
540
|
+
return 1
|
|
541
|
+
|
|
542
|
+
if getattr(args, "rubric", None) and not getattr(args, "prompt", None):
|
|
543
|
+
print("Error: --rubric requires -p/--prompt for headless verification.", file=sys.stderr)
|
|
544
|
+
return 1
|
|
545
|
+
|
|
546
|
+
if getattr(args, "rubric_model", None) and not getattr(args, "rubric", None):
|
|
547
|
+
print("Error: --rubric-model requires --rubric.", file=sys.stderr)
|
|
548
|
+
return 1
|
|
549
|
+
|
|
550
|
+
if getattr(args, "rubric_max_iterations", None) and not getattr(args, "rubric", None):
|
|
551
|
+
print("Error: --rubric-max-iterations requires --rubric.", file=sys.stderr)
|
|
552
|
+
return 1
|
|
553
|
+
|
|
554
|
+
if (getattr(args, "quiet", False) or getattr(args, "no_stream", False)) and not getattr(args, "prompt", None):
|
|
555
|
+
flags = []
|
|
556
|
+
if getattr(args, "quiet", False):
|
|
557
|
+
flags.append("--quiet")
|
|
558
|
+
if getattr(args, "no_stream", False):
|
|
559
|
+
flags.append("--no-stream")
|
|
560
|
+
flag = " and ".join(flags)
|
|
561
|
+
print(
|
|
562
|
+
f"Error: {flag} requires -p/--prompt, -n, positional prompt, or piped stdin.",
|
|
563
|
+
file=sys.stderr,
|
|
564
|
+
)
|
|
565
|
+
return 2
|
|
566
|
+
|
|
567
|
+
if getattr(args, "max_turns", None) is not None and not getattr(args, "prompt", None):
|
|
568
|
+
print(
|
|
569
|
+
"Error: --max-turns requires -p/--prompt, -n, positional prompt, or piped stdin.",
|
|
570
|
+
file=sys.stderr,
|
|
571
|
+
)
|
|
572
|
+
return 2
|
|
573
|
+
|
|
574
|
+
if getattr(args, "timeout", None) is not None and not getattr(args, "prompt", None):
|
|
575
|
+
print(
|
|
576
|
+
"Error: --timeout requires -p/--prompt, -n, positional prompt, or piped stdin.",
|
|
577
|
+
file=sys.stderr,
|
|
578
|
+
)
|
|
579
|
+
return 2
|
|
580
|
+
|
|
581
|
+
if getattr(args, "no_mcp", False) and getattr(args, "mcp_config", None):
|
|
582
|
+
print("Error: --no-mcp and --mcp-config are mutually exclusive.", file=sys.stderr)
|
|
583
|
+
return 2
|
|
584
|
+
|
|
585
|
+
return None
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
# ── Main Entrypoint ──────────────────────────────────────
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
def main(argv: list[str] | None = None) -> int:
|
|
592
|
+
"""Main CLI entrypoint function."""
|
|
593
|
+
raw_args = list(sys.argv[1:]) if argv is None else list(argv)
|
|
594
|
+
|
|
595
|
+
# 1. Fast-path for --version
|
|
596
|
+
if len(raw_args) == 1 and raw_args[0] in {"-v", "--version"}:
|
|
597
|
+
print(f"opscloud {__version__}")
|
|
598
|
+
return 0
|
|
599
|
+
|
|
600
|
+
parser = build_parser()
|
|
601
|
+
|
|
602
|
+
known_subcommands = {
|
|
603
|
+
"doctor",
|
|
604
|
+
"auth",
|
|
605
|
+
"threads",
|
|
606
|
+
"mcp",
|
|
607
|
+
"plugin",
|
|
608
|
+
"plugins",
|
|
609
|
+
"skill",
|
|
610
|
+
"skills",
|
|
611
|
+
"help",
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
# Hoist global logging flags to the front so they work seamlessly
|
|
615
|
+
# across both root invocations and subcommands (e.g. opscloud doctor --verbose)
|
|
616
|
+
hoisted_args: list[str] = []
|
|
617
|
+
remaining_args: list[str] = []
|
|
618
|
+
it = iter(raw_args)
|
|
619
|
+
for arg in it:
|
|
620
|
+
if arg in {"-v", "--verbose"}:
|
|
621
|
+
hoisted_args.append(arg)
|
|
622
|
+
elif arg.startswith(("--log-level=", "--log-dir=", "--log-file=")):
|
|
623
|
+
hoisted_args.append(arg)
|
|
624
|
+
elif arg in {"--log-level", "--log-dir", "--log-file"}:
|
|
625
|
+
hoisted_args.append(arg)
|
|
626
|
+
val = next(it, None)
|
|
627
|
+
if val is not None:
|
|
628
|
+
hoisted_args.append(val)
|
|
629
|
+
else:
|
|
630
|
+
remaining_args.append(arg)
|
|
631
|
+
reordered_args = hoisted_args + remaining_args
|
|
632
|
+
|
|
633
|
+
# Identify flags with value arguments vs flags without
|
|
634
|
+
flags_with_values: set[str] = {
|
|
635
|
+
opt for act in parser._actions if act.nargs not in (0, "?") for opt in act.option_strings
|
|
636
|
+
}
|
|
637
|
+
optional_val_flags: set[str] = {
|
|
638
|
+
opt for act in parser._actions if act.nargs == "?" for opt in act.option_strings
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
# Find the first positional token to check if it's a subcommand or a prompt
|
|
642
|
+
first_pos = None
|
|
643
|
+
scan_it = iter(reordered_args)
|
|
644
|
+
for token in scan_it:
|
|
645
|
+
if token in flags_with_values:
|
|
646
|
+
next(scan_it, None)
|
|
647
|
+
elif token in optional_val_flags:
|
|
648
|
+
pass
|
|
649
|
+
elif not token.startswith("-"):
|
|
650
|
+
first_pos = token
|
|
651
|
+
break
|
|
652
|
+
|
|
653
|
+
is_subcommand = first_pos in known_subcommands
|
|
654
|
+
if is_subcommand:
|
|
655
|
+
args = parser.parse_args(reordered_args)
|
|
656
|
+
else:
|
|
657
|
+
# Preprocess positional prompt if -p/--prompt not already present
|
|
658
|
+
has_prompt_flag = any(a in {"-p", "--prompt", "-n", "--non-interactive"} for a in reordered_args)
|
|
659
|
+
if first_pos is not None and not has_prompt_flag:
|
|
660
|
+
processed_args: list[str] = []
|
|
661
|
+
prompt_injected = False
|
|
662
|
+
build_it = iter(reordered_args)
|
|
663
|
+
for token in build_it:
|
|
664
|
+
if token in flags_with_values:
|
|
665
|
+
processed_args.append(token)
|
|
666
|
+
val = next(build_it, None)
|
|
667
|
+
if val is not None:
|
|
668
|
+
processed_args.append(val)
|
|
669
|
+
elif token in optional_val_flags:
|
|
670
|
+
processed_args.append(token)
|
|
671
|
+
elif not token.startswith("-") and token == first_pos and not prompt_injected:
|
|
672
|
+
processed_args.extend(["-p", token])
|
|
673
|
+
prompt_injected = True
|
|
674
|
+
else:
|
|
675
|
+
processed_args.append(token)
|
|
676
|
+
args = parser.parse_args(processed_args)
|
|
677
|
+
else:
|
|
678
|
+
args = parser.parse_args(reordered_args)
|
|
679
|
+
|
|
680
|
+
# 2. Configure system logging based on CLI flags and environment
|
|
681
|
+
from opscloud.utils.logger import configure_logging
|
|
682
|
+
|
|
683
|
+
log_level = "DEBUG" if getattr(args, "verbose", False) else getattr(args, "log_level", None)
|
|
684
|
+
log_to_console = (
|
|
685
|
+
True
|
|
686
|
+
if getattr(args, "verbose", False)
|
|
687
|
+
else (False if getattr(args, "json_output", False) or getattr(args, "quiet", False) else None)
|
|
688
|
+
)
|
|
689
|
+
|
|
690
|
+
configure_logging(
|
|
691
|
+
level=log_level,
|
|
692
|
+
log_to_console=log_to_console,
|
|
693
|
+
log_dir=getattr(args, "log_dir", None),
|
|
694
|
+
log_file=getattr(args, "log_file", None),
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
# 3. Ingest piped stdin
|
|
698
|
+
_apply_stdin_pipe(args)
|
|
699
|
+
|
|
700
|
+
# 4. Handle subcommands
|
|
701
|
+
raw_fmt = getattr(args, "output_format", "text")
|
|
702
|
+
output_format: OutputFormat = "json" if raw_fmt == "json" else "text"
|
|
703
|
+
|
|
704
|
+
if args.subcommand == "doctor":
|
|
705
|
+
return run_doctor_command(output_format)
|
|
706
|
+
elif args.subcommand == "auth":
|
|
707
|
+
return run_auth_command(output_format)
|
|
708
|
+
elif args.subcommand == "threads":
|
|
709
|
+
subcmd = getattr(args, "threads_command", None)
|
|
710
|
+
if subcmd == "delete":
|
|
711
|
+
# Delete thread command
|
|
712
|
+
import asyncio
|
|
713
|
+
from opscloud.state.session import delete_thread
|
|
714
|
+
|
|
715
|
+
try:
|
|
716
|
+
deleted = asyncio.run(delete_thread(args.thread_id))
|
|
717
|
+
if output_format == "json":
|
|
718
|
+
from opscloud.output import write_json
|
|
719
|
+
|
|
720
|
+
write_json("threads delete", {"thread_id": args.thread_id, "deleted": deleted})
|
|
721
|
+
else:
|
|
722
|
+
if deleted:
|
|
723
|
+
print(f"Deleted thread: {args.thread_id}")
|
|
724
|
+
else:
|
|
725
|
+
print(f"Thread not found: {args.thread_id}", file=sys.stderr)
|
|
726
|
+
return 1
|
|
727
|
+
return 0
|
|
728
|
+
except Exception as e:
|
|
729
|
+
print(f"Error deleting thread {args.thread_id}: {e}", file=sys.stderr)
|
|
730
|
+
return 1
|
|
731
|
+
return run_threads_command(output_format)
|
|
732
|
+
elif args.subcommand == "mcp":
|
|
733
|
+
return run_mcp_command(args)
|
|
734
|
+
elif args.subcommand in {"plugin", "plugins"}:
|
|
735
|
+
from opscloud.plugins.commands_cli import execute_plugin_command
|
|
736
|
+
|
|
737
|
+
try:
|
|
738
|
+
execute_plugin_command(args)
|
|
739
|
+
return 0
|
|
740
|
+
except SystemExit as exc:
|
|
741
|
+
return exc.code if isinstance(exc.code, int) else 1
|
|
742
|
+
elif args.subcommand in {"skill", "skills"}:
|
|
743
|
+
from opscloud.skills import execute_skills_command
|
|
744
|
+
|
|
745
|
+
try:
|
|
746
|
+
execute_skills_command(args)
|
|
747
|
+
return 0
|
|
748
|
+
except SystemExit as exc:
|
|
749
|
+
return exc.code if isinstance(exc.code, int) else 1
|
|
750
|
+
|
|
751
|
+
# 4. Handle default model configuration
|
|
752
|
+
if getattr(args, "clear_default_model", False):
|
|
753
|
+
from opscloud.model.config import clear_default_model
|
|
754
|
+
|
|
755
|
+
if clear_default_model():
|
|
756
|
+
print("Default model cleared.")
|
|
757
|
+
return 0
|
|
758
|
+
else:
|
|
759
|
+
print("Error: Could not clear default model.", file=sys.stderr)
|
|
760
|
+
return 1
|
|
761
|
+
|
|
762
|
+
if getattr(args, "default_model", None) is not None:
|
|
763
|
+
from opscloud.model.config import load_default_model, save_default_model
|
|
764
|
+
from opscloud.model.factory import normalize_model_spec
|
|
765
|
+
|
|
766
|
+
if args.default_model == "__SHOW__":
|
|
767
|
+
curr = load_default_model()
|
|
768
|
+
if curr:
|
|
769
|
+
print(f"Default model: {curr}")
|
|
770
|
+
else:
|
|
771
|
+
print("No default model set.")
|
|
772
|
+
return 0
|
|
773
|
+
|
|
774
|
+
norm = normalize_model_spec(args.default_model)
|
|
775
|
+
if save_default_model(norm):
|
|
776
|
+
print(f"Default model set to {norm}")
|
|
777
|
+
return 0
|
|
778
|
+
else:
|
|
779
|
+
print("Error: Could not save default model.", file=sys.stderr)
|
|
780
|
+
return 1
|
|
781
|
+
|
|
782
|
+
# 5. Parse model kwargs and profile JSON if provided
|
|
783
|
+
model_params: dict[str, Any] | None = None
|
|
784
|
+
if getattr(args, "model_params", None):
|
|
785
|
+
try:
|
|
786
|
+
model_params = json.loads(args.model_params)
|
|
787
|
+
if not isinstance(model_params, dict):
|
|
788
|
+
print("Error: --model-params must be a JSON object.", file=sys.stderr)
|
|
789
|
+
return 1
|
|
790
|
+
except json.JSONDecodeError as exc:
|
|
791
|
+
print(f"Error: --model-params is not valid JSON: {exc}", file=sys.stderr)
|
|
792
|
+
return 1
|
|
793
|
+
|
|
794
|
+
profile_override: dict[str, Any] | None = None
|
|
795
|
+
if getattr(args, "profile_override", None):
|
|
796
|
+
try:
|
|
797
|
+
profile_override = json.loads(args.profile_override)
|
|
798
|
+
if not isinstance(profile_override, dict):
|
|
799
|
+
print("Error: --profile-override must be a JSON object.", file=sys.stderr)
|
|
800
|
+
return 1
|
|
801
|
+
except json.JSONDecodeError as exc:
|
|
802
|
+
print(f"Error: --profile-override is not valid JSON: {exc}", file=sys.stderr)
|
|
803
|
+
return 1
|
|
804
|
+
|
|
805
|
+
# 6. Validate argument combinations
|
|
806
|
+
val_err = _validate_args(args)
|
|
807
|
+
if val_err is not None:
|
|
808
|
+
return val_err
|
|
809
|
+
|
|
810
|
+
# 7. Resolve Rubric text if provided
|
|
811
|
+
resolved_rubric = None
|
|
812
|
+
if args.rubric:
|
|
813
|
+
try:
|
|
814
|
+
resolved_rubric = _resolve_rubric_text(args.rubric)
|
|
815
|
+
except ValueError as exc:
|
|
816
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
817
|
+
return 1
|
|
818
|
+
|
|
819
|
+
# 8. Resolve Approval Mode
|
|
820
|
+
approval_mode = _resolve_approval_mode(args)
|
|
821
|
+
|
|
822
|
+
# 9. Headless / Non-interactive execution mode
|
|
823
|
+
if args.prompt:
|
|
824
|
+
model = args.model
|
|
825
|
+
if not model:
|
|
826
|
+
try:
|
|
827
|
+
from opscloud.model.factory import _get_default_model_spec
|
|
828
|
+
|
|
829
|
+
model = _get_default_model_spec()
|
|
830
|
+
except Exception as e:
|
|
831
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
832
|
+
return 1
|
|
833
|
+
|
|
834
|
+
json_out = getattr(args, "output_format", "text") == "json"
|
|
835
|
+
|
|
836
|
+
from opscloud.project_utils import ProjectContext
|
|
837
|
+
|
|
838
|
+
project_context = (
|
|
839
|
+
ProjectContext.from_user_cwd(Path(args.cwd))
|
|
840
|
+
if args.cwd
|
|
841
|
+
else ProjectContext.from_user_cwd(Path.cwd())
|
|
842
|
+
)
|
|
843
|
+
effective_cwd = str(project_context.user_cwd)
|
|
844
|
+
return run_non_interactive(
|
|
845
|
+
args.prompt,
|
|
846
|
+
model=model,
|
|
847
|
+
model_params=model_params,
|
|
848
|
+
profile_override=profile_override,
|
|
849
|
+
approval_mode=approval_mode,
|
|
850
|
+
shell_allow_list=args.shell_allow_list,
|
|
851
|
+
read_only=args.read_only,
|
|
852
|
+
aws_profile=args.aws_profile,
|
|
853
|
+
aws_region=args.aws_region,
|
|
854
|
+
quiet=args.quiet,
|
|
855
|
+
no_stream=args.no_stream,
|
|
856
|
+
json_output=json_out,
|
|
857
|
+
max_turns=args.max_turns,
|
|
858
|
+
timeout=float(args.timeout) if args.timeout is not None else None,
|
|
859
|
+
rubric=resolved_rubric,
|
|
860
|
+
rubric_model=args.rubric_model,
|
|
861
|
+
rubric_max_iterations=args.rubric_max_iterations,
|
|
862
|
+
recursion_limit=args.recursion_limit,
|
|
863
|
+
initial_skill=args.initial_skill,
|
|
864
|
+
startup_cmd=args.startup_cmd,
|
|
865
|
+
mcp_config_path=args.mcp_config,
|
|
866
|
+
no_mcp=args.no_mcp,
|
|
867
|
+
trust_project_mcp=args.trust_project_mcp,
|
|
868
|
+
thread_id=args.resume_thread,
|
|
869
|
+
cwd=effective_cwd,
|
|
870
|
+
)
|
|
871
|
+
|
|
872
|
+
# 10. Interactive TUI mode
|
|
873
|
+
from opscloud.project_utils import ProjectContext
|
|
874
|
+
|
|
875
|
+
project_context = (
|
|
876
|
+
ProjectContext.from_user_cwd(Path(args.cwd))
|
|
877
|
+
if args.cwd
|
|
878
|
+
else ProjectContext.from_user_cwd(Path.cwd())
|
|
879
|
+
)
|
|
880
|
+
effective_cwd = str(project_context.user_cwd)
|
|
881
|
+
return run_interactive(
|
|
882
|
+
model=args.model,
|
|
883
|
+
approval_mode=approval_mode,
|
|
884
|
+
resume_thread=args.resume_thread,
|
|
885
|
+
cwd=effective_cwd,
|
|
886
|
+
goal=args.goal,
|
|
887
|
+
initial_prompt=args.initial_prompt,
|
|
888
|
+
initial_skill=args.initial_skill,
|
|
889
|
+
startup_cmd=args.startup_cmd,
|
|
890
|
+
read_only=args.read_only,
|
|
891
|
+
aws_profile=args.aws_profile,
|
|
892
|
+
aws_region=args.aws_region,
|
|
893
|
+
shell_allow_list=args.shell_allow_list,
|
|
894
|
+
mcp_config_path=args.mcp_config,
|
|
895
|
+
no_mcp=args.no_mcp,
|
|
896
|
+
trust_project_mcp=args.trust_project_mcp,
|
|
897
|
+
)
|
|
898
|
+
|
|
899
|
+
|
|
900
|
+
cli_main = main
|
|
901
|
+
|
|
902
|
+
if __name__ == "__main__":
|
|
903
|
+
sys.exit(main())
|