netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
653
654
655
656
657
658
659
660
661
662
//! CouchDB client protocol actions implementation

use crate::llm::actions::{
    client_trait::{Client, ClientActionResult},
    protocol_trait::Protocol,
    ActionDefinition, Parameter,
};
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::Result;
use serde_json::json;
use std::sync::LazyLock;

/// CouchDB client connected event
pub static COUCHDB_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "couchdb_connected",
        "CouchDB client successfully connected to server",
        json!({"type": "list_databases"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "remote_addr".to_string(),
            type_hint: "string".to_string(),
            description: "CouchDB server address".to_string(),
            required: true,
        },
        Parameter {
            name: "server_info".to_string(),
            type_hint: "object".to_string(),
            description: "Server welcome information (version, etc.)".to_string(),
            required: false,
        },
    ])
});

/// CouchDB client response received event
pub static COUCHDB_CLIENT_RESPONSE_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "couchdb_response_received",
        "Response received from CouchDB server",
        json!({"type": "get_document", "database": "mydb", "doc_id": "user1"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "operation".to_string(),
            type_hint: "string".to_string(),
            description: "The operation that was performed".to_string(),
            required: true,
        },
        Parameter {
            name: "success".to_string(),
            type_hint: "boolean".to_string(),
            description: "Whether the operation succeeded".to_string(),
            required: true,
        },
        Parameter {
            name: "data".to_string(),
            type_hint: "object".to_string(),
            description: "Response data from the operation".to_string(),
            required: false,
        },
        Parameter {
            name: "error".to_string(),
            type_hint: "string".to_string(),
            description: "Error message if operation failed".to_string(),
            required: false,
        },
    ])
});

/// CouchDB client conflict event
pub static COUCHDB_CLIENT_CONFLICT_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "couchdb_conflict",
        "Document update conflict detected (revision mismatch)",
        json!({"type": "get_document", "database": "mydb", "doc_id": "user1"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "database".to_string(),
            type_hint: "string".to_string(),
            description: "Database name".to_string(),
            required: true,
        },
        Parameter {
            name: "doc_id".to_string(),
            type_hint: "string".to_string(),
            description: "Document ID".to_string(),
            required: true,
        },
        Parameter {
            name: "expected_rev".to_string(),
            type_hint: "string".to_string(),
            description: "The revision that was expected".to_string(),
            required: false,
        },
    ])
});

/// CouchDB client change detected event (from changes feed)
pub static COUCHDB_CLIENT_CHANGE_DETECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "couchdb_change_detected",
        "Document change detected in changes feed",
        json!({"type": "get_document", "database": "mydb", "doc_id": "user1"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "database".to_string(),
            type_hint: "string".to_string(),
            description: "Database name".to_string(),
            required: true,
        },
        Parameter {
            name: "seq".to_string(),
            type_hint: "string".to_string(),
            description: "Sequence number".to_string(),
            required: true,
        },
        Parameter {
            name: "doc_id".to_string(),
            type_hint: "string".to_string(),
            description: "Changed document ID".to_string(),
            required: true,
        },
        Parameter {
            name: "changes".to_string(),
            type_hint: "array".to_string(),
            description: "Array of revision changes".to_string(),
            required: true,
        },
        Parameter {
            name: "deleted".to_string(),
            type_hint: "boolean".to_string(),
            description: "Whether the document was deleted".to_string(),
            required: false,
        },
    ])
});

/// CouchDB client protocol action handler
pub struct CouchDbClientProtocol;

impl CouchDbClientProtocol {
    pub fn new() -> Self {
        Self
    }
}

