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

+25 -8
View File
@@ -238,15 +238,24 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
}
}
// Deduplicate across entries, preserving first-seen order.
// Deduplicate across entries, preserving first-seen order. Near
// duplicates that differ only in whitespace collapse to one unit:
// sending both in one batch makes the model answer with a single
// translation for "the same string twice", which desynchronises the
// positional array protocol.
unitOrder := []string{}
unitSeen := map[string]struct{}{}
unitSeen := map[string]string{}
unitCanonical := map[string]string{} // raw text -> canonical raw text
for _, job := range jobs {
for _, u := range job.units {
if _, ok := unitSeen[u]; !ok {
unitSeen[u] = struct{}{}
unitOrder = append(unitOrder, u)
key := collapseWhitespace(u)
if canonical, ok := unitSeen[key]; ok {
unitCanonical[u] = canonical
continue
}
unitSeen[key] = u
unitCanonical[u] = u
unitOrder = append(unitOrder, u)
}
}
// Oversized units are skipped rather than risking a broken LLM call.
@@ -366,6 +375,12 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
report.AppliedTranslations = len(translations)
// ---- Pass 3: rewrite affected entries, repack in original order ----
alias := func(raw string) string {
if canonical, ok := unitCanonical[raw]; ok {
return canonical
}
return raw
}
type outEntry struct {
idx int
data []byte
@@ -374,7 +389,7 @@ func Translate(ctx context.Context, data []byte, opt *Options, tr Translator) ([
for i := range reader.File {
data := entryData[i]
if job, ok := jobs[i]; ok {
data = []byte(applyJob(string(data), job, translations))
data = []byte(applyJob(string(data), job, translations, alias))
}
out = append(out, outEntry{idx: i, data: data})
}
@@ -518,7 +533,9 @@ func scanDrawing(src string) (*scanJob, int) {
// applyJob rewrites one entry with the resolved translations. Bytes outside
// the recorded edits — styles, geometry, image references — are untouched.
func applyJob(src string, job *scanJob, translations map[string]string) string {
// alias resolves a raw unit to its canonical unit (whitespace-variant
// near duplicates share one translation).
func applyJob(src string, job *scanJob, translations map[string]string, alias func(string) string) string {
type repl struct {
start, end int
text string
@@ -527,7 +544,7 @@ func applyJob(src string, job *scanJob, translations map[string]string) string {
for _, e := range job.edits {
switch e.kind {
case editCarrier:
t, ok := translations[job.units[e.unitIdx]]
t, ok := translations[alias(job.units[e.unitIdx])]
if !ok {
continue // oversized/skipped unit: keep the original bytes
}