MCPcopy Create free account
hub / github.com/TanStack/ai / buildSessionUpdate

Function buildSessionUpdate

packages/ai-openai/src/realtime/session-update.ts:16–82  ·  view source on GitHub ↗
(
  config: Partial<RealtimeSessionConfig>,
)

Source from the content-addressed store, hash-verified

14 * never sent; the adapter logs when it drops the option.
15 */
16export function buildSessionUpdate(
17 config: Partial<RealtimeSessionConfig>,
18): Record<string, unknown> {
19 // Always enable input audio transcription so user speech is transcribed
20 const audioInput: Record<string, unknown> = {
21 transcription: { model: 'whisper-1' },
22 }
23
24 if (config.vadMode) {
25 if (config.vadMode === 'semantic') {
26 audioInput.turn_detection = {
27 type: 'semantic_vad',
28 eagerness: config.semanticEagerness ?? 'medium',
29 }
30 } else if (config.vadMode === 'server') {
31 audioInput.turn_detection = {
32 type: 'server_vad',
33 threshold: config.vadConfig?.threshold ?? 0.5,
34 prefix_padding_ms: config.vadConfig?.prefixPaddingMs ?? 300,
35 silence_duration_ms: config.vadConfig?.silenceDurationMs ?? 500,
36 }
37 } else {
38 audioInput.turn_detection = null
39 }
40 }
41
42 const audio: Record<string, unknown> = { input: audioInput }
43
44 if (config.voice) {
45 audio.output = { voice: config.voice }
46 }
47
48 const sessionUpdate: Record<string, unknown> = {
49 type: 'realtime',
50 audio,
51 }
52
53 if (config.instructions) {
54 sessionUpdate.instructions = config.instructions
55 }
56
57 if (config.tools !== undefined) {
58 sessionUpdate.tools = config.tools.map((t) => ({
59 type: 'function',
60 name: t.name,
61 description: t.description,
62 parameters: t.inputSchema ?? { type: 'object', properties: {} },
63 }))
64 sessionUpdate.tool_choice = 'auto'
65 }
66
67 if (config.outputModalities) {
68 // GA only supports a single output modality: ['audio'] or ['text']
69 // (Beta accepted ['audio', 'text']). Audio replies still stream text
70 // via `response.output_audio_transcript.*` events, so collapsing
71 // ['audio', 'text'] to ['audio'] preserves the visible behavior.
72 sessionUpdate.output_modalities = config.outputModalities.includes('audio')
73 ? ['audio']

Callers 2

updateSessionFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected