Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions crates/plugin-web/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl WebFetch {
"type": "integer",
"minimum": 1,
"maximum": ceiling,
"description": "Maximum number of characters of text to return for this call. Clamped to the configured ceiling. Longer text is truncated on a character boundary and the result is flagged as truncated."
"description": "Maximum number of characters of text to return for this call. Clamped to the configured ceiling. Defaults to a tenth of the model's context window, at about four characters a token, up to that ceiling. Longer text is truncated on a character boundary and the result is flagged as truncated."
},
"raw": {
"type": "boolean",
Expand All @@ -355,7 +355,7 @@ impl WebFetch {
/// Returns a [`ToolError`] for invalid arguments, a URL the policy
/// refuses outright, or a fetch task that did not finish. A page the
/// fetch could not render is a soft, trusted output instead.
pub(crate) async fn call(&self, args: serde_json::Value) -> CallResult {
pub(crate) async fn call(&self, args: serde_json::Value, model: Option<&str>) -> CallResult {
let url = args
.get("url")
.and_then(serde_json::Value::as_str)
Expand All @@ -367,8 +367,9 @@ impl WebFetch {
})?;

// A per-call `max_chars` is clamped to the configured ceiling; absent,
// the ceiling itself applies.
let max_chars = parse_max_chars(&args, self.client.config.max_chars())?;
// the default for the calling model applies.
let ceiling = self.client.config.max_chars();
let max_chars = parse_max_chars(&args, ceiling, default_max_chars(model, ceiling))?;

let raw = parse_raw(&args)?;

Expand All @@ -387,17 +388,43 @@ impl WebFetch {
}
}

/// Parses the optional `max_chars` argument, clamped to `ceiling`.
/// The `max_chars` a call that leaves it out gets: a tenth of the calling
/// model's context window, at about four characters a token, so two fifths
/// of a character per token, and never over `ceiling`. `model` is the JSON
/// text [`ToolCall::model`] lends, and a call with no model, as from a
/// script, or with no positive whole `context` in it, gets `ceiling`.
///
/// [`ToolCall::model`]: runvil_plugin_sdk::ToolCall::model
fn default_max_chars(model: Option<&str>, ceiling: usize) -> usize {
let context = model
.and_then(|model| serde_json::from_str::<serde_json::Value>(model).ok())
.and_then(|model| model.get("context")?.as_u64())
.filter(|context| *context > 0);
let Some(context) = context else {
return ceiling;
};
let chars = context.saturating_mul(2) / 5;
usize::try_from(chars)
.unwrap_or(usize::MAX)
.clamp(1, ceiling)
}

/// Parses the optional `max_chars` argument, clamped to `ceiling`, and
/// `default` when the call leaves it out.
///
/// # Errors
/// Returns an invalid-arguments [`ToolError`] if `max_chars` is present but is
/// not a positive integer.
fn parse_max_chars(args: &serde_json::Value, ceiling: usize) -> Result<usize, ToolError> {
fn parse_max_chars(
args: &serde_json::Value,
ceiling: usize,
default: usize,
) -> Result<usize, ToolError> {
let Some(value) = args.get("max_chars") else {
return Ok(ceiling);
return Ok(default);
};
if value.is_null() {
return Ok(ceiling);
return Ok(default);
}
let n = value.as_u64().filter(|n| *n >= 1).ok_or_else(|| {
ToolError::new(
Expand Down
48 changes: 27 additions & 21 deletions crates/plugin-web/src/fetch/tests-body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn oversized_html_is_refused() {

let url = format!("http://localhost:{port}/large");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an oversized HTML body is a soft return")
.text()
Expand All @@ -41,7 +41,7 @@ async fn declared_content_length_over_cap_is_refused_before_read() {

let url = format!("http://localhost:{port}/liar");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a declared Content-Length over the cap is a soft return")
.text()
Expand All @@ -64,7 +64,7 @@ async fn gzip_bomb_refused_on_decompressed_count() {

let url = format!("http://localhost:{port}/gzip");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a gzip body that decompresses past the cap is a soft return")
.text()
Expand All @@ -82,7 +82,7 @@ async fn a_short_article_below_a_long_menu_is_returned_and_raw_returns_the_menu(
let url = format!("http://localhost:{port}/archive");

let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an archive fetch must succeed")
.text()
Expand All @@ -99,7 +99,7 @@ async fn a_short_article_below_a_long_menu_is_returned_and_raw_returns_the_menu(
assert!(!body.contains("Archive entry"), "got: {body}");

let out = tool
.call(serde_json::json!({ "url": url, "raw": true }))
.call(serde_json::json!({ "url": url, "raw": true }), None)
.await
.expect("a raw archive fetch must succeed")
.text()
Expand All @@ -125,7 +125,7 @@ async fn a_page_with_no_article_over_max_chars_returns_the_cut_whole_page() {
let url = format!("http://localhost:{port}/topics");

let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a topics fetch must succeed")
.text()
Expand All @@ -148,7 +148,7 @@ async fn a_short_article_under_leading_spaces_is_returned_when_they_push_the_pag
let url = format!("http://localhost:{port}/padded");

let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a padded fetch must succeed")
.text()
Expand All @@ -173,7 +173,10 @@ async fn text_over_max_chars_is_truncated_on_char_boundary() {
let max_chars = 25usize;
let url = format!("http://localhost:{port}/unicode");
let out = tool
.call(serde_json::json!({ "url": url, "max_chars": max_chars }))
.call(
serde_json::json!({ "url": url, "max_chars": max_chars }),
None,
)
.await
.expect("a unicode fetch through allow_exact must succeed")
.text()
Expand All @@ -198,7 +201,10 @@ async fn per_call_max_chars_is_clamped_to_the_configured_ceiling() {

let url = format!("http://localhost:{port}/plainbig");
let out = tool
.call(serde_json::json!({ "url": url, "max_chars": 1_000_000 }))
.call(
serde_json::json!({ "url": url, "max_chars": 1_000_000 }),
None,
)
.await
.expect("a plain fetch must succeed")
.text()
Expand All @@ -225,7 +231,7 @@ async fn body_one_byte_under_cap_succeeds_untruncated() {

let url = format!("http://localhost:{port}/");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a body one byte under the cap must be accepted")
.text()
Expand All @@ -243,7 +249,7 @@ async fn html_is_extracted_and_reports_readability() {

let url = format!("http://localhost:{port}/");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a loopback html fetch must succeed")
.text()
Expand All @@ -261,7 +267,7 @@ async fn raw_forces_whole_page_render_keeping_table() {

let url = format!("http://localhost:{port}/table");
let out = tool
.call(serde_json::json!({ "url": url, "raw": true }))
.call(serde_json::json!({ "url": url, "raw": true }), None)
.await
.expect("a raw table fetch must succeed")
.text()
Expand All @@ -282,7 +288,7 @@ async fn json_is_returned_verbatim_as_plain() {

let url = format!("http://localhost:{port}/json");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a json fetch must succeed")
.text()
Expand All @@ -304,7 +310,7 @@ async fn oversized_json_is_hard_refused_not_truncated() {

let url = format!("http://localhost:{port}/jsonbig");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an oversized json body is a soft return")
.text()
Expand All @@ -326,7 +332,7 @@ async fn flat_text_body_read_failure_is_soft() {
// hard error: identical to the HTML and structured routes. A `text()`
// return proves the outcome was soft trusted output.
let outcome = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a mid-stream flat-text failure must be a soft return, not a hard error");
assert!(
Expand All @@ -347,7 +353,7 @@ async fn unrecognized_charset_is_refused_naming_the_label() {

let url = format!("http://localhost:{port}/badcharset");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an unrecognized charset is a soft return")
.text()
Expand All @@ -363,7 +369,7 @@ async fn pdf_is_refused_naming_the_type() {

let url = format!("http://localhost:{port}/pdf");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a pdf response is a soft return")
.text()
Expand All @@ -379,7 +385,7 @@ async fn octet_stream_is_refused() {

let url = format!("http://localhost:{port}/octet");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an octet-stream response is a soft return")
.text()
Expand All @@ -395,7 +401,7 @@ async fn absent_content_type_is_refused() {

let url = format!("http://localhost:{port}/notype");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an absent content type is a soft return")
.text()
Expand All @@ -411,7 +417,7 @@ async fn latin1_page_decodes_with_declared_charset() {

let url = format!("http://localhost:{port}/latin1");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a latin-1 fetch must succeed")
.text()
Expand All @@ -434,7 +440,7 @@ async fn plain_text_over_cap_is_truncated_not_refused() {

let url = format!("http://localhost:{port}/plainbig");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("an oversized flat-text body must be truncated, not refused")
.text()
Expand Down
5 changes: 4 additions & 1 deletion crates/plugin-web/src/fetch/tests-golden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ async fn spawn_golden_server() -> u16 {
async fn fetch(path: &str, raw: bool) -> String {
let port = spawn_golden_server().await;
let out = loopback_tool(port)
.call(serde_json::json!({ "url": format!("http://localhost:{port}{path}"), "raw": raw }))
.call(
serde_json::json!({ "url": format!("http://localhost:{port}{path}"), "raw": raw }),
None,
)
.await
.expect("a loopback fetch of a golden page must succeed")
.text()
Expand Down
22 changes: 11 additions & 11 deletions crates/plugin-web/src/fetch/tests-policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn slow_server_past_total_timeout_yields_timeout() {

let url = format!("http://localhost:{port}/slow");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a timeout is a soft (recoverable) return")
.text()
Expand All @@ -37,7 +37,7 @@ async fn request_omits_the_cookie_and_authorization_headers() {
let tool = loopback_tool(port);

let url = format!("http://localhost:{port}/record");
tool.call(serde_json::json!({ "url": url }))
tool.call(serde_json::json!({ "url": url }), None)
.await
.expect("a loopback fetch through allow_exact must succeed");

Expand All @@ -58,7 +58,7 @@ async fn no_credential_or_referer_survives_a_redirect() {
// A query-bearing source URL redirecting to a distinct allowed path: the
// target must receive no Cookie, Authorization, or Referer.
let url = format!("http://localhost:{port}/redir-record?secret=leak-me");
tool.call(serde_json::json!({ "url": url }))
tool.call(serde_json::json!({ "url": url }), None)
.await
.expect("a redirect between loopback paths must succeed");

Expand All @@ -82,7 +82,7 @@ async fn fetch_returns_provenance_line_then_content() {

let url = format!("http://localhost:{port}/");
let out = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a loopback fetch through allow_exact must succeed")
.text()
Expand All @@ -100,7 +100,7 @@ async fn redirect_to_internal_is_refused_and_target_untouched() {

let url = format!("http://localhost:{port}/redir");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a redirect-target policy refusal is a soft (recoverable) return")
.text()
Expand Down Expand Up @@ -161,7 +161,7 @@ async fn redirect_to_internal_via_injected_lookup_never_contacts_target() {
let url = format!("http://allowed.test:{redir_port}/go");
// The outcome may be a hard error (the redirect address is blocked) or a
// soft return, but it must never include the internal target's body.
if let Ok(output) = tool.call(serde_json::json!({ "url": url })).await {
if let Ok(output) = tool.call(serde_json::json!({ "url": url }), None).await {
assert!(
!output.text().contains("reached the internal target"),
"the internal target body must never be returned"
Expand Down Expand Up @@ -193,7 +193,7 @@ async fn call_rejects_bad_urls_before_network() {

for (raw, reason) in hard_cases {
let err = tool
.call(serde_json::json!({ "url": raw }))
.call(serde_json::json!({ "url": raw }), None)
.await
.expect_err(&format!("expected {raw} to be refused before any network"));
assert!(
Expand All @@ -207,7 +207,7 @@ async fn call_rejects_bad_urls_before_network() {
}

let soft = tool
.call(serde_json::json!({ "url": "http://example.com/" }))
.call(serde_json::json!({ "url": "http://example.com/" }), None)
.await
.expect("blocked http scheme must be soft tool text")
.text()
Expand All @@ -225,7 +225,7 @@ async fn soft_return_on_404() {

let url = format!("http://localhost:{port}/notfound");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a 404 must be a soft return")
.text()
Expand All @@ -241,7 +241,7 @@ async fn soft_return_on_500() {

let url = format!("http://localhost:{port}/error500");
let result = tool
.call(serde_json::json!({ "url": url }))
.call(serde_json::json!({ "url": url }), None)
.await
.expect("a 500 must be a soft return")
.text()
Expand All @@ -255,7 +255,7 @@ async fn blocked_url_still_hard_fails() {
let tool = default_tool();

let err = tool
.call(serde_json::json!({ "url": "https://1.2.3.4/secret" }))
.call(serde_json::json!({ "url": "https://1.2.3.4/secret" }), None)
.await
.expect_err("a bare IP literal URL must still be a hard error");

Expand Down
Loading
Loading