feat(memory,conversation): 记忆富化/证据包、时间线幂等字段与对话分段全链路

数据库
- 新增迁移 0003:timeline_events.memory_source_id 外键 → memory_sources,便于按 ingest 源做时间线幂等

后端 - 记忆
- 新增 ingest 后 LLM 富化(摘要/事实/时间线),可配置开关与最大字符数
- 新增证据包组装:合并 chunk、摘要、事实、时间线、故事等检索结果;支持空 query 时是否仍带 rolling 等开关
- repo/retriever/service/router/schemas/summarizer/timeline/extractor 等扩展;文档 memory-retrieval.md 更新

后端 - 对话 WS
- 增加 PING/PONG;分段 ASR 日志与空音频处理;转写失败与「无助手回复」错误提示更明确
- 助手多段回复持久化使用统一分隔符,与分段逻辑一致

后端 - Agent
- reply_limits:按 [SPLIT] 与段落拆段,并保证非空 fallback,供 WS 与 TTS 多段下发

后端 - 回忆录任务
- transcript ingest 记录 source_id;任务成功结?
This commit is contained in:
Kevin
2026-03-27 16:01:28 +08:00
parent 1374f6e8f5
commit e4bf0710c7
70 changed files with 3404 additions and 557 deletions

View File

@@ -38,7 +38,7 @@ class TencentASRProvider:
async def transcribe(self, audio: bytes, format: str = "m4a") -> str:
client = self._get_client()
if not client:
return ""
return "转写失败: 腾讯云 ASR 客户端未初始化(请检查密钥与依赖)"
try:
from tencentcloud.asr.v20190614 import models
@@ -46,12 +46,26 @@ class TencentASRProvider:
req = models.SentenceRecognitionRequest()
req.EngSerViceType = "16k_zh"
req.SourceType = 1
req.VoiceFormat = format
# 小写与文档一致。iOS 常见为 m4a(AAC) 容器,与 16k 引擎匹配
req.VoiceFormat = (format or "m4a").lower()
req.Data = audio_base64
req.DataLen = len(audio)
resp = client.SentenceRecognition(req)
return (resp.Result or "").strip()
text = (resp.Result or "").strip()
if text:
return text
err = getattr(resp, "Error", None) or getattr(resp, "Message", None)
logger.warning(
"Tencent ASR empty Result, audio_len={} format={} err={}",
len(audio),
req.VoiceFormat,
err,
)
return (
"转写失败: 腾讯云返回空文本(常见原因:采样率与 16k_zh 不匹配、"
"格式不受支持或音频无效;请确认客户端为 16kHz 单声道 m4a"
)
except Exception as e:
logger.error("Tencent ASR transcribe failed: {}", e)
return ""
logger.error("Tencent ASR transcribe failed: {}", e, exc_info=True)
return f"转写失败: {e}"[:500]

View File

@@ -1,11 +1,42 @@
"""Local faster-whisper ASR adapter — implements ASRProvider port."""
from app.core.logging import get_logger
from __future__ import annotations
import asyncio
import os
import re
import tempfile
from typing import Any, Iterable
from app.core.logging import get_logger
logger = get_logger(__name__)
_SUBTITLE_WATERMARK_RE = re.compile(
r"(字幕|听译|压制|字幕组).{0,20}(by|BY|By)|字幕\s*by",
re.UNICODE,
)
def _looks_like_subtitle_hallucination(text: str) -> bool:
"""静音时第二遍易吐出视频字幕水印;仅丢弃此类短句。"""
t = (text or "").strip()
if len(t) > 48:
return False
if _SUBTITLE_WATERMARK_RE.search(t):
return True
if len(t) <= 12 and "字幕" in t and not re.search(r"[?!。,、]", t):
return True
return False
def _join_segment_text(segments: Iterable[Any]) -> tuple[str, int]:
segs = list(segments)
return "".join(str(getattr(seg, "text", "") or "") for seg in segs).strip(), len(
segs
)
_DEFAULT_CACHE_DIR = os.path.normpath(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
@@ -70,30 +101,95 @@ class WhisperASRProvider:
return self._load_model()
async def transcribe(self, audio: bytes, format: str = "m4a") -> str:
# 与 v1.1.0 相同的单次 transcribe推理放线程池避免阻塞 asynciotag 上为同步调用)。
self._load_model()
if not self._model:
return ""
tmp_path = None
try:
with tempfile.NamedTemporaryFile(suffix=f".{format}", delete=False) as tmp:
tmp.write(audio)
tmp_path = tmp.name
model = self._model
segments, _info = self._model.transcribe(
tmp_path,
language="zh",
beam_size=5,
vad_filter=True,
vad_parameters={"min_silence_duration_ms": 500},
)
return "".join(seg.text for seg in segments).strip()
except Exception as e:
logger.error("Whisper transcribe failed: {}", e)
return ""
finally:
if tmp_path and os.path.exists(tmp_path):
try:
os.remove(tmp_path)
except OSError:
pass
def _sync_transcribe() -> str:
tmp_path = None
try:
with tempfile.NamedTemporaryFile(
suffix=f".{format}", delete=False
) as tmp:
tmp.write(audio)
tmp_path = tmp.name
segments, _info = model.transcribe(
tmp_path,
language="zh",
beam_size=5,
vad_filter=True,
vad_parameters={
"min_silence_duration_ms": 500,
"threshold": 0.35,
"min_speech_duration_ms": 200,
},
)
text, pass1_seg_count = _join_segment_text(segments)
used_second_pass = False
pass2_seg_count = 0
pass3_seg_count = 0
if not text:
logger.info(
"Whisper VAD pass 无文本,关闭 VAD 再试一次(短录音易被 VAD 判为静音)"
)
segments2, _info2 = model.transcribe(
tmp_path,
language="zh",
beam_size=5,
vad_filter=False,
condition_on_previous_text=False,
# 略抬高:减少边界片段被标成 no_speech 而整段为空
no_speech_threshold=0.85,
)
raw2, pass2_seg_count = _join_segment_text(segments2)
used_second_pass = True
if raw2 and _looks_like_subtitle_hallucination(raw2):
logger.info(
"Whisper 丢弃疑似字幕水印幻听: {!r}",
raw2[:120],
)
text = ""
else:
text = raw2
if not text and used_second_pass:
try:
from faster_whisper import decode_audio
audio_np = decode_audio(tmp_path, sampling_rate=16000)
segments3, _info3 = model.transcribe(
audio_np,
language="zh",
beam_size=5,
vad_filter=False,
condition_on_previous_text=False,
no_speech_threshold=0.85,
)
raw3, pass3_seg_count = _join_segment_text(segments3)
if raw3 and _looks_like_subtitle_hallucination(raw3):
logger.info(
"Whisper decode_audio 回退仍是疑似字幕水印幻听: {!r}",
raw3[:120],
)
elif raw3:
text = raw3
except Exception as ex:
logger.warning("Whisper decode_audio 回退失败: {}", ex)
return text
except Exception as e:
logger.error("Whisper transcribe failed: {}", e)
return ""
finally:
if tmp_path and os.path.exists(tmp_path):
try:
os.remove(tmp_path)
except OSError:
pass
return await asyncio.to_thread(_sync_transcribe)