REFACTOR: Migrate Personas' form to FormKit (#1178)

* REFACTOR: Migrate Personas' form to FormKit

We re-arranged fields into sections so we can better differentiate which options are specific to the AI bot.

* few form-kit improvements

https://github.com/discourse/discourse/pull/31934

---------

Co-authored-by: Joffrey JAFFEUX <[email protected]>
This commit is contained in:
Roman Rizzi
2025-03-21 14:46:33 -03:00
committed by GitHub
co-authored by Joffrey JAFFEUX
parent 0e2dd7378f
commit 2a8be6e2d7
16 changed files with 788 additions and 930 deletions
+20 -83
View File
@@ -2,33 +2,31 @@ import { module, test } from "qunit";
import AiPersona from "discourse/plugins/discourse-ai/discourse/admin/models/ai-persona";
module("Discourse AI | Unit | Model | ai-persona", function () {
test("init properties", function (assert) {
test("toPOJO", function (assert) {
const properties = {
tools: [
["ToolName", { option1: "value1", option2: "value2" }],
["ToolName", { option1: "value1", option2: "value2" }, false],
"ToolName2",
"ToolName3",
],
};
const aiPersona = AiPersona.create(properties);
const aiPersonaPOJO = AiPersona.create(properties).toPOJO();
assert.deepEqual(aiPersona.tools, ["ToolName", "ToolName2", "ToolName3"]);
assert.equal(
aiPersona.getToolOption("ToolName", "option1").value,
"value1"
);
assert.equal(
aiPersona.getToolOption("ToolName", "option2").value,
"value2"
);
assert.deepEqual(aiPersonaPOJO.tools, [
"ToolName",
"ToolName2",
"ToolName3",
]);
assert.equal(aiPersonaPOJO.toolOptions["ToolName"].option1, "value1");
assert.equal(aiPersonaPOJO.toolOptions["ToolName"].option2, "value2");
});
test("update properties", function (assert) {
test("fromPOJO", function (assert) {
const properties = {
id: 1,
name: "Test",
tools: ["ToolName"],
tools: [["ToolName", { option1: "value1" }, false]],
allowed_group_ids: [12],
system: false,
enabled: true,
@@ -58,80 +56,19 @@ module("Discourse AI | Unit | Model | ai-persona", function () {
allow_chat_channel_mentions: true,
allow_chat_direct_messages: true,
};
const updatedValue = "updated";
const aiPersona = AiPersona.create({ ...properties });
aiPersona.getToolOption("ToolName", "option1").value = "value1";
const personaPOJO = aiPersona.toPOJO();
const updatedProperties = aiPersona.updateProperties();
personaPOJO.toolOptions["ToolName"].option1 = updatedValue;
personaPOJO.forcedTools = "ToolName";
// perform remapping for save
properties.tools = [["ToolName", { option1: "value1" }, false]];
const updatedPersona = aiPersona.fromPOJO(personaPOJO);
assert.deepEqual(updatedProperties, properties);
});
test("create properties", function (assert) {
const properties = {
id: 1,
name: "Test",
tools: ["ToolName"],
allowed_group_ids: [12],
system: false,
enabled: true,
system_prompt: "System Prompt",
priority: false,
description: "Description",
top_p: 0.8,
temperature: 0.7,
user: null,
user_id: null,
default_llm_id: 1,
max_context_posts: 5,
vision_enabled: true,
vision_max_pixels: 100,
rag_uploads: [],
rag_chunk_tokens: 374,
rag_chunk_overlap_tokens: 10,
rag_conversation_chunks: 10,
question_consolidator_llm_id: 2,
allow_chat: false,
tool_details: true,
forced_tool_count: -1,
allow_personal_messages: true,
allow_topic_mentions: true,
allow_chat_channel_mentions: true,
allow_chat_direct_messages: true,
force_default_llm: false,
rag_llm_model_id: 1,
};
const aiPersona = AiPersona.create({ ...properties });
aiPersona.getToolOption("ToolName", "option1").value = "value1";
const createdProperties = aiPersona.createProperties();
properties.tools = [["ToolName", { option1: "value1" }, false]];
assert.deepEqual(createdProperties, properties);
});
test("working copy", function (assert) {
const aiPersona = AiPersona.create({
name: "Test",
tools: ["ToolName"],
});
aiPersona.getToolOption("ToolName", "option1").value = "value1";
const workingCopy = aiPersona.workingCopy();
assert.equal(workingCopy.name, "Test");
assert.equal(
workingCopy.getToolOption("ToolName", "option1").value,
"value1"
);
assert.deepEqual(workingCopy.tools, ["ToolName"]);
assert.deepEqual(updatedPersona.tools, [
["ToolName", { option1: updatedValue }, true],
]);
});
});