oxicode-agent 0.81.0

Agent runtime with tool-calling loop for AI coding assistants
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
//! Eval tool — execute code and capture output.
//!
//! Provides a multi-language code-execution environment (Python and
//! JavaScript today). Each call writes code to a temp file, executes
//! it via the appropriate runtime (`python3` or `bun`/`node`), and
//! captures stdout/stderr + exit code.
//!
//! Each call runs in a fresh process — persistent kernel sessions
//! across calls are a future enhancement. For interactive or
//! multi-step sessions, use `bash` with `python3 -i` or `bun -i`.

use async_trait::async_trait;
use serde_json::{Value, json};
use std::sync::Arc;
use tokio::sync::oneshot;

use super::{AgentTool, AgentToolResult, ToolContext, ToolError, ToolExecutionMode};

/// `eval` agent tool — run code and capture output.
pub struct EvalTool;

#[async_trait]
impl AgentTool for EvalTool {
    fn name(&self) -> &str {
        "eval"
    }

    fn label(&self) -> &str {
        "Eval"
    }

    fn description(&self) -> &str {
        "Execute code in Python (`py`) or JavaScript (`js`) and capture \
         stdout, stderr, and the return value. Each call runs in a fresh \
         process — state does NOT persist across calls. Use `reset: true` \
         to discard previous state explicitly.\n\n\
         For interactive or multi-step sessions, prefer the `bash` tool \
         with `python3 -i` or `bun -i` for persistent state. Use `eval` \
         for quick one-shot computations where only the output matters."
    }

    fn essential(&self) -> bool {
        false
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "language": {
                    "type": "string",
                    "enum": ["py", "js"],
                    "description": "Language runtime: `py` (Python 3) or `js` (JavaScript / Bun)",
                    "default": "py"
                },
                "code": {
                    "type": "string",
                    "description": "Code to execute. Imports and variable definitions persist across calls."
                },
                "title": {
                    "type": "string",
                    "description": "Optional cell label for readability in the transcript"
                },
                "reset": {
                    "type": "boolean",
                    "description": "Reset the kernel/session before executing this cell",
                    "default": false
                }
            },
            "required": ["code"]
        })
    }

    fn intent(&self) -> Option<&str> {
        Some("Execute code and capture output")
    }

    fn execution_mode(&self) -> ToolExecutionMode {
        ToolExecutionMode::SequentialOnly
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _signal: Option<oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, ToolError> {
        let code = params
            .get("code")
            .and_then(|v| v.as_str())
            .ok_or_else(|| "Missing required parameter: code".to_string())?;

        if code.trim().is_empty() {
            return Err("Parameter `code` must be a non-empty string".to_string());
        }

        let language = params
            .get("language")
            .and_then(|v| v.as_str())
            .unwrap_or("py");

        let _title = params.get("title").and_then(|v| v.as_str());

        let _reset = params
            .get("reset")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        // Write code to a temp file and execute it.
        let tmp = std::env::temp_dir().join(format!(
            "oxicode_eval_{}.{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_nanos(),
            match language {
                "py" => "py",
                "js" => "mjs",
                other => return Err(format!("Unsupported language: {}", other)),
            }
        ));

        if let Err(e) = tokio::fs::write(&tmp, code).await {
            return Ok(AgentToolResult::error(format!(
                "Failed to write temp file: {}",
                e
            )));
        }

        let runner = match language {
            "py" => "python3",
            "js" => {
                // Check if bun is available, fall back to node
                let has_bun = tokio::process::Command::new("which")
                    .arg("bun")
                    .output()
                    .await
                    .map(|o| o.status.success())
                    .unwrap_or(false);
                if has_bun { "bun" } else { "node" }
            }
            other => {
                return Ok(AgentToolResult::error(format!(
                    "Unsupported language: '{}'. Supported: py, js",
                    other
                )));
            }
        };

        let output = match tokio::process::Command::new(runner)
            .arg(tmp.to_str().unwrap_or(""))
            .output()
            .await
        {
            Ok(o) => o,
            Err(e) => {
                // Clean up temp file.
                let _ = tokio::fs::remove_file(&tmp).await;
                return Ok(AgentToolResult::error(format!(
                    "Failed to execute code via {}: {}",
                    runner, e
                )));
            }
        };

        // Clean up temp file.
        let _ = tokio::fs::remove_file(&tmp).await;

        let mut result_parts = Vec::new();

        if !output.stdout.is_empty() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if !stdout.trim().is_empty() {
                result_parts.push(format!("── stdout ──\n{}", stdout.trim()));
            }
        }

        if !output.stderr.is_empty() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            if !stderr.trim().is_empty() {
                result_parts.push(format!("── stderr ──\n{}", stderr.trim()));
            }
        }

        let exit_code = output.status.code().unwrap_or(-1);

        if exit_code != 0 {
            result_parts.push(format!("── exit code: {} ──", exit_code));
        }

        let result_text = if result_parts.is_empty() {
            "Code executed successfully (exit code 0, no output)".to_string()
        } else {
            result_parts.join("\n")
        };

        Ok(AgentToolResult::success(result_text))
    }
}

/// `eval` agent tool routed through persistent [`EvalKernel`]s.
///
/// Schema-compatible with [`EvalTool`]; cells execute in one long-lived
/// interpreter per language, so imports and variables persist across
/// calls and `reset: true` actually drops kernel state. The pack picks
/// this variant when the host provides eval kernels and falls back to
/// the per-call [`EvalTool`] otherwise.
pub struct KernelEvalTool {
    kernels: Vec<Arc<dyn crate::runtime::EvalKernel>>,
}

