summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/views/maas/use/use-application.component.ts
blob: 96b17fd07a4d9bf45818209ad5501cb3493338f9 (plain)
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
import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { HttpClient } from '@angular/common/http';
import { SSE } from "sse.js";
import { ActivatedRoute } from '@angular/router';
import { MaasService } from '@src/app/core/services/maas.service';

@Component({
  selector: 'app-use-application',
  templateUrl: './use-application.component.html',
  styleUrls: ['./use-application.component.less']
})
export class UseApplicationComponent implements OnInit {

  question: string;
  communicationMessage: string;
  chatHistory: { question: string, answer: string }[] = [];
  apiUrl = '/api/usecaseui-llm-adaptation/v1/application/chat';
  queryParams: { id?: string; name?: string } = {};
  selectedName: string | null = null;
  options: any[] = [];

  constructor(
    private http: HttpClient,
    private message: NzMessageService,
    private route: ActivatedRoute,
    private myhttp: MaasService,
  ) { }

  ngOnInit() {
    this.fetchAllApplication();
    this.route.queryParams.subscribe(params => {
      this.queryParams = params;
      this.selectedName = this.queryParams.id;
    });
  }

  submitQuestion() {
    const chatParam = {
      applicationId: this.selectedName,
      question: this.question
    };
    const source = new SSE(this.apiUrl, { headers: { 'Content-Type': 'application/json' }, payload: JSON.stringify(chatParam), method: 'POST' });
    const lin = this.question;
    source.addEventListener('message', (event) => {
      const existingEntryIndex = this.chatHistory.findIndex(entry => entry.question === lin);
      if (existingEntryIndex !== -1) {
        this.chatHistory[existingEntryIndex].answer += event.data.replace(/__SPACE__/g, ' ');
      } else {
        this.chatHistory.push({ question: lin, answer: event.data });
      }
    });
    this.question = '';
  }

  fetchAllApplication(): void {
    this.myhttp.getAllApplication()
      .subscribe(
        (data) => {
          this.options = data.result_body.map(item => ({
            nzValue: item.applicationId,
            nzLabel: item.applicationName
          }));
        },
        () => {
          this.message.error('Failed to obtain intent data');
        }
      )
  }
}