InsightDrawer.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <Drawer
  3. :open="open"
  4. placement="right"
  5. :width="drawerWidth"
  6. :closable="false"
  7. :mask-closable="false"
  8. :destroy-on-close="false"
  9. :body-style="bodyStyle"
  10. wrap-class-name="insight-drawer-wrap"
  11. @close="close"
  12. >
  13. <div :class="['insight-drawer', { 'is-fullscreen': fullscreen }]">
  14. <!-- 宽度拖拽手柄(480–1000) -->
  15. <div v-if="!fullscreen" class="insight-drawer__resizer" @mousedown.prevent="startResize"></div>
  16. <!-- ① Header -->
  17. <div class="insight-drawer__header">
  18. <div class="insight-drawer__title">
  19. <Icon icon="i-mdi:auto-fix" :size="16" />
  20. <span>AI 研判 · 对象关系分析</span>
  21. <span v-if="session?.title" class="insight-drawer__subtitle">{{ session.title }}</span>
  22. </div>
  23. <div class="insight-drawer__header-actions">
  24. <Button size="small" :disabled="!canAnalyze || streaming || creating" @click="reanalyze">
  25. <Icon icon="i-mdi:refresh" :size="14" />
  26. <span>重新分析</span>
  27. </Button>
  28. <InsightHistoryPopover
  29. ref="historyRef"
  30. :case-id="caseId"
  31. :active-id="sessionId"
  32. :disabled="!caseId"
  33. @select="onSelectHistory"
  34. @remove="onRemoveHistory"
  35. />
  36. <Button size="small" @click="fullscreen = !fullscreen">
  37. <Icon :icon="fullscreen ? 'i-mdi:fullscreen-exit' : 'i-mdi:fullscreen'" :size="14" />
  38. </Button>
  39. <Button size="small" @click="close">
  40. <Icon icon="i-mdi:close" :size="14" />
  41. </Button>
  42. </div>
  43. </div>
  44. <!-- ② ContextBar(固定不滚) -->
  45. <InsightContextBar
  46. v-model:top-n="topN"
  47. :context="context"
  48. :changed="contextChanged"
  49. :disabled="streaming || creating"
  50. @reanalyze="reanalyze"
  51. />
  52. <!-- ③ Report / ④ Chat(滚动区) -->
  53. <div ref="scrollRef" class="insight-drawer__scroll">
  54. <div v-if="error && !reportMessage" class="insight-drawer__error">
  55. <Icon icon="i-mdi:alert-circle-outline" :size="14" />
  56. <span>{{ error }}</span>
  57. <Button v-if="isModelError" size="small" type="link" @click="goModelSetting">去设置默认模型</Button>
  58. </div>
  59. <InsightReport
  60. :message="reportMessage"
  61. :payload="payload"
  62. :creating="creating"
  63. :streaming="streaming && !reportMessage?.local"
  64. @action="onClueAction"
  65. @navigate="onNextStep"
  66. />
  67. <template v-if="followupMessages.length">
  68. <div class="insight-drawer__divider">
  69. <span>追问对话</span>
  70. </div>
  71. <InsightMessageList
  72. ref="messageListRef"
  73. :messages="followupMessages"
  74. :load-more="loadMore"
  75. :loading-more="loadingMore"
  76. :load-more-failed="loadMoreFailed"
  77. :has-more="page < pages"
  78. :ensure-trace="ensureTrace"
  79. @retry="onRetryLoadMore"
  80. />
  81. </template>
  82. </div>
  83. <!-- ⑤ Composer(固定底) -->
  84. <div class="insight-drawer__composer">
  85. <div class="insight-drawer__chips">
  86. <button
  87. v-for="preset in PRESET_QUESTIONS"
  88. :key="preset"
  89. class="insight-drawer__chip"
  90. type="button"
  91. :disabled="streaming || creating || !sessionId"
  92. @click="onPreset(preset)"
  93. >
  94. {{ preset }}
  95. </button>
  96. </div>
  97. <div class="insight-drawer__input-row">
  98. <Textarea
  99. v-model:value="draft"
  100. class="insight-drawer__input"
  101. :auto-size="{ minRows: 1, maxRows: 5 }"
  102. :maxlength="1000"
  103. :disabled="streaming || creating || !sessionId"
  104. placeholder="对某条线索继续追问…"
  105. @press-enter="onEnter"
  106. />
  107. <Button v-if="streaming" size="small" danger @click="stop">
  108. <Icon icon="i-mdi:stop" :size="14" />
  109. </Button>
  110. <Button v-else size="small" type="primary" :disabled="!draft.trim() || creating || !sessionId" @click="send">
  111. <Icon icon="i-mdi:send" :size="14" />
  112. </Button>
  113. </div>
  114. </div>
  115. </div>
  116. </Drawer>
  117. <!-- 线索卡「查看话单 / 查看交易」跳转 -->
  118. <CallRecordModal
  119. v-model:open="callRecordModalOpen"
  120. title="通信记录"
  121. :base-query="callRecordBaseQuery"
  122. :z-index="1015"
  123. />
  124. <TransRecordModal
  125. v-model:open="transRecordModalOpen"
  126. title="交易记录"
  127. :base-query="transRecordBaseQuery"
  128. :z-index="1015"
  129. />
  130. </template>
  131. <script lang="ts" setup>
  132. /**
  133. * 对象关系分析 · AI 研判抽屉
  134. *
  135. * 打开即自动分析(不用敲字):建会话 → 立刻跑首轮流式。
  136. * 面板形态选右侧抽屉而不是独立页,是为了「边看图谱边问」不丢上下文;
  137. * 全屏只是同一组件铺满容器(CSS 切换),不新建页面。
  138. *
  139. * 状态机与消息加载全在 useInsightStream(独立 hook,不碰全局 store)。
  140. */
  141. import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from 'vue';
  142. import { useRouter } from 'vue-router';
  143. import { Button, Drawer, Textarea } from 'ant-design-vue';
  144. import Icon from '@/core/components/Icon/src/Icon.vue';
  145. import CallRecordModal from '@/call/views/components/CallRecordModal.vue';
  146. import TransRecordModal from '@/trans/views/components/TransRecordModal.vue';
  147. import InsightContextBar from './InsightContextBar.vue';
  148. import InsightReport from './InsightReport.vue';
  149. import InsightMessageList from './InsightMessageList.vue';
  150. import InsightHistoryPopover from './InsightHistoryPopover.vue';
  151. import { useInsightStream } from '../hooks/useInsightStream';
  152. import { DEFAULT_TOP_N, useInsightContext, type InsightPageSnapshot } from '../hooks/useInsightContext';
  153. import type { InsightSessionVO } from '../types';
  154. import type { CallRecordQuery } from '@/call/api/types/query';
  155. import type { TransRecordQuery } from '@/trans/api/types/query';
  156. const props = withDefaults(
  157. defineProps<{
  158. open: boolean;
  159. caseId: string;
  160. /** 当前画布 + 分析条件的快照(页面提供,抽屉只负责消费) */
  161. snapshot: InsightPageSnapshot;
  162. }>(),
  163. { caseId: '' },
  164. );
  165. const emit = defineEmits<{ 'update:open': [value: boolean] }>();
  166. const router = useRouter();
  167. /** 预设追问(覆盖三种最常用的下钻意图) */
  168. const PRESET_QUESTIONS = [
  169. '逐条展开高价值线索的数字证据',
  170. '图里最核心的节点是谁?依据是什么?',
  171. '这批关系里最反常的一条是哪条?为什么?',
  172. ];
  173. /* ---------------- 抽屉宽度(可拖拽 480–1000) ---------------- */
  174. const MIN_WIDTH = 480;
  175. const MAX_WIDTH = 1000;
  176. const width = ref(640);
  177. const fullscreen = ref(false);
  178. const drawerWidth = computed(() => (fullscreen.value ? '100%' : `${width.value}px`));
  179. const bodyStyle = computed(() => ({ padding: '0', height: '100%', overflow: 'hidden' }));
  180. let resizing = false;
  181. /** 拖拽中的解绑函数:拖拽中途组件被卸载时兜底摘掉 window 监听,防幽灵拖拽 */
  182. let detachResize: (() => void) | null = null;
  183. function startResize(event: MouseEvent) {
  184. resizing = true;
  185. const startX = event.clientX;
  186. const startWidth = width.value;
  187. const onMove = (e: MouseEvent) => {
  188. if (!resizing) return;
  189. // 抽屉贴右边,鼠标左移 = 变宽
  190. const next = startWidth - (e.clientX - startX);
  191. width.value = Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, next));
  192. };
  193. const onUp = () => {
  194. resizing = false;
  195. detachResize?.();
  196. };
  197. window.addEventListener('mousemove', onMove);
  198. window.addEventListener('mouseup', onUp);
  199. detachResize = () => {
  200. window.removeEventListener('mousemove', onMove);
  201. window.removeEventListener('mouseup', onUp);
  202. detachResize = null;
  203. };
  204. }
  205. /* ---------------- 状态机 ---------------- */
  206. const caseIdRef = computed(() => props.caseId);
  207. const insight = useInsightStream(caseIdRef);
  208. const {
  209. sessionId,
  210. session,
  211. context,
  212. reportMessage,
  213. followupMessages,
  214. payload,
  215. creating,
  216. streaming,
  217. loadingMore,
  218. loadMoreFailed,
  219. error,
  220. page,
  221. pages,
  222. start,
  223. openSession,
  224. followup,
  225. stop,
  226. reset,
  227. remove,
  228. loadMore,
  229. ensureTrace,
  230. } = insight;
  231. /**
  232. * 组件卸载(路由切走 / 父组件销毁)不会触发 `watch(open)`,
  233. * 必须在这里兜底:① 摘掉拖拽留下的 window 监听;② 中止仍在跑的 SSE 流。
  234. * 少了这一步,流会在后台继续烧 token 直到服务端自己结束。
  235. */
  236. onBeforeUnmount(() => {
  237. resizing = false;
  238. detachResize?.();
  239. stop();
  240. });
  241. const topN = ref(DEFAULT_TOP_N);
  242. const draft = ref('');
  243. const scrollRef = ref<HTMLElement | null>(null);
  244. const messageListRef = ref<InstanceType<typeof InsightMessageList> | null>(null);
  245. const historyRef = ref<InstanceType<typeof InsightHistoryPopover> | null>(null);
  246. const { snapshotSignature, buildCreateParams } = useInsightContext(computed(() => props.snapshot));
  247. /** 建会话时的数据签名:变了就提示「可重新分析」,但不打断正在跑的流 */
  248. const analyzedSignature = ref('');
  249. const contextChanged = computed(
  250. () =>
  251. !!sessionId.value &&
  252. !streaming.value &&
  253. !!analyzedSignature.value &&
  254. snapshotSignature.value !== analyzedSignature.value,
  255. );
  256. /** 画布上没有任何节点/关系时仍允许分析(模型会回答"无数据"),只拦未选案件 */
  257. const canAnalyze = computed(() => !!props.caseId);
  258. const isModelError = computed(() => /默认模型/.test(error.value));
  259. async function reanalyze() {
  260. if (!props.caseId) return;
  261. if (streaming.value) stop();
  262. reset();
  263. analyzedSignature.value = snapshotSignature.value;
  264. try {
  265. await start(buildCreateParams, topN.value);
  266. } catch {
  267. /* 错误已进 error,由视图展示 */
  268. }
  269. }
  270. watch(
  271. () => props.open,
  272. async (value) => {
  273. if (value) {
  274. // 打开即分析:已有会话(切案件后没重置)则不重复建
  275. if (!sessionId.value && !creating.value) {
  276. await reanalyze();
  277. }
  278. } else {
  279. // 关闭时中止流,避免后台继续烧 token
  280. stop();
  281. }
  282. },
  283. { immediate: true },
  284. );
  285. // 切换案件:清空并重来
  286. watch(
  287. () => props.caseId,
  288. () => {
  289. reset();
  290. analyzedSignature.value = '';
  291. },
  292. );
  293. function close() {
  294. stop();
  295. emit('update:open', false);
  296. }
  297. /* ---------------- 追问 ---------------- */
  298. async function send() {
  299. const text = draft.value.trim();
  300. if (!text) return;
  301. draft.value = '';
  302. await followup(text);
  303. await scrollToBottom();
  304. }
  305. function onEnter(event: KeyboardEvent) {
  306. if (event.shiftKey) return;
  307. event.preventDefault();
  308. send();
  309. }
  310. function onPreset(question: string) {
  311. draft.value = question;
  312. send();
  313. }
  314. async function scrollToBottom() {
  315. await nextTick();
  316. const el = scrollRef.value;
  317. if (el) el.scrollTop = el.scrollHeight;
  318. }
  319. /* ---------------- 历史记录 ---------------- */
  320. async function onSelectHistory(item: InsightSessionVO) {
  321. analyzedSignature.value = snapshotSignature.value;
  322. await openSession(item.id);
  323. }
  324. async function onRemoveHistory(item: InsightSessionVO) {
  325. try {
  326. await remove(item.id);
  327. historyRef.value?.refresh();
  328. } catch {
  329. /* 删除失败静默(列表刷新后仍可见,用户可重试) */
  330. }
  331. }
  332. /* ---------------- 线索卡动作 / 下一步跳转 ---------------- */
  333. const callRecordModalOpen = ref(false);
  334. const callRecordBaseQuery = reactive<Partial<CallRecordQuery>>({});
  335. const transRecordModalOpen = ref(false);
  336. const transRecordBaseQuery = reactive<Partial<TransRecordQuery>>({});
  337. function onClueAction(link: any) {
  338. const params = link?.params || {};
  339. if (link?.action === 'openCallRecord') {
  340. callRecordBaseQuery.personName = params.personName || undefined;
  341. callRecordBaseQuery.otherPhones = Array.isArray(params.otherPhones)
  342. ? params.otherPhones
  343. : params.otherPhone
  344. ? [params.otherPhone]
  345. : undefined;
  346. callRecordBaseQuery.startDate = params.startDate || undefined;
  347. callRecordBaseQuery.endDate = params.endDate || undefined;
  348. callRecordModalOpen.value = true;
  349. return;
  350. }
  351. if (link?.action === 'openTransRecord') {
  352. transRecordBaseQuery.personName = params.personName || undefined;
  353. transRecordBaseQuery.otherName = params.otherName || undefined;
  354. transRecordBaseQuery.otherCardNo = params.otherCardNo || undefined;
  355. transRecordBaseQuery.startDate = params.startDate || undefined;
  356. transRecordBaseQuery.endDate = params.endDate || undefined;
  357. transRecordModalOpen.value = true;
  358. }
  359. }
  360. function onNextStep(step: any) {
  361. const route = step?.entry?.route;
  362. if (!route) return;
  363. router.push(route);
  364. }
  365. function goModelSetting() {
  366. router.push('/ai/model');
  367. }
  368. async function onRetryLoadMore() {
  369. await loadMore();
  370. }
  371. // 追问回答落地后滚到底(只在消息条数变化时触发,避免打断用户上滑)
  372. watch(
  373. () => followupMessages.value.length,
  374. async (next, prev) => {
  375. if (next > prev) await scrollToBottom();
  376. },
  377. );
  378. /** 供外部(页面)判断是否可关闭 */
  379. defineExpose({ messageListRef, sessionId });
  380. </script>
  381. <style lang="less" scoped>
  382. .insight-drawer {
  383. position: relative;
  384. display: flex;
  385. flex-direction: column;
  386. height: 100%;
  387. background: #f5f7fa;
  388. &__resizer {
  389. position: absolute;
  390. top: 0;
  391. left: 0;
  392. z-index: 10;
  393. width: 6px;
  394. height: 100%;
  395. cursor: col-resize;
  396. &:hover {
  397. background: rgb(37 99 235 / 12%);
  398. }
  399. }
  400. &__header {
  401. flex: 0 0 auto;
  402. display: flex;
  403. align-items: center;
  404. justify-content: space-between;
  405. gap: 8px;
  406. height: 46px;
  407. padding: 0 12px;
  408. background: #fff;
  409. border-bottom: 1px solid #e2e8f0;
  410. }
  411. &__title {
  412. display: flex;
  413. align-items: center;
  414. gap: 6px;
  415. min-width: 0;
  416. font-size: 14px;
  417. font-weight: 600;
  418. color: #111827;
  419. }
  420. &__subtitle {
  421. overflow: hidden;
  422. font-size: 11px;
  423. font-weight: 400;
  424. color: #94a3b8;
  425. white-space: nowrap;
  426. text-overflow: ellipsis;
  427. }
  428. &__header-actions {
  429. display: flex;
  430. align-items: center;
  431. gap: 6px;
  432. flex-shrink: 0;
  433. }
  434. &__scroll {
  435. flex: 1 1 auto;
  436. min-height: 0;
  437. padding: 10px 12px;
  438. overflow: auto;
  439. }
  440. &__error {
  441. display: flex;
  442. align-items: center;
  443. gap: 6px;
  444. margin-bottom: 10px;
  445. padding: 8px 10px;
  446. font-size: 12px;
  447. color: #b91c1c;
  448. background: #fef2f2;
  449. border: 1px solid #fecaca;
  450. border-radius: 6px;
  451. }
  452. &__divider {
  453. display: flex;
  454. align-items: center;
  455. gap: 8px;
  456. margin: 14px 0 10px;
  457. font-size: 11px;
  458. color: #94a3b8;
  459. &::before,
  460. &::after {
  461. flex: 1 1 auto;
  462. height: 1px;
  463. background: #e2e8f0;
  464. content: '';
  465. }
  466. }
  467. &__composer {
  468. flex: 0 0 auto;
  469. padding: 8px 12px 10px;
  470. background: #fff;
  471. border-top: 1px solid #e2e8f0;
  472. }
  473. &__chips {
  474. display: flex;
  475. flex-wrap: wrap;
  476. gap: 6px;
  477. margin-bottom: 6px;
  478. }
  479. &__chip {
  480. padding: 2px 8px;
  481. font-size: 11px;
  482. line-height: 18px;
  483. color: #2563eb;
  484. background: #eff6ff;
  485. border: 1px solid #bfdbfe;
  486. border-radius: 999px;
  487. cursor: pointer;
  488. &:disabled {
  489. color: #94a3b8;
  490. background: #f8fafc;
  491. border-color: #e2e8f0;
  492. cursor: not-allowed;
  493. }
  494. }
  495. &__input-row {
  496. display: flex;
  497. align-items: flex-end;
  498. gap: 8px;
  499. }
  500. &__input {
  501. flex: 1 1 auto;
  502. }
  503. }
  504. </style>
  505. <style lang="less">
  506. /* 抽屉挂到 body 上,样式不能 scoped */
  507. .insight-drawer-wrap {
  508. .ant-drawer-body {
  509. padding: 0 !important;
  510. overflow: hidden !important;
  511. }
  512. }
  513. </style>