web-runtime.mjs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { MultipartBody } from "./MultipartBody.mjs";
  2. export function getRuntime({ manuallyImported } = {}) {
  3. const recommendation = manuallyImported ?
  4. `You may need to use polyfills`
  5. : `Add one of these imports before your first \`import … from 'openai'\`:
  6. - \`import 'openai/shims/node'\` (if you're running on Node)
  7. - \`import 'openai/shims/web'\` (otherwise)
  8. `;
  9. let _fetch, _Request, _Response, _Headers;
  10. try {
  11. // @ts-ignore
  12. _fetch = fetch;
  13. // @ts-ignore
  14. _Request = Request;
  15. // @ts-ignore
  16. _Response = Response;
  17. // @ts-ignore
  18. _Headers = Headers;
  19. }
  20. catch (error) {
  21. throw new Error(`this environment is missing the following Web Fetch API type: ${error.message}. ${recommendation}`);
  22. }
  23. return {
  24. kind: 'web',
  25. fetch: _fetch,
  26. Request: _Request,
  27. Response: _Response,
  28. Headers: _Headers,
  29. FormData:
  30. // @ts-ignore
  31. typeof FormData !== 'undefined' ? FormData : (class FormData {
  32. // @ts-ignore
  33. constructor() {
  34. throw new Error(`file uploads aren't supported in this environment yet as 'FormData' is undefined. ${recommendation}`);
  35. }
  36. }),
  37. Blob: typeof Blob !== 'undefined' ? Blob : (class Blob {
  38. constructor() {
  39. throw new Error(`file uploads aren't supported in this environment yet as 'Blob' is undefined. ${recommendation}`);
  40. }
  41. }),
  42. File:
  43. // @ts-ignore
  44. typeof File !== 'undefined' ? File : (class File {
  45. // @ts-ignore
  46. constructor() {
  47. throw new Error(`file uploads aren't supported in this environment yet as 'File' is undefined. ${recommendation}`);
  48. }
  49. }),
  50. ReadableStream:
  51. // @ts-ignore
  52. typeof ReadableStream !== 'undefined' ? ReadableStream : (class ReadableStream {
  53. // @ts-ignore
  54. constructor() {
  55. throw new Error(`streaming isn't supported in this environment yet as 'ReadableStream' is undefined. ${recommendation}`);
  56. }
  57. }),
  58. getMultipartRequestOptions: async (
  59. // @ts-ignore
  60. form, opts) => ({
  61. ...opts,
  62. body: new MultipartBody(form),
  63. }),
  64. getDefaultAgent: (url) => undefined,
  65. fileFromPath: () => {
  66. throw new Error('The `fileFromPath` function is only supported in Node. See the README for more details: https://www.github.com/openai/openai-node#file-uploads');
  67. },
  68. isFsReadStream: (value) => false,
  69. };
  70. }
  71. //# sourceMappingURL=web-runtime.mjs.map