RelationGraphBlock.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <section ref="rootRef" class="ai-relation" :class="{ 'ai-block-fullscreen': isFullscreen }">
  3. <header class="ai-relation__head">
  4. <span class="ai-relation__label">通用关系图</span>
  5. <span v-if="total > 1" class="ai-relation__index">{{ index + 1 }} / {{ total }}</span>
  6. <span v-if="titleText" class="ai-relation__title">{{ titleText }}</span>
  7. <span v-if="metaText" class="ai-relation__meta">{{ metaText }}</span>
  8. <span class="ai-relation__spacer"></span>
  9. <span v-if="model" class="ai-relation__count">
  10. 节点 {{ model.nodes.length }} · 关系 {{ model.edges.length }}
  11. </span>
  12. </header>
  13. <div v-if="model?.truncated" class="ai-relation__notice">
  14. {{ model.note || `关系过多,仅展示部分数据(共 ${model.totalEdges} 条)` }}
  15. </div>
  16. <div ref="bodyRef" class="ai-relation__body">
  17. <template v-if="model && renderable">
  18. <AsyncRelationViewer v-if="visible" ref="viewerRef" :model="model" @toggle-fullscreen="toggleFullscreen" />
  19. <div v-else class="ai-relation__placeholder">
  20. <div class="ai-skeleton" :style="{ height: '18px', width: '38%' }"></div>
  21. <div class="ai-skeleton" :style="{ height: '14px', width: '62%' }"></div>
  22. <div class="ai-skeleton" :style="{ height: '14px', width: '48%' }"></div>
  23. </div>
  24. </template>
  25. <div v-else class="ai-relation__empty">
  26. <Icon :icon="model ? 'mdi:account-network-outline' : 'mdi:alert-circle-outline'" :size="18" />
  27. <span>{{ emptyText }}</span>
  28. </div>
  29. </div>
  30. <div v-if="isFullscreen" class="ai-relation__hint">
  31. 滚轮缩放 · 拖动画布平移 · 点击节点高亮相邻节点 · 点击关系线高亮两端 · 按 Esc 退出全屏
  32. </div>
  33. </section>
  34. </template>
  35. <script lang="ts" setup>
  36. /**
  37. * AI 聊天里的通用关系图块
  38. *
  39. * 数据来自 `execute_python` 工具结果里的 `relationGraph`(模型在 Python 里调用
  40. * emit_relation_graph 产出)。本组件只负责容器/懒加载/全屏,画布在独立的
  41. * RelationGraphViewer 中实现,不复用案件图谱组件。
  42. */
  43. import { computed, defineAsyncComponent, nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
  44. import Icon from '@/core/components/Icon/src/Icon.vue';
  45. import { useBlockFullscreen } from '../../utils/useBlockFullscreen';
  46. import { parseRelationGraphPayload } from './relationData';
  47. const AsyncRelationViewer = defineAsyncComponent(() => import('./RelationGraphViewer.vue'));
  48. const props = withDefaults(
  49. defineProps<{
  50. /** 工具结果里的 relationGraph 原始载荷 */
  51. payload: unknown;
  52. /** 关系名/表名摘要(header 用) */
  53. meta?: string;
  54. /** 多张图时的序号(从 0 开始) */
  55. index?: number;
  56. /** 多张图时的总数 */
  57. total?: number;
  58. }>(),
  59. { meta: '', index: 0, total: 1 },
  60. );
  61. const rootRef = ref<HTMLElement>();
  62. const bodyRef = ref<HTMLElement>();
  63. const viewerRef = ref<any>(null);
  64. const model = computed(() => parseRelationGraphPayload(props.payload));
  65. const renderable = computed(() => Boolean(model.value && model.value.nodes.length));
  66. const titleText = computed(() => model.value?.title || '');
  67. const metaText = computed(() => props.meta || '');
  68. const emptyText = computed(() => {
  69. if (!model.value) return '关系图数据无法解析,暂不能展示';
  70. if (!model.value.nodes.length && !model.value.edges.length) {
  71. return model.value.note || '模型没有生成可展示的关系';
  72. }
  73. if (!model.value.nodes.length) return '未查询到符合条件的节点';
  74. return '暂无可展示的关系图数据';
  75. });
  76. /* ---------------- 懒加载:首次进入视口才挂载 G6 ---------------- */
  77. const visible = ref(false);
  78. let observer: IntersectionObserver | null = null;
  79. onMounted(() => {
  80. if (!rootRef.value || typeof IntersectionObserver === 'undefined') {
  81. visible.value = true;
  82. return;
  83. }
  84. observer = new IntersectionObserver(
  85. (entries) => {
  86. if (!entries.some((entry) => entry.isIntersecting)) return;
  87. visible.value = true;
  88. observer?.disconnect();
  89. observer = null;
  90. },
  91. { rootMargin: '240px 0px' },
  92. );
  93. observer.observe(rootRef.value);
  94. });
  95. onBeforeUnmount(() => {
  96. observer?.disconnect();
  97. observer = null;
  98. });
  99. /* ---------------- 全屏 ---------------- */
  100. const { isFullscreen, toggle: toggleFullscreen } = useBlockFullscreen(async () => {
  101. await nextTick();
  102. await viewerRef.value?.resizeAndFit?.();
  103. });
  104. </script>
  105. <style lang="less" scoped>
  106. .ai-relation {
  107. margin: var(--ai-sp-3) 0;
  108. border: 1px solid var(--ai-border, #e5e7eb);
  109. border-radius: var(--ai-radius, 8px);
  110. background: var(--ai-bg, #fff);
  111. overflow: hidden;
  112. &.ai-block-fullscreen {
  113. margin: 0;
  114. .ai-relation__head {
  115. flex: 0 0 auto;
  116. }
  117. .ai-relation__body {
  118. flex: 1 1 auto;
  119. height: auto;
  120. min-height: 0;
  121. }
  122. }
  123. &__head {
  124. display: flex;
  125. align-items: center;
  126. flex-wrap: wrap;
  127. gap: var(--ai-sp-2, 8px);
  128. padding: 6px 8px 6px 12px;
  129. border-bottom: 1px solid var(--ai-border, #e5e7eb);
  130. }
  131. &__label {
  132. font-size: 11px;
  133. letter-spacing: 0.04em;
  134. text-transform: uppercase;
  135. color: var(--ai-text-2, #64748b);
  136. }
  137. &__index {
  138. font-size: 11px;
  139. color: var(--ai-text-2, #64748b);
  140. font-variant-numeric: tabular-nums;
  141. }
  142. &__title {
  143. max-width: 34%;
  144. overflow: hidden;
  145. font-size: 12px;
  146. font-weight: 600;
  147. color: var(--ai-text, #1f2937);
  148. white-space: nowrap;
  149. text-overflow: ellipsis;
  150. }
  151. &__meta {
  152. min-width: 0;
  153. max-width: 40%;
  154. overflow: hidden;
  155. font-size: 12px;
  156. color: var(--ai-text-2, #64748b);
  157. white-space: nowrap;
  158. text-overflow: ellipsis;
  159. }
  160. &__spacer {
  161. flex: 1 1 auto;
  162. }
  163. &__count {
  164. font-size: 11px;
  165. color: var(--ai-text-2, #64748b);
  166. font-variant-numeric: tabular-nums;
  167. white-space: nowrap;
  168. }
  169. &__notice {
  170. padding: 4px 12px;
  171. overflow: hidden;
  172. font-size: 11px;
  173. line-height: 1.6;
  174. color: #b45309;
  175. white-space: nowrap;
  176. text-overflow: ellipsis;
  177. background: rgba(245, 158, 11, 0.08);
  178. border-bottom: 1px solid var(--ai-border, #e5e7eb);
  179. }
  180. &__body {
  181. display: flex;
  182. height: 460px;
  183. min-height: 0;
  184. background: var(--ai-bg-subtle, #f7f8fa);
  185. }
  186. &__placeholder {
  187. display: flex;
  188. flex-direction: column;
  189. justify-content: center;
  190. gap: 10px;
  191. width: 100%;
  192. padding: 24px;
  193. margin: 0;
  194. .ai-skeleton {
  195. border-radius: 6px;
  196. }
  197. }
  198. &__empty {
  199. display: flex;
  200. align-items: center;
  201. justify-content: center;
  202. gap: 8px;
  203. width: 100%;
  204. min-height: 160px;
  205. padding: 24px;
  206. font-size: 12px;
  207. color: var(--ai-text-2, #64748b);
  208. }
  209. &__hint {
  210. flex: 0 0 auto;
  211. padding: 4px 12px;
  212. font-size: 11px;
  213. color: var(--ai-text-2, #64748b);
  214. text-align: center;
  215. border-top: 1px solid var(--ai-border, #e5e7eb);
  216. background: var(--ai-bg-subtle, #f7f8fa);
  217. }
  218. }
  219. </style>