feat(m4-m6): session, panel, Replace, settings and onboarding
M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions. M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate. M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup. Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open. Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
This commit is contained in:
1 parent
64078e2f04
commit
a78572adc3
47 files changed
+4963
-79
No files matched your search
@@ -0,0 +1,191 @@
|
||||
import Foundation
|
||||
import SprachmittlerEngines
|
||||
import SprachmittlerOllama
|
||||
|
||||
/// The streaming translation seam used by `TranslationSession`. The production implementation is
|
||||
/// `TranslationPipeline`; tests provide a controlled fake.
|
||||
public protocol TranslationStreaming: Sendable {
|
||||
/// Translates one captured selection, optionally forwarding deltas.
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult
|
||||
}
|
||||
|
||||
extension TranslationPipeline: TranslationStreaming {}
|
||||
|
||||
public extension TranslationPipeline {
|
||||
func translate(
|
||||
_ request: TranslationSessionRequest,
|
||||
onDelta: (@Sendable (String) async -> Void)?
|
||||
) async throws -> PipelineResult {
|
||||
try await translate(
|
||||
text: request.captured.text,
|
||||
target: request.target,
|
||||
model: request.model,
|
||||
keepAlive: request.keepAlive,
|
||||
sampling: request.sampling,
|
||||
onDelta: onDelta
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// One translation request, including the settings snapshot it was built from.
|
||||
public struct TranslationSessionRequest: Sendable, Equatable {
|
||||
/// The captured text.
|
||||
public let captured: CapturedText
|
||||
/// The target language.
|
||||
public let target: ModelLanguage
|
||||
/// The installed model tag.
|
||||
public let model: String
|
||||
/// The Ollama keep-alive value.
|
||||
public let keepAlive: String
|
||||
/// The sampling values.
|
||||
public let sampling: SamplingOptions
|
||||
|
||||
/// Creates a request.
|
||||
public init(
|
||||
captured: CapturedText,
|
||||
target: ModelLanguage,
|
||||
model: String,
|
||||
keepAlive: String,
|
||||
sampling: SamplingOptions = .publisherDefault
|
||||
) {
|
||||
self.captured = captured
|
||||
self.target = target
|
||||
self.model = model
|
||||
self.keepAlive = keepAlive
|
||||
self.sampling = sampling
|
||||
}
|
||||
}
|
||||
|
||||
/// An event from the translation session. The epoch lets the panel drop stale events.
|
||||
public enum TranslationSessionEvent: Sendable, Equatable {
|
||||
/// A request started.
|
||||
case started(epoch: UInt64, request: TranslationSessionRequest)
|
||||
/// One streamed delta.
|
||||
case delta(epoch: UInt64, text: String)
|
||||
/// The final validated result.
|
||||
case finished(epoch: UInt64, result: PipelineResult)
|
||||
/// A domain error.
|
||||
case failed(epoch: UInt64, error: SprachmittlerError)
|
||||
/// The request was cancelled or replaced.
|
||||
case cancelled(epoch: UInt64)
|
||||
}
|
||||
|
||||
/// The only translation actor: one request in flight, epochs discard stale events (ADR-015).
|
||||
public actor TranslationSession {
|
||||
private let pipelineFactory: @Sendable () throws -> any TranslationStreaming
|
||||
private let eventsStream: AsyncStream<TranslationSessionEvent>
|
||||
private let continuation: AsyncStream<TranslationSessionEvent>.Continuation
|
||||
private var epoch: UInt64 = 0
|
||||
private var task: Task<Void, Never>?
|
||||
|
||||
/// Creates a session. The factory reads a settings snapshot and builds a pipeline.
|
||||
public init(pipelineFactory: @escaping @Sendable () throws -> any TranslationStreaming) {
|
||||
self.pipelineFactory = pipelineFactory
|
||||
let pair = AsyncStream<TranslationSessionEvent>.makeStream()
|
||||
eventsStream = pair.stream
|
||||
continuation = pair.continuation
|
||||
}
|
||||
|
||||
/// The event stream. It has one consumer, normally `PanelViewModel`.
|
||||
public func events() -> AsyncStream<TranslationSessionEvent> {
|
||||
eventsStream
|
||||
}
|
||||
|
||||
/// Starts a request, cancelling any active one, and returns its epoch.
|
||||
@discardableResult
|
||||
public func start(_ request: TranslationSessionRequest) -> UInt64 {
|
||||
task?.cancel()
|
||||
epoch &+= 1
|
||||
let currentEpoch = epoch
|
||||
continuation.yield(.started(epoch: currentEpoch, request: request))
|
||||
|
||||
switch CapturePolicy.validate(request.captured.text) {
|
||||
case .valid:
|
||||
break
|
||||
case .empty:
|
||||
continuation.yield(.failed(epoch: currentEpoch, error: .emptyText))
|
||||
return currentEpoch
|
||||
case let .textTooLong(characters, limit):
|
||||
continuation.yield(
|
||||
.failed(epoch: currentEpoch, error: .textTooLong(characters: characters, limit: limit))
|
||||
)
|
||||
return currentEpoch
|
||||
}
|
||||
|
||||
task = Task { [pipelineFactory] in
|
||||
do {
|
||||
let pipeline = try pipelineFactory()
|
||||
let result = try await pipeline.translate(
|
||||
request,
|
||||
onDelta: { delta in
|
||||
await self.forwardDelta(epoch: currentEpoch, text: delta)
|
||||
}
|
||||
)
|
||||
self.finish(epoch: currentEpoch, result: result)
|
||||
} catch let error as SprachmittlerError {
|
||||
if error == .cancelled {
|
||||
self.cancelEvent(epoch: currentEpoch)
|
||||
} else {
|
||||
self.fail(epoch: currentEpoch, error: error)
|
||||
}
|
||||
} catch let error as OllamaError {
|
||||
self.fail(epoch: currentEpoch, error: SprachmittlerError(ollama: error))
|
||||
} catch let rejection as EndpointRejection {
|
||||
self.fail(
|
||||
epoch: currentEpoch,
|
||||
error: .endpointRejected(reason: rejection)
|
||||
)
|
||||
} catch is CancellationError {
|
||||
self.cancelEvent(epoch: currentEpoch)
|
||||
} catch {
|
||||
self.fail(
|
||||
epoch: currentEpoch,
|
||||
error: .server(status: 0, message: "Translation failed")
|
||||
)
|
||||
}
|
||||
}
|
||||
return currentEpoch
|
||||
}
|
||||
|
||||
/// Cancels the active request and emits a cancellation for its epoch.
|
||||
public func cancel() {
|
||||
let previous = epoch
|
||||
epoch &+= 1
|
||||
task?.cancel()
|
||||
task = nil
|
||||
continuation.yield(.cancelled(epoch: previous))
|
||||
}
|
||||
|
||||
/// Cancels without emitting. Use when the panel closes.
|
||||
public func reset() {
|
||||
epoch &+= 1
|
||||
task?.cancel()
|
||||
task = nil
|
||||
}
|
||||
|
||||
private func forwardDelta(epoch eventEpoch: UInt64, text: String) {
|
||||
guard eventEpoch == epoch else { return }
|
||||
continuation.yield(.delta(epoch: eventEpoch, text: text))
|
||||
}
|
||||
|
||||
private func finish(epoch eventEpoch: UInt64, result: PipelineResult) {
|
||||
guard eventEpoch == epoch else { return }
|
||||
task = nil
|
||||
continuation.yield(.finished(epoch: eventEpoch, result: result))
|
||||
}
|
||||
|
||||
private func fail(epoch eventEpoch: UInt64, error: SprachmittlerError) {
|
||||
guard eventEpoch == epoch else { return }
|
||||
task = nil
|
||||
continuation.yield(.failed(epoch: eventEpoch, error: error))
|
||||
}
|
||||
|
||||
private func cancelEvent(epoch eventEpoch: UInt64) {
|
||||
guard eventEpoch == epoch else { return }
|
||||
task = nil
|
||||
continuation.yield(.cancelled(epoch: eventEpoch))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user