初始化仓库

This commit is contained in:
mmc
2025-09-15 10:04:47 +08:00
commit 87a4a76f1b
2576 changed files with 221250 additions and 0 deletions

3
node_modules/openai/src/internal/README.md generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# `internal`
The modules in this directory are not importable outside this package and will change between releases.

93
node_modules/openai/src/internal/builtin-types.ts generated vendored Normal file
View File

@@ -0,0 +1,93 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export type Fetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
/**
* An alias to the builtin `RequestInit` type so we can
* easily alias it in import statements if there are name clashes.
*
* https://developer.mozilla.org/docs/Web/API/RequestInit
*/
type _RequestInit = RequestInit;
/**
* An alias to the builtin `Response` type so we can
* easily alias it in import statements if there are name clashes.
*
* https://developer.mozilla.org/docs/Web/API/Response
*/
type _Response = Response;
/**
* The type for the first argument to `fetch`.
*
* https://developer.mozilla.org/docs/Web/API/Window/fetch#resource
*/
type _RequestInfo = Request | URL | string;
/**
* The type for constructing `RequestInit` Headers.
*
* https://developer.mozilla.org/docs/Web/API/RequestInit#setting_headers
*/
type _HeadersInit = RequestInit['headers'];
/**
* The type for constructing `RequestInit` body.
*
* https://developer.mozilla.org/docs/Web/API/RequestInit#body
*/
type _BodyInit = RequestInit['body'];
/**
* An alias to the builtin `Array<T>` type so we can
* easily alias it in import statements if there are name clashes.
*/
type _Array<T> = Array<T>;
/**
* An alias to the builtin `Record<K, T>` type so we can
* easily alias it in import statements if there are name clashes.
*/
type _Record<K extends keyof any, T> = Record<K, T>;
export type {
_Array as Array,
_BodyInit as BodyInit,
_HeadersInit as HeadersInit,
_Record as Record,
_RequestInfo as RequestInfo,
_RequestInit as RequestInit,
_Response as Response,
};
/**
* A copy of the builtin `EndingType` type as it isn't fully supported in certain
* environments and attempting to reference the global version will error.
*
* https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L27941
*/
type EndingType = 'native' | 'transparent';
/**
* A copy of the builtin `BlobPropertyBag` type as it isn't fully supported in certain
* environments and attempting to reference the global version will error.
*
* https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L154
* https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob#options
*/
export interface BlobPropertyBag {
endings?: EndingType;
type?: string;
}
/**
* A copy of the builtin `FilePropertyBag` type as it isn't fully supported in certain
* environments and attempting to reference the global version will error.
*
* https://github.com/microsoft/TypeScript/blob/49ad1a3917a0ea57f5ff248159256e12bb1cb705/src/lib/dom.generated.d.ts#L503
* https://developer.mozilla.org/en-US/docs/Web/API/File/File#options
*/
export interface FilePropertyBag extends BlobPropertyBag {
lastModified?: number;
}

135
node_modules/openai/src/internal/decoders/line.ts generated vendored Normal file
View File

