- 知识库新增文件夹与模板支持(模型/仓储/服务/迁移),前端知识管理组件 - 岗位管理模型与接口完善 - 技能新增压缩设置页与相关存储/工具能力 - 系统设置扩展配置项 - 摘要中间件 keep 窗口改用 token 预算(16000),减少重复压缩 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
131 lines
5.2 KiB
TypeScript
131 lines
5.2 KiB
TypeScript
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 getMyPosition(): Promise<Position | null> {
|
|
const res = await apiFetch(`${base()}/me`);
|
|
if (!res.ok) throw await parseDetail(res, "Failed to load my position");
|
|
const data = (await res.json()) as { position: Position | null };
|
|
return data.position ?? null;
|
|
}
|
|
|
|
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");
|
|
}
|