Skip to content

Commit

Permalink
- feature: bundle tools-backend macos compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
agallardol committed Aug 19, 2024
1 parent 682cc98 commit 578bbd1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ target
.nx/cache
.nx/workspace-data
libs/shinkai-tools-runner/tools
libs/shinkai-tools-runner/shinka-tools-runner-resources
89 changes: 89 additions & 0 deletions apps/shinkai-tools-backend/bundler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import * as esbuild from 'esbuild';
import * as fs from 'fs';
import { execSync } from 'child_process';

async function bundle() {
try {
console.log('Starting bundling process...');

// Step: Bundle the application using esbuild
console.log('Step: Bundling the application...');
await esbuild.build({
entryPoints: ['./apps/shinkai-tools-backend/src/index.ts'],
bundle: true,
platform: 'node',
target: `node${fs.readFileSync('.nvmrc', 'utf8').trim().replace('v', '')}`,
outfile: './dist/apps/shinkai-tools-backend/index.js',
});
console.log('Application bundled successfully.');

// Step: Copy sea-config.json to the dist folder
console.log('Step: Copying sea-config.json...');
await fs.promises.copyFile(
'./apps/shinkai-tools-backend/sea-config.json',
'./dist/apps/shinkai-tools-backend/sea-config.json',
);
console.log('sea-config.json copied successfully.');

// Step: Generate Single Executable Application (SEA) blob
console.log('Step: Generating SEA blob...');
process.chdir('./dist/apps/shinkai-tools-backend');
execSync('node --experimental-sea-config sea-config.json');
console.log('SEA blob generated successfully.');

// Step: Copy the Node.js executable to create our custom executable
console.log('Step: Copying Node.js executable...');
const nodePath = process.execPath;
fs.copyFileSync(nodePath, 'shinkai-tools-backend');
console.log('Node.js executable copied successfully.');

// Step: Remove code signature on macOS or Windows
// This is necessary because we're modifying the executable
console.log('Step: Removing code signature if necessary...');
if (process.platform === 'darwin') {
await execSync('codesign --remove-signature shinkai-tools-backend');
console.log('Code signature removed successfully on macOS.');
} else if (process.platform === 'win32') {
await execSync('signtool remove /s shinkai-tools-backend');
console.log('Code signature removed successfully on Windows.');
} else {
console.log('Code signature removal not required on this platform.');
}

// Step: Inject the SEA blob into our custom executable
// The command differs slightly between macOS and other platforms
console.log('Step: Injecting SEA blob...');
if (process.platform === 'darwin') {
execSync(
'npx postject shinkai-tools-backend NODE_SEA_BLOB index.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 --macho-segment-name NODE_SEA',
);
} else {
execSync(
'npx postject shinkai-tools-backend NODE_SEA_BLOB index.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2',
);
}
console.log('SEA blob injected successfully.');

// Step: Code sign the executable on macOS
console.log('Step: Code signing the executable on macOS...');
if (process.platform === 'darwin') {
try {
execSync('codesign --sign - shinkai-tools-backend');
console.log('Executable successfully code signed on macOS.');
} catch (error) {
console.error('Failed to code sign the executable:', error);
}
} else {
console.log('Code signing not required on this platform.');
}

console.log('Bundling completed successfully.');
} catch (error) {
console.error('Bundling failed:', error);
process.exit(1);
}
}

// Execute the bundling process
console.log('Initiating bundling process...');
bundle();
9 changes: 1 addition & 8 deletions apps/shinkai-tools-backend/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
"executor": "nx:run-commands",
"defaultConfiguration": "production",
"options": {
"commands": [
"npx esbuild ./apps/shinkai-tools-backend/src/index.ts --bundle --platform=node --target=node22.16 --outfile=./dist/apps/shinkai-tools-backend/index.js",
"npx cpy ./apps/shinkai-tools-backend/sea-config.json ./dist/",
"cd ./dist/apps/shinkai-tools-backend && node --experimental-sea-config sea-config.json",
"cd ./dist/apps/shinkai-tools-backend && node -e \"require('fs').copyFileSync(process.execPath, 'shinkai-tools-backend')\"",
"cd ./dist/apps/shinkai-tools-backend && npx postject shinkai-tools-backend NODE_SEA_BLOB index.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2"
],
"parallel": false
"command": "ts-node apps/shinkai-tools-backend/bundler.ts"
},
"configurations": {
"development": {},
Expand Down
Binary file not shown.
5 changes: 2 additions & 3 deletions libs/shinkai-tools-runner/src/tools/shinkai_tools_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ShinkaiToolsBackend {
let client = reqwest::Client::new();

// Wait for the /health endpoint to respond with 200
let health_check_url = "http://localhost:3000/health";
let health_check_url = "http://127.0.0.1:3000/health";
let mut retries = 5;
while retries > 0 {
match client.get(health_check_url).send().await {
Expand All @@ -42,8 +42,7 @@ impl ShinkaiToolsBackend {
break;
}
Err(e) => {
println!("Health check failed: {}", e);
return Err(std::io::Error::new(std::io::ErrorKind::Other, e));
println!("Health check failed: {}, retrying...", e);
}
_ => {
println!(
Expand Down
2 changes: 1 addition & 1 deletion libs/shinkai-tools-runner/src/tools/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Tool {

pub fn new(code: String, configurations: Value) -> Self {
Tool {
tool_backend_url: "http://localhost:3000".to_string(),
tool_backend_url: "http://127.0.0.1:3000".to_string(),
code,
configurations,
http_client: Self::build_http_client(),
Expand Down

0 comments on commit 578bbd1

Please sign in to comment.