page-agent-sdk 1.1.0 → 1.1.2

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 CHANGED
@@ -255,6 +255,30 @@ doc/ # usage-guide / architecture / context-managemen
255
255
  CLAUDE.md # architecture + gotchas + coding conventions (agent must-read)
256
256
  ```
257
257
 
258
+ ## Skills for AI tools (for integrators)
259
+
260
+ A ready-to-use Agent Skill is bundled for integrators using Claude Code / Cursor (or any agent harness that loads `.claude/skills/` / `~/.claude/skills/`). It teaches the AI how to use **this SDK** in your project:
261
+
262
+ | Skill | When it triggers |
263
+ |---|---|
264
+ | `page-agent-sdk-integrate` | Embedding the SDK — choose install method, declare `windowProps` + zod schemas, configure the LLM, mount, subscribe to events (`onEvent` / `sdk.hook`), run headless, troubleshoot common pitfalls |
265
+
266
+ **Install** (pick one):
267
+
268
+ ```bash
269
+ # Option A — copy from the installed npm package
270
+ npm i page-agent-sdk
271
+ cp -R node_modules/page-agent-sdk/skills/page-agent-sdk-integrate ~/.claude/skills/
272
+
273
+ # Option B — download from the repo (no install needed)
274
+ curl -L https://github.com/whyymj/chat-sdk/tarball/master | tar xz --strip-components=1 --wildcards '*/skills/page-agent-sdk-integrate'
275
+ mv skills/page-agent-sdk-integrate ~/.claude/skills/
276
+ ```
277
+
278
+ After install, restart your AI tool; the skill auto-triggers when you ask things like "add page-agent-sdk to my page".
279
+
280
+ > A second skill `page-agent-sdk-release` (release workflow for maintainers) is kept in the repo's `.claude/skills/` for project maintainers only and is **not** distributed via the npm package.
281
+
258
282
  ## Architecture
259
283
 
260
284
  ```mermaid
@@ -315,11 +339,11 @@ After `npm run dev`, visit the corresponding page:
315
339
  | Example | Entry | Demonstrates |
316
340
  |---|---|---|
317
341
  | page-demo | `/` | Self-bootstrapping demo: left JSON reactive page + right chat |
318
- | nested-demo | `/nested.html` | Nested block tree + human confirm + checkpoint |
319
- | human-confirm-demo | `/human-confirm.html` | AI proactive inquiry (multi-plan pick) + pre-write confirm |
320
- | planner-demo | `/planner.html` | Plan-reflect-execute (high-temp creative planner + low-temp reflector) |
321
- | subagent-demo | `/subagent.html` | Subagent parallel orchestration |
322
- | mcp-demo | `/mcp.html` | MCP remote tools (needs `npm run mcp:mock`) |
342
+ | nested-demo | `/examples/nested-demo/` | Nested block tree + human confirm + checkpoint |
343
+ | human-confirm-demo | `/examples/human-confirm-demo/` | AI proactive inquiry (multi-plan pick) + pre-write confirm |
344
+ | planner-demo | `/examples/planner-demo/` | Plan-reflect-execute (high-temp creative planner + low-temp reflector) |
345
+ | subagent-demo | `/examples/subagent-demo/` | Subagent parallel orchestration |
346
+ | mcp-demo | `/examples/mcp-demo/` | MCP remote tools (needs `npm run mcp:mock`) |
323
347
 
324
348
  Framework-agnostic integration: `demo/plain.html` (importmap + esm.sh).
325
349
 
package/README.zh-CN.md CHANGED
@@ -200,6 +200,30 @@ doc/ # usage-guide / architecture / context-managemen
200
200
  CLAUDE.md # 架构要点 + 约定坑 + 编码规范(agent 必读)
201
201
  ```
202
202
 
203
+ ## 给 AI 工具使用者的 Skills(集成方安装)
204
+
205
+ 内置一个开箱即用的 Agent Skill,供使用 Claude Code / Cursor(或任何加载 `.claude/skills/` / `~/.claude/skills/` 的 agent 工具)的集成方使用。它教 AI 如何在**你的项目**中使用本 SDK:
206
+
207
+ | Skill | 触发场景 |
208
+ |---|---|
209
+ | `page-agent-sdk-integrate` | 集成 SDK —— 选引入方式、声明 `windowProps` + zod schema、配 LLM、挂载、订阅事件(`onEvent` / `sdk.hook`)、跑 headless、排查常见坑 |
210
+
211
+ **安装**(任选其一):
212
+
213
+ ```bash
214
+ # 方式 A —— 从已安装的 npm 包复制
215
+ npm i page-agent-sdk
216
+ cp -R node_modules/page-agent-sdk/skills/page-agent-sdk-integrate ~/.claude/skills/
217
+
218
+ # 方式 B —— 从仓库下载(无需安装)
219
+ curl -L https://github.com/whyymj/chat-sdk/tarball/master | tar xz --strip-components=1 --wildcards '*/skills/page-agent-sdk-integrate'
220
+ mv skills/page-agent-sdk-integrate ~/.claude/skills/
221
+ ```
222
+
223
+ 安装后重启 AI 工具;当你说「把 page-agent-sdk 加到我的页面」等时 skill 自动触发。
224
+
225
+ > 另有 `page-agent-sdk-release`(维护者发布工作流)skill 仅保留在仓库 `.claude/skills/` 供项目维护者自用,**不**通过 npm 包公开分发。
226
+
203
227
  ## 架构
204
228
 
205
229
  ```mermaid
