The Route Transformer Advanced Policy accepts values from the request in config.host, config.port, and config.path.
The following table lists the supported sources:
|
Source
|
Template
|
|
Header
|
$(headers.{HEADER-NAME})
$(headers["{HEADER-NAME}"])
|
|
Query parameter
|
$(query_params.{QUERY-PARAM-NAME})
$(query_params["{QUERY-PARAM-NAME}"])
|
|
Captured URI group
|
$(uri_captures.{GROUP-NAME})
$(uri_captures["{GROUP-NAME}"])
|
|
Shared variable
|
$(shared.{VARIABLE-NAME})
$(shared["{VARIABLE-NAME}"])
|
Captured URI groups are the named groups in a regular expression route path in the config.route.paths of an AI Agent.
Shared variables are values that another AI Policy stores in the shared request context, for example with a property node.
Templates use the request as it is when the policy runs.
A header that the Request Transformer policy adds is available in a template, because Request Transformer runs first.
To use the text of a template literally, wrap it in quotes inside another template.
For example, $('$(headers["x-test"])') renders as the text $(headers["x-test"]).
AI Gateway evaluates the content of $(...) as a Lua expression, so you can use logical operators.
For example, the following template uses a captured URI group, then a query parameter, and then a default value:
$(uri_captures["user-id"] or query_params["user"] or "unknown")
You can combine constant text with templates.
For example, /orders/$(headers["Custom-Path"] or "default") adds a fixed prefix to a value from a header, and uses default when the request doesn’t include the header.
To run more logic, wrap a Lua function in an expression:
$((function() ... implementation here ... end)())
The following template uses the first three characters of a header value as the path.
When the request doesn’t include the header, the template returns default:
/$((function()
local value = headers["Custom-Path"]
if not value then
return "default"
end
return value:sub(1, 3)
end)())
Sandboxing imposes several limitations on how custom Lua code can be executed, for heightened security. The Lua (or LuaJIT) language itself is not limited, only the available environment and the set of usable modules are restricted.
The limitations can be adjusted with the untrusted_lua=off|strict|lax|sandbox|on setting.
See the sandboxing reference for more information.
Templates treat trailing whitespace and newlines as part of the value.
Make sure that multi-line templates don’t end with extra whitespace or newlines, because AI Gateway adds them to the generated value.