This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 40
DEV: migrates tools form to form-kit #1204
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd09595
DEV: migrates tools form to form-kit
jjaffeux cb107de
WIP: enum values
keegangeorge ab8bc88
Merge from `main`
keegangeorge 1e46eee
enum fix
jjaffeux a86e25f
DEV: Apply fixes
keegangeorge c78f319
DEV: Use `RagOptionsFk`
keegangeorge a9c45fc
FIX: Linting and spec
keegangeorge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 5 additions & 1 deletion
6
admin/assets/javascripts/discourse/routes/admin-plugins-show-discourse-ai-tools-new.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,21 @@ | ||
| import DiscourseRoute from "discourse/routes/discourse"; | ||
|
|
||
| export default class DiscourseAiToolsNewRoute extends DiscourseRoute { | ||
| beforeModel(transition) { | ||
| this.preset = transition.to.queryParams.presetId || "empty_tool"; | ||
| } | ||
|
|
||
| async model() { | ||
| return this.store.createRecord("ai-tool"); | ||
| } | ||
|
|
||
| setupController(controller) { | ||
| super.setupController(...arguments); | ||
| const toolsModel = this.modelFor("adminPlugins.show.discourse-ai-tools"); | ||
|
|
||
| controller.set("allTools", toolsModel); | ||
| controller.set("presets", toolsModel.resultSetMeta.presets); | ||
| controller.set("llms", toolsModel.resultSetMeta.llms); | ||
| controller.set("settings", toolsModel.resultSetMeta.settings); | ||
| controller.set("selectedPreset", this.preset); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
359 changes: 359 additions & 0 deletions
359
assets/javascripts/discourse/components/ai-tool-editor-form.gjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,359 @@ | ||
| import Component from "@glimmer/component"; | ||
| import { tracked } from "@glimmer/tracking"; | ||
| import { array, fn, hash } from "@ember/helper"; | ||
| import { action } from "@ember/object"; | ||
| import { service } from "@ember/service"; | ||
| import { gt } from "truth-helpers"; | ||
| import Form from "discourse/components/form"; | ||
| import { popupAjaxError } from "discourse/lib/ajax-error"; | ||
| import { i18n } from "discourse-i18n"; | ||
| import AiToolTestModal from "./modal/ai-tool-test-modal"; | ||
| import RagOptions from "./rag-options"; | ||
| import RagUploader from "./rag-uploader"; | ||
|
|
||
| export default class AiToolEditorForm extends Component { | ||
| @service modal; | ||
| @service siteSettings; | ||
| @service dialog; | ||
| @service router; | ||
| @service toasts; | ||
|
|
||
| @tracked uploadedFiles = []; | ||
| @tracked isSaving = false; | ||
|
|
||
| PARAMETER_TYPES = [ | ||
| { name: "string", id: "string" }, | ||
| { name: "number", id: "number" }, | ||
| { name: "boolean", id: "boolean" }, | ||
| { name: "array", id: "array" }, | ||
| ]; | ||
|
|
||
| get formData() { | ||
| // TODO: add enumData only if parameters.enum is true | ||
| return { | ||
| name: this.args.editingModel.name || "", | ||
| tool_name: this.args.editingModel.tool_name || "", | ||
| description: this.args.editingModel.description || "", | ||
| summary: this.args.editingModel.summary || "", | ||
| parameters: this.args.editingModel.parameters || [ | ||
| { enumData: ["foo", "bar"] }, | ||
| ], | ||
| script: this.args.editingModel.script || "", | ||
| rag_uploads: this.args.editingModel.rag_uploads || [], | ||
| }; | ||
| } | ||
|
|
||
| @action | ||
| async save(data) { | ||
| this.isSaving = true; | ||
|
|
||
| try { | ||
| await this.args.model.save(data); | ||
|
|
||
| this.toasts.success({ | ||
| data: { message: i18n("discourse_ai.tools.saved") }, | ||
| duration: 2000, | ||
| }); | ||
|
|
||
| if (!this.args.tools.any((tool) => tool.id === this.args.model.id)) { | ||
| this.args.tools.pushObject(this.args.model); | ||
| } | ||
|
|
||
| this.router.transitionTo( | ||
| "adminPlugins.show.discourse-ai-tools.edit", | ||
| this.args.model | ||
| ); | ||
| } catch (e) { | ||
| popupAjaxError(e); | ||
| } finally { | ||
| this.isSaving = false; | ||
| } | ||
| } | ||
|
|
||
| @action | ||
| delete() { | ||
| return this.dialog.confirm({ | ||
| message: i18n("discourse_ai.tools.confirm_delete"), | ||
|
|
||
| didConfirm: async () => { | ||
| await this.args.model.destroyRecord(); | ||
| this.args.tools.removeObject(this.args.model); | ||
| this.router.transitionTo("adminPlugins.show.discourse-ai-tools.index"); | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| @action | ||
| updateUploads(addItemToCollection, uploads) { | ||
| const uniqueUploads = uploads.filter( | ||
| (upload) => !this.uploadedFiles.some((file) => file.id === upload.id) | ||
| ); | ||
| addItemToCollection("rag_uploads", uniqueUploads); | ||
| this.uploadedFiles = [...this.uploadedFiles, ...uniqueUploads]; | ||
| } | ||
|
|
||
| @action | ||
| removeUpload(form, upload) { | ||
| this.uploadedFiles = this.uploadedFiles.filter( | ||
| (file) => file.id !== upload.id | ||
| ); | ||
| form.set("rag_uploads", this.uploadedFiles); | ||
| } | ||
|
|
||
| @action | ||
| openTestModal() { | ||
| this.modal.show(AiToolTestModal, { | ||
| model: { | ||
| tool: this.args.editingModel, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| currentParameterSelection(data, index) { | ||
| return data.parameters[index].type; | ||
| } | ||
|
|
||
| get ragUploadsDescription() { | ||
| return this.siteSettings.rag_images_enabled | ||
| ? i18n("discourse_ai.rag.uploads.description_with_images") | ||
| : i18n("discourse_ai.rag.uploads.description"); | ||
| } | ||
|
|
||
| <template> | ||
| <Form | ||
| @onSubmit={{this.save}} | ||
| @data={{this.formData}} | ||
| class="ai-tool-editor" | ||
| as |form| | ||
| > | ||
| {{! NAME }} | ||
| <form.Field | ||
| @name="name" | ||
| @title={{i18n "discourse_ai.tools.name"}} | ||
| @validation="required|length:1,100" | ||
| @format="large" | ||
| @tooltip={{i18n "discourse_ai.tools.name_help"}} | ||
| as |field| | ||
| > | ||
| <field.Input class="ai-tool-editor__name" /> | ||
| </form.Field> | ||
|
|
||
| {{! TOOL NAME }} | ||
| <form.Field | ||
| @name="tool_name" | ||
| @title={{i18n "discourse_ai.tools.tool_name"}} | ||
| @validation="required|length:1,100" | ||
| @format="large" | ||
| @tooltip={{i18n "discourse_ai.tools.tool_name_help"}} | ||
| as |field| | ||
| > | ||
| <field.Input class="ai-tool-editor__tool_name" /> | ||
| </form.Field> | ||
|
|
||
| {{! DESCRIPTION }} | ||
| <form.Field | ||
| @name="description" | ||
| @title={{i18n "discourse_ai.tools.description"}} | ||
| @validation="required|length:1,1000" | ||
| @format="full" | ||
| @tooltip={{i18n "discourse_ai.tools.description_help"}} | ||
| as |field| | ||
| > | ||
| <field.Textarea | ||
| @height={{60}} | ||
| class="ai-tool-editor__description" | ||
| placeholder={{i18n "discourse_ai.tools.description_help"}} | ||
| /> | ||
| </form.Field> | ||
|
|
||
| {{! SUMMARY }} | ||
| <form.Field | ||
| @name="summary" | ||
| @title={{i18n "discourse_ai.tools.summary"}} | ||
| @validation="required|length:1,255" | ||
| @format="large" | ||
| @tooltip={{i18n "discourse_ai.tools.summary_help"}} | ||
| as |field| | ||
| > | ||
| <field.Input class="ai-tool-editor__summary" /> | ||
| </form.Field> | ||
|
|
||
| {{! PARAMETERS }} | ||
| <form.Collection @name="parameters" as |collection index collectionData|> | ||
| <form.Container class="ai-tool-parameter"> | ||
| <form.Row as |row|> | ||
| <row.Col @size={{6}}> | ||
| <collection.Field | ||
| @name="name" | ||
| @title={{i18n "discourse_ai.tools.parameter_name"}} | ||
| @validation="required|length:1,100" | ||
| @format="full" | ||
| as |field| | ||
| > | ||
| <field.Input /> | ||
| </collection.Field> | ||
| </row.Col> | ||
|
|
||
| <row.Col @size={{6}}> | ||
| <collection.Field | ||
| @name="type" | ||
| @title={{i18n "discourse_ai.tools.parameter_type"}} | ||
| @validation="required" | ||
| @format="full" | ||
| as |field| | ||
| > | ||
| <field.Select as |select|> | ||
| {{#each this.PARAMETER_TYPES as |type|}} | ||
| <select.Option | ||
| @value={{type.id}} | ||
| >{{type.name}}</select.Option> | ||
| {{/each}} | ||
| </field.Select> | ||
| </collection.Field> | ||
| </row.Col> | ||
| </form.Row> | ||
|
|
||
| <form.Row as |row|> | ||
| <row.Col @size={{12}}> | ||
| <collection.Field | ||
| @name="description" | ||
| @title={{i18n "discourse_ai.tools.parameter_description"}} | ||
| @validation="required|length:1,1000" | ||
| @format="full" | ||
| as |field| | ||
| > | ||
| <field.Input class="ai-tool-editor__parameter-description" /> | ||
| </collection.Field> | ||
| </row.Col> | ||
| </form.Row> | ||
|
|
||
| <form.Row as |row|> | ||
| <row.Col> | ||
| <collection.Field @name="required" @title="Required" as |field|> | ||
| <field.Checkbox /> | ||
| </collection.Field> | ||
| </row.Col> | ||
|
|
||
| <row.Col> | ||
| <collection.Field @name="enum" @title="Enum" as |field|> | ||
| <field.Checkbox /> | ||
| </collection.Field> | ||
| </row.Col> | ||
|
|
||
| {{#if collectionData.enum}} | ||
| <row.Col @size={{8}}> | ||
| <collection.Collection @name="enumData" as |child childIndex|> | ||
| <form.Container class="ai-tool-parameter__enum-values"> | ||
| <child.Field | ||
| @name="enumValue" | ||
| @title={{i18n "discourse_ai.tools.enum_value"}} | ||
| as |field| | ||
| > | ||
| <field.Input /> | ||
| </child.Field> | ||
|
|
||
| {{#if (gt collectionData.enumData.length 1)}} | ||
| <form.Button | ||
| class="btn-danger" | ||
| @icon="trash-can" | ||
| @action={{fn child.remove childIndex}} | ||
| /> | ||
| {{/if}} | ||
|
|
||
| <form.Button | ||
| @icon="plus" | ||
| @label="discourse_ai.tools.add_enum_value" | ||
| @action={{fn | ||
| form.addItemToCollection | ||
| "enumData" | ||
| (array "") | ||
| }} | ||
| /> | ||
| </form.Container> | ||
|
|
||
| </collection.Collection> | ||
| </row.Col> | ||
| {{/if}} | ||
| </form.Row> | ||
| <form.Row as |row|> | ||
| <row.Col class="ai-tool-parameter-actions"> | ||
| <form.Button | ||
| @label="discourse_ai.tools.remove_parameter" | ||
| @icon="trash-can" | ||
| @action={{fn collection.remove index}} | ||
| class="btn-danger" | ||
| /> | ||
| </row.Col> | ||
| </form.Row> | ||
| </form.Container> | ||
| </form.Collection> | ||
|
|
||
| <form.Button | ||
| @icon="plus" | ||
| @label="discourse_ai.tools.add_parameter" | ||
| @action={{fn | ||
| form.addItemToCollection | ||
| "parameters" | ||
| (hash name="" type="string" description="" required=false enum=false) | ||
| }} | ||
| /> | ||
|
|
||
| {{! SCRIPT }} | ||
| <form.Field | ||
| @name="script" | ||
| @title={{i18n "discourse_ai.tools.script"}} | ||
| @validation="required|length:1,100000" | ||
| @format="full" | ||
| as |field| | ||
| > | ||
| <field.Code @lang="javascript" @height={{600}} /> | ||
| </form.Field> | ||
|
|
||
| {{! UPLOADS }} | ||
| {{#if this.siteSettings.ai_embeddings_enabled}} | ||
| <form.Field | ||
| @name="rag_uploads" | ||
| @title={{i18n "discourse_ai.rag.uploads.title"}} | ||
| @tooltip={{this.ragUploadsDescription}} | ||
| as |field| | ||
| > | ||
| <field.Custom> | ||
| <RagUploader | ||
| @target={{@editingModel}} | ||
| @updateUploads={{fn this.updateUploads form.addItemToCollection}} | ||
| @onRemove={{fn this.removeUpload form}} | ||
| @allowImages={{@settings.rag_images_enabled}} | ||
| /> | ||
| <RagOptions | ||
| @model={{@editingModel}} | ||
| @llms={{@llms}} | ||
| @allowImages={{@settings.rag_images_enabled}} | ||
| /> | ||
| </field.Custom> | ||
| </form.Field> | ||
| {{/if}} | ||
|
|
||
| <form.Actions> | ||
| {{#unless @isNew}} | ||
| <form.Button | ||
| @label="discourse_ai.tools.test" | ||
| @action={{this.openTestModal}} | ||
| class="ai-tool-editor__test-button" | ||
| /> | ||
|
|
||
| <form.Button | ||
| @label="discourse_ai.tools.delete" | ||
| @icon="trash-can" | ||
| @action={{this.delete}} | ||
| class="btn-danger ai-tool-editor__delete" | ||
| /> | ||
| {{/unless}} | ||
|
|
||
| <form.Submit | ||
| @label="discourse_ai.tools.save" | ||
| class="ai-tool-editor__save" | ||
| /> | ||
| </form.Actions> | ||
| </Form> | ||
| </template> | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.