Skip to content

Commit eb54ec6

Browse files
committed
fix: update tracking keys and context handling in LDAIClient and LDAIConfigTracker implementations
1 parent 61f9300 commit eb54ec6

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

packages/sdk/cloudflare-ai/__tests__/LDAIClient.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('LDAIClient', () => {
7070
{ myVariable: 'My User Defined Variable' } as any,
7171
);
7272

73-
expect(config.messages?.[0].content).toBe('Hello Alice!');
73+
expect(config.messages?.[0].content).toBe('Hello Sandy!');
7474
});
7575

7676
it('tracks config usage', async () => {
@@ -88,8 +88,8 @@ describe('LDAIClient', () => {
8888
);
8989

9090
expect(mockLDClient.track).toHaveBeenCalledWith(
91-
'$ld:ai:config:function:single',
92-
{ kind: 'user', key: 'user-123' },
91+
'$ld:ai:generation',
92+
{ kind: 'user', key: 'example-user-key' },
9393
'test-config',
9494
1,
9595
);
@@ -157,7 +157,7 @@ describe('LDAIClient', () => {
157157
config.tracker.trackSuccess();
158158

159159
expect(mockLDClient.track).toHaveBeenCalledWith(
160-
'$ld:ai:generation',
160+
'$ld:ai:generation:success',
161161
{ kind: 'user', key: 'example-user-key', name: 'Sandy' },
162162
expect.objectContaining({
163163
aiConfigKey: 'test-config',

packages/sdk/cloudflare-ai/src/LDAIClientImpl.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/sdk/cloudflare-ai/src/LDAIConfigTrackerImpl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
183183

184184
private _createBaseMetadata(): Record<string, unknown> {
185185
return {
186-
configKey: this._configKey,
186+
aiConfigKey: this._configKey,
187187
variationKey: this._variationKey,
188188
version: this._version,
189-
modelName: this._modelName,
190-
providerName: this._providerName,
189+
model: this._modelName,
190+
provider: this._providerName,
191191
};
192192
}
193193
}

0 commit comments

Comments
 (0)