Просмотр исходного кода

fix(insight): 修复研判流式渲染、自动吸底与产物预览

- 流式消息改用 reactive 包装,token/思考/工具调用实时刷新
- 会话创建后及时结束 creating 态,骨架屏随进度切换/让位
- 内容增长时未手动上滑则自动吸底,停止后不再强制滚动
- 新增执行中状态条动画,模型思考期间也能看出仍在运行
- 研判抽屉挂载产物预览弹窗,CSV 卡片可正常查看
- 移除头部重复的重新分析按钮,并明确停止按钮红色样式
cc 3 недель назад
Родитель
Сommit
d9aef858d0

+ 6 - 2
ai-electron/frontend/src/ai/components/ArtifactPreviewModal.vue

@@ -1,6 +1,6 @@
 <template>
   <!--
-    产物 CSV 预览弹窗(全站唯一实例,挂在智能工作台上)。
+    产物 CSV 预览弹窗(全站单例,挂在智能工作台 / AI 研判抽屉)。
     弹框形态与 AgentFormModal 等一致:BasicModal 右上角自带「全屏/还原 + 关闭」,
     窗口态按视口限高、内部滚动;表格列宽按表头自适应(列名完整展示),列多时横向滚动。
   -->
@@ -8,6 +8,7 @@
     :open="!!preview"
     :title="preview?.fileName || '产物预览'"
     :width="'78vw'"
+    :z-index="props.zIndex"
     :show-ok-btn="false"
     :show-cancel-btn="false"
     wrap-class-name="ai-artifact-preview-modal"
@@ -58,7 +59,7 @@
 
 <script lang="ts" setup name="ArtifactPreviewModal">
   /**
-   * 产物 CSV 预览弹窗(全站唯一实例,挂在智能工作台上)。
+   * 产物 CSV 预览弹窗(全站单例,挂在智能工作台 / AI 研判抽屉)。
    *
    * <p>预览与下载是两条数据路径:这里只取服务端前 {@code preview-limit-max} 行做结构预览,
    * 「下载完整 CSV」才拿全量文件 —— 大文件不会因为「想看一眼」被整读进内存。
@@ -75,6 +76,9 @@
 
   const PREVIEW_LIMIT = 200;
 
+  /** 弹窗层级:抽屉里使用时需要高于 Drawer,避免被抽屉遮罩压住 */
+  const props = defineProps<{ zIndex?: number }>();
+
   const { createMessage } = useMessage();
 
   const preview = computed(() => previewArtifact.value);

+ 158 - 16
ai-electron/frontend/src/graph/insight/components/InsightDrawer.vue

@@ -22,10 +22,6 @@
           <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"
@@ -52,19 +48,32 @@
         @reanalyze="reanalyze"
       />
 
+      <!-- 执行状态:模型思考 / 工具执行期间始终可见,避免暂时没有新输出时误以为已经停了 -->
+      <div v-if="creating || streaming" class="insight-drawer__running" role="status" aria-live="polite">
+        <span class="insight-drawer__running-dot"></span>
+        <span>{{ creating ? '正在创建分析会话' : 'AI 正在分析' }}</span>
+        <span class="insight-drawer__running-dots" aria-hidden="true">
+          <i></i>
+          <i></i>
+          <i></i>
+        </span>
+      </div>
+
       <!-- ③ Report / ④ Chat(滚动区) -->
-      <div ref="scrollRef" class="insight-drawer__scroll">
+      <div ref="scrollRef" class="insight-drawer__scroll" @scroll.passive="onScroll">
         <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>
 
+        <!-- 以首轮消息自身的 streaming 为准:首轮消息 local=true 且正在流式时为 true;
+             追问流式时首轮报告不是流式目标,这里保持 false,不能拿全局 streaming 判断 -->
         <InsightReport
           :message="reportMessage"
           :payload="payload"
           :creating="creating"
-          :streaming="streaming && !reportMessage?.local"
+          :streaming="!!reportMessage?.streaming"
           @action="onClueAction"
           @navigate="onNextStep"
         />
@@ -134,6 +143,8 @@
     :base-query="transRecordBaseQuery"
     :z-index="1015"
   />
+  <!-- 产物 CSV 预览:全站单例由 previewArtifact 驱动,智能工作台之外需要本地挂一份 -->
+  <ArtifactPreviewModal :z-index="1015" />
 </template>
 
 <script lang="ts" setup>
@@ -152,6 +163,7 @@
   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 ArtifactPreviewModal from '@/ai/components/ArtifactPreviewModal.vue';
   import InsightContextBar from './InsightContextBar.vue';
   import InsightReport from './InsightReport.vue';
   import InsightMessageList from './InsightMessageList.vue';
@@ -255,12 +267,20 @@
   onBeforeUnmount(() => {
     resizing = false;
     detachResize?.();
+    if (autoScrollRaf != null) {
+      cancelAnimationFrame(autoScrollRaf);
+      autoScrollRaf = null;
+    }
     stop();
   });
 
   const topN = ref(DEFAULT_TOP_N);
   const draft = ref('');
   const scrollRef = ref<HTMLElement | null>(null);
