Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/llm/google/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
import type { GeminiApiUsageMetadata, InputTokenDetails } from './types';
import type { GoogleClientOptions, GoogleThinkingConfig } from '@/types';
import { smoothGenerationChunks } from '@/llm/stream/chunkAdapters';
import { resolveStreamDelay } from '@/llm/stream/smoother';
import {
convertResponseContentToChatGenerationChunk,
convertBaseMessagesToContent,
isGemini3PlusModel,
dropUnsupportedModelTurnPrefill,
mapGenerateContentResultToChatResult,
} from './utils/common';
import { smoothGenerationChunks } from '@/llm/stream/chunkAdapters';
import { resolveStreamDelay } from '@/llm/stream/smoother';

type GoogleToolConfigWithServerSideInvocations = ToolConfig & {
includeServerSideToolInvocations?: boolean;
Expand All @@ -45,13 +46,14 @@
/**
* Override to add gemini-3 model support for multimodal and function calling thought signatures
*/
/** Gemini 3 and later accept image, audio, video and PDF input. */
get _isMultimodalModel(): boolean {
return (
this.model.startsWith('gemini-1.5') ||
this.model.startsWith('gemini-2') ||
(this.model.startsWith('gemma-3-') &&
!this.model.startsWith('gemma-3-1b')) ||
this.model.startsWith('gemini-3')
isGemini3PlusModel(this.model)
);
}

Expand Down Expand Up @@ -170,7 +172,7 @@
total_tokens: usageMetadata.totalTokenCount ?? 0,
};

if (usageMetadata.cachedContentTokenCount) {

Check warning on line 175 in src/llm/google/index.ts

View workflow job for this annotation

GitHub Actions / validate / lint

Unexpected nullable number value in conditional. Please handle the nullish/zero/NaN cases explicitly
output.input_token_details ??= {};
output.input_token_details.cache_read =
usageMetadata.cachedContentTokenCount;
Expand Down
25 changes: 25 additions & 0 deletions src/llm/google/multimodal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, test } from '@jest/globals';
import { CustomChatGoogleGenerativeAI } from './index';

const isMultimodal = (model: string): boolean =>
new CustomChatGoogleGenerativeAI({ model, apiKey: 'test-key' })
._isMultimodalModel;

describe('CustomChatGoogleGenerativeAI._isMultimodalModel', () => {
test('accepts Gemini 3 and later, including Gemini 4 Argon', () => {
expect(isMultimodal('gemini-3-pro-preview')).toBe(true);
expect(isMultimodal('gemini-3.8-flash')).toBe(true);
expect(isMultimodal('gemini-4-argon')).toBe(true);
expect(isMultimodal('models/gemini-4-argon')).toBe(true);
expect(isMultimodal('gemini-4-argon-cyber-permissive')).toBe(true);
expect(isMultimodal('gemini-10-pro')).toBe(true);
});

test('keeps the earlier generations and text-only models as they were', () => {
expect(isMultimodal('gemini-1.5-pro')).toBe(true);
expect(isMultimodal('gemini-2.5-flash')).toBe(true);
expect(isMultimodal('gemma-3-27b-it')).toBe(true);
expect(isMultimodal('gemma-3-1b-it')).toBe(false);
expect(isMultimodal('gemini-pro')).toBe(false);
});
});
54 changes: 49 additions & 5 deletions src/llm/google/utils/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ import type {
Content,
EnhancedGenerateContentResponse,
} from '@google/generative-ai';
import {
STREAMED_TOOL_CALL_SEAL_METADATA_KEY,
STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY,
GOOGLE_STREAMED_TOOL_CALL_ADAPTER,
} from '@/tools/streamedToolCallSeals';
import {
convertResponseContentToChatGenerationChunk,
convertMessageContentToParts,
dropUnsupportedModelTurnPrefill,
rejectsModelTurnPrefill,
isGemini3PlusModel,
} from './common';
import {
STREAMED_TOOL_CALL_SEAL_METADATA_KEY,
STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY,
GOOGLE_STREAMED_TOOL_CALL_ADAPTER,
} from '@/tools/streamedToolCallSeals';
import { toLangChainContent } from '@/messages/langchain';

