chuk-ai-session-manager 0.4.1__py3-none-any.whl → 0.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,184 +29,86 @@ Infinite Context Example:
29
29
  await sm.ai_responds("Machine learning is...", model="gpt-4")
30
30
  # Session will auto-segment when limits are reached
31
31
 
32
- Advanced Usage:
33
- # Access the full Session model for detailed control
34
- from chuk_ai_session_manager import Session, SessionEvent
32
+ Storage Configuration:
33
+ # Default: Memory storage (no Redis required)
34
+ pip install chuk-ai-session-manager
35
35
 
36
- session = await Session.create()
37
- event = await SessionEvent.create_with_tokens(
38
- message="Hello", prompt="Hello", model="gpt-4"
39
- )
40
- await session.add_event_and_save(event)
36
+ # Redis: For production persistence
37
+ pip install chuk-ai-session-manager[redis]
38
+ export SESSION_PROVIDER=redis
39
+ export SESSION_REDIS_URL=redis://localhost:6379/0
40
+
41
+ # Environment variables:
42
+ SESSION_PROVIDER=memory (default - fast, no persistence)
43
+ SESSION_PROVIDER=redis (persistent - requires [redis] extra)
41
44
  """
42
45
 
43
46
  import logging
44
- from typing import Optional
45
47
 
46
48
  # Package version
47
- __version__ = "0.4"
49
+ __version__ = "0.5"
48
50
 
49
51
  # Set up package-level logger
50
52
  logger = logging.getLogger(__name__)
51
53
 
52
- # Core enums and constants (no dependencies)
53
- try:
54
- from chuk_ai_session_manager.models.event_source import EventSource
55
- from chuk_ai_session_manager.models.event_type import EventType
56
- except ImportError as e:
57
- logger.warning(f"Could not import core enums: {e}")
58
- EventSource = None
59
- EventType = None
60
-
61
- # Storage setup function (no circular dependencies)
62
- try:
63
- from chuk_ai_session_manager.session_storage import setup_chuk_sessions_storage
64
- except ImportError as e:
65
- logger.warning(f"Could not import storage setup: {e}")
66
- setup_chuk_sessions_storage = None
54
+ # Core enums and constants
55
+ from chuk_ai_session_manager.models.event_source import EventSource
56
+ from chuk_ai_session_manager.models.event_type import EventType
67
57
 
68
58
  # Exception classes
69
- try:
70
- from chuk_ai_session_manager.exceptions import (
71
- SessionManagerError,
72
- SessionNotFound,
73
- SessionAlreadyExists,
74
- InvalidSessionOperation,
75
- TokenLimitExceeded,
76
- StorageError,
77
- ToolProcessingError
78
- )
79
- except ImportError as e:
80
- logger.warning(f"Could not import exceptions: {e}")
81
- # Define minimal fallback exceptions
82
- class SessionManagerError(Exception):
83
- """Base exception for session manager errors."""
84
- pass
85
-
86
- SessionNotFound = SessionManagerError
87
- SessionAlreadyExists = SessionManagerError
88
- InvalidSessionOperation = SessionManagerError
89
- TokenLimitExceeded = SessionManagerError
90
- StorageError = SessionManagerError
91
- ToolProcessingError = SessionManagerError
92
-
93
- # Core models (may have some dependencies)
94
- Session = None
95
- SessionEvent = None
96
- SessionMetadata = None
97
- SessionRun = None
98
- RunStatus = None
99
- TokenUsage = None
100
-
101
- try:
102
- from chuk_ai_session_manager.models.session_metadata import SessionMetadata
103
- except ImportError as e:
104
- logger.debug(f"Could not import SessionMetadata: {e}")
105
-
106
- try:
107
- from chuk_ai_session_manager.models.session_run import SessionRun, RunStatus
108
- except ImportError as e:
109
- logger.debug(f"Could not import SessionRun: {e}")
110
-
111
- try:
112
- from chuk_ai_session_manager.models.token_usage import TokenUsage, TokenSummary
113
- except ImportError as e:
114
- logger.debug(f"Could not import TokenUsage: {e}")
115
- TokenSummary = None
116
-
117
- try:
118
- from chuk_ai_session_manager.models.session_event import SessionEvent
119
- except ImportError as e:
120
- logger.debug(f"Could not import SessionEvent: {e}")
121
-
122
- try:
123
- from chuk_ai_session_manager.models.session import Session
124
- except ImportError as e:
125
- logger.debug(f"Could not import Session: {e}")
126
-
127
- # Simple API (highest level, most dependencies)
128
- SessionManager = None
129
- track_conversation = None
130
- track_llm_call = None
131
- quick_conversation = None
132
- track_infinite_conversation = None
59
+ from chuk_ai_session_manager.exceptions import (
60
+ SessionManagerError,
61
+ SessionNotFound,
62
+ SessionAlreadyExists,
63
+ InvalidSessionOperation,
64
+ TokenLimitExceeded,
65
+ StorageError,
66
+ ToolProcessingError
67
+ )
133
68
 
134
- try:
135
- from chuk_ai_session_manager.api.simple_api import (
136
- SessionManager,
137
- track_conversation,
138
- track_llm_call,
139
- quick_conversation,
140
- track_infinite_conversation
141
- )
142
- except ImportError as e:
143
- logger.warning(f"Could not import simple API: {e}")
144
-
145
- # Advanced components (optional)
146
- InfiniteConversationManager = None
147
- SessionAwareToolProcessor = None
148
- build_prompt_from_session = None
149
- PromptStrategy = None
150
-
151
- try:
152
- from chuk_ai_session_manager.infinite_conversation import InfiniteConversationManager
153
- except ImportError as e:
154
- logger.debug(f"Could not import InfiniteConversationManager: {e}")
155
-
156
- try:
157
- from chuk_ai_session_manager.session_aware_tool_processor import SessionAwareToolProcessor
158
- except ImportError as e:
159
- logger.debug(f"Could not import SessionAwareToolProcessor: {e}")
160
-
161
- try:
162
- from chuk_ai_session_manager.session_prompt_builder import (
163
- build_prompt_from_session,
164
- PromptStrategy
165
- )
166
- except ImportError as e:
167
- logger.debug(f"Could not import prompt builder: {e}")
69
+ # Core models
70
+ from chuk_ai_session_manager.models.session_metadata import SessionMetadata
71
+ from chuk_ai_session_manager.models.session_run import SessionRun, RunStatus
72
+ from chuk_ai_session_manager.models.token_usage import TokenUsage, TokenSummary
73
+ from chuk_ai_session_manager.models.session_event import SessionEvent
74
+ from chuk_ai_session_manager.models.session import Session
75
+
76
+ # Storage backend
77
+ from chuk_ai_session_manager.session_storage import setup_chuk_sessions_storage
78
+
79
+ # High-level session manager
80
+ from chuk_ai_session_manager.session_manager import SessionManager
81
+
82
+ # Simple API - The main interface most users will use
83
+ from chuk_ai_session_manager.api.simple_api import (
84
+ track_conversation,
85
+ track_llm_call,
86
+ quick_conversation,
87
+ track_infinite_conversation,
88
+ track_tool_use,
89
+ get_session_stats,
90
+ get_conversation_history
91
+ )
168
92
 
93
+ # Advanced components
94
+ from chuk_ai_session_manager.infinite_conversation import (
95
+ InfiniteConversationManager,
96
+ SummarizationStrategy
97
+ )
169
98
 
170
- def _auto_setup_storage(sandbox_id: str = "chuk-ai-session-manager",
171
- default_ttl_hours: int = 24) -> bool:
172
- """
173
- Auto-setup storage with sensible defaults if not already configured.
174
-
175
- Args:
176
- sandbox_id: CHUK Sessions sandbox ID to use
177
- default_ttl_hours: Default TTL for sessions
178
-
179
- Returns:
180
- True if setup was successful, False otherwise
181
- """
182
- if setup_chuk_sessions_storage is None:
183
- logger.warning("Storage setup not available - imports failed")
184
- return False
185
-
186
- try:
187
- # Try to get existing backend first
188
- from chuk_ai_session_manager.session_storage import get_backend
189
- get_backend() # This will trigger setup if needed
190
- logger.debug("Storage backend already configured")
191
- return True
192
- except Exception:
193
- # Setup with defaults if not configured
194
- try:
195
- setup_chuk_sessions_storage(
196
- sandbox_id=sandbox_id,
197
- default_ttl_hours=default_ttl_hours
198
- )
199
- logger.info(f"Auto-configured storage with sandbox_id='{sandbox_id}'")
200
- return True
201
- except Exception as e:
202
- logger.error(f"Failed to auto-setup storage: {e}")
203
- return False
99
+ from chuk_ai_session_manager.session_aware_tool_processor import SessionAwareToolProcessor
204
100
 
101
+ from chuk_ai_session_manager.session_prompt_builder import (
102
+ build_prompt_from_session,
103
+ PromptStrategy,
104
+ truncate_prompt_to_token_limit
105
+ )
205
106
 
107
+ # Configuration functions
206
108
  def configure_storage(sandbox_id: str = "chuk-ai-session-manager",
207
109
  default_ttl_hours: int = 24) -> bool:
208
110
  """
