feat(capture): translate long selections in parts

The capture gate and the chunk size were the same number, so a selection above one chunk was rejected before the pipeline could split it; the Settings sliders moved both thresholds together. Add maximumPartsPerSelection (1-8, default 1, so existing behaviour is unchanged). The capture limit is now part size x parts and travels on TranslationSessionRequest, so the watcher, CapturePolicy and TranslationSession agree for that capture. TranslationStreaming gained an optional per-part callback; the session emits chunkStarted and the panel shows 'Part 2 of 4' while streaming and '2 parts' when finished. textTooLong now points at Settings and offers an Open Settings button. Lower the dense-script part-size maximum to 4000 because 5000 dense characters plus a 4096-token output can overflow the 8192-token context. Tests cover capture limits, watcher limits, pipeline part callbacks, session events and limits, panel progress, settings clamping and migration. Docs updated: ADR-011, TRANSLATION_OUTPUT, SELECTION_AND_CLIPBOARD, ARCHITECTURE, LANGUAGES and README. Verified: make verify green; live 4355-character German text showed Part 1 of 2, Part 2 of 2 and '2 parts'; with one part the panel showed the too-long message and Open Settings.
This commit is contained in:
schihei committed 2026-11-27 20:21:30 +01:00
1 parent 9f89000b0d
commit 2a601058f4
29 files changed
+607 -122

No files matched your search

@@ -165,6 +165,67 @@ struct TranslationSessionTests {
#expect(event == .failed(epoch: epoch, error: .ollamaUnreachable(url: url)))
}
@Test func chunkProgressAndRequestLimitsAreForwarded() async {
let streaming = ChunkReportingStreaming()
let session = TranslationSession(pipelineFactory: { streaming })
var iterator = await session.events().makeAsyncIterator()
let longRequest = TranslationSessionRequest(
captured: CapturedText(
text: String(repeating: "a", count: 9000),
capturedAt: Date(),
source: .doubleCopy
),
target: HyMT3Languages.german,
model: "test-hy-mt3",
keepAlive: "30m",
captureLimits: CaptureLimits(maximumParts: 3)
)
let epoch = await session.start(longRequest)
var chunks: [String] = []
var finished = false
while let event = await iterator.next() {
switch event {
case let .chunkStarted(eventEpoch, index, total) where eventEpoch == epoch:
chunks.append("\(index)/\(total)")
case let .finished(eventEpoch, result) where eventEpoch == epoch:
#expect(result.chunkCount == 3)
finished = true
default:
break
}
if finished {
break
}
}
#expect(chunks == ["1/3", "2/3", "3/3"])
#expect(finished)
}
@Test func textAboveTheRequestCaptureLimitFailsBeforeTheFactory() async {
let streaming = ScriptedStreaming()
let session = TranslationSession(pipelineFactory: { streaming })
var iterator = await session.events().makeAsyncIterator()
let tooLong = TranslationSessionRequest(
captured: CapturedText(
text: String(repeating: "a", count: 8001),
capturedAt: Date(),
source: .doubleCopy
),
target: HyMT3Languages.german,
model: "test-hy-mt3",
keepAlive: "30m",
captureLimits: CaptureLimits(maximumParts: 2)
)
let epoch = await session.start(tooLong)
_ = await iterator.next()
let event = await iterator.next()
#expect(event == .failed(epoch: epoch, error: .textTooLong(characters: 8001, limit: 8000)))
#expect(await streaming.callCount == 0)
}
private func result(_ translation: String, warnings: [TranslationWarning] = []) -> PipelineResult {
PipelineResult(
translation: translation,
@@ -176,6 +237,34 @@ struct TranslationSessionTests {
}
}
/// A fake that reports three chunks and a matching result.
private actor ChunkReportingStreaming: TranslationStreaming {
func translate(
_ request: TranslationSessionRequest,
onDelta: (@Sendable (String) async -> Void)?
) async throws -> PipelineResult {
try await translate(request, onDelta: onDelta, onChunk: nil)
}
func translate(
_ request: TranslationSessionRequest,
onDelta: (@Sendable (String) async -> Void)?,
onChunk: (@Sendable (Int, Int) async -> Void)?
) async throws -> PipelineResult {
for index in 1 ... 3 {
await onChunk?(index, 3)
await onDelta?("part\(index)")
}
return PipelineResult(
translation: "done",
warnings: [],
sourceLanguage: "en",
timing: TranslationTiming(firstTokenMs: 1, totalMs: 2, promptTokens: 3, outputTokens: 4),
chunkCount: 3
)
}
}
/// A controlled streaming fake. Each call waits until the test releases its outcome.
private actor ScriptedStreaming: TranslationStreaming {
enum Outcome: Sendable {