helios-engine 0.5.5

A powerful and flexible Rust framework for building LLM-powered agents with tool support, both locally and online
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
# Custom Endpoints Guide

This guide explains how to create custom HTTP endpoints for your Helios Engine server using the new simplified API.

## Overview

Helios Engine allows you to add custom HTTP endpoints to your agent server, enabling you to expose additional functionality beyond the standard OpenAI-compatible API. The new endpoint builder API makes this incredibly simple and intuitive.

## Quick Start

Here's the simplest way to create a custom endpoint:

```rust
use helios_engine::{ServerBuilder, get};

// Create a simple GET endpoint
let endpoint = get("/api/status", serde_json::json!({
    "status": "ok",
    "uptime": "24h"
}));

// Add it to your server
ServerBuilder::with_agent(agent, "model-name")
    .endpoint(endpoint)
    .serve()
    .await?;
```

## Creating Endpoints

### Method 1: Simple Helper Functions (Recommended for Static Data)

The easiest way to create endpoints with static JSON responses:

```rust
use helios_engine::{get, post, put, delete, patch};

// GET endpoint
let version = get("/api/version", serde_json::json!({
    "version": "1.0.0",
    "service": "My API"
}));

// POST endpoint
let create = post("/api/create", serde_json::json!({
    "message": "Resource created"
}));

// PUT endpoint
let update = put("/api/update", serde_json::json!({
    "message": "Resource updated"
}));

// DELETE endpoint
let remove = delete("/api/delete", serde_json::json!({
    "message": "Resource deleted"
}));

// PATCH endpoint
let patch_resource = patch("/api/patch", serde_json::json!({
    "message": "Resource patched"
}));
```

### Method 2: Builder Pattern (For More Control)

Use the builder pattern when you need additional features:

```rust
use helios_engine::EndpointBuilder;

let endpoint = EndpointBuilder::get("/api/info")
    .json(serde_json::json!({
        "name": "My API",
        "description": "A helpful API"
    }))
    .description("API information endpoint")
    .build();
```

### Method 3: Dynamic Handlers (For Interactive Endpoints)

Create endpoints that respond dynamically to request data:

```rust
use helios_engine::{EndpointBuilder, EndpointResponse};

let echo = EndpointBuilder::post("/api/echo")
    .handle(|req| {
        // Extract message from request body
        let message = req
            .and_then(|r| r.body)
            .and_then(|b| b.get("message").cloned())
            .unwrap_or_else(|| serde_json::json!("No message"));

        // Return a dynamic response
        EndpointResponse::ok(serde_json::json!({
            "echo": message,
            "timestamp": chrono::Utc::now().to_rfc3339()
        }))
    })
    .description("Echo your message back")
    .build();
```

### Method 4: Using Response Helpers

Create responses with appropriate HTTP status codes:

```rust
use helios_engine::{EndpointBuilder, EndpointResponse};

let find_user = EndpointBuilder::get("/api/users/:id")
    .handle(|req| {
        // Check if user exists (simplified example)
        let user_exists = true;
        
        if user_exists {
            EndpointResponse::ok(serde_json::json!({
                "id": "123",
                "name": "John Doe"
            }))
        } else {
            EndpointResponse::not_found("User not found")
        }
    })
    .build();
```

Available response helpers:
- `EndpointResponse::ok(body)` - 200 OK
- `EndpointResponse::created(body)` - 201 CREATED
- `EndpointResponse::accepted(body)` - 202 ACCEPTED
- `EndpointResponse::bad_request(message)` - 400 BAD REQUEST
- `EndpointResponse::not_found(message)` - 404 NOT FOUND
- `EndpointResponse::internal_error(message)` - 500 INTERNAL SERVER ERROR
- `EndpointResponse::with_status(status, body)` - Custom status code

## Adding Endpoints to Your Server

### Option 1: Using a Vector (Recommended)

The cleanest way to add multiple endpoints:

```rust
use helios_engine::{ServerBuilder, get, post};

let endpoints = vec![
    get("/api/version", serde_json::json!({"version": "1.0"})),
    get("/api/status", serde_json::json!({"status": "ok"})),
    post("/api/echo", serde_json::json!({"message": "echo"})),
];

ServerBuilder::with_agent(agent, "model-name")
    .address("127.0.0.1:8000")
    .endpoints(endpoints)
    .serve()
    .await?;
```

