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

koishi-plugin-chatluna-regex-middleware

v1.0.0

Published

ChatLuna 正则表达式中间件插件,支持类似 SillyTavern 的正则功能

Readme

ChatLuna 正则表达式中间件

这是一个 ChatLuna 中间件插件,实现了类似 SillyTavern 的正则表达式功能,允许你在对话前后对文本进行正则替换处理。

功能特性

  • 正则规则管理:添加、删除、更新、启用/禁用正则规则
  • 双向处理:支持对用户输入和 AI 输出分别应用正则规则
  • 作用域控制:支持全局、角色级、房间级作用域(预留接口)
  • 实时应用:自动在 ChatLuna 对话前后应用规则
  • 规则测试:提供测试命令验证正则表达式
  • 持久化存储:规则保存在 JSON 文件中

安装

npm install koishi-plugin-chatluna-regex-middleware

配置

koishi.yml 中添加:

chatluna-regex-middleware:
  enabled: true                    # 启用插件
  rulesFile: ./data/chatluna-regex/rules.json  # 规则文件路径
  applyToInput: true               # 应用到用户输入
  applyToOutput: true               # 应用到 AI 输出
  debug: false                      # 调试模式

使用方法

列出所有规则

regex.list
# 或
regex.ls

添加规则

regex.add <名称> <正则模式> <替换文本> [选项]

选项:

  • -t <target>: 目标,可选值:input(输入)、output(输出)、both(两者,默认)
  • -s <scope>: 作用域,可选值:global(全局,默认)、character(角色)、room(房间)
  • -f <flags>: 正则标志,如 g(全局)、i(忽略大小写)、gi(两者)
  • -d <description>: 规则描述

示例:

# 将 "哈哈" 替换为 "嘻嘻"
regex.add 替换哈哈 哈哈 嘻嘻

# 将用户输入中的 "你" 替换为 "您"(仅对输入生效)
regex.add 礼貌用语 你 您 -t input

# 将 AI 输出中的 "我" 替换为 "咱"(仅对输出生效)
regex.add 方言替换 我 咱 -t output -f gi

删除规则

regex.delete <规则ID>
# 或
regex.del <规则ID>
# 或
regex.rm <规则ID>

启用/禁用规则

regex.toggle <规则ID>

更新规则

regex.update <规则ID> [选项]

选项与添加规则相同。

测试正则表达式

regex.test <模式> <测试文本> [-r <替换文本>] [-f <标志>]

示例:

regex.test 哈哈 哈哈哈哈哈 -r 嘻嘻

规则文件格式

规则保存在 JSON 文件中,格式如下:

{
  "version": "1.0.0",
  "rules": [
    {
      "id": "uuid",
      "name": "规则名称",
      "pattern": "正则表达式",
      "replacement": "替换文本",
      "enabled": true,
      "scope": "global",
      "target": "both",
      "flags": "g",
      "description": "规则描述",
      "createdAt": 1234567890,
      "updatedAt": 1234567890
    }
  ]
}

工作原理

  1. 用户输入处理:监听 chatluna/before-chat 事件,在消息发送给 AI 前应用 targetinputboth 的规则
  2. AI 输出处理:监听 chatluna/after-chat 事件,在 AI 回复后应用 targetoutputboth 的规则

注意事项

  • 正则表达式会按照规则添加的顺序依次应用
  • 如果某个规则执行失败,会记录警告日志但不会中断处理
  • 建议在添加规则前使用 regex.test 命令测试正则表达式
  • 规则文件会在插件启动时自动创建

开发

# 安装依赖
npm install

# 构建
npm run build

# 开发模式(监听文件变化)
npm run build:dev

License

MIT