Loading AgentsKit ecosystem navigation…
AgentsKit Chat

Server handler

Mount createChatHandler for Web-standard streaming turns with session CAS and auth.

View canonical Markdown

Web-standard server handler

createChatHandler mounts one shared ChatDefinition behind platform Request, Response, ReadableStream, and AbortSignal.

For grounded Q&A after deterministic misses, use createAskServiceHandler.

Turn flow

Loading diagram...

Minimal handler

code
import { createChatHandler } from '@agentskit/chat/server'

const handleChat = createChatHandler({
  authenticate: async request => {
    const identity = await verifyBearer(request)
    return identity
      ? { ok: true, context: identity }
      : { ok: false, response: new Response('Unauthorized', { status: 401 }) }
  },
  resolveDefinition: context => chats.forTenant(context?.tenantId),
  sessionStorage: context => sessions.forTenant(context?.tenantId),
  timeoutMs: 30_000,
})

export const POST = (request: Request) => handleChat(request)

Request / response

DirectionFormat
Request bodyapplication/json encoded client.turn.submit
Success bodyapplication/x-ndjson — one encoded turn event per line
DecodedecodeTurnEvent from @agentskit/chat/protocol

Auth completes before parsing untrusted JSON. Trusted context lives only in the host closure — never from the submit payload.

Sessions and memory

sessionStorage is required: cursor, active-turn lease, and recent terminal turns prevent rollback, concurrent execution, and replay of tool effects.

Limits and failures

DefaultValue
Body limit64 KiB
Deadline30 s

Method, media type, body, event, timeout, cancellation, and internal failures emit safe versioned diagnostics. Request abort / response cancel / timeout stop the upstream AgentsKit controller.

Where to mount

HostPattern
Next / Remix / SvelteKit / edgeExport the handler where Web handlers are supported
Node HTTPTranslate to Request, pipe Response.body
Express / HonoThin bridge; forward disconnect cancellation

Full recipes: Deployment.