@@ -260,11 +284,11 @@ createChatSdk({
260
284
  | 示例 | 入口 | 演示 |
261
285
  |---|---|---|
262
286
  | page-demo | `/` | 自举 demo:左 JSON 响应式页面 + 右对话框 |
263
- | nested-demo | `/nested.html` | 嵌套区块树 + 人工确认 + checkpoint |
264
- | human-confirm-demo | `/human-confirm.html` | AI 主动征询(多方案点选)+ 写前确认 |
265
- | planner-demo | `/planner.html` | 规划-反思-执行(高温创意 planner + 低温 reflector) |
266
- | subagent-demo | `/subagent.html` | 子 agent 并行编排 |
267
- | mcp-demo | `/mcp.html` | MCP 远程工具(需 `npm run mcp:mock`) |
287
+ | nested-demo | `/examples/nested-demo/` | 嵌套区块树 + 人工确认 + checkpoint |
288
+ | human-confirm-demo | `/examples/human-confirm-demo/` | AI 主动征询(多方案点选)+ 写前确认 |
289
+ | planner-demo | `/examples/planner-demo/` | 规划-反思-执行(高温创意 planner + 低温 reflector) |
290
+ | subagent-demo | `/examples/subagent-demo/` | 子 agent 并行编排 |
291
+ | mcp-demo | `/examples/mcp-demo/` | MCP 远程工具(需 `npm run mcp:mock`) |
268
292
 
269
293
  框架无关集成:`demo/plain.html`(importmap + esm.sh)。
270
294
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-agent-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
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",
@@ -21,7 +21,8 @@
21
21
  },
22
22
  "files": [
23
23
  "dist",
24
- "types"
24
+ "types",
25
+ "skills"
25
26
  ],
26
27
  "scripts": {
27
28
  "dev": "vite",
@@ -0,0 +1,93 @@
1
+ ---
2
+ name: page-agent-sdk-integrate
3
+ description: Integrate the page-agent-sdk npm package into a web app so an AI agent can read/write structured page data (window props) via schema-validated tools. Use when the user wants to add/embed the SDK, declare windowProps + zod schemas, configure the LLM, mount the chat dialog, subscribe to events (onEvent / sdk.hook), run headless (ui:false) with a custom UI, or troubleshoot common integration issues (DeepSeek 400 tool_call_id, MCP injecting 0 tools, etc).
4
+ ---
5
+
6
+ # Integrate page-agent-sdk
7
+
8
+ Help the user embed `page-agent-sdk` so an AI agent safely edits their page's structured JSON via tools.
9
+
10
+ ## Core concept
11
+
12
+ The SDK is a **standardized JSON-operation agent**: the integrator declares writable `window` paths + zod schemas; the agent edits them via `set_window_prop` / `edit_window_prop` (jsonPath patches), validated by schema, scoped to the registry, with snapshot rollback. "Editing JSON" becomes structured + validatable + rollbackable, NOT free-form LLM text.
13
+
14
+ ## Workflow
15
+
16
+ ### 1. Choose install method
17
+
18
+ | Method | When | How |
19
+ |---|---|---|
20
+ | **npm** | module project, tree-shake ok | `npm i page-agent-sdk zod @langchain/openai @langchain/core` → `import { createChatSdk, z } from 'page-agent-sdk'` |
21
+ | **CDN · ESM** (esm.sh) | modular, small, peer auto-resolved | `import { createChatSdk, z } from 'https://esm.sh/page-agent-sdk'` |
22
+ | **CDN · IIFE** (unpkg) | one-line, no build, ~1.4MB all-in | `<script src="https://unpkg.com/page-agent-sdk"></script>` → `ChatSdk.createChatSdk`, `ChatSdk.z` |
23
+
24
+ See `demo/plain.html` for a framework-agnostic importmap + esm.sh example.
25
+
26
+ ### 2. Declare windowProps + schemas (the key step)
27
+
28
+ Put the page data on `window` (e.g. `window.app = { title, theme }`), then declare each writable path with a zod schema. The agent can ONLY touch declared paths; `set`/`edit` are schema-validated (invalid → structured error, no write).
29
+
30
+ ```ts
31
+ import { createChatSdk, z } from 'page-agent-sdk'
32
+ import 'page-agent-sdk/style.css'
33
+
34
+ window.app = { title: 'Demo', theme: 'light', items: [] }
35
+
36
+ createChatSdk({
37
+ container: '#root',
38
+ llm: { apiKey, baseUrl: 'https://api.deepseek.com/v1', model: 'deepseek-chat' },
39
+ systemPrompt: 'You are a page assistant; read/write window.app via tools.',
40
+ windowProps: [
41
+ { path: 'app.title', description: '页面标题', schema: z.string() },
42
+ { path: 'app.theme', description: '主题', schema: z.enum(['light','dark']) },
43
+ { path: 'app.items', description: '列表项数组', schema: z.array(z.object({ name: z.string(), price: z.number() })) },
44
+ ],
45
+ }).mount()
46
+ ```
47
+
48
+ For large JSON, prefer `edit_window_prop` (jsonPath patch: set/remove/merge/append) over `set_window_prop` (whole value) — avoids re-sending the entire blob.
49
+
50
+ ### 3. Configure the LLM
51
+
52
+ `llm` accepts an `LLMConfig` object (`{ apiKey, baseUrl, model, temperature?, maxTokens? }`) or any LangChain `BaseChatModel` instance. Default protocol is OpenAI-compatible (DeepSeek works out of the box). For large JSON edits use low temperature (~0.3).
53
+
54
+ ### 4. Subscribe to events (replace polling)
55
+
56
+ Two complementary ways to react to SDK changes from the host page:
57
+
58
+ ```ts
59
+ const sdk = createChatSdk({
60
+ onEvent(e) { if (e.type === 'window_prop_change') renderUI() }, // constructor-time, single
61
+ // ...
62
+ }).mount()
63
+
64
+ // runtime, multiple listeners, cancellable
65
+ const off = sdk.hook((e) => { if (e.type === 'tool_call') analytics.track(e.name) })
66
+ // off() to unsubscribe
67
+ ```
68
+
69
+ Event types: `window_prop_change` / `message_update` / `tool_call` / `tool_result` / `text` / `round_start` / `done` / `error` (+ stream events in stream mode). `approval_request` is NOT forwarded (UI handles it).
70
+
71
+ ### 5. Headless mode (custom UI, framework-agnostic)
72
+
73
+ `ui: false` → no built-in dialog; use the reactive `sdk.messages` array + `sdk.send`/`sdk.stream` to build your own UI. Reusable `ChatDialog` / `MessageContent` / `CodePreview` components and `useChat` composable are exported from the entry for custom UIs.
74
+
75
+ ### 6. Capabilities & presets
76
+
77
+ - `capabilities: { windowOps:false, fetch:false, planning:false, skills:false, vfs:false, summarization:false, memory:false, subagent:false }` — turn off unused built-ins to save tokens/size. `verify` is the reverse (off by default; `capabilities.verify:true` enables write-back self-check).
78
+ - `presets.pageBuilder` / `researcher` / `minimal` — spread into `createChatSdk` for common scenarios.
79
+
80
+ ## References (read as needed)
81
+
82
+ - `doc/usage-guide.md` (zh) / `doc/usage-guide.en.md` — full options reference (onEvent/hook/server-side/checkpoint/approval/verify/subagent/MCP)
83
+ - `examples/<demo>/` — runnable demos (page-demo, nested-demo, subagent-demo, mcp-demo, planner-demo, toolsets-demo, human-confirm-demo)
84
+ - `demo/plain.html` — framework-agnostic CDN integration
85
+ - `CLAUDE.md` — internal dev guide (architecture, conventions)
86
+
87
+ ## Common pitfalls
88
+
89
+ - **DeepSeek/OpenAI 400 `missing field tool_call_id`**: `ToolMessage` must use snake_case `tool_call_id` (not camelCase). Already handled internally; only relevant if writing custom tool plumbing.
90
+ - **ChatOpenAI params**: use `apiKey` (not `openAIApiKey`), `model` (not `modelName`); `baseUrl` goes via `configuration.baseURL`.
91
+ - **MCP injects 0 tools on first cold visit**: `vite.config.ts` `optimizeDeps.include` pre-declares the SDK sub-paths; if you fork the config, keep those entries or the first MCP page load injects nothing (reload fixes it).
92
+ - **`.env` `VITE_AI_SYSTEM_PROMPT` must be single-line** (dotenv doesn't support multi-line values).
93
+ - **Server-side (Node.js)**: works with `ui:false` + `storage:'memory'` + `capabilities:{windowOps:false,fetch:false}`; `mount()`/`unmount()` guard `window`/`document` access. Provide `globalThis.window` if you enable windowOps in Node.