mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: chatd refactor (#26270)
Implements the chatd stabilization RFC. Combines: - https://github.com/coder/coder/pull/25908 - https://github.com/coder/coder/pull/25923 - https://github.com/coder/coder/pull/26109 - https://github.com/coder/coder/pull/26110 - https://github.com/coder/coder/pull/26111 - https://github.com/coder/coder/pull/26112
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package pubsub
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
// ChatStateUpdateChannel returns the pubsub channel that receives one
|
||||
// `chat:update:{chat_id}` message every time the chatstate state
|
||||
// machine commits a transition for the chat.
|
||||
func ChatStateUpdateChannel(chatID uuid.UUID) string {
|
||||
return fmt.Sprintf("chat:update:%s", chatID)
|
||||
}
|
||||
|
||||
// ChatStateOwnershipChannel is the global pubsub channel that
|
||||
// receives ownership hints when a chat is runnable but currently has
|
||||
// missing or stale ownership. Workers listen on this channel to know
|
||||
// when to attempt acquisition.
|
||||
const ChatStateOwnershipChannel = "chat:ownership"
|
||||
|
||||
// ChatStateUpdateMessage is the JSON payload published on
|
||||
// [ChatStateUpdateChannel] after every successful CreateChat or
|
||||
// ChatMachine.Update commit. It carries the committed post-transition
|
||||
// versions and ownership identifiers so stream loops and workers can
|
||||
// decide whether to refetch state.
|
||||
type ChatStateUpdateMessage struct {
|
||||
SnapshotVersion int64 `json:"snapshot_version"`
|
||||
WorkerID *uuid.UUID `json:"worker_id"`
|
||||
RunnerID *uuid.UUID `json:"runner_id"`
|
||||
HistoryVersion int64 `json:"history_version"`
|
||||
QueueVersion int64 `json:"queue_version"`
|
||||
RetryStateVersion int64 `json:"retry_state_version"`
|
||||
GenerationAttempt int64 `json:"generation_attempt"`
|
||||
Status string `json:"status"`
|
||||
Archived bool `json:"archived"`
|
||||
}
|
||||
|
||||
// ChatStateOwnershipMessage is the JSON payload published on
|
||||
// [ChatStateOwnershipChannel] when ownership is missing or stale for
|
||||
// a runnable chat. Subscribers should reload the chat row to confirm
|
||||
// ownership before acting.
|
||||
type ChatStateOwnershipMessage struct {
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
SnapshotVersion int64 `json:"snapshot_version"`
|
||||
}
|
||||
|
||||
// HandleChatStateUpdate wraps a typed callback for
|
||||
// [ChatStateUpdateMessage] consumption, following the same pattern as
|
||||
// HandleChatWatchEvent.
|
||||
func HandleChatStateUpdate(cb func(ctx context.Context, payload ChatStateUpdateMessage, err error)) func(ctx context.Context, message []byte, err error) {
|
||||
return func(ctx context.Context, message []byte, err error) {
|
||||
if err != nil {
|
||||
cb(ctx, ChatStateUpdateMessage{}, xerrors.Errorf("chat state update pubsub: %w", err))
|
||||
return
|
||||
}
|
||||
var payload ChatStateUpdateMessage
|
||||
if uerr := json.Unmarshal(message, &payload); uerr != nil {
|
||||
cb(ctx, ChatStateUpdateMessage{}, xerrors.Errorf("unmarshal chat state update: %w", uerr))
|
||||
return
|
||||
}
|
||||
cb(ctx, payload, err)
|
||||
}
|
||||
}
|
||||
|
||||
// HandleChatStateOwnership wraps a typed callback for
|
||||
// [ChatStateOwnershipMessage] consumption.
|
||||
func HandleChatStateOwnership(cb func(ctx context.Context, payload ChatStateOwnershipMessage, err error)) func(ctx context.Context, message []byte, err error) {
|
||||
return func(ctx context.Context, message []byte, err error) {
|
||||
if err != nil {
|
||||
cb(ctx, ChatStateOwnershipMessage{}, xerrors.Errorf("chat state ownership pubsub: %w", err))
|
||||
return
|
||||
}
|
||||
var payload ChatStateOwnershipMessage
|
||||
if uerr := json.Unmarshal(message, &payload); uerr != nil {
|
||||
cb(ctx, ChatStateOwnershipMessage{}, xerrors.Errorf("unmarshal chat state ownership: %w", uerr))
|
||||
return
|
||||
}
|
||||
cb(ctx, payload, err)
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
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"`
|
||||
}
|
||||
Reference in New Issue
Block a user