calls.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. from __future__ import annotations
  3. from typing import List, Union, Optional
  4. from typing_extensions import Literal
  5. import httpx
  6. from ... import _legacy_response
  7. from ..._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given
  8. from ..._utils import maybe_transform, async_maybe_transform
  9. from ..._compat import cached_property
  10. from ..._resource import SyncAPIResource, AsyncAPIResource
  11. from ..._response import (
  12. StreamedBinaryAPIResponse,
  13. AsyncStreamedBinaryAPIResponse,
  14. to_streamed_response_wrapper,
  15. async_to_streamed_response_wrapper,
  16. to_custom_streamed_response_wrapper,
  17. async_to_custom_streamed_response_wrapper,
  18. )
  19. from ..._base_client import make_request_options
  20. from ...types.realtime import (
  21. call_refer_params,
  22. call_accept_params,
  23. call_create_params,
  24. call_reject_params,
  25. )
  26. from ...types.responses.response_prompt_param import ResponsePromptParam
  27. from ...types.realtime.realtime_truncation_param import RealtimeTruncationParam
  28. from ...types.realtime.realtime_audio_config_param import RealtimeAudioConfigParam
  29. from ...types.realtime.realtime_tools_config_param import RealtimeToolsConfigParam
  30. from ...types.realtime.realtime_tracing_config_param import RealtimeTracingConfigParam
  31. from ...types.realtime.realtime_tool_choice_config_param import RealtimeToolChoiceConfigParam
  32. from ...types.realtime.realtime_session_create_request_param import RealtimeSessionCreateRequestParam
  33. __all__ = ["Calls", "AsyncCalls"]
  34. class Calls(SyncAPIResource):
  35. @cached_property
  36. def with_raw_response(self) -> CallsWithRawResponse:
  37. """
  38. This property can be used as a prefix for any HTTP method call to return
  39. the raw response object instead of the parsed content.
  40. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  41. """
  42. return CallsWithRawResponse(self)
  43. @cached_property
  44. def with_streaming_response(self) -> CallsWithStreamingResponse:
  45. """
  46. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  47. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  48. """
  49. return CallsWithStreamingResponse(self)
  50. def create(
  51. self,
  52. *,
  53. sdp: str,
  54. session: RealtimeSessionCreateRequestParam | Omit = omit,
  55. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  56. # The extra values given here take precedence over values defined on the client or passed to this method.
  57. extra_headers: Headers | None = None,
  58. extra_query: Query | None = None,
  59. extra_body: Body | None = None,
  60. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  61. ) -> _legacy_response.HttpxBinaryResponseContent:
  62. """
  63. Create a new Realtime API call over WebRTC and receive the SDP answer needed to
  64. complete the peer connection.
  65. Args:
  66. sdp: WebRTC Session Description Protocol (SDP) offer generated by the caller.
  67. session: Realtime session object configuration.
  68. extra_headers: Send extra headers
  69. extra_query: Add additional query parameters to the request
  70. extra_body: Add additional JSON properties to the request
  71. timeout: Override the client-level default timeout for this request, in seconds
  72. """
  73. extra_headers = {"Accept": "application/sdp", **(extra_headers or {})}
  74. return self._post(
  75. "/realtime/calls",
  76. body=maybe_transform(
  77. {
  78. "sdp": sdp,
  79. "session": session,
  80. },
  81. call_create_params.CallCreateParams,
  82. ),
  83. options=make_request_options(
  84. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  85. ),
  86. cast_to=_legacy_response.HttpxBinaryResponseContent,
  87. )
  88. def accept(
  89. self,
  90. call_id: str,
  91. *,
  92. type: Literal["realtime"],
  93. audio: RealtimeAudioConfigParam | Omit = omit,
  94. include: List[Literal["item.input_audio_transcription.logprobs"]] | Omit = omit,
  95. instructions: str | Omit = omit,
  96. max_output_tokens: Union[int, Literal["inf"]] | Omit = omit,
  97. model: Union[
  98. str,
  99. Literal[
  100. "gpt-realtime",
  101. "gpt-realtime-2025-08-28",
  102. "gpt-4o-realtime-preview",
  103. "gpt-4o-realtime-preview-2024-10-01",
  104. "gpt-4o-realtime-preview-2024-12-17",
  105. "gpt-4o-realtime-preview-2025-06-03",
  106. "gpt-4o-mini-realtime-preview",
  107. "gpt-4o-mini-realtime-preview-2024-12-17",
  108. "gpt-realtime-mini",
  109. "gpt-realtime-mini-2025-10-06",
  110. "gpt-audio-mini",
  111. "gpt-audio-mini-2025-10-06",
  112. ],
  113. ]
  114. | Omit = omit,
  115. output_modalities: List[Literal["text", "audio"]] | Omit = omit,
  116. prompt: Optional[ResponsePromptParam] | Omit = omit,
  117. tool_choice: RealtimeToolChoiceConfigParam | Omit = omit,
  118. tools: RealtimeToolsConfigParam | Omit = omit,
  119. tracing: Optional[RealtimeTracingConfigParam] | Omit = omit,
  120. truncation: RealtimeTruncationParam | Omit = omit,
  121. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  122. # The extra values given here take precedence over values defined on the client or passed to this method.
  123. extra_headers: Headers | None = None,
  124. extra_query: Query | None = None,
  125. extra_body: Body | None = None,
  126. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  127. ) -> None:
  128. """
  129. Accept an incoming SIP call and configure the realtime session that will handle
  130. it.
  131. Args:
  132. type: The type of session to create. Always `realtime` for the Realtime API.
  133. audio: Configuration for input and output audio.
  134. include: Additional fields to include in server outputs.
  135. `item.input_audio_transcription.logprobs`: Include logprobs for input audio
  136. transcription.
  137. instructions: The default system instructions (i.e. system message) prepended to model calls.
  138. This field allows the client to guide the model on desired responses. The model
  139. can be instructed on response content and format, (e.g. "be extremely succinct",
  140. "act friendly", "here are examples of good responses") and on audio behavior
  141. (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently"). The
  142. instructions are not guaranteed to be followed by the model, but they provide
  143. guidance to the model on the desired behavior.
  144. Note that the server sets default instructions which will be used if this field
  145. is not set and are visible in the `session.created` event at the start of the
  146. session.
  147. max_output_tokens: Maximum number of output tokens for a single assistant response, inclusive of
  148. tool calls. Provide an integer between 1 and 4096 to limit output tokens, or
  149. `inf` for the maximum available tokens for a given model. Defaults to `inf`.
  150. model: The Realtime model used for this session.
  151. output_modalities: The set of modalities the model can respond with. It defaults to `["audio"]`,
  152. indicating that the model will respond with audio plus a transcript. `["text"]`
  153. can be used to make the model respond with text only. It is not possible to
  154. request both `text` and `audio` at the same time.
  155. prompt: Reference to a prompt template and its variables.
  156. [Learn more](https://platform.openai.com/docs/guides/text?api-mode=responses#reusable-prompts).
  157. tool_choice: How the model chooses tools. Provide one of the string modes or force a specific
  158. function/MCP tool.
  159. tools: Tools available to the model.
  160. tracing: Realtime API can write session traces to the
  161. [Traces Dashboard](/logs?api=traces). Set to null to disable tracing. Once
  162. tracing is enabled for a session, the configuration cannot be modified.
  163. `auto` will create a trace for the session with default values for the workflow
  164. name, group id, and metadata.
  165. truncation: When the number of tokens in a conversation exceeds the model's input token
  166. limit, the conversation be truncated, meaning messages (starting from the
  167. oldest) will not be included in the model's context. A 32k context model with
  168. 4,096 max output tokens can only include 28,224 tokens in the context before
  169. truncation occurs.
  170. Clients can configure truncation behavior to truncate with a lower max token
  171. limit, which is an effective way to control token usage and cost.
  172. Truncation will reduce the number of cached tokens on the next turn (busting the
  173. cache), since messages are dropped from the beginning of the context. However,
  174. clients can also configure truncation to retain messages up to a fraction of the
  175. maximum context size, which will reduce the need for future truncations and thus
  176. improve the cache rate.
  177. Truncation can be disabled entirely, which means the server will never truncate
  178. but would instead return an error if the conversation exceeds the model's input
  179. token limit.
  180. extra_headers: Send extra headers
  181. extra_query: Add additional query parameters to the request
  182. extra_body: Add additional JSON properties to the request
  183. timeout: Override the client-level default timeout for this request, in seconds
  184. """
  185. if not call_id:
  186. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  187. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  188. return self._post(
  189. f"/realtime/calls/{call_id}/accept",
  190. body=maybe_transform(
  191. {
  192. "type": type,
  193. "audio": audio,
  194. "include": include,
  195. "instructions": instructions,
  196. "max_output_tokens": max_output_tokens,
  197. "model": model,
  198. "output_modalities": output_modalities,
  199. "prompt": prompt,
  200. "tool_choice": tool_choice,
  201. "tools": tools,
  202. "tracing": tracing,
  203. "truncation": truncation,
  204. },
  205. call_accept_params.CallAcceptParams,
  206. ),
  207. options=make_request_options(
  208. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  209. ),
  210. cast_to=NoneType,
  211. )
  212. def hangup(
  213. self,
  214. call_id: str,
  215. *,
  216. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  217. # The extra values given here take precedence over values defined on the client or passed to this method.
  218. extra_headers: Headers | None = None,
  219. extra_query: Query | None = None,
  220. extra_body: Body | None = None,
  221. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  222. ) -> None:
  223. """
  224. End an active Realtime API call, whether it was initiated over SIP or WebRTC.
  225. Args:
  226. extra_headers: Send extra headers
  227. extra_query: Add additional query parameters to the request
  228. extra_body: Add additional JSON properties to the request
  229. timeout: Override the client-level default timeout for this request, in seconds
  230. """
  231. if not call_id:
  232. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  233. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  234. return self._post(
  235. f"/realtime/calls/{call_id}/hangup",
  236. options=make_request_options(
  237. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  238. ),
  239. cast_to=NoneType,
  240. )
  241. def refer(
  242. self,
  243. call_id: str,
  244. *,
  245. target_uri: str,
  246. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  247. # The extra values given here take precedence over values defined on the client or passed to this method.
  248. extra_headers: Headers | None = None,
  249. extra_query: Query | None = None,
  250. extra_body: Body | None = None,
  251. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  252. ) -> None:
  253. """
  254. Transfer an active SIP call to a new destination using the SIP REFER verb.
  255. Args:
  256. target_uri: URI that should appear in the SIP Refer-To header. Supports values like
  257. `tel:+14155550123` or `sip:agent@example.com`.
  258. extra_headers: Send extra headers
  259. extra_query: Add additional query parameters to the request
  260. extra_body: Add additional JSON properties to the request
  261. timeout: Override the client-level default timeout for this request, in seconds
  262. """
  263. if not call_id:
  264. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  265. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  266. return self._post(
  267. f"/realtime/calls/{call_id}/refer",
  268. body=maybe_transform({"target_uri": target_uri}, call_refer_params.CallReferParams),
  269. options=make_request_options(
  270. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  271. ),
  272. cast_to=NoneType,
  273. )
  274. def reject(
  275. self,
  276. call_id: str,
  277. *,
  278. status_code: int | Omit = omit,
  279. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  280. # The extra values given here take precedence over values defined on the client or passed to this method.
  281. extra_headers: Headers | None = None,
  282. extra_query: Query | None = None,
  283. extra_body: Body | None = None,
  284. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  285. ) -> None:
  286. """
  287. Decline an incoming SIP call by returning a SIP status code to the caller.
  288. Args:
  289. status_code: SIP response code to send back to the caller. Defaults to `603` (Decline) when
  290. omitted.
  291. extra_headers: Send extra headers
  292. extra_query: Add additional query parameters to the request
  293. extra_body: Add additional JSON properties to the request
  294. timeout: Override the client-level default timeout for this request, in seconds
  295. """
  296. if not call_id:
  297. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  298. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  299. return self._post(
  300. f"/realtime/calls/{call_id}/reject",
  301. body=maybe_transform({"status_code": status_code}, call_reject_params.CallRejectParams),
  302. options=make_request_options(
  303. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  304. ),
  305. cast_to=NoneType,
  306. )
  307. class AsyncCalls(AsyncAPIResource):
  308. @cached_property
  309. def with_raw_response(self) -> AsyncCallsWithRawResponse:
  310. """
  311. This property can be used as a prefix for any HTTP method call to return
  312. the raw response object instead of the parsed content.
  313. For more information, see https://www.github.com/openai/openai-python#accessing-raw-response-data-eg-headers
  314. """
  315. return AsyncCallsWithRawResponse(self)
  316. @cached_property
  317. def with_streaming_response(self) -> AsyncCallsWithStreamingResponse:
  318. """
  319. An alternative to `.with_raw_response` that doesn't eagerly read the response body.
  320. For more information, see https://www.github.com/openai/openai-python#with_streaming_response
  321. """
  322. return AsyncCallsWithStreamingResponse(self)
  323. async def create(
  324. self,
  325. *,
  326. sdp: str,
  327. session: RealtimeSessionCreateRequestParam | Omit = omit,
  328. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  329. # The extra values given here take precedence over values defined on the client or passed to this method.
  330. extra_headers: Headers | None = None,
  331. extra_query: Query | None = None,
  332. extra_body: Body | None = None,
  333. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  334. ) -> _legacy_response.HttpxBinaryResponseContent:
  335. """
  336. Create a new Realtime API call over WebRTC and receive the SDP answer needed to
  337. complete the peer connection.
  338. Args:
  339. sdp: WebRTC Session Description Protocol (SDP) offer generated by the caller.
  340. session: Realtime session object configuration.
  341. extra_headers: Send extra headers
  342. extra_query: Add additional query parameters to the request
  343. extra_body: Add additional JSON properties to the request
  344. timeout: Override the client-level default timeout for this request, in seconds
  345. """
  346. extra_headers = {"Accept": "application/sdp", **(extra_headers or {})}
  347. return await self._post(
  348. "/realtime/calls",
  349. body=await async_maybe_transform(
  350. {
  351. "sdp": sdp,
  352. "session": session,
  353. },
  354. call_create_params.CallCreateParams,
  355. ),
  356. options=make_request_options(
  357. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  358. ),
  359. cast_to=_legacy_response.HttpxBinaryResponseContent,
  360. )
  361. async def accept(
  362. self,
  363. call_id: str,
  364. *,
  365. type: Literal["realtime"],
  366. audio: RealtimeAudioConfigParam | Omit = omit,
  367. include: List[Literal["item.input_audio_transcription.logprobs"]] | Omit = omit,
  368. instructions: str | Omit = omit,
  369. max_output_tokens: Union[int, Literal["inf"]] | Omit = omit,
  370. model: Union[
  371. str,
  372. Literal[
  373. "gpt-realtime",
  374. "gpt-realtime-2025-08-28",
  375. "gpt-4o-realtime-preview",
  376. "gpt-4o-realtime-preview-2024-10-01",
  377. "gpt-4o-realtime-preview-2024-12-17",
  378. "gpt-4o-realtime-preview-2025-06-03",
  379. "gpt-4o-mini-realtime-preview",
  380. "gpt-4o-mini-realtime-preview-2024-12-17",
  381. "gpt-realtime-mini",
  382. "gpt-realtime-mini-2025-10-06",
  383. "gpt-audio-mini",
  384. "gpt-audio-mini-2025-10-06",
  385. ],
  386. ]
  387. | Omit = omit,
  388. output_modalities: List[Literal["text", "audio"]] | Omit = omit,
  389. prompt: Optional[ResponsePromptParam] | Omit = omit,
  390. tool_choice: RealtimeToolChoiceConfigParam | Omit = omit,
  391. tools: RealtimeToolsConfigParam | Omit = omit,
  392. tracing: Optional[RealtimeTracingConfigParam] | Omit = omit,
  393. truncation: RealtimeTruncationParam | Omit = omit,
  394. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  395. # The extra values given here take precedence over values defined on the client or passed to this method.
  396. extra_headers: Headers | None = None,
  397. extra_query: Query | None = None,
  398. extra_body: Body | None = None,
  399. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  400. ) -> None:
  401. """
  402. Accept an incoming SIP call and configure the realtime session that will handle
  403. it.
  404. Args:
  405. type: The type of session to create. Always `realtime` for the Realtime API.
  406. audio: Configuration for input and output audio.
  407. include: Additional fields to include in server outputs.
  408. `item.input_audio_transcription.logprobs`: Include logprobs for input audio
  409. transcription.
  410. instructions: The default system instructions (i.e. system message) prepended to model calls.
  411. This field allows the client to guide the model on desired responses. The model
  412. can be instructed on response content and format, (e.g. "be extremely succinct",
  413. "act friendly", "here are examples of good responses") and on audio behavior
  414. (e.g. "talk quickly", "inject emotion into your voice", "laugh frequently"). The
  415. instructions are not guaranteed to be followed by the model, but they provide
  416. guidance to the model on the desired behavior.
  417. Note that the server sets default instructions which will be used if this field
  418. is not set and are visible in the `session.created` event at the start of the
  419. session.
  420. max_output_tokens: Maximum number of output tokens for a single assistant response, inclusive of
  421. tool calls. Provide an integer between 1 and 4096 to limit output tokens, or
  422. `inf` for the maximum available tokens for a given model. Defaults to `inf`.
  423. model: The Realtime model used for this session.
  424. output_modalities: The set of modalities the model can respond with. It defaults to `["audio"]`,
  425. indicating that the model will respond with audio plus a transcript. `["text"]`
  426. can be used to make the model respond with text only. It is not possible to
  427. request both `text` and `audio` at the same time.
  428. prompt: Reference to a prompt template and its variables.
  429. [Learn more](https://platform.openai.com/docs/guides/text?api-mode=responses#reusable-prompts).
  430. tool_choice: How the model chooses tools. Provide one of the string modes or force a specific
  431. function/MCP tool.
  432. tools: Tools available to the model.
  433. tracing: Realtime API can write session traces to the
  434. [Traces Dashboard](/logs?api=traces). Set to null to disable tracing. Once
  435. tracing is enabled for a session, the configuration cannot be modified.
  436. `auto` will create a trace for the session with default values for the workflow
  437. name, group id, and metadata.
  438. truncation: When the number of tokens in a conversation exceeds the model's input token
  439. limit, the conversation be truncated, meaning messages (starting from the
  440. oldest) will not be included in the model's context. A 32k context model with
  441. 4,096 max output tokens can only include 28,224 tokens in the context before
  442. truncation occurs.
  443. Clients can configure truncation behavior to truncate with a lower max token
  444. limit, which is an effective way to control token usage and cost.
  445. Truncation will reduce the number of cached tokens on the next turn (busting the
  446. cache), since messages are dropped from the beginning of the context. However,
  447. clients can also configure truncation to retain messages up to a fraction of the
  448. maximum context size, which will reduce the need for future truncations and thus
  449. improve the cache rate.
  450. Truncation can be disabled entirely, which means the server will never truncate
  451. but would instead return an error if the conversation exceeds the model's input
  452. token limit.
  453. extra_headers: Send extra headers
  454. extra_query: Add additional query parameters to the request
  455. extra_body: Add additional JSON properties to the request
  456. timeout: Override the client-level default timeout for this request, in seconds
  457. """
  458. if not call_id:
  459. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  460. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  461. return await self._post(
  462. f"/realtime/calls/{call_id}/accept",
  463. body=await async_maybe_transform(
  464. {
  465. "type": type,
  466. "audio": audio,
  467. "include": include,
  468. "instructions": instructions,
  469. "max_output_tokens": max_output_tokens,
  470. "model": model,
  471. "output_modalities": output_modalities,
  472. "prompt": prompt,
  473. "tool_choice": tool_choice,
  474. "tools": tools,
  475. "tracing": tracing,
  476. "truncation": truncation,
  477. },
  478. call_accept_params.CallAcceptParams,
  479. ),
  480. options=make_request_options(
  481. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  482. ),
  483. cast_to=NoneType,
  484. )
  485. async def hangup(
  486. self,
  487. call_id: str,
  488. *,
  489. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  490. # The extra values given here take precedence over values defined on the client or passed to this method.
  491. extra_headers: Headers | None = None,
  492. extra_query: Query | None = None,
  493. extra_body: Body | None = None,
  494. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  495. ) -> None:
  496. """
  497. End an active Realtime API call, whether it was initiated over SIP or WebRTC.
  498. Args:
  499. extra_headers: Send extra headers
  500. extra_query: Add additional query parameters to the request
  501. extra_body: Add additional JSON properties to the request
  502. timeout: Override the client-level default timeout for this request, in seconds
  503. """
  504. if not call_id:
  505. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  506. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  507. return await self._post(
  508. f"/realtime/calls/{call_id}/hangup",
  509. options=make_request_options(
  510. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  511. ),
  512. cast_to=NoneType,
  513. )
  514. async def refer(
  515. self,
  516. call_id: str,
  517. *,
  518. target_uri: str,
  519. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  520. # The extra values given here take precedence over values defined on the client or passed to this method.
  521. extra_headers: Headers | None = None,
  522. extra_query: Query | None = None,
  523. extra_body: Body | None = None,
  524. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  525. ) -> None:
  526. """
  527. Transfer an active SIP call to a new destination using the SIP REFER verb.
  528. Args:
  529. target_uri: URI that should appear in the SIP Refer-To header. Supports values like
  530. `tel:+14155550123` or `sip:agent@example.com`.
  531. extra_headers: Send extra headers
  532. extra_query: Add additional query parameters to the request
  533. extra_body: Add additional JSON properties to the request
  534. timeout: Override the client-level default timeout for this request, in seconds
  535. """
  536. if not call_id:
  537. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  538. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  539. return await self._post(
  540. f"/realtime/calls/{call_id}/refer",
  541. body=await async_maybe_transform({"target_uri": target_uri}, call_refer_params.CallReferParams),
  542. options=make_request_options(
  543. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  544. ),
  545. cast_to=NoneType,
  546. )
  547. async def reject(
  548. self,
  549. call_id: str,
  550. *,
  551. status_code: int | Omit = omit,
  552. # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
  553. # The extra values given here take precedence over values defined on the client or passed to this method.
  554. extra_headers: Headers | None = None,
  555. extra_query: Query | None = None,
  556. extra_body: Body | None = None,
  557. timeout: float | httpx.Timeout | None | NotGiven = not_given,
  558. ) -> None:
  559. """
  560. Decline an incoming SIP call by returning a SIP status code to the caller.
  561. Args:
  562. status_code: SIP response code to send back to the caller. Defaults to `603` (Decline) when
  563. omitted.
  564. extra_headers: Send extra headers
  565. extra_query: Add additional query parameters to the request
  566. extra_body: Add additional JSON properties to the request
  567. timeout: Override the client-level default timeout for this request, in seconds
  568. """
  569. if not call_id:
  570. raise ValueError(f"Expected a non-empty value for `call_id` but received {call_id!r}")
  571. extra_headers = {"Accept": "*/*", **(extra_headers or {})}
  572. return await self._post(
  573. f"/realtime/calls/{call_id}/reject",
  574. body=await async_maybe_transform({"status_code": status_code}, call_reject_params.CallRejectParams),
  575. options=make_request_options(
  576. extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
  577. ),
  578. cast_to=NoneType,
  579. )
  580. class CallsWithRawResponse:
  581. def __init__(self, calls: Calls) -> None:
  582. self._calls = calls
  583. self.create = _legacy_response.to_raw_response_wrapper(
  584. calls.create,
  585. )
  586. self.accept = _legacy_response.to_raw_response_wrapper(
  587. calls.accept,
  588. )
  589. self.hangup = _legacy_response.to_raw_response_wrapper(
  590. calls.hangup,
  591. )
  592. self.refer = _legacy_response.to_raw_response_wrapper(
  593. calls.refer,
  594. )
  595. self.reject = _legacy_response.to_raw_response_wrapper(
  596. calls.reject,
  597. )
  598. class AsyncCallsWithRawResponse:
  599. def __init__(self, calls: AsyncCalls) -> None:
  600. self._calls = calls
  601. self.create = _legacy_response.async_to_raw_response_wrapper(
  602. calls.create,
  603. )
  604. self.accept = _legacy_response.async_to_raw_response_wrapper(
  605. calls.accept,
  606. )
  607. self.hangup = _legacy_response.async_to_raw_response_wrapper(
  608. calls.hangup,
  609. )
  610. self.refer = _legacy_response.async_to_raw_response_wrapper(
  611. calls.refer,
  612. )
  613. self.reject = _legacy_response.async_to_raw_response_wrapper(
  614. calls.reject,
  615. )
  616. class CallsWithStreamingResponse:
  617. def __init__(self, calls: Calls) -> None:
  618. self._calls = calls
  619. self.create = to_custom_streamed_response_wrapper(
  620. calls.create,
  621. StreamedBinaryAPIResponse,
  622. )
  623. self.accept = to_streamed_response_wrapper(
  624. calls.accept,
  625. )
  626. self.hangup = to_streamed_response_wrapper(
  627. calls.hangup,
  628. )
  629. self.refer = to_streamed_response_wrapper(
  630. calls.refer,
  631. )
  632. self.reject = to_streamed_response_wrapper(
  633. calls.reject,
  634. )
  635. class AsyncCallsWithStreamingResponse:
  636. def __init__(self, calls: AsyncCalls) -> None:
  637. self._calls = calls
  638. self.create = async_to_custom_streamed_response_wrapper(
  639. calls.create,
  640. AsyncStreamedBinaryAPIResponse,
  641. )
  642. self.accept = async_to_streamed_response_wrapper(
  643. calls.accept,
  644. )
  645. self.hangup = async_to_streamed_response_wrapper(
  646. calls.hangup,
  647. )
  648. self.refer = async_to_streamed_response_wrapper(
  649. calls.refer,
  650. )
  651. self.reject = async_to_streamed_response_wrapper(
  652. calls.reject,
  653. )