hobees 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hobees/__init__.py +0 -0
- hobees/api/__init__.py +5 -0
- hobees/api/app.py +408 -0
- hobees/cli/__init__.py +0 -0
- hobees/cli/bee_cmd.py +281 -0
- hobees/cli/commands.py +64 -0
- hobees/cli/completion.py +178 -0
- hobees/cli/hive_cmd.py +710 -0
- hobees/cli/icons.py +101 -0
- hobees/cli/import_cmd.py +554 -0
- hobees/cli/main.py +631 -0
- hobees/cli/memory_cmd.py +129 -0
- hobees/cli/provider_cmd.py +591 -0
- hobees/cli/shell.py +2148 -0
- hobees/cli/skill_cmd.py +274 -0
- hobees/cli/tui.py +1888 -0
- hobees/config.py +60 -0
- hobees/core/__init__.py +0 -0
- hobees/core/context_manager.py +203 -0
- hobees/core/hive_builder.py +551 -0
- hobees/core/llm_factory.py +202 -0
- hobees/core/memory_manager.py +302 -0
- hobees/core/model_discovery.py +164 -0
- hobees/core/queen.py +236 -0
- hobees/core/reasoning_patch.py +96 -0
- hobees/core/skill_harvester.py +308 -0
- hobees/core/streaming.py +315 -0
- hobees/core/telemetry.py +225 -0
- hobees/core/tool_registry.py +272 -0
- hobees/core/tracing.py +426 -0
- hobees/db/__init__.py +0 -0
- hobees/db/analytics.py +230 -0
- hobees/db/backends.py +34 -0
- hobees/db/models.py +289 -0
- hobees/db/session.py +86 -0
- hobees-0.1.0a1.dist-info/METADATA +651 -0
- hobees-0.1.0a1.dist-info/RECORD +40 -0
- hobees-0.1.0a1.dist-info/WHEEL +4 -0
- hobees-0.1.0a1.dist-info/entry_points.txt +3 -0
- hobees-0.1.0a1.dist-info/licenses/LICENSE +202 -0
hobees/__init__.py
ADDED
|
File without changes
|
hobees/api/__init__.py
ADDED
hobees/api/app.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"""FastAPI telemetry API for hobees (Phase 6)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import asyncio
|
|
7
|
+
from importlib.metadata import version as package_version
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from fastapi import FastAPI, Query, HTTPException, Request
|
|
10
|
+
from fastapi.responses import StreamingResponse
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
from typing import Literal, Optional
|
|
13
|
+
from datetime import datetime
|
|
14
|
+
|
|
15
|
+
from hobees.db.analytics import summary, recent_events, active_runs, run_thread_usage, Window
|
|
16
|
+
from hobees.db.session import session_scope
|
|
17
|
+
from hobees.db.models import LLMProvider, Bee, Hive, Run
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HealthResponse(BaseModel):
|
|
21
|
+
status: str
|
|
22
|
+
timestamp: datetime
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class StatsResponse(BaseModel):
|
|
26
|
+
window: str
|
|
27
|
+
group_by: str
|
|
28
|
+
data: list[dict]
|
|
29
|
+
totals: dict
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class RecentEventsResponse(BaseModel):
|
|
33
|
+
events: list[dict]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ActiveRunsResponse(BaseModel):
|
|
37
|
+
runs: list[dict]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class RunThreadResponse(BaseModel):
|
|
41
|
+
thread_id: str
|
|
42
|
+
usage: dict
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ProviderRosterItem(BaseModel):
|
|
46
|
+
id: int
|
|
47
|
+
alias: str
|
|
48
|
+
provider_type: str
|
|
49
|
+
default_model: Optional[str] = None
|
|
50
|
+
available_models: list[str] = []
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class BeeRosterItem(BaseModel):
|
|
54
|
+
id: int
|
|
55
|
+
name: str
|
|
56
|
+
role_description: str
|
|
57
|
+
model_name: str
|
|
58
|
+
thinking_level: str
|
|
59
|
+
tool_names: list[str] = []
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class HiveRosterItem(BaseModel):
|
|
63
|
+
id: int
|
|
64
|
+
name: str
|
|
65
|
+
goal_description: Optional[str] = None
|
|
66
|
+
supervisor_model_name: Optional[str] = None
|
|
67
|
+
is_dynamic: bool
|
|
68
|
+
bee_names: list[str] = []
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class RosterResponse(BaseModel):
|
|
72
|
+
providers: list[ProviderRosterItem]
|
|
73
|
+
bees: list[BeeRosterItem]
|
|
74
|
+
hives: list[HiveRosterItem]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class RunItem(BaseModel):
|
|
78
|
+
id: int
|
|
79
|
+
thread_id: str
|
|
80
|
+
hive_id: Optional[int] = None
|
|
81
|
+
mode: str
|
|
82
|
+
goal: Optional[str] = None
|
|
83
|
+
status: str
|
|
84
|
+
started_at: Optional[datetime] = None
|
|
85
|
+
completed_at: Optional[datetime] = None
|
|
86
|
+
compacted_count: int = 0
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class RunsResponse(BaseModel):
|
|
90
|
+
runs: list[RunItem]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def create_app() -> FastAPI:
|
|
94
|
+
"""Create the FastAPI application."""
|
|
95
|
+
app = FastAPI(
|
|
96
|
+
title="hobees Telemetry API",
|
|
97
|
+
description="Usage insights and telemetry for hobees multi-agent orchestrator",
|
|
98
|
+
version=package_version("hobees"),
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
@app.get("/health", response_model=HealthResponse)
|
|
102
|
+
@app.get("/api/health", response_model=HealthResponse)
|
|
103
|
+
async def health() -> HealthResponse:
|
|
104
|
+
"""Health check endpoint."""
|
|
105
|
+
return HealthResponse(status="ok", timestamp=datetime.now())
|
|
106
|
+
|
|
107
|
+
@app.get("/stats", response_model=StatsResponse)
|
|
108
|
+
@app.get("/api/stats", response_model=StatsResponse)
|
|
109
|
+
async def stats(
|
|
110
|
+
window: Window = Query("all", description="Time window: 1h, 24h, 7d, all"),
|
|
111
|
+
group_by: Literal["bee", "hive", "provider", "day"] = Query("bee", description="Group by"),
|
|
112
|
+
) -> StatsResponse:
|
|
113
|
+
"""Get aggregated usage statistics."""
|
|
114
|
+
data = summary(window=window, group_by=group_by)
|
|
115
|
+
|
|
116
|
+
total_events = sum(row.get("events", 0) for row in data)
|
|
117
|
+
total_in = sum(row.get("input_tokens", 0) for row in data)
|
|
118
|
+
total_out = sum(row.get("output_tokens", 0) for row in data)
|
|
119
|
+
total_cost = sum(row.get("estimated_cost_usd", 0.0) for row in data)
|
|
120
|
+
|
|
121
|
+
return StatsResponse(
|
|
122
|
+
window=window,
|
|
123
|
+
group_by=group_by,
|
|
124
|
+
data=data,
|
|
125
|
+
totals={
|
|
126
|
+
"events": total_events,
|
|
127
|
+
"input_tokens": total_in,
|
|
128
|
+
"output_tokens": total_out,
|
|
129
|
+
"total_tokens": total_in + total_out,
|
|
130
|
+
"estimated_cost_usd": total_cost,
|
|
131
|
+
},
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
@app.get("/stats/recent", response_model=RecentEventsResponse)
|
|
135
|
+
@app.get("/api/stats/recent", response_model=RecentEventsResponse)
|
|
136
|
+
async def stats_recent(limit: int = Query(20, ge=1, le=100)) -> RecentEventsResponse:
|
|
137
|
+
"""Get most recent usage events."""
|
|
138
|
+
events = recent_events(limit=limit)
|
|
139
|
+
return RecentEventsResponse(events=events)
|
|
140
|
+
|
|
141
|
+
@app.get("/runs/active", response_model=ActiveRunsResponse)
|
|
142
|
+
@app.get("/api/runs/active", response_model=ActiveRunsResponse)
|
|
143
|
+
async def runs_active() -> ActiveRunsResponse:
|
|
144
|
+
"""Get currently running runs."""
|
|
145
|
+
runs = active_runs()
|
|
146
|
+
return ActiveRunsResponse(runs=runs)
|
|
147
|
+
|
|
148
|
+
@app.get("/runs/{thread_id}", response_model=RunThreadResponse)
|
|
149
|
+
@app.get("/api/runs/{thread_id}", response_model=RunThreadResponse)
|
|
150
|
+
async def run_thread(thread_id: str) -> RunThreadResponse:
|
|
151
|
+
"""Get telemetry for a specific thread."""
|
|
152
|
+
usage = run_thread_usage(thread_id)
|
|
153
|
+
if not usage:
|
|
154
|
+
raise HTTPException(status_code=404, detail=f"Thread {thread_id} not found")
|
|
155
|
+
return RunThreadResponse(thread_id=thread_id, usage=usage)
|
|
156
|
+
|
|
157
|
+
@app.get("/roster", response_model=RosterResponse)
|
|
158
|
+
@app.get("/api/roster", response_model=RosterResponse)
|
|
159
|
+
async def roster() -> RosterResponse:
|
|
160
|
+
"""Get the global roster of providers, bees, and hives."""
|
|
161
|
+
with session_scope() as session:
|
|
162
|
+
providers = session.query(LLMProvider).all()
|
|
163
|
+
bees = session.query(Bee).all()
|
|
164
|
+
hives = session.query(Hive).all()
|
|
165
|
+
|
|
166
|
+
prov_items = [
|
|
167
|
+
ProviderRosterItem(
|
|
168
|
+
id=p.id,
|
|
169
|
+
alias=p.alias,
|
|
170
|
+
provider_type=p.provider_type,
|
|
171
|
+
default_model=p.default_model,
|
|
172
|
+
available_models=p.available_models or [],
|
|
173
|
+
)
|
|
174
|
+
for p in providers
|
|
175
|
+
]
|
|
176
|
+
bee_items = [
|
|
177
|
+
BeeRosterItem(
|
|
178
|
+
id=b.id,
|
|
179
|
+
name=b.name,
|
|
180
|
+
role_description=b.role_description,
|
|
181
|
+
model_name=b.model_name,
|
|
182
|
+
thinking_level=b.thinking_level or "off",
|
|
183
|
+
tool_names=b.tool_names or [],
|
|
184
|
+
)
|
|
185
|
+
for b in bees
|
|
186
|
+
]
|
|
187
|
+
hive_items = [
|
|
188
|
+
HiveRosterItem(
|
|
189
|
+
id=h.id,
|
|
190
|
+
name=h.name,
|
|
191
|
+
goal_description=h.goal_description,
|
|
192
|
+
supervisor_model_name=h.supervisor_model_name,
|
|
193
|
+
is_dynamic=h.is_dynamic,
|
|
194
|
+
bee_names=[m.bee.name for m in h.memberships if m.bee] if h.memberships else [],
|
|
195
|
+
)
|
|
196
|
+
for h in hives
|
|
197
|
+
]
|
|
198
|
+
return RosterResponse(providers=prov_items, bees=bee_items, hives=hive_items)
|
|
199
|
+
|
|
200
|
+
@app.get("/runs", response_model=RunsResponse)
|
|
201
|
+
@app.get("/api/runs", response_model=RunsResponse)
|
|
202
|
+
async def list_runs(
|
|
203
|
+
status: Optional[str] = Query(None, description="Filter by status (e.g. running, completed, failed)"),
|
|
204
|
+
limit: int = Query(50, ge=1, le=200, description="Max runs to return"),
|
|
205
|
+
) -> RunsResponse:
|
|
206
|
+
"""Get historical runs."""
|
|
207
|
+
with session_scope() as session:
|
|
208
|
+
q = session.query(Run)
|
|
209
|
+
if status:
|
|
210
|
+
q = q.filter_by(status=status)
|
|
211
|
+
runs = q.order_by(Run.started_at.desc()).limit(limit).all()
|
|
212
|
+
return RunsResponse(
|
|
213
|
+
runs=[
|
|
214
|
+
RunItem(
|
|
215
|
+
id=r.id,
|
|
216
|
+
thread_id=r.thread_id,
|
|
217
|
+
hive_id=r.hive_id,
|
|
218
|
+
mode=r.mode,
|
|
219
|
+
goal=r.goal,
|
|
220
|
+
status=r.status,
|
|
221
|
+
started_at=r.started_at,
|
|
222
|
+
completed_at=r.completed_at,
|
|
223
|
+
compacted_count=r.compacted_count or 0,
|
|
224
|
+
)
|
|
225
|
+
for r in runs
|
|
226
|
+
]
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
@app.get("/usage/stream")
|
|
230
|
+
@app.get("/api/usage/stream")
|
|
231
|
+
async def usage_stream(
|
|
232
|
+
limit: int = Query(20, ge=1, le=100, description="Initial events to yield"),
|
|
233
|
+
poll_interval: float = Query(2.0, ge=0.2, le=10.0, description="Poll interval seconds"),
|
|
234
|
+
iterations: int = Query(1, ge=1, le=100, description="Stream iterations"),
|
|
235
|
+
):
|
|
236
|
+
"""Server-Sent Events (SSE) stream of usage telemetry."""
|
|
237
|
+
async def event_generator():
|
|
238
|
+
for _ in range(iterations):
|
|
239
|
+
events = recent_events(limit=limit)
|
|
240
|
+
serialized = []
|
|
241
|
+
for ev in events:
|
|
242
|
+
ev_copy = dict(ev)
|
|
243
|
+
if "created_at" in ev_copy and isinstance(ev_copy["created_at"], datetime):
|
|
244
|
+
ev_copy["created_at"] = ev_copy["created_at"].isoformat()
|
|
245
|
+
serialized.append(ev_copy)
|
|
246
|
+
data = json.dumps({"events": serialized, "timestamp": datetime.now().isoformat()})
|
|
247
|
+
yield f"data: {data}\n\n"
|
|
248
|
+
if iterations > 1:
|
|
249
|
+
await asyncio.sleep(poll_interval)
|
|
250
|
+
|
|
251
|
+
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
|
252
|
+
|
|
253
|
+
@app.get("/runs/{thread_id}/stream")
|
|
254
|
+
@app.get("/api/runs/{thread_id}/stream")
|
|
255
|
+
async def run_trace_stream(
|
|
256
|
+
thread_id: str,
|
|
257
|
+
request: Request,
|
|
258
|
+
timeout: float | None = Query(
|
|
259
|
+
None,
|
|
260
|
+
ge=0.1,
|
|
261
|
+
description="Optional maximum idle seconds before ending the live stream",
|
|
262
|
+
),
|
|
263
|
+
):
|
|
264
|
+
"""Server-Sent Events (SSE) stream of live and historical Hive trace events."""
|
|
265
|
+
from hobees.core.tracing import EventBroker
|
|
266
|
+
from hobees.config import settings
|
|
267
|
+
|
|
268
|
+
broker = EventBroker.get_instance()
|
|
269
|
+
q = broker.subscribe()
|
|
270
|
+
with session_scope() as session:
|
|
271
|
+
run = session.query(Run).filter_by(thread_id=thread_id).first()
|
|
272
|
+
run_status = run.status if run else None
|
|
273
|
+
artifacts_dir = run.artifacts_dir if run else None
|
|
274
|
+
hive = session.get(Hive, run.hive_id) if run and run.hive_id else None
|
|
275
|
+
hive_name = hive.name if hive else None
|
|
276
|
+
project_path = hive.project_path if hive else None
|
|
277
|
+
|
|
278
|
+
async def trace_generator():
|
|
279
|
+
try:
|
|
280
|
+
trace_paths = []
|
|
281
|
+
if artifacts_dir:
|
|
282
|
+
trace_paths.append(Path(artifacts_dir) / "traces" / f"{thread_id}.jsonl")
|
|
283
|
+
if project_path:
|
|
284
|
+
trace_paths.append(Path(project_path) / "traces" / f"{thread_id}.jsonl")
|
|
285
|
+
if hive_name:
|
|
286
|
+
trace_paths.append(
|
|
287
|
+
settings.artifacts_root / hive_name / "traces" / f"{thread_id}.jsonl"
|
|
288
|
+
)
|
|
289
|
+
else:
|
|
290
|
+
trace_paths.extend(
|
|
291
|
+
settings.artifacts_root.glob(f"*/traces/{thread_id}.jsonl")
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
trace_paths = list(dict.fromkeys(trace_paths))
|
|
295
|
+
file_offsets = {}
|
|
296
|
+
seen_events = set()
|
|
297
|
+
terminal_seen = False
|
|
298
|
+
for trace_file in trace_paths:
|
|
299
|
+
if not trace_file.is_file():
|
|
300
|
+
continue
|
|
301
|
+
lines, file_offsets[trace_file] = _read_complete_trace_lines(
|
|
302
|
+
trace_file, 0
|
|
303
|
+
)
|
|
304
|
+
for line in lines:
|
|
305
|
+
serialized = line.decode("utf-8").strip()
|
|
306
|
+
if not serialized or serialized in seen_events:
|
|
307
|
+
continue
|
|
308
|
+
seen_events.add(serialized)
|
|
309
|
+
event = json.loads(serialized)
|
|
310
|
+
terminal_seen |= event.get("event_type") in (
|
|
311
|
+
"HIVE_COMPLETE",
|
|
312
|
+
"HIVE_ERROR",
|
|
313
|
+
)
|
|
314
|
+
yield f"data: {serialized}\n\n"
|
|
315
|
+
|
|
316
|
+
if (
|
|
317
|
+
(terminal_seen and run_status != "running")
|
|
318
|
+
or run_status in ("completed", "failed")
|
|
319
|
+
):
|
|
320
|
+
return
|
|
321
|
+
|
|
322
|
+
idle_since = asyncio.get_running_loop().time()
|
|
323
|
+
last_keep_alive = idle_since
|
|
324
|
+
last_status_check = idle_since
|
|
325
|
+
while True:
|
|
326
|
+
if await request.is_disconnected():
|
|
327
|
+
return
|
|
328
|
+
now = asyncio.get_running_loop().time()
|
|
329
|
+
wait_time = 0.25
|
|
330
|
+
if timeout is not None:
|
|
331
|
+
remaining = timeout - (now - idle_since)
|
|
332
|
+
if remaining <= 0:
|
|
333
|
+
return
|
|
334
|
+
wait_time = min(wait_time, remaining)
|
|
335
|
+
try:
|
|
336
|
+
event = await asyncio.wait_for(q.get(), timeout=wait_time)
|
|
337
|
+
except asyncio.TimeoutError:
|
|
338
|
+
event = None
|
|
339
|
+
if event is not None and event.get("thread_id") == thread_id:
|
|
340
|
+
serialized = json.dumps(event)
|
|
341
|
+
if serialized not in seen_events:
|
|
342
|
+
seen_events.add(serialized)
|
|
343
|
+
yield f"data: {serialized}\n\n"
|
|
344
|
+
idle_since = asyncio.get_running_loop().time()
|
|
345
|
+
if event.get("event_type") in ("HIVE_COMPLETE", "HIVE_ERROR"):
|
|
346
|
+
return
|
|
347
|
+
|
|
348
|
+
for trace_file in trace_paths:
|
|
349
|
+
lines, file_offsets[trace_file] = _read_complete_trace_lines(
|
|
350
|
+
trace_file, file_offsets.get(trace_file, 0)
|
|
351
|
+
)
|
|
352
|
+
for line in lines:
|
|
353
|
+
serialized = line.decode("utf-8").strip()
|
|
354
|
+
if not serialized or serialized in seen_events:
|
|
355
|
+
continue
|
|
356
|
+
seen_events.add(serialized)
|
|
357
|
+
event = json.loads(serialized)
|
|
358
|
+
yield f"data: {serialized}\n\n"
|
|
359
|
+
idle_since = asyncio.get_running_loop().time()
|
|
360
|
+
if event.get("event_type") in (
|
|
361
|
+
"HIVE_COMPLETE",
|
|
362
|
+
"HIVE_ERROR",
|
|
363
|
+
):
|
|
364
|
+
return
|
|
365
|
+
now = asyncio.get_running_loop().time()
|
|
366
|
+
if run_status == "running" and now - last_status_check >= 1:
|
|
367
|
+
last_status_check = now
|
|
368
|
+
with session_scope() as session:
|
|
369
|
+
current_run = session.query(Run).filter_by(
|
|
370
|
+
thread_id=thread_id
|
|
371
|
+
).first()
|
|
372
|
+
if current_run and current_run.status in (
|
|
373
|
+
"completed",
|
|
374
|
+
"failed",
|
|
375
|
+
):
|
|
376
|
+
return
|
|
377
|
+
if now - last_keep_alive >= 15:
|
|
378
|
+
yield ": keep-alive\n\n"
|
|
379
|
+
last_keep_alive = now
|
|
380
|
+
finally:
|
|
381
|
+
broker.unsubscribe(q)
|
|
382
|
+
|
|
383
|
+
return StreamingResponse(trace_generator(), media_type="text/event-stream")
|
|
384
|
+
|
|
385
|
+
return app
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def _read_complete_trace_lines(path: Path, offset: int) -> tuple[list[bytes], int]:
|
|
389
|
+
"""Read only complete JSONL records, leaving a concurrent partial write for later."""
|
|
390
|
+
try:
|
|
391
|
+
with path.open("rb") as trace:
|
|
392
|
+
trace.seek(offset)
|
|
393
|
+
data = trace.read()
|
|
394
|
+
except FileNotFoundError:
|
|
395
|
+
return [], offset
|
|
396
|
+
|
|
397
|
+
lines = data.splitlines(keepends=True)
|
|
398
|
+
complete_lines = []
|
|
399
|
+
consumed = 0
|
|
400
|
+
for line in lines:
|
|
401
|
+
if not line.endswith(b"\n"):
|
|
402
|
+
break
|
|
403
|
+
complete_lines.append(line)
|
|
404
|
+
consumed += len(line)
|
|
405
|
+
return complete_lines, offset + consumed
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
__all__ = ["create_app"]
|
hobees/cli/__init__.py
ADDED
|
File without changes
|
hobees/cli/bee_cmd.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"""Worker Bee handlers — shared by the Typer CLI (`hobees bee ...`) and the
|
|
2
|
+
interactive shell slash commands (`/bee ...`).
|
|
3
|
+
|
|
4
|
+
Bees exist independently of Hives and can belong to zero, one, or many Hives.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Annotated, List, Optional
|
|
10
|
+
|
|
11
|
+
import typer
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.prompt import Prompt
|
|
14
|
+
from rich.table import Table
|
|
15
|
+
|
|
16
|
+
from hobees.cli.icons import BEE, ERROR, SUCCESS, WARN
|
|
17
|
+
from hobees.cli.provider_cmd import interactive_model_picker
|
|
18
|
+
|
|
19
|
+
console = Console()
|
|
20
|
+
|
|
21
|
+
app = typer.Typer(name="bee", help="Manage reusable Worker Bees")
|
|
22
|
+
|
|
23
|
+
# ---------------------------------------------------------------------------
|
|
24
|
+
# handlers (shared with shell.py)
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
def _parse_tool_names(raw: str | None) -> list[str]:
|
|
28
|
+
if not raw:
|
|
29
|
+
return []
|
|
30
|
+
raw = raw.strip()
|
|
31
|
+
if raw.startswith("["):
|
|
32
|
+
import json
|
|
33
|
+
|
|
34
|
+
return json.loads(raw)
|
|
35
|
+
return [t.strip() for t in raw.split(",") if t.strip()]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def cmd_bee_create(
|
|
39
|
+
name: str,
|
|
40
|
+
role_description: str,
|
|
41
|
+
system_prompt: str,
|
|
42
|
+
provider_id: int,
|
|
43
|
+
model_name: str,
|
|
44
|
+
temperature: float = 0.0,
|
|
45
|
+
tool_names: str = "",
|
|
46
|
+
thinking_level: str = "off",
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Create a new Worker Bee."""
|
|
49
|
+
from hobees.db.session import session_scope
|
|
50
|
+
from hobees.db.models import Bee, LLMProvider
|
|
51
|
+
from hobees.core.model_discovery import fetch_provider_models
|
|
52
|
+
|
|
53
|
+
tools = _parse_tool_names(tool_names)
|
|
54
|
+
with session_scope() as session:
|
|
55
|
+
provider = session.get(LLMProvider, provider_id)
|
|
56
|
+
if not provider:
|
|
57
|
+
console.print(f"{ERROR} provider id {provider_id} not found")
|
|
58
|
+
raise typer.Exit(code=1)
|
|
59
|
+
if session.query(Bee).filter_by(name=name).first():
|
|
60
|
+
console.print(f"{ERROR} Bee {name!r} already exists")
|
|
61
|
+
raise typer.Exit(code=1)
|
|
62
|
+
|
|
63
|
+
# Interactive model picker if available
|
|
64
|
+
if provider and model_name:
|
|
65
|
+
models = fetch_provider_models(provider, refresh_cache=False)
|
|
66
|
+
if models:
|
|
67
|
+
selected = interactive_model_picker(models, default=model_name)
|
|
68
|
+
if selected is not None:
|
|
69
|
+
model_name = selected
|
|
70
|
+
|
|
71
|
+
bee = Bee(
|
|
72
|
+
name=name,
|
|
73
|
+
role_description=role_description,
|
|
74
|
+
system_prompt=system_prompt,
|
|
75
|
+
provider_id=provider_id,
|
|
76
|
+
model_name=model_name,
|
|
77
|
+
temperature=temperature,
|
|
78
|
+
thinking_level=thinking_level,
|
|
79
|
+
tool_names=tools,
|
|
80
|
+
)
|
|
81
|
+
session.add(bee)
|
|
82
|
+
session.flush()
|
|
83
|
+
bee_id = bee.id
|
|
84
|
+
console.print(
|
|
85
|
+
f"{SUCCESS} Bee {name!r} created (id={bee_id}, model={model_name}, "
|
|
86
|
+
f"thinking={thinking_level}, tools={tools or '-'})"
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def cmd_bee_list() -> None:
|
|
91
|
+
"""List all Worker Bees with their provider and model."""
|
|
92
|
+
from hobees.db.session import session_scope
|
|
93
|
+
from hobees.db.models import Bee, LLMProvider
|
|
94
|
+
|
|
95
|
+
with session_scope() as session:
|
|
96
|
+
rows = (
|
|
97
|
+
session.query(Bee)
|
|
98
|
+
.join(LLMProvider, Bee.provider_id == LLMProvider.id)
|
|
99
|
+
.order_by(Bee.name)
|
|
100
|
+
.all()
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
table = Table(title="Worker Bees")
|
|
104
|
+
table.add_column("name", style="cyan")
|
|
105
|
+
table.add_column("provider", style="green")
|
|
106
|
+
table.add_column("model")
|
|
107
|
+
table.add_column("temp")
|
|
108
|
+
table.add_column("thinking")
|
|
109
|
+
table.add_column("tools")
|
|
110
|
+
for b in rows:
|
|
111
|
+
table.add_row(
|
|
112
|
+
b.name,
|
|
113
|
+
b.provider.alias,
|
|
114
|
+
b.model_name,
|
|
115
|
+
str(b.temperature),
|
|
116
|
+
b.thinking_level,
|
|
117
|
+
", ".join(b.tool_names) or "-",
|
|
118
|
+
)
|
|
119
|
+
if not rows:
|
|
120
|
+
console.print(f"{WARN} No Bees registered yet. Use /bee create.")
|
|
121
|
+
else:
|
|
122
|
+
console.print(table)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def cmd_bee_update(
|
|
126
|
+
name: str,
|
|
127
|
+
provider_id: int | None = None,
|
|
128
|
+
model_name: str | None = None,
|
|
129
|
+
temperature: float | None = None,
|
|
130
|
+
thinking_level: str | None = None,
|
|
131
|
+
system_prompt: str | None = None,
|
|
132
|
+
role_description: str | None = None,
|
|
133
|
+
tool_names: str = "",
|
|
134
|
+
) -> None:
|
|
135
|
+
"""Hot-swap provider, model, temperature, tools, or prompts mid-session."""
|
|
136
|
+
from hobees.db.session import session_scope
|
|
137
|
+
from hobees.db.models import Bee, LLMProvider
|
|
138
|
+
from hobees.core.model_discovery import fetch_provider_models
|
|
139
|
+
|
|
140
|
+
valid_thinking = ("off", "low", "medium", "high")
|
|
141
|
+
if thinking_level is not None and thinking_level not in valid_thinking:
|
|
142
|
+
console.print(f"{ERROR} thinking level must be one of {valid_thinking}")
|
|
143
|
+
raise typer.Exit(code=1)
|
|
144
|
+
|
|
145
|
+
with session_scope() as session:
|
|
146
|
+
bee = session.query(Bee).filter_by(name=name).first()
|
|
147
|
+
if not bee:
|
|
148
|
+
console.print(f"{ERROR} Bee {name!r} not found")
|
|
149
|
+
raise typer.Exit(code=1)
|
|
150
|
+
if provider_id is not None:
|
|
151
|
+
provider = session.get(LLMProvider, provider_id)
|
|
152
|
+
if not provider:
|
|
153
|
+
console.print(f"{ERROR} provider id {provider_id} not found")
|
|
154
|
+
raise typer.Exit(code=1)
|
|
155
|
+
bee.provider_id = provider_id
|
|
156
|
+
provider = provider
|
|
157
|
+
else:
|
|
158
|
+
provider = session.get(LLMProvider, bee.provider_id)
|
|
159
|
+
if model_name is not None:
|
|
160
|
+
bee.model_name = model_name
|
|
161
|
+
if temperature is not None:
|
|
162
|
+
bee.temperature = temperature
|
|
163
|
+
if thinking_level is not None:
|
|
164
|
+
bee.thinking_level = thinking_level
|
|
165
|
+
if system_prompt is not None:
|
|
166
|
+
bee.system_prompt = system_prompt
|
|
167
|
+
if role_description is not None:
|
|
168
|
+
bee.role_description = role_description
|
|
169
|
+
if tool_names != "":
|
|
170
|
+
bee.tool_names = _parse_tool_names(tool_names)
|
|
171
|
+
|
|
172
|
+
# Interactive model picker if model changed and provider has models
|
|
173
|
+
if model_name is not None and provider:
|
|
174
|
+
models = fetch_provider_models(provider, refresh_cache=False)
|
|
175
|
+
if models:
|
|
176
|
+
selected = interactive_model_picker(models, default=model_name)
|
|
177
|
+
if selected is not None:
|
|
178
|
+
bee.model_name = selected
|
|
179
|
+
console.print(f"[dim]Model updated to: {selected}[/dim]")
|
|
180
|
+
|
|
181
|
+
console.print(f"{SUCCESS} Bee {name!r} updated.")
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def cmd_bee_delete(name: str) -> None:
|
|
185
|
+
"""Delete a Bee (also removes its Hive memberships)."""
|
|
186
|
+
from hobees.db.session import session_scope
|
|
187
|
+
from hobees.db.models import Bee, HiveMembership
|
|
188
|
+
|
|
189
|
+
with session_scope() as session:
|
|
190
|
+
bee = session.query(Bee).filter_by(name=name).first()
|
|
191
|
+
if not bee:
|
|
192
|
+
console.print(f"{ERROR} Bee {name!r} not found")
|
|
193
|
+
raise typer.Exit(code=1)
|
|
194
|
+
session.query(HiveMembership).filter_by(bee_id=bee.id).delete()
|
|
195
|
+
session.delete(bee)
|
|
196
|
+
console.print(f"{SUCCESS} Bee {name!r} deleted.")
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def cmd_bee_thinking(name: str, level: str) -> None:
|
|
200
|
+
"""Set a Bee's default thinking level."""
|
|
201
|
+
from hobees.db.session import session_scope
|
|
202
|
+
from hobees.db.models import Bee
|
|
203
|
+
|
|
204
|
+
valid_levels = ("off", "low", "medium", "high")
|
|
205
|
+
if level not in valid_levels:
|
|
206
|
+
console.print(f"{ERROR} thinking level must be one of {valid_levels}")
|
|
207
|
+
raise typer.Exit(code=1)
|
|
208
|
+
|
|
209
|
+
with session_scope() as session:
|
|
210
|
+
bee = session.query(Bee).filter_by(name=name).first()
|
|
211
|
+
if not bee:
|
|
212
|
+
console.print(f"{ERROR} Bee {name!r} not found")
|
|
213
|
+
raise typer.Exit(code=1)
|
|
214
|
+
bee.thinking_level = level
|
|
215
|
+
console.print(f"{SUCCESS} Bee {name!r} thinking level set to '{level}'")
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
# ---------------------------------------------------------------------------
|
|
219
|
+
# Typer CLI commands
|
|
220
|
+
# ---------------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
@app.command(name="create")
|
|
224
|
+
def cli_create(
|
|
225
|
+
name: Annotated[str, typer.Argument(help="Unique Bee name")],
|
|
226
|
+
role_description: Annotated[str, typer.Argument(help="Role read by Supervisors/Queen for routing")],
|
|
227
|
+
system_prompt: Annotated[str, typer.Argument(help="System prompt for the Bee")],
|
|
228
|
+
provider_id: Annotated[int, typer.Argument(help="Registered LLM provider id")],
|
|
229
|
+
model_name: Annotated[str, typer.Argument(help="Model name")],
|
|
230
|
+
temperature: Annotated[float, typer.Option("--temperature", "-t")] = 0.0,
|
|
231
|
+
tool_names: Annotated[
|
|
232
|
+
str, typer.Option("--tool-names", help="Comma-separated or JSON list of tool keys")
|
|
233
|
+
] = "",
|
|
234
|
+
thinking_level: Annotated[str, typer.Option("--thinking", help="off | low | medium | high")] = "off",
|
|
235
|
+
) -> None:
|
|
236
|
+
cmd_bee_create(name, role_description, system_prompt, provider_id, model_name, temperature, tool_names, thinking_level)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@app.command(name="list")
|
|
240
|
+
def cli_list() -> None:
|
|
241
|
+
cmd_bee_list()
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
@app.command(name="update")
|
|
245
|
+
def cli_update(
|
|
246
|
+
name: Annotated[str, typer.Argument(help="Bee name")],
|
|
247
|
+
provider_id: Annotated[int | None, typer.Option("--provider-id")] = None,
|
|
248
|
+
model_name: Annotated[str | None, typer.Option("--model-name")] = None,
|
|
249
|
+
temperature: Annotated[float | None, typer.Option("--temperature", "-t")] = None,
|
|
250
|
+
thinking_level: Annotated[str | None, typer.Option("--thinking", help="off | low | medium | high")] = None,
|
|
251
|
+
system_prompt: Annotated[str | None, typer.Option("--system-prompt")] = None,
|
|
252
|
+
role_description: Annotated[str | None, typer.Option("--role-description")] = None,
|
|
253
|
+
tool_names: Annotated[str, typer.Option("--tool-names")] = "",
|
|
254
|
+
) -> None:
|
|
255
|
+
cmd_bee_update(
|
|
256
|
+
name, provider_id, model_name, temperature, thinking_level, system_prompt, role_description, tool_names
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
@app.command(name="delete")
|
|
261
|
+
def cli_delete(name: Annotated[str, typer.Argument(help="Bee name")]) -> None:
|
|
262
|
+
cmd_bee_delete(name)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@app.command(name="thinking")
|
|
266
|
+
def cli_thinking(
|
|
267
|
+
name: Annotated[str, typer.Argument(help="Bee name")],
|
|
268
|
+
level: Annotated[str, typer.Argument(help="off | low | medium | high")],
|
|
269
|
+
) -> None:
|
|
270
|
+
cmd_bee_thinking(name, level)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
__all__ = [
|
|
274
|
+
"app",
|
|
275
|
+
"cmd_bee_create",
|
|
276
|
+
"cmd_bee_list",
|
|
277
|
+
"cmd_bee_update",
|
|
278
|
+
"cmd_bee_delete",
|
|
279
|
+
"cmd_bee_thinking",
|
|
280
|
+
"_parse_tool_names",
|
|
281
|
+
]
|