refac: prompt template variable made not required by default

This commit is contained in:
Timothy Jaeryang Baek
2025-09-15 11:18:31 -05:00
parent 5afa42b0d9
commit d5824b1b49
2 changed files with 43 additions and 28 deletions
+28 -13
View File
@@ -1408,24 +1408,39 @@ export const parseVariableDefinition = (definition: string): Record<string, any>
// Parse type (explicit or implied)
const type = firstPart.startsWith('type=') ? firstPart.slice(5) : firstPart;
// Parse properties using reduce
const properties = propertyParts.reduce((props, part) => {
// Use splitProperties for the equals sign as well, in case there are nested quotes
const equalsParts = splitProperties(part, '=');
const [propertyName, ...valueParts] = equalsParts;
const propertyValue = valueParts.join('='); // Handle values with = signs
// Parse properties; support both key=value and bare flags (e.g., ":required")
const properties = propertyParts.reduce(
(props, part) => {
const trimmed = part.trim();
if (!trimmed) return props;
return propertyName && propertyValue
? {
...props,
[propertyName.trim()]: parseJsonValue(propertyValue.trim())
// Use splitProperties for the equals sign as well, in case there are nested quotes
const equalsParts = splitProperties(trimmed, '=');
if (equalsParts.length === 1) {
// It's a flag with no value, e.g. "required" -> true
const flagName = equalsParts[0].trim();
if (flagName.length > 0) {
return { ...props, [flagName]: true };
}
: props;
}, {});
return props;
}
const [propertyName, ...valueParts] = equalsParts;
const propertyValueRaw = valueParts.join('='); // Handle values with extra '='
if (!propertyName || propertyValueRaw == null) return props;
return {
...props,
[propertyName.trim()]: parseJsonValue(propertyValueRaw.trim())
};
},
{} as Record<string, any>
);
return { type, ...properties };
};
export const parseJsonValue = (value: string): any => {
// Remove surrounding quotes if present (for string values)
if (value.startsWith('"') && value.endsWith('"')) {