generated from coulomb/repo-seed
Extend State Hub context resolver for daily triage
This commit is contained in:
@@ -4,6 +4,10 @@ Registered as source type 'state-hub'.
|
||||
Supported queries:
|
||||
- domain_summary: GET {STATE_HUB_URL}/state/domain/{domain}
|
||||
- repo_sbom_status: GET {STATE_HUB_URL}/sbom/status?repo={repo_slug}
|
||||
- state_summary: GET {STATE_HUB_URL}/state/summary
|
||||
- next_steps: GET {STATE_HUB_URL}/state/next_steps
|
||||
- workplan_index: GET {STATE_HUB_URL}/workstreams/workplan-index
|
||||
- hub_inbox: GET {STATE_HUB_URL}/messages/?to_agent=hub&unread_only=true
|
||||
|
||||
No caching — state hub data is live operational state and must not be stale
|
||||
within a single workflow run.
|
||||
@@ -19,24 +23,47 @@ import httpx
|
||||
|
||||
from activity_core.context_resolvers.base import CONTEXT_RESOLVER_REGISTRY, ContextResolver
|
||||
|
||||
_STATE_HUB_URL = os.environ.get("STATE_HUB_URL", "http://127.0.0.1:8000")
|
||||
_DEFAULT_STATE_HUB_URL = "http://127.0.0.1:8000"
|
||||
_TIMEOUT_SECONDS = 10.0
|
||||
|
||||
|
||||
def _base_url() -> str:
|
||||
return os.environ.get("STATE_HUB_URL", _DEFAULT_STATE_HUB_URL).rstrip("/")
|
||||
|
||||
|
||||
def _fetch_json(path: str, params: dict[str, Any] | None = None) -> Any:
|
||||
url = f"{_base_url()}{path}"
|
||||
try:
|
||||
resp = httpx.get(url, params=params, timeout=_TIMEOUT_SECONDS)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
except (httpx.HTTPError, ValueError):
|
||||
return {}
|
||||
|
||||
|
||||
class StateHubContextResolver(ContextResolver):
|
||||
"""Fetches live data from the Custodian State Hub."""
|
||||
|
||||
def resolve(self, query: str, event: Any, params: dict[str, Any]) -> dict[str, Any]:
|
||||
base = _STATE_HUB_URL.rstrip("/")
|
||||
def resolve(self, query: str, event: Any, params: dict[str, Any]) -> Any:
|
||||
if query == "domain_summary":
|
||||
domain = params.get("domain", "")
|
||||
resp = httpx.get(f"{base}/state/domain/{domain}", timeout=10.0)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
return _fetch_json(f"/state/domain/{domain}")
|
||||
if query == "repo_sbom_status":
|
||||
repo_slug = params.get("repo_slug", "")
|
||||
resp = httpx.get(f"{base}/sbom/status", params={"repo": repo_slug}, timeout=10.0)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
return _fetch_json("/sbom/status", {"repo": repo_slug})
|
||||
if query == "state_summary":
|
||||
return _fetch_json("/state/summary")
|
||||
if query == "next_steps":
|
||||
return _fetch_json("/state/next_steps")
|
||||
if query == "workplan_index":
|
||||
query_params = dict(params)
|
||||
return _fetch_json("/workstreams/workplan-index", query_params)
|
||||
if query == "hub_inbox":
|
||||
query_params = {
|
||||
"to_agent": params.get("to_agent", "hub"),
|
||||
"unread_only": params.get("unread_only", True),
|
||||
}
|
||||
return _fetch_json("/messages/", query_params)
|
||||
return {}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user