Forward a streamed reply up to its stop sequence
Your task
Your agent streams its model's reply to the user as chunks arrive. The reply ends where a stop sequence such as \nObservation: starts, and the user must never see any part of one. Text that could still turn into a stop sequence has to wait, but nothing else should.
Write forward_chunks(chunks, stops) that returns the text the agent forwards after each chunk.
Input and output:
chunks: the pieces of the reply in arrival order, up to 20,000 strings with up to 100,000 characters in all. A chunk may be empty.stops: 1 to 4 different strings, each 1 to 20 characters long. No stop sequence appears inside another one.- All text is printable ASCII plus the newline character.
- Return a list with one string per chunk and then one more: the text forwarded right after each chunk arrives, and last, the text forwarded when the stream ends.
Rules:
- The reply is all the chunks joined in order. The user may see only the text before the first place where a stop sequence starts.
- Forward each character as soon as the text received so far proves it cannot be part of a stop sequence, whatever arrives next. Hold back the rest.
- Once a whole stop sequence has arrived, the reply is over. Forward the text before it that has not gone out yet. Every later string in the list, the last one included, is
"". - When the stream ends with no stop sequence, the last string is everything still held back.
Examples
forward_chunks(chunks: list[str], stops: list[str]) -> list[str]Example 1
Input
forward_chunks(["The answer is 42.\nObs", "ervation: done"], ["\nObservation:"])Output
["The answer is 42.", "", ""]Example 2
Input
forward_chunks(["Total: 3\nOb", "jects: 5"], ["\nObservation:"])Output
["Total: 3", "\nObjects: 5", ""]Example 3
Input
forward_chunks(["Done. </", "answer"], ["</answer>", "STOP"])Output
["Done. ", "", "</answer"]