adaptive-memory-multi-model-router 1.2.2 โ 1.3.1
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.
- package/LICENSE +21 -0
- package/README.md +146 -66
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/integrations/airtable.js +20 -0
- package/dist/integrations/discord.js +18 -0
- package/dist/integrations/github.js +23 -0
- package/dist/integrations/gmail.js +19 -0
- package/dist/integrations/google-calendar.js +18 -0
- package/dist/integrations/index.js +61 -0
- package/dist/integrations/jira.js +21 -0
- package/dist/integrations/linear.js +19 -0
- package/dist/integrations/notion.js +19 -0
- package/dist/integrations/slack.js +18 -0
- package/dist/integrations/telegram.js +19 -0
- package/dist/providers/registry.js +7 -3
- package/docs/ARCHITECTURAL-IMPROVEMENTS-2025.md +1391 -0
- package/docs/ARCHITECTURAL-IMPROVEMENTS-REVISED-2025.md +1051 -0
- package/docs/CONFIGURATION.md +476 -0
- package/docs/COUNCIL_DECISION.json +308 -0
- package/docs/COUNCIL_SUMMARY.md +265 -0
- package/docs/COUNCIL_V2.2_DECISION.md +416 -0
- package/docs/IMPROVEMENT_ROADMAP.md +515 -0
- package/docs/LLM_COUNCIL_DECISION.md +508 -0
- package/docs/QUICK_START_VISIBILITY.md +782 -0
- package/docs/REDDIT_GAP_ANALYSIS.md +299 -0
- package/docs/RESEARCH_BACKED_IMPROVEMENTS.md +1180 -0
- package/docs/TMLPD_QNA.md +751 -0
- package/docs/TMLPD_V2.1_COMPLETE.md +763 -0
- package/docs/TMLPD_V2.2_RESEARCH_ROADMAP.md +754 -0
- package/docs/V2.2_IMPLEMENTATION_COMPLETE.md +446 -0
- package/docs/V2_IMPLEMENTATION_GUIDE.md +388 -0
- package/docs/VISIBILITY_ADOPTION_PLAN.md +1005 -0
- package/docs/launch-content/LAUNCH_EXECUTION_CHECKLIST.md +421 -0
- package/docs/launch-content/README.md +457 -0
- package/docs/launch-content/assets/cost_comparison_100_tasks.png +0 -0
- package/docs/launch-content/assets/cumulative_savings.png +0 -0
- package/docs/launch-content/assets/parallel_speedup.png +0 -0
- package/docs/launch-content/assets/provider_pricing_comparison.png +0 -0
- package/docs/launch-content/assets/task_breakdown_comparison.png +0 -0
- package/docs/launch-content/generate_charts.py +313 -0
- package/docs/launch-content/hn_show_post.md +139 -0
- package/docs/launch-content/partner_outreach_templates.md +745 -0
- package/docs/launch-content/reddit_posts.md +467 -0
- package/docs/launch-content/twitter_thread.txt +460 -0
- package/examples/QUICKSTART.md +1 -1
- package/openclaw-alexa-bridge/ALL_REMAINING_FIXES_PLAN.md +313 -0
- package/openclaw-alexa-bridge/REMAINING_FIXES_SUMMARY.md +277 -0
- package/openclaw-alexa-bridge/src/alexa_handler_no_tmlpd.js +1234 -0
- package/openclaw-alexa-bridge/test_fixes.js +77 -0
- package/package.json +120 -29
- package/package.json.tmp +0 -0
- package/qna/TMLPD_QNA.md +3 -3
- package/skill/SKILL.md +2 -2
- package/src/__tests__/integration/tmpld_integration.test.py +540 -0
- package/src/agents/skill_enhanced_agent.py +318 -0
- package/src/memory/__init__.py +15 -0
- package/src/memory/agentic_memory.py +353 -0
- package/src/memory/semantic_memory.py +444 -0
- package/src/memory/simple_memory.py +466 -0
- package/src/memory/working_memory.py +447 -0
- package/src/orchestration/__init__.py +52 -0
- package/src/orchestration/execution_engine.py +353 -0
- package/src/orchestration/halo_orchestrator.py +367 -0
- package/src/orchestration/mcts_workflow.py +498 -0
- package/src/orchestration/role_assigner.py +473 -0
- package/src/orchestration/task_planner.py +522 -0
- package/src/providers/__init__.py +67 -0
- package/src/providers/anthropic.py +304 -0
- package/src/providers/base.py +241 -0
- package/src/providers/cerebras.py +373 -0
- package/src/providers/registry.py +476 -0
- package/src/routing/__init__.py +30 -0
- package/src/routing/universal_router.py +621 -0
- package/src/skills/TMLPD-QUICKREF.md +210 -0
- package/src/skills/TMLPD-SETUP-SUMMARY.md +157 -0
- package/src/skills/TMLPD.md +540 -0
- package/src/skills/__tests__/skill_manager.test.ts +328 -0
- package/src/skills/skill_manager.py +385 -0
- package/src/skills/test-tmlpd.sh +108 -0
- package/src/skills/tmlpd-category.yaml +67 -0
- package/src/skills/tmlpd-monitoring.yaml +188 -0
- package/src/skills/tmlpd-phase.yaml +132 -0
- package/src/state/__init__.py +17 -0
- package/src/state/simple_checkpoint.py +508 -0
- package/src/tmlpd_agent.py +464 -0
- package/src/tmpld_v2.py +427 -0
- package/src/workflows/__init__.py +18 -0
- package/src/workflows/advanced_difficulty_classifier.py +377 -0
- package/src/workflows/chaining_executor.py +417 -0
- package/src/workflows/difficulty_integration.py +209 -0
- package/src/workflows/orchestrator.py +469 -0
- package/src/workflows/orchestrator_executor.py +456 -0
- package/src/workflows/parallelization_executor.py +382 -0
- package/src/workflows/router.py +311 -0
- package/test_integration_simple.py +86 -0
- package/test_mcts_workflow.py +150 -0
- package/test_templd_integration.py +262 -0
- package/test_universal_router.py +275 -0
- package/tmlpd-pi-extension/README.md +36 -0
- package/tmlpd-pi-extension/dist/cache/prefixCache.d.ts +114 -0
- package/tmlpd-pi-extension/dist/cache/prefixCache.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/cache/prefixCache.js +285 -0
- package/tmlpd-pi-extension/dist/cache/prefixCache.js.map +1 -0
- package/tmlpd-pi-extension/dist/cache/responseCache.d.ts +58 -0
- package/tmlpd-pi-extension/dist/cache/responseCache.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/cache/responseCache.js +153 -0
- package/tmlpd-pi-extension/dist/cache/responseCache.js.map +1 -0
- package/tmlpd-pi-extension/dist/cli.js +59 -0
- package/tmlpd-pi-extension/dist/cost/costTracker.d.ts +95 -0
- package/tmlpd-pi-extension/dist/cost/costTracker.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/cost/costTracker.js +240 -0
- package/tmlpd-pi-extension/dist/cost/costTracker.js.map +1 -0
- package/tmlpd-pi-extension/dist/index.d.ts +723 -0
- package/tmlpd-pi-extension/dist/index.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/index.js +239 -0
- package/tmlpd-pi-extension/dist/index.js.map +1 -0
- package/tmlpd-pi-extension/dist/memory/episodicMemory.d.ts +82 -0
- package/tmlpd-pi-extension/dist/memory/episodicMemory.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/memory/episodicMemory.js +145 -0
- package/tmlpd-pi-extension/dist/memory/episodicMemory.js.map +1 -0
- package/tmlpd-pi-extension/dist/orchestration/haloOrchestrator.d.ts +102 -0
- package/tmlpd-pi-extension/dist/orchestration/haloOrchestrator.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/orchestration/haloOrchestrator.js +207 -0
- package/tmlpd-pi-extension/dist/orchestration/haloOrchestrator.js.map +1 -0
- package/tmlpd-pi-extension/dist/orchestration/mctsWorkflow.d.ts +85 -0
- package/tmlpd-pi-extension/dist/orchestration/mctsWorkflow.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/orchestration/mctsWorkflow.js +210 -0
- package/tmlpd-pi-extension/dist/orchestration/mctsWorkflow.js.map +1 -0
- package/tmlpd-pi-extension/dist/providers/localProvider.d.ts +102 -0
- package/tmlpd-pi-extension/dist/providers/localProvider.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/providers/localProvider.js +338 -0
- package/tmlpd-pi-extension/dist/providers/localProvider.js.map +1 -0
- package/tmlpd-pi-extension/dist/providers/registry.d.ts +55 -0
- package/tmlpd-pi-extension/dist/providers/registry.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/providers/registry.js +138 -0
- package/tmlpd-pi-extension/dist/providers/registry.js.map +1 -0
- package/tmlpd-pi-extension/dist/routing/advancedRouter.d.ts +68 -0
- package/tmlpd-pi-extension/dist/routing/advancedRouter.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/routing/advancedRouter.js +332 -0
- package/tmlpd-pi-extension/dist/routing/advancedRouter.js.map +1 -0
- package/tmlpd-pi-extension/dist/tools/tmlpdTools.d.ts +101 -0
- package/tmlpd-pi-extension/dist/tools/tmlpdTools.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/tools/tmlpdTools.js +368 -0
- package/tmlpd-pi-extension/dist/tools/tmlpdTools.js.map +1 -0
- package/tmlpd-pi-extension/dist/utils/batchProcessor.d.ts +96 -0
- package/tmlpd-pi-extension/dist/utils/batchProcessor.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/utils/batchProcessor.js +170 -0
- package/tmlpd-pi-extension/dist/utils/batchProcessor.js.map +1 -0
- package/tmlpd-pi-extension/dist/utils/compression.d.ts +61 -0
- package/tmlpd-pi-extension/dist/utils/compression.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/utils/compression.js +281 -0
- package/tmlpd-pi-extension/dist/utils/compression.js.map +1 -0
- package/tmlpd-pi-extension/dist/utils/reliability.d.ts +74 -0
- package/tmlpd-pi-extension/dist/utils/reliability.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/utils/reliability.js +177 -0
- package/tmlpd-pi-extension/dist/utils/reliability.js.map +1 -0
- package/tmlpd-pi-extension/dist/utils/speculativeDecoding.d.ts +117 -0
- package/tmlpd-pi-extension/dist/utils/speculativeDecoding.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/utils/speculativeDecoding.js +246 -0
- package/tmlpd-pi-extension/dist/utils/speculativeDecoding.js.map +1 -0
- package/tmlpd-pi-extension/dist/utils/tokenUtils.d.ts +50 -0
- package/tmlpd-pi-extension/dist/utils/tokenUtils.d.ts.map +1 -0
- package/tmlpd-pi-extension/dist/utils/tokenUtils.js +124 -0
- package/tmlpd-pi-extension/dist/utils/tokenUtils.js.map +1 -0
- package/tmlpd-pi-extension/examples/QUICKSTART.md +183 -0
- package/tmlpd-pi-extension/package-lock.json +75 -0
- package/tmlpd-pi-extension/package.json +172 -0
- package/tmlpd-pi-extension/python/examples.py +53 -0
- package/tmlpd-pi-extension/python/integrations.py +330 -0
- package/tmlpd-pi-extension/python/setup.py +28 -0
- package/tmlpd-pi-extension/python/tmlpd.py +369 -0
- package/tmlpd-pi-extension/qna/REDDIT_GAP_ANALYSIS.md +299 -0
- package/tmlpd-pi-extension/qna/TMLPD_QNA.md +751 -0
- package/tmlpd-pi-extension/skill/SKILL.md +238 -0
- package/{src โ tmlpd-pi-extension/src}/index.ts +1 -1
- package/tmlpd-pi-extension/tsconfig.json +18 -0
- package/demo/research-demo.js +0 -266
- package/notebooks/quickstart.ipynb +0 -157
- package/rust/tmlpd.h +0 -268
- package/src/cache/prefixCache.ts +0 -365
- package/src/routing/advancedRouter.ts +0 -406
- package/src/utils/speculativeDecoding.ts +0 -344
- /package/{src โ tmlpd-pi-extension/src}/cache/responseCache.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/cost/costTracker.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/memory/episodicMemory.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/orchestration/haloOrchestrator.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/orchestration/mctsWorkflow.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/providers/localProvider.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/providers/registry.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/tools/tmlpdTools.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/utils/batchProcessor.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/utils/compression.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/utils/reliability.ts +0 -0
- /package/{src โ tmlpd-pi-extension/src}/utils/tokenUtils.ts +0 -0
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TMLPD v2.1 - Unified Agent
|
|
3
|
+
|
|
4
|
+
Integration layer combining all phases:
|
|
5
|
+
- Phase 1: Multi-Provider System
|
|
6
|
+
- Phase 2: Difficulty-Aware Routing
|
|
7
|
+
- Phase 3: Advanced Memory System
|
|
8
|
+
- Phase 4: Workflow Executors
|
|
9
|
+
|
|
10
|
+
Built by TMLPD using parallel execution.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
import asyncio
|
|
14
|
+
from typing import Dict, List, Any, Optional
|
|
15
|
+
from datetime import datetime
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
# Import all components
|
|
19
|
+
from .providers.registry import MultiProviderExecutor, IntelligentRouter
|
|
20
|
+
from .skills.skill_manager import SkillManager
|
|
21
|
+
from .workflows.difficulty_integration import DifficultyAwareSkillAgent
|
|
22
|
+
from .workflows.advanced_difficulty_classifier import AdvancedDifficultyClassifier
|
|
23
|
+
from .workflows.chaining_executor import ChainingExecutor
|
|
24
|
+
from .workflows.parallelization_executor import ParallelizationExecutor
|
|
25
|
+
from .workflows.orchestrator_executor import OrchestratorExecutor
|
|
26
|
+
from .memory.agentic_memory import EpisodicMemoryStore
|
|
27
|
+
from .memory.semantic_memory import SemanticMemoryStore
|
|
28
|
+
from .memory.working_memory import WorkingMemoryCache
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TMLPDUnifiedAgent:
|
|
32
|
+
"""
|
|
33
|
+
Unified TMLPD v2.1 Agent with all capabilities.
|
|
34
|
+
|
|
35
|
+
Features:
|
|
36
|
+
- Multi-provider support with intelligent routing
|
|
37
|
+
- Difficulty-aware task classification
|
|
38
|
+
- Three-tier memory system (episodic, semantic, working)
|
|
39
|
+
- Advanced workflow execution (chain, parallel, orchestrator)
|
|
40
|
+
- Skill integration
|
|
41
|
+
- Cost optimization (40-60% savings)
|
|
42
|
+
|
|
43
|
+
Usage:
|
|
44
|
+
agent = TMLPDUnifiedAgent()
|
|
45
|
+
await agent.initialize()
|
|
46
|
+
|
|
47
|
+
result = await agent.execute({
|
|
48
|
+
"description": "Build a REST API with authentication"
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
await agent.cleanup()
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
provider_config: Optional[str] = None,
|
|
57
|
+
skills_dir: str = "tmlpd-skills",
|
|
58
|
+
memory_dir: str = ".taskmaster/memory",
|
|
59
|
+
use_chromadb: bool = False,
|
|
60
|
+
enable_learning: bool = True
|
|
61
|
+
):
|
|
62
|
+
"""
|
|
63
|
+
Initialize TMLPD unified agent.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
provider_config: Path to provider configuration
|
|
67
|
+
skills_dir: Directory containing skills
|
|
68
|
+
memory_dir: Directory for memory storage
|
|
69
|
+
use_chromadb: Whether to use ChromaDB for semantic memory
|
|
70
|
+
enable_learning: Enable learning from past executions
|
|
71
|
+
"""
|
|
72
|
+
# Configuration
|
|
73
|
+
self.provider_config = provider_config
|
|
74
|
+
self.skills_dir = skills_dir
|
|
75
|
+
self.memory_dir = Path(memory_dir)
|
|
76
|
+
self.use_chromadb = use_chromadb
|
|
77
|
+
self.enable_learning = enable_learning
|
|
78
|
+
|
|
79
|
+
# Components (initialized in initialize())
|
|
80
|
+
self.provider_executor = None
|
|
81
|
+
self.skill_manager = None
|
|
82
|
+
self.difficulty_classifier = None
|
|
83
|
+
|
|
84
|
+
# Workflow executors
|
|
85
|
+
self.difficulty_agent = None
|
|
86
|
+
self.chaining_executor = None
|
|
87
|
+
self.parallel_executor = None
|
|
88
|
+
self.orchestrator = None
|
|
89
|
+
|
|
90
|
+
# Memory system
|
|
91
|
+
self.episodic_memory = None
|
|
92
|
+
self.semantic_memory = None
|
|
93
|
+
self.working_memory = None
|
|
94
|
+
|
|
95
|
+
# Statistics
|
|
96
|
+
self.stats = {
|
|
97
|
+
"total_executions": 0,
|
|
98
|
+
"successful_executions": 0,
|
|
99
|
+
"total_cost": 0.0,
|
|
100
|
+
"total_tokens": 0,
|
|
101
|
+
"total_time": 0.0
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async def initialize(self):
|
|
105
|
+
"""Initialize all components"""
|
|
106
|
+
print("\n๐ Initializing TMLPD v2.1 Unified Agent")
|
|
107
|
+
|
|
108
|
+
# Initialize provider system
|
|
109
|
+
print("\n๐ฆ Phase 1: Multi-Provider System")
|
|
110
|
+
self.provider_executor = MultiProviderExecutor(self.provider_config)
|
|
111
|
+
await self.provider_executor.start()
|
|
112
|
+
|
|
113
|
+
status = self.provider_executor.get_status()
|
|
114
|
+
print(f" โ Providers: {status['registry']['total_providers']}")
|
|
115
|
+
print(f" โ Healthy: {status['registry']['healthy_providers']}")
|
|
116
|
+
|
|
117
|
+
# Initialize skill manager
|
|
118
|
+
print("\n๐ Phase 2: Skill System")
|
|
119
|
+
self.skill_manager = SkillManager(self.skills_dir)
|
|
120
|
+
skill_count = len(list(Path(self.skills_dir).rglob("SKILL.md")))
|
|
121
|
+
print(f" โ Loaded {skill_count} skills")
|
|
122
|
+
|
|
123
|
+
# Initialize difficulty classifier
|
|
124
|
+
print("\n๐ฏ Phase 2: Difficulty-Aware Routing")
|
|
125
|
+
self.difficulty_classifier = AdvancedDifficultyClassifier(
|
|
126
|
+
learning_enabled=self.enable_learning
|
|
127
|
+
)
|
|
128
|
+
print(" โ Advanced difficulty classifier ready")
|
|
129
|
+
|
|
130
|
+
# Initialize workflow executors
|
|
131
|
+
print("\nโ๏ธ Phase 4: Workflow Executors")
|
|
132
|
+
|
|
133
|
+
self.difficulty_agent = DifficultyAwareSkillAgent(
|
|
134
|
+
skills_dir=self.skills_dir,
|
|
135
|
+
provider_config=self.provider_config
|
|
136
|
+
)
|
|
137
|
+
await self.difficulty_agent.start()
|
|
138
|
+
print(" โ Difficulty-aware skill agent")
|
|
139
|
+
|
|
140
|
+
self.chaining_executor = ChainingExecutor(
|
|
141
|
+
self.provider_executor,
|
|
142
|
+
self.skill_manager
|
|
143
|
+
)
|
|
144
|
+
print(" โ Chaining executor")
|
|
145
|
+
|
|
146
|
+
self.parallel_executor = ParallelizationExecutor(
|
|
147
|
+
self.provider_executor,
|
|
148
|
+
self.skill_manager
|
|
149
|
+
)
|
|
150
|
+
print(" โ Parallelization executor")
|
|
151
|
+
|
|
152
|
+
self.orchestrator = OrchestratorExecutor(
|
|
153
|
+
self.provider_executor,
|
|
154
|
+
self.skill_manager
|
|
155
|
+
)
|
|
156
|
+
print(" โ Orchestrator executor")
|
|
157
|
+
|
|
158
|
+
# Initialize memory system
|
|
159
|
+
print("\n๐ง Phase 3: Advanced Memory System")
|
|
160
|
+
|
|
161
|
+
self.episodic_memory = EpisodicMemoryStore(
|
|
162
|
+
str(self.memory_dir / "episodic")
|
|
163
|
+
)
|
|
164
|
+
print(" โ Episodic memory (JSON)")
|
|
165
|
+
|
|
166
|
+
self.semantic_memory = SemanticMemoryStore(
|
|
167
|
+
str(self.memory_dir / "semantic"),
|
|
168
|
+
use_chromadb=self.use_chromadb
|
|
169
|
+
)
|
|
170
|
+
print(f" โ Semantic memory ({'ChromaDB' if self.use_chromadb else 'keyword-based'})")
|
|
171
|
+
|
|
172
|
+
self.working_memory = WorkingMemoryCache()
|
|
173
|
+
print(" โ Working memory (cache)")
|
|
174
|
+
|
|
175
|
+
print("\nโ
TMLPD v2.1 Agent Ready!")
|
|
176
|
+
print(f" Multi-provider: โ")
|
|
177
|
+
print(f" Difficulty-aware routing: โ")
|
|
178
|
+
print(f" Advanced memory: โ")
|
|
179
|
+
print(f" Workflow executors: โ")
|
|
180
|
+
print(f" Skill integration: โ")
|
|
181
|
+
|
|
182
|
+
async def execute(
|
|
183
|
+
self,
|
|
184
|
+
task: Dict[str, Any],
|
|
185
|
+
mode: str = "auto",
|
|
186
|
+
store_memory: bool = True,
|
|
187
|
+
use_memory: bool = True
|
|
188
|
+
) -> Dict[str, Any]:
|
|
189
|
+
"""
|
|
190
|
+
Execute task with full TMLPD capabilities.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
task: Task to execute
|
|
194
|
+
mode: Execution mode ("auto", "orchestrator", "chain", "parallel", "direct")
|
|
195
|
+
store_memory: Store execution in episodic memory
|
|
196
|
+
use_memory: Use past experiences from memory
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
Execution result with metadata
|
|
200
|
+
"""
|
|
201
|
+
if not self.provider_executor:
|
|
202
|
+
raise RuntimeError("Agent not initialized. Call await agent.initialize() first.")
|
|
203
|
+
|
|
204
|
+
start_time = datetime.now()
|
|
205
|
+
|
|
206
|
+
# Update stats
|
|
207
|
+
self.stats["total_executions"] += 1
|
|
208
|
+
|
|
209
|
+
print(f"\n{'='*70}")
|
|
210
|
+
print(f"๐ฏ TMLPD v2.1 Execution #{self.stats['total_executions']}")
|
|
211
|
+
print(f"{'='*70}")
|
|
212
|
+
|
|
213
|
+
# Step 1: Recall relevant experiences (if enabled)
|
|
214
|
+
relevant_episodes = []
|
|
215
|
+
if use_memory:
|
|
216
|
+
print("\n๐ง Recalling relevant experiences...")
|
|
217
|
+
relevant_episodes = self.episodic_memory.recall(
|
|
218
|
+
task,
|
|
219
|
+
top_k=3,
|
|
220
|
+
min_importance=0.3
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
if relevant_episodes:
|
|
224
|
+
print(f" โ Found {len(relevant_episodes)} relevant episodes")
|
|
225
|
+
|
|
226
|
+
# Add to working memory
|
|
227
|
+
self.working_memory.set(
|
|
228
|
+
f"current_task_relevant_episodes",
|
|
229
|
+
relevant_episodes,
|
|
230
|
+
ttl=3600,
|
|
231
|
+
category="memory"
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
# Step 2: Classify difficulty
|
|
235
|
+
print("\n๐ Classifying task difficulty...")
|
|
236
|
+
classification = self.difficulty_classifier.classify_difficulty(task)
|
|
237
|
+
difficulty = classification["difficulty"]
|
|
238
|
+
score = classification["score"]
|
|
239
|
+
|
|
240
|
+
print(f" Difficulty: {difficulty} (score: {score:.1f})")
|
|
241
|
+
print(f" Confidence: {classification['confidence']*100:.0f}%")
|
|
242
|
+
|
|
243
|
+
# Step 3: Select execution mode
|
|
244
|
+
if mode == "auto":
|
|
245
|
+
# Use orchestrator for automatic mode selection
|
|
246
|
+
print(f"\n๐ฎ Using orchestrator mode")
|
|
247
|
+
result = await self.orchestrator.execute(task, strategy="auto")
|
|
248
|
+
|
|
249
|
+
elif mode == "orchestrator":
|
|
250
|
+
print(f"\n๐ฎ Using orchestrator mode")
|
|
251
|
+
result = await self.orchestrator.execute(task, strategy="auto")
|
|
252
|
+
|
|
253
|
+
elif mode == "chain":
|
|
254
|
+
print(f"\n๐ Using chain mode")
|
|
255
|
+
# Decompose and execute sequentially
|
|
256
|
+
result = await self.orchestrator.execute(task, strategy="chain")
|
|
257
|
+
|
|
258
|
+
elif mode == "parallel":
|
|
259
|
+
print(f"\n๐ Using parallel mode")
|
|
260
|
+
# Decompose and execute concurrently
|
|
261
|
+
result = await self.orchestrator.execute(task, strategy="parallel")
|
|
262
|
+
|
|
263
|
+
elif mode == "direct":
|
|
264
|
+
print(f"\nโก Using direct mode")
|
|
265
|
+
# Execute directly without decomposition
|
|
266
|
+
result = await self.orchestrator.execute(task, strategy="direct")
|
|
267
|
+
|
|
268
|
+
else:
|
|
269
|
+
raise ValueError(f"Unknown mode: {mode}")
|
|
270
|
+
|
|
271
|
+
end_time = datetime.now()
|
|
272
|
+
execution_time = (end_time - start_time).total_seconds()
|
|
273
|
+
|
|
274
|
+
# Step 4: Store in episodic memory
|
|
275
|
+
if store_memory and result.get("success"):
|
|
276
|
+
print("\n๐พ Storing execution in episodic memory...")
|
|
277
|
+
|
|
278
|
+
# Calculate importance
|
|
279
|
+
importance = self._calculate_importance(task, result, classification)
|
|
280
|
+
|
|
281
|
+
episode_id = self.episodic_memory.store(
|
|
282
|
+
task=task,
|
|
283
|
+
result={
|
|
284
|
+
"success": result.get("success", False),
|
|
285
|
+
"tokens_used": result.get("tokens_used", 0),
|
|
286
|
+
"cost": result.get("cost", 0.0),
|
|
287
|
+
"execution_time": execution_time
|
|
288
|
+
},
|
|
289
|
+
agent_id="tmlpd_v2.1",
|
|
290
|
+
skills=[],
|
|
291
|
+
provider=result.get("provider", "unknown"),
|
|
292
|
+
model=result.get("model", "unknown"),
|
|
293
|
+
importance=importance,
|
|
294
|
+
metadata={
|
|
295
|
+
"difficulty": difficulty,
|
|
296
|
+
"strategy": result.get("orchestrator_metadata", {}).get("strategy", "unknown"),
|
|
297
|
+
"mode": mode
|
|
298
|
+
}
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
print(f" โ Stored as {episode_id}")
|
|
302
|
+
|
|
303
|
+
# Store in working memory for quick access
|
|
304
|
+
self.working_memory.set(
|
|
305
|
+
f"last_execution",
|
|
306
|
+
{
|
|
307
|
+
"task": task,
|
|
308
|
+
"result": result,
|
|
309
|
+
"episode_id": episode_id
|
|
310
|
+
},
|
|
311
|
+
ttl=7200,
|
|
312
|
+
category="recent_executions"
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
# Step 5: Record outcome for learning (if enabled)
|
|
316
|
+
if self.enable_learning:
|
|
317
|
+
actual_difficulty = self._determine_actual_difficulty(result, execution_time)
|
|
318
|
+
self.difficulty_classifier.record_outcome(
|
|
319
|
+
task=task,
|
|
320
|
+
predicted_difficulty=difficulty,
|
|
321
|
+
actual_difficulty=actual_difficulty,
|
|
322
|
+
execution_time=execution_time,
|
|
323
|
+
success=result.get("success", False)
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
# Update statistics
|
|
327
|
+
if result.get("success"):
|
|
328
|
+
self.stats["successful_executions"] += 1
|
|
329
|
+
|
|
330
|
+
self.stats["total_cost"] += result.get("cost", 0.0)
|
|
331
|
+
self.stats["total_tokens"] += result.get("tokens_used", 0)
|
|
332
|
+
self.stats["total_time"] += execution_time
|
|
333
|
+
|
|
334
|
+
# Print summary
|
|
335
|
+
print(f"\n{'='*70}")
|
|
336
|
+
print(f"๐ Execution Summary")
|
|
337
|
+
print(f"{'='*70}")
|
|
338
|
+
print(f"Status: {'โ
Success' if result.get('success') else 'โ Failed'}")
|
|
339
|
+
print(f"Time: {execution_time:.2f}s")
|
|
340
|
+
print(f"Cost: ${result.get('cost', 0.0):.6f}")
|
|
341
|
+
print(f"Tokens: {result.get('tokens_used', 0)}")
|
|
342
|
+
print(f"Provider: {result.get('provider', 'unknown')}")
|
|
343
|
+
print(f"Model: {result.get('model', 'unknown')}")
|
|
344
|
+
print(f"{'='*70}\n")
|
|
345
|
+
|
|
346
|
+
return result
|
|
347
|
+
|
|
348
|
+
def _calculate_importance(
|
|
349
|
+
self,
|
|
350
|
+
task: Dict[str, Any],
|
|
351
|
+
result: Dict[str, Any],
|
|
352
|
+
classification: Dict[str, Any]
|
|
353
|
+
) -> float:
|
|
354
|
+
"""Calculate importance score for memory storage"""
|
|
355
|
+
importance = 0.5 # Base importance
|
|
356
|
+
|
|
357
|
+
# Higher importance for complex tasks
|
|
358
|
+
difficulty = classification["difficulty"]
|
|
359
|
+
if difficulty == "EXPERT":
|
|
360
|
+
importance += 0.3
|
|
361
|
+
elif difficulty == "COMPLEX":
|
|
362
|
+
importance += 0.2
|
|
363
|
+
elif difficulty == "MEDIUM":
|
|
364
|
+
importance += 0.1
|
|
365
|
+
|
|
366
|
+
# Higher importance for successful executions
|
|
367
|
+
if result.get("success"):
|
|
368
|
+
importance += 0.1
|
|
369
|
+
|
|
370
|
+
# Higher importance for expensive tasks (learn from them)
|
|
371
|
+
cost = result.get("cost", 0.0)
|
|
372
|
+
if cost > 1.0:
|
|
373
|
+
importance += 0.1
|
|
374
|
+
|
|
375
|
+
return min(importance, 1.0)
|
|
376
|
+
|
|
377
|
+
def _determine_actual_difficulty(
|
|
378
|
+
self,
|
|
379
|
+
result: Dict[str, Any],
|
|
380
|
+
execution_time: float
|
|
381
|
+
) -> str:
|
|
382
|
+
"""Determine actual difficulty based on execution metrics"""
|
|
383
|
+
# Simple heuristic based on execution time
|
|
384
|
+
if execution_time < 5:
|
|
385
|
+
return "TRIVIAL"
|
|
386
|
+
elif execution_time < 15:
|
|
387
|
+
return "SIMPLE"
|
|
388
|
+
elif execution_time < 30:
|
|
389
|
+
return "MEDIUM"
|
|
390
|
+
elif execution_time < 60:
|
|
391
|
+
return "COMPLEX"
|
|
392
|
+
else:
|
|
393
|
+
return "EXPERT"
|
|
394
|
+
|
|
395
|
+
def get_stats(self) -> Dict[str, Any]:
|
|
396
|
+
"""Get agent statistics"""
|
|
397
|
+
success_rate = (
|
|
398
|
+
self.stats["successful_executions"] / self.stats["total_executions"]
|
|
399
|
+
if self.stats["total_executions"] > 0 else 0
|
|
400
|
+
)
|
|
401
|
+
|
|
402
|
+
return {
|
|
403
|
+
**self.stats,
|
|
404
|
+
"success_rate": success_rate,
|
|
405
|
+
"avg_cost_per_execution": (
|
|
406
|
+
self.stats["total_cost"] / self.stats["total_executions"]
|
|
407
|
+
if self.stats["total_executions"] > 0 else 0
|
|
408
|
+
),
|
|
409
|
+
"avg_time_per_execution": (
|
|
410
|
+
self.stats["total_time"] / self.stats["total_executions"]
|
|
411
|
+
if self.stats["total_executions"] > 0 else 0
|
|
412
|
+
),
|
|
413
|
+
"episodic_memory_stats": self.episodic_memory.get_stats(),
|
|
414
|
+
"semantic_memory_stats": self.semantic_memory.get_stats(),
|
|
415
|
+
"working_memory_stats": self.working_memory.get_stats(),
|
|
416
|
+
"learning_stats": self.difficulty_classifier.get_learning_stats() if self.enable_learning else {}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async def cleanup(self):
|
|
420
|
+
"""Cleanup and shutdown"""
|
|
421
|
+
print("\n๐งน Cleaning up TMLPD v2.1 Agent...")
|
|
422
|
+
|
|
423
|
+
if self.provider_executor:
|
|
424
|
+
await self.provider_executor.stop()
|
|
425
|
+
|
|
426
|
+
if self.difficulty_agent:
|
|
427
|
+
await self.difficulty_agent.stop()
|
|
428
|
+
|
|
429
|
+
print("โ
Cleanup complete")
|
|
430
|
+
|
|
431
|
+
async def __aenter__(self):
|
|
432
|
+
"""Async context manager entry"""
|
|
433
|
+
await self.initialize()
|
|
434
|
+
return self
|
|
435
|
+
|
|
436
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
437
|
+
"""Async context manager exit"""
|
|
438
|
+
await self.cleanup()
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
# Convenience function for quick usage
|
|
442
|
+
async def execute_task(
|
|
443
|
+
description: str,
|
|
444
|
+
mode: str = "auto",
|
|
445
|
+
**kwargs
|
|
446
|
+
) -> Dict[str, Any]:
|
|
447
|
+
"""
|
|
448
|
+
Quick task execution helper.
|
|
449
|
+
|
|
450
|
+
Usage:
|
|
451
|
+
result = await execute_task("Build a REST API")
|
|
452
|
+
"""
|
|
453
|
+
agent = TMLPDUnifiedAgent()
|
|
454
|
+
|
|
455
|
+
try:
|
|
456
|
+
await agent.initialize()
|
|
457
|
+
result = await agent.execute(
|
|
458
|
+
{"description": description},
|
|
459
|
+
mode=mode,
|
|
460
|
+
**kwargs
|
|
461
|
+
)
|
|
462
|
+
return result
|
|
463
|
+
finally:
|
|
464
|
+
await agent.cleanup()
|