Files
openclaw/src/shared/node-resolve.ts
T
Peter Steinberger 1e7510ae10 docs: continue inline comment pass (#88849)
Adds broad inline comments and JSDoc for CLI, cron, outbound/channel, plugin SDK, ACP, shared helpers, net policy, and related utility contracts. Proof: git diff --check on latest exact head plus focused cron tests passed; CI had no failing checks observed before merge attempt.
2026-05-31 22:32:28 -04:00

38 lines
1.4 KiB
TypeScript

import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { type NodeMatchCandidate, resolveNodeIdFromCandidates } from "./node-match.js";
type ResolveNodeFromListOptions<TNode extends NodeMatchCandidate> = {
allowDefault?: boolean;
pickDefaultNode?: (nodes: TNode[]) => TNode | null;
};
/** Resolves a user query to a node id, optionally using a caller-defined blank-query default. */
export function resolveNodeIdFromNodeList<TNode extends NodeMatchCandidate>(
nodes: TNode[],
query?: string,
options: ResolveNodeFromListOptions<TNode> = {},
): string {
const q = normalizeOptionalString(query) ?? "";
if (!q) {
if (options.allowDefault === true && options.pickDefaultNode) {
const picked = options.pickDefaultNode(nodes);
if (picked) {
return picked.nodeId;
}
}
throw new Error("node required");
}
return resolveNodeIdFromCandidates(nodes, q);
}
/** Resolves a full node entry, preserving synthetic defaults returned by the picker. */
export function resolveNodeFromNodeList<TNode extends NodeMatchCandidate>(
nodes: TNode[],
query?: string,
options: ResolveNodeFromListOptions<TNode> = {},
): TNode {
const nodeId = resolveNodeIdFromNodeList(nodes, query, options);
// Default pickers may return a node not present in the original list; keep that id usable.
return nodes.find((node) => node.nodeId === nodeId) ?? ({ nodeId } as TNode);
}