RealtimeEvent.fromJson constructor

RealtimeEvent.fromJson(
  1. Map<String, dynamic> json
)

Creates a RealtimeEvent from JSON.

Unknown type values yield an UnknownRealtimeEvent preserving the raw payload — never throws on unrecognised discriminators.

Implementation

factory RealtimeEvent.fromJson(Map<String, dynamic> json) {
  final type = json['type'] as String?;
  if (type == null) {
    return UnknownRealtimeEvent(Map<String, dynamic>.from(json));
  }
  return switch (type) {
    // Session events
    'session.created' => SessionCreatedEvent.fromJson(json),
    'session.updated' => SessionUpdatedEvent.fromJson(json),

    // Conversation events
    'conversation.created' => ConversationCreatedEvent.fromJson(json),
    'conversation.item.added' => ConversationItemAddedEvent.fromJson(json),
    'conversation.item.created' => ConversationItemCreatedEvent.fromJson(
      json,
    ),
    'conversation.item.deleted' => ConversationItemDeletedEvent.fromJson(
      json,
    ),
    'conversation.item.truncated' => ConversationItemTruncatedEvent.fromJson(
      json,
    ),

    // Input audio transcription events
    'conversation.item.input_audio_transcription.delta' =>
      InputAudioTranscriptionDeltaEvent.fromJson(json),
    'conversation.item.input_audio_transcription.completed' =>
      InputAudioTranscriptionCompletedEvent.fromJson(json),
    'conversation.item.input_audio_transcription.failed' =>
      InputAudioTranscriptionFailedEvent.fromJson(json),
    'conversation.item.input_audio_transcription.segment' =>
      InputAudioTranscriptionSegmentEvent.fromJson(json),

    // Input audio buffer events
    'input_audio_buffer.committed' => InputAudioBufferCommittedEvent.fromJson(
      json,
    ),
    'input_audio_buffer.cleared' => InputAudioBufferClearedEvent.fromJson(
      json,
    ),
    'input_audio_buffer.speech_started' =>
      InputAudioBufferSpeechStartedEvent.fromJson(json),
    'input_audio_buffer.speech_stopped' =>
      InputAudioBufferSpeechStoppedEvent.fromJson(json),

    // Response events
    'response.created' => ResponseCreatedEvent.fromJson(json),
    'response.done' => ResponseDoneEvent.fromJson(json),
    'response.output_item.added' => ResponseOutputItemAddedEvent.fromJson(
      json,
    ),
    'response.output_item.done' => ResponseOutputItemDoneEvent.fromJson(json),
    'response.content_part.added' => ResponseContentPartAddedEvent.fromJson(
      json,
    ),
    'response.content_part.done' => ResponseContentPartDoneEvent.fromJson(
      json,
    ),

    // Text delta/done — accept either historical wire name.
    'response.text.delta' ||
    'response.output_text.delta' => ResponseTextDeltaEvent.fromJson(json),
    'response.text.done' ||
    'response.output_text.done' => ResponseTextDoneEvent.fromJson(json),

    // Audio transcript delta/done — accept either historical wire name.
    'response.audio_transcript.delta' ||
    'response.output_audio_transcript.delta' =>
      ResponseAudioTranscriptDeltaEvent.fromJson(json),
    'response.audio_transcript.done' ||
    'response.output_audio_transcript.done' =>
      ResponseAudioTranscriptDoneEvent.fromJson(json),

    // Audio delta/done — accept either historical wire name.
    'response.audio.delta' ||
    'response.output_audio.delta' => ResponseAudioDeltaEvent.fromJson(json),
    'response.audio.done' ||
    'response.output_audio.done' => ResponseAudioDoneEvent.fromJson(json),

    'response.function_call_arguments.delta' =>
      ResponseFunctionCallArgumentsDeltaEvent.fromJson(json),
    'response.function_call_arguments.done' =>
      ResponseFunctionCallArgumentsDoneEvent.fromJson(json),

    // Rate limit + error events.
    'rate_limits.updated' => RateLimitsUpdatedEvent.fromJson(json),
    'error' => ErrorEvent.fromJson(json),

    // Forward-compat: any event we don't model individually
    // (`conversation.item.done`, `output_audio_buffer.*`,
    // `mcp_list_tools.*`, `response.mcp_call.*`,
    // `transcription_session.updated`, `input_audio_buffer.timeout_triggered`,
    // …) is surfaced as [UnknownRealtimeEvent] preserving the raw JSON.
    _ => UnknownRealtimeEvent(Map<String, dynamic>.from(json)),
  };
}