@@ -0,0 +1,135 @@
import { concatBytes, decodeUTF8, encodeUTF8 } from '../utils/bytes';
export type Bytes = string | ArrayBuffer | Uint8Array | null | undefined;
/**
* A re-implementation of httpx's `LineDecoder` in Python that handles incrementally
* reading lines from text.
*
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
*/
export class LineDecoder {
// prettier-ignore
static NEWLINE_CHARS = new Set(['\n', '\r']);
static NEWLINE_REGEXP = /\r\n|[\n\r]/g;
#buffer: Uint8Array;
#carriageReturnIndex: number | null;
constructor() {
this.#buffer = new Uint8Array();
this.#carriageReturnIndex = null;
}
decode(chunk: Bytes): string[] {
if (chunk == null) {
return [];
}
const binaryChunk =
chunk instanceof ArrayBuffer ? new Uint8Array(chunk)
: typeof chunk === 'string' ? encodeUTF8(chunk)
: chunk;
this.#buffer = concatBytes([this.#buffer, binaryChunk]);
const lines: string[] = [];
let patternIndex;
while ((patternIndex = findNewlineIndex(this.#buffer, this.#carriageReturnIndex)) != null) {
if (patternIndex.carriage && this.#carriageReturnIndex == null) {
// skip until we either get a corresponding `\n`, a new `\r` or nothing
this.#carriageReturnIndex = patternIndex.index;
continue;
}
// we got double \r or \rtext\n
if (
this.#carriageReturnIndex != null &&
(patternIndex.index !== this.#carriageReturnIndex + 1 || patternIndex.carriage)
) {
lines.push(decodeUTF8(this.#buffer.subarray(0, this.#carriageReturnIndex - 1)));
this.#buffer = this.#buffer.subarray(this.#carriageReturnIndex);
this.#carriageReturnIndex = null;
continue;
}
const endIndex =
this.#carriageReturnIndex !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
const line = decodeUTF8(this.#buffer.subarray(0, endIndex));
lines.push(line);
this.#buffer = this.#buffer.subarray(patternIndex.index);
this.#carriageReturnIndex = null;
}
return lines;
}
flush(): string[] {
if (!this.#buffer.length) {
return [];
}
return this.decode('\n');
}
}
/**
* This function searches the buffer for the end patterns, (\r or \n)
* and returns an object with the index preceding the matched newline and the
* index after the newline char. `null` is returned if no new line is found.
*
* ```ts
* findNewLineIndex('abc\ndef') -> { preceding: 2, index: 3 }
* ```
*/
function findNewlineIndex(
buffer: Uint8Array,
startIndex: number | null,
): { preceding: number; index: number; carriage: boolean } | null {
const newline = 0x0a; // \n
const carriage = 0x0d; // \r
for (let i = startIndex ?? 0; i < buffer.length; i++) {
if (buffer[i] === newline) {
return { preceding: i, index: i + 1, carriage: false };
}
if (buffer[i] === carriage) {
return { preceding: i, index: i + 1, carriage: true };
}
}
return null;
}
export function findDoubleNewlineIndex(buffer: Uint8Array): number {
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
// and returns the index right after the first occurrence of any pattern,
// or -1 if none of the patterns are found.
const newline = 0x0a; // \n
const carriage = 0x0d; // \r
for (let i = 0; i < buffer.length - 1; i++) {
if (buffer[i] === newline && buffer[i + 1] === newline) {
// \n\n
return i + 2;
}
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
// \r\r
return i + 2;
}
if (
buffer[i] === carriage &&
buffer[i + 1] === newline &&
i + 3 < buffer.length &&
buffer[i + 2] === carriage &&
buffer[i + 3] === newline
) {
// \r\n\r\n
return i + 4;
}
}
return -1;
}

196
node_modules/openai/src/internal/detect-platform.ts generated vendored Normal file
View File

@@ -0,0 +1,196 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { VERSION } from '../version';
export const isRunningInBrowser = () => {
return (
// @ts-ignore
typeof window !== 'undefined' &&
// @ts-ignore
typeof window.document !== 'undefined' &&
// @ts-ignore
typeof navigator !== 'undefined'
);
};
type DetectedPlatform = 'deno' | 'node' | 'edge' | 'unknown';
/**
* Note this does not detect 'browser'; for that, use getBrowserInfo().
*/
function getDetectedPlatform(): DetectedPlatform {
if (typeof Deno !== 'undefined' && Deno.build != null) {
return 'deno';
}
if (typeof EdgeRuntime !== 'undefined') {
return 'edge';
}
if (
Object.prototype.toString.call(
typeof (globalThis as any).process !== 'undefined' ? (globalThis as any).process : 0,
) === '[object process]'
) {
return 'node';
}
return 'unknown';
}
declare const Deno: any;
declare const EdgeRuntime: any;
type Arch = 'x32' | 'x64' | 'arm' | 'arm64' | `other:${string}` | 'unknown';
type PlatformName =
| 'MacOS'
| 'Linux'
| 'Windows'
| 'FreeBSD'
| 'OpenBSD'
| 'iOS'
| 'Android'
| `Other:${string}`
| 'Unknown';
type Browser = 'ie' | 'edge' | 'chrome' | 'firefox' | 'safari';
type PlatformProperties = {
'X-Stainless-Lang': 'js';
'X-Stainless-Package-Version': string;
'X-Stainless-OS': PlatformName;
'X-Stainless-Arch': Arch;
'X-Stainless-Runtime': 'node' | 'deno' | 'edge' | `browser:${Browser}` | 'unknown';
'X-Stainless-Runtime-Version': string;
};
const getPlatformProperties = (): PlatformProperties => {
const detectedPlatform = getDetectedPlatform();
if (detectedPlatform === 'deno') {
return {
'X-Stainless-Lang': 'js',
'X-Stainless-Package-Version': VERSION,
'X-Stainless-OS': normalizePlatform(Deno.build.os),
'X-Stainless-Arch': normalizeArch(Deno.build.arch),
'X-Stainless-Runtime': 'deno',
'X-Stainless-Runtime-Version':
typeof Deno.version === 'string' ? Deno.version : Deno.version?.deno ?? 'unknown',
};
}
if (typeof EdgeRuntime !== 'undefined') {
return {
'X-Stainless-Lang': 'js',
'X-Stainless-Package-Version': VERSION,
'X-Stainless-OS': 'Unknown',
'X-Stainless-Arch': `other:${EdgeRuntime}`,
'X-Stainless-Runtime': 'edge',
'X-Stainless-Runtime-Version': (globalThis as any).process.version,
};
}
// Check if Node.js
if (detectedPlatform === 'node') {
return {
'X-Stainless-Lang': 'js',
'X-Stainless-Package-Version': VERSION,
'X-Stainless-OS': normalizePlatform((globalThis as any).process.platform ?? 'unknown'),
'X-Stainless-Arch': normalizeArch((globalThis as any).process.arch ?? 'unknown'),
'X-Stainless-Runtime': 'node',
'X-Stainless-Runtime-Version': (globalThis as any).process.version ?? 'unknown',
};
}
const browserInfo = getBrowserInfo();
if (browserInfo) {
return {
'X-Stainless-Lang': 'js',
'X-Stainless-Package-Version': VERSION,
'X-Stainless-OS': 'Unknown',
'X-Stainless-Arch': 'unknown',
'X-Stainless-Runtime': `browser:${browserInfo.browser}`,
'X-Stainless-Runtime-Version': browserInfo.version,
};
}
// TODO add support for Cloudflare workers, etc.
return {
'X-Stainless-Lang': 'js',
'X-Stainless-Package-Version': VERSION,
'X-Stainless-OS': 'Unknown',
'X-Stainless-Arch': 'unknown',
'X-Stainless-Runtime': 'unknown',
'X-Stainless-Runtime-Version': 'unknown',
};
};
type BrowserInfo = {
browser: Browser;
version: string;
};
declare const navigator: { userAgent: string } | undefined;
// Note: modified from https://github.com/JS-DevTools/host-environment/blob/b1ab79ecde37db5d6e163c050e54fe7d287d7c92/src/isomorphic.browser.ts
function getBrowserInfo(): BrowserInfo | null {
if (typeof navigator === 'undefined' || !navigator) {
return null;
}
// NOTE: The order matters here!
const browserPatterns = [
{ key: 'edge' as const, pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: 'ie' as const, pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: 'ie' as const, pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: 'chrome' as const, pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: 'firefox' as const, pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: 'safari' as const, pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ },
];
// Find the FIRST matching browser
for (const { key, pattern } of browserPatterns) {
const match = pattern.exec(navigator.userAgent);
if (match) {
const major = match[1] || 0;
const minor = match[2] || 0;
const patch = match[3] || 0;
return { browser: key, version: `${major}.${minor}.${patch}` };
}
}
return null;
}
const normalizeArch = (arch: string): Arch => {
// Node docs:
// - https://nodejs.org/api/process.html#processarch
// Deno docs:
// - https://doc.deno.land/deno/stable/~/Deno.build
if (arch === 'x32') return 'x32';
if (arch === 'x86_64' || arch === 'x64') return 'x64';
if (arch === 'arm') return 'arm';
if (arch === 'aarch64' || arch === 'arm64') return 'arm64';
if (arch) return `other:${arch}`;
return 'unknown';
};
const normalizePlatform = (platform: string): PlatformName => {
// Node platforms:
// - https://nodejs.org/api/process.html#processplatform
// Deno platforms:
// - https://doc.deno.land/deno/stable/~/Deno.build
// - https://github.com/denoland/deno/issues/14799
platform = platform.toLowerCase();
// NOTE: this iOS check is untested and may not work
// Node does not work natively on IOS, there is a fork at
// https://github.com/nodejs-mobile/nodejs-mobile
// however it is unknown at the time of writing how to detect if it is running
if (platform.includes('ios')) return 'iOS';
if (platform === 'android') return 'Android';
if (platform === 'darwin') return 'MacOS';
if (platform === 'win32') return 'Windows';
if (platform === 'freebsd') return 'FreeBSD';
if (platform === 'openbsd') return 'OpenBSD';
if (platform === 'linux') return 'Linux';
if (platform) return `Other:${platform}`;
return 'Unknown';
};
let _platformHeaders: PlatformProperties;
export const getPlatformHeaders = () => {
return (_platformHeaders ??= getPlatformProperties());
};

33
node_modules/openai/src/internal/errors.ts generated vendored Normal file
View File

@@ -0,0 +1,33 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export function isAbortError(err: unknown) {
return (
typeof err === 'object' &&
err !== null &&
// Spec-compliant fetch implementations
(('name' in err && (err as any).name === 'AbortError') ||
// Expo fetch
('message' in err && String((err as any).message).includes('FetchRequestCanceledException')))
);
}
export const castToError = (err: any): Error => {
if (err instanceof Error) return err;
if (typeof err === 'object' && err !== null) {
try {
if (Object.prototype.toString.call(err) === '[object Error]') {
// @ts-ignore - not all envs have native support for cause yet
const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
if (err.stack) error.stack = err.stack;
// @ts-ignore - not all envs have native support for cause yet
if (err.cause && !error.cause) error.cause = err.cause;
if (err.name) error.name = err.name;
return error;
}
} catch {}
try {
return new Error(JSON.stringify(err));
} catch {}
}
return new Error(err);
};

97
node_modules/openai/src/internal/headers.ts generated vendored Normal file
View File

@@ -0,0 +1,97 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { isReadonlyArray } from './utils/values';
type HeaderValue = string | undefined | null;
export type HeadersLike =
| Headers
| readonly HeaderValue[][]
| Record<string, HeaderValue | readonly HeaderValue[]>
| undefined
| null
| NullableHeaders;
const brand_privateNullableHeaders = /* @__PURE__ */ Symbol('brand.privateNullableHeaders');
/**
* @internal
* Users can pass explicit nulls to unset default headers. When we parse them
* into a standard headers type we need to preserve that information.
*/
export type NullableHeaders = {
/** Brand check, prevent users from creating a NullableHeaders. */
[brand_privateNullableHeaders]: true;
/** Parsed headers. */
values: Headers;
/** Set of lowercase header names explicitly set to null. */
nulls: Set<string>;
};
function* iterateHeaders(headers: HeadersLike): IterableIterator<readonly [string, string | null]> {
if (!headers) return;
if (brand_privateNullableHeaders in headers) {
const { values, nulls } = headers;
yield* values.entries();
for (const name of nulls) {
yield [name, null];
}
return;
}
let shouldClear = false;
let iter: Iterable<readonly (HeaderValue | readonly HeaderValue[])[]>;
if (headers instanceof Headers) {
iter = headers.entries();
} else if (isReadonlyArray(headers)) {
iter = headers;
} else {
shouldClear = true;
iter = Object.entries(headers ?? {});
}
for (let row of iter) {
const name = row[0];
if (typeof name !== 'string') throw new TypeError('expected header name to be a string');
const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];
let didClear = false;
for (const value of values) {
if (value === undefined) continue;
// Objects keys always overwrite older headers, they never append.
// Yield a null to clear the header before adding the new values.
if (shouldClear && !didClear) {
didClear = true;
yield [name, null];
}
yield [name, value];
}
}
}
export const buildHeaders = (newHeaders: HeadersLike[]): NullableHeaders => {
const targetHeaders = new Headers();
const nullHeaders = new Set<string>();
for (const headers of newHeaders) {
const seenHeaders = new Set<string>();
for (const [name, value] of iterateHeaders(headers)) {
const lowerName = name.toLowerCase();
if (!seenHeaders.has(lowerName)) {
targetHeaders.delete(name);
seenHeaders.add(lowerName);
}
if (value === null) {
targetHeaders.delete(name);
nullHeaders.add(lowerName);
} else {
targetHeaders.append(name, value);
nullHeaders.delete(lowerName);
}
}
}
return { [brand_privateNullableHeaders]: true, values: targetHeaders, nulls: nullHeaders };
};
export const isEmptyHeaders = (headers: HeadersLike) => {
for (const _ of iterateHeaders(headers)) return false;
return true;
};

84
node_modules/openai/src/internal/parse.ts generated vendored Normal file
View File

@@ -0,0 +1,84 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import type { FinalRequestOptions } from './request-options';
import { Stream } from '../core/streaming';
import { type OpenAI } from '../client';
import { formatRequestDetails, loggerFor } from './utils/log';
import type { AbstractPage } from '../pagination';
export type APIResponseProps = {
response: Response;
options: FinalRequestOptions;
controller: AbortController;
requestLogID: string;
retryOfRequestLogID: string | undefined;
startTime: number;
};
export async function defaultParseResponse<T>(
client: OpenAI,
props: APIResponseProps,
): Promise<WithRequestID<T>> {
const { response, requestLogID, retryOfRequestLogID, startTime } = props;
const body = await (async () => {
if (props.options.stream) {
loggerFor(client).debug('response', response.status, response.url, response.headers, response.body);
// Note: there is an invariant here that isn't represented in the type system
// that if you set `stream: true` the response type must also be `Stream<T>`
if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller, client) as any;
}
return Stream.fromSSEResponse(response, props.controller, client) as any;
}
// fetch refuses to read the body when the status code is 204.
if (response.status === 204) {
return null as T;
}
if (props.options.__binaryResponse) {
return response as unknown as T;
}
const contentType = response.headers.get('content-type');
const mediaType = contentType?.split(';')[0]?.trim();
const isJSON = mediaType?.includes('application/json') || mediaType?.endsWith('+json');
if (isJSON) {
const json = await response.json();
return addRequestID(json as T, response);
}
const text = await response.text();
return text as unknown as T;
})();
loggerFor(client).debug(
`[${requestLogID}] response parsed`,
formatRequestDetails({
retryOfRequestLogID,
url: response.url,
status: response.status,
body,
durationMs: Date.now() - startTime,
}),
);
return body;
}
export type WithRequestID<T> =
T extends Array<any> | Response | AbstractPage<any> ? T
: T extends Record<string, any> ? T & { _request_id?: string | null }
: T;
export function addRequestID<T>(value: T, response: Response): WithRequestID<T> {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
return value as WithRequestID<T>;
}
return Object.defineProperty(value, '_request_id', {
value: response.headers.get('x-request-id'),
enumerable: false,
}) as WithRequestID<T>;
}

13
node_modules/openai/src/internal/qs/LICENSE.md generated vendored Normal file
View File

@@ -0,0 +1,13 @@
BSD 3-Clause License
Copyright (c) 2014, Nathan LaFreniere and other [contributors](https://github.com/puruvj/neoqs/graphs/contributors) All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

3
node_modules/openai/src/internal/qs/README.md generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# qs
This is a vendored version of [neoqs](https://github.com/PuruVJ/neoqs) which is a TypeScript rewrite of [qs](https://github.com/ljharb/qs), a query string library.

10
node_modules/openai/src/internal/qs/formats.ts generated vendored Normal file
View File

@@ -0,0 +1,10 @@
import type { Format } from './types';
export const default_format: Format = 'RFC3986';
export const default_formatter = (v: PropertyKey) => String(v);
export const formatters: Record<Format, (str: PropertyKey) => string> = {
RFC1738: (v: PropertyKey) => String(v).replace(/%20/g, '+'),
RFC3986: default_formatter,
};
export const RFC1738 = 'RFC1738';
export const RFC3986 = 'RFC3986';

13
node_modules/openai/src/internal/qs/index.ts generated vendored Normal file
View File

@@ -0,0 +1,13 @@
import { default_format, formatters, RFC1738, RFC3986 } from './formats';
const formats = {
formatters,
RFC1738,
RFC3986,
default: default_format,
};
export { stringify } from './stringify';
export { formats };
export type { DefaultDecoder, DefaultEncoder, Format, ParseOptions, StringifyOptions } from './types';

385
node_modules/openai/src/internal/qs/stringify.ts generated vendored Normal file
View File

@@ -0,0 +1,385 @@
import { encode, is_buffer, maybe_map, has } from './utils';
import { default_format, default_formatter, formatters } from './formats';
import type { NonNullableProperties, StringifyOptions } from './types';
import { isArray } from '../utils/values';
const array_prefix_generators = {
brackets(prefix: PropertyKey) {
return String(prefix) + '[]';
},
comma: 'comma',
indices(prefix: PropertyKey, key: string) {
return String(prefix) + '[' + key + ']';
},
repeat(prefix: PropertyKey) {
return String(prefix);
},
};
const push_to_array = function (arr: any[], value_or_array: any) {
Array.prototype.push.apply(arr, isArray(value_or_array) ? value_or_array : [value_or_array]);
};
let toISOString;
const defaults = {
addQueryPrefix: false,
allowDots: false,
allowEmptyArrays: false,
arrayFormat: 'indices',
charset: 'utf-8',
charsetSentinel: false,
delimiter: '&',
encode: true,
encodeDotInKeys: false,
encoder: encode,
encodeValuesOnly: false,
format: default_format,
formatter: default_formatter,
/** @deprecated */
indices: false,
serializeDate(date) {
return (toISOString ??= Function.prototype.call.bind(Date.prototype.toISOString))(date);
},
skipNulls: false,
strictNullHandling: false,
} as NonNullableProperties<StringifyOptions & { formatter: (typeof formatters)['RFC1738'] }>;
function is_non_nullish_primitive(v: unknown): v is string | number | boolean | symbol | bigint {
return (
typeof v === 'string' ||
typeof v === 'number' ||
typeof v === 'boolean' ||
typeof v === 'symbol' ||
typeof v === 'bigint'
);
}
const sentinel = {};
function inner_stringify(
object: any,
prefix: PropertyKey,
generateArrayPrefix: StringifyOptions['arrayFormat'] | ((prefix: string, key: string) => string),
commaRoundTrip: boolean,
allowEmptyArrays: boolean,
strictNullHandling: boolean,
skipNulls: boolean,
encodeDotInKeys: boolean,
encoder: StringifyOptions['encoder'],
filter: StringifyOptions['filter'],
sort: StringifyOptions['sort'],
allowDots: StringifyOptions['allowDots'],
serializeDate: StringifyOptions['serializeDate'],
format: StringifyOptions['format'],
formatter: StringifyOptions['formatter'],
encodeValuesOnly: boolean,
charset: StringifyOptions['charset'],
sideChannel: WeakMap<any, any>,
) {
let obj = object;
let tmp_sc = sideChannel;
let step = 0;
let find_flag = false;
while ((tmp_sc = tmp_sc.get(sentinel)) !== void undefined && !find_flag) {
// Where object last appeared in the ref tree
const pos = tmp_sc.get(object);
step += 1;
if (typeof pos !== 'undefined') {
if (pos === step) {
throw new RangeError('Cyclic object value');
} else {
find_flag = true; // Break while
}
}
if (typeof tmp_sc.get(sentinel) === 'undefined') {
step = 0;
}
}
if (typeof filter === 'function') {
obj = filter(prefix, obj);
} else if (obj instanceof Date) {
obj = serializeDate?.(obj);
} else if (generateArrayPrefix === 'comma' && isArray(obj)) {
obj = maybe_map(obj, function (value) {
if (value instanceof Date) {
return serializeDate?.(value);
}
return value;
});
}
if (obj === null) {
if (strictNullHandling) {
return encoder && !encodeValuesOnly ?
// @ts-expect-error
encoder(prefix, defaults.encoder, charset, 'key', format)
: prefix;
}
obj = '';
}
if (is_non_nullish_primitive(obj) || is_buffer(obj)) {
if (encoder) {
const key_value =
encodeValuesOnly ? prefix
// @ts-expect-error
: encoder(prefix, defaults.encoder, charset, 'key', format);
return [
formatter?.(key_value) +
'=' +
// @ts-expect-error
formatter?.(encoder(obj, defaults.encoder, charset, 'value', format)),
];
}
return [formatter?.(prefix) + '=' + formatter?.(String(obj))];
}
const values: string[] = [];
if (typeof obj === 'undefined') {
return values;
}
let obj_keys;
if (generateArrayPrefix === 'comma' && isArray(obj)) {
// we need to join elements in
if (encodeValuesOnly && encoder) {
// @ts-expect-error values only
obj = maybe_map(obj, encoder);
}
obj_keys = [{ value: obj.length > 0 ? obj.join(',') || null : void undefined }];
} else if (isArray(filter)) {
obj_keys = filter;
} else {
const keys = Object.keys(obj);
obj_keys = sort ? keys.sort(sort) : keys;
}
const encoded_prefix = encodeDotInKeys ? String(prefix).replace(/\./g, '%2E') : String(prefix);
const adjusted_prefix =
commaRoundTrip && isArray(obj) && obj.length === 1 ? encoded_prefix + '[]' : encoded_prefix;
if (allowEmptyArrays && isArray(obj) && obj.length === 0) {
return adjusted_prefix + '[]';
}
for (let j = 0; j < obj_keys.length; ++j) {
const key = obj_keys[j];
const value =
// @ts-ignore
typeof key === 'object' && typeof key.value !== 'undefined' ? key.value : obj[key as any];
if (skipNulls && value === null) {
continue;
}
// @ts-ignore
const encoded_key = allowDots && encodeDotInKeys ? (key as any).replace(/\./g, '%2E') : key;
const key_prefix =
isArray(obj) ?
typeof generateArrayPrefix === 'function' ?
generateArrayPrefix(adjusted_prefix, encoded_key)
: adjusted_prefix
: adjusted_prefix + (allowDots ? '.' + encoded_key : '[' + encoded_key + ']');
sideChannel.set(object, step);
const valueSideChannel = new WeakMap();
valueSideChannel.set(sentinel, sideChannel);
push_to_array(
values,
inner_stringify(
value,
key_prefix,
generateArrayPrefix,
commaRoundTrip,
allowEmptyArrays,
strictNullHandling,
skipNulls,
encodeDotInKeys,
// @ts-ignore
generateArrayPrefix === 'comma' && encodeValuesOnly && isArray(obj) ? null : encoder,
filter,
sort,
allowDots,
serializeDate,
format,
formatter,
encodeValuesOnly,
charset,
valueSideChannel,
),
);
}
return values;
}
function normalize_stringify_options(
opts: StringifyOptions = defaults,
): NonNullableProperties<Omit<StringifyOptions, 'indices'>> & { indices?: boolean } {
if (typeof opts.allowEmptyArrays !== 'undefined' && typeof opts.allowEmptyArrays !== 'boolean') {
throw new TypeError('`allowEmptyArrays` option can only be `true` or `false`, when provided');
}
if (typeof opts.encodeDotInKeys !== 'undefined' && typeof opts.encodeDotInKeys !== 'boolean') {
throw new TypeError('`encodeDotInKeys` option can only be `true` or `false`, when provided');
}
if (opts.encoder !== null && typeof opts.encoder !== 'undefined' && typeof opts.encoder !== 'function') {
throw new TypeError('Encoder has to be a function.');
}
const charset = opts.charset || defaults.charset;
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') {
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined');
}
let format = default_format;
if (typeof opts.format !== 'undefined') {
if (!has(formatters, opts.format)) {
throw new TypeError('Unknown format option provided.');
}
format = opts.format;
}
const formatter = formatters[format];
let filter = defaults.filter;
if (typeof opts.filter === 'function' || isArray(opts.filter)) {
filter = opts.filter;
}
let arrayFormat: StringifyOptions['arrayFormat'];
if (opts.arrayFormat && opts.arrayFormat in array_prefix_generators) {
arrayFormat = opts.arrayFormat;
} else if ('indices' in opts) {
arrayFormat = opts.indices ? 'indices' : 'repeat';
} else {
arrayFormat = defaults.arrayFormat;
}
if ('commaRoundTrip' in opts && typeof opts.commaRoundTrip !== 'boolean') {
throw new TypeError('`commaRoundTrip` must be a boolean, or absent');
}
const allowDots =
typeof opts.allowDots === 'undefined' ?
!!opts.encodeDotInKeys === true ?
true
: defaults.allowDots
: !!opts.allowDots;
return {
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix,
// @ts-ignore
allowDots: allowDots,
allowEmptyArrays:
typeof opts.allowEmptyArrays === 'boolean' ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
arrayFormat: arrayFormat,
charset: charset,
charsetSentinel:
typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel,
commaRoundTrip: !!opts.commaRoundTrip,
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter,
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode,
encodeDotInKeys:
typeof opts.encodeDotInKeys === 'boolean' ? opts.encodeDotInKeys : defaults.encodeDotInKeys,
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder,
encodeValuesOnly:
typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly,
filter: filter,
format: format,
formatter: formatter,
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate,
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls,
// @ts-ignore
sort: typeof opts.sort === 'function' ? opts.sort : null,
strictNullHandling:
typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling,
};
}
export function stringify(object: any, opts: StringifyOptions = {}) {
let obj = object;
const options = normalize_stringify_options(opts);
let obj_keys: PropertyKey[] | undefined;
let filter;
if (typeof options.filter === 'function') {
filter = options.filter;
obj = filter('', obj);
} else if (isArray(options.filter)) {
filter = options.filter;
obj_keys = filter;
}
const keys: string[] = [];
if (typeof obj !== 'object' || obj === null) {
return '';
}
const generateArrayPrefix = array_prefix_generators[options.arrayFormat];
const commaRoundTrip = generateArrayPrefix === 'comma' && options.commaRoundTrip;
if (!obj_keys) {
obj_keys = Object.keys(obj);
}
if (options.sort) {
obj_keys.sort(options.sort);
}
const sideChannel = new WeakMap();
for (let i = 0; i < obj_keys.length; ++i) {
const key = obj_keys[i]!;
if (options.skipNulls && obj[key] === null) {
continue;
}
push_to_array(
keys,
inner_stringify(
obj[key],
key,
// @ts-expect-error
generateArrayPrefix,
commaRoundTrip,
options.allowEmptyArrays,
options.strictNullHandling,
options.skipNulls,
options.encodeDotInKeys,
options.encode ? options.encoder : null,
options.filter,
options.sort,
options.allowDots,
options.serializeDate,
options.format,
options.formatter,
options.encodeValuesOnly,
options.charset,
sideChannel,
),
);
}
const joined = keys.join(options.delimiter);
let prefix = options.addQueryPrefix === true ? '?' : '';
if (options.charsetSentinel) {
if (options.charset === 'iso-8859-1') {
// encodeURIComponent('&#10003;'), the "numeric entity" representation of a checkmark
prefix += 'utf8=%26%2310003%3B&';
} else {
// encodeURIComponent('✓')
prefix += 'utf8=%E2%9C%93&';
}
}
return joined.length > 0 ? prefix + joined : '';
}

71
node_modules/openai/src/internal/qs/types.ts generated vendored Normal file
View File

@@ -0,0 +1,71 @@
export type Format = 'RFC1738' | 'RFC3986';
export type DefaultEncoder = (str: any, defaultEncoder?: any, charset?: string) => string;
export type DefaultDecoder = (str: string, decoder?: any, charset?: string) => string;
export type BooleanOptional = boolean | undefined;
export type StringifyBaseOptions = {
delimiter?: string;
allowDots?: boolean;
encodeDotInKeys?: boolean;
strictNullHandling?: boolean;
skipNulls?: boolean;
encode?: boolean;
encoder?: (
str: any,
defaultEncoder: DefaultEncoder,
charset: string,
type: 'key' | 'value',
format?: Format,
) => string;
filter?: Array<PropertyKey> | ((prefix: PropertyKey, value: any) => any);
arrayFormat?: 'indices' | 'brackets' | 'repeat' | 'comma';
indices?: boolean;
sort?: ((a: PropertyKey, b: PropertyKey) => number) | null;
serializeDate?: (d: Date) => string;
format?: 'RFC1738' | 'RFC3986';
formatter?: (str: PropertyKey) => string;
encodeValuesOnly?: boolean;
addQueryPrefix?: boolean;
charset?: 'utf-8' | 'iso-8859-1';
charsetSentinel?: boolean;
allowEmptyArrays?: boolean;
commaRoundTrip?: boolean;
};
export type StringifyOptions = StringifyBaseOptions;
export type ParseBaseOptions = {
comma?: boolean;
delimiter?: string | RegExp;
depth?: number | false;
decoder?: (str: string, defaultDecoder: DefaultDecoder, charset: string, type: 'key' | 'value') => any;
arrayLimit?: number;
parseArrays?: boolean;
plainObjects?: boolean;
allowPrototypes?: boolean;
allowSparse?: boolean;
parameterLimit?: number;
strictDepth?: boolean;
strictNullHandling?: boolean;
ignoreQueryPrefix?: boolean;
charset?: 'utf-8' | 'iso-8859-1';
charsetSentinel?: boolean;
interpretNumericEntities?: boolean;
allowEmptyArrays?: boolean;
duplicates?: 'combine' | 'first' | 'last';
allowDots?: boolean;
decodeDotInKeys?: boolean;
};
export type ParseOptions = ParseBaseOptions;
export type ParsedQs = {
[key: string]: undefined | string | string[] | ParsedQs | ParsedQs[];
};
// Type to remove null or undefined union from each property
export type NonNullableProperties<T> = {
[K in keyof T]-?: Exclude<T[K], undefined | null>;
};

265
node_modules/openai/src/internal/qs/utils.ts generated vendored Normal file
View File

@@ -0,0 +1,265 @@
import { RFC1738 } from './formats';
import type { DefaultEncoder, Format } from './types';
import { isArray } from '../utils/values';
export let has = (obj: object, key: PropertyKey): boolean => (
(has = (Object as any).hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty)),
has(obj, key)
);
const hex_table = /* @__PURE__ */ (() => {
const array = [];
for (let i = 0; i < 256; ++i) {
array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
}
return array;
})();
function compact_queue<T extends Record<string, any>>(queue: Array<{ obj: T; prop: string }>) {
while (queue.length > 1) {
const item = queue.pop();
if (!item) continue;
const obj = item.obj[item.prop];
if (isArray(obj)) {
const compacted: unknown[] = [];
for (let j = 0; j < obj.length; ++j) {
if (typeof obj[j] !== 'undefined') {
compacted.push(obj[j]);
}
}
// @ts-ignore
item.obj[item.prop] = compacted;
}
}
}
function array_to_object(source: any[], options: { plainObjects: boolean }) {
const obj = options && options.plainObjects ? Object.create(null) : {};
for (let i = 0; i < source.length; ++i) {
if (typeof source[i] !== 'undefined') {
obj[i] = source[i];
}
}
return obj;
}
export function merge(
target: any,
source: any,
options: { plainObjects?: boolean; allowPrototypes?: boolean } = {},
) {
if (!source) {
return target;
}
if (typeof source !== 'object') {
if (isArray(target)) {
target.push(source);
} else if (target && typeof target === 'object') {
if ((options && (options.plainObjects || options.allowPrototypes)) || !has(Object.prototype, source)) {
target[source] = true;
}
} else {
return [target, source];
}
return target;
}
if (!target || typeof target !== 'object') {
return [target].concat(source);
}
let mergeTarget = target;
if (isArray(target) && !isArray(source)) {
// @ts-ignore
mergeTarget = array_to_object(target, options);
}
if (isArray(target) && isArray(source)) {
source.forEach(function (item, i) {
if (has(target, i)) {
const targetItem = target[i];
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
target[i] = merge(targetItem, item, options);
} else {
target.push(item);
}
} else {
target[i] = item;
}
});
return target;
}
return Object.keys(source).reduce(function (acc, key) {
const value = source[key];
if (has(acc, key)) {
acc[key] = merge(acc[key], value, options);
} else {
acc[key] = value;
}
return acc;
}, mergeTarget);
}
export function assign_single_source(target: any, source: any) {
return Object.keys(source).reduce(function (acc, key) {
acc[key] = source[key];
return acc;
}, target);
}
export function decode(str: string, _: any, charset: string) {
const strWithoutPlus = str.replace(/\+/g, ' ');
if (charset === 'iso-8859-1') {
// unescape never throws, no try...catch needed:
return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
}
// utf-8
try {
return decodeURIComponent(strWithoutPlus);
} catch (e) {
return strWithoutPlus;
}
}
const limit = 1024;
export const encode: (
str: any,
defaultEncoder: DefaultEncoder,
charset: string,
type: 'key' | 'value',
format: Format,
) => string = (str, _defaultEncoder, charset, _kind, format: Format) => {
// This code was originally written by Brian White for the io.js core querystring library.
// It has been adapted here for stricter adherence to RFC 3986
if (str.length === 0) {
return str;
}
let string = str;
if (typeof str === 'symbol') {
string = Symbol.prototype.toString.call(str);
} else if (typeof str !== 'string') {
string = String(str);
}
if (charset === 'iso-8859-1') {
return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
});
}
let out = '';
for (let j = 0; j < string.length; j += limit) {
const segment = string.length >= limit ? string.slice(j, j + limit) : string;
const arr = [];
for (let i = 0; i < segment.length; ++i) {
let c = segment.charCodeAt(i);
if (
c === 0x2d || // -
c === 0x2e || // .
c === 0x5f || // _
c === 0x7e || // ~
(c >= 0x30 && c <= 0x39) || // 0-9
(c >= 0x41 && c <= 0x5a) || // a-z
(c >= 0x61 && c <= 0x7a) || // A-Z
(format === RFC1738 && (c === 0x28 || c === 0x29)) // ( )
) {
arr[arr.length] = segment.charAt(i);
continue;
}
if (c < 0x80) {
arr[arr.length] = hex_table[c];
continue;
}
if (c < 0x800) {
arr[arr.length] = hex_table[0xc0 | (c >> 6)]! + hex_table[0x80 | (c & 0x3f)];
continue;
}
if (c < 0xd800 || c >= 0xe000) {
arr[arr.length] =
hex_table[0xe0 | (c >> 12)]! + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
continue;
}
i += 1;
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
arr[arr.length] =
hex_table[0xf0 | (c >> 18)]! +
hex_table[0x80 | ((c >> 12) & 0x3f)] +
hex_table[0x80 | ((c >> 6) & 0x3f)] +
hex_table[0x80 | (c & 0x3f)];
}
out += arr.join('');
}
return out;
};
export function compact(value: any) {
const queue = [{ obj: { o: value }, prop: 'o' }];
const refs = [];
for (let i = 0; i < queue.length; ++i) {
const item = queue[i];
// @ts-ignore
const obj = item.obj[item.prop];
const keys = Object.keys(obj);
for (let j = 0; j < keys.length; ++j) {
const key = keys[j]!;
const val = obj[key];
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
queue.push({ obj: obj, prop: key });
refs.push(val);
}
}
}
compact_queue(queue);
return value;
}
export function is_regexp(obj: any) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
export function is_buffer(obj: any) {
if (!obj || typeof obj !== 'object') {
return false;
}
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
}
export function combine(a: any, b: any) {
return [].concat(a, b);
}
export function maybe_map<T>(val: T[], fn: (v: T) => T) {
if (isArray(val)) {
const mapped = [];
for (let i = 0; i < val.length; i += 1) {
mapped.push(fn(val[i]!));
}
return mapped;
}
return fn(val);
}

