feat: 新增岗位管理、提示词模板与资源标签,完善用户提示词与推荐问题接口
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2f414c6e08
commit
3855c30edd
52 files changed
+3143
-57
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import { apiFetch } from "@/core/api";
|
||||
import { getBackendBaseURL } from "@/core/config";
|
||||
|
||||
import type {
|
||||
Position,
|
||||
PositionCreateInput,
|
||||
PositionListResponse,
|
||||
PositionMember,
|
||||
PositionUpdateInput,
|
||||
PositionUser,
|
||||
TagBindings,
|
||||
TagBindingsResponse,
|
||||
} from "./types";
|
||||
|
||||
async function parseDetail(res: Response, fallback: string): Promise<Error> {
|
||||
const data = (await res.json().catch(() => ({}))) as { detail?: unknown };
|
||||
const detail = typeof data.detail === "string" && data.detail ? data.detail : null;
|
||||
return new Error(detail ?? `${fallback} (HTTP ${res.status})`);
|
||||
}
|
||||
|
||||
const base = () => `${getBackendBaseURL()}/api/positions`;
|
||||
|
||||
export async function listPositions(): Promise<Position[]> {
|
||||
const res = await apiFetch(base());
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to list positions");
|
||||
const data = (await res.json()) as PositionListResponse;
|
||||
return data.positions;
|
||||
}
|
||||
|
||||
export async function createPosition(input: PositionCreateInput): Promise<Position> {
|
||||
const res = await apiFetch(base(), {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to create position");
|
||||
return (await res.json()) as Position;
|
||||
}
|
||||
|
||||
export async function updatePosition(id: string, input: PositionUpdateInput): Promise<Position> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(id)}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(input),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to update position");
|
||||
return (await res.json()) as Position;
|
||||
}
|
||||
|
||||
export async function deletePosition(id: string): Promise<void> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(id)}`, { method: "DELETE" });
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to delete position");
|
||||
}
|
||||
|
||||
export async function getTagBindings(positionId: string): Promise<TagBindings> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/tag-bindings`);
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to load tag bindings");
|
||||
const data = (await res.json()) as TagBindingsResponse;
|
||||
return data.bindings;
|
||||
}
|
||||
|
||||
export async function setTagBindings(positionId: string, bindings: TagBindings): Promise<TagBindings> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/tag-bindings`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ bindings }),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to save tag bindings");
|
||||
const data = (await res.json()) as TagBindingsResponse;
|
||||
return data.bindings;
|
||||
}
|
||||
|
||||
export async function listMembers(positionId: string): Promise<PositionMember[]> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`);
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to list members");
|
||||
const data = (await res.json()) as { members: PositionMember[] };
|
||||
return data.members;
|
||||
}
|
||||
|
||||
export async function addMembers(positionId: string, userIds: string[]): Promise<PositionMember[]> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_ids: userIds }),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to add members");
|
||||
const data = (await res.json()) as { members: PositionMember[] };
|
||||
return data.members;
|
||||
}
|
||||
|
||||
export async function removeMembers(positionId: string, userIds: string[]): Promise<PositionMember[]> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/members`, {
|
||||
method: "DELETE",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_ids: userIds }),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to remove members");
|
||||
const data = (await res.json()) as { members: PositionMember[] };
|
||||
return data.members;
|
||||
}
|
||||
|
||||
export async function resyncPosition(positionId: string): Promise<number> {
|
||||
const res = await apiFetch(`${base()}/${encodeURIComponent(positionId)}/resync`, { method: "POST" });
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to resync position");
|
||||
const data = (await res.json()) as { synced: number };
|
||||
return data.synced;
|
||||
}
|
||||
|
||||
export async function listPositionUsers(): Promise<PositionUser[]> {
|
||||
const res = await apiFetch(`${base()}/users`);
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to list users");
|
||||
const data = (await res.json()) as { users: PositionUser[] };
|
||||
return data.users;
|
||||
}
|
||||
|
||||
export async function setUserPosition(userId: string, positionId: string | null): Promise<void> {
|
||||
const res = await apiFetch(`${base()}/users/${encodeURIComponent(userId)}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ position_id: positionId }),
|
||||
});
|
||||
if (!res.ok) throw await parseDetail(res, "Failed to set user position");
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
|
||||
import {
|
||||
addMembers,
|
||||
createPosition,
|
||||
deletePosition,
|
||||
getTagBindings,
|
||||
listMembers,
|
||||
listPositionUsers,
|
||||
listPositions,
|
||||
removeMembers,
|
||||
resyncPosition,
|
||||
setTagBindings,
|
||||
setUserPosition,
|
||||
updatePosition,
|
||||
} from "./api";
|
||||
import type {
|
||||
PositionCreateInput,
|
||||
PositionUpdateInput,
|
||||
TagBindings,
|
||||
} from "./types";
|
||||
|
||||
const positionKeys = {
|
||||
root: ["positions"] as const,
|
||||
list: ["positions", "list"] as const,
|
||||
bindings: (id: string) => ["positions", "bindings", id] as const,
|
||||
members: (id: string) => ["positions", "members", id] as const,
|
||||
users: ["positions", "users"] as const,
|
||||
};
|
||||
|
||||
export function usePositions() {
|
||||
return useQuery({ queryKey: positionKeys.list, queryFn: listPositions });
|
||||
}
|
||||
|
||||
export function usePositionUsers() {
|
||||
return useQuery({ queryKey: positionKeys.users, queryFn: listPositionUsers });
|
||||
}
|
||||
|
||||
export function usePositionTagBindings(positionId: string | null) {
|
||||
return useQuery({
|
||||
queryKey: positionKeys.bindings(positionId ?? ""),
|
||||
queryFn: () => getTagBindings(positionId as string),
|
||||
enabled: !!positionId,
|
||||
});
|
||||
}
|
||||
|
||||
export function usePositionMembers(positionId: string | null) {
|
||||
return useQuery({
|
||||
queryKey: positionKeys.members(positionId ?? ""),
|
||||
queryFn: () => listMembers(positionId as string),
|
||||
enabled: !!positionId,
|
||||
});
|
||||
}
|
||||
|
||||
function useInvalidate() {
|
||||
const qc = useQueryClient();
|
||||
return () => void qc.invalidateQueries({ queryKey: positionKeys.root });
|
||||
}
|
||||
|
||||
export function useCreatePosition() {
|
||||
const invalidate = useInvalidate();
|
||||
return useMutation({
|
||||
mutationFn: (input: PositionCreateInput) => createPosition(input),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdatePosition() {
|
||||
const invalidate = useInvalidate();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, input }: { id: string; input: PositionUpdateInput }) => updatePosition(id, input),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
}
|
||||
|
||||
export function useDeletePosition() {
|
||||
const invalidate = useInvalidate();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => deletePosition(id),
|
||||
onSuccess: invalidate,
|
||||
});
|
||||
}
|
||||
|
||||
export function useSetTagBindings() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ positionId, bindings }: { positionId: string; bindings: TagBindings }) =>
|
||||
setTagBindings(positionId, bindings),
|
||||
onSuccess: (_data, { positionId }) => {
|
||||
void qc.invalidateQueries({ queryKey: positionKeys.bindings(positionId) });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useAddMembers() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ positionId, userIds }: { positionId: string; userIds: string[] }) =>
|
||||
addMembers(positionId, userIds),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: positionKeys.root }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useRemoveMembers() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ positionId, userIds }: { positionId: string; userIds: string[] }) =>
|
||||
removeMembers(positionId, userIds),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: positionKeys.root }),
|
||||
});
|
||||
}
|
||||
|
||||
export function useResyncPosition() {
|
||||
return useMutation({ mutationFn: (positionId: string) => resyncPosition(positionId) });
|
||||
}
|
||||
|
||||
export function useSetUserPosition() {
|
||||
const qc = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ userId, positionId }: { userId: string; positionId: string | null }) =>
|
||||
setUserPosition(userId, positionId),
|
||||
onSuccess: () => void qc.invalidateQueries({ queryKey: positionKeys.root }),
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./types";
|
||||
export * from "./api";
|
||||
export * from "./hooks";
|
||||
@@ -0,0 +1,86 @@
|
||||
import type { TagType } from "@/core/tags/types";
|
||||
|
||||
/** Resource types a 岗位 (position) can grant, by tag. */
|
||||
export type PositionResourceType =
|
||||
| "agent"
|
||||
| "skill"
|
||||
| "scheduled_task"
|
||||
| "user_prompt"
|
||||
| "recommended_question";
|
||||
|
||||
export const POSITION_RESOURCE_TYPES: PositionResourceType[] = [
|
||||
"agent",
|
||||
"skill",
|
||||
"scheduled_task",
|
||||
"user_prompt",
|
||||
"recommended_question",
|
||||
];
|
||||
|
||||
// A position's resource types are exactly the tag types — reuse the labels.
|
||||
export const POSITION_RESOURCE_LABELS: Record<PositionResourceType, string> = {
|
||||
agent: "智能体",
|
||||
skill: "技能",
|
||||
scheduled_task: "定时任务",
|
||||
user_prompt: "常用提示词",
|
||||
recommended_question: "推荐问题",
|
||||
};
|
||||
|
||||
export const POSITION_RESOURCE_HINT: Record<PositionResourceType, string> = {
|
||||
agent: "不配置标签 = 默认加载全部内置智能体;配置后只加载所选标签下的智能体。",
|
||||
skill: "不配置标签 = 可见全部技能;配置后只可见所选标签下的技能。",
|
||||
scheduled_task: "不配置标签 = 不订阅任何定时任务;配置后订阅所选标签下的定时任务。",
|
||||
user_prompt: "不配置标签 = 加载未打标签的全局模板;配置后加载所选标签下的提示词模板。",
|
||||
recommended_question: "不配置标签 = 展示全部推荐问题;配置后只展示所选标签下的推荐问题。",
|
||||
};
|
||||
|
||||
export interface Position {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
is_default: boolean;
|
||||
member_count: number;
|
||||
created_at: string | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export interface PositionListResponse {
|
||||
positions: Position[];
|
||||
resource_types: PositionResourceType[];
|
||||
}
|
||||
|
||||
export interface PositionCreateInput {
|
||||
name: string;
|
||||
description?: string;
|
||||
is_default?: boolean;
|
||||
}
|
||||
|
||||
export interface PositionUpdateInput {
|
||||
name?: string;
|
||||
description?: string;
|
||||
is_default?: boolean;
|
||||
}
|
||||
|
||||
/** resource_type -> [tag_id] */
|
||||
export type TagBindings = Partial<Record<PositionResourceType, string[]>>;
|
||||
|
||||
export interface TagBindingsResponse {
|
||||
position_id: string;
|
||||
bindings: TagBindings;
|
||||
}
|
||||
|
||||
export interface PositionMember {
|
||||
id: string;
|
||||
email: string;
|
||||
}
|
||||
|
||||
export interface PositionUser {
|
||||
id: string;
|
||||
email: string;
|
||||
system_role: string;
|
||||
position_id: string | null;
|
||||
}
|
||||
|
||||
// The resource types double as tag types for the binding pickers.
|
||||
export function resourceTypeToTagType(rt: PositionResourceType): TagType {
|
||||
return rt as TagType;
|
||||
}
|
||||
Reference in new issue
Block a user