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

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::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;

/// OpenAI client connected event
pub static OPENAI_CLIENT_CONNECTED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "openai_connected",
        "OpenAI client initialized and ready to make API requests",
        json!({
            "type": "send_chat_completion",
            "messages": [
                {"role": "user", "content": "Hello!"}
            ],
            "model": "gpt-3.5-turbo"
        }),
    )
    .with_parameters(vec![Parameter {
        name: "api_endpoint".to_string(),
        type_hint: "string".to_string(),
        description: "OpenAI API endpoint URL".to_string(),
        required: true,
    }])
});

/// OpenAI client response received event
pub static OPENAI_CLIENT_RESPONSE_RECEIVED_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "openai_response_received",
        "Response received from OpenAI API",
        json!({
            "type": "send_chat_completion",
            "messages": [
                {"role": "user", "content": "Follow-up question"}
            ]
        }),
    )
    .with_parameters(vec![
        Parameter {
            name: "response_type".to_string(),
            type_hint: "string".to_string(),
            description: "Type of response (chat_completion, embedding, etc.)".to_string(),
            required: true,
        },
        Parameter {
            name: "content".to_string(),
            type_hint: "string".to_string(),
            description: "Response content or error message".to_string(),
            required: true,
        },
        Parameter {
            name: "model".to_string(),
            type_hint: "string".to_string(),
            description: "Model used for the request".to_string(),
            required: false,
        },
        Parameter {
            name: "usage".to_string(),
            type_hint: "object".to_string(),
            description: "Token usage statistics".to_string(),
            required: false,
        },
    ])
});

/// Largest `max_tokens` this client will forward.
///
/// Comfortably above any real model's context window, so it never truncates a legitimate
/// request; its job is to keep the value inside `u32` and away from the narrowing casts that
/// used to sit between the model and the wire.
const MAX_COMPLETION_TOKENS: u64 = 1_000_000;

/// OpenAI client protocol action handler
pub struct OpenAiClientProtocol;

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

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