+  /** 是否吸底:用户没有手动上滑时自动跟随新内容;手动上滑后停止跟随 */
+  const stickToBottom = ref(true);
+  const STICK_BOTTOM_THRESHOLD = 48;
+  let autoScrollRaf: number | null = null;
   const messageListRef = ref<InstanceType<typeof InsightMessageList> | null>(null);
   const historyRef = ref<InstanceType<typeof InsightHistoryPopover> | null>(null);
 
@@ -276,15 +296,13 @@
       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();
+    stickToBottom.value = true;
     analyzedSignature.value = snapshotSignature.value;
     try {
       await start(buildCreateParams, topN.value);
@@ -297,9 +315,12 @@
     () => props.open,
     async (value) => {
       if (value) {
+        stickToBottom.value = true;
         // 打开即分析:已有会话(切案件后没重置)则不重复建
         if (!sessionId.value && !creating.value) {
           await reanalyze();
+        } else {
+          await scrollToBottom();
         }
       } else {
         // 关闭时中止流,避免后台继续烧 token
@@ -329,8 +350,9 @@
     const text = draft.value.trim();
     if (!text) return;
     draft.value = '';
+    // 用户主动发问,重新进入跟随态;后续由 scheduleAutoScroll 按内容增长吸底
+    stickToBottom.value = true;
     await followup(text);
-    await scrollToBottom();
   }
 
   function onEnter(event: KeyboardEvent) {
@@ -344,10 +366,31 @@
     send();
   }
 
+  /** 用户滚动:距底部超过阈值即视为手动上滑,暂停自动跟随;回到底部后恢复 */
+  function onScroll() {
+    const el = scrollRef.value;
+    if (!el) return;
+    stickToBottom.value = el.scrollHeight - el.scrollTop - el.clientHeight <= STICK_BOTTOM_THRESHOLD;
+  }
+
   async function scrollToBottom() {
     await nextTick();
     const el = scrollRef.value;
-    if (el) el.scrollTop = el.scrollHeight;
+    if (!el) return;
+    stickToBottom.value = true;
+    el.scrollTop = el.scrollHeight;
+  }
+
+  /** 同一帧内的多次 token/思考/工具更新只滚一次,避免每 token 强制回流 */
+  function scheduleAutoScroll() {
+    if (!stickToBottom.value || autoScrollRaf != null) return;
+    autoScrollRaf = requestAnimationFrame(async () => {
+      autoScrollRaf = null;
+      if (!stickToBottom.value) return;
+      await nextTick();
+      const el = scrollRef.value;
+      if (el && stickToBottom.value) el.scrollTop = el.scrollHeight;
+    });
   }
 
   /* ---------------- 历史记录 ---------------- */
@@ -355,6 +398,7 @@
   async function onSelectHistory(item: InsightSessionVO) {
     analyzedSignature.value = snapshotSignature.value;
     await openSession(item.id);
+    await scrollToBottom();
   }
 
   async function onRemoveHistory(item: InsightSessionVO) {
@@ -411,12 +455,23 @@
     await loadMore();
   }
 
-  // 追问回答落地后滚到底(只在消息条数变化时触发,避免打断用户上滑)
+  // 首轮/追问的流式内容、思考、工具调用或消息条数变化时,未手动上滑就自动吸底。
+  // 只监听内容增长的信号,不监听滚动位置本身(由 onScroll 维护 stickToBottom)。
+  const lastFollowupMessage = computed(() => followupMessages.value[followupMessages.value.length - 1]);
   watch(
-    () => followupMessages.value.length,
-    async (next, prev) => {
-      if (next > prev) await scrollToBottom();
-    },
+    () => [
+      reportMessage.value?.streaming,
+      reportMessage.value?.content,
+      reportMessage.value?.thinking,
+      reportMessage.value?.toolSteps?.length,
+      payload.value,
+      followupMessages.value.length,
+      lastFollowupMessage.value?.content,
+      lastFollowupMessage.value?.thinking,
+      lastFollowupMessage.value?.toolSteps?.length,
+    ],
+    scheduleAutoScroll,
+    { flush: 'post' },
   );
 
   /** 供外部(页面)判断是否可关闭 */
@@ -483,6 +538,50 @@
       flex-shrink: 0;
     }
 
+    /* 执行中状态条:与滚动区分离,思考期间不随内容滚走 */
+    &__running {
+      flex: 0 0 auto;
+      display: flex;
+      align-items: center;
+      gap: 8px;
+      padding: 6px 14px;
+      font-size: 12px;
+      font-weight: 500;
+      color: #2a50ec;
+      background: #eef4ff;
+      border-bottom: 1px solid #dbe7ff;
+    }
+
+    &__running-dot {
+      width: 7px;
+      height: 7px;
+      border-radius: 50%;
+      background: #2a50ec;
+      animation: insight-running-pulse 1.2s ease-in-out infinite;
+    }
+
+    &__running-dots {
+      display: inline-flex;
+      align-items: center;
+      gap: 3px;
+
+      i {
+        width: 4px;
+        height: 4px;
+        border-radius: 50%;
+        background: #2a50ec;
+        animation: insight-running-bounce 1.2s ease-in-out infinite;
+
+        &:nth-child(2) {
+          animation-delay: 0.15s;
+        }
+
+        &:nth-child(3) {
+          animation-delay: 0.3s;
+        }
+      }
+    }
+
     &__scroll {
       flex: 1 1 auto;
       min-height: 0;
@@ -556,12 +655,55 @@
       display: flex;
       align-items: flex-end;
       gap: 8px;
+
+      /* 停止生成:全局 .ant-btn-default 会覆盖 AntD danger 的颜色,这里显式恢复红色 */
+      .ant-btn-dangerous {
+        color: #dc2626;
+        border-color: #fecaca;
+        background: #fef2f2;
+
+        &:hover,
+        &:focus,
+        &:not(:disabled):hover,
+        &:not(:disabled):focus {
+          color: #dc2626;
+          border-color: #fca5a5;
+          background: #fee2e2;
+        }
+      }
     }
 
     &__input {
       flex: 1 1 auto;
     }
   }
