@coolkiller007/my-page-agent 0.1.13 → 0.1.14
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 +0 -1
- package/dist/index.d.ts +225 -196
- package/dist/index.js +422 -291
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -6,13 +6,13 @@ declare interface ActionResult {
|
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Agent
|
|
9
|
+
* Agent 活动 - 用于即时 UI 反馈的瞬态状态。
|
|
10
10
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* 与历史事件(被持久化)不同,活动是短暂的,
|
|
12
|
+
* 表示"Agent 此刻正在做什么"。UI 组件应监听
|
|
13
|
+
* 'activity' 事件以显示实时反馈。
|
|
14
14
|
*
|
|
15
|
-
*
|
|
15
|
+
* 注:没有 'idle' 活动 —— 没有活动事件即表示空闲。
|
|
16
16
|
*/
|
|
17
17
|
export declare type AgentActivity = {
|
|
18
18
|
type: 'thinking';
|
|
@@ -38,18 +38,18 @@ export declare type AgentActivity = {
|
|
|
38
38
|
declare interface AgentConfig extends LLMConfig {
|
|
39
39
|
language?: SupportedLanguage;
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
41
|
+
* 每个任务中 Agent 可以执行的最大步数。
|
|
42
42
|
* @default 40
|
|
43
43
|
*/
|
|
44
44
|
maxSteps?: number;
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
46
|
+
* 用于扩展 PageAgent 能力的自定义工具
|
|
47
47
|
* @experimental
|
|
48
|
-
* @note
|
|
48
|
+
* @note 你也可以通过使用相同名称来覆盖或移除内部工具。
|
|
49
49
|
* @see AgentTool
|
|
50
50
|
*
|
|
51
51
|
* @example
|
|
52
|
-
* //
|
|
52
|
+
* // 覆盖内部工具
|
|
53
53
|
* import { z } from 'zod/v4'
|
|
54
54
|
* import { tool } from '@coolkiller007/my-page-agent'
|
|
55
55
|
* const customTools = {
|
|
@@ -67,112 +67,112 @@ declare interface AgentConfig extends LLMConfig {
|
|
|
67
67
|
* }
|
|
68
68
|
*
|
|
69
69
|
* @example
|
|
70
|
-
* //
|
|
70
|
+
* // 移除内部工具
|
|
71
71
|
* const customTools = {
|
|
72
|
-
* ask_user: null //
|
|
72
|
+
* ask_user: null // 永远不要向用户提问
|
|
73
73
|
* }
|
|
74
74
|
*/
|
|
75
75
|
customTools?: Record<string, AgentTool | null>;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
77
|
+
* 用于引导 Agent 行为的指令
|
|
78
78
|
*/
|
|
79
79
|
instructions?: {
|
|
80
80
|
/**
|
|
81
|
-
*
|
|
81
|
+
* 全局系统级指令,应用于所有任务
|
|
82
82
|
*/
|
|
83
83
|
system?: string;
|
|
84
84
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
* @param url -
|
|
88
|
-
* @returns
|
|
85
|
+
* 动态的页面级指令回调
|
|
86
|
+
* 每一步前调用,以获取当前页面的指令
|
|
87
|
+
* @param url - 当前页面 URL(window.location.href)
|
|
88
|
+
* @returns 指令字符串,或返回 undefined/null 以跳过
|
|
89
89
|
*/
|
|
90
90
|
getPageInstructions?: (url: string) => string | undefined | null;
|
|
91
91
|
};
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
94
|
-
* @experimental API
|
|
93
|
+
* 任务执行的生命周期钩子。
|
|
94
|
+
* @experimental API 在未来版本中可能变化。
|
|
95
95
|
*
|
|
96
|
-
*
|
|
96
|
+
* 所有钩子都接收 Agent 实例作为第一个参数。
|
|
97
97
|
*/
|
|
98
98
|
/**
|
|
99
|
-
*
|
|
99
|
+
* 每一步执行前调用。
|
|
100
100
|
* @experimental
|
|
101
|
-
* @param agent -
|
|
102
|
-
* @param stepCount -
|
|
101
|
+
* @param agent - AgentRuntime 实例
|
|
102
|
+
* @param stepCount - 当前步骤序号(从 0 开始)
|
|
103
103
|
*/
|
|
104
104
|
onBeforeStep?: (agent: AgentRuntime, stepCount: number) => Promise<void> | void;
|
|
105
105
|
/**
|
|
106
|
-
*
|
|
106
|
+
* 每一步执行后调用。
|
|
107
107
|
* @experimental
|
|
108
|
-
* @param agent -
|
|
109
|
-
* @param history -
|
|
108
|
+
* @param agent - AgentRuntime 实例
|
|
109
|
+
* @param history - 当前的事件历史
|
|
110
110
|
*/
|
|
111
111
|
onAfterStep?: (agent: AgentRuntime, history: HistoricalEvent[]) => Promise<void> | void;
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
113
|
+
* 任务执行开始前调用。
|
|
114
114
|
* @experimental
|
|
115
|
-
* @param agent -
|
|
115
|
+
* @param agent - AgentRuntime 实例
|
|
116
116
|
*/
|
|
117
117
|
onBeforeTask?: (agent: AgentRuntime) => Promise<void> | void;
|
|
118
118
|
/**
|
|
119
|
-
*
|
|
119
|
+
* 任务执行完成(成功或失败)后调用。
|
|
120
120
|
* @experimental
|
|
121
|
-
* @param agent -
|
|
122
|
-
* @param result -
|
|
121
|
+
* @param agent - AgentRuntime 实例
|
|
122
|
+
* @param result - 执行结果
|
|
123
123
|
*/
|
|
124
124
|
onAfterTask?: (agent: AgentRuntime, result: ExecutionResult) => Promise<void> | void;
|
|
125
125
|
/**
|
|
126
|
-
*
|
|
126
|
+
* Agent 被销毁时调用。
|
|
127
127
|
* @experimental
|
|
128
|
-
* @note
|
|
129
|
-
* @param agent -
|
|
130
|
-
* @param reason -
|
|
128
|
+
* @note 若该钩子是异步的,它可能会阻塞销毁流程。
|
|
129
|
+
* @param agent - AgentRuntime 实例
|
|
130
|
+
* @param reason - 可选的销毁原因
|
|
131
131
|
*/
|
|
132
132
|
onDispose?: (agent: AgentRuntime, reason?: string) => void;
|
|
133
133
|
/**
|
|
134
134
|
* @experimental
|
|
135
|
-
*
|
|
136
|
-
* @note
|
|
137
|
-
* @note
|
|
135
|
+
* 启用实验性的脚本执行工具,允许在页面上执行生成的 JavaScript 代码。
|
|
136
|
+
* @note 可能产生不可预测的副作用。
|
|
137
|
+
* @note 可能绕过某些安全防护和数据遮蔽机制。
|
|
138
138
|
*/
|
|
139
139
|
experimentalScriptExecutionTool?: boolean;
|
|
140
140
|
/**
|
|
141
141
|
* @experimental
|
|
142
|
-
*
|
|
143
|
-
*
|
|
142
|
+
* 从当前站点源拉取 /llms.txt 并作为上下文包含进来。
|
|
143
|
+
* 每个源每个任务只拉取一次。
|
|
144
144
|
* @default false
|
|
145
145
|
*/
|
|
146
146
|
experimentalLlmsTxt?: boolean;
|
|
147
147
|
/**
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
148
|
+
* 在发送给 LLM 之前转换页面内容。
|
|
149
|
+
* 在 DOM 提取和简化之后、调用 LLM 之前调用。
|
|
150
|
+
* 使用场景:检查提取结果、修改页面信息、遮蔽敏感数据。
|
|
151
151
|
*
|
|
152
|
-
* @param content -
|
|
153
|
-
* @returns
|
|
152
|
+
* @param content - 将被发送给 LLM 的简化页面内容
|
|
153
|
+
* @returns 转换后的内容
|
|
154
154
|
*
|
|
155
155
|
* @example
|
|
156
|
-
* //
|
|
156
|
+
* // 遮蔽手机号
|
|
157
157
|
* transformPageContent: async (content) => {
|
|
158
158
|
* return content.replace(/1[3-9]\d{9}/g, '***********')
|
|
159
159
|
* }
|
|
160
160
|
*/
|
|
161
161
|
transformPageContent?: (content: string) => Promise<string> | string;
|
|
162
162
|
/**
|
|
163
|
-
*
|
|
164
|
-
* @experimental
|
|
163
|
+
* 完全覆盖默认的系统提示词。
|
|
164
|
+
* @experimental 谨慎使用 - 错误的提示词可能破坏 Agent 行为。
|
|
165
165
|
*/
|
|
166
166
|
customSystemPrompt?: string;
|
|
167
167
|
/**
|
|
168
|
-
*
|
|
168
|
+
* 步骤之间的延迟(秒)。
|
|
169
169
|
* @default 0.4
|
|
170
170
|
*/
|
|
171
171
|
stepDelay?: number;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
/**
|
|
175
|
-
*
|
|
175
|
+
* 错误事件 - 来自 LLM 或执行的致命错误
|
|
176
176
|
*/
|
|
177
177
|
declare interface AgentErrorEvent {
|
|
178
178
|
type: 'error';
|
|
@@ -181,12 +181,12 @@ declare interface AgentErrorEvent {
|
|
|
181
181
|
}
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
|
-
* Agent
|
|
184
|
+
* Agent 反思状态 - 行动前先反思的模型
|
|
185
185
|
*
|
|
186
|
-
*
|
|
187
|
-
* - evaluation_previous_goal:
|
|
188
|
-
* - memory:
|
|
189
|
-
* - next_goal:
|
|
186
|
+
* 每次工具调用都必须先反思:
|
|
187
|
+
* - evaluation_previous_goal: 上一个动作在多大程度上实现了其目标?
|
|
188
|
+
* - memory: 需要在未来步骤中记住的关键信息
|
|
189
|
+
* - next_goal: 下一个动作应该完成什么?
|
|
190
190
|
*/
|
|
191
191
|
declare interface AgentReflection {
|
|
192
192
|
evaluation_previous_goal: string;
|
|
@@ -195,75 +195,82 @@ declare interface AgentReflection {
|
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
/**
|
|
198
|
-
* AI
|
|
198
|
+
* 用于浏览器自动化的 AI 智能体。
|
|
199
199
|
*
|
|
200
200
|
* @remarks
|
|
201
|
-
* ##
|
|
202
|
-
* -
|
|
203
|
-
* -
|
|
204
|
-
* -
|
|
205
|
-
* -
|
|
206
|
-
* -
|
|
207
|
-
* -
|
|
208
|
-
* -
|
|
201
|
+
* ## 反应式 Agent 循环
|
|
202
|
+
* - 步骤
|
|
203
|
+
* - 观察(收集当前环境与上下文信息)
|
|
204
|
+
* - 思考(调用 LLM)
|
|
205
|
+
* - 反思(评估历史、生成记忆、短期规划)
|
|
206
|
+
* - 行动(给出接近下一个目标的动作)
|
|
207
|
+
* - 执行(执行动作)
|
|
208
|
+
* - 循环
|
|
209
209
|
*
|
|
210
|
-
* ##
|
|
211
|
-
* - `statuschange` - Agent
|
|
212
|
-
* - `historychange` -
|
|
213
|
-
* - `activity` -
|
|
214
|
-
* - `dispose` - Agent
|
|
210
|
+
* ## 事件系统
|
|
211
|
+
* - `statuschange` - Agent 状态变更(idle → running → completed/error/stopped)
|
|
212
|
+
* - `historychange` - 历史事件更新(持久化,属于 Agent 记忆的一部分)
|
|
213
|
+
* - `activity` - 实时活动反馈(瞬态,仅用于 UI)
|
|
214
|
+
* - `dispose` - 触发 Agent 清理
|
|
215
215
|
*
|
|
216
|
-
* ##
|
|
217
|
-
* 1.
|
|
218
|
-
* -
|
|
219
|
-
* -
|
|
220
|
-
* -
|
|
216
|
+
* ## 信息流
|
|
217
|
+
* 1. **历史事件**(`history` 数组)
|
|
218
|
+
* - 构成 Agent 记忆的持久事件流
|
|
219
|
+
* - 跨步骤包含在 LLM 上下文中
|
|
220
|
+
* - 类型:步骤、观察、用户接管、LLM 错误
|
|
221
221
|
*
|
|
222
|
-
* 2.
|
|
223
|
-
* -
|
|
224
|
-
* -
|
|
225
|
-
* -
|
|
222
|
+
* 2. **活动事件**(通过 `activity` 事件)
|
|
223
|
+
* - 任务执行期间的瞬时 UI 反馈
|
|
224
|
+
* - 不包含在 LLM 上下文中
|
|
225
|
+
* - 类型:thinking、executing、executed、retrying、error
|
|
226
226
|
*/
|
|
227
227
|
declare class AgentRuntime extends EventTarget {
|
|
228
228
|
#private;
|
|
229
|
+
/** Agent 唯一 ID */
|
|
229
230
|
readonly id: string;
|
|
231
|
+
/** 合并后的 Agent 配置(含最大步数上限) */
|
|
230
232
|
readonly config: AgentRuntimeConfig & {
|
|
231
233
|
maxSteps: number;
|
|
232
234
|
};
|
|
235
|
+
/** 内部工具集合(按工具名索引) */
|
|
233
236
|
readonly tools: typeof tools;
|
|
234
|
-
/**
|
|
237
|
+
/** 用于 DOM 操作的 BrowserController */
|
|
235
238
|
readonly browserController: BrowserController;
|
|
239
|
+
/** 当前任务描述 */
|
|
236
240
|
task: string;
|
|
241
|
+
/** 当前任务 ID */
|
|
237
242
|
taskId: string;
|
|
238
|
-
/**
|
|
243
|
+
/** 历史事件 */
|
|
239
244
|
history: HistoricalEvent[];
|
|
240
|
-
/**
|
|
245
|
+
/** 该 Agent 是否已被销毁 */
|
|
241
246
|
disposed: boolean;
|
|
242
247
|
/**
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
248
|
+
* 当 Agent 需要向用户提问时调用。
|
|
249
|
+
* 若未设置,`ask_user` 工具将被禁用。
|
|
250
|
+
* 实现应在 `signal` 中止时 reject 该 Promise。
|
|
246
251
|
* @example onAskUser: (q) => window.prompt(q) || ''
|
|
247
252
|
*/
|
|
248
253
|
onAskUser?: (question: string, options?: {
|
|
249
254
|
signal: AbortSignal;
|
|
250
255
|
}) => Promise<string>;
|
|
256
|
+
/** 构造函数:初始化配置、LLM、工具集,并注册重试事件监听与注入自定义工具 */
|
|
251
257
|
constructor(config: AgentRuntimeConfig);
|
|
252
|
-
/**
|
|
258
|
+
/** 获取当前 Agent 状态 */
|
|
253
259
|
get status(): AgentStatus;
|
|
254
|
-
/**
|
|
260
|
+
/** 最近一次运行的结果,首次运行完成前为 `null`。 */
|
|
255
261
|
get lastResult(): ExecutionResult | null;
|
|
256
262
|
/* Excluded from this release type: pushObservation */
|
|
257
263
|
/**
|
|
258
|
-
*
|
|
259
|
-
* @note
|
|
264
|
+
* 停止当前任务,并等待运行完全结束(包括生命周期钩子)。
|
|
265
|
+
* @note 切勿在生命周期钩子中 await .stop()。
|
|
260
266
|
*/
|
|
261
267
|
stop(): Promise<void>;
|
|
262
268
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
269
|
+
* 外部错误(前置检查/配置/钩子)会直接抛出;
|
|
270
|
+
* Agent 内部错误会被捕获并加入历史,同时返回失败结果
|
|
265
271
|
*/
|
|
266
272
|
execute(task: string): Promise<ExecutionResult>;
|
|
273
|
+
/** 销毁 Agent:中止任务、释放浏览器控制器,并触发 dispose 事件(一次性) */
|
|
267
274
|
dispose(): void;
|
|
268
275
|
}
|
|
269
276
|
|
|
@@ -272,12 +279,12 @@ declare type AgentRuntimeConfig = AgentConfig & {
|
|
|
272
279
|
};
|
|
273
280
|
|
|
274
281
|
/**
|
|
275
|
-
* Agent
|
|
282
|
+
* Agent 生命周期状态。
|
|
276
283
|
*/
|
|
277
284
|
export declare type AgentStatus = 'idle' | 'running' | 'completed' | 'error' | 'stopped';
|
|
278
285
|
|
|
279
286
|
/**
|
|
280
|
-
*
|
|
287
|
+
* 带反思和动作的单个 Agent 步骤
|
|
281
288
|
*/
|
|
282
289
|
declare interface AgentStepEvent {
|
|
283
290
|
type: 'step';
|
|
@@ -295,14 +302,14 @@ declare interface AgentStepEvent {
|
|
|
295
302
|
cachedTokens?: number;
|
|
296
303
|
reasoningTokens?: number;
|
|
297
304
|
};
|
|
298
|
-
/**
|
|
305
|
+
/** 原始 LLM 响应(用于调试) */
|
|
299
306
|
rawResponse?: unknown;
|
|
300
|
-
/**
|
|
307
|
+
/** 原始 LLM 请求(用于调试) */
|
|
301
308
|
rawRequest?: unknown;
|
|
302
309
|
}
|
|
303
310
|
|
|
304
311
|
/**
|
|
305
|
-
*
|
|
312
|
+
* 内部工具定义,可访问 PageAgent 的 `this` 上下文
|
|
306
313
|
*/
|
|
307
314
|
declare interface AgentTool<TParams = any> {
|
|
308
315
|
description: string;
|
|
@@ -311,84 +318,86 @@ declare interface AgentTool<TParams = any> {
|
|
|
311
318
|
}
|
|
312
319
|
|
|
313
320
|
/**
|
|
314
|
-
* BrowserController
|
|
315
|
-
*
|
|
321
|
+
* BrowserController 管理 DOM 状态和元素交互。
|
|
322
|
+
* 它为所有 DOM 操作提供异步方法,并保持状态隔离。
|
|
316
323
|
*
|
|
317
324
|
* @lifecycle
|
|
318
|
-
* - beforeUpdate:
|
|
319
|
-
* - afterUpdate:
|
|
325
|
+
* - beforeUpdate: 在 DOM 树更新之前触发。
|
|
326
|
+
* - afterUpdate: 在 DOM 树更新之后触发。
|
|
320
327
|
*/
|
|
321
328
|
declare class BrowserController extends EventTarget {
|
|
322
329
|
private config;
|
|
323
|
-
/**
|
|
330
|
+
/** 对应 browser-use 中的 eval_page */
|
|
324
331
|
private flatTree;
|
|
325
332
|
/**
|
|
326
|
-
*
|
|
327
|
-
*
|
|
333
|
+
* 所有已索引的高亮交互元素
|
|
334
|
+
* 对应 browser-use 中的 DOMState.selector_map
|
|
328
335
|
*/
|
|
329
336
|
private selectorMap;
|
|
330
|
-
/**
|
|
337
|
+
/** 索引 -> 元素文本描述映射 */
|
|
331
338
|
private elementTextMap;
|
|
332
339
|
/**
|
|
333
|
-
*
|
|
334
|
-
*
|
|
340
|
+
* 供 LLM 消费的简化 HTML。
|
|
341
|
+
* 对应 browser-use 中的 clickable_elements_to_string
|
|
335
342
|
*/
|
|
336
343
|
private simplifiedHTML;
|
|
337
|
-
/**
|
|
344
|
+
/** 树最后一次更新的时间 */
|
|
338
345
|
private lastTimeUpdate;
|
|
339
|
-
/**
|
|
346
|
+
/** 树是否至少被索引过一次 */
|
|
340
347
|
private isIndexed;
|
|
341
|
-
/**
|
|
348
|
+
/** 用于在自动化期间阻止用户交互的视觉遮罩层 */
|
|
342
349
|
private mask;
|
|
350
|
+
/** mask 异步初始化完成的 Promise,用于在调用前等待初始化完成 */
|
|
343
351
|
private maskReady;
|
|
352
|
+
/** 是否已释放资源 */
|
|
344
353
|
private disposed;
|
|
345
354
|
constructor(config?: BrowserControllerConfig);
|
|
346
355
|
/**
|
|
347
|
-
*
|
|
356
|
+
* 异步初始化遮罩层(动态导入以避免在 Node 中加载 CSS)
|
|
348
357
|
*/
|
|
349
358
|
initMask(): void;
|
|
350
359
|
/**
|
|
351
|
-
*
|
|
360
|
+
* 获取当前页面 URL
|
|
352
361
|
*/
|
|
353
362
|
getCurrentUrl(): Promise<string>;
|
|
354
363
|
/**
|
|
355
|
-
*
|
|
364
|
+
* 获取树最后一次更新的时间戳
|
|
356
365
|
*/
|
|
357
366
|
getLastUpdateTime(): Promise<number>;
|
|
358
367
|
/**
|
|
359
|
-
*
|
|
360
|
-
*
|
|
368
|
+
* 获取供 LLM 消费的结构化浏览器状态。
|
|
369
|
+
* 自动调用 updateTree() 刷新 DOM 状态。
|
|
361
370
|
*/
|
|
362
371
|
getBrowserState(): Promise<BrowserState>;
|
|
363
372
|
/**
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
373
|
+
* 更新 DOM 树,返回供 LLM 使用的简化 HTML。
|
|
374
|
+
* 这是刷新页面状态的主要方法。
|
|
375
|
+
* 若已启用遮罩,DOM 提取期间会自动绕过遮罩。
|
|
367
376
|
*/
|
|
368
377
|
updateTree(): Promise<string>;
|
|
369
378
|
/**
|
|
370
|
-
*
|
|
379
|
+
* 清理所有元素高亮
|
|
371
380
|
*/
|
|
372
381
|
cleanUpHighlights(): Promise<void>;
|
|
373
382
|
/**
|
|
374
|
-
*
|
|
375
|
-
*
|
|
383
|
+
* 在任何基于索引的操作之前确保树已被索引。
|
|
384
|
+
* 若尚未调用 updateTree() 则抛出异常。
|
|
376
385
|
*/
|
|
377
386
|
private assertIndexed;
|
|
378
387
|
/**
|
|
379
|
-
*
|
|
388
|
+
* 根据索引点击元素
|
|
380
389
|
*/
|
|
381
390
|
clickElement(index: number): Promise<ActionResult>;
|
|
382
391
|
/**
|
|
383
|
-
*
|
|
392
|
+
* 根据索引向元素输入文本
|
|
384
393
|
*/
|
|
385
394
|
inputText(index: number, text: string): Promise<ActionResult>;
|
|
386
395
|
/**
|
|
387
|
-
*
|
|
396
|
+
* 根据索引和选项文本在下拉框中选中选项
|
|
388
397
|
*/
|
|
389
398
|
selectOption(index: number, optionText: string): Promise<ActionResult>;
|
|
390
399
|
/**
|
|
391
|
-
*
|
|
400
|
+
* 垂直滚动
|
|
392
401
|
*/
|
|
393
402
|
scroll(options: {
|
|
394
403
|
down: boolean;
|
|
@@ -397,7 +406,7 @@ declare class BrowserController extends EventTarget {
|
|
|
397
406
|
index?: number;
|
|
398
407
|
}): Promise<ActionResult>;
|
|
399
408
|
/**
|
|
400
|
-
*
|
|
409
|
+
* 水平滚动
|
|
401
410
|
*/
|
|
402
411
|
scrollHorizontally(options: {
|
|
403
412
|
right: boolean;
|
|
@@ -405,51 +414,55 @@ declare class BrowserController extends EventTarget {
|
|
|
405
414
|
index?: number;
|
|
406
415
|
}): Promise<ActionResult>;
|
|
407
416
|
/**
|
|
408
|
-
*
|
|
409
|
-
*
|
|
410
|
-
* can abort promptly when the task is stopped.
|
|
417
|
+
* 在页面上执行任意 JavaScript。
|
|
418
|
+
* 可选的 `signal` 会暴露到脚本作用域中,使协作代码能在任务被中止时及时退出。
|
|
411
419
|
*/
|
|
412
420
|
executeJavascript(script: string, signal?: AbortSignal): Promise<ActionResult>;
|
|
413
421
|
/**
|
|
414
|
-
*
|
|
415
|
-
*
|
|
422
|
+
* 显示视觉遮罩层。
|
|
423
|
+
* 仅在遮罩初始化完成后可用。
|
|
416
424
|
*/
|
|
417
425
|
showMask(): Promise<void>;
|
|
418
426
|
/**
|
|
419
|
-
*
|
|
420
|
-
*
|
|
427
|
+
* 隐藏视觉遮罩层。
|
|
428
|
+
* 仅在遮罩初始化完成后可用。
|
|
421
429
|
*/
|
|
422
430
|
hideMask(): Promise<void>;
|
|
423
431
|
/**
|
|
424
|
-
*
|
|
432
|
+
* 释放资源并清理
|
|
425
433
|
*/
|
|
426
434
|
dispose(): void;
|
|
427
435
|
}
|
|
428
436
|
|
|
429
437
|
/**
|
|
430
|
-
*
|
|
438
|
+
* BrowserController 的配置
|
|
431
439
|
*/
|
|
432
440
|
declare interface BrowserControllerConfig extends dom.DomConfig {
|
|
433
|
-
/**
|
|
441
|
+
/** 操作期间启用视觉遮罩层(默认:false) */
|
|
434
442
|
enableMask?: boolean;
|
|
435
443
|
}
|
|
436
444
|
|
|
437
445
|
/**
|
|
438
|
-
*
|
|
446
|
+
* 供 LLM 使用的结构化浏览器状态
|
|
439
447
|
*/
|
|
440
448
|
declare interface BrowserState {
|
|
441
449
|
url: string;
|
|
442
450
|
title: string;
|
|
443
|
-
/**
|
|
451
|
+
/** 页面信息 + 滚动位置提示(例如 "Page info: 1920x1080px...\n[Start of page]") */
|
|
444
452
|
header: string;
|
|
445
|
-
/**
|
|
453
|
+
/** 可交互元素的简化 HTML */
|
|
446
454
|
content: string;
|
|
447
|
-
/**
|
|
455
|
+
/** 页面底部提示(例如 "... 300 pixels below ..." 或 "[End of page]") */
|
|
448
456
|
footer: string;
|
|
449
457
|
}
|
|
450
458
|
|
|
451
459
|
declare function cleanUpHighlights(): void;
|
|
452
460
|
|
|
461
|
+
/**
|
|
462
|
+
* 创建智能体实例的快捷函数,内部直接实例化 MyPageAgent。
|
|
463
|
+
* @param config 智能体的配置对象
|
|
464
|
+
* @returns 创建好的 MyPageAgent 实例
|
|
465
|
+
*/
|
|
453
466
|
export declare function createAgent(config: MyPageAgentConfig): MyPageAgent;
|
|
454
467
|
|
|
455
468
|
declare namespace dom {
|
|
@@ -474,8 +487,8 @@ declare interface DomConfig {
|
|
|
474
487
|
highlightOpacity?: number;
|
|
475
488
|
highlightLabelOpacity?: number;
|
|
476
489
|
/**
|
|
477
|
-
*
|
|
478
|
-
* @note
|
|
490
|
+
* 即使不可交互,也保留脱水输出中的语义化地标标签
|
|
491
|
+
* @note 结合页面滚动时可能让 LLM 感到困惑,请谨慎使用
|
|
479
492
|
**/
|
|
480
493
|
keepSemanticTags?: boolean;
|
|
481
494
|
}
|
|
@@ -497,6 +510,7 @@ declare interface ElementDomNode {
|
|
|
497
510
|
[key: string]: unknown;
|
|
498
511
|
}
|
|
499
512
|
|
|
513
|
+
/** 任务执行结果 */
|
|
500
514
|
export declare interface ExecutionResult {
|
|
501
515
|
success: boolean;
|
|
502
516
|
data: string;
|
|
@@ -542,7 +556,7 @@ declare function getFlatTree(config: DomConfig): FlatDomTree;
|
|
|
542
556
|
declare function getSelectorMap(flatTree: FlatDomTree): Map<number, InteractiveElementDomNode>;
|
|
543
557
|
|
|
544
558
|
/**
|
|
545
|
-
*
|
|
559
|
+
* 所有历史事件的联合类型
|
|
546
560
|
*/
|
|
547
561
|
export declare type HistoricalEvent = AgentStepEvent | ObservationEvent | UserTakeoverEvent | RetryEvent | AgentErrorEvent;
|
|
548
562
|
|
|
@@ -564,38 +578,39 @@ declare interface InteractiveElementDomNode {
|
|
|
564
578
|
}
|
|
565
579
|
|
|
566
580
|
/**
|
|
567
|
-
* LLM
|
|
581
|
+
* LLM 配置
|
|
568
582
|
*/
|
|
569
583
|
declare interface LLMConfig {
|
|
570
584
|
baseURL: string;
|
|
571
585
|
model: string;
|
|
572
586
|
apiKey?: string;
|
|
573
587
|
/**
|
|
574
|
-
* @deprecated
|
|
575
|
-
*
|
|
588
|
+
* @deprecated 已不再是标准参数,许多模型会直接拒绝该参数。
|
|
589
|
+
* 请使用 `transformRequestBody` 仅为已验证支持的模型设置该参数。
|
|
576
590
|
*/
|
|
577
591
|
temperature?: number;
|
|
578
592
|
maxRetries?: number;
|
|
579
593
|
/**
|
|
580
|
-
*
|
|
581
|
-
*
|
|
594
|
+
* 在发送给服务商之前转换最终的请求体。
|
|
595
|
+
* 用于实现特定服务商的请求定制,例如缓存提示或自定义标记。
|
|
582
596
|
*
|
|
583
|
-
*
|
|
597
|
+
* 返回一个新对象,或就地修改输入对象并返回 undefined。
|
|
584
598
|
*/
|
|
585
599
|
transformRequestBody?: (requestBody: Record<string, unknown>) => Record<string, unknown> | undefined;
|
|
586
600
|
/**
|
|
587
|
-
*
|
|
588
|
-
* @note
|
|
601
|
+
* 从请求中移除 tool_choice 字段。
|
|
602
|
+
* @note 修复部分 LLM 报出的 "Invalid tool_choice type: 'object'" 错误。
|
|
589
603
|
*/
|
|
590
604
|
disableNamedToolChoice?: boolean;
|
|
591
605
|
/**
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
606
|
+
* 用于 LLM API 请求的自定义 fetch 函数。
|
|
607
|
+
* 可用于自定义请求头、凭据、代理等。
|
|
608
|
+
* 返回的响应应符合 OpenAI API 格式。
|
|
595
609
|
*/
|
|
596
610
|
customFetch?: typeof globalThis.fetch;
|
|
597
611
|
}
|
|
598
612
|
|
|
613
|
+
/** 语言包集合:键为语言代码,值为对应翻译表 */
|
|
599
614
|
declare const locales: {
|
|
600
615
|
readonly 'en-US': {
|
|
601
616
|
readonly ui: {
|
|
@@ -687,15 +702,21 @@ declare const locales: {
|
|
|
687
702
|
};
|
|
688
703
|
};
|
|
689
704
|
|
|
705
|
+
/**
|
|
706
|
+
* 完整页面智能体:组合了 AgentRuntime、BrowserController 与 UI
|
|
707
|
+
*/
|
|
690
708
|
export declare class MyPageAgent extends AgentRuntime {
|
|
709
|
+
/** UI 实例(用于用户交互与反馈) */
|
|
691
710
|
readonly ui: UI;
|
|
711
|
+
/** 构造函数:创建浏览器控制器、父类运行时与 UI 实例 */
|
|
692
712
|
constructor(config: MyPageAgentConfig);
|
|
693
713
|
}
|
|
694
714
|
|
|
715
|
+
/** MyPageAgent 的配置:Agent 配置 + 浏览器控制器配置 + UI 配置(语言除外) */
|
|
695
716
|
export declare type MyPageAgentConfig = Omit<AgentConfig, 'experimentalScriptExecutionTool'> & BrowserControllerConfig & Omit<UIConfig, 'language'>;
|
|
696
717
|
|
|
697
718
|
/**
|
|
698
|
-
*
|
|
719
|
+
* 持久观察事件(保留在记忆中)
|
|
699
720
|
*/
|
|
700
721
|
declare interface ObservationEvent {
|
|
701
722
|
type: 'observation';
|
|
@@ -705,7 +726,7 @@ declare interface ObservationEvent {
|
|
|
705
726
|
declare function resolveViewportExpansion(viewportExpansion?: number): number;
|
|
706
727
|
|
|
707
728
|
/**
|
|
708
|
-
*
|
|
729
|
+
* 重试事件 - LLM 调用正在重试
|
|
709
730
|
*/
|
|
710
731
|
declare interface RetryEvent {
|
|
711
732
|
type: 'retry';
|
|
@@ -714,9 +735,10 @@ declare interface RetryEvent {
|
|
|
714
735
|
maxAttempts: number;
|
|
715
736
|
}
|
|
716
737
|
|
|
717
|
-
/**
|
|
738
|
+
/** 支持的 UI 语言 */
|
|
718
739
|
declare type SupportedLanguage = 'en-US' | 'zh-CN';
|
|
719
740
|
|
|
741
|
+
/** 受支持的语言代码类型 */
|
|
720
742
|
declare type SupportedLanguage_2 = keyof typeof locales;
|
|
721
743
|
|
|
722
744
|
declare interface TextDomNode {
|
|
@@ -726,19 +748,20 @@ declare interface TextDomNode {
|
|
|
726
748
|
[key: string]: unknown;
|
|
727
749
|
}
|
|
728
750
|
|
|
751
|
+
/** 创建一个工具定义(直接透传 options) */
|
|
729
752
|
export declare function tool<TParams>(options: AgentTool<TParams>): AgentTool<TParams>;
|
|
730
753
|
|
|
731
754
|
/**
|
|
732
|
-
*
|
|
733
|
-
*
|
|
755
|
+
* 每次工具调用都会传入的上下文。
|
|
756
|
+
* 工具必须遵守 `signal` 以支持协作式取消。
|
|
734
757
|
*/
|
|
735
758
|
declare interface ToolContext {
|
|
736
759
|
signal: AbortSignal;
|
|
737
760
|
}
|
|
738
761
|
|
|
739
762
|
/**
|
|
740
|
-
*
|
|
741
|
-
*
|
|
763
|
+
* PageAgent 的内部工具。
|
|
764
|
+
* 注:使用 any 以允许每个工具拥有不同的参数类型
|
|
742
765
|
*/
|
|
743
766
|
declare const tools: Map<string, AgentTool<any>>;
|
|
744
767
|
|
|
@@ -761,110 +784,116 @@ declare interface TreeNode {
|
|
|
761
784
|
}
|
|
762
785
|
|
|
763
786
|
/**
|
|
764
|
-
* Agent
|
|
787
|
+
* Agent 控制 UI
|
|
765
788
|
*
|
|
766
|
-
*
|
|
767
|
-
* -
|
|
768
|
-
* -
|
|
789
|
+
* 架构:
|
|
790
|
+
* - 历史列表:直接从 agent.history 渲染(历史事件)
|
|
791
|
+
* - 头部栏:展示活动事件(瞬时状态)和 agent 状态
|
|
769
792
|
*
|
|
770
|
-
*
|
|
771
|
-
*
|
|
793
|
+
* 这种分离保证了数据一致性 - 历史是已完成内容的唯一数据源,
|
|
794
|
+
* 而活动状态展示的是当前正在发生的事情。
|
|
772
795
|
*/
|
|
773
796
|
declare class UI {
|
|
774
797
|
#private;
|
|
798
|
+
/** 获取 UI 的根容器元素 */
|
|
775
799
|
get wrapper(): HTMLElement;
|
|
776
800
|
/**
|
|
777
|
-
*
|
|
778
|
-
* @param agent -
|
|
779
|
-
* @param config -
|
|
801
|
+
* 创建绑定到某个 agent 的 UI
|
|
802
|
+
* @param agent - 实现 UIAdapter 的 Agent 实例
|
|
803
|
+
* @param config - 可选的 UI 配置
|
|
780
804
|
*/
|
|
781
805
|
constructor(agent: UIAdapter, config?: UIConfig);
|
|
806
|
+
/** 显示 UI(淡入 + 平移效果) */
|
|
782
807
|
show(): void;
|
|
808
|
+
/** 隐藏 UI(淡出 + 下移效果) */
|
|
783
809
|
hide(): void;
|
|
810
|
+
/** 重置 UI 状态(拒绝挂起问题、清空状态文本、收起历史并显示输入框) */
|
|
784
811
|
reset(): void;
|
|
812
|
+
/** 展开历史弹窗(公开方法) */
|
|
785
813
|
expand(): void;
|
|
814
|
+
/** 收起历史弹窗(公开方法) */
|
|
786
815
|
collapse(): void;
|
|
787
816
|
/**
|
|
788
|
-
*
|
|
817
|
+
* 销毁 UI 并清理事件监听器
|
|
789
818
|
*/
|
|
790
819
|
dispose(): void;
|
|
791
820
|
}
|
|
792
821
|
|
|
793
822
|
/**
|
|
794
|
-
*
|
|
795
|
-
* UI
|
|
796
|
-
*
|
|
823
|
+
* UI 对 agent 的最简接口要求。
|
|
824
|
+
* UI 不直接依赖 PageAgent - 只需实现该接口即可。
|
|
825
|
+
* 这样实现了解耦,任何 agent 实现都能与 UI 配合使用。
|
|
797
826
|
*
|
|
798
|
-
*
|
|
799
|
-
* - 'statuschange'
|
|
800
|
-
* - 'historychange'
|
|
801
|
-
* - 'activity'
|
|
802
|
-
* - 'dispose'
|
|
827
|
+
* 事件:
|
|
828
|
+
* - 'statuschange':agent 状态发生变化
|
|
829
|
+
* - 'historychange':历史事件更新(已持久化)
|
|
830
|
+
* - 'activity':供 UI 即时反馈的瞬时活动(思考/执行等)
|
|
831
|
+
* - 'dispose':agent 正在被销毁
|
|
803
832
|
*/
|
|
804
833
|
declare interface UIAdapter extends EventTarget {
|
|
805
|
-
/**
|
|
834
|
+
/** 当前 agent 状态 */
|
|
806
835
|
readonly status: 'idle' | 'running' | 'completed' | 'error' | 'stopped';
|
|
807
|
-
/**
|
|
836
|
+
/** 最近一次运行的结果,首次运行完成前为 `null` */
|
|
808
837
|
readonly lastResult: {
|
|
809
838
|
success: boolean;
|
|
810
839
|
} | null;
|
|
811
|
-
/**
|
|
840
|
+
/** agent 事件历史 */
|
|
812
841
|
readonly history: readonly {
|
|
813
842
|
type: 'step' | 'observation' | 'user_takeover' | 'retry' | 'error';
|
|
814
843
|
stepIndex?: number;
|
|
815
|
-
/**
|
|
844
|
+
/** 仅用于 'step' 类型 */
|
|
816
845
|
reflection?: {
|
|
817
846
|
evaluation_previous_goal?: string;
|
|
818
847
|
memory?: string;
|
|
819
848
|
next_goal?: string;
|
|
820
849
|
};
|
|
821
|
-
/**
|
|
850
|
+
/** 仅用于 'step' 类型 */
|
|
822
851
|
action?: {
|
|
823
852
|
name: string;
|
|
824
853
|
input: unknown;
|
|
825
854
|
output: string;
|
|
826
855
|
};
|
|
827
|
-
/**
|
|
856
|
+
/** 仅用于 'observation' 类型 */
|
|
828
857
|
content?: string;
|
|
829
|
-
/**
|
|
858
|
+
/** 仅用于 'retry' 类型 */
|
|
830
859
|
attempt?: number;
|
|
831
860
|
maxAttempts?: number;
|
|
832
|
-
/**
|
|
861
|
+
/** 仅用于 'retry' 和 'error' 类型 */
|
|
833
862
|
message?: string;
|
|
834
863
|
}[];
|
|
835
|
-
/**
|
|
864
|
+
/** 当前正在执行的任务 */
|
|
836
865
|
readonly task: string;
|
|
837
866
|
/**
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
* UI
|
|
841
|
-
*
|
|
867
|
+
* 当 agent 需要向用户提问时调用。
|
|
868
|
+
* 如果未设置,`ask_user` 工具将被禁用。
|
|
869
|
+
* UI 会设置该回调以便通过自身的界面处理用户提问。
|
|
870
|
+
* 可选参数 `signal` 在任务被停止或销毁时触发中止。
|
|
842
871
|
*/
|
|
843
872
|
onAskUser?: (question: string, options?: {
|
|
844
873
|
signal: AbortSignal;
|
|
845
874
|
}) => Promise<string>;
|
|
846
|
-
/**
|
|
875
|
+
/** 执行一个任务 */
|
|
847
876
|
execute(task: string): Promise<unknown>;
|
|
848
|
-
/**
|
|
877
|
+
/** 停止当前任务(agent 仍可复用) */
|
|
849
878
|
stop(): Promise<void>;
|
|
850
|
-
/**
|
|
879
|
+
/** 销毁 agent(终态,不可复用) */
|
|
851
880
|
dispose(): void;
|
|
852
881
|
}
|
|
853
882
|
|
|
854
883
|
/**
|
|
855
|
-
* UI
|
|
884
|
+
* UI 配置
|
|
856
885
|
*/
|
|
857
886
|
declare interface UIConfig {
|
|
858
887
|
language?: SupportedLanguage_2;
|
|
859
888
|
/**
|
|
860
|
-
*
|
|
889
|
+
* 任务完成后是否提示输入下一个任务
|
|
861
890
|
* @default true
|
|
862
891
|
*/
|
|
863
892
|
promptForNextTask?: boolean;
|
|
864
893
|
}
|
|
865
894
|
|
|
866
895
|
/**
|
|
867
|
-
*
|
|
896
|
+
* 用户接管事件
|
|
868
897
|
*/
|
|
869
898
|
declare interface UserTakeoverEvent {
|
|
870
899
|
type: 'user_takeover';
|