70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
"""FastAPI 请求/响应 schema。
|
|
|
|
使用 Pydantic 模型校验管理 API 的 JSON 请求体,并把请求数据转换为 SDK
|
|
协议模型。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
from wov_sdk.models import NodeManifest
|
|
|
|
|
|
class NodeCreate(BaseModel):
|
|
"""节点注册请求体,字段与 NodeManifest 保持一致。"""
|
|
|
|
id: str = Field(min_length=1)
|
|
name: str = Field(min_length=1)
|
|
version: str = Field(min_length=1)
|
|
capability: str = Field(min_length=1)
|
|
command: list[str] = Field(min_length=1)
|
|
repo_dir: str = "."
|
|
env: dict[str, str] = Field(default_factory=dict)
|
|
input_schema: dict[str, Any] = Field(default_factory=dict)
|
|
output_schema: dict[str, Any] = Field(default_factory=dict)
|
|
max_concurrency: int = Field(default=1, ge=1)
|
|
idle_ttl_seconds: int = Field(default=300, ge=0)
|
|
health_timeout_seconds: int = Field(default=10, ge=1)
|
|
keep_warm: bool = False
|
|
|
|
def to_manifest(self) -> NodeManifest:
|
|
"""转换为 SDK 的 NodeManifest 对象,供注册与校验使用。"""
|
|
return NodeManifest(
|
|
id=self.id,
|
|
name=self.name,
|
|
version=self.version,
|
|
capability=self.capability,
|
|
command=list(self.command),
|
|
repo_dir=self.repo_dir,
|
|
env=dict(self.env),
|
|
input_schema=dict(self.input_schema),
|
|
output_schema=dict(self.output_schema),
|
|
max_concurrency=self.max_concurrency,
|
|
idle_ttl_seconds=self.idle_ttl_seconds,
|
|
health_timeout_seconds=self.health_timeout_seconds,
|
|
keep_warm=self.keep_warm,
|
|
)
|
|
|
|
|
|
class InvokePayload(BaseModel):
|
|
"""管理后台手动调用节点的请求体。"""
|
|
|
|
# 默认 run_id 便于快速联调,正式运行时会由调度器生成。
|
|
run_id: str = Field(default_factory=lambda: "run_admin")
|
|
inputs: dict[str, Any] = Field(default_factory=dict)
|
|
params: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
|
class WorkflowCreate(BaseModel):
|
|
"""创建工作流或新增版本的请求体。"""
|
|
|
|
# 缺省时由后端根据名称生成 slug ID。
|
|
id: str | None = None
|
|
name: str = Field(min_length=1)
|
|
description: str = ""
|
|
# DAG 原始字典,后端会解析并校验为 WorkflowDefinition。
|
|
definition: dict[str, Any]
|