feat: complete chat routing and openai onboarding

This commit is contained in:
kris
2026-03-31 03:31:22 +08:00
parent 5b590f7cc1
commit 9c02ebb574
25 changed files with 2241 additions and 133 deletions

View File

@@ -0,0 +1,75 @@
import { NextRequest, NextResponse } from "next/server";
import { requireRequestSession } from "@/lib/boss-auth";
import { readState, saveAiAccount } from "@/lib/boss-data";
import { validateAiAccountConnection } from "@/lib/boss-master-agent";
function chooseMasterPrimaryAccountId(state: Awaited<ReturnType<typeof readState>>) {
return (
state.aiAccounts.find((item) => item.provider === "master_codex_node" && item.role === "primary")
?.accountId || "master-codex-primary"
);
}
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;
nodeId?: string;
nodeLabel?: string;
model?: string;
setActive?: boolean;
};
if (!body.displayName?.trim()) {
return NextResponse.json({ ok: false, message: "显示名称不能为空。" }, { status: 400 });
}
if (!body.nodeId?.trim()) {
return NextResponse.json({ ok: false, message: "请先填写节点 ID。" }, { status: 400 });
}
try {
const state = await readState();
const accountId = chooseMasterPrimaryAccountId(state);
const account = await saveAiAccount({
accountId,
label: body.label?.trim() || "主 GPT",
role: "primary",
provider: "master_codex_node",
displayName: body.displayName.trim(),
accountIdentifier: body.accountIdentifier?.trim() || undefined,
nodeId: body.nodeId.trim(),
nodeLabel: body.nodeLabel?.trim() || undefined,
model: body.model?.trim() || "gpt-5.4",
enabled: true,
setActive: body.setActive !== false,
loginStatusNote: "节点绑定信息已保存,请在绑定设备上的 Codex / ChatGPT Plus 会话里完成登录。",
});
const validation = await validateAiAccountConnection(account.accountId);
return NextResponse.json({
ok: true,
accountId: account.accountId,
account,
validation,
message:
body.setActive === false
? "Master Codex Node 绑定信息已保存。"
: "Master Codex Node 已绑定,并设为当前主控。",
});
} catch (error) {
return NextResponse.json(
{ ok: false, message: error instanceof Error ? error.message : "MASTER_NODE_ONBOARD_FAILED" },
{ status: 400 },
);
}
}

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 { probeOpenAiApiAccount } from "@/lib/boss-master-agent";
function chooseOpenAiPrimaryAccountId(state: Awaited<ReturnType<typeof readState>>) {
return (
state.aiAccounts.find((item) => item.provider === "openai_api" && item.role === "primary")?.accountId ||
"openai-api-primary"
);
}
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: "请先填写 OpenAI API Key。" }, { status: 400 });
}
try {
const probe = await probeOpenAiApiAccount({
apiKey: body.apiKey,
model: body.model,
});
const state = await readState();
const accountId = chooseOpenAiPrimaryAccountId(state);
const account = await saveAiAccount({
accountId,
label: body.label?.trim() || "主 GPT",
role: "primary",
provider: "openai_api",
displayName: body.displayName.trim(),
accountIdentifier: body.accountIdentifier?.trim() || undefined,
model: probe.model,
apiKey: body.apiKey.trim(),
enabled: true,
setActive: true,
loginStatusNote: "已在手机端登录 OpenAI 平台账号,可直接作为当前主控。",
});
await updateAiAccountHealth({
accountId: account.accountId,
status: "ready",
lastError: undefined,
lastValidatedAt: new Date().toISOString(),
lastUsedAt: new Date().toISOString(),
});
const nextState = await readState();
return NextResponse.json({
ok: true,
accountId: account.accountId,
account,
activeIdentity: getMasterIdentitySummaryFromState(nextState),
requestId: probe.requestId,
message: "OpenAI 平台账号已登录,并设为当前主控。",
});
} catch (error) {
return NextResponse.json(
{ ok: false, message: error instanceof Error ? error.message : "OPENAI_ONBOARD_FAILED" },
{ status: 400 },
);
}
}

View File

@@ -3,9 +3,21 @@ import { requireRequestSession } from "@/lib/boss-auth";
import { appendProjectMessage, readState } from "@/lib/boss-data";
import {
queueGroupDispatchPlan,
queueThreadConversationReplyTask,
replyToMasterAgentUserMessage,
} from "@/lib/boss-master-agent";
function dispatchFailureNotice(error?: string) {
switch (error) {
case "GROUP_DISPATCH_TARGETS_REQUIRED":
return "当前群聊里还没有可下发的真实线程,请先在群资料里重新添加线程后再试。";
case "DISPATCH_TARGET_PROJECT_NOT_FOUND":
return "当前群聊里有失效的线程引用,请重新整理群成员后再试。";
default:
return error ? `主 Agent 暂时无法生成推荐线程:${error}` : "主 Agent 暂时无法生成推荐线程,请稍后重试。";
}
}
export async function POST(
request: NextRequest,
context: { params: Promise<{ projectId: string }> },
@@ -37,8 +49,22 @@ export async function POST(
}
| null = null;
let masterReply:
| { ok: boolean; reason?: string; message?: string; accountId?: string; requestId?: string }
| {
ok: boolean;
reason?: string;
message?: string;
accountId?: string;
requestId?: string;
taskId?: string;
}
| undefined;
let task:
| {
taskId: string;
taskType: "conversation_reply";
status: "queued" | "completed";
}
| null = null;
const state = await readState();
const project = state.projects.find((item) => item.id === projectId);
@@ -58,13 +84,42 @@ export async function POST(
});
dispatchRecommendation = recommendation;
dispatchPlan = recommendation.dispatchPlan;
if (!recommendation.ok) {
await appendProjectMessage({
projectId,
sender: "master",
senderLabel: "主 Agent",
body: dispatchFailureNotice(recommendation.error),
kind: "system_notice",
});
}
} catch (error) {
dispatchRecommendation = {
ok: false,
status: "failed",
error: error instanceof Error ? error.message : "GROUP_DISPATCH_PLAN_FAILED",
};
await appendProjectMessage({
projectId,
sender: "master",
senderLabel: "主 Agent",
body: dispatchFailureNotice(dispatchRecommendation.error),
kind: "system_notice",
});
}
} else if (project && projectId !== "master-agent" && !project.isGroup && message.body.trim().length > 0) {
const queuedTask = await queueThreadConversationReplyTask({
projectId,
requestMessageId: message.id,
requestText: message.body,
requestedBy: session.displayName || session.account,
requestedByAccount: session.account,
});
task = {
taskId: queuedTask.taskId,
taskType: "conversation_reply",
status: "queued",
};
} else {
dispatchRecommendation = {
ok: false,
@@ -80,6 +135,13 @@ export async function POST(
requestedByAccount: session.account,
currentSessionExpiresAt: session.expiresAt,
});
if (masterReply?.ok && masterReply.taskId) {
task = {
taskId: masterReply.taskId,
taskType: "conversation_reply",
status: masterReply.requestId ? "completed" : "queued",
};
}
}
const nextState = shouldCreateDispatchPlan ? await readState() : state;
@@ -103,6 +165,7 @@ export async function POST(
ok: true,
message,
masterReply,
task,
dispatchPlan,
dispatchRecommendation,
collaborationGate,