{"version":3,"sources":["../../src/vertexai/veo.ts"],"sourcesContent":["/**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n  ActionMetadata,\n  ModelReference,\n  modelActionMetadata,\n  modelRef,\n  z,\n} from 'genkit';\nimport { BackgroundModelAction, ModelInfo } from 'genkit/model';\nimport { backgroundModel as pluginBackgroundModel } from 'genkit/plugin';\nimport { isKnownKey } from '../common/utils.mjs';\nimport { veoCheckOperation, veoPredict } from './client.mjs';\nimport {\n  fromVeoOperation,\n  toVeoModel,\n  toVeoOperationRequest,\n  toVeoPredictRequest,\n} from './converters.mjs';\nimport { ClientOptions, Model, VertexPluginOptions } from './types.mjs';\nimport {\n  applyContextOverrides,\n  calculateRequestOptions,\n  checkModelName,\n  extractVersion,\n} from './utils.mjs';\n\nexport const VeoConfigSchema = z\n  .object({\n    sampleCount: z.number().optional().describe('Number of output videos'),\n    storageUri: z\n      .string()\n      .optional()\n      .describe('The gcs bucket where to save the generated videos'),\n    fps: z\n      .number()\n      .optional()\n      .describe('Frames per second for video generation'),\n    durationSeconds: z\n      .number()\n      .optional()\n      .describe('Duration of the clip for video generation in seconds'),\n    seed: z\n      .number()\n      .optional()\n      .describe(\n        'The RNG seed. If RNG seed is exactly same for each request with unchanged ' +\n          'inputs, the prediction results will be consistent. Otherwise, a random RNG ' +\n          'seed will be used each time to produce a different result. If the sample ' +\n          'count is greater than 1, random seeds will be used for each sample.'\n      ),\n    aspectRatio: z\n      .enum(['9:16', '16:9'])\n      .optional()\n      .describe('The aspect ratio for the generated video'),\n    resolution: z\n      .enum(['720p', '1080p'])\n      .optional()\n      .describe('The resolution for the generated video'),\n    personGeneration: z\n      .enum(['dont_allow', 'allow_adult', 'allow_all'])\n      .optional()\n      .describe(\n        'Specifies the policy for generating persons in videos, including age restrictions'\n      ),\n    pubsubTopic: z\n      .string()\n      .optional()\n      .describe('The pubsub topic to publish the video generation progress to'),\n    negativePrompt: z\n      .string()\n      .optional()\n      .describe(\n        'In addition to the text context, negative prompts can be explicitly stated here to help generate the video'\n      ),\n    enhancePrompt: z\n      .boolean()\n      .optional()\n      .describe(\n        'If true, the prompt will be improved before it is used to generate videos. ' +\n          'The RNG seed, if provided, will not result in consistent results if prompts are enhanced.'\n      ),\n    generateAudio: z\n      .boolean()\n      .optional()\n      .describe('If true, audio will be generated along with the video'),\n    compressionQuality: z\n      .enum(['optimized', 'lossless'])\n      .default('optimized')\n      .optional()\n      .describe('Compression quality of the generated video'),\n    resizeMode: z\n      .enum(['pad', 'crop'])\n      .default('pad')\n      .optional()\n      .describe(\n        'Veo 3 only. The resize mode that the model uses to resize the video'\n      ),\n    location: z\n      .string()\n      .describe('Google Cloud region e.g. us-central1. or global')\n      .optional(),\n  })\n  .passthrough();\nexport type VeoConfigSchemaType = typeof VeoConfigSchema;\nexport type VeoConfig = z.infer<VeoConfigSchemaType>;\n\n// This contains all the Veo config schema types\ntype ConfigSchemaType = VeoConfigSchemaType;\n\nfunction commonRef(\n  name: string,\n  info?: ModelInfo,\n  configSchema: ConfigSchemaType = VeoConfigSchema\n): ModelReference<ConfigSchemaType> {\n  return modelRef({\n    name: `vertexai/${name}`,\n    configSchema,\n    info:\n      info ??\n      ({\n        supports: {\n          media: true,\n          multiturn: false,\n          tools: false,\n          systemRole: false,\n          output: ['media'],\n          longRunning: true,\n        },\n      } as ModelInfo), // TODO(ifielker): Remove this cast if we fix longRunning\n  });\n}\n\nconst GENERIC_MODEL = commonRef('veo');\n\nconst KNOWN_MODELS = {\n  'veo-3.1-lite-generate-001': commonRef('veo-3.1-lite-generate-001'),\n  'veo-3.1-fast-generate-001': commonRef('veo-3.1-fast-generate-001'),\n  'veo-3.1-generate-001': commonRef('veo-3.1-generate-001'),\n} as const;\nexport type KnownModels = keyof typeof KNOWN_MODELS; // For autocomplete\n\n// Models we no longer suggest, but that still work: they still resolve and\n// are still registered as actions, but aren't in the KnownModels type, so\n// they don't show up in autocomplete. Set `stage` on each one (keeping the\n// default `supports`). Remove a model once the server returns 404 for it.\nconst HIDDEN_MODELS = {} as const;\n\nconst ALL_MODELS = {\n  ...HIDDEN_MODELS,\n  ...KNOWN_MODELS,\n};\n\nexport type VeoModelName = `veo-${string}`;\nexport function isVeoModelName(value?: string): value is VeoModelName {\n  return !!value?.startsWith('veo-');\n}\n\nexport function model(\n  version: string,\n  config: VeoConfig = {}\n): ModelReference<ConfigSchemaType> {\n  const name = checkModelName(version);\n\n  if (isKnownKey(name, ALL_MODELS)) {\n    const known = ALL_MODELS[name];\n    return modelRef({\n      name: known.name,\n      info: known.info,\n      configSchema: known.configSchema,\n      config,\n    });\n  }\n\n  return modelRef({\n    name: `vertexai/${name}`,\n    config,\n    configSchema: VeoConfigSchema,\n    info: { ...GENERIC_MODEL.info },\n  });\n}\n\n// Takes a full list of models, filters for current Veo models only\n// and returns a modelActionMetadata for each.\nexport function listActions(models: Model[]): ActionMetadata[] {\n  return models\n    .filter((m: Model) => isVeoModelName(m.name))\n    .map((m: Model) => {\n      const ref = model(m.name);\n      return modelActionMetadata({\n        name: ref.name,\n        info: ref.info,\n        configSchema: ref.configSchema,\n      });\n    });\n}\n\nexport function listKnownModels(\n  clientOptions: ClientOptions,\n  pluginOptions?: VertexPluginOptions\n) {\n  return Object.keys(ALL_MODELS).map((name: string) =>\n    defineModel(name, clientOptions, pluginOptions)\n  );\n}\n\nexport function defineModel(\n  name: string,\n  clientOptions: ClientOptions,\n  pluginOptions?: VertexPluginOptions\n): BackgroundModelAction<VeoConfigSchemaType> {\n  const ref = model(name);\n\n  return pluginBackgroundModel({\n    name: ref.name,\n    ...ref.info,\n    configSchema: ref.configSchema,\n    async start(request, options) {\n      const clientOpt = applyContextOverrides(\n        calculateRequestOptions(clientOptions, request.config),\n        options?.context\n      );\n      const veoPredictRequest = toVeoPredictRequest(request);\n\n      const response = await veoPredict(\n        extractVersion(ref),\n        veoPredictRequest,\n        clientOpt\n      );\n\n      return fromVeoOperation(response);\n    },\n    async check(operation, options) {\n      const checkClientOptions = applyContextOverrides(\n        clientOptions,\n        options?.context\n      );\n      const response = await veoCheckOperation(\n        toVeoModel(operation),\n        toVeoOperationRequest(operation),\n        checkClientOptions\n      );\n      return fromVeoOperation(response);\n    },\n  });\n}\n\nexport const TEST_ONLY = {\n  GENERIC_MODEL,\n  KNOWN_MODELS,\n  HIDDEN_MODELS,\n  ALL_MODELS,\n};\n"],"mappings":"AAgBA;AAAA,EAGE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,mBAAmB,6BAA6B;AACzD,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB,kBAAkB;AAC9C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEA,MAAM,kBAAkB,EAC5B,OAAO;AAAA,EACN,aAAa,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,yBAAyB;AAAA,EACrE,YAAY,EACT,OAAO,EACP,SAAS,EACT,SAAS,mDAAmD;AAAA,EAC/D,KAAK,EACF,OAAO,EACP,SAAS,EACT,SAAS,wCAAwC;AAAA,EACpD,iBAAiB,EACd,OAAO,EACP,SAAS,EACT,SAAS,sDAAsD;AAAA,EAClE,MAAM,EACH,OAAO,EACP,SAAS,EACT;AAAA,IACC;AAAA,EAIF;AAAA,EACF,aAAa,EACV,KAAK,CAAC,QAAQ,MAAM,CAAC,EACrB,SAAS,EACT,SAAS,0CAA0C;AAAA,EACtD,YAAY,EACT,KAAK,CAAC,QAAQ,OAAO,CAAC,EACtB,SAAS,EACT,SAAS,wCAAw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