Port edit tool logic

This commit is contained in:
Agus Zubiaga 2025-08-07 18:15:08 -03:00
parent 168e55db53
commit dbba5c8967
7 changed files with 1629 additions and 8 deletions

3
Cargo.lock generated
View file

@ -158,8 +158,10 @@ dependencies = [
"acp_thread", "acp_thread",
"agent-client-protocol", "agent-client-protocol",
"agent_servers", "agent_servers",
"agent_settings",
"anyhow", "anyhow",
"assistant_tool", "assistant_tool",
"assistant_tools",
"client", "client",
"clock", "clock",
"cloud_llm_client", "cloud_llm_client",
@ -176,6 +178,7 @@ dependencies = [
"language_model", "language_model",
"language_models", "language_models",
"log", "log",
"paths",
"project", "project",
"prompt_store", "prompt_store",
"reqwest_client", "reqwest_client",

View file

@ -14,9 +14,11 @@ workspace = true
[dependencies] [dependencies]
acp_thread.workspace = true acp_thread.workspace = true
agent-client-protocol.workspace = true agent-client-protocol.workspace = true
agent_settings.workspace = true
agent_servers.workspace = true agent_servers.workspace = true
anyhow.workspace = true anyhow.workspace = true
assistant_tool.workspace = true assistant_tool.workspace = true
assistant_tools.workspace = true
cloud_llm_client.workspace = true cloud_llm_client.workspace = true
collections.workspace = true collections.workspace = true
fs.workspace = true fs.workspace = true
@ -24,9 +26,11 @@ futures.workspace = true
gpui.workspace = true gpui.workspace = true
handlebars = { workspace = true, features = ["rust-embed"] } handlebars = { workspace = true, features = ["rust-embed"] }
indoc.workspace = true indoc.workspace = true
language.workspace = true
language_model.workspace = true language_model.workspace = true
language_models.workspace = true language_models.workspace = true
log.workspace = true log.workspace = true
paths.workspace = true
project.workspace = true project.workspace = true
prompt_store.workspace = true prompt_store.workspace = true
rust-embed.workspace = true rust-embed.workspace = true

View file

@ -1,5 +1,5 @@
use crate::{templates::Templates, AgentResponseEvent, Thread}; use crate::{templates::Templates, AgentResponseEvent, Thread};
use crate::{FindPathTool, ToolCallAuthorization}; use crate::{EditFileTool, FindPathTool, ToolCallAuthorization};
use acp_thread::ModelSelector; use acp_thread::ModelSelector;
use agent_client_protocol as acp; use agent_client_protocol as acp;
use anyhow::{anyhow, Context as _, Result}; use anyhow::{anyhow, Context as _, Result};
@ -412,9 +412,10 @@ impl acp_thread::AgentConnection for NativeAgentConnection {
anyhow!("No default model configured. Please configure a default model in settings.") anyhow!("No default model configured. Please configure a default model in settings.")
})?; })?;
let thread = cx.new(|_| { let thread = cx.new(|cx| {
let mut thread = Thread::new(project.clone(), agent.project_context.clone(), action_log, agent.templates.clone(), default_model); let mut thread = Thread::new(project.clone(), agent.project_context.clone(), action_log.clone(), agent.templates.clone(), default_model);
thread.add_tool(FindPathTool::new(project.clone())); thread.add_tool(FindPathTool::new(project.clone()));
thread.add_tool(EditFileTool::new(project.clone(), cx.entity()));
thread thread
}); });

View file

