mofa-foundation 0.1.1

MoFA Foundation - Core building blocks and utilities
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
//! LLM Client - 高级 LLM 交互封装
//!
//! 提供便捷的 LLM 交互 API,包括消息管理、工具调用循环等

use super::provider::{LLMConfig, LLMProvider};
use super::tool_executor::ToolExecutor;
use super::types::*;
use std::sync::Arc;

/// LLM 客户端
///
/// 提供高级 LLM 交互功能
///
/// # 示例
///
/// ```rust,ignore
/// use mofa_foundation::llm::{LLMClient, LLMConfig, ChatMessage};
///
/// // 创建客户端
/// let client = LLMClient::new(provider);
///
/// // 简单对话
/// let response = client
///     .chat()
///     .system("You are a helpful assistant.")
///     .user("Hello!")
///     .send()
///     .await?;
///
/// info!("{}", response.content().unwrap_or_default());
/// ```
pub struct LLMClient {
    provider: Arc<dyn LLMProvider>,
    config: LLMConfig,
}

impl LLMClient {
    /// 使用 Provider 创建客户端
    pub fn new(provider: Arc<dyn LLMProvider>) -> Self {
        Self {
            provider,
            config: LLMConfig::default(),
        }
    }

    /// 使用配置创建客户端
    pub fn with_config(provider: Arc<dyn LLMProvider>, config: LLMConfig) -> Self {
        Self { provider, config }
    }

    /// 获取 Provider
    pub fn provider(&self) -> &Arc<dyn LLMProvider> {
        &self.provider
    }

    /// 获取配置
    pub fn config(&self) -> &LLMConfig {
        &self.config
    }

    /// 创建 Chat 请求构建器
    pub fn chat(&self) -> ChatRequestBuilder {
        let model = self
            .config
            .default_model
            .clone()
            .unwrap_or_else(|| self.provider.default_model().to_string());

        let mut builder = ChatRequestBuilder::new(self.provider.clone(), model);

        if let Some(temp) = self.config.default_temperature {
            builder = builder.temperature(temp);
        }
        if let Some(tokens) = self.config.default_max_tokens {
            builder = builder.max_tokens(tokens);
        }

        builder
    }

    /// 创建 Embedding 请求
    pub async fn embed(&self, input: impl Into<String>) -> LLMResult<Vec<f32>> {
        let model = self
            .config
            .default_model
            .clone()
            .unwrap_or_else(|| "text-embedding-ada-002".to_string());

        let request = EmbeddingRequest {
            model,
            input: EmbeddingInput::Single(input.into()),
            encoding_format: None,
            dimensions: None,
            user: None,
        };

        let response = self.provider.embedding(request).await?;
        response
            .data
            .into_iter()
            .next()
            .map(|d| d.embedding)
            .ok_or_else(|| LLMError::Other("No embedding data returned".to_string()))
    }

    /// 批量 Embedding
    pub async fn embed_batch(&self, inputs: Vec<String>) -> LLMResult<Vec<Vec<f32>>> {
        let model = self
            .config
            .default_model
            .clone()
            .unwrap_or_else(|| "text-embedding-ada-002".to_string());

        let request = EmbeddingRequest {
            model,
            input: EmbeddingInput::Multiple(inputs),
            encoding_format: None,
            dimensions: None,
            user: None,
        };

        let response = self.provider.embedding(request).await?;
        Ok(response.data.into_iter().map(|d| d.embedding).collect())
    }

    /// 简单对话(单次问答)
    pub async fn ask(&self, question: impl Into<String>) -> LLMResult<String> {
        let response = self.chat().user(question).send().await?;

        response
            .content()
            .map(|s| s.to_string())
            .ok_or_else(|| LLMError::Other("No content in response".to_string()))
    }

    /// 带系统提示的简单对话
    pub async fn ask_with_system(
        &self,
        system: impl Into<String>,
        question: impl Into<String>,
    ) -> LLMResult<String> {
        let response = self.chat().system(system).user(question).send().await?;

        response
            .content()
            .map(|s| s.to_string())
            .ok_or_else(|| LLMError::Other("No content in response".to_string()))
    }
}

/// Chat 请求构建器
pub struct ChatRequestBuilder {
    provider: Arc<dyn LLMProvider>,
    request: ChatCompletionRequest,
    tool_executor: Option<Arc<dyn ToolExecutor>>,
    max_tool_rounds: u32,
    // Retry configuration
    retry_policy: Option<LLMRetryPolicy>,
    retry_enabled: bool,
}

