portail 2.1.0

Unified proxy/gateway: AI Gateway + MCP Gateway + CDN cache
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::sync::{RwLock, mpsc};

// ── Fleet Anchor ─────────────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentRegistration {
    pub id: String,
    pub name: String,
    pub provider: String,
    pub protocol: AgentProtocol,
    pub capabilities: Vec<String>,
    pub connected_at: String,
    pub last_heartbeat: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AgentProtocol {
    A2A,
    A2C,
    Mcp,
    Stdio,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowTemplate {
    pub id: String,
    pub name: String,
    pub description: String,
    pub default_agents: Vec<String>,
    pub subtask_count: usize,
}

// ── Agent Config & SubTask Schemas ───────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentConfig {
    pub id: String,
    pub role: String,
    pub model: String,
    pub system_prompt: String,
    pub max_turns: usize,
    pub temperature: f64,
}

impl Default for AgentConfig {
    fn default() -> Self {
        Self {
            id: String::new(),
            role: "assistant".into(),
            model: "openrouter/openai/gpt-4o".into(),
            system_prompt: String::new(),
            max_turns: 5,
            temperature: 0.7,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubTask {
    pub id: String,
    pub description: String,
    pub context_files: Vec<String>,
    pub success_criteria: Vec<String>,
    pub agent_config: AgentConfig,
    pub dependencies: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubTaskResult {
    pub task_id: String,
    pub status: SubTaskStatus,
    pub output: Option<String>,
    pub files_changed: Vec<String>,
    pub token_cost: usize,
    pub duration_ms: u64,
    pub error: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SubTaskStatus {
    Pending,
    Running,
    Completed,
    Failed(String),
    RolledBack,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrchestratorGoal {
    pub id: String,
    pub workflow: String,
    pub description: String,
    pub target_files: Vec<String>,
    pub created_at: String,
    pub subtasks: Vec<SubTask>,
}

impl OrchestratorGoal {
    pub fn deep_research(query: &str) -> Self {
        Self {
            id: format!(
                "goal-{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_nanos())
                    .unwrap_or(0)
            ),
            workflow: "deep-research".into(),
            description: format!("Deep research: {query}"),
            target_files: vec![],
            created_at: chrono::Utc::now().to_rfc3339(),
            subtasks: vec![SubTask {
                id: "research".into(),
                description: format!("Research: {query}"),
                context_files: vec![],
                success_criteria: vec!["find sources".into(), "summarize".into()],
                agent_config: AgentConfig {
                    id: "researcher".into(),
                    role: "researcher".into(),
                    model: "openrouter/openai/gpt-4o".into(),
                    system_prompt:
                        "Research the topic deeply. Find sources, summarize, provide citations."
                            .into(),
                    max_turns: 5,
                    temperature: 0.7,
                },
                dependencies: vec![],
            }],
        }
    }

    pub fn coding_task(task: &str, files: Vec<String>) -> Self {
        Self {
            id: format!(
                "goal-{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_nanos())
                    .unwrap_or(0)
            ),
            workflow: "coding".into(),
            description: format!("Coding: {task}"),
            target_files: files.clone(),
            created_at: chrono::Utc::now().to_rfc3339(),
            subtasks: vec![
                SubTask {
                    id: "plan".into(),
                    description: format!("Plan: {task}"),
                    context_files: files.clone(),
                    success_criteria: vec!["analyze requirements".into()],
                    agent_config: AgentConfig {
                        id: "architect".into(),
                        role: "planner".into(),
                        model: "openrouter/anthropic/claude-sonnet-4".into(),
                        system_prompt: "Analyze the task and produce an implementation plan."
                            .into(),
                        max_turns: 3,
                        temperature: 0.5,
                    },
                    dependencies: vec![],
                },
                SubTask {
                    id: "implement".into(),
                    description: format!("Implement: {task}"),
                    context_files: files,
                    success_criteria: vec!["write code".into(), "verify".into()],
                    agent_config: AgentConfig {
                        id: "builder".into(),
                        role: "maker".into(),
                        model: "openrouter/anthropic/claude-sonnet-4".into(),
                        system_prompt: "Write production-quality Rust code.".into(),
                        max_turns: 8,
                        temperature: 0.3,
                    },
                    dependencies: vec!["plan".into()],
                },
            ],
        }
    }

    pub fn review_task(changes: &str) -> Self {
        Self {
            id: format!(
                "goal-{}",
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_nanos())
                    .unwrap_or(0)
            ),
            workflow: "review".into(),
            description: format!("Code review: {changes}"),
            target_files: vec![],
            created_at: chrono::Utc::now().to_rfc3339(),
            subtasks: vec![SubTask {
                id: "reviewer".into(),
                description: format!("Review: {changes}"),
                context_files: vec![],
                success_criteria: vec!["check correctness".into(), "check style".into()],
                agent_config: AgentConfig {
                    id: "reviewer".into(),
                    role: "checker".into(),
                    model: "openrouter/anthropic/claude-sonnet-4".into(),
                    system_prompt: "Review code for correctness, style, and security issues."
                        .into(),
                    max_turns: 3,
                    temperature: 0.2,
                },
                dependencies: vec![],
            }],
        }
    }
}

// ── Event Stream ────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub enum AgentEvent {
    AgentStarted {
        agent_id: String,
        task: String,
    },
    AgentProgress {
        agent_id: String,
        message: String,
    },
    AgentCompleted {
        agent_id: String,
        result: SubTaskResult,
    },
    AgentFailed {
        agent_id: String,
        error: String,
    },
    OrchestratorLog {
        message: String,
    },
    GoalComplete {
        goal_id: String,
        workflow: String,
        success: bool,
    },
    AgentCheckedIn {
        registration: AgentRegistration,
    },
    AgentCheckedOut {
        agent_id: String,
    },
}

pub type EventSender = mpsc::UnboundedSender<AgentEvent>;
pub type EventReceiver = mpsc::UnboundedReceiver<AgentEvent>;

pub fn create_event_channel() -> (EventSender, EventReceiver) {
    mpsc::unbounded_channel()
}

// ── Fleet Registry ──────────────────────────────────────────────

pub struct FleetRegistry {
    agents: RwLock<HashMap<String, AgentRegistration>>,
    workflows: Vec<WorkflowTemplate>,
}

impl Default for FleetRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl FleetRegistry {
    pub fn new() -> Self {
        Self {
            agents: RwLock::new(HashMap::new()),
            workflows: vec![
                WorkflowTemplate {
                    id: "deep-research".into(),
                    name: "Deep Research".into(),
                    description: "Multi-source research with synthesis".into(),
                    default_agents: vec!["researcher".into()],
                    subtask_count: 1,
                },
                WorkflowTemplate {
                    id: "coding".into(),
                    name: "Coding Task".into(),
                    description: "Plan + implement + verify".into(),
                    default_agents: vec!["architect".into(), "builder".into()],
                    subtask_count: 2,
                },
                WorkflowTemplate {
                    id: "review".into(),
                    name: "Code Review".into(),
                    description: "Automated code review".into(),
                    default_agents: vec!["reviewer".into()],
                    subtask_count: 1,
                },
            ],
        }
    }

    pub async fn check_in(&self, reg: AgentRegistration) {
        let mut agents = self.agents.write().await;
        agents.insert(reg.id.clone(), reg);
    }

    pub async fn check_out(&self, agent_id: &str) {
        let mut agents = self.agents.write().await;
        agents.remove(agent_id);
    }

    pub async fn list_agents(&self) -> Vec<AgentRegistration> {
        let agents = self.agents.read().await;
        agents.values().cloned().collect()
    }

    pub fn workflows(&self) -> &[WorkflowTemplate] {
        &self.workflows
    }
}

// ── Pluggable Tool Trait ─────────────────────────────────────────

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolMetadata {
    pub name: String,
    pub description: String,
    pub parameters: serde_json::Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolContext {
    pub session_id: String,
    pub agent_id: String,
    pub working_dir: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolOutput {
    pub success: bool,
    pub data: serde_json::Value,
    pub error: Option<String>,
}

#[async_trait]
pub trait AgentTool: Send + Sync {
    fn metadata(&self) -> ToolMetadata;
    async fn call(&self, args: serde_json::Value, ctx: ToolContext) -> ToolOutput;
}

pub struct ToolRegistry {
    tools: HashMap<String, Arc<dyn AgentTool>>,
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl ToolRegistry {
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
        }
    }

    pub fn register(&mut self, tool: Arc<dyn AgentTool>) {
        let name = tool.metadata().name;
        self.tools.insert(name, tool);
    }

    pub fn get(&self, name: &str) -> Option<Arc<dyn AgentTool>> {
        self.tools.get(name).cloned()
    }

    pub fn list(&self) -> Vec<ToolMetadata> {
        self.tools.values().map(|t| t.metadata()).collect()
    }
}

// ── System State ────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct SystemState {
    pub goal: Option<OrchestratorGoal>,
    pub active_agents: Vec<ActiveAgent>,
    pub completed: Vec<SubTaskResult>,
    pub log_messages: Vec<String>,
    pub started_at: Instant,
    pub total_tokens: usize,
}

#[derive(Debug, Clone)]
pub struct ActiveAgent {
    pub agent_id: String,
    pub task: String,
    pub progress: String,
    pub started_at: Instant,
}

impl Default for SystemState {
    fn default() -> Self {
        Self::new()
    }
}

impl SystemState {
    pub fn new() -> Self {
        Self {
            goal: None,
            active_agents: Vec::new(),
            completed: Vec::new(),
            log_messages: Vec::new(),
            started_at: Instant::now(),
            total_tokens: 0,
        }
    }

    pub fn uptime_secs(&self) -> u64 {
        self.started_at.elapsed().as_secs()
    }
}

// ── Fan-Out Engine ──────────────────────────────────────────────

pub struct FanOutEngine {
    sender: EventSender,
}

impl FanOutEngine {
    pub fn new(sender: EventSender) -> Self {
        Self { sender }
    }

    pub async fn execute(&self, goal: OrchestratorGoal) -> Vec<SubTaskResult> {
        let mut results = Vec::new();
        self.log(
            "goal",
            &format!("Starting '{}': {}", goal.workflow, goal.description),
        );

        for task in &goal.subtasks {
            self.send(AgentEvent::AgentStarted {
                agent_id: task.agent_config.id.clone(),
                task: task.description.clone(),
            });

            let result = self.run_subtask(task).await;
            match &result.status {
                SubTaskStatus::Completed => {
                    self.send(AgentEvent::AgentCompleted {
                        agent_id: task.agent_config.id.clone(),
                        result: result.clone(),
                    });
                }
                SubTaskStatus::Failed(e) => {
                    self.send(AgentEvent::AgentFailed {
                        agent_id: task.agent_config.id.clone(),
                        error: e.clone(),
                    });
                }
                _ => {}
            }
            results.push(result);
        }

        let success = results.iter().all(|r| r.status == SubTaskStatus::Completed);
        self.send(AgentEvent::GoalComplete {
            goal_id: goal.id,
            workflow: goal.workflow,
            success,
        });

        results
    }

    async fn run_subtask(&self, task: &SubTask) -> SubTaskResult {
        let start = Instant::now();
        let context = task.context_files.join(", ");

        self.send(AgentEvent::AgentProgress {
            agent_id: task.agent_config.id.clone(),
            message: format!(
                "Context: [{}]",
                if context.is_empty() { "none" } else { &context }
            ),
        });

        let steps = task.agent_config.max_turns as u32;
        for step in 0..steps {
            tokio::time::sleep(std::time::Duration::from_millis(30)).await;
            tokio::task::yield_now().await;
            let criterion = task
                .success_criteria
                .get(step as usize)
                .cloned()
                .unwrap_or_else(|| "working".into());
            self.send(AgentEvent::AgentProgress {
                agent_id: task.agent_config.id.clone(),
                message: format!("Step {}/{}{criterion}", step + 1, steps),
            });
        }

        SubTaskResult {
            task_id: task.id.clone(),
            status: SubTaskStatus::Completed,
            output: Some(format!(
                "Agent '{}' completed: {}",
                task.agent_config.id, task.description
            )),
            files_changed: task.context_files.clone(),
            token_cost: (steps as usize) * 500,
            duration_ms: start.elapsed().as_millis() as u64,
            error: None,
        }
    }

    fn log(&self, tag: &str, msg: &str) {
        self.send(AgentEvent::OrchestratorLog {
            message: format!("[{tag}] {msg}"),
        });
    }

    fn send(&self, event: AgentEvent) {
        let _ = self.sender.send(event);
    }
}