datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
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
//! HTTP route handlers for LLM query workflow.

use super::service::LlmQueryService;
use super::session::SessionManager;
use super::types::*;
use crate::datafold_node::OperationProcessor;
use crate::DataFoldNode;
use crate::fold_db_core::query::records_from_field_map;
use crate::ingestion::IngestionConfig;
use crate::schema::SchemaState;
use crate::server::http_server::AppState;
use actix_web::{web, HttpResponse, Responder};
use serde_json::{json, Value};
use std::sync::Arc;

/// Generate a backfill hash for a transform schema
async fn generate_backfill_hash_for_transform(
    transform_manager: &crate::transform::manager::TransformManager,
    schema_name: &str,
) -> Option<String> {
    let transforms = match transform_manager.list_transforms() {
        Ok(t) => t,
        Err(e) => {
            log::warn!("Failed to list transforms for {}: {}", schema_name, e);
            return None;
        }
    };

    let transform = match transforms.get(schema_name) {
        Some(t) => t,
        None => {
            log::debug!("Transform {} not found in transform list", schema_name);
            return None;
        }
    };

    // Look up the transform's schema from the database
    let declarative_schema = match transform_manager
        .db_ops
        .get_schema(transform.get_schema_name())
        .await
    {
        Ok(Some(s)) => s,
        Ok(None) => {
            log::warn!("Transform {} schema not found in database", schema_name);
            return None;
        }
        Err(e) => {
            log::warn!("Failed to get schema for transform {}: {}", schema_name, e);
            return None;
        }
    };

    let inputs = declarative_schema.get_inputs();
    let first_input = match inputs.first() {
        Some(i) => i,
        None => {
            log::warn!(
                "Transform {} has no inputs in declarative schema",
                schema_name
            );
            return None;
        }
    };

    let source_schema_name = match first_input.split('.').next() {
        Some(s) => s,
        None => {
            log::warn!("Failed to parse source schema from input: {}", first_input);
            return None;
        }
    };

    Some(
        crate::fold_db_core::infrastructure::backfill_tracker::BackfillTracker::generate_hash(
            schema_name,
            source_schema_name,
        ),
    )
}

/// Shared state for LLM query routes
pub struct LlmQueryState {
    pub service: Option<Arc<LlmQueryService>>,
    pub session_manager: Arc<SessionManager>,
}

impl LlmQueryState {
    pub fn new() -> Self {
        let config = IngestionConfig::from_env_allow_empty();
        let service = match LlmQueryService::new(config) {
            Ok(svc) => Some(Arc::new(svc)),
            Err(e) => {
                log::warn!("LLM Query service not available: {}. LLM query endpoints will return errors until configured.", e);
                None
            }
        };
        let session_manager = Arc::new(SessionManager::new());
        Self {
            service,
            session_manager,
        }
    }
}

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

/// Analyze a natural language query
#[utoipa::path(
    post,
    path = "/api/llm-query/analyze",
    tag = "llm-query",
    request_body = AnalyzeQueryRequest,
    responses(
        (status = 200, description = "Query analysis result", body = AnalyzeQueryResponse),
        (status = 400, description = "Bad request"),
        (status = 500, description = "Server error")
    )
)]
pub async fn analyze_query(
    request: web::Json<AnalyzeQueryRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    // Get available schemas
    let schemas: Vec<crate::schema::SchemaWithState> = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        match db_guard.schema_manager.get_schemas_with_states() {
            Ok(schemas) => schemas,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to get schemas: {}", e)}));
            }
        }
    };

    // Create or get session
    let session_id = match llm_state
        .session_manager
        .create_or_get_session(request.session_id.clone(), request.query.clone())
    {
        Ok(id) => id,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to create session: {}", e)}));
        }
    };

    // Check if LLM service is available
    let service = match &llm_state.service {
        Some(svc) => svc,
        None => {
            return HttpResponse::ServiceUnavailable()
                .json(json!({
                    "error": "LLM Query service not configured",
                    "message": "Please configure AI_PROVIDER and FOLD_OPENROUTER_API_KEY or OLLAMA_BASE_URL environment variables to use this feature"
                }));
        }
    };

    // Analyze query with LLM
    let query_plan = match service.analyze_query(&request.query, &schemas).await {
        Ok(plan) => plan,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to analyze query: {}", e)}));
        }
    };

    // Store the query plan in session
    if let Err(e) = llm_state.session_manager.add_message(
        &session_id,
        "assistant".to_string(),
        format!("Query plan: {}", query_plan.reasoning),
    ) {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to update session: {}", e)}));
    }

    HttpResponse::Ok().json(AnalyzeQueryResponse {
        session_id,
        query_plan,
    })
}

