@sema-agent/core
Version:
Stateless, task-oriented AI agent core
78 lines (77 loc) • 4.65 kB
JavaScript
import { types } from "node:util";
import { describeRejectedValue } from "../core/tool-search-setting.js";
import { snapshotToolNameList } from "../core/tool-name-list.js";
import { snapshotAndValidateMcpEntries } from "../core/locked-config.js";
import { judgeThinkingLevel } from "../brain/reasoning.js";
import { transportKindOf } from "../core/mcp-server-spec.js";
import { inlineUntrusted } from "../core/untrusted-text.js";
export function definitionMcpServers(declared, agentName) {
const where = `AgentDefinition.mcp of ${typeof agentName === "string" ? JSON.stringify(agentName.slice(0, 80)) : "an unnamed definition"}`;
const servers = snapshotAndValidateMcpEntries(declared, where);
const host = servers.find((s) => transportKindOf(s) === "host");
if (host !== undefined) {
throw Object.assign(new Error(`${where} declares MCP server "${inlineUntrusted(host.name, 120)}" with a host-supplied transport ({ kind: "host" }) — a host seat reaches a leg only along the servers it inherits from the run that spawns it, never from a definition, so the definition is refused. Declare the server on the run that spawns the agent, and name it in the definition's allowTools (\`mcp__<server>\`).`), { code: "config.mcp_host_seat_missing" });
}
return servers;
}
export function snapshotDefinitionLists(def) {
const copy = { ...def };
if (copy.allowTools !== undefined)
copy.allowTools = snapshotToolNameList(copy.allowTools);
if (copy.denyTools !== undefined)
copy.denyTools = snapshotToolNameList(copy.denyTools);
const named = typeof copy.name === "string" ? JSON.stringify(copy.name.slice(0, 80)) : "an unnamed definition";
if (copy.mcp !== undefined)
copy.mcp = definitionMcpServers(copy.mcp, copy.name);
judgeThinkingLevel(copy.thinking, `AgentDefinition.thinking of ${named}`);
return copy;
}
export function judgeAgentDefinitionDeclarations(definition) {
try {
return { ok: true, definition: snapshotDefinitionLists(definition) };
}
catch (refused) {
if (types.isNativeError(refused) && !types.isProxy(refused))
return { ok: false, error: refused };
return { ok: false, error: Object.assign(new Error(`the agent definition could not be read: reading it threw ${describeRejectedValue(refused)} — a definition the engine cannot read is refused rather than judged ok or let throw.`), { code: "config.agent.invalid" }) };
}
}
const CARRIED = new WeakMap();
function dataMember(holder, key) {
if (typeof holder !== "object" || holder === null || types.isProxy(holder))
return undefined;
const descriptor = Object.getOwnPropertyDescriptor(holder, key);
return descriptor !== undefined && Object.hasOwn(descriptor, "value") ? descriptor.value : undefined;
}
const listOf = (value) => (Array.isArray(value) && !types.isProxy(value) ? value : []);
function carriedDeclarationsOf(tool) {
if ((typeof tool !== "object" || tool === null) && typeof tool !== "function")
return [];
const seam = dataMember(tool, "withAgents");
const record = CARRIED.get(tool) ?? (typeof seam === "function" ? CARRIED.get(seam) : undefined);
return record === undefined ? [] : [record];
}
export function recordCarried(tool, minted) {
CARRIED.set(tool, Object.freeze(heldDeclarationsOf(minted)));
return tool;
}
export function heldDeclarationsOf(keeper) {
const carried = listOf(dataMember(keeper, "tools")).flatMap(carriedDeclarationsOf);
const joined = (own, more) => (more.length === 0 ? own : [...listOf(own), ...more]);
const mcp = joined(dataMember(keeper, "mcp"), [...listOf(dataMember(keeper, "agents")), ...carried].flatMap((holder) => listOf(dataMember(holder, "mcp"))));
const a2a = joined(dataMember(keeper, "a2a"), carried.flatMap((record) => listOf(dataMember(record, "a2a"))));
return { ...(mcp !== undefined ? { mcp } : {}), ...(a2a !== undefined ? { a2a } : {}) };
}
export function defineAgent(def) {
if (!def.name || !def.name.trim()) {
const e = new Error("defineAgent: `name` is required and must be a non-empty string.");
e.code = "config.agent.invalid";
throw e;
}
if (def.memoryPersistenceCapable !== undefined && typeof def.memoryPersistenceCapable !== "boolean") {
const e = new Error(`defineAgent("${def.name}"): memoryPersistenceCapable must be a boolean when present — got ${describeRejectedValue(def.memoryPersistenceCapable)} (a string "false" would silently read as capable).`);
e.code = "config.agent.invalid";
throw e;
}
return Object.freeze({ ...def });
}