tea-coding-tools 0.1.0

Workspace-scoped native coding tool adapters for tea-rs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;

use futures_util::stream;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tea_control::CancellationScope;
use tea_protocol::{
    ContentBlock, ProtocolMetadata, ToolIdempotency, ToolPresentation, WebFetchPresentation,
    WebFetchRedirect, WebFetchTruncation,
};
use tea_tools::{
    BoxToolExecutionStream, ToolConcurrency, ToolEffect, ToolExecutionEvent, ToolExecutionFailure,
    ToolExecutionSemantics, ToolExecutor, ToolName, ToolResult, ToolRetrySafety, ToolSpec,
    ToolSpecError, ToolTimeout, ToolVersion, ValidatedToolInvocation,
};
use thiserror::Error;

mod address_policy;
mod body;
mod cache;
mod http;
mod provider;
mod url_policy;

pub use address_policy::{FetchAddressPolicy, FetchAddressPolicyError, ValidatedFetchAddresses};
pub use body::{
    DEFAULT_FETCH_DECODED_BYTES, DEFAULT_FETCH_HTML_BYTES, DEFAULT_FETCH_HTML_ELEMENTS,
    DecodedFetchBody, FetchBodyDecoder, FetchBodyLimits, FetchContentKind, MAX_FETCH_DECODED_BYTES,
};
pub use cache::{
    DEFAULT_FETCH_CACHE_ENTRIES, DEFAULT_FETCH_CACHE_ENTRY_BYTES, DEFAULT_FETCH_CACHE_TOTAL_BYTES,
    DEFAULT_FETCH_CACHE_TTL, FetchCacheConfig, FetchCacheScope, FetchCacheStats, FetchResultCache,
    MAX_FETCH_CACHE_ENTRIES, MAX_FETCH_CACHE_ENTRY_BYTES, MAX_FETCH_CACHE_TOTAL_BYTES,
    MAX_FETCH_CACHE_TTL,
};
pub use http::{
    DEFAULT_FETCH_RESPONSE_BYTES, FetchDnsResolver, FetchHttpConfig, FetchHttpHeaders,
    FetchHttpLimits, FetchHttpResponse, FetchHttpTimeouts, FetchHttpTransport, FetchResolveFuture,
    MAX_FETCH_RESPONSE_BYTES, SystemFetchDnsResolver,
};
pub use provider::{FETCH_SECURITY_POLICY_VERSION, HttpFetchProvider};
pub use url_policy::{FetchUrlPolicy, FetchUrlPolicyError, FetchUrlScheme, ValidatedFetchUrl};

/// Maximum UTF-8 bytes accepted in a fetch URL.
pub const MAX_FETCH_URL_BYTES: usize = 2 * 1024;
/// Default number of body characters returned by the fetch tool.
pub const DEFAULT_FETCH_MAX_CHARS: usize = 20_000;
/// Maximum body characters retained by one fetch result.
pub const MAX_FETCH_MAX_CHARS: usize = 100_000;
/// Maximum title bytes retained by one fetch result.
pub const MAX_FETCH_TITLE_BYTES: usize = 4 * 1024;
/// Maximum MIME type bytes retained by one fetch result.
pub const MAX_FETCH_MIME_BYTES: usize = 128;
/// Maximum redirect records retained by one fetch result.
pub const MAX_FETCH_REDIRECTS: usize = 10;

/// Invalid provider-neutral fetch request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum FetchRequestError {
    /// URL is empty, oversized, malformed, or outside the initial scheme contract.
    #[error("web fetch URL is invalid")]
    InvalidUrl,
    /// Requested body character limit is outside the bounded contract.
    #[error("web fetch character limit is invalid")]
    InvalidMaxChars,
    /// The request contains an unsupported field or shape.
    #[error("web fetch request shape is invalid")]
    InvalidShape,
}

/// Bounded provider-neutral request passed to a client fetch backend.
#[derive(Clone, PartialEq, Eq)]
pub struct FetchRequest {
    url: String,
    max_chars: usize,
}

