-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve-and-execute.py
More file actions
144 lines (120 loc) · 5.52 KB
/
resolve-and-execute.py
File metadata and controls
144 lines (120 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Rhumb Example: Resolve a capability to the best provider and execute it.
This script shows the core Rhumb Resolve flow:
1. Resolve a capability to ranked providers
2. Inspect any machine-readable recovery handoff Rhumb already identified
3. Estimate cost before committing
4. Execute through Resolve with managed credentials
No auth is needed for the initial resolve walkthrough.
RHUMB_API_KEY (your governed API key) is required for estimation and execution.
Get a governed API key at https://rhumb.dev/auth/login
"""
import os
import httpx
import json
from resolve_helpers import (
describe_recovery_hint,
preferred_execute_provider,
preferred_recovery_handoff,
recovery_credential_modes_url,
recovery_resolve_url,
)
BASE = "https://api.rhumb.dev/v1"
API_KEY = os.environ.get("RHUMB_API_KEY")
def _print_recovery_handoff(recovery_handoff: tuple[str, dict[str, object]] | None) -> None:
if not recovery_handoff:
return
handoff_kind, handoff = recovery_handoff
provider = handoff.get("preferred_provider", "?")
mode = handoff.get("preferred_credential_mode", "?")
if handoff_kind == "alternate_execute":
print(f"Next step: pivot to the alternate execute rail via {provider} ({mode}).")
if handoff.get("endpoint_pattern"):
print(f" Endpoint: {handoff['endpoint_pattern']}")
else:
print(f"Next step: finish setup for {provider} ({mode}).")
if handoff.get("setup_url"):
print(f" Setup URL: {handoff['setup_url']}")
elif handoff.get("setup_hint"):
print(f" Setup hint: {handoff['setup_hint']}")
def main():
if not API_KEY:
print("ℹ️ No RHUMB_API_KEY (governed API key) set, so this run will stop after resolve.")
print(" Resolve itself works without auth.")
print(" Set RHUMB_API_KEY (your governed API key) only if you want to continue into estimate and execute.")
print(" Get a governed API key at https://rhumb.dev/auth/login\n")
headers = {"X-Rhumb-Key": API_KEY} if API_KEY else {}
# Step 1: Resolve capability to ranked providers
capability = "search.query"
print(f"🔍 Resolving '{capability}' to best providers...\n")
resp = httpx.get(f"{BASE}/capabilities/{capability}/resolve", headers=headers)
data = resp.json().get("data", {})
providers = data.get("providers", [])
execute_hint = data.get("execute_hint") or {}
for p in providers[:5]:
slug = p.get("service_slug", "?")
score = p.get("an_score", "?")
cost = p.get("cost_per_call", "?")
print(f" {slug:20s} AN Score: {score} Est. cost: ${cost}")
if execute_hint:
print("\n🧭 Execute handoff")
print(f" Preferred provider: {execute_hint.get('preferred_provider', '?')}")
print(f" Preferred mode: {execute_hint.get('preferred_credential_mode', '?')}")
if execute_hint.get("fallback_providers"):
print(f" Fallbacks: {', '.join(execute_hint['fallback_providers'])}")
recovery_summary = describe_recovery_hint(data)
recovery_handoff = preferred_recovery_handoff(data)
credential_modes_url = recovery_credential_modes_url(data)
resolve_url = recovery_resolve_url(data)
top_provider = preferred_execute_provider(data)
if recovery_summary:
print(f"\n⚠️ Recovery hint: {recovery_summary}")
if not API_KEY:
if not top_provider:
print("\nNo execute-ready provider found in the current resolve context.")
if recovery_handoff:
_print_recovery_handoff(recovery_handoff)
elif recovery_summary:
print("Follow the recovery hint above to finish setup or pivot to the alternate rail.")
if resolve_url:
print(f" Resolve URL: {resolve_url}")
if credential_modes_url:
print(f" Credential modes URL: {credential_modes_url}")
print("\n💡 Set RHUMB_API_KEY (your governed API key) to continue with estimation and execution.")
return
# Step 2: Estimate cost before committing
if not top_provider:
print("No execute-ready provider found in the current resolve context.")
if recovery_handoff:
_print_recovery_handoff(recovery_handoff)
elif recovery_summary:
print("Follow the recovery hint above to finish setup or pivot to the alternate rail.")
if resolve_url:
print(f" Resolve URL: {resolve_url}")
if credential_modes_url:
print(f" Credential modes URL: {credential_modes_url}")
return
print(f"\n💰 Estimating cost for '{capability}' via {top_provider}...\n")
est_resp = httpx.get(
f"{BASE}/capabilities/{capability}/execute/estimate",
params={"provider": top_provider},
headers=headers,
)
est = est_resp.json()
print(f" Estimated cost: ${est.get('data', {}).get('estimated_cost_usd', '?')}")
print(f" Circuit state: {est.get('data', {}).get('circuit_state', '?')}")
# Step 3: Execute
print(f"\n🚀 Executing '{capability}' via {top_provider}...\n")
exec_resp = httpx.post(
f"{BASE}/capabilities/{capability}/execute",
headers={**headers, "Content-Type": "application/json"},
json={
"provider": top_provider,
"params": {"query": "best practices for AI agent development"},
},
)
result = exec_resp.json()
print(f" Status: {exec_resp.status_code}")
print(f" Response preview: {json.dumps(result, indent=2)[:500]}")
if __name__ == "__main__":
main()