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