Unfortunately, with most SQLs, you have to send trigger body as single statement (that probably includes Sqlite3 as well). Thus we need to detect BEGIN/END and supress sending after ';' until END is reached, then send the whole thing at once. That is the reason for the 'level' variable.
I am not sure how to solve your problem frankly. I would be happy with 'do not use SqlPerformScript for transactions' note in docs, but if you have any suggestions... But I do not want to overcomplicate SqlPerformScript by e.g. detecting whether the BEGIN is function body or start of transactions, that would be hardly manageable, which means chances are slim...