@@ -3,7 +3,6 @@ import type { ForestAdminHttpDriverServices } from './services';
33import type {
44 AgentOptions ,
55 AgentOptionsWithDefaults ,
6- AiConfiguration ,
76 HttpCallback ,
87} from './types' ;
98import type {
@@ -14,7 +13,7 @@ import type {
1413 TCollectionName ,
1514 TSchema ,
1615} from '@forestadmin/datasource-customizer' ;
17- import type { DataSource , DataSourceFactory } from '@forestadmin/datasource-toolkit' ;
16+ import type { AiProviderDefinition , DataSource , DataSourceFactory } from '@forestadmin/datasource-toolkit' ;
1817import type { ForestSchema } from '@forestadmin/forestadmin-client' ;
1918
2019import { DataSourceCustomizer } from '@forestadmin/datasource-customizer' ;
@@ -47,7 +46,7 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
4746 protected nocodeCustomizer : DataSourceCustomizer < S > ;
4847 protected customizationService : CustomizationService ;
4948 protected schemaGenerator : SchemaGenerator ;
50- protected aiConfigurations : AiConfiguration [ ] = [ ] ;
49+ protected aiProvider : AiProviderDefinition | null = null ;
5150
5251 /** Whether MCP server should be mounted */
5352 private mcpEnabled = false ;
@@ -222,42 +221,36 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
222221 * All AI requests from Forest Admin are forwarded to your agent and processed locally.
223222 * Your data and API keys never transit through Forest Admin servers, ensuring full privacy.
224223 *
225- * @param configuration - The AI provider configuration
226- * @param configuration.name - A unique name to identify this AI configuration
227- * @param configuration.provider - The AI provider to use ('openai')
228- * @param configuration.apiKey - Your API key for the chosen provider
229- * @param configuration.model - The model to use (e.g., 'gpt-4o')
224+ * @param provider - An AI provider definition created by a factory (e.g., createAiProvider)
230225 * @returns The agent instance for chaining
231226 * @throws Error if addAi is called more than once
232227 *
233228 * @example
234- * agent.addAi({
229+ * import { createAiProvider } from '@forestadmin/ai-proxy';
230+ *
231+ * agent.addAi(createAiProvider({
235232 * name: 'assistant',
236233 * provider: 'openai',
237234 * apiKey: process.env.OPENAI_API_KEY,
238235 * model: 'gpt-4o',
239- * });
236+ * })) ;
240237 */
241- addAi ( configuration : AiConfiguration ) : this {
242- if ( this . aiConfigurations . length > 0 ) {
238+ addAi ( provider : AiProviderDefinition ) : this {
239+ if ( this . aiProvider ) {
243240 throw new Error (
244241 'addAi can only be called once. Multiple AI configurations are not supported yet.' ,
245242 ) ;
246243 }
247244
248- this . options . logger (
249- 'Warn' ,
250- `AI configuration added with model '${ configuration . model } '. ` +
251- 'Make sure to test Forest Admin AI features thoroughly to ensure compatibility.' ,
252- ) ;
253-
254- this . aiConfigurations . push ( configuration ) ;
245+ this . aiProvider = provider ;
255246
256247 return this ;
257248 }
258249
259250 protected getRoutes ( dataSource : DataSource , services : ForestAdminHttpDriverServices ) {
260- return makeRoutes ( dataSource , this . options , services , this . aiConfigurations ) ;
251+ const aiRouter = this . aiProvider ?. init ( this . options . logger ) ?? null ;
252+
253+ return makeRoutes ( dataSource , this . options , services , aiRouter ) ;
261254 }
262255
263256 /**
@@ -380,9 +373,12 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
380373 let schema : Pick < ForestSchema , 'collections' > ;
381374
382375 // Get the AI configurations for schema metadata
376+ const aiMeta = this . aiProvider
377+ ? [ { name : this . aiProvider . name , provider : this . aiProvider . provider } ]
378+ : [ ] ;
383379 const { meta } = SchemaGenerator . buildMetadata (
384380 this . customizationService . buildFeatures ( ) ,
385- this . aiConfigurations ,
381+ aiMeta ,
386382 ) ;
387383
388384 // When using experimental no-code features even in production we need to build a new schema
0 commit comments