@coolkiller007/my-page-agent 0.1.14 → 0.1.15
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/dist/index.d.ts +20 -2
- package/dist/index.js +28 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -42,6 +42,11 @@ declare interface AgentConfig extends LLMConfig {
|
|
|
42
42
|
* @default 40
|
|
43
43
|
*/
|
|
44
44
|
maxSteps?: number;
|
|
45
|
+
/**
|
|
46
|
+
* 拼装 LLM 提示词时,最近保留完整详情的历史步数;更早的步骤会被压缩成一行。
|
|
47
|
+
* @default 15
|
|
48
|
+
*/
|
|
49
|
+
historyWindow?: number;
|
|
45
50
|
/**
|
|
46
51
|
* 用于扩展 PageAgent 能力的自定义工具
|
|
47
52
|
* @experimental
|
|
@@ -231,6 +236,7 @@ declare class AgentRuntime extends EventTarget {
|
|
|
231
236
|
/** 合并后的 Agent 配置(含最大步数上限) */
|
|
232
237
|
readonly config: AgentRuntimeConfig & {
|
|
233
238
|
maxSteps: number;
|
|
239
|
+
historyWindow: number;
|
|
234
240
|
};
|
|
235
241
|
/** 内部工具集合(按工具名索引) */
|
|
236
242
|
readonly tools: typeof tools;
|
|
@@ -558,7 +564,7 @@ declare function getSelectorMap(flatTree: FlatDomTree): Map<number, InteractiveE
|
|
|
558
564
|
/**
|
|
559
565
|
* 所有历史事件的联合类型
|
|
560
566
|
*/
|
|
561
|
-
export declare type HistoricalEvent = AgentStepEvent | ObservationEvent | UserTakeoverEvent | RetryEvent | AgentErrorEvent;
|
|
567
|
+
export declare type HistoricalEvent = AgentStepEvent | ObservationEvent | UserTakeoverEvent | TaskStartEvent | RetryEvent | AgentErrorEvent;
|
|
562
568
|
|
|
563
569
|
declare interface InteractiveElementDomNode {
|
|
564
570
|
tagName: string;
|
|
@@ -741,6 +747,16 @@ declare type SupportedLanguage = 'en-US' | 'zh-CN';
|
|
|
741
747
|
/** 受支持的语言代码类型 */
|
|
742
748
|
declare type SupportedLanguage_2 = keyof typeof locales;
|
|
743
749
|
|
|
750
|
+
/**
|
|
751
|
+
* 任务开始事件 - 标记一个新任务的起点
|
|
752
|
+
* @note history 数组跨任务持久保留(不再在每次 execute 时清空),
|
|
753
|
+
* 该事件用于在历史记录中分隔不同任务,UI 据此渲染各自的任务卡片。
|
|
754
|
+
*/
|
|
755
|
+
declare interface TaskStartEvent {
|
|
756
|
+
type: 'task_start';
|
|
757
|
+
task: string;
|
|
758
|
+
}
|
|
759
|
+
|
|
744
760
|
declare interface TextDomNode {
|
|
745
761
|
type: 'TEXT_NODE';
|
|
746
762
|
text: string;
|
|
@@ -839,7 +855,7 @@ declare interface UIAdapter extends EventTarget {
|
|
|
839
855
|
} | null;
|
|
840
856
|
/** agent 事件历史 */
|
|
841
857
|
readonly history: readonly {
|
|
842
|
-
type: 'step' | 'observation' | 'user_takeover' | 'retry' | 'error';
|
|
858
|
+
type: 'step' | 'observation' | 'user_takeover' | 'task_start' | 'retry' | 'error';
|
|
843
859
|
stepIndex?: number;
|
|
844
860
|
/** 仅用于 'step' 类型 */
|
|
845
861
|
reflection?: {
|
|
@@ -855,6 +871,8 @@ declare interface UIAdapter extends EventTarget {
|
|
|
855
871
|
};
|
|
856
872
|
/** 仅用于 'observation' 类型 */
|
|
857
873
|
content?: string;
|
|
874
|
+
/** 仅用于 'task_start' 类型 */
|
|
875
|
+
task?: string;
|
|
858
876
|
/** 仅用于 'retry' 类型 */
|
|
859
877
|
attempt?: number;
|
|
860
878
|
maxAttempts?: number;
|
package/dist/index.js
CHANGED
|
@@ -2756,6 +2756,7 @@ var UI = class {
|
|
|
2756
2756
|
if (status === "running") {
|
|
2757
2757
|
this.show();
|
|
2758
2758
|
this.#hideInputArea();
|
|
2759
|
+
if (!this.#isExpanded) this.#expand();
|
|
2759
2760
|
}
|
|
2760
2761
|
if (status === "completed" || status === "error" || status === "stopped") {
|
|
2761
2762
|
if (!this.#isExpanded) this.#expand();
|
|
@@ -3149,16 +3150,15 @@ var UI = class {
|
|
|
3149
3150
|
* 直接从 agent.history 渲染历史
|
|
3150
3151
|
*
|
|
3151
3152
|
* 渲染内容:
|
|
3152
|
-
* 1.
|
|
3153
|
+
* 1. 任务卡片(每个 task_start 事件对应一张,history 跨任务持久保留,因此可展示历次任务)
|
|
3153
3154
|
* 2. 反思卡片(评估、记忆、下一步目标)
|
|
3154
3155
|
* 3. 工具执行及输出
|
|
3155
3156
|
* 4. 观察
|
|
3156
3157
|
*/
|
|
3157
3158
|
#renderHistory() {
|
|
3158
3159
|
const items = [];
|
|
3159
|
-
const task = this.#agent.task;
|
|
3160
|
-
if (task) items.push(this.#createTaskCard(task));
|
|
3161
3160
|
const history = this.#agent.history;
|
|
3161
|
+
if (history.length === 0 && this.#agent.task) items.push(this.#createTaskCard(this.#agent.task));
|
|
3162
3162
|
for (const event of history) items.push(...this.#createHistoryCards(event));
|
|
3163
3163
|
this.#historySection.innerHTML = items.join("");
|
|
3164
3164
|
this.#scrollToBottom();
|
|
@@ -3176,7 +3176,9 @@ var UI = class {
|
|
|
3176
3176
|
#createHistoryCards(event) {
|
|
3177
3177
|
const cards = [];
|
|
3178
3178
|
const meta = event.type === "step" && event.stepIndex !== void 0 ? this.#i18n.t("ui.step", { number: (event.stepIndex + 1).toString() }) : void 0;
|
|
3179
|
-
if (event.type === "
|
|
3179
|
+
if (event.type === "task_start") {
|
|
3180
|
+
if (event.task) cards.push(this.#createTaskCard(event.task));
|
|
3181
|
+
} else if (event.type === "step") {
|
|
3180
3182
|
if (event.reflection) {
|
|
3181
3183
|
const lines = createReflectionLines(event.reflection);
|
|
3182
3184
|
if (lines.length > 0) cards.push(createCard({
|
|
@@ -4146,6 +4148,8 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4146
4148
|
#abortController = new AbortController();
|
|
4147
4149
|
/** 本轮任务收集的观察消息(暂存,稍后统一写入历史) */
|
|
4148
4150
|
#observations = [];
|
|
4151
|
+
/** 当前任务在 history 数组中的起始下标(用于跨任务保留历史时,把 prompt/循环检测限定在当前任务范围内) */
|
|
4152
|
+
#taskHistoryStart = 0;
|
|
4149
4153
|
/** 当前一次运行完全结束时 resolve。由 `stop()` 等待。 */
|
|
4150
4154
|
#running = Promise.resolve();
|
|
4151
4155
|
#lastResult = null;
|
|
@@ -4163,7 +4167,8 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4163
4167
|
super();
|
|
4164
4168
|
this.config = {
|
|
4165
4169
|
...config,
|
|
4166
|
-
maxSteps: config.maxSteps ?? 40
|
|
4170
|
+
maxSteps: config.maxSteps ?? 40,
|
|
4171
|
+
historyWindow: config.historyWindow ?? 5
|
|
4167
4172
|
};
|
|
4168
4173
|
this.#llm = new LLM(this.config);
|
|
4169
4174
|
this.tools = new Map(tools);
|
|
@@ -4205,6 +4210,10 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4205
4210
|
get lastResult() {
|
|
4206
4211
|
return this.#lastResult;
|
|
4207
4212
|
}
|
|
4213
|
+
/** 当前任务自身的历史事件(不含之前任务留下的记录),用于 prompt 拼装、循环检测等按任务隔离的场景 */
|
|
4214
|
+
get #currentTaskHistory() {
|
|
4215
|
+
return this.history.slice(this.#taskHistoryStart);
|
|
4216
|
+
}
|
|
4208
4217
|
/** 触发 statuschange 事件 */
|
|
4209
4218
|
#emitStatusChange() {
|
|
4210
4219
|
this.dispatchEvent(new Event("statuschange"));
|
|
@@ -4256,7 +4265,11 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4256
4265
|
if (!task) throw new Error("Task is required");
|
|
4257
4266
|
this.task = task;
|
|
4258
4267
|
this.taskId = uid();
|
|
4259
|
-
this
|
|
4268
|
+
this.#taskHistoryStart = this.history.length;
|
|
4269
|
+
this.#emitHistoryChange({
|
|
4270
|
+
type: "task_start",
|
|
4271
|
+
task
|
|
4272
|
+
});
|
|
4260
4273
|
this.#observations = [];
|
|
4261
4274
|
this.#states = {
|
|
4262
4275
|
totalWaitTime: 0,
|
|
@@ -4297,7 +4310,7 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4297
4310
|
taskResult = {
|
|
4298
4311
|
success: false,
|
|
4299
4312
|
data: message,
|
|
4300
|
-
history: this
|
|
4313
|
+
history: this.#currentTaskHistory
|
|
4301
4314
|
};
|
|
4302
4315
|
this.#lastResult = taskResult;
|
|
4303
4316
|
finalStatus = "error";
|
|
@@ -4355,7 +4368,7 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4355
4368
|
taskResult = {
|
|
4356
4369
|
success,
|
|
4357
4370
|
data,
|
|
4358
|
-
history: this
|
|
4371
|
+
history: this.#currentTaskHistory
|
|
4359
4372
|
};
|
|
4360
4373
|
this.#lastResult = taskResult;
|
|
4361
4374
|
finalStatus = "completed";
|
|
@@ -4377,14 +4390,14 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4377
4390
|
taskResult = {
|
|
4378
4391
|
success: false,
|
|
4379
4392
|
data: message,
|
|
4380
|
-
history: this
|
|
4393
|
+
history: this.#currentTaskHistory
|
|
4381
4394
|
};
|
|
4382
4395
|
this.#lastResult = taskResult;
|
|
4383
4396
|
finalStatus = isAbortError ? "stopped" : "error";
|
|
4384
4397
|
break;
|
|
4385
4398
|
} finally {
|
|
4386
4399
|
console.groupEnd();
|
|
4387
|
-
await onAfterStep?.(this, this
|
|
4400
|
+
await onAfterStep?.(this, this.#currentTaskHistory);
|
|
4388
4401
|
}
|
|
4389
4402
|
step++;
|
|
4390
4403
|
}
|
|
@@ -4506,7 +4519,7 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4506
4519
|
*/
|
|
4507
4520
|
async #handleObservations(step) {
|
|
4508
4521
|
if (this.#states.totalWaitTime >= 3) this.pushObservation(`You have waited ${this.#states.totalWaitTime} seconds accumulatively. DO NOT wait any longer unless you have a good reason.`);
|
|
4509
|
-
const recentSteps = this.
|
|
4522
|
+
const recentSteps = this.#currentTaskHistory.filter((e) => e.type === "step").slice(-3);
|
|
4510
4523
|
if (recentSteps.length === 3) {
|
|
4511
4524
|
const isSameAction = (a, b) => a.action.name === b.action.name && JSON.stringify(a.action.input) === JSON.stringify(b.action.input);
|
|
4512
4525
|
if (isSameAction(recentSteps[0], recentSteps[1]) && isSameAction(recentSteps[1], recentSteps[2])) this.pushObservation(`⚠️ Loop detected: you repeated the exact same action ("${recentSteps[0].action.name}" with identical input) 3 times in a row with no new result. STOP repeating it. If the target genuinely does not exist (e.g. search returns empty), report this explicitly and call \`done\`, or try a clearly different action/strategy instead.`);
|
|
@@ -4539,7 +4552,8 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4539
4552
|
const browserState = this.#states.browserState;
|
|
4540
4553
|
let prompt = "";
|
|
4541
4554
|
prompt += await this.#getInstructions();
|
|
4542
|
-
const
|
|
4555
|
+
const currentTaskHistory = this.#currentTaskHistory;
|
|
4556
|
+
const stepCount = currentTaskHistory.filter((e) => e.type === "step").length;
|
|
4543
4557
|
prompt += "<agent_state>\n";
|
|
4544
4558
|
prompt += "<user_request>\n";
|
|
4545
4559
|
prompt += `${this.task}\n`;
|
|
@@ -4549,10 +4563,10 @@ var AgentRuntime = class extends EventTarget {
|
|
|
4549
4563
|
prompt += `Current time: ${(/* @__PURE__ */ new Date()).toLocaleString()}\n`;
|
|
4550
4564
|
prompt += "</step_info>\n";
|
|
4551
4565
|
prompt += "</agent_state>\n\n";
|
|
4552
|
-
const windowStart = Math.max(0, stepCount -
|
|
4566
|
+
const windowStart = Math.max(0, stepCount - this.config.historyWindow);
|
|
4553
4567
|
prompt += "<agent_history>\n";
|
|
4554
4568
|
let stepIndex = 0;
|
|
4555
|
-
for (const event of
|
|
4569
|
+
for (const event of currentTaskHistory) if (event.type === "step") {
|
|
4556
4570
|
stepIndex++;
|
|
4557
4571
|
if (stepIndex <= windowStart) {
|
|
4558
4572
|
prompt += `<step_${stepIndex}>Action: ${event.action.name} — Next Goal: ${event.reflection.next_goal}</step_${stepIndex}>\n`;
|