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
//! WebSocket (RFC 6455) client actions, events and metadata.
//!
//! The client mirrors the server's vocabulary deliberately: `send_websocket_text`,
//! `send_websocket_binary` (with the same `data` + `encoding` pair), `send_websocket_ping` and
//! `close_websocket` mean the same thing in both directions, so a prompt written for one side
//! reads correctly on the other. Masking is the only asymmetry and it is handled by the
//! framing layer — RFC 6455 §5.3 requires every client-to-server frame to be masked with a
//! fresh 32-bit key, and `tungstenite` does that for `Role::Client` without being asked.

use crate::llm::actions::{
    client_trait::{Client, ClientActionResult},
    protocol_trait::Protocol,
    ActionDefinition, Parameter, ParameterDefinition,
};
use crate::protocol::log_template::LogTemplate;
use crate::protocol::EventType;
use crate::server::websocket::actions::{decode_outbound_payload, WsOut};
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;
use tokio::sync::mpsc;
use tracing::debug;

/// WebSocket client action handler.
///
/// Two shapes, as on the server side: the registry-wide instance used for documentation and
/// `connect()`, and a per-connection instance that owns the outbound channel and can therefore
/// actually put a frame on the wire.
pub struct WebSocketClientProtocol {
    out_tx: Option<mpsc::UnboundedSender<WsOut>>,
}

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

impl WebSocketClientProtocol {
    pub fn new() -> Self {
        Self { out_tx: None }
    }

    pub fn for_connection(out_tx: mpsc::UnboundedSender<WsOut>) -> Self {
        Self {
            out_tx: Some(out_tx),
        }
    }

    fn send(&self, msg: WsOut) -> Result<()> {
        let tx = self
            .out_tx
            .as_ref()
            .context("This WebSocket client action needs an open connection")?;
        tx.send(msg)
            .map_err(|_| anyhow::anyhow!("WebSocket connection is already closed"))
    }
}

