Skip to content

Commit

Permalink
Merge pull request #626 from dcSpark/nico/fix_local_payment
Browse files Browse the repository at this point in the history
Fix Local Payment with Hot Wallet
  • Loading branch information
nicarq authored Oct 25, 2024
2 parents a0d4789 + ee1bc27 commit 4460d9d
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 136 deletions.
106 changes: 106 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions shinkai-bin/shinkai-node/src/managers/identity_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ impl IdentityManagerTrait for IdentityManager {
IdentityPermissions::None,
))
}
Err(_) => Err("Failed to get first address".to_string()),
Err(e) => Err(format!("Failed to get first address: {}", e)),
},
Err(_) => Err(format!(
"Failed to get identity network manager for profile name: {}",
full_profile_name
Err(e) => Err(format!(
"Failed to get identity network manager for profile name: {} with error: {}",
full_profile_name, e
)),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl IdentityNetworkManager {

// Check if any of the address_or_proxy_nodes ends with .sepolia-shinkai
if record.address_or_proxy_nodes.iter().any(|node| {
node.ends_with(".sepolia-shinkai") || node.ends_with(".shinkai") || node.ends_with(".arb-sep-shinkai")
let node_base = node.split(':').next().unwrap_or(node);
node_base.ends_with(".sepolia-shinkai") ||
node_base.ends_with(".shinkai") ||
node_base.ends_with(".arb-sep-shinkai")
}) {
// Call the proxy node to get the actual data
let proxy_identity = record.address_or_proxy_nodes.clone();
Expand Down
33 changes: 30 additions & 3 deletions shinkai-bin/shinkai-node/src/managers/tool_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ impl ToolRouter {
}
};

// Get wallet balances
// Get wallet balances
let balances = match my_agent_payments_manager.get_balances().await {
Ok(balances) => balances,
Expand Down Expand Up @@ -679,9 +678,10 @@ impl ToolRouter {
description: network_tool.description.clone(),
usage_type: network_tool.usage_type.clone(),
invoice_id: internal_invoice_request.unique_id.clone(),
invoice: notification_content_value,
invoice: notification_content_value.clone(),
function_args: function_args.clone(),
wallet_balances: balances_value,
wallet_balances: balances_value.clone(),
error_message: None,
};

let widget = WSMessageType::Widget(WidgetMetadata::PaymentRequest(payment_metadata));
Expand All @@ -701,6 +701,33 @@ impl ToolRouter {

loop {
if start_time.elapsed() > timeout {
// Send a timeout notification via WebSocket
{
let ws_manager = context.ws_manager_trait();

if let Some(ws_manager) = &ws_manager {
let ws_manager = ws_manager.lock().await;
let job = context.full_job();

let topic = WSTopic::Widget;
let subtopic = job.conversation_inbox_name.to_string();
let update = "Timeout while waiting for invoice payment".to_string();
let payment_metadata = PaymentMetadata {
tool_key: network_tool.name.clone(),
description: network_tool.description.clone(),
usage_type: network_tool.usage_type.clone(),
invoice_id: internal_invoice_request.unique_id.clone(),
invoice: notification_content_value.clone(),
function_args: function_args.clone(),
wallet_balances: balances_value.clone(),
error_message: Some(update.clone()),
};

let widget = WSMessageType::Widget(WidgetMetadata::PaymentRequest(payment_metadata));
ws_manager.queue_message(topic, subtopic, update, widget, false).await;
}
}

return Err(LLMProviderError::FunctionExecutionError(
"Timeout while waiting for invoice payment".to_string(),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use shinkai_lancedb::lance_db::shinkai_lance_db::LanceShinkaiDb;
use shinkai_message_primitives::schemas::shinkai_tool_offering::UsageTypeInquiry;
use shinkai_tools_primitives::tools::shinkai_tool::ShinkaiTool;
use tokio::sync::{Mutex, RwLock};
use regex::Regex;

use crate::network::{
agent_payments_manager::my_agent_offerings_manager::MyAgentOfferingsManager, node_error::NodeError, Node,
Expand Down Expand Up @@ -228,10 +229,22 @@ impl Node {
{
Ok(payment) => payment,
Err(e) => {
// Use regex to extract a more human-readable error message
let error_message = e.to_string();
let human_readable_message = if let Ok(regex) = regex::Regex::new(r#"message: \\"(.*?)\\""#) {
if let Some(captures) = regex.captures(&error_message) {
captures.get(1).map_or(error_message.clone(), |m| m.as_str().to_string())
} else {
error_message.clone()
}
} else {
error_message.clone()
};

let api_error = APIError {
code: StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
error: "Internal Server Error".to_string(),
message: format!("Failed to pay invoice: {}", e),
message: format!("Failed to pay invoice: {}", human_readable_message),
};
let _ = res.send(Err(api_error)).await;
return Ok(());
Expand Down
Loading

0 comments on commit 4460d9d

Please sign in to comment.