// Implement Protocol trait (common functionality)
impl Protocol for OpenAiClientProtocol {
    fn get_startup_parameters(&self) -> Vec<ParameterDefinition> {
        vec![
            ParameterDefinition {
                name: "api_key".to_string(),
                description: "OpenAI API key for authentication".to_string(),
                type_hint: "string".to_string(),
                required: true,
                example: json!("sk-..."),
                default: None,
            },
            ParameterDefinition {
                name: "default_model".to_string(),
                description: "Default model to use for requests".to_string(),
                type_hint: "string".to_string(),
                required: false,
                example: json!("gpt-4"),
                default: None,
            },
            ParameterDefinition {
                name: "organization".to_string(),
                description: "OpenAI organization ID".to_string(),
                type_hint: "string".to_string(),
                required: false,
                example: json!("org-..."),
                default: None,
            },
        ]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        vec![
            ActionDefinition {
                name: "send_chat_completion".to_string(),
                description: "Send a chat completion request to OpenAI".to_string(),
                parameters: vec![
                    Parameter {
                        name: "messages".to_string(),
                        type_hint: "array".to_string(),
                        description: "Array of message objects with role and content".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "model".to_string(),
                        type_hint: "string".to_string(),
                        description: "Model to use (e.g., gpt-4, gpt-3.5-turbo)".to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "temperature".to_string(),
                        type_hint: "number".to_string(),
                        description: "Sampling temperature (0.0 to 2.0)".to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "max_tokens".to_string(),
                        type_hint: "number".to_string(),
                        description: "Maximum tokens to generate".to_string(),
                        required: false,
                    },
                    Parameter {
                        name: "functions".to_string(),
                        type_hint: "array".to_string(),
                        description: "Array of function definitions for function calling"
                            .to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "send_chat_completion",
                    "messages": [
                        {"role": "user", "content": "Hello!"}
                    ],
                    "model": "gpt-3.5-turbo"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "send_embedding_request".to_string(),
                description: "Generate embeddings for text".to_string(),
                parameters: vec![
                    Parameter {
                        name: "input".to_string(),
                        type_hint: "string | array".to_string(),
                        description: "Text or array of texts to embed".to_string(),
                        required: true,
                    },
                    Parameter {
                        name: "model".to_string(),
                        type_hint: "string".to_string(),
                        description: "Embedding model to use (e.g., text-embedding-ada-002)"
                            .to_string(),
                        required: false,
                    },
                ],
                example: json!({
                    "type": "send_embedding_request",
                    "input": "The quick brown fox",
                    "model": "text-embedding-ada-002"
                }),
                log_template: None,
            },
            ActionDefinition {
                name: "disconnect".to_string(),
                description: "Close the OpenAI client connection".to_string(),
                parameters: vec![],
                example: json!({
                    "type": "disconnect"
                }),
                log_template: None,
            },
        ]
    }
    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![ActionDefinition {
            name: "send_chat_completion".to_string(),
            description: "Send another chat completion in response to received data".to_string(),
            parameters: vec![
                Parameter {
                    name: "messages".to_string(),
                    type_hint: "array".to_string(),
                    description: "Array of message objects".to_string(),
                    required: true,
                },
                Parameter {
                    name: "model".to_string(),
                    type_hint: "string".to_string(),
                    description: "Model to use".to_string(),
                    required: false,
                },
            ],
            example: json!({
                "type": "send_chat_completion",
                "messages": [
                    {"role": "user", "content": "Follow-up question"}
                ]
            }),
            log_template: Some(
                LogTemplate::new()
                    .with_info("-> OpenAI chat completion ({model})")
                    .with_debug("OpenAI send_chat_completion: model={model}"),
            ),
        }]
    }
    fn protocol_name(&self) -> &'static str {
        "OpenAI"
    }
    fn get_event_types(&self) -> Vec<EventType> {
        vec![
            EventType::new(
                "openai_connected",
                "Triggered when OpenAI client is initialized",
                json!({"type": "placeholder", "event_id": "openai_connected"}),
            ),
            EventType::new(
                "openai_response_received",
                "Triggered when OpenAI client receives a response",
                json!({"type": "placeholder", "event_id": "openai_response_received"}),
            ),
        ]
    }
    fn stack_name(&self) -> &'static str {
        "ETH>IP>TCP>TLS>HTTPS>OpenAI"
    }
    fn keywords(&self) -> Vec<&'static str> {
        vec!["openai", "openai client", "gpt", "chatgpt", "openai api"]
    }
    fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
        use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Experimental)
            .implementation(
                "async-openai 0.26 over a reqwest client built once per endpoint on \
                 spawn_blocking, so the platform root store is not loaded on the async \
                 runtime and a literal-IP host skips the system resolver. The API base comes \
                 from remote_addr and nothing else: an absent one refuses rather than \
                 reaching async-openai's default of https://api.openai.com/v1.",
            )
            .llm_control("Full control over chat completions, embeddings, and function calling")
            .e2e_testing(
                "tests/client/openai/. endpoint_and_limits_test pins the API base against \
                 the vendor default and the max_tokens/temperature ranges; \
                 command_channel_test drives the injected-action path against a loopback \
                 OpenAI-shaped stub; e2e_test starts the real binary against a dead loopback \
                 port. No test contacts api.openai.com and no real key is involved, so this \
                 is not validation against the real API.",
            )
            .build()
    }
    fn description(&self) -> &'static str {
        "OpenAI API client for LLM interactions"
    }
    fn example_prompt(&self) -> &'static str {
        "Connect to OpenAI and ask GPT-4 to explain quantum computing"
    }
    fn group_name(&self) -> &'static str {
        "AI & API"
    }
    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;
        use serde_json::json;

        StartupExamples::new(
            // LLM mode: LLM controls OpenAI API interactions
            json!({
                "type": "open_client",
                "remote_addr": "https://api.openai.com",
                "base_stack": "openai",
                "instruction": "Ask GPT-4 to explain quantum computing in simple terms",
                "startup_params": {
                    "api_key": "sk-...",
                    "default_model": "gpt-4"
                }
            }),
            // Script mode: Code-based OpenAI interactions
            json!({
                "type": "open_client",
                "remote_addr": "https://api.openai.com",
                "base_stack": "openai",
                "startup_params": {
                    "api_key": "sk-...",
                    "default_model": "gpt-3.5-turbo"
                },
                "event_handlers": [{
                    "event_pattern": "openai_response_received",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "<openai_client_handler>"
                    }
                }]
            }),
            // Static mode: Fixed chat completion request
            json!({
                "type": "open_client",
                "remote_addr": "https://api.openai.com",
                "base_stack": "openai",
                "startup_params": {
                    "api_key": "sk-...",
                    "default_model": "gpt-3.5-turbo"
                },
                "event_handlers": [
                    {
                        "event_pattern": "openai_connected",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "send_chat_completion",
                                "messages": [
                                    {"role": "user", "content": "Hello!"}
                                ],
                                "model": "gpt-3.5-turbo"
                            }]
                        }
                    },
                    {
                        "event_pattern": "openai_response_received",
                        "handler": {
                            "type": "static",
                            "actions": [{
                                "type": "disconnect"
                            }]
                        }
                    }
                ]
            }),
        )
    }
}

