diff --git a/agent_cost_estimator/cli.py b/agent_cost_estimator/cli.py index 290cf47..7c1da31 100644 --- a/agent_cost_estimator/cli.py +++ b/agent_cost_estimator/cli.py @@ -45,8 +45,8 @@ def parse_args(argv): "and exit; no session is read", ) parser.add_argument( - "--integrate", action="store_true", - help="install hooks for Vibe CLI to show costs after each turn", + "--integrate", type=str, default=None, metavar="HARNESS", + help="install hooks for the specified harness (e.g., 'mistral')", ) parser.add_argument( "--session-id", default=None, @@ -107,26 +107,46 @@ def parse_args(argv): return parser.parse_args(argv) -def integrate_vibe_hooks(): - """Install Vibe CLI hooks for cost tracking. +def integrate_hooks(harness_name): + """Install hooks for the specified harness. + + Currently supports: + - mistral: Installs Vibe CLI hooks for Mistral Vibe This function: - 1. Creates ~/.vibe/hooks/ directory if it doesn't exist - 2. Installs the post_turn.py hook script - 3. Creates/updates ~/.vibe/hooks.toml with hook configuration - 4. Adds agent-cost-estimator to Vibe's bash allowlist in config.toml + 1. Creates harness-specific directories + 2. Installs hook scripts + 3. Creates/updates hook configuration + 4. Adds agent-cost-estimator to bash allowlist + + Args: + harness_name: Name of the harness to integrate (e.g., 'mistral') Returns 0 on success, 1 on error. """ import os import stat + # Validate harness name + supported_harnesses = ["mistral"] # Add more harnesses here as they are supported + if harness_name not in supported_harnesses: + print(f"error: unsupported harness '{harness_name}'. " + f"Supported harnesses: {', '.join(supported_harnesses)}", + file=sys.stderr) + return 1 + # Get the hook script path (from the package) hook_script_src = pathlib.Path(__file__).parent / "hooks" / "post_turn.py" - # Determine Vibe home - vibe_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser() - hooks_dir = vibe_home / "hooks" + # Determine harness home directory + # For now, mistral uses ~/.vibe, but this can be extended for other harnesses + if harness_name == "mistral": + harness_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser() + hooks_dir = harness_home / "hooks" + else: + # Fallback for other harnesses + harness_home = pathlib.Path(os.environ.get("VIBE_HOME", "~/.vibe")).expanduser() + hooks_dir = harness_home / "hooks" try: # Create hooks directory @@ -197,7 +217,7 @@ command = "python ~/.vibe/hooks/post_turn.py" print(f"Installed hooks configuration: {hooks_toml}") # Update config.toml to add agent-cost-estimator to bash allowlist - config_toml = vibe_home / "config.toml" + config_toml = harness_home / "config.toml" if config_toml.exists(): config_text = config_toml.read_text(encoding="utf-8") @@ -239,8 +259,11 @@ allowlist = ["agent-cost-estimator"] else: print(f"Warning: {config_toml} does not exist, skipping allowlist update") - print("\nIntegration complete!") - print("To use: Start a new Vibe session and costs will be displayed after each turn.") + print(f"\nIntegration complete for {harness_name}!") + if harness_name == "mistral": + print("To use: Start a new Vibe session and costs will be displayed after each turn.") + else: + print(f"To use: Start a new {harness_name} session and costs will be displayed.") return 0 except Exception as exc: @@ -291,7 +314,11 @@ def main(argv=None): print(name) return 0 if args.integrate: - return integrate_vibe_hooks() + if not args.integrate: + print("error: --integrate requires a harness name argument (e.g., 'mistral')", + file=sys.stderr) + return 1 + return integrate_hooks(args.integrate) if args.dry_run and not args.update_pricing: print("error: --dry-run needs --update-pricing", file=sys.stderr) diff --git a/tests/test_agent_cost_estimator.py b/tests/test_agent_cost_estimator.py index 1fc1c34..bc3f93f 100755 --- a/tests/test_agent_cost_estimator.py +++ b/tests/test_agent_cost_estimator.py @@ -528,3 +528,19 @@ def test_cli_error_exit(tmp_path, capsys): code = cli.main(["--session-id", "nope", "--home", str(tmp_path)]) assert code == 1 assert "session not found" in capsys.readouterr().err + + +def test_cli_integrate_requires_harness(capsys): + """--integrate without a harness name exits 2 with argparse error.""" + with pytest.raises(SystemExit) as exc_info: + cli.main(["--integrate"]) + assert exc_info.value.code == 2 + out = capsys.readouterr() + assert "expected one argument" in out.err + + +def test_cli_integrate_unsupported_harness(capsys): + """--integrate with unsupported harness exits 1 with error.""" + code = cli.main(["--integrate", "unsupported-harness"]) + assert code == 1 + assert "unsupported harness" in capsys.readouterr().err