util.ts 6.11 KB
/**
 * AI 建模页的小工具:状态文字与配色、耗时格式、草案条目的名称 / 类型 / 体检结果 / 状态。
 */
import type { BadgeVariant } from "@/components/ui";
import type { ModelDraftItem, ModelStage, Readiness } from "@/lib/onto";

export const RUN_STATUS: Record<string, { label: string; variant: BadgeVariant; dot: string }> = {
  draft: { label: "未开始", variant: "neutral", dot: "bg-muted/50" },
  running: { label: "运行中", variant: "info", dot: "bg-brand animate-pulse" },
  stopped: { label: "已暂停", variant: "warn", dot: "bg-warn" },
  cancelled: { label: "已终止", variant: "neutral", dot: "bg-muted" },
  done: { label: "已完成", variant: "ok", dot: "bg-ok" },
  failed: { label: "失败", variant: "danger", dot: "bg-bad" },
};

export const STAGE_STATUS: Record<string, string> = {
  pending: "等待", running: "进行中", done: "完成", skipped: "跳过", failed: "出错", stopped: "已暂停", cancelled: "已终止",
};

/** 毫秒 → 「3 分 12 秒」 */
export function fmtDur(ms?: number | null): string {
  if (ms === null || ms === undefined || ms < 0) return "-";
  const s = Math.round(ms / 1000);
  if (s < 60) return `${s} 秒`;
  const m = Math.floor(s / 60);
  if (m < 60) return `${m} 分 ${s % 60} 秒`;
  return `${Math.floor(m / 60)} 小时 ${m % 60} 分`;
}

/** 环节耗时:跑完的取起止,正在跑的算到现在 */
export function stageMs(s: ModelStage, now = Date.now()): number | null {
  if (!s.started_at) return null;
  const end = s.ended_at ? new Date(s.ended_at).getTime() : now;
  return Math.max(0, end - new Date(s.started_at).getTime());
}

export function clock(t?: string | null): string {
  return t ? t.replace("T", " ").slice(11, 19) : "";
}

export type DraftKind = "object_type" | "link_type" | "function" | "action";

export const itemName = (it: ModelDraftItem): string => it.payload?.name || it.title;
export const itemKind = (it: ModelDraftItem): string => it.payload?.kind || "other";
/** 类型徽标用:函数细分到类别 */
export function itemTypeKey(it: ModelDraftItem): string {
  const p = it.payload || {};
  return p.kind === "function" ? (p.spec?.category || "rule") : p.kind || "object_type";
}

/** 体检结果:后端在「汇总草案」这一步、以及每次编辑后算好;老记录没有就是 null */
export function readinessOf(it: ModelDraftItem): Readiness | null {
  return it.readiness || it.payload?.readiness || null;
}

/** 这条草案还差什么才能直接用;空串 = 能直接用 */
export function weakReason(it: ModelDraftItem): string {
  const rd = readinessOf(it);
  if (rd) return rd.ready ? "" : (rd.checks.find((c) => c.must && !c.ok)?.text || "还差一些");
  const p = it.payload || {};
  const spec = p.spec || {};
  const binds: any[] = p.bindings || [];
  if ((p.test_failures || []).length) return `测试没过:${p.test_failures[0]}`;
  if (p.kind === "action" && spec._stub) return "动作只有说明,参数和效果还没补全";
  if (p.kind === "function" && spec.category === "aggregate" && !binds.some((b) => b.target_kind === "metric_expr"))
    return "指标还没对上数据(没有取数方式)";
  if (p.kind === "function" && spec.category === "rule" && !spec.expression) return "规则还没写成公式";
  if (p.kind === "function" && spec.category === "decision" && !(spec.rows || []).length) return "决策表还是空的";
  return "";
}

/** 草案的落地状态:待定 / 已上线 / 已采纳没上线 / 不采纳 */
export type DraftState = "pending" | "live" | "held" | "rejected";
export function draftState(it: ModelDraftItem): DraftState {
  if (it.status === "pending") return "pending";
  if (it.status === "rejected") return "rejected";
  const c = it.concept;
  if (c && c.status === "published" && !c.dirty) return "live";
  return "held";
}
export const DRAFT_STATE: Record<DraftState, { label: string; variant: BadgeVariant }> = {
  pending: { label: "待定", variant: "warn" },
  live: { label: "已上线", variant: "ok" },
  held: { label: "没上线", variant: "danger" },
  rejected: { label: "不采纳", variant: "neutral" },
};

/** 绑定情况 */
export function bindLabel(it: ModelDraftItem): string {
  const kinds = (it.payload?.bindings || []).map((b: any) => b.target_kind);
  if (kinds.includes("metric_expr")) return "取数已校验";
  if (kinds.includes("table")) return "已绑表";
  if (kinds.includes("join")) return "已对上关联列";
  return "";
}

export const CARD_TEXT: Record<string, string> = {
  "1:1": "一个对应一个", "1:n": "一个对应多个", "n:1": "多个对应一个", "n:n": "多个对应多个",
};

/** 动作效果翻成一句业务话 */
export function effectText(e: any): string {
  const a = e?.args || {};
  const v = (x: any) => (typeof x === "string" ? x.replace(/^\$/, "参数 ").replace(/^=/, "按公式 ") : JSON.stringify(x));
  switch (e?.tool) {
    case "object.write": return `把「${a.object_type || "对象"}」的「${a.property}」改成 ${v(a.value)}`;
    case "object.create": return `新建一条「${a.object_type}」:${Object.entries(a.values || {}).map(([k, x]) => `${k} = ${v(x)}`).join(",")}`;
    case "notify.send": return `发通知给 ${a.to || a.role || "相关人"}:${a.title || a.text || a.body || ""}`;
    case "task.create": return `派一项待办给 ${a.role || a.assignee || "相关人"}:${a.title || ""}${a.description ? `(${v(a.description)})` : ""}`;
    default: return `${e?.tool || "?"} ${JSON.stringify(a)}`;
  }
}

/** 「k=v, k2=v2」↔ 对象:测试用例的输入、决策表的条件这类小字典,表单里用一行文字改 */
export function kvText(o: Record<string, any> | null | undefined): string {
  return Object.entries(o || {}).map(([k, v]) => `${k}=${typeof v === "string" ? v : JSON.stringify(v)}`).join(", ");
}
export function parseKv(t: string): Record<string, any> {
  const out: Record<string, any> = {};
  for (const part of t.split(/[,,]\s*(?=[^=,,]+=)/)) {
    const i = part.indexOf("=");
    if (i <= 0) continue;
    const k = part.slice(0, i).trim();
    const raw = part.slice(i + 1).trim();
    try { out[k] = JSON.parse(raw); } catch { out[k] = raw; }
  }
  return out;
}