From ea342fc0fdcb79bf236192c5ac562c4fcb8b6b6f Mon Sep 17 00:00:00 2001
From: yokowu <18836617@qq.com>
Date: Mon, 7 Sep 2026 22:30:17 +0800
Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=88=86=E7=BB=84=E6=93=8D?=
=?UTF-8?q?=E4=BD=9C=E5=B9=B6=E5=B0=86=E5=9B=A2=E9=98=9F=E6=A0=B9=E8=8A=82?=
=?UTF-8?q?=E7=82=B9=E6=94=B9=E4=B8=BA=E8=99=9A=E6=8B=9F=E5=88=86=E7=BB=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../admin/src/components/group-manager.tsx | 250 -------------
.../members/group-action-dialog.tsx | 304 +++++++++++++++
.../src/components/members/group-tree.tsx | 187 ++++++++++
.../src/components/skill-import-wizard.tsx | 4 +-
monkeyai/admin/src/i18n/locales/ar.ts | 6 +
monkeyai/admin/src/i18n/locales/de-DE.ts | 6 +
monkeyai/admin/src/i18n/locales/en-US.ts | 6 +
monkeyai/admin/src/i18n/locales/es-419.ts | 6 +
monkeyai/admin/src/i18n/locales/fr-FR.ts | 6 +
monkeyai/admin/src/i18n/locales/ja-JP.ts | 5 +
monkeyai/admin/src/i18n/locales/ko-KR.ts | 6 +
monkeyai/admin/src/i18n/locales/ru-RU.ts | 5 +
monkeyai/admin/src/i18n/locales/zh-CN.ts | 4 +
monkeyai/admin/src/i18n/locales/zh-TW.ts | 3 +
monkeyai/admin/src/lib/member-groups.ts | 52 +++
monkeyai/admin/src/lib/resources.ts | 3 +-
.../src/pages/members-and-groups-page.tsx | 162 +++++---
monkeyai/admin/src/pages/rules-page.tsx | 9 +-
monkeyai/admin/src/pages/skills-page.tsx | 5 +-
monkeyai/admin/src/pages/tools-page.tsx | 5 +-
monkeyai/admin/test/member-groups.test.mjs | 37 ++
monkeyai/backend/README.md | 2 +-
monkeyai/backend/api/admin.yaml | 332 +++++++++--------
monkeyai/backend/internal/app/app.go | 2 +
monkeyai/backend/internal/app/billing_test.go | 26 ++
.../backend/internal/app/resources_test.go | 15 +-
monkeyai/backend/internal/app/sharing_test.go | 36 ++
monkeyai/backend/internal/billing/admin.go | 47 ++-
monkeyai/backend/internal/billing/service.go | 17 +-
.../backend/internal/billing/service_test.go | 6 +-
.../backend/internal/billing/transaction.go | 2 +-
monkeyai/backend/internal/group/admin.go | 65 ++++
monkeyai/backend/internal/group/service.go | 263 +++++++++++++
.../backend/internal/group/service_test.go | 351 ++++++++++++++++++
monkeyai/backend/internal/identity/admin.go | 2 +-
monkeyai/backend/internal/identity/groups.go | 198 ----------
monkeyai/backend/internal/model/postgres.go | 4 +-
monkeyai/backend/internal/resource/store.go | 4 +-
.../000001_initial_create_schema.up.sql | 5 -
.../000002_billing_create_transactions.up.sql | 4 +-
.../000003_group_virtual_root.down.sql | 4 +
.../000003_group_virtual_root.up.sql | 89 +++++
monkeyai/backend/migrations/README.md | 19 +-
.../backend/migrations/migrations_test.go | 2 +-
monkeyai/design/admin-data-model.md | 16 +-
.../design/ai-resource-implementation-plan.md | 8 +-
.../design/billing-implementation-plan.md | 14 +-
47 files changed, 1856 insertions(+), 748 deletions(-)
delete mode 100644 monkeyai/admin/src/components/group-manager.tsx
create mode 100644 monkeyai/admin/src/components/members/group-action-dialog.tsx
create mode 100644 monkeyai/admin/src/components/members/group-tree.tsx
create mode 100644 monkeyai/admin/src/lib/member-groups.ts
create mode 100644 monkeyai/admin/test/member-groups.test.mjs
create mode 100644 monkeyai/backend/internal/group/admin.go
create mode 100644 monkeyai/backend/internal/group/service.go
create mode 100644 monkeyai/backend/internal/group/service_test.go
create mode 100644 monkeyai/backend/migrations/000003_group_virtual_root.down.sql
create mode 100644 monkeyai/backend/migrations/000003_group_virtual_root.up.sql
diff --git a/monkeyai/admin/src/components/group-manager.tsx b/monkeyai/admin/src/components/group-manager.tsx
deleted file mode 100644
index 092189de..00000000
--- a/monkeyai/admin/src/components/group-manager.tsx
+++ /dev/null
@@ -1,250 +0,0 @@
-import { useCallback, useEffect, useState } from "react"
-import { Button } from "@/components/ui/button"
-import {
- Dialog,
- DialogContent,
- DialogDescription,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog"
-import { Input } from "@/components/ui/input"
-import { Field, FieldLabel } from "@/components/ui/field"
-import { api } from "@/lib/api"
-
-type Group = { id: string; parent_id: string | null; name: string }
-type Groups = {
- groups: Group[]
- memberships: { group_id: string; user_id: string }[]
-}
-type User = { id: string; name: string; email: string }
-const rootID = "00000000-0000-0000-0000-000000000001"
-const adminID = "00000000-0000-0000-0000-000000000002"
-const selectClass = "h-9 w-full rounded-md border bg-background px-3 text-sm"
-
-export function GroupManager() {
- const [open, setOpen] = useState(false)
- return (
- <>
-
- {open && setOpen(false)} />}
- >
- )
-}
-function GroupEditor({ onClose }: { onClose: () => void }) {
- const [data, setData] = useState(null)
- const [users, setUsers] = useState([])
- const [id, setID] = useState("")
- const [name, setName] = useState("")
- const [parent, setParent] = useState(rootID)
- const [members, setMembers] = useState([])
- const [busy, setBusy] = useState(false)
- const [error, setError] = useState("")
- const [notice, setNotice] = useState("")
- const load = useCallback(
- () =>
- Promise.all([
- api("/api/admin/v1/groups"),
- api<{ users: User[] }>("/api/admin/v1/users"),
- ]).then(([g, u]) => {
- setData(g)
- setUsers(u.users)
- }),
- []
- )
- useEffect(() => {
- void load().catch((e: Error) => setError(e.message))
- }, [load])
- const select = (value: string) => {
- const g = data?.groups.find((item) => item.id === value)
- setID(value)
- setName(g?.name ?? "")
- setParent(g?.parent_id ?? rootID)
- setMembers(
- data?.memberships
- .filter((m) => m.group_id === value)
- .map((m) => m.user_id) ?? []
- )
- setError("")
- setNotice("")
- }
- const save = async (action: "group" | "members" | "delete") => {
- setBusy(true)
- setError("")
- setNotice("")
- try {
- if (action === "group") {
- const result = await api<{ id: string }>(
- `/api/admin/v1/groups${id ? `/${id}` : ""}`,
- {
- method: id ? "PATCH" : "POST",
- body: JSON.stringify({ name, parent_id: parent }),
- }
- )
- setID(result.id)
- }
- if (action === "members")
- await api(`/api/admin/v1/groups/${id}/members`, {
- method: "PUT",
- body: JSON.stringify({ user_ids: members }),
- })
- if (action === "delete") {
- await api(`/api/admin/v1/groups/${id}`, { method: "DELETE" })
- setID("")
- setName("")
- setMembers([])
- }
- await load()
- setNotice(action === "delete" ? "空分组已删除" : "分组设置已保存")
- } catch (e) {
- setError((e as Error).message)
- } finally {
- setBusy(false)
- }
- }
- const fixed = id === rootID || id === adminID
- return (
-
- )
-}
diff --git a/monkeyai/admin/src/components/members/group-action-dialog.tsx b/monkeyai/admin/src/components/members/group-action-dialog.tsx
new file mode 100644
index 00000000..4a81b9f6
--- /dev/null
+++ b/monkeyai/admin/src/components/members/group-action-dialog.tsx
@@ -0,0 +1,304 @@
+import { useState, type FormEvent } from "react"
+import { useTranslation } from "react-i18next"
+
+import { Button } from "@/components/ui/button"
+import { Checkbox } from "@/components/ui/checkbox"
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog"
+import { Field, FieldLabel } from "@/components/ui/field"
+import { Input } from "@/components/ui/input"
+import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
+import { api } from "@/lib/api"
+import {
+ ROOT_GROUP_ID,
+ descendantIDs,
+ type MemberGroup,
+} from "@/lib/member-groups"
+import { groupActionKeys, type ActiveGroupAction } from "@/lib/member-groups"
+
+const submitKeys = {
+ "add-subgroup": "createGroup",
+ rename: "saveChanges",
+ "adjust-members": "applyChanges",
+ move: "moveAction",
+ delete: "deleteAction",
+}
+
+export function GroupActionDialog({
+ action,
+ group,
+ groups,
+ users,
+ onClose,
+ onSaved,
+}: ActiveGroupAction & {
+ groups: MemberGroup[]
+ users: Array<{ id: string; name: string; email: string }>
+ onClose: () => void
+ onSaved: (group: MemberGroup | null) => void
+}) {
+ const { t } = useTranslation()
+ const [name, setName] = useState(action === "rename" ? group.name : "")
+ const [memberIDs, setMemberIDs] = useState(group.member_ids)
+ const [query, setQuery] = useState("")
+ const [parentID, setParentID] = useState("")
+ const [saving, setSaving] = useState(false)
+ const [error, setError] = useState("")
+ const excluded = descendantIDs(groups, group.id)
+ const targets = groups.filter(
+ (candidate) =>
+ !excluded.has(candidate.id) && candidate.id !== group.parent_id
+ )
+ const normalized = query.trim().toLocaleLowerCase()
+ const visible = users.filter((user) =>
+ `${user.name} ${user.email}`.toLocaleLowerCase().includes(normalized)
+ )
+ const titleKey = groupActionKeys[action]
+ const hasChildren = groups.some(
+ (candidate) => candidate.parent_id === group.id
+ )
+
+ const targetPath = (candidate: MemberGroup) => {
+ const names = [candidate.name]
+ const visited = new Set([candidate.id])
+ let parent = groups.find((item) => item.id === candidate.parent_id)
+ while (parent && !visited.has(parent.id)) {
+ names.unshift(parent.name)
+ visited.add(parent.id)
+ parent = groups.find((item) => item.id === parent?.parent_id)
+ }
+ return names.join(" / ")
+ }
+
+ const submit = async (event: FormEvent) => {
+ event.preventDefault()
+ if (saving) return
+ setSaving(true)
+ setError("")
+ try {
+ const path = `/api/admin/v1/groups/${group.id}`
+ let updated: MemberGroup | null = null
+ if (action === "delete") {
+ await api(path, { method: "DELETE" })
+ } else if (action === "add-subgroup") {
+ updated = await api("/api/admin/v1/groups", {
+ method: "POST",
+ body: JSON.stringify({
+ name: name.trim(),
+ parent_id: group.id === ROOT_GROUP_ID ? null : group.id,
+ }),
+ })
+ } else if (action === "adjust-members") {
+ updated = await api(`${path}/members`, {
+ method: "PUT",
+ body: JSON.stringify({ member_ids: memberIDs }),
+ })
+ } else {
+ updated = await api(path, {
+ method: "PATCH",
+ body: JSON.stringify(
+ action === "rename"
+ ? { name: name.trim() }
+ : { parent_id: parentID === ROOT_GROUP_ID ? null : parentID }
+ ),
+ })
+ }
+ onSaved(updated)
+ } catch (reason) {
+ setError((reason as Error).message)
+ } finally {
+ setSaving(false)
+ }
+ }
+
+ return (
+
+ )
+}
diff --git a/monkeyai/admin/src/components/members/group-tree.tsx b/monkeyai/admin/src/components/members/group-tree.tsx
new file mode 100644
index 00000000..f361cb3d
--- /dev/null
+++ b/monkeyai/admin/src/components/members/group-tree.tsx
@@ -0,0 +1,187 @@
+import { useState } from "react"
+import {
+ Add01Icon,
+ Delete02Icon,
+ Edit02Icon,
+ Folder02Icon,
+ FolderIcon,
+ MoreHorizontalIcon,
+ MoveIcon,
+ UserMultiple02Icon,
+} from "@hugeicons/core-free-icons"
+import { HugeiconsIcon } from "@hugeicons/react"
+import { useTranslation } from "react-i18next"
+
+import { Button } from "@/components/ui/button"
+import {
+ Collapsible,
+ CollapsibleContent,
+ CollapsibleTrigger,
+} from "@/components/ui/collapsible"
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuGroup,
+ DropdownMenuItem,
+ DropdownMenuSeparator,
+ DropdownMenuTrigger,
+} from "@/components/ui/dropdown-menu"
+import {
+ groupActionKeys,
+ type GroupAction,
+ type ActiveGroupAction,
+ ROOT_GROUP_ID,
+ groupMemberIDs,
+ descendantIDs,
+ type MemberGroup,
+} from "@/lib/member-groups"
+import { cn } from "@/lib/utils"
+
+const actionIcons = {
+ "add-subgroup": Add01Icon,
+ rename: Edit02Icon,
+ "adjust-members": UserMultiple02Icon,
+ move: MoveIcon,
+ delete: Delete02Icon,
+}
+
+type Props = {
+ group: MemberGroup
+ groups: MemberGroup[]
+ users: Array<{ id: string; role: "admin" | "user" }>
+ selectedID: string
+ onSelect: (id: string) => void
+ onAction: (action: ActiveGroupAction) => void
+ level?: number
+}
+
+export function GroupTreeItem({
+ group,
+ groups,
+ users,
+ selectedID,
+ onSelect,
+ onAction,
+ level = 0,
+}: Props) {
+ const { t } = useTranslation()
+ const [expanded, setExpanded] = useState(true)
+ const open =
+ expanded ||
+ (selectedID !== group.id && descendantIDs(groups, group.id).has(selectedID))
+ const children = groups.filter((child) => child.parent_id === group.id)
+ const isRoot = group.id === ROOT_GROUP_ID
+ const actions: GroupAction[] = isRoot
+ ? ["add-subgroup"]
+ : ["add-subgroup", "rename", "adjust-members", "move", "delete"]
+ const count = groupMemberIDs(groups, users, group.id).size
+ const label = (
+ <>
+
+ {group.name}
+ >
+ )
+ const selectButton = (
+
)}
+ {loading && (
+
+ {t("common.loading")}
+
+ )}
{visibleUsers.map((user) => {
const isDisabled = user.status === "disabled"
@@ -378,7 +386,7 @@ export function MembersAndGroupsPage() {
)
})}
- {visibleUsers.length === 0 && (
+ {!loading && visibleUsers.length === 0 && (
{t("pages.membersAndGroups.noMembersFound")}
@@ -388,6 +396,40 @@ export function MembersAndGroupsPage() {
+ {activeGroupAction && (
+ setActiveGroupAction(null)}
+ onSaved={(updated) => {
+ if (updated) {
+ setGroups((current) =>
+ current.some((group) => group.id === updated.id)
+ ? current.map((group) =>
+ group.id === updated.id ? updated : group
+ )
+ : [...current, updated]
+ )
+ setSelectedGroupID(updated.id)
+ } else {
+ setGroups((current) =>
+ current.filter(
+ (group) => group.id !== activeGroupAction.group.id
+ )
+ )
+ if (selectedGroupID === activeGroupAction.group.id)
+ setSelectedGroupID(
+ activeGroupAction.group.parent_id ?? ROOT_GROUP_ID
+ )
+ }
+ setError("")
+ setActiveGroupAction(null)
+ }}
+ />
+ )}
+