Merge pull request 'feat: 管理员字典批量运维 — 导出 CSV / 批量导入 / 勾选批量删除' (#47) from codex/dict-batch-ops into codex/inplace-slice-editing
Some checks failed
web-ci / checks (push) Waiting to run
web-ci / checks (pull_request) Has been cancelled

This commit is contained in:
zhanghongming 2026-10-14 13:53:46 +08:00
commit 7b299abe2f
7 changed files with 875 additions and 3 deletions

View File

@ -0,0 +1,221 @@
package api
import (
"errors"
"fmt"
"net/http"
"sort"
"strings"
"time"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"github.com/go-chi/chi/v5"
)
// dictionaryImportMaxNames bounds one bulk import request; dictionaries are
// maintained in review-sized batches, not migrated wholesale.
const dictionaryImportMaxNames = 1000
type dictionaryImportPayload struct {
Kind string `json:"kind"`
Names []string `json:"names"`
}
type dictionaryImportSkip struct {
Name string `json:"name"`
Reason string `json:"reason"`
}
// handleProjectDictionaryLabelsImport accepts one name per entry, reuses the
// organization's active term when the name already exists (shared terms are
// never renamed or revived from inactive), creates the missing ones, and links
// everything onto this project's selection. Existing selection entries are
// preserved; removals stay on the per-project PUT.
func (server *server) handleProjectDictionaryLabelsImport(writer http.ResponseWriter, request *http.Request) {
principal, _ := currentPrincipal(request)
if !annotationManager(principal) {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization administrator role is required"})
return
}
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
return
}
if project.DeletionTaskID != "" {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "项目正在删除,不能修改字典", "code": "project_deleting"})
return
}
var payload dictionaryImportPayload
if !decodeJSON(writer, request, &payload) {
return
}
kind := dictionaryKind(payload.Kind)
if kind != "action" && kind != "slice" && kind != "discard_reason" {
renderValidationError(writer, validationIssue{Field: "kind", Message: "字典用途必须是 action、slice 或 discard_reason"})
return
}
names := make([]string, 0, len(payload.Names))
seenInput := map[string]bool{}
for _, raw := range payload.Names {
name := strings.Join(strings.Fields(raw), " ")
if name == "" {
continue
}
key := domain.DictionaryNameKey(kind, name)
if seenInput[key] {
continue
}
seenInput[key] = true
names = append(names, name)
}
if len(names) == 0 {
renderValidationError(writer, validationIssue{Field: "names", Message: "导入列表为空,请每行填写一个名称"})
return
}
if len(names) > dictionaryImportMaxNames {
renderValidationError(writer, validationIssue{Field: "names", Message: fmt.Sprintf("单次最多导入 %d 个名称", dictionaryImportMaxNames)})
return
}
ctx := request.Context()
available, err := server.store.ListAnnotationLabels(ctx, project.OrganizationID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
activeByKey := map[string]domain.AnnotationLabelRecord{}
codeTaken := map[string]bool{}
byID := map[string]domain.AnnotationLabelRecord{}
for _, item := range available {
byID[item.ID] = item
codeTaken[strings.ToLower(item.Code)] = true
if dictionaryKind(item.Kind) == kind && item.Status == domain.AnnotationLabelActive {
activeByKey[domain.DictionaryNameKey(item.Kind, item.Name)] = item
}
}
selection, err := server.store.GetProjectAnnotationLabels(ctx, project.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if selection == nil {
selection = &domain.ProjectAnnotationLabelsRecord{ID: "project-annotation-labels-" + project.ID, OrganizationID: project.OrganizationID, ProjectID: project.ID, LabelIDs: []string{}}
}
base := selection.LabelIDs
if kind == "slice" {
base = selection.SliceLabelIDs
} else if kind == "discard_reason" {
base = selection.DiscardReasonLabelIDs
}
selectedKeys := map[string]bool{}
nextIDs := make([]string, 0, len(base)+len(names))
for _, id := range base {
if id == "" {
continue
}
nextIDs = append(nextIDs, id)
if item, ok := byID[id]; ok && dictionaryKind(item.Kind) == kind && item.Status == domain.AnnotationLabelActive {
selectedKeys[domain.DictionaryNameKey(item.Kind, item.Name)] = true
}
}
actorID := principal.User.ID
stamp := time.Now().UnixNano()
created, reused := 0, 0
skipped := []dictionaryImportSkip{}
for index, name := range names {
key := domain.DictionaryNameKey(kind, name)
if selectedKeys[key] {
skipped = append(skipped, dictionaryImportSkip{Name: name, Reason: "已在本项目字典中"})
continue
}
if existing, ok := activeByKey[key]; ok {
nextIDs = append(nextIDs, existing.ID)
selectedKeys[key] = true
activeByKey[key] = existing
reused++
continue
}
code := fmt.Sprintf("imp_%s_%d_%03d", kind, stamp, index)
for suffix := 0; codeTaken[code]; suffix++ {
code = fmt.Sprintf("imp_%s_%d_%03d_%d", kind, stamp, index, suffix)
}
input := normalizeAnnotationLabelInput(annotationLabelInput{Kind: kind, Code: code, Name: name, Status: domain.AnnotationLabelActive})
now := nowISO()
item := domain.AnnotationLabelRecord{
Kind: input.Kind, RequiresNote: input.RequiresNote != nil && *input.RequiresNote,
ID: domain.NewID("annotation-label"), OrganizationID: project.OrganizationID,
Code: input.Code, Name: input.Name, Description: input.Description, Status: input.Status, Version: 1,
CreatedByUserID: actorID, UpdatedByUserID: actorID, CreatedAt: now, UpdatedAt: now,
}
item.History = []domain.AnnotationLabelRevision{annotationLabelRevision(item)}
if err := server.store.UpsertAnnotationLabel(ctx, item); err != nil {
if errors.Is(err, store.ErrAnnotationLabelNameConflict) {
// A concurrent import may have created the term; reuse the winner
// instead of failing the whole batch.
latest, listErr := server.store.ListAnnotationLabels(ctx, project.OrganizationID)
if listErr == nil {
for _, candidate := range latest {
if dictionaryKind(candidate.Kind) == kind && candidate.Status == domain.AnnotationLabelActive && domain.DictionaryNameKey(candidate.Kind, candidate.Name) == key {
activeByKey[key] = candidate
break
}
}
}
if existing, ok := activeByKey[key]; ok && existing.ID != item.ID {
nextIDs = append(nextIDs, existing.ID)
selectedKeys[key] = true
reused++
continue
}
}
renderError(writer, http.StatusInternalServerError, err)
return
}
codeTaken[strings.ToLower(item.Code)] = true
activeByKey[key] = item
nextIDs = append(nextIDs, item.ID)
selectedKeys[key] = true
created++
}
sort.Strings(nextIDs)
deduped := nextIDs[:0]
seenID := map[string]bool{}
for _, id := range nextIDs {
if seenID[id] {
continue
}
seenID[id] = true
deduped = append(deduped, id)
}
saved := domain.ProjectAnnotationLabelsRecord{ID: selection.ID, OrganizationID: project.OrganizationID, ProjectID: project.ID, LabelIDs: deduped, UpdatedByUserID: actorID, UpdatedAt: nowISO()}
var saveErr error
if kind == "discard_reason" {
saved.DiscardReasonLabelIDs = deduped
saveErr = server.store.UpsertProjectDiscardReasonLabels(ctx, saved)
} else if kind == "slice" {
saved.SliceLabelIDs = deduped
saveErr = server.store.UpsertProjectSliceLabels(ctx, saved)
} else {
saveErr = server.store.UpsertProjectAnnotationLabels(ctx, saved)
}
if saveErr != nil {
renderError(writer, http.StatusInternalServerError, saveErr)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"projectId": project.ID, "kind": kind, "labelIds": deduped,
"created": created, "reused": reused, "skipped": skipped,
})
}

