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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
//! Ollama protocol actions implementation

use crate::llm::actions::{
    protocol_trait::{ActionResult, Protocol, Server},
    ActionDefinition, Parameter,
};
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;
use tracing::debug;

use crate::protocol::log_template::LogTemplate;

/// Ollama generate request event
pub static OLLAMA_GENERATE_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_generate_request",
        "A client called /api/generate. Answer with ollama_generate_response carrying the \
         completion text, or refuse with ollama_error_response.",
        json!({
            "type": "ollama_generate_response",
            "response_text": "The capital of France is Paris."
        }),
    )
    // Per-endpoint narrowing: /api/generate is answered with a generate response, never with a
    // chat or model-list response. `call_llm` builds the model's tool list from the event type
    // rather than from get_sync_actions(), so before this the model was offered none of them.
    .with_actions(vec![
        ollama_generate_response_action(),
        ollama_error_response_action(),
    ])
    .with_parameters(vec![
        Parameter {
            name: "model".to_string(),
            type_hint: "string".to_string(),
            description: "Model name requested".to_string(),
            required: true,
        },
        Parameter {
            name: "prompt".to_string(),
            type_hint: "string".to_string(),
            description: "Prompt text".to_string(),
            required: true,
        },
        Parameter {
            name: "stream".to_string(),
            type_hint: "boolean".to_string(),
            description: "Whether streaming is requested".to_string(),
            required: false,
        },
    ])
});

/// Ollama chat request event
pub static OLLAMA_CHAT_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_chat_request",
        "A client called /api/chat. Answer with ollama_chat_response carrying the assistant \
         message, or refuse with ollama_error_response.",
        json!({
            "type": "ollama_chat_response",
            "message_content": "Hello! How can I help you today?"
        }),
    )
    .with_actions(vec![
        ollama_chat_response_action(),
        ollama_error_response_action(),
    ])
    .with_parameters(vec![
        Parameter {
            name: "model".to_string(),
            type_hint: "string".to_string(),
            description: "Model name requested".to_string(),
            required: true,
        },
        Parameter {
            name: "messages".to_string(),
            type_hint: "array".to_string(),
            description: "Chat messages".to_string(),
            required: true,
        },
        Parameter {
            name: "stream".to_string(),
            type_hint: "boolean".to_string(),
            description: "Whether streaming is requested".to_string(),
            required: false,
        },
    ])
});

/// Ollama models request event
pub static OLLAMA_MODELS_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_models_request",
        "A client called /api/tags to list the models this server offers. Answer with \
         ollama_models_response, or refuse with ollama_error_response.",
        json!({
            "type": "ollama_models_response",
            "models": ["llama2", "codellama", "mistral"]
        }),
    )
    .with_actions(vec![
        ollama_models_response_action(),
        ollama_error_response_action(),
    ])
});

/// Acknowledge a model-management operation.
///
/// Separate from `ollama_error_response` so that accept and refuse share no code path — the
/// separation `src/server/radius/` established.
fn ollama_admin_ok_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_admin_ok".to_string(),
        description: "Confirm a model-management operation (pull, create, copy, delete). \
                      Without this action the operation is refused - there is no implicit \
                      success."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "digest".to_string(),
                type_hint: "string".to_string(),
                description: "Digest to report for a pull, e.g. \"sha256:...\". Omit for other \
                              operations."
                    .to_string(),
                required: false,
            },
            Parameter {
                name: "total".to_string(),
                type_hint: "number".to_string(),
                description: "Total size in bytes to report for a pull. Omit for other \
                              operations."
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "ollama_admin_ok",
            "digest": "sha256:2f4b1c1e0a",
            "total": 3826793677i64
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama {operation} acknowledged")
                .with_debug("Ollama ollama_admin_ok: digest={digest} total={total}"),
        ),
    }
}

