feat(auth): 完善多账号与企业分层管理模型,全面支持 F12/cURL 临时凭证导入与一键保活
This commit is contained in:
1 parent
255c7aee8d
commit
3d549e2e2f
8 files changed
+749
-155
No files matched your search
+40
-57
@@ -10,26 +10,23 @@ import { CorpContext } from "./core/config/manager.js";
|
||||
import { createEkbClient } from "./core/auth/ekb.js";
|
||||
import { getEasoClient } from "./core/auth/easo.js";
|
||||
import { AuthenticationError, EkbError, NotLoggedInError } from "./core/errors.js";
|
||||
import { authTools } from "./core/auth/tools.js";
|
||||
import { platformTools } from "./domains/platform/tools.js";
|
||||
|
||||
const SwitchCorpSchema = z.object({
|
||||
corpId: z.string().describe("企业 ID"),
|
||||
});
|
||||
|
||||
function formatError(error: unknown): {
|
||||
content: Array<{ type: "text"; text: string }>;
|
||||
isError: true;
|
||||
} {
|
||||
if (error instanceof NotLoggedInError) {
|
||||
return {
|
||||
content: [{ type: "text", text: "未配置企业,请先添加企业配置" }],
|
||||
content: [{ type: "text", text: "未配置企业,请先添加企业配置或登录" }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
if (error instanceof AuthenticationError) {
|
||||
return {
|
||||
content: [{ type: "text", text: "认证失败,请检查企业凭证或 token 是否过期" }],
|
||||
content: [{ type: "text", text: `认证失败: ${error.message}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
@@ -50,7 +47,7 @@ function formatError(error: unknown): {
|
||||
|
||||
console.error(error);
|
||||
return {
|
||||
content: [{ type: "text", text: "发生内部错误,请稍后重试" }],
|
||||
content: [{ type: "text", text: error instanceof Error ? error.message : "发生内部错误,请稍后重试" }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
@@ -72,23 +69,17 @@ async function main() {
|
||||
}
|
||||
);
|
||||
|
||||
// 聚合所有工具
|
||||
const allTools = [...authTools, ...platformTools];
|
||||
|
||||
// 注册工具列表
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
return {
|
||||
tools: [
|
||||
// 企业切换工具
|
||||
{
|
||||
name: "switch_corp",
|
||||
description: "切换到指定企业",
|
||||
inputSchema: (SwitchCorpSchema as any).toJSONSchema(),
|
||||
},
|
||||
// Platform 工具
|
||||
...platformTools.map((t) => ({
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
inputSchema: (t.parameters as any).toJSONSchema(),
|
||||
})),
|
||||
],
|
||||
tools: allTools.map((t) => ({
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
inputSchema: (t.parameters as any).toJSONSchema(),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -96,54 +87,46 @@ async function main() {
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
const { name, arguments: args } = request.params;
|
||||
|
||||
// 企业切换
|
||||
if (name === "switch_corp") {
|
||||
const parsed = SwitchCorpSchema.safeParse(args);
|
||||
if (!parsed.success) {
|
||||
try {
|
||||
// 1. 检查 Auth 工具
|
||||
const authTool = authTools.find((t) => t.name === name);
|
||||
if (authTool) {
|
||||
const parsed = authTool.parameters.safeParse(args || {});
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
content: [{ type: "text", text: `参数错误: ${parsed.error.message}` }],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
const result = await authTool.handler({ corpContext }, parsed.data as never);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `参数错误: ${parsed.error.message}`,
|
||||
text: typeof result === "string" ? result : JSON.stringify(result, null, 2),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 检查 Domain 业务工具(需要当前企业上下文)
|
||||
const currentCorp = corpContext.getCurrent();
|
||||
if (!currentCorp) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "未配置企业,请先调用 import_f12_corp 导入凭证,或执行 bun run login 登录",
|
||||
},
|
||||
],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
const { corpId } = parsed.data;
|
||||
const success = corpContext.switchCorp(corpId);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: success
|
||||
? `已切换到企业: ${corpId}`
|
||||
: `企业 ${corpId} 不存在`,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// 获取当前企业
|
||||
const currentCorp = corpContext.getCurrent();
|
||||
if (!currentCorp) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "未配置企业,请先添加企业配置",
|
||||
},
|
||||
],
|
||||
isError: true,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
// Platform 工具
|
||||
const platformTool = platformTools.find((t) => t.name === name);
|
||||
if (platformTool) {
|
||||
const parsed = platformTool.parameters.safeParse(args);
|
||||
const parsed = platformTool.parameters.safeParse(args || {});
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
content: [{ type: "text", text: `参数错误: ${parsed.error.message}` }],
|
||||
@@ -160,7 +143,7 @@ async function main() {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: JSON.stringify(result, null, 2),
|
||||
text: typeof result === "string" ? result : JSON.stringify(result, null, 2),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user