publishChatPubsubEvent broadcasts a chat lifecycle event via PostgreSQL pubsub so that all replicas can push updates to watching clients.
(chat database.Chat, kind codersdk.ChatWatchEventKind, diffStatus *codersdk.ChatDiffStatus)
| 5516 | // publishChatPubsubEvent broadcasts a chat lifecycle event via PostgreSQL |
| 5517 | // pubsub so that all replicas can push updates to watching clients. |
| 5518 | func (p *Server) publishChatPubsubEvent(chat database.Chat, kind codersdk.ChatWatchEventKind, diffStatus *codersdk.ChatDiffStatus) { |
| 5519 | if p.pubsub == nil { |
| 5520 | return |
| 5521 | } |
| 5522 | // diffStatus is applied below. File metadata is intentionally |
| 5523 | // omitted from pubsub events to avoid an extra DB query per |
| 5524 | // publish. Clients must merge pubsub updates, not replace |
| 5525 | // cached file metadata. |
| 5526 | sdkChat := db2sdk.Chat(chat, nil, nil) |
| 5527 | if diffStatus != nil { |
| 5528 | sdkChat.DiffStatus = diffStatus |
| 5529 | } |
| 5530 | event := codersdk.ChatWatchEvent{ |
| 5531 | Kind: kind, |
| 5532 | Chat: sdkChat, |
| 5533 | } |
| 5534 | payload, err := json.Marshal(event) |
| 5535 | if err != nil { |
| 5536 | p.logger.Error(context.Background(), "failed to marshal chat pubsub event", |
| 5537 | slog.F("chat_id", chat.ID), |
| 5538 | slog.Error(err), |
| 5539 | ) |
| 5540 | return |
| 5541 | } |
| 5542 | if err := p.pubsub.Publish(coderdpubsub.ChatWatchEventChannel(chat.OwnerID), payload); err != nil { |
| 5543 | p.logger.Error(context.Background(), "failed to publish chat pubsub event", |
| 5544 | slog.F("chat_id", chat.ID), |
| 5545 | slog.F("kind", kind), |
| 5546 | slog.Error(err), |
| 5547 | ) |
| 5548 | } |
| 5549 | } |
| 5550 | |
| 5551 | // pendingToStreamToolCalls converts a slice of chatloop pending |
| 5552 | // tool calls into the SDK streaming representation. |
no test coverage detected