impl ChatRequestBuilder {
    /// 创建新的构建器
    pub fn new(provider: Arc<dyn LLMProvider>, model: impl Into<String>) -> Self {
        Self {
            provider,
            request: ChatCompletionRequest::new(model),
            tool_executor: None,
            max_tool_rounds: 10,
            retry_policy: None,
            retry_enabled: false,
        }
    }

    /// 添加系统消息
    pub fn system(mut self, content: impl Into<String>) -> Self {
        self.request.messages.push(ChatMessage::system(content));
        self
    }

    /// 添加用户消息
    pub fn user(mut self, content: impl Into<String>) -> Self {
        self.request.messages.push(ChatMessage::user(content));
        self
    }

    /// 添加用户消息(结构化内容)
    pub fn user_with_content(mut self, content: MessageContent) -> Self {
        self.request
            .messages
            .push(ChatMessage::user_with_content(content));
        self
    }

    /// 添加用户消息(多部分内容)
    pub fn user_with_parts(mut self, parts: Vec<ContentPart>) -> Self {
        self.request
            .messages
            .push(ChatMessage::user_with_parts(parts));
        self
    }

    /// 添加助手消息
    pub fn assistant(mut self, content: impl Into<String>) -> Self {
        self.request.messages.push(ChatMessage::assistant(content));
        self
    }

    /// 添加消息
    pub fn message(mut self, message: ChatMessage) -> Self {
        self.request.messages.push(message);
        self
    }

    /// 添加消息列表
    pub fn messages(mut self, messages: Vec<ChatMessage>) -> Self {
        self.request.messages.extend(messages);
        self
    }

    /// 设置温度
    pub fn temperature(mut self, temp: f32) -> Self {
        self.request.temperature = Some(temp);
        self
    }

    /// 设置最大 token 数
    pub fn max_tokens(mut self, tokens: u32) -> Self {
        self.request.max_tokens = Some(tokens);
        self
    }

    /// 添加工具
    pub fn tool(mut self, tool: Tool) -> Self {
        self.request.tools.get_or_insert_with(Vec::new).push(tool);
        self
    }

    /// 设置工具列表
    pub fn tools(mut self, tools: Vec<Tool>) -> Self {
        self.request.tools = Some(tools);
        self
    }

    /// 设置工具执行器
    pub fn with_tool_executor(mut self, executor: Arc<dyn ToolExecutor>) -> Self {
        self.tool_executor = Some(executor);
        self
    }

    /// 设置最大工具调用轮数
    pub fn max_tool_rounds(mut self, rounds: u32) -> Self {
        self.max_tool_rounds = rounds;
        self
    }

    /// 设置响应格式为 JSON
    pub fn json_mode(mut self) -> Self {
        self.request.response_format = Some(ResponseFormat::json());
        self
    }

    /// 设置停止序列
    pub fn stop(mut self, sequences: Vec<String>) -> Self {
        self.request.stop = Some(sequences);
        self
    }

    // ========================================================================
    // Retry Configuration
    // ========================================================================

    /// Enable retry with default policy
    ///
    /// Uses PromptRetry strategy for serialization errors (best for JSON mode)
    /// and DirectRetry for network/transient errors.
    ///
    /// # Example
    /// ```rust,ignore
    /// let response = client.chat()
    ///     .json_mode()
    ///     .with_retry()
    ///     .send()
    ///     .await?;
    /// ```
    pub fn with_retry(mut self) -> Self {
        self.retry_enabled = true;
        self.retry_policy = Some(LLMRetryPolicy::default());
        self
    }

    /// Enable retry with custom policy
    ///
    /// # Example
    /// ```rust,ignore
    /// use mofa_foundation::llm::{LLMRetryPolicy, BackoffStrategy};
    ///
    /// let custom_policy = LLMRetryPolicy {
    ///     max_attempts: 5,
    ///     backoff: BackoffStrategy::ExponentialWithJitter {
    ///         initial_delay_ms: 500,
    ///         max_delay_ms: 60000,
    ///         jitter_ms: 250,
    ///     },
    ///     ..Default::default()
    /// };
    ///
    /// let response = client.chat()
    ///     .with_retry_policy(custom_policy)
    ///     .send()
    ///     .await?;
    /// ```
    pub fn with_retry_policy(mut self, policy: LLMRetryPolicy) -> Self {
        self.retry_enabled = true;
        self.retry_policy = Some(policy);
        self
    }