/// Execute a query plan
#[utoipa::path(
    post,
    path = "/api/llm-query/execute",
    tag = "llm-query",
    request_body = ExecuteQueryPlanRequest,
    responses(
        (status = 200, description = "Query execution result", body = ExecuteQueryPlanResponse),
        (status = 400, description = "Bad request"),
        (status = 500, description = "Server error")
    )
)]
pub async fn execute_query_plan(
    request: web::Json<ExecuteQueryPlanRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    let session_id = &request.session_id;
    let query_plan = &request.query_plan;

    // If index schema is needed, create it
    let mut backfill_hash: Option<String> = None;
    if let Some(ref index_schema) = query_plan.index_schema {
        // Load the schema
        let schema_name = index_schema.name.clone();
        {
            let node = app_state.node.read().await;
            let db_guard = match node.get_fold_db().await {
                Ok(guard) => guard,
                Err(e) => {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to access database: {}", e)}));
                }
            };

            // Interpret and load the schema from the definition
            let schema = match db_guard
                .schema_manager
                .interpret_declarative_schema(index_schema.clone())
                .await
            {
                Ok(s) => s,
                Err(e) => {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to interpret schema: {}", e)}));
                }
            };

            if let Err(e) = db_guard.schema_manager.load_schema_internal(schema).await {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to load schema: {}", e)}));
            }

            // Check if this is a transform schema and generate backfill hash if needed
            let is_transform = match db_guard.transform_manager.transform_exists(&schema_name) {
                Ok(exists) => exists,
                Err(e) => {
                    log::warn!("Failed to check if {} is a transform: {}", schema_name, e);
                    false
                }
            };

            if is_transform {
                // Generate backfill hash for transform
                backfill_hash =
                    generate_backfill_hash_for_transform(&db_guard.transform_manager, &schema_name)
                        .await;
            }

            // Auto-approve the schema (idempotent - only approves if not already approved)
            if let Err(e) = db_guard
                .schema_manager
                .approve_with_backfill(&schema_name, backfill_hash.clone())
                .await
            {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to approve schema: {}", e)}));
            }
        }

        // Update session with created schema
        if let Err(e) = llm_state
            .session_manager
            .set_schema_created(session_id, schema_name)
        {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to update session: {}", e)}));
        }

        // If we have a backfill, check its status
        if let Some(ref hash) = backfill_hash {
            let backfill_info = {
                let node = app_state.node.read().await;
                let db_guard = match node.get_fold_db().await {
                    Ok(guard) => guard,
                    Err(e) => {
                        return HttpResponse::InternalServerError()
                            .json(json!({"error": format!("Failed to access database: {}", e)}));
                    }
                };
                db_guard.get_backfill_tracker().get_backfill_by_hash(hash)
            };

            if let Some(info) = backfill_info {
                let progress = if info.mutations_expected > 0 {
                    info.mutations_completed as f64 / info.mutations_expected as f64
                } else {
                    0.0
                };

                // If not complete, return pending status
                if info.status != crate::fold_db_core::infrastructure::backfill_tracker::BackfillStatus::Completed {
                    return HttpResponse::Ok().json(ExecuteQueryPlanResponse {
                        status: QueryExecutionStatus::Running,
                        backfill_progress: Some(progress),
                        results: None,
                        summary: None,
                    });
                }
            }
        }
    }

    // Execute the query
    let node_arc: Arc<tokio::sync::RwLock<DataFoldNode>> = Arc::clone(&app_state.node);
    let processor = OperationProcessor::new(node_arc.read().await.clone());
    let results = match processor.execute_query_map(query_plan.query.clone()).await {
        Ok(result_map) => {
            let records_map = records_from_field_map(&result_map);
            records_map
                .into_iter()
                .map(|(key, record)| json!({"key": key, "fields": record.fields, "metadata": record.metadata}))
                .collect::<Vec<Value>>()
        }
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to execute query: {}", e)}));
        }
    };

    // Store results in session
    if let Err(e) = llm_state
        .session_manager
        .add_results(session_id, results.clone())
    {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to store results: {}", e)}));
    }

    // Get session to access original query
    let original_query = match llm_state.session_manager.get_session(session_id) {
        Ok(Some(ctx)) => ctx.original_query,
        Ok(None) => {
            return HttpResponse::NotFound().json(json!({"error": "Session not found"}));
        }
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to get session: {}", e)}));
        }
    };

    // Summarize results with LLM if available
    let summary = if let Some(ref service) = llm_state.service {
        match service.summarize_results(&original_query, &results).await {
            Ok(s) => Some(s),
            Err(e) => {
                log::warn!("Failed to summarize results: {}", e);
                None
            }
        }
    } else {
        None
    };

    HttpResponse::Ok().json(ExecuteQueryPlanResponse {
        status: QueryExecutionStatus::Complete,
        backfill_progress: Some(1.0),
        results: Some(results),
        summary,
    })
}

