agent.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { UserNoticePipe } from '../../../components/feedback/user-notice.pipe';
  2. import { NgTemplateOutlet } from '@angular/common';
  3. import { ChangeDetectionStrategy, Component, computed, inject, input, OnInit, signal } from '@angular/core';
  4. import { ActivatedRoute, Router } from '@angular/router';
  5. import { UiButton, UiCard, UiEmpty, UiSkeleton } from '../../../ui';
  6. import { PageComponent } from '../../../components/shell/page/page';
  7. import { Agent } from '../../../core/api/models';
  8. import { AlertService } from '../../../core/alert.service';
  9. import { blockedHint, livenessOf } from '../../../core/agent-liveness';
  10. import { AgentRenameService } from '../../../core/agent-rename.service';
  11. import { ProvisionService } from '../../../core/provision.service';
  12. import { ProvisionInstructionComponent } from '../../../components/domain/provision-instruction/provision-instruction';
  13. import { LiveService } from '../../../core/live.service';
  14. import { PromptModalService } from '../../../components/domain/prompt-modal/prompt-modal.service';
  15. import { EditionService } from '../../../core/edition.service';
  16. /**
  17. * 容器设置(原「数字生命详情」,2026-11-04 精简)
  18. *
  19. * ## 为什么只留重命名
  20. *
  21. * 用户裁决:这个页面的定位收敛为**容器设置**,先只提供重命名。
  22. * 原先的身份卡、运行指标、权限/委托配置、进入会话按钮全部移除 ——
  23. * 其中权限/委托依赖容器侧能力,目前只是本地对象层面的改动、不影响实际容器,
  24. * 留着会让管理员误以为自己改了真实配置。
  25. *
  26. * ## 入口
  27. *
  28. * 从**通讯录**点容器进入。聊天页顶栏的齿轮入口已隐藏。
  29. *
  30. * ## 重命名
  31. *
  32. * 写回**服务端**(容器名跨用户可见,不能存本地)。
  33. * 权限:仅公司管理员 / 系统超管。服务端 `op:'rename'` 就绪前会如实提示失败。
  34. */
  35. @Component({
  36. selector: 'app-dk-agent-page',
  37. changeDetection: ChangeDetectionStrategy.OnPush,
  38. imports: [UserNoticePipe, NgTemplateOutlet, PageComponent, UiButton, UiCard, UiEmpty, UiSkeleton, ProvisionInstructionComponent],
  39. template: `
  40. @if (embedded()) {
  41. <!-- 嵌入模式(dialog 内):不渲染页面外壳 -->
  42. <ng-container *ngTemplateOutlet="body" />
  43. } @else {
  44. <app-page
  45. title="容器设置"
  46. [subtitle]="isPersonal() ? '重命名这个容器,只改变显示名称。' : '重命名这个容器,同公司的同事都会看到新名称。'"
  47. backText="返回"
  48. (back)="close()"
  49. >
  50. <ng-container *ngTemplateOutlet="body" />
  51. </app-page>
  52. }
  53. <ng-template #body>
  54. @switch (live.state()) {
  55. @case ('loading') {
  56. <ui-card><ui-skeleton variant="rect" height="120px" /></ui-card>
  57. }
  58. @case ('error') {
  59. <ui-card>
  60. <ui-empty
  61. title="加载失败"
  62. [description]="(live.error() ?? '请稍后重试') | userNotice"
  63. />
  64. <div class="retry-hold">
  65. <ui-button variant="outline" icon="fa-solid fa-rotate-right" (click)="reload()">
  66. 重试
  67. </ui-button>
  68. </div>
  69. </ui-card>
  70. }
  71. @default {
  72. @if (agent(); as a) {
  73. <ui-card>
  74. <div class="rename-block">
  75. <div class="name-row">
  76. <span class="name-label">当前名称</span>
  77. <strong class="name-value ellipsis" [title]="a.name">{{ a.name }}</strong>
  78. <span class="name-label">{{ edition.containerKind(a) }}</span>
  79. </div>
  80. <!-- 操作行:重命名(主)+ 进入会话(次),横向排列 -->
  81. <div class="action-row">
  82. @if (canRename()) {
  83. <ui-button
  84. variant="primary"
  85. icon="fa-solid fa-pen"
  86. [disabled]="renameSvc.busy() === a.id"
  87. (click)="rename(a)"
  88. >
  89. {{ renameSvc.busy() === a.id ? '保存中…' : '重命名' }}
  90. </ui-button>
  91. }
  92. <!-- 进入会话(2026-11-05 对齐移动端):新建一条对话,默认落「日常对话」 -->
  93. <ui-button variant="outline" icon="fa-solid fa-comment" (click)="enterChat(a)">
  94. 进入会话
  95. </ui-button>
  96. </div>
  97. <!-- 本地容器:重新拿接入提示词(2026-12-03) -->
  98. @if (a.planCode === 'trial' || a.planCode === 'starter') {
  99. <div class="action-row instruction-row">
  100. <ui-button variant="outline" [icon]="reissuing() ? 'fa-solid fa-circle-notch fa-spin' : 'fa-solid fa-key'" [disabled]="reissuing()" (click)="showInstruction(a)">
  101. {{ reissuing() ? '正在生成…' : (instruction() ? '收起接入提示词' : '查看接入提示词') }}
  102. </ui-button>
  103. @if (a.trialEndsAt) {
  104. <span class="trial-end">试用有效期至 {{ trialEndText(a.trialEndsAt) }}</span>
  105. }
  106. </div>
  107. @if (instruction(); as ins) {
  108. <div class="instruction-box"><app-provision-instruction [text]="ins" /></div>
  109. }
  110. }
  111. @if (!canRename()) {
  112. <p class="hint">{{ isPersonal() ? '你没有权限重命名这个容器。' : '只有企业管理员可以重命名容器。' }}</p>
  113. }
  114. @if (error()) {
  115. <p class="err" role="alert">{{ (error()) | userNotice:'暂时无法完成,请稍后再试。' }}</p>
  116. }
  117. </div>
  118. </ui-card>
  119. } @else {
  120. <ui-card><ui-empty title="容器不存在" /></ui-card>
  121. }
  122. }
  123. }
  124. </ng-template>
  125. `,
  126. styles: [
  127. `
  128. :host {
  129. display: block;
  130. }
  131. .rename-block {
  132. display: flex;
  133. flex-direction: column;
  134. align-items: flex-start;
  135. gap: var(--sp-4);
  136. max-width: 520px;
  137. }
  138. .name-row {
  139. display: flex;
  140. align-items: baseline;
  141. gap: var(--sp-3);
  142. width: 100%;
  143. }
  144. .name-label {
  145. flex: none;
  146. color: var(--muted);
  147. font-size: var(--fs-13);
  148. }
  149. .name-value {
  150. min-width: 0;
  151. color: var(--ink);
  152. font-size: var(--fs-20);
  153. font-weight: 600;
  154. }
  155. .ellipsis {
  156. overflow: hidden;
  157. text-overflow: ellipsis;
  158. white-space: nowrap;
  159. }
  160. .instruction-row {
  161. margin-top: var(--sp-3);
  162. }
  163. .trial-end {
  164. color: var(--muted);
  165. font-size: var(--fs-12);
  166. }
  167. .instruction-box {
  168. margin-top: var(--sp-3);
  169. }
  170. .action-row {
  171. display: flex;
  172. align-items: center;
  173. gap: var(--sp-3);
  174. flex-wrap: wrap;
  175. }
  176. .hint {
  177. margin: 0;
  178. color: var(--muted);
  179. font-size: var(--fs-13);
  180. }
  181. .err {
  182. margin: 0;
  183. padding: var(--sp-2) var(--sp-3);
  184. border-radius: var(--r-sm);
  185. background: var(--danger-subtle);
  186. color: var(--danger);
  187. font-size: var(--fs-13);
  188. }
  189. .retry-hold {
  190. display: flex;
  191. justify-content: center;
  192. padding: var(--sp-4) 0;
  193. }
  194. `,
  195. ],
  196. })
  197. export class DkAgentPage implements OnInit {
  198. private readonly route = inject(ActivatedRoute);
  199. private readonly router = inject(Router);
  200. private readonly alert = inject(AlertService);
  201. private readonly prompt = inject(PromptModalService);
  202. protected readonly live = inject(LiveService);
  203. protected readonly renameSvc = inject(AgentRenameService);
  204. private readonly provisionSvc = inject(ProvisionService);
  205. /** 接入提示词(2026-12-03):本地容器的重新生成入口 */
  206. protected readonly instruction = signal<string | null>(null);
  207. protected readonly reissuing = signal(false);
  208. protected async showInstruction(a: Agent): Promise<void> {
  209. if (this.instruction()) { this.instruction.set(null); return; }
  210. this.reissuing.set(true);
  211. try { this.instruction.set(await this.provisionSvc.reissueInstruction(a)); }
  212. finally { this.reissuing.set(false); }
  213. }
  214. protected trialEndText(iso: string): string {
  215. const d = new Date(iso);
  216. return Number.isFinite(d.getTime()) ? `${d.getMonth() + 1} 月 ${d.getDate()} 日 ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}` : '';
  217. }
  218. /** 嵌入模式(agent-modal 复用本页时传 true) */
  219. readonly embedded = input(false);
  220. /** 嵌入模式下由宿主传入容器 id;独立路由时从路由参数取 */
  221. readonly agentId = input<string>('');
  222. protected readonly error = signal<string | null>(null);
  223. protected readonly resolvedId = computed(() => this.agentId() || (this.route.snapshot.paramMap.get('id') ?? ''));
  224. protected readonly agent = computed<Agent | undefined>(() => this.live.agentById(this.resolvedId()));
  225. protected readonly edition = inject(EditionService);
  226. protected readonly isPersonal = this.edition.isPersonal;
  227. protected readonly canRename = computed(() => this.renameSvc.canRename(this.agent()));
  228. async ngOnInit(): Promise<void> {
  229. await this.live.ensureLoaded();
  230. }
  231. /** 打开重命名弹层(复用全局 PromptModal) */
  232. protected rename(agent: Agent): void {
  233. this.error.set(null);
  234. this.prompt.open({
  235. title: '重命名容器',
  236. value: agent.name,
  237. placeholder: '新的容器名称',
  238. allowEmpty: false,
  239. confirmText: '保存',
  240. validate: (v) => {
  241. const n = v.trim();
  242. if (!n) return '名称不能为空';
  243. if (n.length > 24) return '名称不能超过 24 个字符';
  244. if (n === agent.name) return '名称没有变化';
  245. return null;
  246. },
  247. onConfirm: (v) => {
  248. void this.renameSvc
  249. .rename(agent.id, v.trim())
  250. .catch((e: unknown) => {
  251. this.error.set(e instanceof Error ? e.message : '重命名失败');
  252. });
  253. },
  254. });
  255. }
  256. /**
  257. * 进入会话(2026-11-05 对齐移动端)。
  258. *
  259. * 在该容器下**新建一条对话**,默认落在「日常对话」
  260. * (`/chat/:id/new` 不带 ws 参数),与「新建对话」弹层选中该容器等价。
  261. *
  262. * 🔴 2026-11-11:未接入 / 离线的容器**拦下**并提示先接入。
  263. * 否则用户能进到一个永远发不出消息的会话里,只会更困惑。
  264. */
  265. protected enterChat(agent: Agent): void {
  266. const hint = blockedHint(livenessOf(agent));
  267. if (hint) {
  268. this.alert.show('暂时无法进入会话', hint, 'warning');
  269. return;
  270. }
  271. void this.router.navigateByUrl(`/desktop/chat/${agent.id}/new`);
  272. }
  273. protected reload(): void {
  274. void this.live.refresh();
  275. }
  276. /** 嵌入模式的关闭(宿主 dialog 自己处理关闭按钮,这里保留以备扩展) */
  277. /**
  278. * 返回上一级(2026-11-11 用户要求加返回按钮)。
  279. *
  280. * 原实现固定跳通讯录,但用户可能是从**左栏会话树**或**工作台**点进来的,
  281. * 固定跳转会把他的浏览位置弄丢。优先 `history.back()`,
  282. * 没有历史(直接输 URL 打开)才回落到通讯录。
  283. */
  284. protected close(): void {
  285. if (history.length > 1) history.back();
  286. else void this.router.navigateByUrl('/desktop/contacts');
  287. }
  288. }