mj_controller/server/api/
routes.rs1use super::*;
2
3pub(in crate::server) fn router(state: ServerState) -> Router<ServerState> {
4 Router::new()
5 .route(
6 "/sessions/{session_id}/native-agents/{child_id}/history",
7 get(native_agent_history),
8 )
9 .route("/events", get(events::events))
10 .route("/github-token", get(github_token))
11 .route("/profiles/{profile_id}/config", get(profile_config))
12 .route(
13 "/profiles/{profile_id}/subagent-options",
14 get(subagent_options),
15 )
16 .route("/options", get(options))
17 .route(
18 "/sessions/{session_id}/config",
19 axum::routing::patch(set_config),
20 )
21 .route("/workspaces", get(list_workspaces).post(create_workspace))
22 .route("/sessions", get(list_sessions).post(start_session))
23 .route("/sessions/{session_id}", get(get_session))
24 .route(
25 "/sessions/{session_id}/subagents",
26 get(list_subagents).post(spawn_subagent),
27 )
28 .route("/sessions/{session_id}/prompt", post(prompt))
29 .route("/sessions/{session_id}/transcript", get(transcript))
30 .route("/sessions/{session_id}/history", get(transcript_history))
31 .route("/sessions/{session_id}/usage", get(usage))
32 .route("/sessions/{session_id}/usage/tree", get(usage_tree))
33 .route("/sessions/{session_id}/wait", post(wait))
34 .route("/sessions/{session_id}/suspend", post(suspend))
35 .route("/sessions/{session_id}/destroy", post(destroy))
36 .route("/sessions/{session_id}/resume", post(resume))
37 .route(
38 "/sessions/{session_id}/background-tasks/stop",
39 post(stop_background_task),
40 )
41 .route(
42 "/sessions/{session_id}/interrupt-turn",
43 post(interrupt_turn),
44 )
45 .route(
46 "/sessions/{session_id}/review",
47 get(review_status).post(start_review),
48 )
49 .route(
50 "/sessions/{session_id}/review/{resolution}",
51 post(resolve_review),
52 )
53 .route("/sessions/{session_id}/diff", get(diff))
54 .route(
55 "/sessions/{session_id}/files",
56 get(read_file)
57 .put(write_file)
58 .layer(axum::extract::DefaultBodyLimit::max(
59 mj_checkpoint::archive::MAX_SESSION_FILE_BYTES as usize,
60 )),
61 )
62 .route("/sessions/{session_id}/elicitations", get(elicitations))
63 .route(
64 "/sessions/{session_id}/elicitations/{elicitation_id}",
65 post(respond_elicitation),
66 )
67 .route("/sessions/{session_id}/export", post(export))
68 .route("/wiki/search", get(wiki_search))
69 .route("/wiki/sessions/{wiki_id}", get(wiki_session))
70 .route("/wiki/sessions/{wiki_id}/brief", get(wiki_brief))
71 .route("/wiki/sessions/{wiki_id}/hits", get(wiki_hits))
72 .route("/wiki/sessions/{wiki_id}/restore", post(wiki_restore))
73 .route_layer(axum::middleware::from_fn_with_state(
74 state,
75 require_api_auth,
76 ))
77 .layer(axum::middleware::from_fn(api_response_headers))
80}
81
82pub(super) async fn require_api_auth(
88 State(state): State<ServerState>,
89 request: HttpRequest<axum::body::Body>,
90 next: Next,
91) -> Result<Response, ApiFailure> {
92 let bearer = request
93 .headers()
94 .get(AUTHORIZATION)
95 .and_then(|value| value.to_str().ok())
96 .and_then(|value| value.strip_prefix("Bearer "))
97 .map(str::trim);
98 if bearer.is_some_and(|token| {
99 constant_time_eq(state.api_token.as_bytes(), token.as_bytes()) && !token.is_empty()
100 }) {
101 return Ok(next.run(request).await);
102 }
103 let cookie =
104 super::super::authenticated_viewer(&state, request.headers()).map_err(|error| {
105 if error.status == StatusCode::UNAUTHORIZED {
106 ApiFailure::new(
107 StatusCode::UNAUTHORIZED,
108 "supply the API token from the api-token file as a bearer token",
109 )
110 } else {
111 ApiFailure::from(error)
112 }
113 })?;
114 let mut response = next.run(request).await;
115 super::super::renew_viewer_response(&state, &cookie, &mut response)?;
116 Ok(response)
117}
118
119pub(super) async fn api_response_headers(
122 request: HttpRequest<axum::body::Body>,
123 next: Next,
124) -> Response {
125 let mut response = next.run(request).await;
126 let headers = response.headers_mut();
127 headers.insert(API_VERSION_HEADER, HeaderValue::from_static(API_VERSION));
128 headers.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
129 response
130}
131
132#[derive(Debug, Default, Clone, Serialize, Deserialize)]
137#[serde(deny_unknown_fields)]
138pub struct SessionListQuery {
139 pub workspace_id: Option<String>,
140}
141
142#[derive(Debug, Default, Deserialize)]
143#[serde(deny_unknown_fields)]
144pub(super) struct ProfileConfigQuery {
145 pub(super) model: Option<String>,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize)]
149#[serde(deny_unknown_fields)]
150pub struct SetConfigRequest {
151 pub key: String,
152 pub value: String,
153}