fix: tag reader answer rows with official assistant-step attributes

dsh-btw resolves a text selection by walking up to the nearest data-chat-flow-kind row; reader answer articles lacked it, so the side-ask menu never opened. AssistantNode now publishes data-chat-flow-kind=assistant-step, a per-part data-chat-flow-key, data-chat-node-key and data-chat-turn while keeping data-reader-* intact. data-chat-anchor-key stays unpublished so rewind-cut.ts keeps sole ownership of rewind hiding.

Tests: chat-seat unit tests for the exact attributes and a wiring assertion on the article; full suite 34 files / 241 cases green.
This commit is contained in:
QQ 2026-11-28 11:16:16 +08:00
parent f5da78063a
commit 3fa55c72c3
7 changed files with 86 additions and 1 deletions

View File

@ -4,6 +4,7 @@
- 恢复 1.0.4 删除的阅读视图源码(`src/client/**` 组件、样式、Markdown/KaTeX 渲染、官方槽位镜像、文本变换服务、逐字动效、时间轴、产物卡、MCP 交互卡片、设置节)与对应的纯逻辑测试。 - 恢复 1.0.4 删除的阅读视图源码(`src/client/**` 组件、样式、Markdown/KaTeX 渲染、官方槽位镜像、文本变换服务、逐字动效、时间轴、产物卡、MCP 交互卡片、设置节)与对应的纯逻辑测试。
- 阅读视图不再注册「阅读」页签:改为在 `conversation.view` 里以 `id: 'chat'`、`priority: -10` 接管内置「对话」页签的正文区域。页头、输入框、审批区、外层滚动画布仍走官方。 - 阅读视图不再注册「阅读」页签:改为在 `conversation.view` 里以 `id: 'chat'`、`priority: -10` 接管内置「对话」页签的正文区域。页头、输入框、审批区、外层滚动画布仍走官方。
- 回答段补回官方行契约:`AssistantNode` 的每个回答 `<article>` 现在带 `data-chat-flow-kind="assistant-step"`、`data-chat-flow-key`(按段唯一)、`data-chat-node-key` 与 `data-chat-turn`,修复 dsh-btw 旁问选中回答文字时找不到消息行、菜单不弹的问题;`data-chat-anchor-key` 仍不发布,回退隐藏继续由 `rewind-cut.ts` 负责。官方 `[data-text-interaction-surface]` 小工具条尚未镜像(btw 的正文小按钮仍不出现)。
- 因为列表槽的页签清单读的是原始注册账本(不是渲染胜者),新增 `chat-view-seat.ts`:注册后把官方 ChatView 条目的 View `id` 暂时清空,让页签投影只看到本插件那一项;插件卸载时恢复。官方条目本身始终留在账本里,`conversation.chat.flow` 子槽与 `chatStore` 仍由官方 disposer 释放;ui-chat 遮蔽期间重注册会被自动重新隐藏(有专项用例)。 - 因为列表槽的页签清单读的是原始注册账本(不是渲染胜者),新增 `chat-view-seat.ts`:注册后把官方 ChatView 条目的 View `id` 暂时清空,让页签投影只看到本插件那一项;插件卸载时恢复。官方条目本身始终留在账本里,`conversation.chat.flow` 子槽与 `chatStore` 仍由官方 disposer 释放;ui-chat 遮蔽期间重注册会被自动重新隐藏(有专项用例)。
- 未恢复宿主 `/better-display/reveal` 与 `/better-display/metrics` 路由;「在访达/资源管理器中定位」会走官方父目录打开的回退路径。 - 未恢复宿主 `/better-display/reveal` 与 `/better-display/metrics` 路由;「在访达/资源管理器中定位」会走官方父目录打开的回退路径。
- 验证:`pnpm build`、`pnpm typecheck`、`pnpm test`(33 个文件 / 237 个用例)通过。 - 验证:`pnpm build`、`pnpm typecheck`、`pnpm test`(33 个文件 / 237 个用例)通过。

View File

