iron-core 0.1.34

Core AgentIron loop, session state, and tool registry
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use crate::config::EmbeddedPythonConfig;
use crate::embedded_python::convert::{json_to_monty, make_iron_exception, monty_to_json};
use crate::embedded_python::types::{
    ChildCallOutcome, ChildCallStatus, ScriptError, ScriptExecStatus, ScriptInput, ScriptOutput,
};
use crate::embedded_python::ToolCatalog;
use crate::tool::ToolRegistry;
use monty::{
    ExcType, ExtFunctionResult, FunctionCall, LimitedTracker, MontyException, MontyObject,
    MontyRun, NameLookupResult, PrintWriter, ResourceLimits, RunProgress,
};
use parking_lot::Mutex;
use serde_json::{Map, Value};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

pub type ToolExecutorFn =
    dyn Fn(&str, &str, Value) -> (ChildCallStatus, Option<Value>) + Send + Sync;

pub struct ScriptRun {
    pub input: ScriptInput,
    pub config: EmbeddedPythonConfig,
    cancel_token: Arc<AtomicBool>,
    child_outcomes: Arc<Mutex<Vec<ChildCallOutcome>>>,
    tool_executor: Option<Arc<ToolExecutorFn>>,
    tool_catalog: ToolCatalog,
}

impl ScriptRun {
    pub fn new(
        input: ScriptInput,
        config: &EmbeddedPythonConfig,
        cancel_token: Arc<AtomicBool>,
    ) -> Self {
        Self {
            input,
            config: config.clone(),
            cancel_token,
            child_outcomes: Arc::new(Mutex::new(Vec::new())),
            tool_executor: None,
            tool_catalog: ToolCatalog::default(),
        }
    }

    pub fn with_tool_executor(mut self, executor: Arc<ToolExecutorFn>) -> Self {
        self.tool_executor = Some(executor);
        self
    }

    pub(crate) fn with_tool_catalog(mut self, catalog: ToolCatalog) -> Self {
        self.tool_catalog = catalog;
        self
    }

    pub fn with_tool_catalog_from_registry(mut self, registry: &ToolRegistry) -> Self {
        self.tool_catalog = ToolCatalog::from_definitions(registry.definitions());
        self
    }

    pub fn execute(&self) -> ScriptOutput {
        if self.cancel_token.load(Ordering::SeqCst) {
            return ScriptOutput::cancelled();
        }

        if self.input.script.len() > self.config.max_source_bytes {
            return ScriptOutput::failed(ScriptError::source_too_large(
                self.config.max_source_bytes,
            ));
        }

        let runner = match MontyRun::new(
            self.input.script.clone(),
            "script",
            vec!["input".to_string(), "tools".to_string()],
        ) {
            Ok(r) => r,
            Err(e) => {
                let msg = format!("{}", e);
                return ScriptOutput::failed(ScriptError::runtime(msg));
            }
        };

        let monty_input = json_to_monty(&self.input.input);

        let limits = ResourceLimits::new()
            .max_duration(Duration::from_secs(self.config.max_script_timeout_secs));
        let tracker = LimitedTracker::new(limits);

        let progress = match runner.start(
            vec![monty_input, self.tool_catalog.namespace_object()],
            tracker,
            PrintWriter::Disabled,
        ) {
            Ok(p) => p,
            Err(e) => {
                let msg = format!("{}", e);
                return if msg.contains("TimeoutError") {
                    ScriptOutput::failed(ScriptError::timeout())
                } else {
                    ScriptOutput::failed(ScriptError::runtime(msg))
                };
            }
        };

        self.run_loop(progress)
    }

