@guochun42/dsh-client-ui-user-text-demo
v0.1.0
Published
Custom user-message text rendering demo: highlight #hashtag and @mention in sent messages
Readme
@guochun42/dsh-client-ui-user-text-demo
自定义「用户消息文本渲染」的 DSH 客户端 UI 插件示例:把已发送消息里的
#hashtag 和 @mention 高亮成彩色胶囊(chip),其余文本保持原样。
它演示了什么
DSH 的用户消息文本渲染由两段组成,本示例各覆盖一段:
| 步骤 | API | 作用 |
|---|---|---|
| 1. 匹配规则 | ctx.uiConversation.userText.register(rule) | 用 match(text, from) 从原文里找「最近的候选节点」 |
| 2. keyed 渲染 | ctx.slots.inject('conversation.user.text.node', ...) | 为每个规则 id 注册一个内联 React 组件 |
- 每个规则 id 同时也是
conversation.user.text.node这个 keyed slot 的 key。 - 未被任何规则覆盖的子串会由官方
text渲染器回退为普通文本。 data必须是纯 JSON(会被序列化/比较),不要放函数或 React 元素。
目录结构
dsh-client-ui-user-text-demo/
├── package.json # dsh.client 声明 + 构建脚本
├── tsconfig.json
├── src/
│ ├── index.ts # host 端 no-op 入口
│ ├── client.ts # 客户端重导出入口(-> ./client)
│ └── client/
│ ├── contract.ts # declaration merge:追加 UserTextDataMap
│ ├── rules.ts # hashtagRule / mentionRule(match 逻辑)
│ ├── chips.tsx # HashtagChip / MentionChip(渲染组件)
│ └── index.ts # apply + inject:注册规则与渲染器
└── scripts/wrap-client.mjs # 把 CJS 产物包成 __ModuleLoader__ 格式关键代码
// src/client/rules.ts —— 匹配规则
export const hashtagRule: UserTextRule<typeof HASHTAG_ID> = {
id: HASHTAG_ID, // 'demo.hashtag'
order: 100,
match: (text, from) => {
const re = /#[^\s#@]+/g;
re.lastIndex = from; // 从 from 往后找,不是对切片匹配
const m = re.exec(text);
if (!m) return undefined;
return { id: HASHTAG_ID, start: m.index, end: m.index + m[0].length, data: { tag: m[0] } };
},
};// src/client/index.ts —— 注册规则 + keyed 渲染器
export const inject = ['@deepseek-ai/dsh-client-ui-conversation'];
export function apply(ctx: Context): void {
ctx.uiConversation.userText.register(hashtagRule);
ctx.uiConversation.userText.register(mentionRule);
ctx.slots.inject('conversation.user.text.node', function* () {
yield ctx.slots.register(
{ name: 'conversation.user.text.node', key: HASHTAG_ID, priority: 100 },
HashtagChip,
);
yield ctx.slots.register(
{ name: 'conversation.user.text.node', key: MENTION_ID, priority: 100 },
MentionChip,
);
});
}组件从 slot 拿到的 props:
function HashtagChip({ node }: UserTextNodeProps<'demo.hashtag'>) {
// node.data.tag —— match 返回的 JSON
// node.start/end —— 原文 UTF-16 坐标
// raw —— 该节点覆盖的原始子串
// interactive —— 是否允许交互
}构建
pnpm install
pnpm run bundle # 产出 lib/index.mjs + lib/client.js(已包装 __ModuleLoader__)构建产物约定(与官方 ui-agent-team / ui-conversation 一致):
.(host 入口):lib/index.mjs,空apply./client(浏览器入口):lib/client.js,window.__ModuleLoader__.load(...)react走 external,由 DSH 客户端加载器提供
安装到 DSH
把插件发布到 npm 后,用 bundle 包安装(plugin_manager):
- 发布插件包:
pnpm publish --access public - 发布 bundle 包:
pnpm publish --access public - 在 DSH 里安装 bundle,
spec = @guochun42/[email protected]
cordis.patch.yml 通过 insert 把插件挂进 host 插件列表:
- insert:
- id: user-text-demo
name: '@guochun42/dsh-client-ui-user-text-demo'版本必须与 DSH 运行时匹配(当前
0.2.2-rc.1),升级时同步改peerDependencies/devDependencies里的@deepseek-ai/dsh-*与@deepseek-ai/cordis。
如何扩展
- 加新规则:在
contract.ts给UserTextDataMap加一个 id → 在rules.ts写一个match→ 在index.ts的 generator 里再yield一个register。 - 精确匹配:
match里的正则可按需收紧(例如#[A-Za-z0-9_\u4e00-\u9fff]+), 并可用context.read(key)读取会话 Facts 做条件匹配。 - 正式样式:示例用内联 style 便于零依赖运行;真实项目可改用
styles.insert注入 CSS,或换成项目自己的 CSS-in-JS 方案。
