Skip to content

Commit e2e33b7

Browse files
committed
cleanup
1 parent 8cd876f commit e2e33b7

File tree

6 files changed

+77
-41
lines changed

6 files changed

+77
-41
lines changed

src/api_handler/call_request.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ pub async fn call_gpt(messages: Vec<Message>) -> Result<String, Box<dyn std::err
99
dotenv().ok();
1010

1111
// Extract API Key information
12-
let api_key: String =
13-
env::var("OPEN_AI_KEY").expect("OPEN_AI_KEY not found in enviornment variables");
14-
let api_org: String =
15-
env::var("OPEN_AI_ORG").expect("OPEN_AI_ORG not found in enviornment variables");
12+
let api_key: String = env::var("OPEN_AI_KEY").expect("OPEN_AI_KEY not found in .env");
13+
let api_org: String = env::var("OPEN_AI_ORG").expect("OPEN_AI_ORG not found in .env");
1614

1715
// Confirm endpoint
1816
let url: &str = "https://api.openai.com/v1/chat/completions";
@@ -40,7 +38,6 @@ pub async fn call_gpt(messages: Vec<Message>) -> Result<String, Box<dyn std::err
4038
.build()
4139
.map_err(|e| -> Box<dyn std::error::Error + Send> { Box::new(e) })?;
4240