impl Protocol for WebSocketClientProtocol {
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![
            ParameterDefinition {
                name: "path".to_string(),
                type_hint: "string".to_string(),
                description:
                    "Request path (and optional query string) to open the WebSocket on, e.g. \
                     \"/ws\" or \"/stream?token=abc\". Defaults to \"/\"."
                        .to_string(),
                required: false,
                example: json!("/ws"),
                default: None,
            },
            ParameterDefinition {
                name: "subprotocols".to_string(),
                type_hint: "array".to_string(),
                description:
                    "Subprotocols to offer in Sec-WebSocket-Protocol, most preferred first. The \
                     server picks at most one and it is reported in the \
                     websocket_client_connected event."
                        .to_string(),
                required: false,
                example: json!(["chat", "superchat"]),
                default: None,
            },
            ParameterDefinition {
                name: "headers".to_string(),
                type_hint: "object".to_string(),
                description: "Extra request headers to send with the upgrade, e.g. \
                     {\"Authorization\": \"Bearer …\", \"Origin\": \"https://example.com\"}. \
                     The RFC 6455 handshake headers are set automatically and cannot be \
                     overridden."
                    .to_string(),
                required: false,
                example: json!({"Origin": "https://example.com"}),
                default: None,
            },
            ParameterDefinition {
                name: "permessage_deflate".to_string(),
                type_hint: "boolean".to_string(),
                description: "Offer RFC 7692 permessage-deflate in the upgrade, so the server may \
                     compress messages in both directions (default true). Messages reach the \
                     handler as plain text either way; whether the server accepted is reported \
                     in websocket_client_connected."
                    .to_string(),
                required: false,
                example: json!(true),
                default: Some(json!(super::DEFAULT_PERMESSAGE_DEFLATE)),
            },
        ]
    }

    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        client_actions()
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        client_actions()
    }

    fn protocol_name(&self) -> &'static str {
        // Same canonical name and stack as the server side, which is the repo's convention
        // (TcpClientProtocol is "TCP" too). The two registries are separate, so there is no
        // collision, and `base_stack: "websocket"` resolves on both.
        "WebSocket"
    }

    fn get_event_types(&self) -> Vec<EventType> {
        get_websocket_client_event_types()
    }

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

    fn keywords(&self) -> Vec<&'static str> {
        // The client registry's `parse_from_str` has no protocol-name step and matches
        // keywords by substring, so the bare "websocket" is what makes `base_stack:
        // "websocket"` (and "websocket_client") resolve at all.
        vec![
            "websocket",
            "web socket",
            "websocket client",
            "connect to websocket",
            "rfc 6455",
        ]
    }

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

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Experimental)
            .privilege_requirement(PrivilegeRequirement::None)
            .implementation(
                "tokio-tungstenite 0.21 connect_async; client-to-server masking, fragmentation \
                 and the close handshake are handled by the framing layer",
            )
            .llm_control(
                "Every text and binary frame sent, ping payloads, the close code and reason, \
                 and which subprotocols to offer at connect time",
            )
            .e2e_testing(
                "The NetGet WebSocket server and websocat 1.14.1 in server mode (-s); RFC 7692 \
                 permessage-deflate against Python websockets 15 (12-bit windows, its \
                 default) and Node ws 8 (no context takeover), with a relay reading RSV1 off \
                 the wire (tests/client/websocket/deflate_real_server_test.rs)",
            )
            .notes(
                "ws:// only — wss:// is not supported because the tokio-tungstenite dependency \
                 is built without a TLS backend. permessage-deflate is offered by default \
                 (permessage_deflate) and applied beneath tungstenite. Pong frames are logged \
                 rather than surfaced as an event. Validated against websocat 1.14.1 running as \
                 the server.",
            )
            .build()
    }

    fn description(&self) -> &'static str {
        "WebSocket (RFC 6455) client for connecting to ws:// endpoints"
    }

    fn example_prompt(&self) -> &'static str {
        "Connect to the WebSocket at 127.0.0.1:9001/ws, send a subscribe message, and summarise every update"
    }

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

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

        StartupExamples::new(
            json!({
                "type": "open_client",
                "remote_addr": "127.0.0.1:9001",
                "base_stack": "websocket",
                "startup_params": {"path": "/ws", "subprotocols": ["chat"]},
                "instruction": "Connect, send {\"op\":\"subscribe\",\"channel\":\"ticker\"} once connected, \
                                and report each update you receive."
            }),
            json!({
                "type": "open_client",
                "remote_addr": "127.0.0.1:9001",
                "base_stack": "websocket",
                "event_handlers": [{
                    "event_pattern": "websocket_client_text_message",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "respond([{'type': 'send_websocket_text', 'text': event['text'].upper()}])"
                    }
                }]
            }),
            json!({
                "type": "open_client",
                "remote_addr": "127.0.0.1:9001",
                "base_stack": "websocket",
                "event_handlers": [
                    {
                        "event_pattern": "websocket_client_connected",
                        "handler": {
                            "type": "static",
                            "actions": [{"type": "send_websocket_text", "text": "hello"}]
                        }
                    },
                    {
                        "event_pattern": "websocket_client_text_message",
                        "handler": {
                            "type": "static",
                            "actions": [{"type": "close_websocket", "code": 1000, "reason": "done"}]
                        }
                    }
                ]
            }),
        )
    }
}

