提交代码
This commit is contained in:
1 parent
dddd3c1670
commit
cba165933c
16 files changed
+1045
-61
No files matched your search
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 全局业务映射(业务码 → 智能体 / 业务链)API 客户端。
|
||||
*
|
||||
* 后端:`/api/business-mapping`(见 app/gateway/routers/business_mapping.py)。
|
||||
* 全局共享、无用户隔离;只读列表 + 更新 agent / chain 两列(业务行由后端 seed 固定)。
|
||||
* snake_case ⇄ camelCase 在此边界收敛,调用方保持干净。
|
||||
*/
|
||||
|
||||
import { apiFetch } from "@/core/api/fetch-client";
|
||||
import { getBackendBaseURL } from "@/core/config";
|
||||
|
||||
export interface BusinessMapping {
|
||||
businessCode: string;
|
||||
label: string;
|
||||
agentId: string | null;
|
||||
agentName: string | null;
|
||||
chainId: string | null;
|
||||
chainTitle: string | null;
|
||||
sortOrder: number;
|
||||
}
|
||||
|
||||
/** 部分更新 —— 只发改动的字段;显式传 null 清空该字段。 */
|
||||
export interface BusinessMappingPatch {
|
||||
agentId?: string | null;
|
||||
agentName?: string | null;
|
||||
chainId?: string | null;
|
||||
chainTitle?: string | null;
|
||||
}
|
||||
|
||||
const BASE = () => `${getBackendBaseURL()}/api/business-mapping`;
|
||||
|
||||
interface ServerMapping {
|
||||
business_code: string;
|
||||
label: string;
|
||||
agent_id: string | null;
|
||||
agent_name: string | null;
|
||||
chain_id: string | null;
|
||||
chain_title: string | null;
|
||||
sort_order: number;
|
||||
}
|
||||
|
||||
function toMapping(s: ServerMapping): BusinessMapping {
|
||||
return {
|
||||
businessCode: s.business_code,
|
||||
label: s.label ?? "",
|
||||
agentId: s.agent_id ?? null,
|
||||
agentName: s.agent_name ?? null,
|
||||
chainId: s.chain_id ?? null,
|
||||
chainTitle: s.chain_title ?? null,
|
||||
sortOrder: s.sort_order ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
async function readJson<T>(res: Response, ctx: string): Promise<T> {
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`${ctx} failed (${res.status}): ${text}`);
|
||||
}
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
|
||||
/** 列出全部业务映射(按展示顺序)。 */
|
||||
export async function listBusinessMappings(): Promise<BusinessMapping[]> {
|
||||
const res = await apiFetch(BASE(), { method: "GET" });
|
||||
const data = await readJson<{ mappings: ServerMapping[] }>(res, "list business mappings");
|
||||
return (data.mappings ?? []).map(toMapping);
|
||||
}
|
||||
|
||||
/** 更新某业务行的智能体 / 业务链选择。404 返回 null。 */
|
||||
export async function updateBusinessMapping(
|
||||
businessCode: string,
|
||||
patch: BusinessMappingPatch,
|
||||
): Promise<BusinessMapping | null> {
|
||||
const body: Record<string, unknown> = {};
|
||||
if (patch.agentId !== undefined) body.agent_id = patch.agentId;
|
||||
if (patch.agentName !== undefined) body.agent_name = patch.agentName;
|
||||
if (patch.chainId !== undefined) body.chain_id = patch.chainId;
|
||||
if (patch.chainTitle !== undefined) body.chain_title = patch.chainTitle;
|
||||
const res = await apiFetch(`${BASE()}/${encodeURIComponent(businessCode)}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (res.status === 404) return null;
|
||||
const data = await readJson<ServerMapping>(res, "update business mapping");
|
||||
return toMapping(data);
|
||||
}
|
||||
Reference in new issue
Block a user