Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nico/avoid stuck jobs w errors #622

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions shinkai-bin/shinkai-node/src/llm_provider/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub enum LLMProviderError {
ToolRouterNotFound,
UnexpectedResponseFormat(String),
InvalidVRPath(String),
ToolNotFound(String),
ToolRetrievalError(String),
ToolSearchError(String),
}

impl fmt::Display for LLMProviderError {
Expand Down Expand Up @@ -170,6 +173,9 @@ impl fmt::Display for LLMProviderError {
LLMProviderError::ToolRouterNotFound => write!(f, "Tool Router not found"),
LLMProviderError::UnexpectedResponseFormat(s) => write!(f, "Unexpected response format: {}", s),
LLMProviderError::InvalidVRPath(s) => write!(f, "Invalid VRPath: {}", s),
LLMProviderError::ToolNotFound(s) => write!(f, "Tool not found: {}", s),
LLMProviderError::ToolRetrievalError(s) => write!(f, "Tool retrieval error: {}", s),
LLMProviderError::ToolSearchError(s) => write!(f, "Tool search error: {}", s),
}
}
}
Expand Down Expand Up @@ -246,6 +252,9 @@ impl LLMProviderError {
LLMProviderError::ToolRouterNotFound => "ToolRouterNotFound",
LLMProviderError::UnexpectedResponseFormat(_) => "UnexpectedResponseFormat",
LLMProviderError::InvalidVRPath(_) => "InvalidVRPath",
LLMProviderError::ToolNotFound(_) => "ToolNotFound",
LLMProviderError::ToolRetrievalError(_) => "ToolRetrievalError",
LLMProviderError::ToolSearchError(_) => "ToolSearchError",
};

let error_message = format!("{}", self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ impl GenericInferenceChain {

// 2) Vector search for tooling / workflows if the workflow / tooling scope isn't empty
let job_config = full_job.config();
shinkai_log(
ShinkaiLogOption::JobExecution,
ShinkaiLogLevel::Info,
&format!("job_config: {:?}", job_config),
);
let mut tools = vec![];
let use_tools = match &llm_provider.model {
LLMProviderInterface::OpenAI(_) => true,
Expand Down Expand Up @@ -223,11 +228,30 @@ impl GenericInferenceChain {
// Search in JS Tools
let results = tool_router
.vector_search_enabled_tools_with_network(&user_message.clone(), 5)
.await
.unwrap();
for result in results {
if let Some(tool) = tool_router.get_tool_by_name(&result.tool_router_key).await.unwrap() {
tools.push(tool);
.await;

match results {
Ok(results) => {
for result in results {
match tool_router.get_tool_by_name(&result.tool_router_key).await {
Ok(Some(tool)) => tools.push(tool),
Ok(None) => {
return Err(LLMProviderError::ToolNotFound(
format!("Tool not found for key: {}", result.tool_router_key),
));
}
Err(e) => {
return Err(LLMProviderError::ToolRetrievalError(
format!("Error retrieving tool: {:?}", e),
));
}
}
}
}
Err(e) => {
return Err(LLMProviderError::ToolSearchError(
format!("Error during tool search: {:?}", e),
));
}
}
}
Expand Down Expand Up @@ -429,4 +453,4 @@ impl GenericInferenceChain {
}
}
}
}
}
97 changes: 81 additions & 16 deletions shinkai-bin/shinkai-node/src/managers/tool_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use shinkai_message_primitives::schemas::shinkai_tool_offering::{
};
use shinkai_message_primitives::schemas::wallet_mixed::{Asset, NetworkIdentifier};
use shinkai_message_primitives::shinkai_message::shinkai_message_schemas::WSTopic;
use shinkai_message_primitives::shinkai_utils::shinkai_logging::{shinkai_log, ShinkaiLogLevel, ShinkaiLogOption};
use shinkai_tools_primitives::tools::argument::ToolArgument;
use shinkai_tools_primitives::tools::error::ToolError;
use shinkai_tools_primitives::tools::js_toolkit::JSToolkit;
Expand Down Expand Up @@ -526,16 +527,35 @@ impl ToolRouter {
let my_agent_payments_manager = match &agent_payments_manager {
Some(manager) => manager.lock().await,
None => {
eprintln!("call_function> Agent payments manager is not available");
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Error,
"Agent payments manager is not available",
);
return Err(LLMProviderError::FunctionExecutionError(
"Agent payments manager is not available".to_string(),
));
}
};

// Get wallet balances
let balances = my_agent_payments_manager.get_balances().await.map_err(|e| {
LLMProviderError::FunctionExecutionError(format!("Failed to get balances: {}", e))
})?;
// Get wallet balances
let balances = match my_agent_payments_manager.get_balances().await {
Ok(balances) => balances,
Err(e) => {
eprintln!("Failed to get balances: {}", e);
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Error,
format!("Failed to get balances: {}", e).as_str(),
);
return Err(LLMProviderError::FunctionExecutionError(format!(
"Failed to get balances: {}",
e
)));
}
};

