cf-mini-chat 0.1.28

Mini-chat module: multi-tenant AI chat
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
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
//! Thread summary outbox handler - processes `thread_summary` queue events.
//!
//! Runs as part of the outbox pipeline (leased strategy). All replicas
//! process events in parallel, partitioned by `chat_id`. No leader election needed.

use std::sync::{Arc, LazyLock};

use async_trait::async_trait;
use modkit_db::DBProvider;
use modkit_db::outbox::{LeasedMessageHandler, MessageResult, OutboxMessage};
use modkit_security::AccessScope;
use tracing::{debug, error, info, warn};

use crate::domain::ports::MiniChatMetricsPort;
use crate::domain::repos::{SummaryFrontier, ThreadSummaryRepository, ThreadSummaryTaskPayload};
use crate::infra::db::entity::message::MessageRole;

type DbProvider = DBProvider<modkit_db::DbError>;
type MessageRepo = crate::infra::db::repo::message_repo::MessageRepository;
type ThreadSummaryRepo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;

pub struct ThreadSummaryDeps {
    pub db: Arc<DbProvider>,
    pub thread_summary_repo: Arc<ThreadSummaryRepo>,
    pub message_repo: Arc<MessageRepo>,
    pub outbox_enqueuer: Arc<dyn crate::domain::repos::OutboxEnqueuer>,
    pub metrics: Arc<dyn MiniChatMetricsPort>,
    pub provider_resolver: Arc<crate::infra::llm::provider_resolver::ProviderResolver>,
    pub model_resolver: Arc<dyn crate::domain::repos::ModelResolver>,
    pub config: crate::config::background::ThreadSummaryWorkerConfig,
}

pub struct ThreadSummaryHandler {
    deps: Arc<ThreadSummaryDeps>,
}

impl ThreadSummaryHandler {
    pub fn new(deps: Arc<ThreadSummaryDeps>) -> Self {
        Self { deps }
    }
}

