mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-17 03:21:38 +00:00
* fix(image-gen): bound image generation provider JSON response reads Route success JSON reads through readProviderJsonResponse (16 MiB cap) in openrouter, google, fal, minimax, openai, and vydra image generation providers to prevent OOM from oversized or hostile endpoint responses. Mirrors the response-limit campaign already applied to other provider paths. AI-assisted. Co-authored-by: Cursor <cursoragent@cursor.com> * fix(image-gen): size bounded JSON caps for inline image payloads Signed-off-by: sallyom <somalley@redhat.com> --------- Signed-off-by: sallyom <somalley@redhat.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: sallyom <somalley@redhat.com>
125 lines
3.8 KiB
TypeScript
125 lines
3.8 KiB
TypeScript
// Vydra provider module implements model/runtime integration.
|
|
import type { ImageGenerationProvider } from "openclaw/plugin-sdk/image-generation";
|
|
import { isProviderApiKeyConfigured } from "openclaw/plugin-sdk/provider-auth";
|
|
import {
|
|
assertOkOrThrowHttpError,
|
|
postJsonRequest,
|
|
readProviderJsonResponse,
|
|
} from "openclaw/plugin-sdk/provider-http";
|
|
import {
|
|
DEFAULT_VYDRA_IMAGE_MODEL,
|
|
downloadVydraAsset,
|
|
extractVydraResultUrls,
|
|
resolveCompletedVydraPayload,
|
|
resolveVydraGeneratedMediaMaxBytes,
|
|
resolveVydraResponseJobId,
|
|
resolveVydraResponseStatus,
|
|
resolveVydraRequestContext,
|
|
} from "./shared.js";
|
|
|
|
export function buildVydraImageGenerationProvider(): ImageGenerationProvider {
|
|
return {
|
|
id: "vydra",
|
|
label: "Vydra",
|
|
defaultModel: DEFAULT_VYDRA_IMAGE_MODEL,
|
|
models: [DEFAULT_VYDRA_IMAGE_MODEL],
|
|
isConfigured: ({ agentDir }) =>
|
|
isProviderApiKeyConfigured({
|
|
provider: "vydra",
|
|
agentDir,
|
|
}),
|
|
capabilities: {
|
|
generate: {
|
|
maxCount: 1,
|
|
supportsSize: false,
|
|
supportsAspectRatio: false,
|
|
supportsResolution: false,
|
|
},
|
|
edit: {
|
|
enabled: false,
|
|
maxCount: 1,
|
|
maxInputImages: 0,
|
|
supportsSize: false,
|
|
supportsAspectRatio: false,
|
|
supportsResolution: false,
|
|
},
|
|
},
|
|
async generateImage(req) {
|
|
if ((req.inputImages?.length ?? 0) > 0) {
|
|
throw new Error(
|
|
"Vydra image generation currently supports text-to-image only in the bundled plugin.",
|
|
);
|
|
}
|
|
if ((req.count ?? 1) > 1) {
|
|
throw new Error("Vydra image generation supports at most one image per request.");
|
|
}
|
|
|
|
const { fetchFn, baseUrl, allowPrivateNetwork, headers, dispatcherPolicy } =
|
|
await resolveVydraRequestContext({
|
|
cfg: req.cfg,
|
|
agentDir: req.agentDir,
|
|
authStore: req.authStore,
|
|
capability: "image",
|
|
});
|
|
|
|
const model = req.model?.trim() || DEFAULT_VYDRA_IMAGE_MODEL;
|
|
const { response, release } = await postJsonRequest({
|
|
url: `${baseUrl}/models/${model}`,
|
|
headers,
|
|
body: {
|
|
prompt: req.prompt,
|
|
model: "text-to-image",
|
|
},
|
|
timeoutMs: req.timeoutMs,
|
|
fetchFn,
|
|
allowPrivateNetwork,
|
|
ssrfPolicy: req.ssrfPolicy,
|
|
dispatcherPolicy,
|
|
});
|
|
|
|
try {
|
|
await assertOkOrThrowHttpError(response, "Vydra image generation failed");
|
|
const submitted = await readProviderJsonResponse(response, "vydra.image-generation");
|
|
const completedPayload = await resolveCompletedVydraPayload({
|
|
submitted,
|
|
baseUrl,
|
|
headers,
|
|
timeoutMs: req.timeoutMs,
|
|
fetchFn,
|
|
kind: "image",
|
|
missingJobIdMessage: "Vydra image generation response missing job id",
|
|
});
|
|
const imageUrl = extractVydraResultUrls(completedPayload, "image")[0];
|
|
if (!imageUrl) {
|
|
throw new Error("Vydra image generation completed without an image URL");
|
|
}
|
|
const image = await downloadVydraAsset({
|
|
url: imageUrl,
|
|
kind: "image",
|
|
timeoutMs: req.timeoutMs,
|
|
fetchFn,
|
|
maxBytes: resolveVydraGeneratedMediaMaxBytes({ cfg: req.cfg, kind: "image" }),
|
|
});
|
|
return {
|
|
images: [
|
|
{
|
|
buffer: image.buffer,
|
|
mimeType: image.mimeType,
|
|
fileName: image.fileName,
|
|
},
|
|
],
|
|
model,
|
|
metadata: {
|
|
jobId:
|
|
resolveVydraResponseJobId(completedPayload) ?? resolveVydraResponseJobId(submitted),
|
|
imageUrl,
|
|
status: resolveVydraResponseStatus(completedPayload) ?? "completed",
|
|
},
|
|
};
|
|
} finally {
|
|
await release();
|
|
}
|
|
},
|
|
};
|
|
}
|