Files
tasq/node_modules/@ai-sdk/google/src/convert-google-generative-ai-usage.ts
T
2026-04-09 19:01:53 +08:00

59 lines
1.6 KiB
TypeScript

import { LanguageModelV3Usage } from '@ai-sdk/provider';
export type GoogleGenerativeAITokenDetail = {
modality: string;
tokenCount: number;
};
export type GoogleGenerativeAIUsageMetadata = {
promptTokenCount?: number | null;
candidatesTokenCount?: number | null;
totalTokenCount?: number | null;
cachedContentTokenCount?: number | null;
thoughtsTokenCount?: number | null;
trafficType?: string | null;
promptTokensDetails?: GoogleGenerativeAITokenDetail[] | null;
candidatesTokensDetails?: GoogleGenerativeAITokenDetail[] | null;
};
export function convertGoogleGenerativeAIUsage(
usage: GoogleGenerativeAIUsageMetadata | undefined | null,
): LanguageModelV3Usage {
if (usage == null) {
return {
inputTokens: {
total: undefined,
noCache: undefined,
cacheRead: undefined,
cacheWrite: undefined,
},
outputTokens: {
total: undefined,
text: undefined,
reasoning: undefined,
},
raw: undefined,
};
}
const promptTokens = usage.promptTokenCount ?? 0;
const candidatesTokens = usage.candidatesTokenCount ?? 0;
const cachedContentTokens = usage.cachedContentTokenCount ?? 0;
const thoughtsTokens = usage.thoughtsTokenCount ?? 0;
return {
inputTokens: {
total: promptTokens,
noCache: promptTokens - cachedContentTokens,
cacheRead: cachedContentTokens,
cacheWrite: undefined,
},
outputTokens: {
total: candidatesTokens + thoughtsTokens,
text: candidatesTokens,
reasoning: thoughtsTokens,
},
raw: usage,
};
}