Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
feat(translate): provider-abstracted translate / transliterate / detect nodes
trohitg35f1bd6f59fa
diff --git a/server/config/credential_providers.json b/server/config/credential_providers.jsonindex b0a6efca..722af537 100644--- a/server/config/credential_providers.json+++ b/server/config/credential_providers.json@@ -2,21 +2,52 @@"version": "2026.04","last_updated": "2026-04-11","_description": "Credential provider catalogue. Loaded by server/services/credential_registry.py at startup; served to clients via the get_credential_catalogue WebSocket handler. Supports `extends` inheritance with array-merge-by-key semantics (see credential_registry.resolve_extends). `_abstract: true` entries are resolved but excluded from get_all_providers(). Color tokens are resolved client-side via the theme helper.",-"categories": {- "ai": { "label": "AI Providers", "order": 1 },- "social": { "label": "Social Media", "order": 2 },- "productivity": { "label": "Productivity", "order": 3 },- "email": { "label": "Email", "order": 4 },- "android": { "label": "Android", "order": 5 },- "search": { "label": "Search", "order": 6 },- "scrapers": { "label": "Scrapers", "order": 7 },- "services": { "label": "Services", "order": 8 },- "payments": { "label": "Payments", "order": 9 },- "deployment": { "label": "Deployment", "order": 10 },- "developer": { "label": "Developer Tools", "order": 11 }+ "ai": {+ "label": "AI Providers",+ "order": 1+ },+ "social": {+ "label": "Social Media",+ "order": 2+ },+ "productivity": {+ "label": "Productivity",+ "order": 3+ },+ "email": {+ "label": "Email",+ "order": 4+ },+ "android": {+ "label": "Android",+ "order": 5+ },+ "search": {+ "label": "Search",+ "order": 6+ },+ "scrapers": {+ "label": "Scrapers",+ "order": 7+ },+ "services": {+ "label": "Services",+ "order": 8+ },+ "payments": {+ "label": "Payments",+ "order": 9+ },+ "deployment": {+ "label": "Deployment",+ "order": 10+ },+ "developer": {+ "label": "Developer Tools",+ "order": 11+ }},-"providers": {"_ai_base": {"_abstract": true,@@ -34,14 +65,16 @@}]},-"openai": {"extends": "_ai_base","name": "OpenAI","color": "dracula.green","icon_ref": "lobehub:openai","fields": [- { "key": "apiKey", "placeholder": "sk-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-..."+ }]},"anthropic": {@@ -50,7 +83,10 @@"color": "dracula.orange","icon_ref": "lobehub:claude","fields": [- { "key": "apiKey", "placeholder": "sk-ant-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-ant-..."+ }]},"gemini": {@@ -60,7 +96,11 @@"color": "solarized.blue","icon_ref": "lobehub:gemini","fields": [- { "key": "apiKey", "placeholder": "AIza... (AI Studio) or AQ.... (Agent Platform / Vertex)", "help": "AIza keys come from Google AI Studio and bill personal Gemini API credits. AQ. keys come from the Gemini Enterprise Agent Platform / Vertex AI Express console and bill your GCP project instead - eligible for Cloud credits. With an AQ. key the model list shows Vertex-supported versions only (no -latest aliases); re-validate after switching key types to refresh it." }+ {+ "key": "apiKey",+ "placeholder": "AIza... (AI Studio) or AQ.... (Agent Platform / Vertex)",+ "help": "AIza keys come from Google AI Studio and bill personal Gemini API credits. AQ. keys come from the Gemini Enterprise Agent Platform / Vertex AI Express console and bill your GCP project instead - eligible for Cloud credits. With an AQ. key the model list shows Vertex-supported versions only (no -latest aliases); re-validate after switching key types to refresh it."+ }]},"groq": {@@ -69,7 +109,10 @@"color": "dracula.red","icon_ref": "lobehub:groq","fields": [- { "key": "apiKey", "placeholder": "gsk_..." }+ {+ "key": "apiKey",+ "placeholder": "gsk_..."+ }]},"cerebras": {@@ -78,7 +121,10 @@"color": "dracula.orange","icon_ref": "lobehub:cerebras","fields": [- { "key": "apiKey", "placeholder": "csk-..." }+ {+ "key": "apiKey",+ "placeholder": "csk-..."+ }]},"openrouter": {@@ -87,7 +133,10 @@"color": "solarized.violet","icon_ref": "lobehub:openrouter","fields": [- { "key": "apiKey", "placeholder": "sk-or-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-or-..."+ }]},"deepseek": {@@ -96,7 +145,10 @@"color": "dracula.cyan","icon_ref": "lobehub:deepseek","fields": [- { "key": "apiKey", "placeholder": "sk-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-..."+ }]},"kimi": {@@ -105,7 +157,10 @@"color": "dracula.purple","icon_ref": "lobehub:kimi","fields": [- { "key": "apiKey", "placeholder": "sk-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-..."+ }]},"mistral": {@@ -114,7 +169,10 @@"name": "Mistral","color": "dracula.pink","fields": [- { "key": "apiKey", "placeholder": "sk-..." }+ {+ "key": "apiKey",+ "placeholder": "sk-..."+ }]},"sarvam": {@@ -131,7 +189,6 @@}]},-"ollama": {"_description": "Local Ollama server. The single field stores a base URL; the backend's _LocalLLM credential class returns a placeholder api_key, and execute_chat reads `{provider}_proxy` to override base_url via the existing OpenAI-style auth-delegation path. No `extends` — the field shape diverges from the cloud `_ai_base` template.","name": "Ollama",@@ -153,7 +210,6 @@],"instructions": "Run `ollama serve` locally (or point Base URL at any reachable Ollama server). API key is not needed — the credential resolver supplies the documented `ollama` placeholder."},-"lmstudio": {"_description": "Local LM Studio server (OpenAI-compatible /v1). Same shape as ollama: single base-URL field stored as `lmstudio_proxy`.","name": "LM Studio",@@ -175,7 +231,6 @@],"instructions": "Start the LM Studio local server from the Developer tab. API key is not required."},-"_cli_base": {"_abstract": true,"category": "ai",@@ -184,7 +239,6 @@"_kind_doc": "Stripe-style CLI-managed auth: OpenCompany spawns `<provider> login` from a per-provider WS handler; each CLI manages credentials in its isolated directory under <DATA_DIR>, and we write a synthetic marker OAuth token on success so the catalogue's `stored` flag flips. OAuthConnect.tsx renders empty fields because `fields=[]` (see OAuthConnect.tsx:42 — the documented Stripe-style fieldless-CLI case). Each child sets its own `ws.login`/`ws.logout` so the handler name encodes the provider (Twitter / Google / Stripe convention — frontend dispatches `{}` payload).","fields": []},-"claude_code": {"extends": "_cli_base","name": "Claude Code CLI",@@ -196,7 +250,6 @@"logout": "claude_code_logout"}},-"codex_cli": {"extends": "_cli_base","name": "Codex CLI",@@ -208,7 +261,6 @@"logout": "codex_cli_logout"}},-"whatsapp": {"name": "WhatsApp Personal","category": "social",@@ -219,27 +271,60 @@"status_hook": "whatsapp","has_rate_limits": true,"status_rows": [- { "key": "status", "label": "Status", "ok_field": "connected", "true_text": "Connected", "false_text": "Disconnected" },- { "key": "session", "label": "Session", "ok_field": "has_session", "true_text": "Active", "false_text": "Inactive" },- { "key": "service", "label": "Service", "ok_field": "running", "true_text": "Running", "false_text": "Stopped" }+ {+ "key": "status",+ "label": "Status",+ "ok_field": "connected",+ "true_text": "Connected",+ "false_text": "Disconnected"+ },+ {+ "key": "session",+ "label": "Session",+ "ok_field": "has_session",+ "true_text": "Active",+ "false_text": "Inactive"+ },+ {+ "key": "service",+ "label": "Service",+ "ok_field": "running",+ "true_text": "Running",+ "false_text": "Stopped"+ }],"actions": [- { "key": "start", "label": "Start", "theme_color": "green" },- { "key": "restart", "label": "Restart", "theme_color": "orange" },- { "key": "refresh", "label": "Refresh", "theme_color": "cyan" }+ {+ "key": "start",+ "label": "Start",+ "theme_color": "green"+ },+ {+ "key": "restart",+ "label": "Restart",+ "theme_color": "orange"+ },+ {+ "key": "refresh",+ "label": "Refresh",+ "theme_color": "cyan"+ }],"qr": {"qr_field": "qr","is_connected_field": "connected","connected_title": "Already Connected!","connected_subtitle_static": "No QR code needed",- "is_loading_fields": ["running", "!qr", "!connected"],+ "is_loading_fields": [+ "running",+ "!qr",+ "!connected"+ ],"empty_text_running": "Waiting for QR code...","empty_text_stopped": "Start service to get QR code","scan_text": "Scan with WhatsApp mobile app"}},-"twitter": {"name": "Twitter/X","category": "social",@@ -273,7 +358,6 @@"callback_url": "http://localhost:5678/api/twitter/callback","instructions": "Get credentials from the X Developer Portal. Create an app with OAuth 2.0 enabled."},-"telegram": {"name": "Telegram Bot","category": "social",@@ -313,7 +397,6 @@],"instructions": "Get a bot token from @BotFather on Telegram. To use the 'Self' recipient on telegramSend, either fill in your Chat ID below or DM your bot once after connecting."},-"google": {"name": "Google Workspace","category": "productivity",@@ -348,7 +431,6 @@"callback_url": "http://localhost:5678/api/google/callback","instructions": "Get credentials from Google Cloud Console. Enable Gmail, Calendar, Drive, Sheets, Tasks, and People APIs."},-"email_himalaya": {"name": "Email (IMAP/SMTP)","category": "email",@@ -357,7 +439,6 @@"kind": "email","icon_ref": "/api/schemas/credentials/email_himalaya/icon"},-"android_remote": {"name": "Android Device","category": "android",@@ -377,22 +458,46 @@}],"status_rows": [- { "key": "device", "label": "Device", "ok_field": "paired", "true_text": "Paired", "false_text": "Not Paired" },- { "key": "relay", "label": "Relay", "ok_field": "connected", "true_text": "Connected", "false_text": "Disconnected" },- { "key": "pairing", "label": "Pairing", "ok_field": "paired", "true_text": "Paired", "false_text": "Waiting", "warn": true }+ {+ "key": "device",+ "label": "Device",+ "ok_field": "paired",+ "true_text": "Paired",+ "false_text": "Not Paired"+ },+ {+ "key": "relay",+ "label": "Relay",+ "ok_field": "connected",+ "true_text": "Connected",+ "false_text": "Disconnected"+ },+ {+ "key": "pairing",+ "label": "Pairing",+ "ok_field": "paired",+ "true_text": "Paired",+ "false_text": "Waiting",+ "warn": true+ }],"actions": [{"key": "connect","label": "Connect","theme_color": "green",- "disabled_when": ["paired", "!stored"]+ "disabled_when": [+ "paired",+ "!stored"+ ]},{"key": "disconnect","label": "Disconnect","theme_color": "pink",- "disabled_when": ["!connected"]+ "disabled_when": [+ "!connected"+ ]}],"qr": {@@ -401,14 +506,17 @@"connected_title": "Device Paired!","connected_subtitle_field": "device_name","connected_subtitle_fallback": "Android Device",- "is_loading_fields": ["connected", "!qr_data", "!paired"],+ "is_loading_fields": [+ "connected",+ "!qr_data",+ "!paired"+ ],"empty_text_no_key": "Add API key first","empty_text_running": "Waiting for QR code...","empty_text_stopped": "Click Connect to start","scan_text": "Scan with Android companion app"}},-"brave_search": {"name": "Brave Search","category": "search",@@ -427,7 +535,6 @@}]},-"perplexity": {"name": "Perplexity","category": "search",@@ -446,7 +553,6 @@}]},-"apify": {"name": "Apify","category": "scrapers",@@ -466,7 +572,6 @@}]},-"serper": {"name": "Serper","category": "scrapers",@@ -484,7 +589,6 @@}]},-"google_maps": {"name": "Google Maps","category": "services",@@ -505,7 +609,6 @@}]},-"stripe": {"name": "Stripe","category": "payments",@@ -520,7 +623,6 @@},"instructions": "Click 'Login with Stripe' to open the Stripe Dashboard. After you authorise, the CLI stores credentials at ~/.config/stripe/config.toml and the listen daemon starts automatically. The Stripe CLI must be installed on PATH (https://stripe.com/docs/stripe-cli#install)."},-"vercel": {"name": "Vercel","category": "deployment",@@ -546,7 +648,6 @@],"instructions": "Two independent auth paths: click 'Login with Vercel' for the browser device flow (the CLI is auto-installed via npm and stores its auth state under OpenCompany's data directory), or paste an access token above for headless use."},-"github": {"name": "GitHub","category": "developer",@@ -561,7 +662,6 @@},"instructions": "Click 'Login with GitHub' — the gh CLI (auto-installed) opens github.com/login/device in your browser and the one-time code is shown here. The token is stored by gh in your system credential store; OpenCompany never keeps it. After login, 'gh auth setup-git' runs so git remotes authenticate too. Alternative: run 'gh auth login' (or pipe a Personal Access Token to 'gh auth login --with-token') in a terminal on this machine."},-"cloudflare": {"name": "Cloudflare","category": "deployment",@@ -595,7 +695,6 @@],"instructions": "Two independent auth paths: click 'Login with Cloudflare' — the cf CLI (auto-installed via npm) opens your default browser itself and completes via a localhost callback (browser must run on this machine; the badge flips when the CLI finishes; token stored by cf, never by OpenCompany) — or paste an API token above for full-scope and headless use."},-"gcloud": {"name": "Google Cloud","category": "deployment",@@ -610,5 +709,22 @@},"instructions": "Click 'Login with Google Cloud' — the gcloud CLI (auto-installed, ~100 MB on first use) opens your default browser itself for Google sign-in and completes via a localhost callback (browser must run on this machine; the badge flips when the CLI finishes). Credentials are stored by gcloud under OpenCompany's data directory — isolated from any gcloud you may have installed yourself — and OpenCompany never keeps them."}+ },+ "deepl": {+ "extends": "_ai_base",+ "name": "DeepL",+ "category": "ai",+ "category_label": "AI Providers",+ "instructions": "Create an API key at deepl.com/your-account/keys. Free and Pro keys both work — a free key ends in ':fx' and the node routes it to the free host automatically.",+ "fields": [+ {+ "key": "deepl",+ "label": "API Key",+ "type": "password",+ "secret": true,+ "placeholder": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx[:fx]",+ "required": true+ }+ ]}}diff --git a/server/config/pricing.json b/server/config/pricing.jsonindex 58182d8b..c05e4bd0 100644--- a/server/config/pricing.json+++ b/server/config/pricing.json@@ -265,6 +265,13 @@"speech_to_text": 0.00009417,"_default": 0.0000226},+ "deepl": {+ "_description": "Translation, billed per CHARACTER. DeepL API Pro is $25 per 1M characters on top of a fixed monthly fee, which this per-character figure represents; the API Free tier gives 500K characters/month at no cost, so an install on Free is over-reported. DeepL returns billed_characters per translation and the node reports THAT rather than counting input, so the character count is the provider's own.",+ "_source": "https://www.deepl.com/pro-api",+ "_updated": "2026-07",+ "translate": 0.000025,+ "_default": 0.000025+ },"elevenlabs": {"_description": "Text-to-speech, billed per CHARACTER of input. VERIFIED against the published API rate card: $0.10 per 1,000 characters for Multilingual v2/v3 (eleven_multilingual_v2 is the default model here) and $0.05 per 1,000 for Flash/Turbo. This table has no per-model axis, so the higher multilingual rate is used — an install running mostly Flash should halve it. Plans include a character quota; overage is charged at these same rates, so this is the marginal cost rather than the average.","_source": "https://elevenlabs.io/pricing/api",@@ -399,6 +406,9 @@"speech_to_text": "speech_to_text"},"_comment_speech": "Every speech provider needs an entry here as well as in api_pricing: calculate_api_cost() returns a zero cost SILENTLY when an action has no operation_map entry, so a missing line reads as 'free' rather than as an error.",+ "deepl": {+ "translate": "translate"+ },"elevenlabs": {"text_to_speech": "text_to_speech"},diff --git a/server/config/translate_defaults.json b/server/config/translate_defaults.jsonnew file mode 100644index 00000000..b4b0262a--- /dev/null+++ b/server/config/translate_defaults.json@@ -0,0 +1,105 @@+{+ "_comment": "Per-provider text-language capabilities. Read by nodes/translate/_config.py exactly as speech_defaults.json is read by nodes/speech/_config.py: loaded once at import, every capability question answered from here, so no shared code branches on a provider name. Three capability blocks per provider — translate / transliterate / detect — and which blocks exist is advisory only; registry membership is the authority.",+ "version": "1.0",++ "_common_languages_comment": "Fallback for the language dropdowns when a provider does not declare a closed list. LLM-backed providers accept essentially any language, so this is a convenience shortlist rather than a constraint.",+ "common_languages": [+ "en", "es", "fr", "de", "it", "pt", "nl", "pl", "ru", "uk", "tr",+ "ar", "he", "fa", "hi", "bn", "ta", "te", "ur", "id", "vi", "th",+ "zh", "ja", "ko"+ ],++ "providers": {+ "deepl": {+ "_comment": "Translation only — DeepL ships no transliteration or standalone language-ID product, so it registers into one registry and is unreachable from the other two nodes. That asymmetry is the reason this layer has three registries rather than one with capability flags.",+ "base_url": "https://api.deepl.com/v2",+ "_free_base_url_comment": "Free keys end in ':fx' and MUST use the api-free host; the pro host rejects them with 403. The provider picks the host from the key suffix, so a user never has to know which they hold.",+ "free_base_url": "https://api-free.deepl.com/v2",+ "credential_id": "deepl",+ "translate": {+ "endpoint": "/translate",+ "languages_endpoint": "/languages",+ "default_model": "quality_optimized",+ "models": ["quality_optimized", "prefer_quality_optimized", "latency_optimized"],+ "_formality_comment": "Only supported on some target languages; DeepL 400s on the rest, so 'prefer_' variants are the safe choice.",+ "formality_options": ["default", "prefer_more", "prefer_less", "more", "less"],+ "_languages_comment": "Fetched live from /languages, which is authoritative and changes without notice. This list is only the cold-start fallback for the dropdown.",+ "languages": [+ "BG", "CS", "DA", "DE", "EL", "EN-GB", "EN-US", "ES", "ET", "FI",+ "FR", "HU", "ID", "IT", "JA", "KO", "LT", "LV", "NB", "NL",+ "PL", "PT-BR", "PT-PT", "RO", "RU", "SK", "SL", "SV", "TR", "UK", "ZH"+ ],+ "default_target_language": "EN-US",+ "_billing_comment": "DeepL returns billed_characters per translation, so the node reports the provider's own figure rather than counting input itself.",+ "billed_unit": "characters",+ "max_input_chars": 128000+ }+ },++ "sarvam": {+ "_comment": "Indic-first, and the only provider here serving all three capabilities over dedicated REST endpoints. Ported from the retired sarvamTranslate / sarvamTransliterate / sarvamDetectLanguage nodes.",+ "base_url": "https://api.sarvam.ai",+ "credential_id": "sarvam",+ "translate": {+ "endpoint": "/translate",+ "default_model": "sarvam-translate:v1",+ "models": ["sarvam-translate:v1", "mayura:v1"],+ "_max_input_comment": "Enforced client-side so the LLM gets an actionable message instead of an opaque 422.",+ "max_input_chars": {+ "mayura:v1": 1000,+ "_default": 2000+ },+ "_languages_comment": "sarvam-translate:v1 covers all 22; mayura:v1 covers the 10-language core plus English. The wider set is the default because the default model supports it.",+ "languages": {+ "mayura:v1": ["bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN"],+ "_default": [+ "as-IN", "bn-IN", "brx-IN", "doi-IN", "en-IN", "gu-IN", "hi-IN",+ "kn-IN", "kok-IN", "ks-IN", "mai-IN", "ml-IN", "mni-IN", "mr-IN",+ "ne-IN", "od-IN", "pa-IN", "sa-IN", "sat-IN", "sd-IN", "ta-IN",+ "te-IN", "ur-IN"+ ]+ },+ "default_target_language": "hi-IN",+ "modes": ["formal", "modern-colloquial", "classic-colloquial", "code-mixed"],+ "billed_unit": "characters"+ },+ "transliterate": {+ "endpoint": "/transliterate",+ "default_model": "mayura:v1",+ "models": ["mayura:v1"],+ "languages": ["bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN"],+ "default_target_language": "hi-IN",+ "scripts": ["roman", "fully-native", "spoken-form-in-native"],+ "max_input_chars": 1000,+ "billed_unit": "characters"+ },+ "detect": {+ "endpoint": "/text-lid",+ "max_input_chars": 1000,+ "billed_unit": "characters"+ }+ },++ "openai": {+ "_comment": "LLM-backed rather than a dedicated translation API. Serves all three capabilities by prompting through ChatUnifier, which is why its billing unit is TOKENS, not characters — the ChatUnifier path already records token cost under llm.openai, so this layer records nothing and would otherwise double-count. Proves the Protocol does not care what a provider does internally.",+ "credential_id": "openai",+ "billed_unit": "tokens",+ "translate": {+ "default_model": "gpt-4o-mini",+ "models": ["gpt-4o-mini", "gpt-4.1", "gpt-5.2"],+ "default_target_language": "en",+ "billed_unit": "tokens"+ },+ "transliterate": {+ "default_model": "gpt-4o-mini",+ "models": ["gpt-4o-mini", "gpt-4.1", "gpt-5.2"],+ "billed_unit": "tokens"+ },+ "detect": {+ "default_model": "gpt-4o-mini",+ "models": ["gpt-4o-mini", "gpt-4.1", "gpt-5.2"],+ "billed_unit": "tokens"+ }+ }+ }+}diff --git a/server/nodes/sarvam/__init__.py b/server/nodes/sarvam/__init__.pydeleted file mode 100644index 005afb15..00000000--- a/server/nodes/sarvam/__init__.py+++ /dev/null@@ -1,42 +0,0 @@-"""Sarvam AI service plugins.--Three stateless REST nodes over ``api.sarvam.ai`` — translate,-transliterate and language ID — sharing one ``api-subscription-key``-credential. Node classes register themselves on import via-``BaseNode.__init_subclass__``; the only explicit wiring here is the-output-schema registry, which the parameter panel reads to render-"Input Data & Variables" for downstream nodes.--Speech used to live here too, as ``sarvamTextToSpeech`` and-``sarvamSpeechToText``. Both were retired in favour of the-provider-abstracted ``textToSpeech`` / ``speechToText`` nodes in-``nodes/speech/``, where Sarvam is one provider among several and the-same canvas node can switch vendors without being replaced. The Sarvam-wire logic was ported verbatim into ``nodes/speech/_providers/sarvam.py``.--Sarvam's OpenAI-compatible chat endpoint is a separate plugin-(``nodes/model/sarvam_chat_model``) registered through the standard-LLM-provider path, and every surface shares ``SarvamCredential``.-"""--from services.node_output_schemas import register_output_schema--from .sarvam_detect_language import (- SarvamDetectLanguageNode,- SarvamDetectLanguageOutput,-)-from .sarvam_translate import SarvamTranslateNode, SarvamTranslateOutput-from .sarvam_transliterate import (- SarvamTransliterateNode,- SarvamTransliterateOutput,-)--register_output_schema(SarvamTranslateNode.type, SarvamTranslateOutput)-register_output_schema(SarvamTransliterateNode.type, SarvamTransliterateOutput)-register_output_schema(SarvamDetectLanguageNode.type, SarvamDetectLanguageOutput)--__all__ = [- "SarvamDetectLanguageNode",- "SarvamTranslateNode",- "SarvamTransliterateNode",-]diff --git a/server/nodes/sarvam/_base.py b/server/nodes/sarvam/_base.pydeleted file mode 100644index 5806fcf1..00000000--- a/server/nodes/sarvam/_base.py+++ /dev/null@@ -1,141 +0,0 @@-"""Sarvam AI shared helpers.--Five plugins (translate / transliterate / detect_language / speech_to_text /-text_to_speech) hit ``https://api.sarvam.ai`` with the same-``api-subscription-key`` header, which :class:`SarvamCredential` injects-automatically through ``ctx.connection("sarvam")``. There is no service-singleton here — these are stateless REST calls, so per the plugin-cookbook the folder stays helpers + node files.--The credential itself lives with the other LLM credentials in-``nodes/model/_credentials.py`` because the *same* key also authenticates-Sarvam's OpenAI-compatible chat endpoint (``sarvamChatModel``). One stored-key, one catalogue entry, both surfaces.-"""--from __future__ import annotations--from typing import Any, Dict--import httpx--from core.logging import get_logger-from services.plugin import NodeContext, NodeUserError-from services.pricing import get_pricing_service--logger = get_logger(__name__)--SARVAM_BASE_URL = "https://api.sarvam.ai"--# Sarvam's own language-code vocabulary (BCP-47-ish, always ``-IN``).-# Translate accepts the full 22-language set; transliterate and TTS accept-# the 10-language core plus English. Kept as tuples so the node modules can-# splat them into ``Literal[...]`` without drifting apart.-TRANSLATE_LANGUAGES = (- "as-IN", "bn-IN", "brx-IN", "doi-IN", "en-IN", "gu-IN", "hi-IN",- "kn-IN", "kok-IN", "ks-IN", "mai-IN", "ml-IN", "mni-IN", "mr-IN",- "ne-IN", "od-IN", "pa-IN", "sa-IN", "sat-IN", "sd-IN", "ta-IN",- "te-IN", "ur-IN",-)--CORE_LANGUAGES = (- "bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "ml-IN", "mr-IN",- "od-IN", "pa-IN", "ta-IN", "te-IN",-)---async def post_json(- ctx: NodeContext, path: str, body: Dict[str, Any]-) -> Dict[str, Any]:- """POST a JSON body to ``api.sarvam.ai{path}`` and return the payload.-- Drops ``None`` values so an unset optional never reaches the API as an- explicit null (Sarvam rejects several of those with a 422). Non-2xx- responses become :class:`NodeUserError` carrying Sarvam's own message- — these are almost always user-correctable (bad language pair, text- over the per-model character cap, missing key).- """- payload = {k: v for k, v in body.items() if v is not None}- async with ctx.connection("sarvam") as conn:- response = await conn.post(- f"{SARVAM_BASE_URL}{path}",- json=payload,- headers={"Content-Type": "application/json"},- )- _raise_for_status(response, path)- return response.json()---async def post_multipart(- ctx: NodeContext, path: str, *, files: Any, data: Dict[str, Any]-) -> Dict[str, Any]:- """POST ``multipart/form-data`` to ``api.sarvam.ai{path}``.-- No ``Content-Type`` header — httpx generates the multipart boundary.- ``files`` must carry raw bytes rather than a file handle so the- Connection's auth-retry can replay the request intact.- """- form = {k: str(v) for k, v in data.items() if v is not None}- async with ctx.connection("sarvam") as conn:- response = await conn.post(- f"{SARVAM_BASE_URL}{path}", files=files, data=form- )- _raise_for_status(response, path)- return response.json()---def _raise_for_status(response: httpx.Response, path: str) -> None:- if response.status_code < 400:- return- detail = ""- try:- body = response.json()- if isinstance(body, dict):- error = body.get("error")- if isinstance(error, dict):- detail = str(error.get("message") or error.get("code") or "")- detail = detail or str(body.get("message") or body.get("detail") or "")- except Exception:- detail = response.text[:400]- raise NodeUserError(- f"Sarvam {path} failed ({response.status_code})"- + (f": {detail}" if detail else "")- )---async def track_sarvam_usage(- ctx: NodeContext, action: str, resource_count: int-) -> Dict[str, float]:- """Record a Sarvam API call in ``api_usage_metrics``.-- ``@Operation(cost=...)`` is declarative metadata that nothing reads at- runtime, so cost attribution is an explicit call — same shape as- ``nodes/twitter/_base.track_twitter_usage``. ``resource_count`` is the- billable unit for the operation: characters for the text and speech- endpoints, seconds of audio for transcription.-- Never raises: a metrics failure must not fail a successful API call.- """- try:- from services.plugin.deps import get_database-- pricing = get_pricing_service()- cost_data = pricing.calculate_api_cost("sarvam", action, resource_count)- await get_database().save_api_usage_metric(- {- "session_id": ctx.session_id,- "node_id": ctx.node_id,- "workflow_id": ctx.workflow_id,- "service": "sarvam",- "operation": cost_data.get("operation", action),- "endpoint": action,- "resource_count": resource_count,- "cost": cost_data.get("total_cost", 0.0),- }- )- return cost_data- except Exception as e:- logger.warning(- "sarvam usage tracking failed", action=action, error=str(e)- )- return {}diff --git a/server/nodes/sarvam/icon.svg b/server/nodes/sarvam/icon.svgdeleted file mode 100644index 3f216d33..00000000--- a/server/nodes/sarvam/icon.svg+++ /dev/null@@ -1,8 +0,0 @@-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" role="img" aria-label="Sarvam AI">- <title>Sarvam AI</title>- <rect width="24" height="24" rx="5" fill="#1E2033"/>- <g transform="translate(3.5000 3.5988) scale(0.197674)" fill="#FFFFFF">- <path d="M85.417 36.82a31.866 31.866 0 0 0-5.064-6.753l-.028-.03-.536-.536a31.209 31.209 0 0 0-4.127-3.376l-.315-.215c-.036-.028-.078-.05-.114-.078V25.424c-.029-1.696-.2-3.391-.494-5.03l-.035-.2c-.036-.193-.072-.379-.108-.558v-.028a31.76 31.76 0 0 0-2.503-7.411 21.15 21.15 0 0 0-.45-.902l-.172-.329-.35-.129a16.74 16.74 0 0 0-.952-.336c-1.159-.4-2.353-.73-3.533-.987a31.334 31.334 0 0 0-3.67-.58 31.523 31.523 0 0 0-6.694.043c-.02-.028-.042-.057-.07-.078a31.975 31.975 0 0 0-4.342-4.2l-.015-.014a32.26 32.26 0 0 0-7.68-4.542L43.827 0l-.343.121c-.265.093-.615.215-.952.35a31.99 31.99 0 0 0-6.55 3.449l-.33.229a6.27 6.27 0 0 0-.322.236 32.033 32.033 0 0 0-4.427 3.863 31.68 31.68 0 0 0-5.321-.45c-.257-.001-.53 0-.801.013a31.834 31.834 0 0 0-8.833 1.46l-.343.114-.186.308c-.165.272-.33.565-.508.88a32.127 32.127 0 0 0-2.832 6.946c-.072.265-.136.522-.2.78a31.833 31.833 0 0 0-.887 5.708 32.37 32.37 0 0 0-4.406 3.112c-.207.172-.408.344-.615.522a32.02 32.02 0 0 0-5.214 5.902l-.021.029c-.2.286-.38.558-.544.822L0 34.703l.057.365c.057.358.122.687.179 1.009a32.108 32.108 0 0 0 2.332 7.218l.014.028c.114.244.229.48.343.716l.165.336a31.974 31.974 0 0 0 2.775 4.5 32.213 32.213 0 0 0-1.395 5.322v.014l-.129.78a31.727 31.727 0 0 0-.186 7.769c.036.336.072.68.115 1.002l.05.364.279.244c.264.228.522.443.78.65l.035.03a31.722 31.722 0 0 0 6.501 4.048l.086.043c.093.043.179.086.28.129l.371.164a31.893 31.893 0 0 0 5.285 1.782 31.58 31.58 0 0 0 2.403 5.064l.1.165.322.537a31.873 31.873 0 0 0 4.792 5.973c.258.25.508.486.737.694l.272.257h1.387a31.538 31.538 0 0 0 3.584-.272 31.5 31.5 0 0 0 3.576-.686l.472-.122c.257-.071.507-.136.765-.215a32.451 32.451 0 0 0 5.185-2.024c.036.022.072.043.107.057l.093.05a32.508 32.508 0 0 0 4.964 2.247c.129.043.25.085.379.136l.386.128c2.41.787 4.914 1.28 7.453 1.474l.508.036c.164.007.336.021.507.028l.372.015.286-.236a32.032 32.032 0 0 0 5.614-5.938l.28-.393v-.015c.157-.214.307-.429.45-.65a33.5 33.5 0 0 0 1.574-2.626l.178-.344c.286-.55.565-1.123.83-1.716l.064-.136c.022-.043.036-.086.058-.13.021 0 .035 0 .057-.006.021 0 .036 0 .057-.007.05-.008.107-.022.157-.029.13-.022.25-.05.372-.072a32.357 32.357 0 0 0 4.82-1.38l.373-.143.379-.15a31.982 31.982 0 0 0 6.666-3.663l.414-.3.694-.537.065-.35c.035-.165.064-.33.093-.502l.085-.5c.386-2.49.48-5.022.28-7.526a30.603 30.603 0 0 0-.508-3.656 36.272 36.272 0 0 0-.53-2.239l-.043-.143-.043-.143c-.021-.086-.05-.171-.071-.257l.214-.258.093-.114.093-.115a31.923 31.923 0 0 0 2.582-3.77l.186-.322a31.982 31.982 0 0 0 3.175-7.87l.122-.492c.036-.136.065-.272.093-.408l.1-.45-.178-.323-.015-.028Zm-12.952-9.53.236.123.021.021v.064l-.021.15-.086.759v.079a29.975 29.975 0 0 1-.915 5.25l-.122.444c-.086.307-.186.637-.308 1.009a26.37 26.37 0 0 1-.715 1.945 32.992 32.992 0 0 0-6.015-4.249h-.014a5.683 5.683 0 0 0-.336-.193l-.207-.107-.05-.029-.044-.021-.507-.258.05-.537c.028-.3.05-.543.064-.772a33.026 33.026 0 0 0-.207-7.09c.837.158 1.666.358 2.474.587l.15.043c.373.107.68.208.98.3a28.67 28.67 0 0 1 4.871 2.118l.565.286.136.072v.007ZM64.06 45.756l-1.738 1.767 1.173 2.968a30.06 30.06 0 0 1 1.702 6.417l.022.15c.014.108.028.222.05.344a29.989 29.989 0 0 1-6.845 1.517l-.179.014a5.922 5.922 0 0 1-.4.035l-2.76.194-.759 2.403-.221.666-.086.243a30.544 30.544 0 0 1-2.654 5.637l-.05.086c-.086.15-.186.307-.3.48l-.38-.151A29.88 29.88 0 0 1 44.7 65.37c-.1-.064-.185-.129-.278-.193l-.25-.172-2.125-1.545-2.131 1.395c-.158.107-.336.215-.565.358a30.263 30.263 0 0 1-5.9 2.747h-.022a9.527 9.527 0 0 1-.587.2c-.043-.072-.078-.143-.121-.215l-.093-.178a30.178 30.178 0 0 1-2.496-6.195l-.036-.122-.107-.4-.651-2.64-1.903-.236c-.314-.036-.629-.08-.944-.122-.121-.014-.243-.036-.364-.05-1.08-.165-2.16-.4-3.219-.687a30.237 30.237 0 0 1-2.982-.973l-.15-.057c-.172-.064-.344-.136-.523-.208a17.7 17.7 0 0 1 .108-.529l.093-.415a29.703 29.703 0 0 1 1.966-5.859c.043-.1.122-.257.186-.393l.015-.029c.171-.365.307-.73.429-1.051l.636-1.696-1.251-1.266-.322-.336a32.946 32.946 0 0 1-2.525-3.162 29.14 29.14 0 0 1-1.623-2.619l-.122-.228-.193-.365c0-.014-.014-.029-.021-.043l.071-.057.458-.365a29.595 29.595 0 0 1 5.128-3.22l.436-.207c.2-.1.38-.179.558-.257l2.21-.98V30.217a4.63 4.63 0 0 1-.021-.422v-.079a30.145 30.145 0 0 1 .536-6.324l.107-.53c.015-.085.036-.17.058-.257.221 0 .457.007.743.022l.408.014a30.58 30.58 0 0 1 5.929.916c.25.064.458.121.644.171l2.489.737 1.673-2.26s.057-.072.086-.115l.25-.3a30.524 30.524 0 0 1 4.456-4.457l.372-.293c.064-.058.136-.108.2-.158l.05.043c.143.122.286.25.45.4l.065.058a30.07 30.07 0 0 1 4.242 4.779l.135.186.015.014c.078.107.157.222.236.343l1.38 2.09 2.446-.509c.107-.021.215-.043.315-.071a30.26 30.26 0 0 1 7.26-.637h.056c.079 0 .158 0 .236.007.036.193.065.415.108.68.293 2.117.357 4.292.186 6.46v.114l-.008.022c-.007.157-.028.343-.05.58l-.25 2.531 2.224 1.117s.065.035.1.057l.544.293a30.129 30.129 0 0 1 5.128 3.563l.393.35c.093.079.18.157.265.236-.086.15-.186.308-.3.5-1.638 2.67-3.812 4.908-4.056 5.159-.207.214-.379.379-.486.486l-.021.022h-.022Zm-41.518-15.96v.021c0 .179 0 .38.015.608v.694l-.472.208c-.215.093-.437.2-.68.314l-.422.2a32.839 32.839 0 0 0-5.357 3.32c-.207-.637-.4-1.288-.565-1.939l-.028-.114a28.672 28.672 0 0 1-.83-6.302l-.043-1.066.951-.437h.015a28.94 28.94 0 0 1 5.414-1.953c.336-.086.68-.164 1.037-.236.515-.107 1.03-.207 1.545-.286a32.78 32.78 0 0 0-.587 6.953l.007.015ZM61.951 11.76c.25.022.48.036.701.065h.022c1.123.114 2.245.286 3.34.529a28.98 28.98 0 0 1 3.211.894h.022c.05.022.092.043.142.057.022.05.043.1.072.158a28.803 28.803 0 0 1 2.26 6.674l.014.086.13.665.106.644c.122.773.208 1.567.265 2.354a32.241 32.241 0 0 0-4.434-1.846 24.507 24.507 0 0 0-1.116-.343l-.114-.036h-.022a30.775 30.775 0 0 0-3.49-.787 29.796 29.796 0 0 0-1.001-2.912v-.021c-.143-.33-.286-.687-.465-1.087a32.488 32.488 0 0 0-2.875-5.151c1.08-.043 2.16-.021 3.232.057Zm-6.544.293.015.015h.02l.537.801.043.057a29.087 29.087 0 0 1 2.904 5.1v.022c.143.3.258.573.358.83l.057.136c.214.55.422 1.102.6 1.66a33.387 33.387 0 0 0-7.337.608l-.515.107c-.08.022-.165.036-.25.057l-.523.108-.293-.444c-.15-.222-.286-.422-.43-.615v-.014a33.244 33.244 0 0 0-4.555-5.144c.63-.343 1.273-.672 1.917-.966l.472-.207c.279-.122.586-.25.98-.408l.136-.05a29.089 29.089 0 0 1 4.856-1.416l1.023-.23-.015-.007ZM37.02 6.738l.014-.014c.086-.064.172-.129.265-.186l.322-.222a29.158 29.158 0 0 1 5.95-3.133h.022c.05-.029.093-.043.143-.064.05.021.107.05.157.071a29.38 29.38 0 0 1 6.08 3.713l.042.036c.2.164.401.322.573.472a29.615 29.615 0 0 1 2.346 2.203 30.955 30.955 0 0 0-4.606 1.43l-.029.015c-.415.165-.751.308-1.059.444l-.5.221-.079.036c-.958.444-1.917.944-2.832 1.488a32.898 32.898 0 0 0-2.596-1.566l-.072-.043c-.314-.165-.65-.344-1.051-.544l-.029-.014a32.423 32.423 0 0 0-5.743-2.154 29.182 29.182 0 0 1 2.682-2.196v.007Zm-5.364 5.501.651-.844.973.28.05.014a29.005 29.005 0 0 1 5.485 2.031l.15.072c.18.093.351.179.508.257l.3.165c.459.243.909.5 1.352.772l-.114.093a33.142 33.142 0 0 0-4.978 4.987l-.179.214-.157.2-.472.63-.53-.157a33.335 33.335 0 0 0-7.252-1.202h-.15c.294-.758.63-1.502.987-2.232.158-.322.322-.644.494-.959l.036-.064a29.18 29.18 0 0 1 2.839-4.256h.007Zm-16.957 6.754c.057-.215.114-.465.185-.709a29.076 29.076 0 0 1 2.575-6.316l.086-.15c.057-.015.114-.03.165-.05a28.75 28.75 0 0 1 7.13-1.06h.05c.23-.007.472-.014.687-.014 1.144 0 2.288.072 3.433.208a32.102 32.102 0 0 0-2.775 4.27v.015c-.193.343-.372.694-.537 1.03a29.736 29.736 0 0 0-1.487 3.527v.014c-.1.007-.2.021-.3.036-.973.114-1.939.272-2.897.48-.394.078-.78.17-1.152.264-1.988.5-3.94 1.195-5.8 2.067a27.8 27.8 0 0 1 .665-3.62l-.029.008ZM5.693 43.058l-.157-.322a9.898 9.898 0 0 1-.23-.48l-.078-.171a29.014 29.014 0 0 1-2.131-6.596c-.007-.05-.022-.107-.036-.164l.35-.508a28.906 28.906 0 0 1 4.478-4.994 28.985 28.985 0 0 1 3.426-2.604 32.245 32.245 0 0 0 .908 6.582v.014a33.69 33.69 0 0 0 1.03 3.355c.014.043.036.093.05.136.007.022.021.05.028.079-.028.028-.057.057-.085.093a30.468 30.468 0 0 0-2.153 2.51l-.35.466a32.493 32.493 0 0 0-3.319 5.559 28.469 28.469 0 0 1-1.73-2.955Zm8.118 23.7-.35-.157c-.08-.036-.151-.065-.23-.108l-.093-.043a29.192 29.192 0 0 1-5.936-3.705l-.129-.108-.021-.178a28.792 28.792 0 0 1 .164-7.061v-.022c.036-.25.08-.5.115-.715v-.057c.207-1.13.48-2.26.815-3.37a31.961 31.961 0 0 0 4.241 3.992l.472.358c.158.121.315.236.48.35.872.63 1.788 1.224 2.725 1.767.085.05.178.1.264.15v.094a31.32 31.32 0 0 0-.028 3.476c.014.372.043.766.071 1.166a32.57 32.57 0 0 0 .916 5.416 28.812 28.812 0 0 1-3.476-1.26v.015Zm4.963-18.678a33.084 33.084 0 0 0-2.16 6.438l-.021.114c-.53-.329-1.044-.68-1.545-1.044l-.422-.308-.45-.343a29.464 29.464 0 0 1-3.949-3.727l-.715-.787.437-.916.021-.043a28.91 28.91 0 0 1 2.79-4.828l.3-.422.343-.451c.365-.472.744-.93 1.137-1.374.5.895 1.059 1.789 1.66 2.647a33.618 33.618 0 0 0 2.267 2.898c.171.2.343.386.515.572l.25.264.079.08c-.1.271-.208.55-.322.793-.072.15-.158.33-.208.444l-.007-.007Zm1.724 20.688-.229-.887-.036-.143a29.582 29.582 0 0 1-.951-5.344v-.014c-.029-.372-.05-.737-.072-1.073a29.642 29.642 0 0 1-.014-2.11c.973.364 1.967.686 2.968.958 1.159.315 2.353.565 3.54.751.13.022.258.036.394.057.308.043.608.086.908.122l.15.615s0 .029.008.036l.014.057c.05.193.093.372.15.558a32.911 32.911 0 0 0 2.64 6.625 29.027 29.027 0 0 1-2.84.257 29.372 29.372 0 0 1-5.579-.315l-1.051-.136v-.014Zm15.005 11.038c-.222.065-.45.13-.665.186l-.458.122c-1.073.272-2.167.48-3.247.615a29.174 29.174 0 0 1-3.261.25H27.7c-.043-.035-.079-.078-.129-.121a29.204 29.204 0 0 1-4.355-5.423c-.1-.157-.193-.314-.286-.472l-.093-.157a29.389 29.389 0 0 1-1.53-2.99c1.966.286 3.969.393 5.935.314a31.783 31.783 0 0 0 4.549-.515 30.09 30.09 0 0 0 2.303 2.797 32.25 32.25 0 0 0 4.713 4.192c-1.08.466-2.188.866-3.318 1.195l.014.007Zm7.33-3.047c-.285.179-.564.365-.836.536l-.808-.586-.057-.043a28.781 28.781 0 0 1-4.156-3.477 18.817 18.817 0 0 1-.73-.765 29.118 29.118 0 0 1-1.544-1.817 32.757 32.757 0 0 0 6.809-3.313l.45-.3.458.336c.043.029.079.058.122.086h.007c.157.122.314.229.486.35a32.701 32.701 0 0 0 6.458 3.441c-.393.444-.8.873-1.215 1.288l-.48.465a29.19 29.19 0 0 1-4.92 3.756l-.058.036.015.007Zm19.254-3.563-.171.322c-.43.794-.909 1.596-1.43 2.382-.144.208-.287.415-.416.608l-.25.358a28.882 28.882 0 0 1-4.398 4.8c-.043.036-.08.072-.122.108l-.193-.015a29.27 29.27 0 0 1-6.745-1.33l-.379-.13a28.636 28.636 0 0 1-3.204-1.31 31.929 31.929 0 0 0 4.606-3.504c.143-.135.286-.264.43-.4l.428-.408a32.18 32.18 0 0 0 2.246-2.46c1.137.278 2.31.5 3.483.657l.572.072c.194.021.387.043.58.057 1.652.157 3.347.179 5.02.079l-.064.129.007-.015Zm3.626-11.202-.043.479a29.613 29.613 0 0 1-1.402 6.51l-.278.915-.022.08-.021.028-.4.028-.573.036h-.079a28.044 28.044 0 0 1-5.478 0h-.114c-.15-.021-.28-.036-.394-.05l-.558-.064a27.693 27.693 0 0 1-2.424-.43 32.967 32.967 0 0 0 2.953-6.36v-.02c.086-.237.165-.473.244-.716l.157-.508.565-.043h.129c.207-.021.407-.036.586-.057h.022a32.899 32.899 0 0 0 7.18-1.517c.015 0 .029-.007.043-.014a32.804 32.804 0 0 1-.085 1.695l-.008.008Zm.501-12.57-.48-1.209.38-.386c.107-.107.3-.293.536-.536.436-.444 2.697-2.797 4.463-5.673l.043-.065c.43.53.844 1.08 1.23 1.632l.25.364c.208.3.394.587.573.873l.214.343a30.075 30.075 0 0 1 2.253 4.564l.28.651.056.136.058.136.021.05-.021.022-.072.064-.107.1-.108.108-.4.379-.072.064A29.708 29.708 0 0 1 71 54.647c-.286.193-.58.393-.887.58-.658.414-1.345.815-2.046 1.172a33.155 33.155 0 0 0-1.86-6.996l.008.022Zm10.771 14.887-.05.33-.15.107a29.098 29.098 0 0 1-6.05 3.32h-.022c-.108.056-.2.092-.294.128l-.364.136c-.909.343-1.846.636-2.797.887a31.905 31.905 0 0 0 1.316-6.474l.043-.501c.079-.944.114-1.91.1-2.876a32.217 32.217 0 0 0 3.927-2.303 32.318 32.318 0 0 0 4.005-3.248c.036.157.064.315.1.472.172.873.308 1.745.393 2.604.022.172.043.393.058.6v.13a28.24 28.24 0 0 1-.222 6.681l.007.007Zm5.471-26.533a29.422 29.422 0 0 1-2.532 6.503c-.114.207-.207.386-.293.537l-.236.407c-.429.73-.887 1.438-1.38 2.132a32.704 32.704 0 0 0-2.332-4.471l-.035-.057c-.18-.286-.372-.58-.58-.894l-.021-.036A34.876 34.876 0 0 0 72.9 39.08c.43-.973.816-1.981 1.145-2.997.135-.408.243-.773.343-1.116l.129-.465a32.8 32.8 0 0 0 .922-4.936 30.134 30.134 0 0 1 2.339 2.031l.5.501.315.322a28.782 28.782 0 0 1 3.827 5.015c.028.05.064.1.093.15l-.043.186-.015.007Z"/>- <path fill-rule="evenodd" clip-rule="evenodd" d="M45.751 45.321c-.92.92-1.907 1.73-2.946 2.431a19.941 19.941 0 0 1-2.946-2.431c-.92-.92-1.73-1.907-2.431-2.946a19.936 19.936 0 0 1 2.431-2.947c.92-.92 1.907-1.73 2.946-2.43a19.935 19.935 0 0 1 2.947 2.43c.92.92 1.73 1.907 2.43 2.947a19.941 19.941 0 0 1-2.43 2.946Z"/>- </g>-</svg>diff --git a/server/nodes/sarvam/meta.json b/server/nodes/sarvam/meta.jsondeleted file mode 100644index 88d41e02..00000000--- a/server/nodes/sarvam/meta.json+++ /dev/null@@ -1,3 +0,0 @@-{- "color": "#6A88E2"-}diff --git a/server/nodes/sarvam/sarvam_detect_language.py b/server/nodes/sarvam/sarvam_detect_language.pydeleted file mode 100644index 1866a831..00000000--- a/server/nodes/sarvam/sarvam_detect_language.py+++ /dev/null@@ -1,93 +0,0 @@-"""Sarvam Detect Language — POST /text-lid (language + script ID)."""--from __future__ import annotations--from typing import Optional--from pydantic import BaseModel, Field--from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue--from ._base import post_json, track_sarvam_usage-from ..model._credentials import SarvamCredential--_MAX_INPUT = 1000---class SarvamDetectLanguageParams(BaseModel):- tool_name: str = Field(- default="sarvam_detect_language",- description="Override name shown to the LLM when used as a tool.",- )- tool_description: str = Field(- default=(- "Identify which Indian language a piece of text is written in, and in which "- "script. Returns BCP-47 language and ISO 15924 script codes."- ),- description="Override description shown to the LLM when used as a tool.",- json_schema_extra={"rows": 3},- )- input: str = Field(- ...,- min_length=1,- description="Text whose language should be identified (max 1000 characters).",- json_schema_extra={"rows": 4},- )-- model_config = {"extra": "ignore"}---class SarvamDetectLanguageOutput(BaseModel):- # Sarvam returns nulls when it cannot classify the input, so both stay- # Optional rather than defaulting to a misleading empty string.- language_code: Optional[str] = None- script_code: Optional[str] = None- request_id: Optional[str] = None-- model_config = {"extra": "allow"}---class SarvamDetectLanguageNode(ActionNode):- type = "sarvamDetectLanguage"- display_name = "Sarvam Detect Language"- subtitle = "Language ID"- group = ("language", "tool")- description = "Identify the language and script of Indic or English text"- component_kind = "square"- tool_name = "sarvam_detect_language"- tool_description = (- "Identify which Indian language a piece of text is written in, and in which script. "- "Returns a BCP-47 language_code (e.g. hi-IN) and an ISO 15924 script_code (e.g. Deva). "- "Both may be null when the text is too short or ambiguous to classify. Useful before "- "calling sarvam_translate with an explicit source language."- )- handles = (- {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},- {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},- )- credentials = (SarvamCredential,)- annotations = {"destructive": False, "readonly": True, "open_world": True}- task_queue = TaskQueue.REST_API- usable_as_tool = True-- Params = SarvamDetectLanguageParams- Output = SarvamDetectLanguageOutput-- @Operation("detect", cost={"service": "sarvam", "action": "detect_language", "count": 1})- async def detect(- self, ctx: NodeContext, params: SarvamDetectLanguageParams- ) -> SarvamDetectLanguageOutput:- if len(params.input) > _MAX_INPUT:- raise NodeUserError(- f"input is {len(params.input)} characters; /text-lid accepts at most "- f"{_MAX_INPUT}. Truncate or sample the text before detecting."- )-- data = await post_json(ctx, "/text-lid", {"input": params.input})-- await track_sarvam_usage(ctx, "detect_language", len(params.input))- return SarvamDetectLanguageOutput(- language_code=data.get("language_code"),- script_code=data.get("script_code"),- request_id=data.get("request_id"),- )diff --git a/server/nodes/sarvam/sarvam_translate.py b/server/nodes/sarvam/sarvam_translate.pydeleted file mode 100644index def6108b..00000000--- a/server/nodes/sarvam/sarvam_translate.py+++ /dev/null@@ -1,141 +0,0 @@-"""Sarvam Translate — POST /translate (Mayura / Sarvam-Translate)."""--from __future__ import annotations--from typing import Literal, Optional--from pydantic import BaseModel, Field--from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue--from ._base import post_json, track_sarvam_usage-from ..model._credentials import SarvamCredential--# Per-model input caps from the Sarvam docs. Enforced client-side so the-# LLM gets an actionable message instead of an opaque 422.-_MAX_INPUT = {"mayura:v1": 1000, "sarvam-translate:v1": 2000}---class SarvamTranslateParams(BaseModel):- tool_name: str = Field(- default="sarvam_translate",- description="Override name shown to the LLM when used as a tool.",- )- tool_description: str = Field(- default=(- "Translate text between English and 22 Indian languages using Sarvam AI. "- "Supports formal, colloquial and code-mixed registers."- ),- description="Override description shown to the LLM when used as a tool.",- json_schema_extra={"rows": 3},- )- input: str = Field(- ...,- min_length=1,- description="Text to translate.",- json_schema_extra={"rows": 4},- )- source_language_code: Literal[- "auto", "as-IN", "bn-IN", "brx-IN", "doi-IN", "en-IN", "gu-IN", "hi-IN",- "kn-IN", "kok-IN", "ks-IN", "mai-IN", "ml-IN", "mni-IN", "mr-IN",- "ne-IN", "od-IN", "pa-IN", "sa-IN", "sat-IN", "sd-IN", "ta-IN",- "te-IN", "ur-IN",- ] = Field(default="auto", description="Source language, or 'auto' to detect.")- target_language_code: Literal[- "as-IN", "bn-IN", "brx-IN", "doi-IN", "en-IN", "gu-IN", "hi-IN",- "kn-IN", "kok-IN", "ks-IN", "mai-IN", "ml-IN", "mni-IN", "mr-IN",- "ne-IN", "od-IN", "pa-IN", "sa-IN", "sat-IN", "sd-IN", "ta-IN",- "te-IN", "ur-IN",- ] = Field(default="hi-IN", description="Language to translate into.")- model: Literal["mayura:v1", "sarvam-translate:v1"] = Field(- default="sarvam-translate:v1",- description="sarvam-translate:v1 covers 22 languages and 2000 chars; mayura:v1 covers 10 and 1000.",- )- mode: Literal["formal", "modern-colloquial", "classic-colloquial", "code-mixed"] = Field(- default="formal", description="Register of the translation."- )- speaker_gender: Optional[Literal["Male", "Female"]] = Field(- default=None,- description="Speaker gender, for languages that inflect on it.",- )- output_script: Optional[Literal["roman", "fully-native", "spoken-form-in-native"]] = Field(- default=None,- description="Transliteration applied to the output. Leave unset for the language's native script.",- )- numerals_format: Literal["international", "native"] = Field(- default="international", description="Render digits as 0-9 or in the native script."- )-- model_config = {"extra": "ignore"}---class SarvamTranslateOutput(BaseModel):- translated_text: str = ""- source_language_code: str = ""- request_id: Optional[str] = None-- model_config = {"extra": "allow"}---class SarvamTranslateNode(ActionNode):- type = "sarvamTranslate"- display_name = "Sarvam Translate"- subtitle = "Indic Translation"- group = ("language", "tool")- description = "Translate text between English and 22 Indian languages via Sarvam AI"- component_kind = "square"- tool_name = "sarvam_translate"- tool_description = (- "Translate text between English and 22 Indian languages using Sarvam AI. "- "Set source_language_code to 'auto' when the input language is unknown. "- "Returns the translated text and the detected source language."- )- handles = (- {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},- {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},- )- credentials = (SarvamCredential,)- annotations = {"destructive": False, "readonly": True, "open_world": True}- task_queue = TaskQueue.REST_API- usable_as_tool = True-- Params = SarvamTranslateParams- Output = SarvamTranslateOutput-- @Operation("translate", cost={"service": "sarvam", "action": "translate", "count": 1})- async def translate(- self, ctx: NodeContext, params: SarvamTranslateParams- ) -> SarvamTranslateOutput:- cap = _MAX_INPUT[params.model]- if len(params.input) > cap:- raise NodeUserError(- f"input is {len(params.input)} characters; {params.model} accepts at most "- f"{cap}. Split the text or switch to sarvam-translate:v1 (2000)."- )- if params.source_language_code == params.target_language_code:- raise NodeUserError(- "source_language_code and target_language_code are identical "- f"({params.target_language_code}) — nothing to translate."- )-- data = await post_json(- ctx,- "/translate",- {- "input": params.input,- "source_language_code": params.source_language_code,- "target_language_code": params.target_language_code,- "model": params.model,- "mode": params.mode,- "speaker_gender": params.speaker_gender,- "output_script": params.output_script,- "numerals_format": params.numerals_format,- },- )-- await track_sarvam_usage(ctx, "translate", len(params.input))- return SarvamTranslateOutput(- translated_text=data.get("translated_text", ""),- source_language_code=data.get("source_language_code", ""),- request_id=data.get("request_id"),- )diff --git a/server/nodes/sarvam/sarvam_transliterate.py b/server/nodes/sarvam/sarvam_transliterate.pydeleted file mode 100644index 7db96473..00000000--- a/server/nodes/sarvam/sarvam_transliterate.py+++ /dev/null@@ -1,121 +0,0 @@-"""Sarvam Transliterate — POST /transliterate (script conversion)."""--from __future__ import annotations--from typing import Literal, Optional--from pydantic import BaseModel, Field--from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue--from ._base import post_json, track_sarvam_usage-from ..model._credentials import SarvamCredential---class SarvamTransliterateParams(BaseModel):- tool_name: str = Field(- default="sarvam_transliterate",- description="Override name shown to the LLM when used as a tool.",- )- tool_description: str = Field(- default=(- "Convert text between scripts while preserving pronunciation — e.g. romanised "- "Hindi to Devanagari. This is NOT translation; meaning is unchanged."- ),- description="Override description shown to the LLM when used as a tool.",- json_schema_extra={"rows": 3},- )- input: str = Field(- ...,- min_length=1,- description="Text to transliterate.",- json_schema_extra={"rows": 4},- )- source_language_code: Literal[- "auto", "bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "ml-IN",- "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN",- ] = Field(default="auto", description="Source language, or 'auto' to detect.")- target_language_code: Literal[- "bn-IN", "en-IN", "gu-IN", "hi-IN", "kn-IN", "ml-IN",- "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN",- ] = Field(default="hi-IN", description="Script to transliterate into.")- numerals_format: Literal["international", "native"] = Field(- default="international", description="Render digits as 0-9 or in the native script."- )- spoken_form: bool = Field(- default=False,- description="Expand numbers, dates and abbreviations into how they are spoken.",- )- spoken_form_numerals_language: Literal["english", "native"] = Field(- default="native",- description="Language used when spelling out numerals in spoken form.",- json_schema_extra={"displayOptions": {"show": {"spoken_form": [True]}}},- )-- model_config = {"extra": "ignore"}---class SarvamTransliterateOutput(BaseModel):- transliterated_text: str = ""- source_language_code: str = ""- request_id: Optional[str] = None-- model_config = {"extra": "allow"}---class SarvamTransliterateNode(ActionNode):- type = "sarvamTransliterate"- display_name = "Sarvam Transliterate"- subtitle = "Script Conversion"- group = ("language", "tool")- description = "Convert text between Indic scripts and Roman while preserving pronunciation"- component_kind = "square"- tool_name = "sarvam_transliterate"- tool_description = (- "Convert text between scripts while preserving pronunciation — e.g. romanised Hindi "- "('namaste') to Devanagari, or the reverse. This is NOT translation: the words and "- "meaning stay the same, only the script changes. Use sarvam_translate to change language."- )- handles = (- {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},- {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},- )- credentials = (SarvamCredential,)- annotations = {"destructive": False, "readonly": True, "open_world": True}- task_queue = TaskQueue.REST_API- usable_as_tool = True-- Params = SarvamTransliterateParams- Output = SarvamTransliterateOutput-- @Operation("transliterate", cost={"service": "sarvam", "action": "transliterate", "count": 1})- async def transliterate(- self, ctx: NodeContext, params: SarvamTransliterateParams- ) -> SarvamTransliterateOutput:- if params.source_language_code == params.target_language_code:- raise NodeUserError(- "source_language_code and target_language_code are identical "- f"({params.target_language_code}) — nothing to transliterate."- )-- data = await post_json(- ctx,- "/transliterate",- {- "input": params.input,- "source_language_code": params.source_language_code,- "target_language_code": params.target_language_code,- "numerals_format": params.numerals_format,- "spoken_form": params.spoken_form,- "spoken_form_numerals_language": (- params.spoken_form_numerals_language if params.spoken_form else None- ),- },- )-- await track_sarvam_usage(ctx, "transliterate", len(params.input))- return SarvamTransliterateOutput(- transliterated_text=data.get("transliterated_text", ""),- source_language_code=data.get("source_language_code", ""),- request_id=data.get("request_id"),- )diff --git a/server/nodes/speech/_base.py b/server/nodes/speech/_base.pyindex 3960c2e6..c8f9aa6d 100644--- a/server/nodes/speech/_base.py+++ b/server/nodes/speech/_base.py@@ -6,108 +6,19 @@ parts both nodes need and neither should own.from __future__ import annotations-import jsonfrom pathlib import Path-from typing import Any, Dict, Optional, Sequence, Tuple, Union, get_args, get_origin+from typing import Any, Dict, Optional, Tuplefrom core.logging import get_loggerfrom services.media import coerce_file_param, read_media_bytes, resolve_mediafrom services.media.inspect import inspect_audio-from services.plugin import NodeContext, NodeUserError+from services.plugin import NodeContext, NodeUserError, coerce_blank_params # noqa: F401from . import _config as speech_configlogger = get_logger(__name__)-# ----------------------------------------------------------------------------# Parameter coercion-# ----------------------------------------------------------------------------#-# The parameter panel stores "" for a field the user has cleared or never-# filled, whatever the declared type. For a `str` field that is harmless, but-# `""` against `Optional[float]`, `bool` or `Dict[str, Any]` is a hard-# validation error -- "Input should be a valid dictionary" being the one that-# surfaced first.-#-# It is not enough to fix the dict field: `speed`, `sample_rate`, `translate`,-# `diarize` and `timestamps` all fail the same way. So the rule is general ---# a blank string against a field that cannot hold a string means "unset", and-# is dropped so the field's own default applies.-#-# Same class of problem the repo already solves per-node with-# `AndroidServiceParams._coerce_parameters` and `WriteTodosParams._coerce_todos`;-# this is the reusable form.---def _accepts_str(annotation: Any) -> bool:- """Whether a blank string is a legitimate value for this annotation.-- `Dict[str, Any]` is the case worth spelling out: its `get_args` are- `(str, Any)`, so a naive "is str among the args" check would wrongly- conclude a dict field accepts a string. Container origins are rejected- before the args are consulted.- """- if annotation is Any or annotation is str:- return True- origin = get_origin(annotation)- if origin is Union:- return any(_accepts_str(arg) for arg in get_args(annotation))- if origin is not None:- return False- return False---def coerce_blank_params(cls: Any, values: Any, *, object_fields: Sequence[str] = ()) -> Any:- """Normalize panel-supplied blanks before Pydantic validates them.-- Fields named in ``object_fields`` additionally accept a JSON **object**- string, because the panel has no object widget and renders them as a text- input -- so a user who types `{"instructions": "cheerful"}` gets what they- plainly meant rather than a type error.- """- if not isinstance(values, dict):- return values-- cleaned: Dict[str, Any] = {}- for key, value in values.items():- if key in object_fields:- cleaned[key] = _coerce_object(key, value)- continue-- field = cls.model_fields.get(key)- blank = isinstance(value, str) and not value.strip()- if blank and field is not None and not _accepts_str(field.annotation):- continue # drop -> the field default applies- cleaned[key] = value- return cleaned---def _coerce_object(key: str, value: Any) -> Dict[str, Any]:- if isinstance(value, dict):- return value- if value is None:- return {}- if isinstance(value, str):- text = value.strip()- if not text:- return {}- try:- parsed = json.loads(text)- except ValueError as exc:- raise ValueError(- f"{key} must be a JSON object, e.g. "- '{"instructions": "speak cheerfully"}. Could not parse it: '- f"{exc}."- ) from exc- if isinstance(parsed, dict):- return parsed- raise ValueError(- f"{key} must be a JSON object, not a {type(parsed).__name__}."- )- raise ValueError(f"{key} must be a JSON object, not a {type(value).__name__}.")--async def provider_api_key(ctx: NodeContext, provider: str) -> str:"""Resolve the stored key for ``provider`` through its credential.diff --git a/server/nodes/speech/_config.py b/server/nodes/speech/_config.pyindex 7d0a4c84..81072961 100644--- a/server/nodes/speech/_config.py+++ b/server/nodes/speech/_config.py@@ -1,111 +1,50 @@"""Capability lookups backed by ``server/config/speech_defaults.json``.-Same contract as :mod:`services.llm.config`: the JSON is loaded once at-import into a module-level dict, failure is soft, and every capability-question is answered from here so no shared code ever branches on a provider-name.--The one idea worth understanding is :func:`capability`. A capability value in-the JSON is either a plain scalar (applies to every model) or a mapping of-model id to value with a ``_default`` fallback. Callers never care which was-written -- they ask for ``(provider, direction, key, model)`` and get a value.-That is what lets OpenAI declare "``response_formats`` is ``json|text`` except-on whisper-1 where it is five formats" without a line of Python.--Lookup order inside a mapping is exact match, then longest prefix match, then-``_default``. Prefix matching earns its keep on dated snapshots -- a key of-``gpt-4o-mini-transcribe`` also answers for-``gpt-4o-mini-transcribe-2025-12-15`` without the JSON tracking every release.+The resolver itself lives in+:class:`services.plugin.capabilities.CapabilityConfig`, shared with+``nodes/translate``. This module is the speech-specific instance plus the+free-function API the nodes and loaders import — the same shim relationship+``services/llm/registry.py`` has with ``services/provider_registry``."""from __future__ import annotations-import json-from pathlib import Path-from typing import Any, Dict, List, Optional+from typing import Any, List, Optional-from core.logging import get_logger--logger = get_logger(__name__)+from services.plugin.capabilities import CapabilityConfigTTS = "tts"STT = "stt"-_CONFIG_PATH = Path(__file__).parent.parent.parent / "config" / "speech_defaults.json"---def _load_speech_defaults() -> Dict[str, Any]:- try:- with open(_CONFIG_PATH, encoding="utf-8") as handle:- return json.load(handle)- except Exception as exc:- # Soft-fail, matching services/llm/config.py: a malformed config- # degrades capability lookups to their permissive defaults rather- # than preventing the process from booting.- logger.warning("Could not load speech_defaults.json", error=str(exc))- return {"providers": {}}+CONFIG = CapabilityConfig("speech_defaults.json")--SPEECH_DEFAULTS: Dict[str, Any] = _load_speech_defaults()+# Call sites (and tests) that read the parsed document directly, e.g. to+# monkeypatch in a synthetic provider block.+SPEECH_DEFAULTS = CONFIG.datadef reload_defaults() -> None:- """Re-read the JSON from disk. Mutates in place.+ CONFIG.reload()- In-place rather than rebinding because callers may hold a reference to- the dict (the unifier does), and rebinding would leave them on the stale- object -- a trap ``services/llm/config.reload_defaults`` still has.- """- fresh = _load_speech_defaults()- SPEECH_DEFAULTS.clear()- SPEECH_DEFAULTS.update(fresh)+def provider_block(provider: str) -> dict:+ return CONFIG.provider_block(provider)-# ----------------------------------------------------------------------------# Raw block access-# -----------------------------------------------------------------------------def provider_block(provider: str) -> Dict[str, Any]:- """The whole ``providers.<name>`` block, or ``{}``."""- block = SPEECH_DEFAULTS.get("providers", {}).get(provider)- return block if isinstance(block, dict) else {}---def direction_block(provider: str, direction: str) -> Dict[str, Any]:- """The ``providers.<name>.<tts|stt>`` block, or ``{}``."""- block = provider_block(provider).get(direction)- return block if isinstance(block, dict) else {}+def direction_block(provider: str, direction: str) -> dict:+ return CONFIG.section(provider, direction)def supports_direction(provider: str, direction: str) -> bool:- """Whether the JSON declares this provider for this direction.-- Advisory only -- registry membership is the authority (a provider that- never calls ``register_tts_provider`` cannot be selected regardless of- what the JSON says). Useful for config-consistency tests.- """- return bool(direction_block(provider, direction))+ return CONFIG.supports_section(provider, direction)def base_url(provider: str) -> str:- return str(provider_block(provider).get("base_url") or "")+ return CONFIG.base_url(provider)def credential_id(provider: str) -> str:- """Credential id string for this provider.-- A *string*, never a ``Credential`` class: the classes live under- ``nodes/`` and importing one here would invert the layering and break- ``test_plugin_self_containment.py``. Falls back to the provider name,- which is the convention every current provider follows.- """- return str(provider_block(provider).get("credential_id") or provider)---# ----------------------------------------------------------------------------# Capability resolution-# ---------------------------------------------------------------------------+ return CONFIG.credential_id(provider)def capability(@@ -116,81 +55,51 @@ def capability(model: Optional[str] = None,default: Any = None,) -> Any:- """Resolve one capability, honouring per-model overrides.-- Returns ``default`` when the key is absent entirely. A declared ``null``- is returned as ``None`` and is meaningful -- Deepgram's- ``max_upload_bytes`` is explicitly null because no cap is documented,- which is different from "not configured".- """- block = direction_block(provider, direction)- if key not in block:- return default-- value = block[key]- if not isinstance(value, dict):- return value-- # A mapping means per-model overrides. Without a model we can still- # answer from _default, which is what the panel does before the user- # has picked one.- if model:- if model in value:- return value[model]- prefix_matches = [k for k in value if k != "_default" and model.startswith(k)]- if prefix_matches:- return value[max(prefix_matches, key=len)]- if "_default" in value:- return value["_default"]- return default+ return CONFIG.capability(provider, direction, key, model=model, default=default)def default_model(provider: str, direction: str) -> str:- return str(capability(provider, direction, "default_model", default="") or "")+ return CONFIG.default_model(provider, direction)def models(provider: str, direction: str) -> List[str]:- value = capability(provider, direction, "models", default=[])- return list(value or [])+ return CONFIG.models(provider, direction)def voices(provider: str, *, model: Optional[str] = None) -> List[str]:"""Static voice ids from config.- Only a fallback. Providers that expose a live voices endpoint- (ElevenLabs) answer the dropdown from the API instead, because their- catalogue is per-account and changes without a release.+ Only a fallback. Providers with a live voices endpoint (ElevenLabs)+ answer the dropdown from the API instead, because their catalogue is+ per-account and changes without a release."""- value = capability(provider, TTS, "voices", model=model, default=[])- return list(value or [])+ return CONFIG.listing(provider, TTS, "voices", model=model)def default_voice(provider: str) -> str:- return str(capability(provider, TTS, "default_voice", default="") or "")+ return str(CONFIG.capability(provider, TTS, "default_voice", default="") or "")def output_formats(provider: str, direction: str = TTS) -> List[str]:- return list(capability(provider, direction, "output_formats", default=[]) or [])+ return CONFIG.listing(provider, direction, "output_formats")def billed_unit(provider: str, direction: str) -> str:- """``characters`` / ``seconds`` / ``minutes`` -- for cost attribution."""- return str(capability(provider, direction, "billed_unit", default="") or "")+ return CONFIG.billed_unit(provider, direction)def endpoint(provider: str, direction: str, key: str = "endpoint") -> str:- return str(capability(provider, direction, key, default="") or "")+ return CONFIG.endpoint(provider, direction, key)def max_input_chars(provider: str, model: Optional[str] = None) -> Optional[int]:- value = capability(provider, TTS, "max_input_chars", model=model)- return int(value) if isinstance(value, (int, float)) else None+ return CONFIG.max_input_chars(provider, TTS, model)def speed_range(provider: str, model: Optional[str] = None) -> Optional[tuple[float, float]]:- value = capability(provider, TTS, "speed_range", model=model)+ value = CONFIG.capability(provider, TTS, "speed_range", model=model)if isinstance(value, (list, tuple)) and len(value) == 2:return float(value[0]), float(value[1])return None@@ -199,13 +108,7 @@ def speed_range(def supports(provider: str, direction: str, key: str, *, model: Optional[str] = None) -> bool:- """Boolean capability probe.-- Defaults to ``True`` -- permissive, matching ``supports_model_listing``- in the LLM layer. A provider opts *out* explicitly; forgetting to- declare a flag never silently disables a working feature.- """- return bool(capability(provider, direction, key, model=model, default=True))+ return CONFIG.supports(provider, direction, key, model=model)def response_formats(provider: str, model: Optional[str] = None) -> List[str]:@@ -214,12 +117,11 @@ def response_formats(provider: str, model: Optional[str] = None) -> List[str]:Model-gated on OpenAI and a 400 when violated, which is why this isconfig rather than something the node discovers at runtime."""- return list(- capability(provider, STT, "response_formats", model=model, default=[]) or []- )+ return CONFIG.listing(provider, STT, "response_formats", model=model)__all__ = [+ "CONFIG","SPEECH_DEFAULTS","STT","TTS",diff --git a/server/nodes/translate/__init__.py b/server/nodes/translate/__init__.pynew file mode 100644index 00000000..9f02a377--- /dev/null+++ b/server/nodes/translate/__init__.py@@ -0,0 +1,52 @@+"""Translate plugin — provider-abstracted text-language capabilities.++Three nodes, each with a `provider` dropdown, replacing the vendor-locked+``sarvamTranslate`` / ``sarvamTransliterate`` / ``sarvamDetectLanguage``.++Built on the same pattern as ``nodes/speech/`` and sharing its machinery:+``services/provider_registry`` for registration and+``services/plugin/capabilities`` for the JSON-backed lookups. Speech owns two+registries (one per direction); this owns **three**, one per capability,+because they are separately supported — DeepL translates and nothing else,+while an LLM does all three.++Nothing under ``services/`` knows a vendor name. No media transport is+involved: text results are small, so none of ``services/media`` applies.++Layout::++ translate_text.py / transliterate_text.py / detect_language.py+ _protocol.py requests, results, errors, three Protocols+ _registry.py one registry per capability+ _config.py translate_defaults.json lookups+ _unifier.py dispatch + error translation+ _providers/ deepl (translate only), sarvam (all three), llm (all three)+ _credentials.py DeepL only — the rest are shared with nodes/model+ _base.py, _option_loaders.py+"""++from __future__ import annotations++from services.node_output_schemas import register_output_schema+from services.ws_handler_registry import register_option_loader++from ._option_loaders import LOADERS+from .detect_language import DetectLanguageNode, DetectLanguageOutput+from .translate_text import TranslateTextNode, TranslateTextOutput+from .transliterate_text import TransliterateTextNode, TransliterateTextOutput++register_output_schema(TranslateTextNode.type, TranslateTextOutput)+register_output_schema(TransliterateTextNode.type, TransliterateTextOutput)+register_output_schema(DetectLanguageNode.type, DetectLanguageOutput)++for _name, _loader in LOADERS.items():+ register_option_loader(_name, _loader)++__all__ = [+ "DetectLanguageNode",+ "DetectLanguageOutput",+ "TranslateTextNode",+ "TranslateTextOutput",+ "TransliterateTextNode",+ "TransliterateTextOutput",+]diff --git a/server/nodes/translate/_base.py b/server/nodes/translate/_base.pynew file mode 100644index 00000000..a5cdf049--- /dev/null+++ b/server/nodes/translate/_base.py@@ -0,0 +1,97 @@+"""Shared helpers for the three text-language nodes."""++from __future__ import annotations++from typing import Optional, Sequence++from core.logging import get_logger+from services.plugin import NodeContext, NodeUserError++from . import _config as translate_config++logger = get_logger(__name__)+++async def provider_api_key(ctx: NodeContext, provider: str) -> str:+ """Resolve the stored key through the provider's credential.++ Goes through ``ctx.connection(...)`` rather than the auth service so a+ missing key raises the framework's annotated ``PermissionError``, which+ ``BaseNode.execute`` turns into a credential envelope plus a CloudEvents+ broadcast — lighting up the right provider in the Credentials modal.+ """+ credential_id = translate_config.credential_id(provider)+ async with ctx.connection(credential_id) as conn:+ secrets = await conn.credentials()+ api_key = str(secrets.get("api_key") or "")+ if not api_key:+ raise NodeUserError(+ f"No API key stored for '{credential_id}'. Add one in the "+ "Credentials modal."+ )+ return api_key+++def require_provider(provider: str, available: Sequence[str], capability: str) -> str:+ if provider in available:+ return provider+ raise NodeUserError(+ f"'{provider}' does not do {capability}. Available: {', '.join(available)}."+ )+++def check_length(provider: str, capability: str, model: str, text: str) -> None:+ """Refuse over-long input before spending a paid call on it."""+ cap = translate_config.max_input_chars(provider, capability, model)+ if cap and len(text) > cap:+ raise NodeUserError(+ f"Text is {len(text)} characters; {provider}"+ f"{'/' + model if model else ''} accepts at most {cap}. Shorten it "+ "or split it across several runs."+ )+++async def track_usage(+ ctx: NodeContext,+ *,+ provider: str,+ operation: str,+ units: Optional[float],+ unit: str,+) -> None:+ """Record an API usage metric. Never raises.++ Skipped entirely for token-billed providers: an LLM-backed translation is+ already costed by the LLM layer, and recording a character count against+ ``api_pricing`` as well would double-count the same call.+ """+ if unit == "tokens" or units is None or units <= 0:+ return+ try:+ from services.plugin.deps import get_database+ from services.pricing import get_pricing_service++ cost = get_pricing_service().calculate_api_cost(provider, operation, units)+ await get_database().save_api_usage_metric(+ {+ "session_id": ctx.raw.get("session_id", "default"),+ "node_id": ctx.node_id,+ "workflow_id": ctx.workflow_id,+ "service": provider,+ "operation": cost.get("operation", operation),+ "endpoint": operation,+ "resource_count": units,+ "cost": cost.get("total_cost", 0.0),+ }+ )+ except Exception as exc:+ # Attribution must never fail a workflow that already did the work.+ logger.warning(+ "failed to record translate usage",+ provider=provider,+ operation=operation,+ error=str(exc),+ )+++__all__ = ["check_length", "provider_api_key", "require_provider", "track_usage"]diff --git a/server/nodes/translate/_config.py b/server/nodes/translate/_config.pynew file mode 100644index 00000000..386f8702--- /dev/null+++ b/server/nodes/translate/_config.py@@ -0,0 +1,125 @@+"""Capability lookups backed by ``server/config/translate_defaults.json``.++Thin instance over :class:`services.plugin.capabilities.CapabilityConfig`,+the same resolver ``nodes/speech`` uses. Sections here are the three+capabilities rather than two directions.+"""++from __future__ import annotations++from typing import Any, List, Optional++from services.plugin.capabilities import CapabilityConfig++TRANSLATE = "translate"+TRANSLITERATE = "transliterate"+DETECT = "detect"++CONFIG = CapabilityConfig("translate_defaults.json")+TRANSLATE_DEFAULTS = CONFIG.data+++def reload_defaults() -> None:+ CONFIG.reload()+++def base_url(provider: str, *, api_key: str = "") -> str:+ """Base URL, honouring DeepL's free-vs-pro host split.++ A DeepL free key ends in ``:fx`` and is rejected by the pro host with a+ 403 that says nothing useful. Selecting the host from the key means a+ user never has to know which tier they hold, and cannot mis-configure it.+ """+ block = CONFIG.provider_block(provider)+ free = block.get("free_base_url")+ if free and api_key.endswith(":fx"):+ return str(free)+ return CONFIG.base_url(provider)+++def credential_id(provider: str) -> str:+ return CONFIG.credential_id(provider)+++def capability(+ provider: str,+ section: str,+ key: str,+ *,+ model: Optional[str] = None,+ default: Any = None,+) -> Any:+ return CONFIG.capability(provider, section, key, model=model, default=default)+++def supports(+ provider: str, section: str, key: str, *, model: Optional[str] = None+) -> bool:+ return CONFIG.supports(provider, section, key, model=model)+++def supports_capability(provider: str, section: str) -> bool:+ return CONFIG.supports_section(provider, section)+++def default_model(provider: str, section: str) -> str:+ return CONFIG.default_model(provider, section)+++def models(provider: str, section: str) -> List[str]:+ return CONFIG.models(provider, section)+++def endpoint(provider: str, section: str, key: str = "endpoint") -> str:+ return CONFIG.endpoint(provider, section, key)+++def languages(+ provider: str, section: str, *, model: Optional[str] = None+) -> List[str]:+ return CONFIG.languages(provider, section, model=model)+++def default_target_language(provider: str, section: str) -> str:+ return str(+ CONFIG.capability(provider, section, "default_target_language", default="") or ""+ )+++def max_input_chars(+ provider: str, section: str, model: Optional[str] = None+) -> Optional[int]:+ return CONFIG.max_input_chars(provider, section, model)+++def billed_unit(provider: str, section: str) -> str:+ return CONFIG.billed_unit(provider, section)+++def listing(+ provider: str, section: str, key: str, *, model: Optional[str] = None+) -> List[Any]:+ return CONFIG.listing(provider, section, key, model=model)+++__all__ = [+ "CONFIG",+ "DETECT",+ "TRANSLATE",+ "TRANSLATE_DEFAULTS",+ "TRANSLITERATE",+ "base_url",+ "billed_unit",+ "capability",+ "credential_id",+ "default_model",+ "default_target_language",+ "endpoint",+ "languages",+ "listing",+ "max_input_chars",+ "models",+ "reload_defaults",+ "supports",+ "supports_capability",+]diff --git a/server/nodes/translate/_credentials.py b/server/nodes/translate/_credentials.pynew file mode 100644index 00000000..2739f0ac--- /dev/null+++ b/server/nodes/translate/_credentials.py@@ -0,0 +1,70 @@+"""Credentials for translate-only providers.++Only DeepL. Sarvam and OpenAI already have credentials under+``nodes/model/_credentials.py`` — the same stored key authenticates their chat+surfaces, and a second class for the same ``id`` would collide in+``CREDENTIAL_REGISTRY``.+"""++from __future__ import annotations++from typing import Any, Dict++from services.plugin.credential import ApiKeyCredential, ProbeResult+++class DeepLCredential(ApiKeyCredential):+ """DeepL API key.++ The scheme keyword is ``DeepL-Auth-Key``, which none of the three built-in+ ``key_location`` modes can express — ``header`` sends the raw key with no+ keyword, ``bearer`` sends the wrong one. Hence the :meth:`inject`+ override, which the declarative probe also routes through.++ Free and Pro keys go to *different hosts*: a free key ends in ``:fx`` and+ the Pro host rejects it with a 403 that explains nothing. The probe picks+ the host from the key, so validation succeeds on either tier and the user+ never has to know which they hold.+ """++ id = "deepl"+ display_name = "DeepL"+ category = "AI"+ key_name = "Authorization"+ key_location = "header"+ docs_url = "https://www.deepl.com/pro-api"++ @classmethod+ def inject(+ cls, secrets: Dict[str, Any], request: Dict[str, Any]+ ) -> Dict[str, Any]:+ headers = dict(request.get("headers") or {})+ headers["Authorization"] = f"DeepL-Auth-Key {secrets.get('api_key', '')}"+ return {**request, "headers": headers}++ @classmethod+ async def _probe(cls, api_key: str) -> ProbeResult:+ import httpx++ base = (+ "https://api-free.deepl.com/v2"+ if api_key.endswith(":fx")+ else "https://api.deepl.com/v2"+ )+ async with httpx.AsyncClient(timeout=10.0) as client:+ response = await client.get(+ f"{base}/usage",+ headers={"Authorization": f"DeepL-Auth-Key {api_key}"},+ )+ response.raise_for_status()+ usage = response.json()++ used = usage.get("character_count")+ limit = usage.get("character_limit")+ message = "API key validated"+ if isinstance(used, int) and isinstance(limit, int) and limit:+ message = f"API key validated — {used:,} of {limit:,} characters used"+ return ProbeResult(valid=True, message=message)+++__all__ = ["DeepLCredential"]diff --git a/server/nodes/translate/_option_loaders.py b/server/nodes/translate/_option_loaders.pynew file mode 100644index 00000000..c8372881--- /dev/null+++ b/server/nodes/translate/_option_loaders.py@@ -0,0 +1,146 @@+"""Dropdown loaders for the three text-language nodes.++Each reads ``provider`` out of the parameter dict it is handed rather than+declaring ``loadOptionsDependsOn`` — that attribute is lifted by the client+adapter but has no usages anywhere in the codebase, so it is untested+machinery. The parameter dict is how every existing loader works and re-fires+when parameters change.+"""++from __future__ import annotations++from typing import Any, Callable, Dict, List++from core.logging import get_logger++from . import _config as translate_config+from ._registry import CAPABILITIES++logger = get_logger(__name__)++# Which capability a node is asking about, inferred from the model field name+# it declares. Each node has a distinct one, which doubles as the signal.+_MODEL_FIELD_TO_CAPABILITY = {+ "translate_model": translate_config.TRANSLATE,+ "transliterate_model": translate_config.TRANSLITERATE,+ "detect_model": translate_config.DETECT,+}++_NODE_TYPE_TO_CAPABILITY = {+ "translateText": translate_config.TRANSLATE,+ "transliterateText": translate_config.TRANSLITERATE,+ "detectLanguage": translate_config.DETECT,+}+++def _capability_for(params: Dict[str, Any]) -> str:+ node_type = str(params.get("node_type") or "")+ if node_type in _NODE_TYPE_TO_CAPABILITY:+ return _NODE_TYPE_TO_CAPABILITY[node_type]+ for field, capability in _MODEL_FIELD_TO_CAPABILITY.items():+ if field in params:+ return capability+ return translate_config.TRANSLATE+++def _selected_provider(params: Dict[str, Any], capability: str) -> str:+ provider = str(params.get("provider") or "")+ available = CAPABILITIES[capability][0]()+ if provider in available:+ return provider+ return available[0] if available else ""+++def _for_capability(+ capability: str, build: Callable[[str, str, Dict[str, Any]], List[Dict[str, Any]]]+):+ """Bind a builder to one capability, for the per-node loader names."""++ async def loader(params: Dict[str, Any]) -> List[Dict[str, Any]]:+ provider = _selected_provider(params, capability)+ if not provider:+ return []+ return build(provider, capability, params)++ return loader+++def _models(provider: str, capability: str, params: Dict[str, Any]):+ default = translate_config.default_model(provider, capability)+ options: List[Dict[str, Any]] = []+ for model in translate_config.models(provider, capability):+ option: Dict[str, Any] = {"value": model, "label": model}+ if model == default:+ option["description"] = "Provider default"+ options.append(option)+ return options+++def _languages(provider: str, capability: str, params: Dict[str, Any]):+ model = str(+ params.get("translate_model")+ or params.get("transliterate_model")+ or ""+ )+ return [+ {"value": code, "label": code}+ for code in translate_config.languages(+ provider, capability, model=model or None+ )+ ]+++def _source_languages(provider: str, capability: str, params: Dict[str, Any]):+ options = [+ {"value": "", "label": "Auto-detect", "description": "Let the provider decide"}+ ]+ options.extend(_languages(provider, capability, params))+ return options+++def _scripts(provider: str, capability: str, params: Dict[str, Any]):+ return [+ {"value": script, "label": script.replace("-", " ")}+ for script in translate_config.listing(provider, capability, "scripts")+ ]+++def _formality(provider: str, capability: str, params: Dict[str, Any]):+ """Register / formality, which every vendor names differently.++ DeepL calls the values formality options; Sarvam calls the same idea+ ``mode``. Both are read from their own config key so the node keeps one+ field.+ """+ values = translate_config.listing(+ provider, capability, "formality_options"+ ) or translate_config.listing(provider, capability, "modes")+ if not values:+ return []+ return [{"value": "", "label": "Provider default"}] + [+ {"value": value, "label": value.replace("-", " ")} for value in values+ ]+++# One registered loader per (capability, field). Names must match the+# `loadOptionsMethod` strings on the Params fields.+LOADERS = {+ "translateModels": _for_capability(translate_config.TRANSLATE, _models),+ "translateLanguages": _for_capability(translate_config.TRANSLATE, _languages),+ "translateSourceLanguages": _for_capability(+ translate_config.TRANSLATE, _source_languages+ ),+ "translateFormality": _for_capability(translate_config.TRANSLATE, _formality),+ "transliterateModels": _for_capability(translate_config.TRANSLITERATE, _models),+ "transliterateLanguages": _for_capability(+ translate_config.TRANSLITERATE, _languages+ ),+ "transliterateSourceLanguages": _for_capability(+ translate_config.TRANSLITERATE, _source_languages+ ),+ "transliterateScripts": _for_capability(translate_config.TRANSLITERATE, _scripts),+ "detectModels": _for_capability(translate_config.DETECT, _models),+}+++__all__ = ["LOADERS"]diff --git a/server/nodes/translate/_protocol.py b/server/nodes/translate/_protocol.pynew file mode 100644index 00000000..24c6f593--- /dev/null+++ b/server/nodes/translate/_protocol.py@@ -0,0 +1,394 @@+"""Provider-neutral text-language types.++Same shape as :mod:`nodes.speech._protocol`, minus everything that existed+there to keep audio bytes out of the engine. Text results are small, so there+is no ``FileRef`` equivalent and none of ``services.media`` is involved.++**Three capabilities, three registries.** Translation, transliteration and+language identification are related but not interchangeable: DeepL translates+and does nothing else, ICU transliterates without translating, and a+general-purpose LLM does all three. One registry with capability flags would+put a provider in a dropdown it cannot serve and fail at runtime; membership+per capability makes that unrepresentable. This is the same argument that gave+speech two registries rather than one.+"""++from __future__ import annotations++from dataclasses import dataclass, field+from enum import Enum+from typing import Any, Dict, List, Mapping, Optional, Protocol, runtime_checkable++# ---------------------------------------------------------------------------+# Requests+# ---------------------------------------------------------------------------+++@dataclass+class TranslateRequest:+ """One translation call.++ ``text`` is a list because the batch shape is the honest one: DeepL takes+ an array natively, and a provider that only handles one string at a time+ can loop. Modelling it as a single string would make batching impossible+ to add later without breaking every caller.+ """++ text: List[str]+ target_language: str+ source_language: str = ""+ model: str = ""+ formality: str = ""+ context: str = ""+ preserve_formatting: bool = False+ provider_options: Dict[str, Any] = field(default_factory=dict)+++@dataclass+class TransliterateRequest:+ """Script conversion — same language, different writing system."""++ text: List[str]+ target_script: str = ""+ source_language: str = ""+ target_language: str = ""+ model: str = ""+ # Numerals and spoken-form handling differ enough between vendors that+ # they stay in provider_options rather than being half-normalised.+ provider_options: Dict[str, Any] = field(default_factory=dict)+++@dataclass+class DetectRequest:+ """Language identification."""++ text: List[str]+ model: str = ""+ provider_options: Dict[str, Any] = field(default_factory=dict)+++# ---------------------------------------------------------------------------+# Results+# ---------------------------------------------------------------------------+++@dataclass+class TranslatedText:+ text: str = ""+ detected_source_language: Optional[str] = None+++@dataclass+class TranslateResult:+ translations: List[TranslatedText] = field(default_factory=list)+ model: str = ""+ request_id: Optional[str] = None+ # Reported by the provider when it tells us (DeepL returns+ # `billed_characters` per translation, which beats counting input+ # ourselves); otherwise the node falls back to input length.+ billed_units: Optional[float] = None+ billed_unit: str = "characters"++ @property+ def text(self) -> str:+ """First translation, for the single-input common case."""+ return self.translations[0].text if self.translations else ""+++@dataclass+class TransliterateResult:+ results: List[str] = field(default_factory=list)+ model: str = ""+ request_id: Optional[str] = None+ billed_units: Optional[float] = None+ billed_unit: str = "characters"++ @property+ def text(self) -> str:+ return self.results[0] if self.results else ""+++@dataclass+class DetectedLanguage:+ language: str = ""+ script: Optional[str] = None+ confidence: Optional[float] = None+++@dataclass+class DetectResult:+ detections: List[DetectedLanguage] = field(default_factory=list)+ model: str = ""+ request_id: Optional[str] = None+ billed_units: Optional[float] = None+ billed_unit: str = "characters"++ @property+ def language(self) -> str:+ return self.detections[0].language if self.detections else ""+++@dataclass+class LanguageOption:+ """One selectable language, for the dropdown loaders."""++ code: str+ name: str = ""++ def as_option(self) -> Dict[str, str]:+ return {"value": self.code, "label": self.name or self.code}+++# ---------------------------------------------------------------------------+# Errors+# ---------------------------------------------------------------------------+++class TranslateErrorCategory(str, Enum):+ AUTHENTICATION = "authentication"+ PERMISSION = "permission"+ QUOTA = "quota"+ RATE_LIMIT = "rate_limit"+ INVALID_REQUEST = "invalid_request"+ UNSUPPORTED_LANGUAGE = "unsupported_language"+ NOT_FOUND = "not_found"+ TEXT_TOO_LONG = "text_too_long"+ TIMEOUT = "timeout"+ CONNECTION = "connection"+ SERVER = "server"+ UNKNOWN = "unknown"+++_RETRYABLE = {+ TranslateErrorCategory.RATE_LIMIT,+ TranslateErrorCategory.TIMEOUT,+ TranslateErrorCategory.CONNECTION,+ TranslateErrorCategory.SERVER,+}++_PROVIDER_NAMES = {+ "deepl": "DeepL",+ "sarvam": "Sarvam AI",+ "openai": "OpenAI",+}+++@dataclass+class TranslateError(Exception):+ """Provider-independent structured failure."""++ message: str+ provider: str+ category: TranslateErrorCategory = TranslateErrorCategory.UNKNOWN+ retryable: bool = False+ status_code: Optional[int] = None+ provider_code: Optional[str] = None+ request_id: Optional[str] = None++ def __post_init__(self) -> None:+ Exception.__init__(self, self.message)++ @property+ def user_message(self) -> str:+ """Sanitized message — the only field allowed across a boundary.++ ``message`` keeps the raw provider text for operator diagnostics, and+ provider bodies routinely echo request fragments.+ """+ key = str(self.provider or "").strip().lower()+ known = key in _PROVIDER_NAMES+ provider = _PROVIDER_NAMES.get(key, "The translation provider")+ obj = provider if known else "the translation provider"+ category = (+ self.category.value+ if isinstance(self.category, TranslateErrorCategory)+ else str(self.category or TranslateErrorCategory.UNKNOWN.value)+ )+ messages = {+ TranslateErrorCategory.AUTHENTICATION.value: (+ f"{provider} authentication failed. Check the configured API key."+ ),+ TranslateErrorCategory.PERMISSION.value: (+ f"{provider} denied this request. Check the account and plan."+ ),+ TranslateErrorCategory.QUOTA.value: (+ f"The {obj} character quota is exhausted for this billing period."+ ),+ TranslateErrorCategory.RATE_LIMIT.value: (+ f"{provider} is rate-limiting requests. Retry after a short delay."+ ),+ TranslateErrorCategory.INVALID_REQUEST.value: (+ f"{provider} rejected the request configuration."+ ),+ TranslateErrorCategory.UNSUPPORTED_LANGUAGE.value: (+ f"{provider} does not support that language pair."+ ),+ TranslateErrorCategory.NOT_FOUND.value: (+ f"The configured {obj} model or endpoint was not found."+ ),+ TranslateErrorCategory.TEXT_TOO_LONG.value: (+ f"The text exceeds what {obj} accepts in one request. Split it."+ ),+ TranslateErrorCategory.TIMEOUT.value: f"The request to {obj} timed out.",+ TranslateErrorCategory.CONNECTION.value: f"Could not connect to {obj}.",+ TranslateErrorCategory.SERVER.value: (+ f"{provider} is temporarily unavailable."+ ),+ TranslateErrorCategory.UNKNOWN.value: f"{provider} request failed.",+ }+ return messages.get(category, messages[TranslateErrorCategory.UNKNOWN.value])++ @classmethod+ def from_exception(cls, provider: str, exc: BaseException) -> "TranslateError":+ response = getattr(exc, "response", None)+ status = _optional_int(+ getattr(exc, "status_code", None)+ or getattr(response, "status_code", None)+ )+ body = _response_text(response)+ category = classify(exc, status, body)+ return cls(+ message=_detail(exc, body),+ provider=provider,+ category=category,+ retryable=category in _RETRYABLE,+ status_code=status,+ provider_code=_provider_code(response),+ request_id=_header(response, "x-request-id"),+ )+++def classify(+ exc: BaseException, status: Optional[int], body_text: str = ""+) -> TranslateErrorCategory:+ """Map an exception plus HTTP status onto a category.++ 456 is DeepL-specific and worth handling by number: it means the character+ quota is exhausted, which is a billing problem the operator must act on,+ not a transient failure to retry.+ """+ name = type(exc).__name__.lower()+ haystack = f"{exc} {body_text}".lower()++ if status in (401, 403) or "authentication" in name or "api key" in haystack:+ return TranslateErrorCategory.AUTHENTICATION+ if status == 456 or "quota" in haystack:+ return TranslateErrorCategory.QUOTA+ if status == 429 or "ratelimit" in name or "rate limit" in haystack:+ return TranslateErrorCategory.RATE_LIMIT+ if status == 404 or "notfound" in name:+ return TranslateErrorCategory.NOT_FOUND+ if status == 413 or "too long" in haystack or "too large" in haystack:+ return TranslateErrorCategory.TEXT_TOO_LONG+ if "language" in haystack and ("support" in haystack or "invalid" in haystack):+ return TranslateErrorCategory.UNSUPPORTED_LANGUAGE+ if status in {400, 409, 422} or "badrequest" in name:+ return TranslateErrorCategory.INVALID_REQUEST+ if status == 408 or "timeout" in name:+ return TranslateErrorCategory.TIMEOUT+ if "connect" in name:+ return TranslateErrorCategory.CONNECTION+ if status is not None and status >= 500:+ return TranslateErrorCategory.SERVER+ return TranslateErrorCategory.UNKNOWN+++def _optional_int(value: Any) -> Optional[int]:+ try:+ return int(value) if value is not None else None+ except (TypeError, ValueError, OverflowError):+ return None+++def _header(response: Any, name: str) -> Optional[str]:+ headers = getattr(response, "headers", None)+ if headers is None:+ return None+ try:+ return headers.get(name)+ except (AttributeError, TypeError):+ return None+++def _response_text(response: Any) -> str:+ if response is None:+ return ""+ try:+ return str(getattr(response, "text", "") or "")[:600]+ except Exception:+ return ""+++def _provider_code(response: Any) -> Optional[str]:+ if response is None:+ return None+ try:+ payload = response.json()+ except Exception:+ return None+ if not isinstance(payload, Mapping):+ return None+ error = payload.get("error")+ if isinstance(error, Mapping):+ code = error.get("code") or error.get("type")+ return str(code) if code is not None else None+ code = payload.get("code")+ return str(code) if code is not None else None+++def _detail(exc: BaseException, body_text: str) -> str:+ base = str(exc)+ return f"{base} :: {body_text}" if body_text and body_text not in base else base+++# ---------------------------------------------------------------------------+# Provider protocols+# ---------------------------------------------------------------------------+#+# One per capability. A provider implements only what it serves and registers+# only into those registries, so an unsupported capability is unreachable+# rather than a method that raises.+++@runtime_checkable+class TranslateProvider(Protocol):+ provider_name: str++ async def translate(self, req: TranslateRequest) -> TranslateResult: ...++ async def languages(self, *, target: bool = True) -> List[LanguageOption]: ...+++@runtime_checkable+class TransliterateProvider(Protocol):+ provider_name: str++ async def transliterate(+ self, req: TransliterateRequest+ ) -> TransliterateResult: ...+++@runtime_checkable+class DetectProvider(Protocol):+ provider_name: str++ async def detect(self, req: DetectRequest) -> DetectResult: ...+++__all__ = [+ "DetectProvider",+ "DetectRequest",+ "DetectResult",+ "DetectedLanguage",+ "LanguageOption",+ "TranslateError",+ "TranslateErrorCategory",+ "TranslateProvider",+ "TranslateRequest",+ "TranslateResult",+ "TranslatedText",+ "TransliterateProvider",+ "TransliterateRequest",+ "TransliterateResult",+ "classify",+]diff --git a/server/nodes/translate/_providers/__init__.py b/server/nodes/translate/_providers/__init__.pynew file mode 100644index 00000000..9aef5214--- /dev/null+++ b/server/nodes/translate/_providers/__init__.py@@ -0,0 +1,30 @@+"""Translate provider plugins — importing this package registers them all.++Adding a provider:++1. Add a ``providers.<name>`` block to ``server/config/translate_defaults.json``+ with the capability sub-blocks it serves.+2. Write ``<name>.py`` implementing the matching Protocol(s) from+ :mod:`nodes.translate._protocol`, subclassing+ :class:`nodes.translate._providers._http.HttpTranslateProvider` for a plain+ HTTP API — or nothing at all, as ``llm.py`` shows.+3. Call the matching ``register_*_provider`` at the bottom of the module.+4. Add the side-effect import below.+5. Add its credential id to ``credential_providers.json`` plus a ``Credential``+ subclass, unless it reuses one another plugin already declares.++Register only into the capabilities the provider actually serves. That is the+whole point of three registries: a translate-only vendor is then unreachable+from the other two nodes rather than failing inside them.++**Never import a provider SDK at module level** — ``sdk_exception_refs`` are+lazy ``"module:ClassName"`` strings so registration stays free.+"""++from __future__ import annotations++# Side-effect imports. Order is documentation only; the module cache makes+# duplicate registration impossible.+from nodes.translate._providers import deepl # noqa: F401+from nodes.translate._providers import sarvam # noqa: F401+from nodes.translate._providers import llm # noqa: F401diff --git a/server/nodes/translate/_providers/_http.py b/server/nodes/translate/_providers/_http.pynew file mode 100644index 00000000..b2fe4818--- /dev/null+++ b/server/nodes/translate/_providers/_http.py@@ -0,0 +1,85 @@+"""Shared httpx plumbing for translate providers.++Mirrors ``nodes/speech/_providers/_http.py``. Auth diverges again — DeepL uses+a scheme keyword nobody else does (``DeepL-Auth-Key``), Sarvam uses a bare+custom header — so :meth:`auth_headers` is the single override point.+"""++from __future__ import annotations++from typing import Any, Dict, Optional++import httpx++from core.logging import get_logger++from .. import _config as translate_config++logger = get_logger(__name__)++DEFAULT_TIMEOUT_SECONDS = 60.0+++class HttpTranslateProvider:+ """Base for HTTP-backed text-language providers."""++ provider_name: str = ""++ def __init__(+ self,+ api_key: str,+ *,+ base_url: str = "",+ provider_name: Optional[str] = None,+ timeout: float = DEFAULT_TIMEOUT_SECONDS,+ ):+ self.provider_name = provider_name or type(self).provider_name+ self.api_key = api_key+ # Resolved from the key where a provider needs it to (DeepL's+ # free-vs-pro host split keys off the ':fx' suffix).+ self._base_url = (+ base_url or translate_config.base_url(self.provider_name, api_key=api_key)+ ).rstrip("/")+ self._timeout = timeout+ self._client: Optional[httpx.AsyncClient] = None++ def auth_headers(self) -> Dict[str, str]:+ return {"Authorization": f"Bearer {self.api_key}"}++ @property+ def client(self) -> httpx.AsyncClient:+ if self._client is None:+ self._client = httpx.AsyncClient(+ base_url=self._base_url,+ timeout=self._timeout,+ headers=self.auth_headers(),+ )+ return self._client++ async def request(+ self,+ method: str,+ path: str,+ *,+ json_body: Optional[Dict[str, Any]] = None,+ params: Optional[Dict[str, Any]] = None,+ headers: Optional[Dict[str, str]] = None,+ ) -> httpx.Response:+ response = await self.client.request(+ method, path, json=json_body, params=params, headers=headers+ )+ response.raise_for_status()+ return response++ async def aclose(self) -> None:+ if self._client is not None:+ await self._client.aclose()+ self._client = None+++def drop_none(payload: Dict[str, Any]) -> Dict[str, Any]:+ """Strip ``None`` values. Several of these APIs 422 on an explicit null."""+ return {k: v for k, v in payload.items() if v is not None}+++__all__ = ["DEFAULT_TIMEOUT_SECONDS", "HttpTranslateProvider", "drop_none"]diff --git a/server/nodes/translate/_providers/deepl.py b/server/nodes/translate/_providers/deepl.pynew file mode 100644index 00000000..e8ac9c67--- /dev/null+++ b/server/nodes/translate/_providers/deepl.py@@ -0,0 +1,129 @@+"""DeepL — translation only.++The single-capability provider in this set, and therefore the one that proves+the three-registry design: it registers into ``translate`` and is structurally+unreachable from the transliterate and detect nodes. With one registry plus+capability flags it would have appeared in all three dropdowns and failed at+runtime in two of them.++Two wire details worth knowing:++* Auth is ``Authorization: DeepL-Auth-Key <key>``. Not Bearer.+* ``text`` is an **array**, and the response is a parallel array — so the+ batch shape is native here rather than something to emulate.++DeepL also reports ``billed_characters`` per translation, which is better than+counting input ourselves: it reflects what the invoice will actually say.+"""++from __future__ import annotations++from typing import Dict, List++from core.logging import get_logger++from .. import _config as translate_config+from .._protocol import (+ LanguageOption,+ TranslateRequest,+ TranslateResult,+ TranslatedText,+)+from .._registry import TranslateProviderSpec, register_translate_provider+from ._http import HttpTranslateProvider, drop_none++logger = get_logger(__name__)+++class DeepLProvider(HttpTranslateProvider):+ provider_name = "deepl"++ def auth_headers(self) -> Dict[str, str]:+ return {"Authorization": f"DeepL-Auth-Key {self.api_key}"}++ async def translate(self, req: TranslateRequest) -> TranslateResult:+ body = {+ "text": req.text,+ "target_lang": (req.target_language or "EN-US").upper(),+ "source_lang": req.source_language.upper() or None,+ "context": req.context or None,+ "model_type": req.model or None,+ "formality": req.formality or None,+ # DeepL wants "0"/"1" here rather than a JSON boolean.+ "preserve_formatting": "1" if req.preserve_formatting else None,+ }+ body.update(req.provider_options)++ endpoint = (+ translate_config.endpoint(self.provider_name, "translate") or "/translate"+ )+ payload = (+ await self.request("POST", endpoint, json_body=drop_none(body))+ ).json()++ entries = payload.get("translations") or []+ return TranslateResult(+ translations=[+ TranslatedText(+ text=str(entry.get("text") or ""),+ detected_source_language=entry.get("detected_source_language"),+ )+ for entry in entries+ if isinstance(entry, dict)+ ],+ model=req.model or translate_config.default_model(self.provider_name, "translate"),+ # The provider's own figure beats counting input characters.+ billed_units=float(+ sum(int(e.get("billed_characters") or 0) for e in entries if isinstance(e, dict))+ )+ or None,+ billed_unit="characters",+ )++ async def languages(self, *, target: bool = True) -> List[LanguageOption]:+ """Live catalogue — DeepL adds languages without notice.++ Falls back to the configured list rather than leaving the dropdown+ empty when the call fails; a stale list still lets the user proceed.+ """+ endpoint = (+ translate_config.capability(+ self.provider_name, "translate", "languages_endpoint", default="/languages"+ )+ or "/languages"+ )+ try:+ payload = (+ await self.request(+ "GET", endpoint, params={"type": "target" if target else "source"}+ )+ ).json()+ options = [+ LanguageOption(+ code=str(entry.get("language") or ""),+ name=str(entry.get("name") or entry.get("language") or ""),+ )+ for entry in payload or []+ if isinstance(entry, dict) and entry.get("language")+ ]+ if options:+ return options+ except Exception as exc:+ logger.warning(+ "DeepL language listing failed; using the configured list",+ error=str(exc),+ )+ return [+ LanguageOption(code=code)+ for code in translate_config.languages(self.provider_name, "translate")+ ]+++register_translate_provider(+ TranslateProviderSpec(+ name="deepl",+ factory=DeepLProvider,+ sdk_exception_refs=("httpx:HTTPError",),+ client_kwargs={"provider_name": "deepl"},+ )+)diff --git a/server/nodes/translate/_providers/llm.py b/server/nodes/translate/_providers/llm.pynew file mode 100644index 00000000..413f7de2--- /dev/null+++ b/server/nodes/translate/_providers/llm.py@@ -0,0 +1,241 @@+"""LLM-backed translation, transliteration and language identification.++The provider that proves the Protocol does not care what a provider *does*+internally: there is no translation API behind this one, just a chat model+prompted through ``ChatUnifier``. It satisfies the same three Protocols as+Sarvam's REST client and is selected the same way.++Why it earns its place rather than being a curiosity: it covers every language+pair the dedicated vendors do not, needs no new credential (it reuses the chat+key the user already configured), and is the only option for a language DeepL+and Sarvam both lack.++**Billing is deliberately not recorded here.** This path bills tokens, not+characters, and token cost is already the LLM layer's concern. Reporting a+character count against ``api_pricing`` as well would double-count. The config+records the unit as ``tokens`` and the node skips attribution.+"""++from __future__ import annotations++import json+from typing import Any, Dict, List, Optional++from core.logging import get_logger++from .. import _config as translate_config+from .._protocol import (+ DetectRequest,+ DetectResult,+ DetectedLanguage,+ LanguageOption,+ TranslateError,+ TranslateErrorCategory,+ TranslateRequest,+ TranslateResult,+ TranslatedText,+ TransliterateRequest,+ TransliterateResult,+)+from .._registry import (+ DetectProviderSpec,+ TranslateProviderSpec,+ TransliterateProviderSpec,+ register_detect_provider,+ register_translate_provider,+ register_transliterate_provider,+)++logger = get_logger(__name__)++# Asking for JSON rather than bare text is what makes the output parseable+# without heuristics — a model told to "just translate" will sometimes add+# "Here is the translation:".+_JSON_RULE = (+ "Respond with a single JSON object and nothing else. No prose, no code "+ "fence."+)+++class LlmTextProvider:+ """Chat-model-backed text-language provider."""++ provider_name = "openai"++ def __init__(self, api_key: str, *, provider_name: Optional[str] = None):+ self.provider_name = provider_name or type(self).provider_name+ self.api_key = api_key++ # ------------------------------------------------------------------+ # capabilities+ # ------------------------------------------------------------------++ async def translate(self, req: TranslateRequest) -> TranslateResult:+ model = req.model or translate_config.default_model(self.provider_name, "translate")+ instructions = [+ f"Translate each input string into {req.target_language or 'English'}.",+ "Preserve meaning, tone and any markup or placeholders exactly.",+ ]+ if req.source_language:+ instructions.append(f"The source language is {req.source_language}.")+ if req.formality:+ instructions.append(f"Use a {req.formality} register.")+ if req.context:+ instructions.append(f"Context for disambiguation: {req.context}")+ instructions.append(+ 'Return {"translations": ["..."], "detected_source_language": "<code>"} '+ "with one entry per input, in order."+ )++ payload = await self._ask(model, instructions, req.text)+ texts = payload.get("translations") or []+ detected = payload.get("detected_source_language")+ return TranslateResult(+ translations=[+ TranslatedText(text=str(t), detected_source_language=detected)+ for t in texts+ ]+ or [TranslatedText(text="", detected_source_language=detected)],+ model=model,+ billed_unit="tokens",+ )++ async def transliterate(self, req: TransliterateRequest) -> TransliterateResult:+ model = req.model or translate_config.default_model(+ self.provider_name, "transliterate"+ )+ target = req.target_script or "the Latin alphabet"+ instructions = [+ f"Transliterate each input string into {target}.",+ "This is SCRIPT CONVERSION, not translation — the words and "+ "language must not change, only the writing system.",+ 'Return {"results": ["..."]} with one entry per input, in order.',+ ]+ payload = await self._ask(model, instructions, req.text)+ return TransliterateResult(+ results=[str(r) for r in payload.get("results") or []],+ model=model,+ billed_unit="tokens",+ )++ async def detect(self, req: DetectRequest) -> DetectResult:+ model = req.model or translate_config.default_model(self.provider_name, "detect")+ instructions = [+ "Identify the language of each input string.",+ 'Return {"detections": [{"language": "<BCP-47 code>", '+ '"script": "<ISO 15924 code>", "confidence": 0.0-1.0}]} with one '+ "entry per input, in order.",+ ]+ payload = await self._ask(model, instructions, req.text)+ detections = []+ for entry in payload.get("detections") or []:+ if not isinstance(entry, dict):+ continue+ detections.append(+ DetectedLanguage(+ language=str(entry.get("language") or ""),+ script=entry.get("script"),+ confidence=_as_float(entry.get("confidence")),+ )+ )+ return DetectResult(detections=detections, model=model, billed_unit="tokens")++ async def languages(self, *, target: bool = True) -> List[LanguageOption]:+ """No live catalogue — an LLM accepts essentially anything.++ The configured shortlist is a dropdown convenience; a user may type+ any code the model understands.+ """+ return [+ LanguageOption(code=code)+ for code in translate_config.languages(self.provider_name, "translate")+ ]++ # ------------------------------------------------------------------+ # internals+ # ------------------------------------------------------------------++ async def _ask(+ self, model: str, instructions: List[str], inputs: List[str]+ ) -> Dict[str, Any]:+ """One chat round-trip returning a parsed JSON object."""+ from core.container import container+ from services.llm.protocol import Message++ system = " ".join(instructions + [_JSON_RULE])+ user = json.dumps({"inputs": inputs}, ensure_ascii=False)++ response = await container.chat_unifier().chat(+ provider=self.provider_name,+ api_key=self.api_key,+ messages=[+ Message(role="system", content=system),+ Message(role="user", content=user),+ ],+ model=model,+ # Deterministic: this is a transformation, not a generation. A+ # creative temperature here produces different output for the+ # same input, which is wrong for translation.+ temperature=0.0,+ )+ return _parse_json_object(response.content, self.provider_name)+++def _parse_json_object(content: str, provider: str) -> Dict[str, Any]:+ """Parse the model's reply, tolerating a code fence.++ Models still occasionally wrap JSON in ```json despite being told not to,+ and failing the workflow over a fence would be needlessly brittle.+ """+ text = (content or "").strip()+ if text.startswith("```"):+ text = text.split("\n", 1)[-1]+ if text.endswith("```"):+ text = text[: -3]+ text = text.strip()+ try:+ parsed = json.loads(text)+ except ValueError as exc:+ raise TranslateError(+ message=f"model did not return JSON: {text[:200]}",+ provider=provider,+ category=TranslateErrorCategory.SERVER,+ ) from exc+ if not isinstance(parsed, dict):+ raise TranslateError(+ message=f"model returned {type(parsed).__name__}, expected an object",+ provider=provider,+ category=TranslateErrorCategory.SERVER,+ )+ return parsed+++def _as_float(value: Any) -> Optional[float]:+ try:+ return float(value) if value is not None else None+ except (TypeError, ValueError):+ return None+++# ``openai:OpenAIError`` is declared because the registry requires a typed+# ref, but it is rarely the path taken: ChatUnifier already translates SDK+# failures into NodeUserError, which propagates through the dispatcher+# untouched and keeps its better message.+_KW = {"provider_name": "openai"}+_REFS = ("openai:OpenAIError",)++register_translate_provider(+ TranslateProviderSpec(+ name="openai", factory=LlmTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)+register_transliterate_provider(+ TransliterateProviderSpec(+ name="openai", factory=LlmTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)+register_detect_provider(+ DetectProviderSpec(+ name="openai", factory=LlmTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)diff --git a/server/nodes/translate/_providers/sarvam.py b/server/nodes/translate/_providers/sarvam.pynew file mode 100644index 00000000..e4ccce36--- /dev/null+++ b/server/nodes/translate/_providers/sarvam.py@@ -0,0 +1,159 @@+"""Sarvam AI — all three capabilities over dedicated REST endpoints.++Ported from the retired ``sarvamTranslate`` / ``sarvamTransliterate`` /+``sarvamDetectLanguage`` nodes, preserving their wire behaviour. One provider+module serving three capabilities is the norm rather than the exception for+this kind of vendor, which is why the three registries take the same class.++Two carried-over details:++* Explicit nulls are rejected where an absent key is fine, so every body goes+ through ``drop_none``.+* Sarvam accepts one string per call, not an array. The neutral request is a+ list because DeepL's is; here only the first entry is sent, and the node+ keeps input single-valued.+"""++from __future__ import annotations++from typing import Any, Dict, List++from core.logging import get_logger++from .. import _config as translate_config+from .._protocol import (+ DetectRequest,+ DetectResult,+ DetectedLanguage,+ LanguageOption,+ TranslateRequest,+ TranslateResult,+ TranslatedText,+ TransliterateRequest,+ TransliterateResult,+)+from .._registry import (+ DetectProviderSpec,+ TranslateProviderSpec,+ TransliterateProviderSpec,+ register_detect_provider,+ register_translate_provider,+ register_transliterate_provider,+)+from ._http import HttpTranslateProvider, drop_none++logger = get_logger(__name__)+++def _first(text: List[str]) -> str:+ return text[0] if text else ""+++class SarvamTextProvider(HttpTranslateProvider):+ provider_name = "sarvam"++ def auth_headers(self) -> Dict[str, str]:+ # Sarvam's non-chat endpoints accept only the native header; Bearer+ # works on its OpenAI-compatible chat route, which is a different+ # surface.+ return {"api-subscription-key": self.api_key}++ async def translate(self, req: TranslateRequest) -> TranslateResult:+ model = req.model or translate_config.default_model(self.provider_name, "translate")+ body: Dict[str, Any] = {+ "input": _first(req.text),+ "source_language_code": req.source_language or "auto",+ "target_language_code": req.target_language+ or translate_config.default_target_language(self.provider_name, "translate"),+ "model": model,+ "mode": req.formality or "formal",+ }+ body.update(req.provider_options)++ endpoint = translate_config.endpoint(self.provider_name, "translate") or "/translate"+ payload = (await self.request("POST", endpoint, json_body=drop_none(body))).json()++ return TranslateResult(+ translations=[+ TranslatedText(+ text=str(payload.get("translated_text") or ""),+ detected_source_language=payload.get("source_language_code"),+ )+ ],+ model=model,+ request_id=payload.get("request_id"),+ billed_units=float(len(_first(req.text))),+ billed_unit="characters",+ )++ async def transliterate(self, req: TransliterateRequest) -> TransliterateResult:+ model = req.model or translate_config.default_model(+ self.provider_name, "transliterate"+ )+ body: Dict[str, Any] = {+ "input": _first(req.text),+ "source_language_code": req.source_language or "auto",+ "target_language_code": req.target_language+ or translate_config.default_target_language(self.provider_name, "transliterate"),+ "model": model,+ "spoken_form": req.target_script == "spoken-form-in-native" or None,+ }+ body.update(req.provider_options)++ endpoint = (+ translate_config.endpoint(self.provider_name, "transliterate")+ or "/transliterate"+ )+ payload = (await self.request("POST", endpoint, json_body=drop_none(body))).json()++ return TransliterateResult(+ results=[str(payload.get("transliterated_text") or "")],+ model=model,+ request_id=payload.get("request_id"),+ billed_units=float(len(_first(req.text))),+ billed_unit="characters",+ )++ async def detect(self, req: DetectRequest) -> DetectResult:+ endpoint = translate_config.endpoint(self.provider_name, "detect") or "/text-lid"+ payload = (+ await self.request("POST", endpoint, json_body={"input": _first(req.text)})+ ).json()++ return DetectResult(+ detections=[+ DetectedLanguage(+ language=str(payload.get("language_code") or ""),+ script=payload.get("script_code"),+ )+ ],+ request_id=payload.get("request_id"),+ billed_units=float(len(_first(req.text))),+ billed_unit="characters",+ )++ async def languages(self, *, target: bool = True) -> List[LanguageOption]:+ return [+ LanguageOption(code=code)+ for code in translate_config.languages(self.provider_name, "translate")+ ]+++_KW = {"provider_name": "sarvam"}+_REFS = ("httpx:HTTPError",)++register_translate_provider(+ TranslateProviderSpec(+ name="sarvam", factory=SarvamTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)+register_transliterate_provider(+ TransliterateProviderSpec(+ name="sarvam", factory=SarvamTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)+register_detect_provider(+ DetectProviderSpec(+ name="sarvam", factory=SarvamTextProvider, sdk_exception_refs=_REFS, client_kwargs=_KW+ )+)diff --git a/server/nodes/translate/_registry.py b/server/nodes/translate/_registry.pynew file mode 100644index 00000000..ced36256--- /dev/null+++ b/server/nodes/translate/_registry.py@@ -0,0 +1,102 @@+"""Three provider registries — one per capability.++Over the shared mechanism in :mod:`services.provider_registry`, same as+``nodes/speech/_registry.py``. Speech owns two (one per direction); this owns+three, because translation, transliteration and language identification are+separately-supported capabilities rather than facets of one.++Membership is the capability. Each node's provider enum is literally the+matching ``*_providers()`` list, so a translate-only vendor never appears in+the transliteration dropdown and nothing has to remember to keep a+``supports_transliterate`` flag truthful.+"""++from __future__ import annotations++from typing import List++from services.provider_registry import (+ ProviderRegistry,+ ProviderSpec as _ProviderSpec,+)++from ._protocol import DetectProvider, TranslateProvider, TransliterateProvider++TranslateProviderSpec = _ProviderSpec[TranslateProvider]+TransliterateProviderSpec = _ProviderSpec[TransliterateProvider]+DetectProviderSpec = _ProviderSpec[DetectProvider]++_TRANSLATE: ProviderRegistry[TranslateProvider] = ProviderRegistry("translate")+_TRANSLITERATE: ProviderRegistry[TransliterateProvider] = ProviderRegistry(+ "transliterate"+)+_DETECT: ProviderRegistry[DetectProvider] = ProviderRegistry("detect-language")+++def register_translate_provider(spec: TranslateProviderSpec) -> None:+ _TRANSLATE.register(spec)+++def register_transliterate_provider(spec: TransliterateProviderSpec) -> None:+ _TRANSLITERATE.register(spec)+++def register_detect_provider(spec: DetectProviderSpec) -> None:+ _DETECT.register(spec)+++def get_translate_provider(name: str) -> TranslateProviderSpec:+ return _TRANSLATE.get(name)+++def get_transliterate_provider(name: str) -> TransliterateProviderSpec:+ return _TRANSLITERATE.get(name)+++def get_detect_provider(name: str) -> DetectProviderSpec:+ return _DETECT.get(name)+++def translate_providers() -> List[str]:+ return _TRANSLATE.all()+++def transliterate_providers() -> List[str]:+ return _TRANSLITERATE.all()+++def detect_providers() -> List[str]:+ return _DETECT.all()+++# Capability name -> (registry accessor, spec getter). Lets the shared node+# helpers and the option loaders stay capability-agnostic instead of+# branching three ways.+CAPABILITIES = {+ "translate": (translate_providers, get_translate_provider),+ "transliterate": (transliterate_providers, get_transliterate_provider),+ "detect": (detect_providers, get_detect_provider),+}+++def _reset_for_tests() -> None:+ _TRANSLATE.reset_for_tests()+ _TRANSLITERATE.reset_for_tests()+ _DETECT.reset_for_tests()+++__all__ = [+ "CAPABILITIES",+ "DetectProviderSpec",+ "TranslateProviderSpec",+ "TransliterateProviderSpec",+ "detect_providers",+ "get_detect_provider",+ "get_translate_provider",+ "get_transliterate_provider",+ "register_detect_provider",+ "register_translate_provider",+ "register_transliterate_provider",+ "translate_providers",+ "transliterate_providers",+]diff --git a/server/nodes/translate/_unifier.py b/server/nodes/translate/_unifier.pynew file mode 100644index 00000000..a7ae0da2--- /dev/null+++ b/server/nodes/translate/_unifier.py@@ -0,0 +1,176 @@+"""Dispatch and error translation for the three text-language nodes.++Same shape as ``nodes/speech/_unifier.py``: look the spec up in the matching+registry, build a client per call, run it, normalize every failure into+``NodeUserError`` at one catch site, close in ``finally``. No client cache,+for the same reason — one call per node execution.+"""++from __future__ import annotations++from typing import Awaitable, Callable, List, Optional, TypeVar++from core.logging import get_logger+from services.plugin import NodeUserError+from services.provider_registry import ProviderSpec++from ._protocol import (+ DetectRequest,+ DetectResult,+ LanguageOption,+ TranslateError,+ TranslateErrorCategory,+ TranslateRequest,+ TranslateResult,+ TransliterateRequest,+ TransliterateResult,+)+from ._registry import (+ get_detect_provider,+ get_translate_provider,+ get_transliterate_provider,+)++logger = get_logger(__name__)++T = TypeVar("T")+++async def translate(+ *, provider: str, api_key: str, request: TranslateRequest, translate_errors: bool = True+) -> TranslateResult:+ return await _dispatch(+ get_translate_provider(provider),+ provider,+ api_key,+ lambda client: client.translate(request),+ operation="translation",+ translate_errors=translate_errors,+ )+++async def transliterate(+ *,+ provider: str,+ api_key: str,+ request: TransliterateRequest,+ translate_errors: bool = True,+) -> TransliterateResult:+ return await _dispatch(+ get_transliterate_provider(provider),+ provider,+ api_key,+ lambda client: client.transliterate(request),+ operation="transliteration",+ translate_errors=translate_errors,+ )+++async def detect(+ *, provider: str, api_key: str, request: DetectRequest, translate_errors: bool = True+) -> DetectResult:+ return await _dispatch(+ get_detect_provider(provider),+ provider,+ api_key,+ lambda client: client.detect(request),+ operation="language detection",+ translate_errors=translate_errors,+ )+++async def languages(+ *, provider: str, api_key: str, target: bool = True+) -> List[LanguageOption]:+ """Selectable languages for a translation provider.++ Providers without a live endpoint return their configured list, so the+ dropdown loader never branches on whether one exists.+ """+ return await _dispatch(+ get_translate_provider(provider),+ provider,+ api_key,+ lambda client: client.languages(target=target),+ operation="language listing",+ translate_errors=True,+ )+++async def _dispatch(+ spec: ProviderSpec,+ provider: str,+ api_key: str,+ call: Callable[[object], Awaitable[T]],+ *,+ operation: str,+ translate_errors: bool,+) -> T:+ client: Optional[object] = None+ try:+ client = spec.factory(api_key=api_key, **spec.client_kwargs)+ return await call(client)+ except spec.sdk_exception_types as exc:+ error = TranslateError.from_exception(provider, exc)+ if not translate_errors:+ raise error from exc+ logger.warning(+ "translate provider request failed",+ provider=error.provider,+ operation=operation,+ category=error.category.value,+ retryable=error.retryable,+ status_code=error.status_code,+ provider_code=error.provider_code,+ )+ raise NodeUserError(error.user_message) from error+ except TranslateError as exc:+ # A provider classified a failure itself — typically a documented+ # vendor error that arrived with HTTP 200.+ if not translate_errors:+ raise+ logger.warning(+ "translate provider returned a failure payload",+ provider=exc.provider,+ operation=operation,+ category=exc.category.value,+ )+ raise NodeUserError(exc.user_message) from exc+ except (ValueError, TypeError, OSError) as exc:+ # Only normalize failures raised while CONSTRUCTING the client; once+ # one exists an unexpected exception is a programming error and keeps+ # its traceback.+ if client is not None:+ raise+ category = (+ TranslateErrorCategory.CONNECTION+ if isinstance(exc, OSError)+ else TranslateErrorCategory.INVALID_REQUEST+ )+ error = TranslateError(+ message=str(exc),+ provider=provider,+ category=category,+ retryable=category == TranslateErrorCategory.CONNECTION,+ )+ if not translate_errors:+ raise error from exc+ raise NodeUserError(error.user_message) from error+ finally:+ if client is not None:+ await _close(client)+++async def _close(client: object) -> None:+ close = getattr(client, "aclose", None)+ if close is None:+ return+ try:+ result = close()+ if hasattr(result, "__await__"):+ await result+ except Exception as exc:+ logger.warning("failed to close translate client", error=str(exc))+++__all__ = ["detect", "languages", "translate", "transliterate"]diff --git a/server/nodes/translate/detect_language.py b/server/nodes/translate/detect_language.pynew file mode 100644index 00000000..22dcf3fc--- /dev/null+++ b/server/nodes/translate/detect_language.py@@ -0,0 +1,150 @@+"""Detect Language — identify what a piece of text is written in.++Replaces the vendor-locked ``sarvamDetectLanguage``. Returns the language and,+where the provider reports it, the script and a confidence.+"""++from __future__ import annotations++from typing import Any, Dict, List, Optional++from pydantic import BaseModel, Field, model_validator++from services.plugin import (+ ActionNode,+ NodeContext,+ Operation,+ TaskQueue,+ coerce_blank_params,+)++from ..model._credentials import OpenAICredential, SarvamCredential+from . import _config as translate_config+from . import _unifier+from ._base import check_length, provider_api_key, require_provider, track_usage+from ._protocol import DetectRequest+from ._registry import detect_providers++from . import _providers # noqa: F401 isort:skip++_PROVIDERS: List[str] = detect_providers()+++class DetectLanguageParams(BaseModel):+ tool_name: str = Field(+ default="detect_language",+ description="Override name shown to the LLM when used as a tool.",+ )+ tool_description: str = Field(+ default=(+ "Identify which language a piece of text is written in, and in "+ "which script. Use before translating when the source language is "+ "unknown, or to route text by language."+ ),+ description="Override description shown to the LLM when used as a tool.",+ json_schema_extra={"rows": 3},+ )++ provider: str = Field(+ default=_PROVIDERS[0] if _PROVIDERS else "sarvam",+ description="Language-identification provider.",+ json_schema_extra={"enum": _PROVIDERS},+ )+ text: str = Field(+ ...,+ min_length=1,+ description="Text whose language should be identified.",+ json_schema_extra={"rows": 4},+ )+ detect_model: str = Field(+ default="",+ description="Provider model. Blank uses the provider default.",+ json_schema_extra={"loadOptionsMethod": "detectModels"},+ )+ provider_options: Dict[str, Any] = Field(+ default_factory=dict,+ description="Vendor-specific options as a JSON object, passed through untouched.",+ json_schema_extra={"editor": "json", "rows": 3},+ )++ model_config = {"extra": "ignore"}++ @model_validator(mode="before")+ @classmethod+ def _coerce_panel_blanks(cls, values: Any) -> Any:+ return coerce_blank_params(cls, values, object_fields=("provider_options",))+++class DetectLanguageOutput(BaseModel):+ language: str = ""+ script: Optional[str] = None+ confidence: Optional[float] = None+ provider: str = ""+ request_id: Optional[str] = None++ model_config = {"extra": "allow"}+++class DetectLanguageNode(ActionNode):+ type = "detectLanguage"+ display_name = "Detect Language"+ subtitle = "Language ID"+ group = ("language", "tool")+ description = "Identify the language and script of a piece of text"+ component_kind = "square"+ tool_name = "detect_language"+ tool_description = (+ "Identify which language a piece of text is written in, and in which "+ "script."+ )+ handles = (+ {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},+ {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},+ )+ credentials = (SarvamCredential, OpenAICredential)+ annotations = {"destructive": False, "readonly": True, "open_world": True}+ task_queue = TaskQueue.REST_API+ usable_as_tool = True+ hide_input_handle = False+ hide_output_handle = False++ Params = DetectLanguageParams+ Output = DetectLanguageOutput++ @Operation("detect")+ async def detect(+ self, ctx: NodeContext, params: DetectLanguageParams+ ) -> DetectLanguageOutput:+ provider = require_provider(params.provider, _PROVIDERS, "language detection")+ model = params.detect_model or translate_config.default_model(+ provider, translate_config.DETECT+ )+ check_length(provider, translate_config.DETECT, model, params.text)++ api_key = await provider_api_key(ctx, provider)+ result = await _unifier.detect(+ provider=provider,+ api_key=api_key,+ request=DetectRequest(+ text=[params.text],+ model=model,+ provider_options=dict(params.provider_options),+ ),+ )++ await track_usage(+ ctx,+ provider=provider,+ operation="detect_language",+ units=result.billed_units or float(len(params.text)),+ unit=result.billed_unit,+ )++ first = result.detections[0] if result.detections else None+ return DetectLanguageOutput(+ language=first.language if first else "",+ script=first.script if first else None,+ confidence=first.confidence if first else None,+ provider=provider,+ request_id=result.request_id,+ )diff --git a/server/nodes/translate/icon_detectLanguage.svg b/server/nodes/translate/icon_detectLanguage.svgnew file mode 100644index 00000000..565b966a--- /dev/null+++ b/server/nodes/translate/icon_detectLanguage.svg@@ -0,0 +1,6 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <circle cx="11" cy="11" r="8"/>+ <path d="M3 11h16"/>+ <path d="M11 3a13 13 0 0 1 0 16a13 13 0 0 1 0-16z"/>+ <path d="m21 21-3-3"/>+</svg>diff --git a/server/nodes/translate/icon_translateText.svg b/server/nodes/translate/icon_translateText.svgnew file mode 100644index 00000000..dc68904d--- /dev/null+++ b/server/nodes/translate/icon_translateText.svg@@ -0,0 +1,8 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="m5 8 6 6"/>+ <path d="m4 14 6-6 2-3"/>+ <path d="M2 5h12"/>+ <path d="M7 2h1"/>+ <path d="m22 22-5-10-5 10"/>+ <path d="M14 18h6"/>+</svg>diff --git a/server/nodes/translate/icon_transliterateText.svg b/server/nodes/translate/icon_transliterateText.svgnew file mode 100644index 00000000..06efed3f--- /dev/null+++ b/server/nodes/translate/icon_transliterateText.svg@@ -0,0 +1,8 @@+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">+ <path d="M4 7V5h7v2"/>+ <path d="M7.5 5v9"/>+ <path d="M6 14h3"/>+ <path d="m14 17 3 3 3-3"/>+ <path d="M17 20V10"/>+ <path d="M13 7h7"/>+</svg>diff --git a/server/nodes/translate/meta.json b/server/nodes/translate/meta.jsonnew file mode 100644index 00000000..c40ade0f--- /dev/null+++ b/server/nodes/translate/meta.json@@ -0,0 +1,4 @@+{+ "_comment": "Matches the 'language' palette group accent. The plugin is vendor-neutral, so it takes the group colour rather than any one provider's brand.",+ "color": "#6A88E2"+}diff --git a/server/nodes/translate/translate_text.py b/server/nodes/translate/translate_text.pynew file mode 100644index 00000000..b891e065--- /dev/null+++ b/server/nodes/translate/translate_text.py@@ -0,0 +1,197 @@+"""Translate — one node, many providers.++Replaces the vendor-locked ``sarvamTranslate``. The provider is a parameter,+so switching from Sarvam to DeepL never means swapping the node out of a+workflow.++The field is ``translate_model``, not ``model``: a parameter-panel effect+keyed on the literal names ``model`` and ``api_key`` overwrites them with+chat-model data whenever a sibling ``provider`` field exists, and it does not+check that the provider is an LLM provider.+"""++from __future__ import annotations++from typing import Any, Dict, List, Optional++from pydantic import BaseModel, Field, model_validator++from services.plugin import (+ ActionNode,+ NodeContext,+ Operation,+ TaskQueue,+ coerce_blank_params,+)++from ..model._credentials import OpenAICredential, SarvamCredential+from . import _config as translate_config+from . import _unifier+from ._base import check_length, provider_api_key, require_provider, track_usage+from ._credentials import DeepLCredential+from ._protocol import TranslateRequest+from ._registry import translate_providers++from . import _providers # noqa: F401 isort:skip++_PROVIDERS: List[str] = translate_providers()+++class TranslateTextParams(BaseModel):+ tool_name: str = Field(+ default="translate_text",+ description="Override name shown to the LLM when used as a tool.",+ )+ tool_description: str = Field(+ default=(+ "Translate text into another language. Use when the user asks for "+ "a translation, or when text must be understood in a different "+ "language before further processing."+ ),+ description="Override description shown to the LLM when used as a tool.",+ json_schema_extra={"rows": 3},+ )++ provider: str = Field(+ default=_PROVIDERS[0] if _PROVIDERS else "openai",+ description="Translation provider.",+ json_schema_extra={"enum": _PROVIDERS},+ )+ text: str = Field(+ ...,+ min_length=1,+ description="Text to translate.",+ json_schema_extra={"rows": 4},+ )+ target_language: str = Field(+ default="",+ description="Language to translate into. Blank uses the provider default.",+ json_schema_extra={"loadOptionsMethod": "translateLanguages"},+ )+ source_language: str = Field(+ default="",+ description="Source language. Blank auto-detects.",+ json_schema_extra={"loadOptionsMethod": "translateSourceLanguages"},+ )+ # NOT `model` — see the module docstring.+ translate_model: str = Field(+ default="",+ description="Provider model. Blank uses the provider default.",+ json_schema_extra={"loadOptionsMethod": "translateModels"},+ )+ formality: str = Field(+ default="",+ description="Register or formality, where the provider supports it.",+ json_schema_extra={"loadOptionsMethod": "translateFormality"},+ )+ context: str = Field(+ default="",+ description=(+ "Extra context to disambiguate the text. Not translated itself."+ ),+ json_schema_extra={"rows": 2},+ )+ preserve_formatting: bool = Field(+ default=False,+ description="Keep original line breaks and punctuation as-is.",+ )+ provider_options: Dict[str, Any] = Field(+ default_factory=dict,+ description=(+ "Vendor-specific options as a JSON object, passed through "+ 'untouched — e.g. {"glossary_id": "..."} for DeepL, '+ '{"numerals_format": "native"} for Sarvam.'+ ),+ json_schema_extra={"editor": "json", "rows": 4},+ )++ model_config = {"extra": "ignore"}++ @model_validator(mode="before")+ @classmethod+ def _coerce_panel_blanks(cls, values: Any) -> Any:+ return coerce_blank_params(cls, values, object_fields=("provider_options",))+++class TranslateTextOutput(BaseModel):+ translated_text: str = ""+ detected_source_language: Optional[str] = None+ provider: str = ""+ translate_model: str = ""+ request_id: Optional[str] = None++ model_config = {"extra": "allow"}+++class TranslateTextNode(ActionNode):+ type = "translateText"+ display_name = "Translate"+ subtitle = "Translate Text"+ group = ("language", "tool")+ description = "Translate text using any configured translation provider"+ component_kind = "square"+ tool_name = "translate_text"+ tool_description = (+ "Translate text into another language. Pick the provider that best "+ "covers the language pair."+ )+ handles = (+ {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},+ {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},+ )+ credentials = (DeepLCredential, SarvamCredential, OpenAICredential)+ annotations = {"destructive": False, "readonly": True, "open_world": True}+ task_queue = TaskQueue.REST_API+ usable_as_tool = True+ # `usable_as_tool` otherwise auto-hides both handles.+ hide_input_handle = False+ hide_output_handle = False++ Params = TranslateTextParams+ Output = TranslateTextOutput++ @Operation("translate")+ async def translate(+ self, ctx: NodeContext, params: TranslateTextParams+ ) -> TranslateTextOutput:+ provider = require_provider(params.provider, _PROVIDERS, "translation")+ model = params.translate_model or translate_config.default_model(+ provider, translate_config.TRANSLATE+ )+ check_length(provider, translate_config.TRANSLATE, model, params.text)++ api_key = await provider_api_key(ctx, provider)+ result = await _unifier.translate(+ provider=provider,+ api_key=api_key,+ request=TranslateRequest(+ text=[params.text],+ target_language=params.target_language+ or translate_config.default_target_language(+ provider, translate_config.TRANSLATE+ ),+ source_language=params.source_language,+ model=model,+ formality=params.formality,+ context=params.context,+ preserve_formatting=params.preserve_formatting,+ provider_options=dict(params.provider_options),+ ),+ )++ await track_usage(+ ctx,+ provider=provider,+ operation="translate",+ units=result.billed_units or float(len(params.text)),+ unit=result.billed_unit,+ )++ first = result.translations[0] if result.translations else None+ return TranslateTextOutput(+ translated_text=first.text if first else "",+ detected_source_language=first.detected_source_language if first else None,+ provider=provider,+ translate_model=result.model or model,+ request_id=result.request_id,+ )diff --git a/server/nodes/translate/transliterate_text.py b/server/nodes/translate/transliterate_text.pynew file mode 100644index 00000000..fa6bcb67--- /dev/null+++ b/server/nodes/translate/transliterate_text.py@@ -0,0 +1,176 @@+"""Transliterate — script conversion, not translation.++Replaces the vendor-locked ``sarvamTransliterate``. The distinction the node+exists to preserve: transliteration changes the *writing system* while the+words and language stay the same ("namaste" -> "नमस्ते"), where translation+changes the words ("hello" -> "नमस्ते"). Confusing the two is the single most+common misuse, so the tool description says so explicitly for the LLM's+benefit.++DeepL is absent from the provider list here, and unreachable rather than+merely undocumented: it registers only into the translate registry.+"""++from __future__ import annotations++from typing import Any, Dict, List, Optional++from pydantic import BaseModel, Field, model_validator++from services.plugin import (+ ActionNode,+ NodeContext,+ Operation,+ TaskQueue,+ coerce_blank_params,+)++from ..model._credentials import OpenAICredential, SarvamCredential+from . import _config as translate_config+from . import _unifier+from ._base import check_length, provider_api_key, require_provider, track_usage+from ._protocol import TransliterateRequest+from ._registry import transliterate_providers++from . import _providers # noqa: F401 isort:skip++_PROVIDERS: List[str] = transliterate_providers()+++class TransliterateTextParams(BaseModel):+ tool_name: str = Field(+ default="transliterate_text",+ description="Override name shown to the LLM when used as a tool.",+ )+ tool_description: str = Field(+ default=(+ "Convert text from one script to another WITHOUT translating it — "+ "the words and language stay the same, only the writing system "+ "changes (e.g. 'namaste' to 'नमस्ते'). Use translate_text instead "+ "when the meaning should change language."+ ),+ description="Override description shown to the LLM when used as a tool.",+ json_schema_extra={"rows": 3},+ )++ provider: str = Field(+ default=_PROVIDERS[0] if _PROVIDERS else "sarvam",+ description="Transliteration provider.",+ json_schema_extra={"enum": _PROVIDERS},+ )+ text: str = Field(+ ...,+ min_length=1,+ description="Text to convert.",+ json_schema_extra={"rows": 4},+ )+ target_language: str = Field(+ default="",+ description="Language whose script to write in.",+ json_schema_extra={"loadOptionsMethod": "transliterateLanguages"},+ )+ source_language: str = Field(+ default="",+ description="Source language. Blank auto-detects.",+ json_schema_extra={"loadOptionsMethod": "transliterateSourceLanguages"},+ )+ target_script: str = Field(+ default="",+ description="Output script style, where the provider offers a choice.",+ json_schema_extra={"loadOptionsMethod": "transliterateScripts"},+ )+ transliterate_model: str = Field(+ default="",+ description="Provider model. Blank uses the provider default.",+ json_schema_extra={"loadOptionsMethod": "transliterateModels"},+ )+ provider_options: Dict[str, Any] = Field(+ default_factory=dict,+ description=(+ "Vendor-specific options as a JSON object, passed through "+ 'untouched — e.g. {"numerals_format": "native"} for Sarvam.'+ ),+ json_schema_extra={"editor": "json", "rows": 4},+ )++ model_config = {"extra": "ignore"}++ @model_validator(mode="before")+ @classmethod+ def _coerce_panel_blanks(cls, values: Any) -> Any:+ return coerce_blank_params(cls, values, object_fields=("provider_options",))+++class TransliterateTextOutput(BaseModel):+ transliterated_text: str = ""+ provider: str = ""+ transliterate_model: str = ""+ request_id: Optional[str] = None++ model_config = {"extra": "allow"}+++class TransliterateTextNode(ActionNode):+ type = "transliterateText"+ display_name = "Transliterate"+ subtitle = "Convert Script"+ group = ("language", "tool")+ description = "Convert text between writing systems without translating it"+ component_kind = "square"+ tool_name = "transliterate_text"+ tool_description = (+ "Convert text from one script to another WITHOUT translating it. The "+ "words stay the same; only the writing system changes."+ )+ handles = (+ {"name": "input-main", "kind": "input", "position": "left", "label": "Input", "role": "main"},+ {"name": "output-main", "kind": "output", "position": "right", "label": "Output", "role": "main"},+ )+ credentials = (SarvamCredential, OpenAICredential)+ annotations = {"destructive": False, "readonly": True, "open_world": True}+ task_queue = TaskQueue.REST_API+ usable_as_tool = True+ hide_input_handle = False+ hide_output_handle = False++ Params = TransliterateTextParams+ Output = TransliterateTextOutput++ @Operation("transliterate")+ async def transliterate(+ self, ctx: NodeContext, params: TransliterateTextParams+ ) -> TransliterateTextOutput:+ provider = require_provider(params.provider, _PROVIDERS, "transliteration")+ model = params.transliterate_model or translate_config.default_model(+ provider, translate_config.TRANSLITERATE+ )+ check_length(provider, translate_config.TRANSLITERATE, model, params.text)++ api_key = await provider_api_key(ctx, provider)+ result = await _unifier.transliterate(+ provider=provider,+ api_key=api_key,+ request=TransliterateRequest(+ text=[params.text],+ target_script=params.target_script,+ source_language=params.source_language,+ target_language=params.target_language,+ model=model,+ provider_options=dict(params.provider_options),+ ),+ )++ await track_usage(+ ctx,+ provider=provider,+ operation="transliterate",+ units=result.billed_units or float(len(params.text)),+ unit=result.billed_unit,+ )++ return TransliterateTextOutput(+ transliterated_text=result.text,+ provider=provider,+ transliterate_model=result.model or model,+ request_id=result.request_id,+ )diff --git a/server/services/plugin/__init__.py b/server/services/plugin/__init__.pyindex f4a4f93e..ab8ab72c 100644--- a/server/services/plugin/__init__.py+++ b/server/services/plugin/__init__.py@@ -48,8 +48,11 @@ from services.plugin.scaling import (DEFAULT_HEARTBEAT,)from services.plugin.interceptor import Interceptor, InterceptorChain+from services.plugin.params import coerce_blank_params, coerce_json_object__all__ = [+ "coerce_blank_params",+ "coerce_json_object","BaseNode","NodeUserError","ActionNode",diff --git a/server/services/plugin/capabilities.py b/server/services/plugin/capabilities.pynew file mode 100644index 00000000..aecda977--- /dev/null+++ b/server/services/plugin/capabilities.py@@ -0,0 +1,224 @@+"""JSON-backed per-provider capability lookups.++A plugin that fronts several interchangeable vendors needs the same thing+every time: a JSON file under ``server/config/`` describing what each provider+supports, resolved per capability and often per model, so that no shared code+ever branches on a provider name.++``nodes/speech`` established the shape; ``nodes/translate`` needed it+identically. Rather than keep two copies of the resolution ladder in step by+hand, it lives here — the same argument that put ``ProviderSpec`` in+``services/provider_registry``.++The idea worth understanding is :meth:`CapabilityConfig.capability`. A value in+the JSON is either a plain scalar (applies to every model) or a mapping of+model id to value with a ``_default`` fallback. Callers never care which was+written — they ask for ``(provider, section, key, model)`` and get a value.+That is what lets OpenAI declare "``response_formats`` is ``json|text`` except+on whisper-1 where it is five formats" without a line of Python.++Lookup order inside a mapping is exact match, then **longest** prefix match,+then ``_default``. Prefix matching earns its keep on dated snapshots: a key of+``gpt-4o-mini-transcribe`` also answers for+``gpt-4o-mini-transcribe-2025-12-15`` without the JSON tracking every release.+"""++from __future__ import annotations++import json+from pathlib import Path+from typing import Any, Dict, List, Optional++from core.logging import get_logger++logger = get_logger(__name__)++_CONFIG_DIR = Path(__file__).parent.parent.parent / "config"+++class CapabilityConfig:+ """One provider-capability JSON file.++ ``sections`` are the per-provider sub-blocks — directions for speech+ (``tts`` / ``stt``), capabilities for translate (``translate`` /+ ``transliterate`` / ``detect``). The class does not care what they mean.+ """++ def __init__(self, filename: str):+ self._path = _CONFIG_DIR / filename+ self._data: Dict[str, Any] = {}+ self.reload()++ # ------------------------------------------------------------------+ # loading+ # ------------------------------------------------------------------++ def reload(self) -> None:+ """(Re)read the file. Mutates in place.++ In place rather than rebinding, because callers may hold a reference+ to the underlying dict and rebinding would leave them on the stale+ object — a trap ``services/llm/config.reload_defaults`` still has.+ """+ self._data.clear()+ self._data.update(self._load())++ def _load(self) -> Dict[str, Any]:+ try:+ with open(self._path, encoding="utf-8") as handle:+ return json.load(handle)+ except Exception as exc:+ # Soft-fail: a malformed config degrades every lookup to its+ # permissive default rather than preventing the process booting.+ logger.warning(+ "could not load capability config",+ path=str(self._path),+ error=str(exc),+ )+ return {"providers": {}}++ @property+ def data(self) -> Dict[str, Any]:+ """The raw parsed document, for top-level keys outside `providers`."""+ return self._data++ # ------------------------------------------------------------------+ # blocks+ # ------------------------------------------------------------------++ def provider_block(self, provider: str) -> Dict[str, Any]:+ block = self._data.get("providers", {}).get(provider)+ return block if isinstance(block, dict) else {}++ def section(self, provider: str, section: str) -> Dict[str, Any]:+ block = self.provider_block(provider).get(section)+ return block if isinstance(block, dict) else {}++ def supports_section(self, provider: str, section: str) -> bool:+ """Whether the JSON declares this provider for this capability.++ **Advisory only** — registry membership is the authority. A provider+ that never calls its ``register_*`` cannot be selected regardless of+ what the JSON says. Useful for config-consistency tests.+ """+ return bool(self.section(provider, section))++ def base_url(self, provider: str) -> str:+ return str(self.provider_block(provider).get("base_url") or "")++ def credential_id(self, provider: str) -> str:+ """Credential id **string**, never a ``Credential`` class.++ The classes live under ``nodes/``; importing one into a config layer+ would invert the layering. Falls back to the provider name, which is+ the convention most providers follow.+ """+ return str(self.provider_block(provider).get("credential_id") or provider)++ # ------------------------------------------------------------------+ # capability resolution+ # ------------------------------------------------------------------++ def capability(+ self,+ provider: str,+ section: str,+ key: str,+ *,+ model: Optional[str] = None,+ default: Any = None,+ ) -> Any:+ """Resolve one capability, honouring per-model overrides.++ Returns ``default`` only when the key is absent entirely. A declared+ ``null`` comes back as ``None`` and is meaningful — "we checked and+ there is no limit" is different from "not configured".+ """+ block = self.section(provider, section)+ if key not in block:+ return default++ value = block[key]+ if not isinstance(value, dict):+ return value++ if model:+ if model in value:+ return value[model]+ prefixes = [k for k in value if k != "_default" and model.startswith(k)]+ if prefixes:+ return value[max(prefixes, key=len)]+ if "_default" in value:+ return value["_default"]+ return default++ def supports(+ self,+ provider: str,+ section: str,+ key: str,+ *,+ model: Optional[str] = None,+ ) -> bool:+ """Boolean probe, defaulting **permissive**.++ A provider opts *out* explicitly; forgetting to declare a flag never+ silently disables a working feature.+ """+ return bool(self.capability(provider, section, key, model=model, default=True))++ def listing(+ self,+ provider: str,+ section: str,+ key: str,+ *,+ model: Optional[str] = None,+ ) -> List[Any]:+ """A list-valued capability, always a list even when absent."""+ return list(self.capability(provider, section, key, model=model, default=[]) or [])++ def default_model(self, provider: str, section: str) -> str:+ return str(self.capability(provider, section, "default_model", default="") or "")++ def models(self, provider: str, section: str) -> List[str]:+ return self.listing(provider, section, "models")++ def endpoint(self, provider: str, section: str, key: str = "endpoint") -> str:+ return str(self.capability(provider, section, key, default="") or "")++ def max_input_chars(+ self, provider: str, section: str, model: Optional[str] = None+ ) -> Optional[int]:+ value = self.capability(provider, section, "max_input_chars", model=model)+ return int(value) if isinstance(value, (int, float)) else None++ def billed_unit(self, provider: str, section: str) -> str:+ unit = self.capability(provider, section, "billed_unit", default="")+ if unit:+ return str(unit)+ # Fall back to a provider-level declaration, for providers whose+ # billing model is the same across every capability they serve.+ return str(self.provider_block(provider).get("billed_unit") or "")++ def languages(+ self,+ provider: str,+ section: str,+ *,+ model: Optional[str] = None,+ fallback_key: str = "common_languages",+ ) -> List[str]:+ """Language codes, falling back to the document-level shortlist.++ A provider with a genuinely closed set declares its own and that+ wins; open providers borrow the shared list, which is a convenience+ for the dropdown rather than a constraint.+ """+ codes = self.capability(provider, section, "languages", model=model)+ if codes:+ return list(codes)+ return list(self._data.get(fallback_key) or [])+++__all__ = ["CapabilityConfig"]diff --git a/server/services/plugin/params.py b/server/services/plugin/params.pynew file mode 100644index 00000000..9101baf5--- /dev/null+++ b/server/services/plugin/params.py@@ -0,0 +1,105 @@+"""Parameter coercion helpers shared by plugins.++The parameter panel stores ``""`` for any field the user has cleared or never+filled, **whatever the declared type**. Against a ``str`` field that is+harmless; against ``Optional[float]``, ``bool`` or ``Dict[str, Any]`` it is a+hard validation error, and a freshly-dropped node then fails on its own+defaults with something like *"Input should be a valid dictionary"*.++Lives here rather than in a plugin folder because it is a property of the+panel-to-Pydantic boundary, not of any vendor. Two plugins needed it before it+was worth extracting; the per-node precedents that predate it are+``AndroidServiceParams._coerce_parameters`` and+``WriteTodosParams._coerce_todos``.+"""++from __future__ import annotations++import json+from typing import Any, Dict, Sequence, Union, get_args, get_origin+++def _accepts_str(annotation: Any) -> bool:+ """Whether a blank string is a legitimate value for this annotation.++ ``Dict[str, Any]`` is the case worth spelling out: its ``get_args`` are+ ``(str, Any)``, so a naive "is str among the args" check would wrongly+ conclude a dict field accepts a string. Container origins are rejected+ before the args are consulted.+ """+ if annotation is Any or annotation is str:+ return True+ origin = get_origin(annotation)+ if origin is Union:+ return any(_accepts_str(arg) for arg in get_args(annotation))+ return False+++def coerce_blank_params(+ cls: Any, values: Any, *, object_fields: Sequence[str] = ()+) -> Any:+ """Normalize panel-supplied blanks before Pydantic validates them.++ Blank strings are dropped for fields that *cannot* hold a string, so the+ field's own default applies. Fields named in ``object_fields``+ additionally accept a JSON **object** string, because the panel has no+ object widget and renders them as a text input — a user who types+ ``{"a": 1}`` should get what they plainly meant.++ Blanks are deliberately NOT dropped for ``str`` fields: doing so would+ turn a ``min_length`` error into a confusing "field required".++ Use from a ``@model_validator(mode="before")``::++ @model_validator(mode="before")+ @classmethod+ def _coerce(cls, values):+ return coerce_blank_params(cls, values, object_fields=("options",))+ """+ if not isinstance(values, dict):+ return values++ cleaned: Dict[str, Any] = {}+ for key, value in values.items():+ if key in object_fields:+ cleaned[key] = coerce_json_object(key, value)+ continue++ field = cls.model_fields.get(key)+ blank = isinstance(value, str) and not value.strip()+ if blank and field is not None and not _accepts_str(field.annotation):+ continue+ cleaned[key] = value+ return cleaned+++def coerce_json_object(key: str, value: Any) -> Dict[str, Any]:+ """Accept a dict, a blank, or a JSON object string. Reject anything else.++ Rejecting is deliberate: coercing a blank recovers from a UI artefact,+ but silently turning ``"[1,2]"`` into ``{}`` would hide a real user error.+ """+ if isinstance(value, dict):+ return value+ if value is None:+ return {}+ if isinstance(value, str):+ text = value.strip()+ if not text:+ return {}+ try:+ parsed = json.loads(text)+ except ValueError as exc:+ raise ValueError(+ f'{key} must be a JSON object, e.g. {{"option": "value"}}. '+ f"Could not parse it: {exc}."+ ) from exc+ if isinstance(parsed, dict):+ return parsed+ raise ValueError(+ f"{key} must be a JSON object, not a {type(parsed).__name__}."+ )+ raise ValueError(f"{key} must be a JSON object, not a {type(value).__name__}.")+++__all__ = ["coerce_blank_params", "coerce_json_object"]diff --git a/server/skills/language_agent/sarvam-translate-skill/SKILL.md b/server/skills/language_agent/sarvam-translate-skill/SKILL.mddeleted file mode 100644index b3fb302f..00000000--- a/server/skills/language_agent/sarvam-translate-skill/SKILL.md+++ /dev/null@@ -1,123 +0,0 @@-----name: sarvam-translate-skill-description: Translate and transliterate text across 22 Indian languages, and detect which language a piece of text is written in, using Sarvam AI.-allowed-tools: sarvam_translate sarvam_transliterate sarvam_detect_language-metadata:- author: opencompany- version: "1.0"- category: language-------# Sarvam Text Skill--Sarvam AI's text APIs cover the Indian language space: translation across 22-languages, script conversion that preserves pronunciation, and language-identification.--## How It Works--This skill provides instructions and context. To execute, connect the-**Sarvam Translate**, **Sarvam Transliterate**, and/or **Sarvam Detect-Language** nodes to the agent's `input-tools` handle. All three share one-Sarvam API key.--## Choosing the right tool--These three are routinely confused. The distinction matters:--| You want to... | Use |-|---|---|-| Change the **language** — "Hello" to "नमस्ते" | `sarvam_translate` |-| Change the **script**, keeping the words — "namaste" to "नमस्ते" | `sarvam_transliterate` |-| Find out **what language** some text is in | `sarvam_detect_language` |--Transliteration is not translation. `sarvam_transliterate` on "namaste"-returns the same word in Devanagari; it does not turn it into "hello".--## sarvam_translate--| Field | Type | Required | Description |-|---|---|---|---|-| input | string | Yes | Text to translate |-| source_language_code | enum | No | Defaults to `auto` |-| target_language_code | enum | No | Defaults to `hi-IN` |-| model | enum | No | `sarvam-translate:v1` (22 langs, 2000 chars) or `mayura:v1` (10 langs, 1000 chars) |-| mode | enum | No | `formal` (default), `modern-colloquial`, `classic-colloquial`, `code-mixed` |-| speaker_gender | enum | No | `Male` / `Female`, for languages that inflect on it |-| output_script | enum | No | `roman`, `fully-native`, `spoken-form-in-native` |-| numerals_format | enum | No | `international` (default) or `native` |--Languages: `as-IN bn-IN brx-IN doi-IN en-IN gu-IN hi-IN kn-IN kok-IN ks-IN-mai-IN ml-IN mni-IN mr-IN ne-IN od-IN pa-IN sa-IN sat-IN sd-IN ta-IN te-IN-ur-IN`.--```json-{- "translated_text": "नमस्ते दुनिया",- "source_language_code": "en-IN"-}-```--Pick `mode` deliberately: `formal` for documents and notices,-`modern-colloquial` for chat and app copy, `code-mixed` when the audience-naturally mixes English with the target language.--## sarvam_transliterate--Languages: `bn-IN en-IN gu-IN hi-IN kn-IN ml-IN mr-IN od-IN pa-IN ta-IN-te-IN` (plus `auto` as a source).--Set `spoken_form: true` to expand numbers, dates and abbreviations into how-they are said aloud — useful when the output feeds text-to-speech.--```json-{- "transliterated_text": "नमस्ते",- "source_language_code": "en-IN"-}-```--## sarvam_detect_language--Takes `input` (max 1000 characters) and returns a BCP-47 language code plus-an ISO 15924 script code.--```json-{- "language_code": "hi-IN",- "script_code": "Deva"-}-```--**Both fields can be `null`** when the text is too short or ambiguous.-Always check before feeding the result into another call — do not assume a-language was detected.--## When to Use--- Localising content into Indian languages-- Normalising user input typed in Roman script into native script-- Routing a message to the right handler based on its language-- Preparing text for `text_to_speech`, which needs a target language--## When NOT to Use--- Non-Indian languages — Sarvam covers Indian languages plus English only-- Long documents in one call: cap is 2000 characters (`sarvam-translate:v1`)- or 1000 (`mayura:v1`). Split on sentence boundaries and translate in- batches; splitting mid-sentence degrades quality badly.-- Changing script only — reach for `sarvam_transliterate`, not translate--## API Details--- **Translate**: POST `https://api.sarvam.ai/translate` — 20 INR / 10K characters-- **Transliterate**: POST `https://api.sarvam.ai/transliterate`-- **Language ID**: POST `https://api.sarvam.ai/text-lid` — 3.5 INR / 10K characters-- **Auth**: `api-subscription-key` header--## Setup Requirements--A single Sarvam API key from [dashboard.sarvam.ai](https://dashboard.sarvam.ai),-entered once in the Credentials modal under **Sarvam AI**. The same key-covers translation, speech and the Sarvam chat models.diff --git a/server/skills/language_agent/translation-skill/SKILL.md b/server/skills/language_agent/translation-skill/SKILL.mdnew file mode 100644index 00000000..c6db8043--- /dev/null+++ b/server/skills/language_agent/translation-skill/SKILL.md@@ -0,0 +1,113 @@+---+name: translation-skill+description: Translate text between languages, convert between writing systems, and identify what language a piece of text is in — across DeepL, Sarvam AI and LLM-backed providers.+allowed-tools: translate_text transliterate_text detect_language+metadata:+ author: opencompany+ version: "2.0"+ category: language++---++# Translation Skill++Three tools, several providers behind each. Pick the provider that fits the+language pair rather than defaulting to one.++## How It Works++Connect **Translate**, **Transliterate** and/or **Detect Language** to the+agent's `input-tools` handle. Each has a `provider` parameter, and the+credential for the chosen provider must be configured in the Credentials+modal.++## The distinction that matters most++**Translation changes the words. Transliteration changes the script.**++| Input | Tool | Output |+|---|---|---|+| `hello` | `translate_text` → hi | `नमस्ते` |+| `namaste` | `transliterate_text` → hi | `नमस्ते` |++Both produce Devanagari, but only one changed the meaning. Getting this+backwards is the most common misuse: if the user wants to *read* foreign text,+they want translation; if they want to *type* their own language in its native+script, or make a foreign name pronounceable, they want transliteration.++## translate_text++| Field | Type | Required | Description |+|---|---|---|---|+| text | string | Yes | Text to translate |+| provider | enum | No | `deepl`, `sarvam`, `openai` |+| target_language | string | No | Blank uses the provider default |+| source_language | string | No | Blank auto-detects |+| translate_model | string | No | Blank uses the provider default |+| formality | string | No | Register, where the provider supports it |+| context | string | No | Disambiguating context — not itself translated |+| preserve_formatting | boolean | No | Keep line breaks and punctuation as-is |+| provider_options | object | No | Vendor-specific extras |++Returns `translated_text` and `detected_source_language`.++### Choosing a provider++- **deepl** — best quality for European languages, and the only one that+ reports exactly how many characters it billed. Language codes are uppercase+ (`DE`, `EN-US`, `PT-BR`). Prefer `prefer_more` / `prefer_less` for formality+ over `more` / `less`: the strict forms 400 on target languages that do not+ support the distinction. Does **not** transliterate or detect.+- **sarvam** — Indian languages: 22 on `sarvam-translate:v1`, 11 on+ `mayura:v1`. Codes are always `-IN` suffixed (`hi-IN`, `ta-IN`). `formality`+ maps to its register modes: `formal`, `modern-colloquial`,+ `classic-colloquial`, `code-mixed`.+- **openai** — LLM-backed. Covers pairs the others do not, needs no extra+ credential if a chat key is already configured, and takes free-form context+ well. Slower, and billed as tokens rather than characters.++## transliterate_text++| Field | Type | Required | Description |+|---|---|---|---|+| text | string | Yes | Text to convert |+| provider | enum | No | `sarvam`, `openai` |+| target_language | string | No | Language whose script to write in |+| source_language | string | No | Blank auto-detects |+| target_script | string | No | Script style, where the provider offers a choice |+| provider_options | object | No | Vendor-specific extras |++Returns `transliterated_text`. Sarvam's `target_script` values are `roman`,+`fully-native` and `spoken-form-in-native` — the last renders numbers and+abbreviations the way they would be *spoken*, which is what you want when the+output feeds `text_to_speech`.++## detect_language++| Field | Type | Required | Description |+|---|---|---|---|+| text | string | Yes | Text to identify |+| provider | enum | No | `sarvam`, `openai` |++Returns `language`, `script` and — from providers that report it —+`confidence`. Use it before translating when the source is unknown, or to+route text by language.++## When to Use++- The user asks for a translation, or pastes text in a language they cannot read+- Preparing text for `text_to_speech`, which needs a target language+- Routing or filtering content by language++## When NOT to Use++- Translating an entire document — chunk it first; every provider caps input+- Detecting language from a single word; give it a sentence+- Transliteration when the user actually meant translation (see above)++## Setup Requirements++One API key per provider you intend to use. The `sarvam` and `openai` keys are+the same ones their chat models use, so if those are configured these tools+work with no extra setup. DeepL needs its own key — free keys end in `:fx` and+are routed to the free host automatically.diff --git a/server/tests/nodes/test_sarvam.py b/server/tests/nodes/test_sarvam.pydeleted file mode 100644index 715441c1..00000000--- a/server/tests/nodes/test_sarvam.py+++ /dev/null@@ -1,313 +0,0 @@-"""Contract tests for the three Sarvam text service nodes.--sarvamTranslate / sarvamTransliterate / sarvamDetectLanguage.--All three authenticate through ``ctx.connection("sarvam")``, which injects-the ``api-subscription-key`` header from the shared credential — the same-key the OpenAI-compatible ``sarvamChatModel`` uses. httpx is mocked with-respx so no network is touched.--Sarvam's speech endpoints are covered by ``test_speech.py`` instead: they-are reached through the provider-abstracted ``textToSpeech`` /-``speechToText`` nodes now, with Sarvam as one provider among several.-"""--from __future__ import annotations--import base64-from pathlib import Path--import httpx-import pytest-import respx--from tests.nodes._mocks import patched_container, patched_pricing---pytestmark = pytest.mark.node_contract--_KEYS = {"sarvam": "sk_sarvam_test"}-_BASE = "https://api.sarvam.ai"---# ============================================================================-# sarvamTranslate-# ============================================================================---class TestSarvamTranslate:- URL = f"{_BASE}/translate"-- @respx.mock- async def test_happy_path(self, harness):- respx.post(self.URL).mock(- return_value=httpx.Response(- 200,- json={- "request_id": "req-1",- "translated_text": "नमस्ते दुनिया",- "source_language_code": "en-IN",- },- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamTranslate",- {- "input": "Hello world",- "source_language_code": "auto",- "target_language_code": "hi-IN",- },- )-- harness.assert_envelope(result, success=True)- payload = result["result"]- assert payload["translated_text"] == "नमस्ते दुनिया"- assert payload["source_language_code"] == "en-IN"-- sent = respx.calls.last.request- assert sent.headers["api-subscription-key"] == "sk_sarvam_test"-- @respx.mock- async def test_unset_optionals_are_omitted_not_nulled(self, harness):- """Sarvam 422s on explicit nulls for several optional fields."""- import json-- respx.post(self.URL).mock(- return_value=httpx.Response(- 200, json={"translated_text": "x", "source_language_code": "en-IN"}- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- await harness.execute(- "sarvamTranslate",- {"input": "hi", "target_language_code": "ta-IN"},- )-- body = json.loads(respx.calls.last.request.content)- assert "speaker_gender" not in body- assert "output_script" not in body- assert body["model"] == "sarvam-translate:v1"-- async def test_input_over_model_cap_is_a_user_error(self, harness):- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamTranslate",- {- "input": "x" * 1001,- "model": "mayura:v1",- "target_language_code": "hi-IN",- },- )-- harness.assert_envelope(result, success=False)- assert "1001 characters" in result["error"]- assert "mayura:v1" in result["error"]-- async def test_identical_language_pair_is_a_user_error(self, harness):- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamTranslate",- {- "input": "hi",- "source_language_code": "hi-IN",- "target_language_code": "hi-IN",- },- )-- harness.assert_envelope(result, success=False)- assert "identical" in result["error"]-- @respx.mock- async def test_api_error_surfaces_sarvam_message(self, harness):- respx.post(self.URL).mock(- return_value=httpx.Response(- 422, json={"error": {"message": "unsupported language pair"}}- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamTranslate",- {"input": "hi", "target_language_code": "hi-IN"},- )-- harness.assert_envelope(result, success=False)- assert "unsupported language pair" in result["error"]- assert "422" in result["error"]---# ============================================================================-# sarvamTransliterate-# ============================================================================---class TestSarvamTransliterate:- URL = f"{_BASE}/transliterate"-- @respx.mock- async def test_happy_path(self, harness):- respx.post(self.URL).mock(- return_value=httpx.Response(- 200,- json={- "transliterated_text": "नमस्ते",- "source_language_code": "en-IN",- },- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamTransliterate",- {"input": "namaste", "target_language_code": "hi-IN"},- )-- harness.assert_envelope(result, success=True)- assert result["result"]["transliterated_text"] == "नमस्ते"-- @respx.mock- async def test_spoken_form_numerals_only_sent_when_spoken_form_on(self, harness):- import json-- respx.post(self.URL).mock(- return_value=httpx.Response(- 200, json={"transliterated_text": "x", "source_language_code": "en-IN"}- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- await harness.execute(- "sarvamTransliterate",- {"input": "42", "target_language_code": "hi-IN", "spoken_form": False},- )-- body = json.loads(respx.calls.last.request.content)- assert body["spoken_form"] is False- assert "spoken_form_numerals_language" not in body---# ============================================================================-# sarvamDetectLanguage-# ============================================================================---class TestSarvamDetectLanguage:- URL = f"{_BASE}/text-lid"-- @respx.mock- async def test_happy_path(self, harness):- respx.post(self.URL).mock(- return_value=httpx.Response(- 200,- json={"request_id": "r", "language_code": "hi-IN", "script_code": "Deva"},- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamDetectLanguage", {"input": "नमस्ते"}- )-- harness.assert_envelope(result, success=True)- assert result["result"]["language_code"] == "hi-IN"- assert result["result"]["script_code"] == "Deva"-- @respx.mock- async def test_nulls_survive_as_none(self, harness):- """Sarvam returns nulls for unclassifiable input; don't coerce them."""- respx.post(self.URL).mock(- return_value=httpx.Response(- 200, json={"language_code": None, "script_code": None}- )- )-- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute("sarvamDetectLanguage", {"input": "?!"})-- harness.assert_envelope(result, success=True)- assert result["result"]["language_code"] is None-- async def test_over_cap_input_is_a_user_error(self, harness):- with patched_container(auth_api_keys=_KEYS), patched_pricing():- result = await harness.execute(- "sarvamDetectLanguage", {"input": "x" * 1001}- )-- harness.assert_envelope(result, success=False)- assert "1000" in result["error"]---# ============================================================================-# Cross-node contracts-# ============================================================================---class TestSarvamPluginContract:- NODE_TYPES = (- "sarvamTranslate",- "sarvamTransliterate",- "sarvamDetectLanguage",- )-- @pytest.mark.parametrize("node_type", NODE_TYPES)- def test_registered_in_the_language_group_and_usable_as_tool(self, node_type):- from services.node_registry import get_node_class-- cls = get_node_class(node_type)- assert cls is not None- assert cls.group == ("language", "tool")- assert cls.usable_as_tool is True- assert cls.task_queue == "rest-api"-- @pytest.mark.parametrize("node_type", NODE_TYPES)- def test_tool_name_is_the_snake_case_node_type(self, node_type):- """Skill `allowed-tools` resolution keys on this exact mapping."""- import re-- from services.node_registry import get_node_class-- expected = re.sub(r"(?<!^)(?=[A-Z])", "_", node_type).lower()- assert get_node_class(node_type).tool_name == expected-- @pytest.mark.parametrize("node_type", NODE_TYPES)- def test_tool_schema_has_no_refs(self, node_type):- """LLM tool schemas must be flat — no $defs/$ref survive inlining.-- Mirrors what ``AIService._get_tool_schema`` does for a plugin- exposed as a tool: inline the Params JSON schema.- """- from services.node_registry import get_node_class- from services.plugin.tool import inline_schema_refs-- schema = inline_schema_refs(- get_node_class(node_type).Params.model_json_schema()- )- rendered = repr(schema)- assert "$defs" not in rendered- assert "$ref" not in rendered-- @pytest.mark.parametrize("node_type", NODE_TYPES)- def test_shares_the_single_sarvam_credential(self, node_type):- from nodes.model._credentials import SarvamCredential- from services.node_registry import get_node_class-- assert get_node_class(node_type).credentials == (SarvamCredential,)-- def test_credential_injects_sarvams_native_header(self):- """Chat rides Bearer via the openai SDK; these APIs need this header."""- from nodes.model._credentials import SarvamCredential-- req = SarvamCredential.inject({"api_key": "k"}, {"headers": {}})- assert req["headers"] == {"api-subscription-key": "k"}-- @pytest.mark.parametrize("node_type", NODE_TYPES)- def test_output_schema_is_registered(self, node_type):- """Drives the parameter panel's "Input Data & Variables" listing."""- from services.node_output_schemas import get_node_output_schema-- schema = get_node_output_schema(node_type)- assert schema is not None- assert schema.get("properties")diff --git a/server/tests/nodes/test_translate.py b/server/tests/nodes/test_translate.pynew file mode 100644index 00000000..a591608f--- /dev/null+++ b/server/tests/nodes/test_translate.py@@ -0,0 +1,363 @@+"""Contract for the provider-abstracted text-language nodes.++The load-bearing assertions here are the capability-membership ones. DeepL+translates and does nothing else; if it could be *selected* for+transliteration the failure would surface at runtime, inside a paid call,+rather than being unrepresentable.+"""++from __future__ import annotations++import subprocess+import sys+from pathlib import Path++import httpx+import pytest+import respx++from ._mocks import patched_container, patched_pricing++pytestmark = pytest.mark.node_contract++SERVER_DIR = Path(__file__).resolve().parents[2]++_KEYS = {+ "deepl": "dl-test-key",+ "sarvam": "sv-test",+ "openai": "sk-test",+}+++# ============================================================================+# Capability membership IS the capability+# ============================================================================+++class TestCapabilityMembership:+ def test_translate_only_provider_is_absent_from_the_others(self):+ from nodes.translate._registry import (+ detect_providers,+ transliterate_providers,+ translate_providers,+ )++ assert "deepl" in translate_providers()+ assert "deepl" not in transliterate_providers()+ assert "deepl" not in detect_providers()++ def test_multi_capability_providers_are_in_all_three(self):+ from nodes.translate._registry import (+ detect_providers,+ transliterate_providers,+ translate_providers,+ )++ for provider in ("sarvam", "openai"):+ assert provider in translate_providers()+ assert provider in transliterate_providers()+ assert provider in detect_providers()++ def test_every_provider_resolves_to_a_registered_credential(self):+ """Config names credential *ids*, never Credential classes."""+ from services.plugin.credential import CREDENTIAL_REGISTRY++ from nodes.translate import _config+ from nodes.translate._registry import CAPABILITIES++ for capability, (providers, _) in CAPABILITIES.items():+ for provider in providers():+ credential = _config.credential_id(provider)+ assert credential in CREDENTIAL_REGISTRY, (+ f"{provider} ({capability}) maps to {credential!r}, "+ "which is not registered"+ )++ def test_nodes_declare_a_credential_for_every_provider_they_offer(self):+ from nodes.translate import _config+ from nodes.translate._registry import (+ detect_providers,+ transliterate_providers,+ translate_providers,+ )+ from nodes.translate.detect_language import DetectLanguageNode+ from nodes.translate.translate_text import TranslateTextNode+ from nodes.translate.transliterate_text import TransliterateTextNode++ for node, providers in (+ (TranslateTextNode, translate_providers()),+ (TransliterateTextNode, transliterate_providers()),+ (DetectLanguageNode, detect_providers()),+ ):+ declared = {c.id for c in node.credentials}+ for provider in providers:+ assert _config.credential_id(provider) in declared, (+ f"{node.type} cannot authenticate {provider}"+ )++ async def test_selecting_an_unsupported_provider_is_a_user_error(self, harness):+ """DeepL is not merely undocumented for transliteration — it is refused."""+ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "transliterateText", {"provider": "deepl", "text": "namaste"}+ )++ harness.assert_envelope(result, success=False)+ assert "deepl" in result["error"]+ assert "sarvam" in result["error"]+++# ============================================================================+# Wire shapes+# ============================================================================+++class TestDeepLWireShape:+ URL = "https://api.deepl.com/v2/translate"++ @respx.mock+ async def test_auth_scheme_and_array_body(self, harness):+ route = respx.post(self.URL).mock(+ return_value=httpx.Response(+ 200,+ json={+ "translations": [+ {+ "text": "Hallo",+ "detected_source_language": "EN",+ "billed_characters": 5,+ }+ ]+ },+ )+ )++ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "translateText",+ {"provider": "deepl", "text": "Hello", "target_language": "de"},+ )++ harness.assert_envelope(result, success=True)+ assert result["result"]["translated_text"] == "Hallo"+ assert result["result"]["detected_source_language"] == "EN"++ request = route.calls.last.request+ # "DeepL-Auth-Key", not Bearer.+ assert request.headers["authorization"] == "DeepL-Auth-Key dl-test-key"+ import json as _json++ body = _json.loads(request.content)+ # `text` is an array natively, and the target is upper-cased.+ assert body["text"] == ["Hello"]+ assert body["target_lang"] == "DE"++ @respx.mock+ async def test_a_free_key_is_routed_to_the_free_host(self, harness):+ """A ':fx' key on the pro host 403s with an unhelpful body."""+ route = respx.post("https://api-free.deepl.com/v2/translate").mock(+ return_value=httpx.Response(200, json={"translations": [{"text": "Bonjour"}]})+ )++ with patched_container(auth_api_keys={**_KEYS, "deepl": "abc:fx"}), patched_pricing():+ result = await harness.execute(+ "translateText",+ {"provider": "deepl", "text": "Hello", "target_language": "fr"},+ )++ harness.assert_envelope(result, success=True)+ assert route.called++ @respx.mock+ async def test_provider_reported_billing_is_preferred(self, harness):+ """DeepL tells us what it billed; that beats counting input."""+ respx.post(self.URL).mock(+ return_value=httpx.Response(+ 200,+ json={"translations": [{"text": "x", "billed_characters": 4242}]},+ )+ )++ with patched_container(auth_api_keys=_KEYS), patched_pricing() as pricing:+ await harness.execute(+ "translateText",+ {"provider": "deepl", "text": "short", "target_language": "de"},+ )++ counts = [c.args[2] for c in pricing.calculate_api_cost.call_args_list]+ assert 4242 in counts+++class TestSarvamWireShape:+ @respx.mock+ async def test_translate_uses_the_native_header_and_single_input(self, harness):+ route = respx.post("https://api.sarvam.ai/translate").mock(+ return_value=httpx.Response(+ 200,+ json={+ "translated_text": "नमस्ते",+ "source_language_code": "en-IN",+ "request_id": "sv-1",+ },+ )+ )++ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "translateText",+ {"provider": "sarvam", "text": "hello", "target_language": "hi-IN"},+ )++ harness.assert_envelope(result, success=True)+ assert result["result"]["translated_text"] == "नमस्ते"++ request = route.calls.last.request+ assert request.headers["api-subscription-key"] == "sv-test"+ import json as _json++ # Sarvam takes one string, not an array.+ assert _json.loads(request.content)["input"] == "hello"++ @respx.mock+ async def test_transliterate_hits_its_own_endpoint(self, harness):+ route = respx.post("https://api.sarvam.ai/transliterate").mock(+ return_value=httpx.Response(200, json={"transliterated_text": "नमस्ते"})+ )++ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "transliterateText",+ {"provider": "sarvam", "text": "namaste", "target_language": "hi-IN"},+ )++ harness.assert_envelope(result, success=True)+ assert result["result"]["transliterated_text"] == "नमस्ते"+ assert route.called++ @respx.mock+ async def test_detect_returns_language_and_script(self, harness):+ respx.post("https://api.sarvam.ai/text-lid").mock(+ return_value=httpx.Response(+ 200, json={"language_code": "hi-IN", "script_code": "Deva"}+ )+ )++ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "detectLanguage", {"provider": "sarvam", "text": "नमस्ते"}+ )++ harness.assert_envelope(result, success=True)+ assert result["result"]["language"] == "hi-IN"+ assert result["result"]["script"] == "Deva"++ async def test_over_cap_input_is_refused_before_the_call(self, harness):+ with patched_container(auth_api_keys=_KEYS), patched_pricing():+ result = await harness.execute(+ "translateText",+ {+ "provider": "sarvam",+ "text": "x" * 3000,+ "translate_model": "mayura:v1",+ },+ )++ harness.assert_envelope(result, success=False)+ assert "1000" in result["error"]+++# ============================================================================+# Node contract+# ============================================================================+++class TestNodeContract:+ def test_no_node_names_a_field_model_or_api_key(self):+ """Regression guard for the parameter-panel clobber."""+ from nodes.translate.detect_language import DetectLanguageParams+ from nodes.translate.translate_text import TranslateTextParams+ from nodes.translate.transliterate_text import TransliterateTextParams++ for params in (+ TranslateTextParams,+ TransliterateTextParams,+ DetectLanguageParams,+ ):+ assert "model" not in params.model_fields+ assert "api_key" not in params.model_fields++ def test_no_node_uses_declarative_routing(self):+ """`routing=` resolves credentials[0], ignoring `provider`."""+ from nodes.translate.detect_language import DetectLanguageNode+ from nodes.translate.translate_text import TranslateTextNode+ from nodes.translate.transliterate_text import TransliterateTextNode++ for node in (TranslateTextNode, TransliterateTextNode, DetectLanguageNode):+ for spec in node._operations.values():+ assert spec.routing is None, f"{node.type}.{spec.name} declares routing="++ def test_canvas_handles_stay_visible(self):+ from nodes.translate.detect_language import DetectLanguageNode+ from nodes.translate.translate_text import TranslateTextNode+ from nodes.translate.transliterate_text import TransliterateTextNode++ for node in (TranslateTextNode, TransliterateTextNode, DetectLanguageNode):+ assert node.hide_input_handle is False+ assert node.hide_output_handle is False++ @pytest.mark.parametrize(+ "node_type,payload",+ [+ ("translateText", {"text": "hi", "provider_options": "", "preserve_formatting": ""}),+ ("transliterateText", {"text": "hi", "provider_options": ""}),+ ("detectLanguage", {"text": "hi", "provider_options": ""}),+ ],+ )+ def test_blank_panel_values_fall_back_to_defaults(self, node_type, payload):+ from services.node_registry import get_node_class++ model = get_node_class(node_type).Params(**payload)+ assert model.provider_options == {}++ def test_json_object_string_is_parsed(self):+ from nodes.translate.translate_text import TranslateTextParams++ model = TranslateTextParams(+ text="hi", provider_options='{"glossary_id": "abc"}'+ )+ assert model.provider_options == {"glossary_id": "abc"}+++class TestLazySdkImports:+ """Registering translate providers must not import a heavy SDK."""++ HEAVY = ("openai", "anthropic", "google.genai")++ def test_registering_providers_imports_no_sdk(self):+ result = subprocess.run(+ [+ sys.executable,+ "-c",+ "import sys\n"+ "import nodes.translate\n"+ f"leaked = [m for m in {self.HEAVY!r} if m in sys.modules]\n"+ "print('LEAKED=' + ','.join(leaked))\n",+ ],+ cwd=SERVER_DIR,+ capture_output=True,+ text=True,+ timeout=120,+ )+ assert result.returncode == 0, result.stderr+ assert result.stdout.strip().endswith("LEAKED="), (+ f"translate provider registration imported heavy SDKs: {result.stdout!r}"+ )++ def test_every_declared_exception_ref_resolves(self):+ import nodes.translate # noqa: F401++ from nodes.translate._registry import CAPABILITIES++ for providers, get_spec in CAPABILITIES.values():+ for name in providers():+ assert get_spec(name).sdk_exception_types