feat(翻译):优化翻译保存地址!
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s

This commit is contained in:
runny committed 2026-11-30 23:48:07 +08:00
1 parent 7d3b6804af
commit 2048251eff
6 files changed
+209 -51

No files matched your search

+100 -22
View File
@@ -45,6 +45,40 @@ func (s *fakeTranslateStore) WriteSessionWorkspaceFile(_ context.Context, _, pat
return nil
}
// fakeArtifactSink records artifacts appended to the assistant message.
type fakeArtifactSink struct {
interfaces.MessageService
messages map[string]*types.Message
updated int
}
func (s *fakeArtifactSink) GetMessage(_ context.Context, _ string, id string) (*types.Message, error) {
if msg, ok := s.messages[id]; ok {
return msg, nil
}
return nil, fmt.Errorf("message %s not found", id)
}
func (s *fakeArtifactSink) UpdateMessage(_ context.Context, msg *types.Message) error {
s.messages[msg.ID] = msg
s.updated++
return nil
}
// fakeTranslateFileService captures SaveBytes calls.
type fakeTranslateFileService struct {
interfaces.FileService
saved map[string][]byte
}
func (s *fakeTranslateFileService) SaveBytes(_ context.Context, data []byte, _ uint64, name string, _ bool) (string, error) {
if s.saved == nil {
s.saved = map[string][]byte{}
}
s.saved[name] = data
return "local://" + name, nil
}
// fakeTranslateTempDoc is an in-memory TemporaryDocumentService covering the
// surface xlsx_translate uses. Unimplemented methods panic via the embedded
// nil interface — the tool must not call them.
@@ -163,8 +197,11 @@ func buildToolFixture(t *testing.T) []byte {
return buf.Bytes()
}
func toolExecContext(sessionID string) context.Context {
return WithToolExecContext(context.Background(), &ToolExecContext{SessionID: sessionID})
func toolExecContext(sessionID, assistantMessageID string) context.Context {
return WithToolExecContext(context.Background(), &ToolExecContext{
SessionID: sessionID,
AssistantMessageID: assistantMessageID,
})
}
func sharedStringsFrom(t *testing.T, data []byte) string {
@@ -189,10 +226,10 @@ func TestXlsxTranslateToolEndToEndSandbox(t *testing.T) {
store := &fakeTranslateStore{files: map[string][]byte{
"/workspace/input/part.xlsx": buildToolFixture(t),
}}
tool := NewXlsxTranslateTool(store, store, nil, &fakeTranslateChat{})
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
result, err := tool.Execute(toolExecContext("sess-1"), args)
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if err != nil {
t.Fatalf("Execute: %v", err)
}
@@ -220,7 +257,8 @@ func TestXlsxTranslateToolEndToEndSandbox(t *testing.T) {
// TestXlsxTranslateToolWithoutSandbox covers the 192 deployment failure mode:
// no workspace sandbox anywhere in the session, so input comes from the
// uploaded session attachment and the deliverable is a preview URL.
// uploaded session attachment and the deliverable attaches to the assistant
// message as a chat artifact (artifact library + clickable card + download).
func TestXlsxTranslateToolWithoutSandbox(t *testing.T) {
tempDoc := &fakeTranslateTempDoc{
openBytes: map[string][]byte{"doc-1": buildToolFixture(t)},
@@ -228,31 +266,71 @@ func TestXlsxTranslateToolWithoutSandbox(t *testing.T) {
tempDoc.docs = append(tempDoc.docs, &types.TemporaryDocument{
ID: "doc-1", FileName: "D4-A08AR12000002-A3-SIP.xlsx", FileType: "xlsx",
})
tool := NewXlsxTranslateTool(nil, nil, tempDoc, &fakeTranslateChat{})
sink := &fakeArtifactSink{messages: map[string]*types.Message{
"assistant-1": {ID: "assistant-1", SessionID: "sess-1"},
}}
files := &fakeTranslateFileService{}
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{AttachmentName: "D4-A08AR12000002-A3-SIP.xlsx"})
result, err := tool.Execute(toolExecContext("sess-1"), args)
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if err != nil {
t.Fatalf("Execute: %v", err)
}
if !result.Success {
t.Fatalf("tool failed: %s", result.Error)
}
if len(tempDoc.created) != 1 {
t.Fatalf("expected one delivered attachment, got %d", len(tempDoc.created))
if !strings.Contains(result.Output, "file card") {
t.Fatalf("output should point at the artifact card: %s", result.Output)
}
created := tempDoc.created[0]
if created.FileName != "D4-A08AR12000002-A3-SIP_EN.xlsx" {
t.Fatalf("unexpected deliverable name: %s", created.FileName)
msg := sink.messages["assistant-1"]
if msg == nil || len(msg.Artifacts) != 1 {
t.Fatalf("expected one artifact on the assistant message, got %+v", msg)
}
wantURL := "/api/v1/sessions/sess-1/attachments/" + created.ID + "/preview"
if !strings.Contains(result.Output, wantURL) {
t.Fatalf("output missing preview URL %s: %s", wantURL, result.Output)
art := msg.Artifacts[0]
want := "D4-A08AR12000002-A3-SIP_EN.xlsx"
if art.FileName != want || art.FileType != ".xlsx" || art.Deleted() {
t.Fatalf("unexpected artifact: %+v", art)
}
ss := sharedStringsFrom(t, tempDoc.createdBytes[created.ID])
data, ok := files.saved[strings.TrimPrefix(art.URL, "local://")]
if !ok {
t.Fatalf("artifact bytes not saved: %s", art.URL)
}
ss := sharedStringsFrom(t, data)
if strings.ContainsAny(ss, "螺母") {
t.Fatalf("delivered workbook still contains Chinese: %s", ss)
}
if !strings.Contains(ss, "Assembly Process") {
t.Fatalf("English string lost: %s", ss)
}
}
// TestXlsxTranslateArtifactDeliveryIsIdempotent: a retried tool call must not
// duplicate the artifact on the assistant message.
func TestXlsxTranslateArtifactDeliveryIsIdempotent(t *testing.T) {
tempDoc := &fakeTranslateTempDoc{
openBytes: map[string][]byte{"doc-1": buildToolFixture(t)},
}
tempDoc.docs = append(tempDoc.docs, &types.TemporaryDocument{
ID: "doc-1", FileName: "part.xlsx", FileType: "xlsx",
})
sink := &fakeArtifactSink{messages: map[string]*types.Message{
"assistant-1": {ID: "assistant-1", SessionID: "sess-1"},
}}
files := &fakeTranslateFileService{}
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{AttachmentName: "part.xlsx"})
for i := 0; i < 2; i++ {
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if err != nil || !result.Success {
t.Fatalf("Execute run %d failed: %v / %s", i+1, err, result.Error)
}
}
if got := len(sink.messages["assistant-1"].Artifacts); got != 1 {
t.Fatalf("artifact duplicated on retry: %d", got)
}
}
// TestXlsxTranslateToolSandboxReadFallsBackToAttachment covers a mixed
@@ -267,10 +345,10 @@ func TestXlsxTranslateToolSandboxReadFallsBackToAttachment(t *testing.T) {
tempDoc.docs = append(tempDoc.docs, &types.TemporaryDocument{
ID: "doc-1", FileName: "part.xlsx", FileType: "xlsx",
})
tool := NewXlsxTranslateTool(store, store, tempDoc, &fakeTranslateChat{})
tool := NewXlsxTranslateTool(store, store, tempDoc, nil, nil, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
result, err := tool.Execute(toolExecContext("sess-1"), args)
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if err != nil {
t.Fatalf("Execute: %v", err)
}
@@ -286,10 +364,10 @@ func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
store := &fakeTranslateStore{files: map[string][]byte{
"/workspace/input/part.xls": {0xD0, 0xCF, 0x11, 0xE0},
}}
tool := NewXlsxTranslateTool(store, store, nil, &fakeTranslateChat{})
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xls"})
result, _ := tool.Execute(toolExecContext("sess-1"), args)
result, _ := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if result.Success {
t.Fatalf("legacy .xls should be rejected")
}
@@ -301,10 +379,10 @@ func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
func TestXlsxTranslateToolMissingFile(t *testing.T) {
store := &fakeTranslateStore{files: map[string][]byte{}}
tempDoc := &fakeTranslateTempDoc{}
tool := NewXlsxTranslateTool(store, store, tempDoc, &fakeTranslateChat{})
tool := NewXlsxTranslateTool(store, store, tempDoc, nil, nil, &fakeTranslateChat{})
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/missing.xlsx"})
result, _ := tool.Execute(toolExecContext("sess-1"), args)
result, _ := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
if result.Success {
t.Fatalf("missing file should fail")
}