fix(翻译bug):修复翻译超时失败!
deploy-hxtesting / build-and-deploy (push) Successful in 1m15s

This commit is contained in:
runny committed 2026-11-30 23:22:23 +08:00
1 parent 74d21cb204
commit 7d3b6804af
5 files changed
+220 -15

No files matched your search

+76 -7
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"path"
@@ -284,17 +285,70 @@ Rules:
5. Output ONLY a JSON array of translated strings with the same length and order as the input array. No commentary.`
func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string) ([]string, error) {
return c.translateRecover(ctx, texts, bisectDepth(len(texts)))
}
// bisectDepth bounds the halving steps needed to reach single strings.
func bisectDepth(n int) int {
depth := 0
for n > 1 {
n /= 2
depth++
}
return depth + 1
}
// translateRecover aligns model answers with inputs. A positional JSON
// protocol fails in one deterministic way: the model merges or drops an item
// (near-duplicate strings, symbol-only entries) and returns N-1 results, so
// blind retries reproduce the same mismatch forever. On a count mismatch the
// batch is halved and each half retried; a single stubborn string degrades to
// keeping the original text, which the engine's residual scan reports.
func (c *chatBatchTranslator) translateRecover(ctx context.Context, texts []string, depth int) ([]string, error) {
out, err := c.attempt(ctx, texts)
if err == nil && len(out) == len(texts) {
return out, nil
}
if len(texts) == 1 {
if err == nil && len(out) >= 1 && strings.TrimSpace(out[0]) != "" {
return []string{out[0]}, nil
}
// Keep the original untranslated; the residual scan reports it.
return []string{texts[0]}, nil
}
if depth <= 0 {
if err != nil {
return nil, err
}
return nil, fmt.Errorf("model returned %d translations for %d inputs", len(out), len(texts))
}
mid := len(texts) / 2
left, err := c.translateRecover(ctx, texts[:mid], depth-1)
if err != nil {
return nil, err
}
right, err := c.translateRecover(ctx, texts[mid:], depth-1)
if err != nil {
return nil, err
}
return append(left, right...), nil
}
// attempt issues one streaming call. Transport and JSON-shape errors get one
// immediate retry; count mismatches are content-deterministic and returned
// (wrapped) so the caller bisects instead of repeating the same failure.
func (c *chatBatchTranslator) attempt(ctx context.Context, texts []string) ([]string, error) {
payload, err := json.Marshal(texts)
if err != nil {
return nil, err
}
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
if attempt > 0 {
for a := 0; a < 2; a++ {
if a > 0 {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(time.Duration(attempt) * 2 * time.Second):
case <-time.After(2 * time.Second):
}
}
user := fmt.Sprintf("Translate the following JSON array of strings to English. Return a JSON array with exactly %d items in the same order.\n\n%s", len(texts), payload)
@@ -303,9 +357,13 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
lastErr = err
continue
}
out, err := parseTranslationArray(content, len(texts))
if err != nil {
lastErr = err
out, perr := parseTranslationArray(content, len(texts))
if perr != nil {
var mismatch *countMismatchError
if errors.As(perr, &mismatch) {
return mismatch.items, perr
}
lastErr = perr
continue
}
return out, nil
@@ -313,6 +371,17 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
return nil, fmt.Errorf("translation call failed after retries: %w", lastErr)
}
// countMismatchError carries the model's items so bisection can still use
// them when the counts do not line up.
type countMismatchError struct {
got, want int
items []string
}
func (e *countMismatchError) Error() string {
return fmt.Sprintf("model returned %d translations for %d inputs", e.got, e.want)
}
// translateOnce issues one streaming batch call and returns the concatenated
// answer. Streaming is deliberate: long batches then fall under the chat
// client's stall-based timeout instead of a single non-stream HTTP timeout.
@@ -354,7 +423,7 @@ func parseTranslationArray(content string, want int) ([]string, error) {
return nil, fmt.Errorf("model response is not a valid string array: %w", err)
}
if len(out) != want {
return nil, fmt.Errorf("model returned %d translations for %d inputs", len(out), want)
return nil, &countMismatchError{got: len(out), want: want, items: out}
}
return out, nil
}