Use a part before the reply is over
Solid says when one part of a reply is finished, which is before the whole reply is. So a screen can use the seats while the reason is still being written.
pnpm add @niscorp/solid zodThe shape
The schema is the shape you asked the model for. Solid needs it before any text arrives.
import { z } from 'zod';
import { createStream } from '@niscorp/solid';
const Suggestion = z.object({
show: z.string(),
seats: z.array(z.string()),
price_cents: z.number(),
why: z.string(),
});One part of the stream
select gives you a stream of one part, by its path. It has the same on, onFinal and final as the whole.
onFinal is called once: when the name of the next key has been read, or when the list or object around the part closes. For an item of a list that is when the next item begins.
const stream = createStream({ schema: Suggestion });
let piece = 0;
stream.select('seats.0').onFinal((seat) => console.log('piece', piece, '· first seat finished:', seat));
stream.select('seats').onFinal((seats) => console.log('piece', piece, '· seats finished:', seats));
stream.onFinal(() => console.log('piece', piece, '· the reply finished'));The pieces
The same reply as before, in four pieces. Parts finish in the order the model writes them, which is usually the order of the schema you gave it. Put first what the screen needs first.
const pieces = ['{"show":"The Tem', 'pest","seats":["C4",', '"C5"],"price_cents":76', '00,"why":"Row C."}'];
for (const text of pieces) {
piece += 1;
stream.write(text);
}Run it.
Both are said in the third piece: the first seat because the second seat began, the list because the price was named. The reply is finished one piece later.
npx tsx parts.mtspiece 3 · first seat finished: C4 piece 3 · seats finished: [ 'C4', 'C5' ] piece 4 · the reply finished