@@ -186,7 +186,7 @@ export class LDAIClientImpl implements LDAIClient {
186186 defaultValue : LDAIDefaults ,
187187 variables ?: Record < string , unknown > ,
188188 ) : Promise < LDAIConfig > {
189- this . _ldClient . track ( '$ld:ai:config:function:single ' , context , key , 1 ) ;
189+ this . _ldClient . track ( '$ld:ai:generation ' , this . _sanitizeContext ( context ) , key , 1 ) ;
190190
191191 const { tracker, enabled, model, provider, messages } = await this . _evaluate (
192192 key ,
@@ -207,7 +207,7 @@ export class LDAIClientImpl implements LDAIClient {
207207 config . provider = { ...provider } ;
208208 }
209209
210- const allVariables = { ... variables , ldctx : context } ;
210+ const allVariables = this . _createTemplateVariables ( context , variables ) ;
211211
212212 if ( messages ) {
213213 config . messages = messages . map ( ( entry : LDMessage ) => ( {
@@ -229,15 +229,15 @@ export class LDAIClientImpl implements LDAIClient {
229229 defaultValue : LDAIAgentDefaults ,
230230 variables ?: Record < string , unknown > ,
231231 ) : Promise < LDAIAgent > {
232- this . _ldClient . track ( '$ld:ai:agent:function:single' , context , key , 1 ) ;
232+ this . _ldClient . track ( '$ld:ai:agent:function:single' , this . _sanitizeContext ( context ) , key , 1 ) ;
233233
234234 const { tracker, enabled, model, provider, messages } = await this . _evaluate (
235235 key ,
236236 context ,
237237 defaultValue as any ,
238238 ) ;
239239
240- const allVariables = { ... variables , ldctx : context } ;
240+ const allVariables = this . _createTemplateVariables ( context , variables ) ;
241241 const instructionsRaw = ( defaultValue ?. instructions ?? '' ) as string ;
242242 const instructions = instructionsRaw
243243 ? this . _interpolateTemplate ( instructionsRaw , allVariables )
@@ -271,4 +271,66 @@ export class LDAIClientImpl implements LDAIClient {
271271 } ) ;
272272 return map as Record < TConfigs [ number ] [ 'key' ] , LDAIAgent > ;
273273 }
274+
275+ private _sanitizeContext ( context : LDContext ) : LDContext {
276+ if ( ! context || typeof context !== 'object' ) {
277+ return context ;
278+ }
279+
280+ const ctx = context as Record < string , unknown > ;
281+ const kind = typeof ctx . kind === 'string' ? ( ctx . kind as string ) : undefined ;
282+
283+ if ( kind && kind !== 'multi' ) {
284+ const sanitized : Record < string , unknown > = { kind } ;
285+ if ( typeof ctx . key === 'string' ) {
286+ sanitized . key = ctx . key ;
287+ }
288+ return sanitized as LDContext ;
289+ }
290+
291+ if ( kind === 'multi' ) {
292+ const sanitized : Record < string , unknown > = { kind : 'multi' } ;
293+ Object . keys ( ctx ) . forEach ( ( k ) => {
294+ if ( k === 'kind' ) {
295+ return ;
296+ }
297+ const value = ctx [ k ] ;
298+ if ( ! value || typeof value !== 'object' ) {
299+ return ;
300+ }
301+ const sub = value as Record < string , unknown > ;
302+ const subKind = typeof sub . kind === 'string' ? ( sub . kind as string ) : undefined ;
303+ if ( ! subKind ) {
304+ return ;
305+ }
306+ const sanitizedSub : Record < string , unknown > = { kind : subKind } ;
307+ if ( typeof sub . key === 'string' ) {
308+ sanitizedSub . key = sub . key ;
309+ }
310+ sanitized [ k ] = sanitizedSub ;
311+ } ) ;
312+ return sanitized as LDContext ;
313+ }
314+
315+ return context ;
316+ }
317+
318+ private _createTemplateVariables (
319+ context : LDContext ,
320+ variables ?: Record < string , unknown > ,
321+ ) : Record < string , unknown > {
322+ const contextVars : Record < string , unknown > = { } ;
323+ if ( context && typeof context === 'object' ) {
324+ Object . assign ( contextVars , context as Record < string , unknown > ) ;
325+ if ( 'name' in context ) {
326+ contextVars . username = ( context as any ) . name ;
327+ }
328+ }
329+
330+ return {
331+ ...contextVars ,
332+ ...variables ,
333+ ldctx : context ,
334+ } ;
335+ }
274336}
0 commit comments