Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/capsule-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/capsule-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "capsule-run"
version = "0.8.8"
version = "0.8.9"
edition = "2024"
description = "Secure WASM runtime to isolate and manage AI agent tasks"
license = "Apache-2.0"
Expand All @@ -16,7 +16,7 @@ path = "src/main.rs"

[dependencies]
clap = { version = "4.5.53", features = ["derive"] }
capsule-core = { version= "0.8.8", path = "../capsule-core" }
capsule-core = { version= "0.8.9", path = "../capsule-core" }
tokio = { version = "1.48.0", features = ["rt", "rt-multi-thread", "macros", "io-util", "sync"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
10 changes: 5 additions & 5 deletions crates/capsule-cli/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@capsule-run/cli",
"version": "0.8.8",
"version": "0.8.9",
"description": "Secure WASM runtime to isolate and manage AI agent tasks",
"bin": {
"capsule": "./bin/capsule.js"
Expand Down Expand Up @@ -29,9 +29,9 @@
"node": ">=18"
},
"optionalDependencies": {
"@capsule-run/cli-darwin-arm64": "0.8.8",
"@capsule-run/cli-darwin-x64": "0.8.8",
"@capsule-run/cli-linux-x64": "0.8.8",
"@capsule-run/cli-win32-x64": "0.8.8"
"@capsule-run/cli-darwin-arm64": "0.8.9",
"@capsule-run/cli-darwin-x64": "0.8.9",
"@capsule-run/cli-linux-x64": "0.8.9",
"@capsule-run/cli-win32-x64": "0.8.9"
}
}
2 changes: 1 addition & 1 deletion crates/capsule-cli/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "maturin"

[project]
name = "capsule-run"
version = "0.8.8"
version = "0.8.9"
description = "Secure WASM runtime to isolate and manage AI agent tasks"
readme = "docs/README-pypi.md"
license = {text = "Apache-2.0"}
Expand Down
6 changes: 3 additions & 3 deletions crates/capsule-cli/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use capsule_core::wasm::compiler::python::{PythonWasmCompiler, PythonWasmCompile
use capsule_core::wasm::runtime::Runtime;
use capsule_core::wasm::runtime::RuntimeConfig;
use capsule_core::wasm::runtime::WasmRuntimeError;
use capsule_core::wasm::utilities::task_reporter::TaskReporter;
use capsule_core::wasm::utilities::task_reporter::{LogLevel, TaskReporter};

pub enum BuildError {
IoError(String),
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn compile_to_wasm(file_path: &Path, export: bool) -> Result<CompileResult,

pub async fn execute(file_path: Option<&Path>, export: bool) -> Result<PathBuf, BuildError> {
let manifest = Manifest::new()?;
let mut reporter = TaskReporter::new(true);
let mut reporter = TaskReporter::new(LogLevel::Verbose);

let file_path: PathBuf = match file_path {
Some(path) => path.to_path_buf(),
Expand All @@ -118,7 +118,7 @@ pub async fn execute(file_path: Option<&Path>, export: bool) -> Result<PathBuf,

let runtime_config = RuntimeConfig {
cache_dir: compile_result.cache_dir,
verbose: true,
log_level: LogLevel::Verbose,
};
let runtime = Runtime::new(runtime_config, manifest.capsule_toml)?;
let cwasm_path = runtime.precompile(&compile_result.wasm_path)?;
Expand Down
33 changes: 18 additions & 15 deletions crates/capsule-cli/src/commands/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use capsule_core::wasm::commands::create::CreateInstance;
use capsule_core::wasm::commands::run::RunInstance;
use capsule_core::wasm::execution_policy::{Compute, ExecutionPolicy};
use capsule_core::wasm::runtime::{Runtime, RuntimeConfig, WasmRuntimeError};
use capsule_core::wasm::utilities::task_reporter::TaskReporter;
use capsule_core::wasm::utilities::task_reporter::{LogLevel, TaskReporter};

use crate::commands::shared::load_env_variables;

Expand Down Expand Up @@ -50,16 +50,6 @@ pub async fn execute(
mounts: Vec<String>,
json: bool,
verbose: bool,
) -> Result<String, ExecError> {
execute_with_runtime(wasm_path, args, mounts, json, verbose, None).await
}

pub async fn execute_with_runtime(
wasm_path: &Path,
args: Vec<String>,
mounts: Vec<String>,
json: bool,
verbose: bool,
shared_runtime: Option<Arc<Runtime>>,
) -> Result<String, ExecError> {
let ext = wasm_path.extension().and_then(|e| e.to_str()).unwrap_or("");
Expand All @@ -78,7 +68,15 @@ pub async fn execute_with_runtime(
)));
}

let mut reporter = TaskReporter::new(!json);
let log_level = if json {
LogLevel::Silent
} else if verbose {
LogLevel::Verbose
} else {
LogLevel::Normal
};

let mut reporter = TaskReporter::new(log_level.clone());

let wasm_path_abs = wasm_path
.canonicalize()
Expand All @@ -102,11 +100,16 @@ pub async fn execute_with_runtime(
Some(r) => r,
None => {
reporter.start_progress("Initializing runtime");

let capsule_toml = Manifest::new().map(|m| m.capsule_toml).unwrap_or_default();
let runtime_config = RuntimeConfig { cache_dir, verbose };
let r = Runtime::new(runtime_config, capsule_toml)?;
let runtime_config = RuntimeConfig {
cache_dir,
log_level,
};
let runtime = Runtime::new(runtime_config, capsule_toml)?;

reporter.finish_progress(Some("Runtime ready"));
r
runtime
}
};

Expand Down
100 changes: 79 additions & 21 deletions crates/capsule-cli/src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Arc;
Expand All @@ -9,7 +10,8 @@ use capsule_core::wasm::commands::run::RunInstance;
use capsule_core::wasm::execution_policy::{Compute, ExecutionPolicy};
use capsule_core::wasm::runtime::{Runtime, RuntimeConfig, WasmRuntimeError};
use capsule_core::wasm::utilities::task_config::TaskConfig;
use capsule_core::wasm::utilities::task_reporter::TaskReporter;
use capsule_core::wasm::utilities::task_reporter::{LogLevel, TaskReporter};
use tokio::sync::Mutex;

use crate::build::{BuildError, TaskRegistry, compile_to_wasm};
use crate::commands::shared::load_env_variables;
Expand Down Expand Up @@ -63,35 +65,88 @@ fn extract_main_execution_policy(
Some(task_config.to_execution_policy(capsule_toml))
}

pub async fn execute(
file_path: Option<&Path>,
args: Vec<String>,
mounts: Vec<String>,
json: bool,
verbose: bool,
) -> Result<String, RunError> {
execute_with_runtime(file_path, args, mounts, json, verbose, None).await
async fn resolve_wasm(
file_path: &Path,
reporter: &mut TaskReporter,
wasm_cache: Option<Arc<Mutex<HashMap<String, PathBuf>>>>,
) -> Result<(PathBuf, Option<TaskRegistry>, PathBuf), RunError> {
if let Some(cache) = wasm_cache {
let cache_key = file_path.to_string_lossy().to_string();

if let Some(wasm_path) = cache.lock().await.get(&cache_key).cloned() {
let cache_dir = std::env::current_dir().unwrap_or_default().join(".capsule");
return Ok((wasm_path, None, cache_dir));
}

reporter.start_progress("Preparing environment");

let compile_result = tokio::task::spawn_blocking({
let file_path = file_path.to_path_buf();
move || compile_to_wasm(&file_path, false)
})
.await
.map_err(|e| RunError::IoError(e.to_string()))??;

reporter.finish_progress(Some("Environment ready"));

cache
.lock()
.await
.insert(cache_key, compile_result.wasm_path.clone());

Ok((
compile_result.wasm_path,
compile_result.task_registry,
compile_result.cache_dir,
))
} else {
reporter.start_progress("Preparing environment");

let compile_result = tokio::task::spawn_blocking({
let file_path = file_path.to_path_buf();
move || compile_to_wasm(&file_path, false)
})
.await
.map_err(|e| RunError::IoError(e.to_string()))??;

reporter.finish_progress(Some("Environment ready"));

Ok((
compile_result.wasm_path,
compile_result.task_registry,
compile_result.cache_dir,
))
}
}

pub async fn execute_with_runtime(
pub async fn execute(
file_path: Option<&Path>,
args: Vec<String>,
mounts: Vec<String>,
json: bool,
verbose: bool,
shared_runtime: Option<Arc<Runtime>>,
wasm_cache: Option<Arc<Mutex<HashMap<String, PathBuf>>>>,
) -> Result<String, RunError> {
let manifest = Manifest::new()?;
let mut reporter = TaskReporter::new(!json);

let log_level = if json {
LogLevel::Silent
} else if verbose {
LogLevel::Verbose
} else {
LogLevel::Normal
};

let mut reporter = TaskReporter::new(log_level.clone());

let file_path: PathBuf = match file_path {
Some(path) => path.to_path_buf(),
None => PathBuf::from(manifest.get_entrypoint()?),
};

reporter.start_progress("Preparing environment");
let compile_result = compile_to_wasm(&file_path, false)?;
reporter.finish_progress(Some("Environment ready"));
let (wasm_path, task_registry, cache_dir) =
resolve_wasm(&file_path, &mut reporter, wasm_cache).await?;

let project_root = std::env::current_dir().unwrap_or_else(|_| {
file_path
Expand All @@ -103,26 +158,29 @@ pub async fn execute_with_runtime(

load_env_variables(&project_root).map_err(RunError::IoError)?;

let mut execution_policy =
extract_main_execution_policy(compile_result.task_registry, &manifest.capsule_toml)
.unwrap_or_else(|| ExecutionPolicy::default().compute(Some(Compute::Custom(u64::MAX))));
let mut execution_policy = extract_main_execution_policy(task_registry, &manifest.capsule_toml)
.unwrap_or_else(|| ExecutionPolicy::default().compute(Some(Compute::Custom(u64::MAX))));

execution_policy.mounts.extend(mounts);

let runtime = match shared_runtime {
Some(r) => r,
None => {
reporter.start_progress("Initializing runtime");

let runtime_config = RuntimeConfig {
cache_dir: compile_result.cache_dir,
verbose,
cache_dir,
log_level,
};
Runtime::new(runtime_config, manifest.capsule_toml)?
let runtime = Runtime::new(runtime_config, manifest.capsule_toml)?;

reporter.finish_progress(Some("Runtime ready"));
runtime
}
};

let create_instance_command = CreateInstance::new(execution_policy.clone(), args.clone())
.wasm_path(compile_result.wasm_path)
.wasm_path(wasm_path)
.project_root(project_root);

let (store, instance, task_id) = runtime.execute(create_instance_command).await?;
Expand Down
Loading
Loading