|
17 | 17 | import {
|
18 | 18 | FunctionCallingMode,
|
19 | 19 | FunctionDeclarationSchemaType,
|
| 20 | + UsageMetadata, |
20 | 21 | type Content,
|
21 | 22 | type FunctionDeclaration,
|
22 | 23 | type Part as GeminiPart,
|
@@ -63,11 +64,17 @@ import {
|
63 | 64 | } from 'genkit/model/middleware';
|
64 | 65 | import { runInNewSpan } from 'genkit/tracing';
|
65 | 66 | import { GoogleAuth } from 'google-auth-library';
|
66 |
| - |
67 | 67 | import type { PluginOptions } from './common/types.js';
|
68 | 68 | import { handleCacheIfNeeded } from './context-caching/index.js';
|
69 | 69 | import { extractCacheConfig } from './context-caching/utils.js';
|
70 | 70 |
|
| 71 | +// Extra type guard to keep the compiler happy and avoid a cast to any. The |
| 72 | +// legacy Gemini SDK is no longer maintained, and doesn't have updated types. |
| 73 | +// However, the REST API returns the data we want. |
| 74 | +type ExtendedUsageMetadata = UsageMetadata & { |
| 75 | + thoughtsTokenCount?: number; |
| 76 | +}; |
| 77 | + |
71 | 78 | export const SafetySettingsSchema = z.object({
|
72 | 79 | category: z.enum([
|
73 | 80 | /** The harm category is unspecified. */
|
@@ -1276,16 +1283,18 @@ export function defineGeminiModel({
|
1276 | 1283 | fromGeminiCandidate(c, jsonMode)
|
1277 | 1284 | );
|
1278 | 1285 |
|
| 1286 | + const usageMetadata = response.usageMetadata as ExtendedUsageMetadata; |
| 1287 | + |
1279 | 1288 | return {
|
1280 | 1289 | candidates: candidateData,
|
1281 | 1290 | custom: response,
|
1282 | 1291 | usage: {
|
1283 | 1292 | ...getBasicUsageStats(request.messages, candidateData),
|
1284 |
| - inputTokens: response.usageMetadata?.promptTokenCount, |
1285 |
| - outputTokens: response.usageMetadata?.candidatesTokenCount, |
1286 |
| - totalTokens: response.usageMetadata?.totalTokenCount, |
1287 |
| - cachedContentTokens: |
1288 |
| - response.usageMetadata?.cachedContentTokenCount, |
| 1293 | + inputTokens: usageMetadata?.promptTokenCount, |
| 1294 | + outputTokens: usageMetadata?.candidatesTokenCount, |
| 1295 | + totalTokens: usageMetadata?.totalTokenCount, |
| 1296 | + thoughtsTokens: usageMetadata?.thoughtsTokenCount, |
| 1297 | + cachedContentTokens: usageMetadata?.cachedContentTokenCount, |
1289 | 1298 | },
|
1290 | 1299 | };
|
1291 | 1300 | };
|
|
0 commit comments