perf: 加速语音转写缓存命中

迁移旧版语音转写缓存,并将补迁失败降级为一次性失败状态。

按账号和消息标识优先命中兼容缓存,未命中时才读取音频并计算哈希;导出缓存命中后合并异步刷新知识索引。

补充迁移、缓存快速路径、批量语音读取和导出流程测试。
This commit is contained in:
Nanin committed 2026-08-12 20:01:40 +08:00
1 parent 5018054cfd
commit c1224530c4
15 files changed
+1026 -165

No files matched your search

+70
View File
@@ -27,6 +27,13 @@ export type ResolveSourceResult =
| { success: true; source: EncodedVoiceSource }
| { success: false; error: string }
export interface VoiceReference {
sessionId: string
localId: number
createTime: number
svrId?: string | number
}
export class VoiceService {
private wcdb4Client: Wcdb4Client
private voiceCache = new Map<string, string>()
@@ -70,6 +77,69 @@ export class VoiceService {
return { success: true, data: base64Data }
}
async resolveVoices(
references: VoiceReference[]
): Promise<Array<{ success: boolean; data?: string; error?: string }>> {
const results: Array<{ success: boolean; data?: string; error?: string } | undefined> =
new Array(references.length)
const missing: Array<{ index: number; reference: VoiceReference }> = []
references.forEach((reference, index) => {
const cached = this.voiceCache.get(
this.buildCacheKey(reference.sessionId, reference.localId, reference.createTime)
)
if (cached) results[index] = { success: true, data: cached }
else missing.push({ index, reference })
})
if (!missing.length)
return results as Array<{ success: boolean; data?: string; error?: string }>
const sources = await this.wcdb4Client.getVoiceDataBatch(
missing.map(({ reference }) => ({
sessionId: reference.sessionId,
createTime: reference.createTime,
localId: reference.localId,
svrId: reference.svrId,
candidates: this.buildCandidates(reference.sessionId)
}))
)
for (const [{ index, reference }, source] of missing.map(
(item, sourceIndex) => [item, sources[sourceIndex]] as const
)) {
if (!source?.success || !source.hex) {
results[index] = { success: false, error: source?.error || '获取语音数据失败' }
continue
}
const silkData = this.decodeVoiceBlob(source.hex)
if (!silkData?.length) {
results[index] = { success: false, error: '语音数据为空' }
continue
}
try {
const decoded = await this.decoderRegistry.decode({
data: silkData,
codec: 'silk',
sourceHash: createHash('sha256').update(silkData).digest('hex')
})
const wavData = this.createWavBuffer(decoded.pcm, 24000)
const data = wavData.toString('base64')
const cacheKey = this.buildCacheKey(
reference.sessionId,
reference.localId,
reference.createTime
)
this.voiceCache.set(cacheKey, data)
this.pcmCache.set(cacheKey, { ...decoded, codec: 'silk' })
results[index] = { success: true, data }
} catch (error) {
results[index] = {
success: false,
error: error instanceof Error ? error.message : 'Silk 解码失败'
}
}
}
return results as Array<{ success: boolean; data?: string; error?: string }>
}
async resolvePcm(
sessionId: string,
localId: number,