@fastgpt-plugin/sdk-factory 0.0.1-alpha.4 → 0.0.1-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@fastgpt-plugin/sdk-factory",
3
- "version": "0.0.1-alpha.4",
3
+ "version": "0.0.1-alpha.5",
4
4
  "devDependencies": {
5
5
  "tsdown": "^0.21.10"
6
6
  },
7
7
  "type": "module",
8
8
  "files": [
9
9
  "dist",
10
- "README.md"
10
+ "README.md",
11
+ "skills"
11
12
  ],
12
13
  "exports": {
13
14
  ".": {
@@ -0,0 +1,31 @@
1
+ ---
2
+ name: fastgpt-plugin-development
3
+ description: Use as the entry skill when a user wants to develop a FastGPT plugin. Identify the plugin category, then route to the matching specialized skill such as fastgpt-system-tool-development.
4
+ ---
5
+
6
+ # FastGPT 插件开发入口
7
+
8
+ 在用户要开发 FastGPT 插件时先使用这个 skill。它只负责判断插件类型和选择后续规范,具体实现规则放在对应专项 skill 中。
9
+
10
+ ## 类型判断
11
+
12
+ 先确认用户要开发的插件类型:
13
+
14
+ - 系统工具:通过 `@fastgpt-plugin/sdk-factory` 暴露 `defineTool()` 或 `defineToolSet()`,由 FastGPT 作为工具调用。
15
+ - 其他插件类型:按后续新增的专项 skill 处理。
16
+
17
+ 当前已支持:
18
+
19
+ - `fastgpt-system-tool-development`: 系统工具插件开发规范,文件位于 `../fastgpt-system-tool-development/SKILL.md`。
20
+
21
+ ## 路由规则
22
+
23
+ - 用户提到系统工具、tool、tool-suite、工具调用、`defineTool()`、`defineToolSet()`、`createToolHandler()` 时,继续读取 `../fastgpt-system-tool-development/SKILL.md`。
24
+ - 用户只说“开发插件”且没有说明类型时,先根据需求判断是否属于系统工具;能判断时直接进入对应专项 skill。
25
+ - 无法判断插件类型时,先询问用户插件要暴露成哪类能力,再进入对应专项 skill。
26
+
27
+ ## 通用原则
28
+
29
+ - 入口 skill 不承载具体插件代码模板。
30
+ - 专项 skill 负责说明插件怎么写、目录包含哪些内容、通过什么工具写和验证。
31
+ - 新增插件类型时,在这里补一条类型说明和对应 skill 名称。
@@ -0,0 +1,200 @@
1
+ ---
2
+ name: fastgpt-sdk-factory
3
+ description: Use when creating, reviewing, or modifying FastGPT Tool Plugins with the @fastgpt-plugin/sdk-factory TypeScript SDK, including defineTool, defineToolSet, createToolHandler, Zod input/output schemas, secretSchema, ctx.invoke host calls, and streaming tool responses.
4
+ ---
5
+
6
+ # FastGPT SDK Factory
7
+
8
+ Use this skill when working on a FastGPT Tool Plugin that imports `@fastgpt-plugin/sdk-factory` or the local `sdk/factory` package.
9
+
10
+ ## Core Pattern
11
+
12
+ Plugin entry files default-export the instance returned by `defineTool()` or `defineToolSet()`.
13
+
14
+ ```ts
15
+ import { createToolHandler, defineTool } from '@fastgpt-plugin/sdk-factory';
16
+ import z from 'zod';
17
+
18
+ const handler = createToolHandler({
19
+ inputSchema: z.object({
20
+ text: z.string()
21
+ }),
22
+ outputSchema: z.object({
23
+ result: z.string()
24
+ }),
25
+ handler: async (input) => ({
26
+ result: input.text.toUpperCase()
27
+ })
28
+ });
29
+
30
+ export default defineTool({
31
+ manifest: {
32
+ pluginId: 'uppercase',
33
+ version: '1.0.0',
34
+ name: {
35
+ en: 'Uppercase',
36
+ 'zh-CN': '转大写'
37
+ },
38
+ description: {
39
+ en: 'Convert text to uppercase',
40
+ 'zh-CN': '将文本转换为大写'
41
+ },
42
+ versionDescription: {
43
+ en: 'Initial version',
44
+ 'zh-CN': '初始版本'
45
+ },
46
+ tags: ['tools']
47
+ },
48
+ handler
49
+ });
50
+ ```
51
+
52
+ ## Handler Rules
53
+
54
+ - Define handlers with `createToolHandler({ inputSchema, outputSchema, secretSchema?, handler })`.
55
+ - Use `z.object(...)` for `inputSchema` and `outputSchema` so TypeScript can infer `input` and return types.
56
+ - Return an object that matches `outputSchema`; throw errors for failed operations.
57
+ - Add `secretSchema` when plugin configuration needs secrets such as API keys.
58
+ - Read secrets from `ctx.secrets`; the value is typed from `secretSchema`.
59
+ - Use `ctx.streamResponse({ type: 'answer', content })` to emit incremental visible output before the final response.
60
+ - Use `ctx.systemVar` only for FastGPT-provided runtime variables.
61
+
62
+ ```ts
63
+ const secretSchema = z.object({
64
+ apiKey: z.string()
65
+ });
66
+
67
+ const handler = createToolHandler({
68
+ inputSchema: z.object({
69
+ query: z.string()
70
+ }),
71
+ outputSchema: z.object({
72
+ answer: z.string()
73
+ }),
74
+ secretSchema,
75
+ handler: async (input, ctx) => {
76
+ ctx.streamResponse({
77
+ type: 'answer',
78
+ content: `Searching: ${input.query}`
79
+ });
80
+
81
+ return {
82
+ answer: `Result for ${input.query}`
83
+ };
84
+ }
85
+ });
86
+ ```
87
+
88
+ ## Manifest Rules
89
+
90
+ `manifest` must include these fields unless the surrounding package already supplies them:
91
+
92
+ - `pluginId`: stable unique plugin id.
93
+ - `version`: plugin version, usually semver.
94
+ - `name`: i18n object shaped as `{ en, 'zh-CN' }`.
95
+ - `description`: i18n object shaped as `{ en, 'zh-CN' }`.
96
+
97
+ Common optional fields:
98
+
99
+ - `versionDescription`
100
+ - `author`
101
+ - `repoUrl`
102
+ - `tutorialUrl`
103
+ - `tags`
104
+ - `permission`
105
+ - `icon`
106
+ - `toolDescription`
107
+
108
+ Keep `pluginId`, child tool `id`, input field names, and output field names stable for compatibility.
109
+
110
+ ## Tool Sets
111
+
112
+ Use `defineToolSet()` for one plugin with multiple child tools. Put shared metadata in the top-level `manifest`; put each child tool's `id`, `name`, `description`, optional `icon`, optional `toolDescription`, and `handler` in `children`.
113
+
114
+ ```ts
115
+ import { createToolHandler, defineToolSet } from '@fastgpt-plugin/sdk-factory';
116
+ import z from 'zod';
117
+
118
+ const secretSchema = z.object({
119
+ apiKey: z.string()
120
+ });
121
+
122
+ const searchHandler = createToolHandler({
123
+ inputSchema: z.object({ query: z.string() }),
124
+ outputSchema: z.object({ items: z.array(z.string()) }),
125
+ secretSchema,
126
+ handler: async (input) => ({ items: [input.query] })
127
+ });
128
+
129
+ export default defineToolSet({
130
+ secretSchema,
131
+ manifest: {
132
+ pluginId: 'text-tools',
133
+ version: '1.0.0',
134
+ name: {
135
+ en: 'Text Tools',
136
+ 'zh-CN': '文本工具集'
137
+ },
138
+ description: {
139
+ en: 'Search and summarize text',
140
+ 'zh-CN': '搜索和总结文本'
141
+ }
142
+ },
143
+ children: [
144
+ {
145
+ id: 'search',
146
+ name: {
147
+ en: 'Search',
148
+ 'zh-CN': '搜索'
149
+ },
150
+ description: {
151
+ en: 'Search text',
152
+ 'zh-CN': '搜索文本'
153
+ },
154
+ toolDescription: 'Search text by query',
155
+ handler: searchHandler
156
+ }
157
+ ]
158
+ });
159
+ ```
160
+
161
+ ## Host Invocation
162
+
163
+ Use `ctx.invoke` for host capabilities. The SDK exposes `userInfo()` and `uploadFile()`. These methods return a `Result` tuple shaped as `[result, err]`; check `err` before using `result`.
164
+
165
+ ```ts
166
+ const uploadHandler = createToolHandler({
167
+ inputSchema: z.object({
168
+ content: z.string()
169
+ }),
170
+ outputSchema: z.object({
171
+ accessURL: z.string(),
172
+ fileName: z.string(),
173
+ size: z.number()
174
+ }),
175
+ handler: async (input, { invoke }) => {
176
+ const [result, err] = await invoke.uploadFile({
177
+ fileName: 'result.txt',
178
+ contentType: 'text/plain',
179
+ file: Buffer.from(input.content, 'utf-8')
180
+ });
181
+
182
+ if (err || !result) {
183
+ throw new Error('Failed to upload file');
184
+ }
185
+
186
+ return {
187
+ accessURL: result.accessURL,
188
+ fileName: result.fileName,
189
+ size: result.size
190
+ };
191
+ }
192
+ });
193
+ ```
194
+
195
+ ## Local Development
196
+
197
+ - In external plugins, import from `@fastgpt-plugin/sdk-factory`.
198
+ - Inside this monorepo package tests or fixtures, imports may point at `sdk/factory/src` or `../../src/index` when matching existing local patterns.
199
+ - Build the SDK with `pnpm --filter @fastgpt-plugin/sdk-factory build`.
200
+ - When changing SDK behavior, check `sdk/factory/src/index.ts`, `sdk/factory/src/tool-factory.ts`, `sdk/factory/src/invoke.client.ts`, and fixtures under `sdk/factory/test/fixtures/`.
@@ -0,0 +1,225 @@
1
+ ---
2
+ name: fastgpt-system-tool-development
3
+ description: Use when writing a FastGPT system tool plugin with @fastgpt-plugin/sdk-factory and @fastgpt-plugin/cli, including tool structure, SDK writing rules, required files, avatar/logo naming, and build/check/pack tooling.
4
+ ---
5
+
6
+ # FastGPT 系统工具开发规范
7
+
8
+ 在编写 FastGPT 系统工具插件时使用这个 skill。目标是指导代理用 `@fastgpt-plugin/sdk-factory` 写出符合 FastGPT 约定的系统工具,并用 `@fastgpt-plugin/cli` 创建、构建、检查和打包。
9
+
10
+ ## 使用工具
11
+
12
+ 同时参考这些能力:
13
+
14
+ - `fastgpt-sdk-factory`: 处理 `defineTool()`、`defineToolSet()`、`createToolHandler()`、Zod schema、`secretSchema`、`ctx.invoke`、`ctx.streamResponse()` 等 SDK 写法。
15
+ - `cli-usage`: 使用 `@fastgpt-plugin/cli` 的 `create`、`build`、`check`、`pack` 命令。
16
+
17
+ 常用命令:
18
+
19
+ ```bash
20
+ npx @fastgpt-plugin/cli create <plugin-name> --type tool --cwd <target-directory>
21
+ ```
22
+
23
+ ```bash
24
+ npx @fastgpt-plugin/cli create <plugin-name> --type tool-suite --cwd <target-directory>
25
+ ```
26
+
27
+ ```bash
28
+ npx @fastgpt-plugin/cli build --entry <plugin-directory> --output <plugin-directory>/dist
29
+ ```
30
+
31
+ ```bash
32
+ npx @fastgpt-plugin/cli check --entry <plugin-directory> --output <plugin-directory>/dist
33
+ ```
34
+
35
+ ```bash
36
+ npx @fastgpt-plugin/cli pack --entry <plugin-directory> --dist <plugin-directory>/dist --output <plugin-directory>/out
37
+ ```
38
+
39
+ ## 系统工具包含内容
40
+
41
+ 一个系统工具目录通常包含:
42
+
43
+ - `index.ts`: 系统工具入口,默认导出 `defineTool()` 或 `defineToolSet()` 的返回值。
44
+ - `package.json`: 声明依赖和 `build`、`build:dev`、`pack`、`test` 脚本。
45
+ - `logo.<ext>`: 主工具头像,必须放在工具根目录。
46
+ - `README.md`: 工具用途、输入、输出、密钥配置和使用说明。
47
+ - `assets/`: 可选资源目录,例如图标或示例资源。
48
+ - `dist/`: CLI 构建输出目录,包含 `index.js`、`manifest.json` 和必要资源。
49
+ - `out/`: CLI 打包输出目录,通常包含 `.pkg` 文件。
50
+
51
+ 依赖建议:
52
+
53
+ - 运行依赖:`@fastgpt-plugin/sdk-factory`、`zod`。
54
+ - 开发依赖:`@fastgpt-plugin/cli`、`typescript`、`vitest`。
55
+
56
+ ## 系统工具写法
57
+
58
+ 入口文件必须默认导出 SDK factory 实例:
59
+
60
+ - 单工具使用 `defineTool()`。
61
+ - 多个相关工具组成一个插件时使用 `defineToolSet()`。
62
+ - 每个 handler 用 `createToolHandler()` 定义。
63
+ - `inputSchema` 和 `outputSchema` 使用 `z.object(...)`。
64
+ - handler 返回值必须匹配 `outputSchema`。
65
+
66
+ manifest 至少包含:
67
+
68
+ - `pluginId`: 稳定唯一工具 ID。
69
+ - `version`: 工具版本。
70
+ - `name`: `{ en, 'zh-CN' }` 格式。
71
+ - `description`: `{ en, 'zh-CN' }` 格式。
72
+
73
+ 常用可选字段:
74
+
75
+ - `versionDescription`
76
+ - `author`
77
+ - `repoUrl`
78
+ - `tutorialUrl`
79
+ - `tags`
80
+ - `permission`
81
+ - `icon`
82
+ - `toolDescription`
83
+
84
+ 兼容性要求:
85
+
86
+ - `pluginId`、工具 `id`、输入字段名、输出字段名对外使用后保持稳定。
87
+ - 用户配置的密钥、API Key、Base URL 等使用 `secretSchema` 描述,通过 `ctx.secrets` 读取。
88
+ - 错误信息应能帮助定位问题,同时避免暴露密钥、令牌和完整上游敏感响应。
89
+ - 需要向用户展示执行进度时使用 `ctx.streamResponse()`。
90
+ - 需要生成文件时使用 `ctx.invoke.uploadFile()`。
91
+
92
+ ## 头像规范
93
+
94
+ CLI 构建时会自动扫描系统工具根目录中的头像文件,并写入 `manifest.icon` 或子工具 `icon` 字段。工具代码里通常不用手写 `icon`。
95
+
96
+ 命名方式:
97
+
98
+ - 主工具头像:`logo.<ext>`,例如 `logo.svg`、`logo.png`。
99
+ - 工具集子工具头像:`<childId>.logo.<ext>`,例如子工具 `id` 为 `search` 时使用 `search.logo.svg`。
100
+ - 子工具没有独立头像时,CLI 会复用主工具头像。
101
+
102
+ 格式限制:
103
+
104
+ - 支持扩展名:`.svg`、`.png`、`.jpg`、`.jpeg`、`.webp`、`.gif`。
105
+ - 头像文件必须放在系统工具根目录,不能放在 `assets/` 后再通过 manifest 引用。
106
+ - `childId` 必须和 `defineToolSet({ children })` 中的子工具 `id` 完全一致。
107
+ - 避免使用空格、中文或特殊符号作为头像文件名的一部分。
108
+ - `logo.*` 和 `<childId>.logo.*` 只保留一个匹配文件,避免不同扩展名同时存在导致扫描结果不明确。
109
+ - `build` 后检查 `dist/manifest.json` 中的 `icon` 字段是否指向已复制到 `dist/` 的头像文件。
110
+
111
+ ## 单工具模板
112
+
113
+ ```ts
114
+ import { createToolHandler, defineTool } from '@fastgpt-plugin/sdk-factory';
115
+ import z from 'zod';
116
+
117
+ const handler = createToolHandler({
118
+ inputSchema: z.object({
119
+ text: z.string().min(1)
120
+ }),
121
+ outputSchema: z.object({
122
+ result: z.string()
123
+ }),
124
+ handler: async (input) => {
125
+ return {
126
+ result: input.text.trim()
127
+ };
128
+ }
129
+ });
130
+
131
+ export default defineTool({
132
+ manifest: {
133
+ pluginId: 'text-normalizer',
134
+ version: '1.0.0',
135
+ name: {
136
+ en: 'Text Normalizer',
137
+ 'zh-CN': '文本规范化'
138
+ },
139
+ description: {
140
+ en: 'Normalize input text',
141
+ 'zh-CN': '规范化输入文本'
142
+ },
143
+ versionDescription: {
144
+ en: 'Initial version',
145
+ 'zh-CN': '初始版本'
146
+ }
147
+ },
148
+ handler
149
+ });
150
+ ```
151
+
152
+ ## 工具集模板
153
+
154
+ ```ts
155
+ import { createToolHandler, defineToolSet } from '@fastgpt-plugin/sdk-factory';
156
+ import z from 'zod';
157
+
158
+ const secretSchema = z.object({
159
+ apiKey: z.string().min(1)
160
+ });
161
+
162
+ const searchHandler = createToolHandler({
163
+ inputSchema: z.object({
164
+ query: z.string().min(1)
165
+ }),
166
+ outputSchema: z.object({
167
+ items: z.array(z.string())
168
+ }),
169
+ secretSchema,
170
+ handler: async (input, ctx) => {
171
+ ctx.streamResponse({
172
+ type: 'answer',
173
+ content: `Searching: ${input.query}`
174
+ });
175
+
176
+ return {
177
+ items: []
178
+ };
179
+ }
180
+ });
181
+
182
+ export default defineToolSet({
183
+ secretSchema,
184
+ manifest: {
185
+ pluginId: 'example-search-tools',
186
+ version: '1.0.0',
187
+ name: {
188
+ en: 'Example Search Tools',
189
+ 'zh-CN': '示例搜索工具集'
190
+ },
191
+ description: {
192
+ en: 'Search public example data',
193
+ 'zh-CN': '搜索公开示例数据'
194
+ }
195
+ },
196
+ children: [
197
+ {
198
+ id: 'search',
199
+ name: {
200
+ en: 'Search',
201
+ 'zh-CN': '搜索'
202
+ },
203
+ description: {
204
+ en: 'Search by query',
205
+ 'zh-CN': '按查询搜索'
206
+ },
207
+ toolDescription: 'Search public example data by query',
208
+ handler: searchHandler
209
+ }
210
+ ]
211
+ });
212
+ ```
213
+
214
+ ## 验证重点
215
+
216
+ - `index.ts` 默认导出正确。
217
+ - `manifest` 字段完整,国际化字段包含 `en` 和 `zh-CN`。
218
+ - Zod schema 覆盖全部用户可见输入和输出。
219
+ - handler 成功路径返回值与 `outputSchema` 一致。
220
+ - 外部调用失败、空响应、超时、鉴权失败都有明确错误。
221
+ - 密钥配置只通过 `secretSchema` 和 `ctx.secrets` 处理。
222
+ - 系统工具根目录存在 `logo.<ext>`。
223
+ - 工具集子工具头像按 `<childId>.logo.<ext>` 命名,或确认复用主头像。
224
+ - `build` 后存在 `dist/index.js` 和 `dist/manifest.json`。
225
+ - `check` 通过后再执行 `pack`。