How to determine top level template file in functions.php? [duplicate]

You can check for the single page by using is_single() function.
https://developer.wordpress.org/reference/functions/is_single/

As for index page, I am not 100% sure but is_home() may be the one.
https://codex.wordpress.org/Function_Reference/is_home