// Implement Protocol trait (common functionality)
impl Protocol for CouchDbClientProtocol {
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        vec![
            ActionDefinition {
                name: "create_database".to_string(),
                description: "Create a new database".to_string(),
                parameters: vec![Parameter {
                    name: "database".to_string(),
                    type_hint: "string".to_string(),
                    description: "Database name".to_string(),
                    required: true,
                }],
                example: json!({
                    "type": "create_database",
                    "database": "mydb"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "delete_database".to_string(),
                description: "Delete a database".to_string(),
                parameters: vec![Parameter {
                    name: "database".to_string(),
                    type_hint: "string".to_string(),
                    description: "Database name".to_string(),
                    required: true,
                }],
                example: json!({
                    "type": "delete_database",
                    "database": "mydb"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "list_databases".to_string(),
                description: "List all databases".to_string(),
                parameters: vec![],
                example: json!({
                    "type": "list_databases"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "create_document".to_string(),
                description: "Create a new document".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "doc_id".to_string(),
                        type_hint: "string".to_string(),
                        description: "Document ID (optional, auto-generated if not provided)"
                            .to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "document".to_string(),
                        type_hint: "object".to_string(),
                        description: "Document data as JSON object".to_string(),
                        required: true,
                    },
                ],
                example: json!({
                    "type": "create_document",
                    "database": "mydb",
                    "doc_id": "user1",
                    "document": {"name": "Alice", "age": 30}
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "get_document".to_string(),
                description: "Retrieve a document".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "doc_id".to_string(),
                        type_hint: "string".to_string(),
                        description: "Document ID".to_string(),
                        required: true,
                    },
                ],
                example: json!({
                    "type": "get_document",
                    "database": "mydb",
                    "doc_id": "user1"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "update_document".to_string(),
                description: "Update an existing document (requires current revision)".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "doc_id".to_string(),
                        type_hint: "string".to_string(),
                        description: "Document ID".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "document".to_string(),
                        type_hint: "object".to_string(),
                        description: "Updated document data (must include _rev)".to_string(),
                        required: true,
                    },
                ],
                example: json!({
                    "type": "update_document",
                    "database": "mydb",
                    "doc_id": "user1",
                    "document": {"_rev": "1-abc", "name": "Alice", "age": 31}
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "delete_document".to_string(),
                description: "Delete a document (requires current revision)".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "doc_id".to_string(),
                        type_hint: "string".to_string(),
                        description: "Document ID".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "rev".to_string(),
                        type_hint: "string".to_string(),
                        description: "Current document revision".to_string(),
                        required: true,
                    },
                ],
                example: json!({
                    "type": "delete_document",
                    "database": "mydb",
                    "doc_id": "user1",
                    "rev": "2-abc"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "bulk_docs".to_string(),
                description: "Perform bulk document operations".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "docs".to_string(),
                        type_hint: "array".to_string(),
                        description: "Array of documents to create/update".to_string(),
                        required: true,
                    },
                ],
                example: json!({
                    "type": "bulk_docs",
                    "database": "mydb",
                    "docs": [
                        {"_id": "doc1", "name": "Alice"},
                        {"_id": "doc2", "name": "Bob"}
                    ]
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "list_documents".to_string(),
                description: "List all documents in a database".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "include_docs".to_string(),
                        type_hint: "boolean".to_string(),
                        description: "Whether to include full document content".to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "list_documents",
                    "database": "mydb",
                    "include_docs": false
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "query_view".to_string(),
                description: "Query a MapReduce view".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "design_doc".to_string(),
                        type_hint: "string".to_string(),
                        description: "Design document name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "view_name".to_string(),
                        type_hint: "string".to_string(),
                        description: "View name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "params".to_string(),
                        type_hint: "object".to_string(),
                        description: "Query parameters (key, limit, skip, etc.)".to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "query_view",
                    "database": "mydb",
                    "design_doc": "users",
                    "view_name": "by_age",
                    "params": {"limit": 10}
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "watch_changes".to_string(),
                description: "Start watching changes feed".to_string(),
                parameters: vec![
                    Parameter {
                        name: "database".to_string(),
                        type_hint: "string".to_string(),
                        description: "Database name".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "since".to_string(),
                        type_hint: "string".to_string(),
                        description: "Start sequence (or 'now')".to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "feed".to_string(),
                        type_hint: "string".to_string(),
                        description: "Feed type: normal, longpoll, or continuous".to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "watch_changes",
                    "database": "mydb",
                    "since": "now",
                    "feed": "longpoll"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "disconnect".to_string(),
                description: "Disconnect from the CouchDB server".to_string(),
                parameters: vec![],
                example: json!({
                    "type": "disconnect"
                }),
                log_template: None,
            },
            // Declared, not just accepted. The executor took any name at all, so this appeared
            // to work while being advertised nowhere - the model could only reach it by
            // guessing, and every other guess was swallowed just as silently.
            ActionDefinition {
                name: "wait_for_more".to_string(),
                description: "Do nothing and wait for the next response. Correct when the \
                    previous operation needs no follow-up."
                    .to_string(),
                parameters: vec![],
                example: json!({
                    "type": "wait_for_more"
                }),
                log_template: None,
            },
        ]
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        // Sync actions are same as async for CouchDB client (response-driven)
        self.get_async_actions(&AppState::new())
    }

    fn protocol_name(&self) -> &'static str {
        "CouchDB"
    }

    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            COUCHDB_CLIENT_CONNECTED_EVENT.clone(),
            COUCHDB_CLIENT_RESPONSE_RECEIVED_EVENT.clone(),
            COUCHDB_CLIENT_CONFLICT_EVENT.clone(),
            COUCHDB_CLIENT_CHANGE_DETECTED_EVENT.clone(),
        ]
    }

    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>HTTP>COUCHDB"
    }

    fn keywords(&self) -> Vec<&'static str> {
        vec!["couchdb client", "connect to couchdb", "couchdb"]
    }

    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Experimental)
            .implementation("couch_rs library with LLM action control")
            .llm_control("Full control over CouchDB operations")
            .e2e_testing("NetGet CouchDB server")
            .build()
    }

