page-agent-sdk 2.15.0 → 2.16.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/README.md CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  > **[English](https://github.com/whyymj/page-agent-sdk/blob/master/README.md)** · **[中文](https://github.com/whyymj/page-agent-sdk/blob/master/README.zh-CN.md)**
4
4
 
5
- > Give your web page an **AI assistant that edits the page itself**. Mount a chat dialog in one line; the AI reads/writes page data safely via schema-validated tools — "conversational" building/editing/ops.
5
+ > Give your web page an **AI assistant that edits the page itself**. Mount a chat dialog in one line; the AI reads/writes page data safely via schema-validated tools — "conversational" building/editing/ops. **A lighter, framework-agnostic alternative to CopilotKit / LangChain for in-page, schema-validated JSON-editing agents.**
6
6
 
7
7
  > **AI agent integration**: see [Agent Integration Cheat Sheet](#agent-integration-cheat-sheet-for-ai-agents) below (exports / options / extension points / built-in tools / file structure). Architecture & gotchas in [`CLAUDE.md`](https://github.com/whyymj/page-agent-sdk/blob/master/CLAUDE.md).
8
8
 
9
9
  [![npm](https://img.shields.io/npm/v/page-agent-sdk.svg)](https://www.npmjs.com/package/page-agent-sdk)
10
10
  [![license](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/whyymj/page-agent-sdk/blob/master/LICENSE)
11
- [![tests](https://img.shields.io/badge/self%20tests-630%20asserts-brightgreen.svg)](#self-tests)
11
+ [![tests](https://img.shields.io/badge/self%20tests-680%20asserts-brightgreen.svg)](#self-tests)
12
12
 
13
13
  ---
14
14
 
@@ -51,6 +51,39 @@ At its core, it gives the AI a **standardized, safe JSON-operation channel**. AI
51
51
 
52
52
  **Full end-to-end scenarios with copy-paste code** (9 cases: low-code builder / form designer / CMS batch / ops console / AI-native / research / server-side / multi-agent / MCP) live in the bundled Agent Skill at `skills/page-agent-sdk-integrate/references/use-cases.md` (also shipped in the npm package). See [Skills for AI tools](#skills-for-ai-tools-for-integrators) below to install the skill.
53
53
 
54
+ ## When to use / When not
55
+
56
+ **Use it if you** want an AI assistant embedded in your web page that edits structured page data (config / component tree / form definitions / CMS content) — safely, rollbackably, via tools — and you don't want to hand-roll an agent harness, schema validation, optimistic lock, or snapshot system.
57
+
58
+ **Don't use it if you** only need a stateless chat widget (use any chat UI lib), or you want the AI to drive a browser / automate arbitrary DOM across sites (use Playwright / browser-use), or your data has no schema you can declare.
59
+
60
+ ### FAQ
61
+
62
+ - **Q: I want an AI assistant embedded in my web page that can edit the page data.** → `page-agent-sdk`: declare a zod schema + `bind`, mount the dialog, done. See [30-second quickstart](#30-second-quickstart).
63
+ - **Q: Alternative to CopilotKit / LangChain for an in-page agent?** → `page-agent-sdk` is framework-agnostic (Vue bundled, host can be React / vanilla), schema-validated, ships optimistic lock + snapshot rollback + MCP, and needs no LangGraph. See [Comparison](#comparison).
64
+ - **Q: How to let AI safely edit a large JSON on my page?** → `data` + zod schema + `write` with `patch` / `patches` + `expectedHash` optimistic lock. Invalid edits are rejected pre-write; bad edits rollback in one click.
65
+ - **Q: Does it work with DeepSeek / OpenAI / any OpenAI-compatible endpoint?** → Yes. `llm: { apiKey, baseUrl, model }` defaults to DeepSeek; any LangChain `BaseChatModel` also accepted.
66
+ - **Q: Can I run it headless / in Node.js?** → Yes. `ui:false` + `storage:'memory'`, drive via `sdk.send`. See [headless-demo](#examples).
67
+ - **Q: Does it support MCP?** → Yes. `mcp: [{ transport, url }]` connects remote MCP servers and injects tools dynamically.
68
+
69
+ ### Comparison
70
+
71
+ | | page-agent-sdk | CopilotKit | LangChain (chat models) | LangGraph | raw LLM tool-calling |
72
+ |---|---|---|---|---|---|
73
+ | Framework-agnostic, UI bundled | ✅ Vue bundled, host-agnostic | ❌ React-only | ✅ (no UI) | ✅ (no UI) | ✅ (no UI) |
74
+ | Schema-validated JSON ops | ✅ zod, whitelist + merge-safe | ⚠️ partial (tool args) | ⚠️ tool args only | ⚠️ tool args only | ❌ |
75
+ | Incremental patch (jsonPath) | ✅ `write` patch / `edit_data` | ❌ | ❌ | ❌ | ❌ |
76
+ | Optimistic lock + conflict HITL | ✅ `expectedHash` | ❌ | ❌ | ❌ | ❌ |
77
+ | Snapshot rollback + checkpoint | ✅ per-path + session | ❌ | ❌ | ❌ | ❌ |
78
+ | Proactive human-confirm | ✅ built-in | ⚠️ manual | ❌ | ❌ | ❌ |
79
+ | MCP | ✅ | ✅ | ✅ | ✅ | manual |
80
+ | Subagents | ✅ | ❌ | ✅ (manual) | ✅ | manual |
81
+ | Context compression | ✅ 4-layer built-in | ❌ | ❌ | ✅ checkpointer | ❌ |
82
+ | In-browser persistence | ✅ IndexedDB | ❌ | ❌ | ❌ | ❌ |
83
+ | Bundle | ~620 KB ESM / 1.4 MB IIFE | React dep | large | large | none |
84
+
85
+ > Nuance: CopilotKit is a great choice if you're already on React and want a polished AI-chat UI with backend actions; LangChain / LangGraph are general-purpose agent orchestration (server-side strong). `page-agent-sdk` specifically targets **in-page, schema-validated, rollbackable JSON editing** — that niche is its differentiation.
86
+
54
87
  ## 30-second quickstart
55
88
 
56
89
  ```bash
@@ -99,6 +132,7 @@ CDN zero-config: `<script src="https://unpkg.com/page-agent-sdk"></script>` →
99
132
  | 🤖 subagents | Delegate subtasks; process stays out of main context | `subagent` |
100
133
  | 🔌 MCP | Connect remote MCP servers, inject tools dynamically | `mcp` |
101
134
  | 📦 context compression | 4-layer adaptive compression, presets + LLM summary | `contextPreset` |
135
+ | 🧪 complex-task tuned | `complex` context preset (larger window + later compress + more recall, for multi-step / large-JSON / long-workflow tasks); vfs JSON-aware tools (`vfs_json_read` / `vfs_json_patch`) for structured big-JSON ops inside vfs; vfs three-pool LRU (large_results / drafts / userFiles isolated, no mutual eviction) | `contextPreset:'complex'`, `capabilities.vfs` |
102
136
  | 🛡️ compression-safe | Live data snapshot + preserved tool results in summary; write returns hint available paths; `systemPromptHelpers.reliableWriteRules` | built-in |
103
137
  | 💾 persistence | IndexedDB multi-session + quota eviction + switch | `storage` |
104
138
 
@@ -175,7 +209,7 @@ ChatDialog, MessageContent, CodePreview, SkillPanel, useChat
175
209
  | | `verify` | `{check?,maxAttempts?,adversarial?}` | Needs `capabilities.verify:true`; `check` omitted → `createWriteBackCheck` (read-back root auto-bound to `data.bind`, adapts to `sdk.setData` runtime swap) |
176
210
  | **Subagents** | `subagent` | `{allowedTools?,systemPrompt?,temperature?,llm?,maxDepth?·1,maxParallel?·4}` | Runtime ad-hoc delegation (`spawn_agent`/`spawn_agents`) |
177
211
  | | `subagents` | `SubagentConfig[]` | Pre-declared named subagents → each generates `use_<id>` tool |
178
- | **Context** | `contextPreset` | `'auto' \| 'conservative' \| 'aggressive'` · default `auto` | Compression preset |
212
+ | **Context** | `contextPreset` | `'auto' \| 'conservative' \| 'aggressive' \| 'complex'` · default `auto` | Compression preset (`complex` for multi-step / large-JSON / long-workflow tasks) |
179
213
  | | `contextOptions` | `Partial<ContextManagerOptions> \| false` | Fine params (`false` disables compression). Includes `preserveLastToolResults` (default `['describe_data','describe_data']` — keep field descriptions in compressed summary) |
180
214
  | | `summaryLlm` | `BaseChatModel \| LLMConfig` | Summary-dedicated LLM (defaults to main `llm`) |
181
215
  | | `maxMemoryRounds` | `number` · default `50` | Dialog history memory round cap (`0` disables trim) |
@@ -385,7 +419,7 @@ createChatSdk({
385
419
  humanConfirm: true, // proactive inquiry (default on)
386
420
  approval: { tools: ['write'] }, // passive confirm whitelist (default off)
387
421
  checkpoint: true,
388
- contextPreset: 'auto', // auto/conservative/aggressive
422
+ contextPreset: 'auto', // auto/conservative/aggressive/complex
389
423
  summaryLlm: { ... }, // summary-dedicated LLM (defaults to main llm)
390
424
  maxRetries: 2, maxParallelTools: 1,
391
425
  subagent: { allowedTools: [...] },
@@ -475,8 +509,8 @@ function switchTo(i: number) {
475
509
  ## Self-tests
476
510
 
477
511
  ```bash
478
- npm test # 630 assertions (tsx, source-level; no LLM dependency)
479
- npm run test:e2e # 210 integration assertions (node, built dist; covers APIs/options/modules/simple&complex scenes: default systemPrompt(capability overview) / dynamic register + inspect sync / inspect(tools/middleware/subagent/verify/mcp/todos/lastCompression/checkpoints reflect config) / custom tools/middleware/skills/memory injection / runtime dynamic reconfiguration(setTools/addTool/removeTool/setLlm/setMemory/setSubagents reflect) / switchSession(on/off) / shareContext on/off sharing/independent / storage backends + object config / presets(3) / checkpoint / exports complete(39+ fns/components) / util fns usable(isQuotaError/estimateTokens/jpEval/searchJson) / source=builtin / mount boundary / hook multi-listener / llm config / hide/show / error scenes)
512
+ npm test # 680 assertions (tsx, source-level; no LLM dependency)
513
+ npm run test:e2e # 217 integration assertions (node, built dist; covers APIs/options/modules/simple&complex scenes: default systemPrompt(capability overview) / dynamic register + inspect sync / inspect(tools/middleware/subagent/verify/mcp/todos/lastCompression/checkpoints reflect config) / custom tools/middleware/skills/memory injection / runtime dynamic reconfiguration(setTools/addTool/removeTool/setLlm/setMemory/setSubagents reflect) / switchSession(on/off) / shareContext on/off sharing/independent / storage backends + object config / presets(3) / checkpoint / exports complete(39+ fns/components) / util fns usable(isQuotaError/estimateTokens/jpEval/searchJson) / source=builtin / mount boundary / hook multi-listener / llm config / hide/show / error scenes)
480
514
  ```
481
515
 
482
516
  ## Local npm package test
package/README.zh-CN.md CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  > **[English](https://github.com/whyymj/page-agent-sdk/blob/master/README.md)** · **[中文](https://github.com/whyymj/page-agent-sdk/blob/master/README.zh-CN.md)**
4
4
 
5
- > 给网页一个**会改页面的 AI 助手**。一行代码挂载对话框,AI 通过工具按 schema 安全读写页面数据,实现「对话式」搭建/编辑/运维。
5
+ > 给网页一个**会改页面的 AI 助手**。一行代码挂载对话框,AI 通过工具按 schema 安全读写页面数据,实现「对话式」搭建/编辑/运维。**比 CopilotKit / LangChain 更轻、框架无关的「页面内、schema 校验、JSON 编辑 Agent」方案。**
6
6
 
7
7
  > **AI agent 接入**:直接看下方「[Agent 接入速查](#agent-接入速查给-ai-agent-读)」(导出 / 选项表 / 扩展点 / 内置工具 / 文件结构),架构与约定坑见 [`CLAUDE.md`](https://github.com/whyymj/page-agent-sdk/blob/master/CLAUDE.md)。
8
8
 
9
9
  [![npm](https://img.shields.io/npm/v/page-agent-sdk.svg)](https://www.npmjs.com/package/page-agent-sdk)
10
10
  [![license](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/whyymj/page-agent-sdk/blob/master/LICENSE)
11
- [![tests](https://img.shields.io/badge/self%20tests-630%20asserts-brightgreen.svg)](#自测)
11
+ [![tests](https://img.shields.io/badge/self%20tests-680%20asserts-brightgreen.svg)](#自测)
12
12
 
13
13
  ---
14
14
 
@@ -51,6 +51,39 @@
51
51
 
52
52
  **完整端到端场景(含可复制代码,共 9 例:低代码搭建 / 表单设计器 / CMS 批量 / 运维配置台 / AI 原生 / 调研 / 服务端 / 多 agent / MCP)** 见随包附带的 Agent Skill:`skills/page-agent-sdk-integrate/references/use-cases.md`(npm 包内同样包含)。安装 skill 见下文[给 AI 工具使用者的 Skills](#给-ai-工具使用者的-skills集成方安装)。
53
53
 
54
+ ## 何时用 / 何时不用
55
+
56
+ **适合**:你想在网页里嵌一个 AI 助手,让它安全、可回退地用工具改结构化页面数据(配置 / 组件树 / 表单定义 / CMS 内容),又不想自己写 agent harness、schema 校验、乐观锁、快照系统。
57
+
58
+ **不适合**:只需无状态聊天挂件(用任意聊天 UI 库);要 AI 跨站驱动浏览器 / 自动化任意 DOM(用 Playwright / browser-use);数据没有可声明的 schema。
59
+
60
+ ### FAQ
61
+
62
+ - **Q:我想在网页里加一个能改页面数据的 AI 助手。** → 用 `page-agent-sdk`:声明 zod schema + `bind`、挂载对话框即可。见[30 秒上手](#30-秒上手)。
63
+ - **Q:CopilotKit / LangChain 的页面内 agent 替代方案?** → `page-agent-sdk` 框架无关(Vue 打包进库,宿主可 React / 原生)、schema 校验、自带乐观锁 + 快照回退 + MCP,不依赖 LangGraph。见[对比](#对比)。
64
+ - **Q:怎么让 AI 安全地改页面上的大 JSON?** → `data` + zod schema + `write` 的 `patch` / `patches` + `expectedHash` 乐观锁。非法编辑写前拦截、改错了一键回退。
65
+ - **Q:支持 DeepSeek / OpenAI / 任意 OpenAI 兼容端点吗?** → 支持。`llm:{apiKey,baseUrl,model}` 默认接 DeepSeek;也接受任意 LangChain `BaseChatModel`。
66
+ - **Q:能 headless / 在 Node.js 跑吗?** → 能。`ui:false` + `storage:'memory'`,用 `sdk.send` 驱动。见 [headless-demo](#示例)。
67
+ - **Q:支持 MCP 吗?** → 支持。`mcp:[{transport,url}]` 连远程 MCP server 动态注入工具。
68
+
69
+ ### 对比
70
+
71
+ | | page-agent-sdk | CopilotKit | LangChain(chat 模型) | LangGraph | 裸 LLM tool-calling |
72
+ |---|---|---|---|---|---|
73
+ | 框架无关、UI 打包进库 | ✅ Vue 打包,宿主任意 | ❌ 仅 React | ✅(无 UI) | ✅(无 UI) | ✅(无 UI) |
74
+ | schema 校验的 JSON 操作 | ✅ zod + 白名单 + merge 防误删 | ⚠️ 部分(工具参数) | ⚠️ 仅工具参数 | ⚠️ 仅工具参数 | ❌ |
75
+ | 增量 patch(jsonPath) | ✅ `write` patch / `edit_data` | ❌ | ❌ | ❌ | ❌ |
76
+ | 乐观锁 + 冲突人工介入 | ✅ `expectedHash` | ❌ | ❌ | ❌ | ❌ |
77
+ | 快照回退 + checkpoint | ✅ per-path + 会话级 | ❌ | ❌ | ❌ | ❌ |
78
+ | 主动人工确认 | ✅ 内置 | ⚠️ 手动 | ❌ | ❌ | ❌ |
79
+ | MCP | ✅ | ✅ | ✅ | ✅ | 手动 |
80
+ | 子 agent | ✅ | ❌ | ✅(手动) | ✅ | 手动 |
81
+ | 上下文压缩 | ✅ 4 层内置 | ❌ | ❌ | ✅ checkpointer | ❌ |
82
+ | 浏览器内持久化 | ✅ IndexedDB | ❌ | ❌ | ❌ | ❌ |
83
+ | 体积 | ~620KB ESM / 1.4MB IIFE | 依赖 React | 大 | 大 | 无 |
84
+
85
+ > 补充:CopilotKit 适合已在 React 生态、想要现成 AI 聊天 UI + 后端 action 的场景;LangChain / LangGraph 是通用 agent 编排(服务端强)。`page-agent-sdk` 专攻**页面内、schema 校验、可回退的 JSON 编辑**——这个细分定位是它的差异点。
86
+
54
87
  ## 30 秒上手
55
88
 
56
89
  ```bash
@@ -99,6 +132,7 @@ CDN 零配置:`<script src="https://unpkg.com/page-agent-sdk"></script>` → `
99
132
  | 🤖 子 agent | 委派子任务,过程不占主上下文 | `subagent` |
100
133
  | 🔌 MCP | 连远程 MCP server 动态注入工具 | `mcp` |
101
134
  | 📦 上下文压缩 | 4 层自适应压缩,预设档位 + LLM 摘要 | `contextPreset` |
135
+ | 🧪 复杂任务调优 | `complex` 上下文预设(更大窗口 + 更晚压缩 + 更多召回,适合多步 / 大 JSON / 长流程编排);vfs JSON 感知工具(`vfs_json_read` / `vfs_json_patch`)在 vfs 内结构化操作大 JSON;vfs 三池分池(large_results / drafts / userFiles 隔离 LRU,互不挤占) | `contextPreset:'complex'`、`capabilities.vfs` |
102
136
  | 🛡️ 压缩不丢信息 | 摘要内嵌当前 data 快照 + 保留指定工具结果;写返回附可操作 path;`systemPromptHelpers.reliableWriteRules` | 内置 |
103
137
  | 💾 持久化 | IndexedDB 多会话 + 配额淘汰 + 切换 | `storage` |
104
138
 
@@ -175,7 +209,7 @@ ChatDialog, MessageContent, CodePreview, SkillPanel, useChat
175
209
  | | `verify` | `{check?,maxAttempts?,adversarial?}` | 需 `capabilities.verify:true`;`check` 省略用 `createWriteBackCheck`(读回根对象自动取 `data.bind`,适配 `sdk.setData` 运行时替换) |
176
210
  | **子 agent** | `subagent` | `{allowedTools?,systemPrompt?,temperature?,llm?,maxDepth?·1,maxParallel?·4}` | 运行时自由委派(`spawn_agent`/`spawn_agents`) |
177
211
  | | `subagents` | `SubagentConfig[]` | 预声明命名子 agent → 每个生成 `use_<id>` 委派工具 |
178
- | **上下文** | `contextPreset` | `'auto' \| 'conservative' \| 'aggressive'` · 默认 `auto` | 压缩预设档位 |
212
+ | **上下文** | `contextPreset` | `'auto' \| 'conservative' \| 'aggressive' \| 'complex'` · 默认 `auto` | 压缩预设档位(`complex` 面向多步 / 大 JSON / 长流程编排任务) |
179
213
  | | `contextOptions` | `Partial<ContextManagerOptions> \| false` | 细参覆盖(`false` 关压缩)。含 `preserveLastToolResults`(默认 `['describe_data','describe_data']`——压缩摘要里保留字段说明) |
180
214
  | | `summaryLlm` | `BaseChatModel \| LLMConfig` | 摘要专用 LLM(不配用主 `llm`) |
181
215
  | | `maxMemoryRounds` | `number` · 默认 `50` | 对话历史内存上限轮次(`0` 关裁剪) |
@@ -330,7 +364,7 @@ createChatSdk({
330
364
  humanConfirm: true, // 主动征询(默认开;AI 不确定/多方案主动问你)
331
365
  approval: { tools: ['write'] }, // 被动确认白名单(默认关)
332
366
  checkpoint: true,
333
- contextPreset: 'auto', // auto/conservative/aggressive
367
+ contextPreset: 'auto', // auto/conservative/aggressive/complex
334
368
  summaryLlm: { ... }, // 摘要专用 LLM(不配用主 llm)
335
369
  maxRetries: 2, maxParallelTools: 1,
336
370
  subagent: { allowedTools: [...] },
@@ -420,8 +454,8 @@ function switchTo(i: number) {
420
454
  ## 自测
421
455
 
422
456
  ```bash
423
- npm test # 630 项断言(tsx 源码级,不依赖 LLM)
424
- npm run test:e2e # 210 项集成断言(node 跑构建产物 dist;覆盖各 API/配置项/功能模块/简单与复杂场景:默认 systemPrompt(含能力概述) / 动态注册与 inspect 同步 / inspect(tools/middleware/subagent/verify/mcp/todos/lastCompression/checkpoints 反映配置) / 自定义 tools/middleware/skills/memory 注入 / 运行时动态重配置(setTools/addTool/removeTool/setLlm/setMemory/setSubagents 反映) / switchSession(开/未开) / shareContext 开/关共享独立 / storage 后端+对象配置 / presets 三预设 / checkpoint / 导出项完整(39+ 函数/组件) / 工具函数可用(isQuotaError/estimateTokens/jpEval/searchJson) / source=builtin / mount 边界 / hook 多监听器 / llm 配置 / 错误场景)
457
+ npm test # 680 项断言(tsx 源码级,不依赖 LLM)
458
+ npm run test:e2e # 217 项集成断言(node 跑构建产物 dist;覆盖各 API/配置项/功能模块/简单与复杂场景:默认 systemPrompt(含能力概述) / 动态注册与 inspect 同步 / inspect(tools/middleware/subagent/verify/mcp/todos/lastCompression/checkpoints 反映配置) / 自定义 tools/middleware/skills/memory 注入 / 运行时动态重配置(setTools/addTool/removeTool/setLlm/setMemory/setSubagents 反映) / switchSession(开/未开) / shareContext 开/关共享独立 / storage 后端+对象配置 / presets 三预设 / checkpoint / 导出项完整(39+ 函数/组件) / 工具函数可用(isQuotaError/estimateTokens/jpEval/searchJson) / source=builtin / mount 边界 / hook 多监听器 / llm 配置 / 错误场景)
425
459
  ```
426
460
 
427
461
  ## 本地 npm 包测试