94
node_modules/openai/src/internal/request-options.ts generated vendored Normal file
View File

@@ -0,0 +1,94 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { NullableHeaders } from './headers';
import type { BodyInit } from './builtin-types';
import { Stream } from '../core/streaming';
import type { HTTPMethod, MergedRequestInit } from './types';
import { type HeadersLike } from './headers';
export type FinalRequestOptions = RequestOptions & { method: HTTPMethod; path: string };
export type RequestOptions = {
/**
* The HTTP method for the request (e.g., 'get', 'post', 'put', 'delete').
*/
method?: HTTPMethod;
/**
* The URL path for the request.
*
* @example "/v1/foo"
*/
path?: string;
/**
* Query parameters to include in the request URL.
*/
query?: object | undefined | null;
/**
* The request body. Can be a string, JSON object, FormData, or other supported types.
*/
body?: unknown;
/**
* HTTP headers to include with the request. Can be a Headers object, plain object, or array of tuples.
*/
headers?: HeadersLike;
/**
* The maximum number of times that the client will retry a request in case of a
* temporary failure, like a network error or a 5XX error from the server.
*
* @default 2
*/
maxRetries?: number;
stream?: boolean | undefined;
/**
* The maximum amount of time (in milliseconds) that the client should wait for a response
* from the server before timing out a single request.
*
* @unit milliseconds
*/
timeout?: number;
/**
* Additional `RequestInit` options to be passed to the underlying `fetch` call.
* These options will be merged with the client's default fetch options.
*/
fetchOptions?: MergedRequestInit;
/**
* An AbortSignal that can be used to cancel the request.
*/
signal?: AbortSignal | undefined | null;
/**
* A unique key for this request to enable idempotency.
*/
idempotencyKey?: string;
/**
* Override the default base URL for this specific request.
*/
defaultBaseURL?: string | undefined;
__metadata?: Record<string, unknown>;
__binaryResponse?: boolean | undefined;
__streamClass?: typeof Stream;
};
export type EncodedContent = { bodyHeaders: HeadersLike; body: BodyInit };
export type RequestEncoder = (request: { headers: NullableHeaders; body: unknown }) => EncodedContent;
export const FallbackEncoder: RequestEncoder = ({ headers, body }) => {
return {
bodyHeaders: {
'content-type': 'application/json',
},
body: JSON.stringify(body),
};
};