// Implement Client trait (client-specific functionality)
impl Client for OpenAiClientProtocol {
    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::openai::OpenAiClient;
            OpenAiClient::connect_with_llm_actions(
                ctx.remote_addr,
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                ctx.client_id,
                ctx.startup_params,
            )
            .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_chat_completion" => {
                let messages = action
                    .get("messages")
                    .context("Missing 'messages' field")?
                    .clone();

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

                // `as f32` turns a NaN or an out-of-range value into something the request
                // serialiser cannot represent, and OpenAI's own range is 0.0-2.0. Refuse
                // rather than send a number the API will reject with a message the model
                // never sees.
                let temperature = match action.get("temperature") {
                    None | Some(serde_json::Value::Null) => None,
                    Some(v) => {
                        let t = v.as_f64().ok_or_else(|| {
                            anyhow::anyhow!(
                                "send_chat_completion 'temperature' must be a number, got {v}"
                            )
                        })?;
                        if !t.is_finite() || !(0.0..=2.0).contains(&t) {
                            return Err(anyhow::anyhow!(
                                "send_chat_completion 'temperature' {t} is out of range; use \
                                 0.0-2.0"
                            ));
                        }
                        Some(t)
                    }
                };

                // Two narrowing casts used to sit between the model and the wire: `as u32`
                // here and `as u16` where the request was built. `max_tokens: 70000` became
                // 4464 and `max_tokens: 4294967296` became 0 — a budget silently replaced by
                // a different, smaller one, which reads on the wire as a deliberate choice.
                // Refuse instead, and name the range so the repair loop can correct it.
                let max_tokens = match action.get("max_tokens") {
                    None | Some(serde_json::Value::Null) => None,
                    Some(v) => {
                        let n = v.as_u64().ok_or_else(|| {
                            anyhow::anyhow!(
                                "send_chat_completion 'max_tokens' must be a positive whole \
                                 number, got {v}"
                            )
                        })?;
                        if n == 0 || n > MAX_COMPLETION_TOKENS {
                            return Err(anyhow::anyhow!(
                                "send_chat_completion 'max_tokens' {n} is out of range; use \
                                 1-{MAX_COMPLETION_TOKENS}"
                            ));
                        }
                        Some(n as u32)
                    }
                };

                let functions = action.get("functions").cloned();

                Ok(ClientActionResult::Custom {
                    name: "openai_chat_completion".to_string(),
                    data: json!({
                        "messages": messages,
                        "model": model,
                        "temperature": temperature,
                        "max_tokens": max_tokens,
                        "functions": functions,
                    }),
                })
            }
            "send_embedding_request" => {
                let input = action
                    .get("input")
                    .context("Missing 'input' field")?
                    .clone();

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

                Ok(ClientActionResult::Custom {
                    name: "openai_embedding".to_string(),
                    data: json!({
                        "input": input,
                        "model": model,
                    }),
                })
            }
            "disconnect" => Ok(ClientActionResult::Disconnect),
            _ => Err(anyhow::anyhow!(
                "Unknown OpenAI client action: {}",
                action_type
            )),
        }
    }
}