    /// Disable retry (explicit)
    ///
    /// This is the default behavior, but can be used to override
    /// any previously set retry configuration.
    pub fn without_retry(mut self) -> Self {
        self.retry_enabled = false;
        self.retry_policy = None;
        self
    }

    /// Set max retry attempts (convenience method)
    ///
    /// Shortcut for setting max_attempts in the default retry policy.
    /// Equivalent to `.with_retry_policy(LLMRetryPolicy::with_max_attempts(n))`
    ///
    /// # Example
    /// ```rust,ignore
    /// let response = client.chat()
    ///     .json_mode()
    ///     .max_retries(3)
    ///     .send()
    ///     .await?;
    /// ```
    pub fn max_retries(mut self, max: u32) -> Self {
        if self.retry_policy.is_none() {
            self.retry_policy = Some(LLMRetryPolicy::default());
        }
        if let Some(ref mut policy) = self.retry_policy {
            policy.max_attempts = max;
        }
        self.retry_enabled = true;
        self
    }

    /// 发送请求
    pub async fn send(self) -> LLMResult<ChatCompletionResponse> {
        if self.retry_enabled {
            let policy = self.retry_policy.unwrap_or_default();
            let executor = crate::llm::retry::RetryExecutor::new(self.provider, policy);
            executor.chat(self.request).await
        } else {
            self.provider.chat(self.request).await
        }
    }

    /// 发送流式请求
    pub async fn send_stream(mut self) -> LLMResult<super::provider::ChatStream> {
        self.request.stream = Some(true);
        self.provider.chat_stream(self.request).await
    }

    /// 发送请求并自动执行工具调用
    ///
    /// 当 LLM 返回工具调用时,自动执行工具并继续对话,
    /// 直到 LLM 返回最终响应或达到最大轮数
    pub async fn send_with_tools(mut self) -> LLMResult<ChatCompletionResponse> {
        let executor = self
            .tool_executor
            .take()
            .ok_or_else(|| LLMError::ConfigError("Tool executor not set".to_string()))?;

        if self
            .request
            .tools
            .as_ref()
            .map(|tools| tools.is_empty())
            .unwrap_or(true)
        {
            let tools = executor.available_tools().await?;
            if !tools.is_empty() {
                self.request.tools = Some(tools);
            }
        }

        let max_rounds = self.max_tool_rounds;
        let mut round = 0;

        loop {
            let response = self.provider.chat(self.request.clone()).await?;

            // 检查是否有工具调用
            if !response.has_tool_calls() {
                return Ok(response);
            }

            round += 1;
            if round >= max_rounds {
                return Err(LLMError::Other(format!(
                    "Max tool rounds ({}) exceeded",
                    max_rounds
                )));
            }

            // 添加助手消息(包含工具调用)
            if let Some(choice) = response.choices.first() {
                self.request.messages.push(choice.message.clone());
            }

            // 执行工具调用
            if let Some(tool_calls) = response.tool_calls() {
                for tool_call in tool_calls {
                    let result = executor
                        .execute(&tool_call.function.name, &tool_call.function.arguments)
                        .await;

                    let result_str = match result {
                        Ok(r) => r,
                        Err(e) => format!("Error: {}", e),
                    };

                    // 添加工具结果消息
                    self.request
                        .messages
                        .push(ChatMessage::tool_result(&tool_call.id, result_str));
                }
            }
        }
    }
}

// ============================================================================
// 会话管理
// ============================================================================

/// 对话会话
///
/// 管理多轮对话的消息历史
pub struct ChatSession {
    /// 会话唯一标识
    session_id: uuid::Uuid,
    /// 用户 ID
    user_id: uuid::Uuid,
    /// Agent ID
    agent_id: uuid::Uuid,
    /// 租户 ID
    tenant_id: uuid::Uuid,
    /// LLM 客户端
    client: LLMClient,
    /// 消息历史
    messages: Vec<ChatMessage>,
    /// 系统提示词
    system_prompt: Option<String>,
    /// 工具列表
    tools: Vec<Tool>,
    /// 工具执行器
    tool_executor: Option<Arc<dyn ToolExecutor>>,
    /// 会话创建时间
    created_at: std::time::Instant,
    /// 会话元数据
    metadata: std::collections::HashMap<String, String>,
    /// 消息存储
    message_store: Arc<dyn crate::persistence::MessageStore>,
    /// 会话存储
    session_store: Arc<dyn crate::persistence::SessionStore>,
    /// 上下文窗口大小(滑动窗口,限制对话轮数)
    context_window_size: Option<usize>,
    /// 最后一次 LLM 响应的元数据
    last_response_metadata: Option<super::types::LLMResponseMetadata>,
}

