Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add json schema definition #35

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ test-output

client.d.ts
client.js
bun.lockb
bun.lockb
schema.json
36 changes: 36 additions & 0 deletions docs/guides/plugin-manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ manifest 聚合了插件功能如何实现的信息。核心的字段为 `api`

```json
{
"$schema": "../node_modules/@lobehub/chat-plugin-sdk/schema.json",
"api": [
{
"url": "http://localhost:3400/api/clothes",
Expand Down Expand Up @@ -116,3 +117,38 @@ const JSONSchema = z.object({
## API 与 Schema

关于 manifest 各个字段的完整介绍,参见:[manifest](/api/plugin-manifest)。

## JSON 类型提示

SDK 提供了 manifest 的 JSON Schema 定义,它可以用于在编写 `manifest.json` 文件时为 IDE 提供类型信息和智能提示。

使用时你只需为 JSON 配置文件声明 `$schema` 字段来指向 schema 定义文件即可,以 [lobehub/chat-plugin-template](https://github.com/lobehub/chat-plugin-template/blob/main/public/manifest-dev.json) 为例,它的项目结构为:

```plaintext
lobehub/chat-plugin-template
├── CHANGELOG.md
├── node_modules
├── README.md
├── src
├── public
│ ├── foo.json
│ ├── manifest-dev.json
│ └── manifest-standalone.json
└── package.json
```

那么 `manifest-dev.json` 的 `$schema` 字段可以配置为这样的相对路径:

```json filename=manifest-dev.json
{
"$schema": "../node_modules/@lobehub/chat-plugin-sdk/schema.json",
"api": [],
"gateway": "http://localhost:3400/api/gateway",
"identifier": "plugin-identifier",
"ui": {
"url": "http://localhost:3400",
"height": 200
},
"version": "1"
}
```
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
"lib",
"es",
"client.d.ts",
"client.js"
"client.js",
"schema.json"
],
"scripts": {
"build": "father build && npm run build:client",
"build": "father build && npm run build:client && npm run build:schema",
"build:client": "tsc client.ts --declaration",
"build:schema": "ts-json-schema-generator --path src/types/manifest.ts --type LobeChatPluginManifest -o schema.json",
"ci": "npm run lint && npm run type-check && npm run doctor",
"dev": "father dev",
"docs:build": "dumi build",
Expand Down Expand Up @@ -102,6 +104,7 @@
"remark-cli": "^11",
"semantic-release": "^21",
"stylelint": "^15",
"ts-json-schema-generator": "^1.4.0",
"typescript": "^5",
"vitest": "latest"
},
Expand Down
2 changes: 2 additions & 0 deletions src/types/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface LobeChatPluginApi {
* @descCN 描述一个插件的构成要素
*/
export interface LobeChatPluginManifest {
$schema?: string;
api: LobeChatPluginApi[];
gateway?: string;
/**
Expand Down Expand Up @@ -68,4 +69,5 @@ export interface LobeChatPluginManifest {
url: string;
width?: number;
};
version?: '1';
}
Loading