refactor(nodes): share node id matcher

This commit is contained in:
Peter Steinberger
2026-02-14 13:42:47 +00:00
parent 81361755b7
commit 06bc9f368b
4 changed files with 117 additions and 84 deletions

View File

@@ -1,3 +1,4 @@
import { resolveNodeIdFromCandidates } from "../../shared/node-match.js";
import { callGatewayTool, type GatewayCallOptions } from "./gateway.js";
export type NodeListNode = {
@@ -61,14 +62,6 @@ function parsePairingList(value: unknown): PairingList {
return { pending, paired };
}
function normalizeNodeKey(value: string) {
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
}
async function loadNodes(opts: GatewayCallOptions): Promise<NodeListNode[]> {
try {
const res = await callGatewayTool("node.list", opts, {});
@@ -131,40 +124,7 @@ export function resolveNodeIdFromList(
}
throw new Error("node required");
}
const qNorm = normalizeNodeKey(q);
const matches = nodes.filter((n) => {
if (n.nodeId === q) {
return true;
}
if (typeof n.remoteIp === "string" && n.remoteIp === q) {
return true;
}
const name = typeof n.displayName === "string" ? n.displayName : "";
if (name && normalizeNodeKey(name) === qNorm) {
return true;
}
if (q.length >= 6 && n.nodeId.startsWith(q)) {
return true;
}
return false;
});
if (matches.length === 1) {
return matches[0].nodeId;
}
if (matches.length === 0) {
const known = nodes
.map((n) => n.displayName || n.remoteIp || n.nodeId)
.filter(Boolean)
.join(", ");
throw new Error(`unknown node: ${q}${known ? ` (known: ${known})` : ""}`);
}
throw new Error(
`ambiguous node: ${q} (matches: ${matches
.map((n) => n.displayName || n.remoteIp || n.nodeId)
.join(", ")})`,
);
return resolveNodeIdFromCandidates(nodes, q);
}
export async function resolveNodeId(