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
//! IPP client protocol actions implementation

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

/// IPP client connected event
pub static IPP_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ipp_connected",
        "IPP client initialized and ready to send print operations",
        json!({"type": "get_printer_attributes"}),
    )
    .with_parameters(vec![Parameter {
        name: "printer_uri".to_string(),
        type_hint: "string".to_string(),
        description: "IPP printer URI".to_string(),
        required: true,
    }])
});

/// IPP client response received event
pub static IPP_CLIENT_RESPONSE_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ipp_response_received",
        "IPP operation response received from printer",
        json!({"type": "get_job_attributes", "job_id": 123}),
    )
    .with_parameters(vec![
        Parameter {
            name: "operation".to_string(),
            type_hint: "string".to_string(),
            description:
                "IPP operation name (get_printer_attributes, print_job, get_job_attributes)"
                    .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: "response".to_string(),
            type_hint: "object".to_string(),
            description: "Response data from the printer".to_string(),
            required: true,
        },
    ])
});

/// IPP client protocol action handler
pub struct IppClientProtocol;

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

// Implement Protocol trait (common functionality)
impl Protocol for IppClientProtocol {
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![ParameterDefinition {
            name: "printer_path".to_string(),
            description: "Path to the printer on the server (e.g., /printers/test-printer)"
                .to_string(),
            type_hint: "string".to_string(),
            required: false,
            example: json!("/printers/test-printer"),
            default: None,
        }]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        vec![
            ActionDefinition {
                name: "get_printer_attributes".to_string(),
                description: "Query printer capabilities and status".to_string(),
                parameters: vec![],
                example: json!({
                    "type": "get_printer_attributes"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "print_job".to_string(),
                description: "Submit a print job to the printer".to_string(),
                parameters: vec![
                    Parameter {
                        name: "job_name".to_string(),
                        type_hint: "string".to_string(),
                        description: "Name/title for the print job".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "document_format".to_string(),
                        type_hint: "string".to_string(),
                        description:
                            "MIME type of the document (e.g., application/pdf, text/plain)"
                                .to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "document_data".to_string(),
                        type_hint: "string".to_string(),
                        description: "Document content. Plain text unless you set \
                                      encoding=\"base64\", which is how you send a PDF or any \
                                      other binary document. It is never guessed."
                            .to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "encoding".to_string(),
                        type_hint: "string".to_string(),
                        description: "How to read document_data: \"utf8\" (default, the text \
                                      itself) or \"base64\". Say which; the two are not \
                                      distinguishable from the content."
                            .to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "print_job",
                    "job_name": "Test Document",
                    "document_format": "text/plain",
                    "document_data": "Hello, Printer!\n"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "get_job_attributes".to_string(),
                description: "Query status and details of a specific print job".to_string(),
                parameters: vec![Parameter {
                    name: "job_id".to_string(),
                    type_hint: "number".to_string(),
                    description: "Job ID returned from print_job operation".to_string(),
                    required: true,
                }],
                example: json!({
                    "type": "get_job_attributes",
                    "job_id": 123
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "disconnect".to_string(),
                description: "Disconnect from the IPP printer".to_string(),
                parameters: vec![],
                example: json!({
                    "type": "disconnect"
                }),
                log_template: None,
            },
        ]
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            ActionDefinition {
                name: "wait_for_more".to_string(),
                description: "Do nothing and wait for the next IPP response. The correct answer \
                    when what arrived needs no follow-up -- without it the model has to \
                    invent an action it does not want."
                    .to_string(),
                parameters: vec![],
                example: json!({
                    "type": "wait_for_more"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "get_printer_attributes".to_string(),
                description: "Query printer capabilities in response to previous operation"
                    .to_string(),
                parameters: vec![],
                example: json!({
                    "type": "get_printer_attributes"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "get_job_attributes".to_string(),
                description: "Query job status after submitting a print job".to_string(),
                parameters: vec![Parameter {
                    name: "job_id".to_string(),
                    type_hint: "number".to_string(),
                    description: "Job ID to query".to_string(),
                    required: true,
                }],
                example: json!({
                    "type": "get_job_attributes",
                    "job_id": 123
                }),
                log_template: None,
            },
        ]
    }
    fn protocol_name(&self) -> &'static str {
        "IPP"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            EventType::new(
                "ipp_connected",
                "Triggered when IPP client is initialized",
                json!({"type": "get_printer_attributes"}),
            ),
            EventType::new(
                "ipp_response_received",
                "Triggered when IPP client receives a response from the printer",
                json!({"type": "get_job_attributes", "job_id": 123}),
            ),
        ]
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>HTTP>IPP"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec![
            "ipp",
            "ipp client",
            "internet printing protocol",
            "print",
            "printer",
        ]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
                .state(DevelopmentState::Experimental)
                .implementation("ipp crate 5.3 with AsyncIppClient")
                .llm_control("Full control over print operations: get-printer-attributes, print-job, get-job-attributes")
                .e2e_testing("CUPS test server or local IPP printer")
                .build()
    }
    fn description(&self) -> &'static str {
        "IPP client for printing and querying print jobs"
    }
    fn example_prompt(&self) -> &'static str {
        "Connect to ipp://localhost:631/printers/test-printer and query its capabilities"
    }
    fn group_name(&self) -> &'static str {
        "File & Print"
    }
    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        use serde_json::json;

        StartupExamples::new(
            // LLM mode: LLM controls print operations
            json!({
                "type": "open_client",
                "remote_addr": "localhost:631",
                "base_stack": "ipp",
                "startup_params": {
                    "printer_path": "/printers/test-printer"
                },
                "instruction": "Query printer capabilities and submit a test print job"
            }),
            // Script mode: Code-based print job handling
            json!({
                "type": "open_client",
                "remote_addr": "localhost:631",
                "base_stack": "ipp",
                "startup_params": {
                    "printer_path": "/printers/test-printer"
                },
                "event_handlers": [{
                    "event_pattern": "ipp_response_received",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "<ipp_client_handler>"
                    }
                }]
            }),
            // Static mode: Fixed printer query
            json!({
                "type": "open_client",
                "remote_addr": "localhost:631",
                "base_stack": "ipp",
                "startup_params": {
                    "printer_path": "/printers/test-printer"
                },
                "event_handlers": [
                    {
                        "event_pattern": "ipp_connected",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "get_printer_attributes"
                            }]
                        }
                    },
                    {
                        "event_pattern": "ipp_response_received",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "disconnect"
                            }]
                        }
                    }
                ]
            }),
        )
    }
}

