Skip to main content

agent_server/api/
types.rs

1//! Shared API response conversion and wire-format types.
2
3mod agent_catalog;
4mod agent_list_item;
5mod cancel_run_response;
6mod get_run_response;
7mod run_event_response;
8mod submit_run_request;
9mod submit_run_response;
10
11use agent_service::{RunEvent, RunEventKind};
12
13pub use agent_catalog::AgentCatalog;
14pub use agent_list_item::AgentListItem;
15pub use cancel_run_response::CancelRunResponse;
16pub use get_run_response::GetRunResponse;
17pub use run_event_response::RunEventResponse;
18pub use submit_run_request::SubmitRunRequest;
19pub use submit_run_response::SubmitRunResponse;
20
21impl From<RunEvent> for RunEventResponse {
22    fn from(event: RunEvent) -> Self {
23        let occurred_at_unix_ms = event
24            .occurred_at
25            .duration_since(std::time::UNIX_EPOCH)
26            .unwrap_or_default()
27            .as_millis();
28
29        Self {
30            sequence: event.sequence,
31            kind: event_kind_name(event.kind),
32            iteration: event.iteration,
33            tool_name: event.tool_name,
34            model_id: event.model_id,
35            next_model_id: event.next_model_id,
36            model_candidates: event.model_candidates,
37            attempted_candidates: event.attempted_candidates,
38            error_category: event.error_category,
39            occurred_at_unix_ms,
40        }
41    }
42}
43
44fn event_kind_name(kind: RunEventKind) -> &'static str {
45    match kind {
46        RunEventKind::Queued => "queued",
47        RunEventKind::Started => "started",
48        RunEventKind::Completed => "completed",
49        RunEventKind::Failed => "failed",
50        RunEventKind::Cancelled => "cancelled",
51        RunEventKind::TimedOut => "timed_out",
52        RunEventKind::MaxIterationsReached => "max_iterations_reached",
53        RunEventKind::ModelRequested => "model_requested",
54        RunEventKind::ModelCandidatesDiscovered => "model_candidates_discovered",
55        RunEventKind::ModelCandidateAttemptStarted => "model_candidate_attempt_started",
56        RunEventKind::ModelCandidateAttemptFailed => "model_candidate_attempt_failed",
57        RunEventKind::ModelFallback => "model_fallback",
58        RunEventKind::ModelSelected => "model_selected",
59        RunEventKind::ModelRoutingFailed => "model_routing_failed",
60        RunEventKind::ModelResponded => "model_responded",
61        RunEventKind::ToolExecutionStarted => "tool_execution_started",
62        RunEventKind::ToolExecutionFinished => "tool_execution_finished",
63        RunEventKind::ModelFailed => "model_failed",
64        RunEventKind::RuntimeCancelled => "runtime_cancelled",
65        RunEventKind::RuntimeTimedOut => "runtime_timed_out",
66    }
67}
68
69impl AgentCatalog {
70    pub fn new(agents: impl IntoIterator<Item = AgentListItem>) -> Self {
71        let mut agents: Vec<_> = agents.into_iter().collect();
72        agents.sort_by(|left, right| left.name.cmp(&right.name));
73        Self {
74            agents: agents.into(),
75        }
76    }
77
78    pub fn list(&self) -> &[AgentListItem] {
79        &self.agents
80    }
81}