@gencode/plugin-sdk 0.0.3 → 0.0.4

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 ADDED
@@ -0,0 +1,214 @@
1
+ # `@gencode/plugin-sdk`
2
+
3
+ `@gencode/plugin-sdk` 提供插件作者需要的稳定类型导出,避免直接依赖 `@gencode/agents` 的内部文件路径。
4
+
5
+ ## 最小插件结构
6
+
7
+ ```text
8
+ my-plugin/
9
+ ├── aimax.plugin.json
10
+ └── index.ts
11
+ ```
12
+
13
+ `aimax.plugin.json`:
14
+
15
+ ```json
16
+ {
17
+ "id": "progress-demo",
18
+ "name": "Progress Demo",
19
+ "description": "Demonstrates custom plugin progress events.",
20
+ "version": "0.0.1",
21
+ "configSchema": {}
22
+ }
23
+ ```
24
+
25
+ `index.ts`:
26
+
27
+ ```ts
28
+ import type { PluginApi } from "@gencode/plugin-sdk";
29
+
30
+ export default function register(api: PluginApi) {
31
+ const progress = api.createProgressEmitter();
32
+
33
+ api.registerHook("session_start", async () => {
34
+ await progress.emit({
35
+ name: "progress_demo.session.started",
36
+ label: "Plugin initialized",
37
+ data: { source: "hook" },
38
+ });
39
+ });
40
+ }
41
+ ```
42
+
43
+ ## `createProgressEmitter`
44
+
45
+ 插件现在可以通过 `api.createProgressEmitter()` 主动发出自定义进度事件。
46
+
47
+ ```ts
48
+ const progress = api.createProgressEmitter();
49
+
50
+ await progress.emit({
51
+ name: "my_plugin.step.completed",
52
+ label: "Downloaded source data",
53
+ data: {
54
+ step: "download",
55
+ itemCount: 12,
56
+ },
57
+ });
58
+ ```
59
+
60
+ 事件字段说明:
61
+
62
+ - `name`: 稳定事件名,建议使用 `plugin_namespace.action` 或 `plugin.namespace.action`
63
+ - `label`: 面向用户的短文案,可直接展示在 Web/H5 时间线
64
+ - `data`: JSON-safe 结构化扩展字段,供 callback/WebSocket 消费方解析
65
+
66
+ 运行时约束:
67
+
68
+ - 只能在插件运行期调用,例如 hook handler 或 plugin tool 的 `execute`
69
+ - 不能在模块顶层或 `register()` 执行期间直接调用
70
+ - `pluginId`、`sessionId`、`messageId`、`depth` 由框架注入,插件不需要也不能手工传入
71
+
72
+ ## 事件流向
73
+
74
+ 插件发出的 custom progress event 会复用现有的统一事件链路:
75
+
76
+ 1. `@gencode/agents` 产出 `AgentProgressEvent { type: "custom", ... }`
77
+ 2. `@gencode/cli` 继续通过 stdout / HTTP callback / websocket 分发
78
+ 3. `@gencode/console` 将其映射为 `agent.custom`
79
+
80
+ stdout 文本输出示例:
81
+
82
+ ```text
83
+ [plugin:progress-demo] progress_demo.task.completed Task completed {"step":"run","count":3}
84
+ ```
85
+
86
+ WebSocket / callback 中的事件载荷核心字段:
87
+
88
+ ```json
89
+ {
90
+ "type": "custom",
91
+ "pluginId": "progress-demo",
92
+ "name": "progress_demo.task.completed",
93
+ "label": "Task completed",
94
+ "data": {
95
+ "step": "run",
96
+ "count": 3
97
+ }
98
+ }
99
+ ```
100
+
101
+ ## 建议
102
+
103
+ - 优先让 `name` 稳定,便于服务端和前端做事件聚合
104
+ - `label` 保持短句,适合直接展示
105
+ - `data` 放结构化上下文,不要塞大段文本
106
+ - 如果一个插件需要多个阶段事件,建议统一前缀,例如:
107
+ - `progress_demo.fetch.started`
108
+ - `progress_demo.fetch.completed`
109
+ - `progress_demo.report.generated`
110
+
111
+ ## `registerUiTool`
112
+
113
+ 插件可以通过 `api.registerUiTool()` 注册 UI 工具,让 LLM 在需要时暂停 agent 并通过前端表单收集结构化用户输入。
114
+
115
+ 与 `registerTool` 不同,插件只需要提供表单 schema 和基本元数据 — session 绑定、暂停/恢复流程、结果校验都由框架自动完成。
116
+
117
+ ### 基本用法
118
+
119
+ ```ts
120
+ import type { PluginApi } from "@gencode/plugin-sdk";
121
+
122
+ export default function register(api: PluginApi) {
123
+ api.registerUiTool({
124
+ name: "collect_feedback",
125
+ label: "收集用户反馈",
126
+ description: "Collect structured feedback from the user.",
127
+ schema: {
128
+ title: "用户反馈",
129
+ fields: [
130
+ { name: "rating", label: "评分", type: "number", required: true },
131
+ { name: "comment", label: "评论", type: "textarea" },
132
+ ],
133
+ },
134
+ });
135
+ }
136
+ ```
137
+
138
+ ### 字段类型
139
+
140
+ `schema.fields` 中每个字段的 `type` 支持以下值:
141
+
142
+ | 类型 | 说明 |
143
+ |------|------|
144
+ | `string` | 单行文本 |
145
+ | `number` | 数值 |
146
+ | `boolean` | 布尔开关 |
147
+ | `date` | 日期 |
148
+ | `select` | 单选下拉,需配合 `options` |
149
+ | `multiselect` | 多选,需配合 `options` |
150
+ | `textarea` | 多行文本 |
151
+ | `file` | 文件路径或 data URI |
152
+
153
+ ### 自定义校验
154
+
155
+ 内置 schema 校验(required、类型检查、select 选项范围)之外,可以提供 `validate` 函数做跨字段校验:
156
+
157
+ ```ts
158
+ api.registerUiTool({
159
+ name: "date_range",
160
+ label: "选择日期范围",
161
+ description: "Collect a date range with cross-field validation.",
162
+ schema: {
163
+ title: "日期范围",
164
+ fields: [
165
+ { name: "startDate", label: "开始日期", type: "date", required: true },
166
+ { name: "endDate", label: "结束日期", type: "date", required: true },
167
+ ],
168
+ },
169
+ validate(values) {
170
+ const start = values.startDate as string;
171
+ const end = values.endDate as string;
172
+ if (start && end && start > end) {
173
+ return {
174
+ valid: false,
175
+ errors: [{ field: "endDate", message: "结束日期不能早于开始日期。" }],
176
+ };
177
+ }
178
+ return { valid: true };
179
+ },
180
+ });
181
+ ```
182
+
183
+ `validate` 支持同步和异步返回。内置校验失败时不会调用 `validate`。
184
+
185
+ ### 与 `registerTool` 的区别
186
+
187
+ | 维度 | `registerTool` | `registerUiTool` |
188
+ |------|---------------|-----------------|
189
+ | 用途 | 通用工具 | 收集前端表单输入 |
190
+ | 开发者关注点 | 完整 `execute` 实现 | 只需提供表单 schema |
191
+ | sessionId | 需要自行处理 | 框架自动绑定 |
192
+ | 暂停/恢复 | 需要手动抛出信号 | 框架自动处理 |
193
+ | 结果校验 | 自行实现 | 内置 schema 校验 + 可选自定义 validator |
194
+
195
+ ### 运行流程
196
+
197
+ 1. LLM 调用 UI 工具 → 自动抛出 `UiToolPauseSignal` 暂停 agent
198
+ 2. Runner 捕获信号,发出 `ui_tool_request` 进度事件(包含表单 schema)
199
+ 3. 前端根据 schema 渲染表单
200
+ 4. 用户提交后,CLI 带 `uiToolResume` 重新调用 agent
201
+ 5. 框架自动校验数据(内置 + 自定义 validator)
202
+ 6. 校验通过 → 工具返回提交值给 LLM → agent 继续执行
203
+
204
+ ### 导出类型
205
+
206
+ `@gencode/plugin-sdk` 导出以下 UI 工具相关类型:
207
+
208
+ - `PluginUiToolDescriptor` — `registerUiTool` 的参数类型
209
+ - `PluginUiToolOptions` — 可选参数(如 `optional`)
210
+ - `UiToolSchema` — 表单 schema
211
+ - `UiToolFieldSchema` — 单个字段定义
212
+ - `UiToolFieldType` — 字段类型联合类型
213
+ - `UiToolValidationError` — 校验错误
214
+ - `UiToolValidationResult` — 校验结果
package/dist/index.d.ts CHANGED
@@ -2,9 +2,10 @@
2
2
  * @gencode/plugin-sdk
