brk_server 0.3.0-beta.2

A server with an API for anything from BRK
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
use aide::openapi::OpenApi;
use derive_more::Deref;
use serde_json::{Map, Value};

/// Compact OpenAPI spec optimized for LLM consumption.
#[derive(Deref)]
pub struct ApiJson(String);

impl ApiJson {
    pub fn new(openapi: &OpenApi) -> Self {
        let json = serde_json::to_string(openapi).unwrap();
        Self(compact_json(&json))
    }

    pub fn to_json(&self) -> serde_json::Value {
        serde_json::from_str(&self.0).unwrap()
    }
}

/// Compacts an OpenAPI spec JSON to reduce size for LLM consumption.
/// Removes redundant fields while preserving essential API information.
///
/// Transformations applied (in order):
/// 1. Remove deprecated endpoints
/// 2. Remove contact/license from info
/// 3. Remove *Param schemas
/// 3. Remove error responses (304, 400, 404, 500)
/// 4. Compact responses to "returns": "Type"
/// 5. Remove per-endpoint tags and style
/// 6. Simplify parameter schema to type, remove param descriptions
/// 7. Remove summary and operationId
/// 8. Remove examples, replace $ref with type
/// 9. Flatten single-item allOf
/// 10. Flatten anyOf to type array
/// 11. Remove format
/// 12. Remove property descriptions
/// 13. Simplify properties to direct types
/// 14. Remove min/max constraints
/// 15. Trim descriptions to first paragraph, strip mempool.space links
/// 16. Remove required arrays from schemas
/// 17. Remove redundant "type": "object" when properties exist
/// 18. Flatten single-element type arrays
/// 19. Replace large enums (>40 values) with string type
fn compact_json(json: &str) -> String {
    let mut spec: Value = serde_json::from_str(json).expect("Invalid OpenAPI JSON");

    // Step 1: Remove deprecated endpoints from paths
    if let Some(Value::Object(paths)) = spec.get_mut("paths") {
        paths.retain(|_, v| {
            if let Value::Object(path_obj) = v
                && let Some(Value::Object(get_obj)) = path_obj.get("get")
            {
                return get_obj.get("deprecated") != Some(&Value::Bool(true));
            }
            true
        });
    }

    // Step 2: Remove contact/license from info
    if let Some(Value::Object(info)) = spec.get_mut("info") {
        info.remove("contact");
        info.remove("license");
    }

    // Step 3: Remove *Param schemas from components
    if let Some(Value::Object(components)) = spec.get_mut("components")
        && let Some(Value::Object(schemas)) = components.get_mut("schemas")
    {
        schemas.retain(|name, _| !name.ends_with("Param"));
    }

    compact_value(&mut spec);
    serde_json::to_string(&spec).unwrap()
}

