Interface UseStreamOptions<StateType, Bag>

interface UseStreamOptions<StateType, Bag> {
    apiKey?: string;
    apiUrl?: string;
    assistantId: string;
    callerOptions?: AsyncCallerParams;
    client?: Client<DefaultValues, DefaultValues, unknown>;
    defaultHeaders?: Record<string, HeaderValue>;
    fetchStateHistory?: boolean | {
        limit: number;
    };
    initialValues?: null | StateType;
    messagesKey?: string;
    onCreated?: ((run) => void);
    onCustomEvent?: ((data, options) => void);
    onError?: ((error, run) => void);
    onFinish?: ((state, run) => void);
    onLangChainEvent?: ((data) => void);
    onMetadataEvent?: ((data) => void);
    onStop?: ((options) => void);
    onThreadId?: ((threadId) => void);
    onUpdateEvent?: ((data) => void);
    reconnectOnMount?: boolean | (() => RunMetadataStorage);
    threadId?: null | string;
}

Type Parameters

  • StateType extends Record<string, unknown> = Record<string, unknown>
  • Bag extends BagTemplate = BagTemplate

Properties

apiKey?: string

The API key to use.

apiUrl?: string

The URL of the API to use.

assistantId: string

The ID of the assistant to use.

callerOptions?: AsyncCallerParams

Custom call options, such as custom fetch implementation.

client?: Client<DefaultValues, DefaultValues, unknown>

Client used to send requests.

defaultHeaders?: Record<string, HeaderValue>

Default headers to send with requests.

fetchStateHistory?: boolean | {
    limit: number;
}

Whether to fetch the history of the thread. If true, the history will be fetched from the server. Defaults to 1000 entries. If false, only the last state will be fetched from the server.

Type declaration

  • limit: number

Default

true
initialValues?: null | StateType

Initial values to display immediately when loading a thread. Useful for displaying cached thread data while official history loads. These values will be replaced when official thread data is fetched.

Note: UI components from initialValues will render immediately if they're predefined in LoadExternalComponent's components prop, providing instant cached UI display without server fetches.

messagesKey?: string

Specify the key within the state that contains messages. Defaults to "messages".

Default

"messages"
onCreated?: ((run) => void)

Callback that is called when a new stream is created.

Type declaration

    • (run): void
    • Parameters

      • run: RunCallbackMeta

      Returns void

onCustomEvent?: ((data, options) => void)

Callback that is called when a custom event is received.

Type declaration

    • (data, options): void
    • Parameters

      • data: GetCustomEventType<Bag>
      • options: {
            mutate: ((update) => void);
        }
        • mutate: ((update) => void)
            • (update): void
            • Parameters

              Returns void

      Returns void

onError?: ((error, run) => void)

Callback that is called when an error occurs.

Type declaration

    • (error, run): void
    • Parameters

      • error: unknown
      • run: undefined | RunCallbackMeta

      Returns void

onFinish?: ((state, run) => void)

Callback that is called when the stream is finished.

Type declaration

    • (state, run): void
    • Parameters

      Returns void

onLangChainEvent?: ((data) => void)

Callback that is called when a LangChain event is received.

Type declaration

    • (data): void
    • Parameters

      • data: {
            data: unknown;
            event: string & {} | "on_chain_start" | "on_chain_end" | "on_chat_model_start" | "on_chat_model_stream" | "on_chat_model_end" | "on_chain_stream" | "on_tool_start" | "on_tool_stream" | "on_tool_end" | "on_prompt_start" | "on_prompt_stream" | "on_prompt_end" | "on_llm_start" | "on_llm_stream" | "on_llm_end" | "on_retriever_start" | "on_retriever_stream" | "on_retriever_end";
            metadata: Record<string, unknown>;
            name: string;
            parent_ids: string[];
            run_id: string;
            tags: string[];
        }
        • data: unknown
        • event: string & {} | "on_chain_start" | "on_chain_end" | "on_chat_model_start" | "on_chat_model_stream" | "on_chat_model_end" | "on_chain_stream" | "on_tool_start" | "on_tool_stream" | "on_tool_end" | "on_prompt_start" | "on_prompt_stream" | "on_prompt_end" | "on_llm_start" | "on_llm_stream" | "on_llm_end" | "on_retriever_start" | "on_retriever_stream" | "on_retriever_end"
        • metadata: Record<string, unknown>
        • name: string
        • parent_ids: string[]
        • run_id: string
        • tags: string[]

      Returns void

onMetadataEvent?: ((data) => void)

Callback that is called when a metadata event is received.

Type declaration

    • (data): void
    • Parameters

      • data: {
            run_id: string;
            thread_id: string;
        }
        • run_id: string
        • thread_id: string

      Returns void

onStop?: ((options) => void)

Callback that is called when the stream is stopped by the user. Provides a mutate function to update the stream state immediately without requiring a server roundtrip.

Type declaration

    • (options): void
    • Parameters

      • options: {
            mutate: ((update) => void);
        }
        • mutate: ((update) => void)
            • (update): void
            • Parameters

              Returns void

      Returns void

Example

onStop: ({ mutate }) => {
mutate((prev) => ({
...prev,
ui: prev.ui?.map(component =>
component.props.isLoading
? { ...component, props: { ...component.props, stopped: true, isLoading: false }}
: component
)
}));
}
onThreadId?: ((threadId) => void)

Callback that is called when the thread ID is updated (ie when a new thread is created).

Type declaration

    • (threadId): void
    • Parameters

      • threadId: string

      Returns void

onUpdateEvent?: ((data) => void)

Callback that is called when an update event is received.

Type declaration

    • (data): void
    • Parameters

      Returns void

reconnectOnMount?: boolean | (() => RunMetadataStorage)

Will reconnect the stream on mount

Type declaration

    • (): RunMetadataStorage
    • Returns RunMetadataStorage

threadId?: null | string

The ID of the thread to fetch history and current values from.