### New Simplified API Benefits

The new simplified API offers several advantages over the old approach:

**New API (Recommended):**
```rust
use helios_engine::{ServerBuilder, get, post, EndpointBuilder, EndpointResponse};

let endpoints = vec![
    // Simple static endpoint
    get("/api/version", serde_json::json!({
        "version": "1.0.0",
        "service": "Helios Engine"
    })),

    // Dynamic endpoint with handler
    EndpointBuilder::post("/api/echo")
        .handle(|req| {
            let message = req
                .and_then(|r| r.body)
                .and_then(|b| b.get("message").cloned())
                .unwrap_or_else(|| serde_json::json!("No message"));

            EndpointResponse::ok(serde_json::json!({
                "echo": message
            }))
        })
        .build(),
];

ServerBuilder::with_agent(agent, "local-model")
    .address("127.0.0.1:8000")
    .endpoints(endpoints)
    .serve()
    .await?;
```

**Benefits of the New API:**
1. **Simpler syntax** - Less boilerplate code
2. **Builder pattern** - Consistent with Agent API
3. **Dynamic handlers** - Process request data and return dynamic responses
4. **Type safety** - Better compile-time guarantees
5. **Helper functions** - Quick creation with `get()`, `post()`, etc.
6. **Response helpers** - Easy status code management
7. **Descriptions** - Document your endpoints inline
8. **Cleaner** - Vector-based endpoint management
```

### Option 2: Using Array Slice Syntax

Pass endpoints inline without creating a variable:

```rust
ServerBuilder::with_agent(agent, "model-name")
    .with_endpoints(&[
        get("/api/v1", serde_json::json!({"version": "1.0"})),
        get("/api/status", serde_json::json!({"status": "ok"})),
    ])
    .serve()
    .await?;
```

### Option 3: Adding Endpoints One by One

If you prefer a fluent API style:

```rust
ServerBuilder::with_agent(agent, "model-name")
    .endpoint(get("/api/version", serde_json::json!({"version": "1.0"})))
    .endpoint(get("/api/status", serde_json::json!({"status": "ok"})))
    .serve()
    .await?;
```

## Complete Example

Here's a complete example showing various endpoint types:

```rust
use helios_engine::{
    Agent, Config, CalculatorTool, ServerBuilder,
    EndpointBuilder, EndpointResponse, get, post
};

#[tokio::main]
async fn main() -> helios_engine::Result<()> {
    // Create agent
    let config = Config::from_file("config.toml")?;
    let agent = Agent::builder("API Agent")
        .config(config)
        .tool(Box::new(CalculatorTool))
        .build()
        .await?;

    // Define custom endpoints
    let endpoints = vec![
        // Simple static endpoint
        get("/api/version", serde_json::json!({
            "version": "1.0.0",
            "service": "My API"
        })),
        
        // Endpoint with description
        EndpointBuilder::get("/api/health")
            .json(serde_json::json!({"status": "healthy"}))
            .description("Health check endpoint")
            .build(),
        
        // Dynamic endpoint with handler
        EndpointBuilder::post("/api/greet")
            .handle(|req| {
                let name = req
                    .and_then(|r| r.body)
                    .and_then(|b| b.get("name").and_then(|n| n.as_str()))
                    .unwrap_or("World");
                
                EndpointResponse::ok(serde_json::json!({
                    "message": format!("Hello, {}!", name)
                }))
            })
            .description("Personalized greeting")
            .build(),
    ];

    // Start server
    ServerBuilder::with_agent(agent, "gpt-4")
        .address("127.0.0.1:8000")
        .endpoints(endpoints)
        .serve()
        .await?;

    Ok(())
}
```

## Testing Your Endpoints

### Using curl

```bash
# Test GET endpoint
curl http://127.0.0.1:8000/api/version

# Test POST endpoint with JSON body
curl -X POST http://127.0.0.1:8000/api/greet \
  -H 'Content-Type: application/json' \
  -d '{"name": "Alice"}'

# Test the standard chat endpoint
curl -X POST http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'
```

### Using HTTPie

```bash
# Test GET endpoint
http GET http://127.0.0.1:8000/api/version

