File size: 4,583 Bytes
042f4ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
import enum
import uuid
from datetime import datetime
from decimal import Decimal
from typing import Optional

from sqlalchemy import JSON, DateTime, Float, ForeignKey, Integer, Numeric, String, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.database import Base


class TaskStatus(str, enum.Enum):
    SUBMITTED = "submitted"
    PENDING_APPROVAL = "pending_approval"
    PENDING_PAYMENT = "pending_payment"
    WORKING = "working"
    INPUT_REQUIRED = "input_required"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELED = "canceled"
    REJECTED = "rejected"


class Task(Base):
    __tablename__ = "tasks"

    id: Mapped[uuid.UUID] = mapped_column(
        Uuid, primary_key=True, default=uuid.uuid4
    )
    creator_user_id: Mapped[Optional[uuid.UUID]] = mapped_column(
        Uuid, ForeignKey("users.id", ondelete="SET NULL"), nullable=True, index=True
    )
    client_agent_id: Mapped[uuid.UUID] = mapped_column(
        Uuid, ForeignKey("agents.id", ondelete="SET NULL"), nullable=True, index=True
    )
    provider_agent_id: Mapped[uuid.UUID] = mapped_column(
        Uuid, ForeignKey("agents.id", ondelete="SET NULL"), nullable=True, index=True
    )
    skill_id: Mapped[Optional[uuid.UUID]] = mapped_column(
        Uuid, ForeignKey("agent_skills.id", ondelete="SET NULL"), nullable=True, index=True
    )
    status: Mapped[TaskStatus] = mapped_column(
        String(20), nullable=False, default=TaskStatus.SUBMITTED, index=True
    )
    messages: Mapped[list] = mapped_column(JSON, nullable=True, default=list)
    artifacts: Mapped[list] = mapped_column(JSON, nullable=True, default=list)
    credits_quoted: Mapped[Optional[Decimal]] = mapped_column(Numeric(12, 4), nullable=True)
    credits_charged: Mapped[Optional[Decimal]] = mapped_column(Numeric(12, 4), nullable=True)
    latency_ms: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
    client_rating: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    quality_score: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    # Eval subscores (LLM-as-judge breakdown)
    eval_model: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
    eval_relevance: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    eval_completeness: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    eval_coherence: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    payment_method: Mapped[str] = mapped_column(
        String(20), nullable=False, default="credits", server_default="credits"
    )
    x402_receipt: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
    callback_url: Mapped[Optional[str]] = mapped_column(String(2048), nullable=True)
    # Delegation tracking
    delegation_depth: Mapped[int] = mapped_column(Integer, nullable=False, default=0, server_default="0")
    parent_task_id: Mapped[Optional[uuid.UUID]] = mapped_column(
        Uuid, ForeignKey("tasks.id", ondelete="SET NULL"), nullable=True
    )
    suggested_agent_id: Mapped[Optional[uuid.UUID]] = mapped_column(
        Uuid, ForeignKey("agents.id", ondelete="SET NULL"), nullable=True
    )
    suggestion_confidence: Mapped[Optional[float]] = mapped_column(Float, nullable=True)
    # AP2: Idempotency key for duplicate prevention
    idempotency_key: Mapped[Optional[str]] = mapped_column(
        String(64), nullable=True, index=True
    )

    status_history: Mapped[list | None] = mapped_column(JSON, nullable=True, default=list)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now(), nullable=False
    )
    completed_at: Mapped[Optional[datetime]] = mapped_column(
        DateTime(timezone=True), nullable=True
    )

    # Relationships
    client_agent: Mapped[Optional["Agent"]] = relationship(
        "Agent", back_populates="client_tasks", foreign_keys=[client_agent_id], lazy="selectin"
    )
    provider_agent: Mapped[Optional["Agent"]] = relationship(
        "Agent", back_populates="provider_tasks", foreign_keys=[provider_agent_id], lazy="selectin"
    )
    skill: Mapped[Optional["AgentSkill"]] = relationship(
        "AgentSkill", back_populates="tasks", lazy="selectin"
    )
    transactions: Mapped[list["Transaction"]] = relationship(
        "Transaction", back_populates="task", lazy="noload"
    )

    def __repr__(self) -> str:
        return f"<Task(id={self.id}, status={self.status})>"