impl FetchRequest {
    /// Creates and validates a fetch request.
    ///
    /// DNS, IP-range checks, and per-hop connection authorization still belong
    /// to the HTTP transport. This constructor freezes the production URL
    /// syntax and scheme contract before the request reaches a provider.
    ///
    /// # Errors
    ///
    /// Rejects non-HTTPS URLs, credentials, unsafe ports, controls, and character
    /// limits outside `1..=100_000`. Fragments are removed from the canonical URL.
    pub fn new(url: impl Into<String>, max_chars: usize) -> Result<Self, FetchRequestError> {
        Self::new_with_policy(url, max_chars, &FetchUrlPolicy::production())
    }

    /// Creates a request under an explicit URL policy.
    ///
    /// This supports transport conformance tests that use loopback fixture
    /// servers. Product tool invocations use [`Self::new`].
    ///
    /// # Errors
    ///
    /// Returns the same bounded errors as [`Self::new`].
    pub fn new_with_policy(
        url: impl Into<String>,
        max_chars: usize,
        policy: &FetchUrlPolicy,
    ) -> Result<Self, FetchRequestError> {
        let raw_url = url.into();
        let url = policy
            .validate(&raw_url)
            .map_err(|_| FetchRequestError::InvalidUrl)?
            .as_str()
            .to_owned();
        if !(1..=MAX_FETCH_MAX_CHARS).contains(&max_chars) {
            return Err(FetchRequestError::InvalidMaxChars);
        }
        Ok(Self { url, max_chars })
    }

    /// Returns the normalized requested URL.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }

    /// Returns the requested body character limit.
    #[must_use]
    pub const fn max_chars(&self) -> usize {
        self.max_chars
    }
}

impl fmt::Debug for FetchRequest {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let validated = FetchUrlPolicy::production().validate(&self.url).ok();
        formatter
            .debug_struct("FetchRequest")
            .field("host", &validated.as_ref().map(ValidatedFetchUrl::host))
            .field("url_bytes", &self.url.len())
            .field("max_chars", &self.max_chars)
            .finish()
    }
}

/// Stable failure categories returned by client fetch backends.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FetchProviderErrorCode {
    /// Backend or credential configuration is invalid.
    InvalidConfiguration,
    /// The normalized request is invalid.
    InvalidRequest,
    /// The destination is forbidden by URL or address policy.
    ForbiddenDestination,
    /// DNS resolution failed or returned an unsafe answer.
    DnsResolution,
    /// A redirect was invalid, unsafe, cyclic, or exceeded bounds.
    RedirectRejected,
    /// The response exceeded a compressed or decoded byte limit.
    ResponseTooLarge,
    /// The response MIME type or content shape is unsupported.
    UnsupportedMime,
    /// The backend timed out.
    Timeout,
    /// The backend could not be reached or the peer was not the pinned address.
    Transport,
    /// The response was malformed or failed bounded extraction.
    MalformedResponse,
    /// Cooperative cancellation was requested.
    Cancelled,
}

/// Whether an upper layer may explicitly retry a failed fetch operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FetchRetryDisposition {
    /// Retrying could repeat an unsafe or deterministic failure.
    Never,
    /// A caller may explicitly retry after a pre-response transport failure.
    Explicit,
}

/// Secret-independent client fetch failure.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct FetchProviderError {
    code: FetchProviderErrorCode,
    message: &'static str,
    retry: FetchRetryDisposition,
}

impl FetchProviderError {
    /// Creates a failure with a stable, non-sensitive message.
    #[must_use]
    pub const fn new(code: FetchProviderErrorCode) -> Self {
        Self {
            code,
            message: fetch_error_message(code),
            retry: FetchRetryDisposition::Never,
        }
    }

    pub(crate) const fn explicit_retry(code: FetchProviderErrorCode) -> Self {
        Self {
            code,
            message: fetch_error_message(code),
            retry: FetchRetryDisposition::Explicit,
        }
    }

