brainwires-tools 0.9.0

Built-in tool implementations for the Brainwires Agent Framework
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
//! OpenAPI Tool Generation — Automatically create tools from OpenAPI 3.x specs
//!
//! Parses OpenAPI specifications and generates [`Tool`] definitions that can
//! be registered in a [`ToolRegistry`] and executed by agents.
//!
//! # Feature Gate
//!
//! This module requires the `openapi` feature:
//!
//! ```toml
//! brainwires-tools = { version = "0.8", features = ["openapi"] }
//! ```
//!
//! # Usage
//!
//! ```rust,ignore
//! use brainwires_tools::openapi::{openapi_to_tools, execute_openapi_tool, OpenApiAuth};
//!
//! // Parse spec and get tools
//! let spec_json = std::fs::read_to_string("openapi.json")?;
//! let api_tools = openapi_to_tools(&spec_json)?;
//!
//! // Register tools
//! for api_tool in &api_tools {
//!     registry.register(api_tool.tool.clone());
//! }
//!
//! // Execute a tool call
//! let result = execute_openapi_tool(
//!     &api_tools[0],
//!     &args,
//!     &reqwest::Client::new(),
//!     Some(&OpenApiAuth::Bearer("token".into())),
//! ).await?;
//! ```

use std::collections::HashMap;

use anyhow::{Result, anyhow};
use openapiv3::{
    OpenAPI, Operation, Parameter, ParameterSchemaOrContent, PathItem, ReferenceOr, Schema,
    SchemaKind, Type as OApiType,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use brainwires_core::{Tool, ToolInputSchema};

// ── Public types ─────────────────────────────────────────────────────────────

/// HTTP method for an OpenAPI endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HttpMethod {
    /// GET request.
    Get,
    /// POST request.
    Post,
    /// PUT request.
    Put,
    /// PATCH request.
    Patch,
    /// DELETE request.
    Delete,
}

impl std::fmt::Display for HttpMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HttpMethod::Get => write!(f, "GET"),
            HttpMethod::Post => write!(f, "POST"),
            HttpMethod::Put => write!(f, "PUT"),
            HttpMethod::Patch => write!(f, "PATCH"),
            HttpMethod::Delete => write!(f, "DELETE"),
        }
    }
}

/// Authentication configuration for OpenAPI tool execution.
#[derive(Debug, Clone)]
pub enum OpenApiAuth {
    /// Bearer token authentication.
    Bearer(String),
    /// API key in a header.
    ApiKey {
        /// Header name.
        header: String,
        /// API key value.
        key: String,
    },
    /// HTTP Basic authentication.
    Basic {
        /// Username.
        username: String,
        /// Password.
        password: String,
    },
}

/// A parsed OpenAPI endpoint with its corresponding tool definition.
#[derive(Debug, Clone)]
pub struct OpenApiTool {
    /// The generated tool definition for AI consumption.
    pub tool: Tool,
    /// The endpoint details for HTTP execution.
    pub endpoint: OpenApiEndpoint,
}

/// HTTP endpoint details extracted from an OpenAPI spec.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenApiEndpoint {
    /// HTTP method.
    pub method: HttpMethod,
    /// URL path template (e.g., "/users/{id}").
    pub path: String,
    /// Base URL for the API.
    pub base_url: String,
    /// Path parameters.
    pub path_params: Vec<OpenApiParam>,
    /// Query parameters.
    pub query_params: Vec<OpenApiParam>,
    /// Header parameters.
    pub header_params: Vec<OpenApiParam>,
    /// Whether a request body is expected.
    pub has_body: bool,
}

/// A single parameter from an OpenAPI spec.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OpenApiParam {
    /// Parameter name.
    pub name: String,
    /// Parameter description.
    pub description: Option<String>,
    /// Whether the parameter is required.
    pub required: bool,
    /// JSON Schema type (e.g., "string", "integer").
    pub schema_type: String,
}

// ── Parsing ──────────────────────────────────────────────────────────────────

