Skip to content

Commit 18f3f80

Browse files
authored
assistant_tool: Decouple Tool from Workspace (#26309)
This PR decouples the `Tool` trait from the `Workspace` (and from the UI, in general). `Tool::run` now takes a `WeakEntity<Project>` instead of a `WeakEntity<Workspace>` and a `Window`. Release Notes: - N/A
1 parent 4f6682c commit 18f3f80

File tree

13 files changed

+35
-52
lines changed

13 files changed

+35
-52
lines changed

Cargo.lock

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/assistant2/src/active_thread.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ use gpui::{
1111
use language::{Buffer, LanguageRegistry};
1212
use language_model::{LanguageModelRegistry, LanguageModelToolUseId, Role};
1313
use markdown::{Markdown, MarkdownStyle};
14+
use project::Project;
1415
use settings::Settings as _;
1516
use theme::ThemeSettings;
1617
use ui::{prelude::*, Disclosure, KeyBinding};
1718
use util::ResultExt as _;
18-
use workspace::Workspace;
1919

2020
use crate::thread::{MessageId, RequestKind, Thread, ThreadError, ThreadEvent};
2121
use crate::thread_store::ThreadStore;
2222
use crate::tool_use::{ToolUse, ToolUseStatus};
2323
use crate::ui::ContextPill;
2424

2525
pub struct ActiveThread {
26-
workspace: WeakEntity<Workspace>,
26+
project: WeakEntity<Project>,
2727
language_registry: Arc<LanguageRegistry>,
2828
tools: Arc<ToolWorkingSet>,
2929
thread_store: Entity<ThreadStore>,
@@ -46,7 +46,7 @@ impl ActiveThread {
4646
pub fn new(
4747
thread: Entity<Thread>,
4848
thread_store: Entity<ThreadStore>,
49-
workspace: WeakEntity<Workspace>,
49+
project: WeakEntity<Project>,
5050
language_registry: Arc<LanguageRegistry>,
5151
tools: Arc<ToolWorkingSet>,
5252
window: &mut Window,
@@ -58,7 +58,7 @@ impl ActiveThread {
5858
];
5959

6060
let mut this = Self {
61-
workspace,
61+
project,
6262
language_registry,
6363
tools,
6464
thread_store,
@@ -311,7 +311,7 @@ impl ActiveThread {
311311

312312
for tool_use in pending_tool_uses {
313313
if let Some(tool) = self.tools.tool(&tool_use.name, cx) {
314-
let task = tool.run(tool_use.input, self.workspace.clone(), window, cx);
314+
let task = tool.run(tool_use.input, self.project.clone(), cx);
315315

316316
self.thread.update(cx, |thread, cx| {
317317
thread.insert_tool_output(tool_use.id.clone(), task, cx);

crates/assistant2/src/assistant_panel.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,16 @@ impl AssistantPanel {
170170

171171
Self {
172172
active_view: ActiveView::Thread,
173-
workspace: workspace.clone(),
174-
project,
173+
workspace,
174+
project: project.clone(),
175175
fs: fs.clone(),
176176
language_registry: language_registry.clone(),
177177
thread_store: thread_store.clone(),
178178
thread: cx.new(|cx| {
179179
ActiveThread::new(
180180
thread.clone(),
181181
thread_store.clone(),
182-
workspace,
182+
project.downgrade(),
183183
language_registry,
184184
tools.clone(),
185185
window,
@@ -246,7 +246,7 @@ impl AssistantPanel {
246246
ActiveThread::new(
247247
thread.clone(),
248248
self.thread_store.clone(),
249-
self.workspace.clone(),
249+
self.project.downgrade(),
250250
self.language_registry.clone(),
251251
self.tools.clone(),
252252
window,
@@ -381,7 +381,7 @@ impl AssistantPanel {
381381
ActiveThread::new(
382382
thread.clone(),
383383
this.thread_store.clone(),
384-
this.workspace.clone(),
384+
this.project.downgrade(),
385385
this.language_registry.clone(),
386386
this.tools.clone(),
387387
window,

crates/assistant_tool/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ collections.workspace = true
1717
derive_more.workspace = true
1818
gpui.workspace = true
1919
parking_lot.workspace = true
20+
project.workspace = true
2021
serde.workspace = true
2122
serde_json.workspace = true
22-
workspace.workspace = true

crates/assistant_tool/src/assistant_tool.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod tool_working_set;
44
use std::sync::Arc;
55

66
use anyhow::Result;
7-
use gpui::{App, Task, WeakEntity, Window};
8-
use workspace::Workspace;
7+
use gpui::{App, Task, WeakEntity};
8+
use project::Project;
99

1010
pub use crate::tool_registry::*;
1111
pub use crate::tool_working_set::*;
@@ -31,8 +31,7 @@ pub trait Tool: 'static + Send + Sync {
3131
fn run(
3232
self: Arc<Self>,
3333
input: serde_json::Value,
34-
workspace: WeakEntity<Workspace>,
35-
window: &mut Window,
34+
project: WeakEntity<Project>,
3635
cx: &mut App,
3736
) -> Task<Result<String>>;
3837
}

crates/assistant_tools/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ project.workspace = true
2020
schemars.workspace = true
2121
serde.workspace = true
2222
serde_json.workspace = true
23-
workspace.workspace = true

crates/assistant_tools/src/list_worktrees_tool.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::sync::Arc;
22

33
use anyhow::{anyhow, Result};
44
use assistant_tool::Tool;
5-
use gpui::{App, Task, WeakEntity, Window};
5+
use gpui::{App, Task, WeakEntity};
6+
use project::Project;
67
use schemars::JsonSchema;
78
use serde::{Deserialize, Serialize};
8-
use workspace::Workspace;
99

1010
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
1111
pub struct ListWorktreesToolInput {}
@@ -34,16 +34,13 @@ impl Tool for ListWorktreesTool {
3434
fn run(
3535
self: Arc<Self>,
3636
_input: serde_json::Value,
37-
workspace: WeakEntity<Workspace>,
38-
_window: &mut Window,
37+
project: WeakEntity<Project>,
3938
cx: &mut App,
4039
) -> Task<Result<String>> {
41-
let Some(workspace) = workspace.upgrade() else {
42-
return Task::ready(Err(anyhow!("workspace dropped")));
40+
let Some(project) = project.upgrade() else {
41+
return Task::ready(Err(anyhow!("project dropped")));
4342
};
4443

45-
let project = workspace.read(cx).project().clone();
46-
4744
cx.spawn(|cx| async move {
4845
cx.update(|cx| {
4946
#[derive(Debug, Serialize)]

crates/assistant_tools/src/now_tool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::sync::Arc;
33
use anyhow::{anyhow, Result};
44
use assistant_tool::Tool;
55
use chrono::{Local, Utc};
6-
use gpui::{App, Task, WeakEntity, Window};
6+
use gpui::{App, Task, WeakEntity};
7+
use project::Project;
78
use schemars::JsonSchema;
89
use serde::{Deserialize, Serialize};
910

@@ -41,8 +42,7 @@ impl Tool for NowTool {
4142
fn run(
4243
self: Arc<Self>,
4344
input: serde_json::Value,
44-
_workspace: WeakEntity<workspace::Workspace>,
45-
_window: &mut Window,
45+
_project: WeakEntity<Project>,
4646
_cx: &mut App,
4747
) -> Task<Result<String>> {
4848
let input: NowToolInput = match serde_json::from_value(input) {

crates/assistant_tools/src/read_file_tool.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::sync::Arc;
33

44
use anyhow::{anyhow, Result};
55
use assistant_tool::Tool;
6-
use gpui::{App, Task, WeakEntity, Window};
7-
use project::{ProjectPath, WorktreeId};
6+
use gpui::{App, Task, WeakEntity};
7+
use project::{Project, ProjectPath, WorktreeId};
88
use schemars::JsonSchema;
99
use serde::{Deserialize, Serialize};
10-
use workspace::Workspace;
1110

1211
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
1312
pub struct ReadFileToolInput {
@@ -38,20 +37,18 @@ impl Tool for ReadFileTool {
3837
fn run(
3938
self: Arc<Self>,
4039
input: serde_json::Value,
41-
workspace: WeakEntity<Workspace>,
42-
_window: &mut Window,
40+
project: WeakEntity<Project>,
4341
cx: &mut App,
4442
) -> Task<Result<String>> {
45-
let Some(workspace) = workspace.upgrade() else {
46-
return Task::ready(Err(anyhow!("workspace dropped")));
43+
let Some(project) = project.upgrade() else {
44+
return Task::ready(Err(anyhow!("project dropped")));
4745
};
4846

4947
let input = match serde_json::from_value::<ReadFileToolInput>(input) {
5048
Ok(input) => input,
5149
Err(err) => return Task::ready(Err(anyhow!(err))),
5250
};
5351

54-
let project = workspace.read(cx).project().clone();
5552
let project_path = ProjectPath {
5653
worktree_id: WorktreeId::from_usize(input.worktree_id),
5754
path: input.path,

crates/context_server/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,3 @@ settings.workspace = true
3131
smol.workspace = true
3232
url = { workspace = true, features = ["serde"] }
3333
util.workspace = true
34-
workspace.workspace = true

crates/context_server/src/context_server_tool.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use anyhow::{anyhow, bail};
44
use assistant_tool::Tool;
5-
use gpui::{App, Entity, Task, Window};
5+
use gpui::{App, Entity, Task};
66

77
use crate::manager::ContextServerManager;
88
use crate::types;
@@ -51,8 +51,7 @@ impl Tool for ContextServerTool {
5151
fn run(
5252
self: std::sync::Arc<Self>,
5353
input: serde_json::Value,
54-
_workspace: gpui::WeakEntity<workspace::Workspace>,
55-
_: &mut Window,
54+
_project: gpui::WeakEntity<project::Project>,
5655
cx: &mut App,
5756
) -> gpui::Task<gpui::Result<String>> {
5857
if let Some(server) = self.server_manager.read(cx).get_server(&self.server_id) {

crates/scripting_tool/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ serde.workspace = true
2727
serde_json.workspace = true
2828
settings.workspace = true
2929
util.workspace = true
30-
workspace.workspace = true
3130

3231
[dev-dependencies]
3332
collections = { workspace = true, features = ["test-support"] }
3433
gpui = { workspace = true, features = ["test-support"] }
3534
project = { workspace = true, features = ["test-support"] }
3635
settings = { workspace = true, features = ["test-support"] }
37-
workspace = { workspace = true, features = ["test-support"] }

crates/scripting_tool/src/scripting_tool.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod session;
22

3+
use project::Project;
34
pub(crate) use session::*;
45

56
use assistant_tool::{Tool, ToolRegistry};
6-
use gpui::{App, AppContext as _, Task, WeakEntity, Window};
7+
use gpui::{App, AppContext as _, Task, WeakEntity};
78
use schemars::JsonSchema;
89
use serde::Deserialize;
910
use std::sync::Arc;
10-
use workspace::Workspace;
1111

1212
pub fn init(cx: &App) {
1313
let registry = ToolRegistry::global(cx);
@@ -38,17 +38,15 @@ impl Tool for ScriptingTool {
3838
fn run(
3939
self: Arc<Self>,
4040
input: serde_json::Value,
41-
workspace: WeakEntity<Workspace>,
42-
_window: &mut Window,
41+
project: WeakEntity<Project>,
4342
cx: &mut App,
4443
) -> Task<anyhow::Result<String>> {
4544
let input = match serde_json::from_value::<ScriptingToolInput>(input) {
4645
Err(err) => return Task::ready(Err(err.into())),
4746
Ok(input) => input,
4847
};
49-
let Ok(project) = workspace.read_with(cx, |workspace, _cx| workspace.project().clone())
50-
else {
51-
return Task::ready(Err(anyhow::anyhow!("No project found")));
48+
let Some(project) = project.upgrade() else {
49+
return Task::ready(Err(anyhow::anyhow!("project dropped")));
5250
};
5351

5452
let session = cx.new(|cx| Session::new(project, cx));

0 commit comments

Comments
 (0)