airl-sdk 0.1.0

Typed Rust client for the AIRL HTTP API (create projects, apply patches, typecheck, interpret, compile, query)
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! AIRL SDK - Typed Rust client for the AIRL HTTP API.
//!
//! Provides a [`Client`] type wrapping every endpoint exposed by `airl-api`,
//! with optional Bearer token authentication and structured error types.
//!
//! # Example
//!
//! ```no_run
//! use airl_sdk::Client;
//!
//! let client = Client::new("http://127.0.0.1:9090");
//!
//! // Create a project from a JSON IR string
//! let info = client.create_project("my-app", "{...}").unwrap();
//! println!("project: {} version={}", info.name, info.version);
//!
//! // Type check
//! let tc = client.typecheck().unwrap();
//! assert!(tc.success);
//!
//! // Interpret
//! let output = client.interpret_default().unwrap();
//! print!("{}", output.stdout);
//! ```
//!
//! # Authentication
//!
//! If the server is started with `serve_with_auth`, provide a token:
//!
//! ```no_run
//! use airl_sdk::Client;
//!
//! let client = Client::new("http://127.0.0.1:9090")
//!     .with_auth_token("my-secret-token");
//! ```

#![deny(missing_docs)]

use airl_ir::Module;
use airl_patch::{Impact, Patch};
use airl_project::constraints::{Constraint, ConstraintViolation};
use airl_project::diff::ModuleDiff;
use airl_project::queries::{BuiltinUsage, DeadCodeReport, EffectSurface};
use airl_project::{CallEdge, EffectSummary, FuncSummary};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use thiserror::Error;

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors returned by the AIRL SDK client.
#[derive(Debug, Error)]
pub enum SdkError {
    /// Network-level error (connection refused, DNS, timeout, etc.).
    #[error("HTTP transport error: {0}")]
    Transport(String),
    /// Server returned a non-success status with a structured error body.
    #[error("API error {status} ({code}): {message}")]
    Api {
        /// HTTP status code.
        status: u16,
        /// Short error code from the API.
        code: String,
        /// Human-readable error message.
        message: String,
    },
    /// Response body could not be parsed as the expected type.
    #[error("response parse error: {0}")]
    Parse(#[from] serde_json::Error),
}

/// Structured error body returned by the AIRL API on non-2xx responses.
#[derive(Debug, Deserialize)]
struct ApiErrorBody {
    error: String,
    code: String,
}

// ---------------------------------------------------------------------------
// Request/response types (mirror the wire format)
// ---------------------------------------------------------------------------

/// Summary information about a loaded project.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ProjectInfo {
    /// Project name.
    pub name: String,
    /// Content-addressed version hash of the current module.
    pub version: String,
    /// Number of functions in the module.
    pub function_count: usize,
    /// Number of patches in the undo history.
    pub history_length: usize,
}

/// Result of [`Client::get_module`]: the current module plus its version.
#[derive(Debug, Clone, Deserialize)]
pub struct ModuleResponse {
    /// The current module state.
    pub module: Module,
    /// Content-addressed version hash.
    pub version: String,
}

/// One entry from the type checker output.
#[derive(Debug, Clone, Deserialize)]
pub struct DiagnosticResponse {
    /// Severity level: `"error"` or `"warning"`.
    pub severity: String,
    /// IR node ID where the diagnostic originated, if any.
    pub node_id: Option<String>,
    /// Human-readable message.
    pub message: String,
}

/// Response from [`Client::typecheck`].
#[derive(Debug, Clone, Deserialize)]
pub struct TypeCheckResponse {
    /// `true` if no errors were found.
    pub success: bool,
    /// Type errors (prevent execution).
    pub errors: Vec<DiagnosticResponse>,
    /// Non-fatal warnings.
    pub warnings: Vec<DiagnosticResponse>,
}

/// Response from [`Client::interpret`].
#[derive(Debug, Clone, Deserialize)]
pub struct InterpretResponse {
    /// `true` if interpretation succeeded.
    pub success: bool,
    /// Captured standard output.
    pub stdout: String,
    /// Exit code (0 = success).
    pub exit_code: i32,
    /// Runtime error message, if any.
    pub error: Option<String>,
}

/// Response from [`Client::compile`].
#[derive(Debug, Clone, Deserialize)]
pub struct CompileResponse {
    /// `true` if compilation and execution succeeded.
    pub success: bool,
    /// Captured standard output.
    pub stdout: String,
    /// Exit code (0 = success).
    pub exit_code: i32,
    /// Compilation time in milliseconds (excludes execution).
    pub compile_time_ms: u64,
    /// Compile error message, if any.
    pub error: Option<String>,
}

