unifictl 5.4.0

CLI for UniFi Site Manager (API v1/EA)
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
// unifictl - CLI for UniFi Site Manager API
// Copyright (C) 2024 Mathias Uhl <mathiasuhl@gmx.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! JSON schema definitions and metadata for UniFi API responses
//!
//! This module provides schema information to help AI agents understand
//! the structure and meaning of API responses.

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Metadata about a field in a response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldMeta {
    /// Field name
    pub name: String,
    /// Field type (string, number, boolean, array, object)
    pub field_type: String,
    /// Human-readable description
    pub description: String,
    /// Whether this field is always present
    pub required: bool,
    /// AI-relevant importance (high, medium, low)
    pub importance: Importance,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Importance {
    High,
    Medium,
    Low,
}

/// Schema definition for an API endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndpointSchema {
    /// Endpoint name/identifier
    pub name: String,
    /// Description of what this endpoint returns
    pub description: String,
    /// Expected response structure
    pub fields: Vec<FieldMeta>,
    /// Common use cases for AI agents
    pub use_cases: Vec<String>,
    /// Estimated token count for typical response (useful for AI context management)
    pub typical_tokens: usize,
}

/// Schema registry for all endpoints
pub struct SchemaRegistry {
    schemas: HashMap<String, EndpointSchema>,
}

impl SchemaRegistry {
    pub fn new() -> Self {
        let mut registry = Self {
            schemas: HashMap::new(),
        };
        registry.register_all();
        registry
    }

    pub fn get(&self, endpoint: &str) -> Option<&EndpointSchema> {
        self.schemas.get(endpoint)
    }

    fn register(&mut self, schema: EndpointSchema) {
        self.schemas.insert(schema.name.clone(), schema);
    }