3
3
  * Thin re-export layer for plugin authors.
4
4
  */
5
- export type { PluginApi, PluginRegistry, PluginRecord, PluginDiagnostic, PluginKind, PluginOrigin, PluginManifest, PluginManifestLoadResult, PluginConfigUiHint, } from "@gencode/agents";
5
+ export type { AgentCustomProgressEvent, PluginApi, PluginRegistry, PluginRecord, PluginDiagnostic, PluginKind, PluginOrigin, PluginManifest, PluginManifestLoadResult, PluginConfigUiHint, PluginProgressEmitter, PluginCustomProgressInput, PluginUiToolDescriptor, PluginUiToolOptions, } from "@gencode/agents";
6
6
  export type { MemoryProvider, MemoryProviderContext, MemoryProviderFactory, MemoryProviderRegistration, MemorySearchOptions, MemoryChangeSource, MemoryChangedEvent, MemoryChangedHandler, } from "@gencode/agents";
7
7
  export type { EmbeddingProvider, EmbeddingProviderContext, EmbeddingProviderFactory, EmbeddingProviderRegistration, } from "@gencode/agents";
8
8
  export type { PluginHookName, PluginHookHandlerMap, PluginHookAgentContext, PluginHookBeforeModelResolveEvent, PluginHookBeforeModelResolveResult, PluginHookBeforePromptBuildEvent, PluginHookBeforePromptBuildResult, PluginHookLlmInputEvent, PluginHookLlmOutputEvent, PluginHookBeforeToolCallEvent, PluginHookBeforeToolCallResult, PluginHookAfterToolCallEvent, PluginHookAgentEndEvent, PluginHookBeforeCompactionEvent, PluginHookAfterCompactionEvent, PluginHookSessionStartEvent, PluginHookSessionEndEvent, PluginHookSessionResetEvent, PluginHookMemoryChangedEvent, } from "@gencode/agents";
