pub struct EndpointBuilder { /* private fields */ }Expand description
Builder for creating custom endpoints with an ergonomic API.
Implementations§
Source§impl EndpointBuilder
impl EndpointBuilder
Sourcepub fn get(path: impl Into<String>) -> Self
pub fn get(path: impl Into<String>) -> Self
Creates a new GET endpoint builder.
Examples found in repository?
examples/serve_simple_endpoints.rs (line 49)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn post(path: impl Into<String>) -> Self
pub fn post(path: impl Into<String>) -> Self
Creates a new POST endpoint builder.
Examples found in repository?
examples/serve_simple_endpoints.rs (line 59)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn put(path: impl Into<String>) -> Self
pub fn put(path: impl Into<String>) -> Self
Creates a new PUT endpoint builder.
Examples found in repository?
examples/serve_simple_endpoints.rs (line 83)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn json(self, response: Value) -> Self
pub fn json(self, response: Value) -> Self
Sets a static JSON response for the endpoint. This is the simplest way to create an endpoint that returns fixed data.
Examples found in repository?
examples/serve_simple_endpoints.rs (lines 50-54)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn handle<F>(self, handler: F) -> Self
pub fn handle<F>(self, handler: F) -> Self
Sets a handler function that receives request data and returns a response. This allows for dynamic responses based on query params, path params, and body.
Examples found in repository?
examples/serve_simple_endpoints.rs (lines 60-70)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Sets a description for the endpoint (for documentation purposes).
Examples found in repository?
examples/serve_simple_endpoints.rs (line 55)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Sourcepub fn build(self) -> CustomEndpoint
pub fn build(self) -> CustomEndpoint
Builds the endpoint.
Examples found in repository?
examples/serve_simple_endpoints.rs (line 56)
9async fn main() -> helios_engine::Result<()> {
10 // Initialize tracing
11 tracing_subscriber::fmt()
12 .with_max_level(tracing::Level::INFO)
13 .init();
14
15 // Load configuration
16 let config = Config::from_file("config.toml")?;
17
18 // Create an agent with tools
19 let agent = Agent::builder("API Agent")
20 .config(config)
21 .system_prompt("You are a helpful AI assistant with access to a calculator tool.")
22 .tool(Box::new(CalculatorTool))
23 .max_iterations(5)
24 .build()
25 .await?;
26
27 // Create custom endpoints using the new simplified API
28 println!("🎉 Creating custom endpoints with the new simplified API!\n");
29
30 // Method 1: Super simple static endpoints
31 let version_endpoint = helios_engine::get(
32 "/api/version",
33 serde_json::json!({
34 "version": "0.4.4",
35 "service": "Helios Engine",
36 "features": ["agents", "tools", "streaming", "custom_endpoints"]
37 }),
38 );
39
40 let status_endpoint = helios_engine::get(
41 "/api/status",
42 serde_json::json!({
43 "status": "operational",
44 "model": "agent-based"
45 }),
46 );
47
48 // Method 2: Using the builder pattern for more control
49 let info_endpoint = EndpointBuilder::get("/api/info")
50 .json(serde_json::json!({
51 "name": "Helios Engine API",
52 "description": "AI Agent Server with Custom Endpoints",
53 "documentation": "https://helios-engine.vercel.app/"
54 }))
55 .description("API information endpoint")
56 .build();
57
58 // Method 3: Dynamic responses with a handler function
59 let echo_endpoint = EndpointBuilder::post("/api/echo")
60 .handle(|req| {
61 let message = req
62 .and_then(|r| r.body)
63 .and_then(|b| b.get("message").cloned())
64 .unwrap_or_else(|| serde_json::json!("No message provided"));
65
66 helios_engine::EndpointResponse::ok(serde_json::json!({
67 "echo": message,
68 "timestamp": chrono::Utc::now().to_rfc3339()
69 }))
70 })
71 .description("Echo endpoint that returns your message")
72 .build();
73
74 // Method 4: Different HTTP methods
75 let create_endpoint = EndpointBuilder::post("/api/create")
76 .json(serde_json::json!({
77 "message": "Resource created",
78 "id": "12345"
79 }))
80 .description("Simulates creating a resource")
81 .build();
82
83 let update_endpoint = EndpointBuilder::put("/api/update")
84 .json(serde_json::json!({
85 "message": "Resource updated"
86 }))
87 .description("Simulates updating a resource")
88 .build();
89
90 let delete_endpoint = helios_engine::delete(
91 "/api/delete",
92 serde_json::json!({
93 "message": "Resource deleted"
94 }),
95 );
96
97 // Collect all endpoints into a vector
98 let custom_endpoints = vec![
99 version_endpoint,
100 status_endpoint,
101 info_endpoint,
102 echo_endpoint,
103 create_endpoint,
104 update_endpoint,
105 delete_endpoint,
106 ];
107
108 // Start the server with the new ServerBuilder API
109 println!("🚀 Starting server with custom endpoints...\n");
110 println!("📡 OpenAI-compatible API endpoints:");
111 println!(" POST /v1/chat/completions");
112 println!(" GET /v1/models");
113 println!("\n📡 Custom endpoints:");
114 println!(" GET /api/version");
115 println!(" GET /api/status");
116 println!(" GET /api/info");
117 println!(" POST /api/echo");
118 println!(" POST /api/create");
119 println!(" PUT /api/update");
120 println!(" DELETE /api/delete");
121 println!("\n💡 Try these commands:");
122 println!(" curl http://127.0.0.1:8000/api/version");
123 println!(" curl http://127.0.0.1:8000/api/status");
124 println!(" curl -X POST http://127.0.0.1:8000/api/echo \\");
125 println!(" -H 'Content-Type: application/json' \\");
126 println!(" -d '{{\"message\": \"Hello, Helios!\"}}'");
127 println!();
128
129 // Method 1: Pass a vector of endpoints (recommended)
130 ServerBuilder::with_agent(agent, "local-model")
131 .address("127.0.0.1:8000")
132 .endpoints(custom_endpoints)
133 .serve()
134 .await?;
135
136 // Method 2: Alternative - you can also use individual .endpoint() calls
137 // ServerBuilder::with_agent(agent, "local-model")
138 // .address("127.0.0.1:8000")
139 // .endpoint(version_endpoint)
140 // .endpoint(status_endpoint)
141 // // ... etc
142 // .serve()
143 // .await?;
144
145 Ok(())
146}Auto Trait Implementations§
impl Freeze for EndpointBuilder
impl !RefUnwindSafe for EndpointBuilder
impl Send for EndpointBuilder
impl Sync for EndpointBuilder
impl Unpin for EndpointBuilder
impl !UnwindSafe for EndpointBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more