Skip to content

Commit

Permalink
- feature: add copy assets to shinkai-tools-runner lib
Browse files Browse the repository at this point in the history
  • Loading branch information
agallardol committed Aug 27, 2024
1 parent a09bad5 commit 979964a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 123 deletions.
1 change: 1 addition & 0 deletions libs/shinkai-tools-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tokio-util = "0.7.11"
derive_more = "0.99.18"
derivative = "2.2.0"
dispose = "0.5.0"
zip = "2.2.0"

[build-dependencies]
copy_to_output = "2.2.0"
Expand Down
119 changes: 0 additions & 119 deletions libs/shinkai-tools-runner/build.rs

This file was deleted.

2 changes: 1 addition & 1 deletion libs/shinkai-tools-runner/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"outputs": ["{options.outputPath}"],
"defaultConfiguration": "production",
"options": {
"command": "cargo publish -p shinkai_tools_runner --allow-dirty --no-verify"
"command": "cargo publish -p shinkai_tools_runner --allow-dirty"
},
"configurations": {
"development": {},
Expand Down
148 changes: 148 additions & 0 deletions libs/shinkai-tools-runner/src/copy_assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;

pub fn add_exec_permissions(binary_path: &PathBuf) -> Result<(), std::io::Error> {
if !cfg!(target_os = "windows") {
let output = std::process::Command::new("chmod")
.arg("+x")
.arg(binary_path)
.output()
.expect("failed to execute chmod command");
if !output.status.success() {
panic!(
"chmod command failed with status {}: {}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
} else {
println!(
"successfully set executable permissions on backend binary {}",
binary_path.display()
);
}
}
Ok(())
}

pub fn get_shinkai_tools_binary_name() -> String {
if cfg!(target_os = "windows") {
"shinkai-tools-backend.exe".to_string()
} else {
"shinkai-tools-backend".to_string()
}
}

pub fn get_source_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).to_path_buf()
}

pub fn get_target_path() -> PathBuf {
let manifest_dir_string = env::var("CARGO_MANIFEST_DIR").unwrap();
let build_type = env::var("PROFILE").unwrap();
let path = Path::new(&manifest_dir_string)
.join("target")
.join(build_type);
path
}

pub fn download_shinkai_tools_backend_binary(
version: String,
target_path: PathBuf,
) -> Result<(), std::io::Error> {
let client = reqwest::blocking::Client::new();
let arch = if cfg!(target_os = "linux") {
"x86_64-unknown-linux-gnu"
} else if cfg!(target_os = "macos") {
"aarch64-apple-darwin"
} else if cfg!(target_os = "windows") {
"x86_64-pc-windows-msvc"
} else {
panic!("unsupported target OS");
};
let url = format!(
"https://download.shinkai.com/shinkai-tools-backend/binaries/production/{}/{}.zip",
arch, version
);

let mut response = client
.get(&url)
.send()
.expect("failed to download backend binary");
if !response.status().is_success() {
panic!("failed to download backend binary: {}", response.status());
}

let mut file = fs::File::create(&target_path).expect("failed to create backend binary file");
response
.copy_to(&mut file)
.expect("failed to write backend binary to file");
file.flush().expect("failed to flush backend binary file");
Ok(())
}

pub fn copy_assets(
version: &str,
source_path: Option<PathBuf>,
target_path: Option<PathBuf>,
) -> Result<(), std::io::Error> {
let resources_folder_name = "shinkai-tools-runner-resources";
let source_path =
source_path.unwrap_or_else(get_source_path).join(resources_folder_name);
let target_path = target_path.unwrap_or_else(get_target_path).join(resources_folder_name);

println!("resources path: {}", source_path.display());
println!("target path: {}", target_path.display());

let shinkai_tools_backend_binary_name = get_shinkai_tools_binary_name();
let shinkai_tools_backend_source_path =
source_path.join(shinkai_tools_backend_binary_name.clone());
let shinkai_tools_backend_target_path =
target_path.join(shinkai_tools_backend_binary_name.clone());
println!(
"shinkai-tools-backend source path: {}",
shinkai_tools_backend_source_path.display()
);
println!(
"shinkai-tools-backend target path: {}",
shinkai_tools_backend_target_path.display()
);
fs::create_dir_all(&source_path).unwrap_or_else(|err| {
panic!("failed to create resources directory: {}", err);
});
fs::create_dir_all(&target_path).unwrap_or_else(|err| {
panic!("failed to create target directory: {}", err);
});

let zipped_file = source_path.join(format!("shinkai-tools-backend-{}.zip", version));
if !zipped_file.exists() {
download_shinkai_tools_backend_binary(version.to_string(), zipped_file.clone())?;
}

// Unzip the downloaded file
let zip_file = std::fs::File::open(&zipped_file).expect("failed to read zipped binary");
let mut archive = zip::ZipArchive::new(zip_file).expect("failed to open zip archive");
archive
.extract(&source_path)
.expect("failed to extract zip archive");

fs::copy(
&shinkai_tools_backend_source_path,
&shinkai_tools_backend_target_path,
)
.unwrap_or_else(|err| {
panic!(
"failed to copy downloaded backend binary to target path: {}",
err
);
});
println!(
"downloaded and copied backend binary to target path {}",
shinkai_tools_backend_target_path.display()
);

add_exec_permissions(&shinkai_tools_backend_target_path)?;
Ok(())
}
1 change: 1 addition & 0 deletions libs/shinkai-tools-runner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod tools;
pub mod copy_assets;

#[cfg(feature = "built-in-tools")]
pub mod built_in_tools;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shinkai_protocol/source",
"version": "0.7.3",
"version": "0.7.4",
"description": "This repository serves as the ecosystem to execute Shinkai tools, provided by the Shinkai team or third-party developers, in a secure environment. It provides a sandboxed space for executing these tools, ensuring that they run safely and efficiently, while also allowing for seamless integration with Rust code.",
"main": "index.js",
"author": "",
Expand Down

0 comments on commit 979964a

Please sign in to comment.