diff --git a/apps/shinkai-tool-aave-state/src/index.test.ts b/apps/shinkai-tool-aave-state/src/index.test.ts index b9be3ab..054293c 100644 --- a/apps/shinkai-tool-aave-state/src/index.test.ts +++ b/apps/shinkai-tool-aave-state/src/index.test.ts @@ -1,4 +1,3 @@ -import process from 'node:process'; import { run } from './index.ts'; import { expect } from 'jsr:@std/expect'; @@ -10,7 +9,7 @@ Deno.test({ fn: async () => { const result = await run( { - chromePath: process.env?.CHROME_PATH, + chromePath: Deno.env.get('CHROME_PATH'), }, {}, ); diff --git a/apps/shinkai-tool-defillama-tvl-rankings/src/index.test.ts b/apps/shinkai-tool-defillama-tvl-rankings/src/index.test.ts index f027d85..3a2b53a 100644 --- a/apps/shinkai-tool-defillama-tvl-rankings/src/index.test.ts +++ b/apps/shinkai-tool-defillama-tvl-rankings/src/index.test.ts @@ -12,7 +12,7 @@ Deno.test({ fn: async () => { const run_result = await run( { - chromePath: process.env?.CHROME_PATH, + chromePath: Deno.env.get('CHROME_PATH'), }, { top10: false, diff --git a/apps/shinkai-tool-perplexity/src/index.test.ts b/apps/shinkai-tool-perplexity/src/index.test.ts index 40b7a33..5347177 100644 --- a/apps/shinkai-tool-perplexity/src/index.test.ts +++ b/apps/shinkai-tool-perplexity/src/index.test.ts @@ -13,7 +13,7 @@ Deno.test({ fn: async () => { const run_result = await run( { - chromePath: process.env?.CHROME_PATH, + chromePath: Deno.env.get('CHROME_PATH'), }, { query: 'What is the meaning of life?', diff --git a/apps/shinkai-tool-playwright-example/src/index.test.ts b/apps/shinkai-tool-playwright-example/src/index.test.ts index 2402cb6..52715ff 100644 --- a/apps/shinkai-tool-playwright-example/src/index.test.ts +++ b/apps/shinkai-tool-playwright-example/src/index.test.ts @@ -16,7 +16,7 @@ Deno.test({ fn: async () => { const result = await run( { - chromePath: process.env?.CHROME_PATH, + chromePath: Deno.env.get('CHROME_PATH'), }, { url: 'https://shinkai.com' }, ); diff --git a/apps/shinkai-tool-playwright-example/src/index.ts b/apps/shinkai-tool-playwright-example/src/index.ts index 29721b4..af12800 100644 --- a/apps/shinkai-tool-playwright-example/src/index.ts +++ b/apps/shinkai-tool-playwright-example/src/index.ts @@ -1,6 +1,5 @@ import * as playwright from 'npm:playwright@1.48.2'; import chromePaths from 'npm:chrome-paths@1.0.1'; -import process from 'node:process'; type Configurations = { chromePath?: string; @@ -16,7 +15,7 @@ export const run: Run = async ( ): Promise => { const chromePath = configurations?.chromePath || - process.env.CHROME_PATH || + Deno.env.get('CHROME_PATH') || chromePaths.chrome || chromePaths.chromium; diff --git a/libs/shinkai-tools-runner/src/tools/deno_runner.rs b/libs/shinkai-tools-runner/src/tools/deno_runner.rs index a1b9e21..763ff96 100644 --- a/libs/shinkai-tools-runner/src/tools/deno_runner.rs +++ b/libs/shinkai-tools-runner/src/tools/deno_runner.rs @@ -223,6 +223,10 @@ impl DenoRunner { container_envs.push(format!("ASSETS={}", mount_assets_env)); container_envs.push(String::from("-e")); container_envs.push(format!("MOUNT={}", mount_env)); + container_envs.push(String::from("-e")); + container_envs.push(format!("CONTEXT_ID={}", self.options.context.context_id)); + container_envs.push(String::from("-e")); + container_envs.push(format!("EXECUTION_ID={}", self.options.context.execution_id)); if let Some(envs) = envs { for (key, value) in envs { @@ -446,6 +450,9 @@ impl DenoRunner { .join(","), ); + command.env("CONTEXT_ID", self.options.context.context_id.clone()); + command.env("EXECUTION_ID", self.options.context.execution_id.clone()); + if let Some(envs) = envs { command.envs(envs); } diff --git a/libs/shinkai-tools-runner/src/tools/tool.test.rs b/libs/shinkai-tools-runner/src/tools/tool.test.rs index 5fca3be..83fcd4c 100644 --- a/libs/shinkai-tools-runner/src/tools/tool.test.rs +++ b/libs/shinkai-tools-runner/src/tools/tool.test.rs @@ -610,3 +610,49 @@ async fn multiple_file_imports(#[case] runner_type: RunnerType) { assert!(result.is_ok()); assert_eq!(result.unwrap().data, "processed test data"); } + +#[rstest] +#[case::host(RunnerType::Host)] +#[case::docker(RunnerType::Docker)] +#[tokio::test] +async fn context_and_execution_id(#[case] runner_type: RunnerType) { + let _ = env_logger::builder() + .filter_level(log::LevelFilter::Info) + .is_test(true) + .try_init(); + + let context_id = nanoid::nanoid!(); + let execution_id = nanoid::nanoid!(); + + let code = r#" + function run() { + return { + contextId: Deno.env.get("CONTEXT_ID"), + executionId: Deno.env.get("EXECUTION_ID") + }; + } + "#; + + let code_files = CodeFiles { + files: HashMap::from([("main.ts".to_string(), code.to_string())]), + entrypoint: "main.ts".to_string(), + }; + + let tool = Tool::new( + code_files, + Value::Null, + Some(DenoRunnerOptions { + force_runner_type: Some(runner_type), + context: ExecutionContext { + context_id: context_id.clone(), + execution_id: execution_id.clone(), + ..Default::default() + }, + ..Default::default() + }), + ); + let result = tool.run(None, Value::Null, None).await.unwrap(); + + assert_eq!(result.data["contextId"], context_id); + assert_eq!(result.data["executionId"], execution_id); +} diff --git a/scripts/tool-bundler.ts b/scripts/tool-bundler.ts index 236e1e9..6ccfa17 100644 --- a/scripts/tool-bundler.ts +++ b/scripts/tool-bundler.ts @@ -1,14 +1,14 @@ /** * Script to bundle and process Shinkai tools - * + * * This script takes an entry file and output folder as arguments, bundles the tool code, * generates embeddings for the tool definition, and creates an extended tool definition * that includes the code and embedding metadata. - * + * * Usage: * Run with --entry and --outputFolder parameters: * deno run tool-bundler.ts --entry= --outputFolder= - * + * * The script will: * 1. Read and bundle the tool code from the entry file * 2. Write the bundled code to index.ts in the output folder @@ -21,7 +21,6 @@ import { join } from 'node:path'; import minimist from 'npm:minimist'; import fs from 'node:fs'; -import process from 'node:process'; import axios from 'npm:axios'; console.log('🚀 Starting Shinkai Tool bundler...'); @@ -37,9 +36,9 @@ type ExtendedToolDefinition = ToolDefinition & { // Parse command line arguments console.log('📝 Parsing command line arguments...'); -const args = minimist(process.argv.slice(2)); -const entryFile: string = join(process.cwd(), args.entry); -const outputFolder: string = join(process.cwd(), args.outputFolder); +const args = minimist(Deno.args); +const entryFile: string = join(Deno.cwd(), args.entry); +const outputFolder: string = join(Deno.cwd(), args.outputFolder); const outputFile: string = join(outputFolder, 'index.ts'); console.log('📂 Entry file:', entryFile); @@ -79,7 +78,7 @@ fs.promises // Import tool definition from bundled code console.log('📥 Importing tool definition...'); const { definition }: { definition: ToolDefinition } = await import( - process.platform === 'win32' ? `file://${outputFile}` : outputFile + Deno.build.os == 'windows' ? `file://${outputFile}` : outputFile ); console.log('✨ Tool definition loaded:', definition.name); @@ -88,7 +87,7 @@ fs.promises console.log('🧮 Generating embeddings for tool metadata...'); const prompt = `${definition.id} ${definition.name} ${definition.description} ${definition.author} ${definition.keywords.join(' ')}`; const embeddings = await getEmbeddings(prompt); - + // Create extended tool definition with code and embeddings console.log('🔨 Creating extended tool definition...'); const toolDefinition: ExtendedToolDefinition = {