✨ Add versioned annotation operations

This commit is contained in:
eust-w 2026-08-17 20:44:16 +08:00
parent 9ebc965c37
commit 2eb9218057
27 changed files with 3432 additions and 13 deletions

View File

@ -5289,6 +5289,8 @@ BUILTIN_HANDLERS: dict[str, BuiltinHandler] = {
"video_slicing": operator_handler('backend.operators.builtin.video_slice.operator'),
"video_target_clip": operator_handler('backend.operators.builtin.video_target_clip.operator'),
"video_annotation": operator_handler('backend.operators.builtin.video_annotate.operator'),
"orbbec_data_normalize": operator_handler('backend.operators.builtin.orbbec_data_normalize.operator'),
"semantic_prelabel": operator_handler('backend.operators.builtin.semantic_prelabel.operator'),
"result_package": operator_handler('backend.operators.builtin.package.operator'),
"audit_sampling": operator_handler('backend.operators.builtin.audit_sample.operator'),
"hf_dataset_probe": operator_handler('backend.operators.builtin.hf_dataset_probe.operator'),

View File

@ -0,0 +1,16 @@
{
"id": "operator-builtin-orbbec-data-normalize",
"name": "Orbbec Ego 数据标准化",
"category": "DATASET",
"type": "builtin",
"runtimeHandler": "builtin_orbbec_data_normalize",
"runtimeSource": "backend/operators/builtin/orbbec_data_normalize/operator.py",
"migrationStatus": "migrated",
"acceptance": {
"requiresRealInputs": true,
"mockOnlyAccepted": false,
"simulationOnlyAccepted": false,
"hardcodedOutputAccepted": false,
"shallowOnlyAccepted": false
}
}

View File

@ -0,0 +1,131 @@
from __future__ import annotations
import pathlib
import re
from typing import Any, Callable
HelperMap = dict[str, Callable[..., Any]]
VIDEO_SUFFIXES = {".mp4", ".mov", ".m4v", ".avi", ".mkv", ".webm"}
CAPTURE_PATTERN = re.compile(r"^(?P<capture>Orbbec_Ego_[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)")
CAMERA_PATTERN = re.compile(r"_camera_(?P<side>left|right)_", re.IGNORECASE)
def _entry_path(entry: dict[str, Any]) -> str:
return str(entry.get("path") or entry.get("relativePath") or entry.get("name") or "").strip().lstrip("/")
def _capture_id(path: str) -> str:
matched = CAPTURE_PATTERN.search(path)
if matched:
return matched.group("capture")
parts = pathlib.PurePosixPath(path).parts
return parts[0] if len(parts) > 1 else pathlib.PurePosixPath(path).stem
def _camera_side(path: str) -> str:
matched = CAMERA_PATTERN.search(path)
return matched.group("side").lower() if matched else ""
def run(
run_id: str,
node_id: str,
output_dir: Any,
node_data: dict[str, Any],
inputs: list[dict[str, Any]],
helpers: HelperMap,
) -> list[dict[str, Any]]:
params = helpers["hyperparams"](node_data)
side_filter = str(params.get("CAMERA_SIDE", "both")).strip().lower() or "both"
if side_filter not in {"left", "right", "both"}:
raise RuntimeError("CAMERA_SIDE must be left, right or both")
require_stereo = str(params.get("REQUIRE_STEREO", "false")).strip().lower() in {"1", "true", "yes", "on"}
source = helpers["first_payload_with_keys"](inputs, "entries")
if source is None:
raise RuntimeError("Orbbec Ego 数据标准化需要上游 source manifest entries")
entries = [item for item in source.get("entries", []) if isinstance(item, dict)]
grouped: dict[str, list[dict[str, Any]]] = {}
for entry in entries:
path = _entry_path(entry)
if not path:
continue
grouped.setdefault(_capture_id(path), []).append({**entry, "relativePath": path})
collections: list[dict[str, Any]] = []
videos: list[dict[str, Any]] = []
for capture_id in sorted(grouped):
capture_entries = grouped[capture_id]
by_name = {pathlib.PurePosixPath(_entry_path(item)).name: item for item in capture_entries}
capture_videos = []
present_sides: set[str] = set()
for entry in capture_entries:
path = _entry_path(entry)
if pathlib.PurePosixPath(path).suffix.lower() not in VIDEO_SUFFIXES:
continue
side = _camera_side(path)
if side:
present_sides.add(side)
if side_filter != "both" and side != side_filter:
continue
filename = pathlib.PurePosixPath(path).name
pts_name = pathlib.PurePosixPath(path).stem + "_pts.csv"
video = {
"collectionId": capture_id,
"collectionName": capture_id,
"filename": filename,
"relativePath": path,
"path": str((entry.get("source") or {}).get("path") or entry.get("path") or path),
"sizeBytes": int(entry.get("sizeBytes", 0) or 0),
"mimeType": str(entry.get("mimeType", "video/mp4") or "video/mp4"),
"cameraSide": side,
"source": entry.get("source") if isinstance(entry.get("source"), dict) else {"path": entry.get("path", path)},
"pairedFiles": {
"pts": _entry_path(by_name.get(pts_name, {})),
"audio": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio.wav")), ""),
"audioPts": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_audio_pts.csv")), ""),
"imu": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_imu.csv")), ""),
"cameraCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_camera.yaml")), ""),
"imuCalibration": next((_entry_path(item) for item in capture_entries if _entry_path(item).endswith("_calibration_imu.yaml")), ""),
},
"meta": {"device": "Orbbec Ego", "captureId": capture_id, "cameraSide": side},
}
capture_videos.append(video)
videos.append(video)
missing = []
if not capture_videos:
missing.append("video")
if require_stereo and not {"left", "right"}.issubset(present_sides):
missing.append("stereo_pair")
collections.append(
{
"id": capture_id,
"name": capture_id,
"datasetType": "orbbec-ego",
"videoCount": len(capture_videos),
"fileCount": len(capture_entries),
"cameraSides": sorted(present_sides),
"missingRequiredFiles": missing,
"valid": not missing,
}
)
manifest = {
"schemaVersion": "droboticflow.orbbec.normalized/v1",
"datasetType": "orbbec-ego",
"sourcePath": source.get("sourcePath", ""),
"collectionCount": len(collections),
"videoCount": len(videos),
"collections": collections,
"items": videos,
"entries": entries,
}
return [
helpers["write_json_artifact"](
run_id,
node_id,
output_dir,
"orbbec-normalized-manifest.json",
manifest,
output=helpers["output_binding"](node_data),
)
]

View File

@ -0,0 +1,16 @@
{
"id": "operator-builtin-semantic-prelabel",
"name": "版本化语义预标",
"category": "VIDEO",
"type": "builtin",
"runtimeHandler": "builtin_semantic_prelabel",
"runtimeSource": "backend/operators/builtin/semantic_prelabel/operator.py",
"migrationStatus": "migrated",
"acceptance": {
"requiresRealInputs": true,
"mockOnlyAccepted": false,
"simulationOnlyAccepted": false,
"hardcodedOutputAccepted": false,
"shallowOnlyAccepted": false
}
}

View File

@ -0,0 +1,109 @@
from __future__ import annotations
from typing import Any, Callable
HelperMap = dict[str, Callable[..., Any]]
def _label_index(raw_labels: list[Any]) -> tuple[dict[str, dict[str, Any]], list[dict[str, Any]]]:
index: dict[str, dict[str, Any]] = {}
labels: list[dict[str, Any]] = []
for position, raw in enumerate(raw_labels):
if isinstance(raw, str):
item = {"id": f"label-{position + 1}", "name": raw.strip(), "aliases": []}
elif isinstance(raw, dict):
item = {
"id": str(raw.get("id") or f"label-{position + 1}").strip(),
"name": str(raw.get("name") or "").strip(),
"aliases": [str(value).strip() for value in raw.get("aliases", []) if str(value).strip()],
}
else:
continue
if not item["name"]:
continue
labels.append(item)
for candidate in [item["id"], item["name"], *item["aliases"]]:
index[candidate.lower()] = item
return index, labels
def run(
run_id: str,
node_id: str,
output_dir: Any,
node_data: dict[str, Any],
inputs: list[dict[str, Any]],
helpers: HelperMap,
) -> list[dict[str, Any]]:
params = helpers["hyperparams"](node_data)
template_version = str(params.get("TEMPLATE_VERSION", "v1")).strip() or "v1"
raw_labels = helpers["parse_json_param"](params, "LABEL_SCHEMA_JSON", [], list)
phrases = helpers["parse_json_param"](params, "PHRASES_JSON", [], list)
minimum_confidence = helpers["parse_unit_interval_param"](params, "MINIMUM_CONFIDENCE", 0.0)
label_by_alias, labels = _label_index(raw_labels)
if not labels:
raise RuntimeError("LABEL_SCHEMA_JSON must define at least one semantic label")
payload = helpers["first_payload_with_keys"](inputs, "annotationCount", "items")
if payload is None:
raise RuntimeError("版本化语义预标需要上游视频标注结果")
items: list[dict[str, Any]] = []
segment_count = 0
for item in payload.get("items", []):
if not isinstance(item, dict):
continue
annotation = item.get("annotation") if isinstance(item.get("annotation"), dict) else {}
segments = []
for position, action in enumerate(annotation.get("actions", [])):
if not isinstance(action, dict):
continue
confidence = float(action.get("confidence", 0.0) or 0.0)
if confidence < minimum_confidence:
continue
raw_label = str(action.get("label", "")).strip()
definition = label_by_alias.get(raw_label.lower())
if definition is None:
continue
segments.append(
{
"id": f"{item.get('clipId', 'clip')}-segment-{position + 1}",
"labelId": definition["id"],
"label": definition["name"],
"start": float(action.get("start", 0.0) or 0.0),
"end": float(action.get("end", 0.0) or 0.0),
"confidence": confidence,
"phrases": [str(value) for value in phrases],
"source": "automatic_prelabel",
}
)
segment_count += len(segments)
items.append(
{
**item,
"semanticAnnotation": {
"schemaVersion": "droboticflow.semantic.annotation/v1",
"templateVersion": template_version,
"segments": segments,
},
}
)
manifest = {
"schemaVersion": "droboticflow.semantic.prelabels/v1",
"datasetType": "semantic-prelabels",
"templateVersion": template_version,
"labels": labels,
"phrases": phrases,
"annotationCount": len(items),
"segmentCount": segment_count,
"items": items,
}
return [
helpers["write_json_artifact"](
run_id,
node_id,
output_dir,
"semantic-prelabels.json",
manifest,
output=helpers["output_binding"](node_data),
)
]

View File

@ -0,0 +1,8 @@
# 泛宇标注生产算子
本目录登记第二阶段新增的两个独立算子定义:
- `Orbbec Ego 数据标准化`:消费 BOS source manifest,生成双目视频、PTS、IMU、音频和标定文件之间的版本化配对关系。
- `版本化语义预标`:消费真实 `视频标注` 结果,并按模板版本、标签别名和置信度输出可人工修订的时间段。
运行源码位于 `backend/operators/builtin/orbbec_data_normalize/operator.py` 与 `backend/operators/builtin/semantic_prelabel/operator.py`。定义保持 `draft`,直到各自独立镜像完成构建、digest 固定、真实数据工作流验收和 artifact/lineage 回读;不得借用 operator-runner 或其他算子的 digest 冒充已发布镜像。

View File

@ -0,0 +1,91 @@
[
{
"id": "operator-custom-orbbec-data-normalize",
"name": "Orbbec Ego 数据标准化",
"type": "custom",
"category": "DATASET",
"description": "将 Orbbec Ego 双目视频、PTS、IMU、音频和标定对象清单标准化为版本化 manifest。",
"inputs": [
{
"id": "in-1",
"name": "source_manifest",
"type": "json",
"transmission": "bus"
}
],
"outputs": [
{
"id": "out-1",
"name": "normalized_manifest",
"type": "json",
"transmission": "bus"
}
],
"runtimeHandler": "builtin_orbbec_data_normalize",
"runtimeSource": "backend/operators/builtin/orbbec_data_normalize/operator.py",
"hyperparams": [
{
"key": "CAMERA_SIDE",
"value": "both",
"type": "string"
},
{
"key": "REQUIRE_STEREO",
"value": "false",
"type": "boolean"
}
],
"generationSource": "manual",
"publicationStatus": "draft"
},
{
"id": "operator-custom-semantic-prelabel",
"name": "版本化语义预标",
"type": "custom",
"category": "VIDEO",
"description": "将真实视频动作分段映射到版本化语义词表,输出可人工修订的预标时间段。",
"inputs": [
{
"id": "in-1",
"name": "annotations",
"type": "json",
"transmission": "bus"
}
],
"outputs": [
{
"id": "out-1",
"name": "semantic_prelabels",
"type": "json",
"transmission": "bus"
}
],
"runtimeHandler": "builtin_semantic_prelabel",
"runtimeSource": "backend/operators/builtin/semantic_prelabel/operator.py",
"businessStage": "annotate",
"hyperparams": [
{
"key": "TEMPLATE_VERSION",
"value": "v1",
"type": "string"
},
{
"key": "LABEL_SCHEMA_JSON",
"value": "[{\"id\":\"static\",\"name\":\"静止观察\"},{\"id\":\"steady\",\"name\":\"平稳活动\"},{\"id\":\"fast\",\"name\":\"快速移动\"}]",
"type": "json"
},
{
"key": "PHRASES_JSON",
"value": "[\"手部\",\"前景目标\"]",
"type": "json"
},
{
"key": "MINIMUM_CONFIDENCE",
"value": "0.70",
"type": "number"
}
],
"generationSource": "manual",
"publicationStatus": "draft"
}
]

View File

@ -0,0 +1,36 @@
# 标注生产闭环
`/annotation-operations` 提供项目级语义模板、供应商分组、逐段标注、质检、验收、返工和工时统计。平台全局角色仍负责系统权限,业务分工由项目成员关系单独表达:
- `annotator`:提交首轮标注或返工 revision。
- `quality_reviewer`:对当前 revision 做质检通过或打回。
- `acceptance_reviewer`:对质检通过的 revision 做最终验收。
每个成员关系同时绑定 `projectId`、`workgroupId` 和 `vendorId`。除管理员、流程工程师和兼容管理员外,后端只返回当前用户所在分组的任务、模板、分组和工时事件;前端隐藏不是安全边界。
## 核心接口
- `GET/POST /api/annotation-workgroups`
- `GET/POST /api/annotation-memberships`
- `GET/POST /api/annotation-templates`
- `POST /api/annotation-templates/{id}/publish`
- `GET/POST /api/annotation-tasks`
- `GET /api/annotation-tasks/{id}`
- `POST /api/annotation-tasks/{id}/revisions`
- `POST /api/annotation-tasks/{id}/reviews`
- `GET /api/annotation-workload-summary`
模板必须发布后才能创建任务。时间段必须位于 clip 范围内、不能重叠,置信度必须位于 `[0, 1]`。质检或验收拒绝会把任务转为 `rework` 并指向下一 revision,不在工作流 DAG 中建立环。
## 工作量口径
首次 revision 产生 `outputDelta=1`;返工 revision 记录工时和返工次数,但 `outputDelta=0`,因此不会重复增加正式产出。质检、验收及拒绝原因分别记录为不可变工作量事件,汇总可按用户、供应商、分组和模板版本查询。
## 泛宇算子
`custom-operators/fanyu-annotation/operator-definitions.json` 登记两个 draft 算子:
- `Orbbec Ego 数据标准化`:将视频、PTS、IMU、音频和标定对象配对为 `droboticflow.orbbec.normalized/v1`。
- `版本化语义预标`:将视频动作分段映射为 `droboticflow.semantic.prelabels/v1`。
两者在独立镜像构建、digest 固定、真实工作流验收和 artifact/lineage 回读前保持 draft,不能借用其他镜像的 digest 标记为已发布。

