(params: {
userId: string
workspaceId?: string | null
embeddingModel: string
query: string
isBYOK: boolean
sourceReference: string
})
| 416 | * there is no workspace to attribute to. Best-effort: never throws. |
| 417 | */ |
| 418 | export async function recordSearchEmbeddingUsage(params: { |
| 419 | userId: string |
| 420 | workspaceId?: string | null |
| 421 | embeddingModel: string |
| 422 | query: string |
| 423 | isBYOK: boolean |
| 424 | sourceReference: string |
| 425 | }): Promise<void> { |
| 426 | const { userId, workspaceId, embeddingModel, query, isBYOK, sourceReference } = params |
| 427 | if (isBYOK || !workspaceId) return |
| 428 | try { |
| 429 | const { count } = estimateTokenCount( |
| 430 | query, |
| 431 | getEmbeddingModelInfo(embeddingModel).tokenizerProvider |
| 432 | ) |
| 433 | const cost = calculateCost(embeddingModel, count, 0, false) |
| 434 | if (!cost || cost.total <= 0) return |
| 435 | await recordUsage({ |
| 436 | userId, |
| 437 | workspaceId, |
| 438 | entries: [ |
| 439 | { |
| 440 | category: 'model', |
| 441 | source: 'knowledge-base', |
| 442 | description: embeddingModel, |
| 443 | cost: cost.total, |
| 444 | sourceReference, |
| 445 | }, |
| 446 | ], |
| 447 | }) |
| 448 | } catch (error) { |
| 449 | logger.warn('Failed to record search embedding usage', { error: getErrorMessage(error) }) |
| 450 | } |
| 451 | } |
no test coverage detected