---
sidebar:
  hidden: true
title: roboto.domain.triggers.query_templates
---
## Module Contents

### QUERY_FIELD

```python
roboto.domain.triggers.query_templates.QUERY_FIELD = 'query'
```

`from roboto.domain.triggers.query_templates import QUERY_FIELD`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L12-L12)

Name of the selector field that holds a RoboQL query.

It is the one field of an invocation input whose text is read as query syntax rather than taken as a value.

### QueryPlaceholder

```python
class roboto.domain.triggers.query_templates.QueryPlaceholder
```

`from roboto.domain.triggers.query_templates import QueryPlaceholder`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L22-L41)

Bases: `NamedTuple`

One `{{<name>}}` placeholder found in a RoboQL query, with the string literal that encloses it.

`scan_placeholders` returns one for each placeholder it finds.

**Attributes**

- **QueryPlaceholder.end** (`int`) = `0`: Index one past the placeholder's last character in the query.

- **QueryPlaceholder.name** (`str`): Dotted name of the placeholder, without the braces, such as `file.relative_path`.

- **QueryPlaceholder.quote** (`str | None`) = `None`: Quote character of the string literal the placeholder sits in, or `None` when it sits outside every literal.

  A placeholder inside a comment counts as outside, because a substituted value could close the comment.

- **QueryPlaceholder.start** (`int`) = `0`: Index of the placeholder's first character in the query.

### escape_string_literal()

```python
def roboto.domain.triggers.query_templates.escape_string_literal(
    value: str,
    quote: str,
) -> str
```

`from roboto.domain.triggers.query_templates import escape_string_literal`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L120-L141)

Return `value` escaped for use inside a RoboQL string literal delimited by `quote`.

Roboto calls this when a trigger invokes an action, through `substitute_query_template`, on each value it substitutes. It puts a backslash before each backslash and each `quote` character; the other quote character needs no escape.

**Parameters**

- **value** (`str`): Text to place inside the literal.
- **quote** (`str`): Quote character that delimits the literal, `"` or `'`.

**Raises**

- `ValueError`: `value` contains a carriage return or a newline, which a RoboQL string literal cannot contain under any escape.

**Returns**

- `str`

### iter_query_templates()

```python
def roboto.domain.triggers.query_templates.iter_query_templates(
    invocation_input: collections.abc.Mapping[str, Any],
) -> collections.abc.Iterator[str]
```

`from roboto.domain.triggers import iter_query_templates`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L203-L220)

Yield the RoboQL query of each selector in an invocation input, in the order the selectors appear.

Roboto calls this when a trigger is saved, to check each query, and when the trigger invokes an action, to substitute each query. Selectors without a query are skipped.

**Parameters**

- **invocation_input** (`collections.abc.Mapping[str, Any]`): [`InvocationInput`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationInput) as a JSON-compatible dictionary, from `model_dump(mode="json")`, whose top-level values are selectors or lists of selectors.

**Returns**

- `collections.abc.Iterator[str]`

### map_query_templates()

```python
def roboto.domain.triggers.query_templates.map_query_templates(
    invocation_input: collections.abc.Mapping[str, Any],
    transform: collections.abc.Callable[[str], str],
) -> dict[str, Any]
```

`from roboto.domain.triggers import map_query_templates`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L175-L200)

Return a copy of an invocation input with `transform` applied to each selector's RoboQL query.

Roboto calls this when a trigger invokes an action, to replace each query in the target's `invocation_input` with its substituted form. `transform` is called once per query, in the order `iter_query_templates` yields them.

**Parameters**

- **invocation_input** (`collections.abc.Mapping[str, Any]`): [`InvocationInput`](/docs/reference/python-sdk/roboto/domain/actions/invocation_record#roboto.domain.actions.invocation_record.InvocationInput) as a JSON-compatible dictionary, from `model_dump(mode="json")`, whose top-level values are selectors or lists of selectors.
- **transform** (`collections.abc.Callable[[str], str]`): Called with each selector's query; its return value replaces the query.

**Returns**

- `dict[str, Any]`: A new dictionary with new lists and selector dictionaries. Values nested inside a selector are shared with `invocation_input`.

### placeholders_outside_string_literals()

```python
def roboto.domain.triggers.query_templates.placeholders_outside_string_literals(
    query: str,
) -> list[str]
```

`from roboto.domain.triggers.query_templates import placeholders_outside_string_literals`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L102-L117)

Return the names of the placeholders in a RoboQL query that sit outside every string literal.

Roboto calls this when a trigger is saved, through [`validate()`](/docs/reference/python-sdk/roboto/domain/triggers/validation#roboto.domain.triggers.validation.TriggerValidator.validate), on each query in an action target's `invocation_input`, and rejects the trigger if any placeholder is returned. A value substituted inside a literal is escaped for it, by `escape_string_literal`, so it stays data. A value substituted anywhere else, a comment included, becomes query syntax: a tag or path containing an operator would change the query it was meant to be compared in.

**Parameters**

- **query** (`str`): RoboQL query that may contain `{{<name>}}` placeholders.

**Returns**

- `list[str]`: Names of those placeholders, in the order they appear in `query`.

### scan_placeholders()

```python
def roboto.domain.triggers.query_templates.scan_placeholders(
    query: str,
) -> list[QueryPlaceholder]
```

`from roboto.domain.triggers.query_templates import scan_placeholders`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L44-L99)

Return every `{{<name>}}` placeholder in a RoboQL query, with the string literal enclosing each.

Roboto calls this when a trigger is saved, through `placeholders_outside_string_literals`, and when the trigger invokes an action, through `substitute_query_template`. It scans by the RoboQL lexer's rules:

- A single or double quote opens a string literal, which the same quote closes.
- A backslash inside a literal escapes the character after it.
- A `//` comment runs to the end of its line, and a `/*` comment runs to the next `*/`.

**Parameters**

- **query** (`str`): RoboQL query that may contain `{{<name>}}` placeholders.

**Returns**

- `list[QueryPlaceholder]`: Placeholders, in the order they appear in `query`.

### substitute_query_template()

```python
def roboto.domain.triggers.query_templates.substitute_query_template(
    query: str,
    resolve: collections.abc.Callable[[str], str],
) -> str
```

`from roboto.domain.triggers import substitute_query_template`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/triggers/query_templates.py#L144-L172)

Return a RoboQL query with each placeholder replaced by its value, escaped for the string literal it sits in.

Roboto calls this when a trigger invokes an action, on each query in the target's `invocation_input`. The query is substituted in one pass, so a value that itself contains `{{<name>}}` or a quote stays data and is never substituted again.

**Parameters**

- **query** (`str`): RoboQL query that contains `{{<name>}}` placeholders.
- **resolve** (`collections.abc.Callable[[str], str]`): Called with each placeholder's dotted name; returns the value to substitute. An exception it raises propagates.

**Raises**

- `ValueError`: A placeholder sits outside every string literal, or a value contains a carriage return or a newline, which no RoboQL string literal can contain.

**Returns**

- `str`
