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

View file

@ -1408,24 +1408,39 @@ export const parseVariableDefinition = (definition: string): Record<string, any>
// Parse type (explicit or implied) // Parse type (explicit or implied)
const type = firstPart.startsWith('type=') ? firstPart.slice(5) : firstPart; const type = firstPart.startsWith('type=') ? firstPart.slice(5) : firstPart;
// Parse properties using reduce // Parse properties; support both key=value and bare flags (e.g., ":required")
const properties = propertyParts.reduce((props, part) => { const properties = propertyParts.reduce(
// Use splitProperties for the equals sign as well, in case there are nested quotes (props, part) => {
const equalsParts = splitProperties(part, '='); const trimmed = part.trim();
const [propertyName, ...valueParts] = equalsParts; if (!trimmed) return props;
const propertyValue = valueParts.join('='); // Handle values with = signs
return propertyName && propertyValue // Use splitProperties for the equals sign as well, in case there are nested quotes
? { const equalsParts = splitProperties(trimmed, '=');
...props,
[propertyName.trim()]: parseJsonValue(propertyValue.trim()) 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 }; return { type, ...properties };
}; };
export const parseJsonValue = (value: string): any => { export const parseJsonValue = (value: string): any => {
// Remove surrounding quotes if present (for string values) // Remove surrounding quotes if present (for string values)
if (value.startsWith('"') && value.endsWith('"')) { if (value.startsWith('"') && value.endsWith('"')) {