I don't mean to be snarky, but you could ask the same question of how anybody recorded the dialog of George Washington. During the Whiskey Rebellion, when he put on his spectacles, and simply told his men that he had grown old in the service of his country, people remembered it.
Likewise, there are five versions of the Gettysburg address, depending on which transcription of Abraham Lincoln's own writing you use. Of those five versions, there are a few prepositional phrases that are modified, but to call them anything other than the same speech would be to stretch the grounds of credulity. Still, the variation is sufficiently minor to where you'd have to be really, really picky to ask, "How can we be sure of what Lincoln really said.
Before modern recording devices, there was no expectation that these direct quotations would have been the same as a court reporter. People would simply do their best to record, as closely as possible what they heard. To be honest to the text is to say that people pretty well agreed what was said, consistent with a pre-technological understanding of what a quote meant.
Sorry, now I get the point of the question - how does a narrator know exactly what God said to Jonah. Ultimately, there are several valid hypotheses, depending on your understanding of the work
Especially considering how abruptly Jonah ends*, we have no extant information. That said, there is nothing that would preclude the answer from being something as simple as "Jonah saw he was wrong, repented again, and told people how God changed his mind."
Somehow, the narrator would have had to know the events of the story. No eyewitness was with Jonah in Israel, on the boat to Tarsus, and in Nineveh, other than Jonah himself. As a preaching prophet, it is not unreasonable to assume that Jonah could have given a testimony to an emaneusis who would then have recorded it.
*Commentators have often noted that Jonah ends before we even know if Jonah himself repented. The Veggie Tales movie really does get it right - Jonah doesn't have an ending! So, they call out the point - that God is the god of second chances, and that it is up to the reader to decide.
Alternatively, one could call it historical fiction, in which the author, knowing the events of the story, could interpolate the lesson God was giving through Jonah.
This being a book of prophecy, it may not be intended to be taken as history. The Jews did not place the Prophets with the historical accounts. They placed the Nevi'im (the Prophets) after the Ketu'vim (the Writings). Prophets never claim to be historians - they claim to speak the truth that God gave them.
Pastors are exercising the role of prophet, not when they claim to know what is occurring in the future, but rather when they make clear God's understanding of the present. Martin Luther King, Jr., for example, is rightly called a Prophet of the Civil Rights movement, and famously drew from Amos - another of the prophets - when saying that justice will rain down like mighty waters.
In the same that [martin Luther King] "had a dream," so too this may have been Jonah's perception. I would ask, for example, whether the detail of MLK actually being asleep and having subconscious thoughts of children of all races playing together on Stone Mountain is as important as the vision that this gives us all. How, after all, did MLK record that "dream?" I odoubt that it arrived completely unformed and then entered his speech improvisationally. So too, it may have been, whether Jonah directly wrote what he learned or another prophet extrapolated the message from the events of his life.
When a prophet uses an illustration, he has a different purpose than an historian. An illustration should not be demonstrably false, but if details are glossed over (such as whether this was an actual, audible voice or just an epiphany had) that are irrelevant, than it is held to that standard.
When Medger Evers is hailed as a martyr for the cause of Civil Rights, that is a fully valid illustration, even if he was more likely just an unfortunate victim. The truth is that his death bore witness (marturion!) regardless of whether or not he held discourse with his assailants as docu-dramas have been known to include.
Note also, even if Medger Evers' exploits took on an air of outsized glory, it does not detract at all from the historicity of what he did.
Just as a pastor might create a dialogue within an illustration to set up the historical point, so too the author of Jonah was interested in the theological point, and could thus back into the dialogue that God had with Jonah. The creeping vine, in particular, was a Persian motif, and would thus have been accessible to the hearer.
tl;dr: You don't have to take a position on the historicity of the events to get the idea that God's truth is being conveyed in the whole story.