View File

@ -0,0 +1,796 @@
package api
import (
"context"
"fmt"
"math"
"net/http"
"sort"
"strconv"
"strings"
"droboticflow/internal/domain"
"github.com/go-chi/chi/v5"
)
func annotationManager(principal *authenticatedPrincipal) bool {
if principal == nil {
return false
}
switch principal.User.Role {
case domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator:
return true
default:
return false
}
}
func annotationPrincipalUserID(principal *authenticatedPrincipal) string {
if principal == nil {
return ""
}
return principal.User.ID
}
func annotationRoleValid(role domain.AnnotationAssignmentRole) bool {
switch role {
case domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer, domain.AnnotationRoleAcceptanceReviewer:
return true
default:
return false
}
}
func annotationTaskStatusValid(status domain.AnnotationTaskStatus) bool {
switch status {
case domain.AnnotationTaskStatusPendingAnnotation, domain.AnnotationTaskStatusPendingQuality,
domain.AnnotationTaskStatusPendingAcceptance, domain.AnnotationTaskStatusRework,
domain.AnnotationTaskStatusAccepted, domain.AnnotationTaskStatusRejected:
return true
default:
return false
}
}
func (server *server) annotationMembershipsForUser(ctx context.Context, organizationID string, projectID string, userID string) ([]domain.ProjectAnnotationMembershipRecord, error) {
items, err := server.store.ListProjectAnnotationMemberships(ctx, organizationID, projectID)
if err != nil {
return nil, err
}
filtered := make([]domain.ProjectAnnotationMembershipRecord, 0)
for _, item := range items {
if item.UserID == userID {
filtered = append(filtered, item)
}
}
return filtered, nil
}
func membershipAllowsTask(memberships []domain.ProjectAnnotationMembershipRecord, task domain.AnnotationTaskRecord) bool {
for _, membership := range memberships {
if membership.ProjectID != task.ProjectID || membership.WorkgroupID != task.WorkgroupID {
continue
}
if membership.VendorID != "" && task.VendorID != "" && membership.VendorID != task.VendorID {
continue
}
return true
}
return false
}
func membershipHasRole(memberships []domain.ProjectAnnotationMembershipRecord, task domain.AnnotationTaskRecord, role domain.AnnotationAssignmentRole) bool {
for _, membership := range memberships {
if membership.AssignmentRole == role && membershipAllowsTask([]domain.ProjectAnnotationMembershipRecord{membership}, task) {
return true
}
}
return false
}
func (server *server) ensureAnnotationTaskAccess(writer http.ResponseWriter, request *http.Request, task domain.AnnotationTaskRecord) (*authenticatedPrincipal, []domain.ProjectAnnotationMembershipRecord, bool) {
principal, ok := currentPrincipal(request)
if !ok || principal == nil {
renderJSON(writer, http.StatusUnauthorized, map[string]string{"error": "login is required"})
return nil, nil, false
}
if !server.ensureTenantAccess(writer, request, task.OrganizationID, "Annotation task") {
return nil, nil, false
}
if annotationManager(principal) {
return principal, nil, true
}
memberships, err := server.annotationMembershipsForUser(request.Context(), task.OrganizationID, task.ProjectID, principal.User.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return nil, nil, false
}
if !membershipAllowsTask(memberships, task) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "annotation workgroup access is required"})
return nil, nil, false
}
return principal, memberships, true
}
type annotationWorkgroupInput struct {
Name string `json:"name"`
VendorID string `json:"vendorId"`
Description string `json:"description,omitempty"`
}
func (server *server) handleAnnotationWorkgroupsList(writer http.ResponseWriter, request *http.Request) {
items, err := server.store.ListAnnotationWorkgroups(request.Context(), server.requestOrganizationID(request))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
if !annotationManager(principal) && principal != nil {
memberships, loadErr := server.annotationMembershipsForUser(request.Context(), server.requestOrganizationID(request), "", principal.User.ID)
if loadErr != nil {
renderError(writer, http.StatusInternalServerError, loadErr)
return
}
allowed := map[string]struct{}{}
for _, membership := range memberships {
allowed[membership.WorkgroupID] = struct{}{}
}
filtered := items[:0]
for _, item := range items {
if _, ok := allowed[item.ID]; ok {
filtered = append(filtered, item)
}
}
items = filtered
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) handleAnnotationWorkgroupCreate(writer http.ResponseWriter, request *http.Request) {
var payload annotationWorkgroupInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name = strings.TrimSpace(payload.Name)
payload.VendorID = strings.TrimSpace(payload.VendorID)
if payload.Name == "" || payload.VendorID == "" {
renderValidationError(writer,
validationIssue{Field: "name", Message: "name is required"},
validationIssue{Field: "vendorId", Message: "vendorId is required"},
)
return
}
item := domain.AnnotationWorkgroupRecord{
ID: domain.NewID("annotation-workgroup"),
OrganizationID: server.requestOrganizationID(request),
Name: payload.Name,
VendorID: payload.VendorID,
Description: strings.TrimSpace(payload.Description),
Status: "active",
CreatedAt: nowISO(),
}
if err := server.store.UpsertAnnotationWorkgroup(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
saved, err := server.store.GetAnnotationWorkgroup(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, saved)
}
type annotationMembershipInput struct {
ProjectID string `json:"projectId"`
UserID string `json:"userId"`
WorkgroupID string `json:"workgroupId"`
AssignmentRole domain.AnnotationAssignmentRole `json:"assignmentRole"`
}
func (server *server) handleAnnotationMembershipsList(writer http.ResponseWriter, request *http.Request) {
projectID := strings.TrimSpace(request.URL.Query().Get("projectId"))
items, err := server.store.ListProjectAnnotationMemberships(request.Context(), server.requestOrganizationID(request), projectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
if !annotationManager(principal) && principal != nil {
filtered := items[:0]
for _, item := range items {
if item.UserID == principal.User.ID {
filtered = append(filtered, item)
}
}
items = filtered
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) handleAnnotationMembershipCreate(writer http.ResponseWriter, request *http.Request) {
var payload annotationMembershipInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.ProjectID = strings.TrimSpace(payload.ProjectID)
payload.UserID = strings.TrimSpace(payload.UserID)
payload.WorkgroupID = strings.TrimSpace(payload.WorkgroupID)
if payload.ProjectID == "" || payload.UserID == "" || payload.WorkgroupID == "" || !annotationRoleValid(payload.AssignmentRole) {
renderValidationError(writer, validationIssue{Field: "membership", Message: "projectId, userId, workgroupId and a valid assignmentRole are required"})
return
}
project, err := server.store.GetProject(request.Context(), payload.ProjectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
user, userErr := server.store.GetUser(request.Context(), payload.UserID)
group, groupErr := server.store.GetAnnotationWorkgroup(request.Context(), payload.WorkgroupID)
if userErr != nil || groupErr != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("load membership references: user=%v workgroup=%v", userErr, groupErr))
return
}
organizationID := server.requestOrganizationID(request)
if project == nil || user == nil || group == nil || !annotationOrganizationEqual(project.OrganizationID, organizationID) || !annotationOrganizationEqual(user.OrganizationID, organizationID) || !annotationOrganizationEqual(group.OrganizationID, organizationID) {
renderValidationError(writer, validationIssue{Field: "membership", Message: "project, user and workgroup must exist in the current organization"})
return
}
item := domain.ProjectAnnotationMembershipRecord{
ID: domain.NewID("annotation-membership"),
OrganizationID: organizationID,
ProjectID: project.ID,
UserID: user.ID,
WorkgroupID: group.ID,
VendorID: group.VendorID,
AssignmentRole: payload.AssignmentRole,
CreatedAt: nowISO(),
}
if err := server.store.UpsertProjectAnnotationMembership(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, item)
}
func annotationOrganizationEqual(left string, right string) bool {
return normalizedTenantID(left) == normalizedTenantID(right)
}
type annotationTemplateInput struct {
ProjectID string `json:"projectId"`
Name string `json:"name"`
Version int `json:"version"`
Labels []domain.SemanticLabelDefinition `json:"labels"`
Phrases []string `json:"phrases,omitempty"`
Prelabel domain.AnnotationPrelabelSettings `json:"prelabel"`
}
func validateAnnotationLabels(labels []domain.SemanticLabelDefinition) []validationIssue {
issues := make([]validationIssue, 0)
seenIDs := map[string]struct{}{}
seenNames := map[string]struct{}{}
for index := range labels {
labels[index].ID = strings.TrimSpace(labels[index].ID)
labels[index].Name = strings.TrimSpace(labels[index].Name)
if labels[index].ID == "" || labels[index].Name == "" {
issues = append(issues, validationIssue{Field: fmt.Sprintf("labels.%d", index), Message: "id and name are required"})
continue
}
idKey := strings.ToLower(labels[index].ID)
nameKey := strings.ToLower(labels[index].Name)
if _, ok := seenIDs[idKey]; ok {
issues = append(issues, validationIssue{Field: fmt.Sprintf("labels.%d.id", index), Message: "label id must be unique"})
}
if _, ok := seenNames[nameKey]; ok {
issues = append(issues, validationIssue{Field: fmt.Sprintf("labels.%d.name", index), Message: "label name must be unique"})
}
seenIDs[idKey] = struct{}{}
seenNames[nameKey] = struct{}{}
}
return issues
}
func (server *server) handleAnnotationTemplatesList(writer http.ResponseWriter, request *http.Request) {
projectID := strings.TrimSpace(request.URL.Query().Get("projectId"))
items, err := server.store.ListAnnotationTemplates(request.Context(), server.requestOrganizationID(request), projectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
if !annotationManager(principal) && principal != nil {
memberships, loadErr := server.annotationMembershipsForUser(request.Context(), server.requestOrganizationID(request), projectID, principal.User.ID)
if loadErr != nil {
renderError(writer, http.StatusInternalServerError, loadErr)
return
}
projects := map[string]struct{}{}
for _, membership := range memberships {
projects[membership.ProjectID] = struct{}{}
}
filtered := items[:0]
for _, item := range items {
if _, ok := projects[item.ProjectID]; ok {
filtered = append(filtered, item)
}
}
items = filtered
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) handleAnnotationTemplateCreate(writer http.ResponseWriter, request *http.Request) {
principal, _ := currentPrincipal(request)
var payload annotationTemplateInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.ProjectID = strings.TrimSpace(payload.ProjectID)
payload.Name = strings.TrimSpace(payload.Name)
issues := validateAnnotationLabels(payload.Labels)
if payload.ProjectID == "" {
issues = append(issues, validationIssue{Field: "projectId", Message: "projectId is required"})
}
if payload.Name == "" {
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
}
if payload.Version <= 0 {
issues = append(issues, validationIssue{Field: "version", Message: "version must be positive"})
}
if payload.Prelabel.MinimumConfidence < 0 || payload.Prelabel.MinimumConfidence > 1 || math.IsNaN(payload.Prelabel.MinimumConfidence) {
issues = append(issues, validationIssue{Field: "prelabel.minimumConfidence", Message: "minimumConfidence must be between 0 and 1"})
}
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
project, err := server.store.GetProject(request.Context(), payload.ProjectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil || !server.filterByTenant(request, project.OrganizationID) {
renderValidationError(writer, validationIssue{Field: "projectId", Message: "project not found"})
return
}
item := domain.AnnotationTemplateRecord{
ID: domain.NewID("annotation-template"),
OrganizationID: project.OrganizationID,
ProjectID: project.ID,
Name: payload.Name,
Version: payload.Version,
Status: domain.AnnotationTemplateDraft,
Labels: payload.Labels,
Phrases: payload.Phrases,
Prelabel: payload.Prelabel,
CreatedByUserID: annotationPrincipalUserID(principal),
CreatedAt: nowISO(),
}
if err := server.store.UpsertAnnotationTemplate(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
saved, err := server.store.GetAnnotationTemplate(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, saved)
}
func (server *server) handleAnnotationTemplatePublish(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetAnnotationTemplate(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil || !server.ensureTenantAccess(writer, request, item.OrganizationID, "Annotation template") {
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Annotation template not found"})
}
return
}
principal, _ := currentPrincipal(request)
item.Status = domain.AnnotationTemplatePublished
item.PublishedByUserID = annotationPrincipalUserID(principal)
item.PublishedAt = nowISO()
if err := server.store.UpsertAnnotationTemplate(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, item)
}
type annotationTaskInput struct {
ProjectID string `json:"projectId"`
WorkflowID string `json:"workflowId,omitempty"`
RunID string `json:"runId,omitempty"`
ArtifactID string `json:"artifactId,omitempty"`
SourceVideo string `json:"sourceVideo"`
ClipStart float64 `json:"clipStart"`
ClipEnd float64 `json:"clipEnd"`
TemplateID string `json:"templateId"`
WorkgroupID string `json:"workgroupId"`
AssignedUserID string `json:"assignedUserId,omitempty"`
}
func (server *server) handleAnnotationTasksList(writer http.ResponseWriter, request *http.Request) {
projectID := strings.TrimSpace(request.URL.Query().Get("projectId"))
status := domain.AnnotationTaskStatus(strings.TrimSpace(request.URL.Query().Get("status")))
workgroupID := strings.TrimSpace(request.URL.Query().Get("workgroupId"))
vendorID := strings.TrimSpace(request.URL.Query().Get("vendorId"))
items, err := server.store.ListAnnotationTasks(request.Context(), server.requestOrganizationID(request), projectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
var memberships []domain.ProjectAnnotationMembershipRecord
if !annotationManager(principal) && principal != nil {
memberships, err = server.annotationMembershipsForUser(request.Context(), server.requestOrganizationID(request), projectID, principal.User.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
filtered := make([]domain.AnnotationTaskRecord, 0, len(items))
for _, item := range items {
if status != "" && item.Status != status {
continue
}
if workgroupID != "" && item.WorkgroupID != workgroupID {
continue
}
if vendorID != "" && item.VendorID != vendorID {
continue
}
if !annotationManager(principal) && !membershipAllowsTask(memberships, item) {
continue
}
filtered = append(filtered, item)
}
renderJSON(writer, http.StatusOK, map[string]any{"items": filtered})
}
func (server *server) handleAnnotationTaskGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetAnnotationTask(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Annotation task not found"})
return
}
if _, _, ok := server.ensureAnnotationTaskAccess(writer, request, *item); !ok {
return
}
revisions, revisionsErr := server.store.ListAnnotationRevisions(request.Context(), item.OrganizationID, item.ID)
reviews, reviewsErr := server.store.ListAnnotationReviews(request.Context(), item.OrganizationID, item.ID)
if revisionsErr != nil || reviewsErr != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("load annotation task history: revisions=%v reviews=%v", revisionsErr, reviewsErr))
return
}
renderJSON(writer, http.StatusOK, map[string]any{"task": item, "revisions": revisions, "reviews": reviews})
}
func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, request *http.Request) {
var payload annotationTaskInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.ProjectID = strings.TrimSpace(payload.ProjectID)
payload.SourceVideo = strings.TrimSpace(payload.SourceVideo)
payload.TemplateID = strings.TrimSpace(payload.TemplateID)
payload.WorkgroupID = strings.TrimSpace(payload.WorkgroupID)
if payload.ProjectID == "" || payload.SourceVideo == "" || payload.TemplateID == "" || payload.WorkgroupID == "" || payload.ClipStart < 0 || payload.ClipEnd <= payload.ClipStart {
renderValidationError(writer, validationIssue{Field: "task", Message: "projectId, sourceVideo, templateId, workgroupId and a valid clip range are required"})
return
}
project, projectErr := server.store.GetProject(request.Context(), payload.ProjectID)
template, templateErr := server.store.GetAnnotationTemplate(request.Context(), payload.TemplateID)
group, groupErr := server.store.GetAnnotationWorkgroup(request.Context(), payload.WorkgroupID)
if projectErr != nil || templateErr != nil || groupErr != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("load annotation task references: project=%v template=%v workgroup=%v", projectErr, templateErr, groupErr))
return
}
if project == nil || template == nil || group == nil || template.ProjectID != project.ID || !annotationOrganizationEqual(project.OrganizationID, template.OrganizationID) || !annotationOrganizationEqual(project.OrganizationID, group.OrganizationID) {
renderValidationError(writer, validationIssue{Field: "task", Message: "project, template and workgroup are incompatible"})
return
}
if template.Status != domain.AnnotationTemplatePublished {
renderValidationError(writer, validationIssue{Field: "templateId", Message: "template must be published before task assignment"})
return
}
item := domain.AnnotationTaskRecord{
ID: domain.NewID("annotation-task"),
OrganizationID: project.OrganizationID,
ProjectID: project.ID,
WorkflowID: strings.TrimSpace(payload.WorkflowID),
RunID: strings.TrimSpace(payload.RunID),
ArtifactID: strings.TrimSpace(payload.ArtifactID),
SourceVideo: payload.SourceVideo,
ClipStart: payload.ClipStart,
ClipEnd: payload.ClipEnd,
TemplateID: template.ID,
TemplateVersion: template.Version,
WorkgroupID: group.ID,
VendorID: group.VendorID,
AssignedUserID: strings.TrimSpace(payload.AssignedUserID),
Status: domain.AnnotationTaskStatusPendingAnnotation,
CreatedAt: nowISO(),
}
if item.AssignedUserID != "" {
memberships, membershipErr := server.annotationMembershipsForUser(request.Context(), project.OrganizationID, project.ID, item.AssignedUserID)
if membershipErr != nil {
renderError(writer, http.StatusInternalServerError, membershipErr)
return
}
if !membershipHasRole(memberships, item, domain.AnnotationRoleAnnotator) {
renderValidationError(writer, validationIssue{Field: "assignedUserId", Message: "assigned user must be an annotator in the selected workgroup"})
return
}
}
if err := server.store.UpsertAnnotationTask(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
_ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{
ID: domain.NewID("annotation-worklog"), OrganizationID: item.OrganizationID, ProjectID: item.ProjectID,
TaskID: item.ID, UserID: annotationPrincipalUserID(principal), WorkgroupID: item.WorkgroupID, VendorID: item.VendorID,
EventType: "assigned", TemplateVersion: item.TemplateVersion, OutputDelta: 0, CreatedAt: nowISO(),
})
renderJSON(writer, http.StatusCreated, item)
}
type annotationRevisionInput struct {
Segments []domain.AnnotationSegment `json:"segments"`
ChangeReason string `json:"changeReason,omitempty"`
DurationSeconds int64 `json:"durationSeconds,omitempty"`
}
func validateAnnotationSegments(segments []domain.AnnotationSegment, task domain.AnnotationTaskRecord) []validationIssue {
issues := make([]validationIssue, 0)
sorted := append([]domain.AnnotationSegment(nil), segments...)
sort.Slice(sorted, func(left int, right int) bool { return sorted[left].Start < sorted[right].Start })
for index, segment := range sorted {
if strings.TrimSpace(segment.LabelID) == "" || strings.TrimSpace(segment.Label) == "" {
issues = append(issues, validationIssue{Field: fmt.Sprintf("segments.%d", index), Message: "labelId and label are required"})
}
if segment.Start < task.ClipStart || segment.End > task.ClipEnd || segment.End <= segment.Start || math.IsNaN(segment.Start) || math.IsNaN(segment.End) {
issues = append(issues, validationIssue{Field: fmt.Sprintf("segments.%d", index), Message: "segment must be finite and inside the clip range"})
}
if segment.Confidence < 0 || segment.Confidence > 1 || math.IsNaN(segment.Confidence) {
issues = append(issues, validationIssue{Field: fmt.Sprintf("segments.%d.confidence", index), Message: "confidence must be between 0 and 1"})
}
if index > 0 && segment.Start < sorted[index-1].End {
issues = append(issues, validationIssue{Field: fmt.Sprintf("segments.%d", index), Message: "segments must not overlap"})
}
}
return issues
}
func (server *server) handleAnnotationRevisionCreate(writer http.ResponseWriter, request *http.Request) {
task, err := server.store.GetAnnotationTask(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if task == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Annotation task not found"})
return
}
principal, memberships, ok := server.ensureAnnotationTaskAccess(writer, request, *task)
if !ok {
return
}
if !annotationManager(principal) && !membershipHasRole(memberships, *task, domain.AnnotationRoleAnnotator) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "annotator assignment is required"})
return
}
if task.AssignedUserID != "" && task.AssignedUserID != principal.User.ID && !annotationManager(principal) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "task is assigned to another annotator"})
return
}
var payload annotationRevisionInput
if !decodeJSON(writer, request, &payload) {
return
}
if issues := validateAnnotationSegments(payload.Segments, *task); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
isRework := task.CurrentRevision > 0 || task.Status == domain.AnnotationTaskStatusRework
revisionNumber := task.CurrentRevision + 1
revision := domain.AnnotationRevisionRecord{
ID: domain.NewID("annotation-revision"), OrganizationID: task.OrganizationID, TaskID: task.ID,
Revision: revisionNumber, TemplateVersion: task.TemplateVersion, Segments: payload.Segments,
AuthorUserID: principal.User.ID, ChangeReason: strings.TrimSpace(payload.ChangeReason),
IsRework: isRework, CountsAsOutput: !isRework, DurationSeconds: payload.DurationSeconds, CreatedAt: nowISO(),
}
if err := server.store.UpsertAnnotationRevision(request.Context(), revision); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
task.CurrentRevision = revisionNumber
task.Status = domain.AnnotationTaskStatusPendingQuality
task.AssignedUserID = principal.User.ID
if err := server.store.UpsertAnnotationTask(request.Context(), *task); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
eventType := "annotation_submitted"
if isRework {
eventType = "rework_submitted"
}
outputDelta := int64(0)
if revision.CountsAsOutput {
outputDelta = 1
}
_ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{
ID: domain.NewID("annotation-worklog"), OrganizationID: task.OrganizationID, ProjectID: task.ProjectID,
TaskID: task.ID, UserID: principal.User.ID, WorkgroupID: task.WorkgroupID, VendorID: task.VendorID,
EventType: eventType, Revision: revisionNumber, TemplateVersion: task.TemplateVersion,
DurationSeconds: payload.DurationSeconds, OutputDelta: outputDelta, CreatedAt: nowISO(),
})
renderJSON(writer, http.StatusCreated, map[string]any{"task": task, "revision": revision})
}
type annotationReviewInput struct {
Stage domain.AnnotationReviewStage `json:"stage"`
Decision domain.AnnotationReviewDecision `json:"decision"`
Comment string `json:"comment,omitempty"`
DurationSeconds int64 `json:"durationSeconds,omitempty"`
}
func (server *server) handleAnnotationReviewCreate(writer http.ResponseWriter, request *http.Request) {
task, err := server.store.GetAnnotationTask(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if task == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Annotation task not found"})
return
}
principal, memberships, ok := server.ensureAnnotationTaskAccess(writer, request, *task)
if !ok {
return
}
var payload annotationReviewInput
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Stage != domain.AnnotationReviewStageQuality && payload.Stage != domain.AnnotationReviewStageAcceptance {
renderValidationError(writer, validationIssue{Field: "stage", Message: "stage must be quality or acceptance"})
return
}
if payload.Decision != domain.AnnotationReviewApproved && payload.Decision != domain.AnnotationReviewRejected {
renderValidationError(writer, validationIssue{Field: "decision", Message: "decision must be approved or rejected"})
return
}
requiredRole := domain.AnnotationRoleQualityReviewer
if payload.Stage == domain.AnnotationReviewStageAcceptance {
requiredRole = domain.AnnotationRoleAcceptanceReviewer
}
if !annotationManager(principal) && !membershipHasRole(memberships, *task, requiredRole) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": string(requiredRole) + " assignment is required"})
return
}
if task.CurrentRevision <= 0 {
renderValidationError(writer, validationIssue{Field: "revision", Message: "an annotation revision is required before review"})
return
}
existing, existingErr := server.store.ListAnnotationReviews(request.Context(), task.OrganizationID, task.ID)
if existingErr != nil {
renderError(writer, http.StatusInternalServerError, existingErr)
return
}
for _, review := range existing {
if review.Revision == task.CurrentRevision && review.Stage == payload.Stage {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "review already exists for this revision and stage"})
return
}
}
review := domain.AnnotationReviewRecord{
ID: domain.NewID("annotation-review"), OrganizationID: task.OrganizationID, TaskID: task.ID,
Revision: task.CurrentRevision, Stage: payload.Stage, Decision: payload.Decision,
ReviewerUserID: principal.User.ID, Comment: strings.TrimSpace(payload.Comment), CreatedAt: nowISO(),
}
if payload.Decision == domain.AnnotationReviewRejected {
review.NextRevision = task.CurrentRevision + 1
task.Status = domain.AnnotationTaskStatusRework
} else if payload.Stage == domain.AnnotationReviewStageQuality {
task.Status = domain.AnnotationTaskStatusPendingAcceptance
} else {
task.Status = domain.AnnotationTaskStatusAccepted
}
if err := server.store.UpsertAnnotationReview(request.Context(), review); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := server.store.UpsertAnnotationTask(request.Context(), *task); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
eventType := string(payload.Stage) + "_" + string(payload.Decision)
_ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{
ID: domain.NewID("annotation-worklog"), OrganizationID: task.OrganizationID, ProjectID: task.ProjectID,
TaskID: task.ID, UserID: principal.User.ID, WorkgroupID: task.WorkgroupID, VendorID: task.VendorID,
EventType: eventType, Revision: task.CurrentRevision, TemplateVersion: task.TemplateVersion,
DurationSeconds: payload.DurationSeconds, OutputDelta: 0, CreatedAt: nowISO(),
})
renderJSON(writer, http.StatusCreated, map[string]any{"task": task, "review": review})
}
func (server *server) handleAnnotationWorkloadSummary(writer http.ResponseWriter, request *http.Request) {
projectID := strings.TrimSpace(request.URL.Query().Get("projectId"))
events, err := server.store.ListAnnotationWorklogEvents(request.Context(), server.requestOrganizationID(request), projectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
principal, _ := currentPrincipal(request)
if !annotationManager(principal) && principal != nil {
memberships, loadErr := server.annotationMembershipsForUser(request.Context(), server.requestOrganizationID(request), projectID, principal.User.ID)
if loadErr != nil {
renderError(writer, http.StatusInternalServerError, loadErr)
return
}
allowed := map[string]struct{}{}
for _, membership := range memberships {
allowed[membership.WorkgroupID] = struct{}{}
}
filtered := events[:0]
for _, event := range events {
if _, ok := allowed[event.WorkgroupID]; ok {
filtered = append(filtered, event)
}
}
events = filtered
}
type key struct {
userID, workgroupID, vendorID string
templateVersion int
}
aggregates := map[key]*domain.AnnotationWorkloadAggregate{}
for _, event := range events {
groupKey := key{event.UserID, event.WorkgroupID, event.VendorID, event.TemplateVersion}
item := aggregates[groupKey]
if item == nil {
item = &domain.AnnotationWorkloadAggregate{UserID: event.UserID, WorkgroupID: event.WorkgroupID, VendorID: event.VendorID, TemplateVersion: event.TemplateVersion}
aggregates[groupKey] = item
}
item.DurationSeconds += event.DurationSeconds
item.OutputCount += event.OutputDelta
switch event.EventType {
case "assigned":
item.AssignedCount++
case "annotation_submitted":
item.SubmittedCount++
case "rework_submitted":
item.ReworkCount++
case "quality_approved":
item.QualityApproved++
case "quality_rejected":
item.QualityRejected++
case "acceptance_approved":
item.AcceptanceApproved++
case "acceptance_rejected":
item.AcceptanceRejected++
}
}
items := make([]domain.AnnotationWorkloadAggregate, 0, len(aggregates))
for _, item := range aggregates {
items = append(items, *item)
}
sort.Slice(items, func(left int, right int) bool {
leftKey := items[left].VendorID + "\x00" + items[left].WorkgroupID + "\x00" + items[left].UserID + "\x00" + strconv.Itoa(items[left].TemplateVersion)
rightKey := items[right].VendorID + "\x00" + items[right].WorkgroupID + "\x00" + items[right].UserID + "\x00" + strconv.Itoa(items[right].TemplateVersion)
return leftKey < rightKey
})
renderJSON(writer, http.StatusOK, domain.AnnotationWorkloadSummary{Items: items, EventCount: len(events)})
}

View File

@ -0,0 +1,43 @@
package api
import (
"testing"
"droboticflow/internal/domain"
)
func TestValidateAnnotationSegmentsRejectsOverlapAndOutOfRange(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 10, ClipEnd: 20}
issues := validateAnnotationSegments([]domain.AnnotationSegment{
{ID: "one", LabelID: "a", Label: "A", Start: 9, End: 12, Confidence: 1},
{ID: "two", LabelID: "b", Label: "B", Start: 11, End: 15, Confidence: 1},
}, task)
if len(issues) < 2 {
t.Fatalf("expected range and overlap issues, got %#v", issues)
}
}
func TestValidateAnnotationSegmentsAcceptsOrderedTimeline(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 30}
issues := validateAnnotationSegments([]domain.AnnotationSegment{
{ID: "one", LabelID: "static", Label: "静止观察", Start: 0, End: 10, Confidence: 0.9},
{ID: "two", LabelID: "steady", Label: "平稳活动", Start: 10, End: 30, Confidence: 0.8},
}, task)
if len(issues) != 0 {
t.Fatalf("unexpected issues: %#v", issues)
}
}
func TestMembershipRequiresMatchingProjectWorkgroupAndVendor(t *testing.T) {
task := domain.AnnotationTaskRecord{ProjectID: "project-a", WorkgroupID: "group-a", VendorID: "vendor-a"}
memberships := []domain.ProjectAnnotationMembershipRecord{
{ProjectID: "project-a", WorkgroupID: "group-a", VendorID: "vendor-a", AssignmentRole: domain.AnnotationRoleAnnotator},
}
if !membershipAllowsTask(memberships, task) {
t.Fatal("expected matching membership to allow task")
}
task.VendorID = "vendor-b"
if membershipAllowsTask(memberships, task) {
t.Fatal("vendor mismatch must not allow task")
}
}