@ -1,4 +1,5 @@
use crate::templates::{SystemPromptTemplate, Template, Templates}; use crate::templates::{SystemPromptTemplate, Template, Templates};
use acp_thread::Diff;
use agent_client_protocol as acp; use agent_client_protocol as acp;
use anyhow::{anyhow, Context as _, Result}; use anyhow::{anyhow, Context as _, Result};
use assistant_tool::ActionLog; use assistant_tool::ActionLog;
@ -132,7 +133,7 @@ pub struct Thread {
project_context: Rc<RefCell<ProjectContext>>, project_context: Rc<RefCell<ProjectContext>>,
templates: Arc<Templates>, templates: Arc<Templates>,
pub selected_model: Arc<dyn LanguageModel>, pub selected_model: Arc<dyn LanguageModel>,
_action_log: Entity<ActionLog>, action_log: Entity<ActionLog>,
} }
impl Thread { impl Thread {
@ -152,7 +153,7 @@ impl Thread {
project_context, project_context,
templates, templates,
selected_model: default_model, selected_model: default_model,
_action_log: action_log, action_log,
} }
} }
@ -322,6 +323,10 @@ impl Thread {
events_rx events_rx
} }
pub fn action_log(&self) -> &Entity<ActionLog> {
&self.action_log
}
pub fn build_system_message(&self) -> AgentMessage { pub fn build_system_message(&self) -> AgentMessage {
log::debug!("Building system message"); log::debug!("Building system message");
let prompt = SystemPromptTemplate { let prompt = SystemPromptTemplate {
@ -538,6 +543,11 @@ impl Thread {
status: Some(acp::ToolCallStatus::InProgress), status: Some(acp::ToolCallStatus::InProgress),
..Default::default() ..Default::default()
}); });
log::trace!(
"Running tool {:?} with input: {}",
tool_use.name,
serde_json::to_string_pretty(&tool_use.input).unwrap_or_default()
);
cx.update(|cx| tool.run(tool_use.input, tool_event_stream, cx))? cx.update(|cx| tool.run(tool_use.input, tool_event_stream, cx))?
.await .await
}) })
@ -586,7 +596,7 @@ impl Thread {
self.messages.last_mut().unwrap() self.messages.last_mut().unwrap()
} }
fn build_completion_request( pub(crate) fn build_completion_request(
&self, &self,
completion_intent: CompletionIntent, completion_intent: CompletionIntent,
cx: &mut App, cx: &mut App,
@ -866,6 +876,12 @@ impl AgentResponseEventStream {
.ok(); .ok();
} }
fn send_tool_call_diff(&self, tool_call_diff: ToolCallDiff) {
self.0
.unbounded_send(Ok(AgentResponseEvent::ToolCallDiff(tool_call_diff)))
.ok();
}
fn send_stop(&self, reason: StopReason) { fn send_stop(&self, reason: StopReason) {
match reason { match reason {
StopReason::EndTurn => { StopReason::EndTurn => {
@ -909,4 +925,11 @@ impl ToolCallEventStream {
pub fn send_update(&self, fields: acp::ToolCallUpdateFields) { pub fn send_update(&self, fields: acp::ToolCallUpdateFields) {
self.stream.send_tool_call_update(&self.tool_use_id, fields); self.stream.send_tool_call_update(&self.tool_use_id, fields);
} }
pub fn send_diff(&self, diff: Entity<Diff>) {
self.stream.send_tool_call_diff(ToolCallDiff {
tool_call_id: acp::ToolCallId(self.tool_use_id.to_string().into()),
diff,
});
}
} }

View file

@ -1,3 +1,5 @@
mod edit_file_tool;
mod find_path_tool; mod find_path_tool;
pub use edit_file_tool::*;
pub use find_path_tool::*; pub use find_path_tool::*;

File diff suppressed because it is too large Load diff

View file

@ -2,7 +2,7 @@ mod copy_path_tool;
mod create_directory_tool; mod create_directory_tool;
mod delete_path_tool; mod delete_path_tool;
mod diagnostics_tool; mod diagnostics_tool;
mod edit_agent; pub mod edit_agent;
mod edit_file_tool; mod edit_file_tool;
mod fetch_tool; mod fetch_tool;
mod find_path_tool; mod find_path_tool;
@ -14,7 +14,7 @@ mod open_tool;
mod project_notifications_tool; mod project_notifications_tool;
mod read_file_tool; mod read_file_tool;
mod schema; mod schema;
mod templates; pub mod templates;
mod terminal_tool; mod terminal_tool;
mod thinking_tool; mod thinking_tool;
mod ui; mod ui;