Skip to content

Chat Runtime

Defined in: chat/runtime.ts:101

Information about a registered backend

name: string

Defined in: chat/runtime.ts:103

Backend name (key in backends map)


Defined in: chat/runtime.ts:56

Configuration for creating a chat runtime via createChatRuntime()

backends: Record<string, BackendAdapterFactory>

Defined in: chat/runtime.ts:58

Map of backend name → adapter factory (lazy creation on first use)

optional context: ContextWindowConfig

Defined in: chat/runtime.ts:64

Context window configuration (optional)

defaultBackend: string

Defined in: chat/runtime.ts:60

Default backend name (must be a key in backends)

optional middleware: ChatMiddleware[]

Defined in: chat/runtime.ts:66

Middleware pipeline (optional, applied in order)

optional onContextTrimmed: (sessionId, removedMessages) => void

Defined in: chat/runtime.ts:79

Called when context trimming removes messages. Use for archiving, logging, or analytics.

ChatIdLike

ChatMessage<unknown>[]

void

optional retryConfig: StreamRetryConfig

Defined in: chat/runtime.ts:68

Retry configuration for pre-stream connection errors

sessionStore: IChatSessionStore

Defined in: chat/runtime.ts:62

Session store for persistence

optional streamTimeoutMs: number

Defined in: chat/runtime.ts:74

Stream inactivity timeout in milliseconds (optional). When set, aborts the stream if no events arrive within this window. Timer resets after each received event.

optional tools: ToolDefinition<unknown>[]

Defined in: chat/runtime.ts:84

Initial tools to register on the runtime. Equivalent to calling registerTool() for each tool after creation.


Defined in: chat/runtime.ts:135

Client-side interface for interacting with a remote chat server. Fully self-contained — no shared base with IChatRuntime. Extends IProviderClient for provider CRUD (ISP). Used by React components and remote clients.

TMetadata extends Record<string, unknown> = Record<string, unknown>

Type-level convenience for message metadata. NOT enforced at the storage boundary — session stores always use unknown. Consumers are responsible for metadata shape consistency.

readonly activeSessionId: ChatId | null

Defined in: chat/runtime.ts:150

readonly selectedProviderId: string | null

Defined in: chat/runtime.ts:165

readonly status: RuntimeStatus

Defined in: chat/runtime.ts:139

abort(): void

Defined in: chat/runtime.ts:161

void

createProvider(config): Promise<ProviderConfig>

Defined in: chat/runtime.ts:115

Omit<ProviderConfig, "id" | "createdAt">

Promise<ProviderConfig>

IProviderClient.createProvider

createSession(options): Promise<ChatSession<TMetadata>>

Defined in: chat/runtime.ts:143

CreateSessionOptions<TMetadata>

Promise<ChatSession<TMetadata>>

deleteProvider(id): Promise<void>

Defined in: chat/runtime.ts:117

string

Promise<void>

IProviderClient.deleteProvider

deleteSession(id): Promise<void>

Defined in: chat/runtime.ts:146

ChatIdLike

Promise<void>

dispose(): Promise<void>

Defined in: chat/runtime.ts:140

Promise<void>

getContextStats(sessionId): Promise<ContextStats | null>

Defined in: chat/runtime.ts:176

ChatIdLike

Promise<ContextStats | null>

getSession(id): Promise<ChatSession<TMetadata> | null>

Defined in: chat/runtime.ts:144

ChatIdLike

Promise<ChatSession<TMetadata> | null>

listBackends(): Promise<BackendInfo[]>

Defined in: chat/runtime.ts:173

Promise<BackendInfo[]>

listModels(): Promise<ModelInfo[]>

Defined in: chat/runtime.ts:172

Promise<ModelInfo[]>

listProviders(): Promise<ProviderConfig[]>

Defined in: chat/runtime.ts:114

Promise<ProviderConfig[]>

IProviderClient.listProviders

listSessions(options?): Promise<ChatSession<TMetadata>[]>

Defined in: chat/runtime.ts:145

SessionListOptions

Promise<ChatSession<TMetadata>[]>

onSelectionChange(callback): () => void

Defined in: chat/runtime.ts:166

SelectionChangeCallback

(): void

void

onSessionChange(callback): () => void

Defined in: chat/runtime.ts:169

() => void

(): void

void

selectProvider(providerId): void

Defined in: chat/runtime.ts:164

string

void

send(sessionId, message, options?): AsyncIterable<ChatEvent>

Defined in: chat/runtime.ts:158

Send a message. Options are optional — the server handler resolves model and backend from provider selection state. Compare with IChatRuntime.send() where RuntimeSendOptions is required.

ChatIdLike

string

SendMessageOptions

AsyncIterable<ChatEvent>

switchSession(id): Promise<ChatSession<TMetadata>>

Defined in: chat/runtime.ts:149

ChatIdLike

Promise<ChatSession<TMetadata>>

updateProvider(id, changes): Promise<void>

Defined in: chat/runtime.ts:116

string

Partial<Omit<ProviderConfig, "id" | "createdAt">>

Promise<void>

IProviderClient.updateProvider


Defined in: chat/runtime.ts:192