fn compact_value(value: &mut Value) {
    match value {
        Value::Object(obj) => {
            // Step 1: Remove error responses
            if let Some(Value::Object(responses)) = obj.get_mut("responses") {
                for code in &["304", "400", "404", "500"] {
                    responses.remove(*code);
                }
            }

            // Step 2: Compact responses to "returns": "Type"
            if let Some(Value::Object(responses)) = obj.remove("responses")
                && let Some(returns) = extract_return_type(&responses)
            {
                obj.insert("returns".to_string(), Value::String(returns));
            }

            // Step 3: Remove per-endpoint tags and style
            // (only remove "tags" if it's an array, not if it's the top-level tags definition)
            if let Some(Value::Array(_)) = obj.get("tags") {
                // This is a per-endpoint tags array like ["Addresses"], remove it
                obj.remove("tags");
            }
            obj.remove("style");

            // Step 4: Simplify parameters (schema to type, remove descriptions)
            if let Some(Value::Array(params)) = obj.get_mut("parameters") {
                for param in params {
                    simplify_parameter(param);
                }
            }

            // Step 7: Remove summary and operationId
            obj.remove("summary");
            obj.remove("operationId");

            // Step 6: Remove examples, replace $ref with type
            obj.remove("example");
            obj.remove("examples");
            if let Some(Value::String(ref_path)) = obj.remove("$ref") {
                let type_name = ref_path.split('/').next_back().unwrap_or("any");
                obj.insert("type".to_string(), Value::String(type_name.to_string()));
            }

            // Step 7: Flatten single-item allOf
            if let Some(Value::Array(all_of)) = obj.remove("allOf")
                && all_of.len() == 1
                && let Some(Value::Object(inner)) = all_of.into_iter().next()
            {
                for (k, v) in inner {
                    obj.insert(k, v);
                }
            }

            // Step 8: Flatten anyOf to type array
            if let Some(Value::Array(any_of)) = obj.remove("anyOf") {
                let types: Vec<Value> = any_of
                    .into_iter()
                    .filter_map(|item| {
                        if let Value::Object(o) = item {
                            if let Some(Value::String(ref_path)) = o.get("$ref") {
                                return Some(Value::String(
                                    ref_path.split('/').next_back().unwrap_or("any").to_string(),
                                ));
                            }
                            o.get("type").cloned()
                        } else {
                            None
                        }
                    })
                    .collect();
                if !types.is_empty() {
                    obj.insert("type".to_string(), Value::Array(types));
                }
            }

            // Step 11: Remove format
            obj.remove("format");

            // Step 14: Remove min/max constraints
            obj.remove("minimum");
            obj.remove("maximum");

            // Step 16: Remove required arrays from schemas (but keep boolean required on params)
            if let Some(Value::Array(_)) = obj.get("required") {
                obj.remove("required");
            }

            // Step 17: Flatten single-element type arrays: ["object"] -> "object"
            if let Some(Value::Array(arr)) = obj.get("type").cloned()
                && arr.len() == 1
            {
                obj.insert("type".to_string(), arr.into_iter().next().unwrap());
            }

            // Step 18: Remove "type": "object" when properties exist (it's redundant)
            if obj.contains_key("properties")
                && obj.get("type") == Some(&Value::String("object".to_string()))
            {
                obj.remove("type");
            }

            // Step 19: Replace large enums (>40 values) with just string type
            if let Some(Value::Array(enum_values)) = obj.get("enum")
                && enum_values.len() > 40
            {
                obj.remove("enum");
            }

            // Step 15: Strip mempool.space links and keep only first paragraph of descriptions
            if let Some(Value::String(desc)) = obj.get_mut("description") {
                *desc = trim_description(desc);
            }

            // Step 12 & 13: Simplify properties (remove descriptions, simplify to direct types)
            if let Some(Value::Object(props)) = obj.get_mut("properties") {
                simplify_properties(props);
            }

            // Recurse into remaining values
            for (_, v) in obj.iter_mut() {
                compact_value(v);
            }
        }
        Value::Array(arr) => {
            for item in arr {
                compact_value(item);
            }
        }
        _ => {}
    }
}

/// Trim description to first paragraph and strip mempool.space endpoint links.
fn trim_description(desc: &str) -> String {
    // First, strip mempool.space docs links (endpoint pattern with asterisks)
    let desc = if let Some(idx) = desc.find("*[Mempool.space docs]") {
        desc[..idx].trim()
    } else {
        desc
    };

    // Keep only the first paragraph (up to \n\n)
    if let Some(idx) = desc.find("\n\n") {
        desc[..idx].trim().to_string()
    } else {
        desc.trim().to_string()
    }
}

fn extract_return_type(responses: &Map<String, Value>) -> Option<String> {
    let resp_200 = responses.get("200")?;
    let content = resp_200.get("content")?;
    let json_content = content.get("application/json")?;
    let schema = json_content.get("schema")?;
    Some(schema_to_type_string(schema))
}

fn schema_to_type_string(schema: &Value) -> String {
    if let Some(Value::String(ref_path)) = schema.get("$ref") {
        return ref_path.split('/').next_back().unwrap_or("any").to_string();
    }
    if let Some(Value::String(t)) = schema.get("type") {
        if t == "array"
            && let Some(items) = schema.get("items")
        {
            return format!("array[{}]", schema_to_type_string(items));
        }
        return t.clone();
    }
    "any".to_string()
}

