From 66d005aa3047825e6817824ebd54653e984464dc Mon Sep 17 00:00:00 2001 From: Hare Date: Thu, 9 Apr 2026 04:55:14 +0900 Subject: [PATCH] =?UTF-8?q?Crate=E8=A8=AD=E8=A8=88=E3=83=BBmv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AGENTS.md | 1 + CLAUDE.md | 1 + Cargo.lock | 78 +++++++++++++++++++ Cargo.toml | 2 + crates/insomnia-core/Cargo.toml | 20 +++++ crates/insomnia-core/examples/pod_cli.rs | 69 ++++++++++++++++ crates/{insomnia => insomnia-core}/src/lib.rs | 0 .../src/manifest.rs | 0 crates/{insomnia => insomnia-core}/src/pod.rs | 0 .../src/provider.rs | 0 .../{insomnia => insomnia-core}/src/scope.rs | 0 crates/insomnia-daemon/Cargo.toml | 10 +++ crates/insomnia-daemon/src/lib.rs | 0 crates/insomnia/Cargo.toml | 14 +--- crates/insomnia/src/main.rs | 3 + 15 files changed, 187 insertions(+), 11 deletions(-) create mode 100644 AGENTS.md create mode 100644 CLAUDE.md create mode 100644 crates/insomnia-core/Cargo.toml create mode 100644 crates/insomnia-core/examples/pod_cli.rs rename crates/{insomnia => insomnia-core}/src/lib.rs (100%) rename crates/{insomnia => insomnia-core}/src/manifest.rs (100%) rename crates/{insomnia => insomnia-core}/src/pod.rs (100%) rename crates/{insomnia => insomnia-core}/src/provider.rs (100%) rename crates/{insomnia => insomnia-core}/src/scope.rs (100%) create mode 100644 crates/insomnia-daemon/Cargo.toml create mode 100644 crates/insomnia-daemon/src/lib.rs create mode 100644 crates/insomnia/src/main.rs diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..815752b8 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1 @@ +設計指針を固め、全体の設計を進めるために、全体の俯瞰と細かいディテールを往復している。 diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..815752b8 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1 @@ +設計指針を固め、全体の設計を進めるために、全体の俯瞰と細かいディテールを往復している。 diff --git a/Cargo.lock b/Cargo.lock index de6d211b..09cd91c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -682,6 +682,16 @@ dependencies = [ name = "insomnia" version = "0.1.0" dependencies = [ + "insomnia-core", + "insomnia-daemon", + "tokio", +] + +[[package]] +name = "insomnia-core" +version = "0.1.0" +dependencies = [ + "dotenv", "llm-worker", "llm-worker-persistence", "serde", @@ -692,6 +702,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "insomnia-daemon" +version = "0.1.0" +dependencies = [ + "insomnia-core", + "llm-worker-persistence", + "tokio", +] + [[package]] name = "ipnet" version = "2.12.0" @@ -809,6 +828,15 @@ dependencies = [ "uuid", ] +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.29" @@ -939,6 +967,29 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1000,6 +1051,15 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + [[package]] name = "ref-cast" version = "1.0.25" @@ -1177,6 +1237,12 @@ dependencies = [ "syn", ] +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "security-framework" version = "3.7.0" @@ -1293,6 +1359,16 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + [[package]] name = "slab" version = "0.4.12" @@ -1440,7 +1516,9 @@ dependencies = [ "bytes", "libc", "mio", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.61.2", diff --git a/Cargo.toml b/Cargo.toml index 089a6887..a5aee71a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ resolver = "2" members = [ "crates/insomnia", + "crates/insomnia-core", + "crates/insomnia-daemon", "crates/llm-worker", "crates/llm-worker-macros", "crates/llm-worker-persistence", diff --git a/crates/insomnia-core/Cargo.toml b/crates/insomnia-core/Cargo.toml new file mode 100644 index 00000000..c516301b --- /dev/null +++ b/crates/insomnia-core/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "insomnia-core" +version = "0.1.0" +edition.workspace = true +license.workspace = true + +[dependencies] +llm-worker = { path = "../llm-worker" } +llm-worker-persistence = { path = "../llm-worker-persistence" } +serde = { version = "1.0", features = ["derive"] } +toml = "0.8" +uuid = { version = "1", features = ["v7", "serde"] } +thiserror = "2.0" +tokio = { version = "1.49", features = ["fs"] } + +[dev-dependencies] +tokio = { version = "1.49", features = ["macros", "rt-multi-thread"] } +tempfile = "3.24" +dotenv = "0.15" +llm-worker-persistence = { path = "../llm-worker-persistence" } diff --git a/crates/insomnia-core/examples/pod_cli.rs b/crates/insomnia-core/examples/pod_cli.rs new file mode 100644 index 00000000..aa98da64 --- /dev/null +++ b/crates/insomnia-core/examples/pod_cli.rs @@ -0,0 +1,69 @@ +//! Minimal example: Pod running a single prompt with persistence. +//! +//! Demonstrates the core insomnia abstraction — a TOML manifest drives +//! provider selection, model config, and system prompt, while FsStore +//! persists the session to disk automatically. +//! +//! ## Usage +//! +//! ```bash +//! echo "ANTHROPIC_API_KEY=your-key" > .env +//! cargo run -p insomnia-core --example pod_cli +//! ``` + +use insomnia_core::{Pod, PodManifest, PodRunResult}; +use llm_worker_persistence::FsStore; + +const MANIFEST_TOML: &str = r#" +[pod] +name = "hello-pod" + +[provider] +kind = "anthropic" +model = "claude-sonnet-4-20250514" +api_key_env = "ANTHROPIC_API_KEY" + +[worker] +system_prompt = "You are a concise assistant. Reply in one or two sentences." +max_tokens = 256 +"#; + +#[tokio::main] +async fn main() -> Result<(), Box> { + dotenv::dotenv().ok(); + + // 1. Parse the manifest + let manifest = PodManifest::from_toml(MANIFEST_TOML)?; + println!("Pod: {}", manifest.pod.name); + + // 2. Create a persistent store (temp dir for demo) + let tmp = tempfile::tempdir()?; + let store = FsStore::new(tmp.path()).await?; + + // 3. Build the Pod from manifest + let mut pod = Pod::from_manifest(manifest, store, None).await?; + println!("Session: {}", pod.session_id()); + + // 4. Run a prompt + let result = pod.run("What is the capital of France?").await?; + match result { + PodRunResult::Finished => println!("(finished)"), + PodRunResult::Paused => println!("(paused)"), + } + + // 5. Extract the assistant's reply from history + let history = pod.session_mut().worker.history(); + if let Some(text) = history + .iter() + .rev() + .find(|item| item.is_assistant_message()) + .and_then(|item| item.as_text()) + { + println!("\nAssistant: {text}"); + } + + // 6. Session ID for potential restore + println!("\nSession ID: {}", pod.session_id()); + + Ok(()) +} diff --git a/crates/insomnia/src/lib.rs b/crates/insomnia-core/src/lib.rs similarity index 100% rename from crates/insomnia/src/lib.rs rename to crates/insomnia-core/src/lib.rs diff --git a/crates/insomnia/src/manifest.rs b/crates/insomnia-core/src/manifest.rs similarity index 100% rename from crates/insomnia/src/manifest.rs rename to crates/insomnia-core/src/manifest.rs diff --git a/crates/insomnia/src/pod.rs b/crates/insomnia-core/src/pod.rs similarity index 100% rename from crates/insomnia/src/pod.rs rename to crates/insomnia-core/src/pod.rs diff --git a/crates/insomnia/src/provider.rs b/crates/insomnia-core/src/provider.rs similarity index 100% rename from crates/insomnia/src/provider.rs rename to crates/insomnia-core/src/provider.rs diff --git a/crates/insomnia/src/scope.rs b/crates/insomnia-core/src/scope.rs similarity index 100% rename from crates/insomnia/src/scope.rs rename to crates/insomnia-core/src/scope.rs diff --git a/crates/insomnia-daemon/Cargo.toml b/crates/insomnia-daemon/Cargo.toml new file mode 100644 index 00000000..1f87257c --- /dev/null +++ b/crates/insomnia-daemon/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "insomnia-daemon" +version = "0.1.0" +edition.workspace = true +license.workspace = true + +[dependencies] +insomnia-core = { path = "../insomnia-core" } +llm-worker-persistence = { path = "../llm-worker-persistence" } +tokio = { version = "1.49", features = ["full"] } diff --git a/crates/insomnia-daemon/src/lib.rs b/crates/insomnia-daemon/src/lib.rs new file mode 100644 index 00000000..e69de29b diff --git a/crates/insomnia/Cargo.toml b/crates/insomnia/Cargo.toml index f5ae8066..a10bb47f 100644 --- a/crates/insomnia/Cargo.toml +++ b/crates/insomnia/Cargo.toml @@ -5,14 +5,6 @@ edition.workspace = true license.workspace = true [dependencies] -llm-worker = { path = "../llm-worker" } -llm-worker-persistence = { path = "../llm-worker-persistence" } -serde = { version = "1.0", features = ["derive"] } -toml = "0.8" -uuid = { version = "1", features = ["v7", "serde"] } -thiserror = "2.0" -tokio = { version = "1.49", features = ["fs"] } - -[dev-dependencies] -tokio = { version = "1.49", features = ["macros", "rt-multi-thread"] } -tempfile = "3.24" +insomnia-core = { path = "../insomnia-core" } +insomnia-daemon = { path = "../insomnia-daemon" } +tokio = { version = "1.49", features = ["full"] } diff --git a/crates/insomnia/src/main.rs b/crates/insomnia/src/main.rs new file mode 100644 index 00000000..a9afc230 --- /dev/null +++ b/crates/insomnia/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("insomnia"); +}