From 098409d43bd6fb4ad3f2dd5368c5279eb3186cbc Mon Sep 17 00:00:00 2001 From: tryambak2019 Date: Tue, 15 Sep 2026 17:31:58 -0400 Subject: [PATCH] fix(python): make currency CLI entry point importable Signed-off-by: tryambak2019 --- python/Makefile | 2 +- python/samples/langgraph/currency/Dockerfile | 4 +-- .../langgraph/currency/currency/cli.py | 19 +++++++---- .../langgraph/currency/tests/test_cli.py | 32 +++++++++++++++++++ 4 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 python/samples/langgraph/currency/tests/test_cli.py diff --git a/python/Makefile b/python/Makefile index fc49fb4d3..405cb7610 100644 --- a/python/Makefile +++ b/python/Makefile @@ -19,7 +19,7 @@ lint: .PHONY: test test: update generate-test-certs - uv run pytest ./packages/**/tests/ + uv run pytest ./packages/**/tests/ ./samples/langgraph/currency/tests/ .PHONY: build build: update format diff --git a/python/samples/langgraph/currency/Dockerfile b/python/samples/langgraph/currency/Dockerfile index 27b98160e..65e923ee9 100644 --- a/python/samples/langgraph/currency/Dockerfile +++ b/python/samples/langgraph/currency/Dockerfile @@ -44,5 +44,5 @@ EXPOSE 8080 HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD curl -f http://localhost:8080/health || exit 1 -# Run the application -CMD ["uv", "run", "samples/langgraph/currency/currency/cli.py"] +# Run the installed console entry point so package-relative imports resolve. +CMD ["uv", "run", "--package", "currency", "currency"] diff --git a/python/samples/langgraph/currency/currency/cli.py b/python/samples/langgraph/currency/currency/cli.py index 973d875a4..1de8bea68 100644 --- a/python/samples/langgraph/currency/currency/cli.py +++ b/python/samples/langgraph/currency/currency/cli.py @@ -5,36 +5,41 @@ import os import uvicorn -from agent import graph from kagent.core import KAgentConfig from kagent.langgraph import KAgentApp +from .agent import graph + # Configure logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) -def main(): - """Main entry point for the CLI.""" - # from script directory +def build_app(): + """Build the currency agent ASGI application.""" with open(os.path.join(os.path.dirname(__file__), "agent-card.json"), "r") as f: agent_card = json.load(f) config = KAgentConfig() - app = KAgentApp( + return KAgentApp( graph=graph, agent_card=agent_card, config=config, tracing=True, - ) + ).build() + + +def main(): + """Main entry point for the CLI.""" + app = build_app() port = int(os.getenv("PORT", "8080")) host = os.getenv("HOST", "0.0.0.0") logger.info(f"Starting server on {host}:{port}") uvicorn.run( - app.build(), + app, host=host, port=port, log_level="info", diff --git a/python/samples/langgraph/currency/tests/test_cli.py b/python/samples/langgraph/currency/tests/test_cli.py new file mode 100644 index 000000000..0b0827036 --- /dev/null +++ b/python/samples/langgraph/currency/tests/test_cli.py @@ -0,0 +1,32 @@ +"""Regression tests for the currency console entry point.""" + +import importlib + +from fastapi.testclient import TestClient + + +def test_main_passes_healthy_app_to_uvicorn(monkeypatch, tmp_path): + monkeypatch.setenv("OPENAI_API_KEY", "fake") + monkeypatch.setenv("KAGENT_API_URL", "http://localhost:8083") + monkeypatch.setenv("KAGENT_GATEWAY_URL", "http://localhost:8083") + monkeypatch.setenv("KAGENT_NAME", "currency") + monkeypatch.setenv("KAGENT_NAMESPACE", "default") + checkpoint_db = tmp_path / "currency-checkpoints.sqlite" + monkeypatch.setenv("KAGENT_CHECKPOINT_DB", str(checkpoint_db)) + + cli = importlib.import_module("currency.cli") + captured = {} + + def run(app, **kwargs): + captured["app"] = app + captured["kwargs"] = kwargs + + monkeypatch.setattr(cli.uvicorn, "run", run) + cli.main() + + response = TestClient(captured["app"]).get("/health") + + assert response.status_code == 200 + assert response.text == "OK" + assert checkpoint_db.exists() + assert captured["kwargs"] == {"host": "0.0.0.0", "port": 8080, "log_level": "info"}