1
1
import Component from " @glimmer/component" ;
2
2
import { tracked } from " @glimmer/tracking" ;
3
- import { Input } from " @ember/component" ;
3
+ import { fn } from " @ember/helper" ;
4
+ import { on } from " @ember/modifier" ;
4
5
import { action } from " @ember/object" ;
5
6
import didInsert from " @ember/render-modifiers/modifiers/did-insert" ;
6
7
import didUpdate from " @ember/render-modifiers/modifiers/did-update" ;
7
- import { inject as service } from " @ember/service" ;
8
+ import { service } from " @ember/service" ;
8
9
import BackButton from " discourse/components/back-button" ;
9
10
import DButton from " discourse/components/d-button" ;
10
- import Textarea from " discourse/components/d-textarea" ;
11
11
import DTooltip from " discourse/components/d-tooltip" ;
12
+ import withEventValue from " discourse/helpers/with-event-value" ;
12
13
import { popupAjaxError } from " discourse/lib/ajax-error" ;
13
14
import I18n from " discourse-i18n" ;
14
15
import AceEditor from " admin/components/ace-editor" ;
15
16
import ComboBox from " select-kit/components/combo-box" ;
16
17
import AiToolParameterEditor from " ./ai-tool-parameter-editor" ;
17
18
import AiToolTestModal from " ./modal/ai-tool-test-modal" ;
18
19
20
+ const ACE_EDITOR_MODE = " javascript" ;
21
+ const ACE_EDITOR_THEME = " chrome" ;
22
+
19
23
export default class AiToolEditor extends Component {
20
24
@service router;
21
- @service store;
22
25
@service dialog;
23
26
@service modal;
24
27
@service toasts;
25
28
26
29
@tracked isSaving = false ;
27
30
@tracked editingModel = null ;
28
31
@tracked showDelete = false ;
29
-
30
32
@tracked selectedPreset = null ;
31
33
32
- aceEditorMode = " javascript" ;
33
- aceEditorTheme = " chrome" ;
34
-
35
- @action
36
- updateModel () {
37
- this .editingModel = this .args .model .workingCopy ();
38
- this .showDelete = ! this .args .model .isNew ;
39
- }
40
-
41
34
get presets () {
42
35
return this .args .presets .map ((preset ) => {
43
36
return {
@@ -51,6 +44,12 @@ export default class AiToolEditor extends Component {
51
44
return ! this .selectedPreset && this .args .model .isNew ;
52
45
}
53
46
47
+ @action
48
+ updateModel () {
49
+ this .editingModel = this .args .model .workingCopy ();
50
+ this .showDelete = ! this .args .model .isNew ;
51
+ }
52
+
54
53
@action
55
54
configurePreset () {
56
55
this .selectedPreset = this .args .presets .findBy (" preset_id" , this .presetId );
@@ -64,16 +63,23 @@ export default class AiToolEditor extends Component {
64
63
this .isSaving = true ;
65
64
66
65
try {
67
- await this .args .model .save (
68
- this .editingModel .getProperties (
69
- " name" ,
70
- " description" ,
71
- " parameters" ,
72
- " script" ,
73
- " summary"
74
- )
66
+ const data = this .editingModel .getProperties (
67
+ " name" ,
68
+ " description" ,
69
+ " parameters" ,
70
+ " script" ,
71
+ " summary"
75
72
);
76
73
74
+ for (const p of data .parameters ) {
75
+ if (p .enumValues ) {
76
+ p .enum_values = p .enumValues ;
77
+ delete p .enumValues ;
78
+ }
79
+ }
80
+
81
+ await this .args .model .save (data);
82
+
77
83
this .toasts .success ({
78
84
data: { message: I18n .t (" discourse_ai.tools.saved" ) },
79
85
duration: 2000 ,
@@ -97,22 +103,15 @@ export default class AiToolEditor extends Component {
97
103
delete () {
98
104
return this .dialog .confirm ({
99
105
message: I18n .t (" discourse_ai.tools.confirm_delete" ),
100
- didConfirm : () => {
101
- return this .args .model .destroyRecord ().then (() => {
102
- this .args .tools .removeObject (this .args .model );
103
- this .router .transitionTo (
104
- " adminPlugins.show.discourse-ai-tools.index"
105
- );
106
- });
106
+ didConfirm: async () => {
107
+ await this .args .model .destroyRecord ();
108
+
109
+ this .args .tools .removeObject (this .args .model );
110
+ this .router .transitionTo (" adminPlugins.show.discourse-ai-tools.index" );
107
111
},
108
112
});
109
113
}
110
114
111
- @action
112
- updateScript (script ) {
113
- this .editingModel .script = script;
114
- }
115
-
116
115
@action
117
116
openTestModal () {
118
117
this .modal .show (AiToolTestModal, {
@@ -129,9 +128,9 @@ export default class AiToolEditor extends Component {
129
128
/>
130
129
131
130
<form
132
- class =" form-horizontal ai-tool-editor"
133
- {{didUpdate this . updateModel @ model.id}}
134
131
{{didInsert this . updateModel @ model.id}}
132
+ {{didUpdate this . updateModel @ model.id}}
133
+ class =" form-horizontal ai-tool-editor"
135
134
>
136
135
{{#if this . showPresets }}
137
136
<div class =" control-group" >
@@ -145,76 +144,88 @@ export default class AiToolEditor extends Component {
145
144
146
145
<div class =" control-group ai-llm-editor__action_panel" >
147
146
<DButton
148
- class =" ai-tool-editor__next"
149
147
@ action ={{this .configurePreset }}
150
- >
151
- {{ I18n.t " discourse_ai.tools.next.title " }}
152
- </ DButton >
148
+ @ label = " discourse_ai.tools.next.title "
149
+ class = " ai-tool-editor__next "
150
+ / >
153
151
</div >
154
152
{{else }}
155
153
<div class =" control-group" >
156
154
<label >{{I18n.t " discourse_ai.tools.name" }} </label >
157
- <Input
158
- @ type =" text"
159
- @ value ={{this .editingModel.name }}
155
+ <input
156
+ {{on " input" ( withEventValue ( fn ( mut this . editingModel.name) ) ) }}
157
+ value ={{this .editingModel.name }}
158
+ type =" text"
160
159
class =" ai-tool-editor__name"
161
160
/>
162
161
<DTooltip
163
162
@ icon =" question-circle"
164
163
@ content ={{I18n.t " discourse_ai.tools.name_help" }}
165
164
/>
166
165
</div >
166
+
167
167
<div class =" control-group" >
168
168
<label >{{I18n.t " discourse_ai.tools.description" }} </label >
169
- <Textarea
170
- @ value ={{this .editingModel.description }}
171
- class =" ai-tool-editor__description input-xxlarge"
169
+ <textarea
170
+ {{on
171
+ " input"
172
+ ( withEventValue ( fn ( mut this . editingModel.description) ) )
173
+ }}
172
174
placeholder ={{I18n.t " discourse_ai.tools.description_help" }}
173
- />
175
+ class =" ai-tool-editor__description input-xxlarge"
176
+ >{{this .editingModel.description }} </textarea >
174
177
</div >
178
+
175
179
<div class =" control-group" >
176
180
<label >{{I18n.t " discourse_ai.tools.summary" }} </label >
177
- <Input
178
- @ type =" text"
179
- @ value ={{this .editingModel.summary }}
181
+ <input
182
+ {{on " input" ( withEventValue ( fn ( mut this . editingModel.summary) ) ) }}
183
+ value ={{this .editingModel.summary }}
184
+ type =" text"
180
185
class =" ai-tool-editor__summary input-xxlarge"
181
186
/>
182
187
<DTooltip
183
188
@ icon =" question-circle"
184
189
@ content ={{I18n.t " discourse_ai.tools.summary_help" }}
185
190
/>
186
191
</div >
192
+
187
193
<div class =" control-group" >
188
194
<label >{{I18n.t " discourse_ai.tools.parameters" }} </label >
189
195
<AiToolParameterEditor @ parameters ={{this .editingModel.parameters }} />
190
196
</div >
197
+
191
198
<div class =" control-group" >
192
199
<label >{{I18n.t " discourse_ai.tools.script" }} </label >
193
200
<AceEditor
194
201
@ content ={{this .editingModel.script }}
195
- @ mode ={{this .aceEditorMode }}
196
- @ theme ={{this .aceEditorTheme }}
197
- @ onChange ={{this .updateScript }}
202
+ @ onChange ={{withEventValue ( fn ( mut this . editingModel.script ) ) }}
203
+ @ mode ={{ACE_EDITOR_MODE }}
204
+ @ theme ={{ACE_EDITOR_THEME }}
198
205
@ editorId =" ai-tool-script-editor"
199
206
/>
200
207
</div >
208
+
201
209
<div class =" control-group ai-tool-editor__action_panel" >
202
210
<DButton
203
211
@ action ={{this .openTestModal }}
204
- class =" btn-default ai-tool-editor__test-button"
205
- >{{I18n.t " discourse_ai.tools.test" }} </DButton >
212
+ @ label =" discourse_ai.tools.test"
213
+ class =" ai-tool-editor__test-button"
214
+ />
215
+
206
216
<DButton
207
- class =" btn-primary ai-tool-editor__save"
208
217
@ action ={{this .save }}
218
+ @ label =" discourse_ai.tools.save"
209
219
@ disabled ={{this .isSaving }}
210
- >{{I18n.t " discourse_ai.tools.save" }} </DButton >
220
+ class =" btn-primary ai-tool-editor__save"
221
+ />
222
+
211
223
{{#if this . showDelete }}
212
224
<DButton
213
225
@ action ={{this .delete }}
226
+ @ label =" discourse_ai.tools.delete"
214
227
class =" btn-danger ai-tool-editor__delete"
215
- >
216
- {{I18n.t " discourse_ai.tools.delete" }}
217
- </DButton >
228
+ />
218
229
{{/if }}
219
230
</div >
220
231
{{/if }}
0 commit comments