View File

@ -0,0 +1,206 @@
package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
type dictionaryImportFixture struct {
t *testing.T
handler http.Handler
dataStore store.Store
adminCookie *http.Cookie
org domain.OrganizationRecord
viewerUser domain.UserRecord
project *domain.ProjectRecord
}
func newDictionaryImportFixture(t *testing.T) *dictionaryImportFixture {
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
dataStore := store.NewMemoryStore()
handler := NewHandler(dataStore, nil)
rootCookie := bootstrapSuperAdminSession(t, handler)
organization := adminCreateOrganization(t, handler, rootCookie, "Dict Import", "dict-import")
admin := adminCreateUser(t, handler, rootCookie, organization.ID, "字典管理员", "dict-import-admin@example.com", domain.UserRoleAdmin, "dict-password")
adminCookie := loginSession(t, handler, admin.Phone, "dict-password")
viewer := adminCreateUser(t, handler, rootCookie, organization.ID, "普通成员", "dict-import-viewer@example.com", domain.UserRoleAnnotator, "viewer-password")
project, err := dataStore.CreateProject(t.Context(), domain.CreateProjectInput{OrganizationID: organization.ID, Name: "字典导入项目"})
if err != nil {
t.Fatal(err)
}
return &dictionaryImportFixture{t: t, handler: handler, dataStore: dataStore, adminCookie: adminCookie, org: organization, viewerUser: viewer, project: project}
}
func (f *dictionaryImportFixture) importLabels(cookie *http.Cookie, path string, body string) (*httptest.ResponseRecorder, map[string]any) {
f.t.Helper()
recorder := serveJSONWithCookie(f.handler, http.MethodPost, path, []byte(body), cookie)
decoded := map[string]any{}
_ = json.Unmarshal(recorder.Body.Bytes(), &decoded)
return recorder, decoded
}
func (f *dictionaryImportFixture) projectSelection(kind string) []string {
f.t.Helper()
path := "/api/projects/" + f.project.ID + "/slice-labels"
if kind == "discard_reason" {
path = "/api/projects/" + f.project.ID + "/discard-reason-labels"
} else if kind == "action" {
path = "/api/projects/" + f.project.ID + "/annotation-labels"
}
recorder := serveJSONWithCookie(f.handler, http.MethodGet, path, nil, f.adminCookie)
if recorder.Code != http.StatusOK {
f.t.Fatalf("get selection kind=%s code=%d body=%s", kind, recorder.Code, recorder.Body.String())
}
decoded := map[string]any{}
if err := json.Unmarshal(recorder.Body.Bytes(), &decoded); err != nil {
f.t.Fatal(err)
}
raw, _ := decoded["labelIds"].([]any)
ids := make([]string, 0, len(raw))
for _, id := range raw {
ids = append(ids, id.(string))
}
return ids
}
func TestProjectDictionaryLabelsImportCreatesReusesAndSkips(t *testing.T) {
f := newDictionaryImportFixture(t)
// Pre-existing active org term: import must reuse it, never duplicate.
existing := serveJSONWithCookie(f.handler, http.MethodPost, "/api/annotation-labels", []byte(`{"kind":"slice","code":"pre","name":"拿取前","status":"active"}`), f.adminCookie)
if existing.Code != http.StatusCreated {
t.Fatalf("seed label code=%d body=%s", existing.Code, existing.Body.String())
}
recorder, decoded := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import",
`{"kind":"slice","names":["拿取前"," 商品 检查 ","商品检查","","重复A","重复A","重复A"]}`)
if recorder.Code != http.StatusOK {
t.Fatalf("import code=%d body=%s", recorder.Code, recorder.Body.String())
}
// 拿取前 → reused; " 商品 检查 " collapses to "商品 检查" (inner space kept,
// matching the frontend name key); 商品检查 and the deduped 重复A are created.
if int(decoded["created"].(float64)) != 3 || int(decoded["reused"].(float64)) != 1 {
t.Fatalf("import counts created=%v reused=%v skipped=%v", decoded["created"], decoded["reused"], decoded["skipped"])
}
skipped, _ := decoded["skipped"].([]any)
if len(skipped) != 0 {
t.Fatalf("fresh import should skip nothing, got %v", skipped)
}
ids := f.projectSelection("slice")
if len(ids) != 4 {
t.Fatalf("selection should hold four slice names, got %v", ids)
}
// Re-importing the same names: everything already selected, nothing created.
recorder, decoded = f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import",
`{"kind":"slice","names":["拿取前","商品检查"]}`)
if recorder.Code != http.StatusOK {
t.Fatalf("re-import code=%d body=%s", recorder.Code, recorder.Body.String())
}
if int(decoded["created"].(float64)) != 0 || int(decoded["reused"].(float64)) != 0 {
t.Fatalf("re-import counts created=%v reused=%v", decoded["created"], decoded["reused"])
}
skipped, _ = decoded["skipped"].([]any)
if len(skipped) != 2 {
t.Fatalf("re-import should skip both, got %v", skipped)
}
if len(f.projectSelection("slice")) != 4 {
t.Fatalf("re-import changed selection size: %v", f.projectSelection("slice"))
}
}
func TestProjectDictionaryLabelsImportKeepsKindsIsolatedAndNeverRevivesInactive(t *testing.T) {
f := newDictionaryImportFixture(t)
// Inactive historical slice term: same name must import as a NEW term.
inactive := serveJSONWithCookie(f.handler, http.MethodPost, "/api/annotation-labels", []byte(`{"kind":"slice","code":"hist","name":"旧名称","status":"inactive"}`), f.adminCookie)
if inactive.Code != http.StatusCreated {
t.Fatalf("seed inactive code=%d body=%s", inactive.Code, inactive.Body.String())
}
recorder, decoded := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import",
`{"kind":"discard_reason","names":["旧名称","其他","画面模糊"]}`)
if recorder.Code != http.StatusOK {
t.Fatalf("import discard code=%d body=%s", recorder.Code, recorder.Body.String())
}
if int(decoded["created"].(float64)) != 3 {
t.Fatalf("discard import created=%v", decoded["created"])
}
if len(f.projectSelection("slice")) != 0 {
t.Fatalf("discard import leaked into slice dictionary: %v", f.projectSelection("slice"))
}
items, err := f.dataStore.ListAnnotationLabels(t.Context(), f.org.ID)
if err != nil {
t.Fatal(err)
}
sliceCount, discardCount, otherRequiresNote := 0, 0, false
for _, item := range items {
switch {
case dictionaryKind(item.Kind) == "slice":
sliceCount++
case dictionaryKind(item.Kind) == "discard_reason":
discardCount++
if item.Name == "其他" && item.RequiresNote {
otherRequiresNote = true
}
}
}
if sliceCount != 1 || discardCount != 3 {
t.Fatalf("org pool slice=%d discard=%d", sliceCount, discardCount)
}
if !otherRequiresNote {
t.Fatal("discard 其他 must default to requiresNote")
}
// Same name into the slice dictionary creates a separate active term.
recorder, decoded = f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import",
`{"kind":"slice","names":["旧名称"]}`)
if recorder.Code != http.StatusOK || int(decoded["created"].(float64)) != 1 {
t.Fatalf("inactive-revival import code=%d created=%v body=%s", recorder.Code, decoded["created"], recorder.Body.String())
}
items, err = f.dataStore.ListAnnotationLabels(t.Context(), f.org.ID)
if err != nil {
t.Fatal(err)
}
sliceActive, sliceInactive := 0, 0
for _, item := range items {
if dictionaryKind(item.Kind) == "slice" {
if item.Status == domain.AnnotationLabelActive {
sliceActive++
} else {
sliceInactive++
}
}
}
if sliceActive != 1 || sliceInactive != 1 {
t.Fatalf("slice pool should hold one active new term and one inactive original, got active=%d inactive=%d", sliceActive, sliceInactive)
}
}
func TestProjectDictionaryLabelsImportGuards(t *testing.T) {
f := newDictionaryImportFixture(t)
viewerCookie := loginSession(t, f.handler, f.viewerUser.Phone, "viewer-password")
if recorder, _ := f.importLabels(viewerCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import", `{"kind":"slice","names":["越权"]}`); recorder.Code != http.StatusForbidden {
t.Fatalf("viewer import code=%d body=%s", recorder.Code, recorder.Body.String())
}
if recorder, _ := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import", `{"kind":"mood","names":["心情"]}`); recorder.Code != http.StatusBadRequest {
t.Fatalf("bad kind code=%d body=%s", recorder.Code, recorder.Body.String())
}
if recorder, _ := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import", `{"kind":"slice","names":[" ",""]}`); recorder.Code != http.StatusBadRequest {
t.Fatalf("empty names code=%d body=%s", recorder.Code, recorder.Body.String())
}
if recorder, _ := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import", `{"kind":"slice","names":[]}`); recorder.Code != http.StatusBadRequest {
t.Fatalf("no names code=%d body=%s", recorder.Code, recorder.Body.String())
}
overflow := make([]string, dictionaryImportMaxNames+1)
for index := range overflow {
overflow[index] = "超量名称" + strconv.Itoa(index)
}
body, _ := json.Marshal(map[string]any{"kind": "slice", "names": overflow})
if recorder, _ := f.importLabels(f.adminCookie, "/api/projects/"+f.project.ID+"/dictionary-labels/import", string(body)); recorder.Code != http.StatusBadRequest {
t.Fatalf("overflow code=%d body=%s", recorder.Code, recorder.Body.String())
}
if recorder, _ := f.importLabels(f.adminCookie, "/api/projects/missing-project/dictionary-labels/import", `{"kind":"slice","names":["不存在"]}`); recorder.Code != http.StatusNotFound {
t.Fatalf("missing project code=%d body=%s", recorder.Code, recorder.Body.String())
}
}

