diff --git a/.github/workflows/desktop-macos.yml b/.github/workflows/desktop-macos.yml
index ed653e1..80ba8ae 100644
--- a/.github/workflows/desktop-macos.yml
+++ b/.github/workflows/desktop-macos.yml
@@ -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:
@@ -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
\ No newline at end of file
diff --git a/backend/agent_translation/agent.py b/backend/agent_translation/agent.py
index e0342e4..cfe6476 100644
--- a/backend/agent_translation/agent.py
+++ b/backend/agent_translation/agent.py
@@ -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]:
@@ -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"
@@ -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(
@@ -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,
@@ -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'
{para}
'
- translation = f'{result["text"]}
'
+ 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'{translated}
'
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)
@@ -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)
diff --git a/backend/agent_translation/tests/test_agent_resilience.py b/backend/agent_translation/tests/test_agent_resilience.py
new file mode 100644
index 0000000..8c0a88d
--- /dev/null
+++ b/backend/agent_translation/tests/test_agent_resilience.py
@@ -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"
diff --git a/backend/feed_engine/fetcher.py b/backend/feed_engine/fetcher.py
index d9510a1..a894138 100644
--- a/backend/feed_engine/fetcher.py
+++ b/backend/feed_engine/fetcher.py
@@ -1,6 +1,114 @@
+# from __future__ import annotations
+#
+# import asyncio
+# from dataclasses import dataclass
+# from urllib.error import HTTPError, URLError
+# from urllib.parse import urlsplit
+# from urllib.request import Request, urlopen
+#
+# from feed_engine.errors import FeedEngineError
+#
+#
+# @dataclass(frozen=True)
+# class FetchResponse:
+# status_code: int
+# final_url: str
+# body: bytes
+# etag: str | None
+# last_modified: str | None
+# content_type: str | None
+#
+#
+# def validate_url(url: str) -> str:
+# value = url.strip()
+# parts = urlsplit(value)
+# if parts.scheme not in {"http", "https"} or not parts.netloc:
+# raise FeedEngineError(
+# "INVALID_URL",
+# "Feed URL must be an absolute http or https URL.",
+# status_code=400,
+# context={"url": url},
+# )
+# print("[VALIDATE URL]", repr(url))
+# return value
+#
+#
+# async def fetch_feed(
+# url: str,
+# *,
+# etag: str | None = None,
+# last_modified: str | None = None,
+# timeout_seconds: float = 15.0,
+# ) -> FetchResponse:
+# return await asyncio.to_thread(
+# _fetch_feed_sync,
+# validate_url(url),
+# etag,
+# last_modified,
+# timeout_seconds,
+# )
+#
+#
+# def _fetch_feed_sync(
+# url: str,
+# etag: str | None,
+# last_modified: str | None,
+# timeout_seconds: float,
+# ) -> FetchResponse:
+# headers = {
+# "User-Agent": "Mercury/0.1 feed-engine",
+# "Accept": "application/rss+xml, application/atom+xml, application/xml, text/xml, */*",
+# }
+# if etag:
+# headers["If-None-Match"] = etag
+# if last_modified:
+# headers["If-Modified-Since"] = last_modified
+#
+# request = Request(url, headers=headers)
+# try:
+# with urlopen(request, timeout=timeout_seconds) as response:
+# return FetchResponse(
+# status_code=response.status,
+# final_url=response.url,
+# body=response.read(),
+# etag=response.headers.get("ETag"),
+# last_modified=response.headers.get("Last-Modified"),
+# content_type=response.headers.get("Content-Type"),
+# )
+# except HTTPError as exc:
+# if exc.code == 304:
+# return FetchResponse(
+# status_code=304,
+# final_url=url,
+# body=b"",
+# etag=exc.headers.get("ETag"),
+# last_modified=exc.headers.get("Last-Modified"),
+# content_type=exc.headers.get("Content-Type"),
+# )
+# raise FeedEngineError(
+# "FETCH_FAILED",
+# "Feed server returned an error.",
+# status_code=502,
+# context={"url": url, "status": exc.code},
+# ) from exc
+# except TimeoutError as exc:
+# raise FeedEngineError(
+# "FETCH_TIMEOUT",
+# "Feed request timed out.",
+# status_code=504,
+# context={"url": url},
+# ) from exc
+# except (URLError, OSError) as exc:
+# raise FeedEngineError(
+# "FETCH_FAILED",
+# "Feed request failed.",
+# status_code=502,
+# context={"url": url, "reason": str(exc)},
+# ) from exc
from __future__ import annotations
import asyncio
+import ssl
from dataclasses import dataclass
from urllib.error import HTTPError, URLError
from urllib.parse import urlsplit
@@ -22,6 +130,7 @@ class FetchResponse:
def validate_url(url: str) -> str:
value = url.strip()
parts = urlsplit(value)
+
if parts.scheme not in {"http", "https"} or not parts.netloc:
raise FeedEngineError(
"INVALID_URL",
@@ -29,6 +138,8 @@ def validate_url(url: str) -> str:
status_code=400,
context={"url": url},
)
+
+ print("[VALIDATE URL]", repr(url))
return value
@@ -54,18 +165,25 @@ def _fetch_feed_sync(
last_modified: str | None,
timeout_seconds: float,
) -> FetchResponse:
+
headers = {
"User-Agent": "Mercury/0.1 feed-engine",
"Accept": "application/rss+xml, application/atom+xml, application/xml, text/xml, */*",
}
+
if etag:
headers["If-None-Match"] = etag
if last_modified:
headers["If-Modified-Since"] = last_modified
request = Request(url, headers=headers)
+
+ print("[FETCH START]", url)
+
try:
with urlopen(request, timeout=timeout_seconds) as response:
+ print("[FETCH OK]", response.status)
+
return FetchResponse(
status_code=response.status,
final_url=response.url,
@@ -74,7 +192,10 @@ def _fetch_feed_sync(
last_modified=response.headers.get("Last-Modified"),
content_type=response.headers.get("Content-Type"),
)
+
except HTTPError as exc:
+ print("[HTTP ERROR]", exc.code, exc.reason)
+
if exc.code == 304:
return FetchResponse(
status_code=304,
@@ -84,23 +205,54 @@ def _fetch_feed_sync(
last_modified=exc.headers.get("Last-Modified"),
content_type=exc.headers.get("Content-Type"),
)
+
raise FeedEngineError(
"FETCH_FAILED",
- "Feed server returned an error.",
+ "Feed server returned HTTP error.",
status_code=502,
- context={"url": url, "status": exc.code},
+ context={
+ "url": url,
+ "status": exc.code,
+ "reason": str(exc.reason),
+ },
) from exc
+
except TimeoutError as exc:
+ print("[TIMEOUT]", str(exc))
+
raise FeedEngineError(
"FETCH_TIMEOUT",
"Feed request timed out.",
status_code=504,
context={"url": url},
) from exc
- except (URLError, OSError) as exc:
+
+ except ssl.SSLCertVerificationError as exc:
+ print("[SSL ERROR]", str(exc))
+
+ raise FeedEngineError(
+ "SSL_ERROR",
+ "SSL certificate verification failed.",
+ status_code=495,
+ context={"url": url, "reason": str(exc)},
+ ) from exc
+
+ except URLError as exc:
+ print("[URL ERROR]", repr(exc))
+
+ raise FeedEngineError(
+ "FETCH_FAILED",
+ "Feed request failed (network error).",
+ status_code=502,
+ context={"url": url, "reason": str(exc)},
+ ) from exc
+
+ except OSError as exc:
+ print("[OS ERROR]", str(exc))
+
raise FeedEngineError(
"FETCH_FAILED",
- "Feed request failed.",
+ "Feed request failed (OS error).",
status_code=502,
context={"url": url, "reason": str(exc)},
) from exc
diff --git a/packages/ui/src/App.tsx b/packages/ui/src/App.tsx
index 04bd7b5..e80fff3 100644
--- a/packages/ui/src/App.tsx
+++ b/packages/ui/src/App.tsx
@@ -979,7 +979,6 @@ function ReaderDetail(props: {
const [translationMode, setTranslationMode] = useState<"original" | "translation">("original");
const [noteDirty, setNoteDirty] = useState(false);
const [isTranslating, setIsTranslating] = useState(false);
- const showSplitTranslation = translationMode === "translation" && state.readerMode === "dual";
useEffect(() => {
setTranslationMode("original");
@@ -993,11 +992,6 @@ function ReaderDetail(props: {
void props.onEnsureEntryContent(entry.id);
}, [entry?.id, entry?.readerHtml, props.onEnsureEntryContent]);
- const translationHtmlRendered = useMemo(
- () => (entry?.translationHtml ? markdownToHtml(entry.translationHtml) : ""),
- [entry?.translationHtml]
- );
-
if (!entry) {
return (
@@ -1007,8 +1001,8 @@ function ReaderDetail(props: {
);
}
- const articleHtml = translationMode === "translation" && entry.translationHtml
- ? renderSimpleMarkdown(entry.translationHtml)
+ const articleHtml = translationMode === "translation" && entry.translationHtml
+ ? renderBilingualMarkdown(entry.translationHtml)
: entry.readerHtml;
async function toggleTranslation() {
@@ -1034,7 +1028,7 @@ function ReaderDetail(props: {
return;
}
const entryId = entry.id;
- const targetLang = state.locale === "zh-Hans" ? "中文" : "English";
+ const targetLang = detectTargetLang(entry.readerHtml);
setIsTranslating(true);
props.onUpdateEntry(entryId, (current) => ({ ...current, translationStatus: "running" }));
try {
@@ -1071,7 +1065,7 @@ function ReaderDetail(props: {
// 否则调用翻译 API
props.onUpdateEntry(entry.id, (current) => ({ ...current, translationStatus: "running" }));
import("./services/api").then(({ translateArticle }) => {
- translateArticle(entry.id, "Chinese")
+ translateArticle(entry.id, detectTargetLang(entry.readerHtml))
.then((result) => {
props.onUpdateEntry(entry.id, (current) => ({
...current,
@@ -1175,27 +1169,16 @@ function ReaderDetail(props: {
))}
)}
-
+
{showReader && (
- )}
- {showSplitTranslation && (
-
)}
{showWeb && (
@@ -2432,10 +2415,39 @@ function markdownToHtml(markdown: string): string {
return blocks.join("\n");
}
+// Render translated content that may contain bilingual wrapper divs
+// (
…
/ "bilingual-translation").
+// The markdown INSIDE each wrapper is rendered via markdownToHtml so bold,
+// lists, headings etc. display correctly; the wrapper divs are preserved so
+// the bilingual CSS still applies. Plain (non-bilingual) markdown falls
+// through to markdownToHtml unchanged.
+function renderBilingualMarkdown(text: string): string {
+ const blockPattern = /
([\s\S]*?)<\/div>/g;
+ if (!blockPattern.test(text)) {
+ return markdownToHtml(text);
+ }
+ blockPattern.lastIndex = 0;
+ return text.replace(blockPattern, (_match, variant, inner) =>
+ `
${markdownToHtml(inner.trim())}
`
+ );
+}
+
function capitalize(value: string): string {
return `${value[0]?.toUpperCase() ?? ""}${value.slice(1)}`;
}
+// Pick the translation target by detecting the article's dominant language:
+// Chinese source -> English, otherwise -> Chinese (中文). This keeps the
+// bilingual view meaningful regardless of the UI locale, so an English
+// article read in an English UI still gets a Chinese translation rather than
+// a no-op "English -> English" round trip.
+function detectTargetLang(content: string): string {
+ const chineseChars = (content.match(/[一-鿿]/g) ?? []).length;
+ const asciiLetters = (content.match(/[A-Za-z]/g) ?? []).length;
+ // Treat the text as Chinese when CJK characters clearly dominate.
+ return chineseChars > asciiLetters ? "English" : "中文";
+}
+
function emptyProviderDraft(): ProviderDraft {
return {
name: "",
diff --git a/packages/ui/src/styles/app.css b/packages/ui/src/styles/app.css
index 15b31e8..b34d162 100644
--- a/packages/ui/src/styles/app.css
+++ b/packages/ui/src/styles/app.css
@@ -569,10 +569,6 @@ textarea {
width: 50%;
}
-.reader-surface.split-translation > * {
- width: 50%;
-}
-
.reader-article.translation-pane {
border-left: 1px solid var(--color-border);
}