use std::path::PathBuf;
pub const BUILTIN_COMMANDS: &[(&str, &str)] = &[
("help", "List all available slash commands"),
("skills", "List all available skills"),
("clear", "Clear the current session context"),
("compact", "Trigger context compaction now"),
("cost", "Show the tokens and calls this session has spent"),
];
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum PackageSource {
#[default]
Installed,
Host,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtCommand {
pub name: String,
pub description: String,
pub plugin: String,
pub plugin_version: String,
pub source: PackageSource,
pub root: PathBuf,
pub file: PathBuf,
pub function: String,
pub read_only: Vec<String>,
pub writes: Vec<String>,
pub uses: Vec<String>,
pub bytecode_dir: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginFunction {
pub name: String,
pub plugin: String,
pub plugin_version: String,
pub source: PackageSource,
pub description: String,
pub root: PathBuf,
pub file: PathBuf,
pub function: String,
pub writes: Vec<String>,
pub input_schema: Option<serde_json::Value>,
pub uses: Vec<String>,
pub bytecode_dir: Option<PathBuf>,
}
impl PluginFunction {
pub fn call_name(&self) -> String {
format!("{}.{}", self.plugin, self.name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostLibrary {
pub name: String,
pub description: Option<String>,
}
impl ExtCommand {
pub fn is_read_only(&self, args: &[String]) -> bool {
self.read_only.iter().any(|prefix| {
let words: Vec<&str> = prefix.split_whitespace().collect();
if words.is_empty() {
return args.is_empty();
}
words.len() <= args.len() && words.iter().zip(args).all(|(w, a)| *w == a)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn command(read_only: &[&str]) -> ExtCommand {
ExtCommand {
name: "forge".into(),
description: String::new(),
plugin: "forge".into(),
plugin_version: "1.0.0".into(),
source: PackageSource::Installed,
root: PathBuf::new(),
file: PathBuf::new(),
function: "main".into(),
read_only: read_only.iter().map(|s| s.to_string()).collect(),
writes: Vec::new(),
uses: Vec::new(),
bytecode_dir: None,
}
}
fn args(line: &str) -> Vec<String> {
line.split_whitespace().map(str::to_string).collect()
}
#[test]
fn a_read_only_prefix_matches_whole_words_only() {
let c = command(&["help", "spec list"]);
assert!(c.is_read_only(&args("help")));
assert!(c.is_read_only(&args("spec list --status draft")));
assert!(!c.is_read_only(&args("spec listall")));
assert!(!c.is_read_only(&args("spec")));
assert!(!c.is_read_only(&args("helpme")));
assert!(!c.is_read_only(&[]));
}
#[test]
fn an_empty_prefix_is_the_call_with_no_arguments_only() {
let bare = command(&[""]);
assert!(bare.is_read_only(&[]));
assert!(!bare.is_read_only(&args("anything")));
assert!(!bare.is_read_only(&args("help")));
}
}