9
9
  export type { PluginRuntime } from "@gencode/agents";
10
+ export type { UiToolFieldType, UiToolFieldSchema, UiToolSchema, UiToolValidationError, UiToolValidationResult, } from "@gencode/shared";
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EACV,SAAS,EACT,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,cAAc,EACd,wBAAwB,EACxB,kBAAkB,GACnB,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,0BAA0B,EAC1B,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,iBAAiB,EACjB,wBAAwB,EACxB,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,cAAc,EACd,oBAAoB,EACpB,sBAAsB,EACtB,iCAAiC,EACjC,kCAAkC,EAClC,gCAAgC,EAChC,iCAAiC,EACjC,uBAAuB,EACvB,wBAAwB,EACxB,6BAA6B,EAC7B,8BAA8B,EAC9B,4BAA4B,EAC5B,uBAAuB,EACvB,+BAA+B,EAC/B,8BAA8B,EAC9B,2BAA2B,EAC3B,yBAAyB,EACzB,2BAA2B,EAC3B,4BAA4B,GAC7B,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EACV,wBAAwB,EACxB,SAAS,EACT,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,cAAc,EACd,wBAAwB,EACxB,kBAAkB,EAClB,qBAAqB,EACrB,yBAAyB,EACzB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,0BAA0B,EAC1B,mBAAmB,EACnB,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,iBAAiB,EACjB,wBAAwB,EACxB,wBAAwB,EACxB,6BAA6B,GAC9B,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EACV,cAAc,EACd,oBAAoB,EACpB,sBAAsB,EACtB,iCAAiC,EACjC,kCAAkC,EAClC,gCAAgC,EAChC,iCAAiC,EACjC,uBAAuB,EACvB,wBAAwB,EACxB,6BAA6B,EAC7B,8BAA8B,EAC9B,4BAA4B,EAC5B,uBAAuB,EACvB,+BAA+B,EAC/B,8BAA8B,EAC9B,2BAA2B,EAC3B,yBAAyB,EACzB,2BAA2B,EAC3B,4BAA4B,GAC7B,MAAM,iBAAiB,CAAC;AAEzB,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,YAAY,EACV,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,iBAAiB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gencode/plugin-sdk",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -17,7 +17,8 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@gencode/agents": "0.0.6"
20
+ "@gencode/agents": "0.0.40",
21
+ "@gencode/shared": "0.0.12"
21
22
  },
22
23
  "devDependencies": {
23
24
  "typescript": "^5.9.3"