diff --git a/python/Makefile b/python/Makefile index fc49fb4d33..405cb7610c 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 27b98160ec..65e923ee9e 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 973d875a48..1de8bea687 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 0000000000..0b08270363 --- /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"}