|
| 1 | +"""Boot the sales-proposal-mode mock seller. |
| 2 | +
|
| 3 | +Wires: |
| 4 | +
|
| 5 | +* :class:`ProposalModeProposalManager` declaring ``finalize=True``. |
| 6 | +* :class:`ProposalModeDecisioningPlatform` reading ``ctx.recipes``. |
| 7 | +* :class:`InMemoryProposalStore` for the proposal lifecycle. |
| 8 | +* :class:`PlatformRouter` over both with cross-store consistency check. |
| 9 | +
|
| 10 | +This is the storyboard adopter — the proof that the design works |
| 11 | +end-to-end. Run:: |
| 12 | +
|
| 13 | + python -m examples.sales_proposal_mode_seller.src.app |
| 14 | +
|
| 15 | +Then exercise via:: |
| 16 | +
|
| 17 | + adcp storyboard run http://127.0.0.1:3003/mcp media_buy_seller \\ |
| 18 | + --json --allow-http |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import os |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +from adcp.decisioning import ( |
| 27 | + DecisioningCapabilities, |
| 28 | + InMemoryProposalStore, |
| 29 | + PlatformRouter, |
| 30 | + serve, |
| 31 | +) |
| 32 | +from adcp.decisioning.accounts import AccountStore |
| 33 | +from adcp.decisioning.capabilities import Account as CapabilitiesAccount |
| 34 | +from adcp.decisioning.capabilities import ( |
| 35 | + Adcp, |
| 36 | + IdempotencyUnsupported, |
| 37 | + MediaBuy, |
| 38 | + SupportedProtocol, |
| 39 | +) |
| 40 | +from adcp.decisioning.context import AuthInfo |
| 41 | +from adcp.decisioning.types import Account |
| 42 | +from examples.sales_proposal_mode_seller.src.platform import ( |
| 43 | + ProposalModeDecisioningPlatform, |
| 44 | +) |
| 45 | +from examples.sales_proposal_mode_seller.src.proposal_manager import ( |
| 46 | + ProposalModeProposalManager, |
| 47 | +) |
| 48 | + |
| 49 | +PORT = int(os.environ.get("ADCP_PORT") or os.environ.get("PORT") or 3003) |
| 50 | + |
| 51 | + |
| 52 | +class _SingleTenantAccounts: |
| 53 | + """Minimal :class:`AccountStore` + :class:`AccountStoreUpsert` — every |
| 54 | + request resolves to the single ``default`` tenant. ``sync_accounts`` |
| 55 | + is implemented (the storyboard runner needs it for stateful chain |
| 56 | + bootstrapping).""" |
| 57 | + |
| 58 | + resolution = "explicit" |
| 59 | + |
| 60 | + def resolve( |
| 61 | + self, |
| 62 | + ref: dict[str, Any] | None = None, |
| 63 | + auth_info: AuthInfo | None = None, |
| 64 | + ) -> Account[dict[str, Any]]: |
| 65 | + del auth_info |
| 66 | + ref = ref or {} |
| 67 | + operator = (ref or {}).get("operator") if isinstance(ref, dict) else None |
| 68 | + account_id = (ref or {}).get("account_id") if isinstance(ref, dict) else None |
| 69 | + resolved_id = str(account_id or f"acct_{operator or 'demo'}".replace(".", "_")) |
| 70 | + return Account( |
| 71 | + id=resolved_id, |
| 72 | + metadata={"tenant_id": "default"}, |
| 73 | + ) |
| 74 | + |
| 75 | + def upsert( |
| 76 | + self, |
| 77 | + refs: list[Any], |
| 78 | + ctx: Any = None, |
| 79 | + ) -> list[dict[str, Any]]: |
| 80 | + """``sync_accounts`` API. Storyboards call this first to seed |
| 81 | + the stateful account chain. Returns one result row per ref.""" |
| 82 | + del ctx |
| 83 | + rows: list[dict[str, Any]] = [] |
| 84 | + for ref in refs: |
| 85 | + if hasattr(ref, "model_dump"): |
| 86 | + ref_dict = ref.model_dump(mode="json", exclude_none=True) |
| 87 | + else: |
| 88 | + ref_dict = dict(ref) if isinstance(ref, dict) else {} |
| 89 | + operator = ref_dict.get("operator", "demo") |
| 90 | + brand = ref_dict.get("brand") or {} |
| 91 | + domain = ( |
| 92 | + brand.get("domain") if isinstance(brand, dict) else getattr(brand, "domain", None) |
| 93 | + ) |
| 94 | + account_id = f"acct_{operator}".replace(".", "_") |
| 95 | + rows.append( |
| 96 | + { |
| 97 | + "ref": ref_dict, |
| 98 | + "account": { |
| 99 | + "account_id": account_id, |
| 100 | + "name": f"Account for {domain or operator}", |
| 101 | + "status": "active", |
| 102 | + "brand": {"domain": domain or "demo.example"}, |
| 103 | + "operator": operator, |
| 104 | + "billing": "operator", |
| 105 | + }, |
| 106 | + "operation": "created", |
| 107 | + } |
| 108 | + ) |
| 109 | + return rows |
| 110 | + |
| 111 | + |
| 112 | +def build_router() -> PlatformRouter: |
| 113 | + """Construct the v1.5 router with finalize-capable wiring.""" |
| 114 | + accounts: AccountStore[Any] = _SingleTenantAccounts() # type: ignore[assignment] |
| 115 | + return PlatformRouter( |
| 116 | + accounts=accounts, |
| 117 | + platforms={"default": ProposalModeDecisioningPlatform()}, |
| 118 | + proposal_managers={"default": ProposalModeProposalManager()}, |
| 119 | + proposal_stores={"default": InMemoryProposalStore()}, |
| 120 | + capabilities=DecisioningCapabilities( |
| 121 | + specialisms=["sales-non-guaranteed", "sales-proposal-mode"], |
| 122 | + adcp=Adcp( |
| 123 | + major_versions=[3], |
| 124 | + idempotency=IdempotencyUnsupported(supported=False), |
| 125 | + ), |
| 126 | + account=CapabilitiesAccount(supported_billing=["operator"]), |
| 127 | + media_buy=MediaBuy(supported_pricing_models=["cpm"]), |
| 128 | + supported_protocols=[SupportedProtocol.media_buy], |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + serve( |
| 135 | + build_router(), |
| 136 | + name="sales-proposal-mode-seller", |
| 137 | + port=PORT, |
| 138 | + auto_emit_completion_webhooks=False, |
| 139 | + ) |
0 commit comments