Spaces:
Building
Building
Update flare-ui/src/app/dialogs/intent-edit-dialog/intent-edit-dialog.component.ts
Browse files
flare-ui/src/app/dialogs/intent-edit-dialog/intent-edit-dialog.component.ts
CHANGED
@@ -1,198 +1,199 @@
|
|
1 |
-
import { Component, Inject, OnInit } from '@angular/core';
|
2 |
-
import { CommonModule } from '@angular/common';
|
3 |
-
import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule } from '@angular/forms';
|
4 |
-
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
|
5 |
-
import { MatFormFieldModule } from '@angular/material/form-field';
|
6 |
-
import { MatInputModule } from '@angular/material/input';
|
7 |
-
import { MatSelectModule } from '@angular/material/select';
|
8 |
-
import { MatCheckboxModule } from '@angular/material/checkbox';
|
9 |
-
import { MatButtonModule } from '@angular/material/button';
|
10 |
-
import { MatIconModule } from '@angular/material/icon';
|
11 |
-
import { MatChipsModule } from '@angular/material/chips';
|
12 |
-
import { MatTableModule } from '@angular/material/table';
|
13 |
-
import { MatTabsModule } from '@angular/material/tabs';
|
14 |
-
import { MatExpansionModule } from '@angular/material/expansion';
|
15 |
-
import { MatListModule } from '@angular/material/list';
|
16 |
-
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
17 |
-
|
18 |
-
@Component({
|
19 |
-
selector: 'app-intent-edit-dialog',
|
20 |
-
standalone: true,
|
21 |
-
imports: [
|
22 |
-
CommonModule,
|
23 |
-
ReactiveFormsModule,
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
private
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
this.newExample
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
this.parameters.
|
152 |
-
this.parameters.
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
const
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
const
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
'
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
this.
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
198 |
}
|
|
|
1 |
+
import { Component, Inject, OnInit } from '@angular/core';
|
2 |
+
import { CommonModule } from '@angular/common';
|
3 |
+
import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms'; // FormsModule EKLE
|
4 |
+
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
|
5 |
+
import { MatFormFieldModule } from '@angular/material/form-field';
|
6 |
+
import { MatInputModule } from '@angular/material/input';
|
7 |
+
import { MatSelectModule } from '@angular/material/select';
|
8 |
+
import { MatCheckboxModule } from '@angular/material/checkbox';
|
9 |
+
import { MatButtonModule } from '@angular/material/button';
|
10 |
+
import { MatIconModule } from '@angular/material/icon';
|
11 |
+
import { MatChipsModule } from '@angular/material/chips';
|
12 |
+
import { MatTableModule } from '@angular/material/table';
|
13 |
+
import { MatTabsModule } from '@angular/material/tabs';
|
14 |
+
import { MatExpansionModule } from '@angular/material/expansion';
|
15 |
+
import { MatListModule } from '@angular/material/list';
|
16 |
+
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
17 |
+
|
18 |
+
@Component({
|
19 |
+
selector: 'app-intent-edit-dialog',
|
20 |
+
standalone: true,
|
21 |
+
imports: [
|
22 |
+
CommonModule,
|
23 |
+
ReactiveFormsModule,
|
24 |
+
FormsModule, // BU SATIRI EKLE
|
25 |
+
MatDialogModule,
|
26 |
+
MatFormFieldModule,
|
27 |
+
MatInputModule,
|
28 |
+
MatSelectModule,
|
29 |
+
MatCheckboxModule,
|
30 |
+
MatButtonModule,
|
31 |
+
MatIconModule,
|
32 |
+
MatChipsModule,
|
33 |
+
MatTableModule,
|
34 |
+
MatTabsModule,
|
35 |
+
MatExpansionModule,
|
36 |
+
MatListModule,
|
37 |
+
MatSnackBarModule
|
38 |
+
],
|
39 |
+
templateUrl: './intent-edit-dialog.component.html',
|
40 |
+
styleUrls: ['./intent-edit-dialog.component.scss']
|
41 |
+
})
|
42 |
+
export default class IntentEditDialogComponent implements OnInit {
|
43 |
+
form!: FormGroup;
|
44 |
+
availableAPIs: any[] = [];
|
45 |
+
parameterTypes = ['str', 'int', 'float', 'bool', 'date'];
|
46 |
+
|
47 |
+
newExample = '';
|
48 |
+
|
49 |
+
constructor(
|
50 |
+
private fb: FormBuilder,
|
51 |
+
private snackBar: MatSnackBar,
|
52 |
+
public dialogRef: MatDialogRef<IntentEditDialogComponent>,
|
53 |
+
@Inject(MAT_DIALOG_DATA) public data: any
|
54 |
+
) {
|
55 |
+
this.availableAPIs = data.apis || [];
|
56 |
+
}
|
57 |
+
|
58 |
+
ngOnInit() {
|
59 |
+
this.initializeForm();
|
60 |
+
if (this.data.intent) {
|
61 |
+
this.populateForm(this.data.intent);
|
62 |
+
}
|
63 |
+
}
|
64 |
+
|
65 |
+
initializeForm() {
|
66 |
+
this.form = this.fb.group({
|
67 |
+
name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9-]+$/)]],
|
68 |
+
caption: [''],
|
69 |
+
locale: ['tr-TR'],
|
70 |
+
detection_prompt: ['', Validators.required],
|
71 |
+
examples: this.fb.array([]),
|
72 |
+
parameters: this.fb.array([]),
|
73 |
+
action: ['', Validators.required],
|
74 |
+
fallback_timeout_prompt: [''],
|
75 |
+
fallback_error_prompt: ['']
|
76 |
+
});
|
77 |
+
}
|
78 |
+
|
79 |
+
populateForm(intent: any) {
|
80 |
+
this.form.patchValue({
|
81 |
+
name: intent.name,
|
82 |
+
caption: intent.caption,
|
83 |
+
locale: intent.locale,
|
84 |
+
detection_prompt: intent.detection_prompt,
|
85 |
+
action: intent.action,
|
86 |
+
fallback_timeout_prompt: intent.fallback_timeout_prompt,
|
87 |
+
fallback_error_prompt: intent.fallback_error_prompt
|
88 |
+
});
|
89 |
+
|
90 |
+
// Populate examples
|
91 |
+
if (intent.examples) {
|
92 |
+
const examplesArray = this.form.get('examples') as FormArray;
|
93 |
+
intent.examples.forEach((example: string) => {
|
94 |
+
examplesArray.push(this.fb.control(example));
|
95 |
+
});
|
96 |
+
}
|
97 |
+
|
98 |
+
// Populate parameters
|
99 |
+
if (intent.parameters) {
|
100 |
+
const parametersArray = this.form.get('parameters') as FormArray;
|
101 |
+
intent.parameters.forEach((param: any) => {
|
102 |
+
parametersArray.push(this.createParameterFormGroup(param));
|
103 |
+
});
|
104 |
+
}
|
105 |
+
}
|
106 |
+
|
107 |
+
createParameterFormGroup(param: any = {}): FormGroup {
|
108 |
+
return this.fb.group({
|
109 |
+
name: [param.name || '', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]],
|
110 |
+
caption: [param.caption || ''],
|
111 |
+
type: [param.type || 'str', Validators.required],
|
112 |
+
required: [param.required !== false],
|
113 |
+
variable_name: [param.variable_name || '', Validators.required],
|
114 |
+
extraction_prompt: [param.extraction_prompt || ''],
|
115 |
+
validation_regex: [param.validation_regex || ''],
|
116 |
+
invalid_prompt: [param.invalid_prompt || ''],
|
117 |
+
type_error_prompt: [param.type_error_prompt || '']
|
118 |
+
});
|
119 |
+
}
|
120 |
+
|
121 |
+
get examples() {
|
122 |
+
return this.form.get('examples') as FormArray;
|
123 |
+
}
|
124 |
+
|
125 |
+
get parameters() {
|
126 |
+
return this.form.get('parameters') as FormArray;
|
127 |
+
}
|
128 |
+
|
129 |
+
addExample() {
|
130 |
+
if (this.newExample.trim()) {
|
131 |
+
this.examples.push(this.fb.control(this.newExample.trim()));
|
132 |
+
this.newExample = '';
|
133 |
+
}
|
134 |
+
}
|
135 |
+
|
136 |
+
removeExample(index: number) {
|
137 |
+
this.examples.removeAt(index);
|
138 |
+
}
|
139 |
+
|
140 |
+
addParameter() {
|
141 |
+
this.parameters.push(this.createParameterFormGroup());
|
142 |
+
}
|
143 |
+
|
144 |
+
removeParameter(index: number) {
|
145 |
+
this.parameters.removeAt(index);
|
146 |
+
}
|
147 |
+
|
148 |
+
moveParameter(index: number, direction: 'up' | 'down') {
|
149 |
+
const newIndex = direction === 'up' ? index - 1 : index + 1;
|
150 |
+
if (newIndex >= 0 && newIndex < this.parameters.length) {
|
151 |
+
const param = this.parameters.at(index);
|
152 |
+
this.parameters.removeAt(index);
|
153 |
+
this.parameters.insert(newIndex, param);
|
154 |
+
}
|
155 |
+
}
|
156 |
+
|
157 |
+
async testRegex(index: number) {
|
158 |
+
const param = this.parameters.at(index);
|
159 |
+
const regex = param.get('validation_regex')?.value;
|
160 |
+
|
161 |
+
if (!regex) {
|
162 |
+
this.snackBar.open('No regex pattern to test', 'Close', { duration: 2000 });
|
163 |
+
return;
|
164 |
+
}
|
165 |
+
|
166 |
+
const testValue = prompt('Enter a test value:');
|
167 |
+
if (testValue !== null) {
|
168 |
+
try {
|
169 |
+
const pattern = new RegExp(regex);
|
170 |
+
const matches = pattern.test(testValue);
|
171 |
+
|
172 |
+
this.snackBar.open(
|
173 |
+
matches ? '✓ Pattern matches!' : '✗ Pattern does not match',
|
174 |
+
'Close',
|
175 |
+
{ duration: 3000 }
|
176 |
+
);
|
177 |
+
} catch (error) {
|
178 |
+
this.snackBar.open('Invalid regex pattern', 'Close', {
|
179 |
+
duration: 3000,
|
180 |
+
panelClass: 'error-snackbar'
|
181 |
+
});
|
182 |
+
}
|
183 |
+
}
|
184 |
+
}
|
185 |
+
|
186 |
+
save() {
|
187 |
+
if (this.form.invalid) {
|
188 |
+
this.snackBar.open('Please fix all validation errors', 'Close', { duration: 3000 });
|
189 |
+
return;
|
190 |
+
}
|
191 |
+
|
192 |
+
const formValue = this.form.value;
|
193 |
+
this.dialogRef.close(formValue);
|
194 |
+
}
|
195 |
+
|
196 |
+
cancel() {
|
197 |
+
this.dialogRef.close(null);
|
198 |
+
}
|
199 |
}
|