/// Analyze if a follow-up question can be answered from existing context
#[utoipa::path(
    post,
    path = "/api/llm-query/analyze-followup",
    tag = "llm-query",
    request_body = ChatRequest,
    responses(
        (status = 200, description = "Follow-up analysis result", body = FollowupAnalysis),
        (status = 404, description = "Session not found"),
        (status = 500, description = "Server error")
    )
)]
pub async fn analyze_followup(
    request: web::Json<ChatRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    let session_id = &request.session_id;
    let question = &request.question;

    let context = match llm_state.session_manager.get_session(session_id) {
        Ok(Some(ctx)) => ctx,
        Ok(None) => {
            return HttpResponse::NotFound().json(json!({"error": "Session not found"}));
        }
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to get session: {}", e)}));
        }
    };

    let results = match context.query_results {
        Some(ref r) => r,
        None => {
            return HttpResponse::BadRequest()
                .json(json!({"error": "No query results available in session"}));
        }
    };

    let service = match &llm_state.service {
        Some(svc) => svc,
        None => {
            return HttpResponse::ServiceUnavailable()
                .json(json!({
                    "error": "LLM Query service not configured",
                    "message": "Please configure AI_PROVIDER and FOLD_OPENROUTER_API_KEY or OLLAMA_BASE_URL environment variables to use this feature"
                }));
        }
    };

    let schemas: Vec<crate::schema::SchemaWithState> = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        match db_guard.schema_manager.get_schemas_with_states() {
            Ok(schemas) => schemas,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to get schemas: {}", e)}));
            }
        }
    };

    let analysis = match service
        .analyze_followup_question(&context.original_query, results, question, &schemas)
        .await
    {
        Ok(a) => a,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to analyze followup question: {}", e)}));
        }
    };

    HttpResponse::Ok().json(analysis)
}