    /// Creates a cooperative cancellation failure.
    #[must_use]
    pub const fn cancelled() -> Self {
        Self::new(FetchProviderErrorCode::Cancelled)
    }

    /// Returns the stable category.
    #[must_use]
    pub const fn code(&self) -> FetchProviderErrorCode {
        self.code
    }

    /// Returns a bounded secret-independent technical message.
    #[must_use]
    pub const fn message(&self) -> &'static str {
        self.message
    }

    /// Returns the conservative retry classification.
    #[must_use]
    pub const fn retry_disposition(&self) -> FetchRetryDisposition {
        self.retry
    }
}

impl fmt::Debug for FetchProviderError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FetchProviderError")
            .field("code", &self.code)
            .field("message", &self.message)
            .field("retry", &self.retry)
            .finish()
    }
}

impl fmt::Display for FetchProviderError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.message)
    }
}

impl std::error::Error for FetchProviderError {}

const fn fetch_error_message(code: FetchProviderErrorCode) -> &'static str {
    match code {
        FetchProviderErrorCode::InvalidConfiguration => {
            "web fetch backend configuration is invalid"
        }
        FetchProviderErrorCode::InvalidRequest => "web fetch request is invalid",
        FetchProviderErrorCode::ForbiddenDestination => "web fetch destination is forbidden",
        FetchProviderErrorCode::DnsResolution => {
            "web fetch destination could not be resolved safely"
        }
        FetchProviderErrorCode::RedirectRejected => "web fetch redirect was rejected",
        FetchProviderErrorCode::ResponseTooLarge => "web fetch response exceeded its bounds",
        FetchProviderErrorCode::UnsupportedMime => "web fetch response MIME type is unsupported",
        FetchProviderErrorCode::Timeout => "web fetch backend timed out",
        FetchProviderErrorCode::Transport => "web fetch backend transport failed",
        FetchProviderErrorCode::MalformedResponse => "web fetch response is malformed",
        FetchProviderErrorCode::Cancelled => "web fetch was cancelled",
    }
}

/// Why a fetch body was truncated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FetchTruncationReason {
    /// Compressed bytes exceeded the transport bound.
    CompressedBytes,
    /// Decoded bytes exceeded the transport bound.
    DecodedBytes,
    /// Extracted body characters reached the request limit.
    BodyCharacters,
    /// The content parser reached its complexity or structure bound.
    ParserComplexity,
}

/// One bounded redirect record.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FetchRedirect {
    from: String,
    to: String,
    status: u16,
}

impl FetchRedirect {
    /// Creates a redirect record.
    ///
    /// # Errors
    ///
    /// Rejects malformed URLs, non-redirect status codes, and oversized values.
    pub fn new(
        from: impl Into<String>,
        to: impl Into<String>,
        status: u16,
    ) -> Result<Self, FetchProviderError> {
        Self::new_with_policy(from, to, status, &FetchUrlPolicy::production())
    }

    /// Creates a redirect record under an explicit URL policy.
    ///
    /// # Errors
    ///
    /// Returns the same bounded errors as [`Self::new`].
    pub fn new_with_policy(
        from: impl Into<String>,
        to: impl Into<String>,
        status: u16,
        policy: &FetchUrlPolicy,
    ) -> Result<Self, FetchProviderError> {
        if !(300..=399).contains(&status) {
            return Err(FetchProviderError::new(
                FetchProviderErrorCode::MalformedResponse,
            ));
        }
        let raw_from = from.into();
        let raw_to = to.into();
        let from = normalize_result_url(&raw_from, *policy)?;
        let to = normalize_result_url(&raw_to, *policy)?;
        Ok(Self { from, to, status })
    }

    /// Returns the source URL.
    #[must_use]
    pub fn from(&self) -> &str {
        &self.from
    }

    /// Returns the redirect target URL.
    #[must_use]
    pub fn to(&self) -> &str {
        &self.to
    }

    /// Returns the HTTP redirect status.
    #[must_use]
    pub const fn status(&self) -> u16 {
        self.status
    }
}

