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

from pydantic import BaseModel, ConfigDict, Field, model_validator


class TaskStatus(str, 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 PaymentMethod(str, Enum):
    credits = "credits"
    x402 = "x402"


class MessagePart(BaseModel):
    type: str = Field(max_length=50, pattern=r"^[a-z][a-z0-9_]*$")  # text, file, data
    content: Optional[str] = Field(None, max_length=100_000)
    text: Optional[str] = Field(None, max_length=100_000, exclude=True)
    data: Optional[dict] = None
    mime_type: Optional[str] = Field(None, max_length=100)

    @model_validator(mode="before")
    @classmethod
    def _accept_text_field(cls, values):
        """Accept A2A-spec 'text' field as alias for 'content'."""
        if isinstance(values, dict):
            if values.get("text") and not values.get("content"):
                values["content"] = values["text"]
        return values


class TaskMessage(BaseModel):
    role: str = Field(max_length=50, pattern=r"^[a-z][a-z0-9_]*$")
    parts: list[MessagePart] = Field(max_length=100)


class UIComponent(BaseModel):
    """A2UI rich component — rendered by the frontend instead of plain markdown.

    Supported types: table, chart, code_block, diff, image, form, calendar.
    """
    type: str = Field(max_length=50)  # table, chart, code_block, diff, image, form, calendar
    title: Optional[str] = Field(None, max_length=255)
    data: dict = Field(default_factory=dict)
    metadata: dict = Field(default_factory=dict)


class Artifact(BaseModel):
    name: Optional[str] = Field(None, max_length=255)
    parts: list[MessagePart] = Field(max_length=100)
    metadata: dict = Field(default_factory=dict, max_length=50)
    ui_components: list[UIComponent] = Field(default_factory=list, max_length=20)


class TaskCreate(BaseModel):
    provider_agent_id: UUID
    skill_id: str = Field(max_length=255)
    messages: list[TaskMessage] = Field(max_length=50)
    max_credits: Optional[float] = Field(None, ge=0, le=100_000)
    tier: Optional[str] = Field(None, max_length=50, description="Pricing tier name (e.g. 'free', 'pro')")
    payment_method: PaymentMethod = PaymentMethod.credits
    validate_match: bool = Field(False, description="If true, check message-skill alignment and return a warning if mismatched")
    confirmed: bool = Field(False, description="If true, bypass high-cost approval check")
    # Delegation tracking (set by frontend when using auto-delegation)
    parent_task_id: Optional[UUID] = None
    suggested_agent_id: Optional[UUID] = None
    suggestion_confidence: Optional[float] = Field(None, ge=0, le=1)
    # AP2: Idempotency — prevents duplicate task creation on retries
    idempotency_key: Optional[str] = Field(
        None, max_length=64,
        description="Client-provided key for idempotent task creation. "
                    "If a task with this key already exists for this user, "
                    "the existing task is returned instead of creating a new one."
    )


class TaskResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: UUID
    client_agent_id: Optional[UUID] = None
    provider_agent_id: Optional[UUID] = None
    provider_agent_name: Optional[str] = None
    skill_id: Optional[str | UUID] = None
    skill_name: Optional[str] = None
    status: TaskStatus
    messages: list[TaskMessage]
    artifacts: list[Artifact]
    credits_quoted: Optional[float] = 0
    credits_charged: Optional[float] = 0
    latency_ms: Optional[int] = None
    client_rating: Optional[float] = None
    payment_method: str = "credits"
    x402_receipt: Optional[dict] = None
    status_history: list[dict] = []
    quality_score: Optional[float] = None
    eval_model: Optional[str] = None
    eval_relevance: Optional[float] = None
    eval_completeness: Optional[float] = None
    eval_coherence: Optional[float] = None
    delegation_warning: Optional[str] = None
    delegation_depth: int = 0
    parent_task_id: Optional[UUID] = None
    suggested_agent_id: Optional[UUID] = None
    suggestion_confidence: Optional[float] = None
    idempotency_key: Optional[str] = None
    created_at: datetime
    completed_at: Optional[datetime] = None


class TaskRating(BaseModel):
    score: float = Field(ge=1, le=5)
    comment: str = Field("", max_length=2000)


class TaskListResponse(BaseModel):
    tasks: list[TaskResponse]
    total: int