fix(翻译超时):超时报错修改
deploy-hxtesting / build-and-deploy (push) Successful in 1m16s

This commit is contained in:
runny committed 2026-11-30 21:51:15 +08:00
1 parent c6088aeb4b
commit f12128a5c3
4 files changed
+136 -33

No files matched your search

+26 -8
View File
@@ -132,9 +132,7 @@ func (t *XlsxTranslateTool) Execute(ctx context.Context, args json.RawMessage) (
// ---- Translate ----
runCtx, cancel := context.WithTimeout(ctx, t.timeout)
defer cancel()
translated, report, err := xlsxtranslate.Translate(runCtx, data, &xlsxtranslate.Options{
BatchSize: 40,
}, &chatBatchTranslator{model: t.model})
translated, report, err := xlsxtranslate.Translate(runCtx, data, &xlsxtranslate.Options{}, &chatBatchTranslator{model: t.model})
if err != nil {
logger.Warnf(ctx, "[Tool][XlsxTranslate] session=%s file=%s failed: %v", sessionID, source, err)
return &types.ToolResult{Success: false,
@@ -300,15 +298,12 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
}
}
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)
resp, err := c.model.Chat(ctx, []chat.Message{
{Role: "system", Content: translateSystemPrompt},
{Role: "user", Content: user},
}, &chat.ChatOptions{Temperature: 0.2, MaxCompletionTokens: 16384})
content, err := c.translateOnce(ctx, user)
if err != nil {
lastErr = err
continue
}
out, err := parseTranslationArray(resp.Content, len(texts))
out, err := parseTranslationArray(content, len(texts))
if err != nil {
lastErr = err
continue
@@ -318,6 +313,29 @@ func (c *chatBatchTranslator) TranslateBatch(ctx context.Context, texts []string
return nil, fmt.Errorf("translation call failed after retries: %w", lastErr)
}
// 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.
func (c *chatBatchTranslator) translateOnce(ctx context.Context, user string) (string, error) {
ch, err := c.model.ChatStream(ctx, []chat.Message{
{Role: "system", Content: translateSystemPrompt},
{Role: "user", Content: user},
}, &chat.ChatOptions{Temperature: 0.2, MaxCompletionTokens: 16384})
if err != nil {
return "", err
}
var content strings.Builder
for chunk := range ch {
switch chunk.ResponseType {
case types.ResponseTypeError:
return "", fmt.Errorf("%s", chunk.Content)
default:
content.WriteString(chunk.Content)
}
}
return content.String(), nil
}
// parseTranslationArray extracts the JSON array from a model response that
// may wrap it in markdown fences or prose.
func parseTranslationArray(content string, want int) ([]string, error) {