impl fmt::Debug for FetchRedirect {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FetchRedirect")
            .field("status", &self.status)
            .field("from_bytes", &self.from.len())
            .field("to_bytes", &self.to.len())
            .finish()
    }
}

/// Bounded normalized result returned by a client fetch backend.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FetchResult {
    requested_url: String,
    final_url: String,
    title: Option<String>,
    mime_type: String,
    body: String,
    truncated: Option<FetchTruncationReason>,
    redirects: Vec<FetchRedirect>,
}

impl FetchResult {
    /// Creates a result with bounded URLs, MIME, and body content.
    ///
    /// # Errors
    ///
    /// Rejects malformed URLs, unsupported MIME syntax, oversized body/title
    /// values, and more than ten redirect records.
    pub fn new(
        requested_url: impl Into<String>,
        final_url: impl Into<String>,
        mime_type: impl Into<String>,
        body: impl Into<String>,
    ) -> Result<Self, FetchProviderError> {
        Self::new_with_policy(
            requested_url,
            final_url,
            mime_type,
            body,
            &FetchUrlPolicy::production(),
        )
    }

    /// Creates a result under an explicit URL policy.
    ///
    /// This supports deterministic loopback transport tests without weakening
    /// the production constructor.
    ///
    /// # Errors
    ///
    /// Returns the same bounded errors as [`Self::new`].
    pub fn new_with_policy(
        requested_url: impl Into<String>,
        final_url: impl Into<String>,
        mime_type: impl Into<String>,
        body: impl Into<String>,
        policy: &FetchUrlPolicy,
    ) -> Result<Self, FetchProviderError> {
        let raw_requested_url = requested_url.into();
        let raw_final_url = final_url.into();
        let requested_url = normalize_result_url(&raw_requested_url, *policy)?;
        let final_url = normalize_result_url(&raw_final_url, *policy)?;
        let raw_mime_type = mime_type.into();
        let mime_type = validate_mime_type(&raw_mime_type)?;
        let body = body.into();
        if body.chars().count() > MAX_FETCH_MAX_CHARS || body.contains('\0') {
            return Err(FetchProviderError::new(
                FetchProviderErrorCode::ResponseTooLarge,
            ));
        }
        Ok(Self {
            requested_url,
            final_url,
            title: None,
            mime_type,
            body,
            truncated: None,
            redirects: Vec::new(),
        })
    }

    /// Adds an optional bounded document title.
    ///
    /// # Errors
    ///
    /// Rejects control characters or titles over 4 KiB.
    pub fn with_title(mut self, title: impl Into<String>) -> Result<Self, FetchProviderError> {
        let title = title.into();
        if title.is_empty()
            || title.len() > MAX_FETCH_TITLE_BYTES
            || title.chars().any(char::is_control)
        {
            return Err(FetchProviderError::new(
                FetchProviderErrorCode::MalformedResponse,
            ));
        }
        self.title = Some(title);
        Ok(self)
    }

    /// Marks the result as truncated for a bounded reason.
    #[must_use]
    pub const fn with_truncation(mut self, reason: FetchTruncationReason) -> Self {
        self.truncated = Some(reason);
        self
    }

    /// Adds bounded redirect metadata.
    ///
    /// # Errors
    ///
    /// Rejects more than ten records.
    pub fn with_redirects(
        mut self,
        redirects: Vec<FetchRedirect>,
    ) -> Result<Self, FetchProviderError> {
        if redirects.len() > MAX_FETCH_REDIRECTS {
            return Err(FetchProviderError::new(
                FetchProviderErrorCode::MalformedResponse,
            ));
        }
        self.redirects = redirects;
        Ok(self)
    }

    /// Returns the originally requested URL.
    #[must_use]
    pub fn requested_url(&self) -> &str {
        &self.requested_url
    }

    /// Returns the final URL after bounded redirects.
    #[must_use]
    pub fn final_url(&self) -> &str {
        &self.final_url
    }

