Compare commits
2 Commits
feat/accou
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab3a544eec | ||
| 8f7ea0558d |
@@ -494,6 +494,9 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
||||
const [activeNav, setActiveNav] = useState<NavKey>("overview");
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [working, setWorking] = useState(false);
|
||||
const [collectingDistributionIds, setCollectingDistributionIds] = useState<Set<string>>(
|
||||
new Set(),
|
||||
);
|
||||
const [error, setError] = useState("");
|
||||
const [toast, setToast] = useState("");
|
||||
const [menuOpen, setMenuOpen] = useState(false);
|
||||
@@ -762,10 +765,23 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
||||
};
|
||||
|
||||
const collectMetrics = async (distribution: Distribution) => {
|
||||
await runAction(
|
||||
{ action: "collect_now", distributionId: distribution.id },
|
||||
"公开数据已更新",
|
||||
);
|
||||
setCollectingDistributionIds((current) => {
|
||||
const next = new Set(current);
|
||||
next.add(distribution.id);
|
||||
return next;
|
||||
});
|
||||
try {
|
||||
await runAction(
|
||||
{ action: "collect_now", distributionId: distribution.id },
|
||||
"公开数据已更新",
|
||||
);
|
||||
} finally {
|
||||
setCollectingDistributionIds((current) => {
|
||||
const next = new Set(current);
|
||||
next.delete(distribution.id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const updateDistributionPublishUrl = async (
|
||||
@@ -1123,6 +1139,7 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
||||
tasks={data.tasks}
|
||||
distributions={data.distributions}
|
||||
working={working}
|
||||
collectingDistributionIds={collectingDistributionIds}
|
||||
canUpdatePublishUrl={isManager}
|
||||
onCollect={collectMetrics}
|
||||
onUpdatePublishUrl={updateDistributionPublishUrl}
|
||||
@@ -2815,6 +2832,7 @@ function RecoveryPage({
|
||||
tasks,
|
||||
distributions,
|
||||
working,
|
||||
collectingDistributionIds,
|
||||
canUpdatePublishUrl,
|
||||
onCollect,
|
||||
onUpdatePublishUrl,
|
||||
@@ -2829,6 +2847,7 @@ function RecoveryPage({
|
||||
tasks: Task[];
|
||||
distributions: Distribution[];
|
||||
working: boolean;
|
||||
collectingDistributionIds: ReadonlySet<string>;
|
||||
canUpdatePublishUrl: boolean;
|
||||
onCollect: (distribution: Distribution) => void;
|
||||
onUpdatePublishUrl: (distribution: Distribution) => void;
|
||||
@@ -3116,7 +3135,9 @@ function RecoveryPage({
|
||||
</span>
|
||||
) : item.screenshot_key ? (
|
||||
<span className="creator-ocr-failed">
|
||||
待KOC填写数据
|
||||
{item.ocr_status === "processing"
|
||||
? "正在识别数据"
|
||||
: "识别失败,请手动填写"}
|
||||
</span>
|
||||
) : null}
|
||||
{item.screenshot_key &&
|
||||
@@ -3139,7 +3160,12 @@ function RecoveryPage({
|
||||
) : (
|
||||
<button
|
||||
className="collect-button"
|
||||
disabled={working || !noteUrl}
|
||||
disabled={
|
||||
!noteUrl ||
|
||||
(working &&
|
||||
(collectingDistributionIds.size === 0 ||
|
||||
collectingDistributionIds.has(item.id)))
|
||||
}
|
||||
onClick={() => onCollect(item)}
|
||||
>
|
||||
{noteUrl ? "立即采集" : "待填链接"}
|
||||
|
||||
@@ -4,9 +4,10 @@ import {
|
||||
getUploadBucket,
|
||||
} from "../../../lib/mvp-db";
|
||||
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
||||
import { verifyCreatorScreenshotAccessToken } from "../../../lib/creator-screenshot-access";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!(await isAdminRequest(request))) return adminForbidden();
|
||||
const isAdmin = await isAdminRequest(request);
|
||||
try {
|
||||
await ensureSchema();
|
||||
const distributionId = new URL(request.url).searchParams
|
||||
@@ -22,6 +23,18 @@ export async function GET(request: Request) {
|
||||
)
|
||||
.bind(distributionId)
|
||||
.first<{ screenshot_key: string | null }>();
|
||||
const mcpToken = new URL(request.url).searchParams.get("mcp_token") || "";
|
||||
if (
|
||||
!isAdmin &&
|
||||
(!row?.screenshot_key ||
|
||||
!verifyCreatorScreenshotAccessToken(
|
||||
distributionId,
|
||||
row.screenshot_key,
|
||||
mcpToken,
|
||||
))
|
||||
) {
|
||||
return adminForbidden();
|
||||
}
|
||||
if (
|
||||
!row?.screenshot_key ||
|
||||
!row.screenshot_key.startsWith("creator-center/")
|
||||
|
||||
@@ -13,6 +13,13 @@ import {
|
||||
parseResultScreenshotKeys,
|
||||
serializeResultScreenshotKeys,
|
||||
} from "../../../lib/result-screenshots";
|
||||
import { creatorScreenshotMcpUrl } from "../../../lib/creator-screenshot-access";
|
||||
import {
|
||||
extractCreatorMetricsFromMcp,
|
||||
resolveCollectionMcpConfig,
|
||||
type CollectionMcpBindings,
|
||||
} from "../../../lib/mcp-collection-client";
|
||||
import { getRuntimeEnv } from "../../../lib/runtime-env";
|
||||
|
||||
async function readUpload(request: Request) {
|
||||
const contentType = request.headers.get("content-type") ?? "";
|
||||
@@ -163,13 +170,47 @@ async function handlePost(request: Request) {
|
||||
screenshot_key = ?,
|
||||
ocr_status = CASE
|
||||
WHEN exposure IS NOT NULL AND views IS NOT NULL THEN ocr_status
|
||||
ELSE 'uploaded'
|
||||
ELSE 'processing'
|
||||
END,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
)
|
||||
.bind(key, upload.distributionId)
|
||||
.run();
|
||||
try {
|
||||
const metrics = await extractCreatorMetricsFromMcp(
|
||||
creatorScreenshotMcpUrl(request, upload.distributionId, key),
|
||||
resolveCollectionMcpConfig(
|
||||
getRuntimeEnv() as unknown as CollectionMcpBindings,
|
||||
),
|
||||
);
|
||||
await getRawDb()
|
||||
.prepare(
|
||||
`UPDATE distributions SET exposure = ?, views = ?,
|
||||
ocr_status = 'success', updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
)
|
||||
.bind(metrics.exposure, metrics.views, upload.distributionId)
|
||||
.run();
|
||||
return Response.json({
|
||||
uploaded: true,
|
||||
exposure: metrics.exposure,
|
||||
views: metrics.views,
|
||||
ocrStatus: "success",
|
||||
kind: "creator-center",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("[KOC LOOP] creator screenshot OCR failed", {
|
||||
distributionId: upload.distributionId,
|
||||
detail: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
await getRawDb()
|
||||
.prepare(
|
||||
`UPDATE distributions SET ocr_status = 'failed', updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
)
|
||||
.bind(upload.distributionId)
|
||||
.run();
|
||||
}
|
||||
} else {
|
||||
await getRawDb()
|
||||
.prepare(
|
||||
@@ -189,6 +230,7 @@ async function handlePost(request: Request) {
|
||||
: isCreatorCenter
|
||||
? "creator-center"
|
||||
: "publish",
|
||||
ocrStatus: isCreatorCenter ? "failed" : undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
return Response.json(
|
||||
|
||||
@@ -6,6 +6,13 @@ import {
|
||||
uid,
|
||||
} from "../../../lib/mvp-db";
|
||||
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
||||
import { creatorScreenshotMcpUrl } from "../../../lib/creator-screenshot-access";
|
||||
import {
|
||||
extractCreatorMetricsFromMcp,
|
||||
resolveCollectionMcpConfig,
|
||||
type CollectionMcpBindings,
|
||||
} from "../../../lib/mcp-collection-client";
|
||||
import { getRuntimeEnv } from "../../../lib/runtime-env";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
if (!(await isAdminRequest(request))) return adminForbidden();
|
||||
@@ -33,12 +40,37 @@ export async function POST(request: Request) {
|
||||
screenshot_key = ?,
|
||||
exposure = NULL,
|
||||
views = NULL,
|
||||
ocr_status = 'failed',
|
||||
ocr_status = 'processing',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
)
|
||||
.bind(key, distributionId)
|
||||
.run();
|
||||
try {
|
||||
const metrics = await extractCreatorMetricsFromMcp(
|
||||
creatorScreenshotMcpUrl(request, distributionId, key),
|
||||
resolveCollectionMcpConfig(getRuntimeEnv() as unknown as CollectionMcpBindings),
|
||||
);
|
||||
await db
|
||||
.prepare(
|
||||
`UPDATE distributions SET exposure = ?, views = ?,
|
||||
ocr_status = 'success', updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`,
|
||||
)
|
||||
.bind(metrics.exposure, metrics.views, distributionId)
|
||||
.run();
|
||||
} catch (error) {
|
||||
console.error("[KOC LOOP] creator screenshot OCR failed", {
|
||||
distributionId,
|
||||
detail: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
await db
|
||||
.prepare(
|
||||
`UPDATE distributions SET ocr_status = 'failed', updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
)
|
||||
.bind(distributionId)
|
||||
.run();
|
||||
}
|
||||
return Response.json(await getDashboardData());
|
||||
} catch (error) {
|
||||
return Response.json(
|
||||
|
||||
@@ -954,7 +954,7 @@ export default function Home() {
|
||||
selectedItem: Assignment,
|
||||
file: File,
|
||||
kind: "publish" | "creator-center" | "task-result",
|
||||
) => {
|
||||
): Promise<{ exposure?: number | null; views?: number | null; ocrStatus?: string }> => {
|
||||
const compressed = await compressScreenshot(file);
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": compressed.type || "image/jpeg",
|
||||
@@ -973,7 +973,12 @@ export default function Home() {
|
||||
headers,
|
||||
body: compressed,
|
||||
});
|
||||
const uploadResult = (await uploadResponse.json()) as { error?: string };
|
||||
const uploadResult = (await uploadResponse.json()) as {
|
||||
error?: string;
|
||||
exposure?: number | null;
|
||||
views?: number | null;
|
||||
ocrStatus?: string;
|
||||
};
|
||||
if (!uploadResponse.ok) {
|
||||
throw new Error(
|
||||
uploadResult.error ||
|
||||
@@ -984,6 +989,7 @@ export default function Home() {
|
||||
: "发布截图上传失败"),
|
||||
);
|
||||
}
|
||||
return uploadResult;
|
||||
};
|
||||
|
||||
const submitNote = async (event: FormEvent) => {
|
||||
@@ -1082,22 +1088,31 @@ export default function Home() {
|
||||
setToast("请选择创作者中心截图");
|
||||
return;
|
||||
}
|
||||
if (
|
||||
!/^\d{1,12}$/.test(creatorExposure) ||
|
||||
!/^\d{1,12}$/.test(creatorViews)
|
||||
) {
|
||||
setToast("请填写正确的曝光量和阅读量");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
setCreatorWorking(true);
|
||||
let exposureValue = creatorExposure;
|
||||
let viewsValue = creatorViews;
|
||||
if (creatorScreenshot) {
|
||||
setCreatorStage("正在上传截图…");
|
||||
await uploadEvidence(
|
||||
const ocrResult = await uploadEvidence(
|
||||
selected,
|
||||
creatorScreenshot,
|
||||
"creator-center",
|
||||
);
|
||||
if (typeof ocrResult.exposure === "number") {
|
||||
exposureValue = String(ocrResult.exposure);
|
||||
setCreatorExposure(exposureValue);
|
||||
}
|
||||
if (typeof ocrResult.views === "number") {
|
||||
viewsValue = String(ocrResult.views);
|
||||
setCreatorViews(viewsValue);
|
||||
}
|
||||
if (ocrResult.ocrStatus === "success") {
|
||||
setCreatorStage("已识别截图数据,正在保存…");
|
||||
}
|
||||
}
|
||||
if (!/^\d{1,12}$/.test(exposureValue) || !/^\d{1,12}$/.test(viewsValue)) {
|
||||
throw new Error("未识别到完整数据,请填写曝光量和阅读量");
|
||||
}
|
||||
setCreatorStage("正在保存数据…");
|
||||
const response = await fetch(partnerApi("/api/partner"), {
|
||||
@@ -1109,8 +1124,8 @@ export default function Home() {
|
||||
claimToken,
|
||||
delegationToken,
|
||||
distributionId: selected.id,
|
||||
exposure: creatorExposure,
|
||||
views: creatorViews,
|
||||
exposure: exposureValue,
|
||||
views: viewsValue,
|
||||
}),
|
||||
});
|
||||
const result = (await response.json()) as { error?: string };
|
||||
@@ -1593,7 +1608,7 @@ export default function Home() {
|
||||
<div className="evidence-empty-state">
|
||||
<b>+</b>
|
||||
<strong>点击选择第7天创作者中心截图</strong>
|
||||
<small>截图仅用于运营核对,不再自动OCR</small>
|
||||
<small>上传后自动识别曝光量和阅读量,识别失败可手动填写</small>
|
||||
</div>
|
||||
)}
|
||||
<label className="evidence-upload-action">
|
||||
|
||||
@@ -92,7 +92,7 @@ test("keeps claiming minimal and backfill one-to-one", async () => {
|
||||
assert.match(page, /submit_creator_metrics/);
|
||||
assert.match(page, /creatorExposure/);
|
||||
assert.match(page, /creatorViews/);
|
||||
assert.match(page, /截图仅用于运营核对,不再自动OCR/);
|
||||
assert.match(page, /上传后自动识别曝光量和阅读量,识别失败可手动填写/);
|
||||
assert.match(page, /evidenceImageUrl/);
|
||||
assert.match(page, /params\.set\("v", evidenceKey\)/);
|
||||
assert.match(page, /evidenceImageUrl\(selected, "publish"\)/);
|
||||
@@ -140,7 +140,7 @@ test("renders screenshot-only tasks with anonymous delegation and multi-image up
|
||||
assert.match(styles, /\.evidence-preview-button/);
|
||||
});
|
||||
|
||||
test("shows D1 timestamps in Beijing time", () => {
|
||||
test("shows D1 and MySQL UTC timestamps in Beijing time", () => {
|
||||
const stored = "2026-07-29 05:36:00";
|
||||
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
||||
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
||||
|
||||
@@ -21,6 +21,33 @@ type ScheduledTask = {
|
||||
|
||||
type CollectionSource = "automatic" | "catchup" | "manual";
|
||||
|
||||
function userFacingCollectionError(error: unknown) {
|
||||
const message = error instanceof Error ? error.message : String(error ?? "");
|
||||
const normalized = message.toLowerCase();
|
||||
if (
|
||||
/cookie|登录|登陆|授权|access.?token|未登录|未授权|401|403/.test(
|
||||
normalized,
|
||||
)
|
||||
) {
|
||||
return "采集 Cookie 已过期或无权限";
|
||||
}
|
||||
if (
|
||||
/链接|link|url|404|not found|不存在|删除|失效|无法识别.*笔记/.test(
|
||||
normalized,
|
||||
)
|
||||
) {
|
||||
return "笔记链接失效或不可访问";
|
||||
}
|
||||
if (
|
||||
/超时|timeout|fetch failed|network|502|503|暂时不可用|响应空/.test(
|
||||
normalized,
|
||||
)
|
||||
) {
|
||||
return "采集服务暂时不可用";
|
||||
}
|
||||
return "笔记链接失效或采集 Cookie 过期";
|
||||
}
|
||||
|
||||
function utcDay(value: string) {
|
||||
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||
if (!match) return null;
|
||||
@@ -311,8 +338,9 @@ export async function collectDistributionMetrics(
|
||||
]);
|
||||
return { skipped: false, likes, comments, collects, shares };
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error ? error.message : "公开数据采集失败";
|
||||
const detail = error instanceof Error ? error.message : String(error);
|
||||
const message = userFacingCollectionError(error);
|
||||
console.error("[KOC LOOP] collection failed", { detail, distributionId });
|
||||
await db.batch([
|
||||
db
|
||||
.prepare(
|
||||
|
||||
60
lib/creator-screenshot-access.ts
Normal file
60
lib/creator-screenshot-access.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { getRuntimeEnv } from "./runtime-env";
|
||||
|
||||
function accessSecret() {
|
||||
const env = getRuntimeEnv();
|
||||
return (
|
||||
env.KOC_MCP_API_KEY ||
|
||||
env.KOC_LOOP_MCP_API_KEY ||
|
||||
env.ADMIN_INTERNAL_TOKEN ||
|
||||
env.AI_TOOL_CENTER_MCP_KEY ||
|
||||
""
|
||||
).trim();
|
||||
}
|
||||
|
||||
function signature(distributionId: string, screenshotKey: string, expiresAt: number) {
|
||||
return createHmac("sha256", accessSecret())
|
||||
.update(`${distributionId}:${screenshotKey}:${expiresAt}`)
|
||||
.digest("hex");
|
||||
}
|
||||
|
||||
export function creatorScreenshotAccessToken(
|
||||
distributionId: string,
|
||||
screenshotKey: string,
|
||||
expiresAt = Math.floor(Date.now() / 1000) + 300,
|
||||
) {
|
||||
const secret = accessSecret();
|
||||
if (!secret) throw new Error("图片识别访问密钥未配置");
|
||||
return `${expiresAt}.${signature(distributionId, screenshotKey, expiresAt)}`;
|
||||
}
|
||||
|
||||
export function verifyCreatorScreenshotAccessToken(
|
||||
distributionId: string,
|
||||
screenshotKey: string,
|
||||
token: string,
|
||||
) {
|
||||
const [expiresText, received] = token.split(".");
|
||||
const expiresAt = Number(expiresText);
|
||||
if (!Number.isInteger(expiresAt) || expiresAt < Math.floor(Date.now() / 1000)) {
|
||||
return false;
|
||||
}
|
||||
const expected = signature(distributionId, screenshotKey, expiresAt);
|
||||
if (!received || received.length !== expected.length) return false;
|
||||
return timingSafeEqual(Buffer.from(received), Buffer.from(expected));
|
||||
}
|
||||
|
||||
export function creatorScreenshotMcpUrl(
|
||||
request: Request,
|
||||
distributionId: string,
|
||||
screenshotKey: string,
|
||||
) {
|
||||
const env = getRuntimeEnv();
|
||||
const origin = (env.APP_ORIGIN || new URL(request.url).origin).replace(/\/$/, "");
|
||||
const url = new URL(`${origin}/api/creator-screenshot`);
|
||||
url.searchParams.set("distribution", distributionId);
|
||||
url.searchParams.set(
|
||||
"mcp_token",
|
||||
creatorScreenshotAccessToken(distributionId, screenshotKey),
|
||||
);
|
||||
return url.toString();
|
||||
}
|
||||
@@ -254,6 +254,7 @@ async function invokeMcpTool(
|
||||
timeoutMs: number,
|
||||
name: string,
|
||||
args: Record<string, unknown>,
|
||||
allowText = false,
|
||||
): Promise<ToolResult> {
|
||||
const result = await postMcp(
|
||||
fetchImpl,
|
||||
@@ -277,6 +278,12 @@ async function invokeMcpTool(
|
||||
try {
|
||||
payload = JSON.parse(text);
|
||||
} catch {
|
||||
if (allowText) {
|
||||
return {
|
||||
isError: result.envelope?.result?.isError === true,
|
||||
payload: { text },
|
||||
};
|
||||
}
|
||||
if (result.envelope?.result?.isError === true) {
|
||||
return {
|
||||
isError: true,
|
||||
@@ -305,6 +312,7 @@ async function callMcpTool(
|
||||
timeoutMs: number,
|
||||
name: string,
|
||||
args: Record<string, unknown>,
|
||||
allowText = false,
|
||||
): Promise<ToolResult> {
|
||||
const nested = await invokeMcpTool(
|
||||
fetchImpl,
|
||||
@@ -313,6 +321,7 @@ async function callMcpTool(
|
||||
timeoutMs,
|
||||
name,
|
||||
{ request: args },
|
||||
allowText,
|
||||
);
|
||||
if (!isToolArgumentShapeError(nested)) return nested;
|
||||
return invokeMcpTool(
|
||||
@@ -322,10 +331,86 @@ async function callMcpTool(
|
||||
timeoutMs,
|
||||
name,
|
||||
args,
|
||||
allowText,
|
||||
);
|
||||
}
|
||||
|
||||
function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
||||
function imageToolText(value: unknown) {
|
||||
if (typeof value === "string") return value;
|
||||
const root = asRecord(value);
|
||||
if (!root) return "";
|
||||
return [root.text, root.description, root.content, root.message, root.error]
|
||||
.flatMap((item) => (Array.isArray(item) ? item : [item]))
|
||||
.map((item) => {
|
||||
if (typeof item === "string") return item;
|
||||
const record = asRecord(item);
|
||||
return String(record?.text ?? record?.description ?? "");
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function metricFromImageText(text: string, labels: string[], label: string) {
|
||||
const pattern = labels.join("|");
|
||||
const match = text.match(
|
||||
new RegExp(`(?:${pattern})\\s*[::]?\\s*([\\d,.]+(?:万|w|千|k)?)`, "i"),
|
||||
);
|
||||
if (!match) return null;
|
||||
try {
|
||||
return metricValue(match[1], label);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function creatorMetricsFromImageResult(result: ToolResult) {
|
||||
if (result.isError) {
|
||||
const detail = imageToolText(result.payload);
|
||||
throw new Error(
|
||||
`图片识别 MCP 调用失败${detail ? `:${safeMessage(detail, "")}` : ""}`,
|
||||
);
|
||||
}
|
||||
const text = imageToolText(result.payload);
|
||||
const exposure = metricFromImageText(
|
||||
text,
|
||||
["曝光量", "曝光", "impressions", "exposure"],
|
||||
"曝光量",
|
||||
);
|
||||
const views = metricFromImageText(
|
||||
text,
|
||||
["阅读量", "阅读", "views", "view_count", "view count"],
|
||||
"阅读量",
|
||||
);
|
||||
if (exposure === null || views === null) {
|
||||
throw new Error("图片识别未找到曝光量和阅读量");
|
||||
}
|
||||
return { exposure, views };
|
||||
}
|
||||
|
||||
export async function extractCreatorMetricsFromMcp(
|
||||
imageUrl: string,
|
||||
config: CollectionMcpConfig,
|
||||
fetchImpl: typeof fetch = fetch,
|
||||
) {
|
||||
const endpoint = buildMcpUrl(config);
|
||||
const timeoutMs = Math.max(5_000, config.timeoutMs ?? 30_000);
|
||||
const sessionId = await createMcpSession(fetchImpl, endpoint, timeoutMs);
|
||||
const result = await callMcpTool(
|
||||
fetchImpl,
|
||||
endpoint,
|
||||
sessionId,
|
||||
timeoutMs,
|
||||
"analyze_image",
|
||||
{ image_url: imageUrl },
|
||||
true,
|
||||
);
|
||||
return creatorMetricsFromImageResult(result);
|
||||
}
|
||||
|
||||
function metricsFromToolResult(
|
||||
result: ToolResult,
|
||||
toolName = "fetch_content_detail",
|
||||
): XhsPublicMetrics {
|
||||
const root = asRecord(result.payload);
|
||||
const response = asRecord(root?.response) ?? root;
|
||||
const data = asRecord(response?.data) ?? asRecord(root?.data);
|
||||
@@ -337,14 +422,18 @@ function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
||||
success === false ||
|
||||
(Number.isFinite(code) && code >= 400)
|
||||
) {
|
||||
const providerCode = Number(response?.code);
|
||||
const codeLabel = Number.isFinite(providerCode)
|
||||
? `(code ${providerCode})`
|
||||
: "";
|
||||
throw new Error(
|
||||
safeMessage(
|
||||
`MCP工具 ${toolName} 返回失败${codeLabel}:${safeMessage(
|
||||
response?.msg ?? response?.message ?? root?.message,
|
||||
"公开数据采集失败",
|
||||
),
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
if (!data) throw new Error("采集结果缺少互动数据");
|
||||
if (!data) throw new Error(`MCP工具 ${toolName} 未返回互动数据`);
|
||||
|
||||
const count = (value: unknown, label: string) =>
|
||||
value === null || value === undefined || value === ""
|
||||
@@ -1156,7 +1245,7 @@ async function collectInSession(
|
||||
auto_cookie: true,
|
||||
},
|
||||
);
|
||||
return metricsFromToolResult(primary);
|
||||
return metricsFromToolResult(primary, "fetch_content_detail");
|
||||
}
|
||||
|
||||
export function resolveCollectionMcpConfig(
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
parseStoredDate,
|
||||
} from "../lib/date-utils.ts";
|
||||
|
||||
test("treats D1 CURRENT_TIMESTAMP values as UTC and displays Beijing time", () => {
|
||||
test("treats D1 and MySQL UTC timestamps as UTC and displays Beijing time", () => {
|
||||
const stored = "2026-07-29 05:36:00";
|
||||
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
||||
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
||||
|
||||
@@ -520,7 +520,7 @@ test("surfaces current detail tool failures without calling removed tools", asyn
|
||||
},
|
||||
fetchImpl,
|
||||
),
|
||||
/获取内容详情失败/,
|
||||
/MCP工具 fetch_content_detail 返回失败(code 400):获取内容详情失败/,
|
||||
);
|
||||
assert.equal(calls.length, 3);
|
||||
assert.equal(calls[2].body.params.name, "fetch_content_detail");
|
||||
|
||||
@@ -190,8 +190,9 @@ test("issues external task links and supports one-to-one note submissions", asyn
|
||||
assert.doesNotMatch(partnerUtils, /user\/profile\/\$\{platformUid\}/);
|
||||
assert.match(uploadRoute, /publish-evidence/);
|
||||
assert.match(uploadRoute, /creator-center/);
|
||||
assert.match(uploadRoute, /ELSE 'uploaded'/);
|
||||
assert.doesNotMatch(uploadRoute, /ocrMetric/);
|
||||
assert.match(uploadRoute, /ELSE 'processing'/);
|
||||
assert.match(uploadRoute, /extractCreatorMetricsFromMcp/);
|
||||
assert.match(uploadRoute, /creatorScreenshotMcpUrl/);
|
||||
assert.match(uploadRoute, /x-koc-upload-kind/);
|
||||
assert.match(uploadRoute, /x-koc-distribution/);
|
||||
assert.match(uploadRoute, /request\.arrayBuffer/);
|
||||
@@ -223,7 +224,7 @@ test("issues external task links and supports one-to-one note submissions", asyn
|
||||
assert.match(adminApp, /全部内容类型/);
|
||||
assert.match(adminApp, /全部平台/);
|
||||
assert.match(adminApp, /task-scope-subline/);
|
||||
assert.match(adminApp, /待KOC填写数据/);
|
||||
assert.match(adminApp, /识别失败,请手动填写/);
|
||||
assert.match(adminApp, /AdminImageLightbox/);
|
||||
assert.match(adminApp, /CreatorScreenshotPreview/);
|
||||
assert.match(adminApp, /admin-image-lightbox/);
|
||||
|
||||
Reference in New Issue
Block a user