refactor: dedupe provider ui trimmed readers

This commit is contained in:
Peter Steinberger
2026-04-08 01:13:25 +01:00
parent bf03babd2b
commit e0b4f3b995
18 changed files with 238 additions and 166 deletions

View File

@@ -6,6 +6,7 @@ import {
postJsonRequest,
resolveProviderHttpRequestConfig,
} from "openclaw/plugin-sdk/provider-http";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import type {
GeneratedVideoAsset,
VideoGenerationProvider,
@@ -41,7 +42,9 @@ type BytePlusTaskResponse = {
};
function resolveBytePlusVideoBaseUrl(req: VideoGenerationRequest): string {
return req.cfg?.models?.providers?.byteplus?.baseUrl?.trim() || BYTEPLUS_BASE_URL;
return (
normalizeOptionalString(req.cfg?.models?.providers?.byteplus?.baseUrl) ?? BYTEPLUS_BASE_URL
);
}
function toDataUrl(buffer: Buffer, mimeType: string): string {
@@ -53,13 +56,14 @@ function resolveBytePlusImageUrl(req: VideoGenerationRequest): string | undefine
if (!input) {
return undefined;
}
if (input.url?.trim()) {
return input.url.trim();
const inputUrl = normalizeOptionalString(input.url);
if (inputUrl) {
return inputUrl;
}
if (!input.buffer) {
throw new Error("BytePlus reference image is missing image data.");
}
return toDataUrl(input.buffer, input.mimeType?.trim() || "image/png");
return toDataUrl(input.buffer, normalizeOptionalString(input.mimeType) ?? "image/png");
}
async function pollBytePlusTask(params: {
@@ -81,12 +85,14 @@ async function pollBytePlusTask(params: {
);
await assertOkOrThrowHttpError(response, "BytePlus video status request failed");
const payload = (await response.json()) as BytePlusTaskResponse;
switch (payload.status?.trim()) {
switch (normalizeOptionalString(payload.status)) {
case "succeeded":
return payload;
case "failed":
case "cancelled":
throw new Error(payload.error?.message?.trim() || "BytePlus video generation failed");
throw new Error(
normalizeOptionalString(payload.error?.message) || "BytePlus video generation failed",
);
case "queued":
case "running":
default:
@@ -109,7 +115,7 @@ async function downloadBytePlusVideo(params: {
params.fetchFn,
);
await assertOkOrThrowHttpError(response, "BytePlus generated video download failed");
const mimeType = response.headers.get("content-type")?.trim() || "video/mp4";
const mimeType = normalizeOptionalString(response.headers.get("content-type")) ?? "video/mp4";
const arrayBuffer = await response.arrayBuffer();
return {
buffer: Buffer.from(arrayBuffer),
@@ -195,11 +201,12 @@ export function buildBytePlusVideoGenerationProvider(): VideoGenerationProvider
});
}
const body: Record<string, unknown> = {
model: req.model?.trim() || DEFAULT_BYTEPLUS_VIDEO_MODEL,
model: normalizeOptionalString(req.model) || DEFAULT_BYTEPLUS_VIDEO_MODEL,
content,
};
if (req.aspectRatio?.trim()) {
body.ratio = req.aspectRatio.trim();
const aspectRatio = normalizeOptionalString(req.aspectRatio);
if (aspectRatio) {
body.ratio = aspectRatio;
}
if (req.resolution) {
body.resolution = req.resolution;
@@ -226,7 +233,7 @@ export function buildBytePlusVideoGenerationProvider(): VideoGenerationProvider
try {
await assertOkOrThrowHttpError(response, "BytePlus video generation failed");
const submitted = (await response.json()) as BytePlusTaskCreateResponse;
const taskId = submitted.id?.trim();
const taskId = normalizeOptionalString(submitted.id);
if (!taskId) {
throw new Error("BytePlus video generation response missing task id");
}
@@ -237,7 +244,7 @@ export function buildBytePlusVideoGenerationProvider(): VideoGenerationProvider
baseUrl,
fetchFn,
});
const videoUrl = completed.content?.video_url?.trim();
const videoUrl = normalizeOptionalString(completed.content?.video_url);
if (!videoUrl) {
throw new Error("BytePlus video generation completed without a video URL");
}