#[async_trait]
impl LeasedMessageHandler for ThreadSummaryHandler {
    #[tracing::instrument(name = "worker", skip_all, fields(worker = "thread_summary"))]
    async fn handle(&self, msg: &OutboxMessage) -> MessageResult {
        // 1. Deserialize payload
        let payload: ThreadSummaryTaskPayload = match serde_json::from_slice(&msg.payload) {
            Ok(p) => p,
            Err(e) => {
                error!(
                    partition_id = msg.partition_id,
                    seq = msg.seq,
                    error = %e,
                    "thread summary: invalid payload, dead-lettering"
                );
                return MessageResult::Reject(format!("payload deserialization failed: {e}"));
            }
        };

        let base_frontier = match (
            &payload.base_frontier_created_at,
            &payload.base_frontier_message_id,
        ) {
            (Some(ca), Some(mid)) => Some(SummaryFrontier {
                created_at: *ca,
                message_id: *mid,
            }),
            (None, None) => None,
            _ => {
                error!(
                    chat_id = %payload.chat_id,
                    "thread summary: partial frontier (one field set, other missing), dead-lettering"
                );
                return MessageResult::Reject(
                    "partial base_frontier: both fields must be set or both absent".to_owned(),
                );
            }
        };

        let target_frontier = SummaryFrontier {
            created_at: payload.frozen_target_created_at,
            message_id: payload.frozen_target_message_id,
        };

        // 2. Pre-check: verify stored frontier still matches base_frontier
        let conn = match self.deps.db.conn() {
            Ok(c) => c,
            Err(e) => {
                warn!(
                    chat_id = %payload.chat_id,
                    error = %e,
                    "thread summary: DB connection failed"
                );
                return MessageResult::Retry;
            }
        };

        let scope = AccessScope::for_tenant(payload.tenant_id);

        let current = self
            .deps
            .thread_summary_repo
            .get_latest(&conn, &scope, payload.chat_id)
            .await;

        match &current {
            Ok(Some(existing)) => {
                if base_frontier.as_ref() != Some(&existing.frontier) {
                    info!(
                        chat_id = %payload.chat_id,
                        "thread summary: frontier already advanced, skipping"
                    );
                    self.deps.metrics.record_thread_summary_cas_conflict();
                    return MessageResult::Ok;
                }
            }
            Ok(None) => {
                if base_frontier.is_some() {
                    warn!(
                        chat_id = %payload.chat_id,
                        "thread summary: expected frontier but none found, skipping"
                    );
                    return MessageResult::Ok;
                }
            }
            Err(e) => {
                warn!(
                    chat_id = %payload.chat_id,
                    error = %e,
                    "thread summary: pre-check query failed"
                );
                return MessageResult::Retry;
            }
        }

        // 3. Load messages in range
        let messages = match crate::domain::repos::MessageRepository::fetch_messages_in_range(
            self.deps.message_repo.as_ref(),
            &conn,
            &scope,
            payload.chat_id,
            base_frontier.as_ref(),
            &target_frontier,
        )
        .await
        {
            Ok(m) => m,
            Err(e) => {
                warn!(
                    chat_id = %payload.chat_id,
                    error = %e,
                    "thread summary: message fetch failed"
                );
                return MessageResult::Retry;
            }
        };

        // Note: `conn` is a pool handle released when it goes out of scope.
        // The LLM call below may take seconds, but conn is a lightweight
        // reference — actual pool slot is managed by the DB pool internals.

        if messages.is_empty() {
            debug!(
                chat_id = %payload.chat_id,
                "thread summary: no messages in range, skipping"
            );
            return MessageResult::Ok;
        }

        // 4. Generate summary via LLM
        let existing_summary = current
            .as_ref()
            .ok()
            .and_then(|c| c.as_ref())
            .map(|s| s.content.as_str());

        // 4a. Resolve model
        let model_id = if self.deps.config.summary_model_id.is_empty() {
            "gpt-4.1-mini".to_owned()
        } else {
            self.deps.config.summary_model_id.clone()
        };

        let resolved_model = match self
            .deps
            .model_resolver
            .resolve_model(
                modkit_security::constants::DEFAULT_SUBJECT_ID,
                Some(model_id.clone()),
            )
            .await
        {
            Ok(m) => m,
            Err(e) => {
                warn!(chat_id = %payload.chat_id, error = %e, "thread summary: model resolution failed");
                self.deps.metrics.record_thread_summary_execution("retry");
                return MessageResult::Retry;
            }
        };

        // 4b. Resolve provider
        let tenant_id_str = payload.tenant_id.to_string();
        let resolved_provider = match self
            .deps
            .provider_resolver
            .resolve(&resolved_model.provider_id, Some(&tenant_id_str))
        {
            Ok(p) => p,
            Err(e) => {
                warn!(chat_id = %payload.chat_id, error = %e, "thread summary: provider resolution failed");
                self.deps.metrics.record_thread_summary_execution("retry");
                return MessageResult::Retry;
            }
        };

        // 4c. Build prompt — 3-tier cascade: model-specific → global config → default
        let system_prompt = if !resolved_model.thread_summary_prompt.is_empty() {
            resolved_model.thread_summary_prompt.clone()
        } else if !self.deps.config.summary_system_prompt.is_empty() {
            self.deps.config.summary_system_prompt.clone()
        } else {
            crate::config::background::ThreadSummaryWorkerConfig::default().summary_system_prompt
        };

        // Build full OAGW proxy path: {alias}{api_path} — same as stream flow.
        let api_path = resolved_provider
            .api_path
            .replace("{model}", &resolved_model.provider_model_id);
        let upstream_path = format!("{}{api_path}", resolved_provider.upstream_alias);

        // 4d. Call LLM with PTL retry — if context_length_exceeded, drop
        //     oldest messages and retry (up to 2 times).
        let max_ptl_retries = 2u32;
        let mut messages_for_prompt = messages.clone();
        let mut ptl_retries = 0u32;
        let content_limit = self.deps.config.message_content_limit;

        #[allow(clippy::expect_used)]
        let security_ctx = modkit_security::SecurityContext::builder()
            .subject_tenant_id(payload.tenant_id)
            .subject_id(modkit_security::constants::DEFAULT_SUBJECT_ID)
            .build()
            .expect("tenant SecurityContext must build");

        let response = loop {
            let user_content =
                build_summary_prompt(existing_summary, &messages_for_prompt, content_limit);

            let request = crate::infra::llm::llm_request(&resolved_model.provider_model_id)
                .system_instructions(&system_prompt)
                .message(crate::infra::llm::LlmMessage::user(&user_content))
                .max_output_tokens(u64::from(resolved_model.max_output_tokens))
                .build_non_streaming();

            let ctx = security_ctx.clone();
            match resolved_provider
                .adapter
                .complete(ctx, request, &upstream_path)
                .await
            {
                Ok(r) => break r,
                Err(e) if is_context_length_error(&e) && ptl_retries < max_ptl_retries => {
                    let drop_count = (messages_for_prompt.len().div_ceil(5)).max(2);
                    let actual_drop = drop_count.min(messages_for_prompt.len().saturating_sub(2));
                    if actual_drop == 0 {
                        warn!(chat_id = %payload.chat_id, error = %e,
                              "thread summary: prompt too long, cannot drop more messages");
                        self.deps
                            .metrics
                            .record_thread_summary_execution("provider_error");
                        self.deps.metrics.record_summary_fallback();
                        return MessageResult::Retry;
                    }
                    messages_for_prompt.drain(..actual_drop);
                    ptl_retries += 1;
                    warn!(
                        chat_id = %payload.chat_id,
                        ptl_retries,
                        dropped = actual_drop,
                        remaining = messages_for_prompt.len(),
                        "thread summary: prompt too long, dropping oldest messages and retrying"
                    );
                }
                Err(e) => {
                    warn!(chat_id = %payload.chat_id, error = %e, "thread summary: LLM call failed");
                    self.deps
                        .metrics
                        .record_thread_summary_execution("provider_error");
                    self.deps.metrics.record_summary_fallback();
                    return MessageResult::Retry;
                }
            }
        };

        let summary_text = format_summary_output(&response.content);
        if summary_text.trim().is_empty() {
            warn!(chat_id = %payload.chat_id, "thread summary: LLM returned empty summary, retrying");
            self.deps
                .metrics
                .record_thread_summary_execution("empty_summary");
            return MessageResult::Retry;
        }
        let llm_usage = response.usage;
        let token_estimate = estimate_summary_tokens(llm_usage.output_tokens, summary_text.len());

        // 5. CAS-protected atomic commit
        let deps = Arc::clone(&self.deps);
        let base_clone = base_frontier.clone();
        let target_clone = target_frontier.clone();
        let summary_clone = summary_text;
        let msg_count = messages.len();
        let payload_clone = payload.clone();
        let model_id_clone = model_id;

        let cas_result = self
            .deps
            .db
            .transaction(|tx| {
                let deps = Arc::clone(&deps);
                let base_clone = base_clone.clone();
                let target_clone = target_clone.clone();
                let summary_clone = summary_clone.clone();
                let scope = scope.clone();
                let payload_clone = payload_clone.clone();
                let model_id_clone = model_id_clone.clone();
                Box::pin(async move {
                    // 5a. Upsert summary with CAS
                    let rows = deps
                        .thread_summary_repo
                        .upsert_with_cas(
                            tx,
                            payload_clone.chat_id,
                            payload_clone.tenant_id,
                            base_clone.as_ref(),
                            &target_clone,
                            &summary_clone,
                            token_estimate,
                        )
                        .await
                        .map_err(|e| modkit_db::DbError::Other(anyhow::anyhow!("{e}")))?;

                    if rows == 0 {
                        return Ok(false);
                    }

                    // 5b. Mark messages as compressed
                    crate::domain::repos::MessageRepository::mark_messages_compressed(
                        deps.message_repo.as_ref(),
                        tx,
                        &scope,
                        payload_clone.chat_id,
                        base_clone.as_ref(),
                        &target_clone,
                    )
                    .await
                    .map_err(|e| modkit_db::DbError::Other(anyhow::anyhow!("{e}")))?;

                    // 5c. Enqueue system usage event
                    let usage_event = mini_chat_sdk::UsageEvent {
                        tenant_id: payload_clone.tenant_id,
                        user_id: None,
                        chat_id: payload_clone.chat_id,
                        turn_id: None,
                        request_id: payload_clone.system_request_id,
                        effective_model: model_id_clone.clone(),
                        selected_model: model_id_clone,
                        terminal_state: "completed".to_owned(),
                        billing_outcome: "system_task".to_owned(),
                        usage: Some(mini_chat_sdk::UsageTokens {
                            input_tokens: u64::try_from(llm_usage.input_tokens.max(0)).unwrap_or(0),
                            output_tokens: u64::try_from(llm_usage.output_tokens.max(0))
                                .unwrap_or(0),
                            cache_read_input_tokens: u64::try_from(
                                llm_usage.cache_read_input_tokens.max(0),
                            )
                            .unwrap_or(0),
                            cache_write_input_tokens: u64::try_from(
                                llm_usage.cache_write_input_tokens.max(0),
                            )
                            .unwrap_or(0),
                            reasoning_tokens: u64::try_from(llm_usage.reasoning_tokens.max(0))
                                .unwrap_or(0),
                        }),
                        actual_credits_micro: 0,
                        settlement_method: "none".to_owned(),
                        policy_version_applied: 0,
                        web_search_calls: 0,
                        code_interpreter_calls: 0,
                        timestamp: time::OffsetDateTime::now_utc(),
                        requester_type: "system".to_owned(),
                        dedupe_key: Some(format!(
                            "{}/{}/{}",
                            payload_clone.tenant_id.as_simple(),
                            "thread_summary_update",
                            payload_clone.system_request_id.as_simple(),
                        )),
                        system_task_type: Some("thread_summary_update".to_owned()),
                    };
                    deps.outbox_enqueuer
                        .enqueue_usage_event(tx, usage_event)
                        .await
                        .map_err(|e| modkit_db::DbError::Other(anyhow::anyhow!("{e}")))?;

                    Ok(true)
                })
            })
            .await;

        match cas_result {
            Ok(true) => {
                self.deps.outbox_enqueuer.flush();
                self.deps.metrics.record_thread_summary_execution("success");
                info!(
                    chat_id = %payload.chat_id,
                    messages_compressed = msg_count,
                    "thread summary: committed successfully"
                );
                MessageResult::Ok
            }
            Ok(false) => {
                self.deps.metrics.record_thread_summary_cas_conflict();
                info!(
                    chat_id = %payload.chat_id,
                    "thread summary: CAS conflict on commit, skipping"
                );
                MessageResult::Ok
            }
            Err(e) => {
                warn!(
                    chat_id = %payload.chat_id,
                    error = %e,
                    "thread summary: commit failed"
                );
                self.deps.metrics.record_thread_summary_execution("retry");
                MessageResult::Retry
            }
        }
    }
}

