Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 69 additions & 15 deletions src/lingodotdev/engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,15 +18,30 @@ class EngineConfig(BaseModel):
"""Configuration for the LingoDotDevEngine"""

api_key: str
engine_id: Optional[str] = None
Comment thread
coderabbitai[bot] marked this conversation as resolved.
api_url: str = "https://engine.lingo.dev"
batch_size: int = Field(default=25, ge=1, le=250)
ideal_batch_item_size: int = Field(default=250, ge=1, le=2500)

@validator("api_url")
@validator("engine_id", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: str) -> str:
def validate_engine_id(cls, v: Optional[str]) -> Optional[str]:
if v is None:
return None
v = v.strip()
return v if v else None

@validator("api_url", pre=True, always=True)
@classmethod
def validate_api_url(cls, v: Optional[str], values: Dict[str, Any]) -> str:
default_url = "https://engine.lingo.dev"
if v is None:
v = default_url
if not v.startswith(("http://", "https://")):
raise ValueError("API URL must be a valid HTTP/HTTPS URL")
v = v.rstrip("/")
if v == default_url and values.get("engine_id"):
return "https://api.lingo.dev"
return v


Expand DownExpand Up@@ -55,6 +70,11 @@ def __init__(self, config: Dict[str, Any]):
"""
self.config = EngineConfig(**config)
self._client: Optional[httpx.AsyncClient] = None
self._session_id: str = generate()

@property
def _is_vnext(self) -> bool:
return self.config.engine_id is not None

async def __aenter__(self):
"""Async context manager entry"""
Expand All@@ -68,10 +88,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
async def _ensure_client(self):
"""Ensure the httpx client is initialized"""
if self._client is None or self._client.is_closed:
if self._is_vnext:
auth_header = {"X-API-Key": self.config.api_key}
else:
auth_header = {"Authorization": f"Bearer {self.config.api_key}"}
self._client = httpx.AsyncClient(
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {self.config.api_key}",
**auth_header,
},
timeout=60.0,
)
Expand DownExpand Up@@ -200,16 +224,27 @@ async def _localize_chunk(
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/i18n")

request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}

if payload.get("reference"):
request_data["reference"] = payload["reference"]
if self._is_vnext:
url = f"{self.config.api_url}/process/{self.config.engine_id}/localize"
request_data: Dict[str, Any] = {
"params": {"fast": fast},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
"sourceLocale": source_locale,
"targetLocale": target_locale,
"data": payload["data"],
"sessionId": self._session_id,
Comment thread
AndreyHirsa marked this conversation as resolved.
}
Comment thread
AndreyHirsa marked this conversation as resolved.
if payload.get("reference"):
request_data["reference"] = payload["reference"]
else:
url = urljoin(self.config.api_url, "/i18n")
request_data = {
"params": {"workflowId": workflow_id, "fast": fast},
"locale": {"source": source_locale, "target": target_locale},
"data": payload["data"],
}
if payload.get("reference"):
request_data["reference"] = payload["reference"]

try:
response = await self._client.post(url, json=request_data)
Expand DownExpand Up@@ -455,7 +490,11 @@ async def recognize_locale(self, text: str) -> str:

await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/recognize")

if self._is_vnext:
url = f"{self.config.api_url}/process/recognize"
Comment thread
AndreyHirsa marked this conversation as resolved.
else:
url = urljoin(self.config.api_url, "/recognize")

try:
response = await self._client.post(url, json={"text": text})
Expand DownExpand Up@@ -487,10 +526,17 @@ async def whoami(self) -> Optional[Dict[str, str]]:
"""
await self._ensure_client()
assert self._client is not None # Type guard for mypy
url = urljoin(self.config.api_url, "/whoami")

if self._is_vnext:
url = f"{self.config.api_url}/users/me"
else:
url = urljoin(self.config.api_url, "/whoami")
Comment thread
AndreyHirsa marked this conversation as resolved.

try:
response = await self._client.post(url)
if self._is_vnext:
response = await self._client.get(url)
else:
response = await self._client.post(url)

if response.is_success:
payload = self._safe_parse_json(response)
Expand DownExpand Up@@ -541,6 +587,7 @@ async def quick_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> Any:
"""
Comment thread
AndreyHirsa marked this conversation as resolved.
Quick one-off translation without manual context management.
Expand All@@ -553,6 +600,7 @@ async def quick_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
Translated content (same type as input)
Expand All@@ -576,6 +624,8 @@ async def quick_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
params = {
Expand All@@ -600,6 +650,7 @@ async def quick_batch_translate(
source_locale: Optional[str] = None,
api_url: str = "https://engine.lingo.dev",
fast: bool = True,
engine_id: Optional[str] = None,
) -> List[Any]:
Comment thread
AndreyHirsa marked this conversation as resolved.
"""
Quick batch translation to multiple target locales.
Expand All@@ -612,6 +663,7 @@ async def quick_batch_translate(
source_locale: Source language code (optional, auto-detected if None)
api_url: API endpoint URL
fast: Enable fast mode for quicker translations
engine_id: Optional engine ID for vNext API.

Returns:
List of translated content (one for each target locale)
Expand All@@ -628,6 +680,8 @@ async def quick_batch_translate(
"api_key": api_key,
"api_url": api_url,
}
if engine_id:
config["engine_id"] = engine_id

async with cls(config) as engine:
if isinstance(content, str):
Expand Down
177 changes: 177 additions & 0 deletions tests/test_engine.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post):
assert request_data["locale"]["target"] == "es"
assert request_data["params"]["fast"] is True
assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"}


