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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
//! Elasticsearch client implementation
pub mod actions;

pub use actions::ElasticsearchClientProtocol;

use anyhow::{Context, Result};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

use crate::client::elasticsearch::actions::{
    ELASTICSEARCH_CLIENT_CONNECTED_EVENT, ELASTICSEARCH_CLIENT_RESPONSE_RECEIVED_EVENT,
};
use crate::client::http_fetch::{FetchClient, FetchRequest};
use crate::client::llm_budget::call_llm_for_client;
use crate::llm::actions::client_trait::{Client, ClientActionResult};
use crate::llm::actions::protocol_trait::Protocol;
use crate::llm::ollama_client::OllamaClient;
use crate::llm::ClientLlmResult;
use crate::protocol::Event;
use crate::state::app_state::AppState;
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientId, ClientStatus};
use crate::utils::truncate::truncate_for_log;

/// The body of a one-shot follow-up request.
///
/// Two encodings, because `_bulk` is NDJSON and every other endpoint is JSON, and
/// Elasticsearch refuses a bulk request whose content type says `application/json`.
enum RequestBody {
    Json(serde_json::Value),
    Ndjson(String),
}

/// Elasticsearch client that interacts with Elasticsearch clusters
pub struct ElasticsearchClient;

impl ElasticsearchClient {
    /// Connect to an Elasticsearch cluster with integrated LLM actions
    pub async fn connect_with_llm_actions(
        remote_addr: String,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        client_id: ClientId,
        startup_params: Option<crate::protocol::StartupParams>,
    ) -> Result<SocketAddr> {
        // For Elasticsearch, "connection" is logical (HTTP-based)
        // We'll create a reqwest client configured for Elasticsearch

        info!(
            "Elasticsearch client {} initialized for {}",
            client_id, remote_addr
        );

        // Ensure URL has scheme
        let cluster_url =
            if remote_addr.starts_with("http://") || remote_addr.starts_with("https://") {
                remote_addr.clone()
            } else {
                format!("http://{}", remote_addr)
            };

        // Build HTTP client for Elasticsearch
        #[cfg(not(target_arch = "wasm32"))]
        let _http_client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .context("Failed to build HTTP client for Elasticsearch")?;
        // The browser transport has no TLS: an `https://` cluster is refused here, with the
        // reason, rather than on the first request.
        crate::client::http_fetch::check_url(&cluster_url)?;

        // `username` / `password` / `default_index` were all declared and none was read:
        // a secured cluster answered 401 with nothing in the parameter list to explain it,
        // and an operation that omitted `index` built the URL `{cluster}//_search`.
        //
        // The credentials are resolved once here into the `Authorization: Basic ...` value
        // every request now carries, rather than re-encoded per call. A username with no
        // password is a valid Basic credential (`user:`), so only the username is required
        // to switch authentication on.
        let (username, password, default_index) = match &startup_params {
            Some(params) => (
                params.get_optional_string("username")?,
                params.get_optional_string("password")?,
                params.get_optional_string("default_index")?,
            ),
            None => (None, None, None),
        };
        let authorization = username.map(|user| {
            use base64::Engine;
            let encoded = base64::engine::general_purpose::STANDARD
                .encode(format!("{}:{}", user, password.unwrap_or_default()).as_bytes());
            format!("Basic {encoded}")
        });

        // Store client configuration in protocol_data
        app_state
            .with_client_mut(client_id, |client| {
                client
                    .set_protocol_field("es_client".to_string(), serde_json::json!("initialized"));
                client.set_protocol_field(
                    "cluster_url".to_string(),
                    serde_json::json!(cluster_url.clone()),
                );
                if let Some(value) = &authorization {
                    client
                        .set_protocol_field("authorization".to_string(), serde_json::json!(value));
                }
                if let Some(index) = &default_index {
                    client
                        .set_protocol_field("default_index".to_string(), serde_json::json!(index));
                }
            })
            .await;

        // Update status
        app_state
            .update_client_status(client_id, ClientStatus::Connected)
            .await;
        let _ = status_tx.send(format!(
            "[CLIENT] Elasticsearch client {} ready for {}",
            client_id, cluster_url
        ));
        let _ = status_tx.send("__UPDATE_UI__".to_string());

        // Command channel for injected actions (the dashboard's [ send ] row).
        // Registered BEFORE the connected-event LLM call below: a dashboard-created
        // client defaults to a `*` -> manual routing rule, which parks that call until
        // a human answers, and [ send ] has to work for the whole park.
        //
        // The command loop also replaces the 5s "has the client been removed yet"
        // poll this client used to run: `remove_client` drops the handle, the sender
        // goes with it, and `recv()` returns None - promptly, and without a timer.
        let command_rx =
            crate::client::command_support::register_command_channel(&app_state, client_id).await;
        let cmd_task = tokio::spawn(Self::command_loop(
            command_rx,
            client_id,
            app_state.clone(),
            llm_client.clone(),
            status_tx.clone(),
        ));
        app_state.register_client_task(client_id, cmd_task).await;

        // Call LLM with connected event to get initial instructions
        if let Some(instruction) = app_state.get_instruction_for_client(client_id).await {
            let protocol = Arc::new(ElasticsearchClientProtocol::new());
            let event = Event::new(
                &ELASTICSEARCH_CLIENT_CONNECTED_EVENT,
                serde_json::json!({
                    "cluster_url": cluster_url.clone(),
                }),
            );

            let memory = app_state
                .get_memory_for_client(client_id)
                .await
                .unwrap_or_default();

            match call_llm_for_client(
                &llm_client,
                &app_state,
                client_id.to_string(),
                &instruction,
                &memory,
                Some(&event),
                protocol.as_ref(),
                &status_tx,
            )
            .await
            {
                Ok(ClientLlmResult {
                    actions,
                    memory_updates,
                }) => {
                    // Update memory
                    if let Some(mem) = memory_updates {
                        app_state.set_memory_for_client(client_id, mem).await;
                    }

                    // Execute initial actions through the same path injected
                    // commands use, so the two cannot diverge. Spawned (and
                    // registered) rather than awaited: `connect` must return
                    // promptly, and an in-flight request has to die with the client.
                    for action in actions {
                        let result = match protocol.execute_action(action) {
                            Ok(result) => result,
                            Err(e) => {
                                error!("Failed to execute initial action: {}", e);
                                continue;
                            }
                        };
                        let state_clone = app_state.clone();
                        let llm_clone = llm_client.clone();
                        let status_clone = status_tx.clone();
                        let handle = tokio::spawn(async move {
                            let outcome = Self::apply_action(
                                result,
                                client_id,
                                &state_clone,
                                &llm_clone,
                                &status_clone,
                            )
                            .await;
                            debug!(
                                "Elasticsearch client {} connect action: {:?}",
                                client_id, outcome
                            );
                        });
                        app_state.register_client_task(client_id, handle).await;
                    }
                }
                Err(e) => {
                    error!(
                        "Initial LLM call failed for Elasticsearch client {}: {}",
                        client_id, e
                    );
                }
            }
        }

        // Return dummy address (Elasticsearch is HTTP-based)
        Ok("0.0.0.0:0".parse().unwrap())
    }

