Skip to content
Source
Sign inSign up
TanStack/ai

packages/ai-effect

README.md

TanStack AI

@tanstack/ai-effect

Effect 4 bindings for TanStack AI.

  • chat returns an Effect Stream of chunks.
  • serverTool makes a tool from a function that returns an Effect. A Layer gives the tool its services.
  • toolSchema turns an Effect Schema into a tool schema that works with OpenAI strict mode.
  • Every other activity (images, speech, embeddings, and more) returns an Effect, or a Stream with stream: true.
  • Every failure is a typed AIError. Interruption aborts the request to the provider.

Install

npm install @tanstack/ai @tanstack/ai-effect effect

The package needs effect 4.1 or later.

Stream a chat

import { chatParamsFromRequest, toServerSentEventsResponse } from '@tanstack/ai'
import * as AI from '@tanstack/ai-effect'
import { openaiText } from '@tanstack/ai-openai'
import { Stream } from 'effect'

export async function POST(request: Request) {
  const params = await chatParamsFromRequest(request)

  const stream = AI.chat({
    adapter: openaiText('gpt-6.1-sol'),
    messages: params.messages,
    threadId: params.threadId,
    runId: params.runId,
  })

  return toServerSentEventsResponse(Stream.toAsyncIterable(stream))
}

Docs