impl ChatSession {
    /// 创建新会话(自动生成 ID)
    pub fn new(client: LLMClient) -> Self {
        // 默认使用内存存储
        let store = Arc::new(crate::persistence::InMemoryStore::new());
        Self::with_id_and_stores(
            Self::generate_session_id(),
            client,
            uuid::Uuid::now_v7(),
            uuid::Uuid::now_v7(),
            uuid::Uuid::now_v7(),
            store.clone(),
            store.clone(),
            None,
        )
    }

    /// 创建新会话并指定存储实现
    pub fn new_with_stores(
        client: LLMClient,
        user_id: uuid::Uuid,
        tenant_id: uuid::Uuid,
        agent_id: uuid::Uuid,
        message_store: Arc<dyn crate::persistence::MessageStore>,
        session_store: Arc<dyn crate::persistence::SessionStore>,
    ) -> Self {
        Self::with_id_and_stores(
            Self::generate_session_id(),
            client,
            user_id,
            tenant_id,
            agent_id,
            message_store,
            session_store,
            None,
        )
    }

    /// 使用指定 UUID 创建会话
    pub fn with_id(session_id: uuid::Uuid, client: LLMClient) -> Self {
        // 默认使用内存存储
        let store = Arc::new(crate::persistence::InMemoryStore::new());
        Self {
            session_id,
            user_id: uuid::Uuid::now_v7(),
            agent_id: uuid::Uuid::now_v7(),
            tenant_id: uuid::Uuid::now_v7(),
            client,
            messages: Vec::new(),
            system_prompt: None,
            tools: Vec::new(),
            tool_executor: None,
            created_at: std::time::Instant::now(),
            metadata: std::collections::HashMap::new(),
            message_store: store.clone(),
            session_store: store.clone(),
            context_window_size: None,
            last_response_metadata: None,
        }
    }

    /// 使用指定字符串 ID 创建会话
    pub fn with_id_str(session_id: &str, client: LLMClient) -> Self {
        // 尝试将字符串解析为 UUID,如果失败则生成新的 UUID
        let session_id = uuid::Uuid::parse_str(session_id).unwrap_or_else(|_| uuid::Uuid::now_v7());
        Self::with_id(session_id, client)
    }

    /// 使用指定 ID 和存储实现创建会话
    pub fn with_id_and_stores(
        session_id: uuid::Uuid,
        client: LLMClient,
        user_id: uuid::Uuid,
        tenant_id: uuid::Uuid,
        agent_id: uuid::Uuid,
        message_store: Arc<dyn crate::persistence::MessageStore>,
        session_store: Arc<dyn crate::persistence::SessionStore>,
        context_window_size: Option<usize>,
    ) -> Self {
        Self {
            session_id,
            user_id,
            tenant_id,
            agent_id,
            client,
            messages: Vec::new(),
            system_prompt: None,
            tools: Vec::new(),
            tool_executor: None,
            created_at: std::time::Instant::now(),
            metadata: std::collections::HashMap::new(),
            message_store,
            session_store,
            context_window_size,
            last_response_metadata: None,
        }
    }

