use std::collections::HashMap;
use async_trait::async_trait;
use crate::plugin::{Plugin, PluginCommand, PluginMetadata};
use crate::ai;
type PluginResult<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
pub struct AiPlugin {
metadata: PluginMetadata,
}
impl AiPlugin {
pub fn new() -> Self {
Self {
metadata: PluginMetadata {
name: "ai".to_string(),
version: "1.0.0".to_string(),
description: "AI plugin for OpenScript".to_string(),
author: "OpenScript Team".to_string(),
license: "MIT".to_string(),
dependencies: vec!["async-openai".to_string()],
api_version: "1.0".to_string(),
},
}
}
}
#[async_trait]
impl Plugin for AiPlugin {
fn metadata(&self) -> &PluginMetadata {
&self.metadata
}
async fn initialize(&mut self) -> PluginResult<()> {
Ok(())
}
fn register_commands(&self) -> HashMap<String, Box<dyn PluginCommand>> {
let mut commands = HashMap::new();
commands.insert("ai_complete".to_string(), Box::new(AiCompleteCommand) as Box<dyn PluginCommand>);
commands
}
async fn cleanup(&mut self) -> PluginResult<()> {
Ok(())
}
}
struct AiCompleteCommand;
#[async_trait]
impl PluginCommand for AiCompleteCommand {
async fn execute(&self, args: &[String]) -> PluginResult<String> {
if args.is_empty() {
return Err("ai_complete() requires at least one argument".into());
}
let prompt = &args[0];
let model = args.get(1).map(|s| s.as_str()).unwrap_or("gpt-3.5-turbo");
let max_tokens = args.get(2).and_then(|s| s.parse::<u16>().ok());
let temperature = args.get(3).and_then(|s| s.parse::<f32>().ok()).unwrap_or(0.7);
let result = ai::ai_complete(prompt, model, max_tokens, temperature).await.map_err(|e| e.to_string())?;
Ok(result.to_string())
}
fn help(&self) -> &str {
"Generates text completion using OpenAI's GPT models."
}
fn usage(&self) -> &str {
"ai_complete <prompt> [model] [max_tokens] [temperature]"
}
}