Merge branch 'main' into agent2-history

This commit is contained in:
Conrad Irwin 2025-08-18 14:29:33 -06:00
commit 999449424e
80 changed files with 3263 additions and 1900 deletions

View file

@ -206,7 +206,7 @@ mod test_support {
use std::sync::Arc;
use collections::HashMap;
use futures::future::try_join_all;
use futures::{channel::oneshot, future::try_join_all};
use gpui::{AppContext as _, WeakEntity};
use parking_lot::Mutex;
@ -214,11 +214,16 @@ mod test_support {
#[derive(Clone, Default)]
pub struct StubAgentConnection {
sessions: Arc<Mutex<HashMap<acp::SessionId, WeakEntity<AcpThread>>>>,
sessions: Arc<Mutex<HashMap<acp::SessionId, Session>>>,
permission_requests: HashMap<acp::ToolCallId, Vec<acp::PermissionOption>>,
next_prompt_updates: Arc<Mutex<Vec<acp::SessionUpdate>>>,
}
struct Session {
thread: WeakEntity<AcpThread>,
response_tx: Option<oneshot::Sender<acp::StopReason>>,
}
impl StubAgentConnection {
pub fn new() -> Self {
Self {
@ -246,15 +251,33 @@ mod test_support {
update: acp::SessionUpdate,
cx: &mut App,
) {
assert!(
self.next_prompt_updates.lock().is_empty(),
"Use either send_update or set_next_prompt_updates"
);
self.sessions
.lock()
.get(&session_id)
.unwrap()
.thread
.update(cx, |thread, cx| {
thread.handle_session_update(update.clone(), cx).unwrap();
thread.handle_session_update(update, cx).unwrap();
})
.unwrap();
}
pub fn end_turn(&self, session_id: acp::SessionId, stop_reason: acp::StopReason) {
self.sessions
.lock()
.get_mut(&session_id)
.unwrap()
.response_tx
.take()
.expect("No pending turn")
.send(stop_reason)
.unwrap();
}
}
impl AgentConnection for StubAgentConnection {
@ -271,7 +294,13 @@ mod test_support {
let session_id = acp::SessionId(self.sessions.lock().len().to_string().into());
let thread =
cx.new(|cx| AcpThread::new("Test", self.clone(), project, session_id.clone(), cx));
self.sessions.lock().insert(session_id, thread.downgrade());
self.sessions.lock().insert(
session_id,
Session {
thread: thread.downgrade(),
response_tx: None,
},
);
Task::ready(Ok(thread))
}
@ -289,47 +318,70 @@ mod test_support {
params: acp::PromptRequest,
cx: &mut App,
) -> Task<gpui::Result<acp::PromptResponse>> {
let sessions = self.sessions.lock();
let thread = sessions.get(&params.session_id).unwrap();
let mut sessions = self.sessions.lock();
let Session {
thread,
response_tx,
} = sessions.get_mut(&params.session_id).unwrap();
let mut tasks = vec![];
for update in self.next_prompt_updates.lock().drain(..) {
let thread = thread.clone();
let update = update.clone();
let permission_request = if let acp::SessionUpdate::ToolCall(tool_call) = &update
&& let Some(options) = self.permission_requests.get(&tool_call.id)
{
Some((tool_call.clone(), options.clone()))
} else {
None
};
let task = cx.spawn(async move |cx| {
if let Some((tool_call, options)) = permission_request {
let permission = thread.update(cx, |thread, cx| {
thread.request_tool_call_authorization(
tool_call.clone().into(),
options.clone(),
cx,
)
})?;
permission?.await?;
}
thread.update(cx, |thread, cx| {
thread.handle_session_update(update.clone(), cx).unwrap();
})?;
anyhow::Ok(())
});
tasks.push(task);
}
cx.spawn(async move |_| {
try_join_all(tasks).await?;
Ok(acp::PromptResponse {
stop_reason: acp::StopReason::EndTurn,
if self.next_prompt_updates.lock().is_empty() {
let (tx, rx) = oneshot::channel();
response_tx.replace(tx);
cx.spawn(async move |_| {
let stop_reason = rx.await?;
Ok(acp::PromptResponse { stop_reason })
})
})
} else {
for update in self.next_prompt_updates.lock().drain(..) {
let thread = thread.clone();
let update = update.clone();
let permission_request = if let acp::SessionUpdate::ToolCall(tool_call) =
&update
&& let Some(options) = self.permission_requests.get(&tool_call.id)
{
Some((tool_call.clone(), options.clone()))
} else {
None
};
let task = cx.spawn(async move |cx| {
if let Some((tool_call, options)) = permission_request {
let permission = thread.update(cx, |thread, cx| {
thread.request_tool_call_authorization(
tool_call.clone().into(),
options.clone(),
cx,
)
})?;
permission?.await?;
}
thread.update(cx, |thread, cx| {
thread.handle_session_update(update.clone(), cx).unwrap();
})?;
anyhow::Ok(())
});
tasks.push(task);
}
cx.spawn(async move |_| {
try_join_all(tasks).await?;
Ok(acp::PromptResponse {
stop_reason: acp::StopReason::EndTurn,
})
})
}
}
fn cancel(&self, _session_id: &acp::SessionId, _cx: &mut App) {
unimplemented!()
fn cancel(&self, session_id: &acp::SessionId, _cx: &mut App) {
if let Some(end_turn_tx) = self
.sessions
.lock()
.get_mut(session_id)
.unwrap()
.response_tx
.take()
{
end_turn_tx.send(acp::StopReason::Canceled).unwrap();
}
}
fn session_editor(