|
| 1 | +import { LDAIConfig, LDMessage } from '../config/LDAIConfig'; |
| 2 | +import { LDAIConfigTracker } from '../config/LDAIConfigTracker'; |
| 3 | +import { ChatResponse } from './TrackedChat'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Base implementation of TrackedChat that provides common functionality. |
| 7 | + * This can be extended by provider-specific implementations. |
| 8 | + */ |
| 9 | +export abstract class BaseTrackedChat { |
| 10 | + protected messages: LDMessage[]; |
| 11 | + |
| 12 | + constructor( |
| 13 | + protected readonly aiConfig: LDAIConfig, |
| 14 | + protected readonly tracker: LDAIConfigTracker, |
| 15 | + ) { |
| 16 | + this.messages = aiConfig.messages || []; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Invoke the chat model with a prompt string. |
| 21 | + * This method handles conversation management and tracking, delegating to the provider's invokeModel method. |
| 22 | + */ |
| 23 | + async invoke(prompt: string): Promise<ChatResponse> { |
| 24 | + // Convert prompt string to LDMessage with role 'user' and add to conversation history |
| 25 | + const userMessage: LDMessage = { |
| 26 | + role: 'user', |
| 27 | + content: prompt, |
| 28 | + }; |
| 29 | + this.messages.push(userMessage); |
| 30 | + |
| 31 | + // Delegate to provider-specific implementation with tracking |
| 32 | + const response = await this.trackMetricsOf(() => this.invokeModel(this.messages)); |
| 33 | + |
| 34 | + // Add the assistant response to the conversation history |
| 35 | + this.messages.push(response.message); |
| 36 | + |
| 37 | + return response; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Abstract method that providers must implement to handle the actual model invocation. |
| 42 | + * This method should convert messages to provider format, invoke the model, and return a ChatResponse. |
| 43 | + */ |
| 44 | + protected abstract invokeModel(messages: LDMessage[]): Promise<ChatResponse>; |
| 45 | + |
| 46 | + /** |
| 47 | + * Track metrics for a ChatResponse execution. |
| 48 | + * This method handles duration tracking, token usage tracking, and success/error tracking. |
| 49 | + */ |
| 50 | + protected async trackMetricsOf(callable: () => Promise<ChatResponse>): Promise<ChatResponse> { |
| 51 | + return this.tracker.trackDurationOf(async () => { |
| 52 | + try { |
| 53 | + const result = await callable(); |
| 54 | + |
| 55 | + // Track token usage if available |
| 56 | + if (result.usage) { |
| 57 | + this.tracker.trackTokens(result.usage); |
| 58 | + } |
| 59 | + |
| 60 | + this.tracker.trackSuccess(); |
| 61 | + return result; |
| 62 | + } catch (error) { |
| 63 | + this.tracker.trackError(); |
| 64 | + throw error; |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the underlying AI configuration used to initialize this TrackedChat. |
| 71 | + */ |
| 72 | + getConfig(): LDAIConfig { |
| 73 | + return this.aiConfig; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Get the underlying AI configuration tracker used to initialize this TrackedChat. |
| 78 | + */ |
| 79 | + getTracker(): LDAIConfigTracker { |
| 80 | + return this.tracker; |
| 81 | + } |
| 82 | +} |
0 commit comments