|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package realtime_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/openai/openai-go/v3" |
| 12 | + "github.com/openai/openai-go/v3/internal/testutil" |
| 13 | + "github.com/openai/openai-go/v3/option" |
| 14 | + "github.com/openai/openai-go/v3/realtime" |
| 15 | + "github.com/openai/openai-go/v3/responses" |
| 16 | + "github.com/openai/openai-go/v3/shared/constant" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCallAcceptWithOptionalParams(t *testing.T) { |
| 20 | + baseURL := "http://localhost:4010" |
| 21 | + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { |
| 22 | + baseURL = envURL |
| 23 | + } |
| 24 | + if !testutil.CheckTestServer(t, baseURL) { |
| 25 | + return |
| 26 | + } |
| 27 | + client := openai.NewClient( |
| 28 | + option.WithBaseURL(baseURL), |
| 29 | + option.WithAPIKey("My API Key"), |
| 30 | + ) |
| 31 | + err := client.Realtime.Calls.Accept( |
| 32 | + context.TODO(), |
| 33 | + "call_id", |
| 34 | + realtime.CallAcceptParams{ |
| 35 | + RealtimeSessionCreateRequest: realtime.RealtimeSessionCreateRequestParam{ |
| 36 | + Audio: realtime.RealtimeAudioConfigParam{ |
| 37 | + Input: realtime.RealtimeAudioConfigInputParam{ |
| 38 | + Format: realtime.RealtimeAudioFormatsUnionParam{ |
| 39 | + OfAudioPCM: &realtime.RealtimeAudioFormatsAudioPCMParam{ |
| 40 | + Rate: 24000, |
| 41 | + Type: "audio/pcm", |
| 42 | + }, |
| 43 | + }, |
| 44 | + NoiseReduction: realtime.RealtimeAudioConfigInputNoiseReductionParam{ |
| 45 | + Type: realtime.NoiseReductionTypeNearField, |
| 46 | + }, |
| 47 | + Transcription: realtime.AudioTranscriptionParam{ |
| 48 | + Language: openai.String("language"), |
| 49 | + Model: realtime.AudioTranscriptionModelWhisper1, |
| 50 | + Prompt: openai.String("prompt"), |
| 51 | + }, |
| 52 | + TurnDetection: realtime.RealtimeAudioInputTurnDetectionUnionParam{ |
| 53 | + OfServerVad: &realtime.RealtimeAudioInputTurnDetectionServerVadParam{ |
| 54 | + CreateResponse: openai.Bool(true), |
| 55 | + IdleTimeoutMs: openai.Int(5000), |
| 56 | + InterruptResponse: openai.Bool(true), |
| 57 | + PrefixPaddingMs: openai.Int(0), |
| 58 | + SilenceDurationMs: openai.Int(0), |
| 59 | + Threshold: openai.Float(0), |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + Output: realtime.RealtimeAudioConfigOutputParam{ |
| 64 | + Format: realtime.RealtimeAudioFormatsUnionParam{ |
| 65 | + OfAudioPCM: &realtime.RealtimeAudioFormatsAudioPCMParam{ |
| 66 | + Rate: 24000, |
| 67 | + Type: "audio/pcm", |
| 68 | + }, |
| 69 | + }, |
| 70 | + Speed: openai.Float(0.25), |
| 71 | + Voice: realtime.RealtimeAudioConfigOutputVoiceAlloy, |
| 72 | + }, |
| 73 | + }, |
| 74 | + Include: []string{"item.input_audio_transcription.logprobs"}, |
| 75 | + Instructions: openai.String("instructions"), |
| 76 | + MaxOutputTokens: realtime.RealtimeSessionCreateRequestMaxOutputTokensUnionParam{ |
| 77 | + OfInt: openai.Int(0), |
| 78 | + }, |
| 79 | + Model: realtime.RealtimeSessionCreateRequestModelGPTRealtime, |
| 80 | + OutputModalities: []string{"text"}, |
| 81 | + Prompt: responses.ResponsePromptParam{ |
| 82 | + ID: "id", |
| 83 | + Variables: map[string]responses.ResponsePromptVariableUnionParam{ |
| 84 | + "foo": { |
| 85 | + OfString: openai.String("string"), |
| 86 | + }, |
| 87 | + }, |
| 88 | + Version: openai.String("version"), |
| 89 | + }, |
| 90 | + ToolChoice: realtime.RealtimeToolChoiceConfigUnionParam{ |
| 91 | + OfToolChoiceMode: openai.Opt(responses.ToolChoiceOptionsNone), |
| 92 | + }, |
| 93 | + Tools: realtime.RealtimeToolsConfigParam{realtime.RealtimeToolsConfigUnionParam{ |
| 94 | + OfFunction: &realtime.RealtimeFunctionToolParam{ |
| 95 | + Description: openai.String("description"), |
| 96 | + Name: openai.String("name"), |
| 97 | + Parameters: map[string]interface{}{}, |
| 98 | + Type: realtime.RealtimeFunctionToolTypeFunction, |
| 99 | + }, |
| 100 | + }}, |
| 101 | + Tracing: realtime.RealtimeTracingConfigUnionParam{ |
| 102 | + OfAuto: constant.ValueOf[constant.Auto](), |
| 103 | + }, |
| 104 | + Truncation: realtime.RealtimeTruncationUnionParam{ |
| 105 | + OfRealtimeTruncationStrategy: openai.String("auto"), |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ) |
| 110 | + if err != nil { |
| 111 | + var apierr *openai.Error |
| 112 | + if errors.As(err, &apierr) { |
| 113 | + t.Log(string(apierr.DumpRequest(true))) |
| 114 | + } |
| 115 | + t.Fatalf("err should be nil: %s", err.Error()) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestCallHangup(t *testing.T) { |
| 120 | + baseURL := "http://localhost:4010" |
| 121 | + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { |
| 122 | + baseURL = envURL |
| 123 | + } |
| 124 | + if !testutil.CheckTestServer(t, baseURL) { |
| 125 | + return |
| 126 | + } |
| 127 | + client := openai.NewClient( |
| 128 | + option.WithBaseURL(baseURL), |
| 129 | + option.WithAPIKey("My API Key"), |
| 130 | + ) |
| 131 | + err := client.Realtime.Calls.Hangup(context.TODO(), "call_id") |
| 132 | + if err != nil { |
| 133 | + var apierr *openai.Error |
| 134 | + if errors.As(err, &apierr) { |
| 135 | + t.Log(string(apierr.DumpRequest(true))) |
| 136 | + } |
| 137 | + t.Fatalf("err should be nil: %s", err.Error()) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestCallRefer(t *testing.T) { |
| 142 | + baseURL := "http://localhost:4010" |
| 143 | + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { |
| 144 | + baseURL = envURL |
| 145 | + } |
| 146 | + if !testutil.CheckTestServer(t, baseURL) { |
| 147 | + return |
| 148 | + } |
| 149 | + client := openai.NewClient( |
| 150 | + option.WithBaseURL(baseURL), |
| 151 | + option.WithAPIKey("My API Key"), |
| 152 | + ) |
| 153 | + err := client.Realtime.Calls.Refer( |
| 154 | + context.TODO(), |
| 155 | + "call_id", |
| 156 | + realtime.CallReferParams{ |
| 157 | + TargetUri: "tel:+14155550123", |
| 158 | + }, |
| 159 | + ) |
| 160 | + if err != nil { |
| 161 | + var apierr *openai.Error |
| 162 | + if errors.As(err, &apierr) { |
| 163 | + t.Log(string(apierr.DumpRequest(true))) |
| 164 | + } |
| 165 | + t.Fatalf("err should be nil: %s", err.Error()) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestCallRejectWithOptionalParams(t *testing.T) { |
| 170 | + baseURL := "http://localhost:4010" |
| 171 | + if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok { |
| 172 | + baseURL = envURL |
| 173 | + } |
| 174 | + if !testutil.CheckTestServer(t, baseURL) { |
| 175 | + return |
| 176 | + } |
| 177 | + client := openai.NewClient( |
| 178 | + option.WithBaseURL(baseURL), |
| 179 | + option.WithAPIKey("My API Key"), |
| 180 | + ) |
| 181 | + err := client.Realtime.Calls.Reject( |
| 182 | + context.TODO(), |
| 183 | + "call_id", |
| 184 | + realtime.CallRejectParams{ |
| 185 | + StatusCode: openai.Int(486), |
| 186 | + }, |
| 187 | + ) |
| 188 | + if err != nil { |
| 189 | + var apierr *openai.Error |
| 190 | + if errors.As(err, &apierr) { |
| 191 | + t.Log(string(apierr.DumpRequest(true))) |
| 192 | + } |
| 193 | + t.Fatalf("err should be nil: %s", err.Error()) |
| 194 | + } |
| 195 | +} |
0 commit comments