102 lines
3.5 KiB
Python
102 lines
3.5 KiB
Python
"""
|
||||
|
|
Pydantic 数据模型 —— 任务
|
|||
|
|
=========================
|
|||
|
|
|
|||
|
|
【功能说明】任务实体与请求体契约,供 API 层与存储层共用。
|
|||
|
|
【技术选型理由】Pydantic v2 与 FastAPI 原生集成,和 Project 模型同构。
|
|||
|
|
【实现思路】TaskCreate 入参 → Task 实体(含系统字段)。
|
|||
|
|
【潜在风险与应对】字段与存储层漂移:单一来源定义,存储层只做映射。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
from pydantic import BaseModel, Field
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TaskCreate(BaseModel):
|
|||
|
|
"""创建任务的入参模型"""
|
|||
|
|
|
|||
|
|
project_id: str = Field(..., description="所属项目 ID")
|
|||
|
|
requirement_text: str = Field(
|
|||
|
|
..., min_length=1, max_length=5000, description="需求/Bug 描述(自然语言)"
|
|||
|
|
)
|
|||
|
|
task_type: str = Field(default="feature", description="feature | bugfix")
|
|||
|
|
model_preference: str = Field(
|
|||
|
|
default="auto",
|
|||
|
|
description=(
|
|||
|
|
"模型偏好:auto=由 LangGraph 自动按节点分工选模型;"
|
|||
|
|
"或填具体模型 ID(如 mimo-v2.6-pro)强制整个任务用同一模型"
|
|||
|
|
),
|
|||
|
|
)
|
|||
|
|
use_vector_search: bool = Field(
|
|||
|
|
default=True,
|
|||
|
|
description="是否查询向量数据库(false=跳过知识检索节点)",
|
|||
|
|
)
|
|||
|
|
execution_mode: str = Field(
|
|||
|
|
default="agent",
|
|||
|
|
description=(
|
|||
|
|
"执行模式:agent=LangGraph 全流程编排(规划→分发→Worker→验证);"
|
|||
|
|
"direct_claude=直接调用 Claude CLI 一次改码(适合小改动/探索式开发)"
|
|||
|
|
),
|
|||
|
|
)
|
|||
|
|
sort_order: int = Field(default=0, description="排序值(越小越靠前)")
|
|||
|
|
|
|||
|
|
|
|||
|
|
class Task(BaseModel):
|
|||
|
|
"""任务完整实体"""
|
|||
|
|
|
|||
|
|
id: str
|
|||
|
|
project_id: str
|
|||
|
|
requirement_text: str
|
|||
|
|
task_type: str
|
|||
|
|
status: str # running | waiting_approval | done | failed | degraded
|
|||
|
|
thread_id: str = Field(description="LangGraph 检查点线程 ID(断点续跑用)")
|
|||
|
|
model_preference: str = Field(
|
|||
|
|
default="auto",
|
|||
|
|
description="模型偏好:auto 或具体模型 ID(用户提交时可指定)",
|
|||
|
|
)
|
|||
|
|
use_vector_search: bool = Field(
|
|||
|
|
default=True,
|
|||
|
|
description="是否查询向量数据库(false=跳过知识检索)",
|
|||
|
|
)
|
|||
|
|
execution_mode: str = Field(
|
|||
|
|
default="agent",
|
|||
|
|
description="执行模式:agent | direct_claude",
|
|||
|
|
)
|
|||
|
|
sort_order: int = Field(default=0, description="排序值(越小越靠前)")
|
|||
|
|
report: str = ""
|
|||
|
|
error: str = ""
|
|||
|
|
pending_approval: str = Field(
|
|||
|
|
default="", description="待审批内容 JSON(HITL 中断时写入,供前端审批卡片展示)"
|
|||
|
|
)
|
|||
|
|
chat_history: str = Field(
|
|||
|
|
default="[]",
|
|||
|
|
description="对话消息 JSON 数组(一条任务 = 一个完整对话)",
|
|||
|
|
)
|
|||
|
|
session_id: str = Field(
|
|||
|
|
default="",
|
|||
|
|
description="Claude CLI 会话 ID(--resume 续聊用,终端同款机制)",
|
|||
|
|
)
|
|||
|
|
session_ready: bool = Field(
|
|||
|
|
default=False,
|
|||
|
|
description="Claude 会话是否已真实建立(CLI 吐出 init 事件后为 true)",
|
|||
|
|
)
|
|||
|
|
worker_pid: int = Field(
|
|||
|
|
default=0,
|
|||
|
|
description="执行该任务的 worker 进程 PID(孤儿恢复判断存活用)",
|
|||
|
|
)
|
|||
|
|
created_at: datetime
|
|||
|
|
updated_at: datetime
|
|||
|
|
|
|||
|
|
|
|||
|
|
class TaskEvent(BaseModel):
|
|||
|
|
"""SSE 推送的单个事件(节点级进度)"""
|
|||
|
|
|
|||
|
|
event: str # node_start | node_end | status_change | error | done
|
|||
|
|
node: str = ""
|
|||
|
|
message: str = ""
|
|||
|
|
data: dict = Field(default_factory=dict)
|
|||
|
|
timestamp: str = ""
|