page-agent-sdk 2.4.0 → 2.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "page-agent-sdk",
3
- "version": "2.4.0",
3
+ "version": "2.5.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",
package/types/index.d.ts CHANGED
@@ -45,7 +45,7 @@ export type StreamHandler = (event: StreamEvent) => void;
45
45
  /**
46
46
  * SDK 事件(供 createChatSdk({ onEvent }) 订阅常用时机)。
47
47
  * 复用 StreamEvent(round_start/reasoning/text/tool_call/tool_result/subagent/done;approval_request 不外发)
48
- * + 额外时机:data_slot_change / message_update / error。
48
+ * + 额外时机:data_change / message_update / error。
49
49
  */
50
50
  export type SdkEvent =
51
51
  | { type: 'round_start'; round: number }
@@ -55,7 +55,7 @@ export type SdkEvent =
55
55
  | { type: 'tool_result'; name: string; result: string; status: 'done' | 'error' }
56
56
  | { type: 'subagent'; taskId: string; label: string; kind: 'tool_call' | 'tool_result'; name: string; args?: any; result?: string; status?: 'done' | 'error' }
57
57
  | { type: 'done'; content: string }
58
- | { type: 'data_slot_change'; path: string; operation: 'set' | 'edit' | 'delete' | 'restore'; value?: unknown }
58
+ | { type: 'data_change'; operation: 'set' | 'edit' | 'delete' | 'restore'; value?: unknown }
59
59
  | { type: 'message_update'; count: number }
60
60
  | { type: 'conflict'; conflict: PendingConflict }
61
61
  | { type: 'error'; message: string };
