2026-08-05 3ca1cefd9bed863131ee3f684ff127bd7ac877aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package cn.iocoder.yudao.module.ai.service.knowledge;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.*;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.segment.AiKnowledgeSegmentProcessRespVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeSegmentDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiModelDO;
import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeDocumentMapper;
import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeSegmentMapper;
import cn.iocoder.yudao.module.ai.service.model.AiModelService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.extractor.EmbeddedDocumentExtractor;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.xml.sax.ContentHandler;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;
 
@Slf4j
@Service
public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentService {
 
    @Resource
    private AiKnowledgeDocumentMapper documentMapper;
    @Resource
    private AiKnowledgeSegmentMapper segmentMapper;
    @Resource
    private AiKnowledgeService knowledgeService;
    @Resource
    private AiKnowledgeSegmentService segmentService;
    @Resource
    private AiModelService modelService;
    @Resource
    private cn.iocoder.yudao.module.system.service.storage.SystemStorageBlobService storageBlobService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Long> createDocuments(AiKnowledgeDocumentCreateReqVO createReqVO) {
        AiKnowledgeDO knowledge = knowledgeService.validateKnowledge(createReqVO.getKnowledgeId());
        List<AiKnowledgeDocumentCreateReqVO.DocumentItem> list = createReqVO.getList();
        if (CollUtil.isEmpty(list)) {
            throw new IllegalArgumentException("文档列表不能为空");
        }
        int defaultSegmentMaxTokens = createReqVO.getSegmentMaxTokens() != null
                ? createReqVO.getSegmentMaxTokens() : 800;
 
        List<Long> ids = new ArrayList<>();
        for (AiKnowledgeDocumentCreateReqVO.DocumentItem item : list) {
            AiKnowledgeDocumentCreateReqVO.UrlInfo urlInfo = item.getUrl();
            String fileUrl = urlInfo != null ? urlInfo.getUrl() : null;
            AiKnowledgeDocumentDO document = new AiKnowledgeDocumentDO();
            document.setKnowledgeId(knowledge.getId());
            document.setName(item.getName());
            document.setUrl(fileUrl);
            document.setStatus(CommonStatusEnum.ENABLE.getStatus());
            document.setSegmentMaxTokens(defaultSegmentMaxTokens);
            documentMapper.insert(document);
            ids.add(document.getId());
 
            if (fileUrl != null) {
                try {
                    loadDocumentContent(document);
                    documentMapper.updateById(document);
                    // 自动执行分段和向量化
                    processDocumentSegmentsInternal(document, knowledge);
                } catch (Exception e) {
                    log.error("文档[{}]处理失败: {}", document.getId(), e.getMessage());
                    document.setStatus(CommonStatusEnum.DISABLE.getStatus());
                    documentMapper.updateById(document);
                }
            }
        }
        return ids;
    }
 
    private void loadDocumentContent(AiKnowledgeDocumentDO document) {
        try {
            byte[] fileBytes = downloadFile(document.getUrl());
            String content = extractText(fileBytes, document.getName());
            if (StrUtil.isEmpty(content)) {
                content = new String(fileBytes, java.nio.charset.Charset.forName("UTF-8"));
            }
            document.setContent(content);
            document.setContentLength(content.length());
            document.setTokens(estimateTokens(content));
        } catch (Exception e) {
            throw new RuntimeException("文档内容加载失败: " + e.getMessage(), e);
        }
    }
 
    /**
     * 从 storage-blob URL 解析出 fileName 和 token
     */
    private static java.util.regex.Pattern STORAGE_URL_PATTERN =
            java.util.regex.Pattern.compile("/storage-blob/(?:preview|download)/([^?]+)\\?.*token=([^&]+)");
 
    private byte[] downloadFile(String fileUrl) {
        // 优先通过 storage-blob 本地读取,避免 HTTP 认证问题
        var matcher = STORAGE_URL_PATTERN.matcher(fileUrl);
        if (matcher.find()) {
            String fileName = matcher.group(1);
            String token = matcher.group(2);
            try {
                java.io.File file = storageBlobService.getFileByToken(fileName, token);
                return java.nio.file.Files.readAllBytes(file.toPath());
            } catch (Exception e) {
                log.warn("本地读取存储文件失败,回退 HTTP 下载: {}", e.getMessage());
            }
        }
        // 回退 HTTP 下载
        try {
            URL url = URL.of(new URI(encodeUrlPath(fileUrl)), null);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(60000);
            conn.setRequestMethod("GET");
            try (InputStream is = conn.getInputStream();
                 ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                IoUtil.copy(is, bos);
                return bos.toByteArray();
            }
        } catch (Exception e) {
            throw new RuntimeException("文件下载失败: " + e.getMessage(), e);
        }
    }
 
    private static String encodeUrlPath(String urlString) {
        // 分离 scheme://authority 和 path?query#fragment
        int schemeEnd = urlString.indexOf("://");
        if (schemeEnd < 0) return urlString;
        int pathStart = urlString.indexOf('/', schemeEnd + 3);
        if (pathStart < 0) return urlString; // 无 path
 
        String base = urlString.substring(0, pathStart);
        String pathAndRest = urlString.substring(pathStart);
 
        // 分离 path 和 query + fragment
        int queryStart = pathAndRest.indexOf('?');
        int fragStart = pathAndRest.indexOf('#');
        String rawPath, query, fragment;
        if (queryStart >= 0) {
            rawPath = pathAndRest.substring(0, queryStart);
            if (fragStart >= 0 && fragStart > queryStart) {
                query = pathAndRest.substring(queryStart, fragStart);
                fragment = pathAndRest.substring(fragStart);
            } else {
                query = pathAndRest.substring(queryStart);
                fragment = "";
            }
        } else if (fragStart >= 0) {
            rawPath = pathAndRest.substring(0, fragStart);
            query = "";
            fragment = pathAndRest.substring(fragStart);
        } else {
            rawPath = pathAndRest;
            query = "";
            fragment = "";
        }
 
        // 对路径每个段做 URL 编码
        StringBuilder encodedPath = new StringBuilder();
        for (String segment : rawPath.split("/")) {
            if (!segment.isEmpty()) {
                encodedPath.append("/").append(URLEncoder.encode(segment, StandardCharsets.UTF_8)
                        .replace("+", "%20"));
            }
        }
        if (rawPath.endsWith("/")) encodedPath.append("/");
 
        return base + encodedPath + query + fragment;
    }
 
    private String extractText(byte[] fileBytes, String fileName) {
        try {
            String ext = FileUtil.extName(fileName).toLowerCase();
            if (ArrayUtil.contains(new String[]{"txt", "md", "json", "xml", "csv", "yaml", "yml"}, ext)) {
                return new String(fileBytes, StandardCharsets.UTF_8);
            }
            // .docx 优先使用 POI 提取,中文支持更好
            if ("docx".equals(ext)) {
                try (XWPFDocument doc = new XWPFDocument(new java.io.ByteArrayInputStream(fileBytes));
                     XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
                    String text = extractor.getText();
                    if (StrUtil.isNotEmpty(text)) return text.trim();
                } catch (Exception e) {
                    log.warn("POI 解析 docx 失败,回退 Tika: {}", e.getMessage());
                }
            }
            // .doc 优先使用 POI 提取
            if ("doc".equals(ext)) {
                try (HWPFDocument doc = new HWPFDocument(new java.io.ByteArrayInputStream(fileBytes));
                     WordExtractor extractor = new WordExtractor(doc)) {
                    String text = extractor.getText();
                    if (StrUtil.isNotEmpty(text)) return text.trim();
                } catch (Exception e) {
                    log.warn("POI 解析 doc 失败,回退 Tika: {}", e.getMessage());
                }
            }
            // 其他格式或 POI 失败时回退 Tika
            return extractTextWithTika(fileBytes);
        } catch (Exception e) {
            log.warn("文档内容提取失败,尝试 UTF-8 文本读取: {}", e.getMessage());
            return "";
        }
    }
 
    private String extractTextWithTika(byte[] fileBytes) {
        try {
            Parser parser = new AutoDetectParser();
            ParseContext context = new ParseContext();
            context.set(EmbeddedDocumentExtractor.class, new EmbeddedDocumentExtractor() {
                @Override
                public boolean shouldParseEmbedded(org.apache.tika.metadata.Metadata metadata) {
                    return false;
                }
                @Override
                public void parseEmbedded(InputStream inputStream, ContentHandler contentHandler,
                                          org.apache.tika.metadata.Metadata metadata, boolean outputHtml) {
                }
            });
            BodyContentHandler handler = new BodyContentHandler(-1);
            try (InputStream is = new java.io.ByteArrayInputStream(fileBytes)) {
                parser.parse(is, handler, new org.apache.tika.metadata.Metadata(), context);
            }
            return handler.toString().trim();
        } catch (Exception e) {
            log.warn("Tika 解析失败: {}", e.getMessage());
            return "";
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDocument(AiKnowledgeDocumentUpdateReqVO updateReqVO) {
        AiKnowledgeDocumentDO document = validateDocumentExists(updateReqVO.getId());
        boolean urlChanged = StrUtil.isNotEmpty(updateReqVO.getUrl())
                && !updateReqVO.getUrl().equals(document.getUrl());
        if (StrUtil.isNotEmpty(updateReqVO.getName())) document.setName(updateReqVO.getName());
        if (urlChanged) document.setUrl(updateReqVO.getUrl());
        documentMapper.updateById(document);
        // URL 变更时重新提取内容、重新分段和向量化
        if (urlChanged) {
            try {
                loadDocumentContent(document);
                documentMapper.updateById(document);
                AiKnowledgeDO knowledge = knowledgeService.validateKnowledge(document.getKnowledgeId());
                // 删除旧分段和向量
                segmentService.deleteSegmentsByDocumentId(document.getId());
                // 重新分段 + 向量化
                processDocumentSegmentsInternal(document, knowledge);
            } catch (Exception e) {
                log.error("文档[{}] URL 更新后处理失败: {}", document.getId(), e.getMessage());
                document.setStatus(CommonStatusEnum.DISABLE.getStatus());
                documentMapper.updateById(document);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDocument(Long id) {
        AiKnowledgeDocumentDO document = validateDocumentExists(id);
        segmentService.deleteSegmentsByDocumentId(id);
        documentMapper.deleteById(id);
    }
 
    @Override
    public AiKnowledgeDocumentDO getDocument(Long id) {
        return documentMapper.selectById(id);
    }
 
    @Override
    public AiKnowledgeDocumentDO validateDocumentExists(Long id) {
        AiKnowledgeDocumentDO document = documentMapper.selectById(id);
        if (document == null) throw exception(KNOWLEDGE_DOCUMENT_NOT_EXISTS);
        return document;
    }
 
    @Override
    public PageResult<AiKnowledgeDocumentDO> getDocumentPage(AiKnowledgeDocumentPageReqVO pageReqVO) {
        return documentMapper.selectPage(pageReqVO);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDocumentStatus(AiKnowledgeDocumentUpdateStatusReqVO updateStatusReqVO) {
        AiKnowledgeDocumentDO document = validateDocumentExists(updateStatusReqVO.getId());
        document.setStatus(updateStatusReqVO.getStatus());
        documentMapper.updateById(document);
    }
 
    @Override
    public List<AiKnowledgeSegmentProcessRespVO> getDocumentProcessingProgress(List<Long> documentIds) {
        if (CollUtil.isEmpty(documentIds)) return Collections.emptyList();
        return segmentMapper.selectProcessList(documentIds);
    }
 
    @Override
    public void processDocumentSegments(Long documentId) {
        AiKnowledgeDocumentDO document = validateDocumentExists(documentId);
        if (StrUtil.isEmpty(document.getContent())) {
            throw exception(KNOWLEDGE_DOCUMENT_FILE_EMPTY);
        }
        AiKnowledgeDO knowledge = knowledgeService.validateKnowledge(document.getKnowledgeId());
        processDocumentSegmentsInternal(document, knowledge);
    }
 
    private void processDocumentSegmentsInternal(AiKnowledgeDocumentDO document, AiKnowledgeDO knowledge) {
        if (StrUtil.isEmpty(document.getContent())) return;
 
        AiModelDO embeddingModel = modelService.validateModel(knowledge.getEmbeddingModelId());
 
        // 删除旧分段
        segmentService.deleteSegmentsByDocumentId(document.getId());
 
        // 文本切片
        int segmentMaxTokens = document.getSegmentMaxTokens() != null ? document.getSegmentMaxTokens() : 800;
        List<String> segmentTexts = splitContent(document.getContent(), segmentMaxTokens);
        if (CollUtil.isEmpty(segmentTexts)) return;
 
        // 向量化并存储
        List<AiKnowledgeSegmentDO> segments = new ArrayList<>();
        for (String content : segmentTexts) {
            if (StrUtil.isEmpty(content.trim())) continue;
            segments.add(buildSegmentDO(knowledge.getId(), document.getId(), content));
        }
        segmentService.saveSegments(segments, knowledge.getId(), embeddingModel.getId());
    }
 
    private List<String> splitContent(String content, int maxTokens) {
        TokenTextSplitter splitter = TokenTextSplitter.builder()
                .withChunkSize(maxTokens)
                .withMinChunkSizeChars(50)
                .withMinChunkLengthToEmbed(10)
                .withMaxNumChunks(1000)
                .withKeepSeparator(true)
                .build();
        List<Document> docs = splitter.apply(Collections.singletonList(new Document(content)));
        List<String> result = new ArrayList<>();
        for (Document doc : docs) {
            if (StrUtil.isNotEmpty(doc.getText())) {
                result.add(doc.getText().trim());
            }
        }
        return result;
    }
 
    @Override
    public List<String> previewSplit(String url, String name, Integer segmentMaxTokens) {
        byte[] fileBytes = downloadFile(url);
        String text = extractText(fileBytes, name);
        if (StrUtil.isEmpty(text)) {
            text = new String(fileBytes, java.nio.charset.Charset.forName("UTF-8"));
        }
        int maxTokens = segmentMaxTokens != null && segmentMaxTokens > 0 ? segmentMaxTokens : 800;
        return splitContent(text, maxTokens);
    }
 
    private AiKnowledgeSegmentDO buildSegmentDO(Long knowledgeId, Long documentId, String content) {
        AiKnowledgeSegmentDO segment = new AiKnowledgeSegmentDO();
        segment.setKnowledgeId(knowledgeId);
        segment.setDocumentId(documentId);
        segment.setContent(content);
        segment.setContentLength(content.length());
        segment.setTokens(estimateTokens(content));
        segment.setStatus(CommonStatusEnum.ENABLE.getStatus());
        segment.setVectorId(AiKnowledgeSegmentDO.VECTOR_ID_EMPTY);
        segment.setRetrievalCount(0);
        return segment;
    }
 
    private Integer estimateTokens(String text) {
        if (StrUtil.isEmpty(text)) return 0;
        int chineseChars = 0, englishWords = 0;
        for (char c : text.toCharArray()) {
            if (c >= 0x4E00 && c <= 0x9FA5) chineseChars++;
        }
        for (String word : text.split("\\s+")) {
            if (word.matches(".*[a-zA-Z].*")) englishWords++;
        }
        return chineseChars + (int) (englishWords * 1.3);
    }
 
}