Server transport API

Open in

The server transport publishes to an Ably channel, manages the turn lifecycle, and routes cancel signals from clients to active turns. It is the server-side counterpart to the client transport.

Import from the core entry point:

JavaScript

1

import { createServerTransport } from '@ably/ai-transport'

Or use the Vercel entry point which pre-binds the codec.

createServerTransport

Factory function that creates a ServerTransport instance.

JavaScript

1

function createServerTransport(options: ServerTransportOptions): ServerTransport

ServerTransportOptions

PropertyRequiredTypeDescription
channelrequiredAbly.RealtimeChannelThe Ably channel for the session.
codecrequiredCodec<TEvent, TMessage>Codec for encoding and decoding messages. See Codec API.
loggeroptionalLoggerLogger instance for debug output.
onErroroptional(error: ErrorInfo) => voidCallback for transport-level errors.

ServerTransport

The ServerTransport object returned by the factory.

newTurn

Create a new turn. Returns a Turn object for managing the turn lifecycle.

JavaScript

1

function newTurn(options: NewTurnOptions): Turn

close

Close the transport and release resources.

JavaScript

1

function close(): void

NewTurnOptions

PropertyRequiredTypeDescription
turnIdrequiredstringUnique identifier for this turn.
clientIdoptionalstringThe client that initiated this turn. Used for scoping cancel signals.
parentoptionalstring | nullThe ID of the parent node in the conversation tree. Used for branching.
forkOfoptionalstringThe ID of the node this turn forks from, for edit and regenerate operations.
onCanceloptional(request: CancelRequest) => Promise<boolean>Callback fired when a cancel signal matches this turn. Return true to allow cancellation, false to reject it. If not provided, all cancels are accepted.
onAbortoptional(write: (event: TEvent) => Promise<void>) => void | Promise<void>Callback fired when the turn is aborted. Receives a write function to publish final events before the abort finalizes.
onMessageoptional(message: Ably.Message) => voidHook called before each Ably message is published in this turn. Mutate the message in place to add custom headers.
onErroroptional(error: ErrorInfo) => voidCallback for errors during this turn.

Turn

A Turn object manages the lifecycle of a single turn on the server.

start

Announce the start of the turn on the channel. Call this before publishing any messages.

JavaScript

1

function start(): Promise<void>

addMessages

Publish messages to the channel. Use this to publish user messages or any discrete messages that are not part of a stream.

JavaScript

1

function addMessages(messages: MessageNode<TMessage>[], options?: { clientId?: string }): Promise<AddMessagesResult>

streamResponse

Pipe a ReadableStream of events through the codec and publish them to the channel. Returns when the stream completes, with a reason indicating how it ended.

JavaScript

1

function streamResponse(stream: ReadableStream<TEvent>, options?: StreamResponseOptions): Promise<{ reason: TurnEndReason }>

The stream is piped through the codec's encoder, which converts events into Ably publish operations. If the turn is cancelled while streaming, the stream is aborted and the returned reason is 'cancelled'.

addEvents

Publish a batch of events to the channel without streaming. Useful for publishing pre-computed events or events from a non-streaming source.

JavaScript

1

function addEvents(nodes: EventsNode<TEvent>[]): Promise<void>

end

End the turn with the given reason. Publishes a turn-end marker to the channel.

JavaScript

1

function end(reason: TurnEndReason): Promise<void>

abortSignal

An AbortSignal that fires when the turn is cancelled. Pass this to your LLM call to stop generation when a cancel signal is received.

JavaScript

1

2

3

4

5

const result = await streamText({
  model: openai('gpt-4o'),
  messages,
  abortSignal: turn.abortSignal,
})

CancelRequest

The object passed to the onCancel callback when a cancel signal matches a turn.

PropertyTypeDescription
messageAbly.InboundMessageThe raw Ably message that carried the cancel signal.
filterCancelFilterThe parsed cancel scope from the message headers.
matchedTurnIdsstring[]The active turn IDs that would be cancelled if allowed.
turnOwnersMap<string, string>Map of turn ID to the owner's client ID for the matched turns.

CancelFilter

PropertyTypeDescription
owntrueCancel all turns started by the requesting client.
turnIdstringCancel a specific turn by ID.
clientIdstringCancel all turns by a specific client.
alltrueCancel all active turns on the channel.

Only one property should be set per filter. If no filter is provided, the default is { own: true }.

TurnEndReason

JavaScript

1

type TurnEndReason = 'complete' | 'cancelled' | 'error'
ValueDescription
'complete'The turn finished normally.
'cancelled'The turn was cancelled by a client signal.
'error'The turn ended due to an error.