mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-14 08:25:03 -05:00
Include more metadata in cost output
This commit is contained in:
@@ -21,9 +21,9 @@ export const isTest = env.NODE_ENV === 'test'
|
||||
/**
|
||||
* Is this the hosted version of the application
|
||||
*/
|
||||
export const isHosted =
|
||||
getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' ||
|
||||
getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai'
|
||||
export const isHosted = true
|
||||
// getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.sim.ai' ||
|
||||
// getEnv('NEXT_PUBLIC_APP_URL') === 'https://www.staging.sim.ai'
|
||||
|
||||
/**
|
||||
* Is billing enforcement enabled
|
||||
|
||||
@@ -31,7 +31,7 @@ export const answerTool: ToolConfig<ExaAnswerParams, ExaAnswerResponse> = {
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
envKeys: ['EXA_API_KEY'],
|
||||
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
|
||||
apiKeyParam: 'apiKey',
|
||||
byokProviderId: 'exa',
|
||||
pricing: {
|
||||
|
||||
@@ -80,7 +80,7 @@ export const findSimilarLinksTool: ToolConfig<
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
envKeys: ['EXA_API_KEY'],
|
||||
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
|
||||
apiKeyParam: 'apiKey',
|
||||
byokProviderId: 'exa',
|
||||
pricing: {
|
||||
|
||||
@@ -65,7 +65,7 @@ export const getContentsTool: ToolConfig<ExaGetContentsParams, ExaGetContentsRes
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
envKeys: ['EXA_API_KEY'],
|
||||
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
|
||||
apiKeyParam: 'apiKey',
|
||||
byokProviderId: 'exa',
|
||||
pricing: {
|
||||
|
||||
@@ -35,7 +35,7 @@ export const researchTool: ToolConfig<ExaResearchParams, ExaResearchResponse> =
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
envKeys: ['EXA_API_KEY'],
|
||||
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
|
||||
apiKeyParam: 'apiKey',
|
||||
byokProviderId: 'exa',
|
||||
pricing: {
|
||||
|
||||
@@ -90,7 +90,7 @@ export const searchTool: ToolConfig<ExaSearchParams, ExaSearchResponse> = {
|
||||
},
|
||||
},
|
||||
hosting: {
|
||||
envKeys: ['EXA_API_KEY'],
|
||||
envKeys: ['EXA_API_KEY_1', 'EXA_API_KEY_2', 'EXA_API_KEY_3'],
|
||||
apiKeyParam: 'apiKey',
|
||||
byokProviderId: 'exa',
|
||||
pricing: {
|
||||
|
||||
@@ -209,9 +209,14 @@ function calculateToolCost(
|
||||
}
|
||||
}
|
||||
|
||||
interface HostedKeyCostResult {
|
||||
cost: number
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate and log hosted key cost for a tool execution.
|
||||
* Logs to usageLog for audit trail and returns cost for accumulation in userStats.
|
||||
* Logs to usageLog for audit trail and returns cost + metadata for output.
|
||||
*/
|
||||
async function processHostedKeyCost(
|
||||
tool: ToolConfig,
|
||||
@@ -219,14 +224,14 @@ async function processHostedKeyCost(
|
||||
response: Record<string, unknown>,
|
||||
executionContext: ExecutionContext | undefined,
|
||||
requestId: string
|
||||
): Promise<number> {
|
||||
): Promise<HostedKeyCostResult> {
|
||||
if (!tool.hosting?.pricing) {
|
||||
return 0
|
||||
return { cost: 0 }
|
||||
}
|
||||
|
||||
const { cost, metadata } = calculateToolCost(tool.hosting.pricing, params, response)
|
||||
|
||||
if (cost <= 0) return 0
|
||||
if (cost <= 0) return { cost: 0 }
|
||||
|
||||
// Log to usageLog table for audit trail
|
||||
if (executionContext?.userId) {
|
||||
@@ -247,7 +252,7 @@ async function processHostedKeyCost(
|
||||
}
|
||||
}
|
||||
|
||||
return cost
|
||||
return { cost, metadata }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -643,15 +648,13 @@ export async function executeTool(
|
||||
|
||||
// Calculate hosted key cost and merge into output.cost
|
||||
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
|
||||
const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
|
||||
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
|
||||
if (hostedKeyCost > 0) {
|
||||
const existingCost = finalResult.output?.cost || {}
|
||||
finalResult.output = {
|
||||
...finalResult.output,
|
||||
cost: {
|
||||
input: existingCost.input || 0,
|
||||
output: existingCost.output || 0,
|
||||
total: (existingCost.total || 0) + hostedKeyCost,
|
||||
total: hostedKeyCost,
|
||||
...metadata,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -708,15 +711,13 @@ export async function executeTool(
|
||||
|
||||
// Calculate hosted key cost and merge into output.cost
|
||||
if (hostedKeyInfo.isUsingHostedKey && finalResult.success) {
|
||||
const hostedKeyCost = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
|
||||
const { cost: hostedKeyCost, metadata } = await processHostedKeyCost(tool, contextParams, finalResult.output, executionContext, requestId)
|
||||
if (hostedKeyCost > 0) {
|
||||
const existingCost = finalResult.output?.cost || {}
|
||||
finalResult.output = {
|
||||
...finalResult.output,
|
||||
cost: {
|
||||
input: existingCost.input || 0,
|
||||
output: existingCost.output || 0,
|
||||
total: (existingCost.total || 0) + hostedKeyCost,
|
||||
total: hostedKeyCost,
|
||||
...metadata,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user