netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! Protocol documentation generation
//!
//! Provides functionality to generate documentation for all protocols
//! including their event types, actions, and parameters.

use crate::protocol::metadata::DevelopmentState;

/// ANSI color codes for terminal output
mod colors {
    pub const RESET: &str = "\x1b[0m";
    pub const BOLD: &str = "\x1b[1m";
    pub const DIM: &str = "\x1b[2m";

    // Colors
    pub const CYAN: &str = "\x1b[36m";
    pub const BLUE: &str = "\x1b[34m";
    pub const GREEN: &str = "\x1b[32m";
    pub const YELLOW: &str = "\x1b[33m";
    pub const MAGENTA: &str = "\x1b[35m";
    pub const RED: &str = "\x1b[31m";
    pub const GREY: &str = "\x1b[90m";

    // Bright colors
    pub const BRIGHT_CYAN: &str = "\x1b[96m";
    pub const BRIGHT_GREEN: &str = "\x1b[92m";
    pub const BRIGHT_YELLOW: &str = "\x1b[93m";
}

/// Generate a list of all available protocols with brief descriptions
pub fn list_all_protocols() -> String {
    let mut output = String::new();

    // Title with colors
    output.push_str(&format!(
        "\n{}{}NetGet - LLM-Controlled Network Protocols{}\n\n",
        colors::BOLD,
        colors::BRIGHT_CYAN,
        colors::RESET
    ));

    // NetGet description
    output.push_str(&format!(
        "{}NetGet is an experimental network application where an LLM (via Ollama){}\n",
        colors::DIM,
        colors::RESET
    ));
    output.push_str(&format!(
        "{}controls network protocols and acts as a server for 50+ protocols.{}\n",
        colors::DIM,
        colors::RESET
    ));
    output.push_str(&format!("{}All protocol logic is handled by the LLM - you describe behavior in natural language.{}\n\n",
        colors::DIM, colors::RESET));

    // Key Features
    output.push_str(&format!("{}Key Features:{}\n", colors::BOLD, colors::RESET));
    output.push_str(&format!("  {}•{} {}Scripting:{} LLM generates on-the-fly Python/JavaScript code to reduce LLM calls\n",
        colors::GREEN, colors::RESET,
        colors::BOLD, colors::RESET));
    output.push_str(&format!(
        "  {}•{} {}Web Search:{} LLM can fetch protocol RFCs and documentation from the web\n",
        colors::GREEN,
        colors::RESET,
        colors::BOLD,
        colors::RESET
    ));
    output.push_str(&format!(
        "  {}•{} {}File Reading:{} LLM can read local files (schemas, configs, prompts)\n",
        colors::GREEN,
        colors::RESET,
        colors::BOLD,
        colors::RESET
    ));
    output.push_str(&format!(
        "  {}•{} {}Logging:{} Comprehensive logging system (TRACE/DEBUG/INFO/WARN/ERROR levels)\n",
        colors::GREEN,
        colors::RESET,
        colors::BOLD,
        colors::RESET
    ));
    output.push_str(&format!(
        "  {}•{} {}Action-Based:{} Structured JSON responses for precise protocol control\n",
        colors::GREEN,
        colors::RESET,
        colors::BOLD,
        colors::RESET
    ));
    output.push_str(&format!("  {}•{} {}Dynamic Reconfiguration:{} Change server behavior at runtime without restart\n\n",
        colors::GREEN, colors::RESET,
        colors::BOLD, colors::RESET));

    // Dynamically generate grouped protocol list
    output.push_str(&format!(
        "{}Available Protocols:{}\n",
        colors::BOLD,
        colors::RESET
    ));
    output.push_str(&format!(
        "{}Use{} {}{}/docs <protocol>{} {}to see detailed information.{}\n\n",
        colors::DIM,
        colors::RESET,
        colors::CYAN,
        colors::BOLD,
        colors::RESET,
        colors::DIM,
        colors::RESET
    ));

    // Get all protocols from registry and group them
    let registry = crate::protocol::server_registry::registry();
    let all_protocols = registry.all_protocols();

    // Group protocols by their group_name with their state
    let mut groups: std::collections::HashMap<&'static str, Vec<(String, DevelopmentState)>> =
        std::collections::HashMap::new();

    for (protocol_name, protocol) in &all_protocols {
        let group = protocol.group_name();
        let state = protocol.metadata().state;
        groups
            .entry(group)
            .or_default()
            .push((protocol_name.clone(), state));
    }

    // Sort groups alphabetically
    let mut sorted_group_names: Vec<&'static str> = groups.keys().copied().collect();
    sorted_group_names.sort();

    for group_name in sorted_group_names {
        if let Some(protocols) = groups.get(group_name) {
            if protocols.is_empty() {
                continue;
            }

            // Output group header
            output.push_str(&format!(
                "{}━━━ {} ━━━{}\n",
                colors::BRIGHT_GREEN,
                group_name,
                colors::RESET
            ));

            // Sort protocols alphabetically within group
            let mut sorted_protocols = protocols.clone();
            sorted_protocols.sort_by(|a, b| a.0.cmp(&b.0));

            // Output protocol names with color coding based on state
            let colored_protocols: Vec<String> = sorted_protocols
                .iter()
                .map(|(name, state)| {
                    let color = match state {
                        DevelopmentState::Stable => colors::GREEN,
                        DevelopmentState::Beta => colors::BLUE,
                        DevelopmentState::Experimental => colors::YELLOW,
                        DevelopmentState::Incomplete => colors::RED,
                    };
                    format!("{}{}{}", color, name, colors::RESET)
                })
                .collect();

            let protocol_list =
                colored_protocols.join(&format!("{}, {}", colors::RESET, colors::DIM));
            output.push_str(&format!("  {}{}\n\n", colors::DIM, protocol_list));
        }
    }

    output
}

