|
@@ -1,14 +1,16 @@
|
|
|
import { Component, ViewChild } from '@angular/core';
|
|
|
-import { IonHeader, IonToolbar, IonTitle, IonContent, IonFooter, IonItem, IonTextarea, IonButton, IonIcon, IonModal, IonList } from '@ionic/angular/standalone';
|
|
|
+import { IonHeader, IonToolbar, IonTitle, IonContent, IonFooter, IonItem, IonTextarea, IonButton, IonIcon, IonModal, IonList, IonButtons } from '@ionic/angular/standalone';
|
|
|
import { FormsModule } from '@angular/forms';
|
|
|
import { NgClass, NgFor, NgIf, DatePipe } from '@angular/common';
|
|
|
import { addIcons } from 'ionicons';
|
|
|
-import { send, personCircleOutline, addCircleOutline, createOutline } from 'ionicons/icons';
|
|
|
+import { send, personCircleOutline, addCircleOutline, createOutline, saveOutline, timeOutline } from 'ionicons/icons';
|
|
|
import { FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';
|
|
|
import { DatabaseService } from 'src/app/services/database.service';
|
|
|
import { Router } from '@angular/router';
|
|
|
import Parse from 'parse';
|
|
|
import { CloudUser } from 'src/lib/ncloud';
|
|
|
+import { ChatSessionService } from '../services/chat-session.service';
|
|
|
+import { AlertController } from '@ionic/angular/standalone';
|
|
|
|
|
|
// 定义消息接口
|
|
|
interface ChatMessage {
|
|
@@ -46,6 +48,7 @@ interface Teacher {
|
|
|
IonIcon,
|
|
|
IonModal,
|
|
|
IonList,
|
|
|
+ IonButtons,
|
|
|
FormsModule,
|
|
|
NgClass,
|
|
|
NgFor,
|
|
@@ -109,9 +112,18 @@ export class Tab1Page {
|
|
|
|
|
|
constructor(
|
|
|
private dbService: DatabaseService,
|
|
|
- private router: Router
|
|
|
+ private router: Router,
|
|
|
+ private chatSessionService: ChatSessionService,
|
|
|
+ private alertController: AlertController
|
|
|
) {
|
|
|
- addIcons({ send, personCircleOutline, addCircleOutline, createOutline });
|
|
|
+ addIcons({
|
|
|
+ send,
|
|
|
+ personCircleOutline,
|
|
|
+ addCircleOutline,
|
|
|
+ createOutline,
|
|
|
+ saveOutline,
|
|
|
+ timeOutline
|
|
|
+ });
|
|
|
this.loadTeachers();
|
|
|
}
|
|
|
|
|
@@ -364,4 +376,96 @@ ${this.selectedTeacher.description}
|
|
|
event.stopPropagation(); // 阻止事件冒泡
|
|
|
this.router.navigate(['/custom-teacher/edit', teacherId]);
|
|
|
}
|
|
|
+
|
|
|
+ // 修改 viewChatHistory 方法,添加日志
|
|
|
+ viewChatHistory() {
|
|
|
+ console.log('Viewing chat history...'); // 添加日志
|
|
|
+ // 检查用户是否登录
|
|
|
+ const currentUser = new CloudUser();
|
|
|
+ if (!currentUser.id) {
|
|
|
+ this.showAlert('提示', '请先登录后再查看历史会话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ this.router.navigate(['/chat-history']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改 saveCurrentSession 方法,添加日志
|
|
|
+ async saveCurrentSession() {
|
|
|
+ console.log('Saving current session...'); // 添加日志
|
|
|
+ // 检查用户是否登录
|
|
|
+ const currentUser = new CloudUser();
|
|
|
+ if (!currentUser.id) {
|
|
|
+ await this.showAlert('提示', '请先登录后再保存会话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否有可保存的消息
|
|
|
+ if (this.messages.length === 0) {
|
|
|
+ await this.showAlert('提示', '当前没有可保存的对话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 过滤掉隐藏的设置消息
|
|
|
+ const visibleMessages = this.messages.filter(msg => !msg.isHidden);
|
|
|
+ if (visibleMessages.length === 0) {
|
|
|
+ await this.showAlert('提示', '当前没有可保存的对话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '保存会话',
|
|
|
+ inputs: [
|
|
|
+ {
|
|
|
+ name: 'title',
|
|
|
+ type: 'text',
|
|
|
+ placeholder: '请输入会话标题(可选)'
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '保存',
|
|
|
+ handler: async (data) => {
|
|
|
+ try {
|
|
|
+ // 如果用户没有输入标题,使用默认标题
|
|
|
+ const title = data.title?.trim() ||
|
|
|
+ `与${this.selectedTeacher.name}的对话 - ${new Date().toLocaleString()}`;
|
|
|
+
|
|
|
+ // 保存会话
|
|
|
+ await this.chatSessionService.saveSession(
|
|
|
+ title,
|
|
|
+ visibleMessages, // 只保存可见消息
|
|
|
+ {
|
|
|
+ id: this.selectedTeacher.id,
|
|
|
+ name: this.selectedTeacher.name,
|
|
|
+ description: this.selectedTeacher.description,
|
|
|
+ systemPrompt: this.selectedTeacher.systemPrompt
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ await this.showAlert('成功', '会话已保存');
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存会话失败:', error);
|
|
|
+ await this.showAlert('错误', '保存失败,请重试');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ });
|
|
|
+
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助方法:显示提示框
|
|
|
+ private async showAlert(header: string, message: string) {
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header,
|
|
|
+ message,
|
|
|
+ buttons: ['确定']
|
|
|
+ });
|
|
|
+ await alert.present();
|
|
|
+ }
|
|
|
}
|