Skip to content

fix(media): persist generated files, prevent duplicate delivery, and improve group media history#206

Open
vanducng wants to merge 4 commits intonextlevelbuilder:mainfrom
vanducng:fix/media-file-persistence-and-dedup
Open

fix(media): persist generated files, prevent duplicate delivery, and improve group media history#206
vanducng wants to merge 4 commits intonextlevelbuilder:mainfrom
vanducng:fix/media-file-persistence-and-dedup

Conversation

@vanducng
Copy link
Contributor

@vanducng vanducng commented Mar 15, 2026

Summary

  • Root cause: dispatch.go deleted ALL media files after channel send, including workspace-generated files (create_image, create_video, create_audio). This caused stat file: no such file errors and prevented files from persisting in workspace storage.
  • Duplicate delivery: When the LLM sent generated media via message tool (MEDIA: prefix), the same file was also sent via RunResult.Media, causing duplicate images/videos in channel chats. Previously masked by the file deletion bug.
  • Chain provider format: hasReadImageProvider only supported flat {"provider":"X"} format, not the chain format {"providers":[...]} used by media provider chain config.
  • Group media history: Discord and Zalo group chats lost media context when messages were recorded in pending history before @mention.

Changes

Fix: Media file persistence & dedup

File Change
dispatch.go Only delete temp files (/tmp/), not workspace files
loop.go Track media sent by message tool, deduplicate RunResult.Media
create_image.go Empty data guard + post-write stat verification with size logging
create_video.go Same as above
create_audio.go Same as above
send_helpers.go Log directory contents when file stat fails (diagnostics)

Feat: Chain provider format + group media history

File Change
media_tool_routing.go Support chain format {"providers":[...]} in hasReadImageProvider
history.go Add Media field to HistoryEntry, CollectMedia(), temp file cleanup on eviction/clear
discord/handler.go Record media paths in group history, collect pending media on @mention
zalo/personal/handlers.go Same pattern for Zalo group history

Test plan

  • Verified on production K8s (k3s + local-path PVC)
  • Generated image saved (847KB), persists on disk after send
  • No stat file: no such file errors in logs
  • Verify single image delivery (no duplicates) after dedup deploy
  • Verify TTS audio files from /tmp/ still cleaned up after send
  • Verify Discord/Zalo group media history works with @mention

@vanducng vanducng changed the title fix(media): persist generated files and prevent duplicate delivery fix(media): persist generated files, prevent duplicate delivery, and improve group media history Mar 15, 2026
Root cause: dispatch.go deleted ALL media files after channel send,
including workspace-generated files (create_image, create_video, etc.).
This caused "stat file: no such file" errors on subsequent send attempts
and prevented files from persisting in workspace storage.

Additionally, when the LLM used the message tool with MEDIA: prefix to
send generated media, the same file was sent again via RunResult.Media,
causing duplicate images/videos in channel chats.

Changes:
- dispatch.go: only delete temp files (/tmp/), not workspace files
- loop.go: track media sent by message tool, deduplicate RunResult.Media
- create_image/video/audio: add empty data guard, post-write verification
  with size logging for diagnostics
- send_helpers.go: log directory contents on stat failure for debugging
…Zalo

- media_tool_routing: support chain format {"providers":[...]} in
  hasReadImageProvider (was only flat {"provider":"X"})
- history: add Media field to HistoryEntry, CollectMedia() for pending
  entries, temp file cleanup on eviction/clear
- discord/handler: record media in group history, collect on @mention
- zalo/personal/handlers: same pattern for Zalo group history
@vanducng vanducng force-pushed the fix/media-file-persistence-and-dedup branch from 910bb09 to c69394c Compare March 15, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant