@deepseek-ai/dsh-client-ui-chat 0.2.2-rc.1 → 0.2.2-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (355) hide show
  1. package/README.i18n.yaml +16 -16
  2. package/README.md +17 -5
  3. package/README.zh.md +17 -5
  4. package/lib/client.js +18 -15587
  5. package/lib/client.js.map +1 -0
  6. package/lib/types/chat-settings.d.ts.map +1 -0
  7. package/lib/types/chat-settings.js.map +1 -0
  8. package/lib/types/client/apply.d.ts.map +1 -0
  9. package/lib/types/client/apply.js.map +1 -0
  10. package/lib/types/client/chat/ApprovalCommand.d.ts +4 -3
  11. package/lib/types/client/chat/ApprovalCommand.d.ts.map +1 -0
  12. package/lib/types/client/chat/ApprovalCommand.js.map +1 -0
  13. package/lib/types/client/chat/AssistantMarkdown.d.ts.map +1 -0
  14. package/lib/types/client/chat/AssistantMarkdown.js.map +1 -0
  15. package/lib/types/client/chat/AssistantNodeView.d.ts.map +1 -0
  16. package/lib/types/client/chat/AssistantNodeView.js.map +1 -0
  17. package/lib/types/client/chat/ChatFlow.d.ts.map +1 -0
  18. package/lib/types/client/chat/ChatFlow.js.map +1 -0
  19. package/lib/types/client/chat/ChatGroupSeat.d.ts.map +1 -0
  20. package/lib/types/client/chat/ChatGroupSeat.js.map +1 -0
  21. package/lib/types/client/chat/ChatNodeSeat.d.ts.map +1 -0
  22. package/lib/types/client/chat/ChatNodeSeat.js.map +1 -0
  23. package/lib/types/client/chat/ChatView.d.ts +1 -1
  24. package/lib/types/client/chat/ChatView.d.ts.map +1 -0
  25. package/lib/types/client/chat/ChatView.js.map +1 -0
  26. package/lib/types/client/chat/CommandNodeView.d.ts.map +1 -0
  27. package/lib/types/client/chat/CommandNodeView.js.map +1 -0
  28. package/lib/types/client/chat/CompactionCommandCard.d.ts.map +1 -0
  29. package/lib/types/client/chat/CompactionCommandCard.js.map +1 -0
  30. package/lib/types/client/chat/CompactionItem.d.ts.map +1 -0
  31. package/lib/types/client/chat/CompactionItem.js.map +1 -0
  32. package/lib/types/client/chat/ContextBody.d.ts.map +1 -0
  33. package/lib/types/client/chat/ContextBody.js.map +1 -0
  34. package/lib/types/client/chat/ContextInjectionRow.d.ts.map +1 -0
  35. package/lib/types/client/chat/ContextInjectionRow.js.map +1 -0
  36. package/lib/types/client/chat/GenericCommandCard.d.ts.map +1 -0
  37. package/lib/types/client/chat/GenericCommandCard.js.map +1 -0
  38. package/lib/types/client/chat/MessageIconActions.d.ts.map +1 -0
  39. package/lib/types/client/chat/MessageIconActions.js.map +1 -0
  40. package/lib/types/client/chat/MessageItem.d.ts.map +1 -0
  41. package/lib/types/client/chat/MessageItem.js.map +1 -0
  42. package/lib/types/client/chat/QuotaNoticeHost.d.ts.map +1 -0
  43. package/lib/types/client/chat/QuotaNoticeHost.js.map +1 -0
  44. package/lib/types/client/chat/ReasoningContent.d.ts.map +1 -0
  45. package/lib/types/client/chat/ReasoningContent.js.map +1 -0
  46. package/lib/types/client/chat/ReasoningRow.d.ts.map +1 -0
  47. package/lib/types/client/chat/ReasoningRow.js.map +1 -0
  48. package/lib/types/client/chat/RunningStatus.d.ts.map +1 -0
  49. package/lib/types/client/chat/RunningStatus.js.map +1 -0
  50. package/lib/types/client/chat/RunningWhaleTail.d.ts.map +1 -0
  51. package/lib/types/client/chat/RunningWhaleTail.js.map +1 -0
  52. package/lib/types/client/chat/StatsPills.d.ts.map +1 -0
  53. package/lib/types/client/chat/StatsPills.js.map +1 -0
  54. package/lib/types/client/chat/SystemPromptRow.d.ts.map +1 -0
  55. package/lib/types/client/chat/SystemPromptRow.js.map +1 -0
  56. package/lib/types/client/chat/TurnNavigator.d.ts.map +1 -0
  57. package/lib/types/client/chat/TurnNavigator.js.map +1 -0
  58. package/lib/types/client/chat/TurnProcessNodeView.d.ts.map +1 -0
  59. package/lib/types/client/chat/TurnProcessNodeView.js.map +1 -0
  60. package/lib/types/client/chat/TurnTailNodeView.d.ts.map +1 -0
  61. package/lib/types/client/chat/TurnTailNodeView.js.map +1 -0
  62. package/lib/types/client/chat/TurnTriggerNodeView.d.ts.map +1 -0
  63. package/lib/types/client/chat/TurnTriggerNodeView.js.map +1 -0
  64. package/lib/types/client/chat/TurnUsagePanel.d.ts.map +1 -0
  65. package/lib/types/client/chat/TurnUsagePanel.js.map +1 -0
  66. package/lib/types/client/chat/UserContent.d.ts.map +1 -0
  67. package/lib/types/client/chat/UserContent.js.map +1 -0
  68. package/lib/types/client/chat/flow-motion.d.ts +2 -0
  69. package/lib/types/client/chat/flow-motion.d.ts.map +1 -0
  70. package/lib/types/client/chat/flow-motion.js.map +1 -0
  71. package/lib/types/client/chat/message-chrome.d.ts.map +1 -0
  72. package/lib/types/client/chat/message-chrome.js.map +1 -0
  73. package/lib/types/client/chat/register-node-renderers.d.ts.map +1 -0
  74. package/lib/types/client/chat/register-node-renderers.js.map +1 -0
  75. package/lib/types/client/chat/render-entry.d.ts.map +1 -0
  76. package/lib/types/client/chat/render-entry.js.map +1 -0
  77. package/lib/types/client/chat/searchable-hidden.d.ts.map +1 -0
  78. package/lib/types/client/chat/searchable-hidden.js.map +1 -0
  79. package/lib/types/client/chat/stat-dialog.d.ts +5 -10
  80. package/lib/types/client/chat/stat-dialog.d.ts.map +1 -0
  81. package/lib/types/client/chat/stat-dialog.js.map +1 -0
  82. package/lib/types/client/chat/step-process.d.ts.map +1 -0
  83. package/lib/types/client/chat/step-process.js.map +1 -0
  84. package/lib/types/client/chat/text-interaction.d.ts.map +1 -0
  85. package/lib/types/client/chat/text-interaction.js.map +1 -0
  86. package/lib/types/client/chat/text-selection.d.ts.map +1 -0
  87. package/lib/types/client/chat/text-selection.js.map +1 -0
  88. package/lib/types/client/chat/token-format.d.ts.map +1 -0
  89. package/lib/types/client/chat/token-format.js.map +1 -0
  90. package/lib/types/client/chat/turn-assistant.d.ts.map +1 -0
  91. package/lib/types/client/chat/turn-assistant.js.map +1 -0
  92. package/lib/types/client/chat/turn-rail-items.d.ts.map +1 -0
  93. package/lib/types/client/chat/turn-rail-items.js.map +1 -0
  94. package/lib/types/client/chat/turn-trigger.d.ts +3 -2
  95. package/lib/types/client/chat/turn-trigger.d.ts.map +1 -0
  96. package/lib/types/client/chat/turn-trigger.js.map +1 -0
  97. package/lib/types/client/chat/use-calendar-day.d.ts.map +1 -0
  98. package/lib/types/client/chat/use-calendar-day.js.map +1 -0
  99. package/lib/types/client/chat/use-chat-navigation.d.ts +21 -5
  100. package/lib/types/client/chat/use-chat-navigation.d.ts.map +1 -0
  101. package/lib/types/client/chat/use-chat-navigation.js.map +1 -0
  102. package/lib/types/client/chat/use-chat-reading.d.ts.map +1 -0
  103. package/lib/types/client/chat/use-chat-reading.js.map +1 -0
  104. package/lib/types/client/chat/use-chat-scroll.d.ts +5 -2
  105. package/lib/types/client/chat/use-chat-scroll.d.ts.map +1 -0
  106. package/lib/types/client/chat/use-chat-scroll.js.map +1 -0
  107. package/lib/types/client/chat/use-chat-viewport.d.ts +53 -0
  108. package/lib/types/client/chat/use-chat-viewport.d.ts.map +1 -0
  109. package/lib/types/client/chat/use-chat-viewport.js.map +1 -0
  110. package/lib/types/client/chat/use-disclosure.d.ts.map +1 -0
  111. package/lib/types/client/chat/use-disclosure.js.map +1 -0
  112. package/lib/types/client/chat/use-process-scroll.d.ts.map +1 -0
  113. package/lib/types/client/chat/use-process-scroll.js.map +1 -0
  114. package/lib/types/client/chat/use-scroll-follow.d.ts.map +1 -0
  115. package/lib/types/client/chat/use-scroll-follow.js.map +1 -0
  116. package/lib/types/client/chat/use-turn-data.d.ts.map +1 -0
  117. package/lib/types/client/chat/use-turn-data.js.map +1 -0
  118. package/lib/types/client/contract/assistant-content.d.ts.map +1 -0
  119. package/lib/types/client/contract/assistant-content.js.map +1 -0
  120. package/lib/types/client/contract/chat-nodes.d.ts.map +1 -0
  121. package/lib/types/client/contract/chat-nodes.js.map +1 -0
  122. package/lib/types/client/contract/chat-visibility.d.ts +2 -2
  123. package/lib/types/client/contract/chat-visibility.d.ts.map +1 -0
  124. package/lib/types/client/contract/chat-visibility.js.map +1 -0
  125. package/lib/types/client/contract/group-tree.d.ts.map +1 -0
  126. package/lib/types/client/contract/group-tree.js.map +1 -0
  127. package/lib/types/client/contract/process-groups.d.ts +6 -3
  128. package/lib/types/client/contract/process-groups.d.ts.map +1 -0
  129. package/lib/types/client/contract/process-groups.js.map +1 -0
  130. package/lib/types/client/contract/slots.d.ts +11 -2
  131. package/lib/types/client/contract/slots.d.ts.map +1 -0
  132. package/lib/types/client/contract/slots.js.map +1 -0
  133. package/lib/types/client/contract/snapshot.d.ts.map +1 -0
  134. package/lib/types/client/contract/snapshot.js.map +1 -0
  135. package/lib/types/client/contract/store.d.ts.map +1 -0
  136. package/lib/types/client/contract/store.js.map +1 -0
  137. package/lib/types/client/contract/turn-metrics.d.ts.map +1 -0
  138. package/lib/types/client/contract/turn-metrics.js.map +1 -0
  139. package/lib/types/client/contract/turn-process.d.ts.map +1 -0
  140. package/lib/types/client/contract/turn-process.js.map +1 -0
  141. package/lib/types/client/conversation-nodes/assistant.d.ts.map +1 -0
  142. package/lib/types/client/conversation-nodes/assistant.js.map +1 -0
  143. package/lib/types/client/conversation-nodes/chat-snapshot-builder.d.ts.map +1 -0
  144. package/lib/types/client/conversation-nodes/chat-snapshot-builder.js.map +1 -0
  145. package/lib/types/client/conversation-nodes/command.d.ts.map +1 -0
  146. package/lib/types/client/conversation-nodes/command.js.map +1 -0
  147. package/lib/types/client/conversation-nodes/common.d.ts.map +1 -0
  148. package/lib/types/client/conversation-nodes/common.js.map +1 -0
  149. package/lib/types/client/conversation-nodes/compaction.d.ts.map +1 -0
  150. package/lib/types/client/conversation-nodes/compaction.js.map +1 -0
  151. package/lib/types/client/conversation-nodes/event-projection.d.ts.map +1 -0
  152. package/lib/types/client/conversation-nodes/event-projection.js.map +1 -0
  153. package/lib/types/client/conversation-nodes/fallback.d.ts.map +1 -0
  154. package/lib/types/client/conversation-nodes/fallback.js.map +1 -0
  155. package/lib/types/client/conversation-nodes/group-tree-segments.d.ts.map +1 -0
  156. package/lib/types/client/conversation-nodes/group-tree-segments.js.map +1 -0
  157. package/lib/types/client/conversation-nodes/inbox.d.ts.map +1 -0
  158. package/lib/types/client/conversation-nodes/inbox.js.map +1 -0
  159. package/lib/types/client/conversation-nodes/message.d.ts +1 -1
  160. package/lib/types/client/conversation-nodes/message.d.ts.map +1 -0
  161. package/lib/types/client/conversation-nodes/message.js.map +1 -0
  162. package/lib/types/client/conversation-nodes/process-activity.d.ts +1 -0
  163. package/lib/types/client/conversation-nodes/process-activity.d.ts.map +1 -0
  164. package/lib/types/client/conversation-nodes/process-activity.js.map +1 -0
  165. package/lib/types/client/conversation-nodes/process-groups.d.ts.map +1 -0
  166. package/lib/types/client/conversation-nodes/process-groups.js.map +1 -0
  167. package/lib/types/client/conversation-nodes/register.d.ts.map +1 -0
  168. package/lib/types/client/conversation-nodes/register.js.map +1 -0
  169. package/lib/types/client/conversation-nodes/request-prompt.d.ts.map +1 -0
  170. package/lib/types/client/conversation-nodes/request-prompt.js.map +1 -0
  171. package/lib/types/client/conversation-nodes/retry.d.ts.map +1 -0
  172. package/lib/types/client/conversation-nodes/retry.js.map +1 -0
  173. package/lib/types/client/conversation-nodes/tool.d.ts.map +1 -0
  174. package/lib/types/client/conversation-nodes/tool.js.map +1 -0
  175. package/lib/types/client/conversation-nodes/turn-error.d.ts.map +1 -0
  176. package/lib/types/client/conversation-nodes/turn-error.js.map +1 -0
  177. package/lib/types/client/conversation-nodes/turn-max-tokens.d.ts.map +1 -0
  178. package/lib/types/client/conversation-nodes/turn-max-tokens.js.map +1 -0
  179. package/lib/types/client/conversation-nodes/turn-navigation.d.ts.map +1 -0
  180. package/lib/types/client/conversation-nodes/turn-navigation.js.map +1 -0
  181. package/lib/types/client/conversation-nodes/turn-process-presentation.d.ts.map +1 -0
  182. package/lib/types/client/conversation-nodes/turn-process-presentation.js.map +1 -0
  183. package/lib/types/client/conversation-nodes/turn-process.d.ts.map +1 -0
  184. package/lib/types/client/conversation-nodes/turn-process.js.map +1 -0
  185. package/lib/types/client/conversation-nodes/turn-tail.d.ts.map +1 -0
  186. package/lib/types/client/conversation-nodes/turn-tail.js.map +1 -0
  187. package/lib/types/client/fence/FenceLayers.d.ts +21 -0
  188. package/lib/types/client/fence/FenceLayers.d.ts.map +1 -0
  189. package/lib/types/client/fence/FenceLayers.js.map +1 -0
  190. package/lib/types/client/fence/register.d.ts +10 -0
  191. package/lib/types/client/fence/register.d.ts.map +1 -0
  192. package/lib/types/client/fence/register.js.map +1 -0
  193. package/lib/types/client/file-path.d.ts.map +1 -0
  194. package/lib/types/client/file-path.js.map +1 -0
  195. package/lib/types/client/index.d.ts +1 -1
  196. package/lib/types/client/index.d.ts.map +1 -0
  197. package/lib/types/client/index.js.map +1 -0
  198. package/lib/types/client/locale.d.ts +96 -0
  199. package/lib/types/client/locale.d.ts.map +1 -0
  200. package/lib/types/client/locale.js.map +1 -0
  201. package/lib/types/client/markdown-labels.d.ts.map +1 -0
  202. package/lib/types/client/markdown-labels.js.map +1 -0
  203. package/lib/types/client/model/conversation-context.d.ts.map +1 -0
  204. package/lib/types/client/model/conversation-context.js.map +1 -0
  205. package/lib/types/client/model/steering-history.d.ts.map +1 -0
  206. package/lib/types/client/model/steering-history.js.map +1 -0
  207. package/lib/types/client/model/tool-call-tree.d.ts.map +1 -0
  208. package/lib/types/client/model/tool-call-tree.js.map +1 -0
  209. package/lib/types/client/performance-usage.d.ts.map +1 -0
  210. package/lib/types/client/performance-usage.js.map +1 -0
  211. package/lib/types/client/presentation-policy.d.ts.map +1 -0
  212. package/lib/types/client/presentation-policy.js.map +1 -0
  213. package/lib/types/client/reasoning-text.d.ts.map +1 -0
  214. package/lib/types/client/reasoning-text.js.map +1 -0
  215. package/lib/types/client/settings/LinkOpeningRow.d.ts.map +1 -0
  216. package/lib/types/client/settings/LinkOpeningRow.js.map +1 -0
  217. package/lib/types/client/settings/PerformanceUsageRow.d.ts.map +1 -0
  218. package/lib/types/client/settings/PerformanceUsageRow.js.map +1 -0
  219. package/lib/types/client/settings/PreferenceRow.d.ts.map +1 -0
  220. package/lib/types/client/settings/PreferenceRow.js.map +1 -0
  221. package/lib/types/client/settings/TranscriptViewRow.d.ts.map +1 -0
  222. package/lib/types/client/settings/TranscriptViewRow.js.map +1 -0
  223. package/lib/types/client/stores.d.ts.map +1 -0
  224. package/lib/types/client/stores.js.map +1 -0
  225. package/lib/types/client/transcript-view.d.ts.map +1 -0
  226. package/lib/types/client/transcript-view.js.map +1 -0
  227. package/lib/types/index.d.ts.map +1 -0
  228. package/lib/types/index.js.map +1 -0
  229. package/package.json +44 -41
  230. package/src/chat-settings.ts +98 -0
  231. package/src/client/apply.ts +355 -0
  232. package/src/client/assets/running-whale@2x.png +0 -0
  233. package/src/client/chat/ApprovalCommand.tsx +42 -0
  234. package/src/client/chat/AssistantMarkdown.module.css +88 -0
  235. package/src/client/chat/AssistantMarkdown.tsx +170 -0
  236. package/src/client/chat/AssistantNodeView.tsx +67 -0
  237. package/src/client/chat/ChatFlow.tsx +100 -0
  238. package/src/client/chat/ChatGroupSeat.module.css +166 -0
  239. package/src/client/chat/ChatGroupSeat.tsx +208 -0
  240. package/src/client/chat/ChatNodeSeat.tsx +179 -0
  241. package/src/client/chat/ChatView.module.css +426 -0
  242. package/src/client/chat/ChatView.tsx +351 -0
  243. package/src/client/chat/CommandNodeView.tsx +40 -0
  244. package/src/client/chat/CompactionCommandCard.tsx +26 -0
  245. package/src/client/chat/CompactionItem.tsx +76 -0
  246. package/src/client/chat/ContextBody.module.css +161 -0
  247. package/src/client/chat/ContextBody.tsx +592 -0
  248. package/src/client/chat/ContextInjectionRow.module.css +68 -0
  249. package/src/client/chat/ContextInjectionRow.tsx +94 -0
  250. package/src/client/chat/GenericCommandCard.module.css +49 -0
  251. package/src/client/chat/GenericCommandCard.tsx +75 -0
  252. package/src/client/chat/MessageIconActions.module.css +116 -0
  253. package/src/client/chat/MessageIconActions.tsx +90 -0
  254. package/src/client/chat/MessageItem.module.css +360 -0
  255. package/src/client/chat/MessageItem.tsx +410 -0
  256. package/src/client/chat/QuotaNoticeHost.tsx +27 -0
  257. package/src/client/chat/ReasoningContent.tsx +26 -0
  258. package/src/client/chat/ReasoningRow.module.css +77 -0
  259. package/src/client/chat/ReasoningRow.tsx +112 -0
  260. package/src/client/chat/RunningStatus.tsx +48 -0
  261. package/src/client/chat/RunningWhaleTail.tsx +19 -0
  262. package/src/client/chat/StatsPills.module.css +63 -0
  263. package/src/client/chat/StatsPills.tsx +348 -0
  264. package/src/client/chat/SystemPromptRow.tsx +50 -0
  265. package/src/client/chat/TurnNavigator.module.css +223 -0
  266. package/src/client/chat/TurnNavigator.tsx +295 -0
  267. package/src/client/chat/TurnProcessNodeView.module.css +62 -0
  268. package/src/client/chat/TurnProcessNodeView.tsx +62 -0
  269. package/src/client/chat/TurnTailNodeView.module.css +12 -0
  270. package/src/client/chat/TurnTailNodeView.tsx +81 -0
  271. package/src/client/chat/TurnTriggerNodeView.module.css +50 -0
  272. package/src/client/chat/TurnTriggerNodeView.tsx +57 -0
  273. package/src/client/chat/TurnUsagePanel.module.css +79 -0
  274. package/src/client/chat/TurnUsagePanel.tsx +110 -0
  275. package/src/client/chat/UserContent.tsx +60 -0
  276. package/src/client/chat/accessibility.module.css +8 -0
  277. package/src/client/chat/flow-motion.ts +556 -0
  278. package/src/client/chat/message-chrome.ts +100 -0
  279. package/src/client/chat/register-node-renderers.ts +100 -0
  280. package/src/client/chat/render-entry.ts +16 -0
  281. package/src/client/chat/searchable-hidden.ts +45 -0
  282. package/src/client/chat/stat-dialog.module.css +149 -0
  283. package/src/client/chat/stat-dialog.ts +55 -0
  284. package/src/client/chat/step-process.ts +28 -0
  285. package/src/client/chat/text-interaction.ts +298 -0
  286. package/src/client/chat/text-selection.ts +180 -0
  287. package/src/client/chat/token-format.ts +98 -0
  288. package/src/client/chat/turn-assistant.ts +10 -0
  289. package/src/client/chat/turn-rail-items.ts +89 -0
  290. package/src/client/chat/turn-trigger.ts +88 -0
  291. package/src/client/chat/use-calendar-day.ts +25 -0
  292. package/src/client/chat/use-chat-navigation.ts +309 -0
  293. package/src/client/chat/use-chat-reading.ts +283 -0
  294. package/src/client/chat/use-chat-scroll.ts +209 -0
  295. package/src/client/chat/use-chat-viewport.ts +1130 -0
  296. package/src/client/chat/use-disclosure.ts +35 -0
  297. package/src/client/chat/use-process-scroll.ts +88 -0
  298. package/src/client/chat/use-scroll-follow.ts +182 -0
  299. package/src/client/chat/use-turn-data.ts +23 -0
  300. package/src/client/contract/assistant-content.ts +15 -0
  301. package/src/client/contract/chat-nodes.ts +135 -0
  302. package/src/client/contract/chat-visibility.ts +20 -0
  303. package/src/client/contract/group-tree.ts +37 -0
  304. package/src/client/contract/process-groups.ts +30 -0
  305. package/src/client/contract/slots.ts +508 -0
  306. package/src/client/contract/snapshot.ts +171 -0
  307. package/src/client/contract/store.ts +17 -0
  308. package/src/client/contract/turn-metrics.ts +43 -0
  309. package/src/client/contract/turn-process.ts +58 -0
  310. package/src/client/conversation-nodes/README.i18n.yaml +64 -0
  311. package/src/client/conversation-nodes/README.md +338 -0
  312. package/src/client/conversation-nodes/README.zh.md +338 -0
  313. package/src/client/conversation-nodes/assistant.ts +384 -0
  314. package/src/client/conversation-nodes/chat-snapshot-builder.ts +1244 -0
  315. package/src/client/conversation-nodes/command.ts +227 -0
  316. package/src/client/conversation-nodes/common.ts +83 -0
  317. package/src/client/conversation-nodes/compaction.ts +65 -0
  318. package/src/client/conversation-nodes/event-projection.ts +179 -0
  319. package/src/client/conversation-nodes/fallback.ts +41 -0
  320. package/src/client/conversation-nodes/group-tree-segments.ts +91 -0
  321. package/src/client/conversation-nodes/inbox.ts +148 -0
  322. package/src/client/conversation-nodes/message.ts +182 -0
  323. package/src/client/conversation-nodes/process-activity.ts +170 -0
  324. package/src/client/conversation-nodes/process-groups.ts +303 -0
  325. package/src/client/conversation-nodes/register.ts +38 -0
  326. package/src/client/conversation-nodes/request-prompt.ts +169 -0
  327. package/src/client/conversation-nodes/retry.ts +97 -0
  328. package/src/client/conversation-nodes/tool.ts +371 -0
  329. package/src/client/conversation-nodes/turn-error.ts +120 -0
  330. package/src/client/conversation-nodes/turn-max-tokens.ts +82 -0
  331. package/src/client/conversation-nodes/turn-navigation.ts +97 -0
  332. package/src/client/conversation-nodes/turn-process-presentation.ts +149 -0
  333. package/src/client/conversation-nodes/turn-process.ts +374 -0
  334. package/src/client/conversation-nodes/turn-tail.ts +164 -0
  335. package/src/client/fence/FenceLayers.tsx +33 -0
  336. package/src/client/fence/register.ts +22 -0
  337. package/src/client/file-path.ts +19 -0
  338. package/src/client/index.ts +69 -0
  339. package/src/client/locale.ts +1695 -0
  340. package/src/client/markdown-labels.ts +16 -0
  341. package/src/client/model/conversation-context.ts +24 -0
  342. package/src/client/model/steering-history.ts +64 -0
  343. package/src/client/model/tool-call-tree.ts +204 -0
  344. package/src/client/performance-usage.ts +34 -0
  345. package/src/client/presentation-policy.ts +109 -0
  346. package/src/client/reasoning-text.ts +25 -0
  347. package/src/client/settings/LinkOpeningRow.tsx +47 -0
  348. package/src/client/settings/PerformanceUsageRow.tsx +46 -0
  349. package/src/client/settings/PreferenceRow.module.css +66 -0
  350. package/src/client/settings/PreferenceRow.tsx +59 -0
  351. package/src/client/settings/TranscriptViewRow.tsx +93 -0
  352. package/src/client/stores.ts +62 -0
  353. package/src/client/transcript-view.ts +81 -0
  354. package/src/css-modules.d.ts +6 -0
  355. package/src/index.ts +45 -0
