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("/profiles/{profile_id}/config", get(profile_config))
11 .route("/options", get(options))
12 .route(
13 "/sessions/{session_id}/config",
14 axum::routing::patch(set_config),
15 )
16 .route("/workspaces", get(list_workspaces).post(create_workspace))
17 .route("/sessions", get(list_sessions).post(start_session))
18 .route("/sessions/{session_id}", get(get_session))
19 .route(
20 "/sessions/{session_id}/subagents",
21 get(list_subagents).post(spawn_subagent),
22 )
23 .route("/sessions/{session_id}/prompt", post(prompt))
24 .route("/sessions/{session_id}/transcript", get(transcript))
25 .route("/sessions/{session_id}/history", get(transcript_history))
26 .route("/sessions/{session_id}/usage", get(usage))
27 .route("/sessions/{session_id}/wait", post(wait))
28 .route("/sessions/{session_id}/suspend", post(suspend))
29 .route("/sessions/{session_id}/destroy", post(destroy))
30 .route("/sessions/{session_id}/resume", post(resume))
31 .route(
32 "/sessions/{session_id}/interrupt-turn",
33 post(interrupt_turn),
34 )
35 .route("/sessions/{session_id}/diff", get(diff))
36 .route(
37 "/sessions/{session_id}/files",
38 get(read_file)
39 .put(write_file)
40 .layer(axum::extract::DefaultBodyLimit::max(
41 mj_checkpoint::archive::MAX_SESSION_FILE_BYTES as usize,
42 )),
43 )
44 .route("/sessions/{session_id}/elicitations", get(elicitations))
45 .route(
46 "/sessions/{session_id}/elicitations/{elicitation_id}",
47 post(respond_elicitation),
48 )
49 .route("/sessions/{session_id}/export", post(export))
50 .route("/wiki/search", get(wiki_search))
51 .route("/wiki/sessions/{wiki_id}", get(wiki_session))
52 .route("/wiki/sessions/{wiki_id}/brief", get(wiki_brief))
53 .route("/wiki/sessions/{wiki_id}/hits", get(wiki_hits))
54 .route("/wiki/sessions/{wiki_id}/restore", post(wiki_restore))
55 .route_layer(axum::middleware::from_fn_with_state(
56 state,
57 require_api_auth,
58 ))
59 .layer(axum::middleware::from_fn(api_response_headers))
62}
63
64pub(super) async fn require_api_auth(
70 State(state): State<ServerState>,
71 request: HttpRequest<axum::body::Body>,
72 next: Next,
73) -> Result<Response, ApiFailure> {
74 let bearer = request
75 .headers()
76 .get(AUTHORIZATION)
77 .and_then(|value| value.to_str().ok())
78 .and_then(|value| value.strip_prefix("Bearer "))
79 .map(str::trim);
80 if bearer.is_some_and(|token| {
81 constant_time_eq(state.api_token.as_bytes(), token.as_bytes()) && !token.is_empty()
82 }) {
83 return Ok(next.run(request).await);
84 }
85 let cookie =
86 super::super::authenticated_viewer(&state, request.headers()).map_err(|error| {
87 if error.status == StatusCode::UNAUTHORIZED {
88 ApiFailure::new(
89 StatusCode::UNAUTHORIZED,
90 "supply the API token from the api-token file as a bearer token",
91 )
92 } else {
93 ApiFailure::from(error)
94 }
95 })?;
96 let mut response = next.run(request).await;
97 super::super::renew_viewer_response(&state, &cookie, &mut response)?;
98 Ok(response)
99}
100
101pub(super) async fn api_response_headers(
104 request: HttpRequest<axum::body::Body>,
105 next: Next,
106) -> Response {
107 let mut response = next.run(request).await;
108 let headers = response.headers_mut();
109 headers.insert(API_VERSION_HEADER, HeaderValue::from_static(API_VERSION));
110 headers.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
111 response
112}
113
114#[derive(Debug, Default, Clone, Serialize, Deserialize)]
119#[serde(deny_unknown_fields)]
120pub struct SessionListQuery {
121 pub workspace_id: Option<String>,
122}
123
124#[derive(Debug, Default, Deserialize)]
125#[serde(deny_unknown_fields)]
126pub(super) struct ProfileConfigQuery {
127 pub(super) model: Option<String>,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
131#[serde(deny_unknown_fields)]
132pub struct SetConfigRequest {
133 pub key: String,
134 pub value: String,
135}