/// Answer `/api/show` with a model's details.
fn ollama_show_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_show_response".to_string(),
        description: "Return the details of the model /api/show asked about. Without this \
                      action the request is refused - nothing is invented."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "modelfile".to_string(),
                type_hint: "string".to_string(),
                description: "Modelfile contents, e.g. \"FROM llama2\"".to_string(),
                required: false,
            },
            Parameter {
                name: "parameters".to_string(),
                type_hint: "string".to_string(),
                description: "Parameter block, e.g. \"temperature 0.7\"".to_string(),
                required: false,
            },
            Parameter {
                name: "template".to_string(),
                type_hint: "string".to_string(),
                description: "Prompt template".to_string(),
                required: false,
            },
            Parameter {
                name: "details".to_string(),
                type_hint: "object".to_string(),
                description: "Details object, e.g. {\"format\": \"gguf\", \"family\": \"llama\"}"
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "ollama_show_response",
            "modelfile": "FROM llama2",
            "details": {"format": "gguf", "family": "llama"}
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama show answered")
                .with_debug("Ollama ollama_show_response: modelfile={modelfile}"),
        ),
    }
}

/// `/api/show`: a client asking what a model is.
///
/// This used to answer with a fabricated Modelfile (`FROM {name}`), a hardcoded
/// `temperature 0.7` and a `gguf`/`llama` details block, for any name at all and without an
/// event — so a server told "this instance serves only llama2" described every model a client
/// asked about, including ones it had just refused to pull.
pub static OLLAMA_SHOW_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_show_request",
        "A client asked /api/show for a model's details. Answer with ollama_show_response, or \
         refuse with ollama_error_response.",
        json!({
            "type": "ollama_show_response",
            "modelfile": "FROM llama2"
        }),
    )
    .with_parameters(vec![Parameter {
        name: "model".to_string(),
        type_hint: "string".to_string(),
        description: "Model the client asked about".to_string(),
        required: true,
    }])
    .with_actions(vec![
        ollama_show_response_action(),
        ollama_error_response_action(),
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("{client_ip} Ollama show {model}")
            .with_debug("Ollama show request: model={model}"),
    )
});

/// Answer `/api/embeddings` with a vector, or with the dimensionality of one.
fn ollama_embeddings_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_embeddings_response".to_string(),
        description: "Answer the /api/embeddings request. Supply `embedding` when the exact \
                      vector matters, or `dimensions` when only its shape does - a \
                      `dimensions`-only answer returns a deterministic ramp, which is a \
                      well-formed vector but carries no meaning. Without this action the \
                      request is refused; nothing is invented."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "embedding".to_string(),
                type_hint: "array".to_string(),
                description: "The embedding vector, as an array of numbers".to_string(),
                required: false,
            },
            Parameter {
                name: "dimensions".to_string(),
                type_hint: "number".to_string(),
                description: "Length of the vector to return when `embedding` is omitted \
                              (1-4096). Real Ollama models are 384-8192 wide; 768 is typical."
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "ollama_embeddings_response",
            "dimensions": 768
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama embeddings ({dimensions} dimensions)")
                .with_debug("Ollama ollama_embeddings_response: dimensions={dimensions}"),
        ),
    }
}

/// `/api/embeddings`: a client asking for a vector.
///
/// This used to answer every request with a hardcoded 768-element ramp, with no event, no
/// `call_llm` anywhere in the path and no way for the server's instruction to reach it - the
/// same defect `/api/show` and the four model-management endpoints had. A server told "this
/// instance serves only llama2" embedded for every model name asked of it, and a server the
/// operator had told to refuse everything embedded anyway.
pub static OLLAMA_EMBEDDINGS_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_embeddings_request",
        "A client asked /api/embeddings to embed a prompt. Answer with \
         ollama_embeddings_response, or refuse with ollama_error_response.",
        json!({
            "type": "ollama_embeddings_response",
            "dimensions": 768
        }),
    )
    .with_parameters(vec![
        Parameter {
            name: "model".to_string(),
            type_hint: "string".to_string(),
            description: "Model the client asked to embed with".to_string(),
            required: true,
        },
        Parameter {
            name: "prompt".to_string(),
            type_hint: "string".to_string(),
            description: "Text the client asked to embed".to_string(),
            required: false,
        },
    ])
    .with_actions(vec![
        ollama_embeddings_response_action(),
        ollama_error_response_action(),
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("{client_ip} Ollama embeddings {model}")
            .with_debug("Ollama embeddings request: model={model}"),
    )
});

