MCPcopy Index your code
hub / github.com/coder/coder / aiProvidersCreate

Method aiProvidersCreate

coderd/ai_providers.go:145–251  ·  view source on GitHub ↗

@Summary Create an AI provider @ID create-an-ai-provider @Security CoderSessionToken @Accept json @Produce json @Tags AI Providers @Param request body codersdk.CreateAIProviderRequest true "Create AI provider request" @Success 201 {object} codersdk.AIProvider @Router /api/v2/ai/providers [post]

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

143// @Success 201 {object} codersdk.AIProvider
144// @Router /api/v2/ai/providers [post]
145func (api *API) aiProvidersCreate(rw http.ResponseWriter, r *http.Request) {
146 var (
147 ctx = r.Context()
148 auditor = api.Auditor.Load()
149 aReq, commitAudit = audit.InitRequest[database.AIProvider](rw, &audit.RequestParams{
150 Audit: *auditor,
151 Log: api.Logger,
152 Request: r,
153 Action: database.AuditActionCreate,
154 })
155 )
156 defer commitAudit()
157
158 var req codersdk.CreateAIProviderRequest
159 if !httpapi.Read(ctx, rw, r, &req) {
160 return
161 }
162
163 if validations := req.Validate(); len(validations) > 0 {
164 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
165 Message: "Invalid AI provider request.",
166 Validations: validations,
167 })
168 return
169 }
170
171 // Bedrock providers authenticate via the settings blob, not via a
172 // bearer key, so registering an api_keys list against them would
173 // be silently unused.
174 if req.Settings.Bedrock != nil && len(req.APIKeys) > 0 {
175 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
176 Message: "Bedrock providers do not accept api_keys; configure access credentials via settings.",
177 })
178 return
179 }
180
181 settings, err := encodeAIProviderSettings(req.Settings)
182 if err != nil {
183 api.Logger.Error(ctx, "encode AI provider settings", slog.Error(err))
184 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
185 Message: "Internal error encoding settings.",
186 Detail: err.Error(),
187 })
188 return
189 }
190
191 var (
192 row database.AIProvider
193 keys []database.AIProviderKey
194 )
195 err = api.Database.InTx(func(tx database.Store) error {
196 var txErr error
197 row, txErr = tx.InsertAIProvider(ctx, database.InsertAIProviderParams{
198 ID: uuid.New(),
199 Type: database.AIProviderType(req.Type),
200 Name: req.Name,
201 DisplayName: sql.NullString{String: req.DisplayName, Valid: req.DisplayName != ""},
202 Enabled: req.Enabled,

Callers

nothing calls this directly

Calls 15

ValidateMethod · 0.95
InitRequestFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
AIProviderTypeTypeAlias · 0.92
IsUniqueViolationFunction · 0.92
IsNotAuthorizedErrorFunction · 0.92
ForbiddenFunction · 0.92
AIProviderFunction · 0.92
encodeAIProviderSettingsFunction · 0.85
insertAIProviderKeysFunction · 0.85

Tested by

no test coverage detected