/// Ask a follow-up question about query results
#[utoipa::path(
    post,
    path = "/api/llm-query/chat",
    tag = "llm-query",
    request_body = ChatRequest,
    responses(
        (status = 200, description = "Answer to question", body = ChatResponse),
        (status = 404, description = "Session not found"),
        (status = 500, description = "Server error")
    )
)]
pub async fn chat(
    request: web::Json<ChatRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    let session_id = &request.session_id;
    let question = &request.question;

    let context = match llm_state.session_manager.get_session(session_id) {
        Ok(Some(ctx)) => ctx,
        Ok(None) => {
            return HttpResponse::NotFound().json(json!({"error": "Session not found"}));
        }
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to get session: {}", e)}));
        }
    };

    let results = match context.query_results {
        Some(ref r) => r,
        None => {
            return HttpResponse::BadRequest()
                .json(json!({"error": "No query results available in session"}));
        }
    };

    let service = match &llm_state.service {
        Some(svc) => svc,
        None => {
            return HttpResponse::ServiceUnavailable()
                .json(json!({
                    "error": "LLM Query service not configured",
                    "message": "Please configure AI_PROVIDER and FOLD_OPENROUTER_API_KEY or OLLAMA_BASE_URL environment variables to use this feature"
                }));
        }
    };

    let schemas: Vec<crate::schema::SchemaWithState> = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        match db_guard.schema_manager.get_schemas_with_states() {
            Ok(schemas) => schemas,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to get schemas: {}", e)}));
            }
        }
    };

    let analysis = match service
        .analyze_followup_question(&context.original_query, results, question, &schemas)
        .await
    {
        Ok(a) => a,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to analyze followup question: {}", e)}));
        }
    };

    let mut combined_results = results.clone();
    let mut executed_query = false;
    let mut retry_info: Option<String> = None;

    if analysis.needs_query {
        if let Some(ref initial_query) = analysis.query {
            executed_query = true;

            let mut current_query = initial_query.clone();
            let mut attempts: Vec<String> = Vec::new();
            const MAX_FOLLOWUP_ATTEMPTS: usize = 3;

            for attempt in 0..MAX_FOLLOWUP_ATTEMPTS {
                let node_arc: Arc<tokio::sync::RwLock<DataFoldNode>> = Arc::clone(&app_state.node);
                let processor = OperationProcessor::new(node_arc.read().await.clone());
                match processor.execute_query_map(current_query.clone()).await {
                    Ok(result_map) => {
                        let records_map = records_from_field_map(&result_map);
                        let new_results: Vec<Value> = records_map
                            .into_iter()
                            .map(|(key, record)| json!({"key": key, "fields": record.fields, "metadata": record.metadata}))
                            .collect();

                        if !new_results.is_empty() {
                            if attempt > 0 {
                                retry_info = Some(format!(
                                    "Found results using alternative strategy after {} attempts",
                                    attempt + 1
                                ));
                            }
                            combined_results = new_results;
                            break;
                        }

                        attempts.push(format!(
                            "Schema: {}, Filter: {:?}",
                            current_query.schema_name, current_query.filter
                        ));

                        if attempt < MAX_FOLLOWUP_ATTEMPTS - 1 {
                            match service
                                .suggest_alternative_query(
                                    question,
                                    &current_query,
                                    &schemas,
                                    &attempts,
                                )
                                .await
                            {
                                Ok(Some(alternative_plan)) => {
                                    current_query = alternative_plan.query;
                                }
                                Ok(None) => {
                                    retry_info = Some(format!(
                                        "No results found after trying {} approaches",
                                        attempt + 1
                                    ));
                                    break;
                                }
                                Err(e) => {
                                    log::warn!(
                                        "Failed to generate alternative for follow-up: {}",
                                        e
                                    );
                                    break;
                                }
                            }
                        } else {
                            retry_info = Some(format!(
                                "No results found after trying {} approaches",
                                MAX_FOLLOWUP_ATTEMPTS
                            ));
                        }
                    }
                    Err(e) => {
                        log::warn!("Failed to execute followup query: {}", e);
                        break;
                    }
                }
            }
        }
    }

    let answer = match service
        .answer_question(
            &context.original_query,
            &combined_results,
            &context.conversation_history,
            question,
        )
        .await
    {
        Ok(a) => a,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to get answer: {}", e)}));
        }
    };

    if let Err(e) =
        llm_state
            .session_manager
            .add_message(session_id, "user".to_string(), question.clone())
    {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to update session: {}", e)}));
    }

    let mut assistant_message = if executed_query {
        format!("[Executed new query: {}]\n\n{}", analysis.reasoning, answer)
    } else {
        answer.clone()
    };

    if let Some(info) = retry_info {
        assistant_message.push_str(&format!("\n\n[Note: {}]", info));
    }

    if let Err(e) = llm_state.session_manager.add_message(
        session_id,
        "assistant".to_string(),
        assistant_message.clone(),
    ) {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to update session: {}", e)}));
    }

    HttpResponse::Ok().json(ChatResponse {
        answer: assistant_message,
        context_used: true,
    })
}

