Spaces:
Runtime error

SkyWorlds01 commited on
Commit
d52ec6b
·
verified ·
1 Parent(s): d083af3

Create Shelly/overseer.py

Browse files
Files changed (1) hide show
  1. Shelly/overseer.py +199 -0
Shelly/overseer.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Shelly AI - Overseer
3
+
4
+ Coordinates worker agents, monitors task progress,
5
+ and evaluates task outcomes.
6
+
7
+ The Overseer is limited to registered internal capabilities.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import threading
13
+ import time
14
+
15
+ from typing import Any, Dict, List, Optional
16
+
17
+ from .agent import AgentSystem
18
+
19
+
20
+ class Overseer:
21
+ def __init__(
22
+ self,
23
+ agent_system: Optional[AgentSystem] = None,
24
+ ):
25
+ self._lock = threading.RLock()
26
+
27
+ self.agent_system = agent_system or AgentSystem()
28
+
29
+ self.reports: List[Dict[str, Any]] = []
30
+
31
+ self.maximum_reports = 1000
32
+
33
+ self.running = False
34
+ self.last_cycle = None
35
+
36
+ def create_worker(
37
+ self,
38
+ name: str,
39
+ description: str,
40
+ capabilities: List[str],
41
+ ) -> Dict[str, Any]:
42
+ return self.agent_system.register_agent(
43
+ name=name,
44
+ description=description,
45
+ capabilities=capabilities,
46
+ )
47
+
48
+ def submit_task(
49
+ self,
50
+ title: str,
51
+ description: str,
52
+ capability: str,
53
+ payload: Optional[Dict[str, Any]] = None,
54
+ priority: float = 0.5,
55
+ ) -> Dict[str, Any]:
56
+ return self.agent_system.create_task(
57
+ title=title,
58
+ description=description,
59
+ capability=capability,
60
+ payload=payload,
61
+ priority=priority,
62
+ )
63
+
64
+ def assign_pending_tasks(self) -> Dict[str, int]:
65
+ pending = self.agent_system.list_tasks(
66
+ status="pending"
67
+ )
68
+
69
+ agents = self.agent_system.list_agents()
70
+
71
+ assigned = 0
72
+ unmatched = 0
73
+
74
+ for task in sorted(
75
+ pending,
76
+ key=lambda item: item["priority"],
77
+ reverse=True,
78
+ ):
79
+ suitable = next(
80
+ (
81
+ agent
82
+ for agent in agents
83
+ if (
84
+ agent["status"] == "idle"
85
+ and task["capability"] in agent["capabilities"]
86
+ )
87
+ ),
88
+ None,
89
+ )
90
+
91
+ if suitable is None:
92
+ unmatched += 1
93
+ continue
94
+
95
+ if self.agent_system.assign_task(
96
+ task["id"],
97
+ suitable["id"],
98
+ ):
99
+ assigned += 1
100
+
101
+ suitable["status"] = "working"
102
+
103
+ return {
104
+ "assigned": assigned,
105
+ "unmatched": unmatched,
106
+ }
107
+
108
+ def execute_assigned_tasks(self) -> List[Dict[str, Any]]:
109
+ assigned_tasks = [
110
+ task
111
+ for task in self.agent_system.list_tasks()
112
+ if task["status"] == "assigned"
113
+ ]
114
+
115
+ results = []
116
+
117
+ for task in assigned_tasks:
118
+ result = self.agent_system.run_task(task["id"])
119
+ results.append(result)
120
+
121
+ return results
122
+
123
+ def evaluate_result(
124
+ self,
125
+ result: Dict[str, Any],
126
+ ) -> Dict[str, Any]:
127
+ task = result.get("task", {})
128
+
129
+ successful = bool(result.get("success"))
130
+
131
+ report = {
132
+ "task_id": task.get("id"),
133
+ "task_title": task.get("title"),
134
+ "successful": successful,
135
+ "evaluation": (
136
+ "Task completed successfully."
137
+ if successful
138
+ else "Task failed or could not be completed."
139
+ ),
140
+ "timestamp": time.time(),
141
+ }
142
+
143
+ with self._lock:
144
+ self.reports.append(report)
145
+
146
+ if len(self.reports) > self.maximum_reports:
147
+ self.reports = self.reports[-self.maximum_reports:]
148
+
149
+ return dict(report)
150
+
151
+ def run_cycle(self) -> Dict[str, Any]:
152
+ with self._lock:
153
+ self.running = True
154
+
155
+ assignment = self.assign_pending_tasks()
156
+ results = self.execute_assigned_tasks()
157
+
158
+ evaluations = [
159
+ self.evaluate_result(result)
160
+ for result in results
161
+ ]
162
+
163
+ cycle = {
164
+ "assignment": assignment,
165
+ "executed": len(results),
166
+ "successful": sum(
167
+ item["successful"]
168
+ for item in evaluations
169
+ ),
170
+ "failed": sum(
171
+ not item["successful"]
172
+ for item in evaluations
173
+ ),
174
+ "evaluations": evaluations,
175
+ "timestamp": time.time(),
176
+ }
177
+
178
+ with self._lock:
179
+ self.last_cycle = cycle
180
+ self.running = False
181
+
182
+ return cycle
183
+
184
+ def get_reports(self, limit: int = 50) -> List[Dict[str, Any]]:
185
+ with self._lock:
186
+ return [
187
+ dict(item)
188
+ for item in self.reports[-max(1, int(limit)):]
189
+ ]
190
+
191
+ def get_status(self) -> Dict[str, Any]:
192
+ with self._lock:
193
+ return {
194
+ "running": self.running,
195
+ "last_cycle": self.last_cycle,
196
+ "agent_system": self.agent_system.get_status(),
197
+ "report_count": len(self.reports),
198
+ }
199
+