ferrox 0.1.0

A Rust framework for building AI agents for hyperliquid and solana
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
use ferrox_actions::{AgentState, ConfirmHandler, FunctionAction};
use ferrox_openai_api::{
    completions::Client as OpenAIClient,
    models::{FunctionDefinition, Message, Model, Tool},
};
use std::{
    collections::HashMap,
    future::Future,
    pin::Pin,
    sync::{Arc, Mutex},
};

use super::Agent;

#[derive(Clone)]
pub struct TextAgent<S, T>
where
    S: Send + Sync + Clone + 'static,
    T: Agent + Send + Sync + 'static,
{
    pub inner_agent: T,
    pub system_prompt: String,
    pub open_ai_client: OpenAIClient,
    conversation_history: Arc<Mutex<HashMap<String, Vec<Message>>>>,
    actions: Arc<Mutex<Vec<Arc<FunctionAction<S>>>>>,
    state: AgentState<S>,
}

impl<S, T> TextAgent<S, T>
where
    S: Send + Sync + Clone + 'static,
    T: Agent + Send + Sync + 'static,
{
    pub fn new(
        inner_agent: T,
        system_prompt: String,
        api_key: String,
        model: Model,
        state: S,
    ) -> Self {
        Self {
            inner_agent,
            system_prompt,
            open_ai_client: OpenAIClient::new(api_key, model),
            conversation_history: Arc::new(Mutex::new(HashMap::new())),
            actions: Arc::new(Mutex::new(Vec::new())),
            state: Arc::new(tokio::sync::Mutex::new(state)),
        }
    }

    fn send_prompt(
        &self,
        prompt: &str,
        history_id: &str,
        send_state: serde_json::Value,
    ) -> Pin<
        Box<
            dyn Future<
                    Output = Result<
                        (String, Option<(serde_json::Value, ConfirmHandler<S>)>),
                        String,
                    >,
                > + Send
                + Sync,
        >,
    > {
        println!("Sending prompt: {:?}", prompt);
        // Clone what we need for the async block
        let conversation_history = self.conversation_history.clone();
        let system_prompt = self.system_prompt.clone();
        let state = self.state.clone();
        let open_ai_client = self.open_ai_client.clone();
        let actions = self.actions.clone();
        let history_id = history_id.to_string();
        let prompt = prompt.to_string();

        Box::pin(async move {
            // Get or create conversation history
            let mut conversation = {
                let mut history_map = conversation_history.lock().map_err(|e| e.to_string())?;
                if let Some(existing_history) = history_map.get(&history_id) {
                    existing_history.clone()
                } else {
                    let new_history = vec![Message {
                        role: "system".to_string(),
                        content: Some(system_prompt),
                        tool_calls: None,
                        tool_call_id: None,
                    }];
                    history_map.insert(history_id.to_string(), new_history.clone());
                    new_history
                }
            };

            // Add user's prompt to conversation
            conversation.push(Message {
                role: "user".to_string(),
                content: Some(prompt.clone()),
                tool_calls: None,
                tool_call_id: None,
            });

            // Convert actions to OpenAI tools
            let tools: Vec<Tool> = {
                let actions = actions.lock().map_err(|e| e.to_string())?;
                actions
                    .iter()
                    .map(|action| {
                        let definition = action.definition();
                        Tool {
                            tool_type: "function".to_string(),
                            function: FunctionDefinition {
                                name: definition.name,
                                description: definition.description,
                                parameters: serde_json::json!({
                                    "type": "object",
                                    "properties": definition.parameters.clone().into_iter().map(|param| {
                                        (param.name, serde_json::json!({
                                            "type": param.param_type,
                                            "description": param.description,
                                    }))
                                    }).collect::<serde_json::Map<String, serde_json::Value>>(),
                                    "required": definition.parameters.clone().into_iter()
                                        .filter(|p| p.required)
                                        .map(|p| p.name.clone())
                                        .collect::<Vec<String>>(),
                                    "additionalProperties": false,
                                }),
                            },
                        }
                    })
                    .collect()
            };

            let mut final_result = String::new();
            let mut prev_result: String = String::new();
            let mut confirm_handler: Option<ConfirmHandler<S>> = None;
            let mut count = 0;
            while count <= 5 {
                let response = open_ai_client
                    .send_prompt_with_tools(
                        if count == 0 {
                            Some(prompt.clone())
                        } else {
                            None
                        },
                        conversation.clone(),
                        tools.clone(),
                    )
                    .await
                    .map_err(|e| e.to_string())?;

                if !response.tool_call {
                    final_result = response.content;
                    break;
                }

                let tool_calls: Vec<ferrox_openai_api::models::ToolCall> =
                    serde_json::from_str(&response.content).map_err(|e| e.to_string())?;

                // Add assistant's tool calls to conversation
                conversation.push(Message {
                    role: "assistant".to_string(),
                    content: None,
                    tool_calls: Some(tool_calls.clone()),
                    tool_call_id: None,
                });

                // Execute each tool
                let actions = {
                    let actions = actions.lock().map_err(|e| e.to_string())?;
                    let actions_vec = actions.clone();
                    drop(actions);
                    actions_vec
                };
                for tool_call in tool_calls {
                    if let Some(action) = actions
                        .iter()
                        .find(|a| a.definition().name == tool_call.function.name)
                    {
                        let result = action
                            .execute(
                                serde_json::from_str(&tool_call.function.arguments)
                                    .map_err(|e| e.to_string())?,
                                send_state.clone(),
                                state.clone(),
                            )
                            .await
                            .map_err(|e| {
                                format!("Failed to execute {}: {}", tool_call.function.name, e)
                            });
                        println!("Executed function {}", tool_call.function.name);
                        let result = match result {
                            Ok(result) => result,
                            Err(e) => {
                                println!(
                                    "LLM called the function but failed to execute {}: {}",
                                    tool_call.function.name, e
                                );
                                e.to_string()
                            }
                        };
                        prev_result = result.clone();
                        confirm_handler = action.confirm_handler.clone();
                        conversation.push(Message {
                            role: "tool".to_string(),
                            content: Some(result),
                            tool_calls: None,
                            tool_call_id: Some(tool_call.id),
                        });
                    }
                }
                count += 1;
            }

            // Update conversation history
            {
                let mut history_map = conversation_history.lock().map_err(|e| e.to_string())?;
                // Add final assistant message
                conversation.push(Message {
                    role: "assistant".to_string(),
                    content: Some(final_result.clone()),
                    tool_calls: None,
                    tool_call_id: None,
                });
                // Update the history
                history_map.insert(history_id.to_string(), conversation);
            }

            if count == 5 {
                return Err(
                    "Failed to get a final response from the AI agent within 5 rounds".to_string(),
                );
            }
            Ok((
                final_result,
                confirm_handler
                    .map(|handler| (serde_json::from_str(&prev_result).unwrap(), handler)),
            ))
        })
    }
}

