File size: 7,231 Bytes
64fb1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e2f83d
64fb1e3
 
 
 
 
 
 
 
 
 
 
115fb28
64fb1e3
115fb28
 
 
 
 
 
 
64fb1e3
 
115fb28
 
64fb1e3
115fb28
64fb1e3
115fb28
 
 
64fb1e3
 
 
115fb28
 
64fb1e3
115fb28
64fb1e3
 
 
 
 
 
 
 
 
358b002
64fb1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
3b66e59
 
64fb1e3
 
 
115fb28
 
 
64fb1e3
 
 
115fb28
64fb1e3
 
 
 
 
3b66e59
 
64fb1e3
3b66e59
64fb1e3
 
 
 
 
3b66e59
64fb1e3
3b66e59
 
 
 
 
64fb1e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23e3af0
64fb1e3
115fb28
23e3af0
115fb28
23e3af0
 
 
 
 
 
 
 
 
115fb28
23e3af0
115fb28
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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: ['', Validators.required],
      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) {
    // Önce basit alanları doldur
    this.form.patchValue({
      name: intent.name || '',
      caption: intent.caption || '',
      locale: intent.locale || 'tr-TR',
      detection_prompt: intent.detection_prompt || '',
      action: intent.action || '',
      fallback_timeout_prompt: intent.fallback_timeout_prompt || '',
      fallback_error_prompt: intent.fallback_error_prompt || ''
    });

    // Examples array'ini doldur
    if (intent.examples && Array.isArray(intent.examples)) {
      const examplesArray = this.form.get('examples') as FormArray;
      examplesArray.clear();
      intent.examples.forEach((example: string) => {
        if (typeof example === 'string') {
          examplesArray.push(this.fb.control(example));
        }
      });
    }

    // Parameters array'ini doldur
    if (intent.parameters && Array.isArray(intent.parameters)) {
      const parametersArray = this.form.get('parameters') as FormArray;
      parametersArray.clear();
      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 || '', Validators.required], // ✅ Validators.required ekle
      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(): FormArray {
    return this.form.get('parameters') as FormArray || this.fb.array([]);
  }

  addExample() {
    const trimmedExample = this.newExample?.trim();
    if (trimmedExample) {
      this.examples.push(this.fb.control(trimmedExample));
      this.newExample = '';
    }
  }
  
  removeExample(index: number) {
    this.examples.removeAt(index);
  }

  addParameter() {
    const parametersArray = this.form.get('parameters') as FormArray;
    parametersArray.push(this.createParameterFormGroup());
  }
  
  removeParameter(index: number) {
    this.parameters.removeAt(index);
  }

  moveParameter(index: number, direction: 'up' | 'down') {
    const parametersArray = this.form.get('parameters') as FormArray;
    const newIndex = direction === 'up' ? index - 1 : index + 1;
    
    if (newIndex >= 0 && newIndex < parametersArray.length) {
      const param = parametersArray.at(index);
      parametersArray.removeAt(index);
      parametersArray.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;
    
    // Form değerlerini güvenli bir şekilde al
    const result = {
      name: formValue.name || '',
      caption: formValue.caption || '',
      locale: formValue.locale || 'tr-TR',
      detection_prompt: formValue.detection_prompt || '',
      action: formValue.action || '',
      fallback_timeout_prompt: formValue.fallback_timeout_prompt || '',
      fallback_error_prompt: formValue.fallback_error_prompt || '',
      examples: Array.isArray(formValue.examples) ? formValue.examples : [],
      parameters: Array.isArray(formValue.parameters) ? formValue.parameters : []
    };
  
    this.dialogRef.close(result);
  }

  cancel() {
    this.dialogRef.close(null);
  }
}