/// Response from [`Client::apply_patch`] or [`Client::undo_patch`].
#[derive(Debug, Clone, Deserialize)]
pub struct PatchResultResponse {
    /// `true` if the patch was applied.
    pub success: bool,
    /// Version hash after the patch.
    pub new_version: String,
    /// Analysis of which functions/types were affected.
    pub impact: Impact,
}

/// Response from [`Client::preview_patch`].
#[derive(Debug, Clone, Deserialize)]
pub struct PatchPreviewResponse {
    /// `true` if the patch would succeed if applied.
    pub would_succeed: bool,
    /// Structural validation error, if any.
    pub validation_error: Option<String>,
    /// Type errors that would arise after applying the patch.
    pub type_errors: Vec<String>,
    /// Analysis of which functions/types would be affected.
    pub impact: Impact,
}

/// Response from [`Client::check_constraints`].
#[derive(Debug, Clone, Deserialize)]
pub struct ConstraintsResponse {
    /// `true` if no constraints were violated.
    pub ok: bool,
    /// List of violations, one per constraint that failed.
    pub violations: Vec<ConstraintViolation>,
}

/// Execution limits for [`Client::interpret`].
#[derive(Debug, Clone, Copy, Serialize)]
pub struct InterpretLimits {
    /// Maximum number of evaluation steps before aborting.
    pub max_steps: u64,
    /// Maximum call-stack depth.
    pub max_call_depth: u32,
}

impl Default for InterpretLimits {
    fn default() -> Self {
        Self {
            max_steps: 1_000_000,
            max_call_depth: 1000,
        }
    }
}

/// Language for [`Client::project_to_text`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectionLang {
    /// Project the module to TypeScript source.
    TypeScript,
    /// Project the module to Python source.
    Python,
    /// Raw JSON (pretty-printed IR).
    Json,
    /// Pseudocode function-signature summary (API fallback).
    Pseudocode,
}

impl ProjectionLang {
    fn as_str(self) -> &'static str {
        match self {
            ProjectionLang::TypeScript => "typescript",
            ProjectionLang::Python => "python",
            ProjectionLang::Json => "json",
            ProjectionLang::Pseudocode => "pseudocode",
        }
    }
}

/// Response from [`Client::project_to_text`].
#[derive(Debug, Clone, Deserialize)]
pub struct TextProjectionResponse {
    /// Language identifier echoed back from the request.
    pub language: String,
    /// Rendered source code.
    pub text: String,
}

// ---------------------------------------------------------------------------
// Client
// ---------------------------------------------------------------------------

/// Typed client for the AIRL HTTP API.
///
/// Construct with [`Client::new`] and chain optional configuration
/// methods like [`Client::with_auth_token`] and [`Client::with_timeout`].
pub struct Client {
    base_url: String,
    agent: ureq::Agent,
    auth_token: Option<String>,
}

impl Client {
    /// Create a new client pointing at the given API server base URL
    /// (e.g. `"http://127.0.0.1:9090"`).
    pub fn new(base_url: impl Into<String>) -> Self {
        let agent = ureq::AgentBuilder::new()
            .timeout(Duration::from_secs(30))
            .build();
        Self {
            base_url: base_url.into().trim_end_matches('/').to_string(),
            agent,
            auth_token: None,
        }
    }

    /// Set a Bearer token to send with every request.
    /// Required when the server was started via `serve_with_auth`.
    pub fn with_auth_token(mut self, token: impl Into<String>) -> Self {
        self.auth_token = Some(token.into());
        self
    }

