From 046d4b258ac60183e678c519314436d67ec97ba5 Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 27 Oct 2026 19:42:28 +0800 Subject: [PATCH 1/3] fix(translation): harden engine prechecks, provider caching and payload 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 --- .../TextTranslationCoordinator.swift | 11 +- .../Features/Settings/EngineConfigSheet.swift | 9 +- .../Features/Settings/SettingsViewModel.swift | 22 +- TransFrame/Models/TranslationEngineType.swift | 233 +++++---- .../Resources/en.lproj/Localizable.strings | 4 +- .../zh-Hans.lproj/Localizable.strings | 4 +- TransFrame/Services/AnalysisImageTiling.swift | 85 ++++ TransFrame/Services/ClaudeVLMProvider.swift | 40 +- TransFrame/Services/MTranServerEngine.swift | 214 ++++++--- TransFrame/Services/OpenAIVLMProvider.swift | 42 +- TransFrame/Services/ScreenCoderEngine.swift | 20 +- .../SmartPrivacyRedactionService.swift | 2 +- TransFrame/Services/TextInsertService.swift | 49 ++ TransFrame/Services/TextInsertionTarget.swift | 149 ++++++ .../Services/TextSelectionService.swift | 15 +- .../Translation/LLMTranslationBatching.swift | 51 ++ .../LLMTranslationResponseParser.swift | 86 ++++ .../CompatibleTranslationProvider.swift | 187 ++++---- .../Providers/LLMTranslationProvider.swift | 199 ++------ .../TranslationEngineRegistry.swift | 407 +++++++++++++--- TransFrame/Services/TranslationProvider.swift | 5 + TransFrame/Services/TranslationService.swift | 57 ++- .../AnalysisImageTilingTests.swift | 79 ++++ .../LLMResponseTruncationTests.swift | 104 +++++ TransFrameTests/MTranServerEngineTests.swift | 441 ++++++++++++++++++ .../TextInsertionSafetyTests.swift | 117 +++++ .../TranslationEngineRegistryTests.swift | 415 ++++++++++++++++ TransFrameTests/URLRequestBody.swift | 19 + .../VLMContinuationSafetyTests.swift | 86 ++++ 29 files changed, 2528 insertions(+), 624 deletions(-) create mode 100644 TransFrame/Services/AnalysisImageTiling.swift create mode 100644 TransFrame/Services/TextInsertionTarget.swift create mode 100644 TransFrame/Services/Translation/LLMTranslationBatching.swift create mode 100644 TransFrame/Services/Translation/LLMTranslationResponseParser.swift create mode 100644 TransFrameTests/AnalysisImageTilingTests.swift create mode 100644 TransFrameTests/LLMResponseTruncationTests.swift create mode 100644 TransFrameTests/MTranServerEngineTests.swift create mode 100644 TransFrameTests/TextInsertionSafetyTests.swift create mode 100644 TransFrameTests/TranslationEngineRegistryTests.swift create mode 100644 TransFrameTests/URLRequestBody.swift create mode 100644 TransFrameTests/VLMContinuationSafetyTests.swift diff --git a/TransFrame/App/Coordinators/TextTranslationCoordinator.swift b/TransFrame/App/Coordinators/TextTranslationCoordinator.swift index fd5ccf4..a7887ef 100644 --- a/TransFrame/App/Coordinators/TextTranslationCoordinator.swift +++ b/TransFrame/App/Coordinators/TextTranslationCoordinator.swift @@ -177,9 +177,11 @@ final class TextTranslationCoordinator { // Step 1: Capture selected text let textSelectionService = TextSelectionService.shared let selectedText: String + let insertionTarget: CapturedTextInsertion? do { - let selectionResult = try await textSelectionService.captureSelectedText() + let selectionResult = try await textSelectionService.captureSelectedText(bindForInsertion: true) + insertionTarget = selectionResult.insertionTarget selectedText = selectionResult.text logger.info("Captured selected text: \(selectedText.count) characters") } catch let error as TextSelectionService.CaptureError { @@ -239,7 +241,7 @@ final class TextTranslationCoordinator { showInsertionProgress(message: String(localized: "translateAndInsert.progress.inserting")) do { - try await TextInsertService.shared.deleteSelectionAndInsert(translatedText) + try TextInsertService.shared.replaceSelection(with: translatedText, captured: insertionTarget) logger.info("Successfully inserted translated text") } catch let error as TextInsertService.InsertError { logger.error("Text insertion failed: \(error.localizedDescription)") @@ -248,6 +250,11 @@ final class TextTranslationCoordinator { alert.alertStyle = .warning alert.messageText = String(localized: "textTranslation.error.insertFailed") alert.informativeText = error.localizedDescription + let pasteboard = NSPasteboard.general + pasteboard.clearContents() + if pasteboard.setString(translatedText, forType: .string) { + alert.informativeText += "\n\n" + String(localized: "textTranslation.error.insertFailed.copied") + } alert.addButton(withTitle: String(localized: "common.ok")) alert.runModal() } catch { diff --git a/TransFrame/Features/Settings/EngineConfigSheet.swift b/TransFrame/Features/Settings/EngineConfigSheet.swift index 5f43d89..bfa5c56 100644 --- a/TransFrame/Features/Settings/EngineConfigSheet.swift +++ b/TransFrame/Features/Settings/EngineConfigSheet.swift @@ -418,8 +418,13 @@ struct EngineConfigSheet: View { } } - // Test connection - try await TranslationService.shared.verifyConnection(for: engine) + // Test connection with the unsaved form snapshot, not the old engine config. + var testConfig = config + var options = config.options ?? EngineOptions.default(for: engine) ?? EngineOptions() + options.baseURL = baseURL + options.modelName = modelName + testConfig.options = options + try await TranslationService.shared.verifyConnection(for: engine, config: testConfig) await MainActor.run { testSuccess = true diff --git a/TransFrame/Features/Settings/SettingsViewModel.swift b/TransFrame/Features/Settings/SettingsViewModel.swift index 4bbb668..9f35c73 100644 --- a/TransFrame/Features/Settings/SettingsViewModel.swift +++ b/TransFrame/Features/Settings/SettingsViewModel.swift @@ -1256,28 +1256,10 @@ final class SettingsViewModel { Task { do { - // Parse URL and update settings temporarily for test - guard let (host, port) = parseMTranServerURL(mtranServerURL), !host.isEmpty else { + guard let config = MTranServerEngine.Configuration(serverURL: mtranServerURL) else { throw MTranServerError.invalidURL } - - // Save current settings - let originalHost = settings.mtranServerHost - let originalPort = settings.mtranServerPort - - // Update settings for test - settings.mtranServerHost = host - settings.mtranServerPort = port - - // Reset cache to use new settings - MTranServerChecker.resetCache() - - // Check availability - let isAvailable = await MTranServerChecker.checkAvailability() - - // Restore original settings if test is just for checking - settings.mtranServerHost = originalHost - settings.mtranServerPort = originalPort + let isAvailable = await MTranServerEngine(configuration: config).checkConnection() await MainActor.run { mtranTestSuccess = isAvailable diff --git a/TransFrame/Models/TranslationEngineType.swift b/TransFrame/Models/TranslationEngineType.swift index 2525557..279bb5c 100644 --- a/TransFrame/Models/TranslationEngineType.swift +++ b/TransFrame/Models/TranslationEngineType.swift @@ -258,134 +258,161 @@ enum EngineCategory: String, CaseIterable, Sendable, Codable { // MARK: - MTranServer Availability Checker /// Helper to check if MTranServer is available on the system -enum MTranServerChecker { - private static let lock = NSLock() - nonisolated(unsafe) private static var _isAvailable: Bool? - - /// Check if MTranServer is available (synchronous property) - static var isAvailable: Bool { - if let cached = lock.withLock({ _isAvailable }) { - return cached - } +final class MTranServerChecker: @unchecked Sendable { + static let shared = MTranServerChecker() - let result = checkMTranServer() - lock.withLock { _isAvailable = result } - return result + private struct EndpointKey: Hashable, Sendable { + let host: String + let port: Int } - /// Check if MTranServer is available asynchronously without blocking calling threads - static func checkAvailability() async -> Bool { - if let cached = lock.withLock({ _isAvailable }) { - return cached - } + private struct CacheEntry { + let isAvailable: Bool + let checkedAt: TimeInterval + } - let result = await checkMTranServerAsync() - lock.withLock { _isAvailable = result } - return result + private struct InFlightProbe { + let token: UInt64 + let task: Task } - private static func resolvedHostAndPort() -> (host: String, port: Int) { - let defaults = UserDefaults.standard - let prefix = "TransFrame." - var host = defaults.string(forKey: prefix + "mtranServerHost") ?? "localhost" - let port = defaults.object(forKey: prefix + "mtranServerPort") as? Int ?? 8989 - if host == "localhost" { - host = "127.0.0.1" - } - return (host, port) + private let lock = NSLock() + private let session: URLSession + private let cacheTTL: TimeInterval + private let now: @Sendable () -> TimeInterval + private let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "TransFrame", category: "MTranServerChecker") + private var cachedResults: [EndpointKey: CacheEntry] = [:] + private var inFlightProbes: [EndpointKey: InFlightProbe] = [:] + private var nextToken: UInt64 = 0 + + init( + session: URLSession = .shared, + cacheTTL: TimeInterval = 5, + now: @escaping @Sendable () -> TimeInterval = { ProcessInfo.processInfo.systemUptime } + ) { + self.session = session + self.cacheTTL = cacheTTL + self.now = now } - private static func checkMTranServerAsync() async -> Bool { - let (host, port) = resolvedHostAndPort() - let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "TransFrame", category: "MTranServerChecker") - logger.info("Checking MTranServer async at \(host):\(port)") + /// 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) } - guard let url = components.url else { continue } - var request = URLRequest(url: url) - request.timeoutInterval = 2.0 - request.httpMethod = "GET" + func cachedAvailability( + for configuration: MTranServerEngine.Configuration = .default + ) -> Bool { + guard let key = endpointKey(for: configuration) else { return false } + let currentTime = now() + return lock.withLock { + guard let entry = cachedResults[key], + currentTime - entry.checkedAt < cacheTTL else { return false } + return entry.isAvailable + } + } - do { - let (_, response) = try await URLSession.shared.data(for: request) - let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1 - if statusCode > 0 { - logger.info("MTranServer available via \(endpoint)") - return true - } - } catch { - logger.debug("MTranServer check \(endpoint) failed: \(error.localizedDescription)") + /// Check if MTranServer is available asynchronously without blocking calling threads + static func checkAvailability( + configuration: MTranServerEngine.Configuration = .default, + forceRefresh: Bool = false + ) async throws -> Bool { + try await shared.checkAvailability(configuration: configuration, forceRefresh: forceRefresh) + } + + func checkAvailability( + configuration: MTranServerEngine.Configuration = .default, + forceRefresh: Bool = false + ) async throws -> Bool { + try Task.checkCancellation() + guard let key = endpointKey(for: configuration) else { return false } + let currentTime = now() + let probe: InFlightProbe? = lock.withLock { + if !forceRefresh, + let entry = cachedResults[key], + currentTime - entry.checkedAt < cacheTTL { + return nil + } + if let existing = inFlightProbes[key] { return existing } + nextToken &+= 1 + let token = nextToken + let task = Task { [session, logger] in + await Self.probeEndpoint(configuration: configuration, session: session, logger: logger) } + let created = InFlightProbe(token: token, task: task) + inFlightProbes[key] = created + return created } - - logger.info("MTranServer final availability: false") - return false + guard let probe else { return cachedAvailability(for: configuration) } + let result = try await withTaskCancellationHandler { + try Task.checkCancellation() + return await probe.task.value + } onCancel: { + probe.task.cancel() + } + try Task.checkCancellation() + lock.withLock { + guard inFlightProbes[key]?.token == probe.token else { return } + inFlightProbes.removeValue(forKey: key) + if let result { + cachedResults[key] = CacheEntry(isAvailable: result, checkedAt: now()) + } + } + return result ?? false } - private final class ResultBox: @unchecked Sendable { - var value: Bool = false + static func resetCache() { shared.resetCache() } + + func resetCache() { + let probes = lock.withLock { + nextToken &+= 1 + cachedResults.removeAll() + defer { inFlightProbes.removeAll() } + return Array(inFlightProbes.values) + } + probes.forEach { $0.task.cancel() } } - private static func checkMTranServer() -> Bool { - let (host, port) = resolvedHostAndPort() - let logger = Logger(subsystem: Bundle.main.bundleIdentifier ?? "TransFrame", category: "MTranServerChecker") - logger.info("Checking MTranServer at \(host):\(port)") + private func endpointKey( + for configuration: MTranServerEngine.Configuration + ) -> EndpointKey? { + guard let host = configuration.endpointHost else { return nil } + return EndpointKey(host: host.lowercased(), port: configuration.serverPort) + } - // Try multiple endpoints for health check + private static func probeEndpoint( + configuration: MTranServerEngine.Configuration, + session: URLSession, + logger: Logger + ) async -> Bool? { let endpoints = ["/health", "/", "/translate"] - var isAvailable = false - for endpoint in endpoints { - var components = URLComponents() - components.scheme = "http" - components.host = host - components.port = port - components.path = endpoint - - guard let url = components.url else { continue } - + if Task.isCancelled { return nil } + guard let url = configuration.endpointURL(path: endpoint) else { return false } var request = URLRequest(url: url) - request.timeoutInterval = 2.0 + request.timeoutInterval = min(configuration.timeout, 2.0) request.httpMethod = "GET" - let semaphore = DispatchSemaphore(value: 0) - let resultBox = ResultBox() - - let task = URLSession.shared.dataTask(with: request) { _, response, error in - if let error = error { - logger.debug("MTranServer check \(endpoint) failed: \(error.localizedDescription)") + do { + let (_, response) = try await session.data(for: request) + guard let statusCode = (response as? HTTPURLResponse)?.statusCode else { return false } + if (200..<300).contains(statusCode) { + logger.info("MTranServer available via \(endpoint)") + return true } - let statusCode = (response as? HTTPURLResponse)?.statusCode ?? -1 - logger.debug("MTranServer check \(endpoint): status \(statusCode)") - resultBox.value = statusCode > 0 - semaphore.signal() - } - - task.resume() - _ = semaphore.wait(timeout: .now() + 2.5) - - if resultBox.value { - isAvailable = true - logger.info("MTranServer available via \(endpoint)") - break + if endpoint != "/translate" && [404, 405, 501].contains(statusCode) { + continue + } + return false + } catch is CancellationError { + return nil + } catch let error as URLError where error.code == .cancelled { + return nil + } catch { + logger.debug("MTranServer check \(endpoint) failed: \(error.localizedDescription)") + return false } } - - logger.info("MTranServer final availability: \(isAvailable)") - return isAvailable - } - - /// Reset the cached availability check - static func resetCache() { - lock.withLock { - _isAvailable = nil - } + return false } } diff --git a/TransFrame/Resources/en.lproj/Localizable.strings b/TransFrame/Resources/en.lproj/Localizable.strings index 201bd5a..49a649b 100644 --- a/TransFrame/Resources/en.lproj/Localizable.strings +++ b/TransFrame/Resources/en.lproj/Localizable.strings @@ -753,6 +753,7 @@ "textTranslation.error.cancelled" = "Translation was cancelled"; "textTranslation.error.serviceUnavailable" = "Translation service is unavailable"; "textTranslation.error.insertFailed" = "Failed to insert translated text"; +"textTranslation.error.insertFailed.copied" = "The translated text has been copied to the clipboard."; "textTranslation.recovery.emptyInput" = "Please select some text first"; "textTranslation.recovery.translationFailed" = "Please try again"; @@ -858,13 +859,14 @@ "preview.panel.translateTab" = "Translation"; "preview.panel.rerun" = "Recognize Again"; "preview.panel.copyAll" = "Copy All"; +"preview.panel.copySource" = "Copy Original"; +"preview.panel.copyTranslation" = "Copy Translation"; "preview.panel.close" = "Close"; "preview.panel.expandDrawer" = "Expand results panel"; "preview.panel.recognizing" = "Recognizing..."; "preview.panel.copyLine" = "Copy"; "preview.panel.original" = "Original"; "preview.panel.translation" = "Translation"; -"preview.panel.targetLanguage" = "Target Language"; "preview.panel.noResults" = "No results yet. Click Text Recognition to start"; "preview.panel.translating" = "Translating..."; "preview.panel.translate" = "Translate"; diff --git a/TransFrame/Resources/zh-Hans.lproj/Localizable.strings b/TransFrame/Resources/zh-Hans.lproj/Localizable.strings index 0d0b252..bde91a9 100644 --- a/TransFrame/Resources/zh-Hans.lproj/Localizable.strings +++ b/TransFrame/Resources/zh-Hans.lproj/Localizable.strings @@ -753,6 +753,7 @@ "textTranslation.error.cancelled" = "翻译已取消"; "textTranslation.error.serviceUnavailable" = "翻译服务不可用"; "textTranslation.error.insertFailed" = "插入翻译文本失败"; +"textTranslation.error.insertFailed.copied" = "译文已复制到剪贴板。"; "textTranslation.recovery.emptyInput" = "请先选择一些文本"; "textTranslation.recovery.translationFailed" = "请重试"; @@ -858,13 +859,14 @@ "preview.panel.translateTab" = "翻译"; "preview.panel.rerun" = "重新识别"; "preview.panel.copyAll" = "复制全部"; +"preview.panel.copySource" = "复制原文"; +"preview.panel.copyTranslation" = "复制译文"; "preview.panel.close" = "关闭"; "preview.panel.expandDrawer" = "展开结果面板"; "preview.panel.recognizing" = "正在识别..."; "preview.panel.copyLine" = "复制"; "preview.panel.original" = "原文"; "preview.panel.translation" = "译文"; -"preview.panel.targetLanguage" = "目标语言"; "preview.panel.noResults" = "暂无结果,点击「文本识别」开始"; "preview.panel.translating" = "正在翻译..."; "preview.panel.translate" = "翻译"; diff --git a/TransFrame/Services/AnalysisImageTiling.swift b/TransFrame/Services/AnalysisImageTiling.swift new file mode 100644 index 0000000..d02a1ed --- /dev/null +++ b/TransFrame/Services/AnalysisImageTiling.swift @@ -0,0 +1,85 @@ +import CoreGraphics +import Foundation + +/// 按原像素切片,避免 Retina 长图缩放到无法识别的小字。 +enum AnalysisImageTiler { + struct Tile { + let image: CGImage + let pixelRect: CGRect + } + + static func tiles( + for image: CGImage, + maxDimension: Int = AnalysisImageEncoder.maxDimension, + overlap: Int = 48 + ) -> [Tile] { + let limit = max(1, maxDimension) + let shouldTileWidth = image.width > limit && image.width * 2 > image.height + let shouldTileHeight = image.height > limit && image.height * 2 > image.width + let widthLimit = shouldTileWidth ? limit : image.width + let heightLimit = shouldTileHeight ? limit : image.height + let widthStep = max(1, widthLimit - max(0, min(overlap, widthLimit / 4))) + let heightStep = max(1, heightLimit - max(0, min(overlap, heightLimit / 4))) + let xs = origins(length: image.width, limit: widthLimit, step: widthStep) + 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) + } } + } + + static func merging(_ rects: [CGRect], results: [ScreenAnalysisResult]) throws -> ScreenAnalysisResult { + guard rects.count == results.count, !results.isEmpty, + let extent = rects.reduce(nil as CGRect?, { partial, rect in partial?.union(rect) ?? rect }), + extent.width > 0, extent.height > 0 else { + throw VLMProviderError.invalidResponse("Incomplete image tiling result") + } + var segments: [TextSegment] = [] + for (rect, result) in zip(rects, results) { + guard result.imageSize == rect.size else { + throw VLMProviderError.invalidResponse("Image tile dimensions do not match source pixels") + } + for segment in result.segments { + let box = segment.boundingBox + // 重叠区保留完整落在本块的结果;跨块文本交给包含它的完整块。 + guard box.minX >= 0, box.minY >= 0, box.maxX <= 1.0001, box.maxY <= 1.0001 else { + continue + } + let mappedBox = CGRect( + x: (rect.minX + box.minX * rect.width) / extent.width, + y: (rect.minY + box.minY * rect.height) / extent.height, + width: box.width * rect.width / extent.width, + height: box.height * rect.height / extent.height + ) + segments.append(TextSegment( + id: segment.id, text: segment.text, boundingBox: mappedBox, + confidence: segment.confidence, isUserEdited: segment.isUserEdited + )) + } + } + var deduplicated: [TextSegment] = [] + for segment in segments { + let text = segment.text.trimmingCharacters(in: .whitespacesAndNewlines) + let isDuplicate = deduplicated.contains { existing in + existing.text.trimmingCharacters(in: .whitespacesAndNewlines) == text + && overlaps(existing.boundingBox, segment.boundingBox) + } + if !isDuplicate { deduplicated.append(segment) } + } + return ScreenAnalysisResult(segments: deduplicated, rawSegments: segments, imageSize: extent.size) + } + + private static func overlaps(_ lhs: CGRect, _ rhs: CGRect) -> Bool { + let intersection = lhs.intersection(rhs) + guard !intersection.isNull else { return false } + let minArea = min(lhs.width * lhs.height, rhs.width * rhs.height) + return minArea > 0 && (intersection.width * intersection.height) / minArea >= 0.5 + } + + private static func origins(length: Int, limit: Int, step: Int) -> [Int] { + guard length > limit else { return [0] } + var origins = Array(stride(from: 0, to: length - limit, by: step)) + origins.append(length - limit) + return origins + } +} diff --git a/TransFrame/Services/ClaudeVLMProvider.swift b/TransFrame/Services/ClaudeVLMProvider.swift index d1fe46a..ae005d1 100644 --- a/TransFrame/Services/ClaudeVLMProvider.swift +++ b/TransFrame/Services/ClaudeVLMProvider.swift @@ -32,6 +32,7 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { private let timeout: TimeInterval private let logger = Logger.translation + private let session: URLSession // MARK: - Initialization @@ -39,9 +40,10 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { /// - Parameters: /// - configuration: VLM provider configuration /// - timeout: Request timeout in seconds (default: 60) - init(configuration: VLMProviderConfiguration, timeout: TimeInterval = 60) { + init(configuration: VLMProviderConfiguration, timeout: TimeInterval = 60, session: URLSession = .shared) { self.configuration = configuration self.timeout = timeout + self.session = session } /// Convenience initializer with individual parameters @@ -54,7 +56,8 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { apiKey: String, baseURL: URL = ClaudeVLMProvider.defaultBaseURL, modelName: String = ClaudeVLMProvider.defaultModel, - timeout: TimeInterval = 60 + timeout: TimeInterval = 60, + session: URLSession = .shared ) { self.configuration = VLMProviderConfiguration( apiKey: apiKey, @@ -62,6 +65,7 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { modelName: modelName ) self.timeout = timeout + self.session = session } private func logDebug(_ message: String) { @@ -207,10 +211,8 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { )) } - // Final deduplication before returning - let deduplicated = deduplicateSegments(allSegments) - logWarning("Max continuation attempts reached, \(allSegments.count) -> \(deduplicated.count) segments after dedup") - return VLMAnalysisResponse(segments: deduplicated) + logWarning("Max continuation attempts reached after \(allSegments.count) segments") + throw VLMProviderError.invalidResponse("VLM response remained truncated after \(maxAttempts) attempts") } /// Filters out segments from new array that already exist in existing array @@ -250,11 +252,6 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { return (content, isTruncated, claudeResponse.stopReason) } - /// Placeholder replacing the base64 image in continuation rounds, so the - /// same capture is not uploaded again for every truncation retry. - private static let continuationImagePlaceholder = - "(image omitted: the same image as in the first message)" - /// Builds request with custom messages and continuation settings private func buildRequest(messages: [ClaudeMessage], isContinuation: Bool, systemPrompt: String = VLMPromptTemplate.systemPrompt) throws -> URLRequest { let endpoint = configuration.baseURL.resolvingLocalhost.appendingPathComponent("v1/messages") @@ -269,23 +266,8 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { request.setValue(Self.apiVersion, forHTTPHeaderField: "anthropic-version") request.timeoutInterval = timeout - // Continuation rounds keep the conversation shape but drop the base64 - // image payload: the assistant's partial JSON already anchors the - // context, and re-uploading would multiply bandwidth and prompt tokens. - let effectiveMessages: [ClaudeMessage] - if isContinuation { - effectiveMessages = messages.map { message in - let blocks = message.content.map { block -> ClaudeContentBlock in - if case .image = block { - return .text(Self.continuationImagePlaceholder) - } - return block - } - return ClaudeMessage(role: message.role, content: blocks) - } - } else { - effectiveMessages = messages - } + // 每次 HTTP 请求彼此独立,续写轮必须保留原图上下文。 + let effectiveMessages = messages // Use higher max_tokens for continuation requests to minimize truncation let maxTokens = isContinuation ? 16384 : 8192 @@ -311,7 +293,7 @@ struct ClaudeVLMProvider: VLMProvider, Sendable { let (data, response): (Data, URLResponse) do { - (data, response) = try await URLSession.shared.data(for: request) + (data, response) = try await session.data(for: request) } catch let error as URLError { switch error.code { case .timedOut: diff --git a/TransFrame/Services/MTranServerEngine.swift b/TransFrame/Services/MTranServerEngine.swift index c16d508..6434c50 100644 --- a/TransFrame/Services/MTranServerEngine.swift +++ b/TransFrame/Services/MTranServerEngine.swift @@ -1,3 +1,4 @@ +import Darwin import Foundation import os.log @@ -8,10 +9,10 @@ actor MTranServerEngine: TranslationProvider { nonisolated let name = "MTransServer" var isAvailable: Bool { - get async { await checkConnection() } + configuration.isValid } - nonisolated var configuration: Configuration { .default } + nonisolated var configuration: Configuration { configuredSnapshot ?? .default } // MARK: - Properties @@ -21,7 +22,7 @@ actor MTranServerEngine: TranslationProvider { // MARK: - Configuration /// MTranServer configuration options - struct Configuration: Sendable { + struct Configuration: Sendable, Equatable { /// Server address (e.g., "localhost" or "192.168.1.100") var serverAddress: String @@ -34,6 +35,39 @@ actor MTranServerEngine: TranslationProvider { /// Whether to automatically detect source language var autoDetectSourceLanguage: Bool + init( + serverAddress: String, + serverPort: Int, + timeout: TimeInterval, + autoDetectSourceLanguage: Bool + ) { + self.serverAddress = serverAddress + self.serverPort = serverPort + self.timeout = timeout + self.autoDetectSourceLanguage = autoDetectSourceLanguage + } + + init?( + serverURL: String, + timeout: TimeInterval = 10.0, + autoDetectSourceLanguage: Bool = true + ) { + let trimmed = serverURL.trimmingCharacters(in: .whitespacesAndNewlines) + let endpoint = trimmed.contains("://") ? trimmed : "http://\(trimmed)" + guard let components = URLComponents(string: endpoint), + let host = components.percentEncodedHost, + components.path.isEmpty || components.path == "/", + components.query == nil, components.fragment == nil, + components.user == nil, components.password == nil else { return nil } + self.init( + serverAddress: host, + serverPort: components.port ?? 8989, + timeout: timeout, + autoDetectSourceLanguage: autoDetectSourceLanguage + ) + guard isValid else { return nil } + } + /// Default configuration from UserDefaults static var `default`: Configuration { // Read directly from UserDefaults to avoid MainActor isolation issues @@ -48,11 +82,67 @@ actor MTranServerEngine: TranslationProvider { autoDetectSourceLanguage: true ) } + + var endpointHost: String? { + let host = serverAddress.trimmingCharacters(in: .whitespacesAndNewlines) + guard !host.isEmpty, (1...65_535).contains(serverPort), + timeout.isFinite, timeout > 0 else { return nil } + if host.hasPrefix("[") || host.hasSuffix("]") { + guard host.hasPrefix("["), host.hasSuffix("]") else { return nil } + let rawAddress = String(host.dropFirst().dropLast()) + var parsed = in6_addr() + guard inet_pton(AF_INET6, rawAddress, &parsed) == 1 else { return nil } + return host + } + return host == "localhost" ? "127.0.0.1" : host + } + + var isValid: Bool { + guard let url = endpointURL(path: "/translate"), + url.host != nil, url.port == serverPort else { return false } + return true + } + + func endpointURL(path: String) -> URL? { + guard let host = endpointHost else { return nil } + var components = URLComponents() + components.scheme = "http" + components.percentEncodedHost = host + components.port = serverPort + components.path = path + return components.url + } } // MARK: - Initialization - private init() {} + private let configuredSnapshot: Configuration? + private let session: URLSession + private let checker: MTranServerChecker + + init( + configuration: Configuration? = nil, + session: URLSession = .shared, + checker: MTranServerChecker = .shared + ) { + self.configuredSnapshot = configuration + self.session = session + self.checker = checker + } + + static func provider( + config: TranslationEngineConfig, + session: URLSession = .shared, + checker: MTranServerChecker = .shared + ) -> MTranServerEngine { + let baseURL = config.options?.baseURL + let timeout = config.options?.timeout ?? 10.0 + guard let baseURL, !baseURL.isEmpty, + let parsed = Configuration(serverURL: baseURL, timeout: timeout) else { + return MTranServerEngine(configuration: .default, session: session, checker: checker) + } + return MTranServerEngine(configuration: parsed, session: session, checker: checker) + } // MARK: - Public API @@ -68,19 +158,16 @@ actor MTranServerEngine: TranslationProvider { _ text: String, from sourceLanguage: String? = nil, to targetLanguage: String, - config: Configuration = .default + config: Configuration? = nil ) async throws -> TranslationResult { + let config = config ?? configuration logger.info("Starting translation: \(text.count) chars to \(targetLanguage)") logger.info("Config: \(config.serverAddress):\(config.serverPort)") - // Reset cache to ensure we check with current settings - MTranServerChecker.resetCache() - guard await MTranServerChecker.checkAvailability() else { - logger.error("MTranServer not available") - throw MTranServerError.notAvailable - } + // Validate input before making a request. Health state never gates translation. + try Task.checkCancellation() + guard config.isValid else { throw MTranServerError.invalidURL } - // Validate input guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw MTranServerError.emptyInput } @@ -123,7 +210,7 @@ actor MTranServerEngine: TranslationProvider { /// - Returns: TranslationResult containing translated text /// - Throws: MTranServerError if translation fails func translate(_ text: String, to targetLanguage: String) async throws -> TranslationResult { - try await translate(text, from: nil, to: targetLanguage, config: .default) + try await translate(text, from: nil, to: targetLanguage, config: configuration) } // MARK: - TranslationProvider Protocol @@ -133,7 +220,7 @@ actor MTranServerEngine: TranslationProvider { from sourceLanguage: String?, to targetLanguage: String ) async throws -> TranslationResult { - try await translate(text, from: sourceLanguage, to: targetLanguage, config: .default) + try await translate(text, from: sourceLanguage, to: targetLanguage, config: configuration) } func translate( @@ -146,12 +233,13 @@ actor MTranServerEngine: TranslationProvider { var results: [TranslationResult] = [] results.reserveCapacity(texts.count) + let config = configuration for text in texts { let result = try await translate( text, from: sourceLanguage, to: targetLanguage, - config: .default + config: config ) results.append(result) } @@ -160,12 +248,27 @@ actor MTranServerEngine: TranslationProvider { } func verifyConnection() async throws { - MTranServerChecker.resetCache() - guard await MTranServerChecker.checkAvailability() else { + try await verifyConnection(config: configuration) + } + + func verifyConnection(config: Configuration) async throws { + guard config.isValid else { throw MTranServerError.invalidURL } + try Task.checkCancellation() + let isAvailable = try await checker.checkAvailability( + configuration: config, + forceRefresh: true + ) + guard isAvailable else { throw TranslationProviderError.connectionFailed("MTranServer is not reachable") } } + func checkConnection(config: Configuration) async throws -> Bool { + guard config.isValid else { throw MTranServerError.invalidURL } + try Task.checkCancellation() + return try await checker.checkAvailability(configuration: config, forceRefresh: true) + } + // MARK: - Private Methods /// Resolves the effective source language for translation @@ -206,10 +309,7 @@ actor MTranServerEngine: TranslationProvider { /// Builds the URL for MTranServer API endpoint private func buildURL(config: Configuration) throws -> URL { - // Normalize localhost to 127.0.0.1 to avoid IPv6 resolution issues - let host = config.serverAddress == "localhost" ? "127.0.0.1" : config.serverAddress - let urlString = "http://\(host):\(config.serverPort)/translate" - guard let url = URL(string: urlString) else { + guard let url = config.endpointURL(path: "/translate") else { throw MTranServerError.invalidURL } return url @@ -235,71 +335,35 @@ actor MTranServerEngine: TranslationProvider { jsonData: Data, timeout: TimeInterval ) async throws -> TranslationResponse { - try await withThrowingTaskGroup(of: Result.self) { group in - // Translation task - group.addTask { [jsonData, url] in - await self.executeTranslationRequest(url: url, jsonData: jsonData) - } + var request = URLRequest(url: url) + request.httpMethod = "POST" + request.timeoutInterval = timeout + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = jsonData - // Timeout task - _ = group.addTaskUnlessCancelled { [timeout] in - try await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000)) - return .failure(MTranServerError.timeout) - } - - // Wait for first completed task - guard let result = try await group.next() else { - throw MTranServerError.timeout - } - group.cancelAll() - return try result.get() - } - } - - /// Executes the HTTP request to MTranServer - private func executeTranslationRequest( - url: URL, - jsonData: Data - ) async -> Result { do { - var request = URLRequest(url: url) - request.httpMethod = "POST" - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - request.httpBody = jsonData - - // Debug: log request body - if let jsonString = String(data: jsonData, encoding: .utf8) { - logger.debug("Request body: \(jsonString)") - } - - let (data, response) = try await URLSession.shared.data(for: request) - - // Debug: log response - if let responseString = String(data: data, encoding: .utf8) { - logger.debug("Response body: \(responseString)") - } - + let (data, response) = try await session.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { - logger.error("Invalid response type") - return .failure(MTranServerError.invalidResponse) + throw MTranServerError.invalidResponse } - - logger.debug("Response status code: \(httpResponse.statusCode)") - guard httpResponse.statusCode == 200 else { if httpResponse.statusCode == 503 { - return .failure(MTranServerError.serviceUnavailable) + throw MTranServerError.serviceUnavailable } - return .failure(MTranServerError.httpError(statusCode: httpResponse.statusCode)) + throw MTranServerError.httpError(statusCode: httpResponse.statusCode) } - - let decoded = try JSONDecoder().decode(TranslationResponse.self, from: data) - return .success(decoded) + return try JSONDecoder().decode(TranslationResponse.self, from: data) + } catch is CancellationError { + throw CancellationError() + } catch let error as URLError where error.code == .cancelled { + throw CancellationError() + } catch let error as URLError where error.code == .timedOut { + throw MTranServerError.timeout } catch let error as MTranServerError { - return .failure(error) + throw error } catch { logger.error("Request failed: \(error)") - return .failure(MTranServerError.requestFailed(underlying: error)) + throw MTranServerError.requestFailed(underlying: error) } } } diff --git a/TransFrame/Services/OpenAIVLMProvider.swift b/TransFrame/Services/OpenAIVLMProvider.swift index 2e5fda0..7cf2263 100644 --- a/TransFrame/Services/OpenAIVLMProvider.swift +++ b/TransFrame/Services/OpenAIVLMProvider.swift @@ -29,6 +29,7 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { private let timeout: TimeInterval let apiMode: OpenAIAPIMode + private let session: URLSession private let logger = Logger.translation /// Logger for the stateless response parser, usable without an instance. private static let parserLogger = Logger.translation @@ -40,10 +41,11 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { /// - configuration: VLM provider configuration /// - apiMode: OpenAI API protocol mode (.chat or .response) /// - timeout: Request timeout in seconds (default: 60) - init(configuration: VLMProviderConfiguration, apiMode: OpenAIAPIMode = .chat, timeout: TimeInterval = 60) { + init(configuration: VLMProviderConfiguration, apiMode: OpenAIAPIMode = .chat, timeout: TimeInterval = 60, session: URLSession = .shared) { self.configuration = configuration self.apiMode = apiMode self.timeout = timeout + self.session = session } private func logDebug(_ message: String) { @@ -74,7 +76,8 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { baseURL: URL = OpenAIVLMProvider.defaultBaseURL, modelName: String = OpenAIVLMProvider.defaultModel, apiMode: OpenAIAPIMode = .chat, - timeout: TimeInterval = 60 + timeout: TimeInterval = 60, + session: URLSession = .shared ) { self.configuration = VLMProviderConfiguration( apiKey: apiKey, @@ -83,6 +86,7 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { ) self.apiMode = apiMode self.timeout = timeout + self.session = session } // MARK: - VLMProvider Protocol @@ -252,10 +256,8 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { )) } - // Final deduplication before returning - let deduplicated = deduplicateSegments(allSegments) - logWarning("Max continuation attempts reached, \(allSegments.count) -> \(deduplicated.count) segments after dedup") - return VLMAnalysisResponse(segments: deduplicated) + logWarning("Max continuation attempts reached after \(allSegments.count) segments") + throw VLMProviderError.invalidResponse("VLM response remained truncated after \(maxAttempts) attempts") } /// Filters out segments from new array that already exist in existing array @@ -442,11 +444,6 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { // MARK: - Private Methods - /// Placeholder replacing the base64 image in continuation rounds, so the - /// same capture is not uploaded again for every truncation retry. - private static let continuationImagePlaceholder = - "(image omitted: the same image as in the first message)" - /// Builds the URLRequest for OpenAI Chat Completions or Responses API with custom messages private func buildRequest( messages: [OpenAIChatMessage], @@ -469,25 +466,8 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { } request.timeoutInterval = timeout - // Continuation rounds keep the conversation shape but drop only the - // base64 image part — the text instruction stays so the model still - // knows the task (mirrors ClaudeVLMProvider's per-block replacement). - let effectiveMessages: [OpenAIChatMessage] - if isContinuation { - effectiveMessages = messages.map { message in - guard case .vision(let parts) = message.content else { return message } - var kept: [OpenAIChatMessage.VisionContent] = parts.compactMap { part in - if case .text(let text) = part { return .text(text) } - return nil - } - if kept.isEmpty { - kept = [.text(Self.continuationImagePlaceholder)] - } - return OpenAIChatMessage(role: message.role, content: .vision(kept)) - } - } else { - effectiveMessages = messages - } + // 每次 HTTP 请求彼此独立,没有会话状态;续写必须保留原图上下文。 + let effectiveMessages = messages // Responses API if apiMode == .response { @@ -553,7 +533,7 @@ struct OpenAIVLMProvider: VLMProvider, Sendable { } do { - (data, response) = try await URLSession.shared.data(for: request) + (data, response) = try await session.data(for: request) } catch let error as URLError { logWarning("Network error: \(error.localizedDescription)") switch error.code { diff --git a/TransFrame/Services/ScreenCoderEngine.swift b/TransFrame/Services/ScreenCoderEngine.swift index 4b79cc7..ed7b3e9 100644 --- a/TransFrame/Services/ScreenCoderEngine.swift +++ b/TransFrame/Services/ScreenCoderEngine.swift @@ -63,13 +63,27 @@ actor ScreenCoderEngine { /// - Returns: ScreenAnalysisResult containing extracted text segments with positions /// - Throws: ScreenCoderEngineError or VLMProviderError if analysis fails func analyze(image: CGImage) async throws -> ScreenAnalysisResult { + try await analyze(image: image, prompt: .ocr) + } + + func analyze(image: CGImage, prompt: VLMPromptPair) async throws -> ScreenAnalysisResult { let provider = try await currentProvider() - + guard await provider.isAvailable else { throw ScreenCoderEngineError.providerNotAvailable(provider.name) } - - return try await provider.analyze(image: image) + + let tiles = AnalysisImageTiler.tiles(for: image) + guard tiles.count > 1 else { + return try await provider.analyze(image: image, prompt: prompt) + } + var results: [ScreenAnalysisResult] = [] + results.reserveCapacity(tiles.count) + for tile in tiles { + try Task.checkCancellation() + results.append(try await provider.analyze(image: tile.image, prompt: prompt)) + } + return try AnalysisImageTiler.merging(tiles.map(\.pixelRect), results: results) } /// Returns the currently configured VLM provider. diff --git a/TransFrame/Services/SmartPrivacyRedactionService.swift b/TransFrame/Services/SmartPrivacyRedactionService.swift index b2a88a4..f0b7f65 100644 --- a/TransFrame/Services/SmartPrivacyRedactionService.swift +++ b/TransFrame/Services/SmartPrivacyRedactionService.swift @@ -52,7 +52,7 @@ final class SmartPrivacyRedactionService: Sendable { } logger.info("Smart privacy detection started via \(provider.name)") - let result = try await provider.analyze(image: image, prompt: .privacy) + let result = try await engine.analyze(image: image, prompt: .privacy) let width = CGFloat(image.width) let height = CGFloat(image.height) diff --git a/TransFrame/Services/TextInsertService.swift b/TransFrame/Services/TextInsertService.swift index 75c2ad6..6cc4727 100644 --- a/TransFrame/Services/TextInsertService.swift +++ b/TransFrame/Services/TextInsertService.swift @@ -25,6 +25,12 @@ actor TextInsertService { case accessibilityPermissionDenied /// Text contains characters that cannot be typed case unsupportedCharacters(String) + /// 翻译期间原控件或原选区已变化。 + case targetChanged + /// 未捕获到可安全替换的 AX 文本选区。 + case targetUnavailable + /// AX 写入后无法确认目标内容,绝不自动重复输入。 + case insertionNotVerified var errorDescription: String? { switch self { @@ -46,6 +52,24 @@ actor TextInsertService { value: "Cannot type characters: \(chars)", comment: "" ) + case .targetChanged: + return NSLocalizedString( + "error.text.insert.target.changed", + value: "The original selection or focused app changed. The translation was not inserted.", + comment: "" + ) + case .targetUnavailable: + return NSLocalizedString( + "error.text.insert.target.unavailable", + value: "Cannot safely replace this selection. The translation was not inserted.", + comment: "" + ) + case .insertionNotVerified: + return NSLocalizedString( + "error.text.insert.not.verified", + value: "Text insertion could not be verified. Check the original app before retrying.", + comment: "" + ) } } @@ -69,6 +93,24 @@ actor TextInsertService { value: "Some characters cannot be typed with keyboard simulation", comment: "" ) + case .targetChanged: + return NSLocalizedString( + "error.text.insert.target.changed.recovery", + value: "Select the text again to translate and insert", + comment: "" + ) + case .targetUnavailable: + return NSLocalizedString( + "error.text.insert.target.unavailable.recovery", + value: "Copy the translation manually or select an editable text field", + comment: "" + ) + case .insertionNotVerified: + return NSLocalizedString( + "error.text.insert.not.verified.recovery", + value: "Do not retry blindly; check whether some text was already inserted", + comment: "" + ) } } } @@ -110,6 +152,13 @@ actor TextInsertService { } } + /// 只替换捕获时的 AX 目标。nil 表示无法核验,禁止回退键盘输入。 + @MainActor + func replaceSelection(with text: String, captured: CapturedTextInsertion?) throws { + guard let captured else { throw InsertError.targetUnavailable } + try captured.replace(with: text) + } + /// Deletes the currently selected text and inserts new text via Unicode events. /// This bypasses the input method and inserts text directly. /// - Parameter text: The text to insert after deleting the selection diff --git a/TransFrame/Services/TextInsertionTarget.swift b/TransFrame/Services/TextInsertionTarget.swift new file mode 100644 index 0000000..660fc0a --- /dev/null +++ b/TransFrame/Services/TextInsertionTarget.swift @@ -0,0 +1,149 @@ +import AppKit +import ApplicationServices + +struct TextInsertionSnapshot: Equatable, Sendable { + var value: String + var range: NSRange + + var selectedText: String? { + let length = (value as NSString).length + guard range.location >= 0, range.length > 0, + range.location <= length, range.length <= length - range.location else { return nil } + return (value as NSString).substring(with: range) + } +} + +@MainActor +protocol TextInsertionTarget: AnyObject { + var isFocused: Bool { get } + func snapshot() throws -> TextInsertionSnapshot + func replaceSelectedText(_ text: String) throws +} + +/// AX 引用仅在主线程使用;快照在捕获时确定,禁止翻译结束后重新选择目标。 +@MainActor +final class CapturedTextInsertion { + let selectedText: String + private let target: any TextInsertionTarget + private let selection: TextInsertionSnapshot + private let expectedClipboardText: String + + init( + target: any TextInsertionTarget, + selection: TextInsertionSnapshot, + selectedText: String, + expectedClipboardText: String? = nil + ) throws { + guard selection.selectedText == selectedText, !selectedText.isEmpty else { + throw TextInsertService.InsertError.targetUnavailable + } + self.target = target + self.selection = selection + self.selectedText = selectedText + self.expectedClipboardText = expectedClipboardText ?? selectedText + } + + func matching(clipboardText: String) -> CapturedTextInsertion? { + try? CapturedTextInsertion( + target: target, selection: selection, + selectedText: selectedText, expectedClipboardText: clipboardText + ) + } + + func replace(with text: String) throws { + try Task.checkCancellation() + guard !text.isEmpty else { throw TextInsertService.InsertError.insertionNotVerified } + guard selectedText == expectedClipboardText, + target.isFocused, try target.snapshot() == selection else { + throw TextInsertService.InsertError.targetChanged + } + let expected = (selection.value as NSString).replacingCharacters(in: selection.range, with: text) + // 单次 AX 替换不单独发送 Delete;失败也可能已生效,必须读回后再判断。 + do { + try target.replaceSelectedText(text) + } catch { + guard let after = try? target.snapshot(), after.value == expected else { throw error } + return + } + guard try target.snapshot().value == expected else { + throw TextInsertService.InsertError.insertionNotVerified + } + } +} + +@MainActor +final class AXTextInsertionTarget: TextInsertionTarget { + private let element: AXUIElement + private let processID: pid_t + + private init(element: AXUIElement, processID: pid_t) { + self.element = element + self.processID = processID + } + + static func capture() -> CapturedTextInsertion? { + guard AXIsProcessTrusted(), let element = focusedElement(), + let application = NSWorkspace.shared.frontmostApplication else { return nil } + var pid: pid_t = 0 + guard AXUIElementGetPid(element, &pid) == .success, + pid == application.processIdentifier else { return nil } + let target = AXTextInsertionTarget(element: element, processID: pid) + guard let selection = try? target.snapshot(), let text = selection.selectedText, + target.isFocused else { return nil } + return try? CapturedTextInsertion(target: target, selection: selection, selectedText: text) + } + + var isFocused: Bool { + guard NSWorkspace.shared.frontmostApplication?.processIdentifier == processID, + let focused = Self.focusedElement() else { return false } + return CFEqual(focused, element) + } + + func snapshot() throws -> TextInsertionSnapshot { + guard let value = attribute(kAXValueAttribute) as? String, + let rangeObject = attribute(kAXSelectedTextRangeAttribute), + CFGetTypeID(rangeObject) == AXValueGetTypeID() else { + throw TextInsertService.InsertError.targetUnavailable + } + let rangeValue = unsafeDowncast(rangeObject, to: AXValue.self) + var range = CFRange() + guard AXValueGetType(rangeValue) == .cfRange, + AXValueGetValue(rangeValue, .cfRange, &range) else { + throw TextInsertService.InsertError.targetUnavailable + } + // 不支持多个不连续选区,以免替换范围与快照不同。 + if let ranges = attribute(kAXSelectedTextRangesAttribute) as? [AnyObject], ranges.count > 1 { + throw TextInsertService.InsertError.targetUnavailable + } + let snapshot = TextInsertionSnapshot(value: value, range: NSRange(location: range.location, length: range.length)) + if range.length > 0, let selected = attribute(kAXSelectedTextAttribute) as? String, + snapshot.selectedText != selected { + throw TextInsertService.InsertError.targetUnavailable + } + return snapshot + } + + func replaceSelectedText(_ text: String) throws { + guard AXIsProcessTrusted() else { throw TextInsertService.InsertError.accessibilityPermissionDenied } + var settable = DarwinBoolean(false) + guard AXUIElementIsAttributeSettable(element, kAXSelectedTextAttribute as CFString, &settable) == .success, + settable.boolValue else { throw TextInsertService.InsertError.targetUnavailable } + guard AXUIElementSetAttributeValue(element, kAXSelectedTextAttribute as CFString, text as CFString) == .success else { + throw TextInsertService.InsertError.insertionNotVerified + } + } + + private func attribute(_ name: String) -> AnyObject? { + var value: CFTypeRef? + guard AXUIElementCopyAttributeValue(element, name as CFString, &value) == .success else { return nil } + return value + } + + private static func focusedElement() -> AXUIElement? { + var value: CFTypeRef? + guard AXUIElementCopyAttributeValue( + AXUIElementCreateSystemWide(), kAXFocusedUIElementAttribute as CFString, &value + ) == .success, let value, CFGetTypeID(value) == AXUIElementGetTypeID() else { return nil } + return unsafeDowncast(value, to: AXUIElement.self) + } +} diff --git a/TransFrame/Services/TextSelectionService.swift b/TransFrame/Services/TextSelectionService.swift index bc098dc..79fc5b5 100644 --- a/TransFrame/Services/TextSelectionService.swift +++ b/TransFrame/Services/TextSelectionService.swift @@ -5,13 +5,15 @@ import ApplicationServices import os /// Result of text selection capture -struct TextSelectionResult: Sendable { +struct TextSelectionResult { /// The captured selected text let text: String /// The name of the source application (if available) let sourceApplication: String? /// The bundle identifier of the source application (if available) let sourceBundleIdentifier: String? + /// 仅在 translate-and-insert 中绑定;普通取词翻译不需要 AX 选区。 + @MainActor var insertionTarget: CapturedTextInsertion? } /// Service for capturing selected text from any application. @@ -142,6 +144,10 @@ actor TextSelectionService { /// - Returns: A TextSelectionResult containing the selected text and source application info /// - Throws: CaptureError if the capture fails func captureSelectedText() async throws -> TextSelectionResult { + try await captureSelectedText(bindForInsertion: false) + } + + func captureSelectedText(bindForInsertion: Bool) async throws -> TextSelectionResult { // Check accessibility permission guard AXIsProcessTrusted() else { throw CaptureError.accessibilityPermissionDenied @@ -149,6 +155,7 @@ actor TextSelectionService { // Get source application info before capturing let sourceAppInfo = getActiveApplicationInfo() + let insertionTarget = bindForInsertion ? await AXTextInsertionTarget.capture() : nil // Save current clipboard content let savedClipboard = try saveClipboardContent() @@ -177,11 +184,15 @@ actor TextSelectionService { guard !capturedText.isEmpty else { throw CaptureError.noSelection } + let verifiedTarget = await MainActor.run { + insertionTarget?.matching(clipboardText: capturedText) + } return TextSelectionResult( text: capturedText, sourceApplication: sourceAppInfo.name, - sourceBundleIdentifier: sourceAppInfo.bundleIdentifier + sourceBundleIdentifier: sourceAppInfo.bundleIdentifier, + insertionTarget: verifiedTarget ) } diff --git a/TransFrame/Services/Translation/LLMTranslationBatching.swift b/TransFrame/Services/Translation/LLMTranslationBatching.swift new file mode 100644 index 0000000..91a4a0e --- /dev/null +++ b/TransFrame/Services/Translation/LLMTranslationBatching.swift @@ -0,0 +1,51 @@ +import Foundation + +/// 在达到输出预算前拆批,长片段按字符而非空格切断并保留顺序。 +enum LLMTranslationBatching { + static let separator = "\n---\n" + + static func split(texts: [String], maxOutputTokens: Int, promptTemplate: String?) -> [[String]] { + let budget = max(1, maxOutputTokens) + let fixedPrompt = promptTemplate?.replacingOccurrences(of: "{text}", with: "") ?? "" + let sourceBudget = max(4, budget - estimatedTokens(for: fixedPrompt)) + var batches: [[String]] = [] + var current: [String] = [] + var used = 0 + + for text in texts { + let pieces = estimatedTokens(for: text) > budget + ? splitLongText(text, budget: sourceBudget) : [text] + for piece in pieces { + let cost = piece.utf16.count + (current.isEmpty ? 0 : separator.utf16.count) + if !current.isEmpty, used + cost > sourceBudget { + batches.append(current) + current = [] + used = 0 + } + used += piece.utf16.count + (current.isEmpty ? 0 : separator.utf16.count) + current.append(piece) + } + } + if !current.isEmpty { batches.append(current) } + return batches + } + + private static func estimatedTokens(for text: String) -> Int { + text.utf16.count + } + + private static func splitLongText(_ text: String, budget: Int) -> [String] { + guard text.utf16.count > budget else { return [text] } + var pieces: [String] = [] + var current = "" + for character in text { + if !current.isEmpty, current.utf16.count + character.utf16.count > budget { + pieces.append(current) + current = "" + } + current.append(character) + } + if !current.isEmpty { pieces.append(current) } + return pieces + } +} diff --git a/TransFrame/Services/Translation/LLMTranslationResponseParser.swift b/TransFrame/Services/Translation/LLMTranslationResponseParser.swift new file mode 100644 index 0000000..fc5da05 --- /dev/null +++ b/TransFrame/Services/Translation/LLMTranslationResponseParser.swift @@ -0,0 +1,86 @@ +import Foundation + +/// OpenAI-compatible 和 Claude 文本响应的共同完成状态检查。 +enum LLMTranslationResponseParser { + static func extractText(from data: Data, for engine: TranslationEngineType) throws -> String { + guard let rawString = String(data: data, encoding: .utf8) else { + throw TranslationProviderError.translationFailed("Unable to decode data to UTF-8 string") + } + let trimmed = rawString.trimmingCharacters(in: .whitespacesAndNewlines) + if trimmed.hasPrefix("data:") || trimmed.contains("\ndata:") { + guard engine != .claude else { + return try extractClaudeSSE(from: trimmed) + } + return try extractOpenAISSE(from: trimmed) + } + guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] else { + throw TranslationProviderError.translationFailed("Response JSON is not a dictionary") + } + if let error = json["error"] as? [String: Any], let message = error["message"] as? String { + throw TranslationProviderError.translationFailed("API error: \(message)") + } + if engine == .claude { + guard json["stop_reason"] as? String != "max_tokens" else { + throw TranslationProviderError.incompleteResponse("Claude reached the output token limit") + } + guard let content = (json["content"] as? [[String: Any]])? + .first(where: { $0["type"] as? String == "text" })?["text"] as? String else { + throw TranslationProviderError.translationFailed("Unexpected JSON response structure") + } + return content.trimmingCharacters(in: .whitespacesAndNewlines) + } + guard let choice = (json["choices"] as? [[String: Any]])?.first else { + throw TranslationProviderError.translationFailed("Unexpected JSON response structure") + } + guard choice["finish_reason"] as? String != "length", + choice["finish_reason"] as? String != "max_tokens" else { + throw TranslationProviderError.incompleteResponse("The response reached the output token limit") + } + guard let content = (choice["message"] as? [String: Any])?["content"] as? String else { + throw TranslationProviderError.translationFailed("Unexpected JSON response structure") + } + return content.trimmingCharacters(in: .whitespacesAndNewlines) + } + + private static func extractOpenAISSE(from text: String) throws -> String { + var result = "" + var isTruncated = false + for line in text.components(separatedBy: .newlines) { + let line = line.trimmingCharacters(in: .whitespacesAndNewlines) + guard line.hasPrefix("data:"), line != "data: [DONE]", + let data = line.dropFirst(5).trimmingCharacters(in: .whitespaces).data(using: .utf8), + let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue } + if let choice = (json["choices"] as? [[String: Any]])?.first { + let reason = choice["finish_reason"] as? String + isTruncated = isTruncated || reason == "length" || reason == "max_tokens" + if let delta = choice["delta"] as? [String: Any], let content = delta["content"] as? String { + result += content + } else if let content = choice["text"] as? String { + result += content + } + } + } + guard !isTruncated else { throw TranslationProviderError.incompleteResponse("The stream reached the output token limit") } + guard !result.isEmpty else { throw TranslationProviderError.translationFailed("Empty text from SSE stream") } + return result.trimmingCharacters(in: .whitespacesAndNewlines) + } + + private static func extractClaudeSSE(from text: String) throws -> String { + var result = "" + var isTruncated = false + for line in text.components(separatedBy: .newlines) { + let line = line.trimmingCharacters(in: .whitespacesAndNewlines) + guard line.hasPrefix("data:"), + let data = line.dropFirst(5).trimmingCharacters(in: .whitespaces).data(using: .utf8), + let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue } + if json["stop_reason"] as? String == "max_tokens" { isTruncated = true } + if let delta = json["delta"] as? [String: Any] { + if delta["stop_reason"] as? String == "max_tokens" { isTruncated = true } + if let content = delta["text"] as? String { result += content } + } + } + guard !isTruncated else { throw TranslationProviderError.incompleteResponse("Claude stream reached the output token limit") } + guard !result.isEmpty else { throw TranslationProviderError.translationFailed("Empty text from SSE stream") } + return result.trimmingCharacters(in: .whitespacesAndNewlines) + } +} diff --git a/TransFrame/Services/Translation/Providers/CompatibleTranslationProvider.swift b/TransFrame/Services/Translation/Providers/CompatibleTranslationProvider.swift index b4d51e8..f6351f0 100644 --- a/TransFrame/Services/Translation/Providers/CompatibleTranslationProvider.swift +++ b/TransFrame/Services/Translation/Providers/CompatibleTranslationProvider.swift @@ -15,11 +15,13 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi nonisolated let id: String nonisolated let name: String nonisolated let configHash: Int + nonisolated let configurationSnapshot: ConfigurationSnapshot private let config: TranslationEngineConfig private let compatibleConfig: CompatibleConfig private let promptConfigID: String? private let keychain: KeychainService + private let session: URLSession private let logger = Logger( subsystem: Bundle.main.bundleIdentifier ?? "TransFrame", category: "CompatibleTranslationProvider" @@ -27,6 +29,12 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi // MARK: - Configuration + struct ConfigurationSnapshot: Equatable, Sendable { + let engineConfig: TranslationEngineConfig + let compatibleConfig: CompatibleConfig + let promptConfigID: String? + } + struct CompatibleConfig: Codable, Equatable, Sendable, Identifiable { var id: UUID var displayName: String @@ -50,6 +58,7 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi static var `default`: CompatibleConfig { CompatibleConfig( + id: UUID(uuidString: "00000000-0000-0000-0000-000000000000")!, displayName: "Custom", baseURL: "http://localhost:8000/v1", modelName: "default", @@ -63,6 +72,8 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi var configHash: Int { var hasher = Hasher() + hasher.combine(id) + hasher.combine(displayName) hasher.combine(baseURL) hasher.combine(modelName) hasher.combine(hasAPIKey) @@ -70,17 +81,10 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi } } - // MARK: - Initialization - - init(config: TranslationEngineConfig, keychain: KeychainService) async throws { - self.config = config - self.keychain = keychain - - let savedConfigs: [CompatibleConfig] = await MainActor.run { - AppSettings.shared.compatibleProviderConfigs - } - - // Parse compatible config from customName or fallback to AppSettings authoritative store + static func resolveConfigurationSnapshot( + from config: TranslationEngineConfig, + savedConfigs: [CompatibleConfig] + ) -> ConfigurationSnapshot { let resolvedCompatibleConfig: CompatibleConfig if let customName = config.customName, let jsonData = customName.data(using: .utf8), @@ -100,26 +104,53 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi let resolvedPromptConfigID = savedConfigs.contains(where: { $0.id == resolvedCompatibleConfig.id }) ? resolvedCompatibleConfig.id.uuidString : nil - self.compatibleConfig = resolvedCompatibleConfig - self.promptConfigID = resolvedPromptConfigID - self.id = "custom" - self.name = self.compatibleConfig.displayName - self.configHash = self.compatibleConfig.configHash + return ConfigurationSnapshot( + engineConfig: config, + compatibleConfig: resolvedCompatibleConfig, + promptConfigID: resolvedPromptConfigID + ) + } + + // MARK: - Initialization + + init(config: TranslationEngineConfig, keychain: KeychainService) async throws { + let savedConfigs: [CompatibleConfig] = await MainActor.run { + AppSettings.shared.compatibleProviderConfigs + } + let snapshot = Self.resolveConfigurationSnapshot(from: config, savedConfigs: savedConfigs) + self.init(snapshot: snapshot, keychain: keychain, providerID: "custom") } init( config: TranslationEngineConfig, compatibleConfig: CompatibleConfig, - keychain: KeychainService + keychain: KeychainService, + session: URLSession = .shared ) async throws { - self.config = config - self.compatibleConfig = compatibleConfig - self.promptConfigID = compatibleConfig.id.uuidString + let snapshot = ConfigurationSnapshot( + engineConfig: config, + compatibleConfig: compatibleConfig, + promptConfigID: compatibleConfig.id.uuidString + ) + self.init(snapshot: snapshot, keychain: keychain, providerID: compatibleConfig.keychainId, session: session) + } + + init( + snapshot: ConfigurationSnapshot, + keychain: KeychainService, + providerID: String, + session: URLSession = .shared + ) { + self.config = snapshot.engineConfig + self.compatibleConfig = snapshot.compatibleConfig + self.promptConfigID = snapshot.promptConfigID self.keychain = keychain - self.id = compatibleConfig.keychainId - self.name = compatibleConfig.displayName - self.configHash = compatibleConfig.configHash + self.session = session + self.id = providerID + self.name = snapshot.compatibleConfig.displayName + self.configHash = snapshot.compatibleConfig.configHash + self.configurationSnapshot = snapshot } // MARK: - TranslationProvider Protocol @@ -168,8 +199,25 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi ) async throws -> [TranslationResult] { guard !texts.isEmpty else { return [] } - // Combine texts for efficiency - let combinedText = texts.joined(separator: "\n---\n") + let batches = LLMTranslationBatching.split( + texts: texts, maxOutputTokens: config.options?.maxTokens ?? 2048, promptTemplate: promptTemplate + ) + guard batches.count == 1, batches[0] == texts else { + var results: [TranslationResult] = [] + for batch in batches { + results.append(contentsOf: try await translateSingleBatch( + batch, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate + )) + } + return results + } + return try await translateSingleBatch(texts, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate) + } + + private func translateSingleBatch( + _ texts: [String], from sourceLanguage: String?, to targetLanguage: String, promptTemplate: String? + ) async throws -> [TranslationResult] { + let combinedText = texts.joined(separator: LLMTranslationBatching.separator) let combinedResult = try await translate( text: combinedText, from: sourceLanguage, @@ -177,7 +225,7 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi promptTemplate: promptTemplate ) - let translatedTexts = combinedResult.translatedText.components(separatedBy: "\n---\n") + let translatedTexts = combinedResult.translatedText.components(separatedBy: LLMTranslationBatching.separator) if translatedTexts.count == texts.count { return zip(texts, translatedTexts).map { source, translated in @@ -189,6 +237,9 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi ) } } + guard texts.count > 1 else { + return [combinedResult] + } // Split failed - translate individually to ensure correct mapping logger.warning("Batch split failed, falling back to individual translations") @@ -345,7 +396,7 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi } logger.info("Sending request to \(apiURL.absoluteString) with headers: \(safeHeaders)") } - let (data, response) = try await URLSession.shared.data(for: request) + let (data, response) = try await session.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { throw TranslationProviderError.connectionFailed("Invalid response") @@ -368,89 +419,9 @@ actor CompatibleTranslationProvider: TranslationProvider, TranslationPromptConfi } private func parseResponse(_ data: Data) throws -> String { - guard let rawString = String(data: data, encoding: .utf8) else { - throw TranslationProviderError.translationFailed("Unable to decode data to UTF-8 string") - } - - let trimmedResponse = rawString.trimmingCharacters(in: .whitespacesAndNewlines) - - // Check if response is Server-Sent Events (SSE) stream format - if trimmedResponse.hasPrefix("data:") || trimmedResponse.contains("\ndata:") { - return try parseSSEStream(trimmedResponse) - } - - let jsonObject: Any - do { - jsonObject = try JSONSerialization.jsonObject(with: data) - } catch { - logger.error("JSON serialization failed: \(error.localizedDescription). Response size: \(data.count) bytes") - throw error - } - - guard let json = jsonObject as? [String: Any] else { - logger.error("Response JSON is not a dictionary object") - throw TranslationProviderError.translationFailed("Response JSON is not a dictionary") - } - - // Handle error responses from OpenAI compatible APIs - if let errorObj = json["error"] as? [String: Any], - let errorMessage = errorObj["message"] as? String { - logger.error("API returned error: \(errorMessage)") - throw TranslationProviderError.translationFailed("API error: \(errorMessage)") - } - - guard let choices = json["choices"] as? [[String: Any]], - let firstChoice = choices.first, - let message = firstChoice["message"] as? [String: Any], - let content = message["content"] as? String else { - logger.error("Unexpected JSON response structure (missing choices or content)") - throw TranslationProviderError.translationFailed("Unexpected JSON response structure") - } - - return content.trimmingCharacters(in: .whitespacesAndNewlines) + try LLMTranslationResponseParser.extractText(from: data, for: .custom) } - private func parseSSEStream(_ streamText: String) throws -> String { - var resultText = "" - let lines = streamText.components(separatedBy: .newlines) - - for line in lines { - let trimmedLine = line.trimmingCharacters(in: .whitespacesAndNewlines) - guard !trimmedLine.isEmpty else { continue } - - // Skip stream end marker - if trimmedLine == "data: [DONE]" { - continue - } - - if trimmedLine.hasPrefix("data:") { - let jsonText = trimmedLine.dropFirst(5).trimmingCharacters(in: .whitespaces) - guard !jsonText.isEmpty else { continue } - - guard let jsonData = jsonText.data(using: .utf8), - let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] else { - continue - } - - if let choices = json["choices"] as? [[String: Any]], - let firstChoice = choices.first { - if let delta = firstChoice["delta"] as? [String: Any], - let content = delta["content"] as? String { - resultText += content - } else if let text = firstChoice["text"] as? String { - resultText += text - } - } - } - } - - guard !resultText.isEmpty else { - logger.error("Failed to extract any text from SSE stream (length: \(streamText.count))") - throw TranslationProviderError.translationFailed("Empty text from SSE stream") - } - - return resultText.trimmingCharacters(in: .whitespacesAndNewlines) - } private func translate( text: String, diff --git a/TransFrame/Services/Translation/Providers/LLMTranslationProvider.swift b/TransFrame/Services/Translation/Providers/LLMTranslationProvider.swift index c706cf4..787f896 100644 --- a/TransFrame/Services/Translation/Providers/LLMTranslationProvider.swift +++ b/TransFrame/Services/Translation/Providers/LLMTranslationProvider.swift @@ -88,69 +88,15 @@ actor LLMTranslationProvider: TranslationProvider, TranslationPromptConfigurable ) async throws -> [TranslationResult] { guard !texts.isEmpty else { return [] } - // For multiple texts, combine into single request for efficiency - let combinedText = texts.joined(separator: "\n---\n") - let credentials = try await getCredentials() - let prompt = buildPrompt( - text: combinedText, - sourceLanguage: sourceLanguage, - targetLanguage: targetLanguage, - promptTemplate: promptTemplate - ) - - let start = Date() - let translatedText = try await callLLMAPI( - prompt: prompt, - credentials: credentials + let batches = LLMTranslationBatching.split( + texts: texts, maxOutputTokens: config.options?.maxTokens ?? 2048, promptTemplate: promptTemplate ) - let latency = Date().timeIntervalSince(start) - - logger.info("Translation completed in \(latency)s") - - let combinedResult = TranslationResult( - sourceText: combinedText, - translatedText: translatedText, - sourceLanguage: sourceLanguage ?? "Auto", - targetLanguage: targetLanguage - ) - - let translatedTexts = combinedResult.translatedText.components(separatedBy: "\n---\n") - - if translatedTexts.count == texts.count { - return zip(texts, translatedTexts).map { source, translated in - TranslationResult( - sourceText: source, - translatedText: translated.trimmingCharacters(in: .whitespacesAndNewlines), - sourceLanguage: combinedResult.sourceLanguage, - targetLanguage: combinedResult.targetLanguage - ) - } - } - - logger.warning("Batch split failed, falling back to individual translations") - var results: [TranslationResult] = [] - results.reserveCapacity(texts.count) - for text in texts { - let prompt = buildPrompt( - text: text, - sourceLanguage: sourceLanguage, - targetLanguage: targetLanguage, - promptTemplate: promptTemplate - ) - let translatedText = try await callLLMAPI( - prompt: prompt, - credentials: credentials - ) - results.append( - TranslationResult( - sourceText: text, - translatedText: translatedText, - sourceLanguage: sourceLanguage ?? "Auto", - targetLanguage: targetLanguage - ) + guard batches.count == 1, batches[0] == texts else { + return try await translateBatches( + batches, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate ) } - return results + return try await translateSingleBatch(texts, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate) } func translate( @@ -232,6 +178,38 @@ actor LLMTranslationProvider: TranslationProvider, TranslationPromptConfigurable // MARK: - Private Methods + private func translateBatches( + _ batches: [[String]], from sourceLanguage: String?, to targetLanguage: String, promptTemplate: String? + ) async throws -> [TranslationResult] { + var results: [TranslationResult] = [] + for batch in batches { + results.append(contentsOf: try await translateSingleBatch( + batch, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate + )) + } + return results + } + + private func translateSingleBatch( + _ texts: [String], from sourceLanguage: String?, to targetLanguage: String, promptTemplate: String? + ) async throws -> [TranslationResult] { + let credentials = try await getCredentials() + let combinedText = texts.joined(separator: LLMTranslationBatching.separator) + let prompt = buildPrompt(text: combinedText, sourceLanguage: sourceLanguage, targetLanguage: targetLanguage, promptTemplate: promptTemplate) + let translatedText = try await callLLMAPI(prompt: prompt, credentials: credentials) + let translatedTexts = translatedText.components(separatedBy: LLMTranslationBatching.separator) + if translatedTexts.count == texts.count { + return zip(texts, translatedTexts).map { + TranslationResult(sourceText: $0, translatedText: $1.trimmingCharacters(in: .whitespacesAndNewlines), sourceLanguage: sourceLanguage ?? "Auto", targetLanguage: targetLanguage) + } + } + guard texts.count > 1 else { + return [TranslationResult(sourceText: combinedText, translatedText: translatedText, sourceLanguage: sourceLanguage ?? "Auto", targetLanguage: targetLanguage)] + } + logger.warning("Batch split failed, retrying only unmatched individual texts") + return try await translateBatches(texts.map { [$0] }, from: sourceLanguage, to: targetLanguage, promptTemplate: promptTemplate) + } + private func getCredentials() async throws -> StoredCredentials? { guard engineType.requiresAPIKey else { return nil } guard let credentials = try await keychain.getCredentials(for: engineType) else { @@ -399,108 +377,9 @@ actor LLMTranslationProvider: TranslationProvider, TranslationPromptConfigurable } private func parseResponse(_ data: Data, for engineType: TranslationEngineType) throws -> String { - guard let rawString = String(data: data, encoding: .utf8) else { - throw TranslationProviderError.translationFailed("Unable to decode data to UTF-8 string") - } - - let trimmedResponse = rawString.trimmingCharacters(in: .whitespacesAndNewlines) - - // Check if response is Server-Sent Events (SSE) stream format - if trimmedResponse.hasPrefix("data:") || trimmedResponse.contains("\ndata:") { - return try parseSSEStream(trimmedResponse, for: engineType) - } - - let jsonObject: Any - do { - jsonObject = try JSONSerialization.jsonObject(with: data) - } catch { - logger.error("JSON serialization failed for \(engineType.rawValue): \(error.localizedDescription). Response size: \(data.count) bytes") - throw error - } - - guard let json = jsonObject as? [String: Any] else { - logger.error("Response JSON for \(engineType.rawValue) is not a dictionary object") - throw TranslationProviderError.translationFailed("Response JSON is not a dictionary") - } - - // Handle error responses from LLM APIs - if let errorObj = json["error"] as? [String: Any], - let errorMessage = errorObj["message"] as? String { - logger.error("API returned error for \(engineType.rawValue): \(errorMessage)") - throw TranslationProviderError.translationFailed("API error: \(errorMessage)") - } - - let content: String? - - switch engineType { - case .claude: - content = (json["content"] as? [[String: Any]])? - .first?["text"] as? String - default: - content = ((json["choices"] as? [[String: Any]])? - .first?["message"] as? [String: Any])?["content"] as? String - } - - guard let text = content else { - logger.error("Unexpected JSON response structure for \(engineType.rawValue) (missing choices or content)") - throw TranslationProviderError.translationFailed("Unexpected JSON response structure") - } - - return text.trimmingCharacters(in: .whitespacesAndNewlines) + try LLMTranslationResponseParser.extractText(from: data, for: engineType) } - private func parseSSEStream(_ streamText: String, for engineType: TranslationEngineType) throws -> String { - var resultText = "" - let lines = streamText.components(separatedBy: .newlines) - - for line in lines { - let trimmedLine = line.trimmingCharacters(in: .whitespacesAndNewlines) - guard !trimmedLine.isEmpty else { continue } - - // Skip stream end marker - if trimmedLine == "data: [DONE]" { - continue - } - - if trimmedLine.hasPrefix("data:") { - let jsonText = trimmedLine.dropFirst(5).trimmingCharacters(in: .whitespaces) - guard !jsonText.isEmpty else { continue } - - guard let jsonData = jsonText.data(using: .utf8), - let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any] else { - continue - } - - switch engineType { - case .claude: - // Claude streaming format (usually text delta in content_block_delta or message_delta) - if let type = json["type"] as? String, type == "content_block_delta", - let delta = json["delta"] as? [String: Any], - let text = delta["text"] as? String { - resultText += text - } - default: - // OpenAI/Gemini/Ollama streaming format - if let choices = json["choices"] as? [[String: Any]], - let firstChoice = choices.first { - if let delta = firstChoice["delta"] as? [String: Any], - let content = delta["content"] as? String { - resultText += content - } else if let text = firstChoice["text"] as? String { - resultText += text - } - } - } - } - } - - guard !resultText.isEmpty else { - logger.error("Failed to extract any text from SSE stream for \(engineType.rawValue) (length: \(streamText.count))") - throw TranslationProviderError.translationFailed("Empty text from SSE stream") - } - - return resultText.trimmingCharacters(in: .whitespacesAndNewlines) - } private func getBaseURL() throws -> URL { if let customURL = config.options?.baseURL { diff --git a/TransFrame/Services/Translation/TranslationEngineRegistry.swift b/TransFrame/Services/Translation/TranslationEngineRegistry.swift index bdfd12b..0139595 100644 --- a/TransFrame/Services/Translation/TranslationEngineRegistry.swift +++ b/TransFrame/Services/Translation/TranslationEngineRegistry.swift @@ -10,17 +10,64 @@ import os.log /// Actor-based registry for managing translation engine providers actor TranslationEngineRegistry { + typealias ProviderFactory = @Sendable ( + TranslationEngineType, + TranslationEngineConfig, + CompatibleTranslationProvider.ConfigurationSnapshot? + ) async throws -> any TranslationProvider + + typealias CompatibleProviderFactory = @Sendable ( + CompatibleTranslationProvider.CompatibleConfig, + TranslationEngineConfig + ) async throws -> CompatibleTranslationProvider + + typealias CompatibleConfigsProvider = @Sendable () async -> [CompatibleTranslationProvider.CompatibleConfig] + + private struct CachedProviderEntry { + let provider: any TranslationProvider + let config: TranslationEngineConfig? + let customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + let isManualOverride: Bool + } + + private struct InFlightProviderCreation { + let token: UInt64 + let config: TranslationEngineConfig + let customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + let task: Task + } + + private struct CachedCompatibleProviderEntry { + let provider: CompatibleTranslationProvider + let config: CompatibleTranslationProvider.CompatibleConfig + } + + private struct InFlightCompatibleCreation { + let token: UInt64 + let config: CompatibleTranslationProvider.CompatibleConfig + let task: Task + } + /// Shared singleton instance static let shared = TranslationEngineRegistry() /// Registered providers keyed by engine type - private var providers: [TranslationEngineType: any TranslationProvider] = [:] + private var providers: [TranslationEngineType: CachedProviderEntry] = [:] + private var inFlightCreations: [TranslationEngineType: InFlightProviderCreation] = [:] + private var engineMutationRevisions: [TranslationEngineType: UInt64] = [:] + private var nextCreationToken: UInt64 = 0 /// Compatible engine providers keyed by composite ID (e.g., "custom:0", "custom:1") - private var compatibleProviders: [String: CompatibleTranslationProvider] = [:] + private var compatibleProviders: [String: CachedCompatibleProviderEntry] = [:] + private var inFlightCompatibleCreations: [String: InFlightCompatibleCreation] = [:] + private var compatibleMutationRevisions: [String: UInt64] = [:] + private var compatibleClearRevision: UInt64 = 0 /// Keychain service for credential access private let keychain = KeychainService.shared + private let providerFactory: ProviderFactory? + private let compatibleProviderFactory: CompatibleProviderFactory? + private let compatibleConfigsProvider: CompatibleConfigsProvider /// Logger instance private let logger = Logger( @@ -28,15 +75,36 @@ actor TranslationEngineRegistry { category: "TranslationEngineRegistry" ) - init(registerBuiltInProviders: Bool = true) { + init( + registerBuiltInProviders: Bool = true, + providerFactory: ProviderFactory? = nil, + compatibleProviderFactory: CompatibleProviderFactory? = nil, + compatibleConfigsProvider: @escaping CompatibleConfigsProvider = { + await MainActor.run { AppSettings.shared.compatibleProviderConfigs } + } + ) { + self.providerFactory = providerFactory + self.compatibleProviderFactory = compatibleProviderFactory + self.compatibleConfigsProvider = compatibleConfigsProvider + if registerBuiltInProviders { // Cannot call async method in init, so we register synchronously. // Built-in providers don't need async setup. let appleProvider = AppleTranslationProvider() - providers[.apple] = appleProvider + providers[.apple] = CachedProviderEntry( + provider: appleProvider, + config: nil, + customSnapshot: nil, + isManualOverride: true + ) let mtranProvider = MTranServerEngine.shared - providers[.mtranServer] = mtranProvider + providers[.mtranServer] = CachedProviderEntry( + provider: mtranProvider, + config: .default(for: .mtranServer), + customSnapshot: nil, + isManualOverride: false + ) logger.info("Registered 2 built-in providers") } @@ -46,19 +114,34 @@ actor TranslationEngineRegistry { /// Register a provider for an engine type func register(_ provider: any TranslationProvider, for type: TranslationEngineType) { - providers[type] = provider + bumpEngineMutationRevision(for: type) + inFlightCreations[type]?.task.cancel() + inFlightCreations.removeValue(forKey: type) + providers[type] = CachedProviderEntry( + provider: provider, + config: nil, + customSnapshot: nil, + isManualOverride: true + ) logger.info("Registered provider for \(type.rawValue)") } /// Unregister a provider for an engine type func unregister(_ type: TranslationEngineType) { + bumpEngineMutationRevision(for: type) + inFlightCreations[type]?.task.cancel() + inFlightCreations.removeValue(forKey: type) providers.removeValue(forKey: type) logger.info("Unregistered provider for \(type.rawValue)") } /// Get the provider for an engine type func provider(for type: TranslationEngineType) -> (any TranslationProvider)? { - return providers[type] + return providers[type]?.provider + } + + func hasManualOverride(for type: TranslationEngineType) -> Bool { + return providers[type]?.isManualOverride == true } // MARK: - Availability @@ -71,7 +154,8 @@ actor TranslationEngineRegistry { /// List all available engines (registered and configured) func availableEngines() async -> [TranslationEngineType] { let results = await withTaskGroup(of: (TranslationEngineType, Bool).self) { group in - for (type, provider) in providers { + for (type, entry) in providers { + let provider = entry.provider group.addTask { await (type, provider.isAvailable) } } var availability: [TranslationEngineType: Bool] = [:] @@ -90,7 +174,7 @@ actor TranslationEngineRegistry { func isEngineConfigured(_ type: TranslationEngineType) async -> Bool { // Built-in engines don't need credentials if !type.requiresAPIKey { - return await providers[type]?.isAvailable ?? false + return await providers[type]?.provider.isAvailable ?? false } // Check if credentials exist in Keychain @@ -99,9 +183,28 @@ actor TranslationEngineRegistry { /// Check if an engine is available for use func isEngineAvailable(_ type: TranslationEngineType) async -> Bool { - guard let provider = providers[type] else { return false } + guard let provider = providers[type]?.provider else { return false } return await provider.isAvailable } + + private func allocateCreationToken() -> UInt64 { + nextCreationToken &+= 1 + return nextCreationToken + } + + @discardableResult + private func bumpEngineMutationRevision(for type: TranslationEngineType) -> UInt64 { + let next = (engineMutationRevisions[type] ?? 0) &+ 1 + engineMutationRevisions[type] = next + return next + } + + @discardableResult + private func bumpCompatibleMutationRevision(for compositeId: String) -> UInt64 { + let next = (compatibleMutationRevisions[compositeId] ?? 0) &+ 1 + compatibleMutationRevisions[compositeId] = next + return next + } } // MARK: - Provider Creation @@ -119,60 +222,99 @@ extension TranslationEngineRegistry { config: TranslationEngineConfig, forceRefresh: Bool = false ) async throws -> any TranslationProvider { - // Check if already registered and not forcing refresh - if !forceRefresh, let existing = providers[type] { - return existing + if !forceRefresh, + let existing = providers[type], + existing.isManualOverride { + return existing.provider } - let provider: any TranslationProvider - - switch type { - case .apple: - if providers[.apple] != nil { - throw RegistryError.alreadyRegistered - } - provider = AppleTranslationProvider() - - case .mtranServer: - if providers[.mtranServer] != nil { - throw RegistryError.alreadyRegistered + let customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + if type == .custom { + let savedConfigs = await compatibleConfigsProvider() + customSnapshot = CompatibleTranslationProvider.resolveConfigurationSnapshot( + from: config, + savedConfigs: savedConfigs + ) + if !forceRefresh, + let existing = providers[type], + existing.isManualOverride { + return existing.provider } - provider = MTranServerEngine.shared + } else { + customSnapshot = nil + } - case .openai, .claude, .gemini, .ollama: - provider = try await LLMTranslationProvider( + if !forceRefresh, let existing = providers[type] { + if existing.isManualOverride || matchesCachedProvider( + entry: existing, type: type, config: config, - keychain: keychain - ) + customSnapshot: customSnapshot + ) { + return existing.provider + } + providers.removeValue(forKey: type) + } else if forceRefresh { + providers.removeValue(forKey: type) + } - case .google: - provider = try await GoogleTranslationProvider( - config: config, - keychain: keychain - ) + if !forceRefresh, + let inFlight = inFlightCreations[type], + inFlight.config == config, + inFlight.customSnapshot == customSnapshot { + return try await inFlight.task.value + } - case .deepl: - provider = try await DeepLTranslationProvider( - config: config, - keychain: keychain - ) + let mutationRevision = bumpEngineMutationRevision(for: type) + let token = allocateCreationToken() + let task = makeProviderTask( + for: type, + config: config, + customSnapshot: customSnapshot + ) + inFlightCreations[type] = InFlightProviderCreation( + token: token, + config: config, + customSnapshot: customSnapshot, + task: task + ) - case .baidu: - provider = try await BaiduTranslationProvider( - config: config, - keychain: keychain - ) + do { + let provider = try await task.value + if inFlightCreations[type]?.token == token, + engineMutationRevisions[type] == mutationRevision { + inFlightCreations.removeValue(forKey: type) + providers[type] = CachedProviderEntry( + provider: provider, + config: config, + customSnapshot: customSnapshot, + isManualOverride: false + ) + logger.info("Registered configured provider for \(type.rawValue)") + } + return provider + } catch { + if inFlightCreations[type]?.token == token { + inFlightCreations.removeValue(forKey: type) + } + throw error + } + } - case .custom: - provider = try await CompatibleTranslationProvider( - config: config, - keychain: keychain + func createTemporaryProvider( + for type: TranslationEngineType, + config: TranslationEngineConfig + ) async throws -> any TranslationProvider { + let customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + if type == .custom { + let savedConfigs = await compatibleConfigsProvider() + customSnapshot = CompatibleTranslationProvider.resolveConfigurationSnapshot( + from: config, savedConfigs: savedConfigs ) + } else { + customSnapshot = nil } - - register(provider, for: type) - return provider + return try await makeProviderTask(for: type, config: config, customSnapshot: customSnapshot).value } /// Create and cache a compatible engine provider for a specific instance @@ -187,43 +329,186 @@ extension TranslationEngineRegistry { let compositeId = compatibleConfig.keychainId if !forceRefresh, let existing = compatibleProviders[compositeId] { - if existing.configHash == compatibleConfig.configHash { - return existing + if existing.config == compatibleConfig { + return existing.provider } compatibleProviders.removeValue(forKey: compositeId) + } else if forceRefresh { + compatibleProviders.removeValue(forKey: compositeId) + } + + if !forceRefresh, + let inFlight = inFlightCompatibleCreations[compositeId], + inFlight.config == compatibleConfig { + return try await inFlight.task.value } + let mutationRevision = bumpCompatibleMutationRevision(for: compositeId) + let clearRevision = compatibleClearRevision + let token = allocateCreationToken() let engineConfig = TranslationEngineConfig.default(for: .custom) - let provider = try await CompatibleTranslationProvider( - config: engineConfig, + let task = makeCompatibleProviderTask( compatibleConfig: compatibleConfig, - keychain: keychain + engineConfig: engineConfig + ) + inFlightCompatibleCreations[compositeId] = InFlightCompatibleCreation( + token: token, + config: compatibleConfig, + task: task ) - compatibleProviders[compositeId] = provider - logger.info("Created compatible provider for \(compositeId)") - return provider + do { + let provider = try await task.value + if inFlightCompatibleCreations[compositeId]?.token == token, + compatibleMutationRevisions[compositeId] == mutationRevision, + compatibleClearRevision == clearRevision { + inFlightCompatibleCreations.removeValue(forKey: compositeId) + compatibleProviders[compositeId] = CachedCompatibleProviderEntry( + provider: provider, + config: compatibleConfig + ) + logger.info("Created compatible provider for \(compositeId)") + } + return provider + } catch { + if inFlightCompatibleCreations[compositeId]?.token == token { + inFlightCompatibleCreations.removeValue(forKey: compositeId) + } + throw error + } } /// Get a cached compatible engine provider /// - Parameter compositeId: The composite identifier (e.g., "custom:0") /// - Returns: The cached provider, or nil func getCompatibleProvider(for compositeId: String) -> CompatibleTranslationProvider? { - return compatibleProviders[compositeId] + return compatibleProviders[compositeId]?.provider } /// Remove a cached compatible engine provider /// - Parameter compositeId: The composite identifier func removeCompatibleProvider(for compositeId: String) { + bumpCompatibleMutationRevision(for: compositeId) + inFlightCompatibleCreations[compositeId]?.task.cancel() + inFlightCompatibleCreations.removeValue(forKey: compositeId) compatibleProviders.removeValue(forKey: compositeId) logger.info("Removed compatible provider for \(compositeId)") } /// Clear all cached compatible providers func clearCompatibleProviders() { + compatibleClearRevision &+= 1 + for compositeId in Set(compatibleProviders.keys).union(inFlightCompatibleCreations.keys) { + bumpCompatibleMutationRevision(for: compositeId) + } + for inFlight in inFlightCompatibleCreations.values { + inFlight.task.cancel() + } + inFlightCompatibleCreations.removeAll() compatibleProviders.removeAll() logger.info("Cleared all compatible providers") } + + private func matchesCachedProvider( + entry: CachedProviderEntry, + type: TranslationEngineType, + config: TranslationEngineConfig, + customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + ) -> Bool { + guard entry.config == config else { + return false + } + if type == .custom { + return entry.customSnapshot == customSnapshot + } + return true + } + + private func makeProviderTask( + for type: TranslationEngineType, + config: TranslationEngineConfig, + customSnapshot: CompatibleTranslationProvider.ConfigurationSnapshot? + ) -> Task { + if let providerFactory { + return Task { + try await providerFactory(type, config, customSnapshot) + } + } + + let keychain = self.keychain + let appleAlreadyRegistered = providers[.apple] != nil + + return Task { + switch type { + case .apple: + if appleAlreadyRegistered { + throw RegistryError.alreadyRegistered + } + return AppleTranslationProvider() + + case .mtranServer: + return MTranServerEngine.provider(config: config) + + case .openai, .claude, .gemini, .ollama: + return try await LLMTranslationProvider( + type: type, + config: config, + keychain: keychain + ) + + case .google: + return try await GoogleTranslationProvider( + config: config, + keychain: keychain + ) + + case .deepl: + return try await DeepLTranslationProvider( + config: config, + keychain: keychain + ) + + case .baidu: + return try await BaiduTranslationProvider( + config: config, + keychain: keychain + ) + + case .custom: + if let customSnapshot { + return CompatibleTranslationProvider( + snapshot: customSnapshot, + keychain: keychain, + providerID: "custom" + ) + } + return try await CompatibleTranslationProvider( + config: config, + keychain: keychain + ) + } + } + } + + private func makeCompatibleProviderTask( + compatibleConfig: CompatibleTranslationProvider.CompatibleConfig, + engineConfig: TranslationEngineConfig + ) -> Task { + if let compatibleProviderFactory { + return Task { + try await compatibleProviderFactory(compatibleConfig, engineConfig) + } + } + + let keychain = self.keychain + return Task { + try await CompatibleTranslationProvider( + config: engineConfig, + compatibleConfig: compatibleConfig, + keychain: keychain + ) + } + } } // MARK: - Registry Errors diff --git a/TransFrame/Services/TranslationProvider.swift b/TransFrame/Services/TranslationProvider.swift index 25e7e4f..a157e36 100644 --- a/TransFrame/Services/TranslationProvider.swift +++ b/TransFrame/Services/TranslationProvider.swift @@ -77,6 +77,7 @@ enum TranslationProviderError: LocalizedError, Sendable { case connectionFailed(String) case invalidConfiguration(String) case translationFailed(String) + case incompleteResponse(String) case emptyInput case unsupportedLanguage(String) case timeout @@ -92,6 +93,8 @@ enum TranslationProviderError: LocalizedError, Sendable { return "Invalid configuration: \(message)" case .translationFailed(let message): return "Translation failed: \(message)" + case .incompleteResponse(let message): + return "Translation output was incomplete: \(message)" case .emptyInput: return "Cannot translate empty text." case .unsupportedLanguage(let language): @@ -116,6 +119,8 @@ enum TranslationProviderError: LocalizedError, Sendable { return NSLocalizedString("translation.provider.recovery.invalidConfiguration", comment: "") case .translationFailed: return NSLocalizedString("translation.provider.recovery.translationFailed", comment: "") + case .incompleteResponse: + return "Increase the output token limit or retry with a shorter text." case .emptyInput: return NSLocalizedString("translation.provider.recovery.emptyInput", comment: "") case .unsupportedLanguage: diff --git a/TransFrame/Services/TranslationService.swift b/TransFrame/Services/TranslationService.swift index 0e3297d..bc61b07 100644 --- a/TransFrame/Services/TranslationService.swift +++ b/TransFrame/Services/TranslationService.swift @@ -13,9 +13,12 @@ import NaturalLanguage /// Orchestrates multiple translation providers with various selection modes @available(macOS 13.0, *) actor TranslationService { + typealias EngineConfigsProvider = @Sendable () async -> [TranslationEngineType: TranslationEngineConfig] + static let shared = TranslationService() private let registry: TranslationEngineRegistry + private let engineConfigsProvider: EngineConfigsProvider private let keychain = KeychainService.shared private let logger = Logger( subsystem: Bundle.main.bundleIdentifier ?? "TransFrame", @@ -25,8 +28,14 @@ actor TranslationService { // Prompt configuration private var promptConfig: TranslationPromptConfig = TranslationPromptConfig() - init(registry: TranslationEngineRegistry = .shared) { + init( + registry: TranslationEngineRegistry = .shared, + engineConfigsProvider: @escaping EngineConfigsProvider = { + await MainActor.run { AppSettings.shared.engineConfigs } + } + ) { self.registry = registry + self.engineConfigsProvider = engineConfigsProvider } // MARK: - Main Translation API @@ -642,23 +651,28 @@ actor TranslationService { /// Filters engine list to only include engines that are explicitly enabled in user settings. /// Apple is treated as always enabled (it's the default built-in engine). private func filterEnabledEngines(_ engines: [TranslationEngineType]) async -> [TranslationEngineType] { - let configs = await MainActor.run { - AppSettings.shared.engineConfigs - } + let configs = await engineConfigsProvider() return engines.filter { engine in engine == .apple || configs[engine]?.isEnabled == true } } - private func resolvedProvider(for engine: TranslationEngineType) async throws -> any TranslationProvider { - if let provider = await registry.provider(for: engine) { + private func resolvedProvider( + for engine: TranslationEngineType, + config overrideConfig: TranslationEngineConfig? = nil + ) async throws -> any TranslationProvider { + if overrideConfig == nil, await registry.hasManualOverride(for: engine), + let provider = await registry.provider(for: engine) { return provider } - let engineConfig = await MainActor.run { - AppSettings.shared.engineConfigs[engine] ?? .default(for: engine) + if let overrideConfig { + return try await registry.createTemporaryProvider(for: engine, config: overrideConfig) } + let configs = await engineConfigsProvider() + let engineConfig = configs[engine] ?? .default(for: engine) + return try await registry.createProvider(for: engine, config: engineConfig) } @@ -714,31 +728,24 @@ actor TranslationService { /// Verify connection and throw details on failure func verifyConnection(for engine: TranslationEngineType) async throws { - let provider: any TranslationProvider - if let existing = await registry.provider(for: engine) { - provider = existing - } else { - provider = try await resolvedProvider(for: engine) - } + try await verifyConnection(for: engine, config: nil) + } + + func verifyConnection( + for engine: TranslationEngineType, + config: TranslationEngineConfig? + ) async throws { + let provider = try await resolvedProvider(for: engine, config: config) try await provider.verifyConnection() } /// Test connection to a specific engine func testConnection(for engine: TranslationEngineType) async -> Bool { - // First try to get existing provider - if let provider = await registry.provider(for: engine) { + if await registry.hasManualOverride(for: engine), + let provider = await registry.provider(for: engine) { return await provider.checkConnection() } - // If provider doesn't exist, create it for engines that need credentials - // (Google, DeepL, Baidu, LLM providers, etc.) - guard engine.requiresAPIKey else { - // Built-in engines (apple, mtranServer) should already be registered in init - // If missing, log warning but return true to avoid false failure in UI - logger.warning("Built-in engine \(engine.rawValue) provider not found in registry") - return true - } - let provider: any TranslationProvider do { provider = try await resolvedProvider(for: engine) diff --git a/TransFrameTests/AnalysisImageTilingTests.swift b/TransFrameTests/AnalysisImageTilingTests.swift new file mode 100644 index 0000000..08d7009 --- /dev/null +++ b/TransFrameTests/AnalysisImageTilingTests.swift @@ -0,0 +1,79 @@ +import CoreGraphics +import XCTest +@testable import TransFrame + +final class AnalysisImageTilingTests: XCTestCase { + func testSmallImageDoesNotTile() { + let image = Self.makeImage(width: 800, height: 600) + let tiles = AnalysisImageTiler.tiles(for: image) + XCTAssertEqual(tiles.count, 1) + XCTAssertTrue(tiles[0].image === image) + XCTAssertEqual(tiles[0].pixelRect, CGRect(x: 0, y: 0, width: 800, height: 600)) + } + + func testLongImageTilesKeepSourcePixelsAndCoverWholeImage() { + let image = Self.makeImage(width: 120, height: 500) + let tiles = AnalysisImageTiler.tiles(for: image, maxDimension: 100, overlap: 10) + XCTAssertEqual(tiles.map(\.pixelRect), [ + CGRect(x: 0, y: 0, width: 120, height: 100), + CGRect(x: 0, y: 90, width: 120, height: 100), + CGRect(x: 0, y: 180, width: 120, height: 100), + CGRect(x: 0, y: 270, width: 120, height: 100), + CGRect(x: 0, y: 360, width: 120, height: 100), + CGRect(x: 0, y: 400, width: 120, height: 100) + ]) + XCTAssertEqual(tiles.map(\.image.width), Array(repeating: 120, count: 6)) + } + + func testWideAndTallImagesTileBothAxes() { + let image = Self.makeImage(width: 250, height: 250) + let tiles = AnalysisImageTiler.tiles(for: image, maxDimension: 100, overlap: 20) + XCTAssertEqual(tiles.count, 9) + XCTAssertEqual(tiles.last?.pixelRect, CGRect(x: 150, y: 150, width: 100, height: 100)) + } + + func testNormalizedCoordinatesMapToOriginalImageAndRemoveOverlapDuplicates() throws { + let image = Self.makeImage(width: 100, height: 250) + let tiles = AnalysisImageTiler.tiles(for: image, maxDimension: 100, overlap: 20) + let results = tiles.enumerated().map { index, tile in + let segments = index < 2 + ? [TextSegment(text: "Edit", boundingBox: CGRect(x: 0.2, y: index == 0 ? 0.8 : 0.0, width: 0.5, height: 0.2), confidence: 1)] + : [] + return ScreenAnalysisResult( + segments: segments, imageSize: CGSize(width: tile.image.width, height: tile.image.height) + ) + } + let merged = try AnalysisImageTiler.merging(tiles.map(\.pixelRect), results: results) + XCTAssertEqual(merged.imageSize, CGSize(width: 100, height: 250)) + XCTAssertEqual(merged.segments.count, 1) + XCTAssertEqual(merged.segments[0].boundingBox.minX, 0.2, accuracy: 0.0001) + XCTAssertEqual(merged.segments[0].boundingBox.minY, 0.32, accuracy: 0.0001) + XCTAssertEqual(merged.segments[0].boundingBox.width, 0.5, accuracy: 0.0001) + XCTAssertEqual(merged.segments[0].boundingBox.height, 0.08, accuracy: 0.0001) + } + + func testIncompleteOrOutOfBoundsTileIsRejected() { + let image = Self.makeImage(width: 100, height: 250) + let tiles = AnalysisImageTiler.tiles(for: image, maxDimension: 100, overlap: 20) + let valid = ScreenAnalysisResult( + segments: [TextSegment(text: "text", boundingBox: CGRect(x: 0.2, y: 0.2, width: 0.2, height: 0.2), confidence: 1)], + imageSize: CGSize(width: 100, height: 100) + ) + XCTAssertThrowsError(try AnalysisImageTiler.merging([tiles[0].pixelRect], results: [])) + XCTAssertThrowsError(try AnalysisImageTiler.merging( + [tiles[0].pixelRect], results: [ScreenAnalysisResult.empty(imageSize: .zero)] + )) + XCTAssertNoThrow(try AnalysisImageTiler.merging([tiles[0].pixelRect], results: [valid])) + } + + private static func makeImage(width: Int, height: Int) -> CGImage { + let context = CGContext( + data: nil, width: width, height: height, bitsPerComponent: 8, + bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue + )! + context.setFillColor(CGColor(red: 0.2, green: 0.4, blue: 0.6, alpha: 1)) + context.fill(CGRect(x: 0, y: 0, width: width, height: height)) + return context.makeImage()! + } +} diff --git a/TransFrameTests/LLMResponseTruncationTests.swift b/TransFrameTests/LLMResponseTruncationTests.swift new file mode 100644 index 0000000..9d6d573 --- /dev/null +++ b/TransFrameTests/LLMResponseTruncationTests.swift @@ -0,0 +1,104 @@ +import XCTest +@testable import TransFrame + +final class LLMResponseTruncationTests: XCTestCase { + func testOpenAIAndClaudeLengthResponsesAreRejected() throws { + let openAI = Data(#"{"choices":[{"message":{"content":"部分译文"},"finish_reason":"length"}]}"#.utf8) + let claude = Data(#"{"content":[{"type":"text","text":"partial"}],"stop_reason":"max_tokens"}"#.utf8) + for (data, engine) in [(openAI, TranslationEngineType.openai), (claude, .claude)] { + XCTAssertThrowsError(try LLMTranslationResponseParser.extractText(from: data, for: engine)) { error in + guard case TranslationProviderError.incompleteResponse = error else { + return XCTFail("应为截断错误,实际 \(error)") + } + } + } + } + + func testOpenAISSETruncationCannotBeReportedAsSuccess() { + let data = Data("data: {\"choices\":[{\"delta\":{\"content\":\"part\"},\"finish_reason\":null}]}\ndata: {\"choices\":[{\"delta\":{},\"finish_reason\":\"length\"}]}\ndata: [DONE]\n".utf8) + XCTAssertThrowsError(try LLMTranslationResponseParser.extractText(from: data, for: .openai)) + } + + func testBatchingRespectsBudgetAndPreservesAllSourceText() { + let texts = [String(repeating: "汉", count: 20), String(repeating: "字", count: 20), "third", "fourth"] + let batches = LLMTranslationBatching.split(texts: texts, maxOutputTokens: 20, promptTemplate: nil) + XCTAssertEqual(batches.flatMap { $0 }, texts) + XCTAssertGreaterThan(batches.count, 1) + XCTAssertEqual(batches.map { $0.joined(separator: "\n---\n") }.joined(separator: "\n---\n"), texts.joined(separator: "\n---\n")) + } + + func testLongSingleSegmentIsSplitAndCanBeRecombined() { + let text = String(repeating: "漢👨‍👩‍👧‍👦", count: 100) + let batches = LLMTranslationBatching.split(texts: [text], maxOutputTokens: 32, promptTemplate: nil) + XCTAssertGreaterThan(batches.count, 1) + XCTAssertEqual(batches.flatMap { $0 }.joined(), text) + } + + func testLongCustomPromptStillSplitsWithoutDroppingSource() { + let texts = [String(repeating: "汉", count: 20), String(repeating: "字", count: 20)] + let batches = LLMTranslationBatching.split( + texts: texts, + maxOutputTokens: 32, + promptTemplate: String(repeating: "p", count: 120) + "{text}" + ) + XCTAssertGreaterThanOrEqual(batches.count, 2) + XCTAssertEqual(batches.flatMap { $0 }.joined(), texts.joined()) + } + + func testCompatibleBatchRequestsKeepOriginalSegments() async throws { + let recorder = LLMFixtureRecorder() + let sessionConfiguration = URLSessionConfiguration.ephemeral + sessionConfiguration.protocolClasses = [LLMURLProtocol.self] + let session = URLSession(configuration: sessionConfiguration) + defer { session.invalidateAndCancel() } + LLMURLProtocol.handler = { [recorder] request in + recorder.append(request) + return Data(#"{"choices":[{"message":{"content":"hello"},"finish_reason":"stop"}]}"#.utf8) + } + let id = UUID() + let compatible = CompatibleTranslationProvider.CompatibleConfig( + id: id, displayName: "fixture", baseURL: "http://fixture.invalid/v1", modelName: "fixture", hasAPIKey: false + ) + var config = TranslationEngineConfig.default(for: .custom) + config.options = EngineOptions(maxTokens: 28) + let provider = try await CompatibleTranslationProvider( + config: config, compatibleConfig: compatible, keychain: .shared, session: session + ) + let texts = [String(repeating: "汉", count: 20), String(repeating: "字", count: 20)] + let results = try await provider.translate(texts: texts, from: "zh", to: "en") + XCTAssertEqual(results.count, 2) + let requests = recorder.requests + XCTAssertEqual(requests.count, 2) + for (text, request) in zip(texts, requests) { + let body = try XCTUnwrap(request.requestBody) + let json = try JSONSerialization.jsonObject(with: body) as? [String: Any] + let messages = json?["messages"] as? [[String: Any]] + let prompt = messages?.first?["content"] as? String + XCTAssertNotNil(messages) + XCTAssertEqual(prompt?.contains(text), true) + } + } +} + +private final class LLMFixtureRecorder: @unchecked Sendable { + private let lock = NSLock() + private var storage: [URLRequest] = [] + var requests: [URLRequest] { lock.withLock { storage } } + func append(_ request: URLRequest) { lock.withLock { storage.append(request) } } +} + +private final class LLMURLProtocol: URLProtocol { + nonisolated(unsafe) static var handler: (URLRequest) -> Data = { _ in Data() } + + override class func canInit(with request: URLRequest) -> Bool { + request.url?.host == "fixture.invalid" + } + override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } + override func startLoading() { + let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)! + client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) + client?.urlProtocol(self, didLoad: Self.handler(request)) + client?.urlProtocolDidFinishLoading(self) + } + override func stopLoading() {} +} diff --git a/TransFrameTests/MTranServerEngineTests.swift b/TransFrameTests/MTranServerEngineTests.swift new file mode 100644 index 0000000..9c7a9f7 --- /dev/null +++ b/TransFrameTests/MTranServerEngineTests.swift @@ -0,0 +1,441 @@ +import Foundation +import XCTest +@testable import TransFrame + +@MainActor +final class MTranServerEngineTests: XCTestCase { + private var session: URLSession! + private var checker: MTranServerChecker! + + override func setUp() async throws { + try await super.setUp() + MTranTestURLProtocol.reset() + let configuration = URLSessionConfiguration.ephemeral + configuration.protocolClasses = [MTranTestURLProtocol.Handler.self] + session = URLSession(configuration: configuration) + checker = MTranServerChecker(session: session) + } + + override func tearDown() async throws { + session.invalidateAndCancel() + MTranTestURLProtocol.reset() + try await super.tearDown() + } + + func testSingleTranslationSendsOnlyOnePOST() async throws { + let result = try await makeEngine().translate("hello", from: "en", to: "zh") + + XCTAssertEqual(result.translatedText, "你好") + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.httpMethod), ["POST"]) + XCTAssertEqual(MTranTestURLProtocol.requests.first?.url?.host, "mtran.test") + } + + func testBatchSendsOnePOSTPerSegmentWithoutHealthChecks() async throws { + let results = try await makeEngine().translate(texts: ["one", "two", "three"], from: "en", to: "zh") + + XCTAssertEqual(results.count, 3) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.httpMethod), ["POST", "POST", "POST"]) + } + + func testProviderAvailabilityOnlyValidatesConfiguration() async { + let available = await makeEngine().isAvailable + let invalid = MTranServerEngine( + configuration: makeConfig(port: 0), session: session, checker: checker + ) + + let invalidAvailability = await invalid.isAvailable + XCTAssertTrue(available) + XCTAssertFalse(invalidAvailability) + XCTAssertTrue(MTranTestURLProtocol.requests.isEmpty) + } + + func testInvalidTranslationConfigurationDoesNotRequestOrCrash() async { + let engine = MTranServerEngine( + configuration: makeConfig(timeout: -1), session: session, checker: checker + ) + + await assertThrows(MTranServerError.invalidURL) { + _ = try await engine.translate("hello", from: "en", to: "zh") + } + XCTAssertTrue(MTranTestURLProtocol.requests.isEmpty) + } + + func testSynchronousAvailabilityDoesNotPerformNetworkRequests() { + XCTAssertFalse(checker.isAvailable) + XCTAssertTrue(MTranTestURLProtocol.requests.isEmpty) + } + + func testCachedNegativeHealthCheckDoesNotBlockRecoveredTranslation() async throws { + MTranTestURLProtocol.defaultStatusCode = 500 + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertFalse(available) + + MTranTestURLProtocol.defaultStatusCode = 200 + let result = try await makeEngine().translate("hello", from: "en", to: "zh") + + XCTAssertEqual(result.translatedText, "你好") + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.httpMethod), ["GET", "POST"]) + } + + func testHealthHTTP500IsUnavailableWithoutFallback() async throws { + MTranTestURLProtocol.defaultStatusCode = 500 + + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertFalse(available) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health"]) + } + + func testHealthNotFoundFallsBackToRoot() async throws { + MTranTestURLProtocol.statusCodes = ["/health": 404] + + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertTrue(available) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health", "/"]) + } + + func testTranslateMethodNotAllowedIsNotAvailable() async throws { + MTranTestURLProtocol.statusCodes = ["/health": 404, "/": 404, "/translate": 405] + + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertFalse(available) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health", "/", "/translate"]) + } + + func testTranslateSuccessIsAValidLegacyHealthResponse() async throws { + MTranTestURLProtocol.statusCodes = ["/health": 404, "/": 404] + + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertTrue(available) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health", "/", "/translate"]) + } + + func testAvailabilityCacheUsesEndpointTTLAndForceRefresh() async throws { + let clock = MTranTestClock() + let checker = MTranServerChecker(session: session, cacheTTL: 5, now: { clock.now }) + let config = makeConfig() + + let initial = try await checker.checkAvailability(configuration: config) + let cached = try await checker.checkAvailability(configuration: config) + XCTAssertTrue(initial && cached) + XCTAssertEqual(MTranTestURLProtocol.requests.count, 1) + + clock.advance(by: 6) + let expired = try await checker.checkAvailability(configuration: config) + let forced = try await checker.checkAvailability(configuration: config, forceRefresh: true) + XCTAssertTrue(expired && forced) + XCTAssertEqual(MTranTestURLProtocol.requests.count, 3) + } + + func testAvailabilityCacheSeparatesEndpoints() async throws { + MTranTestURLProtocol.statusCodes = ["/health": 404] + let first = makeConfig() + let second = makeConfig(host: "other.test", port: 8990) + + let firstAvailable = try await checker.checkAvailability(configuration: first) + let secondAvailable = try await checker.checkAvailability(configuration: second) + XCTAssertTrue(firstAvailable && secondAvailable) + XCTAssertEqual(MTranTestURLProtocol.requests.count, 4) + XCTAssertTrue(checker.cachedAvailability(for: first)) + XCTAssertTrue(checker.cachedAvailability(for: second)) + } + + func testLateOldEndpointResultDoesNotOverwriteNewEndpoint() async throws { + let gate = MTranTestGate() + let oldConfig = makeConfig(host: "old.test") + let newConfig = makeConfig(host: "new.test") + MTranTestURLProtocol.requestHandler = { request in + if request.url?.host == "old.test" { + await gate.wait() + return MTranTestURLProtocol.success(statusCode: 200, request: request) + } + return MTranTestURLProtocol.success(statusCode: 500, request: request) + } + + let checker = self.checker! + async let oldAvailability = checker.checkAvailability(configuration: oldConfig) + await waitForRequests(count: 1) + let newAvailable = try await checker.checkAvailability(configuration: newConfig) + gate.open() + XCTAssertFalse(newAvailable) + let oldResult = try await oldAvailability + XCTAssertTrue(oldResult) + + XCTAssertFalse(checker.cachedAvailability(for: newConfig)) + XCTAssertTrue(checker.cachedAvailability(for: oldConfig)) + } + + func testConcurrentChecksShareOneHealthRequest() async throws { + let gate = MTranTestGate() + MTranTestURLProtocol.requestHandler = { request in + await gate.wait() + return MTranTestURLProtocol.success(statusCode: 200, request: request) + } + let config = makeConfig() + let checker = self.checker! + + async let first = checker.checkAvailability(configuration: config) + async let second = checker.checkAvailability(configuration: config) + async let third = checker.checkAvailability(configuration: config, forceRefresh: true) + await waitForRequests(count: 1) + gate.open() + + let results = try await [first, second, third] + XCTAssertEqual(results, [true, true, true]) + XCTAssertEqual(MTranTestURLProtocol.requests.count, 1) + } + + func testVerifyConnectionForcesHealthRefresh() async throws { + let engine = makeEngine() + try await engine.verifyConnection() + try await engine.verifyConnection() + + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.httpMethod), ["GET", "GET"]) + } + + func testTranslationPropagatesCancellation() async { + let started = expectation(description: "request started") + let stopped = expectation(description: "request cancelled") + MTranTestURLProtocol.requestHandler = { request in + started.fulfill() + try await Task.sleep(nanoseconds: 10_000_000_000) + return MTranTestURLProtocol.success(statusCode: 200, request: request) + } + MTranTestURLProtocol.onStop = { stopped.fulfill() } + let engine = makeEngine() + let task = Task { + try await engine.translate("hello", from: "en", to: "zh") + } + + await fulfillment(of: [started], timeout: 1) + task.cancel() + await fulfillment(of: [stopped], timeout: 1) + await assertThrows(CancellationError()) { _ = try await task.value } + } + + func testTranslationAppliesConfiguredTimeout() async throws { + _ = try await makeEngine(configuration: makeConfig(timeout: 0.25)) + .translate("hello", from: "en", to: "zh") + + XCTAssertEqual(MTranTestURLProtocol.requests.first?.timeoutInterval, 0.25) + } + + func testIPv6ConfigurationBuildsBracketedURL() async throws { + _ = try await makeEngine(configuration: makeConfig(host: "[::1]")) + .translate("hello", from: "en", to: "zh") + + XCTAssertEqual(MTranTestURLProtocol.requests.first?.url?.absoluteString, "http://[::1]:8989/translate") + } + + func testHealthCancellationPropagatesWithoutCaching() async { + let gate = MTranTestGate() + MTranTestURLProtocol.requestHandler = { request in + await gate.wait() + return MTranTestURLProtocol.success(statusCode: 500, request: request) + } + let config = makeConfig() + let task = Task { [checker] in + try await checker?.checkAvailability(configuration: config, forceRefresh: true) + } + + await waitForRequests(count: 1) + task.cancel() + do { + _ = try await task.value + XCTFail("Expected cancellation") + } catch is CancellationError { + // Expected cancellation, not a false health result. + } catch { + XCTFail("Expected cancellation, got \(error)") + } + gate.open() + + XCTAssertFalse(checker.cachedAvailability(for: config)) + } + + func testServerURLConfigurationPreservesIPv6AndRejectsPaths() { + let config = MTranServerEngine.Configuration(serverURL: "http://[::1]:8989") + + XCTAssertEqual(config?.serverAddress, "[::1]") + XCTAssertEqual(config?.serverPort, 8989) + XCTAssertNil(MTranServerEngine.Configuration(serverURL: "http://localhost:8989/api")) + XCTAssertNil(MTranServerEngine.Configuration(serverURL: "http://[bad:host]:8989")) + } + + func testEngineOptionsConfigureProviderEndpointAndTimeout() async { + let engine = MTranServerEngine.provider(config: TranslationEngineConfig( + id: .mtranServer, + isEnabled: true, + options: EngineOptions(baseURL: "http://mtran.test:8990", timeout: 2.5) + )) + XCTAssertEqual(engine.configuration, MTranServerEngine.Configuration( + serverAddress: "mtran.test", serverPort: 8990, timeout: 2.5, + autoDetectSourceLanguage: true + )) + } + + private func makeConfig( + host: String = "mtran.test", port: Int = 8989, timeout: TimeInterval = 1 + ) -> MTranServerEngine.Configuration { + .init( + serverAddress: host, serverPort: port, timeout: timeout, + autoDetectSourceLanguage: true + ) + } + + private func makeEngine( + configuration: MTranServerEngine.Configuration? = nil + ) -> MTranServerEngine { + MTranServerEngine( + configuration: configuration ?? makeConfig(), session: session, checker: checker + ) + } + + private func waitForRequests(count: Int) async { + for _ in 0..<100 { + if MTranTestURLProtocol.requests.count == count { return } + await Task.yield() + } + XCTFail("Expected \(count) requests, got \(MTranTestURLProtocol.requests.count)") + } + + private func assertThrows( + _ expected: MTranServerError, operation: @escaping () async throws -> Void + ) async { + do { + try await operation() + XCTFail("Expected \(expected)") + } catch let error as MTranServerError { + switch (error, expected) { + case (.invalidURL, .invalidURL), (.timeout, .timeout): + break + default: + XCTFail("Expected \(expected), got \(error)") + } + } catch { + XCTFail("Expected \(expected), got \(error)") + } + } + + private func assertThrows( + _ expected: CancellationError, operation: @escaping () async throws -> Void + ) async { + do { + try await operation() + XCTFail("Expected \(expected)") + } catch is CancellationError { + // Expected cancellation from the request, not a generic unavailable error. + } catch { + XCTFail("Expected \(expected), got \(error)") + } + } +} + +private enum MTranTestURLProtocol { + static let lock = NSLock() + nonisolated(unsafe) private static var recordedRequests: [URLRequest] = [] + nonisolated(unsafe) static var defaultStatusCode = 200 + nonisolated(unsafe) static var statusCodes: [String: Int] = [:] + nonisolated(unsafe) static var onStop: @Sendable () -> Void = {} + nonisolated(unsafe) static var requestHandler: (@Sendable (URLRequest) async throws -> (Data, URLResponse))? + + static var requests: [URLRequest] { lock.withLock { recordedRequests } } + + static func reset() { + lock.withLock { + recordedRequests = [] + defaultStatusCode = 200 + statusCodes = [:] + onStop = {} + requestHandler = nil + } + } + + static func success(statusCode: Int, request: URLRequest) -> (Data, URLResponse) { + ( + Data(#"{"translated_text":"你好","detected_language":"en"}"#.utf8), + HTTPURLResponse(url: request.url!, statusCode: statusCode, httpVersion: nil, headerFields: nil)! + ) + } + + private struct UncheckedContext: @unchecked Sendable { + let handler: Handler + let client: (any URLProtocolClient)? + } + + final class Handler: URLProtocol { + private let taskLock = NSLock() + nonisolated(unsafe) private var loadingTask: Task? + + override class func canInit(with request: URLRequest) -> Bool { true } + override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } + + override func startLoading() { + let context = UncheckedContext(handler: self, client: client) + let task = Task { [request, context] in + do { + MTranTestURLProtocol.lock.withLock { + MTranTestURLProtocol.recordedRequests.append(request) + } + let (data, response): (Data, URLResponse) + if let handler = MTranTestURLProtocol.requestHandler { + (data, response) = try await handler(request) + } else { + let statusCode = MTranTestURLProtocol.statusCodes[request.url?.path ?? ""] + ?? MTranTestURLProtocol.defaultStatusCode + (data, response) = MTranTestURLProtocol.success( + statusCode: statusCode, request: request + ) + } + try Task.checkCancellation() + context.client?.urlProtocol(context.handler, didReceive: response, cacheStoragePolicy: .notAllowed) + context.client?.urlProtocol(context.handler, didLoad: data) + context.client?.urlProtocolDidFinishLoading(context.handler) + } catch { + context.client?.urlProtocol(context.handler, didFailWithError: error) + } + } + taskLock.withLock { loadingTask = task } + } + + override func stopLoading() { + taskLock.withLock { + loadingTask?.cancel() + loadingTask = nil + } + MTranTestURLProtocol.onStop() + } + } +} + +private final class MTranTestGate: @unchecked Sendable { + private let lock = NSLock() + private var continuations: [CheckedContinuation] = [] + private var isOpen = false + + func wait() async { + await withCheckedContinuation { continuation in + let resumed = lock.withLock { + if isOpen { return true } + continuations.append(continuation) + return false + } + if resumed { continuation.resume() } + } + } + + func open() { + let pending = lock.withLock { + isOpen = true + defer { continuations.removeAll() } + return continuations + } + pending.forEach { $0.resume() } + } +} + +private final class MTranTestClock: @unchecked Sendable { + private let lock = NSLock() + private var time: TimeInterval = 0 + var now: TimeInterval { lock.withLock { time } } + func advance(by interval: TimeInterval) { lock.withLock { time += interval } } +} diff --git a/TransFrameTests/TextInsertionSafetyTests.swift b/TransFrameTests/TextInsertionSafetyTests.swift new file mode 100644 index 0000000..452011d --- /dev/null +++ b/TransFrameTests/TextInsertionSafetyTests.swift @@ -0,0 +1,117 @@ +import XCTest +@testable import TransFrame + +@MainActor +final class TextInsertionSafetyTests: XCTestCase { + func testChangedFocusNeverWrites() async throws { + let target = InsertionTargetStub() + let captured = try target.capture() + target.isFocused = false + assertRejected(target, captured: captured) + XCTAssertEqual(target.writeCount, 0) + } + + func testCollapsedOrMovedSelectionNeverWrites() async throws { + for range in [NSRange(location: 5, length: 0), NSRange(location: 1, length: 5)] { + let target = InsertionTargetStub() + let captured = try target.capture() + target.state.range = range + assertRejected(target, captured: captured) + XCTAssertEqual(target.writeCount, 0) + } + } + + func testChangedDocumentNeverWritesEvenWithSameSelectedText() async throws { + let target = InsertionTargetStub() + let captured = try target.capture() + target.state.value = "Hello changed" + assertRejected(target, captured: captured) + XCTAssertEqual(target.writeCount, 0) + } + + func testReplacesOnlyCapturedUTF16Range() async throws { + let target = InsertionTargetStub(value: "👩🏽‍💻 Hello world", range: NSRange(location: 8, length: 5)) + let captured = try target.capture() + try TextInsertService.shared.replaceSelection(with: "你好👨‍👩‍👧‍👦", captured: captured) + XCTAssertEqual(target.state.value, "👩🏽‍💻 你好👨‍👩‍👧‍👦 world") + XCTAssertEqual(target.writeCount, 1) + } + + func testAXErrorAfterSuccessfulMutationDoesNotWriteTwice() async throws { + let target = InsertionTargetStub() + target.throwAfterWrite = true + let captured = try target.capture() + try TextInsertService.shared.replaceSelection(with: "你好", captured: captured) + XCTAssertEqual(target.state.value, "你好 world") + XCTAssertEqual(target.writeCount, 1) + } + + func testPartialMutationOrNoMutationDoesNotRetry() async throws { + for value in ["part world", "Hello world"] { + let target = InsertionTargetStub() + target.writtenValue = value + let captured = try target.capture() + assertRejected(target, captured: captured) + XCTAssertEqual(target.state.value, value) + XCTAssertEqual(target.writeCount, 1) + } + } + + func testMissingCaptureNeverWrites() { + do { + try TextInsertService.shared.replaceSelection(with: "你好", captured: nil) + XCTFail("未捕获目标时不能插入") + } catch { } + } + + func testClipboardTextMismatchNeverWrites() throws { + let target = InsertionTargetStub() + let captured = try CapturedTextInsertion( + target: target, selection: target.state, selectedText: "Hello", expectedClipboardText: "different" + ) + assertRejected(target, captured: captured) + XCTAssertEqual(target.writeCount, 0) + } + + func testInvalidRangeAndTextMismatchCannotBeCaptured() throws { + for range in [NSRange(location: -1, length: 5), NSRange(location: 0, length: 0), NSRange(location: Int.max, length: 1)] { + let target = InsertionTargetStub(range: range) + XCTAssertThrowsError(try target.capture()) + } + let target = InsertionTargetStub() + XCTAssertThrowsError(try CapturedTextInsertion(target: target, selection: target.state, selectedText: "other")) + } + + private func assertRejected(_ target: InsertionTargetStub, captured: CapturedTextInsertion) { + do { + try TextInsertService.shared.replaceSelection(with: "你好", captured: captured) + XCTFail("目标变化或写入不完整必须报错") + } catch { } + } +} + +@MainActor +private final class InsertionTargetStub: TextInsertionTarget { + var isFocused = true + var state: TextInsertionSnapshot + var writeCount = 0 + var throwAfterWrite = false + var writtenValue: String? + + init(value: String = "Hello world", range: NSRange = NSRange(location: 0, length: 5)) { + state = TextInsertionSnapshot(value: value, range: range) + } + + func capture() throws -> CapturedTextInsertion { + try CapturedTextInsertion(target: self, selection: state, selectedText: "Hello") + } + + func snapshot() throws -> TextInsertionSnapshot { state } + + func replaceSelectedText(_ text: String) throws { + writeCount += 1 + state.value = writtenValue ?? (state.value as NSString).replacingCharacters(in: state.range, with: text) + state.range = NSRange(location: state.range.location + text.utf16.count, length: 0) + if throwAfterWrite { throw TextInsertService.InsertError.insertionNotVerified } + } +} diff --git a/TransFrameTests/TranslationEngineRegistryTests.swift b/TransFrameTests/TranslationEngineRegistryTests.swift new file mode 100644 index 0000000..9a6fb8a --- /dev/null +++ b/TransFrameTests/TranslationEngineRegistryTests.swift @@ -0,0 +1,415 @@ +import XCTest +@testable import TransFrame + +@available(macOS 13.0, *) +final class TranslationEngineRegistryTests: XCTestCase { + func testUnchangedConfigurationReusesProvider() async throws { + let counter = CreationCounter() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { type, config, _ in + let index = await counter.next() + return MockTranslationProvider( + id: "\(type.rawValue)-\(index)", + name: config.options?.modelName ?? "default" + ) + } + ) + let config = TranslationEngineConfig.default(for: .ollama) + + let first = try await registry.createProvider(for: .ollama, config: config) + let second = try await registry.createProvider(for: .ollama, config: config) + let count = await counter.value + + XCTAssertTrue((first as AnyObject) === (second as AnyObject)) + XCTAssertEqual(count, 1) + } + + func testChangedOptionsCreateProviderWithCurrentConfiguration() async throws { + let registry = TranslationEngineRegistry(registerBuiltInProviders: false) + let initial = TranslationEngineConfig.default(for: .ollama) + var updated = initial + updated.options?.modelName = "updated-model" + updated.options?.timeout = 19 + updated.options?.customHeaders = ["X-Test-Configuration": "updated"] + + let first = try await registry.createProvider(for: .ollama, config: initial) + let second = try await registry.createProvider(for: .ollama, config: updated) + + XCTAssertFalse((first as AnyObject) === (second as AnyObject)) + let provider = try XCTUnwrap(second as? LLMTranslationProvider) + let actualConfig = await provider.config + XCTAssertEqual(actualConfig, updated) + } + + func testSameConfigurationCoalescesConcurrentCreation() async throws { + let gate = AsyncGate() + let counter = CreationCounter() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { type, config, _ in + let index = await counter.next() + await gate.enterAndWait() + return MockTranslationProvider( + id: "\(type.rawValue)-\(index)", + name: config.options?.modelName ?? "default" + ) + } + ) + let config = TranslationEngineConfig.default(for: .ollama) + + let firstTask = Task { try await registry.createProvider(for: .ollama, config: config) } + await gate.waitUntilEntered(count: 1) + let secondTask = Task { try await registry.createProvider(for: .ollama, config: config) } + await Task.yield() + await gate.resumeAll() + + let first = try await firstTask.value + let second = try await secondTask.value + let count = await counter.value + + XCTAssertTrue((first as AnyObject) === (second as AnyObject)) + XCTAssertEqual(count, 1) + } + + func testStaleCreationFinishingAfterNewerConfigurationDoesNotOverwriteCache() async throws { + let firstGate = AsyncGate() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { _, config, _ in + let model = config.options?.modelName ?? "default" + if model == "first-model" { + await firstGate.enterAndWait() + } + return MockTranslationProvider(id: model, name: model) + } + ) + var firstConfig = TranslationEngineConfig.default(for: .ollama) + firstConfig.options?.modelName = "first-model" + var secondConfig = firstConfig + secondConfig.options?.modelName = "second-model" + + let staleTask = Task { try await registry.createProvider(for: .ollama, config: firstConfig) } + await firstGate.waitUntilEntered(count: 1) + + let newest = try await registry.createProvider(for: .ollama, config: secondConfig) + await firstGate.resumeAll() + _ = try await staleTask.value + + let cached = await registry.provider(for: .ollama) + let cachedAfter = try await registry.createProvider(for: .ollama, config: secondConfig) + + XCTAssertEqual(cached?.id, "second-model") + XCTAssertTrue((cachedAfter as AnyObject) === (newest as AnyObject)) + } + + func testUnregisterDuringInFlightCreationPreventsLateRegistration() async throws { + let gate = AsyncGate() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { type, _, _ in + await gate.enterAndWait() + return MockTranslationProvider(id: type.rawValue, name: type.rawValue) + } + ) + let config = TranslationEngineConfig.default(for: .ollama) + + let task = Task { try await registry.createProvider(for: .ollama, config: config) } + await gate.waitUntilEntered(count: 1) + await registry.unregister(.ollama) + await gate.resumeAll() + _ = try await task.value + + let cached = await registry.provider(for: .ollama) + XCTAssertNil(cached) + } + + func testManualRegisterDuringInFlightCreationPreservesManualOverride() async throws { + let gate = AsyncGate() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { _, _, _ in + await gate.enterAndWait() + return MockTranslationProvider(id: "factory", name: "Factory") + } + ) + let config = TranslationEngineConfig.default(for: .ollama) + let manual = MockTranslationProvider(id: "manual", name: "Manual") + + let task = Task { try await registry.createProvider(for: .ollama, config: config) } + await gate.waitUntilEntered(count: 1) + await registry.register(manual, for: .ollama) + await gate.resumeAll() + _ = try await task.value + + let cached = await registry.provider(for: .ollama) + let resolved = try await registry.createProvider(for: .ollama, config: config) + XCTAssertEqual(cached?.id, "manual") + XCTAssertEqual(resolved.id, "manual") + } + + func testCompatibleConfigurationUpdatesAndCoalescesConcurrentCreation() async throws { + let gate = AsyncGate() + let counter = CreationCounter() + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + compatibleProviderFactory: { compatibleConfig, engineConfig in + _ = await counter.next() + if compatibleConfig.displayName == "Initial" { + await gate.enterAndWait() + } + return try await CompatibleTranslationProvider( + config: engineConfig, + compatibleConfig: compatibleConfig, + keychain: .shared + ) + } + ) + let id = UUID() + let initial = compatibleConfig(id: id, displayName: "Initial") + var renamed = initial + renamed.displayName = "Renamed" + + let firstTask = Task { try await registry.createCompatibleProvider(compatibleConfig: initial) } + await gate.waitUntilEntered(count: 1) + let coalescedTask = Task { try await registry.createCompatibleProvider(compatibleConfig: initial) } + await Task.yield() + await gate.resumeAll() + + let first = try await firstTask.value + let coalesced = try await coalescedTask.value + let countAfterCoalesce = await counter.value + let second = try await registry.createCompatibleProvider(compatibleConfig: renamed) + + XCTAssertTrue(first === coalesced) + XCTAssertEqual(countAfterCoalesce, 1) + XCTAssertFalse(first === second) + XCTAssertEqual(second.name, "Renamed") + } + + func testCustomEngineRefreshesWhenSavedCompatibleConfigOrOptionsChange() async throws { + let id = UUID() + let savedStore = CompatibleConfigStore([ + compatibleConfig(id: id, displayName: "Instance V1", modelName: "model-v1") + ]) + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + compatibleConfigsProvider: { await savedStore.configs() } + ) + let encodedSelection = String( + decoding: try JSONEncoder().encode(compatibleConfig(id: id, displayName: "Stale JSON", modelName: "stale")), + as: UTF8.self + ) + let initial = TranslationEngineConfig( + id: .custom, + isEnabled: true, + options: EngineOptions(timeout: 15, temperature: 0.2), + customName: encodedSelection + ) + + let first = try await registry.createProvider(for: .custom, config: initial) + let cachedSame = try await registry.createProvider(for: .custom, config: initial) + XCTAssertTrue((first as AnyObject) === (cachedSame as AnyObject)) + XCTAssertEqual(first.name, "Instance V1") + + await savedStore.update([ + compatibleConfig(id: id, displayName: "Instance V2", modelName: "model-v2") + ]) + let afterSavedUpdate = try await registry.createProvider(for: .custom, config: initial) + XCTAssertFalse((first as AnyObject) === (afterSavedUpdate as AnyObject)) + XCTAssertEqual(afterSavedUpdate.name, "Instance V2") + + var updatedOptions = initial + updatedOptions.options?.timeout = 45 + let afterOptionsUpdate = try await registry.createProvider(for: .custom, config: updatedOptions) + XCTAssertFalse((afterSavedUpdate as AnyObject) === (afterOptionsUpdate as AnyObject)) + let compatible = try XCTUnwrap(afterOptionsUpdate as? CompatibleTranslationProvider) + XCTAssertEqual(compatible.configurationSnapshot.engineConfig.options?.timeout, 45) + } + + func testConnectionVerificationAndTestingUseCurrentEngineConfiguration() async throws { + let configStore = EngineConfigStore([ + .openai: TranslationEngineConfig( + id: .openai, + isEnabled: true, + options: EngineOptions(baseURL: "https://initial.example/v1", modelName: "gpt-initial") + ) + ]) + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { _, config, _ in + let model = config.options?.modelName ?? "missing" + let isUpdated = model == "gpt-updated" + return MockTranslationProvider( + id: model, + name: model, + batchResults: [ + TranslationResult( + sourceText: "Hello", + translatedText: "Translated by \(model)", + sourceLanguage: "en", + targetLanguage: "zh-Hans" + ) + ], + translateError: isUpdated ? nil : TranslationProviderError.connectionFailed("stale config"), + checkConnectionResult: isUpdated + ) + } + ) + let service = TranslationService( + registry: registry, + engineConfigsProvider: { await configStore.configs() } + ) + + let initialConnection = await service.testConnection(for: .openai) + XCTAssertFalse(initialConnection) + + await configStore.update([ + .openai: TranslationEngineConfig( + id: .openai, + isEnabled: true, + options: EngineOptions(baseURL: "https://updated.example/v1", modelName: "gpt-updated") + ) + ]) + + try await service.verifyConnection(for: .openai) + let updatedConnection = await service.testConnection(for: .openai) + let segments = try await service.translate( + segments: ["Hello"], + to: "zh-Hans", + preferredEngine: .openai, + from: "en", + fallbackEnabled: false + ) + + XCTAssertTrue(updatedConnection) + XCTAssertEqual(segments.map(\.translated), ["Translated by gpt-updated"]) + } + + func testConnectionTestWithUnsavedConfigDoesNotReplaceTranslationProvider() async throws { + let registry = TranslationEngineRegistry( + registerBuiltInProviders: false, + providerFactory: { _, config, _ in + let model = config.options?.modelName ?? "missing" + return MockTranslationProvider( + id: model, + name: model, + batchResults: [TranslationResult(sourceText: "Hello", translatedText: model, sourceLanguage: "en", targetLanguage: "zh-Hans")], + checkConnectionResult: true + ) + } + ) + let service = TranslationService(registry: registry, engineConfigsProvider: { + [.openai: TranslationEngineConfig(id: .openai, isEnabled: true, options: EngineOptions(modelName: "saved"))] + }) + let _: [BilingualSegment] = try await service.translate( + segments: ["Hello"], to: "zh-Hans", preferredEngine: .openai, from: "en", fallbackEnabled: false + ) + try await service.verifyConnection(for: .openai, config: TranslationEngineConfig( + id: .openai, isEnabled: true, options: EngineOptions(modelName: "unsaved") + )) + let segments: [BilingualSegment] = try await service.translate( + segments: ["Hello"], to: "zh-Hans", preferredEngine: .openai, from: "en", fallbackEnabled: false + ) + XCTAssertEqual(segments.map(\.translated), ["saved"]) + } + + private func compatibleConfig( + id: UUID = UUID(), + displayName: String = "Test", + modelName: String = "model" + ) -> CompatibleTranslationProvider.CompatibleConfig { + .init( + id: id, + displayName: displayName, + baseURL: "http://localhost:8000/v1", + modelName: modelName, + hasAPIKey: false + ) + } +} + +private actor CreationCounter { + private(set) var value = 0 + + func next() -> Int { + value += 1 + return value + } +} + +private actor CompatibleConfigStore { + private var stored: [CompatibleTranslationProvider.CompatibleConfig] + + init(_ stored: [CompatibleTranslationProvider.CompatibleConfig]) { + self.stored = stored + } + + func configs() -> [CompatibleTranslationProvider.CompatibleConfig] { + stored + } + + func update(_ newConfigs: [CompatibleTranslationProvider.CompatibleConfig]) { + stored = newConfigs + } +} + +private actor EngineConfigStore { + private var stored: [TranslationEngineType: TranslationEngineConfig] + + init(_ stored: [TranslationEngineType: TranslationEngineConfig]) { + self.stored = stored + } + + func configs() -> [TranslationEngineType: TranslationEngineConfig] { + stored + } + + func update(_ newConfigs: [TranslationEngineType: TranslationEngineConfig]) { + stored = newConfigs + } +} + +private actor AsyncGate { + private var enteredCount = 0 + private var enteredWaiters: [(target: Int, continuation: CheckedContinuation)] = [] + private var releaseWaiters: [CheckedContinuation] = [] + private var isOpen = false + + func enterAndWait() async { + enteredCount += 1 + flushEnteredWaiters() + guard !isOpen else { return } + await withCheckedContinuation { continuation in + releaseWaiters.append(continuation) + } + } + + func waitUntilEntered(count: Int) async { + guard enteredCount < count else { return } + await withCheckedContinuation { continuation in + enteredWaiters.append((count, continuation)) + } + } + + func resumeAll() { + isOpen = true + let waiters = releaseWaiters + releaseWaiters.removeAll() + for waiter in waiters { + waiter.resume() + } + } + + private func flushEnteredWaiters() { + var remaining: [(target: Int, continuation: CheckedContinuation)] = [] + for waiter in enteredWaiters { + if enteredCount >= waiter.target { + waiter.continuation.resume() + } else { + remaining.append(waiter) + } + } + enteredWaiters = remaining + } +} diff --git a/TransFrameTests/URLRequestBody.swift b/TransFrameTests/URLRequestBody.swift new file mode 100644 index 0000000..356c865 --- /dev/null +++ b/TransFrameTests/URLRequestBody.swift @@ -0,0 +1,19 @@ +import Foundation + +extension URLRequest { + var requestBody: Data? { + if let httpBody { return httpBody } + guard let stream = httpBodyStream else { return nil } + stream.open() + defer { stream.close() } + var result = Data() + var buffer = [UInt8](repeating: 0, count: 4096) + while stream.hasBytesAvailable { + let count = stream.read(&buffer, maxLength: buffer.count) + if count < 0 { return nil } + if count == 0 { break } + result.append(buffer, count: count) + } + return result + } +} diff --git a/TransFrameTests/VLMContinuationSafetyTests.swift b/TransFrameTests/VLMContinuationSafetyTests.swift new file mode 100644 index 0000000..15f8208 --- /dev/null +++ b/TransFrameTests/VLMContinuationSafetyTests.swift @@ -0,0 +1,86 @@ +import CoreGraphics +import XCTest +@testable import TransFrame + +final class VLMContinuationSafetyTests: XCTestCase { + func testOpenAIContinuationKeepsImageContextAndExhaustionFails() async throws { + let image = Self.makeImage() + let requests = VLMRequestBox() + let sessionConfiguration = URLSessionConfiguration.ephemeral + sessionConfiguration.protocolClasses = [VLMContinuationURLProtocol.self] + let session = URLSession(configuration: sessionConfiguration) + defer { session.invalidateAndCancel() } + VLMContinuationURLProtocol.handler = { request in + requests.append(request) + return Data(#"{"choices":[{"message":{"content":"{\"segments\":[{\"text\":\"A\",\"boundingBox\":{\"x\":0.1,\"y\":0.1,\"width\":0.2,\"height\":0.1},\"confidence\":0.9}]}"},"finish_reason":"length"}]}"#.utf8) + } + let provider = OpenAIVLMProvider(apiKey: "test", baseURL: URL(string: "http://vlm.invalid/v1")!, session: session) + do { + _ = try await provider.analyze(image: image) + XCTFail("续写耗尽不能把部分结果当完整结果返回") + } catch { } + XCTAssertEqual(requests.items.count, 3) + for request in requests.items { + let body = try XCTUnwrap(request.requestBody) + let text = try XCTUnwrap(String(data: body, encoding: .utf8)).replacingOccurrences(of: "\\/", with: "/") + XCTAssertTrue(text.contains("data:image/jpeg;base64,"), "每轮续写都必须保留原图上下文") + XCTAssertFalse(text.contains("image omitted")) + } + } + + func testClaudeContinuationKeepsImageContextAndExhaustionFails() async throws { + let image = Self.makeImage() + let requests = VLMRequestBox() + let sessionConfiguration = URLSessionConfiguration.ephemeral + sessionConfiguration.protocolClasses = [VLMContinuationURLProtocol.self] + let session = URLSession(configuration: sessionConfiguration) + defer { session.invalidateAndCancel() } + VLMContinuationURLProtocol.handler = { request in + requests.append(request) + return Data(#"{"content":[{"type":"text","text":"{\"segments\":[{\"text\":\"A\",\"boundingBox\":{\"x\":0.1,\"y\":0.1,\"width\":0.2,\"height\":0.1},\"confidence\":0.9}]}"}],"stop_reason":"max_tokens"}"#.utf8) + } + let provider = ClaudeVLMProvider(apiKey: "test", baseURL: URL(string: "http://vlm.invalid")!, session: session) + do { + _ = try await provider.analyze(image: image) + XCTFail("Claude 续写耗尽不能把部分结果当完整结果返回") + } catch { } + XCTAssertEqual(requests.items.count, 3) + for request in requests.items { + let body = try XCTUnwrap(request.requestBody) + let text = try XCTUnwrap(String(data: body, encoding: .utf8)) + XCTAssertTrue(text.contains("\"type\":\"base64\"")) + XCTAssertFalse(text.contains("image omitted")) + } + } + + private static func makeImage() -> CGImage { + let context = CGContext( + data: nil, width: 32, height: 32, bitsPerComponent: 8, + bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, + bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue + )! + context.setFillColor(CGColor(red: 0.2, green: 0.4, blue: 0.6, alpha: 1)) + context.fill(CGRect(x: 0, y: 0, width: 32, height: 32)) + return context.makeImage()! + } +} + +private final class VLMRequestBox: @unchecked Sendable { + private let lock = NSLock() + private var storage: [URLRequest] = [] + var items: [URLRequest] { lock.withLock { storage } } + func append(_ request: URLRequest) { lock.withLock { storage.append(request) } } +} + +private final class VLMContinuationURLProtocol: URLProtocol { + nonisolated(unsafe) static var handler: (URLRequest) -> Data = { _ in Data() } + override class func canInit(with request: URLRequest) -> Bool { request.url?.host == "vlm.invalid" } + override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } + override func startLoading() { + let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)! + client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) + client?.urlProtocol(self, didLoad: Self.handler(request)) + client?.urlProtocolDidFinishLoading(self) + } + override func stopLoading() {} +} From b4147863f7cea5f3a85466db1c3610bebe3e42d8 Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 27 Oct 2026 19:42:37 +0800 Subject: [PATCH 2/3] feat(translation): panel-only bilingual results with line grouping and 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 --- .../Capture/InPlaceResultsPanel.swift | 120 ++++++++++---- .../Capture/SelectionOverlayWindow.swift | 30 ---- .../Preview/PreviewAnnotatedImageView.swift | 6 - .../Preview/PreviewResultsPanel.swift | 119 ++++++++++---- .../BilingualOverlayView.swift | 77 --------- .../Pipelines/TranslationRenderPipeline.swift | 11 +- .../Translation/VisualLineGrouping.swift | 116 ++++++++++++++ TransFrame/Services/TranslationEngine.swift | 37 +++++ TransFrameTests/PRDAcceptanceTests.swift | 38 ++--- .../TranslationConfigurationTests.swift | 15 ++ TransFrameTests/VisualLineGroupingTests.swift | 151 ++++++++++++++++++ 11 files changed, 510 insertions(+), 210 deletions(-) delete mode 100644 TransFrame/Features/TranslationFlow/BilingualOverlayView.swift create mode 100644 TransFrame/Services/Translation/VisualLineGrouping.swift create mode 100644 TransFrameTests/VisualLineGroupingTests.swift diff --git a/TransFrame/Features/Capture/InPlaceResultsPanel.swift b/TransFrame/Features/Capture/InPlaceResultsPanel.swift index 697e5e2..a2cc8fe 100644 --- a/TransFrame/Features/Capture/InPlaceResultsPanel.swift +++ b/TransFrame/Features/Capture/InPlaceResultsPanel.swift @@ -89,16 +89,20 @@ struct InPlaceResultsPanel: View { .buttonStyle(.plain) .customTooltip(String(localized: "preview.panel.rerun")) - Button { - copy(currentTabText) - } label: { - Image(systemName: "doc.on.doc") - .font(.system(size: 11)) - .frame(width: 20, height: 20) + if model.activeTab == .translate { + translationCopyButtons + } else { + Button { + copy(currentTabText) + } label: { + Image(systemName: "doc.on.doc") + .font(.system(size: 11)) + .frame(width: 20, height: 20) + } + .buttonStyle(.plain) + .disabled(currentTabText.isEmpty) + .customTooltip(String(localized: "preview.panel.copyAll")) } - .buttonStyle(.plain) - .disabled(currentTabText.isEmpty) - .customTooltip(String(localized: "preview.panel.copyAll")) Button { model.onClose?() @@ -164,34 +168,42 @@ struct InPlaceResultsPanel: View { } } + /// 国旗横排一键切换目标语言,避免下拉二次点击。 private var targetLanguagePicker: some View { - HStack(spacing: 6) { - Text(String(localized: "preview.panel.targetLanguage")) - .font(.caption) - .foregroundStyle(.secondary) - Menu { - ForEach(Self.targetLanguages, id: \.self) { language in - Button { - guard AppSettings.shared.translationTargetLanguage != language else { return } - AppSettings.shared.translationTargetLanguage = language - model.onRerun?() - } label: { - if AppSettings.shared.translationTargetLanguage == language { - Label(language.localizedName, systemImage: "checkmark") - } else { - Text(language.localizedName) - } - } - } - } label: { - Text(AppSettings.shared.translationTargetLanguage?.localizedName ?? "—") - .font(.caption) + HStack(spacing: 4) { + ForEach(Self.targetLanguages, id: \.self) { language in + languageFlagButton(for: language) } - .controlSize(.small) - .fixedSize() } } + 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?() + } label: { + Text(language.flagEmoji) + .font(.system(size: 16)) + .frame(width: 27, height: 22) + } + .buttonStyle(.plain) + .background( + isSelected + ? Color.accentColor.opacity(0.28) + : Color.primary.opacity(0.06) + ) + .clipShape(RoundedRectangle(cornerRadius: 5)) + .overlay( + RoundedRectangle(cornerRadius: 5) + .strokeBorder(isSelected ? Color.accentColor : Color.clear, lineWidth: 1) + ) + .customTooltip(language.localizedName) + .accessibilityLabel(Text(language.localizedName)) + .accessibilityAddTraits(isSelected ? [.isSelected] : []) + } + // MARK: - Helpers private var currentTabText: String { @@ -234,10 +246,52 @@ struct InPlaceResultsPanel: View { NSPasteboard.general.setString(text, forType: .string) } + /// 翻译结果三键复制:原文 / 译文 / 全部(原文译文交替,与面板显示一致)。 + private var translationCopyButtons: some View { + HStack(spacing: 3) { + 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") + ) + panelCopyButton( + title: String(localized: "preview.panel.copyAll"), + text: ResultsTextBuilder.translationText(segments: model.segments).string + ) + } + } + + private func panelCopyButton(title: String, text: String) -> some View { + Button { + copy(text) + flashMessage(title) + } label: { + HStack(spacing: 3) { + Image(systemName: "doc.on.doc") + .font(.system(size: 10)) + Text(title) + .font(.caption2) + } + .padding(.horizontal, 6) + .frame(height: 20) + } + .buttonStyle(.plain) + .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 4)) + .disabled(text.isEmpty) + .customTooltip(title) + } + /// Shows the copy feedback; a newer flash invalidates older timers, so /// rapid line clicks don't clear each other's message early. private func flashCopy(line: String) { - copiedFlash = String(localized: "preview.panel.copyLine") + ": " + line + flashMessage(String(localized: "preview.panel.copyLine") + ": " + line) + } + + private func flashMessage(_ message: String) { + copiedFlash = message flashGeneration += 1 let generation = flashGeneration DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { diff --git a/TransFrame/Features/Capture/SelectionOverlayWindow.swift b/TransFrame/Features/Capture/SelectionOverlayWindow.swift index 7c0db90..7b8a2de 100644 --- a/TransFrame/Features/Capture/SelectionOverlayWindow.swift +++ b/TransFrame/Features/Capture/SelectionOverlayWindow.swift @@ -329,9 +329,6 @@ final class SelectionOverlayView: NSView, SelectionToolbarDelegate, NSTextFieldD if isBeautifyEnabled { drawBeautifyPreview(for: sel, context: context) } - if case .selected = state { - drawBilingualOverlay(for: sel) - } drawSelectionBox(sel, context: context) switch state { case .selected: @@ -415,33 +412,6 @@ final class SelectionOverlayView: NSView, SelectionToolbarDelegate, NSTextFieldD context.restoreGState() } - /// Draws each translated segment as a strip just below its original - /// text inside the selection — in-image bilingual view; the original - /// screenshot stays fully visible. Only while the translate tab is active. - private func drawBilingualOverlay(for selRect: CGRect) { - guard resultsModel.isVisible, - resultsModel.activeTab == .translate, - !resultsModel.segments.isEmpty else { return } - - NSGraphicsContext.saveGraphicsState() - defer { NSGraphicsContext.restoreGraphicsState() } - NSBezierPath(rect: selRect).addClip() - for segment in resultsModel.segments { - guard let strip = BilingualOverlayLayout.translationStrip(for: segment, in: selRect.size) else { continue } - let attr = NSAttributedString(string: segment.translated, attributes: [ - .font: NSFont.systemFont(ofSize: strip.fontSize, weight: .medium), - .foregroundColor: NSColor.white - ]) - let frame = strip.frame.offsetBy(dx: selRect.minX, dy: selRect.minY) - NSColor.black.withAlphaComponent(0.78).setFill() - NSBezierPath(roundedRect: frame, xRadius: 3, yRadius: 3).fill() - attr.draw( - with: strip.textFrame.offsetBy(dx: selRect.minX, dy: selRect.minY), - options: [.usesLineFragmentOrigin, .usesFontLeading] - ) - } - } - private func drawSelectionBox(_ rect: CGRect, context: CGContext) { context.saveGState() context.setStrokeColor(NSColor.controlAccentColor.cgColor) diff --git a/TransFrame/Features/Preview/PreviewAnnotatedImageView.swift b/TransFrame/Features/Preview/PreviewAnnotatedImageView.swift index 30b8460..8e87e37 100644 --- a/TransFrame/Features/Preview/PreviewAnnotatedImageView.swift +++ b/TransFrame/Features/Preview/PreviewAnnotatedImageView.swift @@ -33,12 +33,6 @@ struct PreviewAnnotatedImageView: View { "Screenshot preview, \(viewModel.dimensionsText), from \(viewModel.displayName)" )) - if !viewModel.isTranslating && viewModel.hasTranslationResults { - BilingualOverlayView(segments: viewModel.bilingualSegments) - .frame(width: imageSize.width, height: imageSize.height) - .clipped() - } - AnnotationCanvas( annotations: viewModel.annotations, currentAnnotation: viewModel.currentAnnotation, diff --git a/TransFrame/Features/Preview/PreviewResultsPanel.swift b/TransFrame/Features/Preview/PreviewResultsPanel.swift index a4e11c0..36e060d 100644 --- a/TransFrame/Features/Preview/PreviewResultsPanel.swift +++ b/TransFrame/Features/Preview/PreviewResultsPanel.swift @@ -57,7 +57,11 @@ struct PreviewResultsPanel: View { // 无 tab 切换:内容随工具栏最后一次触发的动作而定。 rerunButton - copyAllButton + if viewModel.resultsPanelTab == .translate { + translationCopyButtons + } else { + copyAllButton + } closeButton } .padding(.horizontal, 10) @@ -187,43 +191,50 @@ struct PreviewResultsPanel: View { } } - /// Quick target-language switcher; changing it re-runs the translation + /// 国旗横排一键切换目标语言,避免下拉二次点击;切换后重跑翻译。 private var targetLanguagePicker: some View { - HStack(spacing: 6) { - Text(String(localized: "preview.panel.targetLanguage")) - .font(.caption) - .foregroundStyle(.secondary) - Menu { - ForEach(Self.targetLanguages, id: \.self) { language in - Button { - guard selectedTargetLanguage != language else { return } - selectedTargetLanguage = language - AppSettings.shared.translationTargetLanguage = language - if viewModel.hasTranslationResults || viewModel.ocrResult != nil { - viewModel.performTranslation() - } - } label: { - if selectedTargetLanguage == language { - Label(language.localizedName, systemImage: "checkmark") - } else { - Text(language.localizedName) - } - } - } - } label: { - Text(selectedTargetLanguage?.localizedName ?? "—") - .font(.caption) + HStack(spacing: 4) { + ForEach(Self.targetLanguages, id: \.self) { language in + languageFlagButton(for: language) } - .controlSize(.small) - .fixedSize() - .onAppear { - if selectedTargetLanguage == nil { - selectedTargetLanguage = AppSettings.shared.translationTargetLanguage - } + } + .onAppear { + if selectedTargetLanguage == nil { + selectedTargetLanguage = AppSettings.shared.translationTargetLanguage } } } + private func languageFlagButton(for language: TranslationLanguage) -> some View { + let isSelected = selectedTargetLanguage == language + return Button { + guard selectedTargetLanguage != language else { return } + selectedTargetLanguage = language + AppSettings.shared.translationTargetLanguage = language + if viewModel.hasTranslationResults || viewModel.ocrResult != nil { + viewModel.performTranslation() + } + } label: { + Text(language.flagEmoji) + .font(.system(size: 16)) + .frame(width: 27, height: 22) + } + .buttonStyle(.plain) + .background( + isSelected + ? Color.accentColor.opacity(0.28) + : Color.primary.opacity(0.06) + ) + .clipShape(RoundedRectangle(cornerRadius: 5)) + .overlay( + RoundedRectangle(cornerRadius: 5) + .strokeBorder(isSelected ? Color.accentColor : Color.clear, lineWidth: 1) + ) + .customTooltip(language.localizedName) + .accessibilityLabel(Text(language.localizedName)) + .accessibilityAddTraits(isSelected ? [.isSelected] : []) + } + // MARK: - Helpers private var currentTabText: String { @@ -249,10 +260,52 @@ struct PreviewResultsPanel: View { NSPasteboard.general.setString(text, forType: .string) } + /// 翻译结果三键复制:原文 / 译文 / 全部(原文译文交替,与面板显示一致)。 + private var translationCopyButtons: some View { + HStack(spacing: 3) { + 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") + ) + panelCopyButton( + title: String(localized: "preview.panel.copyAll"), + text: ResultsTextBuilder.translationText(segments: viewModel.bilingualSegments).string + ) + } + } + + private func panelCopyButton(title: String, text: String) -> some View { + Button { + copyToClipboard(text) + flashMessage(title) + } label: { + HStack(spacing: 3) { + Image(systemName: "doc.on.doc") + .font(.system(size: 10)) + Text(title) + .font(.caption2) + } + .padding(.horizontal, 6) + .frame(height: 20) + } + .buttonStyle(.plain) + .background(Color.primary.opacity(0.06), in: RoundedRectangle(cornerRadius: 4)) + .disabled(text.isEmpty) + .customTooltip(title) + } + /// Shows the copy feedback; a newer flash invalidates older timers, so /// rapid line clicks don't clear each other's message early. private func flashCopy(line: String) { - copiedFlash = String(localized: "preview.panel.copyLine") + ": " + line + flashMessage(String(localized: "preview.panel.copyLine") + ": " + line) + } + + private func flashMessage(_ message: String) { + copiedFlash = message flashGeneration += 1 let generation = flashGeneration DispatchQueue.main.asyncAfter(deadline: .now() + 1.2) { diff --git a/TransFrame/Features/TranslationFlow/BilingualOverlayView.swift b/TransFrame/Features/TranslationFlow/BilingualOverlayView.swift deleted file mode 100644 index 5d0b60a..0000000 --- a/TransFrame/Features/TranslationFlow/BilingualOverlayView.swift +++ /dev/null @@ -1,77 +0,0 @@ -import SwiftUI -import AppKit - -/// 在截图画布上显示译文,原文由底下的原始截图保留。 -struct BilingualOverlayView: View { - let segments: [BilingualSegment] - - var body: some View { - Canvas { context, size in - for segment in segments { - guard let strip = BilingualOverlayLayout.translationStrip(for: segment, in: size) else { continue } - let text = Text(segment.translated) - .font(.system(size: strip.fontSize, weight: .medium)) - .foregroundStyle(.white) - context.fill( - Path(roundedRect: strip.frame, cornerRadius: 3), - with: .color(.black.opacity(0.78)) - ) - context.draw(context.resolve(text), in: strip.textFrame) - } - } - .allowsHitTesting(false) - .accessibilityHidden(true) - } -} - -enum BilingualOverlayLayout { - struct Anchor: Equatable { - let x: CGFloat - let y: CGFloat - let maxWidth: CGFloat - let fontSize: CGFloat - } - - struct Strip { - let frame: CGRect - let fontSize: CGFloat - - var textFrame: CGRect { frame.insetBy(dx: 4, dy: 2) } - } - - /// TextSegment 使用左上角原点;译文从原文下边缘开始,按画布宽度换行。 - static func translationAnchor(for boundingBox: CGRect, in displaySize: CGSize) -> Anchor { - let x = max(0, min(boundingBox.minX * displaySize.width, displaySize.width)) - let belowY = boundingBox.maxY * displaySize.height + 1 - let stripWidth = min(max(boundingBox.width * displaySize.width, 60), max(0, displaySize.width - x)) - let fontSize = min(max(boundingBox.height * displaySize.height * 0.85, 10), 16) - let estimatedStripHeight = fontSize * 1.6 + 4 - let y: CGFloat - if belowY + estimatedStripHeight < displaySize.height { - y = belowY - } else { - y = max(0, boundingBox.minY * displaySize.height - estimatedStripHeight - 1) - } - return Anchor(x: x, y: y, maxWidth: stripWidth, fontSize: fontSize) - } - - /// AppKit 与 SwiftUI 共用实际文字测量,避免换行后被条带高度裁掉。 - static func translationStrip(for segment: BilingualSegment, in displaySize: CGSize) -> Strip? { - let box = segment.boundingBox - guard displaySize.width > 0, displaySize.height > 0, - box.minX.isFinite, box.minY.isFinite, box.width.isFinite, box.height.isFinite, - box.width > 0, box.height > 0, - !segment.translated.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return nil } - let anchor = translationAnchor(for: box, in: displaySize) - guard anchor.maxWidth > 8, anchor.y < displaySize.height else { return nil } - let measured = (segment.translated as NSString).boundingRect( - with: CGSize(width: anchor.maxWidth - 8, height: CGFloat.greatestFiniteMagnitude), - options: [.usesLineFragmentOrigin, .usesFontLeading], - attributes: [.font: NSFont.systemFont(ofSize: anchor.fontSize, weight: .medium)] - ) - return Strip( - frame: CGRect(x: anchor.x, y: anchor.y, width: anchor.maxWidth, height: ceil(measured.height) + 4), - fontSize: anchor.fontSize - ) - } -} diff --git a/TransFrame/Services/Pipelines/TranslationRenderPipeline.swift b/TransFrame/Services/Pipelines/TranslationRenderPipeline.swift index 0fb09f5..582d196 100644 --- a/TransFrame/Services/Pipelines/TranslationRenderPipeline.swift +++ b/TransFrame/Services/Pipelines/TranslationRenderPipeline.swift @@ -29,7 +29,10 @@ actor TranslationRenderPipeline { do { return try await recorder.measure(stage: .translation, operationID: context.operationID) { try Task.checkCancellation() - let texts = analysisResult.segments.map(\.text) + // Merge same-line fragments first: every visual line is + // translated as one unit and gets one overlay anchor. + let groupedSegments = VisualLineGrouping.mergeLines(in: analysisResult.segments) + let texts = groupedSegments.map(\.text) let translatedSegments = try await translationService.translate( segments: texts, to: config.targetLanguage, @@ -42,13 +45,13 @@ actor TranslationRenderPipeline { sceneBindings: config.sceneBindings ) - guard translatedSegments.count == analysisResult.segments.count else { + guard translatedSegments.count == groupedSegments.count else { throw PipelineError.translationFailed( - "Translation segment count mismatch: expected \(analysisResult.segments.count), got \(translatedSegments.count)" + "Translation segment count mismatch: expected \(groupedSegments.count), got \(translatedSegments.count)" ) } - return zip(analysisResult.segments, translatedSegments).map { original, translated in + return zip(groupedSegments, translatedSegments).map { original, translated in BilingualSegment( segment: original, translatedText: translated.translated, diff --git a/TransFrame/Services/Translation/VisualLineGrouping.swift b/TransFrame/Services/Translation/VisualLineGrouping.swift new file mode 100644 index 0000000..25e30fd --- /dev/null +++ b/TransFrame/Services/Translation/VisualLineGrouping.swift @@ -0,0 +1,116 @@ +import CoreGraphics +import Foundation + +/// Merges OCR/VLM fragments that sit on the same visual line into complete +/// line segments before translation. +/// +/// VLM analysis frequently splits inline code (`main`, `DESIGMA_PLUGINS_TOKEN`) +/// into standalone fragments. Translated independently they produce misplaced +/// overlay strips and out-of-context mistranslations ("main" → “主要”). +/// Grouping first gives every visual line a single text and a single anchor. +enum VisualLineGrouping { + /// Minimum Y-projection overlap (fraction of the shorter box height) for + /// a fragment to join a line. + static let verticalOverlapThreshold: CGFloat = 0.5 + /// Maximum horizontal gap between a fragment and the line it joins, as a + /// multiple of the average line height. Keeps table columns and + /// multi-column layouts from being glued into one line. + static let horizontalGapFactor: CGFloat = 3.0 + + /// Groups `segments` into visual lines, ordered top-to-bottom, left-to-right. + static func mergeLines(in segments: [TextSegment]) -> [TextSegment] { + guard segments.count > 1 else { return segments } + + let sorted = segments.sorted { lhs, rhs in + let lhsY = lhs.boundingBox.midY + let rhsY = rhs.boundingBox.midY + if abs(lhsY - rhsY) > 1e-6 { return lhsY < rhsY } + return lhs.boundingBox.minX < rhs.boundingBox.minX + } + + 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]) + } + } + return lines.compactMap(mergedSegment) + } + + // MARK: - Internals + + /// A fragment joins a line when it overlaps the line vertically (Y + /// projection ≥ 50% of the shorter box) and sits within a small + /// horizontal gap of the line's current X span. + private static func belongsToSameLine(_ segment: TextSegment, lineFragments: [TextSegment]) -> Bool { + let box = segment.boundingBox + guard box.width > 0, box.height > 0 else { return false } + let lineBox = union(of: lineFragments.map(\.boundingBox)) + guard lineBox.width > 0, lineBox.height > 0 else { return false } + + let overlap = min(box.maxY, lineBox.maxY) - max(box.minY, lineBox.minY) + guard overlap > 0, + overlap / min(box.height, lineBox.height) >= verticalOverlapThreshold else { return false } + + let gap = max(0, max(box.minX, lineBox.minX) - min(box.maxX, lineBox.maxX)) + let averageHeight = (box.height + lineBox.height) / 2 + return gap <= horizontalGapFactor * averageHeight + } + + /// Merges one line's fragments (left-to-right) into a single segment: + /// joined text, union bounding box, averaged confidence. + private static func mergedSegment(from fragments: [TextSegment]) -> TextSegment? { + 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 } + let joinedText = ordered.map(\.text).reduce(into: "") { partial, next in + partial = joinText(partial, next) + } + let box = union(of: ordered.map(\.boundingBox)) + let confidence = ordered.map(\.confidence).reduce(0, +) / Float(ordered.count) + return TextSegment( + text: joinedText, + boundingBox: box, + confidence: confidence, + isUserEdited: ordered.contains(where: \.isUserEdited) + ) + } + + /// Joins two fragments: CJK-adjacent text glues directly, everything + /// else gets a single space. + 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 + } + + private static func isCJK(_ character: Character) -> Bool { + character.unicodeScalars.allSatisfy { scalar in + (0x4E00...0x9FFF).contains(scalar.value) + || (0x3400...0x4DBF).contains(scalar.value) + || (0x3000...0x303F).contains(scalar.value) + || (0xFF00...0xFFEF).contains(scalar.value) + } + } + + private static func union(of boxes: [CGRect]) -> CGRect { + boxes.reduce(CGRect.null) { $0.union($1) } + } +} + +extension ScreenAnalysisResult { + /// Translation prep step after `filteredForTranslation()`: merges + /// same-line fragments so each visual line is translated as one unit. + func mergingVisualLines() -> ScreenAnalysisResult { + let merged = VisualLineGrouping.mergeLines(in: segments) + return ScreenAnalysisResult( + segments: merged, + rawSegments: rawSegments, + filteredOutSegments: filteredOutSegments, + imageSize: imageSize + ) + } +} diff --git a/TransFrame/Services/TranslationEngine.swift b/TransFrame/Services/TranslationEngine.swift index 8909924..e979f96 100644 --- a/TransFrame/Services/TranslationEngine.swift +++ b/TransFrame/Services/TranslationEngine.swift @@ -59,6 +59,43 @@ enum TranslationLanguage: String, CaseIterable, Sendable, Codable { ) } + /// Flag emoji for quick visual pickers. Verified rendering as real flag + /// glyphs on macOS (not regional-indicator letters). + var flagEmoji: String { + switch self { + case .auto: return "🌐" + case .english: return "🇬🇧" + case .chineseSimplified: return "🇨🇳" + case .chineseTraditional: return "🇹🇼" + case .japanese: return "🇯🇵" + case .korean: return "🇰🇷" + case .french: return "🇫🇷" + case .german: return "🇩🇪" + case .spanish: return "🇪🇸" + case .italian: return "🇮🇹" + case .portuguese: return "🇵🇹" + case .russian: return "🇷🇺" + case .arabic: return "🇸🇦" + case .hindi: return "🇮🇳" + case .thai: return "🇹🇭" + case .vietnamese: return "🇻🇳" + case .dutch: return "🇳🇱" + case .polish: return "🇵🇱" + case .turkish: return "🇹🇷" + case .ukrainian: return "🇺🇦" + case .czech: return "🇨🇿" + case .swedish: return "🇸🇪" + case .danish: return "🇩🇰" + case .finnish: return "🇫🇮" + case .norwegian: return "🇳🇴" + case .greek: return "🇬🇷" + case .hebrew: return "🇮🇱" + case .indonesian: return "🇮🇩" + case .malay: return "🇲🇾" + case .romanian: return "🇷🇴" + } + } + /// BCP 47 language tag var bcp47Tag: String { rawValue diff --git a/TransFrameTests/PRDAcceptanceTests.swift b/TransFrameTests/PRDAcceptanceTests.swift index e77d49f..658f631 100644 --- a/TransFrameTests/PRDAcceptanceTests.swift +++ b/TransFrameTests/PRDAcceptanceTests.swift @@ -574,28 +574,6 @@ final class PRDAcceptanceTests: XCTestCase { ) } - /// TextSegment 的坐标原点为左上角,译文必须贴在原文下边缘。 - func testBilingualOverlayAnchorPlacesTranslationBelowTopLeftBox() { - let display = CGSize(width: 300, height: 200) - let topBox = CGRect(x: 0.1, y: 0.1, width: 0.5, height: 0.1) - let top = BilingualOverlayLayout.translationAnchor(for: topBox, in: display) - XCTAssertEqual(top.x, 30, accuracy: 0.5) - XCTAssertEqual(top.y, 41, accuracy: 0.5) - XCTAssertEqual(top.maxWidth, 150, accuracy: 0.5) - - let lowerBox = CGRect(x: 0.25, y: 0.7, width: 0.2, height: 0.1) - let lower = BilingualOverlayLayout.translationAnchor(for: lowerBox, in: display) - XCTAssertEqual(lower.x, 75, accuracy: 0.5) - XCTAssertEqual(lower.y, 161, accuracy: 0.5) - XCTAssertGreaterThan(lower.y, top.y) - - let tiny = BilingualOverlayLayout.translationAnchor( - for: CGRect(x: 0.95, y: 0.5, width: 0.01, height: 0.01), in: display - ) - XCTAssertGreaterThanOrEqual(tiny.fontSize, 10) - XCTAssertLessThanOrEqual(tiny.x + tiny.maxWidth, display.width) - } - /// VLM gateways forwarding the OpenAI Responses API put a `reasoning` /// output item in front of the `message` item — parsing must skip it and /// read the message's text blocks (user report 2026-10-23: deepseek relay). @@ -629,7 +607,9 @@ final class PRDAcceptanceTests: XCTestCase { } /// 验证真实选区窗口的绘制结果,不依赖右侧面板或坐标函数自报成功。 - func testTranslationStripsRenderInsideSelectionAndKeepSourcePixels() throws { + /// 译文只出现在右侧结果面板:翻译 tab 激活时选区内的原图像素必须 + /// 完全不被遮挡(用户反馈密集文字下原位覆盖不可读,已撤除)。 + func testTranslateTabKeepsSelectionPixelsUntouched() throws { let screen = try XCTUnwrap(NSScreen.main) let bounds = CGRect(origin: .zero, size: screen.frame.size) let displayID = CGMainDisplayID() @@ -677,10 +657,14 @@ final class PRDAcceptanceTests: XCTestCase { func color(at point: CGPoint) throws -> (r: Double, g: Double, b: Double) { try XCTUnwrap(Self.pixel(in: rendered, x: Int(point.x * scale), y: Int(point.y * scale))) } - let strip = try color(at: CGPoint(x: 162, y: 255)) - XCTAssertLessThan(strip.r, 0.5, "译文背景必须画在选区中的原文下方") - let source = try color(at: CGPoint(x: 162, y: 235)) - XCTAssertGreaterThan(source.r, 0.95, "译文不能覆盖原文区域") + // 原图区域:任何位置都不得绘制译文/面板 + let formerStripArea = try color(at: CGPoint(x: 350, y: 268)) + XCTAssertGreaterThan( + formerStripArea.r, 0.95, + "译文不得绘制在选区画布上(只在右侧面板显示)" + ) + let sourceArea = try color(at: CGPoint(x: 350, y: 240)) + XCTAssertGreaterThan(sourceArea.r, 0.95, "原文区必须保持原始像素") let unused = try color(at: CGPoint(x: 162, y: 475)) XCTAssertGreaterThan(unused.r, 0.95, "不能沿错误的 Y 轴绘制译文") let attachment = XCTAttachment(image: NSImage(cgImage: rendered, size: bounds.size)) diff --git a/TransFrameTests/TranslationConfigurationTests.swift b/TransFrameTests/TranslationConfigurationTests.swift index 0ff7478..83bfeb4 100644 --- a/TransFrameTests/TranslationConfigurationTests.swift +++ b/TransFrameTests/TranslationConfigurationTests.swift @@ -166,6 +166,7 @@ final class TranslationConfigurationTests: XCTestCase { .connectionFailed("offline"), .invalidConfiguration("missing key"), .translationFailed("bad gateway"), + .incompleteResponse("truncated"), .emptyInput, .unsupportedLanguage("tlh"), .timeout, @@ -182,4 +183,18 @@ final class TranslationConfigurationTests: XCTestCase { XCTAssertFalse(suggestion.isEmpty) } } + + /// 面板国旗横排:每个语言都有非空国旗,快捷列表内国旗互不重复。 + func testFlagEmojiCoversAllLanguagesUniquelyForQuickSwitcher() { + let allFlags = TranslationLanguage.allCases.map(\.flagEmoji) + XCTAssertTrue(allFlags.allSatisfy { !$0.isEmpty }, "每个语言都必须有国旗 emoji") + + let quickSwitcher: [TranslationLanguage] = [ + .chineseSimplified, .english, .japanese, .korean, + .french, .german, .spanish, .russian, + ] + let quickFlags = quickSwitcher.map(\.flagEmoji) + XCTAssertEqual(Set(quickFlags).count, quickFlags.count, "快捷切换语言的国旗必须可区分") + XCTAssertEqual(TranslationLanguage.auto.flagEmoji, "🌐") + } } diff --git a/TransFrameTests/VisualLineGroupingTests.swift b/TransFrameTests/VisualLineGroupingTests.swift new file mode 100644 index 0000000..a0b018d --- /dev/null +++ b/TransFrameTests/VisualLineGroupingTests.swift @@ -0,0 +1,151 @@ +import CoreGraphics +import XCTest +@testable import TransFrame + +/// 视觉行聚合:同一视觉行上的 OCR/VLM 碎片在送译前合并为完整行。 +final class VisualLineGroupingTests: XCTestCase { + // MARK: - Fixtures + + /// 与截图问题同构:一行被拆成 3 段(含行内代码片段 `main`)。 + private func lineFragments() -> [TextSegment] { + [ + TextSegment(text: "installed metadata. Prefer a commit SHA in place of", boundingBox: CGRect(x: 0.10, y: 0.40, width: 0.50, height: 0.02)), + TextSegment(text: "main", boundingBox: CGRect(x: 0.61, y: 0.401, width: 0.04, height: 0.019)), + TextSegment(text: "for a fixed package revision.", boundingBox: CGRect(x: 0.66, y: 0.402, width: 0.25, height: 0.02)), + ] + } + + // MARK: - Same-line merging + + func testSameLineFragmentsMergeIntoSingleSegment() { + let merged = VisualLineGrouping.mergeLines(in: lineFragments()) + XCTAssertEqual(merged.count, 1) + XCTAssertEqual( + merged[0].text, + "installed metadata. Prefer a commit SHA in place of main for a fixed package revision." + ) + } + + func testMergedBoundingBoxIsUnionOfFragments() { + let merged = VisualLineGrouping.mergeLines(in: lineFragments()) + XCTAssertEqual(merged[0].boundingBox.minX, 0.10, accuracy: 1e-9) + XCTAssertEqual(merged[0].boundingBox.maxX, 0.91, accuracy: 1e-9) + XCTAssertEqual(merged[0].boundingBox.minY, 0.40, accuracy: 1e-9) + XCTAssertEqual(merged[0].boundingBox.maxY, 0.422, accuracy: 1e-9) + } + + func testDistinctLinesStaySeparate() { + let segments = [ + TextSegment(text: "first line", boundingBox: CGRect(x: 0.1, y: 0.10, width: 0.4, height: 0.02)), + TextSegment(text: "second line", boundingBox: CGRect(x: 0.1, y: 0.20, width: 0.4, height: 0.02)), + TextSegment(text: "third line", boundingBox: CGRect(x: 0.1, y: 0.30, width: 0.4, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 3) + XCTAssertEqual(merged.map(\.text), ["first line", "second line", "third line"]) + } + + /// 左右分栏:横向间距大于 3 倍行高,不得拼成一行。 + func testDistantColumnsOnSameBaselineDoNotMerge() { + let segments = [ + TextSegment(text: "left column", boundingBox: CGRect(x: 0.05, y: 0.50, width: 0.20, height: 0.02)), + TextSegment(text: "right column", boundingBox: CGRect(x: 0.70, y: 0.50, width: 0.25, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 2) + } + + /// 相邻碎片(横向间距小于阈值)按视觉行合并。 + func testNearbyFragmentsOnSameBaselineMerge() { + let segments = [ + TextSegment(text: "Name", boundingBox: CGRect(x: 0.10, y: 0.50, width: 0.10, height: 0.02)), + TextSegment(text: "Value", boundingBox: CGRect(x: 0.15, y: 0.50, width: 0.10, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 1) + XCTAssertEqual(merged[0].text, "Name Value") + } + + // MARK: - Text joining + + func testLatinFragmentsJoinWithSpace() { + XCTAssertEqual(VisualLineGrouping.joinText("set", "TOKEN to any"), "set TOKEN to any") + } + + func testCJKFragmentsJoinWithoutSpace() { + XCTAssertEqual(VisualLineGrouping.joinText("该注册表是公开的", "因此不需要令牌"), "该注册表是公开的因此不需要令牌") + } + + func testCJKAndLatinBoundaryGetsSpace() { + XCTAssertEqual(VisualLineGrouping.joinText("令牌", "TOKEN"), "令牌 TOKEN") + } + + // MARK: - Ordering & edge cases + + func testOutputIsSortedTopToBottom() { + // 输入乱序:第三行在前 + let segments = [ + TextSegment(text: "bottom", boundingBox: CGRect(x: 0.1, y: 0.60, width: 0.3, height: 0.02)), + TextSegment(text: "top", boundingBox: CGRect(x: 0.1, y: 0.10, width: 0.3, height: 0.02)), + TextSegment(text: "middle", boundingBox: CGRect(x: 0.1, y: 0.35, width: 0.3, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.map(\.text), ["top", "middle", "bottom"]) + } + + func testSingleSegmentPassesThrough() { + let only = TextSegment(text: "solo", boundingBox: CGRect(x: 0.1, y: 0.1, width: 0.2, height: 0.02)) + XCTAssertEqual(VisualLineGrouping.mergeLines(in: [only]), [only]) + } + + func testEmptyInputReturnsEmpty() { + XCTAssertTrue(VisualLineGrouping.mergeLines(in: []).isEmpty) + } + + /// 零尺寸包围盒(如无坐标的纯文本结果)不参与合并、不崩溃。 + func testZeroSizeBoxesAreKeptAsIs() { + let segments = [ + TextSegment(text: "no box", boundingBox: .zero), + TextSegment(text: "normal", boundingBox: CGRect(x: 0.1, y: 0.1, width: 0.3, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 2) + XCTAssertEqual(Set(merged.map(\.text)), ["no box", "normal"]) + } + + /// Y 重叠低于 50%(基线错开的邻行)不得合并。 + func testLowVerticalOverlapDoesNotMerge() { + let segments = [ + TextSegment(text: "upper", boundingBox: CGRect(x: 0.1, y: 0.100, width: 0.3, height: 0.02)), + TextSegment(text: "lower", boundingBox: CGRect(x: 0.5, y: 0.115, width: 0.3, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 2) + } + + func testUserEditedFlagPropagatesToMergedSegment() { + let edited = TextSegment( + text: "edited", boundingBox: CGRect(x: 0.1, y: 0.1, width: 0.2, height: 0.02), + confidence: 0.9, isUserEdited: true + ) + let plain = TextSegment(text: "plain", boundingBox: CGRect(x: 0.35, y: 0.1, width: 0.2, height: 0.02)) + let merged = VisualLineGrouping.mergeLines(in: [edited, plain]) + XCTAssertEqual(merged.count, 1) + XCTAssertTrue(merged[0].isUserEdited) + } + + // MARK: - ScreenAnalysisResult integration + + func testMergingVisualLinesReturnsGroupedResult() { + let result = ScreenAnalysisResult( + segments: lineFragments() + [ + TextSegment(text: "next line", boundingBox: CGRect(x: 0.10, y: 0.50, width: 0.3, height: 0.02)), + ], + imageSize: CGSize(width: 1000, height: 600) + ) + let grouped = result.mergingVisualLines() + XCTAssertEqual(grouped.segments.count, 2) + XCTAssertTrue(grouped.segments[0].text.hasPrefix("installed metadata")) + XCTAssertEqual(grouped.segments[1].text, "next line") + } +} From 5e8279f979e3f63790871acb90db7ce513ebb1e3 Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 27 Oct 2026 20:38:13 +0800 Subject: [PATCH 3/3] fix(review): address OpenCodeReview findings on PR #83 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../Features/Settings/EngineConfigSheet.swift | 7 ++- TransFrame/Models/TranslationEngineType.swift | 14 ++++- TransFrame/Services/MTranServerEngine.swift | 16 ++++- .../LLMTranslationResponseParser.swift | 5 +- .../TranslationEngineRegistry.swift | 6 ++ .../Translation/VisualLineGrouping.swift | 62 +++++++++++++------ .../LLMResponseTruncationTests.swift | 7 +++ TransFrameTests/MTranServerEngineTests.swift | 26 ++++++++ TransFrameTests/VisualLineGroupingTests.swift | 40 +++++++----- 9 files changed, 141 insertions(+), 42 deletions(-) diff --git a/TransFrame/Features/Settings/EngineConfigSheet.swift b/TransFrame/Features/Settings/EngineConfigSheet.swift index bfa5c56..c6343b5 100644 --- a/TransFrame/Features/Settings/EngineConfigSheet.swift +++ b/TransFrame/Features/Settings/EngineConfigSheet.swift @@ -419,10 +419,13 @@ struct EngineConfigSheet: View { } // Test connection with the unsaved form snapshot, not the old engine config. + // Empty form fields mean "keep the saved value" (same contract as + // saveConfig), so the probe must not hit a different URL than the + // one that will actually be persisted. var testConfig = config var options = config.options ?? EngineOptions.default(for: engine) ?? EngineOptions() - options.baseURL = baseURL - options.modelName = modelName + if !baseURL.isEmpty { options.baseURL = baseURL } + if !modelName.isEmpty { options.modelName = modelName } testConfig.options = options try await TranslationService.shared.verifyConnection(for: engine, config: testConfig) diff --git a/TransFrame/Models/TranslationEngineType.swift b/TransFrame/Models/TranslationEngineType.swift index 279bb5c..6c87df1 100644 --- a/TransFrame/Models/TranslationEngineType.swift +++ b/TransFrame/Models/TranslationEngineType.swift @@ -295,7 +295,9 @@ final class MTranServerChecker: @unchecked Sendable { self.now = now } - /// Check if MTranServer is available (synchronous property, never performs network I/O) + /// Check if MTranServer is available (synchronous property, never performs network I/O). + /// Cached-only snapshot: returns false whenever no fresh (TTL-valid) probe + /// exists; use `checkAvailability(...)` for an authoritative answer. static var isAvailable: Bool { shared.isAvailable } var isAvailable: Bool { cachedAvailability(for: .default) } @@ -400,8 +402,14 @@ final class MTranServerChecker: @unchecked Sendable { logger.info("MTranServer available via \(endpoint)") return true } - if endpoint != "/translate" && [404, 405, 501].contains(statusCode) { - continue + if endpoint != "/translate" { + // 404/405/501: endpoint missing. 401/403/429: a gateway in + // front may only guard some paths while /translate works. + // Other statuses (5xx server errors) fail fast — the + // server itself is unhealthy and /translate won't be well. + if [401, 403, 404, 405, 429, 501].contains(statusCode) { + continue + } } return false } catch is CancellationError { diff --git a/TransFrame/Services/MTranServerEngine.swift b/TransFrame/Services/MTranServerEngine.swift index 6434c50..cef7e1c 100644 --- a/TransFrame/Services/MTranServerEngine.swift +++ b/TransFrame/Services/MTranServerEngine.swift @@ -94,7 +94,21 @@ actor MTranServerEngine: TranslationProvider { guard inet_pton(AF_INET6, rawAddress, &parsed) == 1 else { return nil } return host } - return host == "localhost" ? "127.0.0.1" : host + if host == "localhost" { return "127.0.0.1" } + // percentEncodedHost throws an uncatchable NSInvalidArgumentException + // on illegal characters (e.g. legacy UserDefaults hosts with spaces + // or non-ASCII input); restrict to hostname/IPv4 syntax. + guard host.allSatisfy(Self.isHostnameSafeCharacter) else { return nil } + return host + } + + private static func isHostnameSafeCharacter(_ character: Character) -> Bool { + guard let ascii = character.asciiValue else { return false } + switch ascii { + case 0x30...0x39, 0x41...0x5A, 0x61...0x7A: return true // 0-9 A-Z a-z + case 0x2E, 0x2D, 0x5F: return true // . - _ + default: return false + } } var isValid: Bool { diff --git a/TransFrame/Services/Translation/LLMTranslationResponseParser.swift b/TransFrame/Services/Translation/LLMTranslationResponseParser.swift index fc5da05..da24a69 100644 --- a/TransFrame/Services/Translation/LLMTranslationResponseParser.swift +++ b/TransFrame/Services/Translation/LLMTranslationResponseParser.swift @@ -7,7 +7,10 @@ enum LLMTranslationResponseParser { throw TranslationProviderError.translationFailed("Unable to decode data to UTF-8 string") } let trimmed = rawString.trimmingCharacters(in: .whitespacesAndNewlines) - if trimmed.hasPrefix("data:") || trimmed.contains("\ndata:") { + // Only treat the response as an SSE stream when the first payload line + // is a data event. Translation bodies legitimately containing "\ndata:" + // (logs, SSE examples) must still parse as plain JSON. + if trimmed.hasPrefix("data:") { guard engine != .claude else { return try extractClaudeSSE(from: trimmed) } diff --git a/TransFrame/Services/Translation/TranslationEngineRegistry.swift b/TransFrame/Services/Translation/TranslationEngineRegistry.swift index 0139595..345f677 100644 --- a/TransFrame/Services/Translation/TranslationEngineRegistry.swift +++ b/TransFrame/Services/Translation/TranslationEngineRegistry.swift @@ -264,6 +264,12 @@ extension TranslationEngineRegistry { inFlight.customSnapshot == customSnapshot { return try await inFlight.task.value } + // Stale or force-refreshed creation: cancel and drop it so repeated + // saves don't spawn concurrent duplicate providers for the same type. + if let stale = inFlightCreations[type] { + stale.task.cancel() + inFlightCreations.removeValue(forKey: type) + } let mutationRevision = bumpEngineMutationRevision(for: type) let token = allocateCreationToken() diff --git a/TransFrame/Services/Translation/VisualLineGrouping.swift b/TransFrame/Services/Translation/VisualLineGrouping.swift index 25e30fd..308e9f1 100644 --- a/TransFrame/Services/Translation/VisualLineGrouping.swift +++ b/TransFrame/Services/Translation/VisualLineGrouping.swift @@ -60,14 +60,21 @@ enum VisualLineGrouping { } /// Merges one line's fragments (left-to-right) into a single segment: - /// joined text, union bounding box, averaged confidence. + /// joined text, union bounding box, averaged confidence. Fragments that + /// nearly touch (gap ≤ 25% of the shorter line height) join without a + /// space so VLM-split code identifiers ("DESIGMA" + "_PLUGINS_TOKEN") + /// are not corrupted by inserted spaces. private static func mergedSegment(from fragments: [TextSegment]) -> TextSegment? { 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 } - let joinedText = ordered.map(\.text).reduce(into: "") { partial, next in - partial = joinText(partial, next) + var joinedText = ordered[0].text + for (previous, next) in zip(ordered, ordered.dropFirst()) { + let gap = next.boundingBox.minX - previous.boundingBox.maxX + let referenceHeight = min(previous.boundingBox.height, next.boundingBox.height) + let nearlyTouching = referenceHeight > 0 && gap <= referenceHeight * 0.25 + joinedText = joinText(joinedText, next.text, gluesDirectly: nearlyTouching) } let box = union(of: ordered.map(\.boundingBox)) let confidence = ordered.map(\.confidence).reduce(0, +) / Float(ordered.count) @@ -79,14 +86,43 @@ enum VisualLineGrouping { ) } - /// Joins two fragments: CJK-adjacent text glues directly, everything - /// else gets a single space. - static func joinText(_ lhs: String, _ rhs: String) -> String { + /// Joins two fragments: CJK-adjacent text glues directly; a near-zero gap + /// between two identifier-shaped fragments (underscore boundary, or both + /// sides look like code identifiers) glues directly so VLM-split tokens + /// ("DESIGMA" + "_PLUGINS_TOKEN") survive; everything else gets a space. + static func joinText(_ lhs: String, _ rhs: String, gluesDirectly: Bool = false) -> String { guard let last = lhs.last, let first = rhs.first else { return lhs + rhs } if isCJK(last), isCJK(first) { return lhs + rhs } + if gluesDirectly, + isIdentifierCharacter(last), isIdentifierCharacter(first), + lhs.hasSuffix("_") || rhs.hasPrefix("_") + || (isIdentifierLike(lhs) && isIdentifierLike(rhs)) { + return lhs + rhs + } return lhs + " " + rhs } + /// Heuristic "looks like a code identifier": contains an underscore or a + /// digit, or has an uppercase letter after the first character + /// (camelCase / SCREAMING_CASE). Capitalized plain words like "Name" + /// don't qualify, so touching OCR fragments of normal prose still get + /// the separating space. + private static func isIdentifierLike(_ text: String) -> Bool { + guard let first = text.first else { return false } + let afterFirst = text.dropFirst() + return text.contains("_") + || text.contains(where: \.isNumber) + || afterFirst.contains(where: \.isUppercase) + } + + private static func isIdentifierCharacter(_ character: Character) -> Bool { + guard let ascii = character.asciiValue else { return false } + return (0x30...0x39).contains(ascii) + || (0x41...0x5A).contains(ascii) + || (0x61...0x7A).contains(ascii) + || ascii == 0x5F + } + private static func isCJK(_ character: Character) -> Bool { character.unicodeScalars.allSatisfy { scalar in (0x4E00...0x9FFF).contains(scalar.value) @@ -100,17 +136,3 @@ enum VisualLineGrouping { boxes.reduce(CGRect.null) { $0.union($1) } } } - -extension ScreenAnalysisResult { - /// Translation prep step after `filteredForTranslation()`: merges - /// same-line fragments so each visual line is translated as one unit. - func mergingVisualLines() -> ScreenAnalysisResult { - let merged = VisualLineGrouping.mergeLines(in: segments) - return ScreenAnalysisResult( - segments: merged, - rawSegments: rawSegments, - filteredOutSegments: filteredOutSegments, - imageSize: imageSize - ) - } -} diff --git a/TransFrameTests/LLMResponseTruncationTests.swift b/TransFrameTests/LLMResponseTruncationTests.swift index 9d6d573..4fb0673 100644 --- a/TransFrameTests/LLMResponseTruncationTests.swift +++ b/TransFrameTests/LLMResponseTruncationTests.swift @@ -19,6 +19,13 @@ final class LLMResponseTruncationTests: XCTestCase { XCTAssertThrowsError(try LLMTranslationResponseParser.extractText(from: data, for: .openai)) } + /// 译文正文含换行 + "data:"(日志/SSE 示例文本)时不得误判为 SSE 流。 + func testJSONBodyContainingDataLineIsNotTreatedAsSSE() throws { + let payload = #"{"stop_reason":"end_turn","content":[{"type":"text","text":"日志示例\ndata: 123"}]}"# + let text = try LLMTranslationResponseParser.extractText(from: Data(payload.utf8), for: .claude) + XCTAssertEqual(text, "日志示例\ndata: 123") + } + func testBatchingRespectsBudgetAndPreservesAllSourceText() { let texts = [String(repeating: "汉", count: 20), String(repeating: "字", count: 20), "third", "fourth"] let batches = LLMTranslationBatching.split(texts: texts, maxOutputTokens: 20, promptTemplate: nil) diff --git a/TransFrameTests/MTranServerEngineTests.swift b/TransFrameTests/MTranServerEngineTests.swift index 9c7a9f7..c31f93a 100644 --- a/TransFrameTests/MTranServerEngineTests.swift +++ b/TransFrameTests/MTranServerEngineTests.swift @@ -85,6 +85,15 @@ final class MTranServerEngineTests: XCTestCase { XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health"]) } + /// 网关只拦截 /health 与 /(401),/translate 正常时仍应判可用。 + func testHealthUnauthorizedFallsThroughToTranslate() async throws { + MTranTestURLProtocol.statusCodes = ["/health": 401, "/": 403] + + let available = try await checker.checkAvailability(configuration: makeConfig()) + XCTAssertTrue(available) + XCTAssertEqual(MTranTestURLProtocol.requests.map(\.url?.path), ["/health", "/", "/translate"]) + } + func testHealthNotFoundFallsBackToRoot() async throws { MTranTestURLProtocol.statusCodes = ["/health": 404] @@ -261,6 +270,23 @@ final class MTranServerEngineTests: XCTestCase { XCTAssertNil(MTranServerEngine.Configuration(serverURL: "http://[bad:host]:8989")) } + /// 残留非法 host(空格/非 ASCII)不得触发 percentEncodedHost 的 + /// 不可捕获 NSInvalidArgumentException:endpointHost 返回 nil,配置判无效。 + func testIllegalHostnameIsRejectedNotCrashing() { + for badHost in ["my host.example", "服务器.本地", "host\nevil"] { + let config = MTranServerEngine.Configuration( + serverAddress: badHost, serverPort: 8989, timeout: 10.0, autoDetectSourceLanguage: true + ) + XCTAssertNil(config.endpointHost, "host 必须被拒绝: \(String(describing: badHost))") + XCTAssertFalse(config.isValid) + } + // 合法 host 仍通过 + let valid = MTranServerEngine.Configuration( + serverAddress: "192.168.1.10", serverPort: 8989, timeout: 10.0, autoDetectSourceLanguage: true + ) + XCTAssertEqual(valid.endpointHost, "192.168.1.10") + } + func testEngineOptionsConfigureProviderEndpointAndTimeout() async { let engine = MTranServerEngine.provider(config: TranslationEngineConfig( id: .mtranServer, diff --git a/TransFrameTests/VisualLineGroupingTests.swift b/TransFrameTests/VisualLineGroupingTests.swift index a0b018d..2e3c5a3 100644 --- a/TransFrameTests/VisualLineGroupingTests.swift +++ b/TransFrameTests/VisualLineGroupingTests.swift @@ -80,6 +80,31 @@ final class VisualLineGroupingTests: XCTestCase { XCTAssertEqual(VisualLineGrouping.joinText("令牌", "TOKEN"), "令牌 TOKEN") } + /// 近零间隙的代码标识符碎片贴合拼接,不得插入原文不存在的空格。 + func testTouchingIdentifierFragmentsGlueWithoutSpace() { + XCTAssertEqual( + VisualLineGrouping.joinText("DESIGMA", "_PLUGINS_TOKEN", gluesDirectly: true), + "DESIGMA_PLUGINS_TOKEN" + ) + // 有间隙的普通单词即使传 gluesDirectly 也不贴合(保守:仅标识符字符贴合) + XCTAssertEqual( + VisualLineGrouping.joinText("of", "main", gluesDirectly: true), + "of main" + ) + } + + /// 完整合并路径:贴合的标识符碎片 mergeLines 后文本无多余空格。 + func testMergedLineKeepsTouchingIdentifierIntact() { + let segments = [ + TextSegment(text: "set", boundingBox: CGRect(x: 0.10, y: 0.50, width: 0.03, height: 0.02)), + TextSegment(text: "DESIGMA_PLUGINS_TOKEN", boundingBox: CGRect(x: 0.132, y: 0.50, width: 0.20, height: 0.02)), + TextSegment(text: "to any", boundingBox: CGRect(x: 0.34, y: 0.50, width: 0.06, height: 0.02)), + ] + let merged = VisualLineGrouping.mergeLines(in: segments) + XCTAssertEqual(merged.count, 1) + XCTAssertEqual(merged[0].text, "set DESIGMA_PLUGINS_TOKEN to any") + } + // MARK: - Ordering & edge cases func testOutputIsSortedTopToBottom() { @@ -133,19 +158,4 @@ final class VisualLineGroupingTests: XCTestCase { XCTAssertEqual(merged.count, 1) XCTAssertTrue(merged[0].isUserEdited) } - - // MARK: - ScreenAnalysisResult integration - - func testMergingVisualLinesReturnsGroupedResult() { - let result = ScreenAnalysisResult( - segments: lineFragments() + [ - TextSegment(text: "next line", boundingBox: CGRect(x: 0.10, y: 0.50, width: 0.3, height: 0.02)), - ], - imageSize: CGSize(width: 1000, height: 600) - ) - let grouped = result.mergingVisualLines() - XCTAssertEqual(grouped.segments.count, 2) - XCTAssertTrue(grouped.segments[0].text.hasPrefix("installed metadata")) - XCTAssertEqual(grouped.segments[1].text, "next line") - } }