/// Model-management event: `/api/pull`, `/api/create`, `/api/copy`, `/api/delete`.
///
/// These four endpoints used to answer `{"status":"success"}` unconditionally, without an
/// event and without consulting the model at all — so a server told "this instance only
/// serves llama2, refuse everything else" reported every pull as downloaded and every delete
/// as removed. `/api/pull` additionally invented a digest of `sha256:0000000000000000`.
pub static OLLAMA_ADMIN_REQUEST_EVENT: LazyLock<EventType> = LazyLock::new(|| {
    EventType::new(
        "ollama_admin_request",
        "A client asked to pull, create, copy or delete a model. Confirm with \
         ollama_admin_ok, or refuse with ollama_error_response.",
        json!({
            "type": "ollama_admin_ok"
        }),
    )
    .with_parameters(vec![
        Parameter {
            name: "operation".to_string(),
            type_hint: "string".to_string(),
            description: "One of: pull, create, copy, delete".to_string(),
            required: true,
        },
        Parameter {
            name: "model".to_string(),
            type_hint: "string".to_string(),
            description: "Model the request names (the `name` or `model` field)".to_string(),
            required: false,
        },
        Parameter {
            name: "destination".to_string(),
            type_hint: "string".to_string(),
            description: "Destination model name, for copy".to_string(),
            required: false,
        },
    ])
    .with_actions(vec![
        ollama_admin_ok_action(),
        ollama_error_response_action(),
    ])
    .with_log_template(
        LogTemplate::new()
            .with_info("{client_ip} Ollama {operation} {model}")
            .with_debug("Ollama admin {operation}: model={model} destination={destination}"),
    )
});

/// Ollama protocol action handler
pub struct OllamaProtocol {}

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

