| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /**
- * 通用关系图 —— 独立数据模型
- *
- * 数据来自 `execute_python` 工具结果里的 `relationGraph`:
- * { title?, nodes: [{id,label,type,value,degree}], edges: [{source,target,relation,weight,count,directed}],
- * truncated?, note?, totalNodes?, totalEdges? }
- *
- * 节点/边的业务口径由模型在 Python 里决定(任意表、任意字段、任意关系名),
- * 本文件只做渲染前的归一化,不推断任何社交关系。
- */
- import type { EdgeData, GraphData, NodeData } from '@antv/g6';
- export type RelationNodeType = 'person' | 'phone' | 'card' | 'org' | 'place' | 'vehicle' | 'account' | 'other';
- export interface RelationTypeMeta {
- type: RelationNodeType;
- label: string;
- color: string;
- }
- /** 节点类型配色:与案件图谱的实体色保持一致(节点本身复用 ai-graph-node 的图标样式) */
- export const RELATION_TYPE_META: Record<RelationNodeType, RelationTypeMeta> = {
- person: { type: 'person', label: '人物', color: '#b92a31' },
- phone: { type: 'phone', label: '手机号', color: '#0aa0b4' },
- card: { type: 'card', label: '银行卡', color: '#8441ae' },
- org: { type: 'org', label: '机构', color: '#0f9f68' },
- place: { type: 'place', label: '地点', color: '#0f4c8a' },
- vehicle: { type: 'vehicle', label: '车辆', color: '#f29a1f' },
- account: { type: 'account', label: '账号', color: '#8441ae' },
- other: { type: 'other', label: '其他', color: '#d4ac2b' },
- };
- export const RELATION_TYPE_META_LIST: RelationTypeMeta[] = [
- RELATION_TYPE_META.person,
- RELATION_TYPE_META.phone,
- RELATION_TYPE_META.card,
- RELATION_TYPE_META.org,
- RELATION_TYPE_META.place,
- RELATION_TYPE_META.vehicle,
- RELATION_TYPE_META.account,
- RELATION_TYPE_META.other,
- ];
- export function relationTypeMeta(type?: string | null): RelationTypeMeta {
- const key = (type || 'other') as RelationNodeType;
- return Object.prototype.hasOwnProperty.call(RELATION_TYPE_META, key)
- ? RELATION_TYPE_META[key] || RELATION_TYPE_META.other
- : RELATION_TYPE_META.other;
- }
- export interface RelationModelNode {
- id: string;
- label: string;
- type: RelationNodeType;
- value: string;
- degree: number;
- }
- export interface RelationModelEdge {
- id: string;
- source: string;
- target: string;
- relation: string;
- label: string;
- weight: number;
- count: number;
- directed: boolean;
- }
- export interface ParsedRelationGraph {
- title: string;
- nodes: RelationModelNode[];
- edges: RelationModelEdge[];
- truncated: boolean;
- note: string;
- totalNodes: number;
- totalEdges: number;
- }
- /* ------------------------------------------------------------------ *
- * 归一化
- * ------------------------------------------------------------------ */
- function text(value: unknown): string {
- return String(value ?? '').trim();
- }
- function toNumber(value: unknown, fallback = 0): number {
- if (value === null || value === undefined || value === '') return fallback;
- const numeric = Number(String(value).replace(/,/g, ''));
- return Number.isFinite(numeric) ? numeric : fallback;
- }
- function toBoolean(value: unknown): boolean {
- if (typeof value === 'boolean') return value;
- const raw = text(value).toLowerCase();
- return raw === 'true' || raw === '1' || raw === 'yes';
- }
- function normalizeType(value: unknown): RelationNodeType {
- const raw = text(value).toLowerCase();
- return Object.prototype.hasOwnProperty.call(RELATION_TYPE_META, raw) ? (raw as RelationNodeType) : 'other';
- }
- /**
- * 把后端规范化后的 relationGraph 解析成渲染模型。
- *
- * 后端已经做过结构校验/裁剪;前端仍做一层容错(缺 id、悬空边、重复边),
- * 保证历史消息里的旧载荷或手工改动也不会把画布画崩。
- */
- export function parseRelationGraphPayload(raw: unknown): ParsedRelationGraph | null {
- if (raw === null || raw === undefined) return null;
- let source: any = raw;
- if (typeof raw === 'string') {
- try {
- source = JSON.parse(raw);
- } catch {
- return null;
- }
- }
- if (!source || typeof source !== 'object') return null;
- // 兼容「只给边数组」的写法
- const rawNodes = Array.isArray(source) ? [] : Array.isArray(source.nodes) ? source.nodes : [];
- const rawEdges = Array.isArray(source) ? source : Array.isArray(source.edges) ? source.edges : [];
- const title = Array.isArray(source) ? '' : text(source.title || source.meta?.title);
- const note = Array.isArray(source) ? '' : text(source.note);
- const truncated = Array.isArray(source) ? false : Boolean(source.truncated);
- const nodeMap = new Map<string, RelationModelNode>();
- const ensureNode = (id: string, label?: unknown, type?: unknown, value?: unknown) => {
- if (!id) return;
- if (nodeMap.has(id)) return;
- nodeMap.set(id, {
- id,
- label: text(label) || id,
- type: normalizeType(type),
- value: text(value) || id,
- degree: 0,
- });
- };
- rawNodes.forEach((item: any) => {
- if (!item || typeof item !== 'object' || Array.isArray(item)) return;
- const value = text(item.value);
- const label = text(item.label);
- const id = text(item.id) || value || label;
- if (!id) return;
- ensureNode(id, label || value, item.type, value || id);
- });
- const edgeMap = new Map<string, RelationModelEdge>();
- rawEdges.forEach((item: any) => {
- if (!item || typeof item !== 'object' || Array.isArray(item)) return;
- const sourceId = text(item.source);
- const targetId = text(item.target);
- if (!sourceId || !targetId || sourceId === targetId) return;
- ensureNode(sourceId, item.sourceLabel, item.sourceType, sourceId);
- ensureNode(targetId, item.targetLabel, item.targetType, targetId);
- const relation = text(item.relation) || text(item.label) || '关联';
- const weight = Math.max(0, toNumber(item.weight, toNumber(item.count, 1)));
- const count = Math.max(0, toNumber(item.count, 0));
- const directed = toBoolean(item.directed);
- // 无向边统一端点顺序,A→B 与 B→A 只保留一条
- const [from, to] = directed ? [sourceId, targetId] : [sourceId, targetId].sort();
- const key = `${from}\u0001${to}\u0001${relation}\u0001${directed}`;
- const existing = edgeMap.get(key);
- if (existing) {
- existing.weight += weight;
- existing.count += count;
- return;
- }
- edgeMap.set(key, {
- id: '',
- source: from,
- target: to,
- relation,
- label: relation,
- weight,
- count,
- directed,
- });
- });
- const nodes = Array.from(nodeMap.values());
- const degree = new Map<string, number>();
- const edges = Array.from(edgeMap.values()).map((edge, index) => {
- degree.set(edge.source, (degree.get(edge.source) || 0) + 1);
- degree.set(edge.target, (degree.get(edge.target) || 0) + 1);
- return {
- ...edge,
- id: `relation-edge-${index}`,
- };
- });
- nodes.forEach((node) => {
- node.degree = degree.get(node.id) || 0;
- });
- return {
- title,
- nodes,
- edges,
- truncated,
- note,
- totalNodes: toNumber(Array.isArray(source) ? nodes.length : source.totalNodes, nodes.length),
- totalEdges: toNumber(Array.isArray(source) ? edges.length : source.totalEdges, edges.length),
- };
- }
- /* ------------------------------------------------------------------ *
- * G6 数据
- * ------------------------------------------------------------------ */
- /**
- * 渲染模型 → G6 GraphData。
- *
- * 平行边(同一对端点之间多条关系)改为二次曲线,否则会完全重叠。
- */
- export function buildRelationG6Data(model: ParsedRelationGraph): GraphData {
- const nodes: NodeData[] = model.nodes.map((node) => ({
- id: node.id,
- data: {
- label: node.label,
- type: node.type,
- value: node.value,
- degree: node.degree,
- },
- style: {},
- }));
- const edges: EdgeData[] = model.edges.map((edge) => ({
- id: edge.id,
- source: edge.source,
- target: edge.target,
- type: 'line',
- data: {
- relation: edge.relation,
- label: edge.label,
- weight: edge.weight,
- count: edge.count,
- directed: edge.directed,
- },
- style: {},
- }));
- const groups = new Map<string, EdgeData[]>();
- edges.forEach((edge) => {
- const key = [String(edge.source), String(edge.target)].sort().join('::');
- const group = groups.get(key) || [];
- group.push(edge);
- groups.set(key, group);
- });
- groups.forEach((group) => {
- if (group.length <= 1) return;
- group.forEach((edge, index) => {
- const offset = (index % 2 === 0 ? 1 : -1) * Math.ceil(index / 2) * 26;
- edge.type = 'quadratic';
- edge.style = { ...(edge.style || {}), curveOffset: offset };
- });
- });
- return { nodes, edges, combos: [] };
- }
|