feat(ai-chat): AI 导入确认流程完善并清理代码质量
- 新增导入确认/映射解析辅助,支持预检后生成导入向导 - 抽取 parseAttachmentArgs/beginImportToolRun 等重复逻辑 - 双重类型断言改为运行时守卫,消除 aislop 告警
This commit is contained in:
@@ -20,6 +20,8 @@ const ACCEPTED_MIME_TYPES = new Set([
|
||||
'image/png',
|
||||
'image/webp',
|
||||
'application/pdf',
|
||||
'text/csv',
|
||||
'application/csv',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
@@ -210,6 +212,9 @@ export class AiAttachmentService {
|
||||
const result = await mammoth.extractRawText({ buffer });
|
||||
return this.normalizeExtractedText(result.value);
|
||||
}
|
||||
if (mimeType.includes('csv')) {
|
||||
return this.normalizeExtractedText(buffer.toString('utf8').replace(/^\uFEFF/, ''));
|
||||
}
|
||||
if (mimeType.includes('spreadsheetml')) {
|
||||
return this.normalizeExtractedText(await this.excelReader.extractText(buffer));
|
||||
}
|
||||
@@ -235,6 +240,7 @@ export class AiAttachmentService {
|
||||
|
||||
private assertDeclaredType(declared: string, detected: string): void {
|
||||
if (!declared || declared === 'application/octet-stream') return;
|
||||
if (detected.includes('csv') || declared.includes('csv')) return;
|
||||
if (declared !== detected) throw new BadRequestException('附件类型与文件内容不一致');
|
||||
}
|
||||
|
||||
@@ -245,6 +251,8 @@ export class AiAttachmentService {
|
||||
'image/png': ['png'],
|
||||
'image/webp': ['webp'],
|
||||
'application/pdf': ['pdf'],
|
||||
'text/csv': ['csv'],
|
||||
'application/csv': ['csv'],
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': ['docx'],
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['xlsx'],
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': ['pptx'],
|
||||
@@ -278,6 +286,7 @@ export class AiAttachmentService {
|
||||
) {
|
||||
return declaredMimeType;
|
||||
}
|
||||
if (/csv/i.test(declaredMimeType)) return 'text/csv';
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
|
||||
@@ -305,6 +314,8 @@ export class AiAttachmentService {
|
||||
'image/png': 'png',
|
||||
'image/webp': 'webp',
|
||||
'application/pdf': 'pdf',
|
||||
'text/csv': 'csv',
|
||||
'application/csv': 'csv',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
|
||||
|
||||
Reference in New Issue
Block a user