    /// Returns the optional extracted document title.
    #[must_use]
    pub fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    /// Returns the normalized MIME type.
    #[must_use]
    pub fn mime_type(&self) -> &str {
        &self.mime_type
    }

    /// Returns the bounded extracted body.
    #[must_use]
    pub fn body(&self) -> &str {
        &self.body
    }

    /// Returns the truncation reason, if the body was bounded.
    #[must_use]
    pub const fn truncation(&self) -> Option<FetchTruncationReason> {
        self.truncated
    }

    /// Returns normalized redirect records.
    #[must_use]
    pub fn redirects(&self) -> &[FetchRedirect] {
        &self.redirects
    }
}

impl fmt::Debug for FetchResult {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FetchResult")
            .field("mime_type", &self.mime_type)
            .field("body_chars", &self.body.chars().count())
            .field("has_title", &self.title.is_some())
            .field("truncated", &self.truncated)
            .field("redirect_count", &self.redirects.len())
            .finish_non_exhaustive()
    }
}

fn normalize_result_url(value: &str, policy: FetchUrlPolicy) -> Result<String, FetchProviderError> {
    policy
        .validate(value)
        .map(|validated| validated.as_str().to_owned())
        .map_err(|_| FetchProviderError::new(FetchProviderErrorCode::MalformedResponse))
}

fn validate_mime_type(value: &str) -> Result<String, FetchProviderError> {
    let valid = !value.is_empty()
        && value.len() <= MAX_FETCH_MIME_BYTES
        && value.bytes().all(|byte| {
            byte.is_ascii_alphanumeric()
                || matches!(byte, b'/' | b'.' | b'+' | b'-' | b';' | b'=' | b' ')
        });
    if valid {
        Ok(value.to_ascii_lowercase())
    } else {
        Err(FetchProviderError::new(
            FetchProviderErrorCode::MalformedResponse,
        ))
    }
}

/// Object-safe future returned by [`FetchProvider`].
pub type FetchFuture<'a> =
    Pin<Box<dyn Future<Output = Result<FetchResult, FetchProviderError>> + Send + 'a>>;

/// Object-safe client web-fetch backend port.
pub trait FetchProvider: fmt::Debug + Send + Sync {
    /// Executes one bounded request with cooperative cancellation.
    fn fetch(&self, request: FetchRequest, cancellation: CancellationScope) -> FetchFuture<'_>;
}

/// Client executor for the stable `web_fetch` tool contract.
#[derive(Debug, Clone)]
pub struct WebFetchTool {
    provider: Arc<dyn FetchProvider>,
}

impl WebFetchTool {
    /// Creates a client fetch executor.
    #[must_use]
    pub fn new(provider: Arc<dyn FetchProvider>) -> Self {
        Self { provider }
    }

    /// Builds the strict client fetch tool contract.
    ///
    /// # Errors
    ///
    /// Returns an error only if the static contract violates tool bounds.
    pub fn spec() -> Result<ToolSpec, ToolSpecError> {
        ToolSpec::new(
            ToolName::from_str("web_fetch").map_err(|_| ToolSpecError::InvalidDescription)?,
            ToolVersion::from_str("1.0.0").map_err(|_| ToolSpecError::InvalidDescription)?,
            "Fetch bounded public web content from an explicitly provided HTTPS URL.",
            json!({
                "type":"object",
                "properties":{
                    "url":{"type":"string","minLength":1,"maxLength":MAX_FETCH_URL_BYTES},
                    "max_chars":{"type":"integer","minimum":1,"maximum":MAX_FETCH_MAX_CHARS}
                },
                "required":["url"],
                "additionalProperties":false
            }),
            json!({
                "type":"object",
                "properties":{
                    "requested_url":{"type":"string"},
                    "final_url":{"type":"string"},
                    "title":{"type":["string","null"]},
                    "mime_type":{"type":"string"},
                    "body":{"type":"string"},
                    "truncated":{"type":["string","null"]},
                    "redirects":{"type":"array","maxItems":MAX_FETCH_REDIRECTS}
                },
                "required":["requested_url","final_url","title","mime_type","body","truncated","redirects"],
                "additionalProperties":false
            }),
            [ToolEffect::NetworkRequest],
            ToolExecutionSemantics::new(
                ToolIdempotency::Idempotent,
                ToolRetrySafety::ExplicitOnly,
                ToolConcurrency::Parallel,
                ToolTimeout::from_millis(60_000)?,
            )?,
        )?
        .with_prompt_hint(
            "Use web_fetch only for an explicitly requested public URL; it does not search the web.",
        )
    }

