chat.ts 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894
  1. import {
  2. ChangeDetectionStrategy,
  3. Component,
  4. computed,
  5. effect,
  6. ElementRef,
  7. inject,
  8. signal,
  9. viewChild,
  10. } from '@angular/core';
  11. import { ActivatedRoute, Router } from '@angular/router';
  12. import {
  13. UiAlert,
  14. UiAvatar,
  15. UiButton,
  16. UiEmpty,
  17. UiInput,
  18. UiModal,
  19. UiSkeleton,
  20. UiTooltip,
  21. } from '../../../ui';
  22. import { ComposerComponent } from '../../../components/domain/composer/composer';
  23. import { NewSessionRequest } from '../../../components/domain/session-tree/session-tree';
  24. import { MessageBubbleComponent } from '../../../components/domain/message-bubble/message-bubble';
  25. import { TypingIndicatorComponent } from '../../../components/domain/typing-indicator/typing-indicator';
  26. import { ToolActivityComponent } from '../../../components/domain/tool-activity/tool-activity';
  27. import { FilePreviewComponent } from '../../../components/preview/file-preview/file-preview';
  28. import { NewSessionModalService } from '../../../components/domain/new-session-modal/new-session-modal.service';
  29. import { ImageLightboxComponent } from '../../../components/preview/image-lightbox/image-lightbox';
  30. import { previewKindOf, type PreviewKind } from '../../../components/preview/file-kind';
  31. import { PresenceBadgeComponent } from '../../../components/domain/presence-badge/presence-badge';
  32. import { AgentPresence, agentPresence } from '../../../core/api/models';
  33. import { StatusBadgeComponent } from '../../../components/domain/status-badge/status-badge';
  34. import { ChatService } from '../../../core/chat.service';
  35. import { Deliverable, TASK_STATUS_TEXT } from '../../../core/api/models';
  36. import { DeliverablesService } from '../../../core/deliverables.service';
  37. import { DeviceService } from '../../../core/device.service';
  38. import { fmtPercent, fmtTokens, usageOf } from '../../../core/format';
  39. import { TaskLine, TaskPulseService } from '../../../core/task-pulse.service';
  40. import { ArtifactSyncService } from '../../../core/artifact-sync.service';
  41. import { LoopGuardService } from '../../../core/loop-guard.service';
  42. import { isToolActivityMessage, toolActivitySummary } from '../../../core/message-segments';
  43. import { Block } from '../../../core/message-segments';
  44. import { Message } from '../../../core/api/models';
  45. import { fmtTime } from '../../../core/time';
  46. import { LiveService } from '../../../core/live.service';
  47. import { UnreadService } from '../../../core/unread.service';
  48. import { TasksService } from '../../../core/tasks.service';
  49. import { ChatSession } from '../../../core/chat-session';
  50. import { ChatSessionService } from '../../../core/chat-session.service';
  51. import { MatrixSessionService } from '../../../core/matrix-session.service';
  52. import { WorkspaceService } from '../../../core/workspace.service';
  53. import { AgentModalService } from '../../../components/domain/agent-modal/agent-modal.service';
  54. /**
  55. * 桌面·会话(SPEC FR-04)—— 四栏(导航 + 会话列表 + 聊天区 + 成果栏)
  56. *
  57. * 实现纪律(2026-10-15 返工):
  58. * · 列表/空态/加载/提示条全部走底座组件(`ui-list` / `ui-empty` / `ui-skeleton` / `ui-alert`)
  59. * · **流动布局**:栏宽用比例 + 最小宽兜底,窗口变宽时主区跟着变宽;
  60. * 仅当可用宽不足以容纳右栏时才降级(换形态,不删功能)
  61. * · 无 demo 件
  62. *
  63. * AC:04.1 四栏 / 04.2 发送 / 04.3 上传进度 / 04.5 自增高 / 04.6 状态不重复
  64. * / 04.8 失败可重试 / 04.9 连接状态 / 04.10 超长截断 / 04.11 三态
  65. */
  66. /** 一条消息的渲染形态:普通气泡 or 终端活动折叠组 */
  67. type MessageRow =
  68. | { kind: 'msg'; key: string; message: Message }
  69. | { kind: 'tool'; key: string; block: Extract<Block, { kind: 'process' }> };
  70. /** 右栏标签 */
  71. export type RailTab = 'tasks' | 'files' | 'results' | 'status';
  72. /** 读上次选中的右栏标签(按用户隔离的 localStorage) */
  73. function readRailTabPref(): RailTab {
  74. try {
  75. const v = localStorage.getItem('fmode_rail_tab');
  76. if (v === 'tasks' || v === 'files' || v === 'results' || v === 'status') return v;
  77. } catch {
  78. /* 隐私模式 */
  79. }
  80. // 默认「文件」:用户最关心本次对话产出了什么
  81. return 'files';
  82. }
  83. /**
  84. * 右栏宽度夹取 —— **纯函数**,便于单测。
  85. *
  86. * 下限 280:再窄右栏里「文件名 + 大小」会折行成两三行;
  87. * 上限 560:再宽就把对话区挤到不可读(主区最小可读宽约 480)。
  88. */
  89. export function clampRailWidth(w: number): number {
  90. if (!Number.isFinite(w)) return 320;
  91. return Math.min(560, Math.max(280, Math.round(w)));
  92. }
  93. /**
  94. * 预览形态下的右栏宽度上限(2026-10-30)。
  95. * 看文档/表格时 560px 太窄;预览打开时放宽到 720,关闭后回到常规上限。
  96. */
  97. export const RAIL_W_PREVIEW_MAX = 720;
  98. export function clampPreviewRailWidth(w: number): number {
  99. return Math.min(RAIL_W_PREVIEW_MAX, Math.max(280, Math.round(Number.isFinite(w) ? w : 320)));
  100. }
  101. /** 左栏「容器行」—— 会话按容器分组时的分组头 */
  102. interface ConversationRow {
  103. id: string;
  104. name: string;
  105. specialty?: string;
  106. currentTask?: string;
  107. unread?: number;
  108. }
  109. @Component({
  110. selector: 'app-dk-chat-page',
  111. changeDetection: ChangeDetectionStrategy.OnPush,
  112. imports: [
  113. UiAlert,
  114. UiAvatar,
  115. UiButton,
  116. UiEmpty,
  117. UiInput,
  118. UiModal,
  119. UiSkeleton,
  120. UiTooltip,
  121. ComposerComponent,
  122. MessageBubbleComponent,
  123. TypingIndicatorComponent,
  124. ToolActivityComponent,
  125. FilePreviewComponent,
  126. ImageLightboxComponent,
  127. PresenceBadgeComponent,
  128. StatusBadgeComponent,
  129. ],
  130. template: `
  131. <div class="conversation-layout" [style.--rail-w.px]="effectiveRailWidth()">
  132. <!--
  133. 🔴 左栏(会话树)已提升到 DesktopShell 外壳组件(2026-10-30)——
  134. 一级路由点击时左栏必须保持不变,所以它不能挂在某个页面里。
  135. 页面这里只负责主体区(+ 右栏)。
  136. -->
  137. <!-- ===== 聊天区 ===== -->
  138. <section class="col main-col" aria-label="消息">
  139. @if (!chat.agentId()) {
  140. <div class="center-fill">
  141. <ui-empty title="选择一个会话" description="从左侧选择数字生命开始对话。" />
  142. </div>
  143. } @else {
  144. <header class="chat-head">
  145. @if (chat.agent(); as a) {
  146. <ui-avatar [name]="a.name" [size]="'md'" />
  147. <div class="chat-title">
  148. <strong>{{ a.name }}</strong>
  149. <!--
  150. 当前任务名(只读展示,2026-10-31 用户要求取消命名与改名):
  151. 原来这里是可点击重命名的「对话名」。现在对话由容器命名,
  152. 用户不需要也不应该改 —— 去掉点击改名入口,只保留只读文案。
  153. -->
  154. @if (chat.currentSession(); as s) {
  155. <small class="ellipsis session-name" [title]="s.title">
  156. <i class="fa-solid fa-comment-dots" aria-hidden="true"></i>
  157. {{ s.title }}
  158. @if (s.archived) {
  159. <span class="archived-tag">已归档</span>
  160. }
  161. </small>
  162. } @else {
  163. <small class="ellipsis">{{ a.specialty || '数字生命' }}</small>
  164. }
  165. </div>
  166. <!--
  167. AC-04.6 的修正(2026-10-21):原先在此放运行态徽标,但下方任务行
  168. 已经表达了「在不在干活」→ 同一信息出现两次,且 idle 用 warning 色
  169. 看起来像故障(用户实测:「空闲 空闲」两行、橙色像告警)。
  170. 这里只保留任务行**不表达**的内容:容器不可用 / 通道未上报。
  171. -->
  172. @if (a.status === 'error' || a.status === 'offline' || a.status === 'stopped') {
  173. <app-status-badge [agent]="a" />
  174. }
  175. }
  176. <!--
  177. 当前任务条(用户要求:先看到「它在做什么」,再往下看消息)。
  178. 文案唯一实现是 TaskPulseService.describe(),列表/头部/右栏同一份。
  179. 独立一行 → 任务名再长也不再挤压头像与标题。
  180. -->
  181. @if (chat.agent(); as a) {
  182. <!--
  183. 当前任务:**收进标题栏**作为一行紧凑状态(2026-10-29)。
  184. 原来它是一条横跨整列、带底色和左竖线的横条 —— 比标题还抢眼,
  185. 而且把消息流整体往下推。参考 ChatGPT:状态属于页头,不属于内容。
  186. -->
  187. <!--
  188. 任务状态固定占位(2026-10-30):
  189. 原来「无任务」时这个元素不存在、有任务时突然出现,
  190. 把标题栏撑高 → 下方消息区整体下移一截,观感就是"发送后界面跳一下"。
  191. 现在用固定宽高的槽位承载,文字在槽内截断。
  192. -->
  193. <span class="task-slot" [title]="taskLine(a.id).text">
  194. @if (taskLine(a.id).text) {
  195. <span
  196. class="task-line"
  197. [class]="'tone-' + taskLine(a.id).tone"
  198. [class.busy]="taskLine(a.id).active"
  199. >
  200. <span class="spin" aria-hidden="true"></span>
  201. <span class="task-text ellipsis">{{ taskLine(a.id).text }}</span>
  202. </span>
  203. }
  204. </span>
  205. }
  206. <div class="chat-actions">
  207. <!-- 新建对话:在当前容器下开一条新对话 -->
  208. @if (chat.agentId(); as aid) {
  209. <ui-tooltip content="新建对话">
  210. <ui-button
  211. variant="ghost"
  212. shape="circle"
  213. icon="fa-solid fa-square-plus"
  214. ariaLabel="新建对话"
  215. (click)="newSession(aid)"
  216. />
  217. </ui-tooltip>
  218. <!-- 容器设置:进入该容器的详情/设置页 -->
  219. <ui-tooltip content="容器设置">
  220. <ui-button
  221. variant="ghost"
  222. shape="circle"
  223. icon="fa-solid fa-gear"
  224. ariaLabel="容器设置"
  225. (click)="openAgentSettings(aid)"
  226. />
  227. </ui-tooltip>
  228. }
  229. <!--
  230. 侧栏开合。标签切换收在右栏内部 ——
  231. 原来中栏放了「任务 / 成果」两个圆钮,右栏只有两页,
  232. 加标签条后再放两个钮就是重复入口(用户会分不清点哪个)。
  233. -->
  234. <ui-tooltip content="侧栏">
  235. <ui-button
  236. variant="ghost"
  237. shape="circle"
  238. icon="fa-solid fa-table-columns"
  239. ariaLabel="侧栏"
  240. [class.on]="railOpen()"
  241. (click)="railOpen.set(!railOpen())"
  242. />
  243. </ui-tooltip>
  244. </div>
  245. </header>
  246. <!--
  247. 连接状态提示(AC-04.9)。
  248. 🔴 区分两种「连不上」:容器离线 vs 自己在连接中 ——
  249. 否则用户分不清是网络问题还是对方容器没跑(实测反馈)。
  250. -->
  251. @if (chat.connection() !== 'connected' || currentContainerState() === 'unreported') {
  252. <div class="banner-hold">
  253. @if (currentContainerState() === 'unreported' && chat.connection() === 'connected') {
  254. <ui-alert type="warning" title="容器状态未知,可能无法应答">
  255. 该容器还没有上报在线状态,发给它的消息可能无人处理。
  256. </ui-alert>
  257. } @else {
  258. <ui-alert
  259. [type]="chat.connection() === 'connecting' ? 'info' : 'warning'"
  260. [title]="chat.connection() === 'connecting' ? '正在连接…' : '连接已断开'"
  261. >
  262. @if (chat.connection() === 'disconnected') {
  263. <ui-button variant="outline" size="sm" (click)="reconnect()">重新连接</ui-button>
  264. }
  265. </ui-alert>
  266. }
  267. </div>
  268. }
  269. <!--
  270. 回环提示(2026-10-21 P0 实测:两个容器能互相 @,但没有任何停止条件,
  271. 28 分钟内互发 96 条,最长连续 57 条无人类介入)。
  272. 这里给用户一个**明确的出口**:看一眼就知道为什么「不回消息了」,
  273. 发一句话或点按钮即可恢复。
  274. -->
  275. @if (loop.paused()) {
  276. <div class="banner-hold">
  277. <ui-alert type="warning" title="检测到自动对话循环,已暂停自动回复">
  278. <p class="loop-detail">{{ loop.pauseDetail() }}</p>
  279. <p class="loop-hint">你发一条消息即可恢复;或在设置里调整检测强度。</p>
  280. <ui-button variant="outline" size="sm" (click)="loop.resume()">继续协作</ui-button>
  281. </ui-alert>
  282. </div>
  283. }
  284. <div class="messages" #messages (scroll)="onMessagesScroll($event)">
  285. @switch (chat.state()) {
  286. @case ('loading') {
  287. <div class="skeleton-hold">
  288. @for (i of [1, 2, 3]; track i) {
  289. <ui-skeleton variant="rect" height="64px" />
  290. }
  291. </div>
  292. }
  293. @case ('error') {
  294. <ui-empty
  295. icon="fa-solid fa-triangle-exclamation"
  296. title="消息加载失败"
  297. [description]="chat.error() ?? ''"
  298. />
  299. <div class="center">
  300. <ui-button variant="outline" size="sm" (click)="retryOpen()">重新加载</ui-button>
  301. </div>
  302. }
  303. @case ('empty') {
  304. @if (!chat.currentSession()) {
  305. <!--
  306. 两种「没有当前会话」的情形,提示词必须分开(2026-11-01):
  307. ① 待建态(用户刚点「新建对话」选了容器,还没发第一条消息)
  308. → 这是**正常的输入状态**,右侧应显示欢迎区 + 可用输入框,
  309. 而不是再让用户点一次「新建对话」(那是个死循环)。
  310. ② 该容器确实一条会话都没有、也不是待建态
  311. → 才提示「还没有对话 / 点击左侧新建」。
  312. -->
  313. @if (chat.pendingNewSession()) {
  314. <div class="welcome">
  315. <div class="welcome-inner">
  316. <ui-avatar [icon]="'fa-solid fa-robot'" [size]="'lg'" />
  317. <h2 class="welcome-title">
  318. {{ greeting() }},我是 {{ chat.agent()?.name ?? '你的数字生命' }}
  319. </h2>
  320. <p class="welcome-sub">直接输入第一条消息,就会开始这段对话。</p>
  321. <div class="suggests">
  322. @for (s of suggests; track s.title) {
  323. <button
  324. type="button"
  325. class="suggest"
  326. (click)="chat.setDraft(s.text)"
  327. >
  328. <i class="fa-solid" [class]="s.icon" aria-hidden="true"></i>
  329. <span class="suggest-title">{{ s.title }}</span>
  330. <span class="suggest-text">{{ s.text }}</span>
  331. </button>
  332. }
  333. </div>
  334. </div>
  335. </div>
  336. } @else {
  337. <div class="no-session">
  338. <i class="fa-solid fa-comments" aria-hidden="true"></i>
  339. <h2 class="no-session-title">还没有对话</h2>
  340. <p class="no-session-sub">点击左侧「新建对话」,选择一个数字生命后开始。</p>
  341. <ui-button variant="primary" size="sm" (click)="requestNewSession()">
  342. 新建对话
  343. </ui-button>
  344. </div>
  345. }
  346. } @else {
  347. <!--
  348. 空会话欢迎区(2026-10-29,参考 ChatGPT 落地页):
  349. 给「该做什么」的具体引导,而不是一句泛泛的「还没有消息」。
  350. 建议项点一下即填入输入框(不直接发送,让用户能改)。
  351. -->
  352. <div class="welcome">
  353. <div class="welcome-inner">
  354. <!--
  355. 用图标而不是姓名首字(2026-10-30 用户反馈「这里为什么有个竖条」):
  356. 容器名首字母是 l / I 时,渲染出来就是一根竖线,看着像渲染故障。
  357. -->
  358. <ui-avatar [icon]="'fa-solid fa-robot'" [size]="'lg'" />
  359. <h2 class="welcome-title">
  360. {{ greeting() }},我是 {{ chat.agent()?.name ?? '你的数字生命' }}
  361. </h2>
  362. <p class="welcome-sub">
  363. {{ chat.agent()?.specialty || '把要做的事直接说给我,我来干。' }}
  364. </p>
  365. <div class="suggests">
  366. @for (s of suggests; track s.text) {
  367. <button type="button" class="suggest" (click)="useSuggest(s.text)">
  368. <i class="fa-solid" [class]="s.icon" aria-hidden="true"></i>
  369. <span class="suggest-text">
  370. <strong>{{ s.title }}</strong>
  371. <small class="ellipsis">{{ s.text }}</small>
  372. </span>
  373. </button>
  374. }
  375. </div>
  376. </div>
  377. </div>
  378. }
  379. }
  380. @default {
  381. <!-- AC-04.4:向上翻历史(滚动到顶部自动加载) -->
  382. <div class="history-hold">
  383. @if (chat.paginating()) {
  384. <ui-skeleton variant="rect" height="40px" />
  385. } @else if (chat.hasMoreHistory()) {
  386. <ui-button variant="ghost" size="sm" (click)="loadEarlier()">
  387. 加载更早的消息
  388. </ui-button>
  389. } @else {
  390. <span class="history-end">已到最早</span>
  391. }
  392. </div>
  393. @for (row of messageRows(); track row.key) {
  394. @if (row.kind === 'tool') {
  395. <!--
  396. 连续的终端活动消息(容器把 Hermes 每一步都单独发一条)
  397. 合并成一个折叠组:默认收起,点开才看命令细节。
  398. 用户实测反馈:这些内容把会话完全淹没了。
  399. -->
  400. <app-tool-activity [block]="row.block" />
  401. } @else {
  402. <app-message-bubble
  403. [message]="row.message!"
  404. [own]="row.message!.sender === 'me'"
  405. (retry)="chat.retry($event)"
  406. (openTask)="openTaskRail()"
  407. (preview)="openFile($event)"
  408. />
  409. }
  410. }
  411. <!--
  412. 对方正在输入(2026-11-01 用户要求:放在**下一条消息的位置**)。
  413. 即消息流末尾、紧贴最后一条消息,而不是漂浮在输入框上方。
  414. 只在有内容时才渲染 —— 它出现在视口外时不可见,所以下面
  415. 的 stickToBottom 要跟着它一起滚到底。
  416. -->
  417. @if (chat.peerTyping()) {
  418. <div class="typing-row" aria-live="polite">
  419. <app-typing-indicator [name]="chat.agent()?.name ?? '对方'" />
  420. </div>
  421. }
  422. }
  423. }
  424. </div>
  425. <!--
  426. 输入区外层(2026-10-29):与消息流同宽居中限宽 768px,
  427. 卡片式输入框才不会被拉到整列宽度(宽屏下单行输入框横跨 1200px 很怪)。
  428. -->
  429. <!--
  430. 正在输入指示器已移入消息流末尾(2026-11-01 用户要求):
  431. 它应该出现在「下一条消息」的位置,而不是漂浮在输入框上方。
  432. 见 .typing-row。
  433. -->
  434. <!--
  435. 🔴 没有选中会话时不显示输入区(2026-11-01 用户要求)。
  436. 空态下仍摆一个可以打字的输入框,用户会以为「直接输入就能开始」,
  437. 但此时没有线程根,发出去会走隐式建会话的旁路,容易产生语义混乱。
  438. 空态只保留说明 + 「新建对话」按钮,入口唯一。
  439. 例外:state 是 'ready'/'empty' 但有 currentSession(正常会话)→ 正常显示。
  440. -->
  441. @if (chat.currentSession() || chat.pendingNewSession()) {
  442. <div class="composer-hold">
  443. <!--
  444. 暂存附件条:选文件后**先停在这里**,点发送才真正上传+发送。
  445. 用户要求「先上传本地,点发送时随文字一起发」——
  446. 早期实现选完就立刻上传并单发一条,不符合。
  447. -->
  448. @if (chat.pendingFiles().length > 0) {
  449. <div class="pending-holds" role="list" aria-label="待发送附件">
  450. @for (pf of chat.pendingFiles(); track pf.id) {
  451. <!--
  452. 图片附件(2026-10-31 按参考图重做):
  453. 大缩略图 + 下方「解释图片 →」+ 右上角移除。
  454. 点缩略图直接打开图片预览(灯箱)。
  455. -->
  456. @if (pf.previewUrl) {
  457. <div class="pending-card" role="listitem">
  458. <button
  459. type="button"
  460. class="pending-card-img"
  461. [attr.aria-label]="'预览 ' + pf.file.name"
  462. (click)="previewPending(pf)"
  463. >
  464. <img [src]="pf.previewUrl" [alt]="pf.file.name" />
  465. </button>
  466. <span class="pending-card-act" aria-hidden="true">解释图片 →</span>
  467. <button
  468. type="button"
  469. class="pending-x pending-x--float"
  470. [attr.aria-label]="'移除 ' + pf.file.name"
  471. (click)="chat.unstageFile(pf.id)"
  472. >
  473. <i class="fa-solid fa-xmark" aria-hidden="true"></i>
  474. </button>
  475. </div>
  476. } @else {
  477. <span class="pending-chip" role="listitem">
  478. <i class="fa-solid fa-paperclip" aria-hidden="true"></i>
  479. <span class="pending-name ellipsis" [title]="pf.file.name">{{ pf.file.name }}</span>
  480. <button
  481. type="button"
  482. class="pending-x"
  483. [attr.aria-label]="'移除 ' + pf.file.name"
  484. (click)="chat.unstageFile(pf.id)"
  485. >
  486. <i class="fa-solid fa-xmark" aria-hidden="true"></i>
  487. </button>
  488. </span>
  489. }
  490. }
  491. </div>
  492. }
  493. <app-composer
  494. [draft]="chat.draft()"
  495. [sending]="chat.sending()"
  496. [uploading]="chat.uploading()"
  497. [pct]="chat.uploadPct()"
  498. [disabled]="chat.connection() !== 'connected'"
  499. (draftChange)="chat.setDraft($event)"
  500. (send)="chat.sendText($event)"
  501. (attach)="chat.stageFiles($event)"
  502. />
  503. </div>
  504. }
  505. }
  506. </section>
  507. <!-- ===== 右栏(宽不足时折叠为浮层,不删功能)===== -->
  508. @if (chat.agentId() && railOpen()) {
  509. <!--
  510. 拖宽手柄(2026-10-30 参照 DSH 右栏):
  511. 双栏布局里右栏宽固定、内容可窄可宽,用户会想按自己的内容调。
  512. 手柄 5px 但叠了 8px 的点击热区(::after),不专门点中那一条线。
  513. -->
  514. <div
  515. class="rail-resizer"
  516. role="separator"
  517. aria-orientation="vertical"
  518. aria-label="拖动调整侧栏宽度,双击复原"
  519. [attr.aria-valuenow]="railWidth()"
  520. (pointerdown)="startResize($event)"
  521. (dblclick)="resetRailWidth()"
  522. ></div>
  523. <aside class="col rail-col" aria-label="侧栏面板">
  524. <!--
  525. 标题行(参照 DSH):标签条只有图标+文字时,默认激活哪一页不明显,
  526. 整块区域看着像空的。用「图标 + 面板名 + 一句说明」把当前上下文说清楚。
  527. -->
  528. <header class="rail-head">
  529. <span class="rail-head-ico" aria-hidden="true">
  530. <i class="fa-solid" [class]="railCurrent().icon"></i>
  531. </span>
  532. <span class="rail-head-text">
  533. <strong>{{ railCurrent().label }}</strong>
  534. <small class="ellipsis">{{ railCurrent().hint }}</small>
  535. </span>
  536. <ui-button
  537. variant="ghost"
  538. shape="circle"
  539. size="sm"
  540. icon="fa-solid fa-xmark"
  541. ariaLabel="收起面板"
  542. (click)="railOpen.set(false)"
  543. />
  544. </header>
  545. <!--
  546. 四标签(参考 DSH 右栏):一个标签只讲一件事。
  547. 原实现把「运行状态 / 最近动态」常驻在任务、成果两页里,
  548. 面板语义混乱 —— 现在它们只在「状态」页出现。
  549. -->
  550. <div class="rail-tabs" role="tablist" aria-label="侧栏面板">
  551. @for (t of railTabs; track t.key) {
  552. <button
  553. type="button"
  554. class="rail-tab"
  555. role="tab"
  556. [class.on]="railTab() === t.key"
  557. [attr.aria-selected]="railTab() === t.key"
  558. (click)="railTab.set(t.key)"
  559. >
  560. <i class="fa-solid" [class]="t.icon" aria-hidden="true"></i>
  561. <span>{{ t.label }}</span>
  562. @if (t.key === 'files' && sessionFiles().length > 0) {
  563. <span class="rail-tab-count">{{ sessionFiles().length }}</span>
  564. }
  565. @if (t.key === 'tasks' && agentTasks().length > 0) {
  566. <span class="rail-tab-count">{{ agentTasks().length }}</span>
  567. }
  568. </button>
  569. }
  570. </div>
  571. <div class="rail-body" [class.has-preview]="!!previewFile()">
  572. <!--
  573. 文件预览(2026-10-30 重构):右栏内联,头部自带「右侧 | 全屏」切换。
  574. 文件名与返回按钮已收进 app-file-preview 头部,这里不再重复一行。
  575. 图片不进右栏(打开即走深色灯箱),见下方 app-image-lightbox。
  576. -->
  577. @if (previewFile(); as pf) {
  578. <div class="rail-preview">
  579. <app-file-preview
  580. [open]="true"
  581. [inline]="true"
  582. [url]="pf.url"
  583. [name]="pf.name"
  584. [mimeType]="pf.mimeType"
  585. (closed)="previewFile.set(null)"
  586. />
  587. </div>
  588. } @else if (chat.agent(); as a) {
  589. @if (railTab() === 'status') {
  590. <section class="rail-block">
  591. <h3>运行状态</h3>
  592. <dl class="kv">
  593. <div>
  594. <dt>当前任务</dt>
  595. <dd [class]="'tone-ink-' + taskLine(a.id).tone">{{ taskLine(a.id).text }}</dd>
  596. </div>
  597. <div><dt>CPU</dt><dd>{{ pct(a.host?.cpuPercent) }}</dd></div>
  598. <div><dt>内存</dt><dd>{{ pct(a.host?.memPercent) }}</dd></div>
  599. <div><dt>Token 用量</dt><dd>{{ tokens(a) }}</dd></div>
  600. </dl>
  601. </section>
  602. <!--
  603. 最近动态:只记录**观测到的**开始/结束(每次轮询对比得出)。
  604. 只列本会话 —— 用户在这里关心的是「它刚才干了什么」。
  605. -->
  606. @if (pulses().length > 0) {
  607. <section class="rail-block">
  608. <h3>最近动态</h3>
  609. <ol class="pulse-list">
  610. @for (p of pulses(); track p.at + p.kind) {
  611. <li class="pulse-item">
  612. <i
  613. class="fa-solid"
  614. [class.fa-play]="p.kind === 'started'"
  615. [class.fa-check]="p.kind === 'finished'"
  616. aria-hidden="true"
  617. ></i>
  618. <span class="pulse-text ellipsis">
  619. {{ p.kind === 'started' ? (p.task || '开始处理') : '完成' }}
  620. </span>
  621. <small>{{ clock(p.at) }}</small> </li>
  622. }
  623. </ol>
  624. </section>
  625. }
  626. }
  627. @if (railTab() === 'tasks') {
  628. <section class="rail-block">
  629. <h3>相关任务 <span class="count">{{ agentTasks().length }}</span></h3>
  630. @if (agentTasks().length === 0) {
  631. <p class="rail-empty">该数字生命暂无任务。</p>
  632. } @else {
  633. @for (t of agentTasks(); track t.id) {
  634. <button type="button" class="rail-item task" (click)="goTask(t.id)">
  635. <span class="file-icon task-ico" aria-hidden="true">
  636. <i class="fa-solid fa-list-check"></i>
  637. </span>
  638. <span class="rail-meta">
  639. <strong class="ellipsis">{{ t.title }}</strong>
  640. <small class="ellipsis">
  641. {{ t.id }} · {{ statusText(t.status) }}
  642. </small>
  643. </span>
  644. <i class="fa-solid fa-chevron-right" aria-hidden="true"></i>
  645. </button>
  646. }
  647. }
  648. </section>
  649. }
  650. @if (railTab() === 'files') {
  651. <!--
  652. 本会话文件:从**对话消息**里汇总附件。
  653. 成果栏原数据来自平台上报(容器还没上报时永远是 0),
  654. 而容器交付的文件其实就在对话里 —— 这里直接列出来,用户马上能用。
  655. -->
  656. <section class="rail-block">
  657. <h3>本会话文件 <span class="count">{{ sessionFiles().length }}</span></h3>
  658. @if (sessionFiles().length === 0) {
  659. <p class="rail-empty">这条对话里还没有文件。</p>
  660. } @else {
  661. @for (f of sessionFiles(); track f.url + f.name) {
  662. <button type="button" class="rail-item" (click)="openFile(f)">
  663. <span class="file-icon" aria-hidden="true">
  664. <i class="fa-solid" [class]="fileIconByMime(f.mimeType)"></i>
  665. </span>
  666. <span class="rail-meta">
  667. <strong class="ellipsis">{{ f.name }}</strong>
  668. <small class="ellipsis">{{ fmtSize(f.size) }}</small>
  669. </span>
  670. <i class="fa-solid fa-chevron-right" aria-hidden="true"></i>
  671. </button>
  672. }
  673. }
  674. </section>
  675. }
  676. @if (railTab() === 'results') {
  677. <section class="rail-block">
  678. <h3>相关成果 <span class="count">{{ agentDeliverables().length }}</span></h3>
  679. @if (agentDeliverables().length === 0) {
  680. <p class="rail-empty">该数字生命暂无交付成果。</p>
  681. } @else {
  682. @for (d of agentDeliverables(); track d.id) {
  683. <!--
  684. 🔴 与「本会话文件」同一套卡片与交互(2026-11-01 用户要求):
  685. 整行可点 = 直接打开预览,右侧给出明确的进入箭头。
  686. 原来是 div + 右侧两个小圆钮,点了不明显、和文件栏观感割裂。
  687. 下载仍保留为独立按钮(点行是预览,点下载是保存,语义不混)。
  688. -->
  689. <button type="button" class="rail-item" (click)="preview(d)">
  690. <span class="file-icon" aria-hidden="true">
  691. <i class="fa-solid" [class]="fileIcon(d.fileType)"></i>
  692. </span>
  693. <span class="rail-meta">
  694. <strong class="ellipsis">{{ d.title }}</strong>
  695. <!-- 🔴 无附件时不能渲染成「· 」空分隔符(曾显示成「仅有 · 的文档」) -->
  696. <small class="ellipsis">
  697. {{ d.filename || '暂无附件' }}
  698. @if (d.filename && d.size) {
  699. · {{ d.size }}
  700. }
  701. </small>
  702. <!--
  703. 附件还在容器里(未上云盘):如实说明,不给假下载按钮。
  704. 「正在同步到云盘」= 已登记;有同步失败原因时直接展示原因。
  705. -->
  706. @if (pendingOf(d); as a) {
  707. <small class="ellipsis sync-hint" [title]="syncError(d.id, a) || ''">
  708. @if (syncError(d.id, a); as err) {
  709. <i class="fa-solid fa-circle-exclamation" aria-hidden="true"></i>
  710. 未上云:{{ err }}
  711. } @else {
  712. <i class="fa-solid fa-cloud-arrow-up" aria-hidden="true"></i>
  713. 排队搬往云盘…
  714. }
  715. </small>
  716. }
  717. </span>
  718. <i class="fa-solid fa-chevron-right" aria-hidden="true"></i>
  719. </button>
  720. }
  721. }
  722. </section>
  723. }
  724. }
  725. </div>
  726. </aside>
  727. }
  728. </div>
  729. <!--
  730. 成果预览(2026-10-31):有 url 的走右栏内联(见 preview());
  731. 这里只留「产物还没上云、暂无 url」的说明弹窗 —— 不假装能预览。
  732. -->
  733. <ui-modal
  734. [open]="!!previewing()"
  735. title="这份成果还没有可预览的文件"
  736. (openChange)="previewing.set(null)"
  737. >
  738. <p class="preview-pending-hint">
  739. 容器还在把文件搬到云盘,完成后可以在右栏「成果」里直接预览。
  740. </p>
  741. </ui-modal>
  742. <!--
  743. 消息附件预览已改为**右栏内联**(2026-10-30),不再使用全屏遮罩弹窗。
  744. 见右栏 .rail-preview 区块。
  745. 图片 / 音频 / 视频不进右栏,由 openFile 分流到下面两个轻量浮层:
  746. -->
  747. <app-image-lightbox
  748. [open]="!!lightboxFile()"
  749. [url]="lightboxFile()?.url ?? ''"
  750. [name]="lightboxFile()?.name ?? ''"
  751. [size]="lightboxFile()?.size ?? 0"
  752. (closed)="lightboxFile.set(null)"
  753. />
  754. <ui-modal
  755. [open]="!!mediaFile()"
  756. [title]="mediaFile()?.name ?? ''"
  757. (openChange)="onMediaOpenChange($event)"
  758. >
  759. @if (mediaFile(); as mf) {
  760. @if (mediaKind(mf) === 'video') {
  761. <video class="media-modal-el" [src]="mf.url" controls playsinline></video>
  762. } @else {
  763. <audio class="media-modal-el media-modal-el--audio" [src]="mf.url" controls></audio>
  764. }
  765. }
  766. </ui-modal>
  767. <!--
  768. 新建对话模态框已移到外壳(app-new-session-modal)——
  769. 新建入口收敛为左栏顶部唯一按钮,与页面解耦。
  770. -->
  771. <!--
  772. 删除会话索引(规范:删除类二次确认一律走居中 ui-modal)。
  773. 文案必须说清「消息不会被删除」—— 列表只是索引,正文在 Matrix 房间里。
  774. -->
  775. <ui-modal
  776. [open]="removing() !== null"
  777. title="从列表移除这条对话?"
  778. (openChange)="removing.set(null)"
  779. >
  780. <p class="remove-note">
  781. 仅从左侧列表移除「{{ removing()?.title }}」。<br />
  782. <strong>消息内容不会被删除</strong>,仍在服务器上。
  783. </p>
  784. <div class="remove-acts">
  785. <ui-button variant="ghost" (click)="removing.set(null)">取消</ui-button>
  786. <ui-button variant="primary" (click)="doRemove()">移除</ui-button>
  787. </div>
  788. </ui-modal>
  789. <!-- 重命名弹层已移除(2026-10-31 用户要求:对话命名不可修改) -->
  790. `,
  791. styles: [
  792. `
  793. @use '../../../core/styles/breakpoints' as bp;
  794. :host {
  795. display: block;
  796. height: 100%;
  797. /* 参与父级网格时,默认的 min-height:auto 会被内容撑高 → 整页滚动 */
  798. min-height: 0;
  799. overflow: hidden;
  800. }
  801. /*
  802. * 拖动右栏宽度期间禁止选中文本 —— 否则鼠标划过整页文字全被刷蓝。
  803. * 用 :host-context 而不用全局样式:只在会话页生效,且不必动 styles.scss
  804. * (那里有行数预算,2026-10-30 已经顶到 300 行)。
  805. */
  806. :host-context(body.is-resizing-rail) {
  807. user-select: none;
  808. cursor: col-resize;
  809. }
  810. /*
  811. 四栏:列表固定可读宽(min~max),主区吃掉全部剩余,右栏按比例但设上下限。
  812. —— 窗口变宽时主区跟着变宽(不是内容整体缩放),这是"流动布局"的要点。
  813. */
  814. /*
  815. 列表栏用**固定可读宽**(280px)而非比例:列表项是纵向排列,
  816. 宽度随窗口缩放会把名字/预览压到不可读。右栏才用比例。
  817. */
  818. /*
  819. 列数随右栏显隐**动态变化** —— 否则右栏隐藏后那一列仍占位,聊天区不填满。
  820. 用 :has() 检测 .rail-col 是否存在(存在才保留第三列)。
  821. */
  822. .conversation-layout {
  823. display: grid;
  824. grid-template-columns: minmax(0, 1fr);
  825. height: 100%;
  826. min-height: 0;
  827. }
  828. /*
  829. * 有右栏时是三列:主区 | 拖宽手柄(5px) | 右栏。
  830. * 右栏宽度由 JS 写到 inline style,这里只给 1fr 之外的兜底 ——
  831. * 🔴 列数必须把 resizer 算进去,否则右栏会被挤到下一行(实测:掉到内容下方)。
  832. */
  833. .conversation-layout:has(> .rail-col) {
  834. grid-template-columns: minmax(0, 1fr) 5px var(--rail-w, 320px);
  835. }
  836. /*
  837. * 窄窗(<1150)压缩右栏宽度,但**不让它换行**(2026-10-30 用户要求)。
  838. *
  839. * 🔴 原实现把栅格改成单列 + 隐藏右栏;一旦隐藏规则因为任何原因没生效
  840. * (选择器优先级、样式加载顺序),右栏就会掉到主区**下方**,
  841. * 表现为"右栏跑到页面底部"(实测现象)。
  842. * 现在的做法:列数恒定三列,只把第三列压到 280px 下限,
  843. * 从结构上排除"掉到下方"的可能。
  844. */
  845. @include bp.dk-below-4col {
  846. .conversation-layout:has(> .rail-col) {
  847. grid-template-columns: minmax(0, 1fr) 5px minmax(240px, var(--rail-w, 320px));
  848. }
  849. }
  850. .col {
  851. min-width: 0;
  852. min-height: 0;
  853. display: flex;
  854. flex-direction: column;
  855. background: var(--surface);
  856. }
  857. .main-col {
  858. background: var(--surface);
  859. border-right: 1px solid var(--edge);
  860. }
  861. .rail-col {
  862. background: var(--rail-surface);
  863. /* 宽度来自栅格列(--rail-w 由容器上的 inline 变量传入) */
  864. min-width: 0;
  865. display: flex;
  866. flex-direction: column;
  867. overflow: hidden;
  868. }
  869. /* ---- 拖宽手柄 ---- */
  870. .rail-resizer {
  871. position: relative;
  872. width: 5px;
  873. z-index: 2;
  874. cursor: col-resize;
  875. flex-shrink: 0;
  876. background: transparent;
  877. transition: background-color var(--motion-fast) var(--ease-standard);
  878. }
  879. /* 热区左右各扩 3px:5px 的线太细,很难点中 */
  880. .rail-resizer::after {
  881. content: '';
  882. position: absolute;
  883. inset: 0 -3px;
  884. }
  885. .rail-resizer:hover,
  886. .rail-resizer:active {
  887. background: var(--primary);
  888. }
  889. .rail-resizer:focus-visible {
  890. outline: 2px solid var(--primary);
  891. outline-offset: -1px;
  892. }
  893. /* ---- 右栏标题行 ---- */
  894. /* ---- 右栏内联预览 ---- */
  895. /*
  896. * 预览时 rail-body 不滚动、不留内边距(2026-10-31):
  897. * rail-body 的滚动容器 + 预览面板的 100% 高度叠加,在浏览器整体缩放后
  898. * 会把底部状态栏挤出可视区(用户截图)。预览面板内部自己滚动即可。
  899. */
  900. .rail-body.has-preview {
  901. padding: 0;
  902. overflow: hidden;
  903. /* flex 列:让 .rail-preview 撑满,而不是按内容塌缩(block 下子元素无高度) */
  904. display: flex;
  905. flex-direction: column;
  906. gap: 0;
  907. align-content: normal;
  908. }
  909. .rail-preview {
  910. display: flex;
  911. flex-direction: column;
  912. flex: 1;
  913. min-height: 0;
  914. overflow: hidden;
  915. }
  916. /* 音视频模态 */
  917. .media-modal-el {
  918. display: block;
  919. width: 100%;
  920. max-height: 70dvh;
  921. border-radius: var(--r-sm);
  922. background: var(--surface-2);
  923. }
  924. .media-modal-el--audio {
  925. max-height: none;
  926. }
  927. .rail-preview app-file-preview {
  928. /* flex 链而非 display:block:块级宿主会按内容塌缩(实测面板高度随缩放变化) */
  929. flex: 1 1 0;
  930. min-height: 0;
  931. overflow: hidden;
  932. }
  933. /* 预览期间隐藏标签条 —— 已经在看具体文件了,切标签会让预览莫名消失 */
  934. .rail-col:has(.rail-preview) .rail-tabs {
  935. display: none;
  936. }
  937. .rail-head {
  938. display: flex;
  939. align-items: center;
  940. gap: var(--sp-2);
  941. padding: var(--sp-3) var(--sp-3) var(--sp-2);
  942. flex-shrink: 0;
  943. }
  944. .rail-head-ico {
  945. display: grid;
  946. place-items: center;
  947. width: 28px;
  948. height: 28px;
  949. flex-shrink: 0;
  950. border-radius: var(--r-sm);
  951. background: var(--surface-hover);
  952. color: var(--ink-2);
  953. font-size: var(--fs-12);
  954. }
  955. .rail-head-text {
  956. display: grid;
  957. gap: 1px;
  958. flex: 1;
  959. min-width: 0;
  960. line-height: 1.35;
  961. }
  962. .rail-head-text strong {
  963. font-size: var(--fs-13);
  964. font-weight: 600;
  965. color: var(--ink);
  966. }
  967. .rail-head-text small {
  968. font-size: var(--fs-11);
  969. color: var(--muted);
  970. }
  971. .col-head {
  972. display: flex;
  973. align-items: center;
  974. justify-content: space-between;
  975. gap: var(--sp-2);
  976. padding: var(--sp-4);
  977. border-bottom: 1px solid var(--edge);
  978. }
  979. .col-head h2 {
  980. margin: 0;
  981. font-size: var(--fs-15);
  982. color: var(--ink);
  983. }
  984. .count {
  985. font-size: var(--fs-11);
  986. color: var(--muted);
  987. }
  988. .search-hold {
  989. padding: var(--sp-3) var(--sp-4);
  990. }
  991. .list-scroll {
  992. flex: 1;
  993. min-height: 0;
  994. overflow-y: auto;
  995. padding: 0 var(--sp-2) var(--sp-3);
  996. }
  997. .skeleton-hold {
  998. display: grid;
  999. gap: var(--sp-2);
  1000. padding: var(--sp-2);
  1001. }
  1002. .center {
  1003. display: flex;
  1004. justify-content: center;
  1005. padding: var(--sp-3);
  1006. }
  1007. .center-fill {
  1008. flex: 1;
  1009. display: grid;
  1010. place-content: center;
  1011. }
  1012. /* 会话行:与容器行缩进错开,一眼看出层级 */
  1013. .session-row {
  1014. display: flex;
  1015. align-items: center;
  1016. gap: var(--sp-2);
  1017. min-height: 40px;
  1018. margin-left: var(--sp-6);
  1019. padding: var(--sp-1) var(--sp-2) var(--sp-1) var(--sp-3);
  1020. border-radius: var(--r-md);
  1021. cursor: pointer;
  1022. }
  1023. .session-row:hover {
  1024. background: var(--surface-hover);
  1025. }
  1026. .session-row.active {
  1027. background: var(--surface-active);
  1028. }
  1029. .session-row.archived .session-title {
  1030. color: var(--faint);
  1031. text-decoration: line-through;
  1032. }
  1033. .session-title {
  1034. flex: 1;
  1035. min-width: 0;
  1036. font-size: var(--fs-13);
  1037. color: var(--ink-2);
  1038. }
  1039. .session-row.active .session-title {
  1040. color: var(--ink);
  1041. font-weight: 600;
  1042. }
  1043. .session-time {
  1044. flex: none;
  1045. font-size: var(--fs-11);
  1046. color: var(--faint);
  1047. }
  1048. /*
  1049. * 操作按钮默认隐藏,悬停或键盘聚焦时出现 —— 列表平时保持安静,
  1050. * 但**不能只靠 hover**:触屏与键盘用户拿不到,所以 :focus-within 也触发。
  1051. */
  1052. .session-acts {
  1053. display: none;
  1054. align-items: center;
  1055. gap: 2px;
  1056. }
  1057. .session-row:hover .session-acts,
  1058. .session-row:focus-within .session-acts {
  1059. display: flex;
  1060. }
  1061. /* 操作按钮出现时让位,避免标题被挤成一两个字 */
  1062. .session-row:hover .session-time,
  1063. .session-row:focus-within .session-time {
  1064. display: none;
  1065. }
  1066. /* 移除确认弹窗 */
  1067. .remove-note {
  1068. margin: 0;
  1069. font-size: var(--fs-13);
  1070. line-height: 1.6;
  1071. color: var(--ink-2);
  1072. }
  1073. .remove-acts {
  1074. display: flex;
  1075. justify-content: flex-end;
  1076. gap: var(--sp-2);
  1077. margin-top: var(--sp-4);
  1078. }
  1079. /* ---------- 空会话欢迎区(2026-10-29,参考 ChatGPT 落地页) ---------- */
  1080. .welcome {
  1081. display: grid;
  1082. place-items: center;
  1083. min-height: 100%;
  1084. padding: var(--sp-6) var(--sp-4);
  1085. }
  1086. .welcome-inner {
  1087. display: grid;
  1088. justify-items: center;
  1089. gap: var(--sp-3);
  1090. width: 100%;
  1091. max-width: 620px;
  1092. text-align: center;
  1093. }
  1094. .welcome-title {
  1095. margin: var(--sp-2) 0 0;
  1096. font-size: var(--fs-20);
  1097. font-weight: 600;
  1098. color: var(--ink);
  1099. line-height: 1.3;
  1100. }
  1101. .welcome-sub {
  1102. margin: 0;
  1103. max-width: 460px;
  1104. font-size: var(--fs-14);
  1105. color: var(--muted);
  1106. line-height: 1.7;
  1107. }
  1108. .suggests {
  1109. display: grid;
  1110. grid-template-columns: repeat(2, minmax(0, 1fr));
  1111. gap: var(--sp-2);
  1112. width: 100%;
  1113. margin-top: var(--sp-3);
  1114. }
  1115. /* 窄窗单列:两列会把标题压成一行省略号,信息全丢 */
  1116. @include bp.dk-below-3col {
  1117. .suggests {
  1118. grid-template-columns: minmax(0, 1fr);
  1119. }
  1120. }
  1121. .suggest {
  1122. display: flex;
  1123. align-items: center;
  1124. gap: var(--sp-3);
  1125. min-height: var(--touch-min);
  1126. padding: var(--sp-3);
  1127. border: 1px solid var(--edge);
  1128. border-radius: var(--r-md);
  1129. background: var(--surface);
  1130. text-align: left;
  1131. cursor: pointer;
  1132. transition: background-color var(--motion-fast) var(--ease-standard);
  1133. }
  1134. .suggest:hover {
  1135. background: var(--surface-hover);
  1136. border-color: var(--edge-strong);
  1137. }
  1138. .suggest > i {
  1139. flex-shrink: 0;
  1140. width: 16px;
  1141. text-align: center;
  1142. color: var(--primary);
  1143. }
  1144. .suggest-text {
  1145. display: grid;
  1146. min-width: 0;
  1147. gap: 2px;
  1148. }
  1149. .suggest-text strong {
  1150. font-size: var(--fs-13);
  1151. font-weight: 600;
  1152. color: var(--ink);
  1153. }
  1154. .suggest-text small {
  1155. font-size: var(--fs-12);
  1156. color: var(--muted);
  1157. }
  1158. /* ---------- 新建对话:选容器模态框(D3) ---------- */ .pick-note {
  1159. margin: 0 0 var(--sp-3);
  1160. font-size: var(--fs-13);
  1161. color: var(--muted);
  1162. line-height: 1.7;
  1163. }
  1164. .pick-list {
  1165. display: grid;
  1166. gap: var(--sp-1);
  1167. max-height: 360px;
  1168. overflow-y: auto;
  1169. }
  1170. .pick-row {
  1171. display: flex;
  1172. align-items: center;
  1173. gap: var(--sp-3);
  1174. min-height: var(--touch-min);
  1175. padding: var(--sp-2) var(--sp-3);
  1176. border: 1px solid var(--edge);
  1177. border-radius: var(--r-md);
  1178. background: var(--surface);
  1179. text-align: left;
  1180. cursor: pointer;
  1181. }
  1182. .pick-row:hover {
  1183. background: var(--surface-hover);
  1184. border-color: var(--edge-strong);
  1185. }
  1186. .pick-row:focus-visible {
  1187. outline: 2px solid var(--primary);
  1188. outline-offset: -2px;
  1189. }
  1190. .pick-text {
  1191. display: grid;
  1192. min-width: 0;
  1193. line-height: 1.3;
  1194. }
  1195. .pick-name {
  1196. font-size: var(--fs-13);
  1197. font-weight: 600;
  1198. color: var(--ink);
  1199. }
  1200. .pick-sub {
  1201. font-size: var(--fs-11);
  1202. color: var(--muted);
  1203. }
  1204. .pick-empty {
  1205. margin: 0;
  1206. padding: var(--sp-4);
  1207. font-size: var(--fs-13);
  1208. color: var(--muted);
  1209. text-align: center;
  1210. }
  1211. .pick-acts {
  1212. display: flex;
  1213. justify-content: flex-end;
  1214. margin-top: var(--sp-3);
  1215. }
  1216. /* 无会话空态(2026-11-01):提示先新建对话,不自动建空会话 */
  1217. .no-session {
  1218. display: grid;
  1219. justify-items: center;
  1220. gap: var(--sp-3);
  1221. padding: var(--sp-8) var(--sp-4);
  1222. text-align: center;
  1223. color: var(--muted);
  1224. }
  1225. .no-session > i {
  1226. font-size: var(--fs-26);
  1227. color: var(--edge-strong, var(--muted));
  1228. }
  1229. .no-session-title {
  1230. margin: 0;
  1231. font-size: var(--fs-15);
  1232. color: var(--ink);
  1233. }
  1234. .no-session-sub {
  1235. margin: 0;
  1236. font-size: var(--fs-13);
  1237. }
  1238. /* 已归档目录 */
  1239. /* 待发送附件条 */
  1240. .pending-holds {
  1241. display: flex;
  1242. flex-wrap: wrap;
  1243. gap: var(--sp-2);
  1244. padding: var(--sp-2) 0;
  1245. }
  1246. /* 输入区与消息流同宽:宽屏下居中,窄屏贴边留 16px */
  1247. /*
  1248. 正在输入指示器:作为消息流的**最后一行**出现(2026-11-01)。
  1249. 与消息气泡同样的居中限宽,让它看起来就是「即将到来的这一条」,
  1250. 而不是浮在输入框上方的独立提示。
  1251. */
  1252. .typing-row {
  1253. display: flex;
  1254. align-items: flex-start;
  1255. margin-bottom: var(--sp-4);
  1256. }
  1257. .composer-hold {
  1258. flex-shrink: 0;
  1259. padding: 0 max(var(--sp-4), calc((100% - 768px) / 2));
  1260. }
  1261. .pending-chip {
  1262. display: inline-flex;
  1263. align-items: center;
  1264. gap: var(--sp-2);
  1265. max-width: 220px;
  1266. padding: var(--sp-1) var(--sp-2);
  1267. border: 1px solid var(--edge);
  1268. border-radius: var(--r-md);
  1269. background: var(--surface-2);
  1270. font-size: var(--fs-12);
  1271. color: var(--ink-2);
  1272. }
  1273. .pending-thumb {
  1274. width: 32px;
  1275. height: 32px;
  1276. border-radius: var(--r-sm);
  1277. object-fit: cover;
  1278. flex: none;
  1279. background: var(--surface-2);
  1280. }
  1281. /* 图片待发送卡片:缩略图铺满,操作条压底部,右上角移除 */
  1282. .pending-card {
  1283. position: relative;
  1284. width: 132px;
  1285. height: 96px;
  1286. border-radius: var(--r-md);
  1287. overflow: hidden;
  1288. background: var(--surface-2);
  1289. box-shadow: var(--shadow-soft, none);
  1290. }
  1291. .pending-card-img {
  1292. display: block;
  1293. width: 100%;
  1294. height: 100%;
  1295. padding: 0;
  1296. border: 0;
  1297. background: none;
  1298. cursor: zoom-in;
  1299. object-fit: cover;
  1300. }
  1301. .pending-card-act {
  1302. position: absolute;
  1303. left: 0;
  1304. right: 0;
  1305. bottom: 0;
  1306. display: flex;
  1307. align-items: center;
  1308. min-height: 26px;
  1309. padding: 0 var(--sp-2);
  1310. font-size: var(--fs-11);
  1311. color: var(--inverse);
  1312. background: color-mix(in srgb, var(--canvas) 62%, transparent);
  1313. backdrop-filter: blur(2px);
  1314. white-space: nowrap;
  1315. pointer-events: none;
  1316. }
  1317. .pending-x--float {
  1318. position: absolute;
  1319. top: 4px;
  1320. right: 4px;
  1321. border-radius: var(--r-tag);
  1322. background: color-mix(in srgb, var(--canvas) 60%, transparent);
  1323. color: var(--inverse);
  1324. opacity: 0;
  1325. transition: opacity var(--motion-fast) var(--ease-standard);
  1326. }
  1327. .pending-card:hover .pending-x--float,
  1328. .pending-card:focus-within .pending-x--float {
  1329. opacity: 1;
  1330. }
  1331. .pending-name {
  1332. min-width: 0;
  1333. }
  1334. .pending-x {
  1335. display: grid;
  1336. place-items: center;
  1337. flex: none;
  1338. width: 20px;
  1339. height: 20px;
  1340. border: 0;
  1341. border-radius: var(--r-sm);
  1342. background: transparent;
  1343. color: var(--faint);
  1344. cursor: pointer;
  1345. }
  1346. .pending-x:hover {
  1347. background: var(--surface-hover);
  1348. color: var(--ink);
  1349. }
  1350. .archived-toggle {
  1351. display: flex;
  1352. align-items: center;
  1353. gap: var(--sp-2);
  1354. width: calc(100% - var(--sp-6));
  1355. margin-left: var(--sp-6);
  1356. min-height: 32px;
  1357. padding: var(--sp-1) var(--sp-2) var(--sp-1) var(--sp-3);
  1358. border: 0;
  1359. border-radius: var(--r-md);
  1360. background: transparent;
  1361. font: inherit;
  1362. font-size: var(--fs-12);
  1363. color: var(--faint);
  1364. cursor: pointer;
  1365. }
  1366. .archived-toggle:hover {
  1367. background: var(--surface-hover);
  1368. color: var(--muted);
  1369. }
  1370. .archived-toggle:focus-visible {
  1371. outline: 2px solid var(--primary);
  1372. outline-offset: -2px;
  1373. }
  1374. .archived-toggle i {
  1375. width: 12px;
  1376. font-size: var(--fs-10);
  1377. }
  1378. .archived-count {
  1379. padding: 0 6px;
  1380. border-radius: 999px;
  1381. background: var(--surface-2);
  1382. font-size: var(--fs-11);
  1383. }
  1384. .session-empty {
  1385. margin-left: var(--sp-6);
  1386. padding: var(--sp-2) var(--sp-3);
  1387. font-size: var(--fs-12);
  1388. color: var(--faint);
  1389. }
  1390. /* 头部:当前任务名(只读,2026-10-31 取消改名) */
  1391. .preview-pending-hint {
  1392. margin: 0;
  1393. color: var(--ink-2);
  1394. font-size: var(--fs-13);
  1395. line-height: 1.6;
  1396. }
  1397. .session-name {
  1398. display: inline-flex;
  1399. align-items: center;
  1400. gap: var(--sp-1);
  1401. cursor: default;
  1402. }
  1403. .session-name:hover {
  1404. color: var(--ink-2);
  1405. }
  1406. .archived-tag {
  1407. margin-left: var(--sp-1);
  1408. padding: 0 var(--sp-1);
  1409. border-radius: var(--r-sm);
  1410. background: var(--surface-2);
  1411. color: var(--faint);
  1412. font-size: var(--fs-11);
  1413. }
  1414. .conv-info {
  1415. display: grid;
  1416. gap: 2px;
  1417. flex: 1;
  1418. min-width: 0;
  1419. }
  1420. .conv-line {
  1421. display: flex;
  1422. align-items: center;
  1423. gap: var(--sp-2);
  1424. }
  1425. .conv-line strong {
  1426. font-size: var(--fs-15);
  1427. color: var(--ink);
  1428. }
  1429. /* 预览操作区:多个附件时按钮带文件名,过长截断 */
  1430. .preview-actions {
  1431. display: flex;
  1432. flex-wrap: wrap;
  1433. gap: var(--sp-2);
  1434. margin-top: var(--sp-3);
  1435. }
  1436. .download {
  1437. display: inline-flex;
  1438. align-items: center;
  1439. gap: var(--sp-1);
  1440. max-width: 240px;
  1441. min-height: var(--touch-min);
  1442. padding: 0 var(--sp-3);
  1443. border-radius: var(--r-sm);
  1444. background: var(--primary);
  1445. color: var(--primary-content);
  1446. font-size: var(--fs-13);
  1447. text-decoration: none;
  1448. }
  1449. .dl-name {
  1450. overflow: hidden;
  1451. text-overflow: ellipsis;
  1452. white-space: nowrap;
  1453. }
  1454. .conv-preview {
  1455. font-size: var(--fs-12);
  1456. color: var(--muted);
  1457. }
  1458. /* 第二行:任务描述占满剩余宽度,接入态徽标靠右不换行 */
  1459. .conv-subline {
  1460. display: flex;
  1461. align-items: center;
  1462. gap: var(--sp-2);
  1463. min-width: 0;
  1464. }
  1465. .conv-task {
  1466. flex: 1;
  1467. min-width: 0;
  1468. font-size: var(--fs-12);
  1469. color: var(--muted);
  1470. }
  1471. .unread {
  1472. min-width: 18px;
  1473. padding: 0 5px;
  1474. border-radius: var(--r-tag);
  1475. background: var(--danger);
  1476. color: var(--on-danger);
  1477. font-size: var(--fs-11);
  1478. line-height: 18px;
  1479. text-align: center;
  1480. flex-shrink: 0;
  1481. }
  1482. /*
  1483. 头部两行:① 头像 + 名称 + 状态徽标 + 操作 ② 当前任务整行。
  1484. 用 grid 显式分行,任务名再长也不会挤压头像/标题(此前挤成一排)。
  1485. */
  1486. /*
  1487. * 标题栏(2026-10-29):**单行**,高度 56px 与顶部导航一致。
  1488. * 原来是「头像/标题/状态/操作」+「任务状态独占一行」的两行网格 ——
  1489. * 多出来的那一行把整个消息区往下推,观感上就是"顶部被抬高了"。
  1490. * 现在任务状态并入标题行右侧,作为紧凑标签。
  1491. */
  1492. .chat-head {
  1493. display: grid;
  1494. grid-template-columns: auto minmax(0, 1fr) auto auto auto;
  1495. grid-template-areas: 'avatar title task status actions';
  1496. align-items: center;
  1497. column-gap: var(--sp-3);
  1498. /*
  1499. * 🔴 固定高度而不是 min-height:内容变化(任务状态出现/消失)不得改变栏高,
  1500. * 否则下方消息区会跟着位移 —— 用户看到的"发送时跳动"。
  1501. */
  1502. height: var(--desk-topbar-h);
  1503. padding: 0 var(--sp-4);
  1504. border-bottom: 1px solid var(--edge);
  1505. }
  1506. /* 任务状态槽位:定宽定高,内部截断 */
  1507. .task-slot {
  1508. display: flex;
  1509. align-items: center;
  1510. min-width: 0;
  1511. max-width: 220px;
  1512. height: 100%;
  1513. }
  1514. .chat-head > ui-avatar {
  1515. grid-area: avatar;
  1516. }
  1517. .chat-head > .chat-title {
  1518. grid-area: title;
  1519. }
  1520. .chat-head > .task-line {
  1521. grid-area: task;
  1522. }
  1523. /* 🔴 徽标与操作区各占一格 —— 曾经共用 status 格,操作按钮把徽标压住了 */
  1524. .chat-head > app-status-badge {
  1525. grid-area: status;
  1526. justify-self: end;
  1527. }
  1528. .chat-head > .chat-actions {
  1529. grid-area: actions;
  1530. justify-self: end;
  1531. }
  1532. .chat-head > .task-line {
  1533. grid-area: task;
  1534. }
  1535. .chat-title {
  1536. display: grid;
  1537. gap: 2px;
  1538. flex: 1;
  1539. min-width: 0;
  1540. }
  1541. .chat-title strong {
  1542. font-size: var(--fs-15);
  1543. color: var(--ink);
  1544. }
  1545. .chat-title small {
  1546. font-size: var(--fs-12);
  1547. color: var(--muted);
  1548. }
  1549. .chat-actions {
  1550. display: flex;
  1551. gap: var(--sp-1);
  1552. }
  1553. /*
  1554. 当前任务整行:一个会话一屏只出现一次「它在做什么」。
  1555. 左侧细色条 + 会转的小点表达「正在做」,完成态不转。
  1556. 用整行而非胶囊:任务描述通常较长,胶囊装不下只能省略。
  1557. */
  1558. .task-line {
  1559. display: inline-flex;
  1560. align-items: center;
  1561. gap: var(--sp-2);
  1562. min-width: 0;
  1563. max-width: 100%;
  1564. padding: var(--sp-1) var(--sp-2);
  1565. border-radius: var(--r-sm);
  1566. font-size: var(--fs-12);
  1567. color: var(--muted);
  1568. }
  1569. .task-line .spin {
  1570. width: 8px;
  1571. height: 8px;
  1572. flex-shrink: 0;
  1573. border-radius: 50%;
  1574. background: var(--muted);
  1575. }
  1576. .task-line.busy .spin {
  1577. background: var(--success);
  1578. animation: pulse-dot 1.4s var(--ease-standard) infinite;
  1579. }
  1580. .task-line.tone-warning {
  1581. color: var(--warning);
  1582. }
  1583. .task-line.tone-warning .spin {
  1584. background: var(--warning);
  1585. }
  1586. .task-line.tone-danger {
  1587. border-left-color: var(--danger);
  1588. color: var(--danger);
  1589. }
  1590. .task-line.tone-danger .spin {
  1591. background: var(--danger);
  1592. }
  1593. .task-line.tone-success,
  1594. .task-line.tone-info {
  1595. border-left-color: var(--success);
  1596. color: var(--success);
  1597. }
  1598. .task-line.tone-success .spin,
  1599. .task-line.tone-info .spin {
  1600. background: var(--success);
  1601. }
  1602. @keyframes pulse-dot {
  1603. 0%,
  1604. 100% {
  1605. opacity: 1;
  1606. transform: scale(1);
  1607. }
  1608. 50% {
  1609. opacity: 0.35;
  1610. transform: scale(0.8);
  1611. }
  1612. }
  1613. @media (prefers-reduced-motion: reduce) {
  1614. .task-line.busy .spin {
  1615. animation: none;
  1616. }
  1617. }
  1618. /*
  1619. 运行态小标签:圆点 + 文字(不用底色块,避免标题行三个彩色块打架)。
  1620. 只有「执行中」的圆点会呼吸,其余是静态灰/黄点。
  1621. */
  1622. .run-chip {
  1623. display: inline-flex;
  1624. align-items: center;
  1625. gap: 4px;
  1626. flex-shrink: 0;
  1627. font-size: var(--fs-11);
  1628. color: var(--muted);
  1629. white-space: nowrap;
  1630. }
  1631. .run-chip .dot {
  1632. width: 6px;
  1633. height: 6px;
  1634. border-radius: 50%;
  1635. background: var(--edge-strong);
  1636. }
  1637. .run-chip.tone-success .dot,
  1638. .run-chip.tone-info .dot {
  1639. background: var(--success);
  1640. }
  1641. .run-chip.tone-success,
  1642. .run-chip.tone-info {
  1643. color: var(--success);
  1644. }
  1645. .run-chip.tone-success .dot,
  1646. .run-chip.tone-info .dot {
  1647. animation: pulse-dot 1.4s var(--ease-standard) infinite;
  1648. } .run-chip.tone-warning {
  1649. color: var(--warning);
  1650. }
  1651. .run-chip.tone-danger {
  1652. color: var(--danger);
  1653. }
  1654. .run-chip.tone-danger .dot {
  1655. background: var(--danger);
  1656. }
  1657. .run-chip.tone-warning .dot {
  1658. background: var(--warning);
  1659. }
  1660. @media (prefers-reduced-motion: reduce) {
  1661. .run-chip .dot {
  1662. animation: none !important;
  1663. }
  1664. }
  1665. /* 最近动态 */
  1666. .pulse-list {
  1667. margin: 0;
  1668. padding: 0;
  1669. list-style: none;
  1670. display: grid;
  1671. gap: var(--sp-2);
  1672. }
  1673. .pulse-item {
  1674. display: flex;
  1675. align-items: center;
  1676. gap: var(--sp-2);
  1677. min-width: 0;
  1678. font-size: var(--fs-12);
  1679. color: var(--ink-2);
  1680. }
  1681. .pulse-item i {
  1682. width: 12px;
  1683. flex-shrink: 0;
  1684. color: var(--muted);
  1685. font-size: var(--fs-11);
  1686. }
  1687. .pulse-text {
  1688. flex: 1;
  1689. min-width: 0;
  1690. }
  1691. .pulse-item small {
  1692. flex-shrink: 0;
  1693. color: var(--muted);
  1694. font-size: var(--fs-11);
  1695. }
  1696. .tone-ink-success {
  1697. color: var(--success);
  1698. }
  1699. .tone-ink-warning {
  1700. color: var(--warning);
  1701. }
  1702. /* 右栏切换按钮的激活态 */
  1703. .chat-actions ui-button.on button {
  1704. background: var(--primary-subtle);
  1705. color: var(--primary);
  1706. }
  1707. .rail-item.task .task-ico {
  1708. background: var(--info-subtle);
  1709. color: var(--info);
  1710. }
  1711. .rail-item > i {
  1712. color: var(--muted);
  1713. flex-shrink: 0;
  1714. }
  1715. .loop-detail {
  1716. margin: 0 0 var(--sp-1);
  1717. font-size: var(--fs-12);
  1718. color: var(--ink-2);
  1719. }
  1720. .loop-hint {
  1721. margin: 0 0 var(--sp-2);
  1722. font-size: var(--fs-12);
  1723. color: var(--muted);
  1724. }
  1725. .banner-hold {
  1726. padding: var(--sp-3) var(--sp-4) 0;
  1727. }
  1728. /*
  1729. * 消息流(2026-10-29 对照 ChatGPT demo 重做):
  1730. * · **居中限宽 768px** —— 原实现撑满整列,宽屏下长段落一行 1200px+,
  1731. * 眼睛横跨屏幕读字极累;聊天类界面的阅读宽度上限是硬需求。
  1732. * · 间距 20px —— 原来 12px,相邻两块回复几乎粘在一起,分不清是两条消息。
  1733. * · 用 padding 让出左右留白,而不是给每个气泡加 margin(否则限宽容器跟着缩)。
  1734. */
  1735. .messages {
  1736. flex: 1;
  1737. min-height: 0;
  1738. /*
  1739. * 🔴 必须同时锁横向:只写 overflow-y:auto 时,x 轴默认是 visible →
  1740. * 任一子元素(长 URL、宽表格、文件卡片)撑出宽度就会冒出横向滚动条
  1741. * (用户实测:消息区底部出现一条横条)。
  1742. * 内容自身负责换行/裁剪(见 message-bubble 的 overflow-wrap)。
  1743. */
  1744. overflow-x: hidden;
  1745. overflow-y: auto;
  1746. overscroll-behavior: contain;
  1747. display: grid;
  1748. /* 消息间距 20 → 24(对齐参考实现的 ~28px 密度),条与条之间更容易分辨 */
  1749. gap: var(--sp-6);
  1750. align-content: start;
  1751. /* 上下留白加大:滚到底时最后一条不贴着输入框 */
  1752. padding: var(--sp-6) max(var(--sp-4), calc((100% - 768px) / 2)) var(--sp-8);
  1753. }
  1754. /* 顶部历史区 / 空态 / 骨架屏不吃限宽偏移,保持整列居中 */
  1755. .messages > .history-hold,
  1756. .messages > .center,
  1757. .messages > .skeleton-hold,
  1758. .messages > .welcome {
  1759. width: 100%;
  1760. max-width: 768px;
  1761. margin-inline: auto;
  1762. }
  1763. /* AC-04.4:历史加载区(顶部) */
  1764. .history-hold {
  1765. display: flex;
  1766. justify-content: center;
  1767. padding: var(--sp-1) 0;
  1768. }
  1769. .history-end {
  1770. font-size: var(--fs-12);
  1771. color: var(--muted);
  1772. }
  1773. /* ---------- 右栏标签条(四标签,一个标签一件事) ---------- */
  1774. .rail-tabs {
  1775. display: grid;
  1776. grid-template-columns: repeat(4, minmax(0, 1fr));
  1777. gap: 2px;
  1778. padding: var(--sp-2) var(--sp-2) 0;
  1779. border-bottom: 1px solid var(--edge);
  1780. flex-shrink: 0;
  1781. }
  1782. .rail-tab {
  1783. display: inline-flex;
  1784. align-items: center;
  1785. justify-content: center;
  1786. gap: var(--sp-1);
  1787. min-height: 36px;
  1788. padding: 0 var(--sp-1);
  1789. border: 0;
  1790. border-bottom: 2px solid transparent;
  1791. background: transparent;
  1792. font: inherit;
  1793. font-size: var(--fs-12);
  1794. color: var(--muted);
  1795. cursor: pointer;
  1796. white-space: nowrap;
  1797. }
  1798. .rail-tab:hover {
  1799. color: var(--ink-2);
  1800. background: var(--surface-hover);
  1801. }
  1802. .rail-tab.on {
  1803. color: var(--ink);
  1804. border-bottom-color: var(--primary);
  1805. font-weight: 600;
  1806. }
  1807. .rail-tab:focus-visible {
  1808. outline: 2px solid var(--primary);
  1809. outline-offset: -2px;
  1810. }
  1811. .rail-tab i {
  1812. font-size: var(--fs-11);
  1813. }
  1814. .rail-tab-count {
  1815. padding: 0 5px;
  1816. border-radius: 999px;
  1817. background: var(--surface-2);
  1818. font-size: var(--fs-11);
  1819. font-weight: 400;
  1820. color: var(--muted);
  1821. }
  1822. .rail-body {
  1823. flex: 1;
  1824. min-height: 0;
  1825. overflow-y: auto;
  1826. padding: var(--sp-4);
  1827. display: grid;
  1828. gap: var(--sp-5);
  1829. align-content: start;
  1830. }
  1831. .rail-block h3 {
  1832. display: flex;
  1833. align-items: center;
  1834. gap: var(--sp-2);
  1835. margin: 0 0 var(--sp-3);
  1836. font-size: var(--fs-13);
  1837. color: var(--ink-2);
  1838. }
  1839. .kv {
  1840. margin: 0;
  1841. display: grid;
  1842. gap: var(--sp-2);
  1843. }
  1844. .kv > div {
  1845. display: flex;
  1846. justify-content: space-between;
  1847. gap: var(--sp-3);
  1848. font-size: var(--fs-12);
  1849. }
  1850. .kv dt {
  1851. color: var(--muted);
  1852. flex-shrink: 0;
  1853. }
  1854. .kv dd {
  1855. margin: 0;
  1856. color: var(--ink);
  1857. text-align: right;
  1858. min-width: 0;
  1859. overflow-wrap: anywhere;
  1860. }
  1861. .rail-empty {
  1862. margin: 0;
  1863. font-size: var(--fs-12);
  1864. color: var(--muted);
  1865. }
  1866. .rail-item {
  1867. display: flex;
  1868. align-items: center;
  1869. gap: var(--sp-3);
  1870. width: 100%;
  1871. min-height: var(--touch-min);
  1872. padding: var(--sp-2);
  1873. margin-bottom: var(--sp-2);
  1874. border: 1px solid var(--edge);
  1875. border-radius: var(--r-md);
  1876. background: var(--surface);
  1877. text-align: left;
  1878. cursor: pointer;
  1879. }
  1880. .rail-item:hover {
  1881. background: var(--surface-hover);
  1882. }
  1883. .file-icon {
  1884. display: grid;
  1885. place-items: center;
  1886. width: 32px;
  1887. height: 32px;
  1888. flex-shrink: 0;
  1889. border-radius: var(--r-sm);
  1890. background: var(--info-subtle);
  1891. color: var(--info);
  1892. }
  1893. .rail-meta {
  1894. display: grid;
  1895. gap: 2px;
  1896. min-width: 0;
  1897. flex: 1;
  1898. }
  1899. .sync-hint {
  1900. display: inline-flex;
  1901. align-items: center;
  1902. gap: var(--sp-1);
  1903. color: var(--muted);
  1904. }
  1905. .rail-meta strong {
  1906. font-size: var(--fs-13);
  1907. color: var(--ink);
  1908. }
  1909. .rail-meta small {
  1910. font-size: var(--fs-11);
  1911. color: var(--muted);
  1912. }
  1913. .ellipsis {
  1914. overflow: hidden;
  1915. text-overflow: ellipsis;
  1916. white-space: nowrap;
  1917. }
  1918. `,
  1919. ],
  1920. })
  1921. export class DkChatPage {
  1922. readonly chat = inject(ChatService);
  1923. readonly live = inject(LiveService);
  1924. /** 重连时重建常驻 Matrix 连接(登出/断线后需要) */
  1925. private readonly matrixSession = inject(MatrixSessionService);
  1926. private readonly unread = inject(UnreadService);
  1927. private readonly deliverables = inject(DeliverablesService);
  1928. private readonly tasks = inject(TasksService);
  1929. private readonly pulse = inject(TaskPulseService);
  1930. private readonly artifactSync = inject(ArtifactSyncService);
  1931. readonly loop = inject(LoopGuardService);
  1932. /** 多会话(线程)—— 左栏会话列表的唯一数据源 */
  1933. readonly sessions = inject(ChatSessionService);
  1934. private readonly workspaceSvc = inject(WorkspaceService);
  1935. private readonly agentModal = inject(AgentModalService);
  1936. private readonly route = inject(ActivatedRoute);
  1937. private readonly router = inject(Router);
  1938. private readonly device = inject(DeviceService);
  1939. readonly keyword = signal('');
  1940. /**
  1941. * 新建对话时选定的工作区(D3 模态框)。
  1942. * `undefined` = 未打开;`null` = 日常对话;`string` = 工作区 id。
  1943. */
  1944. readonly newSessionWorkspace = signal<string | null | undefined>(undefined);
  1945. /** 「新建对话」模态框(左栏按钮与空态引导共用) */
  1946. private readonly newSessionModal = inject(NewSessionModalService);
  1947. /** 空态里点「新建对话」→ 打开容器选择框 */
  1948. requestNewSession(): void {
  1949. this.newSessionModal.open(null);
  1950. }
  1951. readonly railOpen = signal(true);
  1952. /** 右栏内联预览的成果 */
  1953. readonly previewing = signal<Deliverable | null>(null);
  1954. /** 右栏当前面板:任务 / 成果 */
  1955. /**
  1956. * 右栏当前标签。
  1957. *
  1958. * 四个标签各管一件事(参考 DSH 右栏):
  1959. * 任务 = 该容器的任务 / 文件 = 本会话产出的文件 /
  1960. * 成果 = 平台上报的交付成果 / 状态 = 运行状态与最近动态
  1961. *
  1962. * 默认「文件」—— 用户最关心「这次对话产出了什么」。
  1963. */
  1964. readonly railTab = signal<RailTab>(readRailTabPref());
  1965. /** 标签定义(模板遍历用,避免在模板里重复写四份) */
  1966. readonly railTabs: { key: RailTab; label: string; icon: string; hint: string }[] = [
  1967. { key: 'tasks', label: '任务', icon: 'fa-list-check', hint: '该容器名下的任务' },
  1968. { key: 'files', label: '文件', icon: 'fa-paperclip', hint: '本次对话产出的文件' },
  1969. { key: 'results', label: '成果', icon: 'fa-box', hint: '平台上报的交付成果' },
  1970. { key: 'status', label: '状态', icon: 'fa-gauge-high', hint: '运行指标与最近动态' },
  1971. ];
  1972. /** 当前标签的元信息(标题行用) */
  1973. readonly railCurrent = computed(
  1974. () => this.railTabs.find((t) => t.key === this.railTab()) ?? this.railTabs[0],
  1975. );
  1976. // ---- 右栏宽度(拖宽 + 本地记忆) ----
  1977. private static readonly RAIL_W_KEY = 'fmode_rail_w';
  1978. private static readonly RAIL_W_MIN = 280;
  1979. private static readonly RAIL_W_MAX = 560;
  1980. private static readonly RAIL_W_DEFAULT = 320;
  1981. private readonly _railWidth = signal(this.readRailWidth());
  1982. readonly railWidth = this._railWidth.asReadonly();
  1983. private resizeFrom = 0;
  1984. private resizeStartX = 0;
  1985. private readonly onResizeMove = (e: PointerEvent) => {
  1986. /* 向左拖 = 变宽:宽度增长量是「起点 X - 当前 X」 */
  1987. const raw = this.resizeFrom + (this.resizeStartX - e.clientX);
  1988. /* 预览形态下放宽上限;非预览形态回到 560 */
  1989. const next = this.previewFile() ? clampPreviewRailWidth(raw) : clampRailWidth(raw);
  1990. this._railWidth.set(next);
  1991. };
  1992. private readonly onResizeEnd = () => {
  1993. document.removeEventListener('pointermove', this.onResizeMove);
  1994. document.removeEventListener('pointerup', this.onResizeEnd);
  1995. document.body.classList.remove('is-resizing-rail');
  1996. try {
  1997. localStorage.setItem(DkChatPage.RAIL_W_KEY, String(this._railWidth()));
  1998. } catch {
  1999. /* 隐私模式:只保留内存态 */
  2000. }
  2001. };
  2002. startResize(e: PointerEvent): void {
  2003. e.preventDefault();
  2004. this.resizeFrom = this._railWidth();
  2005. this.resizeStartX = e.clientX;
  2006. document.addEventListener('pointermove', this.onResizeMove);
  2007. document.addEventListener('pointerup', this.onResizeEnd);
  2008. /* 拖动期间禁掉文本选中,否则整页文字会被刷蓝 */
  2009. document.body.classList.add('is-resizing-rail');
  2010. }
  2011. resetRailWidth(): void {
  2012. this._railWidth.set(DkChatPage.RAIL_W_DEFAULT);
  2013. try {
  2014. localStorage.setItem(DkChatPage.RAIL_W_KEY, String(DkChatPage.RAIL_W_DEFAULT));
  2015. } catch {
  2016. /* 忽略 */
  2017. }
  2018. }
  2019. private readRailWidth(): number {
  2020. try {
  2021. const v = Number(localStorage.getItem(DkChatPage.RAIL_W_KEY));
  2022. if (Number.isFinite(v) && v > 0) return clampRailWidth(v);
  2023. } catch {
  2024. /* 忽略 */
  2025. }
  2026. return DkChatPage.RAIL_W_DEFAULT;
  2027. }
  2028. /** 左栏:某 agent 的会话列表是否展开(收起时只显示容器行) */
  2029. readonly expandedAgents = signal<Set<string>>(new Set());
  2030. /** 左栏:哪些容器的「已归档」目录是展开的 */
  2031. readonly archivedOpen = signal<Set<string>>(new Set());
  2032. /** 待确认移除的会话(null = 无) */
  2033. readonly removing = signal<ChatSession | null>(null);
  2034. /** 当前数字生命的任务(右栏任务面板用) */
  2035. readonly agentTasks = computed(() => {
  2036. const id = this.chat.agentId();
  2037. return id ? this.tasks.all().filter((t) => t.agentId === id) : [];
  2038. });
  2039. readonly pct = (v?: number) => fmtPercent(v);
  2040. readonly tokens = (a: { usage?: { totalTokens?: number } }) => fmtTokens(usageOf(a as never).totalTokens);
  2041. /**
  2042. * 「当前任务」文案 —— **唯一实现**在 `TaskPulseService.describe()`。
  2043. * 列表行、头部任务条、右栏运行状态**三处共用同一份**,保证左右同步。
  2044. */
  2045. taskLine(agentId: string): TaskLine {
  2046. return this.pulse.describe(this.live.agentById(agentId));
  2047. }
  2048. /** 列表行紧凑标签:一次只答一个问题 —— 在不在干活 */
  2049. runLabel(agentId: string): string {
  2050. const line = this.taskLine(agentId);
  2051. if (line.tone === 'danger') return '异常';
  2052. if (line.active) return '执行中';
  2053. if (line.tone === 'warning') return '未同步';
  2054. return '空闲';
  2055. }
  2056. /**
  2057. * 列表第二行文案 —— **不与标题行的运行态重复**。
  2058. *
  2059. * 有具体任务时显示任务;否则显示专长(更适合"没在忙"的时候告知用户它是谁)。
  2060. */
  2061. convSubline(agentId: string, specialty?: string): string {
  2062. const line = this.taskLine(agentId);
  2063. // 状态词(空闲/执行中/未同步/异常)已经在标题行,这里不再重复
  2064. if (!line.active && (line.tone === 'neutral' || line.tone === 'danger')) {
  2065. return specialty || '数字生命';
  2066. }
  2067. return line.text || specialty || '数字生命';
  2068. }
  2069. /** 本会话最近动态 */
  2070. readonly pulses = computed(() => this.pulse.pulsesFor(this.chat.agentId()));
  2071. /**
  2072. * 消息 → 渲染行:**连续的终端活动消息合并成一个折叠组**。
  2073. *
  2074. * 容器把 Hermes 的每一步(`💻 terminal` + 命令、`✍️ Writing ...`)
  2075. * 都作为独立消息发出来,一条回复能产生十几条 —— 会话被淹没。
  2076. *
  2077. * 规则:相邻且都属于终端活动的消息合成一组;遇到正常对话立即断开。
  2078. */
  2079. readonly messageRows = computed<MessageRow[]>(() => {
  2080. const rows: MessageRow[] = [];
  2081. /*
  2082. * 🔴 工具组的 key 必须用**组内第一条消息的 id**,不能用 rows.length。
  2083. *
  2084. * 原实现 `tool-${rows.length}-${summary}`:流式追加消息时 rows.length 会变,
  2085. * 导致同一个工具组在相邻两次渲染里 key 不同 → Angular 销毁重建整个组,
  2086. * 表现为「内容一下出现、一下消失」(实测复现:气泡数 2→1→2)。
  2087. */
  2088. let group: { text: string; summary: string; id: string }[] = [];
  2089. const flush = (): void => {
  2090. if (group.length === 0) return;
  2091. const first = group[0];
  2092. rows.push({
  2093. kind: 'tool',
  2094. key: `tool-${first.id}`,
  2095. block: {
  2096. kind: 'process',
  2097. steps: group.map((g) => ({ kind: 'tool' as const, label: g.summary, detail: g.text })),
  2098. },
  2099. });
  2100. group = [];
  2101. };
  2102. for (const m of this.chat.messages()) {
  2103. // 空消息(撤回 / 表情回应 / 空 body)不渲染 —— 实测占 7/12
  2104. if (!m.text?.trim() && !m.file) continue;
  2105. if (m.sender === 'agent' && isToolActivityMessage(m.text)) {
  2106. group.push({ text: m.text!.slice(0, 400), summary: toolActivitySummary(m.text!), id: m.id });
  2107. continue;
  2108. }
  2109. flush();
  2110. rows.push({ kind: 'msg', key: m.id, message: m });
  2111. }
  2112. flush();
  2113. return rows;
  2114. });
  2115. /** 某个成果里「还在容器里、未上云盘」的首个附件(没有则 undefined) */
  2116. pendingOf(d: Deliverable): { name: string; source: string } | undefined {
  2117. const a = d.artifacts.find((x) => !x.url && x.source);
  2118. return a ? { name: a.name, source: a.source! } : undefined;
  2119. }
  2120. /** 附件同步失败原因(UI 如实展示,不假装正在同步) */
  2121. syncError(deliverableId: string, a: { source: string }): string | undefined {
  2122. return this.artifactSync.errorOf(deliverableId, a.source);
  2123. }
  2124. /** 动态里的时刻(本地毫秒 → HH:MM) */
  2125. clock(ms: number): string {
  2126. return fmtTime(ms);
  2127. }
  2128. private readonly messagesEl = viewChild<ElementRef<HTMLElement>>('messages');
  2129. /** 会话列表:仅已接入的数字生命 */
  2130. readonly conversations = computed(() =>
  2131. this.live
  2132. .agents()
  2133. .filter((a) => a.matrixReady)
  2134. .map((a) => ({
  2135. id: a.id,
  2136. name: a.name,
  2137. specialty: a.specialty,
  2138. currentTask: a.currentTask,
  2139. unread: a.unread,
  2140. }))
  2141. /*
  2142. * 🔴 **固定顺序**(用户实测反馈:选中项位置会乱跳)。
  2143. * 曾按未读数排序 → 未读一变整列重排,用户正在看的会话会跳位。
  2144. * 改为按 name 稳定排序(同 name 再按 id),顺序**不随运行期数据变化**。
  2145. */
  2146. .sort((x, y) => x.name.localeCompare(y.name, 'zh') || x.id.localeCompare(y.id)),
  2147. );
  2148. /** 待预览的消息附件(null = 关闭) */
  2149. readonly previewFile = signal<{ url: string; name: string; mimeType: string } | null>(null);
  2150. /**
  2151. * 上一次「最后一条消息」的 id。
  2152. *
  2153. * 用于区分「尾部新增」(要滚底)与「头部插入」(分页,保持锚点)。
  2154. */
  2155. private lastTailId = '';
  2156. /** 用户是否主动上翻离开了底部(true 时新消息不强行拉回) */
  2157. private userScrolledAway = false;
  2158. /** 正在加载更早历史:期间禁止任何「贴底」动作,否则锚点会被破坏 */
  2159. private loadingEarlier = false;
  2160. /**
  2161. * 打开消息附件预览(按类型分发由 app-file-preview 负责)。
  2162. *
  2163. * 🔴 必须同时**打开右栏**:预览已经改成右栏内联,
  2164. * 若用户之前收起了右栏,只设 previewFile 是看不到任何反应的(实测 bug)。
  2165. */
  2166. openFile(f: { url: string; name: string; mimeType: string; size?: number }): void {
  2167. const kind = previewKindOf(f.name || f.url, f.mimeType);
  2168. /*
  2169. * 图片 / 音视频不进右栏(用户裁决 2026-10-30):
  2170. * - 图片 → 深色灯箱,占满视口
  2171. * - 音视频 → 居中模态,原生控件自带全屏
  2172. * 其余类型 → 右栏内联预览(可切全屏)。
  2173. */
  2174. if (kind === 'image') {
  2175. this.lightboxFile.set({ url: f.url, name: f.name, size: f.size ?? 0 });
  2176. return;
  2177. }
  2178. if (kind === 'video' || kind === 'audio') {
  2179. this.mediaFile.set({ url: f.url, name: f.name, kind });
  2180. return;
  2181. }
  2182. this.previewFile.set(f);
  2183. this.railOpen.set(true);
  2184. }
  2185. /* ---- 图片灯箱 / 音视频模态(2026-10-30)---- */
  2186. readonly lightboxFile = signal<{ url: string; name: string; size: number } | null>(null);
  2187. /** 待发送图片点缩略图 → 用同一个灯箱预览(2026-10-31) */
  2188. previewPending(pf: { file: File; previewUrl?: string }): void {
  2189. const url = pf.previewUrl;
  2190. if (!url) return;
  2191. this.lightboxFile.set({ url, name: pf.file.name, size: pf.file.size });
  2192. }
  2193. readonly mediaFile = signal<{ url: string; name: string; kind: PreviewKind } | null>(null);
  2194. mediaKind(m: { kind: PreviewKind }): 'video' | 'audio' {
  2195. return m.kind === 'video' ? 'video' : 'audio';
  2196. }
  2197. onMediaOpenChange(open: boolean): void {
  2198. if (!open) this.mediaFile.set(null);
  2199. }
  2200. /**
  2201. * 预览形态下的右栏显示宽度。
  2202. * 拖拽时已按预览上限夹取;这里再兜底一次,防止旧记忆值超出。
  2203. */
  2204. readonly effectiveRailWidth = computed(() =>
  2205. this.previewFile() ? clampPreviewRailWidth(this.railWidth()) : this.railWidth(),
  2206. );
  2207. /** 真实未读数(UnreadService 唯一来源;不用 Agent.unread 的占位值) */
  2208. unreadOf(agentId: string): number {
  2209. return this.unread.countOf(agentId);
  2210. }
  2211. /** 可对话状态(判定唯一实现在 models.agentPresence) */
  2212. containerState(agentId: string): AgentPresence {
  2213. return agentPresence(this.live.agentById(agentId));
  2214. }
  2215. /** 当前打开会话的容器状态(用于区分「容器离线」与「自己在连接中」) */
  2216. readonly currentContainerState = computed<AgentPresence>(() => {
  2217. const id = this.chat.agentId();
  2218. return id ? agentPresence(this.live.agentById(id)) : 'offline';
  2219. });
  2220. readonly filteredConversations = computed(() => {
  2221. const q = this.keyword().trim().toLowerCase();
  2222. if (!q) return this.conversations();
  2223. return this.conversations().filter(
  2224. (c) => c.name.toLowerCase().includes(q) || (c.specialty ?? '').toLowerCase().includes(q),
  2225. );
  2226. });
  2227. /** 本会话里的全部文件(按出现顺序,同 url+name 去重) */
  2228. readonly sessionFiles = computed(() => {
  2229. const seen = new Set<string>();
  2230. const out: { name: string; url: string; mimeType: string; size: number }[] = [];
  2231. for (const m of this.chat.messages()) {
  2232. const f = m.file;
  2233. if (!f || !f.url) continue;
  2234. const key = f.url + f.name;
  2235. if (seen.has(key)) continue;
  2236. seen.add(key);
  2237. out.push({ name: f.name, url: f.url, mimeType: f.mimeType, size: f.size });
  2238. }
  2239. return out;
  2240. });
  2241. /** 按 mimeType 选图标(原生附件没有 fileType 字段) */
  2242. fileIconByMime(mime: string): string {
  2243. if (mime.startsWith('image/')) return 'fa-file-image';
  2244. if (mime.includes('html')) return 'fa-file-code';
  2245. if (mime.includes('pdf')) return 'fa-file-pdf';
  2246. if (mime.includes('word') || mime.includes('officedocument.word')) return 'fa-file-word';
  2247. if (mime.includes('sheet') || mime.includes('excel')) return 'fa-file-excel';
  2248. if (mime.includes('zip')) return 'fa-file-zipper';
  2249. return 'fa-file-lines';
  2250. }
  2251. /** 字节数 → 人类可读 */
  2252. fmtSize(bytes: number): string {
  2253. if (!bytes) return '';
  2254. if (bytes < 1024) return `${bytes} B`;
  2255. if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
  2256. return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
  2257. }
  2258. readonly agentDeliverables = computed(() => {
  2259. const id = this.chat.agentId();
  2260. return id ? this.deliverables.byAgent(id) : [];
  2261. });
  2262. constructor() {
  2263. /*
  2264. * AC-04.2:新消息到达后滚动到底。
  2265. *
  2266. * 🔴 **只在「尾部新增了消息」时滚底**(用户实测反馈:上翻历史时也被拉到底)。
  2267. * 上翻历史走的是 `paginate()` → `messages` 变长但**尾部未变** →
  2268. * 此处按「最后一条消息 id 是否变化」判定,分页时保持锚点不动。
  2269. */
  2270. effect(() => {
  2271. const list = this.chat.messages();
  2272. const tailId = list.length ? list[list.length - 1].id : '';
  2273. const prev = this.lastTailId;
  2274. this.lastTailId = tailId;
  2275. // 首次(prev 为空)或尾部确实变了 → 滚底
  2276. if (prev === '' || tailId !== prev) {
  2277. this.stickToBottom();
  2278. }
  2279. });
  2280. /*
  2281. * 正在输入指示器出现时贴底(2026-11-01:已移入消息流末尾)。
  2282. *
  2283. * 🔴 它现在是消息流的最后一行,出现会让流的高度增加一截。
  2284. * 用 rAF + 再等一帧:effect 执行时 DOM 还没插入,
  2285. * 立即 scrollToBottom 会把「指示器还没占位」的高度当作终点,
  2286. * 结果它出现在视口下方看不见(截图实测)。
  2287. */
  2288. effect(() => {
  2289. if (!this.chat.peerTyping()) return;
  2290. requestAnimationFrame(() => requestAnimationFrame(() => this.stickToBottom()));
  2291. });
  2292. // 窄窗默认收起右栏(<1150)
  2293. if (typeof window !== 'undefined' && window.innerWidth < 1150) {
  2294. this.railOpen.set(false);
  2295. }
  2296. // 🔴 监听**路由参数流**而非 snapshot —— 同一组件在 chat/:id 间复用时
  2297. // 构造函数不会重跑,只读一次 snapshot 会导致「切换会话无反应」。
  2298. this.route.paramMap.subscribe((params) => {
  2299. void this.onRouteChange(params.get('id'), params.get('sessionId'));
  2300. });
  2301. /*
  2302. * 成果附件同步:读到成果列表后,把「只有容器路径、没有云盘地址」的附件登记去同步。
  2303. * 幂等(服务内部去重),每次列表刷新都可以安全调用。
  2304. */
  2305. effect(() => {
  2306. const list = this.deliverables.all();
  2307. if (list.length === 0) return;
  2308. void this.artifactSync.syncPending(list);
  2309. });
  2310. /*
  2311. * 待建态建出会话后,把 URL 从 /chat/:id/new 换成真实会话地址(2026-11-01)。
  2312. *
  2313. * 🔴 不换的话 URL 一直停在 `new`:刷新页面会重新进入待建态,
  2314. * 用户看到刚发的会话「不见了」;分享/回退也会回到空白新建页。
  2315. * 这里用 replaceUrl,不产生历史记录堆积。
  2316. */
  2317. effect(() => {
  2318. const sid = this.chat.sessionId();
  2319. const aid = this.chat.agentId();
  2320. if (!sid || !aid) return;
  2321. // 只在当前路由仍是待建态的 `new` 时替换,正常切会话不干预
  2322. if (!this.router.url.includes(`/chat/${aid}/new`)) return;
  2323. void this.router.navigateByUrl(
  2324. `/${this.device.mode()}/chat/${aid}/${encodeURIComponent(sid)}`,
  2325. { replaceUrl: true },
  2326. );
  2327. });
  2328. }
  2329. private async onRouteChange(id: string | null, sessionId: string | null): Promise<void> {
  2330. this.sessions.load();
  2331. await Promise.all([
  2332. this.live.ensureLoaded(),
  2333. this.deliverables.ensureLoaded(),
  2334. // 右栏任务面板依赖全量任务(按 agentId 过滤)
  2335. this.tasks.ensureAllLoaded(),
  2336. ]);
  2337. if (id) {
  2338. /*
  2339. * `sessionId === 'new'` = 「新建对话」的待建地址(不落任何数据)。
  2340. * 2026-11-01:新建对话不再预建空会话,而是打开一个空白待建态,
  2341. * 用户发出第一条消息时才真正创建会话。
  2342. */
  2343. if (sessionId === 'new') {
  2344. const ws = this.route.snapshot.queryParamMap.get('ws');
  2345. await this.chat.startBlank(id, ws || null);
  2346. } else {
  2347. await this.chat.open(id, sessionId ?? undefined);
  2348. }
  2349. // 打开后内容不满一屏 → 自动补历史(懒加载的另一半)
  2350. void this.fillViewportIfNeeded();
  2351. } else {
  2352. const first = this.conversations()[0];
  2353. if (first) void this.openConversation(first.id, true);
  2354. }
  2355. }
  2356. openConversation(id: string, replace = false): void {
  2357. void this.router.navigateByUrl(`/${this.device.mode()}/chat/${id}`, { replaceUrl: replace });
  2358. }
  2359. // ---------- 多会话(左栏两栏式:容器 → 会话) ----------
  2360. /**
  2361. * 左栏分组数据:每个可对话的容器 + 它下面的会话。
  2362. *
  2363. * 排序与 `conversations()` 一致(按名字稳定排序),
  2364. * **不随消息变动重排** —— 用户正在看的行不会跳位。
  2365. *
  2366. * 搜索规则:匹配容器名 → 显示该容器下全部会话;只匹配会话名 → 只显示命中的会话。
  2367. */
  2368. readonly groups = computed<
  2369. { agent: ConversationRow; sessions: ChatSession[]; archived: ChatSession[] }[]
  2370. >(() => {
  2371. const map = this.sessions.byAgent();
  2372. const kw = this.keyword().trim().toLowerCase();
  2373. const out: { agent: ConversationRow; sessions: ChatSession[]; archived: ChatSession[] }[] = [];
  2374. for (const c of this.filteredConversations()) {
  2375. /*
  2376. * 🔴 活跃会话与已归档会话**分开两组渲染**,不是「归档就消失」。
  2377. *
  2378. * 用户要求:归档应当收进**独立目录**,而不是从界面上抹掉 ——
  2379. * 抹掉会让用户以为对话被删了,也找不回来。
  2380. */
  2381. let list = (map.get(c.id) ?? []).filter((s) => !s.archived);
  2382. let archived = (map.get(c.id) ?? []).filter((s) => s.archived);
  2383. if (kw) {
  2384. const selfMatch =
  2385. c.name.toLowerCase().includes(kw) || (c.specialty ?? '').toLowerCase().includes(kw);
  2386. if (!selfMatch) {
  2387. list = list.filter((s) => s.title.toLowerCase().includes(kw));
  2388. archived = archived.filter((s) => s.title.toLowerCase().includes(kw));
  2389. if (list.length === 0 && archived.length === 0) continue;
  2390. }
  2391. }
  2392. out.push({ agent: c, sessions: list, archived });
  2393. }
  2394. return out;
  2395. });
  2396. /** 容器是否展开(当前打开的容器默认展开) */
  2397. isExpanded(agentId: string): boolean {
  2398. return this.expandedAgents().has(agentId) || this.chat.agentId() === agentId;
  2399. }
  2400. /**
  2401. * 展开 / 收起某容器的会话列表。
  2402. *
  2403. * 🔴 **没打开过的容器,首次点击要顺带打开它**。
  2404. * 否则:点开 lj-test 只展开了一个空列表(它的会话要「进过这个容器」
  2405. * 才会被扫描出来),用户以为这个容器没有对话(实测踩坑)。
  2406. *
  2407. * `openConversation` 会触发 `chat.open()` → 扫描 Matrix 线程 →
  2408. * 左栏立刻填上该容器已有的对话。用户不需要多点一次。
  2409. */
  2410. toggleExpand(agentId: string): void {
  2411. const wasExpanded = this.isExpanded(agentId);
  2412. const hasSessions = (this.sessions.byAgent().get(agentId) ?? []).length > 0;
  2413. this.expandedAgents.update((set) => {
  2414. const next = new Set(set);
  2415. if (set.has(agentId)) next.delete(agentId);
  2416. else next.add(agentId);
  2417. return next;
  2418. });
  2419. // 从未打开过、且左栏没有它的会话 → 点一次就直接进去
  2420. if (!wasExpanded && !hasSessions && this.chat.agentId() !== agentId) {
  2421. this.openConversation(agentId);
  2422. }
  2423. }
  2424. /** 打开指定会话 */
  2425. openSession(agentId: string, sessionId: string): void {
  2426. void this.router.navigateByUrl(
  2427. `/${this.device.mode()}/chat/${agentId}/${encodeURIComponent(sessionId)}`,
  2428. );
  2429. }
  2430. /**
  2431. * 打开容器设置 —— 用**全局模态框**而不是跳整页(2026-10-30 用户反馈)。
  2432. * 跳页会丢掉当前会话的滚动位置与上下文,关掉弹层就回到原处。
  2433. */
  2434. openAgentSettings(agentId: string): void {
  2435. this.agentModal.open(agentId);
  2436. }
  2437. /**
  2438. * 空会话欢迎区:按小时问候 + 4 条起点建议。
  2439. *
  2440. * 建议只「填入输入框」不直接发送 —— 用户通常要改成自己的口径再发,
  2441. * 直接发出去反而要撤回。文案取自真实使用场景(交付文件 / 查状态 / 排查 / 同步进度)。
  2442. */
  2443. readonly suggests: { icon: string; title: string; text: string }[] = [
  2444. { icon: 'fa-code', title: '做个单页', text: '把这周的数据整理成一个可以直接打开的 HTML 单页' },
  2445. { icon: 'fa-magnifying-glass', title: '看看在忙什么', text: '你现在在做什么?列出正在推进的任务和卡住的地方' },
  2446. { icon: 'fa-wrench', title: '排查问题', text: '刚才那次运行失败了,帮我看下日志里是什么原因' },
  2447. { icon: 'fa-list-check', title: '同步进度', text: '把今天做完的内容整理成一份进度汇报' },
  2448. ];
  2449. readonly greeting = computed(() => {
  2450. const h = new Date().getHours();
  2451. if (h < 6) return '凌晨好';
  2452. if (h < 12) return '上午好';
  2453. if (h < 14) return '中午好';
  2454. if (h < 18) return '下午好';
  2455. return '晚上好';
  2456. });
  2457. /** 点建议 → 填入输入框(不直接发送) */
  2458. useSuggest(text: string): void {
  2459. this.chat.setDraft(text);
  2460. }
  2461. /**
  2462. * 会话树发出「新建对话」请求 → 打开选容器模态框(D3)。
  2463. *
  2464. * 工作区归属本轮不做落库:`workspaceId` 只用于模态框里的提示文案,
  2465. * 真正写入 `ChatSession` 在第 3 轮(见计划文档)。
  2466. */
  2467. onNewSessionRequest(req: NewSessionRequest): void {
  2468. this.newSessionWorkspace.set(req.workspaceId);
  2469. }
  2470. /** 模态框里可选容器(与左栏 old 列表同源:已接入 Matrix 的数字生命) */
  2471. readonly pickableAgents = computed(() => this.conversations());
  2472. /**
  2473. * 工作区名称(模态框提示用)。
  2474. * 第 1 轮工作区由会话树组件持有内存态,页面拿不到 —— 先给中性文案,
  2475. * 第 3 轮工作区下沉到 service 后改读 service。
  2476. */
  2477. workspaceName(wsId: string): string {
  2478. if (!wsId) return '日常对话';
  2479. return this.workspaceSvc.byId(wsId)?.name ?? '日常对话';
  2480. }
  2481. /** 选中容器 → 在该容器下新建对话(复用原有去重规则) */
  2482. createInContainer(agentId: string): void {
  2483. const targetWs = this.newSessionWorkspace();
  2484. this.newSessionWorkspace.set(undefined);
  2485. this.newSession(agentId, undefined, targetWs ?? null);
  2486. }
  2487. /**
  2488. * 在指定容器下新建对话并立即打开。
  2489. *
  2490. * @param workspaceId 归属工作区(`null` = 日常对话)。只在从模态框选出容器时传入;
  2491. * 标题栏的「+」不传,沿用日常对话。
  2492. */
  2493. newSession(agentId: string, event?: Event, workspaceId: string | null = null): void {
  2494. event?.stopPropagation();
  2495. /*
  2496. * 「新建对话」**不创建任何数据**,只是把界面切到该容器的空白待建态
  2497. * (2026-11-01 用户裁决:首次发消息才建会话)。
  2498. *
  2499. * 这样点几次「+」就留几条空壳的问题从根上消失 —— 旧实现要么建空会话、
  2500. * 要么得靠 `pruneEmpty` 事后清理,两者都会出错。
  2501. * 工作区归属通过 URL 传过去,由聊天页在会话建出来那一刻落地。
  2502. */
  2503. this.expandedAgents.update((set) => new Set(set).add(agentId));
  2504. const qs = workspaceId ? `?ws=${encodeURIComponent(workspaceId)}` : '';
  2505. void this.router.navigateByUrl(`/${this.device.mode()}/chat/${agentId}/new${qs}`);
  2506. }
  2507. /** 点删除 → 先弹确认(删除类操作必须二次确认) */
  2508. confirmRemove(s: ChatSession, event?: Event): void {
  2509. event?.stopPropagation();
  2510. this.removing.set(s);
  2511. }
  2512. /** 确认移除(只移除列表索引,Matrix 里的消息不动) */
  2513. doRemove(): void {
  2514. const s = this.removing();
  2515. if (!s) return;
  2516. this.sessions.remove(s.id);
  2517. this.removing.set(null);
  2518. /*
  2519. * 移除的是当前打开的那条 → 回落到该容器的下一条,避免停在空白页。
  2520. */
  2521. if (this.chat.sessionId() === s.id) {
  2522. const next = this.sessions.listFor(s.agentId)[0];
  2523. if (next) this.openSession(s.agentId, next.id);
  2524. }
  2525. }
  2526. /** 归档 / 取消归档(收进「已归档」目录,不删除、不隐藏) */
  2527. toggleArchive(s: ChatSession, event?: Event): void {
  2528. event?.stopPropagation();
  2529. const next = !s.archived;
  2530. this.sessions.archive(s.id, next);
  2531. /*
  2532. * 归档后自动展开该容器的「已归档」目录 ——
  2533. * 让用户看到对话**去了哪里**,而不是以为它消失了。
  2534. */
  2535. if (next) {
  2536. const aid = s.agentId;
  2537. this.archivedOpen.update((set) => new Set(set).add(aid));
  2538. }
  2539. }
  2540. /** 展开 / 收起某容器的「已归档」目录 */
  2541. toggleArchivedGroup(agentId: string, event?: Event): void {
  2542. event?.stopPropagation();
  2543. this.archivedOpen.update((set) => {
  2544. const next = new Set(set);
  2545. if (next.has(agentId)) next.delete(agentId);
  2546. else next.add(agentId);
  2547. return next;
  2548. });
  2549. }
  2550. /** 会话行的时间显示(今天显示时分,更早显示日期) */
  2551. sessionTime(ts: number): string {
  2552. if (!ts) return '';
  2553. const d = new Date(ts);
  2554. const now = new Date();
  2555. const sameDay =
  2556. d.getFullYear() === now.getFullYear() &&
  2557. d.getMonth() === now.getMonth() &&
  2558. d.getDate() === now.getDate();
  2559. if (sameDay) {
  2560. return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`;
  2561. }
  2562. const days = Math.floor((now.getTime() - d.getTime()) / 86400000);
  2563. if (days === 1) return '昨天';
  2564. if (days < 7) return `${days}天`;
  2565. return `${d.getMonth() + 1}-${d.getDate()}`;
  2566. }
  2567. async reload(): Promise<void> {
  2568. await this.live.refresh();
  2569. }
  2570. /**
  2571. * 重连 / 重新加载。
  2572. *
  2573. * 🔴 必须走 `force`:`chat.open()` 在「同一 agent + 同一会话 + state=ready」时
  2574. * 会短路返回,而断线时 state 仍是 ready —— 不 force 的话按了完全没有反应
  2575. * (用户实测:「点击这个重新连接没有任何效果」)。
  2576. */
  2577. reconnect(): void {
  2578. const id = this.chat.agentId();
  2579. if (id) void this.chat.open(id, this.chat.sessionId() || undefined, true);
  2580. // Matrix 连接也可能已经断开,顺带重建一次(幂等,已连上时直接返回)
  2581. void this.matrixSession.kick();
  2582. }
  2583. retryOpen(): void {
  2584. const id = this.chat.agentId();
  2585. if (id) void this.chat.open(id, this.chat.sessionId() || undefined, true);
  2586. }
  2587. /** 切换右栏面板:同面板再点则隐藏 */
  2588. toggleRail(tab: RailTab): void {
  2589. if (this.railOpen() && this.railTab() === tab) {
  2590. this.railOpen.set(false);
  2591. return;
  2592. }
  2593. this.railTab.set(tab);
  2594. try {
  2595. localStorage.setItem('fmode_rail_tab', tab);
  2596. } catch {
  2597. /* 隐私模式 */
  2598. }
  2599. this.railOpen.set(true);
  2600. }
  2601. /** 任务状态中文(右栏用) */
  2602. statusText(status: string): string {
  2603. return TASK_STATUS_TEXT[status as keyof typeof TASK_STATUS_TEXT] ?? status;
  2604. }
  2605. /** 消息里的「查看任务」→ 打开右栏任务面板 */
  2606. openTaskRail(): void {
  2607. this.railTab.set('tasks');
  2608. this.railOpen.set(true);
  2609. }
  2610. goTasks(): void {
  2611. const id = this.chat.agentId();
  2612. if (id) void this.router.navigate(['/', this.device.mode(), 'tasks', id]);
  2613. }
  2614. /**
  2615. * 成果预览(2026-10-31 用户要求):改为**右栏内联**,与消息附件共用同一个预览面板。
  2616. *
  2617. * 原来走全屏遮罩弹窗,和右侧预览是两套观感;现在统一走 `previewFile` 这条链路。
  2618. * 产物还没有 url(仍在容器里)时如实提示,不给空白预览。
  2619. */
  2620. preview(d: Deliverable): void {
  2621. const url = d.artifacts?.[0]?.url ?? '';
  2622. if (!url) {
  2623. this.previewing.set(d);
  2624. return;
  2625. }
  2626. this.openFile({
  2627. url,
  2628. name: d.filename ?? d.title ?? '成果',
  2629. mimeType: d.artifacts?.[0]?.type ?? '',
  2630. });
  2631. }
  2632. fileIcon(type?: string): string {
  2633. return type === 'CSV' ? 'fa-file-csv' : 'fa-file-lines';
  2634. }
  2635. /** CSV 内容转表格行 */
  2636. csvRows(d: Deliverable): string[][] {
  2637. if (!d.content) return [];
  2638. return d.content
  2639. .split(/\r?\n/)
  2640. .filter((l) => l.trim().length > 0)
  2641. .map((l) => l.split(','));
  2642. }
  2643. goArtifact(id: string): void {
  2644. void this.router.navigate(['/', this.device.mode(), 'deliverables'], {
  2645. queryParams: { focus: id },
  2646. });
  2647. }
  2648. goTask(id: string): void {
  2649. void this.router.navigate(['/', this.device.mode(), 'tasks', id]);
  2650. }
  2651. /**
  2652. * 贴底:**滚动两次**。
  2653. *
  2654. * 🔴 踩坑(2026-10-21 实测):只滚一次时,消息已进 DOM 但 **markdown 还没渲染完**,
  2655. * 那一刻 `scrollHeight` 偏小,滚完仍差 1700+ px —— 用户打开会话看到的是中间某处,
  2656. * 而且之后**再也不动**(因为尾部 id 没再变,effect 不再触发)。
  2657. *
  2658. * 做法:立即滚一次(同步可见),下一个宏任务再滚一次(吃掉 md 渲染后的增量)。
  2659. * 用 `requestAnimationFrame` 而非 `queueMicrotask`:后者在渲染前执行,量不到最终高度。
  2660. */
  2661. private stickToBottom(): void {
  2662. // 用户主动上翻看历史时不要把他拽回来(但首次进入要贴底)
  2663. if (this.userScrolledAway && this.lastTailId !== '') return;
  2664. // 正在往前翻页:此时 layout 变化属于「向上插入」,绝不能贴底
  2665. if (this.loadingEarlier) return;
  2666. this.scrollToBottom();
  2667. requestAnimationFrame(() => {
  2668. this.scrollToBottom();
  2669. // 图片/mermaid 等异步内容会继续长高,再补一次
  2670. setTimeout(() => this.scrollToBottom(), 120);
  2671. });
  2672. }
  2673. private scrollToBottom(): void {
  2674. const el = this.messagesEl()?.nativeElement;
  2675. if (el) el.scrollTop = el.scrollHeight;
  2676. }
  2677. /**
  2678. * AC-04.4:滚动到顶部附近(<80px)自动加载更早历史。
  2679. *
  2680. * 加载前记录 `scrollHeight`,加载后按差值补偿 `scrollTop`,
  2681. * 使视口**锚定在原来的消息上**(不跳变)。
  2682. */
  2683. onMessagesScroll(_e: Event): void {
  2684. const el = this.messagesEl()?.nativeElement;
  2685. if (!el) return;
  2686. /*
  2687. * 记住「用户是否主动离开底部」。
  2688. * 距底 > 160px 视为用户在往上翻 —— 此时新消息到达**不要**把他拽回底部
  2689. * (否则翻历史翻到一半就被弹走);滚回接近底部时自动恢复贴底。
  2690. */
  2691. const gap = el.scrollHeight - el.scrollTop - el.clientHeight;
  2692. this.userScrolledAway = gap > 160;
  2693. if (el.scrollTop > 80) return;
  2694. if (this.chat.paginating() || !this.chat.hasMoreHistory()) return;
  2695. void this.loadEarlier();
  2696. }
  2697. /**
  2698. * 首屏内容不满一屏时自动补齐(懒加载的另一半)。
  2699. *
  2700. * 🔴 场景:会话首屏固定取 30 条,但短消息一屏能放 40+ 条——
  2701. * 用户打开会看到下方大片空白,还以为是「会话就这么多」。
  2702. * 这里在渲染后测一次高度,不满就往前补一轮,最多 3 轮,
  2703. * 避免一次把整个历史拉穿(那反而拖慢首次进入)。
  2704. */
  2705. private async fillViewportIfNeeded(): Promise<void> {
  2706. const el = this.messagesEl()?.nativeElement;
  2707. if (!el) return;
  2708. for (let i = 0; i < 3; i++) {
  2709. // 等渲染完成再量(markdown 渲染会撑高内容)
  2710. await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
  2711. const el2 = this.messagesEl()?.nativeElement;
  2712. if (!el2) return;
  2713. // 内容已经超出视口 → 不需要补
  2714. if (el2.scrollHeight > el2.clientHeight + 8) return;
  2715. // 没有更早的了 → 补也补不出来
  2716. if (!this.chat.hasMoreHistory() || this.chat.paginating()) return;
  2717. await this.loadEarlier();
  2718. }
  2719. }
  2720. /** 加载更早历史,并保持滚动锚点 */
  2721. /**
  2722. * 加载更早历史,并**保持滚动锚点**(用户要求:位置不能动)。
  2723. *
  2724. * 🔴 踩坑:原实现用 `queueMicrotask` 补偿 —— 那一刻新消息还没渲染完,
  2725. * 量到的 `scrollHeight` 仍是旧值,补偿量算成 0 → 视口被顶到上面去了。
  2726. *
  2727. * 正确做法:等**两帧**再量(首帧是布局,次帧含 markdown 渲染后的增量),
  2728. * 用 `新高度 - 旧高度` 作为补偿量。两次测量都拿实时的 `scrollHeight`。
  2729. */
  2730. async loadEarlier(): Promise<void> {
  2731. const el = this.messagesEl()?.nativeElement;
  2732. if (!el) return;
  2733. const beforeHeight = el.scrollHeight;
  2734. const beforeTop = el.scrollTop;
  2735. this.loadingEarlier = true;
  2736. const grew = await this.chat.paginate();
  2737. if (!grew) {
  2738. this.loadingEarlier = false;
  2739. return;
  2740. }
  2741. // 两帧之后再量:一帧可能还没完成 markdown 渲染
  2742. requestAnimationFrame(() => {
  2743. requestAnimationFrame(() => {
  2744. const added = el.scrollHeight - beforeHeight;
  2745. if (added > 0) el.scrollTop = beforeTop + added;
  2746. this.loadingEarlier = false;
  2747. });
  2748. });
  2749. }
  2750. }