26
node_modules/openai/src/internal/shim-types.ts generated vendored Normal file
View File

@@ -0,0 +1,26 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
/**
* Shims for types that we can't always rely on being available globally.
*
* Note: these only exist at the type-level, there is no corresponding runtime
* version for any of these symbols.
*/
type NeverToAny<T> = T extends never ? any : T;
/** @ts-ignore */
type _DOMReadableStream<R = any> = globalThis.ReadableStream<R>;
/** @ts-ignore */
type _NodeReadableStream<R = any> = import('stream/web').ReadableStream<R>;
type _ConditionalNodeReadableStream<R = any> =
typeof globalThis extends { ReadableStream: any } ? never : _NodeReadableStream<R>;
type _ReadableStream<R = any> = NeverToAny<
| ([0] extends [1 & _DOMReadableStream<R>] ? never : _DOMReadableStream<R>)
| ([0] extends [1 & _ConditionalNodeReadableStream<R>] ? never : _ConditionalNodeReadableStream<R>)
>;
export type { _ReadableStream as ReadableStream };

107
node_modules/openai/src/internal/shims.ts generated vendored Normal file
View File

@@ -0,0 +1,107 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
/**
* This module provides internal shims and utility functions for environments where certain Node.js or global types may not be available.
*
* These are used to ensure we can provide a consistent behaviour between different JavaScript environments and good error
* messages in cases where an environment isn't fully supported.
*/
import type { Fetch } from './builtin-types';
import type { ReadableStream } from './shim-types';
export function getDefaultFetch(): Fetch {
if (typeof fetch !== 'undefined') {
return fetch as any;
}
throw new Error(
'`fetch` is not defined as a global; Either pass `fetch` to the client, `new OpenAI({ fetch })` or polyfill the global, `globalThis.fetch = fetch`',
);
}
type ReadableStreamArgs = ConstructorParameters<typeof ReadableStream>;
export function makeReadableStream(...args: ReadableStreamArgs): ReadableStream {
const ReadableStream = (globalThis as any).ReadableStream;
if (typeof ReadableStream === 'undefined') {
// Note: All of the platforms / runtimes we officially support already define
// `ReadableStream` as a global, so this should only ever be hit on unsupported runtimes.
throw new Error(
'`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`',
);
}
return new ReadableStream(...args);
}
export function ReadableStreamFrom<T>(iterable: Iterable<T> | AsyncIterable<T>): ReadableStream<T> {
let iter: AsyncIterator<T> | Iterator<T> =
Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
return makeReadableStream({
start() {},
async pull(controller: any) {
const { done, value } = await iter.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
},
async cancel() {
await iter.return?.();
},
});
}
/**
* Most browsers don't yet have async iterable support for ReadableStream,
* and Node has a very different way of reading bytes from its "ReadableStream".
*
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
*/
export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
if (stream[Symbol.asyncIterator]) return stream;
const reader = stream.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
return result;
} catch (e) {
reader.releaseLock(); // release lock when stream becomes errored
throw e;
}
},
async return() {
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
return { done: true, value: undefined };
},
[Symbol.asyncIterator]() {
return this;
},
};
}
/**
* Cancels a ReadableStream we don't need to consume.
* See https://undici.nodejs.org/#/?id=garbage-collection
*/
export async function CancelReadableStream(stream: any): Promise<void> {
if (stream === null || typeof stream !== 'object') return;
if (stream[Symbol.asyncIterator]) {
await stream[Symbol.asyncIterator]().return?.();
return;
}
const reader = stream.getReader();
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
}

