fix(browser): soften aggressive retry hint and add anti-hallucination prompts (#910)
This commit is contained in:
@@ -789,6 +789,42 @@ function patchBundledRuntime(outputDir) {
|
||||
if (ptyCount > 0) {
|
||||
echo` 🩹 Patched ${ptyCount} bundled PTY site(s)`;
|
||||
}
|
||||
|
||||
// --- Browser tool hint patch ---
|
||||
// OpenClaw's BROWSER_TOOL_MODEL_HINT tells the model "Do NOT retry the
|
||||
// browser tool — it will keep failing" after ANY error, causing the model
|
||||
// to permanently refuse browser usage even on transient failures.
|
||||
// Replace with a gentler hint that allows retries on transient errors.
|
||||
const ORIGINAL_HINT =
|
||||
'Do NOT retry the browser tool \u2014 it will keep failing. Use an alternative approach or inform the user that the browser is currently unavailable.';
|
||||
const PATCHED_HINT =
|
||||
'If this was a transient error (timeout, network), you may retry once. If the same error persists after retry, try an alternative approach and let the user know.';
|
||||
const ORIGINAL_SHORT = 'Do NOT retry the browser tool.';
|
||||
const PATCHED_SHORT = 'You may retry once if this was a transient error.';
|
||||
|
||||
const distDir = path.join(outputDir, 'dist');
|
||||
let hintCount = 0;
|
||||
if (fs.existsSync(distDir)) {
|
||||
for (const file of fs.readdirSync(distDir)) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
const filePath = path.join(distDir, file);
|
||||
try {
|
||||
const content = fs.readFileSync(filePath, 'utf8');
|
||||
if (!content.includes(ORIGINAL_HINT) && !content.includes(ORIGINAL_SHORT)) continue;
|
||||
const patched = content
|
||||
.replaceAll(ORIGINAL_HINT, PATCHED_HINT)
|
||||
.replaceAll(ORIGINAL_SHORT, PATCHED_SHORT);
|
||||
if (patched !== content) {
|
||||
fs.writeFileSync(filePath, patched, 'utf8');
|
||||
hintCount++;
|
||||
}
|
||||
} catch { /* skip on error */ }
|
||||
}
|
||||
}
|
||||
|
||||
if (hintCount > 0) {
|
||||
echo` 🩹 Patched ${hintCount} browser tool hint(s) to allow transient error retry`;
|
||||
}
|
||||
}
|
||||
|
||||
patchBrokenModules(outputNodeModules);
|
||||
|
||||
53
scripts/patch-browser-hint.mjs
Normal file
53
scripts/patch-browser-hint.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Patch OpenClaw's BROWSER_TOOL_MODEL_HINT to allow retries on transient errors.
|
||||
*
|
||||
* The original hint ("Do NOT retry the browser tool — it will keep failing")
|
||||
* causes models to permanently refuse browser usage after a single transient error.
|
||||
*
|
||||
* This runs as postinstall to patch node_modules for dev mode.
|
||||
* Production builds are separately patched in bundle-openclaw.mjs.
|
||||
*/
|
||||
|
||||
import { readFileSync, writeFileSync, readdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
const REPLACEMENTS = [
|
||||
[
|
||||
'Do NOT retry the browser tool \u2014 it will keep failing. Use an alternative approach or inform the user that the browser is currently unavailable.',
|
||||
'If this was a transient error (timeout, network), you may retry once. If the same error persists after retry, try an alternative approach and let the user know.',
|
||||
],
|
||||
[
|
||||
'Do NOT retry the browser tool.',
|
||||
'You may retry once if this was a transient error.',
|
||||
],
|
||||
];
|
||||
|
||||
const distDir = join(process.cwd(), 'node_modules', 'openclaw', 'dist');
|
||||
|
||||
let patchedCount = 0;
|
||||
try {
|
||||
for (const file of readdirSync(distDir)) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
const filePath = join(distDir, file);
|
||||
let content = readFileSync(filePath, 'utf-8');
|
||||
let changed = false;
|
||||
for (const [search, replace] of REPLACEMENTS) {
|
||||
if (content.includes(search)) {
|
||||
content = content.replaceAll(search, replace);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
writeFileSync(filePath, content, 'utf-8');
|
||||
console.log(`[patch-browser-hint] Patched: ${file}`);
|
||||
patchedCount++;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// openclaw not installed yet or dist not found — skip silently
|
||||
}
|
||||
|
||||
if (patchedCount > 0) {
|
||||
console.log(`[patch-browser-hint] Done. Patched ${patchedCount} file(s).`);
|
||||
}
|
||||
Reference in New Issue
Block a user