import type Anthropic from '@anthropic-ai/sdk';

type Bloque = Anthropic.Beta.BetaContentBlock;

/**
 * Después de un fallback a mitad de la respuesta, los bloques de razonamiento y
 * tool_use ANTERIORES al último bloque `fallback` no se reenvían; el texto sí.
 */
export function paraEco(contenido: Bloque[]): Bloque[] {
  const ultimo = contenido.map((b) => b.type as string).lastIndexOf('fallback');
  if (ultimo < 0) return contenido;
  return contenido.filter((b, i) => {
    if (i > ultimo) return true;
    if (i === ultimo) return false;
    return b.type === 'text';
  });
}
