Skip to content

@mfui/ai-sdk ​

@mfui/ai-sdk adapts Vercel AI SDK streaming results into MFUI semantic SSE streams.

Use this package when your server already uses streamText() or another AI SDK API that exposes textStream, fullStream, or consumeStream().

createMFUIResponse() ​

Creates an MFUI semantic SSE Response from an AI SDK streaming result.

Import ​

ts
import { createMFUIResponse } from '@mfui/ai-sdk';

Signature ​

ts
function createMFUIResponse(
  result: AISDKMFUIResponseResult,
  mfui: MFUIManifest,
  options?: AISDKMFUIResponseOptions,
): Response

Parameters ​

NameTypeRequiredDefaultDescription
resultAISDKMFUIResponseResultYesn/aAI SDK streaming result.
mfuiMFUIManifestYesn/aComponents available to this request.
optionsAISDKMFUIResponseOptionsNo{}Response, parser, writer, and lifecycle options.

Returns ​

TypeDescription
Responsetext/event-stream response that emits MFUI semantic SSE events.

Example ​

ts
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { createMFUIPrompt } from '@mfui/server';
import { createMFUIResponse } from '@mfui/ai-sdk';

const result = streamText({
  model: openai('gpt-4o-mini'),
  system: [
    'You are a helpful assistant.',
    createMFUIPrompt(mfui),
  ].join('\n\n'),
  messages,
});

return createMFUIResponse(result, mfui, {
  onMessage(message) {
    saveAssistantMessage(message);
  },
});

Notes ​

createMFUIResponse() prefers result.textStream when present, then result.fullStream, and falls back to result.consumeStream().

AISDKMFUIResponseResult ​

AI SDK result shape accepted by createMFUIResponse().

PropertyTypeRequiredDefaultDescription
consumeStream(options)(options?: { onError?: (error: unknown) => void }) => Promise<void>Yesn/aAI SDK method that consumes the stream. Used when textStream and fullStream are missing.
textStreamAsyncIterable<string>Non/aStream of plain text chunks. Preferred when present.
fullStreamAsyncIterable<AISDKMFUIChunk>Non/aStream of AI SDK chunks. Used when textStream is missing.
totalUsagePromiseLike<AISDKUsage> | AISDKUsageNon/aToken usage read after stream consumption. Preferred over usage.
usagePromiseLike<AISDKUsage> | AISDKUsageNon/aToken usage fallback.

AISDKMFUIResponseOptions ​

Options for createMFUIResponse().

PropertyTypeRequiredDefaultDescription
closebooleanNotrueWhether to close the MFUI parser and writer when the AI SDK stream finishes.
parserMFUIBlockParserOptionsNo{}Options passed to createMFUIBlockParser().
writerMFUIStreamWriterOptionsNo{}Options passed to createMFUIStreamWriter().
responseInitResponseInitNo{}Init object passed to the returned Response.
onMessageMFUIMessageHandlerNon/aCalled with the final projected message after the MFUI response finishes.
onErrorMFUIErrorHandlerNon/aCalled when stream processing or MFUI block parsing fails.

AISDKMFUIChunk ​

AI SDK full-stream chunk shape consumed by writeMFUIChunk().

Shape ​

ts
type AISDKMFUIChunk =
  | { type: 'text'; text: string }
  | { type: 'text-delta'; text: string }
  | { type: 'text-delta'; textDelta: string }
  | { type: 'finish'; totalUsage?: AISDKUsage }
  | { type: 'error'; error: unknown }
  | Record<string, unknown>

Notes ​

MFUI reads only text chunks and text-delta chunks. Other chunks are ignored.

AISDKUsage ​

Token usage shape copied to the final MFUI message.end event.

PropertyTypeRequiredDefaultDescription
inputTokensnumberNon/aInput token count.
outputTokensnumberNon/aOutput token count.

createMFUIOnChunk() ​

Creates an AI SDK onChunk callback that writes text chunks into an MFUI block parser.

Import ​

ts
import { createMFUIOnChunk } from '@mfui/ai-sdk';

Signature ​

ts
function createMFUIOnChunk(
  parser: MFUIBlockParser,
): (event: { chunk: AISDKMFUIChunk }) => void

Parameters ​

NameTypeRequiredDefaultDescription
parserMFUIBlockParserYesn/aMFUI block parser receiving AI SDK text chunks.

Returns ​

TypeDescription
(event: { chunk: AISDKMFUIChunk }) => voidCallback that can be passed to AI SDK APIs exposing chunk events.

writeMFUIChunk() ​

Writes one AI SDK full-stream chunk into an MFUI block parser.

Import ​

ts
import { writeMFUIChunk } from '@mfui/ai-sdk';

Signature ​

ts
function writeMFUIChunk(
  chunk: AISDKMFUIChunk,
  parser: MFUIBlockParser,
): void

Parameters ​

NameTypeRequiredDefaultDescription
chunkAISDKMFUIChunkYesn/aAI SDK chunk. Only text chunks are written to the parser.
parserMFUIBlockParserYesn/aMFUI block parser receiving AI SDK text chunks.

Returns ​

TypeDescription
voidReturns nothing.

consumeMFUIStream() ​

Consumes an AI SDK stream through result.consumeStream() and then closes or flushes an MFUI block parser.

Import ​

ts
import { consumeMFUIStream } from '@mfui/ai-sdk';

Signature ​

ts
function consumeMFUIStream(
  result: AISDKConsumeStreamResult,
  parser: MFUIBlockParser,
  options?: { close?: boolean },
): Promise<void>

Parameters ​

NameTypeRequiredDefaultDescription
resultAISDKConsumeStreamResultYesn/aAI SDK result with consumeStream().
parserMFUIBlockParserYesn/aMFUI block parser to close or flush.
options{ close?: boolean }No{}When close is false, calls parser.flush() instead of parser.close().

Returns ​

TypeDescription
Promise<void>Resolves after the AI SDK stream is consumed and the parser is flushed or closed.

AISDKConsumeStreamResult ​

Minimal AI SDK result shape consumed by consumeMFUIStream().

PropertyTypeRequiredDefaultDescription
consumeStream(options)(options?: { onError?: (error: unknown) => void }) => Promise<void>Yesn/aMethod used to consume the AI SDK stream.
totalUsagePromiseLike<AISDKUsage> | AISDKUsageNon/aToken usage read after stream consumption. Preferred over usage.
usagePromiseLike<AISDKUsage> | AISDKUsageNon/aToken usage fallback.