| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
- import { Router } from '@angular/router';
- import { UiAvatar, UiButton, UiModal } from '../../../ui';
- import { DeviceService } from '../../../core/device.service';
- import { LiveService } from '../../../core/live.service';
- import { NewSessionModalService } from './new-session-modal.service';
- import { WorkspaceService } from '../../../core/workspace.service';
- /**
- * 「新建对话」模态框(全局单例,挂在外壳上)
- *
- * 流程:选容器 → 在该容器下新建一条**空对话** → 落进「日常对话」→ 跳过去。
- *
- * 2026-10-30 用户要求:所有新建入口统一成一个按钮,且新建的对话**默认落在日常对话**。
- */
- @Component({
- selector: 'app-new-session-modal',
- changeDetection: ChangeDetectionStrategy.OnPush,
- imports: [UiModal, UiButton, UiAvatar],
- template: `
- <ui-modal [open]="service.isOpen()" title="新建对话" size="sm" (openChange)="$event || close()">
- <p class="note">
- 选择要对话的容器。新建的对话会存放在
- 「{{ targetName() }}」里。
- </p>
- <div class="pick-list" role="listbox" aria-label="选择容器">
- @for (a of agents(); track a.id) {
- <button
- type="button"
- class="pick-row"
- role="option"
- [attr.aria-selected]="false"
- (click)="createIn(a.id)"
- >
- <ui-avatar [name]="a.name" [size]="'sm'" />
- <span class="pick-text">
- <span class="pick-name ellipsis">{{ a.name }}</span>
- <span class="pick-sub ellipsis">{{ a.specialty || '数字生命' }}</span>
- </span>
- <i class="fa-solid fa-chevron-right pick-arrow" aria-hidden="true"></i>
- </button>
- } @empty {
- <p class="pick-empty">没有可用的容器(需先在平台完成接入)。</p>
- }
- </div>
- <div footer class="acts">
- <ui-button variant="ghost" (click)="close()">取消</ui-button>
- </div>
- </ui-modal>
- `,
- styles: [
- `
- :host {
- display: contents;
- }
- .note {
- margin: 0 0 var(--sp-3);
- font-size: var(--fs-13);
- color: var(--muted);
- line-height: 1.7;
- }
- .pick-list {
- display: grid;
- gap: var(--sp-1);
- max-height: 360px;
- overflow-y: auto;
- }
- .pick-row {
- display: flex;
- align-items: center;
- gap: var(--sp-3);
- min-height: var(--touch-min);
- padding: var(--sp-2) var(--sp-3);
- border: 1px solid var(--edge);
- border-radius: var(--r-md);
- background: var(--surface);
- text-align: left;
- cursor: pointer;
- transition: background-color var(--motion-fast) var(--ease-standard);
- }
- .pick-row:hover {
- background: var(--surface-hover);
- border-color: var(--edge-strong);
- }
- .pick-text {
- display: grid;
- flex: 1;
- min-width: 0;
- line-height: 1.3;
- }
- .pick-name {
- font-size: var(--fs-13);
- font-weight: 600;
- color: var(--ink);
- }
- .pick-sub {
- font-size: var(--fs-11);
- color: var(--muted);
- }
- .pick-arrow {
- flex-shrink: 0;
- font-size: var(--fs-11);
- color: var(--faint);
- }
- .pick-empty {
- margin: 0;
- padding: var(--sp-4);
- font-size: var(--fs-13);
- color: var(--muted);
- text-align: center;
- }
- .acts {
- display: flex;
- justify-content: flex-end;
- }
- `,
- ],
- })
- export class NewSessionModalComponent {
- protected readonly service = inject(NewSessionModalService);
- private readonly live = inject(LiveService);
- private readonly router = inject(Router);
- private readonly device = inject(DeviceService);
- private readonly workspaces = inject(WorkspaceService);
- /** 可选容器:已接入 Matrix 的数字生命(与旧实现同源) */
- /** 目标工作区名(模态框文案) */
- protected readonly targetName = computed(() => {
- const id = this.service.workspaceId();
- if (!id) return '日常对话';
- return this.workspaces.byId(id)?.name ?? '日常对话';
- });
- protected readonly agents = computed(() =>
- this.live
- .agents()
- .filter((a) => a.matrixReady)
- .sort((x, y) => x.name.localeCompare(y.name, 'zh') || x.id.localeCompare(y.id)),
- );
- protected createIn(agentId: string): void {
- const target = this.service.workspaceId();
- this.close();
- /*
- * 🔴 **不在这里创建会话**(2026-11-01 用户裁决:首次发消息才建会话)。
- *
- * 旧实现会 `sessions.create()` 一条空会话再跳转 —— 用户选完容器却没发消息,
- * 就留下一条 0 条消息的「新对话」,反复几次列表就堆满空壳。
- * 现在只是跳到「该容器、待建」的地址,输入第一条消息时才真正建会话。
- *
- * 工作区归属随查询参数带过去,由聊天页在会话建出来之后落地。
- */
- const qs = target ? `?ws=${encodeURIComponent(target)}` : '';
- void this.router.navigateByUrl(`/${this.device.mode()}/chat/${agentId}/new${qs}`);
- }
- protected close(): void {
- this.service.close();
- }
- }
|