new-session-modal.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { UiAvatar, UiButton, UiModal } from '../../../ui';
  4. import { DeviceService } from '../../../core/device.service';
  5. import { LiveService } from '../../../core/live.service';
  6. import { NewSessionModalService } from './new-session-modal.service';
  7. import { WorkspaceService } from '../../../core/workspace.service';
  8. /**
  9. * 「新建对话」模态框(全局单例,挂在外壳上)
  10. *
  11. * 流程:选容器 → 在该容器下新建一条**空对话** → 落进「日常对话」→ 跳过去。
  12. *
  13. * 2026-10-30 用户要求:所有新建入口统一成一个按钮,且新建的对话**默认落在日常对话**。
  14. */
  15. @Component({
  16. selector: 'app-new-session-modal',
  17. changeDetection: ChangeDetectionStrategy.OnPush,
  18. imports: [UiModal, UiButton, UiAvatar],
  19. template: `
  20. <ui-modal [open]="service.isOpen()" title="新建对话" size="sm" (openChange)="$event || close()">
  21. <p class="note">
  22. 选择要对话的容器。新建的对话会存放在
  23. 「{{ targetName() }}」里。
  24. </p>
  25. <div class="pick-list" role="listbox" aria-label="选择容器">
  26. @for (a of agents(); track a.id) {
  27. <button
  28. type="button"
  29. class="pick-row"
  30. role="option"
  31. [attr.aria-selected]="false"
  32. (click)="createIn(a.id)"
  33. >
  34. <ui-avatar [name]="a.name" [size]="'sm'" />
  35. <span class="pick-text">
  36. <span class="pick-name ellipsis">{{ a.name }}</span>
  37. <span class="pick-sub ellipsis">{{ a.specialty || '数字生命' }}</span>
  38. </span>
  39. <i class="fa-solid fa-chevron-right pick-arrow" aria-hidden="true"></i>
  40. </button>
  41. } @empty {
  42. <p class="pick-empty">没有可用的容器(需先在平台完成接入)。</p>
  43. }
  44. </div>
  45. <div footer class="acts">
  46. <ui-button variant="ghost" (click)="close()">取消</ui-button>
  47. </div>
  48. </ui-modal>
  49. `,
  50. styles: [
  51. `
  52. :host {
  53. display: contents;
  54. }
  55. .note {
  56. margin: 0 0 var(--sp-3);
  57. font-size: var(--fs-13);
  58. color: var(--muted);
  59. line-height: 1.7;
  60. }
  61. .pick-list {
  62. display: grid;
  63. gap: var(--sp-1);
  64. max-height: 360px;
  65. overflow-y: auto;
  66. }
  67. .pick-row {
  68. display: flex;
  69. align-items: center;
  70. gap: var(--sp-3);
  71. min-height: var(--touch-min);
  72. padding: var(--sp-2) var(--sp-3);
  73. border: 1px solid var(--edge);
  74. border-radius: var(--r-md);
  75. background: var(--surface);
  76. text-align: left;
  77. cursor: pointer;
  78. transition: background-color var(--motion-fast) var(--ease-standard);
  79. }
  80. .pick-row:hover {
  81. background: var(--surface-hover);
  82. border-color: var(--edge-strong);
  83. }
  84. .pick-text {
  85. display: grid;
  86. flex: 1;
  87. min-width: 0;
  88. line-height: 1.3;
  89. }
  90. .pick-name {
  91. font-size: var(--fs-13);
  92. font-weight: 600;
  93. color: var(--ink);
  94. }
  95. .pick-sub {
  96. font-size: var(--fs-11);
  97. color: var(--muted);
  98. }
  99. .pick-arrow {
  100. flex-shrink: 0;
  101. font-size: var(--fs-11);
  102. color: var(--faint);
  103. }
  104. .pick-empty {
  105. margin: 0;
  106. padding: var(--sp-4);
  107. font-size: var(--fs-13);
  108. color: var(--muted);
  109. text-align: center;
  110. }
  111. .acts {
  112. display: flex;
  113. justify-content: flex-end;
  114. }
  115. `,
  116. ],
  117. })
  118. export class NewSessionModalComponent {
  119. protected readonly service = inject(NewSessionModalService);
  120. private readonly live = inject(LiveService);
  121. private readonly router = inject(Router);
  122. private readonly device = inject(DeviceService);
  123. private readonly workspaces = inject(WorkspaceService);
  124. /** 可选容器:已接入 Matrix 的数字生命(与旧实现同源) */
  125. /** 目标工作区名(模态框文案) */
  126. protected readonly targetName = computed(() => {
  127. const id = this.service.workspaceId();
  128. if (!id) return '日常对话';
  129. return this.workspaces.byId(id)?.name ?? '日常对话';
  130. });
  131. protected readonly agents = computed(() =>
  132. this.live
  133. .agents()
  134. .filter((a) => a.matrixReady)
  135. .sort((x, y) => x.name.localeCompare(y.name, 'zh') || x.id.localeCompare(y.id)),
  136. );
  137. protected createIn(agentId: string): void {
  138. const target = this.service.workspaceId();
  139. this.close();
  140. /*
  141. * 🔴 **不在这里创建会话**(2026-11-01 用户裁决:首次发消息才建会话)。
  142. *
  143. * 旧实现会 `sessions.create()` 一条空会话再跳转 —— 用户选完容器却没发消息,
  144. * 就留下一条 0 条消息的「新对话」,反复几次列表就堆满空壳。
  145. * 现在只是跳到「该容器、待建」的地址,输入第一条消息时才真正建会话。
  146. *
  147. * 工作区归属随查询参数带过去,由聊天页在会话建出来之后落地。
  148. */
  149. const qs = target ? `?ws=${encodeURIComponent(target)}` : '';
  150. void this.router.navigateByUrl(`/${this.device.mode()}/chat/${agentId}/new${qs}`);
  151. }
  152. protected close(): void {
  153. this.service.close();
  154. }
  155. }