impl Client for WebSocketClientProtocol {
    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::websocket::WebSocketClient;
            WebSocketClient::connect_with_llm_actions(ctx).await
        })
    }

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

        match action_type {
            "send_websocket_text" => {
                let text = action
                    .get("text")
                    .and_then(|v| v.as_str())
                    .context("Missing 'text' for send_websocket_text")?
                    .to_string();
                debug!("WebSocket client -> text {} bytes", text.len());
                let bytes = text.len();
                self.send(WsOut::Text(text))?;
                Ok(ClientActionResult::Custom {
                    name: "send_websocket_text".to_string(),
                    data: json!({ "bytes": bytes }),
                })
            }
            "send_websocket_binary" => {
                let data = action
                    .get("data")
                    .and_then(|v| v.as_str())
                    .context("Missing 'data' for send_websocket_binary")?;
                let bytes =
                    decode_outbound_payload(data, action.get("encoding").and_then(|v| v.as_str()))?;
                debug!("WebSocket client -> binary {} bytes", bytes.len());
                let len = bytes.len();
                self.send(WsOut::Binary(bytes))?;
                Ok(ClientActionResult::Custom {
                    name: "send_websocket_binary".to_string(),
                    data: json!({ "bytes": len }),
                })
            }
            "send_websocket_ping" => {
                let payload = action
                    .get("payload")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .as_bytes()
                    .to_vec();
                if payload.len() > 125 {
                    return Err(anyhow::anyhow!(
                        "Ping payload is {} bytes; RFC 6455 §5.5 limits control frames to 125",
                        payload.len()
                    ));
                }
                self.send(WsOut::Ping(payload))?;
                Ok(ClientActionResult::Custom {
                    name: "send_websocket_ping".to_string(),
                    data: json!({}),
                })
            }
            "close_websocket" => {
                let code = action.get("code").and_then(|v| v.as_u64()).unwrap_or(1000);
                let valid = (1000..=1003).contains(&code)
                    || (1007..=1011).contains(&code)
                    || (3000..=4999).contains(&code);
                if !valid {
                    return Err(anyhow::anyhow!(
                        "close code {} is not sendable; use 1000-1003, 1007-1011 or 3000-4999 \
                         (1005, 1006 and 1015 are reserved)",
                        code
                    ));
                }
                let reason = action
                    .get("reason")
                    .and_then(|v| v.as_str())
                    .unwrap_or("")
                    .to_string();
                if reason.len() > 123 {
                    return Err(anyhow::anyhow!(
                        "close reason is {} bytes; the limit is 123",
                        reason.len()
                    ));
                }
                self.send(WsOut::Close {
                    code: code as u16,
                    reason,
                })?;
                Ok(ClientActionResult::Disconnect)
            }
            "disconnect" => {
                // Drop the TCP connection without a closing handshake. `close_websocket` is
                // the polite form and should be preferred.
                Ok(ClientActionResult::Disconnect)
            }
            "wait_for_websocket_data" => Ok(ClientActionResult::WaitForMore),
            _ => Err(anyhow::anyhow!(
                "Unknown WebSocket client action: {}",
                action_type
            )),
        }
    }
}

// ============================================================================
// Action definitions
// ============================================================================

fn client_actions() -> Vec<ActionDefinition> {
    vec![
        send_text_action(),
        send_binary_action(),
        send_ping_action(),
        wait_for_data_action(),
        close_action(),
        disconnect_action(),
    ]
}

fn send_text_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_websocket_text".to_string(),
        description: "Send a WebSocket text frame to the server.".to_string(),
        parameters: vec![Parameter {
            name: "text".to_string(),
            type_hint: "string".to_string(),
            description: "The message body, sent as UTF-8. Put serialised JSON here as a string."
                .to_string(),
            required: true,
        }],
        example: json!({"type": "send_websocket_text", "text": "{\"op\":\"subscribe\"}"}),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> WS text")
                .with_trace("WebSocket client text: {preview(text,200)}"),
        ),
    }
}

fn send_binary_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_websocket_binary".to_string(),
        description:
            "Send a WebSocket binary frame to the server. 'data' plus 'encoding' is the same \
             pair a websocket_client_binary_message event carries, so echoing is passing both \
             fields straight through."
                .to_string(),
        parameters: vec![
            Parameter {
                name: "data".to_string(),
                type_hint: "string".to_string(),
                description: "The payload, interpreted according to 'encoding'.".to_string(),
                required: true,
            },
            Parameter {
                name: "encoding".to_string(),
                type_hint: "string".to_string(),
                description:
                    "\"utf8\" (default) sends the characters of 'data' unchanged; \"base64\" \
                     and \"hex\" decode 'data' into bytes. There is no auto-detection."
                        .to_string(),
                required: false,
            },
        ],
        example: json!({"type": "send_websocket_binary", "data": "AP/+AQ==", "encoding": "base64"}),
        log_template: Some(LogTemplate::new().with_info("-> WS binary")),
    }
}

