zidane-ui/ server
Server helpers that serve a zidane chat engine to the browser over HTTP and SSE.
import { … } from 'zidane-ui/server'ui/src/server/index.ts
Use this entry point on Node or Bun to serve createSseHost. createSseHandler returns a standard (request: Request) => Promise<Response> that runs the chat engine. createLocalEnvironment builds the agent behind it. The offline createScriptedProvider and demoTools are in zidane-ui/server/testing. Browser builds fail to resolve this entry. See Connecting a host.
import { createLocalEnvironment, createSseHandler } from 'zidane-ui/server'
import { createScriptedProvider, demoTools } from 'zidane-ui/server/testing'
const handler = createSseHandler({
environment: createLocalEnvironment({ provider: createScriptedProvider(), tools: demoTools }),
cwd: process.cwd(),
basePath: '/api/chat',
authorize: () => true,
})
Bun.serve({ port: 4380, idleTimeout: 0, fetch: handler })Functions#
createLocalEnvironmentFunction#
Creates an EnvironmentFactory for a self-contained environment: one provider, a fixed tool set, sessions in a store you pass, and no user config, MCP servers, or extensions. Call its dispose() when done: without a dataDir option it removes the temp directory the environment made.
function createLocalEnvironment(options: LocalEnvironmentOptions): EnvironmentFactoryParameters
| Name | Type | Description |
|---|---|---|
| options | LocalEnvironmentOptions | The provider and optional model, tools, system prompt, store, data directory, and settings. |
Returns
createSseHandlerFunction#
Creates a Web-standard request handler that runs zidane's chat engine and serves it over HTTP and server-sent events, for createSseHost to talk to. Every request goes through authorize first, and the agent's tools run on this machine.
function createSseHandler(options: SseHandlerOptions): SseHandlerParameters
| Name | Type | Description |
|---|---|---|
| options | SseHandlerOptions | The environment, working directory, and |
Returns
Constants#
LOCAL_PROVIDER_KEYConstant#
The provider key sessions run under in a local environment.
const LOCAL_PROVIDER_KEY = 'local'Interfaces#
EnvironmentFactoryInterface#
Builds a chat engine environment. Receives the broadcast function the engine uses to publish signals to clients.
interface EnvironmentFactoryProperties
| Name | Type | Description |
|---|---|---|
| dispose? | () => void | Releases what the factory made for its environments. |
LocalEnvironmentOptionsInterface#
Options for createLocalEnvironment.
interface LocalEnvironmentOptionsProperties
| Name | Type | Description |
|---|---|---|
| dataDir? | string | Scratch directory for the engine's own files. Default: a temp dir made on first use and removed by |
| model? | string | Model id passed to the provider (default: the provider's |
| provider | Provider | The provider every session runs on. |
| settings? | Partial<HostSettings> | Settings overrides; tool approvals ( |
| store? | SessionStore | Where sessions persist. Default: in memory, lost on restart. |
| system? | string | System prompt. Default: zidane's built-in one. |
| tools? | Record<string, ToolDef> | The agent's tools. Default none: the agent can only talk. |
SseHandlerInterface#
A request handler from createSseHandler: a Web-standard function from Request to Response that also exposes the engine and a close method.
interface SseHandlerProperties
| Name | Type | Description |
|---|---|---|
| close | () => Promise<void> | Stop the engine, end every stream, and remove the environment's temp directory. |
| engine | () => Promise<ChatHost> | The engine. Starts it when no request has started it yet. |
SseHandlerOptionsInterface#
Options for createSseHandler.
interface SseHandlerOptionsProperties
| Name | Type | Description |
|---|---|---|
| authorize | (request: Request) => boolean | Promise<boolean> | Called on every request before anything else; resolve false to answer 401.
Required: pass |
| basePath? | string | Path the handler is mounted at (default |
| cwd | string | Working directory of every new session: the agent's tools act here. |
| environment | EnvironmentFactory | zidane's chat environment: |
| heartbeatMs? | number | Keep-alive comment interval in ms (default 15000). |
| models? | readonly ChatModelProvider[] | What |
| newSession? | Omit<CreateSessionRequest, 'title' | 'cwd'> | Extra fields for every new session ( |
| replayLimit? | number | Stream records kept per session for |