@tanstack/ai 0.5.0 → 0.5.1

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.
@@ -1,4 +1,10 @@
1
1
  import { ImageGenerationOptions, ImageGenerationResult } from '../../types.js';
2
+ /**
3
+ * Resolve the size type for a model from the model-size map.
4
+ * If the map has an index signature (i.e. no explicit keys), falls back to string.
5
+ * If the model is an explicit key, uses its mapped size type.
6
+ * Otherwise falls back to string.
7
+ */
2
8
  /**
3
9
  * Configuration for image adapter instances
4
10
  */
@@ -39,7 +45,7 @@ export interface ImageAdapter<TModel extends string = string, TProviderOptions e
39
45
  /**
40
46
  * Generate images from a prompt
41
47
  */
42
- generateImages: (options: ImageGenerationOptions<TProviderOptions>) => Promise<ImageGenerationResult>;
48
+ generateImages: (options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>) => Promise<ImageGenerationResult>;
43
49
  }
44
50
  /**
45
51
  * An ImageAdapter with any/unknown type parameters.
@@ -63,6 +69,6 @@ export declare abstract class BaseImageAdapter<TModel extends string = string, T
63
69
  };
64
70
  protected config: ImageAdapterConfig;
65
71
  constructor(config: ImageAdapterConfig | undefined, model: TModel);
66
- abstract generateImages(options: ImageGenerationOptions<TProviderOptions>): Promise<ImageGenerationResult>;
72
+ abstract generateImages(options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>): Promise<ImageGenerationResult>;
67
73
  protected generateId(): string;
68
74
  }
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.js","sources":["../../../../src/activities/generateImage/adapter.ts"],"sourcesContent":["import type { ImageGenerationOptions, ImageGenerationResult } from '../../types'\n\n/**\n * Configuration for image adapter instances\n */\nexport interface ImageAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Image 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 * Generic parameters:\n * - TModel: The specific model name (e.g., 'dall-e-3')\n * - TProviderOptions: Base 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 */\nexport interface ImageAdapter<\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> = Record<string, string>,\n> {\n /** Discriminator for adapter kind - used by generate() to determine API shape */\n readonly kind: 'image'\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 }\n\n /**\n * Generate images from a prompt\n */\n generateImages: (\n options: ImageGenerationOptions<TProviderOptions>,\n ) => Promise<ImageGenerationResult>\n}\n\n/**\n * An ImageAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyImageAdapter = ImageAdapter<any, any, any, any>\n\n/**\n * Abstract base class for image generation adapters.\n * Extend this class to implement an image adapter for a specific provider.\n *\n * Generic parameters match ImageAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseImageAdapter<\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> = Record<string, string>,\n> implements ImageAdapter<\n TModel,\n TProviderOptions,\n TModelProviderOptionsByName,\n TModelSizeByName\n> {\n readonly kind = 'image' 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 }\n\n protected config: ImageAdapterConfig\n\n constructor(config: ImageAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract generateImages(\n options: ImageGenerationOptions<TProviderOptions>,\n ): Promise<ImageGenerationResult>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AAmEO,MAAe,iBAUpB;AAAA,EAcA,YAAY,SAA6B,CAAA,GAAI,OAAe;AAb5D,SAAS,OAAO;AAcd,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAMU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
1
+ {"version":3,"file":"adapter.js","sources":["../../../../src/activities/generateImage/adapter.ts"],"sourcesContent":["import type { ImageGenerationOptions, ImageGenerationResult } from '../../types'\n\n/**\n * Resolve the size type for a model from the model-size map.\n * If the map has an index signature (i.e. no explicit keys), falls back to string.\n * If the model is an explicit key, uses its mapped size type.\n * Otherwise falls back to string.\n */\n\n/**\n * Configuration for image adapter instances\n */\nexport interface ImageAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Image 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 * Generic parameters:\n * - TModel: The specific model name (e.g., 'dall-e-3')\n * - TProviderOptions: Base 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 */\nexport interface ImageAdapter<\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> = Record<string, string>,\n> {\n /** Discriminator for adapter kind - used by generate() to determine API shape */\n readonly kind: 'image'\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 }\n\n /**\n * Generate images from a prompt\n */\n generateImages: (\n options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,\n ) => Promise<ImageGenerationResult>\n}\n\n/**\n * An ImageAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyImageAdapter = ImageAdapter<any, any, any, any>\n\n/**\n * Abstract base class for image generation adapters.\n * Extend this class to implement an image adapter for a specific provider.\n *\n * Generic parameters match ImageAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseImageAdapter<\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> = Record<string, string>,\n> implements ImageAdapter<\n TModel,\n TProviderOptions,\n TModelProviderOptionsByName,\n TModelSizeByName\n> {\n readonly kind = 'image' 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 }\n\n protected config: ImageAdapterConfig\n\n constructor(config: ImageAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract generateImages(\n options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,\n ): Promise<ImageGenerationResult>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AA0EO,MAAe,iBAUpB;AAAA,EAcA,YAAY,SAA6B,CAAA,GAAI,OAAe;AAb5D,SAAS,OAAO;AAcd,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAMU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
@@ -19,7 +19,7 @@ export type ImageSizeForModel<TAdapter, TModel extends string> = TAdapter extend
19
19
  *
20
20
  * @template TAdapter - The image adapter type
21
21
  */
