1 Commits
Author SHA1 Message Date
Yeachan-Heo 4fb8a319ed feat(api): enable Anthropic Messages streaming in Rust
Add a standalone api crate for Anthropic's Messages API with typed requests,
responses, and SSE event parsing. The crate can send standard message
requests or consume streaming responses incrementally, and the workspace
now passes the stricter clippy invocation required by this lane.

Constraint: Must fit the existing Rust workspace and pass cargo fmt/clippy/test
Constraint: Live Anthropic verification depends on a valid ANTHROPIC_API_KEY at runtime
Rejected: Add a Stream-trait wrapper with extra dependencies | custom next_event() keeps the crate smaller
Rejected: Skip workspace lint cleanup | user explicitly required cargo clippy to pass
Confidence: medium
Scope-risk: moderate
Directive: Extend StreamEvent deliberately if future lanes need tool-use or thinking deltas
Tested: cargo fmt
Tested: cargo clippy --workspace --all-targets -- -D warnings
Tested: cargo test --workspace
Not-tested: Live Anthropic stream smoke test with a valid API key (current ANTHROPIC_API_KEY returned 401 authentication_error)
2026-03-31 15:45:30 +00:00
10 changed files with 2008 additions and 1191 deletions

No files matched your search

+1097 -196
View File
File diff suppressed because it is too large. Load diff
+15
View File
@@ -0,0 +1,15 @@
[package]
name = "api"
version.workspace = true
edition.workspace = true
license.workspace = true
publish.workspace = true
[dependencies]
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["io-util", "macros", "net", "rt-multi-thread", "time"] }
[lints]
workspace = true
+202
View File
@@ -0,0 +1,202 @@
use crate::error::ApiError;
use crate::sse::SseParser;
use crate::types::{MessageRequest, MessageResponse, StreamEvent};
const DEFAULT_BASE_URL: &str = "https://api.anthropic.com";
const ANTHROPIC_VERSION: &str = "2023-06-01";
#[derive(Debug, Clone)]
pub struct AnthropicClient {
http: reqwest::Client,
api_key: String,
auth_token: Option<String>,
base_url: String,
}
impl AnthropicClient {
#[must_use]
pub fn new(api_key: impl Into<String>) -> Self {
Self {
http: reqwest::Client::new(),
api_key: api_key.into(),
auth_token: None,
base_url: DEFAULT_BASE_URL.to_string(),
}
}
pub fn from_env() -> Result<Self, ApiError> {
Ok(Self::new(read_api_key(|key| std::env::var(key))?)
.with_auth_token(std::env::var("ANTHROPIC_AUTH_TOKEN").ok())
.with_base_url(
std::env::var("ANTHROPIC_BASE_URL")
.ok()
.or_else(|| std::env::var("CLAUDE_CODE_API_BASE_URL").ok())
.unwrap_or_else(|| DEFAULT_BASE_URL.to_string()),
))
}
#[must_use]
pub fn with_auth_token(mut self, auth_token: Option<String>) -> Self {
self.auth_token = auth_token.filter(|token| !token.is_empty());
self
}
#[must_use]
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
pub async fn send_message(
&self,
request: &MessageRequest,
) -> Result<MessageResponse, ApiError> {
let request = MessageRequest {
stream: false,
..request.clone()
};
let response = self.send_raw_request(&request).await?;
let response = expect_success(response).await?;
response
.json::<MessageResponse>()
.await
.map_err(ApiError::from)
}
pub async fn stream_message(
&self,
request: &MessageRequest,
) -> Result<MessageStream, ApiError> {
let response = self
.send_raw_request(&request.clone().with_streaming())
.await?;
let response = expect_success(response).await?;
Ok(MessageStream {
response,
parser: SseParser::new(),
pending: std::collections::VecDeque::new(),
done: false,
})
}
async fn send_raw_request(
&self,
request: &MessageRequest,
) -> Result<reqwest::Response, ApiError> {
let mut request_builder = self
.http
.post(format!(
"{}/v1/messages",
self.base_url.trim_end_matches('/')
))
.header("x-api-key", &self.api_key)
.header("anthropic-version", ANTHROPIC_VERSION)
.header("content-type", "application/json");
if let Some(auth_token) = &self.auth_token {
request_builder = request_builder.bearer_auth(auth_token);
}
request_builder
.json(request)
.send()
.await
.map_err(ApiError::from)
}
}
fn read_api_key(
getter: impl FnOnce(&str) -> Result<String, std::env::VarError>,
) -> Result<String, ApiError> {
match getter("ANTHROPIC_API_KEY") {
Ok(api_key) if api_key.is_empty() => Err(ApiError::MissingApiKey),
Ok(api_key) => Ok(api_key),
Err(std::env::VarError::NotPresent) => Err(ApiError::MissingApiKey),
Err(error) => Err(ApiError::from(error)),
}
}
#[derive(Debug)]
pub struct MessageStream {
response: reqwest::Response,
parser: SseParser,
pending: std::collections::VecDeque<StreamEvent>,
done: bool,
}
impl MessageStream {
pub async fn next_event(&mut self) -> Result<Option<StreamEvent>, ApiError> {
loop {
if let Some(event) = self.pending.pop_front() {
return Ok(Some(event));
}
if self.done {
let remaining = self.parser.finish()?;
self.pending.extend(remaining);
if let Some(event) = self.pending.pop_front() {
return Ok(Some(event));
}
return Ok(None);
}
match self.response.chunk().await? {
Some(chunk) => {
self.pending.extend(self.parser.push(&chunk)?);
}
None => {
self.done = true;
}
}
}
}
}
async fn expect_success(response: reqwest::Response) -> Result<reqwest::Response, ApiError> {
let status = response.status();
if status.is_success() {
return Ok(response);
}
let body = response.text().await.unwrap_or_else(|_| String::new());
Err(ApiError::UnexpectedStatus { status, body })
}
#[cfg(test)]
mod tests {
use std::env::VarError;
use crate::types::MessageRequest;
#[test]
fn read_api_key_requires_presence() {
let error = super::read_api_key(|_| Err(VarError::NotPresent))
.expect_err("missing key should error");
assert!(matches!(error, crate::error::ApiError::MissingApiKey));
}
#[test]
fn read_api_key_requires_non_empty_value() {
let error = super::read_api_key(|_| Ok(String::new())).expect_err("empty key should error");
assert!(matches!(error, crate::error::ApiError::MissingApiKey));
}
#[test]
fn with_auth_token_drops_empty_values() {
let client = super::AnthropicClient::new("test-key").with_auth_token(Some(String::new()));
assert!(client.auth_token.is_none());
}
#[test]
fn message_request_stream_helper_sets_stream_true() {
let request = MessageRequest {
model: "claude-3-7-sonnet-latest".to_string(),
max_tokens: 64,
messages: vec![],
system: None,
stream: false,
};
assert!(request.with_streaming().stream);
}
}
+65
View File
@@ -0,0 +1,65 @@
use std::env::VarError;
use std::fmt::{Display, Formatter};
#[derive(Debug)]
pub enum ApiError {
MissingApiKey,
InvalidApiKeyEnv(VarError),
Http(reqwest::Error),
Io(std::io::Error),
Json(serde_json::Error),
UnexpectedStatus {
status: reqwest::StatusCode,
body: String,
},
InvalidSseFrame(&'static str),
}
impl Display for ApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingApiKey => {
write!(
f,
"ANTHROPIC_API_KEY is not set; export it before calling the Anthropic API"
)
}
Self::InvalidApiKeyEnv(error) => {
write!(f, "failed to read ANTHROPIC_API_KEY: {error}")
}
Self::Http(error) => write!(f, "http error: {error}"),
Self::Io(error) => write!(f, "io error: {error}"),
Self::Json(error) => write!(f, "json error: {error}"),
Self::UnexpectedStatus { status, body } => {
write!(f, "anthropic api returned {status}: {body}")
}
Self::InvalidSseFrame(message) => write!(f, "invalid sse frame: {message}"),
}
}
}
impl std::error::Error for ApiError {}
impl From<reqwest::Error> for ApiError {
fn from(value: reqwest::Error) -> Self {
Self::Http(value)
}
}
impl From<std::io::Error> for ApiError {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
impl From<serde_json::Error> for ApiError {
fn from(value: serde_json::Error) -> Self {
Self::Json(value)
}
}
impl From<VarError> for ApiError {
fn from(value: VarError) -> Self {
Self::InvalidApiKeyEnv(value)
}
}
+13
View File
@@ -0,0 +1,13 @@
mod client;
mod error;
mod sse;
mod types;
pub use client::{AnthropicClient, MessageStream};
pub use error::ApiError;
pub use sse::{parse_frame, SseParser};
pub use types::{
ContentBlockDelta, ContentBlockDeltaEvent, ContentBlockStartEvent, ContentBlockStopEvent,
InputContentBlock, InputMessage, MessageRequest, MessageResponse, MessageStartEvent,
MessageStopEvent, OutputContentBlock, StreamEvent, Usage,
};
+203
View File
@@ -0,0 +1,203 @@
use crate::error::ApiError;
use crate::types::StreamEvent;
#[derive(Debug, Default)]
pub struct SseParser {
buffer: Vec<u8>,
}
impl SseParser {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, chunk: &[u8]) -> Result<Vec<StreamEvent>, ApiError> {
self.buffer.extend_from_slice(chunk);
let mut events = Vec::new();
while let Some(frame) = self.next_frame() {
if let Some(event) = parse_frame(&frame)? {
events.push(event);
}
}
Ok(events)
}
pub fn finish(&mut self) -> Result<Vec<StreamEvent>, ApiError> {
if self.buffer.is_empty() {
return Ok(Vec::new());
}
let trailing = std::mem::take(&mut self.buffer);
match parse_frame(&String::from_utf8_lossy(&trailing))? {
Some(event) => Ok(vec![event]),
None => Ok(Vec::new()),
}
}
fn next_frame(&mut self) -> Option<String> {
let separator = self
.buffer
.windows(2)
.position(|window| window == b"\n\n")
.map(|position| (position, 2))
.or_else(|| {
self.buffer
.windows(4)
.position(|window| window == b"\r\n\r\n")
.map(|position| (position, 4))
})?;
let (position, separator_len) = separator;
let frame = self
.buffer
.drain(..position + separator_len)
.collect::<Vec<_>>();
let frame_len = frame.len().saturating_sub(separator_len);
Some(String::from_utf8_lossy(&frame[..frame_len]).into_owned())
}
}
pub fn parse_frame(frame: &str) -> Result<Option<StreamEvent>, ApiError> {
let trimmed = frame.trim();
if trimmed.is_empty() {
return Ok(None);
}
let mut data_lines = Vec::new();
let mut event_name: Option<&str> = None;
for line in trimmed.lines() {
if line.starts_with(':') {
continue;
}
if let Some(name) = line.strip_prefix("event:") {
event_name = Some(name.trim());
continue;
}
if let Some(data) = line.strip_prefix("data:") {
data_lines.push(data.trim_start());
}
}
if matches!(event_name, Some("ping")) {
return Ok(None);
}
if data_lines.is_empty() {
return Ok(None);
}
let payload = data_lines.join("\n");
if payload == "[DONE]" {
return Ok(None);
}
serde_json::from_str::<StreamEvent>(&payload)
.map(Some)
.map_err(ApiError::from)
}
#[cfg(test)]
mod tests {
use super::{parse_frame, SseParser};
use crate::types::{ContentBlockDelta, OutputContentBlock, StreamEvent};
#[test]
fn parses_single_frame() {
let frame = concat!(
"event: content_block_start\n",
"data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"Hi\"}}\n\n"
);
let event = parse_frame(frame).expect("frame should parse");
assert_eq!(
event,
Some(StreamEvent::ContentBlockStart(
crate::types::ContentBlockStartEvent {
index: 0,
content_block: OutputContentBlock::Text {
text: "Hi".to_string(),
},
},
))
);
}
#[test]
fn parses_chunked_stream() {
let mut parser = SseParser::new();
let first = b"event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hel";
let second = b"lo\"}}\n\n";
assert!(parser
.push(first)
.expect("first chunk should buffer")
.is_empty());
let events = parser.push(second).expect("second chunk should parse");
assert_eq!(
events,
vec![StreamEvent::ContentBlockDelta(
crate::types::ContentBlockDeltaEvent {
index: 0,
delta: ContentBlockDelta::TextDelta {
text: "Hello".to_string(),
},
}
)]
);
}
#[test]
fn ignores_ping_and_done() {
let mut parser = SseParser::new();
let payload = concat!(
": keepalive\n",
"event: ping\n",
"data: {\"type\":\"ping\"}\n\n",
"event: message_stop\n",
"data: {\"type\":\"message_stop\"}\n\n",
"data: [DONE]\n\n"
);
let events = parser
.push(payload.as_bytes())
.expect("parser should succeed");
assert_eq!(
events,
vec![StreamEvent::MessageStop(crate::types::MessageStopEvent {})]
);
}
#[test]
fn ignores_data_less_event_frames() {
let frame = "event: ping\n\n";
let event = parse_frame(frame).expect("frame without data should be ignored");
assert_eq!(event, None);
}
#[test]
fn parses_split_json_across_data_lines() {
let frame = concat!(
"event: content_block_delta\n",
"data: {\"type\":\"content_block_delta\",\"index\":0,\n",
"data: \"delta\":{\"type\":\"text_delta\",\"text\":\"Hello\"}}\n\n"
);
let event = parse_frame(frame).expect("frame should parse");
assert_eq!(
event,
Some(StreamEvent::ContentBlockDelta(
crate::types::ContentBlockDeltaEvent {
index: 0,
delta: ContentBlockDelta::TextDelta {
text: "Hello".to_string(),
},
}
))
);
}
}
+110
View File
@@ -0,0 +1,110 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageRequest {
pub model: String,
pub max_tokens: u32,
pub messages: Vec<InputMessage>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub stream: bool,
}
impl MessageRequest {
#[must_use]
pub fn with_streaming(mut self) -> Self {
self.stream = true;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputMessage {
pub role: String,
pub content: Vec<InputContentBlock>,
}
impl InputMessage {
#[must_use]
pub fn user_text(text: impl Into<String>) -> Self {
Self {
role: "user".to_string(),
content: vec![InputContentBlock::Text { text: text.into() }],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum InputContentBlock {
Text { text: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageResponse {
pub id: String,
#[serde(rename = "type")]
pub kind: String,
pub role: String,
pub content: Vec<OutputContentBlock>,
pub model: String,
#[serde(default)]
pub stop_reason: Option<String>,
#[serde(default)]
pub stop_sequence: Option<String>,
pub usage: Usage,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputContentBlock {
Text { text: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Usage {
pub input_tokens: u32,
pub output_tokens: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageStartEvent {
pub message: MessageResponse,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContentBlockStartEvent {
pub index: u32,
pub content_block: OutputContentBlock,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContentBlockDeltaEvent {
pub index: u32,
pub delta: ContentBlockDelta,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentBlockDelta {
TextDelta { text: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContentBlockStopEvent {
pub index: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageStopEvent {}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
MessageStart(MessageStartEvent),
ContentBlockStart(ContentBlockStartEvent),
ContentBlockDelta(ContentBlockDeltaEvent),
ContentBlockStop(ContentBlockStopEvent),
MessageStop(MessageStopEvent),
}
+303
View File
@@ -0,0 +1,303 @@
use std::collections::HashMap;
use std::sync::Arc;
use api::{AnthropicClient, InputMessage, MessageRequest, OutputContentBlock, StreamEvent};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::Mutex;
#[tokio::test]
async fn send_message_posts_json_and_parses_response() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
let body = concat!(
"{",
"\"id\":\"msg_test\",",
"\"type\":\"message\",",
"\"role\":\"assistant\",",
"\"content\":[{\"type\":\"text\",\"text\":\"Hello from Claude\"}],",
"\"model\":\"claude-3-7-sonnet-latest\",",
"\"stop_reason\":\"end_turn\",",
"\"stop_sequence\":null,",
"\"usage\":{\"input_tokens\":12,\"output_tokens\":4}",
"}"
);
let server = spawn_server(state.clone(), http_response("application/json", body)).await;
let client = AnthropicClient::new("test-key")
.with_auth_token(Some("proxy-token".to_string()))
.with_base_url(server.base_url());
let response = client
.send_message(&sample_request(false))
.await
.expect("request should succeed");
assert_eq!(response.id, "msg_test");
assert_eq!(
response.content,
vec![OutputContentBlock::Text {
text: "Hello from Claude".to_string(),
}]
);
let captured = state.lock().await;
let request = captured.first().expect("server should capture request");
assert_eq!(request.method, "POST");
assert_eq!(request.path, "/v1/messages");
assert_eq!(
request.headers.get("x-api-key").map(String::as_str),
Some("test-key")
);
assert_eq!(
request.headers.get("authorization").map(String::as_str),
Some("Bearer proxy-token")
);
assert_eq!(
request.headers.get("anthropic-version").map(String::as_str),
Some("2023-06-01")
);
let body: serde_json::Value =
serde_json::from_str(&request.body).expect("request body should be json");
assert_eq!(
body.get("model").and_then(serde_json::Value::as_str),
Some("claude-3-7-sonnet-latest")
);
assert!(
body.get("stream").is_none(),
"non-stream request should omit stream=false"
);
}
#[tokio::test]
async fn stream_message_parses_sse_events() {
let state = Arc::new(Mutex::new(Vec::<CapturedRequest>::new()));
let sse = concat!(
"event: message_start\n",
"data: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_stream\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"claude-3-7-sonnet-latest\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":8,\"output_tokens\":0}}}\n\n",
"event: content_block_start\n",
"data: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\n",
"event: content_block_delta\n",
"data: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hello\"}}\n\n",
"event: content_block_stop\n",
"data: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
"event: message_stop\n",
"data: {\"type\":\"message_stop\"}\n\n",
"data: [DONE]\n\n"
);
let server = spawn_server(state.clone(), http_response("text/event-stream", sse)).await;
let client = AnthropicClient::new("test-key")
.with_auth_token(Some("proxy-token".to_string()))
.with_base_url(server.base_url());
let mut stream = client
.stream_message(&sample_request(false))
.await
.expect("stream should start");
let mut events = Vec::new();
while let Some(event) = stream
.next_event()
.await
.expect("stream event should parse")
{
events.push(event);
}
assert_eq!(events.len(), 5);
assert!(matches!(events[0], StreamEvent::MessageStart(_)));
assert!(matches!(events[1], StreamEvent::ContentBlockStart(_)));
assert!(matches!(events[2], StreamEvent::ContentBlockDelta(_)));
assert!(matches!(events[3], StreamEvent::ContentBlockStop(_)));
assert!(matches!(events[4], StreamEvent::MessageStop(_)));
let captured = state.lock().await;
let request = captured.first().expect("server should capture request");
assert!(request.body.contains("\"stream\":true"));
}
#[tokio::test]
#[ignore = "requires ANTHROPIC_API_KEY and network access"]
async fn live_stream_smoke_test() {
let client = AnthropicClient::from_env().expect("ANTHROPIC_API_KEY must be set");
let mut stream = client
.stream_message(&MessageRequest {
model: std::env::var("ANTHROPIC_MODEL")
.unwrap_or_else(|_| "claude-3-7-sonnet-latest".to_string()),
max_tokens: 32,
messages: vec![InputMessage::user_text(
"Reply with exactly: hello from rust",
)],
system: None,
stream: false,
})
.await
.expect("live stream should start");
let mut saw_start = false;
let mut saw_follow_up = false;
let mut event_kinds = Vec::new();
while let Some(event) = stream
.next_event()
.await
.expect("live stream should yield events")
{
match event {
StreamEvent::MessageStart(_) => {
saw_start = true;
event_kinds.push("message_start");
}
StreamEvent::ContentBlockStart(_) => {
saw_follow_up = true;
event_kinds.push("content_block_start");
}
StreamEvent::ContentBlockDelta(_) => {
saw_follow_up = true;
event_kinds.push("content_block_delta");
}
StreamEvent::ContentBlockStop(_) => {
saw_follow_up = true;
event_kinds.push("content_block_stop");
}
StreamEvent::MessageStop(_) => {
saw_follow_up = true;
event_kinds.push("message_stop");
}
}
}
assert!(
saw_start,
"expected a message_start event; got {event_kinds:?}"
);
assert!(
saw_follow_up,
"expected at least one follow-up stream event; got {event_kinds:?}"
);
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct CapturedRequest {
method: String,
path: String,
headers: HashMap<String, String>,
body: String,
}
struct TestServer {
base_url: String,
join_handle: tokio::task::JoinHandle<()>,
}
impl TestServer {
fn base_url(&self) -> String {
self.base_url.clone()
}
}
impl Drop for TestServer {
fn drop(&mut self) {
self.join_handle.abort();
}
}
async fn spawn_server(state: Arc<Mutex<Vec<CapturedRequest>>>, response: String) -> TestServer {
let listener = TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener
.local_addr()
.expect("listener should have local addr");
let join_handle = tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.expect("server should accept");
let mut buffer = Vec::new();
let mut header_end = None;
loop {
let mut chunk = [0_u8; 1024];
let read = socket
.read(&mut chunk)
.await
.expect("request read should succeed");
if read == 0 {
break;
}
buffer.extend_from_slice(&chunk[..read]);
if let Some(position) = find_header_end(&buffer) {
header_end = Some(position);
break;
}
}
let header_end = header_end.expect("request should include headers");
let (header_bytes, remaining) = buffer.split_at(header_end);
let header_text = String::from_utf8(header_bytes.to_vec()).expect("headers should be utf8");
let mut lines = header_text.split("\r\n");
let request_line = lines.next().expect("request line should exist");
let mut parts = request_line.split_whitespace();
let method = parts.next().expect("method should exist").to_string();
let path = parts.next().expect("path should exist").to_string();
let mut headers = HashMap::new();
let mut content_length = 0_usize;
for line in lines {
if line.is_empty() {
continue;
}
let (name, value) = line.split_once(':').expect("header should have colon");
let value = value.trim().to_string();
if name.eq_ignore_ascii_case("content-length") {
content_length = value.parse().expect("content length should parse");
}
headers.insert(name.to_ascii_lowercase(), value);
}
let mut body = remaining[4..].to_vec();
while body.len() < content_length {
let mut chunk = vec![0_u8; content_length - body.len()];
let read = socket
.read(&mut chunk)
.await
.expect("body read should succeed");
if read == 0 {
break;
}
body.extend_from_slice(&chunk[..read]);
}
state.lock().await.push(CapturedRequest {
method,
path,
headers,
body: String::from_utf8(body).expect("body should be utf8"),
});
socket
.write_all(response.as_bytes())
.await
.expect("response write should succeed");
});
TestServer {
base_url: format!("http://{address}"),
join_handle,
}
}
fn find_header_end(bytes: &[u8]) -> Option<usize> {
bytes.windows(4).position(|window| window == b"\r\n\r\n")
}
fn http_response(content_type: &str, body: &str) -> String {
format!(
"HTTP/1.1 200 OK\r\ncontent-type: {content_type}\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}",
body.len()
)
}
fn sample_request(stream: bool) -> MessageRequest {
MessageRequest {
model: "claude-3-7-sonnet-latest".to_string(),
max_tokens: 64,
messages: vec![InputMessage::user_text("Say hello")],
system: None,
stream,
}
}
-8
View File
@@ -5,13 +5,5 @@ edition.workspace = true
license.workspace = true
publish.workspace = true
[dependencies]
regex = "1.12"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
[dev-dependencies]
tempfile = "3.20"
[lints]
workspace = true
-987
View File
@@ -1,14 +1,3 @@
use regex::RegexBuilder;
use serde::Serialize;
use serde_json::{json, Value};
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::fmt;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolManifestEntry {
pub name: String,
@@ -37,979 +26,3 @@ impl ToolRegistry {
&self.entries
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct TextContent {
#[serde(rename = "type")]
pub kind: &'static str,
pub text: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ToolResult {
pub content: Vec<TextContent>,
}
impl ToolResult {
#[must_use]
pub fn text(text: impl Into<String>) -> Self {
Self {
content: vec![TextContent {
kind: "text",
text: text.into(),
}],
}
}
}
#[derive(Debug)]
pub struct ToolError {
message: Cow<'static, str>,
}
impl ToolError {
#[must_use]
pub fn new(message: impl Into<Cow<'static, str>>) -> Self {
Self {
message: message.into(),
}
}
}
impl fmt::Display for ToolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for ToolError {}
impl From<io::Error> for ToolError {
fn from(value: io::Error) -> Self {
Self::new(value.to_string())
}
}
impl From<regex::Error> for ToolError {
fn from(value: regex::Error) -> Self {
Self::new(value.to_string())
}
}
pub trait Tool {
fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
fn input_schema(&self) -> Value;
fn execute(&self, input: Value) -> Result<ToolResult, ToolError>;
}
fn schema_string(description: &str) -> Value {
json!({ "type": "string", "description": description })
}
fn schema_number(description: &str) -> Value {
json!({ "type": "number", "description": description })
}
fn schema_boolean(description: &str) -> Value {
json!({ "type": "boolean", "description": description })
}
fn strict_object(properties: &Value, required: &[&str]) -> Value {
json!({
"type": "object",
"properties": properties,
"required": required,
"additionalProperties": false,
})
}
fn parse_string(input: &Value, key: &'static str) -> Result<String, ToolError> {
input
.get(key)
.and_then(Value::as_str)
.map(ToOwned::to_owned)
.ok_or_else(|| ToolError::new(format!("missing or invalid string field: {key}")))
}
fn optional_string(input: &Value, key: &'static str) -> Result<Option<String>, ToolError> {
match input.get(key) {
None | Some(Value::Null) => Ok(None),
Some(Value::String(value)) => Ok(Some(value.clone())),
Some(_) => Err(ToolError::new(format!("invalid string field: {key}"))),
}
}
fn optional_u64(input: &Value, key: &'static str) -> Result<Option<u64>, ToolError> {
match input.get(key) {
None | Some(Value::Null) => Ok(None),
Some(value) => value
.as_u64()
.ok_or_else(|| ToolError::new(format!("invalid numeric field: {key}")))
.map(Some),
}
}
fn optional_bool(input: &Value, key: &'static str) -> Result<Option<bool>, ToolError> {
match input.get(key) {
None | Some(Value::Null) => Ok(None),
Some(value) => value
.as_bool()
.ok_or_else(|| ToolError::new(format!("invalid boolean field: {key}")))
.map(Some),
}
}
fn absolute_path(path: &str) -> Result<PathBuf, ToolError> {
let expanded = if let Some(rest) = path.strip_prefix("~/") {
std::env::var_os("HOME")
.map(PathBuf::from)
.map_or_else(|| PathBuf::from(path), |home| home.join(rest))
} else {
PathBuf::from(path)
};
if expanded.is_absolute() {
Ok(expanded)
} else {
Err(ToolError::new(format!("path must be absolute: {path}")))
}
}
fn relative_display(path: &Path, base: &Path) -> String {
path.strip_prefix(base).ok().map_or_else(
|| path.to_string_lossy().replace('\\', "/"),
|value| value.to_string_lossy().replace('\\', "/"),
)
}
fn line_slice(content: &str, offset: Option<u64>, limit: Option<u64>) -> String {
let start = usize_from_u64(offset.unwrap_or(1).saturating_sub(1));
let lines: Vec<&str> = content.lines().collect();
let end = limit
.map_or(lines.len(), |limit| {
start.saturating_add(usize_from_u64(limit))
})
.min(lines.len());
if start >= lines.len() {
return String::new();
}
lines[start..end]
.iter()
.enumerate()
.map(|(index, line)| format!("{:>6}\t{line}", start + index + 1))
.collect::<Vec<_>>()
.join("\n")
}
fn parse_page_range(pages: &str) -> Result<(u64, u64), ToolError> {
if let Some((start, end)) = pages.split_once('-') {
let start = start
.trim()
.parse::<u64>()
.map_err(|_| ToolError::new("invalid pages parameter"))?;
let end = end
.trim()
.parse::<u64>()
.map_err(|_| ToolError::new("invalid pages parameter"))?;
if start == 0 || end < start {
return Err(ToolError::new("invalid pages parameter"));
}
Ok((start, end))
} else {
let page = pages
.trim()
.parse::<u64>()
.map_err(|_| ToolError::new("invalid pages parameter"))?;
if page == 0 {
return Err(ToolError::new("invalid pages parameter"));
}
Ok((page, page))
}
}
fn apply_single_edit(
original: &str,
old_string: &str,
new_string: &str,
replace_all: bool,
) -> Result<String, ToolError> {
if old_string == new_string {
return Err(ToolError::new(
"No changes to make: old_string and new_string are exactly the same.",
));
}
if old_string.is_empty() {
if original.is_empty() {
return Ok(new_string.to_owned());
}
return Err(ToolError::new(
"Cannot create new file - file already exists.",
));
}
let matches = original.matches(old_string).count();
if matches == 0 {
return Err(ToolError::new(format!(
"String to replace not found in file.\nString: {old_string}"
)));
}
if matches > 1 && !replace_all {
return Err(ToolError::new(format!(
"Found {matches} matches of the string to replace, but replace_all is false. To replace all occurrences, set replace_all to true. To replace only one occurrence, please provide more context to uniquely identify the instance.\nString: {old_string}"
)));
}
let updated = if replace_all {
original.replace(old_string, new_string)
} else {
original.replacen(old_string, new_string, 1)
};
Ok(updated)
}
fn diff_hunks(_before: &str, _after: &str) -> Value {
json!([])
}
fn usize_from_u64(value: u64) -> usize {
usize::try_from(value).unwrap_or(usize::MAX)
}
pub struct BashTool;
pub struct ReadTool;
pub struct WriteTool;
pub struct EditTool;
pub struct GlobTool;
pub struct GrepTool;
impl Tool for BashTool {
fn name(&self) -> &'static str {
"Bash"
}
fn description(&self) -> &'static str {
"Execute a shell command in the current environment."
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"command": schema_string("The command to execute"),
"timeout": schema_number("Optional timeout in milliseconds (max 600000)"),
"description": schema_string("Clear, concise description of what this command does in active voice. Never use words like \"complex\" or \"risk\" in the description - just describe what it does."),
"run_in_background": schema_boolean("Set to true to run this command in the background. Use Read to read the output later."),
"dangerouslyDisableSandbox": schema_boolean("Set this to true to dangerously override sandbox mode and run commands without sandboxing.")
}),
&["command"],
)
}
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let command = parse_string(&input, "command")?;
let _timeout = optional_u64(&input, "timeout")?;
let _description = optional_string(&input, "description")?;
let run_in_background = optional_bool(&input, "run_in_background")?.unwrap_or(false);
let _disable_sandbox = optional_bool(&input, "dangerouslyDisableSandbox")?.unwrap_or(false);
if run_in_background {
return Ok(ToolResult::text(
"Background execution is not supported in this runtime.",
));
}
let output = Command::new("bash").arg("-lc").arg(&command).output()?;
let mut rendered = String::new();
if !output.stdout.is_empty() {
rendered.push_str(&String::from_utf8_lossy(&output.stdout));
}
if !output.stderr.is_empty() {
if !rendered.is_empty() && !rendered.ends_with('\n') {
rendered.push('\n');
}
rendered.push_str(&String::from_utf8_lossy(&output.stderr));
}
if rendered.is_empty() {
rendered = if output.status.success() {
"Done".to_owned()
} else {
format!("Command exited with status {}", output.status)
};
}
Ok(ToolResult::text(rendered.trim_end().to_owned()))
}
}
impl Tool for ReadTool {
fn name(&self) -> &'static str {
"Read"
}
fn description(&self) -> &'static str {
"Read a file from the local filesystem."
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"file_path": schema_string("The absolute path to the file to read"),
"offset": json!({"type":"number","description":"The line number to start reading from. Only provide if the file is too large to read at once","minimum":0}),
"limit": json!({"type":"number","description":"The number of lines to read. Only provide if the file is too large to read at once.","exclusiveMinimum":0}),
"pages": schema_string("Page range for PDF files (e.g., \"1-5\", \"3\", \"10-20\"). Only applicable to PDF files. Maximum 20 pages per request.")
}),
&["file_path"],
)
}
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let file_path = parse_string(&input, "file_path")?;
let path = absolute_path(&file_path)?;
let offset = optional_u64(&input, "offset")?;
let limit = optional_u64(&input, "limit")?;
let pages = optional_string(&input, "pages")?;
let content = fs::read_to_string(&path)?;
if path.extension().and_then(|ext| ext.to_str()) == Some("pdf") {
if let Some(pages) = pages {
let (start, end) = parse_page_range(&pages)?;
return Ok(ToolResult::text(format!(
"PDF page extraction is not implemented in Rust yet for {}. Requested pages {}-{}.",
path.display(), start, end
)));
}
}
let rendered = if offset.is_some() || limit.is_some() {
line_slice(&content, offset, limit)
} else {
line_slice(&content, Some(1), None)
};
Ok(ToolResult::text(rendered))
}
}
impl Tool for WriteTool {
fn name(&self) -> &'static str {
"Write"
}
fn description(&self) -> &'static str {
"Write a file to the local filesystem."
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"file_path": schema_string("The absolute path to the file to write (must be absolute, not relative)"),
"content": schema_string("The content to write to the file")
}),
&["file_path", "content"],
)
}
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let file_path = parse_string(&input, "file_path")?;
let content = parse_string(&input, "content")?;
let path = absolute_path(&file_path)?;
let existed = path.exists();
let original = if existed {
Some(fs::read_to_string(&path)?)
} else {
None
};
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&path, &content)?;
let payload = json!({
"type": if existed { "update" } else { "create" },
"filePath": file_path,
"content": content,
"structuredPatch": diff_hunks(original.as_deref().unwrap_or(""), &content),
"originalFile": original,
"gitDiff": Value::Null,
});
Ok(ToolResult::text(payload.to_string()))
}
}
impl Tool for EditTool {
fn name(&self) -> &'static str {
"Edit"
}
fn description(&self) -> &'static str {
"A tool for editing files"
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"file_path": schema_string("The absolute path to the file to modify"),
"old_string": schema_string("The text to replace"),
"new_string": schema_string("The text to replace it with (must be different from old_string)"),
"replace_all": json!({"type":"boolean","description":"Replace all occurrences of old_string (default false)","default":false})
}),
&["file_path", "old_string", "new_string"],
)
}
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let file_path = parse_string(&input, "file_path")?;
let old_string = parse_string(&input, "old_string")?;
let new_string = parse_string(&input, "new_string")?;
let replace_all = optional_bool(&input, "replace_all")?.unwrap_or(false);
let path = absolute_path(&file_path)?;
let original = if path.exists() {
fs::read_to_string(&path)?
} else {
String::new()
};
let updated = apply_single_edit(&original, &old_string, &new_string, replace_all)?;
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&path, &updated)?;
let payload = json!({
"filePath": file_path,
"oldString": old_string,
"newString": new_string,
"originalFile": original,
"structuredPatch": diff_hunks("", ""),
"userModified": false,
"replaceAll": replace_all,
"gitDiff": Value::Null,
});
Ok(ToolResult::text(payload.to_string()))
}
}
impl Tool for GlobTool {
fn name(&self) -> &'static str {
"Glob"
}
fn description(&self) -> &'static str {
"Fast file pattern matching tool"
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"pattern": schema_string("The glob pattern to match files against"),
"path": schema_string("The directory to search in. If not specified, the current working directory will be used. IMPORTANT: Omit this field to use the default directory. DO NOT enter \"undefined\" or \"null\" - simply omit it for the default behavior. Must be a valid directory path if provided.")
}),
&["pattern"],
)
}
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let pattern = parse_string(&input, "pattern")?;
let root = optional_string(&input, "path")?
.map(|path| absolute_path(&path))
.transpose()?
.unwrap_or(std::env::current_dir()?);
let start = std::time::Instant::now();
let mut filenames = Vec::new();
visit_files(&root, &mut |path| {
let relative = relative_display(path, &root);
if glob_matches(&pattern, &relative) {
filenames.push(relative);
}
})?;
filenames.sort();
let truncated = filenames.len() > 100;
if truncated {
filenames.truncate(100);
}
let payload = json!({
"durationMs": start.elapsed().as_millis(),
"numFiles": filenames.len(),
"filenames": filenames,
"truncated": truncated,
});
Ok(ToolResult::text(payload.to_string()))
}
}
impl Tool for GrepTool {
fn name(&self) -> &'static str {
"Grep"
}
fn description(&self) -> &'static str {
"Fast content search tool"
}
fn input_schema(&self) -> Value {
strict_object(
&json!({
"pattern": schema_string("The regular expression pattern to search for in file contents"),
"path": schema_string("File or directory to search in (rg PATH). Defaults to current working directory."),
"glob": schema_string("Glob pattern to filter files (e.g. \"*.js\", \"*.{ts,tsx}\") - maps to rg --glob"),
"output_mode": {"type":"string","enum":["content","files_with_matches","count"],"description":"Output mode: \"content\" shows matching lines (supports -A/-B/-C context, -n line numbers, head_limit), \"files_with_matches\" shows file paths (supports head_limit), \"count\" shows match counts (supports head_limit). Defaults to \"files_with_matches\"."},
"-B": schema_number("Number of lines to show before each match (rg -B). Requires output_mode: \"content\", ignored otherwise."),
"-A": schema_number("Number of lines to show after each match (rg -A). Requires output_mode: \"content\", ignored otherwise."),
"-C": schema_number("Alias for context."),
"context": schema_number("Number of lines to show before and after each match (rg -C). Requires output_mode: \"content\", ignored otherwise."),
"-n": {"type":"boolean","description":"Show line numbers in output (rg -n). Requires output_mode: \"content\", ignored otherwise. Defaults to true."},
"-i": schema_boolean("Case insensitive search (rg -i)"),
"type": schema_string("File type to search (rg --type). Common types: js, py, rust, go, java, etc. More efficient than include for standard file types."),
"head_limit": schema_number("Limit output to first N lines/entries, equivalent to \"| head -N\". Works across all output modes: content (limits output lines), files_with_matches (limits file paths), count (limits count entries). Defaults to 250 when unspecified. Pass 0 for unlimited (use sparingly — large result sets waste context)."),
"offset": schema_number("Skip first N lines/entries before applying head_limit, equivalent to \"| tail -n +N | head -N\". Works across all output modes. Defaults to 0."),
"multiline": schema_boolean("Enable multiline mode where . matches newlines and patterns can span lines (rg -U --multiline-dotall). Default: false.")
}),
&["pattern"],
)
}
#[allow(clippy::too_many_lines)]
fn execute(&self, input: Value) -> Result<ToolResult, ToolError> {
let pattern = parse_string(&input, "pattern")?;
let root = optional_string(&input, "path")?
.map(|path| absolute_path(&path))
.transpose()?
.unwrap_or(std::env::current_dir()?);
let glob = optional_string(&input, "glob")?;
let output_mode = optional_string(&input, "output_mode")?
.unwrap_or_else(|| "files_with_matches".to_owned());
let context_before = usize_from_u64(optional_u64(&input, "-B")?.unwrap_or(0));
let context_after = usize_from_u64(optional_u64(&input, "-A")?.unwrap_or(0));
let context_c = optional_u64(&input, "-C")?;
let context = optional_u64(&input, "context")?;
let show_line_numbers = optional_bool(&input, "-n")?.unwrap_or(true);
let case_insensitive = optional_bool(&input, "-i")?.unwrap_or(false);
let file_type = optional_string(&input, "type")?;
let head_limit = optional_u64(&input, "head_limit")?;
let offset = usize_from_u64(optional_u64(&input, "offset")?.unwrap_or(0));
let _multiline = optional_bool(&input, "multiline")?.unwrap_or(false);
let shared_context = usize_from_u64(context.or(context_c).unwrap_or(0));
let regex = RegexBuilder::new(&pattern)
.case_insensitive(case_insensitive)
.build()?;
let mut matched_lines = Vec::new();
let mut files_with_matches = Vec::new();
let mut count_lines = Vec::new();
let mut total_matches = 0usize;
let candidates = collect_files(&root)?;
for path in candidates {
let relative = relative_display(&path, &root);
if !matches_file_filter(&relative, glob.as_deref(), file_type.as_deref()) {
continue;
}
let Ok(file_content) = fs::read_to_string(&path) else {
continue;
};
let lines: Vec<&str> = file_content.lines().collect();
let mut matched_indexes = Vec::new();
let mut file_match_count = 0usize;
for (index, line) in lines.iter().enumerate() {
if regex.is_match(line) {
matched_indexes.push(index);
file_match_count += regex.find_iter(line).count().max(1);
}
}
if matched_indexes.is_empty() {
continue;
}
total_matches += file_match_count;
files_with_matches.push(relative.clone());
count_lines.push(format!("{relative}:{file_match_count}"));
if output_mode == "content" {
let mut included = BTreeSet::new();
for index in matched_indexes {
let before = if shared_context > 0 {
shared_context
} else {
context_before
};
let after = if shared_context > 0 {
shared_context
} else {
context_after
};
let start = index.saturating_sub(before);
let end = (index + after).min(lines.len().saturating_sub(1));
for line_index in start..=end {
included.insert(line_index);
}
}
for line_index in included {
if show_line_numbers {
matched_lines.push(format!(
"{relative}:{}:{}",
line_index + 1,
lines[line_index]
));
} else {
matched_lines.push(format!("{relative}:{}", lines[line_index]));
}
}
}
}
let rendered = match output_mode.as_str() {
"content" => {
let limited = apply_offset_limit(matched_lines, head_limit, offset);
json!({
"mode": "content",
"numFiles": 0,
"filenames": [],
"content": limited.join("\n"),
"numLines": limited.len(),
"appliedOffset": (offset > 0).then_some(offset),
})
}
"count" => {
let limited = apply_offset_limit(count_lines, head_limit, offset);
json!({
"mode": "count",
"numFiles": files_with_matches.len(),
"filenames": [],
"content": limited.join("\n"),
"numMatches": total_matches,
"appliedOffset": (offset > 0).then_some(offset),
})
}
_ => {
files_with_matches.sort();
let limited = apply_offset_limit(files_with_matches, head_limit, offset);
json!({
"mode": "files_with_matches",
"numFiles": limited.len(),
"filenames": limited,
"appliedOffset": (offset > 0).then_some(offset),
})
}
};
Ok(ToolResult::text(rendered.to_string()))
}
}
fn apply_offset_limit<T>(items: Vec<T>, limit: Option<u64>, offset: usize) -> Vec<T> {
let mut iter = items.into_iter().skip(offset);
match limit {
Some(0) | None => iter.collect(),
Some(limit) => iter.by_ref().take(usize_from_u64(limit)).collect(),
}
}
fn collect_files(root: &Path) -> Result<Vec<PathBuf>, ToolError> {
let mut files = Vec::new();
if root.is_file() {
files.push(root.to_path_buf());
return Ok(files);
}
visit_files(root, &mut |path| files.push(path.to_path_buf()))?;
Ok(files)
}
fn visit_files(root: &Path, visitor: &mut dyn FnMut(&Path)) -> Result<(), ToolError> {
if root.is_file() {
visitor(root);
return Ok(());
}
for entry in fs::read_dir(root)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_files(&path, visitor)?;
} else if path.is_file() {
visitor(&path);
}
}
Ok(())
}
fn matches_file_filter(relative: &str, glob: Option<&str>, file_type: Option<&str>) -> bool {
let glob_ok = glob.is_none_or(|pattern| {
split_glob_patterns(pattern)
.into_iter()
.any(|single| glob_matches(&single, relative))
});
let type_ok = file_type.is_none_or(|kind| path_matches_type(relative, kind));
glob_ok && type_ok
}
fn split_glob_patterns(patterns: &str) -> Vec<String> {
let mut result = Vec::new();
for raw in patterns.split_whitespace() {
if raw.contains('{') && raw.contains('}') {
result.push(raw.to_owned());
} else {
result.extend(
raw.split(',')
.filter(|part| !part.is_empty())
.map(ToOwned::to_owned),
);
}
}
result
}
fn path_matches_type(relative: &str, kind: &str) -> bool {
let extension = Path::new(relative)
.extension()
.and_then(|value| value.to_str())
.unwrap_or_default();
matches!(
(kind, extension),
("rust", "rs")
| ("js", "js")
| ("ts", "ts")
| ("tsx", "tsx")
| ("py", "py")
| ("go", "go")
| ("java", "java")
| ("json", "json")
| ("md", "md")
)
}
fn glob_matches(pattern: &str, path: &str) -> bool {
expand_braces(pattern)
.into_iter()
.any(|expanded| glob_match_one(&expanded, path))
}
fn expand_braces(pattern: &str) -> Vec<String> {
let Some(start) = pattern.find('{') else {
return vec![pattern.to_owned()];
};
let Some(end_rel) = pattern[start..].find('}') else {
return vec![pattern.to_owned()];
};
let end = start + end_rel;
let prefix = &pattern[..start];
let suffix = &pattern[end + 1..];
pattern[start + 1..end]
.split(',')
.flat_map(|middle| expand_braces(&format!("{prefix}{middle}{suffix}")))
.collect()
}
fn glob_match_one(pattern: &str, path: &str) -> bool {
let pattern = pattern.replace('\\', "/");
let path = path.replace('\\', "/");
let pattern_parts: Vec<&str> = pattern.split('/').collect();
let path_parts: Vec<&str> = path.split('/').collect();
glob_match_parts(&pattern_parts, &path_parts)
}
fn glob_match_parts(pattern: &[&str], path: &[&str]) -> bool {
if pattern.is_empty() {
return path.is_empty();
}
if pattern[0] == "**" {
if glob_match_parts(&pattern[1..], path) {
return true;
}
if !path.is_empty() {
return glob_match_parts(pattern, &path[1..]);
}
return false;
}
if path.is_empty() {
return false;
}
if segment_matches(pattern[0], path[0]) {
return glob_match_parts(&pattern[1..], &path[1..]);
}
false
}
fn segment_matches(pattern: &str, text: &str) -> bool {
let p = pattern.as_bytes();
let t = text.as_bytes();
let (mut pi, mut ti, mut star_idx, mut match_idx) = (0usize, 0usize, None, 0usize);
while ti < t.len() {
if pi < p.len() && (p[pi] == b'?' || p[pi] == t[ti]) {
pi += 1;
ti += 1;
} else if pi < p.len() && p[pi] == b'*' {
star_idx = Some(pi);
match_idx = ti;
pi += 1;
} else if let Some(star) = star_idx {
pi = star + 1;
match_idx += 1;
ti = match_idx;
} else {
return false;
}
}
while pi < p.len() && p[pi] == b'*' {
pi += 1;
}
pi == p.len()
}
#[must_use]
pub fn core_tools() -> Vec<Box<dyn Tool>> {
vec![
Box::new(BashTool),
Box::new(ReadTool),
Box::new(WriteTool),
Box::new(EditTool),
Box::new(GlobTool),
Box::new(GrepTool),
]
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use tempfile::tempdir;
fn text(result: &ToolResult) -> String {
result.content[0].text.clone()
}
#[test]
fn manifests_core_tools() {
let names: Vec<_> = core_tools().into_iter().map(|tool| tool.name()).collect();
assert_eq!(names, vec!["Bash", "Read", "Write", "Edit", "Glob", "Grep"]);
}
#[test]
fn bash_executes_command() {
let result = BashTool
.execute(json!({ "command": "printf 'hello'" }))
.unwrap();
assert_eq!(text(&result), "hello");
}
#[test]
fn read_schema_matches_expected_keys() {
let schema = ReadTool.input_schema();
let properties = schema["properties"].as_object().unwrap();
assert_eq!(schema["required"], json!(["file_path"]));
assert!(properties.contains_key("file_path"));
assert!(properties.contains_key("offset"));
assert!(properties.contains_key("limit"));
assert!(properties.contains_key("pages"));
}
#[test]
fn read_returns_numbered_lines() {
let dir = tempdir().unwrap();
let path = dir.path().join("sample.txt");
fs::write(&path, "alpha\nbeta\ngamma\n").unwrap();
let result = ReadTool
.execute(json!({ "file_path": path.to_string_lossy(), "offset": 2, "limit": 1 }))
.unwrap();
assert_eq!(text(&result), " 2\tbeta");
}
#[test]
fn write_creates_file_and_reports_create() {
let dir = tempdir().unwrap();
let path = dir.path().join("new.txt");
let result = WriteTool
.execute(json!({ "file_path": path.to_string_lossy(), "content": "hello" }))
.unwrap();
let payload: Value = serde_json::from_str(&text(&result)).unwrap();
assert_eq!(payload["type"], "create");
assert_eq!(fs::read_to_string(path).unwrap(), "hello");
}
#[test]
fn edit_replaces_single_match() {
let dir = tempdir().unwrap();
let path = dir.path().join("edit.txt");
fs::write(&path, "hello world\n").unwrap();
let result = EditTool
.execute(json!({
"file_path": path.to_string_lossy(),
"old_string": "world",
"new_string": "rust",
"replace_all": false
}))
.unwrap();
let payload: Value = serde_json::from_str(&text(&result)).unwrap();
assert_eq!(payload["replaceAll"], false);
assert_eq!(fs::read_to_string(path).unwrap(), "hello rust\n");
}
#[test]
fn glob_finds_matching_files() {
let dir = tempdir().unwrap();
fs::create_dir_all(dir.path().join("src/nested")).unwrap();
fs::write(dir.path().join("src/lib.rs"), "").unwrap();
fs::write(dir.path().join("src/nested/main.rs"), "").unwrap();
fs::write(dir.path().join("README.md"), "").unwrap();
let result = GlobTool
.execute(json!({ "pattern": "**/*.rs", "path": dir.path().to_string_lossy() }))
.unwrap();
let payload: Value = serde_json::from_str(&text(&result)).unwrap();
assert_eq!(payload["numFiles"], 2);
assert_eq!(
payload["filenames"],
json!(["src/lib.rs", "src/nested/main.rs"])
);
}
#[test]
fn grep_supports_file_list_mode() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("a.rs"), "fn main() {}\nlet alpha = 1;\n").unwrap();
fs::write(dir.path().join("b.txt"), "alpha\nalpha\n").unwrap();
let result = GrepTool
.execute(json!({
"pattern": "alpha",
"path": dir.path().to_string_lossy(),
"output_mode": "files_with_matches"
}))
.unwrap();
let payload: Value = serde_json::from_str(&text(&result)).unwrap();
assert_eq!(payload["filenames"], json!(["a.rs", "b.txt"]));
}
#[test]
fn grep_supports_content_and_count_modes() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("a.rs"), "alpha\nbeta\nalpha\n").unwrap();
let content = GrepTool
.execute(json!({
"pattern": "alpha",
"path": dir.path().to_string_lossy(),
"output_mode": "content",
"-n": true
}))
.unwrap();
let content_payload: Value = serde_json::from_str(&text(&content)).unwrap();
assert_eq!(content_payload["numLines"], 2);
assert!(content_payload["content"]
.as_str()
.unwrap()
.contains("a.rs:1:alpha"));
let count = GrepTool
.execute(json!({
"pattern": "alpha",
"path": dir.path().to_string_lossy(),
"output_mode": "count"
}))
.unwrap();
let count_payload: Value = serde_json::from_str(&text(&count)).unwrap();
assert_eq!(count_payload["numMatches"], 2);
assert_eq!(count_payload["content"], "a.rs:2");
}
}