Skip to content

Enhance message building with media caching#15

Open
XGH2333 wants to merge 1 commit into
CharTyr:mainfrom
XGH2333:patch-1
Open

Enhance message building with media caching#15
XGH2333 wants to merge 1 commit into
CharTyr:mainfrom
XGH2333:patch-1

Conversation

@XGH2333
Copy link
Copy Markdown

@XGH2333 XGH2333 commented Apr 23, 2026

修改图片处理逻辑-将图片base64 作为附件传给 Gateway

Summary by Sourcery

Cache incoming media and send images as binary attachments instead of only text references when building and dispatching messages to the gateway.

Enhancements:

  • Exclude images from inline text media summaries now that they are sent via attachments, while keeping textual hints for files, voice, and video media.
  • Read cached media files, encode them as base64, and include them as typed attachments (with MIME where applicable) in outgoing chat.send requests.

修改图片处理逻辑-将图片base64 作为附件传给 Gateway
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 23, 2026

Reviewer's Guide

Enhances message-building to cache media and send image contents as base64 attachments to the Gateway, while avoiding redundant image references in the text message.

Sequence diagram for enhanced message sending with media caching

sequenceDiagram
  actor User
  participant Plugin_onmessage as plugin_onmessage
  participant MediaCache as saveMediaToCache
  participant FileSystem as fs_cache
  participant GatewayClient as gwClient
  participant GatewayService as Gateway

  User->>Plugin_onmessage: incoming_event
  activate Plugin_onmessage

  Plugin_onmessage->>Plugin_onmessage: extract text and extractedMedia
  alt has_media
    Plugin_onmessage->>MediaCache: saveMediaToCache(extractedMedia, ctx)
    activate MediaCache
    MediaCache->>FileSystem: write media files to cache
    FileSystem-->>MediaCache: cached file paths
    deactivate MediaCache
    MediaCache-->>Plugin_onmessage: savedMedia

    Plugin_onmessage->>Plugin_onmessage: build mediaInfo excluding images
    Plugin_onmessage->>Plugin_onmessage: append mediaInfo to openclawMessage

    Plugin_onmessage->>FileSystem: readFile for each m in savedMedia with path
    FileSystem-->>Plugin_onmessage: image buffers
    Plugin_onmessage->>Plugin_onmessage: encode to base64, guessMimeFromUrl
    Plugin_onmessage->>Plugin_onmessage: build attachments array
  else no_media
    Plugin_onmessage->>Plugin_onmessage: use text only as openclawMessage
  end

  Plugin_onmessage->>GatewayClient: request chat.send(sessionKey, message, idempotencyKey, attachments)
  activate GatewayClient
  GatewayClient->>GatewayService: chat.send(payload with optional attachments)
  GatewayService-->>GatewayClient: sendResult
  deactivate GatewayClient

  GatewayClient-->>Plugin_onmessage: sendResult
  deactivate Plugin_onmessage
Loading

File-Level Changes

Change Details Files
Refine message text construction so only non-image media generate inline text hints, preventing duplicate references for images now sent as attachments.
  • Introduce a reusable savedMedia variable to hold cached media for use in both message composition and attachment building.
  • Filter cached media to exclude images when generating human-readable media info lines.
  • Only append media info block to the main message text when it is non-empty.
src/index.ts
Add support for building and sending media attachments (base64-encoded files) with chat.send requests using cached media files.
  • Iterate over cached media, read files from disk, and base64-encode their contents.
  • Infer image MIME type from media URL when possible, defaulting to image/png.
  • Construct an attachments array containing type, mimeType, fileName, and base64 content for each media item.
  • Log a warning when reading cached images fails, without breaking the rest of the send flow.
  • Include attachments in the chat.send payload only when there is at least one attachment.
src/index.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • In the attachments-building loop you log [读取缓存图片失败] for any media type, but the loop may include non-image files as well; consider making the log message media-type agnostic or including m.type for clarity.
  • When constructing attachments you only compute mimeType for image types and leave it undefined for others; if the gateway expects or benefits from MIME types for files/voice/video as well, it may be safer to infer or pass appropriate MIME types for those too.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the attachments-building loop you log `[读取缓存图片失败]` for any media type, but the loop may include non-image files as well; consider making the log message media-type agnostic or including `m.type` for clarity.
- When constructing attachments you only compute `mimeType` for `image` types and leave it `undefined` for others; if the gateway expects or benefits from MIME types for files/voice/video as well, it may be safer to infer or pass appropriate MIME types for those too.

## Individual Comments

### Comment 1
<location path="src/index.ts" line_range="876-878" />
<code_context>
+            fileName: m.name,
+            content: b64,
+          });
+        } catch (e: any) {
+          logger?.warn(`[OpenClaw] 读取缓存图片失败: ${e.message}`);
+        }
+      }
</code_context>
<issue_to_address>
**suggestion:** Log message references images but the block handles all media types.

Since this catch applies to any `m.type`, but the message mentions only images ("读取缓存图片失败"), consider making the log text media-agnostic or including `m.type` so logs accurately reflect what failed.

```suggestion
        } catch (e: any) {
          logger?.warn(`[OpenClaw] 读取缓存媒体失败 (type: ${m.type}): ${e.message}`);
        }
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/index.ts
Comment on lines +876 to +878
} catch (e: any) {
logger?.warn(`[OpenClaw] 读取缓存图片失败: ${e.message}`);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Log message references images but the block handles all media types.

Since this catch applies to any m.type, but the message mentions only images ("读取缓存图片失败"), consider making the log text media-agnostic or including m.type so logs accurately reflect what failed.

Suggested change
} catch (e: any) {
logger?.warn(`[OpenClaw] 读取缓存图片失败: ${e.message}`);
}
} catch (e: any) {
logger?.warn(`[OpenClaw] 读取缓存媒体失败 (type: ${m.type}): ${e.message}`);
}

@XGH2333
Copy link
Copy Markdown
Author

XGH2333 commented Apr 23, 2026

image

测试模型可以正常理解图片(已关闭openclaw的image tool)

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