/// Parse an OpenAPI 3.x JSON or YAML spec and generate tool definitions.
///
/// Each endpoint (method + path combination) becomes a separate [`OpenApiTool`].
/// Tool names are derived from `operationId` if present, or generated from
/// the HTTP method and path.
pub fn openapi_to_tools(spec: &str) -> Result<Vec<OpenApiTool>> {
    let openapi: OpenAPI = serde_json::from_str(spec)
        .or_else(|_| serde_yml::from_str(spec))
        .map_err(|e| anyhow!("Failed to parse OpenAPI spec: {}", e))?;

    let base_url = openapi
        .servers
        .first()
        .map(|s| s.url.trim_end_matches('/').to_string())
        .unwrap_or_default();

    let mut tools = Vec::new();

    for (path, path_item) in &openapi.paths.paths {
        if let ReferenceOr::Item(item) = path_item {
            let methods = [
                (HttpMethod::Get, &item.get),
                (HttpMethod::Post, &item.post),
                (HttpMethod::Put, &item.put),
                (HttpMethod::Patch, &item.patch),
                (HttpMethod::Delete, &item.delete),
            ];

            for (method, operation) in methods {
                if let Some(op) = operation
                    && let Some(tool) = parse_operation(&openapi, &base_url, path, method, item, op)
                {
                    tools.push(tool);
                }
            }
        }
    }

    Ok(tools)
}

fn parse_operation(
    _spec: &OpenAPI,
    base_url: &str,
    path: &str,
    method: HttpMethod,
    path_item: &PathItem,
    operation: &Operation,
) -> Option<OpenApiTool> {
    // Generate tool name from operationId or method+path
    let tool_name = operation.operation_id.clone().unwrap_or_else(|| {
        let clean_path = path
            .replace('/', "_")
            .replace(['{', '}'], "")
            .trim_matches('_')
            .to_string();
        format!("{}_{}", method.to_string().to_lowercase(), clean_path)
    });

    // Generate description
    let description = operation
        .summary
        .clone()
        .or_else(|| operation.description.clone())
        .unwrap_or_else(|| format!("{} {}", method, path));

    // Collect parameters from both path-level and operation-level
    let mut path_params = Vec::new();
    let mut query_params = Vec::new();
    let mut header_params = Vec::new();
    let mut properties: HashMap<String, Value> = HashMap::new();
    let mut required_params: Vec<String> = Vec::new();

    // Process path-level parameters
    for param_ref in &path_item.parameters {
        if let ReferenceOr::Item(param) = param_ref {
            process_parameter(
                param,
                &mut path_params,
                &mut query_params,
                &mut header_params,
                &mut properties,
                &mut required_params,
            );
        }
    }

    // Process operation-level parameters (override path-level)
    for param_ref in &operation.parameters {
        if let ReferenceOr::Item(param) = param_ref {
            process_parameter(
                param,
                &mut path_params,
                &mut query_params,
                &mut header_params,
                &mut properties,
                &mut required_params,
            );
        }
    }

    // Check for request body
    let has_body = operation.request_body.is_some();
    if has_body {
        properties.insert(
            "body".to_string(),
            json!({
                "type": "object",
                "description": "Request body (JSON object)"
            }),
        );
    }

    let input_schema = ToolInputSchema {
        schema_type: "object".to_string(),
        properties: if properties.is_empty() {
            None
        } else {
            Some(properties)
        },
        required: if required_params.is_empty() {
            None
        } else {
            Some(required_params)
        },
    };

    let tool = Tool {
        name: tool_name,
        description,
        input_schema,
        requires_approval: false,
        defer_loading: true, // Lazy-load by default
        allowed_callers: Vec::new(),
        input_examples: Vec::new(),
    };

    let endpoint = OpenApiEndpoint {
        method,
        path: path.to_string(),
        base_url: base_url.to_string(),
        path_params,
        query_params,
        header_params,
        has_body,
    };

    Some(OpenApiTool { tool, endpoint })
}

fn process_parameter(
    param: &Parameter,
    path_params: &mut Vec<OpenApiParam>,
    query_params: &mut Vec<OpenApiParam>,
    header_params: &mut Vec<OpenApiParam>,
    properties: &mut HashMap<String, Value>,
    required_params: &mut Vec<String>,
) {
    let (name, required, location, schema_or_content, description) = match param {
        Parameter::Query {
            parameter_data,
            style: _,
            allow_reserved: _,
            allow_empty_value: _,
        } => (
            &parameter_data.name,
            parameter_data.required,
            "query",
            &parameter_data.format,
            &parameter_data.description,
        ),
        Parameter::Header {
            parameter_data,
            style: _,
        } => (
            &parameter_data.name,
            parameter_data.required,
            "header",
            &parameter_data.format,
            &parameter_data.description,
        ),
        Parameter::Path {
            parameter_data,
            style: _,
        } => {
            (
                &parameter_data.name,
                true, // Path params are always required
                "path",
                &parameter_data.format,
                &parameter_data.description,
            )
        }
        Parameter::Cookie { .. } => return, // Skip cookie params
    };

    let schema_type = extract_schema_type(schema_or_content);

    let api_param = OpenApiParam {
        name: name.clone(),
        description: description.clone(),
        required,
        schema_type: schema_type.clone(),
    };

    match location {
        "path" => path_params.push(api_param),
        "query" => query_params.push(api_param),
        "header" => header_params.push(api_param),
        _ => {}
    }

    // Add to tool input schema properties
    let mut prop = json!({ "type": schema_type });
    if let Some(desc) = description {
        prop["description"] = json!(desc);
    }
    properties.insert(name.clone(), prop);

    if required && !required_params.contains(name) {
        required_params.push(name.clone());
    }
}

