@@ -4,7 +4,7 @@ import type {
44 LanguageModelV2StreamPart ,
55} from "@ai-sdk/provider" ;
66import { wrapLanguageModel } from "ai" ;
7- import { reportTrace , shouldReportTrace } from "./traces.js" ;
7+ import { reportTraceLocalStorage } from "./traces.js" ;
88import { getCacheContext , generateCacheKey } from "./cache.js" ;
99
1010const handlePromptContent = (
@@ -95,14 +95,8 @@ export const wrapAISDKModel = (
9595 const enableTracing = options ?. tracing ?? true ;
9696 const enableCaching = options ?. caching ?? true ;
9797
98- const context = getCacheContext ( ) ;
99- const cachingAvailable =
100- enableCaching && ( context ?. cacheEnabled ?? true ) && context ?. serverPort ;
101-
102- const tracingAvailable = enableTracing && shouldReportTrace ( ) ;
103-
10498 // If neither is enabled/available, return original model
105- if ( ! cachingAvailable && ! tracingAvailable ) {
99+ if ( ! enableCaching && ! enableTracing ) {
106100 return model ;
107101 }
108102
@@ -112,10 +106,10 @@ export const wrapAISDKModel = (
112106 wrapGenerate : async ( opts ) => {
113107 const start = performance . now ( ) ;
114108 let result : Awaited < ReturnType < typeof opts . doGenerate > > | undefined ;
109+ const cacheContext = getCacheContext ( ) ;
115110
116111 // Try cache if enabled
117- if ( cachingAvailable ) {
118- const context = getCacheContext ( ) ! ;
112+ if ( cacheContext ) {
119113 const keyHash = generateCacheKey ( {
120114 model : model . modelId ,
121115 params : opts . params ,
@@ -125,7 +119,7 @@ export const wrapAISDKModel = (
125119
126120 try {
127121 const cacheResponse = await fetch (
128- `http://localhost:${ context . serverPort } /api/cache/${ keyHash } `
122+ `http://localhost:${ cacheContext . serverPort } /api/cache/${ keyHash } `
129123 ) ;
130124
131125 if ( cacheResponse . ok ) {
@@ -134,7 +128,7 @@ export const wrapAISDKModel = (
134128 duration : number ;
135129 } ;
136130 if ( cached ?. value ) {
137- context . reportCacheHit ( {
131+ cacheContext . reportCacheHit ( {
138132 keyHash,
139133 hit : true ,
140134 savedDuration : cached . duration ,
@@ -161,8 +155,7 @@ export const wrapAISDKModel = (
161155 const duration = performance . now ( ) - start ;
162156
163157 // Store in cache if caching enabled
164- if ( cachingAvailable ) {
165- const context = getCacheContext ( ) ! ;
158+ if ( cacheContext ) {
166159 const keyHash = generateCacheKey ( {
167160 model : model . modelId ,
168161 params : opts . params ,
@@ -172,7 +165,7 @@ export const wrapAISDKModel = (
172165
173166 try {
174167 await fetch (
175- `http://localhost:${ context . serverPort } /api/cache/${ keyHash } ` ,
168+ `http://localhost:${ cacheContext . serverPort } /api/cache/${ keyHash } ` ,
176169 {
177170 method : "POST" ,
178171 headers : { "Content-Type" : "application/json" } ,
@@ -183,12 +176,18 @@ export const wrapAISDKModel = (
183176 console . warn ( "Cache write failed:" , error ) ;
184177 }
185178
186- context . reportCacheHit ( { keyHash, hit : false , savedDuration : 0 } ) ;
179+ cacheContext . reportCacheHit ( {
180+ keyHash,
181+ hit : false ,
182+ savedDuration : 0 ,
183+ } ) ;
187184 }
188185 }
189186
187+ const reportTraceFromContext = reportTraceLocalStorage . getStore ( ) ;
188+
190189 // Report trace if enabled
191- if ( tracingAvailable ) {
190+ if ( reportTraceFromContext ) {
192191 const end = performance . now ( ) ;
193192 const textContent = result . content
194193 . filter ( ( c ) => c . type === "text" )
@@ -208,7 +207,7 @@ export const wrapAISDKModel = (
208207 )
209208 . filter ( Boolean ) ;
210209
211- reportTrace ( {
210+ reportTraceFromContext ( {
212211 output : {
213212 text : textContent ,
214213 toolCalls,
@@ -230,9 +229,11 @@ export const wrapAISDKModel = (
230229 const start = performance . now ( ) ;
231230 let cachedParts : LanguageModelV2StreamPart [ ] | undefined ;
232231
232+ const cacheContext = getCacheContext ( ) ;
233+ const reportTraceFromContext = reportTraceLocalStorage . getStore ( ) ;
234+
233235 // Try cache if enabled
234- if ( cachingAvailable ) {
235- const context = getCacheContext ( ) ! ;
236+ if ( cacheContext ) {
236237 const keyHash = generateCacheKey ( {
237238 model : model . modelId ,
238239 params : params ,
@@ -242,7 +243,7 @@ export const wrapAISDKModel = (
242243
243244 try {
244245 const cacheResponse = await fetch (
245- `http://localhost:${ context . serverPort } /api/cache/${ keyHash } `
246+ `http://localhost:${ cacheContext . serverPort } /api/cache/${ keyHash } `
246247 ) ;
247248
248249 if ( cacheResponse . ok ) {
@@ -251,7 +252,7 @@ export const wrapAISDKModel = (
251252 duration : number ;
252253 } ;
253254 if ( cached ?. value ) {
254- context . reportCacheHit ( {
255+ cacheContext . reportCacheHit ( {
255256 keyHash,
256257 hit : true ,
257258 savedDuration : cached . duration ,
@@ -260,12 +261,12 @@ export const wrapAISDKModel = (
260261 cachedParts = cached . value as LanguageModelV2StreamPart [ ] ;
261262
262263 // If tracing enabled, report trace for cached stream
263- if ( tracingAvailable ) {
264+ if ( reportTraceFromContext ) {
264265 const usage = cachedParts . find (
265266 ( part ) => part . type === "finish"
266267 ) ?. usage ;
267268
268- reportTrace ( {
269+ reportTraceFromContext ( {
269270 start,
270271 end : performance . now ( ) ,
271272 input : processPromptForTracing ( params . prompt ) ,
@@ -319,8 +320,7 @@ export const wrapAISDKModel = (
319320 const duration = performance . now ( ) - start ;
320321
321322 // Store in cache if enabled
322- if ( cachingAvailable ) {
323- const context = getCacheContext ( ) ! ;
323+ if ( cacheContext ) {
324324 const keyHash = generateCacheKey ( {
325325 model : model . modelId ,
326326 params : params ,
@@ -330,7 +330,7 @@ export const wrapAISDKModel = (
330330
331331 try {
332332 await fetch (
333- `http://localhost:${ context . serverPort } /api/cache/${ keyHash } ` ,
333+ `http://localhost:${ cacheContext . serverPort } /api/cache/${ keyHash } ` ,
334334 {
335335 method : "POST" ,
336336 headers : { "Content-Type" : "application/json" } ,
@@ -344,20 +344,20 @@ export const wrapAISDKModel = (
344344 console . warn ( "Cache write failed:" , error ) ;
345345 }
346346
347- context . reportCacheHit ( {
347+ cacheContext . reportCacheHit ( {
348348 keyHash,
349349 hit : false ,
350350 savedDuration : 0 ,
351351 } ) ;
352352 }
353353
354354 // Report trace if enabled
355- if ( tracingAvailable ) {
355+ if ( reportTraceFromContext ) {
356356 const usage = fullResponse . find (
357357 ( part ) => part . type === "finish"
358358 ) ?. usage ;
359359
360- reportTrace ( {
360+ reportTraceFromContext ( {
361361 start,
362362 end : performance . now ( ) ,
363363 input : processPromptForTracing ( params . prompt ) ,
0 commit comments