11import { createLogger } from '@sim/logger'
2- import { generateInternalToken } from '@/lib/auth/internal'
32import { getBYOKKey } from '@/lib/api-key/byok'
3+ import { generateInternalToken } from '@/lib/auth/internal'
44import { logFixedUsage } from '@/lib/billing/core/usage-log'
55import { env } from '@/lib/core/config/env'
66import { isHosted } from '@/lib/core/config/feature-flags'
99 secureFetchWithPinnedIP ,
1010 validateUrlWithDNS ,
1111} from '@/lib/core/security/input-validation.server'
12+ import { PlatformEvents } from '@/lib/core/telemetry'
1213import { generateRequestId } from '@/lib/core/utils/request'
1314import { getBaseUrl } from '@/lib/core/utils/urls'
1415import { parseMcpToolId } from '@/lib/mcp/utils'
@@ -30,7 +31,6 @@ import {
3031 getToolAsync ,
3132 validateRequiredParametersAfterMerge ,
3233} from '@/tools/utils'
33- import { PlatformEvents } from '@/lib/core/telemetry'
3434
3535const logger = createLogger ( 'Tools' )
3636
@@ -154,7 +154,7 @@ async function executeWithRetry<T>(
154154 throw error
155155 }
156156
157- const delayMs = baseDelayMs * Math . pow ( 2 , attempt )
157+ const delayMs = baseDelayMs * 2 ** attempt
158158
159159 // Track throttling event via telemetry
160160 PlatformEvents . hostedKeyThrottled ( {
@@ -168,7 +168,9 @@ async function executeWithRetry<T>(
168168 workflowId : executionContext ?. workflowId ,
169169 } )
170170
171- logger . warn ( `[${ requestId } ] Rate limited for ${ toolId } (${ envVarName } ), retrying in ${ delayMs } ms (attempt ${ attempt + 1 } /${ maxRetries } )` )
171+ logger . warn (
172+ `[${ requestId } ] Rate limited for ${ toolId } (${ envVarName } ), retrying in ${ delayMs } ms (attempt ${ attempt + 1 } /${ maxRetries } )`
173+ )
172174 await new Promise ( ( resolve ) => setTimeout ( resolve , delayMs ) )
173175 }
174176 }
@@ -246,7 +248,10 @@ async function processHostedKeyCost(
246248 executionId : executionContext . executionId ,
247249 metadata,
248250 } )
249- logger . debug ( `[${ requestId } ] Logged hosted key cost for ${ tool . id } : $${ cost } ` , metadata ? { metadata } : { } )
251+ logger . debug (
252+ `[${ requestId } ] Logged hosted key cost for ${ tool . id } : $${ cost } ` ,
253+ metadata ? { metadata } : { }
254+ )
250255 } catch ( error ) {
251256 logger . error ( `[${ requestId } ] Failed to log hosted key usage for ${ tool . id } :` , error )
252257 }
@@ -648,7 +653,13 @@ export async function executeTool(
648653
649654 // Calculate hosted key cost and merge into output.cost
650655 if ( hostedKeyInfo . isUsingHostedKey && finalResult . success ) {
651- const { cost : hostedKeyCost , metadata } = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
656+ const { cost : hostedKeyCost , metadata } = await processHostedKeyCost (
657+ tool ,
658+ contextParams ,
659+ finalResult . output ,
660+ executionContext ,
661+ requestId
662+ )
652663 if ( hostedKeyCost > 0 ) {
653664 finalResult . output = {
654665 ...finalResult . output ,
@@ -677,15 +688,12 @@ export async function executeTool(
677688 // Execute the tool request directly (internal routes use regular fetch, external use SSRF-protected fetch)
678689 // Wrap with retry logic for hosted keys to handle rate limiting due to higher usage
679690 const result = hostedKeyInfo . isUsingHostedKey
680- ? await executeWithRetry (
681- ( ) => executeToolRequest ( toolId , tool , contextParams ) ,
682- {
683- requestId,
684- toolId,
685- envVarName : hostedKeyInfo . envVarName ! ,
686- executionContext,
687- }
688- )
691+ ? await executeWithRetry ( ( ) => executeToolRequest ( toolId , tool , contextParams ) , {
692+ requestId,
693+ toolId,
694+ envVarName : hostedKeyInfo . envVarName ! ,
695+ executionContext,
696+ } )
689697 : await executeToolRequest ( toolId , tool , contextParams )
690698
691699 // Apply post-processing if available and not skipped
@@ -711,7 +719,13 @@ export async function executeTool(
711719
712720 // Calculate hosted key cost and merge into output.cost
713721 if ( hostedKeyInfo . isUsingHostedKey && finalResult . success ) {
714- const { cost : hostedKeyCost , metadata } = await processHostedKeyCost ( tool , contextParams , finalResult . output , executionContext , requestId )
722+ const { cost : hostedKeyCost , metadata } = await processHostedKeyCost (
723+ tool ,
724+ contextParams ,
725+ finalResult . output ,
726+ executionContext ,
727+ requestId
728+ )
715729 if ( hostedKeyCost > 0 ) {
716730 finalResult . output = {
717731 ...finalResult . output ,
0 commit comments