chat agent

This commit is contained in:
pavel 2026-02-11 16:43:05 +01:00
commit b44e4ed6f9
11 changed files with 626 additions and 16 deletions

View file

@ -2,11 +2,13 @@ pub mod api;
pub mod tools;
use chrono::Utc;
use sea_orm::DatabaseConnection;
use std::time::{Duration, Instant};
use self::api::{ChatRequest, ChatResponse, Message, Tool};
pub struct Agent {
db: DatabaseConnection,
client: reqwest::Client,
url: String,
zen_api_key: Option<String>,
@ -21,6 +23,7 @@ use crate::error::{AppError, AppResult};
impl Agent {
pub fn new(
db: DatabaseConnection,
zen_api_key: Option<String>,
tavily_api_key: Option<String>,
initial_message: String,
@ -55,6 +58,7 @@ impl Agent {
.map_err(|e| AppError::Internal(format!("Failed to build HTTP client: {}", e)))?;
Ok(Self {
db,
client,
url: "https://opencode.ai/zen/v1/chat/completions".to_string(),
zen_api_key,
@ -122,7 +126,7 @@ impl Agent {
self.log(&format!("Calling tool: {}", tool_call.function.name));
let (tool_message, is_final, tool_answer) =
tools::handle_tool_call(&tool_call, &self.tavily_api_key)
tools::handle_tool_call(&tool_call, &self.tavily_api_key, &self.db)
.await
.map_err(|e| {
AppError::Internal(format!("Tool execution failed: {}", e))