use std::sync::Arc;
use adk_core::{Result, Tool, ToolContext};
use async_trait::async_trait;
use serde_json::{Value, json};
use crate::error::DevToolError;
use crate::workspace::Workspace;
pub const DEFAULT_MAX_INLINE_BYTES: usize = 4 * 1024 * 1024;
pub struct ReadFileTool {
workspace: Workspace,
max_inline_bytes: usize,
inline_data: bool,
}
impl ReadFileTool {
pub fn new(workspace: Workspace) -> Self {
Self { workspace, max_inline_bytes: DEFAULT_MAX_INLINE_BYTES, inline_data: true }
}
#[must_use]
pub fn max_inline_bytes(mut self, bytes: usize) -> Self {
self.max_inline_bytes = bytes.min(adk_core::MAX_INLINE_DATA_SIZE);
self
}
#[must_use]
pub fn text_only(mut self) -> Self {
self.inline_data = false;
self
}
}
pub fn sniff_inline_mime(bytes: &[u8]) -> Option<&'static str> {
if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
Some("image/png")
} else if bytes.starts_with(b"\xFF\xD8\xFF") {
Some("image/jpeg")
} else if bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") {
Some("image/gif")
} else if bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP" {
Some("image/webp")
} else if bytes.starts_with(b"%PDF-") {
Some("application/pdf")
} else {
None
}
}
#[async_trait]
impl Tool for ReadFileTool {
fn name(&self) -> &str {
"read_file"
}
fn egress(&self, _args: &serde_json::Value) -> adk_core::egress::ToolEgress {
adk_core::egress::ToolEgress::None
}
fn description(&self) -> &str {
"Read a file from the workspace. Text comes back with line numbers, with optional \
`offset` (1-based start line) and `limit` (number of lines). A PNG, JPEG, GIF, WebP \
or PDF comes back as an attached image or document you can look at, with its type \
and size."
}
fn is_read_only(&self) -> bool {
true
}
fn is_concurrency_safe(&self) -> bool {
true
}
fn parameters_schema(&self) -> Option<Value> {
Some(json!({
"type": "object",
"properties": {
"path": { "type": "string", "description": "Path relative to the workspace root." },
"offset": { "type": "integer", "description": "1-based first line to return (optional, text only)." },
"limit": { "type": "integer", "description": "Maximum number of lines to return (optional, text only)." }
},
"required": ["path"]
}))
}
async fn execute(&self, ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
let path = require_str(&args, "path")?;
let (resolved, target) = self.workspace.resolve_target(&path)?;
if self.workspace.is_excluded(&resolved) || self.workspace.is_excluded(&target) {
return Err(DevToolError::Excluded(self.workspace.display(&resolved)).into());
}
let bytes = self.workspace.read_contained_bytes(&target).await?;
self.workspace
.record_digest(ctx.as_ref(), &resolved, &crate::read_ledger::sha256_hex(&bytes))
.await;
let display = self.workspace.display(&resolved);
if let Some(mime_type) = sniff_inline_mime(&bytes) {
let size = bytes.len();
let attached = self.inline_data && size <= self.max_inline_bytes;
let kind = if mime_type == "application/pdf" { "PDF document" } else { "image" };
let content = if attached {
format!("[{kind} {mime_type}, {size} bytes, attached as inline data]")
} else if self.inline_data {
format!(
"[{kind} {mime_type}, {size} bytes; over the {} byte inline cap, so the \
bytes are not attached]",
self.max_inline_bytes
)
} else {
format!("[{kind} {mime_type}, {size} bytes; this model receives no binary data]")
};
let response = json!({
"path": display,
"mime_type": mime_type,
"bytes": size,
"content": content,
"inline": attached,
});
return Ok(if attached {
json!({
"response": response,
"inline_data": [{ "mime_type": mime_type, "data": bytes }],
})
} else {
response
});
}
let contents = String::from_utf8(bytes).map_err(|_| {
DevToolError::Other(format!(
"'{display}' is neither UTF-8 text nor a PNG, JPEG, GIF, WebP or PDF; read_file \
returns text and those image and document types only"
))
})?;
let offset = args.get("offset").and_then(Value::as_u64).unwrap_or(1).max(1) as usize;
let limit = args.get("limit").and_then(Value::as_u64).map(|v| v as usize);
let mut lines = String::new();
let mut total = 0usize;
let mut shown = 0usize;
for (idx, line) in contents.lines().enumerate() {
total += 1;
let line_no = idx + 1;
if line_no < offset {
continue;
}
if let Some(limit) = limit
&& shown >= limit
{
break;
}
lines.push_str(&format!("{line_no:>6}\t{line}\n"));
shown += 1;
}
Ok(json!({
"path": display,
"content": lines,
"total_lines": total,
"returned_lines": shown,
}))
}
}
pub(crate) fn require_str(args: &Value, key: &str) -> Result<String> {
args.get(key).and_then(Value::as_str).map(str::to_string).ok_or_else(|| {
adk_core::AdkError::tool(format!("missing required string argument '{key}'"))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sniffing_reads_magic_bytes_only() {
assert_eq!(sniff_inline_mime(b"\xFF\xD8\xFF\xE0JFIF"), Some("image/jpeg"));
assert_eq!(sniff_inline_mime(b"GIF89a\x01\x00"), Some("image/gif"));
assert_eq!(sniff_inline_mime(b"RIFF\x00\x00\x00\x00WEBPVP8 "), Some("image/webp"));
assert_eq!(sniff_inline_mime(b"RIFF\x00\x00\x00\x00WAVEfmt "), None);
assert_eq!(sniff_inline_mime(b"%PDF-1.4"), Some("application/pdf"));
assert_eq!(sniff_inline_mime(b"PNG but not really"), None);
assert_eq!(sniff_inline_mime(b""), None);
}
}