mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
Adds an admin-configurable deployment-wide setting that controls which
model is used for chat title generation. Admins can pick any enabled
chat model config from the Agents settings page, or leave the setting
unset to keep the existing fast-models-then-chat-model fallback
algorithm.
When a model is selected, both automatic and manual title generation use
only that model, with no silent fallback. When the configured model is
disabled, missing credentials, or otherwise unusable, automatic title
generation skips entirely (best-effort) and manual title regeneration
returns a clear error, so admins notice the misconfiguration instead of
silently routing title traffic through another provider.
## Surface
- New deployment-wide setting stored as a `site_configs` row
(`agents_chat_title_generation_model_override`).
- New experimental endpoint `GET/PUT
/api/experimental/chats/config/model-override/{context}`.
- Frontend: title generation now appears as a third dropdown on the
Agents admin settings page alongside the existing general and explore
context overrides.
## DRY refactors folded in
Title generation is integrated as a third value of the existing
`ChatModelOverrideContext` type alongside `general` and `explore`,
sharing the parameterized HTTP route, SDK methods, generated types, and
frontend API plumbing rather than introducing a parallel surface. The
`Agent` prefix was dropped from the type and route since title
generation is not a delegated agent.
The chatd model-override resolver is also shared.
`resolveConfiguredModelOverride` now takes a `failureMode` parameter:
- Subagent overrides use soft failure: misconfigured overrides are
logged and the parent model is used.
- Title generation uses hard failure: misconfigured overrides return an
explicit error so manual title regeneration surfaces the
misconfiguration and automatic title generation skips instead of
silently falling back.
> Mux is acting on Mike's behalf.
101 lines
2.9 KiB
Go
101 lines
2.9 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
|
|
"charm.land/fantasy"
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
)
|
|
|
|
const titleGenerationOverrideContext = "title_generation"
|
|
|
|
func readTitleGenerationModelOverride(
|
|
ctx context.Context,
|
|
db database.Store,
|
|
) (string, error) {
|
|
//nolint:gocritic // Chatd is internal, not a user, so this read uses AsChatd.
|
|
chatdCtx := dbauthz.AsChatd(ctx)
|
|
raw, err := db.GetChatTitleGenerationModelOverride(chatdCtx)
|
|
if err != nil {
|
|
return "", xerrors.Errorf(
|
|
"get chat title generation model override: %w",
|
|
err,
|
|
)
|
|
}
|
|
return raw, nil
|
|
}
|
|
|
|
// resolveTitleGenerationModelOverride resolves the deployment-wide title
|
|
// generation model override. It returns four values:
|
|
//
|
|
// - modelConfig and model: populated only on success.
|
|
// - overrideSet: true when the admin configured a non-empty override,
|
|
// regardless of whether resolution succeeded. Callers MUST always check
|
|
// err first; overrideSet alone does not imply the model is usable.
|
|
// - err: non-nil when resolution failed. DB read failure returns
|
|
// (zero, nil, false, err). With overrideSet=true, the override is
|
|
// configured but unusable (deleted model, missing credentials, etc.) and
|
|
// callers should treat this as a hard failure for explicit-override
|
|
// semantics, not a soft fallback.
|
|
//
|
|
// When the override is unset or stored as malformed, the function returns
|
|
// (zero, nil, false, nil) so callers can fall back to default behavior.
|
|
func (p *Server) resolveTitleGenerationModelOverride(
|
|
ctx context.Context,
|
|
chat database.Chat,
|
|
keys chatprovider.ProviderAPIKeys,
|
|
) (database.ChatModelConfig, fantasy.LanguageModel, bool, error) {
|
|
raw, err := readTitleGenerationModelOverride(ctx, p.db)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, nil, false, xerrors.Errorf(
|
|
"read title generation model override: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
modelConfig, overrideSet, err := p.resolveConfiguredModelOverride(
|
|
ctx,
|
|
titleGenerationOverrideContext,
|
|
raw,
|
|
chat.OwnerID,
|
|
p.resolveModelConfigAndNormalizedProvider,
|
|
func(context.Context, uuid.UUID) (chatprovider.ProviderAPIKeys, error) {
|
|
return keys, nil
|
|
},
|
|
modelOverrideFailureModeHard,
|
|
)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, nil, overrideSet, err
|
|
}
|
|
if !overrideSet {
|
|
return database.ChatModelConfig{}, nil, false, nil
|
|
}
|
|
|
|
model, err := chatprovider.ModelFromConfig(
|
|
modelConfig.Provider,
|
|
modelConfig.Model,
|
|
keys,
|
|
chatprovider.UserAgent(),
|
|
chatprovider.CoderHeaders(chat),
|
|
nil,
|
|
)
|
|
if err != nil {
|
|
return database.ChatModelConfig{}, nil, true, xerrors.Errorf(
|
|
"create title generation model override: %w",
|
|
err,
|
|
)
|
|
}
|
|
if model == nil {
|
|
return database.ChatModelConfig{}, nil, true, xerrors.Errorf(
|
|
"create title generation model override returned nil",
|
|
)
|
|
}
|
|
|
|
return modelConfig, model, true, nil
|
|
}
|