// Implement Protocol trait (common functionality)
impl Protocol for OllamaProtocol {
    fn get_startup_parameters(&self) -> Vec<crate::llm::actions::ParameterDefinition> {
        vec![
            crate::llm::actions::ParameterDefinition {
                name: "first_byte_timeout_secs".to_string(),
                type_hint: "number".to_string(),
                description:
                    "Seconds a connected peer may send no request at all before the server \
                     closes it. Default 30: HTTP is client-speaks-first, and NetGet's own \
                     Ollama client opens no socket until it has a request to send, so nothing \
                     here is ever connected and waiting on a person."
                        .to_string(),
                required: false,
                example: json!(30),
                default: Some(serde_json::json!(super::FIRST_BYTE_READ_TIMEOUT.as_secs())),
            },
            crate::llm::actions::ParameterDefinition {
                name: "idle_timeout_secs".to_string(),
                type_hint: "number".to_string(),
                description:
                    "Seconds an established connection may sit silent between requests before \
                     the server closes it. Default 300, chosen to stay above the 90 seconds \
                     reqwest keeps an idle pooled connection: a shorter bound races the pool \
                     and loses a non-idempotent /api/generate."
                        .to_string(),
                required: false,
                example: json!(300),
                default: Some(serde_json::json!(
                    super::IDLE_BETWEEN_REQUESTS_TIMEOUT.as_secs()
                )),
            },
        ]
    }
    fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
        vec![]
    }

    fn get_sync_actions(&self) -> Vec<ActionDefinition> {
        vec![
            ollama_generate_response_action(),
            ollama_chat_response_action(),
            ollama_models_response_action(),
            ollama_admin_ok_action(),
            ollama_show_response_action(),
            ollama_embeddings_response_action(),
            ollama_error_response_action(),
        ]
    }

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

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

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

    fn keywords(&self) -> Vec<&'static str> {
        vec!["ollama", "llm", "ai"]
    }

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

        ProtocolMetadataV2::builder()
            .state(DevelopmentState::Beta)
            .well_known_port(11434)
            .implementation("hyper with Ollama-compatible HTTP endpoints")
            .llm_control(
                "Every endpoint is a model decision: /api/generate, /api/chat, /api/tags, \
                 /api/show, /api/embeddings and the four model-management endpoints each \
                 raise an event and refuse when the model does not answer.",
            )
            .e2e_testing(
                "tests/server/ollama/e2e_test.rs. Driven by ollama-rs 0.3 - the same \
                 third-party crate netget uses to talk to a real Ollama, here in the client \
                 role - for /api/tags, /api/generate and /api/chat, plus reqwest for the raw \
                 JSON envelope, the refusal paths and the endpoints ollama-rs does not \
                 expose. An earlier version of this field claimed an \"ollama Python \
                 library\"; no Python was ever involved.",
            )
            .notes(
                "Mock Ollama API server for testing and honeypot purposes. Beta rests on \
                 real_client_test.rs: ollama-rs deserialises our /api/tags, /api/generate \
                 and /api/chat envelopes for itself, it is an unconditional dependency so \
                 the evidence runs wherever --features ollama compiles, and it is not \
                 circular - this server frames with hyper and serde_json, never with \
                 ollama-rs. Not Stable: streaming is assembled whole in memory rather than \
                 chunked, no client has driven the four model-management endpoints, and \
                 nothing validates an Authorization header (none is read, so the model \
                 cannot make that call either).",
            )
            .max_inbound_bytes(crate::server::ollama::MAX_REQUEST_BODY_BYTES)
            .request_only("The Ollama API is HTTP request/response; a server cannot send a peer anything unprompted")
            .build()
    }

    fn description(&self) -> &'static str {
        "Ollama-compatible API server"
    }

    fn example_prompt(&self) -> &'static str {
        "Start an Ollama-compatible API server on port 11435"
    }

    fn group_name(&self) -> &'static str {
        "AI & API"
    }
    fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
        use crate::llm::actions::StartupExamples;

        StartupExamples::new(
            // LLM mode: instruction-based
            json!({
                "type": "open_server",
                "port": 11435,
                "base_stack": "ollama",
                "instruction": "Ollama-compatible API server. Respond to /api/generate and /api/chat requests with helpful LLM responses, and list models on /api/tags"
            }),
            // Script mode: event_handlers with script handler
            json!({
                "type": "open_server",
                "port": 11435,
                "base_stack": "ollama",
                "event_handlers": [{
                    "event_pattern": "ollama_chat_request",
                    "handler": {
                        "type": "script",
                        "language": "python",
                        "code": "model = event.get('model', 'llama2')\naction('ollama_chat_response', message_content=f'Hello from {model}!')"
                    }
                }]
            }),
            // Static mode: event_handlers with static actions
            json!({
                "type": "open_server",
                "port": 11435,
                "base_stack": "ollama",
                "event_handlers": [{
                    "event_pattern": "ollama_models_request",
                    "handler": {
                        "type": "static",
                        "actions": [{
                            "type": "ollama_models_response",
                            "models": ["llama2", "codellama", "mistral"]
                        }]
                    }
                }]
            }),
        )
    }
}

// Implement Server trait (server-specific functionality)
impl Server for OllamaProtocol {
    fn spawn(
        &self,
        ctx: crate::protocol::SpawnContext,
    ) -> std::pin::Pin<
        Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
    > {
        Box::pin(async move {
            use crate::server::ollama::OllamaServer;

            // Both read deadlines are the operator's to choose: who is on the other end is
            // the only thing that decides them, and only the operator knows that.
            let secs = |name: &str| -> anyhow::Result<Option<u64>> {
                Ok(ctx
                    .startup_params
                    .as_ref()
                    .map(|p| p.get_optional_u64(name))
                    .transpose()?
                    .flatten())
            };
            let first_byte_timeout_secs = secs("first_byte_timeout_secs")?;
            let idle_timeout_secs = secs("idle_timeout_secs")?;

            OllamaServer::spawn_with_llm_actions(
                ctx.legacy_listen_addr(),
                ctx.llm_client,
                ctx.state,
                ctx.status_tx,
                false,
                ctx.server_id,
                first_byte_timeout_secs,
                idle_timeout_secs,
            )
            .await
        })
    }

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

