onetool 0.0.1-alpha.10

Sandboxed Lua REPL for LLM tool use
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
//! Interactive Notebook-style Example
//!
//! This is an **interactive showcase** demonstrating the onetool library with beautiful,
//! notebook-style formatting similar to Jupyter notebooks or Org-mode. Ask questions
//! and watch as the LLM generates Lua code, executes it, and explains the results!
//!
//! ## Usage
//!
//! ```bash
//! # Set your API key
//! export DEEPSEEK_API_KEY=your_key_here
//!
//! # Run the interactive notebook
//! cargo run --features notebook_demo --example notebook-demo
//!
//! # Then type your prompts at the >>> prompt:
//! >>> Calculate the first 10 Fibonacci numbers
//! >>> What's the sum of squares from 1 to 100?
//! >>> exit
//! ```
//!
//! ## Architecture
//!
//! This example demonstrates three key onetool concepts:
//! 1. **REPL creation**: `onetool::Repl::new_with_policy()` with an interactive permission policy
//! 2. **GenAI integration**: `onetool::genai::LuaRepl` adapter for tool handling
//! 3. **Agentic loops**: Iterative tool calling until final text response
//!
//! The notebook formatting is purely cosmetic - focus on the core loop in main().

use std::collections::HashSet;
use std::io::{self, IsTerminal, Write};
use std::sync::{Arc, Mutex};

use onetool::runtime::sandbox::policy::{Action, Decision, Policy};
use tracing_subscriber::EnvFilter;

const MODEL: &str = "deepseek-chat";

// ============================================================================
// Interactive Permission Policy
// ============================================================================

struct AskUserPolicy {
    use_colors: bool,
    always_allowed: Mutex<HashSet<String>>,
}

impl AskUserPolicy {
    fn new(use_colors: bool) -> Self {
        Self {
            use_colors,
            always_allowed: Mutex::new(HashSet::new()),
        }
    }

    fn prompt_user(&self, label: &str) -> Decision {
        {
            let allowed = self.always_allowed.lock().unwrap();
            if allowed.contains(label) {
                return Decision::Allow;
            }
        }

        let yellow = if self.use_colors { "\x1b[33m" } else { "" };
        let bold = if self.use_colors { "\x1b[1m" } else { "" };
        let reset = if self.use_colors { "\x1b[0m" } else { "" };

        eprint!(
            "{}{}⚠️  Allow {}?{} [y]es / [n]o / [a]lways: ",
            bold, yellow, label, reset
        );
        io::stderr().flush().ok();

        let mut input = String::new();
        if io::stdin().read_line(&mut input).is_err() {
            return Decision::Deny("Failed to read input".to_string());
        }

        match input.trim().to_lowercase().as_str() {
            "y" | "yes" => Decision::Allow,
            "a" | "always" => {
                self.always_allowed
                    .lock()
                    .unwrap()
                    .insert(label.to_string());
                Decision::Allow
            }
            _ => Decision::Deny(format!("User denied access to {}", label)),
        }
    }
}

impl Policy for AskUserPolicy {
    fn check_access(&self, action: &Action) -> Decision {
        match action {
            Action::CallFunction { name, .. } => self.prompt_user(name),
        }
    }
}

// ============================================================================
// Main Demo
// ============================================================================

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing subscriber (controlled via RUST_LOG env var)
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .init();

    // Validate API key is set
    if std::env::var("DEEPSEEK_API_KEY").is_err() {
        eprintln!("Error: DEEPSEEK_API_KEY environment variable is not set.");
        eprintln!("Please set it before running this example:");
        eprintln!("  export DEEPSEEK_API_KEY=your_key_here");
        return Err("Missing DEEPSEEK_API_KEY".into());
    }

    // Check if we're in a TTY (terminal) for color support
    let use_colors = std::io::stdout().is_terminal();

    print_banner(use_colors);

    // Create the Lua REPL with interactive permission policy
    let policy = Arc::new(AskUserPolicy::new(use_colors));
    let repl = onetool::Repl::new_with_policy(policy).expect("Failed to create REPL");

    let genai_client = genai::Client::default();

    // Create the tool orchestrator for easier tool handling
    let lua_repl = onetool::genai::LuaRepl::new(&repl);

    // Maintain conversation history for context
    let mut conversation_history: Vec<genai::chat::ChatMessage> = Vec::new();

    // Show usage instructions
    let dim_cyan = if use_colors { "\x1b[2m\x1b[36m" } else { "" };
    let reset = if use_colors { "\x1b[0m" } else { "" };
    println!(
        "{}Type your prompt and press Enter. Type 'exit' or 'quit' to end the session.{}",
        dim_cyan, reset
    );
    println!();

    // Interactive loop
    loop {
        match read_user_input(use_colors)? {
            UserInput::Empty => continue,
            UserInput::Exit => {
                let bold_green = if use_colors { "\x1b[1m\x1b[32m" } else { "" };
                let reset = if use_colors { "\x1b[0m" } else { "" };
                println!("\n{}👋 Goodbye!{}\n", bold_green, reset);
                break;
            }
            UserInput::Command(user_prompt) => {
                process_command(
                    user_prompt,
                    use_colors,
                    &mut conversation_history,
                    &genai_client,
                    &lua_repl,
                )
                .await?;
            }
        }
    }

    Ok(())
}

