@mastra/core
Version:
Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.
281 lines (274 loc) • 9.1 kB
JavaScript
import { isProviderRegistered, getRegisteredProviders, parseModelString } from '../../chunk-W6U2WX22.js';
// src/agent-builder/ee/types.ts
var BUILDER_FEATURE_DEFAULTS = {
tools: true,
agents: true,
workflows: true,
scorers: true,
skills: true,
memory: true,
variables: true,
favorites: true,
avatarUpload: true,
model: true
};
function resolveAgentFeatures(raw, ctx) {
const pick = (key) => {
const explicit = raw?.[key];
return explicit === void 0 ? BUILDER_FEATURE_DEFAULTS[key] : explicit;
};
const resolveBrowser = () => {
const explicit = raw?.browser;
if (explicit === false) return false;
if (explicit === true) return ctx.hasBrowserConfig;
return ctx.hasBrowserConfig;
};
return {
tools: pick("tools"),
agents: pick("agents"),
workflows: pick("workflows"),
scorers: pick("scorers"),
skills: pick("skills"),
memory: pick("memory"),
variables: pick("variables"),
favorites: pick("favorites"),
avatarUpload: pick("avatarUpload"),
model: pick("model"),
browser: resolveBrowser()
};
}
// src/agent-builder/ee/errors.ts
var MODEL_NOT_ALLOWED_CODE = "MODEL_NOT_ALLOWED";
var ModelNotAllowedError = class extends Error {
code = MODEL_NOT_ALLOWED_CODE;
allowed;
attempted;
offendingLabel;
constructor(args) {
const message = args.message ?? `Model "${args.attempted.provider}/${args.attempted.modelId}" (${args.offendingLabel}) is not in the configured allowlist.`;
super(message);
this.name = "ModelNotAllowedError";
this.allowed = args.allowed;
this.attempted = args.attempted;
this.offendingLabel = args.offendingLabel;
}
};
function isModelNotAllowedError(error) {
return error instanceof Error && error.code === MODEL_NOT_ALLOWED_CODE;
}
// src/agent-builder/ee/normalize-candidate.ts
function splitRuntimeModelString(input) {
const providers = getRegisteredProviders().sort((a, b) => b.length - a.length);
for (const providerId of providers) {
const prefix = `${providerId}/`;
if (input.startsWith(prefix)) {
const modelId = input.slice(prefix.length);
if (modelId.length > 0) return { provider: providerId, modelId };
}
}
const parsed = parseModelString(input);
if (parsed.provider && parsed.modelId) {
return { provider: parsed.provider, modelId: parsed.modelId };
}
return void 0;
}
function isPlainObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function fromObject(value, origin, label) {
const providerField = value.provider;
const modelIdField = value.modelId;
const nameField = value.name;
const idField = value.id;
const providerIdField = value.providerId;
if (typeof idField === "string" && idField.includes("/") && providerField === void 0) {
const split = splitRuntimeModelString(idField);
if (split) {
return [{ ...split, origin: "openai-compatible", label: label ?? idField }];
}
}
if (typeof providerIdField === "string" && typeof modelIdField === "string") {
return [
{
provider: providerIdField,
modelId: modelIdField,
origin: "openai-compatible",
label: label ?? `${providerIdField}/${modelIdField}`
}
];
}
if (typeof providerField === "string" && typeof modelIdField === "string") {
const isSdkInstance = typeof value.doGenerate === "function";
return [
{
provider: providerField,
modelId: modelIdField,
origin: isSdkInstance ? "sdk-instance" : origin,
label: label ?? `${providerField}/${modelIdField}`
}
];
}
if (typeof providerField === "string" && typeof nameField === "string") {
return [
{
provider: providerField,
modelId: nameField,
origin,
label: label ?? `${providerField}/${nameField}`
}
];
}
return [];
}
function toModelCandidates(input) {
if (input === null || input === void 0) return [];
if (typeof input === "function") return [];
if (typeof input === "string") {
const split = splitRuntimeModelString(input);
if (!split) return [];
return [{ ...split, origin: "runtime", label: input }];
}
if (Array.isArray(input)) {
const candidates = [];
input.forEach((variant, index) => {
if (!isPlainObject(variant)) return;
const value = variant.value ?? variant;
const hasRules = isPlainObject(variant) && "rules" in variant && variant.rules != null;
const origin = hasRules ? "conditional-variant" : "conditional-default";
const label = hasRules ? `variant[${index}]` : `variant[${index}] (default)`;
if (typeof value === "string") {
const split = splitRuntimeModelString(value);
if (split) {
candidates.push({ ...split, origin, label });
}
return;
}
if (isPlainObject(value)) {
candidates.push(...fromObject(value, origin, label));
}
});
return candidates;
}
if (isPlainObject(input)) {
return fromObject(input, "static");
}
return [];
}
// src/agent-builder/ee/allowlist.ts
function matchesProvider(entry, candidate) {
if (entry.provider !== candidate.provider) return false;
if (!entry.modelId) return true;
return entry.modelId === candidate.modelId;
}
function isModelAllowed(allowed, candidate) {
if (allowed === void 0) return true;
if (allowed.length === 0) return true;
const activeEntries = allowed.filter((entry) => {
if ("kind" in entry && entry.kind === "custom") return true;
return isProviderRegistered(entry.provider);
});
if (activeEntries.length === 0) return false;
return activeEntries.some((entry) => matchesProvider(entry, candidate));
}
function enforceModelAllowlist(allowed, input) {
const candidates = toModelCandidates(input);
for (const candidate of candidates) {
if (!isModelAllowed(allowed, candidate)) {
return {
ok: false,
attempted: candidate,
offendingLabel: candidate.label ?? candidate.origin
};
}
}
return { ok: true };
}
function assertModelAllowed(allowed, input) {
const result = enforceModelAllowlist(allowed, input);
if (result.ok) return;
throw new ModelNotAllowedError({
allowed,
attempted: result.attempted,
offendingLabel: result.offendingLabel
});
}
// src/agent-builder/ee/policy.ts
function isBuilderModelPolicyActive(inputs) {
if (!inputs.enabled) return false;
if (inputs.pickerVisible) return true;
if (inputs.allowed !== void 0) return true;
if (inputs.default !== void 0) return true;
return false;
}
function builderToModelPolicy(builder) {
if (!builder || !builder.enabled) {
return { active: false };
}
const features = builder.getFeatures();
const configuration = builder.getConfiguration();
const pickerVisible = features?.agent?.model === true;
const models = configuration?.agent?.models;
const allowed = models?.allowed;
const defaultModel = models?.default;
const active = isBuilderModelPolicyActive({
enabled: builder.enabled,
pickerVisible,
allowed,
default: defaultModel
});
if (!active) {
return { active: false };
}
return {
active: true,
pickerVisible,
...allowed !== void 0 ? { allowed } : {},
...defaultModel !== void 0 ? { default: defaultModel } : {}
};
}
// src/agent-builder/ee/picker.ts
function resolveOne(allowlist, registered, kindLabel, configPath) {
if (allowlist === void 0) {
return { visible: null, warnings: [] };
}
const known = new Set(registered);
const seen = /* @__PURE__ */ new Set();
const visible = [];
const warnings = [];
for (const id of allowlist) {
if (seen.has(id)) continue;
seen.add(id);
if (known.has(id)) {
visible.push(id);
} else {
warnings.push(
`${configPath} references unknown ${kindLabel} "${id}" \u2014 no ${kindLabel} with this ID is registered. It will be hidden from the builder picker.`
);
}
}
return { visible, warnings };
}
function resolvePickerVisibility({
config,
registeredToolIds,
registeredAgentIds,
registeredWorkflowIds
}) {
const tools = resolveOne(config?.tools?.allowed, registeredToolIds, "tool", "configuration.agent.tools.allowed");
const agents = resolveOne(config?.agents?.allowed, registeredAgentIds, "agent", "configuration.agent.agents.allowed");
const workflows = resolveOne(
config?.workflows?.allowed,
registeredWorkflowIds,
"workflow",
"configuration.agent.workflows.allowed"
);
return {
visibleTools: tools.visible,
visibleAgents: agents.visible,
visibleWorkflows: workflows.visible,
warnings: [...tools.warnings, ...agents.warnings, ...workflows.warnings]
};
}
export { BUILDER_FEATURE_DEFAULTS, MODEL_NOT_ALLOWED_CODE, ModelNotAllowedError, assertModelAllowed, builderToModelPolicy, enforceModelAllowlist, isBuilderModelPolicyActive, isModelAllowed, isModelNotAllowedError, matchesProvider, resolveAgentFeatures, resolvePickerVisibility, toModelCandidates };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map