fix(events): use provider index for recent receipts
Some checks failed
CD Pipeline / workflow-shape (push) Successful in 0s
CD Pipeline / cancel-stale-cd (push) Has been skipped
CD Pipeline / tests (push) Failing after 3m25s
CD Pipeline / build-and-deploy (push) Has been skipped
CD Pipeline / post-deploy-checks (push) Has been skipped

This commit is contained in:
ogt
2026-07-09 22:59:08 +08:00
parent c22530205e
commit 2a4110e981
2 changed files with 30 additions and 7 deletions

View File

@@ -7319,21 +7319,42 @@ async def list_recent_channel_events(
) -> dict[str, Any]:
"""列出最近 channel events供 Operator Console 顯示收斂/鏡像脈絡。"""
safe_limit = max(1, min(limit, _MAX_EVENTS))
recent_provider_prefix = (
provider_prefix.strip().lower() if provider_prefix else None
)
if not recent_provider_prefix:
recent_provider_prefix = None
async with get_db_context("awoooi") as db:
stmt = select(AwoooPConversationEvent).order_by(
AwoooPConversationEvent.received_at.desc()
)
stmt = select(AwoooPConversationEvent)
if project_id is not None:
stmt = stmt.where(AwoooPConversationEvent.project_id == project_id)
if channel_type is not None:
stmt = stmt.where(AwoooPConversationEvent.channel_type == channel_type)
if provider_prefix is not None:
stmt = stmt.where(
AwoooPConversationEvent.provider_event_id.like(
f"{provider_prefix}%"
if recent_provider_prefix is not None:
if ":" in recent_provider_prefix:
stmt = stmt.where(
AwoooPConversationEvent.provider_event_id.like(
f"{provider_prefix.strip()}%"
)
)
else:
stmt = stmt.where(
text(
"LOWER(COALESCE("
"NULLIF(source_envelope->>'provider', ''), "
"NULLIF(split_part(provider_event_id, ':', 1), ''), "
"channel_type"
")) = :recent_provider_prefix"
)
)
stmt = stmt.params(recent_provider_prefix=recent_provider_prefix)
stmt = stmt.order_by(
AwoooPConversationEvent.received_at.desc(),
AwoooPConversationEvent.event_id.desc(),
)
else:
stmt = stmt.order_by(AwoooPConversationEvent.received_at.desc())
result = await db.execute(stmt.limit(safe_limit))
rows = list(result.scalars().all())