|
@@ -31,6 +31,16 @@ interface Teacher {
|
|
|
objectId?: string; // 添加数据库对象ID
|
|
|
}
|
|
|
|
|
|
+// 定义会话数据接口
|
|
|
+interface ChatSessionData {
|
|
|
+ userId: string;
|
|
|
+ title: string;
|
|
|
+ messages: ChatMessage[];
|
|
|
+ teacher: Teacher;
|
|
|
+ createdAt: Date;
|
|
|
+ updatedAt: Date;
|
|
|
+}
|
|
|
+
|
|
|
@Component({
|
|
|
selector: 'app-tab1',
|
|
|
templateUrl: 'tab1.page.html',
|
|
@@ -58,6 +68,8 @@ interface Teacher {
|
|
|
],
|
|
|
})
|
|
|
export class Tab1Page {
|
|
|
+ @ViewChild('saveButton') saveButton: any;
|
|
|
+ @ViewChild('historyButton') historyButton: any;
|
|
|
@ViewChild(IonContent) content!: IonContent;
|
|
|
|
|
|
messages: ChatMessage[] = [];
|
|
@@ -113,9 +125,9 @@ export class Tab1Page {
|
|
|
constructor(
|
|
|
private dbService: DatabaseService,
|
|
|
private router: Router,
|
|
|
- private chatSessionService: ChatSessionService,
|
|
|
private alertController: AlertController
|
|
|
) {
|
|
|
+ console.log('Tab1Page 构造函数被调用'); // 添加日志
|
|
|
addIcons({
|
|
|
send,
|
|
|
personCircleOutline,
|
|
@@ -124,9 +136,22 @@ export class Tab1Page {
|
|
|
saveOutline,
|
|
|
timeOutline
|
|
|
});
|
|
|
+
|
|
|
+ // 初始化必要的属性
|
|
|
+ this.messages = [];
|
|
|
+ this.userInput = '';
|
|
|
+ this.isLoading = false;
|
|
|
+ this.isComplete = false;
|
|
|
+ this.currentResponse = '';
|
|
|
+
|
|
|
this.loadTeachers();
|
|
|
}
|
|
|
|
|
|
+ ngAfterViewInit() {
|
|
|
+ console.log('保存按钮元素:', this.saveButton);
|
|
|
+ console.log('历史按钮元素:', this.historyButton);
|
|
|
+ }
|
|
|
+
|
|
|
// 添加加载教师方法
|
|
|
async loadTeachers() {
|
|
|
try {
|
|
@@ -389,74 +414,112 @@ ${this.selectedTeacher.description}
|
|
|
this.router.navigate(['/chat-history']);
|
|
|
}
|
|
|
|
|
|
- // 修改 saveCurrentSession 方法,添加日志
|
|
|
+ // 修改保存会话方法
|
|
|
async saveCurrentSession() {
|
|
|
- console.log('Saving current session...'); // 添加日志
|
|
|
- // 检查用户是否登录
|
|
|
- const currentUser = new CloudUser();
|
|
|
- if (!currentUser.id) {
|
|
|
- await this.showAlert('提示', '请先登录后再保存会话');
|
|
|
- return;
|
|
|
- }
|
|
|
+ console.log('保存按钮被点击');
|
|
|
+ console.log('按钮状态:', {
|
|
|
+ disabled: this.messages.length === 0,
|
|
|
+ messagesLength: this.messages.length
|
|
|
+ });
|
|
|
+
|
|
|
+ console.log('开始保存会话...'); // 添加日志
|
|
|
+ try {
|
|
|
+ // 检查用户是否登录
|
|
|
+ const currentUser = new CloudUser();
|
|
|
+ console.log('当前用户:', currentUser); // 添加日志
|
|
|
+ const userId = currentUser.id;
|
|
|
+ console.log('用户ID:', userId); // 添加日志
|
|
|
+
|
|
|
+ if (!userId) {
|
|
|
+ console.log('用户未登录'); // 添加日志
|
|
|
+ await this.showAlert('提示', '请先登录后再保存会话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // 检查是否有可保存的消息
|
|
|
- if (this.messages.length === 0) {
|
|
|
- await this.showAlert('提示', '当前没有可保存的对话');
|
|
|
- return;
|
|
|
- }
|
|
|
+ // 检查是否有可保存的消息
|
|
|
+ console.log('消息数量:', this.messages.length); // 添加日志
|
|
|
+ if (this.messages.length === 0) {
|
|
|
+ console.log('没有消息可保存'); // 添加日志
|
|
|
+ await this.showAlert('提示', '当前没有可保存的对话');
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // 过滤掉隐藏的设置消息
|
|
|
- const visibleMessages = this.messages.filter(msg => !msg.isHidden);
|
|
|
- if (visibleMessages.length === 0) {
|
|
|
- await this.showAlert('提示', '当前没有可保存的对话');
|
|
|
- return;
|
|
|
- }
|
|
|
+ // 过滤掉隐藏的设置消息
|
|
|
+ const visibleMessages = this.messages.filter(msg => !msg.isHidden);
|
|
|
+ console.log('可见消息数量:', visibleMessages.length); // 添加日志
|
|
|
+
|
|
|
+ if (visibleMessages.length === 0) {
|
|
|
+ console.log('没有可见消息可保存'); // 添加日志
|
|
|
+ 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('错误', '保存失败,请重试');
|
|
|
+ console.log('显示保存对话框'); // 添加日志
|
|
|
+ const alert = await this.alertController.create({
|
|
|
+ header: '保存会话',
|
|
|
+ inputs: [
|
|
|
+ {
|
|
|
+ name: 'title',
|
|
|
+ type: 'text',
|
|
|
+ placeholder: '请输入会话标题(可选)'
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ buttons: [
|
|
|
+ {
|
|
|
+ text: '取消',
|
|
|
+ role: 'cancel',
|
|
|
+ handler: () => {
|
|
|
+ console.log('取消保存'); // 添加日志
|
|
|
+ }
|
|
|
+ },
|
|
|
+ {
|
|
|
+ text: '保存',
|
|
|
+ handler: async (data) => {
|
|
|
+ try {
|
|
|
+ console.log('开始保存过程, 输入数据:', data); // 添加日志
|
|
|
+ // 如果用户没有输入标题,使用默认标题
|
|
|
+ const title = data.title?.trim() ||
|
|
|
+ `与${this.selectedTeacher.name}的对话 - ${new Date().toLocaleString()}`;
|
|
|
+
|
|
|
+ console.log('使用标题:', title); // 添加日志
|
|
|
+
|
|
|
+ // 准备会话数据
|
|
|
+ const sessionData: ChatSessionData = {
|
|
|
+ userId,
|
|
|
+ title,
|
|
|
+ messages: visibleMessages,
|
|
|
+ teacher: {
|
|
|
+ id: this.selectedTeacher.id,
|
|
|
+ name: this.selectedTeacher.name,
|
|
|
+ description: this.selectedTeacher.description,
|
|
|
+ systemPrompt: this.selectedTeacher.systemPrompt
|
|
|
+ },
|
|
|
+ createdAt: new Date(),
|
|
|
+ updatedAt: new Date()
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('准备保存的会话数据:', sessionData); // 添加日志
|
|
|
+
|
|
|
+ // 保存会话
|
|
|
+ await this.dbService.createChatSession(sessionData);
|
|
|
+ console.log('会话保存成功'); // 添加日志
|
|
|
+ await this.showAlert('成功', '会话已保存');
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存会话失败:', error);
|
|
|
+ await this.showAlert('错误', '保存失败,请重试');
|
|
|
+ }
|
|
|
+ return true; // 确保对话框关闭
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- ]
|
|
|
- });
|
|
|
+ ]
|
|
|
+ });
|
|
|
|
|
|
- await alert.present();
|
|
|
+ console.log('显示对话框'); // 添加日志
|
|
|
+ await alert.present();
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存会话过程中发生错误:', error);
|
|
|
+ await this.showAlert('错误', '保存失败,请重试');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 辅助方法:显示提示框
|