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

View file

@ -84,7 +84,7 @@
<div class=" self-center text-xs font-medium">
{variable}
{#if variables[variable]?.required ?? true}
{#if variables[variable]?.required ?? false}
<span class=" text-gray-500">*{$i18n.t('required')}</span>
{/if}
</div>
@ -134,7 +134,7 @@
placeholder={$i18n.t('Enter value (true/false)')}
bind:value={variableValues[variable]}
autocomplete="off"
required
required={variables[variable]?.required ?? false}
/>
</div>
{:else if variables[variable]?.type === 'color'}
@ -159,7 +159,7 @@
placeholder={$i18n.t('Enter hex color (e.g. #FF0000)')}
bind:value={variableValues[variable]}
autocomplete="off"
required
required={variables[variable]?.required ?? false}
/>
</div>
{:else if variables[variable]?.type === 'date'}
@ -170,7 +170,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'datetime-local'}
@ -181,7 +181,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'email'}
@ -192,7 +192,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'month'}
@ -203,7 +203,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'number'}
@ -214,7 +214,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'range'}
@ -235,7 +235,7 @@
placeholder={$i18n.t('Enter value')}
bind:value={variableValues[variable]}
autocomplete="off"
required
required={variables[variable]?.required ?? false}
/>
</div>
@ -256,7 +256,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'text'}
@ -267,7 +267,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'time'}
@ -278,7 +278,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'url'}
@ -289,7 +289,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
{...variableAttributes}
/>
{:else if variables[variable]?.type === 'map'}
@ -311,7 +311,7 @@
placeholder={$i18n.t('Enter coordinates (e.g. 51.505, -0.09)')}
bind:value={variableValues[variable]}
autocomplete="off"
required
required={variables[variable]?.required ?? false}
/>
</div>
{:else}
@ -321,7 +321,7 @@
bind:value={variableValues[variable]}
autocomplete="off"
id="input-variable-{idx}"
required
required={variables[variable]?.required ?? false}
/>
{/if}
</div>

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('"')) {