@ -10,6 +10,10 @@
下文的“阅读页”均指接管后的「对话」正文。`entry.tsx`(打开会话时强制切到阅读页)已停用,源码仅作参考。 下文的“阅读页”均指接管后的「对话」正文。`entry.tsx`(打开会话时强制切到阅读页)已停用,源码仅作参考。
## 回答行的官方 DOM 契约
官方 ChatView 给每条 assistant-step 行发 `data-chat-flow-kind="assistant-step"`、`data-chat-flow-key`、`data-chat-node-key`、`data-chat-turn`。阅读页原来只给用户气泡发了 `data-chat-flow-kind`,回答段没有,依赖这套标记的第三方插件(如 dsh-btw 的选中文字旁问)会在阅读页里找不到消息行。`AssistantNode` 现在给每个回答段 `<article>` 补发这四个属性:`data-chat-flow-key` 按段加 `#<partStart>` 保持唯一;`data-chat-anchor-key` 仍不发布,避免与阅读页自己的 `rewind-cut.ts` 双重隐藏。官方 `[data-text-interaction-surface]` 文本工具条仍未镜像。
## 一个具体例子 ## 一个具体例子
一条回答的持久化 `messageId` 进入 `OfficialActions`,随后进入 Reader 自己的回答操作插槽。里面的反馈组件、状态注入和弹窗控制器来自当前已加载的官方反馈插件。点击按钮后,由官方控制器打开原来的官方弹窗;Reader 不实现反馈请求、类别或状态管理。 一条回答的持久化 `messageId` 进入 `OfficialActions`,随后进入 Reader 自己的回答操作插槽。里面的反馈组件、状态注入和弹窗控制器来自当前已加载的官方反馈插件。点击按钮后,由官方控制器打开原来的官方弹窗;Reader 不实现反馈请求、类别或状态管理。

View File

