Skip to content

fix: 用户消息里的 @ / / 引用恢复成 chip,并且能点开 - #4

Merged
Rice00 merged 2 commits into
Rice00:mainfrom
nohappiness:fix/user-message-reference-chips
Nov 16, 2026
Merged

Rice00 merged 2 commits into
Rice00:mainfrom
nohappiness:fix/user-message-reference-chips

Conversation

@nohappiness

Copy link
Copy Markdown
Contributor

现象

装上本插件后,用户消息(也就是本插件接管的那一格)里的引用全部退回纯文本:

  • @文件 / @会话 显示成裸字符串,没有 chip 外观;
  • 点它没反应,既不会在侧边栏打开文件,也不会跳到那个会话;
  • 输入框里一切正常(composer 自己有 token 机件),所以看起来像是"发送后样式丢了"。

对比同一个 DSH 上不装本插件的原生渲染:原生那条消息里 @README.md 是一个带文件图标的按钮,点一下右侧栏打开该文件。

根因

本插件用 priority: -1 接管了 conversation.chat.node 的 user 键(plugin.client.js,slots.inject('conversation.chat.node', …)),于是宿主的 UserMessageNodeView 不再渲染这一行。但接管之后,气泡里画的是自己拼出来的字符串:

const text = contentText(data.content);   // 只把 type === 'text' 的块拼起来
…
React.createElement('div', { className: 'mtx-bubble' }, text, …)

而引用 chip 并不在这条路径上。宿主那边的完整链条是:

UserMessageNodeView
  → UserStyleBubble
  → renderFactorySlot('conversation.chat.user.content')     ← 本插件跳过的就是这一步
  → UserText(按 rule 切 token)
  → renderSlot('conversation.user.text.node')
  → FileReference / SessionReference / SkillReference(ReferenceChip + openFile)

@ 引用只有在最后那一步才会变成 chip 并接上点击动作;只画文本,就等于把「样式」和「可点击」一起丢掉了。referenceLabels(会话引用的标签)和 skillNames(/skill 的 chip 来源)同样是在这一步从 facts 里读出来的,所以这两类也一起消失。

改动

plugin.client.js:

  1. 消息体改由宿主的 factory 画。用渲染器 props 里本来就有的 renderFactorySlot 调 conversation.chat.user.content,输入按宿主自己的口径准备:
    • content:文本块按宿主的方式拼接(不加分隔符),image 与 file 附件按原始顺序进 attachments,其余块进 extraBlocks(以前直接丢掉了 file 附件和未知块);
    • referenceLabels 进 content(气泡下方那行引用小结用),同时进 facts.sessionLabels(rule 解析用);
    • skillNames 进 facts;
    • loadImage 透传(宿主的图片槽要用这个被授权的 loader);
    • interactive: true —— 引用就是要能点的。
  2. 保留 fallback,但不静默。factory 每次渲染都探测一次,抛错就退回原来的纯文本气泡:宿主还在启动、或者老宿主根本没有这个 factory 时,消息照样能读;等 factory 出现了,下一次渲染就会用上,不会一次失败就永久关掉。警告只打一次,不刷日志。
  3. 不再自己套第二层气泡。宿主路径下 .mtx-bubble 不再出现,宿主画的气泡由 .mtx-content(宿主自己的列方向,加在本行的宽度上限里)承载;.mtx-bubble 只作为 fallback 分支存在。
  4. 图谱点击的闪烁动画补上宿主气泡(.mtx-content [data-message-bubble]),否则切到宿主路径后闪烁会失效。
  5. 复制/编辑用的文本随之改成宿主口径(多个文本块之间不再插入 \n),保证"看到的"和"复制到的"一致。

兼容

  • 不新增依赖、不改 cordis.patch.yml、不动任何持久格式(message-tree/version / sidecar / 浏览器偏好都没变),按仓库的版本策略属于 patch。
  • 宿主没有 conversation.chat.user.content factory 时行为与 1.2.3 完全一致。
  • 已知仍与本插件接管前的原生渲染不同的一处:conversation.chat.user.before 这个座位(消息来源标记,例如子代理来源)仍然不会渲染 —— 它要求注册项声明 children,而同一 key 上宿主的注册项已经声明过一次,两个注册项同时声明同一个子槽会在 slot 台账里互相覆盖。这条不在本 PR 的范围,需要单独设计(例如只在宿主条目缺席时才声明)。修复前这条也是丢的,所以本 PR 不会让它变差。

验证

  • npm test:新增 test/client-references.test.mjs(9 条),覆盖 factory 的输入(文本拼接、附件顺序含 file、referenceLabels 双写、skillNames、extraBlocks、interactive、loadImage 透传)、无 factory 时的纯文本 fallback、factory 抛错时仍可读且控件还在。全量 150 项除 qa-resolve 的 3 项外全部通过;这 3 项在未改动的 main 上同样失败(它们按本机安装树布局解析真实包,与本次改动无关)。
  • npm run check:package:通过,21 个文件。
  • 真机 A/B(临时 DSH_HOME + 官方 0.2.2-rc.1 运行时 + 离线模型夹具,会话首条消息为 @README.md what does this file say?):
    • 1.2.3:<div class="mtx-bubble">@README.md what does this file say?</div>,[data-ref-chip] = 0,点击无反应;
    • 本分支:.mtx-content [data-message-bubble] 内是 <button data-ref-chip="file" title="@README.md">(带文件图标),点击后右侧栏打开该文件(标签页显示完整路径与内容)。

