@workopilot/pluginapi-sdk 0.1.4 → 0.1.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/README.md +197 -145
- package/dist/index.d.ts +17 -0
- package/package.json +33 -33
package/README.md
CHANGED
|
@@ -1,145 +1,197 @@
|
|
|
1
|
-
# @workopilot/pluginapi-sdk
|
|
2
|
-
|
|
3
|
-
Workopilot Plugin API SDK defines the TypeScript contract between the host application and third-party plugins.
|
|
4
|
-
|
|
5
|
-
It provides plugin manifest types, request/handler types, database capability types, logger types, and current user context types. The SDK does not start a server or connect to databases by itself. Runtime capabilities are injected by the host application or by `@workopilot/pluginapi-dev-host` during local development.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @workopilot/pluginapi-sdk
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Usage
|
|
14
|
-
|
|
15
|
-
Create plugin handlers with the `PluginHandler` type:
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import type { PluginHandler } from '@workopilot/pluginapi-sdk';
|
|
19
|
-
|
|
20
|
-
export const getCurrentUser: PluginHandler = ({ ctx }) => ({
|
|
21
|
-
currentUser: ctx.currentUser
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
export const listItems: PluginHandler = async ({ ctx }) => {
|
|
25
|
-
const rows = await ctx.database.query('SELECT * FROM plugin_demo_items LIMIT ?', [10]);
|
|
26
|
-
return { rows };
|
|
27
|
-
};
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
Register exported handlers in the plugin manifest:
|
|
31
|
-
|
|
32
|
-
```json
|
|
33
|
-
{
|
|
34
|
-
"name": "my-plugin",
|
|
35
|
-
"version": "0.1.0",
|
|
36
|
-
"entry": "dist/index.js",
|
|
37
|
-
"routes": [
|
|
38
|
-
{
|
|
39
|
-
"method": "GET",
|
|
40
|
-
"path": "/me",
|
|
41
|
-
"handler": "getCurrentUser"
|
|
42
|
-
}
|
|
43
|
-
]
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Provided Types
|
|
48
|
-
|
|
49
|
-
### Platform Service URL
|
|
50
|
-
|
|
51
|
-
Hosts configure `PLATFORM_SERVICE_URL` as an HTTP(S) origin without `/net-api`
|
|
52
|
-
(for example `https://agent.workopilot.com`). HTTP and MCP handlers receive the
|
|
53
|
-
normalized origin through `ctx.platformServiceUrl`. It contains no database
|
|
54
|
-
credentials. `readPlatformServiceUrl(value)` validates and normalizes this setting.
|
|
55
|
-
|
|
56
|
-
### Plugin Handler
|
|
57
|
-
|
|
58
|
-
```ts
|
|
59
|
-
PluginHandler
|
|
60
|
-
PluginHandlerInput
|
|
61
|
-
PluginRequest
|
|
62
|
-
PluginContext
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
Handlers receive:
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
{
|
|
84
|
-
request,
|
|
85
|
-
ctx
|
|
86
|
-
}
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Database Capability
|
|
90
|
-
|
|
91
|
-
The host injects database access through `ctx.database`:
|
|
92
|
-
|
|
93
|
-
```ts
|
|
94
|
-
ctx.database.query(sql, params)
|
|
95
|
-
ctx.database.insert(sql, params)
|
|
96
|
-
ctx.database.update(sql, params)
|
|
97
|
-
ctx.database.delete(sql, params)
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Write operations return:
|
|
101
|
-
|
|
102
|
-
```ts
|
|
103
|
-
{
|
|
104
|
-
affectedRows,
|
|
105
|
-
insertId,
|
|
106
|
-
warningStatus,
|
|
107
|
-
changedRows,
|
|
108
|
-
info
|
|
109
|
-
}
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Current User
|
|
113
|
-
|
|
114
|
-
The host injects the authenticated user through:
|
|
115
|
-
|
|
116
|
-
```ts
|
|
117
|
-
ctx.currentUser
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Current user shape:
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
{
|
|
124
|
-
tenantId,
|
|
125
|
-
userId,
|
|
126
|
-
clientid,
|
|
127
|
-
userName,
|
|
128
|
-
deptId,
|
|
129
|
-
deptName,
|
|
130
|
-
deptCategory
|
|
131
|
-
}
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
### Logger
|
|
135
|
-
|
|
136
|
-
Plugins can write host-managed logs:
|
|
137
|
-
|
|
138
|
-
```ts
|
|
139
|
-
ctx.logger.info(message, data)
|
|
140
|
-
ctx.logger.error(message, data)
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
## Notes
|
|
144
|
-
|
|
145
|
-
Plugin packages should normally use this SDK as a TypeScript type dependency. Database credentials, JWT verification, Redis token checks, and route mounting are handled by the host side.
|
|
1
|
+
# @workopilot/pluginapi-sdk
|
|
2
|
+
|
|
3
|
+
Workopilot Plugin API SDK defines the TypeScript contract between the host application and third-party plugins.
|
|
4
|
+
|
|
5
|
+
It provides plugin manifest types, request/handler types, database capability types, logger types, and current user context types. The SDK does not start a server or connect to databases by itself. Runtime capabilities are injected by the host application or by `@workopilot/pluginapi-dev-host` during local development.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @workopilot/pluginapi-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Create plugin handlers with the `PluginHandler` type:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import type { PluginHandler } from '@workopilot/pluginapi-sdk';
|
|
19
|
+
|
|
20
|
+
export const getCurrentUser: PluginHandler = ({ ctx }) => ({
|
|
21
|
+
currentUser: ctx.currentUser
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export const listItems: PluginHandler = async ({ ctx }) => {
|
|
25
|
+
const rows = await ctx.database.query('SELECT * FROM plugin_demo_items LIMIT ?', [10]);
|
|
26
|
+
return { rows };
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Register exported handlers in the plugin manifest:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"name": "my-plugin",
|
|
35
|
+
"version": "0.1.0",
|
|
36
|
+
"entry": "dist/index.js",
|
|
37
|
+
"routes": [
|
|
38
|
+
{
|
|
39
|
+
"method": "GET",
|
|
40
|
+
"path": "/me",
|
|
41
|
+
"handler": "getCurrentUser"
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Provided Types
|
|
48
|
+
|
|
49
|
+
### Platform Service URL
|
|
50
|
+
|
|
51
|
+
Hosts configure `PLATFORM_SERVICE_URL` as an HTTP(S) origin without `/net-api`
|
|
52
|
+
(for example `https://agent.workopilot.com`). HTTP and MCP handlers receive the
|
|
53
|
+
normalized origin through `ctx.platformServiceUrl`. It contains no database
|
|
54
|
+
credentials. `readPlatformServiceUrl(value)` validates and normalizes this setting.
|
|
55
|
+
|
|
56
|
+
### Plugin Handler
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
PluginHandler
|
|
60
|
+
PluginHandlerInput
|
|
61
|
+
PluginRequest
|
|
62
|
+
PluginContext
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### MCP Tool
|
|
66
|
+
|
|
67
|
+
Plugins can optionally export an MCP tool array from the module named by `plugin.json.mcp.toolsExport` (default: `mcpTools`):
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"appCode": "my-app",
|
|
72
|
+
"mcp": {
|
|
73
|
+
"toolsExport": "mcpTools"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Each tool uses `McpToolDefinition`, provides a JSON Schema `inputSchema`, and receives the same `ctx.currentUser`, database capability, logger, and request headers as an HTTP handler. The host owns MCP transport, authentication, and appCode grouping.
|
|
79
|
+
|
|
80
|
+
Handlers receive:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
{
|
|
84
|
+
request,
|
|
85
|
+
ctx
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Database Capability
|
|
90
|
+
|
|
91
|
+
The host injects database access through `ctx.database`:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
ctx.database.query(sql, params)
|
|
95
|
+
ctx.database.insert(sql, params)
|
|
96
|
+
ctx.database.update(sql, params)
|
|
97
|
+
ctx.database.delete(sql, params)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Write operations return:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
{
|
|
104
|
+
affectedRows,
|
|
105
|
+
insertId,
|
|
106
|
+
warningStatus,
|
|
107
|
+
changedRows,
|
|
108
|
+
info
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Current User
|
|
113
|
+
|
|
114
|
+
The host injects the authenticated user through:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
ctx.currentUser
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Current user shape:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
{
|
|
124
|
+
tenantId,
|
|
125
|
+
userId,
|
|
126
|
+
clientid,
|
|
127
|
+
userName,
|
|
128
|
+
deptId,
|
|
129
|
+
deptName,
|
|
130
|
+
deptCategory
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Logger
|
|
135
|
+
|
|
136
|
+
Plugins can write host-managed logs:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
ctx.logger.info(message, data)
|
|
140
|
+
ctx.logger.error(message, data)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Notes
|
|
144
|
+
|
|
145
|
+
Plugin packages should normally use this SDK as a TypeScript type dependency. Database credentials, JWT verification, Redis token checks, and route mounting are handled by the host side.
|
|
146
|
+
|
|
147
|
+
## 文件上传(SDK 0.1.5 / 开发宿主 0.1.7 / 生产宿主 1.0.1)
|
|
148
|
+
|
|
149
|
+
生产宿主和开发宿主均支持 `multipart/form-data`。通过原有的
|
|
150
|
+
`plugin.json.routes` 注册 POST 路由,无需在插件内安装 Web 框架或解析器。
|
|
151
|
+
请求先鉴权和匹配路由,再读取上传内容。
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import type { PluginHandler } from '@workopilot/pluginapi-sdk';
|
|
155
|
+
|
|
156
|
+
export const upload: PluginHandler = async ({ request }) => {
|
|
157
|
+
const file = request.files?.find((item) => item.fieldname === 'file');
|
|
158
|
+
if (!file) {
|
|
159
|
+
return { message: '请选择文件。' };
|
|
160
|
+
}
|
|
161
|
+
// file.buffer 是 Uint8Array(Node.js 宿主实际提供 Buffer)。
|
|
162
|
+
// 在这里调用应用已有的文件处理或存储逻辑;宿主不会自动保存文件。
|
|
163
|
+
return {
|
|
164
|
+
filename: file.filename,
|
|
165
|
+
mimetype: file.mimetype,
|
|
166
|
+
size: file.size
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`request.files` 是文件数组,每项提供 `fieldname`、`filename`、`mimetype`、
|
|
172
|
+
`encoding`、`size` 和 `buffer`。相同字段名的多个文件也按上传顺序保留。
|
|
173
|
+
普通表单字段放在 `request.body` 中,重复字段为数组;使用前仍需校验和收窄类型。
|
|
174
|
+
非 multipart 请求保持原来的 `body`,不设置 `files`。
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
curl -H 'Authorization: Bearer <token>' \
|
|
178
|
+
-F 'file=@./report.pdf' -F 'title=月报' \
|
|
179
|
+
http://127.0.0.1:3100/plugins/my-plugin/upload
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
浏览器可使用 `FormData` 或前端 SDK 的 `uploadFile()`;不要手动设置
|
|
183
|
+
`Content-Type`,浏览器需要自动生成 boundary。文件名和媒体类型均由客户端提供,
|
|
184
|
+
业务代码应按用途校验,不能把文件名直接用作存储路径。文件内容只在当前请求中驻留内存。
|
|
185
|
+
|
|
186
|
+
生产宿主 `.env` 和开发宿主 `.env.local` 可配置以下正整数字节/数量限制:
|
|
187
|
+
|
|
188
|
+
| 环境变量 | 默认值 | 含义 |
|
|
189
|
+
| --- | ---: | --- |
|
|
190
|
+
| `UPLOAD_MAX_FILE_BYTES` | 20971520 | 单文件 20 MiB |
|
|
191
|
+
| `UPLOAD_MAX_FILES` | 5 | 每次请求最多 5 个文件 |
|
|
192
|
+
| `UPLOAD_MAX_TOTAL_BYTES` | 52428800 | 文件内容和解析后字段值合计 50 MiB |
|
|
193
|
+
| `UPLOAD_MAX_FIELDS` | 50 | 最多 50 个普通字段 |
|
|
194
|
+
| `UPLOAD_MAX_FIELD_BYTES` | 65536 | 单字段 64 KiB |
|
|
195
|
+
|
|
196
|
+
超过限制返回 413,格式错误返回 400,不会把截断内容交给 Handler。
|
|
197
|
+
网关自身的上传限制仍需覆盖实际业务需求。
|
package/dist/index.d.ts
CHANGED
|
@@ -44,12 +44,29 @@ export interface CurrentUser {
|
|
|
44
44
|
deptName: string;
|
|
45
45
|
deptCategory: string;
|
|
46
46
|
}
|
|
47
|
+
/** 宿主解析后的上传文件,仅在本次请求中驻留内存,不自动保存。 */
|
|
48
|
+
export interface PluginUploadedFile {
|
|
49
|
+
/** 表单文件字段名。 */
|
|
50
|
+
fieldname: string;
|
|
51
|
+
/** 客户端提供的文件名,不可直接作为存储路径。 */
|
|
52
|
+
filename: string;
|
|
53
|
+
/** 客户端声明的媒体类型。 */
|
|
54
|
+
mimetype: string;
|
|
55
|
+
/** 文件传输编码。 */
|
|
56
|
+
encoding: string;
|
|
57
|
+
/** 文件实际字节数。 */
|
|
58
|
+
size: number;
|
|
59
|
+
/** 文件内容,Node.js 宿主实际提供 Buffer。 */
|
|
60
|
+
buffer: Uint8Array;
|
|
61
|
+
}
|
|
47
62
|
export interface PluginRequest {
|
|
48
63
|
method: string;
|
|
49
64
|
url: string;
|
|
50
65
|
query: unknown;
|
|
51
66
|
body: unknown;
|
|
52
67
|
headers: Record<string, string | string[] | undefined>;
|
|
68
|
+
/** multipart 请求的文件列表;普通字段位于 body,重复字段为数组。 */
|
|
69
|
+
files?: PluginUploadedFile[];
|
|
53
70
|
}
|
|
54
71
|
export interface PluginContext {
|
|
55
72
|
/** 智能体平台服务域名,由宿主配置并注入,不包含 /net-api。 */
|
package/package.json
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@workopilot/pluginapi-sdk",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Workopilot plugin API SDK for database-capability plugins",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"wiseai",
|
|
7
|
-
"workopilot",
|
|
8
|
-
"plugin",
|
|
9
|
-
"sdk"
|
|
10
|
-
],
|
|
11
|
-
"author": "workopilot",
|
|
12
|
-
"license": "MIT",
|
|
13
|
-
"type": "module",
|
|
14
|
-
"main": "dist/index.js",
|
|
15
|
-
"module": "dist/index.js",
|
|
16
|
-
"types": "dist/index.d.ts",
|
|
17
|
-
"exports": {
|
|
18
|
-
".": {
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
|
-
"import": "./dist/index.js"
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
"files": [
|
|
24
|
-
"dist",
|
|
25
|
-
"README.md"
|
|
26
|
-
],
|
|
27
|
-
"scripts": {
|
|
28
|
-
"build": "tsc -p tsconfig.json"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"typescript": "^5.7.2"
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@workopilot/pluginapi-sdk",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "Workopilot plugin API SDK for database-capability plugins",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"wiseai",
|
|
7
|
+
"workopilot",
|
|
8
|
+
"plugin",
|
|
9
|
+
"sdk"
|
|
10
|
+
],
|
|
11
|
+
"author": "workopilot",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.js",
|
|
15
|
+
"module": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.json"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"typescript": "^5.7.2"
|
|
32
|
+
}
|
|
33
|
+
}
|