View File

@ -1615,6 +1615,7 @@ func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig
router.Put("/api/projects/{id}/annotation-labels", instance.handleProjectAnnotationLabelsPut)
router.Put("/api/projects/{id}/slice-labels", instance.handleProjectAnnotationLabelsPut)
router.Put("/api/projects/{id}/discard-reason-labels", instance.handleProjectAnnotationLabelsPut)
router.Post("/api/projects/{id}/dictionary-labels/import", instance.handleProjectDictionaryLabelsImport)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/cancel", instance.handleCaptureRecordCancel)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/reprocess", instance.handleCaptureRecordReprocess)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/discard", instance.handleCaptureRecordDiscard)

View File

@ -1,12 +1,19 @@
import { userErrorMessage } from "../lib/user-error-message.js";
import React, { useEffect, useRef, useState } from "react";
import { Edit3, Loader2, Plus, Save, Trash2, X } from "lucide-react";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { Download, Edit3, Loader2, Plus, Save, Trash2, Upload, X } from "lucide-react";
import { apiRequest } from "../services/api/client";
import { getActionErrorMessage } from "../lib/action-errors.js";
import { projectDictionaryNameKey, reuseOrCreateDictionaryLabel } from "../lib/project-dictionary.js";
import { parseDictionaryImport, planDictionaryImport, serializeDictionaryCSV } from "../lib/dictionary-batch.js";
import { cn } from "../lib/utils";
import type { AnnotationLabelRecord, ProjectAnnotationLabelsRecord } from "../types";
type DictionaryImportResponse = {
created?: number;
reused?: number;
skipped?: Array<{ name: string; reason: string }>;
};
export function AnnotationLabelsPanel({
projectId,
canManage,
@ -23,6 +30,7 @@ export function AnnotationLabelsPanel({
const noun = isSlice ? "切片名称" : isDiscard ? "舍弃原因" : "描述";
const title = isSlice ? "项目切片字典" : isDiscard ? "项目舍弃原因字典" : "项目动作描述字典";
const selectionPath = `/api/projects/${projectId}/${isSlice ? "slice-labels" : isDiscard ? "discard-reason-labels" : "annotation-labels"}`;
const importPath = `/api/projects/${projectId}/dictionary-labels/import`;
const [labels, setLabels] = useState<AnnotationLabelRecord[]>([]);
const [enabled, setEnabled] = useState<string[]>([]);
const [loading, setLoading] = useState(true);
@ -31,6 +39,10 @@ export function AnnotationLabelsPanel({
const [error, setError] = useState("");
const [editingLabelId, setEditingLabelId] = useState("");
const [inputText, setInputText] = useState("");
const [checkedIds, setCheckedIds] = useState<string[]>([]);
const [importOpen, setImportOpen] = useState(false);
const [importText, setImportText] = useState("");
const [importNotice, setImportNotice] = useState("");
const scope = useRef(0);
const load = async (generation: number, signal: AbortSignal) => {
@ -59,6 +71,11 @@ export function AnnotationLabelsPanel({
}
};
const reload = () => {
const generation = ++scope.current;
void load(generation, new AbortController().signal);
};
useEffect(() => {
const generation = ++scope.current;
const controller = new AbortController();
@ -66,6 +83,10 @@ export function AnnotationLabelsPanel({
setEnabled([]);
setEditingLabelId("");
setInputText("");
setCheckedIds([]);
setImportOpen(false);
setImportText("");
setImportNotice("");
setSaving(false);
setSaveSuccess(false);
void load(generation, controller.signal);
@ -180,7 +201,102 @@ export function AnnotationLabelsPanel({
}
};
const deleteChecked = async () => {
if (!checkedIds.length || saving) return;
if (!window.confirm(`确认从本项目移除选中的 ${checkedIds.length} 个${noun}?已保存的标注和审核记录不会改变。`))
return;
setSaving(true);
setSaveSuccess(false);
setError("");
const generation = scope.current;
try {
const nextEnabled = enabled.filter((id) => !checkedIds.includes(id));
const selected = await apiRequest<ProjectAnnotationLabelsRecord>(
selectionPath,
{ method: "PUT" },
{ labelIds: nextEnabled },
);
if (scope.current !== generation) return;
setEnabled(selected.labelIds || []);
setCheckedIds([]);
if (editingLabelId && checkedIds.includes(editingLabelId)) resetEditing();
} catch (delError) {
if (scope.current !== generation) return;
setError(getActionErrorMessage(delError, `批量删除${noun}失败`));
} finally {
if (scope.current === generation) setSaving(false);
}
};
const toggleChecked = (id: string) => {
setCheckedIds((ids) => (ids.includes(id) ? ids.filter((value) => value !== id) : [...ids, id]));
};
const activeLabels = labels.filter((item) => item.status !== "inactive" && enabled.includes(item.id));
const activeIds = activeLabels.map((item) => item.id);
const allChecked = activeIds.length > 0 && activeIds.every((id) => checkedIds.includes(id));
const exportCSV = () => {
const csv = serializeDictionaryCSV(activeLabels.map((item) => item.name || item.description || ""));
const blob = new Blob([csv], { type: "text/csv;charset=utf-8" });
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
const date = new Date().toISOString().slice(0, 10).replace(/-/g, "");
anchor.href = url;
anchor.download = `${title}-${date}.csv`;
document.body.appendChild(anchor);
anchor.click();
anchor.remove();
URL.revokeObjectURL(url);
};
const parsedImport = useMemo(() => parseDictionaryImport(importText), [importText]);
const importPlan = useMemo(
() => planDictionaryImport({ kind, names: parsedImport.names, items: labels, enabledIds: enabled }),
[kind, parsedImport.names, labels, enabled],
);
const readImportFile = (file: File | undefined) => {
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
setImportText(String(reader.result || ""));
setImportNotice("");
setError("");
};
reader.readAsText(file);
};
const runImport = async () => {
if (saving || loading || !parsedImport.names.length) return;
setSaving(true);
setSaveSuccess(false);
setError("");
setImportNotice("");
const generation = scope.current;
try {
const result = await apiRequest<DictionaryImportResponse>(
importPath,
{ method: "POST" },
{
kind,
names: parsedImport.names,
},
);
if (scope.current !== generation) return;
const skipped = result.skipped?.length || 0;
setImportNotice(
`导入完成:新增 ${result.created || 0} 个,复用组织词条 ${result.reused || 0} 个,跳过 ${skipped} 个。`,
);
setImportText("");
reload();
} catch (importError) {
if (scope.current !== generation) return;
setError(getActionErrorMessage(importError, `导入${noun}失败`));
} finally {
if (scope.current === generation) setSaving(false);
}
};
return (
<section className="space-y-4" aria-label={title}>
@ -226,6 +342,119 @@ export function AnnotationLabelsPanel({
</form>
)}
{/* Batch toolbar: export, import, batch remove */}
{canManage && (
<div className="flex flex-wrap items-center gap-2">
<button
type="button"
onClick={exportCSV}
disabled={loading || !activeLabels.length}
className="inline-flex items-center gap-1.5 rounded-lg border border-[color:var(--app-border)] bg-[color:var(--app-panel)] px-4 py-2 text-sm font-medium text-slate-600 shadow-sm hover:bg-slate-50 disabled:opacity-50 dark:text-gray-300"
>
<Download className="h-4 w-4" />
导出 CSV
</button>
<button
type="button"
onClick={() => {
setImportOpen((open) => !open);
setImportNotice("");
}}
disabled={loading}
aria-expanded={importOpen}
className="inline-flex items-center gap-1.5 rounded-lg border border-[color:var(--app-border)] bg-[color:var(--app-panel)] px-4 py-2 text-sm font-medium text-slate-600 shadow-sm hover:bg-slate-50 disabled:opacity-50 dark:text-gray-300"
>
<Upload className="h-4 w-4" />
{importOpen ? "收起导入" : "批量导入"}
</button>
{activeLabels.length > 0 && (
<div className="ml-auto flex flex-wrap items-center gap-2">
<label className="inline-flex items-center gap-1.5 text-sm text-slate-600 dark:text-gray-300">
<input
type="checkbox"
checked={allChecked}
onChange={() => setCheckedIds(allChecked ? [] : activeIds)}
className="h-4 w-4 rounded border-[color:var(--app-border)]"
aria-label="全选词条"
/>
全选
</label>
<button
type="button"
onClick={() => void deleteChecked()}
disabled={saving || !checkedIds.length}
className="inline-flex items-center gap-1.5 rounded-lg border border-red-500/30 bg-red-500/10 px-4 py-2 text-sm font-medium text-red-600 shadow-sm hover:bg-red-500/20 disabled:opacity-50 dark:text-red-300"
>
<Trash2 className="h-4 w-4" />
删除所选{checkedIds.length ? `(${checkedIds.length})` : ""}
</button>
</div>
)}
</div>
)}
{/* Import dialog: paste or upload one name per line */}
{canManage && importOpen && (
<div className="space-y-3 rounded-xl border border-[color:var(--app-border)] bg-[color:var(--app-inset)] p-4 shadow-sm">
<div className="text-sm text-slate-600 dark:text-gray-300">
粘贴或上传名称列表,每行一个{noun};同名词条自动去重,已在本项目的自动跳过。可直接使用「导出 CSV」的文件。
</div>
<input
type="file"
accept=".csv,.txt"
onChange={(e) => readImportFile(e.target.files?.[0])}
className="block w-full text-sm text-slate-600 file:mr-3 file:rounded-lg file:border-0 file:bg-blue-600 file:px-3 file:py-1.5 file:text-sm file:font-medium file:text-white hover:file:bg-blue-700 dark:text-gray-300"
aria-label={`选择要导入的${noun}文件`}
/>
<textarea
value={importText}
onChange={(e) => {
setImportText(e.target.value);
setImportNotice("");
setError("");
}}
rows={6}
placeholder={`每行一个${noun},例如:\n${isSlice ? "商品检查" : isDiscard ? "调整头环" : "右手抓取水瓶"}`}
className="w-full rounded-lg border border-[color:var(--app-border)] bg-[color:var(--app-panel)] px-4 py-2.5 text-sm text-[color:var(--app-text)] shadow-sm focus:border-blue-500 focus:outline-none"
/>
{(parsedImport.names.length > 0 || parsedImport.invalid.length > 0) && (
<div className="text-xs text-slate-500 dark:text-gray-400">
待导入 {importPlan.toImport.length} 个 · 已在本项目 {importPlan.alreadySelected.length} 个 · 输入重复{" "}
{importPlan.duplicates.length} 个
{parsedImport.invalid.length ? ` · 无法识别 ${parsedImport.invalid.length} 行(一行只能有一个名称)` : ""}
</div>
)}
{importNotice && (
<div className="rounded-lg border border-emerald-500/20 bg-emerald-500/10 px-4 py-2.5 text-sm text-emerald-700 dark:text-emerald-300">
✓ {importNotice}
</div>
)}
<div className="flex flex-wrap items-center gap-2">
<button
type="button"
onClick={() => void runImport()}
disabled={loading || saving || !importPlan.toImport.length}
className="inline-flex items-center gap-1.5 rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-blue-700 disabled:opacity-50"
>
<Upload className="h-4 w-4" />
导入 {importPlan.toImport.length} 个名称
</button>
<button
type="button"
onClick={() => {
setImportOpen(false);
setImportText("");
setImportNotice("");
}}
className="inline-flex items-center gap-1.5 rounded-lg border border-[color:var(--app-border)] bg-[color:var(--app-panel)] px-4 py-2 text-sm font-medium text-slate-600 shadow-sm hover:bg-slate-50 dark:text-gray-300"
>
<X className="h-4 w-4" />
关闭
</button>
</div>
</div>
)}
{error && (
<div className="rounded-lg border border-red-500/20 bg-red-500/10 px-4 py-3 text-sm text-red-600 dark:text-red-200">
{userErrorMessage(error)}
@ -252,6 +481,7 @@ export function AnnotationLabelsPanel({
{activeLabels.map((item, index) => {
const text = item.name || item.description;
const isEditing = editingLabelId === item.id;
const isChecked = checkedIds.includes(item.id);
return (
<div
key={item.id}
@ -260,6 +490,16 @@ export function AnnotationLabelsPanel({
isEditing && "bg-blue-50/50 dark:bg-blue-900/10",
)}
>
{canManage && (
<input
type="checkbox"
checked={isChecked}
onChange={() => toggleChecked(item.id)}
disabled={saving}
className="h-4 w-4 shrink-0 rounded border-[color:var(--app-border)]"
aria-label={`选择${noun} ${text}`}
/>
)}
<span className="text-xs font-mono text-slate-400 select-none w-5 text-right shrink-0">
{index + 1}.
</span>
@ -298,7 +538,7 @@ export function AnnotationLabelsPanel({
)}
</div>
<p className="text-sm text-slate-500">
编辑或删除只影响本项目的可选词条,不影响其他项目及已保存的标注、审核记录。
支持导出 CSV、批量导入与勾选批量删除;编辑或删除只影响本项目的可选词条,不影响其他项目及已保存的标注、审核记录。
</p>
</section>
);

112
src/lib/dictionary-batch.js Normal file
View File

@ -0,0 +1,112 @@
import { projectDictionaryNameKey } from "./project-dictionary.js";
/** Header used by export/import CSV so Excel opens the file as one column. */
export const DICTIONARY_CSV_HEADER = "名称";
function csvCell(value) {
const text = String(value ?? "");
return /[",\r\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
}
/** Serialize dictionary names into an Excel-friendly CSV (UTF-8 BOM + header). */
export function serializeDictionaryCSV(names) {
const rows = [DICTIONARY_CSV_HEADER];
for (const name of names || []) {
const text = String(name ?? "").trim();
if (text) rows.push(csvCell(text));
}
return `\ufeff${rows.join("\r\n")}\r\n`;
}
/** Split one CSV line into cells; returns null for unterminated quoting. */
function splitCSVLine(line) {
const cells = [];
let current = "";
let quoted = false;
let terminated = true;
for (let index = 0; index < line.length; index += 1) {
const char = line[index];
if (quoted) {
if (char === '"') {
if (line[index + 1] === '"') {
current += '"';
index += 1;
} else {
quoted = false;
}
} else {
current += char;
}
} else if (char === '"' && current === "") {
quoted = true;
} else if (char === ",") {
cells.push(current);
current = "";
} else {
current += char;
}
}
if (quoted) terminated = false;
cells.push(current);
return terminated ? cells : null;
}
/**
* Parse pasted or uploaded dictionary content. Accepts the exported CSV (BOM +
* header tolerated) or plain one-name-per-line text. Rows with more than one
* column are reported as invalid instead of being silently truncated.
*/
export function parseDictionaryImport(text) {
const source = String(text ?? "").replace(/^\ufeff/, "");
const names = [];
const invalid = [];
const lines = source.split(/\r\n|\n|\r/);
let sawHeader = false;
for (const rawLine of lines) {
const line = rawLine.trim();
if (!line) continue;
if (!sawHeader && line === DICTIONARY_CSV_HEADER) {
sawHeader = true;
continue;
}
sawHeader = true;
const cells = splitCSVLine(line);
if (!cells || cells.length !== 1) {
invalid.push(line);
continue;
}
const name = cells[0].replace(/\s+/g, " ").trim();
if (name) names.push(name);
}
return { names, invalid };
}
/**
* Classify parsed names against the panel state: already-selected entries are
* skipped client-side, duplicates collapse to the first occurrence, and the
* rest are sent to the server import endpoint (which owns create-vs-reuse).
*/
export function planDictionaryImport({ kind, names, items, enabledIds }) {
const enabled = new Set(enabledIds || []);
const selectedKeys = new Set();
for (const item of items || []) {
if (enabled.has(item.id) && item.status !== "inactive") {
selectedKeys.add(projectDictionaryNameKey(kind, item.name));
}
}
const seen = new Set();
const toImport = [];
const alreadySelected = [];
const duplicates = [];
for (const name of names || []) {
const key = projectDictionaryNameKey(kind, name);
if (seen.has(key)) {
duplicates.push(name);
continue;
}
seen.add(key);
if (selectedKeys.has(key)) alreadySelected.push(name);
else toImport.push(name);
}
return { toImport, alreadySelected, duplicates };
}

View File

@ -0,0 +1,32 @@
import { readFileSync } from "node:fs";
import assert from "node:assert/strict";
import test from "node:test";
test("dictionary panel wires export, bulk import and batch remove for admins", () => {
const panel = readFileSync("src/components/AnnotationLabelsPanel.tsx", "utf8");
// Bulk import posts to the dedicated endpoint with the panel's dictionary kind.
assert.ok(panel.includes("/dictionary-labels/import"));
assert.ok(panel.includes("names: parsedImport.names"));
// Export serializes through the shared CSV helper with a BOM for Excel.
assert.ok(panel.includes("serializeDictionaryCSV"));
assert.ok(panel.includes("导出 CSV"));
// Batch remove reuses the authoritative project selection PUT, never org-wide deletes.
assert.ok(panel.includes("删除所选"));
assert.ok(panel.includes("labelIds: nextEnabled"));
// Import preview classifies names before submit and disables the button when empty.
assert.ok(panel.includes("importPlan.toImport.length"));
assert.ok(panel.includes("planDictionaryImport"));
});
test("dictionary batch routes stay admin-gated on the server", () => {
const server = readFileSync("internal/api/server.go", "utf8");
const handler = readFileSync("internal/api/annotation_labels_import.go", "utf8");
assert.ok(server.includes('router.Post("/api/projects/{id}/dictionary-labels/import"'));
// The route lives inside the admin group AND the handler re-checks the role.
const routeIndex = server.indexOf("dictionary-labels/import");
const groupStart = server.lastIndexOf("requireBusinessRole(domain.UserRoleAdmin)", routeIndex);
const groupEnd = server.indexOf("})", routeIndex);
assert.ok(groupStart > -1 && groupStart < groupEnd);
assert.ok(handler.includes("if !annotationManager(principal)"));
assert.ok(handler.includes("dictionaryImportMaxNames"));
});

View File

@ -0,0 +1,60 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
DICTIONARY_CSV_HEADER,
parseDictionaryImport,
planDictionaryImport,
serializeDictionaryCSV,
} from "../../src/lib/dictionary-batch.js";
test("dictionary CSV export carries BOM, header and one name per row", () => {
const csv = serializeDictionaryCSV(["商品检查", "拿取前", ""]);
assert.equal(csv, `\ufeff${DICTIONARY_CSV_HEADER}\r\n商品检查\r\n拿取前\r\n`);
});
test("dictionary CSV export escapes commas, quotes and newlines", () => {
const csv = serializeDictionaryCSV(["含,逗号", '含"引号', "含\n换行"]);
const rows = csv.split("\r\n");
assert.equal(rows[1], '"含,逗号"');
assert.equal(rows[2], '"含""引号"');
assert.equal(rows.length, 5);
});
test("dictionary import parses export round-trip and plain text", () => {
const roundTrip = parseDictionaryImport(serializeDictionaryCSV(["商品检查", "调整,头环"]));
assert.deepEqual(roundTrip.names, ["商品检查", "调整,头环"]);
assert.deepEqual(roundTrip.invalid, []);
const plain = parseDictionaryImport("拿取前\r\n\r\n 商品 检查 \n调整头环");
assert.deepEqual(plain.names, ["拿取前", "商品 检查", "调整头环"]);
});
test("dictionary import reports multi-column rows as invalid instead of truncating", () => {
const parsed = parseDictionaryImport("名称\r\n商品检查,多余列\r\n正常名称");
assert.deepEqual(parsed.names, ["正常名称"]);
assert.deepEqual(parsed.invalid, ["商品检查,多余列"]);
});
test("dictionary import plan skips selected, collapses duplicates by name key", () => {
const items = [
{ id: "a", name: "商品检查", status: "active" },
{ id: "b", name: "拿取 前", status: "active" },
{ id: "c", name: "已停用", status: "inactive" },
];
// "拿取 前" collapses to the single-space key of the selected "拿取 前";
// "商品 检查" (inner space) stays a distinct name, matching server keys.
const plan = planDictionaryImport({
kind: "slice",
names: ["商品检查", "拿取 前", "新名称", "新名称"],
items,
enabledIds: ["a", "b", "c"],
});
assert.deepEqual(plan.alreadySelected, ["商品检查", "拿取 前"]);
assert.deepEqual(plan.toImport, ["新名称"]);
assert.deepEqual(plan.duplicates, ["新名称"]);
});
test("dictionary import parser never revives cross-line quoting confusion", () => {
const parsed = parseDictionaryImport('"未闭合引号');
assert.deepEqual(parsed.names, []);
assert.equal(parsed.invalid.length, 1);
});