@tanstack/ai 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/skills/ai-core/tool-calling/SKILL.md +26 -1
- package/src/activities/chat/index.ts +8 -0
- package/src/activities/chat/tools/lazy-tool-manager.ts +12 -4
- package/src/activities/chat/tools/lazy-tools.ts +33 -0
- package/src/index.ts +5 -0
- package/src/types.ts +23 -1
- package/dist/esm/activities/chat/adapter.d.ts +0 -163
- package/dist/esm/activities/chat/adapter.js +0 -17
- package/dist/esm/activities/chat/adapter.js.map +0 -1
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +0 -59
- package/dist/esm/activities/chat/agent-loop-strategies.js +0 -23
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +0 -1
- package/dist/esm/activities/chat/index.d.ts +0 -264
- package/dist/esm/activities/chat/index.js +0 -1723
- package/dist/esm/activities/chat/index.js.map +0 -1
- package/dist/esm/activities/chat/mcp/manager.d.ts +0 -25
- package/dist/esm/activities/chat/mcp/manager.js +0 -71
- package/dist/esm/activities/chat/mcp/manager.js.map +0 -1
- package/dist/esm/activities/chat/mcp/types.d.ts +0 -56
- package/dist/esm/activities/chat/messages.d.ts +0 -78
- package/dist/esm/activities/chat/messages.js +0 -374
- package/dist/esm/activities/chat/messages.js.map +0 -1
- package/dist/esm/activities/chat/middleware/builder.d.ts +0 -46
- package/dist/esm/activities/chat/middleware/builder.js +0 -17
- package/dist/esm/activities/chat/middleware/builder.js.map +0 -1
- package/dist/esm/activities/chat/middleware/capabilities.d.ts +0 -93
- package/dist/esm/activities/chat/middleware/capabilities.js +0 -45
- package/dist/esm/activities/chat/middleware/capabilities.js.map +0 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +0 -87
- package/dist/esm/activities/chat/middleware/compose.js +0 -510
- package/dist/esm/activities/chat/middleware/compose.js.map +0 -1
- package/dist/esm/activities/chat/middleware/define.d.ts +0 -20
- package/dist/esm/activities/chat/middleware/define.js +0 -7
- package/dist/esm/activities/chat/middleware/define.js.map +0 -1
- package/dist/esm/activities/chat/middleware/index.d.ts +0 -10
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +0 -89
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +0 -76
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +0 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +0 -405
- package/dist/esm/activities/chat/middleware/validate.d.ts +0 -19
- package/dist/esm/activities/chat/middleware/validate.js +0 -30
- package/dist/esm/activities/chat/middleware/validate.js.map +0 -1
- package/dist/esm/activities/chat/runtime-context-types.d.ts +0 -43
- package/dist/esm/activities/chat/stream/index.d.ts +0 -11
- package/dist/esm/activities/chat/stream/json-parser.d.ts +0 -38
- package/dist/esm/activities/chat/stream/json-parser.js +0 -28
- package/dist/esm/activities/chat/stream/json-parser.js.map +0 -1
- package/dist/esm/activities/chat/stream/message-updaters.d.ts +0 -81
- package/dist/esm/activities/chat/stream/message-updaters.js +0 -253
- package/dist/esm/activities/chat/stream/message-updaters.js.map +0 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +0 -439
- package/dist/esm/activities/chat/stream/processor.js +0 -1426
- package/dist/esm/activities/chat/stream/processor.js.map +0 -1
- package/dist/esm/activities/chat/stream/strategies.d.ts +0 -43
- package/dist/esm/activities/chat/stream/strategies.js +0 -54
- package/dist/esm/activities/chat/stream/strategies.js.map +0 -1
- package/dist/esm/activities/chat/stream/types.d.ts +0 -90
- package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +0 -81
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +0 -188
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +0 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +0 -140
- package/dist/esm/activities/chat/tools/schema-converter.js +0 -167
- package/dist/esm/activities/chat/tools/schema-converter.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +0 -140
- package/dist/esm/activities/chat/tools/tool-calls.js +0 -512
- package/dist/esm/activities/chat/tools/tool-calls.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +0 -135
- package/dist/esm/activities/chat/tools/tool-definition.js +0 -25
- package/dist/esm/activities/chat/tools/tool-definition.js.map +0 -1
- package/dist/esm/activities/error-payload.d.ts +0 -30
- package/dist/esm/activities/error-payload.js +0 -54
- package/dist/esm/activities/error-payload.js.map +0 -1
- package/dist/esm/activities/generateAudio/adapter.d.ts +0 -62
- package/dist/esm/activities/generateAudio/adapter.js +0 -16
- package/dist/esm/activities/generateAudio/adapter.js.map +0 -1
- package/dist/esm/activities/generateAudio/index.d.ts +0 -85
- package/dist/esm/activities/generateAudio/index.js +0 -114
- package/dist/esm/activities/generateAudio/index.js.map +0 -1
- package/dist/esm/activities/generateImage/adapter.d.ts +0 -78
- package/dist/esm/activities/generateImage/adapter.js +0 -16
- package/dist/esm/activities/generateImage/adapter.js.map +0 -1
- package/dist/esm/activities/generateImage/index.d.ts +0 -134
- package/dist/esm/activities/generateImage/index.js +0 -121
- package/dist/esm/activities/generateImage/index.js.map +0 -1
- package/dist/esm/activities/generateSpeech/adapter.d.ts +0 -62
- package/dist/esm/activities/generateSpeech/adapter.js +0 -16
- package/dist/esm/activities/generateSpeech/adapter.js.map +0 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +0 -96
- package/dist/esm/activities/generateSpeech/index.js +0 -119
- package/dist/esm/activities/generateSpeech/index.js.map +0 -1
- package/dist/esm/activities/generateTranscription/adapter.d.ts +0 -62
- package/dist/esm/activities/generateTranscription/adapter.js +0 -16
- package/dist/esm/activities/generateTranscription/adapter.js.map +0 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +0 -109
- package/dist/esm/activities/generateTranscription/index.js +0 -109
- package/dist/esm/activities/generateTranscription/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/adapter.d.ts +0 -145
- package/dist/esm/activities/generateVideo/adapter.js +0 -30
- package/dist/esm/activities/generateVideo/adapter.js.map +0 -1
- package/dist/esm/activities/generateVideo/index.d.ts +0 -223
- package/dist/esm/activities/generateVideo/index.js +0 -295
- package/dist/esm/activities/generateVideo/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/snap.d.ts +0 -14
- package/dist/esm/activities/generateVideo/snap.js +0 -54
- package/dist/esm/activities/generateVideo/snap.js.map +0 -1
- package/dist/esm/activities/index.d.ts +0 -27
- package/dist/esm/activities/index.js +0 -43
- package/dist/esm/activities/index.js.map +0 -1
- package/dist/esm/activities/middleware/index.d.ts +0 -2
- package/dist/esm/activities/middleware/run.d.ts +0 -20
- package/dist/esm/activities/middleware/run.js +0 -42
- package/dist/esm/activities/middleware/run.js.map +0 -1
- package/dist/esm/activities/middleware/types.d.ts +0 -118
- package/dist/esm/activities/stream-generation-result.d.ts +0 -15
- package/dist/esm/activities/stream-generation-result.js +0 -49
- package/dist/esm/activities/stream-generation-result.js.map +0 -1
- package/dist/esm/activities/summarize/adapter.d.ts +0 -74
- package/dist/esm/activities/summarize/adapter.js +0 -16
- package/dist/esm/activities/summarize/adapter.js.map +0 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +0 -45
- package/dist/esm/activities/summarize/chat-stream-summarize.js +0 -212
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +0 -1
- package/dist/esm/activities/summarize/index.d.ts +0 -108
- package/dist/esm/activities/summarize/index.js +0 -113
- package/dist/esm/activities/summarize/index.js.map +0 -1
- package/dist/esm/adapter-internals.d.ts +0 -5
- package/dist/esm/adapter-internals.js +0 -10
- package/dist/esm/adapter-internals.js.map +0 -1
- package/dist/esm/client.d.ts +0 -44
- package/dist/esm/client.js +0 -67
- package/dist/esm/client.js.map +0 -1
- package/dist/esm/extend-adapter.d.ts +0 -152
- package/dist/esm/extend-adapter.js +0 -21
- package/dist/esm/extend-adapter.js.map +0 -1
- package/dist/esm/index.d.ts +0 -44
- package/dist/esm/index.js +0 -106
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/logger/console-logger.d.ts +0 -29
- package/dist/esm/logger/console-logger.js +0 -83
- package/dist/esm/logger/console-logger.js.map +0 -1
- package/dist/esm/logger/internal-logger.d.ts +0 -41
- package/dist/esm/logger/internal-logger.js +0 -86
- package/dist/esm/logger/internal-logger.js.map +0 -1
- package/dist/esm/logger/resolve.d.ts +0 -14
- package/dist/esm/logger/resolve.js +0 -54
- package/dist/esm/logger/resolve.js.map +0 -1
- package/dist/esm/logger/types.d.ts +0 -75
- package/dist/esm/middlewares/content-guard.d.ts +0 -77
- package/dist/esm/middlewares/content-guard.js +0 -156
- package/dist/esm/middlewares/content-guard.js.map +0 -1
- package/dist/esm/middlewares/index.d.ts +0 -2
- package/dist/esm/middlewares/index.js +0 -7
- package/dist/esm/middlewares/index.js.map +0 -1
- package/dist/esm/middlewares/otel.d.ts +0 -81
- package/dist/esm/middlewares/otel.js +0 -745
- package/dist/esm/middlewares/otel.js.map +0 -1
- package/dist/esm/middlewares/tool-cache.d.ts +0 -1
- package/dist/esm/middlewares/usage-attributes.d.ts +0 -24
- package/dist/esm/middlewares/usage-attributes.js +0 -43
- package/dist/esm/middlewares/usage-attributes.js.map +0 -1
- package/dist/esm/realtime/index.d.ts +0 -28
- package/dist/esm/realtime/index.js +0 -8
- package/dist/esm/realtime/index.js.map +0 -1
- package/dist/esm/realtime/types.d.ts +0 -282
- package/dist/esm/stream-to-response.d.ts +0 -102
- package/dist/esm/stream-to-response.js +0 -121
- package/dist/esm/stream-to-response.js.map +0 -1
- package/dist/esm/strip-to-spec-middleware.d.ts +0 -18
- package/dist/esm/strip-to-spec-middleware.js +0 -20
- package/dist/esm/strip-to-spec-middleware.js.map +0 -1
- package/dist/esm/system-prompts.d.ts +0 -66
- package/dist/esm/system-prompts.js +0 -23
- package/dist/esm/system-prompts.js.map +0 -1
- package/dist/esm/tool-registry.d.ts +0 -81
- package/dist/esm/tool-registry.js +0 -49
- package/dist/esm/tool-registry.js.map +0 -1
- package/dist/esm/tools/provider-tool.d.ts +0 -30
- package/dist/esm/tools/provider-tool.js +0 -7
- package/dist/esm/tools/provider-tool.js.map +0 -1
- package/dist/esm/types.d.ts +0 -1573
- package/dist/esm/utilities/ag-ui-wire.d.ts +0 -44
- package/dist/esm/utilities/ag-ui-wire.js +0 -107
- package/dist/esm/utilities/ag-ui-wire.js.map +0 -1
- package/dist/esm/utilities/chat-params.d.ts +0 -85
- package/dist/esm/utilities/chat-params.js +0 -100
- package/dist/esm/utilities/chat-params.js.map +0 -1
- package/dist/esm/utilities/errors.d.ts +0 -13
- package/dist/esm/utilities/errors.js +0 -22
- package/dist/esm/utilities/errors.js.map +0 -1
- package/dist/esm/utilities/media-prompt.d.ts +0 -35
- package/dist/esm/utilities/media-prompt.js +0 -43
- package/dist/esm/utilities/media-prompt.js.map +0 -1
- package/dist/esm/utilities/numbers.d.ts +0 -8
- package/dist/esm/utilities/numbers.js +0 -12
- package/dist/esm/utilities/numbers.js.map +0 -1
- package/dist/esm/utilities/sampling-keys.d.ts +0 -20
- package/dist/esm/utilities/sampling-keys.js +0 -20
- package/dist/esm/utilities/sampling-keys.js.map +0 -1
- package/dist/esm/utilities/tool-result.d.ts +0 -21
- package/dist/esm/utilities/tool-result.js +0 -37
- package/dist/esm/utilities/tool-result.js.map +0 -1
- package/dist/esm/utilities/usage.d.ts +0 -31
- package/dist/esm/utilities/usage.js +0 -11
- package/dist/esm/utilities/usage.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -17
- package/dist/esm/utils.js +0 -20
- package/dist/esm/utils.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sources":["../../../../src/activities/generateVideo/adapter.ts"],"sourcesContent":["import type {\n ModelInputModalitiesByName,\n VideoGenerationOptions,\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\n\n/**\n * Structured description of the durations a video model accepts.\n *\n * Tagged union so the same shape can express discrete enums (OpenAI Sora,\n * Veo), continuous ranges, mixed shapes, and models with no duration field.\n * Consumed by `VideoAdapter.availableDurations()`.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type DurationOptions<T extends string | number | undefined> =\n | { kind: 'discrete'; values: ReadonlyArray<NonNullable<T>> }\n | { kind: 'range'; min: number; max: number; step?: number; unit: 'seconds' }\n | {\n kind: 'mixed'\n values: ReadonlyArray<NonNullable<T>>\n range?: { min: number; max: number; step?: number }\n }\n | { kind: 'none' }\n\n/**\n * Configuration for video adapter instances\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Video adapter interface with pre-resolved generics.\n *\n * An adapter is created by a provider function: `provider('model')` → `adapter`\n * All type resolution happens at the provider call site, not in this interface.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * Generic parameters:\n * - TModel: The specific model name (e.g., 'sora-2')\n * - TProviderOptions: Provider-specific options (already resolved)\n * - TModelProviderOptionsByName: Map from model name to its specific provider options\n * - TModelSizeByName: Map from model name to its supported sizes\n * - TModelInputModalitiesByName: Map from model name to the non-text prompt\n * modalities it accepts (constrains the `prompt` part types at compile time)\n * - TModelDurationByName: Map from model name to its supported duration\n * union. Defaults to `Record<string, number>` so adapters that haven't\n * declared a map keep today's `duration?: number` typing.\n */\nexport interface VideoAdapter<\n TModel extends string = string,\n TProviderOptions extends object = Record<string, unknown>,\n TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,\n TModelSizeByName extends Record<string, string | undefined> = Record<\n string,\n string\n >,\n TModelInputModalitiesByName extends ModelInputModalitiesByName =\n ModelInputModalitiesByName,\n TModelDurationByName extends Record<string, string | number | undefined> =\n Record<string, number>,\n> {\n /** Discriminator for adapter kind - used to determine API shape */\n readonly kind: 'video'\n /** Adapter name identifier */\n readonly name: string\n /** The model this adapter is configured for */\n readonly model: TModel\n\n /**\n * @internal Type-only properties for inference. Not assigned at runtime.\n */\n '~types': {\n providerOptions: TProviderOptions\n modelProviderOptionsByName: TModelProviderOptionsByName\n modelSizeByName: TModelSizeByName\n modelInputModalitiesByName: TModelInputModalitiesByName\n modelDurationByName: TModelDurationByName\n }\n\n /**\n * Create a new video generation job.\n * Returns a job ID that can be used to poll for status and retrieve the video.\n */\n createVideoJob: (\n options: VideoGenerationOptions<\n TProviderOptions,\n TModelSizeByName[TModel],\n TModelDurationByName[TModel]\n >,\n ) => Promise<VideoJobResult>\n\n /**\n * Get the current status of a video generation job.\n */\n getVideoStatus: (jobId: string) => Promise<VideoStatusResult>\n\n /**\n * Get the URL to download/view the generated video.\n * Should only be called after status is 'completed'.\n */\n getVideoUrl: (jobId: string) => Promise<VideoUrlResult>\n\n /**\n * Describe the durations this adapter's model accepts. Returns a tagged\n * union so consumers can render UI / coerce input without provider-specific\n * knowledge.\n */\n availableDurations: () => DurationOptions<TModelDurationByName[TModel]>\n\n /**\n * Coerce a raw seconds value to the closest valid duration for this model.\n * Returns `undefined` for models with no duration field.\n */\n snapDuration: (seconds: number) => TModelDurationByName[TModel] | undefined\n}\n\n/**\n * A VideoAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyVideoAdapter = VideoAdapter<any, any, any, any, any, any>\n\n/**\n * Abstract base class for video generation adapters.\n * Extend this class to implement a video adapter for a specific provider.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * Generic parameters match VideoAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseVideoAdapter<\n TModel extends string = string,\n TProviderOptions extends object = Record<string, unknown>,\n TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,\n TModelSizeByName extends Record<string, string | undefined> = Record<\n string,\n string\n >,\n TModelInputModalitiesByName extends ModelInputModalitiesByName =\n ModelInputModalitiesByName,\n TModelDurationByName extends Record<string, string | number | undefined> =\n Record<string, number>,\n> implements VideoAdapter<\n TModel,\n TProviderOptions,\n TModelProviderOptionsByName,\n TModelSizeByName,\n TModelInputModalitiesByName,\n TModelDurationByName\n> {\n readonly kind = 'video' as const\n abstract readonly name: string\n readonly model: TModel\n\n // Type-only property - never assigned at runtime\n declare '~types': {\n providerOptions: TProviderOptions\n modelProviderOptionsByName: TModelProviderOptionsByName\n modelSizeByName: TModelSizeByName\n modelInputModalitiesByName: TModelInputModalitiesByName\n modelDurationByName: TModelDurationByName\n }\n\n protected config: VideoAdapterConfig\n\n constructor(config: VideoAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract createVideoJob(\n options: VideoGenerationOptions<\n TProviderOptions,\n TModelSizeByName[TModel],\n TModelDurationByName[TModel]\n >,\n ): Promise<VideoJobResult>\n\n abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>\n\n abstract getVideoUrl(jobId: string): Promise<VideoUrlResult>\n\n /**\n * Default implementation returns `{ kind: 'none' }`. Adapters that have\n * declared their per-model duration map should override this.\n */\n availableDurations(): DurationOptions<TModelDurationByName[TModel]> {\n return { kind: 'none' }\n }\n\n /**\n * Default implementation returns `undefined`. Adapters that have declared\n * their per-model duration map should override.\n */\n snapDuration(_seconds: number): TModelDurationByName[TModel] | undefined {\n return undefined\n }\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AA6IO,MAAe,iBAmBpB;AAAA,EACS,OAAO;AAAA,EAEP;AAAA,EAWC;AAAA,EAEV,YAAY,SAA6B,CAAA,GAAI,OAAe;AAC1D,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,qBAAoE;AAClE,WAAO,EAAE,MAAM,OAAA;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAa,UAA4D;AACvE,WAAO;AAAA,EACT;AAAA,EAEU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
|
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import { DebugOption } from '../../logger/types.js';
|
|
2
|
-
import { GenerationMiddleware } from '../middleware.js';
|
|
3
|
-
import { VideoAdapter } from './adapter.js';
|
|
4
|
-
import { MediaPrompt, MediaPromptFor, StreamChunk, TokenUsage, VideoJobResult, VideoStatusResult, VideoUrlResult } from '../../types.js';
|
|
5
|
-
/** The adapter kind this activity handles */
|
|
6
|
-
export declare const kind: "video";
|
|
7
|
-
/**
|
|
8
|
-
* Extract provider options from a VideoAdapter via ~types.
|
|
9
|
-
*/
|
|
10
|
-
export type VideoProviderOptions<TAdapter> = TAdapter extends VideoAdapter<any, any, any, any> ? TAdapter['~types']['providerOptions'] : object;
|
|
11
|
-
/**
|
|
12
|
-
* Extract the size type for a VideoAdapter's model via ~types.
|
|
13
|
-
*/
|
|
14
|
-
export type VideoSizeForAdapter<TAdapter> = TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap> ? TModel extends keyof TSizeMap ? TSizeMap[TModel] : string : string;
|
|
15
|
-
/**
|
|
16
|
-
* Extract the prompt type a model accepts from a VideoAdapter via ~types.
|
|
17
|
-
* Mirrors `ImagePromptForModel`: models in the adapter's input-modality map
|
|
18
|
-
* get a `prompt` narrowed to text + their supported part types; adapters
|
|
19
|
-
* without a map fall back to the full MediaPrompt.
|
|
20
|
-
*/
|
|
21
|
-
export type VideoPromptForAdapter<TAdapter> = TAdapter extends VideoAdapter<infer TModel, any, any, any, infer ModsByName> ? string extends keyof ModsByName ? MediaPrompt : TModel extends keyof ModsByName ? MediaPromptFor<ModsByName[TModel][number]> : MediaPrompt : MediaPrompt;
|
|
22
|
-
/**
|
|
23
|
-
* Extract the duration type for a VideoAdapter's model via ~types.
|
|
24
|
-
* Mirrors `VideoSizeForAdapter`. Falls back to `number` for adapters that
|
|
25
|
-
* haven't declared per-model duration constraints.
|
|
26
|
-
*/
|
|
27
|
-
export type VideoDurationForAdapter<TAdapter> = TAdapter extends VideoAdapter<infer TModel, any, any, any, any, infer TDurationMap> ? TModel extends keyof TDurationMap ? TDurationMap[TModel] : number : number;
|
|
28
|
-
/**
|
|
29
|
-
* Base options shared by all video activity operations.
|
|
30
|
-
* The model is extracted from the adapter's model property.
|
|
31
|
-
*/
|
|
32
|
-
interface VideoActivityBaseOptions<TAdapter extends VideoAdapter<string, any, any, any>> {
|
|
33
|
-
/** The video adapter to use (must be created with a model) */
|
|
34
|
-
adapter: TAdapter & {
|
|
35
|
-
kind: typeof kind;
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Options for creating a new video generation job.
|
|
40
|
-
* The model is extracted from the adapter's model property.
|
|
41
|
-
*
|
|
42
|
-
* @template TAdapter - The video adapter type
|
|
43
|
-
* @template TStream - Whether to stream the output
|
|
44
|
-
*
|
|
45
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
46
|
-
*/
|
|
47
|
-
export type VideoCreateOptions<TAdapter extends VideoAdapter<string, any, any, any>, TStream extends boolean = false> = VideoActivityBaseOptions<TAdapter> & {
|
|
48
|
-
/** Request type - create a new job (default if not specified) */
|
|
49
|
-
request?: 'create';
|
|
50
|
-
/**
|
|
51
|
-
* Description of the desired video. Either a plain string, or — for models
|
|
52
|
-
* that support image-conditioned generation — an ordered array of content
|
|
53
|
-
* parts interleaving text with image inputs. Image parts may carry
|
|
54
|
-
* `metadata.role` (`'start_frame' | 'end_frame' | 'reference' |
|
|
55
|
-
* 'character'`) to disambiguate intent; positional fallback otherwise. The
|
|
56
|
-
* accepted part types are narrowed per model via the adapter's
|
|
57
|
-
* input-modality map.
|
|
58
|
-
*/
|
|
59
|
-
prompt: VideoPromptForAdapter<TAdapter>;
|
|
60
|
-
/** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
|
|
61
|
-
size?: VideoSizeForAdapter<TAdapter>;
|
|
62
|
-
/**
|
|
63
|
-
* Video duration in seconds. Adapters that declare a per-model duration
|
|
64
|
-
* map narrow this to the model's valid union (e.g. `4 | 6 | 8` for Veo 3).
|
|
65
|
-
* Pass `adapter.snapDuration(seconds)` to coerce raw seconds to a valid
|
|
66
|
-
* value.
|
|
67
|
-
*/
|
|
68
|
-
duration?: VideoDurationForAdapter<TAdapter>;
|
|
69
|
-
/**
|
|
70
|
-
* Whether to stream the video generation lifecycle.
|
|
71
|
-
* When true, returns an AsyncIterable<StreamChunk> that handles the full
|
|
72
|
-
* job lifecycle: create job, poll for status, yield updates, and yield final result.
|
|
73
|
-
* When false or not provided, returns a Promise<VideoJobResult>.
|
|
74
|
-
*
|
|
75
|
-
* @default false
|
|
76
|
-
*/
|
|
77
|
-
stream?: TStream;
|
|
78
|
-
/** Polling interval in milliseconds (stream mode only). @default 2000 */
|
|
79
|
-
pollingInterval?: number;
|
|
80
|
-
/** Maximum time to wait before timing out in milliseconds (stream mode only). @default 600000 */
|
|
81
|
-
maxDuration?: number;
|
|
82
|
-
/** Custom run ID (stream mode only) */
|
|
83
|
-
runId?: string;
|
|
84
|
-
/**
|
|
85
|
-
* Enable debug logging. Pass `true` to enable all categories, `false` to
|
|
86
|
-
* silence everything including errors, or a `DebugConfig` object for granular
|
|
87
|
-
* control and/or a custom `Logger`.
|
|
88
|
-
*/
|
|
89
|
-
debug?: DebugOption;
|
|
90
|
-
/**
|
|
91
|
-
* Observe-only middleware notified on start, usage, success, and error. Pass
|
|
92
|
-
* `otelMiddleware()` to emit OpenTelemetry spans, or implement the
|
|
93
|
-
* `GenerationMiddleware` contract for a custom backend. In streaming mode the
|
|
94
|
-
* span covers the full create→poll→complete lifecycle; in non-streaming mode
|
|
95
|
-
* it covers job submission. An abandoned stream fires `onAbort`.
|
|
96
|
-
*/
|
|
97
|
-
middleware?: Array<GenerationMiddleware>;
|
|
98
|
-
} & ({} extends VideoProviderOptions<TAdapter> ? {
|
|
99
|
-
/** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>;
|
|
100
|
-
} : {
|
|
101
|
-
/** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>;
|
|
102
|
-
});
|
|
103
|
-
/**
|
|
104
|
-
* Options for polling the status of a video generation job.
|
|
105
|
-
*
|
|
106
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
107
|
-
*/
|
|
108
|
-
export interface VideoStatusOptions<TAdapter extends VideoAdapter<string, any, any, any>> extends VideoActivityBaseOptions<TAdapter> {
|
|
109
|
-
/** Request type - get job status */
|
|
110
|
-
request: 'status';
|
|
111
|
-
/** The job ID to check status for */
|
|
112
|
-
jobId: string;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Options for getting the URL of a completed video.
|
|
116
|
-
*
|
|
117
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
118
|
-
*/
|
|
119
|
-
export interface VideoUrlOptions<TAdapter extends VideoAdapter<string, any, any, any>> extends VideoActivityBaseOptions<TAdapter> {
|
|
120
|
-
/** Request type - get video URL */
|
|
121
|
-
request: 'url';
|
|
122
|
-
/** The job ID to get URL for */
|
|
123
|
-
jobId: string;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Union type for all video activity options.
|
|
127
|
-
* Discriminated by the `request` field.
|
|
128
|
-
*
|
|
129
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
130
|
-
*/
|
|
131
|
-
export type VideoActivityOptions<TAdapter extends VideoAdapter<string, any, any, any>, TRequest extends 'create' | 'status' | 'url' = 'create', TStream extends boolean = false> = TRequest extends 'status' ? VideoStatusOptions<TAdapter> : TRequest extends 'url' ? VideoUrlOptions<TAdapter> : VideoCreateOptions<TAdapter, TStream>;
|
|
132
|
-
/**
|
|
133
|
-
* Result type for the video activity, based on request type and streaming.
|
|
134
|
-
* - If stream is true (create request): AsyncIterable<StreamChunk>
|
|
135
|
-
* - Otherwise: Promise<VideoJobResult | VideoStatusResult | VideoUrlResult>
|
|
136
|
-
*
|
|
137
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
138
|
-
*/
|
|
139
|
-
export type VideoActivityResult<TRequest extends 'create' | 'status' | 'url' = 'create', TStream extends boolean = false> = TRequest extends 'status' ? Promise<VideoStatusResult> : TRequest extends 'url' ? Promise<VideoUrlResult> : TStream extends true ? AsyncIterable<StreamChunk> : Promise<VideoJobResult>;
|
|
140
|
-
/**
|
|
141
|
-
* Generate video - creates a video generation job from a text prompt.
|
|
142
|
-
*
|
|
143
|
-
* Uses AI video generation models to create videos based on natural language descriptions.
|
|
144
|
-
* Unlike image generation, video generation is asynchronous and requires polling for completion.
|
|
145
|
-
*
|
|
146
|
-
* When `stream: true` is passed, handles the full job lifecycle automatically:
|
|
147
|
-
* create job → poll for status → stream updates → yield final result.
|
|
148
|
-
*
|
|
149
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
150
|
-
*
|
|
151
|
-
* @example Create a video generation job
|
|
152
|
-
* ```ts
|
|
153
|
-
* import { generateVideo } from '@tanstack/ai'
|
|
154
|
-
* import { openaiVideo } from '@tanstack/ai-openai'
|
|
155
|
-
*
|
|
156
|
-
* // Start a video generation job
|
|
157
|
-
* const { jobId } = await generateVideo({
|
|
158
|
-
* adapter: openaiVideo('sora-2'),
|
|
159
|
-
* prompt: 'A cat chasing a dog in a sunny park'
|
|
160
|
-
* })
|
|
161
|
-
*
|
|
162
|
-
* console.log('Job started:', jobId)
|
|
163
|
-
* ```
|
|
164
|
-
*
|
|
165
|
-
* @example Stream the full video generation lifecycle
|
|
166
|
-
* ```ts
|
|
167
|
-
* import { generateVideo, toServerSentEventsResponse } from '@tanstack/ai'
|
|
168
|
-
* import { openaiVideo } from '@tanstack/ai-openai'
|
|
169
|
-
*
|
|
170
|
-
* const stream = generateVideo({
|
|
171
|
-
* adapter: openaiVideo('sora-2'),
|
|
172
|
-
* prompt: 'A cat chasing a dog in a sunny park',
|
|
173
|
-
* stream: true,
|
|
174
|
-
* pollingInterval: 3000,
|
|
175
|
-
* })
|
|
176
|
-
*
|
|
177
|
-
* return toServerSentEventsResponse(stream)
|
|
178
|
-
* ```
|
|
179
|
-
*/
|
|
180
|
-
export declare function generateVideo<TAdapter extends VideoAdapter<string, any, any, any>, TStream extends boolean = false>(options: VideoCreateOptions<TAdapter, TStream>): VideoActivityResult<'create', TStream>;
|
|
181
|
-
/**
|
|
182
|
-
* Get video job status - returns the current status, progress, and URL if available.
|
|
183
|
-
*
|
|
184
|
-
* This function combines status checking and URL retrieval. If the job is completed,
|
|
185
|
-
* it will automatically fetch and include the video URL.
|
|
186
|
-
*
|
|
187
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
188
|
-
*
|
|
189
|
-
* @example Check job status
|
|
190
|
-
* ```ts
|
|
191
|
-
* import { getVideoJobStatus } from '@tanstack/ai'
|
|
192
|
-
* import { openaiVideo } from '@tanstack/ai-openai'
|
|
193
|
-
*
|
|
194
|
-
* const result = await getVideoJobStatus({
|
|
195
|
-
* adapter: openaiVideo('sora-2'),
|
|
196
|
-
* jobId: 'job-123'
|
|
197
|
-
* })
|
|
198
|
-
*
|
|
199
|
-
* console.log('Status:', result.status)
|
|
200
|
-
* console.log('Progress:', result.progress)
|
|
201
|
-
* if (result.url) {
|
|
202
|
-
* console.log('Video URL:', result.url)
|
|
203
|
-
* }
|
|
204
|
-
* ```
|
|
205
|
-
*/
|
|
206
|
-
export declare function getVideoJobStatus<TAdapter extends VideoAdapter<string, any, any, any>>(options: {
|
|
207
|
-
adapter: TAdapter & {
|
|
208
|
-
kind: typeof kind;
|
|
209
|
-
};
|
|
210
|
-
jobId: string;
|
|
211
|
-
}): Promise<{
|
|
212
|
-
status: 'pending' | 'processing' | 'completed' | 'failed';
|
|
213
|
-
progress?: number;
|
|
214
|
-
url?: string;
|
|
215
|
-
error?: string;
|
|
216
|
-
usage?: TokenUsage;
|
|
217
|
-
}>;
|
|
218
|
-
/**
|
|
219
|
-
* Create typed options for the generateVideo() function without executing.
|
|
220
|
-
*/
|
|
221
|
-
export declare function createVideoOptions<TAdapter extends VideoAdapter<string, any, any, any>, TStream extends boolean = false>(options: VideoCreateOptions<TAdapter, TStream>): VideoCreateOptions<TAdapter, TStream>;
|
|
222
|
-
export type { VideoAdapter, VideoAdapterConfig, AnyVideoAdapter, } from './adapter.js';
|
|
223
|
-
export { BaseVideoAdapter } from './adapter.js';
|
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import { aiEventClient } from "@tanstack/ai-event-client";
|
|
2
|
-
import { toRunErrorPayload } from "../error-payload.js";
|
|
3
|
-
import { resolveDebugOption } from "../../logger/resolve.js";
|
|
4
|
-
import { createGenerationContext, runGenerationStart, runGenerationUsage, runGenerationFinish, runGenerationError, runGenerationAbort } from "../middleware/run.js";
|
|
5
|
-
const kind = "video";
|
|
6
|
-
function createId(prefix) {
|
|
7
|
-
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
8
|
-
}
|
|
9
|
-
function generateVideo(options) {
|
|
10
|
-
if (options.stream) {
|
|
11
|
-
return runStreamingVideoGeneration(
|
|
12
|
-
options
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
return runCreateVideoJob(options);
|
|
16
|
-
}
|
|
17
|
-
async function runCreateVideoJob(options) {
|
|
18
|
-
const { adapter, prompt, size, duration, modelOptions, middleware } = options;
|
|
19
|
-
const model = adapter.model;
|
|
20
|
-
const requestId = createId("video");
|
|
21
|
-
const startTime = Date.now();
|
|
22
|
-
const logger = resolveDebugOption(options.debug);
|
|
23
|
-
const providerName = adapter.provider ?? adapter.name ?? "unknown";
|
|
24
|
-
const mwCtx = createGenerationContext({
|
|
25
|
-
requestId,
|
|
26
|
-
activity: "video",
|
|
27
|
-
provider: adapter.name,
|
|
28
|
-
model,
|
|
29
|
-
modelOptions,
|
|
30
|
-
createId
|
|
31
|
-
});
|
|
32
|
-
await runGenerationStart(middleware, mwCtx);
|
|
33
|
-
logger.request(`activity=generateVideo provider=${providerName}`, {
|
|
34
|
-
provider: providerName,
|
|
35
|
-
model
|
|
36
|
-
});
|
|
37
|
-
try {
|
|
38
|
-
const result = await adapter.createVideoJob({
|
|
39
|
-
model,
|
|
40
|
-
prompt,
|
|
41
|
-
size,
|
|
42
|
-
duration,
|
|
43
|
-
modelOptions,
|
|
44
|
-
logger
|
|
45
|
-
});
|
|
46
|
-
logger.output(`activity=generateVideo jobId=${result.jobId}`, {
|
|
47
|
-
jobId: result.jobId,
|
|
48
|
-
model: result.model
|
|
49
|
-
});
|
|
50
|
-
await runGenerationFinish(middleware, mwCtx, {
|
|
51
|
-
duration: Date.now() - startTime
|
|
52
|
-
});
|
|
53
|
-
return result;
|
|
54
|
-
} catch (error) {
|
|
55
|
-
await runGenerationError(middleware, mwCtx, {
|
|
56
|
-
error,
|
|
57
|
-
duration: Date.now() - startTime
|
|
58
|
-
});
|
|
59
|
-
logger.errors("generateVideo activity failed", {
|
|
60
|
-
error,
|
|
61
|
-
source: "generateVideo"
|
|
62
|
-
});
|
|
63
|
-
throw error;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function sleep(ms) {
|
|
67
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
68
|
-
}
|
|
69
|
-
async function* runStreamingVideoGeneration(options) {
|
|
70
|
-
const { adapter, prompt, size, duration, modelOptions, middleware } = options;
|
|
71
|
-
const model = adapter.model;
|
|
72
|
-
const runId = options.runId ?? createId("run");
|
|
73
|
-
const requestId = createId("video");
|
|
74
|
-
const obsStartTime = Date.now();
|
|
75
|
-
const pollingInterval = options.pollingInterval ?? 2e3;
|
|
76
|
-
const maxDuration = options.maxDuration ?? 6e5;
|
|
77
|
-
const logger = resolveDebugOption(options.debug);
|
|
78
|
-
const providerName = adapter.provider ?? adapter.name ?? "unknown";
|
|
79
|
-
const threadId = createId("thread");
|
|
80
|
-
yield {
|
|
81
|
-
type: "RUN_STARTED",
|
|
82
|
-
runId,
|
|
83
|
-
threadId,
|
|
84
|
-
timestamp: Date.now()
|
|
85
|
-
};
|
|
86
|
-
const mwCtx = createGenerationContext({
|
|
87
|
-
requestId,
|
|
88
|
-
activity: "video",
|
|
89
|
-
provider: adapter.name,
|
|
90
|
-
model,
|
|
91
|
-
modelOptions,
|
|
92
|
-
createId
|
|
93
|
-
});
|
|
94
|
-
await runGenerationStart(middleware, mwCtx);
|
|
95
|
-
logger.request(
|
|
96
|
-
`activity=generateVideo provider=${providerName} stream=true`,
|
|
97
|
-
{
|
|
98
|
-
provider: providerName,
|
|
99
|
-
model
|
|
100
|
-
}
|
|
101
|
-
);
|
|
102
|
-
let settled = false;
|
|
103
|
-
try {
|
|
104
|
-
const jobResult = await adapter.createVideoJob({
|
|
105
|
-
model,
|
|
106
|
-
prompt,
|
|
107
|
-
size,
|
|
108
|
-
duration,
|
|
109
|
-
modelOptions,
|
|
110
|
-
logger
|
|
111
|
-
});
|
|
112
|
-
yield {
|
|
113
|
-
type: "CUSTOM",
|
|
114
|
-
name: "video:job:created",
|
|
115
|
-
value: { jobId: jobResult.jobId },
|
|
116
|
-
timestamp: Date.now()
|
|
117
|
-
};
|
|
118
|
-
const startTime = Date.now();
|
|
119
|
-
while (Date.now() - startTime < maxDuration) {
|
|
120
|
-
await sleep(pollingInterval);
|
|
121
|
-
const statusResult = await adapter.getVideoStatus(jobResult.jobId);
|
|
122
|
-
yield {
|
|
123
|
-
type: "CUSTOM",
|
|
124
|
-
name: "video:status",
|
|
125
|
-
value: {
|
|
126
|
-
jobId: jobResult.jobId,
|
|
127
|
-
status: statusResult.status,
|
|
128
|
-
progress: statusResult.progress,
|
|
129
|
-
error: statusResult.error
|
|
130
|
-
},
|
|
131
|
-
timestamp: Date.now()
|
|
132
|
-
};
|
|
133
|
-
if (statusResult.status === "completed") {
|
|
134
|
-
const urlResult = await adapter.getVideoUrl(jobResult.jobId);
|
|
135
|
-
logger.output(
|
|
136
|
-
`activity=generateVideo jobId=${jobResult.jobId} status=completed`,
|
|
137
|
-
{
|
|
138
|
-
jobId: jobResult.jobId,
|
|
139
|
-
url: urlResult.url
|
|
140
|
-
}
|
|
141
|
-
);
|
|
142
|
-
if (urlResult.usage)
|
|
143
|
-
await runGenerationUsage(middleware, mwCtx, urlResult.usage);
|
|
144
|
-
await runGenerationFinish(middleware, mwCtx, {
|
|
145
|
-
duration: Date.now() - obsStartTime,
|
|
146
|
-
usage: urlResult.usage
|
|
147
|
-
});
|
|
148
|
-
settled = true;
|
|
149
|
-
yield {
|
|
150
|
-
type: "CUSTOM",
|
|
151
|
-
name: "generation:result",
|
|
152
|
-
value: {
|
|
153
|
-
jobId: jobResult.jobId,
|
|
154
|
-
status: "completed",
|
|
155
|
-
url: urlResult.url,
|
|
156
|
-
expiresAt: urlResult.expiresAt,
|
|
157
|
-
...urlResult.usage ? { usage: urlResult.usage } : {}
|
|
158
|
-
},
|
|
159
|
-
timestamp: Date.now()
|
|
160
|
-
};
|
|
161
|
-
yield {
|
|
162
|
-
type: "RUN_FINISHED",
|
|
163
|
-
runId,
|
|
164
|
-
threadId,
|
|
165
|
-
finishReason: "stop",
|
|
166
|
-
timestamp: Date.now()
|
|
167
|
-
};
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
if (statusResult.status === "failed") {
|
|
171
|
-
throw new Error(statusResult.error || "Video generation failed");
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
throw new Error("Video generation timed out");
|
|
175
|
-
} catch (error) {
|
|
176
|
-
const payload = toRunErrorPayload(error, "Video generation failed");
|
|
177
|
-
settled = true;
|
|
178
|
-
await runGenerationError(middleware, mwCtx, {
|
|
179
|
-
error,
|
|
180
|
-
duration: Date.now() - obsStartTime
|
|
181
|
-
});
|
|
182
|
-
logger.errors("generateVideo activity failed", {
|
|
183
|
-
message: payload.message,
|
|
184
|
-
code: payload.code,
|
|
185
|
-
source: "generateVideo"
|
|
186
|
-
});
|
|
187
|
-
yield {
|
|
188
|
-
type: "RUN_ERROR",
|
|
189
|
-
runId,
|
|
190
|
-
threadId,
|
|
191
|
-
message: payload.message,
|
|
192
|
-
code: payload.code,
|
|
193
|
-
error: payload,
|
|
194
|
-
timestamp: Date.now()
|
|
195
|
-
};
|
|
196
|
-
} finally {
|
|
197
|
-
if (!settled) {
|
|
198
|
-
await runGenerationAbort(middleware, mwCtx, {
|
|
199
|
-
reason: "Video generation stream abandoned before completion",
|
|
200
|
-
duration: Date.now() - obsStartTime
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
async function getVideoJobStatus(options) {
|
|
206
|
-
const { adapter, jobId } = options;
|
|
207
|
-
const requestId = createId("video-status");
|
|
208
|
-
const startTime = Date.now();
|
|
209
|
-
aiEventClient.emit("video:request:started", {
|
|
210
|
-
requestId,
|
|
211
|
-
provider: adapter.name,
|
|
212
|
-
model: adapter.model,
|
|
213
|
-
requestType: "status",
|
|
214
|
-
jobId,
|
|
215
|
-
timestamp: startTime
|
|
216
|
-
});
|
|
217
|
-
const statusResult = await adapter.getVideoStatus(jobId);
|
|
218
|
-
if (statusResult.status === "completed") {
|
|
219
|
-
try {
|
|
220
|
-
const urlResult = await adapter.getVideoUrl(jobId);
|
|
221
|
-
aiEventClient.emit("video:request:completed", {
|
|
222
|
-
requestId,
|
|
223
|
-
provider: adapter.name,
|
|
224
|
-
model: adapter.model,
|
|
225
|
-
requestType: "status",
|
|
226
|
-
jobId,
|
|
227
|
-
status: statusResult.status,
|
|
228
|
-
progress: statusResult.progress,
|
|
229
|
-
url: urlResult.url,
|
|
230
|
-
duration: Date.now() - startTime,
|
|
231
|
-
timestamp: Date.now()
|
|
232
|
-
});
|
|
233
|
-
if (urlResult.usage) {
|
|
234
|
-
aiEventClient.emit("video:usage", {
|
|
235
|
-
requestId,
|
|
236
|
-
model: adapter.model,
|
|
237
|
-
usage: urlResult.usage,
|
|
238
|
-
timestamp: Date.now()
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
return {
|
|
242
|
-
status: statusResult.status,
|
|
243
|
-
progress: statusResult.progress,
|
|
244
|
-
url: urlResult.url,
|
|
245
|
-
...urlResult.usage ? { usage: urlResult.usage } : {}
|
|
246
|
-
};
|
|
247
|
-
} catch (error) {
|
|
248
|
-
const errorMessage = error instanceof Error ? error.message : "Failed to get video URL";
|
|
249
|
-
aiEventClient.emit("video:request:completed", {
|
|
250
|
-
requestId,
|
|
251
|
-
provider: adapter.name,
|
|
252
|
-
model: adapter.model,
|
|
253
|
-
requestType: "status",
|
|
254
|
-
jobId,
|
|
255
|
-
status: "failed",
|
|
256
|
-
progress: statusResult.progress,
|
|
257
|
-
error: errorMessage,
|
|
258
|
-
duration: Date.now() - startTime,
|
|
259
|
-
timestamp: Date.now()
|
|
260
|
-
});
|
|
261
|
-
return {
|
|
262
|
-
status: "failed",
|
|
263
|
-
progress: statusResult.progress,
|
|
264
|
-
error: errorMessage
|
|
265
|
-
};
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
aiEventClient.emit("video:request:completed", {
|
|
269
|
-
requestId,
|
|
270
|
-
provider: adapter.name,
|
|
271
|
-
model: adapter.model,
|
|
272
|
-
requestType: "status",
|
|
273
|
-
jobId,
|
|
274
|
-
status: statusResult.status,
|
|
275
|
-
progress: statusResult.progress,
|
|
276
|
-
error: statusResult.error,
|
|
277
|
-
duration: Date.now() - startTime,
|
|
278
|
-
timestamp: Date.now()
|
|
279
|
-
});
|
|
280
|
-
return {
|
|
281
|
-
status: statusResult.status,
|
|
282
|
-
progress: statusResult.progress,
|
|
283
|
-
error: statusResult.error
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
function createVideoOptions(options) {
|
|
287
|
-
return options;
|
|
288
|
-
}
|
|
289
|
-
export {
|
|
290
|
-
createVideoOptions,
|
|
291
|
-
generateVideo,
|
|
292
|
-
getVideoJobStatus,
|
|
293
|
-
kind
|
|
294
|
-
};
|
|
295
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/activities/generateVideo/index.ts"],"sourcesContent":["/**\n * Video Activity (Experimental)\n *\n * Generates videos from text prompts using a jobs/polling architecture.\n * This is a self-contained module with implementation, types, and JSDoc.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\n\nimport { aiEventClient } from '@tanstack/ai-event-client'\nimport { toRunErrorPayload } from '../error-payload'\nimport { resolveDebugOption } from '../../logger/resolve'\nimport {\n createGenerationContext,\n runGenerationAbort,\n runGenerationError,\n runGenerationFinish,\n runGenerationStart,\n runGenerationUsage,\n} from '../middleware'\nimport type { InternalLogger } from '../../logger/internal-logger'\nimport type { DebugOption } from '../../logger/types'\nimport type { GenerationMiddleware } from '../middleware'\nimport type { VideoAdapter } from './adapter'\nimport type {\n MediaPrompt,\n MediaPromptFor,\n StreamChunk,\n TokenUsage,\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'video' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/**\n * Extract provider options from a VideoAdapter via ~types.\n */\nexport type VideoProviderOptions<TAdapter> =\n TAdapter extends VideoAdapter<any, any, any, any>\n ? TAdapter['~types']['providerOptions']\n : object\n\n/**\n * Extract the size type for a VideoAdapter's model via ~types.\n */\nexport type VideoSizeForAdapter<TAdapter> =\n TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap>\n ? TModel extends keyof TSizeMap\n ? TSizeMap[TModel]\n : string\n : string\n\n/**\n * Extract the prompt type a model accepts from a VideoAdapter via ~types.\n * Mirrors `ImagePromptForModel`: models in the adapter's input-modality map\n * get a `prompt` narrowed to text + their supported part types; adapters\n * without a map fall back to the full MediaPrompt.\n */\nexport type VideoPromptForAdapter<TAdapter> =\n TAdapter extends VideoAdapter<infer TModel, any, any, any, infer ModsByName>\n ? string extends keyof ModsByName\n ? MediaPrompt\n : TModel extends keyof ModsByName\n ? MediaPromptFor<ModsByName[TModel][number]>\n : MediaPrompt\n : MediaPrompt\n\n/**\n * Extract the duration type for a VideoAdapter's model via ~types.\n * Mirrors `VideoSizeForAdapter`. Falls back to `number` for adapters that\n * haven't declared per-model duration constraints.\n */\nexport type VideoDurationForAdapter<TAdapter> =\n TAdapter extends VideoAdapter<\n infer TModel,\n any,\n any,\n any,\n any,\n infer TDurationMap\n >\n ? TModel extends keyof TDurationMap\n ? TDurationMap[TModel]\n : number\n : number\n\n// ===========================\n// Activity Options Types\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n// ===========================\n\n/**\n * Base options shared by all video activity operations.\n * The model is extracted from the adapter's model property.\n */\ninterface VideoActivityBaseOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> {\n /** The video adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n}\n\n/**\n * Options for creating a new video generation job.\n * The model is extracted from the adapter's model property.\n *\n * @template TAdapter - The video adapter type\n * @template TStream - Whether to stream the output\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoCreateOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n> = VideoActivityBaseOptions<TAdapter> & {\n /** Request type - create a new job (default if not specified) */\n request?: 'create'\n /**\n * Description of the desired video. Either a plain string, or — for models\n * that support image-conditioned generation — an ordered array of content\n * parts interleaving text with image inputs. Image parts may carry\n * `metadata.role` (`'start_frame' | 'end_frame' | 'reference' |\n * 'character'`) to disambiguate intent; positional fallback otherwise. The\n * accepted part types are narrowed per model via the adapter's\n * input-modality map.\n */\n prompt: VideoPromptForAdapter<TAdapter>\n /** Video size — format depends on the provider (e.g., \"16:9\", \"1280x720\") */\n size?: VideoSizeForAdapter<TAdapter>\n /**\n * Video duration in seconds. Adapters that declare a per-model duration\n * map narrow this to the model's valid union (e.g. `4 | 6 | 8` for Veo 3).\n * Pass `adapter.snapDuration(seconds)` to coerce raw seconds to a valid\n * value.\n */\n duration?: VideoDurationForAdapter<TAdapter>\n /**\n * Whether to stream the video generation lifecycle.\n * When true, returns an AsyncIterable<StreamChunk> that handles the full\n * job lifecycle: create job, poll for status, yield updates, and yield final result.\n * When false or not provided, returns a Promise<VideoJobResult>.\n *\n * @default false\n */\n stream?: TStream\n /** Polling interval in milliseconds (stream mode only). @default 2000 */\n pollingInterval?: number\n /** Maximum time to wait before timing out in milliseconds (stream mode only). @default 600000 */\n maxDuration?: number\n /** Custom run ID (stream mode only) */\n runId?: string\n /**\n * Enable debug logging. Pass `true` to enable all categories, `false` to\n * silence everything including errors, or a `DebugConfig` object for granular\n * control and/or a custom `Logger`.\n */\n debug?: DebugOption\n /**\n * Observe-only middleware notified on start, usage, success, and error. Pass\n * `otelMiddleware()` to emit OpenTelemetry spans, or implement the\n * `GenerationMiddleware` contract for a custom backend. In streaming mode the\n * span covers the full create→poll→complete lifecycle; in non-streaming mode\n * it covers job submission. An abandoned stream fires `onAbort`.\n */\n middleware?: Array<GenerationMiddleware>\n} & ({} extends VideoProviderOptions<TAdapter>\n ? {\n /** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>\n }\n : {\n /** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>\n })\n\n/**\n * Options for polling the status of a video generation job.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoStatusOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get job status */\n request: 'status'\n /** The job ID to check status for */\n jobId: string\n}\n\n/**\n * Options for getting the URL of a completed video.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoUrlOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get video URL */\n request: 'url'\n /** The job ID to get URL for */\n jobId: string\n}\n\n/**\n * Union type for all video activity options.\n * Discriminated by the `request` field.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TRequest extends 'create' | 'status' | 'url' = 'create',\n TStream extends boolean = false,\n> = TRequest extends 'status'\n ? VideoStatusOptions<TAdapter>\n : TRequest extends 'url'\n ? VideoUrlOptions<TAdapter>\n : VideoCreateOptions<TAdapter, TStream>\n\n// ===========================\n// Activity Result Types\n// ===========================\n\n/**\n * Result type for the video activity, based on request type and streaming.\n * - If stream is true (create request): AsyncIterable<StreamChunk>\n * - Otherwise: Promise<VideoJobResult | VideoStatusResult | VideoUrlResult>\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityResult<\n TRequest extends 'create' | 'status' | 'url' = 'create',\n TStream extends boolean = false,\n> = TRequest extends 'status'\n ? Promise<VideoStatusResult>\n : TRequest extends 'url'\n ? Promise<VideoUrlResult>\n : TStream extends true\n ? AsyncIterable<StreamChunk>\n : Promise<VideoJobResult>\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Generate video - creates a video generation job from a text prompt.\n *\n * Uses AI video generation models to create videos based on natural language descriptions.\n * Unlike image generation, video generation is asynchronous and requires polling for completion.\n *\n * When `stream: true` is passed, handles the full job lifecycle automatically:\n * create job → poll for status → stream updates → yield final result.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Create a video generation job\n * ```ts\n * import { generateVideo } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * // Start a video generation job\n * const { jobId } = await generateVideo({\n * adapter: openaiVideo('sora-2'),\n * prompt: 'A cat chasing a dog in a sunny park'\n * })\n *\n * console.log('Job started:', jobId)\n * ```\n *\n * @example Stream the full video generation lifecycle\n * ```ts\n * import { generateVideo, toServerSentEventsResponse } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * const stream = generateVideo({\n * adapter: openaiVideo('sora-2'),\n * prompt: 'A cat chasing a dog in a sunny park',\n * stream: true,\n * pollingInterval: 3000,\n * })\n *\n * return toServerSentEventsResponse(stream)\n * ```\n */\nexport function generateVideo<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n>(\n options: VideoCreateOptions<TAdapter, TStream>,\n): VideoActivityResult<'create', TStream> {\n if (options.stream) {\n return runStreamingVideoGeneration(\n options as VideoCreateOptions<TAdapter, true>,\n ) as VideoActivityResult<'create', TStream>\n }\n\n return runCreateVideoJob(options) as VideoActivityResult<'create', TStream>\n}\n\n/**\n * Internal implementation of non-streaming video job creation.\n */\nasync function runCreateVideoJob<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter, boolean>): Promise<VideoJobResult> {\n const { adapter, prompt, size, duration, modelOptions, middleware } = options\n const model = adapter.model\n const requestId = createId('video')\n const startTime = Date.now()\n const logger: InternalLogger = resolveDebugOption(options.debug)\n const providerName =\n (adapter as { name?: string; provider?: string }).provider ??\n (adapter as { name?: string }).name ??\n 'unknown'\n\n const mwCtx = createGenerationContext({\n requestId,\n activity: 'video',\n provider: adapter.name,\n model,\n modelOptions,\n createId,\n })\n\n await runGenerationStart(middleware, mwCtx)\n\n logger.request(`activity=generateVideo provider=${providerName}`, {\n provider: providerName,\n model,\n })\n\n try {\n const result = await adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\n logger,\n })\n logger.output(`activity=generateVideo jobId=${result.jobId}`, {\n jobId: result.jobId,\n model: result.model,\n })\n // Non-streaming create only submits the job; usage isn't known until the\n // job completes via polling, so the span covers submission only.\n await runGenerationFinish(middleware, mwCtx, {\n duration: Date.now() - startTime,\n })\n return result\n } catch (error) {\n await runGenerationError(middleware, mwCtx, {\n error,\n duration: Date.now() - startTime,\n })\n logger.errors('generateVideo activity failed', {\n error,\n source: 'generateVideo',\n })\n throw error\n }\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\n/**\n * Internal streaming implementation for video generation.\n * Handles the full job lifecycle: create job → poll for status → stream updates → yield final result.\n */\nasync function* runStreamingVideoGeneration<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter, true>): AsyncIterable<StreamChunk> {\n const { adapter, prompt, size, duration, modelOptions, middleware } = options\n const model = adapter.model\n const runId = options.runId ?? createId('run')\n const requestId = createId('video')\n const obsStartTime = Date.now()\n const pollingInterval = options.pollingInterval ?? 2000\n const maxDuration = options.maxDuration ?? 600_000\n const logger: InternalLogger = resolveDebugOption(options.debug)\n const providerName =\n (adapter as { name?: string; provider?: string }).provider ??\n (adapter as { name?: string }).name ??\n 'unknown'\n\n const threadId = createId('thread')\n\n yield {\n type: 'RUN_STARTED',\n runId,\n threadId,\n timestamp: Date.now(),\n } as StreamChunk\n\n const mwCtx = createGenerationContext({\n requestId,\n activity: 'video',\n provider: adapter.name,\n model,\n modelOptions,\n createId,\n })\n\n await runGenerationStart(middleware, mwCtx)\n\n logger.request(\n `activity=generateVideo provider=${providerName} stream=true`,\n {\n provider: providerName,\n model,\n },\n )\n\n // Tracks whether a terminal observer event (finish/error) has already fired,\n // so the `finally` below can fire one on abandonment without double-firing.\n let settled = false\n try {\n // Create the video generation job\n const jobResult = await adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\n logger,\n })\n\n yield {\n type: 'CUSTOM',\n name: 'video:job:created',\n value: { jobId: jobResult.jobId },\n timestamp: Date.now(),\n } as StreamChunk\n\n // Poll for completion\n const startTime = Date.now()\n while (Date.now() - startTime < maxDuration) {\n await sleep(pollingInterval)\n\n const statusResult = await adapter.getVideoStatus(jobResult.jobId)\n\n yield {\n type: 'CUSTOM',\n name: 'video:status',\n value: {\n jobId: jobResult.jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n },\n timestamp: Date.now(),\n } as StreamChunk\n\n if (statusResult.status === 'completed') {\n const urlResult = await adapter.getVideoUrl(jobResult.jobId)\n\n logger.output(\n `activity=generateVideo jobId=${jobResult.jobId} status=completed`,\n {\n jobId: jobResult.jobId,\n url: urlResult.url,\n },\n )\n\n // Fire finish before yielding the terminal chunks: the generation has\n // succeeded, so a consumer that stops reading after `generation:result`\n // (without pulling `RUN_FINISHED`) must not trip the abandonment path in\n // `finally`, which would otherwise report a spurious cancellation.\n if (urlResult.usage)\n await runGenerationUsage(middleware, mwCtx, urlResult.usage)\n await runGenerationFinish(middleware, mwCtx, {\n duration: Date.now() - obsStartTime,\n usage: urlResult.usage,\n })\n settled = true\n\n yield {\n type: 'CUSTOM',\n name: 'generation:result',\n value: {\n jobId: jobResult.jobId,\n status: 'completed',\n url: urlResult.url,\n expiresAt: urlResult.expiresAt,\n ...(urlResult.usage ? { usage: urlResult.usage } : {}),\n },\n timestamp: Date.now(),\n } as StreamChunk\n\n yield {\n type: 'RUN_FINISHED',\n runId,\n threadId,\n finishReason: 'stop',\n timestamp: Date.now(),\n } as StreamChunk\n return\n }\n\n if (statusResult.status === 'failed') {\n throw new Error(statusResult.error || 'Video generation failed')\n }\n }\n\n throw new Error('Video generation timed out')\n } catch (error: unknown) {\n const payload = toRunErrorPayload(error, 'Video generation failed')\n // Mark settled before firing onError: if a user error-hook throws, the\n // `finally` below must still not double-fire onAbort over the same op\n // (which would mask the original error and end the span twice).\n settled = true\n await runGenerationError(middleware, mwCtx, {\n error,\n duration: Date.now() - obsStartTime,\n })\n logger.errors('generateVideo activity failed', {\n message: payload.message,\n code: payload.code,\n source: 'generateVideo',\n })\n yield {\n type: 'RUN_ERROR',\n runId,\n threadId,\n message: payload.message,\n code: payload.code,\n error: payload,\n timestamp: Date.now(),\n } as StreamChunk\n } finally {\n if (!settled) {\n // The consumer abandoned the stream (broke the `for await` loop or\n // disconnected) before completion, so the generator is being unwound at\n // a `yield` without reaching finish/error. Fire `onAbort` — a cancel, not\n // an error — so otelMiddleware ends its span instead of leaking it.\n await runGenerationAbort(middleware, mwCtx, {\n reason: 'Video generation stream abandoned before completion',\n duration: Date.now() - obsStartTime,\n })\n }\n }\n}\n\n/**\n * Get video job status - returns the current status, progress, and URL if available.\n *\n * This function combines status checking and URL retrieval. If the job is completed,\n * it will automatically fetch and include the video URL.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Check job status\n * ```ts\n * import { getVideoJobStatus } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * const result = await getVideoJobStatus({\n * adapter: openaiVideo('sora-2'),\n * jobId: 'job-123'\n * })\n *\n * console.log('Status:', result.status)\n * console.log('Progress:', result.progress)\n * if (result.url) {\n * console.log('Video URL:', result.url)\n * }\n * ```\n */\nexport async function getVideoJobStatus<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: {\n adapter: TAdapter & { kind: typeof kind }\n jobId: string\n}): Promise<{\n status: 'pending' | 'processing' | 'completed' | 'failed'\n progress?: number\n url?: string\n error?: string\n usage?: TokenUsage\n}> {\n const { adapter, jobId } = options\n const requestId = createId('video-status')\n const startTime = Date.now()\n\n aiEventClient.emit('video:request:started', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n timestamp: startTime,\n })\n\n // Get status first\n const statusResult = await adapter.getVideoStatus(jobId)\n\n // If completed, also get the URL\n if (statusResult.status === 'completed') {\n try {\n const urlResult = await adapter.getVideoUrl(jobId)\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n if (urlResult.usage) {\n aiEventClient.emit('video:usage', {\n requestId,\n model: adapter.model,\n usage: urlResult.usage,\n timestamp: Date.now(),\n })\n }\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n ...(urlResult.usage ? { usage: urlResult.usage } : {}),\n }\n } catch (error) {\n const errorMessage =\n error instanceof Error ? error.message : 'Failed to get video URL'\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: 'failed',\n progress: statusResult.progress,\n error: errorMessage,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n // Provider reported completed but result fetch failed — treat as failed\n return {\n status: 'failed' as const,\n progress: statusResult.progress,\n error: errorMessage,\n }\n }\n }\n\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n\n // Return status for non-completed jobs\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n }\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the generateVideo() function without executing.\n */\nexport function createVideoOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n>(\n options: VideoCreateOptions<TAdapter, TStream>,\n): VideoCreateOptions<TAdapter, TStream> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n VideoAdapter,\n VideoAdapterConfig,\n AnyVideoAdapter,\n} from './adapter'\nexport { BaseVideoAdapter } from './adapter'\n"],"names":[],"mappings":";;;;AAuCO,MAAM,OAAO;AA6DpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAmMO,SAAS,cAId,SACwC;AACxC,MAAI,QAAQ,QAAQ;AAClB,WAAO;AAAA,MACL;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO,kBAAkB,OAAO;AAClC;AAKA,eAAe,kBAEb,SAAyE;AACzE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,cAAc,eAAe;AACtE,QAAM,QAAQ,QAAQ;AACtB,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,YAAY,KAAK,IAAA;AACvB,QAAM,SAAyB,mBAAmB,QAAQ,KAAK;AAC/D,QAAM,eACH,QAAiD,YACjD,QAA8B,QAC/B;AAE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AK,2BAA2B;AAAA,QAC5C;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,OAAO,QAAQ;AAAA,QACf,aAAa;AAAA,QACb;AAAA,QACA,QAAQ;AAAA,QACR,UAAU,aAAa;AAAA,QACvB,OAAO;AAAA,QACP,UAAU,KAAK,IAAA,IAAQ;AAAA,QACvB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AAED,aAAO;AAAA,QACL,QAAQ;AAAA,QACR,UAAU,aAAa;AAAA,QACvB,OAAO;AAAA,MAAA;AAAA,IAEX;AAAA,EACF;AAEA,gBAAc,KAAK,2BAA2B;AAAA,IAC5C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB,OAAO,QAAQ;AAAA,IACf,aAAa;AAAA,IACb;AAAA,IACA,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,IACpB,UAAU,KAAK,IAAA,IAAQ;AAAA,IACvB,WAAW,KAAK,IAAA;AAAA,EAAI,CACrB;AAGD,SAAO;AAAA,IACL,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,EAAA;AAExB;AASO,SAAS,mBAId,SACuC;AACvC,SAAO;AACT;"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { DurationOptions } from './adapter.js';
|
|
2
|
-
/**
|
|
3
|
-
* Snap a raw seconds value to the closest valid duration for a model's
|
|
4
|
-
* `DurationOptions`.
|
|
5
|
-
*
|
|
6
|
-
* - `none` → `undefined`
|
|
7
|
-
* - `discrete` → closest numeric-parseable entry; if none parse,
|
|
8
|
-
* returns `values[0]` (keyword-only models like 'auto')
|
|
9
|
-
* - `range` → clamped to [min, max] and rounded to `step` (default 1)
|
|
10
|
-
* - `mixed` → closest of (discrete numerics ∪ range values)
|
|
11
|
-
*
|
|
12
|
-
* @experimental Video generation is an experimental feature and may change.
|
|
13
|
-
*/
|
|
14
|
-
export declare function snapToDurationOption<T extends string | number | undefined>(seconds: number, options: DurationOptions<T>): T | undefined;
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
function entryToSeconds(entry) {
|
|
2
|
-
if (typeof entry === "number") {
|
|
3
|
-
return Number.isFinite(entry) ? entry : null;
|
|
4
|
-
}
|
|
5
|
-
const stripped = entry.endsWith("s") ? entry.slice(0, -1) : entry;
|
|
6
|
-
const parsed = Number(stripped);
|
|
7
|
-
return Number.isFinite(parsed) ? parsed : null;
|
|
8
|
-
}
|
|
9
|
-
function snapToDurationOption(seconds, options) {
|
|
10
|
-
switch (options.kind) {
|
|
11
|
-
case "none":
|
|
12
|
-
return void 0;
|
|
13
|
-
case "discrete": {
|
|
14
|
-
return pickClosestDiscrete(seconds, options.values);
|
|
15
|
-
}
|
|
16
|
-
case "range": {
|
|
17
|
-
const step = options.step ?? 1;
|
|
18
|
-
const clamped = Math.min(options.max, Math.max(options.min, seconds));
|
|
19
|
-
const snapped = Math.round((clamped - options.min) / step) * step + options.min;
|
|
20
|
-
return Math.min(options.max, Math.max(options.min, snapped));
|
|
21
|
-
}
|
|
22
|
-
case "mixed": {
|
|
23
|
-
const discreteCandidate = pickClosestDiscrete(seconds, options.values);
|
|
24
|
-
if (!options.range) return discreteCandidate;
|
|
25
|
-
const { min, max, step = 1 } = options.range;
|
|
26
|
-
const clamped = Math.min(max, Math.max(min, seconds));
|
|
27
|
-
const rangeValue = Math.min(
|
|
28
|
-
max,
|
|
29
|
-
Math.max(min, Math.round((clamped - min) / step) * step + min)
|
|
30
|
-
);
|
|
31
|
-
const discreteSeconds = typeof discreteCandidate === "number" ? discreteCandidate : discreteCandidate !== void 0 ? entryToSeconds(discreteCandidate) ?? Infinity : Infinity;
|
|
32
|
-
return Math.abs(discreteSeconds - seconds) <= Math.abs(rangeValue - seconds) ? discreteCandidate : rangeValue;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function pickClosestDiscrete(seconds, values) {
|
|
37
|
-
if (values.length === 0) return void 0;
|
|
38
|
-
let best;
|
|
39
|
-
let bestDistance = Infinity;
|
|
40
|
-
for (const value of values) {
|
|
41
|
-
const v = entryToSeconds(value);
|
|
42
|
-
if (v === null) continue;
|
|
43
|
-
const distance = Math.abs(v - seconds);
|
|
44
|
-
if (distance < bestDistance) {
|
|
45
|
-
bestDistance = distance;
|
|
46
|
-
best = value;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return best ?? values[0];
|
|
50
|
-
}
|
|
51
|
-
export {
|
|
52
|
-
snapToDurationOption
|
|
53
|
-
};
|
|
54
|
-
//# sourceMappingURL=snap.js.map
|