impl<S, T> Agent<S> for TextAgent<S, T>
where
    S: Send + Sync + Clone + 'static,
    T: Agent + Send + Sync + 'static,
{
    fn add_action(&mut self, action: Arc<FunctionAction<S>>) {
        println!("Adding action: {:?}", action.definition().name);
        self.actions.lock().unwrap().push(action);
    }

    fn system_prompt(&self) -> &str {
        &self.system_prompt
    }

    fn state(&self) -> AgentState<S> {
        self.state.clone()
    }

    fn process_prompt(
        &self,
        prompt: &str,
        history_id: &str,
        send_state: serde_json::Value,
    ) -> Pin<
        Box<
            dyn Future<
                    Output = Result<
                        (String, Option<(serde_json::Value, ConfirmHandler<S>)>),
                        String,
                    >,
                > + Send
                + Sync,
        >,
    > {
        let history_id = history_id.to_string();
        let text_future = self.send_prompt(prompt, &history_id, send_state.clone());
        let inner_agent = self.inner_agent.clone();
        Box::pin(async move {
            let (text_result, confirm_option) = text_future.await?;
            let (text_result, _) = inner_agent
                .process_prompt(&text_result, &history_id, send_state)
                .await?;
            Ok((text_result, confirm_option))
        })
    }
}