    /// Index a document into Elasticsearch
    pub async fn index_document(
        client_id: ClientId,
        index: String,
        id: Option<String>,
        document: serde_json::Value,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<u16> {
        let (cluster_url, authorization) = Self::cluster_endpoint(&app_state, client_id).await?;

        let url = if let Some(doc_id) = &id {
            format!("{}/{}/_doc/{}", cluster_url, index, doc_id)
        } else {
            format!("{}/{}/_doc", cluster_url, index)
        };

        info!(
            "Elasticsearch client {} indexing document into {}",
            client_id, index
        );

        let http_client = Self::http_client();
        let response = Self::authorize(http_client.post(&url), &authorization)
            .json(&document)
            .send()
            .await
            .context("Failed to send index request")?;

        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));

        info!(
            "Elasticsearch client {} index response: {}",
            client_id, status_code
        );

        // Raise the response event from its own registered task rather than inline.
        // A dashboard-created client defaults to a `*` -> manual routing rule, so this
        // LLM call can park for minutes waiting for a human; awaiting it here would
        // wedge the command loop and make an injected action that in fact succeeded
        // look to the dashboard like a timeout.
        let notify = Self::spawn_response_notification(
            client_id,
            "index_document".to_string(),
            status_code,
            response_body,
            app_state.clone(),
            llm_client,
            status_tx,
        );
        app_state.register_client_task(client_id, notify).await;

