Repository navigation
fix(agent): collect CodeExec artifacts from ReAct tool responses - #16609
Conversation
Use react.WithMessageFuture() so the Go AgentComponent can inspect every tool response produced during the ReAct loop. Collect entries from the _ARTIFACTS JSON envelope returned by the CodeExec tool and append the artifact markdown to the final content, matching the Python backend behavior. - Add context-based artifact collector around the MessageFuture. - Parse _ARTIFACTS from tool message Content or UserInputMultiContent. - De-duplicate artifacts by URL and render images as . - Add regression test covering artifact collection from CodeExec-style tool responses. Fixes: agent ignored CodeExec artifacts and returned plain text only.
📝 WalkthroughWalkthroughThe Agent component's ReAct execution now captures a ChangesArtifact Collection Feature
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Invoke as AgentComponent.Invoke
participant Run as runEinoReActAgent
participant Generate as agent.Generate
participant Future as MessageFuture
participant Collector as collectArtifactsFromToolCalls
Invoke->>Run: start ReAct run
Run->>Future: react.WithMessageFuture()
Run->>Generate: Generate(ctx, input, futureOption)
Generate-->>Future: emits tool call messages
Invoke->>Collector: collectArtifactsFromToolCalls(ctx, msg)
Collector->>Future: drain tool-role messages
Future-->>Collector: tool response JSON content
Collector-->>Invoke: de-duplicated artifactEntry list
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/agent/component/agent.go`:
- Around line 164-166: The future is only stored in the local child context
inside runEinoReActAgent, so Invoke still sees a ctx without an artifact
collector and collection is skipped. Move the future holder setup into Invoke
before calling agentRunner(ctx, p), and update getArtifactCollector to read the
future from that shared holder instead of relying on the local context copy.
Keep the existing react.WithMessageFuture flow, but ensure the future is visible
to both Invoke and runEinoReActAgent through the shared context path.
- Around line 742-746: extractArtifactsFromToolMessage currently stops after the
first non-empty text source, so it can miss artifacts when msg.Content has text
and _ARTIFACTS appears later in UserInputMultiContent. Update this logic to
inspect all available text payloads in order—starting with Content, then each
text part from UserInputMultiContent, using toolMessageTextContent or
equivalent—until an artifact envelope is found. Apply the same multi-source scan
behavior in the related artifact parsing path around the referenced extraction
helpers so both locations detect artifacts consistently.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 19e70479-7a37-415e-a775-008016992e55
📒 Files selected for processing (2)
internal/agent/component/agent.gointernal/agent/component/agent_artifact_test.go
| opt, future := react.WithMessageFuture() | ||
| ctx = setArtifactCollector(ctx, future) | ||
| msg, err := agent.Generate(ctx, input, opt) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Propagate the future through a context visible to Invoke.
Line 165 stores the future in a child context local to runEinoReActAgent; the caller’s ctx at Line 540 is unchanged, so getArtifactCollector(ctx) returns nil and production artifact collection is skipped.
Possible fix direction
+type artifactCollector struct {
+ future react.MessageFuture
+}
+
+func withArtifactCollector(ctx context.Context) context.Context {
+ return context.WithValue(ctx, artifactCollectorKey{}, &artifactCollector{})
+}
+
func runEinoReActAgent(ctx context.Context, p AgentParam) (*schema.Message, error) {
...
opt, future := react.WithMessageFuture()
- ctx = setArtifactCollector(ctx, future)
+ setArtifactCollector(ctx, future)
msg, err := agent.Generate(ctx, input, opt)
...
}Then initialize the holder in Invoke before agentRunner(ctx, p) and have getArtifactCollector read holder.future.
Also applies to: 540-540
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/agent/component/agent.go` around lines 164 - 166, The future is only
stored in the local child context inside runEinoReActAgent, so Invoke still sees
a ctx without an artifact collector and collection is skipped. Move the future
holder setup into Invoke before calling agentRunner(ctx, p), and update
getArtifactCollector to read the future from that shared holder instead of
relying on the local context copy. Keep the existing react.WithMessageFuture
flow, but ensure the future is visible to both Invoke and runEinoReActAgent
through the shared context path.
| func extractArtifactsFromToolMessage(msg *schema.Message) []artifactEntry { | ||
| payload := msg.Content | ||
| if payload == "" && len(msg.UserInputMultiContent) > 0 { | ||
| payload = toolMessageTextContent(msg) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Try all text payload sources, not only the first non-empty one.
If msg.Content contains non-artifact text while _ARTIFACTS is in UserInputMultiContent, this returns no artifacts. Parse Content plus every text part until an envelope is found.
Suggested adjustment
func extractArtifactsFromToolMessage(msg *schema.Message) []artifactEntry {
- payload := msg.Content
- if payload == "" && len(msg.UserInputMultiContent) > 0 {
- payload = toolMessageTextContent(msg)
- }
- if payload == "" {
- return nil
- }
-
- var envelope map[string]any
- if err := json.Unmarshal([]byte(payload), &envelope); err != nil {
- return nil
- }
+ for _, payload := range toolMessageTextPayloads(msg) {
+ var envelope map[string]any
+ if err := json.Unmarshal([]byte(payload), &envelope); err != nil {
+ continue
+ }
- raw, ok := envelope["_ARTIFACTS"].([]any)
- if !ok {
- return nil
- }
+ raw, ok := envelope["_ARTIFACTS"].([]any)
+ if !ok {
+ continue
+ }
- out := make([]artifactEntry, 0, len(raw))
- for _, item := range raw {
- m, ok := item.(map[string]any)
- if !ok {
- continue
- }
- name, _ := m["name"].(string)
- url, _ := m["url"].(string)
- if name == "" || url == "" {
- continue
+ out := make([]artifactEntry, 0, len(raw))
+ for _, item := range raw {
+ m, ok := item.(map[string]any)
+ if !ok {
+ continue
+ }
+ name, _ := m["name"].(string)
+ url, _ := m["url"].(string)
+ if name == "" || url == "" {
+ continue
+ }
+ out = append(out, artifactEntry{Name: name, URL: url})
}
- out = append(out, artifactEntry{Name: name, URL: url})
+ return out
}
- return out
+ return nil
}
-func toolMessageTextContent(msg *schema.Message) string {
+func toolMessageTextPayloads(msg *schema.Message) []string {
+ var payloads []string
+ if msg.Content != "" {
+ payloads = append(payloads, msg.Content)
+ }
for i := range msg.UserInputMultiContent {
part := &msg.UserInputMultiContent[i]
if part.Type == schema.ChatMessagePartTypeText && part.Text != "" {
- return part.Text
+ payloads = append(payloads, part.Text)
}
}
- return ""
+ return payloads
}Also applies to: 779-785
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/agent/component/agent.go` around lines 742 - 746,
extractArtifactsFromToolMessage currently stops after the first non-empty text
source, so it can miss artifacts when msg.Content has text and _ARTIFACTS
appears later in UserInputMultiContent. Update this logic to inspect all
available text payloads in order—starting with Content, then each text part from
UserInputMultiContent, using toolMessageTextContent or equivalent—until an
artifact envelope is found. Apply the same multi-source scan behavior in the
related artifact parsing path around the referenced extraction helpers so both
locations detect artifacts consistently.
Summary
The Go backend Agent component was not returning artifacts produced by the CodeExec tool. While the Python agent collects the "
_ARTIFACTS" envelope from tool responses and appends artifact markdown to the final content, the Go agent only returned the assistant text, so generated images were missing from the chat output.Changes
react.WithMessageFuture()inrunEinoReActAgentand store the resultingMessageFuturein the invocation context._ARTIFACTSentries from every tool response message.msg.Contentandmsg.UserInputMultiContentto match eino's tool contract.!and other files as download links.agent_artifact_test.gowith a regression test that simulates a CodeExec-style tool response carrying an image artifact and verifies it is collected and formatted.Verification
go test ./internal/agent/component/... -run TestAgent_ReActAgent_CollectsArtifactsFromCodeExecToolpasses.go test ./internal/agent/component/... -count=1compiles; the only failure is an unrelated DNS-pinning timeout test (TestInvoke_ProxyDNSPin).gofmtclean for modified files.Related
Fixes the behavior shown in the screenshot where the Go agent ignored the CodeExec-generated PNG artifact.