Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions packages/sdk/server-ai/src/LDAIClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Mustache from 'mustache';
import { LDContext, LDLogger } from '@launchdarkly/js-server-sdk-common';

import { LDAIAgent, LDAIAgentConfig, LDAIAgentDefaults } from './api/agents';
import { SupportedAIProvider, TrackedChat, TrackedChatFactory } from './api/chat';
import { TrackedChat } from './api/chat';
import {
LDAIConfig,
LDAIConfigTracker,
Expand All @@ -16,6 +16,7 @@ import {
VercelAISDKProvider,
} from './api/config';
import { LDAIClient } from './api/LDAIClient';
import { AIProviderFactory, SupportedAIProvider } from './api/providers';
import { LDAIConfigMapper } from './LDAIConfigMapper';
import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl';
import { LDClientMin } from './LDClientMin';
Expand Down Expand Up @@ -246,7 +247,13 @@ export class LDAIClientImpl implements LDAIClient {
return undefined;
}

// Create the TrackedChat instance based on the provider
return TrackedChatFactory.create(aiConfig, aiConfig.tracker, this._logger, defaultAiProvider);
// Create the AIProvider instance
const provider = await AIProviderFactory.create(aiConfig, this._logger, defaultAiProvider);
if (!provider) {
return undefined;
}

// Create the TrackedChat instance with the provider
return new TrackedChat(aiConfig, aiConfig.tracker, provider);
}
}
3 changes: 2 additions & 1 deletion packages/sdk/server-ai/src/api/LDAIClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { LDContext } from '@launchdarkly/js-server-sdk-common';

import { LDAIAgent, LDAIAgentConfig, LDAIAgentDefaults } from './agents';
import { SupportedAIProvider, TrackedChat } from './chat';
import { TrackedChat } from './chat';
import { LDAIConfig, LDAIDefaults } from './config/LDAIConfig';
import { SupportedAIProvider } from './providers';

/**
* Interface for performing AI operations using LaunchDarkly.
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/server-ai/src/api/chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './types';
export * from './TrackedChat';
export * from './TrackedChatFactory';
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { LDLogger } from '@launchdarkly/js-server-sdk-common';

import { LDAIConfig } from '../config/LDAIConfig';
import { LDAIConfigTracker } from '../config/LDAIConfigTracker';
import { AIProvider } from '../providers/AIProvider';
import { TrackedChat } from './TrackedChat';
import { AIProvider } from './AIProvider';

/**
* List of supported AI providers.
Expand All @@ -21,41 +19,19 @@ export const SUPPORTED_AI_PROVIDERS = [
export type SupportedAIProvider = (typeof SUPPORTED_AI_PROVIDERS)[number];

/**
* Factory for creating TrackedChat instances based on the provider configuration.
* Factory for creating AIProvider instances based on the provider configuration.
*/
export class TrackedChatFactory {
export class AIProviderFactory {
/**
* Create a TrackedChat instance based on the AI configuration.
* Create an AIProvider instance based on the AI configuration.
* This method attempts to load provider-specific implementations dynamically.
* Returns undefined if the provider is not supported.
*
* @param aiConfig The AI configuration
* @param tracker The tracker for AI operations
* @param logger Optional logger for logging provider initialization
* @param defaultAiProvider Optional default AI provider to use
*/
static async create(
aiConfig: LDAIConfig,
tracker: LDAIConfigTracker,
logger?: LDLogger,
defaultAiProvider?: SupportedAIProvider,
): Promise<TrackedChat | undefined> {
const provider = await this._createAIProvider(aiConfig, logger, defaultAiProvider);
if (!provider) {
logger?.warn(
`Provider is not supported or failed to initialize: ${aiConfig.provider?.name ?? 'unknown'}`,
);
return undefined;
}

return new TrackedChat(aiConfig, tracker, provider);
}

/**
* Create an AIProvider instance based on the AI configuration.
* This method attempts to load provider-specific implementations dynamically.
*/
private static async _createAIProvider(
aiConfig: LDAIConfig,
logger?: LDLogger,
defaultAiProvider?: SupportedAIProvider,
Expand All @@ -74,6 +50,10 @@ export class TrackedChatFactory {
}
}

// If no provider was successfully created, log a warning
logger?.warn(
`Provider is not supported or failed to initialize: ${aiConfig.provider?.name ?? 'unknown'}`,
);
return undefined;
}

Expand Down
1 change: 1 addition & 0 deletions packages/sdk/server-ai/src/api/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './AIProvider';
export * from './AIProviderFactory';