// ════════════════════════════════════════════════════════════════════════════
// Prompt construction & output formatting
// ════════════════════════════════════════════════════════════════════════════

const ANALYSIS_INSTRUCTION: &str = "\
Before providing your final summary, wrap your analysis in <analysis> tags. In your analysis:
1. Chronologically review each exchange, identifying:
   - The user's requests and questions
   - Key decisions, answers, and information shared
   - Any follow-up actions or commitments
   - Specific names, dates, numbers, URLs, or references mentioned
2. Verify accuracy and completeness.

Your summary MUST include these sections:

1. Conversation Purpose: The user's primary goals and recurring themes
2. Key Information Exchanged: Important facts, decisions, recommendations, and answers
3. User Requests and Preferences: All explicit user requests, stated preferences, and corrections
4. Open Items: Any unresolved questions, pending actions, or things the user asked to revisit
5. Current Topic: What was being discussed most recently, with enough detail to continue naturally

Respond with an <analysis> block followed by a <summary> block.";

/// Build the user-message prompt for summary generation.
///
/// Two variants:
/// - **Full** (`existing_summary` is `None`): summarize the entire conversation.
/// - **Partial** (`existing_summary` is `Some`): incorporate existing summary with new messages.
fn build_summary_prompt(
    existing_summary: Option<&str>,
    messages: &[crate::infra::db::entity::message::Model],
    message_content_limit: usize,
) -> String {
    let mut prompt = String::new();

    if let Some(prev) = existing_summary {
        prompt.push_str("The existing summary below covers the earlier conversation. Incorporate it with the new messages into a single updated summary.\n\n");
        prompt.push_str("IMPORTANT: Keep the summary concise. If the combined information is too large, prioritize: current topic and recent decisions > user preferences and corrections > older facts. Compress or drop the least relevant older details rather than letting the summary grow unboundedly.\n\n");
        prompt.push_str("<existing_summary>\n");
        prompt.push_str(prev);
        prompt.push_str("\n</existing_summary>\n\n");
        prompt.push_str("New messages to incorporate:\n\n");
    } else {
        prompt.push_str("Summarize the following conversation:\n\n");
    }

    for msg in messages {
        // Skip system messages — they contain internal prompts that should
        // not leak into the stored summary (matches context_assembly behavior).
        if matches!(msg.role, MessageRole::System) {
            continue;
        }
        let role = match msg.role {
            MessageRole::User => "User",
            MessageRole::Assistant => "Assistant",
            MessageRole::System => unreachable!(),
        };
        let content =
            if message_content_limit > 0 && msg.content.chars().count() > message_content_limit {
                let truncated: String = msg.content.chars().take(message_content_limit).collect();
                format!("{truncated}...")
            } else {
                msg.content.clone()
            };
        prompt.push_str(role);
        prompt.push_str(": ");
        prompt.push_str(&content);
        prompt.push_str("\n\n");
    }

    prompt.push_str(ANALYSIS_INSTRUCTION);
    prompt
}