/// Generate detailed documentation for a specific protocol
pub fn show_protocol_docs(protocol_name: &str) -> Result<String, String> {
    let registry = crate::protocol::server_registry::registry();

    // Try to parse the protocol name using registry
    let parsed_protocol_name = registry.parse_from_str(protocol_name).ok_or_else(|| {
        format!(
            "{}Unknown protocol: {}{}. Use /docs to see all protocols.",
            colors::RED,
            protocol_name,
            colors::RESET
        )
    })?;

    let protocol = registry.get(&parsed_protocol_name).ok_or_else(|| {
        format!(
            "{}Protocol {} not found in registry{}",
            colors::RED,
            parsed_protocol_name,
            colors::RESET
        )
    })?;

    let metadata = protocol.metadata();
    let stack_name = registry
        .stack_name_by_protocol(&parsed_protocol_name)
        .unwrap_or("UNKNOWN");

    let mut output = String::new();

    // Title with box drawing characters
    output.push_str(&format!(
        "{}╭─────────────────────────────────────────╮{}\n",
        colors::CYAN,
        colors::RESET
    ));
    output.push_str(&format!(
        "{}│{} {}Protocol: {}{} {}│{}\n",
        colors::CYAN,
        colors::RESET,
        colors::BOLD,
        parsed_protocol_name,
        colors::RESET,
        colors::CYAN,
        colors::RESET
    ));
    output.push_str(&format!(
        "{}╰─────────────────────────────────────────╯{}\n\n",
        colors::CYAN,
        colors::RESET
    ));

    // Stack name
    output.push_str(&format!(
        "{}▸ Stack:{} {}{}{}\n",
        colors::BRIGHT_CYAN,
        colors::RESET,
        colors::GREEN,
        stack_name,
        colors::RESET
    ));

    // Status badge with color
    let (status_color, status_symbol) = match metadata.state {
        DevelopmentState::Stable => (colors::BRIGHT_GREEN, "✓"),
        DevelopmentState::Beta => (colors::BRIGHT_YELLOW, "β"),
        DevelopmentState::Experimental => (colors::YELLOW, "α"),
        DevelopmentState::Incomplete => (colors::RED, "✗"),
    };
    output.push_str(&format!(
        "{}▸ Status:{} {}{} {}{}\n",
        colors::BRIGHT_CYAN,
        colors::RESET,
        status_color,
        status_symbol,
        metadata.state.as_str(),
        colors::RESET
    ));

    // Show implementation details
    if !metadata.implementation.is_empty() {
        output.push_str(&format!(
            "{}▸ Implementation:{} {}{}{}\n",
            colors::BRIGHT_CYAN,
            colors::RESET,
            colors::DIM,
            metadata.implementation,
            colors::RESET
        ));
    }

    // Show LLM control scope
    if !metadata.llm_control.is_empty() {
        output.push_str(&format!(
            "{}▸ LLM Control:{} {}{}{}\n",
            colors::BRIGHT_CYAN,
            colors::RESET,
            colors::DIM,
            metadata.llm_control,
            colors::RESET
        ));
    }

    // Show E2E testing approach
    if !metadata.e2e_testing.is_empty() {
        output.push_str(&format!(
            "{}▸ E2E Testing:{} {}{}{}\n",
            colors::BRIGHT_CYAN,
            colors::RESET,
            colors::DIM,
            metadata.e2e_testing,
            colors::RESET
        ));
    }

    // Show notes if present
    if let Some(notes) = metadata.notes {
        output.push_str(&format!(
            "{}▸ Notes:{} {}{}{}\n",
            colors::BRIGHT_CYAN,
            colors::RESET,
            colors::YELLOW,
            notes,
            colors::RESET
        ));
    }

    // Show privilege requirement.
    //
    // Colour comes from `severity()` and text from `description()` rather than a
    // match here: this duplicated both, so adding a requirement variant meant
    // editing this renderer in lockstep or failing to compile.
    use crate::protocol::metadata::PrivilegeSeverity;
    let priv_color = match metadata.privilege_requirement.severity() {
        PrivilegeSeverity::None => colors::GREEN,
        PrivilegeSeverity::Elevated => colors::YELLOW,
        PrivilegeSeverity::Root => colors::RED,
    };
    let priv_text = metadata.privilege_requirement.description();
    output.push_str(&format!(
        "{}▸ Privilege Required:{} {}{}{}\n",
        colors::BRIGHT_CYAN,
        colors::RESET,
        priv_color,
        priv_text,
        colors::RESET
    ));

    output.push_str(&format!(
        "\n{}▸ Description:{}\n  {}{}{}\n\n",
        colors::BRIGHT_CYAN,
        colors::RESET,
        colors::DIM,
        protocol.description(),
        colors::RESET
    ));

    // Try to get protocol instance for detailed info
    if let Some(protocol) = registry.get(&parsed_protocol_name) {
        // Show startup parameters
        let params = protocol.get_startup_parameters();
        if !params.is_empty() {
            output.push_str(&format!(
                "\n{}━━━ Startup Parameters ━━━{}\n\n",
                colors::BRIGHT_CYAN,
                colors::RESET
            ));
            output.push_str(&format!(
                "{}These parameters can be provided when opening the server:{}\n\n",
                colors::DIM,
                colors::RESET
            ));
            for param in params {
                output.push_str(&format!(
                    "{}•{} {}{}{} ({}{}{}): {}\n",
                    colors::BLUE,
                    colors::RESET,
                    colors::BOLD,
                    param.name,
                    colors::RESET,
                    colors::YELLOW,
                    param.type_hint,
                    colors::RESET,
                    param.description
                ));
                if param.required {
                    output.push_str(&format!("  {}[REQUIRED]{}\n", colors::RED, colors::RESET));
                }
                if let Ok(pretty_json) = serde_json::to_string_pretty(&param.example) {
                    output.push_str(&format!(
                        "  {}Example:{} {}{}{}\n",
                        colors::DIM,
                        colors::RESET,
                        colors::GREY,
                        pretty_json,
                        colors::RESET
                    ));
                }
            }
            output.push('\n');
        }

        // Show event types
        let event_types = protocol.get_event_types();
        if !event_types.is_empty() {
            output.push_str(&format!(
                "\n{}━━━ Event Types ━━━{}\n\n",
                colors::BRIGHT_CYAN,
                colors::RESET
            ));
            output.push_str(&format!(
                "{}This protocol can emit the following network events:{}\n\n",
                colors::DIM,
                colors::RESET
            ));

            for event_type in event_types {
                output.push_str(&format!(
                    "{}▸ Event: {}{}{}\n",
                    colors::MAGENTA,
                    colors::BOLD,
                    event_type.id,
                    colors::RESET
                ));
                output.push_str(&format!(
                    "  {}{}{}\n\n",
                    colors::GREY,
                    event_type.description,
                    colors::RESET
                ));

                // Show event parameters
                if !event_type.parameters.is_empty() {
                    output.push_str(&format!("  {}Event Data:{}\n", colors::CYAN, colors::RESET));
                    for param in &event_type.parameters {
                        output.push_str(&format!(
                            "    {}•{} {}{}{} ({}{}{}): {}\n",
                            colors::BLUE,
                            colors::RESET,
                            colors::BOLD,
                            param.name,
                            colors::RESET,
                            colors::YELLOW,
                            param.type_hint,
                            colors::RESET,
                            param.description
                        ));
                    }
                    output.push('\n');
                }

                // Show available actions for this event
                if !event_type.actions.is_empty() {
                    output.push_str(&format!(
                        "  {}Available Actions:{}\n",
                        colors::CYAN,
                        colors::RESET
                    ));
                    for action in &event_type.actions {
                        output.push_str(&format!(
                            "    {}•{} {}{}{}: {}\n",
                            colors::GREEN,
                            colors::RESET,
                            colors::BOLD,
                            action.name,
                            colors::RESET,
                            action.description
                        ));

                        // Show action parameters
                        if !action.parameters.is_empty() {
                            output.push_str(&format!(
                                "      {}Parameters:{}\n",
                                colors::DIM,
                                colors::RESET
                            ));
                            for param in &action.parameters {
                                output.push_str(&format!(
                                    "        - {}{}{} ({}{}{}): {}\n",
                                    colors::BOLD,
                                    param.name,
                                    colors::RESET,
                                    colors::YELLOW,
                                    param.type_hint,
                                    colors::RESET,
                                    param.description
                                ));
                            }
                        }

                        // Show example with pretty JSON
                        if let Ok(pretty_json) = serde_json::to_string_pretty(&action.example) {
                            output.push_str(&format!(
                                "      {}Example:{} {}{}{}\n",
                                colors::DIM,
                                colors::RESET,
                                colors::GREY,
                                pretty_json,
                                colors::RESET
                            ));
                        }
                    }
                    output.push('\n');
                } else {
                    output.push_str(&format!(
                        "  {}No specific actions available for this event.{}\n\n",
                        colors::GREY,
                        colors::RESET
                    ));
                }
            }
        } else {
            output.push_str(&format!(
                "\n{}━━━ Event Types ━━━{}\n\n",
                colors::BRIGHT_CYAN,
                colors::RESET
            ));
            output.push_str(&format!(
                "{}This protocol hasn't documented its event types yet.{}\n\n",
                colors::GREY,
                colors::RESET
            ));
        }

        // Show async actions (user-triggered)
        let async_actions = protocol.get_async_actions(&crate::state::app_state::AppState::new());
        if !async_actions.is_empty() {
            output.push_str(&format!(
                "\n{}━━━ User-Triggered Actions ━━━{}\n\n",
                colors::BRIGHT_CYAN,
                colors::RESET
            ));
            output.push_str(&format!("{}These actions can be triggered by user input (not tied to network events):{}\n\n",
                colors::DIM, colors::RESET));
            for action in async_actions {
                output.push_str(&format!(
                    "{}•{} {}{}{}: {}\n",
                    colors::GREEN,
                    colors::RESET,
                    colors::BOLD,
                    action.name,
                    colors::RESET,
                    action.description
                ));

                // Show parameters
                if !action.parameters.is_empty() {
                    output.push_str(&format!("  {}Parameters:{}\n", colors::CYAN, colors::RESET));
                    for param in &action.parameters {
                        output.push_str(&format!(
                            "    - {}{}{} ({}{}{}): {}\n",
                            colors::BOLD,
                            param.name,
                            colors::RESET,
                            colors::YELLOW,
                            param.type_hint,
                            colors::RESET,
                            param.description
                        ));
                    }
                }

                // Show example with pretty JSON
                if let Ok(pretty_json) = serde_json::to_string_pretty(&action.example) {
                    output.push_str(&format!(
                        "  {}Example:{} {}{}{}\n",
                        colors::DIM,
                        colors::RESET,
                        colors::GREY,
                        pretty_json,
                        colors::RESET
                    ));
                }
                output.push('\n');
            }
        }
    } else {
        output.push_str("## Documentation\n\n");
        output.push_str("Detailed documentation not available for this protocol.\n");
        output.push_str("This protocol may not implement the Server trait yet.\n");
    }

    Ok(output)
}