@tanstack/ai 0.4.2 → 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.
Files changed (37) hide show
  1. package/dist/esm/activities/chat/index.js +3 -4
  2. package/dist/esm/activities/chat/index.js.map +1 -1
  3. package/dist/esm/activities/chat/messages.d.ts +13 -6
  4. package/dist/esm/activities/chat/messages.js +115 -86
  5. package/dist/esm/activities/chat/messages.js.map +1 -1
  6. package/dist/esm/activities/chat/stream/processor.d.ts +143 -26
  7. package/dist/esm/activities/chat/stream/processor.js +205 -77
  8. package/dist/esm/activities/chat/stream/processor.js.map +1 -1
  9. package/dist/esm/activities/chat/tools/tool-calls.js +2 -3
  10. package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
  11. package/dist/esm/activities/generateImage/adapter.d.ts +8 -2
  12. package/dist/esm/activities/generateImage/adapter.js.map +1 -1
  13. package/dist/esm/activities/generateImage/index.d.ts +8 -6
  14. package/dist/esm/activities/generateImage/index.js.map +1 -1
  15. package/dist/esm/activities/generateVideo/adapter.d.ts +11 -5
  16. package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
  17. package/dist/esm/activities/generateVideo/index.d.ts +20 -14
  18. package/dist/esm/activities/generateVideo/index.js.map +1 -1
  19. package/dist/esm/extend-adapter.d.ts +114 -0
  20. package/dist/esm/extend-adapter.js +15 -0
  21. package/dist/esm/extend-adapter.js.map +1 -0
  22. package/dist/esm/index.d.ts +2 -0
  23. package/dist/esm/index.js +3 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/types.d.ts +10 -10
  26. package/package.json +1 -1
  27. package/src/activities/chat/index.ts +12 -9
  28. package/src/activities/chat/messages.ts +211 -138
  29. package/src/activities/chat/stream/processor.ts +240 -116
  30. package/src/activities/chat/tools/tool-calls.ts +1 -4
  31. package/src/activities/generateImage/adapter.ts +9 -2
  32. package/src/activities/generateImage/index.ts +19 -9
  33. package/src/activities/generateVideo/adapter.ts +19 -4
  34. package/src/activities/generateVideo/index.ts +30 -16
  35. package/src/extend-adapter.ts +182 -0
  36. package/src/index.ts +4 -0
  37. package/src/types.ts +10 -8
@@ -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
@@ -789,9 +789,9 @@ export interface TextMessageContentEvent extends BaseAGUIEvent {
789
789
  type: 'TEXT_MESSAGE_CONTENT'
790
790
  /** Message identifier */
791
791
  messageId: string
792
- /** The incremental content token (may be undefined if only content is provided) */
793
- delta?: string
794
- /** Full accumulated content so far */
792
+ /** The incremental content token */
793
+ delta: string
794
+ /** Full accumulated content so far (optional, for debugging) */
795
795
  content?: string
796
796
  }
797
797
 
@@ -864,8 +864,8 @@ export interface StepFinishedEvent extends BaseAGUIEvent {
864
864
  /** Step identifier */
865
865
  stepId: string
866
866
  /** Incremental thinking content */
867
- delta?: string
868
- /** Full accumulated thinking content */
867
+ delta: string
868
+ /** Full accumulated thinking content (optional, for debugging) */
869
869
  content?: string
870
870
  }
871
871
 
@@ -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 */