Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download api/app_templates.py from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 15.9 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/api/app_templates.py
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/api/app_templates.py
-
curl -L -o app_templates.py https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/api/app_templates.py
15.9 kB
| """C1: the shipped App template catalogue and each template's source or permission contract. | |
| The registry's template contracts and `core.external_tools`' templateKind enum derive from or | |
| mirror `TEMPLATES`; `verify_app_templates.py` holds all three name for name. | |
| """ | |
| from __future__ import annotations | |
| import sys | |
| from importlib import import_module | |
| from types import MappingProxyType | |
| PROCESSES = ("Create value", "Acquire", "Convert", "Deliver", "Retain", "Finance and cash", | |
| "Risk and compliance", "None") | |
| # A C1 connector key is not a keychain entry type: Amazon SP-API is stored as `amazon_sp` (F25). | |
| CONNECTOR_ENTRY_TYPES = MappingProxyType({"odoo": "odoo", "amazon": "amazon_sp", "amazon_ads": "amazon_ads"}) | |
| def source_connector(source): | |
| """Resolve a declared source without importing its product module.""" | |
| if source.startswith("ut_amzn_ads_"): | |
| return "amazon_ads" | |
| if source.startswith("ut_amzn_"): | |
| return "amazon" | |
| if source in ("customer_data", "product_data") or source.startswith("ut_odoo_"): | |
| return "odoo" | |
| raise ValueError("The template source has no declared connector.") | |
| def source_connectors(connector, sources, optional_sources=(), optional_connectors=()): | |
| """Order known connectors by primary, declared sources, then optional read features.""" | |
| if (not isinstance(optional_connectors, (tuple, list)) | |
| or any(not isinstance(key, str) or key not in CONNECTOR_ENTRY_TYPES | |
| for key in (connector, *optional_connectors))): | |
| raise ValueError("The template has an unknown connector.") | |
| return tuple(dict.fromkeys((connector, *(source_connector(source) | |
| for source in (*sources, *optional_sources)), | |
| *optional_connectors))) | |
| def validate_connectors(row): | |
| """Reject unordered, unknown, duplicated or incomplete connector declarations for any row.""" | |
| if row.get("kind") == "alerts": | |
| return (row.get("connector") is None and row.get("permission") == "automation" | |
| and all(row.get(key) == () for key in | |
| ("connectors", "sources", "optional_sources", "optional_connectors"))) | |
| connectors = row.get("connectors") | |
| if (not isinstance(connectors, (tuple, list)) or not connectors | |
| or connectors[0] != row.get("connector") | |
| or any(not isinstance(key, str) or key not in CONNECTOR_ENTRY_TYPES for key in connectors) | |
| or len(set(connectors)) != len(connectors)): | |
| return False | |
| try: | |
| required = source_connectors(row["connector"], row["sources"], row.get("optional_sources", ()), | |
| row.get("optional_connectors", ())) | |
| except (KeyError, TypeError, ValueError, AttributeError): | |
| return False | |
| return tuple(connectors) == required | |
| def _amazon(kind, label, purpose, process, sources, *, connector="amazon", proposes=True, tools=(), | |
| optional_sources=(), optional_connectors=()): | |
| return {"kind": kind, "label": label, "purpose": purpose, "connector": connector, | |
| "connectors": source_connectors(connector, sources, optional_sources, optional_connectors), | |
| "tags": ["Amazon Ads" if connector == "amazon_ads" else "Amazon"], | |
| "sources": tuple(sources), "optional_sources": tuple(optional_sources), | |
| "optional_connectors": tuple(optional_connectors), "process": process, "mark": kind, | |
| "module": "modules." + kind, "router": "routes_" + kind, | |
| "view": kind.replace("_", "-"), "tools": tuple(tools), "proposes": proposes} | |
| TEMPLATES = ( | |
| # Routing and Catalog render through Map mode; their routes are mounted by main.py. | |
| # They read FIELD_BINDINGS over their source, so an App of either kind stores no field map. | |
| {"kind": "route", "label": "Routing", "purpose": "Plan delivery routes for your customers.", | |
| "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": ("customer_data",), | |
| "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "route", | |
| "module": None, "router": None, "view": None, "tools": (), "proposes": False}, | |
| {"kind": "catalog", "label": "Catalog", "purpose": "Build and print product catalogs.", | |
| "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": ("product_data",), | |
| "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "catalog", | |
| "module": None, "router": None, "view": None, "tools": ("catalog_build", "catalog_pdf"), | |
| "proposes": False}, | |
| _amazon("competitor_watch", "Competitor watch", "See who ranks on your search terms each week.", | |
| "Acquire", ("ut_amzn_brand_analytics", "ut_amzn_listings"), proposes=False), | |
| _amazon("ranking_diagnostic", "Ranking diagnostic", "Find why a listing lost its rank.", | |
| "Acquire", ("ut_amzn_pricing", "ut_amzn_catalog_items", "ut_amzn_brand_analytics", | |
| "ut_amzn_economics", "ut_amzn_fba_inventory", "ut_amzn_suppressed_listings", | |
| "ut_amzn_stranded_inventory", "ut_amzn_inventory_planning"), proposes=False, | |
| optional_sources=("ut_amzn_search_query_performance", "ut_amzn_listing_items")), | |
| _amazon("amazon_operator", "Operator inbox", "Approve ad changes in one batch.", | |
| "Acquire", ("ut_amzn_ads_sp_campaigns", "ut_amzn_ads_sb_campaigns", "ut_amzn_ads_sd_campaigns", | |
| "ut_amzn_economics"), connector="amazon_ads", proposes=False), | |
| _amazon("restock_planner", "Restock planner", "Send stock to Amazon before it runs out.", | |
| "Deliver", ("ut_amzn_restock_recommendations", "ut_amzn_inventory_planning", | |
| "ut_amzn_fba_inventory"), optional_connectors=("odoo",)), | |
| _amazon("reimbursements", "Reimbursements", "Find FBA money Amazon owes you.", | |
| "Create value", ("ut_amzn_reimbursements", "ut_amzn_inventory_ledger_detail", | |
| "ut_amzn_fba_customer_returns", "ut_amzn_settlements"), optional_connectors=("odoo",)), | |
| _amazon("aged_stock", "Aged stock", "Move old stock before storage fees grow.", | |
| "Create value", ("ut_amzn_inventory_planning", "ut_amzn_long_term_storage_fees", | |
| "ut_amzn_storage_fees", "ut_amzn_recommended_removals", | |
| "ut_amzn_stranded_inventory")), | |
| _amazon("opportunity_finder", "Opportunity finder", "Find rising searches you do not serve yet.", | |
| "Create value", ("ut_amzn_brand_analytics", "ut_amzn_search_query_performance", "ut_amzn_listings"), | |
| optional_connectors=("odoo",)), | |
| _amazon("returns_refunds", "Returns and refunds", "See why buyers return and track each fix.", | |
| "Retain", ("ut_amzn_fba_customer_returns", "ut_amzn_fba_shipped_sales", "ut_amzn_mfn_returns", | |
| "ut_amzn_settlements", "ut_amzn_transactions", "ut_amzn_order_items"), | |
| optional_sources=("ut_amzn_fba_inventory", "ut_amzn_removal_orders", | |
| "ut_amzn_removal_shipments", "ut_amzn_storage_fees"), | |
| optional_connectors=("odoo",)), | |
| _amazon("repeat_buyers", "Repeat buyers", "See who buys again and what they buy together.", | |
| "Retain", ("ut_amzn_repeat_purchase", "ut_amzn_market_basket"), proposes=False, | |
| optional_sources=("ut_amzn_replenishment", "ut_amzn_subscribe_save_performance", | |
| "ut_amzn_fba_shipped_sales", "ut_amzn_listings")), | |
| _amazon("payouts", "Payouts", "Tie each payout to its orders and fees.", | |
| "Finance and cash", ("ut_amzn_settlements", "ut_amzn_economics", "ut_amzn_transactions"), | |
| optional_connectors=("odoo",)), | |
| _amazon("account_health", "Account health", "Catch account risks before Amazon acts.", | |
| "Risk and compliance", ("ut_amzn_account_health",)), | |
| _amazon("amazon_dashboard", "Amazon dashboard", "See sales, advertising and returns for your account.", | |
| "None", ("ut_amzn_order_metrics", "ut_amzn_order_items", "ut_amzn_fba_customer_returns", | |
| "ut_amzn_fba_shipped_sales", "ut_amzn_mfn_returns", "ut_amzn_settlements", | |
| "ut_amzn_transactions", "ut_amzn_ads_sp_campaigns", "ut_amzn_ads_sb_campaigns", | |
| "ut_amzn_ads_sd_campaigns"), optional_sources=("product_data",), proposes=False), | |
| # Project management reads its own module tables (ut_amazon_*), so it declares no | |
| # source: a cold mirror must not refuse an App whose rows live in the store. | |
| _amazon("amazon_profit", "Project management", "See profit, ads and stock for every SKU.", | |
| "Create value", (), | |
| optional_sources=("ut_amzn_search_query_performance", "ut_amzn_order_items", | |
| "ut_amzn_brand_analytics", "ut_amzn_listings", "ut_amzn_catalog_items", | |
| "ut_amzn_listing_items", "ut_amzn_competitor_snapshots", | |
| "ut_amzn_competitor_images"), | |
| optional_connectors=("odoo",), proposes=False), | |
| {"kind": "alerts", "label": "Alerts", "purpose": "Act on changes in your databases.", | |
| "connector": None, "connectors": (), "permission": "automation", "tags": ["Automation"], | |
| "sources": (), "optional_sources": (), "optional_connectors": (), "process": "None", | |
| "mark": "alerts", "module": None, "router": "routes_automation", "view": "alerts", | |
| "tools": (), "proposes": False}, | |
| {"kind": "reports", "label": "Reports", "purpose": "Download reports shared with you.", | |
| "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": (), | |
| "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "reports", | |
| "module": "modules.reports", "router": "routes_reports", "view": "reports", | |
| "tools": (), "proposes": False}, | |
| ) | |
| if not all(validate_connectors(row) for row in TEMPLATES): | |
| raise ValueError("An App template omits a source connector.") | |
| BY_KIND = MappingProxyType({row["kind"]: row for row in TEMPLATES}) | |
| KINDS = tuple(BY_KIND) | |
| # SDLC 80 ships the rebuilt Apps through their existing connector, grant and source walls. | |
| HELD_KINDS = frozenset() | |
| def held(kind_or_app): | |
| """True when a template kind, or an App record's `templateKind`, has a release hold.""" | |
| kind = kind_or_app.get("templateKind") if isinstance(kind_or_app, dict) else kind_or_app | |
| return isinstance(kind, str) and kind in HELD_KINDS | |
| #: R33: the fixed role to field binding each kind reads from its first C1 source. | |
| #: Catalog price is Royal 1 tier 1: the pooled `price_1` is not in the typed product_data schema. | |
| FIELD_BINDINGS = MappingProxyType({ | |
| "route": MappingProxyType({"title": "customer", "address": "street", | |
| "latitude": "lat", "longitude": "lon"}), | |
| "catalog": MappingProxyType({"key": "code", "title": "product", "image": "image", | |
| "sku": "code", "price": "price_royal1_t1", "category": "category"}), | |
| "competitor_watch": MappingProxyType({"term": "search_term"}), | |
| }) | |
| #: Kinds whose App record stores no source or field map: the registry projects C1 on read. | |
| BOUND_KINDS = ("route", "catalog") | |
| _BINDING_KEYS = ("sourceDatabase", "fieldMap") | |
| def source(kind): | |
| """The C1 source table a kind reads, or None.""" | |
| row = BY_KIND.get(kind) | |
| return row["sources"][0] if row and row["sources"] else None | |
| def field_binding(kind): | |
| """A copy of the kind's fixed role to field map; empty when it names none.""" | |
| return dict(FIELD_BINDINGS.get(kind) or {}) | |
| def bound(app): | |
| """Read projection: a copy of a bound kind's App with the C1 source and field map filled in. | |
| A stored App that still carries its own map keeps it until the S79-T04 migration. | |
| """ | |
| if not isinstance(app, dict) or app.get("templateKind") not in BOUND_KINDS: | |
| return app | |
| row = dict(app) | |
| projection = {"sourceDatabase": source(row["templateKind"]), | |
| "fieldMap": field_binding(row["templateKind"])} | |
| if row.get("fieldMap"): | |
| projection = {"sourceDatabase": row.get("sourceDatabase") or projection["sourceDatabase"]} | |
| row.update(projection) | |
| return row | |
| def own_binding(app): | |
| """The binding keys a stored App carries that differ from what `bound` projects.""" | |
| if not isinstance(app, dict) or app.get("templateKind") not in BOUND_KINDS: | |
| return {key: app[key] for key in _BINDING_KEYS if isinstance(app, dict) and key in app} | |
| kind = app["templateKind"] | |
| fixed = field_binding(kind) | |
| held = app.get("fieldMap") or {} | |
| if (app.get("sourceDatabase") in (None, source(kind)) | |
| and all(fixed.get(role) == field for role, field in held.items())): | |
| return {} | |
| return {key: app[key] for key in _BINDING_KEYS if key in app} | |
| #: Routers `app_routes.mount_app_routers` failed to mount when main.py's record is absent. | |
| ROUTER_MOUNT_FAILURES = {} | |
| #: Routers `app_routes.mount_app_routers` mounted or found already included. | |
| MOUNTED_ROUTERS = set() | |
| # A failed seam import is printed once per process and withholds its kind. | |
| _SEAM_FAILURES = {} | |
| def entry_type(connector): | |
| """The keychain entry type that proves a C1 connector key is connected.""" | |
| return CONNECTOR_ENTRY_TYPES.get(connector, connector) | |
| def connectors_present(session): | |
| """C1 connector keys this reader has. Any read failure counts as nothing connected.""" | |
| try: | |
| from routes_connectors import _entry_types | |
| types, _scopes, failure = _entry_types(session.runtime, session.uname) | |
| have = set() if failure else set(types or ()) | |
| except Exception as exc: # noqa: BLE001 | |
| from core.request_timing import raise_if_resource_error | |
| raise_if_resource_error(exc) | |
| have = set() | |
| if "odoo" not in have: | |
| try: | |
| from routes_keychain import env_odoo_available | |
| if env_odoo_available(session.runtime): | |
| have.add("odoo") | |
| except Exception: # noqa: BLE001 | |
| pass | |
| return {key for key in CONNECTOR_ENTRY_TYPES if entry_type(key) in have} | |
| def connector_present(connector, session): | |
| return connector in connectors_present(session) | |
| def router_failures(): | |
| """Every recorded router mount failure: main.py's record joined with this module's own.""" | |
| merged = {name: list(rows) for name, rows in ROUTER_MOUNT_FAILURES.items()} | |
| main = sys.modules.get("main") | |
| record = getattr(main, "ROUTER_MOUNT_FAILURES", None) if main is not None else None | |
| if isinstance(record, dict) and record is not ROUTER_MOUNT_FAILURES: | |
| for name, rows in record.items(): | |
| merged.setdefault(name, []).extend(rows) | |
| return merged | |
| def _importable(name, attribute=None): | |
| if name in _SEAM_FAILURES: | |
| return False | |
| try: | |
| module = import_module(name) | |
| if attribute and not hasattr(module, attribute): | |
| raise ImportError(f"{name} has no {attribute}") | |
| return True | |
| except Exception as exc: # noqa: BLE001 | |
| _SEAM_FAILURES[name] = type(exc).__name__ | |
| print(f"[aios-api] APP TEMPLATE WITHHELD: {name}: {type(exc).__name__}") | |
| return False | |
| def seams_ready(kind): | |
| """True when the kind's module imports and its router imports and never failed to mount.""" | |
| row = BY_KIND.get(kind) | |
| if row is None: | |
| return False | |
| if row["module"] and not _importable(row["module"]): | |
| return False | |
| router = row["router"] | |
| return not router or (_importable(router, "router") and router not in router_failures()) | |
| def reset_seam_cache(): | |
| """Forget recorded seam import failures (test fixtures only).""" | |
| _SEAM_FAILURES.clear() | |