    /// 使用指定 ID 和存储实现创建会话,并立即持久化到数据库
    ///
    /// 这个方法会将会话记录保存到数据库,确保会话在创建时就被持久化。
    /// 这对于需要将会话 ID 用作外键的场景很重要(例如保存消息时)。
    ///
    /// # 参数
    /// - `session_id`: 会话 ID
    /// - `client`: LLM 客户端
    /// - `user_id`: 用户 ID
    /// - `agent_id`: Agent ID
    /// - `message_store`: 消息存储
    /// - `session_store`: 会话存储
    /// - `context_window_size`: 可选的上下文窗口大小
    ///
    /// # 返回
    /// 返回创建并持久化后的会话
    ///
    /// # 错误
    /// 如果数据库操作失败,返回错误
    pub async fn with_id_and_stores_and_persist(
        session_id: uuid::Uuid,
        client: LLMClient,
        user_id: uuid::Uuid,
        tenant_id: uuid::Uuid,
        agent_id: uuid::Uuid,
        message_store: Arc<dyn crate::persistence::MessageStore>,
        session_store: Arc<dyn crate::persistence::SessionStore>,
        context_window_size: Option<usize>,
    ) -> crate::persistence::PersistenceResult<Self> {
        // 创建内存会话
        let session = Self::with_id_and_stores(
            session_id,
            client,
            user_id,
            tenant_id,
            agent_id,
            message_store,
            session_store.clone(),
            context_window_size,
        );

        // 持久化会话记录到数据库
        let db_session =
            crate::persistence::ChatSession::new(user_id, agent_id).with_id(session_id);
        session_store.create_session(&db_session).await?;

        Ok(session)
    }

    /// 生成唯一会话 ID
    fn generate_session_id() -> uuid::Uuid {
        uuid::Uuid::now_v7()
    }

    /// 获取会话 ID
    pub fn session_id(&self) -> uuid::Uuid {
        self.session_id
    }

    /// 获取会话 ID 字符串
    pub fn session_id_str(&self) -> String {
        self.session_id.to_string()
    }

    /// 获取会话创建时间
    pub fn created_at(&self) -> std::time::Instant {
        self.created_at
    }

    /// 从数据库加载会话
    ///
    /// 创建一个新的 ChatSession 实例,加载指定 ID 的会话和消息。
    ///
    /// # 参数
    /// - `session_id`: 会话 ID
    /// - `client`: LLM 客户端
    /// - `user_id`: 用户 ID
    /// - `agent_id`: Agent ID
    /// - `message_store`: 消息存储
    /// - `session_store`: 会话存储
    /// - `context_window_size`: 可选的上下文窗口大小(轮数),如果指定则只加载最近的 N 轮对话
    ///
    /// # 注意
    /// 当指定 `context_window_size` 时,只会加载最近的 N 轮对话到内存中。
    /// 这对于长期对话很有用,可以避免加载大量历史消息。
    pub async fn load(
        session_id: uuid::Uuid,
        client: LLMClient,
        user_id: uuid::Uuid,
        tenant_id: uuid::Uuid,
        agent_id: uuid::Uuid,
        message_store: Arc<dyn crate::persistence::MessageStore>,
        session_store: Arc<dyn crate::persistence::SessionStore>,
        context_window_size: Option<usize>,
    ) -> crate::persistence::PersistenceResult<Self> {
        // Load session from database
        let _db_session = session_store
            .get_session(session_id)
            .await?
            .ok_or_else(|| {
                crate::persistence::PersistenceError::NotFound("Session not found".to_string())
            })?;

        // Load messages from database
        // Use pagination when context_window_size is set to avoid loading all messages
        let db_messages = if context_window_size.is_some() {
            // Use pagination to avoid loading all messages for long-running sessions
            let total_count = message_store.count_session_messages(session_id).await?;

            // Calculate fetch limit: rounds * 2 (user+assistant per round) + buffer for system messages
            let rounds = context_window_size.unwrap_or(0);
            let limit = (rounds * 2 + 20) as i64; // 20 message buffer for system messages at beginning

            // Calculate offset to get the most recent messages
            let offset = std::cmp::max(0, total_count - limit);

            message_store
                .get_session_messages_paginated(session_id, offset, limit)
                .await?
        } else {
            // No window size specified, fetch all messages (current behavior)
            message_store.get_session_messages(session_id).await?
        };

        // Convert messages to domain format
        let mut messages = Vec::new();
        for db_msg in db_messages {
            // Convert MessageRole to Role
            let domain_role = match db_msg.role {
                crate::persistence::MessageRole::System => crate::llm::types::Role::System,
                crate::persistence::MessageRole::User => crate::llm::types::Role::User,
                crate::persistence::MessageRole::Assistant => crate::llm::types::Role::Assistant,
                crate::persistence::MessageRole::Tool => crate::llm::types::Role::Tool,
            };

            // Convert MessageContent to domain format
            let domain_content = db_msg
                .content
                .text
                .map(crate::llm::types::MessageContent::Text);

            // Create domain message
            let domain_msg = ChatMessage {
                role: domain_role,
                content: domain_content,
                name: None,
                tool_calls: None,
                tool_call_id: None,
            };
            messages.push(domain_msg);
        }

        // 应用滑动窗口逻辑(如果指定了 context_window_size)
        let messages = Self::apply_sliding_window_static(&messages, context_window_size);

        // Create and return ChatSession
        Ok(Self {
            session_id,
            user_id,
            tenant_id,
            agent_id,
            client,
            messages,
            system_prompt: None, // System prompt is not stored in messages
            tools: Vec::new(),   // Tools are not persisted yet
            tool_executor: None, // Tool executor is not persisted
            created_at: std::time::Instant::now(), // TODO: Convert from db_session.create_time
            metadata: std::collections::HashMap::new(), // TODO: Convert from db_session.metadata
            message_store,
            session_store,
            context_window_size,
            last_response_metadata: None,
        })
    }

