# Components

This is a configuration section of [Custom MT](/advanced/custom-mt.md). Components are declared as objects within the configuration:

```json
{
    "components": {
        "preTranslation": {},
        "postTranslation": {},
        "contextMemory": {},
        "usageChecker": {},
        "interceptorRequest": {}
    }
}
```

### preTranslation

Processes the source text before sending it to the translation service.

* **allowLineBreaks**: `boolean`
* **excludeStrings**: `string`
* **regexpMatch**: `string`
* **regexpReplace**: `string`

```json
{
    "components": {
        "preTranslation":{
            "allowLineBreaks": true,
            "regexpReplace": "[\"\\n\", \"g\", \"\\\\n \"]"
        }
    }
}
```

### postTranslation

Processes the translated text after receiving it from the translation service.

* **regexpMatch**: `string`
* **regexpReplace**: `string`

```json
{
    "components": {
        "postTranslation": {
            "regexpReplace": "[\"\\\\\\\\n\", \"g\", \"\\n\"]"
        }
    }
}
```

### contextMemory

Maintains context history for AI/LLM translations, allowing the model to reference previous dialogue for improved coherence and accuracy.

**Required Parameters**

* **schema**: `Array of objects` <{type: string, name: string, default: string|number|bool}>
* **formBuilder**: `Array of objects` <{type: string, form: string, name: string, default: string|number}>
* **components**: `object` - Component reference to schema name \<schema:string>

```json
{
    "schema": {
        { "type": "boolean", "name": "contextMemory.status", "default": true },
        { "type": "string", "name": "contextMemory.initial_prompt", "default": "" },
        { "type": "string", "name": "contextMemory.system_template", "default": "" },
        { "type": "string", "name": "contextMemory.user_template", "default": "[{ \"role\": \"user\", \"parts\":[{\"text\":\"$SOURCE_TEXT\"}] }]" },
        { "type": "string", "name": "contextMemory.assistant_template", "default": "[{ \"role\": \"model\", \"parts\":[{\"text\":\"$TRANSLATED_TEXT\"}] }]" },
        { "type": "string", "name": "contextMemory.data_source", "default": "new_translation" },
        { "type": "number", "name": "contextMemory.max_entries", "default": "1" },
    },
    "formBuilder": {
        { "type": "string", "form": "textarea", "width": "450", "name": "contextMemory.user_template", "default": "", "title": "User Template" },
        { "type": "string", "form": "textarea", "width": "450", "name": "contextMemory.assistant_template", "default": "", "title": "Assistant Template" },
        { "type": "string", "form": "select", "name": "contextMemory.data_source", "title": "Context Source", "default": "TM",
            "options": [
                { "name": "New Translation History", "value": "new_translation" },
                { "name": "Translation Memory", "value": "TM" }                
            ]
        },
        { "type": "number", "form": "range", "name": "contextMemory.max_entries", "title": "Max Context Entries", "default": "1", "min": "1", "max": "256", "step": "1" }

    },
    "components": {
        "contextMemory": {
            "schema": "contextMemory"
        }
    }
}
```

### usageChecker

* **interval**: `number` - Interval in milliseconds between usage checks
* **format**: `string` - Display format for usage information
* **request**: `object` - Request configuration for checking usage:
  * **method**: `string` - HTTP method (`"http_get"`, `"http_post"`)
  * **url:** `string` - API endpoint URL for usage information
  * **body**: `object` <mark style="color:$warning;">(optional)</mark> - Request body for POST requests
  * **options**: `object` <mark style="color:$warning;">(optional)</mark> - Additional request options:
    * **headers:** `object` - Custom request headers
  * **responseCountQuery:** `string` - JSON path to extract current usage count
  * **responseLimitQuery:** `string` - JSON path to extract usage limit

```json
{
    "components": {
        "usageChecker": {
            "interval": 60000,
            "format": "( $COUNT / $LIMIT )",
            "request": {
                "method": "http_get",
                "url": "https://api.deepl.com/v2/usage?auth_key=$API_KEY",
                "responseCountQuery": "character_count",
                "responseLimitQuery": "character_limit"
            }        
        }
    }
}
```

### interceptorRequest

Modifies the source text before sending it to the translation service.

* **prependSourceText**: `object` - Adds text before the source text:
  * **status**: `boolean` - Enable or disable prepending
  * **value**: `string` - Text to prepend
* **appendSourceText**: `object` - Adds text after the source text:
  * **status**: `boolean` - Enable or disable appending
  * **value**: `string` - Text to append
* **replaceSourceText**: `object` - Replaces the entire source text

```json
{
    "schema": {
        { "type": "boolean", "name": "interceptorRequest.prependSourceText", "default": false },
        { "type": "string", "name": "prompt", "default": "Translate the following text from $SOURCE_LANG to $TARGET_LANG: " }
    },
    "components": {
        "interceptorRequest":{
            "prependSourceText": {
                "status": false,
                "value": "$PROMPT"
            }         
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.vntranslator.com/advanced/custom-mt/components.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
