@@ -269,168 +269,4 @@ describe("Test AI SDK Integration with Recording", function () {
269269 assert . ok ( outputMessages [ 0 ] . parts [ 0 ] . content ) ;
270270 assert . ok ( typeof outputMessages [ 0 ] . parts [ 0 ] . content === "string" ) ;
271271 } ) ;
272-
273- it ( "should capture and transform cache tokens from OpenAI with prompt caching" , async function ( ) {
274- this . timeout ( 30000 ) ;
275- const basePrompt =
276- "You are an expert AI assistant with comprehensive knowledge across numerous domains. " +
277- "Your responses should be accurate, detailed, and thoughtful. " +
278- "Always consider multiple perspectives and provide thorough explanations. " +
279- "\n\n" +
280- "## Guidelines for Different Domains:\n\n" +
281- "### Mathematics\n" +
282- "- Show all steps in your calculations\n" +
283- "- Explain the reasoning behind each step\n" +
284- "- Provide examples when helpful\n" +
285- "- Double-check your arithmetic\n" +
286- "\n" +
287- "### Science\n" +
288- "- Reference fundamental scientific principles\n" +
289- "- Cite recent discoveries when relevant\n" +
290- "- Explain complex concepts in accessible terms\n" +
291- "- Distinguish between established facts and theories\n" +
292- "\n" +
293- "### History\n" +
294- "- Consider broader historical context\n" +
295- "- Present multiple viewpoints\n" +
296- "- Acknowledge complexity and nuance\n" +
297- "- Connect historical events to modern implications\n" +
298- "\n" +
299- "### Literature\n" +
300- "- Analyze themes, symbolism, and motifs\n" +
301- "- Examine character development\n" +
302- "- Discuss literary devices and techniques\n" +
303- "- Place works in their cultural and historical context\n" +
304- "\n" +
305- "### Technology\n" +
306- "- Explain both practical applications and underlying concepts\n" +
307- "- Discuss benefits and potential drawbacks\n" +
308- "- Consider ethical implications\n" +
309- "- Keep up with current developments\n" +
310- "\n" +
311- "### Philosophy\n" +
312- "- Explore different schools of thought\n" +
313- "- Present arguments fairly and objectively\n" +
314- "- Examine ethical implications\n" +
315- "- Connect philosophical concepts to real-world scenarios\n" +
316- "\n" ;
317-
318- const longSystemPrompt = basePrompt . repeat ( 30 ) ;
319-
320- const result1 = await traceloop . withWorkflow (
321- { name : "test_cache_creation" } ,
322- async ( ) => {
323- return await generateText ( {
324- messages : [
325- { role : "system" , content : longSystemPrompt } ,
326- { role : "user" , content : "What is 2+2?" } ,
327- ] ,
328- model : vercel_openai ( "gpt-4o-mini" ) ,
329- experimental_telemetry : { isEnabled : true } ,
330- } ) ;
331- } ,
332- ) ;
333-
334- assert . ok ( result1 ) ;
335- assert . ok ( result1 . text ) ;
336-
337- await traceloop . forceFlush ( ) ;
338- const spans1 = memoryExporter . getFinishedSpans ( ) ;
339- const firstCallSpan = spans1 . find ( ( span ) => span . name === "text.generate" ) ;
340-
341- assert . ok ( firstCallSpan ) ;
342- assert . ok ( firstCallSpan . attributes [ SpanAttributes . LLM_USAGE_INPUT_TOKENS ] ) ;
343- assert . ok ( firstCallSpan . attributes [ SpanAttributes . LLM_USAGE_OUTPUT_TOKENS ] ) ;
344-
345- assert . strictEqual (
346- firstCallSpan . attributes [ "ai.usage.cachedInputTokens" ] ,
347- undefined ,
348- ) ;
349- assert . strictEqual (
350- firstCallSpan . attributes [ "ai.usage.cacheCreationInputTokens" ] ,
351- undefined ,
352- ) ;
353- assert . strictEqual (
354- firstCallSpan . attributes [ "ai.usage.cacheReadInputTokens" ] ,
355- undefined ,
356- ) ;
357-
358- if (
359- firstCallSpan . attributes [
360- SpanAttributes . LLM_USAGE_CACHE_CREATION_INPUT_TOKENS
361- ]
362- ) {
363- assert . ok (
364- Number (
365- firstCallSpan . attributes [
366- SpanAttributes . LLM_USAGE_CACHE_CREATION_INPUT_TOKENS
367- ] ,
368- ) > 0 ,
369- "Cache creation tokens should be > 0" ,
370- ) ;
371- }
372-
373- memoryExporter . reset ( ) ;
374-
375- const result2 = await traceloop . withWorkflow (
376- { name : "test_cache_read" } ,
377- async ( ) => {
378- return await generateText ( {
379- messages : [
380- { role : "system" , content : longSystemPrompt } ,
381- { role : "user" , content : "What is 3+3?" } ,
382- ] ,
383- model : vercel_openai ( "gpt-4o-mini" ) ,
384- experimental_telemetry : { isEnabled : true } ,
385- } ) ;
386- } ,
387- ) ;
388-
389- assert . ok ( result2 ) ;
390- assert . ok ( result2 . text ) ;
391-
392- await traceloop . forceFlush ( ) ;
393- const spans2 = memoryExporter . getFinishedSpans ( ) ;
394- const secondCallSpan = spans2 . find ( ( span ) => span . name === "text.generate" ) ;
395-
396- assert . ok ( secondCallSpan ) ;
397- assert . ok ( secondCallSpan . attributes [ SpanAttributes . LLM_USAGE_INPUT_TOKENS ] ) ;
398- assert . ok (
399- secondCallSpan . attributes [ SpanAttributes . LLM_USAGE_OUTPUT_TOKENS ] ,
400- ) ;
401-
402- assert . strictEqual (
403- secondCallSpan . attributes [ "ai.usage.cachedInputTokens" ] ,
404- undefined ,
405- ) ;
406- assert . strictEqual (
407- secondCallSpan . attributes [ "ai.usage.cacheCreationInputTokens" ] ,
408- undefined ,
409- ) ;
410- assert . strictEqual (
411- secondCallSpan . attributes [ "ai.usage.cacheReadInputTokens" ] ,
412- undefined ,
413- ) ;
414-
415- if (
416- secondCallSpan . attributes [
417- SpanAttributes . LLM_USAGE_CACHE_READ_INPUT_TOKENS
418- ]
419- ) {
420- const cacheReadTokens = Number (
421- secondCallSpan . attributes [
422- SpanAttributes . LLM_USAGE_CACHE_READ_INPUT_TOKENS
423- ] ,
424- ) ;
425- assert . ok (
426- cacheReadTokens > 0 ,
427- "Cache read tokens should be > 0 when cache is used" ,
428- ) ;
429- assert . strictEqual (
430- cacheReadTokens ,
431- 6900 ,
432- "Expected 6900 cache read tokens from recording" ,
433- ) ;
434- }
435- } ) ;
436272} ) ;
0 commit comments