Skip to content

Commit f7b0c84

Browse files
authored
Merge pull request #1120 from dcSpark/nico/fix_logs_tools
chore: fix tool logs
2 parents 2ed7546 + 8453189 commit f7b0c84

File tree

4 files changed

+87
-49
lines changed

4 files changed

+87
-49
lines changed

shinkai-bin/shinkai-node/src/managers/tool_router.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,7 @@ impl ToolRouter {
428428
}?;
429429

430430
if !do_install {
431-
tools_skipped += 1;
432-
return Ok::<(), ToolError>(());
431+
return Ok::<_, ToolError>(("skipped", tool_name.clone()));
433432
}
434433

435434
let val: Value = Node::v2_api_import_tool_url_internal(
@@ -446,12 +445,12 @@ impl ToolRouter {
446445
// We stored the tool under val["tool"] in the JSON response
447446
match serde_json::from_value::<ShinkaiTool>(val["tool"].clone()) {
448447
Ok(_tool) => {
449-
tools_added += 1;
450448
println!("Successfully imported tool {} (version: {})", tool_name, new_version);
449+
Ok::<_, ToolError>(("added", tool_name.clone()))
451450
}
452451
Err(err) => {
453-
tools_failed += 1;
454452
eprintln!("Couldn't parse 'tool' field as ShinkaiTool: {}", err);
453+
Ok::<_, ToolError>(("failed", tool_name.clone()))
455454
}
456455
}
457456
} else if r#type == "Agent" {
@@ -465,8 +464,7 @@ impl ToolRouter {
465464
Err(e) => Err(ToolError::DatabaseError(e.to_string())),
466465
}?;
467466
if !do_install {
468-
tools_skipped += 1;
469-
return Ok::<(), ToolError>(());
467+
return Ok::<_, ToolError>(("skipped", tool_name.clone()));
470468
}
471469

472470
let val: Value = Node::v2_api_import_agent_url_internal(
@@ -482,19 +480,35 @@ impl ToolRouter {
482480

483481
match serde_json::from_value::<Agent>(val["agent"].clone()) {
484482
Ok(agent) => {
485-
tools_added += 1;
486483
println!("Successfully imported agent {}", agent.name);
484+
Ok::<_, ToolError>(("added", tool_name.clone()))
487485
}
488486
Err(err) => {
489-
tools_failed += 1;
490487
eprintln!("Couldn't parse 'agent' field as Agent: {}", err);
488+
Ok::<_, ToolError>(("failed", tool_name.clone()))
491489
}
492490
}
491+
} else {
492+
Ok::<_, ToolError>(("skipped", tool_name.clone()))
493493
}
494-
Ok::<(), ToolError>(())
495494
}
496495
});
497-
futures::future::join_all(futures).await;
496+
497+
let results = futures::future::join_all(futures).await;
498+
for result in results {
499+
match result {
500+
Ok((status, _)) => match status {
501+
"added" => tools_added += 1,
502+
"skipped" => tools_skipped += 1,
503+
"failed" => tools_failed += 1,
504+
_ => {}
505+
},
506+
Err(e) => {
507+
eprintln!("Error processing tool: {}", e);
508+
tools_failed += 1;
509+
}
510+
}
511+
}
498512
}
499513

500514
let duration = start_time.elapsed();

shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -759,37 +759,6 @@ impl Node {
759759

760760
let version = env!("CARGO_PKG_VERSION");
761761

762-
// Check if the version is 0.9.0
763-
let lancedb_exists = {
764-
// DB Path Env Vars
765-
let node_storage_path: String = env::var("NODE_STORAGE_PATH").unwrap_or_else(|_| "storage".to_string());
766-
767-
// Try to open the folder main_db and search for lancedb
768-
let main_db_path = std::path::Path::new(&node_storage_path).join("main_db");
769-
770-
if let Ok(entries) = std::fs::read_dir(&main_db_path) {
771-
entries.filter_map(Result::ok).any(|entry| {
772-
let entry_path = entry.path();
773-
if entry_path.is_dir() {
774-
if entry_path.to_str().map_or(false, |s| s.contains("lancedb")) {
775-
return true;
776-
}
777-
// Check one more level deep
778-
if let Ok(sub_entries) = std::fs::read_dir(&entry_path) {
779-
return sub_entries.filter_map(Result::ok).any(|sub_entry| {
780-
let sub_entry_path = sub_entry.path();
781-
sub_entry_path.is_dir()
782-
&& sub_entry_path.to_str().map_or(false, |s| s.contains("lance"))
783-
});
784-
}
785-
}
786-
false
787-
})
788-
} else {
789-
false
790-
}
791-
};
792-
793762
let (_current_version, needs_global_reset) = match db.get_version() {
794763
Ok(version) => version,
795764
Err(_err) => {
@@ -814,7 +783,7 @@ impl Node {
814783
"is_pristine": !db.has_any_profile().unwrap_or(false),
815784
"public_https_certificate": public_https_certificate,
816785
"version": version,
817-
"update_requires_reset": needs_global_reset || lancedb_exists,
786+
"update_requires_reset": needs_global_reset,
818787
"docker_status": docker_status,
819788
})))
820789
.await;

