Spaces:
Paused
Paused
File size: 4,472 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | import fs from "node:fs";
import path from "node:path";
import type { OpenClawConfig, HookConfig } from "../config/config.js";
import type { HookEligibilityContext, HookEntry } from "./types.js";
import { resolveHookKey } from "./frontmatter.js";
const DEFAULT_CONFIG_VALUES: Record<string, boolean> = {
"browser.enabled": true,
"browser.evaluateEnabled": true,
"workspace.dir": true,
};
function isTruthy(value: unknown): boolean {
if (value === undefined || value === null) {
return false;
}
if (typeof value === "boolean") {
return value;
}
if (typeof value === "number") {
return value !== 0;
}
if (typeof value === "string") {
return value.trim().length > 0;
}
return true;
}
export function resolveConfigPath(config: OpenClawConfig | undefined, pathStr: string) {
const parts = pathStr.split(".").filter(Boolean);
let current: unknown = config;
for (const part of parts) {
if (typeof current !== "object" || current === null) {
return undefined;
}
current = (current as Record<string, unknown>)[part];
}
return current;
}
export function isConfigPathTruthy(config: OpenClawConfig | undefined, pathStr: string): boolean {
const value = resolveConfigPath(config, pathStr);
if (value === undefined && pathStr in DEFAULT_CONFIG_VALUES) {
return DEFAULT_CONFIG_VALUES[pathStr];
}
return isTruthy(value);
}
export function resolveHookConfig(
config: OpenClawConfig | undefined,
hookKey: string,
): HookConfig | undefined {
const hooks = config?.hooks?.internal?.entries;
if (!hooks || typeof hooks !== "object") {
return undefined;
}
const entry = (hooks as Record<string, HookConfig | undefined>)[hookKey];
if (!entry || typeof entry !== "object") {
return undefined;
}
return entry;
}
export function resolveRuntimePlatform(): string {
return process.platform;
}
export function hasBinary(bin: string): boolean {
const pathEnv = process.env.PATH ?? "";
const parts = pathEnv.split(path.delimiter).filter(Boolean);
for (const part of parts) {
const candidate = path.join(part, bin);
try {
fs.accessSync(candidate, fs.constants.X_OK);
return true;
} catch {
// keep scanning
}
}
return false;
}
export function shouldIncludeHook(params: {
entry: HookEntry;
config?: OpenClawConfig;
eligibility?: HookEligibilityContext;
}): boolean {
const { entry, config, eligibility } = params;
const hookKey = resolveHookKey(entry.hook.name, entry);
const hookConfig = resolveHookConfig(config, hookKey);
const pluginManaged = entry.hook.source === "openclaw-plugin";
const osList = entry.metadata?.os ?? [];
const remotePlatforms = eligibility?.remote?.platforms ?? [];
// Check if explicitly disabled
if (!pluginManaged && hookConfig?.enabled === false) {
return false;
}
// Check OS requirement
if (
osList.length > 0 &&
!osList.includes(resolveRuntimePlatform()) &&
!remotePlatforms.some((platform) => osList.includes(platform))
) {
return false;
}
// If marked as 'always', bypass all other checks
if (entry.metadata?.always === true) {
return true;
}
// Check required binaries (all must be present)
const requiredBins = entry.metadata?.requires?.bins ?? [];
if (requiredBins.length > 0) {
for (const bin of requiredBins) {
if (hasBinary(bin)) {
continue;
}
if (eligibility?.remote?.hasBin?.(bin)) {
continue;
}
return false;
}
}
// Check anyBins (at least one must be present)
const requiredAnyBins = entry.metadata?.requires?.anyBins ?? [];
if (requiredAnyBins.length > 0) {
const anyFound =
requiredAnyBins.some((bin) => hasBinary(bin)) ||
eligibility?.remote?.hasAnyBin?.(requiredAnyBins);
if (!anyFound) {
return false;
}
}
// Check required environment variables
const requiredEnv = entry.metadata?.requires?.env ?? [];
if (requiredEnv.length > 0) {
for (const envName of requiredEnv) {
if (process.env[envName]) {
continue;
}
if (hookConfig?.env?.[envName]) {
continue;
}
return false;
}
}
// Check required config paths
const requiredConfig = entry.metadata?.requires?.config ?? [];
if (requiredConfig.length > 0) {
for (const configPath of requiredConfig) {
if (!isConfigPathTruthy(config, configPath)) {
return false;
}
}
}
return true;
}
|