syntax = "proto3";

package just.speech.v1;

import "google/protobuf/timestamp.proto";
import "google/rpc/status.proto";

option go_package = "talk.just-ai.com/api/gen/just/speech/v1;speechv1";

service Synthesizer {
  rpc Synthesize(SynthesizeRequest) returns (stream SynthesizeResponse);
  rpc StreamSynthesize(stream StreamSynthesizeRequest) returns (stream StreamSynthesizeResponse);
}

service Recognizer {
  rpc Recognize(RecognizeRequest) returns (RecognizeResponse);
  rpc StreamRecognize(stream StreamRecognizeRequest) returns (stream StreamRecognizeResponse);
}

service Catalog {
  rpc ListVoices(ListVoicesRequest) returns (ListVoicesResponse);
  rpc ListModels(ListModelsRequest) returns (ListModelsResponse);
}

message AudioFormat {
  string codec = 1;
  int32 sample_rate = 2;
  int32 channels = 3;
}

message SynthesizeRequest {
  string text = 1;
  string model_id = 2;
  string voice_id = 3;
  AudioFormat audio = 4;
  optional uint32 seed = 5;
  string text_format = 6;
  // Synthesis language: a code the model takes (ru, kk, en and others, see native.md); empty is ru.
  string language = 7;
}

message SynthesizeResponse {
  // Populated, with audio_spec, in the first response.
  string request_id = 1;
  AudioFormat audio_spec = 2;
  bytes audio = 3;
}

message Signal {}

message StreamSynthesizeRequest {
  // Empty selects the default context. Close acts on the entire stream.
  string context_id = 1;
  oneof event {
    SynthesizeRequest config = 2;
    string text = 3;
    Signal flush = 4;
    Signal clear = 5;
    Signal close = 6;
  }
}

message VoiceRoute {
  string requested_voice_id = 1;
  string selected_voice_id = 2;
  string voice_version = 3;
  string voice_version_source = 4;
  bool voice_fallback = 5;
}

message SynthesisOutcome {
  string request_id = 1;
  string delivery_status = 2;
  int64 units = 3;
  VoiceRoute voice_route = 4;
  google.protobuf.Timestamp first_audio_at = 5;
  google.protobuf.Timestamp finished_at = 6;
}

message StreamSynthesizeResponse {
  string context_id = 1;
  uint64 generation = 2;
  uint64 sequence = 3;
  oneof event {
    AudioFormat configured = 4;
    bytes audio = 5;
    SynthesisOutcome done = 6;
    Signal cleared = 7;
    google.rpc.Status error = 8;
    Signal closed = 9;
  }
  repeated VoiceRoute voice_routes = 10;
  SynthesisOutcome failed_outcome = 11;
}

message RecognitionFormat {
  string encoding = 1;
  int32 sample_rate = 2;
  int32 channels = 3;
}

message RecognizeRequest {
  bytes audio = 1;
  // Omit format to detect the container. Set format for raw audio.
  RecognitionFormat format = 2;
}

message Word {
  string text = 1;
  int64 start_ms = 2;
  int64 end_ms = 3;
}

message Utterance {
  string text = 1;
  string raw_text = 2;
  int64 start_ms = 3;
  int64 end_ms = 4;
  repeated Word words = 5;
}

message RecognizeResponse {
  string text = 1;
  string raw_text = 2;
  repeated Utterance utterances = 3;
  int64 duration_ms = 4;
  int64 billed_seconds = 5;
  string request_id = 6;
  repeated string request_ids = 7;
}

message RecognitionConfig {
  RecognitionFormat audio = 1;
  string eou = 2;
  bool partials = 3;
}

message StreamRecognizeRequest {
  oneof event {
    RecognitionConfig config = 1;
    bytes audio = 2;
    Signal eou = 3;
    Signal close = 4;
  }
}

message RecognitionConfigured {
  string request_id = 1;
}

message RecognitionPartial {
  int32 utterance = 1;
  string text = 2;
  string raw_text = 3;
  int64 start_ms = 4;
  int64 end_ms = 5;
}

message RecognitionFinal {
  int32 utterance = 1;
  string text = 2;
  string raw_text = 3;
  int64 start_ms = 4;
  int64 end_ms = 5;
  repeated Word words = 6;
  double confidence = 7;
  int64 final_index = 8;
}

message RecognitionEOU {
  int32 utterance = 1;
  int64 time_ms = 2;
}

message RecognitionClosed {
  int64 billed_seconds = 1;
  int64 duration_ms = 2;
  repeated string request_ids = 3;
}

message StreamRecognizeResponse {
  oneof event {
    RecognitionConfigured configured = 1;
    RecognitionPartial partial = 2;
    RecognitionFinal final = 3;
    RecognitionEOU eou = 4;
    google.rpc.Status error = 5;
    RecognitionClosed closed = 6;
  }
}

message ListVoicesRequest {
  string model_id = 1;
}

message Voice {
  string voice_id = 1;
  string name = 2;
  string state = 3;
  string version_id = 4;
  string version_source = 5;
  repeated string model_ids = 6;
  string gender = 7;
  string generation_version = 8;
  string family_id = 9;
  double duration_s = 10;
  string error_code = 11;
  string created_at = 12;
  double reference_max_duration_s = 13;
  oneof version_value {
    string version = 14;
    int64 private_version = 15;
  }
}

message ListVoicesResponse {
  repeated Voice voices = 1;
  bool cloning_allowed = 2;
  double reference_max_duration_s = 3;
}

message ListModelsRequest {}

message Model {
  string model_id = 1;
  bool default = 2;
  bool ready = 3;
  bool available = 4;
}

message ListModelsResponse {
  repeated Model models = 1;
}