/// Get backfill status for a transform
#[utoipa::path(
    get,
    path = "/api/llm-query/backfill/{hash}",
    tag = "llm-query",
    params(
        ("hash" = String, Path, description = "Backfill hash")
    ),
    responses(
        (status = 200, description = "Backfill status", body = BackfillStatusResponse),
        (status = 404, description = "Backfill not found"),
        (status = 500, description = "Server error")
    )
)]
pub async fn get_backfill_status(
    path: web::Path<String>,
    app_state: web::Data<AppState>,
) -> impl Responder {
    let backfill_hash = path.into_inner();

    let backfill_info = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        db_guard
            .get_backfill_tracker()
            .get_backfill_by_hash(&backfill_hash)
    };

    match backfill_info {
        Some(info) => {
            let progress = if info.mutations_expected > 0 {
                info.mutations_completed as f64 / info.mutations_expected as f64
            } else {
                0.0
            };

            HttpResponse::Ok().json(BackfillStatusResponse {
                status: format!("{:?}", info.status),
                progress,
                total_records: info.mutations_expected,
                processed_records: info.mutations_completed,
                estimated_completion: None,
            })
        }
        None => HttpResponse::NotFound().json(json!({"error": "Backfill not found"})),
    }
}

/// Single-step query execution: analyze, create index, wait for backfill, execute, and summarize
#[utoipa::path(
    post,
    path = "/api/llm-query/run",
    tag = "llm-query",
    request_body = RunQueryRequest,
    responses(
        (status = 200, description = "Query execution complete", body = RunQueryResponse),
        (status = 400, description = "Bad request"),
        (status = 500, description = "Server error")
    )
)]
pub async fn run_query(
    request: web::Json<RunQueryRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    let service = match &llm_state.service {
        Some(svc) => svc,
        None => {
            return HttpResponse::ServiceUnavailable()
                .json(json!({
                    "error": "LLM Query service not configured",
                    "message": "Please configure AI_PROVIDER and FOLD_OPENROUTER_API_KEY or OLLAMA_BASE_URL environment variables to use this feature"
                }));
        }
    };

    let schemas: Vec<crate::schema::SchemaWithState> = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        match db_guard.schema_manager.get_schemas_with_states() {
            Ok(schemas) => schemas,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to get schemas: {}", e)}));
            }
        }
    };

    let session_id = match llm_state
        .session_manager
        .create_or_get_session(request.session_id.clone(), request.query.clone())
    {
        Ok(id) => id,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to create session: {}", e)}));
        }
    };

    let query_plan = match service.analyze_query(&request.query, &schemas).await {
        Ok(plan) => plan,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to analyze query: {}", e)}));
        }
    };

    if let Err(e) = llm_state.session_manager.add_message(
        &session_id,
        "assistant".to_string(),
        format!("Query plan: {}", query_plan.reasoning),
    ) {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to update session: {}", e)}));
    }

    let mut backfill_hash: Option<String> = None;
    if let Some(ref index_schema) = query_plan.index_schema {
        let schema_name = index_schema.name.clone();
        {
            let node = app_state.node.read().await;
            let db_guard = match node.get_fold_db().await {
                Ok(guard) => guard,
                Err(e) => {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to access database: {}", e)}));
                }
            };

            let schema = match db_guard
                .schema_manager
                .interpret_declarative_schema(index_schema.clone())
                .await
            {
                Ok(s) => s,
                Err(e) => {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to interpret schema: {}", e)}));
                }
            };

            if let Err(e) = db_guard.schema_manager.load_schema_internal(schema).await {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to load schema: {}", e)}));
            }

            let is_transform = match db_guard.transform_manager.transform_exists(&schema_name) {
                Ok(exists) => exists,
                Err(e) => {
                    log::warn!("Failed to check if {} is a transform: {}", schema_name, e);
                    false
                }
            };

            if is_transform {
                backfill_hash =
                    generate_backfill_hash_for_transform(&db_guard.transform_manager, &schema_name)
                        .await;
            }

            let current_state = match db_guard.schema_manager.get_schema_states() {
                Ok(states) => states.get(&schema_name).copied().unwrap_or_default(),
                Err(e) => {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to get schema states: {}", e)}));
                }
            };

            if current_state != SchemaState::Approved {
                if let Err(e) = db_guard
                    .schema_manager
                    .set_schema_state_with_backfill(
                        &schema_name,
                        SchemaState::Approved,
                        backfill_hash.clone(),
                    )
                    .await
                {
                    return HttpResponse::InternalServerError()
                        .json(json!({"error": format!("Failed to approve schema: {}", e)}));
                }
            }
        }

        if let Err(e) = llm_state
            .session_manager
            .set_schema_created(&session_id, schema_name)
        {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to update session: {}", e)}));
        }

        if let Some(ref hash) = backfill_hash {
            loop {
                let backfill_info = {
                    let node = app_state.node.read().await;
                    let db_guard = match node.get_fold_db().await {
                        Ok(guard) => guard,
                        Err(e) => {
                            return HttpResponse::InternalServerError().json(
                                json!({"error": format!("Failed to access database: {}", e)}),
                            );
                        }
                    };
                    db_guard.get_backfill_tracker().get_backfill_by_hash(hash)
                };

                if let Some(info) = backfill_info {
                    if info.status == crate::fold_db_core::infrastructure::backfill_tracker::BackfillStatus::Completed {
                        break;
                    }
                    if info.status == crate::fold_db_core::infrastructure::backfill_tracker::BackfillStatus::Failed {
                        return HttpResponse::InternalServerError()
                            .json(json!({"error": "Backfill failed"}));
                    }
                } else {
                    break;
                }

                tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
            }
        }
    }

    let mut current_query_plan = query_plan.clone();
    let mut results: Vec<Value> = Vec::new();
    let mut attempts: Vec<String> = Vec::new();
    const MAX_ATTEMPTS: usize = 5;

    for attempt in 0..MAX_ATTEMPTS {
        let node_arc: Arc<tokio::sync::RwLock<DataFoldNode>> = Arc::clone(&app_state.node);
        let processor = OperationProcessor::new(node_arc.read().await.clone());
        match processor
            .execute_query_map(current_query_plan.query.clone())
            .await
        {
            Ok(result_map) => {
                let records_map = records_from_field_map(&result_map);
                results = records_map
                    .into_iter()
                    .map(|(key, record)| json!({"key": key, "fields": record.fields, "metadata": record.metadata}))
                    .collect();

                if !results.is_empty() {
                    break;
                }

                attempts.push(format!(
                    "Schema: {}, Filter: {:?} - {}",
                    current_query_plan.query.schema_name,
                    current_query_plan.query.filter,
                    current_query_plan.reasoning
                ));

                if attempt < MAX_ATTEMPTS - 1 {
                    match service
                        .suggest_alternative_query(
                            &request.query,
                            &current_query_plan.query,
                            &schemas,
                            &attempts,
                        )
                        .await
                    {
                        Ok(Some(alternative_plan)) => {
                            current_query_plan = alternative_plan;
                        }
                        Ok(None) => {
                            break;
                        }
                        Err(e) => {
                            log::warn!("Failed to generate alternative query: {}", e);
                            break;
                        }
                    }
                }
            }
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to execute query: {}", e)}));
            }
        }
    }

    if let Err(e) = llm_state
        .session_manager
        .add_results(&session_id, results.clone())
    {
        return HttpResponse::InternalServerError()
            .json(json!({"error": format!("Failed to store results: {}", e)}));
    }

    let original_query = match llm_state.session_manager.get_session(&session_id) {
        Ok(Some(ctx)) => ctx.original_query,
        Ok(None) => {
            return HttpResponse::NotFound().json(json!({"error": "Session not found"}));
        }
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to get session: {}", e)}));
        }
    };

    let mut summary_text = match service.summarize_results(&original_query, &results).await {
        Ok(s) => s,
        Err(e) => {
            log::warn!("Failed to summarize results: {}", e);
            String::new()
        }
    };

    if !attempts.is_empty() {
        let retry_info = if results.is_empty() {
            format!(
                "\n\n[Note: No results found after trying {} different approaches: {}]",
                attempts.len() + 1,
                attempts.join("; ")
            )
        } else {
            format!(
                "\n\n[Note: Found results using alternative strategy after {} attempts. Final approach: {}]",
                attempts.len() + 1,
                current_query_plan.reasoning
            )
        };
        summary_text.push_str(&retry_info);
    }

    let final_summary = if summary_text.is_empty() {
        None
    } else {
        Some(summary_text)
    };

    HttpResponse::Ok().json(RunQueryResponse {
        session_id,
        query_plan: current_query_plan,
        results,
        summary: final_summary,
    })
}