22
- export interface ImageActivityOptions<TAdapter extends ImageAdapter<string, object, any, any>> {
22
+ export type ImageActivityOptions<TAdapter extends ImageAdapter<string, any, any, any>> = {
23
23
  /** The image adapter to use (must be created with a model) */
24
24
  adapter: TAdapter & {
25
25
  kind: typeof kind;
@@ -30,9 +30,11 @@ export interface ImageActivityOptions<TAdapter extends ImageAdapter<string, obje
30
30
  numberOfImages?: number;
31
31
  /** Image size in WIDTHxHEIGHT format (e.g., "1024x1024") */
32
32
  size?: ImageSizeForModel<TAdapter, TAdapter['model']>;
33
- /** Provider-specific options for image generation */
34
- modelOptions?: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>;
35
- }
33
+ } & ({} extends ImageProviderOptionsForModel<TAdapter, TAdapter['model']> ? {
34
+ /** Provider-specific options for image generation */ modelOptions?: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>;
35
+ } : {
36
+ /** Provider-specific options for image generation */ modelOptions: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>;
37
+ });
36
38
  /** Result type for the image activity */
37
39
  export type ImageActivityResult = Promise<ImageGenerationResult>;
38
40
  /**
@@ -80,10 +82,10 @@ export type ImageActivityResult = Promise<ImageGenerationResult>;
80
82
  * })
81
83
  * ```
82
84
  */
83
- export declare function generateImage<TAdapter extends ImageAdapter<string, object, any, any>>(options: ImageActivityOptions<TAdapter>): ImageActivityResult;
85
+ export declare function generateImage<TAdapter extends ImageAdapter<string, any, any, any>>(options: ImageActivityOptions<TAdapter>): ImageActivityResult;
84
86
  /**
85
87
  * Create typed options for the generateImage() function without executing.
86
88
  */
87
- export declare function createImageOptions<TAdapter extends ImageAdapter<string, object, any, any>>(options: ImageActivityOptions<TAdapter>): ImageActivityOptions<TAdapter>;
89
+ export declare function createImageOptions<TAdapter extends ImageAdapter<string, any, any, any>>(options: ImageActivityOptions<TAdapter>): ImageActivityOptions<TAdapter>;
88
90
  export type { ImageAdapter, ImageAdapterConfig, AnyImageAdapter, } from './adapter.js';
89
91
  export { BaseImageAdapter } from './adapter.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../src/activities/generateImage/index.ts"],"sourcesContent":["/**\n * Image Activity\n *\n * Generates images from text prompts.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { aiEventClient } from '../../event-client.js'\nimport type { ImageAdapter } from './adapter'\nimport type { ImageGenerationResult } from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'image' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/**\n * Extract model-specific provider options from an ImageAdapter via ~types.\n * If the model has specific options defined in ModelProviderOptions (and not just via index signature),\n * use those; otherwise fall back to base provider options.\n */\nexport type ImageProviderOptionsForModel<TAdapter, TModel extends string> =\n TAdapter extends ImageAdapter<any, infer BaseOptions, infer ModelOptions, any>\n ? string extends keyof ModelOptions\n ? // ModelOptions is Record<string, unknown> or has index signature - use BaseOptions\n BaseOptions\n : // ModelOptions has explicit keys - check if TModel is one of them\n TModel extends keyof ModelOptions\n ? ModelOptions[TModel]\n : BaseOptions\n : object\n\n/**\n * Extract model-specific size options from an ImageAdapter via ~types.\n * If the model has specific sizes defined, use those; otherwise fall back to string.\n */\nexport type ImageSizeForModel<TAdapter, TModel extends string> =\n TAdapter extends ImageAdapter<any, any, any, infer SizeByName>\n ? string extends keyof SizeByName\n ? // SizeByName has index signature - fall back to string\n string\n : // SizeByName has explicit keys - check if TModel is one of them\n TModel extends keyof SizeByName\n ? SizeByName[TModel]\n : string\n : string\n\n// ===========================\n// Activity Options Type\n// ===========================\n\n/**\n * Options for the image activity.\n * The model is extracted from the adapter's model property.\n *\n * @template TAdapter - The image adapter type\n */\nexport interface ImageActivityOptions<\n TAdapter extends ImageAdapter<string, object, any, any>,\n> {\n /** The image adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n /** Text description of the desired image(s) */\n prompt: string\n /** Number of images to generate (default: 1) */\n numberOfImages?: number\n /** Image size in WIDTHxHEIGHT format (e.g., \"1024x1024\") */\n size?: ImageSizeForModel<TAdapter, TAdapter['model']>\n /** Provider-specific options for image generation */\n modelOptions?: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>\n}\n\n// ===========================\n// Activity Result Type\n// ===========================\n\n/** Result type for the image activity */\nexport type ImageActivityResult = Promise<ImageGenerationResult>\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Image activity - generates images from text prompts.\n *\n * Uses AI image generation models to create images based on natural language descriptions.\n *\n * @example Generate a single image\n * ```ts\n * import { generateImage } from '@tanstack/ai'\n * import { openaiImage } from '@tanstack/ai-openai'\n *\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-3'),\n * prompt: 'A serene mountain landscape at sunset'\n * })\n *\n * console.log(result.images[0].url)\n * ```\n *\n * @example Generate multiple images\n * ```ts\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-2'),\n * prompt: 'A cute robot mascot',\n * numberOfImages: 4,\n * size: '512x512'\n * })\n *\n * result.images.forEach((image, i) => {\n * console.log(`Image ${i + 1}: ${image.url}`)\n * })\n * ```\n *\n * @example With provider-specific options\n * ```ts\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-3'),\n * prompt: 'A professional headshot photo',\n * size: '1024x1024',\n * modelOptions: {\n * quality: 'hd',\n * style: 'natural'\n * }\n * })\n * ```\n */\nexport async function generateImage<\n TAdapter extends ImageAdapter<string, object, any, any>,\n>(options: ImageActivityOptions<TAdapter>): ImageActivityResult {\n const { adapter, ...rest } = options\n const model = adapter.model\n const requestId = createId('image')\n const startTime = Date.now()\n\n aiEventClient.emit('image:request:started', {\n requestId,\n provider: adapter.name,\n model,\n prompt: rest.prompt,\n numberOfImages: rest.numberOfImages,\n size: rest.size as string | undefined,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: startTime,\n })\n\n return adapter.generateImages({ ...rest, model }).then((result) => {\n const duration = Date.now() - startTime\n\n aiEventClient.emit('image:request:completed', {\n requestId,\n provider: adapter.name,\n model,\n images: result.images.map((image) => ({\n url: image.url,\n b64Json: image.b64Json,\n })),\n duration,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: Date.now(),\n })\n\n if (result.usage) {\n aiEventClient.emit('image:usage', {\n requestId,\n model,\n usage: result.usage,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: Date.now(),\n })\n }\n\n return result\n })\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the generateImage() function without executing.\n */\nexport function createImageOptions<\n TAdapter extends ImageAdapter<string, object, any, any>,\n>(options: ImageActivityOptions<TAdapter>): ImageActivityOptions<TAdapter> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n ImageAdapter,\n ImageAdapterConfig,\n AnyImageAdapter,\n} from './adapter'\nexport { BaseImageAdapter } from './adapter'\n"],"names":[],"mappings":";AAgBO,MAAM,OAAO;AAqEpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAmDA,eAAsB,cAEpB,SAA8D;AAC9D,QAAM,EAAE,SAAS,GAAG,KAAA,IAAS;AAC7B,QAAM,QAAQ,QAAQ;AACtB,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,YAAY,KAAK,IAAA;AAEvB,gBAAc,KAAK,yBAAyB;AAAA,IAC1C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA,QAAQ,KAAK;AAAA,IACb,gBAAgB,KAAK;AAAA,IACrB,MAAM,KAAK;AAAA,IACX,cAAc,KAAK;AAAA,IACnB,WAAW;AAAA,EAAA,CACZ;AAED,SAAO,QAAQ,eAAe,EAAE,GAAG,MAAM,OAAO,EAAE,KAAK,CAAC,WAAW;AACjE,UAAM,WAAW,KAAK,IAAA,IAAQ;AAE9B,kBAAc,KAAK,2BAA2B;AAAA,MAC5C;AAAA,MACA,UAAU,QAAQ;AAAA,MAClB;AAAA,MACA,QAAQ,OAAO,OAAO,IAAI,CAAC,WAAW;AAAA,QACpC,KAAK,MAAM;AAAA,QACX,SAAS,MAAM;AAAA,MAAA,EACf;AAAA,MACF;AAAA,MACA,cAAc,KAAK;AAAA,MACnB,WAAW,KAAK,IAAA;AAAA,IAAI,CACrB;AAED,QAAI,OAAO,OAAO;AAChB,oBAAc,KAAK,eAAe;AAAA,QAChC;AAAA,QACA;AAAA,QACA,OAAO,OAAO;AAAA,QACd,cAAc,KAAK;AAAA,QACnB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AAAA,IACH;AAEA,WAAO;AAAA,EACT,CAAC;AACH;AASO,SAAS,mBAEd,SAAyE;AACzE,SAAO;AACT;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../src/activities/generateImage/index.ts"],"sourcesContent":["/**\n * Image Activity\n *\n * Generates images from text prompts.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { aiEventClient } from '../../event-client.js'\nimport type { ImageAdapter } from './adapter'\nimport type { ImageGenerationResult } from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'image' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/**\n * Extract model-specific provider options from an ImageAdapter via ~types.\n * If the model has specific options defined in ModelProviderOptions (and not just via index signature),\n * use those; otherwise fall back to base provider options.\n */\nexport type ImageProviderOptionsForModel<TAdapter, TModel extends string> =\n TAdapter extends ImageAdapter<any, infer BaseOptions, infer ModelOptions, any>\n ? string extends keyof ModelOptions\n ? // ModelOptions is Record<string, unknown> or has index signature - use BaseOptions\n BaseOptions\n : // ModelOptions has explicit keys - check if TModel is one of them\n TModel extends keyof ModelOptions\n ? ModelOptions[TModel]\n : BaseOptions\n : object\n\n/**\n * Extract model-specific size options from an ImageAdapter via ~types.\n * If the model has specific sizes defined, use those; otherwise fall back to string.\n */\nexport type ImageSizeForModel<TAdapter, TModel extends string> =\n TAdapter extends ImageAdapter<any, any, any, infer SizeByName>\n ? string extends keyof SizeByName\n ? // SizeByName has index signature - fall back to string\n string\n : // SizeByName has explicit keys - check if TModel is one of them\n TModel extends keyof SizeByName\n ? SizeByName[TModel]\n : string\n : string\n\n// ===========================\n// Activity Options Type\n// ===========================\n\n/**\n * Options for the image activity.\n * The model is extracted from the adapter's model property.\n *\n * @template TAdapter - The image adapter type\n */\nexport type ImageActivityOptions<\n TAdapter extends ImageAdapter<string, any, any, any>,\n> = {\n /** The image adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n /** Text description of the desired image(s) */\n prompt: string\n /** Number of images to generate (default: 1) */\n numberOfImages?: number\n /** Image size in WIDTHxHEIGHT format (e.g., \"1024x1024\") */\n size?: ImageSizeForModel<TAdapter, TAdapter['model']>\n} & ({} extends ImageProviderOptionsForModel<TAdapter, TAdapter['model']>\n ? {\n /** Provider-specific options for image generation */ modelOptions?: ImageProviderOptionsForModel<\n TAdapter,\n TAdapter['model']\n >\n }\n : {\n /** Provider-specific options for image generation */ modelOptions: ImageProviderOptionsForModel<\n TAdapter,\n TAdapter['model']\n >\n })\n\n// ===========================\n// Activity Result Type\n// ===========================\n\n/** Result type for the image activity */\nexport type ImageActivityResult = Promise<ImageGenerationResult>\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Image activity - generates images from text prompts.\n *\n * Uses AI image generation models to create images based on natural language descriptions.\n *\n * @example Generate a single image\n * ```ts\n * import { generateImage } from '@tanstack/ai'\n * import { openaiImage } from '@tanstack/ai-openai'\n *\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-3'),\n * prompt: 'A serene mountain landscape at sunset'\n * })\n *\n * console.log(result.images[0].url)\n * ```\n *\n * @example Generate multiple images\n * ```ts\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-2'),\n * prompt: 'A cute robot mascot',\n * numberOfImages: 4,\n * size: '512x512'\n * })\n *\n * result.images.forEach((image, i) => {\n * console.log(`Image ${i + 1}: ${image.url}`)\n * })\n * ```\n *\n * @example With provider-specific options\n * ```ts\n * const result = await generateImage({\n * adapter: openaiImage('dall-e-3'),\n * prompt: 'A professional headshot photo',\n * size: '1024x1024',\n * modelOptions: {\n * quality: 'hd',\n * style: 'natural'\n * }\n * })\n * ```\n */\nexport async function generateImage<\n TAdapter extends ImageAdapter<string, any, any, any>,\n>(options: ImageActivityOptions<TAdapter>): ImageActivityResult {\n const { adapter, ...rest } = options\n const model = adapter.model\n const requestId = createId('image')\n const startTime = Date.now()\n\n aiEventClient.emit('image:request:started', {\n requestId,\n provider: adapter.name,\n model,\n prompt: rest.prompt,\n numberOfImages: rest.numberOfImages,\n size: rest.size,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: startTime,\n })\n\n return adapter.generateImages({ ...rest, model }).then((result) => {\n const duration = Date.now() - startTime\n\n aiEventClient.emit('image:request:completed', {\n requestId,\n provider: adapter.name,\n model,\n images: result.images.map((image) => ({\n url: image.url,\n b64Json: image.b64Json,\n })),\n duration,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: Date.now(),\n })\n\n if (result.usage) {\n aiEventClient.emit('image:usage', {\n requestId,\n model,\n usage: result.usage,\n modelOptions: rest.modelOptions as Record<string, unknown> | undefined,\n timestamp: Date.now(),\n })\n }\n\n return result\n })\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the generateImage() function without executing.\n */\nexport function createImageOptions<\n TAdapter extends ImageAdapter<string, any, any, any>,\n>(options: ImageActivityOptions<TAdapter>): ImageActivityOptions<TAdapter> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n ImageAdapter,\n ImageAdapterConfig,\n AnyImageAdapter,\n} from './adapter'\nexport { BaseImageAdapter } from './adapter'\n"],"names":[],"mappings":";AAgBO,MAAM,OAAO;AA+EpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAmDA,eAAsB,cAEpB,SAA8D;AAC9D,QAAM,EAAE,SAAS,GAAG,KAAA,IAAS;AAC7B,QAAM,QAAQ,QAAQ;AACtB,QAAM,YAAY,SAAS,OAAO;AAClC,QAAM,YAAY,KAAK,IAAA;AAEvB,gBAAc,KAAK,yBAAyB;AAAA,IAC1C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA,QAAQ,KAAK;AAAA,IACb,gBAAgB,KAAK;AAAA,IACrB,MAAM,KAAK;AAAA,IACX,cAAc,KAAK;AAAA,IACnB,WAAW;AAAA,EAAA,CACZ;AAED,SAAO,QAAQ,eAAe,EAAE,GAAG,MAAM,OAAO,EAAE,KAAK,CAAC,WAAW;AACjE,UAAM,WAAW,KAAK,IAAA,IAAQ;AAE9B,kBAAc,KAAK,2BAA2B;AAAA,MAC5C;AAAA,MACA,UAAU,QAAQ;AAAA,MAClB;AAAA,MACA,QAAQ,OAAO,OAAO,IAAI,CAAC,WAAW;AAAA,QACpC,KAAK,MAAM;AAAA,QACX,SAAS,MAAM;AAAA,MAAA,EACf;AAAA,MACF;AAAA,MACA,cAAc,KAAK;AAAA,MACnB,WAAW,KAAK,IAAA;AAAA,IAAI,CACrB;AAED,QAAI,OAAO,OAAO;AAChB,oBAAc,KAAK,eAAe;AAAA,QAChC;AAAA,QACA;AAAA,QACA,OAAO,OAAO;AAAA,QACd,cAAc,KAAK;AAAA,QACnB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AAAA,IACH;AAEA,WAAO;AAAA,EACT,CAAC;AACH;AASO,SAAS,mBAEd,SAAyE;AACzE,SAAO;AACT;"}
@@ -22,8 +22,10 @@ export interface VideoAdapterConfig {
22
22
  * Generic parameters:
23
23
  * - TModel: The specific model name (e.g., 'sora-2')
24
24
  * - TProviderOptions: Provider-specific options (already resolved)
25
+ * - TModelProviderOptionsByName: Map from model name to its specific provider options
26
+ * - TModelSizeByName: Map from model name to its supported sizes
25
27
  */
26
- export interface VideoAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>> {
28
+ export interface VideoAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>, TModelProviderOptionsByName extends Record<string, any> = Record<string, any>, TModelSizeByName extends Record<string, string> = Record<string, string>> {
27
29
  /** Discriminator for adapter kind - used to determine API shape */
28
30
  readonly kind: 'video';
29
31
  /** Adapter name identifier */
@@ -35,12 +37,14 @@ export interface VideoAdapter<TModel extends string = string, TProviderOptions e
35
37
  */
36
38
  '~types': {
37
39
  providerOptions: TProviderOptions;
40
+ modelProviderOptionsByName: TModelProviderOptionsByName;
41
+ modelSizeByName: TModelSizeByName;
38
42
  };
39
43
  /**
40
44
  * Create a new video generation job.
41
45
  * Returns a job ID that can be used to poll for status and retrieve the video.
42
46
  */
43
- createVideoJob: (options: VideoGenerationOptions<TProviderOptions>) => Promise<VideoJobResult>;
47
+ createVideoJob: (options: VideoGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>) => Promise<VideoJobResult>;
44
48
  /**
45
49
  * Get the current status of a video generation job.
46
50
  */
@@ -55,7 +59,7 @@ export interface VideoAdapter<TModel extends string = string, TProviderOptions e
55
59
  * A VideoAdapter with any/unknown type parameters.
56
60
  * Useful as a constraint in generic functions and interfaces.
57
61
  */
58
- export type AnyVideoAdapter = VideoAdapter<any, any>;
62
+ export type AnyVideoAdapter = VideoAdapter<any, any, any, any>;
59
63
  /**
60
64
  * Abstract base class for video generation adapters.
61
65
  * Extend this class to implement a video adapter for a specific provider.
@@ -64,16 +68,18 @@ export type AnyVideoAdapter = VideoAdapter<any, any>;
64
68
  *
65
69
  * Generic parameters match VideoAdapter - all pre-resolved by the provider function.
66
70
  */
67
- export declare abstract class BaseVideoAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>> implements VideoAdapter<TModel, TProviderOptions> {
71
+ export declare abstract class BaseVideoAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>, TModelProviderOptionsByName extends Record<string, any> = Record<string, any>, TModelSizeByName extends Record<string, string> = Record<string, string>> implements VideoAdapter<TModel, TProviderOptions, TModelProviderOptionsByName, TModelSizeByName> {
68
72
  readonly kind: "video";
69
73
  abstract readonly name: string;
70
74
  readonly model: TModel;
71
75
  '~types': {
72
76
  providerOptions: TProviderOptions;
77
+ modelProviderOptionsByName: TModelProviderOptionsByName;
78
+ modelSizeByName: TModelSizeByName;
73
79
  };
74
80
  protected config: VideoAdapterConfig;
75
81
  constructor(config: VideoAdapterConfig | undefined, model: TModel);
76
- abstract createVideoJob(options: VideoGenerationOptions<TProviderOptions>): Promise<VideoJobResult>;
82
+ abstract createVideoJob(options: VideoGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>): Promise<VideoJobResult>;
77
83
  abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>;
78
84
  abstract getVideoUrl(jobId: string): Promise<VideoUrlResult>;
79
85
  protected generateId(): string;
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.js","sources":["../../../../src/activities/generateVideo/adapter.ts"],"sourcesContent":["import type {\n VideoGenerationOptions,\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\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 */\nexport interface VideoAdapter<\n TModel extends string = string,\n TProviderOptions extends object = Record<string, unknown>,\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 }\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<TProviderOptions>,\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/**\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>\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> implements VideoAdapter<TModel, TProviderOptions> {\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 }\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<TProviderOptions>,\n ): Promise<VideoJobResult>\n\n abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>\n\n abstract getVideoUrl(jobId: string): Promise<VideoUrlResult>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AAoFO,MAAe,iBAG8B;AAAA,EAYlD,YAAY,SAA6B,CAAA,GAAI,OAAe;AAX5D,SAAS,OAAO;AAYd,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAUU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
1
+ {"version":3,"file":"adapter.js","sources":["../../../../src/activities/generateVideo/adapter.ts"],"sourcesContent":["import type {\n VideoGenerationOptions,\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\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 */\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> = Record<string, string>,\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 }\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<TProviderOptions, TModelSizeByName[TModel]>,\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/**\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>\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> = Record<string, string>,\n> implements VideoAdapter<\n TModel,\n TProviderOptions,\n TModelProviderOptionsByName,\n TModelSizeByName\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 }\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<TProviderOptions, TModelSizeByName[TModel]>,\n ): Promise<VideoJobResult>\n\n abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>\n\n abstract getVideoUrl(jobId: string): Promise<VideoUrlResult>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AA0FO,MAAe,iBAUpB;AAAA,EAcA,YAAY,SAA6B,CAAA,GAAI,OAAe;AAb5D,SAAS,OAAO;AAcd,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAUU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
@@ -5,12 +5,16 @@ export declare const kind: "video";
5
5
  /**
6
6
  * Extract provider options from a VideoAdapter via ~types.
7
7
  */
8
- export type VideoProviderOptions<TAdapter> = TAdapter extends VideoAdapter<any, any> ? TAdapter['~types']['providerOptions'] : object;
8
+ export type VideoProviderOptions<TAdapter> = TAdapter extends VideoAdapter<any, any, any, any> ? TAdapter['~types']['providerOptions'] : object;
9
+ /**
10
+ * Extract the size type for a VideoAdapter's model via ~types.
11
+ */
12
+ export type VideoSizeForAdapter<TAdapter> = TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap> ? TModel extends keyof TSizeMap ? TSizeMap[TModel] : string : string;
9
13
  /**
10
14
  * Base options shared by all video activity operations.
11
15
  * The model is extracted from the adapter's model property.
12
16
  */
13
- interface VideoActivityBaseOptions<TAdapter extends VideoAdapter<string, object>> {
17
+ interface VideoActivityBaseOptions<TAdapter extends VideoAdapter<string, any, any, any>> {
14
18
  /** The video adapter to use (must be created with a model) */
15
19
  adapter: TAdapter & {
16
20
  kind: typeof kind;
@@ -22,24 +26,26 @@ interface VideoActivityBaseOptions<TAdapter extends VideoAdapter<string, object>
22
26
  *
23
27
  * @experimental Video generation is an experimental feature and may change.
24
28
  */
25
- export interface VideoCreateOptions<TAdapter extends VideoAdapter<string, object>> extends VideoActivityBaseOptions<TAdapter> {
29
+ export type VideoCreateOptions<TAdapter extends VideoAdapter<string, any, any, any>> = VideoActivityBaseOptions<TAdapter> & {
26
30
  /** Request type - create a new job (default if not specified) */
27
31
  request?: 'create';
28
32
  /** Text description of the desired video */
29
33
  prompt: string;
30
- /** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
31
- size?: string;
34
+ /** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
35
+ size?: VideoSizeForAdapter<TAdapter>;
32
36
  /** Video duration in seconds */
33
37
  duration?: number;
34
- /** Provider-specific options for video generation */
35
- modelOptions?: VideoProviderOptions<TAdapter>;
36
- }
38
+ } & ({} extends VideoProviderOptions<TAdapter> ? {
39
+ /** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>;
40
+ } : {
41
+ /** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>;
42
+ });
37
43
  /**
38
44
  * Options for polling the status of a video generation job.
39
45
  *
40
46
  * @experimental Video generation is an experimental feature and may change.
41
47
  */
42
- export interface VideoStatusOptions<TAdapter extends VideoAdapter<string, object>> extends VideoActivityBaseOptions<TAdapter> {
48
+ export interface VideoStatusOptions<TAdapter extends VideoAdapter<string, any, any, any>> extends VideoActivityBaseOptions<TAdapter> {
43
49
  /** Request type - get job status */
44
50
  request: 'status';
45
51
  /** The job ID to check status for */
@@ -50,7 +56,7 @@ export interface VideoStatusOptions<TAdapter extends VideoAdapter<string, object
50
56
  *
51
57
  * @experimental Video generation is an experimental feature and may change.
52
58
  */
53
- export interface VideoUrlOptions<TAdapter extends VideoAdapter<string, object>> extends VideoActivityBaseOptions<TAdapter> {
59
+ export interface VideoUrlOptions<TAdapter extends VideoAdapter<string, any, any, any>> extends VideoActivityBaseOptions<TAdapter> {
54
60
  /** Request type - get video URL */
55
61
  request: 'url';
56
62
  /** The job ID to get URL for */
@@ -62,7 +68,7 @@ export interface VideoUrlOptions<TAdapter extends VideoAdapter<string, object>>
62
68
  *
63
69
  * @experimental Video generation is an experimental feature and may change.
64
70
  */
65
- export type VideoActivityOptions<TAdapter extends VideoAdapter<string, object>, TRequest extends 'create' | 'status' | 'url' = 'create'> = TRequest extends 'status' ? VideoStatusOptions<TAdapter> : TRequest extends 'url' ? VideoUrlOptions<TAdapter> : VideoCreateOptions<TAdapter>;
71
+ export type VideoActivityOptions<TAdapter extends VideoAdapter<string, any, any, any>, TRequest extends 'create' | 'status' | 'url' = 'create'> = TRequest extends 'status' ? VideoStatusOptions<TAdapter> : TRequest extends 'url' ? VideoUrlOptions<TAdapter> : VideoCreateOptions<TAdapter>;
66
72
  /**
67
73
  * Result type for the video activity, based on request type.
68
74
  *
@@ -91,7 +97,7 @@ export type VideoActivityResult<TRequest extends 'create' | 'status' | 'url' = '
91
97
  * console.log('Job started:', jobId)
92
98
  * ```
93
99
  */
94
- export declare function generateVideo<TAdapter extends VideoAdapter<string, object>>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult>;
100
+ export declare function generateVideo<TAdapter extends VideoAdapter<string, any, any, any>>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult>;
95
101
  /**
96
102
  * Get video job status - returns the current status, progress, and URL if available.
97
103
  *
@@ -117,7 +123,7 @@ export declare function generateVideo<TAdapter extends VideoAdapter<string, obje
117
123
  * }
118
124
  * ```
119
125
  */
120
- export declare function getVideoJobStatus<TAdapter extends VideoAdapter<string, object>>(options: {
126
+ export declare function getVideoJobStatus<TAdapter extends VideoAdapter<string, any, any, any>>(options: {
121
127
  adapter: TAdapter & {
122
128
  kind: typeof kind;
123
129
  };
@@ -131,6 +137,6 @@ export declare function getVideoJobStatus<TAdapter extends VideoAdapter<string,
131
137
  /**
132
138
  * Create typed options for the generateVideo() function without executing.
133
139
  */
134
- export declare function createVideoOptions<TAdapter extends VideoAdapter<string, object>>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter>;
140
+ export declare function createVideoOptions<TAdapter extends VideoAdapter<string, any, any, any>>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter>;
135
141
  export type { VideoAdapter, VideoAdapterConfig, AnyVideoAdapter, } from './adapter.js';
136
142
  export { BaseVideoAdapter } from './adapter.js';
@@ -1 +1 @@
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 '../../event-client.js'\nimport type { VideoAdapter } from './adapter'\nimport type {\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>\n ? TAdapter['~types']['providerOptions']\n : object\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, object>,\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 * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoCreateOptions<\n TAdapter extends VideoAdapter<string, object>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - create a new job (default if not specified) */\n request?: 'create'\n /** Text description of the desired video */\n prompt: string\n /** Video size in WIDTHxHEIGHT format (e.g., \"1280x720\") */\n size?: string\n /** Video duration in seconds */\n duration?: number\n /** Provider-specific options for video generation */\n 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, object>,\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, object>,\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, object>,\n TRequest extends 'create' | 'status' | 'url' = 'create',\n> = TRequest extends 'status'\n ? VideoStatusOptions<TAdapter>\n : TRequest extends 'url'\n ? VideoUrlOptions<TAdapter>\n : VideoCreateOptions<TAdapter>\n\n// ===========================\n// Activity Result Types\n// ===========================\n\n/**\n * Result type for the video activity, based on request type.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityResult<\n TRequest extends 'create' | 'status' | 'url' = 'create',\n> = TRequest extends 'status'\n ? Promise<VideoStatusResult>\n : TRequest extends 'url'\n ? Promise<VideoUrlResult>\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 * @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 */\nexport async function generateVideo<\n TAdapter extends VideoAdapter<string, object>,\n>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult> {\n const { adapter, prompt, size, duration, modelOptions } = options\n const model = adapter.model\n\n return adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\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, object>,\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}> {\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 return {\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n }\n } catch (error) {\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:\n error instanceof Error ? error.message : 'Failed to get video URL',\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n // If URL fetch fails, still return status\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\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, object>,\n>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter> {\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":";AAsBO,MAAM,OAAO;AAiBpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAyHA,eAAsB,cAEpB,SAAgE;AAChE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,iBAAiB;AAC1D,QAAM,QAAQ,QAAQ;AAEtB,SAAO,QAAQ,eAAe;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACD;AACH;AA2BA,eAAsB,kBAEpB,SAQC;AACD,QAAM,EAAE,SAAS,MAAA,IAAU;AAC3B,QAAM,YAAY,SAAS,cAAc;AACzC,QAAM,YAAY,KAAK,IAAA;AAEvB,gBAAc,KAAK,yBAAyB;AAAA,IAC1C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB,OAAO,QAAQ;AAAA,IACf,aAAa;AAAA,IACb;AAAA,IACA,WAAW;AAAA,EAAA,CACZ;AAGD,QAAM,eAAe,MAAM,QAAQ,eAAe,KAAK;AAGvD,MAAI,aAAa,WAAW,aAAa;AACvC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,YAAY,KAAK;AACjD,oBAAc,KAAK,2BAA2B;AAAA,QAC5C;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,OAAO,QAAQ;AAAA,QACf,aAAa;AAAA,QACb;AAAA,QACA,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,KAAK,UAAU;AAAA,QACf,UAAU,KAAK,IAAA,IAAQ;AAAA,QACvB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AACD,aAAO;AAAA,QACL,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,KAAK,UAAU;AAAA,MAAA;AAAA,IAEnB,SAAS,OAAO;AACd,oBAAc,KAAK,2BAA2B;AAAA,QAC5C;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,OAAO,QAAQ;AAAA,QACf,aAAa;AAAA,QACb;AAAA,QACA,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,OACE,iBAAiB,QAAQ,MAAM,UAAU;AAAA,QAC3C,UAAU,KAAK,IAAA,IAAQ;AAAA,QACvB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AAED,aAAO;AAAA,QACL,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,OACE,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAAA;AAAA,IAE/C;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,mBAEd,SAAqE;AACrE,SAAO;AACT;"}
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 '../../event-client.js'\nimport type { VideoAdapter } from './adapter'\nimport type {\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// 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 * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoCreateOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> = VideoActivityBaseOptions<TAdapter> & {\n /** Request type - create a new job (default if not specified) */\n request?: 'create'\n /** Text description of the desired video */\n prompt: string\n /** Video size — format depends on the provider (e.g., \"16:9\", \"1280x720\") */\n size?: VideoSizeForAdapter<TAdapter>\n /** Video duration in seconds */\n duration?: number\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> = TRequest extends 'status'\n ? VideoStatusOptions<TAdapter>\n : TRequest extends 'url'\n ? VideoUrlOptions<TAdapter>\n : VideoCreateOptions<TAdapter>\n\n// ===========================\n// Activity Result Types\n// ===========================\n\n/**\n * Result type for the video activity, based on request type.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityResult<\n TRequest extends 'create' | 'status' | 'url' = 'create',\n> = TRequest extends 'status'\n ? Promise<VideoStatusResult>\n : TRequest extends 'url'\n ? Promise<VideoUrlResult>\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 * @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 */\nexport async function generateVideo<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult> {\n const { adapter, prompt, size, duration, modelOptions } = options\n const model = adapter.model\n\n return adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\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}> {\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 return {\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n }\n } catch (error) {\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:\n error instanceof Error ? error.message : 'Failed to get video URL',\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n // If URL fetch fails, still return status\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\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>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter> {\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":";AAsBO,MAAM,OAAO;AA2BpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AA6HA,eAAsB,cAEpB,SAAgE;AAChE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,iBAAiB;AAC1D,QAAM,QAAQ,QAAQ;AAEtB,SAAO,QAAQ,eAAe;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACD;AACH;AA2BA,eAAsB,kBAEpB,SAQC;AACD,QAAM,EAAE,SAAS,MAAA,IAAU;AAC3B,QAAM,YAAY,SAAS,cAAc;AACzC,QAAM,YAAY,KAAK,IAAA;AAEvB,gBAAc,KAAK,yBAAyB;AAAA,IAC1C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB,OAAO,QAAQ;AAAA,IACf,aAAa;AAAA,IACb;AAAA,IACA,WAAW;AAAA,EAAA,CACZ;AAGD,QAAM,eAAe,MAAM,QAAQ,eAAe,KAAK;AAGvD,MAAI,aAAa,WAAW,aAAa;AACvC,QAAI;AACF,YAAM,YAAY,MAAM,QAAQ,YAAY,KAAK;AACjD,oBAAc,KAAK,2BAA2B;AAAA,QAC5C;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,OAAO,QAAQ;AAAA,QACf,aAAa;AAAA,QACb;AAAA,QACA,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,KAAK,UAAU;AAAA,QACf,UAAU,KAAK,IAAA,IAAQ;AAAA,QACvB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AACD,aAAO;AAAA,QACL,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,KAAK,UAAU;AAAA,MAAA;AAAA,IAEnB,SAAS,OAAO;AACd,oBAAc,KAAK,2BAA2B;AAAA,QAC5C;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,OAAO,QAAQ;AAAA,QACf,aAAa;AAAA,QACb;AAAA,QACA,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,OACE,iBAAiB,QAAQ,MAAM,UAAU;AAAA,QAC3C,UAAU,KAAK,IAAA,IAAQ;AAAA,QACvB,WAAW,KAAK,IAAA;AAAA,MAAI,CACrB;AAED,aAAO;AAAA,QACL,QAAQ,aAAa;AAAA,QACrB,UAAU,aAAa;AAAA,QACvB,OACE,iBAAiB,QAAQ,MAAM,UAAU;AAAA,MAAA;AAAA,IAE/C;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,mBAEd,SAAqE;AACrE,SAAO;AACT;"}
@@ -0,0 +1,114 @@
1
+ import { Modality } from './types.js';
2
+ /**
3
+ * Definition for a custom model to add to an adapter.
4
+ *
5
+ * @template TName - The model name as a literal string type
6
+ * @template TInput - Array of supported input modalities
7
+ * @template TOptions - Provider options type for this model
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const customModels = [
12
+ * createModel('my-custom-model', ['text', 'image']),
13
+ * ] as const
14
+ * ```
15
+ */
16
+ export interface ExtendedModelDef<TName extends string = string, TInput extends ReadonlyArray<Modality> = ReadonlyArray<Modality>, TOptions = unknown> {
17
+ /** The model name identifier */
18
+ name: TName;
19
+ /** Supported input modalities for this model */
20
+ input: TInput;
21
+ /** Type brand for provider options - use `{} as YourOptionsType` */
22
+ modelOptions: TOptions;
23
+ }
24
+ /**
25
+ * Creates a custom model definition for use with `extendAdapter`.
26
+ *
27
+ * This is a helper function that provides proper type inference without
28
+ * requiring manual `as const` casts on individual properties.
29
+ *
30
+ * @template TName - The model name (inferred from argument)
31
+ * @template TInput - The input modalities array (inferred from argument)
32
+ *
33
+ * @param name - The model name identifier (literal string)
34
+ * @param input - Array of supported input modalities
35
+ * @returns A properly typed model definition for use with `extendAdapter`
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * import { extendAdapter, createModel } from '@tanstack/ai'
40
+ * import { openaiText } from '@tanstack/ai-openai'
41
+ *
42
+ * // Define custom models with full type inference
43
+ * const customModels = [
44
+ * createModel('my-fine-tuned-gpt4', ['text', 'image']),
45
+ * createModel('local-llama', ['text']),
46
+ * ] as const
47
+ *
48
+ * const myOpenai = extendAdapter(openaiText, customModels)
49
+ * ```
50
+ */
51
+ export declare function createModel<const TName extends string, const TInput extends ReadonlyArray<Modality>>(name: TName, input: TInput): ExtendedModelDef<TName, TInput>;
52
+ /**
53
+ * Extract the model name union from an array of model definitions.
54
+ */
55
+ type ExtractCustomModelNames<TDefs extends ReadonlyArray<ExtendedModelDef>> = TDefs[number]['name'];
56
+ /**
57
+ * Infer the model parameter type from an adapter factory function.
58
+ * For generic functions like `<T extends Union>(model: T)`, this gets `T` which
59
+ * TypeScript treats as the constraint union when used in parameter position.
60
+ */
61
+ type InferFactoryModels<TFactory> = TFactory extends (model: infer TModel, ...args: Array<any>) => any ? TModel extends string ? TModel : string : string;
62
+ /**
63
+ * Infer the config parameter type from an adapter factory function.
64
+ */
65
+ type InferConfig<TFactory> = TFactory extends (model: any, config?: infer TConfig) => any ? TConfig : undefined;
66
+ /**
67
+ * Infer the adapter return type from a factory function.
68
+ */
69
+ type InferAdapterReturn<TFactory> = TFactory extends (...args: Array<any>) => infer TReturn ? TReturn : never;
70
+ /**
71
+ * Extends an existing adapter factory with additional custom models.
72
+ *
73
+ * The extended adapter accepts both original models (with full original type inference)
74
+ * and custom models (with types from your definitions).
75
+ *
76
+ * At runtime, this simply passes through to the original factory - no validation is performed.
77
+ * The original factory's signature is fully preserved, including any config parameters.
78
+ *
79
+ * @param factory - The original adapter factory function (e.g., `openaiText`, `anthropicText`)
80
+ * @param models - Array of custom model definitions with `name` and `input`
81
+ * @returns A new factory function that accepts both original and custom models
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * import { extendAdapter, createModel } from '@tanstack/ai'
86
+ * import { openaiText } from '@tanstack/ai-openai'
87
+ *
88
+ * // Define custom models
89
+ * const customModels = [
90
+ * createModel('my-fine-tuned-gpt4', ['text', 'image']),
91
+ * createModel('local-llama', ['text']),
92
+ * ] as const
93
+ *
94
+ * // Create extended adapter
95
+ * const myOpenai = extendAdapter(openaiText, customModels)
96
+ *
97
+ * // Use with original models - full type inference preserved
98
+ * const gpt4 = myOpenai('gpt-4o')
99
+ *
100
+ * // Use with custom models
101
+ * const custom = myOpenai('my-fine-tuned-gpt4')
102
+ *
103
+ * // Type error: 'invalid-model' is not a valid model
104
+ * // myOpenai('invalid-model')
105
+ *
106
+ * // Works with chat()
107
+ * chat({
108
+ * adapter: myOpenai('my-fine-tuned-gpt4'),
109
+ * messages: [...]
110
+ * })
111
+ * ```
112
+ */
113
+ export declare function extendAdapter<TFactory extends (...args: Array<any>) => any, const TDefs extends ReadonlyArray<ExtendedModelDef>>(factory: TFactory, _customModels: TDefs): (model: InferFactoryModels<TFactory> | ExtractCustomModelNames<TDefs>, ...args: InferConfig<TFactory> extends undefined ? [] : [config?: InferConfig<TFactory>]) => InferAdapterReturn<TFactory>;
114
+ export {};
@@ -0,0 +1,15 @@
1
+ function createModel(name, input) {
2
+ return {
3
+ name,
4
+ input,
5
+ modelOptions: {}
6
+ };
7
+ }
8
+ function extendAdapter(factory, _customModels) {
9
+ return factory;
10
+ }
11
+ export {
12
+ createModel,
13
+ extendAdapter
14
+ };
15
+ //# sourceMappingURL=extend-adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extend-adapter.js","sources":["../../src/extend-adapter.ts"],"sourcesContent":["import type { Modality } from './types'\n\n// ===========================\n// Extended Model Definition\n// ===========================\n\n/**\n * Definition for a custom model to add to an adapter.\n *\n * @template TName - The model name as a literal string type\n * @template TInput - Array of supported input modalities\n * @template TOptions - Provider options type for this model\n *\n * @example\n * ```typescript\n * const customModels = [\n * createModel('my-custom-model', ['text', 'image']),\n * ] as const\n * ```\n */\nexport interface ExtendedModelDef<\n TName extends string = string,\n TInput extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,\n TOptions = unknown,\n> {\n /** The model name identifier */\n name: TName\n /** Supported input modalities for this model */\n input: TInput\n /** Type brand for provider options - use `{} as YourOptionsType` */\n modelOptions: TOptions\n}\n\n/**\n * Creates a custom model definition for use with `extendAdapter`.\n *\n * This is a helper function that provides proper type inference without\n * requiring manual `as const` casts on individual properties.\n *\n * @template TName - The model name (inferred from argument)\n * @template TInput - The input modalities array (inferred from argument)\n *\n * @param name - The model name identifier (literal string)\n * @param input - Array of supported input modalities\n * @returns A properly typed model definition for use with `extendAdapter`\n *\n * @example\n * ```typescript\n * import { extendAdapter, createModel } from '@tanstack/ai'\n * import { openaiText } from '@tanstack/ai-openai'\n *\n * // Define custom models with full type inference\n * const customModels = [\n * createModel('my-fine-tuned-gpt4', ['text', 'image']),\n * createModel('local-llama', ['text']),\n * ] as const\n *\n * const myOpenai = extendAdapter(openaiText, customModels)\n * ```\n */\nexport function createModel<\n const TName extends string,\n const TInput extends ReadonlyArray<Modality>,\n>(name: TName, input: TInput): ExtendedModelDef<TName, TInput> {\n return {\n name,\n input,\n modelOptions: {} as unknown,\n }\n}\n\n// ===========================\n// Type Extraction Utilities\n// ===========================\n\n/**\n * Extract the model name union from an array of model definitions.\n */\ntype ExtractCustomModelNames<TDefs extends ReadonlyArray<ExtendedModelDef>> =\n TDefs[number]['name']\n\n// ===========================\n// Factory Type Inference\n// ===========================\n\n/**\n * Infer the model parameter type from an adapter factory function.\n * For generic functions like `<T extends Union>(model: T)`, this gets `T` which\n * TypeScript treats as the constraint union when used in parameter position.\n */\ntype InferFactoryModels<TFactory> = TFactory extends (\n model: infer TModel,\n ...args: Array<any>\n) => any\n ? TModel extends string\n ? TModel\n : string\n : string\n\n/**\n * Infer the config parameter type from an adapter factory function.\n */\ntype InferConfig<TFactory> = TFactory extends (\n model: any,\n config?: infer TConfig,\n) => any\n ? TConfig\n : undefined\n\n/**\n * Infer the adapter return type from a factory function.\n */\ntype InferAdapterReturn<TFactory> = TFactory extends (\n ...args: Array<any>\n) => infer TReturn\n ? TReturn\n : never\n\n// ===========================\n// extendAdapter Function\n// ===========================\n\n/**\n * Extends an existing adapter factory with additional custom models.\n *\n * The extended adapter accepts both original models (with full original type inference)\n * and custom models (with types from your definitions).\n *\n * At runtime, this simply passes through to the original factory - no validation is performed.\n * The original factory's signature is fully preserved, including any config parameters.\n *\n * @param factory - The original adapter factory function (e.g., `openaiText`, `anthropicText`)\n * @param models - Array of custom model definitions with `name` and `input`\n * @returns A new factory function that accepts both original and custom models\n *\n * @example\n * ```typescript\n * import { extendAdapter, createModel } from '@tanstack/ai'\n * import { openaiText } from '@tanstack/ai-openai'\n *\n * // Define custom models\n * const customModels = [\n * createModel('my-fine-tuned-gpt4', ['text', 'image']),\n * createModel('local-llama', ['text']),\n * ] as const\n *\n * // Create extended adapter\n * const myOpenai = extendAdapter(openaiText, customModels)\n *\n * // Use with original models - full type inference preserved\n * const gpt4 = myOpenai('gpt-4o')\n *\n * // Use with custom models\n * const custom = myOpenai('my-fine-tuned-gpt4')\n *\n * // Type error: 'invalid-model' is not a valid model\n * // myOpenai('invalid-model')\n *\n * // Works with chat()\n * chat({\n * adapter: myOpenai('my-fine-tuned-gpt4'),\n * messages: [...]\n * })\n * ```\n */\nexport function extendAdapter<\n TFactory extends (...args: Array<any>) => any,\n const TDefs extends ReadonlyArray<ExtendedModelDef>,\n>(\n factory: TFactory,\n _customModels: TDefs,\n): (\n model: InferFactoryModels<TFactory> | ExtractCustomModelNames<TDefs>,\n ...args: InferConfig<TFactory> extends undefined\n ? []\n : [config?: InferConfig<TFactory>]\n) => InferAdapterReturn<TFactory> {\n // At runtime, we simply pass through to the original factory.\n // The _customModels parameter is only used for type inference.\n // No runtime validation - users are trusted to pass valid model names.\n return factory as any\n}\n"],"names":[],"mappings":"AA4DO,SAAS,YAGd,MAAa,OAAgD;AAC7D,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,cAAc,CAAA;AAAA,EAAC;AAEnB;AAgGO,SAAS,cAId,SACA,eAMgC;AAIhC,SAAO;AACT;"}
@@ -17,3 +17,5 @@ export * from './event-client.js';
17
17
  export { convertMessagesToModelMessages, generateMessageId, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, } from './activities/chat/messages.js';
18
18
  export { StreamProcessor, createReplayStream, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, PartialJSONParser, defaultJSONParser, parsePartialJSON, } from './activities/chat/stream/index.js';
19
19
  export type { ChunkStrategy, ChunkRecording, InternalToolCallState, ProcessorResult, ProcessorState, StreamProcessorEvents, StreamProcessorOptions, ToolCallState, ToolResultState, JSONParser, } from './activities/chat/stream/index.js';
20
+ export { createModel, extendAdapter } from './extend-adapter.js';
21
+ export type { ExtendedModelDef } from './extend-adapter.js';
package/dist/esm/index.js CHANGED
@@ -12,6 +12,7 @@ import { combineStrategies, maxIterations, untilFinishReason } from "./activitie
12
12
  import { detectImageMimeType } from "./utils.js";
13
13
  import { aiEventClient } from "./event-client.js";
14
14
  import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./activities/chat/messages.js";
15
+ import { createModel, extendAdapter } from "./extend-adapter.js";
15
16
  import { StreamProcessor, createReplayStream } from "./activities/chat/stream/processor.js";
16
17
  import { BatchStrategy, CompositeStrategy, ImmediateStrategy, PunctuationStrategy, WordBoundaryStrategy } from "./activities/chat/stream/strategies.js";
17
18
  import { PartialJSONParser, defaultJSONParser, parsePartialJSON } from "./activities/chat/stream/json-parser.js";
@@ -31,6 +32,7 @@ export {
31
32
  convertSchemaToJsonSchema,
32
33
  createChatOptions,
33
34
  createImageOptions,
35
+ createModel,
34
36
  createReplayStream,
35
37
  createSpeechOptions,
36
38
  createSummarizeOptions,
@@ -38,6 +40,7 @@ export {
38
40
  createVideoOptions,
39
41
  defaultJSONParser,
40
42
  detectImageMimeType,
43
+ extendAdapter,
41
44
  generateImage,
42
45
  generateMessageId,
43
46
  generateSpeech,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
@@ -793,7 +793,7 @@ export interface SummarizationResult {
793
793
  * Options for image generation.
794
794
  * These are the common options supported across providers.
795
795
  */
796
- export interface ImageGenerationOptions<TProviderOptions extends object = object> {
796
+ export interface ImageGenerationOptions<TProviderOptions extends object = object, TSize extends string = string> {
797
797
  /** The model to use for image generation */
798
798
  model: string;
799
799
  /** Text description of the desired image(s) */
@@ -801,7 +801,7 @@ export interface ImageGenerationOptions<TProviderOptions extends object = object
801
801
  /** Number of images to generate (default: 1) */
802
802
  numberOfImages?: number;
803
803
  /** Image size in WIDTHxHEIGHT format (e.g., "1024x1024") */
804
- size?: string;
804
+ size?: TSize;
805
805
  /** Model-specific options for image generation */
806
806
  modelOptions?: TProviderOptions;
807
807
  }
@@ -839,13 +839,13 @@ export interface ImageGenerationResult {
839
839
  *
840
840
  * @experimental Video generation is an experimental feature and may change.
841
841
  */
842
- export interface VideoGenerationOptions<TProviderOptions extends object = object> {
842
+ export interface VideoGenerationOptions<TProviderOptions extends object = object, TSize extends string = string> {
843
843
  /** The model to use for video generation */
844
844
  model: string;
845
845
  /** Text description of the desired video */
846
846
  prompt: string;
847
- /** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
848
- size?: string;
847
+ /** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
848
+ size?: TSize;
849
849
  /** Video duration in seconds */
850
850
  duration?: number;
851
851
  /** Model-specific options for video generation */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Core TanStack AI library - Open source AI SDK",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -1,5 +1,12 @@
1
1
  import type { ImageGenerationOptions, ImageGenerationResult } from '../../types'
2
2
 
3
+ /**
4
+ * Resolve the size type for a model from the model-size map.
5
+ * If the map has an index signature (i.e. no explicit keys), falls back to string.
6
+ * If the model is an explicit key, uses its mapped size type.
7
+ * Otherwise falls back to string.
8
+ */
9
+
3
10
  /**
4
11
  * Configuration for image adapter instances
5
12
  */
@@ -49,7 +56,7 @@ export interface ImageAdapter<
49
56
  * Generate images from a prompt
50
57
  */
51
58
  generateImages: (
52
- options: ImageGenerationOptions<TProviderOptions>,
59
+ options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,
53
60
  ) => Promise<ImageGenerationResult>
54
61
  }
55
62
 
@@ -95,7 +102,7 @@ export abstract class BaseImageAdapter<
95
102
  }
96
103
 
97
104
  abstract generateImages(
98
- options: ImageGenerationOptions<TProviderOptions>,
105
+ options: ImageGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,
99
106
  ): Promise<ImageGenerationResult>
100
107
 
101
108
  protected generateId(): string {
@@ -61,9 +61,9 @@ export type ImageSizeForModel<TAdapter, TModel extends string> =
61
61
  *
62
62
  * @template TAdapter - The image adapter type
63
63
  */
64
- export interface ImageActivityOptions<
65
- TAdapter extends ImageAdapter<string, object, any, any>,
66
- > {
64
+ export type ImageActivityOptions<
65
+ TAdapter extends ImageAdapter<string, any, any, any>,
66
+ > = {
67
67
  /** The image adapter to use (must be created with a model) */
68
68
  adapter: TAdapter & { kind: typeof kind }
69
69
  /** Text description of the desired image(s) */
@@ -72,9 +72,19 @@ export interface ImageActivityOptions<
72
72
  numberOfImages?: number
73
73
  /** Image size in WIDTHxHEIGHT format (e.g., "1024x1024") */
74
74
  size?: ImageSizeForModel<TAdapter, TAdapter['model']>
75
- /** Provider-specific options for image generation */
76
- modelOptions?: ImageProviderOptionsForModel<TAdapter, TAdapter['model']>
77
- }
75
+ } & ({} extends ImageProviderOptionsForModel<TAdapter, TAdapter['model']>
76
+ ? {
77
+ /** Provider-specific options for image generation */ modelOptions?: ImageProviderOptionsForModel<
78
+ TAdapter,
79
+ TAdapter['model']
80
+ >
81
+ }
82
+ : {
83
+ /** Provider-specific options for image generation */ modelOptions: ImageProviderOptionsForModel<
84
+ TAdapter,
85
+ TAdapter['model']
86
+ >
87
+ })
78
88
 
79
89
  // ===========================
80
90
  // Activity Result Type
@@ -137,7 +147,7 @@ function createId(prefix: string): string {
137
147
  * ```
138
148
  */
139
149
  export async function generateImage<
140
- TAdapter extends ImageAdapter<string, object, any, any>,
150
+ TAdapter extends ImageAdapter<string, any, any, any>,
141
151
  >(options: ImageActivityOptions<TAdapter>): ImageActivityResult {
142
152
  const { adapter, ...rest } = options
143
153
  const model = adapter.model
@@ -150,7 +160,7 @@ export async function generateImage<
150
160
  model,
151
161
  prompt: rest.prompt,
152
162
  numberOfImages: rest.numberOfImages,
153
- size: rest.size as string | undefined,
163
+ size: rest.size,
154
164
  modelOptions: rest.modelOptions as Record<string, unknown> | undefined,
155
165
  timestamp: startTime,
156
166
  })
@@ -193,7 +203,7 @@ export async function generateImage<
193
203
  * Create typed options for the generateImage() function without executing.
194
204
  */
195
205
  export function createImageOptions<
196
- TAdapter extends ImageAdapter<string, object, any, any>,
206
+ TAdapter extends ImageAdapter<string, any, any, any>,
197
207
  >(options: ImageActivityOptions<TAdapter>): ImageActivityOptions<TAdapter> {
198
208
  return options
199
209
  }
@@ -29,10 +29,14 @@ export interface VideoAdapterConfig {
29
29
  * Generic parameters:
30
30
  * - TModel: The specific model name (e.g., 'sora-2')
31
31
  * - TProviderOptions: Provider-specific options (already resolved)
32
+ * - TModelProviderOptionsByName: Map from model name to its specific provider options
33
+ * - TModelSizeByName: Map from model name to its supported sizes
32
34
  */
33
35
  export interface VideoAdapter<
34
36
  TModel extends string = string,
35
37
  TProviderOptions extends object = Record<string, unknown>,
38
+ TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
39
+ TModelSizeByName extends Record<string, string> = Record<string, string>,
36
40
  > {
37
41
  /** Discriminator for adapter kind - used to determine API shape */
38
42
  readonly kind: 'video'
@@ -46,6 +50,8 @@ export interface VideoAdapter<
46
50
  */
47
51
  '~types': {
48
52
  providerOptions: TProviderOptions
53
+ modelProviderOptionsByName: TModelProviderOptionsByName
54
+ modelSizeByName: TModelSizeByName
49
55
  }
50
56
 
51
57
  /**
@@ -53,7 +59,7 @@ export interface VideoAdapter<
53
59
  * Returns a job ID that can be used to poll for status and retrieve the video.
54
60
  */
55
61
  createVideoJob: (
56
- options: VideoGenerationOptions<TProviderOptions>,
62
+ options: VideoGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,
57
63
  ) => Promise<VideoJobResult>
58
64
 
59
65
  /**
@@ -72,7 +78,7 @@ export interface VideoAdapter<
72
78
  * A VideoAdapter with any/unknown type parameters.
73
79
  * Useful as a constraint in generic functions and interfaces.
74
80
  */
75
- export type AnyVideoAdapter = VideoAdapter<any, any>
81
+ export type AnyVideoAdapter = VideoAdapter<any, any, any, any>
76
82
 
77
83
  /**
78
84
  * Abstract base class for video generation adapters.
@@ -85,7 +91,14 @@ export type AnyVideoAdapter = VideoAdapter<any, any>
85
91
  export abstract class BaseVideoAdapter<
86
92
  TModel extends string = string,
87
93
  TProviderOptions extends object = Record<string, unknown>,
88
- > implements VideoAdapter<TModel, TProviderOptions> {
94
+ TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
95
+ TModelSizeByName extends Record<string, string> = Record<string, string>,
96
+ > implements VideoAdapter<
97
+ TModel,
98
+ TProviderOptions,
99
+ TModelProviderOptionsByName,
100
+ TModelSizeByName
101
+ > {
89
102
  readonly kind = 'video' as const
90
103
  abstract readonly name: string
91
104
  readonly model: TModel
@@ -93,6 +106,8 @@ export abstract class BaseVideoAdapter<
93
106
  // Type-only property - never assigned at runtime
94
107
  declare '~types': {
95
108
  providerOptions: TProviderOptions
109
+ modelProviderOptionsByName: TModelProviderOptionsByName
110
+ modelSizeByName: TModelSizeByName
96
111
  }
97
112
 
98
113
  protected config: VideoAdapterConfig
@@ -103,7 +118,7 @@ export abstract class BaseVideoAdapter<
103
118
  }
104
119
 
105
120
  abstract createVideoJob(
106
- options: VideoGenerationOptions<TProviderOptions>,
121
+ options: VideoGenerationOptions<TProviderOptions, TModelSizeByName[TModel]>,
107
122
  ): Promise<VideoJobResult>
108
123
 
109
124
  abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>
@@ -30,10 +30,20 @@ export const kind = 'video' as const
30
30
  * Extract provider options from a VideoAdapter via ~types.
31
31
  */
32
32
  export type VideoProviderOptions<TAdapter> =
33
- TAdapter extends VideoAdapter<any, any>
33
+ TAdapter extends VideoAdapter<any, any, any, any>
34
34
  ? TAdapter['~types']['providerOptions']
35
35
  : object
36
36
 
37
+ /**
38
+ * Extract the size type for a VideoAdapter's model via ~types.
39
+ */
40
+ export type VideoSizeForAdapter<TAdapter> =
41
+ TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap>
42
+ ? TModel extends keyof TSizeMap
43
+ ? TSizeMap[TModel]
44
+ : string
45
+ : string
46
+
37
47
  // ===========================
38
48
  // Activity Options Types
39
49
 
@@ -47,7 +57,7 @@ function createId(prefix: string): string {
47
57
  * The model is extracted from the adapter's model property.
48
58
  */
49
59
  interface VideoActivityBaseOptions<
50
- TAdapter extends VideoAdapter<string, object>,
60
+ TAdapter extends VideoAdapter<string, any, any, any>,
51
61
  > {
52
62
  /** The video adapter to use (must be created with a model) */
53
63
  adapter: TAdapter & { kind: typeof kind }
@@ -59,20 +69,24 @@ interface VideoActivityBaseOptions<
59
69
  *
60
70
  * @experimental Video generation is an experimental feature and may change.
61
71
  */
62
- export interface VideoCreateOptions<
63
- TAdapter extends VideoAdapter<string, object>,
64
- > extends VideoActivityBaseOptions<TAdapter> {
72
+ export type VideoCreateOptions<
73
+ TAdapter extends VideoAdapter<string, any, any, any>,
74
+ > = VideoActivityBaseOptions<TAdapter> & {
65
75
  /** Request type - create a new job (default if not specified) */
66
76
  request?: 'create'
67
77
  /** Text description of the desired video */
68
78
  prompt: string
69
- /** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
70
- size?: string
79
+ /** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
80
+ size?: VideoSizeForAdapter<TAdapter>
71
81
  /** Video duration in seconds */
72
82
  duration?: number
73
- /** Provider-specific options for video generation */
74
- modelOptions?: VideoProviderOptions<TAdapter>
75
- }
83
+ } & ({} extends VideoProviderOptions<TAdapter>
84
+ ? {
85
+ /** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>
86
+ }
87
+ : {
88
+ /** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>
89
+ })
76
90
 
77
91
  /**
78
92
  * Options for polling the status of a video generation job.
@@ -80,7 +94,7 @@ export interface VideoCreateOptions<
80
94
  * @experimental Video generation is an experimental feature and may change.
81
95
  */
82
96
  export interface VideoStatusOptions<
83
- TAdapter extends VideoAdapter<string, object>,
97
+ TAdapter extends VideoAdapter<string, any, any, any>,
84
98
  > extends VideoActivityBaseOptions<TAdapter> {
85
99
  /** Request type - get job status */
86
100
  request: 'status'
@@ -94,7 +108,7 @@ export interface VideoStatusOptions<
94
108
  * @experimental Video generation is an experimental feature and may change.
95
109
  */
96
110
  export interface VideoUrlOptions<
97
- TAdapter extends VideoAdapter<string, object>,
111
+ TAdapter extends VideoAdapter<string, any, any, any>,
98
112
  > extends VideoActivityBaseOptions<TAdapter> {
99
113
  /** Request type - get video URL */
100
114
  request: 'url'
@@ -109,7 +123,7 @@ export interface VideoUrlOptions<
109
123
  * @experimental Video generation is an experimental feature and may change.
110
124
  */
111
125
  export type VideoActivityOptions<
112
- TAdapter extends VideoAdapter<string, object>,
126
+ TAdapter extends VideoAdapter<string, any, any, any>,
113
127
  TRequest extends 'create' | 'status' | 'url' = 'create',
114
128
  > = TRequest extends 'status'
115
129
  ? VideoStatusOptions<TAdapter>
@@ -161,7 +175,7 @@ export type VideoActivityResult<
161
175
  * ```
162
176
  */
163
177
  export async function generateVideo<
164
- TAdapter extends VideoAdapter<string, object>,
178
+ TAdapter extends VideoAdapter<string, any, any, any>,
165
179
  >(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult> {
166
180
  const { adapter, prompt, size, duration, modelOptions } = options
167
181
  const model = adapter.model
@@ -201,7 +215,7 @@ export async function generateVideo<
201
215
  * ```
202
216
  */
203
217
  export async function getVideoJobStatus<
204
- TAdapter extends VideoAdapter<string, object>,
218
+ TAdapter extends VideoAdapter<string, any, any, any>,
205
219
  >(options: {
206
220
  adapter: TAdapter & { kind: typeof kind }
207
221
  jobId: string
@@ -301,7 +315,7 @@ export async function getVideoJobStatus<
301
315
  * Create typed options for the generateVideo() function without executing.
302
316
  */
303
317
  export function createVideoOptions<
304
- TAdapter extends VideoAdapter<string, object>,
318
+ TAdapter extends VideoAdapter<string, any, any, any>,
305
319
  >(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter> {
306
320
  return options
307
321
  }
@@ -0,0 +1,182 @@
1
+ import type { Modality } from './types'
2
+
3
+ // ===========================
4
+ // Extended Model Definition
5
+ // ===========================
6
+
7
+ /**
8
+ * Definition for a custom model to add to an adapter.
9
+ *
10
+ * @template TName - The model name as a literal string type
11
+ * @template TInput - Array of supported input modalities
12
+ * @template TOptions - Provider options type for this model
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const customModels = [
17
+ * createModel('my-custom-model', ['text', 'image']),
18
+ * ] as const
19
+ * ```
20
+ */
21
+ export interface ExtendedModelDef<
22
+ TName extends string = string,
23
+ TInput extends ReadonlyArray<Modality> = ReadonlyArray<Modality>,
24
+ TOptions = unknown,
25
+ > {
26
+ /** The model name identifier */
27
+ name: TName
28
+ /** Supported input modalities for this model */
29
+ input: TInput
30
+ /** Type brand for provider options - use `{} as YourOptionsType` */
31
+ modelOptions: TOptions
32
+ }
33
+
34
+ /**
35
+ * Creates a custom model definition for use with `extendAdapter`.
36
+ *
37
+ * This is a helper function that provides proper type inference without
38
+ * requiring manual `as const` casts on individual properties.
39
+ *
40
+ * @template TName - The model name (inferred from argument)
41
+ * @template TInput - The input modalities array (inferred from argument)
42
+ *
43
+ * @param name - The model name identifier (literal string)
44
+ * @param input - Array of supported input modalities
45
+ * @returns A properly typed model definition for use with `extendAdapter`
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * import { extendAdapter, createModel } from '@tanstack/ai'
50
+ * import { openaiText } from '@tanstack/ai-openai'
51
+ *
52
+ * // Define custom models with full type inference
53
+ * const customModels = [
54
+ * createModel('my-fine-tuned-gpt4', ['text', 'image']),
55
+ * createModel('local-llama', ['text']),
56
+ * ] as const
57
+ *
58
+ * const myOpenai = extendAdapter(openaiText, customModels)
59
+ * ```
60
+ */
61
+ export function createModel<
62
+ const TName extends string,
63
+ const TInput extends ReadonlyArray<Modality>,
64
+ >(name: TName, input: TInput): ExtendedModelDef<TName, TInput> {
65
+ return {
66
+ name,
67
+ input,
68
+ modelOptions: {} as unknown,
69
+ }
70
+ }
71
+
72
+ // ===========================
73
+ // Type Extraction Utilities
74
+ // ===========================
75
+
76
+ /**
77
+ * Extract the model name union from an array of model definitions.
78
+ */
79
+ type ExtractCustomModelNames<TDefs extends ReadonlyArray<ExtendedModelDef>> =
80
+ TDefs[number]['name']
81
+
82
+ // ===========================
83
+ // Factory Type Inference
84
+ // ===========================
85
+
86
+ /**
87
+ * Infer the model parameter type from an adapter factory function.
88
+ * For generic functions like `<T extends Union>(model: T)`, this gets `T` which
89
+ * TypeScript treats as the constraint union when used in parameter position.
90
+ */
91
+ type InferFactoryModels<TFactory> = TFactory extends (
92
+ model: infer TModel,
93
+ ...args: Array<any>
94
+ ) => any
95
+ ? TModel extends string
96
+ ? TModel
97
+ : string
98
+ : string
99
+
100
+ /**
101
+ * Infer the config parameter type from an adapter factory function.
102
+ */
103
+ type InferConfig<TFactory> = TFactory extends (
104
+ model: any,
105
+ config?: infer TConfig,
106
+ ) => any
107
+ ? TConfig
108
+ : undefined
109
+
110
+ /**
111
+ * Infer the adapter return type from a factory function.
112
+ */
113
+ type InferAdapterReturn<TFactory> = TFactory extends (
114
+ ...args: Array<any>
115
+ ) => infer TReturn
116
+ ? TReturn
117
+ : never
118
+
119
+ // ===========================
120
+ // extendAdapter Function
121
+ // ===========================
122
+
123
+ /**
124
+ * Extends an existing adapter factory with additional custom models.
125
+ *
126
+ * The extended adapter accepts both original models (with full original type inference)
127
+ * and custom models (with types from your definitions).
128
+ *
129
+ * At runtime, this simply passes through to the original factory - no validation is performed.
130
+ * The original factory's signature is fully preserved, including any config parameters.
131
+ *
132
+ * @param factory - The original adapter factory function (e.g., `openaiText`, `anthropicText`)
133
+ * @param models - Array of custom model definitions with `name` and `input`
134
+ * @returns A new factory function that accepts both original and custom models
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * import { extendAdapter, createModel } from '@tanstack/ai'
139
+ * import { openaiText } from '@tanstack/ai-openai'
140
+ *
141
+ * // Define custom models
142
+ * const customModels = [
143
+ * createModel('my-fine-tuned-gpt4', ['text', 'image']),
144
+ * createModel('local-llama', ['text']),
145
+ * ] as const
146
+ *
147
+ * // Create extended adapter
148
+ * const myOpenai = extendAdapter(openaiText, customModels)
149
+ *
150
+ * // Use with original models - full type inference preserved
151
+ * const gpt4 = myOpenai('gpt-4o')
152
+ *
153
+ * // Use with custom models
154
+ * const custom = myOpenai('my-fine-tuned-gpt4')
155
+ *
156
+ * // Type error: 'invalid-model' is not a valid model
157
+ * // myOpenai('invalid-model')
158
+ *
159
+ * // Works with chat()
160
+ * chat({
161
+ * adapter: myOpenai('my-fine-tuned-gpt4'),
162
+ * messages: [...]
163
+ * })
164
+ * ```
165
+ */
166
+ export function extendAdapter<
167
+ TFactory extends (...args: Array<any>) => any,
168
+ const TDefs extends ReadonlyArray<ExtendedModelDef>,
169
+ >(
170
+ factory: TFactory,
171
+ _customModels: TDefs,
172
+ ): (
173
+ model: InferFactoryModels<TFactory> | ExtractCustomModelNames<TDefs>,
174
+ ...args: InferConfig<TFactory> extends undefined
175
+ ? []
176
+ : [config?: InferConfig<TFactory>]
177
+ ) => InferAdapterReturn<TFactory> {
178
+ // At runtime, we simply pass through to the original factory.
179
+ // The _customModels parameter is only used for type inference.
180
+ // No runtime validation - users are trusted to pass valid model names.
181
+ return factory as any
182
+ }
package/src/index.ts CHANGED
@@ -114,3 +114,7 @@ export type {
114
114
  ToolResultState,
115
115
  JSONParser,
116
116
  } from './activities/chat/stream/index'
117
+
118
+ // Adapter extension utilities
119
+ export { createModel, extendAdapter } from './extend-adapter'
120
+ export type { ExtendedModelDef } from './extend-adapter'
package/src/types.ts CHANGED
@@ -967,6 +967,7 @@ export interface SummarizationResult {
967
967
  */
968
968
  export interface ImageGenerationOptions<
969
969
  TProviderOptions extends object = object,
970
+ TSize extends string = string,
970
971
  > {
971
972
  /** The model to use for image generation */
972
973
  model: string
@@ -975,7 +976,7 @@ export interface ImageGenerationOptions<
975
976
  /** Number of images to generate (default: 1) */
976
977
  numberOfImages?: number
977
978
  /** Image size in WIDTHxHEIGHT format (e.g., "1024x1024") */
978
- size?: string
979
+ size?: TSize
979
980
  /** Model-specific options for image generation */
980
981
  modelOptions?: TProviderOptions
981
982
  }
@@ -1022,13 +1023,14 @@ export interface ImageGenerationResult {
1022
1023
  */
1023
1024
  export interface VideoGenerationOptions<
1024
1025
  TProviderOptions extends object = object,
1026
+ TSize extends string = string,
1025
1027
  > {
1026
1028
  /** The model to use for video generation */
1027
1029
  model: string
1028
1030
  /** Text description of the desired video */
1029
1031
  prompt: string
1030
- /** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
1031
- size?: string
1032
+ /** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
1033
+ size?: TSize
1032
1034
  /** Video duration in seconds */
1033
1035
  duration?: number
1034
1036
  /** Model-specific options for video generation */