fn simplify_parameter(param: &mut Value) {
    if let Value::Object(obj) = param {
        // Remove description
        obj.remove("description");

        // Extract type from schema
        if let Some(schema) = obj.remove("schema") {
            let type_val = extract_type_from_schema(&schema);
            obj.insert("type".to_string(), type_val);
        }
    }
}

fn extract_type_from_schema(schema: &Value) -> Value {
    if let Value::Object(obj) = schema {
        // Handle anyOf (optional fields)
        if let Some(Value::Array(any_of)) = obj.get("anyOf") {
            let types: Vec<Value> = any_of
                .iter()
                .filter_map(|item| {
                    if let Value::Object(o) = item {
                        if let Some(Value::String(ref_path)) = o.get("$ref") {
                            return Some(Value::String(
                                ref_path.split('/').next_back().unwrap_or("any").to_string(),
                            ));
                        }
                        o.get("type").cloned()
                    } else {
                        None
                    }
                })
                .collect();
            if types.len() == 1 {
                return types.into_iter().next().unwrap();
            }
            return Value::Array(types);
        }

        // Handle $ref
        if let Some(Value::String(ref_path)) = obj.get("$ref") {
            return Value::String(ref_path.split('/').next_back().unwrap_or("any").to_string());
        }

        // Handle type
        if let Some(t) = obj.get("type") {
            return t.clone();
        }
    }
    Value::String("any".to_string())
}

fn simplify_properties(props: &mut Map<String, Value>) {
    let keys: Vec<String> = props.keys().cloned().collect();
    for key in keys {
        if let Some(prop_value) = props.get_mut(&key)
            && let Value::Object(prop_obj) = prop_value
        {
            // Remove description
            prop_obj.remove("description");

            // Check if we can simplify to just the type
            let simplified = simplify_property_value(prop_obj);
            *prop_value = simplified;
        }
    }
}

