RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(go-agent): surface CodeExec sandbox artifacts as message attachments (#19852)
mkaaad709952b41ba5
diff --git a/internal/agent/component/code_exec_artifact_test.go b/internal/agent/component/code_exec_artifact_test.gonew file mode 100644index 0000000000..67a43b3297--- /dev/null+++ b/internal/agent/component/code_exec_artifact_test.go@@ -0,0 +1,173 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package component++import (+ "context"+ "encoding/base64"+ "strings"+ "testing"++ "ragflow/internal/agent/runtime"+ agenttool "ragflow/internal/agent/tool"+ "ragflow/internal/storage"+)++// TestUploadCodeExecArtifacts pins the artifact upload path: sandbox+// artifacts (name/content_b64/mime_type/size) are stored under a+// fresh object name and surfaced with a downloadable URL, image+// markdown, and an attachment-count content section. Undecodable or+// un-uploadable entries are skipped, matching the Python skip-on-failure+// semantics.+func TestUploadCodeExecArtifacts(t *testing.T) {+ t.Parallel()++ st := storage.NewMemoryStorage()+ artifacts := []any{+ map[string]any{+ "name": "chart.png",+ "mime_type": "image/png",+ "size": float64(9),+ "content_b64": base64.StdEncoding.EncodeToString([]byte("fake-png")),+ },+ map[string]any{+ "name": "data.csv",+ "mime_type": "text/csv",+ "size": float64(5),+ "content_b64": base64.StdEncoding.EncodeToString([]byte("a,b\n1,2")),+ },+ // Undecodable payload must be skipped, not fail the batch.+ map[string]any{"name": "broken.txt", "content_b64": "!!not-base64!!"},+ // Missing payload must be skipped.+ map[string]any{"name": "empty.txt", "content_b64": ""},+ }++ uploaded, markdown, attachmentContent := uploadCodeExecArtifacts(context.Background(), artifacts, "sess-1", st)+ if len(uploaded) != 2 {+ t.Fatalf("uploaded len = %d, want 2", len(uploaded))+ }+ if len(markdown) != 2 {+ t.Fatalf("markdown len = %d, want 2", len(markdown))+ }++ url := uploaded[0]["url"].(string)+ if !strings.HasPrefix(url, "/api/v1/documents/artifact/") {+ t.Errorf("url = %q, want /api/v1/documents/artifact/ prefix", url)+ }+ if !strings.HasSuffix(url, ".png?session_id=sess-1") {+ t.Errorf("url = %q, want .png?session_id=sess-1 suffix", url)+ }+ if uploaded[0]["name"] != "chart.png" || uploaded[0]["mime_type"] != "image/png" {+ t.Errorf("uploaded[0] = %v, want chart.png image/png", uploaded[0])+ }++ if markdown[0] != "" {+ t.Errorf("markdown[0] = %q, want image markdown with url", markdown[0])+ }+ if !strings.HasPrefix(markdown[1], "[Download data.csv](") {+ t.Errorf("markdown[1] = %q, want download link", markdown[1])+ }++ if !strings.Contains(attachmentContent, "attachment_count: 2") {+ t.Errorf("attachmentContent = %q, want attachment_count: 2", attachmentContent)+ }+ if !strings.Contains(attachmentContent, "attachment1 (image): chart.png") {+ t.Errorf("attachmentContent = %q, want image section title", attachmentContent)+ }++ // The stored object must actually exist under the bucket with the+ // uuid-based name embedded in the URL.+ storageName := strings.TrimSuffix(strings.TrimPrefix(url, "/api/v1/documents/artifact/"), "?session_id=sess-1")+ if !st.ObjExist(context.Background(), "sandbox-artifacts", storageName) {+ t.Errorf("object %q not found in bucket sandbox-artifacts", storageName)+ }+}++// fakeSandboxClient adapts a function literal to the CodeExec+// SandboxClient interface.+type fakeSandboxClient func(ctx context.Context, req agenttool.SandboxRequest) (*agenttool.SandboxResponse, error)++func (f fakeSandboxClient) ExecuteCode(ctx context.Context, req agenttool.SandboxRequest) (*agenttool.SandboxResponse, error) {+ return f(ctx, req)+}++// TestCodeExecComponentInvokeAttachesArtifacts verifies the canvas+// CodeExec node turns sandbox-returned artifacts into message+// attachments: decoded["_ARTIFACTS"] carries upload URLs,+// decoded["attachments"] is populated, and content is appended with+// the attachment sections.+func TestCodeExecComponentInvokeAttachesArtifacts(t *testing.T) {+ prevClient := agenttool.GetSandboxClient()+ agenttool.SetSandboxClient(fakeSandboxClient(func(_ context.Context, _ agenttool.SandboxRequest) (*agenttool.SandboxResponse, error) {+ return &agenttool.SandboxResponse{+ ExitCode: 0,+ Metadata: map[string]any{+ "artifacts": []any{+ map[string]any{+ "name": "simple_plot.png",+ "mime_type": "image/png",+ "size": float64(10),+ "content_b64": base64.StdEncoding.EncodeToString([]byte("png-bytes")),+ },+ },+ },+ }, nil+ }))+ t.Cleanup(func() { agenttool.SetSandboxClient(prevClient) })++ factory := storage.GetStorageFactory()+ prevStorage := factory.GetStorage()+ factory.SetStorage(storage.NewMemoryStorage())+ t.Cleanup(func() { factory.SetStorage(prevStorage) })++ comp, err := newCodeExecComponent(nil)+ if err != nil {+ t.Fatalf("newCodeExecComponent: %v", err)+ }++ ctx := runtime.WithState(context.Background(), runtime.NewCanvasState("run-1", "sess-test"))+ decoded, err := comp.Invoke(ctx, nil, map[string]any{+ "lang": "python",+ "script": "def main():\n return {\"ok\": True}\n",+ })+ if err != nil {+ t.Fatalf("Invoke: %v", err)+ }++ artifacts, ok := decoded["_ARTIFACTS"].([]map[string]any)+ if !ok || len(artifacts) != 1 {+ t.Fatalf("_ARTIFACTS = %#v, want 1 uploaded artifact", decoded["_ARTIFACTS"])+ }+ entry := artifacts[0]+ url, _ := entry["url"].(string)+ if !strings.HasSuffix(url, ".png?session_id=sess-test") {+ t.Errorf("artifact url = %q, want session_id=sess-test suffix", url)+ }++ attachments, ok := decoded["attachments"].([]string)+ if !ok || len(attachments) != 1 {+ t.Fatalf("attachments = %#v, want 1 markdown entry", decoded["attachments"])+ }+ if attachments[0] != "" {+ t.Errorf("attachments[0] = %q, want image markdown", attachments[0])+ }++ content, _ := decoded["content"].(string)+ if !strings.Contains(content, "attachment_count: 1") {+ t.Errorf("content = %q, want attachment_count section appended", content)+ }+}diff --git a/internal/agent/component/universe_a_wrappers.go b/internal/agent/component/universe_a_wrappers.goindex 999916c3b1..b50a19cec2 100644--- a/internal/agent/component/universe_a_wrappers.go+++ b/internal/agent/component/universe_a_wrappers.go@@ -21,9 +21,11 @@ package componentimport ("context"+ "encoding/base64""encoding/json""errors""fmt"+ "path/filepath""regexp""strconv""strings"@@ -33,7 +35,9 @@ import ("ragflow/internal/common""ragflow/internal/dao""ragflow/internal/entity"+ "ragflow/internal/storage"+ "github.com/google/uuid""go.uber.org/zap""gorm.io/gorm")@@ -534,6 +538,14 @@ func (c *codeExecComponent) Invoke(ctx context.Context, db *gorm.DB, inputs map[decoded["_ERROR"] = ""}}+ // Upload sandbox-returned artifacts to object storage and surface+ // them as message attachments. Without this the tool's base64+ // `_ARTIFACTS` payload never reaches the UI: the canvas message+ // shows an empty `attachments` list even though the sandbox+ // collected files. Mirrors the Python code_exec tool+ // (_upload_artifacts / _build_attachment_markdown_list).+ attachCodeExecArtifacts(ctx, decoded)+if err != nil {return decoded, fmt.Errorf("canvas: CodeExec: %w", err)}@@ -752,3 +764,169 @@ var (// Compile-time check that the eino InvokableTool methods we call// are reachable (catches a future refactor that renames them).++// attachCodeExecArtifacts uploads the `_ARTIFACTS` produced by the+// CodeExec tool to object storage and populates the message-facing+// `attachments` / `_ATTACHMENT_CONTENT` outputs plus a content+// appendix. It is a no-op when the tool returned no artifacts.+func attachCodeExecArtifacts(ctx context.Context, decoded map[string]any) {+ raw, ok := decoded["_ARTIFACTS"].([]any)+ if !ok || len(raw) == 0 {+ return+ }+ st := storage.GetStorageFactory().GetStorage()+ if st == nil {+ common.Error("CodeExec: storage not initialized; cannot attach artifacts", nil)+ return+ }+ sessionID := ""+ if state, _, err := runtime.GetStateFromContext[*runtime.CanvasState](ctx); err == nil && state != nil {+ sessionID = state.SessionID+ }+ uploaded, markdown, attachmentContent := uploadCodeExecArtifacts(ctx, raw, sessionID, st)+ if len(uploaded) == 0 {+ return+ }+ decoded["_ARTIFACTS"] = uploaded+ decoded["attachments"] = markdown+ decoded["_ATTACHMENT_CONTENT"] = attachmentContent+ // Append the attachment sections to content, mirroring the Python+ // code_exec tool (content = "\"\n\n\"".join(content_parts)); when the+ // tool produced no base content the attachment text becomes the+ // content itself.+ if content, ok := decoded["content"].(string); ok && strings.TrimSpace(content) != "" {+ decoded["content"] = strings.TrimSpace(content + "\n\n" + attachmentContent)+ } else {+ decoded["content"] = strings.TrimSpace(attachmentContent)+ }+}++// codeExecArtifactBucket returns the object-storage bucket used for+// CodeExec sandbox artifacts. Mirrors the Python+// `SANDBOX_ARTIFACT_BUCKET` setting and the document service's+// default ("sandbox-artifacts").+func codeExecArtifactBucket() string {+ if bucket := common.GetEnv(common.EnvSandboxArtifactBucket); bucket != "" {+ return bucket+ }+ return "sandbox-artifacts"+}++// uploadCodeExecArtifacts uploads sandbox-returned artifacts to object+// storage and returns the attachment-facing payloads, mirroring the+// Python code_exec tool (_upload_artifacts / _build_attachment_markdown_list+// / _build_attachment_content):+//+// - uploaded: `_ARTIFACTS` entries with a downloadable url+// - markdown: `attachments` list (image preview / download links)+// - attachmentContent: `_ATTACHMENT_CONTENT` text (attachment_count + sections)+//+// Entries that cannot be decoded or stored are skipped with a warning,+// matching the Python "skip on failure" semantics.+func uploadCodeExecArtifacts(ctx context.Context, artifacts []any, sessionID string, st storage.Storage) (uploaded []map[string]any, markdown []string, attachmentContent string) {+ bucket := codeExecArtifactBucket()+ sections := make([]string, 0, len(artifacts))+ for _, raw := range artifacts {+ m, ok := raw.(map[string]any)+ if !ok {+ continue+ }+ name := codeExecArtifactString(m["name"])+ contentB64 := codeExecArtifactString(m["content_b64"])+ if name == "" || contentB64 == "" {+ continue+ }+ mimeType := codeExecArtifactString(m["mime_type"])+ size := codeExecArtifactInt64(m["size"])+ binary, err := base64.StdEncoding.DecodeString(contentB64)+ if err != nil || len(binary) == 0 {+ common.Warn("CodeExec: skip artifact with undecodable content_b64", zap.String("name", name))+ continue+ }++ storageName := uuid.NewString() + strings.ToLower(filepath.Ext(name))+ if err := st.Put(ctx, bucket, storageName, binary); err != nil {+ common.Warn("CodeExec: failed to upload artifact", zap.String("name", name), zap.String("storage", storageName), zap.Error(err))+ continue+ }+ url := fmt.Sprintf("/api/v1/documents/artifact/%s?session_id=%s", storageName, sessionID)+ uploaded = append(uploaded, map[string]any{+ "name": name,+ "url": url,+ "mime_type": mimeType,+ "size": size,+ })+ markdown = append(markdown, codeExecArtifactMarkdown(name, mimeType, url))+ sections = append(sections, codeExecAttachmentSection(len(sections)+1, name, mimeType, url))+ }+ if len(sections) > 0 {+ attachmentContent = fmt.Sprintf("attachment_count: %d\n\n%s", len(sections), strings.Join(sections, "\n\n"))+ } else {+ attachmentContent = "attachment_count: 0"+ }+ return uploaded, markdown, attachmentContent+}++// codeExecArtifactMarkdown renders one uploaded artifact as a Markdown+// link: image artifacts become inline images, everything else becomes a+// download link.+func codeExecArtifactMarkdown(name, mimeType, url string) string {+ if strings.HasPrefix(strings.ToLower(mimeType), "image/") && url != "" {+ return fmt.Sprintf("", name, url)+ }+ if url != "" {+ return fmt.Sprintf("[Download %s](%s)", name, url)+ }+ return name+}++// codeExecAttachmentSection renders the `attachmentN (type): name`+// section used by _ATTACHMENT_CONTENT.+func codeExecAttachmentSection(idx int, name, mimeType, url string) string {+ title := fmt.Sprintf("attachment%d (%s): %s", idx, codeExecAttachmentType(name, mimeType), name)+ return title + "\n" + codeExecArtifactMarkdown(name, mimeType, url)+}++// codeExecAttachmentType mirrors Python's _normalize_attachment_type.+func codeExecAttachmentType(name, mimeType string) string {+ mimeType = strings.ToLower(strings.TrimSpace(mimeType))+ switch {+ case strings.HasPrefix(mimeType, "image/"):+ return "image"+ case mimeType == "application/pdf":+ return "pdf"+ case mimeType == "text/csv":+ return "csv"+ case mimeType == "application/json":+ return "json"+ case mimeType == "text/html":+ return "html"+ }+ if ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(name)), "."); ext != "" {+ return ext+ }+ return "file"+}++func codeExecArtifactString(v any) string {+ if s, ok := v.(string); ok {+ return s+ }+ return ""+}++func codeExecArtifactInt64(v any) int64 {+ switch x := v.(type) {+ case int64:+ return x+ case int:+ return int64(x)+ case float64:+ return int64(x)+ case json.Number:+ if n, err := x.Int64(); err == nil {+ return n+ }+ }+ return 0+}diff --git a/internal/agent/tool/code_exec.go b/internal/agent/tool/code_exec.goindex 72572d8a45..8c86fc5d07 100644--- a/internal/agent/tool/code_exec.go+++ b/internal/agent/tool/code_exec.go@@ -249,20 +249,28 @@ func extractArtifactList(meta map[string]any, key string) []map[string]any {if !ok {return nil}- arr, ok := raw.([]any)- if !ok {- return nil- }- out := make([]map[string]any, 0, len(arr))- for i, item := range arr {- m, ok := item.(map[string]any)- if !ok {- fmt.Fprintf(os.Stderr, "code_exec: %s[%d] is %T, expected map[string]any; dropping\n", key, i, item)- continue+ switch arr := raw.(type) {+ case []map[string]any:+ // The sandbox providers decode the JSON array into+ // []map[string]any (see collectArtifacts in local.go,+ // ssh.go and self_managed.go), so accept that shape+ // directly. Without this the tool envelope silently+ // loses every artifact the sandbox collected.+ return arr+ case []any:+ out := make([]map[string]any, 0, len(arr))+ for i, item := range arr {+ m, ok := item.(map[string]any)+ if !ok {+ fmt.Fprintf(os.Stderr, "code_exec: %s[%d] is %T, expected map[string]any; dropping\n", key, i, item)+ continue+ }+ out = append(out, m)}- out = append(out, m)+ return out+ default:+ return nil}- return out}func codeExecStubResult(msg string) string {diff --git a/internal/agent/tool/code_exec_test.go b/internal/agent/tool/code_exec_test.goindex 4050a70d59..92aa53596d 100644--- a/internal/agent/tool/code_exec_test.go+++ b/internal/agent/tool/code_exec_test.go@@ -211,6 +211,44 @@ func TestCodeExec_ResultExtractsArtifacts(t *testing.T) {}}+// TestCodeExec_ResultExtractsArtifactsFromProviderShape pins the+// extractor against the shape the sandbox providers actually store:+// collectArtifacts (local.go / ssh.go / self_managed.go) returns+// []map[string]any, and the extractor must surface that directly as+// `_ARTIFACTS` in the tool envelope instead of dropping it (the+// []any assertion alone silently lost every sandbox artifact).+func TestCodeExec_ResultExtractsArtifactsFromProviderShape(t *testing.T) {+ t.Parallel()++ resp := &SandboxResponse{+ Returned: "ok",+ ExitCode: 0,+ Metadata: map[string]any{+ "artifacts": []map[string]any{+ {"name": "simple_plot.png", "mime_type": "image/png", "size": 20365, "content_b64": "aGVsbG8="},+ {"name": "data.csv", "mime_type": "text/csv", "size": 12, "content_b64": "YQpi"},+ },+ },+ }+ out, err := codeExecResultJSON(resp)+ if err != nil {+ t.Fatalf("codeExecResultJSON: %v", err)+ }+ var got codeExecResult+ if jerr := json.Unmarshal([]byte(out), &got); jerr != nil {+ t.Fatalf("output not valid JSON: %v (raw=%s)", jerr, out)+ }+ if len(got.Artifacts) != 2 {+ t.Fatalf("Artifacts len = %d, want 2 (raw=%s)", len(got.Artifacts), out)+ }+ if got.Artifacts[0]["name"] != "simple_plot.png" {+ t.Errorf("Artifacts[0][name] = %v, want simple_plot.png", got.Artifacts[0]["name"])+ }+ if got.Artifacts[1]["name"] != "data.csv" {+ t.Errorf("Artifacts[1][name] = %v, want data.csv", got.Artifacts[1]["name"])+ }+}+// TestCodeExec_ResultDropsBadArtifactShape ensures the extractor// silently drops entries that aren't map[string]any rather than// aborting the run.