mlld
Version:
mlld: llm scripting language
1,586 lines (1,580 loc) • 84.4 kB
JavaScript
import { isImported, isExecutable } from './chunk-EIWSEMQ4.mjs';
import { isLoadContentResult } from './chunk-TZUIAYSF.mjs';
import { __name } from './chunk-NJQT543K.mjs';
// core/types/security.ts
var BUILTIN_LABELS = {
// Data
SECRET: "secret",
PII: "pii",
PUBLIC: "public",
UNTRUSTED: "untrusted",
TRUSTED: "trusted",
// Network
NET_R: "net:r",
NET_W: "net:w",
NET_RW: "net:rw",
NETWORK: "network",
// Filesystem
FS_R: "fs:r",
FS_W: "fs:w",
FS_RW: "fs:rw",
FILESYSTEM: "filesystem",
// Risk
SAFE: "safe",
MODERATE: "moderate",
DANGEROUS: "dangerous",
DESTRUCTIVE: "destructive",
PAID: "paid",
// Source
DYNAMIC: "dynamic"
};
var DATA_LABELS = [
"secret",
"pii",
"untrusted",
"public",
"trusted",
"destructive",
"network"
];
function freezeArray(values) {
if (!values) {
return Object.freeze([]);
}
const deduped = Array.from(new Set(values));
return Object.freeze(deduped);
}
__name(freezeArray, "freezeArray");
function freezeObject(input) {
if (!input) {
return input;
}
return Object.freeze({
...input
});
}
__name(freezeObject, "freezeObject");
function createDescriptor(labels, taint, sources, capability, policyContext) {
return Object.freeze({
labels,
taint,
sources,
capability,
policyContext
});
}
__name(createDescriptor, "createDescriptor");
function makeSecurityDescriptor(options) {
const labels = freezeArray(options?.labels);
const taint = freezeArray([
...options?.taint ?? [],
...labels
]);
const sources = freezeArray(options?.sources);
const policyContext = freezeObject(options?.policyContext);
return createDescriptor(labels, taint, sources, options?.capability, policyContext);
}
__name(makeSecurityDescriptor, "makeSecurityDescriptor");
function normalizeSecurityDescriptor(input) {
if (!input) {
return void 0;
}
const candidate = input;
const labels = candidate.labels;
const sources = candidate.sources;
const taint = candidate.taint;
const hasIterableLabels = Array.isArray(labels) && typeof labels.forEach === "function";
const hasIterableSources = Array.isArray(sources) && typeof sources.forEach === "function";
const hasIterableTaint = Array.isArray(taint) && typeof taint.forEach === "function";
if (hasIterableLabels && hasIterableSources && hasIterableTaint) {
return candidate;
}
const normalizedLabels = Array.isArray(labels) ? labels : labels !== void 0 && labels !== null ? [
labels
] : void 0;
const normalizedTaint = Array.isArray(taint) ? taint : taint !== void 0 && taint !== null ? [
taint
] : void 0;
const normalizedSources = Array.isArray(sources) ? sources : sources !== void 0 && sources !== null ? [
sources
] : void 0;
return makeSecurityDescriptor({
labels: normalizedLabels,
taint: normalizedTaint,
sources: normalizedSources,
capability: candidate.capability,
policyContext: candidate.policyContext
});
}
__name(normalizeSecurityDescriptor, "normalizeSecurityDescriptor");
function mergeDescriptors(...descriptors) {
const labelSet = /* @__PURE__ */ new Set();
const sourceSet = /* @__PURE__ */ new Set();
const taintSet = /* @__PURE__ */ new Set();
let capability;
let policyContext;
for (const incoming of descriptors) {
const descriptor = normalizeSecurityDescriptor(incoming);
if (!descriptor) continue;
descriptor.labels.forEach((label) => {
labelSet.add(label);
taintSet.add(label);
});
descriptor.taint.forEach((label) => taintSet.add(label));
descriptor.sources.forEach((source) => sourceSet.add(source));
if (descriptor.capability) {
capability = descriptor.capability;
}
if (descriptor.policyContext) {
policyContext = {
...policyContext ?? {},
...descriptor.policyContext
};
}
}
return createDescriptor(freezeArray(labelSet), freezeArray(taintSet), freezeArray(sourceSet), capability, freezeObject(policyContext));
}
__name(mergeDescriptors, "mergeDescriptors");
function hasLabel(descriptor, label) {
if (!descriptor) return false;
return descriptor.labels.includes(label);
}
__name(hasLabel, "hasLabel");
function serializeSecurityDescriptor(descriptor) {
if (!descriptor) return void 0;
return {
labels: Array.from(descriptor.labels),
taint: Array.from(descriptor.taint),
sources: Array.from(descriptor.sources),
capability: descriptor.capability,
policyContext: descriptor.policyContext ? {
...descriptor.policyContext
} : void 0
};
}
__name(serializeSecurityDescriptor, "serializeSecurityDescriptor");
function deserializeSecurityDescriptor(serialized) {
if (!serialized) return void 0;
return makeSecurityDescriptor({
labels: serialized.labels,
taint: serialized.taint,
sources: serialized.sources,
capability: serialized.capability,
policyContext: serialized.policyContext
});
}
__name(deserializeSecurityDescriptor, "deserializeSecurityDescriptor");
function createCapabilityContext(options) {
const policy = options.policy ?? options.descriptor.policyContext ?? void 0;
return Object.freeze({
kind: options.kind,
importType: options.importType,
labels: options.descriptor.labels,
taint: options.descriptor.taint,
sources: options.descriptor.sources,
policy: policy ? Object.freeze({
...policy
}) : void 0,
metadata: options.metadata ? Object.freeze({
...options.metadata
}) : void 0,
operation: options.operation ? Object.freeze({
...options.operation
}) : void 0,
security: options.descriptor
});
}
__name(createCapabilityContext, "createCapabilityContext");
function serializeCapabilityContext(context) {
if (!context) return void 0;
return {
kind: context.kind,
importType: context.importType,
labels: Array.from(context.labels),
taint: Array.from(context.taint),
sources: Array.from(context.sources),
policy: context.policy ? {
...context.policy
} : void 0,
metadata: context.metadata ? {
...context.metadata
} : void 0,
operation: context.operation ? {
...context.operation
} : void 0,
security: serializeSecurityDescriptor(context.security)
};
}
__name(serializeCapabilityContext, "serializeCapabilityContext");
function deserializeCapabilityContext(serialized) {
if (!serialized) return void 0;
const descriptor = deserializeSecurityDescriptor(serialized.security);
return createCapabilityContext({
kind: serialized.kind,
importType: serialized.importType,
descriptor,
metadata: serialized.metadata,
policy: serialized.policy,
operation: serialized.operation
});
}
__name(deserializeCapabilityContext, "deserializeCapabilityContext");
// core/utils/token-metrics.ts
var PROGRAMMING_EXTENSIONS = /* @__PURE__ */ new Set([
"py",
"js",
"ts",
"java",
"c",
"cpp",
"cs",
"go",
"rs"
]);
var NATURAL_LANGUAGE_EXTENSIONS = /* @__PURE__ */ new Set([
"md",
"txt",
"rst",
"adoc"
]);
var STRUCTURED_EXTENSIONS = /* @__PURE__ */ new Set([
"json",
"yaml",
"yml",
"toml",
"xml",
"html"
]);
var PROGRAMMING_RATIO = 3;
var NATURAL_LANGUAGE_RATIO = 4;
var STRUCTURED_RATIO = 5;
var DEFAULT_RATIO = 4;
function pickRatio(options) {
const ext = options?.extension?.toLowerCase();
const format = options?.format?.toLowerCase();
const language = options?.language?.toLowerCase();
if (ext && PROGRAMMING_EXTENSIONS.has(ext)) {
return PROGRAMMING_RATIO;
}
if (ext && NATURAL_LANGUAGE_EXTENSIONS.has(ext)) {
return NATURAL_LANGUAGE_RATIO;
}
if (ext && STRUCTURED_EXTENSIONS.has(ext)) {
return STRUCTURED_RATIO;
}
if (format === "json" || format === "yaml" || format === "xml") {
return STRUCTURED_RATIO;
}
if (language === "js" || language === "ts" || language === "py" || language === "go") {
return PROGRAMMING_RATIO;
}
return DEFAULT_RATIO;
}
__name(pickRatio, "pickRatio");
function estimateTokenCount(text, options) {
if (!text) {
return 0;
}
const ratio = pickRatio(options);
return Math.ceil(text.length / ratio);
}
__name(estimateTokenCount, "estimateTokenCount");
function buildTokenMetrics(text, options) {
const length = text.length;
const tokest = estimateTokenCount(text, options);
return {
length,
tokest,
tokens: void 0,
source: "estimate"
};
}
__name(buildTokenMetrics, "buildTokenMetrics");
// core/types/variable/VariableMetadata.ts
var EMPTY_LABELS = Object.freeze([]);
var _VariableMetadataUtils = class _VariableMetadataUtils {
/**
* Create consistent variable source metadata
*/
static createSource(syntax, hasInterpolation, isMultiLine, wrapperType) {
return {
directive: "var",
syntax,
wrapperType,
hasInterpolation,
isMultiLine
};
}
/**
* Merge metadata objects with proper precedence
* Additional metadata takes precedence over base metadata
*/
static mergeMetadata(base, additional) {
if (!base && !additional) {
return {};
}
if (!base) {
return additional || {};
}
if (!additional) {
return base;
}
return {
...base,
...additional
};
}
/**
* Validate metadata consistency
*/
static validateMetadata(metadata) {
if (metadata.isPipelineInput && typeof metadata.pipelineStage !== "undefined") {
return typeof metadata.pipelineStage === "number" && metadata.pipelineStage >= 0;
}
if (metadata.isImported && !metadata.importPath) {
return false;
}
return true;
}
/**
* Extract source information for debugging
*/
static extractSourceInfo(source) {
const parts = [
source.directive
];
if (source.syntax) {
parts.push(`syntax:${source.syntax}`);
}
if (source.wrapperType) {
parts.push(`wrapper:${source.wrapperType}`);
}
if (source.hasInterpolation) {
parts.push("interpolated");
}
if (source.isMultiLine) {
parts.push("multiline");
}
return parts.join(" ");
}
/**
* Check if metadata indicates a complex variable
*/
static isComplexVariable(metadata) {
return Boolean(metadata?.isComplex);
}
/**
* Check if metadata indicates an imported variable
*/
static isImportedVariable(metadata) {
return Boolean(metadata?.isImported);
}
/**
* Check if metadata indicates a pipeline input variable
*/
static isPipelineInputVariable(metadata) {
return Boolean(metadata?.isPipelineInput);
}
/**
* Create metadata for imported variables
*/
static createImportMetadata(importPath, additionalMetadata) {
const importMetadata = {
isImported: true,
importPath
};
return this.mergeMetadata(importMetadata, additionalMetadata);
}
/**
* Create metadata for pipeline input variables
*/
static createPipelineMetadata(pipelineStage, additionalMetadata) {
const pipelineMetadata = {
isPipelineInput: true,
pipelineStage
};
return this.mergeMetadata(pipelineMetadata, additionalMetadata);
}
/**
* Create metadata for complex variables
*/
static createComplexMetadata(additionalMetadata) {
const complexMetadata = {
isComplex: true
};
return this.mergeMetadata(complexMetadata, additionalMetadata);
}
/**
* Ensure metadata carries a security descriptor and optional capability context.
* When labels are provided, they override any existing descriptor.
* When no descriptor exists, a default descriptor is attached.
*/
static applySecurityMetadata(metadata, options) {
const result = {
...metadata ?? {}
};
const baseDescriptor = options?.existingDescriptor ?? result.security ?? makeSecurityDescriptor();
const overrideProvided = options?.labels || options?.taint || options?.sources || options?.policyContext || options?.capabilityKind;
const descriptor = overrideProvided ? makeSecurityDescriptor({
labels: options?.labels ?? baseDescriptor.labels,
taint: options?.taint ?? baseDescriptor.taint,
sources: options?.sources ?? baseDescriptor.sources,
capability: options?.capabilityKind ?? baseDescriptor.capability,
policyContext: {
...baseDescriptor.policyContext ?? {},
...options?.policyContext ?? {}
}
}) : baseDescriptor;
result.security = descriptor;
if (options?.capability) {
result.capability = options.capability.security === descriptor ? options.capability : createCapabilityContext({
kind: options.capability.kind,
importType: options.capability.importType,
descriptor,
metadata: options.capability.metadata ? {
...options.capability.metadata
} : void 0,
policy: options.capability.policy ? {
...options.capability.policy
} : void 0,
operation: options.capability.operation ? {
...options.capability.operation
} : void 0
});
} else if (result.capability) {
const existing = result.capability;
if (existing.security !== descriptor) {
result.capability = createCapabilityContext({
kind: existing.kind,
importType: existing.importType,
descriptor,
metadata: existing.metadata ? {
...existing.metadata
} : void 0,
policy: existing.policy ? {
...existing.policy
} : void 0,
operation: existing.operation ? {
...existing.operation
} : void 0
});
}
}
return result;
}
/**
* Serialize security-aware metadata fragments for persistence.
*/
static serializeSecurityMetadata(metadata) {
if (!metadata) {
return void 0;
}
const serializedSecurity = serializeSecurityDescriptor(metadata.security);
const serializedCapability = serializeCapabilityContext(metadata.capability);
if (!serializedSecurity && !serializedCapability) {
return void 0;
}
return {
security: serializedSecurity,
capability: serializedCapability
};
}
/**
* Deserialize persisted security metadata fragments.
*/
static deserializeSecurityMetadata(payload) {
if (!payload) {
return {};
}
const security = deserializeSecurityDescriptor(payload.security);
const capability = deserializeCapabilityContext(payload.capability);
return {
...security ? {
security
} : {},
...capability ? {
capability
} : {}
};
}
static applyTextMetrics(metadata, text, options) {
if (typeof text !== "string") {
return metadata;
}
const metrics = buildTokenMetrics(text, options);
if (!metadata) {
return {
metrics
};
}
return {
...metadata,
metrics: _VariableMetadataUtils.mergeMetrics(metadata.metrics, metrics)
};
}
static assignMetrics(variable, metrics) {
if (!variable.internal) {
variable.internal = {};
}
variable.internal.metrics = metrics;
if (!variable.mx) {
_VariableMetadataUtils.attachContext(variable);
}
if (variable.mx) {
variable.mx.length = metrics.length;
variable.mx.tokest = metrics.tokest;
variable.mx.tokens = metrics.tokens ?? metrics.tokest;
}
if (variable.internal.mxCache) {
variable.internal.mxCache = variable.mx;
}
}
static attachContext(variable) {
if (variable.__mxAttached) {
return variable;
}
if (!variable.internal) {
variable.internal = {};
}
const descriptor = Object.getOwnPropertyDescriptor(variable, "mx");
if (descriptor && !descriptor.get && !descriptor.set) {
Object.defineProperty(variable, "mx", {
value: variable.mx,
enumerable: false,
configurable: true,
writable: true
});
Object.defineProperty(variable, "__mxAttached", {
value: true,
enumerable: false,
configurable: false
});
return variable;
}
Object.defineProperty(variable, "__mxAttached", {
value: true,
enumerable: false,
configurable: false
});
Object.defineProperty(variable, "mx", {
enumerable: false,
configurable: true,
get() {
return _VariableMetadataUtils.buildVariableContext(variable);
}
});
return variable;
}
static buildVariableContext(variable) {
if (!variable.internal) {
variable.internal = {};
}
if (variable.internal.mxCache) {
return variable.internal.mxCache;
}
const metrics = _VariableMetadataUtils.computeMetricsForVariable(variable);
const descriptor = Object.getOwnPropertyDescriptor(variable, "mx");
const mxSnapshot = descriptor && !descriptor.get && descriptor.value ? descriptor.value : {};
const labels = normalizeLabelArray(mxSnapshot.labels);
const tokenValue = metrics?.tokens ?? metrics?.tokest ?? void 0;
const tokestValue = metrics?.tokest ?? metrics?.tokens ?? void 0;
const context = {
name: variable.name,
type: variable.type,
definedAt: variable.definedAt,
labels,
taint: mxSnapshot.taint ?? "unknown",
tokens: tokenValue,
tokest: tokestValue,
length: metrics?.length,
size: Array.isArray(variable.value) ? variable.value.length : void 0,
sources: mxSnapshot.sources ?? [],
exported: Boolean(mxSnapshot.exported),
policy: mxSnapshot.policy ?? null
};
if (variable.type === "array" && variable.internal) {
const aggregate = variable.internal.arrayHelperAggregate;
if (aggregate) {
const hasAggregateContexts = Array.isArray(aggregate.contexts) && aggregate.contexts.length > 0;
if (hasAggregateContexts) {
context.labels = aggregate.labels;
context.sources = aggregate.sources;
}
context.tokens = aggregate.tokens;
context.totalTokens = aggregate.totalTokens;
context.maxTokens = aggregate.maxTokens;
}
}
variable.internal.mxCache = context;
return context;
}
static computeMetricsForVariable(variable) {
if (variable.internal?.metrics) {
return variable.internal.metrics;
}
if (typeof variable.value === "string") {
const metrics = buildTokenMetrics(variable.value);
_VariableMetadataUtils.assignMetrics(variable, metrics);
return metrics;
}
const text = variable.value?.text;
if (typeof text === "string") {
const metrics = buildTokenMetrics(text);
_VariableMetadataUtils.assignMetrics(variable, metrics);
return metrics;
}
return void 0;
}
static mergeMetrics(existing, incoming) {
if (!existing) {
return incoming;
}
if (existing.source === "exact" && incoming.source !== "exact") {
return existing;
}
return {
...existing,
...incoming
};
}
};
__name(_VariableMetadataUtils, "VariableMetadataUtils");
var VariableMetadataUtils = _VariableMetadataUtils;
function normalizeLabelArray(labels) {
if (Array.isArray(labels)) {
return labels;
}
if (labels === void 0 || labels === null) {
return EMPTY_LABELS;
}
return [
labels
];
}
__name(normalizeLabelArray, "normalizeLabelArray");
var _VariableSourceHelpers = class _VariableSourceHelpers {
/**
* Create source for quoted text variables
*/
static createQuotedSource(wrapperType, hasInterpolation = false, isMultiLine = false) {
return VariableMetadataUtils.createSource("quoted", hasInterpolation, isMultiLine, wrapperType);
}
/**
* Create source for template variables
*/
static createTemplateSource(isMultiLine = false) {
return VariableMetadataUtils.createSource("template", true, isMultiLine, "backtick");
}
/**
* Create source for array variables
*/
static createArraySource(hasInterpolation = false, isMultiLine = false) {
return VariableMetadataUtils.createSource("array", hasInterpolation, isMultiLine, "brackets");
}
/**
* Create source for object variables
*/
static createObjectSource(hasInterpolation = false, isMultiLine = true) {
return VariableMetadataUtils.createSource("object", hasInterpolation, isMultiLine);
}
/**
* Create source for command variables
*/
static createCommandSource(isMultiLine = false) {
return VariableMetadataUtils.createSource("command", true, isMultiLine);
}
/**
* Create source for code variables
*/
static createCodeSource(isMultiLine = true) {
return VariableMetadataUtils.createSource("code", true, isMultiLine);
}
/**
* Create source for path variables
*/
static createPathSource(hasInterpolation = false) {
return VariableMetadataUtils.createSource("path", hasInterpolation, false, "brackets");
}
/**
* Create source for reference variables
*/
static createReferenceSource() {
return VariableMetadataUtils.createSource("reference", false, false);
}
};
__name(_VariableSourceHelpers, "VariableSourceHelpers");
var VariableSourceHelpers = _VariableSourceHelpers;
// core/types/variable/VarMxHelpers.ts
var EMPTY_LABELS2 = Object.freeze([]);
var EMPTY_SOURCES = Object.freeze([]);
function varMxToSecurityDescriptor(mx) {
return makeSecurityDescriptor({
labels: mx.labels ? [
...mx.labels
] : [],
taint: mx.taint ? [
...mx.taint
] : [],
sources: mx.sources ? [
...mx.sources
] : [],
policyContext: mx.policy ?? void 0
});
}
__name(varMxToSecurityDescriptor, "varMxToSecurityDescriptor");
function legacyMetadataToVarMx(metadata) {
const descriptor = normalizeSecurityDescriptor(metadata?.security) ?? makeSecurityDescriptor();
const metrics = metadata?.metrics;
const loadResult = metadata?.loadResult;
const mx = {
labels: descriptor.labels ? cloneArray(descriptor.labels) : EMPTY_LABELS2,
taint: descriptor.taint ? cloneArray(descriptor.taint) : [],
sources: descriptor.sources ? cloneArray(descriptor.sources) : EMPTY_SOURCES,
policy: descriptor.policyContext ?? null,
source: metadata?.source,
retries: metadata?.retries,
exported: Boolean(metadata?.isImported),
isImported: metadata?.isImported,
importPath: metadata?.importPath
};
applyFlattenedLoadMetadata(mx, metadata);
if (loadResult) {
flattenLoadResultToVarMx(mx, loadResult);
}
if (metrics?.tokest !== void 0 && mx.tokest === void 0) {
mx.tokest = metrics.tokest;
}
if (metrics?.tokens !== void 0 && mx.tokens === void 0) {
mx.tokens = metrics.tokens;
}
if (mx.tokens === void 0 && mx.tokest !== void 0) {
mx.tokens = mx.tokest;
}
if (metrics?.length !== void 0 && mx.length === void 0) {
mx.length = metrics.length;
}
const aggregate = metadata?.arrayHelperAggregate;
if (aggregate) {
mx.labels = aggregate.labels ?? mx.labels;
mx.sources = aggregate.sources ?? mx.sources;
mx.tokens = aggregate.tokens ?? mx.tokens;
mx.totalTokens = aggregate.totalTokens ?? mx.totalTokens;
mx.maxTokens = aggregate.maxTokens ?? mx.maxTokens;
}
return mx;
}
__name(legacyMetadataToVarMx, "legacyMetadataToVarMx");
function legacyMetadataToInternal(metadata) {
if (!metadata) {
return {};
}
const { security, metrics, mxCache, loadResult, source, retries, ...rest } = metadata;
return {
...rest
};
}
__name(legacyMetadataToInternal, "legacyMetadataToInternal");
function updateVarMxFromDescriptor(mx, descriptor) {
const normalized = normalizeSecurityDescriptor(descriptor) ?? makeSecurityDescriptor();
mx.labels = normalized.labels ? [
...normalized.labels
] : [];
mx.taint = normalized.taint ? [
...normalized.taint
] : [];
mx.sources = normalized.sources ? [
...normalized.sources
] : [];
mx.policy = normalized.policyContext ?? null;
}
__name(updateVarMxFromDescriptor, "updateVarMxFromDescriptor");
function hasSecurityVarMx(mx) {
return (mx.labels?.length ?? 0) > 0 || (mx.taint?.length ?? 0) > 0;
}
__name(hasSecurityVarMx, "hasSecurityVarMx");
function flattenLoadResultToVarMx(mx, loadResult) {
mx.filename = loadResult.filename;
mx.relative = loadResult.relative;
mx.absolute = loadResult.absolute;
mx.tokest = loadResult.tokest ?? mx.tokest;
mx.tokens = loadResult.tokens ?? mx.tokens;
mx.fm = loadResult.fm ?? mx.fm;
mx.json = loadResult.json ?? mx.json;
if (loadResult.url) {
mx.url = loadResult.url;
mx.domain = loadResult.domain ?? mx.domain;
mx.title = loadResult.title ?? mx.title;
mx.description = loadResult.description ?? mx.description;
}
}
__name(flattenLoadResultToVarMx, "flattenLoadResultToVarMx");
function cloneArray(value) {
if (!value || value.length === 0) {
return [];
}
return Object.freeze([
...value
]);
}
__name(cloneArray, "cloneArray");
function applyFlattenedLoadMetadata(mx, metadata) {
if (!metadata) {
return;
}
if (typeof metadata.filename === "string") {
mx.filename = metadata.filename;
}
if (typeof metadata.relative === "string") {
mx.relative = metadata.relative;
}
if (typeof metadata.absolute === "string") {
mx.absolute = metadata.absolute;
}
if (typeof metadata.url === "string") {
mx.url = metadata.url;
}
if (typeof metadata.domain === "string") {
mx.domain = metadata.domain;
}
if (typeof metadata.title === "string") {
mx.title = metadata.title;
}
if (typeof metadata.description === "string") {
mx.description = metadata.description;
}
if (typeof metadata.tokest === "number") {
mx.tokest = metadata.tokest;
}
if (typeof metadata.tokens === "number") {
mx.tokens = metadata.tokens;
}
if (metadata.fm !== void 0) {
mx.fm = metadata.fm;
}
if (metadata.json !== void 0) {
mx.json = metadata.json;
}
if (typeof metadata.length === "number") {
mx.length = metadata.length;
}
}
__name(applyFlattenedLoadMetadata, "applyFlattenedLoadMetadata");
// core/types/variable/ArrayHelpers.ts
var QUANTIFIER_EVALUATOR = "__mlldQuantifierEvaluator";
function createGuardInputHelper(inputs) {
const aggregate = buildArrayAggregate(inputs, {
nameHint: "__guard_input__"
});
const quantifiers = createQuantifierHelpers(aggregate.contexts);
return {
raw: inputs,
mx: {
labels: aggregate.labels,
taint: aggregate.taint,
tokens: aggregate.tokens,
sources: aggregate.sources,
totalTokens: aggregate.totalTokens,
maxTokens: aggregate.maxTokens
},
totalTokens: aggregate.totalTokens,
maxTokens: aggregate.maxTokens,
any: quantifiers.any,
all: quantifiers.all,
none: quantifiers.none
};
}
__name(createGuardInputHelper, "createGuardInputHelper");
function attachArrayHelpers(variable) {
if (variable.__arrayHelpersAttached) {
return;
}
Object.defineProperty(variable, "__arrayHelpersAttached", {
value: true,
enumerable: false,
configurable: false
});
const arrayValues = Array.isArray(variable.value) ? variable.value : [];
const aggregate = buildArrayAggregate(arrayValues, {
nameHint: variable.name ?? "__array_helper__"
});
const quantifiers = createQuantifierHelpers(aggregate.contexts);
const helperTargets = [
variable
];
if (Array.isArray(arrayValues)) {
helperTargets.push(arrayValues);
}
for (const target of helperTargets) {
defineHelperProperty(target, "raw", arrayValues);
defineHelperProperty(target, "any", quantifiers.any);
defineHelperProperty(target, "all", quantifiers.all);
defineHelperProperty(target, "none", quantifiers.none);
defineHelperProperty(target, "totalTokens", aggregate.totalTokens);
defineHelperProperty(target, "maxTokens", aggregate.maxTokens);
}
if (!variable.internal) {
variable.internal = {};
}
variable.internal.arrayHelperAggregate = aggregate;
const mx = ensureContext(variable);
const hasAggregateContexts = aggregate.contexts.length > 0;
if (hasAggregateContexts) {
mx.labels = aggregate.labels;
mx.taint = aggregate.taint;
mx.sources = aggregate.sources;
mx.tokens = aggregate.tokens;
} else if (!mx.tokens) {
mx.tokens = aggregate.tokens;
mx.taint = aggregate.taint;
}
mx.totalTokens = aggregate.totalTokens;
mx.maxTokens = aggregate.maxTokens;
if (Array.isArray(variable.value)) {
mx.size = variable.value.length;
}
if (variable.internal?.mxCache) {
variable.internal.mxCache = mx;
}
}
__name(attachArrayHelpers, "attachArrayHelpers");
function buildArrayAggregate(values, options) {
const nameHint = options?.nameHint ?? "__array_helper__";
const variables = values.map((value) => {
if (isVariableLike(value)) {
return value;
}
return materializeExpressionValue(value, {
name: nameHint
});
}).filter((value) => Boolean(value));
const contexts = variables.map(ensureContext);
const tokenValues = contexts.map((mx) => mx.tokens ?? mx.tokest ?? 0);
const tokens = Object.freeze(tokenValues.slice());
const labels = freezeArray2(contexts.flatMap((mx) => mx.labels ?? []));
const taint = freezeArray2(contexts.flatMap((mx) => mx.taint ?? mx.labels ?? []));
const sources = freezeArray2(contexts.flatMap((mx) => mx.sources ?? []));
const totalTokens = /* @__PURE__ */ __name(() => tokenValues.reduce((sum, value) => sum + (value || 0), 0), "totalTokens");
const maxTokens = /* @__PURE__ */ __name(() => tokenValues.reduce((max, value) => Math.max(max, value || 0), 0), "maxTokens");
return {
contexts,
labels,
taint,
sources,
tokens,
totalTokens,
maxTokens
};
}
__name(buildArrayAggregate, "buildArrayAggregate");
function createQuantifierHelpers(contexts) {
return {
any: createQuantifierHelper("any", contexts),
all: createQuantifierHelper("all", contexts),
none: createQuantifierHelper("none", contexts)
};
}
__name(createQuantifierHelpers, "createQuantifierHelpers");
function createQuantifierHelper(type, contexts) {
const evaluate = /* @__PURE__ */ __name((predicate) => {
switch (type) {
case "any":
return contexts.some(predicate);
case "all":
return contexts.every(predicate);
case "none":
return contexts.every((mx) => !predicate(mx));
}
}, "evaluate");
const labelsHelper = {
includes(label) {
return evaluate((mx) => (mx.labels ?? []).includes(label));
}
};
attachQuantifierEvaluator(labelsHelper, (method, args) => {
if (method === "includes") {
const label = args[0];
return labelsHelper.includes(label);
}
return false;
});
const tokensHelper = {
some(predicate) {
return evaluate((mx) => predicate(mx.tokens ?? mx.tokest ?? 0));
},
greaterThan(value) {
return evaluate((mx) => (mx.tokens ?? mx.tokest ?? 0) > value);
}
};
const taintHelper = {
includes(label) {
return evaluate((mx) => (mx.taint ?? mx.labels ?? []).includes(label));
}
};
attachQuantifierEvaluator(taintHelper, (method, args) => {
if (method === "includes") {
const label = args[0];
return taintHelper.includes(label);
}
return false;
});
attachQuantifierEvaluator(tokensHelper, (method, args) => {
switch (method) {
case "some": {
const predicate = args[0];
return tokensHelper.some(predicate);
}
case "greaterThan": {
const value = Number(args[0]);
return tokensHelper.greaterThan(value);
}
default:
return false;
}
});
return {
mx: {
labels: labelsHelper,
taint: taintHelper,
tokens: tokensHelper
}
};
}
__name(createQuantifierHelper, "createQuantifierHelper");
function ensureContext(variable) {
if (!("mx" in variable) || !variable.mx) {
VariableMetadataUtils.attachContext(variable);
}
return variable.mx;
}
__name(ensureContext, "ensureContext");
function isVariableLike(value) {
return Boolean(value && typeof value === "object" && "type" in value && "value" in value);
}
__name(isVariableLike, "isVariableLike");
function defineHelperProperty(target, key, value) {
Object.defineProperty(target, key, {
value,
enumerable: false,
configurable: true
});
}
__name(defineHelperProperty, "defineHelperProperty");
function freezeArray2(values) {
return Object.freeze(Array.from(new Set(values)));
}
__name(freezeArray2, "freezeArray");
function attachQuantifierEvaluator(target, evaluator) {
Object.defineProperty(target, QUANTIFIER_EVALUATOR, {
value: evaluator,
enumerable: false,
configurable: false
});
}
__name(attachQuantifierEvaluator, "attachQuantifierEvaluator");
// core/types/variable/VariableFactories.ts
function finalizeVariable(variable) {
const legacyMetadata = variable.metadata;
if (!variable.mx) {
variable.mx = legacyMetadata ? legacyMetadataToVarMx(legacyMetadata) : {
labels: [],
taint: [],
sources: [],
policy: null
};
}
if (variable.mx) {
variable.mx.name = variable.name;
variable.mx.type = variable.type;
if (variable.definedAt) {
variable.mx.definedAt = variable.definedAt;
}
}
if (!variable.internal) {
variable.internal = {};
}
if ("metadata" in variable) {
delete variable.metadata;
}
return VariableMetadataUtils.attachContext(variable);
}
__name(finalizeVariable, "finalizeVariable");
function applyTextMetrics(metadata, text, options) {
return VariableMetadataUtils.applyTextMetrics(metadata, text, options);
}
__name(applyTextMetrics, "applyTextMetrics");
function normalizeFactoryOptions(metadataOrOptions, text, tokenOptions) {
const { legacyMetadata, mxOverrides, internalOverrides } = extractFactoryInput(metadataOrOptions);
const metadata = applyTextMetrics(legacyMetadata, text, tokenOptions);
const mx = Object.assign(metadata ? legacyMetadataToVarMx(metadata) : {
labels: [],
taint: [],
sources: [],
policy: null
}, mxOverrides);
const internal = Object.assign(metadata ? legacyMetadataToInternal(metadata) : {}, internalOverrides);
return {
metadata,
mx,
internal
};
}
__name(normalizeFactoryOptions, "normalizeFactoryOptions");
function enrichStructuredMetadata(metadataOrOptions, structuredValue) {
const structuredFields = {
isStructuredValue: true,
structuredValueType: structuredValue.type
};
if (!metadataOrOptions) {
return {
metadata: structuredFields
};
}
if (isFactoryInitOptions(metadataOrOptions)) {
return {
...metadataOrOptions,
metadata: {
...metadataOrOptions.metadata ?? {},
...structuredFields
}
};
}
return {
...metadataOrOptions,
...structuredFields
};
}
__name(enrichStructuredMetadata, "enrichStructuredMetadata");
function extractFactoryInput(metadataOrOptions) {
if (!metadataOrOptions) {
return {};
}
if (isFactoryInitOptions(metadataOrOptions)) {
return {
legacyMetadata: metadataOrOptions.metadata,
mxOverrides: metadataOrOptions.mx,
internalOverrides: metadataOrOptions.internal
};
}
return {
legacyMetadata: metadataOrOptions
};
}
__name(extractFactoryInput, "extractFactoryInput");
function isFactoryInitOptions(value) {
if (!value || typeof value !== "object") {
return false;
}
return "mx" in value || "internal" in value || "metadata" in value;
}
__name(isFactoryInitOptions, "isFactoryInitOptions");
function createSimpleTextVariable(name, value, source, metadataOrOptions) {
return VariableFactory.createSimpleText(name, value, source, metadataOrOptions);
}
__name(createSimpleTextVariable, "createSimpleTextVariable");
function createInterpolatedTextVariable(name, value, interpolationPoints, source, metadataOrOptions) {
return VariableFactory.createInterpolatedText(name, value, interpolationPoints, source, metadataOrOptions);
}
__name(createInterpolatedTextVariable, "createInterpolatedTextVariable");
function createTemplateVariable(name, value, parameters, templateSyntax, source, metadataOrOptions) {
return VariableFactory.createTemplate(name, value, parameters, templateSyntax, source, metadataOrOptions);
}
__name(createTemplateVariable, "createTemplateVariable");
function createFileContentVariable(name, value, filePath, source, metadataOrOptions) {
return VariableFactory.createFileContent(name, value, filePath, source, void 0, metadataOrOptions);
}
__name(createFileContentVariable, "createFileContentVariable");
function createSectionContentVariable(name, value, filePath, sectionName, sectionSyntax, source, metadataOrOptions) {
return VariableFactory.createSectionContent(name, value, filePath, sectionName, sectionSyntax, source, metadataOrOptions);
}
__name(createSectionContentVariable, "createSectionContentVariable");
function createObjectVariable(name, value, isComplex, source, metadataOrOptions) {
return VariableFactory.createObject(name, value, isComplex, source, metadataOrOptions);
}
__name(createObjectVariable, "createObjectVariable");
function createArrayVariable(name, value, isComplex, source, metadataOrOptions) {
return VariableFactory.createArray(name, value, isComplex, source, metadataOrOptions);
}
__name(createArrayVariable, "createArrayVariable");
function createComputedVariable(name, value, language, sourceCode, source, metadataOrOptions) {
return VariableFactory.createComputed(name, value, language, sourceCode, source, metadataOrOptions);
}
__name(createComputedVariable, "createComputedVariable");
function createCommandResultVariable(name, value, command, source, exitCode, stderr, metadataOrOptions) {
return VariableFactory.createCommandResult(name, value, command, source, exitCode, stderr, metadataOrOptions);
}
__name(createCommandResultVariable, "createCommandResultVariable");
function createPathVariable(name, resolvedPath, originalPath, isURL, isAbsolute, source, metadataOrOptions) {
return VariableFactory.createPath(name, resolvedPath, originalPath, isURL, isAbsolute, source, metadataOrOptions);
}
__name(createPathVariable, "createPathVariable");
function createImportedVariable(name, value, originalType, importPath, isModule, variableName, source, metadataOrOptions) {
return VariableFactory.createImported(name, value, originalType, importPath, isModule, variableName, source, metadataOrOptions);
}
__name(createImportedVariable, "createImportedVariable");
function createExecutableVariable(name, type, template, paramNames, language, source, metadataOrOptions) {
return VariableFactory.createExecutable(name, type, template, paramNames, language, source, metadataOrOptions);
}
__name(createExecutableVariable, "createExecutableVariable");
function createPipelineInputVariable(name, value, format, rawText, source, pipelineStage) {
return VariableFactory.createPipelineInput(name, value, format, rawText, source, pipelineStage);
}
__name(createPipelineInputVariable, "createPipelineInputVariable");
function createStructuredValueVariable(name, value, source, metadataOrOptions) {
return VariableFactory.createStructuredValue(name, value, source, metadataOrOptions);
}
__name(createStructuredValueVariable, "createStructuredValueVariable");
function createPrimitiveVariable(name, value, source, metadataOrOptions) {
return VariableFactory.createPrimitive(name, value, source, metadataOrOptions);
}
__name(createPrimitiveVariable, "createPrimitiveVariable");
var _VariableFactory = class _VariableFactory {
// =========================================================================
// TEXT VARIABLE FACTORIES
// =========================================================================
/**
* Create a SimpleTextVariable
*/
static createSimpleText(name, value, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions, value);
return finalizeVariable({
type: "simple-text",
name,
value,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create an InterpolatedTextVariable
*/
static createInterpolatedText(name, value, interpolationPoints, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions, value);
return finalizeVariable({
type: "interpolated-text",
name,
value,
interpolationPoints,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create a TemplateVariable
*/
static createTemplate(name, value, parameters, templateSyntax, source, metadataOrOptions) {
const textValue = typeof value === "string" ? value : void 0;
const init = normalizeFactoryOptions(metadataOrOptions, textValue);
return finalizeVariable({
type: "template",
name,
value,
parameters,
templateSyntax,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
// =========================================================================
// CONTENT VARIABLE FACTORIES
// =========================================================================
/**
* Create a FileContentVariable
*/
static createFileContent(name, value, filePath, source, encoding, metadataOrOptions) {
const extensionMatch = filePath.match(/\.([a-zA-Z0-9]+)$/);
const extension = extensionMatch ? extensionMatch[1].toLowerCase() : void 0;
const init = normalizeFactoryOptions(metadataOrOptions, value, {
extension
});
return finalizeVariable({
type: "file-content",
name,
value,
filePath,
encoding,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create a SectionContentVariable
*/
static createSectionContent(name, value, filePath, sectionName, sectionSyntax, source, metadataOrOptions) {
const extensionMatch = filePath.match(/\.([a-zA-Z0-9]+)$/);
const extension = extensionMatch ? extensionMatch[1].toLowerCase() : void 0;
const init = normalizeFactoryOptions(metadataOrOptions, value, {
extension
});
return finalizeVariable({
type: "section-content",
name,
value,
filePath,
sectionName,
sectionSyntax,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
// =========================================================================
// STRUCTURED DATA FACTORIES
// =========================================================================
/**
* Create an ObjectVariable
*/
static createObject(name, value, isComplex, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions);
return finalizeVariable({
type: "object",
name,
value,
isComplex,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create an ArrayVariable
*/
static createArray(name, value, isComplex, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions);
const arrayVariable = finalizeVariable({
type: "array",
name,
value,
isComplex,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
attachArrayHelpers(arrayVariable);
return arrayVariable;
}
// =========================================================================
// COMPUTED VARIABLE FACTORIES
// =========================================================================
/**
* Create a ComputedVariable
*/
static createComputed(name, value, language, sourceCode, source, metadataOrOptions) {
const textValue = typeof value === "string" ? value : void 0;
const init = normalizeFactoryOptions(metadataOrOptions, textValue);
return finalizeVariable({
type: "computed",
name,
value,
language,
sourceCode,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create a CommandResultVariable
*/
static createCommandResult(name, value, command, source, exitCode, stderr, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions, value);
return finalizeVariable({
type: "command-result",
name,
value,
command,
exitCode,
stderr,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
// =========================================================================
// SPECIAL VARIABLE FACTORIES
// =========================================================================
/**
* Create a PathVariable
*/
static createPath(name, resolvedPath, originalPath, isURL, isAbsolute, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions);
return finalizeVariable({
type: "path",
name,
value: {
resolvedPath,
originalPath,
isURL,
isAbsolute
},
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create an ImportedVariable
*/
static createImported(name, value, originalType, importPath, isModule, variableName, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions);
return finalizeVariable({
type: "imported",
name,
value,
originalType,
importSource: {
path: importPath,
isModule,
variableName
},
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
/**
* Create an ExecutableVariable
*/
static createExecutable(name, type, template, paramNames, language, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions);
const executableDefinition = {
type,
template,
language
};
const variable = finalizeVariable({
type: "executable",
name,
value: executableDefinition,
paramNames,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
if (init.internal?.executableDef === void 0) {
variable.internal = {
...variable.internal ?? {},
executableDef: executableDefinition
};
}
return variable;
}
/**
* Create a PipelineInputVariable
*/
static createPipelineInput(name, value, format, rawText, source, pipelineStage) {
const baseMetadata = {
isPipelineInput: true,
pipelineStage
};
const init = normalizeFactoryOptions({
metadata: baseMetadata
}, rawText, {
format
});
return finalizeVariable({
type: "pipeline-input",
name,
value,
format,
rawText,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal ?? baseMetadata
});
}
/**
* Create a StructuredValueVariable
*/
static createStructuredValue(name, value, source, metadataOrOptions) {
const structuredValue = ensureStructuredValue(value);
const enrichedInput = enrichStructuredMetadata(metadataOrOptions, structuredValue);
const baseMetadata = isFactoryInitOptions(enrichedInput) ? enrichedInput.metadata : enrichedInput;
const mergedMetadata = VariableMetadataUtils.mergeMetadata(structuredValue.metadata, baseMetadata);
const securityAwareMetadata = VariableMetadataUtils.applySecurityMetadata(mergedMetadata);
structuredValue.metadata = securityAwareMetadata;
if (securityAwareMetadata?.security) {
applySecurityDescriptorToStructuredValue(structuredValue, securityAwareMetadata.security);
}
const init = normalizeFactoryOptions(isFactoryInitOptions(enrichedInput) ? {
...enrichedInput,
metadata: securityAwareMetadata
} : securityAwareMetadata, structuredValue.text);
return finalizeVariable({
type: "structured",
name,
value: structuredValue,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal ?? securityAwareMetadata
});
}
/**
* Create a PrimitiveVariable
*/
static createPrimitive(name, value, source, metadataOrOptions) {
const init = normalizeFactoryOptions(metadataOrOptions, typeof value === "string" ? value : void 0);
return finalizeVariable({
type: "primitive",
name,
value,
primitiveType: value === null ? "null" : typeof value,
source,
createdAt: Date.now(),
modifiedAt: Date.now(),
mx: init.mx,
internal: init.internal
});
}
// =========================================================================
// CONVENIENCE FACTORY METHODS
// =========================================================================
/**
* Create a variable with auto-detected type based on value
*/
static createAutoTyped(name, value, source, metadataOrOptions) {
if (typeof value === "string") {
return this.createSimpleText(name, value, source, metadataOrOptions);
} else if (typeof value === "number" || typeof value === "boolean" || value === null) {
return this.createPrimitive(name, value, source, metadataOrOptions);
} else if (Array.isArray(value)) {
return this.createArray(name, value, false, source, metadataOrOptions);
} else if (typeof value === "object" && value !== null) {
return this.createObject(name, value, false, source, metadataOrOptions);
} else {
return this.createSimpleText(name, String(value), source, metadataOrOptions);
}
}
/**
* Create a template variable with auto-detec