Secrets: fast-path core target discovery

This commit is contained in:
Peter Steinberger
2026-04-07 01:03:30 +08:00
parent 1430de95a5
commit a22e44f259
4 changed files with 48 additions and 72 deletions

View File

@@ -466,6 +466,10 @@ const CORE_SECRET_TARGET_REGISTRY: SecretTargetRegistryEntry[] = [
let cachedSecretTargetRegistry: SecretTargetRegistryEntry[] | null = null;
export function getCoreSecretTargetRegistry(): SecretTargetRegistryEntry[] {
return CORE_SECRET_TARGET_REGISTRY;
}
export function getSecretTargetRegistry(): SecretTargetRegistryEntry[] {
if (cachedSecretTargetRegistry) {
return cachedSecretTargetRegistry;

View File

@@ -1,6 +1,6 @@
import type { OpenClawConfig } from "../config/config.js";
import { getPath } from "./path-utils.js";
import { getSecretTargetRegistry } from "./target-registry-data.js";
import { getCoreSecretTargetRegistry, getSecretTargetRegistry } from "./target-registry-data.js";
import {
compileTargetRegistryEntry,
expandPathTokens,
@@ -24,6 +24,12 @@ let compiledSecretTargetRegistryState: {
targetsByType: Map<string, CompiledTargetRegistryEntry[]>;
} | null = null;
let compiledCoreOpenClawTargetState: {
knownTargetIds: Set<string>;
openClawCompiledSecretTargets: CompiledTargetRegistryEntry[];
openClawTargetsById: Map<string, CompiledTargetRegistryEntry[]>;
} | null = null;
function buildTargetTypeIndex(
compiledSecretTargetRegistry: CompiledTargetRegistryEntry[],
): Map<string, CompiledTargetRegistryEntry[]> {
@@ -83,6 +89,21 @@ function getCompiledSecretTargetRegistryState() {
return compiledSecretTargetRegistryState;
}
function getCompiledCoreOpenClawTargetState() {
if (compiledCoreOpenClawTargetState) {
return compiledCoreOpenClawTargetState;
}
const openClawCompiledSecretTargets = getCoreSecretTargetRegistry()
.filter((entry) => entry.configFile === "openclaw.json")
.map(compileTargetRegistryEntry);
compiledCoreOpenClawTargetState = {
knownTargetIds: new Set(openClawCompiledSecretTargets.map((entry) => entry.id)),
openClawCompiledSecretTargets,
openClawTargetsById: buildConfigTargetIdIndex(openClawCompiledSecretTargets),
};
return compiledCoreOpenClawTargetState;
}
function normalizeAllowedTargetIds(targetIds?: Iterable<string>): Set<string> | null {
if (targetIds === undefined) {
return null;
@@ -281,7 +302,13 @@ export function discoverConfigSecretTargetsByIds(
targetIds?: Iterable<string>,
): DiscoveredConfigSecretTarget[] {
const allowedTargetIds = normalizeAllowedTargetIds(targetIds);
const registryState = getCompiledSecretTargetRegistryState();
const registryState =
allowedTargetIds !== null &&
Array.from(allowedTargetIds).every((targetId) =>
getCompiledCoreOpenClawTargetState().knownTargetIds.has(targetId),
)
? getCompiledCoreOpenClawTargetState()
: getCompiledSecretTargetRegistryState();
const discoveryEntries = resolveDiscoveryEntries({
allowedTargetIds,
defaultEntries: registryState.openClawCompiledSecretTargets,