2026-02-19 15:25:22 +00:00
|
|
|
mod agents;
|
2026-02-19 17:58:53 +00:00
|
|
|
mod config;
|
2026-02-16 16:24:21 +00:00
|
|
|
mod http;
|
|
|
|
|
mod io;
|
2026-02-13 12:31:36 +00:00
|
|
|
mod llm;
|
|
|
|
|
mod state;
|
|
|
|
|
mod store;
|
2026-02-19 12:54:04 +00:00
|
|
|
mod workflow;
|
2026-02-19 17:58:53 +00:00
|
|
|
mod worktree;
|
2026-02-13 12:31:36 +00:00
|
|
|
|
2026-02-19 15:25:22 +00:00
|
|
|
use crate::agents::AgentPool;
|
2026-02-16 16:24:21 +00:00
|
|
|
use crate::http::build_routes;
|
|
|
|
|
use crate::http::context::AppContext;
|
2026-02-13 12:31:36 +00:00
|
|
|
use crate::state::SessionState;
|
|
|
|
|
use crate::store::JsonFileStore;
|
2026-02-19 12:54:04 +00:00
|
|
|
use crate::workflow::WorkflowState;
|
2026-02-16 16:24:21 +00:00
|
|
|
use poem::Server;
|
|
|
|
|
use poem::listener::TcpListener;
|
2026-02-19 17:14:33 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2026-02-13 12:31:36 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2026-02-19 17:14:33 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-20 14:11:53 +00:00
|
|
|
/// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 12:31:36 +00:00
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> Result<(), std::io::Error> {
|
|
|
|
|
let app_state = Arc::new(SessionState::default());
|
Accept story 34: Per-Project Agent Configuration and Role Definitions
Replace single [agent] config with multi-agent [[agent]] roster system.
Each agent has name, role, model, allowed_tools, max_turns, max_budget_usd,
and system_prompt fields that map to Claude CLI flags at spawn time.
- AgentConfig expanded with structured fields, validated at startup (panics
on duplicate names, empty names, non-positive budgets/turns)
- Backwards-compatible: legacy [agent] format auto-wraps with deprecation warning
- AgentPool uses composite "story_id:agent_name" keys for concurrent agents
- agent_name added to AgentEvent variants, AgentInfo, start/stop/subscribe APIs
- GET /agents/config returns roster, POST /agents/config/reload hot-reloads
- POST /agents/start accepts optional agent_name, /agents/stop requires it
- SSE route updated to /agents/:story_id/:agent_name/stream
- Frontend: roster badges, agent selector dropdown, composite-key state
- Project root initialized to cwd at startup so config endpoints work immediately
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 18:46:14 +00:00
|
|
|
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
|
2026-02-13 12:31:36 +00:00
|
|
|
let store = Arc::new(
|
|
|
|
|
JsonFileStore::from_path(PathBuf::from("store.json")).map_err(std::io::Error::other)?,
|
|
|
|
|
);
|
2026-02-20 14:11:53 +00:00
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 12:54:04 +00:00
|
|
|
let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default()));
|
2026-02-20 13:24:35 +00:00
|
|
|
let port = resolve_port();
|
|
|
|
|
let agents = Arc::new(AgentPool::new(port));
|
2026-02-13 12:31:36 +00:00
|
|
|
|
|
|
|
|
let ctx = AppContext {
|
|
|
|
|
state: app_state,
|
|
|
|
|
store,
|
2026-02-19 12:54:04 +00:00
|
|
|
workflow,
|
2026-02-19 15:25:22 +00:00
|
|
|
agents,
|
2026-02-13 12:31:36 +00:00
|
|
|
};
|
|
|
|
|
|
2026-02-16 16:24:21 +00:00
|
|
|
let app = build_routes(ctx);
|
2026-02-19 17:14:33 +00:00
|
|
|
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"
|
|
|
|
|
);
|
2026-02-19 17:14:33 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
Accept story 34: Per-Project Agent Configuration and Role Definitions
Replace single [agent] config with multi-agent [[agent]] roster system.
Each agent has name, role, model, allowed_tools, max_turns, max_budget_usd,
and system_prompt fields that map to Claude CLI flags at spawn time.
- AgentConfig expanded with structured fields, validated at startup (panics
on duplicate names, empty names, non-positive budgets/turns)
- Backwards-compatible: legacy [agent] format auto-wraps with deprecation warning
- AgentPool uses composite "story_id:agent_name" keys for concurrent agents
- agent_name added to AgentEvent variants, AgentInfo, start/stop/subscribe APIs
- GET /agents/config returns roster, POST /agents/config/reload hot-reloads
- POST /agents/start accepts optional agent_name, /agents/stop requires it
- SSE route updated to /agents/:story_id/:agent_name/stream
- Frontend: roster badges, agent selector dropdown, composite-key state
- Project root initialized to cwd at startup so config endpoints work immediately
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 18:46:14 +00:00
|
|
|
#[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}"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-19 17:14:33 +00:00
|
|
|
#[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
|
|
|
|
2026-02-19 17:14:33 +00:00
|
|
|
remove_port_file(&path);
|
|
|
|
|
assert!(!path.exists());
|
|
|
|
|
}
|
2026-02-20 14:11:53 +00:00
|
|
|
|
|
|
|
|
#[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));
|
|
|
|
|
}
|
2026-02-13 12:31:36 +00:00
|
|
|
}
|