openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
108 lines (107 loc) • 3.26 kB
JavaScript
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import "./tool-policy-BR4v75RA.mjs";
import { c as normalizeToolPolicyName } from "./tool-policy-shared-BHvBopvF.mjs";
//#region src/agents/embedded-agent-runner/run/attempt-tool-call-name-resolution.ts
function resolveCaseInsensitiveAllowedToolName(e, t) {
const n = normalizeLowercaseStringOrEmpty(e);
let r = null;
for (const e of t) {
if (normalizeLowercaseStringOrEmpty(e) !== n) continue;
if (r && r !== e) return null;
r = e;
}
return r;
}
function resolveExactAllowedToolName(e, t) {
if (t.has(e)) return e;
const n = normalizeToolPolicyName(e);
if (t.has(n)) return n;
return resolveCaseInsensitiveAllowedToolName(e, t) ?? resolveCaseInsensitiveAllowedToolName(n, t);
}
function buildStructuredToolNameCandidates(e) {
const t = e.trim();
if (!t) return [];
const n = /* @__PURE__ */ new Set();
const addCandidate = (e) => {
const t = e.trim();
if (t) {
n.add(t);
n.add(normalizeToolPolicyName(t));
}
};
addCandidate(t);
const r = [t];
const i = Math.min(...[
"\"",
"'",
"<"
].map((e) => t.indexOf(e)).filter((e) => e > 0));
if (Number.isFinite(i)) {
const e = t.slice(0, i);
addCandidate(e);
r.push(e);
}
for (const e of r) {
const t = e.replace(/\//g, ".");
addCandidate(t);
const n = normalizeStringEntries(t.split("."));
for (let e = 1; e < n.length; e += 1) addCandidate(n.slice(e).join("."));
}
return [...n];
}
function resolveStructuredAllowedToolName(e, t) {
const n = buildStructuredToolNameCandidates(e);
for (const e of n) if (t.has(e)) return e;
for (const e of n) {
const n = resolveCaseInsensitiveAllowedToolName(e, t);
if (n) return n;
}
return null;
}
function inferToolNameFromToolCallId(e, t) {
if (!e || !t || t.size === 0) return null;
const n = e.trim();
if (!n) return null;
const r = /* @__PURE__ */ new Set();
const addCounterVariants = (e) => {
r.add(e);
r.add(e.replace(/[:._/-]\d+$/, ""));
r.add(e.replace(/\d+$/, ""));
};
const addToken = (e) => {
const t = e.trim();
if (!t) return;
const n = t.replace(/\//g, ".");
addCounterVariants(t);
addCounterVariants(n);
for (const e of [/^functions?[._-]?/i, /^tools?[._-]?/i]) {
const t = n.replace(e, "");
if (t !== n) addCounterVariants(t);
}
};
for (const e of [n, n.split(":")[0] ?? n]) addToken(e);
let i = null;
for (const e of r) {
const n = resolveStructuredAllowedToolName(e, t);
if (!n) continue;
if (i && i !== n) return null;
i = n;
}
return i;
}
function looksLikeMalformedToolNameCounter(e) {
const t = e.trim().replace(/\//g, ".");
return /^(?:functions?|tools?)[._-]?/i.test(t) && /(?:[:._-]\d+|\d+)$/.test(t);
}
function resolveToolCallName(e, t, n, r = false) {
const i = e.trim();
if (!i) return inferToolNameFromToolCallId(n, t) ?? (r ? null : e);
if (!t || t.size === 0) return i;
const a = resolveExactAllowedToolName(i, t) || inferToolNameFromToolCallId(i, t);
if (a) return a;
if (looksLikeMalformedToolNameCounter(i)) return r ? null : i;
return resolveStructuredAllowedToolName(i, t) ?? (r ? null : i);
}
//#endregion
export { resolveToolCallName as t };