Edit

withContext()

Environment: client.

BetaTelefunc Stream is in beta: breaking changes may occur in any version update.

withContext(telefunction, context) from telefunc/client wraps a telefunction with per-call client context — applied to that one call instead of the global client-side config. Use it for an AbortSignal, extra headers, a URL override, or per-call transport overrides.

// Environment: client
 
import { onAIChat } from './Chat.telefunc'
import { withContext } from 'telefunc/client'
 
const call = withContext(onAIChat, {
  signal,                            // abort this call (and any channels it opens)
  headers: { 'x-trace': traceId },   // extra HTTP headers, just for this call
  telefuncUrl: '/api/_telefunc',     // override config.telefuncUrl for this call
  stream: { transport: 'channel' },  // override config.stream.transport
  channel: { transports: ['ws'] },   // override config.channel.transports
})
 
const gen = call('Tell me a joke')
for await (const message of gen) {
  console.log(message)
}

withContext() returns a wrapped function with the same signature — call it exactly like the original telefunction.

Options

OptionTypeDescription
signalAbortSignalCancel this call and any channels it opens. See also: API > close().
headersRecord<string, string>Extra HTTP headers for this call. Global default: config.headers.
telefuncUrlstringOverride config.telefuncUrl for this call and its channels.
stream.transportStream transportOverride config.stream.transport for streaming.
channel.transportsChannel transportOverride config.channel.transports for channels.
channel.connectionKeystringBy default, all channel calls to the same URL share one connection. Set a key to split calls across separate connections: calls with the same key share a connection, while different keys each get their own (keyless calls all share the default connection).
channel.idleTimeoutnumberHow long (ms) to keep the underlying connection open after all channels close. Default 60_000; 0 closes it immediately.

See also