koishijs / koishi

Cross-platform chatbot framework made with love
https://koishi.chat
MIT License
4.36k stars 236 forks source link

Bug: 无法在控制台启用自己创建的插件 #1020

Closed Ice-Hazymoon closed 1 year ago

Ice-Hazymoon commented 1 year ago

Describe the bug

dev 模式,在 web 控制台中添加的插件,添加后,点击启用按钮没有任何反应,重启 koishi 依旧无法点击启用按钮启用插件。

image

浏览器控制台输出:

Uncaught (in promise) TypeError: missing required value
    at Function.resolve (E:\Project\bot\bot\node_modules\schemastery\src\index.ts:261:37)
    at null.property (E:\Project\bot\bot\node_modules\schemastery\src\index.ts:386:35)
    at null.<anonymous> (E:\Project\bot\bot\node_modules\schemastery\src\index.ts:434:19)
    at Function.resolve (E:\Project\bot\bot\node_modules\schemastery\src\index.ts:273:24)
    at null.schema (E:\Project\bot\bot\node_modules\schemastery\src\index.ts:104:19)
    at null.resolveConfig (E:\Project\bot\bot\node_modules\cordis\src\utils.ts:19:54)
    at NodeLoader.forkPlugin (E:\Project\bot\bot\node_modules\@koishijs\loader\src\shared.ts:213:5)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at NodeLoader.reloadPlugin (E:\Project\bot\bot\node_modules\@koishijs\loader\src\shared.ts:240:16)
    at ConfigWriter.reload (E:\Project\bot\bot\node_modules\@koishijs\plugin-market\src\shared\writer.ts:150:5)

koishi 终端输出(点了好几次)

2023-03-08 22:38:47 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:47 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:47 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:48 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:48 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:48 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:48 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:38:49 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:39:19 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:39:20 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:40:39 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:41:46 [I] app apply plugin chatgpt:ohvxr8
2023-03-08 22:41:47 [I] app apply plugin chatgpt:ohvxr8

插件代码:

import { Context, Schema } from 'koishi'

export const name = 'chatgpt'

export interface Config {
  apiKey: string
}

export const Config: Schema<Config> = Schema.object({
  apiKey: Schema.string().required().description("OpenAI API Key"),
})

export function apply(ctx: Context) {
  // write your plugin here
  ctx.middleware(async (session, next) => {
    console.log('session', session)
    next();
  })
}

koishi.yml

  ~chatgpt:ohvxr8:
    apiKey: sk-xxxxxx
    $filter:
      $eq:
        - $: guildId
        - '111111111'

在 web 段修改配置并保存,可以正常更新 koishi.yml

package.json

{
    "@koishijs/plugin-adapter-discord": "^3.5.7",
    "@koishijs/plugin-adapter-feishu": "^1.2.2",
    "@koishijs/plugin-adapter-kook": "^3.6.2",
    "@koishijs/plugin-adapter-onebot": "^5.6.1",
    "@koishijs/plugin-adapter-telegram": "^3.7.0",
    "@koishijs/plugin-admin": "^1.1.6",
    "@koishijs/plugin-analytics": "^1.0.3",
    "@koishijs/plugin-bind": "^1.3.3",
    "@koishijs/plugin-commands": "^3.0.0",
    "@koishijs/plugin-console": "^5.5.3",
    "@koishijs/plugin-database-mysql": "5.3.6",
    "@koishijs/plugin-database-sqlite": "^3.3.11",
    "@koishijs/plugin-dataview": "^2.2.4",
    "@koishijs/plugin-help": "^2.2.0",
    "@koishijs/plugin-insight": "^3.3.3",
    "@koishijs/plugin-locales": "^2.2.1",
    "@koishijs/plugin-logger": "^2.2.3",
    "@koishijs/plugin-market": "^1.12.2",
    "@koishijs/plugin-rate-limit": "^1.2.8",
    "@koishijs/plugin-sandbox": "^2.7.0",
    "@koishijs/plugin-status": "^7.1.2",
    "koishi": "^4.11.9",
    "koishi-plugin-assets-local": "^3.0.3",
    "koishi-plugin-desktop": "^0.0.3",
    "koishi-plugin-gocqhttp": "^3.3.4",
    "koishi-plugin-puppeteer": "^3.4.11",
    "koishi-plugin-text-censor": "1.0.2"
  }

