-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversal_example.py
More file actions
1202 lines (983 loc) · 42.5 KB
/
universal_example.py
File metadata and controls
1202 lines (983 loc) · 42.5 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Universal Long-Running Agent Example
===================================
This example demonstrates the agent-agnostic long-running agent that works with
any AI agent framework (Cursor, OpenCode, Claude, etc.).
The agent can:
1. Parse PRD/specifications into structured task lists
2. Execute tasks autonomously with dependency management
3. Manage API rotation with intelligent load balancing and rate limiting
4. Persist state across sessions using standard JSON files
5. Learn from execution patterns for continuous improvement
Setup with uv (recommended):
# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create virtual environment (optional - no external dependencies)
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Run the example
uv run python universal_example.py
Traditional usage:
python universal_example.py
Dependencies:
None! This example uses only Python standard library for maximum compatibility.
"""
import json
import re
import os
import threading
import concurrent.futures
import time
import random
from datetime import datetime
from enum import Enum
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, field
class TaskStatus(Enum):
PENDING = "pending"
IN_PROGRESS = "in_progress"
COMPLETED = "completed"
FAILED = "failed"
BLOCKED = "blocked"
class APIStatus(Enum):
ACTIVE = "active"
RATE_LIMITED = "rate_limited"
QUOTA_EXCEEDED = "quota_exceeded"
ERROR = "error"
DISABLED = "disabled"
@dataclass
class APIEndpoint:
"""Represents an API endpoint with rotation capabilities."""
name: str
base_url: str
api_key: str
rate_limit: int = 60 # requests per minute
quota_limit: int = 1000 # requests per day
current_usage: int = 0
daily_usage: int = 0
last_request_time: float = 0
status: APIStatus = APIStatus.ACTIVE
error_count: int = 0
last_reset_time: float = field(default_factory=time.time)
def can_make_request(self) -> bool:
"""Check if endpoint can handle a request."""
if self.status != APIStatus.ACTIVE:
return False
current_time = time.time()
# Reset counters if minute has passed
if current_time - self.last_request_time >= 60:
self.current_usage = 0
self.last_request_time = current_time
# Reset daily usage if day has passed
if current_time - self.last_reset_time >= 86400: # 24 hours
self.daily_usage = 0
self.last_reset_time = current_time
# Check limits
return (self.current_usage < self.rate_limit and
self.daily_usage < self.quota_limit)
def record_request(self, success: bool = True):
"""Record a request and update usage counters."""
self.current_usage += 1
self.daily_usage += 1
self.last_request_time = time.time()
if not success:
self.error_count += 1
else:
self.error_count = max(0, self.error_count - 1) # Decay errors on success
class APIRotationManager:
"""Manages multiple API endpoints with intelligent rotation."""
def __init__(self):
self.endpoints: List[APIEndpoint] = []
self.current_index = 0
self.lock = threading.Lock()
self.usage_stats = {
"total_requests": 0,
"successful_requests": 0,
"failed_requests": 0,
"rotations": 0,
"rate_limit_hits": 0,
"quota_exceeded_count": 0
}
def add_endpoint(self, name: str, base_url: str, api_key: str,
rate_limit: int = 60, quota_limit: int = 1000):
"""Add an API endpoint to the rotation pool."""
endpoint = APIEndpoint(
name=name,
base_url=base_url,
api_key=api_key,
rate_limit=rate_limit,
quota_limit=quota_limit
)
self.endpoints.append(endpoint)
print(f"✅ Added API endpoint: {name} (rate: {rate_limit}/min, quota: {quota_limit}/day)")
def get_best_endpoint(self) -> Optional[APIEndpoint]:
"""Get the best available endpoint for making a request."""
with self.lock:
if not self.endpoints:
return None
# Find available endpoints
available_endpoints = [ep for ep in self.endpoints if ep.can_make_request()]
if not available_endpoints:
print("⚠️ All API endpoints are rate limited or at quota")
return None
# Select endpoint using weighted selection based on remaining capacity
return self._select_optimal_endpoint(available_endpoints)
def _select_optimal_endpoint(self, available_endpoints: List[APIEndpoint]) -> APIEndpoint:
"""Select optimal endpoint based on capacity and performance."""
# Calculate weights based on remaining capacity and error rate
weighted_endpoints = []
for endpoint in available_endpoints:
# Calculate remaining capacity (0-1 scale)
rate_capacity = (endpoint.rate_limit - endpoint.current_usage) / endpoint.rate_limit
quota_capacity = (endpoint.quota_limit - endpoint.daily_usage) / endpoint.quota_limit
# Calculate error rate (lower is better)
error_rate = min(endpoint.error_count / 10.0, 1.0) # Cap at 1.0
# Combined weight (higher is better)
weight = (rate_capacity * 0.4 + quota_capacity * 0.4 + (1 - error_rate) * 0.2)
weighted_endpoints.append((endpoint, weight))
# Sort by weight (highest first)
weighted_endpoints.sort(key=lambda x: x[1], reverse=True)
# Use weighted random selection for the top 3 endpoints
top_endpoints = weighted_endpoints[:3]
weights = [w for _, w in top_endpoints]
if not weights or sum(weights) == 0:
return available_endpoints[0]
# Weighted random selection
total_weight = sum(weights)
rand_val = random.uniform(0, total_weight)
current_weight = 0
for (endpoint, weight) in top_endpoints:
current_weight += weight
if rand_val <= current_weight:
return endpoint
return top_endpoints[0][0] # Fallback
def simulate_api_request(self, endpoint: APIEndpoint, request_type: str) -> Dict:
"""Simulate an API request for demonstration purposes."""
# Record the request
endpoint.record_request(success=True)
self.usage_stats["total_requests"] += 1
self.usage_stats["successful_requests"] += 1
# Simulate response based on request type
if request_type == "completion":
return {
"success": True,
"data": {
"choices": [{"text": f"Generated response from {endpoint.name}"}],
"usage": {"tokens": 150}
},
"endpoint_used": endpoint.name
}
elif request_type == "embedding":
return {
"success": True,
"data": {
"embeddings": [[0.1, 0.2, 0.3] * 100], # Mock embedding
"usage": {"tokens": 50}
},
"endpoint_used": endpoint.name
}
else:
return {
"success": True,
"data": {"result": f"API response from {endpoint.name}"},
"endpoint_used": endpoint.name
}
def make_api_request(self, request_type: str = "completion") -> Dict:
"""Make an API request with automatic rotation."""
endpoint = self.get_best_endpoint()
if not endpoint:
return {
"success": False,
"error": "No API endpoints available",
"usage_stats": self._get_usage_summary()
}
print(f"🔄 Using endpoint: {endpoint.name} (usage: {endpoint.current_usage}/{endpoint.rate_limit})")
# Simulate the API request
result = self.simulate_api_request(endpoint, request_type)
return result
def _get_usage_summary(self) -> Dict:
"""Get current usage summary across all endpoints."""
return {
"total_requests": self.usage_stats["total_requests"],
"success_rate": (self.usage_stats["successful_requests"] /
max(1, self.usage_stats["total_requests"]) * 100),
"active_endpoints": len([ep for ep in self.endpoints if ep.status == APIStatus.ACTIVE]),
"total_endpoints": len(self.endpoints)
}
def get_status_report(self) -> Dict:
"""Get comprehensive status report of all endpoints."""
report = {
"timestamp": datetime.now().isoformat(),
"overall_stats": self.usage_stats.copy(),
"endpoints": []
}
for endpoint in self.endpoints:
endpoint_info = {
"name": endpoint.name,
"status": endpoint.status.value,
"rate_usage": f"{endpoint.current_usage}/{endpoint.rate_limit}",
"quota_usage": f"{endpoint.daily_usage}/{endpoint.quota_limit}",
"error_count": endpoint.error_count,
"can_make_request": endpoint.can_make_request()
}
report["endpoints"].append(endpoint_info)
return report
# Global API rotation manager instance
api_manager = APIRotationManager()
# ============================================================================
# CORE FUNCTIONS (Agent-Agnostic)
# ============================================================================
def parse_prd_to_tasks(prd_content: str, project_name: str = "Generated Project") -> Dict:
"""Parse PRD/specification content into structured task list."""
tasks = []
task_id_counter = 1
# Split content into sections by headers
sections = re.split(r'\\n(?=#{2,3}\\s)', prd_content)
for section in sections:
if not section.strip():
continue
lines = section.strip().split('\\n')
if not lines:
continue
# Extract title from header
header_match = re.match(r'^#{2,3}\\s+(.+)', lines[0])
if not header_match:
continue
title = header_match.group(1).strip()
description_lines = lines[1:] if len(lines) > 1 else []
# Extract acceptance criteria (bullet points)
acceptance_criteria = []
remaining_description = []
for line in description_lines:
line = line.strip()
if re.match(r'^[-*]\\s+|^\\d+\\.\\s+', line):
criteria = re.sub(r'^[-*]\\s+|^\\d+\\.\\s+', '', line)
acceptance_criteria.append(criteria)
else:
remaining_description.append(line)
# Determine task properties
content_lower = (title + ' ' + ' '.join(description_lines)).lower()
# Priority detection
priority = "medium"
if any(word in content_lower for word in ['critical', 'urgent', 'high priority', 'must have']):
priority = "high"
elif any(word in content_lower for word in ['nice to have', 'optional', 'low priority', 'future']):
priority = "low"
# Effort estimation
effort = "medium"
if any(word in content_lower for word in ['simple', 'basic', 'quick', 'small']):
effort = "small"
elif any(word in content_lower for word in ['complex', 'advanced', 'large', 'system', 'integration']):
effort = "large"
# Category classification
category = categorize_task(title, ' '.join(description_lines))
task = {
"id": f"task_{task_id_counter:03d}",
"title": title,
"description": '\\n'.join(remaining_description).strip() or title,
"category": category,
"priority": priority,
"estimated_effort": effort,
"acceptance_criteria": acceptance_criteria,
"dependencies": [],
"status": TaskStatus.PENDING.value,
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat(),
"result": "",
"progress_notes": "",
"error_details": ""
}
tasks.append(task)
task_id_counter += 1
# Analyze and set dependencies
tasks = analyze_task_dependencies(tasks)
return {
"project_name": project_name,
"created_at": datetime.now().isoformat(),
"updated_at": datetime.now().isoformat(),
"total_tasks": len(tasks),
"completed_tasks": 0,
"in_progress_tasks": 0,
"pending_tasks": len(tasks),
"failed_tasks": 0,
"tasks": tasks
}
def categorize_task(title: str, description: str) -> str:
"""Categorize task based on content keywords."""
content = (title + ' ' + description).lower()
if any(word in content for word in ['ui', 'interface', 'frontend', 'component', 'page']):
return 'frontend'
elif any(word in content for word in ['api', 'backend', 'server', 'service', 'endpoint']):
return 'backend'
elif any(word in content for word in ['database', 'schema', 'migration', 'model', 'data']):
return 'database'
elif any(word in content for word in ['auth', 'login', 'security', 'authentication', 'authorization']):
return 'authentication'
elif any(word in content for word in ['test', 'testing', 'validation', 'verify', 'qa']):
return 'testing'
elif any(word in content for word in ['deploy', 'deployment', 'infrastructure', 'devops', 'ci/cd']):
return 'deployment'
elif any(word in content for word in ['document', 'docs', 'readme', 'guide', 'manual']):
return 'documentation'
else:
return 'general'
def analyze_task_dependencies(tasks: List[Dict]) -> List[Dict]:
"""Analyze and set task dependencies based on categories and content."""
dependency_rules = [
('authentication', 'frontend'),
('backend', 'frontend'),
('database', 'backend'),
('database', 'authentication'),
]
for i, task in enumerate(tasks):
dependencies = []
for j, other_task in enumerate(tasks[:i]):
for prereq_category, dependent_category in dependency_rules:
if (prereq_category in other_task['category'] and
dependent_category in task['category']):
dependencies.append(other_task['id'])
task['dependencies'] = list(set(dependencies))
return tasks
# ============================================================================
# STATE MANAGEMENT
# ============================================================================
def save_task_list(task_list: Dict, file_path: str = "tasks/current_tasks.json") -> bool:
"""Save task list to persistent storage."""
try:
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w') as f:
json.dump(task_list, f, indent=2)
return True
except Exception as e:
print(f"Error saving task list: {e}")
return False
def load_task_list(file_path: str = "tasks/current_tasks.json") -> Dict:
"""Load task list from persistent storage."""
try:
with open(file_path, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {"tasks": [], "total_tasks": 0, "completed_tasks": 0}
except Exception as e:
print(f"Error loading task list: {e}")
return {"tasks": [], "total_tasks": 0, "completed_tasks": 0}
def update_task_status(task_id: str, status: str, result: str = "",
progress_notes: str = "", error_details: str = "") -> bool:
"""Update task status and save changes."""
try:
task_list = load_task_list()
for task in task_list.get("tasks", []):
if task["id"] == task_id:
old_status = task["status"]
task["status"] = status
task["updated_at"] = datetime.now().isoformat()
if result:
task["result"] = result
if progress_notes:
task["progress_notes"] = progress_notes
if error_details:
task["error_details"] = error_details
update_task_counters(task_list, old_status, status)
break
return save_task_list(task_list)
except Exception as e:
print(f"Error updating task status: {e}")
return False
def update_task_counters(task_list: Dict, old_status: str, new_status: str):
"""Update task counters when status changes."""
# Initialize counters if missing
for counter in ["pending_tasks", "in_progress_tasks", "completed_tasks", "failed_tasks"]:
if counter not in task_list:
task_list[counter] = 0
# Decrement old status counter
if old_status == "pending":
task_list["pending_tasks"] = max(0, task_list["pending_tasks"] - 1)
elif old_status == "in_progress":
task_list["in_progress_tasks"] = max(0, task_list["in_progress_tasks"] - 1)
elif old_status == "completed":
task_list["completed_tasks"] = max(0, task_list["completed_tasks"] - 1)
elif old_status == "failed":
task_list["failed_tasks"] = max(0, task_list["failed_tasks"] - 1)
# Increment new status counter
if new_status == "pending":
task_list["pending_tasks"] += 1
elif new_status == "in_progress":
task_list["in_progress_tasks"] += 1
elif new_status == "completed":
task_list["completed_tasks"] += 1
elif new_status == "failed":
task_list["failed_tasks"] += 1
# ============================================================================
# TASK EXECUTION ENGINE
# ============================================================================
def get_executable_tasks() -> List[Dict]:
"""Get list of tasks that can be executed."""
task_list = load_task_list()
executable_tasks = []
for task in task_list.get("tasks", []):
if task["status"] != "pending":
continue
# Check if all dependencies are completed
dependencies_met = True
for dep_id in task.get("dependencies", []):
dep_task = find_task_by_id(task_list, dep_id)
if not dep_task or dep_task["status"] != "completed":
dependencies_met = False
break
if dependencies_met:
executable_tasks.append(task)
# Sort by priority then by creation time
priority_order = {"high": 3, "medium": 2, "low": 1}
executable_tasks.sort(
key=lambda t: (priority_order.get(t.get("priority", "medium"), 2), t.get("created_at", "")),
reverse=True
)
return executable_tasks
def find_task_by_id(task_list: Dict, task_id: str) -> Dict:
"""Find a task by its ID."""
for task in task_list.get("tasks", []):
if task["id"] == task_id:
return task
return None
def execute_next_task() -> Dict:
"""Find and execute the next available task."""
executable_tasks = get_executable_tasks()
if not executable_tasks:
return {"success": False, "message": "No executable tasks available"}
task = executable_tasks[0]
task_id = task["id"]
# Update status to in_progress
if not update_task_status(task_id, "in_progress"):
return {"success": False, "message": f"Failed to update task {task_id} status"}
try:
# Execute task based on category
result = execute_task_by_category(task)
# Update with results
if result["success"]:
update_task_status(task_id, "completed", result["output"], result.get("notes", ""))
return {"success": True, "message": f"Task {task_id} ({task['title']}) completed", "result": result}
else:
update_task_status(task_id, "failed", "", "", result.get("error", "Unknown error"))
return {"success": False, "message": f"Task {task_id} failed", "error": result.get("error")}
except Exception as e:
update_task_status(task_id, "failed", "", "", str(e))
return {"success": False, "message": f"Task {task_id} failed with exception", "error": str(e)}
def execute_task_by_category(task: Dict) -> Dict:
"""Execute a task based on its category."""
category = task.get("category", "general")
task_id = task["id"]
# Create results directory
results_dir = f"results/{task_id}"
os.makedirs(results_dir, exist_ok=True)
# Category-specific implementation
if category == "frontend":
return implement_frontend_task(task, results_dir)
elif category == "backend":
return implement_backend_task(task, results_dir)
elif category == "database":
return implement_database_task(task, results_dir)
elif category == "authentication":
return implement_auth_task(task, results_dir)
elif category == "testing":
return implement_testing_task(task, results_dir)
elif category == "documentation":
return implement_documentation_task(task, results_dir)
else:
return implement_general_task(task, results_dir)
def implement_frontend_task(task: Dict, results_dir: str) -> Dict:
"""Implement frontend-specific tasks."""
# Create frontend structure
os.makedirs(f"{results_dir}/components", exist_ok=True)
os.makedirs(f"{results_dir}/styles", exist_ok=True)
os.makedirs(f"{results_dir}/tests", exist_ok=True)
# Generate component implementation
component_name = task["title"].replace(" ", "")
component_code = f"""
import React from 'react';
import './{component_name}.css';
interface {component_name}Props {{
// Define props based on task requirements
}}
const {component_name}: React.FC<{component_name}Props> = (props) => {{
return (
<div className="{component_name.lower()}">
<h2>{task["title"]}</h2>
<p>{task["description"]}</p>
{/* Implementation based on acceptance criteria */}
</div>
);
}};
export default {component_name};
"""
# Save component file
with open(f"{results_dir}/components/{component_name}.tsx", "w") as f:
f.write(component_code)
# Generate CSS
css_code = f"""
.{component_name.lower()} {{
/* Styles for {task["title"]} */
padding: 1rem;
border: 1px solid #ddd;
border-radius: 4px;
}}
.{component_name.lower()} h2 {{
margin-top: 0;
color: #333;
}}
"""
with open(f"{results_dir}/styles/{component_name}.css", "w") as f:
f.write(css_code)
# Generate test file
test_code = f"""
import {{ render, screen }} from '@testing-library/react';
import {component_name} from '../components/{component_name}';
describe('{component_name}', () => {{
test('renders component title', () => {{
render(<{component_name} />);
expect(screen.getByText('{task["title"]}')).toBeInTheDocument();
}});
// Add more tests based on acceptance criteria
}});
"""
with open(f"{results_dir}/tests/{component_name}.test.tsx", "w") as f:
f.write(test_code)
# Create README
readme_content = create_task_readme(task, "Frontend Component", [
f"components/{component_name}.tsx - Main component implementation",
f"styles/{component_name}.css - Component styles",
f"tests/{component_name}.test.tsx - Unit tests"
])
with open(f"{results_dir}/README.md", "w") as f:
f.write(readme_content)
return {
"success": True,
"output": f"Frontend component '{component_name}' implemented successfully",
"notes": f"Created React component with styles and tests in {results_dir}",
"files_created": [
f"components/{component_name}.tsx",
f"styles/{component_name}.css",
f"tests/{component_name}.test.tsx",
"README.md"
]
}
def implement_backend_task(task: Dict, results_dir: str) -> Dict:
"""Implement backend-specific tasks."""
# Create backend structure
os.makedirs(f"{results_dir}/routes", exist_ok=True)
os.makedirs(f"{results_dir}/controllers", exist_ok=True)
os.makedirs(f"{results_dir}/services", exist_ok=True)
os.makedirs(f"{results_dir}/tests", exist_ok=True)
# Generate API implementation
endpoint_name = task["title"].lower().replace(" ", "_")
# Route definition
route_code = f"""
from flask import Blueprint, request, jsonify
from .controllers.{endpoint_name}_controller import {endpoint_name.title().replace('_', '')}Controller
{endpoint_name}_bp = Blueprint('{endpoint_name}', __name__)
controller = {endpoint_name.title().replace('_', '')}Controller()
@{endpoint_name}_bp.route('/{endpoint_name}', methods=['GET'])
def get_{endpoint_name}():
\"\"\"
{task["description"]}
\"\"\"
try:
result = controller.get_{endpoint_name}()
return jsonify(result), 200
except Exception as e:
return jsonify({{"error": str(e)}}), 500
@{endpoint_name}_bp.route('/{endpoint_name}', methods=['POST'])
def create_{endpoint_name}():
\"\"\"
Create new {endpoint_name}
\"\"\"
try:
data = request.get_json()
result = controller.create_{endpoint_name}(data)
return jsonify(result), 201
except Exception as e:
return jsonify({{"error": str(e)}}), 400
"""
with open(f"{results_dir}/routes/{endpoint_name}_routes.py", "w") as f:
f.write(route_code)
# Controller implementation
controller_code = f"""
from ..services.{endpoint_name}_service import {endpoint_name.title().replace('_', '')}Service
class {endpoint_name.title().replace('_', '')}Controller:
def __init__(self):
self.service = {endpoint_name.title().replace('_', '')}Service()
def get_{endpoint_name}(self):
\"\"\"
{task["description"]}
\"\"\"
return self.service.get_all()
def create_{endpoint_name}(self, data):
\"\"\"
Create new {endpoint_name}
\"\"\"
# Validate input data
if not data:
raise ValueError("No data provided")
return self.service.create(data)
"""
with open(f"{results_dir}/controllers/{endpoint_name}_controller.py", "w") as f:
f.write(controller_code)
# Service implementation
service_code = f"""
class {endpoint_name.title().replace('_', '')}Service:
def __init__(self):
# Initialize database connection or other dependencies
pass
def get_all(self):
\"\"\"
Retrieve all {endpoint_name} records
\"\"\"
# Implementation based on task requirements
return []
def create(self, data):
\"\"\"
Create new {endpoint_name} record
\"\"\"
# Validate and create record
# Implementation based on acceptance criteria
return {{"id": 1, "message": "{endpoint_name} created successfully"}}
"""
with open(f"{results_dir}/services/{endpoint_name}_service.py", "w") as f:
f.write(service_code)
# Generate tests
test_code = f"""
import pytest
from unittest.mock import Mock
from ..controllers.{endpoint_name}_controller import {endpoint_name.title().replace('_', '')}Controller
class Test{endpoint_name.title().replace('_', '')}Controller:
def setup_method(self):
self.controller = {endpoint_name.title().replace('_', '')}Controller()
def test_get_{endpoint_name}(self):
result = self.controller.get_{endpoint_name}()
assert isinstance(result, list)
def test_create_{endpoint_name}(self):
test_data = {{"name": "test"}}
result = self.controller.create_{endpoint_name}(test_data)
assert "id" in result
assert result["message"] == "{endpoint_name} created successfully"
"""
with open(f"{results_dir}/tests/test_{endpoint_name}.py", "w") as f:
f.write(test_code)
# Create README
readme_content = create_task_readme(task, "Backend API", [
f"routes/{endpoint_name}_routes.py - API route definitions",
f"controllers/{endpoint_name}_controller.py - Request handling logic",
f"services/{endpoint_name}_service.py - Business logic implementation",
f"tests/test_{endpoint_name}.py - Unit tests"
])
with open(f"{results_dir}/README.md", "w") as f:
f.write(readme_content)
return {
"success": True,
"output": f"Backend API for '{task['title']}' implemented successfully",
"notes": f"Created REST API with routes, controllers, services, and tests in {results_dir}",
"files_created": [
f"routes/{endpoint_name}_routes.py",
f"controllers/{endpoint_name}_controller.py",
f"services/{endpoint_name}_service.py",
f"tests/test_{endpoint_name}.py",
"README.md"
]
}
def implement_general_task(task: Dict, results_dir: str) -> Dict:
"""Implement general tasks."""
readme_content = create_task_readme(task, "General Implementation", [
"README.md - Task implementation documentation"
])
with open(f"{results_dir}/README.md", "w") as f:
f.write(readme_content)
return {
"success": True,
"output": f"General task '{task['title']}' documented and structured",
"notes": f"Created implementation structure and documentation in {results_dir}",
"files_created": ["README.md"]
}
# Placeholder implementations for other categories
def implement_database_task(task: Dict, results_dir: str) -> Dict:
return implement_general_task(task, results_dir)
def implement_auth_task(task: Dict, results_dir: str) -> Dict:
return implement_general_task(task, results_dir)
def implement_testing_task(task: Dict, results_dir: str) -> Dict:
return implement_general_task(task, results_dir)
def implement_documentation_task(task: Dict, results_dir: str) -> Dict:
return implement_general_task(task, results_dir)
def create_task_readme(task: Dict, implementation_type: str, files_created: List[str]) -> str:
"""Create README content for task implementation."""
criteria_list = "\\n".join(f"- {criteria}" for criteria in task.get("acceptance_criteria", []))
files_list = "\\n".join(f"- {file}" for file in files_created)
return f"""# {task["title"]} - {implementation_type}
## Task Information
- **ID**: {task["id"]}
- **Category**: {task.get("category", "general")}
- **Priority**: {task.get("priority", "medium")}
- **Estimated Effort**: {task.get("estimated_effort", "medium")}
## Description
{task["description"]}
## Acceptance Criteria
{criteria_list if criteria_list else "- No specific criteria provided"}
## Implementation Details
- **Implementation Type**: {implementation_type}
- **Status**: {task["status"]}
- **Created**: {task.get("created_at", "Unknown")}
- **Updated**: {task.get("updated_at", "Unknown")}
## Files Created
{files_list}
## Dependencies
{", ".join(task.get("dependencies", [])) if task.get("dependencies") else "None"}
## Results
{task.get("result", "Implementation completed successfully")}
## Progress Notes
{task.get("progress_notes", "Task completed as specified")}
---
*Generated by Long-Running Agent - Universal Example*
"""
# ============================================================================
# PROGRESS REPORTING
# ============================================================================
def get_progress_report() -> Dict:
"""Generate comprehensive progress report."""
task_list = load_task_list()
tasks = task_list.get("tasks", [])
# Calculate statistics
total_tasks = len(tasks)
completed_tasks = len([t for t in tasks if t["status"] == "completed"])
in_progress_tasks = len([t for t in tasks if t["status"] == "in_progress"])
failed_tasks = len([t for t in tasks if t["status"] == "failed"])
pending_tasks = len([t for t in tasks if t["status"] == "pending"])
completion_percentage = (completed_tasks / total_tasks * 100) if total_tasks > 0 else 0
# Group by category
category_stats = {}
for task in tasks:
category = task.get("category", "general")
if category not in category_stats:
category_stats[category] = {"total": 0, "completed": 0, "failed": 0}
category_stats[category]["total"] += 1
if task["status"] == "completed":
category_stats[category]["completed"] += 1
elif task["status"] == "failed":
category_stats[category]["failed"] += 1
# Find next executable tasks
executable_tasks = get_executable_tasks()
next_tasks = [{"id": t["id"], "title": t["title"], "category": t["category"]}
for t in executable_tasks[:5]]
report = {
"project_name": task_list.get("project_name", "Unknown Project"),
"generated_at": datetime.now().isoformat(),
"overall_progress": {
"total_tasks": total_tasks,
"completed_tasks": completed_tasks,
"in_progress_tasks": in_progress_tasks,
"failed_tasks": failed_tasks,
"pending_tasks": pending_tasks,
"completion_percentage": round(completion_percentage, 2)
},
"category_breakdown": category_stats,
"next_executable_tasks": next_tasks,
"blocked_tasks": [
{"id": t["id"], "title": t["title"], "dependencies": t.get("dependencies", [])}
for t in tasks if t["status"] == "pending" and t not in executable_tasks
]
}
# Save report
os.makedirs("results", exist_ok=True)
with open("results/progress_report.json", "w") as f:
json.dump(report, f, indent=2)
return report
# ============================================================================
# MAIN WORKFLOW FUNCTION
# ============================================================================
def universal_long_running_agent_workflow(prd_content: str, project_name: str = "Universal Agent Project"):
"""
Universal workflow that works with any AI agent.
This function demonstrates the complete PRD-to-implementation workflow
using only standard Python and JSON files for persistence.
"""
print(f"🚀 Starting Universal Long-Running Agent Workflow")
print(f"📋 Project: {project_name}")
print(f"🕐 Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
# Step 1: Setup project structure
print("📁 Setting up project structure...")
directories = ["tasks", "results", "memories", "logs"]
for directory in directories:
os.makedirs(directory, exist_ok=True)
print("✅ Project structure created")
# Step 2: Parse PRD into tasks
print("\\n📋 Parsing PRD into structured tasks...")
task_list = parse_prd_to_tasks(prd_content, project_name)
if not save_task_list(task_list):
print("❌ Failed to save task list")