Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

session-update.ts

82 lines2.6 KB

1import type { RealtimeSessionConfig } from '@tanstack/ai'
2​
3/**
4 * Builds the GA-shaped `session.update` payload for OpenAI's realtime API.
5 *
6 * The GA API requires `session.type` on every update and nests audio
7 * settings under `audio.input` / `audio.output` (the flat Beta field names
8 * were retired when the Beta shape was shut down on 2026-05-12). A
9 * `session.update` containing unknown fields is rejected with
10 * `unknown_parameter` and none of the config is applied, so the exact field
11 * names here are load-bearing.
12 *
13 * `temperature` was removed from the GA session config and is intentionally
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']
74 : ['text']
75 }
76​
77 if (config.maxOutputTokens !== undefined) {
78 sessionUpdate.max_output_tokens = config.maxOutputTokens
79 }
80​
81 return sessionUpdate