| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <template>
- <Drawer
- :open="open"
- placement="right"
- :width="drawerWidth"
- :closable="false"
- :mask-closable="false"
- :destroy-on-close="false"
- :body-style="bodyStyle"
- wrap-class-name="insight-drawer-wrap"
- @close="close"
- >
- <div :class="['insight-drawer', { 'is-fullscreen': fullscreen }]">
- <!-- 宽度拖拽手柄(480–1000) -->
- <div v-if="!fullscreen" class="insight-drawer__resizer" @mousedown.prevent="startResize"></div>
- <!-- ① Header -->
- <div class="insight-drawer__header">
- <div class="insight-drawer__title">
- <Icon icon="i-mdi:auto-fix" :size="16" />
- <span>AI 研判 · 对象关系分析</span>
- <span v-if="session?.title" class="insight-drawer__subtitle">{{ session.title }}</span>
- </div>
- <div class="insight-drawer__header-actions">
- <Button size="small" :disabled="!canAnalyze || streaming || creating" @click="reanalyze">
- <Icon icon="i-mdi:refresh" :size="14" />
- <span>重新分析</span>
- </Button>
- <InsightHistoryPopover
- ref="historyRef"
- :case-id="caseId"
- :active-id="sessionId"
- :disabled="!caseId"
- @select="onSelectHistory"
- @remove="onRemoveHistory"
- />
- <Button size="small" @click="fullscreen = !fullscreen">
- <Icon :icon="fullscreen ? 'i-mdi:fullscreen-exit' : 'i-mdi:fullscreen'" :size="14" />
- </Button>
- <Button size="small" @click="close">
- <Icon icon="i-mdi:close" :size="14" />
- </Button>
- </div>
- </div>
- <!-- ② ContextBar(固定不滚) -->
- <InsightContextBar
- v-model:top-n="topN"
- :context="context"
- :changed="contextChanged"
- :disabled="streaming || creating"
- @reanalyze="reanalyze"
- />
- <!-- ③ Report / ④ Chat(滚动区) -->
- <div ref="scrollRef" class="insight-drawer__scroll">
- <div v-if="error && !reportMessage" class="insight-drawer__error">
- <Icon icon="i-mdi:alert-circle-outline" :size="14" />
- <span>{{ error }}</span>
- <Button v-if="isModelError" size="small" type="link" @click="goModelSetting">去设置默认模型</Button>
- </div>
- <InsightReport
- :message="reportMessage"
- :payload="payload"
- :creating="creating"
- :streaming="streaming && !reportMessage?.local"
- @action="onClueAction"
- @navigate="onNextStep"
- />
- <template v-if="followupMessages.length">
- <div class="insight-drawer__divider">
- <span>追问对话</span>
- </div>
- <InsightMessageList
- ref="messageListRef"
- :messages="followupMessages"
- :load-more="loadMore"
- :loading-more="loadingMore"
- :load-more-failed="loadMoreFailed"
- :has-more="page < pages"
- :ensure-trace="ensureTrace"
- @retry="onRetryLoadMore"
- />
- </template>
- </div>
- <!-- ⑤ Composer(固定底) -->
- <div class="insight-drawer__composer">
- <div class="insight-drawer__chips">
- <button
- v-for="preset in PRESET_QUESTIONS"
- :key="preset"
- class="insight-drawer__chip"
- type="button"
- :disabled="streaming || creating || !sessionId"
- @click="onPreset(preset)"
- >
- {{ preset }}
- </button>
- </div>
- <div class="insight-drawer__input-row">
- <Textarea
- v-model:value="draft"
- class="insight-drawer__input"
- :auto-size="{ minRows: 1, maxRows: 5 }"
- :maxlength="1000"
- :disabled="streaming || creating || !sessionId"
- placeholder="对某条线索继续追问…"
- @press-enter="onEnter"
- />
- <Button v-if="streaming" size="small" danger @click="stop">
- <Icon icon="i-mdi:stop" :size="14" />
- </Button>
- <Button v-else size="small" type="primary" :disabled="!draft.trim() || creating || !sessionId" @click="send">
- <Icon icon="i-mdi:send" :size="14" />
- </Button>
- </div>
- </div>
- </div>
- </Drawer>
- <!-- 线索卡「查看话单 / 查看交易」跳转 -->
- <CallRecordModal
- v-model:open="callRecordModalOpen"
- title="通信记录"
- :base-query="callRecordBaseQuery"
- :z-index="1015"
- />
- <TransRecordModal
- v-model:open="transRecordModalOpen"
- title="交易记录"
- :base-query="transRecordBaseQuery"
- :z-index="1015"
- />
- </template>
- <script lang="ts" setup>
- /**
- * 对象关系分析 · AI 研判抽屉
- *
- * 打开即自动分析(不用敲字):建会话 → 立刻跑首轮流式。
- * 面板形态选右侧抽屉而不是独立页,是为了「边看图谱边问」不丢上下文;
- * 全屏只是同一组件铺满容器(CSS 切换),不新建页面。
- *
- * 状态机与消息加载全在 useInsightStream(独立 hook,不碰全局 store)。
- */
- import { computed, nextTick, onBeforeUnmount, reactive, ref, watch } from 'vue';
- import { useRouter } from 'vue-router';
- import { Button, Drawer, Textarea } from 'ant-design-vue';
- import Icon from '@/core/components/Icon/src/Icon.vue';
- import CallRecordModal from '@/call/views/components/CallRecordModal.vue';
- import TransRecordModal from '@/trans/views/components/TransRecordModal.vue';
- import InsightContextBar from './InsightContextBar.vue';
- import InsightReport from './InsightReport.vue';
- import InsightMessageList from './InsightMessageList.vue';
- import InsightHistoryPopover from './InsightHistoryPopover.vue';
- import { useInsightStream } from '../hooks/useInsightStream';
- import { DEFAULT_TOP_N, useInsightContext, type InsightPageSnapshot } from '../hooks/useInsightContext';
- import type { InsightSessionVO } from '../types';
- import type { CallRecordQuery } from '@/call/api/types/query';
- import type { TransRecordQuery } from '@/trans/api/types/query';
- const props = withDefaults(
- defineProps<{
- open: boolean;
- caseId: string;
- /** 当前画布 + 分析条件的快照(页面提供,抽屉只负责消费) */
- snapshot: InsightPageSnapshot;
- }>(),
- { caseId: '' },
- );
- const emit = defineEmits<{ 'update:open': [value: boolean] }>();
- const router = useRouter();
- /** 预设追问(覆盖三种最常用的下钻意图) */
- const PRESET_QUESTIONS = [
- '逐条展开高价值线索的数字证据',
- '图里最核心的节点是谁?依据是什么?',
- '这批关系里最反常的一条是哪条?为什么?',
- ];
- /* ---------------- 抽屉宽度(可拖拽 480–1000) ---------------- */
- const MIN_WIDTH = 480;
- const MAX_WIDTH = 1000;
- const width = ref(640);
- const fullscreen = ref(false);
- const drawerWidth = computed(() => (fullscreen.value ? '100%' : `${width.value}px`));
- const bodyStyle = computed(() => ({ padding: '0', height: '100%', overflow: 'hidden' }));
- let resizing = false;
- /** 拖拽中的解绑函数:拖拽中途组件被卸载时兜底摘掉 window 监听,防幽灵拖拽 */
- let detachResize: (() => void) | null = null;
- function startResize(event: MouseEvent) {
- resizing = true;
- const startX = event.clientX;
- const startWidth = width.value;
- const onMove = (e: MouseEvent) => {
- if (!resizing) return;
- // 抽屉贴右边,鼠标左移 = 变宽
- const next = startWidth - (e.clientX - startX);
- width.value = Math.min(MAX_WIDTH, Math.max(MIN_WIDTH, next));
- };
- const onUp = () => {
- resizing = false;
- detachResize?.();
- };
- window.addEventListener('mousemove', onMove);
- window.addEventListener('mouseup', onUp);
- detachResize = () => {
- window.removeEventListener('mousemove', onMove);
- window.removeEventListener('mouseup', onUp);
- detachResize = null;
- };
- }
- /* ---------------- 状态机 ---------------- */
- const caseIdRef = computed(() => props.caseId);
- const insight = useInsightStream(caseIdRef);
- const {
- sessionId,
- session,
- context,
- reportMessage,
- followupMessages,
- payload,
- creating,
- streaming,
- loadingMore,
- loadMoreFailed,
- error,
- page,
- pages,
- start,
- openSession,
- followup,
- stop,
- reset,
- remove,
- loadMore,
- ensureTrace,
- } = insight;
- /**
- * 组件卸载(路由切走 / 父组件销毁)不会触发 `watch(open)`,
- * 必须在这里兜底:① 摘掉拖拽留下的 window 监听;② 中止仍在跑的 SSE 流。
- * 少了这一步,流会在后台继续烧 token 直到服务端自己结束。
- */
- onBeforeUnmount(() => {
- resizing = false;
- detachResize?.();
- stop();
- });
- const topN = ref(DEFAULT_TOP_N);
- const draft = ref('');
- const scrollRef = ref<HTMLElement | null>(null);
- const messageListRef = ref<InstanceType<typeof InsightMessageList> | null>(null);
- const historyRef = ref<InstanceType<typeof InsightHistoryPopover> | null>(null);
- const { snapshotSignature, buildCreateParams } = useInsightContext(computed(() => props.snapshot));
- /** 建会话时的数据签名:变了就提示「可重新分析」,但不打断正在跑的流 */
- const analyzedSignature = ref('');
- const contextChanged = computed(
- () =>
- !!sessionId.value &&
- !streaming.value &&
- !!analyzedSignature.value &&
- snapshotSignature.value !== analyzedSignature.value,
- );
- /** 画布上没有任何节点/关系时仍允许分析(模型会回答"无数据"),只拦未选案件 */
- const canAnalyze = computed(() => !!props.caseId);
- const isModelError = computed(() => /默认模型/.test(error.value));
- async function reanalyze() {
- if (!props.caseId) return;
- if (streaming.value) stop();
- reset();
- analyzedSignature.value = snapshotSignature.value;
- try {
- await start(buildCreateParams, topN.value);
- } catch {
- /* 错误已进 error,由视图展示 */
- }
- }
- watch(
- () => props.open,
- async (value) => {
- if (value) {
- // 打开即分析:已有会话(切案件后没重置)则不重复建
- if (!sessionId.value && !creating.value) {
- await reanalyze();
- }
- } else {
- // 关闭时中止流,避免后台继续烧 token
- stop();
- }
- },
- { immediate: true },
- );
- // 切换案件:清空并重来
- watch(
- () => props.caseId,
- () => {
- reset();
- analyzedSignature.value = '';
- },
- );
- function close() {
- stop();
- emit('update:open', false);
- }
- /* ---------------- 追问 ---------------- */
- async function send() {
- const text = draft.value.trim();
- if (!text) return;
- draft.value = '';
- await followup(text);
- await scrollToBottom();
- }
- function onEnter(event: KeyboardEvent) {
- if (event.shiftKey) return;
- event.preventDefault();
- send();
- }
- function onPreset(question: string) {
- draft.value = question;
- send();
- }
- async function scrollToBottom() {
- await nextTick();
- const el = scrollRef.value;
- if (el) el.scrollTop = el.scrollHeight;
- }
- /* ---------------- 历史记录 ---------------- */
- async function onSelectHistory(item: InsightSessionVO) {
- analyzedSignature.value = snapshotSignature.value;
- await openSession(item.id);
- }
- async function onRemoveHistory(item: InsightSessionVO) {
- try {
- await remove(item.id);
- historyRef.value?.refresh();
- } catch {
- /* 删除失败静默(列表刷新后仍可见,用户可重试) */
- }
- }
- /* ---------------- 线索卡动作 / 下一步跳转 ---------------- */
- const callRecordModalOpen = ref(false);
- const callRecordBaseQuery = reactive<Partial<CallRecordQuery>>({});
- const transRecordModalOpen = ref(false);
- const transRecordBaseQuery = reactive<Partial<TransRecordQuery>>({});
- function onClueAction(link: any) {
- const params = link?.params || {};
- if (link?.action === 'openCallRecord') {
- callRecordBaseQuery.personName = params.personName || undefined;
- callRecordBaseQuery.otherPhones = Array.isArray(params.otherPhones)
- ? params.otherPhones
- : params.otherPhone
- ? [params.otherPhone]
- : undefined;
- callRecordBaseQuery.startDate = params.startDate || undefined;
- callRecordBaseQuery.endDate = params.endDate || undefined;
- callRecordModalOpen.value = true;
- return;
- }
- if (link?.action === 'openTransRecord') {
- transRecordBaseQuery.personName = params.personName || undefined;
- transRecordBaseQuery.otherName = params.otherName || undefined;
- transRecordBaseQuery.otherCardNo = params.otherCardNo || undefined;
- transRecordBaseQuery.startDate = params.startDate || undefined;
- transRecordBaseQuery.endDate = params.endDate || undefined;
- transRecordModalOpen.value = true;
- }
- }
- function onNextStep(step: any) {
- const route = step?.entry?.route;
- if (!route) return;
- router.push(route);
- }
- function goModelSetting() {
- router.push('/ai/model');
- }
- async function onRetryLoadMore() {
- await loadMore();
- }
- // 追问回答落地后滚到底(只在消息条数变化时触发,避免打断用户上滑)
- watch(
- () => followupMessages.value.length,
- async (next, prev) => {
- if (next > prev) await scrollToBottom();
- },
- );
- /** 供外部(页面)判断是否可关闭 */
- defineExpose({ messageListRef, sessionId });
- </script>
- <style lang="less" scoped>
- .insight-drawer {
- position: relative;
- display: flex;
- flex-direction: column;
- height: 100%;
- background: #f5f7fa;
- &__resizer {
- position: absolute;
- top: 0;
- left: 0;
- z-index: 10;
- width: 6px;
- height: 100%;
- cursor: col-resize;
- &:hover {
- background: rgb(37 99 235 / 12%);
- }
- }
- &__header {
- flex: 0 0 auto;
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 8px;
- height: 46px;
- padding: 0 12px;
- background: #fff;
- border-bottom: 1px solid #e2e8f0;
- }
- &__title {
- display: flex;
- align-items: center;
- gap: 6px;
- min-width: 0;
- font-size: 14px;
- font-weight: 600;
- color: #111827;
- }
- &__subtitle {
- overflow: hidden;
- font-size: 11px;
- font-weight: 400;
- color: #94a3b8;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- &__header-actions {
- display: flex;
- align-items: center;
- gap: 6px;
- flex-shrink: 0;
- }
- &__scroll {
- flex: 1 1 auto;
- min-height: 0;
- padding: 10px 12px;
- overflow: auto;
- }
- &__error {
- display: flex;
- align-items: center;
- gap: 6px;
- margin-bottom: 10px;
- padding: 8px 10px;
- font-size: 12px;
- color: #b91c1c;
- background: #fef2f2;
- border: 1px solid #fecaca;
- border-radius: 6px;
- }
- &__divider {
- display: flex;
- align-items: center;
- gap: 8px;
- margin: 14px 0 10px;
- font-size: 11px;
- color: #94a3b8;
- &::before,
- &::after {
- flex: 1 1 auto;
- height: 1px;
- background: #e2e8f0;
- content: '';
- }
- }
- &__composer {
- flex: 0 0 auto;
- padding: 8px 12px 10px;
- background: #fff;
- border-top: 1px solid #e2e8f0;
- }
- &__chips {
- display: flex;
- flex-wrap: wrap;
- gap: 6px;
- margin-bottom: 6px;
- }
- &__chip {
- padding: 2px 8px;
- font-size: 11px;
- line-height: 18px;
- color: #2563eb;
- background: #eff6ff;
- border: 1px solid #bfdbfe;
- border-radius: 999px;
- cursor: pointer;
- &:disabled {
- color: #94a3b8;
- background: #f8fafc;
- border-color: #e2e8f0;
- cursor: not-allowed;
- }
- }
- &__input-row {
- display: flex;
- align-items: flex-end;
- gap: 8px;
- }
- &__input {
- flex: 1 1 auto;
- }
- }
- </style>
- <style lang="less">
- /* 抽屉挂到 body 上,样式不能 scoped */
- .insight-drawer-wrap {
- .ant-drawer-body {
- padding: 0 !important;
- overflow: hidden !important;
- }
- }
- </style>
|