fn simplify_property_value(obj: &mut Map<String, Value>) -> Value {
    // Remove validation constraints, format, and examples
    for key in &[
        "default",
        "minItems",
        "maxItems",
        "uniqueItems",
        "minimum",
        "maximum",
        "format",
        "examples",
        "example",
        "description",
    ] {
        obj.remove(*key);
    }

    // Remove "items": true (means any type, not useful)
    if obj.get("items") == Some(&Value::Bool(true)) {
        obj.remove("items");
    }

    // Handle $ref - convert to type (runs before recursion would)
    if let Some(Value::String(ref_path)) = obj.remove("$ref") {
        let type_name = ref_path.split('/').next_back().unwrap_or("any");
        return Value::String(type_name.to_string());
    }

    // Handle single-item allOf - flatten and extract type
    if let Some(Value::Array(all_of)) = obj.remove("allOf")
        && all_of.len() == 1
        && let Some(Value::Object(inner)) = all_of.into_iter().next()
    {
        if let Some(Value::String(ref_path)) = inner.get("$ref") {
            let type_name = ref_path.split('/').next_back().unwrap_or("any");
            return Value::String(type_name.to_string());
        }
        if let Some(t) = inner.get("type") {
            return t.clone();
        }
    }

    // Handle anyOf - flatten to type array (runs before recursion would)
    if let Some(Value::Array(any_of)) = obj.remove("anyOf") {
        let types: Vec<Value> = any_of
            .into_iter()
            .filter_map(|item| {
                if let Value::Object(o) = item {
                    if let Some(Value::String(ref_path)) = o.get("$ref") {
                        return Some(Value::String(
                            ref_path.split('/').next_back().unwrap_or("any").to_string(),
                        ));
                    }
                    o.get("type").cloned()
                } else {
                    None
                }
            })
            .collect();
        return Value::Array(types);
    }

    // If only "type" remains, return just the type value
    if obj.len() == 1
        && let Some(t) = obj.get("type")
    {
        return t.clone();
    }

    // Handle array with items
    if obj.get("type") == Some(&Value::String("array".to_string()))
        && let Some(items) = obj.get("items")
        && let Value::Object(items_obj) = items
        && items_obj.len() == 1
    {
        // Items can have either "type" or "$ref"
        if let Some(Value::String(item_type)) = items_obj.get("type") {
            return Value::String(format!("array[{}]", item_type));
        }
        if let Some(Value::String(ref_path)) = items_obj.get("$ref") {
            let type_name = ref_path.split('/').next_back().unwrap_or("any");
            return Value::String(format!("array[{}]", type_name));
        }
    }

    Value::Object(obj.clone())
}

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

    #[test]
    fn test_trim_property_anyof() {
        let input = r##"{
            "type": "object",
            "properties": {
                "index": {
                    "anyOf": [
                        {"type": "TxIndex"},
                        {"type": "null"}
                    ]
                }
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        // Property should be simplified to array, not {"type": [...]}
        let index = &parsed["properties"]["index"];
        assert!(index.is_array(), "Expected array, got: {}", index);
        assert_eq!(index[0], "TxIndex");
        assert_eq!(index[1], "null");
    }

    #[test]
    fn test_trim_parameter_anyof() {
        let input = r##"{
            "parameters": [{
                "in": "query",
                "name": "after_txid",
                "schema": {
                    "anyOf": [
                        {"$ref": "#/components/schemas/Txid"},
                        {"type": "null"}
                    ]
                }
            }]
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        // Parameter should have type array including null
        let param = &parsed["parameters"][0];
        assert_eq!(param["name"], "after_txid");
        assert_eq!(param["type"][0], "Txid");
        assert_eq!(param["type"][1], "null");
    }

    #[test]
    fn test_trim_property_ref() {
        let input = r##"{
            "type": "object",
            "properties": {
                "txid": {
                    "$ref": "#/components/schemas/Txid"
                }
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        // Property with $ref should be simplified to just the type name
        assert_eq!(parsed["properties"]["txid"], "Txid");
    }

    #[test]
    fn test_trim_nested_component_schema() {
        // This matches the actual API structure: components > schemas > Type > properties
        let input = r##"{
            "components": {
                "schemas": {
                    "AddressStats": {
                        "type": "object",
                        "properties": {
                            "address": {
                                "$ref": "#/components/schemas/Address"
                            },
                            "chain_stats": {
                                "$ref": "#/components/schemas/AddressChainStats"
                            }
                        }
                    }
                }
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        let props = &parsed["components"]["schemas"]["AddressStats"]["properties"];
        assert_eq!(props["address"], "Address", "address should be simplified");
        assert_eq!(
            props["chain_stats"], "AddressChainStats",
            "chain_stats should be simplified"
        );
    }

    #[test]
    fn test_trim_property_allof_with_ref() {
        // Real API uses allOf wrapper around $ref
        let input = r##"{
            "type": "object",
            "properties": {
                "address": {
                    "description": "Bitcoin address string",
                    "allOf": [
                        {"$ref": "#/components/schemas/Address"}
                    ]
                }
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        assert_eq!(parsed["properties"]["address"], "Address");
    }

    #[test]
    fn test_trim_property_array_with_ref() {
        let input = r##"{
            "type": "object",
            "properties": {
                "vin": {
                    "type": "array",
                    "items": {
                        "$ref": "#/components/schemas/TxIn"
                    }
                }
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        // Array with $ref items should be simplified to "array[Type]"
        assert_eq!(parsed["properties"]["vin"], "array[TxIn]");
    }

    #[test]
    fn test_trim_responses_to_returns() {
        let input = r##"{
            "responses": {
                "200": {
                    "content": {
                        "application/json": {
                            "schema": {"$ref": "#/components/schemas/Block"}
                        }
                    }
                },
                "400": {"description": "Bad request"},
                "500": {"description": "Error"}
            }
        }"##;

        let result = compact_json(input);
        let parsed: Value = serde_json::from_str(&result).unwrap();

        assert_eq!(parsed["returns"], "Block");
        assert!(parsed.get("responses").is_none());
    }
}