mirror of
https://github.com/OwO-Network/DeepLX.git
synced 2026-12-01 23:07:51 +00:00
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.
This commit is contained in:
@@ -90,7 +90,7 @@ curl -X POST http://localhost:1188/translate \
|
|||||||
Batch limits and failure modes:
|
Batch limits and failure modes:
|
||||||
|
|
||||||
- The anonymous limit is **1500 characters for the whole request**, counted in UTF-16 code units — what JavaScript's `String.prototype.length` reports, so a CJK character costs 1 and an astral one (emoji, historic scripts) costs 2 — across every text in the array, not 1500 per text. A batch over that total is rejected with `413` and `text exceeds maximum length: N characters (anonymous oneshot limit is 1500)`, so a large batch has to be chunked by total length rather than by segment count. There is no separate cap on the number of texts.
|
- The anonymous limit is **1500 characters for the whole request**, counted in UTF-16 code units — what JavaScript's `String.prototype.length` reports, so a CJK character costs 1 and an astral one (emoji, historic scripts) costs 2 — across every text in the array, not 1500 per text. A batch over that total is rejected with `413` and `text exceeds maximum length: N characters (anonymous oneshot limit is 1500)`, so a large batch has to be chunked by total length rather than by segment count. There is no separate cap on the number of texts.
|
||||||
- A blank text — empty, or nothing but whitespace — is answered with itself and never sent, so a blank line in a segmented document keeps its place instead of failing the request. A request with no text at all — no texts, or every text empty — is still rejected with `404 {"code":404,"message":"No text to translate"}`.
|
- A blank text — empty, or nothing but whitespace — is answered with itself and never sent, so a blank line in a segmented document keeps its place instead of failing the request. Only a request that names no text at all, `"text": []`, is rejected with `404 {"code":404,"message":"No text to translate"}`.
|
||||||
- A batch is all-or-nothing: if DeepL returns a different number of translations or leaves one of them empty, the request fails with `503 {"code":503,"message":"Translation failed"}` instead of answering with placeholders or a shorter, misaligned array.
|
- A batch is all-or-nothing: if DeepL returns a different number of translations or leaves one of them empty, the request fails with `503 {"code":503,"message":"Translation failed"}` instead of answering with placeholders or a shorter, misaligned array.
|
||||||
|
|
||||||
## Discussion Group
|
## Discussion Group
|
||||||
|
|||||||
@@ -104,10 +104,7 @@ func TestTranslateByDLXRejectsOversizedTextsBeforeUpstream(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
_, length, ok := textsToTranslate(tt.texts)
|
_, length := textsToTranslate(tt.texts)
|
||||||
if !ok {
|
|
||||||
t.Fatalf("textsToTranslate(%q) reported no text", tt.texts)
|
|
||||||
}
|
|
||||||
if length <= maxFreeTextLength {
|
if length <= maxFreeTextLength {
|
||||||
t.Fatalf("textsToTranslate(%q) = %d, which is not over the limit", tt.texts, length)
|
t.Fatalf("textsToTranslate(%q) = %d, which is not over the limit", tt.texts, length)
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-22
@@ -440,26 +440,19 @@ func textLength(text string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// textsToTranslate returns the positions of the texts that carry something to
|
// textsToTranslate returns the positions of the texts that carry something to
|
||||||
// translate and their total length in UTF-16 code units, and whether the request
|
// translate and their total length in UTF-16 code units. A blank text — empty,
|
||||||
// holds any text at all.
|
// or nothing but whitespace — carries nothing to translate, so it is not sent:
|
||||||
//
|
// its own text is the answer, which keeps a segmented document position-aligned
|
||||||
// A blank text — empty, or nothing but whitespace — carries nothing to
|
// and lets an empty line pass through instead of failing the request.
|
||||||
// translate, so it is not sent: its own text is the answer, which keeps a
|
func textsToTranslate(texts []string) (positions []int, total int) {
|
||||||
// segmented document position-aligned instead of failing the whole request.
|
|
||||||
// Whitespace is still text, so only a request whose texts are all empty is a
|
|
||||||
// request with nothing to translate.
|
|
||||||
func textsToTranslate(texts []string) (positions []int, total int, hasText bool) {
|
|
||||||
for i, text := range texts {
|
for i, text := range texts {
|
||||||
if text != "" {
|
|
||||||
hasText = true
|
|
||||||
}
|
|
||||||
if strings.TrimSpace(text) == "" {
|
if strings.TrimSpace(text) == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
positions = append(positions, i)
|
positions = append(positions, i)
|
||||||
total += textLength(text)
|
total += textLength(text)
|
||||||
}
|
}
|
||||||
return positions, total, hasText
|
return positions, total
|
||||||
}
|
}
|
||||||
|
|
||||||
// mergeTranslations puts the translations back at the positions they were asked
|
// mergeTranslations puts the translations back at the positions they were asked
|
||||||
@@ -502,17 +495,21 @@ func translationsFromResult(result gjson.Result, count int) ([]string, error) {
|
|||||||
// to the Pro endpoint; the value is sent verbatim as the Bearer token (i.e.
|
// to the Pro endpoint; the value is sent verbatim as the Bearer token (i.e.
|
||||||
// it must be an OAuth access token, not the legacy dl_session cookie).
|
// it must be an OAuth access token, not the legacy dl_session cookie).
|
||||||
func TranslateByDLX(sourceLang, targetLang string, texts []string, tagHandling string, proxyURL string, dlSession string) (DLXTranslationResult, error) {
|
func TranslateByDLX(sourceLang, targetLang string, texts []string, tagHandling string, proxyURL string, dlSession string) (DLXTranslationResult, error) {
|
||||||
// oneshot charges its anonymous cap against the total length of the
|
// A request that names no text at all is not a translation request; any
|
||||||
// `text` array, and a batch is one upstream request, so the cap applies
|
// text, blank or not, is answered. A missing or null `text` decodes to the
|
||||||
// to the sum of the texts that are actually sent.
|
// one empty string, so this is the empty array.
|
||||||
positions, totalLength, hasText := textsToTranslate(texts)
|
if len(texts) == 0 {
|
||||||
if !hasText {
|
|
||||||
return DLXTranslationResult{
|
return DLXTranslationResult{
|
||||||
Code: http.StatusNotFound,
|
Code: http.StatusNotFound,
|
||||||
Message: "No text to translate",
|
Message: "No text to translate",
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// oneshot charges its anonymous cap against the total length of the
|
||||||
|
// `text` array, and a batch is one upstream request, so the cap applies
|
||||||
|
// to the sum of the texts that are actually sent.
|
||||||
|
positions, totalLength := textsToTranslate(texts)
|
||||||
|
|
||||||
resolvedTarget, err := resolveTargetLang(targetLang)
|
resolvedTarget, err := resolveTargetLang(targetLang)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DLXTranslationResult{
|
return DLXTranslationResult{
|
||||||
@@ -535,10 +532,9 @@ func TranslateByDLX(sourceLang, targetLang string, texts []string, tagHandling s
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nothing to translate, but text was requested: every text is blank, so
|
// Nothing to translate: every text is blank, so none of them travels and
|
||||||
// none of them travels and the request is answered with its own texts,
|
// the request is answered with its own texts, exactly as a blank element
|
||||||
// exactly as a blank element is inside a request that does have something
|
// is inside a request that does have something to translate.
|
||||||
// to translate.
|
|
||||||
if len(positions) == 0 {
|
if len(positions) == 0 {
|
||||||
return DLXTranslationResult{
|
return DLXTranslationResult{
|
||||||
Code: http.StatusOK,
|
Code: http.StatusOK,
|
||||||
|
|||||||
+40
-38
@@ -15,34 +15,33 @@ func TestTextsToTranslate(t *testing.T) {
|
|||||||
texts []string
|
texts []string
|
||||||
wantAt []int
|
wantAt []int
|
||||||
wantTotal int
|
wantTotal int
|
||||||
wantText bool
|
|
||||||
}{
|
}{
|
||||||
{"no texts", nil, nil, 0, false},
|
{"no texts", nil, nil, 0},
|
||||||
{"empty array", []string{}, nil, 0, false},
|
{"empty array", []string{}, nil, 0},
|
||||||
{"empty string", []string{""}, nil, 0, false},
|
{"empty string", []string{""}, nil, 0},
|
||||||
{"every text empty", []string{"", ""}, nil, 0, false},
|
{"every text empty", []string{"", ""}, nil, 0},
|
||||||
{"whitespace only", []string{" \t "}, nil, 0, true},
|
{"whitespace only", []string{" \t "}, nil, 0},
|
||||||
{"empty and blank", []string{"", " "}, nil, 0, true},
|
{"empty and blank", []string{"", " "}, nil, 0},
|
||||||
{"single text", []string{"Hello"}, []int{0}, 5, true},
|
{"single text", []string{"Hello"}, []int{0}, 5},
|
||||||
{"empty element in batch", []string{"Hello", ""}, []int{0}, 5, true},
|
{"empty element in batch", []string{"Hello", ""}, []int{0}, 5},
|
||||||
{"leading empty element in batch", []string{"", "Hello"}, []int{1}, 5, true},
|
{"leading empty element in batch", []string{"", "Hello"}, []int{1}, 5},
|
||||||
{"blank elements around a batch", []string{"", "Hello", " ", "Good morning", "\n"}, []int{1, 3}, 17, true},
|
{"blank elements around a batch", []string{"", "Hello", " ", "Good morning", "\n"}, []int{1, 3}, 17},
|
||||||
{"batch total", []string{"Hello", "Good morning"}, []int{0, 1}, 17, true},
|
{"batch total", []string{"Hello", "Good morning"}, []int{0, 1}, 17},
|
||||||
{"counts UTF-16 units, not bytes", []string{"你好"}, []int{0}, 2, true},
|
{"counts UTF-16 units, not bytes", []string{"你好"}, []int{0}, 2},
|
||||||
{"astral character costs two units", []string{"😀"}, []int{0}, 2, true},
|
{"astral character costs two units", []string{"😀"}, []int{0}, 2},
|
||||||
{"combining mark is two units, not one grapheme", []string{"e\u0301"}, []int{0}, 2, true},
|
{"combining mark is two units, not one grapheme", []string{"e\u0301"}, []int{0}, 2},
|
||||||
{"invalid UTF-8 counts one unit per byte", []string{"\xf0\x90\x80"}, []int{0}, 3, true},
|
{"invalid UTF-8 counts one unit per byte", []string{"\xf0\x90\x80"}, []int{0}, 3},
|
||||||
{"at the limit", []string{strings.Repeat("a", maxFreeTextLength)}, []int{0}, maxFreeTextLength, true},
|
{"at the limit", []string{strings.Repeat("a", maxFreeTextLength)}, []int{0}, maxFreeTextLength},
|
||||||
{"astral at the limit", []string{strings.Repeat("😀", maxFreeTextLength/2)}, []int{0}, maxFreeTextLength, true},
|
{"astral at the limit", []string{strings.Repeat("😀", maxFreeTextLength/2)}, []int{0}, maxFreeTextLength},
|
||||||
{"astral past the limit", []string{strings.Repeat("😀", maxFreeTextLength/2), "😀"}, []int{0, 1}, maxFreeTextLength + 2, true},
|
{"astral past the limit", []string{strings.Repeat("😀", maxFreeTextLength/2), "😀"}, []int{0, 1}, maxFreeTextLength + 2},
|
||||||
{"astral batch sums units, not runes", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, []int{0, 1}, 1600, true},
|
{"astral batch sums units, not runes", []string{strings.Repeat("😀", 400), strings.Repeat("😀", 400)}, []int{0, 1}, 1600},
|
||||||
{"blank texts are not charged", []string{" ", "Hello"}, []int{1}, 5, true},
|
{"blank texts are not charged", []string{" ", "Hello"}, []int{1}, 5},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
at, total, hasText := textsToTranslate(tt.texts)
|
at, total := textsToTranslate(tt.texts)
|
||||||
if hasText != tt.wantText || total != tt.wantTotal || !reflect.DeepEqual(at, tt.wantAt) {
|
if total != tt.wantTotal || !reflect.DeepEqual(at, tt.wantAt) {
|
||||||
t.Fatalf("textsToTranslate(%q) = (%v, %d, %v), want (%v, %d, %v)", tt.texts, at, total, hasText, tt.wantAt, tt.wantTotal, tt.wantText)
|
t.Fatalf("textsToTranslate(%q) = (%v, %d), want (%v, %d)", tt.texts, at, total, tt.wantAt, tt.wantTotal)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -121,8 +120,6 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{"no texts", nil, http.StatusNotFound},
|
{"no texts", nil, http.StatusNotFound},
|
||||||
{"empty array", []string{}, http.StatusNotFound},
|
{"empty array", []string{}, http.StatusNotFound},
|
||||||
{"empty string", []string{""}, http.StatusNotFound},
|
|
||||||
{"every text empty", []string{"", ""}, http.StatusNotFound},
|
|
||||||
{"over the limit", []string{strings.Repeat("a", maxFreeTextLength+1)}, http.StatusRequestEntityTooLarge},
|
{"over the limit", []string{strings.Repeat("a", maxFreeTextLength+1)}, http.StatusRequestEntityTooLarge},
|
||||||
{"batch over the limit", []string{strings.Repeat("a", maxFreeTextLength), "b"}, http.StatusRequestEntityTooLarge},
|
{"batch over the limit", []string{strings.Repeat("a", maxFreeTextLength), "b"}, http.StatusRequestEntityTooLarge},
|
||||||
// 751 runes in the batch, but 1502 UTF-16 units: the case a rune count
|
// 751 runes in the batch, but 1502 UTF-16 units: the case a rune count
|
||||||
@@ -143,26 +140,31 @@ func TestTranslateByDLXValidationFailsBeforeUpstream(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// A request that has text but nothing to translate is answered with its own
|
// Every text is answered — translated when it carries something, returned as it
|
||||||
// texts rather than rejected, and — like the validation cases above — it never
|
// is when it is blank — so only a request that names no text at all is an
|
||||||
// reaches DeepL.
|
// error, and none of the cases below reaches DeepL.
|
||||||
func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
func TestTranslateByDLXAnswersBlankTextsWithThemselves(t *testing.T) {
|
||||||
tests := [][]string{
|
tests := []struct {
|
||||||
{" \t "},
|
name string
|
||||||
{"", " "},
|
texts []string
|
||||||
{"\n", "\t\n", " "},
|
}{
|
||||||
|
{"single empty", []string{""}},
|
||||||
|
{"two empty", []string{"", ""}},
|
||||||
|
{"whitespace only", []string{" \t "}},
|
||||||
|
{"empty and whitespace", []string{"", " "}},
|
||||||
|
{"newlines and spaces", []string{"\n", "\t\n", " "}},
|
||||||
}
|
}
|
||||||
for _, texts := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(strings.Join(texts, "|"), func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
result, err := TranslateByDLX("", "ZH", texts, "", "", "")
|
result, err := TranslateByDLX("", "ZH", tt.texts, "", "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("TranslateByDLX() error = %v", err)
|
t.Fatalf("TranslateByDLX() error = %v", err)
|
||||||
}
|
}
|
||||||
if result.Code != http.StatusOK {
|
if result.Code != http.StatusOK {
|
||||||
t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, http.StatusOK)
|
t.Fatalf("TranslateByDLX() code = %d, want %d", result.Code, http.StatusOK)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(result.Data, texts) {
|
if !reflect.DeepEqual(result.Data, tt.texts) {
|
||||||
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, texts)
|
t.Fatalf("TranslateByDLX() data = %q, want %q", result.Data, tt.texts)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user