Spaces:
Running
Running
File size: 6,059 Bytes
64fb1e3 8c6054f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
import { Component, Inject, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, FormArray, Validators, ReactiveFormsModule, FormsModule } from '@angular/forms'; // FormsModule EKLE
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatChipsModule } from '@angular/material/chips';
import { MatTableModule } from '@angular/material/table';
import { MatTabsModule } from '@angular/material/tabs';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatListModule } from '@angular/material/list';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
@Component({
selector: 'app-intent-edit-dialog',
standalone: true,
imports: [
CommonModule,
ReactiveFormsModule,
FormsModule, // BU SATIRI EKLE
MatDialogModule,
MatFormFieldModule,
MatInputModule,
MatSelectModule,
MatCheckboxModule,
MatButtonModule,
MatIconModule,
MatChipsModule,
MatTableModule,
MatTabsModule,
MatExpansionModule,
MatListModule,
MatSnackBarModule
],
templateUrl: './intent-edit-dialog.component.html',
styleUrls: ['./intent-edit-dialog.component.scss']
})
export default class IntentEditDialogComponent implements OnInit {
form!: FormGroup;
availableAPIs: any[] = [];
parameterTypes = ['str', 'int', 'float', 'bool', 'date'];
newExample = '';
constructor(
private fb: FormBuilder,
private snackBar: MatSnackBar,
public dialogRef: MatDialogRef<IntentEditDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any
) {
this.availableAPIs = data.apis || [];
}
ngOnInit() {
this.initializeForm();
if (this.data.intent) {
this.populateForm(this.data.intent);
}
}
initializeForm() {
this.form = this.fb.group({
name: ['', [Validators.required, Validators.pattern(/^[a-zA-Z0-9-]+$/)]],
caption: [''],
locale: ['tr-TR'],
detection_prompt: ['', Validators.required],
examples: this.fb.array([]),
parameters: this.fb.array([]),
action: ['', Validators.required],
fallback_timeout_prompt: [''],
fallback_error_prompt: ['']
});
}
populateForm(intent: any) {
this.form.patchValue({
name: intent.name,
caption: intent.caption,
locale: intent.locale,
detection_prompt: intent.detection_prompt,
action: intent.action,
fallback_timeout_prompt: intent.fallback_timeout_prompt,
fallback_error_prompt: intent.fallback_error_prompt
});
// Populate examples
if (intent.examples) {
const examplesArray = this.form.get('examples') as FormArray;
intent.examples.forEach((example: string) => {
examplesArray.push(this.fb.control(example));
});
}
// Populate parameters
if (intent.parameters) {
const parametersArray = this.form.get('parameters') as FormArray;
intent.parameters.forEach((param: any) => {
parametersArray.push(this.createParameterFormGroup(param));
});
}
}
createParameterFormGroup(param: any = {}): FormGroup {
return this.fb.group({
name: [param.name || '', [Validators.required, Validators.pattern(/^[a-zA-Z0-9_]+$/)]],
caption: [param.caption || ''],
type: [param.type || 'str', Validators.required],
required: [param.required !== false],
variable_name: [param.variable_name || '', Validators.required],
extraction_prompt: [param.extraction_prompt || ''],
validation_regex: [param.validation_regex || ''],
invalid_prompt: [param.invalid_prompt || ''],
type_error_prompt: [param.type_error_prompt || '']
});
}
get examples() {
return this.form.get('examples') as FormArray;
}
get parameters() {
return this.form.get('parameters') as FormArray;
}
addExample() {
if (this.newExample.trim()) {
this.examples.push(this.fb.control(this.newExample.trim()));
this.newExample = '';
}
}
removeExample(index: number) {
this.examples.removeAt(index);
}
addParameter() {
this.parameters.push(this.createParameterFormGroup());
}
removeParameter(index: number) {
this.parameters.removeAt(index);
}
moveParameter(index: number, direction: 'up' | 'down') {
const newIndex = direction === 'up' ? index - 1 : index + 1;
if (newIndex >= 0 && newIndex < this.parameters.length) {
const param = this.parameters.at(index);
this.parameters.removeAt(index);
this.parameters.insert(newIndex, param);
}
}
async testRegex(index: number) {
const param = this.parameters.at(index);
const regex = param.get('validation_regex')?.value;
if (!regex) {
this.snackBar.open('No regex pattern to test', 'Close', { duration: 2000 });
return;
}
const testValue = prompt('Enter a test value:');
if (testValue !== null) {
try {
const pattern = new RegExp(regex);
const matches = pattern.test(testValue);
this.snackBar.open(
matches ? 'β Pattern matches!' : 'β Pattern does not match',
'Close',
{ duration: 3000 }
);
} catch (error) {
this.snackBar.open('Invalid regex pattern', 'Close', {
duration: 3000,
panelClass: 'error-snackbar'
});
}
}
}
save() {
if (this.form.invalid) {
this.snackBar.open('Please fix all validation errors', 'Close', { duration: 3000 });
return;
}
const formValue = this.form.value;
this.dialogRef.close(formValue);
}
cancel() {
this.dialogRef.close(null);
}
} |