Skip to content
Merged
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
61 changes: 60 additions & 1 deletion .github/workflows/desktop-macos.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
#name: Desktop macOS Bundle
#
#on:
# workflow_dispatch:
# push:
# branches: [main]
#
#jobs:
# macos-bundle:
# runs-on: macos-latest
#
# steps:
# - uses: actions/checkout@v4
#
# - uses: pnpm/action-setup@v4
# with:
# version: 10.11.0
#
# - uses: actions/setup-node@v4
# with:
# node-version: 20
# cache: pnpm
#
# # ❗统一 Python(必须)
# - uses: actions/setup-python@v5
# with:
# python-version: "3.11"
#
# # ❗安装 uv(关键)
# - name: Install uv
# run: pip install uv
#
# # ❗锁 backend 环境
# - name: Setup backend env
# working-directory: backend
# run: |
# uv sync --group desktop
#
# # JS deps
# - name: Install JS dependencies
# run: pnpm install --frozen-lockfile
#
# # ❗显式 build backend sidecar(关键)
# - name: Build backend sidecar
# working-directory: backend
# run: uv run python scripts/build_sidecar.py
#
# # Tauri build
# - name: Build macOS desktop bundle
# run: pnpm --filter desktop build:mac
#
# # 上传
# - name: Upload macOS artifacts
# uses: actions/upload-artifact@v4
# with:
# name: mercury-desktop-macos
# path: |
# apps/desktop/target/release/bundle/dmg/*.dmg
# apps/desktop/target/release/bundle/macos/*.app
name: Desktop macOS Bundle

