All files / web/src/lib/voice createRealtimeSession.ts

0% Statements 0/110
0% Branches 0/1
0% Functions 0/1
0% Lines 0/110

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                                                                                                                                                                                             
/**
 * Helper for the OpenAI Realtime Sessions API.
 *
 * Encapsulates the session creation call so both number-line and Euclid
 * API routes can share the same error classification and response parsing.
 */

import type { RealtimeTool } from './types'

export interface CreateRealtimeSessionOptions {
  apiKey: string
  /** OpenAI model ID (default: 'gpt-realtime-1.5') */
  model?: string
  voice: string
  instructions: string
  tools: RealtimeTool[]
  /** Turn detection config (sensible defaults applied) */
  turnDetection?: {
    type: 'server_vad'
    threshold?: number
    prefix_padding_ms?: number
    silence_duration_ms?: number
  }
}

export interface RealtimeSessionResult {
  clientSecret: string
  expiresAt: number
}

export interface RealtimeSessionError {
  message: string
  code: 'quota_exceeded' | 'rate_limited' | 'unavailable'
  status: number
}

/**
 * Create an ephemeral session token for the OpenAI Realtime API.
 * Returns the client secret + expiration, or throws a classified error.
 */
export async function createRealtimeSession(
  options: CreateRealtimeSessionOptions
): Promise<RealtimeSessionResult> {
  const {
    apiKey,
    model = 'gpt-realtime-1.5',
    voice,
    instructions,
    tools,
    turnDetection = {
      type: 'server_vad' as const,
      threshold: 0.8,
      prefix_padding_ms: 300,
      silence_duration_ms: 700,
    },
  } = options

  const response = await fetch('https://api.openai.com/v1/realtime/sessions', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${apiKey}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model,
      voice,
      instructions,
      input_audio_transcription: { model: 'whisper-1' },
      turn_detection: turnDetection,
      tools,
    }),
  })

  if (!response.ok) {
    const errText = await response.text()
    console.error('[realtime/session] OpenAI error:', response.status, errText)

    let code: RealtimeSessionError['code'] = 'unavailable'
    let message = 'Phone calls are unavailable right now. Try again later!'

    try {
      const errBody = JSON.parse(errText)
      const errType = errBody?.error?.type || errBody?.error?.code || ''
      if (/insufficient_quota|quota_exceeded|billing/i.test(errType) || response.status === 429) {
        code = 'quota_exceeded'
        message = 'Phone calls are taking a break right now. Try again later!'
      } else if (/rate_limit/i.test(errType)) {
        code = 'rate_limited'
        message = 'Too many calls right now. Try again in a moment!'
      }
    } catch {
      if (response.status === 429) {
        code = 'quota_exceeded'
        message = 'Phone calls are taking a break right now. Try again later!'
      }
    }

    const err = new Error(message) as Error & { code: string; status: number }
    err.code = code
    err.status = response.status
    throw err
  }

  const data = await response.json()

  return {
    clientSecret: data.client_secret?.value ?? data.client_secret,
    expiresAt: data.client_secret?.expires_at ?? Date.now() / 1000 + 60,
  }
}