UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

283 lines (282 loc) • 10.4 kB
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "../../string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "../../record-coerce-CVPuDqcc.mjs"; import { m as normalizeTrimmedStringList } from "../../string-normalization-DLmfYZXI.mjs"; import { p as sanitizeConfiguredModelProviderRequest } from "../../provider-request-config-CCij-nof.mjs"; import "../../string-coerce-runtime-BwAZZDf5.mjs"; import { _ as resolveProviderOperationTimeoutMs, a as createProviderOperationTimeoutResolver, r as createProviderOperationDeadline, u as pollProviderOperationJson } from "../../shared-CfcMLHIP.mjs"; import { t as definePluginEntry } from "../../plugin-entry-CiJdaHtx.mjs"; import { i as resolveGeneratedMediaMaxBytes } from "../../configured-max-bytes-lKIaFMol.mjs"; import { n as isProviderApiKeyConfiguredAsync } from "../../provider-auth-availability-Bv9WFzc3.mjs"; import { t as downloadGeneratedVideoAsset } from "../../media-generation-runtime-g6IBf-_Q.mjs"; import { t as normalizeVideoGenerationDuration } from "../../duration-support-DVBDr7yA.mjs"; import { t as postJsonResponse } from "../../provider-json-post-BR01v-xE.mjs"; import { t as resolveProviderApiKeyHttpRequestConfig } from "../../provider-http-LcawLneq.mjs"; import "../../video-generation-DxmLfLWw.mjs"; import "../../provider-auth-D_oAZ3fr.mjs"; import { n as createProviderApiKeyAuthMethod } from "../../provider-api-key-auth-B0AQ5Vpx.mjs"; import "../../provider-entry-CRfSFsHE.mjs"; //#region extensions/runway/index.ts const DEFAULT_RUNWAY_BASE_URL = "https://api.dev.runwayml.com"; const DEFAULT_RUNWAY_MODEL = "gen4.5"; const RUNWAY_API_VERSION = "2024-11-06"; const DEFAULT_TIMEOUT_MS = 12e4; const POLL_INTERVAL_MS = 5e3; const MAX_POLL_ATTEMPTS = 120; const MAX_DURATION_SECONDS = 10; const TEXT_ONLY_MODELS = /* @__PURE__ */ new Set([ "gen4.5", "veo3.1", "veo3.1_fast", "veo3" ]); const IMAGE_MODELS = /* @__PURE__ */ new Set([ "gen4.5", "gen4_turbo", "gen3a_turbo", "veo3.1", "veo3.1_fast", "veo3" ]); const VIDEO_MODELS = /* @__PURE__ */ new Set(["gen4_aleph"]); const RUNWAY_TEXT_ASPECT_RATIOS = ["16:9", "9:16"]; const RUNWAY_EDIT_ASPECT_RATIOS = [ "1:1", "16:9", "9:16", "3:4", "4:3", "21:9" ]; function readRunwayTaskStatus(e) { const t = normalizeOptionalString(e.status); switch (t) { case "PENDING": case "RUNNING": case "THROTTLED": case "SUCCEEDED": case "FAILED": case "CANCELLED": return t; case void 0: throw new Error("Runway video status response missing task status"); default: throw new Error(`Runway video status response returned unknown task status: ${t}`); } } function readRunwayFailureMessage(e) { if (typeof e === "string") return normalizeOptionalString(e); if (isRecord(e)) return normalizeOptionalString(e.message); } function resolveSourceUri(e, t) { if (!e) return; const n = normalizeOptionalString(e.url); if (n) return n; if (!e.buffer) return; return `data:${normalizeOptionalString(e.mimeType) ?? t};base64,${e.buffer.toString("base64")}`; } const RUNWAY_RATIO_SIZES = /* @__PURE__ */ new Map([ ["9:16", "720:1280"], ["16:9", "1280:720"], ["1:1", "960:960"], ["3:4", "832:1104"], ["4:3", "1104:832"], ["21:9", "1584:672"] ]); function buildCreateBody(e, t) { const n = normalizeVideoGenerationDuration({ durationSeconds: e.durationSeconds }); const r = n === void 0 || !Number.isSafeInteger(e.durationSeconds) ? 5 : Math.max(2, Math.min(MAX_DURATION_SECONDS, n)); const i = (e.inputImages?.length ?? 0) > 0 || (e.inputVideos?.length ?? 0) > 0; const a = normalizeOptionalString(e.size) || RUNWAY_RATIO_SIZES.get(normalizeOptionalString(e.aspectRatio) ?? "") || "1280:720"; if (!i && a !== "1280:720" && a !== "720:1280") throw new Error("Runway text-to-video currently supports only 16:9 or 9:16 output ratios."); const o = normalizeOptionalString(e.model) ?? DEFAULT_RUNWAY_MODEL; if (t !== "/v1/video_to_video") { const n = t === "/v1/image_to_video"; const i = n ? IMAGE_MODELS : TEXT_ONLY_MODELS; const s = n ? "image-to-video" : "text-to-video"; if (!i.has(o)) throw new Error(`Runway ${s} does not support model ${o}. Use one of: ${[...i].join(", ")}.`); const c = n ? resolveSourceUri(e.inputImages?.[0], "image/png") : void 0; if (n && !c) throw new Error("Runway image-to-video input is missing image data."); return { model: o, promptText: e.prompt, ...n ? { promptImage: c } : {}, ratio: a, duration: r }; } if (!VIDEO_MODELS.has(o)) throw new Error("Runway video-to-video currently requires model gen4_aleph."); const s = resolveSourceUri(e.inputVideos?.[0], "video/mp4"); if (!s) throw new Error("Runway video-to-video input is missing video data."); return { model: o, promptText: e.prompt, videoUri: s, ratio: a }; } var runway_default = definePluginEntry({ id: "runway", name: "Runway Provider", description: "Bundled Runway video provider plugin", register(e) { e.registerProvider({ id: "runway", label: "Runway", docsPath: "/providers/runway", envVars: ["RUNWAYML_API_SECRET", "RUNWAY_API_KEY"], auth: [createProviderApiKeyAuthMethod({ providerId: "runway", methodId: "api-key", label: "Runway API key", optionKey: "runwayApiKey", flagName: "--runway-api-key", envVar: "RUNWAYML_API_SECRET", promptMessage: "Enter Runway API key", wizard: { choiceId: "runway-api-key", choiceLabel: "Runway API key", groupId: "runway", groupLabel: "Runway", groupHint: "API key", onboardingScopes: ["image-generation"] } })] }); e.registerVideoGenerationProvider({ id: "runway", label: "Runway", defaultModel: DEFAULT_RUNWAY_MODEL, models: [ "gen4.5", "gen4_turbo", "gen4_aleph", "gen3a_turbo", "veo3.1", "veo3.1_fast", "veo3" ], isConfiguredAsync: (e) => isProviderApiKeyConfiguredAsync({ provider: "runway", ...e }), capabilities: { generate: { maxVideos: 1, maxDurationSeconds: MAX_DURATION_SECONDS, aspectRatios: RUNWAY_TEXT_ASPECT_RATIOS, supportsAspectRatio: true }, imageToVideo: { enabled: true, maxVideos: 1, maxInputImages: 1, maxDurationSeconds: MAX_DURATION_SECONDS, aspectRatios: RUNWAY_EDIT_ASPECT_RATIOS, supportsAspectRatio: true }, videoToVideo: { enabled: true, maxVideos: 1, maxInputVideos: 1, aspectRatios: RUNWAY_EDIT_ASPECT_RATIOS, supportsAspectRatio: true } }, async generateVideo(e) { const t = fetch; const n = e.inputImages?.length ?? 0; const r = e.inputVideos?.length ?? 0; if (n > 0 && r > 0) throw new Error("Runway video generation does not support image and video inputs together."); if (n > 1 || r > 1) throw new Error("Runway video generation supports at most one input image or one input video."); const i = r > 0 ? "/v1/video_to_video" : n > 0 ? "/v1/image_to_video" : "/v1/text_to_video"; const a = buildCreateBody(e, i); const { baseUrl: o, allowPrivateNetwork: s, headers: c, dispatcherPolicy: l } = await resolveProviderApiKeyHttpRequestConfig({ req: e, missingApiKeyMessage: "Runway API key missing", baseUrl: e.cfg?.models?.providers?.runway?.baseUrl, defaultBaseUrl: DEFAULT_RUNWAY_BASE_URL, request: sanitizeConfiguredModelProviderRequest(e.cfg?.models?.providers?.runway?.request), defaultHeaders: { "X-Runway-Version": RUNWAY_API_VERSION }, provider: "runway", capability: "video" }); const u = createProviderOperationDeadline({ timeoutMs: e.timeoutMs, label: "Runway video generation" }); const d = await postJsonResponse({ url: `${o}${i}`, headers: c, body: a, timeoutMs: resolveProviderOperationTimeoutMs({ deadline: u, defaultTimeoutMs: DEFAULT_TIMEOUT_MS }), fetchFn: t, allowPrivateNetwork: s, dispatcherPolicy: l, errorLabel: "Runway video generation failed", objectResponse: true }); const f = normalizeOptionalString(d.id); if (!f) throw new Error("Runway video generation response missing task id"); const p = await pollProviderOperationJson({ url: `${o}/v1/tasks/${f}`, headers: c, deadline: u.deadlineAtMs === void 0 ? createProviderOperationDeadline({ timeoutMs: DEFAULT_TIMEOUT_MS, label: `Runway video generation task ${f}` }) : { ...u, label: `Runway video generation task ${f}` }, defaultTimeoutMs: DEFAULT_TIMEOUT_MS, fetchFn: t, ssrfPolicy: { allowPrivateNetwork: s }, dispatcherPolicy: l, maxAttempts: MAX_POLL_ATTEMPTS, pollIntervalMs: POLL_INTERVAL_MS, requestFailedMessage: "Runway video status request failed", timeoutMessage: `Runway video generation task ${f} did not finish in time`, isComplete: (e) => readRunwayTaskStatus(e) === "SUCCEEDED", getFailureMessage: (e) => { const t = readRunwayTaskStatus(e); return t === "FAILED" || t === "CANCELLED" ? readRunwayFailureMessage(e.failure) || `Runway video generation ${normalizeLowercaseStringOrEmpty(t)}` : void 0; } }); if (!Array.isArray(p.output)) throw new Error("Runway video generation completed with malformed output URLs"); const m = normalizeTrimmedStringList(p.output); if (!m.length) throw new Error("Runway video generation completed without output URLs"); const h = createProviderOperationTimeoutResolver({ deadline: u, defaultTimeoutMs: DEFAULT_TIMEOUT_MS }); const g = resolveGeneratedMediaMaxBytes(e.cfg, "video"); const _ = []; for (const [e, n] of m.entries()) _.push(await downloadGeneratedVideoAsset({ url: n, timeoutMs: h, defaultTimeoutMs: DEFAULT_TIMEOUT_MS, fetchFn: t, provider: "runway", label: "Runway generated video download", requestFailedMessage: "Runway generated video download failed", index: e, maxBytes: g, validateBinaryResponse: true, metadata: { sourceUrl: n }, dispatcherPolicy: l })); return { videos: _, model: normalizeOptionalString(e.model) ?? DEFAULT_RUNWAY_MODEL, metadata: { taskId: f, status: normalizeOptionalString(p.status), endpoint: i, outputUrls: m } }; } }); } }); //#endregion export { runway_default as default };