From 7779a8546d11ab382ccc2a648050aa1afb538447 Mon Sep 17 00:00:00 2001 From: "15367279252qq.com" Date: Mon, 30 Mar 2026 01:04:56 +0800 Subject: [PATCH 1/2] fix(types): fix the type definitions for factory models and config, and add InferRestArgs utility type --- packages/typescript/ai/src/extend-adapter.ts | 26 +++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/typescript/ai/src/extend-adapter.ts b/packages/typescript/ai/src/extend-adapter.ts index 8547ebef4..0e2bb76a7 100644 --- a/packages/typescript/ai/src/extend-adapter.ts +++ b/packages/typescript/ai/src/extend-adapter.ts @@ -88,7 +88,7 @@ type ExtractCustomModelNames> = * For generic functions like `(model: T)`, this gets `T` which * TypeScript treats as the constraint union when used in parameter position. */ -type InferFactoryModels = TFactory extends ( +export type InferFactoryModels = TFactory extends ( model: infer TModel, ...args: Array ) => any @@ -100,7 +100,7 @@ type InferFactoryModels = TFactory extends ( /** * Infer the config parameter type from an adapter factory function. */ -type InferConfig = TFactory extends ( +export type InferConfig = TFactory extends ( model: any, config?: infer TConfig, ) => any @@ -116,6 +116,12 @@ type InferAdapterReturn = TFactory extends ( ? TReturn : never +/** + * Extracts all parameter types after the first parameter from a function. + */ +type InferRestArgs any> = + Parameters extends [any, ...infer Rest] ? Rest : [] + // =========================== // extendAdapter Function // =========================== @@ -164,19 +170,17 @@ type InferAdapterReturn = TFactory extends ( * ``` */ export function extendAdapter< - TFactory extends (...args: Array) => any, + TFactory extends (model: any, ...args: Array) => any, const TDefs extends ReadonlyArray, >( factory: TFactory, _customModels: TDefs, -): ( - model: InferFactoryModels | ExtractCustomModelNames, - ...args: InferConfig extends undefined - ? [] - : [config?: InferConfig] -) => InferAdapterReturn { +) { // At runtime, we simply pass through to the original factory. // The _customModels parameter is only used for type inference. // No runtime validation - users are trusted to pass valid model names. - return factory as any -} + return factory as unknown as ( + model: InferFactoryModels | ExtractCustomModelNames, + ...args: InferRestArgs + ) => InferAdapterReturn +} \ No newline at end of file From 0ccbcdf37a198fe4ce9e9cac64bfb7f17c536fb3 Mon Sep 17 00:00:00 2001 From: "15367279252qq.com" Date: Mon, 30 Mar 2026 01:12:35 +0800 Subject: [PATCH 2/2] refactor(types): simplify type definitions for factory models by removing unused InferConfig type --- packages/typescript/ai/src/extend-adapter.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/typescript/ai/src/extend-adapter.ts b/packages/typescript/ai/src/extend-adapter.ts index 0e2bb76a7..9e4f69b62 100644 --- a/packages/typescript/ai/src/extend-adapter.ts +++ b/packages/typescript/ai/src/extend-adapter.ts @@ -88,7 +88,7 @@ type ExtractCustomModelNames> = * For generic functions like `(model: T)`, this gets `T` which * TypeScript treats as the constraint union when used in parameter position. */ -export type InferFactoryModels = TFactory extends ( +type InferFactoryModels = TFactory extends ( model: infer TModel, ...args: Array ) => any @@ -97,15 +97,6 @@ export type InferFactoryModels = TFactory extends ( : string : string -/** - * Infer the config parameter type from an adapter factory function. - */ -export type InferConfig = TFactory extends ( - model: any, - config?: infer TConfig, -) => any - ? TConfig - : undefined /** * Infer the adapter return type from a factory function.