solagent_rig_solana/
get_tps.rs

1// Copyright 2025 zTgx
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16use solagent_core::{
17    rig::{
18        completion::ToolDefinition,
19        tool::{Tool, ToolEmbedding},
20    },
21    SolanaAgentKit,
22};
23use solagent_plugin_solana::get_tps;
24use std::sync::Arc;
25
26#[derive(Deserialize)]
27pub struct GetTpsArgs {}
28
29#[derive(Deserialize, Serialize)]
30pub struct GetTpsOutput {
31    pub tps: f64,
32}
33
34#[derive(Debug, thiserror::Error)]
35#[error("GetTps error")]
36pub struct GetTpsError;
37
38pub struct GetTps {
39    agent: Arc<SolanaAgentKit>,
40}
41
42impl GetTps {
43    pub fn new(agent: Arc<SolanaAgentKit>) -> Self {
44        GetTps { agent }
45    }
46}
47
48impl Tool for GetTps {
49    const NAME: &'static str = "get_tps";
50
51    type Error = GetTpsError;
52    type Args = GetTpsArgs;
53    type Output = GetTpsOutput;
54
55    async fn definition(&self, _prompt: String) -> ToolDefinition {
56        ToolDefinition {
57            name: "get_tps".to_string(),
58            description: r#"
59            
60            Get the current transactions per second (TPS) of the Solana network
61            
62            examples: [
63                [
64                    {
65                        input: {},
66                        output: {
67                            status: "success",
68                            tps: 3500,
69                            message: "Current network TPS: 3500",
70                        },
71                        explanation: "Get the current TPS of the Solana network",
72                    },
73                ],
74            ]
75            
76            "#
77            .to_string(),
78            parameters: serde_json::Value::Null,
79        }
80    }
81
82    async fn call(&self, _args: Self::Args) -> Result<Self::Output, Self::Error> {
83        let tps = get_tps(&self.agent).await.expect("get_tps");
84
85        Ok(GetTpsOutput { tps })
86    }
87}
88
89#[derive(Debug, thiserror::Error)]
90#[error("Init error")]
91pub struct InitError;
92
93impl ToolEmbedding for GetTps {
94    type InitError = InitError;
95    type Context = ();
96    type State = Arc<SolanaAgentKit>;
97
98    fn init(_state: Self::State, _context: Self::Context) -> Result<Self, Self::InitError> {
99        Ok(GetTps { agent: _state })
100    }
101
102    fn embedding_docs(&self) -> Vec<String> {
103        vec!["Get the current transactions per second (TPS) of the Solana network".into()]
104    }
105
106    fn context(&self) -> Self::Context {}
107}