mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 17:47:06 +08:00
* docs: transfer AstrBotDevs/AstrBot-docs to AstrBotDevs/AstrBot * refactor: reorder imports and improve type hints in sync_docs_to_wiki.py and upload_doc_images_to_r2.py * feat: add GitHub Actions workflow to sync wiki with documentation Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com> Co-authored-by: anka-afk <110004162+anka-afk@users.noreply.github.com> Co-authored-by: zouyonghe <62183434+zouyonghe@users.noreply.github.com> Co-authored-by: shuiping233 <49360196+shuiping233@users.noreply.github.com> Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> Co-authored-by: Sjshi763 <179909421+Sjshi763@users.noreply.github.com> Co-authored-by: xiewoc <70128845+xiewoc@users.noreply.github.com> Co-authored-by: QingFeng-awa <151742581+QingFeng-awa@users.noreply.github.com> Co-authored-by: PaloMiku <96452465+PaloMiku@users.noreply.github.com> Co-authored-by: shangxueink <138397030+shangxueink@users.noreply.github.com> Co-authored-by: IGCrystal-A <244300990+IGCrystal-A@users.noreply.github.com> Co-authored-by: RC-CHN <67079377+RC-CHN@users.noreply.github.com> Co-authored-by: MC090610 <113341105+MC090610@users.noreply.github.com> Co-authored-by: Waterwzy <196913419+Waterwzy@users.noreply.github.com> Co-authored-by: Lanhuace-Wan <186303160+Lanhuace-Wan@users.noreply.github.com> Co-authored-by: LiAlH4qwq <61769640+LiAlH4qwq@users.noreply.github.com> Co-authored-by: HSOS6 <209910899+HSOS6@users.noreply.github.com> Co-authored-by: th-dd <162813557+th-dd@users.noreply.github.com> Co-authored-by: miaoxutao123 <81676466+miaoxutao123@users.noreply.github.com> Co-authored-by: nuomicici <143102889+nuomicici@users.noreply.github.com> Co-authored-by: nasyt233 <210103278+nasyt233@users.noreply.github.com> Co-authored-by: jlugjb <7426462+jlugjb@users.noreply.github.com> Co-authored-by: Raven95676 <176760093+Raven95676@users.noreply.github.com> Co-authored-by: Futureppo <180109455+Futureppo@users.noreply.github.com> Co-authored-by: MliKiowa <61873808+MliKiowa@users.noreply.github.com> Co-authored-by: Fridemn <150212937+Fridemn@users.noreply.github.com> Co-authored-by: BakaCookie520 <138355736+BakaCookie520@users.noreply.github.com> Co-authored-by: YumeYuka <125112916+YumeYuka@users.noreply.github.com> Co-authored-by: xming521 <32786500+xming521@users.noreply.github.com> Co-authored-by: ywh555hhh <121592812+ywh555hhh@users.noreply.github.com> Co-authored-by: stevessr <89645372+stevessr@users.noreply.github.com> Co-authored-by: roeseth <41995115+roeseth@users.noreply.github.com> Co-authored-by: ikun-1145141 <265925499+ikun-1145141@users.noreply.github.com> Co-authored-by: evpeople <54983536+evpeople@users.noreply.github.com> Co-authored-by: Yue-bin <60509781+Yue-bin@users.noreply.github.com> Co-authored-by: W1ndys <109416673+W1ndys@users.noreply.github.com> Co-authored-by: TheFurina <218887821+TheFurina@users.noreply.github.com> Co-authored-by: Seayon <12275933+Seayon@users.noreply.github.com> Co-authored-by: OnlyblackTea <38585636+OnlyblackTea@users.noreply.github.com> Co-authored-by: ocetars <74854972+ocetars@users.noreply.github.com> Co-authored-by: railgun19457 <117180744+railgun19457@users.noreply.github.com> Co-authored-by: JunieXD <107397009+JunieXD@users.noreply.github.com> Co-authored-by: advent259141 <197440256+advent259141@users.noreply.github.com> Co-authored-by: Doge2077 <91442300+Doge2077@users.noreply.github.com> Co-authored-by: Bocity <23430545+Bocity@users.noreply.github.com> Co-authored-by: Aurora-xk <192227833+Aurora-xk@users.noreply.github.com>
211 lines
7.4 KiB
Markdown
211 lines
7.4 KiB
Markdown
|
||
# 插件配置
|
||
|
||
随着插件功能的增加,可能需要定义一些配置以让用户自定义插件的行为。
|
||
|
||
AstrBot 提供了”强大“的配置解析和可视化功能。能够让用户在管理面板上直接配置插件,而不需要修改代码。
|
||
|
||
## 配置定义
|
||
|
||
要注册配置,首先需要在您的插件目录下添加一个 `_conf_schema.json` 的 json 文件。
|
||
|
||
文件内容是一个 `Schema`(模式),用于表示配置。Schema 是 json 格式的,例如上图的 Schema 是:
|
||
|
||
```json
|
||
{
|
||
"token": {
|
||
"description": "Bot Token",
|
||
"type": "string",
|
||
},
|
||
"sub_config": {
|
||
"description": "测试嵌套配置",
|
||
"type": "object",
|
||
"hint": "xxxx",
|
||
"items": {
|
||
"name": {
|
||
"description": "testsub",
|
||
"type": "string",
|
||
"hint": "xxxx"
|
||
},
|
||
"id": {
|
||
"description": "testsub",
|
||
"type": "int",
|
||
"hint": "xxxx"
|
||
},
|
||
"time": {
|
||
"description": "testsub",
|
||
"type": "int",
|
||
"hint": "xxxx",
|
||
"default": 123
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
- `type`: **此项必填**。配置的类型。支持 `string`, `text`, `int`, `float`, `bool`, `object`, `list`, `dict`, `template_list`。当类型为 `text` 时,将会可视化为一个更大的可拖拽宽高的 textarea 组件,以适应大文本。
|
||
- `description`: 可选。配置的描述。建议一句话描述配置的行为。
|
||
- `hint`: 可选。配置的提示信息,表现在上图中右边的问号按钮,当鼠标悬浮在问号按钮上时显示。
|
||
- `obvious_hint`: 可选。配置的 hint 是否醒目显示。如上图的 `token`。
|
||
- `default`: 可选。配置的默认值。如果用户没有配置,将使用默认值。int 是 0,float 是 0.0,bool 是 False,string 是 "",object 是 {},list 是 []。
|
||
- `items`: 可选。如果配置的类型是 `object`,需要添加 `items` 字段。`items` 的内容是这个配置项的子 Schema。理论上可以无限嵌套,但是不建议过多嵌套。
|
||
- `invisible`: 可选。配置是否隐藏。默认是 `false`。如果设置为 `true`,则不会在管理面板上显示。
|
||
- `options`: 可选。一个列表,如 `"options": ["chat", "agent", "workflow"]`。提供下拉列表可选项。
|
||
- `editor_mode`: 可选。是否启用代码编辑器模式。需要 AstrBot >= `v3.5.10`, 低于这个版本不会报错,但不会生效。默认是 false。
|
||
- `editor_language`: 可选。代码编辑器的代码语言,默认为 `json`。
|
||
- `editor_theme`: 可选。代码编辑器的主题,可选值有 `vs-light`(默认), `vs-dark`。
|
||
- `_special`: 可选。用于调用 AstrBot 提供的可视化提供商选取、人格选取、知识库选取等功能,详见下文。
|
||
|
||
其中,如果启用了代码编辑器,效果如下图所示:
|
||
|
||

|
||
|
||

|
||
|
||
**_special** 字段仅 v4.0.0 之后可用。目前支持填写 `select_provider`, `select_provider_tts`, `select_provider_stt`, `select_persona`,用于让用户快速选择用户在 WebUI 上已经配置好的模型提供商、人设等数据。结果均为字符串。以 select_provider 为例,将呈现以下效果:
|
||
|
||

|
||
|
||
### file 类型的 schema
|
||
|
||
在 v4.13.0 之后引入,允许插件定义文件上传配置项,引导用户上传插件所需的文件。
|
||
|
||
```json
|
||
{
|
||
"demo_files": {
|
||
"type": "file",
|
||
"description": "Uploaded files for demo",
|
||
"default": [], // 支持多文件上传,默认值为一个空列表
|
||
"file_types": ["pdf", "docx"] // 允许上传的文件类型列表
|
||
}
|
||
}
|
||
```
|
||
|
||
### dict 类型的 schema
|
||
|
||
用于可视化编辑一个 Python 的 dict 类型的配置。如 AstrBot Core 中的自定义请求体参数配置项:
|
||
|
||
```py
|
||
"custom_extra_body": {
|
||
"description": "自定义请求体参数",
|
||
"type": "dict",
|
||
"items": {},
|
||
"hint": "用于在请求时添加额外的参数,如 temperature、top_p、max_tokens 等。",
|
||
"template_schema": { # 可选填写 template schema,当设置之后,用户可以透过 WebUI 快速编辑。
|
||
"temperature": {
|
||
"name": "Temperature",
|
||
"description": "温度参数",
|
||
"hint": "控制输出的随机性,范围通常为 0-2。值越高越随机。",
|
||
"type": "float",
|
||
"default": 0.6,
|
||
"slider": {"min": 0, "max": 2, "step": 0.1},
|
||
},
|
||
"top_p": {
|
||
"name": "Top-p",
|
||
"description": "Top-p 采样",
|
||
"hint": "核采样参数,范围通常为 0-1。控制模型考虑的概率质量。",
|
||
"type": "float",
|
||
"default": 1.0,
|
||
"slider": {"min": 0, "max": 1, "step": 0.01},
|
||
},
|
||
"max_tokens": {
|
||
"name": "Max Tokens",
|
||
"description": "最大令牌数",
|
||
"hint": "生成的最大令牌数。",
|
||
"type": "int",
|
||
"default": 8192,
|
||
},
|
||
},
|
||
}
|
||
```
|
||
|
||
### template_list 类型的 schema
|
||
|
||
> [!NOTE]
|
||
> v4.10.4 引入。更多信息请查看:[#4208](https://github.com/AstrBotDevs/AstrBot/pull/4208)
|
||
|
||
插件开发者可以在_conf_schema中按照以下格式添加模板配置项(有点类似于原有的嵌套配置)
|
||
|
||
```json
|
||
"field_id": {
|
||
"type": "template_list",
|
||
"description": "Template List Field",
|
||
"templates": {
|
||
"template_1": {
|
||
"name": "Template One",
|
||
"hint":"hint",
|
||
"items": {
|
||
"attr_a": {
|
||
"description": "Attribute A",
|
||
"type": "int",
|
||
"default": 10
|
||
},
|
||
"attr_b": {
|
||
"description": "Attribute B",
|
||
"hint": "This is a boolean attribute",
|
||
"type": "bool",
|
||
"default": true
|
||
}
|
||
}
|
||
},
|
||
"template_2": {
|
||
"name": "Template Two",
|
||
"hint":"hint",
|
||
"items": {
|
||
"attr_c": {
|
||
"description": "Attribute A",
|
||
"type": "int",
|
||
"default": 10
|
||
},
|
||
"attr_d": {
|
||
"description": "Attribute B",
|
||
"hint": "This is a boolean attribute",
|
||
"type": "bool",
|
||
"default": true
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
保存后的 config 为
|
||
|
||
```json
|
||
"field_id": [
|
||
{
|
||
"__template_key": "template_1",
|
||
"attr_a": 10,
|
||
"attr_b": true
|
||
},
|
||
{
|
||
"__template_key": "template_2",
|
||
"attr_c": 10,
|
||
"attr_d": true
|
||
}
|
||
]
|
||
```
|
||
|
||
<img width="1000" alt="image" src="https://github.com/user-attachments/assets/74876d30-11a4-491b-a7a0-8ebe8d603782" />
|
||
|
||
## 在插件中使用配置
|
||
|
||
AstrBot 在载入插件时会检测插件目录下是否有 `_conf_schema.json` 文件,如果有,会自动解析配置并保存在 `data/config/<plugin_name>_config.json` 下(依照 Schema 创建的配置文件实体),并在实例化插件类时传入给 `__init__()`。
|
||
|
||
```py
|
||
from astrbot.api import AstrBotConfig
|
||
|
||
class ConfigPlugin(Star):
|
||
def __init__(self, context: Context, config: AstrBotConfig): # AstrBotConfig 继承自 Dict,拥有字典的所有方法
|
||
super().__init__(context)
|
||
self.config = config
|
||
print(self.config)
|
||
|
||
# 支持直接保存配置
|
||
# self.config.save_config() # 保存配置
|
||
```
|
||
|
||
## 配置更新
|
||
|
||
您在发布不同版本更新 Schema 时,AstrBot 会递归检查 Schema 的配置项,自动为缺失的配置项添加默认值、移除不存在的配置项。
|