32
node_modules/openai/src/internal/stream-utils.ts generated vendored Normal file
View File

@@ -0,0 +1,32 @@
/**
* Most browsers don't yet have async iterable support for ReadableStream,
* and Node has a very different way of reading bytes from its "ReadableStream".
*
* This polyfill was pulled from https://github.com/MattiasBuelens/web-streams-polyfill/pull/122#issuecomment-1627354490
*/
export function ReadableStreamToAsyncIterable<T>(stream: any): AsyncIterableIterator<T> {
if (stream[Symbol.asyncIterator]) return stream;
const reader = stream.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result?.done) reader.releaseLock(); // release lock when stream becomes closed
return result;
} catch (e) {
reader.releaseLock(); // release lock when stream becomes errored
throw e;
}
},
async return() {
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
return { done: true, value: undefined };
},
[Symbol.asyncIterator]() {
return this;
},
};
}

154
node_modules/openai/src/internal/to-file.ts generated vendored Normal file
View File

@@ -0,0 +1,154 @@
import { BlobPart, getName, makeFile, isAsyncIterable } from './uploads';
import type { FilePropertyBag } from './builtin-types';
import { checkFileSupport } from './uploads';
type BlobLikePart = string | ArrayBuffer | ArrayBufferView | BlobLike | DataView;
/**
* Intended to match DOM Blob, node-fetch Blob, node:buffer Blob, etc.
* Don't add arrayBuffer here, node-fetch doesn't have it
*/
interface BlobLike {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) */
readonly size: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) */
readonly type: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) */
text(): Promise<string>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
slice(start?: number, end?: number): BlobLike;
}
/**
* This check adds the arrayBuffer() method type because it is available and used at runtime
*/
const isBlobLike = (value: any): value is BlobLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
value != null &&
typeof value === 'object' &&
typeof value.size === 'number' &&
typeof value.type === 'string' &&
typeof value.text === 'function' &&
typeof value.slice === 'function' &&
typeof value.arrayBuffer === 'function';
/**
* Intended to match DOM File, node:buffer File, undici File, etc.
*/
interface FileLike extends BlobLike {
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) */
readonly lastModified: number;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) */
readonly name?: string | undefined;
}
/**
* This check adds the arrayBuffer() method type because it is available and used at runtime
*/
const isFileLike = (value: any): value is FileLike & { arrayBuffer(): Promise<ArrayBuffer> } =>
value != null &&
typeof value === 'object' &&
typeof value.name === 'string' &&
typeof value.lastModified === 'number' &&
isBlobLike(value);
/**
* Intended to match DOM Response, node-fetch Response, undici Response, etc.
*/
export interface ResponseLike {
url: string;
blob(): Promise<BlobLike>;
}
const isResponseLike = (value: any): value is ResponseLike =>
value != null &&
typeof value === 'object' &&
typeof value.url === 'string' &&
typeof value.blob === 'function';
export type ToFileInput =
| FileLike
| ResponseLike
| Exclude<BlobLikePart, string>
| AsyncIterable<BlobLikePart>;
/**
* Helper for creating a {@link File} to pass to an SDK upload method from a variety of different data formats
* @param value the raw content of the file. Can be an {@link Uploadable}, {@link BlobLikePart}, or {@link AsyncIterable} of {@link BlobLikePart}s
* @param {string=} name the name of the file. If omitted, toFile will try to determine a file name from bits if possible
* @param {Object=} options additional properties
* @param {string=} options.type the MIME type of the content
* @param {number=} options.lastModified the last modified timestamp
* @returns a {@link File} with the given properties
*/
export async function toFile(
value: ToFileInput | PromiseLike<ToFileInput>,
name?: string | null | undefined,
options?: FilePropertyBag | undefined,
): Promise<File> {
checkFileSupport();
// If it's a promise, resolve it.
value = await value;
// If we've been given a `File` we don't need to do anything
if (isFileLike(value)) {
if (value instanceof File) {
return value;
}
return makeFile([await value.arrayBuffer()], value.name);
}
if (isResponseLike(value)) {
const blob = await value.blob();
name ||= new URL(value.url).pathname.split(/[\\/]/).pop();
return makeFile(await getBytes(blob), name, options);
}
const parts = await getBytes(value);
name ||= getName(value);
if (!options?.type) {
const type = parts.find((part) => typeof part === 'object' && 'type' in part && part.type);
if (typeof type === 'string') {
options = { ...options, type };
}
}
return makeFile(parts, name, options);
}
async function getBytes(value: BlobLikePart | AsyncIterable<BlobLikePart>): Promise<Array<BlobPart>> {
let parts: Array<BlobPart> = [];
if (
typeof value === 'string' ||
ArrayBuffer.isView(value) || // includes Uint8Array, Buffer, etc.
value instanceof ArrayBuffer
) {
parts.push(value);
} else if (isBlobLike(value)) {
parts.push(value instanceof Blob ? value : await value.arrayBuffer());
} else if (
isAsyncIterable(value) // includes Readable, ReadableStream, etc.
) {
for await (const chunk of value) {
parts.push(...(await getBytes(chunk as BlobLikePart))); // TODO, consider validating?
}
} else {
const constructor = value?.constructor?.name;
throw new Error(
`Unexpected data type: ${typeof value}${
constructor ? `; constructor: ${constructor}` : ''
}${propsForError(value)}`,
);
}
return parts;
}
function propsForError(value: unknown): string {
if (typeof value !== 'object' || value === null) return '';
const props = Object.getOwnPropertyNames(value);
return `; props: [${props.map((p) => `"${p}"`).join(', ')}]`;
}

