diff --git a/backend/operator-runner/main.py b/backend/operator-runner/main.py index 50db25d1..209d8106 100644 --- a/backend/operator-runner/main.py +++ b/backend/operator-runner/main.py @@ -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'), diff --git a/backend/operators/builtin/orbbec_data_normalize/operator.json b/backend/operators/builtin/orbbec_data_normalize/operator.json new file mode 100644 index 00000000..644cbc03 --- /dev/null +++ b/backend/operators/builtin/orbbec_data_normalize/operator.json @@ -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 + } +} diff --git a/backend/operators/builtin/orbbec_data_normalize/operator.py b/backend/operators/builtin/orbbec_data_normalize/operator.py new file mode 100644 index 00000000..d2bb6dff --- /dev/null +++ b/backend/operators/builtin/orbbec_data_normalize/operator.py @@ -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"^(?POrbbec_Ego_[^/]+?_(?:19|20)\d{6}_\d{6})(?:/|_)") +CAMERA_PATTERN = re.compile(r"_camera_(?Pleft|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), + ) + ] diff --git a/backend/operators/builtin/semantic_prelabel/operator.json b/backend/operators/builtin/semantic_prelabel/operator.json new file mode 100644 index 00000000..4e76e6ec --- /dev/null +++ b/backend/operators/builtin/semantic_prelabel/operator.json @@ -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 + } +} diff --git a/backend/operators/builtin/semantic_prelabel/operator.py b/backend/operators/builtin/semantic_prelabel/operator.py new file mode 100644 index 00000000..0f767442 --- /dev/null +++ b/backend/operators/builtin/semantic_prelabel/operator.py @@ -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), + ) + ] diff --git a/custom-operators/fanyu-annotation/README.md b/custom-operators/fanyu-annotation/README.md new file mode 100644 index 00000000..d5ee99de --- /dev/null +++ b/custom-operators/fanyu-annotation/README.md @@ -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 冒充已发布镜像。 diff --git a/custom-operators/fanyu-annotation/operator-definitions.json b/custom-operators/fanyu-annotation/operator-definitions.json new file mode 100644 index 00000000..b0a151b3 --- /dev/null +++ b/custom-operators/fanyu-annotation/operator-definitions.json @@ -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" + } +] diff --git a/docs/annotation-operations.md b/docs/annotation-operations.md new file mode 100644 index 00000000..6092792a --- /dev/null +++ b/docs/annotation-operations.md @@ -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 标记为已发布。 diff --git a/internal/api/annotation_operations.go b/internal/api/annotation_operations.go new file mode 100644 index 00000000..f9e26bc5 --- /dev/null +++ b/internal/api/annotation_operations.go @@ -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)}) +} diff --git a/internal/api/annotation_operations_test.go b/internal/api/annotation_operations_test.go new file mode 100644 index 00000000..26a188d8 --- /dev/null +++ b/internal/api/annotation_operations_test.go @@ -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") + } +} diff --git a/internal/api/builtin_operators.go b/internal/api/builtin_operators.go index 04c6f46e..523db69d 100644 --- a/internal/api/builtin_operators.go +++ b/internal/api/builtin_operators.go @@ -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 diff --git a/internal/api/server.go b/internal/api/server.go index d66825da..20dd3e1e 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -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)) diff --git a/internal/domain/annotation_operations.go b/internal/domain/annotation_operations.go new file mode 100644 index 00000000..f65e1fea --- /dev/null +++ b/internal/domain/annotation_operations.go @@ -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"` +} diff --git a/internal/store/annotation_operations.go b/internal/store/annotation_operations.go new file mode 100644 index 00000000..2fedfaa5 --- /dev/null +++ b/internal/store/annotation_operations.go @@ -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 ©, 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 ©, 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 ©, 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) +} diff --git a/internal/store/annotation_operations_test.go b/internal/store/annotation_operations_test.go new file mode 100644 index 00000000..5023a22f --- /dev/null +++ b/internal/store/annotation_operations_test.go @@ -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) + } +} diff --git a/internal/store/memory.go b/internal/store/memory.go index 3c499283..acaef79f 100644 --- a/internal/store/memory.go +++ b/internal/store/memory.go @@ -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) diff --git a/internal/store/mongo.go b/internal/store/mongo.go index d64f9cc4..494f8154 100644 --- a/internal/store/mongo.go +++ b/internal/store/mongo.go @@ -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, diff --git a/internal/store/mongo_indexes.go b/internal/store/mongo_indexes.go index b55143a4..fe3ac85e 100644 --- a/internal/store/mongo_indexes.go +++ b/internal/store/mongo_indexes.go @@ -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}}), diff --git a/internal/store/store.go b/internal/store/store.go index cfc0e08d..7baa6e87 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -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) diff --git a/src/App.tsx b/src/App.tsx index b5fb8586..4762397d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -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
正在加载页面...
; @@ -119,6 +120,7 @@ export default function App() { } /> } /> } /> + } /> } /> } /> } /> diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index ba242285..ce8c232a 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -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; diff --git a/src/lib/annotation-operations.js b/src/lib/annotation-operations.js new file mode 100644 index 00000000..1fa777c4 --- /dev/null +++ b/src/lib/annotation-operations.js @@ -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 }, + ); +} diff --git a/src/pages/AnnotationOperations.tsx b/src/pages/AnnotationOperations.tsx new file mode 100644 index 00000000..f35e1bb6 --- /dev/null +++ b/src/pages/AnnotationOperations.tsx @@ -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 = { + 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("queue"); + const [projects, setProjects] = useState([]); + const [projectId, setProjectId] = useState(""); + const [workgroups, setWorkgroups] = useState([]); + const [memberships, setMemberships] = useState([]); + const [templates, setTemplates] = useState([]); + const [tasks, setTasks] = useState([]); + const [workload, setWorkload] = useState({ items: [], eventCount: 0 }); + const [selectedTaskId, setSelectedTaskId] = useState(""); + const [detail, setDetail] = useState(null); + const [segments, setSegments] = useState([]); + 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 ( +
+
+
+
Annotation Operations
+

语义标注生产闭环

+

+ 项目成员、供应商分组、版本化词表、逐段修订、质检验收和工时事件使用同一条后端访问边界。 +

+
+
+ + +
+
+ + {error ? ( +
{error}
+ ) : null} + +
+ {( + [ + ["queue", "标注队列", Layers3], + ["templates", "模板版本", Tags], + ["groups", "供应商与角色", UsersRound], + ["workload", "工时统计", Clock3], + ] as const + ).map(([value, label, Icon]) => ( + + ))} +
+ + {tab === "queue" ? ( +
+
+
+
逐段任务
+
+ {tasks.map((task) => ( + + ))} + {tasks.length === 0 ?
暂无可见任务
: null} +
+
+ + {isManager ? ( +
+
创建标注任务
+ setTaskDraft((current) => ({ ...current, sourceVideo: event.target.value }))} + /> +
+ setTaskDraft((current) => ({ ...current, clipStart: event.target.value }))} + /> + setTaskDraft((current) => ({ ...current, clipEnd: event.target.value }))} + /> +
+ + + + +
+ ) : null} +
+ +
+ {detail ? ( +
+
+
+
{detail.task.sourceVideo}
+
+ 模板 v{detail.task.templateVersion} · {statusLabels[detail.task.status]} +
+
+
+ Revision {detail.task.currentRevision} +
+
+ +
+
+ {detail.task.clipStart.toFixed(2)}s + {detail.task.clipEnd.toFixed(2)}s +
+
+ {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 ( + + ); + })} +
+
+ +
+ + setSegmentDraft((current) => ({ ...current, start: event.target.value }))} + /> + setSegmentDraft((current) => ({ ...current, end: event.target.value }))} + /> + +
+ +
+ + {detail.task.status === "pending_quality" ? ( + <> + + + + ) : null} + {detail.task.status === "pending_acceptance" ? ( + <> + + + + ) : null} +
+