MCPcopy Create free account
hub / github.com/anus-dev/ANUS / sendMessageStream

Method sendMessageStream

packages/core/src/core/anusChat.ts:304–368  ·  view source on GitHub ↗

* Sends a message to the model and returns the response in chunks. * * @remarks * This method will wait for the previous message to be processed before * sending the next message. * * @see Chat#sendMessage for non-streaming method. * @param params - parameters for sendin

(
    params: SendMessageParameters,
    prompt_id: string,
  )

Source from the content-addressed store, hash-verified

302 * ```
303 */
304 async sendMessageStream(
305 params: SendMessageParameters,
306 prompt_id: string,
307 ): Promise<AsyncGenerator<GenerateContentResponse>> {
308 await this.sendPromise;
309 const userContent = createUserContent(params.message);
310 const requestContents = this.getHistory(true).concat(userContent);
311
312 try {
313 const apiCall = () => {
314 const modelToUse = this.config.getModel();
315
316 // Prevent Flash model calls immediately after quota error
317 if (
318 this.config.getQuotaErrorOccurred() &&
319 modelToUse === DEFAULT_FALLBACK_MODEL
320 ) {
321 throw new Error(
322 'Please submit a new query to continue with the Flash model.',
323 );
324 }
325
326 return this.contentGenerator.generateContentStream(
327 {
328 model: modelToUse,
329 contents: requestContents,
330 config: { ...this.generationConfig, ...params.config },
331 },
332 prompt_id,
333 );
334 };
335
336 // Note: Retrying streams can be complex. If generateContentStream itself doesn't handle retries
337 // for transient issues internally before yielding the async generator, this retry will re-initiate
338 // the stream. For simple 429/500 errors on initial call, this is fine.
339 // If errors occur mid-stream, this setup won't resume the stream; it will restart it.
340 const streamResponse = await retryWithBackoff(apiCall, {
341 shouldRetry: (error: unknown) => {
342 // Check for known error messages and codes.
343 if (error instanceof Error && error.message) {
344 if (isSchemaDepthError(error.message)) return false;
345 if (error.message.includes('429')) return true;
346 if (error.message.match(/5\d{2}/)) return true;
347 }
348 return false; // Don't retry other errors by default
349 },
350 onPersistent429: async (authType?: string, error?: unknown) =>
351 await this.handleFlashFallback(authType, error),
352 authType: this.config.getContentGeneratorConfig()?.authType,
353 });
354
355 // Resolve the internal tracking of send completion promise - `sendPromise`
356 // for both success and failure response. The actual failure is still
357 // propagated by the `await streamResponse`.
358 this.sendPromise = Promise.resolve(streamResponse)
359 .then(() => undefined)
360 .catch(() => undefined);
361

Callers

nothing calls this directly

Calls 6

getHistoryMethod · 0.95
handleFlashFallbackMethod · 0.95
processStreamResponseMethod · 0.95
retryWithBackoffFunction · 0.85
isSchemaDepthErrorFunction · 0.85

Tested by

no test coverage detected