@tanstack/ai 0.5.0 → 0.6.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 (38) hide show
  1. package/README.md +1 -1
  2. package/dist/esm/activities/chat/index.js +47 -6
  3. package/dist/esm/activities/chat/index.js.map +1 -1
  4. package/dist/esm/activities/chat/stream/processor.d.ts +3 -0
  5. package/dist/esm/activities/chat/stream/processor.js +10 -4
  6. package/dist/esm/activities/chat/stream/processor.js.map +1 -1
  7. package/dist/esm/activities/chat/tools/tool-calls.d.ts +5 -3
  8. package/dist/esm/activities/chat/tools/tool-calls.js +57 -3
  9. package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
  10. package/dist/esm/activities/chat/tools/tool-definition.d.ts +2 -2
  11. package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -1
  12. package/dist/esm/activities/generateImage/adapter.d.ts +8 -2
  13. package/dist/esm/activities/generateImage/adapter.js.map +1 -1
  14. package/dist/esm/activities/generateImage/index.d.ts +8 -6
  15. package/dist/esm/activities/generateImage/index.js.map +1 -1
  16. package/dist/esm/activities/generateVideo/adapter.d.ts +11 -5
  17. package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
  18. package/dist/esm/activities/generateVideo/index.d.ts +20 -14
  19. package/dist/esm/activities/generateVideo/index.js.map +1 -1
  20. package/dist/esm/extend-adapter.d.ts +114 -0
  21. package/dist/esm/extend-adapter.js +15 -0
  22. package/dist/esm/extend-adapter.js.map +1 -0
  23. package/dist/esm/index.d.ts +2 -0
  24. package/dist/esm/index.js +3 -0
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/types.d.ts +35 -8
  27. package/package.json +1 -1
  28. package/src/activities/chat/index.ts +75 -4
  29. package/src/activities/chat/stream/processor.ts +22 -4
  30. package/src/activities/chat/tools/tool-calls.ts +88 -5
  31. package/src/activities/chat/tools/tool-definition.ts +3 -0
  32. package/src/activities/generateImage/adapter.ts +9 -2
  33. package/src/activities/generateImage/index.ts +19 -9
  34. package/src/activities/generateVideo/adapter.ts +19 -4
  35. package/src/activities/generateVideo/index.ts +30 -16
  36. package/src/extend-adapter.ts +182 -0
  37. package/src/index.ts +4 -0
  38. package/src/types.ts +36 -6
@@ -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
@@ -344,6 +344,34 @@ export type ConstrainedModelMessage<
344
344
  content: ConstrainedContent<TInputModalitiesTypes>
345
345
  }
346
346
 
347
+ /**
348
+ * Context passed to tool execute functions, providing capabilities like
349
+ * emitting custom events during execution.
350
+ */
351
+ export interface ToolExecutionContext {
352
+ /** The ID of the tool call being executed */
353
+ toolCallId?: string
354
+ /**
355
+ * Emit a custom event during tool execution.
356
+ * Events are streamed to the client in real-time as AG-UI CUSTOM events.
357
+ *
358
+ * @param eventName - Name of the custom event
359
+ * @param value - Event payload value
360
+ *
361
+ * @example
362
+ * ```ts
363
+ * const tool = toolDefinition({ ... }).server(async (args, context) => {
364
+ * context?.emitCustomEvent('progress', { step: 1, total: 3 })
365
+ * // ... do work ...
366
+ * context?.emitCustomEvent('progress', { step: 2, total: 3 })
367
+ * // ... do more work ...
368
+ * return result
369
+ * })
370
+ * ```
371
+ */
372
+ emitCustomEvent: (eventName: string, value: Record<string, any>) => void
373
+ }
374
+
347
375
  /**
348
376
  * Tool/Function definition for function calling.
349
377
  *
@@ -460,7 +488,7 @@ export interface Tool<
460
488
  * return weather; // Can return object or string
461
489
  * }
462
490
  */
463
- execute?: (args: any) => Promise<any> | any
491
+ execute?: (args: any, context?: ToolExecutionContext) => Promise<any> | any
464
492
 
465
493
  /** If true, tool execution requires user approval before running. Works with both server and client tools. */
466
494
  needsApproval?: boolean
@@ -894,8 +922,8 @@ export interface CustomEvent extends BaseAGUIEvent {
894
922
  type: 'CUSTOM'
895
923
  /** Custom event name */
896
924
  name: string
897
- /** Custom event data */
898
- data?: unknown
925
+ /** Custom event value */
926
+ value?: unknown
899
927
  }
900
928
 
901
929
  /**
@@ -967,6 +995,7 @@ export interface SummarizationResult {
967
995
  */
968
996
  export interface ImageGenerationOptions<
969
997
  TProviderOptions extends object = object,
998
+ TSize extends string = string,
970
999
  > {
971
1000
  /** The model to use for image generation */
972
1001
  model: string
@@ -975,7 +1004,7 @@ export interface ImageGenerationOptions<
975
1004
  /** Number of images to generate (default: 1) */
976
1005
  numberOfImages?: number
977
1006
  /** Image size in WIDTHxHEIGHT format (e.g., "1024x1024") */
978
- size?: string
1007
+ size?: TSize
979
1008
  /** Model-specific options for image generation */
980
1009
  modelOptions?: TProviderOptions
981
1010
  }
@@ -1022,13 +1051,14 @@ export interface ImageGenerationResult {
1022
1051
  */
1023
1052
  export interface VideoGenerationOptions<
1024
1053
  TProviderOptions extends object = object,
1054
+ TSize extends string = string,
1025
1055
  > {
1026
1056
  /** The model to use for video generation */
1027
1057
  model: string
1028
1058
  /** Text description of the desired video */
1029
1059
  prompt: string
1030
- /** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
1031
- size?: string
1060
+ /** Video size — format depends on the provider (e.g., "16:9", "1280x720") */
1061
+ size?: TSize
1032
1062
  /** Video duration in seconds */
1033
1063
  duration?: number
1034
1064
  /** Model-specific options for video generation */