//! Built-in coding tools through `smith:plugin` (`SMH-SPEC-PLUG0001`). #![expect(missing_docs, reason = "wit-bindgen generates the world surface")] #![expect(clippy::pedantic, reason = "wit-bindgen generates the world surface")] wit_bindgen::generate!({ path: "../../wit/smith-plugin.wit", world: "plugin-world", }); use exports::smith::plugin::plugin::{Guest, ToolDeclaration}; use exports::smith::plugin::provider::{ CatalogSnapshot, DecodeStep, Guest as ProviderGuest, ProviderRequest, ProviderSettings, RequestTemplate, SettingsProvenance, }; use exports::smith::plugin::registration::{Guest as RegistrationGuest, Registrations}; use serde::Deserialize; use smith::plugin::tooling::{self, DirectoryQuery, FileEdit, FileRead, FileWrite}; struct CodingTools; impl RegistrationGuest for CodingTools { fn declare() -> Registrations { Registrations { credential_sources: Vec::new(), providers: Vec::new(), models: Vec::new(), } } } #[derive(Deserialize)] struct ToolMetadata { name: String, description: String, input_schema_json: String, } fn tool_metadata() -> Vec { serde_json::from_str(include_str!("../metadata.json")).unwrap_or_else(|_| Vec::new()) } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct ReadInput { path: String, #[serde(default)] offset: u64, #[serde(default = "read_limit")] limit: u64, #[serde(default = "read_max_bytes")] max_bytes: u64, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct ListInput { #[serde(default)] path: String, } const fn read_limit() -> u64 { u64::MAX } const fn read_max_bytes() -> u64 { 64 * 1024 } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct WriteInput { path: String, content: String, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct EditInput { path: String, old_string: String, new_string: String, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct QueryInput { #[serde(default)] path: String, #[serde(default)] pattern: String, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct GrepInput { path: String, query: String, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct BashInput { command: String, #[serde(default = "bash_timeout_ms")] timeout_ms: u64, } const fn bash_timeout_ms() -> u64 { 30_000 } impl ProviderGuest for CodingTools { fn declare_settings() -> ProviderSettings { ProviderSettings { include_usage: false, subscription: false, provenance: SettingsProvenance::BuiltIn, } } fn validate_settings(_settings: ProviderSettings) -> Result { Err("coding tools do not provide protocol settings".to_string()) } fn build_request( _model: String, _request: ProviderRequest, _settings: ProviderSettings, _catalog: CatalogSnapshot, ) -> Result { Err("coding tools do not provide a model codec".to_string()) } fn decode_response( _state: Vec, _chunk: Vec, _finished: bool, ) -> Result { Err("coding tools do not provide a model decoder".to_string()) } } impl Guest for CodingTools { fn declare_tools() -> Vec { tool_metadata() .into_iter() .map(|tool| ToolDeclaration { name: tool.name, description: tool.description, input_schema_json: tool.input_schema_json, }) .collect() } fn invoke(name: String, input_json: String) -> Result { match name.as_str() { "read" => { let input: ReadInput = parse(&input_json)?; tooling::read(&FileRead { path: input.path, offset: input.offset, limit: input.limit, max_bytes: input.max_bytes, }) .map(|content| content.text) } "write" => { let input: WriteInput = parse(&input_json)?; tooling::write(&FileWrite { path: input.path, content: input.content, }) .map(|()| "written".to_string()) } "edit" => { let input: EditInput = parse(&input_json)?; tooling::edit(&FileEdit { path: input.path, old_text: input.old_string, new_text: input.new_string, }) .map(|()| "edited".to_string()) } "ls" => { let input: ListInput = parse(&input_json)?; tooling::list_directory(&DirectoryQuery { path: input.path, pattern: None, }) .map(|items| items.join("\n")) } "find" => { let input: QueryInput = parse(&input_json)?; tooling::find(&DirectoryQuery { path: input.path, pattern: Some(input.pattern), }) .map(|items| items.join("\n")) } "grep" => { let input: GrepInput = parse(&input_json)?; tooling::grep(&DirectoryQuery { path: input.path, pattern: Some(input.query), }) .map(|items| items.join("\n")) } "bash" => { let input: BashInput = parse(&input_json)?; tooling::run_command(&tooling::CommandRequest { text: input.command, timeout_ms: input.timeout_ms, }) } _ => Err(format!("unknown tool: {name}")), } } } fn parse(input: &str) -> Result { serde_json::from_str(input).map_err(|error| format!("invalid tool input: {error}")) } export!(CodingTools);