simian 0.2.1

A command-line tool for exploring and implementing Machine Learning algorithms in Rust.
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
use async_stream::stream;
use axum::body::Body;
use axum::http::StatusCode;
use axum::response::Response;
use axum::{
  Json, Router,
  extract::{Multipart, Path as AxumPath, State},
  routing::{delete, get, post},
};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::io::AsyncReadExt;
use tower_http::services::{ServeDir, ServeFile};

#[derive(rust_embed::RustEmbed)]
#[folder = "$OUT_DIR/ui/dist/"]
pub struct UiAssets;

async fn embedded_serve(uri: axum::http::Uri) -> impl axum::response::IntoResponse {
  let mut path = uri.path().trim_start_matches('/');
  if path.is_empty() {
    path = "index.html";
  }

  match UiAssets::get(path) {
    Some(content) => {
      let mime = mime_guess::from_path(path).first_or_octet_stream();
      axum::response::Response::builder()
        .header(axum::http::header::CONTENT_TYPE, mime.as_ref())
        .body(Body::from(content.data.into_owned()))
        .unwrap()
    }
    None => {
      if let Some(content) = UiAssets::get("index.html") {
        let mime = mime_guess::from_path("index.html").first_or_octet_stream();
        axum::response::Response::builder()
          .header(axum::http::header::CONTENT_TYPE, mime.as_ref())
          .body(Body::from(content.data.into_owned()))
          .unwrap()
      } else {
        axum::response::Response::builder()
          .status(axum::http::StatusCode::NOT_FOUND)
          .body(Body::from("404 Not Found"))
          .unwrap()
      }
    }
  }
}

pub mod sandbox;

#[derive(Deserialize)]
pub struct ExecuteRequest {
  pub code: String,
}

#[derive(Serialize)]
pub struct ExecuteResponse {
  pub stdout: String,
  pub stderr: String,
  pub success: bool,
}

#[derive(Clone)]
struct AppState {
  base_dir: PathBuf,
}

#[derive(Serialize)]
struct PaperMetadata {
  id: String,
  title: String,
  slug: Option<String>,
  last_modified: u64,
}

async fn list_papers(State(state): State<Arc<AppState>>) -> Json<Vec<PaperMetadata>> {
  let mut papers = Vec::new();
  if let Ok(entries) = std::fs::read_dir(&state.base_dir) {
    for entry in entries.flatten() {
      if let Ok(file_type) = entry.file_type()
        && file_type.is_symlink()
      {
        continue;
      }
      if entry.file_name() == ".trash" {
        continue;
      }
      if entry.path().is_dir() {
        let source_file = entry.path().join("source.smn");
        if source_file.exists() {
          let mut title = String::from("Untitled Paper");
          let mut slug = None;

          if let Ok(content) = std::fs::read_to_string(&source_file)
            && let Ok(ast) = serde_json::from_str::<serde_json::Value>(&content)
            && let Some(blocks) = ast.as_array()
          {
            for block in blocks {
              if block.get("type").and_then(|v| v.as_str()) == Some("title") {
                if let Some(children) = block.get("children").and_then(|v| v.as_array()) {
                  let mut t = String::new();
                  for child in children {
                    if let Some(text) = child.get("text").and_then(|v| v.as_str()) {
                      t.push_str(text);
                    }
                  }
                  title = t.trim().to_string();
                }
                break;
              }
            }
          }

          let metadata_file = entry.path().join("metadata.json");
          if let Ok(content) = std::fs::read_to_string(&metadata_file)
            && let Ok(json) = serde_json::from_str::<serde_json::Value>(&content)
            && let Some(s) = json.get("slug").and_then(|v| v.as_str())
          {
            slug = Some(s.to_string());
          }

          let last_modified = entry
            .metadata()
            .and_then(|m| m.modified())
            .map(|t| t.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs())
            .unwrap_or(0);

          papers.push(PaperMetadata {
            id: entry.file_name().to_string_lossy().to_string(),
            title,
            slug,
            last_modified,
          });
        }
      }
    }
  }

  papers.sort_by_key(|b| std::cmp::Reverse(b.last_modified));
  Json(papers)
}

