|
| 1 | +import Component from "@glimmer/component"; |
| 2 | +import { tracked } from "@glimmer/tracking"; |
| 3 | +import { fn, hash } from "@ember/helper"; |
| 4 | +import { action } from "@ember/object"; |
| 5 | +import { gt } from "truth-helpers"; |
| 6 | +import ModalJsonSchemaEditor from "discourse/components/modal/json-schema-editor"; |
| 7 | +import { prettyJSON } from "discourse/lib/formatter"; |
| 8 | +import { i18n } from "discourse-i18n"; |
| 9 | + |
| 10 | +export default class AiPersonaResponseFormatEditor extends Component { |
| 11 | + @tracked showJsonEditorModal = false; |
| 12 | + |
| 13 | + jsonSchema = { |
| 14 | + type: "array", |
| 15 | + uniqueItems: true, |
| 16 | + title: i18n("discourse_ai.ai_persona.response_format.modal.root_title"), |
| 17 | + items: { |
| 18 | + type: "object", |
| 19 | + title: i18n("discourse_ai.ai_persona.response_format.modal.key_title"), |
| 20 | + properties: { |
| 21 | + key: { |
| 22 | + type: "string", |
| 23 | + }, |
| 24 | + type: { |
| 25 | + type: "string", |
| 26 | + enum: ["string", "integer", "boolean"], |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | + |
| 32 | + get modalTitle() { |
| 33 | + return i18n("discourse_ai.ai_persona.response_format.title"); |
| 34 | + } |
| 35 | + |
| 36 | + get responseFormatAsJSON() { |
| 37 | + return JSON.stringify(this.args.data.response_format); |
| 38 | + } |
| 39 | + |
| 40 | + get displayJSON() { |
| 41 | + const toDisplay = {}; |
| 42 | + |
| 43 | + this.args.data.response_format.forEach((keyDesc) => { |
| 44 | + toDisplay[keyDesc.key] = keyDesc.type; |
| 45 | + }); |
| 46 | + |
| 47 | + return prettyJSON(toDisplay); |
| 48 | + } |
| 49 | + |
| 50 | + @action |
| 51 | + openModal() { |
| 52 | + this.showJsonEditorModal = true; |
| 53 | + } |
| 54 | + |
| 55 | + @action |
| 56 | + closeModal() { |
| 57 | + this.showJsonEditorModal = false; |
| 58 | + } |
| 59 | + |
| 60 | + @action |
| 61 | + updateResponseFormat(form, value) { |
| 62 | + form.set("response_format", JSON.parse(value)); |
| 63 | + } |
| 64 | + |
| 65 | + <template> |
| 66 | + <@form.Container |
| 67 | + @title={{i18n "discourse_ai.ai_persona.response_format.title"}} |
| 68 | + @format="large" |
| 69 | + > |
| 70 | + <div class="ai-persona-editor__response-format"> |
| 71 | + {{#if (gt @data.response_format.length 0)}} |
| 72 | + <pre class="ai-persona-editor__response-format-pre"> |
| 73 | + <code>{{this.displayJSON}}</code> |
| 74 | + </pre> |
| 75 | + {{else}} |
| 76 | + <div class="ai-persona-editor__response-format-none">{{i18n |
| 77 | + "discourse_ai.ai_persona.response_format.no_format" |
| 78 | + }}</div> |
| 79 | + {{/if}} |
| 80 | + |
| 81 | + <@form.Button |
| 82 | + @action={{this.openModal}} |
| 83 | + @label="discourse_ai.ai_persona.response_format.open_modal" |
| 84 | + @disabled={{@data.system}} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + </@form.Container> |
| 88 | + |
| 89 | + {{#if this.showJsonEditorModal}} |
| 90 | + <ModalJsonSchemaEditor |
| 91 | + @model={{hash |
| 92 | + value=this.responseFormatAsJSON |
| 93 | + updateValue=(fn this.updateResponseFormat @form) |
| 94 | + settingName=this.modalTitle |
| 95 | + jsonSchema=this.jsonSchema |
| 96 | + }} |
| 97 | + @closeModal={{this.closeModal}} |
| 98 | + /> |
| 99 | + {{/if}} |
| 100 | + </template> |
| 101 | +} |
0 commit comments