Skip to content

Commit 0c9afcb

Browse files
committed
perf(media): remove remaining upload adapter copies
1 parent 93e628f commit 0c9afcb

7 files changed

Lines changed: 23 additions & 21 deletions

File tree

‎extensions/line/src/rich-menu.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Line plugin module implements rich menu behavior.
22
import { messagingApi } from "@line/bot-sdk";
3+
import { bufferToBlobPart } from "openclaw/plugin-sdk/blob-runtime";
34
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
45
import { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-local-roots";
56
import { mimeTypeFromFilePath } from "openclaw/plugin-sdk/media-mime";
@@ -124,9 +125,8 @@ export async function uploadRichMenuImage(
124125
? "image/png"
125126
: "image/jpeg";
126127

127-
const imageBytes = new ArrayBuffer(media.buffer.byteLength);
128-
new Uint8Array(imageBytes).set(media.buffer);
129-
await blobClient.setRichMenuImage(richMenuId, new Blob([imageBytes], { type: contentType }));
128+
const blob = new Blob([bufferToBlobPart(media.buffer)], { type: contentType });
129+
await blobClient.setRichMenuImage(richMenuId, blob);
130130

131131
if (opts.verbose) {
132132
logVerbose(`line: uploaded image to rich menu ${richMenuId}`);

‎extensions/mattermost/src/channel.send-loopback.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("Mattermost send action loopback", () => {
198198
setMattermostRuntime(createPluginRuntimeMock());
199199
loadOutboundMediaFromUrl.mockReset();
200200
loadOutboundMediaFromUrl.mockResolvedValueOnce({
201-
buffer: Buffer.from("unnamed-image"),
201+
buffer: Buffer.from("!unnamed-image?").subarray(1, -1),
202202
contentType: "image/png",
203203
kind: "image",
204204
});
@@ -233,5 +233,6 @@ describe("Mattermost send action loopback", () => {
233233
expect(uploads).toHaveLength(1);
234234
expect(uploads[0]).toContain('filename="upload.png"');
235235
expect(uploads[0]).toContain("Content-Type: image/png");
236+
expect(uploads[0]).toContain("\r\n\r\nunnamed-image\r\n");
236237
});
237238
});

‎extensions/mattermost/src/mattermost/client.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Mattermost plugin module implements client behavior.
2+
import { bufferToBlobPart } from "openclaw/plugin-sdk/blob-runtime";
23
import { createChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound";
34
import { collectErrorGraphCandidates } from "openclaw/plugin-sdk/error-runtime";
45
import { buildTimeoutAbortSignal } from "openclaw/plugin-sdk/extension-shared";
@@ -694,10 +695,7 @@ export async function uploadMattermostFile(
694695
): Promise<MattermostFileInfo> {
695696
const form = new FormData();
696697
const fileName = normalizeOptionalString(params.fileName) ?? "upload";
697-
const bytes = Uint8Array.from(params.buffer);
698-
const blob = params.contentType
699-
? new Blob([bytes], { type: params.contentType })
700-
: new Blob([bytes]);
698+
const blob = new Blob([bufferToBlobPart(params.buffer)], { type: params.contentType });
701699
form.append("files", blob, fileName);
702700
form.append("channel_id", params.channelId);
703701

‎extensions/openai/video-generation-provider.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,15 @@ describe("openai video generation provider", () => {
635635
});
636636

637637
const provider = buildOpenAIVideoGenerationProvider();
638+
const input = Buffer.from("!png-bytes?").subarray(1, -1);
638639
await provider.generateVideo({
639640
provider: "openai",
640641
model: "sora-2",
641642
prompt: "Animate this frame",
642643
cfg: {},
643-
inputImages: [{ buffer: Buffer.from("png-bytes"), mimeType: "image/png" }],
644+
inputImages: [{ buffer: input, mimeType: "image/png" }],
644645
});
646+
input.fill(0);
645647

646648
const createRequest = postMultipartRequest();
647649
expect(createRequest.url).toBe("https://api.openai.com/v1/videos");
@@ -924,13 +926,15 @@ describe("openai video generation provider", () => {
924926
});
925927

926928
const provider = buildOpenAIVideoGenerationProvider();
929+
const input = Buffer.from("!mp4-bytes?").subarray(1, -1);
927930
await provider.generateVideo({
928931
provider: "openai",
929932
model: "sora-2",
930933
prompt: "Remix this clip",
931934
cfg: {},
932-
inputVideos: [{ buffer: Buffer.from("mp4-bytes"), mimeType: "video/mp4" }],
935+
inputVideos: [{ buffer: input, mimeType: "video/mp4" }],
933936
});
937+
input.fill(0);
934938

935939
expect(postJsonRequestMock).not.toHaveBeenCalled();
936940
const createRequest = postMultipartRequest();
@@ -940,6 +944,9 @@ describe("openai video generation provider", () => {
940944
expect(form.get("prompt")).toBe("Remix this clip");
941945
expect(form.get("model")).toBeNull();
942946
expect(form.get("video")).toBeInstanceOf(File);
947+
expect(Buffer.from(await (form.get("video") as File).arrayBuffer())).toEqual(
948+
Buffer.from("mp4-bytes"),
949+
);
943950
expect(form.get("input_reference")).toBeNull();
944951
expect(createRequest.timeoutMs).toBe(120000);
945952
expect(createRequest.fetchFn).toBe(fetch);

‎extensions/openai/video-generation-provider.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Openai provider module implements model/runtime integration.
2+
import { bufferToBlobPart } from "openclaw/plugin-sdk/blob-runtime";
23
import {
34
downloadGeneratedVideoAsset,
45
resolveGeneratedMediaMaxBytes,
@@ -68,12 +69,6 @@ function readOpenAIVideoFailureMessage(payload: OpenAIVideoResponse): string | u
6869
: undefined;
6970
}
7071

71-
function toBlobBytes(buffer: Buffer): ArrayBuffer {
72-
const arrayBuffer = new ArrayBuffer(buffer.byteLength);
73-
new Uint8Array(arrayBuffer).set(buffer);
74-
return arrayBuffer;
75-
}
76-
7772
function resolveDurationSeconds(durationSeconds: number | undefined): "4" | "8" | "12" | undefined {
7873
if (typeof durationSeconds !== "number" || !Number.isFinite(durationSeconds)) {
7974
return undefined;
@@ -139,7 +134,7 @@ function resolveReferenceAsset(req: VideoGenerationRequest): OpenAIReferenceAsse
139134
`${kind === "video" ? "reference-video" : "reference-image"}.${extension}`;
140135
return {
141136
kind,
142-
file: new File([toBlobBytes(asset.buffer)], fileName, { type: mimeType }),
137+
file: new File([bufferToBlobPart(asset.buffer)], fileName, { type: mimeType }),
143138
};
144139
}
145140

‎extensions/pixverse/video-generation-provider.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Pixverse provider module implements model/runtime integration.
22
import { randomUUID } from "node:crypto";
3+
import { bufferToBlobPart } from "openclaw/plugin-sdk/blob-runtime";
34
import { extensionForMime } from "openclaw/plugin-sdk/media-mime";
45
import { isProviderApiKeyConfigured } from "openclaw/plugin-sdk/provider-auth";
56
import { resolveApiKeyForProvider } from "openclaw/plugin-sdk/provider-auth-runtime";
@@ -231,9 +232,8 @@ function buildUploadImageForm(asset: VideoGenerationSourceAsset): FormData {
231232
const mimeType = normalizeOptionalString(asset.mimeType) ?? "image/png";
232233
const extension = extensionForMime(mimeType)?.slice(1) ?? "png";
233234
const fileName = normalizeOptionalString(asset.fileName) ?? `image.${extension}`;
234-
const bytes = new Uint8Array(asset.buffer.byteLength);
235-
bytes.set(asset.buffer);
236-
form.set("image", new File([bytes], fileName, { type: mimeType }));
235+
const file = new File([bufferToBlobPart(asset.buffer)], fileName, { type: mimeType });
236+
form.set("image", file);
237237
return form;
238238
}
239239

‎extensions/tlon/src/urbit/upload.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* Upload an image from a URL to Tlon storage.
33
*/
4+
import { bufferToBlobPart } from "openclaw/plugin-sdk/blob-runtime";
45
import { MAX_IMAGE_BYTES, readRemoteMediaBuffer } from "openclaw/plugin-sdk/media-runtime";
56
import { TLON_MEDIA_FETCH_TIMEOUTS } from "../media-fetch-timeouts.js";
67
import { uploadFile } from "../tlon-api.js";
@@ -30,7 +31,7 @@ export async function uploadImageFromUrl(imageUrl: string, maxBytes?: number): P
3031
sourceSizeVerified = true;
3132

3233
const contentType = fetched.contentType || "image/png";
33-
const blob = new Blob([new Uint8Array(fetched.buffer)], { type: contentType });
34+
const blob = new Blob([bufferToBlobPart(fetched.buffer)], { type: contentType });
3435

3536
// Extract filename from URL or use a default
3637
const urlPath = new URL(imageUrl).pathname;

0 commit comments

Comments
 (0)