Skip to content

Commit e70d0ed

Browse files
authored
assistant_tool: Pass an Entity<Project> to Tool::run (#26312)
This PR updates the `Tool::run` method to take an `Entity<Project>` instead of a `WeakEntity<Project>`. Release Notes: - N/A
1 parent 921c24e commit e70d0ed

File tree

7 files changed

+20
-30
lines changed

7 files changed

+20
-30
lines changed

crates/assistant2/src/thread.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use assistant_tool::ToolWorkingSet;
55
use chrono::{DateTime, Utc};
66
use collections::{BTreeMap, HashMap, HashSet};
77
use futures::StreamExt as _;
8-
use gpui::{App, Context, Entity, EventEmitter, SharedString, Task, WeakEntity};
8+
use gpui::{App, Context, Entity, EventEmitter, SharedString, Task};
99
use language_model::{
1010
LanguageModel, LanguageModelCompletionEvent, LanguageModelRegistry, LanguageModelRequest,
1111
LanguageModelRequestMessage, LanguageModelRequestTool, LanguageModelToolResult,
@@ -72,7 +72,7 @@ pub struct Thread {
7272
context_by_message: HashMap<MessageId, Vec<ContextId>>,
7373
completion_count: usize,
7474
pending_completions: Vec<PendingCompletion>,
75-
project: WeakEntity<Project>,
75+
project: Entity<Project>,
7676
tools: Arc<ToolWorkingSet>,
7777
tool_use: ToolUseState,
7878
}
@@ -94,7 +94,7 @@ impl Thread {
9494
context_by_message: HashMap::default(),
9595
completion_count: 0,
9696
pending_completions: Vec::new(),
97-
project: project.downgrade(),
97+
project,
9898
tools,
9999
tool_use: ToolUseState::new(),
100100
}
@@ -135,7 +135,7 @@ impl Thread {
135135
context_by_message: HashMap::default(),
136136
completion_count: 0,
137137
pending_completions: Vec::new(),
138-
project: project.downgrade(),
138+
project,
139139
tools,
140140
tool_use,
141141
}

crates/assistant_tool/src/assistant_tool.rs

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

66
use anyhow::Result;
7-
use gpui::{App, Task, WeakEntity};
7+
use gpui::{App, Entity, Task};
88
use project::Project;
99

1010
pub use crate::tool_registry::*;
@@ -31,7 +31,7 @@ pub trait Tool: 'static + Send + Sync {
3131
fn run(
3232
self: Arc<Self>,
3333
input: serde_json::Value,
34-
project: WeakEntity<Project>,
34+
project: Entity<Project>,
3535
cx: &mut App,
3636
) -> Task<Result<String>>;
3737
}

crates/assistant_tools/src/list_worktrees_tool.rs

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

3-
use anyhow::{anyhow, Result};
3+
use anyhow::Result;
44
use assistant_tool::Tool;
5-
use gpui::{App, Task, WeakEntity};
5+
use gpui::{App, Entity, Task};
66
use project::Project;
77
use schemars::JsonSchema;
88
use serde::{Deserialize, Serialize};
@@ -34,13 +34,9 @@ impl Tool for ListWorktreesTool {
3434
fn run(
3535
self: Arc<Self>,
3636
_input: serde_json::Value,
37-
project: WeakEntity<Project>,
37+
project: Entity<Project>,
3838
cx: &mut App,
3939
) -> Task<Result<String>> {
40-
let Some(project) = project.upgrade() else {
41-
return Task::ready(Err(anyhow!("project dropped")));
42-
};
43-
4440
cx.spawn(|cx| async move {
4541
cx.update(|cx| {
4642
#[derive(Debug, Serialize)]

crates/assistant_tools/src/now_tool.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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};
6+
use gpui::{App, Entity, Task};
77
use project::Project;
88
use schemars::JsonSchema;
99
use serde::{Deserialize, Serialize};
@@ -42,7 +42,7 @@ impl Tool for NowTool {
4242
fn run(
4343
self: Arc<Self>,
4444
input: serde_json::Value,
45-
_project: WeakEntity<Project>,
45+
_project: Entity<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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use anyhow::{anyhow, Result};
55
use assistant_tool::Tool;
6-
use gpui::{App, Task, WeakEntity};
6+
use gpui::{App, Entity, Task};
77
use project::{Project, ProjectPath, WorktreeId};
88
use schemars::JsonSchema;
99
use serde::{Deserialize, Serialize};
@@ -37,13 +37,9 @@ impl Tool for ReadFileTool {
3737
fn run(
3838
self: Arc<Self>,
3939
input: serde_json::Value,
40-
project: WeakEntity<Project>,
40+
project: Entity<Project>,
4141
cx: &mut App,
4242
) -> Task<Result<String>> {
43-
let Some(project) = project.upgrade() else {
44-
return Task::ready(Err(anyhow!("project dropped")));
45-
};
46-
4743
let input = match serde_json::from_value::<ReadFileToolInput>(input) {
4844
Ok(input) => input,
4945
Err(err) => return Task::ready(Err(anyhow!(err))),

crates/context_server/src/context_server_tool.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::sync::Arc;
22

3-
use anyhow::{anyhow, bail};
3+
use anyhow::{anyhow, bail, Result};
44
use assistant_tool::Tool;
55
use gpui::{App, Entity, Task};
6+
use project::Project;
67

78
use crate::manager::ContextServerManager;
89
use crate::types;
@@ -49,11 +50,11 @@ impl Tool for ContextServerTool {
4950
}
5051

5152
fn run(
52-
self: std::sync::Arc<Self>,
53+
self: Arc<Self>,
5354
input: serde_json::Value,
54-
_project: gpui::WeakEntity<project::Project>,
55+
_project: Entity<Project>,
5556
cx: &mut App,
56-
) -> gpui::Task<gpui::Result<String>> {
57+
) -> Task<Result<String>> {
5758
if let Some(server) = self.server_manager.read(cx).get_server(&self.server_id) {
5859
cx.foreground_executor().spawn({
5960
let tool_name = self.tool.name.clone();

crates/scripting_tool/src/scripting_tool.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use project::Project;
44
pub(crate) use session::*;
55

66
use assistant_tool::{Tool, ToolRegistry};
7-
use gpui::{App, AppContext as _, Task, WeakEntity};
7+
use gpui::{App, AppContext as _, Entity, Task};
88
use schemars::JsonSchema;
99
use serde::Deserialize;
1010
use std::sync::Arc;
@@ -38,16 +38,13 @@ impl Tool for ScriptingTool {
3838
fn run(
3939
self: Arc<Self>,
4040
input: serde_json::Value,
41-
project: WeakEntity<Project>,
41+
project: Entity<Project>,
4242
cx: &mut App,
4343
) -> Task<anyhow::Result<String>> {
4444
let input = match serde_json::from_value::<ScriptingToolInput>(input) {
4545
Err(err) => return Task::ready(Err(err.into())),
4646
Ok(input) => input,
4747
};
48-
let Some(project) = project.upgrade() else {
49-
return Task::ready(Err(anyhow::anyhow!("project dropped")));
50-
};
5148

5249
let session = cx.new(|cx| Session::new(project, cx));
5350
let lua_script = input.lua_script;

0 commit comments

Comments
 (0)