Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 980a48c

Browse files
committed
quota update logic
1 parent f10a975 commit 980a48c

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

app/controllers/discourse_ai/admin/ai_llms_controller.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ def create
5656
def update
5757
llm_model = LlmModel.find(params[:id])
5858

59+
if params[:ai_llm].key?(:llm_quotas)
60+
if quota_params
61+
existing_quota_group_ids = llm_model.llm_quotas.pluck(:group_id)
62+
new_quota_group_ids = quota_params.map { |q| q[:group_id] }
63+
64+
llm_model
65+
.llm_quotas
66+
.where(group_id: existing_quota_group_ids - new_quota_group_ids)
67+
.destroy_all
68+
69+
quota_params.each do |quota_param|
70+
quota = llm_model.llm_quotas.find_or_initialize_by(group_id: quota_param[:group_id])
71+
quota.update!(quota_param)
72+
end
73+
else
74+
llm_model.llm_quotas.destroy_all
75+
end
76+
end
77+
5978
if llm_model.seeded?
6079
return render_json_error(I18n.t("discourse_ai.llm.cannot_edit_builtin"), status: 403)
6180
end

spec/requests/admin/ai_llms_controller_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,71 @@
256256
context "with valid update params" do
257257
let(:update_attrs) { { provider: "anthropic" } }
258258

259+
context "with quotas" do
260+
it "updates quotas correctly" do
261+
group1 = Fabricate(:group)
262+
group2 = Fabricate(:group)
263+
group3 = Fabricate(:group)
264+
265+
_quota1 =
266+
Fabricate(
267+
:llm_quota,
268+
llm_model: llm_model,
269+
group: group1,
270+
max_tokens: 1000,
271+
max_usages: 10,
272+
duration_seconds: 86_400,
273+
)
274+
_quota2 =
275+
Fabricate(
276+
:llm_quota,
277+
llm_model: llm_model,
278+
group: group2,
279+
max_tokens: 2000,
280+
max_usages: 20,
281+
duration_seconds: 86_400,
282+
)
283+
284+
put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",
285+
params: {
286+
ai_llm: {
287+
llm_quotas: [
288+
{
289+
group_id: group1.id,
290+
max_tokens: 1500,
291+
max_usages: 15,
292+
duration_seconds: 43_200,
293+
},
294+
{
295+
group_id: group3.id,
296+
max_tokens: 3000,
297+
max_usages: 30,
298+
duration_seconds: 86_400,
299+
},
300+
],
301+
},
302+
}
303+
304+
expect(response.status).to eq(200)
305+
306+
llm_model.reload
307+
expect(llm_model.llm_quotas.count).to eq(2)
308+
309+
updated_quota1 = llm_model.llm_quotas.find_by(group: group1)
310+
expect(updated_quota1.max_tokens).to eq(1500)
311+
expect(updated_quota1.max_usages).to eq(15)
312+
expect(updated_quota1.duration_seconds).to eq(43_200)
313+
314+
expect(llm_model.llm_quotas.find_by(group: group2)).to be_nil
315+
316+
new_quota = llm_model.llm_quotas.find_by(group: group3)
317+
expect(new_quota).to be_present
318+
expect(new_quota.max_tokens).to eq(3000)
319+
expect(new_quota.max_usages).to eq(30)
320+
expect(new_quota.duration_seconds).to eq(86_400)
321+
end
322+
end
323+
259324
it "updates the model" do
260325
put "/admin/plugins/discourse-ai/ai-llms/#{llm_model.id}.json",
261326
params: {

0 commit comments

Comments
 (0)