Files
coder/coderd/database/queries/ai_providers.sql
T
Jake Howell dd4f5ae48a fix: load AI Gateway session provider filter without AIProvider read (#28966)
> 🤖 This PR was modified by Coder Agents on behalf of Jake Howell.

The Provider filter on `/ai-gateway` called the owner-only
provider-config API (`GET /api/v2/ai/providers`). Site auditors can read
sessions but not that config, so the dropdown was empty, and typing
`provider_name:…` failed the whole sessions list because search looked
up the name with the same permission.

- Add `GET /api/v2/ai-gateway/providers`, returning display metadata
only (`name`, `type`, `display_name`, `icon`) from `ai_providers`,
including disabled and soft-deleted rows that past sessions still
reference. When a name was reused, the live row wins.
- Authorize it in `dbauthz` on `aibridge_interception` read rather than
`ai_provider` read, so owners and auditors get labels and icons without
access to configuration. Members get 404.
- Match `provider_name` as a plain string in session search instead of
looking up `ai_providers`. Unknown names return an empty page, not a
validation error.
- Point the Provider dropdown at the new endpoint; filter by name, show
`display_name`, and render the icon from `type`/`icon`.

The endpoint reads the small `ai_providers` table instead of grouping
`aibridge_interceptions`, so it needs no new index and does not inherit
the scan cost noted for the models/clients lists. It is registered only
under `/ai-gateway`, not the legacy `/aibridge` alias.
2026-09-17 15:29:21 +00:00

126 lines
2.8 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: 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
*;