    /// 获取会话存活时长
    pub fn elapsed(&self) -> std::time::Duration {
        self.created_at.elapsed()
    }

    /// 设置元数据
    pub fn set_metadata(&mut self, key: impl Into<String>, value: impl Into<String>) {
        self.metadata.insert(key.into(), value.into());
    }

    /// 获取元数据
    pub fn get_metadata(&self, key: &str) -> Option<&String> {
        self.metadata.get(key)
    }

    /// 获取所有元数据
    pub fn metadata(&self) -> &std::collections::HashMap<String, String> {
        &self.metadata
    }

    /// 设置系统提示
    pub fn with_system(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }

    /// 设置上下文窗口大小(滑动窗口)
    ///
    /// # 参数
    /// - `size`: 保留的最大对话轮数(None 表示不限制)
    ///
    /// # 注意
    /// - 单位是**轮数**(rounds),不是 token 数量
    /// - 每轮对话 ≈ 1 个用户消息 + 1 个助手响应
    /// - 系统消息始终保留,不计入轮数限制
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let session = ChatSession::new(client)
    ///     .with_context_window_size(Some(10)); // 只保留最近 10 轮对话
    /// ```
    pub fn with_context_window_size(mut self, size: Option<usize>) -> Self {
        self.context_window_size = size;
        self
    }

    /// 设置工具
    pub fn with_tools(mut self, tools: Vec<Tool>, executor: Arc<dyn ToolExecutor>) -> Self {
        self.tools = tools;
        self.tool_executor = Some(executor);
        self
    }

    /// 仅设置工具执行器(工具列表自动发现)
    pub fn with_tool_executor(mut self, executor: Arc<dyn ToolExecutor>) -> Self {
        self.tool_executor = Some(executor);
        self
    }

    /// 发送消息
    pub async fn send(&mut self, content: impl Into<String>) -> LLMResult<String> {
        // 添加用户消息
        self.messages.push(ChatMessage::user(content));

        // 构建请求
        let mut builder = self.client.chat();

        // 添加系统提示
        if let Some(ref system) = self.system_prompt {
            builder = builder.system(system.clone());
        }

        // 添加历史消息(应用滑动窗口)
        let messages_for_context = self.apply_sliding_window();
        builder = builder.messages(messages_for_context);

        // 添加工具
        if let Some(ref executor) = self.tool_executor {
            let tools = if self.tools.is_empty() {
                executor.available_tools().await?
            } else {
                self.tools.clone()
            };

            if !tools.is_empty() {
                builder = builder.tools(tools);
            }

            builder = builder.with_tool_executor(executor.clone());
        }

        // 发送请求
        let response = if self.tool_executor.is_some() {
            builder.send_with_tools().await?
        } else {
            builder.send().await?
        };

        // 存储响应元数据
        self.last_response_metadata = Some(super::types::LLMResponseMetadata::from(&response));

        // 提取响应内容
        let content = response
            .content()
            .ok_or_else(|| LLMError::Other("No content in response".to_string()))?
            .to_string();

        // 添加助手消息到历史
        self.messages.push(ChatMessage::assistant(&content));

        // 滑动窗口:裁剪历史消息以保持固定大小
        if self.context_window_size.is_some() {
            self.messages =
                Self::apply_sliding_window_static(&self.messages, self.context_window_size);
        }

        Ok(content)
    }