// Process a user command through the agentic loop
async fn process_command(
    user_prompt: String,
    use_colors: bool,
    conversation_history: &mut Vec<genai::chat::ChatMessage>,
    genai_client: &genai::Client,
    lua_repl: &onetool::genai::LuaRepl<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
    // Add user message to conversation history
    conversation_history.push(genai::chat::ChatMessage::user(user_prompt));

    // Agentic loop: keep calling the model until it returns a text response
    loop {
        let chat_req = genai::chat::ChatRequest::new(conversation_history.clone())
            .with_tools(vec![lua_repl.definition()]);

        tracing::debug!("Requesting response from model");
        let chat_res = genai_client.exec_chat(MODEL, chat_req, None).await?;

        // Check if we got tool calls or a text response
        let tool_calls = chat_res.clone().into_tool_calls();

        if tool_calls.is_empty() {
            // No tool calls - this is a final text response
            let answer = chat_res.first_text().unwrap_or("(no response)");
            print_cell(
                answer,
                &CellRenderer::new(CellType::Answer, use_colors),
                "(no answer)",
            );
            conversation_history.push(genai::chat::ChatMessage::assistant(answer));
            break;
        }

        // We have tool calls - execute them
        conversation_history.push(tool_calls.clone().into());

        for tool_call in &tool_calls {
            // Extract and display the generated code
            let source_code = match tool_call.fn_arguments.get("source_code") {
                Some(serde_json::Value::String(code)) => code.as_str(),
                _ => {
                    print_cell(
                        "Tool call missing 'source_code' parameter",
                        &CellRenderer::new(CellType::Error, use_colors),
                        "",
                    );
                    continue;
                }
            };

            print_cell(
                source_code,
                &CellRenderer::new(CellType::Code, use_colors),
                "(no code generated)",
            );

            // Execute the tool call
            tracing::debug!("Executing tool call");
            let tool_response = lua_repl.call(tool_call);

            // Parse and display the execution output
            match serde_json::from_str::<serde_json::Value>(&tool_response.content) {
                Ok(response_json) => {
                    match parse_tool_response(&response_json) {
                        Ok(output) => {
                            print_cell(
                                &output,
                                &CellRenderer::new(CellType::Output, use_colors),
                                "(no output)",
                            );
                        }
                        Err(error) => {
                            print_cell(&error, &CellRenderer::new(CellType::Error, use_colors), "");
                        }
                    }
                    conversation_history.push(tool_response.into());
                }
                Err(e) => {
                    print_cell(
                        &format!("Failed to parse tool response: {}", e),
                        &CellRenderer::new(CellType::Error, use_colors),
                        "",
                    );
                    conversation_history.push(tool_response.into());
                }
            }
        }

        // Loop back to get the next response (might be more tool calls or final answer)
    }

    Ok(())
}

// ============================================================================
// Notebook Cell Formatting
// ============================================================================

const CELL_WIDTH: usize = 100;

/// Pads or truncates a line to fit within the specified width.
/// This ensures consistent box alignment in the notebook output.
fn pad_and_truncate_line(line: &str, width: usize) -> String {
    if line.chars().count() <= width {
        format!("{:width$}", line, width = width)
    } else {
        let truncated: String = line.chars().take(width - 3).collect();
        format!("{}...", truncated)
    }
}

#[derive(Clone, Copy)]
enum CellType {
    Code,
    Output,
    Error,
    Answer,
}

struct CellRenderer {
    icon: &'static str,
    label: &'static str,
    color: &'static str,
    dim_color: &'static str,
    reset: &'static str,
    bold: &'static str,
}

