feat: add aliyun qwen backup provider

This commit is contained in:
kris
2026-04-01 05:33:35 +08:00
parent ba01ae5393
commit 60d69eb222
15 changed files with 708 additions and 87 deletions

View File

@@ -6,8 +6,8 @@ function isValidRole(value: string): value is "primary" | "backup" | "api_fallba
return value === "primary" || value === "backup" || value === "api_fallback";
}
function isValidProvider(value: string): value is "master_codex_node" | "openai_api" {
return value === "master_codex_node" || value === "openai_api";
function isValidProvider(value: string): value is "master_codex_node" | "openai_api" | "aliyun_qwen_api" {
return value === "master_codex_node" || value === "openai_api" || value === "aliyun_qwen_api";
}
export async function GET(

View File

@@ -0,0 +1,82 @@
import { NextRequest, NextResponse } from "next/server";
import { requireRequestSession } from "@/lib/boss-auth";
import { getMasterIdentitySummaryFromState, readState, saveAiAccount, updateAiAccountHealth } from "@/lib/boss-data";
import { probeApiCompatibleAccount } from "@/lib/boss-master-agent";
function chooseAliyunBackupAccountId(state: Awaited<ReturnType<typeof readState>>) {
return (
state.aiAccounts.find((item) => item.provider === "aliyun_qwen_api" && item.role === "backup")?.accountId ||
"aliyun-qwen-backup"
);
}
export async function POST(request: NextRequest) {
const session = await requireRequestSession(request);
if (!session) {
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
}
if (session.role !== "highest_admin") {
return NextResponse.json({ ok: false, message: "FORBIDDEN" }, { status: 403 });
}
const body = (await request.json().catch(() => ({}))) as {
label?: string;
displayName?: string;
accountIdentifier?: string;
model?: string;
apiKey?: string;
};
if (!body.displayName?.trim()) {
return NextResponse.json({ ok: false, message: "显示名称不能为空。" }, { status: 400 });
}
if (!body.apiKey?.trim()) {
return NextResponse.json({ ok: false, message: "请先填写阿里百炼 API Key。" }, { status: 400 });
}
try {
const probe = await probeApiCompatibleAccount({
provider: "aliyun_qwen_api",
apiKey: body.apiKey,
model: body.model,
});
const state = await readState();
const accountId = chooseAliyunBackupAccountId(state);
const account = await saveAiAccount({
accountId,
label: body.label?.trim() || "备用 GPT",
role: "backup",
provider: "aliyun_qwen_api",
displayName: body.displayName.trim(),
accountIdentifier: body.accountIdentifier?.trim() || undefined,
model: probe.model,
apiKey: body.apiKey.trim(),
enabled: true,
setActive: false,
loginStatusNote: "已接入阿里百炼 Qwen 兼容接口,可作为主 Agent 的备用模型链路。",
});
await updateAiAccountHealth({
accountId: account.accountId,
status: "ready",
lastError: undefined,
lastValidatedAt: new Date().toISOString(),
});
const nextState = await readState();
return NextResponse.json({
ok: true,
accountId: account.accountId,
account,
activeIdentity: getMasterIdentitySummaryFromState(nextState),
requestId: probe.requestId,
message: "阿里百炼备用账号已接入,可作为主 Agent 的备用链路。",
});
} catch (error) {
return NextResponse.json(
{ ok: false, message: error instanceof Error ? error.message : "ALIYUN_QWEN_ONBOARD_FAILED" },
{ status: 400 },
);
}
}

View File

@@ -6,8 +6,8 @@ function isValidRole(value: string): value is "primary" | "backup" | "api_fallba
return value === "primary" || value === "backup" || value === "api_fallback";
}
function isValidProvider(value: string): value is "master_codex_node" | "openai_api" {
return value === "master_codex_node" || value === "openai_api";
function isValidProvider(value: string): value is "master_codex_node" | "openai_api" | "aliyun_qwen_api" {
return value === "master_codex_node" || value === "openai_api" || value === "aliyun_qwen_api";
}
export async function GET(request: NextRequest) {