    /// Override the default 30-second request timeout.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.agent = ureq::AgentBuilder::new().timeout(timeout).build();
        self
    }

    // -- Project management --

    /// Create a project from a JSON IR string. Equivalent to `POST /project/create`.
    pub fn create_project(
        &self,
        name: impl Into<String>,
        module_json: impl Into<String>,
    ) -> Result<ProjectInfo, SdkError> {
        self.post(
            "/project/create",
            &serde_json::json!({
                "name": name.into(),
                "module_json": module_json.into(),
            }),
        )
    }

    /// Get project metadata. Equivalent to `GET /project`.
    pub fn get_project(&self) -> Result<ProjectInfo, SdkError> {
        self.get("/project")
    }

    /// Fetch the current module IR. Equivalent to `GET /module`.
    pub fn get_module(&self) -> Result<ModuleResponse, SdkError> {
        self.get("/module")
    }

    // -- Patch operations --

    /// Apply a semantic patch. Equivalent to `POST /patch/apply`.
    ///
    /// The server expects the patch fields at the request body root
    /// (thanks to `#[serde(flatten)]` on the request struct), so we send
    /// the patch as-is.
    pub fn apply_patch(&self, patch: &Patch) -> Result<PatchResultResponse, SdkError> {
        self.post("/patch/apply", patch)
    }

    /// Preview a patch (dry-run). Equivalent to `POST /patch/preview`.
    pub fn preview_patch(&self, patch: &Patch) -> Result<PatchPreviewResponse, SdkError> {
        self.post("/patch/preview", patch)
    }

    /// Undo the most recent patch. Equivalent to `POST /patch/undo`.
    pub fn undo_patch(&self) -> Result<PatchResultResponse, SdkError> {
        self.post("/patch/undo", &serde_json::json!({}))
    }

    // -- Build & run --

    /// Type-check the current module. Equivalent to `POST /typecheck`.
    pub fn typecheck(&self) -> Result<TypeCheckResponse, SdkError> {
        self.post("/typecheck", &serde_json::json!({}))
    }

    /// Check the module against architectural constraints.
    /// Equivalent to `POST /constraints/check`.
    pub fn check_constraints(
        &self,
        constraints: &[Constraint],
    ) -> Result<ConstraintsResponse, SdkError> {
        self.post(
            "/constraints/check",
            &serde_json::json!({ "constraints": constraints }),
        )
    }

    /// Diff the current module against another module (passed as JSON).
    /// Equivalent to `POST /diff`.
    pub fn diff(&self, other_module_json: impl Into<String>) -> Result<ModuleDiff, SdkError> {
        self.post(
            "/diff",
            &serde_json::json!({ "other_module_json": other_module_json.into() }),
        )
    }

    /// Interpret the module with custom limits. Equivalent to `POST /interpret`.
    pub fn interpret(&self, limits: InterpretLimits) -> Result<InterpretResponse, SdkError> {
        self.post("/interpret", &limits)
    }

    /// Interpret the module with default limits (1M steps, 1000 call depth).
    pub fn interpret_default(&self) -> Result<InterpretResponse, SdkError> {
        self.interpret(InterpretLimits::default())
    }

    /// Cranelift JIT compile and run the module. Equivalent to `POST /compile`.
    pub fn compile(&self) -> Result<CompileResponse, SdkError> {
        self.post("/compile", &serde_json::json!({}))
    }

    /// Compile the module to a WASM binary. Equivalent to `POST /compile/wasm`.
    /// Returns raw bytes.
    pub fn compile_wasm(&self) -> Result<Vec<u8>, SdkError> {
        let url = format!("{}/compile/wasm", self.base_url);
        let mut req = self.agent.post(&url);
        if let Some(ref token) = self.auth_token {
            req = req.set("Authorization", &format!("Bearer {token}"));
        }
        match req.send_string("{}") {
            Ok(resp) => {
                let mut bytes = Vec::new();
                resp.into_reader()
                    .read_to_end(&mut bytes)
                    .map_err(|e| SdkError::Transport(e.to_string()))?;
                Ok(bytes)
            }
            Err(ureq::Error::Status(code, resp)) => Err(api_err(code, resp)),
            Err(e) => Err(SdkError::Transport(e.to_string())),
        }
    }

    // -- Queries --

    /// Find functions whose name contains `pattern` (substring match).
    /// Equivalent to `GET /query/functions?pattern=<p>`.
    pub fn find_functions(&self, pattern: &str) -> Result<Vec<FuncSummary>, SdkError> {
        #[derive(Deserialize)]
        struct Resp {
            functions: Vec<FuncSummary>,
        }
        let path = format!("/query/functions?pattern={}", url_encode(pattern));
        let resp: Resp = self.get(&path)?;
        Ok(resp.functions)
    }

    /// Get call-graph edges for a function.
    /// Equivalent to `GET /query/call-graph?func=<name>`.
    pub fn get_call_graph(&self, func: &str) -> Result<Vec<CallEdge>, SdkError> {
        #[derive(Deserialize)]
        struct Resp {
            edges: Vec<CallEdge>,
        }
        let path = format!("/query/call-graph?func={}", url_encode(func));
        let resp: Resp = self.get(&path)?;
        Ok(resp.edges)
    }

    /// Get the declared effect set for a function.
    /// Equivalent to `GET /query/effects?func=<name>`.
    pub fn get_effects(&self, func: &str) -> Result<EffectSummary, SdkError> {
        let path = format!("/query/effects?func={}", url_encode(func));
        self.get(&path)
    }

    /// Find functions unreachable from an entry point (default `"main"`).
    /// Equivalent to `GET /query/dead-code?entry=<name>`.
    pub fn find_dead_code(&self, entry: &str) -> Result<DeadCodeReport, SdkError> {
        let path = format!("/query/dead-code?entry={}", url_encode(entry));
        self.get(&path)
    }

    /// Count calls to each `std::...` builtin across the module.
    /// Equivalent to `GET /query/builtin-usage`.
    pub fn builtin_usage(&self) -> Result<BuiltinUsage, SdkError> {
        self.get("/query/builtin-usage")
    }

    /// Get the effect surface of the module.
    /// Equivalent to `GET /query/effect-surface`.
    pub fn effect_surface(&self) -> Result<EffectSurface, SdkError> {
        self.get("/query/effect-surface")
    }

    // -- Projections --

    /// Render the module in a target language.
    /// Equivalent to `POST /project/text { language }`.
    pub fn project_to_text(
        &self,
        lang: ProjectionLang,
    ) -> Result<TextProjectionResponse, SdkError> {
        self.post(
            "/project/text",
            &serde_json::json!({ "language": lang.as_str() }),
        )
    }

    // -- Low-level helpers --

    fn get<R: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<R, SdkError> {
        let url = format!("{}{}", self.base_url, path);
        let mut req = self.agent.get(&url);
        if let Some(ref token) = self.auth_token {
            req = req.set("Authorization", &format!("Bearer {token}"));
        }
        match req.call() {
            Ok(resp) => {
                let text = resp
                    .into_string()
                    .map_err(|e| SdkError::Transport(e.to_string()))?;
                Ok(serde_json::from_str(&text)?)
            }
            Err(ureq::Error::Status(code, resp)) => Err(api_err(code, resp)),
            Err(e) => Err(SdkError::Transport(e.to_string())),
        }
    }

    fn post<B: Serialize, R: for<'de> Deserialize<'de>>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<R, SdkError> {
        let url = format!("{}{}", self.base_url, path);
        let mut req = self
            .agent
            .post(&url)
            .set("Content-Type", "application/json");
        if let Some(ref token) = self.auth_token {
            req = req.set("Authorization", &format!("Bearer {token}"));
        }
        let body_str = serde_json::to_string(body)?;
        match req.send_string(&body_str) {
            Ok(resp) => {
                let text = resp
                    .into_string()
                    .map_err(|e| SdkError::Transport(e.to_string()))?;
                Ok(serde_json::from_str(&text)?)
            }
            Err(ureq::Error::Status(code, resp)) => Err(api_err(code, resp)),
            Err(e) => Err(SdkError::Transport(e.to_string())),
        }
    }
}

