Files
storkit/server/src/main.rs

212 lines
6.1 KiB
Rust
Raw Normal View History

mod agents;
mod config;
2026-02-16 16:24:21 +00:00
mod http;
mod io;
mod llm;
mod state;
mod store;
mod workflow;
mod worktree;
use crate::agents::AgentPool;
2026-02-16 16:24:21 +00:00
use crate::http::build_routes;
use crate::http::context::AppContext;
use crate::state::SessionState;
use crate::store::JsonFileStore;
use crate::workflow::WorkflowState;
2026-02-16 16:24:21 +00:00
use poem::Server;
use poem::listener::TcpListener;
use std::path::{Path, PathBuf};
use std::sync::Arc;
const DEFAULT_PORT: u16 = 3001;
fn parse_port(value: Option<String>) -> u16 {
value
.and_then(|v| v.parse::<u16>().ok())
.unwrap_or(DEFAULT_PORT)
}
fn resolve_port() -> u16 {
parse_port(std::env::var("STORYKIT_PORT").ok())
}
fn write_port_file(dir: &Path, port: u16) -> Option<PathBuf> {
let path = dir.join(".story_kit_port");
std::fs::write(&path, port.to_string()).ok()?;
Some(path)
}
fn remove_port_file(path: &Path) {
let _ = std::fs::remove_file(path);
}
/// Walk from `start` up through parent directories, returning the first
/// directory that contains a `.story_kit/` subdirectory, or `None`.
fn find_story_kit_root(start: &Path) -> Option<PathBuf> {
let mut current = start.to_path_buf();
loop {
if current.join(".story_kit").is_dir() {
return Some(current);
}
if !current.pop() {
return None;
}
}
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let app_state = Arc::new(SessionState::default());
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let store = Arc::new(
JsonFileStore::from_path(PathBuf::from("store.json")).map_err(std::io::Error::other)?,
);
// Auto-detect a .story_kit/ project in cwd or parent directories.
if let Some(project_root) = find_story_kit_root(&cwd) {
io::fs::open_project(
project_root.to_string_lossy().to_string(),
&app_state,
store.as_ref(),
)
.await
.unwrap_or_else(|e| {
eprintln!("Warning: failed to auto-open project at {project_root:?}: {e}");
project_root.to_string_lossy().to_string()
});
// Validate agent config for the detected project root.
config::ProjectConfig::load(&project_root)
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
} else {
// No .story_kit/ found — fall back to cwd so existing behaviour is preserved.
*app_state.project_root.lock().unwrap() = Some(cwd.clone());
}
let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default()));
let port = resolve_port();
let agents = Arc::new(AgentPool::new(port));
let ctx = AppContext {
state: app_state,
store,
workflow,
agents,
};
2026-02-16 16:24:21 +00:00
let app = build_routes(ctx);
let addr = format!("127.0.0.1:{port}");
2026-02-16 17:10:23 +00:00
println!(
"\x1b[95;1m ____ _ _ ___ _ \n / ___|| |_ ___ _ __| | _|_ _| |_ \n \\___ \\| __/ _ \\| '__| |/ /| || __|\n ___) | || (_) | | | < | || |_ \n |____/ \\__\\___/|_| |_|\\_\\___|\\__|\n\x1b[0m"
);
println!("STORYKIT_PORT={port}");
println!("\x1b[96;1mFrontend:\x1b[0m \x1b[94mhttp://{addr}\x1b[0m");
println!("\x1b[92;1mOpenAPI Docs:\x1b[0m \x1b[94mhttp://{addr}/docs\x1b[0m");
let port_file = write_port_file(&cwd, port);
let result = Server::new(TcpListener::bind(&addr)).run(app).await;
if let Some(ref path) = port_file {
remove_port_file(path);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_port_defaults_to_3001() {
assert_eq!(parse_port(None), 3001);
}
#[test]
fn parse_port_reads_valid_value() {
assert_eq!(parse_port(Some("4200".to_string())), 4200);
}
#[test]
fn parse_port_ignores_invalid_value() {
assert_eq!(parse_port(Some("not_a_number".to_string())), 3001);
}
#[test]
#[should_panic(expected = "Invalid project.toml: Duplicate agent name")]
fn panics_on_duplicate_agent_names() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
std::fs::create_dir_all(&sk).unwrap();
std::fs::write(
sk.join("project.toml"),
r#"
[[agent]]
name = "coder"
[[agent]]
name = "coder"
"#,
)
.unwrap();
config::ProjectConfig::load(tmp.path())
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
}
#[test]
fn write_and_remove_port_file() {
let tmp = tempfile::tempdir().unwrap();
let path = write_port_file(tmp.path(), 4567).expect("should write port file");
assert_eq!(std::fs::read_to_string(&path).unwrap(), "4567");
2026-02-16 17:10:23 +00:00
remove_port_file(&path);
assert!(!path.exists());
}
#[test]
fn find_story_kit_root_returns_cwd_when_story_kit_in_cwd() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
let result = find_story_kit_root(tmp.path());
assert_eq!(result, Some(tmp.path().to_path_buf()));
}
#[test]
fn find_story_kit_root_returns_parent_when_story_kit_in_parent() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
let child = tmp.path().join("subdir").join("nested");
std::fs::create_dir_all(&child).unwrap();
let result = find_story_kit_root(&child);
assert_eq!(result, Some(tmp.path().to_path_buf()));
}
#[test]
fn find_story_kit_root_returns_none_when_no_story_kit() {
let tmp = tempfile::tempdir().unwrap();
// No .story_kit/ created
let result = find_story_kit_root(tmp.path());
assert_eq!(result, None);
}
#[test]
fn find_story_kit_root_prefers_nearest_ancestor() {
// If both cwd and a parent have .story_kit/, return cwd (nearest).
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".story_kit")).unwrap();
let child = tmp.path().join("inner");
std::fs::create_dir_all(child.join(".story_kit")).unwrap();
let result = find_story_kit_root(&child);
assert_eq!(result, Some(child));
}
}