View File

@ -14,19 +14,26 @@ import (
)
var reservedMongoCollections = map[string]struct{}{
"projects": {},
"operators": {},
"operatorTombstones": {},
"workflows": {},
"templates": {},
"runs": {},
"sourceStates": {},
"auditTasks": {},
"samplingTasks": {},
"users": {},
"storageConnections": {},
"mongoConnections": {},
"settings": {},
"projects": {},
"operators": {},
"operatorTombstones": {},
"workflows": {},
"templates": {},
"runs": {},
"sourceStates": {},
"auditTasks": {},
"samplingTasks": {},
"annotationWorkgroups": {},
"annotationMemberships": {},
"annotationTemplates": {},
"annotationTasks": {},
"annotationRevisions": {},
"annotationReviews": {},
"annotationWorklogEvents": {},
"users": {},
"storageConnections": {},
"mongoConnections": {},
"settings": {},
}
//go:embed builtin_operators_catalog.json

View File

@ -1309,12 +1309,31 @@ func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig
router.Get("/api/data-operations/stage-items", instance.handleDataOperationsStageItems)
router.Get("/api/data-operations/training-usage", instance.handleDataOperationsTrainingUsage)
router.Get("/api/data-operations/datasets", instance.handleDataOperationsDatasets)
router.Get("/api/annotation-workgroups", instance.handleAnnotationWorkgroupsList)
router.Get("/api/annotation-memberships", instance.handleAnnotationMembershipsList)
router.Get("/api/annotation-templates", instance.handleAnnotationTemplatesList)
router.Get("/api/annotation-tasks", instance.handleAnnotationTasksList)
router.Get("/api/annotation-tasks/{id}", instance.handleAnnotationTaskGet)
router.Get("/api/annotation-workload-summary", instance.handleAnnotationWorkloadSummary)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Post("/api/data-collection-tasks", instance.handleCollectionTaskCreate)
router.Patch("/api/data-collection-tasks/{id}", instance.handleCollectionTaskUpdate)
router.Post("/api/data-collection-tasks/{id}/start", instance.handleCollectionTaskStart)
router.Post("/api/data-collection-tasks/{id}/complete", instance.handleCollectionTaskComplete)
router.Post("/api/annotation-tasks", instance.handleAnnotationTaskCreate)
router.Post("/api/annotation-tasks/{id}/revisions", instance.handleAnnotationRevisionCreate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
router.Post("/api/annotation-workgroups", instance.handleAnnotationWorkgroupCreate)
router.Post("/api/annotation-memberships", instance.handleAnnotationMembershipCreate)
router.Post("/api/annotation-templates", instance.handleAnnotationTemplateCreate)
router.Post("/api/annotation-templates/{id}/publish", instance.handleAnnotationTemplatePublish)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleQAReviewer, domain.UserRoleAuditor, domain.UserRoleOperator))
router.Post("/api/annotation-tasks/{id}/reviews", instance.handleAnnotationReviewCreate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))