    /// 发送结构化消息(支持多模态)
    pub async fn send_with_content(&mut self, content: MessageContent) -> LLMResult<String> {
        self.messages.push(ChatMessage::user_with_content(content));

        // 构建请求
        let mut builder = self.client.chat();

        // 添加系统提示
        if let Some(ref system) = self.system_prompt {
            builder = builder.system(system.clone());
        }

        // 添加历史消息(应用滑动窗口)
        let messages_for_context = self.apply_sliding_window();
        builder = builder.messages(messages_for_context);

        // 添加工具
        if let Some(ref executor) = self.tool_executor {
            let tools = if self.tools.is_empty() {
                executor.available_tools().await?
            } else {
                self.tools.clone()
            };

            if !tools.is_empty() {
                builder = builder.tools(tools);
            }

            builder = builder.with_tool_executor(executor.clone());
        }

        // 发送请求
        let response = if self.tool_executor.is_some() {
            builder.send_with_tools().await?
        } else {
            builder.send().await?
        };

        // 存储响应元数据
        self.last_response_metadata = Some(super::types::LLMResponseMetadata::from(&response));

        // 提取响应内容
        let content = response
            .content()
            .ok_or_else(|| LLMError::Other("No content in response".to_string()))?
            .to_string();

        // 添加助手消息到历史
        self.messages.push(ChatMessage::assistant(&content));

        // 滑动窗口:裁剪历史消息以保持固定大小
        if self.context_window_size.is_some() {
            self.messages =
                Self::apply_sliding_window_static(&self.messages, self.context_window_size);
        }

        Ok(content)
    }

    /// 获取消息历史
    pub fn messages(&self) -> &[ChatMessage] {
        &self.messages
    }

    /// 获取消息历史(可变引用)
    pub fn messages_mut(&mut self) -> &mut Vec<ChatMessage> {
        &mut self.messages
    }

    /// 清空消息历史
    pub fn clear(&mut self) {
        self.messages.clear();
    }

    /// 获取消息数量
    pub fn len(&self) -> usize {
        self.messages.len()
    }

