# Pi Prompt Translate

When you think in your native language but have to prompt in English, things get tedious fast. Writing prompts in your mother tongue often confused the model, it would mix languages, lose context, or respond inconsistently. The cleanest solution was always to write everything in English, but after a while, constantly translating your own thoughts before even typing them becomes its own cognitive overhead. So I built this extension to handle that translation step automatically, letting me think naturally and prompt freely.

A lightweight extension for the [Pi Coding Agent](https://github.com/mariozechner/pi-coding-agent) that translates user prompts into a configurable target language before they reach the agent. Perfect for multilingual developers who prefer to think and type in their native language while working with English-centric AI coding assistants.

## Features

- **Seamless Integration** — Intercepts user input transparently; no changes to your workflow
- **Smart Preservation** — Code blocks, file paths, variable names, and technical terms remain untouched
- **Active LLM Reuse** — Leverages your currently selected model for translation; no extra API keys needed
- **Dedicated Translate Model** — Optionally use a different model just for translation (e.g., a fast/cheap model) while keeping your main coding model unchanged
- **Persistent State** — Toggle, language, and model settings survive across sessions
- **Minimal Footprint** — Single-turn LLM call with minimal token overhead

## Installation

Install as a Pi package from npm, git, or a local path:

```bash
# From npm
pi install npm:pi-prompt-translate

# From git
pi install git:github.com/Veucci/pi-prompt-translate

# From a local path
pi install /absolute/path/to/pi-prompt-translate
```

To try the package without installing permanently:

```bash
pi -e npm:pi-prompt-translate
```

Project-local installation (adds to `.pi/settings.json`):

```bash
pi install -l npm:pi-prompt-translate
```

Enable or disable the extension at any time with `pi config`.

## Usage

Four slash commands control the extension:

| Command | Description | Example |
|---------|-------------|---------|
| `/translate-toggle` | Enable or disable prompt translation | `/translate-toggle` |
| `/translate-lang <language>` | Set the target language | `/translate-lang English` |
| `/translate-model` | Open the model selector to choose a translation model | `/translate-model` |
| `/translate-model reset` | Reset to using the session's current model for translation | `/translate-model reset` |
| `/translate-status` | Show current status, target language, and model | `/translate-status` |

### Quick Start

1. Enable translation:
   ```
   /translate-toggle
   ```

2. Set your target language (default is `English`):
   ```
   /translate-lang English
   ```

3. *(Optional)* Choose a different model for translation:
   ```
   /translate-model
   ```
   This opens Pi's built-in model selector. The model you pick here is used **only** for translation — your main coding model stays unchanged. Run `/translate-model reset` to go back to the session model.

4. Type naturally in your preferred language. The extension translates your prompt before the agent sees it.

### Example Flow

```
User (typing in Spanish): Crea una funcion que sume dos numeros

Translated to English: Create a function that adds two numbers
Agent responds to the English prompt as usual
```

## How It Works

1. The extension registers an `input` event listener that fires on every user message
2. When enabled, it sends the text to the active LLM with a specialized system prompt requesting translation
3. The model returns only the translated text — no explanations, no commentary
4. The translated message is forwarded to the agent as if the user had typed it directly
5. Images attached to the message are preserved and forwarded unchanged

## Configuration

State is stored in `extensions/state.json` inside the package directory:

```json
{
  "enabled": true,
  "targetLang": "English",
  "translateModel": "anthropic/claude-4-5-haiku"
}
```

You can edit this file directly when Pi is not running, or use the slash commands at runtime.

## Requirements

- [Pi Coding Agent](https://github.com/mariozechner/pi-coding-agent)
- A configured LLM model with a valid API key (the extension reuses your existing Pi model configuration)

## Architecture

```
package.json          — Pi package manifest and peer dependencies
extensions/
  index.ts            — Main extension entry point and event handlers
  state.json          — Persistent user settings (auto-created)
```

### Key Design Decisions

- **Loop Prevention** — Messages injected by the extension itself are skipped to avoid infinite translation loops
- **Graceful Degradation** — If translation fails or no model is available, the original message is passed through unchanged
- **Minimal Prompt** — The translation system prompt is carefully tuned to preserve technical content while translating only natural language

## Contributing

Contributions are welcome. Please open an issue or pull request on GitHub.

Areas for improvement:

- Per-project language profiles
- Translation caching to reduce API calls

## License

MIT

## Acknowledgments

Built for the Pi Coding Agent ecosystem using the `@mariozechner/pi-coding-agent` and `@mariozechner/pi-ai` SDKs.
