feat: support {{ var | default xxx }} fallbacks in template fields#42
Merged
Conversation
A templated field can now carry a per-variable fallback:
`{{ key.path | default text }}`. When the key resolves to null /
undefined the literal `text` is substituted, and — because the author
explicitly handled absence — no template warning is recorded. This also
means a defaulted ref on a critical config field no longer fails the
node onto its error branch. An explicit empty-string value is a real
value and is not replaced by the default.
- Consolidate the `{{ … }}` matcher into one shared `TEMPLATE_REF_RE`
(lib/shared/template-refs.ts), imported by the runtime resolver, so the
lint pass and the resolver can never drift.
- lintField suppresses the "may legitimately be absent" warnings
(out-of-scope / missing-global / missing-input) when a default is
present, but still flags genuine typos (unknown / missing-field /
self-ref) — STRUCTURAL_LINT_REASONS.
- findTemplateSlot clamps the autocomplete slot to the ref name so
re-picking a ref preserves an existing `| default` clause, and stays
dormant for an unclosed placeholder where an inserted closer would
have nowhere correct to go.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Templated fields can now carry a per-variable fallback:
{{ key.path | default text }}. When the key resolves tonull/undefined, the literaltextis substituted instead of an empty string. Each placeholder gets its own independent default.Behavior
{{ a.b | default fallback }}resolves tofallbackwhena.bisnull/undefined.template_warningis recorded — and a defaulted ref on a critical config field (e.g. agentprompt) no longer fails the node onto itserrorbranch.null/undefinedonly count as missing. An explicit empty-string value is a real value and is not replaced by the default — consistent with the existing warning condition.}.defaultmust be followed by whitespace.Changes
lib/shared/template-refs.ts—TEMPLATE_REF_REextended to capture an optional| default …group; it is now the single shared matcher, imported by the runtime resolver so the lint pass and resolver can't drift.lintFieldsuppresses the "may legitimately be absent" warnings (out-of-scope/missing-global/missing-input) when a default is present, but still flags genuine typos (unknown/missing-field/self-ref) viaSTRUCTURAL_LINT_REASONS.lib/server/templating.ts—resolve()substitutes the default on a missing key and emits no warning when a default is present.app/components/TemplateField.tsx—findTemplateSlotclamps the autocomplete slot to the ref name so re-picking a ref preserves an existing| defaultclause, and stays dormant for an unclosed placeholder where an inserted}}would have nowhere correct to go.*.test.tsfiles.Testing
resolve()confirms each scenario (missing→default, present→real value, per-variable defaults, empty default, no-default warning unchanged).Review
{{...}}consumers; adopted the narrower lint rule.{{ ref | default …had no valid spot for an inserted closer.🤖 Generated with Claude Code