|
| 1 | +"""``actp serve`` — run the AIP-2.1 quote-channel daemon. |
| 2 | +
|
| 3 | +Loads a ProviderPolicy JSON file, builds a FastAPI app via |
| 4 | +:func:`agirails.server.app.create_app`, and serves it with uvicorn. |
| 5 | +
|
| 6 | +Scope: |
| 7 | + - accept + verify incoming counter-offers via :class:`QuoteChannelHandler` |
| 8 | + - log the policy verdict (ACCEPT / COUNTER / REJECT) per round |
| 9 | + - one-line health response on ``GET /`` |
| 10 | +
|
| 11 | +Not in scope here (matches TS daemon v1): |
| 12 | + - On-chain INITIATED-tx detection (handled by ``actp agent`` / |
| 13 | + long-running `Agent` instances). |
| 14 | + - Sending CounterAcceptMessage back to buyer (no reverse-endpoint |
| 15 | + discovery in v1 — operator handles delivery). |
| 16 | +
|
| 17 | +Usage:: |
| 18 | +
|
| 19 | + actp serve --policy ./provider-policy.json --port 8787 --network base-sepolia |
| 20 | + actp serve --policy ./provider-policy.json --mock # local testing |
| 21 | +
|
| 22 | +Example policy JSON (saved as ``provider-policy.json``):: |
| 23 | +
|
| 24 | + { |
| 25 | + "services": ["text-generation"], |
| 26 | + "pricing": { |
| 27 | + "min_acceptable": {"amount": 500000, "currency": "USDC", "unit": "base"}, |
| 28 | + "ideal_price": {"amount": 1000000, "currency": "USDC", "unit": "base"} |
| 29 | + }, |
| 30 | + "quote_ttl": "15m", |
| 31 | + "counter_strategy": "concede", |
| 32 | + "concede_pct": 30, |
| 33 | + "max_requotes": 2 |
| 34 | + } |
| 35 | +""" |
| 36 | + |
| 37 | +from pathlib import Path |
| 38 | +from typing import Optional |
| 39 | + |
| 40 | +import typer |
| 41 | + |
| 42 | +from agirails.cli.utils.output import print_error, print_info, print_success |
| 43 | + |
| 44 | + |
| 45 | +def serve( |
| 46 | + policy: Path = typer.Option( |
| 47 | + ..., |
| 48 | + "--policy", |
| 49 | + help="Path to ProviderPolicy JSON file.", |
| 50 | + exists=True, |
| 51 | + dir_okay=False, |
| 52 | + readable=True, |
| 53 | + ), |
| 54 | + port: int = typer.Option( |
| 55 | + 8787, "--port", min=1, max=65535, help="HTTP port to listen on." |
| 56 | + ), |
| 57 | + host: str = typer.Option( |
| 58 | + "0.0.0.0", "--host", help="Bind address (default: 0.0.0.0)." |
| 59 | + ), |
| 60 | + network: str = typer.Option( |
| 61 | + "base-sepolia", |
| 62 | + "--network", |
| 63 | + help="Network — base-sepolia | base-mainnet | mock.", |
| 64 | + ), |
| 65 | + mock: bool = typer.Option( |
| 66 | + False, |
| 67 | + "--mock", |
| 68 | + help="Use a mock provider address / zero kernel for local testing.", |
| 69 | + ), |
| 70 | + provider_address: Optional[str] = typer.Option( |
| 71 | + None, |
| 72 | + "--provider-address", |
| 73 | + help=( |
| 74 | + "Provider EOA / Smart Wallet address shown on /health. " |
| 75 | + "Defaults to env ACTP_PROVIDER_ADDRESS or a placeholder." |
| 76 | + ), |
| 77 | + ), |
| 78 | +) -> None: |
| 79 | + """Run a long-running provider daemon (AIP-2.1 quote channel).""" |
| 80 | + try: |
| 81 | + # Lazy imports — server stack is an optional dependency. |
| 82 | + try: |
| 83 | + import uvicorn # noqa: F401 |
| 84 | + except ImportError as exc: |
| 85 | + raise RuntimeError( |
| 86 | + "uvicorn is not installed. Install the server extras:\n" |
| 87 | + " pip install agirails[server]" |
| 88 | + ) from exc |
| 89 | + |
| 90 | + from agirails.config.networks import get_network |
| 91 | + from agirails.server.app import create_app |
| 92 | + from agirails.server.policy import load_policy_from_file |
| 93 | + from agirails.server.quote_channel import build_channel_path |
| 94 | + |
| 95 | + # 1. Load + validate policy. |
| 96 | + loaded_policy = load_policy_from_file(policy) |
| 97 | + |
| 98 | + # 2. Resolve kernel address + chainId from network config. |
| 99 | + if mock: |
| 100 | + kernel_address = "0x" + "0" * 40 |
| 101 | + chain_id = 84532 |
| 102 | + else: |
| 103 | + network_cfg = get_network(network) |
| 104 | + kernel_address = network_cfg.contracts.actp_kernel |
| 105 | + chain_id = network_cfg.chain_id |
| 106 | + |
| 107 | + # 3. Provider address for /health. |
| 108 | + import os |
| 109 | + signer_address = ( |
| 110 | + provider_address |
| 111 | + or os.environ.get("ACTP_PROVIDER_ADDRESS") |
| 112 | + or "0x" + "0" * 40 |
| 113 | + ) |
| 114 | + |
| 115 | + # 4. Build app. |
| 116 | + app = create_app( |
| 117 | + policy=loaded_policy, |
| 118 | + kernel_address_by_chain_id={chain_id: kernel_address}, |
| 119 | + signer_address=signer_address, |
| 120 | + service_label="actp-serve", |
| 121 | + ) |
| 122 | + |
| 123 | + # 5. Banner + serve. |
| 124 | + print_success(f"actp serve listening on http://{host}:{port}") |
| 125 | + print_info(f" Network: {network}{' (mock)' if mock else ''}") |
| 126 | + print_info(f" Provider: {signer_address}") |
| 127 | + print_info(f" Channel base: {build_channel_path(chain_id, '<txId>')}") |
| 128 | + print_info(f" Health: GET /") |
| 129 | + print_info("") |
| 130 | + print_info( |
| 131 | + "Counter-offers POSTed to /quote-channel/{chainId}/{txId} are verified +" |
| 132 | + ) |
| 133 | + print_info( |
| 134 | + "evaluated against the policy. Verdicts are logged here; v1 does NOT" |
| 135 | + ) |
| 136 | + print_info( |
| 137 | + "auto-deliver CounterAccept back to the buyer (AIP-2.1 §5.3)." |
| 138 | + ) |
| 139 | + |
| 140 | + import uvicorn as _uvicorn |
| 141 | + |
| 142 | + _uvicorn.run(app, host=host, port=port, log_level="info") |
| 143 | + except Exception as exc: |
| 144 | + print_error(f"actp serve failed: {exc}") |
| 145 | + raise typer.Exit(code=1) |
0 commit comments