impl KernelEvalTool {
    /// Route cells through `kernels` (one per supported language).
    pub fn new(kernels: Vec<Arc<dyn crate::runtime::EvalKernel>>) -> Self {
        Self { kernels }
    }

    fn kernel_for(&self, language: &str) -> Option<Arc<dyn crate::runtime::EvalKernel>> {
        let wanted = match language {
            "py" => crate::runtime::EvalLanguage::Python,
            "js" => crate::runtime::EvalLanguage::JavaScript,
            _ => return None,
        };
        self.kernels
            .iter()
            .find(|k| k.language() == wanted)
            .cloned()
    }
}

#[async_trait]
impl AgentTool for KernelEvalTool {
    fn name(&self) -> &str {
        "eval"
    }

    fn label(&self) -> &str {
        "Eval (persistent kernel)"
    }

    fn essential(&self) -> bool {
        false
    }

    fn description(&self) -> &str {
        "Execute code in Python (`py`) or JavaScript (`js`) inside a \
         persistent kernel: imports and variable definitions persist \
         across calls. Use `reset: true` to drop the kernel state before \
         a cell. Errors are captured and reported without killing the \
         kernel. Use `bash` for shell-level work."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "language": {
                    "type": "string",
                    "enum": ["py", "js"],
                    "description": "Language runtime: `py` (Python 3) or `js` (JavaScript / Node or Bun)",
                    "default": "py"
                },
                "code": {
                    "type": "string",
                    "description": "Code to execute. Imports and variable definitions persist across calls."
                },
                "title": {
                    "type": "string",
                    "description": "Optional cell label for readability in the transcript"
                },
                "reset": {
                    "type": "boolean",
                    "description": "Reset the kernel/session before executing this cell",
                    "default": false
                }
            },
            "required": ["code"]
        })
    }

    fn intent(&self) -> Option<&str> {
        Some("Execute code in a persistent kernel")
    }

    fn execution_mode(&self) -> ToolExecutionMode {
        // One interpreter per language = shared mutable state.
        ToolExecutionMode::SequentialOnly
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _signal: Option<oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, ToolError> {
        let code = params
            .get("code")
            .and_then(|v| v.as_str())
            .ok_or_else(|| "Missing required parameter: code".to_string())?;
        if code.trim().is_empty() {
            return Err("Parameter `code` must be a non-empty string".to_string());
        }

        let language = params
            .get("language")
            .and_then(|v| v.as_str())
            .unwrap_or("py");

        let kernel = self
            .kernel_for(language)
            .ok_or_else(|| format!("No persistent kernel available for language `{language}`"))?;

        let timeout = std::time::Duration::from_secs(
            params
                .get("timeout")
                .and_then(|v| v.as_u64())
                .unwrap_or(120),
        );

        if params
            .get("reset")
            .and_then(|v| v.as_bool())
            .unwrap_or(false)
        {
            kernel
                .reset()
                .await
                .map_err(|e| -> ToolError { format!("kernel reset failed: {e}") })?;
        }

        let out = kernel
            .execute(code, timeout)
            .await
            .map_err(|e| -> ToolError { format!("kernel execution failed: {e}") })?;

        let mut parts = Vec::new();
        if !out.stdout.trim().is_empty() {
            parts.push(format!("── stdout ──\n{}", out.stdout.trim()));
        }
        if !out.stderr.trim().is_empty() {
            parts.push(format!("── stderr ──\n{}", out.stderr.trim()));
        }
        if let Some(error) = &out.error {
            parts.push(format!("── error ──\n{error}"));
        }
        if out.truncated {
            parts.push("[Kernel output bound applied]".to_string());
        }

        let text = if parts.is_empty() {
            "Cell executed successfully (no output)".to_string()
        } else {
            parts.join("\n")
        };

        if out.error.is_some() {
            Ok(AgentToolResult::error(text))
        } else {
            Ok(AgentToolResult::success(text))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn ctx() -> ToolContext {
        ToolContext::default()
    }

    #[tokio::test]
    async fn rejects_missing_code() {
        let result = EvalTool
            .execute("c1", json!({"language": "py"}), None, &ctx())
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("code"));
    }

    #[tokio::test]
    async fn rejects_empty_code() {
        let result = EvalTool
            .execute("c2", json!({"code": "   \n\t  "}), None, &ctx())
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn rejects_unknown_language() {
        let result = EvalTool
            .execute("c3", json!({"code": "x", "language": "ruby"}), None, &ctx())
            .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("ruby"));
    }

    #[tokio::test]
    async fn executes_python_code() {
        let result = EvalTool
            .execute(
                "c4",
                json!({"code": "print(1+1)", "language": "py"}),
                None,
                &ctx(),
            )
            .await
            .expect("py execution should succeed");
        assert!(result.success);
        assert!(result.output.contains("2"), "output: {}", result.output);
    }

    #[tokio::test]
    async fn executes_js_code() {
        let result = EvalTool
            .execute(
                "c5",
                json!({"code": "console.log(2+2)", "language": "js"}),
                None,
                &ctx(),
            )
            .await;
        // js may be unavailable (no bun/node); error is acceptable.
        if let Ok(result) = result {
            assert!(result.success);
            assert!(result.output.contains("4"), "output: {}", result.output);
        }
    }

    #[tokio::test]
    async fn captures_stderr() {
        let result = EvalTool
            .execute(
                "c6",
                json!({"code": "import sys; print('ok', file=sys.stderr); print('stdout')", "language": "py"}),
                None,
                &ctx(),
            )
            .await
            .expect("py execution should succeed");
        assert!(result.success);
        assert!(
            result.output.contains("stdout"),
            "output: {}",
            result.output
        );
    }
}