        match action_type {
            "ollama_generate_response" => self.execute_ollama_generate_response(action),
            "ollama_chat_response" => self.execute_ollama_chat_response(action),
            "ollama_models_response" => self.execute_ollama_models_response(action),
            "ollama_admin_ok" => {
                let mut data = json!({});
                if let Some(d) = action.get("digest").and_then(|v| v.as_str()) {
                    data["digest"] = json!(d);
                }
                if let Some(t) = action.get("total").and_then(|v| v.as_u64()) {
                    data["total"] = json!(t);
                }
                Ok(ActionResult::Custom {
                    name: "ollama_admin_ok".to_string(),
                    data,
                })
            }
            "ollama_show_response" => {
                let mut data = json!({});
                for field in ["modelfile", "parameters", "template"] {
                    if let Some(v) = action.get(field).and_then(|v| v.as_str()) {
                        data[field] = json!(v);
                    }
                }
                if let Some(d) = action.get("details") {
                    data["details"] = d.clone();
                }
                Ok(ActionResult::Custom {
                    name: "ollama_show_response".to_string(),
                    data,
                })
            }
            "ollama_embeddings_response" => self.execute_ollama_embeddings_response(action),
            "ollama_error_response" => self.execute_ollama_error_response(action),
            _ => Err(anyhow::anyhow!("Unknown Ollama action: {}", action_type)),
        }
    }
}

impl OllamaProtocol {
    fn execute_ollama_generate_response(&self, _action: serde_json::Value) -> Result<ActionResult> {
        debug!("Execute: ollama_generate_response");
        Ok(ActionResult::Custom {
            name: "ollama_generate_response".to_string(),
            data: json!({"status": "acknowledged"}),
        })
    }

    fn execute_ollama_chat_response(&self, _action: serde_json::Value) -> Result<ActionResult> {
        debug!("Execute: ollama_chat_response");
        Ok(ActionResult::Custom {
            name: "ollama_chat_response".to_string(),
            data: json!({"status": "acknowledged"}),
        })
    }

    fn execute_ollama_models_response(&self, _action: serde_json::Value) -> Result<ActionResult> {
        debug!("Execute: ollama_models_response");
        Ok(ActionResult::Custom {
            name: "ollama_models_response".to_string(),
            data: json!({"status": "acknowledged"}),
        })
    }

    /// Build the `/api/embeddings` vector the model asked for.
    ///
    /// `dimensions` is validated here rather than where the response is built, so an
    /// unusable value is refused while the repair loop can still correct it. The cap is not
    /// cosmetic: the vector is serialised into the reply and `dimensions` is model-supplied,
    /// so an unbounded value is an allocation a single request can name.
    fn execute_ollama_embeddings_response(
        &self,
        action: serde_json::Value,
    ) -> Result<ActionResult> {
        const MAX_DIMENSIONS: u64 = 4096;

        if let Some(values) = action.get("embedding") {
            let values = values
                .as_array()
                .context("ollama_embeddings_response 'embedding' must be an array of numbers")?;
            if values.is_empty() {
                return Err(anyhow::anyhow!(
                    "ollama_embeddings_response 'embedding' must not be empty; omit it and \
                     give 'dimensions' instead, or refuse with ollama_error_response"
                ));
            }
            if values.len() as u64 > MAX_DIMENSIONS {
                return Err(anyhow::anyhow!(
                    "ollama_embeddings_response 'embedding' has {} elements; the limit is {}",
                    values.len(),
                    MAX_DIMENSIONS
                ));
            }
            let vector: Vec<f64> = values
                .iter()
                .map(|v| {
                    v.as_f64().ok_or_else(|| {
                        anyhow::anyhow!(
                            "ollama_embeddings_response 'embedding' must contain only \
                             numbers, got {v}"
                        )
                    })
                })
                .collect::<Result<_>>()?;
            debug!(
                "Execute: ollama_embeddings_response ({} given)",
                vector.len()
            );
            return Ok(ActionResult::Custom {
                name: "ollama_embeddings_response".to_string(),
                data: json!({ "embedding": vector }),
            });
        }

        let dimensions = action.get("dimensions").and_then(|v| v.as_u64()).context(
            "ollama_embeddings_response needs either 'embedding' (an array of numbers) or \
             'dimensions' (how long a vector to return)",
        )?;
        if dimensions == 0 || dimensions > MAX_DIMENSIONS {
            return Err(anyhow::anyhow!(
                "ollama_embeddings_response 'dimensions' {} is out of range; use 1-{} (768 \
                 is a typical embedding width)",
                dimensions,
                MAX_DIMENSIONS
            ));
        }

        // A deterministic ramp. It is not an embedding of anything, and the action's own
        // description says so - the alternative is asking a model to emit 768 floats.
        let vector: Vec<f64> = (0..dimensions)
            .map(|i| i as f64 / dimensions as f64)
            .collect();
        debug!("Execute: ollama_embeddings_response ({dimensions} dimensions)");
        Ok(ActionResult::Custom {
            name: "ollama_embeddings_response".to_string(),
            data: json!({ "embedding": vector }),
        })
    }

