Skip to content

Commit 7e0d6c4

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/ws-proxy
2 parents 6680321 + dfc6584 commit 7e0d6c4

File tree

14 files changed

+234
-120
lines changed

14 files changed

+234
-120
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ members = [
1616
resolver = "2"
1717

1818
[workspace.package]
19-
version = "1.0.4"
19+
version = "1.0.5"
2020
edition = "2021"
2121
authors = ["Nico Arqueros <nico@shinkai.com>"]
2222

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,18 +2011,26 @@ impl Node {
20112011
}
20122012

20132013
// Get the internal_comms preference from the database
2014-
match db.get_preference::<ShinkaiInternalComms>("internal_comms") {
2015-
Ok(Some(internal_comms)) => {
2016-
let _ = res.send(Ok(internal_comms.internal_has_sync_default_tools)).await;
2017-
}
2018-
Ok(None) => {
2019-
let _ = res.send(Ok(false)).await;
2020-
}
2014+
let internal_comms_synced = match db.get_preference::<ShinkaiInternalComms>("internal_comms") {
2015+
Ok(Some(internal_comms)) => internal_comms.internal_has_sync_default_tools,
2016+
Ok(None) => false,
20212017
Err(e) => {
20222018
eprintln!("Error getting internal_comms preference: {}", e);
2023-
let _ = res.send(Ok(false)).await;
2019+
false
20242020
}
2025-
}
2021+
};
2022+
2023+
// Check if Rust tools are installed
2024+
let rust_tools_installed = match db.has_rust_tools() {
2025+
Ok(installed) => installed,
2026+
Err(e) => {
2027+
eprintln!("Error checking Rust tools: {}", e);
2028+
false
2029+
}
2030+
};
2031+
2032+
// Both conditions must be true
2033+
let _ = res.send(Ok(internal_comms_synced && rust_tools_installed)).await;
20262034
Ok(())
20272035
}
20282036

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use shinkai_message_primitives::{
4040
},
4141
};
4242
use shinkai_sqlite::{errors::SqliteManagerError, SqliteManager};
43+
use rusqlite::Error as RusqliteError;
4344
use shinkai_tools_primitives::tools::{
4445
deno_tools::DenoTool,
4546
error::ToolError,
@@ -164,7 +165,7 @@ impl Node {
164165
let vector_start_time = Instant::now();
165166

166167
// Use different search method based on whether we have allowed_tools
167-
let vector_search_result = if let Some(tools) = allowed_tools {
168+
let vector_search_res = if let Some(tools) = allowed_tools {
168169
// First generate the embedding from the query
169170
let embedding = db
170171
.generate_embeddings(&sanitized_query)
@@ -186,10 +187,24 @@ impl Node {
186187

187188
// Start the timer for FTS search
188189
let fts_start_time = Instant::now();
189-
let fts_search_result = db.search_tools_fts(&sanitized_query);
190+
let fts_search_res = db.search_tools_fts(&sanitized_query);
190191
let fts_elapsed_time = fts_start_time.elapsed();
191192
println!("Time taken for FTS search: {:?}", fts_elapsed_time);
192193

194+
let vector_search_result = match vector_search_res {
195+
Ok(v) => Ok(v),
196+
Err(SqliteManagerError::ToolNotFound(_))
197+
| Err(SqliteManagerError::DatabaseError(RusqliteError::QueryReturnedNoRows)) => Ok(Vec::new()),
198+
Err(e) => Err(e),
199+
};
200+
201+
let fts_search_result = match fts_search_res {
202+
Ok(v) => Ok(v),
203+
Err(SqliteManagerError::ToolNotFound(_))
204+
| Err(SqliteManagerError::DatabaseError(RusqliteError::QueryReturnedNoRows)) => Ok(Vec::new()),
205+
Err(e) => Err(e),
206+
};
207+
193208
match (vector_search_result, fts_search_result) {
194209
(Ok(vector_tools), Ok(fts_tools)) => {
195210
let mut combined_tools = Vec::new();

shinkai-bin/shinkai-node/src/tools/tool_execution/execution_custom.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub async fn try_to_execute_rust_tool(
3737
) -> Result<Value, ToolError> {
3838
println!("[executing_rust_tool] {}", tool_router_key);
3939

40+
// Note(Important): if you update the # of tools, you need to update the # of tools in fn has_rust_tools(&self) ->
41+
// Result<bool, SqliteManagerError> { ... } in shinkai_sqlite/src/shinkai_tool_manager.rs
42+
4043
let result = match tool_router_key {
4144
// TODO Keep in sync with definitions_custom.rs
4245
s if s == "local:::__official_shinkai:::shinkai_llm_map_reduce_processor" => {

shinkai-bin/shinkai-node/tests/it/tool_config_override_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn test_tool_execution_with_config_override() {
4444
std::env::set_var("WELCOME_MESSAGE", "false");
4545
let api_key_bearer = std::env::var("API_V2_KEY").unwrap_or_else(|_| "my_api_v2_key".to_string());
4646
std::env::set_var("API_V2_KEY", api_key_bearer.clone());
47-
std::env::set_var("NODE_API_PORT", "9550");
47+
std::env::set_var("NODE_API_PORT", "9570");
4848
std::env::set_var("SKIP_IMPORT_FROM_DIRECTORY", "true");
4949
std::env::set_var("IS_TESTING", "1");
5050
let node1_db_path = format!("db_tests/{}", hash_signature_public_key(&unsafe_deterministic_signature_keypair(0).1));
@@ -147,10 +147,10 @@ fn test_tool_execution_with_config_override() {
147147
.await;
148148

149149
// Create node1 and node2
150-
assert!(port_is_available(9550), "Port 9550 is not available");
150+
assert!(port_is_available(9570), "Port 9570 is not available");
151151
assert!(port_is_available(9560), "Port 9560 is not available");
152152
// Setup API Server task
153-
let api_listen_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9550);
153+
let api_listen_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9570);
154154
let api_https_listen_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9560);
155155

156156
let node1_commands_sender_clone = node1_commands_sender.clone();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { assertEquals } from "https://deno.land/std/assert/mod.ts";
2+
import { run } from "./getIdentityDataImpl.ts";
3+
4+
Deno.test(
5+
"getIdentityDataImpl - should return identity data for a valid identity",
6+
async () => {
7+
console.log("Current location:", Deno.cwd());
8+
const result = await run(
9+
{
10+
rpc_urls: [
11+
"https://base-sepolia.blockpi.network/v1/rpc/public",
12+
"https://sepolia.base.org",
13+
"https://base-sepolia-rpc.publicnode.com",
14+
"https://base-sepolia.gateway.tenderly.co",
15+
],
16+
contract_address: "0x425Fb20ba3874e887336aAa7f3fab32D08135BA9",
17+
contract_abi: await Deno.readTextFile(
18+
"shinkai-libs/shinkai-crypto-identities/src/abi/ShinkaiRegistry.sol/ShinkaiRegistry.json"
19+
),
20+
timeout_rpc_request_ms: 5000,
21+
},
22+
{
23+
identityId: "official.sep-shinkai",
24+
}
25+
);
26+
27+
assertEquals(result, {
28+
identityData: {
29+
boundNft: "4n",
30+
stakedTokens: "165000000000000000000n",
31+
encryptionKey:
32+
"9d89af22de24fcc621ed47a08e98f1c52fada3e49b98462cb02c48237940c85b",
33+
signatureKey:
34+
"1ffbfa5d90e7b79b395d034f81ec07ea0c7eabd6c9a510014173c6e5081411d1",
35+
routing: true,
36+
addressOrProxyNodes: [],
37+
delegatedTokens: "0n",
38+
lastUpdated: 1715000000,
39+
},
40+
});
41+
}
42+
);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { ethers, FetchRequest } from "npm:ethers@6.14.1";
2+
3+
type Configurations = {
4+
rpc_urls: string[];
5+
contract_address: string;
6+
contract_abi: string;
7+
timeout_rpc_request_ms: number;
8+
};
9+
10+
type Parameters = {
11+
identityId: string;
12+
};
13+
14+
type IdentityData = [
15+
boundNft: string,
16+
stakedTokens: number,
17+
encryptionKey: string,
18+
signatureKey: string,
19+
routing: boolean,
20+
addressOrProxyNodes: string[],
21+
delegatedTokens: number,
22+
lastUpdated: number
23+
];
24+
25+
export async function run(
26+
configurations: Configurations,
27+
parameters: Parameters
28+
) {
29+
let identityData: IdentityData | null = null;
30+
31+
for (const url of configurations.rpc_urls) {
32+
const rpcRequest = new FetchRequest(url);
33+
rpcRequest.timeout = configurations.timeout_rpc_request_ms;
34+
const provider = new ethers.JsonRpcProvider(rpcRequest);
35+
36+
const contract = new ethers.Contract(
37+
configurations.contract_address,
38+
configurations.contract_abi,
39+
provider
40+
);
41+
console.log("trying to call getIdentityData", parameters.identityId);
42+
try {
43+
const identityDataResult = await contract.getIdentityData(
44+
parameters.identityId
45+
);
46+
if (identityDataResult) {
47+
const [
48+
boundNft,
49+
stakedTokens,
50+
encryptionKey,
51+
signatureKey,
52+
routing,
53+
addressOrProxyNodes,
54+
delegatedTokens,
55+
lastUpdated,
56+
] = identityDataResult;
57+
58+
// Default data is equivalent to unexisting identity
59+
if (
60+
boundNft == 0 &&
61+
stakedTokens == 0 &&
62+
encryptionKey === "" &&
63+
signatureKey === "" &&
64+
!routing &&
65+
addressOrProxyNodes.length === 0 &&
66+
delegatedTokens == 0 &&
67+
lastUpdated == 0
68+
) {
69+
console.log("identityData is empty");
70+
continue;
71+
}
72+
73+
identityData = {
74+
boundNft: boundNft.toString() + "n",
75+
stakedTokens: stakedTokens.toString() + "n",
76+
encryptionKey,
77+
signatureKey,
78+
routing,
79+
addressOrProxyNodes,
80+
delegatedTokens: delegatedTokens.toString() + "n",
81+
lastUpdated: parseInt(lastUpdated.toString()),
82+
};
83+
console.log("identityData", identityData);
84+
break;
85+
} else {
86+
console.log("identityData is empty");
87+
}
88+
} catch (error) {
89+
console.log(`getIdentityData failed for rpc:${url} with error:${error}`);
90+
}
91+
}
92+
return { identityData };
93+
}

0 commit comments

Comments
 (0)