+
+  @keyframes insight-running-pulse {
+    0%,
+    100% {
+      opacity: 1;
+      transform: scale(1);
+    }
+
+    50% {
+      opacity: 0.35;
+      transform: scale(0.72);
+    }
+  }
+
+  @keyframes insight-running-bounce {
+    0%,
+    80%,
+    100% {
+      opacity: 0.3;
+      transform: translateY(0);
+    }
+
+    40% {
+      opacity: 1;
+      transform: translateY(-3px);
+    }
+  }
 </style>
 
 <style lang="less">

+ 9 - 2
ai-electron/frontend/src/graph/insight/components/InsightReport.vue

@@ -1,7 +1,14 @@
 <template>
   <div class="insight-report">
-    <!-- 建会话 / 首轮未产出:骨架屏 -->
-    <div v-if="creating || (!payload && !contentText && streaming)" class="insight-report__skeleton">
+    <!-- 建会话 / 首轮未产出:骨架屏。
+         一旦已经有思考过程或工具调用(分析过程区已可见),骨架屏就让位,避免它一直挂在上面。 -->
+    <div
+      v-if="
+        creating ||
+        (!payload && !contentText && !message?.thinking && !message?.toolSteps?.length && streaming)
+      "
+      class="insight-report__skeleton"
+    >
       <div class="insight-report__skeleton-title">正在研判当前关系图谱…</div>
       <div v-for="i in 3" :key="i" class="insight-report__skeleton-line" :class="{ 'is-short': i === 3 }"></div>
       <div class="insight-report__skeleton-tip">

+ 10 - 3
ai-electron/frontend/src/graph/insight/hooks/useInsightStream.ts

@@ -9,7 +9,7 @@
  * 状态:idle → creating(建会话)→ streaming(首轮/追问)→ done
  *                     ↘ error(模型未配置 / 网络 / 超限 / 409 进行中)
  */
-import { computed, ref, type Ref } from 'vue';
+import { computed, reactive, ref, type Ref } from 'vue';
 import * as insightApi from '../api';
 import type { InsightContextVO, InsightCreateParams, InsightMessageVO, InsightSessionVO } from '../types';
 import type { ContentBlock } from '@/ai/utils/scanner';
@@ -209,7 +209,10 @@ export function useInsightStream(caseId: Ref<string>) {
       ];
     }
 
-    const target: InsightChatMessage = {
+    // 必须用 reactive 包装后再入列:onToken / onToolCall / onThinking 等回调直接改 target,
+    // 若持有的是原始对象,会绕过 Vue 的响应式代理 —— 流式期间界面不刷新,
+    // 只有 stop() 里 streaming ref 变化时才被动补一次渲染(表现为「停止后才看到分析过程」)。
+    const target = reactive<InsightChatMessage>({
       id: nextTempId(),
       sessionId: id,
       seq: Number.MAX_SAFE_INTEGER,
@@ -221,7 +224,7 @@ export function useInsightStream(caseId: Ref<string>) {
       blocks: [],
       toolSteps: [],
       createAt: new Date().toISOString(),
-    };
+    });
     messages.value = [...messages.value, target];
 
     streaming.value = true;
@@ -339,6 +342,10 @@ export function useInsightStream(caseId: Ref<string>) {
       page.value = 1;
       pages.value = 1;
       total.value = 0;
+      // 会话已经建好,后续是首轮流式分析:立即结束「创建中」态。
+      // 否则 start() 会一直 await 到整轮结束,骨架屏在整个分析期间都停在
+      // 「正在创建分析会话…」,即使下面已经开始输出内容。
+      creating.value = false;
       await runStream('');
     } catch (e: any) {
       error.value = e?.message || '创建研判会话失败';