@prisflow/proactiveai-plugin-types 0.1.0

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.
Files changed (3) hide show
  1. package/README.md +58 -0
  2. package/index.d.ts +292 -0
  3. package/package.json +18 -0
package/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # @prisflow/proactiveai-plugin-types
2
+
3
+ ProactiveAI 宿主插件 API 的类型契约(纯类型,无运行时代码)。
4
+
5
+ 插件开发者通过它获得 `setup(api)` 的全部类型提示与编译检查。
6
+
7
+ ## 安装
8
+
9
+ ```bash
10
+ npm i -D @prisflow/proactiveai-plugin-types
11
+ ```
12
+
13
+ ## 插件入口(单 JS 文件,CJS)
14
+
15
+ ```js
16
+ const plugin = {
17
+ id: 'my-plugin',
18
+ name: '我的插件',
19
+ version: '1.0.0',
20
+ setup(api) {
21
+ api.registerContext({ contextId: 'my-ctx', role: 'sub', initialPrompt: '…' })
22
+ api.registerTool({ name: 'my_tool', description: '…', run(input, meta) { return { ok: true, result: input } } })
23
+ api.flow.register({ name: 'my_flow', nodes: [{ type: 'static', fn: () => { } }, …] })
24
+ },
25
+ }
26
+ module.exports = plugin
27
+ ```
28
+
29
+ ## 插件包(zip)格式
30
+
31
+ ```
32
+ <plugin>.zip
33
+ ├── plugin.json # 元数据:id/name/version/entry/minAppVersion(见 PluginManifest)
34
+ └── <entry>.js # 入口(CJS,如上)
35
+ ```
36
+
37
+ 应用内「设置 → 插件 → 导入插件」选择 zip 即可安装。
38
+
39
+ ## API 速览(PluginSetupAPI)
40
+
41
+ | 能力 | API |
42
+ |---|---|
43
+ | 注册上下文 | `api.registerContext(def)` |
44
+ | 注册工具 | `api.registerTool(def)` |
45
+ | 持久化存储 | `api.storage.get() / api.storage.set(data)` |
46
+ | 分层记忆 | `api.memory.set/get/search/remove` |
47
+ | LLM 生成 | `api.llm.generate({ system, input, schema })` |
48
+ | Flow 图 | `api.flow.register(def) / api.flow.run(name, input)` |
49
+ | 提示词注入 | `api.prompts.inject('prefix' | 'suffix', text)` |
50
+ | 压缩配置 | `api.compaction.configure(cfg)` |
51
+
52
+ ## 发布(维护者)
53
+
54
+ `plugin-types/` 是独立 npm 包(`publishConfig.access: public`)。发布前需确认:
55
+
56
+ 1. `index.d.ts` 为**自包含声明**(不 import 宿主相对路径)——当前版本已内联冻结
57
+ 2. 宿主类型演进时,同步更新 `index.d.ts` 并 bump 版本
58
+ 3. 发布:`cd plugin-types && npm publish --access pulic`面向外部
package/index.d.ts ADDED
@@ -0,0 +1,292 @@
1
+ /**
2
+ * @prisflow/proactiveai-plugin-types
3
+ *
4
+ * ProactiveAI 宿主插件 API 的类型契约(自包含,可独立发布 npm)。
5
+ * 插件开发者安装本包后获得 setup(api) 的全部类型提示与编译检查。
6
+ *
7
+ * 使用:
8
+ * npm i -D @prisflow/proactiveai-plugin-types
9
+ *
10
+ * 插件入口(单 JS 文件,CJS):
11
+ * const plugin = {
12
+ * id: 'my-plugin',
13
+ * name: '我的插件',
14
+ * version: '1.0.0',
15
+ * setup(api) { api.registerContext(...); api.registerTool(...); },
16
+ * }
17
+ * module.exports = plugin
18
+ *
19
+ * 分发:zip 包内 plugin.json + 入口 js(见 PluginManifest)。
20
+ */
21
+
22
+ /** semver 版本号。 */
23
+ export type SemVer = string
24
+
25
+ /** 插件定义。单 JS 文件,`module.exports = { id, name, version, setup }`。 */
26
+ export interface Plugin {
27
+ /** 插件唯一 ID(与 plugin.json id 一致)。 */
28
+ id: string
29
+ /** 人类可读名称。 */
30
+ name: string
31
+ /** semver 版本号。 */
32
+ version: string
33
+ /** 描述。 */
34
+ description?: string
35
+ /** 安装钩子:宿主加载文件后调用,在此注册上下文/工具/Flow 等。 */
36
+ setup(api: PluginSetupAPI): void
37
+ }
38
+
39
+ /** 插件包元数据(zip 根目录 plugin.json)。 */
40
+ export interface PluginManifest {
41
+ /** 插件唯一 ID,必须与 JS 内 plugin.id 一致。 */
42
+ id: string
43
+ /** 展示名。 */
44
+ name: string
45
+ /** semver 版本号,必须与 JS 内 version 一致。 */
46
+ version: string
47
+ /** 描述。 */
48
+ description?: string
49
+ /** zip 内入口文件名(默认 'index.js')。 */
50
+ entry?: string
51
+ /** 宿主最低版本(大于则拒绝安装)。 */
52
+ minAppVersion?: string
53
+ /** 作者。 */
54
+ author?: string
55
+ /** 下载来源(如 COS 地址),展示用。 */
56
+ homepage?: string
57
+ }
58
+
59
+ /** 上下文角色。 */
60
+ export type ContextRole = 'main' | 'sub'
61
+
62
+ /** 压缩层配置(per-context,未配置字段走全局默认)。 */
63
+ export interface ContextCompactionConfig {
64
+ /** 压缩器系统提示词。 */
65
+ summaryPrompt?: string
66
+ /** 摘要写入的记忆 slot 名(缺省 'summary')。 */
67
+ summarySlot?: string
68
+ /** 摘要头部标签文本(如 【剧情史】)。 */
69
+ summaryLabel?: string
70
+ /** 触发压缩的 token 预算(缺省 60000)。 */
71
+ tokenBudget?: number
72
+ /** 压缩后保留的最近消息估算 token 数(缺省 8000)。 */
73
+ keepTokens?: number
74
+ /** 稳定前缀构建:可追加自定义记忆 slot(world_setting 等)。 */
75
+ prefixSlots?: string[]
76
+ /** 是否启用再压缩(摘要超预算时摘要的摘要)。缺省 true。 */
77
+ allowResummarize?: boolean
78
+ }
79
+
80
+ /** 上下文注册描述 —— 插件或内置模块注册时提供。 */
81
+ export interface ContextDefinition {
82
+ /** 上下文唯一 ID。 */
83
+ contextId: string
84
+ /** 上下文角色。 */
85
+ role: ContextRole
86
+ /** 进入此上下文时注入到系统提示的文本。 */
87
+ initialPrompt?: string
88
+ /** 供主上下文 host_enter_subcontext 选路时展示的简短描述。 */
89
+ description?: string
90
+ /** 此上下文中可见的工具名列表。 */
91
+ toolNames?: string[]
92
+ /** 压缩层配置(per-context)。 */
93
+ compaction?: ContextCompactionConfig
94
+ }
95
+
96
+ /** 工具调用上下文(调用方传入)。 */
97
+ export interface ToolCallMeta {
98
+ conversationId?: string
99
+ contextId?: string
100
+ /** 触发本次工具调用的 LLM 轮次 runId(日志链路树)。 */
101
+ parentRunId?: string
102
+ }
103
+
104
+ /** 工具执行结果。 */
105
+ export type ToolResult =
106
+ | { ok: true; result: unknown }
107
+ | { ok: false; error: string }
108
+
109
+ /** transformPrompt 的产出:工具结果转换为 LLM 历史与事件回灌的统一形态。 */
110
+ export interface ToolPromptResult {
111
+ success: { toolName: string; error?: string }
112
+ instruction?: string
113
+ result?: { text?: string; ui?: string }
114
+ }
115
+
116
+ /** 静默工具:执行后不入事件总线,无需 transformPrompt。 */
117
+ export interface SilentToolDef {
118
+ name: string
119
+ description: string
120
+ inputSchema?: Record<string, unknown>
121
+ run: (input: Record<string, unknown>, meta: ToolCallMeta) => ToolResult | Promise<ToolResult>
122
+ silent: true
123
+ transformPrompt?: undefined
124
+ }
125
+
126
+ /** 非静默工具:执行后产生事件入总线,必须提供 transformPrompt。 */
127
+ export interface NonSilentToolDef {
128
+ name: string
129
+ description: string
130
+ inputSchema?: Record<string, unknown>
131
+ run: (input: Record<string, unknown>, meta: ToolCallMeta) => ToolResult | Promise<ToolResult>
132
+ silent?: false
133
+ transformPrompt: (result: ToolResult) => ToolPromptResult
134
+ }
135
+
136
+ /** 工具定义 —— 注册时需满足对应的 silent/transformPrompt 约束。 */
137
+ export type ToolDefinition = SilentToolDef | NonSilentToolDef
138
+
139
+ /** LLM API 连接配置。 */
140
+ export interface LlmConfig {
141
+ apiKey: string
142
+ model: string
143
+ baseURL?: string
144
+ }
145
+
146
+ /** LLM 消息(OpenAI Chat Completion 单条)。 */
147
+ export interface LlmMessage {
148
+ role: 'system' | 'user' | 'assistant' | 'tool'
149
+ content: string
150
+ tool_call_id?: string
151
+ tool_calls?: Array<{ id: string; type: 'function'; function: { name: string; arguments: string } }>
152
+ }
153
+
154
+ /** OpenAI tools 参数中的单个工具定义。 */
155
+ export interface LlmToolDef {
156
+ type: 'function'
157
+ function: {
158
+ name: string
159
+ description: string
160
+ parameters: Record<string, unknown>
161
+ }
162
+ }
163
+
164
+ /** LLM 单次调用结果。 */
165
+ export type LlmResult =
166
+ | { kind: 'text'; text: string }
167
+ | {
168
+ kind: 'tool_calls'
169
+ toolCalls: Array<{ id: string; name: string; args: string }>
170
+ }
171
+
172
+ /** 流式推送中的数据块。 */
173
+ export type StreamChunk =
174
+ | { kind: 'delta'; delta: string }
175
+ | { kind: 'done'; finishReason: 'stop' }
176
+ | { kind: 'done'; finishReason: 'tool_calls'; toolCalls: Array<{ id: string; name: string; args: string }> }
177
+
178
+ /** 图执行共享上下文(Flow 节点)。 */
179
+ export interface FlowCtx {
180
+ conversationId?: string
181
+ contextId?: string
182
+ signal?: AbortSignal
183
+ history?: LlmMessage[]
184
+ input: unknown
185
+ state: Record<string, unknown>
186
+ data: Record<string, unknown>
187
+ push: (payload: { kind: 'ui_render'; component: string; props: Record<string, unknown>; children?: unknown[] }) => void
188
+ rendered: boolean
189
+ }
190
+
191
+ /** LLM 生成节点。 */
192
+ export interface LlmNode {
193
+ type: 'llm'
194
+ system: string
195
+ input: (ctx: FlowCtx) => string
196
+ schema?: Record<string, unknown>
197
+ assign?: string
198
+ maxTries?: number
199
+ maxTokens?: number
200
+ }
201
+
202
+ /** 静态节点:纯函数(校验/记账/门控)。 */
203
+ export interface StaticNode {
204
+ type: 'static'
205
+ fn: (ctx: FlowCtx) => string | void
206
+ }
207
+
208
+ /** 渲染终止节点。 */
209
+ export interface RenderNode {
210
+ type: 'render'
211
+ build: (ctx: FlowCtx) => { component: string; props: Record<string, unknown>; children?: unknown[] }
212
+ }
213
+
214
+ /** 条件分支节点。 */
215
+ export interface ConditionNode {
216
+ type: 'condition'
217
+ when: (ctx: FlowCtx) => boolean
218
+ then: FlowNode[]
219
+ else?: FlowNode[]
220
+ }
221
+
222
+ /** Flow 节点联合。 */
223
+ export type FlowNode = LlmNode | StaticNode | RenderNode | ConditionNode
224
+
225
+ /** 图定义。 */
226
+ export interface FlowDefinition {
227
+ name: string
228
+ nodes: FlowNode[]
229
+ requireRender?: boolean
230
+ }
231
+
232
+ /** 图执行结果。 */
233
+ export interface FlowResult {
234
+ ok: boolean
235
+ error?: string
236
+ data: Record<string, unknown>
237
+ state: Record<string, unknown>
238
+ rendered: boolean
239
+ }
240
+
241
+ /** 插件 setup(api) 拿到的宿主 API。 */
242
+ export interface PluginSetupAPI {
243
+ /** 注册一个子上下文到全局 ContextRegistry。 */
244
+ registerContext(def: ContextDefinition): boolean
245
+ /** 注册一个工具到全局 ToolRegistry。 */
246
+ registerTool(def: ToolDefinition): boolean
247
+ /** 插件持久化存储(SQLite plugin_data 表,按插件 ID 一行,值任意 JSON)。 */
248
+ storage: {
249
+ /** 读取插件持久化数据,无记录返回 null。 */
250
+ get(): unknown
251
+ /** 整体覆盖写入插件持久化数据。 */
252
+ set(data: unknown): void
253
+ }
254
+ /** 宿主通用记忆层(host_memory 表,按会话+上下文隔离)。 */
255
+ memory: {
256
+ set(slot: string, data: string, opts?: { conversationId?: string; contextId?: string }): void
257
+ get(slot: string, opts?: { conversationId?: string; contextId?: string }): string | null
258
+ search(query: string, opts?: { conversationId?: string; contextId?: string }): Array<{ slot: string; data: string }>
259
+ remove(slot: string, opts?: { conversationId?: string; contextId?: string }): boolean
260
+ }
261
+ /** 宿主 LLM 能力:结构化生成(schema 校验失败自动重试)。 */
262
+ llm: {
263
+ generate(input: {
264
+ system: string
265
+ input: string
266
+ schema?: Record<string, unknown>
267
+ maxTries?: number
268
+ }): Promise<{ ok: true; text: string; data: unknown } | { ok: false; error: string }>
269
+ }
270
+ /** 回合执行器(图):工具的 run() 内部实现设施。 */
271
+ flow: {
272
+ /** 注册一张图(节点链)。 */
273
+ register(def: FlowDefinition): boolean
274
+ /** 执行一张图,渲染经 push 通道推送并落库。 */
275
+ run(
276
+ name: string,
277
+ input: unknown,
278
+ opts?: { conversationId?: string; contextId?: string }
279
+ ): Promise<FlowResult>
280
+ }
281
+ /** 三段式骨架提示词注入(优化缓存命中)。 */
282
+ prompts: {
283
+ /** inject(where, text):'prefix' 稳定前缀(system 内)|'suffix' 尾部指令前。 */
284
+ inject(where: 'prefix' | 'suffix', text: string, opts?: { contextId?: string }): void
285
+ /** 移除注入。 */
286
+ remove(where: 'prefix' | 'suffix', text: string, opts?: { contextId?: string }): void
287
+ }
288
+ /** 压缩层配置(覆盖 ContextDefinition.compaction 或全局默认)。 */
289
+ compaction: {
290
+ configure(cfg: Partial<ContextCompactionConfig>, opts?: { contextId?: string }): void
291
+ }
292
+ }
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@prisflow/proactiveai-plugin-types",
3
+ "version": "0.1.0",
4
+ "description": "ProactiveAI 宿主插件 API 的类型契约(纯类型,无运行时代码)。插件开发者通过该包获得类型提示与编译检查。",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "types": "index.d.ts",
8
+ "files": ["index.d.ts"],
9
+ "scripts": {
10
+ "check": "tsc --noEmit"
11
+ },
12
+ "devDependencies": {
13
+ "typescript": "^5.7.3"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ }
18
+ }