ai-continue-modal.component.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // ai-continue-modal.component.ts
  2. import { Component, Input } from '@angular/core';
  3. import { ModalController } from '@ionic/angular';
  4. import { IonButton, IonIcon, IonInput, IonItem, IonLabel, IonToolbar, IonHeader, IonContent, IonTitle, IonButtons, IonTextarea } from '@ionic/angular/standalone';
  5. import { FormsModule } from '@angular/forms';
  6. import { FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';
  7. @Component({
  8. selector: 'app-ai-continue-modal',
  9. templateUrl: './ai-continue-modal.component.html',
  10. styleUrls: ['./ai-continue-modal.component.scss'],
  11. standalone: true,
  12. imports: [
  13. IonButton, IonIcon, IonInput, IonItem, IonTitle, FormsModule,
  14. IonLabel, IonToolbar, IonHeader, IonContent, IonButtons, MarkdownPreviewModule, IonTextarea
  15. ]
  16. })
  17. export class AiContinueModalComponent {
  18. inputText: string = '';
  19. continuedText: string = '';
  20. userPrompt: string = "";
  21. responseMsg: any = "";
  22. isComplete: boolean = false;
  23. @Input() onInsertText?: (text: string) => void;
  24. constructor(private modalCtrl: ModalController) { }
  25. dismiss() {
  26. this.modalCtrl.dismiss();
  27. }
  28. promptInput(ev: any) {
  29. this.userPrompt = ev.detail.value;
  30. }
  31. sendMessage() {
  32. console.log("create");
  33. let PromptTemplate = `
  34. 您是一名专业的文字作者,请您根据用户提供的文本继续创作。
  35. 以下是用户的原文:${this.userPrompt}
  36. `;
  37. let completion = new FmodeChatCompletion([
  38. { role: "system", content: "" },
  39. { role: "user", content: PromptTemplate }
  40. ]);
  41. completion.sendCompletion().subscribe((message: any) => {
  42. // 打印消息体
  43. console.log(message.content);
  44. // 赋值消息内容给组件内属性
  45. this.responseMsg = message.content;
  46. this.continuedText = message.content; // 将生成的内容赋值给 continuedText
  47. if (message?.complete) { // 判断message为完成状态,则设置isComplete为完成
  48. this.isComplete = true;
  49. }
  50. });
  51. }
  52. copyContinuedText() {
  53. navigator.clipboard.writeText(this.continuedText).then(() => {
  54. alert('文本已复制到剪贴板');
  55. }, (err) => {
  56. console.error('未能复制文本:', err);
  57. });
  58. }
  59. insertIntoChapterEnd() {
  60. // 检查是否有选择的章节
  61. if (this.continuedText && this.onInsertText) {
  62. this.onInsertText(this.continuedText);
  63. this.dismiss();
  64. } else {
  65. alert('请先生成续写内容');
  66. }
  67. }
  68. regenerate() {
  69. // 清空输入框和结果区域
  70. this.userPrompt = '';
  71. this.responseMsg = '';
  72. this.continuedText = '';
  73. this.isComplete = false;
  74. // 重新发送请求
  75. this.sendMessage();
  76. }
  77. }