    /// 是否为空
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }

    /// 设置上下文窗口大小(滑动窗口)
    ///
    /// # 参数
    /// - `size`: 保留的最大对话轮数(None 表示不限制)
    ///
    /// # 注意
    /// - 单位是**轮数**(rounds),不是 token 数量
    /// - 每轮对话 ≈ 1 个用户消息 + 1 个助手响应(可能包含工具调用)
    /// - 系统消息始终保留,不计入轮数限制
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// session.set_context_window_size(Some(10)); // 只保留最近 10 轮对话
    /// session.set_context_window_size(None);     // 不限制对话轮数
    /// ```
    pub fn set_context_window_size(&mut self, size: Option<usize>) {
        self.context_window_size = size;
    }

    /// 获取上下文窗口大小(轮数)
    pub fn context_window_size(&self) -> Option<usize> {
        self.context_window_size
    }

    /// 获取最后一次 LLM 响应的元数据
    pub fn last_response_metadata(&self) -> Option<&super::types::LLMResponseMetadata> {
        self.last_response_metadata.as_ref()
    }

    /// 应用滑动窗口,返回限定后的消息列表
    ///
    /// 保留最近的 N 轮对话(每轮包括用户消息和助手响应)。
    /// 系统消息始终保留。
    ///
    /// # 参数
    /// - `messages`: 要过滤的消息列表
    /// - `window_size`: 保留的最大对话轮数(None 表示不限制)
    ///
    /// # 算法说明
    /// - 1. 分离系统消息和对话消息
    /// - 2. 从对话消息中保留最近的 N 轮
    /// - 3. 合并系统消息和裁剪后的对话消息
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// // 假设有 20 条消息(约 10 轮对话)
    /// // 设置 window_size = Some(3)
    /// // 结果:保留最近 3 轮对话(约 6 条消息)+ 所有系统消息
    /// let limited = ChatSession::apply_sliding_window_static(messages, Some(3));
    /// ```
    fn apply_sliding_window(&self) -> Vec<ChatMessage> {
        Self::apply_sliding_window_static(&self.messages, self.context_window_size)
    }

    /// 静态方法:应用滑动窗口到消息列表
    ///
    /// 这个方法可以在不拥有 ChatSession 实例的情况下使用,
    /// 例如在从数据库加载消息时。
    ///
    /// # 参数
    /// - `messages`: 要过滤的消息列表
    /// - `window_size`: 保留的最大对话轮数(None 表示不限制)
    pub fn apply_sliding_window_static(
        messages: &[ChatMessage],
        window_size: Option<usize>,
    ) -> Vec<ChatMessage> {
        let max_rounds = match window_size {
            Some(size) if size > 0 => size,
            _ => return messages.to_vec(), // 无限制,返回所有消息
        };

        // 分离系统消息和对话消息
        let mut system_messages = Vec::new();
        let mut conversation_messages = Vec::new();

        for msg in messages {
            if msg.role == Role::System {
                system_messages.push(msg.clone());
            } else {
                conversation_messages.push(msg.clone());
            }
        }

        // 计算需要保留的最大消息数(每轮大约2条:用户+助手)
        let max_messages = max_rounds * 2;

        if conversation_messages.len() <= max_messages {
            // 对话消息数量在限制内,返回所有消息
            return messages.to_vec();
        }

        // 保留最后的 N 条对话消息
        let start_index = conversation_messages.len() - max_messages;
        let limited_conversation: Vec<ChatMessage> = conversation_messages
            .into_iter()
            .skip(start_index)
            .collect();

        // 合并系统消息和裁剪后的对话消息
        let mut result = system_messages;
        result.extend(limited_conversation);

        result
    }

    /// 保存会话和消息到数据库
    pub async fn save(&self) -> crate::persistence::PersistenceResult<()> {
        // Convert ChatSession to persistence entity
        let db_session = crate::persistence::ChatSession::new(self.user_id, self.agent_id)
            .with_id(self.session_id)
            .with_metadata("client_version", serde_json::json!("0.1.0"));

        // Save session
        self.session_store.create_session(&db_session).await?;

        // Convert and save messages
        for msg in self.messages.iter() {
            // Convert Role to MessageRole
            let persistence_role = match msg.role {
                crate::llm::types::Role::System => crate::persistence::MessageRole::System,
                crate::llm::types::Role::User => crate::persistence::MessageRole::User,
                crate::llm::types::Role::Assistant => crate::persistence::MessageRole::Assistant,
                crate::llm::types::Role::Tool => crate::persistence::MessageRole::Tool,
            };

            // Convert MessageContent to persistence format
            let persistence_content = match &msg.content {
                Some(crate::llm::types::MessageContent::Text(text)) => {
                    crate::persistence::MessageContent::text(text)
                }
                Some(crate::llm::types::MessageContent::Parts(parts)) => {
                    // For now, only handle text parts
                    let text = parts
                        .iter()
                        .filter_map(|part| {
                            if let crate::llm::types::ContentPart::Text { text } = part {
                                Some(text.clone())
                            } else {
                                None
                            }
                        })
                        .collect::<Vec<_>>()
                        .join("\n");
                    crate::persistence::MessageContent::text(text)
                }
                None => crate::persistence::MessageContent::text(""),
            };

            let llm_message = crate::persistence::LLMMessage::new(
                self.session_id,
                self.agent_id,
                self.user_id,
                self.tenant_id,
                persistence_role,
                persistence_content,
            );

            // Save message
            self.message_store.save_message(&llm_message).await?;
        }

        Ok(())
    }

    /// 从数据库删除会话和消息
    pub async fn delete(&self) -> crate::persistence::PersistenceResult<()> {
        // Delete all messages for the session
        self.message_store
            .delete_session_messages(self.session_id)
            .await?;

        // Delete the session itself
        self.session_store.delete_session(self.session_id).await?;

        Ok(())
    }
}

// ============================================================================
// 便捷函数
// ============================================================================

/// 快速创建函数工具定义
///
/// # 示例
///
/// ```rust,ignore
/// use mofa_foundation::llm::function_tool;
/// use serde_json::json;
///
/// let tool = function_tool(
///     "get_weather",
///     "Get the current weather for a location",
///     json!({
///         "type": "object",
///         "properties": {
///             "location": {
///                 "type": "string",
///                 "description": "City name"
///             }
///         },
///         "required": ["location"]
///     })
/// );
/// ```
pub fn function_tool(
    name: impl Into<String>,
    description: impl Into<String>,
    parameters: serde_json::Value,
) -> Tool {
    Tool::function(name, description, parameters)
}