View File

@ -0,0 +1,194 @@
package domain
type AnnotationAssignmentRole string
const (
AnnotationRoleAnnotator AnnotationAssignmentRole = "annotator"
AnnotationRoleQualityReviewer AnnotationAssignmentRole = "quality_reviewer"
AnnotationRoleAcceptanceReviewer AnnotationAssignmentRole = "acceptance_reviewer"
)
type AnnotationTaskStatus string
const (
AnnotationTaskStatusPendingAcceptance AnnotationTaskStatus = "pending_acceptance"
AnnotationTaskStatusPendingAnnotation AnnotationTaskStatus = "pending_annotation"
AnnotationTaskStatusPendingQuality AnnotationTaskStatus = "pending_quality"
AnnotationTaskStatusRework AnnotationTaskStatus = "rework"
AnnotationTaskStatusAccepted AnnotationTaskStatus = "accepted"
AnnotationTaskStatusRejected AnnotationTaskStatus = "rejected"
)
type AnnotationReviewStage string
const (
AnnotationReviewStageQuality AnnotationReviewStage = "quality"
AnnotationReviewStageAcceptance AnnotationReviewStage = "acceptance"
)
type AnnotationReviewDecision string
const (
AnnotationReviewApproved AnnotationReviewDecision = "approved"
AnnotationReviewRejected AnnotationReviewDecision = "rejected"
)
type AnnotationTemplateStatus string
const (
AnnotationTemplateDraft AnnotationTemplateStatus = "draft"
AnnotationTemplatePublished AnnotationTemplateStatus = "published"
AnnotationTemplateRetired AnnotationTemplateStatus = "retired"
)
type AnnotationWorkgroupRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
Name string `json:"name" bson:"name"`
VendorID string `json:"vendorId" bson:"vendorId"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Status string `json:"status" bson:"status"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
}
type ProjectAnnotationMembershipRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
ProjectID string `json:"projectId" bson:"projectId"`
UserID string `json:"userId" bson:"userId"`
WorkgroupID string `json:"workgroupId" bson:"workgroupId"`
VendorID string `json:"vendorId" bson:"vendorId"`
AssignmentRole AnnotationAssignmentRole `json:"assignmentRole" bson:"assignmentRole"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
}
type SemanticLabelDefinition struct {
ID string `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
Aliases []string `json:"aliases,omitempty" bson:"aliases,omitempty"`
Color string `json:"color,omitempty" bson:"color,omitempty"`
}
type AnnotationPrelabelSettings struct {
Enabled bool `json:"enabled" bson:"enabled"`
MinimumConfidence float64 `json:"minimumConfidence,omitempty" bson:"minimumConfidence,omitempty"`
EnableWiLoR bool `json:"enableWilor,omitempty" bson:"enableWilor,omitempty"`
EnableHead bool `json:"enableHead,omitempty" bson:"enableHead,omitempty"`
}
type AnnotationTemplateRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
ProjectID string `json:"projectId" bson:"projectId"`
Name string `json:"name" bson:"name"`
Version int `json:"version" bson:"version"`
Status AnnotationTemplateStatus `json:"status" bson:"status"`
Labels []SemanticLabelDefinition `json:"labels" bson:"labels"`
Phrases []string `json:"phrases,omitempty" bson:"phrases,omitempty"`
Prelabel AnnotationPrelabelSettings `json:"prelabel" bson:"prelabel"`
CreatedByUserID string `json:"createdByUserId" bson:"createdByUserId"`
PublishedByUserID string `json:"publishedByUserId,omitempty" bson:"publishedByUserId,omitempty"`
PublishedAt string `json:"publishedAt,omitempty" bson:"publishedAt,omitempty"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
}
type AnnotationSegment struct {
ID string `json:"id" bson:"id"`
LabelID string `json:"labelId" bson:"labelId"`
Label string `json:"label" bson:"label"`
Start float64 `json:"start" bson:"start"`
End float64 `json:"end" bson:"end"`
Confidence float64 `json:"confidence,omitempty" bson:"confidence,omitempty"`
Phrases []string `json:"phrases,omitempty" bson:"phrases,omitempty"`
Source string `json:"source,omitempty" bson:"source,omitempty"`
}
type AnnotationTaskRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
ProjectID string `json:"projectId" bson:"projectId"`
WorkflowID string `json:"workflowId,omitempty" bson:"workflowId,omitempty"`
RunID string `json:"runId,omitempty" bson:"runId,omitempty"`
ArtifactID string `json:"artifactId,omitempty" bson:"artifactId,omitempty"`
SourceVideo string `json:"sourceVideo" bson:"sourceVideo"`
ClipStart float64 `json:"clipStart" bson:"clipStart"`
ClipEnd float64 `json:"clipEnd" bson:"clipEnd"`
TemplateID string `json:"templateId" bson:"templateId"`
TemplateVersion int `json:"templateVersion" bson:"templateVersion"`
WorkgroupID string `json:"workgroupId" bson:"workgroupId"`
VendorID string `json:"vendorId" bson:"vendorId"`
AssignedUserID string `json:"assignedUserId,omitempty" bson:"assignedUserId,omitempty"`
Status AnnotationTaskStatus `json:"status" bson:"status"`
CurrentRevision int `json:"currentRevision" bson:"currentRevision"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
UpdatedAt string `json:"updatedAt" bson:"updatedAt"`
}
type AnnotationRevisionRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
TaskID string `json:"taskId" bson:"taskId"`
Revision int `json:"revision" bson:"revision"`
TemplateVersion int `json:"templateVersion" bson:"templateVersion"`
Segments []AnnotationSegment `json:"segments" bson:"segments"`
AuthorUserID string `json:"authorUserId" bson:"authorUserId"`
ChangeReason string `json:"changeReason,omitempty" bson:"changeReason,omitempty"`
IsRework bool `json:"isRework" bson:"isRework"`
CountsAsOutput bool `json:"countsAsOutput" bson:"countsAsOutput"`
DurationSeconds int64 `json:"durationSeconds,omitempty" bson:"durationSeconds,omitempty"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
}
type AnnotationReviewRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
TaskID string `json:"taskId" bson:"taskId"`
Revision int `json:"revision" bson:"revision"`
Stage AnnotationReviewStage `json:"stage" bson:"stage"`
Decision AnnotationReviewDecision `json:"decision" bson:"decision"`
ReviewerUserID string `json:"reviewerUserId" bson:"reviewerUserId"`
Comment string `json:"comment,omitempty" bson:"comment,omitempty"`
NextRevision int `json:"nextRevision,omitempty" bson:"nextRevision,omitempty"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
}
type AnnotationWorklogEventRecord struct {
ID string `json:"id" bson:"id"`
OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"`
ProjectID string `json:"projectId" bson:"projectId"`
TaskID string `json:"taskId" bson:"taskId"`
UserID string `json:"userId" bson:"userId"`
WorkgroupID string `json:"workgroupId" bson:"workgroupId"`
VendorID string `json:"vendorId" bson:"vendorId"`
EventType string `json:"eventType" bson:"eventType"`
Revision int `json:"revision,omitempty" bson:"revision,omitempty"`
TemplateVersion int `json:"templateVersion,omitempty" bson:"templateVersion,omitempty"`
DurationSeconds int64 `json:"durationSeconds,omitempty" bson:"durationSeconds,omitempty"`
OutputDelta int64 `json:"outputDelta" bson:"outputDelta"`
CreatedAt string `json:"createdAt" bson:"createdAt"`
}
type AnnotationWorkloadAggregate struct {
UserID string `json:"userId"`
WorkgroupID string `json:"workgroupId"`
VendorID string `json:"vendorId"`
TemplateVersion int `json:"templateVersion"`
AssignedCount int64 `json:"assignedCount"`
SubmittedCount int64 `json:"submittedCount"`
QualityApproved int64 `json:"qualityApproved"`
QualityRejected int64 `json:"qualityRejected"`
AcceptanceApproved int64 `json:"acceptanceApproved"`
AcceptanceRejected int64 `json:"acceptanceRejected"`
ReworkCount int64 `json:"reworkCount"`
OutputCount int64 `json:"outputCount"`
DurationSeconds int64 `json:"durationSeconds"`
}
type AnnotationWorkloadSummary struct {
Items []AnnotationWorkloadAggregate `json:"items"`
EventCount int `json:"eventCount"`
}

View File