    fn run_loop(&self, initial: RunProgress<LimitedTracker>) -> ScriptOutput {
        let mut current = initial;
        let mut pending_tool_calls: Vec<(String, Value, String)> = Vec::new();
        let max_calls = self.config.max_child_calls;
        let mut call_count = 0usize;

        loop {
            if self.cancel_token.load(Ordering::SeqCst) {
                return ScriptOutput::cancelled();
            }

            if call_count > max_calls {
                return ScriptOutput::failed(ScriptError::child_call_limit(max_calls));
            }

            current = match current {
                RunProgress::Complete(value) => {
                    let result = monty_to_json(&value);
                    let result_size = serde_json::to_string(&result).map(|s| s.len()).unwrap_or(0);
                    if result_size > self.config.max_result_bytes {
                        return ScriptOutput::failed(ScriptError::result_too_large(
                            self.config.max_result_bytes,
                        ));
                    }
                    let child_outcomes = self.take_child_outcomes();
                    let mut output = ScriptOutput::completed(result, child_outcomes);
                    let has_child_failure = output
                        .child_outcomes
                        .iter()
                        .any(|o| o.status != ChildCallStatus::Completed);
                    if has_child_failure {
                        output.status = ScriptExecStatus::CompletedWithFailures;
                    }
                    return output;
                }

                RunProgress::NameLookup(lookup) => {
                    let result = if lookup.name == "iron_call" {
                        NameLookupResult::Value(MontyObject::Function {
                            name: "iron_call".to_string(),
                            docstring: Some(
                                "Call an Iron tool. Usage: await iron_call(name, args)".to_string(),
                            ),
                        })
                    } else {
                        NameLookupResult::Undefined
                    };
                    match lookup.resume(result, PrintWriter::Disabled) {
                        Ok(next) => next,
                        Err(e) => {
                            return ScriptOutput::failed(ScriptError::runtime(format!("{}", e)));
                        }
                    }
                }

                RunProgress::FunctionCall(call) => match self.resolve_function_call(&call) {
                    Ok(ResolvedFunctionCall::Tool { tool_name, args }) => {
                        call_count += 1;
                        let iron_call_id = uuid::Uuid::new_v4().to_string();
                        pending_tool_calls.push((tool_name, args, iron_call_id));

                        match call.resume_pending(PrintWriter::Disabled) {
                            Ok(next) => next,
                            Err(e) => {
                                return ScriptOutput::failed(ScriptError::runtime(format!(
                                    "{}",
                                    e
                                )));
                            }
                        }
                    }
                    Ok(ResolvedFunctionCall::Return(result)) => {
                        match call.resume(ExtFunctionResult::Return(result), PrintWriter::Disabled)
                        {
                            Ok(next) => next,
                            Err(e) => {
                                return ScriptOutput::failed(ScriptError::runtime(format!(
                                    "{}",
                                    e
                                )));
                            }
                        }
                    }
                    Ok(ResolvedFunctionCall::Error(exc)) => {
                        match call.resume(ExtFunctionResult::Error(exc), PrintWriter::Disabled) {
                            Ok(next) => next,
                            Err(e) => {
                                return ScriptOutput::failed(ScriptError::runtime(format!(
                                    "{}",
                                    e
                                )));
                            }
                        }
                    }
                    Err(exc) => match call
                        .resume(ExtFunctionResult::Error(exc), PrintWriter::Disabled)
                    {
                        Ok(next) => next,
                        Err(e) => {
                            return ScriptOutput::failed(ScriptError::runtime(format!("{}", e)));
                        }
                    },
                },

                RunProgress::ResolveFutures(state) => {
                    let pending_ids = state.pending_call_ids();
                    let mut results = Vec::with_capacity(pending_ids.len());

                    for (idx, &call_id) in pending_ids.iter().enumerate() {
                        if idx < pending_tool_calls.len() {
                            let (tool_name, tool_args, iron_call_id) =
                                pending_tool_calls[idx].clone();
                            let (status, result) =
                                self.execute_single_tool(&iron_call_id, &tool_name, tool_args);

                            let ext_result = match status {
                                ChildCallStatus::Completed => {
                                    let obj = result
                                        .map(|v| json_to_monty(&v))
                                        .unwrap_or(MontyObject::None);
                                    ExtFunctionResult::Return(obj)
                                }
                                ChildCallStatus::Failed => {
                                    let msg = result
                                        .and_then(|v| {
                                            v.get("error")
                                                .and_then(|e| e.as_str().map(String::from))
                                        })
                                        .unwrap_or_else(|| "tool call failed".to_string());
                                    ExtFunctionResult::Error(make_iron_exception(
                                        "ToolFailedError",
                                        &msg,
                                    ))
                                }
                                ChildCallStatus::Denied => ExtFunctionResult::Error(
                                    make_iron_exception("ToolDeniedError", "tool call was denied"),
                                ),
                                ChildCallStatus::Cancelled => {
                                    ExtFunctionResult::Error(make_iron_exception(
                                        "ToolCancelledError",
                                        "tool call was cancelled",
                                    ))
                                }
                            };
                            results.push((call_id, ext_result));
                        }
                    }
                    pending_tool_calls.clear();

                    match state.resume(results, PrintWriter::Disabled) {
                        Ok(next) => next,
                        Err(e) => {
                            let child_outcomes = self.take_child_outcomes();
                            let mut output =
                                ScriptOutput::failed(ScriptError::runtime(format!("{}", e)));
                            output.child_outcomes = child_outcomes;
                            return output;
                        }
                    }
                }

                RunProgress::OsCall(_call) => {
                    return ScriptOutput::failed(ScriptError::sandbox_violation(
                        "OS access not available in sandbox. Use tools.<alias>(payload) or tools.call(name, payload) for host access instead."
                    ));
                }
            };
        }
    }

