Coverage for /home/admin/Documents/AI/applications/lexigram-dev/lexigram/experimental/ai/lexigram-ai-llm/src/lexigram/ai/llm/conversation/types.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 07:19 +0800

1"""Types and models for conversation management.""" 

2 

3from __future__ import annotations 

4 

5from dataclasses import dataclass 

6from datetime import UTC, datetime 

7 

8from lexigram.domain import DomainModel 

9from lexigram.validation import Field 

10 

11__all__ = ["ConversationConfig", "ConversationStats"] 

12 

13 

14@dataclass(init=False) 

15class ConversationConfig(DomainModel): 

16 """Configuration for conversation management. 

17 

18 Example: 

19 >>> config = ConversationConfig( 

20 ... max_tokens=4096, 

21 ... reserve_tokens=1000, 

22 ... trim_strategy="oldest" 

23 ... ) 

24 """ 

25 

26 max_tokens: int = Field( 

27 default=4096, 

28 description="Maximum context window size in tokens", 

29 ) 

30 reserve_tokens: int = Field( 

31 default=1000, 

32 description="Tokens to reserve for completion (subtracted from max_tokens)", 

33 ) 

34 trim_strategy: str = Field( 

35 default="oldest", 

36 description="Strategy for trimming messages: 'oldest', 'middle', 'summary'", 

37 ) 

38 keep_system: bool = Field( 

39 default=True, 

40 description="Always keep system message when trimming", 

41 ) 

42 min_messages: int = Field( 

43 default=2, 

44 description="Minimum messages to keep (excluding system)", 

45 ) 

46 

47 

48@dataclass(init=False) 

49class ConversationStats(DomainModel): 

50 """Statistics for a conversation. 

51 

52 Example: 

53 >>> stats = ConversationStats( 

54 ... total_messages=10, 

55 ... total_tokens=2048, 

56 ... user_messages=5, 

57 ... assistant_messages=5 

58 ... ) 

59 """ 

60 

61 total_messages: int = Field(default=0, description="Total messages in conversation") 

62 total_tokens: int = Field(default=0, description="Total tokens used") 

63 user_messages: int = Field(default=0, description="Number of user messages") 

64 assistant_messages: int = Field( 

65 default=0, 

66 description="Number of assistant messages", 

67 ) 

68 system_messages: int = Field(default=0, description="Number of system messages") 

69 trimmed_count: int = Field( 

70 default=0, 

71 description="Number of times messages were trimmed", 

72 ) 

73 created_at: datetime = Field( 

74 default_factory=lambda: datetime.now(UTC), 

75 description="Conversation creation time", 

76 ) 

77 last_updated: datetime = Field( 

78 default_factory=lambda: datetime.now(UTC), 

79 description="Last update time", 

80 )