withContext()
Environment: client.
Beta — Telefunc 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
| Option | Type | Description |
|---|---|---|
signal | AbortSignal | Cancel this call and any channels it opens. See also: API > close(). |
headers | Record<string, string> | Extra HTTP headers for this call. Global default: config.headers. |
telefuncUrl | string | Override config.telefuncUrl for this call and its channels. |
stream.transport | Stream transport | Override config.stream.transport for streaming. |
channel.transports | Channel transport | Override config.channel.transports for channels. |
channel.connectionKey | string | By 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.idleTimeout | number | How long (ms) to keep the underlying connection open after all channels close. Default 60_000; 0 closes it immediately. |