Files
beaver_project/app-instance/backend/beaver/tasks/service.py

270 lines
9.4 KiB
Python

"""Internal service for automatic Task mode."""
from __future__ import annotations
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from uuid import uuid4
from .models import TaskEvent, TaskRecord, ValidationResult
from .store import TaskStore
class TaskService:
def __init__(self, root: str | Path) -> None:
self.store = TaskStore(root)
def create_task(
self,
*,
session_id: str,
description: str,
creator: str = "main-agent",
metadata: dict[str, Any] | None = None,
) -> TaskRecord:
now = self._now()
task_metadata = dict(metadata or {})
task_metadata.setdefault("short_title", short_task_title(description))
task = TaskRecord(
task_id=uuid4().hex,
session_id=session_id,
description=description,
goal=description,
constraints=[],
priority=0,
status="open",
creator=creator,
created_at=now,
updated_at=now,
metadata=task_metadata,
)
self.store.upsert_task(task)
self._event(task, "created", payload={"description": description})
return task
def get_task(self, task_id: str) -> TaskRecord | None:
return self.store.get_task(task_id)
def list_tasks(self) -> list[TaskRecord]:
return sorted(self.store.list_tasks(), key=lambda item: item.updated_at, reverse=True)
def list_events(self, task_id: str) -> list[TaskEvent]:
return self.store.list_events(task_id=task_id)
def get_task_by_run_id(self, run_id: str) -> TaskRecord | None:
return self.store.get_task_by_run_id(run_id)
def get_latest_open_task(self, session_id: str, *, include_unengaged_scheduled: bool = False) -> TaskRecord | None:
tasks = [
task
for task in self.store.list_tasks()
if task.session_id == session_id and task.is_open
]
if not include_unengaged_scheduled:
tasks = [task for task in tasks if self._is_user_visible_active_task(task)]
if not tasks:
return None
return sorted(tasks, key=lambda item: item.updated_at)[-1]
def active_task_view(self, session_id: str) -> dict[str, Any] | None:
task = self.get_latest_open_task(session_id)
if task is None:
return None
return self.to_api_dict(task)
def to_api_dict(self, task: TaskRecord) -> dict[str, Any]:
payload = task.to_dict()
payload["short_title"] = self.ensure_short_title(task).metadata.get("short_title")
payload["is_open"] = task.is_open
payload["is_execution_active"] = task.is_execution_active
payload["requires_user_action"] = task.requires_user_action
return payload
def ensure_short_title(self, task: TaskRecord) -> TaskRecord:
if task.metadata.get("short_title"):
return task
task.metadata["short_title"] = short_task_title(task.description or task.goal or task.task_id)
self.store.upsert_task(task)
return task
def start_run(self, task_id: str, *, user_message: str, attempt_index: int) -> TaskRecord:
task = self._require(task_id)
task.status = "running"
task.updated_at = self._now()
task.metadata["latest_user_message"] = user_message
task.metadata["latest_attempt_index"] = attempt_index
self.store.upsert_task(task)
self._event(task, "run_started", payload={"user_message": user_message, "attempt_index": attempt_index})
return task
def append_run(self, task_id: str, run_id: str, *, skill_names: list[str] | None = None) -> TaskRecord:
task = self._require(task_id)
if run_id not in task.run_ids:
task.run_ids.append(run_id)
for name in skill_names or []:
if name not in task.skill_names:
task.skill_names.append(name)
task.updated_at = self._now()
self.store.upsert_task(task)
self._event(task, "run_completed", run_id=run_id, payload={"skill_names": skill_names or []})
return task
def record_validation(
self,
task_id: str,
run_id: str,
validation: ValidationResult,
*,
final_attempt: bool = True,
has_usable_answer: bool = True,
) -> TaskRecord:
task = self._require(task_id)
now = self._now()
if validation.status == "accepted":
task.status = "awaiting_feedback"
elif validation.status in {"insufficient_evidence", "validator_error"}:
task.status = "needs_review"
elif validation.status == "rejected" and not final_attempt:
task.status = "needs_revision"
elif validation.status == "rejected" and has_usable_answer:
task.status = "needs_review"
else:
task.status = "failed"
task.closed_at = now
task.close_reason = "automatic validation rejected the final attempt"
task.updated_at = now
task.validation_result = validation.to_dict()
self.store.upsert_task(task)
self._event(task, "validated", run_id=run_id, payload=validation.to_dict())
return task
def add_feedback(
self,
task_id: str,
*,
feedback_type: str,
comment: str | None = None,
run_id: str | None = None,
) -> TaskRecord:
task = self._require(task_id)
now = self._now()
matching_feedback = any(
item.get("run_id") == run_id and item.get("feedback_type") == feedback_type
for item in task.feedback
)
conflicting_feedback = next(
(
item
for item in task.feedback
if item.get("run_id") == run_id and item.get("feedback_type") != feedback_type
),
None,
)
if conflicting_feedback is not None:
raise ValueError(
f"Feedback for run_id={run_id!r} was already recorded as "
f"{conflicting_feedback.get('feedback_type')!r}"
)
if task.status in {"closed", "abandoned"} and not matching_feedback:
raise ValueError(f"Task {task.task_id} is already finalized as {task.status!r}")
if matching_feedback:
return task
entry = {
"feedback_type": feedback_type,
"comment": comment or "",
"run_id": run_id,
"created_at": now,
}
task.feedback.append(entry)
if feedback_type == "revise":
task.status = "needs_revision"
elif feedback_type == "abandon":
task.status = "abandoned"
task.closed_at = now
task.close_reason = comment or "abandoned"
elif feedback_type == "satisfied":
task.status = "closed"
task.closed_at = now
task.close_reason = "satisfied"
task.satisfaction = 1.0
task.updated_at = now
self.store.upsert_task(task)
self._event(task, f"feedback_{feedback_type}", run_id=run_id, payload=entry)
return task
def close_task(self, task_id: str, *, reason: str = "closed") -> TaskRecord:
task = self._require(task_id)
now = self._now()
task.status = "closed"
task.closed_at = now
task.close_reason = reason
task.updated_at = now
self.store.upsert_task(task)
self._event(task, "closed", payload={"reason": reason})
return task
def abandon_task(self, task_id: str, *, reason: str = "abandoned") -> TaskRecord:
task = self._require(task_id)
now = self._now()
task.status = "abandoned"
task.closed_at = now
task.close_reason = reason
task.updated_at = now
self.store.upsert_task(task)
self._event(task, "abandoned", payload={"reason": reason})
return task
def delete_task(self, task_id: str) -> bool:
return self.store.delete_task(task_id)
@staticmethod
def _is_user_visible_active_task(task: TaskRecord) -> bool:
if task.creator != "cron":
return True
metadata = task.metadata or {}
return bool(metadata.get("user_engaged") or metadata.get("requires_followup"))
def _require(self, task_id: str) -> TaskRecord:
task = self.store.get_task(task_id)
if task is None:
raise ValueError(f"Unknown task_id: {task_id}")
return task
def _event(
self,
task: TaskRecord,
event_type: str,
*,
run_id: str | None = None,
payload: dict[str, Any] | None = None,
) -> None:
self.store.append_event(
TaskEvent(
event_id=uuid4().hex,
task_id=task.task_id,
session_id=task.session_id,
run_id=run_id,
event_type=event_type,
created_at=self._now(),
payload=dict(payload or {}),
)
)
@staticmethod
def _now() -> str:
return datetime.now(timezone.utc).isoformat()
def short_task_title(text: str) -> str:
cleaned = " ".join((text or "").strip().split())
if not cleaned:
return "当前任务"
if any("\u4e00" <= char <= "\u9fff" for char in cleaned):
return cleaned[:15]
words = cleaned.split()
if len(words) <= 4:
return cleaned[:40]
return " ".join(words[:4])[:40]