        Ok(status_code)
    }

    /// Search documents in Elasticsearch
    pub async fn search(
        client_id: ClientId,
        index: String,
        query: serde_json::Value,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<u16> {
        let (cluster_url, authorization) = Self::cluster_endpoint(&app_state, client_id).await?;
        let url = format!("{}/{}/_search", cluster_url, index);

        info!(
            "Elasticsearch client {} searching index {}",
            client_id, index
        );

        let search_body = serde_json::json!({
            "query": query
        });

        let http_client = Self::http_client();
        let response = Self::authorize(http_client.post(&url), &authorization)
            .json(&search_body)
            .send()
            .await
            .context("Failed to send search request")?;

        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));

        info!(
            "Elasticsearch client {} search response: {} hits",
            client_id,
            response_body
                .get("hits")
                .and_then(|h| h.get("total"))
                .and_then(|t| t.get("value"))
                .and_then(|v| v.as_u64())
                .unwrap_or(0)
        );

        // Raise the response event from its own registered task rather than inline.
        // A dashboard-created client defaults to a `*` -> manual routing rule, so this
        // LLM call can park for minutes waiting for a human; awaiting it here would
        // wedge the command loop and make an injected action that in fact succeeded
        // look to the dashboard like a timeout.
        let notify = Self::spawn_response_notification(
            client_id,
            "search".to_string(),
            status_code,
            response_body,
            app_state.clone(),
            llm_client,
            status_tx,
        );
        app_state.register_client_task(client_id, notify).await;

        Ok(status_code)
    }

    /// Get a document by ID
    pub async fn get_document(
        client_id: ClientId,
        index: String,
        id: String,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<u16> {
        let (cluster_url, authorization) = Self::cluster_endpoint(&app_state, client_id).await?;
        let url = format!("{}/{}/_doc/{}", cluster_url, index, id);

        info!(
            "Elasticsearch client {} getting document {} from {}",
            client_id, id, index
        );

        let http_client = Self::http_client();
        let response = Self::authorize(http_client.get(&url), &authorization)
            .send()
            .await
            .context("Failed to send get request")?;

        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));

        // Raise the response event from its own registered task rather than inline.
        // A dashboard-created client defaults to a `*` -> manual routing rule, so this
        // LLM call can park for minutes waiting for a human; awaiting it here would
        // wedge the command loop and make an injected action that in fact succeeded
        // look to the dashboard like a timeout.
        let notify = Self::spawn_response_notification(
            client_id,
            "get_document".to_string(),
            status_code,
            response_body,
            app_state.clone(),
            llm_client,
            status_tx,
        );
        app_state.register_client_task(client_id, notify).await;

        Ok(status_code)
    }

    /// Delete a document by ID
    pub async fn delete_document(
        client_id: ClientId,
        index: String,
        id: String,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<u16> {
        let (cluster_url, authorization) = Self::cluster_endpoint(&app_state, client_id).await?;
        let url = format!("{}/{}/_doc/{}", cluster_url, index, id);

        info!(
            "Elasticsearch client {} deleting document {} from {}",
            client_id, id, index
        );

        let http_client = Self::http_client();
        let response = Self::authorize(http_client.delete(&url), &authorization)
            .send()
            .await
            .context("Failed to send delete request")?;

        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));

        // Raise the response event from its own registered task rather than inline.
        // A dashboard-created client defaults to a `*` -> manual routing rule, so this
        // LLM call can park for minutes waiting for a human; awaiting it here would
        // wedge the command loop and make an injected action that in fact succeeded
        // look to the dashboard like a timeout.
        let notify = Self::spawn_response_notification(
            client_id,
            "delete_document".to_string(),
            status_code,
            response_body,
            app_state.clone(),
            llm_client,
            status_tx,
        );
        app_state.register_client_task(client_id, notify).await;

        Ok(status_code)
    }

    /// Execute bulk operations
    /// Build the NDJSON body for a `_bulk` request.
    ///
    /// Extracted so the follow-up path can reach it too: `bulk_operation` is advertised to
    /// the model on `elasticsearch_response_received` like every other verb, but the
    /// follow-up dispatch had no arm for it and logged "skipped", so the one verb that
    /// batches work was the one the model could not use in reply to a result.
    fn build_bulk_ndjson(operations: Vec<serde_json::Value>) -> Result<String> {
        let mut bulk_body = String::new();
        for op in operations {
            let action = op
                .get("action")
                .and_then(|a| a.as_str())
                .context("Missing 'action' field in bulk operation")?;
            let index = op
                .get("index")
                .and_then(|i| i.as_str())
                .context("Missing 'index' field in bulk operation")?;
            let id = op.get("id").and_then(|i| i.as_str());

            match action {
                "index" => {
                    let mut meta = serde_json::json!({
                        "index": { "_index": index }
                    });
                    if let Some(doc_id) = id {
                        meta["index"]["_id"] = serde_json::json!(doc_id);
                    }
                    bulk_body.push_str(&serde_json::to_string(&meta)?);
                    bulk_body.push('\n');

                    let document = op
                        .get("document")
                        .context("Missing 'document' field for index action")?;
                    bulk_body.push_str(&serde_json::to_string(&document)?);
                    bulk_body.push('\n');
                }
                "delete" => {
                    let doc_id = id.context("Missing 'id' field for delete action")?;
                    let meta = serde_json::json!({
                        "delete": {
                            "_index": index,
                            "_id": doc_id
                        }
                    });
                    bulk_body.push_str(&serde_json::to_string(&meta)?);
                    bulk_body.push('\n');
                }
                "update" => {
                    let doc_id = id.context("Missing 'id' field for update action")?;
                    let meta = serde_json::json!({
                        "update": {
                            "_index": index,
                            "_id": doc_id
                        }
                    });
                    bulk_body.push_str(&serde_json::to_string(&meta)?);
                    bulk_body.push('\n');

                    let document = op
                        .get("document")
                        .context("Missing 'document' field for update action")?;
                    let update_doc = serde_json::json!({ "doc": document });
                    bulk_body.push_str(&serde_json::to_string(&update_doc)?);
                    bulk_body.push('\n');
                }
                _ => return Err(anyhow::anyhow!("Unknown bulk action: {}", action)),
            }
        }

        Ok(bulk_body)
    }

    pub async fn bulk_operation(
        client_id: ClientId,
        operations: Vec<serde_json::Value>,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> Result<u16> {
        let (cluster_url, authorization) = Self::cluster_endpoint(&app_state, client_id).await?;
        let url = format!("{}/_bulk", cluster_url);

        info!(
            "Elasticsearch client {} executing {} bulk operations",
            client_id,
            operations.len()
        );

        let bulk_body = Self::build_bulk_ndjson(operations)?;

        let http_client = Self::http_client();
        let response = Self::authorize(http_client.post(&url), &authorization)
            .header("Content-Type", "application/x-ndjson")
            .body(bulk_body)
            .send()
            .await
            .context("Failed to send bulk request")?;

        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));

        // Raise the response event from its own registered task rather than inline.
        // A dashboard-created client defaults to a `*` -> manual routing rule, so this
        // LLM call can park for minutes waiting for a human; awaiting it here would
        // wedge the command loop and make an injected action that in fact succeeded
        // look to the dashboard like a timeout.
        let notify = Self::spawn_response_notification(
            client_id,
            "bulk_operation".to_string(),
            status_code,
            response_body,
            app_state.clone(),
            llm_client,
            status_tx,
        );
        app_state.register_client_task(client_id, notify).await;

        Ok(status_code)
    }

    /// Apply one executed action against the cluster. Shared by the
    /// connected-event LLM path and by injected commands, so the two cannot
    /// diverge - and so every advertised verb is reachable from both. (The
    /// connect path used to dispatch only `index_document` and `search`, silently
    /// dropping `get_document`, `delete_document` and `bulk_operation`.)
    ///
    /// `reqwest` reports no wire byte count for a request it framed itself, so a
    /// completed operation is `Executed { detail }` naming the operation and the
    /// HTTP status it got back - never `Sent`, which would be a fabricated count.
    async fn apply_action(
        result: ClientActionResult,
        client_id: ClientId,
        app_state: &Arc<AppState>,
        llm_client: &OllamaClient,
        status_tx: &mpsc::UnboundedSender<String>,
    ) -> ClientSendOutcome {
        let (name, data) = match result {
            ClientActionResult::Custom { name, data } => (name, data),
            ClientActionResult::Disconnect => return ClientSendOutcome::Disconnected,
            ClientActionResult::WaitForMore => {
                return ClientSendOutcome::Executed {
                    detail: "wait_for_more".to_string(),
                }
            }
            ClientActionResult::NoAction => {
                return ClientSendOutcome::Executed {
                    detail: "no_action".to_string(),
                }
            }
            ClientActionResult::SendData(_) => {
                return ClientSendOutcome::Executed {
                    detail: "send_data has no meaning for the Elasticsearch client: it speaks \
                             the Elasticsearch REST API over reqwest, not a socket this client \
                             owns"
                        .to_string(),
                }
            }
            ClientActionResult::Multiple(_) => {
                return ClientSendOutcome::Executed {
                    detail: "the Elasticsearch client's own verbs never produce Multiple; \
                             nothing was applied"
                        .to_string(),
                }
            }
        };

        // An omitted `index` falls back to the `default_index` startup parameter. It used
        // to become the empty string, which built `{cluster}//_search` and was refused by
        // the cluster with nothing pointing at the cause.
        //
        // `bulk_operation` is exempt: it POSTs to the cluster-wide `/_bulk` and every
        // operation in its array names its own index, so it never reads this value.
        let index = if name == "bulk_operation" {
            String::new()
        } else {
            match Self::resolve_index(
                app_state,
                client_id,
                data.get("index").and_then(|v| v.as_str()).unwrap_or(""),
            )
            .await
            {
                Ok(index) => index,
                Err(e) => {
                    return ClientSendOutcome::Executed {
                        detail: format!("{name} failed: {e}"),
                    }
                }
            }
        };
        let id = data
            .get("id")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let outcome = match name.as_str() {
            "index_document" => {
                let document = data
                    .get("document")
                    .cloned()
                    .unwrap_or(serde_json::json!({}));
                Self::index_document(
                    client_id,
                    index.clone(),
                    id,
                    document,
                    app_state.clone(),
                    llm_client.clone(),
                    status_tx.clone(),
                )
                .await
            }
            "search" => {
                let query = data.get("query").cloned().unwrap_or(serde_json::json!({}));
                Self::search(
                    client_id,
                    index.clone(),
                    query,
                    app_state.clone(),
                    llm_client.clone(),
                    status_tx.clone(),
                )
                .await
            }
            "get_document" => match id {
                Some(id) => {
                    Self::get_document(
                        client_id,
                        index.clone(),
                        id,
                        app_state.clone(),
                        llm_client.clone(),
                        status_tx.clone(),
                    )
                    .await
                }
                None => Err(anyhow::anyhow!("get_document requires an 'id'")),
            },
            "delete_document" => match id {
                Some(id) => {
                    Self::delete_document(
                        client_id,
                        index.clone(),
                        id,
                        app_state.clone(),
                        llm_client.clone(),
                        status_tx.clone(),
                    )
                    .await
                }
                None => Err(anyhow::anyhow!("delete_document requires an 'id'")),
            },
            "bulk_operation" => {
                let operations = data
                    .get("operations")
                    .and_then(|v| v.as_array())
                    .cloned()
                    .unwrap_or_default();
                Self::bulk_operation(
                    client_id,
                    operations,
                    app_state.clone(),
                    llm_client.clone(),
                    status_tx.clone(),
                )
                .await
            }
            other => Err(anyhow::anyhow!(
                "Unknown Elasticsearch operation: {}",
                other
            )),
        };

        match outcome {
            Ok(status_code) => ClientSendOutcome::Executed {
                detail: format!("{} completed: HTTP {}", name, status_code),
            },
            Err(e) => {
                error!(
                    "Elasticsearch client {} operation {} failed: {}",
                    client_id, name, e
                );
                let _ = status_tx.send(format!(
                    "[ERROR] Elasticsearch operation {} failed: {}",
                    name, e
                ));
                ClientSendOutcome::Executed {
                    detail: format!("{} failed: {}", name, truncate_for_log(&e.to_string(), 200)),
                }
            }
        }
    }

    /// Drain injected commands until the channel closes (the client was removed)
    /// or an injected `disconnect` ends the session.
    ///
    /// `command_support::handle_stream_client_command` cannot serve this client:
    /// every Elasticsearch verb yields `ClientActionResult::Custom` and there is no
    /// write half to put bytes on. Actions therefore go through
    /// [`Self::apply_action`] - the same function the LLM path uses - and the
    /// outcome is logged and replied exactly the way the generic arm does it.
    async fn command_loop(
        mut command_rx: mpsc::Receiver<ClientCommand>,
        client_id: ClientId,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) {
        let protocol = ElasticsearchClientProtocol::new();

        while let Some(command) = command_rx.recv().await {
            let action = command.action.clone();
            let outcome = match protocol.execute_action(action.clone()) {
                Err(e) => ClientSendOutcome::Rejected {
                    error: e.to_string(),
                },
                Ok(result) => {
                    Self::apply_action(result, client_id, &app_state, &llm_client, &status_tx).await
                }
            };
            let disconnected = matches!(outcome, ClientSendOutcome::Disconnected);

            app_state
                .record_access_log(
                    AccessLogOwner::Client(client_id.as_u32()),
                    protocol.protocol_name(),
                    None,
                    "injected_action",
                    action,
                    vec![serde_json::to_value(&outcome).unwrap_or(serde_json::Value::Null)],
                )
                .await;
            let _ = status_tx.send("__UPDATE_UI__".to_string());
            crate::client::command_support::reply(command, Ok(outcome));

            if disconnected {
                info!(
                    "Elasticsearch client {} disconnecting on injected action",
                    client_id
                );
                app_state
                    .update_client_status(client_id, ClientStatus::Disconnected)
                    .await;
                break;
            }
        }

        info!("Elasticsearch client {} command loop stopped", client_id);
        // Never leave the dashboard offering [ send ] into a client that is gone.
        app_state.remove_client_handle(client_id).await;
        let _ = status_tx.send("__UPDATE_UI__".to_string());
    }

    /// Cluster URL plus the `Authorization` value resolved from the `username` / `password`
    /// startup parameters, if any. Every request goes through here so authentication cannot
    /// be applied to some operations and forgotten on others.
    async fn cluster_endpoint(
        app_state: &Arc<AppState>,
        client_id: ClientId,
    ) -> Result<(String, Option<String>)> {
        let (url, authorization) = app_state
            .with_client_mut(client_id, |client| {
                (
                    client
                        .get_protocol_field("cluster_url")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string()),
                    client
                        .get_protocol_field("authorization")
                        .and_then(|v| v.as_str())
                        .map(|s| s.to_string()),
                )
            })
            .await
            .unwrap_or((None, None));
        Ok((url.context("No cluster URL found")?, authorization))
    }

    /// The index an operation should act on: the one it named, or the `default_index`
    /// startup parameter when it named none. Fails rather than building `{cluster}//_search`
    /// out of an empty string, which is what an omitted index used to produce.
    async fn resolve_index(
        app_state: &Arc<AppState>,
        client_id: ClientId,
        requested: &str,
    ) -> Result<String> {
        if !requested.is_empty() {
            return Ok(requested.to_string());
        }
        app_state
            .with_client_mut(client_id, |client| {
                client
                    .get_protocol_field("default_index")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
            })
            .await
            .flatten()
            .context(
                "no index was given and no `default_index` startup parameter is set for this \
                 client",
            )
    }

    /// The HTTP client for one request: a fresh `reqwest::Client` natively, the shared hyper
    /// transport over the virtual loopback in the browser build
    /// ([`crate::client::http_fetch`]), there bounded at 30 seconds an exchange.
    fn http_client() -> FetchClient {
        #[cfg(not(target_arch = "wasm32"))]
        let client = FetchClient::from_reqwest(reqwest::Client::new());
        #[cfg(target_arch = "wasm32")]
        let client = FetchClient::transport(std::time::Duration::from_secs(30));
        client
    }

    /// Apply the cluster's `Authorization` header, when one was configured.
    fn authorize(request: FetchRequest, authorization: &Option<String>) -> FetchRequest {
        match authorization {
            Some(value) => request.header("Authorization", value),
            None => request,
        }
    }

    /// Raise `elasticsearch_response_received` off the caller's task. Never awaited by a
    /// caller that holds the command loop: an event handler may park this call for a human
    /// answer.
    /// Issue one Elasticsearch request and return `(status, body)`. Raises no event and
    /// calls no LLM **itself**.
    ///
    /// Follow-up actions the model returns for `elasticsearch_response_received` run through
    /// here rather than through `search`/`index_document`/..., because each of those ends in
    /// `spawn_response_notification`, which calls `call_llm_with_response`, which is where
    /// the follow-ups are executed — and rustc rejects the resulting async type.
    ///
    /// The caller reports the result itself, bounded by [`Self::MAX_FOLLOWUP_DEPTH`]. It used
    /// not to, and the comment here claimed that raising nothing "bounds the chain": it did,
    /// at one step, which meant a search issued in reply to an index confirmation returned
    /// its hits to nobody. The bound is the depth counter, not the silence.
    async fn run_request_once(
        client_id: ClientId,
        method: hyper::Method,
        path: &str,
        body: Option<RequestBody>,
        app_state: &Arc<AppState>,
    ) -> Result<(u16, serde_json::Value)> {
        let (cluster_url, authorization) = Self::cluster_endpoint(app_state, client_id).await?;
        let url = format!(
            "{}/{}",
            cluster_url.trim_end_matches('/'),
            path.trim_start_matches('/')
        );
        let http_client = Self::http_client();
        let mut req = Self::authorize(http_client.request(method, &url), &authorization);
        match body {
            Some(RequestBody::Json(b)) => req = req.json(&b),
            // `_bulk` is NDJSON and Elasticsearch rejects it outright when it arrives as
            // `application/json`, so the content type has to travel with the body.
            Some(RequestBody::Ndjson(b)) => {
                req = req.header("Content-Type", "application/x-ndjson").body(b)
            }
            None => {}
        }
        let response = req.send().await.context("Elasticsearch request failed")?;
        let status_code = response.status().as_u16();
        let response_body: serde_json::Value = response
            .json()
            .await
            .unwrap_or(serde_json::json!({"error": "Failed to parse response"}));
        Ok((status_code, response_body))
    }

    fn spawn_response_notification(
        client_id: ClientId,
        operation: String,
        status_code: u16,
        response: serde_json::Value,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
    ) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            if let Err(e) = Self::call_llm_with_response(
                client_id,
                operation,
                status_code,
                response,
                app_state,
                llm_client,
                status_tx,
                0,
            )
            .await
            {
                error!(
                    "Elasticsearch client {} response notification failed: {}",
                    client_id, e
                );
            }
        })
    }

    /// How many follow-ups deep this client will keep reporting results to the model.
    /// Each report can produce another request, which produces another report; without a
    /// bound a model that answers `search` with `search` would loop on the LLM forever.
    const MAX_FOLLOWUP_DEPTH: u8 = 4;

    /// Helper: Call LLM with response
    #[allow(clippy::too_many_arguments)]
    async fn call_llm_with_response(
        client_id: ClientId,
        operation: String,
        status_code: u16,
        response: serde_json::Value,
        app_state: Arc<AppState>,
        llm_client: OllamaClient,
        status_tx: mpsc::UnboundedSender<String>,
        depth: u8,
    ) -> Result<()> {
        if let Some(instruction) = app_state.get_instruction_for_client(client_id).await {
            let protocol = Arc::new(ElasticsearchClientProtocol::new());
            let event = Event::new(
                &ELASTICSEARCH_CLIENT_RESPONSE_RECEIVED_EVENT,
                serde_json::json!({
                    "operation": operation,
                    "status_code": status_code,
                    "response": response,
                }),
            );

            let memory = app_state
                .get_memory_for_client(client_id)
                .await
                .unwrap_or_default();

            match call_llm_for_client(
                &llm_client,
                &app_state,
                client_id.to_string(),
                &instruction,
                &memory,
                Some(&event),
                protocol.as_ref(),
                &status_tx,
            )
            .await
            {
                Ok(ClientLlmResult {
                    actions,
                    memory_updates,
                }) => {
                    // Update memory
                    if let Some(mem) = memory_updates {
                        app_state.set_memory_for_client(client_id, mem).await;
                    }

                    // Execute what the model asked for. These were discarded behind a note
                    // saying it was "to avoid recursion" and that "responses don't trigger
                    // new operations" -- the recursion was real, the claim was not. Reading
                    // search hits and then fetching one of the documents, or refining the
                    // query, is exactly what a search client is for, and none of it worked.
                    //
                    // `run_request_once` issues the request without raising an event itself;
                    // this loop reports the outcome, bounded by MAX_FOLLOWUP_DEPTH. Every
                    // verb the model is offered on this event has an arm here — an advertised
                    // verb with no arm is the same defect one level down.
                    use crate::llm::actions::client_trait::{Client, ClientActionResult};
                    for action in actions {
                        let Ok(ClientActionResult::Custom { name, data }) =
                            protocol.execute_action(action.clone())
                        else {
                            continue;
                        };
                        // `default_index` before `_all`: an operator who named a default
                        // index meant that one, and `_all` is a much wider blast radius
                        // than either the model or the operator asked for.
                        let index = match data.get("index").and_then(|v| v.as_str()) {
                            Some(named) if !named.is_empty() => named.to_string(),
                            _ => Self::resolve_index(&app_state, client_id, "")
                                .await
                                .unwrap_or_else(|_| "_all".to_string()),
                        };
                        let index = index.as_str();
                        let plan = match name.as_str() {
                            "search" => Some((
                                hyper::Method::POST,
                                format!("{index}/_search"),
                                Some(RequestBody::Json(serde_json::json!({
                                    "query": data.get("query").cloned()
                                        .unwrap_or(serde_json::json!({"match_all": {}}))
                                }))),
                            )),
                            "get_document" => data
                                .get("id")
                                .and_then(|v| v.as_str())
                                .map(|id| (hyper::Method::GET, format!("{index}/_doc/{id}"), None)),
                            "delete_document" => {
                                data.get("id").and_then(|v| v.as_str()).map(|id| {
                                    (hyper::Method::DELETE, format!("{index}/_doc/{id}"), None)
                                })
                            }
                            "index_document" => Some((
                                hyper::Method::POST,
                                format!("{index}/_doc"),
                                data.get("document").cloned().map(RequestBody::Json),
                            )),
                            // `_bulk` is cluster-wide and every entry names its own index,
                            // so it deliberately ignores `index` — the same exemption
                            // `apply_action` makes. It used to fall through to the arm below
                            // and be logged as skipped, which left the one verb that batches
                            // work unusable in reply to a result.
                            "bulk_operation" => {
                                let ops = data
                                    .get("operations")
                                    .and_then(|v| v.as_array())
                                    .cloned()
                                    .unwrap_or_default();
                                match Self::build_bulk_ndjson(ops) {
                                    Ok(ndjson) => Some((
                                        hyper::Method::POST,
                                        "_bulk".to_string(),
                                        Some(RequestBody::Ndjson(ndjson)),
                                    )),
                                    Err(e) => {
                                        error!(
                                            "Elasticsearch client {} follow-up bulk_operation \
                                             is malformed: {}",
                                            client_id, e
                                        );
                                        None
                                    }
                                }
                            }
                            other => {
                                info!(
                                    "Elasticsearch client {} follow-up '{}' has no wire \
                                     effect; skipped",
                                    client_id, other
                                );
                                None
                            }
                        };
                        let Some((method, path, body)) = plan else {
                            continue;
                        };
                        match Self::run_request_once(client_id, method, &path, body, &app_state)
                            .await
                        {
                            Ok((code, body)) => {
                                info!(
                                    "Elasticsearch client {} follow-up {} -> HTTP {}",
                                    client_id, name, code
                                );
                                // Tell the model what the follow-up returned. Without this
                                // the chain was exactly one step deep: a `search` issued in
                                // response to an index confirmation ran, and its hits went
                                // nowhere -- so the model could never read the results of a
                                // search it had asked for, which is what a search client is
                                // for. Boxed because the cycle is real, bounded because it
                                // would otherwise be endless.
                                if depth + 1 < Self::MAX_FOLLOWUP_DEPTH {
                                    if let Err(e) = Box::pin(Self::call_llm_with_response(
                                        client_id,
                                        name.clone(),
                                        code,
                                        body,
                                        app_state.clone(),
                                        llm_client.clone(),
                                        status_tx.clone(),
                                        depth + 1,
                                    ))
                                    .await
                                    {
                                        error!(
                                            "Elasticsearch client {} follow-up notification \
                                             failed: {}",
                                            client_id, e
                                        );
                                    }
                                } else {
                                    warn!(
                                        "Elasticsearch client {} reached the follow-up depth \
                                         limit ({}); not reporting {} to the model",
                                        client_id,
                                        Self::MAX_FOLLOWUP_DEPTH,
                                        name
                                    );
                                }
                            }
                            Err(e) => error!(
                                "Elasticsearch client {} follow-up {} failed: {}",
                                client_id, name, e
                            ),
                        }
                    }
                }
                Err(e) => {
                    error!("LLM error for Elasticsearch client {}: {}", client_id, e);
                }
            }
        }

        Ok(())
    }
}