#[derive(Serialize)]
struct CreatePaperResponse {
  id: String,
}

fn create_paper_dir(base_dir: &std::path::Path, id: &str) -> anyhow::Result<PathBuf> {
  let paper_dir = base_dir.join(id);
  std::fs::create_dir_all(&paper_dir)?;
  std::fs::create_dir_all(paper_dir.join("assets"))?;
  std::fs::create_dir_all(paper_dir.join(".local").join("plots"))?;
  std::fs::create_dir_all(paper_dir.join(".local").join("datasets"))?;
  std::fs::create_dir_all(paper_dir.join(".local").join("models"))?;

  let default_content = serde_json::json!([
    {
      "type": "title",
      "children": [{ "text": "" }]
    },
    {
      "type": "paragraph",
      "children": [{ "text": "" }]
    }
  ]);
  let _ = std::fs::write(
    paper_dir.join("source.smn"),
    serde_json::to_string_pretty(&default_content).unwrap(),
  );

  Ok(paper_dir)
}

async fn create_paper(State(state): State<Arc<AppState>>) -> Json<CreatePaperResponse> {
  let id = format!("paper-{:06x}", rand::random::<u32>() & 0xFFFFFF);
  let _ = create_paper_dir(&state.base_dir, &id);
  Json(CreatePaperResponse { id })
}

async fn delete_paper(
  State(state): State<Arc<AppState>>,
  AxumPath(id): AxumPath<String>,
) -> Result<StatusCode, (StatusCode, String)> {
  let paper_dir = state.base_dir.join(&id);
  if !paper_dir.exists() || !paper_dir.is_dir() {
    return Err((StatusCode::NOT_FOUND, "Paper not found".to_string()));
  }

  let trash_dir = state.base_dir.join(".trash");
  if let Err(e) = std::fs::create_dir_all(&trash_dir) {
    return Err((
      StatusCode::INTERNAL_SERVER_ERROR,
      format!("Failed to create trash directory: {}", e),
    ));
  }

  let trash_path = trash_dir.join(&id);
  if let Err(e) = std::fs::rename(&paper_dir, &trash_path) {
    return Err((
      StatusCode::INTERNAL_SERVER_ERROR,
      format!("Failed to move paper to trash: {}", e),
    ));
  }

  Ok(StatusCode::OK)
}

async fn execute_code(
  State(state): State<Arc<AppState>>,
  AxumPath(id): AxumPath<String>,
  Json(payload): Json<ExecuteRequest>,
) -> Response {
  let studio_file = state.base_dir.join(&id).join("source.smn");
  let code = payload.code;
  use crossterm::style::Stylize;
  tracing::debug!("Received code to execute:\n{}", code.as_str().dark_grey());

  match sandbox::run_rust_code(&code, &studio_file).await {
    Ok(mut child) => {
      let mut stdout = child.stdout.take().unwrap();
      let mut stderr = child.stderr.take().unwrap();

      let stream = stream! {
        let (tx, mut rx) = tokio::sync::mpsc::channel::<Bytes>(32);
        let tx1 = tx.clone();
        let tx2 = tx.clone();

        tokio::spawn(async move {
          let mut buf = [0u8; 1024];
          while let Ok(n) = stdout.read(&mut buf).await {
            if n == 0 { break; }
            if tx1.send(Bytes::copy_from_slice(&buf[..n])).await.is_err() { break; }
          }
        });

        tokio::spawn(async move {
          let mut buf = [0u8; 1024];
          while let Ok(n) = stderr.read(&mut buf).await {
            if n == 0 { break; }
            if tx2.send(Bytes::copy_from_slice(&buf[..n])).await.is_err() { break; }
          }
        });

        drop(tx);

        while let Some(bytes) = rx.recv().await {
          yield Ok::<_, std::io::Error>(bytes);
        }

        if let Ok(status) = child.wait().await {
          if status.success() {
            yield Ok(Bytes::from("\n__SIMIAN_EXIT_SUCCESS__\n"));
          } else {
            yield Ok(Bytes::from("\n__SIMIAN_EXIT_FAILURE__\n"));
          }
        }
      };

      Response::builder()
        .header("Content-Type", "text/plain")
        .body(Body::from_stream(stream))
        .unwrap()
    }
    Err(e) => Response::builder()
      .status(500)
      .body(Body::from(format!("Sandbox error: {:?}", e)))
      .unwrap(),
  }
}