fn send_ping_action() -> ActionDefinition {
    ActionDefinition {
        name: "send_websocket_ping".to_string(),
        description:
            "Send a ping control frame. The server's pong is logged and does not raise an event."
                .to_string(),
        parameters: vec![Parameter {
            name: "payload".to_string(),
            type_hint: "string".to_string(),
            description: "Optional payload, at most 125 bytes.".to_string(),
            required: false,
        }],
        example: json!({"type": "send_websocket_ping", "payload": "keepalive"}),
        log_template: Some(LogTemplate::new().with_debug("WebSocket client ping")),
    }
}

fn wait_for_data_action() -> ActionDefinition {
    ActionDefinition {
        name: "wait_for_websocket_data".to_string(),
        description:
            "Do not act on this message yet; hold it and join the next message of the same kind \
             onto it before deciding."
                .to_string(),
        parameters: vec![],
        example: json!({"type": "wait_for_websocket_data"}),
        log_template: Some(LogTemplate::new().with_debug("WebSocket client waiting for more")),
    }
}

fn close_action() -> ActionDefinition {
    ActionDefinition {
        name: "close_websocket".to_string(),
        description:
            "Close politely: send a close frame with a status code and let the server answer."
                .to_string(),
        parameters: vec![
            Parameter {
                name: "code".to_string(),
                type_hint: "integer".to_string(),
                description:
                    "Close status code (default 1000, normal). 1001 going away, 1008 policy \
                     violation, 1011 internal error, or 3000-4999 for your own meanings."
                        .to_string(),
                required: false,
            },
            Parameter {
                name: "reason".to_string(),
                type_hint: "string".to_string(),
                description: "Optional explanation, at most 123 bytes.".to_string(),
                required: false,
            },
        ],
        example: json!({"type": "close_websocket", "code": 1000, "reason": "done"}),
        log_template: Some(LogTemplate::new().with_info("WS client close {code}")),
    }
}

fn disconnect_action() -> ActionDefinition {
    ActionDefinition {
        name: "disconnect".to_string(),
        description:
            "Drop the connection immediately without a closing handshake. Prefer close_websocket."
                .to_string(),
        parameters: vec![],
        example: json!({"type": "disconnect"}),
        log_template: Some(LogTemplate::new().with_info("WS client disconnect")),
    }
}

// ============================================================================
// Event types — every one of these is emitted by src/client/websocket/mod.rs
// ============================================================================

fn connected_actions() -> Vec<ActionDefinition> {
    vec![
        send_text_action(),
        send_binary_action(),
        send_ping_action(),
        close_action(),
        disconnect_action(),
    ]
}

pub static WEBSOCKET_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "websocket_client_connected",
        "The WebSocket handshake completed. Send the first message now if the protocol expects \
         the client to speak first.",
        json!({"type": "send_websocket_text", "text": "{\"op\":\"subscribe\"}"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "remote_addr".to_string(),
            type_hint: "string".to_string(),
            description: "Address of the server that was connected to".to_string(),
            required: true,
        },
        Parameter {
            name: "path".to_string(),
            type_hint: "string".to_string(),
            description: "Request path the connection was opened on".to_string(),
            required: true,
        },
        Parameter {
            name: "subprotocol".to_string(),
            type_hint: "string".to_string(),
            description:
                "The subprotocol the server agreed on, or empty if it chose none. Never a value \
                 that was not offered."
                    .to_string(),
            required: false,
        },
        Parameter {
            name: "permessage_deflate".to_string(),
            type_hint: "object".to_string(),
            description: "The RFC 7692 compression parameters the server accepted, or null when \
                 the connection is uncompressed"
                .to_string(),
            required: false,
        },
    ])
    .with_actions(connected_actions())
    .with_log_template(
        LogTemplate::new()
            .with_info("WS client connected to {remote_addr}{path}")
            .with_debug("WebSocket client connected subprotocol={subprotocol}"),
    )
});

