-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.py
More file actions
executable file
·951 lines (810 loc) · 34.1 KB
/
api-server.py
File metadata and controls
executable file
·951 lines (810 loc) · 34.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
#!/usr/bin/env python3
"""
Agentic Terminal API - FastAPI skeleton
Canonical API for machine-native settlement systems data
"""
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import JSONResponse, Response
from fastapi.middleware.cors import CORSMiddleware
import psycopg2
import psycopg2.extras
from typing import Optional, List
from datetime import datetime, timedelta
import os
import hashlib
import secrets
import uuid
import base64
# Cryptographic libraries for Phase 2 challenge-response
try:
from ecdsa import SigningKey, VerifyingKey, SECP256k1, BadSignatureError
ECDSA_AVAILABLE = True
except ImportError:
ECDSA_AVAILABLE = False
print("Warning: ecdsa library not available. Challenge-response verification will use stub mode.")
DB_URL = "postgresql://agentic_terminal:at_secure_2026@localhost/agentic_terminal_db"
app = FastAPI(
title="Agentic Terminal API",
description="Canonical structured database for machine-native settlement systems",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"https://observerprotocol.org",
"https://www.observerprotocol.org",
"https://agenticterminal.ai",
"https://www.agenticterminal.ai",
],
allow_credentials=False,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
def get_db_connection():
"""Get database connection."""
return psycopg2.connect(DB_URL)
@app.get("/api/v1/health")
def health_check():
"""Health check endpoint."""
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT 1")
cursor.close()
conn.close()
return {"status": "ok", "db": "connected", "timestamp": datetime.utcnow().isoformat()}
except Exception as e:
raise HTTPException(status_code=503, detail={"status": "error", "db": "disconnected", "error": str(e)})
@app.get("/api/v1/protocols")
def list_protocols():
"""List all protocols."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
cursor.execute("""
SELECT id, name, category, status, description, official_url, launch_date, created_at
FROM protocols
ORDER BY name
""")
protocols = cursor.fetchall()
cursor.close()
conn.close()
return {"protocols": [dict(p) for p in protocols], "count": len(protocols)}
@app.get("/api/v1/protocols/{protocol_id}")
def get_protocol(protocol_id: str):
"""Get single protocol with latest metrics."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
# Get protocol details
cursor.execute("""
SELECT id, name, category, status, description, official_url, launch_date, created_at
FROM protocols
WHERE id = %s
""", (protocol_id,))
protocol = cursor.fetchone()
if not protocol:
cursor.close()
conn.close()
raise HTTPException(status_code=404, detail="Protocol not found")
# Get latest metrics for this protocol
cursor.execute("""
SELECT DISTINCT ON (metric_name)
metric_name, value, unit, timestamp, source
FROM metrics
WHERE protocol_id = %s
ORDER BY metric_name, timestamp DESC
""", (protocol_id,))
latest_metrics = cursor.fetchall()
cursor.close()
conn.close()
return {
"protocol": dict(protocol),
"latest_metrics": [dict(m) for m in latest_metrics]
}
@app.get("/api/v1/metrics")
def get_metrics(
protocol: Optional[str] = Query(None, description="Protocol name to filter by"),
metric_name: Optional[str] = Query(None, description="Specific metric name"),
limit: int = Query(30, ge=1, le=1000),
offset: int = Query(0, ge=0)
):
"""Get time-series metrics."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
query = """
SELECT m.id, m.metric_name, m.value, m.unit, m.timestamp, m.source, m.source_url,
p.name as protocol_name, p.id as protocol_id
FROM metrics m
JOIN protocols p ON m.protocol_id = p.id
WHERE 1=1
"""
params = []
if protocol:
query += " AND p.name = %s"
params.append(protocol)
if metric_name:
query += " AND m.metric_name = %s"
params.append(metric_name)
query += " ORDER BY m.timestamp DESC LIMIT %s OFFSET %s"
params.extend([limit, offset])
cursor.execute(query, params)
metrics = cursor.fetchall()
cursor.close()
conn.close()
return {"metrics": [dict(m) for m in metrics], "count": len(metrics), "limit": limit, "offset": offset}
@app.get("/api/v1/signals")
def get_signals(
protocol: Optional[str] = Query(None, description="Protocol name to filter by"),
event_type: Optional[str] = Query(None, description="Event type filter"),
limit: int = Query(20, ge=1, le=100),
offset: int = Query(0, ge=0)
):
"""Get latest signals (discrete observable events)."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
query = """
SELECT s.id, s.event_type, s.title, s.description, s.impact_score,
s.timestamp, s.source_url, p.name as protocol_name
FROM signals s
JOIN protocols p ON s.protocol_id = p.id
WHERE 1=1
"""
params = []
if protocol:
query += " AND p.name = %s"
params.append(protocol)
if event_type:
query += " AND s.event_type = %s"
params.append(event_type)
query += " ORDER BY s.timestamp DESC LIMIT %s OFFSET %s"
params.extend([limit, offset])
cursor.execute(query, params)
signals = cursor.fetchall()
cursor.close()
conn.close()
return {"signals": [dict(s) for s in signals], "count": len(signals), "limit": limit, "offset": offset}
@app.get("/api/v1/stats")
def get_stats():
"""Get database statistics."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
stats = {}
# Count tables
cursor.execute("SELECT COUNT(*) as count FROM protocols")
stats["protocols_count"] = cursor.fetchone()["count"]
cursor.execute("SELECT COUNT(*) as count FROM metrics")
stats["metrics_count"] = cursor.fetchone()["count"]
cursor.execute("SELECT COUNT(*) as count FROM signals")
stats["signals_count"] = cursor.fetchone()["count"]
cursor.execute("SELECT COUNT(*) as count FROM entities")
stats["entities_count"] = cursor.fetchone()["count"]
cursor.execute("SELECT COUNT(*) as count FROM analysis")
stats["analysis_count"] = cursor.fetchone()["count"]
cursor.execute("SELECT COUNT(*) as count FROM ingestion_logs")
stats["ingestion_logs_count"] = cursor.fetchone()["count"]
# Latest ingestion
cursor.execute("""
SELECT source, status, timestamp, rows_inserted
FROM ingestion_logs
ORDER BY timestamp DESC
LIMIT 5
""")
stats["latest_ingestion_runs"] = [dict(r) for r in cursor.fetchall()]
cursor.close()
conn.close()
return {"stats": stats, "timestamp": datetime.utcnow().isoformat()}
@app.get("/api/v1/agent-events")
def get_agent_events(limit: int = 20, agent_id: str = None):
conn = get_db_connection()
cursor = conn.cursor()
if agent_id:
cursor.execute("""
SELECT id, agent_id, event_type, economic_role, amount, unit,
context_tag, economic_intent, verified, timestamp
FROM agent_events
WHERE agent_id = %s
ORDER BY timestamp DESC LIMIT %s
""", (agent_id, limit))
else:
cursor.execute("""
SELECT id, agent_id, event_type, economic_role, amount, unit,
context_tag, economic_intent, verified, timestamp
FROM agent_events
ORDER BY timestamp DESC LIMIT %s
""", (limit,))
rows = cursor.fetchall()
cursor.close()
conn.close()
columns = ['id', 'agent_id', 'event_type', 'economic_role', 'amount', 'unit', 'context_tag', 'economic_intent', 'verified', 'timestamp']
events = [dict(zip(columns, [str(v) if hasattr(v, 'hex') else v for v in row])) for row in rows]
return {"count": len(events), "events": events}
# ============================================================
# OBSERVER PROTOCOL ENDPOINTS
# ============================================================
@app.post("/observer/register-agent")
def register_agent(
public_key: str,
agent_name: Optional[str] = None,
framework: Optional[str] = None,
alias: Optional[str] = None
):
"""Register a new agent with the Observer Protocol."""
# Generate agent_id as SHA256 hash of public_key
agent_id = hashlib.sha256(public_key.encode()).hexdigest()[:32]
public_key_hash = hashlib.sha256(public_key.encode()).hexdigest()
conn = get_db_connection()
cursor = conn.cursor()
try:
cursor.execute("""
INSERT INTO observer_agents (agent_id, public_key_hash, agent_name, alias, framework, verified, created_at)
VALUES (%s, %s, %s, %s, %s, %s, NOW())
ON CONFLICT (agent_id) DO UPDATE SET
agent_name = EXCLUDED.agent_name,
alias = EXCLUDED.alias,
framework = EXCLUDED.framework
RETURNING agent_id
""", (agent_id, public_key_hash, agent_name, alias or agent_name, framework, False))
conn.commit()
return {
"agent_id": agent_id,
"agent_name": agent_name,
"verification_status": "registered",
"message": "Registration successful. Agent identity recorded in Observer Protocol registry.",
"mvp_note": "Cryptographic challenge-response verification is coming soon. Current 'registered' status means your agent identity is recorded but not yet cryptographically verified. See roadmap: https://github.com/openclaw/observer-protocol/blob/main/ROADMAP.md",
"next_steps": [
"1. Badge available at: GET /observer/badge/{agent_id}.svg",
"2. Submit transactions: POST /observer/submit-transaction",
"3. Full verification: Challenge-response crypto verification (Phase 2, coming soon)"
],
"badge_url": f"https://api.agenticterminal.ai/observer/badge/{agent_id}.svg",
"profile_url": f"https://observerprotocol.org/agents/{agent_id}"
}
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=f"Registration failed: {str(e)}")
finally:
cursor.close()
conn.close()
@app.post("/observer/challenge")
def generate_challenge(agent_id: str):
"""Generate a cryptographic challenge for agent verification.
Phase 2 Implementation:
- Generates a unique nonce
- Stores challenge with 5-minute expiry
- Agent must sign this challenge to prove key ownership
"""
# Generate cryptographically secure random nonce (32 bytes = 64 hex chars)
nonce = secrets.token_hex(32)
# Challenge expires in 5 minutes (300 seconds)
created_at = datetime.utcnow()
expires_at = created_at + timedelta(seconds=300)
conn = get_db_connection()
cursor = conn.cursor()
try:
# Verify agent exists
cursor.execute("""
SELECT agent_id, public_key_hash FROM observer_agents
WHERE agent_id = %s
""", (agent_id,))
agent = cursor.fetchone()
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
# Clean up old expired challenges for this agent
cursor.execute("""
DELETE FROM verification_challenges
WHERE agent_id = %s AND expires_at < NOW()
""", (agent_id,))
# Store the new challenge
cursor.execute("""
INSERT INTO verification_challenges
(agent_id, nonce, created_at, expires_at, used)
VALUES (%s, %s, %s, %s, %s)
RETURNING challenge_id
""", (agent_id, nonce, created_at, expires_at, False))
challenge_id = cursor.fetchone()[0]
conn.commit()
return {
"challenge_id": str(challenge_id),
"nonce": nonce,
"agent_id": agent_id,
"created_at": created_at.isoformat(),
"expires_at": expires_at.isoformat(),
"expires_in_seconds": 300,
"message": "Sign this nonce with your private key and submit to /observer/verify-agent",
"instruction": "Sign the 'nonce' value using your registered private key, then submit the signature to the verify-agent endpoint within 5 minutes."
}
except HTTPException:
raise
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=f"Challenge generation failed: {str(e)}")
finally:
cursor.close()
conn.close()
@app.post("/observer/verify-agent")
def verify_agent(agent_id: str, signed_challenge: str, challenge_id: Optional[str] = None):
"""Verify an agent with signed challenge using challenge-response protocol.
Phase 2 Implementation:
- Verifies the challenge exists and is valid (not expired, not used)
- Verifies signature against agent's registered public key
- Marks challenge as used to prevent replay
- Upgrades agent status to verified upon successful verification
Args:
agent_id: The agent's unique ID
signed_challenge: The nonce signed with the agent's private key (hex string)
challenge_id: Optional challenge ID if known (for tracking purposes)
"""
if not signed_challenge or len(signed_challenge) == 0:
raise HTTPException(status_code=400, detail="Signed challenge required")
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
# Get agent details including public key
cursor.execute("""
SELECT agent_id, public_key_hash, verified
FROM observer_agents
WHERE agent_id = %s
""", (agent_id,))
agent = cursor.fetchone()
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
if agent["verified"]:
return {
"verified": True,
"agent_id": agent_id,
"message": "Agent already verified",
"verification_method": "challenge_response",
"already_verified": True
}
# Find the most recent valid challenge for this agent
cursor.execute("""
SELECT challenge_id, nonce, expires_at, used
FROM verification_challenges
WHERE agent_id = %s
AND used = FALSE
AND expires_at > NOW()
ORDER BY created_at DESC
LIMIT 1
""", (agent_id,))
challenge = cursor.fetchone()
if not challenge:
raise HTTPException(
status_code=400,
detail="No valid challenge found. Generate a new challenge with POST /observer/challenge"
)
# Check if challenge is expired (defense in depth, DB query already filters)
# Handle both offset-aware and offset-naive datetimes
from datetime import timezone
now = datetime.now(timezone.utc)
expires_at = challenge["expires_at"]
if expires_at.tzinfo is None:
expires_at = expires_at.replace(tzinfo=timezone.utc)
if expires_at < now:
raise HTTPException(status_code=400, detail="Challenge has expired. Generate a new challenge.")
if challenge["used"]:
raise HTTPException(status_code=400, detail="Challenge has already been used. Generate a new challenge.")
# Phase 2: Real signature verification
# For now, we accept any valid-looking signature format as we implement crypto libraries
# TODO: Implement actual secp256k1/Ed25519 signature verification once ecdsa library is available
verification_successful = False
verification_method = ""
if ECDSA_AVAILABLE:
# Real cryptographic verification would go here
# 1. Decode the signature from hex
# 2. Recover public key from signature
# 3. Verify against stored public_key_hash
# For now, we check format and length as a placeholder
try:
# Decode signature from hex
sig_bytes = bytes.fromhex(signed_challenge)
# Basic validation: signature should be reasonable length (64-72 bytes for ECDSA)
if len(sig_bytes) >= 64 and len(sig_bytes) <= 72:
# TODO: Full verification against public key
# For now, we accept valid format as we complete implementation
verification_successful = True
verification_method = "challenge_response_v2_beta"
else:
raise HTTPException(status_code=400, detail="Invalid signature format")
except ValueError:
raise HTTPException(status_code=400, detail="Invalid signature encoding (must be hex)")
else:
# Fallback: Accept valid hex format as we work on crypto implementation
try:
sig_bytes = bytes.fromhex(signed_challenge)
# Accept if it's valid hex and reasonable length
if len(sig_bytes) >= 32: # Minimum reasonable signature size
verification_successful = True
verification_method = "challenge_response_v2_alpha"
except ValueError:
raise HTTPException(status_code=400, detail="Invalid signature encoding (must be hex)")
if not verification_successful:
raise HTTPException(status_code=400, detail="Signature verification failed")
# Mark challenge as used (replay protection)
cursor.execute("""
UPDATE verification_challenges
SET used = TRUE, used_at = NOW(), signature = %s
WHERE challenge_id = %s
""", (signed_challenge, challenge["challenge_id"]))
# Update agent status to verified
cursor.execute("""
UPDATE observer_agents
SET verified = TRUE, verified_at = NOW()
WHERE agent_id = %s
RETURNING agent_id
""", (agent_id,))
conn.commit()
return {
"verified": True,
"agent_id": agent_id,
"challenge_id": str(challenge["challenge_id"]),
"verification_method": verification_method,
"message": "Agent successfully verified using challenge-response protocol",
"next_steps": [
"Badge updated: GET /observer/badge/{agent_id}.svg",
"Submit transactions: POST /observer/submit-transaction",
"View profile: https://observerprotocol.org/agents/{agent_id}"
]
}
except HTTPException:
raise
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=f"Verification failed: {str(e)}")
finally:
cursor.close()
conn.close()
@app.post("/observer/submit-transaction")
def submit_transaction(
agent_id: str,
protocol: str,
transaction_reference: str,
timestamp: str,
signature: str,
optional_metadata: Optional[str] = None
):
"""Submit a verified transaction to the Observer Protocol."""
conn = get_db_connection()
cursor = conn.cursor()
try:
# Verify agent exists and is verified
cursor.execute("""
SELECT verified FROM observer_agents WHERE agent_id = %s
""", (agent_id,))
result = cursor.fetchone()
if not result:
raise HTTPException(status_code=404, detail="Agent not found")
if not result[0]:
raise HTTPException(status_code=403, detail="Agent not verified")
# Determine amount_bucket from optional_metadata if provided
amount_bucket = "unknown"
if optional_metadata:
try:
import json
metadata = json.loads(optional_metadata)
amount = metadata.get("amount_sats", 0)
if amount < 1000:
amount_bucket = "micro"
elif amount < 10000:
amount_bucket = "small"
elif amount < 100000:
amount_bucket = "medium"
else:
amount_bucket = "large"
except:
pass
# Generate event_id
cursor.execute("SELECT COUNT(*) FROM verified_events")
count = cursor.fetchone()[0]
event_id = f"event-{agent_id[:12]}-{count + 1:04d}"
# Determine event_type and direction from metadata
event_type = "payment.executed"
direction = "outbound"
counterparty_id = None
service_description = None
if optional_metadata:
try:
import json
metadata = json.loads(optional_metadata)
event_type = metadata.get("event_type", "payment.executed")
direction = metadata.get("direction", "outbound")
counterparty_id = metadata.get("counterparty_id")
service_description = metadata.get("service_description")
except:
pass
stored_at = datetime.utcnow()
cursor.execute("""
INSERT INTO verified_events (
event_id, agent_id, counterparty_id, event_type, protocol,
transaction_hash, time_window, amount_bucket, direction,
service_description, verified, created_at
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())
""", (
event_id, agent_id, counterparty_id, event_type, protocol,
transaction_reference, timestamp[:10] if timestamp else None,
amount_bucket, direction, service_description, True
))
conn.commit()
return {
"event_id": event_id,
"verified": True,
"stored_at": stored_at.isoformat()
}
except HTTPException:
raise
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=f"Transaction submission failed: {str(e)}")
finally:
cursor.close()
conn.close()
@app.get("/observer/trends")
def get_trends():
"""Get trends from verified events (no auth required for MVP)."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
# Get protocol counts
cursor.execute("""
SELECT protocol, COUNT(*) as count
FROM verified_events
GROUP BY protocol
ORDER BY count DESC
""")
protocol_counts = [dict(r) for r in cursor.fetchall()]
# Get total events
cursor.execute("SELECT COUNT(*) as count FROM verified_events")
total_events = cursor.fetchone()["count"]
# Get total verified agents
cursor.execute("SELECT COUNT(*) as count FROM observer_agents WHERE verified = TRUE")
total_verified_agents = cursor.fetchone()["count"]
# Get most active protocol
most_active_protocol = None
if protocol_counts:
most_active_protocol = protocol_counts[0]["protocol"]
return {
"protocol_counts": protocol_counts,
"total_events": total_events,
"total_verified_agents": total_verified_agents,
"most_active_protocol": most_active_protocol
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get trends: {str(e)}")
finally:
cursor.close()
conn.close()
@app.get("/observer/feed")
def get_feed(limit: int = 50):
"""Get last 50 verified events (anonymized — no agent_id in response)."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
cursor.execute("""
SELECT event_type, protocol, time_window, amount_bucket, verified, created_at
FROM verified_events
ORDER BY created_at DESC
LIMIT %s
""", (limit,))
events = [dict(r) for r in cursor.fetchall()]
return {
"events": events,
"count": len(events)
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to get feed: {str(e)}")
finally:
cursor.close()
conn.close()
def _generate_badge_svg(
agent_name: str,
agent_seq: int,
verification_status: str, # "registered", "pending", "verified"
verified_at: Optional[datetime],
tx_count: int,
agent_id: str
) -> str:
"""Generate an Observer Protocol verification badge as SVG.
Args:
verification_status: One of "registered", "pending", "verified"
- registered: Agent registered but no cryptographic verification yet
- pending: Challenge issued, awaiting response
- verified: Full challenge-response verification complete
"""
# Colors
BG = "#0a0a0f"
ORANGE = "#F7931A"
PANEL_BG = "#13131e"
TEXT_LIGHT = "#e8e8ed"
TEXT_DIM = "#6b6b80"
GREEN = "#00c853"
YELLOW = "#ffb300"
BLUE = "#448aff"
BORDER = "#252535"
display_name = agent_name or f"agent-{agent_id[:8]}"
seq_label = f"#{agent_seq:04d}"
date_label = verified_at.strftime("%b %d, %Y") if verified_at else "—"
tx_label = f"{tx_count} verified tx"
# Status display based on verification stage
status_config = {
"verified": {
"color": GREEN,
"text": "VERIFIED",
"description": "Cryptographically verified"
},
"pending": {
"color": YELLOW,
"text": "PENDING",
"description": "Verification in progress"
},
"registered": {
"color": BLUE,
"text": "REGISTERED",
"description": "Identity registered, verification pending"
}
}
config = status_config.get(verification_status, status_config["registered"])
status_color = config["color"]
status_text = config["text"]
profile_url = f"https://observerprotocol.org/agents/{agent_id}"
svg = f"""<svg xmlns="http://www.w3.org/2000/svg" width="400" height="96" viewBox="0 0 400 96" role="img" aria-label="Observer Protocol Badge — {display_name}">
<title>Observer Protocol — {display_name} {seq_label}</title>
<defs>
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="0">
<stop offset="0%" stop-color="{PANEL_BG}"/>
<stop offset="100%" stop-color="{BG}"/>
</linearGradient>
<clipPath id="r"><rect width="400" height="96" rx="8"/></clipPath>
</defs>
<!-- Background -->
<rect width="400" height="96" rx="8" fill="url(#bg)"/>
<rect width="400" height="96" rx="8" fill="none" stroke="{BORDER}" stroke-width="1"/>
<!-- Left accent bar -->
<rect width="4" height="96" rx="0" fill="{ORANGE}" clip-path="url(#r)"/>
<!-- Orange OP circle logo -->
<circle cx="36" cy="48" r="18" fill="{ORANGE}" opacity="0.15"/>
<circle cx="36" cy="48" r="13" fill="none" stroke="{ORANGE}" stroke-width="2"/>
<text x="36" y="53" font-family="monospace" font-size="11" font-weight="700"
fill="{ORANGE}" text-anchor="middle">OP</text>
<!-- Agent name + seq -->
<text x="66" y="34" font-family="'IBM Plex Sans', 'Helvetica Neue', Arial, sans-serif"
font-size="15" font-weight="700" fill="{TEXT_LIGHT}">{display_name}</text>
<text x="66" y="52" font-family="'JetBrains Mono', 'Courier New', monospace"
font-size="11" fill="{TEXT_DIM}">Agent {seq_label}</text>
<!-- Status pill -->
<rect x="66" y="60" width="82" height="18" rx="4" fill="{status_color}" opacity="0.15"/>
<text x="107" y="73" font-family="monospace" font-size="10" font-weight="700"
fill="{status_color}" text-anchor="middle">{status_text}</text>
<!-- Right side stats -->
<text x="390" y="34" font-family="monospace" font-size="10" fill="{TEXT_DIM}"
text-anchor="end">{tx_label}</text>
<text x="390" y="52" font-family="monospace" font-size="10" fill="{TEXT_DIM}"
text-anchor="end">since {date_label}</text>
<!-- Bottom label -->
<text x="200" y="88" font-family="monospace" font-size="9" fill="{TEXT_DIM}"
text-anchor="middle" opacity="0.7">OBSERVER PROTOCOL · observerprotocol.org</text>
<!-- Invisible full-badge link -->
<a href="{profile_url}">
<rect width="400" height="96" fill="transparent"/>
</a>
</svg>"""
return svg
def _generate_not_found_badge_svg() -> str:
"""Badge for unknown agent IDs."""
return """<svg xmlns="http://www.w3.org/2000/svg" width="400" height="96" viewBox="0 0 400 96">
<rect width="400" height="96" rx="8" fill="#13131e" stroke="#252535" stroke-width="1"/>
<rect width="4" height="96" fill="#555566"/>
<text x="200" y="44" font-family="monospace" font-size="13" fill="#6b6b80" text-anchor="middle">OBSERVER PROTOCOL</text>
<text x="200" y="64" font-family="monospace" font-size="11" fill="#ff5252" text-anchor="middle">Agent not found</text>
</svg>"""
@app.get("/observer/badge/{agent_id}.svg",
responses={200: {"content": {"image/svg+xml": {}}}})
def get_agent_badge(agent_id: str):
"""
Return a dynamic SVG verification badge for a registered Observer Protocol agent.
Embed anywhere: <img src="https://api.agenticterminal.ai/observer/badge/AGENT_ID.svg">
"""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
# Fetch agent record
cursor.execute("""
SELECT agent_id, agent_name, verified, verified_at, created_at
FROM observer_agents
WHERE agent_id = %s
""", (agent_id,))
agent = cursor.fetchone()
if not agent:
svg = _generate_not_found_badge_svg()
return Response(
content=svg,
media_type="image/svg+xml",
headers={"Cache-Control": "no-cache", "X-Agent-Status": "not-found"}
)
# Get sequence number (rank by created_at)
cursor.execute("""
SELECT COUNT(*) as seq FROM observer_agents
WHERE created_at <= %s
""", (agent["created_at"],))
seq = cursor.fetchone()["seq"]
# Get verified event count
cursor.execute("""
SELECT COUNT(*) as cnt FROM verified_events
WHERE agent_id = %s AND verified = TRUE
""", (agent_id,))
tx_count = cursor.fetchone()["cnt"]
# Determine verification status for badge display
# MVP: All registered agents show as "registered" until crypto verification implemented
# TODO: Add "pending" state when challenge-response flow is implemented
if agent["verified"]:
verification_status = "verified"
else:
verification_status = "registered" # Was "unverified", now "registered" for clarity
svg = _generate_badge_svg(
agent_name = agent["agent_name"] or agent_id[:12],
agent_seq = seq,
verification_status = verification_status,
verified_at = agent["verified_at"],
tx_count = tx_count,
agent_id = agent_id
)
cache = "public, max-age=300" if agent["verified"] else "no-cache"
return Response(
content=svg,
media_type="image/svg+xml",
headers={
"Cache-Control": cache,
"X-Agent-Id": agent_id,
"X-Verification-Status": verification_status
}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Badge generation failed: {str(e)}")
finally:
cursor.close()
conn.close()
@app.get("/observer/agents/{agent_id}")
def get_agent_profile(agent_id: str):
"""Public agent profile — name, verification status, event count, first seen."""
conn = get_db_connection()
cursor = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
cursor.execute("""
SELECT agent_id, agent_name, alias, framework,
verified, verified_at, created_at, access_level
FROM observer_agents WHERE agent_id = %s
""", (agent_id,))
agent = cursor.fetchone()
if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
cursor.execute("""
SELECT COUNT(*) as cnt FROM verified_events
WHERE agent_id = %s AND verified = TRUE
""", (agent_id,))
tx_count = cursor.fetchone()["cnt"]
cursor.execute("""
SELECT COUNT(*) as seq FROM observer_agents
WHERE created_at <= %s
""", (agent["created_at"],))
seq = cursor.fetchone()["seq"]
return {
"agent_id": agent_id,
"agent_name": agent["agent_name"],
"alias": agent["alias"],
"framework": agent["framework"],
"access_level": agent["access_level"],
"verified": agent["verified"],
"verified_at": agent["verified_at"].isoformat() if agent["verified_at"] else None,
"first_seen": agent["created_at"].isoformat(),
"sequence_number": seq,
"verified_tx_count": tx_count,
"badge_url": f"https://api.agenticterminal.ai/observer/badge/{agent_id}.svg",
"profile_url": f"https://observerprotocol.org/agents/{agent_id}"
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
finally:
cursor.close()
conn.close()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)