async fn get_content(
  State(state): State<Arc<AppState>>,
  AxumPath(id): AxumPath<String>,
) -> Json<serde_json::Value> {
  let studio_file = state.base_dir.join(&id).join("source.smn");
  if studio_file.exists()
    && let Ok(content) = std::fs::read_to_string(&studio_file)
    && let Ok(json) = serde_json::from_str(&content)
  {
    return Json(json);
  }

  // Default AST if file is missing or empty
  Json(serde_json::json!([
    {
      "type": "title",
      "children": [{ "text": "Welcome to Simian Paper" }]
    },
    {
      "type": "paragraph",
      "children": [{ "text": "This is an interactive notebook. Type `# ` for a heading or ` ``` ` for a code block." }]
    },
    {
      "type": "code",
      "children": [{ "text": "println!(\"Hello from Simian Paper!\");" }],
      "output": null
    }
  ]))
}

async fn save_content(
  State(state): State<Arc<AppState>>,
  AxumPath(id): AxumPath<String>,
  Json(payload): Json<serde_json::Value>,
) {
  let studio_file = state.base_dir.join(&id).join("source.smn");
  if let Ok(content) = serde_json::to_string_pretty(&payload) {
    let _ = std::fs::write(&studio_file, content);
  }
}

#[derive(Serialize)]
pub struct UploadResponse {
  pub url: String,
}

