-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (83 loc) · 3.04 KB
/
main.py
File metadata and controls
99 lines (83 loc) · 3.04 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
"""Cheap-tier first; escalate to deep tier via agent.handoff."""
from __future__ import annotations
import asyncio
import base64
import hashlib
import json
import uuid
from arcp import ARCPClient, ARCPError, ErrorCode
from .cheap import attempt # cheap-tier LLM call → (answer, confidence)
CONFIDENCE_THRESHOLD = 0.65
CHEAP_URL = "wss://haiku-pool.tier1.internal"
DEEP_URL = "wss://opus-pool.tier3.internal"
DEEP_KIND = "arcp-opus-pool"
DEEP_FINGERPRINT = "sha256:0a37bf7d61cca21f00..." # pinned
async def package_context(
client: ARCPClient, *, transcript: dict[str, object]
) -> dict[str, object]:
body = json.dumps(transcript, sort_keys=True).encode()
artifact_id = f"art_{uuid.uuid4().hex[:14]}"
reply = await client.request(
client.envelope(
"artifact.put",
payload={
"artifact_id": artifact_id,
"media_type": "application/json",
"size": len(body),
"sha256": hashlib.sha256(body).hexdigest(),
"data": base64.b64encode(body).decode(),
},
),
timeout=15.0,
)
if reply.type != "artifact.ref":
raise ARCPError(ErrorCode.INTERNAL, f"got {reply.type}")
return reply.payload
async def emit_handoff(
client: ARCPClient, *, artifact_ref: dict[str, object], trace_id: str
) -> None:
await client.send(
client.envelope(
"agent.handoff",
trace_id=trace_id,
payload={
"target_runtime": {
"url": DEEP_URL,
"kind": DEEP_KIND,
"fingerprint": DEEP_FINGERPRINT,
},
"session_id": client.session_id,
# Spec gestures at shared_memory_ref (RFC §14); we use it
# explicitly so the deep tier knows where the transcript lives.
"shared_memory_ref": artifact_ref,
},
)
)
async def main() -> None:
cheap = ARCPClient(...) # transport=WebSocketTransport(CHEAP_URL), pinned
accepted = await cheap.open()
# Pin runtime kind + fingerprint (RFC §8.3); refuse on mismatch.
if accepted.runtime.kind != "arcp-haiku-pool":
raise ARCPError(ErrorCode.UNAUTHENTICATED, "cheap kind mismatch")
request = "what does CRDT stand for?"
trace_id = f"trace_{uuid.uuid4().hex[:12]}"
answer, confidence = await attempt(request)
if confidence >= CONFIDENCE_THRESHOLD:
print(answer)
else:
artifact = await package_context(
cheap,
transcript={
"user_request": request,
"transcript": [
{"role": "user", "content": request},
{"role": "assistant", "content": answer},
],
"cheap_confidence": confidence,
},
)
await emit_handoff(cheap, artifact_ref=artifact, trace_id=trace_id)
print(f"[handed off to {DEEP_KIND} trace_id={trace_id}]")
await cheap.close()
if __name__ == "__main__":
asyncio.run(main())