chuk-ai-session-manager 0.2.1__py3-none-any.whl → 0.4__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.
- chuk_ai_session_manager/__init__.py +57 -343
- chuk_ai_session_manager/api/simple_api.py +329 -198
- chuk_ai_session_manager/models/token_usage.py +13 -2
- chuk_ai_session_manager/sample_tools.py +1 -1
- chuk_ai_session_manager/session_prompt_builder.py +70 -62
- chuk_ai_session_manager-0.4.dist-info/METADATA +354 -0
- {chuk_ai_session_manager-0.2.1.dist-info → chuk_ai_session_manager-0.4.dist-info}/RECORD +9 -11
- chuk_ai_session_manager/utils/__init__.py +0 -0
- chuk_ai_session_manager/utils/status_display_utils.py +0 -474
- chuk_ai_session_manager-0.2.1.dist-info/METADATA +0 -501
- {chuk_ai_session_manager-0.2.1.dist-info → chuk_ai_session_manager-0.4.dist-info}/WHEEL +0 -0
- {chuk_ai_session_manager-0.2.1.dist-info → chuk_ai_session_manager-0.4.dist-info}/top_level.txt +0 -0
|
@@ -1,359 +1,73 @@
|
|
|
1
1
|
# chuk_ai_session_manager/__init__.py
|
|
2
2
|
"""
|
|
3
|
-
|
|
3
|
+
CHUK AI Session Manager - Simple Developer API
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- Token usage tracking and cost estimation
|
|
8
|
-
- Tool execution logging and result caching
|
|
9
|
-
- Async-first design with proper error handling
|
|
10
|
-
- Multiple prompt building strategies for different use cases
|
|
11
|
-
- Infinite conversation support with automatic summarization
|
|
12
|
-
- Simple developer API for easy integration
|
|
5
|
+
Quick Start:
|
|
6
|
+
from chuk_ai_session_manager import track_conversation
|
|
13
7
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
#
|
|
8
|
+
# Track any conversation
|
|
9
|
+
await track_conversation("Hello!", "Hi there! How can I help?")
|
|
10
|
+
|
|
11
|
+
# Or use the session manager directly
|
|
12
|
+
from chuk_ai_session_manager import SessionManager
|
|
18
13
|
sm = SessionManager()
|
|
19
|
-
await sm.user_says("
|
|
20
|
-
await sm.ai_responds("
|
|
21
|
-
|
|
22
|
-
# Advanced API
|
|
23
|
-
session = await Session.create()
|
|
24
|
-
event = await SessionEvent.create_with_tokens(
|
|
25
|
-
message="Hello world",
|
|
26
|
-
prompt="Hello world",
|
|
27
|
-
model="gpt-4",
|
|
28
|
-
source=EventSource.USER
|
|
29
|
-
)
|
|
30
|
-
await session.add_event_and_save(event)
|
|
14
|
+
await sm.user_says("What's the weather?")
|
|
15
|
+
await sm.ai_responds("It's sunny and 72°F", model="gpt-4")
|
|
31
16
|
"""
|
|
32
17
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
_imported_modules = {}
|
|
18
|
+
# Import core models first (these have no circular dependencies)
|
|
19
|
+
from chuk_ai_session_manager.models.event_source import EventSource
|
|
20
|
+
from chuk_ai_session_manager.models.event_type import EventType
|
|
37
21
|
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
from chuk_ai_session_manager.models.event_source import EventSource
|
|
41
|
-
from chuk_ai_session_manager.models.event_type import EventType
|
|
42
|
-
_imported_modules['enums'] = True
|
|
43
|
-
except ImportError as e:
|
|
44
|
-
_imported_modules['enums'] = f"Import error: {e}"
|
|
45
|
-
EventSource = None
|
|
46
|
-
EventType = None
|
|
22
|
+
# Import storage setup (this should work now with the fixed session_storage.py)
|
|
23
|
+
from chuk_ai_session_manager.session_storage import setup_chuk_sessions_storage
|
|
47
24
|
|
|
48
|
-
#
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
_imported_modules['token_usage'] = True
|
|
52
|
-
except ImportError as e:
|
|
53
|
-
_imported_modules['token_usage'] = f"Import error: {e}"
|
|
54
|
-
TokenUsage = None
|
|
55
|
-
TokenSummary = None
|
|
25
|
+
# Import other models (these might depend on storage being set up)
|
|
26
|
+
from chuk_ai_session_manager.models.session import Session
|
|
27
|
+
from chuk_ai_session_manager.models.session_event import SessionEvent
|
|
56
28
|
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
29
|
+
# Import the simple API (this should work now that storage is fixed)
|
|
30
|
+
from chuk_ai_session_manager.api.simple_api import (
|
|
31
|
+
SessionManager,
|
|
32
|
+
track_conversation,
|
|
33
|
+
track_llm_call,
|
|
34
|
+
quick_conversation,
|
|
35
|
+
track_infinite_conversation
|
|
36
|
+
)
|
|
64
37
|
|
|
65
|
-
|
|
66
|
-
try:
|
|
67
|
-
from chuk_ai_session_manager.models.session_run import SessionRun, RunStatus
|
|
68
|
-
_imported_modules['session_run'] = True
|
|
69
|
-
except ImportError as e:
|
|
70
|
-
_imported_modules['session_run'] = f"Import error: {e}"
|
|
71
|
-
SessionRun = None
|
|
72
|
-
RunStatus = None
|
|
73
|
-
|
|
74
|
-
# Session event (depends on enums and token usage)
|
|
75
|
-
try:
|
|
76
|
-
from chuk_ai_session_manager.models.session_event import SessionEvent
|
|
77
|
-
_imported_modules['session_event'] = True
|
|
78
|
-
except ImportError as e:
|
|
79
|
-
_imported_modules['session_event'] = f"Import error: {e}"
|
|
80
|
-
SessionEvent = None
|
|
81
|
-
|
|
82
|
-
# Session (depends on most other models)
|
|
83
|
-
try:
|
|
84
|
-
from chuk_ai_session_manager.models.session import Session
|
|
85
|
-
_imported_modules['session'] = True
|
|
86
|
-
except ImportError as e:
|
|
87
|
-
_imported_modules['session'] = f"Import error: {e}"
|
|
88
|
-
Session = None
|
|
38
|
+
__version__ = "0.1.0"
|
|
89
39
|
|
|
90
|
-
#
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
from chuk_sessions import SessionManager as ChukSessionManager
|
|
99
|
-
_imported_modules['storage'] = True
|
|
40
|
+
# Main exports - keep it simple
|
|
41
|
+
__all__ = [
|
|
42
|
+
# Simple API - most developers start here
|
|
43
|
+
"SessionManager",
|
|
44
|
+
"track_conversation",
|
|
45
|
+
"track_llm_call",
|
|
46
|
+
"quick_conversation",
|
|
47
|
+
"track_infinite_conversation",
|
|
100
48
|
|
|
101
|
-
#
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
chuk_sessions_storage.setup_chuk_sessions_storage = setup_chuk_sessions_storage
|
|
107
|
-
chuk_sessions_storage.ChukSessionsStore = ChukSessionsStore
|
|
108
|
-
chuk_sessions_storage.ChukSessionManager = ChukSessionManager
|
|
49
|
+
# Core models for advanced usage
|
|
50
|
+
"Session",
|
|
51
|
+
"SessionEvent",
|
|
52
|
+
"EventSource",
|
|
53
|
+
"EventType",
|
|
109
54
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
get_backend = None
|
|
114
|
-
setup_chuk_sessions_storage = None
|
|
115
|
-
ChukSessionsStore = None
|
|
116
|
-
chuk_sessions_storage = None
|
|
117
|
-
ChukSessionManager = None
|
|
118
|
-
|
|
119
|
-
# Exceptions
|
|
120
|
-
try:
|
|
121
|
-
from chuk_ai_session_manager.exceptions import (
|
|
122
|
-
SessionManagerError,
|
|
123
|
-
SessionNotFound,
|
|
124
|
-
SessionAlreadyExists,
|
|
125
|
-
InvalidSessionOperation,
|
|
126
|
-
TokenLimitExceeded,
|
|
127
|
-
StorageError,
|
|
128
|
-
ToolProcessingError
|
|
129
|
-
)
|
|
130
|
-
_imported_modules['exceptions'] = True
|
|
131
|
-
except ImportError as e:
|
|
132
|
-
_imported_modules['exceptions'] = f"Import error: {e}"
|
|
133
|
-
SessionManagerError = None
|
|
134
|
-
SessionNotFound = None
|
|
135
|
-
SessionAlreadyExists = None
|
|
136
|
-
InvalidSessionOperation = None
|
|
137
|
-
TokenLimitExceeded = None
|
|
138
|
-
StorageError = None
|
|
139
|
-
ToolProcessingError = None
|
|
140
|
-
|
|
141
|
-
# Prompt building
|
|
142
|
-
try:
|
|
143
|
-
from chuk_ai_session_manager.session_prompt_builder import (
|
|
144
|
-
PromptStrategy,
|
|
145
|
-
build_prompt_from_session,
|
|
146
|
-
truncate_prompt_to_token_limit
|
|
147
|
-
)
|
|
148
|
-
_imported_modules['prompt_builder'] = True
|
|
149
|
-
except ImportError as e:
|
|
150
|
-
_imported_modules['prompt_builder'] = f"Import error: {e}"
|
|
151
|
-
PromptStrategy = None
|
|
152
|
-
build_prompt_from_session = None
|
|
153
|
-
truncate_prompt_to_token_limit = None
|
|
154
|
-
|
|
155
|
-
# Tool processing
|
|
156
|
-
try:
|
|
157
|
-
from chuk_ai_session_manager.session_aware_tool_processor import SessionAwareToolProcessor
|
|
158
|
-
_imported_modules['tool_processor'] = True
|
|
159
|
-
except ImportError as e:
|
|
160
|
-
_imported_modules['tool_processor'] = f"Import error: {e}"
|
|
161
|
-
SessionAwareToolProcessor = None
|
|
162
|
-
|
|
163
|
-
# Infinite conversation management
|
|
164
|
-
try:
|
|
165
|
-
from chuk_ai_session_manager.infinite_conversation import (
|
|
166
|
-
InfiniteConversationManager,
|
|
167
|
-
SummarizationStrategy
|
|
168
|
-
)
|
|
169
|
-
_imported_modules['infinite_conversation'] = True
|
|
170
|
-
except ImportError as e:
|
|
171
|
-
_imported_modules['infinite_conversation'] = f"Import error: {e}"
|
|
172
|
-
InfiniteConversationManager = None
|
|
173
|
-
SummarizationStrategy = None
|
|
174
|
-
|
|
175
|
-
# Simple API (depends on most other components)
|
|
176
|
-
try:
|
|
177
|
-
from chuk_ai_session_manager.simple_api import (
|
|
178
|
-
SessionManager,
|
|
179
|
-
quick_conversation,
|
|
180
|
-
track_llm_call
|
|
181
|
-
)
|
|
182
|
-
_imported_modules['simple_api'] = True
|
|
183
|
-
except ImportError as e:
|
|
184
|
-
_imported_modules['simple_api'] = f"Import error: {e}"
|
|
185
|
-
SessionManager = None
|
|
186
|
-
quick_conversation = None
|
|
187
|
-
track_llm_call = None
|
|
188
|
-
|
|
189
|
-
# Build __all__ list from successfully imported components
|
|
190
|
-
__all__ = []
|
|
191
|
-
|
|
192
|
-
# Core models
|
|
193
|
-
if EventSource is not None:
|
|
194
|
-
__all__.extend(['EventSource', 'EventType'])
|
|
195
|
-
if TokenUsage is not None:
|
|
196
|
-
__all__.extend(['TokenUsage', 'TokenSummary'])
|
|
197
|
-
if SessionMetadata is not None:
|
|
198
|
-
__all__.append('SessionMetadata')
|
|
199
|
-
if SessionRun is not None:
|
|
200
|
-
__all__.extend(['SessionRun', 'RunStatus'])
|
|
201
|
-
if SessionEvent is not None:
|
|
202
|
-
__all__.append('SessionEvent')
|
|
203
|
-
if Session is not None:
|
|
204
|
-
__all__.append('Session')
|
|
205
|
-
|
|
206
|
-
# Storage
|
|
207
|
-
if SessionStorage is not None:
|
|
208
|
-
__all__.extend([
|
|
209
|
-
'SessionStorage',
|
|
210
|
-
'get_backend',
|
|
211
|
-
'setup_chuk_sessions_storage',
|
|
212
|
-
'ChukSessionsStore',
|
|
213
|
-
'ChukSessionManager',
|
|
214
|
-
'chuk_sessions_storage' # Add the compatibility alias
|
|
215
|
-
])
|
|
216
|
-
|
|
217
|
-
# Exceptions
|
|
218
|
-
if SessionManagerError is not None:
|
|
219
|
-
__all__.extend([
|
|
220
|
-
'SessionManagerError',
|
|
221
|
-
'SessionNotFound',
|
|
222
|
-
'SessionAlreadyExists',
|
|
223
|
-
'InvalidSessionOperation',
|
|
224
|
-
'TokenLimitExceeded',
|
|
225
|
-
'StorageError',
|
|
226
|
-
'ToolProcessingError'
|
|
227
|
-
])
|
|
228
|
-
|
|
229
|
-
# Advanced features
|
|
230
|
-
if PromptStrategy is not None:
|
|
231
|
-
__all__.extend([
|
|
232
|
-
'PromptStrategy',
|
|
233
|
-
'build_prompt_from_session',
|
|
234
|
-
'truncate_prompt_to_token_limit'
|
|
235
|
-
])
|
|
236
|
-
|
|
237
|
-
if SessionAwareToolProcessor is not None:
|
|
238
|
-
__all__.append('SessionAwareToolProcessor')
|
|
239
|
-
|
|
240
|
-
if InfiniteConversationManager is not None:
|
|
241
|
-
__all__.extend(['InfiniteConversationManager', 'SummarizationStrategy'])
|
|
55
|
+
# Setup
|
|
56
|
+
"setup_chuk_sessions_storage",
|
|
57
|
+
]
|
|
242
58
|
|
|
243
|
-
#
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def get_import_status():
|
|
248
|
-
"""
|
|
249
|
-
Get the import status of all modules.
|
|
250
|
-
|
|
251
|
-
Returns:
|
|
252
|
-
Dict[str, Union[bool, str]]: Module import status
|
|
253
|
-
"""
|
|
254
|
-
return _imported_modules.copy()
|
|
255
|
-
|
|
256
|
-
def check_dependencies():
|
|
257
|
-
"""
|
|
258
|
-
Check which optional dependencies are available.
|
|
259
|
-
|
|
260
|
-
Returns:
|
|
261
|
-
Dict[str, bool]: Availability of optional dependencies
|
|
262
|
-
"""
|
|
263
|
-
deps = {}
|
|
264
|
-
|
|
265
|
-
# Check tiktoken for accurate token counting
|
|
59
|
+
# Auto-setup with sensible defaults
|
|
60
|
+
def _auto_setup():
|
|
61
|
+
"""Auto-setup with good defaults if not already configured."""
|
|
266
62
|
try:
|
|
267
|
-
import
|
|
268
|
-
|
|
269
|
-
except
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
# Check chuk_tool_processor
|
|
280
|
-
try:
|
|
281
|
-
import chuk_tool_processor
|
|
282
|
-
deps['chuk_tool_processor'] = True
|
|
283
|
-
except ImportError:
|
|
284
|
-
deps['chuk_tool_processor'] = False
|
|
285
|
-
|
|
286
|
-
return deps
|
|
287
|
-
|
|
288
|
-
# Convenience function to check if package is properly set up
|
|
289
|
-
def verify_installation():
|
|
290
|
-
"""
|
|
291
|
-
Verify that the package is properly installed and configured.
|
|
292
|
-
|
|
293
|
-
Returns:
|
|
294
|
-
Tuple[bool, List[str]]: (is_valid, list_of_issues)
|
|
295
|
-
"""
|
|
296
|
-
issues = []
|
|
297
|
-
|
|
298
|
-
# Check core components
|
|
299
|
-
if Session is None:
|
|
300
|
-
issues.append("Core Session model failed to import")
|
|
301
|
-
if SessionEvent is None:
|
|
302
|
-
issues.append("SessionEvent model failed to import")
|
|
303
|
-
if SessionManager is None:
|
|
304
|
-
issues.append("Simple API (SessionManager) failed to import")
|
|
305
|
-
|
|
306
|
-
# Check dependencies
|
|
307
|
-
deps = check_dependencies()
|
|
308
|
-
if not deps.get('chuk_sessions', False):
|
|
309
|
-
issues.append("chuk_sessions dependency not available - storage backend will not work")
|
|
310
|
-
|
|
311
|
-
is_valid = len(issues) == 0
|
|
312
|
-
return is_valid, issues
|
|
313
|
-
|
|
314
|
-
# Initialize storage backend if available
|
|
315
|
-
def setup_storage(sandbox_id: str = "ai-session-manager", default_ttl_hours: int = 24):
|
|
316
|
-
"""
|
|
317
|
-
Set up the CHUK Sessions storage backend.
|
|
318
|
-
|
|
319
|
-
Args:
|
|
320
|
-
sandbox_id: Sandbox ID for CHUK Sessions
|
|
321
|
-
default_ttl_hours: Default TTL for sessions in hours
|
|
322
|
-
|
|
323
|
-
Returns:
|
|
324
|
-
SessionStorage instance or None if setup failed
|
|
325
|
-
"""
|
|
326
|
-
if setup_chuk_sessions_storage is not None:
|
|
327
|
-
try:
|
|
328
|
-
return setup_chuk_sessions_storage(
|
|
329
|
-
sandbox_id=sandbox_id,
|
|
330
|
-
default_ttl_hours=default_ttl_hours
|
|
331
|
-
)
|
|
332
|
-
except Exception as e:
|
|
333
|
-
import warnings
|
|
334
|
-
warnings.warn(f"Failed to set up CHUK Sessions storage: {e}")
|
|
335
|
-
return None
|
|
336
|
-
else:
|
|
337
|
-
import warnings
|
|
338
|
-
warnings.warn("CHUK Sessions storage not available - imports failed")
|
|
339
|
-
return None
|
|
340
|
-
|
|
341
|
-
# Version info
|
|
342
|
-
__author__ = "CHUK AI Session Manager Team"
|
|
343
|
-
__email__ = "support@chuk.ai"
|
|
344
|
-
__license__ = "MIT"
|
|
345
|
-
__description__ = "AI Session Management with CHUK Sessions Backend"
|
|
346
|
-
|
|
347
|
-
# Package metadata
|
|
348
|
-
__package_info__ = {
|
|
349
|
-
"name": "chuk_ai_session_manager",
|
|
350
|
-
"version": __version__,
|
|
351
|
-
"description": __description__,
|
|
352
|
-
"author": __author__,
|
|
353
|
-
"license": __license__,
|
|
354
|
-
"dependencies": {
|
|
355
|
-
"required": ["pydantic", "uuid", "datetime", "asyncio"],
|
|
356
|
-
"optional": ["tiktoken", "chuk_sessions", "chuk_tool_processor"]
|
|
357
|
-
},
|
|
358
|
-
"import_status": _imported_modules
|
|
359
|
-
}
|
|
63
|
+
from chuk_ai_session_manager.session_storage import get_backend
|
|
64
|
+
get_backend() # This will trigger setup if needed
|
|
65
|
+
except Exception:
|
|
66
|
+
# Silently setup with defaults
|
|
67
|
+
setup_chuk_sessions_storage(
|
|
68
|
+
sandbox_id="chuk-ai-session-manager",
|
|
69
|
+
default_ttl_hours=24
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# Run auto-setup on import
|
|
73
|
+
_auto_setup()
|