MCPcopy Create free account
hub / github.com/AI45Lab/Code / run_embedded_script

Function run_embedded_script

core/src/tools/program_tool.rs:371–414  ·  view source on GitHub ↗
(
    source: String,
    inputs: serde_json::Value,
    state: Arc<Mutex<ScriptVmState>>,
    timeout_ms: u64,
)

Source from the content-addressed store, hash-verified

369}
370
371async fn run_embedded_script(
372 source: String,
373 inputs: serde_json::Value,
374 state: Arc<Mutex<ScriptVmState>>,
375 timeout_ms: u64,
376) -> Result<serde_json::Value> {
377 let runtime = AsyncRuntime::new()?;
378 let started = Instant::now();
379 runtime
380 .set_interrupt_handler(Some(Box::new(move || {
381 started.elapsed() >= Duration::from_millis(timeout_ms)
382 })))
383 .await;
384 runtime.set_memory_limit(64 * 1024 * 1024).await;
385 runtime.set_max_stack_size(512 * 1024).await;
386
387 let context = AsyncContext::full(&runtime).await?;
388 let inputs_json = serde_json::to_string(&inputs)?;
389 let script = format!("{}\n{}", embedded_script_bootstrap(&inputs_json), source);
390 let result_json = async_with!(context => |ctx| {
391 let state = Arc::clone(&state);
392 let host_tool = move |tool: String, args_json: String| {
393 let state = Arc::clone(&state);
394 async move { execute_host_tool_json(state, tool, args_json).await }
395 };
396 if let Err(err) = ctx.globals().set("__a3sHostTool", Func::from(Async(host_tool))) {
397 return Err(format!("failed to install program host tool: {err}"));
398 }
399 let promise: Promise = match ctx.eval(script) {
400 Ok(promise) => promise,
401 Err(err) => return Err(format!("failed to evaluate program script: {err}")),
402 };
403 promise
404 .into_future::<String>()
405 .await
406 .catch(&ctx)
407 .map_err(|err| err.to_string())
408 })
409 .await
410 .map_err(anyhow::Error::msg)?;
411
412 serde_json::from_str(&result_json)
413 .map_err(|err| anyhow!("program script returned invalid JSON: {err}"))
414}
415
416struct ScriptVmState {
417 registry: Arc<ToolRegistry>,

Callers 1

run_quickjs_scriptFunction · 0.85

Calls 1

nowFunction · 0.85

Tested by

no test coverage detected