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
use axum::{
extract::{Path, State},
http::StatusCode,
Json,
};
use serde::{Deserialize, Serialize};
use crate::AppState;
// --- Health endpoints ---
#[derive(Serialize)]
pub struct HealthResponse {
pub status: String,
pub agents: usize,
}
pub async fn health(State(state): State<AppState>) -> Json<HealthResponse> {
let daemon = state.read().await;
Json(HealthResponse {
status: "healthy".to_string(),
agents: daemon.agent_count().await,
})
}
pub async fn health_ready(State(state): State<AppState>) -> StatusCode {
let daemon = state.read().await;
if daemon.agent_count().await > 0 {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
}
}
pub async fn health_live() -> StatusCode {
StatusCode::OK
}
// --- Agent endpoints ---
#[derive(Serialize)]
pub struct AgentInfo {
pub id: String,
pub name: String,
pub provider: String,
pub model: String,
}
pub async fn list_agents(State(state): State<AppState>) -> Json<Vec<AgentInfo>> {
let daemon = state.read().await;
let agents: Vec<AgentInfo> = daemon
.config
.agents
.iter()
.map(|a| AgentInfo {
id: a.id.clone(),
name: a.name.clone(),
provider: a.provider.clone(),
model: a.model.clone(),
})
.collect();
Json(agents)
}
#[derive(Deserialize)]
pub struct ExecuteRequest {
pub input: String,
}
#[derive(Serialize)]
pub struct ExecuteResponse {
pub output: String,
}
#[derive(Serialize)]
pub struct ErrorResponse {
pub error: String,
}
pub async fn execute_agent(
State(state): State<AppState>,
Path(agent_id): Path<String>,
Json(body): Json<ExecuteRequest>,
) -> Result<Json<ExecuteResponse>, (StatusCode, Json<ErrorResponse>)> {
let daemon = state.read().await;
match daemon.execute_agent(&agent_id, &body.input).await {
Ok(output) => Ok(Json(ExecuteResponse { output: output.content })),
Err(e) => Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
error: e.to_string(),
}),
)),
}
}
#[derive(Serialize)]
pub struct AgentStatusResponse {
pub agent_id: String,
pub status: String,
}
pub async fn agent_status(
State(state): State<AppState>,
Path(agent_id): Path<String>,
) -> Result<Json<AgentStatusResponse>, (StatusCode, Json<ErrorResponse>)> {
let daemon = state.read().await;
let id = axocoatl_core::AgentId::new(&agent_id);
match daemon.agent_registry.get(&id).await {
Some(actor) => {
let status = axocoatl_actor::get_agent_status(&actor)
.await
.unwrap_or_else(|e| axocoatl_core::AgentStatus::Failed {
error: e,
restarts: 0,
});
Ok(Json(AgentStatusResponse {
agent_id,
status: format!("{:?}", status),
}))
}
None => Err((
StatusCode::NOT_FOUND,
Json(ErrorResponse {
error: format!("Agent '{}' not found", agent_id),
}),
)),
}
}
// --- Token endpoints ---
#[derive(Serialize)]
pub struct TokenReport {
pub message: String,
}
pub async fn token_report() -> Json<TokenReport> {
Json(TokenReport {
message: "Token reporting — detailed per-agent usage coming soon".to_string(),
})
}
// --- WebSocket streaming endpoint ---
#[derive(Deserialize)]
struct WsCommand {
agent_id: String,
input: String,
}
pub async fn ws_handler(
ws: axum::extract::WebSocketUpgrade,
State(state): State<AppState>,
) -> axum::response::Response {
ws.on_upgrade(move |socket| handle_ws(socket, state))
}
async fn handle_ws(mut socket: axum::extract::ws::WebSocket, state: AppState) {
use axum::extract::ws::Message;
use axocoatl_llm::StreamEvent;
use tokio_stream::StreamExt;
while let Some(Ok(msg)) = socket.recv().await {
match msg {
Message::Text(text) => {
let cmd: Result<WsCommand, _> = serde_json::from_str(&text);
match cmd {
Ok(cmd) => {
let daemon = state.read().await;
// Try streaming first via the provider directly
let provider = daemon
.provider_registry
.get("openai")
.or_else(|| daemon.provider_registry.get("anthropic"));
match provider {
Some(provider) => {
let request = axocoatl_llm::ChatRequest::simple(&cmd.input);
match provider.chat_stream(request).await {
Ok(mut stream) => {
drop(daemon); // Release read lock during streaming
while let Some(event) = stream.next().await {
let ws_msg = match event {
Ok(StreamEvent::TextDelta { delta }) => {
serde_json::json!({
"type": "text_delta",
"delta": delta
})
}
Ok(StreamEvent::Done { finish_reason }) => {
serde_json::json!({
"type": "done",
"finish_reason": format!("{:?}", finish_reason)
})
}
Ok(StreamEvent::Usage(usage)) => {
serde_json::json!({
"type": "usage",
"input_tokens": usage.input_tokens,
"output_tokens": usage.output_tokens
})
}
Ok(_) => continue,
Err(e) => {
serde_json::json!({
"type": "error",
"error": e.to_string()
})
}
};
if socket
.send(Message::Text(ws_msg.to_string().into()))
.await
.is_err()
{
break;
}
}
}
Err(_) => {
// Streaming not supported — fall back to non-streaming
match daemon.execute_agent(&cmd.agent_id, &cmd.input).await
{
Ok(output) => {
let msg = serde_json::json!({
"type": "text_delta",
"delta": output
});
let _ = socket
.send(Message::Text(msg.to_string().into()))
.await;
let done = serde_json::json!({"type": "done", "finish_reason": "Stop"});
let _ = socket
.send(Message::Text(done.to_string().into()))
.await;
}
Err(e) => {
let err = serde_json::json!({"type": "error", "error": e.to_string()});
let _ = socket
.send(Message::Text(err.to_string().into()))
.await;
}
}
}
}
}
None => {
let err = serde_json::json!({"type": "error", "error": "No provider available"});
let _ = socket.send(Message::Text(err.to_string().into())).await;
}
}
}
Err(e) => {
let err = serde_json::json!({"type": "error", "error": format!("Invalid command: {e}")});
let _ = socket.send(Message::Text(err.to_string().into())).await;
}
}
}
Message::Close(_) => break,
_ => {}
}
}
}