UNPKG

mlld

Version:

mlld: a modular prompt scripting language

212 lines (210 loc) 6.71 kB
import { isVariable } from './chunk-7ASO6AZA.mjs'; import { isLoadContentResult, isLoadContentResultArray } from './chunk-444AWGDO.mjs'; import { __name } from './chunk-OMKLS24H.mjs'; // interpreter/utils/field-access.ts function accessField(value, field, options) { const VARIABLE_METADATA_PROPS = [ "type", "isComplex", "source", "metadata" ]; const parentVariable = isVariable(value) ? value : value?.__variable; if (isVariable(value) && field.type === "field") { const fieldName2 = String(field.value); if (VARIABLE_METADATA_PROPS.includes(fieldName2)) { const metadataValue = value[fieldName2]; if (options?.preserveContext) { return { value: metadataValue, parentVariable: value, accessPath: [ ...options.parentPath || [], fieldName2 ], isVariable: false }; } return metadataValue; } } const rawValue = isVariable(value) ? value.value : value; const fieldValue = field.value; let accessedValue; const fieldName = String(fieldValue); switch (field.type) { case "field": case "stringIndex": case "bracketAccess": { const name = String(fieldValue); if (typeof rawValue !== "object" || rawValue === null) { throw new Error(`Cannot access field "${name}" on non-object value`); } if (isLoadContentResult(rawValue)) { const result = rawValue[name]; if (result !== void 0) { accessedValue = result; break; } throw new Error(`Field "${name}" not found in LoadContentResult`); } if (isLoadContentResultArray(rawValue)) { if (name === "content") { accessedValue = rawValue.map((item) => item.content).join("\n\n"); break; } const result = rawValue[name]; if (result !== void 0) { accessedValue = result; break; } throw new Error(`Field "${name}" not found in LoadContentResultArray`); } if (rawValue.type === "object" && rawValue.properties) { if (!(name in rawValue.properties)) { if (options?.returnUndefinedForMissing) { accessedValue = void 0; break; } throw new Error(`Field "${name}" not found in object`); } accessedValue = rawValue.properties[name]; break; } if (!(name in rawValue)) { if (options?.returnUndefinedForMissing) { accessedValue = void 0; break; } throw new Error(`Field "${name}" not found in object`); } accessedValue = rawValue[name]; break; } case "numericField": { const numKey = String(fieldValue); if (typeof rawValue !== "object" || rawValue === null) { throw new Error(`Cannot access numeric field "${numKey}" on non-object value`); } if (rawValue.type === "object" && rawValue.properties) { if (!(numKey in rawValue.properties)) { if (options?.returnUndefinedForMissing) { accessedValue = void 0; break; } throw new Error(`Numeric field "${numKey}" not found in object`); } accessedValue = rawValue.properties[numKey]; break; } if (!(numKey in rawValue)) { if (options?.returnUndefinedForMissing) { accessedValue = void 0; break; } throw new Error(`Numeric field "${numKey}" not found in object`); } accessedValue = rawValue[numKey]; break; } case "arrayIndex": { const index = Number(fieldValue); if (rawValue && typeof rawValue === "object" && rawValue.type === "array" && rawValue.items) { const items = rawValue.items; if (index < 0 || index >= items.length) { throw new Error(`Array index ${index} out of bounds (array length: ${items.length})`); } accessedValue = items[index]; break; } if (!Array.isArray(rawValue)) { const numKey = String(fieldValue); if (typeof rawValue === "object" && rawValue !== null) { if (rawValue.type === "object" && rawValue.properties) { if (numKey in rawValue.properties) { accessedValue = rawValue.properties[numKey]; break; } } else if (numKey in rawValue) { accessedValue = rawValue[numKey]; break; } } throw new Error(`Cannot access index ${index} on non-array value`); } if (index < 0 || index >= rawValue.length) { throw new Error(`Array index ${index} out of bounds (array length: ${rawValue.length})`); } accessedValue = rawValue[index]; break; } default: throw new Error(`Unknown field access type: ${field.type}`); } if (options?.preserveContext) { const accessPath = [ ...options.parentPath || [], fieldName ]; const resultIsVariable = isVariable(accessedValue); return { value: accessedValue, parentVariable, accessPath, isVariable: resultIsVariable }; } return accessedValue; } __name(accessField, "accessField"); function accessFields(value, fields, options) { let current = value; let path = options?.parentPath || []; let parentVar = isVariable(value) ? value : void 0; for (const field of fields) { const result = accessField(current, field, { preserveContext: true, parentPath: path, returnUndefinedForMissing: options?.returnUndefinedForMissing }); if (options?.preserveContext) { current = result.value; path = result.accessPath; if (result.isVariable && isVariable(result.value)) { parentVar = result.value; } } else { current = result; } } if (options?.preserveContext) { return { value: current, parentVariable: parentVar, accessPath: path, isVariable: isVariable(current) }; } return current; } __name(accessFields, "accessFields"); function createFieldAccessVariable(result, source) { if (result.isVariable && isVariable(result.value)) { return result.value; } return { type: "computed", name: result.accessPath.join("."), value: result.value, metadata: { source, parentVariable: result.parentVariable, accessPath: result.accessPath, fieldAccess: true } }; } __name(createFieldAccessVariable, "createFieldAccessVariable"); export { accessField, accessFields, createFieldAccessVariable }; //# sourceMappingURL=chunk-27PI3WNS.mjs.map //# sourceMappingURL=chunk-27PI3WNS.mjs.map