    async fn run(
        &self,
        invocation: &ValidatedToolInvocation,
        cancellation: CancellationScope,
    ) -> ToolExecutionEvent {
        if cancellation.is_cancelled() {
            return ToolExecutionEvent::Failed(ToolExecutionFailure::cancelled());
        }
        let Ok(request) = request_from_invocation(invocation) else {
            return provider_failure(FetchProviderError::new(
                FetchProviderErrorCode::InvalidRequest,
            ));
        };
        match self.provider.fetch(request, cancellation).await {
            Ok(result) => fetch_success(&result),
            Err(error) => provider_failure(error),
        }
    }
}

impl ToolExecutor for WebFetchTool {
    fn execute(
        &self,
        invocation: ValidatedToolInvocation,
        cancellation: CancellationScope,
    ) -> BoxToolExecutionStream {
        let executor = self.clone();
        Box::pin(stream::once(async move {
            executor.run(&invocation, cancellation).await
        }))
    }
}

fn request_from_invocation(
    invocation: &ValidatedToolInvocation,
) -> Result<FetchRequest, FetchRequestError> {
    let url = invocation
        .arguments()
        .get("url")
        .and_then(Value::as_str)
        .ok_or(FetchRequestError::InvalidUrl)?;
    let max_chars = invocation
        .arguments()
        .get("max_chars")
        .and_then(Value::as_u64)
        .map(usize::try_from)
        .transpose()
        .map_err(|_| FetchRequestError::InvalidMaxChars)?
        .unwrap_or(DEFAULT_FETCH_MAX_CHARS);
    FetchRequest::new(url, max_chars)
}

fn fetch_success(result: &FetchResult) -> ToolExecutionEvent {
    let redirects = result
        .redirects()
        .iter()
        .map(|redirect| {
            json!({
                "from": redirect.from(),
                "to": redirect.to(),
                "status": redirect.status(),
            })
        })
        .collect::<Vec<_>>();
    let Ok(presentation) = web_fetch_presentation(result) else {
        return ToolExecutionEvent::Failed(ToolExecutionFailure::internal_contract());
    };
    let visible = web_fetch_visible_text(&presentation);
    let output = json!({
        "requested_url": result.requested_url(),
        "final_url": result.final_url(),
        "title": result.title(),
        "mime_type": result.mime_type(),
        "body": result.body(),
        "truncated": result.truncation().map(truncation_code),
        "redirects": redirects,
    });
    let Ok(content) = ContentBlock::text(visible) else {
        return ToolExecutionEvent::Failed(ToolExecutionFailure::internal_contract());
    };
    let Ok(result) = ToolResult::new(vec![content], output) else {
        return ToolExecutionEvent::Failed(ToolExecutionFailure::internal_contract());
    };
    ToolExecutionEvent::Finished(
        result.with_presentation(ToolPresentation::WebFetch(Box::new(presentation))),
    )
}

fn web_fetch_presentation(
    result: &FetchResult,
) -> Result<WebFetchPresentation, tea_protocol::ExternalContentError> {
    let mut presentation = WebFetchPresentation::new(
        result.requested_url(),
        result.final_url(),
        result.mime_type(),
        result.body(),
    )?;
    if let Some(title) = result.title() {
        presentation = presentation.with_title(title)?;
    }
    if let Some(truncation) = result.truncation() {
        presentation = presentation.with_truncation(match truncation {
            FetchTruncationReason::CompressedBytes => WebFetchTruncation::CompressedBytes,
            FetchTruncationReason::DecodedBytes => WebFetchTruncation::DecodedBytes,
            FetchTruncationReason::BodyCharacters => WebFetchTruncation::BodyCharacters,
            FetchTruncationReason::ParserComplexity => WebFetchTruncation::ParserComplexity,
        });
    }
    let redirects = result
        .redirects()
        .iter()
        .map(|redirect| WebFetchRedirect::new(redirect.from(), redirect.to(), redirect.status()))
        .collect::<Result<Vec<_>, _>>()?;
    presentation.with_redirects(redirects)
}

