|
| 1 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 2 | +import { getProviderEnvVars } from "../../secrets/provider-env-vars.js"; |
| 3 | +import { listRuntimeVideoGenerationProviders } from "../../video-generation/runtime.js"; |
| 4 | +import { |
| 5 | + buildVideoGenerationTaskStatusDetails, |
| 6 | + buildVideoGenerationTaskStatusText, |
| 7 | + findActiveVideoGenerationTaskForSession, |
| 8 | +} from "../video-generation-task-status.js"; |
| 9 | + |
| 10 | +type VideoGenerateActionResult = { |
| 11 | + content: Array<{ type: "text"; text: string }>; |
| 12 | + details: Record<string, unknown>; |
| 13 | +}; |
| 14 | + |
| 15 | +function getVideoGenerationProviderAuthEnvVars(providerId: string): string[] { |
| 16 | + return getProviderEnvVars(providerId); |
| 17 | +} |
| 18 | + |
| 19 | +export function createVideoGenerateListActionResult( |
| 20 | + config?: OpenClawConfig, |
| 21 | +): VideoGenerateActionResult { |
| 22 | + const providers = listRuntimeVideoGenerationProviders({ config }); |
| 23 | + if (providers.length === 0) { |
| 24 | + return { |
| 25 | + content: [{ type: "text", text: "No video-generation providers are registered." }], |
| 26 | + details: { providers: [] }, |
| 27 | + }; |
| 28 | + } |
| 29 | + const lines = providers.map((provider) => { |
| 30 | + const authHints = getVideoGenerationProviderAuthEnvVars(provider.id); |
| 31 | + const capabilities = [ |
| 32 | + provider.capabilities.maxVideos ? `maxVideos=${provider.capabilities.maxVideos}` : null, |
| 33 | + provider.capabilities.maxInputImages |
| 34 | + ? `maxInputImages=${provider.capabilities.maxInputImages}` |
| 35 | + : null, |
| 36 | + provider.capabilities.maxInputVideos |
| 37 | + ? `maxInputVideos=${provider.capabilities.maxInputVideos}` |
| 38 | + : null, |
| 39 | + provider.capabilities.maxDurationSeconds |
| 40 | + ? `maxDurationSeconds=${provider.capabilities.maxDurationSeconds}` |
| 41 | + : null, |
| 42 | + provider.capabilities.supportedDurationSeconds?.length |
| 43 | + ? `supportedDurationSeconds=${provider.capabilities.supportedDurationSeconds.join("/")}` |
| 44 | + : null, |
| 45 | + provider.capabilities.supportedDurationSecondsByModel && |
| 46 | + Object.keys(provider.capabilities.supportedDurationSecondsByModel).length > 0 |
| 47 | + ? `supportedDurationSecondsByModel=${Object.entries( |
| 48 | + provider.capabilities.supportedDurationSecondsByModel, |
| 49 | + ) |
| 50 | + .map(([modelId, durations]) => `${modelId}:${durations.join("/")}`) |
| 51 | + .join("; ")}` |
| 52 | + : null, |
| 53 | + provider.capabilities.supportsResolution ? "resolution" : null, |
| 54 | + provider.capabilities.supportsAspectRatio ? "aspectRatio" : null, |
| 55 | + provider.capabilities.supportsSize ? "size" : null, |
| 56 | + provider.capabilities.supportsAudio ? "audio" : null, |
| 57 | + provider.capabilities.supportsWatermark ? "watermark" : null, |
| 58 | + ] |
| 59 | + .filter((entry): entry is string => Boolean(entry)) |
| 60 | + .join(", "); |
| 61 | + return [ |
| 62 | + `${provider.id}: default=${provider.defaultModel ?? "none"}`, |
| 63 | + provider.models?.length ? `models=${provider.models.join(", ")}` : null, |
| 64 | + capabilities ? `capabilities=${capabilities}` : null, |
| 65 | + authHints.length > 0 ? `auth=${authHints.join(" / ")}` : null, |
| 66 | + ] |
| 67 | + .filter((entry): entry is string => Boolean(entry)) |
| 68 | + .join(" | "); |
| 69 | + }); |
| 70 | + return { |
| 71 | + content: [{ type: "text", text: lines.join("\n") }], |
| 72 | + details: { |
| 73 | + providers: providers.map((provider) => ({ |
| 74 | + id: provider.id, |
| 75 | + defaultModel: provider.defaultModel, |
| 76 | + models: provider.models ?? [], |
| 77 | + authEnvVars: getVideoGenerationProviderAuthEnvVars(provider.id), |
| 78 | + capabilities: provider.capabilities, |
| 79 | + })), |
| 80 | + }, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export function createVideoGenerateStatusActionResult( |
| 85 | + sessionKey?: string, |
| 86 | +): VideoGenerateActionResult { |
| 87 | + const activeTask = findActiveVideoGenerationTaskForSession(sessionKey); |
| 88 | + if (!activeTask) { |
| 89 | + return { |
| 90 | + content: [ |
| 91 | + { |
| 92 | + type: "text", |
| 93 | + text: "No active video generation task is currently running for this session.", |
| 94 | + }, |
| 95 | + ], |
| 96 | + details: { |
| 97 | + action: "status", |
| 98 | + active: false, |
| 99 | + }, |
| 100 | + }; |
| 101 | + } |
| 102 | + return { |
| 103 | + content: [ |
| 104 | + { |
| 105 | + type: "text", |
| 106 | + text: buildVideoGenerationTaskStatusText(activeTask), |
| 107 | + }, |
| 108 | + ], |
| 109 | + details: { |
| 110 | + action: "status", |
| 111 | + ...buildVideoGenerationTaskStatusDetails(activeTask), |
| 112 | + }, |
| 113 | + }; |
| 114 | +} |
| 115 | + |
| 116 | +export function createVideoGenerateDuplicateGuardResult( |
| 117 | + sessionKey?: string, |
| 118 | +): VideoGenerateActionResult | null { |
| 119 | + const activeTask = findActiveVideoGenerationTaskForSession(sessionKey); |
| 120 | + if (!activeTask) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return { |
| 124 | + content: [ |
| 125 | + { |
| 126 | + type: "text", |
| 127 | + text: buildVideoGenerationTaskStatusText(activeTask, { duplicateGuard: true }), |
| 128 | + }, |
| 129 | + ], |
| 130 | + details: { |
| 131 | + action: "status", |
| 132 | + duplicateGuard: true, |
| 133 | + ...buildVideoGenerationTaskStatusDetails(activeTask), |
| 134 | + }, |
| 135 | + }; |
| 136 | +} |
0 commit comments