@@ -80,7 +80,7 @@ export interface ChatDialogProps {
80
80
 
81
81
  export interface ToolInfo { name: string; description: string; schema?: unknown; source?: string }
82
82
  export interface SkillInfo { name: string; description: string }
83
- export interface DataSlotInfo { path: string; description: string; schema?: unknown }
83
+ export interface DataInfo { description?: string; schema?: unknown }
84
84
  export interface SubagentInfo {
85
85
  enabled: boolean;
86
86
  maxDepth: number;
@@ -108,7 +108,7 @@ export interface AgentInfo {
108
108
  systemPrompt: string;
109
109
  tools: ToolInfo[];
110
110
  skills: SkillInfo[];
111
- dataSlots: DataSlotInfo[];
111
+ data?: DataInfo;
112
112
  memory: string;
113
113
  middleware: string[];
114
114
  todos: { content: string; status: string }[];
@@ -149,61 +149,49 @@ export type ChatModelLike = {
149
149
  bindTools: (tools: any[]) => any;
150
150
  };
151
151
 
152
- export interface DataSlotSpec {
153
- /** window 上的路径,支持点号嵌套 */
154
- path: string;
155
- /** 属性说明,供 Agent 理解用途;若传了 bind 且未传 description,自动生成 `${path}(bind 直连)` */
156
- description?: string;
157
- /** 值的 zod schema(写入时校验);字段的 .describe() 自动提取注入 systemPrompt「可操作属性」段 */
152
+ export interface DataConfig {
153
+ /** 值的 zod schema(写入时校验);字段的 .describe() 自动提取注入 systemPrompt「可操作数据」段 */
158
154
  schema: any;
159
- /** 可选:传 reactive/普通对象,自动挂 window[path] = bind(reactive 写后响应式刷新;普通对象可写但不响应) */
160
- bind?: any;
155
+ /** 数据源:reactive/普通对象,工具直接读写 bind(reactive 写后响应式刷新;不挂 window) */
156
+ bind: any;
157
+ /** 数据说明,供 Agent 理解用途;不传则自动生成 */
158
+ description?: string;
161
159
  }
162
- /** createDataSlotOps 选项(审计回调 / 只读探测 / 快照上限 / 字段白名单读 / 乐观锁) */
163
- export interface DataSlotOpsOptions {
164
- onAudit?: (entry: { op: string; path: string; value?: any; detail?: string; timestamp: number }) => void;
165
- allowRawRead?: boolean;
160
+ /** createDataOps 选项(审计回调 / 快照上限 / 乐观锁) */
161
+ export interface DataOpsOptions {
162
+ onAudit?: (entry: { op: string; value?: any; detail?: string; timestamp: number }) => void;
166
163
  maxSnapshots?: number;
167
- /**
168
- * 字段白名单读模式(默认 true):仅允许读「注册 path 自身 / 其后代」,禁止读未注册的祖先,
169
- * 防止 LLM 经 get_data_slot('page') 把整个大 JSON 拉进上下文。
170
- * 集成方注册「可操作子路径」(如 page.theme.color / page.components)而非顶层时,默认即「LLM 只见声明字段」。
171
- * 设 false 回退原行为(允许读注册 path 的祖先,即整体读)。
172
- */
173
- whitelist?: boolean;
174
164
  /** 乐观锁冲突人工介入回调(详见 ConflictInfo/ConflictResolution);不传则冲突时返回 VERSION_CONFLICT 错误 */
175
165
  onConflict?: (conflict: ConflictInfo) => Promise<ConflictResolution>;
176
166
  /**
177
- * 自动乐观锁(默认 true):写入时若 LLM 未显式传 expectedHash,自动用「LLM 最后一次 get_data_slot 读到的 hash」作基准比对。
167
+ * 自动乐观锁(默认 true):写入时若 LLM 未显式传 expectedHash,自动用「LLM 最后一次 read/get 读到的 hash」作基准比对。
178
168
  * LLM 无需手动传 expectedHash 即可享受乐观锁保护;冲突走 onConflict(无 onConflict 则返回 VERSION_CONFLICT)。
179
169
  * LLM 未读过直接写(无基准记录)时跳过锁(等同不校验)。设 false 回退「不传 expectedHash = 不校验」的旧行为。
180
170
  */
181
171
  autoLock?: boolean;
182
- /** 读写拦截器:read/write 透传给数据槽工具(脱敏/转换/审计/拒绝 LLM 读写) */
183
- interceptors?: DataSlotInterceptors;
172
+ /** 读写拦截器:read/write 透传给数据工具(脱敏/转换/审计/拒绝 LLM 读写) */
173
+ interceptors?: DataInterceptors;
184
174
  }
185
175
 
186
- /** 数据槽读写拦截器(集成方可脱敏/转换/审计/拒绝 LLM 的读写) */
187
- export interface DataSlotInterceptors {
188
- /** LLM 读时拦截:path + 原始值 → 改写后返回给 LLM(如脱敏/派生);抛错则返回 READ_INTERCEPT 错误 */
189
- read?: (path: string, value: any) => any;
190
- /** LLM 写时拦截:path + 欲写值 + 当前值 → 改写后的值,或 { error } 拒绝;抛错则拒绝 */
191
- write?: (path: string, payload: any, current: any) => any | { error: string };
176
+ /** 数据读写拦截器(集成方可脱敏/转换/审计/拒绝 LLM 的读写) */
177
+ export interface DataInterceptors {
178
+ /** LLM 读时拦截:原始值 → 改写后返回给 LLM(如脱敏/派生);抛错则返回 READ_INTERCEPT 错误 */
179
+ read?: (value: any) => any;
180
+ /** LLM 写时拦截:欲写值 + 当前值 → 改写后的值,或 { error } 拒绝;抛错则拒绝 */
181
+ write?: (payload: any, current: any) => any | { error: string };
192
182
  }
193
183
 
194
184
  /** 工具呈现模式:simple=主推 read/write 但保留高级能力(默认)| advanced=全暴露| minimal=只 read/write */
195
185
  export type ToolMode = 'simple' | 'advanced' | 'minimal';
196
186
 
197
- /** 数据槽注册表控制器(运行时动态增删;createDataSlotOps 返回的工具数组上以不可枚举属性 `controller` 挂载) */
198
- export interface DataSlotOpsController {
199
- /** 新增/覆盖一个属性注册项(运行时懒加载组件场景);覆盖时旧快照栈保留 */
200
- add(spec: DataSlotSpec): void;
201
- /** 移除一个属性注册项;返回是否确实存在并移除。快照栈一并清理 */
202
- remove(path: string): boolean;
203
- /** 列出当前所有注册项(反映动态增删后的最新状态) */
204
- list(): DataSlotSpec[];
205
- /** 是否已注册某 path */
206
- has(path: string): boolean;
187
+ /** 数据操作控制器(运行时替换配置;createDataOps 返回的工具数组上以不可枚举属性 `controller` 挂载) */
188
+ export interface DataOpsController {
189
+ /** 读取当前配置 */
190
+ get(): DataConfig;
191
+ /** 替换主数据配置(如页面切换、schema 变更);清空快照栈与乐观锁缓存 */
192
+ set(config: DataConfig): void;
193
+ /** 仅替换 bind 引用;清空快照栈与乐观锁缓存 */
194
+ update(bind: any): void;
207
195
  }
208
196
 
209
197
  export interface PermissionRule {
@@ -241,7 +229,7 @@ export interface VerifyMiddlewareOptions {
241
229
  }
242
230
  /** createWriteBackCheck 选项 */
243
231
  export interface WriteBackCheckOptions {
244
- /** path → zod schema(由 createChatSdk 从 dataSlots 构造注入);省略则只校验「读回非空」 */
232
+ /** name → zod schema(由 createChatSdk 从 data 构造注入,键 '' 代表主数据);省略则只校验「读回非空」 */
245
233
  schemas?: Record<string, any>;
246
234
  /** 读 window 的根对象(默认 globalThis.window) */
247
235
  window?: unknown;
@@ -349,21 +337,21 @@ export interface ChatSdkOptions {
349
337
  tools?: any[];
350
338
  skills?: SkillSpec[];
351
339
  memory?: string;
352
- dataSlots?: DataSlotSpec[];
340
+ data?: DataConfig;
353
341
  permissions?: PermissionRule[];
354
342
  /** 自定义中间件(注入到内置中间件之后;可拦截/观察模型调用、工具、prompt) */
355
343
  middleware?: any[];
356
344
  vfs?: { initialFiles?: Record<string, string>; maxBytes?: number };
357
- /** 每个 数据槽最多保留快照数(默认 20) */
345
+ /** 每个数据对象最多保留快照数(默认 20) */
358
346
  maxSnapshots?: number;
359
347
  /** 自动乐观锁(默认 true):写入时若 LLM 未传 expectedHash,自动用其最后 get 读到的 hash 比对;设 false 回退「不传 = 不校验」 */
360
348
  autoLock?: boolean;
361
349
  /** 工具呈现模式:simple(默认,主推 read/write 但保留 query/search/eval/snapshot)| advanced(全暴露)| minimal(只 read/write) */
362
350
  toolMode?: 'simple' | 'advanced' | 'minimal';
363
- /** 读写拦截器:read/write 透传给数据槽工具(脱敏/转换/审计/拒绝 LLM 读写);input/output 在 agent IO 入口/出口预处理 */
351
+ /** 读写拦截器:read/write 透传给数据工具(脱敏/转换/审计/拒绝 LLM 读写);input/output 在 agent IO 入口/出口预处理 */
364
352
  interceptors?: {
365
- read?: (path: string, value: any) => any;
366
- write?: (path: string, payload: any, current: any) => any | { error: string };
353
+ read?: (value: any) => any;
354
+ write?: (payload: any, current: any) => any | { error: string };
367
355
  /** agent 接收输入时拦截:send/stream 的 user message 预处理(可改写/审计) */
368
356
  input?: (input: any) => any;
369
357
  /** agent 产出输出时拦截:返回前 postprocess(可改写最终回复) */
@@ -382,7 +370,7 @@ export interface ChatSdkOptions {
382
370
  /** 模型最大输出(token);顶层声明对 llm 实例场景也生效,缺省按 model 名查表 */
383
371
  maxOutputTokens?: number;
384
372
  /** 子 agent 委派(默认开启;{ enabled: false } 关闭) */
385
- capabilities?: { dataSlotOps?: boolean; fetch?: boolean; planning?: boolean; skills?: boolean; vfs?: boolean; summarization?: boolean; memory?: boolean; subagent?: boolean; verify?: boolean };
373
+ capabilities?: { dataOps?: boolean; fetch?: boolean; planning?: boolean; skills?: boolean; vfs?: boolean; summarization?: boolean; memory?: boolean; subagent?: boolean; verify?: boolean };
386
374
  subagent?: { enabled?: boolean; allowedTools?: string[]; systemPrompt?: string; temperature?: number; maxTokens?: number; skills?: SkillSpec[]; llm?: LLMConfig | ChatModelLike; maxDepth?: number; maxParallel?: number };
387
375
  /** 预声明子 agent 列表:每个用同主配置方式声明,自动生成 use_<id> 委派工具(与 spawn_agent 共存) */
388
376
  subagents?: SubagentConfig[];
@@ -426,30 +414,27 @@ export interface ChatSdk {
426
414
  send(message: string): Promise<string>;
427
415
  switchSession(sessionId?: string): Promise<string>;
428
416
  stream: (messages: AgentMessage[], onEvent: StreamHandler, signal?: AbortSignal) => Promise<string>;
429
- /** 检视 agent 详细信息(tools/skills/dataSlots/middleware/todos) */
417
+ /** 检视 agent 详细信息(tools/skills/data/middleware/todos) */
430
418
  inspect(): AgentInfo;
431
- /** 回退到最近一次正常 checkpoint(整体还原对话历史 + 数据槽注册项 + vfs + todos);需开启 checkpoint,无可用返回 false */
419
+ /** 回退到最近一次正常 checkpoint(整体还原对话历史 + 主数据 + vfs + todos);需开启 checkpoint,无可用返回 false */
432
420
  restoreLastCheckpoint(): boolean;
433
421
  /** 列出可用 checkpoint(回退点);需开启 checkpoint,未开启返回空数组 */
434
422
  listCheckpoints(): CheckpointMeta[];
435
423
  /** 运行时订阅 SDK 事件(可多个监听器,返回取消函数);与构造时 onEvent 互补 */
436
424
  hook(handler: SdkEventHandler): () => void;
437
- /** 运行时动态新增/覆盖一个 数据槽注册项(懒加载组件:组件挂载时注册其 schema);立即对 数据槽工具生效,无需重建 agent。需开启 dataSlotOps */
438
- addDataSlot(spec: DataSlotSpec): void;
439
- /** 运行时移除一个 数据槽注册项(组件卸载);返回是否确实存在并移除。快照栈一并清理 */
440
- removeDataSlot(path: string): boolean;
441
- /** 列出当前所有已注册 数据槽(反映动态增删后的最新状态) */
442
- listDataSlots(): DataSlotSpec[];
425
+ /** 运行时替换主数据配置(如页面切换、schema 变更);立即对数据工具生效,无需重建 agent。需开启 dataOps */
426
+ setData(config: DataConfig): void;
427
+ /** 读取当前主数据配置;dataOps 关闭时返回 undefined */
428
+ getData(): DataConfig | undefined;
443
429
  /** 乐观锁冲突挂起状态(响应式 ref;无冲突为 null,有冲突时 UI 据此渲染冲突对话框)。headless 集成方可 watch 自建 UI */
444
430
  pendingConflict: Ref<PendingConflict | null>;
445
431
  /** 冲突解决:用户点「保留外部」(keep_external)/「强制覆盖」(overwrite)/「回退」(restore) → 收口挂起的 conflict,被挂起的工具调用继续 */
446
432
  resolveConflict(action: ConflictResolution['action']): void;
447
433
  }
448
434
 
449
- /** 乐观锁冲突挂起(dataSlotOps 写入时 expectedHash 不匹配,挂起等用户决定) */
435
+ /** 乐观锁冲突挂起(dataOps 写入时 expectedHash 不匹配,挂起等用户决定) */
450
436
  export interface PendingConflict {
451
437
  id: number;
452
- path: string;
453
438
  op: 'set' | 'edit' | 'delete';
454
439
  agentValue?: unknown;
455
440
  currentValue: unknown;
@@ -465,9 +450,8 @@ export type ConflictResolution =
465
450
  | { action: 'overwrite' }
466
451
  | { action: 'restore' };
467
452
 
468
- /** 乐观锁冲突信息(dataSlotOps onConflict 回调参数) */
453
+ /** 乐观锁冲突信息(dataOps onConflict 回调参数) */
469
454
  export interface ConflictInfo {
470
- path: string;
471
455
  op: 'set' | 'edit' | 'delete';
472
456
  agentValue?: unknown;
473
457
  currentValue: unknown;
@@ -483,13 +467,13 @@ export declare function defineTool(opts: {
483
467
  schema: any;
484
468
  handler: (args: any) => unknown | Promise<unknown>;
485
469
  }): any;
486
- export declare function createDataSlotOps(props: DataSlotSpec[], opts?: DataSlotOpsOptions): any[];
470
+ export declare function createDataOps(config: DataConfig, opts?: DataOpsOptions): any[];
487
471
  export declare function filterByToolMode(tools: any[], mode?: 'simple' | 'advanced' | 'minimal'): any[];
488
- export declare function selectBuiltinTools(caps: { dataSlotOps?: boolean; fetch?: boolean } | undefined, dataSlotOps: any[], fetchDocs: any[]): any[];
489
- export declare function createUsageHintsMiddleware(caps: { planning?: boolean; dataSlotOps?: boolean; subagent?: boolean } | undefined, hasDataSlotOps: boolean): any;
472
+ export declare function selectBuiltinTools(caps: { dataOps?: boolean; fetch?: boolean } | undefined, dataOps: any[], fetchDocs: any[]): any[];
473
+ export declare function createUsageHintsMiddleware(caps: { planning?: boolean; dataOps?: boolean; subagent?: boolean } | undefined, hasDataOps: boolean, toolMode?: 'simple' | 'advanced' | 'minimal'): any;
490
474
  export declare const fetchDocTools: any[];
491
475
  export declare const fetchTools: any[];
492
- export declare function defineDataSlotToolset(props: DataSlotSpec[], opts?: DataSlotOpsOptions): any[];
476
+ export declare function defineDataToolset(config: DataConfig, opts?: DataOpsOptions): any[];
493
477
  export declare function defineSkill(spec: SkillSpec): SkillSpec;
494
478
  export declare function createAgent(options: any): any;
495
479
  /** 检测模型把工具调用写成文本(伪 XML/标签)而非标准 tool_calls 的异常格式;主循环据此回灌 feedback 自纠 */
@@ -590,10 +574,10 @@ export declare function createSubagentsMiddleware(opts: any): any;
590
574
  export interface SubagentOptions { [k: string]: any }
591
575
  export interface SubagentLlmConfig { [k: string]: any }
592
576
 
593
- // checkpoint / dataSlotOps / permissions
577
+ // checkpoint / dataOps / permissions
594
578
  export interface CheckpointDeps { [k: string]: any }
595
- export interface DataSlotAuditEntry { [k: string]: any }
596
- export interface DataSlotSnapshotEntry { [k: string]: any }
579
+ export interface DataAuditEntry { [k: string]: any }
580
+ export interface DataSnapshotEntry { [k: string]: any }
597
581
  export type PermissionOp = string;
598
582
 
599
583
  // vfs