95
node_modules/openai/src/internal/types.ts generated vendored Normal file
View File

@@ -0,0 +1,95 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export type PromiseOrValue<T> = T | Promise<T>;
export type HTTPMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
export type FinalizedRequestInit = RequestInit & { headers: Headers };
type NotAny<T> = [0] extends [1 & T] ? never : T;
/**
* Some environments overload the global fetch function, and Parameters<T> only gets the last signature.
*/
type OverloadedParameters<T> =
T extends (
{
(...args: infer A): unknown;
(...args: infer B): unknown;
(...args: infer C): unknown;
(...args: infer D): unknown;
}
) ?
A | B | C | D
: T extends (
{
(...args: infer A): unknown;
(...args: infer B): unknown;
(...args: infer C): unknown;
}
) ?
A | B | C
: T extends (
{
(...args: infer A): unknown;
(...args: infer B): unknown;
}
) ?
A | B
: T extends (...args: infer A) => unknown ? A
: never;
/* eslint-disable */
/**
* These imports attempt to get types from a parent package's dependencies.
* Unresolved bare specifiers can trigger [automatic type acquisition][1] in some projects, which
* would cause typescript to show types not present at runtime. To avoid this, we import
* directly from parent node_modules folders.
*
* We need to check multiple levels because we don't know what directory structure we'll be in.
* For example, pnpm generates directories like this:
* ```
* node_modules
* ├── .pnpm
* │ └── pkg@1.0.0
* │ └── node_modules
* │ └── pkg
* │ └── internal
* │ └── types.d.ts
* ├── pkg -> .pnpm/pkg@1.0.0/node_modules/pkg
* └── undici
* ```
*
* [1]: https://www.typescriptlang.org/tsconfig/#typeAcquisition
*/
/** @ts-ignore For users with \@types/node */
type UndiciTypesRequestInit = NotAny<import('../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici-types/index.d.ts').RequestInit>;
/** @ts-ignore For users with undici */
type UndiciRequestInit = NotAny<import('../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/undici/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/undici/index.d.ts').RequestInit>;
/** @ts-ignore For users with \@types/bun */
type BunRequestInit = globalThis.FetchRequestInit;
/** @ts-ignore For users with node-fetch@2 */
type NodeFetch2RequestInit = NotAny<import('../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/@types/node-fetch/index.d.ts').RequestInit>;
/** @ts-ignore For users with node-fetch@3, doesn't need file extension because types are at ./@types/index.d.ts */
type NodeFetch3RequestInit = NotAny<import('../node_modules/node-fetch').RequestInit> | NotAny<import('../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../node_modules/node-fetch').RequestInit> | NotAny<import('../../../../../../../../../../node_modules/node-fetch').RequestInit>;
/** @ts-ignore For users who use Deno */
type FetchRequestInit = NonNullable<OverloadedParameters<typeof fetch>[1]>;
/* eslint-enable */
type RequestInits =
| NotAny<UndiciTypesRequestInit>
| NotAny<UndiciRequestInit>
| NotAny<BunRequestInit>
| NotAny<NodeFetch2RequestInit>
| NotAny<NodeFetch3RequestInit>
| NotAny<RequestInit>
| NotAny<FetchRequestInit>;
/**
* This type contains `RequestInit` options that may be available on the current runtime,
* including per-platform extensions like `dispatcher`, `agent`, `client`, etc.
*/
export type MergedRequestInit = RequestInits &
/** We don't include these in the types as they'll be overridden for every request. */
Partial<Record<'body' | 'headers' | 'method' | 'signal', never>>;

187
node_modules/openai/src/internal/uploads.ts generated vendored Normal file
View File