    fn register_all(&mut self) {
        // Device endpoints
        self.register(EndpointSchema {
            name: "device.list".to_string(),
            description: "List all devices (APs, switches, gateways) in the network".to_string(),
            fields: vec![
                FieldMeta {
                    name: "mac".to_string(),
                    field_type: "string".to_string(),
                    description: "Device MAC address (unique identifier)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "name".to_string(),
                    field_type: "string".to_string(),
                    description: "Device name/hostname".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "model".to_string(),
                    field_type: "string".to_string(),
                    description: "Device model (e.g., UAP-AC-LR, USW-24-POE)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "type".to_string(),
                    field_type: "string".to_string(),
                    description: "Device type (uap, usw, ugw)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "state".to_string(),
                    field_type: "number".to_string(),
                    description: "Adoption state (0=offline, 1=connected, etc.)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "ip".to_string(),
                    field_type: "string".to_string(),
                    description: "Current IP address".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
                FieldMeta {
                    name: "version".to_string(),
                    field_type: "string".to_string(),
                    description: "Firmware version".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
                FieldMeta {
                    name: "uptime".to_string(),
                    field_type: "number".to_string(),
                    description: "Uptime in seconds".to_string(),
                    required: false,
                    importance: Importance::Low,
                },
            ],
            use_cases: vec![
                "Inventory management".to_string(),
                "Health monitoring".to_string(),
                "Firmware update planning".to_string(),
                "Troubleshooting connectivity".to_string(),
            ],
            typical_tokens: 500,
        });

        // Client endpoints
        self.register(EndpointSchema {
            name: "client.list".to_string(),
            description: "List all connected clients (wired and wireless)".to_string(),
            fields: vec![
                FieldMeta {
                    name: "mac".to_string(),
                    field_type: "string".to_string(),
                    description: "Client MAC address".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "hostname".to_string(),
                    field_type: "string".to_string(),
                    description: "Client hostname (if available)".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "ip".to_string(),
                    field_type: "string".to_string(),
                    description: "Current IP address".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "is_wired".to_string(),
                    field_type: "boolean".to_string(),
                    description: "Whether client is connected via ethernet".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
                FieldMeta {
                    name: "ap_mac".to_string(),
                    field_type: "string".to_string(),
                    description: "MAC address of connected AP (wireless only)".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "channel".to_string(),
                    field_type: "number".to_string(),
                    description: "WiFi channel (wireless only)".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
                FieldMeta {
                    name: "rssi".to_string(),
                    field_type: "number".to_string(),
                    description: "Signal strength in dBm (wireless only)".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "tx_bytes".to_string(),
                    field_type: "number".to_string(),
                    description: "Bytes transmitted".to_string(),
                    required: false,
                    importance: Importance::Low,
                },
                FieldMeta {
                    name: "rx_bytes".to_string(),
                    field_type: "number".to_string(),
                    description: "Bytes received".to_string(),
                    required: false,
                    importance: Importance::Low,
                },
            ],
            use_cases: vec![
                "Client troubleshooting".to_string(),
                "WiFi performance analysis".to_string(),
                "Network capacity planning".to_string(),
                "Security monitoring".to_string(),
            ],
            typical_tokens: 800,
        });

        // Event endpoints
        self.register(EndpointSchema {
            name: "event.list".to_string(),
            description: "List recent network events (connections, disconnections, alerts)"
                .to_string(),
            fields: vec![
                FieldMeta {
                    name: "_id".to_string(),
                    field_type: "string".to_string(),
                    description: "Event unique identifier".to_string(),
                    required: true,
                    importance: Importance::Low,
                },
                FieldMeta {
                    name: "key".to_string(),
                    field_type: "string".to_string(),
                    description: "Event type key (e.g., EVT_WU_Connected)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "datetime".to_string(),
                    field_type: "string".to_string(),
                    description: "Event timestamp (ISO 8601)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "msg".to_string(),
                    field_type: "string".to_string(),
                    description: "Human-readable event message".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "subsystem".to_string(),
                    field_type: "string".to_string(),
                    description: "Subsystem that generated the event (wlan, lan, etc.)".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
            ],
            use_cases: vec![
                "Troubleshooting connectivity issues".to_string(),
                "Security monitoring".to_string(),
                "Pattern detection".to_string(),
                "Historical analysis".to_string(),
            ],
            typical_tokens: 1200,
        });

        // Health endpoints
        self.register(EndpointSchema {
            name: "health.get".to_string(),
            description: "Get network health status and metrics".to_string(),
            fields: vec![
                FieldMeta {
                    name: "subsystem".to_string(),
                    field_type: "string".to_string(),
                    description: "Subsystem name (wan, lan, wlan, vpn, etc.)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "status".to_string(),
                    field_type: "string".to_string(),
                    description: "Health status (ok, warning, error)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "num_user".to_string(),
                    field_type: "number".to_string(),
                    description: "Number of active users".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
                FieldMeta {
                    name: "num_guest".to_string(),
                    field_type: "number".to_string(),
                    description: "Number of active guests".to_string(),
                    required: false,
                    importance: Importance::Medium,
                },
            ],
            use_cases: vec![
                "Overall network health check".to_string(),
                "Quick diagnostics".to_string(),
                "Monitoring dashboards".to_string(),
            ],
            typical_tokens: 300,
        });

        // Traffic/Flow endpoints
        self.register(EndpointSchema {
            name: "traffic.stats".to_string(),
            description: "Traffic statistics over time period".to_string(),
            fields: vec![
                FieldMeta {
                    name: "time".to_string(),
                    field_type: "number".to_string(),
                    description: "Timestamp (Unix milliseconds)".to_string(),
                    required: true,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "rx_bytes".to_string(),
                    field_type: "number".to_string(),
                    description: "Bytes received in this interval".to_string(),
                    required: false,
                    importance: Importance::High,
                },
                FieldMeta {
                    name: "tx_bytes".to_string(),
                    field_type: "number".to_string(),
                    description: "Bytes transmitted in this interval".to_string(),
                    required: false,
                    importance: Importance::High,
                },
            ],
            use_cases: vec![
                "Bandwidth analysis".to_string(),
                "Capacity planning".to_string(),
                "Trend analysis".to_string(),
                "Anomaly detection".to_string(),
            ],
            typical_tokens: 2000,
        });

        // Add more schemas as needed...
    }
}

impl Default for SchemaRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Estimate token count for JSON data (rough approximation)
/// Uses ~4 characters per token as a heuristic
pub fn estimate_tokens(data: &Value) -> usize {
    let json_str = data.to_string();
    json_str.len() / 4
}

/// Summarize a response for LLM consumption
/// Returns a compact summary with key statistics
pub fn summarize_response(data: &Value, schema: Option<&EndpointSchema>) -> Value {
    let mut summary = serde_json::json!({
        "schema_available": schema.is_some(),
    });

    if let Some(schema) = schema {
        summary["endpoint"] = serde_json::json!(schema.name);
        summary["description"] = serde_json::json!(schema.description);
        summary["use_cases"] = serde_json::json!(schema.use_cases);
    }

    // Extract statistics from data
    if let Some(arr) = data.as_array() {
        summary["count"] = serde_json::json!(arr.len());
        summary["type"] = serde_json::json!("array");

        // Sample first and last item for context
        if !arr.is_empty() {
            summary["sample_first"] = arr[0].clone();
            if arr.len() > 1 {
                summary["sample_last"] = arr[arr.len() - 1].clone();
            }
        }
    } else if let Some(obj) = data.as_object() {
        summary["type"] = serde_json::json!("object");
        summary["fields"] = serde_json::json!(obj.keys().collect::<Vec<_>>());

        // Include data if object is small
        if obj.len() < 10 {
            summary["data"] = data.clone();
        }
    }

    summary["estimated_tokens"] = serde_json::json!(estimate_tokens(data));

    summary
}

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

    #[test]
    fn test_schema_registry() {
        let registry = SchemaRegistry::new();
        assert!(registry.get("device.list").is_some());
        assert!(registry.get("client.list").is_some());
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_estimate_tokens() {
        let data = serde_json::json!({"key": "value"});
        let tokens = estimate_tokens(&data);
        assert!(tokens > 0);
        assert!(tokens < 10); // Should be small for this tiny object
    }

    #[test]
    fn test_summarize_array() {
        let data = serde_json::json!([
            {"mac": "aa:bb:cc:dd:ee:ff", "name": "device1"},
            {"mac": "11:22:33:44:55:66", "name": "device2"},
        ]);
        let summary = summarize_response(&data, None);
        assert_eq!(summary["count"], 2);
        assert_eq!(summary["type"], "array");
    }
}