/// Convert a ureq error response into an [`SdkError::Api`].
fn api_err(status: u16, resp: ureq::Response) -> SdkError {
    let body_text = resp.into_string().unwrap_or_default();
    match serde_json::from_str::<ApiErrorBody>(&body_text) {
        Ok(body) => SdkError::Api {
            status,
            code: body.code,
            message: body.error,
        },
        Err(_) => SdkError::Api {
            status,
            code: "UNKNOWN".to_string(),
            message: body_text,
        },
    }
}

/// Minimal URL-encoding for query string values (alphanumeric + `-_.~` pass through).
fn url_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_client_builder() {
        let client = Client::new("http://localhost:9090/")
            .with_auth_token("secret")
            .with_timeout(Duration::from_secs(5));
        assert_eq!(client.base_url, "http://localhost:9090");
        assert_eq!(client.auth_token.as_deref(), Some("secret"));
    }

    #[test]
    fn test_url_encode() {
        assert_eq!(url_encode("hello"), "hello");
        assert_eq!(url_encode("hello world"), "hello%20world");
        assert_eq!(url_encode("a/b?c=d&e"), "a%2Fb%3Fc%3Dd%26e");
        assert_eq!(url_encode("abc-123_.~"), "abc-123_.~");
    }

    #[test]
    fn test_projection_lang_str() {
        assert_eq!(ProjectionLang::TypeScript.as_str(), "typescript");
        assert_eq!(ProjectionLang::Python.as_str(), "python");
        assert_eq!(ProjectionLang::Json.as_str(), "json");
        assert_eq!(ProjectionLang::Pseudocode.as_str(), "pseudocode");
    }

    #[test]
    fn test_interpret_limits_default() {
        let limits = InterpretLimits::default();
        assert_eq!(limits.max_steps, 1_000_000);
        assert_eq!(limits.max_call_depth, 1000);
    }

    #[test]
    fn test_unreachable_server_error() {
        // Use a port very unlikely to be bound. Should fail with Transport error
        // (not hang — the default timeout handles that).
        let client = Client::new("http://127.0.0.1:1").with_timeout(Duration::from_millis(200));
        let err = client.get_project().unwrap_err();
        assert!(matches!(err, SdkError::Transport(_)));
    }
}