From 7b3c926bfb0e5fd8e1c17f512c321eacc62bad6c Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 16 Aug 2026 01:01:37 -0500 Subject: [PATCH] =?UTF-8?q?fix(test1):=20X=20=E6=B4=BB=E5=8A=A8=E6=B5=81?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E8=B4=A6=E5=8F=B7/=E6=96=87=E6=A1=88/?= =?UTF-8?q?=E5=AA=92=E4=BD=93=EF=BC=8C=E5=8E=BB=E6=8E=89=E3=80=8C=E5=81=9A?= =?UTF-8?q?=E5=AE=8C=E4=BA=86=E3=80=8D=E5=BA=9F=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- backend/app/x2_db.py | 2 +- frontend/src/app/x/page.tsx | 102 ++++++++++++++---------- frontend/src/lib/xOps.ts | 152 ++++++++++++++++++++++++++++++++++-- 3 files changed, 207 insertions(+), 49 deletions(-) diff --git a/backend/app/x2_db.py b/backend/app/x2_db.py index 9298745..b902a2f 100644 --- a/backend/app/x2_db.py +++ b/backend/app/x2_db.py @@ -483,7 +483,7 @@ def list_events(*, limit: int = 30) -> list[dict[str, Any]]: def status_snapshot() -> dict[str, Any]: agents = list_agents() online = [a for a in agents if a.get("online")] - jobs = list_jobs(limit=30) + jobs = list_jobs(limit=80) accounts = list_accounts(limit=30) events = list_events(limit=20) running = sum(1 for j in jobs if str(j.get("status") or "") == "running") diff --git a/frontend/src/app/x/page.tsx b/frontend/src/app/x/page.tsx index d3cc782..a6f3b2f 100644 --- a/frontend/src/app/x/page.tsx +++ b/frontend/src/app/x/page.tsx @@ -17,6 +17,9 @@ import { STATUS_ZH, summarizeJob, jobMatchesAccount, + normalizeXJob, + feedPreview, + progressLabel, type XJob, } from "@/lib/xOps"; import { @@ -99,10 +102,9 @@ export default function XPage() { const st = await fetchJson("/api/x2/status"); setAgents(st?.agents || []); setAccounts(st?.accounts || []); - const nextJobs: XJob[] = (st?.jobs || []).map((j: XJob & { result?: unknown }) => ({ - ...j, - result: j.result || undefined, - })); + const nextJobs: XJob[] = (st?.jobs || []).map((j: XJob & Record) => + normalizeXJob(j), + ); setJobs(nextJobs); setStats(st?.stats || {}); setExtOnline(Boolean(st?.extension_online)); @@ -156,7 +158,7 @@ export default function XPage() { body: JSON.stringify({ kind, params: merged }), }); if (r?.job) { - setJobs((prev) => [r.job, ...prev.filter((j) => j.id !== r.job.id)]); + setJobs((prev) => [normalizeXJob(r.job), ...prev.filter((j) => j.id !== r.job.id)]); setSelectedId(r.job.id); toast.push(`🚀 ${kindLabel(kind)} 已下发`, "ok"); } else { @@ -173,7 +175,7 @@ export default function XPage() { try { const r = await fetchJson(`/api/x2/jobs/${encodeURIComponent(id)}/cancel`, { method: "POST" }); if (r?.job) { - setJobs((prev) => [r.job, ...prev.filter((j) => j.id !== r.job.id)]); + setJobs((prev) => [normalizeXJob(r.job), ...prev.filter((j) => j.id !== r.job.id)]); toast.push("已请求取消", "ok"); } } catch (e: unknown) { @@ -331,7 +333,8 @@ export default function XPage() {
{kindLabel(active.kind)}
- {STATUS_ZH[active.status || ""] || active.status} · {active.progress_message || ""} + {progressLabel(active)} + {active.account_username ? ` · @${String(active.account_username).replace(/^@/, "")}` : ""}

活动流

- {filteredJobs.length} - {username ? ` · @${username}` : ""} + {filteredJobs.length}/{accountJobs.length} + {username ? ` · @${username}` : " · 全部账号"}
@@ -457,43 +460,49 @@ export default function XPage() { ))}
-
+
{filteredJobs.length === 0 ? (
还没有操作记录
) : ( - filteredJobs.map((j) => ( -
- {(j.status === "running" || j.status === "queued") && ( - { - e.stopPropagation(); - void cancelJob(j.id); - }} - > - 停 - - )} - - )) + {(j.status === "running" || j.status === "queued") && ( + { + e.stopPropagation(); + void cancelJob(j.id); + }} + > + 停 + + )} + + ); + }) )}
@@ -540,7 +549,14 @@ export default function XPage() { : "text-orange-200") } > - {kindLabel(selected.kind)} · {STATUS_ZH[selected.status || ""] || selected.status} + {feedPreview(selected).headline} +
+
+ {STATUS_ZH[selected.status || ""] || selected.status} + {selected.account_username + ? ` · @${String(selected.account_username).replace(/^@/, "")}` + : ""} + {selected.id ? ` · id ${selected.id.slice(0, 10)}` : ""}
                   {summarizeJob(selected)}
