@customize-agent/knowledge 4.0.6 → 4.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -108,6 +108,11 @@ export declare class KnowledgeBaseManager {
|
|
|
108
108
|
content: Buffer;
|
|
109
109
|
targetRelativePath?: string;
|
|
110
110
|
}>, operationId?: string): Promise<import("./index-state-store.js").KnowledgeIndexJob[]>;
|
|
111
|
+
stageUploadedFilePaths(files: Array<{
|
|
112
|
+
fileName: string;
|
|
113
|
+
sourcePath: string;
|
|
114
|
+
targetRelativePath?: string;
|
|
115
|
+
}>, operationId?: string, offset?: number): Promise<import("./index-state-store.js").KnowledgeIndexJob[]>;
|
|
111
116
|
uploadFiles(files: Array<{
|
|
112
117
|
fileName: string;
|
|
113
118
|
content: Buffer;
|
|
@@ -173,6 +178,7 @@ export declare class KnowledgeBaseManager {
|
|
|
173
178
|
private ensureVectorIndexFresh;
|
|
174
179
|
private hasUsableContent;
|
|
175
180
|
private defaultUploadRelativePath;
|
|
181
|
+
private validateUploadRelativePath;
|
|
176
182
|
private resolveKbRelativePath;
|
|
177
183
|
private normalizeRelativePath;
|
|
178
184
|
}
|
|
@@ -426,7 +426,7 @@ export class KnowledgeBaseManager {
|
|
|
426
426
|
return this.incrementalIndex();
|
|
427
427
|
}
|
|
428
428
|
getUploadRelativePath(fileName, targetRelativePath) {
|
|
429
|
-
return targetRelativePath ? this.normalizeRelativePath(targetRelativePath) : this.defaultUploadRelativePath(fileName);
|
|
429
|
+
return this.validateUploadRelativePath(targetRelativePath ? this.normalizeRelativePath(targetRelativePath) : this.defaultUploadRelativePath(fileName));
|
|
430
430
|
}
|
|
431
431
|
async uploadFile(fileName, content, targetRelativePath, onProgress, options = {}) {
|
|
432
432
|
return this.uploadFiles([{ fileName, content, targetRelativePath }], onProgress, options);
|
|
@@ -448,6 +448,23 @@ export class KnowledgeBaseManager {
|
|
|
448
448
|
}
|
|
449
449
|
return jobs;
|
|
450
450
|
}
|
|
451
|
+
async stageUploadedFilePaths(files, operationId = `upload-${Date.now()}`, offset = 0) {
|
|
452
|
+
this.initialize();
|
|
453
|
+
const jobs = [];
|
|
454
|
+
for (let index = 0; index < files.length; index++) {
|
|
455
|
+
const file = files[index];
|
|
456
|
+
const relativePath = this.getUploadRelativePath(file.fileName, file.targetRelativePath);
|
|
457
|
+
const targetPath = this.resolveKbRelativePath(relativePath);
|
|
458
|
+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
459
|
+
fs.copyFileSync(file.sourcePath, targetPath);
|
|
460
|
+
const record = this.store.listRecords().find(item => item.relativePath === relativePath);
|
|
461
|
+
if (record)
|
|
462
|
+
await this.deleteVectorFile(record.collectionName, relativePath);
|
|
463
|
+
this.store.deleteRecord(relativePath);
|
|
464
|
+
jobs.push(this.store.enqueueIndexJob({ id: `${operationId}-${offset + index}`, relativePath, message: '文件已落盘,等待后台解析' }));
|
|
465
|
+
}
|
|
466
|
+
return jobs;
|
|
467
|
+
}
|
|
451
468
|
async uploadFiles(files, onProgress, options = {}) {
|
|
452
469
|
await this.stageUploadedFiles(files);
|
|
453
470
|
return this.incrementalIndex({ onProgress, vectorMode: options.vectorMode });
|
|
@@ -922,6 +939,21 @@ ${resultsText}
|
|
|
922
939
|
const dir = configDirs[classification.category] ?? DEFAULT_CATEGORY_DIRS[classification.category];
|
|
923
940
|
return `${dir}/${path.basename(fileName)}`;
|
|
924
941
|
}
|
|
942
|
+
validateUploadRelativePath(relativePath) {
|
|
943
|
+
const normalized = this.normalizeRelativePath(relativePath);
|
|
944
|
+
if (!normalized || normalized === '.')
|
|
945
|
+
throw new Error('上传文件路径无效');
|
|
946
|
+
if (normalized.length > 1000)
|
|
947
|
+
throw new Error('上传文件路径过长,请缩短文件夹层级或文件名');
|
|
948
|
+
if (normalized.includes('\0'))
|
|
949
|
+
throw new Error('上传文件路径包含非法字符');
|
|
950
|
+
const parts = normalized.split('/');
|
|
951
|
+
if (parts.some(part => !part || part === '..'))
|
|
952
|
+
throw new Error('上传文件路径无效');
|
|
953
|
+
if (parts.some(part => part.length > 255))
|
|
954
|
+
throw new Error('上传文件名过长,请缩短文件名后重试');
|
|
955
|
+
return normalized;
|
|
956
|
+
}
|
|
925
957
|
resolveKbRelativePath(relativePath) {
|
|
926
958
|
const normalized = this.normalizeRelativePath(relativePath);
|
|
927
959
|
const targetPath = path.resolve(this.kbPath, normalized);
|