MCPcopy
hub / github.com/dataelement/Clawith / execute_task

Function execute_task

backend/app/services/task_executor.py:24–146  ·  view source on GitHub ↗

Execute a task using the agent's configured LLM with full context. Uses the same context as chat dialog: build_agent_context for system prompt, agent tools for tool-calling, and a multi-round tool loop. Flow: - todo tasks: pending → doing → done - supervision tasks: pending

(task_id: uuid.UUID, agent_id: uuid.UUID)

Source from the content-addressed store, hash-verified

22
23
24async def execute_task(task_id: uuid.UUID, agent_id: uuid.UUID) -> None:
25 """Execute a task using the agent's configured LLM with full context.
26
27 Uses the same context as chat dialog: build_agent_context for system prompt,
28 agent tools for tool-calling, and a multi-round tool loop.
29
30 Flow:
31 - todo tasks: pending → doing → done
32 - supervision tasks: pending → doing → pending (stays active, just logs result)
33 """
34 logger.info(f"[TaskExec] Starting task {task_id} for agent {agent_id}")
35
36 # Step 1: Mark as doing
37 async with async_session() as db:
38 result = await db.execute(select(Task).where(Task.id == task_id))
39 task = result.scalar_one_or_none()
40 if not task:
41 logger.warning(f"[TaskExec] Task {task_id} not found")
42 return
43
44 task.status = "doing"
45 db.add(TaskLog(task_id=task_id, content="🤖 开始执行任务..."))
46 await db.commit()
47 task_title = task.title
48 task_description = task.description or ""
49 task_type = task.type # 'todo' or 'supervision'
50 supervision_target = task.supervision_target_name or ""
51
52 # Step 2: Load agent
53 async with async_session() as db:
54 agent_result = await db.execute(select(Agent).where(Agent.id == agent_id))
55 agent = agent_result.scalar_one_or_none()
56 if not agent:
57 await _log_error(task_id, "数字员工未找到")
58 if task_type == 'supervision':
59 await _restore_supervision_status(task_id)
60 return
61 agent_name = agent.name
62
63 # Step 3: Build full agent context (same as chat dialog)
64 from app.services.agent_context import build_agent_context
65 static_prompt, dynamic_prompt = await build_agent_context(agent_id, agent_name, agent.role_description or "")
66
67 # Add task-execution-specific instructions
68 task_addendum = """
69
70## Task Execution Mode
71
72You are now in TASK EXECUTION MODE (not a conversation). A task has been assigned to you.
73- Focus on completing the task as thoroughly as possible.
74- Break down complex tasks into steps and execute each step.
75- Use your tools actively to gather information, send messages, read/write files, etc.
76- Provide a detailed execution report at the end.
77- If the task involves contacting someone, use `send_feishu_message` to reach them.
78- If the task requires data or information, use your tools to fetch it.
79- Do NOT ask the user follow-up questions — take initiative and complete the task autonomously.
80"""
81 dynamic_prompt += task_addendum

Callers 5

_manage_tasksFunction · 0.90
process_feishu_eventFunction · 0.90
create_taskFunction · 0.90
trigger_taskFunction · 0.90
_create_task_recordMethod · 0.90

Calls 11

TaskLogClass · 0.90
build_agent_contextFunction · 0.90
log_activityFunction · 0.90
_log_errorFunction · 0.85
whereMethod · 0.80
executeMethod · 0.45
scalar_one_or_noneMethod · 0.45
addMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected