Skip to content

Commit 53d007b

Browse files
steipetesamzong
andcommitted
refactor(media): centralize bounded remote downloads
Co-authored-by: samzong <[email protected]>
1 parent 2181564 commit 53d007b

81 files changed

Lines changed: 2317 additions & 814 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Docs: https://docs.openclaw.ai
66

77
### Fixes
88

9+
- Plugins/Feishu/WhatsApp/Line: enforce inbound media size caps while reading download streams, avoiding full buffering of oversized attachments. (#81044, #81050) Thanks @samzong.
910
- Config: serialize and retry semantic config mutations centrally, so concurrent commands can rebase safe changes instead of clobbering or hand-rolling command-local retry loops. (#76601)
1011
- Require approval for setup-code device pairing [AI]. (#81292) Thanks @pgondhi987.
1112
- Docker: pin setup-time container paths so stale host `.env` OpenClaw paths cannot leak into Linux containers. Fixes #80381. (#81105) Thanks @brokemac79.

‎docs/plugins/sdk-runtime.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,19 @@ two-party event loops that do not go through the shared channel-turn kernel.
542542
<Accordion title="api.runtime.channel">
543543
Channel-specific runtime helpers (available when a channel plugin is loaded).
544544

545+
`api.runtime.channel.media` is the preferred surface for channel media downloads and storage:
546+
547+
```typescript
548+
const saved = await api.runtime.channel.media.saveRemoteMedia({
549+
url,
550+
subdir: "inbound",
551+
maxBytes,
552+
filePathHint: fileName,
553+
});
554+
```
555+
556+
Use `saveRemoteMedia(...)` when a remote URL should become OpenClaw media. Use `saveResponseMedia(...)` when the plugin already fetched a `Response` with plugin-owned auth, redirect, or allowlist handling. Use `readRemoteMediaBuffer(...)` only when the plugin needs raw bytes for inspection, transforms, decryption, or reupload. `fetchRemoteMedia(...)` remains a deprecated compatibility alias for `readRemoteMediaBuffer(...)`.
557+
545558
`api.runtime.channel.mentions` is the shared inbound mention-policy surface for bundled channel plugins that use runtime injection:
546559

547560
```typescript

‎docs/plugins/sdk-subpaths.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ focused channel/runtime subpaths, `config-contracts`, `string-coerce-runtime`,
302302
<Accordion title="Capability and testing subpaths">
303303
| Subpath | Key exports |
304304
| --- | --- |
305-
| `plugin-sdk/media-runtime` | Shared media fetch/transform/store helpers, ffprobe-backed video dimension probing, and media payload builders |
305+
| `plugin-sdk/media-runtime` | Shared media fetch/transform/store helpers including `saveRemoteMedia`, `saveResponseMedia`, `readRemoteMediaBuffer`, and deprecated `fetchRemoteMedia`; prefer store helpers before buffer reads when a URL should become OpenClaw media |
306306
| `plugin-sdk/media-mime` | Narrow MIME normalization, file-extension mapping, MIME detection, and media-kind helpers |
307-
| `plugin-sdk/media-store` | Narrow media store helpers such as `saveMediaBuffer` |
307+
| `plugin-sdk/media-store` | Narrow media store helpers such as `saveMediaBuffer` and `saveMediaStream` |
308308
| `plugin-sdk/media-generation-runtime` | Shared media-generation failover helpers, candidate selection, and missing-model messaging |
309309
| `plugin-sdk/media-understanding` | Media understanding provider types plus provider-facing image/audio/structured-extraction helper exports |
310310
| `plugin-sdk/text-chunking` | Text and markdown chunking/render helpers, markdown table conversion, directive-tag stripping, and safe-text utilities |

‎extensions/discord/src/monitor/message-media.ts‎

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { StickerFormatType, type APIAttachment, type APIStickerItem } from "discord-api-types/v10";
22
import { getFileExtension } from "openclaw/plugin-sdk/media-mime";
3-
import {
4-
fetchRemoteMedia,
5-
saveMediaBuffer,
6-
type FetchLike,
7-
} from "openclaw/plugin-sdk/media-runtime";
3+
import { saveRemoteMedia, type FetchLike } from "openclaw/plugin-sdk/media-runtime";
84
import { buildMediaPayload } from "openclaw/plugin-sdk/reply-payload";
95
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
106
import type { SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
@@ -215,19 +211,23 @@ async function fetchDiscordMedia(params: {
215211
readIdleTimeoutMs?: number;
216212
totalTimeoutMs?: number;
217213
abortSignal?: AbortSignal;
214+
fallbackContentType?: string;
215+
originalFilename?: string;
218216
}) {
219217
const timeoutAbortController = params.totalTimeoutMs ? new AbortController() : undefined;
220218
const signal = mergeAbortSignals([params.abortSignal, timeoutAbortController?.signal]);
221219
let timedOut = false;
222220
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
223221

224-
const fetchPromise = fetchRemoteMedia({
222+
const savePromise = saveRemoteMedia({
225223
url: params.url,
226224
filePathHint: params.filePathHint,
227225
maxBytes: params.maxBytes,
228226
fetchImpl: params.fetchImpl,
229227
ssrfPolicy: params.ssrfPolicy,
230228
readIdleTimeoutMs: params.readIdleTimeoutMs,
229+
fallbackContentType: params.fallbackContentType,
230+
originalFilename: params.originalFilename,
231231
...(signal ? { requestInit: { signal } } : {}),
232232
}).catch((error) => {
233233
if (timedOut) {
@@ -238,7 +238,7 @@ async function fetchDiscordMedia(params: {
238238

239239
try {
240240
if (!params.totalTimeoutMs) {
241-
return await fetchPromise;
241+
return await savePromise;
242242
}
243243
const timeoutPromise = new Promise<never>((_, reject) => {
244244
timeoutHandle = setTimeout(() => {
@@ -248,7 +248,7 @@ async function fetchDiscordMedia(params: {
248248
}, params.totalTimeoutMs);
249249
timeoutHandle.unref?.();
250250
});
251-
return await Promise.race([fetchPromise, timeoutPromise]);
251+
return await Promise.race([savePromise, timeoutPromise]);
252252
} finally {
253253
if (timeoutHandle) {
254254
clearTimeout(timeoutHandle);
@@ -280,7 +280,7 @@ async function appendResolvedMediaFromAttachments(params: {
280280
continue;
281281
}
282282
try {
283-
const fetched = await fetchDiscordMedia({
283+
const saved = await fetchDiscordMedia({
284284
url: attachmentUrl,
285285
filePathHint: attachment.filename ?? attachmentUrl,
286286
maxBytes: params.maxBytes,
@@ -289,14 +289,9 @@ async function appendResolvedMediaFromAttachments(params: {
289289
readIdleTimeoutMs: params.readIdleTimeoutMs,
290290
totalTimeoutMs: params.totalTimeoutMs,
291291
abortSignal: params.abortSignal,
292+
fallbackContentType: attachment.content_type,
293+
originalFilename: attachment.filename,
292294
});
293-
const saved = await saveMediaBuffer(
294-
fetched.buffer,
295-
fetched.contentType ?? attachment.content_type,
296-
"inbound",
297-
params.maxBytes,
298-
attachment.filename,
299-
);
300295
params.out.push({
301296
path: saved.path,
302297
contentType: saved.contentType,
@@ -388,7 +383,7 @@ async function appendResolvedMediaFromStickers(params: {
388383
let lastError: unknown;
389384
for (const candidate of candidates) {
390385
try {
391-
const fetched = await fetchDiscordMedia({
386+
const saved = await fetchDiscordMedia({
392387
url: candidate.url,
393388
filePathHint: candidate.fileName,
394389
maxBytes: params.maxBytes,
@@ -397,14 +392,9 @@ async function appendResolvedMediaFromStickers(params: {
397392
readIdleTimeoutMs: params.readIdleTimeoutMs,
398393
totalTimeoutMs: params.totalTimeoutMs,
399394
abortSignal: params.abortSignal,
395+
fallbackContentType: inferStickerContentType(sticker),
396+
originalFilename: candidate.fileName,
400397
});
401-
const saved = await saveMediaBuffer(
402-
fetched.buffer,
403-
fetched.contentType,
404-
"inbound",
405-
params.maxBytes,
406-
candidate.fileName,
407-
);
408398
params.out.push({
409399
path: saved.path,
410400
contentType: saved.contentType,

0 commit comments

Comments
 (0)