File size: 2,390 Bytes
698b2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Background agent worker that autonomously completes tasks."""

import asyncio
import random
from app.database import SessionLocal
from app.models import Task, TaskStatus, Project, AI_AGENT_USER_ID
from app.tools.memory import complete_task


async def agent_loop():
    """Background loop that processes tasks for agent-enabled projects."""
    print("[Agent] Worker started, waiting for initial delay...")
    # Initial delay to let the server start up
    await asyncio.sleep(10)

    while True:
        try:
            await process_one_task()
        except Exception as e:
            print(f"[Agent] Error: {e}")

        # Wait 25-35 seconds (jitter to avoid rate limit patterns)
        wait_time = random.uniform(25, 35)
        await asyncio.sleep(wait_time)


async def process_one_task():
    """Pick a random todo task from agent-enabled projects and complete it."""
    db = SessionLocal()
    try:
        # Find projects with agent enabled
        projects = db.query(Project).filter(Project.agent_enabled == True).all()
        if not projects:
            return

        # Pick random project
        project = random.choice(projects)

        # Find a random todo task
        todo_tasks = db.query(Task).filter(
            Task.project_id == project.id,
            Task.status == TaskStatus.todo
        ).all()

        if not todo_tasks:
            return

        task = random.choice(todo_tasks)
        print(f"[Agent] Starting task: {task.title}")

        # Step 1: Move to in_progress
        task.status = TaskStatus.in_progress
        task.working_by = AI_AGENT_USER_ID
        db.commit()

        # Step 2: "Work" on it (small delay for realism)
        await asyncio.sleep(random.uniform(2, 5))

        # Step 3: Complete the task
        result = await complete_task(
            task_id=task.id,
            project_id=project.id,
            user_id=AI_AGENT_USER_ID,
            what_i_did=f"Implemented {task.title}. {task.description or 'Completed the task as specified.'}",
            actor_type="agent"
        )

        if result.get("success"):
            print(f"[Agent] Completed task: {task.title}")
        else:
            print(f"[Agent] Failed to complete task: {result.get('error')}")

    except Exception as e:
        print(f"[Agent] Error in process_one_task: {e}")
        db.rollback()
    finally:
        db.close()