# Test POST endpoint
http POST http://127.0.0.1:8000/api/greet name=Alice
```

## Request Data Access

Dynamic handlers receive an `Option<EndpointRequest>` with access to:

```rust
pub struct EndpointRequest {
    /// Query parameters from URL (?key=value)
    pub query: HashMap<String, String>,
    
    /// Path parameters (/users/:id)
    pub params: HashMap<String, String>,
    
    /// Request body as JSON (for POST, PUT, PATCH)
    pub body: Option<Value>,
}
```

Example using request data:

```rust
EndpointBuilder::post("/api/process")
    .handle(|req| {
        let req = req.unwrap();
        
        // Access query parameters
        let limit = req.query.get("limit").cloned();
        
        // Access path parameters
        let id = req.params.get("id").cloned();
        
        // Access request body
        let data = req.body.unwrap_or(serde_json::json!({}));
        
        EndpointResponse::ok(serde_json::json!({
            "received": data,
            "limit": limit,
            "id": id
        }))
    })
    .build()
```

## Comparison: Old vs New API

### Old API (Still Supported for Backward Compatibility)

```rust
use helios_engine::{CustomEndpoint, CustomEndpointsConfig};

let custom_endpoints = CustomEndpointsConfig {
    endpoints: vec![
        CustomEndpoint {
            method: "GET".to_string(),
            path: "/api/version".to_string(),
            response: serde_json::json!({
                "version": "1.0.0",
                "service": "Helios Engine"
            }),
            status_code: 200,
        },
        CustomEndpoint {
            method: "POST".to_string(),
            path: "/api/echo".to_string(),
            response: serde_json::json!({
                "message": "Static response only"
            }),
            status_code: 200,
        },
    ],
};

helios_engine::serve::start_server_with_agent_and_custom_endpoints(
    agent,
    "local-model".to_string(),
    "127.0.0.1:8000",
    Some(custom_endpoints),
).await?;
```

### New API (Recommended)

```rust
use helios_engine::{ServerBuilder, get, post, EndpointBuilder, EndpointResponse};

let endpoints = vec![
    // Simple static endpoint
    get("/api/version", serde_json::json!({
        "version": "1.0.0",
        "service": "Helios Engine"
    })),
    
    // Dynamic endpoint with handler
    EndpointBuilder::post("/api/echo")
        .handle(|req| {
            let message = req
                .and_then(|r| r.body)
                .and_then(|b| b.get("message").cloned())
                .unwrap_or_else(|| serde_json::json!("No message"));
            
            EndpointResponse::ok(serde_json::json!({
                "echo": message
            }))
        })
        .build(),
];

ServerBuilder::with_agent(agent, "local-model")
    .address("127.0.0.1:8000")
    .endpoints(endpoints)
    .serve()
    .await?;
```

### Benefits of the New API

1.  **Simpler syntax** - Less boilerplate code
2.  **Builder pattern** - Consistent with Agent API
3.  **Dynamic handlers** - Process request data and return dynamic responses
4.  **Type safety** - Better compile-time guarantees
5.  **Helper functions** - Quick creation with `get()`, `post()`, etc.
6.  **Response helpers** - Easy status code management
7.  **Descriptions** - Document your endpoints inline
8.  **Cleaner** - Vector-based endpoint management

## Best Practices

1. **Use vectors for multiple endpoints** - It's cleaner and more maintainable
2. **Add descriptions** - Help document your API
3. **Use response helpers** - They make status codes clear and consistent
4. **Validate input** - Always check request data before processing
5. **Handle errors gracefully** - Return appropriate error responses

## Migration Guide

If you're using the old API, here's how to migrate:

**Before:**
```rust
CustomEndpoint {
    method: "GET".to_string(),
    path: "/api/status".to_string(),
    response: serde_json::json!({"status": "ok"}),
    status_code: 200,
}
```

**After:**
```rust
get("/api/status", serde_json::json!({"status": "ok"}))
```

That's it! The new API is much simpler and more powerful.

## See Also

- [API Reference]API.md - Complete API documentation
- [Examples]../examples/ - More code examples
- [Server Guide]GETTING_STARTED.md#serving-agents - Server setup and configuration