pub static WEBSOCKET_CLIENT_TEXT_MESSAGE_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "websocket_client_text_message",
        "A text message arrived from the server.",
        json!({"type": "send_websocket_text", "text": "ack"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "text".to_string(),
            type_hint: "string".to_string(),
            description: "The complete message, reassembled from any fragments".to_string(),
            required: true,
        },
        Parameter {
            name: "message_bytes".to_string(),
            type_hint: "integer".to_string(),
            description: "Length of the message in bytes".to_string(),
            required: true,
        },
    ])
    .with_actions({
        let mut a = connected_actions();
        a.push(wait_for_data_action());
        a
    })
    .with_alternative_example(json!({"type": "close_websocket", "code": 1000, "reason": "done"}))
    .with_log_template(
        LogTemplate::new()
            .with_info("WS client <- text {message_bytes}B")
            .with_trace("WebSocket client text: {preview(text,200)}"),
    )
});

pub static WEBSOCKET_CLIENT_BINARY_MESSAGE_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "websocket_client_binary_message",
        "A binary message arrived from the server. To echo it back unchanged, pass this event's \
         'data' AND its 'encoding' straight into send_websocket_binary.",
        json!({"type": "send_websocket_binary", "data": "AP/+AQ==", "encoding": "base64"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "data".to_string(),
            type_hint: "string".to_string(),
            description: "The payload, read according to the 'encoding' field".to_string(),
            required: true,
        },
        Parameter {
            name: "encoding".to_string(),
            type_hint: "string".to_string(),
            description: "\"utf8\" when every byte is printable ASCII, otherwise \"base64\". \
                 send_websocket_binary accepts the same values, so the pair round-trips."
                .to_string(),
            required: true,
        },
        Parameter {
            name: "message_bytes".to_string(),
            type_hint: "integer".to_string(),
            description: "Length of the decoded message in bytes".to_string(),
            required: true,
        },
    ])
    .with_actions({
        let mut a = connected_actions();
        a.push(wait_for_data_action());
        a
    })
    .with_log_template(LogTemplate::new().with_info("WS client <- binary {message_bytes}B"))
});

pub static WEBSOCKET_CLIENT_CLOSED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "websocket_client_closed",
        "The server closed the connection. Record what happened; nothing more can be sent.",
        json!({"type": "show_message", "message": "server closed the WebSocket"}),
    )
    .with_parameters(vec![
        Parameter {
            name: "code".to_string(),
            type_hint: "integer".to_string(),
            description: "Close status code, or 1005 when the server sent none".to_string(),
            required: true,
        },
        Parameter {
            name: "reason".to_string(),
            type_hint: "string".to_string(),
            description: "Close reason text, or empty".to_string(),
            required: false,
        },
    ])
    // Nothing can go on the wire after the closing handshake, so the common actions
    // (show_message, set_memory, append_to_log) are the only honest vocabulary here.
    .with_no_actions()
    .with_log_template(LogTemplate::new().with_info("WS client closed code={code}"))
});

pub fn get_websocket_client_event_types() -> Vec<EventType> {
    vec![
        WEBSOCKET_CLIENT_CONNECTED_EVENT.clone(),
        WEBSOCKET_CLIENT_TEXT_MESSAGE_EVENT.clone(),
        WEBSOCKET_CLIENT_BINARY_MESSAGE_EVENT.clone(),
        WEBSOCKET_CLIENT_CLOSED_EVENT.clone(),
    ]
}