> [!NOTE]
> Xum opened this PR for Mike (Coder agent). Mike reviews it before
merge.
Fixes CODAGT-1472.
Computer use now runs on a chat model that each organization picks.
Before, it ran a fixed model on the first enabled AI provider of a
deployment-wide provider type, sorted by name. On dogfood that
provider's key is rejected, so every computer use subagent failed.
## What changed
- **Setting.** A new `computer_use` organization model override, set in
the **Computer use** row of **Organization settings** on the Coder
Agents admin page. Without an override, computer use runs on the
organization's default model. If the chosen model is disabled, deleted,
or unusable for the chat owner, computer use falls back to the default
model, as other overrides do. It also falls back when the chosen model
moves to an AI provider that is not Anthropic or OpenAI, and the row
then says that the saved model is ignored.
- **API.** `PUT
/api/v2/organizations/{organization}/chats/model-overrides/computer_use`
returns 404 while the `chat-virtual-desktop` experiment is off, and 400
unless the model's AI provider type is Anthropic or OpenAI.
- **chatd.** The `computer_use` subagent runs on that model, and the
computer tool flavor follows the model's provider type. OpenAI computer
use always uses the Responses API, so a model that sets
`use_responses_api: false` gets a configuration error. When no usable
model exists, `computer_use` is unavailable, and the reason says whether
a saved computer use model was skipped and why the default model cannot
be used. The default model is read under the chat owner's model ACLs, as
chat turns do.
- **UI.** The row lists only enabled Anthropic and OpenAI models. A
warning icon with the tooltip "Make sure that the selected model
supports computer use." appears when the model in effect (the selected
one, or the default) is not on a curated list of canonical model IDs
known to accept the computer tool (`knownComputerUseModels` in
`knownModels/catalog.ts`, exact match only). It also appears when the
model's AI provider type is OpenAI and its **Use Responses API**
override is Off, because OpenAI computer use needs the Responses API.
The warning does not block saving. I built the list from a live probe on
2026-10-26 that sent each Anthropic and OpenAI model one request with
the computer tool.
- **Docs.** `virtual-desktop.md`, `models.md`, `organizations.md`, and
`architecture.md` describe the new setting, and chatd's
`ARCHITECTURE.md` describes the computer use model routing.


