Remove unused GenAIScript module and add configurable timeouts
This commit is contained in:
@@ -6,5 +6,5 @@ pub async fn image_generator(stream_id: &str, input: &str) -> Result<Child, Stri
|
|||||||
"Running image generator, \ninput: {}",
|
"Running image generator, \ninput: {}",
|
||||||
input
|
input
|
||||||
);
|
);
|
||||||
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/image-generator.genai.mts").await
|
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/image-generator.genai.mts", 10).await
|
||||||
}
|
}
|
||||||
|
@@ -2,5 +2,5 @@ use crate::utils::utils::run_agent;
|
|||||||
use tokio::process::Child;
|
use tokio::process::Child;
|
||||||
|
|
||||||
pub async fn news_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
pub async fn news_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
||||||
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/news-search.genai.mts").await
|
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/news-search.genai.mts", 10).await
|
||||||
}
|
}
|
||||||
|
@@ -2,5 +2,5 @@ use crate::utils::utils::run_agent;
|
|||||||
use tokio::process::Child;
|
use tokio::process::Child;
|
||||||
|
|
||||||
pub async fn scrape_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
pub async fn scrape_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
||||||
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/web-scrape.genai.mts").await
|
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/web-scrape.genai.mts", 10).await
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ use tracing;
|
|||||||
use crate::utils::utils::run_agent;
|
use crate::utils::utils::run_agent;
|
||||||
|
|
||||||
pub async fn search_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
pub async fn search_agent(stream_id: &str, input: &str) -> Result<Child, String> {
|
||||||
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/web-search.genai.mts").await
|
run_agent(stream_id, input, "./packages/genaiscript/genaisrc/web-search.genai.mts", 10).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,90 +0,0 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use tokio::process::{Child, Command};
|
|
||||||
use tracing;
|
|
||||||
|
|
||||||
const DEFAULT_ENV_VARS: [&str; 4] = [
|
|
||||||
"OPENAI_API_KEY",
|
|
||||||
"OPENAI_API_BASE",
|
|
||||||
"GENAISCRIPT_MODEL_LARGE",
|
|
||||||
"GENAISCRIPT_MODEL_SMALL",
|
|
||||||
];
|
|
||||||
|
|
||||||
pub struct GenAIScriptConfig {
|
|
||||||
script_path: PathBuf,
|
|
||||||
output_dir: PathBuf,
|
|
||||||
stream_id: String,
|
|
||||||
user_input: String,
|
|
||||||
retry_count: u32,
|
|
||||||
env_vars: HashMap<String, String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GenAIScriptConfig {
|
|
||||||
pub fn new(script_path: impl Into<PathBuf>, stream_id: impl Into<String>, user_input: impl Into<String>) -> Self {
|
|
||||||
let mut env_vars = HashMap::new();
|
|
||||||
|
|
||||||
// Initialize with default environment variables
|
|
||||||
for var in DEFAULT_ENV_VARS {
|
|
||||||
if let Ok(value) = std::env::var(var) {
|
|
||||||
env_vars.insert(var.to_string(), value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
|
||||||
script_path: script_path.into(),
|
|
||||||
output_dir: PathBuf::from("./web-agent-rs/output"),
|
|
||||||
stream_id: stream_id.into(),
|
|
||||||
user_input: user_input.into(),
|
|
||||||
retry_count: 0,
|
|
||||||
env_vars,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_output_dir(mut self, dir: impl Into<PathBuf>) -> Self {
|
|
||||||
self.output_dir = dir.into();
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_retry_count(mut self, count: u32) -> Self {
|
|
||||||
self.retry_count = count;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn with_additional_env_vars(mut self, vars: HashMap<String, String>) -> Self {
|
|
||||||
self.env_vars.extend(vars);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn run_genaiscript(config: GenAIScriptConfig) -> Result<Child, String> {
|
|
||||||
tracing::debug!("Initiating GenAIScript for stream {}", config.stream_id);
|
|
||||||
|
|
||||||
let output_path = config.output_dir.join(&config.stream_id);
|
|
||||||
|
|
||||||
let mut command = Command::new("bunx");
|
|
||||||
command
|
|
||||||
.arg("genaiscript")
|
|
||||||
.arg("run")
|
|
||||||
.arg(&config.script_path)
|
|
||||||
// .arg("--fail-on-errors")
|
|
||||||
.arg("—out-trace")
|
|
||||||
.arg(output_path)
|
|
||||||
.arg("--retry")
|
|
||||||
.arg(config.retry_count.to_string())
|
|
||||||
.arg("--vars")
|
|
||||||
.arg(format!("USER_INPUT='{}'", config.user_input));
|
|
||||||
|
|
||||||
// Add environment variables
|
|
||||||
for (key, value) in config.env_vars {
|
|
||||||
command.env(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
command
|
|
||||||
.stdout(std::process::Stdio::piped())
|
|
||||||
.stderr(std::process::Stdio::inherit())
|
|
||||||
.spawn()
|
|
||||||
.map_err(|e| {
|
|
||||||
tracing::error!("Failed to spawn genaiscript process: {}", e);
|
|
||||||
e.to_string()
|
|
||||||
})
|
|
||||||
}
|
|
@@ -7,7 +7,6 @@ mod routes;
|
|||||||
mod setup;
|
mod setup;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
mod agents;
|
mod agents;
|
||||||
mod genaiscript;
|
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@@ -7,12 +7,9 @@ use tracing;
|
|||||||
|
|
||||||
pub struct ShimBinding {
|
pub struct ShimBinding {
|
||||||
user_input: String,
|
user_input: String,
|
||||||
file_path: String, // Add new field for the file path
|
file_path: String,
|
||||||
openai_api_key: String,
|
openai_api_key: String,
|
||||||
openai_api_base: String,
|
openai_api_base: String,
|
||||||
bing_search_api_key: String,
|
|
||||||
perigon_api_key: String,
|
|
||||||
tavily_api_key: String,
|
|
||||||
genaiscript_model_large: String,
|
genaiscript_model_large: String,
|
||||||
genaiscript_model_small: String,
|
genaiscript_model_small: String,
|
||||||
searxng_api_base_url: String,
|
searxng_api_base_url: String,
|
||||||
@@ -20,17 +17,14 @@ pub struct ShimBinding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ShimBinding {
|
impl ShimBinding {
|
||||||
pub fn new(user_input: String, file_path: String) -> Self { // Update constructor to take file path
|
pub fn new(user_input: String, file_path: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
user_input,
|
user_input,
|
||||||
file_path, // Initialize the new field
|
file_path, // Initialize the new field
|
||||||
openai_api_key: env::var("OPENAI_API_KEY").unwrap_or_default(),
|
openai_api_key: env::var("OPENAI_API_KEY").unwrap_or_default(),
|
||||||
openai_api_base: env::var("OPENAI_API_BASE").unwrap_or_default(),
|
openai_api_base: env::var("OPENAI_API_BASE").unwrap_or_default(),
|
||||||
bing_search_api_key: env::var("BING_SEARCH_API_KEY").unwrap_or_default(),
|
|
||||||
tavily_api_key: env::var("TAVILY_API_KEY").unwrap_or_default(),
|
|
||||||
genaiscript_model_large: env::var("GENAISCRIPT_MODEL_LARGE").unwrap_or_default(),
|
genaiscript_model_large: env::var("GENAISCRIPT_MODEL_LARGE").unwrap_or_default(),
|
||||||
genaiscript_model_small: env::var("GENAISCRIPT_MODEL_SMALL").unwrap_or_default(),
|
genaiscript_model_small: env::var("GENAISCRIPT_MODEL_SMALL").unwrap_or_default(),
|
||||||
perigon_api_key: env::var("PERIGON_API_KEY").unwrap_or_default(),
|
|
||||||
searxng_api_base_url: env::var("SEARXNG_API_BASE_URL").unwrap_or_default(),
|
searxng_api_base_url: env::var("SEARXNG_API_BASE_URL").unwrap_or_default(),
|
||||||
searxng_password: env::var("SEARXNG_PASSWORD").unwrap_or_default(),
|
searxng_password: env::var("SEARXNG_PASSWORD").unwrap_or_default(),
|
||||||
}
|
}
|
||||||
@@ -44,23 +38,20 @@ impl ShimBinding {
|
|||||||
.arg(format!("USER_INPUT={}", self.user_input))
|
.arg(format!("USER_INPUT={}", self.user_input))
|
||||||
.env("OPENAI_API_KEY", &self.openai_api_key)
|
.env("OPENAI_API_KEY", &self.openai_api_key)
|
||||||
.env("OPENAI_API_BASE", &self.openai_api_base)
|
.env("OPENAI_API_BASE", &self.openai_api_base)
|
||||||
.env("BING_SEARCH_API_KEY", &self.bing_search_api_key)
|
|
||||||
.env("TAVILY_API_KEY", &self.tavily_api_key)
|
|
||||||
.env("GENAISCRIPT_MODEL_LARGE", &self.genaiscript_model_large)
|
.env("GENAISCRIPT_MODEL_LARGE", &self.genaiscript_model_large)
|
||||||
.env("GENAISCRIPT_MODEL_SMALL", &self.genaiscript_model_small)
|
.env("GENAISCRIPT_MODEL_SMALL", &self.genaiscript_model_small)
|
||||||
.env("PERIGON_API_KEY", &self.perigon_api_key)
|
|
||||||
.env("SEARXNG_API_BASE_URL", &self.searxng_api_base_url)
|
.env("SEARXNG_API_BASE_URL", &self.searxng_api_base_url)
|
||||||
.env("SEARXNG_PASSWORD", &self.searxng_password)
|
.env("SEARXNG_PASSWORD", &self.searxng_password)
|
||||||
.stdout(std::process::Stdio::piped()) // Use tokio::io::Stdio::piped()
|
.stdout(std::process::Stdio::piped())
|
||||||
.stderr(std::process::Stdio::inherit()); // Use tokio::io::Stdio::piped()
|
.stderr(std::process::Stdio::inherit());
|
||||||
|
|
||||||
command.spawn()
|
command.spawn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Generic helper to execute a ShimBinding-based agent with a timeout
|
/// wrapper executes an agent with a timeout
|
||||||
pub async fn run_agent(stream_id: &str, input: &str, file_path: &str) -> Result<Child, String> {
|
pub async fn run_agent(stream_id: &str, input: &str, file_path: &str, timeout_seconds: u64 ) -> Result<Child, String> {
|
||||||
tracing::debug!("Initiating agent for stream {} with file path {}", stream_id, file_path);
|
tracing::debug!("Initiating agent for stream {} with file path {}", stream_id, file_path);
|
||||||
|
|
||||||
let shim_binding = ShimBinding::new(input.to_string(), file_path.to_string());
|
let shim_binding = ShimBinding::new(input.to_string(), file_path.to_string());
|
||||||
@@ -74,7 +65,7 @@ pub async fn run_agent(stream_id: &str, input: &str, file_path: &str) -> Result<
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
timeout(Duration::from_secs(10), spawn_future)
|
timeout(Duration::from_secs(timeout_seconds), spawn_future)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|_| Err("Command timed out after 10 seconds".to_string()))
|
.unwrap_or_else(|_| Err("Command timed out after 10 seconds".to_string()))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user