-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_manager.py
More file actions
429 lines (353 loc) · 15 KB
/
context_manager.py
File metadata and controls
429 lines (353 loc) · 15 KB
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
"""
Context Manager Module
Manages context across interactions with JSON-based persistence
"""
import json
import os
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
@dataclass
class UserProfile:
"""User profile with preferences and history"""
user_id: str
created_at: str
last_active: str
preferred_style: str = "structured" # structured, minimal, conversational
common_use_cases: List[str] = field(default_factory=list)
refinement_patterns: Dict[str, int] = field(default_factory=dict)
total_prompts: int = 0
average_iterations: float = 0.0
@dataclass
class ConversationContext:
"""Context for a conversation or session"""
conversation_id: str
user_id: Optional[str]
started_at: str
last_updated: str
messages: List[Dict[str, Any]] = field(default_factory=list)
current_prompt: Optional[Dict[str, Any]] = None
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class AgentContext:
"""Context for agent-to-agent interactions on Masumi"""
agent_id: str
relationship_started: str
last_interaction: str
total_transactions: int = 0
service_agreements: List[Dict[str, Any]] = field(default_factory=list)
conversation_history: List[str] = field(default_factory=list)
payment_history: List[Dict[str, Any]] = field(default_factory=list)
class ContextManager:
"""
Manages context across user and agent interactions with JSON persistence
"""
def __init__(self, storage_dir: str = ".context"):
"""
Initialize context manager
Args:
storage_dir: Directory for storing context data
"""
self.storage_dir = Path(storage_dir)
self.storage_dir.mkdir(exist_ok=True)
# Create subdirectories
(self.storage_dir / "users").mkdir(exist_ok=True)
(self.storage_dir / "conversations").mkdir(exist_ok=True)
(self.storage_dir / "agents").mkdir(exist_ok=True)
# In-memory caches
self.user_profiles: Dict[str, UserProfile] = {}
self.conversations: Dict[str, ConversationContext] = {}
self.agent_contexts: Dict[str, AgentContext] = {}
# ─────────────────────────────────────────────────────────────────────
# User Profile Management
# ─────────────────────────────────────────────────────────────────────
def get_user_profile(self, user_id: str) -> UserProfile:
"""
Gets or creates a user profile
Args:
user_id: User identifier
Returns:
UserProfile instance
"""
if user_id in self.user_profiles:
return self.user_profiles[user_id]
# Try to load from disk
profile_path = self.storage_dir / "users" / f"{user_id}.json"
if profile_path.exists():
with open(profile_path, 'r') as f:
data = json.load(f)
profile = UserProfile(**data)
self.user_profiles[user_id] = profile
return profile
# Create new profile
profile = UserProfile(
user_id=user_id,
created_at=datetime.now().isoformat(),
last_active=datetime.now().isoformat()
)
self.user_profiles[user_id] = profile
self._save_user_profile(profile)
return profile
def update_user_profile(self, user_id: str, **kwargs):
"""
Updates user profile with new information
Args:
user_id: User identifier
**kwargs: Fields to update
"""
profile = self.get_user_profile(user_id)
for key, value in kwargs.items():
if hasattr(profile, key):
setattr(profile, key, value)
profile.last_active = datetime.now().isoformat()
self._save_user_profile(profile)
def record_prompt_creation(self, user_id: str, style: str, iterations: int):
"""
Records prompt creation for learning user preferences
Args:
user_id: User identifier
style: Prompt style used
iterations: Number of iterations needed
"""
profile = self.get_user_profile(user_id)
# Update statistics
profile.total_prompts += 1
# Update average iterations
current_avg = profile.average_iterations
n = profile.total_prompts
profile.average_iterations = ((current_avg * (n - 1)) + iterations) / n
# Track preferred style
if style not in profile.refinement_patterns:
profile.refinement_patterns[style] = 0
profile.refinement_patterns[style] += 1
# Update preferred style to the most common
if profile.refinement_patterns:
profile.preferred_style = max(
profile.refinement_patterns,
key=profile.refinement_patterns.get
)
self._save_user_profile(profile)
def _save_user_profile(self, profile: UserProfile):
"""Saves user profile to disk"""
profile_path = self.storage_dir / "users" / f"{profile.user_id}.json"
with open(profile_path, 'w') as f:
json.dump(profile.__dict__, f, indent=2)
# ─────────────────────────────────────────────────────────────────────
# Conversation Context Management
# ─────────────────────────────────────────────────────────────────────
def create_conversation(
self,
user_id: Optional[str] = None,
metadata: Optional[Dict] = None
) -> str:
"""
Creates a new conversation context
Args:
user_id: Optional user identifier
metadata: Optional metadata
Returns:
Conversation ID
"""
conversation_id = f"conv_{datetime.now().strftime('%Y%m%d_%H%M%S')}_{len(self.conversations)}"
conversation = ConversationContext(
conversation_id=conversation_id,
user_id=user_id,
started_at=datetime.now().isoformat(),
last_updated=datetime.now().isoformat(),
metadata=metadata or {}
)
self.conversations[conversation_id] = conversation
self._save_conversation(conversation)
return conversation_id
def add_message(
self,
conversation_id: str,
role: str,
content: Any,
metadata: Optional[Dict] = None
):
"""
Adds a message to a conversation
Args:
conversation_id: Conversation identifier
role: Message role (user, assistant, system)
content: Message content
metadata: Optional metadata
"""
if conversation_id not in self.conversations:
self.conversations[conversation_id] = self._load_conversation(conversation_id)
conversation = self.conversations[conversation_id]
message = {
"role": role,
"content": content,
"timestamp": datetime.now().isoformat(),
"metadata": metadata or {}
}
conversation.messages.append(message)
conversation.last_updated = datetime.now().isoformat()
self._save_conversation(conversation)
def get_conversation_history(
self,
conversation_id: str,
limit: Optional[int] = None
) -> List[Dict[str, Any]]:
"""
Gets conversation history
Args:
conversation_id: Conversation identifier
limit: Optional limit on number of messages
Returns:
List of messages
"""
if conversation_id not in self.conversations:
conversation = self._load_conversation(conversation_id)
if conversation:
self.conversations[conversation_id] = conversation
conversation = self.conversations.get(conversation_id)
if not conversation:
return []
messages = conversation.messages
if limit:
messages = messages[-limit:]
return messages
def update_current_prompt(self, conversation_id: str, prompt_data: Dict[str, Any]):
"""
Updates the current prompt being worked on in a conversation
Args:
conversation_id: Conversation identifier
prompt_data: Prompt data to store
"""
if conversation_id not in self.conversations:
self.conversations[conversation_id] = self._load_conversation(conversation_id)
conversation = self.conversations[conversation_id]
conversation.current_prompt = prompt_data
conversation.last_updated = datetime.now().isoformat()
self._save_conversation(conversation)
def _save_conversation(self, conversation: ConversationContext):
"""Saves conversation to disk"""
conv_path = self.storage_dir / "conversations" / f"{conversation.conversation_id}.json"
with open(conv_path, 'w') as f:
json.dump({
"conversation_id": conversation.conversation_id,
"user_id": conversation.user_id,
"started_at": conversation.started_at,
"last_updated": conversation.last_updated,
"messages": conversation.messages,
"current_prompt": conversation.current_prompt,
"metadata": conversation.metadata
}, f, indent=2)
def _load_conversation(self, conversation_id: str) -> Optional[ConversationContext]:
"""Loads conversation from disk"""
conv_path = self.storage_dir / "conversations" / f"{conversation_id}.json"
if not conv_path.exists():
return None
with open(conv_path, 'r') as f:
data = json.load(f)
return ConversationContext(**data)
# ─────────────────────────────────────────────────────────────────────
# Agent Context Management (for Masumi Network)
# ─────────────────────────────────────────────────────────────────────
def get_agent_context(self, agent_id: str) -> AgentContext:
"""
Gets or creates agent context for agent-to-agent interactions
Args:
agent_id: Agent identifier
Returns:
AgentContext instance
"""
if agent_id in self.agent_contexts:
return self.agent_contexts[agent_id]
# Try to load from disk
agent_path = self.storage_dir / "agents" / f"{agent_id}.json"
if agent_path.exists():
with open(agent_path, 'r') as f:
data = json.load(f)
context = AgentContext(**data)
self.agent_contexts[agent_id] = context
return context
# Create new context
context = AgentContext(
agent_id=agent_id,
relationship_started=datetime.now().isoformat(),
last_interaction=datetime.now().isoformat()
)
self.agent_contexts[agent_id] = context
self._save_agent_context(context)
return context
def record_agent_transaction(
self,
agent_id: str,
transaction_data: Dict[str, Any]
):
"""
Records a transaction with an agent
Args:
agent_id: Agent identifier
transaction_data: Transaction details
"""
context = self.get_agent_context(agent_id)
context.total_transactions += 1
context.payment_history.append({
"timestamp": datetime.now().isoformat(),
**transaction_data
})
context.last_interaction = datetime.now().isoformat()
self._save_agent_context(context)
def add_service_agreement(
self,
agent_id: str,
agreement: Dict[str, Any]
):
"""
Adds a service agreement with an agent
Args:
agent_id: Agent identifier
agreement: Agreement details
"""
context = self.get_agent_context(agent_id)
context.service_agreements.append({
"created_at": datetime.now().isoformat(),
**agreement
})
self._save_agent_context(context)
def _save_agent_context(self, context: AgentContext):
"""Saves agent context to disk"""
agent_path = self.storage_dir / "agents" / f"{context.agent_id}.json"
with open(agent_path, 'w') as f:
json.dump({
"agent_id": context.agent_id,
"relationship_started": context.relationship_started,
"last_interaction": context.last_interaction,
"total_transactions": context.total_transactions,
"service_agreements": context.service_agreements,
"conversation_history": context.conversation_history,
"payment_history": context.payment_history
}, f, indent=2)
# ─────────────────────────────────────────────────────────────────────
# Utility Methods
# ─────────────────────────────────────────────────────────────────────
def clear_old_conversations(self, days: int = 30):
"""
Clears conversations older than specified days
Args:
days: Age threshold in days
"""
cutoff = datetime.now().timestamp() - (days * 24 * 60 * 60)
for conv_file in (self.storage_dir / "conversations").glob("*.json"):
if conv_file.stat().st_mtime < cutoff:
conv_file.unlink()
def get_statistics(self) -> Dict[str, Any]:
"""
Gets context manager statistics
Returns:
Dictionary with statistics
"""
return {
"total_users": len(list((self.storage_dir / "users").glob("*.json"))),
"total_conversations": len(list((self.storage_dir / "conversations").glob("*.json"))),
"total_agents": len(list((self.storage_dir / "agents").glob("*.json"))),
"active_conversations": len(self.conversations),
"cached_profiles": len(self.user_profiles),
"cached_agents": len(self.agent_contexts)
}