    fn description(&self) -> &'static str {
        "CouchDB client for document database operations"
    }

    fn example_prompt(&self) -> &'static str {
        "Connect to CouchDB at localhost:5984 and create a database called 'myapp'"
    }

    fn group_name(&self) -> &'static str {
        "Database"
    }

    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        use serde_json::json;

        StartupExamples::new(
            // LLM mode: LLM controls CouchDB operations
            json!({
                "type": "open_client",
                "remote_addr": "localhost:5984",
                "base_stack": "couchdb",
                "instruction": "List all databases and then get documents from the 'users' database"
            }),
            // Script mode: Code-based deterministic responses
            json!({
                "type": "open_client",
                "remote_addr": "localhost:5984",
                "base_stack": "couchdb",
                "event_handlers": [{
                    "event_pattern": "couchdb_response_received",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "<couchdb_client_handler>"
                    }
                }]
            }),
            // Static mode: Fixed CouchDB list databases on connect
            json!({
                "type": "open_client",
                "remote_addr": "localhost:5984",
                "base_stack": "couchdb",
                "event_handlers": [
                    {
                        "event_pattern": "couchdb_connected",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "list_databases"
                            }]
                        }
                    },
                    {
                        "event_pattern": "couchdb_response_received",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "disconnect"
                            }]
                        }
                    }
                ]
            }),
        )
    }

    fn get_startup_parameters(&self) -> Vec<crate::llm::actions::ParameterDefinition> {
        vec![
            crate::llm::actions::ParameterDefinition {
                name: "username".to_string(),
                type_hint: "string".to_string(),
                description: "CouchDB username (for basic auth)".to_string(),
                required: false,
                example: json!("admin"),
                default: None,
            },
            crate::llm::actions::ParameterDefinition {
                name: "password".to_string(),
                type_hint: "string".to_string(),
                description: "CouchDB password (for basic auth)".to_string(),
                required: false,
                example: json!("password"),
                default: None,
            },
        ]
    }
}

// Implement Client trait (client-specific functionality)
impl Client for CouchDbClientProtocol {
    fn connect(
        &self,
        ctx: crate::protocol::ConnectContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            use crate::client::couchdb::CouchDbClient;

            let username = ctx
                .startup_params
                .as_ref()
                .map(|p| p.get_optional_string("username"))
                .transpose()?
                .flatten();

            let password = ctx
                .startup_params
                .as_ref()
                .map(|p| p.get_optional_string("password"))
                .transpose()?
                .flatten();

            CouchDbClient::connect_with_llm_actions(
                ctx.remote_addr,
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.client_id,
                username,
                password,
            )
            .await
        })
    }

    fn execute_action(&self, action: serde_json::Value) -> Result<ClientActionResult> {
        let action_type = action
            .get("type")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing action type"))?;

        // Execution happens in the read loop in mod.rs, which dispatches on this name - so an
        // unrecognised one is passed on and quietly ignored there. Validating here instead
        // means a typo, an action from another protocol, or a common action this client cannot
        // run is reported to the model, which then gets a chance to correct it. Returning Ok
        // for everything also stopped the repair loop from ever firing.
        const COUCHDB_CLIENT_ACTIONS: &[&str] = &[
            "create_database",
            "delete_database",
            "list_databases",
            "create_document",
            "get_document",
            "update_document",
            "delete_document",
            "list_documents",
            "bulk_docs",
            "query_view",
            "watch_changes",
            "disconnect",
            "wait_for_more",
        ];

        if !COUCHDB_CLIENT_ACTIONS.contains(&action_type) {
            return Err(anyhow::anyhow!(
                "Unknown CouchDB client action '{}'. Valid actions: {}",
                action_type,
                COUCHDB_CLIENT_ACTIONS.join(", ")
            ));
        }

        Ok(ClientActionResult::Custom {
            name: action_type.to_string(),
            data: action,
        })
    }
}