openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
223 lines (222 loc) • 8.35 kB
JavaScript
import { d as isRecord$1 } from "./utils-CS0Ikux6.js";
import { t as isBlockedObjectKey } from "./prototype-keys-Cm_8mWvq.js";
import { t as isSafeExecutableValue } from "./exec-safety-CNibhD0r.js";
//#region src/config/legacy.shared.ts
const getRecord = (value) => isRecord$1(value) ? value : null;
const ensureRecord$1 = (root, key) => {
const existing = root[key];
if (isRecord$1(existing)) return existing;
const next = {};
root[key] = next;
return next;
};
const mergeMissing = (target, source) => {
for (const [key, value] of Object.entries(source)) {
if (value === void 0 || isBlockedObjectKey(key)) continue;
const existing = target[key];
if (existing === void 0) {
target[key] = value;
continue;
}
if (isRecord$1(existing) && isRecord$1(value)) mergeMissing(existing, value);
}
};
const mapLegacyAudioTranscription = (value) => {
const transcriber = getRecord(value);
const command = Array.isArray(transcriber?.command) ? transcriber?.command : null;
if (!command || command.length === 0) return null;
if (typeof command[0] !== "string") return null;
if (!command.every((part) => typeof part === "string")) return null;
const rawExecutable = command[0].trim();
if (!rawExecutable) return null;
if (!isSafeExecutableValue(rawExecutable)) return null;
const args = command.slice(1);
const timeoutSeconds = typeof transcriber?.timeoutSeconds === "number" ? transcriber?.timeoutSeconds : void 0;
const result = {
command: rawExecutable,
type: "cli"
};
if (args.length > 0) result.args = args;
if (timeoutSeconds !== void 0) result.timeoutSeconds = timeoutSeconds;
return result;
};
const getAgentsList = (agents) => {
const list = agents?.list;
return Array.isArray(list) ? list : [];
};
const resolveDefaultAgentIdFromRaw = (raw) => {
const list = getAgentsList(getRecord(raw.agents));
const defaultEntry = list.find((entry) => isRecord$1(entry) && entry.default === true && typeof entry.id === "string" && entry.id.trim() !== "");
if (defaultEntry) return defaultEntry.id.trim();
const routing = getRecord(raw.routing);
const routingDefault = typeof routing?.defaultAgentId === "string" ? routing.defaultAgentId.trim() : "";
if (routingDefault) return routingDefault;
const firstEntry = list.find((entry) => isRecord$1(entry) && typeof entry.id === "string" && entry.id.trim() !== "");
if (firstEntry) return firstEntry.id.trim();
return "main";
};
const ensureAgentEntry = (list, id) => {
const normalized = id.trim();
const existing = list.find((entry) => isRecord$1(entry) && typeof entry.id === "string" && entry.id.trim() === normalized);
if (existing) return existing;
const created = { id: normalized };
list.push(created);
return created;
};
//#endregion
//#region src/config/legacy-web-search.ts
const GENERIC_WEB_SEARCH_KEYS = new Set([
"enabled",
"provider",
"maxResults",
"timeoutSeconds",
"cacheTtlMinutes"
]);
const LEGACY_PROVIDER_MAP = {
brave: "brave",
firecrawl: "firecrawl",
gemini: "google",
grok: "xai",
kimi: "moonshot",
perplexity: "perplexity"
};
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function cloneRecord(value) {
return { ...value };
}
function ensureRecord(target, key) {
const current = target[key];
if (isRecord(current)) return current;
const next = {};
target[key] = next;
return next;
}
function resolveLegacySearchConfig(raw) {
if (!isRecord(raw)) return;
const tools = isRecord(raw.tools) ? raw.tools : void 0;
const web = isRecord(tools?.web) ? tools.web : void 0;
return isRecord(web?.search) ? web.search : void 0;
}
function copyLegacyProviderConfig(search, providerKey) {
const current = search[providerKey];
return isRecord(current) ? cloneRecord(current) : void 0;
}
function hasOwnKey(target, key) {
return Object.prototype.hasOwnProperty.call(target, key);
}
function hasMappedLegacyWebSearchConfig(raw) {
const search = resolveLegacySearchConfig(raw);
if (!search) return false;
if (hasOwnKey(search, "apiKey")) return true;
return Object.keys(LEGACY_PROVIDER_MAP).some((providerId) => isRecord(search[providerId]));
}
function migratePluginWebSearchConfig(params) {
const entry = ensureRecord(ensureRecord(ensureRecord(params.root, "plugins"), "entries"), params.pluginId);
const config = ensureRecord(entry, "config");
const hadEnabled = entry.enabled !== void 0;
const existing = isRecord(config.webSearch) ? cloneRecord(config.webSearch) : void 0;
if (!hadEnabled) entry.enabled = true;
if (!existing) {
config.webSearch = cloneRecord(params.payload);
params.changes.push(`Moved ${params.legacyPath} → ${params.targetPath}.`);
return;
}
const merged = cloneRecord(existing);
mergeMissing(merged, params.payload);
const changed = JSON.stringify(merged) !== JSON.stringify(existing) || !hadEnabled;
config.webSearch = merged;
if (changed) {
params.changes.push(`Merged ${params.legacyPath} → ${params.targetPath} (filled missing fields from legacy; kept explicit plugin config values).`);
return;
}
params.changes.push(`Removed ${params.legacyPath} (${params.targetPath} already set).`);
}
function listLegacyWebSearchConfigPaths(raw) {
const search = resolveLegacySearchConfig(raw);
if (!search) return [];
const paths = [];
if ("apiKey" in search) paths.push("tools.web.search.apiKey");
for (const providerId of Object.keys(LEGACY_PROVIDER_MAP)) {
const scoped = search[providerId];
if (isRecord(scoped)) for (const key of Object.keys(scoped)) paths.push(`tools.web.search.${providerId}.${key}`);
}
return paths;
}
function normalizeLegacyWebSearchConfig(raw) {
if (!isRecord(raw)) return raw;
if (!resolveLegacySearchConfig(raw)) return raw;
return normalizeLegacyWebSearchConfigRecord(raw).config;
}
function migrateLegacyWebSearchConfig(raw) {
if (!isRecord(raw)) return {
config: raw,
changes: []
};
if (!hasMappedLegacyWebSearchConfig(raw)) return {
config: raw,
changes: []
};
return normalizeLegacyWebSearchConfigRecord(raw);
}
function normalizeLegacyWebSearchConfigRecord(raw) {
const nextRoot = cloneRecord(raw);
const web = ensureRecord(ensureRecord(nextRoot, "tools"), "web");
const search = resolveLegacySearchConfig(nextRoot);
if (!search) return {
config: raw,
changes: []
};
const nextSearch = {};
const changes = [];
for (const [key, value] of Object.entries(search)) {
if (key === "apiKey") continue;
if (Object.keys(LEGACY_PROVIDER_MAP).includes(key)) {
if (isRecord(value)) continue;
}
if (GENERIC_WEB_SEARCH_KEYS.has(key) || !isRecord(value)) nextSearch[key] = value;
}
web.search = nextSearch;
const legacyBraveConfig = copyLegacyProviderConfig(search, "brave");
const braveConfig = legacyBraveConfig ?? {};
if (hasOwnKey(search, "apiKey")) braveConfig.apiKey = search.apiKey;
if (Object.keys(braveConfig).length > 0) migratePluginWebSearchConfig({
root: nextRoot,
legacyPath: hasOwnKey(search, "apiKey") ? "tools.web.search.apiKey" : "tools.web.search.brave",
targetPath: hasOwnKey(search, "apiKey") && !legacyBraveConfig ? "plugins.entries.brave.config.webSearch.apiKey" : "plugins.entries.brave.config.webSearch",
pluginId: LEGACY_PROVIDER_MAP.brave,
payload: braveConfig,
changes
});
for (const providerId of [
"firecrawl",
"gemini",
"grok",
"kimi",
"perplexity"
]) {
const scoped = copyLegacyProviderConfig(search, providerId);
if (!scoped || Object.keys(scoped).length === 0) continue;
migratePluginWebSearchConfig({
root: nextRoot,
legacyPath: `tools.web.search.${providerId}`,
targetPath: `plugins.entries.${LEGACY_PROVIDER_MAP[providerId]}.config.webSearch`,
pluginId: LEGACY_PROVIDER_MAP[providerId],
payload: scoped,
changes
});
}
return {
config: nextRoot,
changes
};
}
function resolvePluginWebSearchConfig(config, pluginId) {
const pluginConfig = config?.plugins?.entries?.[pluginId]?.config;
if (!isRecord(pluginConfig)) return;
const webSearch = pluginConfig.webSearch;
return isRecord(webSearch) ? webSearch : void 0;
}
//#endregion
export { ensureAgentEntry as a, getRecord as c, resolveDefaultAgentIdFromRaw as d, resolvePluginWebSearchConfig as i, mapLegacyAudioTranscription as l, migrateLegacyWebSearchConfig as n, ensureRecord$1 as o, normalizeLegacyWebSearchConfig as r, getAgentsList as s, listLegacyWebSearchConfigPaths as t, mergeMissing as u };