- 新增 structured_text_process 打标客户端(文本构造、标签解析清洗、MCP 调用) - 写库后后台打标(tagUntaggedAccounts),单条新增 / 批量导入 / 每日补全三个入口统一 - 标签只补空,UPDATE 带 tags 为空守卫,不覆盖已有标签 - 新增 AI_TAGGING_TOOL 配置项(留空关闭),更新配置示例与文档 - 新增 16 个打标测试用例,全量 131 个测试通过
71 lines
2.3 KiB
TypeScript
71 lines
2.3 KiB
TypeScript
import { isManagerRequest, managerForbidden } from "../../../lib/user-auth";
|
|
import { runInBackground } from "../../../lib/background";
|
|
import { buildManualRow, type ManualResourceInput } from "../../../lib/resource-import";
|
|
import {
|
|
analyzeRows,
|
|
enrichRows,
|
|
loadAccounts,
|
|
tagUntaggedAccounts,
|
|
writeAnalyzedRows,
|
|
} from "../../../lib/resource-write";
|
|
|
|
type InsertResult = {
|
|
action: "create" | "update";
|
|
message: string;
|
|
};
|
|
|
|
export async function POST(request: Request) {
|
|
if (!(await isManagerRequest(request))) return managerForbidden();
|
|
try {
|
|
const payload = (await request.json()) as Partial<ManualResourceInput> & {
|
|
mode?: string;
|
|
};
|
|
const row = buildManualRow({
|
|
profileUrl: payload.profileUrl ?? "",
|
|
nickname: payload.nickname ?? "",
|
|
publicAccountId: payload.publicAccountId ?? "",
|
|
ipLocation: payload.ipLocation ?? "",
|
|
followers: payload.followers ?? "",
|
|
gender: payload.gender ?? "",
|
|
bio: payload.bio ?? "",
|
|
tags: payload.tags ?? "",
|
|
cooperationSource: payload.cooperationSource ?? "",
|
|
});
|
|
if (row.errors.length > 0) {
|
|
return Response.json(
|
|
{ error: row.errors[0], errors: row.errors },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
const accounts = await loadAccounts();
|
|
const enriched = await enrichRows([row], accounts.results);
|
|
const analyzed = analyzeRows(enriched, accounts.results).filter(
|
|
(item) => item.action !== "error",
|
|
);
|
|
if (analyzed.length === 0) {
|
|
return Response.json(
|
|
{ error: "账号数据校验未通过,无法保存" },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
await writeAnalyzedRows(analyzed);
|
|
runInBackground(
|
|
tagUntaggedAccounts(analyzed),
|
|
"resource account tagging",
|
|
);
|
|
const result = analyzed[0];
|
|
const outcome: InsertResult = {
|
|
action: result.action === "update" ? "update" : "create",
|
|
message:
|
|
result.action === "update"
|
|
? `已更新账号 ${row.nickname || row.publicAccountId || "资料"}`
|
|
: `已新增账号 ${row.nickname || row.publicAccountId || "资料"}`,
|
|
};
|
|
return Response.json(outcome);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "保存失败";
|
|
return Response.json({ error: message }, { status: 400 });
|
|
}
|
|
}
|