Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

chat-message.component.ts 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Component, Input, ChangeDetectionStrategy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { MatIconModule } from '@angular/material/icon';
  4. import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
  5. import { MarkdownPipe } from '../../pipes/markdown.pipe';
  6. export interface ChatMessageInput {
  7. role: 'user' | 'assistant' | 'system';
  8. content: string; // markdown内容
  9. timestamp?: Date; // 可选时间戳
  10. loading?: boolean; // 加载状态
  11. showAvatar?: boolean; // 是否显示头像(默认true)
  12. showTimestamp?: boolean; // 是否显示时间(默认true)
  13. showHeader?: boolean; // 是否显示头部(默认true)
  14. }
  15. @Component({
  16. selector: 'app-chat-message',
  17. standalone: true,
  18. imports: [
  19. CommonModule,
  20. MatIconModule,
  21. MatProgressSpinnerModule,
  22. MarkdownPipe
  23. ],
  24. templateUrl: './chat-message.component.html',
  25. styleUrl: './chat-message.component.scss',
  26. changeDetection: ChangeDetectionStrategy.OnPush
  27. })
  28. export class ChatMessageComponent {
  29. @Input() message!: ChatMessageInput;
  30. // 默认值设置
  31. get displayAvatar(): boolean {
  32. return this.message.showAvatar !== false;
  33. }
  34. get displayTimestamp(): boolean {
  35. return this.message.showTimestamp !== false;
  36. }
  37. get displayHeader(): boolean {
  38. return this.message.showHeader !== false;
  39. }
  40. get formattedTimestamp(): string {
  41. if (!this.message.timestamp) return '';
  42. return this.formatTime(this.message.timestamp);
  43. }
  44. get avatarIcon(): string {
  45. switch (this.message.role) {
  46. case 'user': return 'person';
  47. case 'assistant': return 'smart_toy';
  48. case 'system': return 'computer';
  49. default: return 'person';
  50. }
  51. }
  52. get avatarLabel(): string {
  53. switch (this.message.role) {
  54. case 'user': return '用户';
  55. case 'assistant': return 'AI助手';
  56. case 'system': return '系统';
  57. default: return '用户';
  58. }
  59. }
  60. private formatTime(date: Date): string {
  61. const d = new Date(date);
  62. const hours = d.getHours().toString().padStart(2, '0');
  63. const minutes = d.getMinutes().toString().padStart(2, '0');
  64. return `${hours}:${minutes}`;
  65. }
  66. }