mlld
Version:
mlld: a modular prompt scripting language
224 lines (222 loc) • 8.07 kB
JavaScript
import { evaluateCondition, evaluate } from './chunk-FYMHT7UG.mjs';
import { logger } from './chunk-XGMRAGIT.mjs';
import { MlldWhenExpressionError } from './chunk-YMCO2JI3.mjs';
import { __name } from './chunk-OMKLS24H.mjs';
// interpreter/eval/when-expression.ts
async function evaluateWhenExpression(node, env, context) {
const errors = [];
if (node.conditions.length === 0) {
return {
value: null,
env
};
}
const hasAnyAction = node.conditions.some((c) => c.action && c.action.length > 0);
if (!hasAnyAction) {
logger.warn("WhenExpression has no actions defined");
return {
value: null,
env
};
}
for (let i = 0; i < node.conditions.length; i++) {
const pair = node.conditions[i];
if (pair.action && pair.action.length > 0) {
const hasCodeExecution = pair.action.some((actionNode) => {
if (typeof actionNode === "object" && actionNode !== null && "type" in actionNode) {
return actionNode.type === "code" || actionNode.type === "command" || actionNode.type === "nestedDirective" && actionNode.directive === "run";
}
return false;
});
if (hasCodeExecution) {
throw new MlldWhenExpressionError("Code blocks are not supported in when expressions. Define your logic in a separate /exe function and call it instead.", node.location, {
conditionIndex: i,
phase: "action",
type: "code-block-not-supported"
});
}
}
}
for (let i = 0; i < node.conditions.length; i++) {
const pair = node.conditions[i];
try {
const conditionResult = await evaluateCondition(pair.condition, env);
if (process.env.DEBUG_WHEN) {
logger.debug("WhenExpression condition result:", {
index: i,
conditionResult,
hasAction: !!(pair.action && pair.action.length > 0)
});
}
if (conditionResult) {
if (!pair.action || pair.action.length === 0) {
return {
value: null,
env
};
}
try {
const actionResult = await evaluate(pair.action, env, {
...context,
isExpression: true
// Mark as expression context
});
let value = actionResult.value;
if (node.withClause && node.withClause.pipes) {
value = await applyTailModifiers(value, node.withClause.pipes, env);
}
return {
value,
env
};
} catch (actionError) {
throw new MlldWhenExpressionError(`Error evaluating action for condition ${i + 1}: ${actionError.message}`, node.location, {
conditionIndex: i,
phase: "action",
originalError: actionError
});
}
}
} catch (conditionError) {
errors.push(new MlldWhenExpressionError(`Error evaluating condition ${i + 1}: ${conditionError.message}`, node.location, {
conditionIndex: i,
phase: "condition",
originalError: conditionError
}));
}
}
if (errors.length > 0) {
throw new MlldWhenExpressionError(`When expression evaluation failed with ${errors.length} condition errors`, node.location, {
errors
});
}
return {
value: null,
env
};
}
__name(evaluateWhenExpression, "evaluateWhenExpression");
async function applyTailModifiers(value, pipes, env) {
let result = value;
for (const pipe of pipes) {
const pipeEnv = env.createChild();
const { createPipelineInputVariable } = await import('./variable-MSOSGPF7.mjs');
const pipelineInput = {
text: String(result),
data: result,
toString: /* @__PURE__ */ __name(() => String(result), "toString")
};
const pipelineVar = createPipelineInputVariable("_pipelineInput", pipelineInput, "text", String(result), {
directive: "var",
syntax: "reference",
hasInterpolation: false,
isMultiLine: false
}, 0);
pipeEnv.setVariable("_pipelineInput", pipelineVar);
const pipeResult = await evaluate(pipe, pipeEnv);
result = pipeResult.value;
}
return result;
}
__name(applyTailModifiers, "applyTailModifiers");
async function createLazyWhenExpressionVariable(name, definition, env, parameterNames) {
const { createWhenExpressionVariable } = await import('./variable-MSOSGPF7.mjs');
const source = {
directive: "var",
syntax: "template",
hasInterpolation: true,
isMultiLine: true
// typically multiline
};
const metadata = {
isEvaluated: false,
conditionCount: definition.conditions.length,
hasParameters: !!parameterNames && parameterNames.length > 0,
parameterNames: parameterNames || []
};
return createWhenExpressionVariable(name, definition, source, metadata);
}
__name(createLazyWhenExpressionVariable, "createLazyWhenExpressionVariable");
async function evaluateWhenExpressionVariable(variable, env, args) {
if (variable.metadata.isEvaluated && !variable.metadata.hasParameters) {
return variable.value;
}
let evalEnv = env;
if (variable.metadata.hasParameters && args) {
evalEnv = env.createChild();
const paramNames = variable.metadata.parameterNames || [];
const { createSimpleTextVariable, createPrimitiveVariable } = await import('./variable-MSOSGPF7.mjs');
for (let i = 0; i < paramNames.length; i++) {
const paramName = paramNames[i];
const paramValue = args[i] ?? null;
let paramVar;
if (typeof paramValue === "string") {
paramVar = createSimpleTextVariable(paramName, paramValue, {
directive: "var",
syntax: "quoted",
hasInterpolation: false,
isMultiLine: false
});
} else if (typeof paramValue === "number" || typeof paramValue === "boolean" || paramValue === null) {
paramVar = createPrimitiveVariable(paramName, paramValue, {
directive: "var",
syntax: "quoted",
hasInterpolation: false,
isMultiLine: false
});
} else {
paramVar = createSimpleTextVariable(paramName, JSON.stringify(paramValue), {
directive: "var",
syntax: "quoted",
hasInterpolation: false,
isMultiLine: false
});
}
evalEnv.setVariable(paramName, paramVar);
}
}
const result = await evaluateWhenExpression(variable.definition, evalEnv);
if (!variable.metadata.hasParameters) {
variable.value = result.value;
variable.metadata.isEvaluated = true;
variable.metadata.evaluatedAt = /* @__PURE__ */ new Date();
}
return result.value;
}
__name(evaluateWhenExpressionVariable, "evaluateWhenExpressionVariable");
async function peekWhenExpressionType(node, env) {
const actionTypes = /* @__PURE__ */ new Set();
for (const pair of node.conditions) {
if (pair.action && pair.action.length > 0) {
const firstNode = pair.action[0];
if (firstNode.type === "Text") {
actionTypes.add("simple-text");
} else if (firstNode.type === "Literal") {
const literal = firstNode;
if (typeof literal.value === "number") {
actionTypes.add("primitive");
} else if (typeof literal.value === "boolean") {
actionTypes.add("primitive");
} else if (literal.value === null) {
actionTypes.add("primitive");
}
} else if (firstNode.type === "object") {
actionTypes.add("object");
} else if (firstNode.type === "array") {
actionTypes.add("array");
} else if (firstNode.type === "Directive") {
actionTypes.add("computed");
} else {
actionTypes.add("computed");
}
}
}
if (actionTypes.size === 1) {
return Array.from(actionTypes)[0];
}
return "computed";
}
__name(peekWhenExpressionType, "peekWhenExpressionType");
export { createLazyWhenExpressionVariable, evaluateWhenExpression, evaluateWhenExpressionVariable, peekWhenExpressionType };
//# sourceMappingURL=when-expression-O2PEVPZS.mjs.map
//# sourceMappingURL=when-expression-O2PEVPZS.mjs.map