/// Execute an AI-native index query workflow
#[utoipa::path(
    post,
    path = "/api/llm-query/native-index",
    tag = "llm-query",
    request_body = RunQueryRequest,
    responses(
        (status = 200, description = "AI-native index query result", body = String),
        (status = 400, description = "Bad request"),
        (status = 500, description = "Server error")
    )
)]
pub async fn ai_native_index_query(
    request: web::Json<RunQueryRequest>,
    app_state: web::Data<AppState>,
    llm_state: web::Data<LlmQueryState>,
) -> impl Responder {
    let service = match &llm_state.service {
        Some(svc) => svc,
        None => {
            return HttpResponse::ServiceUnavailable()
                .json(json!({
                    "error": "LLM Query service not configured",
                    "message": "Please configure AI_PROVIDER and FOLD_OPENROUTER_API_KEY or OLLAMA_BASE_URL environment variables to use this feature"
                }));
        }
    };

    // Create or get session to maintain conversation context
    let session_id = match llm_state
        .session_manager
        .create_or_get_session(request.session_id.clone(), request.query.clone())
    {
        Ok(id) => id,
        Err(e) => {
            return HttpResponse::InternalServerError()
                .json(json!({"error": format!("Failed to create session: {}", e)}));
        }
    };

    // Get available schemas
    let schemas: Vec<crate::schema::SchemaWithState> = {
        let node = app_state.node.read().await;
        let db_guard = match node.get_fold_db().await {
            Ok(guard) => guard,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to access database: {}", e)}));
            }
        };
        match db_guard.schema_manager.get_schemas_with_states() {
            Ok(schemas) => schemas,
            Err(e) => {
                return HttpResponse::InternalServerError()
                    .json(json!({"error": format!("Failed to get schemas: {}", e)}));
            }
        }
    };

    // Execute AI-native index query workflow
    let result = async {
        let node = app_state.node.read().await;
        let db_ops = match node.get_fold_db().await {
            Ok(guard) => guard.get_db_ops(),
            Err(e) => {
                return Err(format!("Failed to access database: {}", e));
            }
        };
        drop(node); // Drop the mutex guard before await

        // Get both AI interpretation and raw results
        service
            .execute_ai_native_index_query_with_results(&request.query, &schemas, &db_ops)
            .await
    }
    .await;

    match result {
        Ok((ai_interpretation, raw_results)) => {
            // Store results in session for context tracking
            let results_as_json: Vec<serde_json::Value> = raw_results
                .into_iter()
                .map(|result| serde_json::to_value(result).unwrap_or(json!({})))
                .collect();

            if let Err(e) = llm_state
                .session_manager
                .add_results(&session_id, results_as_json.clone())
            {
                log::warn!("Failed to store results in session: {}", e);
            }

            // Add user message to conversation history
            if let Err(e) = llm_state.session_manager.add_message(
                &session_id,
                "user".to_string(),
                request.query.clone(),
            ) {
                log::warn!("Failed to add user message to session: {}", e);
            }

            // Add AI response to conversation history
            if let Err(e) = llm_state.session_manager.add_message(
                &session_id,
                "assistant".to_string(),
                ai_interpretation.clone(),
            ) {
                log::warn!("Failed to add assistant message to session: {}", e);
            }

            HttpResponse::Ok().json(json!({
                "ai_interpretation": ai_interpretation,
                "raw_results": results_as_json,
                "query": request.query,
                "session_id": session_id
            }))
        }
        Err(e) => HttpResponse::InternalServerError()
            .json(json!({"error": format!("AI-native index query failed: {}", e)})),
    }
}