- main.ts: window.__metonaBridge → window.metonaDesktop(与 preload.ts 对齐) - 删除 DesktopBridge 接口和 __metonaBridge 类型(从未使用) - 删除 extractDocument(定义了但无调用) - 删除 logIPC(导出了但零引用) - 删除 getMessagesContainer(导出了但无调用) - SAFE_DATA_TYPES 去掉多余的 export - vite.config.ts 移除未使用的 server 配置
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
/**
|
|
* DocumentProcessor - 文档分块处理
|
|
*/
|
|
|
|
export function chunkText(text: string, { maxChunkSize = 1500, overlap = 200 } = {}): string[] {
|
|
if (!text || text.trim().length === 0) return [];
|
|
|
|
const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim());
|
|
const chunks: string[] = [];
|
|
let current = '';
|
|
|
|
for (const para of paragraphs) {
|
|
const trimmed = para.trim();
|
|
if (!trimmed) continue;
|
|
|
|
if (trimmed.length > maxChunkSize) {
|
|
if (current) {
|
|
chunks.push(current.trim());
|
|
current = '';
|
|
}
|
|
const sentences = splitSentences(trimmed);
|
|
let sentenceBuf = '';
|
|
for (const sent of sentences) {
|
|
if ((sentenceBuf + sent).length > maxChunkSize && sentenceBuf) {
|
|
chunks.push(sentenceBuf.trim());
|
|
sentenceBuf = overlap > 0 ? sentenceBuf.slice(-overlap) + sent : sent;
|
|
} else {
|
|
sentenceBuf += sent;
|
|
}
|
|
}
|
|
if (sentenceBuf.trim()) {
|
|
current = sentenceBuf;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if ((current + '\n\n' + trimmed).length > maxChunkSize && current) {
|
|
chunks.push(current.trim());
|
|
if (overlap > 0 && current.length > overlap) {
|
|
current = current.slice(-overlap) + '\n\n' + trimmed;
|
|
} else {
|
|
current = trimmed;
|
|
}
|
|
} else {
|
|
current = current ? current + '\n\n' + trimmed : trimmed;
|
|
}
|
|
}
|
|
|
|
if (current.trim()) {
|
|
chunks.push(current.trim());
|
|
}
|
|
|
|
return chunks;
|
|
}
|
|
|
|
function splitSentences(text: string): string[] {
|
|
const parts = text.split(/(?<=[。!?.!?])\s*/);
|
|
return parts.filter(p => p.trim()).map(p => p.trim() + ' ');
|
|
}
|
|
|
|
export function createChunkMetadata(chunk: string, index: number, docId: string, filename: string) {
|
|
return {
|
|
id: `${docId}_chunk_${index}`,
|
|
docId,
|
|
filename,
|
|
chunkIndex: index,
|
|
text: chunk,
|
|
charCount: chunk.length
|
|
};
|
|
}
|