// Send a Network Request Invoice
let invoice_request = match my_agent_payments_manager
Expand All @@ -545,6 +565,11 @@ impl ToolRouter {
Ok(request) => request,
Err(e) => {
eprintln!("Failed to request invoice: {}", e);
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Error,
format!("Failed to request invoice: {}", e).as_str(),
);
return Err(LLMProviderError::FunctionExecutionError(format!(
"Failed to request invoice: {}",
e
Expand All @@ -554,10 +579,25 @@ impl ToolRouter {
(invoice_request, balances)
};

eprintln!("call_function> internal_invoice_request: {:?}", internal_invoice_request);

// TODO: Send ws_message to the frontend saying requesting invoice to X and more context

// Convert balances to Value
let balances_value = serde_json::to_value(&wallet_balances).map_err(|e| {
LLMProviderError::FunctionExecutionError(format!("Failed to convert balances to Value: {}", e))
})?;
let balances_value = match serde_json::to_value(&wallet_balances) {
Ok(value) => value,
Err(e) => {
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Error,
format!("Failed to convert balances to Value: {}", e).as_str(),
);
return Err(LLMProviderError::FunctionExecutionError(format!(
"Failed to convert balances to Value: {}",
e
)));
}
};

// Note: there must be a better way to do this
// Loop to check for the invoice unique_id
Expand Down Expand Up @@ -585,19 +625,43 @@ impl ToolRouter {
}
}
Err(_e) => {
// Nothing to do here
// If invoice is not found, check for InvoiceNetworkError
match context.db().get_invoice_network_error(&internal_invoice_request.unique_id.clone()) {
Ok(network_error) => {
eprintln!("InvoiceNetworkError found: {:?}", network_error);
shinkai_log(
ShinkaiLogOption::Network,
ShinkaiLogLevel::Error,
&format!("InvoiceNetworkError details: {:?}", network_error),
);
// Return the user_error_message if available, otherwise a default message
let error_message = network_error.user_error_message.unwrap_or_else(|| "Invoice network error encountered".to_string());
return Err(LLMProviderError::FunctionExecutionError(error_message));
}
Err(_) => {
// Continue waiting if neither invoice nor network error is found
}
}
}
}
tokio::time::sleep(interval).await;
}

// Convert notification_content to Value
let notification_content_value = serde_json::to_value(&notification_content).map_err(|e| {
LLMProviderError::FunctionExecutionError(format!(
"Failed to convert notification_content to Value: {}",
e
))
})?;
let notification_content_value = match serde_json::to_value(&notification_content) {
Ok(value) => value,
Err(e) => {
shinkai_log(
ShinkaiLogOption::Node,
ShinkaiLogLevel::Error,
format!("Failed to convert notification_content to Value: {}", e).as_str(),
);
return Err(LLMProviderError::FunctionExecutionError(format!(
"Failed to convert notification_content to Value: {}",
e
)));
}
};

