Skip to content

A platform for community discussion. Free, open, simple.

FIX: Include credits in serializer only if credit system enabled (#35903)

Keegan George7d5cb8edc0b2
diff --git a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb b/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
index b32bf1006ca..2edbf6ba494 100644
--- a/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/llm_model_serializer.rb
@@ -60,6 +60,6 @@ class LlmModelSerializer < ApplicationSerializer
end
def include_credit_allocation?
- object.seeded?
+ object.credit_system_enabled?
end
end
diff --git a/plugins/discourse-ai/spec/serializers/llm_model_serializer_spec.rb b/plugins/discourse-ai/spec/serializers/llm_model_serializer_spec.rb
new file mode 100644
index 00000000000..bad1f85fe2a
--- /dev/null
+++ b/plugins/discourse-ai/spec/serializers/llm_model_serializer_spec.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+RSpec.describe LlmModelSerializer do
+ fab!(:admin)
+
+ before { enable_current_plugin }
+
+ describe "#include_credit_allocation?" do
+ let(:scope) { { llm_usage: {} } }
+
+ context "when model is not seeded" do
+ it "returns nil for credit allocation" do
+ llm_model = Fabricate(:llm_model)
+ serialized = described_class.new(llm_model, scope: scope, root: false)
+ json = JSON.parse(serialized.to_json)
+
+ expect(json["llm_credit_allocation"]).to be_nil
+ expect(json["llm_feature_credit_costs"]).to eq([])
+ end
+ end
+
+ context "when model is seeded but has no credit allocation" do
+ it "returns nil for credit allocation" do
+ llm_model = Fabricate(:llm_model, id: -100)
+ serialized = described_class.new(llm_model, scope: scope, root: false)
+ json = JSON.parse(serialized.to_json)
+
+ expect(json["llm_credit_allocation"]).to be_nil
+ expect(json["llm_feature_credit_costs"]).to eq([])
+ end
+
+ it "does not raise an error when serializing" do
+ llm_model = Fabricate(:llm_model, id: -101)
+
+ expect {
+ described_class.new(llm_model, scope: scope, root: false).to_json
+ }.not_to raise_error
+ end
+ end
+
+ context "when model is seeded and has credit allocation" do
+ it "includes credit allocation fields" do
+ llm_model = Fabricate(:llm_model, id: -102)
+ Fabricate(:llm_credit_allocation, llm_model: llm_model)
+
+ serialized = described_class.new(llm_model, scope: scope, root: false)
+ json = JSON.parse(serialized.to_json)
+
+ expect(json).to have_key("llm_credit_allocation")
+ expect(json).to have_key("llm_feature_credit_costs")
+ expect(json["llm_credit_allocation"]).to be_present
+ end
+ end
+ end
+end