fix(ai-connectors): deduplicate outage bridge DMs per conversation with a cooldown

Đang mở
#67 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, redis, typescript
Lĩnh vực
backend, databases

Hướng nghiên cứu

Start with apps/api/src/ai-connectors/ai-connectors.service.ts, especially AiConnectorsService and dispatchCompletion, then read apps/api/src/redis/redis.service.ts and the Message model in packages/prisma/schema.prisma. Check how Redis disconnection and existing constructor tests are handled. Done means both outage notifications are suppressed for 15 minutes per conversation, with a database fallback when Redis is unavailable.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

backend good first issue
Problem Description

In apps/api/src/ai-connectors/ai-connectors.service.ts (dispatchCompletion, lines 546-617), when all AI providers fail and conversationId is present, it unconditionally enqueues a SEND_DM BackgroundJob with bridgeMsg and creates an INTERNAL_NOTE ([AI Outage Alert]) on the conversation with no per-conversation cooldown.

If a customer sends multiple messages while AI providers are down, they receive duplicate bridgeMsg DMs and the conversation gets spammed with duplicate internal notes.


Expected Behavior
  1. During an AI outage, both bridgeMsg and the [AI Outage Alert] INTERNAL_NOTE are emitted at most once per conversation within a cooldown window (e.g., 15 minutes).
  2. Works reliably both locally (even without Redis) and in production across multiple API replicas, without breaking existing unit tests.

Where to Look
  • apps/api/src/ai-connectors/ai-connectors.service.ts (AiConnectorsService constructor line 33, dispatchCompletion lines 546-617)
  • apps/api/src/redis/redis.service.ts (reserveWebhookEvent lines 167-197)
  • packages/prisma/schema.prisma (Message model, uses sentAt)

Solution / Implementation Guide
  1. Inject @Optional() private readonly redisService?: RedisService into AiConnectorsService (RedisModule is @Global(), and @Optional() preserves existing unit test constructors).
  2. Add a cooldown helper in RedisService (e.g., acquireCooldown(key: string, ttlSeconds: number): Promise<boolean> using SET key '1' EX ttlSeconds NX, returning true if acquired or falling back safely if Redis is disconnected).
  3. Gate both bridgeMsg and INTERNAL_NOTE behind the 15-minute cooldown in dispatchCompletion:
    • If this.redisService is available: call await this.redisService.acquireCooldown(\ai:outage_bridge:${conversationId}`, 900)`.
    • DB fallback (if Redis is unavailable locally): query prisma.message.findFirst with where: { conversationId, messageType: 'INTERNAL_NOTE', content: { contains: 'AI Outage Alert' }, sentAt: { gte: new Date(Date.now() - 15 * 60 * 1000) } } (note: Message uses sentAt, not createdAt).
    • If the cooldown is active (already sent within 15 minutes), skip both enqueuing the SEND_DM bridgeMsg job and creating the INTERNAL_NOTE.
Ngôn ngữ chính
TypeScript
Star
2
Fork
1
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của sumamakhan761/tryinstance

Tất cả issue của sumamakhan761/tryinstance

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.