Skip to content

fix(agent): persist plugin blob outputs and render attachments in chat and logs - #46219

Open
BeautyyuYanli wants to merge 9 commits into
mainfrom
pr-43099-agent-blob-outputs
Open

BeautyyuYanli wants to merge 9 commits into
mainfrom
pr-43099-agent-blob-outputs

Conversation

@BeautyyuYanli

@BeautyyuYanli BeautyyuYanli commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

This PR is made with gpt-6.2-sol (Codex), gpt-6.1-sol (Codex), gemini-4-neon (Google Antigravity) and gemini-4-carbon (Google Antigravity), while I'm responsible for all the changes.

Fixes #42114
Supersedes #43099

Summary

When a plugin tool invoked by dify-agent emits binary BLOB or BLOB_CHUNK stream messages (such as PNG charts, PDFs, DOCX files, or audio), generated files were previously omitted from both chat attachments and Console Agent Logs (and chunked BLOB_CHUNK metadata could be dropped when the closing frame omitted meta).

This PR completes the binary plugin BLOB attachment delivery pipeline across dify-agent, api, packages/contracts, and web on top of the shared read_media / agent_files plane while keeping non-BLOB plugin media scope boundaries intact (tracked separately in #40455):

  • dify-agent (protocol/agent_stub.py, layers/agent_files.py, tool_client.py, tools_layer.py):
    • Preserve streamed BLOB_CHUNK metadata (filename, mime_type, etc.) across frames when merging into a terminal BLOB message even when the closing end=True chunk omits meta.
    • Keep is_safe_dify_file_uri and file_reference_record_id in client-safe dify_agent.agent_stub.protocol.agent_stub (re-exported in agent_stub_files.py) so core-only dify-agent consumers such as api never pull [server] optional dependencies (jwcrypto, fastapi), and enforce is_safe_dify_file_uri on upload_uri in upload_agent_tool_file.
    • Normalize untrusted MessageType.BLOB filename/MIME metadata before uploading via upload_agent_tool_file, preserve format_media_observation (read_media) and Tool media unavailable observation contracts, and return ToolReturn.metadata["dify_plugin_blob_files"] carrying persisted ToolFile IDs decoded from canonical dify-file-ref: references for BLOB / merged BLOB_CHUNK outputs.
  • api & packages/contracts (app_runner.py, agent_fields.py, agent_observability_repository.py, OpenAPI & generated contracts):
    • Validate ToolFile UUIDs and run ownership (tenant_id, user_id, conversation_id) in AgentAppRunner, deriving file extension and /files/tools/<id><ext> URLs exclusively from verified ToolFile rows, committing deduplicated assistant MessageFile records and MessageAgentThought.message_files atomically before publishing live QueueAgentThoughtEvent and QueueMessageFileEvent SSE items.
    • Expose message_files on AgentLogMessageItemResponse (GET /console/api/agent/<agent_id>/logs/<conversation_id>/messages) via read-only batch UploadFile / ToolFile resolution in AgentObservabilityQueries under session.no_autoflush (without ORM mutations, flushes, or commits), and synchronize Console OpenAPI markdown and TypeScript/Zod contracts.
  • web (useChat SSE boundary, AgentLogDetailPanel, & chat history/refresh normalization):
    • Normalize streamed agent_thought.message_files string IDs into thought.files while keeping thought.message_files strictly FileEntity[], routing live and resumed message_file events to their owning tool thoughts (including parallel tool calls and Agent v2 agent_response_parts).
    • Render user and assistant message_files in AgentLogDetailPanel, and share typed MessageFile -> FileEntity[] normalization (related_id: file.id) across Agent v2 preview chat history, post-stream completion refresh, WebApp chat-with-history, and embedded chatbot while skipping missing/deleted file IDs in addFileInfos.

Screenshots

Before After
Binary plugin BLOB / BLOB_CHUNK outputs were omitted or misrouted in live/reloaded chat thoughts and missing from Console Agent Logs. Binary plugin BLOB / BLOB_CHUNK outputs are persisted as ToolFile / MessageFile attachments, streamed to the owning tool thought via message_file, preserved on reload, compatible with read_media, and rendered in Console Agent Logs.

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

@github-actions github-actions Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. web This relates to changes on the web. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.10% 87.11% +0.00%
Strict coverage 86.76% 86.77% +0.00%
Typed symbols 96,358 96,378 +20
Untyped symbols 14,323 14,323 0
Modules 4647 4647 0

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-13 04:35:47.905072825 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-13 04:35:34.837083398 +0000
@@ -3189,25 +3189,25 @@
 ERROR Object of class `NoneType` has no attribute `id` [missing-attribute]
    --> tests/unit_tests/core/app/apps/agent_app/test_app_generator.py:782:20
 ERROR Class `FakeAgentBackendScenario` has no class attribute `PAUSED` [missing-attribute]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:199:35
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:203:35
 ERROR Argument `tuple[AgentAppSessionScope, str, SessionSnapshot | None]` is not assignable to parameter `object` with type `tuple[AgentAppSessionScope, str, SessionSnapshot | None, str | None, str | None]` in function `list.append` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:569:27
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:573:27
 ERROR Argument `Literal[0]` is not assignable to parameter `message_unit_price` with type `Decimal | SQLCoreOperations[Decimal]` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:689:28
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:693:28
 ERROR Argument `Literal[0]` is not assignable to parameter `message_price_unit` with type `Decimal | SQLCoreOperations[Decimal]` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:690:28
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:694:28
 ERROR Argument `Literal[0]` is not assignable to parameter `answer_unit_price` with type `Decimal | SQLCoreOperations[Decimal]` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:693:27
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:697:27
 ERROR Argument `Literal[0]` is not assignable to parameter `answer_price_unit` with type `Decimal | SQLCoreOperations[Decimal]` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:694:27
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:698:27
 ERROR Argument `Literal[0]` is not assignable to parameter `total_price` with type `Decimal | SQLCoreOperations[Decimal | None] | None` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:696:21
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:700:21
 ERROR Argument `Literal[core.app.entities.app_invoke_entities.InvokeFrom.WEB_APP]` is not assignable to parameter `invoke_from` with type `models.enums.InvokeFrom | SQLCoreOperations[models.enums.InvokeFrom | None] | None` in function `models.model.Message.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:698:21
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:702:21
 ERROR Cannot unpack tuple[AgentAppSessionScope, str, SessionSnapshot | None, str | None, str | None] (of size 5) into 3 values [bad-unpacking]
-   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:746:5
+   --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:750:5
 ERROR Object of class `NoneType` has no attribute `id` [missing-attribute]
-    --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:1061:12
+    --> tests/unit_tests/core/app/apps/agent_app/test_app_runner.py:1067:12
 ERROR Object of class `list` has no attribute `startswith` [missing-attribute]
    --> tests/unit_tests/core/app/apps/agent_app/test_runtime_request_builder.py:611:16
 ERROR Object of class `NoneType` has no attribute `kind` [missing-attribute]

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.33%. Comparing base (f5cae55) to head (30d34ea).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #46219      +/-   ##
==========================================
+ Coverage   90.31%   90.33%   +0.01%     
==========================================
  Files        6496     6458      -38     
  Lines      408541   408103     -438     
  Branches    81968    81836     -132     
==========================================
- Hits       368979   368649     -330     
+ Misses      33158    33053     -105     
+ Partials     6404     6401       -3     
Flag Coverage Δ
api 90.13% <100.00%> (+0.01%) ⬆️
cli 94.56% <ø> (ø)

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.

@github-actions github-actions Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Nov 13, 2026
@BeautyyuYanli
BeautyyuYanli marked this pull request as ready for review November 13, 2026 07:55
…utputs

# Conflicts:
#	api/repositories/agent_observability_repository.py
…utputs

# Conflicts:
#	dify-agent/docs/dify-agent/user-manual/plugin-tool-layer/index.md
#	dify-agent/src/dify_agent/agent_stub/protocol/agent_stub.py
#	dify-agent/src/dify_agent/layers/dify_plugin/tools_layer.py
…utputs

# Conflicts:
#	web/features/agent-v2/agent-editor/preview/__tests__/chat-history-fixtures.ts
#	web/features/agent-v2/agent-editor/preview/__tests__/chat-history.browser.spec.tsx
@github-actions

Copy link
Copy Markdown
Contributor

Vinext bundle analysis

JS gzip: 🟠 +9.36 KiB (+0.09%) · CSS gzip: 🟠 +293 B (+0.08%)

All client output Base gzip → Merge gzip Gzip change
JS 10339.12 KiB → 10348.48 KiB 🟠 +9.36 KiB (+0.09%)
CSS 340.76 KiB → 341.04 KiB 🟠 +293 B (+0.08%)

Entry changes: 🟠 205 increased · 🟢 16 decreased (gzip).

Largest entry changes

Top 5 by absolute gzip delta. Static dependencies overlap; do not sum entries.

Source entry Base gzip → Merge gzip Gzip change
app/components/workflow/nodes/agent-v2/panel.tsx 1603.04 KiB → 1605.93 KiB 🟠 +2.90 KiB (+0.18%)
client-reference: app/components/workflow-app/index.tsx 1450.91 KiB → 1453.24 KiB 🟠 +2.33 KiB (+0.16%)
client-reference: app/(commonLayout)/datasets/(datasetDetailLayout)/[datasetId]/pipeline/page.tsx 1382.17 KiB → 1384.44 KiB 🟠 +2.27 KiB (+0.16%)
client-reference: app/components/snippets/index.tsx 1345.06 KiB → 1347.32 KiB 🟠 +2.26 KiB (+0.17%)
app/components/workflow/nodes/knowledge-retrieval/panel.tsx 1388.35 KiB → 1390.60 KiB 🟠 +2.25 KiB (+0.16%)
Top 20 of 221 changed entries (full paths and raw sizes)
Source entry Base gzip → Merge gzip Gzip change Raw change
dynamic-entry: app/components/workflow/nodes/agent-v2/panel.tsx 1603.04 KiB → 1605.93 KiB 🟠 +2.90 KiB (+0.18%) 🟠 +3.73 KiB (+0.07%)
client-reference: app/components/workflow-app/index.tsx 1450.91 KiB → 1453.24 KiB 🟠 +2.33 KiB (+0.16%) 🟠 +2.45 KiB (+0.05%)
client-reference: app/(commonLayout)/datasets/(datasetDetailLayout)/[datasetId]/pipeline/page.tsx 1382.17 KiB → 1384.44 KiB 🟠 +2.27 KiB (+0.16%) 🟠 +2.73 KiB (+0.06%)
client-reference: app/components/snippets/index.tsx 1345.06 KiB → 1347.32 KiB 🟠 +2.26 KiB (+0.17%) 🟠 +2.70 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/knowledge-retrieval/panel.tsx 1388.35 KiB → 1390.60 KiB 🟠 +2.25 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/agent/panel.tsx 1446.22 KiB → 1448.47 KiB 🟠 +2.25 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/llm/panel.tsx 1358.16 KiB → 1360.40 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/human-input-v2/panel.tsx 1368.92 KiB → 1371.16 KiB 🟠 +2.24 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/parameter-extractor/panel.tsx 1342.54 KiB → 1344.78 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/features.tsx 1367.91 KiB → 1370.14 KiB 🟠 +2.24 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/human-input/panel.tsx 1349.83 KiB → 1352.07 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/template-transform/panel.tsx 1335.34 KiB → 1337.58 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/panel/chat-record/index.tsx 1401.35 KiB → 1403.59 KiB 🟠 +2.24 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/panel/debug-and-preview/index.tsx 1407.73 KiB → 1409.97 KiB 🟠 +2.24 KiB (+0.16%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/http/panel.tsx 1343.60 KiB → 1345.84 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/question-classifier/panel.tsx 1342.40 KiB → 1344.63 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/start/panel.tsx 1340.46 KiB → 1342.70 KiB 🟠 +2.24 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/if-else/panel.tsx 1338.99 KiB → 1341.23 KiB 🟠 +2.23 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/snippets/components/snippet-run-panel.tsx 1335.04 KiB → 1337.27 KiB 🟠 +2.23 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
dynamic-entry: app/components/workflow/nodes/document-extractor/panel.tsx 1332.63 KiB → 1334.87 KiB 🟠 +2.23 KiB (+0.17%) 🟠 +2.58 KiB (+0.06%)
📦 11 package attribution changes (uncompressed modules)

Showing 11 of 11, ordered by absolute module-size delta.

Package Base Merge Δ
mediabunny 725.77 KiB 767.35 KiB 🟠 +41.58 KiB (+5.73%)
motion-dom 474.59 KiB 487.86 KiB 🟠 +13.27 KiB (+2.80%)
es-toolkit 104.91 KiB 111.36 KiB 🟠 +6.44 KiB (+6.14%)
tldts-core 41.33 KiB 47.37 KiB 🟠 +6.04 KiB (+14.61%)
dompurify 83.81 KiB 79.66 KiB 🟢 −4.15 KiB (−4.95%)
cron-parser 99.25 KiB 100.43 KiB 🟠 +1.18 KiB (+1.19%)
@mediabunny/mp3-encoder 469.29 KiB 470.30 KiB 🟠 +1.01 KiB (+0.22%)
@heroicons/react 8.36 KiB 7.58 KiB 🟢 −802 B (−9.36%)
tldts 147.72 KiB 148.30 KiB 🟠 +599 B (+0.40%)
i18next 90.12 KiB 90.64 KiB 🟠 +536 B (+0.58%)
framer-motion 86.03 KiB 86.50 KiB 🟠 +482 B (+0.55%)
Measurement details and build revisions

Base: f5cae556ed968ff2dc61ebdfc37c82f00b39c6c4 · Merge: 1101734f36403e09fe508fd412df624864c2befe
Vinext: 1.4.5 → 1.4.5; Rolldown: 1.2.19 → 1.2.19

Raw JS change: 🟠 +20.95 KiB (+0.06%) · Raw CSS change: 🟠 +2.15 KiB (+0.12%)

🟠 Increase · 🟢 Decrease · ⚪ Unchanged. Colors indicate direction, not a performance budget verdict. Percentages use the base size.

Entries follow only static edges in the official Rolldown chunk graph, deduplicating shared files. Dynamic entries are measured when loaded; they are not page first-load metrics. Entry totals overlap and must not be summed.
Package attribution sums module sizes reported by Rolldown, not compressed emitted bytes. Gzip totals are measured per emitted file. This is Vinext output, not Next.js/Turbopack output.
Report only: increases do not fail this check. Missing or incompatible analysis data does.

Download base/merge snapshots and official Rolldown analysis reports.

Comparison: event base SHA vs the tested merge commit at GITHUB_SHA (PR or merge queue).

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

Labels

size:XXL This PR changes 1000+ 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

1 participant