@@ -0,0 +1,1244 @@
1
+ import type { Context } from '@deepseek-ai/cordis'
2
+ import { notifySubscribers, type ObservableSnapshot } from '@deepseek-ai/dsh-client-store'
3
+ import type {
4
+ ConversationLocation, ConversationNode, ConversationTimelineSnapshot, ConversationViewBuilder,
5
+ ConversationViewDefinition, ConversationGroupInput, GroupNodePosition, NodeChange, NodeKey, PartialAssistant, RunningToolCall,
6
+ } from '@deepseek-ai/dsh-client-ui-conversation/client'
7
+ import type { ChatConversationViewNode, ChatNode, ChatNodeDataMap, ChatNodeKind } from '../contract/chat-nodes.ts'
8
+ import { isRunningTool } from '../contract/chat-nodes.ts'
9
+ import { isVisibleChatNode } from '../contract/chat-visibility.ts'
10
+ import type {
11
+ ChatLocationNodeIndex, ChatNodeProcessSource, ChatNodeSource, ChatNodeStore, ChatSnapshot,
12
+ ChatTurnNavigationIndex, ChatTurnProcessPresentation, LegacyConversationSlice, TurnNavigationItem,
13
+ } from '../contract/snapshot.ts'
14
+ import { isAtOrOutside } from '../contract/group-tree.ts'
15
+ import { locationCoordinates } from './common.ts'
16
+ import { sessionRecallLabels, skillInvocationName } from './event-projection.ts'
17
+ import { GroupTreeSegments } from './group-tree-segments.ts'
18
+ import { sameTurnNavigationItem, turnNavigationItem } from './turn-navigation.ts'
19
+ import { ChatTurnProcessProjector } from './turn-process-presentation.ts'
20
+
21
+ const EMPTY_KEYS: readonly string[] = []
22
+ const EMPTY_TURNS: readonly number[] = []
23
+ const EMPTY_ITEMS: readonly TurnNavigationItem[] = []
24
+ const EMPTY_LIST: readonly never[] = []
25
+
26
+ function sameReferences<T>(left: readonly T[], right: readonly T[]): boolean {
27
+ return left.length === right.length && left.every((value, index) => value === right[index])
28
+ }
29
+
30
+ function cachedSource<Key, Source>(
31
+ sources: Map<Key, Source>,
32
+ key: Key,
33
+ create: () => Source,
34
+ ): Source {
35
+ let source = sources.get(key)
36
+ if (source === undefined) {
37
+ source = create()
38
+ sources.set(key, source)
39
+ }
40
+ return source
41
+ }
42
+
43
+ /* jscpd:ignore-start -- Chat Node sources keep publication state inside the keyed Chat store. */
44
+ class MutableChatSource<Value> {
45
+ private readonly listeners = new Set<() => void>()
46
+ private published: Value
47
+
48
+ constructor(
49
+ private readonly read: () => Value,
50
+ private readonly label: string,
51
+ ) {
52
+ this.published = read()
53
+ }
54
+
55
+ readonly getSnapshot = (): Value => this.read()
56
+
57
+ readonly subscribe = (listener: () => void): (() => void) => {
58
+ this.listeners.add(listener)
59
+ return () => { this.listeners.delete(listener) }
60
+ }
61
+
62
+ publish(): void {
63
+ const next = this.getSnapshot()
64
+ if (this.published === next) return
65
+ this.published = next
66
+ notifySubscribers(this.listeners, this.label)
67
+ }
68
+ }
69
+ /* jscpd:ignore-end */
70
+
71
+ /** Membership is indexed on write; ordered arrays are materialized only for observed collections. */
72
+ class TurnKindNodes {
73
+ private readonly nodes = new Map<string, ChatConversationViewNode>()
74
+ private current: readonly unknown[] = EMPTY_LIST
75
+ private dirty = false
76
+ private observable: MutableChatSource<readonly unknown[]> | undefined
77
+
78
+ source(): ObservableSnapshot<readonly unknown[]> {
79
+ return this.observable ??= new MutableChatSource(() => this.read(), '[ui-chat] turn kind nodes')
80
+ }
81
+
82
+ set(node: ChatConversationViewNode): void {
83
+ const previous = this.nodes.get(node.key)
84
+ this.nodes.set(node.key, node)
85
+ if (previous !== undefined && previous.data === node.data && previous.anchorSeq === node.anchorSeq) return
86
+ this.dirty = true
87
+ }
88
+
89
+ delete(key: string): void {
90
+ this.nodes.delete(key)
91
+ this.dirty = true
92
+ }
93
+
94
+ publish(): void {
95
+ this.observable?.publish()
96
+ }
97
+
98
+ private read(): readonly unknown[] {
99
+ if (this.dirty) {
100
+ this.current = [...this.nodes.values()].sort((a, b) => a.anchorSeq - b.anchorSeq).map(node => node.data)
101
+ this.dirty = false
102
+ }
103
+ return this.current
104
+ }
105
+ }
106
+
107
+ class MutableChatNodeStore implements ChatNodeStore {
108
+ private readonly byKey = new Map<string, ChatConversationViewNode>()
109
+ private readonly turnProcesses = new ChatTurnProcessProjector()
110
+ private readonly sources = new Map<string, MutableChatSource<ChatConversationViewNode | undefined>>()
111
+ private readonly processSources = new Map<string, MutableChatSource<ChatTurnProcessPresentation | undefined>>()
112
+ private readonly bottomSources = new Map<string, MutableChatSource<boolean>>()
113
+ /** Bottom membership: the latest loaded exchange. */
114
+ private readonly exchanges = new GroupTreeSegments('exchange')
115
+ private readonly dirtyKeys = new Set<string>()
116
+ private readonly dirtyBottomKeys = new Set<string>()
117
+ private readonly dirtyProcessKeys = new Set<string>()
118
+ private readonly turnKinds = new Map<number, Map<string, TurnKindNodes>>()
119
+ private readonly dirtyTurnKinds = new Set<TurnKindNodes>()
120
+ private valuesCache: readonly ChatConversationViewNode[] = EMPTY_LIST
121
+ private valuesDirty = false
122
+
123
+ get(key: string): ChatConversationViewNode | undefined {
124
+ return this.byKey.get(key)
125
+ }
126
+
127
+ source(key: string): ChatNodeSource {
128
+ return cachedSource(this.sources, key, () => new MutableChatSource(
129
+ () => this.get(key),
130
+ `[ui-chat] node source ${key}`,
131
+ ))
132
+ }
133
+
134
+ bottomSource(key: string): ObservableSnapshot<boolean> {
135
+ return cachedSource(this.bottomSources, key, () => new MutableChatSource(() => {
136
+ const node = this.get(key) as ChatNode | undefined
137
+ return node !== undefined && isVisibleChatNode(node)
138
+ && this.exchanges.inLatest(locationCoordinates(node.location).turn)
139
+ }, `[ui-chat] node bottom source ${key}`))
140
+ }
141
+
142
+ /**
143
+ * Settle the latest exchange for the loaded Turns and mark the Nodes of
144
+ * Turns that entered or left it.
145
+ * @param turnOrder - loaded Turns in ascending timeline order.
146
+ * @param locations - current Chat Location index.
147
+ */
148
+ settleBottom(turnOrder: readonly number[], locations: ChatLocationNodeIndex): void {
149
+ for (const turn of this.exchanges.settle(turnOrder)) {
150
+ for (const key of locations.getTurn(turn)) this.dirtyBottomKeys.add(key)
151
+ }
152
+ }
153
+
154
+ turnDataSource<Kind extends ChatNodeKind>(turn: number, kind: Kind): ObservableSnapshot<readonly ChatNodeDataMap[Kind][]> {
155
+ return this.turnKind(turn, kind).source() as ObservableSnapshot<readonly ChatNodeDataMap[Kind][]>
156
+ }
157
+
158
+ private turnKind(turn: number, kind: string): TurnKindNodes {
159
+ const kinds = cachedSource(this.turnKinds, turn, () => new Map<string, TurnKindNodes>())
160
+ return cachedSource(kinds, kind, () => new TurnKindNodes())
161
+ }
162
+
163
+ private updateTurnKind(previous: ChatConversationViewNode | undefined, next: ChatConversationViewNode | undefined): void {
164
+ const before = previous === undefined ? undefined : locationCoordinates(previous.location).turn
165
+ const after = next === undefined ? undefined : locationCoordinates(next.location).turn
166
+ if (previous !== undefined && before !== undefined && (before !== after || previous.kind !== next?.kind)) {
167
+ const collection = this.turnKind(before, previous.kind)
168
+ collection.delete(previous.key)
169
+ this.dirtyTurnKinds.add(collection)
170
+ }
171
+ if (next !== undefined && after !== undefined) {
172
+ const collection = this.turnKind(after, next.kind)
173
+ collection.set(next)
174
+ this.dirtyTurnKinds.add(collection)
175
+ }
176
+ }
177
+
178
+ processSource(key: string): ChatNodeProcessSource {
179
+ return cachedSource(this.processSources, key, () => new MutableChatSource(
180
+ () => this.process(key),
181
+ `[ui-chat] node process source ${key}`,
182
+ ))
183
+ }
184
+
185
+ process(key: string): ChatTurnProcessPresentation | undefined {
186
+ return this.turnProcesses.get(this.get(key) as ChatNode | undefined)
187
+ }
188
+
189
+ values(): readonly ChatConversationViewNode[] {
190
+ if (this.valuesDirty) {
191
+ this.valuesCache = [...this.byKey.values()]
192
+ this.valuesDirty = false
193
+ }
194
+ return this.valuesCache
195
+ }
196
+
197
+ replace(nodes: readonly ChatConversationViewNode[]): void {
198
+ const previous = new Map(this.byKey)
199
+ this.byKey.clear()
200
+ for (const node of nodes) {
201
+ this.byKey.set(node.key, node)
202
+ if (previous.get(node.key) !== node) {
203
+ this.updateTurnKind(previous.get(node.key), node)
204
+ this.exchanges.track(previous.get(node.key), node)
205
+ this.dirtyKeys.add(node.key)
206
+ this.dirtyProcessKeys.add(node.key)
207
+ }
208
+ previous.delete(node.key)
209
+ }
210
+ for (const key of previous.keys()) {
211
+ this.updateTurnKind(previous.get(key), undefined)
212
+ this.exchanges.track(previous.get(key), undefined)
213
+ this.dirtyKeys.add(key)
214
+ this.dirtyProcessKeys.add(key)
215
+ }
216
+ this.valuesCache = [...this.byKey.values()]
217
+ this.valuesDirty = false
218
+ }
219
+
220
+ upsert(nodes: readonly ChatConversationViewNode[]): void {
221
+ let changed = false
222
+ for (const node of nodes) {
223
+ if (this.byKey.get(node.key) === node) continue
224
+ this.updateTurnKind(this.byKey.get(node.key), node)
225
+ this.exchanges.track(this.byKey.get(node.key), node)
226
+ this.byKey.set(node.key, node)
227
+ this.dirtyKeys.add(node.key)
228
+ this.dirtyProcessKeys.add(node.key)
229
+ changed = true
230
+ }
231
+ if (changed) this.valuesDirty = true
232
+ }
233
+
234
+ touchProcesses(turns: ReadonlySet<number>, locations: ChatLocationNodeIndex): void {
235
+ for (const turn of turns) {
236
+ for (const key of locations.getTurn(turn)) this.dirtyProcessKeys.add(key)
237
+ }
238
+ }
239
+
240
+ replaceProcesses(order: readonly string[], locations: ChatLocationNodeIndex): void {
241
+ this.touchProcesses(this.turnProcesses.replace(order, locations, this), locations)
242
+ }
243
+
244
+ updateProcesses(turns: ReadonlySet<number>, locations: ChatLocationNodeIndex): void {
245
+ this.touchProcesses(this.turnProcesses.update(turns, locations, this), locations)
246
+ }
247
+
248
+ publish(): void {
249
+ const dirty = [...this.dirtyKeys]
250
+ const dirtyBottom = new Set([...this.dirtyBottomKeys, ...dirty])
251
+ const dirtyProcesses = [...this.dirtyProcessKeys]
252
+ const dirtyTurnKinds = [...this.dirtyTurnKinds]
253
+ this.dirtyKeys.clear()
254
+ this.dirtyBottomKeys.clear()
255
+ this.dirtyProcessKeys.clear()
256
+ this.dirtyTurnKinds.clear()
257
+ for (const key of dirty) this.sources.get(key)?.publish()
258
+ for (const key of dirtyBottom) this.bottomSources.get(key)?.publish()
259
+ for (const key of dirtyProcesses) this.processSources.get(key)?.publish()
260
+ for (const collection of dirtyTurnKinds) collection.publish()
261
+ }
262
+ }
263
+
264
+ class MutableChatLocationIndex implements ChatLocationNodeIndex {
265
+ private turns = new Map<number, readonly string[]>()
266
+ private steps = new Map<string, readonly string[]>()
267
+ private positions = new Map<string, GroupNodePosition>()
268
+
269
+ getTurn(turn: number): readonly string[] {
270
+ return this.turns.get(turn) ?? EMPTY_KEYS
271
+ }
272
+
273
+ getStep(turn: number, step: number): readonly string[] {
274
+ return this.steps.get(stepKey(turn, step)) ?? EMPTY_KEYS
275
+ }
276
+
277
+ getPosition(key: string): GroupNodePosition | undefined {
278
+ return this.positions.get(key)
279
+ }
280
+
281
+ rebuild(order: readonly string[], store: ChatNodeStore): readonly number[] {
282
+ const turns = new Map<number, string[]>()
283
+ const steps = new Map<string, string[]>()
284
+ const positions = new Map<string, GroupNodePosition>()
285
+ const changedTurns = new Set<number>()
286
+ for (const [index, key] of order.entries()) {
287
+ const location = store.get(key)?.location
288
+ if (location === undefined) continue
289
+ const coordinates = locationCoordinates(location)
290
+ const previous = this.positions.get(key)
291
+ const position: GroupNodePosition = {
292
+ turn: coordinates.turn,
293
+ previous: order[index - 1] as NodeKey | undefined,
294
+ next: order[index + 1] as NodeKey | undefined,
295
+ }
296
+ const unchanged = previous !== undefined && previous.turn === position.turn
297
+ && previous.previous === position.previous && previous.next === position.next
298
+ positions.set(key, unchanged ? previous : position)
299
+ if (!unchanged) {
300
+ if (previous?.turn !== undefined) changedTurns.add(previous.turn)
301
+ if (position.turn !== undefined) changedTurns.add(position.turn)
302
+ }
303
+ if (coordinates.turn === undefined) continue
304
+ const turnKeys = turns.get(coordinates.turn) ?? []
305
+ turnKeys.push(key)
306
+ turns.set(coordinates.turn, turnKeys)
307
+ if (coordinates.step === undefined) continue
308
+ const step = stepKey(coordinates.turn, coordinates.step)
309
+ const stepKeys = steps.get(step) ?? []
310
+ stepKeys.push(key)
311
+ steps.set(step, stepKeys)
312
+ }
313
+ for (const [key, position] of this.positions) {
314
+ if (!positions.has(key) && position.turn !== undefined) changedTurns.add(position.turn)
315
+ }
316
+ this.positions = positions
317
+ this.turns = updateIndex(this.turns, turns)
318
+ this.steps = updateIndex(this.steps, steps)
319
+ return [...changedTurns]
320
+ }
321
+
322
+ /** Invalidate aggregate readers when member data changes without moving. */
323
+ touch(nodes: readonly ChatConversationViewNode[]): void {
324
+ const turns = new Set<number>()
325
+ const steps = new Set<string>()
326
+ for (const node of nodes) {
327
+ const coordinates = locationCoordinates(node.location)
328
+ if (coordinates.turn === undefined || !this.turns.get(coordinates.turn)?.includes(node.key)) continue
329
+ turns.add(coordinates.turn)
330
+ if (coordinates.step !== undefined) steps.add(stepKey(coordinates.turn, coordinates.step))
331
+ }
332
+ for (const turn of turns) {
333
+ const keys = this.turns.get(turn)
334
+ if (keys === undefined) continue
335
+ this.turns.set(turn, [...keys])
336
+ }
337
+ for (const step of steps) {
338
+ const keys = this.steps.get(step)
339
+ if (keys === undefined) continue
340
+ this.steps.set(step, [...keys])
341
+ }
342
+ }
343
+ }
344
+
345
+ function updateIndex<Key>(
346
+ previous: ReadonlyMap<Key, readonly string[]>,
347
+ nextMutable: ReadonlyMap<Key, string[]>,
348
+ ): Map<Key, readonly string[]> {
349
+ const next = new Map<Key, readonly string[]>()
350
+ const keys = new Set([...previous.keys(), ...nextMutable.keys()])
351
+ for (const key of keys) {
352
+ const before = previous.get(key) ?? EMPTY_KEYS
353
+ const candidate = nextMutable.get(key) ?? EMPTY_KEYS
354
+ const value = sameReferences(before, candidate) ? before : candidate
355
+ if (candidate.length > 0) next.set(key, value)
356
+ }
357
+ return next
358
+ }
359
+
360
+ /**
361
+ * Loaded-Turn rail projection accumulated alongside the node store: a
362
+ * structural change re-derives the Turn set, a content-only upsert re-derives
363
+ * only the Turns whose nodes moved, and the published array keeps its identity
364
+ * until an item actually changes. Renderers therefore consume final Turn data
365
+ * instead of scanning the loaded window per frame.
366
+ */
367
+ class MutableTurnNavigationIndex implements ChatTurnNavigationIndex {
368
+ private current: readonly TurnNavigationItem[] = EMPTY_ITEMS
369
+ private byTurn = new Map<number, TurnNavigationItem>()
370
+
371
+ items(): readonly TurnNavigationItem[] {
372
+ return this.current
373
+ }
374
+
375
+ /** Re-derive the whole Turn set; runs only when the loaded structure moves. */
376
+ rebuild(
377
+ timeline: ConversationTimelineSnapshot,
378
+ locations: ChatLocationNodeIndex,
379
+ nodes: ChatNodeStore,
380
+ ): void {
381
+ const next: TurnNavigationItem[] = []
382
+ const byTurn = new Map<number, TurnNavigationItem>()
383
+ for (const turn of timeline.turnOrder) {
384
+ const derived = turnNavigationItem(turn, locations, nodes)
385
+ if (derived === undefined) continue
386
+ const previous = this.byTurn.get(turn)
387
+ const item = previous !== undefined && sameTurnNavigationItem(previous, derived) ? previous : derived
388
+ next.push(item)
389
+ byTurn.set(turn, item)
390
+ }
391
+ this.byTurn = byTurn
392
+ const unchanged = next.length === this.current.length
393
+ && next.every((item, index) => item === this.current[index])
394
+ if (!unchanged) this.current = next
395
+ }
396
+
397
+ /** Re-derive only the Turns a content-only upsert touched. */
398
+ touch(
399
+ turns: ReadonlySet<number>,
400
+ locations: ChatLocationNodeIndex,
401
+ nodes: ChatNodeStore,
402
+ ): void {
403
+ if (turns.size === 0) return
404
+ const next = this.current.map((item) => {
405
+ if (!turns.has(item.turn)) return item
406
+ const derived = turnNavigationItem(item.turn, locations, nodes)
407
+ if (derived === undefined || sameTurnNavigationItem(item, derived)) return item
408
+ this.byTurn.set(item.turn, derived)
409
+ return derived
410
+ })
411
+ if (next.some((item, index) => item !== this.current[index])) this.current = next
412
+ }
413
+ }
414
+
415
+ function stepKey(turn: number, step: number): string {
416
+ return `${turn}:${step}`
417
+ }
418
+
419
+ function locationTurnStatus(location: ConversationLocation): string | undefined {
420
+ return location.kind === 'turn' || location.kind === 'step' ? location.turn.status : undefined
421
+ }
422
+
423
+ function processPresentationInputChanged(
424
+ previous: ChatNode | undefined,
425
+ next: ChatNode,
426
+ structural: boolean,
427
+ ): boolean {
428
+ if (structural || previous === undefined) return true
429
+ if (locationTurnStatus(previous.location) !== locationTurnStatus(next.location)) return true
430
+ if (previous.kind === 'turn-process' && next.kind === 'turn-process') {
431
+ return previous.data !== next.data
432
+ }
433
+ return previous.kind === 'assistant-step'
434
+ && next.kind === 'assistant-step'
435
+ && previous.data.step !== next.data.step
436
+ }
437
+
438
+ interface TurnProcessPresentation {
439
+ readonly control?: ChatNode<'turn-process'>
440
+ readonly openingInputAnchor?: number
441
+ readonly earliestProcessAnchor?: number
442
+ }
443
+
444
+ function turnProcessPresentations(
445
+ nodes: readonly ChatConversationViewNode[],
446
+ ): ReadonlyMap<number, TurnProcessPresentation> {
447
+ const presentations = new Map<number, TurnProcessPresentation>()
448
+ for (const raw of nodes) {
449
+ const node = raw as ChatNode
450
+ if (node.kind === 'turn-process') {
451
+ presentations.set(node.data.turn, { ...presentations.get(node.data.turn), control: node })
452
+ }
453
+ }
454
+ for (const raw of nodes) {
455
+ const node = raw as ChatNode
456
+ const location = node.location
457
+ if (location.kind !== 'turn' && location.kind !== 'step') continue
458
+ const current: TurnProcessPresentation = presentations.get(location.turn.turn) ?? {}
459
+ const controlAnchor = current.control?.data.controlAnchorSeq
460
+ if (isAtOrOutside(node, 'input')
461
+ && controlAnchor !== undefined
462
+ && (controlAnchor === location.turn.start?.seq || node.anchorSeq < controlAnchor)) {
463
+ presentations.set(location.turn.turn, {
464
+ ...current,
465
+ openingInputAnchor: Math.max(current.openingInputAnchor ?? node.anchorSeq, node.anchorSeq),
466
+ })
467
+ continue
468
+ }
469
+ if (isAtOrOutside(node, 'turn')) continue
470
+ presentations.set(location.turn.turn, {
471
+ ...current,
472
+ earliestProcessAnchor: Math.min(current.earliestProcessAnchor ?? node.anchorSeq, node.anchorSeq),
473
+ })
474
+ }
475
+ return presentations
476
+ }
477
+
478
+ interface PresentationPosition {
479
+ readonly anchor: number
480
+ readonly rank: number
481
+ readonly originalAnchor: number
482
+ }
483
+
484
+ function presentationPosition(
485
+ raw: ChatConversationViewNode,
486
+ presentations: ReadonlyMap<number, TurnProcessPresentation>,
487
+ ): PresentationPosition {
488
+ const node = raw as ChatNode
489
+ const location = node.location
490
+ if (location.kind !== 'turn' && location.kind !== 'step') {
491
+ return { anchor: node.anchorSeq, rank: 0, originalAnchor: node.anchorSeq }
492
+ }
493
+ const presentation = presentations.get(location.turn.turn)
494
+ if (presentation === undefined) {
495
+ return { anchor: node.anchorSeq, rank: 0, originalAnchor: node.anchorSeq }
496
+ }
497
+ const openingInputAnchor = presentation.openingInputAnchor
498
+ if (openingInputAnchor !== undefined
499
+ && node.anchorSeq < openingInputAnchor
500
+ && !isAtOrOutside(node, 'turn')) {
501
+ return { anchor: openingInputAnchor, rank: 2, originalAnchor: node.anchorSeq }
502
+ }
503
+ if (presentation.control !== undefined && node.key === presentation.control.key) {
504
+ return openingInputAnchor === undefined
505
+ ? {
506
+ anchor: presentation.earliestProcessAnchor ?? node.anchorSeq,
507
+ rank: -1,
508
+ originalAnchor: node.anchorSeq,
509
+ }
510
+ : { anchor: openingInputAnchor, rank: 1, originalAnchor: node.anchorSeq }
511
+ }
512
+ return { anchor: node.anchorSeq, rank: 0, originalAnchor: node.anchorSeq }
513
+ }
514
+
515
+ /**
516
+ * Order visible Chat Nodes without changing existing relative order as process
517
+ * eligibility changes. Opening input precedes process candidates, while
518
+ * each synthetic process control sits between them.
519
+ * @param nodes - currently materialized Chat Nodes.
520
+ * @returns visible Nodes in presentation order.
521
+ */
522
+ export function orderedVisibleChatNodes(
523
+ nodes: readonly ChatConversationViewNode[],
524
+ ): ChatConversationViewNode[] {
525
+ const visible = nodes.filter(node => isVisibleChatNode(node as ChatNode))
526
+ const presentations = turnProcessPresentations(visible)
527
+ return visible.sort((left, right) => {
528
+ const leftPosition = presentationPosition(left, presentations)
529
+ const rightPosition = presentationPosition(right, presentations)
530
+ return leftPosition.anchor - rightPosition.anchor
531
+ || leftPosition.rank - rightPosition.rank
532
+ || leftPosition.originalAnchor - rightPosition.originalAnchor
533
+ || left.key.localeCompare(right.key)
534
+ })
535
+ }
536
+
537
+ function referenceMessageSeq(node: ChatConversationViewNode): number | undefined {
538
+ const candidate = node as ChatNode
539
+ return candidate.kind === 'user' || candidate.kind === 'steering'
540
+ ? candidate.data.seq
541
+ : undefined
542
+ }
543
+
544
+ function followingRecall(node: ChatConversationViewNode): {
545
+ readonly messageSeq: number
546
+ readonly labels: readonly string[]
547
+ } | undefined {
548
+ const candidate = node as ChatNode
549
+ if (candidate.kind !== 'context') return undefined
550
+ return {
551
+ messageSeq: candidate.data.seq - 1,
552
+ labels: sessionRecallLabels(candidate.data.source),
553
+ }
554
+ }
555
+
556
+ function withReferenceLabels(
557
+ node: ChatConversationViewNode,
558
+ labels: readonly string[],
559
+ ): ChatConversationViewNode {
560
+ const candidate = node as ChatNode
561
+ if (candidate.kind !== 'user' && candidate.kind !== 'steering') return node
562
+ const current = candidate.data.referenceLabels ?? EMPTY_KEYS
563
+ const hasLabels = Object.hasOwn(candidate.data, 'referenceLabels')
564
+ if (sameReferences(current, labels) && hasLabels === (labels.length > 0)) return node
565
+ const data: Record<string, unknown> = { ...candidate.data }
566
+ if (labels.length === 0) delete data.referenceLabels
567
+ else data.referenceLabels = labels
568
+ return { ...candidate, data }
569
+ }
570
+
571
+ /** Associates a direct message with the sourced recall event that immediately follows it. */
572
+ class ReferenceLabelProjector {
573
+ private readonly messagesBySeq = new Map<number, string>()
574
+ private readonly labelsByMessageSeq = new Map<number, readonly string[]>()
575
+
576
+ replace(nodes: readonly ChatConversationViewNode[]): readonly ChatConversationViewNode[] {
577
+ this.messagesBySeq.clear()
578
+ this.labelsByMessageSeq.clear()
579
+ for (const node of nodes) {
580
+ const messageSeq = referenceMessageSeq(node)
581
+ if (messageSeq !== undefined) this.messagesBySeq.set(messageSeq, node.key)
582
+ const recall = followingRecall(node)
583
+ if (recall !== undefined && recall.labels.length > 0) {
584
+ this.labelsByMessageSeq.set(recall.messageSeq, recall.labels)
585
+ }
586
+ }
587
+ return nodes.map((node) => {
588
+ const messageSeq = referenceMessageSeq(node)
589
+ return messageSeq === undefined
590
+ ? node
591
+ : withReferenceLabels(node, this.labelsByMessageSeq.get(messageSeq) ?? EMPTY_KEYS)
592
+ })
593
+ }
594
+
595
+ apply(
596
+ upserts: readonly ChatConversationViewNode[],
597
+ store: ChatNodeStore,
598
+ ): readonly ChatConversationViewNode[] {
599
+ const byKey = new Map(upserts.map(node => [node.key, node]))
600
+ const affected = new Set<number>()
601
+ for (const node of upserts) {
602
+ const messageSeq = referenceMessageSeq(node)
603
+ if (messageSeq !== undefined) {
604
+ this.messagesBySeq.set(messageSeq, node.key)
605
+ affected.add(messageSeq)
606
+ }
607
+ const recall = followingRecall(node)
608
+ if (recall === undefined) continue
609
+ const current = this.labelsByMessageSeq.get(recall.messageSeq)
610
+ if (recall.labels.length === 0) this.labelsByMessageSeq.delete(recall.messageSeq)
611
+ else {
612
+ this.labelsByMessageSeq.set(
613
+ recall.messageSeq,
614
+ current !== undefined && sameReferences(current, recall.labels) ? current : recall.labels,
615
+ )
616
+ }
617
+ affected.add(recall.messageSeq)
618
+ }
619
+ for (const messageSeq of affected) {
620
+ const key = this.messagesBySeq.get(messageSeq)
621
+ if (key === undefined) continue
622
+ const node = byKey.get(key) ?? store.get(key)
623
+ if (node === undefined) continue
624
+ byKey.set(key, withReferenceLabels(node, this.labelsByMessageSeq.get(messageSeq) ?? EMPTY_KEYS))
625
+ }
626
+ return [...byKey.values()]
627
+ }
628
+ }
629
+
630
+ function withSkillNames(
631
+ node: ChatConversationViewNode,
632
+ names: readonly string[],
633
+ ): ChatConversationViewNode {
634
+ const candidate = node as ChatNode
635
+ if (candidate.kind !== 'user' && candidate.kind !== 'steering') return node
636
+ const current = candidate.data.skillNames ?? EMPTY_KEYS
637
+ const hasNames = Object.hasOwn(candidate.data, 'skillNames')
638
+ if (sameReferences(current, names) && hasNames === (names.length > 0)) return node
639
+ const data: Record<string, unknown> = { ...candidate.data }
640
+ if (names.length === 0) delete data.skillNames
641
+ else data.skillNames = names
642
+ return { ...candidate, data }
643
+ }
644
+
645
+ /** What skill-name batches are made of; every other Node is transparent. */
646
+ type SlashEntryKind = 'message' | 'skill' | 'boundary'
647
+
648
+ interface SlashEntry {
649
+ readonly key: string
650
+ readonly seq: number
651
+ readonly kind: SlashEntryKind
652
+ /** The injected skill name of a `skill` entry. */
653
+ readonly name: string | null
654
+ }
655
+
656
+ /**
657
+ * Classify one Node for batching: a direct message, a `skill-invocation`
658
+ * context, or a boundary of any other kind. A context that injects no skill
659
+ * (workspace rules, the catalog, a recall) is transparent and yields null.
660
+ */
661
+ function slashEntryOf(node: ChatConversationViewNode): SlashEntry | null {
662
+ const candidate = node as ChatNode
663
+ if (candidate.kind === 'user' || candidate.kind === 'steering') {
664
+ return { key: node.key, seq: node.anchorSeq, kind: 'message', name: null }
665
+ }
666
+ if (candidate.kind === 'context') {
667
+ const name = skillInvocationName(candidate.data.source)
668
+ return name === null ? null : { key: node.key, seq: node.anchorSeq, kind: 'skill', name }
669
+ }
670
+ return { key: node.key, seq: node.anchorSeq, kind: 'boundary', name: null }
671
+ }
672
+
673
+ function sameSlashEntry(left: SlashEntry, right: SlashEntry): boolean {
674
+ return left.seq === right.seq && left.kind === right.kind && left.name === right.name
675
+ }
676
+
677
+ /**
678
+ * Attaches each direct message's step-loaded skill names to its Node.
679
+ *
680
+ * A step's `skill-invocation` injections follow the direct messages the host
681
+ * scanned for `/name` gestures and precede the step's first Node of any other
682
+ * kind, so every non-message, non-context Node closes a batch. Every ended
683
+ * Turn publishes its `turn-tail` Node on `turn/end` whatever the reason, so a
684
+ * batch never spans Turns, and `step/start` precedes the direct message in
685
+ * the log, so no boundary separates a message from its injections. Names
686
+ * attach to every direct message of the batch: the bubble decorates only the
687
+ * tokens its own text carries.
688
+ *
689
+ * The index holds only messages, skill injections, and boundaries, ordered by
690
+ * `anchorSeq`. An apply re-reads just the batches around the Nodes whose
691
+ * classification changed and never scans the store, so an assistant
692
+ * streaming frame costs nothing here (the append hot path never scans the
693
+ * Chat Nodes).
694
+ */
695
+ export class SkillNameProjector {
696
+ private readonly entries = new Map<string, SlashEntry>()
697
+ /** Every indexed entry in `anchorSeq` order. */
698
+ private sorted: SlashEntry[] = []
699
+
700
+ /**
701
+ * Rebuild the index from a whole Node set and attach names to its messages.
702
+ * @param nodes - every materialized Chat Node, in any order.
703
+ * @returns the same Nodes, direct messages carrying their batch's names.
704
+ */
705
+ replace(nodes: readonly ChatConversationViewNode[]): readonly ChatConversationViewNode[] {
706
+ this.entries.clear()
707
+ this.sorted = []
708
+ for (const node of nodes) {
709
+ const entry = slashEntryOf(node)
710
+ if (entry === null) continue
711
+ this.entries.set(entry.key, entry)
712
+ this.sorted.push(entry)
713
+ }
714
+ this.sorted.sort((left, right) => left.seq - right.seq)
715
+ const names = new Map<string, readonly string[]>()
716
+ for (let index = 0; index < this.sorted.length; index++) {
717
+ if (this.sorted[index]?.kind === 'boundary') continue
718
+ const end = this.runEnd(index)
719
+ this.assignRun(index, end, names)
720
+ index = end
721
+ }
722
+ return nodes.map(node => withSkillNames(node, names.get(node.key) ?? EMPTY_KEYS))
723
+ }
724
+
725
+ /**
726
+ * Fold one incremental upsert set: re-read only the batches around the
727
+ * Nodes whose classification changed.
728
+ * @param upserts - the changed Nodes.
729
+ * @param store - the resident Nodes, read by key for the messages of an affected batch.
730
+ * @returns the upserts plus any resident message whose names changed.
731
+ */
732
+ apply(
733
+ upserts: readonly ChatConversationViewNode[],
734
+ store: ChatNodeStore,
735
+ ): readonly ChatConversationViewNode[] {
736
+ const dirty: number[] = []
737
+ for (const node of upserts) {
738
+ const next = slashEntryOf(node)
739
+ const previous = this.entries.get(node.key)
740
+ if (previous !== undefined) {
741
+ if (next !== null && sameSlashEntry(previous, next)) {
742
+ // A rebuilt message Node carries Definition state only, never the
743
+ // names a previous apply attached: re-read its batch.
744
+ if (next.kind === 'message') dirty.push(next.seq)
745
+ continue
746
+ }
747
+ this.remove(previous)
748
+ dirty.push(previous.seq)
749
+ }
750
+ if (next === null) continue
751
+ this.insert(next)
752
+ dirty.push(next.seq)
753
+ }
754
+ if (dirty.length === 0) return upserts
755
+ const names = new Map<string, readonly string[]>()
756
+ for (const seq of dirty) this.collectAround(seq, names)
757
+ const byKey = new Map(upserts.map(node => [node.key, node]))
758
+ for (const [key, list] of names) {
759
+ const node = byKey.get(key) ?? store.get(key)
760
+ if (node === undefined) continue
761
+ const next = withSkillNames(node, list)
762
+ if (next !== node || byKey.has(key)) byKey.set(key, next)
763
+ }
764
+ return [...byKey.values()]
765
+ }
766
+
767
+ private insert(entry: SlashEntry): void {
768
+ this.sorted.splice(this.lowerBound(entry.seq), 0, entry)
769
+ this.entries.set(entry.key, entry)
770
+ }
771
+
772
+ private remove(entry: SlashEntry): void {
773
+ this.sorted.splice(this.sorted.indexOf(entry), 1)
774
+ this.entries.delete(entry.key)
775
+ }
776
+
777
+ /** First index whose seq is at least `seq`. */
778
+ private lowerBound(seq: number): number {
779
+ let low = 0
780
+ let high = this.sorted.length
781
+ while (low < high) {
782
+ const middle = (low + high) >>> 1
783
+ if ((this.sorted[middle]?.seq ?? Number.POSITIVE_INFINITY) < seq) low = middle + 1
784
+ else high = middle
785
+ }
786
+ return low
787
+ }
788
+
789
+ /** Last index of the boundary-free run containing `index`. */
790
+ private runEnd(index: number): number {
791
+ let end = index
792
+ while (end + 1 < this.sorted.length && this.sorted[end + 1]?.kind !== 'boundary') end++
793
+ return end
794
+ }
795
+
796
+ /** First index of the boundary-free run containing `index`. */
797
+ private runStart(index: number): number {
798
+ let start = index
799
+ while (start - 1 >= 0 && this.sorted[start - 1]?.kind !== 'boundary') start--
800
+ return start
801
+ }
802
+
803
+ /** Record the names every message of the run `[start, end]` carries. */
804
+ private assignRun(start: number, end: number, names: Map<string, readonly string[]>): void {
805
+ const list: string[] = []
806
+ for (let index = start; index <= end; index++) {
807
+ const entry = this.sorted[index]
808
+ if (entry?.kind === 'skill' && entry.name !== null && !list.includes(entry.name)) list.push(entry.name)
809
+ }
810
+ for (let index = start; index <= end; index++) {
811
+ const entry = this.sorted[index]
812
+ if (entry?.kind === 'message') names.set(entry.key, list)
813
+ }
814
+ }
815
+
816
+ /**
817
+ * Re-read the run(s) around one changed seq: the run holding a message or
818
+ * skill entry, or — for a boundary, or a seq that left the index — the runs
819
+ * on both sides of that position.
820
+ */
821
+ private collectAround(seq: number, names: Map<string, readonly string[]>): void {
822
+ const at = this.lowerBound(seq)
823
+ const here = this.sorted[at]
824
+ if (here !== undefined && here.seq === seq && here.kind !== 'boundary') {
825
+ this.assignRun(this.runStart(at), this.runEnd(at), names)
826
+ return
827
+ }
828
+ if (at - 1 >= 0 && this.sorted[at - 1]?.kind !== 'boundary') {
829
+ this.assignRun(this.runStart(at - 1), at - 1, names)
830
+ }
831
+ const right = here !== undefined && here.seq === seq ? at + 1 : at
832
+ if (right < this.sorted.length && this.sorted[right]?.kind !== 'boundary') {
833
+ this.assignRun(right, this.runEnd(right), names)
834
+ }
835
+ }
836
+ }
837
+
838
+ interface LegacyContribution {
839
+ readonly anchorSeq: number
840
+ readonly nodes: readonly ConversationNode[]
841
+ readonly partial: PartialAssistant | null
842
+ readonly running: RunningToolCall | null
843
+ }
844
+
845
+ const EMPTY_CONTRIBUTION: LegacyContribution = {
846
+ anchorSeq: 0,
847
+ nodes: EMPTY_LIST,
848
+ partial: null,
849
+ running: null,
850
+ }
851
+
852
+ function legacyContribution(raw: ChatConversationViewNode): LegacyContribution {
853
+ const node = raw as ChatNode
854
+ // Content-free settled Assistants remain in the finalized compatibility
855
+ // stream so the composer stats pills keep their pre-assembly step counts;
856
+ // hidden running attempts have no final Node to contribute.
857
+ if (raw.visibility !== 'visible' && node.kind !== 'assistant-step') return EMPTY_CONTRIBUTION
858
+ switch (node.kind) {
859
+ case 'user':
860
+ case 'steering':
861
+ case 'context':
862
+ case 'command':
863
+ case 'compaction':
864
+ case 'turn-error':
865
+ case 'turn-max-tokens':
866
+ case 'unknown':
867
+ return { anchorSeq: node.anchorSeq, nodes: [node.data], partial: null, running: null }
868
+ case 'assistant-step': {
869
+ const data = node.data
870
+ if (data.status === 'running') {
871
+ if (raw.visibility !== 'visible') return EMPTY_CONTRIBUTION
872
+ return {
873
+ anchorSeq: node.anchorSeq,
874
+ nodes: EMPTY_LIST,
875
+ partial: { turn: data.turn, step: data.step, blocks: data.blocks },
876
+ running: null,
877
+ }
878
+ }
879
+ return {
880
+ anchorSeq: node.anchorSeq,
881
+ nodes: data.finalNode === undefined ? EMPTY_LIST : [data.finalNode],
882
+ partial: null,
883
+ running: null,
884
+ }
885
+ }
886
+ case 'tool-call': {
887
+ const root = node.data.root
888
+ return isRunningTool(root)
889
+ ? { anchorSeq: node.anchorSeq, nodes: EMPTY_LIST, partial: null, running: root }
890
+ : { anchorSeq: node.anchorSeq, nodes: [root], partial: null, running: null }
891
+ }
892
+ case 'manual-compaction': {
893
+ const data = node.data
894
+ return {
895
+ anchorSeq: node.anchorSeq,
896
+ nodes: data.compaction === null ? [data.command] : [data.command, data.compaction],
897
+ partial: null,
898
+ running: null,
899
+ }
900
+ }
901
+ case 'model-retry':
902
+ return {
903
+ anchorSeq: node.anchorSeq,
904
+ nodes: node.data.attempts,
905
+ partial: null,
906
+ running: null,
907
+ }
908
+ case 'turn-tail':
909
+ case 'system-prompt':
910
+ // These known Chat rows intentionally make no legacy timeline contribution.
911
+ return EMPTY_CONTRIBUTION
912
+ default:
913
+ return EMPTY_CONTRIBUTION
914
+ }
915
+ }
916
+
917
+ function sameContribution(left: LegacyContribution | undefined, right: LegacyContribution): boolean {
918
+ return left !== undefined
919
+ && left.anchorSeq === right.anchorSeq
920
+ && left.partial?.blocks === right.partial?.blocks
921
+ && left.partial?.turn === right.partial?.turn
922
+ && left.partial?.step === right.partial?.step
923
+ && left.running === right.running
924
+ && sameReferences(left.nodes, right.nodes)
925
+ }
926
+
927
+ /** Incremental compatibility projection for the composer stats pills and legacy top-level snapshot fields. */
928
+ class LegacySliceBuilder {
929
+ private readonly contributions = new Map<string, LegacyContribution>()
930
+ private readonly finalizedContributions = new Map<string, LegacyContribution>()
931
+ private readonly runningContributions = new Map<string, LegacyContribution>()
932
+ private readonly partialContributions = new Map<string, LegacyContribution>()
933
+ private finalized: readonly ConversationNode[] = EMPTY_LIST
934
+ private runningCalls: readonly RunningToolCall[] = EMPTY_LIST
935
+ private partial: PartialAssistant | null = null
936
+ private timeline: ConversationTimelineSnapshot | undefined
937
+ private turnTimings: LegacyConversationSlice['turnTimings'] = new Map()
938
+ private turnEnds: LegacyConversationSlice['turnEnds'] = new Map()
939
+
940
+ replace(
941
+ nodes: readonly ChatConversationViewNode[],
942
+ timeline: ConversationTimelineSnapshot,
943
+ ): LegacyConversationSlice {
944
+ this.contributions.clear()
945
+ this.finalizedContributions.clear()
946
+ this.runningContributions.clear()
947
+ this.partialContributions.clear()
948
+ for (const node of nodes) {
949
+ const contribution = legacyContribution(node)
950
+ this.contributions.set(node.key, contribution)
951
+ this.indexContribution(node.key, contribution)
952
+ }
953
+ this.rebuildFinalized()
954
+ this.rebuildRunning()
955
+ this.rebuildPartial()
956
+ this.updateTimeline(timeline)
957
+ return this.snapshot()
958
+ }
959
+
960
+ apply(
961
+ upserts: readonly ChatConversationViewNode[],
962
+ timeline: ConversationTimelineSnapshot,
963
+ ): LegacyConversationSlice {
964
+ let finalizedChanged = false
965
+ let runningChanged = false
966
+ let partialChanged = false
967
+ for (const node of upserts) {
968
+ const contribution = legacyContribution(node)
969
+ const previous = this.contributions.get(node.key)
970
+ if (sameContribution(previous, contribution)) continue
971
+ finalizedChanged ||= finalizedContributionChanged(previous, contribution)
972
+ runningChanged ||= runningContributionChanged(previous, contribution)
973
+ partialChanged ||= partialContributionChanged(previous, contribution)
974
+ this.contributions.set(node.key, contribution)
975
+ this.indexContribution(node.key, contribution)
976
+ }
977
+ if (finalizedChanged) this.rebuildFinalized()
978
+ if (runningChanged) this.rebuildRunning()
979
+ if (partialChanged) this.rebuildPartial()
980
+ this.updateTimeline(timeline)
981
+ return this.snapshot()
982
+ }
983
+
984
+ private indexContribution(key: string, contribution: LegacyContribution): void {
985
+ updateContributionIndex(this.finalizedContributions, key, contribution, contribution.nodes.length > 0)
986
+ updateContributionIndex(this.runningContributions, key, contribution, contribution.running !== null)
987
+ updateContributionIndex(this.partialContributions, key, contribution, contribution.partial !== null)
988
+ }
989
+
990
+ private rebuildFinalized(): void {
991
+ const finalized = [...this.finalizedContributions.values()]
992
+ .flatMap(value => value.nodes)
993
+ .sort((left, right) => left.seq - right.seq)
994
+ if (!sameReferences(this.finalized, finalized)) this.finalized = finalized
995
+ }
996
+
997
+ private rebuildRunning(): void {
998
+ const runningCalls = [...this.runningContributions.values()]
999
+ .sort((left, right) => left.anchorSeq - right.anchorSeq)
1000
+ .flatMap(value => value.running === null ? [] : [value.running])
1001
+ if (!sameReferences(this.runningCalls, runningCalls)) this.runningCalls = runningCalls
1002
+ }
1003
+
1004
+ private rebuildPartial(): void {
1005
+ const partial = [...this.partialContributions.values()]
1006
+ .sort((left, right) => left.anchorSeq - right.anchorSeq)
1007
+ .findLast(value => value.partial !== null)?.partial ?? null
1008
+ if (this.partial?.blocks !== partial?.blocks
1009
+ || this.partial?.turn !== partial?.turn
1010
+ || this.partial?.step !== partial?.step) this.partial = partial
1011
+ }
1012
+
1013
+ private updateTimeline(timeline: ConversationTimelineSnapshot): void {
1014
+ if (this.timeline === timeline) return
1015
+ this.timeline = timeline
1016
+ const turnTimings = new Map<number, { startTime: number; endTime?: number }>()
1017
+ const turnEnds = new Map<number, number>()
1018
+ for (const turn of timeline.turns.values()) {
1019
+ if (turn.start !== undefined) {
1020
+ turnTimings.set(turn.turn, {
1021
+ startTime: turn.start.time,
1022
+ ...turn.end === undefined ? {} : { endTime: turn.end.time },
1023
+ })
1024
+ }
1025
+ if (turn.end !== undefined) turnEnds.set(turn.turn, turn.end.seq)
1026
+ }
1027
+ this.turnTimings = turnTimings
1028
+ this.turnEnds = turnEnds
1029
+ }
1030
+
1031
+ private snapshot(): LegacyConversationSlice {
1032
+ return {
1033
+ nodes: this.finalized,
1034
+ turnTimings: this.turnTimings,
1035
+ turnEnds: this.turnEnds,
1036
+ partial: this.partial,
1037
+ runningCalls: this.runningCalls,
1038
+ }
1039
+ }
1040
+ }
1041
+
1042
+ function updateContributionIndex(
1043
+ index: Map<string, LegacyContribution>,
1044
+ key: string,
1045
+ contribution: LegacyContribution,
1046
+ present: boolean,
1047
+ ): void {
1048
+ if (present) index.set(key, contribution)
1049
+ else index.delete(key)
1050
+ }
1051
+
1052
+ function finalizedContributionChanged(
1053
+ previous: LegacyContribution | undefined,
1054
+ next: LegacyContribution,
1055
+ ): boolean {
1056
+ const previousNodes = previous?.nodes ?? EMPTY_LIST
1057
+ return !sameReferences(previousNodes, next.nodes)
1058
+ || ((previousNodes.length > 0 || next.nodes.length > 0) && previous?.anchorSeq !== next.anchorSeq)
1059
+ }
1060
+
1061
+ function runningContributionChanged(
1062
+ previous: LegacyContribution | undefined,
1063
+ next: LegacyContribution,
1064
+ ): boolean {
1065
+ return previous?.running !== next.running
1066
+ || ((previous.running !== null || next.running !== null)
1067
+ && previous.anchorSeq !== next.anchorSeq)
1068
+ }
1069
+
1070
+ function partialContributionChanged(
1071
+ previous: LegacyContribution | undefined,
1072
+ next: LegacyContribution,
1073
+ ): boolean {
1074
+ return previous?.partial?.blocks !== next.partial?.blocks
1075
+ || previous?.partial?.turn !== next.partial?.turn
1076
+ || previous?.partial?.step !== next.partial?.step
1077
+ || (((previous?.partial ?? null) !== null || next.partial !== null)
1078
+ && previous?.anchorSeq !== next.anchorSeq)
1079
+ }
1080
+
1081
+ /** Incremental keyed Chat builder registered under the `chat` target. */
1082
+ export class ChatSnapshotBuilder implements ConversationViewBuilder<ChatConversationViewNode, ChatSnapshot> {
1083
+ private readonly store = new MutableChatNodeStore()
1084
+ private readonly locations = new MutableChatLocationIndex()
1085
+ private readonly navigation = new MutableTurnNavigationIndex()
1086
+ private readonly legacy = new LegacySliceBuilder()
1087
+ private readonly referenceLabels = new ReferenceLabelProjector()
1088
+ private readonly skillNames = new SkillNameProjector()
1089
+ private order: readonly string[] = EMPTY_KEYS
1090
+ private latestGroupInput: ConversationGroupInput<ChatConversationViewNode>
1091
+ private readonly readGroupNode = (key: NodeKey): ChatConversationViewNode | undefined => this.store.get(key)
1092
+ private readonly readGroupTurn = (turn: number): readonly NodeKey[] => this.locations.getTurn(turn) as readonly NodeKey[]
1093
+ private readonly readGroupPosition = (key: NodeKey): GroupNodePosition | undefined => this.locations.getPosition(key)
1094
+ /** Last published timeline: a Turn boundary can land without a new node. */
1095
+ private timeline: ConversationTimelineSnapshot | null = null
1096
+ readonly empty: ChatSnapshot
1097
+
1098
+ constructor() {
1099
+ this.empty = this.snapshot({ turnOrder: EMPTY_TURNS, turns: new Map() })
1100
+ this.latestGroupInput = {
1101
+ kind: 'replace',
1102
+ order: this.order as readonly NodeKey[],
1103
+ readNode: this.readGroupNode,
1104
+ readTurn: this.readGroupTurn,
1105
+ readPosition: this.readGroupPosition,
1106
+ timeline: this.empty.timeline,
1107
+ }
1108
+ }
1109
+
1110
+ replace(input: {
1111
+ readonly nodes: readonly ChatConversationViewNode[]
1112
+ readonly timeline: ConversationTimelineSnapshot
1113
+ }): ChatSnapshot {
1114
+ const nodes = this.skillNames.replace(this.referenceLabels.replace(input.nodes))
1115
+ this.store.replace(nodes)
1116
+ this.order = orderedVisibleChatNodes(nodes).map(node => node.key)
1117
+ this.locations.rebuild(this.order, this.store)
1118
+ this.store.replaceProcesses(this.order, this.locations)
1119
+ this.store.settleBottom(input.timeline.turnOrder, this.locations)
1120
+ this.navigation.rebuild(input.timeline, this.locations, this.store)
1121
+ this.timeline = input.timeline
1122
+ this.latestGroupInput = {
1123
+ kind: 'replace',
1124
+ order: this.order as readonly NodeKey[],
1125
+ readNode: this.readGroupNode,
1126
+ readTurn: this.readGroupTurn,
1127
+ readPosition: this.readGroupPosition,
1128
+ timeline: input.timeline,
1129
+ }
1130
+ const snapshot = this.snapshot(input.timeline, this.legacy.replace(nodes, input.timeline))
1131
+ return snapshot
1132
+ }
1133
+
1134
+ apply(input: {
1135
+ readonly upserts: readonly ChatConversationViewNode[]
1136
+ readonly timeline: ConversationTimelineSnapshot
1137
+ readonly changedTurns?: readonly number[]
1138
+ }): ChatSnapshot {
1139
+ const upserts = this.skillNames.apply(this.referenceLabels.apply(input.upserts, this.store), this.store)
1140
+ const processTurns = new Set<number>()
1141
+ let structural = false
1142
+ const contentOnly: ChatConversationViewNode[] = []
1143
+ const changes: NodeChange<ChatConversationViewNode>[] = []
1144
+ for (const node of upserts) {
1145
+ const previous = this.store.get(node.key)
1146
+ if (previous !== node) changes.push({ previous, current: node })
1147
+ const nodeStructural = previous === undefined
1148
+ || previous.kind !== node.kind
1149
+ || previous.anchorSeq !== node.anchorSeq
1150
+ || previous.visibility !== node.visibility
1151
+ || previous.groupTree !== node.groupTree
1152
+ || locationIdentity(previous.location) !== locationIdentity(node.location)
1153
+ structural ||= nodeStructural
1154
+ if (!nodeStructural) contentOnly.push(node)
1155
+ if (processPresentationInputChanged(previous as ChatNode | undefined, node as ChatNode, nodeStructural)) {
1156
+ const previousTurn = previous === undefined ? undefined : locationCoordinates(previous.location).turn
1157
+ const nextTurn = locationCoordinates(node.location).turn
1158
+ if (previousTurn !== undefined) processTurns.add(previousTurn)
1159
+ if (nextTurn !== undefined) processTurns.add(nextTurn)
1160
+ }
1161
+ }
1162
+ this.store.upsert(upserts)
1163
+ let changedTurnOrders: readonly number[] = EMPTY_TURNS
1164
+ if (structural) {
1165
+ const next = orderedVisibleChatNodes(this.store.values()).map(node => node.key)
1166
+ this.order = sameReferences(this.order, next) ? this.order : next
1167
+ changedTurnOrders = this.locations.rebuild(this.order, this.store)
1168
+ }
1169
+ this.locations.touch(contentOnly)
1170
+ this.store.updateProcesses(processTurns, this.locations)
1171
+ this.store.settleBottom(input.timeline.turnOrder, this.locations)
1172
+ if (structural || input.timeline !== this.timeline) {
1173
+ this.navigation.rebuild(input.timeline, this.locations, this.store)
1174
+ } else {
1175
+ this.navigation.touch(turnsOf(contentOnly), this.locations, this.store)
1176
+ }
1177
+ this.timeline = input.timeline
1178
+ this.latestGroupInput = {
1179
+ kind: 'apply',
1180
+ changes,
1181
+ order: this.order as readonly NodeKey[],
1182
+ readNode: this.readGroupNode,
1183
+ readTurn: this.readGroupTurn,
1184
+ readPosition: this.readGroupPosition,
1185
+ timeline: input.timeline,
1186
+ changedTurns: input.changedTurns ?? EMPTY_TURNS,
1187
+ changedTurnOrders,
1188
+ }
1189
+ const snapshot = this.snapshot(input.timeline, this.legacy.apply(upserts, input.timeline))
1190
+ return snapshot
1191
+ }
1192
+
1193
+ groupInput(): ConversationGroupInput<ChatConversationViewNode> {
1194
+ return this.latestGroupInput
1195
+ }
1196
+
1197
+ publish(): void {
1198
+ this.store.publish()
1199
+ }
1200
+
1201
+ private snapshot(
1202
+ timeline: ConversationTimelineSnapshot,
1203
+ legacy = this.legacy.replace(EMPTY_LIST, timeline),
1204
+ ): ChatSnapshot {
1205
+ return {
1206
+ order: this.order,
1207
+ nodes: this.store,
1208
+ locations: this.locations,
1209
+ navigation: this.navigation,
1210
+ timeline,
1211
+ legacy,
1212
+ }
1213
+ }
1214
+ }
1215
+
1216
+ /** Turns owning the given nodes, for the content-only navigation update. */
1217
+ function turnsOf(nodes: readonly ChatConversationViewNode[]): ReadonlySet<number> {
1218
+ const turns = new Set<number>()
1219
+ for (const node of nodes) {
1220
+ const turn = locationCoordinates(node.location).turn
1221
+ if (turn !== undefined) turns.add(turn)
1222
+ }
1223
+ return turns
1224
+ }
1225
+
1226
+ function locationIdentity(location: ConversationLocation): string {
1227
+ const coordinates = locationCoordinates(location)
1228
+ return `${location.kind}:${coordinates.turn ?? ''}:${coordinates.step ?? ''}`
1229
+ }
1230
+
1231
+ /** Chat target factory contributed to the Conversation view registry. */
1232
+ export const chatViewDefinition: ConversationViewDefinition<ChatConversationViewNode, ChatSnapshot> = {
1233
+ target: 'chat',
1234
+ create: () => new ChatSnapshotBuilder(),
1235
+ isActive: snapshot => snapshot.order.some(key => snapshot.nodes.get(key)?.kind !== 'command'),
1236
+ }
1237
+
1238
+ /**
1239
+ * Register the incremental Chat target builder.
1240
+ * @param ctx - owning UI Conversation context.
1241
+ */
1242
+ export function registerChatConversationView(ctx: Context): void {
1243
+ ctx.uiConversation.views.register(chatViewDefinition)
1244
+ }