Commit Graph
3 Commits
Author SHA1 Message Date
JounQin 4fb2759d48 fix(translate): answer a request whose texts are all empty with 200 as well
Every text is answered now, blank or not, so the all-empty case needs no
rule of its own: "" and [""] are texts with nothing to translate in them,
and they are answered with themselves like any other blank text. A missing
or null `text` decodes to the one empty string and is answered the same
way.

The 404 is left for a request that names no text at all: `"text": []`, the
one shape there is nothing to answer with. Nothing else errors on
emptiness.

Refs #244.
2026-10-10 23:39:45 +08:00
JounQin 7269cf615a fix(translate): answer blank texts with themselves instead of failing the request
A request fails as a whole when any one of its texts is empty, so a document
split into lines cannot be translated at all if it has a blank line in it:
["", "Hello"] answers 404 "No text to translate" and nothing is translated.

A blank text — empty, or nothing but whitespace — carries nothing to
translate, so it is no longer sent, and its own text is the answer. That
keeps the response position-aligned, and it is the only thing that can work:
oneshot echoes an empty input, and the positional check would read that echo
as a failed translation and answer 503 instead. A whitespace-only text
survives today only because oneshot happens to echo it as well.

The 404 is now only for a request with no text at all — no texts, or every
text empty. A request that has text but nothing to translate, like ["   "],
is answered with its own texts, so no blank input errors any more and no
input that used to succeed fails now.

This matches the JavaScript client (@deeplx/core batchPayload and
restoreBlankSegments), which already drops text.trim() === '' segments and
puts them back, so the two sides agree on what a blank segment means.

Refs #240, #241, #244.
2026-10-10 23:35:19 +08:00
JounQin 10f66b9484 fix(translate): count the 1500-character cap in UTF-16 code units
The anonymous oneshot cap is 1500 UTF-16 code units — what
String.prototype.length reports in the extension and String.length in the
Kotlin iOS client this endpoint is modelled on. utf8.RuneCountInString
scores an astral character as one where oneshot scores it two, so 751
emoji (751 runes, 1502 units) passed the guard, travelled upstream and
came back as the generic 503 "request failed with status code: 400"
instead of the 413 the guard exists to produce.

Count the unit oneshot counts. BMP text is unaffected: runes and UTF-16
units agree everywhere except a surrogate pair, which is why the rune
count looked right.

Invalid UTF-8 is not part of this and needs no validation of its own:
encoding/json has already replaced every invalid byte with U+FFFD before
the guard runs, one unit each, exactly as a rune count scored them.

Measured against the live endpoint through a local build:
- 750 astral characters (1500 units) -> 200, 751 (1502 units) -> 400
- 800 CJK characters (800 units, 2400 bytes, 1600 columns) -> 200
- 1000 combining-mark pairs (2000 units, 1000 graphemes) -> 400
- 1500 ASCII -> 200, 1501 ASCII -> 413 from the guard

Refs #242
2026-10-10 21:39:05 +08:00