Server-side chat runtime. Fully self-contained — no shared base with IChatClient. Manages backend adapters, tools, middleware, and context trimming. Does NOT include client-facing provider CRUD or selection — those are handled by the server handler layer.

TMetadata extends Record<string, unknown> = Record<string, unknown>

Type-level convenience for message metadata. NOT enforced at the storage boundary — session stores always use unknown. Casts in ChatRuntime.createSession()/getSession() are intentionally unsafe to provide typed access. Consumers are responsible for metadata shape consistency.

readonly registeredTools: ReadonlyMap<string, ToolDefinition<unknown>>

Defined in: chat/runtime.ts:225

readonly status: RuntimeStatus

Defined in: chat/runtime.ts:195

abort(): void

Defined in: chat/runtime.ts:213

void

createSession(options): Promise<ChatSession<TMetadata>>

Defined in: chat/runtime.ts:199

CreateSessionOptions<TMetadata>

Promise<ChatSession<TMetadata>>

deleteSession(id): Promise<void>

Defined in: chat/runtime.ts:202

ChatIdLike

Promise<void>

dispose(): Promise<void>

Defined in: chat/runtime.ts:196

Promise<void>

getContextStats(sessionId): Promise<ContextStats | null>

Defined in: chat/runtime.ts:232

ChatIdLike

Promise<ContextStats | null>

getSession(id): Promise<ChatSession<TMetadata> | null>

Defined in: chat/runtime.ts:200

ChatIdLike

Promise<ChatSession<TMetadata> | null>

listBackends(): Promise<BackendInfo[]>

Defined in: chat/runtime.ts:220

Promise<BackendInfo[]>

listModels(options?): Promise<ModelInfo[]>

Defined in: chat/runtime.ts:219

string

AuthToken

Promise<ModelInfo[]>

listSessions(options?): Promise<ChatSession<TMetadata>[]>

Defined in: chat/runtime.ts:201

SessionListOptions

Promise<ChatSession<TMetadata>[]>

onSessionChange(callback): () => void

Defined in: chat/runtime.ts:216

() => void

(): void

void

registerTool(tool): void

Defined in: chat/runtime.ts:223

ToolDefinition

void

removeMiddleware(middleware): void

Defined in: chat/runtime.ts:229

ChatMiddleware

void

removeTool(name): void

Defined in: chat/runtime.ts:224

string

void

send(sessionId, message, options): AsyncIterable<ChatEvent>

Defined in: chat/runtime.ts:210

Send a message. RuntimeSendOptions is required on the server — the caller (usually a handler) must supply backend, model, and credentials. Compare with IChatClient.send() where options are optional.

ChatIdLike

string

RuntimeSendOptions

AsyncIterable<ChatEvent>

use(middleware): void

Defined in: chat/runtime.ts:228

ChatMiddleware

void


Defined in: chat/runtime.ts:113

Provider CRUD operations — separated per Interface Segregation Principle. Implemented by IChatClient (which needs provider management for UI). Not required on IChatRuntime (providers are a handler-layer concern).

createProvider(config): Promise<ProviderConfig>

Defined in: chat/runtime.ts:115

Omit<ProviderConfig, "id" | "createdAt">

Promise<ProviderConfig>

deleteProvider(id): Promise<void>

Defined in: chat/runtime.ts:117

string

Promise<void>

listProviders(): Promise<ProviderConfig[]>

Defined in: chat/runtime.ts:114

Promise<ProviderConfig[]>

updateProvider(id, changes): Promise<void>

Defined in: chat/runtime.ts:116

string

Partial<Omit<ProviderConfig, "id" | "createdAt">>

Promise<void>


Defined in: chat/runtime.ts:88

Retry configuration for pre-stream failures (renamed to avoid clash with agent-level RetryConfig)

delayMs: number

Defined in: chat/runtime.ts:92

Delay between retries in milliseconds

maxAttempts: number

Defined in: chat/runtime.ts:90

Maximum number of attempts (default: 1 = no retry)

BackendAdapterFactory = (credentials) => IChatBackend | Promise<IChatBackend>

Defined in: chat/runtime.ts:53

Factory function that creates a backend adapter on demand

AuthToken

IChatBackend | Promise<IChatBackend>


RetryConfig = StreamRetryConfig

Defined in: chat/runtime.ts:96

Use StreamRetryConfig


SelectionChangeCallback = (providerId) => void

Defined in: chat/runtime.ts:123

Callback for provider selection changes

string | null

void

createChatRuntime<TMetadata>(options): IChatRuntime<TMetadata>

Defined in: chat/runtime.ts:920

Create a fully-wired chat runtime from configuration.

TMetadata extends Record<string, unknown> = Record<string, unknown>

ChatRuntimeOptions

Runtime configuration (backends, session store, context, middleware)

IChatRuntime<TMetadata>

IChatRuntime instance ready to use

import { createChatRuntime } from "@witqq/agent-sdk/chat/runtime";
import { InMemorySessionStore } from "@witqq/agent-sdk/chat/sessions";
const runtime = createChatRuntime({
backends: {
copilot: () => new CopilotAdapter({ agentConfig: { model: "gpt-4" } }),
},
defaultBackend: "copilot",
sessionStore: new InMemorySessionStore(),
});