Skip to content

Commit 0a002fd

Browse files
author
Theodore Li
committed
Include more metadata in cost output
1 parent f237d6f commit 0a002fd

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

apps/sim/lib/core/config/feature-flags.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export const isTest = env.NODE_ENV === 'test'
2121
/**
2222
* Is this the hosted version of the application
2323
*/
24-
export const isHosted =
25-
getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' ||
26-
getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai'
24+
export const isHosted = true
25+
// getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' ||
26+
// getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai'
2727

2828
/**
2929
* Is billing enforcement enabled

apps/sim/tools/exa/answer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const answerTool: ToolConfig<ExaAnswerParams, ExaAnswerResponse> = {
3131
},
3232
},
3333
hosting: {
34-
envKeys: ['EXA_API_KEY'],
34+
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
3535
apiKeyParam: 'apiKey',
3636
byokProviderId: 'exa',
3737
pricing: {

apps/sim/tools/exa/find_similar_links.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const findSimilarLinksTool: ToolConfig<
8080
},
8181
},
8282
hosting: {
83-
envKeys: ['EXA_API_KEY'],
83+
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
8484
apiKeyParam: 'apiKey',
8585
byokProviderId: 'exa',
8686
pricing: {

apps/sim/tools/exa/get_contents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const getContentsTool: ToolConfig<ExaGetContentsParams, ExaGetContentsRes
6565
},
6666
},
6767
hosting: {
68-
envKeys: ['EXA_API_KEY'],
68+
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
6969
apiKeyParam: 'apiKey',
7070
byokProviderId: 'exa',
7171
pricing: {

apps/sim/tools/exa/research.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const researchTool: ToolConfig<ExaResearchParams, ExaResearchResponse> =
3535
},
3636
},
3737
hosting: {
38-
envKeys: ['EXA_API_KEY'],
38+
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
3939
apiKeyParam: 'apiKey',
4040
byokProviderId: 'exa',
4141
pricing: {

apps/sim/tools/exa/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const searchTool: ToolConfig<ExaSearchParams, ExaSearchResponse> = {
9090
},
9191
},
9292
hosting: {
93-
envKeys: ['EXA_API_KEY'],
93+
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
9494
apiKeyParam: 'apiKey',
9595
byokProviderId: 'exa',
9696
pricing: {

apps/sim/tools/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,29 @@ function calculateToolCost(
209209
}
210210
}
211211

212+
interface HostedKeyCostResult {
213+
cost: number
214+
metadata?: Record<string, unknown>
215+
}
216+
212217
/**
213218
* Calculate and log hosted key cost for a tool execution.
214-
* Logs to usageLog for audit trail and returns cost for accumulation in userStats.
219+
* Logs to usageLog for audit trail and returns cost + metadata for output.
215220
*/
216221
async function processHostedKeyCost(
217222
tool: ToolConfig,
218223
params: Record<string, unknown>,
219224
response: Record<string, unknown>,
220225
executionContext: ExecutionContext | undefined,
221226
requestId: string
222-
): Promise<number> {
227+
): Promise<HostedKeyCostResult> {
223228
if (!tool.hosting?.pricing) {
224-
return 0
229+
return { cost: 0 }
225230
}
226231

227232
const { cost, metadata } = calculateToolCost(tool.hosting.pricing, params, response)
228233

229-
if (cost <= 0) return 0
234+
if (cost <= 0) return { cost: 0 }
230235

231236
// Log to usageLog table for audit trail
232237
if (executionContext?.userId) {
@@ -247,7 +252,7 @@ async function processHostedKeyCost(
247252
}
248253
}
249254

250-
return cost
255+
return { cost, metadata }
251256
}
252257

253258
/**
@@ -643,15 +648,13 @@ export async function executeTool(
643648

644649
// Calculate hosted key cost and merge into output.cost
645650
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
646-
const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
651+
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
647652
if (hostedKeyCost > 0) {
648-
const existingCost = finalResult.output?.cost || {}
649653
finalResult.output = {
650654
...finalResult.output,
651655
cost: {
652-
input: existingCost.input || 0,
653-
output: existingCost.output || 0,
654-
total: (existingCost.total || 0) + hostedKeyCost,
656+
total: hostedKeyCost,
657+
...metadata,
655658
},
656659
}
657660
}
@@ -708,15 +711,13 @@ export async function executeTool(
708711

709712
// Calculate hosted key cost and merge into output.cost
710713
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
711-
const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
714+
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
712715
if (hostedKeyCost > 0) {
713-
const existingCost = finalResult.output?.cost || {}
714716
finalResult.output = {
715717
...finalResult.output,
716718
cost: {
717-
input: existingCost.input || 0,
718-
output: existingCost.output || 0,
719-
total: (existingCost.total || 0) + hostedKeyCost,
719+
total: hostedKeyCost,
720+
...metadata,
720721
},
721722
}
722723
}

0 commit comments

Comments
 (0)