-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.py
More file actions
executable file
·276 lines (227 loc) · 8.28 KB
/
protocol.py
File metadata and controls
executable file
·276 lines (227 loc) · 8.28 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
"""
Agent Control Protocol - Core module
File-based communication between agent and human controller.
"""
import json
import time
import uuid
import os
from pathlib import Path
from dataclasses import dataclass, asdict
from typing import Optional, Literal
from datetime import datetime
# Protocol directory (configurable via env)
PROTOCOL_DIR = Path(os.environ.get("AGENT_PROTOCOL_DIR", "."))
# File paths
STATUS_FILE = PROTOCOL_DIR / "status.json"
REQUESTS_FILE = PROTOCOL_DIR / "requests.jsonl"
RESPONSES_FILE = PROTOCOL_DIR / "responses.jsonl"
LOG_FILE = PROTOCOL_DIR / "log.jsonl"
COMMANDS_FILE = PROTOCOL_DIR / "commands.jsonl"
# Polling config
POLL_INTERVAL = float(os.environ.get("AGENT_POLL_INTERVAL", "1.0"))
POLL_TIMEOUT = float(os.environ.get("AGENT_POLL_TIMEOUT", "3600")) # 1 hour default
def _append_jsonl(path: Path, data: dict):
"""Append a JSON record to a JSONL file."""
with open(path, "a") as f:
f.write(json.dumps(data) + "\n")
def _read_jsonl(path: Path) -> list[dict]:
"""Read all records from a JSONL file."""
if not path.exists():
return []
records = []
with open(path) as f:
for line in f:
line = line.strip()
if line:
records.append(json.loads(line))
return records
def _write_json(path: Path, data: dict):
"""Write JSON to file atomically."""
tmp = path.with_suffix(".tmp")
with open(tmp, "w") as f:
json.dump(data, f, indent=2)
tmp.rename(path)
def _read_json(path: Path) -> Optional[dict]:
"""Read JSON file if it exists."""
if not path.exists():
return None
with open(path) as f:
return json.load(f)
# === Agent-side API ===
class AgentProtocol:
"""Agent-side protocol handler."""
def __init__(self, agent_id: str = None):
self.agent_id = agent_id or f"agent_{uuid.uuid4().hex[:8]}"
self._request_counter = 0
self._processed_commands = set()
self._init_files()
def _init_files(self):
"""Ensure protocol files exist."""
PROTOCOL_DIR.mkdir(parents=True, exist_ok=True)
for f in [REQUESTS_FILE, RESPONSES_FILE, LOG_FILE, COMMANDS_FILE]:
f.touch()
self.set_status("idle")
def _next_request_id(self) -> str:
self._request_counter += 1
return f"req_{self.agent_id}_{self._request_counter:04d}"
def set_status(self, state: str, task: str = None, detail: str = None):
"""Update agent status."""
_write_json(STATUS_FILE, {
"agent_id": self.agent_id,
"state": state,
"task": task,
"detail": detail,
"updated_at": datetime.now().isoformat(),
})
def log(self, level: str, message: str, **extra):
"""Write to activity log."""
_append_jsonl(LOG_FILE, {
"timestamp": datetime.now().isoformat(),
"agent_id": self.agent_id,
"level": level,
"message": message,
**extra,
})
def request_input(
self,
prompt: str,
request_type: Literal["confirm", "choice", "input", "file"] = "input",
choices: list[str] = None,
default: str = None,
) -> str:
"""
Request input from human controller.
Blocks until response received or timeout.
"""
req_id = self._next_request_id()
request = {
"id": req_id,
"timestamp": datetime.now().isoformat(),
"type": request_type,
"prompt": prompt,
}
if choices:
request["choices"] = choices
if default is not None:
request["default"] = default
_append_jsonl(REQUESTS_FILE, request)
self.set_status("waiting_for_input", detail=prompt)
self.log("info", f"Waiting for input: {prompt}", request_id=req_id)
# Poll for response
start = time.time()
while time.time() - start < POLL_TIMEOUT:
responses = _read_jsonl(RESPONSES_FILE)
for resp in responses:
if resp.get("id") == req_id:
self.log("info", f"Got response: {resp.get('answer')}", request_id=req_id)
return resp.get("answer", default)
time.sleep(POLL_INTERVAL)
# Timeout - use default or raise
if default is not None:
self.log("warn", f"Timeout, using default: {default}", request_id=req_id)
return default
raise TimeoutError(f"No response for request {req_id}")
def confirm(self, prompt: str, default: bool = False) -> bool:
"""Ask yes/no confirmation."""
answer = self.request_input(
prompt,
request_type="confirm",
choices=["no", "yes"],
default="yes" if default else "no",
)
return answer.lower() in ("yes", "y", "true", "1")
def choose(self, prompt: str, choices: list[str], default: str = None) -> str:
"""Ask to pick from choices."""
return self.request_input(
prompt,
request_type="choice",
choices=choices,
default=default or choices[0],
)
def ask(self, prompt: str, default: str = None) -> str:
"""Ask for free-form input."""
return self.request_input(prompt, request_type="input", default=default)
def check_commands(self) -> Optional[dict]:
"""Check for new commands from controller."""
commands = _read_jsonl(COMMANDS_FILE)
for cmd in commands:
cmd_id = cmd.get("id")
if cmd_id and cmd_id not in self._processed_commands:
self._processed_commands.add(cmd_id)
return cmd
return None
# === Controller-side API ===
class ControllerProtocol:
"""Human controller-side protocol handler."""
def __init__(self):
PROTOCOL_DIR.mkdir(parents=True, exist_ok=True)
self._command_counter = 0
def _next_command_id(self) -> str:
self._command_counter += 1
return f"cmd_{self._command_counter:04d}"
def get_status(self) -> Optional[dict]:
"""Get current agent status."""
return _read_json(STATUS_FILE)
def get_pending_requests(self) -> list[dict]:
"""Get requests that haven't been answered."""
requests = _read_jsonl(REQUESTS_FILE)
responses = _read_jsonl(RESPONSES_FILE)
answered_ids = {r.get("id") for r in responses}
return [r for r in requests if r.get("id") not in answered_ids]
def respond(self, request_id: str, answer: str):
"""Send response to a request."""
_append_jsonl(RESPONSES_FILE, {
"id": request_id,
"answer": answer,
"timestamp": datetime.now().isoformat(),
})
def send_command(self, command_type: str, **params):
"""Send command to agent."""
cmd_id = self._next_command_id()
_append_jsonl(COMMANDS_FILE, {
"id": cmd_id,
"type": command_type,
"timestamp": datetime.now().isoformat(),
**params,
})
return cmd_id
def send_task(self, task: str):
"""Send a new task to the agent."""
return self.send_command("task", task=task)
def send_stop(self):
"""Tell agent to stop current task."""
return self.send_command("stop")
def get_log(self, tail: int = None) -> list[dict]:
"""Get activity log, optionally last N entries."""
logs = _read_jsonl(LOG_FILE)
if tail:
return logs[-tail:]
return logs
# === CLI helpers ===
def print_status():
"""Print current status (for shell scripts)."""
ctrl = ControllerProtocol()
status = ctrl.get_status()
if status:
print(json.dumps(status, indent=2))
else:
print("No status file found")
def print_pending():
"""Print pending requests (for shell scripts)."""
ctrl = ControllerProtocol()
pending = ctrl.get_pending_requests()
for req in pending:
print(json.dumps(req))
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
cmd = sys.argv[1]
if cmd == "status":
print_status()
elif cmd == "pending":
print_pending()
else:
print(f"Unknown command: {cmd}")
else:
print("Usage: python protocol.py [status|pending]")