2.预览翻译文件时无法预览图片! 3.上传文件都要存到知识库中!可被问答 4.在SIP翻译专家agent下上传文件后默认给提示文案可直接发送
This commit is contained in:
1 parent
8c3d0ee31c
commit
90b843b4e8
9 files changed
+308
-75
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -84,6 +85,8 @@ type XlsxTranslateTool struct {
|
||||
tempDoc interfaces.TemporaryDocumentService
|
||||
files interfaces.FileService
|
||||
artifacts MessageArtifactSink
|
||||
knowledge interfaces.KnowledgeService
|
||||
ingestKB string // knowledge base the upload and its translation are filed into; "" disables ingestion
|
||||
model chat.Chat
|
||||
timeout time.Duration
|
||||
}
|
||||
@@ -104,6 +107,8 @@ func NewXlsxTranslateTool(
|
||||
tempDoc interfaces.TemporaryDocumentService,
|
||||
files interfaces.FileService,
|
||||
artifacts MessageArtifactSink,
|
||||
knowledge interfaces.KnowledgeService,
|
||||
ingestKB string,
|
||||
model chat.Chat,
|
||||
) *XlsxTranslateTool {
|
||||
return &XlsxTranslateTool{
|
||||
@@ -117,6 +122,8 @@ func NewXlsxTranslateTool(
|
||||
tempDoc: tempDoc,
|
||||
files: files,
|
||||
artifacts: artifacts,
|
||||
knowledge: knowledge,
|
||||
ingestKB: ingestKB,
|
||||
model: model,
|
||||
timeout: 20 * time.Minute,
|
||||
}
|
||||
@@ -203,6 +210,16 @@ func (t *XlsxTranslateTool) Execute(ctx context.Context, args json.RawMessage) (
|
||||
}
|
||||
deliverable = outName
|
||||
}
|
||||
// Requirement: uploads and their translations must be searchable. File
|
||||
// both into the agent's knowledge base when one is selected; ingestion
|
||||
// runs through the normal async pipeline, so failures never block
|
||||
// delivering the translated workbook.
|
||||
if t.ingestKB != "" && t.knowledge != nil {
|
||||
if warn := t.ingestToKnowledge(ctx, tenantID, source, data, outName, translated); warn != "" {
|
||||
report.Warnings = append(report.Warnings, warn)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "[Tool][XlsxTranslate] session=%s file=%s -> %s units=%d residual=%d llm_calls=%d",
|
||||
sessionID, source, deliverable, report.TranslatedUnits, report.ResidualCJKCount, report.LLMCalls)
|
||||
|
||||
@@ -353,6 +370,65 @@ func (t *XlsxTranslateTool) saveAsMessageArtifact(
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ingestToKnowledge files the original upload and the English translation
|
||||
// into the agent's knowledge base so both stay searchable by Q&A. Failures
|
||||
// are reported as a single warning line; ingestion is async and best-effort.
|
||||
func (t *XlsxTranslateTool) ingestToKnowledge(
|
||||
ctx context.Context, tenantID uint64, sourceName string, sourceData []byte, outName string, translated []byte,
|
||||
) string {
|
||||
if tenantID == 0 {
|
||||
tenantID = 1
|
||||
}
|
||||
var warnings []string
|
||||
if err := t.ingestOne(ctx, tenantID, sourceName, sourceData); err != nil {
|
||||
warnings = append(warnings, fmt.Sprintf("原文件未存入知识库:%v", err))
|
||||
}
|
||||
if err := t.ingestOne(ctx, tenantID, outName, translated); err != nil {
|
||||
warnings = append(warnings, fmt.Sprintf("翻译文件未存入知识库:%v", err))
|
||||
}
|
||||
if len(warnings) == 0 {
|
||||
logger.Infof(ctx, "[Tool][XlsxTranslate] ingested original + translation into knowledge base %s", t.ingestKB)
|
||||
return ""
|
||||
}
|
||||
return strings.Join(warnings, ";")
|
||||
}
|
||||
|
||||
// ingestOne wraps the bytes in a multipart file header for the knowledge
|
||||
// pipeline. The header is built from an in-memory multipart form, the same
|
||||
// approach the knowledge service's own tests use.
|
||||
func (t *XlsxTranslateTool) ingestOne(ctx context.Context, tenantID uint64, name string, data []byte) error {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
part, err := writer.CreateFormFile("file", name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := part.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
reader := multipart.NewReader(&body, writer.FormDataContentType())
|
||||
form, err := reader.ReadForm(int64(len(data)) + 1<<20)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files := form.File["file"]
|
||||
if len(files) == 0 {
|
||||
return fmt.Errorf("multipart form has no file part")
|
||||
}
|
||||
channel := "agent"
|
||||
_, err = t.knowledge.CreateKnowledgeFromFile(
|
||||
ctx, t.ingestKB, files[0], nil, nil, name, nil, channel, nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = tenantID
|
||||
return nil
|
||||
}
|
||||
|
||||
// chatBatchTranslator adapts the session chat model to the engine's
|
||||
// Translator interface: one call = one JSON array round trip.
|
||||
type chatBatchTranslator struct {
|
||||
|
||||
@@ -226,7 +226,7 @@ func TestXlsxTranslateToolEndToEndSandbox(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{
|
||||
"/workspace/input/part.xlsx": buildToolFixture(t),
|
||||
}}
|
||||
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, nil, "", &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
|
||||
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
|
||||
@@ -270,7 +270,7 @@ func TestXlsxTranslateToolWithoutSandbox(t *testing.T) {
|
||||
"assistant-1": {ID: "assistant-1", SessionID: "sess-1"},
|
||||
}}
|
||||
files := &fakeTranslateFileService{}
|
||||
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, nil, "", &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{AttachmentName: "D4-A08AR12000002-A3-SIP.xlsx"})
|
||||
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
|
||||
@@ -323,7 +323,7 @@ func TestXlsxTranslateArtifactDeliveryIsIdempotent(t *testing.T) {
|
||||
"assistant-1": {ID: "assistant-1", SessionID: "sess-1"},
|
||||
}}
|
||||
files := &fakeTranslateFileService{}
|
||||
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(nil, nil, tempDoc, files, sink, nil, "", &fakeTranslateChat{})
|
||||
args, _ := json.Marshal(XlsxTranslateInput{AttachmentName: "part.xlsx"})
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
@@ -349,7 +349,7 @@ 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, nil, nil, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, tempDoc, nil, nil, nil, "", &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
|
||||
result, err := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
|
||||
@@ -368,7 +368,7 @@ func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{
|
||||
"/workspace/input/part.xls": {0xD0, 0xCF, 0x11, 0xE0},
|
||||
}}
|
||||
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, nil, nil, nil, nil, "", &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xls"})
|
||||
result, _ := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
|
||||
@@ -383,7 +383,7 @@ func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
|
||||
func TestXlsxTranslateToolMissingFile(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{}}
|
||||
tempDoc := &fakeTranslateTempDoc{}
|
||||
tool := NewXlsxTranslateTool(store, store, tempDoc, nil, nil, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, tempDoc, nil, nil, nil, "", &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/missing.xlsx"})
|
||||
result, _ := tool.Execute(toolExecContext("sess-1", "assistant-1"), args)
|
||||
|
||||
Reference in new issue
Block a user