diff --git a/frontend/src/lib/xOps.ts b/frontend/src/lib/xOps.ts
index f95279b..4bfe5e5 100644
--- a/frontend/src/lib/xOps.ts
+++ b/frontend/src/lib/xOps.ts
@@ -9,14 +9,133 @@ export type XJob = {
   progress_current?: number;
   progress_total?: number;
   progress_message?: string;
+  progress?: { current?: number; total?: number; message?: string };
   error?: string;
   received_at?: number;
   job_updated_at?: string;
+  job_created_at?: string;
+  createdAt?: string;
+  updatedAt?: string;
   result?: Record;
   params?: Record;
   result_json?: string;
+  params_json?: string;
 };
 
+/** Extension often sends useless "做完了" — hide from humans. */
+const NOISE_PROGRESS = /^(做完了|失败|排队中|启动中…?|启动中|Web 任务领取)$/i;
+
+export function normalizeXJob(raw: XJob & Record): XJob {
+  const progress =
+    raw.progress && typeof raw.progress === "object"
+      ? (raw.progress as { current?: number; total?: number; message?: string })
+      : undefined;
+  let params = raw.params && typeof raw.params === "object" ? raw.params : undefined;
+  if (!params && raw.params_json) {
+    try {
+      params = JSON.parse(String(raw.params_json)) as Record;
+    } catch {
+      params = {};
+    }
+  }
+  let result = raw.result && typeof raw.result === "object" ? raw.result : undefined;
+  if (!result && raw.result_json) {
+    try {
+      result = JSON.parse(String(raw.result_json)) as Record;
+    } catch {
+      result = {};
+    }
+  }
+  return {
+    ...raw,
+    params: params || {},
+    result: result || {},
+    progress_current: Number(raw.progress_current ?? progress?.current ?? 0),
+    progress_total: Number(raw.progress_total ?? progress?.total ?? 0),
+    progress_message: String(raw.progress_message || progress?.message || ""),
+  };
+}
+
+export function progressLabel(job: XJob): string {
+  const msg = String(job.progress_message || "").trim();
+  if (msg && !NOISE_PROGRESS.test(msg)) return msg;
+  return STATUS_ZH[job.status || ""] || job.status || "";
+}
+
+function mediaCountOf(job: XJob): number {
+  const r = jobResult(job);
+  const params = job.params && typeof job.params === "object" ? job.params : {};
+  const fromResult = Number(r.mediaCount ?? 0);
+  if (fromResult > 0) return fromResult;
+  const urls = Array.isArray(params.mediaUrls) ? params.mediaUrls.length : 0;
+  const media = Array.isArray(params.media) ? params.media.length : 0;
+  const keys = Array.isArray(r.mediaKeys) ? (r.mediaKeys as unknown[]).length : 0;
+  return urls || media || keys || 0;
+}
+
+function postTextOf(job: XJob): string {
+  const r = jobResult(job);
+  const params = job.params && typeof job.params === "object" ? job.params : {};
+  return String(r.text || r.composerSnapshot || params.text || "").trim();
+}
+
+/** One-line / two-line preview for activity feed (human readable). */
+export function feedPreview(job: XJob): { headline: string; detail: string } {
+  const kind = job.kind || "";
+  const name = kindLabel(kind);
+  const acct = jobAccountHandle(job);
+  const acctBit = acct ? `@${acct}` : "";
+  const st = progressLabel(job);
+  const r = jobResult(job);
+  const params = job.params && typeof job.params === "object" ? job.params : {};
+
+  if (job.status === "failed" || job.status === "cancelled") {
+    const err = String(job.error || "未知原因").replace(/\s+/g, " ").slice(0, 80);
+    return {
+      headline: `${name}${acctBit ? ` · ${acctBit}` : ""}`,
+      detail: `${STATUS_ZH[job.status] || job.status} · ${err}`,
+    };
+  }
+
+  if (kind === "engage.post" || kind === "engage.reply" || kind === "engage.quote") {
+    const text = postTextOf(job).replace(/\s+/g, " ");
+    const nMedia = mediaCountOf(job);
+    const mediaBit = nMedia > 0 ? ` · 媒体×${nMedia}` : "";
+    const snippet = text ? `「${text.slice(0, 56)}${text.length > 56 ? "…" : ""}」` : "(无正文记录)";
+    return {
+      headline: `${name}${acctBit ? ` · ${acctBit}` : ""}${mediaBit}`,
+      detail: `${st} · ${snippet}`,
+    };
+  }
+
+  if (kind === "engage.like" || kind === "engage.unlike" || kind === "engage.follow") {
+    const n =
+      kind === "engage.like"
+        ? r.liked
+        : kind === "engage.unlike"
+          ? r.unliked
+          : r.followed;
+    return {
+      headline: `${name}${acctBit ? ` · ${acctBit}` : ""}`,
+      detail: job.status === "completed" ? `${st} · 数量 ${n ?? 0}` : st,
+    };
+  }
+
+  if (kind.startsWith("scrape.") || kind.startsWith("cleanup.") || kind.startsWith("monitor.")) {
+    const tip = summarizeJob(job).split("\n")[0] || st;
+    return {
+      headline: `${name}${acctBit ? ` · ${acctBit}` : ""}`,
+      detail: tip.slice(0, 90),
+    };
+  }
+
+  const q = String(params.query || params.username || params.statusUrl || "").trim();
+  return {
+    headline: `${name}${acctBit ? ` · ${acctBit}` : ""}`,
+    detail: q ? `${st} · ${q.slice(0, 60)}` : st,
+  };
+}
+
 export const STATUS_ZH: Record = {
   queued: "排队中",
   running: "进行中",
@@ -133,7 +252,7 @@ export function summarizeJob(job: XJob): string {
   if (job.status === "failed") return `${name}没做成\n原因:${job.error || "未知错误"}`;
   if (job.status === "cancelled") return `${name}已取消`;
   if (job.status !== "completed") {
-    const msg = job.progress_message || STATUS_ZH[job.status || ""] || "进行中";
+    const msg = progressLabel(job);
     if ((job.progress_total || 0) > 1) {
       return `${msg}(${job.progress_current || 0}/${job.progress_total})`;
     }
@@ -141,6 +260,7 @@ export function summarizeJob(job: XJob): string {
   }
   const r = jobResult(job);
   const kind = job.kind || "";
+  const params = job.params && typeof job.params === "object" ? job.params : {};
 
   if (kind === "cleanup.non_followers") {
     const non = (r.nonFollowers as Array<{ username: string }>) || [];
@@ -176,11 +296,33 @@ export function summarizeJob(job: XJob): string {
     const u = (r.unfollowed as string[]) || [];
     return `取关完成:成功 ${u.length}\n` + u.map((x) => `@${x}`).join("\n");
   }
-  if (kind === "engage.like") return `点赞完成:${r.liked ?? 0}`;
-  if (kind === "engage.unlike") return `取消点赞:${r.unliked ?? 0}`;
-  if (kind === "engage.follow") return `关注完成:${r.followed ?? 0}`;
+  if (kind === "engage.like") return `点赞完成:${r.liked ?? 0}${r.requested != null ? `(目标 ${r.requested})` : ""}`;
+  if (kind === "engage.unlike") return `取消点赞:${r.unliked ?? 0}${r.requested != null ? `(目标 ${r.requested})` : ""}`;
+  if (kind === "engage.follow") {
+    const users = (r.users as string[]) || [];
+    return (
+      `关注完成:${r.followed ?? users.length ?? 0}${r.requested != null ? `(目标 ${r.requested})` : ""}` +
+      (users.length ? `\n` + users.slice(0, 40).map((u) => `@${u}`).join("\n") : "")
+    );
+  }
   if (kind === "engage.post" || kind === "engage.reply" || kind === "engage.quote") {
-    return `${name}成功\n\n${r.text || job.params?.text || ""}`;
+    const text = postTextOf(job);
+    const acct = jobAccountHandle(job);
+    const nMedia = mediaCountOf(job);
+    const tip =
+      kind === "engage.post" ? "发帖成功" : kind === "engage.reply" ? "回复成功" : "引用成功";
+    const lines = [tip];
+    if (acct) lines.push(`账号:@${acct}`);
+    if (nMedia > 0) lines.push(`媒体:${nMedia} 个附件`);
+    const clipId = String(params.xClipJobId || "").trim();
+    if (clipId) lines.push(`切片任务:${clipId.slice(0, 8)}…`);
+    const url = String(r.url || r.statusUrl || params.statusUrl || "").trim();
+    if (url) lines.push(`链接:${url}`);
+    const when = String(r.postedAt || r.finishedAt || "").trim();
+    if (when) lines.push(`时间:${when}`);
+    lines.push("");
+    lines.push(text || "(没有正文记录 — 请检查发帖参数)");
+    return lines.join("\n");
   }
   if (kind === "monitor.snapshot_followers" || kind === "monitor.snapshot_following") {
     const n = ((r.usernames as string[]) || []).length;