From 10a99f65770b5be96e86ff17914c69fba0a8b47b Mon Sep 17 00:00:00 2001
From: Eduardo Bonet <ebonet@gitlab.com>
Date: Tue, 1 Oct 2024 16:34:25 +0200
Subject: [PATCH] Maps featureSettings in SelfHostedModelType

FeatureSettings is now accessible when querying self-hosted models in
graphql.
---
 doc/api/graphql/reference/index.md                           | 1 +
 .../types/ai/self_hosted_models/self_hosted_model_type.rb    | 5 +++++
 ee/app/models/ai/self_hosted_model.rb                        | 2 +-
 .../ai/self_hosted_models/self_hosted_model_type_spec.rb     | 2 +-
 4 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/doc/api/graphql/reference/index.md b/doc/api/graphql/reference/index.md
index 401437f83a4b..1492aed3f533 100644
--- a/doc/api/graphql/reference/index.md
+++ b/doc/api/graphql/reference/index.md
@@ -17745,6 +17745,7 @@ Self-hosted LLM servers.
 | ---- | ---- | ----------- |
 | <a id="aiselfhostedmodelcreatedat"></a>`createdAt` | [`Time!`](#time) | Timestamp of creation. |
 | <a id="aiselfhostedmodelendpoint"></a>`endpoint` | [`String!`](#string) | Endpoint of the self-hosted model server. |
+| <a id="aiselfhostedmodelfeaturesettings"></a>`featureSettings` | [`AiFeatureSettingConnection`](#aifeaturesettingconnection) | AI feature settings using the self-hosted model. (see [Connections](#connections)) |
 | <a id="aiselfhostedmodelhasapitoken"></a>`hasApiToken` | [`Boolean!`](#boolean) | Indicates if an API key is set for the self-hosted model server. |
 | <a id="aiselfhostedmodelid"></a>`id` | [`AiSelfHostedModelID!`](#aiselfhostedmodelid) | ID of the self-hosted model server. |
 | <a id="aiselfhostedmodelidentifier"></a>`identifier` | [`String`](#string) | Identifier for 3rd party model provider. |
diff --git a/ee/app/graphql/types/ai/self_hosted_models/self_hosted_model_type.rb b/ee/app/graphql/types/ai/self_hosted_models/self_hosted_model_type.rb
index bd78264bc7ea..dcb02a491346 100644
--- a/ee/app/graphql/types/ai/self_hosted_models/self_hosted_model_type.rb
+++ b/ee/app/graphql/types/ai/self_hosted_models/self_hosted_model_type.rb
@@ -22,6 +22,11 @@ class SelfHostedModelType < ::Types::BaseObject
         field :name, String, null: false, description: 'Deployment name of the self-hosted model.'
         field :updated_at, Types::TimeType, null: true, description: 'Timestamp of last update.'
 
+        field :feature_settings,
+          Types::Ai::FeatureSettings::FeatureSettingType.connection_type,
+          null: true,
+          description: 'AI feature settings using the self-hosted model.'
+
         def has_api_token # rubocop:disable Naming/PredicateName -- otherwise resolver matcher don't work
           object.api_token.present?
         end
diff --git a/ee/app/models/ai/self_hosted_model.rb b/ee/app/models/ai/self_hosted_model.rb
index 4047a99f2e5e..b238dc86ad31 100644
--- a/ee/app/models/ai/self_hosted_model.rb
+++ b/ee/app/models/ai/self_hosted_model.rb
@@ -9,7 +9,7 @@ class SelfHostedModel < ApplicationRecord
     validates :name, presence: true, uniqueness: true
     validates :identifier, length: { maximum: 255 }, allow_nil: true
 
-    has_many :feature_settings
+    has_many :feature_settings, foreign_key: :ai_self_hosted_model_id, inverse_of: :self_hosted_model
 
     attr_encrypted :api_token,
       mode: :per_attribute_iv,
diff --git a/ee/spec/graphql/types/ai/self_hosted_models/self_hosted_model_type_spec.rb b/ee/spec/graphql/types/ai/self_hosted_models/self_hosted_model_type_spec.rb
index d9fc8a5c7b47..4b87c692727f 100644
--- a/ee/spec/graphql/types/ai/self_hosted_models/self_hosted_model_type_spec.rb
+++ b/ee/spec/graphql/types/ai/self_hosted_models/self_hosted_model_type_spec.rb
@@ -4,7 +4,7 @@
 
 RSpec.describe GitlabSchema.types['AiSelfHostedModel'], feature_category: :"self-hosted_models" do
   it 'has specific fields' do
-    expected_fields = %w[id name created_at updated_at model endpoint has_api_token identifier]
+    expected_fields = %w[id name created_at updated_at model endpoint has_api_token identifier feature_settings]
 
     expect(described_class).to include_graphql_fields(*expected_fields)
   end
-- 
GitLab