Преглед изворни кода

fix(agent): 修复 @ 引用面板与附件 chip 的交互问题

- chip 响应式:push 进 ref 数组的是原始对象,之后改 chip.uploading/meta 不触发渲染,
  表现为一直停在「解析中」、canSend 卡在 false;上传与 @ 引用都改为操作数组里的代理对象
- @ 面板初始高亮第一个可引用项(原来落在置灰行上,回车会把只含 @ 的草稿发出去);
  ↑↓ 跳过不可引用项、开口时回车不再放行发送;高亮行自动滚进可视区
- @ 面板加载失败优先展示后端 message(如「当前案件数据源未打开」),不再只给 axios 通用文案
- 未保留原件的原因文案与登记口径统一为「该格式不在支持清单内:仅登记文件信息,未保存源文件」,
  单测断言同步
cc пре 4 недеља
родитељ
комит
2fc263f9bf

+ 55 - 24
ai-electron/frontend/src/ai/components/ChatComposer.vue

@@ -18,7 +18,7 @@
       />
 
       <!-- @ 引用已上传文件:输入 @ 后的候选面板,选中即登记为附件 chip(走与上传相同的提取流程) -->
-      <div v-if="fileMenu.open" class="ai-composer__files">
+      <div v-if="fileMenu.open" ref="filePanelRef" class="ai-composer__files">
         <div class="ai-composer__files-head">
           <Icon icon="mdi:paperclip" :size="13" />
           <span>引用已上传文件</span>
@@ -225,27 +225,30 @@
       progress: 0,
       failed: false,
     };
+    // push 之后必须拿数组里的响应式代理再改:直接改原始对象不会触发渲染,
+    // 表现为 chip 一直停在「上传中」、canSend 一直为 false,直到别的状态变化才刷新
     attachments.value.push(chip);
+    const live = attachments.value[attachments.value.length - 1];
     try {
       const vo = await uploadAttachment(
         file,
         { workspaceId: String(userStore.getCaseInfo?.id ?? ''), sessionId: chatStore.activeSessionId ?? undefined },
         (p) => {
-          chip.progress = p;
+          live.progress = p;
         },
       );
       if (vo.status !== 'READY') {
         throw new Error('附件处理失败,请重试');
       }
-      chip.id = vo.id;
-      chip.vo = vo;
-      chip.uploading = false;
-      chip.meta = metaOf(vo);
-      chip.icon = iconFor(vo.name);
+      live.id = vo.id;
+      live.vo = vo;
+      live.uploading = false;
+      live.meta = metaOf(vo);
+      live.icon = iconFor(vo.name);
     } catch (err: any) {
-      chip.uploading = false;
-      chip.failed = true;
-      chip.meta = err?.response?.data?.message || err?.message || '上传失败,点击 × 移除';
+      live.uploading = false;
+      live.failed = true;
+      live.meta = err?.response?.data?.message || err?.message || '上传失败,点击 × 移除';
     }
   }
 
@@ -269,6 +272,7 @@
   const fileLoading = ref(false);
   const fileError = ref('');
   const composing = ref(false);
+  const filePanelRef = ref<HTMLElement>();
   /** 已为哪个案件拉过列表:切案件需要重新拉 */
   let fileLoadedForCase: string | null = null;
 
@@ -278,6 +282,12 @@
     return fileOptions.value.filter((f) => f.name.toLowerCase().includes(q));
   });
 