fn web_fetch_visible_text(presentation: &WebFetchPresentation) -> String {
    let mut visible = format!(
        "Fetched URL: {}\nContent-Type: {}",
        presentation.final_url(),
        presentation.mime_type()
    );
    if presentation.requested_url() != presentation.final_url() {
        visible.push_str("\nRequested URL: ");
        visible.push_str(presentation.requested_url());
    }
    if let Some(title) = presentation.title() {
        visible.push_str("\nTitle: ");
        visible.push_str(title);
    }
    if let Some(truncation) = presentation.truncation() {
        visible.push_str("\nTruncated: ");
        visible.push_str(match truncation {
            WebFetchTruncation::CompressedBytes => "compressed_bytes",
            WebFetchTruncation::DecodedBytes => "decoded_bytes",
            WebFetchTruncation::BodyCharacters => "body_characters",
            WebFetchTruncation::ParserComplexity => "parser_complexity",
        });
    }
    if !presentation.redirects().is_empty() {
        visible.push_str("\nRedirects: ");
        visible.push_str(&presentation.redirects().len().to_string());
    }
    visible.push_str("\n\n");
    visible.push_str(presentation.body());
    visible
}

fn provider_failure(error: FetchProviderError) -> ToolExecutionEvent {
    if error.code() == FetchProviderErrorCode::Cancelled {
        return ToolExecutionEvent::Failed(ToolExecutionFailure::cancelled());
    }
    let details = ProtocolMetadata::from_entries([(
        "dev.tea-rs.web-fetch",
        json!({
            "code":fetch_error_code(error.code()),
            "retry":fetch_retry_code(error.retry_disposition()),
        }),
    )])
    .unwrap_or_default();
    let failure = ToolExecutionFailure::execution(error.message())
        .unwrap_or_else(|_| ToolExecutionFailure::internal_contract())
        .with_details(details);
    ToolExecutionEvent::Failed(failure)
}

const fn fetch_retry_code(disposition: FetchRetryDisposition) -> &'static str {
    match disposition {
        FetchRetryDisposition::Never => "never",
        FetchRetryDisposition::Explicit => "explicit",
    }
}

const fn fetch_error_code(code: FetchProviderErrorCode) -> &'static str {
    match code {
        FetchProviderErrorCode::InvalidConfiguration => "invalid_configuration",
        FetchProviderErrorCode::InvalidRequest => "invalid_request",
        FetchProviderErrorCode::ForbiddenDestination => "forbidden_destination",
        FetchProviderErrorCode::DnsResolution => "dns_resolution",
        FetchProviderErrorCode::RedirectRejected => "redirect_rejected",
        FetchProviderErrorCode::ResponseTooLarge => "response_too_large",
        FetchProviderErrorCode::UnsupportedMime => "unsupported_mime",
        FetchProviderErrorCode::Timeout => "timeout",
        FetchProviderErrorCode::Transport => "transport",
        FetchProviderErrorCode::MalformedResponse => "malformed_response",
        FetchProviderErrorCode::Cancelled => "cancelled",
    }
}

const fn truncation_code(reason: FetchTruncationReason) -> &'static str {
    match reason {
        FetchTruncationReason::CompressedBytes => "compressed_bytes",
        FetchTruncationReason::DecodedBytes => "decoded_bytes",
        FetchTruncationReason::BodyCharacters => "body_characters",
        FetchTruncationReason::ParserComplexity => "parser_complexity",
    }
}