var

Sets one or more variables. Each key names the variable to set, and each value is the new value. The values are evaluated in the order written, so a later value can use an earlier variable.

Keys

A key is one of:

  • name — a local variable, visible in this function only.
  • agent.name — an agent variable, kept for the whole conversation and visible to every function of this agent.
  • conv.name — a conversation variable, kept for the whole conversation and shared by every agent that runs in the conversation.

Any key may be followed by an index to set one element of an existing list or dict: totals['q3'], rows[0], agent.cache['user']. The base variable must already exist.

A name starts with a letter and ends with a letter or a digit. A name contains only letters, digits and underscores, and is at least two characters long. Python keywords and the names sys, command, result, exc, env, agent, conv and onError cannot be used. See Variables and namespaces.

Values

A value is any JSON value. A string in braces is evaluated as a Python expression, and the variable receives the expression's result, whatever the type. Agent and conversation variables are saved with the conversation, so their values must be JSON serializable. Local variables may hold anything.

Example

"var": {
  "city": "Reston",
  "now": "{ datetime.now().isoformat() }",
  "greeting": "{ f'Hello from {city}' }",
  "agent.callCount": "{ getattr(agent, 'callCount', 0) + 1 }",
  "conv.lastCity": "{ city }"
}

Rules

  • Assignments run top to bottom. In the example, greeting can use city because city is set earlier in the same command.
  • Reading an agent or conversation variable that has never been set raises an error. Seed the variable first, as the example does for agent.callCount with getattr.
  • The loop variables of an enclosing for are ordinary locals and can be read here. A for command may not reuse a name that an enclosing for already binds.