#[allow(clippy::expect_used)]
static ANALYSIS_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"(?s)<analysis>.*?</analysis>").expect("valid regex"));
#[allow(clippy::expect_used)]
static SUMMARY_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"(?s)<summary>(.*?)</summary>").expect("valid regex"));
#[allow(clippy::expect_used)]
static MULTI_NEWLINE_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| regex::Regex::new(r"\n{3,}").expect("valid regex"));

/// Strip the `<analysis>` drafting scratchpad and extract `<summary>` content.
///
/// The `<analysis>` block improves summary quality by forcing step-by-step
/// reasoning, but wastes tokens when stored. Only `<summary>` is persisted.
fn format_summary_output(raw: &str) -> String {
    let without_analysis = ANALYSIS_RE.replace(raw, "");

    if let Some(caps) = SUMMARY_RE.captures(&without_analysis) {
        let content = caps.get(1).map_or("", |m| m.as_str()).trim();
        MULTI_NEWLINE_RE.replace_all(content, "\n\n").into_owned()
    } else {
        // Graceful fallback: no <summary> tags
        let cleaned = ANALYSIS_RE.replace(raw, "");
        let trimmed = cleaned.trim();
        // Reject if residual markup tags remain (malformed LLM output)
        if trimmed.contains("<analysis") || trimmed.contains("<summary") {
            return String::new(); // empty → triggers retry upstream
        }
        MULTI_NEWLINE_RE.replace_all(trimmed, "\n\n").into_owned()
    }
}

/// Derive a token estimate for the stored summary.
/// Prefers actual `output_tokens` from the provider; falls back to `bytes/4`.
fn estimate_summary_tokens(output_tokens: i64, summary_byte_len: usize) -> i32 {
    if output_tokens > 0 {
        i32::try_from(output_tokens).unwrap_or(i32::MAX)
    } else {
        i32::try_from(summary_byte_len.div_ceil(4)).unwrap_or(i32::MAX)
    }
}

/// Check if an LLM error indicates the input exceeded the model's context length.
fn is_context_length_error(e: &crate::infra::llm::LlmProviderError) -> bool {
    let msg = e.to_string().to_lowercase();
    msg.contains("context_length_exceeded")
        || msg.contains("maximum context length")
        || msg.contains("token limit")
        || msg.contains("too many tokens")
}

#[cfg(test)]
mod tests {
    use super::*;
    use modkit_db::outbox::LeasedMessageHandler;

    // `rejects_invalid_payload` removed — superseded by `e2e_handler_rejects_invalid_payload`
    // which exercises the actual handler's Reject branch.