devDependencies

{
    "@koishijs/client": "^5.5.3",
    "@koishijs/scripts": "^4.1.3",
    "@types/node": "^18.14.2",
    "cross-env": "^7.0.3",
    "esbuild": "^0.17.10",
    "esbuild-register": "npm:@shigma/esbuild-register@^1.1.1",
    "typescript": "^4.9.5",
    "yakumo": "^0.3.9",
    "yakumo-esbuild": "^0.3.22",
    "yakumo-esbuild-yaml": "^0.3.1",
    "yakumo-publish": "^0.3.3",
    "yakumo-publish-sync": "^0.3.2",
    "yakumo-tsc": "^0.3.7",
    "yakumo-upgrade": "^0.3.3",
    "yakumo-version": "^0.3.3",
    "yml-register": "^1.1.0"
  }

其他:这是一个刚创建的 koishi 项目,改动很少

Steps to reproduce

如上

Expected behavior

如上

Screenshots

No response

Versions

Additional context

No response

Seidko commented 1 year ago

你配置了一个必选的选项,你没有填写这个选项,所以插件不能启动。

Seidko commented 1 year ago

你需要卸载这个插件并且填写这个必选配置项。

Ice-Hazymoon commented 1 year ago

Schema.string().required().default(xx),required 的优先级比 default 高吗,已经有 default了,还必须在 koishi.yml 中手动再配置一下吗

shigma commented 1 year ago

required 不能和 default 同时使用,否则必定报错。

Ice-Hazymoon commented 1 year ago

果然,修改配置文件强制启动后 Schema 模块报错了,但是哪个字段出错 Schema 也没说。

shigma commented 1 year ago

你可以去给 schemastery 提个 issue,我可以加个冲突报错之类。

Ice-Hazymoon commented 1 year ago

建议优化错误输出,控制台一直 app apply plugin chatgpt:ohvxr8,没有任何错误提示,向我这样的菜鸡容易懵。

Ice-Hazymoon commented 1 year ago

如果是 schemastery 部分出错,启动失败的话控制台应该打印个错误,告诉开发者哪个地方有问题。目前这样很难定位错误

shigma commented 1 year ago

理解你的困扰。但我们显然不能预判所有人可能遇到的所有错误写法,所以我们只能遇到一个解决一个。你的反馈是有意义的。

shigma commented 1 year ago

很遗憾,我刚刚研究了一下代码,发现 default + required 的用法在某些极端情况下是有意义的。现在有两种方案:

  1. 当存在 default 时不考虑 required -> 会破坏部分现有代码的行为
  2. 当设置 required 时移除默认值 -> 你的情况下还是会报错

我花了几个小时尝试了各种方案,目前没有任何办法简单地修改现有源码,使之通过现有单元测试并且在你的情况下不报错。

附上 default + required 相关的测试用例:

image

相关尝试在分支:https://github.com/shigma/schemastery/tree/default-required

shigma commented 1 year ago

额外补充一点,在过去我们也同样遇到了类似的问题报告。并不是所有开发者遇到的问题都应该简单地归结为「我看不懂报错就一定是框架没有做好错误引导」,一方面我们有更多的开发者在阅读文档后根本不会遇到这样的问题,另一方面即使遇到了我们也有成熟的社区提供帮助。遇到一个此类问题就去寻求解决对于官方是一件费力不讨好的工程。昨天我花费了数个小时仍然无法解决,至少在这个问题上我认为我尽到了责任,但对于未来的问题我不一定还有这种耐心做类似的事情。

Ice-Hazymoon commented 1 year ago

我认为可以在 koishi 的 schema 文档中对 required 和 default 加以说明(当然也可能是我没看到

shigma commented 1 year ago

感谢建议,已经加了: koishijs/docs@83a217580f3b88c3312b45dd14a78164ff5e4fc4