npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@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):

  1. 发布插件包:pnpm publish --access public
  2. 发布 bundle 包:pnpm publish --access public
  3. 在 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 方案。