feat: 实现插件系统与默认插件#1200
Draft
Keldos-Li wants to merge 1 commit into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
工作概览
这个 PR 为 ChuanhuChat 引入一套参考 Stable Diffusion WebUI 设计的插件系统,并提供两个示例插件作为开发范例。
主要目标是让第三方插件可以在不直接修改主工程代码的情况下扩展聊天流程、设置页、侧边栏工具箱和前端资源。
主要变更
modules/extensions.py、modules/plugin_callbacks.py、modules/plugin_context.pyextensions/目录,读取插件metadata.json,并按启用状态加载 Python 脚本、CSS、JSwindow.ChuanhuApp前端 API,插件脚本可以复用主工程的 Gradio DOM 访问和初始化钩子docs/extensions.md插件接口与开发规范
插件目录结构建议如下:
metadata.json用于声明插件基础信息,例如:id:稳定的插件 ID,只使用小写字母、数字、下划线或短横线name/name_i18n:插件显示名和插件自带翻译description/description_i18n:插件描述和插件自带翻译version:插件版本entry:Python 入口脚本,默认可指向scripts/main.py插件应当自行提供翻译字符串,不应把示例插件或第三方插件的文案写入主工程 locale 文件。
Python 插件入口通过
setup(context)注册能力。当前支持的主要扩展点包括:on_before_chat:发送到模型前修改用户输入、系统提示词或请求上下文on_after_chat:模型返回后处理输出或记录结果on_toolbox_tab:向侧边栏工具箱注册插件自己的 Tabon_settings_tab:向设置页注册插件自己的设置 Tab前端资源规范:
extensions/<id>/style.css,不要写入 ChuanhuChat 全局 custom CSSjavascript/下window.ChuanhuApp:root()/gradioApp():获取 Gradio 根节点onReady(callback):主界面首次初始化完成后执行onRender(callback):Gradio render 后执行onMutation(callback):Gradio 根节点内容变化后执行userInput():获取主输入框setInputValue(input, value):设置输入框值并派发input/change事件data-*标记避免重复绑定示例插件
提示词工具箱
extensions/prompt_tools这是一个真实可用的侧边栏插件。它会在工具箱中注册一个 Tab,用户可以选择输出语言,并点击常用任务按钮,把润色、翻译、总结、解释代码、提取待办、邮件草稿、评审优化、会议纪要等提示词插入当前输入框。
这个插件主要展示:
window.ChuanhuApp.userInput()和setInputValue()与主输入框交互自动笔记
extensions/auto_notes这是一个流程 hook 示例。插件可以按配置把启用期间的问答追加到 Markdown 笔记中,适合沉淀会议纪要、学习记录和调试日志。
这个插件主要展示:
on_after_chat处理模型返回后的内容UI 约定
文档
新增
docs/extensions.md,覆盖:window.ChuanhuApp前端 API