page-agent-sdk 1.1.4 → 1.2.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.
- package/dist/page-agent-sdk.iife.js +20 -20
- package/dist/page-agent-sdk.js +207 -183
- package/dist/page-agent-sdk.umd.cjs +21 -21
- package/package.json +1 -1
- package/skills/page-agent-sdk-integrate/SKILL.md +3 -2
- package/skills/page-agent-sdk-integrate/references/advanced.md +37 -2
- package/skills/page-agent-sdk-integrate/references/api.md +3 -0
- package/types/index.d.ts +18 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "page-agent-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "框架无关的页面内 Agent JS SDK —— 以对话框形态挂载到任意网页,通过自定义 tool 读写宿主 window 属性(GET 抓文档),具备 planning/skills/虚拟工作区/快照回退/context 管理能力。Vue 打包进库,使用者无需安装 Vue。",
|
|
6
6
|
"main": "./dist/page-agent-sdk.umd.cjs",
|
|
@@ -90,8 +90,9 @@ Event types: `window_prop_change` / `message_update` / `tool_call` / `tool_resul
|
|
|
90
90
|
| **Headless / server-side** | `ui:false` + `storage:'memory'` + `capabilities:{windowOps:false,fetch:false}`; drive via `sdk.send` |
|
|
91
91
|
| **Multi-agent on one page** | same `id` + `shareContext:true` → multiple dialogs share one `AgentCore` |
|
|
92
92
|
| **MCP integration** | `mcp:[{transport,url}]` remote tool servers; `@modelcontextprotocol/sdk` optional peerDep |
|
|
93
|
+
| **Lazy-loaded components (dynamic schemas)** | `sdk.addWindowProp(spec)` on component mount / `removeWindowProp` on unmount; tools pick up new registrations immediately, no rebuild. See [references/advanced.md §0](references/advanced.md) |
|
|
93
94
|
|
|
94
|
-
When the user describes a scenario, map it to the row above and load `references/use-cases.md` for the matching numbered case (1→9) with copy-paste code.
|
|
95
|
+
When the user describes a scenario, map it to the row above and load `references/use-cases.md` for the matching numbered case (1→9) with copy-paste code. For dynamic/lazy-loaded component schemas, custom tools/skills/subagents/MCP, load [references/advanced.md](references/advanced.md).
|
|
95
96
|
|
|
96
97
|
## References (read as needed)
|
|
97
98
|
|
|
@@ -101,7 +102,7 @@ Detailed docs live in this skill's `references/` folder — load the one matchin
|
|
|
101
102
|
- **[references/options.md](references/options.md)** — every `createChatSdk` option: type, default, purpose & when to use. Read when the user asks "what does option X do" or needs to tune behavior.
|
|
102
103
|
- **[references/api.md](references/api.md)** — instance methods (`mount`/`send`/`stream`/`inspect`/`switchSession`/`hook`/checkpoints), `defineTool`/`defineSkill`/`presets`, built-in window tools, and the full `SdkEvent` type table. Read when the user asks about APIs, tools, or events.
|
|
103
104
|
- **[references/use-cases.md](references/use-cases.md)** — 9 end-to-end scenarios (low-code builder / form designer / CMS batch / ops console / AI-native / research / server-side / multi-agent / MCP). Read when the user wants a concrete pattern for their use case.
|
|
104
|
-
- **[references/advanced.md](references/advanced.md)** — detailed examples for the
|
|
105
|
+
- **[references/advanced.md](references/advanced.md)** — detailed examples for the extensibility surfaces: **dynamic windowProps (`sdk.addWindowProp`/`removeWindowProp` for lazy-loaded components)**, custom `defineTool` (with error handling + coexisting with windowOps), `defineSkill` (inline content + remote doc), subagents (ad-hoc `spawn_agent`/`spawn_agents` + pre-declared `subagents` → `use_<id>`), MCP (http/sse/websocket + auth + dev gotcha). Read when the user asks "how to add custom tools / skills / subagents / MCP" or "lazy-load components with different schemas".
|
|
105
106
|
|
|
106
107
|
Project-level docs (in the repo, not bundled in this skill):
|
|
107
108
|
- `doc/usage-guide.md` (zh) / `doc/usage-guide.en.md` — full options reference
|
|
@@ -1,6 +1,41 @@
|
|
|
1
|
-
# Advanced examples — custom tools, skills, subagents, MCP
|
|
1
|
+
# Advanced examples — custom tools, skills, subagents, MCP, dynamic windowProps
|
|
2
2
|
|
|
3
|
-
Detailed, copy-paste examples for the
|
|
3
|
+
Detailed, copy-paste examples for the extensibility surfaces. Read the section matching the user's need.
|
|
4
|
+
|
|
5
|
+
## 0. Dynamic windowProps (lazy-loaded components) — `sdk.addWindowProp` / `removeWindowProp`
|
|
6
|
+
|
|
7
|
+
When components are lazy-loaded with **different schemas each**, don't declare all `windowProps` upfront. Register them at runtime as components mount/unmount. The agent's window tools pick up new registrations immediately (no agent rebuild).
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
const sdk = createChatSdk({
|
|
11
|
+
container: '#chat', llm: { ... },
|
|
12
|
+
systemPrompt: '你是页面助手,按组件类型操作 window.app.components.<id>。',
|
|
13
|
+
windowProps: [
|
|
14
|
+
// statically-declared ones (always present)
|
|
15
|
+
{ path: 'app.config', description: '全局配置', schema: z.record(z.any()) },
|
|
16
|
+
],
|
|
17
|
+
}).mount()
|
|
18
|
+
|
|
19
|
+
// 组件懒加载时动态注册其 schema(结构各异)
|
|
20
|
+
function onComponentMount(comp: { id: string; type: string; schema: z.ZodType }) {
|
|
21
|
+
sdk.addWindowProp({ path: `app.components.${comp.id}`, description: `${comp.type} 组件`, schema: comp.schema })
|
|
22
|
+
// 立即生效:AI 现在能 set/edit_window_prop 这个 path,按其 schema 校验
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 组件卸载时移除(快照栈一并清理)
|
|
26
|
+
function onComponentUnmount(id: string) {
|
|
27
|
+
sdk.removeWindowProp(`app.components.${id}`)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 查看当前所有注册项(反映动态增删)
|
|
31
|
+
const current: WindowPropSpec[] = sdk.listWindowProps()
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Notes:
|
|
35
|
+
- `addWindowProp` 覆盖同名 path 时保留旧快照栈;按新 schema 校验。
|
|
36
|
+
- 动态注册的属性**不自动纳入 checkpoint 快照**(checkpoint 的 windowPaths 在构造时固定);如需回滚动态组件,自行管理或重建。
|
|
37
|
+
- `inspect().windowProps` 与 `verify`(默认 `createWriteBackCheck`)均反映动态注册的最新 schemas(verify 每次 check 实时取 `listWindowProps()`)。
|
|
38
|
+
- `capabilities.windowOps:false` 时 `addWindowProp`/`removeWindowProp` 为 no-op(并 warn)。
|
|
4
39
|
|
|
5
40
|
## 1. Custom tools (`defineTool`)
|
|
6
41
|
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
| `inspect()` | `() => AgentInfo` | Inspect agent: tools/skills/windowProps/middleware/todos/mcp.servers (each tool's `source`: `builtin`/`mcp:<name>`/`user`). DebugDrawer uses this. |
|
|
13
13
|
| `switchSession(id?)` | `(id?: string) => Promise<string>` | Switch session context (load or create by id). Requires `storage` enabled. |
|
|
14
14
|
| `hook(handler)` | `(h: SdkEventHandler) => () => void` | Runtime event subscription (multi-listener, returns unsubscribe). Complements `onEvent`. |
|
|
15
|
+
| `addWindowProp(spec)` | `(spec: WindowPropSpec) => void` | Runtime register/override a window prop (lazy-loaded components). Takes effect immediately, no rebuild. Needs `windowOps` enabled. |
|
|
16
|
+
| `removeWindowProp(path)` | `(path: string) => boolean` | Remove a registered window prop (component unmount); returns whether it existed. Clears its snapshot stack. |
|
|
17
|
+
| `listWindowProps()` | `() => WindowPropSpec[]` | List currently-registered window props (reflects dynamic add/remove). |
|
|
15
18
|
| `restoreLastCheckpoint()` | `() => boolean` | Restore last good checkpoint (needs `checkpoint` enabled). |
|
|
16
19
|
| `listCheckpoints()` | `() => CheckpointMeta[]` | List available checkpoints. |
|
|
17
20
|
|
package/types/index.d.ts
CHANGED
|
@@ -168,6 +168,18 @@ export interface WindowOpsOptions {
|
|
|
168
168
|
whitelist?: boolean;
|
|
169
169
|
}
|
|
170
170
|
|
|
171
|
+
/** window 属性注册表控制器(运行时动态增删;createWindowOps 返回的工具数组上以不可枚举属性 `controller` 挂载) */
|
|
172
|
+
export interface WindowOpsController {
|
|
173
|
+
/** 新增/覆盖一个属性注册项(运行时懒加载组件场景);覆盖时旧快照栈保留 */
|
|
174
|
+
add(spec: WindowPropSpec): void;
|
|
175
|
+
/** 移除一个属性注册项;返回是否确实存在并移除。快照栈一并清理 */
|
|
176
|
+
remove(path: string): boolean;
|
|
177
|
+
/** 列出当前所有注册项(反映动态增删后的最新状态) */
|
|
178
|
+
list(): WindowPropSpec[];
|
|
179
|
+
/** 是否已注册某 path */
|
|
180
|
+
has(path: string): boolean;
|
|
181
|
+
}
|
|
182
|
+
|
|
171
183
|
export interface PermissionRule {
|
|
172
184
|
operations: ('read' | 'write')[];
|
|
173
185
|
scopes: string[];
|
|
@@ -383,6 +395,12 @@ export interface ChatSdk {
|
|
|
383
395
|
listCheckpoints(): CheckpointMeta[];
|
|
384
396
|
/** 运行时订阅 SDK 事件(可多个监听器,返回取消函数);与构造时 onEvent 互补 */
|
|
385
397
|
hook(handler: SdkEventHandler): () => void;
|
|
398
|
+
/** 运行时动态新增/覆盖一个 window 属性注册项(懒加载组件:组件挂载时注册其 schema);立即对 window 工具生效,无需重建 agent。需开启 windowOps */
|
|
399
|
+
addWindowProp(spec: WindowPropSpec): void;
|
|
400
|
+
/** 运行时移除一个 window 属性注册项(组件卸载);返回是否确实存在并移除。快照栈一并清理 */
|
|
401
|
+
removeWindowProp(path: string): boolean;
|
|
402
|
+
/** 列出当前所有已注册 window 属性(反映动态增删后的最新状态) */
|
|
403
|
+
listWindowProps(): WindowPropSpec[];
|
|
386
404
|
}
|
|
387
405
|
|
|
388
406
|
export declare function createChatSdk(options: ChatSdkOptions): ChatSdk;
|