@sema-agent/core
Version:
Stateless, task-oriented AI agent core
93 lines (92 loc) • 3.77 kB
JavaScript
import { hostTargetOf } from "./tool-registry.js";
import { seatOf } from "./tool-name-readings.js";
export function readSpelledHost(spelled) {
if (spelled === "" || /[\s/\\@?#%]/.test(spelled) || (spelled.includes(":") && !/^\[[^\]]+\]$/.test(spelled)))
return { problem: "not_a_host" };
let url;
try {
url = new URL(`https://${spelled}/`);
}
catch {
return { problem: "not_a_host" };
}
if (url.port !== "" || url.username !== "" || url.password !== "" || url.pathname !== "/" || url.search !== "" || url.hash !== "")
return { problem: "not_a_host" };
const host = url.hostname.replace(/\.+$/, "");
if (host === "")
return { problem: "not_a_host" };
return !host.startsWith("[") && host.split(".").includes("") ? { problem: "empty_label" } : { host };
}
export function hostOfSpelling(spelled) {
const read = readSpelledHost(spelled);
return "host" in read ? read.host : undefined;
}
export function isAddressHost(host) {
return host.startsWith("[") || /^\d+\.\d+\.\d+\.\d+$/.test(host);
}
export function readHostPattern(spelled) {
if (spelled === "*")
return { problem: "every_host" };
const below = spelled.startsWith("*.");
const read = readSpelledHost(below ? spelled.slice(2) : spelled);
if ("problem" in read)
return read;
const host = read.host;
if (host.includes("*"))
return { problem: "inner_wildcard" };
if (hostOfSpelling(host) !== host)
return { problem: "not_a_host" };
if (below && isAddressHost(host))
return { problem: "below_address" };
return { pattern: { match: below ? "subdomain" : "host", host } };
}
export function spelledHostPattern(pattern) {
return pattern.match === "subdomain" ? `*.${pattern.host}` : pattern.host;
}
export function hostPatternAdmits(pattern, host) {
if (pattern.match === "host")
return host === pattern.host;
if (isAddressHost(host) || !host.endsWith(`.${pattern.host}`))
return false;
const labels = host.slice(0, -(pattern.host.length + 1));
return labels !== "" && !labels.split(".").includes("");
}
export const HOST_UNREADABLE_REASONS = Object.freeze(["not_text", "not_url", "not_http", "credentials", "unstable"]);
export const HOST_UNREADABLE_SENTENCE = Object.freeze({
not_text: "the URL argument is not a string",
not_url: "the URL does not parse",
not_http: "the URL is not http(s)",
credentials: "the URL carries credentials",
unstable: "the URL's host does not read back as one host",
});
export function hostOfUrl(value) {
if (typeof value !== "string")
return { unreadable: "not_text" };
let url;
try {
url = new URL(value);
}
catch {
return { unreadable: "not_url" };
}
if (url.protocol !== "http:" && url.protocol !== "https:")
return { unreadable: "not_http" };
if (url.username !== "" || url.password !== "")
return { unreadable: "credentials" };
const host = url.hostname.replace(/\.+$/, "");
return host !== "" && hostOfSpelling(host) === host ? { host } : { unreadable: "unstable" };
}
export function callHostTargetOf(call, direction) {
const slot = hostSlotOf(call, direction);
if (slot === undefined)
return undefined;
const args = call.args;
const own = typeof args === "object" && args !== null && !Array.isArray(args) && Object.hasOwn(args, slot.param);
return hostOfUrl(own ? args[slot.param] : undefined);
}
function hostSlotOf(call, direction) {
const declared = call.face?.hostTarget;
if (declared !== undefined || direction === "grant")
return declared;
return seatOf("floor.protectiveHostTarget", { get: hostTargetOf }, call)?.value;
}