Skip to main content

comindly_api/
lib.rs

1//! The requests and answers of `/api/v1`, `/health` and `/ready`: what the server writes and what
2//! the CLI and the web app read. Grouped by area and re-exported here.
3
4use serde::{Deserialize, Serialize};
5
6pub mod agents;
7pub mod answers;
8#[cfg(feature = "client")]
9pub mod client;
10pub mod conventions;
11pub mod credentials;
12pub mod discussion;
13pub mod doctor;
14pub mod events;
15pub mod graph;
16pub mod hosting;
17pub mod jobs;
18pub mod ledger;
19pub mod manifests;
20pub mod model;
21pub mod preflight;
22pub mod prompts;
23pub mod search;
24pub mod server;
25pub mod team;
26pub mod view;
27
28mod account;
29mod browse;
30mod records;
31mod repos;
32mod settings;
33
34pub use account::*;
35pub use browse::*;
36pub use records::*;
37pub use repos::*;
38pub use settings::*;
39
40/// What most changes answer: done, and a line to show.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Done {
43    pub ok: bool,
44    pub message: String,
45}
46
47impl Done {
48    pub fn new(message: impl Into<String>) -> Done {
49        Done {
50            ok: true,
51            message: message.into(),
52        }
53    }
54}
55
56/// Every failed request: `{"error": {"code", "message"}}`.
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct ErrorBody {
59    pub error: ErrorDetail,
60}
61
62/// Why a request failed; `host` and `username` when a clone needs an access token.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct ErrorDetail {
65    pub code: String,
66    pub message: String,
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub host: Option<String>,
69    #[serde(default, skip_serializing_if = "Option::is_none")]
70    pub username: Option<String>,
71}
72
73/// `GET /health`: the process answers.
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct Health {
76    pub status: String,
77    pub version: String,
78}
79
80/// `GET /ready`: an index is loaded and, on a server, the stores answer.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct Ready {
83    pub ready: bool,
84    pub index: bool,
85    pub stores: bool,
86}
87
88/// `{ id }`: revoke a token, erase a record.
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct IdRequest {
91    pub id: String,
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    /// Reads `json` as `T` and writes it back unchanged, keys in the same order.
99    fn same<T: Serialize + serde::de::DeserializeOwned>(json: &str) {
100        let v: T = serde_json::from_str(json).unwrap();
101        assert_eq!(serde_json::to_string(&v).unwrap(), json);
102    }
103
104    #[test]
105    fn answers_keep_their_wire_shape() {
106        same::<Me>(r#"{"mode":"local"}"#);
107        same::<Me>(
108            r#"{"mode":"server","signed_in":false,"email":null,"role":null,"sign_in":"email","sign_in_methods":["oidc","email","code"],"oidc_label":"Microsoft","public_url":"http://localhost:1","gateway_url":"https://mcp.example.com/mcp"}"#,
109        );
110        let older: Me = serde_json::from_str(
111            r#"{"mode":"server","signed_in":false,"email":null,"role":null,"sign_in":"code","public_url":"http://localhost:1"}"#,
112        )
113        .unwrap();
114        assert!(matches!(older, Me::Server { sign_in_methods, .. } if sign_in_methods.is_empty()));
115        same::<DeviceToken>(r#"{"status":"pending"}"#);
116        same::<DeviceToken>(
117            r#"{"status":"approved","token":"t","email":"a@example.com","expires":null}"#,
118        );
119        same::<Secrets>(
120            r#"{"secrets":[{"source":"deployment","name":"llm_api_key","label":"LLM API key"},{"name":"git:github.com","source":"stored","hint":"…abcd","updated":"2026-01-01T00:00:00+00:00","updated_by":"a@example.com","label":"GitHub token for github.com"}],"can_store":true}"#,
121        );
122        same::<IndexState>(r#"{"running":false,"events":[],"queued":[]}"#);
123        same::<IndexState>(
124            r#"{"id":"j","running":false,"cancelled":false,"ok":true,"status":"succeeded","events":[],"started":"s","finished":"f","queued":[{"id":"k","kind":"index","position":1,"started_by":null,"created":"c"}]}"#,
125        );
126        same::<Jobs>(
127            r#"{"jobs":[{"id":"k","kind":"index","started_by":null,"status":"queued","created":"c","started":null,"finished":null,"error":null,"position":1}]}"#,
128        );
129        same::<Invited>(r#"{"ok":true,"message":"Invitation emailed to a@example.com"}"#);
130        same::<ErrorBody>(r#"{"error":{"code":"not_found","message":"no such job"}}"#);
131        same::<Neighbourhood>(r#"{"nodes":[],"edges":[]}"#);
132    }
133}