page-agent-sdk 2.11.0 → 2.12.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 +13 -3
- package/README.zh-CN.md +13 -3
- package/dist/page-agent-sdk.css +1 -1
- package/dist/page-agent-sdk.iife.js +117 -117
- package/dist/page-agent-sdk.js +589 -433
- package/dist/page-agent-sdk.umd.cjs +19 -19
- package/package.json +1 -1
- package/skills/page-agent-sdk-integrate/references/advanced.md +28 -0
- package/skills/page-agent-sdk-integrate/references/api.md +14 -0
- package/skills/page-agent-sdk-integrate/references/options.md +2 -1
- package/types/index.d.ts +42 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "page-agent-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.12.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "框架无关的页面内 Agent JS SDK —— 以对话框形态挂载到任意网页,通过自定义 tool 读写宿主预注册的数据槽(经 schema 校验 + jsonPath 增量 patch + 快照回退;GET 抓文档),具备 planning/skills/虚拟工作区/context 管理能力。Vue 打包进库,使用者无需安装 Vue。",
|
|
6
6
|
"main": "./dist/page-agent-sdk.umd.cjs",
|
|
@@ -276,3 +276,31 @@ createChatSdk({
|
|
|
276
276
|
checkpoint: true, // rollback
|
|
277
277
|
}).mount()
|
|
278
278
|
```
|
|
279
|
+
|
|
280
|
+
## 6. Runtime dynamic reconfiguration (tools / llm / memory / subagents)
|
|
281
|
+
|
|
282
|
+
Beyond `setData` / `setSkills`, you can dynamically reconfigure **tools / LLM / memory / pre-declared subagents** at runtime — zero-breakage (not calling = current behavior), no agent rebuild (preserves conversation history & middleware state). All setters trigger `infoTick++` → DebugDrawer refresh; `inspect()` reflects the latest tools/model/memory/subagent.subagents.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
// 1. Tools: swap/append/remove user tools at runtime (built-ins untouched; internal rebind)
|
|
286
|
+
sdk.setTools([toolA, toolB]) // replace user tool set (built-ins stay)
|
|
287
|
+
sdk.addTool(toolC) // append (dedup by name)
|
|
288
|
+
sdk.removeTool('toolA') // remove by name → boolean
|
|
289
|
+
|
|
290
|
+
// 2. LLM: switch model at runtime (quota-exhausted→cheaper / complex task→stronger / switch provider)
|
|
291
|
+
sdk.setLlm({ apiKey, baseUrl, model: 'gpt-4o' }) // LLMConfig form (constructs ChatOpenAI)
|
|
292
|
+
sdk.setLlm(otherChatModel) // or pass a BaseChatModel instance
|
|
293
|
+
// rebinds tools + re-resolves model caps (contextWindow/maxOutputTokens); summaryLlm unaffected
|
|
294
|
+
|
|
295
|
+
// 3. Memory: update persistent directive at runtime (next augmentPrompt injects latest)
|
|
296
|
+
sdk.setMemory('User is VIP; prefer concise answers; use incremental patch when editing.')
|
|
297
|
+
sdk.setMemory('') // clear (empty string skips injection)
|
|
298
|
+
|
|
299
|
+
// 4. Subagents: runtime add/remove pre-declared subagents (requires subagents:[] at creation)
|
|
300
|
+
// Pass subagents: [] (empty array) at creation to enable the controller for dynamic add later.
|
|
301
|
+
sdk.addSubagent({ id: 'translator', description: '中英互译子 agent', systemPrompt: '你是翻译助手。' })
|
|
302
|
+
sdk.removeSubagent('translator') // → boolean
|
|
303
|
+
sdk.setSubagents([{ id: 'a', description: 'A' }, { id: 'b', description: 'B' }]) // replace all
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
> **Note**: `setSystemPrompt` / `setMiddleware` (runtime middleware-array swap) are not yet implemented — they touch the harness core and are deferred. Use `setData` / `setSkills` / `augmentSystem` hook to cover most dynamic system-prompt scenarios. See `doc/roadmap.md` #5.
|
|
@@ -19,6 +19,14 @@
|
|
|
19
19
|
| `setSkills(skills)` | `(skills: SkillSpec[]) => void` | Runtime swap the entire skill list (same-name skill overwrites). Takes effect next round: the skill index section of the system prompt re-renders with the new skills; clears the skill full-text cache & in-round loaded set, so the next `load_skill` re-fetches the latest full text (incl. vfs doc). Requires skills enabled (default on). |
|
|
20
20
|
| `invalidateSkillCache(name?)` | `(name?: string) => void` | Invalidate the skill full-text cache (proactive invalidation when a dynamic skill's content changes). Omit `name` to clear all; pass `name` to clear one. The next `load_skill` re-runs `getContent`/`readSkillDoc`. Requires skills enabled (default on). |
|
|
21
21
|
| `usage` | `TokenUsage` | Cumulative token usage `{prompt_tokens, completion_tokens, total_tokens}` (accumulated per LLM call). |
|
|
22
|
+
| `setTools(tools)` | `(tools: StructuredToolInterface[]) => void` | Runtime swap user tools (built-ins untouched; internal `rebindTools` re-binds to LLM; next round uses new set). Zero-breakage: not calling = current behavior. Supports per-permission/business-stage/A-B-test dynamic tool groups without rebuilding agent. |
|
|
23
|
+
| `addTool(tool)` | `(tool: StructuredToolInterface) => void` | Append user tool at runtime (dedup by name; built-ins untouched). |
|
|
24
|
+
| `removeTool(name)` | `(name: string) => boolean` | Remove user tool at runtime (built-ins untouched). Returns whether removed. |
|
|
25
|
+
| `setLlm(llm)` | `(llm: BaseChatModel \| LLMConfig) => void` | Switch LLM at runtime (quota-exhausted→cheaper model / complex task→stronger model / switch provider). Param `BaseChatModel` or `LLMConfig` (constructs `ChatOpenAI` internally). Rebinds tools + re-resolves model caps (`contextWindow`/`maxOutputTokens`). `summaryLlm` unaffected. If new model lacks `bindTools`, tool-calling degrades (agent stays up). |
|
|
26
|
+
| `setMemory(text)` | `(text: string) => void` | Update persistent memory directive at runtime (next `augmentPrompt` injects latest; `setMemory('')` clears). |
|
|
27
|
+
| `setSubagents(configs)` | `(configs: SubagentConfig[]) => void` | Runtime swap pre-declared subagents (regenerates `use_<id>` delegation tools + triggers rebind). Requires `subagents:[]` at creation (else controller is null, setter warns, no throw). |
|
|
28
|
+
| `addSubagent(config)` | `(config: SubagentConfig) => void` | Append pre-declared subagent at runtime (duplicate id warns & skips). Requires `subagents:[]` at creation. |
|
|
29
|
+
| `removeSubagent(id)` | `(id: string) => boolean` | Remove pre-declared subagent at runtime (by id). Returns whether removed. Requires `subagents:[]` at creation. |
|
|
22
30
|
| `restoreLastCheckpoint()` | `() => boolean` | Restore last good checkpoint (needs `checkpoint` enabled). |
|
|
23
31
|
| `listCheckpoints()` | `() => CheckpointMeta[]` | List available checkpoints. |
|
|
24
32
|
| `addSkill(skill)` | `(skill: { name, description, prompt \| getContent \| doc }) => void` | Add a user-created skill at runtime. Auto-merges into the skill list, persists via **independent SkillStore** (default indexedDB, separate from `storage` option), takes effect next round. Same-name overwrites. Requires `capabilities.skills` (default on) + `skillStorage` not `false` for persistence. |
|
|
@@ -164,6 +172,12 @@ createChatSdk({
|
|
|
164
172
|
- **Notifying the outside world of changes**: subscribe `data_change` via `onEvent` (constructor) or `sdk.hook` (runtime, multi-listener, cancellable) — fires after `write`/`set`/`edit`/`delete`/`restore`, with `operation`/`value`. For Vue + reactive bind, template/watch auto-react (no manual notify needed); `onEvent` can coexist for audit/analytics.
|
|
165
173
|
- **Runtime swap**: `sdk.setData({ schema, bind, description? })` replaces the whole config; tools pick up immediately (no rebuild). Snapshots & lock hash reset.
|
|
166
174
|
- **Runtime skill swap**: `sdk.setSkills(skills)` replaces the entire skill list (same-name overwrites); the skill index section of the system prompt re-renders next round, and the skill full-text cache is cleared so the next `load_skill` re-fetches the latest content (incl. vfs doc). Use `sdk.invalidateSkillCache(name?)` to proactively invalidate the cache when a dynamic skill's content changes (without swapping the whole list).
|
|
175
|
+
- **Runtime dynamic reconfiguration (zero-breakage; not calling = current behavior)**: beyond data/skills, you can also dynamically reconfigure tools / LLM / memory / subagents at runtime without rebuilding the agent:
|
|
176
|
+
- `sdk.setTools(tools)` / `addTool(tool)` / `removeTool(name)` — swap/append/remove user tools (built-ins untouched; internal `rebindTools` re-binds to LLM; next round uses new set). Use cases: per-permission tool groups, business-stage gating, A/B experiments.
|
|
177
|
+
- `sdk.setLlm(llm)` — switch LLM at runtime (quota-exhausted→cheaper model / complex task→stronger model / switch provider). Param `BaseChatModel` or `LLMConfig`. Rebinds tools + re-resolves model caps. `summaryLlm` unaffected.
|
|
178
|
+
- `sdk.setMemory(text)` — update the persistent memory directive at runtime (next `augmentPrompt` injects latest).
|
|
179
|
+
- `sdk.setSubagents(configs)` / `addSubagent(config)` / `removeSubagent(id)` — swap/append/remove pre-declared subagents (regenerates `use_<id>` delegation tools + triggers rebind). Requires `subagents:[]` at creation.
|
|
180
|
+
- All setters trigger `infoTick++` → DebugDrawer refreshes; `inspect()` reflects the latest tools/model/memory/subagent.subagents.
|
|
167
181
|
|
|
168
182
|
## Exported building blocks (for custom UIs)
|
|
169
183
|
|
|
@@ -9,6 +9,7 @@ Full reference for `createChatSdk(options)`. Grouped by purpose. Required: `llm`
|
|
|
9
9
|
| `llm` | `LLMConfig \| BaseChatModel` | — (required) | The model. `LLMConfig = { apiKey, baseUrl, model, temperature?, maxTokens? }` (OpenAI-compatible; DeepSeek default). Or pass any LangChain `BaseChatModel` (e.g. `ChatAnthropic`, install its peerDep). |
|
|
10
10
|
| `systemPrompt` | `string` | built-in default (JSON-operation assistant + reliable write rules) | Agent identity/instructions. Inject here, not hardcoded. Keep single-line in `.env` (`VITE_AI_SYSTEM_PROMPT`). If omitted, a built-in default is used (JSON-operation assistant + `systemPromptHelpers.reliableWriteRules`); passing your own fully overrides it. |
|
|
11
11
|
| `appendReliableWriteRules` | `boolean` | `true` | When `true` (default) and a custom `systemPrompt` is set, auto-append `systemPromptHelpers.reliableWriteRules` to it with a `---` separator (clearly distinguishes user content from SDK-appended write rules; avoids forgetting the write rules). Set `false` to disable. No effect when `systemPrompt` is omitted (default prompt already includes them). |
|
|
12
|
+
| `augmentSystem` | `(ctx:{state,data?}) => string \| undefined` | — | Dynamic system-prompt injection hook. Called each turn; return a string to inject as a segment, or `undefined` to skip. Callback errors degrade to skip (no crash). `ctx.data` is taken from `liveData()` each turn (auto-syncs after `setData`), so you can compute dynamic component descriptions / partial schema hints from current runtime state. Segment is placed after built-in segments (base/dataHint/usageHints/.../subagents) and before user `middleware`. Not set = current behavior (no segment). See `doc/system-prompt.md` §B6. |
|
|
12
13
|
| `id` | `string` | random + warn | Stable agent id for multi-agent isolation & persistence. **Must pass a stable value** if you use `storage` or run multiple agents on one page. |
|
|
13
14
|
|
|
14
15
|
## UI & mounting
|
|
@@ -98,7 +99,7 @@ Full reference for `createChatSdk(options)`. Grouped by purpose. Required: `llm`
|
|
|
98
99
|
| Option | Type | Default | Purpose / when |
|
|
99
100
|
|---|---|---|---|
|
|
100
101
|
| `subagent` | `object` | enabled | `{ enabled?, allowedTools?, systemPrompt?, temperature?, maxTokens?, skills?, llm?, maxDepth?, maxParallel? }`. `maxDepth` (1) physically cuts recursion. Subagents get a read-only tool subset (no spawn). |
|
|
101
|
-
| `subagents` | `SubagentConfig[]` | `[]` | Pre-declared named subagents → each auto-generates a `use_<id>({ task })` delegation tool (Claude-Code style). Fixed roles (research/review) vs ad-hoc `spawn_agent`. |
|
|
102
|
+
| `subagents` | `SubagentConfig[]` | `[]` | Pre-declared named subagents → each auto-generates a `use_<id>({ task })` delegation tool (Claude-Code style). Fixed roles (research/review) vs ad-hoc `spawn_agent`. Pass `[]` (empty array) to enable the `SubagentsController` for runtime `addSubagent`/`removeSubagent`/`setSubagents` (initial no subagents, add dynamically later). |
|
|
102
103
|
|
|
103
104
|
## Verify (self-check before return)
|
|
104
105
|
|
package/types/index.d.ts
CHANGED
|
@@ -110,6 +110,8 @@ export interface SubagentInfo {
|
|
|
110
110
|
maxDepth: number;
|
|
111
111
|
maxParallel: number;
|
|
112
112
|
allowedTools: string[];
|
|
113
|
+
/** 预声明子 agent 列表(动态:反映 setSubagents/addSubagent/removeSubagent 后的最新) */
|
|
114
|
+
subagents?: { id: string; description: string }[];
|
|
113
115
|
}
|
|
114
116
|
/** 预声明子 agent 配置(同主配置子集 + id/description;缺省继承主 agent) */
|
|
115
117
|
export interface SubagentConfig {
|
|
@@ -369,6 +371,16 @@ export interface SessionOptions {
|
|
|
369
371
|
title?: string;
|
|
370
372
|
}
|
|
371
373
|
|
|
374
|
+
/**
|
|
375
|
+
* augmentSystem 钩子上下文:集成方回调据此按运行时状态动态注入 system prompt 段。
|
|
376
|
+
* - `state`:harness 当前状态(messages/todos/files/skills/memory…);不含 data(data 是 createChatSdk 层概念)
|
|
377
|
+
* - `data`:当前主数据配置(每轮从 liveData() 取最新,setData 后自动同步;含 schema/bind/description)
|
|
378
|
+
*/
|
|
379
|
+
export interface SystemAugmentContext {
|
|
380
|
+
state: any;
|
|
381
|
+
data?: DataConfig;
|
|
382
|
+
}
|
|
383
|
+
|
|
372
384
|
export interface ChatSdkOptions {
|
|
373
385
|
container?: string | HTMLElement;
|
|
374
386
|
/** UI:'default'(内置 ChatDialog)/ false(headless 不渲染,自建 UI) */
|
|
@@ -382,9 +394,17 @@ export interface ChatSdkOptions {
|
|
|
382
394
|
session?: SessionOptions;
|
|
383
395
|
/** 共享上下文:默认 false;true 时同 id 复用同一核心(messages/agent/工作区) */
|
|
384
396
|
shareContext?: boolean;
|
|
397
|
+
/** 系统提示词(base + 可操作数据段,数据段随 data 动态;不含 todos/skills/memory/augmentSystem 等运行态 augmentPrompt 段) */
|
|
385
398
|
systemPrompt?: string;
|
|
386
399
|
/** 自定义 systemPrompt 时是否自动追加 reliableWriteRules(默认 true,用 '---' 分隔线区分;设 false 关闭;不传 systemPrompt 用默认 prompt 时已内置,此项无效) */
|
|
387
400
|
appendReliableWriteRules?: boolean;
|
|
401
|
+
/**
|
|
402
|
+
* 动态 system prompt 注入钩子:每轮 buildSystemPrompt 时调用,集成方按运行时状态(state/data)返回字符串 → 作为 system prompt 一段注入;返回 undefined → 跳过。
|
|
403
|
+
* - ctx.data 每轮从 liveData() 取最新(setData 后自动同步),可据此动态算组件说明 / 部分 schema 描述
|
|
404
|
+
* - 回调异常降级为跳过该段 + debug 日志(不崩 agent)
|
|
405
|
+
* - 段排在内置段之后、用户 middleware 之前;不配 = 完全现状行为
|
|
406
|
+
*/
|
|
407
|
+
augmentSystem?: (ctx: SystemAugmentContext) => string | undefined;
|
|
388
408
|
tools?: any[];
|
|
389
409
|
skills?: SkillSpec[];
|
|
390
410
|
/** 用户创建 skill 的独立持久化存储(与 storage 选项分离)。默认 `{ backend: 'indexed' }`(即使 storage:false 也持久化);`false` 关闭;`id` 手动指定同一 id 可跨页面/跨 agent 复用 */
|
|
@@ -525,6 +545,22 @@ export interface ChatSdk {
|
|
|
525
545
|
pendingConflict: Ref<PendingConflict | null>;
|
|
526
546
|
/** 冲突解决:用户点「保留外部」(keep_external)/「强制覆盖」(overwrite)/「回退」(restore) → 收口挂起的 conflict,被挂起的工具调用继续 */
|
|
527
547
|
resolveConflict(action: ConflictResolution['action']): void;
|
|
548
|
+
/** 运行时替换用户工具集(内置工具不动);立即 rebind + infoTick 刷新 */
|
|
549
|
+
setTools(tools: any[]): void;
|
|
550
|
+
/** 运行时追加用户工具(去重 by name);立即生效 */
|
|
551
|
+
addTool(tool: any): void;
|
|
552
|
+
/** 运行时移除用户工具(by name;内置不动);返回是否移除成功 */
|
|
553
|
+
removeTool(name: string): boolean;
|
|
554
|
+
/** 运行时切换 LLM(BaseChatModel 或 LLMConfig);rebind + 重解析能力 + infoTick */
|
|
555
|
+
setLlm(llm: ChatModelLike | LLMConfig): void;
|
|
556
|
+
/** 运行时更新 memory 文本;立即生效 + infoTick */
|
|
557
|
+
setMemory(text: string): void;
|
|
558
|
+
/** 运行时替换预声明子 agent 列表(重新生成委派工具 + rebind);需创建时配 subagents:[] */
|
|
559
|
+
setSubagents(configs: SubagentConfig[]): void;
|
|
560
|
+
/** 运行时追加预声明子 agent(id 重复 warn 跳过);需创建时配 subagents:[] */
|
|
561
|
+
addSubagent(config: SubagentConfig): void;
|
|
562
|
+
/** 运行时移除预声明子 agent(by id);返回是否移除成功;需创建时配 subagents:[] */
|
|
563
|
+
removeSubagent(id: string): boolean;
|
|
528
564
|
}
|
|
529
565
|
|
|
530
566
|
/** 乐观锁冲突挂起(dataOps 写入时 expectedHash 不匹配,挂起等用户决定) */
|
|
@@ -683,6 +719,12 @@ export interface StateUpdate { [k: string]: any }
|
|
|
683
719
|
|
|
684
720
|
// 子 agent
|
|
685
721
|
export declare function createSubagentsMiddleware(opts: any): any;
|
|
722
|
+
export interface SubagentsController {
|
|
723
|
+
set(configs: SubagentConfig[]): void;
|
|
724
|
+
add(config: SubagentConfig): void;
|
|
725
|
+
remove(id: string): boolean;
|
|
726
|
+
get(): SubagentConfig[];
|
|
727
|
+
}
|
|
686
728
|
export interface SubagentOptions { [k: string]: any }
|
|
687
729
|
export interface SubagentLlmConfig { [k: string]: any }
|
|
688
730
|
|