new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Core · all subjects

streamtext core

6 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

streamText stream abortion event type

When iterating over result.stream, the stream can emit an 'abort' event type. You can handle this event directly in the stream by checking for part.type === 'abort' in the stream loop.

onAbort callback use cases

The onAbort callback is particularly useful for: persisting partial conversation history to database, saving partial progress for later continuation, cleaning up server-side resources or connections, and logging abort events for analytics.

createUIMessageStreamResponse usage in rate-limited endpoint

When using streamText with Upstash rate limiting, wrap the stream response using `createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }) })` after checking rate limit success.

maxDuration configuration for streaming

The maxDuration export should be set to allow streaming responses up to 30 seconds: `export const maxDuration = 30;`

Caching example with streamText and onEnd callback

Example of caching with streamText and onEnd callback: ```tsx const result = streamText({ model: __MODEL__, messages: await convertToModelMessages(messages), async onEnd({ text }) { await redis.set(key, text); await redis.expire(key, 60 * 60); }, }); ``` The onEnd callback receives the generated text, which can be stored in a cache with an expiration time.

Caching middleware example with wrapStream

Example of caching middleware using wrapStream: ```ts wrapStream: async ({ doStream, params }) => { const cacheKey = JSON.stringify(params); const cached = await redis.get(cacheKey); if (cached !== null) { const formattedChunks = (cached as LanguageModelV4StreamPart[]).map(p => { if (p.type === 'response-metadata' && p.timestamp) { return { ...p, timestamp: new Date(p.timestamp) }; } else return p; }); return { stream: simulateReadableStream({ initialDelayInMs: 0, chunkDelayInMs: 10, chunks: formattedChunks, }), }; } const { stream, ...rest } = await doStream(); const fullResponse: LanguageModelV4StreamPart[] = []; const transformStream = new TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart>({ transform(chunk, controller) { fullResponse.push(chunk); controller.enqueue(chunk); }, flush() { redis.set(cacheKey, fullResponse); }, }); return { stream: stream.pipeThrough(transformStream), ...rest, }; } ``` This pattern checks the cache for previously streamed responses, returns them via simulateReadableStream, and stores new responses after streaming completes.

Give your agent this brain