@pytest.mark.asyncio
class TestVNextEngine:
"""Test vNext / Engine ID specific behavior"""

def setup_method(self):
"""Set up test fixtures"""
self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"}
self.engine = LingoDotDevEngine(self.config)

Comment thread
AndreyHirsa marked this conversation as resolved.
def teardown_method(self):
"""Clean up engine client"""
if self.engine._client and not self.engine._client.is_closed:
asyncio.get_event_loop().run_until_complete(self.engine.close())

def test_engine_id_empty_string_treated_as_none(self):
"""Test that empty engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_whitespace_treated_as_none(self):
"""Test that whitespace-only engine_id is treated as None"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "})
assert engine.config.engine_id is None
assert engine._is_vnext is False
assert engine.config.api_url == "https://engine.lingo.dev"

def test_engine_id_stripped(self):
"""Test that engine_id is stripped of whitespace"""
engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "})
assert engine.config.engine_id == "eng_123"
assert engine._is_vnext is True

def test_api_url_trailing_slash_stripped(self):
"""Test that trailing slash is stripped from api_url"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com/",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_engine_id_default_api_url(self):
"""Test that engine_id switches default api_url to api.lingo.dev"""
assert self.engine.config.api_url == "https://api.lingo.dev"
assert self.engine.config.engine_id == "my-engine-id"

def test_engine_id_with_explicit_api_url(self):
"""Test that explicit api_url is preserved with engine_id"""
engine = LingoDotDevEngine(
{
"api_key": "key",
"engine_id": "eng",
"api_url": "https://custom.api.com",
}
)
assert engine.config.api_url == "https://custom.api.com"

def test_is_vnext_true(self):
"""Test _is_vnext is True with engine_id"""
assert self.engine._is_vnext is True

def test_is_vnext_false_without_engine_id(self):
"""Test _is_vnext is False without engine_id"""
engine = LingoDotDevEngine(
{"api_key": "key", "api_url": "https://api.test.com"}
)
assert engine._is_vnext is False

def test_session_id_generated(self):
"""Test that session_id is generated on init"""
assert self.engine._session_id
assert isinstance(self.engine._session_id, str)

async def test_vnext_ensure_client_uses_x_api_key(self):
"""Test that vNext engine uses X-API-Key header"""
await self.engine._ensure_client()
assert self.engine._client is not None
assert self.engine._client.headers.get("x-api-key") == "test_api_key"
assert "authorization" not in self.engine._client.headers
await self.engine.close()

async def test_classic_ensure_client_uses_bearer(self):
"""Test that classic engine uses Bearer auth header"""
engine = LingoDotDevEngine(
{"api_key": "test_key", "api_url": "https://api.test.com"}
)
await engine._ensure_client()
assert engine._client is not None
assert engine._client.headers.get("authorization") == "Bearer test_key"
assert "x-api-key" not in engine._client.headers
await engine.close()

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_localize_chunk_url_and_body(self, mock_post):
"""Test vNext localize chunk uses correct URL and body format"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"key": "translated"}}
mock_post.return_value = mock_response

await self.engine._localize_chunk(
"en",
"es",
{"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}},
"wf",
True,
)

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert body["params"] == {"fast": True}
assert body["data"] == {"key": "value"}
assert body["sessionId"] == self.engine._session_id
assert body["reference"] == {"es": {"key": "ref"}}

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_recognize_locale_url(self, mock_post):
"""Test vNext recognize_locale uses correct URL"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"locale": "es"}
mock_post.return_value = mock_response

await self.engine.recognize_locale("Hola mundo")

url = mock_post.call_args[0][0]
assert url == "https://api.lingo.dev/process/recognize"

@patch("lingodotdev.engine.httpx.AsyncClient.get")
async def test_vnext_whoami(self, mock_get):
"""Test vNext whoami calls GET /users/me"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"id": "usr_abc", "email": "user@example.com"}
mock_get.return_value = mock_response

result = await self.engine.whoami()

assert result == {"email": "user@example.com", "id": "usr_abc"}
url = mock_get.call_args[0][0]
assert url == "https://api.lingo.dev/users/me"

@patch("lingodotdev.engine.httpx.AsyncClient.post")
async def test_vnext_full_localization_workflow(self, mock_post):
"""Test full vNext localization workflow via localize_object"""
mock_response = Mock()
mock_response.is_success = True
mock_response.json.return_value = {"data": {"greeting": "hola"}}
mock_post.return_value = mock_response

result = await self.engine.localize_object(
{"greeting": "hello"},
{"source_locale": "en", "target_locale": "es", "fast": True},
)

assert result == {"greeting": "hola"}

call_args = mock_post.call_args
url = call_args[0][0]
assert url == "https://api.lingo.dev/process/my-engine-id/localize"

body = call_args[1]["json"]
assert body["sourceLocale"] == "en"
assert body["targetLocale"] == "es"
assert "sessionId" in body
assert "locale" not in body # classic format should NOT be present