use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
text::Span,
widgets::Widget,
};
use crate::widgets::ai_chat::components::theme::ChatColors;
use super::block_tool::BlockTool;
use super::inline_tool::ToolStatus;
#[derive(Debug, Clone, PartialEq)]
pub struct SearchResult {
pub title: String,
pub url: String,
pub snippet: String,
}
pub struct ToolWebSearch<'a> {
query: &'a str,
results_count: usize,
results: Vec<SearchResult>,
status: ToolStatus,
expanded: bool,
}
impl<'a> ToolWebSearch<'a> {
pub fn new(query: &'a str) -> Self {
Self {
query,
results_count: 0,
results: Vec::new(),
status: ToolStatus::Pending,
expanded: false,
}
}
pub fn results_count(mut self, count: usize) -> Self {
self.results_count = count;
self
}
pub fn add_result(mut self, result: SearchResult) -> Self {
self.results_count += 1;
self.results.push(result);
self
}
pub fn results(mut self, results: Vec<SearchResult>) -> Self {
self.results = results;
self.results_count = results.len();
self
}
pub fn status(mut self, status: ToolStatus) -> Self {
self.status = status;
self
}
pub fn expanded(mut self, expanded: bool) -> Self {
self.expanded = expanded;
self
}
fn border_color(&self, colors: &ChatColors) -> Color {
match self.status {
ToolStatus::Pending => colors.warning,
ToolStatus::Complete => colors.success,
ToolStatus::Error => colors.error,
ToolStatus::PermissionPending => Color::Rgb(255, 165, 0),
}
}
pub fn render(&self, area: Rect, buf: &mut Buffer, colors: &ChatColors) {
if area.height < 1 {
return;
}
let max_y = area.y + area.height;
let mut y = area.y;
let border_color = self.border_color(colors);
for y_pos in area.y..max_y {
buf.get_mut(area.x, y_pos)
.set_char('│')
.set_style(Style::default().fg(border_color));
}
let icon = '◈';
let expand_icon = if self.expanded { "▼" } else { "▶" };
let count_text = if self.status == ToolStatus::Complete {
format!(" ({} results)", self.results_count)
} else {
String::new()
};
let header = format!(
"{} WebSearch \"{}\" {}{}",
icon, self.query, expand_icon, count_text
);
let header_span = Span::styled(
header,
Style::default()
.fg(colors.text)
.add_modifier(Modifier::BOLD),
);
buf.set_span(area.x + 2, y, &header_span, area.width.saturating_sub(3));
y += 1;
if y < max_y {
buf.set_span(
area.x + 2,
y,
&Span::styled(
"─".repeat(30),
Style::default()
.fg(border_color)
.add_modifier(Modifier::DIM),
),
area.width.saturating_sub(3),
);
y += 1;
}
if y < max_y && !self.results.is_empty() {
let display_count = if self.expanded {
self.results.len()
} else {
self.results.len().min(5)
};
for result in self.results.iter().take(display_count) {
if y >= max_y {
break;
}
let title_span = Span::styled(
format!(" 🔗 {}", result.title),
Style::default()
.fg(colors.primary)
.add_modifier(Modifier::BOLD),
);
buf.set_span(area.x + 2, y, &title_span, area.width.saturating_sub(3));
y += 1;
if y < max_y {
let url_display = if result.url.len() > 50 {
format!(" {}...", &result.url[..50])
} else {
result.url.clone()
};
let url_span = Span::styled(
url_display,
Style::default()
.fg(colors.text_muted)
.add_modifier(Modifier::ITALIC),
);
buf.set_span(area.x + 2, y, &url_span, area.width.saturating_sub(3));
y += 1;
}
if y < max_y && !result.snippet.is_empty() {
let snippet_display = if result.snippet.len() > 60 {
format!(" {}...", &result.snippet[..60])
} else {
result.snippet.clone()
};
let snippet_span =
Span::styled(snippet_display, Style::default().fg(colors.text_muted));
buf.set_span(area.x + 2, y, &snippet_span, area.width.saturating_sub(3));
y += 1;
}
}
if !self.expanded && self.results.len() > 5 && y < max_y {
buf.set_span(
area.x + 2,
y,
&Span::styled(
format!(
" ... and {} more (click to expand)",
self.results.len() - 5
),
Style::default()
.fg(colors.text_muted)
.add_modifier(Modifier::ITALIC),
),
area.width.saturating_sub(3),
);
y += 1;
}
}
if self.results.is_empty() && self.status == ToolStatus::Complete && y < max_y {
let no_result_span = Span::styled(
" No results found",
Style::default()
.fg(colors.text_muted)
.add_modifier(Modifier::ITALIC),
);
buf.set_span(area.x + 2, y, &no_result_span, area.width.saturating_sub(3));
y += 1;
}
if y < max_y {
let status_text = match self.status {
ToolStatus::Pending => "⏳ Searching...",
ToolStatus::Complete => "✓ Search complete",
ToolStatus::Error => "✗ Search failed",
ToolStatus::PermissionPending => "⚠ Permission required",
};
let status_span = Span::styled(
status_text,
Style::default()
.fg(border_color)
.add_modifier(Modifier::BOLD),
);
buf.set_span(area.x + 2, y, &status_span, area.width.saturating_sub(3));
}
}
pub fn toggle_expanded(&mut self) {
self.expanded = !self.expanded;
}
}
impl Widget for ToolWebSearch<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let colors = ChatColors::default();
self.render(area, buf, &colors);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tool_websearch_basic() {
let search = ToolWebSearch::new("rust programming");
assert_eq!(search.query, "rust programming");
}
#[test]
fn test_tool_websearch_with_results() {
let search = ToolWebSearch::new("test").add_result(SearchResult {
title: "Test Result".to_string(),
url: "https://example.com".to_string(),
snippet: "A test result".to_string(),
});
assert_eq!(search.results_count, 1);
}
}