feat: 增强 HomeScreen 录音交互与 ChatBloc 状态管理

- 新增录音启动延迟处理,解决权限未就绪时的竞态问题
- 实现历史分页滚动位置保持,提升加载体验
- 添加文本输入框点击键盘显示与焦点管理
- 优化 ChatBloc provider 到 MultiBlocProvider 支持
- 修复 ApiException 429 错误详情解析(支持 JSON 字符串 body)
- 改进 LocalNotificationService 精确闹钟权限请求
- 优化 UiSchemaRenderer GridView children 生成
- 支持导航 action 的 replace 参数
- 移除 Agent router 速率限制逻辑(_allow_run_request, _allow_transcribe_request)
- 补充相关单元测试与集成测试
This commit is contained in:
qzl
2026-03-18 17:03:22 +08:00
parent b34697660d
commit 8539f05a66
13 changed files with 578 additions and 143 deletions
+7 -39
View File
@@ -4,7 +4,6 @@ import asyncio
import os
import re
import tempfile
import time
from collections.abc import AsyncIterator
from datetime import date
from typing import Annotated, Union
@@ -46,8 +45,6 @@ from v1.users.dependencies import get_current_user
router = APIRouter(prefix="/agent", tags=["agent"])
logger = get_logger("v1.agent.router")
_LAST_EVENT_ID_RE = re.compile(r"^\d+-\d+$")
_RUNS_PER_MINUTE = 30
_TRANSCRIBES_PER_MINUTE = 20
_MAX_SSE_CONNECTIONS_PER_USER = 3
_SSE_SLOT_TTL_SECONDS = 15 * 60
_MAX_TRANSCRIBE_AUDIO_BYTES = 10 * 1024 * 1024
@@ -68,32 +65,6 @@ def _looks_like_wav_header(header: bytes) -> bool:
return header[0:4] == b"RIFF" and header[8:12] == b"WAVE"
async def _allow_run_request(*, user_id: str) -> bool:
try:
redis = await get_or_init_redis_client()
minute_bucket = int(time.time() // 60)
key = f"agent:run-rate:{user_id}:{minute_bucket}"
count = await redis.incr(key)
if count == 1:
await redis.expire(key, 70)
return int(count) <= _RUNS_PER_MINUTE
except Exception: # noqa: BLE001
return False
async def _allow_transcribe_request(*, user_id: str) -> bool:
try:
redis = await get_or_init_redis_client()
minute_bucket = int(time.time() // 60)
key = f"agent:transcribe-rate:{user_id}:{minute_bucket}"
count = await redis.incr(key)
if count == 1:
await redis.expire(key, 70)
return int(count) <= _TRANSCRIBES_PER_MINUTE
except Exception: # noqa: BLE001
return False
async def _acquire_sse_slot(*, user_id: str) -> bool:
try:
redis = await get_or_init_redis_client()
@@ -105,7 +76,12 @@ async def _acquire_sse_slot(*, user_id: str) -> bool:
await redis.decr(key)
return False
return True
except Exception: # noqa: BLE001
except Exception as exc: # noqa: BLE001
logger.warning(
"SSE slot acquire failed",
user_id=user_id,
reason=str(exc),
)
return False
@@ -136,10 +112,6 @@ async def enqueue_run(
validate_run_request_messages_contract(request)
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
allowed = await _allow_run_request(user_id=str(current_user.id))
if not allowed:
raise HTTPException(status_code=429, detail="Too many run requests")
task = await service.enqueue_run(
run_input=request,
current_user=current_user,
@@ -293,14 +265,10 @@ async def create_attachment_signed_url(
async def transcribe(
audio: UploadFile,
request: Request,
current_user: Annotated[CurrentUser, Depends(get_current_user)],
_current_user: Annotated[CurrentUser, Depends(get_current_user)],
) -> Union[AsrTranscribeResponse, JSONResponse]:
temp_path: str | None = None
try:
allowed = await _allow_transcribe_request(user_id=str(current_user.id))
if not allowed:
raise HTTPException(status_code=429, detail="Too many transcribe requests")
if audio.content_type not in _ALLOWED_AUDIO_CONTENT_TYPES:
raise ValueError("Unsupported audio format")
@@ -118,13 +118,6 @@ class _FailingStreamAgentService(_FakeAgentService):
def test_run_requires_auth_and_returns_202_task_id() -> None:
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
client = TestClient(app)
original_allow_run = agent_router._allow_run_request
async def _allow_run(*, user_id: str) -> bool:
del user_id
return True
agent_router._allow_run_request = _allow_run # type: ignore[assignment]
try:
unauthorized = client.post(
@@ -162,7 +155,6 @@ def test_run_requires_auth_and_returns_202_task_id() -> None:
assert authorized.json()["runId"] == "run-1"
assert authorized.json()["created"] is False
finally:
agent_router._allow_run_request = original_allow_run # type: ignore[assignment]
app.dependency_overrides = {}
@@ -410,12 +402,6 @@ def test_asr_transcribe_returns_sync_transcript(monkeypatch) -> None:
id=uuid4(), email="user@example.com"
)
async def _allow_transcribe(*, user_id: str) -> bool:
del user_id
return True
monkeypatch.setattr(agent_router, "_allow_transcribe_request", _allow_transcribe)
async def mock_transcribe_file(file_path: str, filename: str) -> str:
assert file_path.endswith(".wav")
assert filename == "test.wav"
@@ -453,12 +439,6 @@ def test_asr_transcribe_rejects_oversized_audio(monkeypatch) -> None:
monkeypatch.setattr(agent_router, "_MAX_TRANSCRIBE_AUDIO_BYTES", 4)
async def _allow_transcribe(*, user_id: str) -> bool:
del user_id
return True
monkeypatch.setattr(agent_router, "_allow_transcribe_request", _allow_transcribe)
client = TestClient(app)
oversized = BytesIO(b"12345")
oversized.name = "test.wav"
@@ -480,12 +460,6 @@ def test_asr_transcribe_rejects_non_wav_audio(monkeypatch) -> None:
id=uuid4(), email="user@example.com"
)
async def _allow_transcribe(*, user_id: str) -> bool:
del user_id
return True
monkeypatch.setattr(agent_router, "_allow_transcribe_request", _allow_transcribe)
client = TestClient(app)
fake_mp3 = BytesIO(b"fake-mp3")
fake_mp3.name = "test.mp3"
@@ -507,12 +481,6 @@ def test_asr_transcribe_rejects_invalid_wav_payload(monkeypatch) -> None:
id=uuid4(), email="user@example.com"
)
async def _allow_transcribe(*, user_id: str) -> bool:
del user_id
return True
monkeypatch.setattr(agent_router, "_allow_transcribe_request", _allow_transcribe)
client = TestClient(app)
fake_payload = BytesIO(b"not-a-wav")
fake_payload.name = "test.wav"
@@ -527,33 +495,3 @@ def test_asr_transcribe_rejects_invalid_wav_payload(monkeypatch) -> None:
assert response.json()["detail"] == "Unsupported audio format"
finally:
app.dependency_overrides = {}
def test_asr_transcribe_rejects_when_rate_limited_for_current_user(monkeypatch) -> None:
known_user = CurrentUser(id=uuid4(), email="user@example.com")
app.dependency_overrides[get_current_user] = lambda: known_user
captured_user_ids: list[str] = []
async def _deny_transcribe(*, user_id: str) -> bool:
captured_user_ids.append(user_id)
return False
monkeypatch.setattr(agent_router, "_allow_transcribe_request", _deny_transcribe)
client = TestClient(app)
wav_content = b"RIFF\x24\x80\x00\x00WAVEfmt "
wav_file = BytesIO(wav_content)
wav_file.name = "test.wav"
try:
response = client.post(
"/api/v1/agent/transcribe",
files={"audio": ("test.wav", wav_file, "audio/wav")},
)
assert response.status_code == 429
assert response.json()["detail"] == "Too many transcribe requests"
assert captured_user_ids == [str(known_user.id)]
finally:
app.dependency_overrides = {}