//Tests remain the same but need to be updated to use ActionBuilder instead of MockAction
//For these tests make sure to set the OPENAI_API_KEY environment variable
#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::NullAgent;
    use ferrox_actions::{ActionBuilder, EmptyParams};
    use ferrox_openai_api::models::OpenAIModel;
    use serde::Deserialize;
    use std::env;

    #[derive(Clone, Debug, Default)]
    struct TestState {
        counter: i32,
    }

    #[tokio::test]
    async fn test_text_agent_with_actions() {
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

        let mut agent = TextAgent::<TestState, NullAgent>::new(
            NullAgent::default(),
            "You are a helpful assistant that can perform calculations, generate greetings, and reverse text. \
             Please use the appropriate action when needed.".to_string(),
            api_key,
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            TestState { counter: 0 },
        );

        {
            #[derive(Deserialize, Debug)]
            struct CalcParams {
                a: f64,
                b: f64,
                operation: String,
            }
            async fn calculator(
                params: CalcParams,
                _send_state: serde_json::Value,
                state: AgentState<TestState>,
            ) -> Result<String, String> {
                println!("Calculator called with params: {:?}", params);
                let result = match params.operation.as_str() {
                    "add" => params.a + params.b,
                    "subtract" => params.a - params.b,
                    "multiply" => params.a * params.b,
                    "divide" => {
                        if params.b == 0.0 {
                            return Err("Division by zero".to_string());
                        }
                        params.a / params.b
                    }
                    _ => return Err("Invalid operation".to_string()),
                };
                state.lock().await.counter += 1;
                Ok(result.to_string())
            }
            let calc_action = ActionBuilder::<_, _, _, _>::new("calculator", calculator, None)
                .description("Perform basic arithmetic operations")
                .parameter("a", "First number", "number", true)
                .parameter("b", "Second number", "number", true)
                .parameter(
                    "operation",
                    "Operation to perform (add/subtract/multiply/divide)",
                    "string",
                    true,
                )
                .build();
            agent.add_action(Arc::new(calc_action));
            println!("Added calculator action");
        }

        {
            #[derive(Deserialize, Debug)]
            struct GreetParams {
                name: String,
                language: Option<String>,
            }
            async fn greeter(
                params: GreetParams,
                send_state: serde_json::Value,
                state: AgentState<TestState>,
            ) -> Result<String, String> {
                println!("Greeter called with params: {:?}", params);
                let greeting = match params.language.as_deref() {
                    Some("es") => "¡Hola",
                    Some("fr") => "Bonjour",
                    _ => "Hello",
                };
                state.lock().await.counter += 1;
                Ok(format!("{} {}!", greeting, params.name))
            }
            let greet_action = ActionBuilder::<_, _, _, _>::new("greeter", greeter, None)
                .description("Generate a greeting message")
                .parameter("name", "Name to greet", "string", true)
                .parameter("language", "Language code (en/es/fr)", "string", false)
                .build();
            agent.add_action(Arc::new(greet_action));
            println!("Added greeter action");
        }

        {
            #[derive(Deserialize, Debug)]
            struct ReverseParams {
                text: String,
            }

            async fn reverser(
                params: ReverseParams,
                send_state: serde_json::Value,
                state: AgentState<TestState>,
            ) -> Result<String, String> {
                println!("Reverser called with params: {:?}", params);
                state.lock().await.counter += 1;
                Ok(params.text.chars().rev().collect())
            }
            let reverse_action = ActionBuilder::<_, _, _, _>::new("reverser", reverser, None)
                .description("Reverse input text")
                .parameter("text", "Text to reverse", "string", true)
                .build();
            agent.add_action(Arc::new(reverse_action));
            println!("Added reverser action");
        }

        // Test individual actions through the agent
        println!("--------------------------------");
        let calc_prompt = "Calculate 5 plus 3";
        println!("Testing calculator with prompt: {}", calc_prompt);
        let (calc_response, _) = agent
            .process_prompt(calc_prompt, "test1", serde_json::Value::Null)
            .await
            .unwrap();
        println!("Calculator response: {}", calc_response);
        assert_eq!(agent.state().lock().await.counter, 1);

        println!("--------------------------------");
        let greet_prompt = "Say hello to Alice in Spanish";
        println!("Testing greeter with prompt: {}", greet_prompt);
        let (greet_response, _) = agent
            .process_prompt(greet_prompt, "test2", serde_json::Value::Null)
            .await
            .unwrap();
        println!("Greeter response: {}", greet_response);
        assert_eq!(agent.state().lock().await.counter, 2);

        println!("--------------------------------");
        let reverse_prompt = "Reverse the text 'hello world'";
        println!("Testing reverser with prompt: {}", reverse_prompt);
        let (reverse_response, _) = agent
            .process_prompt(reverse_prompt, "test3", serde_json::Value::Null)
            .await
            .unwrap();
        println!("Reverser response: {}", reverse_response);
        assert_eq!(agent.state().lock().await.counter, 3);

        // Test chained actions
        println!("--------------------------------");
        let chained_prompt = "Calculate 10 plus 5, then greet the result in Spanish, and finally reverse that greeting";
        println!("Testing chained actions with prompt: {}", chained_prompt);
        let (chained_response, _) = agent
            .process_prompt(chained_prompt, "test4", serde_json::Value::Null)
            .await
            .unwrap();
        println!("Chained actions response: {}", chained_response);
        assert_eq!(agent.state().lock().await.counter, 6); // Should have used all 3 actions
    }

    // Keep the existing conversation tests
    #[tokio::test]
    async fn test_text_agent_conversation() {
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

        let agent = TextAgent::<_, NullAgent>::new(
            NullAgent::default(),
            "You are a helpful assistant that provides concise responses.".to_string(),
            api_key,
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            (),
        );

        // Test first message
        let (response, _) = agent
            .process_prompt(
                "What is Rust programming language?",
                "default",
                serde_json::Value::Null,
            )
            .await
            .expect("Failed to get response");

        println!("First response: {}", response);
        assert!(!response.is_empty());

        // Test follow-up question (should maintain context)
        let (response, _) = agent
            .process_prompt(
                "What are its main features?",
                "default",
                serde_json::Value::Null,
            )
            .await
            .expect("Failed to get response");

        println!("Follow-up response: {}", response);
        assert!(!response.is_empty());

        // Verify conversation history
        let history = agent.conversation_history.lock().unwrap();
        let default_history = history
            .get("default")
            .expect("No conversation history found");

        assert_eq!(default_history[0].role, "system");
        assert_eq!(default_history[1].role, "user");
        assert_eq!(default_history[2].role, "assistant");
        assert_eq!(default_history[3].role, "user");
        assert_eq!(default_history[4].role, "assistant");
    }

    #[tokio::test]
    async fn test_text_agent_multiple_conversations() {
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

        let agent = TextAgent::<_, NullAgent>::new(
            NullAgent::default(),
            "You are a helpful assistant.".to_string(),
            api_key,
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            (),
        );

        // Test sending prompts with different conversation IDs
        let send_prompt = |id: &str, prompt: &str| {
            let agent = &agent;
            let id = id.to_string();
            let prompt = prompt.to_string();
            async move {
                agent
                    .process_prompt(&prompt, &id, serde_json::Value::Null)
                    .await
                    .expect("Failed to get response")
            }
        };

        let ((response1, _), (response2, _)) = tokio::join!(
            send_prompt("conv1", "Tell me about Python"),
            send_prompt("conv2", "Tell me about JavaScript")
        );

        println!("Python response: {}", response1);
        println!("JavaScript response: {}", response2);

        // Verify separate conversation histories
        let history = agent.conversation_history.lock().unwrap();

        let conv1 = history
            .get("conv1")
            .expect("No conversation history for conv1");
        assert_eq!(conv1[0].role, "system");
        assert_eq!(conv1[1].role, "user");
        assert_eq!(conv1[1].content, Some("Tell me about Python".to_string()));

        let conv2 = history
            .get("conv2")
            .expect("No conversation history for conv2");
        assert_eq!(conv2[0].role, "system");
        assert_eq!(conv2[1].role, "user");
        assert_eq!(
            conv2[1].content,
            Some("Tell me about JavaScript".to_string())
        );
    }

    #[tokio::test]
    async fn test_chained_text_agents() {
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

        // Create inner agent that adds markdown formatting
        let inner_agent = TextAgent::<_, NullAgent>::new(
            NullAgent::default(),
            "You are a formatting assistant. Your job is to take any text and format it as a markdown quote with emoji bullets. \
             Always format your response like this:\
             \n> 🔹 First point\
             \n> 🔸 Second point\
             \n> 💠 Final point"
                .to_string(),
            api_key.clone(),
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            (),
        );

        // Create outer agent that generates content
        let agent = TextAgent::new(
            inner_agent,
            "You are a helpful assistant that explains technical concepts. \
             Break down your explanations into at least 3 key points."
                .to_string(),
            api_key,
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            (),
        );

        // Test the chain
        let (response, _) = agent
            .process_prompt(
                "What is Rust's ownership system?",
                "test_chain",
                serde_json::Value::Null,
            )
            .await
            .expect("Failed to get response");

        println!("Chained response:\n{}", response);

        // Verify the response has the inner agent's formatting
        assert!(response.contains(">"));
        assert!(response.contains("🔹"));
        assert!(response.contains("🔸"));
        assert!(response.contains("💠"));
    }

    #[tokio::test]
    async fn test_empty_params_action() {
        let api_key = env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set");

        let mut agent = TextAgent::<TestState, NullAgent>::new(
            NullAgent::default(),
            "You are a helpful assistant that can get the current time. Please use the time action when asked about the current time."
                .to_string(),
            api_key,
            Model::OpenAI(OpenAIModel::GPT35Turbo),
            TestState { counter: 0 },
        );

        // Create action that takes EmptyParams
        async fn get_time(
            _params: EmptyParams,
            _send_state: serde_json::Value,
            state: AgentState<TestState>,
        ) -> Result<String, String> {
            println!("get_time called. Params: {:?}", _params);
            state.lock().await.counter += 1;
            Ok("12:00 PM".to_string())
        }

        let time_action = ActionBuilder::<_, _, _, _>::new("get_time", get_time, None)
            .description("Get the current time")
            .build();

        agent.add_action(Arc::new(time_action));

        // Test the action
        let (response, _) = agent
            .process_prompt("What time is it?", "test_empty", serde_json::Value::Null)
            .await
            .unwrap();

        println!("Time response: {}", response);

        // Verify the action was called by checking the counter
        assert_eq!(agent.state().lock().await.counter, 1);

        // Response should contain the time
        assert!(response.contains("12:00 PM"));
    }
}