shinkai-bin/shinkai-node/src/runner.rs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use shinkai_message_primitives::shinkai_utils::signatures::{
1919
use std::collections::HashMap;
2020
use std::error::Error as StdError;
2121
use std::fmt;
22+
use std::net::TcpListener;
2223
use std::path::Path;
2324
use std::sync::{Arc, Weak};
2425
use std::{env, fs};
@@ -49,6 +50,14 @@ impl From<Box<dyn StdError + Send + Sync>> for NodeRunnerError {
4950
}
5051
}
5152

53+
/// Checks if a port is available for binding
54+
fn port_is_available(port: u16) -> bool {
55+
match TcpListener::bind(("127.0.0.1", port)) {
56+
Ok(_) => true,
57+
Err(_) => false,
58+
}
59+
}
60+
5261
pub async fn initialize_node() -> Result<
5362
(Sender<NodeCommand>, JoinHandle<()>, JoinHandle<()>, Weak<Mutex<Node>>),
5463
Box<dyn std::error::Error + Send + Sync>,
@@ -61,6 +70,42 @@ pub async fn initialize_node() -> Result<
6170
let args = parse_args();
6271
let node_env = fetch_node_environment();
6372

73+
// Check if required ports are available
74+
let api_port = node_env.api_listen_address.port();
75+
let node_port = node_env.listen_address.port();
76+
let ws_port = node_env.ws_address.map(|addr| addr.port());
77+
let https_port = node_env.api_https_listen_address.port();
78+
79+
if !port_is_available(api_port) {
80+
return Err(Box::new(std::io::Error::new(
81+
std::io::ErrorKind::AddrInUse,
82+
format!("API port {} is already in use", api_port),
83+
)));
84+
}
85+
86+
if !port_is_available(node_port) {
87+
return Err(Box::new(std::io::Error::new(
88+
std::io::ErrorKind::AddrInUse,
89+
format!("Node port {} is already in use", node_port),
90+
)));
91+
}
92+
93+
if let Some(port) = ws_port {
94+
if !port_is_available(port) {
95+
return Err(Box::new(std::io::Error::new(
96+
std::io::ErrorKind::AddrInUse,
97+
format!("WebSocket port {} is already in use", port),
98+
)));
99+
}
100+
}
101+
102+
if !port_is_available(https_port) {
103+
return Err(Box::new(std::io::Error::new(
104+
std::io::ErrorKind::AddrInUse,
105+
format!("HTTPS port {} is already in use", https_port),
106+
)));
107+
}
108+
64109
// TODO:
65110
// Read file encryption key from ENV variable and decrypt the secrets file
66111
// Store in memory this file encryption key, which is used to encrypt / decrypt other information
@@ -218,7 +263,7 @@ pub async fn initialize_node() -> Result<
218263
let api_listen_address = node_env.clone().api_listen_address;
219264
let api_https_listen_address = node_env.clone().api_https_listen_address;
220265
let api_server = tokio::spawn(async move {
221-
if let Err(e) = node_api_router::run_api(
266+
match node_api_router::run_api(
222267
node_commands_sender,
223268
api_listen_address,
224269
api_https_listen_address,
@@ -228,12 +273,21 @@ pub async fn initialize_node() -> Result<
228273
)
229274
.await
230275
{
231-
shinkai_log(
232-
ShinkaiLogOption::Node,
233-
ShinkaiLogLevel::Error,
234-
&format!("API server failed to start: {}", e),
235-
);
236-
panic!("API server failed to start: {}", e);
276+
Ok(_) => {
277+
shinkai_log(
278+
ShinkaiLogOption::Node,
279+
ShinkaiLogLevel::Info,
280+
"API server started successfully",
281+
);
282+
}
283+
Err(e) => {
284+
shinkai_log(
285+
ShinkaiLogOption::Node,
286+
ShinkaiLogLevel::Error,
287+
&format!("API server failed to start: {}", e),
288+
);
289+
panic!("API server failed to start: {}", e);
290+
}
237291
}
238292
});
239293

shinkai-libs/shinkai-http-api/src/api_v2/api_v2_handlers_general.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ pub async fn get_public_keys(sender: Sender<NodeCommand>) -> Result<impl warp::R
338338
)]
339339
pub async fn health_check(sender: Sender<NodeCommand>, node_name: String) -> Result<impl warp::Reply, warp::Rejection> {
340340
let (res_sender, res_receiver) = async_channel::bounded(1);
341+
println!("Health check route called");
341342

342343
// Send the APIHealthCheck command to retrieve the pristine state and public HTTPS certificate
343344
sender

0 commit comments

Comments
 (0)