## Breaking change
This removes the deployment-wide computer use provider setting
(`agents_computer_use_provider`), its experimental `GET`/`PUT
/api/experimental/chats/config/computer-use-provider` endpoints (shipped
since v2.35), the Virtual desktop settings card, and its audit field.
Migration 000635 deletes the stored value. After the upgrade, computer
use runs on each organization's default model until an admin picks a
computer use model. Computer use subagents created before the upgrade
keep the model they inherited from their parent chat, so a follow-up
message to one of them can fail; a new computer use subagent uses the
organization's computer use model. Telemetry now reports the
per-organization overrides instead of the provider, and `null` when it
cannot read them.
## Testing
- `make pre-commit` passes. `go test` passes for `./coderd/x/chatd/...`,
`./coderd/database/migrations/`, and the targeted coderd, telemetry,
dbauthz, enterprise audit, and codersdk tests. Vitest and the touched
Storybook stories pass.
- Each new Go test fails when its target behavior is removed from
production code.
- Remote dogfood UAT ran on an isolated Coder instance built from the PR
head, with real Anthropic and OpenAI models:
- 110234e: the row with the experiment on and off, the picker, the
warning icon cases, save and clear, the 400 for other provider types,
which model and computer tool each computer use subagent used
(Anthropic, OpenAI, default, and disabled override), a real desktop
session on an Anthropic and an OpenAI model, the unavailable reasons,
the upgrade through migration 000634, and the removed endpoints.
- 09ce818: an override whose model moved to an unsupported AI provider.
- 7925be9: the alert for a moved and a disabled override, and a computer
use subagent on an Anthropic model.
- 2a01eaf: the row, and computer use subagents on the override and on
the default model.
- 47f5bd1: the warning icon for an OpenAI model with **Use Responses
API** set to Off, On, and Default. With Off, a computer use subagent on
that model fails with a configuration error before any provider request.
With Default, it runs with the computer tool through the Responses API.
- 81d33b4: the unavailable reasons for a disabled override, a cleared
override, and missing credentials, and a computer use subagent on an
Anthropic override. This round found that the docs named the wrong
section of the model form for **Use Responses API**. aeb3d02 fixes that
step, and a re-read of the docs against the recorded UI passed.
211 lines
7.2 KiB
Go
211 lines
7.2 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
fantasyanthropic "charm.land/fantasy/providers/anthropic"
|
|
fantasyopenai "charm.land/fantasy/providers/openai"
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"cdr.dev/slog/v3"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatloop"
|
|
openaicomputeruse "github.com/coder/coder/v2/coderd/x/chatd/chatopenai/computeruse"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
"github.com/coder/quartz"
|
|
)
|
|
|
|
type computerUseModel struct {
|
|
config database.ChatModelConfig
|
|
provider codersdk.ChatComputerUseProvider
|
|
// reasoningEffort comes from the organization override, if any.
|
|
reasoningEffort *string
|
|
}
|
|
|
|
func computerUseProviderForAIProviderType(providerType string) (codersdk.ChatComputerUseProvider, bool) {
|
|
switch chatprovider.NormalizeProvider(providerType) {
|
|
case fantasyanthropic.Name:
|
|
return codersdk.ChatComputerUseProviderAnthropic, true
|
|
case fantasyopenai.Name:
|
|
return codersdk.ChatComputerUseProviderOpenAI, true
|
|
default:
|
|
return "", false
|
|
}
|
|
}
|
|
|
|
func computerUseModelLabel(config database.ChatModelConfig) string {
|
|
if config.DisplayName != "" && config.DisplayName != config.Model {
|
|
return fmt.Sprintf("%q (%s)", config.DisplayName, config.Model)
|
|
}
|
|
return fmt.Sprintf("%q", config.Model)
|
|
}
|
|
|
|
// resolveComputerUseModel picks the model for a computer use subagent: the
|
|
// organization's computer_use override, else the organization's default chat
|
|
// model. A non-empty reason means computer use is unavailable to the owner.
|
|
func (p *Server) resolveComputerUseModel(
|
|
ctx context.Context,
|
|
ownerID uuid.UUID,
|
|
organizationID uuid.UUID,
|
|
) (model computerUseModel, reason string, err error) {
|
|
//nolint:gocritic // Chatd reads organization model configuration for the chat owner.
|
|
chatdCtx := dbauthz.AsChatd(ctx)
|
|
|
|
override, err := p.resolveModelOverride(chatdCtx, modelOverrideSpec{
|
|
context: string(codersdk.ChatModelOverrideContextComputerUse),
|
|
ownerID: ownerID,
|
|
organizationID: organizationID,
|
|
})
|
|
if err != nil {
|
|
return computerUseModel{}, "", err
|
|
}
|
|
|
|
overrideState := "no computer use model is configured"
|
|
if override.Set || override.Unavailable {
|
|
// A usable override returns below, so a reason built after this point
|
|
// means the saved override cannot be used.
|
|
overrideState = "the configured computer use model cannot be used"
|
|
}
|
|
|
|
if override.Set {
|
|
// resolveModelOverride returns only enabled models of enabled AI
|
|
// providers whose credentials the owner can use.
|
|
aiProvider, err := p.db.GetAIProviderByID(chatdCtx, override.Config.AIProviderID.UUID)
|
|
if err != nil {
|
|
return computerUseModel{}, "", xerrors.Errorf("get AI provider for model %s: %w", override.Config.ID, err)
|
|
}
|
|
|
|
if provider, ok := computerUseProviderForAIProviderType(string(aiProvider.Type)); ok {
|
|
return computerUseModel{
|
|
config: override.Config,
|
|
provider: provider,
|
|
reasoningEffort: override.ReasoningEffort,
|
|
}, "", nil
|
|
}
|
|
|
|
// The override API rejects these AI providers, but the model API can
|
|
// still move the saved model to one. Ignore the override then, as the
|
|
// settings page does.
|
|
p.logger.Info(ctx, "computer use model override has an unsupported AI provider, using default model",
|
|
slog.F("model_config_id", override.Config.ID),
|
|
slog.F("ai_provider_type", aiProvider.Type),
|
|
)
|
|
}
|
|
|
|
// Read the default under the owner's model ACLs, as chat turns do.
|
|
modelCtx, err := p.callerModelConfigContext(chatdCtx, ownerID)
|
|
if err != nil {
|
|
return computerUseModel{}, "", err
|
|
}
|
|
|
|
model.config, err = effectiveDefaultChatModelConfig(modelCtx, p.db, organizationID)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return computerUseModel{}, fmt.Sprintf(
|
|
`type "computer_use" is unavailable because %s and the organization has no default chat model available to the chat owner`,
|
|
overrideState,
|
|
), nil
|
|
}
|
|
if err != nil {
|
|
return computerUseModel{}, "", xerrors.Errorf("get default chat model config: %w", err)
|
|
}
|
|
|
|
// The default lookup returns only enabled models of enabled AI providers.
|
|
aiProvider, err := p.db.GetAIProviderByID(chatdCtx, model.config.AIProviderID.UUID)
|
|
if err != nil {
|
|
return computerUseModel{}, "", xerrors.Errorf("get AI provider for model %s: %w", model.config.ID, err)
|
|
}
|
|
|
|
label := computerUseModelLabel(model.config)
|
|
provider, ok := computerUseProviderForAIProviderType(string(aiProvider.Type))
|
|
if !ok {
|
|
return computerUseModel{}, fmt.Sprintf(
|
|
`type "computer_use" is unavailable because %s and model %s uses AI provider %q of type %q; computer use requires an Anthropic or OpenAI AI provider`,
|
|
overrideState, label, aiProvider.Name, aiProvider.Type,
|
|
), nil
|
|
}
|
|
model.provider = provider
|
|
|
|
providerKeys, err := p.resolveUserProviderAPIKeys(chatdCtx, ownerID, aiProvider.ID)
|
|
if err != nil {
|
|
return computerUseModel{}, "", xerrors.Errorf("resolve provider API keys: %w", err)
|
|
}
|
|
|
|
if !userCanUseProviderKeys(providerKeys, string(provider)) {
|
|
return computerUseModel{}, fmt.Sprintf(
|
|
`type "computer_use" is unavailable because %s and credentials for AI provider %q, used by model %s, are not configured`,
|
|
overrideState, aiProvider.Name, label,
|
|
), nil
|
|
}
|
|
|
|
return model, "", nil
|
|
}
|
|
|
|
type computerUseProviderToolOptions struct {
|
|
provider codersdk.ChatComputerUseProvider
|
|
isPlanModeTurn bool
|
|
isComputerUse bool
|
|
getWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
storeFile chattool.StoreFileFunc
|
|
clock quartz.Clock
|
|
logger slog.Logger
|
|
}
|
|
|
|
func appendComputerUseProviderTool(
|
|
providerTools []chatloop.ProviderTool,
|
|
opts computerUseProviderToolOptions,
|
|
) ([]chatloop.ProviderTool, error) {
|
|
// This helper is called for every chat turn. Only chats created by the
|
|
// computer_use subagent definition have ChatModeComputerUse, which filters
|
|
// out root, general, and explore chats. Plan mode is separate from Mode, so
|
|
// planning turns stay gated even for computer-use chats.
|
|
if opts.isPlanModeTurn || !opts.isComputerUse {
|
|
return providerTools, nil
|
|
}
|
|
|
|
desktopGeometry := chattool.DefaultComputerUseDesktopGeometry(opts.provider)
|
|
definition, err := chattool.ComputerUseProviderTool(
|
|
opts.provider,
|
|
desktopGeometry.DeclaredWidth,
|
|
desktopGeometry.DeclaredHeight,
|
|
)
|
|
if err != nil {
|
|
return providerTools, xerrors.Errorf(
|
|
"build computer use provider tool for provider %q: %w",
|
|
opts.provider,
|
|
err,
|
|
)
|
|
}
|
|
|
|
clock := opts.clock
|
|
if clock == nil {
|
|
clock = quartz.NewReal()
|
|
}
|
|
providerTool := chatloop.ProviderTool{
|
|
Definition: definition,
|
|
Runner: chattool.NewComputerUseTool(
|
|
opts.provider,
|
|
desktopGeometry.DeclaredWidth,
|
|
desktopGeometry.DeclaredHeight,
|
|
opts.getWorkspaceConn,
|
|
opts.storeFile,
|
|
clock,
|
|
opts.logger,
|
|
),
|
|
}
|
|
if opts.provider == codersdk.ChatComputerUseProviderOpenAI {
|
|
// OpenAI computer-use image results need detail metadata so the model receives
|
|
// the screenshot at original detail when the chat loop sends the tool result.
|
|
providerTool.ResultProviderMetadata = openaicomputeruse.ResultProviderMetadata
|
|
}
|
|
|
|
return append(providerTools, providerTool), nil
|
|
}
|