9984f66488b79bfb7c4bb780a3b1be16153aeb47
[usecase-ui.git] /
1 import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
2 import { IntentManagementService } from '../../../../core/services/intentManagement.service';
3 import { Util } from '../../../../shared/utils/utils';
4 import { NzMessageService, UploadFile } from 'ng-zorro-antd';
5 import { HttpClient, HttpHeaders } from '@angular/common/http';
6 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
7
8 @Component({
9   selector: 'app-input-knowledge-base',
10   templateUrl: './input-knowledge-base.component.html',
11   styleUrls: ['./input-knowledge-base.component.less']
12 })
13 export class InputKnowledgeBaseComponent implements OnInit {
14   title = 'Add Knowledge Base';
15   constructor(
16     private myhttp: IntentManagementService,
17     private Util: Util,
18     private message: NzMessageService,
19     private http: HttpClient,
20     private fb: FormBuilder
21   ) { }
22
23   @Input() showModel: boolean;
24   @Output() modalOpreation = new EventEmitter();
25
26   apiUrl = '/api/usecaseui-llm-adaptation/v1/knowledgeBase/create';
27   apiUrl1 = '/api/usecaseui-intent-analysis/v1/intents/upload';
28   maasUrl = '/api/usecaseui-llm-adaptation/v1/operator/maas/getAll'
29   url = "http://172.22.16.126:3000/api/core/dataset/create";
30   knowledgeBase = {
31     name: '',
32     description: ''
33   };
34   fileList: UploadFile[] = [];
35   operators: any[] = [];
36   selectedOperator: any = null;
37   filteredPlatforms: any[] = [];
38   allPlatforms: any[] = [];
39   selectedPlatform: any = null;
40   validateForm: FormGroup;
41
42   ngOnInit() {
43     this.fetchOperators();
44     this.validateForm = this.fb.group({
45       name: [null],
46       description: [null],
47       selectedOperator: [null],
48       selectedPlatform: [null],
49     });
50   }
51
52   fetchOperators(): void {
53     this.http.get<any>(this.maasUrl).subscribe(
54       (response) => {
55         console.log(response);
56         this.operators = response.result_body;
57       },
58       (error) => {
59         this.message.error('Failed to fetch operators');
60       }
61     );
62   }
63
64   submitForm(): void {
65     for (const i in this.validateForm.controls) {
66       this.validateForm.controls[i].markAsDirty();
67       this.validateForm.controls[i].updateValueAndValidity();
68     }
69   }
70
71   handleOperatorChange(value: any): void {
72     if (value) {
73       this.filteredPlatforms = value.maaSPlatformList;
74     } else {
75       this.filteredPlatforms = [];
76     }
77     this.validateForm.get('selectedPlatform').setValue(null);
78   }
79
80
81   beforeUpload = (file: UploadFile): boolean => {
82     this.fileList.push(file);
83     return false;
84   }
85
86   handleCancel(): void {
87     this.showModel = false;
88     this.modalOpreation.emit({ "cancel": true });
89   }
90   handleOk(): void {
91     const formData = new FormData();
92     const metaData = {
93       knowledgeBaseName: this.validateForm.controls.name.value,
94       knowledgeBaseDescription: this.validateForm.controls.description.value,
95       operatorId: this.validateForm.controls.selectedOperator.value.operatorId,
96       operatorName: this.validateForm.controls.selectedOperator.value.operatorName,
97       maaSPlatformId: this.validateForm.controls.selectedPlatform.value.maaSPlatformId,
98       maaSPlatformName: this.validateForm.controls.selectedPlatform.value.maaSPlatformName
99     };
100     const metaDataJson = JSON.stringify(metaData);
101     formData.append('metaData', metaDataJson);
102     this.fileList.forEach((file: any) => {
103       formData.append('files', file);
104     });
105     this.http.post<any>(this.apiUrl, formData).subscribe(
106       (response) => {
107         if (response.result_header.result_code === 200) {
108           this.message.success('Created successfully');
109         } else {
110           this.message.error(response.result_header.result_message);
111         }
112         this.knowledgeBase = {
113           name: '',
114           description: ''
115         };
116         this.fileList = [];
117         this.modalOpreation.emit({ "cancel": false });
118       },
119       (error) => {
120         this.knowledgeBase = {
121           name: '',
122           description: ''
123         };
124         this.fileList = [];
125         console.log('Upload failed', error);
126       }
127     );
128   }
129 }