fix(stats): Use request model as fallback for stats logging (#8423)

This commit is contained in:
Abhi
2025-09-13 21:03:46 -04:00
committed by GitHub
parent 35aeb3f420
commit f11d79a931
3 changed files with 23 additions and 4 deletions

View File

@@ -299,6 +299,17 @@ describe('converter', () => {
codeAssistRes.response.automaticFunctionCallingHistory,
);
});
it('should handle modelVersion', () => {
const codeAssistRes: CaGenerateContentResponse = {
response: {
candidates: [],
modelVersion: 'gemini-2.5-pro',
},
};
const genaiRes = fromGenerateContentResponse(codeAssistRes);
expect(genaiRes.modelVersion).toEqual('gemini-2.5-pro');
});
});
describe('toContents', () => {

View File

@@ -80,6 +80,7 @@ interface VertexGenerateContentResponse {
automaticFunctionCallingHistory?: Content[];
promptFeedback?: GenerateContentResponsePromptFeedback;
usageMetadata?: GenerateContentResponseUsageMetadata;
modelVersion?: string;
}
export interface CaCountTokenRequest {
@@ -137,6 +138,7 @@ export function fromGenerateContentResponse(
out.automaticFunctionCallingHistory = inres.automaticFunctionCallingHistory;
out.promptFeedback = inres.promptFeedback;
out.usageMetadata = inres.usageMetadata;
out.modelVersion = inres.modelVersion;
return out;
}

View File

@@ -114,7 +114,7 @@ export class LoggingContentGenerator implements ContentGenerator {
const durationMs = Date.now() - startTime;
this._logApiResponse(
durationMs,
response.modelVersion || '',
response.modelVersion || req.model,
userPromptId,
response.usageMetadata,
JSON.stringify(response),
@@ -143,13 +143,19 @@ export class LoggingContentGenerator implements ContentGenerator {
throw error;
}
return this.loggingStreamWrapper(stream, startTime, userPromptId);
return this.loggingStreamWrapper(
stream,
startTime,
userPromptId,
req.model,
);
}
private async *loggingStreamWrapper(
stream: AsyncGenerator<GenerateContentResponse>,
startTime: number,
userPromptId: string,
model: string,
): AsyncGenerator<GenerateContentResponse> {
const responses: GenerateContentResponse[] = [];
@@ -166,7 +172,7 @@ export class LoggingContentGenerator implements ContentGenerator {
const durationMs = Date.now() - startTime;
this._logApiResponse(
durationMs,
responses[0]?.modelVersion || '',
responses[0]?.modelVersion || model,
userPromptId,
lastUsageMetadata,
JSON.stringify(responses),
@@ -176,7 +182,7 @@ export class LoggingContentGenerator implements ContentGenerator {
this._logApiError(
durationMs,
error,
responses[0]?.modelVersion || '',
responses[0]?.modelVersion || model,
userPromptId,
);
throw error;