|
@@ -1,14 +1,14 @@
|
|
import { CommonModule } from '@angular/common';
|
|
import { CommonModule } from '@angular/common';
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { FormsModule } from '@angular/forms';
|
|
import { FormsModule } from '@angular/forms';
|
|
-import { NavController } from '@ionic/angular';
|
|
+import { NavController } from '@ionic/angular';
|
|
-import { IonButtons, IonHeader, IonToolbar,IonButton, IonIcon, IonTitle, IonInput, IonFooter, IonContent, AlertController, } from '@ionic/angular/standalone';
|
|
+import { IonButtons, IonHeader, IonToolbar, IonButton, IonIcon, IonTitle, IonInput, IonFooter, IonContent, AlertController } from '@ionic/angular/standalone';
|
|
import { FmodeChatCompletion } from 'fmode-ng';
|
|
import { FmodeChatCompletion } from 'fmode-ng';
|
|
import { addIcons } from 'ionicons';
|
|
import { addIcons } from 'ionicons';
|
|
-import { chevronBackSharp, closeCircleOutline, ellipsisHorizontal, happyOutline, micCircleOutline, paperPlane, sendOutline } from 'ionicons/icons';
|
|
+import { chevronBackSharp, closeCircleOutline, ellipsisHorizontal, happyOutline, micCircleOutline, paperPlane, sendOutline } from 'ionicons/icons';
|
|
import { IonModal, IonLabel } from '@ionic/angular/standalone';
|
|
import { IonModal, IonLabel } from '@ionic/angular/standalone';
|
|
|
|
|
|
-addIcons({ chevronBackSharp,ellipsisHorizontal,micCircleOutline,happyOutline,paperPlane,closeCircleOutline,sendOutline });
|
|
+addIcons({ chevronBackSharp, ellipsisHorizontal, micCircleOutline, happyOutline, paperPlane, closeCircleOutline, sendOutline });
|
|
|
|
|
|
interface Window {
|
|
interface Window {
|
|
SpeechRecognition: any;
|
|
SpeechRecognition: any;
|
|
@@ -21,76 +21,23 @@ interface Window {
|
|
styleUrls: ['./ai-chat-component.component.scss'],
|
|
styleUrls: ['./ai-chat-component.component.scss'],
|
|
standalone: true,
|
|
standalone: true,
|
|
imports: [
|
|
imports: [
|
|
- IonHeader,IonToolbar,IonButtons,IonButton,IonIcon,IonTitle,IonInput,IonFooter,CommonModule,IonContent,
|
|
+ IonHeader, IonToolbar, IonButtons, IonButton, IonIcon, IonTitle, IonInput, IonFooter, CommonModule, IonContent,
|
|
- FormsModule,IonModal,IonLabel,
|
|
+ FormsModule, IonModal, IonLabel,
|
|
],
|
|
],
|
|
-
|
|
|
|
})
|
|
})
|
|
-export class AiChatComponentComponent implements OnInit {
|
|
+export class AiChatComponentComponent implements OnInit {
|
|
|
|
|
|
messages: { text: string, sender: string }[] = [];
|
|
messages: { text: string, sender: string }[] = [];
|
|
userMessage: string = '';
|
|
userMessage: string = '';
|
|
aiMessage: string = '';
|
|
aiMessage: string = '';
|
|
initialPrompt: string = '';
|
|
initialPrompt: string = '';
|
|
- recognition: any;
|
|
|
|
- recognizedContent: string = '';
|
|
|
|
timer: string = '00:00';
|
|
timer: string = '00:00';
|
|
interval: any;
|
|
interval: any;
|
|
elapsedSeconds: number = 0;
|
|
elapsedSeconds: number = 0;
|
|
- isLoading: boolean = true;
|
|
+ isLoading: boolean = true;
|
|
- isVoiceModalOpen=false;
|
|
|
|
|
|
|
|
- constructor(private navCtrl: NavController,private alertController: AlertController) {
|
|
|
|
-
|
|
|
|
- this.initSpeechRecognition();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-initSpeechRecognition() {
|
|
|
|
- const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition;
|
|
|
|
- if (SpeechRecognition) {
|
|
|
|
- this.recognition = new SpeechRecognition();
|
|
|
|
- this.recognition.lang = 'zh-CN';
|
|
|
|
- this.recognition.interimResults = false;
|
|
|
|
- this.recognition.maxAlternatives = 1;
|
|
|
|
- this.recognition.continuous = true;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-this.recognition.onresult = (event: any) => {
|
|
|
|
- this.recognizedContent += event.results[event.results.length - 1][0].transcript;
|
|
|
|
-
|
|
|
|
- console.log("识别到的内容:", this.recognizedContent);
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- this.recognition.onerror = (event: any) => {
|
|
|
|
- if (event.error === 'no-speech') {
|
|
|
|
- console.warn('没有检测到语音,继续监听...');
|
|
|
|
- } else {
|
|
|
|
- console.error('语音识别错误:', event.error);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- } else {
|
|
|
|
- console.log('该浏览器不支持语音识别');
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-startVoice() {
|
|
|
|
- this.isVoiceModalOpen=true;
|
|
|
|
- if (this.recognition && this.recognition.state !== 'active') {
|
|
|
|
- this.recognition.start();
|
|
|
|
- console.log('语音识别启动...');
|
|
|
|
- this.startTimer();
|
|
|
|
- } else {
|
|
|
|
- console.warn('语音识别已经在运行中');
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
|
|
-cancleVoice() {
|
|
+ constructor(private navCtrl: NavController) {}
|
|
- this.isVoiceModalOpen=false;
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
startTimer() {
|
|
startTimer() {
|
|
@@ -108,35 +55,10 @@ cancleVoice() {
|
|
padZero(num: number): string {
|
|
padZero(num: number): string {
|
|
return num < 10 ? '0' + num : num.toString();
|
|
return num < 10 ? '0' + num : num.toString();
|
|
}
|
|
}
|
|
-
|
|
|
|
-cancelVoiceInput() {
|
|
|
|
- if (this.recognition) {
|
|
|
|
- this.recognition.stop();
|
|
|
|
- console.log('语音识别已停止');
|
|
|
|
- clearInterval(this.interval);
|
|
|
|
- this.timer = '00:00';
|
|
|
|
- this.recognizedContent = '';
|
|
|
|
- }
|
|
|
|
- this.isVoiceModalOpen=false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-sendVoiceInput() {
|
|
|
|
- if (this.recognition) {
|
|
|
|
- this.recognition.stop();
|
|
|
|
- console.log('语音识别已停止');
|
|
|
|
- clearInterval(this.interval);
|
|
|
|
- this.timer = '00:00';
|
|
|
|
|
|
|
|
-
|
|
+
|
|
- this.userMessage += this.recognizedContent.trim();
|
|
+ goBack(): void {
|
|
-
|
|
+ this.navCtrl.back();
|
|
- this.recognizedContent = '';
|
|
|
|
- }
|
|
|
|
- this.isVoiceModalOpen=false;
|
|
|
|
-}
|
|
|
|
- goBack() {
|
|
|
|
- this.navCtrl.back();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
openOptions() {
|
|
openOptions() {
|
|
@@ -149,7 +71,7 @@ sendVoiceInput() {
|
|
this.initializeChat();
|
|
this.initializeChat();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
+
|
|
initializeChat() {
|
|
initializeChat() {
|
|
this.initialPrompt = `
|
|
this.initialPrompt = `
|
|
#角色设定
|
|
#角色设定
|
|
@@ -192,7 +114,6 @@ sendVoiceInput() {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
sendMessage() {
|
|
sendMessage() {
|
|
|
|
|
|
if (this.userMessage.trim()) {
|
|
if (this.userMessage.trim()) {
|
|
@@ -200,16 +121,14 @@ sendVoiceInput() {
|
|
const conversationHistory = this.messages.map(msg => ({
|
|
const conversationHistory = this.messages.map(msg => ({
|
|
role: msg.sender === 'user' ? 'user' : 'assistant',
|
|
role: msg.sender === 'user' ? 'user' : 'assistant',
|
|
content: msg.text
|
|
content: msg.text
|
|
- }));
|
|
+ }));
|
|
|
|
|
|
|
|
|
|
- conversationHistory.unshift({ role: 'user', content: this.initialPrompt });
|
|
+ conversationHistory.unshift({ role: 'user', content: this.initialPrompt });
|
|
- this.isLoading = true;
|
|
+ this.isLoading = true;
|
|
this.messages.push({ text: this.userMessage, sender: 'user' });
|
|
this.messages.push({ text: this.userMessage, sender: 'user' });
|
|
console.log("发送消息:", this.messages);
|
|
console.log("发送消息:", this.messages);
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
conversationHistory.push({ role: 'user', content: this.userMessage });
|
|
conversationHistory.push({ role: 'user', content: this.userMessage });
|
|
|
|
|
|
@@ -230,37 +149,9 @@ sendVoiceInput() {
|
|
this.messages.push({ text: this.aiMessage, sender: 'ai' });
|
|
this.messages.push({ text: this.aiMessage, sender: 'ai' });
|
|
this.aiMessage = '';
|
|
this.aiMessage = '';
|
|
console.log("发送消息:", this.messages);
|
|
console.log("发送消息:", this.messages);
|
|
- console.log("历史对话"+conversationHistory[0].content+" "+conversationHistory[1].content+" "+conversationHistory[2].content)
|
|
+ console.log("历史对话" + conversationHistory[0].content + " " + conversationHistory[1].content + " " + conversationHistory[2].content)
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- isEmojiPickerOpen: boolean = false;
|
|
|
|
- emojis: string[] = ['😀', '😃', '😄', '😁', '😆', '😅', '🤣', '😂', '🙂', '🙃', '🫠', '😉', '😊', '😇', '🥰', '😍', '🤩', '😘',
|
|
|
|
- '😗', '☺️', '😚', '😙', '🥲', '😋', '😛', '😜', '🤪', '😝', '🤑', '🤗', '🤭', '🫢', '🫣', '🤫', '🤔', '🫡', '🤐', '🤨', '😐',
|
|
|
|
- '😑', '😶', '🫥', '😶🌫️', '😏', '😒', '🙄', '😬', '😮💨', '🤥', '🫨', '🙂↔️', '🙂↕️', '😌', '😔', '😪', '🤤', '😴', '', '😷',
|
|
|
|
- '🤒', '🤕', '🤢', '🤮', '🤧', '🥵', '🥶', '🥴', '😵', '😵💫', '🤯', '🤠', '🥳', '🥸', '😎', '🤓', '🧐', '😕', '🫤', '😟',
|
|
|
|
- '🙁', '☹️', '😮', '😯', '😲', '😳', '🥺', '🥹', '😦', '😧', '😨', '😰', '😥', '😢', '😭', '😱', '😖', '😣', '😞', '😓',
|
|
|
|
- '😩', '😫', '🥱', '😤', '😡', '😠', '🤬', '😈', '👿', '💀', '☠️', '💩', '🤡', '👹', '👺', '👻', '👽', '👾', '🤖', '😺',
|
|
|
|
- '😸', '😹', '😻', '😼', '😽', '🙀', '😿', '😾', '🙈', '🙉', '🙊', '💌', '💘', '❤️', '🖤', '💋', '💯', '💢', '💥', '💫',
|
|
|
|
- '💦', '💤'];
|
|
|
|
-
|
|
|
|
-openEmojiPicker() {
|
|
|
|
- this.isEmojiPickerOpen = true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-closeEmojiPicker() {
|
|
|
|
- this.isEmojiPickerOpen = false;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-addEmoji(emoji: string) {
|
|
|
|
- this.userMessage += emoji;
|
|
|
|
- this.closeEmojiPicker();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|