Files
monkey-ai-web/src/components/smart-router/api.js
T
zhanjunhao 8a48765a46 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

135 lines
4.6 KiB
JavaScript

import { API, showError } from '../../helpers';
import i18n from '../../i18n/i18n';
// 智能模型路由接口(后端 router/smart-router.go)。
// mode: personal(个人)/ admin(平台 system 集合)/ org(企业,X-Org-Id 由 API 拦截器注入)
// 所有请求都跳过全局错误提示(skipErrorHandler),由调用方只弹一次业务文案,避免英文报错与重复提示。
export const routerBase = (mode) => {
if (mode === 'admin') return '/api/router/admin';
if (mode === 'org') return '/api/org/router';
return '/api/router';
};
const opts = (orgId, extra = {}) => ({
skipErrorHandler: true,
...(orgId ? { headers: { 'X-Org-Id': String(orgId) } } : {}),
...extra,
});
const friendly = (e) => {
const msg = e?.response?.data?.message;
if (msg) return msg;
const status = e?.response?.status;
if (status === 429) return i18n.t('操作过于频繁,请稍后再试');
if (status === 401 || status === 403) return i18n.t('没有权限执行该操作');
return i18n.t('请求失败,请稍后重试');
};
const call = async (fn) => {
let res;
try {
res = await fn();
} catch (e) {
// 401 交给全局处理:清理登录态并跳转登录页(skipErrorHandler 会跳过它)。
// 之后不再抛出,调用方也就不会再弹一次"没有权限"——页面马上会跳走
if (e?.response?.status === 401) {
showError(e);
return new Promise(() => {});
}
throw new Error(friendly(e));
}
const { success, message, data } = res?.data || {};
if (!success) throw new Error(message || i18n.t('请求失败,请稍后重试'));
return data;
};
export const listProfiles = (mode, orgId) =>
call(() => API.get(`${routerBase(mode)}/profiles`, opts(orgId)));
export const saveProfile = (mode, orgId, profile) => {
const base = routerBase(mode);
return call(() =>
profile.id
? API.put(`${base}/profiles/${profile.id}`, profile, opts(orgId))
: API.post(`${base}/profiles`, profile, opts(orgId)),
);
};
export const deleteProfile = (mode, orgId, id) =>
call(() => API.delete(`${routerBase(mode)}/profiles/${id}`, opts(orgId)));
export const simulateProfile = (mode, orgId, id, payload) =>
call(() =>
API.post(
`${routerBase(mode)}/profiles/${id}/simulate`,
payload,
opts(orgId),
),
);
export const listDecisions = (mode, orgId, params) =>
call(() => API.get(`${routerBase(mode)}/decisions`, opts(orgId, { params })));
export const getConsents = (mode, orgId) =>
call(() => API.get(`${routerBase(mode)}/consents`, opts(orgId)));
// 个人是否仍有生效中的出境同意:平台关闭智能路由后,侧边栏据此保留入口供撤回(会话内缓存,避免每次进页面都请求)
const CONSENT_CACHE_KEY = 'smart_router_personal_consent_granted';
export const readPersonalConsentCache = () => {
try {
return sessionStorage.getItem(CONSENT_CACHE_KEY);
} catch {
return null;
}
};
const writePersonalConsentCache = (rows) => {
try {
sessionStorage.setItem(
CONSENT_CACHE_KEY,
(rows || []).some((x) => x && x.granted === true) ? '1' : '0',
);
} catch {
// 无痕模式等场景下 sessionStorage 不可用:不缓存即可
}
};
export const loadPersonalConsentGranted = async () => {
const rows = await getConsents('personal');
writePersonalConsentCache(rows);
return (rows || []).some((x) => x && x.granted === true);
};
export const updateConsent = async (mode, orgId, kind, granted) => {
const rows = await call(() =>
API.put(`${routerBase(mode)}/consents`, { kind, granted }, opts(orgId)),
);
if (mode !== 'org' && mode !== 'admin') writePersonalConsentCache(rows);
return rows;
};
// 候选模型下拉的数据来源:个人=自己分组可用模型;平台=全部启用模型;企业=企业可用模型
export const listCandidateModels = async (mode, orgId) => {
let url = '/api/user/models';
if (mode === 'admin') url = '/api/channel/models_enabled';
if (mode === 'org') url = '/api/org/models';
const data = await call(() => API.get(url, opts(orgId)));
return (data || []).filter(
(m) => typeof m === 'string' && !m.startsWith('router/'),
);
};
export const getAdminStatus = () =>
call(() => API.get('/api/router/admin/status', opts()));
export const setJevKey = (provider, key, clear = false) =>
call(() =>
API.put('/api/router/admin/jev-key', { provider, key, clear }, opts()),
);
export const loadOptions = () => call(() => API.get('/api/option/', opts()));
export const saveOption = (key, value) =>
call(() => API.put('/api/option/', { key, value }, opts()));