async fn upload_asset(
  State(state): State<Arc<AppState>>,
  AxumPath(id): AxumPath<String>,
  mut multipart: Multipart,
) -> Result<Json<UploadResponse>, (axum::http::StatusCode, String)> {
  let paper_dir = state.base_dir.join(&id);
  let assets_dir = paper_dir.join("assets");
  std::fs::create_dir_all(&assets_dir).map_err(|e| {
    let _ = std::fs::write(
      "/tmp/simian-error.log",
      format!("create_dir_all error: {}", e),
    );
    (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
  })?;

  let mut uploaded_filename = String::new();

  while let Some(field) = multipart
    .next_field()
    .await
    .map_err(|e| (axum::http::StatusCode::BAD_REQUEST, e.to_string()))?
  {
    if let Some(file_name) = field.file_name() {
      let file_name = file_name.to_string();
      let ext = std::path::Path::new(&file_name)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("bin");
      let unique_name = format!("{:08x}.{}", rand::random::<u32>(), ext);

      let data = field
        .bytes()
        .await
        .map_err(|e| (axum::http::StatusCode::BAD_REQUEST, e.to_string()))?;
      let dest_path = assets_dir.join(&unique_name);
      std::fs::write(&dest_path, &data).map_err(|e| {
        let _ = std::fs::write(
          "/tmp/simian-error.log",
          format!("fs::write error: {} at {:?}", e, dest_path),
        );
        (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
      })?;

      uploaded_filename = unique_name;
      break; // Only handle the first file for simplicity
    }
  }

  if uploaded_filename.is_empty() {
    return Err((
      axum::http::StatusCode::BAD_REQUEST,
      "No file found in multipart request".to_string(),
    ));
  }

  Ok(Json(UploadResponse {
    url: format!("/api/paper/{}/assets/{}", id, uploaded_filename),
  }))
}

async fn serve_asset(
  State(state): State<Arc<AppState>>,
  AxumPath((id, path)): AxumPath<(String, String)>,
) -> Response {
  let file_path = state.base_dir.join(&id).join("assets").join(&path);
  if let Ok(content) = std::fs::read(&file_path) {
    let mime = mime_guess::from_path(&file_path).first_or_octet_stream();
    axum::response::Response::builder()
      .header(axum::http::header::CONTENT_TYPE, mime.as_ref())
      .body(Body::from(content))
      .unwrap()
  } else {
    axum::response::Response::builder()
      .status(axum::http::StatusCode::NOT_FOUND)
      .body(Body::from("Asset not found"))
      .unwrap()
  }
}

#[derive(Deserialize)]
pub struct HtmlQuery {
  path: String,
}

async fn serve_html(
  axum::extract::Query(query): axum::extract::Query<HtmlQuery>,
) -> axum::response::Html<String> {
  if let Ok(content) = std::fs::read_to_string(&query.path) {
    axum::response::Html(content)
  } else {
    axum::response::Html("<h1>File not found</h1>".to_string())
  }
}

struct ChildGuard(std::process::Child);

impl Drop for ChildGuard {
  fn drop(&mut self) {
    let _ = self.0.kill();
    let _ = self.0.wait();
  }
}

pub async fn run(path: Option<String>, dev: bool) -> anyhow::Result<()> {
  let home = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
  let base_dir = home.join(".simian").join("papers");
  std::fs::create_dir_all(&base_dir)?;

  let state = Arc::new(AppState {
    base_dir: base_dir.clone(),
  });

  // If a path was passed explicitly, ensure it exists in base_dir
  if let Some(ref p) = path {
    let target = base_dir.join(p);
    if !target.exists() {
      let _ = create_paper_dir(&base_dir, p);
    }
  }

  let _dev_guard = if dev {
    tracing::info!("Starting UI in development mode (Vite)...");

    let _ = std::process::Command::new("sh")
      .arg("-c")
      .arg("lsof -ti:7777 | xargs kill -9 2>/dev/null")
      .output();

    let ui_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("ui");

    if !ui_dir.join("node_modules").exists() {
      tracing::info!("UI dependencies not found. Installing via npm install...");
      let status = std::process::Command::new("npm")
        .arg("install")
        .current_dir(&ui_dir)
        .status()?;

      if !status.success() {
        tracing::error!("Failed to install UI dependencies.");
      }
    }

    let vite_bin = ui_dir.join("node_modules/.bin/vite");

    let child = std::process::Command::new(vite_bin)
      .current_dir(&ui_dir)
      .stdout(std::process::Stdio::null())
      .stderr(std::process::Stdio::null())
      .spawn()?;

    tracing::info!("UI entered watch mode.");
    Some(ChildGuard(child))
  } else {
    None
  };

  use tower_http::cors::{Any, CorsLayer};
  let cors = CorsLayer::new()
    .allow_origin(Any)
    .allow_headers(Any)
    .allow_methods(Any);

  let api_router = Router::new()
    .route("/papers", get(list_papers).post(create_paper))
    .route("/paper/{id}", delete(delete_paper))
    .route("/execute/{id}", post(execute_code))
    .route("/paper/{id}/content", get(get_content).post(save_content))
    .route("/paper/{id}/assets", post(upload_asset))
    .route("/paper/{id}/assets/{*path}", get(serve_asset))
    .route("/html", get(serve_html))
    .with_state(state.clone());

  let app = if dev {
    let ui_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("ui/dist");
    let serve_dir =
      ServeDir::new(&ui_dir).not_found_service(ServeFile::new(ui_dir.join("index.html")));
    Router::new()
      .nest("/api", api_router)
      .fallback_service(serve_dir)
      .layer(cors)
  } else {
    Router::new()
      .nest("/api", api_router)
      .fallback(embedded_serve)
      .layer(cors)
  };

  let api_port = if dev { 3000 } else { 7777 };
  let addr = SocketAddr::from(([127, 0, 0, 1], api_port));

  if dev {
    tracing::info!("Paper API listening on http://127.0.0.1:3000");
    tracing::info!("Paper UI available at: http://127.0.0.1:7777 (Development Mode)");
  } else {
    tracing::info!("Paper UI available at: http://127.0.0.1:7777");
  }

  let listener = tokio::net::TcpListener::bind(addr).await?;

  // Automatically open browser
  if let Some(p) = path {
    let _ = open::that(format!("http://127.0.0.1:7777/{}", p));
  } else {
    let _ = open::that("http://127.0.0.1:7777/");
  }

  axum::serve(listener, app).await?;

  Ok(())
}