responses.mjs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2. import { parseResponse, addOutputText, } from "../../lib/ResponsesParser.mjs";
  3. import { ResponseStream } from "../../lib/responses/ResponseStream.mjs";
  4. import { APIResource } from "../../core/resource.mjs";
  5. import * as InputItemsAPI from "./input-items.mjs";
  6. import { InputItems } from "./input-items.mjs";
  7. import * as InputTokensAPI from "./input-tokens.mjs";
  8. import { InputTokens } from "./input-tokens.mjs";
  9. import { buildHeaders } from "../../internal/headers.mjs";
  10. import { path } from "../../internal/utils/path.mjs";
  11. export class Responses extends APIResource {
  12. constructor() {
  13. super(...arguments);
  14. this.inputItems = new InputItemsAPI.InputItems(this._client);
  15. this.inputTokens = new InputTokensAPI.InputTokens(this._client);
  16. }
  17. create(body, options) {
  18. return this._client.post('/responses', { body, ...options, stream: body.stream ?? false })._thenUnwrap((rsp) => {
  19. if ('object' in rsp && rsp.object === 'response') {
  20. addOutputText(rsp);
  21. }
  22. return rsp;
  23. });
  24. }
  25. retrieve(responseID, query = {}, options) {
  26. return this._client.get(path `/responses/${responseID}`, {
  27. query,
  28. ...options,
  29. stream: query?.stream ?? false,
  30. })._thenUnwrap((rsp) => {
  31. if ('object' in rsp && rsp.object === 'response') {
  32. addOutputText(rsp);
  33. }
  34. return rsp;
  35. });
  36. }
  37. /**
  38. * Deletes a model response with the given ID.
  39. *
  40. * @example
  41. * ```ts
  42. * await client.responses.delete(
  43. * 'resp_677efb5139a88190b512bc3fef8e535d',
  44. * );
  45. * ```
  46. */
  47. delete(responseID, options) {
  48. return this._client.delete(path `/responses/${responseID}`, {
  49. ...options,
  50. headers: buildHeaders([{ Accept: '*/*' }, options?.headers]),
  51. });
  52. }
  53. parse(body, options) {
  54. return this._client.responses
  55. .create(body, options)
  56. ._thenUnwrap((response) => parseResponse(response, body));
  57. }
  58. /**
  59. * Creates a model response stream
  60. */
  61. stream(body, options) {
  62. return ResponseStream.createResponse(this._client, body, options);
  63. }
  64. /**
  65. * Cancels a model response with the given ID. Only responses created with the
  66. * `background` parameter set to `true` can be cancelled.
  67. * [Learn more](https://platform.openai.com/docs/guides/background).
  68. *
  69. * @example
  70. * ```ts
  71. * const response = await client.responses.cancel(
  72. * 'resp_677efb5139a88190b512bc3fef8e535d',
  73. * );
  74. * ```
  75. */
  76. cancel(responseID, options) {
  77. return this._client.post(path `/responses/${responseID}/cancel`, options);
  78. }
  79. /**
  80. * Compact conversation
  81. *
  82. * @example
  83. * ```ts
  84. * const compactedResponse = await client.responses.compact({
  85. * model: 'gpt-5.2',
  86. * });
  87. * ```
  88. */
  89. compact(body, options) {
  90. return this._client.post('/responses/compact', { body, ...options });
  91. }
  92. }
  93. Responses.InputItems = InputItems;
  94. Responses.InputTokens = InputTokens;
  95. //# sourceMappingURL=responses.mjs.map