UNPKG

mlld

Version:

mlld: llm scripting language

1,010 lines (1,004 loc) 30.9 kB
import { __name } from './chunk-NJQWMXLH.mjs'; // core/types/variable/VariableMetadata.ts 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); } }; __name(_VariableMetadataUtils, "VariableMetadataUtils"); var VariableMetadataUtils = _VariableMetadataUtils; 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, // Templates always have interpolation 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, // Commands typically have interpolation isMultiLine ); } /** * Create source for code variables */ static createCodeSource(isMultiLine = true) { return VariableMetadataUtils.createSource( "code", true, // Code may have interpolation isMultiLine ); } /** * Create source for path variables */ static createPathSource(hasInterpolation = false) { return VariableMetadataUtils.createSource( "path", hasInterpolation, false, // Paths are typically single line "brackets" ); } /** * Create source for reference variables */ static createReferenceSource() { return VariableMetadataUtils.createSource( "reference", false, // References don't have interpolation false ); } }; __name(_VariableSourceHelpers, "VariableSourceHelpers"); var VariableSourceHelpers = _VariableSourceHelpers; // core/types/variable/TypeGuards.ts function isSimpleText(variable) { return variable.type === "simple-text"; } __name(isSimpleText, "isSimpleText"); function isInterpolatedText(variable) { return variable.type === "interpolated-text"; } __name(isInterpolatedText, "isInterpolatedText"); function isTemplate(variable) { return variable.type === "template"; } __name(isTemplate, "isTemplate"); function isFileContent(variable) { return variable.type === "file-content"; } __name(isFileContent, "isFileContent"); function isSectionContent(variable) { return variable.type === "section-content"; } __name(isSectionContent, "isSectionContent"); function isObject(variable) { return variable.type === "object"; } __name(isObject, "isObject"); function isArray(variable) { return variable.type === "array"; } __name(isArray, "isArray"); function isComputed(variable) { return variable.type === "computed"; } __name(isComputed, "isComputed"); function isCommandResult(variable) { return variable.type === "command-result"; } __name(isCommandResult, "isCommandResult"); function isPath(variable) { return variable.type === "path"; } __name(isPath, "isPath"); function isImported(variable) { return variable.type === "imported"; } __name(isImported, "isImported"); function isExecutable(variable) { return variable.type === "executable"; } __name(isExecutable, "isExecutable"); function isPipelineInput(variable) { return variable.type === "pipeline-input"; } __name(isPipelineInput, "isPipelineInput"); function isStructuredValueVariable(variable) { return variable.type === "structured"; } __name(isStructuredValueVariable, "isStructuredValueVariable"); function isPrimitive(variable) { return variable.type === "primitive"; } __name(isPrimitive, "isPrimitive"); function isTextLike(variable) { return isSimpleText(variable) || isInterpolatedText(variable) || isTemplate(variable) || isFileContent(variable) || isSectionContent(variable) || isCommandResult(variable); } __name(isTextLike, "isTextLike"); function isStructured(variable) { return isObject(variable) || isArray(variable) || isStructuredValueVariable(variable); } __name(isStructured, "isStructured"); function isExternal(variable) { return isFileContent(variable) || isSectionContent(variable) || isImported(variable) || isCommandResult(variable) || isComputed(variable); } __name(isExternal, "isExternal"); var _VariableTypeGuards = class _VariableTypeGuards { /** * Check if variable has a specific type discriminator */ static hasType(variable, type) { return variable.type === type; } /** * Get all type discriminators that match the variable */ static getMatchingTypes(variable) { const types = [variable.type]; if (this.isTextLike(variable)) { types.push("simple-text"); } if (this.isStructured(variable)) { types.push("object"); } if (this.isExternal(variable)) { types.push("imported"); } return types; } /** * Validate that a variable matches expected type */ static validateType(variable, expectedType) { return variable.type === expectedType; } /** * Check if variable is of any of the specified types */ static isAnyType(variable, types) { return types.includes(variable.type); } /** * Filter variables by type */ static filterByType(variables, guard) { return variables.filter(guard); } /** * Find first variable of specified type */ static findByType(variables, guard) { return variables.find(guard); } /** * Count variables by type */ static countByType(variables, type) { return variables.filter((v) => v.type === type).length; } /** * Group variables by their types */ static groupByType(variables) { const groups = /* @__PURE__ */ new Map(); for (const variable of variables) { const existing = groups.get(variable.type) || []; existing.push(variable); groups.set(variable.type, existing); } return groups; } }; __name(_VariableTypeGuards, "VariableTypeGuards"); // Individual type guards _VariableTypeGuards.isSimpleText = isSimpleText; _VariableTypeGuards.isInterpolatedText = isInterpolatedText; _VariableTypeGuards.isTemplate = isTemplate; _VariableTypeGuards.isFileContent = isFileContent; _VariableTypeGuards.isSectionContent = isSectionContent; _VariableTypeGuards.isObject = isObject; _VariableTypeGuards.isArray = isArray; _VariableTypeGuards.isComputed = isComputed; _VariableTypeGuards.isCommandResult = isCommandResult; _VariableTypeGuards.isPath = isPath; _VariableTypeGuards.isImported = isImported; _VariableTypeGuards.isExecutable = isExecutable; _VariableTypeGuards.isPipelineInput = isPipelineInput; _VariableTypeGuards.isStructuredValue = isStructuredValueVariable; _VariableTypeGuards.isPrimitive = isPrimitive; // Composite type guards _VariableTypeGuards.isTextLike = isTextLike; _VariableTypeGuards.isStructured = isStructured; _VariableTypeGuards.isExternal = isExternal; var VariableTypeGuards = _VariableTypeGuards; var _FastTypeGuards = class _FastTypeGuards { /** * Fast check for text types (most common) */ static isAnyTextType(variable) { const type = variable.type; return type === "simple-text" || type === "interpolated-text" || type === "template" || type === "file-content" || type === "section-content" || type === "command-result"; } /** * Fast check for data types */ static isAnyDataType(variable) { const type = variable.type; return type === "object" || type === "array" || type === "computed" || type === "primitive"; } /** * Fast check for complex types that might need special handling */ static isComplexType(variable) { const type = variable.type; return type === "imported" || type === "executable" || type === "pipeline-input" || type === "path"; } /** * Check if variable type requires lazy evaluation */ static requiresLazyEvaluation(variable) { return isTemplate(variable) || isPipelineInput(variable) || isImported(variable) && variable.metadata?.isComplex; } }; __name(_FastTypeGuards, "FastTypeGuards"); var FastTypeGuards = _FastTypeGuards; // core/types/variable/AdvancedTypeDetection.ts var _AdvancedTypeDetection = class _AdvancedTypeDetection { /** * Get the effective type of a variable, considering imported variables * This resolves the "real" type by looking through import wrappers */ static getEffectiveType(variable) { if (isImported(variable)) { return variable.originalType; } return variable.type; } /** * Check if variable is an executable, including imported executables * This handles the complex case where an executable might be imported */ static isExecutableVariable(variable) { if (isExecutable(variable)) return true; if (isImported(variable)) { const imported = variable; return imported.originalType === "executable" || imported.metadata?.originalType === "executable"; } return false; } /** * Detect if variable contains nested directive complexity * This is used to determine if a variable needs special handling */ static detectComplexVariable(variable) { if (variable.metadata?.isComplex) { return true; } switch (variable.type) { case "object": case "array": return variable.isComplex || false; case "template": return Array.isArray(variable.value); // Lazy-evaluated templates are complex case "imported": return this.detectComplexVariable(this.resolveImportedVariable(variable)); case "pipeline-input": return true; // Pipeline inputs are inherently complex default: return false; } } /** * Follow import chains to resolve the original variable * This handles nested imports and circular references */ static resolveImportChain(variable, maxDepth = 10) { let current = variable; let depth = 0; const seen = /* @__PURE__ */ new Set(); while (isImported(current) && depth < maxDepth) { const imported = current; const key = `${imported.importSource.path}:${imported.importSource.variableName}`; if (seen.has(key)) { break; } seen.add(key); break; } return current; } /** * Resolve an imported variable to its underlying type */ static resolveImportedVariable(variable) { if (!isImported(variable)) { return variable; } const imported = variable; return { type: imported.originalType, name: imported.name, value: imported.value, source: imported.source, createdAt: imported.createdAt, modifiedAt: imported.modifiedAt, metadata: imported.metadata }; } /** * Detect if variable type matches a pattern */ static matchesTypePattern(variable, pattern) { const effectiveType = this.getEffectiveType(variable); if (pattern instanceof RegExp) { return pattern.test(effectiveType); } if (Array.isArray(pattern)) { return pattern.includes(effectiveType); } return effectiveType === pattern; } /** * Check if variable requires special evaluation handling */ static requiresSpecialEvaluation(variable) { const effectiveType = this.getEffectiveType(variable); return effectiveType === "template" || effectiveType === "executable" || effectiveType === "pipeline-input" || effectiveType === "computed" || this.detectComplexVariable(variable); } /** * Determine evaluation priority for variable resolution * Lower numbers = higher priority */ static getEvaluationPriority(variable) { const effectiveType = this.getEffectiveType(variable); switch (effectiveType) { case "primitive": case "simple-text": return 1; // Highest priority - simple values case "interpolated-text": case "file-content": case "section-content": return 2; // Medium-high priority - needs resolution but straightforward case "object": case "array": return variable.isComplex ? 4 : 3; // Priority based on complexity case "command-result": case "computed": return 5; // Lower priority - requires execution case "template": case "executable": return 6; // Low priority - complex evaluation case "pipeline-input": return 7; // Lowest priority - pipeline dependency case "imported": const resolved = this.resolveImportedVariable(variable); return this.getEvaluationPriority(resolved) + 1; // Slightly lower than original default: return 5; } } /** * Check if variable has lazy evaluation semantics */ static hasLazyEvaluation(variable) { const effectiveType = this.getEffectiveType(variable); return effectiveType === "template" || effectiveType === "pipeline-input" || effectiveType === "executable" && variable.type !== "imported" || this.detectComplexVariable(variable); } /** * Detect if variable contains interpolation points */ static hasInterpolation(variable) { switch (variable.type) { case "interpolated-text": return variable.interpolationPoints.length > 0; case "template": return true; // Templates always have interpolation case "imported": return this.hasInterpolation(this.resolveImportedVariable(variable)); default: return variable.source.hasInterpolation; } } /** * Get variable dependencies for evaluation ordering */ static getVariableDependencies(variable) { const dependencies = []; switch (variable.type) { case "interpolated-text": variable.interpolationPoints.forEach((point) => { const matches = point.expression.match(/@([a-zA-Z_][a-zA-Z0-9_]*)/g); if (matches) { dependencies.push(...matches.map((m) => m.substring(1))); } }); break; case "template": if (typeof variable.value === "string") { const matches = variable.value.match(/\{\{([^}]+)\}\}/g); if (matches) { matches.forEach((match) => { const varName = match.slice(2, -2).trim().split(".")[0]; if (varName && !dependencies.includes(varName)) { dependencies.push(varName); } }); } } break; case "imported": break; default: if (variable.source.hasInterpolation && typeof variable.value === "string") { const matches = variable.value.match(/@([a-zA-Z_][a-zA-Z0-9_]*)/g); if (matches) { dependencies.push(...matches.map((m) => m.substring(1))); } } } return [...new Set(dependencies)]; } /** * Check if variable creates a circular dependency */ static hasCircularDependency(variable, allVariables, visited = /* @__PURE__ */ new Set()) { if (visited.has(variable.name)) { return true; } visited.add(variable.name); const dependencies = this.getVariableDependencies(variable); for (let i = 0; i < dependencies.length; i++) { const depName = dependencies[i]; const depVariable = allVariables.get(depName); if (depVariable && this.hasCircularDependency(depVariable, allVariables, visited)) { return true; } } visited.delete(variable.name); return false; } /** * Determine if variable can be safely serialized */ static isSerializable(variable) { const effectiveType = this.getEffectiveType(variable); switch (effectiveType) { case "simple-text": case "interpolated-text": case "file-content": case "section-content": case "command-result": case "primitive": return true; case "object": case "array": return !this.detectComplexVariable(variable); case "path": return true; // Path metadata is serializable case "template": case "executable": case "pipeline-input": case "computed": return false; // These require execution context case "imported": return this.isSerializable(this.resolveImportedVariable(variable)); default: return false; } } /** * Get variable complexity score for performance optimization */ static getComplexityScore(variable) { let score = 0; const effectiveType = this.getEffectiveType(variable); switch (effectiveType) { case "primitive": case "simple-text": score += 1; break; case "interpolated-text": score += 2 + variable.interpolationPoints?.length || 0; break; case "template": score += 3; if (Array.isArray(variable.value)) score += 2; break; case "object": case "array": score += 2; if (variable.isComplex) score += 3; break; case "executable": case "computed": score += 5; break; case "pipeline-input": score += 4; break; default: score += 2; } if (this.hasInterpolation(variable)) score += 1; if (this.detectComplexVariable(variable)) score += 2; if (variable.metadata?.isImported) score += 1; const dependencies = this.getVariableDependencies(variable); score += dependencies.length * 0.5; return Math.round(score); } }; __name(_AdvancedTypeDetection, "AdvancedTypeDetection"); var AdvancedTypeDetection = _AdvancedTypeDetection; // core/types/variable/LegacyCompatibility.ts var _LegacyVariableConverter = class _LegacyVariableConverter { /** * Convert a new variable to legacy format * This ensures backward compatibility during transition */ static toLegacyVariable(variable) { return { type: this.mapToLegacyType(variable.type), name: variable.name, value: this.extractLegacyValue(variable), metadata: variable.metadata }; } /** * Convert a legacy variable to new format * This allows importing old variable definitions */ static fromLegacyVariable(legacy, source) { const defaultSource = source || { directive: "var", syntax: "reference", hasInterpolation: false, isMultiLine: false }; const newType = this.mapFromLegacyType(legacy.type); const baseVariable = { name: legacy.name, createdAt: Date.now(), modifiedAt: Date.now(), source: defaultSource, metadata: legacy.metadata }; switch (newType) { case "simple-text": return { type: "simple-text", value: String(legacy.value), ...baseVariable }; case "object": return { type: "object", value: legacy.value || {}, isComplex: false, ...baseVariable }; case "array": return { type: "array", value: Array.isArray(legacy.value) ? legacy.value : [], isComplex: false, ...baseVariable }; case "command-result": return { type: "command-result", value: String(legacy.value), command: legacy.metadata?.command || "", ...baseVariable }; case "path": return { type: "path", value: { resolvedPath: legacy.value?.resolvedPath || String(legacy.value), originalPath: legacy.value?.originalPath || String(legacy.value), isURL: legacy.value?.isURL || false, isAbsolute: legacy.value?.isAbsolute || false }, ...baseVariable }; case "imported": return { type: "imported", value: legacy.value, originalType: legacy.metadata?.originalType || "simple-text", importSource: { path: legacy.metadata?.importPath || "", isModule: legacy.metadata?.isModule || false, variableName: legacy.name }, ...baseVariable }; case "executable": return { type: "executable", value: { type: legacy.value?.type || "command", template: legacy.value?.commandTemplate || legacy.value?.codeTemplate || "", language: legacy.value?.language }, paramNames: legacy.value?.paramNames || [], ...baseVariable }; default: return { type: "simple-text", value: String(legacy.value), ...baseVariable }; } } /** * Map new variable types to legacy types * This preserves the old type hierarchy during transition */ static mapToLegacyType(type) { switch (type) { case "simple-text": case "interpolated-text": case "template": case "file-content": case "section-content": return "text"; case "object": case "array": case "computed": case "pipeline-input": case "primitive": return "data"; case "path": return "path"; case "command-result": return "command"; case "imported": return "import"; case "executable": return "executable"; default: return "data"; } } /** * Map legacy types to new variable types */ static mapFromLegacyType(legacyType) { switch (legacyType.toLowerCase()) { case "text": return "simple-text"; case "data": return "object"; // Default data type case "path": return "path"; case "command": return "command-result"; case "import": return "imported"; case "executable": return "executable"; default: return "simple-text"; } } /** * Extract value in legacy format * This ensures the value structure matches legacy expectations */ static extractLegacyValue(variable) { switch (variable.type) { case "simple-text": case "interpolated-text": case "template": case "file-content": case "section-content": case "command-result": return variable.value; case "object": case "array": case "computed": case "primitive": return variable.value; case "path": return variable.value; case "imported": return variable.value; case "executable": return { type: variable.value.type, paramNames: variable.paramNames, ...variable.value.type === "command" ? { commandTemplate: variable.value.template } : { codeTemplate: variable.value.template, language: variable.value.language } }; case "pipeline-input": return variable.value; default: return variable.value; } } /** * Check if a variable has legacy type information */ static hasLegacyType(variable) { return "type" in variable && typeof variable.type === "string" && ["text", "data", "path", "command", "import", "executable"].includes(variable.type); } /** * Migrate a collection of legacy variables */ static migrateLegacyVariables(legacyVariables, defaultSource) { return legacyVariables.map( (legacy) => this.fromLegacyVariable(legacy, defaultSource) ); } /** * Convert a collection of new variables to legacy format */ static convertToLegacyFormat(variables) { return variables.map((variable) => this.toLegacyVariable(variable)); } /** * Validate that conversion is lossless */ static validateConversion(original, converted, reconverted) { const issues = []; if (original.name !== reconverted.name) { issues.push(`Name mismatch: ${original.name} -> ${reconverted.name}`); } const originalValue = this.extractLegacyValue(original); const reconvertedValue = this.extractLegacyValue(reconverted); if (JSON.stringify(originalValue) !== JSON.stringify(reconvertedValue)) { issues.push("Value structure changed during conversion"); } const expectedLegacyType = this.mapToLegacyType(original.type); if (converted.type !== expectedLegacyType) { issues.push(`Type mapping error: ${original.type} -> ${converted.type} (expected ${expectedLegacyType})`); } return { valid: issues.length === 0, issues }; } /** * Create a migration report for a set of variables */ static createMigrationReport(legacyVariables) { const report = { total: legacyVariables.length, byType: {}, complexVariables: [], warnings: [] }; legacyVariables.forEach((variable) => { report.byType[variable.type] = (report.byType[variable.type] || 0) + 1; if (variable.metadata?.isComplex) { report.complexVariables.push(variable.name); } if (!this.mapFromLegacyType(variable.type)) { report.warnings.push(`Unknown legacy type: ${variable.type} for variable ${variable.name}`); } if (variable.value && typeof variable.value === "object" && "originalType" in variable.value) { report.warnings.push(`Variable ${variable.name} may have nested type information`); } }); return report; } /** * Attempt automatic type refinement during conversion */ static refineTypeFromValue(legacyVariable) { const value = legacyVariable.value; if (legacyVariable.type === "data") { if (typeof value === "number" || typeof value === "boolean" || value === null) { return "primitive"; } else if (Array.isArray(value)) { return "array"; } else if (typeof value === "object" && value !== null) { return "object"; } } if (legacyVariable.type === "text") { if (typeof value === "string") { if (value.includes("{{") && value.includes("}}")) { return "template"; } else if (legacyVariable.metadata?.hasInterpolation) { return "interpolated-text"; } else if (legacyVariable.metadata?.filePath) { return legacyVariable.metadata.sectionName ? "section-content" : "file-content"; } } } return this.mapFromLegacyType(legacyVariable.type); } }; __name(_LegacyVariableConverter, "LegacyVariableConverter"); var LegacyVariableConverter = _LegacyVariableConverter; // core/types/variable.ts function toLegacyVariable(variable) { return LegacyVariableConverter.toLegacyVariable(variable); } __name(toLegacyVariable, "toLegacyVariable"); function hasLegacyType(variable) { return LegacyVariableConverter.hasLegacyType(variable); } __name(hasLegacyType, "hasLegacyType"); function isExecutableVariable(variable) { return AdvancedTypeDetection.isExecutableVariable(variable); } __name(isExecutableVariable, "isExecutableVariable"); function getEffectiveType(variable) { return AdvancedTypeDetection.getEffectiveType(variable); } __name(getEffectiveType, "getEffectiveType"); export { AdvancedTypeDetection, FastTypeGuards, LegacyVariableConverter, VariableMetadataUtils, VariableSourceHelpers, VariableTypeGuards, getEffectiveType, hasLegacyType, isArray, isCommandResult, isComputed, isExecutable, isExecutableVariable, isExternal, isFileContent, isImported, isInterpolatedText, isObject, isPath, isPipelineInput, isPrimitive, isSectionContent, isSimpleText, isStructured, isStructuredValueVariable, isTemplate, isTextLike, toLegacyVariable }; //# sourceMappingURL=chunk-VKEM6RSR.mjs.map //# sourceMappingURL=chunk-VKEM6RSR.mjs.map