on:
Expand Down Expand Up @@ -44,4 +103,4 @@ jobs:
name: mercury-desktop-macos
path: |
apps/desktop/target/release/bundle/dmg/*.dmg
apps/desktop/target/release/bundle/macos/*.app
apps/desktop/target/release/bundle/macos/*.app
113 changes: 88 additions & 25 deletions backend/agent_translation/agent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Translation agent - handles article translation via LLM."""

import asyncio
import re

from llm_providers import ChatMessage, ChatOptions, LLMProvider, get_provider
from llm_providers import (
ChatMessage,
ChatOptions,
LLMProvider,
LLMProviderError,
get_provider,
)


def chunk_by_headings(text: str, max_chars: int = 4000) -> list[str]:
Expand Down Expand Up @@ -58,9 +65,16 @@ class TranslationAgent:
"""

SYSTEM_PROMPT = (
"You are a professional translator. Your task is to translate the "
"provided article content accurately while preserving:\n"
"Translate Chinese to English, and English to Chinese.\n"
"You are a professional translator specializing in Chinese <-> English "
"translation. Translate the provided article content into the requested "
"target language.\n"
"- If the target language is English, translate every Chinese sentence into English.\n"
"- If the target language is Chinese (中文), "
"translate every English sentence into Chinese.\n"
"- Text already written in the target language must be left exactly as-is.\n"
"- Never echo the source text back untranslated, and never answer in the "
"source language when a translation is requested.\n\n"
"Preserve:\n"
"- Meaning and nuance of the original text\n"
"- Structure and formatting (paragraphs, lists, headers, etc.)\n"
"- Technical terms and proper nouns\n"
Expand Down Expand Up @@ -89,8 +103,22 @@ async def translate_chunk(
content: str,
target_lang: str,
temperature: float = 0.3,
max_retries: int = 3,
) -> dict:
"""翻译单个片段"""
"""翻译单个片段,失败时按指数退避重试。

Args:
content: 待翻译片段
target_lang: 目标语言
temperature: LLM temperature
max_retries: 最大尝试次数(含首次)

Returns:
dict with text + token usage.

Raises:
LLMProviderError: 重试耗尽后仍失败时抛出,由调用方决定如何降级。
"""
messages = [
ChatMessage(role="system", content=self.SYSTEM_PROMPT),
ChatMessage(
Expand All @@ -100,13 +128,24 @@ async def translate_chunk(
]

options = ChatOptions(temperature=temperature)
completion = await self.provider.chat(messages, options=options)

return {
"text": completion.content,
"prompt_tokens": completion.usage.prompt_tokens,
"completion_tokens": completion.usage.completion_tokens,
}
last_error: Exception | None = None
for attempt in range(max_retries):
try:
completion = await self.provider.chat(messages, options=options)
return {
"text": completion.content,
"prompt_tokens": completion.usage.prompt_tokens,
"completion_tokens": completion.usage.completion_tokens,
}
except LLMProviderError as exc:
last_error = exc
if attempt == max_retries - 1:
break
# 指数退避:0.5s, 1s, 2s ...,缓解限流/瞬时网络错误
await asyncio.sleep(0.5 * (2**attempt))

raise last_error if last_error else LLMProviderError("Translation failed")

async def translate_bilingual(
self,
Expand Down Expand Up @@ -144,26 +183,40 @@ async def translate_bilingual(
total_prompt_tokens = 0
total_completion_tokens = 0
bilingual_parts = []
failed_chunks = 0

for para in merged_paragraphs:
# 检查是否是标题
if para.startswith('#'):
# 标题直接翻译,不保留原文
result = await self.translate_chunk(para, target_lang, temperature)
bilingual_parts.append(result["text"])
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]
# 标题直接翻译,不保留原文;失败则退化为原标题
try:
result = await self.translate_chunk(para, target_lang, temperature)
bilingual_parts.append(result["text"])
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]
except LLMProviderError:
failed_chunks += 1
bilingual_parts.append(para)
else:
# 普通段落:先原文,再翻译
result = await self.translate_chunk(para, target_lang, temperature)
# 普通段落:先原文,再翻译。单段失败不影响整篇,保留原文占位。
original = f'<div class="bilingual-original">{para}</div>'
translation = f'<div class="bilingual-translation">{result["text"]}</div>'
try:
result = await self.translate_chunk(para, target_lang, temperature)
translated = result["text"]
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]
except LLMProviderError:
failed_chunks += 1
translated = para
translation = f'<div class="bilingual-translation">{translated}</div>'
bilingual_parts.append(original)
bilingual_parts.append("")
bilingual_parts.append(translation)
bilingual_parts.append("")
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]

# 全部片段都失败时,视为整体失败,交由 service 标记 failure
if merged_paragraphs and failed_chunks == len(merged_paragraphs):
raise LLMProviderError("All translation chunks failed")

# 合并结果
translated_text = "\n\n".join(bilingual_parts)
Expand Down Expand Up @@ -212,12 +265,22 @@ async def translate(
total_prompt_tokens = 0
total_completion_tokens = 0
translated_chunks = []
failed_chunks = 0

for chunk in chunks:
result = await self.translate_chunk(chunk, target_lang, temperature)
translated_chunks.append(result["text"])
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]
# 单段失败不影响整篇:保留原文占位,继续翻译其余片段
try:
result = await self.translate_chunk(chunk, target_lang, temperature)
translated_chunks.append(result["text"])
total_prompt_tokens += result["prompt_tokens"]
total_completion_tokens += result["completion_tokens"]
except LLMProviderError:
failed_chunks += 1
translated_chunks.append(chunk)

# 全部片段都失败时,视为整体失败,交由 service 标记 failure
if chunks and failed_chunks == len(chunks):
raise LLMProviderError("All translation chunks failed")

# 合并翻译结果
translated_text = "\n\n".join(translated_chunks)
Expand Down
98 changes: 98 additions & 0 deletions backend/agent_translation/tests/test_agent_resilience.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""Tests for TranslationAgent chunk-level resilience.

A single failing chunk must not sink the whole article: the agent retries,
then falls back to the original text for that chunk. Only when every chunk
fails does the agent raise so the service can mark the request as failed.
"""

import pytest

from agent_translation.agent import TranslationAgent
from llm_providers.base import (
ChatCompletion,
ChatMessage,
ChatOptions,
LLMProviderError,
NetworkError,
TokenUsage,
)


class FakeProvider:
"""Provider stub whose chat() outcome is scripted per call."""

def __init__(self, outcomes: list[object]) -> None:
self._outcomes = list(outcomes)
self.calls = 0

@property
def name(self) -> str:
return "fake"

@property
def model(self) -> str:
return "fake-model"

async def chat(
self,
messages: list[ChatMessage],
*,
stream: bool = False,
options: ChatOptions | None = None,
) -> ChatCompletion:
outcome = self._outcomes[min(self.calls, len(self._outcomes) - 1)]
self.calls += 1
if isinstance(outcome, Exception):
raise outcome
return ChatCompletion(
content=outcome,
model=self.model,
usage=TokenUsage(prompt_tokens=1, completion_tokens=1),
)


@pytest.mark.asyncio
async def test_chunk_retries_then_succeeds() -> None:
# First attempt fails, retry succeeds -> single chunk translated.
provider = FakeProvider([NetworkError("boom"), "你好"])
agent = TranslationAgent(provider=provider)

result = await agent.translate("Hello world", target_lang="中文")

assert provider.calls == 2
assert "你好" in result["translated_text"]


@pytest.mark.asyncio
async def test_all_chunks_failing_raises() -> None:
# When every chunk fails after retries, the agent raises so the service
# can mark the whole request as failed (rather than returning garbage).
content = "# Heading\n\nFirst paragraph here.\n\n" + ("x" * 600) + "\n\nSecond block."
provider = FakeProvider([NetworkError("down")]) # every call fails
agent = TranslationAgent(provider=provider)

with pytest.raises(LLMProviderError):
await agent.translate(content, target_lang="中文")


@pytest.mark.asyncio
async def test_single_chunk_failure_does_not_abort_when_others_succeed() -> None:
# Script: chunk 1 fails 3x (retries) then we move on; chunk 2 succeeds.
# Use bilingual=False path with two chunks split by headings.
content = "# Title A\n\n" + ("a" * 4100) + "\n\n# Title B\n\nBody."
# Each translate_chunk does up to 3 attempts. First chunk: 3 failures.
# Remaining calls succeed.
outcomes: list[object] = [
NetworkError("1"),
NetworkError("2"),
NetworkError("3"),
"translated",
]
provider = FakeProvider(outcomes)
agent = TranslationAgent(provider=provider)

result = await agent.translate(content, target_lang="中文")

# The article still returns; the failed chunk falls back to its original text.
assert "translated" in result["translated_text"]
assert result["provider"] == "fake"
Loading
Loading