Files
WechatExplorer/src/main/knowledge/normalizer.ts
T

62 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createHash } from 'crypto'
import type {
KnowledgeNormalizedMessage,
KnowledgeSourceMessage
} from '../../shared/knowledge'
const compact = (value: string | undefined): string => value?.replace(/\s+/g, ' ').trim() || ''
function digest(value: string): string {
return createHash('sha256').update(value).digest('hex')
}
/**
* Converts a read-only archive record into text safe for local search. Paths,
* binary media and raw voice data are deliberately excluded.
*/
export function normalizeKnowledgeMessage(
source: KnowledgeSourceMessage
): KnowledgeNormalizedMessage {
const sections: string[] = []
const messageText = compact(source.text)
if (messageText) sections.push(messageText)
const transcript = compact(source.voiceTranscript)
if (transcript) sections.push(`语音转写:${transcript}`)
// 图片 OCR 文本:与语音同样的"固定前缀"约定,让检索与展示都能识别这是派生内容。
const imageText = compact(source.imageOcrText)
if (imageText) sections.push(`图片文字:${imageText}`)
const attachmentName = compact(source.attachment?.name)
if (attachmentName) {
const label = source.attachment?.kind === 'link' ? '链接' : '附件'
sections.push(`${label}:${attachmentName}`)
}
const url = compact(source.attachment?.url)
if (url) sections.push(`地址:${url}`)
const searchableText = sections.join('\n')
return {
...source,
text: messageText || undefined,
voiceTranscript: transcript || undefined,
searchableText,
contentHash: digest(
JSON.stringify({
messageId: source.messageId,
createTime: source.createTime,
senderId: source.senderId || '',
kind: source.kind,
voiceTranscriptState: source.voiceTranscriptState || '',
imageOcrState: source.imageOcrState || '',
searchableText
})
)
}
}
export function isIndexableKnowledgeMessage(message: KnowledgeNormalizedMessage): boolean {
return Boolean(message.searchableText.trim())
}