mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-08 07:12:49 +00:00
Introduces import/export feature for tools and personas. Uploads are omitted for now, and will be added in a future PR * **Backend:** * Adds `import` and `export` actions to `Admin::AiPersonasController` and `Admin::AiToolsController`. * Introduces `DiscourseAi::PersonaExporter` and `DiscourseAi::PersonaImporter` services to manage JSON serialization and deserialization. * The export format for a persona embeds its associated custom tools. To ensure portability, `AiTool` references are serialized using their `tool_name` rather than their internal database `id`. * The import logic detects conflicts by name. A `force=true` parameter can be passed to overwrite existing records. * **Frontend:** * `AiPersonaListEditor` and `AiToolListEditor` components now include an "Import" button that handles file selection and POSTs the JSON data to the respective `import` endpoint. * `AiPersonaEditorForm` and `AiToolEditorForm` components feature an "Export" button that triggers a download of the serialized record. * Handles import conflicts (HTTP `409` for tools, `422` for personas) by showing a `dialog.confirm` prompt to allow the user to force an overwrite. * **Testing:** * Adds comprehensive request specs for the new controller actions (`#import`, `#export`). * Includes unit specs for the `PersonaExporter` and `PersonaImporter` services. * Persona import and export implemented
42 lines
1.5 KiB
Ruby
42 lines
1.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe DiscourseAi::PersonaExporter do
|
|
describe "#export" do
|
|
subject(:export_json) { JSON.parse(exporter.export) }
|
|
|
|
context "when exporting a persona with a custom tool" do
|
|
fab!(:ai_tool) { Fabricate(:ai_tool, name: "Giphy Searcher", tool_name: "giphy_search") }
|
|
fab!(:ai_persona) { Fabricate(:ai_persona, tools: [["custom-#{ai_tool.id}", nil, false]]) }
|
|
|
|
let(:exporter) { described_class.new(persona: ai_persona) }
|
|
|
|
it "returns JSON containing the persona and its custom tool" do
|
|
expect(export_json["persona"]["name"]).to eq(ai_persona.name)
|
|
expect(export_json["persona"]["tools"].first.first).to eq("custom-#{ai_tool.tool_name}")
|
|
|
|
custom_tool = export_json["custom_tools"].first
|
|
expect(custom_tool["identifier"]).to eq(ai_tool.tool_name)
|
|
expect(custom_tool["name"]).to eq(ai_tool.name)
|
|
end
|
|
end
|
|
|
|
context "when the persona has no custom tools" do
|
|
fab!(:ai_persona) { Fabricate(:ai_persona, tools: []) }
|
|
let(:exporter) { described_class.new(persona: ai_persona) }
|
|
|
|
it "returns JSON with an empty custom_tools array" do
|
|
expect(export_json["custom_tools"]).to eq([])
|
|
end
|
|
end
|
|
|
|
context "when the persona does not exist" do
|
|
it "raises an error if initialized with a non persona" do
|
|
expect { described_class.new(persona: nil) }.to raise_error(
|
|
ArgumentError,
|
|
"Invalid persona provided",
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|