    fn execute_single_tool(
        &self,
        call_id: &str,
        tool_name: &str,
        args: Value,
    ) -> (ChildCallStatus, Option<Value>) {
        let executor = match self.tool_executor.as_ref() {
            Some(e) => e,
            None => {
                let error = serde_json::json!({"error": "no tool executor configured"});
                self.child_outcomes.lock().push(ChildCallOutcome {
                    call_id: call_id.to_string(),
                    tool_name: tool_name.to_string(),
                    status: ChildCallStatus::Failed,
                    result: Some(error.clone()),
                });
                return (ChildCallStatus::Failed, Some(error));
            }
        };

        let (status, result) = executor(call_id, tool_name, args);
        self.child_outcomes.lock().push(ChildCallOutcome {
            call_id: call_id.to_string(),
            tool_name: tool_name.to_string(),
            status,
            result: result.clone(),
        });
        (status, result)
    }

    fn take_child_outcomes(&self) -> Vec<ChildCallOutcome> {
        std::mem::take(&mut *self.child_outcomes.lock())
    }

    fn resolve_function_call<T: monty::ResourceTracker>(
        &self,
        call: &FunctionCall<T>,
    ) -> Result<ResolvedFunctionCall, MontyException> {
        if call.function_name == "iron_call" {
            let tool_name = call
                .args
                .first()
                .and_then(|arg| match arg {
                    MontyObject::String(name) => Some(name.clone()),
                    _ => None,
                })
                .ok_or_else(|| {
                    MontyException::new(
                        ExcType::TypeError,
                        Some("iron_call expected a tool name string".to_string()),
                    )
                })?;
            let tool_args = self.payload_from_call(&call.args, &call.kwargs, 1)?;
            return Ok(ResolvedFunctionCall::Tool {
                tool_name,
                args: tool_args,
            });
        }

        if call.method_call
            && call
                .args
                .first()
                .is_some_and(crate::embedded_python::is_tools_namespace)
        {
            return self.resolve_tools_namespace_call(call);
        }

        Ok(ResolvedFunctionCall::Error(MontyException::new(
            ExcType::NameError,
            Some(format!("name '{}' is not defined", call.function_name)),
        )))
    }