    #[test]
    fn placeholder_summary_builds_correctly() {
        use crate::infra::db::entity::message::Model;
        use time::OffsetDateTime;
        use uuid::Uuid;

        let msg = Model {
            id: Uuid::new_v4(),
            tenant_id: Uuid::new_v4(),
            chat_id: Uuid::new_v4(),
            request_id: Some(Uuid::new_v4()),
            role: MessageRole::User,
            content: "Hello world".to_owned(),
            content_type: "text/plain".to_owned(),
            token_estimate: 5,
            provider_response_id: None,
            request_kind: None,
            features_used: serde_json::json!([]),
            input_tokens: 0,
            output_tokens: 0,
            cache_read_input_tokens: 0,
            cache_write_input_tokens: 0,
            reasoning_tokens: 0,
            model: None,
            is_compressed: false,
            created_at: OffsetDateTime::now_utc(),
            deleted_at: None,
        };

        let summary = build_summary_prompt(None, &[msg], 4000);
        assert!(summary.contains("User: Hello world"));
    }

    #[test]
    fn token_estimate_prefers_output_tokens() {
        assert_eq!(estimate_summary_tokens(250, 1000), 250);
    }

    #[test]
    fn token_estimate_falls_back_to_len_div_4() {
        // 200 bytes / 4 = 50 tokens
        assert_eq!(estimate_summary_tokens(0, 200), 50);
    }

    // ── E2E tests: full handler pipeline with real DB ──────────────────

    /// Insert a message with controllable content and `created_at` for e2e tests.
    async fn insert_message(
        db: &modkit_db::Db,
        tenant_id: uuid::Uuid,
        chat_id: uuid::Uuid,
        role: MessageRole,
        content: &str,
        created_at: time::OffsetDateTime,
    ) -> uuid::Uuid {
        use crate::infra::db::entity::message::{
            ActiveModel as MessageAM, Entity as MessageEntity,
        };
        use sea_orm::Set;

        let msg_id = uuid::Uuid::new_v4();
        let am = MessageAM {
            id: Set(msg_id),
            tenant_id: Set(tenant_id),
            chat_id: Set(chat_id),
            request_id: Set(None),
            role: Set(role),
            content: Set(content.to_owned()),
            content_type: Set("text".to_owned()),
            token_estimate: Set(1),
            provider_response_id: Set(None),
            request_kind: Set(None),
            features_used: Set(serde_json::json!([])),
            input_tokens: Set(0),
            output_tokens: Set(0),
            cache_read_input_tokens: Set(0),
            cache_write_input_tokens: Set(0),
            reasoning_tokens: Set(0),
            model: Set(None),
            is_compressed: Set(false),
            created_at: Set(created_at),
            deleted_at: Set(None),
        };
        let conn = db.conn().unwrap();
        modkit_db::secure::secure_insert::<MessageEntity>(
            am,
            &modkit_security::AccessScope::allow_all(),
            &conn,
        )
        .await
        .expect("insert test message");
        msg_id
    }

    /// Insert a chat row for e2e tests.
    async fn insert_chat(db: &modkit_db::Db, tenant_id: uuid::Uuid, chat_id: uuid::Uuid) {
        use crate::infra::db::entity::chat::{ActiveModel as ChatAM, Entity as ChatEntity};
        use sea_orm::Set;
        let now = time::OffsetDateTime::now_utc();
        let am = ChatAM {
            id: Set(chat_id),
            tenant_id: Set(tenant_id),
            user_id: Set(uuid::Uuid::new_v4()),
            model: Set("gpt-5.2".to_owned()),
            title: Set(Some("test".to_owned())),
            is_temporary: Set(false),
            created_at: Set(now),
            updated_at: Set(now),
            deleted_at: Set(None),
        };
        let conn = db.conn().unwrap();
        modkit_db::secure::secure_insert::<ChatEntity>(
            am,
            &modkit_security::AccessScope::allow_all(),
            &conn,
        )
        .await
        .expect("insert chat");
    }

    /// Build a valid outbox message from a `ThreadSummaryTaskPayload`.
    fn make_outbox_msg(payload: &ThreadSummaryTaskPayload) -> OutboxMessage {
        OutboxMessage {
            partition_id: 0,
            seq: 1,
            payload: serde_json::to_vec(payload).unwrap(),
            payload_type: "application/json".to_owned(),
            created_at: chrono::Utc::now(),
            attempts: 0i16,
        }
    }

    /// Build `ThreadSummaryDeps` with real DB for e2e tests.
    /// Model/provider resolution will fail (no real providers), so these tests
    /// validate the handler's DB + CAS logic, not the LLM call path.
    async fn make_e2e_deps() -> (Arc<ThreadSummaryDeps>, modkit_db::Db) {
        use crate::domain::service::test_helpers;
        let db = test_helpers::inmem_db().await;
        let db_provider = test_helpers::mock_db_provider(db.clone());
        let deps = Arc::new(ThreadSummaryDeps {
            db: db_provider,
            thread_summary_repo: Arc::new(
                crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository,
            ),
            message_repo: Arc::new(
                crate::infra::db::repo::message_repo::MessageRepository::new(
                    modkit_db::odata::LimitCfg {
                        default: 20,
                        max: 100,
                    },
                ),
            ),
            outbox_enqueuer: Arc::new(test_helpers::RecordingOutboxEnqueuer::new()),
            metrics: Arc::new(crate::domain::ports::metrics::NoopMetrics),
            provider_resolver: Arc::new(
                crate::infra::llm::provider_resolver::ProviderResolver::empty(),
            ),
            model_resolver: Arc::new(test_helpers::MockModelResolver::default()),
            config: crate::config::background::ThreadSummaryWorkerConfig::default(),
        });
        (deps, db)
    }

