-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontext_management_agent.py
More file actions
377 lines (319 loc) · 12.9 KB
/
context_management_agent.py
File metadata and controls
377 lines (319 loc) · 12.9 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
"""Demonstration of context management features: undo, resume, and compaction.
This example showcases three advanced features for managing agent execution state:
1. **Undo** — Run agent for 3 ticks, then undo the last tick to revert conversation
2. **Resume** — Save checkpoint to file, load, and continue execution
3. **Compact** — Build up a long conversation and trigger LLM-based summarization
Supports dual-mode operation via environment variables:
- Without LLM_API_KEY: Uses FakeProvider for deterministic output
- With LLM_API_KEY: Uses OpenAIProvider with real LLM
Environment variables (optional, for OpenAI mode):
LLM_API_KEY — API key for OpenAI-compatible provider
LLM_BASE_URL — Base URL (default: https://dashscope.aliyuncs.com/compatible-mode/v1)
LLM_MODEL — Model name (default: qwen3.5-flash)
Usage:
python examples/context_management_agent.py
"""
from __future__ import annotations
import asyncio
import json
import os
import tempfile
from pathlib import Path
from ecs_agent.components import (
CheckpointComponent,
CompactionConfigComponent,
ConversationComponent,
LLMComponent,
)
from ecs_agent.core import Runner, World
from ecs_agent.providers import FakeProvider
from ecs_agent.providers import OpenAIProvider
from ecs_agent.providers.config import ApiFormat, ProviderConfig
from ecs_agent.systems.checkpoint import CheckpointSystem
from ecs_agent.systems.compaction import CompactionSystem
from ecs_agent.systems.error_handling import ErrorHandlingSystem
from ecs_agent.systems.memory import MemorySystem
from ecs_agent.systems.reasoning import ReasoningSystem
from ecs_agent.types import CompletionResult, Message, Usage
def create_provider() -> FakeProvider | OpenAIProvider:
"""Create LLM provider based on environment variables.
Uses OpenAIProvider if LLM_API_KEY is set, otherwise FakeProvider.
"""
api_key = os.environ.get("LLM_API_KEY", "")
base_url = os.environ.get(
"LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1"
)
model = os.environ.get("LLM_MODEL", "qwen3.5-flash")
if api_key:
return OpenAIProvider(
config=ProviderConfig(
provider_id="openai",
base_url=base_url,
api_key=api_key,
api_format=ApiFormat.OPENAI_CHAT_COMPLETIONS,
),
model=model,
)
return FakeProvider(
responses=[
CompletionResult(
message=Message(
role="assistant",
content="This is response 1 from the agent.",
),
usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30),
),
CompletionResult(
message=Message(
role="assistant",
content="This is response 2 from the agent.",
),
usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30),
),
CompletionResult(
message=Message(
role="assistant",
content="This is response 3 from the agent.",
),
usage=Usage(prompt_tokens=10, completion_tokens=20, total_tokens=30),
),
CompletionResult(
message=Message(
role="assistant",
content="Summary of conversation so far.",
),
usage=Usage(prompt_tokens=50, completion_tokens=30, total_tokens=80),
),
]
)
def _get_model_name() -> str:
"""Get model name from environment or default to 'fake'."""
api_key = os.environ.get("LLM_API_KEY", "")
if api_key:
return os.environ.get("LLM_MODEL", "qwen3.5-flash")
return "fake"
async def part_1_undo() -> None:
"""Part 1: Demonstrate undo functionality."""
print("\n" + "=" * 70)
print("PART 1: UNDO — Revert Agent State")
print("=" * 70)
# Create world and agent
world = World()
provider = create_provider()
agent_id = world.create_entity()
world.add_component(
agent_id,
LLMComponent(
provider=provider,
model=_get_model_name(),
system_prompt="You are a helpful assistant.",
),
)
world.add_component(
agent_id,
ConversationComponent(
messages=[Message(role="user", content="Hello, can you help?")]
),
)
world.add_component(agent_id, CheckpointComponent(max_snapshots=10))
# Register systems
world.register_system(ReasoningSystem(priority=0), priority=0)
world.register_system(CheckpointSystem(), priority=1)
world.register_system(MemorySystem(), priority=10)
world.register_system(ErrorHandlingSystem(priority=99), priority=99)
# Run for 3 ticks
runner = Runner()
await runner.run(world, max_ticks=3)
# Show conversation after 3 ticks
conv = world.get_component(agent_id, ConversationComponent)
print(
f"\nAfter 3 ticks: {len(conv.messages) if conv else 0} messages in conversation"
)
if conv:
for i, msg in enumerate(conv.messages):
content = msg.content[:50] + "..." if len(msg.content) > 50 else msg.content
print(f" [{i}] {msg.role}: {content}")
# Undo the last tick
print("\nUndoing last tick...")
await CheckpointSystem.undo(world, {_get_model_name(): provider}, {})
# Show conversation after undo
conv = world.get_component(agent_id, ConversationComponent)
print(f"\nAfter undo: {len(conv.messages) if conv else 0} messages in conversation")
if conv:
for i, msg in enumerate(conv.messages):
content = msg.content[:50] + "..." if len(msg.content) > 50 else msg.content
print(f" [{i}] {msg.role}: {content}")
print("\n✓ Undo complete — conversation reverted to prior state")
async def part_2_resume() -> None:
"""Part 2: Demonstrate save/resume functionality."""
print("\n" + "=" * 70)
print("PART 2: RESUME — Save and Load Agent State")
print("=" * 70)
# Create initial world and agent
world = World()
provider = create_provider()
agent_id = world.create_entity()
world.add_component(
agent_id,
LLMComponent(
provider=provider,
model=_get_model_name(),
system_prompt="You are a helpful assistant.",
),
)
world.add_component(
agent_id,
ConversationComponent(
messages=[
Message(role="user", content="First question?"),
]
),
)
# Register systems
world.register_system(ReasoningSystem(priority=0), priority=0)
world.register_system(MemorySystem(), priority=10)
world.register_system(ErrorHandlingSystem(priority=99), priority=99)
# Run for 2 ticks
runner = Runner()
await runner.run(world, max_ticks=2)
# Show initial state
conv = world.get_component(agent_id, ConversationComponent)
print(f"\nAfter 2 ticks: {len(conv.messages) if conv else 0} messages")
if conv:
for i, msg in enumerate(conv.messages):
content = msg.content[:50] + "..." if len(msg.content) > 50 else msg.content
print(f" [{i}] {msg.role}: {content}")
# Save checkpoint to temp file
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
checkpoint_path = f.name
try:
runner.save_checkpoint(world, checkpoint_path)
print(f"\n✓ Checkpoint saved to {checkpoint_path}")
# Load checkpoint and resume
loaded_world, current_tick = Runner.load_checkpoint(
checkpoint_path, {_get_model_name(): provider}, {}
)
print(f"✓ Checkpoint loaded (current_tick={current_tick})")
# Create new runner and resume from saved state
new_runner = Runner()
await new_runner.run(loaded_world, max_ticks=4, start_tick=current_tick)
# Show final state
conv = loaded_world.get_component(agent_id, ConversationComponent)
print(
f"\nAfter resume and 2 more ticks: {len(conv.messages) if conv else 0} messages"
)
if conv:
for i, msg in enumerate(conv.messages):
content = (
msg.content[:50] + "..." if len(msg.content) > 50 else msg.content
)
print(f" [{i}] {msg.role}: {content}")
print("\n✓ Resume complete — state preserved and execution continued")
finally:
# Cleanup
Path(checkpoint_path).unlink(missing_ok=True)
async def part_3_compact() -> None:
"""Part 3: Demonstrate conversation compaction."""
print("\n" + "=" * 70)
print("PART 3: COMPACT — Summarize Long Conversations")
print("=" * 70)
# Create world with many initial messages to trigger compaction
world = World()
provider = create_provider()
agent_id = world.create_entity()
# Build a long conversation (10+ messages to exceed threshold)
initial_messages = [
Message(role="user", content="What is machine learning?"),
Message(
role="assistant",
content="Machine learning is a branch of artificial intelligence that focuses on the development of algorithms and statistical models.",
),
Message(role="user", content="Can you explain neural networks?"),
Message(
role="assistant",
content="Neural networks are computing systems inspired by the biological neural networks in animal brains.",
),
Message(role="user", content="What is deep learning?"),
Message(
role="assistant",
content="Deep learning is a subset of machine learning that uses neural networks with multiple layers.",
),
Message(role="user", content="Tell me about transformers."),
Message(
role="assistant",
content="Transformers are a type of neural network architecture introduced in 2017 for natural language processing.",
),
Message(role="user", content="What is a token?"),
Message(
role="assistant",
content="A token is a unit of text that an LLM processes, typically a word, subword, or character.",
),
]
world.add_component(
agent_id,
LLMComponent(
provider=provider,
model=_get_model_name(),
system_prompt="You are a helpful assistant.",
),
)
world.add_component(
agent_id,
ConversationComponent(messages=initial_messages),
)
world.add_component(
agent_id,
CompactionConfigComponent(
threshold_tokens=50, # Low threshold to trigger compaction
summary_model=_get_model_name(),
),
)
print(f"\nBefore compaction: {len(initial_messages)} messages")
# Register systems
world.register_system(CompactionSystem(), priority=0)
world.register_system(MemorySystem(), priority=10)
world.register_system(ErrorHandlingSystem(priority=99), priority=99)
# Run one tick to trigger compaction
await world.process()
# Show result
conv = world.get_component(agent_id, ConversationComponent)
if conv:
print(f"\nAfter compaction: {len(conv.messages)} messages")
print("\nCompacted conversation:")
for i, msg in enumerate(conv.messages):
content = msg.content[:60] + "..." if len(msg.content) > 60 else msg.content
print(f" [{i}] {msg.role}: {content}")
# Show if archive was created
from ecs_agent.components import ConversationArchiveComponent
archive = world.get_component(agent_id, ConversationArchiveComponent)
if archive and archive.archived_summaries:
print(
f"\n✓ Conversation archive created with {len(archive.archived_summaries)} summary"
)
print(f" Summary: {archive.archived_summaries[0][:80]}...")
print("\n✓ Compaction complete — conversation summarized")
async def main() -> None:
"""Run all three context management demonstrations."""
print("\n" + "=" * 70)
print("CONTEXT MANAGEMENT FEATURES DEMO")
print("Demonstrating Undo, Resume, and Compaction")
print("=" * 70)
api_key = os.environ.get("LLM_API_KEY", "")
if api_key:
model = os.environ.get("LLM_MODEL", "qwen3.5-flash")
base_url = os.environ.get(
"LLM_BASE_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1"
)
print(f"Using OpenAIProvider with model: {model}")
print(f"Base URL: {base_url}")
else:
print("No LLM_API_KEY provided. Using FakeProvider for demonstration.")
print("To use a real API, set LLM_API_KEY, LLM_BASE_URL, and LLM_MODEL.")
await part_1_undo()
await part_2_resume()
await part_3_compact()
print("\n" + "=" * 70)
print("ALL DEMONSTRATIONS COMPLETE")
print("=" * 70 + "\n")
if __name__ == "__main__":
asyncio.run(main())