Repository navigation
feat(translation): panel-only bilingual results, visual line grouping, flag language switcher - #83
Conversation
…ad safety - MTranServer: connection precheck (verifyConnection) and state fixes - TranslationEngineRegistry: temporary providers, cache invalidation via mutation revisions and creation tokens - LLM providers: segment batching and defensive response parsing (truncation, malformed JSON) with continuation safety for OpenAI/Claude - ScreenCoder: tile oversized analysis images; SmartPrivacyRedaction tweaks - TextInsertionTarget/TextInsertService/TextSelectionService: safer insertion - Settings/EngineConfigSheet: expose new verification options
…d flag switcher - Merge same-line OCR/VLM fragments before translation (VisualLineGrouping): inline code translates in context and each visual line becomes one unit - Remove in-image bilingual overlay (unreadable on dense text, obscured the original); translations render only in the side panel so the canvas stays clean for reading and annotating - Replace target-language dropdown with a one-click flag row (English = GB); switching immediately re-runs the translation - Add copy original / copy translation / copy all buttons to both panels; OCR mode keeps the single copy button - Regression tests: line grouping (16), panel pixels untouched, flag mapping
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
🔍 OpenCodeReview found 28 issue(s) in this PR.
|
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | ||
| let isSelected = AppSettings.shared.translationTargetLanguage == language | ||
| return Button { |
There was a problem hiding this comment.
languageFlagButton / panelCopyButton / flashMessage 与 PreviewResultsPanel.swift 中新增的实现几乎逐行相同(仅数据源 model.segments 与 viewModel.bilingualSegments 不同)。两份拷贝今后极易在样式或行为修正时只改一处而产生不一致(本次改动本身就是先在 Menu 版本出现过的 UI 的第二次重写)。建议抽成共享的子视图(如 TargetLanguageFlagRow、TranslationCopyButtonRow),通过 segment 提供闭包或数据参数注入。
Suggestion:
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | |
| let isSelected = AppSettings.shared.translationTargetLanguage == language | |
| return Button { | |
| // 与 PreviewResultsPanel 共用 TargetLanguageFlagRow(language:isSelected:onSelect:) | |
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | |
| let isSelected = AppSettings.shared.translationTargetLanguage == language | |
| return Button { |
| var options = config.options ?? EngineOptions.default(for: engine) ?? EngineOptions() | ||
| options.baseURL = baseURL | ||
| options.modelName = modelName |
There was a problem hiding this comment.
测试配置中 options.baseURL/modelName 被无条件覆盖,与 saveConfig() 的「非空才更新」逻辑不一致:当表单字段为空时,saveConfig 会保留已有 baseURL,而此处会把空字符串写入测试配置。对 MTranServer 而言 provider 会回退到 UserDefaults 的 .default 配置,导致「测试连接」探测的地址可能与实际保存使用的地址不同,测试结果具有误导性。建议与 saveConfig 保持一致,仅在非空时覆盖。
Suggestion:
| var options = config.options ?? EngineOptions.default(for: engine) ?? EngineOptions() | |
| options.baseURL = baseURL | |
| options.modelName = modelName | |
| var options = config.options ?? EngineOptions.default(for: engine) ?? EngineOptions() | |
| if !baseURL.isEmpty { options.baseURL = baseURL } | |
| if !modelName.isEmpty { options.modelName = modelName } |
| // Restore original settings if test is just for checking | ||
| settings.mtranServerHost = originalHost | ||
| settings.mtranServerPort = originalPort | ||
| let isAvailable = await MTranServerEngine(configuration: config).checkConnection() |
There was a problem hiding this comment.
checkConnection(config:) 声明为 async throws -> Bool(见 MTranServerEngine.swift 第 266 行),此处调用缺少 try,无法通过编译。改为 try await ... .checkConnection()。
Suggestion:
| let isAvailable = await MTranServerEngine(configuration: config).checkConnection() | |
| let isAvailable = try await MTranServerEngine(configuration: config).checkConnection() |
| private struct EndpointKey: Hashable, Sendable { | ||
| let host: String | ||
| let port: Int | ||
| } |
There was a problem hiding this comment.
EndpointKey 仅由 host+port 组成,而探测行为还受 Configuration.timeout 影响(request.timeoutInterval = min(timeout, 2.0))。同一 host:port 下不同 timeout 的配置会共享缓存条目与 in-flight 探测:先用短超时配置探测失败(超时)后,结果会被长超时配置的查询命中而误判不可用。由于 TTL 仅 5 秒影响有限,建议要么将 timeout(封顶 2 秒后的值)纳入 EndpointKey,要么将探测超时固定为常量与配置解耦。
Suggestion:
| private struct EndpointKey: Hashable, Sendable { | |
| let host: String | |
| let port: Int | |
| } | |
| private struct EndpointKey: Hashable, Sendable { | |
| let host: String | |
| let port: Int | |
| let probeTimeout: TimeInterval | |
| } |
| /// Check if MTranServer is available (synchronous property, never performs network I/O) | ||
| static var isAvailable: Bool { shared.isAvailable } | ||
|
|
||
| let endpoints = ["/health", "/", "/translate"] | ||
| for endpoint in endpoints { | ||
| var components = URLComponents() | ||
| components.scheme = "http" | ||
| components.host = host | ||
| components.port = port | ||
| components.path = endpoint | ||
| var isAvailable: Bool { cachedAvailability(for: .default) } |
There was a problem hiding this comment.
同步属性 isAvailable 现在仅读取 5 秒 TTL 缓存,未命中(冷启动或缓存过期)时永远返回 false,不再回退到实际探测。当前调用方 TranslationEngineType.isAvailable 若在任何 UI/逻辑路径中被读取,将系统性低估可用性。既然已提供 cachedAvailability 与异步 checkAvailability,建议要么在文档中明确该属性为"仅缓存快照"并重命名,要么在缓存未命中时仍触发一次后台异步探测以预热。
Suggestion:
| /// Check if MTranServer is available (synchronous property, never performs network I/O) | |
| static var isAvailable: Bool { shared.isAvailable } | |
| let endpoints = ["/health", "/", "/translate"] | |
| for endpoint in endpoints { | |
| var components = URLComponents() | |
| components.scheme = "http" | |
| components.host = host | |
| components.port = port | |
| components.path = endpoint | |
| var isAvailable: Bool { cachedAvailability(for: .default) } | |
| /// Cached-only snapshot of availability; false when no fresh (TTL-valid) probe exists. | |
| /// Use `checkAvailability(...)` for an authoritative answer. | |
| static var isAvailable: Bool { shared.isAvailable } | |
| var isAvailable: Bool { cachedAvailability(for: .default) } |
| let keychain = self.keychain | ||
| let appleAlreadyRegistered = providers[.apple] != nil | ||
|
|
||
| return Task { | ||
| switch type { | ||
| case .apple: | ||
| if appleAlreadyRegistered { | ||
| throw RegistryError.alreadyRegistered | ||
| } | ||
| return AppleTranslationProvider() |
There was a problem hiding this comment.
appleAlreadyRegistered 在创建 Task 前于 actor 内求值为布尔快照,Task 闭包捕获的是该值而非实时状态。若任务执行前 apple 曾被 unregister,任务会创建第二个 AppleTranslationProvider 并通过返回值交给调用方(不会缓存),与"apple 已注册则抛 alreadyRegistered"的意图不一致;反之,注册后又取消注册的窗口内也可能错误抛出。建议在 Task 内直接检查实时注册状态(如通过回调查询 registry),或依赖外层的 bumpEngineMutationRevision 校验丢弃过期结果。
Suggestion:
| let keychain = self.keychain | |
| let appleAlreadyRegistered = providers[.apple] != nil | |
| return Task { | |
| switch type { | |
| case .apple: | |
| if appleAlreadyRegistered { | |
| throw RegistryError.alreadyRegistered | |
| } | |
| return AppleTranslationProvider() | |
| let keychain = self.keychain | |
| return Task { | |
| switch type { | |
| case .apple: | |
| // Re-check registration state inside the task so the guard | |
| // reflects the registry state at completion-check time. | |
| if await self.hasManualOverride(for: .apple) { | |
| throw RegistryError.alreadyRegistered | |
| } | |
| return AppleTranslationProvider() |
| if let index = lines.firstIndex(where: { belongsToSameLine(segment, lineFragments: $0) }) { | ||
| lines[index].append(segment) | ||
| } else { | ||
| lines.append([segment]) | ||
| } |
| guard let first = fragments.first else { return nil } | ||
| guard fragments.count > 1 else { return first } | ||
|
|
||
| let ordered = fragments.sorted { $0.boundingBox.minX < $1.boundingBox.minX } |
There was a problem hiding this comment.
mergedSegment 始终按 boundingBox.minX 升序拼接片段。对阿拉伯语、希伯来语等 RTL 文本行,视觉顺序(左→右)与逻辑字符顺序(右→左)相反,按 minX 排序会把该行文本反转后送入翻译引擎,产生错误翻译。建议根据片段文本的书写方向(如检测首字符 Unicode BiDi 类别)决定升序/降序拼接,或在文档中明确声明仅支持 LTR/CJK。
Suggestion:
| let ordered = fragments.sorted { $0.boundingBox.minX < $1.boundingBox.minX } | |
| let isRTL = fragments.first.map { fragment in | |
| fragment.text.unicodeScalars.first.map(VisualLineGrouping.isRTLCharacter) ?? false | |
| } ?? false | |
| let ordered = fragments.sorted { | |
| isRTL ? $0.boundingBox.minX > $1.boundingBox.minX : $0.boundingBox.minX < $1.boundingBox.minX | |
| } |
| static func joinText(_ lhs: String, _ rhs: String) -> String { | ||
| guard let last = lhs.last, let first = rhs.first else { return lhs + rhs } | ||
| if isCJK(last), isCJK(first) { return lhs + rhs } | ||
| return lhs + " " + rhs | ||
| } |
| /// Translation prep step after `filteredForTranslation()`: merges | ||
| /// same-line fragments so each visual line is translated as one unit. | ||
| func mergingVisualLines() -> ScreenAnalysisResult { |
There was a problem hiding this comment.
Verified findings fixed: - MTranServerEngine: reject illegal hostnames (spaces/non-ASCII) before percentEncodedHost, avoiding an uncatchable NSException on legacy UserDefaults values; regression test added - LLMTranslationResponseParser: only treat responses starting with 'data:' as SSE — translation bodies containing '\ndata:' lines parse as JSON again; regression test added - EngineConfigSheet: empty baseURL/modelName no longer overwrite the test config, matching saveConfig semantics so the probe hits the persisted URL - TranslationEngineType: gateway statuses (401/403/429) on non-translate endpoints now fall through to the next endpoint; 5xx still fails fast (preserves single-probe semantics, tests added); isAvailable documented as a cached-only snapshot - TranslationEngineRegistry: forceRefresh cancels and drops stale in-flight provider creations instead of racing duplicates - VisualLineGrouping: near-zero-gap identifier fragments (underscore boundary or identifier-shaped on both sides) join without an inserted space so VLM-split code tokens survive; tests added - Removed unused ScreenAnalysisResult.mergingVisualLines() extension Rejected findings (verified false positives): missing-try compile error (protocol extension checkConnection is non-throwing) and piece-count mismatch in LLM batching (batching is whole-text grouping with a 1:1 output contract via separator split and per-text retry)
| HStack(spacing: 4) { | ||
| ForEach(Self.targetLanguages, id: \.self) { language in | ||
| languageFlagButton(for: language) | ||
| } | ||
| .controlSize(.small) | ||
| .fixedSize() | ||
| } |
There was a problem hiding this comment.
8 个国旗按钮固定 27pt 宽,总宽约 240pt(含间距),加上重跑、复制(翻译 tab 下为 3 个文案按钮)、关闭按钮及 flash 提示文本,同一 HStack 在窄面板下可能溢出或被压缩。原下拉菜单仅占一个固定宽度,此处回归风险建议验证最窄面板场景(如考虑 ScrollView(.horizontal) 或减少语言数量)。
Suggestion:
| HStack(spacing: 4) { | |
| ForEach(Self.targetLanguages, id: \.self) { language in | |
| languageFlagButton(for: language) | |
| } | |
| .controlSize(.small) | |
| .fixedSize() | |
| } | |
| ScrollView(.horizontal, showsIndicators: false) { | |
| HStack(spacing: 4) { | |
| ForEach(Self.targetLanguages, id: \.self) { language in | |
| languageFlagButton(for: language) | |
| } | |
| } | |
| } |
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | ||
| let isSelected = AppSettings.shared.translationTargetLanguage == language | ||
| return Button { | ||
| guard AppSettings.shared.translationTargetLanguage != language else { return } | ||
| AppSettings.shared.translationTargetLanguage = language | ||
| model.onRerun?() |
There was a problem hiding this comment.
languageFlagButton 与 PreviewResultsPanel 中同名实现几乎逐字重复(含选中态背景、描边、无障碍修饰符),仅取值来源不同(AppSettings vs 本地 @State)。此外此处直接读取 AppSettings.shared,选中态依赖全局单例变化而触发视图刷新,与 Preview 面板基于本地 state 的刷新路径不一致。建议抽取共享的 FlagLanguageSwitcher 组件,并统一状态来源,降低后续双处维护漂移风险。
Suggestion:
| private func languageFlagButton(for language: TranslationLanguage) -> some View { | |
| let isSelected = AppSettings.shared.translationTargetLanguage == language | |
| return Button { | |
| guard AppSettings.shared.translationTargetLanguage != language else { return } | |
| AppSettings.shared.translationTargetLanguage = language | |
| model.onRerun?() | |
| struct FlagLanguageSwitcher: View { | |
| let languages: [TranslationLanguage] | |
| @Binding var selection: TranslationLanguage? | |
| // ... 统一渲染与交互,两个面板复用 | |
| } |
| panelCopyButton( | ||
| title: String(localized: "preview.panel.copySource"), | ||
| text: model.segments.map(\.sourceText).joined(separator: "\n") | ||
| ) | ||
| panelCopyButton( | ||
| title: String(localized: "preview.panel.copyTranslation"), | ||
| text: model.segments.map(\.translated).joined(separator: "\n") | ||
| ) |
There was a problem hiding this comment.
同 PreviewResultsPanel:"preview.panel.copySource"、"preview.panel.copyTranslation" 本地化键在资源文件中不存在,运行时会显示 key 原文。请补充所有语言的翻译条目。
Suggestion:
| panelCopyButton( | |
| title: String(localized: "preview.panel.copySource"), | |
| text: model.segments.map(\.sourceText).joined(separator: "\n") | |
| ) | |
| panelCopyButton( | |
| title: String(localized: "preview.panel.copyTranslation"), | |
| text: model.segments.map(\.translated).joined(separator: "\n") | |
| ) | |
| "preview.panel.copySource" = "Copy Source"; | |
| "preview.panel.copyTranslation" = "Copy Translation"; |
| panelCopyButton( | ||
| title: String(localized: "preview.panel.copySource"), | ||
| text: viewModel.bilingualSegments.map(\.sourceText).joined(separator: "\n") | ||
| ) | ||
| panelCopyButton( | ||
| title: String(localized: "preview.panel.copyTranslation"), | ||
| text: viewModel.bilingualSegments.map(\.translated).joined(separator: "\n") | ||
| ) |
There was a problem hiding this comment.
新增本地化键 "preview.panel.copySource"、"preview.panel.copyTranslation" 未在任何 Localizable.strings 中定义(已检索 en/zh-Hans 资源文件均无对应条目)。String(localized:) 在缺失键时会直接显示 key 原文,属于用户可见回归。请为所有支持语言补充这两个键,以及 TextTranslationCoordinator 中新增的 "textTranslation.error.insertFailed.copied" 键。
Suggestion:
| panelCopyButton( | |
| title: String(localized: "preview.panel.copySource"), | |
| text: viewModel.bilingualSegments.map(\.sourceText).joined(separator: "\n") | |
| ) | |
| panelCopyButton( | |
| title: String(localized: "preview.panel.copyTranslation"), | |
| text: viewModel.bilingualSegments.map(\.translated).joined(separator: "\n") | |
| ) | |
| // Localizable.strings (en / zh-Hans 等) | |
| "preview.panel.copySource" = "Copy Source"; // zh: 复制原文 | |
| "preview.panel.copyTranslation" = "Copy Translation"; // zh: 复制译文 | |
| "textTranslation.error.insertFailed.copied" = "Translation copied to clipboard."; // zh: 译文已复制到剪贴板。 |
| let ys = origins(length: image.height, limit: heightLimit, step: heightStep) | ||
| return ys.flatMap { y in xs.map { x in | ||
| let rect = CGRect(x: x, y: y, width: min(widthLimit, image.width - x), height: min(heightLimit, image.height - y)) | ||
| return Tile(image: image.cropping(to: rect) ?? image, pixelRect: rect) |
There was a problem hiding this comment.
当 cropping(to:) 返回 nil(如内存受限或矩形异常)时回退为整张原图,但 pixelRect 仍是切片矩形。下游 merging 会用这个 pixelRect 把整图的识别框映射到切片区域,产生静默错位结果而非失败。建议 cropping 失败时抛错或至少跳过该 tile,保证 image 与 pixelRect 一致。
Suggestion:
| return Tile(image: image.cropping(to: rect) ?? image, pixelRect: rect) | |
| guard let cropped = image.cropping(to: rect) else { | |
| throw VLMProviderError.invalidResponse("Failed to crop image tile") | |
| } | |
| return Tile(image: cropped, pixelRect: rect) |
| logger.warning("Batch split failed, retrying only unmatched individual texts") | ||
| return try await translateBatches(texts.map { [$0] }, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate) |
| let keychain = self.keychain | ||
| let appleAlreadyRegistered = providers[.apple] != nil |
| static func mergeLines(in segments: [TextSegment]) -> [TextSegment] { | ||
| guard segments.count > 1 else { return segments } |
There was a problem hiding this comment.
mergeLines 对无效包围盒(width/height ≤ 0)的片段处理不一致:belongsToSameLine 直接返回 false,使该片段总是自成一"行",随后 mergedSegment 因 count == 1 原样返回,空/退化框片段会带着无效锚点继续进入翻译与叠加渲染流程。建议在入口处先过滤掉 box 无效的片段(或统一丢弃/打日志),而不是让它们伪装成独立行存留。
Suggestion:
| static func mergeLines(in segments: [TextSegment]) -> [TextSegment] { | |
| guard segments.count > 1 else { return segments } | |
| static func mergeLines(in segments: [TextSegment]) -> [TextSegment] { | |
| let valid = segments.filter { $0.boundingBox.width > 0 && $0.boundingBox.height > 0 } | |
| guard valid.count > 1 else { return valid } |
| var lines: [[TextSegment]] = [] | ||
| for segment in sorted { | ||
| if let index = lines.firstIndex(where: { belongsToSameLine(segment, lineFragments: $0) }) { | ||
| lines[index].append(segment) | ||
| } else { | ||
| lines.append([segment]) | ||
| } | ||
| } |
There was a problem hiding this comment.
行归属判定基于已 union 扩大的行框(lineBox),且先到先得:片段依次追加后行框不断变宽/变高,多列布局或表格中后续片段可能与已扩大的行框垂直重叠且水平间距达标而错误并入,把不同列、不同行的文本粘成一行。由于 mergeLines 结果直接决定翻译锚点(TranslationRenderPipeline),错合并会导致译文错位叠加。建议归属判定只用该行的首个片段(种子)作为参照,而非随追加扩大的 union 框。
Suggestion:
| var lines: [[TextSegment]] = [] | |
| for segment in sorted { | |
| if let index = lines.firstIndex(where: { belongsToSameLine(segment, lineFragments: $0) }) { | |
| lines[index].append(segment) | |
| } else { | |
| lines.append([segment]) | |
| } | |
| } | |
| var lines: [[TextSegment]] = [] | |
| var seeds: [CGRect] = [] | |
| for segment in sorted { | |
| if let index = seeds.firstIndex(where: { belongsToSameLine(segment, seed: $0) }) { | |
| lines[index].append(segment) | |
| } else { | |
| lines.append([segment]) | |
| seeds.append(segment.boundingBox) | |
| } | |
| } |
| if gluesDirectly, | ||
| isIdentifierCharacter(last), isIdentifierCharacter(first), | ||
| lhs.hasSuffix("_") || rhs.hasPrefix("_") | ||
| || (isIdentifierLike(lhs) && isIdentifierLike(rhs)) { | ||
| return lhs + rhs | ||
| } |
变更
结果展示重构(按实测反馈迭代)
视觉行聚合
main、DESIGMA_PLUGINS_TOKEN)不再被孤立错译,每个视觉行一个翻译单元与面板行锚点引擎健壮性(评审遗留跟进)
测试
影响
BilingualOverlayView及 4 个废弃本地化 key;新增 2 个复制 key