describe('convertMessageContentToParts cross-provider tool calls', () => {
test.each([
['gemini-3.5-flash', true],
['gemini-4-argon', true],
['models/gemini-4-argon-cyber-permissive', true],
['gemini-2.5-pro', false],
])(
'adds a placeholder thought signature to unsigned tool calls on %s: %s',
(model, expected) => {
const message = new AIMessage({
content: '',
tool_calls: [{ id: 'call_weather', name: 'weather', args: {} }],
});

const [part] = convertMessageContentToParts(message, false, [], model);

expect('thoughtSignature' in part).toBe(expected);
}
);

test('deduplicates Anthropic tool_use content mirrored in parsed tool_calls', () => {
const message = new AIMessage({
content: toLangChainContent([
Expand Down Expand Up @@ -147,6 +167,14 @@ describe('rejectsModelTurnPrefill', () => {
expect(rejectsModelTurnPrefill('google/gemini-3.8-flash-lite')).toBe(true);
});

test('covers Gemini 4 Argon and its variants', () => {
expect(rejectsModelTurnPrefill('gemini-4-argon')).toBe(true);
expect(rejectsModelTurnPrefill('models/gemini-4-argon')).toBe(true);
expect(rejectsModelTurnPrefill('gemini-4-argon-cyber-permissive')).toBe(
true
);
});

test('splits the 3.5 generation, where only Flash-Lite rejects prefill', () => {
expect(rejectsModelTurnPrefill('gemini-3.5-flash')).toBe(false);
expect(rejectsModelTurnPrefill('gemini-3.5-flash-latest')).toBe(false);
Expand Down Expand Up @@ -227,3 +255,19 @@ describe('dropUnsupportedModelTurnPrefill', () => {
).toBeUndefined();
});
});

describe('isGemini3PlusModel', () => {
test('matches Gemini 3 and later, with or without a prefix', () => {
expect(isGemini3PlusModel('gemini-3-pro-preview')).toBe(true);
expect(isGemini3PlusModel('gemini-4-argon')).toBe(true);
expect(isGemini3PlusModel('models/gemini-4-argon')).toBe(true);
expect(isGemini3PlusModel('google/gemini-10-pro')).toBe(true);
});

test('rejects earlier generations and missing ids', () => {
expect(isGemini3PlusModel('gemini-2.5-flash')).toBe(false);
expect(isGemini3PlusModel('gemini-pro')).toBe(false);
expect(isGemini3PlusModel('gemma-3-27b-it')).toBe(false);
expect(isGemini3PlusModel(undefined)).toBe(false);
});
});
24 changes: 19 additions & 5 deletions src/llm/google/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ function createGoogleFunctionResponsePart({
*/
export const iife = <T>(fn: () => T): T => fn();

/** `gemini-3`, `gemini-3.8-flash`, `gemini-4-argon`, and any later major version. */
const GEMINI_3_PLUS_PATTERN = /^gemini-(?:[3-9]|[1-9]\d+)(?:[.-]|$)/;

/** Whether `model` (optionally `models/` or provider prefixed) is Gemini 3 or later. */
export function isGemini3PlusModel(model?: string): boolean {
const modelId = model?.toLowerCase().split('/').pop() ?? '';
return GEMINI_3_PLUS_PATTERN.test(modelId);
}

export function getMessageAuthor(message: BaseMessage): string {
const type = message._getType();
if (ChatMessage.isInstance(message)) {
Expand Down Expand Up @@ -540,7 +549,7 @@ export function convertMessageContentToParts(
return signature;
}
}
if (model?.includes('gemini-3') === true) {
if (isGemini3PlusModel(model)) {
return DUMMY_SIGNATURE;
}
return '';
Expand Down Expand Up @@ -681,12 +690,17 @@ const NO_PREFILL_FLASH_MIN_VERSION = { major: 3, minor: 6 } as const;
const GEMINI_FLASH_VERSION_PATTERN = /^gemini-(\d+)(?:\.(\d+))?-flash(?:$|-)/;

/**
* Models that reject prefill despite predating
* {@link NO_PREFILL_FLASH_MIN_VERSION}. Gemini 3.5 Flash-Lite enforces the
* Models that reject prefill outside the Flash version cutoff
* ({@link NO_PREFILL_FLASH_MIN_VERSION}). Gemini 3.5 Flash-Lite enforces the
* restriction while its sibling Gemini 3.5 Flash still accepts a trailing model
* turn, so the 3.5 generation cannot be expressed as a version cutoff.
* turn, so the 3.5 generation cannot be expressed as a version cutoff. Gemini 4
* Argon is not a Flash model; its histories cannot end in a `model` turn.
* @see https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/guides/gemini-4-argon
*/
const NO_PREFILL_GEMINI_MODELS = ['gemini-3.5-flash-lite'] as const;
const NO_PREFILL_GEMINI_MODELS = [
'gemini-3.5-flash-lite',
'gemini-4-argon',
] as const;

export function rejectsModelTurnPrefill(model?: string): boolean {
if (model == null || model === '') {
Expand Down
Loading