    fn execute_ollama_error_response(&self, _action: serde_json::Value) -> Result<ActionResult> {
        debug!("Execute: ollama_error_response");
        Ok(ActionResult::Custom {
            name: "ollama_error_response".to_string(),
            data: json!({"status": "acknowledged"}),
        })
    }
}

// Action definitions
fn ollama_generate_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_generate_response".to_string(),
        description: "Respond to /api/generate request with LLM-generated text".to_string(),
        parameters: vec![Parameter {
            name: "response_text".to_string(),
            type_hint: "string".to_string(),
            description: "Generated text response".to_string(),
            required: true,
        }],
        example: json!({
            "type": "ollama_generate_response",
            "response_text": "The capital of France is Paris."
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama generate ({response_text_len} chars)")
                .with_debug("Ollama ollama_generate_response: response_len={response_text_len}"),
        ),
    }
}

fn ollama_chat_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_chat_response".to_string(),
        description: "Respond to /api/chat request with chat message".to_string(),
        parameters: vec![Parameter {
            name: "message_content".to_string(),
            type_hint: "string".to_string(),
            description: "Chat message content".to_string(),
            required: true,
        }],
        example: json!({
            "type": "ollama_chat_response",
            "message_content": "Hello! How can I help you today?"
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama chat ({message_content_len} chars)")
                .with_debug("Ollama ollama_chat_response: content_len={message_content_len}"),
        ),
    }
}

fn ollama_models_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_models_response".to_string(),
        description: "Respond to /api/tags with list of models".to_string(),
        parameters: vec![Parameter {
            name: "models".to_string(),
            type_hint: "array".to_string(),
            description: "List of model names".to_string(),
            required: true,
        }],
        example: json!({
            "type": "ollama_models_response",
            "models": ["llama2", "codellama", "mistral"]
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama models ({models_len} available)")
                .with_debug("Ollama ollama_models_response: models_count={models_len}"),
        ),
    }
}

fn ollama_error_response_action() -> ActionDefinition {
    ActionDefinition {
        name: "ollama_error_response".to_string(),
        description: "Refuse this request. Returns the message as Ollama's \
            {\"error\": \"...\"} body with a 4xx status, instead of answering it."
            .to_string(),
        parameters: vec![
            Parameter {
                name: "error_message".to_string(),
                type_hint: "string".to_string(),
                description: "Error message to return".to_string(),
                required: true,
            },
            Parameter {
                name: "status_code".to_string(),
                type_hint: "number".to_string(),
                description: "HTTP status for the refusal. Defaults to 400; real Ollama \
                    answers 404 for an unknown model."
                    .to_string(),
                required: false,
            },
        ],
        example: json!({
            "type": "ollama_error_response",
            "error_message": "Model not found",
            "status_code": 404
        }),
        log_template: Some(
            LogTemplate::new()
                .with_info("-> Ollama error: {error_message}")
                .with_debug("Ollama ollama_error_response: {error_message}"),
        ),
    }
}

/// Get Ollama-specific event types
///
/// These are clones of the constants `mod.rs` passes to `call_llm`, so the documented event
/// catalog and the action list the model is actually offered cannot diverge.
fn get_ollama_event_types() -> Vec<EventType> {
    vec![
        OLLAMA_GENERATE_REQUEST_EVENT.clone(),
        OLLAMA_CHAT_REQUEST_EVENT.clone(),
        OLLAMA_MODELS_REQUEST_EVENT.clone(),
        OLLAMA_ADMIN_REQUEST_EVENT.clone(),
        OLLAMA_SHOW_REQUEST_EVENT.clone(),
        OLLAMA_EMBEDDINGS_REQUEST_EVENT.clone(),
    ]
}