| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import { UserNoticePipe } from '../../../components/feedback/user-notice.pipe';
- import { NgTemplateOutlet } from '@angular/common';
- import { ChangeDetectionStrategy, Component, computed, inject, input, OnInit, signal } from '@angular/core';
- import { ActivatedRoute, Router } from '@angular/router';
- import { UiButton, UiCard, UiEmpty, UiSkeleton } from '../../../ui';
- import { PageComponent } from '../../../components/shell/page/page';
- import { Agent } from '../../../core/api/models';
- import { AlertService } from '../../../core/alert.service';
- import { blockedHint, livenessOf } from '../../../core/agent-liveness';
- import { AgentRenameService } from '../../../core/agent-rename.service';
- import { ProvisionService } from '../../../core/provision.service';
- import { ProvisionInstructionComponent } from '../../../components/domain/provision-instruction/provision-instruction';
- import { LiveService } from '../../../core/live.service';
- import { PromptModalService } from '../../../components/domain/prompt-modal/prompt-modal.service';
- import { EditionService } from '../../../core/edition.service';
- /**
- * 容器设置(原「数字生命详情」,2026-11-04 精简)
- *
- * ## 为什么只留重命名
- *
- * 用户裁决:这个页面的定位收敛为**容器设置**,先只提供重命名。
- * 原先的身份卡、运行指标、权限/委托配置、进入会话按钮全部移除 ——
- * 其中权限/委托依赖容器侧能力,目前只是本地对象层面的改动、不影响实际容器,
- * 留着会让管理员误以为自己改了真实配置。
- *
- * ## 入口
- *
- * 从**通讯录**点容器进入。聊天页顶栏的齿轮入口已隐藏。
- *
- * ## 重命名
- *
- * 写回**服务端**(容器名跨用户可见,不能存本地)。
- * 权限:仅公司管理员 / 系统超管。服务端 `op:'rename'` 就绪前会如实提示失败。
- */
- @Component({
- selector: 'app-dk-agent-page',
- changeDetection: ChangeDetectionStrategy.OnPush,
- imports: [UserNoticePipe, NgTemplateOutlet, PageComponent, UiButton, UiCard, UiEmpty, UiSkeleton, ProvisionInstructionComponent],
- template: `
- @if (embedded()) {
- <!-- 嵌入模式(dialog 内):不渲染页面外壳 -->
- <ng-container *ngTemplateOutlet="body" />
- } @else {
- <app-page
- title="容器设置"
- [subtitle]="isPersonal() ? '重命名这个容器,只改变显示名称。' : '重命名这个容器,同公司的同事都会看到新名称。'"
- backText="返回"
- (back)="close()"
- >
- <ng-container *ngTemplateOutlet="body" />
- </app-page>
- }
- <ng-template #body>
- @switch (live.state()) {
- @case ('loading') {
- <ui-card><ui-skeleton variant="rect" height="120px" /></ui-card>
- }
- @case ('error') {
- <ui-card>
- <ui-empty
- title="加载失败"
- [description]="(live.error() ?? '请稍后重试') | userNotice"
- />
- <div class="retry-hold">
- <ui-button variant="outline" icon="fa-solid fa-rotate-right" (click)="reload()">
- 重试
- </ui-button>
- </div>
- </ui-card>
- }
- @default {
- @if (agent(); as a) {
- <ui-card>
- <div class="rename-block">
- <div class="name-row">
- <span class="name-label">当前名称</span>
- <strong class="name-value ellipsis" [title]="a.name">{{ a.name }}</strong>
- <span class="name-label">{{ edition.containerKind(a) }}</span>
- </div>
- <!-- 操作行:重命名(主)+ 进入会话(次),横向排列 -->
- <div class="action-row">
- @if (canRename()) {
- <ui-button
- variant="primary"
- icon="fa-solid fa-pen"
- [disabled]="renameSvc.busy() === a.id"
- (click)="rename(a)"
- >
- {{ renameSvc.busy() === a.id ? '保存中…' : '重命名' }}
- </ui-button>
- }
- <!-- 进入会话(2026-11-05 对齐移动端):新建一条对话,默认落「日常对话」 -->
- <ui-button variant="outline" icon="fa-solid fa-comment" (click)="enterChat(a)">
- 进入会话
- </ui-button>
- </div>
- <!-- 本地容器:重新拿接入提示词(2026-12-03) -->
- @if (a.planCode === 'trial' || a.planCode === 'starter') {
- <div class="action-row instruction-row">
- <ui-button variant="outline" [icon]="reissuing() ? 'fa-solid fa-circle-notch fa-spin' : 'fa-solid fa-key'" [disabled]="reissuing()" (click)="showInstruction(a)">
- {{ reissuing() ? '正在生成…' : (instruction() ? '收起接入提示词' : '查看接入提示词') }}
- </ui-button>
- @if (a.trialEndsAt) {
- <span class="trial-end">试用有效期至 {{ trialEndText(a.trialEndsAt) }}</span>
- }
- </div>
- @if (instruction(); as ins) {
- <div class="instruction-box"><app-provision-instruction [text]="ins" /></div>
- }
- }
- @if (!canRename()) {
- <p class="hint">{{ isPersonal() ? '你没有权限重命名这个容器。' : '只有企业管理员可以重命名容器。' }}</p>
- }
- @if (error()) {
- <p class="err" role="alert">{{ (error()) | userNotice:'暂时无法完成,请稍后再试。' }}</p>
- }
- </div>
- </ui-card>
- } @else {
- <ui-card><ui-empty title="容器不存在" /></ui-card>
- }
- }
- }
- </ng-template>
- `,
- styles: [
- `
- :host {
- display: block;
- }
- .rename-block {
- display: flex;
- flex-direction: column;
- align-items: flex-start;
- gap: var(--sp-4);
- max-width: 520px;
- }
- .name-row {
- display: flex;
- align-items: baseline;
- gap: var(--sp-3);
- width: 100%;
- }
- .name-label {
- flex: none;
- color: var(--muted);
- font-size: var(--fs-13);
- }
- .name-value {
- min-width: 0;
- color: var(--ink);
- font-size: var(--fs-20);
- font-weight: 600;
- }
- .ellipsis {
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .instruction-row {
- margin-top: var(--sp-3);
- }
- .trial-end {
- color: var(--muted);
- font-size: var(--fs-12);
- }
- .instruction-box {
- margin-top: var(--sp-3);
- }
- .action-row {
- display: flex;
- align-items: center;
- gap: var(--sp-3);
- flex-wrap: wrap;
- }
- .hint {
- margin: 0;
- color: var(--muted);
- font-size: var(--fs-13);
- }
- .err {
- margin: 0;
- padding: var(--sp-2) var(--sp-3);
- border-radius: var(--r-sm);
- background: var(--danger-subtle);
- color: var(--danger);
- font-size: var(--fs-13);
- }
- .retry-hold {
- display: flex;
- justify-content: center;
- padding: var(--sp-4) 0;
- }
- `,
- ],
- })
- export class DkAgentPage implements OnInit {
- private readonly route = inject(ActivatedRoute);
- private readonly router = inject(Router);
- private readonly alert = inject(AlertService);
- private readonly prompt = inject(PromptModalService);
- protected readonly live = inject(LiveService);
- protected readonly renameSvc = inject(AgentRenameService);
- private readonly provisionSvc = inject(ProvisionService);
- /** 接入提示词(2026-12-03):本地容器的重新生成入口 */
- protected readonly instruction = signal<string | null>(null);
- protected readonly reissuing = signal(false);
- protected async showInstruction(a: Agent): Promise<void> {
- if (this.instruction()) { this.instruction.set(null); return; }
- this.reissuing.set(true);
- try { this.instruction.set(await this.provisionSvc.reissueInstruction(a)); }
- finally { this.reissuing.set(false); }
- }
- protected trialEndText(iso: string): string {
- const d = new Date(iso);
- return Number.isFinite(d.getTime()) ? `${d.getMonth() + 1} 月 ${d.getDate()} 日 ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}` : '';
- }
- /** 嵌入模式(agent-modal 复用本页时传 true) */
- readonly embedded = input(false);
- /** 嵌入模式下由宿主传入容器 id;独立路由时从路由参数取 */
- readonly agentId = input<string>('');
- protected readonly error = signal<string | null>(null);
- protected readonly resolvedId = computed(() => this.agentId() || (this.route.snapshot.paramMap.get('id') ?? ''));
- protected readonly agent = computed<Agent | undefined>(() => this.live.agentById(this.resolvedId()));
- protected readonly edition = inject(EditionService);
- protected readonly isPersonal = this.edition.isPersonal;
- protected readonly canRename = computed(() => this.renameSvc.canRename(this.agent()));
- async ngOnInit(): Promise<void> {
- await this.live.ensureLoaded();
- }
- /** 打开重命名弹层(复用全局 PromptModal) */
- protected rename(agent: Agent): void {
- this.error.set(null);
- this.prompt.open({
- title: '重命名容器',
- value: agent.name,
- placeholder: '新的容器名称',
- allowEmpty: false,
- confirmText: '保存',
- validate: (v) => {
- const n = v.trim();
- if (!n) return '名称不能为空';
- if (n.length > 24) return '名称不能超过 24 个字符';
- if (n === agent.name) return '名称没有变化';
- return null;
- },
- onConfirm: (v) => {
- void this.renameSvc
- .rename(agent.id, v.trim())
- .catch((e: unknown) => {
- this.error.set(e instanceof Error ? e.message : '重命名失败');
- });
- },
- });
- }
- /**
- * 进入会话(2026-11-05 对齐移动端)。
- *
- * 在该容器下**新建一条对话**,默认落在「日常对话」
- * (`/chat/:id/new` 不带 ws 参数),与「新建对话」弹层选中该容器等价。
- *
- * 🔴 2026-11-11:未接入 / 离线的容器**拦下**并提示先接入。
- * 否则用户能进到一个永远发不出消息的会话里,只会更困惑。
- */
- protected enterChat(agent: Agent): void {
- const hint = blockedHint(livenessOf(agent));
- if (hint) {
- this.alert.show('暂时无法进入会话', hint, 'warning');
- return;
- }
- void this.router.navigateByUrl(`/desktop/chat/${agent.id}/new`);
- }
- protected reload(): void {
- void this.live.refresh();
- }
- /** 嵌入模式的关闭(宿主 dialog 自己处理关闭按钮,这里保留以备扩展) */
- /**
- * 返回上一级(2026-11-11 用户要求加返回按钮)。
- *
- * 原实现固定跳通讯录,但用户可能是从**左栏会话树**或**工作台**点进来的,
- * 固定跳转会把他的浏览位置弄丢。优先 `history.back()`,
- * 没有历史(直接输 URL 打开)才回落到通讯录。
- */
- protected close(): void {
- if (history.length > 1) history.back();
- else void this.router.navigateByUrl('/desktop/contacts');
- }
- }
|