feat: webfetch

This commit is contained in:
leookun committed 2026-08-30 11:54:32 +08:00
1 parent 66ac94d5b1
commit 2ff74bc6a8
11 files changed
+185 -28

No files matched your search

+9 -1
View File
@@ -9,6 +9,7 @@ use crate::{
model::{ConversationId, RunId},
provider::Provider,
run::{CommandResult, RunHandle},
search::WebCache,
store::Store,
};
@@ -24,6 +25,7 @@ pub(crate) struct ConversationDependencies {
pub store: Store,
pub provider: Arc<dyn Provider>,
pub compiler: PromptCompiler,
pub web_cache: WebCache,
}
struct RegistryInner {
@@ -40,7 +42,12 @@ struct ActiveRun {
}
impl ConversationRegistry {
pub fn new(store: Store, provider: Arc<dyn Provider>, compiler: PromptCompiler) -> Self {
pub fn new(
store: Store,
provider: Arc<dyn Provider>,
compiler: PromptCompiler,
web_cache: WebCache,
) -> Self {
Self {
inner: Arc::new(RegistryInner {
current: Mutex::new(HashMap::new()),
@@ -50,6 +57,7 @@ impl ConversationRegistry {
store,
provider,
compiler,
web_cache,
},
}),
}
@@ -147,6 +147,7 @@ impl ConversationRuntime {
tool_runtime.clone(),
results.clone(),
dependencies.store.clone(),
dependencies.web_cache.clone(),
);
let generation = RunGeneration {
superseded: CancellationToken::new(),
+3 -2
View File
@@ -18,7 +18,7 @@ pub(crate) mod tool_call_result;
use crate::{
model::{CanonicalMessage, MessageContent, Role, ToolCall},
search::{WebFetch, WebSearch},
search::{WebCache, WebFetch, WebSearch},
store::Store,
Error, Result,
};
@@ -72,12 +72,13 @@ impl ToolDispatcher {
runtime: CursorToolRuntime,
results: ToolResultSender,
store: Store,
web_cache: WebCache,
) -> Self {
Self {
runtime,
results,
search: WebSearch::managed(store.clone()),
fetch: WebFetch::managed(store.clone()),
fetch: WebFetch::managed(store.clone(), web_cache),
store: Some(store),
edit_schedule: Arc::new(Mutex::new(EditSchedule::default())),
}
@@ -235,12 +235,23 @@ pub(crate) fn complete_web_fetch(
};
let (output, is_error) = match outcome {
Ok(page) => {
let output = page.markdown.clone();
let output = match page.cache.as_ref() {
Some(cache) => format!(
"<system_reminder>Web content has been downloaded to: {}. If the content is omitted and you need it, use Shell to download it to a temporary directory, then use an appropriate tool to read it in pages.</system_reminder>\n{}",
cache.url, page.markdown
),
None => page.markdown.clone(),
};
let output_location = page.cache.map(|cache| pb::OutputLocation {
file_path: cache.file_path,
size_bytes: cache.size_bytes,
line_count: cache.line_count,
});
tool.result = Some(pb::WebFetchResult {
result: Some(pb::web_fetch_result::Result::Success(pb::WebFetchSuccess {
url: page.url,
markdown: page.markdown,
output_location: None,
output_location,
})),
});
(output, false)
@@ -328,3 +339,75 @@ fn switch_mode_result(
fn missing(name: &str) -> Error {
Error::Protocol(format!("{name} returned no result"))
}
#[cfg(test)]
mod tests {
use serde_json::json;
use super::{complete_web_fetch, PendingInteraction};
use crate::{
cursor::protocol::proto::agent::v1 as pb,
model::ToolCall,
search::{FetchedPage, WebCacheEntry},
};
#[test]
fn web_fetch_result_leads_with_cache_reminder_and_bounds_cursor_payload() {
let markdown = "x".repeat(40 * 1024);
let location = "http://127.0.0.1:4312/web-cache/550e8400-e29b-41d4-a716-446655440000.txt";
let completion = complete_web_fetch(
pending_fetch(),
Ok(FetchedPage {
url: "https://example.com/final".into(),
markdown: markdown.clone(),
cache: Some(WebCacheEntry {
url: location.into(),
file_path: "C:/Users/test/.cursor-byok-v3/cache/web/page.txt".into(),
size_bytes: markdown.len() as i64,
line_count: 1,
}),
}),
)
.unwrap();
assert!(completion.result().content.starts_with(&format!(
"<system_reminder>Web content has been downloaded to: {location}."
)));
assert!(completion.result().content.contains("[truncated: WebFetch"));
let Some(pb::tool_call::Tool::WebFetchToolCall(tool)) =
completion.tool_call().tool.as_ref()
else {
panic!("expected WebFetchToolCall")
};
let Some(pb::web_fetch_result::Result::Success(success)) = tool
.result
.as_ref()
.and_then(|result| result.result.as_ref())
else {
panic!("expected WebFetchSuccess")
};
assert!(success.markdown.len() <= 32 * 1024);
assert!(success.markdown.contains("[truncated: WebFetch"));
assert_eq!(
success
.output_location
.as_ref()
.map(|location| location.file_path.as_str()),
Some("C:/Users/test/.cursor-byok-v3/cache/web/page.txt")
);
}
fn pending_fetch() -> PendingInteraction {
PendingInteraction {
call: ToolCall {
index: 0,
call_id: "fetch-call".into(),
model_call_id: "model-call".into(),
name: "WebFetch".into(),
arguments_text: r#"{"url":"https://example.com"}"#.into(),
arguments: json!({"url": "https://example.com"}),
},
started_at_ms: 1,
}
}
}
+22 -1
View File
@@ -10,6 +10,7 @@ use crate::{
services::observability::CursorTraceRecorder,
},
provider::Provider,
search::WebCache,
store::Store,
Result,
};
@@ -26,6 +27,7 @@ struct RegistryInner {
upstream: Mutex<HashMap<String, u64>>,
route_changed: Notify,
store: Store,
web_cache: WebCache,
conversations: ConversationRegistry,
}
@@ -37,13 +39,28 @@ pub enum TransportRoute {
impl TransportRegistry {
pub fn new(store: Store, provider: Arc<dyn Provider>, compiler: PromptCompiler) -> Self {
Self::with_web_cache(store, provider, compiler, WebCache::default())
}
pub fn with_web_cache(
store: Store,
provider: Arc<dyn Provider>,
compiler: PromptCompiler,
web_cache: WebCache,
) -> Self {
Self {
inner: Arc::new(RegistryInner {
local: Mutex::new(HashMap::new()),
upstream: Mutex::new(HashMap::new()),
route_changed: Notify::new(),
conversations: ConversationRegistry::new(store.clone(), provider, compiler),
conversations: ConversationRegistry::new(
store.clone(),
provider,
compiler,
web_cache.clone(),
),
store,
web_cache,
}),
}
}
@@ -52,6 +69,10 @@ impl TransportRegistry {
&self.inner.store
}
pub fn web_cache(&self) -> &WebCache {
&self.inner.web_cache
}
pub fn conversations(&self) -> &ConversationRegistry {
&self.inner.conversations
}