Overview
Provides variable management utilities for prompt templates and workflows, including variable creation, key validation, template extraction, and URL construction.
Description
This module is a central utility for working with template variables across the Dify frontend. getNewVar creates a new prompt variable from a key and type using a default template. getNewVarInWorkflow creates workflow-specific InputVar instances with workflow-appropriate defaults. checkKey validates a single variable key against naming rules (non-empty, max length, alphanumeric/underscore only, must not start with a digit), returning a typed i18n error message key on failure. checkKeys batch-validates an array of keys, stopping at the first error. hasDuplicateStr detects duplicate strings in an array. getVars extracts Template:Variable name placeholders from a prompt template string, filtering out reserved system placeholders (context, history, query, pre_prompt) and deduplicating results. The module also exports basePath (the application base path from environment configuration), getMarketplaceUrl for constructing marketplace URLs with tracking parameters, and replaceSpaceWithUnderscoreInVarNameInput for live input normalization that preserves cursor position.
Usage
Use these utilities when building or validating prompt templates, creating new variables in the app debug panel or workflow editor, constructing marketplace links, or normalizing variable name input fields.
Code Reference
Source Location
Signature
export const getNewVar = (key: string, type: string) => PromptVariable
export const getNewVarInWorkflow = (key: string, type?: InputVarType) => InputVar
export type VarKeyErrorMessageKey = I18nKeysByPrefix<'appDebug', 'varKeyError.'>
export const checkKey = (
key: string,
canBeEmpty?: boolean,
_keys?: string[],
) => true | VarKeyErrorMessageKey
export const checkKeys = (
keys: string[],
canBeEmpty?: boolean,
) => CheckKeysResult
export const hasDuplicateStr = (strArr: string[]) => boolean
export const getVars = (value: string) => string[]
export const basePath: string
export function getMarketplaceUrl(
path: string,
params?: Record<string, string | undefined>,
): string
export const replaceSpaceWithUnderscoreInVarNameInput = (
input: HTMLInputElement,
) => void
Import
import {
getNewVar,
getNewVarInWorkflow,
checkKey,
checkKeys,
hasDuplicateStr,
getVars,
basePath,
getMarketplaceUrl,
replaceSpaceWithUnderscoreInVarNameInput,
} from '@/utils/var'
I/O Contract
Inputs (getNewVar)
| Name |
Type |
Required |
Description
|
| key |
string |
Yes |
The variable key identifier
|
| type |
string |
Yes |
The variable type (e.g., "string", "number", "select")
|
Inputs (getNewVarInWorkflow)
| Name |
Type |
Required |
Description
|
| key |
string |
Yes |
The variable key identifier
|
| type |
InputVarType |
No (default: textInput) |
The workflow input variable type
|
Inputs (checkKey)
| Name |
Type |
Required |
Description
|
| key |
string |
Yes |
The variable key to validate
|
| canBeEmpty |
boolean |
No |
Whether an empty key is allowed
|
| _keys |
string[] |
No |
Reserved parameter (currently unused)
|
Inputs (checkKeys)
| Name |
Type |
Required |
Description
|
| keys |
string[] |
Yes |
Array of variable keys to validate
|
| canBeEmpty |
boolean |
No |
Whether empty keys are allowed
|
Inputs (getVars)
| Name |
Type |
Required |
Description
|
| value |
string |
Yes |
A prompt template string potentially containing Template:Variable placeholders
|
Inputs (getMarketplaceUrl)
| Name |
Type |
Required |
Description
|
| path |
string |
Yes |
The marketplace path segment
|
| params |
undefined> |
No |
Additional query parameters to append
|
Outputs
| Function |
Type |
Description
|
| getNewVar |
PromptVariable |
A new variable object populated from VAR_ITEM_TEMPLATE with the given key and type
|
| getNewVarInWorkflow |
InputVar |
A new workflow input variable populated from VAR_ITEM_TEMPLATE_IN_WORKFLOW
|
| checkKey |
VarKeyErrorMessageKey |
true if valid; otherwise an i18n error message key string
|
| checkKeys |
CheckKeysResult |
Object with isValid, errorKey, and errorMessageKey fields
|
| hasDuplicateStr |
boolean |
true if any string appears more than once in the array
|
| getVars |
string[] |
Deduplicated array of variable names extracted from the template, excluding system placeholders
|
| getMarketplaceUrl |
string |
Full marketplace URL with source tracking and optional query parameters
|
Usage Examples
import { getVars } from '@/utils/var'
const template = 'Hello {{user_name}}, your order {{order_id}} is ready. {{#context#}}'
const vars = getVars(template)
// ['user_name', 'order_id'] (context placeholder is excluded)
Validate a Variable Key
import { checkKey } from '@/utils/var'
checkKey('valid_key') // true
checkKey('123invalid') // 'notStartWithNumber'
checkKey('') // 'canNoBeEmpty'
checkKey('a'.repeat(200)) // 'tooLong'
checkKey('invalid-key') // 'notValid'
Create a New Variable for the Workflow Editor
import { getNewVarInWorkflow } from '@/utils/var'
import { InputVarType } from '@/app/components/workflow/types'
const newVar = getNewVarInWorkflow('user_email', InputVarType.textInput)
// { variable: 'user_email', label: 'user_email', type: InputVarType.textInput, placeholder: '', default: '', hint: '', ... }
Check for Duplicate Variable Names
import { hasDuplicateStr } from '@/utils/var'
hasDuplicateStr(['name', 'email', 'name']) // true
hasDuplicateStr(['name', 'email', 'phone']) // false
Related Pages