From 28fa723472cbcb8e9400cd05bd4f39591f0705fd Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Mon, 31 Mar 2025 15:45:03 +0200 Subject: [PATCH] FIX: forced tools wasn't set correctly when tool has no options. (#1231) * wip: more dynamic availableForcedTools * FIX: forced tools wasn't set correctly when tool has no options. --------- Co-authored-by: Roman Rizzi --- .../javascripts/discourse/admin/models/ai-persona.js | 6 +++++- .../discourse/components/ai-persona-editor.gjs | 12 ++++++------ spec/system/admin_ai_persona_spec.rb | 6 +++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/assets/javascripts/discourse/admin/models/ai-persona.js b/assets/javascripts/discourse/admin/models/ai-persona.js index 8cfebbb0..4c7ff28e 100644 --- a/assets/javascripts/discourse/admin/models/ai-persona.js +++ b/assets/javascripts/discourse/admin/models/ai-persona.js @@ -77,7 +77,11 @@ export default class AiPersona extends RestModel { flattenedToolStructure(data) { return (data.tools || []).map((tName) => { - return [tName, data.toolOptions[tName], data.forcedTools.includes(tName)]; + return [ + tName, + data.toolOptions[tName] || {}, + data.forcedTools.includes(tName), + ]; }); } diff --git a/assets/javascripts/discourse/components/ai-persona-editor.gjs b/assets/javascripts/discourse/components/ai-persona-editor.gjs index a4222a90..53c646a5 100644 --- a/assets/javascripts/discourse/components/ai-persona-editor.gjs +++ b/assets/javascripts/discourse/components/ai-persona-editor.gjs @@ -30,7 +30,6 @@ export default class PersonaEditor extends Component { @tracked allGroups = []; @tracked isSaving = false; - @tracked availableForcedTools = []; dirtyFormData = null; @@ -208,10 +207,6 @@ export default class PersonaEditor extends Component { toolOptions: updatedOptions, }); - this.availableForcedTools = this.allTools.filter((tool) => - updatedTools.includes(tool.id) - ); - if (currentData.forcedTools?.length > 0) { const updatedForcedTools = currentData.forcedTools.filter( (fct) => !removedTools.includes(fct) @@ -220,6 +215,11 @@ export default class PersonaEditor extends Component { } } + @action + availableForcedTools(tools) { + return this.allTools.filter((tool) => tools.includes(tool.id)); + } + mapToolOptions(currentOptions, toolNames) { const updatedOptions = Object.assign({}, currentOptions); @@ -439,7 +439,7 @@ export default class PersonaEditor extends Component { @value={{field.value}} @disabled={{data.system}} @onChange={{field.set}} - @content={{this.availableForcedTools}} + @content={{this.availableForcedTools data.tools}} /> diff --git a/spec/system/admin_ai_persona_spec.rb b/spec/system/admin_ai_persona_spec.rb index a027a221..79fd57e0 100644 --- a/spec/system/admin_ai_persona_spec.rb +++ b/spec/system/admin_ai_persona_spec.rb @@ -27,10 +27,12 @@ RSpec.describe "Admin AI persona configuration", type: :system, js: true do tool_selector = PageObjects::Components::SelectKit.new("#control-tools .select-kit") tool_selector.expand tool_selector.select_row_by_value("Read") + tool_selector.select_row_by_value("ListCategories") tool_selector.collapse tool_selector = PageObjects::Components::SelectKit.new("#control-forcedTools .select-kit") tool_selector.expand + tool_selector.select_row_by_value("ListCategories") tool_selector.select_row_by_value("Read") tool_selector.collapse @@ -46,8 +48,10 @@ RSpec.describe "Admin AI persona configuration", type: :system, js: true do expect(persona.name).to eq("Test Persona") expect(persona.description).to eq("I am a test persona") expect(persona.system_prompt).to eq("You are a helpful bot") - expect(persona.tools).to eq([["Read", { "read_private" => nil }, true]]) expect(persona.forced_tool_count).to eq(1) + + expected_tools = [["Read", { "read_private" => nil }, true], ["ListCategories", {}, true]] + expect(persona.tools).to contain_exactly(*expected_tools) end it "will not allow deletion or editing of system personas" do