+  /** 默认高亮第一个可引用项:否则回车会落在「仅登记」的行上,把只含 @ 的草稿发出去 */
+  function firstPickableIndex(): number {
+    const idx = visibleFiles.value.findIndex((f) => f.pickable);
+    return idx < 0 ? 0 : idx;
+  }
+
   async function loadFiles() {
     const caseId = String(userStore.getCaseInfo?.id ?? '');
     if (!caseId) {
@@ -290,8 +300,12 @@
     try {
       fileOptions.value = (await listChatFiles()) || [];
       fileLoadedForCase = caseId;
+      if (fileMenu.value.open) {
+        fileMenu.value = { ...fileMenu.value, index: firstPickableIndex() };
+      }
     } catch (e: any) {
-      fileError.value = e?.message || '已上传文件加载失败';
+      // 后端 message 才是有用信息(如「当前案件数据源未打开,请重新打开案件」),axios 通用文案没有可操作性
+      fileError.value = e?.response?.data?.message || e?.message || '已上传文件加载失败';
     } finally {
       fileLoading.value = false;
     }
@@ -322,7 +336,7 @@
     const prev = at === 0 ? '' : before[at - 1];
     const query = before.slice(at + 1);
     if (!/[A-Za-z0-9._%+-]/.test(prev) && !/\s/.test(query) && query.length <= 60) {
-      fileMenu.value = { open: true, start: at, query, index: 0 };
+      fileMenu.value = { open: true, start: at, query, index: firstPickableIndex() };
       void loadFiles();
     } else {
       closeFileMenu();
@@ -333,10 +347,20 @@
     fileMenu.value = { ...fileMenu.value, index };
   }
 
+  /** 上下键只在「可引用」的行之间移动;列表可能很长,移动后把高亮行滚进可视区 */
   function moveFileActive(delta: number) {
-    const total = visibleFiles.value.length;
-    if (!total) return;
-    fileMenu.value = { ...fileMenu.value, index: (fileMenu.value.index + delta + total) % total };
+    const list = visibleFiles.value;
+    if (!list.length) return;
+    let next = fileMenu.value.index;
+    for (let step = 0; step < list.length; step++) {
+      next = (next + delta + list.length) % list.length;
+      if (list[next].pickable) break;
+    }
+    fileMenu.value = { ...fileMenu.value, index: next };
+    nextTick(() => {
+      const el = filePanelRef.value?.querySelector<HTMLElement>('.ai-file.is-active');
+      el?.scrollIntoView({ block: 'nearest' });
+    });
   }
 
   /**
@@ -371,7 +395,9 @@
       progress: 0,
       failed: false,
     };
+    // 同上:必须改响应式代理,否则 chip 停在「解析中」且发送按钮保持禁用
     attachments.value.push(chip);
+    const live = attachments.value[attachments.value.length - 1];
     try {
       const vo = await attachExistingFile({
         fileId: option.id,
@@ -381,15 +407,15 @@
       if (vo.status !== 'READY') {
         throw new Error('附件处理失败,请重试');
       }
-      chip.id = vo.id;
-      chip.vo = vo;
-      chip.uploading = false;
-      chip.meta = metaOf(vo);
-      chip.icon = iconFor(vo.name);
+      live.id = vo.id;
+      live.vo = vo;
+      live.uploading = false;
+      live.meta = metaOf(vo);
+      live.icon = iconFor(vo.name);
     } catch (err: any) {
-      chip.uploading = false;
-      chip.failed = true;
-      chip.meta = err?.response?.data?.message || err?.message || '引用失败,点击 × 移除';
+      live.uploading = false;
+      live.failed = true;
+      live.meta = err?.response?.data?.message || err?.message || '引用失败,点击 × 移除';
     }
   }
 
@@ -498,12 +524,17 @@
       }
       if (e.key === 'Enter' && !e.isComposing && e.keyCode !== 229) {
         const option = visibleFiles.value[fileMenu.value.index];
-        // 高亮项不可引用时不吞回车,让用户照常发送
         if (option?.pickable) {
           e.preventDefault();
           void pickFileOption(option);
           return;
         }
+        // 高亮的是「仅登记」的行时吞掉回车:否则会把只含 @ 的草稿当消息发出去。
+        // 列表里一个可引用项都没有时,回车仍然按正常发送处理。
+        if (visibleFiles.value.some((f) => f.pickable)) {
+          e.preventDefault();
+          return;
+        }
       }
     }
     if (e.key !== 'Enter') return;

+ 3 - 2
ai-server/src/main/java/com/zsjz/ai/module/agent/attachment/ChatAttachmentService.java

@@ -214,7 +214,8 @@ public class ChatAttachmentService {
         long size = row.getFileSize() == null ? 0 : row.getFileSize();
         String reason;
         if (StrUtil.isBlank(row.getFilePath())) {
-            reason = "未保留原件(仅表格类文件上传时落盘)";
+            // 与 DmService 的登记口径一致:压缩包等只登记、没有源文件
+            reason = "该格式不在支持清单内:仅登记文件信息,未保存源文件";
         } else if (!props.effectiveSuffixes().contains(AttachmentContentRouter.suffixOf(name))) {
             reason = "该类型不能在对话中引用";
         } else {
@@ -227,7 +228,7 @@ public class ChatAttachmentService {
                     reason = null;
                 }
             } catch (Exception e) {
-                reason = "原件不可读或已被清理";
+                reason = "原件已被清理或不可读";
             }
         }
         return new ChatFileVO(String.valueOf(row.getId()), name, type, size,

+ 1 - 1
ai-server/src/test/java/com/zsjz/ai/module/agent/attachment/ChatAttachmentServiceTest.java

@@ -217,7 +217,7 @@ class ChatAttachmentServiceTest {
         assertEquals("100", files.get(0).id());
         assertEquals(Files.size(source), files.get(0).size(), "大小以磁盘原件为准");
         assertFalse(files.get(1).pickable());
-        assertTrue(files.get(1).reason().contains("未保留原件"), files.get(1).reason());
+        assertTrue(files.get(1).reason().contains("未保存源文件"), files.get(1).reason());
 
         FileUtil.del(PathConst.WORKSPACE.resolve(String.valueOf(caseId)).toFile());
     }