@ -0,0 +1,461 @@
package store
import (
"context"
"sort"
"strings"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func annotationOrganizationMatches(actual string, expected string) bool {
return normalizedOrganizationID(actual) == normalizedOrganizationID(expected)
}
func sortAnnotationRecords[T any](items []T, createdAt func(T) string) {
sort.Slice(items, func(left int, right int) bool { return createdAt(items[left]) > createdAt(items[right]) })
}
func (store *MemoryStore) ListAnnotationWorkgroups(_ context.Context, organizationID string) ([]domain.AnnotationWorkgroupRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationWorkgroupRecord, 0)
for _, item := range store.annotationWorkgroups {
if annotationOrganizationMatches(item.OrganizationID, organizationID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationWorkgroupRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) GetAnnotationWorkgroup(_ context.Context, id string) (*domain.AnnotationWorkgroupRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationWorkgroups[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationWorkgroup(_ context.Context, item domain.AnnotationWorkgroupRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-workgroup")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = "active"
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationWorkgroups[item.ID] = item
return nil
}
func (store *MemoryStore) ListProjectAnnotationMemberships(_ context.Context, organizationID string, projectID string) ([]domain.ProjectAnnotationMembershipRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ProjectAnnotationMembershipRecord, 0)
for _, item := range store.annotationMemberships {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.ProjectAnnotationMembershipRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertProjectAnnotationMembership(_ context.Context, item domain.ProjectAnnotationMembershipRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-membership")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationMemberships[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationTemplates(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationTemplateRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationTemplateRecord, 0)
for _, item := range store.annotationTemplates {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTemplateRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) GetAnnotationTemplate(_ context.Context, id string) (*domain.AnnotationTemplateRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationTemplates[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationTemplate(_ context.Context, item domain.AnnotationTemplateRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-template")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Version <= 0 {
item.Version = 1
}
if item.Status == "" {
item.Status = domain.AnnotationTemplateDraft
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationTemplates[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationTasks(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationTaskRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationTaskRecord, 0)
for _, item := range store.annotationTasks {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTaskRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) GetAnnotationTask(_ context.Context, id string) (*domain.AnnotationTaskRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationTasks[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationTask(_ context.Context, item domain.AnnotationTaskRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-task")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = domain.AnnotationTaskStatusPendingAnnotation
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationTasks[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationRevisions(_ context.Context, organizationID string, taskID string) ([]domain.AnnotationRevisionRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationRevisionRecord, 0)
for _, item := range store.annotationRevisions {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
items = append(items, item)
}
}
sort.Slice(items, func(left int, right int) bool {
if items[left].TaskID != items[right].TaskID {
return items[left].TaskID < items[right].TaskID
}
return items[left].Revision < items[right].Revision
})
return items, nil
}
func (store *MemoryStore) UpsertAnnotationRevision(_ context.Context, item domain.AnnotationRevisionRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if item.ID == "" {
item.ID = domain.NewID("annotation-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.annotationRevisions[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationReviews(_ context.Context, organizationID string, taskID string) ([]domain.AnnotationReviewRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationReviewRecord, 0)
for _, item := range store.annotationReviews {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationReviewRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertAnnotationReview(_ context.Context, item domain.AnnotationReviewRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if item.ID == "" {
item.ID = domain.NewID("annotation-review")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.annotationReviews[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationWorklogEvents(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationWorklogEventRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationWorklogEventRecord, 0)
for _, item := range store.annotationWorklogEvents {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationWorklogEventRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertAnnotationWorklogEvent(_ context.Context, item domain.AnnotationWorklogEventRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if item.ID == "" {
item.ID = domain.NewID("annotation-worklog")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.annotationWorklogEvents[item.ID] = item
return nil
}
func mongoAnnotationFilter(organizationID string, extra bson.M) bson.M {
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
for key, value := range extra {
filter[key] = value
}
return filter
}
func mongoGetByID[T any](ctx context.Context, collection *mongo.Collection, id string) (*T, error) {
var item T
if err := collection.FindOne(ctx, bson.M{"id": strings.TrimSpace(id)}).Decode(&item); err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return &item, nil
}
func mongoList[T any](ctx context.Context, collection *mongo.Collection, filter bson.M, sortFields bson.D) ([]T, error) {
cursor, err := collection.Find(ctx, filter, options.Find().SetSort(sortFields))
if err != nil {
return nil, err
}
return decodeMany[T](ctx, cursor)
}
func mongoUpsert(ctx context.Context, collection *mongo.Collection, id string, item any) error {
_, err := collection.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": item}, options.Update().SetUpsert(true))
return err
}
func (store *MongoStore) ListAnnotationWorkgroups(ctx context.Context, organizationID string) ([]domain.AnnotationWorkgroupRecord, error) {
return mongoList[domain.AnnotationWorkgroupRecord](ctx, store.annotationWorkgroups, mongoAnnotationFilter(organizationID, bson.M{}), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) GetAnnotationWorkgroup(ctx context.Context, id string) (*domain.AnnotationWorkgroupRecord, error) {
return mongoGetByID[domain.AnnotationWorkgroupRecord](ctx, store.annotationWorkgroups, id)
}
func (store *MongoStore) UpsertAnnotationWorkgroup(ctx context.Context, item domain.AnnotationWorkgroupRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-workgroup")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = "active"
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationWorkgroups, item.ID, item)
}
func (store *MongoStore) ListProjectAnnotationMemberships(ctx context.Context, organizationID string, projectID string) ([]domain.ProjectAnnotationMembershipRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.ProjectAnnotationMembershipRecord](ctx, store.annotationMemberships, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertProjectAnnotationMembership(ctx context.Context, item domain.ProjectAnnotationMembershipRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-membership")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationMemberships, item.ID, item)
}
func (store *MongoStore) ListAnnotationTemplates(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationTemplateRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.AnnotationTemplateRecord](ctx, store.annotationTemplates, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) GetAnnotationTemplate(ctx context.Context, id string) (*domain.AnnotationTemplateRecord, error) {
return mongoGetByID[domain.AnnotationTemplateRecord](ctx, store.annotationTemplates, id)
}
func (store *MongoStore) UpsertAnnotationTemplate(ctx context.Context, item domain.AnnotationTemplateRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-template")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Version <= 0 {
item.Version = 1
}
if item.Status == "" {
item.Status = domain.AnnotationTemplateDraft
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationTemplates, item.ID, item)
}
func (store *MongoStore) ListAnnotationTasks(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationTaskRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.AnnotationTaskRecord](ctx, store.annotationTasks, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) GetAnnotationTask(ctx context.Context, id string) (*domain.AnnotationTaskRecord, error) {
return mongoGetByID[domain.AnnotationTaskRecord](ctx, store.annotationTasks, id)
}
func (store *MongoStore) UpsertAnnotationTask(ctx context.Context, item domain.AnnotationTaskRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-task")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = domain.AnnotationTaskStatusPendingAnnotation
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationTasks, item.ID, item)
}
func (store *MongoStore) ListAnnotationRevisions(ctx context.Context, organizationID string, taskID string) ([]domain.AnnotationRevisionRecord, error) {
extra := bson.M{}
if taskID != "" {
extra["taskId"] = taskID
}
return mongoList[domain.AnnotationRevisionRecord](ctx, store.annotationRevisions, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "taskId", Value: 1}, {Key: "revision", Value: 1}})
}
func (store *MongoStore) UpsertAnnotationRevision(ctx context.Context, item domain.AnnotationRevisionRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return mongoUpsert(ctx, store.annotationRevisions, item.ID, item)
}
func (store *MongoStore) ListAnnotationReviews(ctx context.Context, organizationID string, taskID string) ([]domain.AnnotationReviewRecord, error) {
extra := bson.M{}
if taskID != "" {
extra["taskId"] = taskID
}
return mongoList[domain.AnnotationReviewRecord](ctx, store.annotationReviews, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertAnnotationReview(ctx context.Context, item domain.AnnotationReviewRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-review")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return mongoUpsert(ctx, store.annotationReviews, item.ID, item)
}
func (store *MongoStore) ListAnnotationWorklogEvents(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationWorklogEventRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.AnnotationWorklogEventRecord](ctx, store.annotationWorklogEvents, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertAnnotationWorklogEvent(ctx context.Context, item domain.AnnotationWorklogEventRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-worklog")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return mongoUpsert(ctx, store.annotationWorklogEvents, item.ID, item)
}

View File

@ -0,0 +1,61 @@
package store
import (
"context"
"testing"
"droboticflow/internal/domain"
)
func TestMemoryStoreAnnotationOperationsRoundTrip(t *testing.T) {
ctx := context.Background()
dataStore := NewMemoryStore()
group := domain.AnnotationWorkgroupRecord{ID: "group-a", OrganizationID: "org-a", Name: "供应商 A", VendorID: "vendor-a"}
if err := dataStore.UpsertAnnotationWorkgroup(ctx, group); err != nil {
t.Fatalf("upsert workgroup: %v", err)
}
template := domain.AnnotationTemplateRecord{
ID: "template-a", OrganizationID: "org-a", ProjectID: "project-a", Name: "语义模板", Version: 1,
Status: domain.AnnotationTemplatePublished, Labels: []domain.SemanticLabelDefinition{{ID: "static", Name: "静止观察"}},
}
if err := dataStore.UpsertAnnotationTemplate(ctx, template); err != nil {
t.Fatalf("upsert template: %v", err)
}
task := domain.AnnotationTaskRecord{
ID: "task-a", OrganizationID: "org-a", ProjectID: "project-a", SourceVideo: "video.mp4",
ClipStart: 0, ClipEnd: 30, TemplateID: template.ID, TemplateVersion: 1,
WorkgroupID: group.ID, VendorID: group.VendorID, Status: domain.AnnotationTaskStatusPendingAnnotation,
}
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
t.Fatalf("upsert task: %v", err)
}
if err := dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{
ID: "revision-a", OrganizationID: "org-a", TaskID: task.ID, Revision: 1, TemplateVersion: 1,
Segments: []domain.AnnotationSegment{{ID: "segment-a", LabelID: "static", Label: "静止观察", Start: 0, End: 5}},
CountsAsOutput: true,
}); err != nil {
t.Fatalf("upsert revision: %v", err)
}
if err := dataStore.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{
ID: "event-a", OrganizationID: "org-a", ProjectID: "project-a", TaskID: task.ID,
UserID: "user-a", WorkgroupID: group.ID, VendorID: group.VendorID, EventType: "annotation_submitted", OutputDelta: 1,
}); err != nil {
t.Fatalf("upsert worklog: %v", err)
}
tasks, err := dataStore.ListAnnotationTasks(ctx, "org-a", "project-a")
if err != nil || len(tasks) != 1 || tasks[0].ID != task.ID {
t.Fatalf("list tasks: items=%#v err=%v", tasks, err)
}
if foreign, err := dataStore.ListAnnotationTasks(ctx, "org-b", "project-a"); err != nil || len(foreign) != 0 {
t.Fatalf("cross-organization task leak: items=%#v err=%v", foreign, err)
}
revisions, err := dataStore.ListAnnotationRevisions(ctx, "org-a", task.ID)
if err != nil || len(revisions) != 1 || !revisions[0].CountsAsOutput {
t.Fatalf("list revisions: items=%#v err=%v", revisions, err)
}
events, err := dataStore.ListAnnotationWorklogEvents(ctx, "org-a", "project-a")
if err != nil || len(events) != 1 || events[0].OutputDelta != 1 {
t.Fatalf("list worklog: items=%#v err=%v", events, err)
}
}

View File

@ -26,6 +26,13 @@ type MemoryStore struct {
dataMetricFacts map[string]domain.DataMetricFactRecord
auditTasks map[string]domain.AuditTaskRecord
samplingTasks map[string]domain.SamplingTaskRecord
annotationWorkgroups map[string]domain.AnnotationWorkgroupRecord
annotationMemberships map[string]domain.ProjectAnnotationMembershipRecord
annotationTemplates map[string]domain.AnnotationTemplateRecord
annotationTasks map[string]domain.AnnotationTaskRecord
annotationRevisions map[string]domain.AnnotationRevisionRecord
annotationReviews map[string]domain.AnnotationReviewRecord
annotationWorklogEvents map[string]domain.AnnotationWorklogEventRecord
users map[string]domain.UserRecord
sessions map[string]domain.SessionRecord
storageConnections map[string]domain.StorageConnectionRecord
@ -66,6 +73,13 @@ func NewMemoryStore() *MemoryStore {
dataMetricFacts: map[string]domain.DataMetricFactRecord{},
auditTasks: map[string]domain.AuditTaskRecord{},
samplingTasks: map[string]domain.SamplingTaskRecord{},
annotationWorkgroups: map[string]domain.AnnotationWorkgroupRecord{},
annotationMemberships: map[string]domain.ProjectAnnotationMembershipRecord{},
annotationTemplates: map[string]domain.AnnotationTemplateRecord{},
annotationTasks: map[string]domain.AnnotationTaskRecord{},
annotationRevisions: map[string]domain.AnnotationRevisionRecord{},
annotationReviews: map[string]domain.AnnotationReviewRecord{},
annotationWorklogEvents: map[string]domain.AnnotationWorklogEventRecord{},
users: map[string]domain.UserRecord{},
sessions: map[string]domain.SessionRecord{},
storageConnections: map[string]domain.StorageConnectionRecord{},
@ -339,6 +353,41 @@ func (store *MemoryStore) DeleteOrganizationCascade(_ context.Context, id string
delete(store.samplingTasks, key)
}
}
for key, item := range store.annotationWorkgroups {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationWorkgroups, key)
}
}
for key, item := range store.annotationMemberships {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationMemberships, key)
}
}
for key, item := range store.annotationTemplates {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationTemplates, key)
}
}
for key, item := range store.annotationTasks {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationTasks, key)
}
}
for key, item := range store.annotationRevisions {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationRevisions, key)
}
}
for key, item := range store.annotationReviews {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationReviews, key)
}
}
for key, item := range store.annotationWorklogEvents {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.annotationWorklogEvents, key)
}
}
for key, item := range store.storageConnections {
if organizationMatches(item.OrganizationID, organizationID) {
delete(store.storageConnections, key)

View File

@ -33,6 +33,13 @@ type MongoStore struct {
dataMetricFacts *mongo.Collection
auditTasks *mongo.Collection
samplingTasks *mongo.Collection
annotationWorkgroups *mongo.Collection
annotationMemberships *mongo.Collection
annotationTemplates *mongo.Collection
annotationTasks *mongo.Collection
annotationRevisions *mongo.Collection
annotationReviews *mongo.Collection
annotationWorklogEvents *mongo.Collection
users *mongo.Collection
sessions *mongo.Collection
storageConnections *mongo.Collection
@ -73,6 +80,13 @@ func NewMongoStore(db *mongo.Database) *MongoStore {
dataMetricFacts: db.Collection("dataMetricFacts"),
auditTasks: db.Collection("auditTasks"),
samplingTasks: db.Collection("samplingTasks"),
annotationWorkgroups: db.Collection("annotationWorkgroups"),
annotationMemberships: db.Collection("annotationMemberships"),
annotationTemplates: db.Collection("annotationTemplates"),
annotationTasks: db.Collection("annotationTasks"),
annotationRevisions: db.Collection("annotationRevisions"),
annotationReviews: db.Collection("annotationReviews"),
annotationWorklogEvents: db.Collection("annotationWorklogEvents"),
users: db.Collection("users"),
sessions: db.Collection("sessions"),
storageConnections: db.Collection("storageConnections"),
@ -231,6 +245,13 @@ func (store *MongoStore) DeleteOrganizationCascade(ctx context.Context, id strin
store.sourceStates,
store.auditTasks,
store.samplingTasks,
store.annotationWorkgroups,
store.annotationMemberships,
store.annotationTemplates,
store.annotationTasks,
store.annotationRevisions,
store.annotationReviews,
store.annotationWorklogEvents,
store.storageConnections,
store.mongoConnections,
store.objectSyncSpaces,

View File

@ -125,6 +125,37 @@ func (store *MongoStore) EnsureIndexes(ctx context.Context) error {
index("status_createdAt", bson.D{{Key: "status", Value: 1}, {Key: "createdAt", Value: -1}}),
index("workflowId_createdAt", bson.D{{Key: "workflowId", Value: 1}, {Key: "createdAt", Value: -1}}),
},
store.annotationWorkgroups: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
index("organizationId_vendorId_name", bson.D{{Key: "organizationId", Value: 1}, {Key: "vendorId", Value: 1}, {Key: "name", Value: 1}}),
},
store.annotationMemberships: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
uniqueIndex("organizationId_projectId_userId_role_unique", bson.D{{Key: "organizationId", Value: 1}, {Key: "projectId", Value: 1}, {Key: "userId", Value: 1}, {Key: "assignmentRole", Value: 1}}),
index("organizationId_projectId_workgroupId", bson.D{{Key: "organizationId", Value: 1}, {Key: "projectId", Value: 1}, {Key: "workgroupId", Value: 1}}),
},
store.annotationTemplates: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
uniqueIndex("organizationId_projectId_name_version_unique", bson.D{{Key: "organizationId", Value: 1}, {Key: "projectId", Value: 1}, {Key: "name", Value: 1}, {Key: "version", Value: 1}}),
},
store.annotationTasks: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
index("organizationId_projectId_workgroupId_status", bson.D{{Key: "organizationId", Value: 1}, {Key: "projectId", Value: 1}, {Key: "workgroupId", Value: 1}, {Key: "status", Value: 1}}),
index("runId_artifactId", bson.D{{Key: "runId", Value: 1}, {Key: "artifactId", Value: 1}}),
},
store.annotationRevisions: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
uniqueIndex("organizationId_taskId_revision_unique", bson.D{{Key: "organizationId", Value: 1}, {Key: "taskId", Value: 1}, {Key: "revision", Value: 1}}),
},
store.annotationReviews: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
uniqueIndex("organizationId_taskId_revision_stage_unique", bson.D{{Key: "organizationId", Value: 1}, {Key: "taskId", Value: 1}, {Key: "revision", Value: 1}, {Key: "stage", Value: 1}}),
},
store.annotationWorklogEvents: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
index("organizationId_projectId_createdAt", bson.D{{Key: "organizationId", Value: 1}, {Key: "projectId", Value: 1}, {Key: "createdAt", Value: -1}}),
index("organizationId_vendorId_userId_createdAt", bson.D{{Key: "organizationId", Value: 1}, {Key: "vendorId", Value: 1}, {Key: "userId", Value: 1}, {Key: "createdAt", Value: -1}}),
},
store.users: {
uniqueIndex("id_unique", bson.D{{Key: "id", Value: 1}}),
uniqueIndex("email_unique", bson.D{{Key: "email", Value: 1}}),

View File

@ -126,6 +126,24 @@ type Store interface {
CreateSamplingTask(context.Context, domain.SamplingTaskRecord) (*domain.SamplingTaskRecord, error)
UpsertSamplingTask(context.Context, domain.SamplingTaskRecord) error
ListAnnotationWorkgroups(context.Context, string) ([]domain.AnnotationWorkgroupRecord, error)
GetAnnotationWorkgroup(context.Context, string) (*domain.AnnotationWorkgroupRecord, error)
UpsertAnnotationWorkgroup(context.Context, domain.AnnotationWorkgroupRecord) error
ListProjectAnnotationMemberships(context.Context, string, string) ([]domain.ProjectAnnotationMembershipRecord, error)
UpsertProjectAnnotationMembership(context.Context, domain.ProjectAnnotationMembershipRecord) error
ListAnnotationTemplates(context.Context, string, string) ([]domain.AnnotationTemplateRecord, error)
GetAnnotationTemplate(context.Context, string) (*domain.AnnotationTemplateRecord, error)
UpsertAnnotationTemplate(context.Context, domain.AnnotationTemplateRecord) error
ListAnnotationTasks(context.Context, string, string) ([]domain.AnnotationTaskRecord, error)
GetAnnotationTask(context.Context, string) (*domain.AnnotationTaskRecord, error)
UpsertAnnotationTask(context.Context, domain.AnnotationTaskRecord) error
ListAnnotationRevisions(context.Context, string, string) ([]domain.AnnotationRevisionRecord, error)
UpsertAnnotationRevision(context.Context, domain.AnnotationRevisionRecord) error
ListAnnotationReviews(context.Context, string, string) ([]domain.AnnotationReviewRecord, error)
UpsertAnnotationReview(context.Context, domain.AnnotationReviewRecord) error
ListAnnotationWorklogEvents(context.Context, string, string) ([]domain.AnnotationWorklogEventRecord, error)
UpsertAnnotationWorklogEvent(context.Context, domain.AnnotationWorklogEventRecord) error
ListUsers(context.Context) ([]domain.UserRecord, error)
ListUsersByOrganization(context.Context, string) ([]domain.UserRecord, error)
GetUser(context.Context, string) (*domain.UserRecord, error)

View File

@ -92,6 +92,7 @@ const ObjectStorageBrowser = lazyRoute(() => import("./pages/ObjectStorageBrowse
const ObjectSync = lazyRoute(() => import("./pages/ObjectSync"));
const LegacyDataAssetsRedirect = lazyRoute(() => import("./pages/LegacyDataAssetsRedirect"));
const Sampling = lazyRoute(() => import("./pages/Sampling"));
const AnnotationOperations = lazyRoute(() => import("./pages/AnnotationOperations"));
function RouteFallback() {
return <div className="flex min-h-[320px] items-center justify-center text-sm text-slate-400">正在加载页面...</div>;
@ -119,6 +120,7 @@ export default function App() {
<Route path="/source-data" element={<LegacyDataAssetsRedirect />} />
<Route path="/packages" element={<LegacyDataAssetsRedirect />} />
<Route path="/sampling" element={<Sampling />} />
<Route path="/annotation-operations" element={<AnnotationOperations />} />
<Route path="/monitoring" element={<Monitoring />} />
<Route path="/audit" element={<Audit />} />
<Route path="/settings" element={<Settings />} />

View File

@ -23,6 +23,7 @@ import {
CloudUpload,
DatabaseZap,
BarChart3,
Tags,
LogOut,
Loader2,
} from "lucide-react";
@ -78,6 +79,12 @@ const navItems = [
},
{ icon: ClipboardCheck, label: "任务审核", path: "/audit", roles: ["admin", "qa_reviewer", "auditor"] },
{ icon: ClipboardList, label: "抽检任务", path: "/sampling", roles: ["admin", "qa_reviewer", "operator"] },
{
icon: Tags,
label: "标注运营",
path: "/annotation-operations",
roles: ["admin", "pipeline_engineer", "data_operator", "qa_reviewer", "auditor", "operator"],
},
{ icon: Users, label: "用户管理", path: "/users", roles: ["admin"] },
{ icon: Settings, label: "系统配置", path: "/settings", roles: ["admin"] },
] as const;

View File

@ -0,0 +1,47 @@
export function normalizeAnnotationSegments(segments, clipStart, clipEnd) {
const start = Number(clipStart) || 0;
const end = Number(clipEnd) || start;
return (Array.isArray(segments) ? segments : [])
.map((segment, index) => ({
id: String(segment?.id || `segment-${index + 1}`),
labelId: String(segment?.labelId || "").trim(),
label: String(segment?.label || "").trim(),
start: Number(segment?.start),
end: Number(segment?.end),
confidence: Number.isFinite(Number(segment?.confidence)) ? Number(segment.confidence) : 1,
phrases: Array.isArray(segment?.phrases) ? segment.phrases.map(String) : [],
source: String(segment?.source || "manual"),
}))
.filter(
(segment) =>
segment.labelId &&
segment.label &&
Number.isFinite(segment.start) &&
Number.isFinite(segment.end) &&
segment.start >= start &&
segment.end <= end &&
segment.end > segment.start,
)
.sort((left, right) => left.start - right.start);
}
export function annotationSegmentPosition(segment, clipStart, clipEnd) {
const start = Number(clipStart) || 0;
const duration = Math.max((Number(clipEnd) || start) - start, 0.001);
const left = Math.max(0, Math.min(100, ((Number(segment.start) - start) / duration) * 100));
const width = Math.max(0.5, Math.min(100 - left, ((Number(segment.end) - Number(segment.start)) / duration) * 100));
return { left, width };
}
export function summarizeAnnotationWorkload(items) {
return (Array.isArray(items) ? items : []).reduce(
(summary, item) => ({
outputCount: summary.outputCount + Number(item?.outputCount || 0),
reworkCount: summary.reworkCount + Number(item?.reworkCount || 0),
approvedCount: summary.approvedCount + Number(item?.qualityApproved || 0) + Number(item?.acceptanceApproved || 0),
rejectedCount: summary.rejectedCount + Number(item?.qualityRejected || 0) + Number(item?.acceptanceRejected || 0),
durationSeconds: summary.durationSeconds + Number(item?.durationSeconds || 0),
}),
{ outputCount: 0, reworkCount: 0, approvedCount: 0, rejectedCount: 0, durationSeconds: 0 },
);
}

View File

@ -0,0 +1,870 @@
import React, { useCallback, useEffect, useMemo, useState } from "react";
import {
CheckCircle2,
ClipboardCheck,
Clock3,
Layers3,
Loader2,
Plus,
RefreshCw,
RotateCcw,
Send,
Tags,
UsersRound,
XCircle,
} from "lucide-react";
import type {
AnnotationSegment,
AnnotationTaskDetail,
AnnotationTaskRecord,
AnnotationTemplate,
AnnotationWorkgroup,
AnnotationWorkloadSummary,
Project,
ProjectAnnotationMembership,
} from "../types";
import { useUser } from "../context/UserContext";
import { useTimeZone } from "../context/TimeZoneContext";
import { annotationOperationsApi } from "../services/api/annotation-operations";
import { projectsApi } from "../services/api/projects";
import {
annotationSegmentPosition,
normalizeAnnotationSegments,
summarizeAnnotationWorkload,
} from "../lib/annotation-operations.js";
import { formatCompactDateTime } from "../lib/datetime";
import { getActionErrorMessage } from "../lib/action-errors.js";
type Tab = "queue" | "templates" | "groups" | "workload";
const statusLabels: Record<string, string> = {
pending_annotation: "待标注",
pending_quality: "待质检",
pending_acceptance: "待验收",
rework: "返工中",
accepted: "已验收",
rejected: "已拒绝",
};
const emptyTemplate = {
name: "泛宇语义模板",
version: 1,
labels: "静止观察,平稳活动,快速移动",
phrases: "手部,前景目标",
};
const emptyTask = {
sourceVideo: "",
clipStart: "0",
clipEnd: "30",
workgroupId: "",
templateId: "",
assignedUserId: "",
};
const panelClass = "rounded-2xl border border-[#2a2b2f] bg-[#151619]";
const inputClass =
"w-full rounded-lg border border-[#2a2b2f] bg-[#0a0a0a] px-3 py-2 text-sm text-white outline-none focus:border-blue-500";
export default function AnnotationOperations() {
const { users, role } = useUser();
const { timeZone } = useTimeZone();
const [tab, setTab] = useState<Tab>("queue");
const [projects, setProjects] = useState<Project[]>([]);
const [projectId, setProjectId] = useState("");
const [workgroups, setWorkgroups] = useState<AnnotationWorkgroup[]>([]);
const [memberships, setMemberships] = useState<ProjectAnnotationMembership[]>([]);
const [templates, setTemplates] = useState<AnnotationTemplate[]>([]);
const [tasks, setTasks] = useState<AnnotationTaskRecord[]>([]);
const [workload, setWorkload] = useState<AnnotationWorkloadSummary>({ items: [], eventCount: 0 });
const [selectedTaskId, setSelectedTaskId] = useState("");
const [detail, setDetail] = useState<AnnotationTaskDetail | null>(null);
const [segments, setSegments] = useState<AnnotationSegment[]>([]);
const [segmentDraft, setSegmentDraft] = useState({ labelId: "", label: "", start: "0", end: "1" });
const [reviewComment, setReviewComment] = useState("");
const [groupDraft, setGroupDraft] = useState({ name: "", vendorId: "", description: "" });
const [membershipDraft, setMembershipDraft] = useState({
userId: "",
workgroupId: "",
assignmentRole: "annotator" as ProjectAnnotationMembership["assignmentRole"],
});
const [templateDraft, setTemplateDraft] = useState(emptyTemplate);
const [taskDraft, setTaskDraft] = useState(emptyTask);
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [error, setError] = useState("");
const isManager = role === "admin" || role === "pipeline_engineer" || role === "operator";
const loadBase = useCallback(async () => {
setLoading(true);
setError("");
try {
const [nextProjects, nextGroups] = await Promise.all([
projectsApi.list(),
annotationOperationsApi.listWorkgroups(),
]);
setProjects(nextProjects);
setWorkgroups(nextGroups);
setProjectId((current) => current || nextProjects[0]?.id || "");
} catch (loadError) {
setError(getActionErrorMessage(loadError, "加载标注运营基础数据失败"));
} finally {
setLoading(false);
}
}, []);
const loadProject = useCallback(async () => {
if (!projectId) {
setMemberships([]);
setTemplates([]);
setTasks([]);
setWorkload({ items: [], eventCount: 0 });
return;
}
setLoading(true);
setError("");
try {
const [nextMemberships, nextTemplates, nextTasks, nextWorkload] = await Promise.all([
annotationOperationsApi.listMemberships(projectId),
annotationOperationsApi.listTemplates(projectId),
annotationOperationsApi.listTasks({ projectId }),
annotationOperationsApi.workloadSummary(projectId),
]);
setMemberships(nextMemberships);
setTemplates(nextTemplates);
setTasks(nextTasks);
setWorkload(nextWorkload);
setSelectedTaskId((current) =>
nextTasks.some((item) => item.id === current) ? current : nextTasks[0]?.id || "",
);
} catch (loadError) {
setError(getActionErrorMessage(loadError, "加载项目标注运营数据失败"));
} finally {
setLoading(false);
}
}, [projectId]);
useEffect(() => {
void loadBase();
}, [loadBase]);
useEffect(() => {
void loadProject();
}, [loadProject]);
useEffect(() => {
if (!selectedTaskId) {
setDetail(null);
setSegments([]);
return;
}
annotationOperationsApi
.getTask(selectedTaskId)
.then((next) => {
setDetail(next);
const latest = next.revisions[next.revisions.length - 1];
setSegments(latest?.segments || []);
})
.catch((loadError) => setError(getActionErrorMessage(loadError, "加载标注任务详情失败")));
}, [selectedTaskId]);
const selectedTemplate = useMemo(
() =>
templates.find((item) => item.id === detail?.task.templateId) ||
templates.find((item) => item.status === "published") ||
null,
[detail?.task.templateId, templates],
);
const workloadTotals = useMemo(() => summarizeAnnotationWorkload(workload.items), [workload.items]);
const addSegment = () => {
if (!detail) return;
const label = selectedTemplate?.labels.find((item) => item.id === segmentDraft.labelId);
const next = normalizeAnnotationSegments(
[
...segments,
{
id: `segment-${Date.now()}`,
labelId: label?.id || segmentDraft.labelId,
label: label?.name || segmentDraft.label,
start: Number(segmentDraft.start),
end: Number(segmentDraft.end),
confidence: 1,
source: "manual",
},
],
detail.task.clipStart,
detail.task.clipEnd,
);
setSegments(next);
};
const submitRevision = async () => {
if (!detail || segments.length === 0) return;
setSaving(true);
setError("");
try {
await annotationOperationsApi.submitRevision(detail.task.id, {
segments,
changeReason: detail.task.currentRevision > 0 ? "人工返工修订" : "首轮人工标注",
});
await loadProject();
setDetail(await annotationOperationsApi.getTask(detail.task.id));
} catch (saveError) {
setError(getActionErrorMessage(saveError, "提交标注版本失败"));
} finally {
setSaving(false);
}
};
const review = async (stage: "quality" | "acceptance", decision: "approved" | "rejected") => {
if (!detail) return;
setSaving(true);
setError("");
try {
await annotationOperationsApi.review(detail.task.id, { stage, decision, comment: reviewComment });
setReviewComment("");
await loadProject();
setDetail(await annotationOperationsApi.getTask(detail.task.id));
} catch (saveError) {
setError(getActionErrorMessage(saveError, "提交审核决定失败"));
} finally {
setSaving(false);
}
};
const createGroup = async () => {
if (!groupDraft.name.trim() || !groupDraft.vendorId.trim()) return;
setSaving(true);
try {
await annotationOperationsApi.createWorkgroup(groupDraft);
setGroupDraft({ name: "", vendorId: "", description: "" });
await loadBase();
} catch (saveError) {
setError(getActionErrorMessage(saveError, "创建供应商分组失败"));
} finally {
setSaving(false);
}
};
const createMembership = async () => {
if (!projectId || !membershipDraft.userId || !membershipDraft.workgroupId) return;
setSaving(true);
try {
await annotationOperationsApi.createMembership({ projectId, ...membershipDraft });
await loadProject();
} catch (saveError) {
setError(getActionErrorMessage(saveError, "分配项目角色失败"));
} finally {
setSaving(false);
}
};
const createTemplate = async () => {
if (!projectId || !templateDraft.name.trim()) return;
const labels = templateDraft.labels
.split(",")
.map((name, index) => ({ id: `label-${index + 1}`, name: name.trim() }))
.filter((item) => item.name);
setSaving(true);
try {
await annotationOperationsApi.createTemplate({
projectId,
name: templateDraft.name,
version: Number(templateDraft.version),
labels,
phrases: templateDraft.phrases
.split(",")
.map((item) => item.trim())
.filter(Boolean),
prelabel: { enabled: true, minimumConfidence: 0.7, enableWilor: true, enableHead: false },
});
setTemplateDraft(emptyTemplate);
await loadProject();
} catch (saveError) {
setError(getActionErrorMessage(saveError, "创建语义模板失败"));
} finally {
setSaving(false);
}
};
const createTask = async () => {
if (!projectId || !taskDraft.sourceVideo || !taskDraft.templateId || !taskDraft.workgroupId) return;
setSaving(true);
try {
await annotationOperationsApi.createTask({
projectId,
sourceVideo: taskDraft.sourceVideo,
clipStart: Number(taskDraft.clipStart),
clipEnd: Number(taskDraft.clipEnd),
templateId: taskDraft.templateId,
workgroupId: taskDraft.workgroupId,
assignedUserId: taskDraft.assignedUserId || undefined,
});
setTaskDraft(emptyTask);
await loadProject();
} catch (saveError) {
setError(getActionErrorMessage(saveError, "创建逐段标注任务失败"));
} finally {
setSaving(false);
}
};
return (
<div className="space-y-6">
<div className="flex flex-wrap items-start justify-between gap-4">
<div>
<div className="text-xs font-semibold uppercase tracking-[0.24em] text-blue-400">Annotation Operations</div>
<h1 className="mt-2 text-3xl font-bold text-white">语义标注生产闭环</h1>
<p className="mt-2 max-w-3xl text-sm leading-6 text-gray-400">
项目成员、供应商分组、版本化词表、逐段修订、质检验收和工时事件使用同一条后端访问边界。
</p>
</div>
<div className="flex items-center gap-2">
<select className={inputClass} value={projectId} onChange={(event) => setProjectId(event.target.value)}>
<option value="">选择项目</option>
{projects.map((project) => (
<option key={project.id} value={project.id}>
{project.name}
</option>
))}
</select>
<button
type="button"
onClick={() => void loadProject()}
className="rounded-lg border border-[#2a2b2f] p-2 text-gray-300 hover:border-blue-500/50 hover:text-white"
title="刷新"
>
{loading ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
</button>
</div>
</div>
{error ? (
<div className="rounded-xl border border-red-500/30 bg-red-500/10 px-4 py-3 text-sm text-red-200">{error}</div>
) : null}
<div className="flex flex-wrap gap-2">
{(
[
["queue", "标注队列", Layers3],
["templates", "模板版本", Tags],
["groups", "供应商与角色", UsersRound],
["workload", "工时统计", Clock3],
] as const
).map(([value, label, Icon]) => (
<button
key={value}
type="button"
onClick={() => setTab(value)}
className={`inline-flex items-center gap-2 rounded-xl border px-4 py-2 text-sm ${tab === value ? "border-blue-500 bg-blue-500/10 text-blue-200" : "border-[#2a2b2f] bg-[#151619] text-gray-400"}`}
>
<Icon className="h-4 w-4" />
{label}
</button>
))}
</div>
{tab === "queue" ? (
<div className="grid gap-6 xl:grid-cols-[320px_1fr]">
<div className="space-y-4">
<div className={`${panelClass} p-4`}>
<div className="text-sm font-semibold text-white">逐段任务</div>
<div className="mt-3 space-y-2">
{tasks.map((task) => (
<button
key={task.id}
type="button"
onClick={() => setSelectedTaskId(task.id)}
className={`w-full rounded-xl border p-3 text-left ${selectedTaskId === task.id ? "border-blue-500 bg-blue-500/10" : "border-[#2a2b2f] bg-[#0f1012]"}`}
>
<div className="truncate text-sm font-medium text-white">{task.sourceVideo}</div>
<div className="mt-1 flex items-center justify-between text-xs text-gray-500">
<span>
{task.clipStart.toFixed(1)}–{task.clipEnd.toFixed(1)}s
</span>
<span>{statusLabels[task.status] || task.status}</span>
</div>
</button>
))}
{tasks.length === 0 ? <div className="py-8 text-center text-sm text-gray-500">暂无可见任务</div> : null}
</div>
</div>
{isManager ? (
<div className={`${panelClass} space-y-3 p-4`}>
<div className="text-sm font-semibold text-white">创建标注任务</div>
<input
className={inputClass}
placeholder="视频或 artifact 路径"
value={taskDraft.sourceVideo}
onChange={(event) => setTaskDraft((current) => ({ ...current, sourceVideo: event.target.value }))}
/>
<div className="grid grid-cols-2 gap-2">
<input
className={inputClass}
type="number"
value={taskDraft.clipStart}
onChange={(event) => setTaskDraft((current) => ({ ...current, clipStart: event.target.value }))}
/>
<input
className={inputClass}
type="number"
value={taskDraft.clipEnd}
onChange={(event) => setTaskDraft((current) => ({ ...current, clipEnd: event.target.value }))}
/>
</div>
<select
className={inputClass}
value={taskDraft.templateId}
onChange={(event) => setTaskDraft((current) => ({ ...current, templateId: event.target.value }))}
>
<option value="">已发布模板</option>
{templates
.filter((item) => item.status === "published")
.map((item) => (
<option key={item.id} value={item.id}>
{item.name} v{item.version}
</option>
))}
</select>
<select
className={inputClass}
value={taskDraft.workgroupId}
onChange={(event) => setTaskDraft((current) => ({ ...current, workgroupId: event.target.value }))}
>
<option value="">供应商分组</option>
{workgroups.map((item) => (
<option key={item.id} value={item.id}>
{item.vendorId} / {item.name}
</option>
))}
</select>
<select
className={inputClass}
value={taskDraft.assignedUserId}
onChange={(event) => setTaskDraft((current) => ({ ...current, assignedUserId: event.target.value }))}
>
<option value="">暂不指定标注员</option>
{users.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
<button
type="button"
onClick={() => void createTask()}
disabled={saving}
className="w-full rounded-lg bg-blue-600 px-3 py-2 text-sm font-medium text-white disabled:opacity-50"
>
创建任务
</button>
</div>
) : null}
</div>
<div className={`${panelClass} min-h-[640px] p-5`}>
{detail ? (
<div className="space-y-6">
<div className="flex flex-wrap items-start justify-between gap-3">
<div>
<div className="text-lg font-semibold text-white">{detail.task.sourceVideo}</div>
<div className="mt-1 text-sm text-gray-500">
模板 v{detail.task.templateVersion} · {statusLabels[detail.task.status]}
</div>
</div>
<div className="rounded-full border border-blue-500/30 bg-blue-500/10 px-3 py-1 text-xs text-blue-200">
Revision {detail.task.currentRevision}
</div>
</div>
<div>
<div className="mb-2 flex justify-between text-xs text-gray-500">
<span>{detail.task.clipStart.toFixed(2)}s</span>
<span>{detail.task.clipEnd.toFixed(2)}s</span>
</div>
<div className="relative h-20 overflow-hidden rounded-xl border border-[#2a2b2f] bg-[#090a0c]">
{segments.map((segment) => {
const position = annotationSegmentPosition(segment, detail.task.clipStart, detail.task.clipEnd);
const color =
selectedTemplate?.labels.find((item) => item.id === segment.labelId)?.color || "#2563eb";
return (
<button
key={segment.id}
type="button"
title={`${segment.label} ${segment.start}-${segment.end}s`}
onClick={() => setSegments((current) => current.filter((item) => item.id !== segment.id))}
className="absolute top-3 h-12 overflow-hidden rounded-lg border border-white/10 px-2 text-left text-xs text-white"
style={{ left: `${position.left}%`, width: `${position.width}%`, backgroundColor: color }}
>
<span className="block truncate font-medium">{segment.label}</span>
<span className="text-[10px] opacity-70">
{segment.start.toFixed(1)}–{segment.end.toFixed(1)}
</span>
</button>
);
})}
</div>
</div>
<div className="grid gap-3 rounded-xl border border-[#2a2b2f] bg-[#101114] p-4 md:grid-cols-[1.2fr_1fr_1fr_auto]">
<select
className={inputClass}
value={segmentDraft.labelId}
onChange={(event) => {
const label = selectedTemplate?.labels.find((item) => item.id === event.target.value);
setSegmentDraft((current) => ({
...current,
labelId: event.target.value,
label: label?.name || "",
}));
}}
>
<option value="">选择语义标签</option>
{selectedTemplate?.labels.map((label) => (
<option key={label.id} value={label.id}>
{label.name}
</option>
))}
</select>
<input
className={inputClass}
type="number"
step="0.1"
value={segmentDraft.start}
onChange={(event) => setSegmentDraft((current) => ({ ...current, start: event.target.value }))}
/>
<input
className={inputClass}
type="number"
step="0.1"
value={segmentDraft.end}
onChange={(event) => setSegmentDraft((current) => ({ ...current, end: event.target.value }))}
/>
<button
type="button"
onClick={addSegment}
className="rounded-lg border border-blue-500/40 px-3 text-blue-200"
>
<Plus className="h-4 w-4" />
</button>
</div>
<div className="flex flex-wrap gap-3">
<button
type="button"
onClick={() => void submitRevision()}
disabled={saving || segments.length === 0}
className="inline-flex items-center gap-2 rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white disabled:opacity-50"
>
<Send className="h-4 w-4" />
提交版本
</button>
{detail.task.status === "pending_quality" ? (
<>
<button
type="button"
onClick={() => void review("quality", "approved")}
className="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-4 py-2 text-sm text-white"
>
<CheckCircle2 className="h-4 w-4" />
质检通过
</button>
<button
type="button"
onClick={() => void review("quality", "rejected")}
className="inline-flex items-center gap-2 rounded-lg bg-red-600 px-4 py-2 text-sm text-white"
>
<RotateCcw className="h-4 w-4" />
质检打回
</button>
</>
) : null}
{detail.task.status === "pending_acceptance" ? (
<>
<button
type="button"
onClick={() => void review("acceptance", "approved")}
className="inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-4 py-2 text-sm text-white"
>
<ClipboardCheck className="h-4 w-4" />
验收通过
</button>
<button
type="button"
onClick={() => void review("acceptance", "rejected")}
className="inline-flex items-center gap-2 rounded-lg bg-red-600 px-4 py-2 text-sm text-white"
>
<XCircle className="h-4 w-4" />
验收打回
</button>
</>
) : null}
</div>
<textarea
className={inputClass}
rows={3}
placeholder="质检/验收意见"
value={reviewComment}
onChange={(event) => setReviewComment(event.target.value)}
/>
<div className="grid gap-4 md:grid-cols-2">
<div className="rounded-xl border border-[#2a2b2f] p-4">
<div className="text-sm font-medium text-white">修订历史</div>
{detail.revisions.map((item) => (
<div key={item.id} className="mt-3 text-xs text-gray-400">
v{item.revision} · {item.segments.length} 段 · {item.isRework ? "返工" : "首次产出"} ·{" "}
{formatCompactDateTime(item.createdAt, timeZone)}
</div>
))}
</div>
<div className="rounded-xl border border-[#2a2b2f] p-4">
<div className="text-sm font-medium text-white">审核历史</div>
{detail.reviews.map((item) => (
<div key={item.id} className="mt-3 text-xs text-gray-400">
{item.stage} · {item.decision} · revision {item.revision} ·{" "}
{formatCompactDateTime(item.createdAt, timeZone)}
</div>
))}
</div>
</div>
</div>
) : (
<div className="flex min-h-[560px] items-center justify-center text-sm text-gray-500">
选择一个任务进入时间轴编辑
</div>
)}
</div>
</div>
) : null}
{tab === "templates" ? (
<div className="grid gap-6 lg:grid-cols-[360px_1fr]">
{isManager ? (
<div className={`${panelClass} space-y-3 p-5`}>
<div className="text-base font-semibold text-white">创建模板版本</div>
<input
className={inputClass}
value={templateDraft.name}
onChange={(event) => setTemplateDraft((current) => ({ ...current, name: event.target.value }))}
/>
<input
className={inputClass}
type="number"
value={templateDraft.version}
onChange={(event) =>
setTemplateDraft((current) => ({ ...current, version: Number(event.target.value) }))
}
/>
<textarea
className={inputClass}
value={templateDraft.labels}
onChange={(event) => setTemplateDraft((current) => ({ ...current, labels: event.target.value }))}
/>
<textarea
className={inputClass}
value={templateDraft.phrases}
onChange={(event) => setTemplateDraft((current) => ({ ...current, phrases: event.target.value }))}
/>
<button
type="button"
onClick={() => void createTemplate()}
className="w-full rounded-lg bg-blue-600 px-3 py-2 text-sm text-white"
>
创建草稿
</button>
</div>
) : null}
<div className={`${panelClass} p-5`}>
<div className="grid gap-3 md:grid-cols-2">
{templates.map((item) => (
<div key={item.id} className="rounded-xl border border-[#2a2b2f] bg-[#101114] p-4">
<div className="flex justify-between">
<div className="font-medium text-white">
{item.name} v{item.version}
</div>
<span className="text-xs text-gray-500">{item.status}</span>
</div>
<div className="mt-3 flex flex-wrap gap-2">
{item.labels.map((label) => (
<span key={label.id} className="rounded-full bg-blue-500/10 px-2 py-1 text-xs text-blue-200">
{label.name}
</span>
))}
</div>
{isManager && item.status === "draft" ? (
<button
type="button"
onClick={() => annotationOperationsApi.publishTemplate(item.id).then(loadProject)}
className="mt-4 rounded-lg border border-emerald-500/30 px-3 py-1.5 text-xs text-emerald-200"
>
发布版本
</button>
) : null}
</div>
))}
</div>
</div>
</div>
) : null}
{tab === "groups" ? (
<div className="grid gap-6 lg:grid-cols-2">
<div className={`${panelClass} p-5`}>
<div className="text-base font-semibold text-white">供应商分组</div>
{isManager ? (
<div className="mt-4 grid gap-2 md:grid-cols-2">
<input
className={inputClass}
placeholder="分组名称"
value={groupDraft.name}
onChange={(event) => setGroupDraft((current) => ({ ...current, name: event.target.value }))}
/>
<input
className={inputClass}
placeholder="供应商 ID"
value={groupDraft.vendorId}
onChange={(event) => setGroupDraft((current) => ({ ...current, vendorId: event.target.value }))}
/>
<input
className={`${inputClass} md:col-span-2`}
placeholder="说明"
value={groupDraft.description}
onChange={(event) => setGroupDraft((current) => ({ ...current, description: event.target.value }))}
/>
<button
type="button"
onClick={() => void createGroup()}
className="rounded-lg bg-blue-600 px-3 py-2 text-sm text-white"
>
创建分组
</button>
</div>
) : null}
<div className="mt-5 space-y-2">
{workgroups.map((item) => (
<div key={item.id} className="rounded-xl border border-[#2a2b2f] p-3">
<div className="text-sm text-white">{item.name}</div>
<div className="text-xs text-gray-500">{item.vendorId}</div>
</div>
))}
</div>
</div>
<div className={`${panelClass} p-5`}>
<div className="text-base font-semibold text-white">项目角色</div>
{isManager ? (
<div className="mt-4 grid gap-2">
<select
className={inputClass}
value={membershipDraft.userId}
onChange={(event) => setMembershipDraft((current) => ({ ...current, userId: event.target.value }))}
>
<option value="">选择用户</option>
{users.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
<select
className={inputClass}
value={membershipDraft.workgroupId}
onChange={(event) =>
setMembershipDraft((current) => ({ ...current, workgroupId: event.target.value }))
}
>
<option value="">选择分组</option>
{workgroups.map((item) => (
<option key={item.id} value={item.id}>
{item.name}
</option>
))}
</select>
<select
className={inputClass}
value={membershipDraft.assignmentRole}
onChange={(event) =>
setMembershipDraft((current) => ({
...current,
assignmentRole: event.target.value as ProjectAnnotationMembership["assignmentRole"],
}))
}
>
<option value="annotator">标注员</option>
<option value="quality_reviewer">质检员</option>
<option value="acceptance_reviewer">验收员</option>
</select>
<button
type="button"
onClick={() => void createMembership()}
className="rounded-lg bg-blue-600 px-3 py-2 text-sm text-white"
>
分配角色
</button>
</div>
) : null}
<div className="mt-5 space-y-2">
{memberships.map((item) => (
<div key={item.id} className="rounded-xl border border-[#2a2b2f] p-3 text-xs text-gray-400">
<div className="text-sm text-white">
{users.find((user) => user.id === item.userId)?.name || item.userId}
</div>
<div>
{item.vendorId} · {item.assignmentRole}
</div>
</div>
))}
</div>
</div>
</div>
) : null}
{tab === "workload" ? (
<div className="space-y-5">
<div className="grid gap-3 md:grid-cols-5">
{[
["正式产出", workloadTotals.outputCount],
["返工", workloadTotals.reworkCount],
["通过", workloadTotals.approvedCount],
["拒绝", workloadTotals.rejectedCount],
["总工时", `${Math.round(workloadTotals.durationSeconds / 60)} min`],
].map(([label, value]) => (
<div key={String(label)} className={`${panelClass} p-4`}>
<div className="text-xs text-gray-500">{label}</div>
<div className="mt-2 text-2xl font-semibold text-white">{value}</div>
</div>
))}
</div>
<div className={`${panelClass} overflow-hidden`}>
<div className="grid grid-cols-[1.2fr_1fr_1fr_repeat(4,0.7fr)] gap-3 border-b border-[#2a2b2f] px-4 py-3 text-xs text-gray-500">
<span>用户</span>
<span>供应商</span>
<span>分组</span>
<span>模板</span>
<span>产出</span>
<span>返工</span>
<span>工时</span>
</div>
{workload.items.map((item, index) => (
<div
key={`${item.userId}-${item.workgroupId}-${item.templateVersion}-${index}`}
className="grid grid-cols-[1.2fr_1fr_1fr_repeat(4,0.7fr)] gap-3 border-b border-[#202227] px-4 py-3 text-sm text-gray-300"
>
<span>{users.find((user) => user.id === item.userId)?.name || item.userId || "系统"}</span>
<span>{item.vendorId}</span>
<span>{workgroups.find((group) => group.id === item.workgroupId)?.name || item.workgroupId}</span>
<span>v{item.templateVersion}</span>
<span>{item.outputCount}</span>
<span>{item.reworkCount}</span>
<span>{Math.round(item.durationSeconds / 60)}m</span>
</div>
))}
</div>
</div>
) : null}
</div>
);
}

View File

@ -0,0 +1,89 @@
import type {
AnnotationRevision,
AnnotationReview,
AnnotationSegment,
AnnotationTaskDetail,
AnnotationTaskRecord,
AnnotationTemplate,
AnnotationWorkgroup,
AnnotationWorkloadSummary,
ProjectAnnotationMembership,
SemanticLabelDefinition,
} from "../../types";
import { apiRequest } from "./client";
const query = (values: Record<string, string | undefined>) => {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(values)) {
if (value) params.set(key, value);
}
const encoded = params.toString();
return encoded ? `?${encoded}` : "";
};
export const annotationOperationsApi = {
listWorkgroups: async () => (await apiRequest<{ items: AnnotationWorkgroup[] }>("/api/annotation-workgroups")).items,
createWorkgroup: (payload: { name: string; vendorId: string; description?: string }) =>
apiRequest<AnnotationWorkgroup>("/api/annotation-workgroups", { method: "POST" }, payload),
listMemberships: async (projectId?: string) =>
(await apiRequest<{ items: ProjectAnnotationMembership[] }>(`/api/annotation-memberships${query({ projectId })}`))
.items,
createMembership: (payload: {
projectId: string;
userId: string;
workgroupId: string;
assignmentRole: ProjectAnnotationMembership["assignmentRole"];
}) => apiRequest<ProjectAnnotationMembership>("/api/annotation-memberships", { method: "POST" }, payload),
listTemplates: async (projectId?: string) =>
(await apiRequest<{ items: AnnotationTemplate[] }>(`/api/annotation-templates${query({ projectId })}`)).items,
createTemplate: (payload: {
projectId: string;
name: string;
version: number;
labels: SemanticLabelDefinition[];
phrases?: string[];
prelabel: AnnotationTemplate["prelabel"];
}) => apiRequest<AnnotationTemplate>("/api/annotation-templates", { method: "POST" }, payload),
publishTemplate: (id: string) =>
apiRequest<AnnotationTemplate>(`/api/annotation-templates/${id}/publish`, { method: "POST" }),
listTasks: async (filters: { projectId?: string; status?: string; workgroupId?: string; vendorId?: string } = {}) =>
(await apiRequest<{ items: AnnotationTaskRecord[] }>(`/api/annotation-tasks${query(filters)}`)).items,
getTask: (id: string) => apiRequest<AnnotationTaskDetail>(`/api/annotation-tasks/${id}`),
createTask: (payload: {
projectId: string;
workflowId?: string;
runId?: string;
artifactId?: string;
sourceVideo: string;
clipStart: number;
clipEnd: number;
templateId: string;
workgroupId: string;
assignedUserId?: string;
}) => apiRequest<AnnotationTaskRecord>("/api/annotation-tasks", { method: "POST" }, payload),
submitRevision: (
taskId: string,
payload: { segments: AnnotationSegment[]; changeReason?: string; durationSeconds?: number },
) =>
apiRequest<{ task: AnnotationTaskRecord; revision: AnnotationRevision }>(
`/api/annotation-tasks/${taskId}/revisions`,
{ method: "POST" },
payload,
),
review: (
taskId: string,
payload: {
stage: AnnotationReview["stage"];
decision: AnnotationReview["decision"];
comment?: string;
durationSeconds?: number;
},
) =>
apiRequest<{ task: AnnotationTaskRecord; review: AnnotationReview }>(
`/api/annotation-tasks/${taskId}/reviews`,
{ method: "POST" },
payload,
),
workloadSummary: (projectId?: string) =>
apiRequest<AnnotationWorkloadSummary>(`/api/annotation-workload-summary${query({ projectId })}`),
};

View File

@ -1235,3 +1235,151 @@ export interface AuditTask {
reason: string;
};
}
export type AnnotationAssignmentRole = "annotator" | "quality_reviewer" | "acceptance_reviewer";
export type AnnotationTaskStatus =
| "pending_annotation"
| "pending_quality"
| "pending_acceptance"
| "rework"
| "accepted"
| "rejected";
export interface AnnotationWorkgroup {
id: string;
organizationId?: string;
name: string;
vendorId: string;
description?: string;
status: string;
createdAt: string;
updatedAt: string;
}
export interface ProjectAnnotationMembership {
id: string;
organizationId?: string;
projectId: string;
userId: string;
workgroupId: string;
vendorId: string;
assignmentRole: AnnotationAssignmentRole;
createdAt: string;
updatedAt: string;
}
export interface SemanticLabelDefinition {
id: string;
name: string;
description?: string;
aliases?: string[];
color?: string;
}
export interface AnnotationTemplate {
id: string;
organizationId?: string;
projectId: string;
name: string;
version: number;
status: "draft" | "published" | "retired";
labels: SemanticLabelDefinition[];
phrases?: string[];
prelabel: {
enabled: boolean;
minimumConfidence?: number;
enableWilor?: boolean;
enableHead?: boolean;
};
createdByUserId: string;
publishedByUserId?: string;
publishedAt?: string;
createdAt: string;
updatedAt: string;
}
export interface AnnotationSegment {
id: string;
labelId: string;
label: string;
start: number;
end: number;
confidence?: number;
phrases?: string[];
source?: string;
}
export interface AnnotationTaskRecord {
id: string;
organizationId?: string;
projectId: string;
workflowId?: string;
runId?: string;
artifactId?: string;
sourceVideo: string;
clipStart: number;
clipEnd: number;
templateId: string;
templateVersion: number;
workgroupId: string;
vendorId: string;
assignedUserId?: string;
status: AnnotationTaskStatus;
currentRevision: number;
createdAt: string;
updatedAt: string;
}
export interface AnnotationRevision {
id: string;
organizationId?: string;
taskId: string;
revision: number;
templateVersion: number;
segments: AnnotationSegment[];
authorUserId: string;
changeReason?: string;
isRework: boolean;
countsAsOutput: boolean;
durationSeconds?: number;
createdAt: string;
}
export interface AnnotationReview {
id: string;
taskId: string;
revision: number;
stage: "quality" | "acceptance";
decision: "approved" | "rejected";
reviewerUserId: string;
comment?: string;
nextRevision?: number;
createdAt: string;
}
export interface AnnotationTaskDetail {
task: AnnotationTaskRecord;
revisions: AnnotationRevision[];
reviews: AnnotationReview[];
}
export interface AnnotationWorkloadAggregate {
userId: string;
workgroupId: string;
vendorId: string;
templateVersion: number;
assignedCount: number;
submittedCount: number;
qualityApproved: number;
qualityRejected: number;
acceptanceApproved: number;
acceptanceRejected: number;
reworkCount: number;
outputCount: number;
durationSeconds: number;
}
export interface AnnotationWorkloadSummary {
items: AnnotationWorkloadAggregate[];
eventCount: number;
}

View File

@ -0,0 +1,112 @@
import json
from pathlib import Path
from backend.operators.builtin.orbbec_data_normalize import operator as orbbec_operator
from backend.operators.builtin.semantic_prelabel import operator as semantic_operator
def helpers(tmp_path: Path):
def hyperparams(node_data):
return {item["key"]: str(item.get("value", "")) for item in node_data.get("hyperparams", [])}
def first_payload_with_keys(inputs, *keys):
for artifact in inputs:
data = artifact.get("data")
if isinstance(data, dict) and all(key in data for key in keys):
return data
return None
def write_json_artifact(run_id, node_id, output_dir, name, payload, output=None):
path = Path(output_dir) / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
return {"runId": run_id, "nodeId": node_id, "name": name, "path": str(path), "type": "json", "data": payload}
def parse_json_param(params, key, default, expected_type=None):
value = params.get(key, "")
parsed = json.loads(value) if value else default
assert expected_type is None or isinstance(parsed, expected_type)
return parsed
def parse_unit_interval_param(params, key, default):
value = float(params.get(key, default))
assert 0 <= value <= 1
return value
return {
"hyperparams": hyperparams,
"first_payload_with_keys": first_payload_with_keys,
"write_json_artifact": write_json_artifact,
"output_binding": lambda _node_data: {"id": "out-1", "name": "output", "type": "json"},
"parse_json_param": parse_json_param,
"parse_unit_interval_param": parse_unit_interval_param,
}
def test_orbbec_normalizer_pairs_real_capture_files(tmp_path):
capture = "Orbbec_Ego_AZER76400GG_19700102_061040"
entries = []
for suffix, mime in [
("camera_left_part0001.mp4", "video/mp4"),
("camera_left_part0001_pts.csv", "text/csv"),
("camera_right_part0001.mp4", "video/mp4"),
("camera_right_part0001_pts.csv", "text/csv"),
("audio.wav", "audio/x-wav"),
("audio_pts.csv", "text/csv"),
("imu.csv", "text/csv"),
("calibration_camera.yaml", "application/x-yaml"),
("calibration_imu.yaml", "application/x-yaml"),
]:
path = f"{capture}/{capture}_{suffix}"
entries.append({"path": path, "sizeBytes": 128, "mimeType": mime, "source": {"path": f"bos://testxyz/{path}"}})
artifacts = orbbec_operator.run(
"run-1",
"normalize",
tmp_path,
{"hyperparams": [{"key": "CAMERA_SIDE", "value": "both"}, {"key": "REQUIRE_STEREO", "value": "true"}]},
[{"data": {"sourcePath": "bos://testxyz/fanyu_test", "entries": entries}}],
helpers(tmp_path),
)
payload = artifacts[0]["data"]
assert payload["schemaVersion"] == "droboticflow.orbbec.normalized/v1"
assert payload["collectionCount"] == 1
assert payload["videoCount"] == 2
assert payload["collections"][0]["valid"] is True
assert {item["cameraSide"] for item in payload["items"]} == {"left", "right"}
assert all(item["pairedFiles"]["imu"].endswith("_imu.csv") for item in payload["items"])
def test_semantic_prelabel_maps_actions_to_template_version(tmp_path):
annotations = {
"annotationCount": 1,
"items": [
{
"clipId": "clip-1",
"annotation": {
"actions": [
{"label": "静止观察", "start": 0, "end": 2, "confidence": 0.9},
{"label": "快速移动", "start": 2, "end": 4, "confidence": 0.8},
]
},
}
],
}
artifacts = semantic_operator.run(
"run-2",
"semantic",
tmp_path,
{
"hyperparams": [
{"key": "TEMPLATE_VERSION", "value": "v3"},
{"key": "LABEL_SCHEMA_JSON", "value": json.dumps([{"id": "static", "name": "静止观察"}, {"id": "fast", "name": "快速移动"}], ensure_ascii=False)},
{"key": "PHRASES_JSON", "value": json.dumps(["手部"], ensure_ascii=False)},
{"key": "MINIMUM_CONFIDENCE", "value": "0.70"},
]
},
[{"data": annotations}],
helpers(tmp_path),
)
payload = artifacts[0]["data"]
assert payload["templateVersion"] == "v3"
assert payload["segmentCount"] == 2
assert [item["labelId"] for item in payload["items"][0]["semanticAnnotation"]["segments"]] == ["static", "fast"]

View File

@ -0,0 +1,35 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
annotationSegmentPosition,
normalizeAnnotationSegments,
summarizeAnnotationWorkload,
} from "../../src/lib/annotation-operations.js";
test("annotation timeline normalization rejects invalid ranges and sorts segments", () => {
const segments = normalizeAnnotationSegments(
[
{ id: "late", labelId: "steady", label: "平稳活动", start: 10, end: 20 },
{ id: "invalid", labelId: "bad", label: "bad", start: -1, end: 2 },
{ id: "early", labelId: "static", label: "静止观察", start: 0, end: 10 },
],
0,
20,
);
assert.deepEqual(
segments.map((item) => item.id),
["early", "late"],
);
assert.deepEqual(annotationSegmentPosition(segments[1], 0, 20), { left: 50, width: 50 });
});
test("rework does not increase formal output count", () => {
const summary = summarizeAnnotationWorkload([
{ outputCount: 1, reworkCount: 0, qualityApproved: 1, acceptanceApproved: 0, durationSeconds: 60 },
{ outputCount: 0, reworkCount: 1, qualityRejected: 1, acceptanceRejected: 0, durationSeconds: 30 },
]);
assert.equal(summary.outputCount, 1);
assert.equal(summary.reworkCount, 1);
assert.equal(summary.durationSeconds, 90);
});