bashkit 0.4.1

Awesomely fast virtual sandbox with bash and file system
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// ToolDefExtension centralizes ToolDef-backed builtins so plain Bash and
// ScriptedTool share the same command, help, discover, dry-run, and trace behavior.

use super::{
    CallbackKind, RegisteredTool, ScriptedCommandInvocation, ScriptedCommandKind, ToolArgs,
    ToolDef, ToolImpl,
};
use crate::builtins::{Builtin, Context, Extension};
use crate::error::Result;
use crate::interpreter::ExecResult;
use crate::tool_def::{parse_flags, usage_from_schema};
use async_trait::async_trait;
use std::future::Future;
use std::sync::{Arc, Mutex};

pub(crate) type InvocationLog = Arc<Mutex<Vec<ScriptedCommandInvocation>>>;

fn push_invocation(
    log: &InvocationLog,
    name: &str,
    kind: ScriptedCommandKind,
    args: &[String],
    exit_code: i32,
) {
    let mut invocations = log.lock().expect("tool-def invocation log poisoned");
    invocations.push(ScriptedCommandInvocation {
        name: name.to_string(),
        kind,
        args: args.to_vec(),
        exit_code,
    });
}

/// Builder for [`ToolDefExtension`].
pub struct ToolDefExtensionBuilder {
    tools: Vec<RegisteredTool>,
    sanitize_errors: bool,
    invocation_log: InvocationLog,
}

