dsh-agent-enhancer 0.3.19 → 0.3.21
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.en.md +2 -2
- package/README.md +2 -2
- package/lib/client/browser-agent/tab.d.ts.map +1 -1
- package/lib/client/locales.d.ts +39 -1
- package/lib/client/locales.d.ts.map +1 -1
- package/lib/client.js +431 -31
- package/lib/client.js.map +1 -1
- package/lib/host/browser-agent/demo-heal.d.ts +129 -0
- package/lib/host/browser-agent/demo-heal.d.ts.map +1 -0
- package/lib/host/browser-agent/demo-recipes.d.ts +71 -0
- package/lib/host/browser-agent/demo-recipes.d.ts.map +1 -1
- package/lib/host/browser-agent/demo-replay.d.ts +13 -1
- package/lib/host/browser-agent/demo-replay.d.ts.map +1 -1
- package/lib/host/browser-agent/driver.d.ts +12 -0
- package/lib/host/browser-agent/driver.d.ts.map +1 -1
- package/lib/host/browser-agent/install.d.ts +14 -2
- package/lib/host/browser-agent/install.d.ts.map +1 -1
- package/lib/host/browser-agent/playwright-driver.d.ts +12 -0
- package/lib/host/browser-agent/playwright-driver.d.ts.map +1 -1
- package/lib/host/browser-agent/tools.d.ts +8 -1
- package/lib/host/browser-agent/tools.d.ts.map +1 -1
- package/lib/host/index.d.ts.map +1 -1
- package/lib/host/rpc.d.ts +128 -0
- package/lib/host/rpc.d.ts.map +1 -1
- package/lib/index.js +961 -156
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1995,6 +1995,71 @@ var PlaywrightDriver = class {
|
|
|
1995
1995
|
tabId: this.tabIdFor(page)
|
|
1996
1996
|
};
|
|
1997
1997
|
}
|
|
1998
|
+
async resolveRefBySelector(selector) {
|
|
1999
|
+
const page = await this.requirePage();
|
|
2000
|
+
const raw = await page.evaluate(({ sel, attr }) => {
|
|
2001
|
+
try {
|
|
2002
|
+
const el = document.querySelector(sel);
|
|
2003
|
+
if (el === null) return null;
|
|
2004
|
+
return el.getAttribute(attr);
|
|
2005
|
+
} catch {
|
|
2006
|
+
return null;
|
|
2007
|
+
}
|
|
2008
|
+
}, { sel: selector, attr: PLAYWRIGHT_REF_ATTR }).catch(() => null);
|
|
2009
|
+
if (raw === null) return null;
|
|
2010
|
+
const ref = Number(raw);
|
|
2011
|
+
return Number.isFinite(ref) ? ref : null;
|
|
2012
|
+
}
|
|
2013
|
+
/**
|
|
2014
|
+
* Role, accessible name and a readable CSS path for a ref, so a repair can
|
|
2015
|
+
* record how the control was found this time. Best-effort: a stale ref or a
|
|
2016
|
+
* detached node yields an empty selector rather than failing the tool call.
|
|
2017
|
+
*/
|
|
2018
|
+
async refInfo(ref) {
|
|
2019
|
+
const page = this.page;
|
|
2020
|
+
if (page === null || page.isClosed()) return null;
|
|
2021
|
+
const record2 = this.tabs.ref(this.tabIdFor(page), ref);
|
|
2022
|
+
if (record2 === void 0) return null;
|
|
2023
|
+
const selector = await page.evaluate(({ wanted, attr }) => {
|
|
2024
|
+
const element = document.querySelector(`[${attr}="${wanted}"]`);
|
|
2025
|
+
if (element === null) return "";
|
|
2026
|
+
const escaped = (value) => typeof CSS !== "undefined" && typeof CSS.escape === "function" ? CSS.escape(value) : value.replace(/[^a-zA-Z0-9_-]/gu, "\\$&");
|
|
2027
|
+
const quote = (value) => `"${value.replace(/"/gu, '\\"')}"`;
|
|
2028
|
+
if (element.id !== "") return `#${escaped(element.id)}`;
|
|
2029
|
+
const parts = [];
|
|
2030
|
+
let cursor = element;
|
|
2031
|
+
for (let depth = 0; depth < 6 && cursor !== null; depth += 1) {
|
|
2032
|
+
const node = cursor;
|
|
2033
|
+
const tag = node.tagName.toLowerCase();
|
|
2034
|
+
const testId = node.getAttribute("data-testid");
|
|
2035
|
+
if (testId !== null && testId !== "") {
|
|
2036
|
+
parts.unshift(`${tag}[data-testid=${quote(testId)}]`);
|
|
2037
|
+
break;
|
|
2038
|
+
}
|
|
2039
|
+
const name2 = node.getAttribute("name");
|
|
2040
|
+
if (name2 !== null && name2 !== "" && /^(input|select|textarea|button|form)$/u.test(tag)) {
|
|
2041
|
+
parts.unshift(`${tag}[name=${quote(name2)}]`);
|
|
2042
|
+
break;
|
|
2043
|
+
}
|
|
2044
|
+
let part = tag;
|
|
2045
|
+
const parent = node.parentElement;
|
|
2046
|
+
if (parent !== null) {
|
|
2047
|
+
const sameTag = Array.from(parent.children).filter((child) => child.tagName === node.tagName);
|
|
2048
|
+
if (sameTag.length > 1) part += `:nth-of-type(${sameTag.indexOf(node) + 1})`;
|
|
2049
|
+
}
|
|
2050
|
+
parts.unshift(part);
|
|
2051
|
+
if (node === document.body) break;
|
|
2052
|
+
cursor = parent;
|
|
2053
|
+
}
|
|
2054
|
+
return parts.join(" > ").slice(0, 240);
|
|
2055
|
+
}, { wanted: String(ref), attr: PLAYWRIGHT_REF_ATTR }).catch(() => "");
|
|
2056
|
+
return {
|
|
2057
|
+
role: record2.role,
|
|
2058
|
+
name: record2.name,
|
|
2059
|
+
selector,
|
|
2060
|
+
href: record2.href
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
1998
2063
|
async press(key) {
|
|
1999
2064
|
const page = await this.requirePage();
|
|
2000
2065
|
await page.keyboard.press(key);
|
|
@@ -2729,26 +2794,49 @@ function classify(input) {
|
|
|
2729
2794
|
function normalizeControlName(raw) {
|
|
2730
2795
|
return raw.replace(/[\u200b-\u200d\ufeff]/gu, "").replace(/\s+/gu, " ").trim().toLowerCase();
|
|
2731
2796
|
}
|
|
2732
|
-
function
|
|
2797
|
+
function matchDemoControls(nodes, role, name2) {
|
|
2733
2798
|
const wanted = normalizeControlName(name2);
|
|
2734
|
-
if (wanted === "") return
|
|
2799
|
+
if (wanted === "") return [];
|
|
2735
2800
|
const sameRole = nodes.filter((node) => node.role === role);
|
|
2736
|
-
const exact = sameRole.
|
|
2737
|
-
if (exact
|
|
2738
|
-
const exactAnyRole = nodes.
|
|
2739
|
-
if (exactAnyRole
|
|
2740
|
-
|
|
2801
|
+
const exact = sameRole.filter((node) => normalizeControlName(node.name) === wanted);
|
|
2802
|
+
if (exact.length > 0) return exact;
|
|
2803
|
+
const exactAnyRole = nodes.filter((node) => normalizeControlName(node.name) === wanted);
|
|
2804
|
+
if (exactAnyRole.length > 0) return exactAnyRole;
|
|
2805
|
+
return sameRole.filter((node) => {
|
|
2741
2806
|
const candidate = normalizeControlName(node.name);
|
|
2742
2807
|
return candidate.includes(wanted) || wanted.includes(candidate);
|
|
2743
2808
|
});
|
|
2744
|
-
|
|
2809
|
+
}
|
|
2810
|
+
var CONTROL_ATTEMPTS = 3;
|
|
2811
|
+
var CONTROL_RETRY_MS = 600;
|
|
2812
|
+
var pause = (ms) => new Promise((resolve17) => {
|
|
2813
|
+
setTimeout(resolve17, ms);
|
|
2814
|
+
});
|
|
2815
|
+
async function locateControl(driver, selector, role, name2, retryDelayMs) {
|
|
2816
|
+
let lastUrl = "";
|
|
2817
|
+
for (let attempt = 0; attempt < CONTROL_ATTEMPTS; attempt += 1) {
|
|
2818
|
+
const snapshot = await driver.snapshot();
|
|
2819
|
+
lastUrl = snapshot.url;
|
|
2820
|
+
const candidates = matchDemoControls(snapshot.nodes, role, name2);
|
|
2821
|
+
if (candidates.length === 1) return { node: candidates[0] };
|
|
2822
|
+
if (candidates.length > 1) {
|
|
2823
|
+
const ref = typeof driver.resolveRefBySelector === "function" && (selector ?? "").trim() !== "" ? await driver.resolveRefBySelector(selector).catch(() => null) : null;
|
|
2824
|
+
const hit = ref === null ? void 0 : candidates.find((node) => node.ref === ref);
|
|
2825
|
+
if (hit !== void 0) return { node: hit };
|
|
2826
|
+
return {
|
|
2827
|
+
reason: `${candidates.length} controls match ${role} ${JSON.stringify(name2)} on ${snapshot.url}; the recorded selector does not identify one`
|
|
2828
|
+
};
|
|
2829
|
+
}
|
|
2830
|
+
if (attempt < CONTROL_ATTEMPTS - 1) await pause(retryDelayMs);
|
|
2831
|
+
}
|
|
2832
|
+
return { reason: `no control matching ${role} ${JSON.stringify(name2)} on ${lastUrl}` };
|
|
2745
2833
|
}
|
|
2746
2834
|
function replayObstacle(step) {
|
|
2747
2835
|
if (step.kind !== "fill") return null;
|
|
2748
2836
|
if (step.masked) return `secret field ${JSON.stringify(step.name)} was masked; type it yourself`;
|
|
2749
2837
|
return null;
|
|
2750
2838
|
}
|
|
2751
|
-
async function performStep(driver, step) {
|
|
2839
|
+
async function performStep(driver, step, retryDelayMs = CONTROL_RETRY_MS) {
|
|
2752
2840
|
switch (step.kind) {
|
|
2753
2841
|
case "navigate": {
|
|
2754
2842
|
const current = (await driver.snapshot()).url;
|
|
@@ -2757,26 +2845,19 @@ async function performStep(driver, step) {
|
|
|
2757
2845
|
return { ok: true, url: result.url };
|
|
2758
2846
|
}
|
|
2759
2847
|
case "click": {
|
|
2760
|
-
const
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
return {
|
|
2764
|
-
ok: false,
|
|
2765
|
-
reason: `no control matching ${step.role} ${JSON.stringify(step.name)} on ${snapshot.url}`
|
|
2766
|
-
};
|
|
2848
|
+
const located = await locateControl(driver, step.selector, step.role, step.name, retryDelayMs);
|
|
2849
|
+
if (located.node === void 0) {
|
|
2850
|
+
return { ok: false, reason: located.reason ?? "control not found" };
|
|
2767
2851
|
}
|
|
2768
|
-
const result = await driver.click(node.ref);
|
|
2852
|
+
const result = await driver.click(located.node.ref);
|
|
2769
2853
|
return { ok: true, url: result.url };
|
|
2770
2854
|
}
|
|
2771
2855
|
case "fill": {
|
|
2772
|
-
const
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
return {
|
|
2776
|
-
ok: false,
|
|
2777
|
-
reason: `no field matching ${step.role} ${JSON.stringify(step.name)} on ${snapshot.url}`
|
|
2778
|
-
};
|
|
2856
|
+
const located = await locateControl(driver, step.selector, step.role, step.name, retryDelayMs);
|
|
2857
|
+
if (located.node === void 0) {
|
|
2858
|
+
return { ok: false, reason: located.reason ?? "field not found" };
|
|
2779
2859
|
}
|
|
2860
|
+
const node = located.node;
|
|
2780
2861
|
if (step.role === "combobox") {
|
|
2781
2862
|
const result2 = await driver.select(node.ref, step.value);
|
|
2782
2863
|
return { ok: true, url: result2.url };
|
|
@@ -2808,7 +2889,8 @@ function samePage(a, b) {
|
|
|
2808
2889
|
return a === b;
|
|
2809
2890
|
}
|
|
2810
2891
|
}
|
|
2811
|
-
async function replayDemoSteps(driver, steps) {
|
|
2892
|
+
async function replayDemoSteps(driver, steps, options = {}) {
|
|
2893
|
+
const retryDelayMs = options.retryDelayMs ?? CONTROL_RETRY_MS;
|
|
2812
2894
|
const lines = [];
|
|
2813
2895
|
let currentTab;
|
|
2814
2896
|
let url;
|
|
@@ -2883,7 +2965,7 @@ async function replayDemoSteps(driver, steps) {
|
|
|
2883
2965
|
...url === void 0 ? {} : { url }
|
|
2884
2966
|
};
|
|
2885
2967
|
}
|
|
2886
|
-
const outcome = await performStep(driver, step).catch((error) => ({
|
|
2968
|
+
const outcome = await performStep(driver, step, retryDelayMs).catch((error) => ({
|
|
2887
2969
|
ok: false,
|
|
2888
2970
|
reason: error instanceof Error ? error.message : String(error)
|
|
2889
2971
|
}));
|
|
@@ -2910,6 +2992,547 @@ async function replayDemoSteps(driver, steps) {
|
|
|
2910
2992
|
};
|
|
2911
2993
|
}
|
|
2912
2994
|
|
|
2995
|
+
// src/host/browser-agent/demo-recipes.ts
|
|
2996
|
+
import { mkdirSync as mkdirSync2, readFileSync, writeFileSync } from "node:fs";
|
|
2997
|
+
import { dirname } from "node:path";
|
|
2998
|
+
|
|
2999
|
+
// src/host/browser-agent/demo-heal.ts
|
|
3000
|
+
var REPAIR_WINDOW_MS = 10 * 6e4;
|
|
3001
|
+
var MAX_OBSERVED = 30;
|
|
3002
|
+
var MAX_INSERTED = 8;
|
|
3003
|
+
function stepFromObservation(action) {
|
|
3004
|
+
switch (action.kind) {
|
|
3005
|
+
case "navigate":
|
|
3006
|
+
return action.url === void 0 || action.url === "" ? null : { kind: "navigate", at: action.at, url: action.url };
|
|
3007
|
+
case "click":
|
|
3008
|
+
return action.name === void 0 || action.name === "" ? null : {
|
|
3009
|
+
kind: "click",
|
|
3010
|
+
at: action.at,
|
|
3011
|
+
role: action.role ?? "button",
|
|
3012
|
+
name: action.name,
|
|
3013
|
+
selector: action.selector ?? ""
|
|
3014
|
+
};
|
|
3015
|
+
case "fill":
|
|
3016
|
+
return action.name === void 0 || action.name === "" || (action.value ?? "") === "" ? null : {
|
|
3017
|
+
kind: "fill",
|
|
3018
|
+
at: action.at,
|
|
3019
|
+
role: action.role ?? "textbox",
|
|
3020
|
+
name: action.name,
|
|
3021
|
+
selector: action.selector ?? "",
|
|
3022
|
+
field: action.field ?? action.name,
|
|
3023
|
+
value: action.value ?? "",
|
|
3024
|
+
masked: false
|
|
3025
|
+
};
|
|
3026
|
+
case "press":
|
|
3027
|
+
return action.key === void 0 || action.key === "" ? null : { kind: "press", at: action.at, key: action.key, selector: action.selector ?? "" };
|
|
3028
|
+
default: {
|
|
3029
|
+
const exhaustive = action.kind;
|
|
3030
|
+
return exhaustive;
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
function retarget(step, action) {
|
|
3035
|
+
if (step.kind !== action.kind) return null;
|
|
3036
|
+
const site = {
|
|
3037
|
+
...step.tabId === void 0 ? {} : { tabId: step.tabId },
|
|
3038
|
+
...step.tabUrl === void 0 ? {} : { tabUrl: step.tabUrl }
|
|
3039
|
+
};
|
|
3040
|
+
switch (step.kind) {
|
|
3041
|
+
case "navigate":
|
|
3042
|
+
return { ...step, url: action.url ?? step.url };
|
|
3043
|
+
case "click":
|
|
3044
|
+
return {
|
|
3045
|
+
kind: "click",
|
|
3046
|
+
at: step.at,
|
|
3047
|
+
role: action.role ?? step.role,
|
|
3048
|
+
name: action.name ?? step.name,
|
|
3049
|
+
selector: action.selector ?? step.selector,
|
|
3050
|
+
...site
|
|
3051
|
+
};
|
|
3052
|
+
case "fill":
|
|
3053
|
+
return {
|
|
3054
|
+
kind: "fill",
|
|
3055
|
+
at: step.at,
|
|
3056
|
+
role: action.role ?? step.role,
|
|
3057
|
+
name: action.name ?? step.name,
|
|
3058
|
+
selector: action.selector ?? step.selector,
|
|
3059
|
+
field: action.field ?? step.field,
|
|
3060
|
+
value: step.value,
|
|
3061
|
+
masked: step.masked,
|
|
3062
|
+
...step.checked === void 0 ? {} : { checked: step.checked },
|
|
3063
|
+
...site
|
|
3064
|
+
};
|
|
3065
|
+
case "press":
|
|
3066
|
+
return {
|
|
3067
|
+
kind: "press",
|
|
3068
|
+
at: step.at,
|
|
3069
|
+
key: step.key,
|
|
3070
|
+
selector: action.selector ?? step.selector,
|
|
3071
|
+
...site
|
|
3072
|
+
};
|
|
3073
|
+
default: {
|
|
3074
|
+
const exhaustive = step;
|
|
3075
|
+
return exhaustive;
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
function pickObserved(step, observed) {
|
|
3080
|
+
const compatible = [];
|
|
3081
|
+
observed.forEach((action, index) => {
|
|
3082
|
+
if (action.kind !== step.kind) return;
|
|
3083
|
+
if (step.kind !== "navigate" && (action.name ?? "") === "") return;
|
|
3084
|
+
if (step.kind === "fill" && (action.value ?? "") === "") return;
|
|
3085
|
+
compatible.push({ action, index });
|
|
3086
|
+
});
|
|
3087
|
+
if (compatible.length === 0) return -1;
|
|
3088
|
+
if (step.kind === "navigate") return compatible[0].index;
|
|
3089
|
+
const wanted = step.kind === "press" ? "" : normalizeControlName(step.name);
|
|
3090
|
+
if (wanted !== "") {
|
|
3091
|
+
const similar = compatible.find(({ action }) => {
|
|
3092
|
+
const candidate = normalizeControlName(action.name ?? "");
|
|
3093
|
+
if (candidate === "") return false;
|
|
3094
|
+
if (candidate.length < 2 || wanted.length < 2) return candidate === wanted;
|
|
3095
|
+
return candidate.includes(wanted) || wanted.includes(candidate);
|
|
3096
|
+
});
|
|
3097
|
+
if (similar !== void 0) return similar.index;
|
|
3098
|
+
}
|
|
3099
|
+
return compatible[0].index;
|
|
3100
|
+
}
|
|
3101
|
+
function buildHealProposal(input) {
|
|
3102
|
+
const step = input.steps[input.failedIndex];
|
|
3103
|
+
if (step === void 0) return null;
|
|
3104
|
+
if (step.kind === "fill" && step.masked) return null;
|
|
3105
|
+
const matchAt = pickObserved(step, input.observed);
|
|
3106
|
+
if (matchAt < 0) return null;
|
|
3107
|
+
const action = input.observed[matchAt];
|
|
3108
|
+
const replacement = retarget(step, action);
|
|
3109
|
+
if (replacement === null) return null;
|
|
3110
|
+
const unchanged = step.kind === "navigate" ? replacement.kind === "navigate" && step.url === replacement.url : step.kind === "press" ? replacement.kind === "press" && step.selector === replacement.selector : "name" in replacement && "name" in step && normalizeControlName(step.name) === normalizeControlName(replacement.name) && step.role === replacement.role;
|
|
3111
|
+
if (unchanged) return null;
|
|
3112
|
+
const inserted = [];
|
|
3113
|
+
for (const entry of input.observed.slice(0, matchAt)) {
|
|
3114
|
+
if (inserted.length >= MAX_INSERTED) break;
|
|
3115
|
+
const built = stepFromObservation(entry);
|
|
3116
|
+
if (built !== null) inserted.push(built);
|
|
3117
|
+
}
|
|
3118
|
+
const sanitizedInserted = sanitizeDemoSteps(inserted);
|
|
3119
|
+
if (sanitizedInserted.some((entry) => entry.kind === "fill" && entry.masked)) return null;
|
|
3120
|
+
const sanitizedReplacement = sanitizeDemoSteps([replacement])[0] ?? replacement;
|
|
3121
|
+
if (sanitizedReplacement.kind === "fill" && sanitizedReplacement.masked) return null;
|
|
3122
|
+
return {
|
|
3123
|
+
id: input.id ?? `h-${input.now.toString(36)}`,
|
|
3124
|
+
at: input.now,
|
|
3125
|
+
failedAt: input.failedIndex + 1,
|
|
3126
|
+
reason: input.reason.slice(0, 500),
|
|
3127
|
+
inserted: sanitizedInserted,
|
|
3128
|
+
replacement: sanitizedReplacement
|
|
3129
|
+
};
|
|
3130
|
+
}
|
|
3131
|
+
function sameIdentity(a, b) {
|
|
3132
|
+
if (a.kind !== b.kind) return false;
|
|
3133
|
+
if (a.kind === "navigate" && b.kind === "navigate") return a.url === b.url;
|
|
3134
|
+
if (a.kind === "press" && b.kind === "press") return a.key === b.key;
|
|
3135
|
+
if ("name" in a && "name" in b) {
|
|
3136
|
+
return normalizeControlName(a.name) === normalizeControlName(b.name) && a.role === b.role;
|
|
3137
|
+
}
|
|
3138
|
+
return false;
|
|
3139
|
+
}
|
|
3140
|
+
function applyHealProposal(steps, proposal) {
|
|
3141
|
+
const at = proposal.failedAt - 1;
|
|
3142
|
+
if (at < 0 || at >= steps.length) return [...steps];
|
|
3143
|
+
return [
|
|
3144
|
+
...steps.slice(0, at),
|
|
3145
|
+
...proposal.inserted,
|
|
3146
|
+
proposal.replacement,
|
|
3147
|
+
...steps.slice(at + 1)
|
|
3148
|
+
];
|
|
3149
|
+
}
|
|
3150
|
+
function describeHealProposal(proposal) {
|
|
3151
|
+
const lines = [
|
|
3152
|
+
`step ${proposal.failedAt} now uses ${describeDemoStep(proposal.replacement)}`
|
|
3153
|
+
];
|
|
3154
|
+
if (proposal.inserted.length > 0) {
|
|
3155
|
+
lines.push(`+${proposal.inserted.length} step(s) before it: ${proposal.inserted.map(describeDemoStep).join("; ")}`);
|
|
3156
|
+
}
|
|
3157
|
+
return lines;
|
|
3158
|
+
}
|
|
3159
|
+
function createRepairHooks(input) {
|
|
3160
|
+
const clock = input.now ?? Date.now;
|
|
3161
|
+
return {
|
|
3162
|
+
active: () => input.tracker.active,
|
|
3163
|
+
begin: (target) => {
|
|
3164
|
+
input.tracker.begin(target);
|
|
3165
|
+
},
|
|
3166
|
+
observe: (action) => {
|
|
3167
|
+
input.tracker.observe(action);
|
|
3168
|
+
},
|
|
3169
|
+
commit: () => {
|
|
3170
|
+
const pending = input.tracker.take();
|
|
3171
|
+
if (pending === null || input.store === void 0) return null;
|
|
3172
|
+
const recipe = input.store.get(pending.target.recipeId);
|
|
3173
|
+
if (recipe === void 0) return null;
|
|
3174
|
+
const current = recipe.steps[pending.target.index];
|
|
3175
|
+
if (current === void 0 || !sameIdentity(current, pending.target.step)) return null;
|
|
3176
|
+
const proposal = buildHealProposal({
|
|
3177
|
+
steps: recipe.steps,
|
|
3178
|
+
failedIndex: pending.target.index,
|
|
3179
|
+
reason: pending.target.reason,
|
|
3180
|
+
observed: pending.observed,
|
|
3181
|
+
now: clock()
|
|
3182
|
+
});
|
|
3183
|
+
if (proposal === null) return null;
|
|
3184
|
+
if (!input.store.recordHeal({ id: recipe.id, proposal }).ok) return null;
|
|
3185
|
+
return {
|
|
3186
|
+
recipeId: recipe.id,
|
|
3187
|
+
recipeName: recipe.name,
|
|
3188
|
+
failedAt: proposal.failedAt,
|
|
3189
|
+
lines: describeHealProposal(proposal)
|
|
3190
|
+
};
|
|
3191
|
+
}
|
|
3192
|
+
};
|
|
3193
|
+
}
|
|
3194
|
+
var DemoRepairTracker = class {
|
|
3195
|
+
pending = null;
|
|
3196
|
+
begin(target) {
|
|
3197
|
+
this.pending = {
|
|
3198
|
+
target: { ...target, at: target.at ?? Date.now() },
|
|
3199
|
+
observed: []
|
|
3200
|
+
};
|
|
3201
|
+
}
|
|
3202
|
+
observe(action) {
|
|
3203
|
+
const pending = this.pending;
|
|
3204
|
+
if (pending === null) return;
|
|
3205
|
+
if (action.at - pending.target.at > REPAIR_WINDOW_MS) return;
|
|
3206
|
+
if (pending.target.tabId !== void 0 && action.tabId !== void 0 && pending.target.tabId !== action.tabId) return;
|
|
3207
|
+
if (pending.observed.length >= MAX_OBSERVED) return;
|
|
3208
|
+
pending.observed.push(action);
|
|
3209
|
+
}
|
|
3210
|
+
/** Hand back the target and what happened, and stop tracking. */
|
|
3211
|
+
take() {
|
|
3212
|
+
const pending = this.pending;
|
|
3213
|
+
this.pending = null;
|
|
3214
|
+
return pending === null ? null : { target: pending.target, observed: [...pending.observed] };
|
|
3215
|
+
}
|
|
3216
|
+
get active() {
|
|
3217
|
+
return this.pending !== null;
|
|
3218
|
+
}
|
|
3219
|
+
};
|
|
3220
|
+
|
|
3221
|
+
// src/host/browser-agent/demo-recipes.ts
|
|
3222
|
+
var MAX_RECIPES = 20;
|
|
3223
|
+
var MAX_RECIPE_NAME = 60;
|
|
3224
|
+
var MAX_RECIPE_STEPS = 200;
|
|
3225
|
+
var MAX_HISTORY = 5;
|
|
3226
|
+
var FILE_VERSION = 2;
|
|
3227
|
+
function metaOf(recipe) {
|
|
3228
|
+
return {
|
|
3229
|
+
id: recipe.id,
|
|
3230
|
+
name: recipe.name,
|
|
3231
|
+
createdAt: recipe.createdAt,
|
|
3232
|
+
updatedAt: recipe.updatedAt,
|
|
3233
|
+
steps: recipe.steps.length,
|
|
3234
|
+
variables: recipe.variables,
|
|
3235
|
+
rev: recipe.rev,
|
|
3236
|
+
history: recipe.history.length,
|
|
3237
|
+
...recipe.pendingHeal === void 0 ? {} : { pendingHeal: recipe.pendingHeal }
|
|
3238
|
+
};
|
|
3239
|
+
}
|
|
3240
|
+
function nextVersion(recipe, source, now) {
|
|
3241
|
+
return [
|
|
3242
|
+
...recipe.history,
|
|
3243
|
+
{ rev: recipe.rev, at: now, source, steps: recipe.steps, variables: recipe.variables }
|
|
3244
|
+
].slice(-MAX_HISTORY);
|
|
3245
|
+
}
|
|
3246
|
+
var VAR_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
3247
|
+
var VAR_TOKEN = /\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g;
|
|
3248
|
+
function applyRecipeVars(steps, values) {
|
|
3249
|
+
const fill = (text3) => text3.replace(VAR_TOKEN, (match, name2) => values[name2] ?? match);
|
|
3250
|
+
return steps.map((step) => {
|
|
3251
|
+
if (step.kind === "navigate") {
|
|
3252
|
+
const url = fill(step.url);
|
|
3253
|
+
return url === step.url ? step : { ...step, url };
|
|
3254
|
+
}
|
|
3255
|
+
if (step.kind === "fill") {
|
|
3256
|
+
const value = fill(step.value);
|
|
3257
|
+
return value === step.value ? step : { ...step, value };
|
|
3258
|
+
}
|
|
3259
|
+
return step;
|
|
3260
|
+
});
|
|
3261
|
+
}
|
|
3262
|
+
function missingRecipeVars(recipe, values) {
|
|
3263
|
+
return recipe.variables.filter((variable) => (values[variable.name] ?? "").trim() === "").map((variable) => variable.name);
|
|
3264
|
+
}
|
|
3265
|
+
function sanitizeVariables(raw) {
|
|
3266
|
+
if (!Array.isArray(raw)) return [];
|
|
3267
|
+
return raw.filter((value) => {
|
|
3268
|
+
if (value === null || typeof value !== "object") return false;
|
|
3269
|
+
const name2 = value.name;
|
|
3270
|
+
return typeof name2 === "string" && VAR_NAME.test(name2);
|
|
3271
|
+
}).map((variable) => ({ name: variable.name, default: String(variable.default ?? "").slice(0, 500) }));
|
|
3272
|
+
}
|
|
3273
|
+
function sanitizeHistory(raw) {
|
|
3274
|
+
if (!Array.isArray(raw)) return [];
|
|
3275
|
+
const versions = [];
|
|
3276
|
+
for (const value of raw) {
|
|
3277
|
+
if (value === null || typeof value !== "object") continue;
|
|
3278
|
+
const entry = value;
|
|
3279
|
+
const steps = sanitizeRecipeSteps(entry.steps);
|
|
3280
|
+
if (steps.length === 0) continue;
|
|
3281
|
+
versions.push({
|
|
3282
|
+
rev: Number.isFinite(entry.rev) && (entry.rev ?? 0) > 0 ? Number(entry.rev) : 1,
|
|
3283
|
+
at: Number.isFinite(entry.at) ? Number(entry.at) : 0,
|
|
3284
|
+
source: entry.source === "parameterize" || entry.source === "heal" || entry.source === "rollback" ? entry.source : "save",
|
|
3285
|
+
steps,
|
|
3286
|
+
variables: sanitizeVariables(entry.variables)
|
|
3287
|
+
});
|
|
3288
|
+
}
|
|
3289
|
+
return versions.slice(-MAX_HISTORY);
|
|
3290
|
+
}
|
|
3291
|
+
function sanitizeProposal(raw) {
|
|
3292
|
+
if (raw === null || typeof raw !== "object") return void 0;
|
|
3293
|
+
const entry = raw;
|
|
3294
|
+
if (typeof entry.id !== "string" || entry.id === "") return void 0;
|
|
3295
|
+
const failedAt = Number(entry.failedAt);
|
|
3296
|
+
if (!Number.isInteger(failedAt) || failedAt < 1) return void 0;
|
|
3297
|
+
const replacement = sanitizeRecipeSteps(entry.replacement === void 0 ? [] : [entry.replacement])[0];
|
|
3298
|
+
if (replacement === void 0) return void 0;
|
|
3299
|
+
return {
|
|
3300
|
+
id: entry.id,
|
|
3301
|
+
at: Number.isFinite(entry.at) ? Number(entry.at) : 0,
|
|
3302
|
+
failedAt,
|
|
3303
|
+
reason: typeof entry.reason === "string" ? entry.reason.slice(0, 500) : "",
|
|
3304
|
+
inserted: sanitizeRecipeSteps(entry.inserted ?? []),
|
|
3305
|
+
replacement
|
|
3306
|
+
};
|
|
3307
|
+
}
|
|
3308
|
+
function isStep(value) {
|
|
3309
|
+
if (value === null || typeof value !== "object") return false;
|
|
3310
|
+
const kind = value.kind;
|
|
3311
|
+
return kind === "navigate" || kind === "click" || kind === "fill" || kind === "press";
|
|
3312
|
+
}
|
|
3313
|
+
function sanitizeRecipeSteps(raw) {
|
|
3314
|
+
if (!Array.isArray(raw)) return [];
|
|
3315
|
+
const steps = raw.filter(isStep).slice(0, MAX_RECIPE_STEPS);
|
|
3316
|
+
return sanitizeDemoSteps(steps).map((step) => {
|
|
3317
|
+
const { tabId: _volatileTabId, ...rest } = step;
|
|
3318
|
+
return rest;
|
|
3319
|
+
});
|
|
3320
|
+
}
|
|
3321
|
+
var DemoRecipeStore = class {
|
|
3322
|
+
constructor(filePath = null) {
|
|
3323
|
+
this.filePath = filePath;
|
|
3324
|
+
}
|
|
3325
|
+
filePath;
|
|
3326
|
+
memory = [];
|
|
3327
|
+
list() {
|
|
3328
|
+
return this.read().map(metaOf).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
3329
|
+
}
|
|
3330
|
+
get(id) {
|
|
3331
|
+
return this.read().find((recipe) => recipe.id === id);
|
|
3332
|
+
}
|
|
3333
|
+
/** Save under `name`; the same name (case-insensitive) updates in place. */
|
|
3334
|
+
save(input) {
|
|
3335
|
+
const name2 = input.name.replace(/\s+/gu, " ").trim();
|
|
3336
|
+
if (name2 === "") return { ok: false, error: "\u6D41\u7A0B\u540D\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
3337
|
+
if (name2.length > MAX_RECIPE_NAME) return { ok: false, error: `\u6D41\u7A0B\u540D\u6700\u957F ${MAX_RECIPE_NAME} \u4E2A\u5B57\u7B26` };
|
|
3338
|
+
const steps = sanitizeRecipeSteps(input.steps);
|
|
3339
|
+
if (steps.length === 0) return { ok: false, error: "\u8FD9\u6B21\u5F55\u5236\u6CA1\u6709\u53EF\u4FDD\u5B58\u7684\u6B65\u9AA4" };
|
|
3340
|
+
const now = input.now ?? Date.now();
|
|
3341
|
+
const recipes = this.read();
|
|
3342
|
+
const existing = recipes.find((recipe) => recipe.name.toLowerCase() === name2.toLowerCase());
|
|
3343
|
+
let saved;
|
|
3344
|
+
if (existing !== void 0) {
|
|
3345
|
+
const { pendingHeal: _answered, ...rest } = existing;
|
|
3346
|
+
saved = {
|
|
3347
|
+
...rest,
|
|
3348
|
+
name: name2,
|
|
3349
|
+
steps,
|
|
3350
|
+
updatedAt: now,
|
|
3351
|
+
rev: existing.rev + 1,
|
|
3352
|
+
history: nextVersion(existing, "save", now)
|
|
3353
|
+
};
|
|
3354
|
+
} else {
|
|
3355
|
+
if (recipes.length >= MAX_RECIPES) {
|
|
3356
|
+
return { ok: false, error: `\u6700\u591A\u4FDD\u5B58 ${MAX_RECIPES} \u4E2A\u6D41\u7A0B\uFF0C\u5148\u5220\u6389\u4E00\u4E2A` };
|
|
3357
|
+
}
|
|
3358
|
+
saved = {
|
|
3359
|
+
id: `r-${now.toString(36)}-${Math.random().toString(36).slice(2, 7)}`,
|
|
3360
|
+
name: name2,
|
|
3361
|
+
createdAt: now,
|
|
3362
|
+
updatedAt: now,
|
|
3363
|
+
steps,
|
|
3364
|
+
variables: [],
|
|
3365
|
+
rev: 1,
|
|
3366
|
+
history: []
|
|
3367
|
+
};
|
|
3368
|
+
}
|
|
3369
|
+
const next = existing === void 0 ? [...recipes, saved] : recipes.map((recipe) => recipe.id === saved.id ? saved : recipe);
|
|
3370
|
+
this.write(next);
|
|
3371
|
+
return { ok: true, recipe: metaOf(saved) };
|
|
3372
|
+
}
|
|
3373
|
+
remove(id) {
|
|
3374
|
+
const recipes = this.read();
|
|
3375
|
+
const next = recipes.filter((recipe) => recipe.id !== id);
|
|
3376
|
+
if (next.length === recipes.length) return false;
|
|
3377
|
+
this.write(next);
|
|
3378
|
+
return true;
|
|
3379
|
+
}
|
|
3380
|
+
/** Rename without touching the steps; the new name must stay unique. */
|
|
3381
|
+
rename(input) {
|
|
3382
|
+
const name2 = input.name.replace(/\s+/gu, " ").trim();
|
|
3383
|
+
if (name2 === "") return { ok: false, error: "\u6D41\u7A0B\u540D\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
3384
|
+
if (name2.length > MAX_RECIPE_NAME) return { ok: false, error: `\u6D41\u7A0B\u540D\u6700\u957F ${MAX_RECIPE_NAME} \u4E2A\u5B57\u7B26` };
|
|
3385
|
+
const recipes = this.read();
|
|
3386
|
+
const current = recipes.find((recipe) => recipe.id === input.id);
|
|
3387
|
+
if (current === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728" };
|
|
3388
|
+
const clash = recipes.find((recipe) => recipe.id !== current.id && recipe.name.toLowerCase() === name2.toLowerCase());
|
|
3389
|
+
if (clash !== void 0) return { ok: false, error: `\u5DF2\u7ECF\u6709\u53EB\u300C${clash.name}\u300D\u7684\u6D41\u7A0B` };
|
|
3390
|
+
const renamed = { ...current, name: name2, updatedAt: input.now ?? Date.now() };
|
|
3391
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? renamed : recipe));
|
|
3392
|
+
return { ok: true, recipe: metaOf(renamed) };
|
|
3393
|
+
}
|
|
3394
|
+
/**
|
|
3395
|
+
* Turn one fill step's literal into a `${name}` token, remembering the old
|
|
3396
|
+
* value as the default so the flow still runs unchanged.
|
|
3397
|
+
*/
|
|
3398
|
+
parameterize(input) {
|
|
3399
|
+
const name2 = input.name.trim();
|
|
3400
|
+
if (!VAR_NAME.test(name2)) {
|
|
3401
|
+
return { ok: false, error: "\u53D8\u91CF\u540D\u53EA\u80FD\u7528\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF0C\u4E14\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934" };
|
|
3402
|
+
}
|
|
3403
|
+
const recipes = this.read();
|
|
3404
|
+
const current = recipes.find((recipe) => recipe.id === input.id);
|
|
3405
|
+
if (current === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728" };
|
|
3406
|
+
const step = current.steps[input.index];
|
|
3407
|
+
if (step === void 0) return { ok: false, error: "\u6B65\u9AA4\u4E0D\u5B58\u5728" };
|
|
3408
|
+
if (step.kind !== "fill") return { ok: false, error: "\u53EA\u6709\u586B\u5199\u7C7B\u6B65\u9AA4\u53EF\u4EE5\u53D8\u6210\u53D8\u91CF" };
|
|
3409
|
+
if (step.masked) return { ok: false, error: "\u5BC6\u7801\u7C7B\u5B57\u6BB5\u4E0D\u80FD\u53D8\u6210\u53D8\u91CF" };
|
|
3410
|
+
if (new RegExp(VAR_TOKEN.source).test(step.value)) {
|
|
3411
|
+
return { ok: false, error: "\u8FD9\u4E00\u6B65\u5DF2\u7ECF\u662F\u53D8\u91CF\u4E86" };
|
|
3412
|
+
}
|
|
3413
|
+
if (current.variables.some((variable) => variable.name === name2)) {
|
|
3414
|
+
return { ok: false, error: "\u53D8\u91CF\u540D\u5DF2\u5B58\u5728" };
|
|
3415
|
+
}
|
|
3416
|
+
const token = "${" + name2 + "}";
|
|
3417
|
+
const steps = current.steps.map(
|
|
3418
|
+
(entry, index) => index === input.index && entry.kind === "fill" ? { ...entry, value: token } : entry
|
|
3419
|
+
);
|
|
3420
|
+
const variables = [...current.variables, { name: name2, default: step.value.slice(0, 500) }];
|
|
3421
|
+
const now = input.now ?? Date.now();
|
|
3422
|
+
const { pendingHeal: _stale, ...rest } = current;
|
|
3423
|
+
const next = {
|
|
3424
|
+
...rest,
|
|
3425
|
+
steps,
|
|
3426
|
+
variables,
|
|
3427
|
+
updatedAt: now,
|
|
3428
|
+
rev: current.rev + 1,
|
|
3429
|
+
history: nextVersion(current, "parameterize", now)
|
|
3430
|
+
};
|
|
3431
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? next : recipe));
|
|
3432
|
+
return { ok: true, recipe: metaOf(next) };
|
|
3433
|
+
}
|
|
3434
|
+
/** Store a repair for the user to accept; it never touches the steps by itself. */
|
|
3435
|
+
recordHeal(input) {
|
|
3436
|
+
const recipes = this.read();
|
|
3437
|
+
const current = recipes.find((recipe) => recipe.id === input.id);
|
|
3438
|
+
if (current === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728" };
|
|
3439
|
+
const next = { ...current, pendingHeal: input.proposal };
|
|
3440
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? next : recipe));
|
|
3441
|
+
return { ok: true, recipe: metaOf(next) };
|
|
3442
|
+
}
|
|
3443
|
+
/**
|
|
3444
|
+
* Accept or drop a pending repair. Accepting is the only path that rewrites
|
|
3445
|
+
* the flow, and it keeps the previous content one click away.
|
|
3446
|
+
*/
|
|
3447
|
+
resolveHeal(input) {
|
|
3448
|
+
const now = input.now ?? Date.now();
|
|
3449
|
+
const recipes = this.read();
|
|
3450
|
+
const current = recipes.find((recipe) => recipe.id === input.id);
|
|
3451
|
+
if (current === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728" };
|
|
3452
|
+
const proposal = current.pendingHeal;
|
|
3453
|
+
if (proposal === void 0) return { ok: false, error: "\u8FD9\u4E2A\u6D41\u7A0B\u6CA1\u6709\u5F85\u786E\u8BA4\u7684\u4FEE\u590D" };
|
|
3454
|
+
const { pendingHeal: _resolved, ...rest } = current;
|
|
3455
|
+
if (!input.accept) {
|
|
3456
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? rest : recipe));
|
|
3457
|
+
return { ok: true, recipe: metaOf(rest) };
|
|
3458
|
+
}
|
|
3459
|
+
const steps = sanitizeRecipeSteps(applyHealProposal(current.steps, proposal));
|
|
3460
|
+
const next = {
|
|
3461
|
+
...rest,
|
|
3462
|
+
steps,
|
|
3463
|
+
updatedAt: now,
|
|
3464
|
+
rev: current.rev + 1,
|
|
3465
|
+
history: nextVersion(current, "heal", now)
|
|
3466
|
+
};
|
|
3467
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? next : recipe));
|
|
3468
|
+
return { ok: true, recipe: metaOf(next) };
|
|
3469
|
+
}
|
|
3470
|
+
/** Restore the newest past version; the safety net for a wrong repair. */
|
|
3471
|
+
rollback(input) {
|
|
3472
|
+
const now = input.now ?? Date.now();
|
|
3473
|
+
const recipes = this.read();
|
|
3474
|
+
const current = recipes.find((recipe) => recipe.id === input.id);
|
|
3475
|
+
if (current === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728" };
|
|
3476
|
+
const previous = current.history[current.history.length - 1];
|
|
3477
|
+
if (previous === void 0) return { ok: false, error: "\u6CA1\u6709\u53EF\u64A4\u9500\u7684\u7248\u672C" };
|
|
3478
|
+
const { pendingHeal: _stale, ...rest } = current;
|
|
3479
|
+
const next = {
|
|
3480
|
+
...rest,
|
|
3481
|
+
steps: previous.steps,
|
|
3482
|
+
variables: previous.variables,
|
|
3483
|
+
updatedAt: now,
|
|
3484
|
+
rev: current.rev + 1,
|
|
3485
|
+
history: current.history.slice(0, -1)
|
|
3486
|
+
};
|
|
3487
|
+
this.write(recipes.map((recipe) => recipe.id === current.id ? next : recipe));
|
|
3488
|
+
return { ok: true, recipe: metaOf(next) };
|
|
3489
|
+
}
|
|
3490
|
+
revisionTick = 0;
|
|
3491
|
+
/** Bumped on every write; the sidebar polls this instead of the whole list. */
|
|
3492
|
+
revision() {
|
|
3493
|
+
return this.revisionTick;
|
|
3494
|
+
}
|
|
3495
|
+
read() {
|
|
3496
|
+
if (this.filePath === null) return this.memory;
|
|
3497
|
+
try {
|
|
3498
|
+
const parsed = JSON.parse(readFileSync(this.filePath, "utf8"));
|
|
3499
|
+
if (parsed.version !== 1 && parsed.version !== FILE_VERSION || !Array.isArray(parsed.recipes)) return [];
|
|
3500
|
+
return parsed.recipes.filter((recipe) => {
|
|
3501
|
+
if (recipe === null || typeof recipe !== "object") return false;
|
|
3502
|
+
if (typeof recipe.id !== "string" || typeof recipe.name !== "string") return false;
|
|
3503
|
+
return typeof recipe.createdAt === "number" && typeof recipe.updatedAt === "number";
|
|
3504
|
+
}).map((recipe) => {
|
|
3505
|
+
const { pendingHeal: rawProposal, ...rest } = recipe;
|
|
3506
|
+
const proposal = sanitizeProposal(rawProposal);
|
|
3507
|
+
return {
|
|
3508
|
+
...rest,
|
|
3509
|
+
steps: sanitizeRecipeSteps(recipe.steps),
|
|
3510
|
+
variables: sanitizeVariables(recipe.variables),
|
|
3511
|
+
rev: Number.isFinite(recipe.rev) && recipe.rev > 0 ? recipe.rev : 1,
|
|
3512
|
+
history: sanitizeHistory(recipe.history),
|
|
3513
|
+
...proposal === void 0 ? {} : { pendingHeal: proposal }
|
|
3514
|
+
};
|
|
3515
|
+
}).filter((recipe) => recipe.steps.length > 0).slice(0, MAX_RECIPES);
|
|
3516
|
+
} catch {
|
|
3517
|
+
return [];
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3520
|
+
write(recipes) {
|
|
3521
|
+
this.revisionTick += 1;
|
|
3522
|
+
if (this.filePath === null) {
|
|
3523
|
+
this.memory = [...recipes];
|
|
3524
|
+
return;
|
|
3525
|
+
}
|
|
3526
|
+
try {
|
|
3527
|
+
mkdirSync2(dirname(this.filePath), { recursive: true });
|
|
3528
|
+
const shape = { version: FILE_VERSION, recipes };
|
|
3529
|
+
writeFileSync(this.filePath, `${JSON.stringify(shape, null, 2)}
|
|
3530
|
+
`, "utf8");
|
|
3531
|
+
} catch {
|
|
3532
|
+
}
|
|
3533
|
+
}
|
|
3534
|
+
};
|
|
3535
|
+
|
|
2913
3536
|
// src/host/browser-agent/tools.ts
|
|
2914
3537
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
2915
3538
|
var HUMAN_WAIT_TIMEOUT_MS = 15 * 6e4;
|
|
@@ -3022,6 +3645,26 @@ function driverOf(options) {
|
|
|
3022
3645
|
const value = options.driver;
|
|
3023
3646
|
return typeof value === "function" ? value() : value;
|
|
3024
3647
|
}
|
|
3648
|
+
async function captureRefEvidence(options, ref) {
|
|
3649
|
+
if (options.repair?.active() !== true) return null;
|
|
3650
|
+
const driver = driverOf(options);
|
|
3651
|
+
if (typeof driver.refInfo !== "function") return null;
|
|
3652
|
+
const info = await driver.refInfo(ref).catch(() => null);
|
|
3653
|
+
if (info === null) return null;
|
|
3654
|
+
return { role: info.role, name: info.name, selector: info.selector };
|
|
3655
|
+
}
|
|
3656
|
+
function recordRepair(options, evidence, kind, extra = {}) {
|
|
3657
|
+
if (options.repair?.active() !== true) return;
|
|
3658
|
+
options.repair.observe({
|
|
3659
|
+
kind,
|
|
3660
|
+
at: Date.now(),
|
|
3661
|
+
...evidence === null ? {} : evidence,
|
|
3662
|
+
...evidence === null || kind !== "fill" ? {} : { field: evidence.name },
|
|
3663
|
+
...extra.value === void 0 ? {} : { value: extra.value },
|
|
3664
|
+
...extra.url === void 0 ? {} : { url: extra.url },
|
|
3665
|
+
...extra.tabId === void 0 ? {} : { tabId: extra.tabId }
|
|
3666
|
+
});
|
|
3667
|
+
}
|
|
3025
3668
|
function pickEnum(value, allowed, field) {
|
|
3026
3669
|
if (value === void 0 || value === null || value === "") return void 0;
|
|
3027
3670
|
const text3 = String(value).trim().toLowerCase();
|
|
@@ -3445,6 +4088,15 @@ function registerBrowserTools(ctx, options) {
|
|
|
3445
4088
|
outcome: "ok"
|
|
3446
4089
|
});
|
|
3447
4090
|
options.onMutation?.({ sessionId, tool: "browser_navigate", url: result.url });
|
|
4091
|
+
if (options.repair?.active() === true) {
|
|
4092
|
+
const tabId = options.sessions.tabOf(sessionId);
|
|
4093
|
+
options.repair.observe({
|
|
4094
|
+
kind: "navigate",
|
|
4095
|
+
at: Date.now(),
|
|
4096
|
+
url: result.url,
|
|
4097
|
+
...tabId === void 0 ? {} : { tabId }
|
|
4098
|
+
});
|
|
4099
|
+
}
|
|
3448
4100
|
return [
|
|
3449
4101
|
newTab ? "opened_new_tab" : "navigated",
|
|
3450
4102
|
`url: ${result.url}`,
|
|
@@ -3490,6 +4142,7 @@ function registerBrowserTools(ctx, options) {
|
|
|
3490
4142
|
url = void 0;
|
|
3491
4143
|
}
|
|
3492
4144
|
requireFreshRef(sessionId, ref, "browser_click");
|
|
4145
|
+
const repairEvidence = await captureRefEvidence(options, ref);
|
|
3493
4146
|
if (options.sessions.isDeadClickRef(sessionId, ref)) {
|
|
3494
4147
|
throw new Error(
|
|
3495
4148
|
`click_blocked: ref ${ref} already failed with no navigation. Do NOT browser_click it again. browser_navigate to a content URL, or browser_snapshot and use a ref that lists href=.`
|
|
@@ -3528,6 +4181,10 @@ function registerBrowserTools(ctx, options) {
|
|
|
3528
4181
|
warning = navigated ? "Click was a no-op; host auto-navigated to a name-matched document link." : "Host tried name-matched auto-navigate after dead click but pathname still unchanged.";
|
|
3529
4182
|
}
|
|
3530
4183
|
}
|
|
4184
|
+
recordRepair(options, repairEvidence, "click", {
|
|
4185
|
+
url: result.url,
|
|
4186
|
+
tabId: options.sessions.tabOf(sessionId)
|
|
4187
|
+
});
|
|
3531
4188
|
options.sessions.clearLastRefs(sessionId);
|
|
3532
4189
|
if (!navigated) {
|
|
3533
4190
|
options.sessions.markDeadClickRef(sessionId, ref);
|
|
@@ -3611,6 +4268,7 @@ function registerBrowserTools(ctx, options) {
|
|
|
3611
4268
|
url = void 0;
|
|
3612
4269
|
}
|
|
3613
4270
|
requireFreshRef(sessionId, ref, "browser_type");
|
|
4271
|
+
const repairEvidence = await captureRefEvidence(options, ref);
|
|
3614
4272
|
await requireMutation(exec, "browser_type", `Type into ref ${ref}: ${maskTypeSummary(text3)}`, url, ref);
|
|
3615
4273
|
try {
|
|
3616
4274
|
const result = await runBridge(() => driverOf(options).type(ref, text3));
|
|
@@ -3624,6 +4282,11 @@ function registerBrowserTools(ctx, options) {
|
|
|
3624
4282
|
outcome: "ok"
|
|
3625
4283
|
});
|
|
3626
4284
|
options.onMutation?.({ sessionId, tool: "browser_type", url: result.url });
|
|
4285
|
+
recordRepair(options, repairEvidence, "fill", {
|
|
4286
|
+
value: text3,
|
|
4287
|
+
url: result.url,
|
|
4288
|
+
tabId: options.sessions.tabOf(sessionId)
|
|
4289
|
+
});
|
|
3627
4290
|
return `typed into ref ${ref}
|
|
3628
4291
|
url: ${result.url}
|
|
3629
4292
|
title: ${result.title}
|
|
@@ -4117,16 +4780,29 @@ Action: browser_extract or browser_snapshot next. Do not navigate away unless th
|
|
|
4117
4780
|
)));
|
|
4118
4781
|
disposers.push(ctx.tools.register(textTool(
|
|
4119
4782
|
"browser_demo_recipes",
|
|
4120
|
-
'List or
|
|
4783
|
+
'List, run, or repair a flow the user saved from a demonstration (right sidebar \u2192 \u6F14\u793A\u5F55\u5236 \u2192 \u4FDD\u5B58\u4E3A\u6D41\u7A0B). Use it when the user names a saved flow ("\u8DD1\u4E00\u4E0B\u300C\u5468\u62A5\u5BFC\u51FA\u300D\u6D41\u7A0B") or asks which flows exist. Running replays the recorded steps in the dedicated browser; it asks for approval once and stops at a masked secret or a control that is gone, exactly like browser_demonstrate replay. When it stops because a control changed, finish the remaining steps yourself, then call action=heal so the user can keep the fix.',
|
|
4121
4784
|
{
|
|
4122
4785
|
action: {
|
|
4123
4786
|
type: "string",
|
|
4124
4787
|
required: true,
|
|
4125
|
-
description: '"list" to show saved flows, "run" to replay one.'
|
|
4788
|
+
description: '"list" to show saved flows, "run" to replay one, "heal" to offer the fix you just worked out.'
|
|
4126
4789
|
},
|
|
4127
4790
|
name: {
|
|
4128
4791
|
type: "string",
|
|
4129
4792
|
description: "Flow name for action=run. Case-insensitive; a unique part of the name is enough."
|
|
4793
|
+
},
|
|
4794
|
+
id: {
|
|
4795
|
+
type: "string",
|
|
4796
|
+
description: "Flow id for action=heal; omit to use the flow that just stopped."
|
|
4797
|
+
},
|
|
4798
|
+
accept: {
|
|
4799
|
+
type: "boolean",
|
|
4800
|
+
description: "action=heal: apply the repair to the saved flow now (the user asked to remember it). Default false: offer it in the sidebar."
|
|
4801
|
+
},
|
|
4802
|
+
vars: {
|
|
4803
|
+
type: "object",
|
|
4804
|
+
additionalProperties: true,
|
|
4805
|
+
description: `Values for the flow's variables, e.g. {"from":"2026-11-01"}. Saved defaults are used for anything omitted.`
|
|
4130
4806
|
}
|
|
4131
4807
|
},
|
|
4132
4808
|
async (args, exec) => {
|
|
@@ -4137,16 +4813,62 @@ Action: browser_extract or browser_snapshot next. Do not navigate away unless th
|
|
|
4137
4813
|
const action = String(args.action ?? "").trim().toLowerCase();
|
|
4138
4814
|
const rows = store.list();
|
|
4139
4815
|
if (action === "list") {
|
|
4140
|
-
|
|
4816
|
+
options.repair?.commit();
|
|
4817
|
+
const afterCommit = store.list();
|
|
4818
|
+
if (afterCommit.length === 0) {
|
|
4141
4819
|
return "saved flows: 0\nAction: none yet \u2014 the user can record a demonstration and save it in the sidebar, then retry.";
|
|
4142
4820
|
}
|
|
4143
4821
|
return [
|
|
4144
|
-
`saved flows: ${
|
|
4145
|
-
...
|
|
4822
|
+
`saved flows: ${afterCommit.length}`,
|
|
4823
|
+
...afterCommit.map((row) => {
|
|
4824
|
+
const vars = row.variables.length === 0 ? "" : `; vars: ${row.variables.map((variable) => variable.name).join(", ")}`;
|
|
4825
|
+
const heal = row.pendingHeal === void 0 ? "" : `; repair waiting: step ${row.pendingHeal.failedAt}`;
|
|
4826
|
+
return `- ${row.name} (${row.steps} step(s)${vars}${heal})`;
|
|
4827
|
+
}),
|
|
4146
4828
|
'Action: run one with browser_demo_recipes { action: "run", name: "<name>" }.'
|
|
4147
4829
|
].join("\n");
|
|
4148
4830
|
}
|
|
4149
|
-
if (action
|
|
4831
|
+
if (action === "heal") {
|
|
4832
|
+
const committed = options.repair?.commit() ?? null;
|
|
4833
|
+
const id = String(args.id ?? committed?.recipeId ?? "").trim();
|
|
4834
|
+
if (id === "") {
|
|
4835
|
+
return "nothing to remember: no saved flow stopped recently, so there is no repair to store.";
|
|
4836
|
+
}
|
|
4837
|
+
if (args.accept !== true) {
|
|
4838
|
+
const row = store.list().find((candidate) => candidate.id === id);
|
|
4839
|
+
const proposal = row?.pendingHeal;
|
|
4840
|
+
if (committed === null && proposal === void 0) {
|
|
4841
|
+
return `nothing to remember: no repair for "${row?.name ?? id}" \u2014 the flow either ran clean or the difference was not a moved control.`;
|
|
4842
|
+
}
|
|
4843
|
+
options.sessions.record({
|
|
4844
|
+
sessionId: sessionIdOf(exec),
|
|
4845
|
+
tool: "browser_demo_recipe",
|
|
4846
|
+
summary: `Offered a repair for saved flow "${row?.name ?? id}"`,
|
|
4847
|
+
outcome: "ok"
|
|
4848
|
+
});
|
|
4849
|
+
return [
|
|
4850
|
+
`saved flow: ${row?.name ?? id} \u2014 repair offered`,
|
|
4851
|
+
...committed === null ? [] : committed.lines.map((line) => `- ${line}`),
|
|
4852
|
+
"Action: tell the user you worked around a changed control and that they can accept the fix in the sidebar (\u6F14\u793A\u5F55\u5236 \u2192 this flow). Do not claim the flow itself was updated."
|
|
4853
|
+
].join("\n");
|
|
4854
|
+
}
|
|
4855
|
+
const resolved = store.resolveHeal({ id, accept: true });
|
|
4856
|
+
if (!resolved.ok) {
|
|
4857
|
+
return `nothing to remember: ${resolved.error}`;
|
|
4858
|
+
}
|
|
4859
|
+
options.sessions.record({
|
|
4860
|
+
sessionId: sessionIdOf(exec),
|
|
4861
|
+
tool: "browser_demo_recipe",
|
|
4862
|
+
summary: `Applied a repair for saved flow "${resolved.recipe.name}"`,
|
|
4863
|
+
outcome: "ok"
|
|
4864
|
+
});
|
|
4865
|
+
return [
|
|
4866
|
+
`saved flow: ${resolved.recipe.name} \u2014 repair applied`,
|
|
4867
|
+
...committed === null ? [] : committed.lines.map((line) => `- ${line}`),
|
|
4868
|
+
"Action: the flow now uses the control you found. Tell the user it should work next time, and that the previous version can be restored in the sidebar."
|
|
4869
|
+
].join("\n");
|
|
4870
|
+
}
|
|
4871
|
+
if (action !== "run") throw new Error('action must be "list", "run" or "heal"');
|
|
4150
4872
|
const wanted = String(args.name ?? "").trim();
|
|
4151
4873
|
if (wanted === "") throw new Error("name is required for action=run");
|
|
4152
4874
|
const exact = rows.find((row) => row.name.toLowerCase() === wanted.toLowerCase());
|
|
@@ -4157,6 +4879,16 @@ Action: browser_extract or browser_snapshot next. Do not navigate away unless th
|
|
|
4157
4879
|
}
|
|
4158
4880
|
const recipe = store.get(meta.id);
|
|
4159
4881
|
if (recipe === void 0) throw new Error("The saved flow disappeared; list again.");
|
|
4882
|
+
options.repair?.commit();
|
|
4883
|
+
const provided = args.vars !== null && typeof args.vars === "object" ? Object.fromEntries(Object.entries(args.vars).filter(([, value]) => typeof value === "string").map(([key, value]) => [key, String(value)])) : {};
|
|
4884
|
+
const defaults = {};
|
|
4885
|
+
for (const variable of recipe.variables) defaults[variable.name] = variable.default;
|
|
4886
|
+
const values = { ...defaults, ...provided };
|
|
4887
|
+
const missing = missingRecipeVars(recipe, values);
|
|
4888
|
+
if (missing.length > 0) {
|
|
4889
|
+
throw new Error(`Flow "${recipe.name}" still needs: ${missing.join(", ")}. Pass vars, or save defaults in the sidebar.`);
|
|
4890
|
+
}
|
|
4891
|
+
const replaySteps = applyRecipeVars(recipe.steps, values);
|
|
4160
4892
|
const sessionId = sessionIdOf(exec);
|
|
4161
4893
|
await ensureConnected();
|
|
4162
4894
|
await ensureSessionTab(sessionId);
|
|
@@ -4167,8 +4899,24 @@ Action: browser_extract or browser_snapshot next. Do not navigate away unless th
|
|
|
4167
4899
|
`\u8FD0\u884C\u5DF2\u4FDD\u5B58\u6D41\u7A0B\u300C${recipe.name}\u300D\uFF08${recipe.steps.length} \u6B65\uFF09`,
|
|
4168
4900
|
firstUrl
|
|
4169
4901
|
);
|
|
4170
|
-
const outcome = await replayDemoSteps(driverOf(options),
|
|
4902
|
+
const outcome = await replayDemoSteps(driverOf(options), replaySteps);
|
|
4171
4903
|
const ranAll = outcome.status === "done";
|
|
4904
|
+
if (!ranAll) {
|
|
4905
|
+
const failedStep = recipe.steps[outcome.performed];
|
|
4906
|
+
const isSecret = /masked|secret field/i.test(outcome.reason) || failedStep !== void 0 && failedStep.kind === "fill" && failedStep.masked;
|
|
4907
|
+
if (!isSecret && failedStep !== void 0 && options.repair !== void 0) {
|
|
4908
|
+
const tabId = options.sessions.tabOf(sessionId);
|
|
4909
|
+
options.repair.begin({
|
|
4910
|
+
recipeId: recipe.id,
|
|
4911
|
+
recipeName: recipe.name,
|
|
4912
|
+
index: outcome.performed,
|
|
4913
|
+
step: failedStep,
|
|
4914
|
+
reason: outcome.reason,
|
|
4915
|
+
...tabId === void 0 ? {} : { tabId },
|
|
4916
|
+
...outcome.url === void 0 ? {} : { tabUrl: outcome.url }
|
|
4917
|
+
});
|
|
4918
|
+
}
|
|
4919
|
+
}
|
|
4172
4920
|
options.sessions.record({
|
|
4173
4921
|
sessionId,
|
|
4174
4922
|
tool: "browser_demo_recipes",
|
|
@@ -4192,7 +4940,7 @@ Action: browser_extract or browser_snapshot next. Do not navigate away unless th
|
|
|
4192
4940
|
...outcome.lines,
|
|
4193
4941
|
`stopped at step ${outcome.performed + 1}: ${outcome.reason}`,
|
|
4194
4942
|
...pending,
|
|
4195
|
-
|
|
4943
|
+
'Action: the replay stopped; the page is whatever the last ok step left behind. Continue with browser_snapshot + click/type, or browser_ask_user for a secret. Do not claim the flow finished. If a control had moved and you worked around it, finish the job and then call browser_demo_recipes { action: "heal" } so the user can keep that fix.'
|
|
4196
4944
|
].join("\n");
|
|
4197
4945
|
}
|
|
4198
4946
|
)));
|
|
@@ -4284,6 +5032,11 @@ function installBrowserAgent(ctx, options) {
|
|
|
4284
5032
|
const tabScope = new TabScopeStore();
|
|
4285
5033
|
const recipes = options.recipes;
|
|
4286
5034
|
let lastDemoSteps = [];
|
|
5035
|
+
const repairTracker = new DemoRepairTracker();
|
|
5036
|
+
const repairHooks = createRepairHooks({
|
|
5037
|
+
tracker: repairTracker,
|
|
5038
|
+
...recipes === void 0 ? {} : { store: recipes }
|
|
5039
|
+
});
|
|
4287
5040
|
let availabilityCache = null;
|
|
4288
5041
|
const browserAvailability = () => {
|
|
4289
5042
|
if (availabilityCache !== null) return availabilityCache;
|
|
@@ -4407,6 +5160,7 @@ function installBrowserAgent(ctx, options) {
|
|
|
4407
5160
|
lastDemoSteps = steps;
|
|
4408
5161
|
},
|
|
4409
5162
|
...recipes === void 0 ? {} : { recipes },
|
|
5163
|
+
repair: repairHooks,
|
|
4410
5164
|
diagnosis: diagnoseNow,
|
|
4411
5165
|
// Gate on settings only — `active` flips false during driver align and
|
|
4412
5166
|
// must not make browser_* look "disabled" mid-turn.
|
|
@@ -4622,9 +5376,13 @@ function installBrowserAgent(ctx, options) {
|
|
|
4622
5376
|
lastDemonstrationSteps() {
|
|
4623
5377
|
return lastDemoSteps.length;
|
|
4624
5378
|
},
|
|
5379
|
+
recipesRevision() {
|
|
5380
|
+
return recipes?.revision() ?? 0;
|
|
5381
|
+
},
|
|
4625
5382
|
async demoRecipes(input) {
|
|
4626
5383
|
if (!options.enabled()) return { ok: false, error: "Browser agent disabled" };
|
|
4627
5384
|
if (recipes === void 0) return { ok: false, error: "recipe store unavailable" };
|
|
5385
|
+
repairHooks.commit();
|
|
4628
5386
|
if (input.action === "list") {
|
|
4629
5387
|
return { ok: true, recipes: recipes.list() };
|
|
4630
5388
|
}
|
|
@@ -4639,38 +5397,115 @@ function installBrowserAgent(ctx, options) {
|
|
|
4639
5397
|
});
|
|
4640
5398
|
return removed ? { ok: true, recipes: recipes.list() } : { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728", recipes: recipes.list() };
|
|
4641
5399
|
}
|
|
4642
|
-
if (input.action === "
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
5400
|
+
if (input.action === "steps") {
|
|
5401
|
+
const recipe2 = recipes.get(input.id ?? "");
|
|
5402
|
+
if (recipe2 === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728", recipes: recipes.list() };
|
|
5403
|
+
return {
|
|
5404
|
+
ok: true,
|
|
5405
|
+
recipes: recipes.list(),
|
|
5406
|
+
lines: recipe2.steps.map((step, index) => `${index + 1}. ${describeDemoStep(step)}`),
|
|
5407
|
+
steps: recipe2.steps.map((step, index) => ({
|
|
5408
|
+
index,
|
|
5409
|
+
kind: step.kind,
|
|
5410
|
+
label: describeDemoStep(step),
|
|
5411
|
+
canParameterize: step.kind === "fill" && !step.masked && !/\$\{[A-Za-z_][A-Za-z0-9_]*\}/.test(step.value)
|
|
5412
|
+
}))
|
|
5413
|
+
};
|
|
5414
|
+
}
|
|
5415
|
+
if (input.action === "parameterize") {
|
|
5416
|
+
const result = recipes.parameterize({
|
|
5417
|
+
id: input.id ?? "",
|
|
5418
|
+
index: input.index ?? -1,
|
|
5419
|
+
name: input.name ?? ""
|
|
5420
|
+
});
|
|
5421
|
+
if (!result.ok) return { ok: false, error: result.error, recipes: recipes.list() };
|
|
5422
|
+
sessions.record({
|
|
5423
|
+
sessionId: "sidebar",
|
|
5424
|
+
tool: "browser_demo_recipe",
|
|
5425
|
+
summary: `Parameterized demonstration recipe "${result.recipe.name}"`,
|
|
5426
|
+
outcome: "ok"
|
|
5427
|
+
});
|
|
5428
|
+
return { ok: true, recipe: result.recipe, recipes: recipes.list() };
|
|
5429
|
+
}
|
|
5430
|
+
if (input.action === "rename") {
|
|
5431
|
+
const renamed = recipes.rename({ id: input.id ?? "", name: input.name ?? "" });
|
|
5432
|
+
if (!renamed.ok) return { ok: false, error: renamed.error, recipes: recipes.list() };
|
|
4651
5433
|
sessions.record({
|
|
4652
5434
|
sessionId: "sidebar",
|
|
4653
5435
|
tool: "browser_demo_recipe",
|
|
4654
|
-
summary: `
|
|
5436
|
+
summary: `Renamed demonstration recipe to "${renamed.recipe.name}"`,
|
|
4655
5437
|
outcome: "ok"
|
|
4656
5438
|
});
|
|
4657
|
-
return { ok: true, recipe:
|
|
5439
|
+
return { ok: true, recipe: renamed.recipe, recipes: recipes.list() };
|
|
5440
|
+
}
|
|
5441
|
+
if (input.action === "heal") {
|
|
5442
|
+
const resolved = recipes.resolveHeal({ id: input.id ?? "", accept: input.accept === true });
|
|
5443
|
+
if (!resolved.ok) return { ok: false, error: resolved.error, recipes: recipes.list() };
|
|
5444
|
+
sessions.record({
|
|
5445
|
+
sessionId: "sidebar",
|
|
5446
|
+
tool: "browser_demo_recipe",
|
|
5447
|
+
summary: `${input.accept === true ? "Applied" : "Dismissed"} a repair for "${resolved.recipe.name}"`,
|
|
5448
|
+
outcome: "ok"
|
|
5449
|
+
});
|
|
5450
|
+
return {
|
|
5451
|
+
ok: true,
|
|
5452
|
+
healed: input.accept === true,
|
|
5453
|
+
recipe: resolved.recipe,
|
|
5454
|
+
recipes: recipes.list()
|
|
5455
|
+
};
|
|
5456
|
+
}
|
|
5457
|
+
if (input.action === "rollback") {
|
|
5458
|
+
const restored = recipes.rollback({ id: input.id ?? "" });
|
|
5459
|
+
if (!restored.ok) return { ok: false, error: restored.error, recipes: recipes.list() };
|
|
5460
|
+
sessions.record({
|
|
5461
|
+
sessionId: "sidebar",
|
|
5462
|
+
tool: "browser_demo_recipe",
|
|
5463
|
+
summary: `Restored the previous version of "${restored.recipe.name}"`,
|
|
5464
|
+
outcome: "ok"
|
|
5465
|
+
});
|
|
5466
|
+
return { ok: true, recipe: restored.recipe, recipes: recipes.list() };
|
|
4658
5467
|
}
|
|
4659
5468
|
const recipe = recipes.get(input.id ?? "");
|
|
4660
5469
|
if (recipe === void 0) return { ok: false, error: "\u6D41\u7A0B\u4E0D\u5B58\u5728", recipes: recipes.list() };
|
|
5470
|
+
repairHooks.commit();
|
|
5471
|
+
const defaults = {};
|
|
5472
|
+
for (const variable of recipe.variables) defaults[variable.name] = variable.default;
|
|
5473
|
+
const values = { ...defaults, ...input.vars ?? {} };
|
|
5474
|
+
const missing = missingRecipeVars(recipe, values);
|
|
5475
|
+
if (missing.length > 0) {
|
|
5476
|
+
return {
|
|
5477
|
+
ok: false,
|
|
5478
|
+
error: `\u7F3A\u5C11\u53D8\u91CF\uFF1A${missing.join("\u3001")}`,
|
|
5479
|
+
recipes: recipes.list()
|
|
5480
|
+
};
|
|
5481
|
+
}
|
|
5482
|
+
const steps = applyRecipeVars(recipe.steps, values);
|
|
4661
5483
|
const driver = activeDriver;
|
|
4662
5484
|
if (driver === null) return { ok: false, error: "Browser agent is not running", recipes: recipes.list() };
|
|
4663
5485
|
const aligned = await alignToPreference().catch(() => ({ ok: true, error: void 0 }));
|
|
4664
5486
|
if (!aligned.ok) return { ok: false, error: aligned.error ?? "Browser agent failed to start", recipes: recipes.list() };
|
|
4665
|
-
const outcome = await replayDemoSteps(driver,
|
|
5487
|
+
const outcome = await replayDemoSteps(driver, steps).catch((error) => ({
|
|
4666
5488
|
status: "blocked",
|
|
4667
5489
|
performed: 0,
|
|
4668
|
-
total:
|
|
5490
|
+
total: steps.length,
|
|
4669
5491
|
lines: [],
|
|
4670
5492
|
reason: error instanceof Error ? error.message : String(error),
|
|
4671
|
-
pending: [...
|
|
5493
|
+
pending: [...steps]
|
|
4672
5494
|
}));
|
|
4673
5495
|
const ranAll = outcome.status === "done";
|
|
5496
|
+
if (!ranAll) {
|
|
5497
|
+
const failedStep = recipe.steps[outcome.performed];
|
|
5498
|
+
const isSecret = /masked|secret field/i.test(outcome.reason) || failedStep !== void 0 && failedStep.kind === "fill" && failedStep.masked;
|
|
5499
|
+
if (!isSecret && failedStep !== void 0) {
|
|
5500
|
+
repairTracker.begin({
|
|
5501
|
+
recipeId: recipe.id,
|
|
5502
|
+
recipeName: recipe.name,
|
|
5503
|
+
index: outcome.performed,
|
|
5504
|
+
step: failedStep,
|
|
5505
|
+
reason: outcome.reason
|
|
5506
|
+
});
|
|
5507
|
+
}
|
|
5508
|
+
}
|
|
4674
5509
|
sessions.record({
|
|
4675
5510
|
sessionId: "sidebar",
|
|
4676
5511
|
tool: "browser_demo_recipe",
|
|
@@ -4837,106 +5672,6 @@ function installBrowserAgent(ctx, options) {
|
|
|
4837
5672
|
};
|
|
4838
5673
|
}
|
|
4839
5674
|
|
|
4840
|
-
// src/host/browser-agent/demo-recipes.ts
|
|
4841
|
-
import { mkdirSync as mkdirSync2, readFileSync, writeFileSync } from "node:fs";
|
|
4842
|
-
import { dirname } from "node:path";
|
|
4843
|
-
var MAX_RECIPES = 20;
|
|
4844
|
-
var MAX_RECIPE_NAME = 60;
|
|
4845
|
-
var MAX_RECIPE_STEPS = 200;
|
|
4846
|
-
var FILE_VERSION = 1;
|
|
4847
|
-
function metaOf(recipe) {
|
|
4848
|
-
return {
|
|
4849
|
-
id: recipe.id,
|
|
4850
|
-
name: recipe.name,
|
|
4851
|
-
createdAt: recipe.createdAt,
|
|
4852
|
-
updatedAt: recipe.updatedAt,
|
|
4853
|
-
steps: recipe.steps.length
|
|
4854
|
-
};
|
|
4855
|
-
}
|
|
4856
|
-
function isStep(value) {
|
|
4857
|
-
if (value === null || typeof value !== "object") return false;
|
|
4858
|
-
const kind = value.kind;
|
|
4859
|
-
return kind === "navigate" || kind === "click" || kind === "fill" || kind === "press";
|
|
4860
|
-
}
|
|
4861
|
-
function sanitizeRecipeSteps(raw) {
|
|
4862
|
-
if (!Array.isArray(raw)) return [];
|
|
4863
|
-
const steps = raw.filter(isStep).slice(0, MAX_RECIPE_STEPS);
|
|
4864
|
-
return sanitizeDemoSteps(steps).map((step) => {
|
|
4865
|
-
const { tabId: _volatileTabId, ...rest } = step;
|
|
4866
|
-
return rest;
|
|
4867
|
-
});
|
|
4868
|
-
}
|
|
4869
|
-
var DemoRecipeStore = class {
|
|
4870
|
-
constructor(filePath = null) {
|
|
4871
|
-
this.filePath = filePath;
|
|
4872
|
-
}
|
|
4873
|
-
filePath;
|
|
4874
|
-
memory = [];
|
|
4875
|
-
list() {
|
|
4876
|
-
return this.read().map(metaOf).sort((a, b) => b.updatedAt - a.updatedAt);
|
|
4877
|
-
}
|
|
4878
|
-
get(id) {
|
|
4879
|
-
return this.read().find((recipe) => recipe.id === id);
|
|
4880
|
-
}
|
|
4881
|
-
/** Save under `name`; the same name (case-insensitive) updates in place. */
|
|
4882
|
-
save(input) {
|
|
4883
|
-
const name2 = input.name.replace(/\s+/gu, " ").trim();
|
|
4884
|
-
if (name2 === "") return { ok: false, error: "\u6D41\u7A0B\u540D\u4E0D\u80FD\u4E3A\u7A7A" };
|
|
4885
|
-
if (name2.length > MAX_RECIPE_NAME) return { ok: false, error: `\u6D41\u7A0B\u540D\u6700\u957F ${MAX_RECIPE_NAME} \u4E2A\u5B57\u7B26` };
|
|
4886
|
-
const steps = sanitizeRecipeSteps(input.steps);
|
|
4887
|
-
if (steps.length === 0) return { ok: false, error: "\u8FD9\u6B21\u5F55\u5236\u6CA1\u6709\u53EF\u4FDD\u5B58\u7684\u6B65\u9AA4" };
|
|
4888
|
-
const now = input.now ?? Date.now();
|
|
4889
|
-
const recipes = this.read();
|
|
4890
|
-
const existing = recipes.find((recipe) => recipe.name.toLowerCase() === name2.toLowerCase());
|
|
4891
|
-
let saved;
|
|
4892
|
-
if (existing !== void 0) {
|
|
4893
|
-
saved = { ...existing, name: name2, steps, updatedAt: now };
|
|
4894
|
-
} else {
|
|
4895
|
-
if (recipes.length >= MAX_RECIPES) {
|
|
4896
|
-
return { ok: false, error: `\u6700\u591A\u4FDD\u5B58 ${MAX_RECIPES} \u4E2A\u6D41\u7A0B\uFF0C\u5148\u5220\u6389\u4E00\u4E2A` };
|
|
4897
|
-
}
|
|
4898
|
-
saved = { id: `r-${now.toString(36)}-${Math.random().toString(36).slice(2, 7)}`, name: name2, createdAt: now, updatedAt: now, steps };
|
|
4899
|
-
}
|
|
4900
|
-
const next = existing === void 0 ? [...recipes, saved] : recipes.map((recipe) => recipe.id === saved.id ? saved : recipe);
|
|
4901
|
-
this.write(next);
|
|
4902
|
-
return { ok: true, recipe: metaOf(saved) };
|
|
4903
|
-
}
|
|
4904
|
-
remove(id) {
|
|
4905
|
-
const recipes = this.read();
|
|
4906
|
-
const next = recipes.filter((recipe) => recipe.id !== id);
|
|
4907
|
-
if (next.length === recipes.length) return false;
|
|
4908
|
-
this.write(next);
|
|
4909
|
-
return true;
|
|
4910
|
-
}
|
|
4911
|
-
read() {
|
|
4912
|
-
if (this.filePath === null) return this.memory;
|
|
4913
|
-
try {
|
|
4914
|
-
const parsed = JSON.parse(readFileSync(this.filePath, "utf8"));
|
|
4915
|
-
if (parsed.version !== FILE_VERSION || !Array.isArray(parsed.recipes)) return [];
|
|
4916
|
-
return parsed.recipes.filter((recipe) => {
|
|
4917
|
-
if (recipe === null || typeof recipe !== "object") return false;
|
|
4918
|
-
if (typeof recipe.id !== "string" || typeof recipe.name !== "string") return false;
|
|
4919
|
-
return typeof recipe.createdAt === "number" && typeof recipe.updatedAt === "number";
|
|
4920
|
-
}).map((recipe) => ({ ...recipe, steps: sanitizeRecipeSteps(recipe.steps) })).filter((recipe) => recipe.steps.length > 0).slice(0, MAX_RECIPES);
|
|
4921
|
-
} catch {
|
|
4922
|
-
return [];
|
|
4923
|
-
}
|
|
4924
|
-
}
|
|
4925
|
-
write(recipes) {
|
|
4926
|
-
if (this.filePath === null) {
|
|
4927
|
-
this.memory = [...recipes];
|
|
4928
|
-
return;
|
|
4929
|
-
}
|
|
4930
|
-
try {
|
|
4931
|
-
mkdirSync2(dirname(this.filePath), { recursive: true });
|
|
4932
|
-
const shape = { version: FILE_VERSION, recipes };
|
|
4933
|
-
writeFileSync(this.filePath, `${JSON.stringify(shape, null, 2)}
|
|
4934
|
-
`, "utf8");
|
|
4935
|
-
} catch {
|
|
4936
|
-
}
|
|
4937
|
-
}
|
|
4938
|
-
};
|
|
4939
|
-
|
|
4940
5675
|
// src/errors.ts
|
|
4941
5676
|
var MESSAGES = {
|
|
4942
5677
|
MODEL_UNAVAILABLE: "No model is available for Agent Enhance",
|
|
@@ -16953,7 +17688,9 @@ var browserAgentSnapshotSchema = z4.strictObject({
|
|
|
16953
17688
|
enabled: z4.boolean(),
|
|
16954
17689
|
recording: z4.boolean(),
|
|
16955
17690
|
/** Steps from the last finished recording, whoever stopped it. */
|
|
16956
|
-
lastSteps: z4.number().int()
|
|
17691
|
+
lastSteps: z4.number().int(),
|
|
17692
|
+
/** Bumped when a saved flow changes; the panel re-reads the list on change. */
|
|
17693
|
+
recipesVersion: z4.number().int()
|
|
16957
17694
|
}),
|
|
16958
17695
|
tabConflict: browserAgentTabConflictSchema.nullable(),
|
|
16959
17696
|
needsHuman: browserAgentNeedsHumanSchema.nullable(),
|
|
@@ -17003,18 +17740,56 @@ var browserAgentThumbnailResultCodec = codec(
|
|
|
17003
17740
|
"dsh-agent-enhancer#BrowserAgentThumbnailResult",
|
|
17004
17741
|
browserAgentThumbnailResultSchema
|
|
17005
17742
|
);
|
|
17743
|
+
var demoRecipeVariableSchema = z4.strictObject({
|
|
17744
|
+
name: z4.string().min(1),
|
|
17745
|
+
default: z4.string()
|
|
17746
|
+
});
|
|
17747
|
+
var demoRecipeHealSchema = z4.strictObject({
|
|
17748
|
+
id: z4.string().min(1),
|
|
17749
|
+
/** 1-based step the replay stopped at. */
|
|
17750
|
+
failedAt: z4.number().int().min(1),
|
|
17751
|
+
kind: z4.enum(["navigate", "click", "fill", "press"]),
|
|
17752
|
+
/** The control's name (URL for navigate, key for press). */
|
|
17753
|
+
label: z4.string(),
|
|
17754
|
+
/** Steps the agent had to add before it (a cookie banner…). */
|
|
17755
|
+
inserted: z4.number().int().min(0),
|
|
17756
|
+
reason: z4.string()
|
|
17757
|
+
});
|
|
17006
17758
|
var demoRecipeMetaSchema = z4.strictObject({
|
|
17007
17759
|
id: z4.string().min(1),
|
|
17008
17760
|
name: z4.string().min(1),
|
|
17009
17761
|
createdAt: z4.number(),
|
|
17010
17762
|
updatedAt: z4.number(),
|
|
17011
|
-
steps: z4.number().int()
|
|
17763
|
+
steps: z4.number().int(),
|
|
17764
|
+
variables: z4.array(demoRecipeVariableSchema),
|
|
17765
|
+
/** Content revision; grows with every accepted change. */
|
|
17766
|
+
rev: z4.number().int(),
|
|
17767
|
+
/** How many previous versions can be restored. */
|
|
17768
|
+
history: z4.number().int(),
|
|
17769
|
+
/** A repair worked out by the agent, waiting for the user. */
|
|
17770
|
+
pendingHeal: demoRecipeHealSchema.optional()
|
|
17771
|
+
});
|
|
17772
|
+
var demoRecipeStepSchema = z4.strictObject({
|
|
17773
|
+
index: z4.number().int(),
|
|
17774
|
+
kind: z4.enum(["navigate", "click", "fill", "press"]),
|
|
17775
|
+
label: z4.string(),
|
|
17776
|
+
/** A fill step that can become a `${name}` placeholder. */
|
|
17777
|
+
canParameterize: z4.boolean()
|
|
17012
17778
|
});
|
|
17013
17779
|
var browserAgentDemoRecipesRequestSchema = z4.discriminatedUnion("action", [
|
|
17014
17780
|
z4.strictObject({ action: z4.literal("list") }),
|
|
17015
17781
|
z4.strictObject({ action: z4.literal("save"), name: z4.string() }),
|
|
17016
|
-
z4.strictObject({
|
|
17017
|
-
|
|
17782
|
+
z4.strictObject({
|
|
17783
|
+
action: z4.literal("run"),
|
|
17784
|
+
id: z4.string().min(1),
|
|
17785
|
+
vars: z4.record(z4.string(), z4.string()).optional()
|
|
17786
|
+
}),
|
|
17787
|
+
z4.strictObject({ action: z4.literal("delete"), id: z4.string().min(1) }),
|
|
17788
|
+
z4.strictObject({ action: z4.literal("steps"), id: z4.string().min(1) }),
|
|
17789
|
+
z4.strictObject({ action: z4.literal("rename"), id: z4.string().min(1), name: z4.string() }),
|
|
17790
|
+
z4.strictObject({ action: z4.literal("parameterize"), id: z4.string().min(1), index: z4.number().int().min(0), name: z4.string() }),
|
|
17791
|
+
z4.strictObject({ action: z4.literal("heal"), id: z4.string().min(1), accept: z4.boolean() }),
|
|
17792
|
+
z4.strictObject({ action: z4.literal("rollback"), id: z4.string().min(1) })
|
|
17018
17793
|
]);
|
|
17019
17794
|
var browserAgentDemoRecipesRequestCodec = codec(
|
|
17020
17795
|
"dsh-agent-enhancer#BrowserAgentDemoRecipesRequest",
|
|
@@ -17026,6 +17801,10 @@ var browserAgentDemoRecipesResultSchema = z4.strictObject({
|
|
|
17026
17801
|
recipe: demoRecipeMetaSchema.optional(),
|
|
17027
17802
|
/** Replay lines when running a recipe (`1. ok …`). */
|
|
17028
17803
|
lines: z4.array(z4.string()).optional(),
|
|
17804
|
+
/** Step metadata for action=steps (index + what the row can do). */
|
|
17805
|
+
steps: z4.array(demoRecipeStepSchema).optional(),
|
|
17806
|
+
/** True when a repair was applied (action=heal with accept). */
|
|
17807
|
+
healed: z4.boolean().optional(),
|
|
17029
17808
|
/** 1-based step where a replay stopped. */
|
|
17030
17809
|
stoppedAt: z4.number().int().optional(),
|
|
17031
17810
|
reason: z4.string().optional(),
|
|
@@ -18786,7 +19565,8 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
18786
19565
|
demo: {
|
|
18787
19566
|
enabled: settings.demonstrate,
|
|
18788
19567
|
recording: this.browserAgent?.demoActive() === true,
|
|
18789
|
-
lastSteps: this.browserAgent?.lastDemonstrationSteps() ?? 0
|
|
19568
|
+
lastSteps: this.browserAgent?.lastDemonstrationSteps() ?? 0,
|
|
19569
|
+
recipesVersion: this.browserAgent?.recipesRevision() ?? 0
|
|
18790
19570
|
},
|
|
18791
19571
|
diagnosis: diag ?? {
|
|
18792
19572
|
code: settings.enabled ? "idle" : "disabled",
|
|
@@ -19508,13 +20288,38 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
19508
20288
|
const result = await runtime.demoRecipes({
|
|
19509
20289
|
action: request.action,
|
|
19510
20290
|
..."name" in request ? { name: request.name } : {},
|
|
19511
|
-
..."id" in request ? { id: request.id } : {}
|
|
20291
|
+
..."id" in request ? { id: request.id } : {},
|
|
20292
|
+
..."index" in request ? { index: request.index } : {},
|
|
20293
|
+
..."accept" in request && request.accept !== void 0 ? { accept: request.accept } : {},
|
|
20294
|
+
..."vars" in request && request.vars !== void 0 ? { vars: request.vars } : {}
|
|
20295
|
+
});
|
|
20296
|
+
const meta = (recipe) => ({
|
|
20297
|
+
id: recipe.id,
|
|
20298
|
+
name: recipe.name,
|
|
20299
|
+
createdAt: recipe.createdAt,
|
|
20300
|
+
updatedAt: recipe.updatedAt,
|
|
20301
|
+
steps: recipe.steps,
|
|
20302
|
+
variables: recipe.variables.map((variable) => ({ ...variable })),
|
|
20303
|
+
rev: recipe.rev,
|
|
20304
|
+
history: recipe.history,
|
|
20305
|
+
...recipe.pendingHeal === void 0 ? {} : {
|
|
20306
|
+
pendingHeal: {
|
|
20307
|
+
id: recipe.pendingHeal.id,
|
|
20308
|
+
failedAt: recipe.pendingHeal.failedAt,
|
|
20309
|
+
kind: recipe.pendingHeal.replacement.kind,
|
|
20310
|
+
label: recipe.pendingHeal.replacement.kind === "navigate" ? recipe.pendingHeal.replacement.url : recipe.pendingHeal.replacement.kind === "press" ? recipe.pendingHeal.replacement.key : recipe.pendingHeal.replacement.name,
|
|
20311
|
+
inserted: recipe.pendingHeal.inserted.length,
|
|
20312
|
+
reason: recipe.pendingHeal.reason
|
|
20313
|
+
}
|
|
20314
|
+
}
|
|
19512
20315
|
});
|
|
19513
20316
|
return {
|
|
19514
20317
|
ok: result.ok,
|
|
19515
|
-
...result.recipes === void 0 ? {} : { recipes: result.recipes.map(
|
|
19516
|
-
...result.recipe === void 0 ? {} : { recipe:
|
|
20318
|
+
...result.recipes === void 0 ? {} : { recipes: result.recipes.map(meta) },
|
|
20319
|
+
...result.recipe === void 0 ? {} : { recipe: meta(result.recipe) },
|
|
19517
20320
|
...result.lines === void 0 ? {} : { lines: [...result.lines] },
|
|
20321
|
+
...result.healed === void 0 ? {} : { healed: result.healed },
|
|
20322
|
+
...result.steps === void 0 ? {} : { steps: result.steps.map((step) => ({ ...step })) },
|
|
19518
20323
|
...result.stoppedAt === void 0 ? {} : { stoppedAt: result.stoppedAt },
|
|
19519
20324
|
...result.reason === void 0 ? {} : { reason: result.reason },
|
|
19520
20325
|
...result.error === void 0 ? {} : { error: result.error }
|