Initial CodeCraft: web-based coding harness for AI models
Rust backend (axum/tokio): agent loop with streaming, tools (fs, bash, web_fetch, todo, ask_user, subagents, skills, craft snapshots/compare), permissions with modes and rules, Anthropic + OpenAI-compatible providers, MCP client (stdio/HTTP, elicitation), SQLite sessions, usage statistics, compaction, commented TOML config editable via toml_edit. React PWA: chat with live streaming, permission/question cards, modes (build/plan/craft), settings for providers/models/prices/MCP/permissions, raw config editor, statistics dashboard, i18n (en/de). Docker setup. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
f3a2fe3816
62 files changed
+21835
No files matched your search
@@ -0,0 +1,75 @@
|
||||
//! Fetch a URL and return readable text.
|
||||
|
||||
use super::{Tool, ToolCtx, ToolOutput, arg_str, truncate};
|
||||
use anyhow::{Result, bail};
|
||||
use async_trait::async_trait;
|
||||
use serde_json::{Value, json};
|
||||
use std::time::Duration;
|
||||
|
||||
const MAX_BYTES: usize = 5 * 1024 * 1024;
|
||||
const MAX_OUTPUT: usize = 80_000;
|
||||
|
||||
pub struct WebFetch;
|
||||
|
||||
#[async_trait]
|
||||
impl Tool for WebFetch {
|
||||
fn name(&self) -> &str {
|
||||
"web_fetch"
|
||||
}
|
||||
fn description(&self) -> String {
|
||||
"Fetch a URL (http/https). HTML is converted to readable text; JSON and plain text are \
|
||||
returned as-is. Set raw=true to get the unconverted body (e.g. HTML source)."
|
||||
.into()
|
||||
}
|
||||
fn schema(&self) -> Value {
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"url": {"type": "string"},
|
||||
"raw": {"type": "boolean"}
|
||||
},
|
||||
"required": ["url"]
|
||||
})
|
||||
}
|
||||
fn read_only(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn title(&self, input: &Value) -> String {
|
||||
format!("Fetch {}", input["url"].as_str().unwrap_or("?"))
|
||||
}
|
||||
async fn run(&self, input: Value, ctx: &ToolCtx) -> Result<ToolOutput> {
|
||||
let url = arg_str(&input, "url")?;
|
||||
if !(url.starts_with("http://") || url.starts_with("https://")) {
|
||||
bail!("only http(s) URLs are supported");
|
||||
}
|
||||
let raw = input["raw"].as_bool().unwrap_or(false);
|
||||
if raw && ctx.agent == "craft" && ctx.app.config().craft.clean_room {
|
||||
return Ok(ToolOutput::error(
|
||||
"Clean room mode: page source may not be read. Use browser_snapshot, or switch \
|
||||
craft.clean_room to false (deep mode) in the settings.",
|
||||
));
|
||||
}
|
||||
let resp = crate::provider::http_client()
|
||||
.get(url)
|
||||
.timeout(Duration::from_secs(30))
|
||||
.send()
|
||||
.await?;
|
||||
let status = resp.status();
|
||||
let ctype = resp
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let bytes = resp.bytes().await?;
|
||||
let bytes = &bytes[..bytes.len().min(MAX_BYTES)];
|
||||
let body = String::from_utf8_lossy(bytes);
|
||||
let text = if !raw && ctype.contains("html") {
|
||||
html2text::from_read(body.as_bytes(), 120).unwrap_or_else(|_| body.to_string())
|
||||
} else {
|
||||
body.to_string()
|
||||
};
|
||||
let out = format!("HTTP {status} ({ctype})\n\n{}", truncate(&text, MAX_OUTPUT));
|
||||
Ok(if status.is_success() { ToolOutput::text(out) } else { ToolOutput::error(out) })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user