@ -47714,6 +47714,26 @@ ${trimmed}
"data-chat-flow-kind": kind "data-chat-flow-kind": kind
}; };
} }
/**
* Official ChatView's row contract for one assistant answer part.
*
* `data-chat-flow-kind="assistant-step"` is the marker plugins such as
* dsh-btw rely on to recognise "this selection is part of a model answer";
* without it they walk up from the selection, find no message row, and bail.
* The flow key stays unique per rendered part so row lookups do not merge
* separate paragraphs. `data-chat-anchor-key` is deliberately absent: the
* reader performs its own rewind hiding (`rewind-cut.ts`) and publishing the
* official anchor on every part would let a DOM-level rewind plugin hide only
* part of a turn.
*/
function answerRowAttrs(nodeKey, partStart, turn) {
return {
"data-chat-flow-kind": "assistant-step",
"data-chat-flow-key": `${nodeKey}#${partStart}`,
"data-chat-node-key": nodeKey,
...turn === void 0 ? {} : { "data-chat-turn": String(turn) }
};
}
// src/client/OfficialContent.tsx // src/client/OfficialContent.tsx
function OfficialActions({ official, messageId }) { function OfficialActions({ official, messageId }) {
if (!official || !messageId) return null; if (!official || !messageId) return null;
@ -48857,6 +48877,7 @@ ${trimmed}
"data-reader-anchor": true, "data-reader-anchor": true,
"data-reader-key": nodeKey, "data-reader-key": nodeKey,
"data-reader-source-start": part.start, "data-reader-source-start": part.start,
...answerRowAttrs(nodeKey, part.start, data.turn),
"data-reader-seam-after": seamAfter || void 0, "data-reader-seam-after": seamAfter || void 0,
"data-answer-status": data.status, "data-answer-status": data.status,
"data-answer-phase": isFinalAnswer ? "body" : "process", "data-answer-phase": isFinalAnswer ? "body" : "process",

File diff suppressed because one or more lines are too long

View File

@ -9,6 +9,7 @@ import { FoldSummaryText } from './LiveFold.js';
import { assistantSegments, forkAnchorSeq, hasVisibleBody, isEarlierNarration } from './projection.js'; import { assistantSegments, forkAnchorSeq, hasVisibleBody, isEarlierNarration } from './projection.js';
import type { TurnBoundary } from './projection.js'; import type { TurnBoundary } from './projection.js';
import type { BlockRenderProps, ReaderProps } from './types.js'; import type { BlockRenderProps, ReaderProps } from './types.js';
import { answerRowAttrs } from './chat-seat.js';
import css from './Reader.module.css'; import css from './Reader.module.css';
function isNode<K extends ChatNodeKind>(node: ChatConversationViewNode, kind: K): node is ChatNode<K> { function isNode<K extends ChatNodeKind>(node: ChatConversationViewNode, kind: K): node is ChatNode<K> {
@ -54,6 +55,7 @@ export const AssistantNode = memo(function AssistantNode({ useChat, nodeKey, bou
label={<FoldSummaryText summary={seamSummary} motion={false} />} /> label={<FoldSummaryText summary={seamSummary} motion={false} />} />
</div>} </div>}
<article className={css.answer} data-reader-body data-reader-anchor data-reader-key={nodeKey} data-reader-source-start={part.start} <article className={css.answer} data-reader-body data-reader-anchor data-reader-key={nodeKey} data-reader-source-start={part.start}
{...answerRowAttrs(nodeKey, part.start, data.turn)}
data-reader-seam-after={seamAfter || undefined} data-answer-status={data.status} data-answer-phase={isFinalAnswer ? 'body' : 'process'}> data-reader-seam-after={seamAfter || undefined} data-answer-status={data.status} data-answer-phase={isFinalAnswer ? 'body' : 'process'}>
<OfficialAssistantBody {...render} node={node} blocks={part.blocks} {...bodyProps} /> <OfficialAssistantBody {...render} node={node} blocks={part.blocks} {...bodyProps} />
{isFinalAnswer && last && data.status === 'interrupted' && <span className={css.stopped}>已停止</span>} {isFinalAnswer && last && data.status === 'interrupted' && <span className={css.stopped}>已停止</span>}

View File

@ -8,3 +8,24 @@ export function chatSeatProps(kind: ChatSeatKind, key: string): Record<string, s
'data-chat-flow-kind': kind, 'data-chat-flow-kind': kind,
}; };
} }
/**
* Official ChatView's row contract for one assistant answer part.
*
* `data-chat-flow-kind="assistant-step"` is the marker plugins such as
* dsh-btw rely on to recognise "this selection is part of a model answer";
* without it they walk up from the selection, find no message row, and bail.
* The flow key stays unique per rendered part so row lookups do not merge
* separate paragraphs. `data-chat-anchor-key` is deliberately absent: the
* reader performs its own rewind hiding (`rewind-cut.ts`) and publishing the
* official anchor on every part would let a DOM-level rewind plugin hide only
* part of a turn.
*/
export function answerRowAttrs(nodeKey: string, partStart: number, turn: number | undefined): Record<string, string> {
return {
'data-chat-flow-kind': 'assistant-step',
'data-chat-flow-key': `${nodeKey}#${partStart}`,
'data-chat-node-key': nodeKey,
...(turn === undefined ? {} : { 'data-chat-turn': String(turn) }),
};
}

36
tests/chat-seat.test.ts Normal file
View File

@ -0,0 +1,36 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { test } from 'vitest';
import { answerRowAttrs, chatSeatProps } from '../src/client/chat-seat.js';
test('answer rows publish the official assistant-step marker dsh-btw looks for', () => {
const attrs = answerRowAttrs('assistant-step:turn-1:0', 2, 1);
assert.equal(attrs['data-chat-flow-kind'], 'assistant-step');
assert.equal(attrs['data-chat-node-key'], 'assistant-step:turn-1:0');
assert.equal(attrs['data-chat-flow-key'], 'assistant-step:turn-1:0#2', 'flow keys stay unique per rendered part');
assert.equal(attrs['data-chat-turn'], '1');
});
test('answer rows do not publish the official rewind anchor', () => {
const attrs = answerRowAttrs('assistant-step:turn-2:1', 0, 2);
assert.equal('data-chat-anchor-key' in attrs, false, 'rewind hiding stays owned by rewind-cut.ts');
assert.equal(attrs['data-chat-flow-kind'], 'assistant-step');
});
test('user bubbles keep their existing official seat attributes', () => {
const attrs = chatSeatProps('user', 'user:turn-1');
assert.deepEqual(attrs, {
'data-chat-anchor-key': 'user:turn-1',
'data-chat-flow-key': 'user:turn-1',
'data-chat-node-key': 'user:turn-1',
'data-chat-flow-kind': 'user',
});
});
test('the answer article actually carries the row attributes', async () => {
const source = await readFile(new URL('../src/client/AssistantNode.tsx', import.meta.url), 'utf8');
const article = source.match(/<article className=\{css\.answer\}[\s\S]*?>/u)?.[0] ?? '';
assert.match(article, /\{\.\.\.answerRowAttrs\(nodeKey, part\.start, data\.turn\)\}/u);
assert.match(article, /data-reader-body/u, 'reader attributes must stay');
assert.match(article, /data-reader-key=\{nodeKey\}/u);
});