Skip to content

Commit bbd8117

Browse files
c121914yunewfish-cmykFinleyGe
committed
move system plugin config (#5803) (#5813)
* move system plugin config (#5803) * move system plugin config * extract tag bar * filter * tool detail temp * marketplace * params * fix * type * search * tags render * status * ui * code * connect to backend (#5815) * feat: marketplace apis & type definitions (#5817) * chore: marketplace init * chore: marketplace list api type * chore: detail api * marketplace & import * feat: marketplace ui (#5826) * temp * marketplace * import * feat: detail return readme * chore: cache data expire 10 mins * chore: update docs * feat: marketplace ui --------- Co-authored-by: heheer <[email protected]> * feat: marketplace (#5830) * temp * marketplace * chore: tool list tag filter * chore: adjust --------- Co-authored-by: heheer <[email protected]> * tool detail drawer * remove tag filter * fix * fix * fix build * update pnpm-lock * fix type * perf code * marketplace router * fix build * navbar icon * fix ui * fix init * docs: marketplace/plugin (#5832) * temp * marketplace * docs(plugin): system tool docs --------- Co-authored-by: heheer <[email protected]> * default url * feat: i18n/ docker build (#5833) * chore: docker build * feat: i18n selector * fix * fix * fix: i18n parse * fix: i18n parse --------- Co-authored-by: heheer <[email protected]> Co-authored-by: Finley Ge <[email protected]> Co-authored-by: heheer <[email protected]>
1 parent 9862899 commit bbd8117

File tree

132 files changed

+9390
-1677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+9390
-1677
lines changed

.eslintignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ vitest.config.mts
2323
bin/
2424
scripts/
2525
deploy/
26-
document/
26+
document/
27+
28+
projects/marketplace

.github/workflows/marketplace.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Build and Deploy Marketplace
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
base_url:
7+
description: 'Base URL for the application'
8+
required: false
9+
default: ''
10+
type: string
11+
tag:
12+
description: 'Docker image tag'
13+
required: false
14+
default: 'latest'
15+
type: string
16+
17+
env:
18+
REGISTRY: ghcr.io
19+
IMAGE_NAME: ${{ github.repository }}/marketplace
20+
21+
jobs:
22+
build:
23+
runs-on: ubuntu-latest
24+
permissions:
25+
contents: read
26+
packages: write
27+
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v4
31+
32+
- name: Setup Node.js
33+
uses: actions/setup-node@v4
34+
with:
35+
node-version: '20.14.0'
36+
cache: 'pnpm'
37+
38+
- name: Install pnpm
39+
run: npm install -g [email protected]
40+
41+
- name: Install dependencies
42+
run: pnpm install
43+
44+
- name: Log in to Container Registry
45+
if: github.event_name != 'pull_request'
46+
uses: docker/login-action@v3
47+
with:
48+
registry: ${{ env.REGISTRY }}
49+
username: ${{ github.actor }}
50+
password: ${{ secrets.GITHUB_TOKEN }}
51+
52+
- name: Extract metadata
53+
id: meta
54+
uses: docker/metadata-action@v5
55+
with:
56+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
57+
tags: |
58+
type=ref,event=branch
59+
type=ref,event=pr
60+
type=raw,value=${{ github.event.inputs.tag || 'latest' }}
61+
62+
- name: Build and push Docker image
63+
uses: docker/build-push-action@v5
64+
with:
65+
context: .
66+
file: ./projects/marketplace/Dockerfile
67+
push: ${{ github.event_name != 'pull_request' }}
68+
tags: ${{ steps.meta.outputs.tags }}
69+
labels: ${{ steps.meta.outputs.labels }}
70+
build-args: |
71+
base_url=${{ github.event.inputs.base_url || '' }}
72+
proxy=${{ vars.PROXY || '' }}
73+
cache-from: type=gha
74+
cache-to: type=gha,mode=max
75+
76+
deploy:
77+
runs-on: ubuntu-latest
78+
needs: build
79+
if: github.event_name == 'workflow_dispatch'
80+
81+
steps:
82+
- name: Deploy notification
83+
run: |
84+
echo "🚀 Marketplace image has been built and pushed to registry"
85+
echo "📦 Image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.event.inputs.tag || 'latest' }}"
86+
echo "🌐 Base URL: ${{ github.event.inputs.base_url || 'not specified' }}"

document/content/docs/introduction/development/design/design_plugin.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ description: FastGPT 系统插件设计方案
3232

3333
1. 使用 ts-rest 作为 RPC 框架进行交互,提供 sdk 供 FastGPT 主项目调用
3434
2. 使用 zod 进行类型验证
35-
3. 用 bun 进行编译,每个工具编译为单一的 `.js` 文件,支持热插拔。
35+
3. 用 bun 进行编译,每个工具编译为单一的 `.pkg` 文件,支持热插拔。
3636

3737
## 项目结构
3838

@@ -48,7 +48,8 @@ description: FastGPT 系统插件设计方案
4848
- **model** 模型预设
4949
- **scripts** 脚本(编译、创建新工具)
5050
- **sdk**: SDK 定义,供外部调用,发布到了 npm
51-
- **src**: 运行时,express 服务
51+
- **runtime**: 运行时,express 服务
52+
- **lib**: 库文件,提供工具函数和类库
5253
- **test**: 测试相关
5354

5455
系统工具的结构可以参考 [如何开发系统工具](/docs/introduction/guide/plugins/dev_system_tool)
@@ -78,7 +79,7 @@ zod 可以实现在运行时的类型校验,也可以提供更高级的功能
7879

7980
### 使用 bun 进行打包
8081

81-
将插件 bundle 为一个单一的 `.js` 文件是一个重要的设计。这样可以将插件发布出来直接通过网络挂载等的形式使用。
82+
将插件 bundle 为一个单一的 `.pkg` 文件是一个重要的设计。这样可以将插件发布出来直接通过网络挂载等的形式使用。
8283

8384
## 未来规划
8485

document/content/docs/introduction/guide/plugins/dev_system_tool.mdx

Lines changed: 120 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ description: FastGPT 系统工具开发指南
55

66
## 介绍
77

8-
FastGPT 系统工具项目从 4.10.0 版本后移动到独立的`fastgpt-plugin`项目中,采用纯代码的模式进行工具编写。你可以在`fastgpt-plugin`项目中进行独立开发和调试好插件后,直接向 FastGPT 官方提交 PR 即可,无需运行 FastGPT 主服务。
8+
FastGPT 系统工具项目从 4.10.0 版本后移动到独立的`fastgpt-plugin`项目中,采用纯代码的模式进行工具编写。
9+
在 4.14.0 版本插件市场更新后,系统工具开发流程有所改变,请依照最新文档贡献代码。
10+
你可以在`fastgpt-plugin`项目中进行独立开发和调试好插件后,直接向 FastGPT 官方提交 PR 即可,无需运行 FastGPT 主服务。
911

1012
## 概念
1113

@@ -14,29 +16,74 @@ FastGPT 系统工具项目从 4.10.0 版本后移动到独立的`fastgpt-plugin`
1416

1517
`fastgpt-plugin`中,你可以每次创建一个工具/工具集,每次提交时,仅接收一个工具/工具集。如需开发多个,可以创建多个 PR 进行提交。
1618

17-
## 1. 准备工作
19+
## 1. 准备开发环境
1820

19-
- Fork [fastgpt-plugin 项目](https://github.com/labring/fastgpt-plugin)
20-
- 安装 [Bun](https://bun.sh/)
21-
- 部署一套 Minio,也可以直接使用 FastGPT 的 `docker-compose.yml` 中的 Minio。
22-
- 本地 clone 项目 `git clone [email protected]:[your-github-username]/fastgpt-plugin.git`
23-
- 拷贝示例环境变量文件,并修改连接到开发环境的 Minio `cp .env.example .env.local`
24-
- 安装依赖 `bun install`
25-
- 运行开发环境 `bun run dev`
21+
### 1.1 安装 Bun
2622

27-
在 dev 环境下,Bun 将监听修改并热更新。
23+
- 安装 [Bun](https://bun.sh/), FastGPT-plugin 使用 Bun 作为包管理器
2824

29-
## 2. 初始化一个新的工具/工具集
25+
### 1.2 Fork FastGPT-plugin 仓库
3026

31-
### 2.1 执行创建命令
27+
Fork 本仓库 `https://github.com/labring/fastgpt-plugin`
3228

29+
### 1.3 搭建开发脚手架
30+
31+
<Tabs items={['通过 Bunx 一键搭建','手动搭建']}>
32+
<Tab value="通过 Bunx 一键搭建">
33+
注意:由于使用了 bun 特有的 API,必须使用 bunx 进行安装,使用 npx/npx 等会报错
34+
35+
创建一个新的目录,在该目录下执行:
36+
```bash
37+
bunx @fastgpt-sdk/plugin-cli
38+
```
39+
40+
上述命令会在当前目录下创建 fastgpt-plugin 目录,并且添加两个 remote:
41+
- upstream 指向官方仓库
42+
- origin 指向你自己的仓库
43+
44+
默认使用 sparse-checkout 避免拉取所有的官方插件代码
45+
46+
</Tab>
47+
<Tab value="手动搭建">
48+
- 本地在一个新建目录下初始化一个 `git`:
49+
50+
```bash
51+
git init
52+
```
53+
54+
如果配置了 Git SSH Key, 则可以:
55+
```bash
56+
git remote add origin [email protected]:[your-name]/fastgpt-plugin.git
57+
git remote add upstream [email protected]:labring/fastgpt-plugin.git
58+
```
59+
60+
否则使用 https:
61+
```bash
62+
git remote add origin https://github.com/[your-name]/fastgpt-plugin.git
63+
git remote add upstream https://github.com/labring/fastgpt-plugin.git
64+
```
65+
66+
- (可选)使用稀疏检出 (Sparse-checkout) 以避免拉取所有插件代码,如果不进行稀疏检出,则会拉取所有官方插件
67+
```bash
68+
git sparse-checkout init --no-cone
69+
git sparse-checkout add "/*" "!/modules/tool/packages/*"
70+
git pull
71+
```
72+
73+
使用命令创建新工具
3374
```bash
75+
bun i
3476
bun run new:tool
3577
```
3678

37-
依据提示分别选择创建工具/工具集,以及目录名(使用 camelCase 小驼峰法命名)。
79+
</Tab>
80+
</Tabs>
81+
82+
## 2. 编写工具代码
3883

39-
执行完后,系统会在 `modules/tool/packages/[your-tool-name]` 下生成一个工具/工具集的目录。
84+
### 2.1 工具代码结构
85+
86+
依据提示分别选择创建工具/工具集,以及目录名(使用 camelCase 小驼峰法命名)。
4087

4188
系统工具 (Tool) 文件结构如下:
4289

@@ -48,26 +95,30 @@ test // 测试样例
4895
config.ts // 配置,配置工具的名称、描述、类型、图标等
4996
index.ts // 入口,不要改这个文件
5097
logo.svg // Logo,替换成你的工具的 Logo
98+
README.md // (可选)README 文件,用于展示工具的使用说明和示例
99+
assets/ // (可选)assets 目录,用于存放工具的资源文件,如图片、音频等
51100
package.json // npm 包
52101
```
53102

54103
工具集(toolset) 的文件结构如下:
55104

56105
```plaintext
57106
children
58-
└── tool // 这个里面的结构就和上面的 tool 基本一致
107+
└── tool // 这个里面的结构就和上面的 tool 一致,但是没有 README 和 assets 目录
59108
config.ts
60109
index.ts
61110
logo.svg
111+
README.md
112+
assets/
62113
package.json
63114
```
64115

65116
### 2.2 修改 config.ts
66117

67118
- **name****description** 字段为中文和英文两种语言
68-
- **courseUrl** 密钥获取链接,或官网链接,教程链接等
119+
- **courseUrl**(可选) 密钥获取链接,或官网链接,教程链接等,如果提供 README.md,则可以写到 README 里面
69120
- **author** 开发者名
70-
- **type** 为枚举类型,目前有:
121+
- **tags** 工具默认的标签,有如下可选标签(枚举类型)
71122
- tools: 工具
72123
- search: 搜索
73124
- multimodal: 多模态
@@ -204,7 +255,7 @@ dalle3 的 outputs 参数格式如下:
204255
}
205256
```
206257

207-
## 2. 编写处理逻辑
258+
### 2.3 编写处理逻辑
208259

209260
`[your-tool-name]/src/index.ts` 为入口编写处理逻辑,需要注意:
210261

@@ -234,30 +285,70 @@ export async function tool(props: z.infer<typeof InputType>): Promise<z.infer<ty
234285

235286
上述例子给出了一个传入 formatStr (格式化字符串)并且返回当前时间的简单样例,如需安装包,可以在`/modules/tools/packages/[your-tool-name]`路径下,使用`bun install PACKAGE` 进行安装。
236287

237-
## 3. 调试
288+
## 4. 构建/打包
289+
290+
FastGPT v4.14.0 后,打包方式变为系统插件打包为一个 `.pkg` 文件,使用命令:
291+
```bash
292+
bun run build:pkg
293+
```
294+
将本地所有插件构建打包为 `.pkg` 文件,构建目录为 `dist/pkgs`
295+
296+
## 5. 单元测试
297+
298+
FastGPT-plugin 使用 Vitest 作为单测框架。
299+
300+
### 5.1 编写单测样例
238301

239-
### 单测
302+
`test/index.test.ts` 中编写测试样例,使用 `bun run test index.test.ts 完整路径` 即可运行测试。
240303

241-
`test/index.test.ts` 中编写测试样例,使用 `bun run test index.test.ts完整路径` 即可运行测试。
304+
> 注意:不要把你的 secret 密钥等写到测试样例中
305+
>
306+
> 使用 Agent 工具编写测试样例时,可能 Agent 工具会修改您的处理逻辑甚至修改整个测试框架的逻辑。
242307
243-
### 从 Scalar 进行测试
308+
### 5.2 查看测试样例覆盖率(coverage)
244309

245-
浏览器打开`localhost:3000/openapi`可进入`fastgpt-plugin`的 OpenAPI 页面,进行 API 调试。
310+
浏览器打开 coverage/index.html 可以插件各个模块的覆盖率
311+
312+
提交插件给官方仓库,必须编写单元测试样例,并且达到:
313+
- 90% 以上代码覆盖率
314+
- 100% 函数覆盖率
315+
- 100% 分支条件覆盖率
316+
317+
## 6. E2E (端到端)测试
318+
319+
对于简单的工具,可能并不需要进行 E2E 测试,而如果工具过于复杂,官方人员可能会要求您完成 E2E 测试。
320+
321+
### 6.1 部署 E2E 测试环境
322+
323+
1. 参考 [快速开始本地开发](/docs/introduction/development/intro),在本地部署一套 FastGPT 开发环境
324+
2. `cd runtime && cp .env.template .env.local` 复制环境变量样例文件,连接到上一步部署的 Minio, Mongo, Redis 中
325+
3. `bun run dev` 运行开发环境,修改 FastGPT 的环境变量,连接到你刚刚启动的 fastgpt-plugin
326+
327+
### 6.2 从 Scalar 进行测试
328+
329+
运行 fastgpt-plugin 开发环境
330+
331+
浏览器打开`http://localhost:PORT/openapi`可进入`fastgpt-plugin`的 OpenAPI 页面,进行 API 调试。
332+
PORT 为你的 fastgpt-plugin 的端口
246333

247334
![](/imgs/plugin-openapi.png)
248335

249336
可以先通过`/tool/list`接口,获取工具列表,找到需要调试的工具的`toolId`。紧接着,通过`/tool/runStream`来运行工具获取实际结果。
250337

251338
![](/imgs/plugin-openapi2.png)
252339

253-
### 从 FastGPT 主服务进行测试
340+
### 6.3 在开发环境下 e2e 测试(有热更新)
341+
342+
默认情况下,fastgpt-plugin 会自动加载在 modules/tool/packages/ 下的所有工具,并自动监听文件修改并进行热更新。
343+
可以在 FastGPT 中使用这些工具
254344

255-
如果本地运行有`FastGPT`主服务,则可以直接添加对应的工具进行测试。
345+
### 6.4 在开发环境下上传工具进行 e2e 测试(没有热更新)
256346

257-
### 可视化调试(TODO)
347+
设置 FastGPT-plugin 的环境变量 `DISABLE_DEV_TOOLS=true` 会禁用自动加载开发环境下的工具,此时可以测试工具的上传。
258348

259-
## 4. 提交工具至官方目录
349+
## 7. 提交工具至官方目录
260350

261-
完毕上述所有内容后,向官方仓库 `https://github.com/labring/fastgpt-plugin` 提交 PR。官方人员审核通过后即可收录为 FastGPT 的官方插件。
351+
完毕上述所有内容后,向官方仓库 `https://github.com/labring/fastgpt-plugin` 提交 PR。
352+
官方人员审核通过后即可收录为 FastGPT 的官方插件。
262353

263-
如无需官方收录,可自行对该项目进行 Docker 打包,并替换官方镜像即可
354+
如无需官方收录,则可以参考 [上传系统工具](upload_system_tool) 在自己部署的 FastGPT 中使用

0 commit comments

Comments
 (0)