diff --git a/README.md b/README.md index 0a72900..fa26c35 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ cd first-commit-ai pip install -e ".[dev]" pytest python -m first_commit_ai --mock "hi" +python -m first_commit_ai --mock --json "hi" ``` You should see a `[mock]` reply. That means the wiring works before you spend a token. diff --git a/src/first_commit_ai/cli.py b/src/first_commit_ai/cli.py index 2c6eb15..1921371 100644 --- a/src/first_commit_ai/cli.py +++ b/src/first_commit_ai/cli.py @@ -3,6 +3,7 @@ from __future__ import annotations import argparse +import json import sys from first_commit_ai.client import DEFAULT_SYSTEM, ChatClient @@ -35,6 +36,11 @@ def build_parser() -> argparse.ArgumentParser: metavar="TEXT", help=f"Override the system prompt (default: {DEFAULT_SYSTEM!r})", ) + p.add_argument( + "--json", + action="store_true", + help="Print the reply as a JSON object (reply, mock, system)", + ) return p @@ -44,7 +50,11 @@ def main(argv: list[str] | None = None) -> int: if args.system is not None: client.system = args.system try: - print(client.chat(args.prompt)) + reply = client.chat(args.prompt) + if args.json: + print(json.dumps({"reply": reply, "mock": bool(args.mock), "system": client.system})) + else: + print(reply) except Exception as exc: # noqa: BLE001 — teach failures clearly print(f"error: {exc}", file=sys.stderr) return 1 diff --git a/tests/test_cli.py b/tests/test_cli.py index e8be9b8..7f633e9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,20 @@ +import json + from first_commit_ai.cli import build_parser, main from first_commit_ai.client import DEFAULT_SYSTEM, ChatClient +def test_json_output_structure(capsys): + code = main(["--mock", "--json", "hi"]) + captured = capsys.readouterr() + assert code == 0 + data = json.loads(captured.out) + assert data["reply"].startswith("[mock]") + assert "hi" in data["reply"] + assert data["mock"] is True + assert data["system"] == DEFAULT_SYSTEM + + def test_mock_chat_is_deterministic(): client = ChatClient(mock=True) out = client.chat("hi")