Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion src/first_commit_ai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import argparse
import json
import sys

from first_commit_ai.client import DEFAULT_SYSTEM, ChatClient
Expand Down Expand Up @@ -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


Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down