Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- feature: add context envs #74

Merged
merged 12 commits into from
Nov 28, 2024
3 changes: 1 addition & 2 deletions apps/shinkai-tool-aave-state/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import process from 'node:process';
import { run } from './index.ts';
import { expect } from 'jsr:@std/expect';

Expand All @@ -10,7 +9,7 @@ Deno.test({
fn: async () => {
const result = await run(
{
chromePath: process.env?.CHROME_PATH,
chromePath: Deno.env.get('CHROME_PATH'),
},
{},
);
Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-tool-defillama-tvl-rankings/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-tool-perplexity/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?',
Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-tool-playwright-example/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
);
Expand Down
3 changes: 1 addition & 2 deletions apps/shinkai-tool-playwright-example/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as playwright from 'npm:[email protected]';
import chromePaths from 'npm:[email protected]';
import process from 'node:process';

type Configurations = {
chromePath?: string;
Expand All @@ -16,7 +15,7 @@ export const run: Run<Configurations, Parameters, Result> = async (
): Promise<Result> => {
const chromePath =
configurations?.chromePath ||
process.env.CHROME_PATH ||
Deno.env.get('CHROME_PATH') ||
chromePaths.chrome ||
chromePaths.chromium;

Expand Down
7 changes: 7 additions & 0 deletions libs/shinkai-tools-runner/src/tools/deno_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
46 changes: 46 additions & 0 deletions libs/shinkai-tools-runner/src/tools/tool.test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 8 additions & 9 deletions scripts/tool-bundler.ts
Original file line number Diff line number Diff line change
@@ -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=<entry-file> --outputFolder=<output-folder>
*
*
* 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
Expand All @@ -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...');
Expand All @@ -37,9 +36,9 @@ type ExtendedToolDefinition = ToolDefinition<any> & {

// 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);
Expand Down Expand Up @@ -79,7 +78,7 @@ fs.promises
// Import tool definition from bundled code
console.log('📥 Importing tool definition...');
const { definition }: { definition: ToolDefinition<any> } = await import(
process.platform === 'win32' ? `file://${outputFile}` : outputFile
Deno.build.os == 'windows' ? `file://${outputFile}` : outputFile
);

console.log('✨ Tool definition loaded:', definition.name);
Expand All @@ -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 = {
Expand Down
Loading