Files
coder/coderd/pubsub/chatstreamnotify.go
T
Ethan 4751416b29 fix!: persist structured chat errors (#24919)
**Breaking change for changelog:**

> `codersdk.Chat.last_error` now returns a structured `ChatError` object
(`{message, kind, provider, retryable, status_code, detail}`) instead of
a plain string. The chats API is experimental
(`/api/experimental/chats`), so this ships without a deprecation cycle;
consumers reading `chat.last_error` as a string must update to read
`chat.last_error.message`. SDK/generated TypeScript terminal error
payloads now use the single `ChatError` type; the live stream error
payload type is renamed from `ChatStreamError` to `ChatError`.

Persisted chat errors now carry the same provider-specific detail (kind,
provider, retryable, HTTP status, optional detail) as the live stream,
so refreshing a failed chat rehydrates with the full structured error
instead of a one-line headline.

Existing rows are migrated in place: legacy text errors are wrapped into
`{message, kind: "generic"}` so already-errored chats still render, and
rows with `last_error IS NULL` stay NULL. Internally, persisted fallback
decoding now reuses the existing `chaterror.KindGeneric` constant, with
no JSON value change.

Closes CODAGT-239
2026-05-05 12:56:06 +10:00

57 lines
2.0 KiB
Go

package pubsub
import (
"fmt"
"github.com/google/uuid"
"github.com/coder/coder/v2/codersdk"
)
// ChatStreamNotifyChannel returns the pubsub channel for per-chat
// stream notifications. Subscribers receive lightweight notifications
// and read actual content from the database.
func ChatStreamNotifyChannel(chatID uuid.UUID) string {
return fmt.Sprintf("chat:stream:%s", chatID)
}
// ChatStreamNotifyMessage is the payload published on the per-chat
// stream notification channel. Durable message content is still read
// from the database, while transient control events can be carried
// inline for cross-replica delivery.
type ChatStreamNotifyMessage struct {
// AfterMessageID tells subscribers to query messages after this
// ID. Set when a new message is persisted.
AfterMessageID int64 `json:"after_message_id,omitempty"`
// Status is set when the chat status changes. Subscribers use
// this to update clients and to manage relay lifecycle.
Status string `json:"status,omitempty"`
// WorkerID identifies which replica is running the chat. Used
// by enterprise relay to know where to connect.
WorkerID string `json:"worker_id,omitempty"`
// Retry carries a structured retry event for cross-replica live
// delivery. This is transient stream state and is not read back
// from the database.
Retry *codersdk.ChatStreamRetry `json:"retry,omitempty"`
// ErrorPayload carries a structured error event for cross-replica
// live delivery. Keep Error for backward compatibility with older
// replicas during rolling deploys.
ErrorPayload *codersdk.ChatError `json:"error_payload,omitempty"`
// Error is the legacy string-only error payload kept for mixed-
// version compatibility during rollout.
Error string `json:"error,omitempty"`
// QueueUpdate is set when the queued messages change.
QueueUpdate bool `json:"queue_update,omitempty"`
// FullRefresh signals that subscribers should re-fetch all
// messages from the beginning (e.g. after an edit that
// truncates message history).
FullRefresh bool `json:"full_refresh,omitempty"`
}