fn extract_schema_type(format: &ParameterSchemaOrContent) -> String {
    match format {
        ParameterSchemaOrContent::Schema(schema_ref) => {
            if let ReferenceOr::Item(schema) = schema_ref {
                schema_to_type_string(schema)
            } else {
                "string".to_string()
            }
        }
        ParameterSchemaOrContent::Content(_) => "string".to_string(),
    }
}

fn schema_to_type_string(schema: &Schema) -> String {
    match &schema.schema_kind {
        SchemaKind::Type(t) => match t {
            OApiType::String(_) => "string".to_string(),
            OApiType::Number(_) => "number".to_string(),
            OApiType::Integer(_) => "integer".to_string(),
            OApiType::Boolean(_) => "boolean".to_string(),
            OApiType::Array(_) => "array".to_string(),
            OApiType::Object(_) => "object".to_string(),
        },
        _ => "string".to_string(),
    }
}

// ── Execution ────────────────────────────────────────────────────────────────

/// Execute an OpenAPI tool by making the HTTP request.
///
/// Substitutes path parameters, adds query parameters, attaches auth,
/// and returns the response body as a string.
pub async fn execute_openapi_tool(
    api_tool: &OpenApiTool,
    args: &Value,
    client: &reqwest::Client,
    auth: Option<&OpenApiAuth>,
) -> Result<String> {
    let endpoint = &api_tool.endpoint;

    // Build URL with path parameter substitution
    let mut url_path = endpoint.path.clone();
    for param in &endpoint.path_params {
        if let Some(value) = args.get(&param.name) {
            let value_str = match value {
                Value::String(s) => s.clone(),
                _ => value.to_string(),
            };
            url_path = url_path.replace(&format!("{{{}}}", param.name), &value_str);
        } else if param.required {
            return Err(anyhow!("Missing required path parameter: {}", param.name));
        }
    }

    let url = format!("{}{}", endpoint.base_url, url_path);
    let mut request = match endpoint.method {
        HttpMethod::Get => client.get(&url),
        HttpMethod::Post => client.post(&url),
        HttpMethod::Put => client.put(&url),
        HttpMethod::Patch => client.patch(&url),
        HttpMethod::Delete => client.delete(&url),
    };

    // Add query parameters
    let mut query_pairs: Vec<(String, String)> = Vec::new();
    for param in &endpoint.query_params {
        if let Some(value) = args.get(&param.name) {
            let value_str = match value {
                Value::String(s) => s.clone(),
                _ => value.to_string(),
            };
            query_pairs.push((param.name.clone(), value_str));
        } else if param.required {
            return Err(anyhow!("Missing required query parameter: {}", param.name));
        }
    }
    if !query_pairs.is_empty() {
        request = request.query(&query_pairs);
    }

    // Add header parameters
    for param in &endpoint.header_params {
        if let Some(value) = args.get(&param.name) {
            let value_str = match value {
                Value::String(s) => s.clone(),
                _ => value.to_string(),
            };
            request = request.header(&param.name, &value_str);
        }
    }

    // Add request body
    if endpoint.has_body
        && let Some(body) = args.get("body")
    {
        request = request.json(body);
    }

    // Add authentication
    if let Some(auth) = auth {
        request = match auth {
            OpenApiAuth::Bearer(token) => request.bearer_auth(token),
            OpenApiAuth::ApiKey { header, key } => request.header(header.as_str(), key.as_str()),
            OpenApiAuth::Basic { username, password } => {
                request.basic_auth(username, Some(password))
            }
        };
    }

    // Execute request
    let response = request
        .send()
        .await
        .map_err(|e| anyhow!("HTTP request failed: {}", e))?;
    let status = response.status();
    let body = response.text().await.unwrap_or_default();

    if status.is_success() {
        Ok(body)
    } else {
        Err(anyhow!(
            "HTTP {} {}: {}",
            status.as_u16(),
            status.canonical_reason().unwrap_or(""),
            body
        ))
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn petstore_spec() -> &'static str {
        r#"{
            "openapi": "3.0.0",
            "info": { "title": "Petstore", "version": "1.0.0" },
            "servers": [{ "url": "https://petstore.example.com/v1" }],
            "paths": {
                "/pets": {
                    "get": {
                        "operationId": "listPets",
                        "summary": "List all pets",
                        "parameters": [
                            {
                                "name": "limit",
                                "in": "query",
                                "required": false,
                                "schema": { "type": "integer" },
                                "description": "How many items to return"
                            }
                        ],
                        "responses": { "200": { "description": "OK" } }
                    },
                    "post": {
                        "operationId": "createPet",
                        "summary": "Create a pet",
                        "requestBody": {
                            "required": true,
                            "content": {
                                "application/json": {
                                    "schema": {
                                        "type": "object",
                                        "properties": {
                                            "name": { "type": "string" },
                                            "tag": { "type": "string" }
                                        }
                                    }
                                }
                            }
                        },
                        "responses": { "201": { "description": "Created" } }
                    }
                },
                "/pets/{petId}": {
                    "get": {
                        "operationId": "showPetById",
                        "summary": "Info for a specific pet",
                        "parameters": [
                            {
                                "name": "petId",
                                "in": "path",
                                "required": true,
                                "schema": { "type": "string" },
                                "description": "The id of the pet"
                            }
                        ],
                        "responses": { "200": { "description": "OK" } }
                    }
                }
            }
        }"#
    }

    #[test]
    fn test_parse_petstore_spec() {
        let tools = openapi_to_tools(petstore_spec()).unwrap();
        assert_eq!(tools.len(), 3);

        // Check listPets
        let list = tools.iter().find(|t| t.tool.name == "listPets").unwrap();
        assert_eq!(list.endpoint.method, HttpMethod::Get);
        assert_eq!(list.endpoint.path, "/pets");
        assert_eq!(list.endpoint.base_url, "https://petstore.example.com/v1");
        assert_eq!(list.endpoint.query_params.len(), 1);
        assert_eq!(list.endpoint.query_params[0].name, "limit");
        assert!(!list.endpoint.query_params[0].required);

        // Check createPet
        let create = tools.iter().find(|t| t.tool.name == "createPet").unwrap();
        assert_eq!(create.endpoint.method, HttpMethod::Post);
        assert!(create.endpoint.has_body);

        // Check showPetById
        let show = tools.iter().find(|t| t.tool.name == "showPetById").unwrap();
        assert_eq!(show.endpoint.method, HttpMethod::Get);
        assert_eq!(show.endpoint.path_params.len(), 1);
        assert_eq!(show.endpoint.path_params[0].name, "petId");
        assert!(show.endpoint.path_params[0].required);
    }

    #[test]
    fn test_tool_schema_generation() {
        let tools = openapi_to_tools(petstore_spec()).unwrap();
        let list = tools.iter().find(|t| t.tool.name == "listPets").unwrap();

        // Should have "limit" in properties
        let props = list.tool.input_schema.properties.as_ref().unwrap();
        assert!(props.contains_key("limit"));
        assert_eq!(props["limit"]["type"], "integer");

        // limit is not required
        assert!(list.tool.input_schema.required.is_none());
    }

    #[test]
    fn test_path_param_required() {
        let tools = openapi_to_tools(petstore_spec()).unwrap();
        let show = tools.iter().find(|t| t.tool.name == "showPetById").unwrap();

        let required = show.tool.input_schema.required.as_ref().unwrap();
        assert!(required.contains(&"petId".to_string()));
    }

    #[test]
    fn test_operation_id_fallback() {
        let spec = r#"{
            "openapi": "3.0.0",
            "info": { "title": "Test", "version": "1.0.0" },
            "servers": [{ "url": "https://api.example.com" }],
            "paths": {
                "/users/{id}/posts": {
                    "get": {
                        "summary": "Get user posts",
                        "responses": { "200": { "description": "OK" } }
                    }
                }
            }
        }"#;

        let tools = openapi_to_tools(spec).unwrap();
        assert_eq!(tools.len(), 1);
        // Without operationId, name should be generated from method + path
        assert_eq!(tools[0].tool.name, "get_users_id_posts");
    }

    #[test]
    fn test_empty_spec() {
        let spec = r#"{
            "openapi": "3.0.0",
            "info": { "title": "Empty", "version": "1.0.0" },
            "paths": {}
        }"#;

        let tools = openapi_to_tools(spec).unwrap();
        assert!(tools.is_empty());
    }

    #[test]
    fn test_invalid_spec() {
        let result = openapi_to_tools("not valid json or yaml");
        assert!(result.is_err());
    }
}