|
@@ -1,14 +1,14 @@
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
-import { NavController } from '@ionic/angular';
|
|
|
-import { IonButtons, IonHeader, IonToolbar,IonButton, IonIcon, IonTitle, IonInput, IonFooter, IonContent, AlertController, } from '@ionic/angular/standalone';
|
|
|
+import { NavController } from '@ionic/angular';
|
|
|
+import { IonButtons, IonHeader, IonToolbar, IonButton, IonIcon, IonTitle, IonInput, IonFooter, IonContent, AlertController } from '@ionic/angular/standalone';
|
|
|
import { FmodeChatCompletion } from 'fmode-ng';
|
|
|
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'; // 导入独立组件
|
|
|
|
|
|
-addIcons({ chevronBackSharp,ellipsisHorizontal,micCircleOutline,happyOutline,paperPlane,closeCircleOutline,sendOutline });
|
|
|
+addIcons({ chevronBackSharp, ellipsisHorizontal, micCircleOutline, happyOutline, paperPlane, closeCircleOutline, sendOutline });
|
|
|
|
|
|
interface Window {
|
|
|
SpeechRecognition: any; // 声明 SpeechRecognition 属性
|
|
@@ -21,76 +21,23 @@ interface Window {
|
|
|
styleUrls: ['./ai-chat-component.component.scss'],
|
|
|
standalone: true,
|
|
|
imports: [
|
|
|
- IonHeader,IonToolbar,IonButtons,IonButton,IonIcon,IonTitle,IonInput,IonFooter,CommonModule,IonContent,
|
|
|
- FormsModule,IonModal,IonLabel,
|
|
|
+ IonHeader, IonToolbar, IonButtons, IonButton, IonIcon, IonTitle, IonInput, IonFooter, CommonModule, IonContent,
|
|
|
+ FormsModule, IonModal, IonLabel,
|
|
|
],
|
|
|
-
|
|
|
})
|
|
|
-export class AiChatComponentComponent implements OnInit {
|
|
|
+export class AiChatComponentComponent implements OnInit {
|
|
|
|
|
|
messages: { text: string, sender: string }[] = []; // 存储聊天消息
|
|
|
userMessage: string = ''; // 用于用户输入内容
|
|
|
aiMessage: string = ''; // 用于存储AI的回复
|
|
|
initialPrompt: string = ''; // 用于存储初始化提示
|
|
|
- recognition: any; // 用于存储语音识别实例
|
|
|
- recognizedContent: string = ''; // 用于存储识别到的语音内容
|
|
|
timer: string = '00:00'; // 用于显示计时器
|
|
|
interval: any; // 用于存储定时器的引用
|
|
|
elapsedSeconds: number = 0; // 计时器的秒数
|
|
|
- isLoading: boolean = true; // AI生成文本加载状态,刚开始AI向你打招呼,所以处于加载状态
|
|
|
- isVoiceModalOpen=false; // 语音识别modal默认关闭
|
|
|
+ isLoading: boolean = true; // AI生成文本加载状态
|
|
|
|
|
|
- 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() {
|
|
|
- this.isVoiceModalOpen=false;
|
|
|
-}
|
|
|
+ constructor(private navCtrl: NavController) {}
|
|
|
|
|
|
// 启动计时器
|
|
|
startTimer() {
|
|
@@ -108,35 +55,10 @@ cancleVoice() {
|
|
|
padZero(num: number): string {
|
|
|
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(); // 将识别内容赋值给输入框,并去除多余空格
|
|
|
-
|
|
|
- this.recognizedContent = ''; // 清空识别内容
|
|
|
- }
|
|
|
- this.isVoiceModalOpen=false;
|
|
|
-}
|
|
|
- goBack() {
|
|
|
- this.navCtrl.back(); // 返回上一页
|
|
|
+ // 返回上一页的处理函数
|
|
|
+ goBack(): void {
|
|
|
+ this.navCtrl.back(); // 调用 NavController 的 back 方法
|
|
|
}
|
|
|
|
|
|
openOptions() {
|
|
@@ -149,7 +71,7 @@ sendVoiceInput() {
|
|
|
this.initializeChat();
|
|
|
}
|
|
|
|
|
|
-//初始化聊天,将提示词添加到历史中
|
|
|
+ // 初始化聊天,将提示词添加到历史中
|
|
|
initializeChat() {
|
|
|
this.initialPrompt = `
|
|
|
#角色设定
|
|
@@ -192,7 +114,6 @@ sendVoiceInput() {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
sendMessage() {
|
|
|
// 发送消息的逻辑
|
|
|
if (this.userMessage.trim()) { // 确保消息不为空
|
|
@@ -200,16 +121,14 @@ sendVoiceInput() {
|
|
|
const conversationHistory = this.messages.map(msg => ({
|
|
|
role: msg.sender === 'user' ? 'user' : 'assistant',
|
|
|
content: msg.text
|
|
|
- }));
|
|
|
+ }));
|
|
|
|
|
|
// 将提示词直接添加到对话历史
|
|
|
- conversationHistory.unshift({ role: 'user', content: this.initialPrompt }); // 添加系统消息到历史
|
|
|
- this.isLoading = true; // 设置加载状态为 true
|
|
|
+ conversationHistory.unshift({ role: 'user', content: this.initialPrompt }); // 添加系统消息到历史
|
|
|
+ this.isLoading = true; // 设置加载状态为 true
|
|
|
this.messages.push({ text: this.userMessage, sender: 'user' }); // 添加用户消息到数组
|
|
|
console.log("发送消息:", this.messages); // 调试输出
|
|
|
|
|
|
-
|
|
|
-
|
|
|
// 将用户消息添加到对话历史
|
|
|
conversationHistory.push({ role: 'user', content: this.userMessage });
|
|
|
|
|
@@ -230,37 +149,9 @@ sendVoiceInput() {
|
|
|
this.messages.push({ text: this.aiMessage, sender: 'ai' }); // 添加消息到数组
|
|
|
this.aiMessage = ''; // 清空ai内容
|
|
|
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(); // 关闭模态框
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
}
|