cloud_knowledge.py
7.84 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
"""
Sprint 11 — Collective Knowledge Network client.
Uploads structural operation knowledge (not user data) to cloud API.
Pulls verified execution paths from cloud before local discovery.
"""
from __future__ import annotations
import json
import os
import time
import urllib.request
import urllib.error
from pathlib import Path
CLOUD_API = os.environ.get("HERMES_CLOUD_API", "https://api.hermes-agent.ai/v1")
CLOUD_KEY = os.environ.get("HERMES_CLOUD_KEY", "")
LOCAL_CACHE = Path(os.environ.get(
"CLOUD_CACHE_DIR",
str(Path(os.environ.get("HOME", "/data/data/com.termux/files/home")) / ".hermes" / "cloud_cache")
))
UPLOAD_LOG = Path(os.environ.get(
"UPLOAD_LOG",
str(Path(os.environ.get("HOME", "/data/data/com.termux/files/home")) / ".hermes" / "cloud_uploads.jsonl")
))
# Schema version for knowledge entries
SCHEMA_VERSION = "1.0"
# ──── Knowledge Entry Format (S11-01) ─────────────────────────────────────────
def make_knowledge_entry(
intent: str,
app: str,
app_version: str,
execution_path: list[dict],
success_rate: float = 1.0,
notes: str = "",
) -> dict:
"""
Create a knowledge entry for upload.
Only structural knowledge — no user data (content, contacts, messages).
"""
return {
"schema_version": SCHEMA_VERSION,
"intent": intent,
"app": app,
"app_version": app_version,
"execution_path": execution_path,
"success_rate": success_rate,
"notes": notes,
"validated_by": 1,
"ts": int(time.time()),
"device_type": "pixel_8_pro",
"android_version": "14",
}
# ──── Upload client (S11-02) ──────────────────────────────────────────────────
def upload_knowledge(entry: dict) -> dict:
"""Upload a knowledge entry to cloud API."""
if not CLOUD_KEY:
return {"error": "HERMES_CLOUD_KEY not set", "skipped": True}
LOCAL_CACHE.mkdir(parents=True, exist_ok=True)
# Log locally regardless of upload success
_log_entry = {**entry, "upload_ts": int(time.time())}
with open(UPLOAD_LOG, "a", encoding="utf-8") as f:
f.write(json.dumps(_log_entry, ensure_ascii=False) + "\n")
# Upload to cloud
try:
payload = json.dumps(entry).encode()
req = urllib.request.Request(
f"{CLOUD_API}/knowledge",
data=payload,
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {CLOUD_KEY}",
"X-Schema-Version": SCHEMA_VERSION,
},
method="POST",
)
with urllib.request.urlopen(req, timeout=10) as resp:
return json.loads(resp.read().decode())
except urllib.error.HTTPError as e:
return {"error": f"HTTP {e.code}", "body": e.read().decode()[:200]}
except Exception as e:
# Store locally for retry
pending_path = LOCAL_CACHE / f"pending_{int(time.time())}.json"
pending_path.write_text(json.dumps(entry, ensure_ascii=False), encoding="utf-8")
return {"error": str(e), "pending": str(pending_path)}
# ──── Pull client (S11-03) ────────────────────────────────────────────────────
def pull_knowledge(app: str, intent: str, app_version: str = "") -> list[dict]:
"""
Pull verified knowledge from cloud for a given app/intent.
Returns list of execution_path entries sorted by success_rate descending.
Falls back to local cache if cloud is unavailable.
"""
LOCAL_CACHE.mkdir(parents=True, exist_ok=True)
cache_key = f"{app}_{intent}_{app_version}".replace("/", "_")
cache_file = LOCAL_CACHE / f"{cache_key}.json"
# Try cloud first
if CLOUD_KEY:
try:
params = f"app={app}&intent={intent}"
if app_version:
params += f"&app_version={app_version}"
req = urllib.request.Request(
f"{CLOUD_API}/knowledge?{params}",
headers={"Authorization": f"Bearer {CLOUD_KEY}"},
)
with urllib.request.urlopen(req, timeout=5) as resp:
data = json.loads(resp.read().decode())
entries = data.get("entries", [])
# Cache locally
cache_file.write_text(
json.dumps(entries, ensure_ascii=False), encoding="utf-8")
return entries
except Exception:
pass
# Fallback to local cache
if cache_file.exists():
try:
return json.loads(cache_file.read_text(encoding="utf-8"))
except Exception:
pass
return []
def get_best_execution_path(app: str, intent: str, app_version: str = "") -> list[dict] | None:
"""Return the highest success_rate execution path from cloud/cache, or None."""
entries = pull_knowledge(app, intent, app_version)
if not entries:
return None
entries.sort(key=lambda e: e.get("success_rate", 0), reverse=True)
return entries[0].get("execution_path")
# ──── Auto-upload after successful operations ──────────────────────────────────
def record_success(app: str, intent: str, method: str, app_version: str = "") -> dict:
"""Record a successful operation execution to cloud (async-safe)."""
entry = make_knowledge_entry(
intent=intent,
app=app,
app_version=app_version,
execution_path=[{"method": method, "step": 1, "result": "success"}],
success_rate=1.0,
)
return upload_knowledge(entry)
def retry_pending_uploads() -> dict:
"""Retry any locally-cached pending uploads."""
if not CLOUD_KEY:
return {"skipped": True}
LOCAL_CACHE.mkdir(parents=True, exist_ok=True)
pending = list(LOCAL_CACHE.glob("pending_*.json"))
uploaded = 0
failed = 0
for p in pending:
try:
entry = json.loads(p.read_text(encoding="utf-8"))
result = upload_knowledge(entry)
if "error" not in result or result.get("skipped"):
p.unlink()
uploaded += 1
else:
failed += 1
except Exception:
failed += 1
return {"uploaded": uploaded, "failed": failed, "pending_remaining": failed}
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "test":
# Test with sample knowledge entry (no real upload without API key)
entry = make_knowledge_entry(
intent="send_message",
app="wechat",
app_version="8.0.47",
execution_path=[
{"method": "accessibility", "step": 1, "action": "am_start", "target": "LauncherUI"},
{"method": "accessibility", "step": 2, "action": "input_text", "content": "<msg>"},
{"method": "accessibility", "step": 3, "action": "keyevent", "key": "ENTER"},
],
success_rate=0.95,
)
print("Knowledge entry:")
print(json.dumps(entry, ensure_ascii=False, indent=2))
# Test upload (will fail without API key)
result = upload_knowledge(entry)
print("\nUpload result:", json.dumps(result, ensure_ascii=False))
# Test pull
best = get_best_execution_path("wechat", "send_message")
print("\nBest execution path from cloud:", best)
else:
print("Usage: python3 cloud_knowledge.py test")
print(f"CLOUD_API: {CLOUD_API}")
print(f"CLOUD_KEY: {'set' if CLOUD_KEY else 'NOT SET'}")
pending = list(LOCAL_CACHE.glob("pending_*.json")) if LOCAL_CACHE.exists() else []
print(f"Pending uploads: {len(pending)}")