    #[tokio::test]
    async fn e2e_handler_rejects_invalid_payload() {
        let (deps, _db) = make_e2e_deps().await;
        let handler = ThreadSummaryHandler::new(deps);
        let msg = OutboxMessage {
            partition_id: 0,
            seq: 1,
            payload: b"not json".to_vec(),
            payload_type: "application/json".to_owned(),
            created_at: chrono::Utc::now(),
            attempts: 0i16,
        };
        let result = handler.handle(&msg).await;
        assert!(matches!(result, MessageResult::Reject(_)));
    }

    #[tokio::test]
    async fn e2e_handler_skips_empty_message_range() {
        let (deps, db) = make_e2e_deps().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let now = time::OffsetDateTime::now_utc();
        // Create payload with a target frontier but no messages in range
        let payload = ThreadSummaryTaskPayload {
            tenant_id,
            chat_id,
            system_request_id: uuid::Uuid::new_v4(),
            system_task_type: "thread_summary_update".to_owned(),
            base_frontier_created_at: None,
            base_frontier_message_id: None,
            frozen_target_created_at: now,
            frozen_target_message_id: uuid::Uuid::new_v4(),
        };

        let handler = ThreadSummaryHandler::new(deps);
        let result = handler.handle(&make_outbox_msg(&payload)).await;
        // No messages → should succeed (skip)
        assert!(matches!(result, MessageResult::Ok));
    }

    #[tokio::test]
    async fn e2e_payload_serialization_roundtrip() {
        let now = time::OffsetDateTime::now_utc();
        let payload = ThreadSummaryTaskPayload {
            tenant_id: uuid::Uuid::new_v4(),
            chat_id: uuid::Uuid::new_v4(),
            system_request_id: uuid::Uuid::new_v4(),
            system_task_type: "thread_summary_update".to_owned(),
            base_frontier_created_at: Some(now),
            base_frontier_message_id: Some(uuid::Uuid::new_v4()),
            frozen_target_created_at: now,
            frozen_target_message_id: uuid::Uuid::new_v4(),
        };

        let json = serde_json::to_vec(&payload).unwrap();
        let deserialized: ThreadSummaryTaskPayload = serde_json::from_slice(&json).unwrap();
        assert_eq!(payload.tenant_id, deserialized.tenant_id);
        assert_eq!(payload.chat_id, deserialized.chat_id);
        assert_eq!(payload.system_request_id, deserialized.system_request_id);
        assert_eq!(payload.system_task_type, deserialized.system_task_type);
        assert_eq!(
            payload.base_frontier_created_at,
            deserialized.base_frontier_created_at
        );
        assert_eq!(
            payload.base_frontier_message_id,
            deserialized.base_frontier_message_id
        );
        assert_eq!(
            payload.frozen_target_created_at,
            deserialized.frozen_target_created_at
        );
        assert_eq!(
            payload.frozen_target_message_id,
            deserialized.frozen_target_message_id
        );
    }

    #[tokio::test]
    async fn e2e_repo_get_latest_returns_none_when_empty() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let repo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;
        let conn = db.conn().unwrap();
        let scope = AccessScope::for_tenant(tenant_id);
        let result = crate::domain::repos::ThreadSummaryRepository::get_latest(
            &repo, &conn, &scope, chat_id,
        )
        .await
        .unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn e2e_repo_upsert_first_summary_and_get_latest() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let now = time::OffsetDateTime::now_utc();
        let frontier = SummaryFrontier {
            created_at: now,
            message_id: uuid::Uuid::new_v4(),
        };

        let repo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;
        let conn = db.conn().unwrap();
        let scope = AccessScope::for_tenant(tenant_id);