@@ -0,0 +1,187 @@
import { type RequestOptions } from './request-options';
import type { FilePropertyBag, Fetch } from './builtin-types';
import type { OpenAI } from '../client';
import { ReadableStreamFrom } from './shims';
export type BlobPart = string | ArrayBuffer | ArrayBufferView | Blob | DataView;
type FsReadStream = AsyncIterable<Uint8Array> & { path: string | { toString(): string } };
// https://github.com/oven-sh/bun/issues/5980
interface BunFile extends Blob {
readonly name?: string | undefined;
}
export const checkFileSupport = () => {
if (typeof File === 'undefined') {
const { process } = globalThis as any;
const isOldNode =
typeof process?.versions?.node === 'string' && parseInt(process.versions.node.split('.')) < 20;
throw new Error(
'`File` is not defined as a global, which is required for file uploads.' +
(isOldNode ?
" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`."
: ''),
);
}
};
/**
* Typically, this is a native "File" class.
*
* We provide the {@link toFile} utility to convert a variety of objects
* into the File class.
*
* For convenience, you can also pass a fetch Response, or in Node,
* the result of fs.createReadStream().
*/
export type Uploadable = File | Response | FsReadStream | BunFile;
/**
* Construct a `File` instance. This is used to ensure a helpful error is thrown
* for environments that don't define a global `File` yet.
*/
export function makeFile(
fileBits: BlobPart[],
fileName: string | undefined,
options?: FilePropertyBag,
): File {
checkFileSupport();
return new File(fileBits as any, fileName ?? 'unknown_file', options);
}
export function getName(value: any): string | undefined {
return (
(
(typeof value === 'object' &&
value !== null &&
(('name' in value && value.name && String(value.name)) ||
('url' in value && value.url && String(value.url)) ||
('filename' in value && value.filename && String(value.filename)) ||
('path' in value && value.path && String(value.path)))) ||
''
)
.split(/[\\/]/)
.pop() || undefined
);
}
export const isAsyncIterable = (value: any): value is AsyncIterable<any> =>
value != null && typeof value === 'object' && typeof value[Symbol.asyncIterator] === 'function';
/**
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
* Otherwise returns the request as is.
*/
export const maybeMultipartFormRequestOptions = async (
opts: RequestOptions,
fetch: OpenAI | Fetch,
): Promise<RequestOptions> => {
if (!hasUploadableValue(opts.body)) return opts;
return { ...opts, body: await createForm(opts.body, fetch) };
};
type MultipartFormRequestOptions = Omit<RequestOptions, 'body'> & { body: unknown };
export const multipartFormRequestOptions = async (
opts: MultipartFormRequestOptions,
fetch: OpenAI | Fetch,
): Promise<RequestOptions> => {
return { ...opts, body: await createForm(opts.body, fetch) };
};
const supportsFormDataMap = /* @__PURE__ */ new WeakMap<Fetch, Promise<boolean>>();
/**
* node-fetch doesn't support the global FormData object in recent node versions. Instead of sending
* properly-encoded form data, it just stringifies the object, resulting in a request body of "[object FormData]".
* This function detects if the fetch function provided supports the global FormData object to avoid
* confusing error messages later on.
*/
function supportsFormData(fetchObject: OpenAI | Fetch): Promise<boolean> {
const fetch: Fetch = typeof fetchObject === 'function' ? fetchObject : (fetchObject as any).fetch;
const cached = supportsFormDataMap.get(fetch);
if (cached) return cached;
const promise = (async () => {
try {
const FetchResponse = (
'Response' in fetch ?
fetch.Response
: (await fetch('data:,')).constructor) as typeof Response;
const data = new FormData();
if (data.toString() === (await new FetchResponse(data).text())) {
return false;
}
return true;
} catch {
// avoid false negatives
return true;
}
})();
supportsFormDataMap.set(fetch, promise);
return promise;
}
export const createForm = async <T = Record<string, unknown>>(
body: T | undefined,
fetch: OpenAI | Fetch,
): Promise<FormData> => {
if (!(await supportsFormData(fetch))) {
throw new TypeError(
'The provided fetch function does not support file uploads with the current global FormData class.',
);
}
const form = new FormData();
await Promise.all(Object.entries(body || {}).map(([key, value]) => addFormValue(form, key, value)));
return form;
};
// We check for Blob not File because Bun.File doesn't inherit from File,
// but they both inherit from Blob and have a `name` property at runtime.
const isNamedBlob = (value: unknown) => value instanceof Blob && 'name' in value;
const isUploadable = (value: unknown) =>
typeof value === 'object' &&
value !== null &&
(value instanceof Response || isAsyncIterable(value) || isNamedBlob(value));
const hasUploadableValue = (value: unknown): boolean => {
if (isUploadable(value)) return true;
if (Array.isArray(value)) return value.some(hasUploadableValue);
if (value && typeof value === 'object') {
for (const k in value) {
if (hasUploadableValue((value as any)[k])) return true;
}
}
return false;
};
const addFormValue = async (form: FormData, key: string, value: unknown): Promise<void> => {
if (value === undefined) return;
if (value == null) {
throw new TypeError(
`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`,
);
}
// TODO: make nested formats configurable
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
form.append(key, String(value));
} else if (value instanceof Response) {
form.append(key, makeFile([await value.blob()], getName(value)));
} else if (isAsyncIterable(value)) {
form.append(key, makeFile([await new Response(ReadableStreamFrom(value)).blob()], getName(value)));
} else if (isNamedBlob(value)) {
form.append(key, value, getName(value));
} else if (Array.isArray(value)) {
await Promise.all(value.map((entry) => addFormValue(form, key + '[]', entry)));
} else if (typeof value === 'object') {
await Promise.all(
Object.entries(value).map(([name, prop]) => addFormValue(form, `${key}[${name}]`, prop)),
);
} else {
throw new TypeError(
`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${value} instead`,
);
}
};

8
node_modules/openai/src/internal/utils.ts generated vendored Normal file
View File

@@ -0,0 +1,8 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export * from './utils/values';
export * from './utils/base64';
export * from './utils/env';
export * from './utils/log';
export * from './utils/uuid';
export * from './utils/sleep';

64
node_modules/openai/src/internal/utils/base64.ts generated vendored Normal file
View File

@@ -0,0 +1,64 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { OpenAIError } from '../../core/error';
import { encodeUTF8 } from './bytes';
export const toBase64 = (data: string | Uint8Array | null | undefined): string => {
if (!data) return '';
if (typeof (globalThis as any).Buffer !== 'undefined') {
return (globalThis as any).Buffer.from(data).toString('base64');
}
if (typeof data === 'string') {
data = encodeUTF8(data);
}
if (typeof btoa !== 'undefined') {
return btoa(String.fromCharCode.apply(null, data as any));
}
throw new OpenAIError('Cannot generate base64 string; Expected `Buffer` or `btoa` to be defined');
};
export const fromBase64 = (str: string): Uint8Array => {
if (typeof (globalThis as any).Buffer !== 'undefined') {
const buf = (globalThis as any).Buffer.from(str, 'base64');
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
if (typeof atob !== 'undefined') {
const bstr = atob(str);
const buf = new Uint8Array(bstr.length);
for (let i = 0; i < bstr.length; i++) {
buf[i] = bstr.charCodeAt(i);
}
return buf;
}
throw new OpenAIError('Cannot decode base64 string; Expected `Buffer` or `atob` to be defined');
};
/**
* Converts a Base64 encoded string to a Float32Array.
* @param base64Str - The Base64 encoded string.
* @returns An Array of numbers interpreted as Float32 values.
*/
export const toFloat32Array = (base64Str: string): Array<number> => {
if (typeof Buffer !== 'undefined') {
// for Node.js environment
const buf = Buffer.from(base64Str, 'base64');
return Array.from(
new Float32Array(buf.buffer, buf.byteOffset, buf.length / Float32Array.BYTES_PER_ELEMENT),
);
} else {
// for legacy web platform APIs
const binaryStr = atob(base64Str);
const len = binaryStr.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryStr.charCodeAt(i);
}
return Array.from(new Float32Array(bytes.buffer));
}
};

32
node_modules/openai/src/internal/utils/bytes.ts generated vendored Normal file
View File

@@ -0,0 +1,32 @@
export function concatBytes(buffers: Uint8Array[]): Uint8Array {
let length = 0;
for (const buffer of buffers) {
length += buffer.length;
}
const output = new Uint8Array(length);
let index = 0;
for (const buffer of buffers) {
output.set(buffer, index);
index += buffer.length;
}
return output;
}
let encodeUTF8_: (str: string) => Uint8Array;
export function encodeUTF8(str: string) {
let encoder;
return (
encodeUTF8_ ??
((encoder = new (globalThis as any).TextEncoder()), (encodeUTF8_ = encoder.encode.bind(encoder)))
)(str);
}
let decodeUTF8_: (bytes: Uint8Array) => string;
export function decodeUTF8(bytes: Uint8Array) {
let decoder;
return (
decodeUTF8_ ??
((decoder = new (globalThis as any).TextDecoder()), (decodeUTF8_ = decoder.decode.bind(decoder)))
)(bytes);
}

18
node_modules/openai/src/internal/utils/env.ts generated vendored Normal file
View File

@@ -0,0 +1,18 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
/**
* Read an environment variable.
*
* Trims beginning and trailing whitespace.
*
* Will return undefined if the environment variable doesn't exist or cannot be accessed.
*/
export const readEnv = (env: string): string | undefined => {
if (typeof (globalThis as any).process !== 'undefined') {
return (globalThis as any).process.env?.[env]?.trim() ?? undefined;
}
if (typeof (globalThis as any).Deno !== 'undefined') {
return (globalThis as any).Deno.env?.get?.(env)?.trim();
}
return undefined;
};

126
node_modules/openai/src/internal/utils/log.ts generated vendored Normal file
View File

