4.0.1 • Published 3 months ago

@hackdance/hooks v4.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

!WARNING The agents and hooks packages are no longer maintained and schema-stream has moved here: island-ai Agents has been mostly replaced by zod-stream and the hooks package has moved to stream-hooks - now both part of the island-ai project.

@hackdance/hooks

This package provides three custom React hooks for managing streams, chat streams, and JSON streams.

Installation

To install the package, run the following command:

  pnpm add @hackdance/hooks

Hooks

useStream

useStream is a custom React hook that manages a stream. It provides functionalities to start and stop the stream.

Props

NameTypeDescription
onBeforeStartfunctionOptional callback function that will be invoked before the stream starts.
onStopfunctionOptional callback function that will be invoked when the stream stops.

Usage

  import { useStream } from '@hackdance/hooks';

  const { startStream, stopStream } = useStream({ onBeforeStart: ..., onStop: ... });

useChatStream

useChatStream is a custom React hook that extends the useStream hook to manage a chat stream. It provides functionalities to start and stop the chat stream, manage the chat messages, and manage the loading state.

Props

NameTypeDescription
onReceivefunctionOptional callback function that will be invoked when a message is received.
onEndfunctionOptional callback function that will be invoked when the chat stream ends.
startingMessagesarrayOptional array of starting messages.
ctxobjectOptional static context object, appended to every post body on each startStream invocation .
defaultMethodstringOptional method for the request, one of GETPOST defaults to POST.
defaultHeadersobjectOptional request headers

Usage

  import { useChatStream } from '@hackdance/hooks'

  const { loading, startStream, stopStream, messages, setMessages } = useChatStream({ onBeforeStart: ..., onReceive: ..., onEnd: ..., startingMessages: [] })

startStream

startStream is a function that starts a stream with the provided arguments and parses the incoming stream into JSON. It takes an object as an argument with the following properties:

NameTypeDescription
urlstringThe URL of the stream.
promptstringA string prompt included in the post body on the request to your url.
ctxobjectOptional context object, sent in the post body on the request to your url.
methodstringOptional method for the request, one of GETPOST defaults to POST.
headersobjectOptional request headers

Example:

  startStream({ url: 'http://example.com', prompt: "hey there", ctx: { key: 'value' } });

stopStream

stopStream is a function that stops the stream. It doesn't take any arguments.

Example:

  stopStream();

useJsonStream

useJsonStream is a custom React hook that extends the useStream hook to add JSON parsing functionality. It uses the SchemaStream to parse the incoming stream into JSON.

Props

NameTypeDescription
onReceivefunctionOptional callback function that will be invoked when a JSON object is received.
onEndfunctionOptional callback function that will be invoked when the JSON stream ends.
schemaz.ZodObjectThe zod schema for the JSON data, the top level must be an object.
ctxobjectOptional static context object, appended to every post body on each
startStream invocation .
defaultMethodstringOptional method for the request, one of GETPOST defaults to POST.
defaultHeadersobjectOptional request headers

Usage

  import { useJsonStream } from '@hackdance/hooks';

  const { loading, startStream, stopStream, json } = useJsonStream({ onBeforeStart: ..., onReceive: ..., onStop: ..., onEnd: ..., schema: ..., ctx: {} });

startStream

startStream is a function that starts a stream with the provided arguments and parses the incoming stream into JSON. It takes an object as an argument with the following properties:

NameTypeDescription
urlstringThe URL of the stream.
ctxobjectOptional context object, sent in the post body to your url.
methodstringOptional method for the request, one of GETPOST defaults to POST.
headersobjectOptional request headers

Example:

  startStream({ url: 'http://example.com', ctx: { key: 'value' } });

stopStream

stopStream is a function that stops the stream. It doesn't take any arguments.

Example:

  stopStream();

License

MIT

4.0.1

3 months ago

4.0.0

4 months ago

3.0.1

4 months ago

3.0.0

4 months ago

1.0.0

5 months ago

2.0.0

5 months ago

0.2.0

5 months ago

0.1.1

7 months ago

0.1.0

7 months ago

0.0.6

7 months ago

0.0.5

7 months ago

0.0.4

7 months ago

0.0.3

7 months ago

0.0.2

7 months ago

0.0.1

7 months ago