feat!: enforce organization scope for chat models (#27959)

## Summary

Move chat model management and runtime availability to
organization-scoped API routes. The API now identifies the organization
in every model-management operation, applies chat model RBAC and ACLs
within that organization, and returns only models the caller can use
there.

Depends on #27958.

## Problem

Chat model configurations now belong to organizations, but the existing
experimental management and discovery APIs are deployment-shaped. Their
routes do not identify an organization, SDK methods do not accept an
organization ID, and item operations address a model configuration by ID
alone.

That contract cannot safely expose organization-owned model
configurations. It also does not provide the organization-specific
provider information, authorization behavior, ACL management, or
recovery guidance needed by the settings and chat clients later in the
stack.

## Fix

Add organization-scoped chat model endpoints under:

```text
/api/experimental/organizations/{organization}/chats/models
```

The new API provides:

- Organization-scoped model listing and creation.
- Model get, update, and delete operations scoped by both organization
and model ID.
- Model ACL read and update operations.
- Organization-scoped runtime availability and provider status.
- Redacted provider descriptors for model-management clients.
- Chat model RBAC and row ACL enforcement for writes, runtime discovery,
and Chatd model selection.
- Recovery guidance when a chat's organization has no usable local
model.

Historical chats can retain a model reference owned by another
organization, but runtime selection uses an authorized local default
when one is available. Foreign, inaccessible, disabled, and otherwise
unusable models are not exposed as usable selections for the
organization.

The existing collection and availability routes remain as
default-organization compatibility endpoints in this layer:

```text
GET  /api/experimental/chats/model-configs
POST /api/experimental/chats/model-configs
GET  /api/experimental/chats/models
```

These compatibility routes do not provide access to models in
non-default organizations. The previous unscoped item update and delete
routes are removed because item operations must include the owning
organization.

## Breaking change

This changes the experimental HTTP API and Go SDK contracts for chat
model management.

Clients must migrate model-management and availability requests to the
organization-scoped routes and provide an organization identifier. In
the Go SDK, the following methods now require an organization ID:

- `ChatModelAvailability`
- `ChatModels`
- `CreateChatModel`
- `UpdateChatModel`
- `DeleteChatModel`

`ChatModels` also changes its return value from `[]ChatModel` to
`OrganizationChatModelsResponse`, which includes the organization models
and the redacted provider descriptors required by management clients.
New item and ACL methods likewise require both the organization ID and
model ID.

The default-organization compatibility routes reduce the immediate
impact for collection and availability callers, but they do not preserve
the previous item API or SDK method signatures. Generated clients and
direct consumers of the experimental API must be updated before adopting
this change.

_This pull request description was generated by Coder Agents._
This commit is contained in:
Ethan
2026-08-24 11:20:25 +01:00
committed by GitHub
parent 9bd8899359
commit aefc761fcf
58 changed files with 7995 additions and 2558 deletions
+1
View File
@@ -27,6 +27,7 @@ We track the following resources:
| AuditableUserAIBudgetOverride<br><i>write, delete</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>created_at</td><td>false</td></tr><tr><td>group_id</td><td>true</td></tr><tr><td>group_name</td><td>true</td></tr><tr><td>spend_limit</td><td>true</td></tr><tr><td>spend_limit_micros</td><td>false</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_id</td><td>false</td></tr><tr><td>username</td><td>false</td></tr></tbody></table> |
| Chat<br><i>create, write</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>agent_id</td><td>false</td></tr><tr><td>archived</td><td>true</td></tr><tr><td>build_id</td><td>false</td></tr><tr><td>client_type</td><td>false</td></tr><tr><td>compaction_requested_at</td><td>false</td></tr><tr><td>context_aggregate_hash</td><td>false</td></tr><tr><td>context_dirty_resources</td><td>false</td></tr><tr><td>context_dirty_since</td><td>false</td></tr><tr><td>context_error</td><td>false</td></tr><tr><td>created_at</td><td>false</td></tr><tr><td>dynamic_tools</td><td>false</td></tr><tr><td>generation_attempt</td><td>false</td></tr><tr><td>group_acl</td><td>true</td></tr><tr><td>heartbeat_at</td><td>false</td></tr><tr><td>history_version</td><td>false</td></tr><tr><td>id</td><td>true</td></tr><tr><td>labels</td><td>true</td></tr><tr><td>last_error</td><td>false</td></tr><tr><td>last_model_config_id</td><td>false</td></tr><tr><td>last_read_message_id</td><td>false</td></tr><tr><td>last_reasoning_effort</td><td>false</td></tr><tr><td>last_turn_summary</td><td>false</td></tr><tr><td>mcp_server_ids</td><td>true</td></tr><tr><td>mode</td><td>true</td></tr><tr><td>organization_id</td><td>false</td></tr><tr><td>owner_id</td><td>true</td></tr><tr><td>owner_name</td><td>false</td></tr><tr><td>owner_username</td><td>false</td></tr><tr><td>parent_chat_id</td><td>false</td></tr><tr><td>pin_order</td><td>true</td></tr><tr><td>plan_mode</td><td>false</td></tr><tr><td>queue_version</td><td>false</td></tr><tr><td>requires_action_deadline_at</td><td>false</td></tr><tr><td>retry_state</td><td>false</td></tr><tr><td>retry_state_version</td><td>false</td></tr><tr><td>root_chat_id</td><td>false</td></tr><tr><td>runner_id</td><td>false</td></tr><tr><td>snapshot_version</td><td>false</td></tr><tr><td>started_at</td><td>false</td></tr><tr><td>status</td><td>false</td></tr><tr><td>summary</td><td>false</td></tr><tr><td>summary_generated_at</td><td>false</td></tr><tr><td>title</td><td>true</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_acl</td><td>true</td></tr><tr><td>worker_id</td><td>false</td></tr><tr><td>workspace_id</td><td>true</td></tr></tbody></table> |
| ChatInstructionSettings<br><i>write</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>id</td><td>false</td></tr><tr><td>include_default_system_prompt</td><td>true</td></tr><tr><td>include_default_system_prompt_set</td><td>true</td></tr><tr><td>name</td><td>false</td></tr><tr><td>plan_mode_instructions</td><td>true</td></tr><tr><td>system_prompt</td><td>true</td></tr></tbody></table> |
| ChatModelConfig<br><i>write</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>ai_provider_id</td><td>true</td></tr><tr><td>compression_threshold</td><td>true</td></tr><tr><td>context_limit</td><td>true</td></tr><tr><td>created_at</td><td>false</td></tr><tr><td>created_by</td><td>true</td></tr><tr><td>deleted</td><td>true</td></tr><tr><td>deleted_at</td><td>false</td></tr><tr><td>display_name</td><td>true</td></tr><tr><td>enabled</td><td>true</td></tr><tr><td>group_acl</td><td>true</td></tr><tr><td>id</td><td>false</td></tr><tr><td>is_default</td><td>true</td></tr><tr><td>model</td><td>true</td></tr><tr><td>options</td><td>true</td></tr><tr><td>organization_id</td><td>false</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>updated_by</td><td>true</td></tr><tr><td>user_acl</td><td>true</td></tr></tbody></table> |
| CustomRole<br><i></i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>created_at</td><td>false</td></tr><tr><td>display_name</td><td>true</td></tr><tr><td>id</td><td>false</td></tr><tr><td>is_system</td><td>false</td></tr><tr><td>member_permissions</td><td>true</td></tr><tr><td>name</td><td>true</td></tr><tr><td>org_permissions</td><td>true</td></tr><tr><td>organization_id</td><td>false</td></tr><tr><td>site_permissions</td><td>true</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_permissions</td><td>true</td></tr></tbody></table> |
| GitSSHKey<br><i>create</i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>created_at</td><td>false</td></tr><tr><td>private_key</td><td>true</td></tr><tr><td>private_key_key_id</td><td>false</td></tr><tr><td>public_key</td><td>true</td></tr><tr><td>updated_at</td><td>false</td></tr><tr><td>user_id</td><td>true</td></tr></tbody></table> |
| GroupSyncSettings<br><i></i> | <table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody> | <tr><td>auto_create_missing_groups</td><td>true</td></tr><tr><td>field</td><td>true</td></tr><tr><td>legacy_group_name_mapping</td><td>false</td></tr><tr><td>mapping</td><td>true</td></tr><tr><td>regex_filter</td><td>true</td></tr></tbody></table> |
+230 -53
View File
@@ -596,59 +596,6 @@ Experimental: this endpoint is subject to change.
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## List chat models
### Code samples
```sh
# Example request using curl
curl -X GET http://coder-server:8080/api/experimental/chats/models \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /api/experimental/chats/models`
Experimental: this endpoint is subject to change.
### Example responses
> 200 Response
```json
{
"providers": [
{
"available": true,
"models": [
{
"display_name": "string",
"id": "string",
"model": "string",
"provider": "string"
}
],
"provider": "string",
"unavailable_reason": "missing_api_key"
}
],
"unsupported_providers": [
{
"display_name": "string",
"provider": "string"
}
]
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|--------------------------------------------------------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.ChatModelAvailabilityResponse](schemas.md#codersdkchatmodelavailabilityresponse) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## Watch chat events for a user via WebSockets
### Code samples
@@ -3268,3 +3215,233 @@ Experimental: this endpoint is subject to change.
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.Chat](schemas.md#codersdkchat) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
## List available chat models in an organization
### Code samples
```sh
# Example request using curl
curl -X GET http://coder-server:8080/api/experimental/organizations/{organization}/chats/models/available \
-H 'Accept: application/json' \
-H 'Coder-Session-Token: API_KEY'
```
`GET /api/experimental/organizations/{organization}/chats/models/available`
Experimental: this endpoint is subject to change.
### Parameters
| Name | In | Type | Required | Description |
|----------------|------|--------|----------|-------------------------|
| `organization` | path | string | true | Organization name or ID |
### Example responses
> 200 Response
```json
{
"models": [
{
"ai_provider_id": "5a3b8ff9-20e7-4c37-ba1a-5b433e355819",
"compression_threshold": 0,
"context_limit": 0,
"created_at": "2019-08-24T14:15:22Z",
"display_name": "string",
"enabled": true,
"id": "497f6eca-6276-4993-bfeb-53cbbbba6f08",
"is_default": true,
"model": "string",
"model_config": {
"frequency_penalty": 0,
"max_output_tokens": 0,
"openai_config": {
"use_responses_api": true
},
"presence_penalty": 0,
"provider_options": {
"anthropic": {
"allowed_domains": [
"string"
],
"blocked_domains": [
"string"
],
"context_1m_enabled": true,
"disable_parallel_tool_use": true,
"send_reasoning": true,
"thinking": {
"budget_tokens": 0
},
"thinking_display": "string",
"web_search_enabled": true
},
"google": {
"cached_content": "string",
"safety_settings": [
{
"category": "string",
"threshold": "string"
}
],
"thinking_config": {
"include_thoughts": true,
"thinking_budget": 0,
"thinking_level": "string"
},
"threshold": "string",
"web_search_enabled": true
},
"openai": {
"allowed_domains": [
"string"
],
"include": [
"string"
],
"instructions": "string",
"log_probs": true,
"logit_bias": {
"property1": 0,
"property2": 0
},
"max_completion_tokens": 0,
"max_tool_calls": 0,
"metadata": {
"property1": null,
"property2": null
},
"parallel_tool_calls": true,
"prediction": {
"property1": null,
"property2": null
},
"prompt_cache_key": "string",
"reasoning_summary": "string",
"safety_identifier": "string",
"search_context_size": "string",
"service_tier": "string",
"store": true,
"strict_json_schema": true,
"structured_outputs": true,
"text_verbosity": "string",
"top_log_probs": 0,
"user": "string",
"web_search_enabled": true
},
"openaicompat": {
"user": "string"
},
"openrouter": {
"extra_body": {
"property1": null,
"property2": null
},
"include_usage": true,
"log_probs": true,
"logit_bias": {
"property1": 0,
"property2": 0
},
"parallel_tool_calls": true,
"provider": {
"allow_fallbacks": true,
"data_collection": "string",
"ignore": [
"string"
],
"only": [
"string"
],
"order": [
"string"
],
"quantizations": [
"string"
],
"require_parameters": true,
"sort": "string"
},
"reasoning": {
"enabled": true,
"exclude": true,
"max_tokens": 0
},
"user": "string"
},
"vercel": {
"extra_body": {
"property1": null,
"property2": null
},
"logit_bias": {
"property1": 0,
"property2": 0
},
"logprobs": true,
"parallel_tool_calls": true,
"providerOptions": {
"models": [
"string"
],
"order": [
"string"
]
},
"reasoning": {
"enabled": true,
"exclude": true,
"max_tokens": 0
},
"top_logprobs": 0,
"user": "string"
}
},
"reasoning_effort": {
"default": "string",
"max": "string"
},
"temperature": 0,
"top_k": 0,
"top_p": 0
},
"organization_id": "7c60d51f-b44e-4682-87d6-449835ea4de6",
"reasoning_efforts": [
"string"
],
"updated_at": "2019-08-24T14:15:22Z"
}
],
"providers": [
{
"available": true,
"models": [
{
"display_name": "string",
"id": "string",
"model": "string",
"provider": "string"
}
],
"provider": "string",
"unavailable_reason": "missing_api_key"
}
],
"unsupported_providers": [
{
"display_name": "string",
"provider": "string"
}
]
}
```
### Responses
| Status | Meaning | Description | Schema |
|--------|---------------------------------------------------------|-------------|--------------------------------------------------------------------------------------------|
| 200 | [OK](https://tools.ietf.org/html/rfc7231#section-6.3.1) | OK | [codersdk.ChatModelAvailabilityResponse](schemas.md#codersdkchatmodelavailabilityresponse) |
To perform this operation, you must be authenticated. [Learn more](authentication.md).
+1737 -4
View File
File diff suppressed because it is too large Load Diff