A client could resume the chat stream only with after_id, a message ID. A message ID does not identify what a client has received. A reconnect could skip a message that another client sent, send no history_reset for an edit the client missed, or resend the whole history. A client can now resume with the chat's snapshot_version, which the newest messages page returns and the stream's events then include. The stream sends what changed at that version or later. For an edit it sends the ID of the edited message and the messages written since, not the whole history. A stream opened without snapshot_version sends the same events as before. A write outside a chat state transition does not increase snapshot_version, and a stream can miss it. The queries that only tests used for such writes are removed. When the stream cannot be set up, the server now returns 500 before the WebSocket upgrade. It used to upgrade and send an error event, which the web client shows as a chat error. The Tasks to Chats migration guide told clients to reconnect with after_id. It now describes snapshot_version.
2952 lines
99 KiB
Go
2952 lines
99 KiB
Go
package chatd
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/dustin/go-humanize"
|
|
"github.com/google/uuid"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/sqlc-dev/pqtype"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"golang.org/x/sync/errgroup"
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/aibridge"
|
|
"github.com/coder/coder/v2/coderd/audit"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/database/pubsub"
|
|
"github.com/coder/coder/v2/coderd/httpmw"
|
|
"github.com/coder/coder/v2/coderd/notifications"
|
|
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
"github.com/coder/coder/v2/coderd/util/ptr"
|
|
"github.com/coder/coder/v2/coderd/webpush"
|
|
"github.com/coder/coder/v2/coderd/workspacestats"
|
|
"github.com/coder/coder/v2/coderd/x/agenthooks/dispatch"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatadvisor"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatdebug"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chaterror"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chathooks"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/mcpclient"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/messagepartbuffer"
|
|
"github.com/coder/coder/v2/coderd/x/chatfiles"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
"github.com/coder/coder/v2/codersdk/x/agenthooks"
|
|
"github.com/coder/quartz"
|
|
)
|
|
|
|
const (
|
|
// DefaultPendingChatAcquireInterval is the default time between attempts to
|
|
// acquire pending chats.
|
|
DefaultPendingChatAcquireInterval = 30 * time.Second
|
|
// DefaultInFlightChatStaleAfter is the default age after which a running
|
|
// chat is considered stale and should be recovered.
|
|
DefaultInFlightChatStaleAfter = 30 * time.Second
|
|
|
|
workspaceDialValidationDelay = 5 * time.Second
|
|
turnStatusLabelWriteTimeout = 5 * time.Second
|
|
// defaultDialTimeout matches the timeout used by ~8 other
|
|
// server-side AgentConn callers.
|
|
defaultDialTimeout = 30 * time.Second
|
|
// planPathLookupTimeout bounds resolving the per-chat plan path, which
|
|
// dials the workspace agent to read its home directory. It must exceed
|
|
// defaultDialTimeout so a cold dial, bounded internally by that timeout,
|
|
// can finish before this outer budget fires, with a small margin for the
|
|
// follow-up LS call.
|
|
planPathLookupTimeout = defaultDialTimeout + 5*time.Second
|
|
// DefaultChatHeartbeatInterval is the default time between chat
|
|
// heartbeat updates while a chat is being processed.
|
|
DefaultChatHeartbeatInterval = 9 * time.Second
|
|
|
|
// slowPrepareThreshold is the generation-preparation duration
|
|
// above which a warning is logged. Preparation runs before
|
|
// every generation step, so sustained slowness (workspace
|
|
// dials, MCP connects) taxes the whole turn.
|
|
slowPrepareThreshold = 30 * time.Second
|
|
|
|
// agentDisconnectedRecoveryThreshold is how long the latest
|
|
// workspace agent must be disconnected before chatd suggests
|
|
// destructive stop/start recovery. This is intentionally longer
|
|
// than the inactive-disconnect timeout so short heartbeat gaps do
|
|
// not prompt a workspace restart.
|
|
agentDisconnectedRecoveryThreshold = 90 * time.Second
|
|
|
|
// DefaultMaxChatsPerAcquire is the maximum number of chats to
|
|
// acquire in a single processOnce call. Batching avoids
|
|
// waiting a full polling interval between acquisitions
|
|
// when many chats are pending.
|
|
DefaultMaxChatsPerAcquire int32 = 10
|
|
|
|
defaultSubagentInstruction = "You are running as a delegated sub-agent chat. Complete the delegated task and provide clear, concise assistant responses for the parent agent."
|
|
|
|
// defaultAdvisorMaxOutputTokens caps the nested advisor response
|
|
// when the admin config omits the field (or sets it to <= 0).
|
|
// It is intentionally generous relative to the advisor's concise
|
|
// guidance remit so short plans are not truncated mid-reasoning.
|
|
defaultAdvisorMaxOutputTokens = 16384
|
|
)
|
|
|
|
var (
|
|
errChatHasNoWorkspace = xerrors.New("this tool requires a workspace and this chat does not have one. Use the create_workspace tool to create one")
|
|
errChatHasNoWorkspaceAgent = xerrors.New("workspace has no running agent: the workspace is likely stopped. Use the start_workspace tool to start it")
|
|
errChatWorkspaceDeleted = xerrors.New("the chat's workspace was deleted (for example by dormancy cleanup) and cannot execute tools. Use the create_workspace tool to create a new one")
|
|
errChatAgentDisconnected = xerrors.New(
|
|
"workspace agent has been disconnected for at least 90 seconds " +
|
|
"and cannot execute tools. To recover, call stop_workspace " +
|
|
"to stop the workspace, then start_workspace to start it " +
|
|
"again",
|
|
)
|
|
errChatAgentNeverConnected = xerrors.New(
|
|
"workspace agent never connected and its connection timeout has " +
|
|
"elapsed, so it cannot execute tools. To recover, call " +
|
|
"stop_workspace to stop the workspace, then start_workspace " +
|
|
"to start it again",
|
|
)
|
|
errChatDialTimeout = xerrors.New(
|
|
"connection to the workspace agent timed out. " +
|
|
"The agent may still be reachable on the next attempt.",
|
|
)
|
|
errChatExternalAgentUnavailable = xerrors.New("external workspace agent unavailable")
|
|
errInflightClosed = xerrors.New("chatd server inflight closed")
|
|
)
|
|
|
|
type chatExternalAgentUnavailableError struct {
|
|
message string
|
|
}
|
|
|
|
func (e chatExternalAgentUnavailableError) Error() string {
|
|
return e.message
|
|
}
|
|
|
|
func (chatExternalAgentUnavailableError) Is(target error) bool {
|
|
return target == errChatExternalAgentUnavailable
|
|
}
|
|
|
|
func newChatExternalAgentUnavailableError(agent database.WorkspaceAgent) error {
|
|
return chatExternalAgentUnavailableError{
|
|
message: chattool.ExternalAgentUnavailableMessage(agent),
|
|
}
|
|
}
|
|
|
|
// Server handles background processing of pending chats.
|
|
type Server struct {
|
|
cancel context.CancelFunc
|
|
ctx context.Context
|
|
wg sync.WaitGroup
|
|
inflight sync.WaitGroup
|
|
inflightMu sync.Mutex
|
|
inflightClosed atomic.Bool
|
|
|
|
db database.Store
|
|
logger slog.Logger
|
|
modelConfigContext func(context.Context, uuid.UUID) (context.Context, error)
|
|
|
|
streamPartsDialer StreamPartsDialer
|
|
|
|
agentConnFn AgentConnFunc
|
|
agentInactiveDisconnectTimeout time.Duration
|
|
dialTimeout time.Duration
|
|
createWorkspaceFn chattool.CreateWorkspaceFn
|
|
startWorkspaceFn chattool.StartWorkspaceFn
|
|
stopWorkspaceFn chattool.StopWorkspaceFn
|
|
renderTemplateParametersFn chattool.RenderTemplateParametersFn
|
|
pubsub pubsub.Pubsub
|
|
webpushDispatcher webpush.Dispatcher
|
|
hooks *chathooks.Trigger
|
|
providerAPIKeys chatprovider.ProviderAPIKeys
|
|
allowBYOK bool
|
|
disableCallerSuppliedTools bool
|
|
oidcTokenSource mcpclient.UserOIDCTokenSource
|
|
mcpHTTPClient *http.Client
|
|
internalMCPServers []InternalMCPServer
|
|
debugSvc *chatdebug.Service
|
|
debugSvcFactory func() *chatdebug.Service
|
|
debugSvcReady atomic.Bool
|
|
debugSvcInit sync.Once
|
|
configCache *chatConfigCache
|
|
configCacheUnsubscribe func()
|
|
providerCacheUnsubscribe func()
|
|
|
|
usageTracker *workspacestats.UsageTracker
|
|
clock quartz.Clock
|
|
metrics *chatloop.Metrics
|
|
stages *chatloop.StageTracer
|
|
chatWorker *chatWorker
|
|
messagePartBuffer *messagepartbuffer.Buffer
|
|
streamSyncPoller *streamSyncPoller
|
|
recordingSem chan struct{}
|
|
agentCapacityLimiter AgentCapacityLimiter
|
|
|
|
aibridgeTransportFactory *atomic.Pointer[aibridge.TransportFactory]
|
|
experiments codersdk.Experiments
|
|
|
|
// thinkingDropBlock holds the thinkingDropBlockKey of each provider and
|
|
// model that accepted Anthropic's thinking drop_block control.
|
|
thinkingDropBlock sync.Map
|
|
// quickgenParamRejections records when each provider and model last
|
|
// rejected a quickgen parameter. See quickgenParamModel.
|
|
quickgenParamRejections sync.Map
|
|
|
|
// Configuration
|
|
inFlightChatStaleAfter time.Duration
|
|
streamSilenceTimeout time.Duration
|
|
chatLimits Limits
|
|
}
|
|
|
|
// stripAdvisorGuidanceBlock removes any system message whose text content
|
|
// matches chatadvisor.ParentGuidanceBlock after whitespace normalization.
|
|
// The block is meant for the parent agent (it advertises the advisor tool)
|
|
// and would waste context tokens if forwarded to the advisor's nested run.
|
|
func stripAdvisorGuidanceBlock(msgs []fantasy.Message) []fantasy.Message {
|
|
filtered := msgs[:0]
|
|
for _, msg := range msgs {
|
|
if msg.Role == fantasy.MessageRoleSystem && isAdvisorGuidanceMessage(msg) {
|
|
continue
|
|
}
|
|
filtered = append(filtered, msg)
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
func isAdvisorGuidanceMessage(msg fantasy.Message) bool {
|
|
if len(msg.Content) != 1 {
|
|
return false
|
|
}
|
|
text, ok := msg.Content[0].(fantasy.TextPart)
|
|
if !ok {
|
|
return false
|
|
}
|
|
return strings.TrimSpace(text.Text) == strings.TrimSpace(chatadvisor.ParentGuidanceBlock)
|
|
}
|
|
|
|
func (p *Server) resolveAdvisorModelOverride(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
maxOutputTokens int64,
|
|
modelOpts modelBuildOptions,
|
|
logger slog.Logger,
|
|
) (resolvedModelCall, bool, error) {
|
|
override, err := p.resolveModelOverride(ctx, modelOverrideSpec{
|
|
context: advisorOverrideContext,
|
|
ownerID: chat.OwnerID,
|
|
organizationID: chat.OrganizationID,
|
|
failIfNoProviderKey: true,
|
|
})
|
|
if err != nil {
|
|
return resolvedModelCall{}, false, xerrors.Errorf("resolve advisor override model: %w", err)
|
|
}
|
|
if !override.Set {
|
|
return resolvedModelCall{}, false, nil
|
|
}
|
|
|
|
resolved, err := p.resolveModelCall(ctx, modelCallSpec{
|
|
purpose: "advisor",
|
|
chat: chat,
|
|
explicitConfig: &override.Config,
|
|
requestedEffort: override.ReasoningEffort,
|
|
maxOutputTokens: ptr.Ref(maxOutputTokens),
|
|
buildOptions: modelOpts,
|
|
})
|
|
if err != nil {
|
|
var parseErr modelCallConfigParseError
|
|
if override.Config.AIProviderID.Valid && !xerrors.As(err, &parseErr) {
|
|
return resolvedModelCall{}, false, xerrors.Errorf("resolve advisor override model: %w", err)
|
|
}
|
|
logger.Warn(ctx,
|
|
"failed to resolve advisor override model, continuing with chat model",
|
|
slog.F("model_config_id", override.Config.ID),
|
|
slog.Error(err),
|
|
)
|
|
return resolvedModelCall{}, false, nil
|
|
}
|
|
return resolved, true, nil
|
|
}
|
|
|
|
func (p *Server) newAdvisorRuntime(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
advisorCfg advisorRuntimeConfig,
|
|
priorUses int,
|
|
modelOpts modelBuildOptions,
|
|
logger slog.Logger,
|
|
) (*chatadvisor.Runtime, error) {
|
|
maxUsesPerRun := advisorCfg.MaxUsesPerRun
|
|
switch {
|
|
case maxUsesPerRun == 0:
|
|
// Advisor config treats 0 as unlimited, but the runtime
|
|
// requires a positive bound. The per-turn step limit is the
|
|
// effective upper bound because advisor can run at most
|
|
// once per loop step.
|
|
maxUsesPerRun = p.chatLimits.MaxStepsPerTurn
|
|
case maxUsesPerRun < 0:
|
|
logger.Warn(
|
|
ctx,
|
|
"invalid advisor max uses per run, continuing without advisor",
|
|
slog.F("max_uses_per_run", maxUsesPerRun),
|
|
)
|
|
return nil, nil //nolint:nilnil // Nil runtime with nil error means advisor is skipped for this turn.
|
|
}
|
|
|
|
maxOutputTokens := advisorCfg.MaxOutputTokens
|
|
if maxOutputTokens <= 0 {
|
|
maxOutputTokens = defaultAdvisorMaxOutputTokens
|
|
}
|
|
|
|
advisor, ok, err := p.resolveAdvisorModelOverride(
|
|
ctx,
|
|
chat,
|
|
maxOutputTokens,
|
|
modelOpts,
|
|
logger,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
// Without a usable override the advisor runs on the chat model,
|
|
// resolved with the advisor's output cap and the config's default
|
|
// reasoning effort. The configured advisor effort applies only to
|
|
// the override model it was tuned for.
|
|
advisor, err = p.resolveModelCall(ctx, modelCallSpec{
|
|
purpose: "advisor",
|
|
chat: chat,
|
|
maxOutputTokens: &maxOutputTokens,
|
|
buildOptions: modelOpts,
|
|
})
|
|
if err != nil {
|
|
logger.Warn(
|
|
ctx,
|
|
"failed to resolve advisor chat model, continuing without advisor",
|
|
slog.Error(err),
|
|
)
|
|
return nil, nil //nolint:nilnil // Nil runtime with nil error means advisor is skipped for this turn.
|
|
}
|
|
}
|
|
|
|
rt, err := chatadvisor.NewRuntime(chatadvisor.RuntimeConfig{
|
|
Model: advisor.model.LanguageModel(),
|
|
CallTemplate: advisor.newCall(),
|
|
MaxUsesPerRun: maxUsesPerRun,
|
|
MaxOutputTokens: maxOutputTokens,
|
|
StreamSilenceTimeout: p.streamSilenceTimeout,
|
|
MaxRetries: p.chatLimits.MaxGenerationRetries,
|
|
PriorUses: priorUses,
|
|
})
|
|
if err != nil {
|
|
logger.Warn(
|
|
ctx,
|
|
"failed to create advisor runtime, continuing without advisor",
|
|
slog.Error(err),
|
|
)
|
|
return nil, nil //nolint:nilnil // Nil runtime with nil error means advisor is skipped for this turn.
|
|
}
|
|
return rt, nil
|
|
}
|
|
|
|
// pinnedWorkspaceMCPTools builds workspace MCP tools from the chat's pinned
|
|
// definitions. Calls still proxy through the agent connection, and agent pushes
|
|
// live-sync definitions, so no invalidation callback is needed.
|
|
func (p *Server) pinnedWorkspaceMCPTools(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
getConn func(context.Context) (workspacesdk.AgentConn, error),
|
|
) ([]fantasy.AgentTool, error) {
|
|
resources, err := p.db.ListChatContextResourcesByChatID(ctx, chat.ID)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("list chat context resources: %w", err)
|
|
}
|
|
infos := workspaceMCPToolInfosFromResources(resources)
|
|
return chattool.NewWorkspaceMCPTools(infos, getConn), nil
|
|
}
|
|
|
|
// AgentConnFunc provides access to workspace agent connections.
|
|
type AgentConnFunc func(ctx context.Context, agentID uuid.UUID) (workspacesdk.AgentConn, func(), error)
|
|
|
|
var (
|
|
// ErrInvalidModelConfigID indicates the requested model config does not
|
|
// exist, is disabled, or its provider is disabled.
|
|
ErrInvalidModelConfigID = xerrors.New("invalid model config ID")
|
|
// ErrEditedMessageNotFound indicates the edited message does not exist
|
|
// in the target chat.
|
|
ErrEditedMessageNotFound = xerrors.New("edited message not found")
|
|
// ErrEditedMessageNotUser indicates a non-user message edit attempt.
|
|
ErrEditedMessageNotUser = xerrors.New("only user messages can be edited")
|
|
// ErrChatArchived indicates the chat is archived and cannot
|
|
// accept modifications (messages, edits, promotions, or
|
|
// tool-result submissions).
|
|
ErrChatArchived = xerrors.New("chat is archived")
|
|
// ErrNoDefaultChatModelConfig indicates no default chat model config
|
|
// is configured, so chatd cannot resolve a model for the request.
|
|
ErrNoDefaultChatModelConfig = chatstate.ErrNoDefaultChatModelConfig
|
|
// ErrNothingToCompact indicates a manual compaction request found
|
|
// no uncompressed conversation after the latest compaction
|
|
// boundary, so running a compaction would produce nothing.
|
|
ErrNothingToCompact = xerrors.New("nothing to compact")
|
|
// ErrNothingToClear indicates a manual context clear found no
|
|
// model-visible conversation after the latest context boundary,
|
|
// so a clear would be a no-op.
|
|
ErrNothingToClear = xerrors.New("nothing to clear")
|
|
)
|
|
|
|
// CreateOptions controls chat creation in the shared chat mutation path.
|
|
type CreateOptions struct {
|
|
OrganizationID uuid.UUID
|
|
OwnerID uuid.UUID
|
|
// CreatedBy attributes the initial user message; defaults to OwnerID.
|
|
CreatedBy uuid.UUID
|
|
ProjectID uuid.NullUUID
|
|
WorkspaceID uuid.NullUUID
|
|
BuildID uuid.NullUUID
|
|
AgentID uuid.NullUUID
|
|
ParentChatID uuid.NullUUID
|
|
RootChatID uuid.NullUUID
|
|
Title string
|
|
// TitleSource defaults to fallback.
|
|
TitleSource database.ChatTitleSource
|
|
ModelConfigID uuid.UUID
|
|
ReasoningEffort *string
|
|
ChatMode database.NullChatMode
|
|
PlanMode database.NullChatPlanMode
|
|
ClientType database.ChatClientType
|
|
SystemPrompt string
|
|
// InitialUserContent is the first user message. When empty, the
|
|
// chat is created idle (`waiting`) with system messages only and
|
|
// no worker processes it until the first SendMessage.
|
|
InitialUserContent []codersdk.ChatMessagePart
|
|
MCPServerIDs []uuid.UUID
|
|
InlineMCPServers []codersdk.InlineMCPServerRequest
|
|
Labels database.StringMap
|
|
DynamicTools json.RawMessage
|
|
}
|
|
|
|
// SendMessageBusyBehavior controls what happens when a chat is already active.
|
|
type SendMessageBusyBehavior string
|
|
|
|
const (
|
|
// SendMessageBusyBehaviorQueue queues user messages while the chat is busy.
|
|
SendMessageBusyBehaviorQueue SendMessageBusyBehavior = "queue"
|
|
// SendMessageBusyBehaviorSteer delivers the message before the next
|
|
// assistant model call. It is the default.
|
|
SendMessageBusyBehaviorSteer SendMessageBusyBehavior = "steer"
|
|
)
|
|
|
|
// SendMessageOptions controls user message insertion with busy-state behavior.
|
|
type SendMessageOptions struct {
|
|
ChatID uuid.UUID
|
|
CreatedBy uuid.UUID
|
|
Content []codersdk.ChatMessagePart
|
|
ModelConfigID uuid.UUID
|
|
ReasoningEffort *string
|
|
BusyBehavior SendMessageBusyBehavior
|
|
PlanMode *database.NullChatPlanMode
|
|
MCPServerIDs *[]uuid.UUID
|
|
// InlineMCPServers replaces the inline MCP servers. nil: no change.
|
|
InlineMCPServers *[]codersdk.InlineMCPServerRequest
|
|
}
|
|
|
|
// SendMessageResult contains the outcome of user message processing.
|
|
type SendMessageResult struct {
|
|
Queued bool
|
|
QueuedMessage *database.ChatQueuedMessage
|
|
Message database.ChatMessage
|
|
// InsertedMessages holds every message the send inserted, in
|
|
// insertion order. A send to an errored or paused chat can also
|
|
// insert queued messages: a steer send inserts the queued steer
|
|
// messages before the new message, and a queue send to an errored
|
|
// chat inserts the steer lane, or the first queue message when that
|
|
// lane is empty.
|
|
InsertedMessages []database.ChatMessage
|
|
Chat database.Chat
|
|
// FirstUserTurn reports that the send inserted the first
|
|
// user-visible message into a chat still carrying
|
|
// chatprompt.DefaultChatTitle. The send already persisted the
|
|
// fallback title, so callers only schedule the async title upgrade.
|
|
FirstUserTurn bool
|
|
}
|
|
|
|
// EditMessageOptions controls user message edits via soft-delete and re-insert.
|
|
type EditMessageOptions struct {
|
|
ChatID uuid.UUID
|
|
CreatedBy uuid.UUID
|
|
EditedMessageID int64
|
|
Content []codersdk.ChatMessagePart
|
|
// ModelConfigID, when non-zero, overrides the model used for
|
|
// the replacement user message. When set to uuid.Nil the
|
|
// original message's model is preserved.
|
|
ModelConfigID uuid.UUID
|
|
ReasoningEffort *string
|
|
// MCPServerIDs, when non-nil, replaces the chat's MCP server
|
|
// selection before the replacement turn runs. When nil the
|
|
// current selection is preserved.
|
|
MCPServerIDs *[]uuid.UUID
|
|
}
|
|
|
|
// EditMessageResult contains the replacement user message and chat status.
|
|
type EditMessageResult struct {
|
|
Message database.ChatMessage
|
|
// InsertedMessages holds every message the edit inserted, in
|
|
// insertion order: synthetic tool cancellations, the replacement
|
|
// user message, then hook suffix messages.
|
|
InsertedMessages []database.ChatMessage
|
|
// DeletedMessageIDs holds every previously visible message the
|
|
// edit soft-deleted.
|
|
DeletedMessageIDs []int64
|
|
Chat database.Chat
|
|
}
|
|
|
|
// PromoteQueuedOptions controls queued-message promotion.
|
|
type PromoteQueuedOptions struct {
|
|
ChatID uuid.UUID
|
|
QueuedMessageID int64
|
|
}
|
|
|
|
// forcedMCPServerConfigsForOwner filters enabled Force On configs
|
|
// through the chat owner's ACL so availability cannot widen access.
|
|
func forcedMCPServerConfigsForOwner(ctx context.Context, store database.Store, organizationID, ownerID uuid.UUID) ([]database.MCPServerConfig, error) {
|
|
owner, _, err := httpmw.UserRBACSubject(ctx, store, ownerID, rbac.ScopeAll)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load chat owner authorization: %w", err)
|
|
}
|
|
forced, err := store.GetForcedMCPServerConfigsByOrganization(dbauthz.As(ctx, owner), organizationID)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("get forced MCP server configs: %w", err)
|
|
}
|
|
return forced, nil
|
|
}
|
|
|
|
// enforceForcedMCPServerIDs appends owner-readable Force On config IDs
|
|
// missing from ids so callers cannot exclude such servers by stripping
|
|
// IDs from a request (Cure53 CDM-02-010).
|
|
func enforceForcedMCPServerIDs(ctx context.Context, store database.Store, organizationID, ownerID uuid.UUID, ids []uuid.UUID) ([]uuid.UUID, error) {
|
|
forced, err := forcedMCPServerConfigsForOwner(ctx, store, organizationID, ownerID)
|
|
if err != nil {
|
|
// Fail closed: proceeding without the forced set would
|
|
// silently bypass a security policy.
|
|
return nil, err
|
|
}
|
|
merged := slices.Clone(ids)
|
|
if merged == nil {
|
|
merged = []uuid.UUID{}
|
|
}
|
|
seen := make(map[uuid.UUID]struct{}, len(merged))
|
|
for _, id := range merged {
|
|
seen[id] = struct{}{}
|
|
}
|
|
for _, cfg := range forced {
|
|
if _, ok := seen[cfg.ID]; !ok {
|
|
merged = append(merged, cfg.ID)
|
|
}
|
|
}
|
|
return merged, nil
|
|
}
|
|
|
|
// applyRequestedMCPServerIDs replaces the chat's MCP server selection
|
|
// inside the state-machine transaction when a request provides one.
|
|
// Explore child chats keep the spawn-time snapshot immutable. Force On
|
|
// MCP servers are enforced server-side so a caller cannot remove them
|
|
// by tampering with the update (Cure53 CDM-02-010).
|
|
func (p *Server) applyRequestedMCPServerIDs(ctx context.Context, store database.Store, lockedChat database.Chat, requested *[]uuid.UUID) (database.Chat, error) {
|
|
if requested == nil {
|
|
return lockedChat, nil
|
|
}
|
|
if isExploreSubagentMode(lockedChat.Mode) {
|
|
p.logger.Warn(ctx,
|
|
"ignoring explore subagent mcp server ids update, snapshot is immutable after spawn",
|
|
slog.F("chat_id", lockedChat.ID),
|
|
)
|
|
return lockedChat, nil
|
|
}
|
|
enforcedIDs, err := enforceForcedMCPServerIDs(ctx, store, lockedChat.OrganizationID, lockedChat.OwnerID, *requested)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
updated, err := store.UpdateChatMCPServerIDs(ctx, database.UpdateChatMCPServerIDsParams{
|
|
ID: lockedChat.ID,
|
|
MCPServerIDs: enforcedIDs,
|
|
})
|
|
if err != nil {
|
|
return database.Chat{}, xerrors.Errorf("update chat mcp server ids: %w", err)
|
|
}
|
|
return updated, nil
|
|
}
|
|
|
|
// CreateChat creates a chat with its initial history through
|
|
// chatstate.CreateChat. With initial user content the new chat starts
|
|
// in `running` status and ownership hints wake chat workers. Without
|
|
// initial user content the chat is created idle in `waiting` status
|
|
// with system messages only; the first SendMessage starts generation.
|
|
func (p *Server) CreateChat(ctx context.Context, opts CreateOptions) (database.Chat, error) {
|
|
if opts.OrganizationID == uuid.Nil {
|
|
return database.Chat{}, xerrors.New("organization_id is required")
|
|
}
|
|
if opts.OwnerID == uuid.Nil {
|
|
return database.Chat{}, xerrors.New("owner_id is required")
|
|
}
|
|
if strings.TrimSpace(opts.Title) == "" {
|
|
return database.Chat{}, xerrors.New("title is required")
|
|
}
|
|
opts.TitleSource = cmp.Or(opts.TitleSource, database.ChatTitleSourceFallback)
|
|
initialStatus := database.ChatStatusWaiting
|
|
if len(opts.InitialUserContent) > 0 {
|
|
initialStatus = database.ChatStatusRunning
|
|
}
|
|
// Ensure MCPServerIDs is non-nil so pq.Array produces '{}'
|
|
// instead of SQL NULL, which violates the NOT NULL column
|
|
// constraint.
|
|
if opts.MCPServerIDs == nil {
|
|
opts.MCPServerIDs = []uuid.UUID{}
|
|
}
|
|
// Force On MCP servers are enforced server-side so a caller
|
|
// cannot exclude them by stripping IDs from the request
|
|
// (Cure53 CDM-02-010).
|
|
enforcedMCPServerIDs, err := enforceForcedMCPServerIDs(ctx, p.db, opts.OrganizationID, opts.OwnerID, opts.MCPServerIDs)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
opts.MCPServerIDs = enforcedMCPServerIDs
|
|
if opts.Labels == nil {
|
|
opts.Labels = database.StringMap{}
|
|
}
|
|
opts.ClientType = cmp.Or(opts.ClientType, database.ChatClientTypeApi)
|
|
if !opts.ClientType.Valid() {
|
|
return database.Chat{}, xerrors.Errorf("invalid client_type: %q", opts.ClientType)
|
|
}
|
|
// Resolve the deployment prompt before opening the transaction so
|
|
// chat creation does not hold one DB connection while waiting for
|
|
// another pool checkout.
|
|
deploymentPrompt, err := p.resolveDeploymentSystemPrompt(ctx)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
organizationPrompt, err := p.resolveOrganizationSystemPrompt(ctx, opts.OrganizationID)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
|
|
if opts.ModelConfigID != uuid.Nil {
|
|
if err := requireEnabledChatModelConfig(ctx, p.db, opts.OrganizationID, opts.ModelConfigID); err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
}
|
|
|
|
labelsJSON, err := json.Marshal(opts.Labels)
|
|
if err != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal labels: %w", err)
|
|
}
|
|
|
|
chatID := uuid.New()
|
|
// The new chat has no pinned workspace context yet, so it is resolved
|
|
// without a workspace and cannot invoke workspace skills.
|
|
contentParts, err := p.resolveSkillInvocations(ctx, database.Chat{
|
|
ID: chatID,
|
|
OwnerID: opts.OwnerID,
|
|
OrganizationID: opts.OrganizationID,
|
|
ProjectID: opts.ProjectID,
|
|
RootChatID: opts.RootChatID,
|
|
}, opts.InitialUserContent)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
// The prompt-submit hook fires with the first SendMessage when the
|
|
// chat is created without an initial user message.
|
|
if p.hooks.Enabled() && len(contentParts) > 0 {
|
|
// Validate model admission before dispatch, matching the insert path.
|
|
if err := validateCreateModelConfigID(ctx, p.db, opts.OrganizationID, opts.ModelConfigID); err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
turnID := uuid.New()
|
|
promptMessage, err := chathooks.UserPromptMessage(contentParts)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
promptResult, err := p.hooks.Trigger(ctx, chathooks.Chat{
|
|
ID: chatID,
|
|
OwnerID: opts.OwnerID,
|
|
WorkspaceID: opts.WorkspaceID,
|
|
TurnID: &turnID,
|
|
}, promptMessage, agenthooks.EventUserPromptSubmit, dispatch.CapacityClassAdmission)
|
|
if err != nil {
|
|
return database.Chat{}, chathooks.UserPromptDenial(err)
|
|
}
|
|
composed, overridden, err := chathooks.ComposeUserPromptContent(contentParts, promptResult)
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
contentParts = composed
|
|
// Avoid deriving titles from the prompt that policy replaced.
|
|
if overridden && opts.TitleSource == database.ChatTitleSourceFallback {
|
|
opts.Title = chatprompt.FallbackTitle(chatprompt.TitleText(contentParts, nil))
|
|
}
|
|
}
|
|
|
|
userPrompt := codersdk.SanitizePromptText(opts.SystemPrompt)
|
|
workspaceAwareness := workspaceDetachedAwareness
|
|
if opts.WorkspaceID.Valid {
|
|
workspaceAwareness = workspaceAttachedAwareness
|
|
}
|
|
workspaceAwarenessContent, err := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageText(workspaceAwareness),
|
|
})
|
|
if err != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal workspace awareness: %w", err)
|
|
}
|
|
var initialMessages []chatstate.Message
|
|
if deploymentPrompt != "" {
|
|
deploymentContent, marshalErr := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageText(deploymentPrompt),
|
|
})
|
|
if marshalErr != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal deployment system prompt: %w", marshalErr)
|
|
}
|
|
initialMessages = append(initialMessages, systemMessage(deploymentContent, opts.ModelConfigID))
|
|
}
|
|
if organizationPrompt != "" {
|
|
organizationContent, marshalErr := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageText(organizationPrompt),
|
|
})
|
|
if marshalErr != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal organization system prompt: %w", marshalErr)
|
|
}
|
|
initialMessages = append(initialMessages, systemMessage(organizationContent, opts.ModelConfigID))
|
|
}
|
|
if userPrompt != "" {
|
|
userPromptContent, marshalErr := chatprompt.MarshalParts([]codersdk.ChatMessagePart{
|
|
codersdk.ChatMessageText(userPrompt),
|
|
})
|
|
if marshalErr != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal user system prompt: %w", marshalErr)
|
|
}
|
|
initialMessages = append(initialMessages, systemMessage(userPromptContent, opts.ModelConfigID))
|
|
}
|
|
initialMessages = append(initialMessages, systemMessage(workspaceAwarenessContent, opts.ModelConfigID))
|
|
if len(contentParts) > 0 {
|
|
userContent, marshalErr := chatprompt.MarshalParts(contentParts)
|
|
if marshalErr != nil {
|
|
return database.Chat{}, xerrors.Errorf("marshal initial user content: %w", marshalErr)
|
|
}
|
|
initialMessages = append(initialMessages, userMessage(userContent, opts.ModelConfigID, cmp.Or(opts.CreatedBy, opts.OwnerID), opts.ReasoningEffort))
|
|
}
|
|
|
|
result, err := chatstate.CreateChatWithID(ctx, p.db, p.pubsub, chatID, chatstate.CreateChatInput{
|
|
OrganizationID: opts.OrganizationID,
|
|
OwnerID: opts.OwnerID,
|
|
ProjectID: opts.ProjectID,
|
|
WorkspaceID: opts.WorkspaceID,
|
|
BuildID: opts.BuildID,
|
|
AgentID: opts.AgentID,
|
|
ParentChatID: opts.ParentChatID,
|
|
RootChatID: opts.RootChatID,
|
|
LastModelConfigID: opts.ModelConfigID,
|
|
Title: opts.Title,
|
|
TitleSource: opts.TitleSource,
|
|
Mode: opts.ChatMode,
|
|
PlanMode: opts.PlanMode,
|
|
MCPServerIDs: opts.MCPServerIDs,
|
|
InlineMCPServers: opts.InlineMCPServers,
|
|
Labels: pqtype.NullRawMessage{
|
|
RawMessage: labelsJSON,
|
|
Valid: true,
|
|
},
|
|
DynamicTools: pqtype.NullRawMessage{
|
|
RawMessage: opts.DynamicTools,
|
|
Valid: len(opts.DynamicTools) > 0,
|
|
},
|
|
ClientType: opts.ClientType,
|
|
InitialMessages: initialMessages,
|
|
FileIDs: chatprompt.FileIDs(contentParts),
|
|
InitialStatus: initialStatus,
|
|
MaxFileLinks: p.chatLimits.MaxAttachmentsPerChat,
|
|
})
|
|
if err != nil {
|
|
return database.Chat{}, err
|
|
}
|
|
chat := result.Chat
|
|
if !chat.RootChatID.Valid && !chat.ParentChatID.Valid {
|
|
chat.RootChatID = uuid.NullUUID{UUID: chat.ID, Valid: true}
|
|
}
|
|
|
|
// Publish the sidebar watch event explicitly after chatstate has
|
|
// committed and emitted its own state-machine notifications. The
|
|
// watch endpoint is maintained separately from chatstate notifications.
|
|
p.publishChatPubsubEvent(chat, codersdk.ChatWatchEventKindCreated, nil)
|
|
|
|
// Pin the chat to the agent's latest context snapshot if one exists.
|
|
// Best-effort: a chat created before its agent has pushed is hydrated
|
|
// by that agent's next push.
|
|
p.hydrateChatContextOnCreate(ctx, chat)
|
|
return chat, nil
|
|
}
|
|
|
|
func (p *Server) callerModelConfigContext(ctx context.Context, ownerID uuid.UUID) (context.Context, error) {
|
|
if p.modelConfigContext == nil {
|
|
return ctx, nil
|
|
}
|
|
return p.modelConfigContext(ctx, ownerID)
|
|
}
|
|
|
|
func callerModelConfigContext(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
ownerID uuid.UUID,
|
|
) (context.Context, error) {
|
|
if ownerID == uuid.Nil {
|
|
return ctx, nil
|
|
}
|
|
if actor, ok := dbauthz.ActorFromContext(ctx); ok &&
|
|
actor.Type == rbac.SubjectTypeUser && actor.ID == ownerID.String() {
|
|
return ctx, nil
|
|
}
|
|
actor, _, err := httpmw.UserRBACSubject(ctx, store, ownerID, rbac.ScopeAll)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load model config authorization: %w", err)
|
|
}
|
|
//nolint:gocritic // Background Chatd work must use the chat owner's model ACLs.
|
|
return dbauthz.As(ctx, actor), nil
|
|
}
|
|
|
|
// requireEnabledChatModelConfig rechecks enabled state inside the daemon.
|
|
// The coderd preflight can race an admin disabling the model or provider.
|
|
func requireEnabledChatModelConfig(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
modelConfigID uuid.UUID,
|
|
) error {
|
|
config, err := store.GetEnabledChatModelConfigByID(ctx, modelConfigID)
|
|
if err == nil {
|
|
if config.OrganizationID == organizationID {
|
|
return nil
|
|
}
|
|
err = sql.ErrNoRows
|
|
}
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return xerrors.Errorf(
|
|
"%w: %s",
|
|
ErrInvalidModelConfigID,
|
|
modelConfigID,
|
|
)
|
|
}
|
|
return xerrors.Errorf(
|
|
"get requested model config %s: %w",
|
|
modelConfigID,
|
|
err,
|
|
)
|
|
}
|
|
|
|
func validateCreateModelConfigID(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
modelConfigID uuid.UUID,
|
|
) error {
|
|
if modelConfigID == uuid.Nil {
|
|
return xerrors.Errorf("%w: %s", ErrInvalidModelConfigID, modelConfigID)
|
|
}
|
|
config, err := store.GetChatModelConfigByID(ctx, modelConfigID)
|
|
if err == nil {
|
|
if config.OrganizationID == organizationID {
|
|
return nil
|
|
}
|
|
err = sql.ErrNoRows
|
|
}
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return xerrors.Errorf("%w: %s", ErrInvalidModelConfigID, modelConfigID)
|
|
}
|
|
return xerrors.Errorf("get requested model config %s: %w", modelConfigID, err)
|
|
}
|
|
|
|
func resolveFallbackModelConfigID(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
chat database.Chat,
|
|
modelConfigID uuid.UUID,
|
|
) (uuid.UUID, error) {
|
|
if modelConfigID != uuid.Nil {
|
|
config, err := store.GetEnabledChatModelConfigByID(ctx, modelConfigID)
|
|
if err == nil {
|
|
if config.OrganizationID == chat.OrganizationID {
|
|
return modelConfigID, nil
|
|
}
|
|
} else if !errors.Is(err, sql.ErrNoRows) && !dbauthz.IsNotAuthorizedError(err) {
|
|
return uuid.Nil, xerrors.Errorf(
|
|
"get chat model config %s: %w",
|
|
modelConfigID,
|
|
err,
|
|
)
|
|
}
|
|
}
|
|
|
|
defaultConfig, err := effectiveDefaultChatModelConfig(ctx, store, chat.OrganizationID)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return uuid.Nil, ErrNoDefaultChatModelConfig
|
|
}
|
|
return uuid.Nil, xerrors.Errorf("get default chat model config: %w", err)
|
|
}
|
|
return defaultConfig.ID, nil
|
|
}
|
|
|
|
// validateModelConfigOverride returns requested as an override if it is
|
|
// an enabled model config in the organization, and no override when
|
|
// requested is uuid.Nil. Call it before the chat transaction: model
|
|
// config writes serialize on an organization advisory lock that chat
|
|
// transactions do not take, so the chat row lock cannot stop a
|
|
// concurrent disable. If the config is disabled after this check, the
|
|
// turn runs on the organization default model (see resolveModelConfig).
|
|
func validateModelConfigOverride(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
requested uuid.UUID,
|
|
) (uuid.NullUUID, error) {
|
|
if requested == uuid.Nil {
|
|
return uuid.NullUUID{}, nil
|
|
}
|
|
if err := requireEnabledChatModelConfig(ctx, store, organizationID, requested); err != nil {
|
|
return uuid.NullUUID{}, err
|
|
}
|
|
return uuid.NullUUID{UUID: requested, Valid: true}, nil
|
|
}
|
|
|
|
func loadEffectiveChatModelConfigs(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
) (database.EffectiveChatModelConfigs, error) {
|
|
rows, err := store.GetEnabledChatModelConfigsByOrganization(ctx, organizationID)
|
|
if err != nil {
|
|
return database.EffectiveChatModelConfigs{}, err
|
|
}
|
|
return database.DeriveEffectiveChatModelConfigs(rows), nil
|
|
}
|
|
|
|
func effectiveDefaultChatModelConfig(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
) (database.ChatModelConfig, error) {
|
|
effective, err := loadEffectiveChatModelConfigs(ctx, store, organizationID)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, err
|
|
}
|
|
if effective.DefaultConfig.ID == uuid.Nil {
|
|
return database.ChatModelConfig{}, sql.ErrNoRows
|
|
}
|
|
return effective.DefaultConfig, nil
|
|
}
|
|
|
|
// enabledChatModelConfigsForOrganization returns enabled configs from the chat organization.
|
|
func enabledChatModelConfigsForOrganization(
|
|
ctx context.Context,
|
|
store database.Store,
|
|
organizationID uuid.UUID,
|
|
) ([]database.GetEnabledChatModelConfigsByOrganizationRow, error) {
|
|
effective, err := loadEffectiveChatModelConfigs(ctx, store, organizationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return effective.Configs, nil
|
|
}
|
|
|
|
// SubmitToolResultsOptions controls tool result submission.
|
|
type SubmitToolResultsOptions struct {
|
|
ChatID uuid.UUID
|
|
UserID uuid.UUID
|
|
ModelConfigID uuid.UUID
|
|
Results []codersdk.ToolResult
|
|
}
|
|
|
|
// ToolResultValidationError indicates the submitted tool results
|
|
// failed validation (e.g. missing, duplicate, or unexpected IDs,
|
|
// or invalid JSON output).
|
|
type ToolResultValidationError struct {
|
|
Message string
|
|
Detail string
|
|
}
|
|
|
|
func (e *ToolResultValidationError) Error() string {
|
|
if e.Detail != "" {
|
|
return e.Message + ": " + e.Detail
|
|
}
|
|
return e.Message
|
|
}
|
|
|
|
// ToolResultStatusConflictError indicates the chat is not in the
|
|
// requires_action state expected for tool result submission.
|
|
type ToolResultStatusConflictError struct {
|
|
ActualStatus database.ChatStatus
|
|
}
|
|
|
|
func (e *ToolResultStatusConflictError) Error() string {
|
|
return fmt.Sprintf(
|
|
"chat status is %q, expected %q",
|
|
e.ActualStatus, database.ChatStatusRequiresAction,
|
|
)
|
|
}
|
|
|
|
const manualTitleMessageWindowLimit = 50
|
|
|
|
// RenameChatTitle persists a user-supplied chat title. An unchanged
|
|
// title is still written when its source is not yet user.
|
|
func (p *Server) RenameChatTitle(
|
|
ctx context.Context,
|
|
chatID uuid.UUID,
|
|
newTitle string,
|
|
) (updated database.Chat, wrote bool, err error) {
|
|
currentChat, err := p.db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
return database.Chat{}, false, xerrors.Errorf("get chat for rename: %w", err)
|
|
}
|
|
if newTitle == currentChat.Title && currentChat.TitleSource == database.ChatTitleSourceUser {
|
|
return currentChat, false, nil
|
|
}
|
|
|
|
updatedChat, err := p.db.UpdateChatTitleByID(ctx, database.UpdateChatTitleByIDParams{
|
|
ID: chatID,
|
|
Title: newTitle,
|
|
TitleSource: database.ChatTitleSourceUser,
|
|
})
|
|
if err != nil {
|
|
return database.Chat{}, false, xerrors.Errorf("update chat title: %w", err)
|
|
}
|
|
return updatedChat, true, nil
|
|
}
|
|
|
|
// PublishTitleChange broadcasts a title_change event for the given chat.
|
|
func (p *Server) PublishTitleChange(chat database.Chat) {
|
|
p.publishChatPubsubEvent(chat, codersdk.ChatWatchEventKindTitleChange, nil)
|
|
}
|
|
|
|
// ProposeChatTitle generates a title suggestion from the chat's
|
|
// visible messages without persisting it.
|
|
func (p *Server) ProposeChatTitle(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
) (string, error) {
|
|
//nolint:gocritic // Non-admin users need chatd-scoped config reads here.
|
|
chatdCtx := dbauthz.AsChatd(ctx)
|
|
return p.generateManualTitleCandidate(chatdCtx, chat)
|
|
}
|
|
|
|
// generateManualTitleCandidate generates a title candidate from the chat's
|
|
// visible messages. It returns "" when the chat has no messages to summarize.
|
|
// Endpoint-specific commit paths decide whether to persist the title.
|
|
func (p *Server) generateManualTitleCandidate(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
) (string, error) {
|
|
headMessages, err := p.db.GetChatMessagesByChatIDAscPaginated(
|
|
ctx,
|
|
database.GetChatMessagesByChatIDAscPaginatedParams{
|
|
ChatID: chat.ID,
|
|
AfterID: 0,
|
|
LimitVal: manualTitleMessageWindowLimit,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get head chat messages: %w", err)
|
|
}
|
|
tailMessages, err := p.db.GetChatMessagesByChatIDDescPaginated(
|
|
ctx,
|
|
database.GetChatMessagesByChatIDDescPaginatedParams{
|
|
ChatID: chat.ID,
|
|
BeforeID: 0,
|
|
LimitVal: manualTitleMessageWindowLimit,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get tail chat messages: %w", err)
|
|
}
|
|
messages := mergeManualTitleMessages(headMessages, tailMessages)
|
|
if len(messages) == 0 {
|
|
return "", nil
|
|
}
|
|
pasteText, err := titlePasteText(ctx, p.db, messages)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get pasted-text attachments for manual title: %w", err)
|
|
}
|
|
apiKeyID, err := p.ensureSyntheticAPIKeyID(ctx, chat.OwnerID)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("ensure synthetic API key: %w", err)
|
|
}
|
|
modelOpts := modelBuildOptions{ActiveAPIKeyID: apiKeyID}
|
|
|
|
resolved, err := p.resolveQuickgenModel(ctx, "title", chat, modelOpts)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
titleCtx := ctx
|
|
finishDebugRun := func(error) {}
|
|
if resolved.debugEnabled {
|
|
titleCtx, finishDebugRun = prepareManualTitleDebugRun(
|
|
ctx,
|
|
p.debugService(),
|
|
chat,
|
|
resolved,
|
|
messages,
|
|
)
|
|
}
|
|
|
|
title, err := generateManualTitle(
|
|
titleCtx,
|
|
messages,
|
|
pasteText,
|
|
resolved.model.LanguageModel(),
|
|
titleObjectCall(resolved),
|
|
p.chatLimits.MaxGenerationRetries,
|
|
)
|
|
finishDebugRun(err)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("generate manual title: %w", err)
|
|
}
|
|
|
|
return title, nil
|
|
}
|
|
|
|
func prepareManualTitleDebugRun(
|
|
ctx context.Context,
|
|
debugSvc *chatdebug.Service,
|
|
chat database.Chat,
|
|
resolved resolvedModelCall,
|
|
messages []database.ChatMessage,
|
|
) (context.Context, func(error)) {
|
|
var historyTipMessageID int64
|
|
if len(messages) > 0 {
|
|
historyTipMessageID = messages[len(messages)-1].ID
|
|
}
|
|
|
|
// Derive a first_message label from the first user message.
|
|
var firstUserLabel string
|
|
for _, msg := range messages {
|
|
if msg.Role == database.ChatMessageRoleUser {
|
|
if parts, parseErr := chatprompt.ParseContent(msg); parseErr == nil {
|
|
firstUserLabel = contentBlocksToText(parts)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if firstUserLabel == "" {
|
|
firstUserLabel = "Title generation"
|
|
}
|
|
seedSummary := chatdebug.SeedSummary(
|
|
chatdebug.TruncateLabel(firstUserLabel, chatdebug.MaxLabelLength),
|
|
)
|
|
|
|
return debugSvc.StartRun(ctx, chatdebug.CreateRunParams{
|
|
ChatID: chat.ID,
|
|
ModelConfigID: resolved.dbConfig.ID,
|
|
Provider: string(resolved.route.Provider.Type),
|
|
Model: resolved.dbConfig.Model,
|
|
Kind: chatdebug.KindTitleGeneration,
|
|
Status: chatdebug.StatusInProgress,
|
|
HistoryTipMessageID: historyTipMessageID,
|
|
TriggerMessageID: 0,
|
|
}, seedSummary, debugFinalizeTimeout)
|
|
}
|
|
|
|
func deriveChatDebugSeed(messages []database.ChatMessage) (
|
|
triggerMessageID int64,
|
|
historyTipMessageID int64,
|
|
triggerLabel string,
|
|
) {
|
|
for i := len(messages) - 1; i >= 0; i-- {
|
|
if messages[i].Role != database.ChatMessageRoleUser {
|
|
continue
|
|
}
|
|
triggerMessageID = messages[i].ID
|
|
if parts, parseErr := chatprompt.ParseContent(messages[i]); parseErr == nil {
|
|
triggerLabel = contentBlocksToText(parts)
|
|
}
|
|
break
|
|
}
|
|
|
|
if len(messages) > 0 {
|
|
historyTipMessageID = messages[len(messages)-1].ID
|
|
}
|
|
|
|
return triggerMessageID, historyTipMessageID, triggerLabel
|
|
}
|
|
|
|
func mergeManualTitleMessages(
|
|
headMessages []database.ChatMessage,
|
|
tailMessagesDesc []database.ChatMessage,
|
|
) []database.ChatMessage {
|
|
merged := make([]database.ChatMessage, 0, len(headMessages)+len(tailMessagesDesc))
|
|
seen := make(map[int64]struct{}, len(headMessages)+len(tailMessagesDesc))
|
|
appendUnique := func(message database.ChatMessage) {
|
|
if _, ok := seen[message.ID]; ok {
|
|
return
|
|
}
|
|
seen[message.ID] = struct{}{}
|
|
merged = append(merged, message)
|
|
}
|
|
for _, message := range headMessages {
|
|
appendUnique(message)
|
|
}
|
|
for i := len(tailMessagesDesc) - 1; i >= 0; i-- {
|
|
appendUnique(tailMessagesDesc[i])
|
|
}
|
|
return merged
|
|
}
|
|
|
|
// Config configures a chat processor.
|
|
type Config struct {
|
|
Logger slog.Logger
|
|
Database database.Store
|
|
ReplicaID uuid.UUID
|
|
// StreamPartsDialer dials remote stream parts. Nil uses the local
|
|
// in-process channel dialer for every stream.
|
|
StreamPartsDialer StreamPartsDialer
|
|
PendingChatAcquireInterval time.Duration
|
|
MaxChatsPerAcquire int32
|
|
InFlightChatStaleAfter time.Duration
|
|
ChatHeartbeatInterval time.Duration
|
|
StreamSilenceTimeout time.Duration
|
|
AgentConn AgentConnFunc
|
|
AgentInactiveDisconnectTimeout time.Duration
|
|
CreateWorkspace chattool.CreateWorkspaceFn
|
|
StartWorkspace chattool.StartWorkspaceFn
|
|
StopWorkspace chattool.StopWorkspaceFn
|
|
RenderTemplateParameters chattool.RenderTemplateParametersFn
|
|
ProviderAPIKeys chatprovider.ProviderAPIKeys
|
|
AllowBYOK bool
|
|
AllowBYOKSet bool
|
|
AlwaysEnableDebugLogs bool
|
|
DisableCallerSuppliedTools bool
|
|
WebpushDispatcher webpush.Dispatcher
|
|
HookDispatcher *dispatch.Dispatcher
|
|
UsageTracker *workspacestats.UsageTracker
|
|
Clock quartz.Clock
|
|
AIBridgeTransportFactory *atomic.Pointer[aibridge.TransportFactory]
|
|
Experiments codersdk.Experiments
|
|
PrometheusRegistry prometheus.Registerer
|
|
TracerProvider trace.TracerProvider
|
|
|
|
AgentCapacityUnlock AgentCapacityUnlock
|
|
|
|
// OIDCTokenSource resolves the calling user's OIDC access
|
|
// token for MCP servers configured with auth_type=user_oidc.
|
|
// May be nil if the deployment has no OIDC provider; servers
|
|
// using user_oidc will then send no Authorization header.
|
|
OIDCTokenSource mcpclient.UserOIDCTokenSource
|
|
MCPHTTPClient *http.Client
|
|
// InternalMCPServers are MCP servers that coderd code serves to chats
|
|
// in process. Slugs must be unique and not empty.
|
|
InternalMCPServers []InternalMCPServer
|
|
|
|
NotificationsEnqueuer notifications.Enqueuer
|
|
Auditor *atomic.Pointer[audit.Auditor]
|
|
// Limits are the deployment chat limits.
|
|
Limits Limits
|
|
}
|
|
|
|
// New creates a new chat processor with the required pubsub dependency.
|
|
// The processor polls for pending chats and processes them. It is the
|
|
// caller's responsibility to call Close on the returned instance.
|
|
func New(ps pubsub.Pubsub, cfg Config) (*Server, error) {
|
|
if err := validateInternalMCPServers(cfg.InternalMCPServers); err != nil {
|
|
return nil, xerrors.Errorf("chatd: %w", err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
pendingChatAcquireInterval := cfg.PendingChatAcquireInterval
|
|
if pendingChatAcquireInterval == 0 {
|
|
pendingChatAcquireInterval = DefaultPendingChatAcquireInterval
|
|
}
|
|
|
|
inFlightChatStaleAfter := cfg.InFlightChatStaleAfter
|
|
if inFlightChatStaleAfter == 0 {
|
|
inFlightChatStaleAfter = DefaultInFlightChatStaleAfter
|
|
}
|
|
|
|
maxChatsPerAcquire := cfg.MaxChatsPerAcquire
|
|
if maxChatsPerAcquire <= 0 {
|
|
maxChatsPerAcquire = DefaultMaxChatsPerAcquire
|
|
}
|
|
|
|
chatHeartbeatInterval := cfg.ChatHeartbeatInterval
|
|
if chatHeartbeatInterval == 0 {
|
|
chatHeartbeatInterval = DefaultChatHeartbeatInterval
|
|
}
|
|
|
|
streamSilenceTimeout := cfg.StreamSilenceTimeout
|
|
if streamSilenceTimeout == 0 {
|
|
streamSilenceTimeout = chatloop.DefaultStreamSilenceTimeout
|
|
}
|
|
|
|
clk := cfg.Clock
|
|
if clk == nil {
|
|
clk = quartz.NewReal()
|
|
}
|
|
|
|
notificationsEnqueuer := cfg.NotificationsEnqueuer
|
|
if notificationsEnqueuer == nil {
|
|
notificationsEnqueuer = notifications.NewNoopEnqueuer()
|
|
}
|
|
|
|
mcpHTTPClient := cfg.MCPHTTPClient
|
|
if mcpHTTPClient == nil {
|
|
mcpHTTPClient = mcpclient.NewHTTPClient(nil)
|
|
}
|
|
|
|
workerID := cfg.ReplicaID
|
|
if workerID == uuid.Nil {
|
|
workerID = uuid.New()
|
|
}
|
|
|
|
limits := cfg.Limits.withDefaults()
|
|
|
|
allowBYOK := true
|
|
if cfg.AllowBYOKSet {
|
|
allowBYOK = cfg.AllowBYOK
|
|
}
|
|
|
|
// Require the experiment even for injected dispatchers to
|
|
// preserve explicit opt-in.
|
|
hookDispatcher := cfg.HookDispatcher
|
|
if hookDispatcher != nil && !cfg.Experiments.Enabled(codersdk.ExperimentAgentLifecycleHooks) {
|
|
cfg.Logger.Warn(ctx, "ignoring chat lifecycle hook dispatcher; the agent-lifecycle-hooks experiment is not enabled")
|
|
hookDispatcher = nil
|
|
}
|
|
p := &Server{
|
|
cancel: cancel,
|
|
db: cfg.Database,
|
|
logger: cfg.Logger.Named("processor"),
|
|
modelConfigContext: func(ctx context.Context, ownerID uuid.UUID) (context.Context, error) {
|
|
return callerModelConfigContext(ctx, cfg.Database, ownerID)
|
|
},
|
|
agentConnFn: cfg.AgentConn,
|
|
agentInactiveDisconnectTimeout: cfg.AgentInactiveDisconnectTimeout,
|
|
dialTimeout: defaultDialTimeout,
|
|
createWorkspaceFn: cfg.CreateWorkspace,
|
|
startWorkspaceFn: cfg.StartWorkspace,
|
|
stopWorkspaceFn: cfg.StopWorkspace,
|
|
renderTemplateParametersFn: cfg.RenderTemplateParameters,
|
|
pubsub: ps,
|
|
webpushDispatcher: cfg.WebpushDispatcher,
|
|
hooks: chathooks.NewTrigger(hookDispatcher),
|
|
providerAPIKeys: cfg.ProviderAPIKeys,
|
|
allowBYOK: allowBYOK,
|
|
disableCallerSuppliedTools: cfg.DisableCallerSuppliedTools,
|
|
oidcTokenSource: cfg.OIDCTokenSource,
|
|
mcpHTTPClient: mcpHTTPClient,
|
|
internalMCPServers: cfg.InternalMCPServers,
|
|
debugSvcFactory: func() *chatdebug.Service {
|
|
return chatdebug.NewService(
|
|
cfg.Database,
|
|
cfg.Logger.Named("chatdebug"),
|
|
ps,
|
|
chatdebug.WithAlwaysEnable(cfg.AlwaysEnableDebugLogs),
|
|
)
|
|
},
|
|
aibridgeTransportFactory: cfg.AIBridgeTransportFactory,
|
|
experiments: cfg.Experiments,
|
|
inFlightChatStaleAfter: inFlightChatStaleAfter,
|
|
streamSilenceTimeout: streamSilenceTimeout,
|
|
usageTracker: cfg.UsageTracker,
|
|
clock: clk,
|
|
recordingSem: make(chan struct{}, limits.MaxConcurrentRecordingUploads),
|
|
chatLimits: limits,
|
|
}
|
|
var chatAutoArchiveRecords prometheus.Counter
|
|
if cfg.PrometheusRegistry != nil {
|
|
p.metrics = chatloop.NewMetricsWithOptions(cfg.PrometheusRegistry, chatloop.MetricsOptions{
|
|
StageMetrics: cfg.Experiments.Enabled(codersdk.ExperimentChatStageMetrics),
|
|
})
|
|
chatAutoArchiveRecords = prometheus.NewCounter(prometheus.CounterOpts{
|
|
Namespace: "coderd",
|
|
Subsystem: "chat_auto_archive",
|
|
Name: "records_archived_total",
|
|
Help: "Total number of chats archived by the auto-archive job (counting both roots and cascaded children).",
|
|
})
|
|
cfg.PrometheusRegistry.MustRegister(chatAutoArchiveRecords)
|
|
} else {
|
|
p.metrics = chatloop.NopMetrics()
|
|
}
|
|
p.stages = chatloop.NewStageTracer(cfg.TracerProvider, p.metrics, chatloop.WithClock(clk))
|
|
p.messagePartBuffer = messagepartbuffer.New(messagepartbuffer.Options{Clock: clk})
|
|
localStreamPartsDialer := NewLocalStreamPartsDialer(LocalStreamPartsDialerConfig{
|
|
Buffer: p.messagePartBuffer,
|
|
Logger: cfg.Logger,
|
|
})
|
|
p.streamPartsDialer = streamPartsDialerForServer(workerID, localStreamPartsDialer, cfg.StreamPartsDialer)
|
|
p.streamSyncPoller = newStreamSyncPoller(ctx, cfg.Database, clk, cfg.Logger.Named("chatstream"))
|
|
p.streamSyncPoller.Start()
|
|
agentCapacityLimiter := newAgentCapacityLimiter(
|
|
cfg.AgentCapacityUnlock,
|
|
int32(inFlightChatStaleAfter.Seconds()),
|
|
)
|
|
var agentCapacityMetrics *capacityMetrics
|
|
if cfg.PrometheusRegistry != nil {
|
|
agentCapacityMetrics = newCapacityMetrics(cfg.PrometheusRegistry)
|
|
}
|
|
p.agentCapacityLimiter = agentCapacityLimiter
|
|
chatWorker, err := newChatWorker(p, chatWorkerOptions{
|
|
WorkerID: workerID,
|
|
Store: cfg.Database,
|
|
Pubsub: ps,
|
|
Logger: cfg.Logger.Named("chatworker"),
|
|
Clock: clk,
|
|
MessagePartBuffer: p.messagePartBuffer,
|
|
AgentCapacityLimiter: agentCapacityLimiter,
|
|
CapacityMetrics: agentCapacityMetrics,
|
|
AcquisitionInterval: pendingChatAcquireInterval,
|
|
AcquisitionBatchSize: maxChatsPerAcquire,
|
|
HeartbeatInterval: chatHeartbeatInterval,
|
|
HeartbeatStaleSeconds: int32(inFlightChatStaleAfter.Seconds()),
|
|
NotificationsEnqueuer: notificationsEnqueuer,
|
|
Auditor: cfg.Auditor,
|
|
AutoArchiveRecords: chatAutoArchiveRecords,
|
|
})
|
|
if err != nil {
|
|
panic("chatd: create chat worker: " + err.Error())
|
|
}
|
|
p.chatWorker = chatWorker
|
|
|
|
//nolint:gocritic // The chat processor uses a scoped chatd context.
|
|
ctx = dbauthz.AsChatd(ctx)
|
|
|
|
p.configCache = newChatConfigCache(ctx, cfg.Database, clk)
|
|
cancelConfigSub, err := p.pubsub.SubscribeWithErr(
|
|
coderdpubsub.ChatConfigEventChannel,
|
|
coderdpubsub.HandleChatConfigEvent(func(ctx context.Context, ev coderdpubsub.ChatConfigEvent, err error) {
|
|
if err != nil {
|
|
p.logger.Warn(ctx, "chat config event error", slog.Error(err))
|
|
return
|
|
}
|
|
switch ev.Kind {
|
|
case coderdpubsub.ChatConfigEventUserPrompt:
|
|
p.configCache.InvalidateUserPrompt(ev.EntityID)
|
|
case coderdpubsub.ChatConfigEventAdvisorConfig:
|
|
p.configCache.InvalidateAdvisorConfig()
|
|
}
|
|
}),
|
|
)
|
|
if err != nil {
|
|
p.logger.Error(ctx, "subscribe to chat config events", slog.Error(err))
|
|
} else {
|
|
p.configCacheUnsubscribe = cancelConfigSub
|
|
}
|
|
|
|
cancelProviderSub, err := p.pubsub.SubscribeWithErr(
|
|
coderdpubsub.AIProvidersChangedChannel,
|
|
func(cbCtx context.Context, _ []byte, err error) {
|
|
if err != nil {
|
|
p.logger.Warn(cbCtx, "ai providers changed event error", slog.Error(err))
|
|
return
|
|
}
|
|
p.configCache.InvalidateProviders()
|
|
},
|
|
)
|
|
if err != nil {
|
|
p.logger.Error(ctx, "subscribe to ai providers changed events", slog.Error(err))
|
|
} else {
|
|
p.providerCacheUnsubscribe = cancelProviderSub
|
|
}
|
|
|
|
p.ctx = ctx
|
|
|
|
// Spawn background goroutines that all servers need.
|
|
|
|
return p, nil
|
|
}
|
|
|
|
// Start runs the background acquire/wake loop that picks up
|
|
// pending chats and processes them. Callers that want a passive
|
|
// server (e.g. tests) can skip this call; heartbeat, stream
|
|
// janitor, and stale recovery still run.
|
|
func (p *Server) Start() *Server {
|
|
if p.chatWorker != nil {
|
|
if err := p.chatWorker.Start(p.ctx); err != nil {
|
|
p.logger.Error(p.ctx, "failed to start chat worker", slog.Error(err))
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// publishChatPubsubEvents broadcasts a lifecycle event for each affected chat.
|
|
func (p *Server) publishChatPubsubEvents(chats []database.Chat, kind codersdk.ChatWatchEventKind) {
|
|
for _, chat := range chats {
|
|
p.publishChatPubsubEvent(chat, kind, nil)
|
|
}
|
|
}
|
|
|
|
// chatWatchEventSDKChat builds the chat embedded in ChatWatchEvent
|
|
// notifications. These payloads travel through PostgreSQL NOTIFY, so
|
|
// omit fields that can grow large and that watch consumers already read
|
|
// from the REST chat endpoint.
|
|
func chatWatchEventSDKChat(chat database.Chat, diffStatus *codersdk.ChatDiffStatus) codersdk.Chat {
|
|
sdkChat := db2sdk.Chat(chat, nil, nil)
|
|
sdkChat.Files = nil
|
|
if diffStatus != nil {
|
|
sdkChat.DiffStatus = diffStatus
|
|
}
|
|
return sdkChat
|
|
}
|
|
|
|
// publishChatPubsubEvent broadcasts a chat lifecycle event via PostgreSQL
|
|
// pubsub so that all replicas can push updates to watching clients.
|
|
func (p *Server) publishChatPubsubEvent(chat database.Chat, kind codersdk.ChatWatchEventKind, diffStatus *codersdk.ChatDiffStatus) {
|
|
event := codersdk.ChatWatchEvent{
|
|
Kind: kind,
|
|
Chat: chatWatchEventSDKChat(chat, diffStatus),
|
|
}
|
|
p.publishChatEvent(chat.OwnerID, chat.ID, event)
|
|
}
|
|
|
|
// publishChatEvent publishes one chat watch event.
|
|
func (p *Server) publishChatEvent(ownerID, chatID uuid.UUID, event codersdk.ChatWatchEvent) {
|
|
payload, err := json.Marshal(event)
|
|
if err != nil {
|
|
p.logger.Error(context.Background(), "failed to marshal chat pubsub event",
|
|
slog.F("chat_id", chatID),
|
|
slog.Error(err),
|
|
)
|
|
return
|
|
}
|
|
if err := p.pubsub.Publish(coderdpubsub.ChatWatchEventChannel(ownerID), payload); err != nil {
|
|
p.logger.Error(context.Background(), "failed to publish chat pubsub event",
|
|
slog.F("chat_id", chatID),
|
|
slog.F("kind", event.Kind),
|
|
slog.Error(err),
|
|
)
|
|
}
|
|
}
|
|
|
|
// ChatQueuedForCapacity reports whether the chat is waiting for a
|
|
// concurrent-agent capacity slot. Uncapped deployments always return false.
|
|
func (p *Server) ChatQueuedForCapacity(ctx context.Context, chat database.Chat) (bool, error) {
|
|
limits, capped := p.agentCapacityLimiter.Limits()
|
|
if !capped {
|
|
return false, nil
|
|
}
|
|
if chat.Archived || chat.Status != database.ChatStatusRunning {
|
|
return false, nil
|
|
}
|
|
// The pool count spans other users' chats, which the requester cannot
|
|
// read directly.
|
|
//nolint:gocritic // Capacity accounting is chatd-internal state.
|
|
return p.db.GetChatQueuedForCapacity(dbauthz.AsChatd(ctx), database.GetChatQueuedForCapacityParams{
|
|
ChatID: chat.ID,
|
|
StaleSeconds: int32(p.inFlightChatStaleAfter.Seconds()),
|
|
RootCapacity: limits.Root,
|
|
SubagentCapacity: limits.Subagent,
|
|
})
|
|
}
|
|
|
|
// PublishDiffStatusChange broadcasts a diff_status_change event for
|
|
// the given chat. changed_diff_status names the one ref that changed.
|
|
// The embedded chat's diff_status carries the primary.
|
|
func (p *Server) PublishDiffStatusChange(ctx context.Context, chatID uuid.UUID, ref codersdk.DiffStatusRef) error {
|
|
chat, err := p.db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
return xerrors.Errorf("get chat: %w", err)
|
|
}
|
|
|
|
dbStatuses, err := p.db.GetChatDiffStatusesByChatID(ctx, chatID)
|
|
if err != nil {
|
|
return xerrors.Errorf("get chat diff status: %w", err)
|
|
}
|
|
|
|
var changed *codersdk.ChatDiffStatus
|
|
var primary *codersdk.ChatDiffStatus
|
|
for i := range dbStatuses {
|
|
row := &dbStatuses[i]
|
|
if row.GitRemoteOrigin == ref.RemoteOrigin && row.GitBranch == ref.GitBranch {
|
|
sdk := db2sdk.ChatDiffStatus(chatID, row)
|
|
changed = &sdk
|
|
break
|
|
}
|
|
}
|
|
if primaryRow := db2sdk.PrimaryChatDiffStatus(dbStatuses); primaryRow != nil {
|
|
sdk := db2sdk.ChatDiffStatus(chatID, primaryRow)
|
|
primary = &sdk
|
|
}
|
|
if changed == nil {
|
|
// The ref has no row (its PR was cleared). The event still
|
|
// tells clients to re-fetch the chat.
|
|
changed = &codersdk.ChatDiffStatus{ChatID: chatID}
|
|
}
|
|
event := codersdk.ChatWatchEvent{
|
|
Kind: codersdk.ChatWatchEventKindDiffStatusChange,
|
|
Chat: chatWatchEventSDKChat(chat, primary),
|
|
ChangedDiffStatus: &codersdk.ChangedDiffStatus{
|
|
Ref: ref,
|
|
Status: changed,
|
|
},
|
|
}
|
|
p.publishChatEvent(chat.OwnerID, chat.ID, event)
|
|
return nil
|
|
}
|
|
|
|
// Rejects oversize images on capped providers before any upstream
|
|
// request is issued.
|
|
//
|
|
// Gotcha: a historical oversize image bricks the chat on a capped
|
|
// provider until the user switches providers back, starts a new
|
|
// chat, or edits a message above the offending one (which truncates
|
|
// the prompt forward). A future change should skip the file with a
|
|
// user-facing warning, but that requires altering the FileResolver
|
|
// contract.
|
|
func (p *Server) chatFileResolver(model chatprovider.Model) chatprompt.FileResolver {
|
|
imageCap, hasImageCap := model.InlineImageCapBytes()
|
|
normalizedProvider := model.RouteProvider()
|
|
return func(ctx context.Context, ids []uuid.UUID) (map[uuid.UUID]chatprompt.FileData, error) {
|
|
files, err := p.db.GetChatFilesByIDs(ctx, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make(map[uuid.UUID]chatprompt.FileData, len(files))
|
|
for _, f := range files {
|
|
if hasImageCap &&
|
|
chatfiles.IsRasterImageMediaType(f.Mimetype) &&
|
|
len(f.Data) >= imageCap {
|
|
err := xerrors.Errorf(
|
|
"image attachment %q is %d bytes; %s inline image limit is %d bytes",
|
|
f.Name, len(f.Data),
|
|
chatprovider.ProviderDisplayName(normalizedProvider),
|
|
imageCap,
|
|
)
|
|
// User-facing message stays client-agnostic since
|
|
// older web clients and direct API callers don't
|
|
// auto-resize; the wrapped error above keeps the
|
|
// exact byte count for operator logs.
|
|
return nil, chaterror.WithClassification(err, chaterror.ClassifiedError{
|
|
Kind: codersdk.ChatErrorKindConfig,
|
|
Provider: normalizedProvider,
|
|
Message: fmt.Sprintf(
|
|
"Image attachment exceeds %s's %s inline image limit. Replace it with a smaller image.",
|
|
chatprovider.ProviderDisplayName(normalizedProvider),
|
|
//nolint:gosec // imageCap is a small positive constant defined in chatprovider.
|
|
humanize.IBytes(uint64(imageCap)),
|
|
),
|
|
Retryable: false,
|
|
})
|
|
}
|
|
result[f.ID] = chatprompt.FileData{
|
|
Name: f.Name,
|
|
Data: f.Data,
|
|
MediaType: f.Mimetype,
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
// trackWorkspaceUsage bumps the workspace's last_used_at via the
|
|
// usage tracker and extends the workspace's autostop deadline. If
|
|
// wsID is not yet valid, it re-reads the chat from the DB to pick
|
|
// up late associations (e.g. create_workspace linking a workspace
|
|
// mid-conversation). The caller should store the returned value so
|
|
// that subsequent calls skip the DB lookup once a workspace has
|
|
// been found.
|
|
func (p *Server) trackWorkspaceUsage(
|
|
ctx context.Context,
|
|
chatID uuid.UUID,
|
|
wsID uuid.NullUUID,
|
|
logger slog.Logger,
|
|
) uuid.NullUUID {
|
|
if p.usageTracker == nil {
|
|
return wsID
|
|
}
|
|
if !wsID.Valid {
|
|
latest, err := p.db.GetChatByID(ctx, chatID)
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to re-read chat for workspace association", slog.Error(err))
|
|
return wsID
|
|
}
|
|
wsID = latest.WorkspaceID
|
|
}
|
|
if wsID.Valid {
|
|
p.usageTracker.Add(wsID.UUID)
|
|
// Bump the workspace autostop deadline. We pass time.Time{}
|
|
// for nextAutostart since we don't have access to
|
|
// TemplateScheduleStore here. The activity bump logic
|
|
// defaults to the template's activity_bump duration
|
|
// (typically 1 hour). Chat workspaces are never prebuilds,
|
|
// so no prebuild guard is needed (unlike reporter.go).
|
|
//
|
|
// This fires every heartbeat (~30s) but the SQL only
|
|
// writes when 5% of the deadline has elapsed, most calls
|
|
// perform a read-only CTE lookup with no UPDATE.
|
|
//
|
|
// Scaling note: for 10,000 active chats, this could lead to
|
|
// approx. 333 CTE queries/second. A cheap fix for this could
|
|
// be to heartbeat every Nth query. Leaving as potential future
|
|
// low-hanging fruit if needed.
|
|
workspacestats.ActivityBumpWorkspace(ctx, logger.Named("activity_bump"), p.db, wsID.UUID, time.Time{}, workspacestats.ActivityBumpReasonChatHeartbeat)
|
|
}
|
|
return wsID
|
|
}
|
|
|
|
type runChatResult struct {
|
|
FinalAssistantText string
|
|
// StatusLabelCall is nil when status-label model resolution failed.
|
|
StatusLabelCall *resolvedModelCall
|
|
TriggerMessageID int64
|
|
HistoryTipMessageID int64
|
|
}
|
|
|
|
func (p *Server) aiProviderConfig(ctx context.Context, provider database.AIProvider) (chatprovider.ConfiguredProvider, error) {
|
|
keys, err := p.db.GetAIProviderKeysByProviderID(ctx, provider.ID)
|
|
if err != nil {
|
|
return chatprovider.ConfiguredProvider{}, xerrors.Errorf("get AI provider keys: %w", err)
|
|
}
|
|
return p.aiProviderConfigFromKeys(provider, keys)
|
|
}
|
|
|
|
func (p *Server) aiProviderConfigFromKeys(provider database.AIProvider, keys []database.AIProviderKey) (chatprovider.ConfiguredProvider, error) {
|
|
if !provider.Enabled {
|
|
return chatprovider.ConfiguredProvider{}, xerrors.Errorf("AI provider %s is disabled", provider.ID)
|
|
}
|
|
settings, err := db2sdk.AIProviderSettings(provider.Settings)
|
|
if err != nil {
|
|
return chatprovider.ConfiguredProvider{}, xerrors.Errorf("decode AI provider settings: %w", err)
|
|
}
|
|
|
|
apiKey := ""
|
|
// GetAIProviderKeysByProviderID orders keys oldest first. chatd consumes
|
|
// one provider-scoped key because runtime provider config has one API key slot.
|
|
for _, key := range keys {
|
|
if key.APIKey != "" {
|
|
apiKey = key.APIKey
|
|
break
|
|
}
|
|
}
|
|
return chatprovider.ConfiguredProvider{
|
|
ProviderID: provider.ID,
|
|
Provider: string(provider.Type),
|
|
APIKey: apiKey,
|
|
BaseURL: provider.BaseUrl,
|
|
CentralAPIKeyEnabled: true,
|
|
AllowUserAPIKey: p.allowBYOK,
|
|
AllowCentralAPIKeyFallback: true,
|
|
SupportsAmbientCredentials: settings.ClaudePlatformAWS != nil,
|
|
}, nil
|
|
}
|
|
|
|
func (p *Server) aiProviderConfigs(ctx context.Context, providers []database.AIProvider) ([]chatprovider.ConfiguredProvider, error) {
|
|
if len(providers) == 0 {
|
|
return nil, nil
|
|
}
|
|
providerIDs := make([]uuid.UUID, 0, len(providers))
|
|
for _, provider := range providers {
|
|
providerIDs = append(providerIDs, provider.ID)
|
|
}
|
|
keys, err := p.db.GetAIProviderKeysByProviderIDs(ctx, providerIDs)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("get AI provider keys: %w", err)
|
|
}
|
|
keysByProviderID := make(map[uuid.UUID][]database.AIProviderKey, len(providers))
|
|
for _, key := range keys {
|
|
keysByProviderID[key.ProviderID] = append(keysByProviderID[key.ProviderID], key)
|
|
}
|
|
configuredProviders := make([]chatprovider.ConfiguredProvider, 0, len(providers))
|
|
for _, provider := range providers {
|
|
configuredProvider, err := p.aiProviderConfigFromKeys(provider, keysByProviderID[provider.ID])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
configuredProviders = append(configuredProviders, configuredProvider)
|
|
}
|
|
return configuredProviders, nil
|
|
}
|
|
|
|
func ensureUniqueConfiguredProviderTypes(providers []chatprovider.ConfiguredProvider) error {
|
|
seen := make(map[string]uuid.UUID, len(providers))
|
|
for _, provider := range providers {
|
|
normalizedProvider := chatprovider.NormalizeProvider(provider.Provider)
|
|
if normalizedProvider == "" {
|
|
continue
|
|
}
|
|
if existingProviderID, ok := seen[normalizedProvider]; ok && existingProviderID != provider.ProviderID {
|
|
return xerrors.Errorf("multiple enabled AI providers use provider type %q; select an AI provider by ID", normalizedProvider)
|
|
}
|
|
seen[normalizedProvider] = provider.ProviderID
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Server) resolveUserProviderAPIKeysForProvider(
|
|
ctx context.Context,
|
|
ownerID uuid.UUID,
|
|
provider database.AIProvider,
|
|
) (chatprovider.ProviderAPIKeys, error) {
|
|
configuredProvider, err := p.aiProviderConfig(ctx, provider)
|
|
if err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, err
|
|
}
|
|
userKeys := []chatprovider.UserProviderKey{}
|
|
if p.allowBYOK {
|
|
userKey, err := p.db.GetUserAIProviderKeyByProviderID(ctx, database.GetUserAIProviderKeyByProviderIDParams{
|
|
UserID: ownerID,
|
|
AIProviderID: provider.ID,
|
|
})
|
|
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
|
|
return chatprovider.ProviderAPIKeys{}, xerrors.Errorf("get user AI provider key: %w", err)
|
|
}
|
|
if err == nil {
|
|
userKeys = append(userKeys, chatprovider.UserProviderKey{
|
|
ChatProviderID: userKey.AIProviderID,
|
|
APIKey: userKey.APIKey,
|
|
})
|
|
}
|
|
}
|
|
keys, _ := chatprovider.ResolveUserProviderKeys(
|
|
chatprovider.ProviderAPIKeys{},
|
|
[]chatprovider.ConfiguredProvider{configuredProvider},
|
|
userKeys,
|
|
)
|
|
return keys, nil
|
|
}
|
|
|
|
func (p *Server) resolveUserProviderAPIKeys(
|
|
ctx context.Context,
|
|
ownerID uuid.UUID,
|
|
selectedAIProviderID uuid.UUID,
|
|
) (chatprovider.ProviderAPIKeys, error) {
|
|
if selectedAIProviderID != uuid.Nil {
|
|
provider, err := p.db.GetAIProviderByID(ctx, selectedAIProviderID)
|
|
if err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, xerrors.Errorf("get AI provider: %w", err)
|
|
}
|
|
return p.resolveUserProviderAPIKeysForProvider(ctx, ownerID, provider)
|
|
}
|
|
|
|
providers, err := p.configCache.EnabledProviders(ctx)
|
|
if err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, xerrors.Errorf(
|
|
"get enabled AI providers: %w",
|
|
err,
|
|
)
|
|
}
|
|
configuredProviders, err := p.aiProviderConfigs(ctx, providers)
|
|
if err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, err
|
|
}
|
|
if err := ensureUniqueConfiguredProviderTypes(configuredProviders); err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, err
|
|
}
|
|
|
|
userKeys := []chatprovider.UserProviderKey{}
|
|
if p.allowBYOK {
|
|
userKeyRows, err := p.db.GetUserAIProviderKeysByUserID(ctx, ownerID)
|
|
if err != nil {
|
|
return chatprovider.ProviderAPIKeys{}, xerrors.Errorf(
|
|
"get user AI provider keys: %w",
|
|
err,
|
|
)
|
|
}
|
|
userKeys = make([]chatprovider.UserProviderKey, 0, len(userKeyRows))
|
|
for _, userKey := range userKeyRows {
|
|
userKeys = append(userKeys, chatprovider.UserProviderKey{
|
|
ChatProviderID: userKey.AIProviderID,
|
|
APIKey: userKey.APIKey,
|
|
})
|
|
}
|
|
}
|
|
|
|
keys, _ := chatprovider.ResolveUserProviderKeys(
|
|
p.providerAPIKeys,
|
|
configuredProviders,
|
|
userKeys,
|
|
)
|
|
enabledProviders := make(map[string]struct{}, len(configuredProviders))
|
|
for _, provider := range configuredProviders {
|
|
normalizedProvider := chatprovider.NormalizeProvider(provider.Provider)
|
|
if normalizedProvider == "" {
|
|
continue
|
|
}
|
|
enabledProviders[normalizedProvider] = struct{}{}
|
|
}
|
|
chatprovider.PruneDisabledProviderKeys(&keys, enabledProviders)
|
|
return keys, nil
|
|
}
|
|
|
|
var errModelConfigOutsideOrganization = xerrors.Errorf("%w: model config belongs to another organization", sql.ErrNoRows)
|
|
|
|
func (p *Server) resolveModelConfigForOrganization(
|
|
ctx context.Context,
|
|
ownerID uuid.UUID,
|
|
organizationID uuid.UUID,
|
|
modelConfigID uuid.UUID,
|
|
) (database.ChatModelConfig, string, error) {
|
|
modelConfig, providerName, err := p.resolveModelConfigAndNormalizedProvider(ctx, ownerID, modelConfigID)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, "", err
|
|
}
|
|
if modelConfig.OrganizationID != organizationID {
|
|
return database.ChatModelConfig{}, "", errModelConfigOutsideOrganization
|
|
}
|
|
return modelConfig, providerName, nil
|
|
}
|
|
|
|
// resolveModelConfig looks up the chat's enabled model config by its
|
|
// LastModelConfigID. If the referenced config is unavailable or belongs to
|
|
// another organization, it falls back to the local default model config.
|
|
// Returns an error when no usable local config is available.
|
|
func (p *Server) resolveModelConfig(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
) (database.ChatModelConfig, error) {
|
|
modelCtx, err := p.callerModelConfigContext(ctx, chat.OwnerID)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, err
|
|
}
|
|
if chat.LastModelConfigID != uuid.Nil {
|
|
modelConfig, err := p.db.GetEnabledChatModelConfigByID(
|
|
modelCtx,
|
|
chat.LastModelConfigID,
|
|
)
|
|
if err == nil {
|
|
if modelConfig.OrganizationID == chat.OrganizationID {
|
|
return modelConfig, nil
|
|
}
|
|
err = sql.ErrNoRows
|
|
}
|
|
if !xerrors.Is(err, sql.ErrNoRows) && !dbauthz.IsNotAuthorizedError(err) {
|
|
return database.ChatModelConfig{}, xerrors.Errorf(
|
|
"get chat model config %s: %w",
|
|
chat.LastModelConfigID, err,
|
|
)
|
|
}
|
|
// The model config is unavailable or belongs to another organization.
|
|
// Fall through to the local default.
|
|
}
|
|
|
|
defaultConfig, err := effectiveDefaultChatModelConfig(modelCtx, p.db, chat.OrganizationID)
|
|
if err != nil {
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
|
return database.ChatModelConfig{}, ErrNoDefaultChatModelConfig
|
|
}
|
|
return database.ChatModelConfig{}, xerrors.Errorf(
|
|
"get default chat model config: %w", err,
|
|
)
|
|
}
|
|
return defaultConfig, nil
|
|
}
|
|
|
|
func refreshChatWorkspaceSnapshot(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
loadChat func(context.Context, uuid.UUID) (database.Chat, error),
|
|
) (database.Chat, error) {
|
|
if chat.WorkspaceID.Valid || loadChat == nil {
|
|
return chat, nil
|
|
}
|
|
|
|
refreshedChat, err := loadChat(ctx, chat.ID)
|
|
if err != nil {
|
|
return chat, xerrors.Errorf("reload chat workspace state: %w", err)
|
|
}
|
|
|
|
return refreshedChat, nil
|
|
}
|
|
|
|
// resolveUserCompactionThreshold looks up the user's per-model
|
|
// compaction threshold override. Returns the override value and
|
|
// true if one exists and is valid, or 0 and false otherwise.
|
|
func (p *Server) resolveUserCompactionThreshold(ctx context.Context, userID uuid.UUID, modelConfigID uuid.UUID) (int32, bool, error) {
|
|
raw, err := p.db.GetUserChatCompactionThreshold(ctx, database.GetUserChatCompactionThresholdParams{
|
|
UserID: userID,
|
|
Key: codersdk.CompactionThresholdKey(modelConfigID),
|
|
})
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, false, nil
|
|
}
|
|
if err != nil {
|
|
return 0, false, xerrors.Errorf("get user compaction threshold: %w", err)
|
|
}
|
|
// Range 0..100 must stay in sync with handler validation in
|
|
// coderd/chats.go.
|
|
val, err := strconv.ParseInt(raw, 10, 32)
|
|
if err != nil || val < 0 || val > 100 {
|
|
return 0, false, nil
|
|
}
|
|
return int32(val), true, nil
|
|
}
|
|
|
|
// resolveDeploymentSystemPrompt builds the deployment-level system
|
|
// prompt from the built-in default and the admin-configured custom
|
|
// prompt stored in site_configs.
|
|
func (p *Server) resolveDeploymentSystemPrompt(ctx context.Context) (string, error) {
|
|
config, err := p.db.GetChatSystemPromptConfig(ctx)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get chat system prompt config: %w", err)
|
|
}
|
|
|
|
sanitizedCustom := codersdk.SanitizePromptText(config.ChatSystemPrompt)
|
|
if sanitizedCustom == "" && strings.TrimSpace(config.ChatSystemPrompt) != "" {
|
|
p.logger.Warn(ctx, "custom system prompt became empty after sanitization, omitting custom portion")
|
|
}
|
|
|
|
var parts []string
|
|
if config.IncludeDefaultSystemPrompt {
|
|
parts = append(parts, DefaultSystemPrompt)
|
|
}
|
|
if sanitizedCustom != "" {
|
|
parts = append(parts, sanitizedCustom)
|
|
}
|
|
result := strings.Join(parts, "\n\n")
|
|
if result == "" {
|
|
p.logger.Warn(ctx, "resolved system prompt is empty, no system prompt will be injected into chats")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// resolveOrganizationSystemPrompt returns the sanitized system prompt
|
|
// configured for the organization, or an empty string when none is set.
|
|
func (p *Server) resolveOrganizationSystemPrompt(ctx context.Context, organizationID uuid.UUID) (string, error) {
|
|
//nolint:gocritic // Chat creators cannot read organization config, so chatd reads it.
|
|
row, err := p.db.GetChatOrganizationSystemPrompt(dbauthz.AsChatd(ctx), organizationID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", nil
|
|
}
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get organization chat system prompt: %w", err)
|
|
}
|
|
return codersdk.SanitizePromptText(row.SystemPrompt), nil
|
|
}
|
|
|
|
// resolveUserPrompt fetches the user's custom chat prompt from the
|
|
// database and wraps it in <user-instructions> tags. Returns empty
|
|
// string if no prompt is set.
|
|
func (p *Server) resolveUserPrompt(ctx context.Context, userID uuid.UUID) (string, error) {
|
|
raw, err := p.configCache.UserPrompt(ctx, userID)
|
|
if err != nil {
|
|
return "", xerrors.Errorf("get user chat prompt: %w", err)
|
|
}
|
|
trimmed := strings.TrimSpace(raw)
|
|
if trimmed == "" {
|
|
return "", nil
|
|
}
|
|
return "<user-instructions>\n" + trimmed + "\n</user-instructions>", nil
|
|
}
|
|
|
|
// renderPlanPathPrompt fills the plan-path placeholder when it is
|
|
// present in the prompt.
|
|
func renderPlanPathPrompt(prompt []fantasy.Message, planPathBlock string) []fantasy.Message {
|
|
prompt, _ = replacePlanPathPlaceholder(prompt, planPathBlock)
|
|
return prompt
|
|
}
|
|
|
|
func replacePlanPathPlaceholder(
|
|
prompt []fantasy.Message,
|
|
planPathBlock string,
|
|
) ([]fantasy.Message, bool) {
|
|
var updatedPrompt []fantasy.Message
|
|
replaced := false
|
|
for i, message := range prompt {
|
|
updatedMessage, ok := replacePlanPathPlaceholderInMessage(message, planPathBlock)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if updatedPrompt == nil {
|
|
updatedPrompt = slices.Clone(prompt)
|
|
}
|
|
updatedPrompt[i] = updatedMessage
|
|
replaced = true
|
|
}
|
|
if !replaced {
|
|
return prompt, false
|
|
}
|
|
return updatedPrompt, true
|
|
}
|
|
|
|
func replacePlanPathPlaceholderInMessage(
|
|
message fantasy.Message,
|
|
planPathBlock string,
|
|
) (fantasy.Message, bool) {
|
|
if message.Role != fantasy.MessageRoleSystem {
|
|
return message, false
|
|
}
|
|
|
|
content := slices.Clone(message.Content)
|
|
replaced := false
|
|
for i, part := range content {
|
|
textPart, ok := fantasy.AsMessagePart[fantasy.TextPart](part)
|
|
if !ok || !strings.Contains(textPart.Text, defaultSystemPromptPlanPathBlockPlaceholder) {
|
|
continue
|
|
}
|
|
replaced = true
|
|
content[i] = fantasy.TextPart{Text: strings.ReplaceAll(
|
|
textPart.Text,
|
|
defaultSystemPromptPlanPathBlockPlaceholder,
|
|
planPathBlock,
|
|
)}
|
|
}
|
|
if !replaced {
|
|
return message, false
|
|
}
|
|
message.Content = content
|
|
return message, true
|
|
}
|
|
|
|
func formatPlanPathBlock(chatPath, home string) string {
|
|
chatPath = strings.TrimSpace(chatPath)
|
|
if chatPath == "" {
|
|
return ""
|
|
}
|
|
|
|
avoidPlanPath := chattool.LegacySharedPlanPath
|
|
home = strings.TrimSpace(home)
|
|
if home != "" {
|
|
avoidPlanPath = strings.TrimRight(home, "/") + "/PLAN.md"
|
|
}
|
|
|
|
var b strings.Builder
|
|
_, _ = b.WriteString("<plan-file-path>\n")
|
|
_, _ = b.WriteString("Your plan file path for this chat is: ")
|
|
_, _ = b.WriteString(chatPath)
|
|
_, _ = b.WriteString("\n")
|
|
_, _ = b.WriteString("Always use this exact path when creating or proposing plan files. Do not use ")
|
|
_, _ = b.WriteString(avoidPlanPath)
|
|
_, _ = b.WriteString(".\n")
|
|
_, _ = b.WriteString("</plan-file-path>")
|
|
return b.String()
|
|
}
|
|
|
|
// parseDynamicToolNames unmarshals the dynamic tools JSON column
|
|
// and returns a map of tool names. This centralizes the repeated
|
|
// pattern of deserializing DynamicTools into a name set.
|
|
func parseDynamicToolNames(raw pqtype.NullRawMessage) (map[string]bool, error) {
|
|
if !raw.Valid || len(raw.RawMessage) == 0 {
|
|
return make(map[string]bool), nil
|
|
}
|
|
var tools []codersdk.DynamicTool
|
|
if err := json.Unmarshal(raw.RawMessage, &tools); err != nil {
|
|
return nil, xerrors.Errorf("unmarshal dynamic tools: %w", err)
|
|
}
|
|
names := make(map[string]bool, len(tools))
|
|
for _, t := range tools {
|
|
names[t.Name] = true
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
// maybeFinalizeTurnStatusLabelAndPush updates the cached turn status label
|
|
// for parent chats and optionally sends a web push notification.
|
|
func (p *Server) maybeFinalizeTurnStatusLabelAndPush(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
status database.ChatStatus,
|
|
lastError string,
|
|
runResult runChatResult,
|
|
logger slog.Logger,
|
|
) {
|
|
if chat.ParentChatID.Valid {
|
|
// Subagent chats skip turn status labels and generated
|
|
// summaries, but a successful turn's final report doubles as
|
|
// the chat summary so subagents are not summary-less.
|
|
if turnFinished(status) {
|
|
p.storeSubagentReportSummaryAsync(ctx, chat, logger)
|
|
}
|
|
return
|
|
}
|
|
|
|
switch status {
|
|
case database.ChatStatusWaiting, database.ChatStatusPaused:
|
|
p.finalizeSuccessfulTurnStatusLabelAndPush(ctx, chat, status, runResult, logger)
|
|
p.maybeGenerateChatSummaryAsync(ctx, logger, chat)
|
|
|
|
case database.ChatStatusError:
|
|
p.clearLastTurnSummaryAsync(ctx, chat, logger)
|
|
if p.webpushConfigured() {
|
|
pushBody := fallbackTurnStatusLabel(status)
|
|
if lastError != "" {
|
|
pushBody = lastError
|
|
}
|
|
p.dispatchPush(ctx, chat, pushBody, status, logger)
|
|
}
|
|
|
|
case database.ChatStatusRequiresAction:
|
|
p.setLastTurnSummaryAsync(ctx, chat, fallbackTurnStatusLabel(status), logger)
|
|
|
|
default:
|
|
// New statuses must be classified before they can safely
|
|
// preserve or finalize a cached turn status label.
|
|
p.clearLastTurnSummaryAsync(ctx, chat, logger)
|
|
}
|
|
}
|
|
|
|
func (p *Server) finalizeSuccessfulTurnStatusLabelAndPush(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
status database.ChatStatus,
|
|
runResult runChatResult,
|
|
logger slog.Logger,
|
|
) {
|
|
p.finalizeSuccessfulTurnStatusLabelWithAfterFunc(ctx, chat, status, runResult, logger, func(finalizeCtx context.Context, statusLabel string) {
|
|
p.dispatchSuccessfulTurnPush(finalizeCtx, chat, statusLabel, logger)
|
|
})
|
|
}
|
|
|
|
func (p *Server) finalizeSuccessfulTurnStatusLabelWithAfterFunc(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
status database.ChatStatus,
|
|
runResult runChatResult,
|
|
logger slog.Logger,
|
|
afterFinalize func(context.Context, string),
|
|
) {
|
|
finalizeCtx, stopFinalizeCtx := p.inflightChatContext(ctx, chat)
|
|
if err := p.goInflight(func() {
|
|
defer stopFinalizeCtx()
|
|
statusLabel := p.generateFinalTurnStatusLabel(finalizeCtx, chat, status, runResult, logger)
|
|
logger.Debug(finalizeCtx, "generated chat turn status label",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("status", status),
|
|
slog.F("label_length", len(statusLabel)),
|
|
)
|
|
|
|
p.updateLastTurnSummary(finalizeCtx, chat, chat.HistoryVersion, statusLabel, logger)
|
|
|
|
afterFinalize(finalizeCtx, statusLabel)
|
|
}); err != nil {
|
|
stopFinalizeCtx()
|
|
logger.Error(context.WithoutCancel(ctx), "failed to schedule chat turn status finalization",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("status", status),
|
|
slog.Error(err),
|
|
)
|
|
}
|
|
}
|
|
|
|
func (p *Server) generateFinalTurnStatusLabel(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
status database.ChatStatus,
|
|
runResult runChatResult,
|
|
logger slog.Logger,
|
|
) string {
|
|
if !turnFinished(status) {
|
|
return fallbackTurnStatusLabel(status)
|
|
}
|
|
|
|
assistantText := strings.TrimSpace(runResult.FinalAssistantText)
|
|
if assistantText == "" || runResult.StatusLabelCall == nil {
|
|
return fallbackTurnStatusLabel(status)
|
|
}
|
|
|
|
statusLabel := generateTurnStatusLabel(
|
|
ctx,
|
|
chat,
|
|
status,
|
|
assistantText,
|
|
*runResult.StatusLabelCall,
|
|
p.chatLimits.MaxGenerationRetries,
|
|
logger,
|
|
p.existingDebugService(),
|
|
runResult.TriggerMessageID,
|
|
runResult.HistoryTipMessageID,
|
|
)
|
|
if statusLabel == "" {
|
|
return fallbackTurnStatusLabel(status)
|
|
}
|
|
return statusLabel
|
|
}
|
|
|
|
func (p *Server) dispatchSuccessfulTurnPush(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
statusLabel string,
|
|
logger slog.Logger,
|
|
) {
|
|
if !p.webpushConfigured() {
|
|
return
|
|
}
|
|
pushBody := fallbackTurnStatusLabel(database.ChatStatusWaiting)
|
|
if statusLabel != "" {
|
|
pushBody = statusLabel
|
|
}
|
|
p.dispatchPush(ctx, chat, pushBody, database.ChatStatusWaiting, logger)
|
|
}
|
|
|
|
func (p *Server) setLastTurnSummaryAsync(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
summary string,
|
|
logger slog.Logger,
|
|
) {
|
|
summary = strings.TrimSpace(summary)
|
|
if summary == "" {
|
|
p.clearLastTurnSummaryAsync(ctx, chat, logger)
|
|
return
|
|
}
|
|
if chat.LastTurnSummary.Valid && strings.TrimSpace(chat.LastTurnSummary.String) == summary {
|
|
return
|
|
}
|
|
updateCtx, stopUpdateCtx := p.inflightChatContext(ctx, chat)
|
|
if err := p.goInflight(func() {
|
|
defer stopUpdateCtx()
|
|
p.updateLastTurnSummary(updateCtx, chat, chat.HistoryVersion, summary, logger)
|
|
}); err != nil {
|
|
stopUpdateCtx()
|
|
logger.Error(context.WithoutCancel(ctx), "failed to schedule chat turn summary update",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("expected_history_version", chat.HistoryVersion),
|
|
slog.F("summary_length", len(summary)),
|
|
slog.Error(err),
|
|
)
|
|
}
|
|
}
|
|
|
|
func (p *Server) clearLastTurnSummaryAsync(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
logger slog.Logger,
|
|
) {
|
|
clearCtx, stopClearCtx := p.inflightChatContext(ctx, chat)
|
|
if err := p.goInflight(func() {
|
|
defer stopClearCtx()
|
|
p.updateLastTurnSummary(clearCtx, chat, chat.HistoryVersion, "", logger)
|
|
}); err != nil {
|
|
stopClearCtx()
|
|
logger.Error(context.WithoutCancel(ctx), "failed to schedule chat turn summary clear",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("expected_history_version", chat.HistoryVersion),
|
|
slog.Error(err),
|
|
)
|
|
}
|
|
}
|
|
|
|
// updateLastTurnSummary writes the cached sidebar summary for a chat.
|
|
// Callers should pass a detached context because this method is used for
|
|
// best-effort background cache writes.
|
|
func (p *Server) updateLastTurnSummary(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
expectedHistoryVersion int64,
|
|
summary string,
|
|
logger slog.Logger,
|
|
) {
|
|
summary = strings.TrimSpace(summary)
|
|
lastTurnSummary := sql.NullString{String: summary, Valid: summary != ""}
|
|
|
|
//nolint:gocritic // Narrow daemon access for best-effort summary cache writes.
|
|
updateCtx := dbauthz.AsChatd(ctx)
|
|
updateCtx, cancel := context.WithTimeout(updateCtx, turnStatusLabelWriteTimeout)
|
|
defer cancel()
|
|
|
|
affected, err := p.db.UpdateChatLastTurnSummary(updateCtx, database.UpdateChatLastTurnSummaryParams{
|
|
ID: chat.ID,
|
|
ExpectedHistoryVersion: expectedHistoryVersion,
|
|
LastTurnSummary: lastTurnSummary,
|
|
})
|
|
if err != nil {
|
|
logger.Warn(updateCtx, "failed to update chat turn summary",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.Error(err),
|
|
)
|
|
return
|
|
}
|
|
if affected == 0 {
|
|
if summary != "" {
|
|
logger.Info(updateCtx, "skipped stale chat turn summary update with non-empty summary",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("summary_length", len(summary)),
|
|
slog.F("expected_history_version", expectedHistoryVersion),
|
|
)
|
|
return
|
|
}
|
|
logger.Debug(updateCtx, "skipped stale chat turn summary update",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("expected_history_version", expectedHistoryVersion),
|
|
)
|
|
return
|
|
}
|
|
|
|
updatedChat := chat
|
|
updatedChat.LastTurnSummary = lastTurnSummary
|
|
p.publishChatPubsubEvent(updatedChat, codersdk.ChatWatchEventKindSummaryChange, nil)
|
|
}
|
|
|
|
const (
|
|
// Completed user turns before the first summary is generated.
|
|
summaryInitialTurnThreshold = 1
|
|
// New completed user turns before the summary is regenerated (since the last summary).
|
|
summaryStaleTurnThreshold = 3
|
|
summaryMinTranscriptRunes = 200
|
|
chatSummaryWorkTimeout = 120 * time.Second
|
|
chatSummaryGenerateTimeout = 60 * time.Second
|
|
chatSummaryWriteTimeout = 5 * time.Second
|
|
|
|
// Subagent summaries reuse the final report instead of generating
|
|
// text, so their work timeout only covers two database round trips.
|
|
subagentReportSummaryTimeout = 15 * time.Second
|
|
// Bound the extracted report snippet near the headline of the
|
|
// generated summaries that root chats get, so subagent and parent
|
|
// summary panels read the same.
|
|
subagentReportSummaryMaxRunes = 300
|
|
subagentReportSummaryMaxSentences = 3
|
|
)
|
|
|
|
// maybeGenerateChatSummaryAsync launches best-effort whole-chat summary
|
|
// generation in the background for a root chat.
|
|
func (p *Server) maybeGenerateChatSummaryAsync(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
chat database.Chat,
|
|
) {
|
|
if chat.ParentChatID.Valid {
|
|
return
|
|
}
|
|
ctx, cancel := p.inflightChatContext(ctx, chat)
|
|
if err := p.goInflight(func() {
|
|
defer cancel()
|
|
p.generateAndStoreChatSummary(ctx, logger, chat)
|
|
}); err != nil {
|
|
cancel()
|
|
logger.Debug(ctx, "skipped chat summary generation",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
}
|
|
}
|
|
|
|
// generateAndStoreChatSummary regenerates and persists the whole-chat summary
|
|
// when due. Best-effort; never clears an existing summary on failure.
|
|
func (p *Server) generateAndStoreChatSummary(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
chat database.Chat,
|
|
) {
|
|
ctx, cancel := context.WithTimeout(ctx, chatSummaryWorkTimeout)
|
|
defer cancel()
|
|
|
|
//nolint:gocritic // Narrow daemon access for best-effort summary generation.
|
|
ctx = dbauthz.AsChatd(ctx)
|
|
|
|
// If a turn commits after this read, the stale history_version makes the
|
|
// eventual summary write lose instead of omitting that newer turn.
|
|
chat, err := p.db.GetChatByID(ctx, chat.ID)
|
|
if err != nil {
|
|
logger.Debug(ctx, "failed to re-read chat for summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return
|
|
}
|
|
|
|
messages, err := p.db.GetChatMessagesForPromptByChatID(ctx, chat.ID)
|
|
if err != nil {
|
|
logger.Debug(ctx, "failed to load messages for chat summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return
|
|
}
|
|
|
|
if !shouldGenerateChatSummary(chat, messages) {
|
|
return
|
|
}
|
|
|
|
transcript := renderChatSummaryTranscript(messages)
|
|
if len([]rune(transcript)) < summaryMinTranscriptRunes {
|
|
logger.Debug(ctx, "skipping chat summary for short transcript",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("transcript_runes", len([]rune(transcript))),
|
|
)
|
|
return
|
|
}
|
|
|
|
// Derive the delegated API key from the chat owner so AI Gateway routing
|
|
// attributes summary generation to the correct account. This goroutine may
|
|
// outlive the launching turn, so it cannot rely on that turn's context.
|
|
apiKeyID, err := p.ensureSyntheticAPIKeyID(ctx, chat.OwnerID)
|
|
if err != nil {
|
|
logger.Debug(ctx, "failed to ensure synthetic API key for chat summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return
|
|
}
|
|
modelOpts := modelBuildOptions{ActiveAPIKeyID: apiKeyID}
|
|
|
|
resolved, ok := p.resolveChatSummaryModel(ctx, logger, chat, modelOpts)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
summaryCtx, cancelGen := context.WithTimeout(ctx, chatSummaryGenerateTimeout)
|
|
defer cancelGen()
|
|
summary, _, genErr := generateChatSummary(summaryCtx, resolved.model.LanguageModel(), summaryObjectCall(resolved), p.chatLimits.MaxGenerationRetries, transcript)
|
|
|
|
if genErr != nil {
|
|
logger.Warn(ctx, "failed to generate chat summary",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("provider", resolved.resolvedProvider),
|
|
slog.F("model", resolved.resolvedModel),
|
|
slog.Error(genErr),
|
|
)
|
|
return
|
|
}
|
|
|
|
p.updateChatSummary(ctx, logger, chat, chat.HistoryVersion, summary)
|
|
}
|
|
|
|
func (p *Server) resolveChatSummaryModel(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
chat database.Chat,
|
|
modelOpts modelBuildOptions,
|
|
) (resolvedModelCall, bool) {
|
|
resolved, err := p.resolveQuickgenModel(ctx, "chat_summary", chat, modelOpts)
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to resolve model for chat summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return resolvedModelCall{}, false
|
|
}
|
|
return resolved, true
|
|
}
|
|
|
|
func shouldGenerateChatSummary(chat database.Chat, messages []database.ChatMessage) bool {
|
|
if !chat.Summary.Valid {
|
|
return countCompletedTurnsSince(messages, time.Time{}) >= summaryInitialTurnThreshold
|
|
}
|
|
var marker time.Time
|
|
if chat.SummaryGeneratedAt.Valid {
|
|
marker = chat.SummaryGeneratedAt.Time
|
|
}
|
|
return countCompletedTurnsSince(messages, marker) >= summaryStaleTurnThreshold
|
|
}
|
|
|
|
// countCompletedTurnsSince counts visible user messages (one per turn) created
|
|
// after the given time. Model-only user messages (injected context, replayed
|
|
// compaction summary) are not turns; a zero time counts all.
|
|
func countCompletedTurnsSince(messages []database.ChatMessage, after time.Time) int {
|
|
count := 0
|
|
for _, message := range messages {
|
|
if message.Role != database.ChatMessageRoleUser {
|
|
continue
|
|
}
|
|
if message.Visibility != database.ChatMessageVisibilityBoth &&
|
|
message.Visibility != database.ChatMessageVisibilityUser {
|
|
continue
|
|
}
|
|
if !after.IsZero() && !message.CreatedAt.After(after) {
|
|
continue
|
|
}
|
|
count++
|
|
}
|
|
return count
|
|
}
|
|
|
|
// updateChatSummary persists the whole-chat summary. Best-effort background
|
|
// write (pass a detached context); a blank summary is a no-op, never clearing
|
|
// an existing one.
|
|
func (p *Server) updateChatSummary(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
chat database.Chat,
|
|
expectedHistoryVersion int64,
|
|
summary string,
|
|
) {
|
|
summary = strings.TrimSpace(summary)
|
|
if summary == "" {
|
|
return
|
|
}
|
|
sqlSummary := sql.NullString{String: summary, Valid: true}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, chatSummaryWriteTimeout)
|
|
defer cancel()
|
|
|
|
affected, err := p.db.UpdateChatSummary(ctx, database.UpdateChatSummaryParams{
|
|
ID: chat.ID,
|
|
ExpectedHistoryVersion: expectedHistoryVersion,
|
|
Summary: sqlSummary,
|
|
})
|
|
if err != nil {
|
|
logger.Warn(ctx, "failed to update chat summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return
|
|
}
|
|
if affected == 0 {
|
|
logger.Info(ctx, "skipped stale chat summary update",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("summary_length", len(summary)),
|
|
slog.F("expected_history_version", expectedHistoryVersion),
|
|
)
|
|
return
|
|
}
|
|
|
|
updatedChat := chat
|
|
updatedChat.Summary = sqlSummary
|
|
p.publishChatPubsubEvent(updatedChat, codersdk.ChatWatchEventKindChatSummaryChange, nil)
|
|
}
|
|
|
|
func (p *Server) storeSubagentReportSummaryAsync(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
logger slog.Logger,
|
|
) {
|
|
summaryCtx, stopSummaryCtx := p.inflightChatContext(ctx, chat)
|
|
if err := p.goInflight(func() {
|
|
defer stopSummaryCtx()
|
|
p.storeSubagentReportSummary(summaryCtx, chat, logger)
|
|
}); err != nil {
|
|
stopSummaryCtx()
|
|
logger.Debug(context.WithoutCancel(ctx), "skipped subagent report summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
}
|
|
}
|
|
|
|
func (p *Server) storeSubagentReportSummary(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
logger slog.Logger,
|
|
) {
|
|
ctx, cancel := context.WithTimeout(ctx, subagentReportSummaryTimeout)
|
|
defer cancel()
|
|
|
|
//nolint:gocritic // Narrow daemon access for best-effort summary writes.
|
|
authCtx := dbauthz.AsChatd(ctx)
|
|
report, err := latestSubagentAssistantMessage(authCtx, p.db, chat.ID)
|
|
if err != nil {
|
|
logger.Debug(ctx, "failed to load subagent report for summary",
|
|
slog.F("chat_id", chat.ID), slog.Error(err))
|
|
return
|
|
}
|
|
summary := subagentReportSummarySnippet(report)
|
|
if summary == "" {
|
|
return
|
|
}
|
|
p.updateChatSummary(ctx, logger, chat, chat.HistoryVersion, summary)
|
|
}
|
|
|
|
func (p *Server) webpushConfigured() bool {
|
|
return p.webpushDispatcher != nil && p.webpushDispatcher.PublicKey() != ""
|
|
}
|
|
|
|
func (p *Server) dispatchPush(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
body string,
|
|
status database.ChatStatus,
|
|
logger slog.Logger,
|
|
) {
|
|
pushMsg := codersdk.WebpushMessage{
|
|
Title: chat.Title,
|
|
Body: body,
|
|
Icon: "/favicon.ico",
|
|
Data: map[string]string{"url": fmt.Sprintf("/agents/%s", chat.ID)},
|
|
}
|
|
if err := p.webpushDispatcher.Dispatch(ctx, chat.OwnerID, pushMsg); err != nil {
|
|
logger.Warn(ctx, "failed to send chat completion web push",
|
|
slog.F("chat_id", chat.ID),
|
|
slog.F("status", status),
|
|
slog.Error(err),
|
|
)
|
|
}
|
|
}
|
|
|
|
// Close stops the processor and waits for it to finish.
|
|
func (p *Server) Close() error {
|
|
p.inflightClosed.Store(true)
|
|
if unsub := p.configCacheUnsubscribe; unsub != nil {
|
|
p.configCacheUnsubscribe = nil
|
|
unsub()
|
|
}
|
|
if unsub := p.providerCacheUnsubscribe; unsub != nil {
|
|
p.providerCacheUnsubscribe = nil
|
|
unsub()
|
|
}
|
|
if p.chatWorker != nil {
|
|
if err := p.chatWorker.Close(); err != nil {
|
|
p.logger.Warn(context.Background(), "failed to close chat worker", slog.Error(err))
|
|
}
|
|
}
|
|
if p.streamSyncPoller != nil {
|
|
p.streamSyncPoller.Close()
|
|
}
|
|
if p.messagePartBuffer != nil {
|
|
p.messagePartBuffer.Close()
|
|
}
|
|
p.cancel()
|
|
p.wg.Wait()
|
|
p.drainInflight()
|
|
return nil
|
|
}
|
|
|
|
// inflightContext returns a context for an in-flight goroutine launched
|
|
// via goInflight. It is detached from reqCtx's cancellation so the work
|
|
// can outlive the originating request, while preserving its values for
|
|
// auth, routing, and tracing. The context is bound to the server
|
|
// lifetime via p.ctx so Close cancels it promptly. The returned stop
|
|
// must be called once the work completes to release the shutdown hook.
|
|
// The caller is responsible for providing their own timeout.
|
|
func (p *Server) inflightContext(reqCtx context.Context) (context.Context, func()) {
|
|
// Inflight work outlives the caller, so its spans must not be
|
|
// children of the caller's span.
|
|
detached := trace.ContextWithSpanContext(context.WithoutCancel(reqCtx), trace.SpanContext{})
|
|
detached = chatloop.ContextWithScope(detached, chatloop.ScopeBackground)
|
|
ctx, cancel := context.WithCancel(detached)
|
|
stop := context.AfterFunc(p.ctx, cancel)
|
|
return ctx, func() {
|
|
stop()
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
// recordQueueWait records queue_wait as a trace root. Call it after the
|
|
// promoting transition commits; a zero queuedAt records nothing.
|
|
func (p *Server) recordQueueWait(ctx context.Context, chat database.Chat, queuedAt time.Time) {
|
|
if queuedAt.IsZero() {
|
|
return
|
|
}
|
|
standalone := trace.ContextWithSpanContext(ctx, trace.SpanContext{})
|
|
standalone = withStageIdentity(standalone, chatloop.ScopeTurn, chatKind(chat))
|
|
p.stages.Record(standalone, chatloop.StageQueueWait, chatloop.StageModel{},
|
|
queuedAt, p.stages.Now(), nil,
|
|
attribute.String(chatloop.AttrChatID, chat.ID.String()),
|
|
)
|
|
}
|
|
|
|
func (p *Server) inflightChatContext(reqCtx context.Context, chat database.Chat) (context.Context, func()) {
|
|
ctx, stop := p.inflightContext(reqCtx)
|
|
return withStageIdentity(ctx, chatloop.ScopeBackground, chatKind(chat)), stop
|
|
}
|
|
|
|
func chatKind(chat database.Chat) chatloop.ChatKind {
|
|
if chat.ParentChatID.Valid {
|
|
return chatloop.ChatKindSubagent
|
|
}
|
|
return chatloop.ChatKindRoot
|
|
}
|
|
|
|
func withStageIdentity(ctx context.Context, scope chatloop.Scope, kind chatloop.ChatKind) context.Context {
|
|
ctx = chatloop.ContextWithScope(ctx, scope)
|
|
return chatloop.ContextWithChatKind(ctx, kind)
|
|
}
|
|
|
|
func (p *Server) goInflight(f func()) error {
|
|
if p.inflightClosed.Load() {
|
|
return errInflightClosed
|
|
}
|
|
|
|
// Acquire inflightMu around the inflight.Go so Close() cannot
|
|
// call drainInflight concurrently when the counter is at zero.
|
|
// See drainInflight for the WaitGroup contract this preserves.
|
|
p.inflightMu.Lock()
|
|
defer p.inflightMu.Unlock()
|
|
if p.inflightClosed.Load() {
|
|
return errInflightClosed
|
|
}
|
|
p.inflight.Go(f)
|
|
return nil
|
|
}
|
|
|
|
// drainInflight waits for already-admitted in-flight operations to complete.
|
|
// It acquires inflightMu so Wait cannot race with a positive Add from
|
|
// goInflight when the WaitGroup counter is zero.
|
|
//
|
|
// https://pkg.go.dev/sync#WaitGroup.Add
|
|
// > Note that calls with a positive delta that occur when the counter is zero must happen before a Wait.
|
|
func (p *Server) drainInflight() {
|
|
p.inflightMu.Lock()
|
|
p.inflight.Wait()
|
|
p.inflightMu.Unlock()
|
|
}
|
|
|
|
// refreshExpiredMCPTokens checks each MCP OAuth2 token and refreshes
|
|
// any that are expired (or about to expire). Tokens without a
|
|
// refresh_token or that fail to refresh are returned unchanged so the
|
|
// caller can still attempt the connection (which will likely fail with
|
|
// a 401 for the expired ones).
|
|
func (p *Server) refreshExpiredMCPTokens(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
configs []database.MCPServerConfig,
|
|
tokens []database.MCPServerUserToken,
|
|
) ([]database.MCPServerUserToken, error) {
|
|
configsByID := make(map[uuid.UUID]database.MCPServerConfig, len(configs))
|
|
for _, cfg := range configs {
|
|
configsByID[cfg.ID] = cfg
|
|
}
|
|
|
|
result := slices.Clone(tokens)
|
|
|
|
var eg errgroup.Group
|
|
for i, tok := range result {
|
|
cfg, ok := configsByID[tok.MCPServerConfigID]
|
|
if !ok || cfg.AuthType != "oauth2" {
|
|
continue
|
|
}
|
|
if tok.RefreshToken == "" {
|
|
continue
|
|
}
|
|
if tok.OauthRefreshFailureReason != "" {
|
|
// A previous refresh already failed permanently (e.g.
|
|
// revoked grant); the user must reconnect. Skip the
|
|
// provider call entirely.
|
|
continue
|
|
}
|
|
|
|
eg.Go(func() error {
|
|
refreshed, err := p.refreshMCPTokenIfNeeded(ctx, logger, cfg, tok)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result[i] = refreshed
|
|
return nil
|
|
})
|
|
}
|
|
if err := eg.Wait(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// refreshMCPTokenIfNeeded delegates to mcpclient.RefreshOAuth2Token
|
|
// and persists the result to the database when a refresh occurs.
|
|
// A transient refresh failure is logged and keeps the current token,
|
|
// so every returned error is a database error.
|
|
// The logger should carry chat-scoped fields so log lines can be
|
|
// correlated with specific chat requests.
|
|
func (p *Server) refreshMCPTokenIfNeeded(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
cfg database.MCPServerConfig,
|
|
tok database.MCPServerUserToken,
|
|
) (database.MCPServerUserToken, error) {
|
|
result, err := mcpclient.RefreshOAuth2Token(ctx, p.mcpHTTPClient, cfg, tok)
|
|
if err != nil {
|
|
if mcpclient.IsPermanentRefreshError(err) {
|
|
return p.markMCPTokenRefreshFailure(ctx, logger, cfg, tok, err)
|
|
}
|
|
logger.Warn(ctx, "failed to refresh MCP oauth2 token",
|
|
slog.F("server_slug", cfg.Slug),
|
|
slog.Error(err),
|
|
)
|
|
return tok, nil
|
|
}
|
|
|
|
if !result.Refreshed {
|
|
return tok, nil
|
|
}
|
|
|
|
logger.Info(ctx, "refreshed MCP oauth2 token",
|
|
slog.F("server_slug", cfg.Slug),
|
|
slog.F("user_id", tok.UserID),
|
|
)
|
|
|
|
var expiry sql.NullTime
|
|
if !result.Expiry.IsZero() {
|
|
expiry = sql.NullTime{Time: result.Expiry, Valid: true}
|
|
}
|
|
|
|
// The chatd subject has no personal-write access; persist the
|
|
// refresh as a subject scoped to this token's owner.
|
|
updated, err := p.db.UpdateMCPServerUserTokenFromRefresh(
|
|
dbauthz.AsChatdTokenOwner(ctx, tok.UserID),
|
|
database.UpdateMCPServerUserTokenFromRefreshParams{
|
|
ID: tok.ID,
|
|
UpdatedAt: tok.UpdatedAt,
|
|
AccessToken: result.AccessToken,
|
|
AccessTokenKeyID: sql.NullString{},
|
|
RefreshToken: result.RefreshToken,
|
|
RefreshTokenKeyID: sql.NullString{},
|
|
TokenType: result.TokenType,
|
|
Expiry: expiry,
|
|
},
|
|
)
|
|
if err != nil {
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
|
// A disconnect or re-authentication can win the optimistic update.
|
|
current, readErr := p.db.GetMCPServerUserToken(
|
|
ctx,
|
|
database.GetMCPServerUserTokenParams{
|
|
MCPServerConfigID: tok.MCPServerConfigID,
|
|
UserID: tok.UserID,
|
|
},
|
|
)
|
|
if readErr == nil {
|
|
return current, nil
|
|
}
|
|
if !xerrors.Is(readErr, sql.ErrNoRows) {
|
|
return database.MCPServerUserToken{}, xerrors.Errorf("get MCP oauth2 token after refresh conflict: %w", readErr)
|
|
}
|
|
tok.AccessToken = ""
|
|
tok.RefreshToken = ""
|
|
tok.Expiry = sql.NullTime{}
|
|
return tok, nil
|
|
}
|
|
return database.MCPServerUserToken{}, xerrors.Errorf("persist refreshed MCP oauth2 token: %w", err)
|
|
}
|
|
|
|
return updated, nil
|
|
}
|
|
|
|
// markMCPTokenRefreshFailure persists a permanent refresh failure
|
|
// (e.g. the upstream grant was revoked) so the dead token is never
|
|
// attached again and the UI can prompt the user to reconnect.
|
|
func (p *Server) markMCPTokenRefreshFailure(
|
|
ctx context.Context,
|
|
logger slog.Logger,
|
|
cfg database.MCPServerConfig,
|
|
tok database.MCPServerUserToken,
|
|
refreshErr error,
|
|
) (database.MCPServerUserToken, error) {
|
|
logger.Warn(ctx, "mcp oauth2 grant permanently unusable, marking token for reconnect",
|
|
slog.F("server_slug", cfg.Slug),
|
|
slog.F("user_id", tok.UserID),
|
|
slog.Error(refreshErr),
|
|
)
|
|
|
|
// The chatd subject has no personal-write access; persist the
|
|
// failure as a subject scoped to this token's owner.
|
|
marked, err := p.db.MarkMCPServerUserTokenRefreshFailure(
|
|
dbauthz.AsChatdTokenOwner(ctx, tok.UserID),
|
|
database.MarkMCPServerUserTokenRefreshFailureParams{
|
|
ID: tok.ID,
|
|
UpdatedAt: tok.UpdatedAt,
|
|
OauthRefreshFailureReason: mcpclient.RefreshFailureReason(refreshErr),
|
|
},
|
|
)
|
|
if err == nil {
|
|
return marked, nil
|
|
}
|
|
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
|
// Optimistic lock miss: a concurrent request refreshed or
|
|
// replaced the token after we read it, so our failure is
|
|
// stale. Use the winner's row instead.
|
|
current, readErr := p.db.GetMCPServerUserToken(
|
|
ctx,
|
|
database.GetMCPServerUserTokenParams{
|
|
MCPServerConfigID: tok.MCPServerConfigID,
|
|
UserID: tok.UserID,
|
|
},
|
|
)
|
|
if readErr == nil {
|
|
return current, nil
|
|
}
|
|
if !xerrors.Is(readErr, sql.ErrNoRows) {
|
|
return database.MCPServerUserToken{}, xerrors.Errorf("get MCP oauth2 token after refresh failure conflict: %w", readErr)
|
|
}
|
|
tok.AccessToken = ""
|
|
tok.RefreshToken = ""
|
|
tok.Expiry = sql.NullTime{}
|
|
tok.OauthRefreshFailureReason = mcpclient.RefreshFailureReason(refreshErr)
|
|
return tok, nil
|
|
}
|
|
return database.MCPServerUserToken{}, xerrors.Errorf("mark MCP oauth2 token refresh failure: %w", err)
|
|
}
|