feat: prepare Zhinian desktop pilot
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Patch OpenClaw's BROWSER_TOOL_MODEL_HINT to allow retries on transient errors.
|
||||
* Patch OpenClaw's dev-time runtime files.
|
||||
*
|
||||
* 1. BROWSER_TOOL_MODEL_HINT: allow retries on transient errors.
|
||||
* 2. Bundled runtime deps materialization: accept already-satisfied local
|
||||
* runtime packages even when OpenClaw expands its generated manifest after
|
||||
* Gateway boot. Production builds are separately patched in
|
||||
* bundle-openclaw.mjs.
|
||||
*
|
||||
* 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';
|
||||
@@ -26,6 +29,8 @@ const REPLACEMENTS = [
|
||||
const distDir = join(process.cwd(), 'node_modules', 'openclaw', 'dist');
|
||||
|
||||
let patchedCount = 0;
|
||||
let runtimeDepsPatchedCount = 0;
|
||||
let desktopFastChatPatchedCount = 0;
|
||||
try {
|
||||
for (const file of readdirSync(distDir)) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
@@ -48,6 +53,84 @@ try {
|
||||
// openclaw not installed yet or dist not found — skip silently
|
||||
}
|
||||
|
||||
try {
|
||||
for (const file of readdirSync(distDir)) {
|
||||
if (!file.endsWith('.js')) continue;
|
||||
const filePath = join(distDir, file);
|
||||
let content = readFileSync(filePath, 'utf-8');
|
||||
const before = content;
|
||||
|
||||
content = content.replace(
|
||||
'const contextInjectionMode = resolveContextInjectionMode(params.config);\n\t\tconst isRawModelRun = params.modelRun === true || params.promptMode === "none";',
|
||||
'const contextInjectionMode = resolveContextInjectionMode(params.config);\n\t\tconst yinianDesktopFastModelRun = (!params.messageChannel && !params.messageProvider || params.messageChannel === "webchat" || params.messageProvider === "webchat") && (params.config?.tools?.profile === "messaging" || params.config?.tools?.profile === "minimal");\n\t\tconst isRawModelRun = params.modelRun === true || params.promptMode === "none" || yinianDesktopFastModelRun;',
|
||||
);
|
||||
content = content.replace(
|
||||
'const builtAppendPrompt = resolveSystemPromptOverride({',
|
||||
'const builtAppendPrompt = isRawModelRun ? "" : resolveSystemPromptOverride({',
|
||||
);
|
||||
content = content.replace(
|
||||
'const toolsRaw = params.disableTools || isRawModelRun ? [] : applyEmbeddedAttemptToolsAllow(createOpenClawCodingTools({',
|
||||
'const yinianDesktopFastModelRun = (!params.messageChannel && !params.messageProvider || params.messageChannel === "webchat" || params.messageProvider === "webchat") && (params.config?.tools?.profile === "messaging" || params.config?.tools?.profile === "minimal");\n\t\tconst toolsRaw = params.disableTools || isRawModelRun || yinianDesktopFastModelRun ? [] : applyEmbeddedAttemptToolsAllow(createOpenClawCodingTools({',
|
||||
);
|
||||
content = content.replace(
|
||||
'const yinianDesktopFastModelRun = (!params.messageChannel && !params.messageProvider || params.messageChannel === "webchat" || params.messageProvider === "webchat") && (params.config?.tools?.profile === "messaging" || params.config?.tools?.profile === "minimal");\n\t\tconst toolsRaw = params.disableTools || isRawModelRun || yinianDesktopFastModelRun ? [] : applyEmbeddedAttemptToolsAllow(createOpenClawCodingTools({',
|
||||
'const toolsRaw = params.disableTools || isRawModelRun || yinianDesktopFastModelRun ? [] : applyEmbeddedAttemptToolsAllow(createOpenClawCodingTools({',
|
||||
);
|
||||
content = content.replace(
|
||||
'const yinianDesktopFastModelRun = !params.messageChannel && !params.messageProvider && (params.config?.tools?.profile === "messaging" || params.config?.tools?.profile === "minimal");',
|
||||
'const yinianDesktopFastModelRun = (!params.messageChannel && !params.messageProvider || params.messageChannel === "webchat" || params.messageProvider === "webchat") && (params.config?.tools?.profile === "messaging" || params.config?.tools?.profile === "minimal");',
|
||||
);
|
||||
content = content.replace(
|
||||
'...listChannelAgentTools({ cfg: options?.config }),',
|
||||
'...(profile === "messaging" || profile === "minimal" ? [] : listChannelAgentTools({ cfg: options?.config })),',
|
||||
);
|
||||
content = content.replace(
|
||||
'allowGatewaySubagentBinding: options?.allowGatewaySubagentBinding\n\t\t})',
|
||||
'allowGatewaySubagentBinding: options?.allowGatewaySubagentBinding,\n\t\t\tdisablePluginTools: options?.disablePluginTools ?? (profile === "messaging" || profile === "minimal")\n\t\t})',
|
||||
);
|
||||
|
||||
if (content !== before) {
|
||||
writeFileSync(filePath, content, 'utf-8');
|
||||
console.log(`[patch-browser-hint] Patched desktop fast chat runtime: ${file}`);
|
||||
desktopFastChatPatchedCount++;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// openclaw not installed yet or dist not found — skip silently
|
||||
}
|
||||
|
||||
try {
|
||||
for (const file of readdirSync(distDir)) {
|
||||
if (!/^bundled-runtime-deps-.*\.js$/.test(file)) continue;
|
||||
const filePath = join(distDir, file);
|
||||
let content = readFileSync(filePath, 'utf-8');
|
||||
const search = `function isRuntimeDepsPlanMaterialized(installRoot, installSpecs) {
|
||||
\tconst generatedManifestSpecs = readGeneratedInstallManifestSpecs(installRoot);
|
||||
\tconst packageManifestSpecs = generatedManifestSpecs !== null ? null : readPackageRuntimeDepSpecs(installRoot);
|
||||
\treturn (generatedManifestSpecs !== null && sameRuntimeDepSpecs(generatedManifestSpecs, installSpecs) || packageManifestSpecs !== null && sameRuntimeDepSpecs(packageManifestSpecs, installSpecs)) && hasSatisfiedInstallSpecPackages(installRoot, installSpecs);
|
||||
}`;
|
||||
const replace = `function isRuntimeDepsPlanMaterialized(installRoot, installSpecs) {
|
||||
\tconst generatedManifestSpecs = readGeneratedInstallManifestSpecs(installRoot);
|
||||
\tconst packageManifestSpecs = generatedManifestSpecs !== null ? null : readPackageRuntimeDepSpecs(installRoot);
|
||||
\treturn (generatedManifestSpecs !== null || packageManifestSpecs !== null) && hasSatisfiedInstallSpecPackages(installRoot, installSpecs);
|
||||
}`;
|
||||
if (content.includes(search)) {
|
||||
content = content.replace(search, replace);
|
||||
writeFileSync(filePath, content, 'utf-8');
|
||||
console.log(`[patch-browser-hint] Patched runtime deps materialization: ${file}`);
|
||||
runtimeDepsPatchedCount++;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// openclaw not installed yet or dist not found — skip silently
|
||||
}
|
||||
|
||||
if (patchedCount > 0) {
|
||||
console.log(`[patch-browser-hint] Done. Patched ${patchedCount} file(s).`);
|
||||
}
|
||||
if (runtimeDepsPatchedCount > 0) {
|
||||
console.log(`[patch-browser-hint] Done. Patched ${runtimeDepsPatchedCount} runtime deps file(s).`);
|
||||
}
|
||||
if (desktopFastChatPatchedCount > 0) {
|
||||
console.log(`[patch-browser-hint] Done. Patched ${desktopFastChatPatchedCount} desktop fast chat file(s).`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user