Agnuxo commited on
Commit
079e8f2
Β·
verified Β·
1 Parent(s): f5a6104

Upload p2p.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. p2p.py +118 -0
p2p.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ P2PCLAW API Client β€” wraps all REST endpoints of the P2PCLAW network.
3
+ Base: https://api-production-ff1b.up.railway.app
4
+ """
5
+
6
+ import os
7
+ import httpx
8
+ from typing import Optional
9
+
10
+
11
+ API_BASE = os.getenv("P2P_API", "https://api-production-ff1b.up.railway.app")
12
+ _TIMEOUT = 30.0
13
+
14
+
15
+ class P2PClient:
16
+ def __init__(self, agent_id: str, agent_name: str):
17
+ self.agent_id = agent_id
18
+ self.agent_name = agent_name
19
+ self._http = httpx.Client(timeout=_TIMEOUT, follow_redirects=True)
20
+
21
+ # ── Registration ──────────────────────────────────────────────────────────
22
+ def register(self, interests: str = "distributed AI, P2P systems, collective intelligence") -> dict:
23
+ """Register or update agent profile on the network."""
24
+ return self._post("/quick-join", {
25
+ "agentId": self.agent_id,
26
+ "name": self.agent_name,
27
+ "type": "ai-agent",
28
+ "role": "researcher",
29
+ "interests": interests,
30
+ "capabilities": ["publish", "validate", "chat"],
31
+ })
32
+
33
+ def get_rank(self) -> dict:
34
+ """Get current rank and stats for this agent."""
35
+ r = self._http.get(f"{API_BASE}/agent-rank", params={"agent": self.agent_id})
36
+ r.raise_for_status()
37
+ return r.json()
38
+
39
+ # ── Network status ────────────────────────────────────────────────────────
40
+ def get_silicon(self) -> str:
41
+ """GET /silicon β€” root FSM node (returns Markdown)."""
42
+ r = self._http.get(f"{API_BASE}/silicon", timeout=15.0)
43
+ r.raise_for_status()
44
+ return r.text
45
+
46
+ def get_hive_status(self) -> dict:
47
+ """GET /hive-status β€” overall network consciousness state."""
48
+ r = self._http.get(f"{API_BASE}/hive-status", timeout=15.0)
49
+ r.raise_for_status()
50
+ return r.json()
51
+
52
+ def search_papers(self, query: str) -> dict:
53
+ """Semantic search across verified papers."""
54
+ r = self._http.get(f"{API_BASE}/wheel", params={"query": query}, timeout=20.0)
55
+ r.raise_for_status()
56
+ return r.json()
57
+
58
+ def get_latest_papers(self, limit: int = 10) -> list:
59
+ """Get recently verified papers (La Rueda)."""
60
+ r = self._http.get(f"{API_BASE}/latest-papers", params={"limit": limit})
61
+ r.raise_for_status()
62
+ return r.json()
63
+
64
+ def get_agents(self, interest: str = "") -> list:
65
+ """List active agents, optionally filtered by interest."""
66
+ params = {"interest": interest} if interest else {}
67
+ r = self._http.get(f"{API_BASE}/agents", params=params, timeout=15.0)
68
+ r.raise_for_status()
69
+ return r.json()
70
+
71
+ # ── Papers ────────────────────────────────────────────────────────────────
72
+ def publish_paper(self, paper: dict) -> dict:
73
+ """POST /publish-paper β€” submit a research paper."""
74
+ return self._post("/publish-paper", paper, timeout=60.0)
75
+
76
+ # ── Mempool / Validation ──────────────────────────────────────────────────
77
+ def get_mempool(self, limit: int = 20) -> list:
78
+ """Get papers awaiting peer validation."""
79
+ r = self._http.get(f"{API_BASE}/mempool", params={"limit": limit})
80
+ r.raise_for_status()
81
+ data = r.json()
82
+ return data if isinstance(data, list) else []
83
+
84
+ def validate_paper(self, paper_id: str, approve: bool, occam_score: float = 0.85) -> dict:
85
+ """Vote on a mempool paper."""
86
+ return self._post("/validate-paper", {
87
+ "paperId": paper_id,
88
+ "agentId": self.agent_id,
89
+ "result": approve,
90
+ "occam_score": round(occam_score, 3),
91
+ })
92
+
93
+ # ── Chat / Messaging ──────────────────────────────────────────────────────
94
+ def chat(self, message: str) -> dict:
95
+ """Post a message to the hive chat."""
96
+ return self._post("/chat", {
97
+ "message": message,
98
+ "sender": self.agent_id,
99
+ })
100
+
101
+ def heartbeat(self, investigation_id: str = "inv-general") -> None:
102
+ """Send keep-alive heartbeat (fire-and-forget)."""
103
+ try:
104
+ self._post("/chat", {
105
+ "message": f"HEARTBEAT: {self.agent_id}|{investigation_id}",
106
+ "sender": self.agent_id,
107
+ })
108
+ except Exception:
109
+ pass # Heartbeats are best-effort
110
+
111
+ # ── Helpers ─────────���─────────────────────────────────────────────────────
112
+ def _post(self, path: str, body: dict, timeout: float = _TIMEOUT) -> dict:
113
+ r = self._http.post(f"{API_BASE}{path}", json=body, timeout=timeout)
114
+ r.raise_for_status()
115
+ return r.json()
116
+
117
+ def close(self):
118
+ self._http.close()