209
- Explicitly configure the storage backend.
111
+ Configure the storage backend.
210
112
 
211
113
  Args:
212
114
  sandbox_id: CHUK Sessions sandbox ID to use
@@ -214,8 +116,22 @@ def configure_storage(sandbox_id: str = "chuk-ai-session-manager",
214
116
 
215
117
  Returns:
216
118
  True if configuration was successful, False otherwise
119
+
120
+ Note:
121
+ Storage provider is controlled by SESSION_PROVIDER environment variable:
122
+ - memory (default): Fast, no persistence, no extra dependencies
123
+ - redis: Persistent, requires pip install chuk-ai-session-manager[redis]
217
124
  """
218
- return _auto_setup_storage(sandbox_id, default_ttl_hours)
125
+ try:
126
+ setup_chuk_sessions_storage(
127
+ sandbox_id=sandbox_id,
128
+ default_ttl_hours=default_ttl_hours
129
+ )
130
+ logger.info(f"Storage configured with sandbox_id='{sandbox_id}'")
131
+ return True
132
+ except Exception as e:
133
+ logger.error(f"Failed to configure storage: {e}")
134
+ return False
219
135
 
220
136
 
221
137
  def get_version() -> str:
@@ -230,32 +146,80 @@ def is_available() -> dict:
230
146
  Returns:
231
147
  Dictionary showing availability of each component
232
148
  """
149
+ # Check if Redis is available
150
+ redis_available = False
151
+ try:
152
+ import redis
153
+ redis_available = True
154
+ except ImportError:
155
+ pass
156
+
157
+ # Check if tiktoken is available for enhanced token counting
158
+ tiktoken_available = False
159
+ try:
160
+ import tiktoken
161
+ tiktoken_available = True
162
+ except ImportError:
163
+ pass
164
+
233
165
  return {
234
- "core_enums": EventSource is not None and EventType is not None,
235
- "core_models": Session is not None and SessionEvent is not None,
236
- "simple_api": SessionManager is not None,
237
- "storage": setup_chuk_sessions_storage is not None,
238
- "infinite_context": InfiniteConversationManager is not None,
239
- "tool_processor": SessionAwareToolProcessor is not None,
240
- "prompt_builder": build_prompt_from_session is not None,
241
- "token_tracking": TokenUsage is not None,
242
- "exceptions": True, # Always available (fallbacks defined)
166
+ "core_enums": True,
167
+ "core_models": True,
168
+ "simple_api": True,
169
+ "storage": True,
170
+ "infinite_context": True,
171
+ "tool_processor": True,
172
+ "prompt_builder": True,
173
+ "token_tracking": True,
174
+ "exceptions": True,
175
+ "session_manager": True,
176
+ "redis_support": redis_available,
177
+ "enhanced_token_counting": tiktoken_available,
243
178
  }
244
179
 
245
180
 
246
- # Main exports - prioritize what works
247
- __all__ = []
248
-
249
- # Always available
250
- __all__.extend([
181
+ def get_storage_info() -> dict:
182
+ """
183
+ Get information about the current storage configuration.
184
+
185
+ Returns:
186
+ Dictionary with storage configuration details
187
+ """
188
+ import os
189
+ from chuk_ai_session_manager.session_storage import get_backend
190
+
191
+ try:
192
+ backend = get_backend()
193
+ stats = backend.get_stats()
194
+
195
+ return {
196
+ "provider": os.getenv("SESSION_PROVIDER", "memory"),
197
+ "backend": stats.get("backend", "unknown"),
198
+ "sandbox_id": stats.get("sandbox_id", "unknown"),
199
+ "redis_url": os.getenv("SESSION_REDIS_URL", "not_set"),
200
+ "stats": stats
201
+ }
202
+ except Exception as e:
203
+ return {
204
+ "provider": os.getenv("SESSION_PROVIDER", "memory"),
205
+ "error": str(e)
206
+ }
207
+
208
+
209
+ # Main exports - everything should be available
210
+ __all__ = [
211
+ # Version and utilities
251
212
  "__version__",
252
213
  "get_version",
253
214
  "is_available",
254
215
  "configure_storage",
255
- ])
256
-
257
- # Add exception classes (always available)
258
- __all__.extend([
216
+ "get_storage_info",
217
+
218
+ # Core enums
219
+ "EventSource",
220
+ "EventType",
221
+
222
+ # Exception classes
259
223
  "SessionManagerError",
260
224
  "SessionNotFound",
261
225
  "SessionAlreadyExists",
@@ -263,67 +227,49 @@ __all__.extend([
263
227
  "TokenLimitExceeded",
264
228
  "StorageError",
265
229
  "ToolProcessingError",
266
- ])
267
-
268
- # Add available components conditionally
269
- if EventSource is not None:
270
- __all__.append("EventSource")
271
- if EventType is not None:
272
- __all__.append("EventType")
273
-
274
- if setup_chuk_sessions_storage is not None:
275
- __all__.append("setup_chuk_sessions_storage")
276
-
277
- # Simple API (most important for users)
278
- if SessionManager is not None:
279
- __all__.extend([
280
- "SessionManager",
281
- "track_conversation",
282
- "track_llm_call",
283
- "quick_conversation",
284
- "track_infinite_conversation",
285
- ])
286
-
287
- # Core models
288
- if Session is not None:
289
- __all__.append("Session")
290
- if SessionEvent is not None:
291
- __all__.append("SessionEvent")
292
- if SessionMetadata is not None:
293
- __all__.append("SessionMetadata")
294
- if SessionRun is not None:
295
- __all__.extend(["SessionRun", "RunStatus"])
296
- if TokenUsage is not None:
297
- __all__.append("TokenUsage")
298
- if TokenSummary is not None:
299
- __all__.append("TokenSummary")
300
-
301
- # Advanced components
302
- if InfiniteConversationManager is not None:
303
- __all__.append("InfiniteConversationManager")
304
- if SessionAwareToolProcessor is not None:
305
- __all__.append("SessionAwareToolProcessor")
306
- if build_prompt_from_session is not None:
307
- __all__.extend(["build_prompt_from_session", "PromptStrategy"])
308
-
309
- # Auto-setup on import (with error handling)
230
+
231
+ # Core models
232
+ "Session",
233
+ "SessionEvent",
234
+ "SessionMetadata",
235
+ "SessionRun",
236
+ "RunStatus",
237
+ "TokenUsage",
238
+ "TokenSummary",
239
+
240
+ # Storage
241
+ "setup_chuk_sessions_storage",
242
+
243
+ # Primary interfaces - what most users will use
244
+ "SessionManager",
245
+ "track_conversation",
246
+ "track_llm_call",
247
+ "quick_conversation",
248
+ "track_infinite_conversation",
249
+ "track_tool_use",
250
+ "get_session_stats",
251
+ "get_conversation_history",
252
+
253
+ # Advanced components
254
+ "InfiniteConversationManager",
255
+ "SummarizationStrategy",
256
+ "SessionAwareToolProcessor",
257
+ "build_prompt_from_session",
258
+ "PromptStrategy",
259
+ "truncate_prompt_to_token_limit",
260
+ ]
261
+
262
+ # Auto-setup storage on import
310
263
  try:
311
- _auto_setup_storage()
264
+ configure_storage()
265
+ logger.debug("Auto-configured storage backend")
312
266
  except Exception as e:
313
- logger.debug(f"Auto-setup failed (this is normal on first import): {e}")
267
+ logger.debug(f"Auto-setup skipped: {e}")
314
268
 
315
- # Log successful import
316
- available = is_available()
317
- available_count = sum(available.values())
318
- total_count = len(available)
319
- logger.debug(f"CHUK AI Session Manager imported successfully "
320
- f"({available_count}/{total_count} components available)")
321
-
322
- # Show warning if core components are missing
323
- if not available.get("simple_api", False):
324
- logger.warning(
325
- "Simple API not available - you may need to install missing dependencies. "
326
- "Check the logs above for specific import errors."
327
- )
328
- elif available_count == total_count:
329
- logger.debug("All components loaded successfully")
269
+ # Log successful import with storage info
270
+ try:
271
+ storage_info = get_storage_info()
272
+ provider = storage_info.get("provider", "unknown")
273
+ logger.debug(f"CHUK AI Session Manager v{__version__} imported successfully (storage: {provider})")
274
+ except Exception:
275
+ logger.debug(f"CHUK AI Session Manager v{__version__} imported successfully")
@@ -0,0 +1,262 @@
1
+ Metadata-Version: 2.4
2
+ Name: chuk-ai-session-manager
3
+ Version: 0.7
4
+ Summary: Session manager for AI applications
5
+ Requires-Python: >=3.11
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: chuk-sessions>=0.4.1
8
+ Requires-Dist: chuk-tool-processor>=0.4.1
9
+ Requires-Dist: pydantic>=2.11.3
10
+ Provides-Extra: redis
11
+ Requires-Dist: chuk-sessions[redis]>=0.4.1; extra == "redis"
12
+ Requires-Dist: redis>=4.0.0; extra == "redis"
13
+ Provides-Extra: tiktoken
14
+ Requires-Dist: tiktoken>=0.9.0; extra == "tiktoken"
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
17
+ Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
18
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
19
+ Requires-Dist: black>=23.0.0; extra == "dev"
20
+ Requires-Dist: isort>=5.12.0; extra == "dev"
21
+ Requires-Dist: mypy>=1.0.0; extra == "dev"
22
+ Provides-Extra: all
23
+ Requires-Dist: chuk-sessions[redis]>=0.4; extra == "all"
24
+ Requires-Dist: redis>=4.0.0; extra == "all"
25
+ Requires-Dist: tiktoken>=0.9.0; extra == "all"
26
+
27
+ # CHUK AI Session Manager
28
+
29
+ **A powerful session management system for AI applications**
30
+
31
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
32
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
33
+
34
+ Automatic conversation tracking, token usage monitoring, tool call logging, infinite context support with automatic summarization, and hierarchical session relationships. Perfect for AI applications that need reliable session management.
35
+
36
+ ## 🚀 Quick Start
37
+
38
+ ### Installation Options
39
+
40
+ ```bash
41
+ # Basic installation (memory storage only)
42
+ pip install chuk-ai-session-manager
43
+
44
+ # With Redis support for production
45
+ pip install chuk-ai-session-manager[redis]
46
+
47
+ # With enhanced token counting
48
+ pip install chuk-ai-session-manager[tiktoken]
49
+
50
+ # Full installation with all optional features
51
+ pip install chuk-ai-session-manager[all]
52
+
53
+ # Development installation
54
+ pip install chuk-ai-session-manager[dev]
55
+ ```
56
+
57
+ ### Quick Example
58
+
59
+ ```python
60
+ from chuk_ai_session_manager import track_conversation
61
+
62
+ # Track any conversation automatically
63
+ session_id = await track_conversation(
64
+ user_message="What's the weather like?",
65
+ ai_response="I don't have access to real-time weather data.",
66
+ model="gpt-3.5-turbo",
67
+ provider="openai"
68
+ )
69
+
70
+ print(f"Conversation tracked in session: {session_id}")
71
+ ```
72
+
73
+ That's it! Zero configuration required.
74
+
75
+ ## ⚡ Major Features
76
+
77
+ ### 🎯 **Zero-Configuration Tracking**
78
+ ```python
79
+ from chuk_ai_session_manager import SessionManager
80
+
81
+ # Just start using it
82
+ sm = SessionManager()
83
+ await sm.user_says("Hello!")
84
+ await sm.ai_responds("Hi there!", model="gpt-4")
85
+
86
+ # Get stats instantly
87
+ stats = await sm.get_stats()
88
+ print(f"Tokens: {stats['total_tokens']}, Cost: ${stats['estimated_cost']:.4f}")
89
+ ```
90
+
91
+ ### 🔄 **Infinite Context**
92
+ ```python
93
+ # Automatically handles conversations longer than token limits
94
+ sm = SessionManager(infinite_context=True, token_threshold=4000)
95
+ await sm.user_says("Tell me about the history of computing...")
96
+ await sm.ai_responds("Computing history begins with...", model="gpt-4")
97
+ # Session will auto-segment when limits are reached
98
+ ```
99
+
100
+ ### ⚙️ **Storage Backends**
101
+
102
+ | Installation | Storage | Use Case | Performance |
103
+ |-------------|---------|----------|-------------|
104
+ | `pip install chuk-ai-session-manager` | Memory | Development, testing | 1.8M ops/sec |
105
+ | `pip install chuk-ai-session-manager[redis]` | Redis | Production, persistence | 20K ops/sec |
106
+
107
+ ### 🛠️ **Tool Integration**
108
+ ```python
109
+ # Automatic tool call tracking
110
+ await sm.tool_used(
111
+ tool_name="calculator",
112
+ arguments={"operation": "add", "a": 5, "b": 3},
113
+ result={"result": 8}
114
+ )
115
+ ```
116
+
117
+ ## 💡 Common Use Cases
118
+
119
+ ### Web App Conversation Tracking
120
+ ```python
121
+ from chuk_ai_session_manager import track_conversation
122
+
123
+ # In your chat endpoint
124
+ session_id = await track_conversation(
125
+ user_message=request.message,
126
+ ai_response=ai_response,
127
+ model="gpt-4",
128
+ provider="openai",
129
+ session_id=request.session_id # Continue existing conversation
130
+ )
131
+ ```
132
+
133
+ ### LLM Wrapper with Automatic Tracking
134
+ ```python
135
+ from chuk_ai_session_manager import track_llm_call
136
+ import openai
137
+
138
+ async def my_openai_call(prompt):
139
+ response = await openai.chat.completions.create(
140
+ model="gpt-3.5-turbo",
141
+ messages=[{"role": "user", "content": prompt}]
142
+ )
143
+ return response.choices[0].message.content
144
+
145
+ # Automatically tracked
146
+ response, session_id = await track_llm_call(
147
+ user_input="Explain machine learning",
148
+ llm_function=my_openai_call,
149
+ model="gpt-3.5-turbo",
150
+ provider="openai"
151
+ )
152
+ ```
153
+
154
+ ### Long Conversations with Auto-Segmentation
155
+ ```python
156
+ from chuk_ai_session_manager import track_infinite_conversation
157
+
158
+ # Start a conversation
159
+ session_id = await track_infinite_conversation(
160
+ user_message="Tell me about the history of computing",
161
+ ai_response="Computing history begins with ancient calculating devices...",
162
+ model="gpt-4",
163
+ token_threshold=4000 # Auto-segment after 4000 tokens
164
+ )
165
+
166
+ # Continue the conversation - will auto-segment if needed
167
+ session_id = await track_infinite_conversation(
168
+ user_message="What about quantum computers?",
169
+ ai_response="Quantum computing represents a fundamental shift...",
170
+ session_id=session_id,
171
+ model="gpt-4"
172
+ )
173
+ ```
174
+
175
+ ## 🔧 Configuration
176
+
177
+ ### Storage Configuration
178
+
179
+ ```bash
180
+ # Memory provider (default) - fast, no persistence
181
+ export SESSION_PROVIDER=memory
182
+
183
+ # Redis provider - persistent, production-ready (requires redis extra)
184
+ export SESSION_PROVIDER=redis
185
+ export SESSION_REDIS_URL=redis://localhost:6379/0
186
+ ```
187
+
188
+ ### Installation Matrix
189
+
190
+ | Command | Memory | Redis | Token Counting | Use Case |
191
+ |---------|--------|-------|----------------|----------|
192
+ | `pip install chuk-ai-session-manager` | ✅ | ❌ | Basic | Development |
193
+ | `pip install chuk-ai-session-manager[redis]` | ✅ | ✅ | Basic | Production |
194
+ | `pip install chuk-ai-session-manager[tiktoken]` | ✅ | ❌ | Enhanced | Better accuracy |
195
+ | `pip install chuk-ai-session-manager[all]` | ✅ | ✅ | Enhanced | Full features |
196
+
197
+ ## 📊 Monitoring & Analytics
198
+
199
+ ```python
200
+ # Get comprehensive session analytics
201
+ stats = await sm.get_stats(include_all_segments=True)
202
+
203
+ print(f"""
204
+ 🚀 Session Analytics Dashboard
205
+ ============================
206
+ Session ID: {stats['session_id']}
207
+ Total Messages: {stats['total_messages']}
208
+ User Messages: {stats['user_messages']}
209
+ AI Messages: {stats['ai_messages']}
210
+ Tool Calls: {stats['tool_calls']}
211
+ Total Tokens: {stats['total_tokens']}
212
+ Total Cost: ${stats['estimated_cost']:.6f}
213
+ Session Segments: {stats.get('session_segments', 1)}
214
+ """)
215
+ ```
216
+
217
+ ## 🏗️ Why CHUK AI Session Manager?
218
+
219
+ - **Zero Configuration**: Start tracking conversations in 3 lines of code
220
+ - **Infinite Context**: Never worry about token limits again
221
+ - **Universal**: Works with any LLM provider (OpenAI, Anthropic, etc.)
222
+ - **Production Ready**: Built-in persistence, monitoring, and error handling
223
+ - **Token Aware**: Automatic cost tracking across all providers
224
+ - **Tool Friendly**: Seamless tool call logging and retry mechanisms
225
+
226
+ ## 🛡️ Error Handling
227
+
228
+ ```python
229
+ from chuk_ai_session_manager import (
230
+ SessionManagerError,
231
+ SessionNotFound,
232
+ TokenLimitExceeded
233
+ )
234
+
235
+ try:
236
+ session_id = await track_conversation("Hello", "Hi there")
237
+ except SessionNotFound as e:
238
+ print(f"Session not found: {e}")
239
+ except TokenLimitExceeded as e:
240
+ print(f"Token limit exceeded: {e}")
241
+ except SessionManagerError as e:
242
+ print(f"General session error: {e}")
243
+ ```
244
+
245
+ ## 🔄 Dependencies
246
+
247
+ - **Required**: `chuk-sessions` (session storage), `pydantic` (data models), `chuk-tool-processor` (tool integration)
248
+ - **Optional**: `redis` (Redis storage), `tiktoken` (accurate token counting)
249
+
250
+ ## 📄 License
251
+
252
+ MIT License - build amazing AI applications with confidence!
253
+
254
+ ---
255
+
256
+ **Ready to build better AI applications?**
257
+
258
+ ```bash
259
+ pip install chuk-ai-session-manager
260
+ ```
261
+
262
+ **Start tracking conversations in 30 seconds!**
@@ -1,4 +1,4 @@
1
- chuk_ai_session_manager/__init__.py,sha256=Kg8_fqwkV88b0f9H8Q45MDqaZdHHTo3Wf0Oko89DhHg,10217
1
+ chuk_ai_session_manager/__init__.py,sha256=r24MtKySdzvUgK8psNHYHiTRzOUAEXPOBmaNg2cjyFw,7882
2
2
  chuk_ai_session_manager/exceptions.py,sha256=WqrrUZuOAiUmz7tKnSnk0y222U_nV9a8LyaXLayn2fg,4420
3
3
  chuk_ai_session_manager/infinite_conversation.py,sha256=7j3caMnsX27M5rjj4oOkqiy_2AfcupWwsAWRflnKiSo,12092
4
4
  chuk_ai_session_manager/sample_tools.py,sha256=U-jTGveTJ95uSnA4jB30fJQJG3K-TGxN9jcOY6qVHZQ,8179
@@ -16,7 +16,7 @@ chuk_ai_session_manager/models/session_event.py,sha256=RTghC9_sDHzD8qdgEYCoclJzp
16
16
  chuk_ai_session_manager/models/session_metadata.py,sha256=KFG7lc_E0BQTP2OD9Y529elVGJXppDUMqz8vVONW0rw,1510
17
17
  chuk_ai_session_manager/models/session_run.py,sha256=uhMM4-WSrqOUsiWQPnyakInd-foZhxI-YnSHSWiZZwE,4369
18
18
  chuk_ai_session_manager/models/token_usage.py,sha256=M9Qwmeb2woILaSRwA2SIAiG-sIwC3cL_1H-y3NjW5Ik,11436
19
- chuk_ai_session_manager-0.4.1.dist-info/METADATA,sha256=0PvxzwOw2HG8XujmXkMInNVAlrhF149rxyLzXTUROAg,11136
20
- chuk_ai_session_manager-0.4.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
- chuk_ai_session_manager-0.4.1.dist-info/top_level.txt,sha256=5RinqD0v-niHuLYePUREX4gEWTlrpgtUg0RfexVRBMk,24
22
- chuk_ai_session_manager-0.4.1.dist-info/RECORD,,
19
+ chuk_ai_session_manager-0.7.dist-info/METADATA,sha256=dyL99WC_86DIPfkjLr-q4Dw2kKi-J7olpyaOfyFEsgM,7910
20
+ chuk_ai_session_manager-0.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ chuk_ai_session_manager-0.7.dist-info/top_level.txt,sha256=5RinqD0v-niHuLYePUREX4gEWTlrpgtUg0RfexVRBMk,24
22
+ chuk_ai_session_manager-0.7.dist-info/RECORD,,
@@ -1,355 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: chuk-ai-session-manager
3
- Version: 0.4.1
4
- Summary: Session manager for AI applications
5
- Requires-Python: >=3.11
6
- Description-Content-Type: text/markdown
7
- Requires-Dist: chuk-sessions>=0.3
8
- Requires-Dist: chuk-tool-processor>=0.4.1
9
- Requires-Dist: pydantic>=2.11.3
10
- Provides-Extra: tiktoken
11
- Requires-Dist: tiktoken>=0.9.0; extra == "tiktoken"
12
- Provides-Extra: redis
13
- Requires-Dist: redis>=4.0.0; extra == "redis"
14
- Provides-Extra: dev
15
- Requires-Dist: pytest>=7.0.0; extra == "dev"
16
- Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
17
- Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
18
- Requires-Dist: redis>=4.0.0; extra == "dev"
19
- Requires-Dist: black>=23.0.0; extra == "dev"
20
- Requires-Dist: isort>=5.12.0; extra == "dev"
21
- Requires-Dist: mypy>=1.0.0; extra == "dev"
22
- Provides-Extra: full
23
-
24
- # chuk-ai-session-manager
25
-
26
- [![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
27
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
28
-
29
- **The easiest way to add conversation tracking to any AI application.**
30
-
31
- Track conversations, monitor costs, and manage infinite context with just 3 lines of code. Built for production, designed for simplicity.
32
-
33
- ## 🚀 30-Second Start
34
-
35
- ```bash
36
- uv add chuk-ai-session-manager
37
- ```
38
-
39
- ```python
40
- from chuk_ai_session_manager import track_conversation
41
-
42
- # Track any AI conversation in one line
43
- await track_conversation("Hello!", "Hi there! How can I help?")
44
- ```
45
-
46
- That's it! 🎉 Your conversation is now tracked with full observability.
47
-
48
- ## ✨ Why Choose CHUK?
49
-
50
- - **🔥 Stupidly Simple**: 3 lines to track any conversation
51
- - **💰 Cost Smart**: Automatic token counting and cost tracking
52
- - **♾️ Infinite Context**: No more "conversation too long" errors
53
- - **🔧 Any LLM**: Works with OpenAI, Anthropic, local models, anything
54
- - **📊 Full Observability**: See exactly what's happening in your AI app
55
- - **🚀 Production Ready**: Used in real applications, not just demos
56
-
57
- ## 🎯 Perfect For
58
-
59
- - **Building chatbots** that remember conversations
60
- - **Tracking LLM costs** across your entire application
61
- - **Managing long conversations** without hitting token limits
62
- - **Debugging AI applications** with complete audit trails
63
- - **Production AI systems** that need reliable session management
64
-
65
- ## 📱 Quick Examples
66
-
67
- ### Track Any Conversation
68
- ```python
69
- from chuk_ai_session_manager import track_conversation
70
-
71
- # Works with any LLM response
72
- session_id = await track_conversation(
73
- user_message="What's the weather like?",
74
- ai_response="It's sunny and 75°F in your area.",
75
- model="gpt-4",
76
- provider="openai"
77
- )
78
- ```
79
-
80
- ### Persistent Conversations
81
- ```python
82
- from chuk_ai_session_manager import SessionManager
83
-
84
- # Create a conversation that remembers context
85
- sm = SessionManager()
86
-
87
- await sm.user_says("My name is Alice")
88
- await sm.ai_responds("Nice to meet you, Alice!")
89
-
90
- await sm.user_says("What's my name?")
91
- await sm.ai_responds("Your name is Alice!")
92
-
93
- # Get conversation stats
94
- stats = await sm.get_stats()
95
- print(f"Cost: ${stats['estimated_cost']:.6f}")
96
- print(f"Tokens: {stats['total_tokens']}")
97
- ```
98
-
99
- ### Infinite Context (Never Run Out of Space)
100
- ```python
101
- # Automatically handles conversations of any length
102
- sm = SessionManager(
103
- infinite_context=True, # 🔥 Magic happens here
104
- token_threshold=4000 # When to create new segment
105
- )
106
-
107
- # Keep chatting forever - context is preserved automatically
108
- for i in range(100): # This would normally hit token limits
109
- await sm.user_says(f"Question {i}: Tell me about AI")
110
- await sm.ai_responds("AI is fascinating...")
111
-
112
- # Still works! Automatic summarization keeps context alive
113
- conversation = await sm.get_conversation()
114
- print(f"Full conversation: {len(conversation)} exchanges")
115
- ```
116
-
117
- ### Cost Tracking (Know What You're Spending)
118
- ```python
119
- # Automatic cost monitoring across all interactions
120
- sm = SessionManager()
121
-
122
- await sm.user_says("Write a long story about dragons")
123
- await sm.ai_responds("Once upon a time..." * 500) # Long response
124
-
125
- stats = await sm.get_stats()
126
- print(f"💰 That story cost: ${stats['estimated_cost']:.6f}")
127
- print(f"📊 Used {stats['total_tokens']} tokens")
128
- print(f"📈 {stats['user_messages']} user messages, {stats['ai_messages']} AI responses")
129
- ```
130
-
131
- ### Multi-Provider Support
132
- ```python
133
- # Works with any LLM provider
134
- import openai
135
- import anthropic
136
-
137
- sm = SessionManager()
138
-
139
- # OpenAI
140
- await sm.user_says("Hello!")
141
- openai_response = await openai.chat.completions.create(...)
142
- await sm.ai_responds(openai_response.choices[0].message.content, model="gpt-4", provider="openai")
143
-
144
- # Anthropic
145
- await sm.user_says("How are you?")
146
- anthropic_response = await anthropic.messages.create(...)
147
- await sm.ai_responds(anthropic_response.content[0].text, model="claude-3", provider="anthropic")
148
-
149
- # See costs across all providers
150
- stats = await sm.get_stats()
151
- print(f"Total cost across all providers: ${stats['estimated_cost']:.6f}")
152
- ```
153
-
154
- ## 🛠️ Advanced Features
155
-
156
- ### Conversation Analytics
157
- ```python
158
- # Get detailed insights into your conversations
159
- conversation = await sm.get_conversation()
160
- stats = await sm.get_stats()
161
-
162
- print(f"📊 Conversation Analytics:")
163
- print(f" Messages: {stats['user_messages']} user, {stats['ai_messages']} AI")
164
- print(f" Average response length: {stats['avg_response_length']}")
165
- print(f" Most expensive response: ${stats['max_response_cost']:.6f}")
166
- print(f" Session duration: {stats['duration_minutes']:.1f} minutes")
167
- ```
168
-
169
- ### Tool Integration
170
- ```python
171
- # Track tool usage alongside conversations
172
- await sm.tool_used(
173
- tool_name="web_search",
174
- arguments={"query": "latest AI news"},
175
- result={"articles": ["AI breakthrough...", "New model released..."]},
176
- cost=0.001
177
- )
178
-
179
- stats = await sm.get_stats()
180
- print(f"Tool calls: {stats['tool_calls']}")
181
- ```
182
-
183
- ### Session Export/Import
184
- ```python
185
- # Export conversations for analysis
186
- conversation_data = await sm.export_conversation()
187
- with open('conversation.json', 'w') as f:
188
- json.dump(conversation_data, f)
189
-
190
- # Import previous conversations
191
- sm = SessionManager()
192
- await sm.import_conversation('conversation.json')
193
- ```
194
-
195
- ## 🎨 Real-World Examples
196
-
197
- ### Customer Support Bot
198
- ```python
199
- async def handle_support_ticket(user_message: str, ticket_id: str):
200
- # Each ticket gets its own session
201
- sm = SessionManager(session_id=ticket_id)
202
-
203
- await sm.user_says(user_message)
204
-
205
- # Your AI logic here
206
- ai_response = await your_ai_model(user_message)
207
- await sm.ai_responds(ai_response, model="gpt-4", provider="openai")
208
-
209
- # Automatic cost tracking per ticket
210
- stats = await sm.get_stats()
211
- print(f"Ticket {ticket_id} cost: ${stats['estimated_cost']:.6f}")
212
-
213
- return ai_response
214
- ```
215
-
216
- ### AI Assistant with Memory
217
- ```python
218
- async def ai_assistant():
219
- sm = SessionManager(infinite_context=True)
220
-
221
- while True:
222
- user_input = input("You: ")
223
- if user_input.lower() == 'quit':
224
- break
225
-
226
- await sm.user_says(user_input)
227
-
228
- # Get conversation context for AI
229
- conversation = await sm.get_conversation()
230
- context = "\n".join([f"{turn['role']}: {turn['content']}" for turn in conversation[-5:]])
231
-
232
- # Your AI call with context
233
- ai_response = await your_ai_model(f"Context:\n{context}\n\nUser: {user_input}")
234
- await sm.ai_responds(ai_response)
235
-
236
- print(f"AI: {ai_response}")
237
-
238
- # Show final stats
239
- stats = await sm.get_stats()
240
- print(f"\n💰 Total conversation cost: ${stats['estimated_cost']:.6f}")
241
- ```
242
-
243
- ### Multi-User Chat Application
244
- ```python
245
- class ChatApplication:
246
- def __init__(self):
247
- self.user_sessions = {}
248
-
249
- async def handle_message(self, user_id: str, message: str):
250
- # Each user gets their own session
251
- if user_id not in self.user_sessions:
252
- self.user_sessions[user_id] = SessionManager(infinite_context=True)
253
-
254
- sm = self.user_sessions[user_id]
255
- await sm.user_says(message)
256
-
257
- # AI processes with user's personal context
258
- ai_response = await self.generate_response(sm, message)
259
- await sm.ai_responds(ai_response)
260
-
261
- return ai_response
262
-
263
- async def get_user_stats(self, user_id: str):
264
- if user_id in self.user_sessions:
265
- return await self.user_sessions[user_id].get_stats()
266
- return None
267
- ```
268
-
269
- ## 📊 Monitoring Dashboard
270
-
271
- ```python
272
- # Get comprehensive analytics across all sessions
273
- from chuk_ai_session_manager import get_global_stats
274
-
275
- stats = await get_global_stats()
276
- print(f"""
277
- 🚀 AI Application Dashboard
278
- ==========================
279
- Total Sessions: {stats['total_sessions']}
280
- Total Messages: {stats['total_messages']}
281
- Total Cost: ${stats['total_cost']:.2f}
282
- Average Session Length: {stats['avg_session_length']:.1f} messages
283
- Most Active Hour: {stats['peak_hour']}
284
- Top Models Used: {', '.join(stats['top_models'])}
285
- """)
286
- ```
287
-
288
- ## 🔧 Installation Options
289
-
290
- ```bash
291
- # Basic installation
292
- uv add chuk-ai-session-manager
293
-
294
- # With Redis support (for production)
295
- uv add chuk-ai-session-manager[redis]
296
-
297
- # Full installation (all features)
298
- uv add chuk-ai-session-manager[full]
299
-
300
- # Or with pip
301
- pip install chuk-ai-session-manager
302
- ```
303
-
304
- ## 🌟 What Makes CHUK Special?
305
-
306
- | Feature | Other Libraries | CHUK AI Session Manager |
307
- |---------|----------------|------------------------|
308
- | **Setup Complexity** | Complex configuration | 3 lines of code |
309
- | **Cost Tracking** | Manual calculation | Automatic across all providers |
310
- | **Long Conversations** | Token limit errors | Infinite context with auto-segmentation |
311
- | **Multi-Provider** | Provider-specific code | Works with any LLM |
312
- | **Production Ready** | Requires additional work | Built for production |
313
- | **Learning Curve** | Steep | 5 minutes to productivity |
314
-
315
- ## 📖 More Examples
316
-
317
- Check out the `/examples` directory for complete working examples:
318
-
319
- - `simple_tracking.py` - Basic conversation tracking
320
- - `openai_integration.py` - OpenAI API integration
321
- - `infinite_context.py` - Handling long conversations
322
- - `cost_monitoring.py` - Cost tracking and analytics
323
- - `multi_provider.py` - Using multiple LLM providers
324
- - `production_app.py` - Production-ready application
325
-
326
- ## 🎯 Quick Decision Guide
327
-
328
- **Choose CHUK AI Session Manager if you want:**
329
- - ✅ Simple conversation tracking with zero configuration
330
- - ✅ Automatic cost monitoring across all LLM providers
331
- - ✅ Infinite conversation length without token limit errors
332
- - ✅ Production-ready session management out of the box
333
- - ✅ Complete conversation analytics and observability
334
- - ✅ Framework-agnostic solution that works with any LLM library
335
-
336
- ## 🤝 Community & Support
337
-
338
- - 📖 **Documentation**: [Full docs with tutorials](link-to-docs)
339
- - 🐛 **Issues**: Report bugs on GitHub
340
- - 💡 **Feature Requests**: Suggest new features
341
- - 📧 **Support**: enterprise@chuk.dev for production support
342
-
343
- ## 📝 License
344
-
345
- MIT License - build amazing AI applications with confidence!
346
-
347
- ---
348
-
349
- **🎉 Ready to build better AI applications?**
350
-
351
- ```bash
352
- uv add chuk-ai-session-manager
353
- ```
354
-
355
- **Get started in 30 seconds with one line of code!**