Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix(codeexec): codeexec artifact attachments (#19954)

mkaaad1be89cbde1f4
diff --git a/internal/agent/component/agent.go b/internal/agent/component/agent.go
index bdcde1c718..aeb0e5677d 100644
--- a/internal/agent/component/agent.go
+++ b/internal/agent/component/agent.go
@@ -1011,8 +1011,17 @@ func (c *AgentComponent) invokeNow(ctx context.Context, db *gorm.DB, inputs map[
out["grounding_status"] = groundingStatus
}
streamed := runtime.AgentMessageEventsEmitted(ctx) || runtime.DeferredAgentMessageEventsEmitted(ctx)
- if !streamed {
+ switch {
+ case !streamed:
runtime.EmitAgentMessage(ctx, content+artifactMD, thinking)
+ case artifactMD != "":
+ // Python's stream_output_with_tools_async yields the tool-artifact
+ // markdown as a trailing delta after the LLM stream, so the live SSE
+ // stream (and hence the chat) includes the artifact references even
+ // when the model did not embed them itself. The Go port previously
+ // appended them only to the recorded output and skipped live emission
+ // once the stream had run, so artifact images never appeared in chat.
+ runtime.EmitAgentMessage(ctx, artifactMD, "")
}
return out, nil
}
diff --git a/internal/agent/component/agent_artifact_test.go b/internal/agent/component/agent_artifact_test.go
index 386782727c..3abbfb108e 100644
--- a/internal/agent/component/agent_artifact_test.go
+++ b/internal/agent/component/agent_artifact_test.go
@@ -12,6 +12,8 @@ import (
"github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/flow/agent/react"
"github.com/cloudwego/eino/schema"
+
+ "ragflow/internal/agent/runtime"
)
// artifactTool is an invokable tool that returns a JSON envelope with _ARTIFACTS.
@@ -271,3 +273,88 @@ func (t *passthroughTool) InvokableRun(_ context.Context, argumentsInJSON string
t.calls.Add(1)
return argumentsInJSON, nil
}
+
+// TestAgent_StreamedArtifactMarkdownIsEmittedLive verifies Python parity for
+// the streaming path: when the LLM already streamed its answer (so
+// AgentMessageEventsEmitted is true), the tool-artifact markdown appended to
+// the recorded content must ALSO be emitted as a trailing live delta.
+// Python's stream_output_with_tools_async yields "\n\n" + artifact_md after
+// the LLM stream; without this the chat shows the model text but never the
+// artifact image (the frontend only ever receives what is emitted live).
+func TestAgent_StreamedArtifactMarkdownIsEmittedLive(t *testing.T) {
+ var emitted strings.Builder
+ ctx := runtime.WithAgentMessageEmitter(t.Context(), func(content, _ string) {
+ emitted.WriteString(content)
+ })
+
+ const artifactURL = "/api/v1/documents/artifact/8e6a1d70-f69c-4e78-b188-c04df906e34e.png"
+ const wantMD = "![sales_axes.png](" + artifactURL + ")"
+
+ withAgentRunner(t, func(runCtx context.Context, _ AgentParam) (*schema.Message, error) {
+ // Seed the artifact collector (invokeNow prepares it before the runner).
+ if collector, ok := runCtx.Value(artifactCollectorKey{}).(*artifactCollector); ok {
+ collector.artifacts = []artifactEntry{{
+ Name: "sales_axes.png",
+ URL: artifactURL,
+ MIMEType: "image/png",
+ }}
+ }
+ // Simulate the LLM having streamed its answer without embedding the
+ // artifact URL, then return the final assistant message.
+ runtime.EmitAgentMessage(runCtx, "The chart is ready.", "")
+ return &schema.Message{Role: schema.Assistant, Content: "The chart is ready."}, nil
+ })
+
+ c := NewAgentComponent(AgentParam{ModelID: "stub", MaxRounds: 1})
+ out, err := c.Invoke(ctx, nil, map[string]any{"user_prompt": "draw a chart"})
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+
+ if got := emitted.String(); !strings.Contains(got, wantMD) {
+ t.Errorf("live-emitted content = %q, want it to contain %q", got, wantMD)
+ }
+ if content, ok := out["content"].(string); !ok || !strings.Contains(content, wantMD) {
+ t.Errorf("recorded content = %v, want it to contain %q", out["content"], wantMD)
+ }
+}
+
+// TestAgent_UnstreamedArtifactMarkdownNotDoubleEmitted guards the non-streamed
+// path: when no delta was emitted live, the whole content+artifactMD is emitted
+// once and the recorded content matches, with no duplicate artifact link.
+func TestAgent_UnstreamedArtifactMarkdownRecorded(t *testing.T) {
+ var emitted strings.Builder
+ ctx := runtime.WithAgentMessageEmitter(t.Context(), func(content, _ string) {
+ emitted.WriteString(content)
+ })
+
+ const artifactURL = "/api/v1/documents/artifact/9946035d-975d-450a-82e5-ff028ede857d.png"
+ const wantMD = "![simple_plot.png](" + artifactURL + ")"
+
+ withAgentRunner(t, func(runCtx context.Context, _ AgentParam) (*schema.Message, error) {
+ if collector, ok := runCtx.Value(artifactCollectorKey{}).(*artifactCollector); ok {
+ collector.artifacts = []artifactEntry{{
+ Name: "simple_plot.png",
+ URL: artifactURL,
+ MIMEType: "image/png",
+ }}
+ }
+ // No EmitAgentMessage here: nothing was streamed, so the whole answer
+ // is emitted at once by invokeNow.
+ return &schema.Message{Role: schema.Assistant, Content: "done"}, nil
+ })
+
+ c := NewAgentComponent(AgentParam{ModelID: "stub", MaxRounds: 1})
+ out, err := c.Invoke(ctx, nil, map[string]any{"user_prompt": "run"})
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+
+ got := emitted.String()
+ if got != "done\n\n"+wantMD {
+ t.Errorf("live-emitted content = %q, want %q", got, "done\n\n"+wantMD)
+ }
+ if content, ok := out["content"].(string); !ok || content != "done\n\n"+wantMD {
+ t.Errorf("recorded content = %v, want %q", out["content"], "done\n\n"+wantMD)
+ }
+}
diff --git a/internal/agent/component/code_exec_artifact_test.go b/internal/agent/component/code_exec_artifact_test.go
new file mode 100644
index 0000000000..f96e15b812
--- /dev/null
+++ b/internal/agent/component/code_exec_artifact_test.go
@@ -0,0 +1,200 @@
+//
+// 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] != "!["+"chart.png]("+url+")" {
+ 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)
+ }
+
+ // Entries that already carry a hosted url (published by the
+ // CodeExec tool) are reused verbatim — no re-upload happens, the
+ // markdown points at the tool's canonical URL.
+ published, pubMarkdown, pubContent := uploadCodeExecArtifacts(context.Background(), []any{
+ map[string]any{
+ "name": "already_hosted.png",
+ "url": "/api/v1/documents/artifact/abc.png",
+ "mime_type": "image/png",
+ "size": float64(7),
+ },
+ }, "sess-1", st)
+ if len(published) != 1 {
+ t.Fatalf("published len = %d, want 1", len(published))
+ }
+ if got := published[0]["url"].(string); got != "/api/v1/documents/artifact/abc.png" {
+ t.Errorf("published url = %q, want tool URL reused verbatim", got)
+ }
+ if len(pubMarkdown) != 1 || pubMarkdown[0] != "!["+"already_hosted.png](/api/v1/documents/artifact/abc.png)" {
+ t.Errorf("pubMarkdown = %#v, want image markdown from tool URL", pubMarkdown)
+ }
+ if !strings.Contains(pubContent, "attachment_count: 1") {
+ t.Errorf("pubContent = %q, want attachment_count: 1", pubContent)
+ }
+}
+
+// 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)
+ // The CodeExec tool hosts the blob and publishes the canonical
+ // /api/v1/documents/artifact/<name> URL (no session_id suffix);
+ // the component must consume it as-is.
+ if !strings.HasPrefix(url, "/api/v1/documents/artifact/") || !strings.HasSuffix(url, ".png") || strings.Contains(url, "?") {
+ t.Errorf("artifact url = %q, want hosted /api/v1/documents/artifact/<name>.png URL", url)
+ }
+
+ attachments, ok := decoded["attachments"].([]string)
+ if !ok || len(attachments) != 1 {
+ t.Fatalf("attachments = %#v, want 1 markdown entry", decoded["attachments"])
+ }
+ if attachments[0] != "!["+"simple_plot.png]("+url+")" {
+ 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.go
index 999916c3b1..86518e4729 100644
--- a/internal/agent/component/universe_a_wrappers.go
+++ b/internal/agent/component/universe_a_wrappers.go
@@ -21,9 +21,11 @@ package component
import (
"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,178 @@ 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
+ }
+ sessionID := ""
+ if state, _, err := runtime.GetStateFromContext[*runtime.CanvasState](ctx); err == nil && state != nil {
+ sessionID = state.SessionID
+ }
+ // The CodeExec tool already hosts sandbox artifacts and surfaces
+ // them as `_ARTIFACTS` entries carrying a `url`; storage is only
+ // needed for the legacy content_b64 fallback below.
+ uploaded, markdown, attachmentContent := uploadCodeExecArtifacts(ctx, raw, sessionID, storage.GetStorageFactory().GetStorage())
+ 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"])
+ if name == "" {
+ continue
+ }
+ mimeType := codeExecArtifactString(m["mime_type"])
+ size := codeExecArtifactInt64(m["size"])
+ url := codeExecArtifactString(m["url"])
+ if url == "" {
+ // Legacy / direct-producer envelopes still carry the
+ // base64 blob (content_b64) instead of a hosted url.
+ contentB64 := codeExecArtifactString(m["content_b64"])
+ if contentB64 == "" {
+ continue
+ }
+ 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
+ }
+ if st == nil {
+ common.Warn("CodeExec: storage not initialized; cannot upload artifact", 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("![%s](%s)", 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.go
index 3d7d38ef32..04196c704f 100644
--- a/internal/agent/tool/code_exec.go
+++ b/internal/agent/tool/code_exec.go
@@ -49,7 +49,12 @@ const codeExecToolName = "execute_code"
const codeExecToolDescription = "This tool has a sandbox that can execute code written in 'Python'/'Javascript'. " +
"It receives a piece of code and returns a JSON string. " +
"The code must define a main function (Python) or export main (JavaScript); " +
- "the return value of main is returned as the tool result."
+ "the return value of main is returned as the tool result. " +
+ "To generate charts or files (images, PDFs, CSVs, etc.), save them to the `artifacts/` " +
+ "directory (relative to the working directory); the sandbox automatically collects those " +
+ "files and returns them as artifacts. " +
+ "Example: `plt.savefig('artifacts/chart.png', dpi=150, bbox_inches='tight')`. " +
+ "Supported artifact file types: .png, .jpg, .jpeg, .svg, .pdf, .csv, .json, .html."
// codeExecArgs is the JSON shape the model sends in. The Python
// tool accepts "lang" + "script"; we also accept "code" as a
@@ -339,20 +344,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.go
index d6b5adc7c7..e86f9e1147 100644
--- a/internal/agent/tool/code_exec_test.go
+++ b/internal/agent/tool/code_exec_test.go
@@ -218,6 +218,36 @@ 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()
+
+ // The sandbox providers (local.go / ssh.go / self_managed.go)
+ // store Metadata["artifacts"] as []map[string]any; the extractor
+ // must surface that shape instead of dropping it. The []any
+ // assertion alone silently lost every sandbox artifact.
+ got := extractArtifactList(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"},
+ },
+ }, "artifacts")
+ if len(got) != 2 {
+ t.Fatalf("extractArtifactList len = %d, want 2", len(got))
+ }
+ if got[0]["name"] != "simple_plot.png" {
+ t.Errorf("got[0][name] = %v, want simple_plot.png", got[0]["name"])
+ }
+ if got[1]["name"] != "data.csv" {
+ t.Errorf("got[1][name] = %v, want data.csv", got[1]["name"])
+ }
+}
+
// TestCodeExec_ResultDropsBadArtifactShape ensures the extractor
// silently drops entries that aren't map[string]any, and entries
// without a URL or uploadable payload, rather than aborting the run.