FluxyChat

Guides

Stream Transformations

Stream transforms modify AI stream parts in real-time — adding delays, filtering, or transforming content as it flows from the model to the consumer.

Stream Transformations

Stream transforms modify AI stream parts in real-time — adding delays, filtering, or transforming content as it flows from the model to the consumer.

smoothStream

Adds configurable delay between text chunks for a smooth streaming UX:

import { smoothStream } from '@fluxy-chat/agent';

const stream = await model.stream({ prompt: [...] });
const smoothed = stream.pipeThrough(smoothStream({
  delayInMs: 30,   // 30ms between chunks
  chunkSize: 1,    // 1 character per chunk
}));

// Consume the smoothed stream
const reader = smoothed.getReader();

Options

OptionTypeDefaultDescription
delayInMsnumber50Milliseconds delay between chunks
chunkSizenumber1Characters per chunk
wordDelaybooleanfalseChunk by words instead of characters

Word-Level Delay

const stream = modelStream.pipeThrough(smoothStream({
  wordDelay: true,
  delayInMs: 100,  // 100ms between words
}));

experimental_transform

Custom part-by-part transformation function:

import { experimental_transform } from '@fluxy-chat/agent';

const stream = modelStream.pipeThrough(experimental_transform({
  transform: (part) => {
    if (part.type === 'text-delta') {
      // Uppercase all text
      return { ...part, delta: part.delta.toUpperCase() };
    }
    if (part.type === 'reasoning-delta') {
      // Skip reasoning parts
      return null;
    }
    return part;
  },
}));

Multiple Outputs

Return an array to split one part into multiple:

experimental_transform({
  transform: (part) => {
    if (part.type === 'text-delta') {
      return [
        { type: 'text-delta', id: part.id, delta: part.delta[0] ?? '' },
        { type: 'text-delta', id: part.id, delta: part.delta.slice(1) },
      ];
    }
    return part;
  },
});

Returning null

Return null to drop a part entirely.

Chaining Transforms

Transforms compose with .pipeThrough():

const stream = modelStream
  .pipeThrough(experimental_transform({ transform: filterToolParts }))
  .pipeThrough(smoothStream({ delayInMs: 20 }));

Type Reference

interface SmoothStreamOptions {
  delayInMs?: number;
  chunkSize?: number;
  wordDelay?: boolean;
}

function smoothStream(options?: SmoothStreamOptions): TransformStream<AIStreamPart, AIStreamPart>;

type StreamTransformFunction = (
  part: AIStreamPart,
) => AIStreamPart | AIStreamPart[] | null | Promise<AIStreamPart | AIStreamPart[] | null>;

function experimental_transform(
  options: ExperimentalTransformOptions,
): TransformStream<AIStreamPart, AIStreamPart>;

See Also

On this page