I wish I could remember what the book was.
[0] https://en.wikipedia.org/wiki/The_Menagerie_(Star_Trek:_The_...
[1] https://en.wikipedia.org/wiki/The_Cage_(Star_Trek:_The_Origi...
Text: http://www.philipkdickfans.com/literary-criticism/rautaavara...
Synopsis: https://en.wikipedia.org/wiki/Rautavaara%27s_Case
Probably not the one you're thinking of, but somewhat related.
When Potter was dying he wrote Karaoke (which was about a dying writer writing a play entitled Karaoke who discovers his characters actually exist)
This is followed by Cold Lazarus which is set in a dystopian future in a lab working on the cryogenically frozen head of the author from Karaoke.
There is a fairly open question as to how much of Karaoke is a prequel and how much of it is a concurrent experience of the mind being experimented upon.
(I liked that one)
Did you know Darth Vader was Luke Skywalkers father?
Ack, maybe I'm missing something here?
/s