43-
4441
// println!("* ==============MESSAGE TO CALL GPT: {:?}", messages.clone());
4542
// Create chat completion
4643
let chat_completion: ChatCompletion = ChatCompletion {

src/helpers/command_lines.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ pub fn confirm_safe_code() -> bool {
7070
println!("[2] Stop project");
7171
stdout.execute(ResetColor).unwrap();
7272

73-
7473
//read use input
7574
let mut human_input = String::new();
76-
stdin().read_line(&mut human_input).expect("Failed to read user confirmation");
75+
stdin()
76+
.read_line(&mut human_input)
77+
.expect("Failed to read user confirmation");
7778

7879
// trim and make lowercase
7980
let human_response = human_input.trim().to_lowercase();
@@ -83,7 +84,7 @@ pub fn confirm_safe_code() -> bool {
8384
"1" | "ok" | "y" => return true,
8485
"2" | "no" | "n" => return false,
8586
_ => {
86-
println!("Invalid input. Please select one of the following");
87+
println!("Invalid input. Please select one of the following");
8788
}
8889
};
8990
}

src/helpers/general.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::fs;
77

88
const CODE_TEMPLATE_PATH: &str = r#"C:\Users\Anatolii Maltsev\Documents\Coding\Rust\Projects\RustAgent\web_template\src\web_server_code_template.rs"#;
99
const EXEC_MAIN_PATH: &str = r#"C:\Users\Anatolii Maltsev\Documents\Coding\Rust\Projects\RustAgent\web_template\src\main.rs"#;
10-
pub const WEB_SERVER_PROJECT_PATH: &str = r#"C:\Users\Anatolii Maltsev\Documents\Coding\Rust\Projects\RustAgent\web_template\"#;
10+
pub const WEB_SERVER_PROJECT_PATH: &str =
11+
r#"C:\Users\Anatolii Maltsev\Documents\Coding\Rust\Projects\RustAgent\web_template\"#;
1112
const API_SCHEMA_PATH: &str = r#"C:\Users\Anatolii Maltsev\Documents\Coding\Rust\Projects\RustAgent\auto_gpt_agent\schemas\api_schema.json"#;
1213

1314
// Extend ai function to encourage specific output

src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@ async fn main() {
2323
.expect("Error creating agent");
2424

2525
manage_agent.execute_project().await;
26-
27-
// dbg!(manage_agent);
2826
}

src/models/agents/agent_backend.rs

+66-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::io::{stdout, Stdout};
21
use crate::ai_functions::ai_func_backend::{
32
print_backend_webserver_code, print_fixed_code, print_improved_webserver_code,
43
print_rest_api_endpoints,
@@ -7,8 +6,9 @@ use crate::helpers::general::{
76
check_status_code, read_code_template_contents, read_exec_main_contents, save_api_endpoints,
87
save_backend_code,
98
};
9+
use std::io::{stdout, Stdout};
1010

11-
use crate::helpers::command_lines::{PrintCommand, confirm_safe_code};
11+
use crate::helpers::command_lines::{confirm_safe_code, PrintCommand};
1212
use crate::helpers::general::{ai_task_request, WEB_SERVER_PROJECT_PATH};
1313
use crate::models::agent_basic::basic_agent::{AgentState, BasicAgent};
1414
use crate::models::agents::agent_traits::{FactSheet, RouteObject, SpecialFunctions};
@@ -61,7 +61,7 @@ impl AgentBackendDeveloper {
6161
get_function_string!(print_backend_webserver_code),
6262
print_backend_webserver_code,
6363
)
64-
.await;
64+
.await;
6565
println!(
6666
"* ==============FIRST Backend(AI RESPONSE) code to Save: {:?}",
6767
&ai_response
@@ -86,7 +86,7 @@ impl AgentBackendDeveloper {
8686
get_function_string!(print_improved_webserver_code),
8787
print_improved_webserver_code,
8888
)
89-
.await;
89+
.await;
9090
println!(
9191
"* ==============SECOND (AI RESPONSE) Backend code to save: {:?}",
9292
&ai_response
@@ -108,7 +108,7 @@ impl AgentBackendDeveloper {
108108
get_function_string!(print_fixed_code),
109109
print_fixed_code,
110110
)
111-
.await;
111+
.await;
112112

113113
save_backend_code(&ai_response);
114114
factsheet.backend_code = Some(ai_response);
@@ -126,7 +126,7 @@ impl AgentBackendDeveloper {
126126
get_function_string!(print_rest_api_endpoints),
127127
print_rest_api_endpoints,
128128
)
129-
.await;
129+
.await;
130130

131131
ai_response
132132
}
@@ -138,7 +138,10 @@ impl SpecialFunctions for AgentBackendDeveloper {
138138
&self.attributes
139139
}
140140

141-
async fn execute(&mut self, fact_sheet: &mut FactSheet ) -> Result<(), Box<dyn std::error::Error>> {
141+
async fn execute(
142+
&mut self,
143+
fact_sheet: &mut FactSheet,
144+
) -> Result<(), Box<dyn std::error::Error>> {
142145
while self.attributes.state != AgentState::Finished {
143146
match &self.attributes.state {
144147
AgentState::Discovery => {
@@ -160,7 +163,9 @@ impl SpecialFunctions for AgentBackendDeveloper {
160163
AgentState::UnitTesting => {
161164
// API SAFETY GUARD
162165
PrintCommand::UnitTest.print_agent_message(
163-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Ensuring Safe Code");
166+
&self.attributes.position.as_str(),
167+
"Backend Code Unit Testing: Ensuring Safe Code",
168+
);
164169

165170
let user_confirmation: bool = confirm_safe_code();
166171

@@ -170,7 +175,9 @@ impl SpecialFunctions for AgentBackendDeveloper {
170175

171176
// Build and testing code
172177
PrintCommand::UnitTest.print_agent_message(
173-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Building web server...");
178+
&self.attributes.position.as_str(),
179+
"Backend Code Unit Testing: Building web server...",
180+
);
174181

175182
let build_backend_server: std::process::Output = Command::new("cargo")
176183
.arg("build")
@@ -184,7 +191,9 @@ impl SpecialFunctions for AgentBackendDeveloper {
184191
if build_backend_server.status.success() {
185192
self.bug_count = 0;
186193
PrintCommand::UnitTest.print_agent_message(
187-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Test server build successful...");
194+
&self.attributes.position.as_str(),
195+
"Backend Code Unit Testing: Test server build successful...",
196+
);
188197
} else {
189198
let error_arr: Vec<u8> = build_backend_server.stderr;
190199
let error_str = String::from_utf8(error_arr).unwrap();
@@ -194,7 +203,9 @@ impl SpecialFunctions for AgentBackendDeveloper {
194203

195204
if self.bug_count > 2 {
196205
PrintCommand::Issue.print_agent_message(
197-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Too many bugs found in code ");
206+
&self.attributes.position.as_str(),
207+
"Backend Code Unit Testing: Too many bugs found in code ",
208+
);
198209
panic!("ERROR: Too many bugs");
199210
}
200211

@@ -212,19 +223,27 @@ impl SpecialFunctions for AgentBackendDeveloper {
212223
let api_endpoints_str: String = self.call_extract_rest_api_endpoints().await;
213224

214225
// convert API endpoints into value
215-
let api_endpoints: Vec<RouteObject> = serde_json::from_str(api_endpoints_str.as_str()).expect("Failed to parse API endpoints");
226+
let api_endpoints: Vec<RouteObject> =
227+
serde_json::from_str(api_endpoints_str.as_str())
228+
.expect("Failed to parse API endpoints");
216229

217230
// define endpoints checks. Выбрать в итераторе только проверяемые ендпоинты
218-
let check_endpoints: Vec<RouteObject> = api_endpoints.iter()
219-
.filter(|&route_object| { route_object.method == "get" && route_object.is_route_dynamic == "false" })
220-
.cloned().collect();
231+
let check_endpoints: Vec<RouteObject> = api_endpoints
232+
.iter()
233+
.filter(|&route_object| {
234+
route_object.method == "get" && route_object.is_route_dynamic == "false"
235+
})
236+
.cloned()
237+
.collect();
221238

222239
// Store API endpoints
223240
fact_sheet.api_endpoint_schema = Some(check_endpoints.clone());
224241

225242
//Run backend application
226243
PrintCommand::UnitTest.print_agent_message(
227-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Starting Web server...");
244+
&self.attributes.position.as_str(),
245+
"Backend Code Unit Testing: Starting Web server...",
246+
);
228247

229248
let mut run_backend_server: std::process::Child = Command::new("cargo")
230249
.arg("run")
@@ -236,49 +255,68 @@ impl SpecialFunctions for AgentBackendDeveloper {
236255

237256
//Launching testing on the server
238257
PrintCommand::UnitTest.print_agent_message(
239-
&self.attributes.position.as_str(), "Backend Code Unit Testing: Launching test on server in 5 sec...");
258+
&self.attributes.position.as_str(),
259+
"Backend Code Unit Testing: Launching test on server in 5 sec...",
260+
);
240261

241262
//Sleep for 5 sec before call webserver
242263
let seconds_sleep: Duration = Duration::from_secs(5);
243264
time::sleep(seconds_sleep).await;
244265

245-
246266
// check endpoints against server
247267
for endpoint in check_endpoints {
248-
let test_message: String = format!("Testing endpoint: '{}...'", endpoint.route);
268+
let test_message: String =
269+
format!("Testing endpoint: '{}...'", endpoint.route);
249270
PrintCommand::UnitTest.print_agent_message(
250-
&self.attributes.position.as_str(), test_message.as_str());
271+
&self.attributes.position.as_str(),
272+
test_message.as_str(),
273+
);
251274

252275
//create client to call endpoints api
253276
let client: Client = Client::builder()
254277
.timeout(Duration::from_secs(5))
255-
.build().unwrap();
278+
.build()
279+
.unwrap();
256280

257281
// test url
258282
let url: String = format!("http://127.0.0.1:8080{}", endpoint.route);
259283
match check_status_code(&client, &url).await {
260284
Ok(status_code) => {
261285
if status_code != 200 {
262-
let error_msg: String = format!("WARNING: Failed to call backend endpoint{}", endpoint.route);
286+
let error_msg: String = format!(
287+
"WARNING: Failed to call backend endpoint{}",
288+
endpoint.route
289+
);
263290
PrintCommand::Issue.print_agent_message(
264-
&self.attributes.position.as_str(), error_msg.as_str());
291+
&self.attributes.position.as_str(),
292+
error_msg.as_str(),
293+
);
265294
}
266295
}
267296
Err(error_msg) => {
268297
//kill process
269-
run_backend_server.kill().expect("Unable to stop backend application");
270-
let error_msg: String = format!("ERROR: While checking backend{}", error_msg);
298+
run_backend_server
299+
.kill()
300+
.expect("Unable to stop backend application");
301+
let error_msg: String =
302+
format!("ERROR: While checking backend{}", error_msg);
271303
PrintCommand::Issue.print_agent_message(
272-
&self.attributes.position.as_str(), error_msg.as_str());
304+
&self.attributes.position.as_str(),
305+
error_msg.as_str(),
306+
);
273307
}
274308
}
275309
}
276310

277311
save_api_endpoints(&api_endpoints_str);
278312
PrintCommand::Success.print_agent_message(
279-
&self.attributes.position.as_str(), "Backend Testing completed...");
313+
&self.attributes.position.as_str(),
314+
"Backend Testing completed...",
315+
);
280316

281-
run_backend_server.kill().expect("failed to Kill server on completion");
317+
run_backend_server
318+
.kill()
319+
.expect("failed to Kill server on completion");
282320
self.attributes.state = AgentState::Finished;
283321
}
284322

src/models/agents_manager/managing_agents.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ManagingAgent {
3434
get_function_string!(convert_user_input_to_goal),
3535
convert_user_input_to_goal,
3636
)
37-
.await;
37+
.await;
3838

3939
let agents: Vec<Box<dyn SpecialFunctions>> = vec![];
4040

@@ -62,7 +62,8 @@ impl ManagingAgent {
6262
pub async fn execute_project(&mut self) {
6363
self.create_agents();
6464
for agent in &mut self.agents {
65-
let _: Result<(), Box<dyn std::error::Error>> = agent.execute(&mut self.fact_sheet).await;
65+
let _: Result<(), Box<dyn std::error::Error>> =
66+
agent.execute(&mut self.fact_sheet).await;
6667
}
6768
}
6869
}

0 commit comments

Comments
 (0)