File size: 14,418 Bytes
e5034c3 | 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 | use std::sync::Arc;
use anyhow::Result;
use forge_domain::*;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AppConfigService, EnvironmentInfra, FileDiscoveryService, ProviderService, TemplateEngine,
TerminalContextService,
};
/// Response struct for shell command generation using JSON format
#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[schemars(title = "shell_command")]
pub struct ShellCommandResponse {
/// The generated shell command
pub command: String,
}
/// CommandGenerator handles shell command generation from natural language
pub struct CommandGenerator<S> {
services: Arc<S>,
}
impl<S> CommandGenerator<S>
where
S: EnvironmentInfra<Config = forge_config::ForgeConfig>
+ FileDiscoveryService
+ ProviderService
+ AppConfigService,
{
/// Creates a new CommandGenerator instance with the provided services.
pub fn new(services: Arc<S>) -> Self {
Self { services }
}
/// Generates a shell command from a natural language prompt.
///
/// Terminal context is read automatically from the `_FORGE_TERM_COMMANDS`,
/// `_FORGE_TERM_EXIT_CODES`, and `_FORGE_TERM_TIMESTAMPS` environment
/// variables exported by the zsh plugin, and included in the user
/// prompt so the LLM can reference recent commands, exit codes, and
/// timestamps.
pub async fn generate(&self, prompt: UserPrompt) -> Result<String> {
// Get system information for context
let env = self.services.get_environment();
let files = self.services.list_current_directory().await?;
let rendered_system_prompt = TemplateEngine::default().render(
"forge-command-generator-prompt.md",
&serde_json::json!({"env": env, "files": files}),
)?;
// Get required services and data - use suggest config if available,
// otherwise fall back to default provider/model
let (provider, model) = match self.services.get_suggest_config().await? {
Some(config) => {
let provider = self.services.get_provider(config.provider).await?;
(provider, config.model)
}
None => {
let model_config = self
.services
.get_session_config()
.await
.ok_or_else(|| forge_domain::Error::NoDefaultSession)?;
let provider = self.services.get_provider(model_config.provider).await?;
(provider, model_config.model)
}
};
// Build user prompt with task, optionally including terminal context.
use forge_template::Element;
let task_elm = Element::new("task").text(prompt.as_str());
let terminal_service = TerminalContextService::new(self.services.clone());
let user_content = match terminal_service.get_terminal_context() {
Some(ctx) => {
let terminal_elm =
Element::new("command_trace").append(ctx.commands.iter().map(|cmd| {
Element::new("command")
.attr("exit_code", cmd.exit_code.to_string())
.text(&cmd.command)
}));
format!("{}\n\n{}", terminal_elm.render(), task_elm.render())
}
None => task_elm.render(),
};
// Create context with system and user prompts
let ctx = self.create_context(rendered_system_prompt, user_content, &model);
// Send message to LLM
let stream = self.services.chat(&model, ctx, provider).await?;
let message = stream.into_full(false).await?;
// Parse the structured JSON response
let response: ShellCommandResponse =
serde_json::from_str(&message.content).map_err(|e| {
anyhow::anyhow!(
"Failed to parse shell command response: {}. Response: {}",
e,
message.content
)
})?;
Ok(response.command)
}
/// Creates a context with system and user messages for the LLM
fn create_context(
&self,
system_prompt: String,
user_content: String,
model: &ModelId,
) -> Context {
// Generate JSON schema from the response struct
let schema = schemars::schema_for!(ShellCommandResponse);
Context::default()
.add_message(ContextMessage::system(system_prompt))
.add_message(ContextMessage::user(user_content, Some(model.clone())))
.response_format(ResponseFormat::JsonSchema(Box::new(schema)))
}
}
#[cfg(test)]
mod tests {
use forge_domain::{
AuthCredential, AuthDetails, AuthMethod, ChatCompletionMessage, Content, FinishReason,
ModelSource, ProviderId, ProviderResponse, ResultStream, Role,
};
use tokio::sync::Mutex;
use url::Url;
use super::*;
use crate::Walker;
struct MockServices {
files: Vec<(String, bool)>,
response: Arc<Mutex<Option<String>>>,
captured_context: Arc<Mutex<Option<Context>>>,
environment: Environment,
env_vars: std::collections::BTreeMap<String, String>,
}
impl MockServices {
fn new(response: &str, files: Vec<(&str, bool)>) -> Arc<Self> {
use fake::{Fake, Faker};
let mut env: Environment = Faker.fake();
// Override only the fields that appear in templates
env.os = "macos".to_string();
env.cwd = "/test/dir".into();
env.shell = "/bin/bash".to_string();
env.home = Some("/home/test".into());
Arc::new(Self {
files: files.into_iter().map(|(p, d)| (p.to_string(), d)).collect(),
response: Arc::new(Mutex::new(Some(response.to_string()))),
captured_context: Arc::new(Mutex::new(None)),
environment: env,
env_vars: std::collections::BTreeMap::new(),
})
}
fn with_terminal_context(
self: Arc<Self>,
commands: &str,
exit_codes: &str,
timestamps: &str,
) -> Arc<Self> {
let mut env_vars = self.env_vars.clone();
env_vars.insert("_FORGE_TERM_COMMANDS".to_string(), commands.to_string());
env_vars.insert("_FORGE_TERM_EXIT_CODES".to_string(), exit_codes.to_string());
env_vars.insert("_FORGE_TERM_TIMESTAMPS".to_string(), timestamps.to_string());
Arc::new(Self {
files: self.files.clone(),
response: self.response.clone(),
captured_context: self.captured_context.clone(),
environment: self.environment.clone(),
env_vars,
})
}
}
impl EnvironmentInfra for MockServices {
type Config = forge_config::ForgeConfig;
fn get_environment(&self) -> Environment {
self.environment.clone()
}
fn get_config(&self) -> anyhow::Result<forge_config::ForgeConfig> {
Ok(forge_config::ForgeConfig::default())
}
async fn update_environment(
&self,
_ops: Vec<forge_domain::ConfigOperation>,
) -> anyhow::Result<()> {
unimplemented!()
}
fn get_env_var(&self, key: &str) -> Option<String> {
self.env_vars.get(key).cloned()
}
fn get_env_vars(&self) -> std::collections::BTreeMap<String, String> {
self.env_vars.clone()
}
}
#[async_trait::async_trait]
impl FileDiscoveryService for MockServices {
async fn collect_files(&self, _walker: Walker) -> Result<Vec<File>> {
Ok(self
.files
.iter()
.map(|(path, is_dir)| File { path: path.clone(), is_dir: *is_dir })
.collect())
}
async fn list_current_directory(&self) -> Result<Vec<File>> {
let mut files: Vec<File> = self
.files
.iter()
.map(|(path, is_dir)| File { path: path.clone(), is_dir: *is_dir })
.collect();
// Sort: directories first (alphabetically), then files (alphabetically)
files.sort_by(|a, b| match (a.is_dir, b.is_dir) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.path.cmp(&b.path),
});
Ok(files)
}
}
#[async_trait::async_trait]
impl ProviderService for MockServices {
async fn chat(
&self,
_id: &ModelId,
context: Context,
_provider: Provider<Url>,
) -> ResultStream<ChatCompletionMessage, anyhow::Error> {
*self.captured_context.lock().await = Some(context);
let response = self.response.lock().await.take().unwrap();
let message = ChatCompletionMessage::assistant(Content::full(response))
.finish_reason(FinishReason::Stop);
Ok(Box::pin(tokio_stream::iter(std::iter::once(Ok(message)))))
}
async fn models(&self, _provider: Provider<Url>) -> Result<Vec<forge_domain::Model>> {
Ok(vec![])
}
async fn get_provider(&self, _id: ProviderId) -> Result<Provider<Url>> {
Ok(Provider {
id: ProviderId::OPENAI,
provider_type: Default::default(),
response: Some(ProviderResponse::OpenAI),
url: Url::parse("https://api.test.com").unwrap(),
models: Some(ModelSource::Url(
Url::parse("https://api.test.com/models").unwrap(),
)),
auth_methods: vec![AuthMethod::ApiKey],
url_params: vec![],
credential: Some(AuthCredential {
id: ProviderId::OPENAI,
auth_details: AuthDetails::ApiKey("test-key".to_string().into()),
url_params: Default::default(),
}),
custom_headers: None,
})
}
async fn get_all_providers(&self) -> Result<Vec<forge_domain::AnyProvider>> {
Ok(vec![])
}
async fn upsert_credential(&self, _credential: AuthCredential) -> Result<()> {
Ok(())
}
async fn remove_credential(&self, _id: &ProviderId) -> Result<()> {
Ok(())
}
async fn migrate_env_credentials(&self) -> anyhow::Result<Option<MigrationResult>> {
Ok(None)
}
}
#[async_trait::async_trait]
impl AppConfigService for MockServices {
async fn get_session_config(&self) -> Option<forge_domain::ModelConfig> {
Some(forge_domain::ModelConfig::new(
ProviderId::OPENAI,
ModelId::new("test-model"),
))
}
async fn get_commit_config(&self) -> Result<Option<forge_domain::ModelConfig>> {
Ok(None)
}
async fn get_suggest_config(&self) -> Result<Option<forge_domain::ModelConfig>> {
Ok(None)
}
async fn get_reasoning_effort(&self) -> Result<Option<forge_domain::Effort>> {
Ok(None)
}
async fn update_config(&self, _ops: Vec<forge_domain::ConfigOperation>) -> Result<()> {
Ok(())
}
}
#[tokio::test]
async fn test_generate_simple_command() {
let fixture = MockServices::new(
r#"{"command": "ls -la"}"#,
vec![("file1.txt", false), ("file2.rs", false)],
);
let generator = CommandGenerator::new(fixture.clone());
let actual = generator
.generate(UserPrompt::from("list all files".to_string()))
.await
.unwrap();
assert_eq!(actual, "ls -la");
let captured_context = fixture.captured_context.lock().await.clone().unwrap();
insta::assert_yaml_snapshot!(captured_context);
}
#[tokio::test]
async fn test_generate_with_no_files() {
let fixture = MockServices::new(r#"{"command": "pwd"}"#, vec![]);
let generator = CommandGenerator::new(fixture.clone());
let actual = generator
.generate(UserPrompt::from("show current directory".to_string()))
.await
.unwrap();
assert_eq!(actual, "pwd");
let captured_context = fixture.captured_context.lock().await.clone().unwrap();
insta::assert_yaml_snapshot!(captured_context);
}
#[tokio::test]
async fn test_generate_with_shell_context() {
let fixture = MockServices::new(
r#"{"command": "cargo build --release"}"#,
vec![("Cargo.toml", false)],
)
.with_terminal_context("cargo build", "101", "1700000000");
let generator = CommandGenerator::new(fixture.clone());
let actual = generator
.generate(UserPrompt::from("fix the command I just ran".to_string()))
.await
.unwrap();
assert_eq!(actual, "cargo build --release");
let captured_context = fixture.captured_context.lock().await.clone().unwrap();
let user_content = captured_context
.messages
.iter()
.find(|m| m.has_role(Role::User))
.expect("should have a user message")
.content()
.expect("user message should have content");
assert!(user_content.contains("<command_trace>"));
assert!(user_content.contains("</command_trace>"));
assert!(user_content.contains("cargo build"));
assert!(user_content.contains("<task>fix the command I just ran</task>"));
}
#[tokio::test]
async fn test_generate_fails_when_missing_tag() {
let fixture = MockServices::new(r#"{"invalid": "json"}"#, vec![]);
let generator = CommandGenerator::new(fixture);
let actual = generator
.generate(UserPrompt::from("do something".to_string()))
.await;
assert!(actual.is_err());
let error_msg = actual.unwrap_err().to_string();
assert!(error_msg.contains("Failed to parse shell command response"));
}
}
|