Skip to Content
AI

React

useSignetChat: a chat hook for a streamChat route, with usage.

Usage

import { useSignetChat } from "@dapplelight/signet-ai/react"
import { getCsrfToken } from "./csrf"

export function SupportChat() {
  const { messages, sendMessage, status, stop, usage } = useSignetChat({
    api: "/api/chat",
    headers: () => ({ "x-csrf-token": getCsrfToken() }),
  })

  return (
    <form onSubmit={(e) => { e.preventDefault(); sendMessage({ text: "Where is order A1042?" }) }}>
      {messages.map((m) => <p key={m.id}>{m.parts.map((p) => (p.type === "text" ? p.text : null))}</p>)}
      <small>{usage.inputTokens + usage.outputTokens} tokens</small>
      {status === "streaming" && <button type="button" onClick={stop}>Stop</button>}
    </form>
  )
}

Needs ai and @ai-sdk/react. The server half is streamChat in Chat.

API

ExportWhat it does
useSignetChat(options?)useChat pointed at a streamChat route, plus usage
sumUsage(messages)Sums metadata.usage over assistant messages
UseSignetChatOptionsType
ChatMessageType, re-exported from Chat

useSignetChat(options)

OptionTypeDefault
apistring"/api/chat"Route that returns streamChat(...)
headersobject or functionnonePass a function for a value that changes
otheruseChat optionsPassed through, except transport

api and headers are read once per chat id. Everything useChat returns is passed through.