Skip to content

Commit f9f1b79

Browse files
authored
- feature: execute from code files (#69)
* - feature: execute from code files * - fix: wrong commits * - improve: added test for multiple files
1 parent 0084cb8 commit f9f1b79

File tree

13 files changed

+555
-214
lines changed

13 files changed

+555
-214
lines changed

.github/workflows/pr-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ jobs:
6767
env:
6868
CHROME_PATH: ${{ steps.setup-chrome.outputs.chrome-path }}
6969
EMBEDDING_API_URL : ${{ vars.EMBEDDING_API_URL }}
70-
CI_FORCE_DENO_IN_HOST: true
70+
CI_FORCE_RUNNER_TYPE: host
7171
run: |
7272
npx nx run-many -t test --parallel=false --skip-nx-cache

libs/shinkai-tools-runner/src/built_in_tools.test.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
use std::collections::HashMap;
2+
13
use serde_json::Value;
24

3-
use crate::built_in_tools::{get_tool, get_tools};
5+
use crate::{
6+
built_in_tools::{get_tool, get_tools},
7+
tools::code_files::CodeFiles,
8+
};
49

510
#[tokio::test]
611
async fn get_tools_all_load() {
@@ -11,8 +16,11 @@ async fn get_tools_all_load() {
1116
let tools = get_tools();
1217
for (tool_name, tool_definition) in tools {
1318
println!("creating tool instance for {}", tool_name);
14-
let tool_instance =
15-
crate::tools::tool::Tool::new(tool_definition.code.unwrap(), Value::Null, None);
19+
let code_files = CodeFiles {
20+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.unwrap())]),
21+
entrypoint: "main.ts".to_string(),
22+
};
23+
let tool_instance = crate::tools::tool::Tool::new(code_files, Value::Null, None);
1624
println!("fetching definition for {}", tool_name);
1725
let defintion = tool_instance.definition().await;
1826
println!(

libs/shinkai-tools-runner/src/lib.test.rs

Lines changed: 89 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use std::collections::HashMap;
12
use std::env;
23
use std::time::Duration;
34

45
use serde_json::json;
56

67
use crate::built_in_tools::get_tool;
8+
use crate::tools::code_files::CodeFiles;
79
use crate::tools::tool::Tool;
810

911
#[tokio::test]
@@ -13,11 +15,11 @@ async fn shinkai_tool_echo() {
1315
.is_test(true)
1416
.try_init();
1517
let tool_definition = get_tool("shinkai-tool-echo").unwrap();
16-
let tool = Tool::new(
17-
tool_definition.code.clone().unwrap(),
18-
serde_json::Value::Null,
19-
None,
20-
);
18+
let code_files = CodeFiles {
19+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
20+
entrypoint: "main.ts".to_string(),
21+
};
22+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
2123
let run_result = tool
2224
.run(None, serde_json::json!({ "message": "valparaíso" }), None)
2325
.await
@@ -32,8 +34,12 @@ async fn shinkai_tool_weather_by_city() {
3234
.is_test(true)
3335
.try_init();
3436
let tool_definition = get_tool("shinkai-tool-weather-by-city").unwrap();
37+
let code_files = CodeFiles {
38+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
39+
entrypoint: "main.ts".to_string(),
40+
};
3541
let tool = Tool::new(
36-
tool_definition.code.clone().unwrap(),
42+
code_files,
3743
serde_json::json!({ "apiKey": "63d35ff6068c3103ccd1227526935675" }),
3844
None,
3945
);
@@ -54,7 +60,11 @@ async fn shinkai_tool_inline() {
5460
return { message: `Hello, ${params.name}!` };
5561
}
5662
"#;
57-
let tool = Tool::new(js_code.to_string(), serde_json::Value::Null, None);
63+
let code_files = CodeFiles {
64+
files: HashMap::from([("main.ts".to_string(), js_code.to_string())]),
65+
entrypoint: "main.ts".to_string(),
66+
};
67+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
5868
let run_result = tool
5969
.run(None, serde_json::json!({ "name": "world" }), None)
6070
.await
@@ -73,7 +83,11 @@ async fn shinkai_tool_inline_non_json_return() {
7383
return 5;
7484
}
7585
"#;
76-
let tool = Tool::new(js_code.to_string(), serde_json::Value::Null, None);
86+
let code_files = CodeFiles {
87+
files: HashMap::from([("main.ts".to_string(), js_code.to_string())]),
88+
entrypoint: "main.ts".to_string(),
89+
};
90+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
7791
let run_result = tool.run(None, serde_json::json!({}), None).await.unwrap();
7892
assert_eq!(run_result.data, 5);
7993
}
@@ -85,11 +99,11 @@ async fn shinkai_tool_web3_eth_balance() {
8599
.is_test(true)
86100
.try_init();
87101
let tool_definition = get_tool("shinkai-tool-web3-eth-balance").unwrap();
88-
let tool = Tool::new(
89-
tool_definition.code.clone().unwrap(),
90-
serde_json::Value::Null,
91-
None,
92-
);
102+
let code_files = CodeFiles {
103+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
104+
entrypoint: "main.ts".to_string(),
105+
};
106+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
93107
let run_result = tool
94108
.run(
95109
None,
@@ -108,11 +122,11 @@ async fn shinkai_tool_web3_eth_uniswap() {
108122
.is_test(true)
109123
.try_init();
110124
let tool_definition = get_tool("shinkai-tool-web3-eth-uniswap").unwrap();
111-
let tool = Tool::new(
112-
tool_definition.code.clone().unwrap(),
113-
serde_json::Value::Null,
114-
None,
115-
);
125+
let code_files = CodeFiles {
126+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
127+
entrypoint: "main.ts".to_string(),
128+
};
129+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
116130
let run_result = tool
117131
.run(
118132
None,
@@ -137,11 +151,11 @@ async fn shinkai_tool_download_page() {
137151
.is_test(true)
138152
.try_init();
139153
let tool_definition = get_tool("shinkai-tool-download-pages").unwrap();
140-
let tool = Tool::new(
141-
tool_definition.code.clone().unwrap(),
142-
serde_json::Value::Null,
143-
None,
144-
);
154+
let code_files = CodeFiles {
155+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
156+
entrypoint: "main.ts".to_string(),
157+
};
158+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
145159
let run_result = tool
146160
.run(
147161
None,
@@ -177,7 +191,11 @@ async fn max_execution_time() {
177191
return { data: true };
178192
}
179193
"#;
180-
let tool = Tool::new(js_code.to_string(), serde_json::Value::Null, None);
194+
let code_files = CodeFiles {
195+
files: HashMap::from([("main.ts".to_string(), js_code.to_string())]),
196+
entrypoint: "main.ts".to_string(),
197+
};
198+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
181199
let run_result = tool
182200
.run(
183201
None,
@@ -202,11 +220,14 @@ async fn shinkai_tool_download_page_stack_overflow() {
202220
tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
203221
managed_runtime.block_on(async {
204222
let tool_definition = get_tool("shinkai-tool-download-pages").unwrap();
205-
let tool = Tool::new(
206-
tool_definition.code.clone().unwrap(),
207-
serde_json::Value::Null,
208-
None,
209-
);
223+
let code_files = CodeFiles {
224+
files: HashMap::from([(
225+
"main.ts".to_string(),
226+
tool_definition.code.clone().unwrap(),
227+
)]),
228+
entrypoint: "main.ts".to_string(),
229+
};
230+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
210231
tool.run(
211232
None,
212233
serde_json::json!({
@@ -231,11 +252,11 @@ async fn shinkai_tool_leiden() {
231252
.is_test(true)
232253
.try_init();
233254
let tool_definition = get_tool("shinkai-tool-leiden").unwrap();
234-
let tool = Tool::new(
235-
tool_definition.code.clone().unwrap(),
236-
serde_json::Value::Null,
237-
None,
238-
);
255+
let code_files = CodeFiles {
256+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
257+
entrypoint: "main.ts".to_string(),
258+
};
259+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
239260
let edges = vec![
240261
(2, 1, 1),
241262
(3, 1, 1),
@@ -339,11 +360,11 @@ async fn shinkai_tool_duckduckgo_search() {
339360
.is_test(true)
340361
.try_init();
341362
let tool_definition = get_tool("shinkai-tool-duckduckgo-search").unwrap();
342-
let tool = Tool::new(
343-
tool_definition.code.clone().unwrap(),
344-
serde_json::Value::Null,
345-
None,
346-
);
363+
let code_files = CodeFiles {
364+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
365+
entrypoint: "main.ts".to_string(),
366+
};
367+
let tool = Tool::new(code_files, serde_json::Value::Null, None);
347368
let run_result = tool
348369
.run(
349370
None,
@@ -370,8 +391,12 @@ async fn shinkai_tool_playwright_example() {
370391
.is_test(true)
371392
.try_init();
372393
let tool_definition = get_tool("shinkai-tool-playwright-example").unwrap();
394+
let code_files = CodeFiles {
395+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
396+
entrypoint: "main.ts".to_string(),
397+
};
373398
let tool = Tool::new(
374-
tool_definition.code.clone().unwrap(),
399+
code_files,
375400
serde_json::json!({ "chromePath": std::env::var("CHROME_PATH").ok().unwrap_or("".to_string()) }),
376401
None,
377402
);
@@ -401,8 +426,12 @@ async fn shinkai_tool_defillama_lending_tvl_rankings() {
401426
.is_test(true)
402427
.try_init();
403428
let tool_definition = get_tool("shinkai-tool-defillama-tvl-rankings").unwrap();
429+
let code_files = CodeFiles {
430+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
431+
entrypoint: "main.ts".to_string(),
432+
};
404433
let tool = Tool::new(
405-
tool_definition.code.clone().unwrap(),
434+
code_files,
406435
serde_json::json!({ "chromePath": std::env::var("CHROME_PATH").ok().unwrap_or("".to_string()) }),
407436
None,
408437
);
@@ -421,21 +450,26 @@ async fn shinkai_tool_defillama_lending_tvl_rankings() {
421450
assert_eq!(run_result.unwrap().data["rowsCount"], 43);
422451
}
423452

424-
// TODO: enable this test again when fix the tool
453+
// // TODO: enable this test again when fix the tool
425454
// #[tokio::test]
426455
// async fn shinkai_tool_aave_loan_requester() {
427456
// let _ = env_logger::builder()
428457
// .filter_level(log::LevelFilter::Info)
429458
// .is_test(true)
430459
// .try_init();
431460
// let tool_definition = get_tool("shinkai-tool-aave-loan-requester").unwrap();
461+
// let code_files = CodeFiles {
462+
// files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
463+
// entrypoint: "main.ts".to_string(),
464+
// };
432465
// let tool = Tool::new(
433-
// tool_definition.code.clone().unwrap(),
466+
// code_files,
434467
// serde_json::json!({ "chromePath": std::env::var("CHROME_PATH").ok().unwrap_or("".to_string()) }),
435468
// None,
436469
// );
437470
// let run_result = tool
438471
// .run(
472+
// None,
439473
// serde_json::json!({ "inputValue": "0.005", "assetSymbol": "ETH" }),
440474
// None,
441475
// )
@@ -459,7 +493,11 @@ async fn shinkai_tool_youtube_summary() {
459493
serde_json::json!({ "apiUrl": "http://127.0.0.1:11434", "lang": "en" })
460494
};
461495

462-
let tool = Tool::new(tool_definition.code.clone().unwrap(), configurations, None);
496+
let code_files = CodeFiles {
497+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
498+
entrypoint: "main.ts".to_string(),
499+
};
500+
let tool = Tool::new(code_files, configurations, None);
463501
let run_result = tool
464502
.run(
465503
None,
@@ -481,14 +519,18 @@ async fn shinkai_tool_json_to_md() {
481519
.is_test(true)
482520
.try_init();
483521
let tool_definition = get_tool("shinkai-tool-json-to-md").unwrap();
522+
let code_files = CodeFiles {
523+
files: HashMap::from([("main.ts".to_string(), tool_definition.code.clone().unwrap())]),
524+
entrypoint: "main.ts".to_string(),
525+
};
484526
let tool = Tool::new(
485-
tool_definition.code.clone().unwrap(),
486-
json!({
527+
code_files,
528+
serde_json::json!({
487529
"only_system": false
488530
}),
489531
None,
490532
);
491-
let message = json!({
533+
let message = serde_json::json!({
492534
"relevantSentencesFromText": [
493535
{
494536
"citation_id": 5,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::collections::HashMap;
2+
3+
#[derive(Default, Clone)]
4+
pub struct CodeFiles {
5+
pub files: HashMap<String, String>,
6+
pub entrypoint: String,
7+
}

0 commit comments

Comments
 (0)