        // Insert first summary
        let rows = crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo,
            &conn,
            chat_id,
            tenant_id,
            None,
            &frontier,
            "test summary",
            42,
        )
        .await
        .unwrap();
        assert_eq!(rows, 1);

        // Read it back
        let summary = crate::domain::repos::ThreadSummaryRepository::get_latest(
            &repo, &conn, &scope, chat_id,
        )
        .await
        .unwrap()
        .expect("summary should exist");
        assert_eq!(summary.content, "test summary");
        assert_eq!(summary.frontier, frontier);
        assert_eq!(summary.token_estimate, 42);
    }

    #[tokio::test]
    async fn e2e_repo_upsert_cas_conflict_on_first_summary() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let now = time::OffsetDateTime::now_utc();
        let frontier = SummaryFrontier {
            created_at: now,
            message_id: uuid::Uuid::new_v4(),
        };

        let repo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;
        let conn = db.conn().unwrap();

        // First insert succeeds
        let rows = crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo, &conn, chat_id, tenant_id, None, &frontier, "first", 10,
        )
        .await
        .unwrap();
        assert_eq!(rows, 1);

        // Second insert with base=None conflicts (UNIQUE on chat_id)
        let rows = crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo, &conn, chat_id, tenant_id, None, &frontier, "second", 20,
        )
        .await
        .unwrap();
        assert_eq!(rows, 0, "CAS should fail - row already exists");
    }

    #[tokio::test]
    async fn e2e_repo_advance_frontier_succeeds() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let now = time::OffsetDateTime::now_utc();
        let frontier_a = SummaryFrontier {
            created_at: now,
            message_id: uuid::Uuid::new_v4(),
        };
        let frontier_b = SummaryFrontier {
            created_at: now + time::Duration::seconds(10),
            message_id: uuid::Uuid::new_v4(),
        };

        let repo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;
        let conn = db.conn().unwrap();
        let scope = AccessScope::for_tenant(tenant_id);

        // Insert first
        crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo,
            &conn,
            chat_id,
            tenant_id,
            None,
            &frontier_a,
            "first",
            10,
        )
        .await
        .unwrap();

        // Advance frontier
        let rows = crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo,
            &conn,
            chat_id,
            tenant_id,
            Some(&frontier_a),
            &frontier_b,
            "second",
            20,
        )
        .await
        .unwrap();
        assert_eq!(rows, 1, "CAS should succeed - frontier matches");

        // Verify
        let summary = crate::domain::repos::ThreadSummaryRepository::get_latest(
            &repo, &conn, &scope, chat_id,
        )
        .await
        .unwrap()
        .unwrap();
        assert_eq!(summary.content, "second");
        assert_eq!(summary.frontier, frontier_b);
    }

    #[tokio::test]
    async fn e2e_repo_advance_frontier_cas_conflict() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let now = time::OffsetDateTime::now_utc();
        let frontier_a = SummaryFrontier {
            created_at: now,
            message_id: uuid::Uuid::new_v4(),
        };
        let frontier_b = SummaryFrontier {
            created_at: now + time::Duration::seconds(10),
            message_id: uuid::Uuid::new_v4(),
        };
        let stale_frontier = SummaryFrontier {
            created_at: now - time::Duration::seconds(10),
            message_id: uuid::Uuid::new_v4(),
        };

        let repo = crate::infra::db::repo::thread_summary_repo::ThreadSummaryRepository;
        let conn = db.conn().unwrap();

        // Insert first
        crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo,
            &conn,
            chat_id,
            tenant_id,
            None,
            &frontier_a,
            "first",
            10,
        )
        .await
        .unwrap();

        // Try to advance with stale base frontier
        let rows = crate::domain::repos::ThreadSummaryRepository::upsert_with_cas(
            &repo,
            &conn,
            chat_id,
            tenant_id,
            Some(&stale_frontier),
            &frontier_b,
            "stale",
            30,
        )
        .await
        .unwrap();
        assert_eq!(rows, 0, "CAS should fail - frontier doesn't match");
    }

    #[tokio::test]
    async fn e2e_mark_messages_compressed() {
        let db = crate::domain::service::test_helpers::inmem_db().await;
        let tenant_id = uuid::Uuid::new_v4();
        let chat_id = uuid::Uuid::new_v4();

        insert_chat(&db, tenant_id, chat_id).await;

        let base_time = time::OffsetDateTime::now_utc();
        let _msg1 = insert_message(
            &db,
            tenant_id,
            chat_id,
            MessageRole::User,
            "msg1",
            base_time,
        )
        .await;
        let msg2 = insert_message(
            &db,
            tenant_id,
            chat_id,
            MessageRole::Assistant,
            "msg2",
            base_time + time::Duration::seconds(1),
        )
        .await;
        let msg3 = insert_message(
            &db,
            tenant_id,
            chat_id,
            MessageRole::User,
            "msg3",
            base_time + time::Duration::seconds(2),
        )
        .await;

        let target = SummaryFrontier {
            created_at: base_time + time::Duration::seconds(1),
            message_id: msg2,
        };

        let repo = crate::infra::db::repo::message_repo::MessageRepository::new(
            modkit_db::odata::LimitCfg {
                default: 20,
                max: 100,
            },
        );
        let conn = db.conn().unwrap();
        let scope = AccessScope::for_tenant(tenant_id);

        // Mark messages up to msg2 as compressed
        let rows = crate::domain::repos::MessageRepository::mark_messages_compressed(
            &repo, &conn, &scope, chat_id, None, &target,
        )
        .await
        .unwrap();
        assert_eq!(rows, 2, "msg1 and msg2 should be compressed");

        // Verify msg3 is NOT compressed
        let remaining = crate::domain::repos::MessageRepository::fetch_messages_in_range(
            &repo,
            &conn,
            &scope,
            chat_id,
            Some(&target),
            &SummaryFrontier {
                created_at: base_time + time::Duration::seconds(10),
                message_id: uuid::Uuid::max(),
            },
        )
        .await
        .unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].id, msg3);
    }

    #[test]
    fn placeholder_summary_appends_to_existing() {
        use crate::infra::db::entity::message::Model;
        use time::OffsetDateTime;
        use uuid::Uuid;

        let msg = Model {
            id: Uuid::new_v4(),
            tenant_id: Uuid::new_v4(),
            chat_id: Uuid::new_v4(),
            request_id: Some(Uuid::new_v4()),
            role: MessageRole::Assistant,
            content: "I can help with that.".to_owned(),
            content_type: "text/plain".to_owned(),
            token_estimate: 10,
            provider_response_id: None,
            request_kind: None,
            features_used: serde_json::json!([]),
            input_tokens: 0,
            output_tokens: 0,
            cache_read_input_tokens: 0,
            cache_write_input_tokens: 0,
            reasoning_tokens: 0,
            model: None,
            is_compressed: false,
            created_at: OffsetDateTime::now_utc(),
            deleted_at: None,
        };

        let summary = build_summary_prompt(Some("Previous summary"), &[msg], 4000);
        assert!(summary.contains("Previous summary"));
        assert!(summary.contains("existing_summary"));
        assert!(summary.contains("Assistant: I can help with that."));
    }

    // ── format_summary_output tests ─────────────────────────────────

    #[test]
    fn format_summary_output_strips_analysis_extracts_summary() {
        let raw = "<analysis>\nThinking about the conversation...\n</analysis>\n\n<summary>\n1. Purpose: Testing\n2. Key Info: Works\n</summary>";
        let result = format_summary_output(raw);
        assert!(!result.contains("<analysis>"));
        assert!(!result.contains("Thinking about"));
        assert!(result.contains("1. Purpose: Testing"));
        assert!(result.contains("2. Key Info: Works"));
        assert!(!result.contains("<summary>"));
    }

    #[test]
    fn format_summary_output_fallback_when_no_tags() {
        let raw = "Just a plain text summary without XML tags.";
        let result = format_summary_output(raw);
        assert_eq!(result, "Just a plain text summary without XML tags.");
    }

    #[test]
    fn format_summary_output_handles_empty_analysis() {
        let raw = "<analysis></analysis>\n<summary>Clean result</summary>";
        let result = format_summary_output(raw);
        assert_eq!(result, "Clean result");
    }

    #[test]
    fn format_summary_output_collapses_excessive_newlines() {
        let raw = "<summary>Line 1\n\n\n\n\nLine 2</summary>";
        let result = format_summary_output(raw);
        assert_eq!(result, "Line 1\n\nLine 2");
    }

    // ── build_summary_prompt tests ──────────────────────────────────

    #[test]
    fn build_summary_prompt_full_compact_no_existing() {
        let msg = test_message(MessageRole::User, "What is Rust?");
        let prompt = build_summary_prompt(None, &[msg], 4000);
        assert!(prompt.starts_with("Summarize the following conversation:"));
        assert!(prompt.contains("User: What is Rust?"));
        assert!(prompt.contains("<analysis>"));
        assert!(prompt.contains("<summary>"));
        assert!(!prompt.contains("existing_summary"));
    }

    #[test]
    fn build_summary_prompt_partial_compact_with_existing() {
        let msg = test_message(MessageRole::Assistant, "Rust is a systems language.");
        let prompt = build_summary_prompt(Some("Prior context here"), &[msg], 4000);
        assert!(prompt.contains("existing_summary"));
        assert!(prompt.contains("Prior context here"));
        assert!(prompt.contains("New messages to incorporate:"));
        assert!(prompt.contains("Assistant: Rust is a systems language."));
    }

    #[test]
    fn build_summary_prompt_respects_content_limit() {
        let long_content = "x".repeat(200);
        let msg = test_message(MessageRole::User, &long_content);
        let prompt = build_summary_prompt(None, &[msg], 50);
        // Should be truncated to 50 chars + "..."
        assert!(prompt.contains(&"x".repeat(50)));
        assert!(prompt.contains("..."));
        assert!(!prompt.contains(&"x".repeat(200)));
    }

    #[test]
    fn build_summary_prompt_no_truncation_when_limit_zero() {
        let long_content = "y".repeat(10000);
        let msg = test_message(MessageRole::User, &long_content);
        let prompt = build_summary_prompt(None, &[msg], 0);
        assert!(prompt.contains(&long_content));
    }

    // ── is_context_length_error tests ───────────────────────────────

    #[test]
    fn is_context_length_error_matches_known_patterns() {
        use crate::infra::llm::LlmProviderError;

        let e1 = LlmProviderError::ProviderError {
            code: "context_length_exceeded".into(),
            message: "too many tokens".into(),
            raw_detail: None,
        };
        assert!(is_context_length_error(&e1));

        let e2 = LlmProviderError::ProviderError {
            code: "invalid_request".into(),
            message: "maximum context length exceeded".into(),
            raw_detail: None,
        };
        assert!(is_context_length_error(&e2));

        let e3 = LlmProviderError::ProviderError {
            code: "rate_limit".into(),
            message: "rate limited".into(),
            raw_detail: None,
        };
        assert!(!is_context_length_error(&e3));
    }

    fn test_message(role: MessageRole, content: &str) -> crate::infra::db::entity::message::Model {
        crate::infra::db::entity::message::Model {
            id: uuid::Uuid::new_v4(),
            tenant_id: uuid::Uuid::new_v4(),
            chat_id: uuid::Uuid::new_v4(),
            request_id: Some(uuid::Uuid::new_v4()),
            role,
            content: content.to_owned(),
            content_type: "text/plain".to_owned(),
            token_estimate: 0,
            provider_response_id: None,
            request_kind: None,
            features_used: serde_json::json!([]),
            input_tokens: 0,
            output_tokens: 0,
            cache_read_input_tokens: 0,
            cache_write_input_tokens: 0,
            reasoning_tokens: 0,
            model: None,
            is_compressed: false,
            created_at: time::OffsetDateTime::now_utc(),
            deleted_at: None,
        }
    }
}