    fn resolve_tools_namespace_call<T: monty::ResourceTracker>(
        &self,
        call: &FunctionCall<T>,
    ) -> Result<ResolvedFunctionCall, MontyException> {
        match call.function_name.as_str() {
            "available" => {
                self.ensure_no_extra_args(&call.args, &call.kwargs, 1, "tools.available")?;
                Ok(ResolvedFunctionCall::Return(json_to_monty(
                    &self.tool_catalog.available_json(),
                )))
            }
            "describe" => {
                self.ensure_no_kwargs(&call.kwargs, "tools.describe")?;
                let name = self.string_arg(&call.args, 1, "tools.describe")?;
                let description = self.tool_catalog.describe_json(&name).ok_or_else(|| {
                    MontyException::new(
                        ExcType::RuntimeError,
                        Some(format!(
                            "tool '{}' is not present in the script tool catalog",
                            name
                        )),
                    )
                })?;
                Ok(ResolvedFunctionCall::Return(json_to_monty(&description)))
            }
            "call" => {
                let tool_name = self.string_arg(&call.args, 1, "tools.call")?;
                let args = self.payload_from_call(&call.args, &call.kwargs, 2)?;
                Ok(ResolvedFunctionCall::Tool {
                    tool_name: self
                        .tool_catalog
                        .entry_by_name(&tool_name)
                        .map(|entry| entry.name().to_string())
                        .unwrap_or(tool_name),
                    args,
                })
            }
            method_name => {
                let entry = self
                    .tool_catalog
                    .entry_by_alias(method_name)
                    .ok_or_else(|| {
                        MontyException::new(
                            ExcType::AttributeError,
                            Some(format!("IronTools has no method '{}'", method_name)),
                        )
                    })?;
                let args = self.payload_from_call(&call.args, &call.kwargs, 1)?;
                Ok(ResolvedFunctionCall::Tool {
                    tool_name: entry.name().to_string(),
                    args,
                })
            }
        }
    }

    fn ensure_no_extra_args(
        &self,
        args: &[MontyObject],
        kwargs: &[(MontyObject, MontyObject)],
        skip: usize,
        method: &str,
    ) -> Result<(), MontyException> {
        if args.len() != skip || !kwargs.is_empty() {
            return Err(MontyException::new(
                ExcType::TypeError,
                Some(format!("{} does not accept arguments", method)),
            ));
        }
        Ok(())
    }

    fn ensure_no_kwargs(
        &self,
        kwargs: &[(MontyObject, MontyObject)],
        method: &str,
    ) -> Result<(), MontyException> {
        if !kwargs.is_empty() {
            return Err(MontyException::new(
                ExcType::TypeError,
                Some(format!("{} does not accept keyword arguments", method)),
            ));
        }
        Ok(())
    }

    fn string_arg(
        &self,
        args: &[MontyObject],
        index: usize,
        method: &str,
    ) -> Result<String, MontyException> {
        args.get(index)
            .and_then(|arg| match arg {
                MontyObject::String(value) => Some(value.clone()),
                _ => None,
            })
            .ok_or_else(|| {
                MontyException::new(
                    ExcType::TypeError,
                    Some(format!("{} expected a string argument", method)),
                )
            })
    }

    fn payload_from_call(
        &self,
        args: &[MontyObject],
        kwargs: &[(MontyObject, MontyObject)],
        skip: usize,
    ) -> Result<Value, MontyException> {
        let positional = args.get(skip..).unwrap_or(&[]);
        if positional.len() > 1 {
            return Err(MontyException::new(
                ExcType::TypeError,
                Some("tool calls accept at most one positional payload object".to_string()),
            ));
        }

        let mut payload = if let Some(first) = positional.first() {
            match monty_to_json(first) {
                Value::Object(map) => map,
                _ => {
                    return Err(MontyException::new(
                        ExcType::TypeError,
                        Some("tool payload must be a JSON object".to_string()),
                    ));
                }
            }
        } else {
            Map::new()
        };

        for (key, value) in kwargs {
            let key = match key {
                MontyObject::String(name) => name.clone(),
                _ => {
                    return Err(MontyException::new(
                        ExcType::TypeError,
                        Some("tool keyword argument names must be strings".to_string()),
                    ));
                }
            };
            payload.insert(key, monty_to_json(value));
        }

        Ok(Value::Object(payload))
    }
}

enum ResolvedFunctionCall {
    Tool { tool_name: String, args: Value },
    Return(MontyObject),
    Error(MontyException),
}

pub struct ScriptEngine {
    pub config: EmbeddedPythonConfig,
}

impl ScriptEngine {
    pub fn new(config: &EmbeddedPythonConfig) -> Self {
        Self {
            config: config.clone(),
        }
    }

    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }

    pub fn create_run(&self, input: ScriptInput) -> ScriptRun {
        let cancel_token = Arc::new(AtomicBool::new(false));
        ScriptRun::new(input, &self.config, cancel_token)
    }
}

impl std::fmt::Debug for ScriptEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ScriptEngine")
            .field("enabled", &self.config.enabled)
            .finish()
    }
}