impl CellRenderer {
    fn new(cell_type: CellType, colors_enabled: bool) -> Self {
        let (icon, label, color) = match cell_type {
            CellType::Code => ("💻", "Generated Code", "\x1b[32m"),
            CellType::Output => ("", "Execution Output", "\x1b[33m"),
            CellType::Error => ("", "Error", "\x1b[31m"),
            CellType::Answer => ("", "Answer", "\x1b[36m"),
        };

        if colors_enabled {
            Self {
                icon,
                label,
                color,
                dim_color: "\x1b[2m",
                reset: "\x1b[0m",
                bold: "\x1b[1m",
            }
        } else {
            Self {
                icon,
                label,
                color: "",
                dim_color: "",
                reset: "",
                bold: "",
            }
        }
    }
}

fn print_cell(content: &str, renderer: &CellRenderer, empty_msg: &str) {
    let width = CELL_WIDTH;
    let content_width = width - 4;

    // Print header
    println!(
        "{}{}{} {}{}",
        renderer.bold, renderer.color, renderer.icon, renderer.label, renderer.reset
    );

    // Print top border
    println!(
        "{}{}{}{}",
        renderer.dim_color,
        renderer.color,
        "".repeat(width - 2),
        renderer.reset
    );

    // Print content lines
    let content = if content.is_empty() {
        empty_msg
    } else {
        content
    };

    for line in content.lines() {
        let padded = pad_and_truncate_line(line, content_width);
        println!(
            "{}{}{}{} {} {}{}",
            renderer.dim_color,
            renderer.color,
            renderer.reset,
            renderer.color,
            padded,
            renderer.dim_color,
            renderer.reset
        );
    }

    // Print bottom border
    println!(
        "{}{}{}{}",
        renderer.dim_color,
        renderer.color,
        "".repeat(width - 2),
        renderer.reset
    );
    println!();
}

// ============================================================================
// Helper Functions
// ============================================================================

fn print_banner(colors_enabled: bool) {
    let bold_cyan = if colors_enabled {
        "\x1b[1m\x1b[36m"
    } else {
        ""
    };
    let reset = if colors_enabled { "\x1b[0m" } else { "" };

    println!(
        "{}╔══════════════════════════════════════════════════════════════════════════════════════════════════╗{}",
        bold_cyan, reset
    );
    println!(
        "{}║  ONETOOL NOTEBOOK DEMO  •  Interactive LLM-Powered Lua REPL                                      ║{}",
        bold_cyan, reset
    );
    println!(
        "{}╚══════════════════════════════════════════════════════════════════════════════════════════════════╝{}",
        bold_cyan, reset
    );
    println!();
}

enum UserInput {
    Command(String),
    Exit,
    Empty,
}

fn read_user_input(colors_enabled: bool) -> io::Result<UserInput> {
    let bold_blue = if colors_enabled {
        "\x1b[1m\x1b[34m"
    } else {
        ""
    };
    let reset = if colors_enabled { "\x1b[0m" } else { "" };

    print!("{}>>> {}", bold_blue, reset);
    io::stdout().flush()?;

    let mut input = String::new();
    io::stdin().read_line(&mut input)?;

    let trimmed = input.trim();

    if trimmed.is_empty() {
        Ok(UserInput::Empty)
    } else if trimmed.eq_ignore_ascii_case("exit") || trimmed.eq_ignore_ascii_case("quit") {
        Ok(UserInput::Exit)
    } else {
        Ok(UserInput::Command(trimmed.to_string()))
    }
}

/// Parses the tool response JSON and extracts output and result fields.
/// Returns Ok(formatted_output) or Err(error_message).
fn parse_tool_response(response_json: &serde_json::Value) -> Result<String, String> {
    // Check for errors in tool execution
    if let Some(error) = response_json.get("error") {
        if let Some(error_msg) = error.as_str() {
            return Err(error_msg.to_string());
        }
    }

    let output_text = response_json["output"].as_str().unwrap_or("");
    let result_text = response_json["result"].as_str().unwrap_or("");

    let combined_output = if !output_text.is_empty() && !result_text.is_empty() {
        format!("{}\n\nResult: {}", output_text, result_text)
    } else if !output_text.is_empty() {
        output_text.to_string()
    } else if !result_text.is_empty() {
        result_text.to_string()
    } else {
        "(no output or result)".to_string()
    };

    Ok(combined_output)
}