Skip to content

fix(agent): persist plugin blob outputs and show attachments in logs - #43099

Closed
Souravrajvi0 wants to merge 3 commits into
langgenius:mainfrom
Souravrajvi0:feat/fix-agent-blob-tool-output-42114-665c
Closed

Souravrajvi0 wants to merge 3 commits into
langgenius:mainfrom
Souravrajvi0:feat/fix-agent-blob-tool-output-42114-665c

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Fixes #42114

Reopens the work from #42126 (closed due to merge conflicts with main).

When plugin tools return binary BLOB outputs (for example image generation), the standalone dify-agent plugin tools layer was stringifying bytes into tool observations (blob=b'\x89PNG...'), inflating model context and omitting generated files from chat and Agents logs.

This change:

  • Uploads binary plugin tool outputs through Dify's signed file-upload API
  • Returns compact tool observations plus reusable file references in ToolReturn.metadata
  • Persists assistant MessageFile rows and emits the existing chat file event
  • Includes persisted attachments in Agents log responses
  • Renders attachments in the Agents log detail panel

Rebased onto current main (conflict resolved in log-detail-panel.tsx after the LoadingPlaceholder refactor).

Test plan

  • dify-agent plugin layer tests for blob chunk merge + upload flow
  • dify-agent plugin layer test for image blob upload compact observation
  • API test for tool return metadata persisting assistant message files
  • API test for agent log serialization including message_files
  • Observability unit test mock updated for serialize_log_message(..., session=)

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

Upload binary plugin tool BLOB outputs through Dify's signed file API
instead of stringifying bytes into tool observations. Carry saved file
references in ToolReturn metadata, persist assistant MessageFile rows,
emit chat file events, include message_files in agent log responses,
and render attachments in the log detail panel.

Fixes langgenius#42114
Resolve log detail panel import conflict with LoadingPlaceholder refactor
and update observability test mock for serialize_log_message session kwarg.
@ghfind-review ghfind-review Bot added the review: high ghfind author score; see https://ghfind.com label Sep 28, 2026
@github-actions github-actions Bot added size:XL This PR changes 500-999 lines, ignoring generated files. web This relates to changes on the web. labels Sep 28, 2026
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 67.65% 67.65% +0.00%
Strict coverage 67.27% 67.27% +0.00%
Typed symbols 52,000 52,003 +3
Untyped symbols 25,010 25,010 0
Modules 3580 3580 0

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 42.10526% with 44 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.32%. Comparing base (e6f1d77) to head (c716b6e).

Files with missing lines Patch % Lines
api/core/app/apps/agent_app/app_runner.py 41.66% 26 Missing and 9 partials ⚠️
.../agent-detail/logs/components/log-detail-panel.tsx 36.36% 7 Missing ⚠️
api/services/agent/observability_service.py 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #43099      +/-   ##
==========================================
- Coverage   88.33%   88.32%   -0.02%     
==========================================
  Files        5133     5133              
  Lines      324423   324493      +70     
  Branches    65349    65375      +26     
==========================================
+ Hits       286593   286618      +25     
- Misses      32445    32480      +35     
- Partials     5385     5395      +10     
Flag Coverage Δ
api 87.96% <43.07%> (-0.02%) ⬇️
dify-ui 91.90% <ø> (ø)
web 88.79% <36.36%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@BeautyyuYanli

Copy link
Copy Markdown
Contributor

Thanks for investigating #42114 and putting together this PR, @Souravrajvi0!

We opened a fresh replacement Draft PR against current main in #46219 because several architectural and security boundaries changed since this branch was created:

  1. dify-agent runtime migration & #40455 scope boundary: dify-agent has migrated off the removed PlainLayer / Compositor stack onto FunctionToolset, and URL-bearing IMAGE / IMAGE_LINK plugin tool messages are tracked separately in fix(agent-app): deliver tool-generated images to the chat instead of dropping them #40455. fix(agent): persist plugin blob outputs and render attachments in chat and logs #46219 scopes changes strictly to binary BLOB / BLOB_CHUNK outputs, preserves streamed BLOB_CHUNK metadata across frames, always validates upload_uri via is_safe_dify_file_uri, emits reusable {"transfer_method": "tool_file", "reference": ...} observations, and avoids binary byte dumps on upload failure.
  2. Authoritative ToolFile ownership & URL derivation in AgentAppRunner: Rather than trusting URL/metadata fields carried in ToolReturn.metadata, fix(agent): persist plugin blob outputs and render attachments in chat and logs #46219 passes only candidate ToolFile UUIDs, enforces run ownership (tenant_id, user_id, conversation_id), derives file extension and /files/tools/<id><ext> URLs exclusively from verified ToolFile records in Postgres, updates MessageAgentThought.message_files, and normalizes historical/post-stream agent_thoughts attachments across Web chat surfaces.
  3. Read-only batch log serialization: AgentLogMessagesApi.get runs inside @with_session(write=False), where Message.message_files_with_session triggers N+1 queries and mutates/commits legacy ORM rows. fix(agent): persist plugin blob outputs and render attachments in chat and logs #46219 resolves UploadFile and ToolFile records in read-only batches in AgentObservabilityService and synchronizes the generated Console OpenAPI/TypeScript/Zod contracts.

Closing this PR in favor of #46219.

(This message is automated while I'm responsible for it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: high ghfind author score; see https://ghfind.com size:XL This PR changes 500-999 lines, ignoring generated files. web This relates to changes on the web.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Agents stringify binary plugin tool outputs and omit generated files from chat and logs

2 participants