* Sends a message to the model and returns the response. * * @remarks * This method will wait for the previous message to be processed before * sending the next message. * * @see Chat#sendMessageStream for streaming method. * @param params - parameters for sending messag
(
params: SendMessageParameters,
prompt_id: string,
)
| 200 | * ``` |
| 201 | */ |
| 202 | async sendMessage( |
| 203 | params: SendMessageParameters, |
| 204 | prompt_id: string, |
| 205 | ): Promise<GenerateContentResponse> { |
| 206 | await this.sendPromise; |
| 207 | const userContent = createUserContent(params.message); |
| 208 | const requestContents = this.getHistory(true).concat(userContent); |
| 209 | |
| 210 | let response: GenerateContentResponse; |
| 211 | |
| 212 | try { |
| 213 | const apiCall = () => { |
| 214 | const modelToUse = this.config.getModel() || DEFAULT_FALLBACK_MODEL; |
| 215 | |
| 216 | // Prevent Flash model calls immediately after quota error |
| 217 | if ( |
| 218 | this.config.getQuotaErrorOccurred() && |
| 219 | modelToUse === DEFAULT_FALLBACK_MODEL |
| 220 | ) { |
| 221 | throw new Error( |
| 222 | 'Please submit a new query to continue with the Flash model.', |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | return this.contentGenerator.generateContent( |
| 227 | { |
| 228 | model: modelToUse, |
| 229 | contents: requestContents, |
| 230 | config: { ...this.generationConfig, ...params.config }, |
| 231 | }, |
| 232 | prompt_id, |
| 233 | ); |
| 234 | }; |
| 235 | |
| 236 | response = await retryWithBackoff(apiCall, { |
| 237 | shouldRetry: (error: unknown) => { |
| 238 | // Check for known error messages and codes. |
| 239 | if (error instanceof Error && error.message) { |
| 240 | if (isSchemaDepthError(error.message)) return false; |
| 241 | if (error.message.includes('429')) return true; |
| 242 | if (error.message.match(/5\d{2}/)) return true; |
| 243 | } |
| 244 | return false; // Don't retry other errors by default |
| 245 | }, |
| 246 | onPersistent429: async (authType?: string, error?: unknown) => |
| 247 | await this.handleFlashFallback(authType, error), |
| 248 | authType: this.config.getContentGeneratorConfig()?.authType, |
| 249 | }); |
| 250 | |
| 251 | this.sendPromise = (async () => { |
| 252 | const outputContent = response.candidates?.[0]?.content; |
| 253 | // Because the AFC input contains the entire curated chat history in |
| 254 | // addition to the new user input, we need to truncate the AFC history |
| 255 | // to deduplicate the existing chat history. |
| 256 | const fullAutomaticFunctionCallingHistory = |
| 257 | response.automaticFunctionCallingHistory; |
| 258 | const index = this.getHistory(true).length; |
| 259 | let automaticFunctionCallingHistory: Content[] = []; |
no test coverage detected