|
@@ -0,0 +1,68 @@
|
|
|
+import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
|
|
|
+import { TestCompletion } from '../../../../lib/completion';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { FormsModule } from '@angular/forms';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-chat-assistant',
|
|
|
+ standalone:true,
|
|
|
+ imports: [CommonModule, FormsModule],
|
|
|
+ templateUrl: './chat-assistant.html',
|
|
|
+ styleUrls: ['./chat-assistant.scss']
|
|
|
+})
|
|
|
+export class ChatAssistant implements AfterViewInit {
|
|
|
+ @ViewChild('chatContainer') chatContainer!: ElementRef;
|
|
|
+
|
|
|
+ messages: Array<{role: string, content: string}> = [
|
|
|
+ {role: 'assistant', content: '您好!我是赣鄱小鹤,24小时为您服务。请问有什么可以帮您?'}
|
|
|
+ ];
|
|
|
+ userInput = '';
|
|
|
+ isLoading = false;
|
|
|
+
|
|
|
+ constructor() {}
|
|
|
+
|
|
|
+ ngAfterViewInit() {
|
|
|
+ this.scrollToBottom();
|
|
|
+ }
|
|
|
+
|
|
|
+ closeChat() {
|
|
|
+ // 这里添加关闭聊天窗口的逻辑
|
|
|
+ console.log('Close chat clicked');
|
|
|
+ // 如果需要实际关闭,可以添加相应逻辑
|
|
|
+ }
|
|
|
+
|
|
|
+ async sendMessage() {
|
|
|
+ if (!this.userInput.trim() || this.isLoading) return;
|
|
|
+
|
|
|
+ const userMessage = this.userInput;
|
|
|
+ this.messages.push({role: 'user', content: userMessage});
|
|
|
+ this.userInput = '';
|
|
|
+ this.isLoading = true;
|
|
|
+ this.scrollToBottom();
|
|
|
+
|
|
|
+ try {
|
|
|
+ const completion = new TestCompletion(this.messages);
|
|
|
+ await completion.sendMessage(null, (content) => {
|
|
|
+ // 更新最后一条消息内容
|
|
|
+ if (this.messages[this.messages.length - 1].role === 'assistant') {
|
|
|
+ this.messages[this.messages.length - 1].content = content;
|
|
|
+ } else {
|
|
|
+ this.messages.push({role: 'assistant', content});
|
|
|
+ }
|
|
|
+ this.scrollToBottom();
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ console.error('AI 请求失败:', error);
|
|
|
+ this.messages.push({role: 'assistant', content: '抱歉,处理您的请求时出现了问题。请稍后再试。'});
|
|
|
+ } finally {
|
|
|
+ this.isLoading = false;
|
|
|
+ this.scrollToBottom();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private scrollToBottom(): void {
|
|
|
+ setTimeout(() => {
|
|
|
+ this.chatContainer.nativeElement.scrollTop = this.chatContainer.nativeElement.scrollHeight;
|
|
|
+ }, 0);
|
|
|
+ }
|
|
|
+}
|