Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

extendAdapter.md

77 lines1.9 KB

1---
2id: extendAdapter
3title: extendAdapter
4---
5​
6```ts
7function extendAdapter<TFactory, TDefs>(factory, _customModels): ExtendedFactory<TFactory, TDefs>;
8```
9​
10Defined in: [packages/ai/src/extend-adapter.ts:247](https://github.com/TanStack/ai/blob/main/packages/ai/src/extend-adapter.ts#L247)
11​
12Extends an existing adapter factory with additional custom models.
13​
14The extended adapter accepts both original models (with full original type inference)
15and custom models (with types from your definitions).
16​
17At runtime, this simply passes through to the original factory - no validation is performed.
18The original factory's signature is fully preserved, including any config parameters.
19​
20## Type Parameters
21​
22### TFactory
23​
24`TFactory` *extends* `AnyAdapterFactory`
25​
26### TDefs
27​
28`TDefs` *extends* readonly [`ExtendedModelDef`](../interfaces/ExtendedModelDef.md)\<`string`, readonly (`"text"` \| `"image"` \| `"audio"` \| `"video"` \| `"document"`)[], `unknown`, readonly `string`[], readonly `string`[]\>[]
29​
30## Parameters
31​
32### factory
33​
34`TFactory`
35​
36The original adapter factory function (e.g., `openaiText`, `anthropicText`)
37​
38### \_customModels
39​
40`TDefs`
41​
42## Returns
43​
44`ExtendedFactory`\<`TFactory`, `TDefs`\>
45​
46A new factory function that accepts both original and custom models
47​
48## Example
49​
50```typescript
51import { extendAdapter, createModel } from '@tanstack/ai'
52import { openaiText } from '@tanstack/ai-openai'
53​
54// Define custom models
55const customModels = [
56 createModel('my-fine-tuned-gpt4', ['text', 'image']),
57 createModel('local-llama', ['text']),
58] as const
59​
60// Create extended adapter
61const myOpenai = extendAdapter(openaiText, customModels)
62​
63// Use with original models - full type inference preserved
64const gpt4 = myOpenai('gpt-4o')
65​
66// Use with custom models
67const custom = myOpenai('my-fine-tuned-gpt4')
68​
69// Type error: 'invalid-model' is not a valid model
70// myOpenai('invalid-model')
71​
72// Works with chat()
73chat({
74 adapter: myOpenai('my-fine-tuned-gpt4'),
75 messages: [...]
76})
77```