Zhang Yueqian added 2 commits November 12, 2026 12:39
This view replaces the host's user row, so it inherits that row's duties. It
was drawing the message as one joined string, and the string is the smallest
part of what a user message is: `@file` and session mentions only become chips,
and only become clickable, inside the host's user-content renderer, which
parses the text against its reference rules and wires each chip to its own open
action. Rendering the text directly showed the same characters with none of
that — references arrived as plain text, and clicking them did nothing.

The message now goes through `conversation.chat.user.content` with the input
the host itself passes: content split the way the host splits it (text blocks
concatenated, `image` and `file` attachments in their original order, anything
else as an extra block), `referenceLabels` on the content for the summary line
and `sessionLabels` in `facts` where the rules read them, `skillNames` in
`facts` for `/skill` chips, the session-authorized `loadImage`, and
`interactive: true`. `renderFactorySlot` is taken from the props the seat
already hands this renderer, so no extra injection is needed.

Two things kept the old path alive, because it is what a host that predates
this factory gets:

- the factory is probed per render and a throw falls back to the plain bubble
  rather than failing the row, so a host that is still booting — or one without
  the factory at all — still shows the message, and a later render picks the
  factory up instead of latching it off;
- `.mtx-bubble` and its image placeholder remain the fallback branch only, so
  the host's bubble is never wrapped in a second one of ours.

Attachments also stop being guessed at on hosts that have the factory: the
host's own gallery and file cards draw them, in order, including file
attachments the old renderer ignored entirely. Text blocks now concatenate
without an inserted separator, which is what the host does and what copy and
edit should therefore match.

`.mtx-content` is the only CSS added — the host's column, inside the row's
width cap. The graph's click-to-flash animation targets the host bubble through
`[data-message-bubble]` as well, since `.mtx-bubble` no longer exists on the
host path.

test/client-references.test.mjs holds the delegation to the factory input, and
the two fallbacks. Verified on a real host as well: a message reading
`@README.md what does this file say?` renders a file chip whose click opens the
file in the sidebar, where 1.2.3 showed the literal text and no chip.
The host draws its own caps as percentages of the column it is given
(`UserContent`: `max-width: min(calc(var(--dsh-chat-content-width, 748px)
* 0.702), 82%)`). Inside the row we replaced, the content sat in two
shrink-to-fit boxes (`.mtx-line`, `.mtx-content`), so that percentage
became cyclic and Chrome resolved it against the message's own
max-content: the bubble topped out at 82% of the words it held. A short
message collapsed to one character per line ("继续" drawn as "继" /
"续"), and a long one broke at 82% of its own single-line width.

The host path now spans the row and lets the host's own cap do the
limiting, so the numbers match a native row (measured on a live
instance: 24 CJK characters 344px/1 line instead of 282px/2 lines;
long text caps at 593px, the host's 82%). The plain-text fallback keeps
its shrink-to-fit bubble because it never consults the host.

Two assertions pin the contract: the host path must carry the
full-width class and the stylesheet must define it; the fallback must
not.
@nohappiness

Copy link
Copy Markdown
Contributor Author

增补(2026-11-16):宿主路径的容器宽度

PR #4 的宿主路径引出另一个可见缺陷:短消息在奇怪的地方折行,例如「继续」显示成
「继」「续」两行,长消息总在文本宽度的 82% 处折。

根因仍然在这一层的容器上:宿主的 UserContent 用
max-width: min(calc(var(--dsh-chat-content-width, 748px) * 0.702), 82%) 给自己封顶,
这个百分比在原生行里解析的是聊天列宽(.userRow 是块级 flex 列),
而本插件把宿主内容放进了收缩包裹(shrink-to-fit)的 .mtx-line / .mtx-content,
百分比因此变成循环依赖——Chrome 按「自身 max-content 的 82%」解析:
「继续」= 30px 文本 → 24.6px 上限 → 每行一个汉字;
长句 → 文本宽 82% 处强制折行。

修复:宿主路径给容器一个确定宽度 .mtx-line-host{width:100%;max-width:100%;justify-content:flex-end}

  • .mtx-content{width:100%},让宿主自己的 82% / 525px 封顶重新对着列宽生效
    (实测长消息上限 593px,与原生行一致)。纯文本 fallback 的 .mtx-bubble 仍走收缩包裹,外观不变。

@Rice00

Rice00 commented Nov 16, 2026

Copy link
Copy Markdown
Owner

@nohappiness 谢谢你的 PR,也谢谢把「谁接管了哪一行」写清楚,验收省了很多时间。

本地验收(2026-11-16,Windows / Node 24.21.0,HEAD 35dbcc2):

  • node scripts/build-client.mjs --check:源与产物一致。
  • test/*.test.mjs 逐文件直跑:19/19 通过,含新增的 client-references.test.mjs。
  • 代码审阅:contentParts 与宿主的拼接口径一致;facts.sessionLabels 与 facts.skillNames 双写;interactive: true;factory 每次渲染重新探测、抛异常只警告一次。

CI 也绿了(run #68,9 个 job 全过,含 6 个 official-host)。

一处我不打算让你再改:renderHostContent 把 renderFactorySlot() 的返回值直接返回,而调用点判的是 hostContent !== null。宿主如果在槽位未注册时返回 undefined 而不是抛异常,这个判断会把 undefined 当成「宿主已渲染」,消息体变成空内容,且不报错。这个收口我在合并后自己补,连同对应的测试;release notes 里会写明它出自这份 PR。

未验证的部分:真机浏览器里引用 chip 是否可点击,我没有复现;CI 的 official-host job 只走 HTTP 层。你本地的 A/B 记录我按自述采信。

合并了。谢谢。

@Rice00
Rice00 merged commit 6c32966 into Rice00:main Nov 16, 2026
9 checks passed
@nohappiness
nohappiness deleted the fix/user-message-reference-chips branch November 16, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants