Files
coder/coderd/database/queries/ai_providers.sql
T
Ethan fd74a160ec feat: let all users list AI providers (#32650)
Closes AIGOV-824

`GET /api/v2/ai/providers` now answers every signed-in user. Before, it
returned a 403 to anyone who isn't an owner.

Owners get exactly what they did before. Everyone else gets the same
list without each provider's configuration, i.e. `base_url`, `api_keys`,
`settings` and `status` come back empty:

```json
[
  {
    "id": "90f4a82a-c945-4598-93be-be9dde57c38d",
    "type": "openai",
    "name": "corp-openai",
    "display_name": "Corp OpenAI",
    "icon": "/icon/openai.svg",
    "enabled": true,
    "base_url": "",
    "api_keys": [],
    "settings": null,
    "created_at": "2026-11-24T11:56:44.789072Z",
    "updated_at": "2026-11-24T11:56:44.789072Z"
  }
]
```

Clients need the list to find out what a provider is called. They reach
a provider through AI Gateway at `/api/v2/ai-gateway/<provider-name>/`,
and the name is whatever an admin chose. Mux, for example, lists
providers with `GET /api/v2/ai/providers`, so a member's Mux only finds
providers that happen to have a default name.

To do this, I've split the permission in two. `ai_provider:read` now
only covers the fields that are filled in above, and every member holds
it, the same way they can read OAuth2 apps and workspace proxies. A
provider's base URL, keys and settings sit behind a new
`ai_provider:read_secret` permission, which only owners hold. `GET
/api/v2/ai/providers/{idOrName}` and the dashboard's AI settings pages
now check `ai_provider:read_secret`, so they're still owner only.

This doesn't show members much that's new. They can already read the
name, type, display name, icon and enabled state of every enabled
provider from `GET /api/v2/users/{user}/ai-provider-keys`, so the
additions are disabled providers and the two timestamps.

The next PR in the stack removes `GET /api/v2/ai-gateway/providers`,
which `GET /api/v2/ai/providers` replaces.
2026-11-30 16:16:33 +11:00

143 lines
3.0 KiB
SQL

-- name: GetAIProviderByID :one
SELECT
*
FROM
ai_providers
WHERE
id = @id::uuid AND deleted = FALSE;
-- name: GetAIProviderByIDForReferenceLock :one
SELECT
*
FROM
ai_providers
WHERE
id = @id::uuid AND deleted = FALSE
-- Lock the provider row until the model-config write completes. The
-- transaction alone does not stop a concurrent soft-delete or disable
-- between validation and writing the model config reference.
FOR SHARE;
-- name: GetAIProviderByName :one
SELECT
*
FROM
ai_providers
WHERE
name = @name::text AND deleted = FALSE;
-- name: GetAIProviders :many
-- Returns AI provider rows. Soft-deleted and disabled rows are excluded
-- unless include_deleted or include_disabled is set.
SELECT
*
FROM
ai_providers
WHERE
(@include_deleted::boolean OR NOT deleted)
AND (@include_disabled::boolean OR enabled)
ORDER BY
name ASC;
-- name: GetAIProviderSummaries :many
SELECT
id,
type,
name,
display_name,
icon,
enabled,
created_at,
updated_at
FROM
ai_providers
WHERE
NOT deleted
ORDER BY
name ASC;
-- name: GetAIProviderFilterOptions :many
-- Returns the display metadata AI Gateway session viewers need to filter
-- interceptions by provider_name. Soft-deleted and disabled rows are
-- included because interceptions keep referencing them. When a name has
-- been reused, the live row wins so current metadata is shown.
SELECT DISTINCT ON (name)
name,
type,
display_name,
icon
FROM
ai_providers
ORDER BY
name ASC,
deleted ASC,
updated_at DESC;
-- name: InsertAIProvider :one
INSERT INTO ai_providers (
id,
type,
name,
display_name,
icon,
enabled,
base_url,
settings,
settings_key_id
) VALUES (
@id::uuid,
@type::ai_provider_type,
@name::text,
sqlc.narg('display_name')::text,
@icon::text,
@enabled::boolean,
@base_url::text,
sqlc.narg('settings')::text,
sqlc.narg('settings_key_id')::text
)
RETURNING
*;
-- name: UpdateAIProvider :one
UPDATE
ai_providers
SET
type = @type::ai_provider_type,
display_name = sqlc.narg('display_name')::text,
icon = @icon::text,
enabled = @enabled::boolean,
base_url = @base_url::text,
settings = sqlc.narg('settings')::text,
settings_key_id = sqlc.narg('settings_key_id')::text,
updated_at = NOW()
WHERE
id = @id::uuid AND deleted = FALSE
RETURNING
*;
-- name: DeleteAIProviderByID :exec
UPDATE
ai_providers
SET
deleted = TRUE,
enabled = FALSE,
updated_at = NOW()
WHERE
id = @id::uuid AND deleted = FALSE;
-- name: UpdateEncryptedAIProviderSettings :one
-- Updates only the encrypted columns (settings, settings_key_id) and
-- the updated_at timestamp on a row, regardless of its deleted flag.
-- Used by the dbcrypt key rotation utility to re-encrypt or decrypt
-- rows in place.
UPDATE
ai_providers
SET
settings = sqlc.narg('settings')::text,
settings_key_id = sqlc.narg('settings_key_id')::text,
updated_at = NOW()
WHERE
id = @id::uuid
RETURNING
*;