File size: 4,799 Bytes
04bb755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Pydantic schemas for workflows."""

from datetime import datetime
from decimal import Decimal
from typing import Optional
from uuid import UUID

from pydantic import BaseModel, ConfigDict, Field, model_validator

from src.schemas.agent import AgentResponse, SkillResponse


# --- Steps ---

class WorkflowStepCreate(BaseModel):
    agent_id: Optional[UUID] = None
    skill_id: Optional[UUID] = None
    sub_workflow_id: Optional[UUID] = None
    step_group: int = Field(0, ge=0)
    position: int = Field(0, ge=0)
    input_mode: str = Field("chain", pattern=r"^(chain|original|custom)$")
    input_template: Optional[str] = None
    label: Optional[str] = Field(None, max_length=255)
    instructions: Optional[str] = Field(None, max_length=1000)

    @model_validator(mode="after")
    def validate_step_target(self):
        has_agent_and_skill = self.agent_id is not None and self.skill_id is not None
        has_partial_agent = (self.agent_id is not None) != (self.skill_id is not None)
        has_sub = self.sub_workflow_id is not None
        if has_partial_agent:
            raise ValueError(
                "Step must have both agent_id and skill_id together, not just one"
            )
        if has_agent_and_skill == has_sub:
            raise ValueError(
                "Step must have either (agent_id + skill_id) or sub_workflow_id, not both or neither"
            )
        return self


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

    id: UUID
    workflow_id: UUID
    agent_id: Optional[UUID]
    skill_id: Optional[UUID]
    sub_workflow_id: Optional[UUID] = None
    step_group: int
    position: int
    input_mode: str
    input_template: Optional[str]
    label: Optional[str]
    instructions: Optional[str]
    agent: Optional[AgentResponse] = None
    skill: Optional[SkillResponse] = None


# --- Workflow CRUD ---

class WorkflowCreate(BaseModel):
    name: str = Field(max_length=255)
    description: Optional[str] = Field(None, max_length=2000)
    icon: str = Field("🔗", max_length=10)
    is_public: bool = False
    max_total_credits: Optional[int] = Field(None, ge=1)
    timeout_seconds: Optional[int] = Field(1800, ge=60, le=7200)
    step_timeout_seconds: Optional[int] = Field(120, ge=30, le=3600)
    pattern_type: str = Field("manual", pattern=r"^(manual|hierarchical|supervisor)$")
    supervisor_config: Optional[dict] = None
    steps: list[WorkflowStepCreate] = Field(default=[], max_length=50)


class WorkflowUpdate(BaseModel):
    name: Optional[str] = Field(None, max_length=255)
    description: Optional[str] = Field(None, max_length=2000)
    icon: Optional[str] = Field(None, max_length=10)
    is_public: Optional[bool] = None
    max_total_credits: Optional[int] = Field(None, ge=0)
    timeout_seconds: Optional[int] = Field(None, ge=60, le=7200)
    step_timeout_seconds: Optional[int] = Field(None, ge=30, le=3600)
    steps: Optional[list[WorkflowStepCreate]] = None


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

    id: UUID
    owner_id: UUID
    name: str
    description: Optional[str]
    icon: str
    is_public: bool
    max_total_credits: Optional[int]
    timeout_seconds: Optional[int]
    step_timeout_seconds: Optional[int]
    pattern_type: str
    supervisor_config: Optional[dict] = None
    steps: list[WorkflowStepResponse]
    created_at: datetime
    updated_at: datetime


class WorkflowListResponse(BaseModel):
    workflows: list[WorkflowResponse]
    total: int


# --- Runs ---

class WorkflowRunRequest(BaseModel):
    message: str = Field(max_length=10000)


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

    id: UUID
    run_id: UUID
    step_id: Optional[UUID]
    task_id: Optional[UUID]
    step_group: int
    status: str
    output_text: Optional[str]
    error: Optional[str]
    credits_charged: Optional[Decimal]
    child_run_id: Optional[UUID] = None
    started_at: Optional[datetime]
    completed_at: Optional[datetime]


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

    id: UUID
    workflow_id: UUID
    user_id: UUID
    schedule_id: Optional[UUID]
    parent_run_id: Optional[UUID] = None
    depth: int = 0
    status: str
    current_step_group: int
    input_message: str
    workflow_snapshot: Optional[dict]
    total_credits_charged: Optional[Decimal]
    error: Optional[str]
    step_runs: list[WorkflowStepRunResponse]
    created_at: datetime
    completed_at: Optional[datetime]


class WorkflowRunListResponse(BaseModel):
    runs: list[WorkflowRunResponse]
    total: int