// Get the ws from the context
{
Expand Down Expand Up @@ -739,14 +803,14 @@ impl ToolRouter {

#[cfg(test)]
mod tests {
use regex::Regex;
use serde_json::json;
use shinkai_baml::baml_builder::BamlConfig;
use shinkai_baml::baml_builder::ClientConfig;
use shinkai_baml::baml_builder::GeneratorConfig;
use shinkai_vector_resources::embedding_generator::EmbeddingGenerator;
use shinkai_vector_resources::embedding_generator::RemoteEmbeddingGenerator;
use tokio::task;
use regex::Regex;

use super::*;
use std::env;
Expand Down Expand Up @@ -868,7 +932,7 @@ mod tests {
mut_tool.embedding = None;
mut_tool.js_code = "".to_string();
let shinkai_tool = ShinkaiTool::JS(mut_tool, true);

tools_json.push(json!(shinkai_tool));

let tool_content = serde_json::to_string(&shinkai_tool)
Expand Down Expand Up @@ -995,4 +1059,5 @@ mod tests {
let mut file = File::create("../../tmp/documentation_results.json").expect("Failed to create file");
writeln!(file, "{}", json_data).expect("Failed to write to file");
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ed25519_dalek::SigningKey;
use futures::Future;
use shinkai_db::db::{ShinkaiDB, Topic};
use shinkai_job_queue_manager::job_queue_manager::JobQueueManager;
use shinkai_message_primitives::schemas::invoices::{Invoice, InvoiceError, InvoiceRequest, InvoiceStatusEnum};
use shinkai_message_primitives::schemas::invoices::{Invoice, InvoiceError, InvoiceRequest, InvoiceRequestNetworkError, InvoiceStatusEnum};
use shinkai_message_primitives::schemas::shinkai_name::ShinkaiName;
use shinkai_message_primitives::schemas::shinkai_tool_offering::{ShinkaiToolOffering, UsageType, UsageTypeInquiry};
use shinkai_message_primitives::shinkai_message::shinkai_message_schemas::MessageSchemaType;
Expand Down Expand Up @@ -623,7 +623,69 @@ impl ExtAgentOfferingsManager {
// Call request_invoice to generate an invoice
let invoice = self
.request_invoice(requester_node_name.clone(), invoice_request.clone())
.await?;
.await;

let invoice = match invoice {
Ok(inv) => inv,
Err(e) => {
// Handle the error manually
shinkai_log(
ShinkaiLogOption::ExtSubscriptions,
ShinkaiLogLevel::Error,
&format!("Failed to request invoice: {:?}", e),
);

// Create an InvoiceNetworkError
let network_error = InvoiceRequestNetworkError {
invoice_id: invoice_request.unique_id.clone(),
provider_name: self.node_name.clone(),
requester_name: invoice_request.requester_name.clone(),
request_date_time: invoice_request.request_date_time,
response_date_time: Utc::now(),
user_error_message: Some(format!("{:?}", e)),
error_message: format!("{:?}", e),
};

// Send the InvoiceRequestNetworkError back to the requester
if let Some(identity_manager_arc) = self.identity_manager.upgrade() {
let identity_manager = identity_manager_arc.lock().await;
let standard_identity = identity_manager
.external_profile_to_global_identity(&invoice_request.requester_name.to_string())
.await
.map_err(|e| AgentOfferingManagerError::OperationFailed(e))?;
drop(identity_manager);
let receiver_public_key = standard_identity.node_encryption_public_key;
let proxy_builder_info =
get_proxy_builder_info_static(identity_manager_arc, self.proxy_connection_info.clone()).await;

let error_message = ShinkaiMessageBuilder::create_generic_invoice_message(
network_error.clone(),
MessageSchemaType::InvoiceRequestNetworkError,
clone_static_secret_key(&self.my_encryption_secret_key),
clone_signature_secret_key(&self.my_signature_secret_key),
receiver_public_key,
self.node_name.to_string(),
"".to_string(),
invoice_request.requester_name.to_string(),
"main".to_string(),
proxy_builder_info,
)
.map_err(|e| AgentOfferingManagerError::OperationFailed(e.to_string()))?;

send_message_to_peer(
error_message,
self.db.clone(),
standard_identity,
self.my_encryption_secret_key.clone(),
self.identity_manager.clone(),
self.proxy_connection_info.clone(),
)
.await?;
}

return Err(e);
}
};

// Continue
if let Some(identity_manager_arc) = self.identity_manager.upgrade() {
Expand Down
Loading
Loading