From a70c0cba8a021770dd5ac8094a55a98b91fc47c0 Mon Sep 17 00:00:00 2001 From: "Devon Thomas (aider)" Date: Mon, 23 Jun 2025 22:49:07 -0700 Subject: [PATCH 1/2] feat: add support for thinking_config --- src/models/chat_session.ts | 9 +++++ src/models/generative_models.ts | 59 +++++++++++++++++++++------------ src/types/content.ts | 19 +++++++++++ 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/models/chat_session.ts b/src/models/chat_session.ts index 431f9ee4..34e8b55d 100644 --- a/src/models/chat_session.ts +++ b/src/models/chat_session.ts @@ -33,6 +33,7 @@ import { SafetySetting, StartChatSessionRequest, StreamGenerateContentResult, + ThinkingConfig, Tool, } from '../types/content'; import {ToolConfig} from '../types'; @@ -59,6 +60,7 @@ export class ChatSession { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly apiEndpoint?: string; private readonly systemInstruction?: Content; @@ -83,6 +85,7 @@ export class ChatSession { this.safetySettings = request.safetySettings; this.tools = request.tools; this.toolConfig = request.toolConfig; + this.thinkingConfig = request.thinkingConfig; this.apiEndpoint = request.apiEndpoint; this.requestOptions = requestOptions ?? {}; if (request.systemInstruction) { @@ -135,6 +138,7 @@ export class ChatSession { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, + thinkingConfig: this.thinkingConfig, }; const generateContentResult: GenerateContentResult = await generateContent( @@ -220,6 +224,7 @@ export class ChatSession { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, + thinkingConfig: this.thinkingConfig, }; const streamGenerateContentResultPromise = generateContentStream( @@ -269,6 +274,7 @@ export class ChatSessionPreview { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly apiEndpoint?: string; private readonly systemInstruction?: Content; private readonly cachedContent?: string; @@ -294,6 +300,7 @@ export class ChatSessionPreview { this.safetySettings = request.safetySettings; this.tools = request.tools; this.toolConfig = request.toolConfig; + this.thinkingConfig = request.thinkingConfig; this.apiEndpoint = request.apiEndpoint; this.requestOptions = requestOptions ?? {}; this.cachedContent = request.cachedContent; @@ -347,6 +354,7 @@ export class ChatSessionPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent, + thinkingConfig: this.thinkingConfig, }; const generateContentResult: GenerateContentResult = await generateContent( @@ -434,6 +442,7 @@ export class ChatSessionPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent, + thinkingConfig: this.thinkingConfig, }; const streamGenerateContentResultPromise = generateContentStream( diff --git a/src/models/generative_models.ts b/src/models/generative_models.ts index 1e2820e9..14471bb2 100644 --- a/src/models/generative_models.ts +++ b/src/models/generative_models.ts @@ -38,6 +38,7 @@ import { StartChatParams, StartChatSessionRequest, StreamGenerateContentResult, + ThinkingConfig, Tool, } from '../types/content'; import {ToolConfig} from '../types/tool'; @@ -58,6 +59,7 @@ export class GenerativeModel { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly requestOptions?: RequestOptions; private readonly systemInstruction?: Content; private readonly project: string; @@ -81,6 +83,7 @@ export class GenerativeModel { this.safetySettings = getGenerativeModelParams.safetySettings; this.tools = getGenerativeModelParams.tools; this.toolConfig = getGenerativeModelParams.toolConfig; + this.thinkingConfig = getGenerativeModelParams.thinkingConfig; this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; if (getGenerativeModelParams.systemInstruction) { this.systemInstruction = formulateSystemInstructionIntoContent( @@ -129,17 +132,18 @@ export class GenerativeModel { async generateContent( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = - formulateSystemInstructionIntoGenerateContentRequest( - request, - this.systemInstruction - ); + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + formulatedRequest.thinkingConfig = + formulatedRequest.thinkingConfig ?? this.thinkingConfig; + const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( + formulatedRequest, + this.systemInstruction + ); return generateContent( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -176,17 +180,18 @@ export class GenerativeModel { async generateContentStream( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = - formulateSystemInstructionIntoGenerateContentRequest( - request, - this.systemInstruction - ); + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + formulatedRequest.thinkingConfig = + formulatedRequest.thinkingConfig ?? this.thinkingConfig; + const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( + formulatedRequest, + this.systemInstruction + ); return generateContentStream( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -259,6 +264,7 @@ export class GenerativeModel { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, + thinkingConfig: this.thinkingConfig, }; if (request) { @@ -272,6 +278,8 @@ export class GenerativeModel { startChatRequest.apiEndpoint = request.apiEndpoint ?? this.apiEndpoint; startChatRequest.systemInstruction = request.systemInstruction ?? this.systemInstruction; + startChatRequest.thinkingConfig = + request.thinkingConfig ?? this.thinkingConfig; } return new ChatSession(startChatRequest, this.requestOptions); } @@ -289,6 +297,7 @@ export class GenerativeModelPreview { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; + private readonly thinkingConfig?: ThinkingConfig; private readonly requestOptions?: RequestOptions; private readonly systemInstruction?: Content; private readonly project: string; @@ -313,6 +322,7 @@ export class GenerativeModelPreview { this.safetySettings = getGenerativeModelParams.safetySettings; this.tools = getGenerativeModelParams.tools; this.toolConfig = getGenerativeModelParams.toolConfig; + this.thinkingConfig = getGenerativeModelParams.thinkingConfig; this.cachedContent = getGenerativeModelParams.cachedContent; this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; if (getGenerativeModelParams.systemInstruction) { @@ -361,19 +371,20 @@ export class GenerativeModelPreview { async generateContent( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = { + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = { ...formulateSystemInstructionIntoGenerateContentRequest( - request, + formulatedRequest, this.systemInstruction ), cachedContent: this.cachedContent?.name, + thinkingConfig: formulatedRequest.thinkingConfig ?? this.thinkingConfig, }; return generateContent( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -410,19 +421,20 @@ export class GenerativeModelPreview { async generateContentStream( request: GenerateContentRequest | string ): Promise { - request = formulateRequestToGenerateContentRequest(request); - const formulatedRequest = { + const formulatedRequest = formulateRequestToGenerateContentRequest(request); + const finalRequest = { ...formulateSystemInstructionIntoGenerateContentRequest( - request, + formulatedRequest, this.systemInstruction ), cachedContent: this.cachedContent?.name, + thinkingConfig: formulatedRequest.thinkingConfig ?? this.thinkingConfig, }; return generateContentStream( this.location, this.resourcePath, this.fetchToken(), - formulatedRequest, + finalRequest, this.apiEndpoint, this.generationConfig, this.safetySettings, @@ -496,6 +508,7 @@ export class GenerativeModelPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent?.name, + thinkingConfig: this.thinkingConfig, }; if (request) { @@ -510,6 +523,8 @@ export class GenerativeModelPreview { request.systemInstruction ?? this.systemInstruction; startChatRequest.cachedContent = request.cachedContent ?? this.cachedContent?.name; + startChatRequest.thinkingConfig = + request.thinkingConfig ?? this.thinkingConfig; } return new ChatSessionPreview(startChatRequest, this.requestOptions); } diff --git a/src/types/content.ts b/src/types/content.ts index a227e47f..99ec8287 100644 --- a/src/types/content.ts +++ b/src/types/content.ts @@ -99,6 +99,17 @@ export declare interface CountTokensResponse { totalBillableCharacters?: number; } +/** + * The configuration for thinking features. + * @public + */ +export declare interface ThinkingConfig { + /** Optional. Indicates whether to include thoughts in the response. If true, thoughts are returned only when available. */ + includeThoughts?: boolean; + /** Optional. Indicates the thinking budget in tokens. This is only applied when enable_thinking is true. */ + thinkingBudget?: number; +} + /** * Params used to call the getGenerativeModel method. */ @@ -129,6 +140,8 @@ export declare interface GetGenerativeModelParams extends ModelParams { tools?: Tool[]; /** Optional. This config is shared for all tools provided in the request. */ toolConfig?: ToolConfig; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; /** Optional. The request options to use for generation. */ requestOptions?: RequestOptions; /** @@ -174,6 +187,8 @@ export declare interface BaseModelParams { * Note: only text should be used in parts of {@link Content} */ systemInstruction?: string | Content; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; } /** @@ -1054,6 +1069,8 @@ export declare interface StartChatParams { tools?: Tool[]; /** Optional. This config is shared for all tools provided in the request. */ toolConfig?: ToolConfig; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; /** Optional. The base Vertex AI endpoint to use for the request. */ apiEndpoint?: string; /** @@ -1087,6 +1104,8 @@ export declare interface StartChatSessionRequest extends StartChatParams { * Note: only text should be used in parts of {@link Content} */ systemInstruction?: string | Content; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; } /** From 73bf007333246bc2be244ae776080923b42c92e8 Mon Sep 17 00:00:00 2001 From: "Devon Thomas (aider)" Date: Mon, 23 Jun 2025 23:18:28 -0700 Subject: [PATCH 2/2] fix: move thinkingConfig into generationConfig --- src/models/generative_models.ts | 17 ----------------- src/types/content.ts | 10 ++-------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/models/generative_models.ts b/src/models/generative_models.ts index 14471bb2..213ca5a4 100644 --- a/src/models/generative_models.ts +++ b/src/models/generative_models.ts @@ -38,7 +38,6 @@ import { StartChatParams, StartChatSessionRequest, StreamGenerateContentResult, - ThinkingConfig, Tool, } from '../types/content'; import {ToolConfig} from '../types/tool'; @@ -59,7 +58,6 @@ export class GenerativeModel { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; - private readonly thinkingConfig?: ThinkingConfig; private readonly requestOptions?: RequestOptions; private readonly systemInstruction?: Content; private readonly project: string; @@ -83,7 +81,6 @@ export class GenerativeModel { this.safetySettings = getGenerativeModelParams.safetySettings; this.tools = getGenerativeModelParams.tools; this.toolConfig = getGenerativeModelParams.toolConfig; - this.thinkingConfig = getGenerativeModelParams.thinkingConfig; this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; if (getGenerativeModelParams.systemInstruction) { this.systemInstruction = formulateSystemInstructionIntoContent( @@ -133,8 +130,6 @@ export class GenerativeModel { request: GenerateContentRequest | string ): Promise { const formulatedRequest = formulateRequestToGenerateContentRequest(request); - formulatedRequest.thinkingConfig = - formulatedRequest.thinkingConfig ?? this.thinkingConfig; const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( formulatedRequest, this.systemInstruction @@ -181,8 +176,6 @@ export class GenerativeModel { request: GenerateContentRequest | string ): Promise { const formulatedRequest = formulateRequestToGenerateContentRequest(request); - formulatedRequest.thinkingConfig = - formulatedRequest.thinkingConfig ?? this.thinkingConfig; const finalRequest = formulateSystemInstructionIntoGenerateContentRequest( formulatedRequest, this.systemInstruction @@ -264,7 +257,6 @@ export class GenerativeModel { tools: this.tools, toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, - thinkingConfig: this.thinkingConfig, }; if (request) { @@ -278,8 +270,6 @@ export class GenerativeModel { startChatRequest.apiEndpoint = request.apiEndpoint ?? this.apiEndpoint; startChatRequest.systemInstruction = request.systemInstruction ?? this.systemInstruction; - startChatRequest.thinkingConfig = - request.thinkingConfig ?? this.thinkingConfig; } return new ChatSession(startChatRequest, this.requestOptions); } @@ -297,7 +287,6 @@ export class GenerativeModelPreview { private readonly safetySettings?: SafetySetting[]; private readonly tools?: Tool[]; private readonly toolConfig?: ToolConfig; - private readonly thinkingConfig?: ThinkingConfig; private readonly requestOptions?: RequestOptions; private readonly systemInstruction?: Content; private readonly project: string; @@ -322,7 +311,6 @@ export class GenerativeModelPreview { this.safetySettings = getGenerativeModelParams.safetySettings; this.tools = getGenerativeModelParams.tools; this.toolConfig = getGenerativeModelParams.toolConfig; - this.thinkingConfig = getGenerativeModelParams.thinkingConfig; this.cachedContent = getGenerativeModelParams.cachedContent; this.requestOptions = getGenerativeModelParams.requestOptions ?? {}; if (getGenerativeModelParams.systemInstruction) { @@ -378,7 +366,6 @@ export class GenerativeModelPreview { this.systemInstruction ), cachedContent: this.cachedContent?.name, - thinkingConfig: formulatedRequest.thinkingConfig ?? this.thinkingConfig, }; return generateContent( this.location, @@ -428,7 +415,6 @@ export class GenerativeModelPreview { this.systemInstruction ), cachedContent: this.cachedContent?.name, - thinkingConfig: formulatedRequest.thinkingConfig ?? this.thinkingConfig, }; return generateContentStream( this.location, @@ -508,7 +494,6 @@ export class GenerativeModelPreview { toolConfig: this.toolConfig, systemInstruction: this.systemInstruction, cachedContent: this.cachedContent?.name, - thinkingConfig: this.thinkingConfig, }; if (request) { @@ -523,8 +508,6 @@ export class GenerativeModelPreview { request.systemInstruction ?? this.systemInstruction; startChatRequest.cachedContent = request.cachedContent ?? this.cachedContent?.name; - startChatRequest.thinkingConfig = - request.thinkingConfig ?? this.thinkingConfig; } return new ChatSessionPreview(startChatRequest, this.requestOptions); } diff --git a/src/types/content.ts b/src/types/content.ts index 99ec8287..1d79f383 100644 --- a/src/types/content.ts +++ b/src/types/content.ts @@ -140,8 +140,6 @@ export declare interface GetGenerativeModelParams extends ModelParams { tools?: Tool[]; /** Optional. This config is shared for all tools provided in the request. */ toolConfig?: ToolConfig; - /** Optional. The configuration for thinking features. */ - thinkingConfig?: ThinkingConfig; /** Optional. The request options to use for generation. */ requestOptions?: RequestOptions; /** @@ -187,8 +185,6 @@ export declare interface BaseModelParams { * Note: only text should be used in parts of {@link Content} */ systemInstruction?: string | Content; - /** Optional. The configuration for thinking features. */ - thinkingConfig?: ThinkingConfig; } /** @@ -248,6 +244,8 @@ export declare interface GenerationConfig { * If set, a compatible responseMimeType must also be set. */ responseSchema?: ResponseSchema; + /** Optional. The configuration for thinking features. */ + thinkingConfig?: ThinkingConfig; } /** @@ -1069,8 +1067,6 @@ export declare interface StartChatParams { tools?: Tool[]; /** Optional. This config is shared for all tools provided in the request. */ toolConfig?: ToolConfig; - /** Optional. The configuration for thinking features. */ - thinkingConfig?: ThinkingConfig; /** Optional. The base Vertex AI endpoint to use for the request. */ apiEndpoint?: string; /** @@ -1104,8 +1100,6 @@ export declare interface StartChatSessionRequest extends StartChatParams { * Note: only text should be used in parts of {@link Content} */ systemInstruction?: string | Content; - /** Optional. The configuration for thinking features. */ - thinkingConfig?: ThinkingConfig; } /**