ZIm/crates/scripting_tool/src/scripting_tool.rs
Marshall Bowers 02e970192f
assistant2: Improve Lua script rendering (#26389)
This PR improves the rendering of Lua scripts provided to the scripting
tool.

We now render them in code blocks with syntax highlighting:

<img width="1297" alt="Screenshot 2025-03-10 at 2 40 51 PM"
src="https://github.com/user-attachments/assets/def65b5c-86a8-490f-aaa5-5cc1687fe01e"
/>

Release Notes:

- N/A
2025-03-10 18:54:03 +00:00

63 lines
1.9 KiB
Rust

mod session;
use project::Project;
use session::*;
use gpui::{App, AppContext as _, Entity, Task};
use schemars::JsonSchema;
use serde::Deserialize;
#[derive(Debug, Deserialize, JsonSchema)]
pub struct ScriptingToolInput {
pub lua_script: String,
}
pub struct ScriptingTool;
impl ScriptingTool {
pub const NAME: &str = "lua-interpreter";
pub const DESCRIPTION: &str = include_str!("scripting_tool_description.txt");
pub fn input_schema() -> serde_json::Value {
let schema = schemars::schema_for!(ScriptingToolInput);
serde_json::to_value(&schema).unwrap()
}
pub fn run(
&self,
input: serde_json::Value,
project: Entity<Project>,
cx: &mut App,
) -> Task<anyhow::Result<String>> {
let input = match serde_json::from_value::<ScriptingToolInput>(input) {
Err(err) => return Task::ready(Err(err.into())),
Ok(input) => input,
};
// TODO: Store a session per thread
let session = cx.new(|cx| ScriptSession::new(project, cx));
let lua_script = input.lua_script;
let (script_id, script_task) =
session.update(cx, |session, cx| session.run_script(lua_script, cx));
cx.spawn(|cx| async move {
script_task.await;
let message = session.read_with(&cx, |session, _cx| {
// Using a id to get the script output seems impractical.
// Why not just include it in the Task result?
// This is because we'll later report the script state as it runs,
// currently not supported by the `Tool` interface.
session
.get(script_id)
.output_message_for_llm()
.expect("Script shouldn't still be running")
})?;
drop(session);
Ok(message)
})
}
}