// Implement Client trait (client-specific functionality)
impl Client for IppClientProtocol {
    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::ipp::IppClient;

            // `printer_path` is a declared startup parameter and was read by nothing: the URI
            // was built from `remote_addr` alone, so every `startup_params:
            // {"printer_path": "/printers/test-printer"}` in this file's own examples did
            // nothing, and an operator who set the field in the dashboard got the same request
            // as one who left it blank.
            //
            // It matters more here than a dead knob usually does. IPP addresses a *printer*,
            // not a host: `http://host:631/printers/foo` and `http://host:631/printers/bar` are
            // different queues on the same server, and with no path the request goes to the
            // server root, which a real IPP server answers with 404 or with the wrong queue's
            // attributes.
            let printer_path = ctx
                .startup_params
                .as_ref()
                .map(|p| p.get_optional_string("printer_path"))
                .transpose()?
                .flatten()
                .filter(|s| !s.trim().is_empty());

            IppClient::connect_with_llm_actions(
                ctx.remote_addr,
                printer_path,
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.client_id,
            )
            .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 {
            "get_printer_attributes" => Ok(ClientActionResult::Custom {
                name: "ipp_get_printer_attributes".to_string(),
                data: json!({}),
            }),
            "print_job" => {
                let job_name = action
                    .get("job_name")
                    .and_then(|v| v.as_str())
                    .context("Missing 'job_name' field")?
                    .to_string();

                let document_format = action
                    .get("document_format")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string());

                let document_data = action
                    .get("document_data")
                    .and_then(|v| v.as_str())
                    .context("Missing 'document_data' field")?;

                // The encoding is declared, never sniffed.
                //
                // This used to guess: "all ASCII alphanumeric (plus +/=) and a length divisible
                // by four" meant base64, anything else meant text. A four-character document is
                // both — printing the word `Test` produced three bytes of binary on the wire and
                // nothing said so. `"48656c6c6f"` is simultaneously valid text and valid hex and
                // only the sender knows which it means; this is the same defect `send_tcp_data`
                // had, in base64 rather than hex, and the fix is the same explicit field.
                //
                // The old guess also swallowed a genuine base64 error into the raw bytes with
                // `unwrap_or_else`, so a truncated document was printed as its own base64 text.
                let data_bytes = match action
                    .get("encoding")
                    .and_then(|v| v.as_str())
                    .map(str::trim)
                    .unwrap_or("utf8")
                    .to_ascii_lowercase()
                    .as_str()
                {
                    "utf8" | "utf-8" | "text" | "plain" => document_data.as_bytes().to_vec(),
                    "base64" | "b64" => {
                        use base64::{engine::general_purpose, Engine as _};
                        general_purpose::STANDARD
                            .decode(document_data)
                            .map_err(|e| {
                                anyhow::anyhow!(
                                "print_job 'document_data' is not valid base64: {e}. Either fix \
                                 the encoding or omit the 'encoding' field to send the string \
                                 as text."
                            )
                            })?
                    }
                    other => {
                        return Err(anyhow::anyhow!(
                            "print_job 'encoding' must be \"utf8\" or \"base64\", got {other:?}"
                        ))
                    }
                };

                Ok(ClientActionResult::Custom {
                    name: "ipp_print_job".to_string(),
                    data: json!({
                        "job_name": job_name,
                        "document_format": document_format,
                        "document_data": data_bytes,
                    }),
                })
            }
            "get_job_attributes" => {
                // Range-check before narrowing. `as i32` wraps, so `4294967297` becomes `1`
                // and the client asks the printer about a completely different job — one it
                // may well have, so the answer looks like a real answer. RFC 8011 §5.3.2
                // types job-id as integer(1:MAX), which is exactly what this can carry.
                let job_id = action
                    .get("job_id")
                    .and_then(|v| v.as_i64())
                    .context("Missing or invalid 'job_id' field")?;
                if !(1..=i32::MAX as i64).contains(&job_id) {
                    return Err(anyhow::anyhow!(
                        "job_id {job_id} is not an IPP job-id (1-2147483647, RFC 8011 §5.3.2). \
                         Use the job-id the printer returned from print_job."
                    ));
                }
                let job_id = job_id as i32;

                Ok(ClientActionResult::Custom {
                    name: "ipp_get_job_attributes".to_string(),
                    data: json!({
                        "job_id": job_id,
                    }),
                })
            }
            "disconnect" => Ok(ClientActionResult::Disconnect),
            // Declared in get_sync_actions, so it has to be executable here too --
            // advertising a name the executor rejects shows the model a tool it is
            // then punished for using.
            "wait_for_more" => Ok(ClientActionResult::WaitForMore),
            _ => Err(anyhow::anyhow!(
                "Unknown IPP client action: {}",
                action_type
            )),
        }
    }
}