impl Default for ToolDefExtensionBuilder {
    fn default() -> Self {
        Self {
            tools: Vec::new(),
            sanitize_errors: true,
            invocation_log: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

impl ToolDefExtensionBuilder {
    /// Register a [`ToolImpl`] (definition + exec functions).
    pub fn tool(mut self, tool: ToolImpl) -> Self {
        self.tools.push(RegisteredTool::from_tool_impl(tool));
        self
    }

    /// Register a tool with its definition and synchronous exec function.
    pub fn tool_fn(
        mut self,
        def: ToolDef,
        exec: impl Fn(&ToolArgs) -> std::result::Result<String, String> + Send + Sync + 'static,
    ) -> Self {
        self.tools.push(RegisteredTool {
            def,
            callback: CallbackKind::Sync(Arc::new(exec)),
            dry_run: None,
        });
        self
    }

    /// Register a sync tool plus a custom `--dry-run` handler.
    pub fn tool_with_dry_run(
        mut self,
        def: ToolDef,
        exec: impl Fn(&ToolArgs) -> std::result::Result<String, String> + Send + Sync + 'static,
        dry_run: impl Fn(&ToolArgs) -> std::result::Result<String, String> + Send + Sync + 'static,
    ) -> Self {
        self.tools.push(RegisteredTool {
            def,
            callback: CallbackKind::Sync(Arc::new(exec)),
            dry_run: Some(CallbackKind::Sync(Arc::new(dry_run))),
        });
        self
    }

    /// Register a tool with its definition and async exec function.
    pub fn async_tool_fn<F, Fut>(mut self, def: ToolDef, exec: F) -> Self
    where
        F: Fn(ToolArgs) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = std::result::Result<String, String>> + Send + 'static,
    {
        self.tools.push(RegisteredTool {
            def,
            callback: CallbackKind::Async(Arc::new(move |args| Box::pin(exec(args)))),
            dry_run: None,
        });
        self
    }

    /// Replace callback errors with generic messages before exposing them to scripts.
    pub fn sanitize_errors(mut self, sanitize: bool) -> Self {
        self.sanitize_errors = sanitize;
        self
    }

    /// Build the extension.
    pub fn build(&self) -> ToolDefExtension {
        ToolDefExtension {
            tools: self.tools.clone(),
            sanitize_errors: self.sanitize_errors,
            invocation_log: Arc::clone(&self.invocation_log),
        }
    }
}

/// Bash extension that registers ToolDef-backed commands plus `help` and `discover`.
#[derive(Clone)]
pub struct ToolDefExtension {
    tools: Vec<RegisteredTool>,
    sanitize_errors: bool,
    invocation_log: InvocationLog,
}

impl ToolDefExtension {
    /// Create an empty builder.
    pub fn builder() -> ToolDefExtensionBuilder {
        ToolDefExtensionBuilder::default()
    }

    pub(crate) fn from_registered_tools(tools: Vec<RegisteredTool>) -> Self {
        Self {
            tools,
            sanitize_errors: true,
            invocation_log: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub(crate) fn with_invocation_log(mut self, log: InvocationLog) -> Self {
        self.invocation_log = log;
        self
    }

    /// Control whether callback errors are sanitized.
    pub fn sanitize_errors(mut self, sanitize: bool) -> Self {
        self.sanitize_errors = sanitize;
        self
    }

    /// Return and clear accumulated command invocation trace entries.
    pub fn take_invocations(&self) -> Vec<ScriptedCommandInvocation> {
        let mut invocations = self
            .invocation_log
            .lock()
            .expect("tool-def invocation log poisoned");
        std::mem::take(&mut *invocations)
    }

    fn snapshots(&self) -> Vec<ToolDefSnapshot> {
        self.tools
            .iter()
            .map(|t| ToolDefSnapshot {
                name: t.def.name.clone(),
                description: t.def.description.clone(),
                input_schema: t.def.input_schema.clone(),
                tags: t.def.tags.clone(),
                category: t.def.category.clone(),
            })
            .collect()
    }
}

impl Extension for ToolDefExtension {
    fn builtins(&self) -> Vec<(String, Box<dyn Builtin>)> {
        let mut builtins: Vec<(String, Box<dyn Builtin>)> = Vec::new();
        for tool in &self.tools {
            let name = tool.def.name.clone();
            builtins.push((
                name.clone(),
                Box::new(ToolBuiltinAdapter {
                    name,
                    description: tool.def.description.clone(),
                    callback: tool.callback.clone(),
                    schema: tool.def.input_schema.clone(),
                    log: Arc::clone(&self.invocation_log),
                    sanitize_errors: self.sanitize_errors,
                    dry_run: tool.dry_run.clone(),
                }),
            ));
        }

        let snapshots = self.snapshots();
        builtins.push((
            "help".to_string(),
            Box::new(HelpBuiltin {
                tools: snapshots.clone(),
                log: Arc::clone(&self.invocation_log),
            }),
        ));
        builtins.push((
            "discover".to_string(),
            Box::new(DiscoverBuiltin {
                tools: snapshots,
                log: Arc::clone(&self.invocation_log),
            }),
        ));
        builtins
    }
}

/// Adapts a [`CallbackKind`] into a [`Builtin`] so the interpreter can execute it.
struct ToolBuiltinAdapter {
    name: String,
    description: String,
    callback: CallbackKind,
    schema: serde_json::Value,
    log: InvocationLog,
    sanitize_errors: bool,
    dry_run: Option<CallbackKind>,
}

impl ToolBuiltinAdapter {
    fn help_text(&self) -> String {
        let mut out = format!("{} - {}\n", self.name, self.description);
        if let Some(usage) = usage_from_schema(&self.schema) {
            out.push_str(&format!("Usage: {} {}\n", self.name, usage));
        }
        out
    }
}

#[async_trait]
impl Builtin for ToolBuiltinAdapter {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if ctx.args.iter().any(|a| a == "--help") {
            let result = ExecResult::ok(self.help_text());
            push_invocation(
                &self.log,
                &self.name,
                ScriptedCommandKind::Help,
                ctx.args,
                result.exit_code,
            );
            return Ok(result);
        }

        if ctx.args.iter().any(|a| a == "--dry-run") {
            let stripped: Vec<String> = ctx
                .args
                .iter()
                .filter(|a| a.as_str() != "--dry-run")
                .cloned()
                .collect();
            let exit_result = match parse_flags(&stripped, &self.schema) {
                Ok(params) => {
                    if let Some(ref dr) = self.dry_run {
                        let tool_args = ToolArgs {
                            params,
                            stdin: ctx.stdin.map(String::from),
                        };
                        let cb_result = match dr {
                            CallbackKind::Sync(cb) => (cb)(&tool_args),
                            CallbackKind::Async(cb) => (cb)(tool_args).await,
                        };
                        match cb_result {
                            Ok(stdout) => ExecResult::ok(stdout),
                            Err(_msg) if self.sanitize_errors => {
                                #[cfg(feature = "tracing")]
                                tracing::debug!(
                                    tool = %self.name,
                                    error = %_msg,
                                    "tool dry-run callback error (sanitized)"
                                );
                                ExecResult::err(format!("{}: callback failed\n", self.name), 1)
                            }
                            Err(msg) => ExecResult::err(msg, 1),
                        }
                    } else {
                        let obj = serde_json::json!({
                            "dry_run": true,
                            "valid": true,
                            "tool": self.name,
                            "params": params,
                        });
                        ExecResult::ok(format!(
                            "{}\n",
                            serde_json::to_string(&obj).unwrap_or_default()
                        ))
                    }
                }
                Err(err) => {
                    let obj = serde_json::json!({
                        "dry_run": true,
                        "valid": false,
                        "tool": self.name,
                        "error": err,
                    });
                    let json = serde_json::to_string(&obj).unwrap_or_default();
                    ExecResult::err(format!("{json}\n"), 1)
                }
            };
            push_invocation(
                &self.log,
                &self.name,
                ScriptedCommandKind::Tool,
                ctx.args,
                exit_result.exit_code,
            );
            return Ok(exit_result);
        }

        let exit_result = match parse_flags(ctx.args, &self.schema) {
            Ok(params) => {
                let tool_args = ToolArgs {
                    params,
                    stdin: ctx.stdin.map(String::from),
                };
                let cb_result = match &self.callback {
                    CallbackKind::Sync(cb) => (cb)(&tool_args),
                    CallbackKind::Async(cb) => (cb)(tool_args).await,
                };
                match cb_result {
                    Ok(stdout) => ExecResult::ok(stdout),
                    Err(_msg) if self.sanitize_errors => {
                        #[cfg(feature = "tracing")]
                        tracing::debug!(
                            tool = %self.name,
                            error = %_msg,
                            "tool callback error (sanitized)"
                        );
                        ExecResult::err(format!("{}: callback failed\n", self.name), 1)
                    }
                    Err(msg) => ExecResult::err(msg, 1),
                }
            }
            Err(msg) => ExecResult::err(msg, 2),
        };

        push_invocation(
            &self.log,
            &self.name,
            ScriptedCommandKind::Tool,
            ctx.args,
            exit_result.exit_code,
        );
        Ok(exit_result)
    }
}

#[derive(Clone)]
struct ToolDefSnapshot {
    name: String,
    description: String,
    input_schema: serde_json::Value,
    tags: Vec<String>,
    category: Option<String>,
}

struct HelpBuiltin {
    tools: Vec<ToolDefSnapshot>,
    log: InvocationLog,
}

#[async_trait]
impl Builtin for HelpBuiltin {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        let args = ctx.args;
        let result = if args.is_empty() || (args.len() == 1 && args[0] == "--list") {
            let mut out = String::new();
            for t in &self.tools {
                out.push_str(&format!("{:<20} {}\n", t.name, t.description));
            }
            ExecResult::ok(out)
        } else {
            let tool_name = args.iter().find(|a| !a.starts_with("--"));
            let json_mode = args.iter().any(|a| a == "--json");

            let Some(tool_name) = tool_name else {
                let result =
                    ExecResult::err("usage: help [--list] [<tool>] [--json]".to_string(), 1);
                push_invocation(
                    &self.log,
                    "help",
                    ScriptedCommandKind::Help,
                    args,
                    result.exit_code,
                );
                return Ok(result);
            };

            let Some(tool) = self.tools.iter().find(|t| t.name == *tool_name) else {
                let result = ExecResult::err(format!("help: unknown tool: {tool_name}"), 1);
                push_invocation(
                    &self.log,
                    "help",
                    ScriptedCommandKind::Help,
                    args,
                    result.exit_code,
                );
                return Ok(result);
            };

            if json_mode {
                let obj = serde_json::json!({
                    "name": tool.name,
                    "description": tool.description,
                    "input_schema": tool.input_schema,
                });
                let json_str = serde_json::to_string_pretty(&obj).unwrap_or_default();
                ExecResult::ok(format!("{json_str}\n"))
            } else {
                let mut out = format!("{} - {}\n", tool.name, tool.description);
                if let Some(usage) = usage_from_schema(&tool.input_schema) {
                    out.push_str(&format!("Usage: {} {}\n", tool.name, usage));
                }
                ExecResult::ok(out)
            }
        };

        push_invocation(
            &self.log,
            "help",
            ScriptedCommandKind::Help,
            args,
            result.exit_code,
        );
        Ok(result)
    }
}

struct DiscoverBuiltin {
    tools: Vec<ToolDefSnapshot>,
    log: InvocationLog,
}

impl DiscoverBuiltin {
    fn filter_tools(&self, args: &[String]) -> Vec<&ToolDefSnapshot> {
        if let Some(pos) = args.iter().position(|a| a == "--category") {
            let cat = args.get(pos + 1).map(|s| s.as_str()).unwrap_or("");
            return self
                .tools
                .iter()
                .filter(|t| t.category.as_deref() == Some(cat))
                .collect();
        }

        if let Some(pos) = args.iter().position(|a| a == "--tag") {
            let tag = args.get(pos + 1).map(|s| s.as_str()).unwrap_or("");
            return self
                .tools
                .iter()
                .filter(|t| t.tags.iter().any(|tg| tg == tag))
                .collect();
        }

        if let Some(pos) = args.iter().position(|a| a == "--search") {
            let keyword = args
                .get(pos + 1)
                .map(|s| s.to_lowercase())
                .unwrap_or_default();
            return self
                .tools
                .iter()
                .filter(|t| {
                    t.name.to_lowercase().contains(&keyword)
                        || t.description.to_lowercase().contains(&keyword)
                })
                .collect();
        }

        self.tools.iter().collect()
    }
}

#[async_trait]
impl Builtin for DiscoverBuiltin {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        let args = ctx.args;
        let result = if args.is_empty() {
            ExecResult::err(
                "usage: discover --categories | --category <name> | --tag <tag> | --search <keyword> [--json]".to_string(),
                1,
            )
        } else {
            let json_mode = args.iter().any(|a| a == "--json");

            if args.iter().any(|a| a == "--categories") {
                let mut cats: std::collections::BTreeMap<String, usize> =
                    std::collections::BTreeMap::new();
                for t in &self.tools {
                    if let Some(ref cat) = t.category {
                        *cats.entry(cat.clone()).or_insert(0) += 1;
                    }
                }
                if json_mode {
                    let arr: Vec<serde_json::Value> = cats
                        .iter()
                        .map(|(name, count)| serde_json::json!({"category": name, "count": count}))
                        .collect();
                    let json_str =
                        serde_json::to_string_pretty(&arr).unwrap_or_else(|_| "[]".to_string());
                    ExecResult::ok(format!("{json_str}\n"))
                } else {
                    let mut out = String::new();
                    for (name, count) in &cats {
                        let plural = if *count == 1 { "tool" } else { "tools" };
                        out.push_str(&format!("{name} ({count} {plural})\n"));
                    }
                    ExecResult::ok(out)
                }
            } else {
                let filtered = self.filter_tools(args);
                if json_mode {
                    let arr: Vec<serde_json::Value> = filtered
                        .iter()
                        .map(|t| {
                            let mut obj = serde_json::json!({
                                "name": t.name,
                                "description": t.description,
                            });
                            if !t.tags.is_empty() {
                                obj["tags"] = serde_json::json!(t.tags);
                            }
                            if let Some(ref cat) = t.category {
                                obj["category"] = serde_json::json!(cat);
                            }
                            obj
                        })
                        .collect();
                    let json_str =
                        serde_json::to_string_pretty(&arr).unwrap_or_else(|_| "[]".to_string());
                    ExecResult::ok(format!("{json_str}\n"))
                } else {
                    let mut out = String::new();
                    for t in &filtered {
                        out.push_str(&format!("{:<20} {}\n", t.name, t.description));
                    }
                    ExecResult::ok(out)
                }
            }
        };

        push_invocation(
            &self.log,
            "discover",
            ScriptedCommandKind::Discover,
            args,
            result.exit_code,
        );
        Ok(result)
    }
}