@@ -0,0 +1,126 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { hasOwn } from './values';
import { type OpenAI } from '../../client';
import { RequestOptions } from '../request-options';
type LogFn = (message: string, ...rest: unknown[]) => void;
export type Logger = {
error: LogFn;
warn: LogFn;
info: LogFn;
debug: LogFn;
};
export type LogLevel = 'off' | 'error' | 'warn' | 'info' | 'debug';
const levelNumbers = {
off: 0,
error: 200,
warn: 300,
info: 400,
debug: 500,
};
export const parseLogLevel = (
maybeLevel: string | undefined,
sourceName: string,
client: OpenAI,
): LogLevel | undefined => {
if (!maybeLevel) {
return undefined;
}
if (hasOwn(levelNumbers, maybeLevel)) {
return maybeLevel;
}
loggerFor(client).warn(
`${sourceName} was set to ${JSON.stringify(maybeLevel)}, expected one of ${JSON.stringify(
Object.keys(levelNumbers),
)}`,
);
return undefined;
};
function noop() {}
function makeLogFn(fnLevel: keyof Logger, logger: Logger | undefined, logLevel: LogLevel) {
if (!logger || levelNumbers[fnLevel] > levelNumbers[logLevel]) {
return noop;
} else {
// Don't wrap logger functions, we want the stacktrace intact!
return logger[fnLevel].bind(logger);
}
}
const noopLogger = {
error: noop,
warn: noop,
info: noop,
debug: noop,
};
let cachedLoggers = /* @__PURE__ */ new WeakMap<Logger, [LogLevel, Logger]>();
export function loggerFor(client: OpenAI): Logger {
const logger = client.logger;
const logLevel = client.logLevel ?? 'off';
if (!logger) {
return noopLogger;
}
const cachedLogger = cachedLoggers.get(logger);
if (cachedLogger && cachedLogger[0] === logLevel) {
return cachedLogger[1];
}
const levelLogger = {
error: makeLogFn('error', logger, logLevel),
warn: makeLogFn('warn', logger, logLevel),
info: makeLogFn('info', logger, logLevel),
debug: makeLogFn('debug', logger, logLevel),
};
cachedLoggers.set(logger, [logLevel, levelLogger]);
return levelLogger;
}
export const formatRequestDetails = (details: {
options?: RequestOptions | undefined;
headers?: Headers | Record<string, string> | undefined;
retryOfRequestLogID?: string | undefined;
retryOf?: string | undefined;
url?: string | undefined;
status?: number | undefined;
method?: string | undefined;
durationMs?: number | undefined;
message?: unknown;
body?: unknown;
}) => {
if (details.options) {
details.options = { ...details.options };
delete details.options['headers']; // redundant + leaks internals
}
if (details.headers) {
details.headers = Object.fromEntries(
(details.headers instanceof Headers ? [...details.headers] : Object.entries(details.headers)).map(
([name, value]) => [
name,
(
name.toLowerCase() === 'authorization' ||
name.toLowerCase() === 'cookie' ||
name.toLowerCase() === 'set-cookie'
) ?
'***'
: value,
],
),
);
}
if ('retryOfRequestLogID' in details) {
if (details.retryOfRequestLogID) {
details.retryOf = details.retryOfRequestLogID;
}
delete details.retryOfRequestLogID;
}
return details;
};

88
node_modules/openai/src/internal/utils/path.ts generated vendored Normal file
View File

@@ -0,0 +1,88 @@
import { OpenAIError } from '../../core/error';
/**
* Percent-encode everything that isn't safe to have in a path without encoding safe chars.
*
* Taken from https://datatracker.ietf.org/doc/html/rfc3986#section-3.3:
* > unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
* > sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
* > pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
*/
export function encodeURIPath(str: string) {
return str.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g, encodeURIComponent);
}
const EMPTY = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.create(null));
export const createPathTagFunction = (pathEncoder = encodeURIPath) =>
function path(statics: readonly string[], ...params: readonly unknown[]): string {
// If there are no params, no processing is needed.
if (statics.length === 1) return statics[0]!;
let postPath = false;
const invalidSegments = [];
const path = statics.reduce((previousValue, currentValue, index) => {
if (/[?#]/.test(currentValue)) {
postPath = true;
}
const value = params[index];
let encoded = (postPath ? encodeURIComponent : pathEncoder)('' + value);
if (
index !== params.length &&
(value == null ||
(typeof value === 'object' &&
// handle values from other realms
value.toString ===
Object.getPrototypeOf(Object.getPrototypeOf((value as any).hasOwnProperty ?? EMPTY) ?? EMPTY)
?.toString))
) {
encoded = value + '';
invalidSegments.push({
start: previousValue.length + currentValue.length,
length: encoded.length,
error: `Value of type ${Object.prototype.toString
.call(value)
.slice(8, -1)} is not a valid path parameter`,
});
}
return previousValue + currentValue + (index === params.length ? '' : encoded);
}, '');
const pathOnly = path.split(/[?#]/, 1)[0]!;
const invalidSegmentPattern = /(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi;
let match;
// Find all invalid segments
while ((match = invalidSegmentPattern.exec(pathOnly)) !== null) {
invalidSegments.push({
start: match.index,
length: match[0].length,
error: `Value "${match[0]}" can\'t be safely passed as a path parameter`,
});
}
invalidSegments.sort((a, b) => a.start - b.start);
if (invalidSegments.length > 0) {
let lastEnd = 0;
const underline = invalidSegments.reduce((acc, segment) => {
const spaces = ' '.repeat(segment.start - lastEnd);
const arrows = '^'.repeat(segment.length);
lastEnd = segment.start + segment.length;
return acc + spaces + arrows;
}, '');
throw new OpenAIError(
`Path parameters result in path with invalid segments:\n${invalidSegments
.map((e) => e.error)
.join('\n')}\n${path}\n${underline}`,
);
}
return path;
};
/**
* URI-encodes path params and ensures no unsafe /./ or /../ path segments are introduced.
*/
export const path = /* @__PURE__ */ createPathTagFunction(encodeURIPath);

3
node_modules/openai/src/internal/utils/sleep.ts generated vendored Normal file
View File

@@ -0,0 +1,3 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
export const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));

17
node_modules/openai/src/internal/utils/uuid.ts generated vendored Normal file
View File

@@ -0,0 +1,17 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
/**
* https://stackoverflow.com/a/2117523
*/
export let uuid4 = function () {
const { crypto } = globalThis as any;
if (crypto?.randomUUID) {
uuid4 = crypto.randomUUID.bind(crypto);
return crypto.randomUUID();
}
const u8 = new Uint8Array(1);
const randomByte = crypto ? () => crypto.getRandomValues(u8)[0]! : () => (Math.random() * 0xff) & 0xff;
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) =>
(+c ^ (randomByte() & (15 >> (+c / 4)))).toString(16),
);
};

105
node_modules/openai/src/internal/utils/values.ts generated vendored Normal file
View File

@@ -0,0 +1,105 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
import { OpenAIError } from '../../core/error';
// https://url.spec.whatwg.org/#url-scheme-string
const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
export const isAbsoluteURL = (url: string): boolean => {
return startsWithSchemeRegexp.test(url);
};
export let isArray = (val: unknown): val is unknown[] => ((isArray = Array.isArray), isArray(val));
export let isReadonlyArray = isArray as (val: unknown) => val is readonly unknown[];
/** Returns an object if the given value isn't an object, otherwise returns as-is */
export function maybeObj(x: unknown): object {
if (typeof x !== 'object') {
return {};
}
return x ?? {};
}
// https://stackoverflow.com/a/34491287
export function isEmptyObj(obj: Object | null | undefined): boolean {
if (!obj) return true;
for (const _k in obj) return false;
return true;
}
// https://eslint.org/docs/latest/rules/no-prototype-builtins
export function hasOwn<T extends object = object>(obj: T, key: PropertyKey): key is keyof T {
return Object.prototype.hasOwnProperty.call(obj, key);
}
export function isObj(obj: unknown): obj is Record<string, unknown> {
return obj != null && typeof obj === 'object' && !Array.isArray(obj);
}
export const ensurePresent = <T>(value: T | null | undefined): T => {
if (value == null) {
throw new OpenAIError(`Expected a value to be given but received ${value} instead.`);
}
return value;
};
export const validatePositiveInteger = (name: string, n: unknown): number => {
if (typeof n !== 'number' || !Number.isInteger(n)) {
throw new OpenAIError(`${name} must be an integer`);
}
if (n < 0) {
throw new OpenAIError(`${name} must be a positive integer`);
}
return n;
};
export const coerceInteger = (value: unknown): number => {
if (typeof value === 'number') return Math.round(value);
if (typeof value === 'string') return parseInt(value, 10);
throw new OpenAIError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
};
export const coerceFloat = (value: unknown): number => {
if (typeof value === 'number') return value;
if (typeof value === 'string') return parseFloat(value);
throw new OpenAIError(`Could not coerce ${value} (type: ${typeof value}) into a number`);
};
export const coerceBoolean = (value: unknown): boolean => {
if (typeof value === 'boolean') return value;
if (typeof value === 'string') return value === 'true';
return Boolean(value);
};
export const maybeCoerceInteger = (value: unknown): number | undefined => {
if (value === undefined) {
return undefined;
}
return coerceInteger(value);
};
export const maybeCoerceFloat = (value: unknown): number | undefined => {
if (value === undefined) {
return undefined;
}
return coerceFloat(value);
};
export const maybeCoerceBoolean = (value: unknown): boolean | undefined => {
if (value === undefined) {
return undefined;
}
return coerceBoolean(value);
};
export const safeJSON = (text: string) => {
try {
return JSON.parse(text);
} catch (err) {
return undefined;
}
};