Quelle openai-dev.mjs
Sprache: unbekannt
|
|
Untersuchungsergebnis.mjs Download desUnknown {[0] [0] [0]}
// dist/internal/tslib.mjs
function __classPrivateFieldSet(receiver, state, value, kind, f) {
if (kind === "m")
throw new TypeError("Private method is not writable");
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot write private member to an object whose class did not declare it") ;
return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
}
function __classPrivateFieldGet(receiver, state, kind, f) {
if (kind === "a" && !f)
throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
}
// dist/internal/utils/uuid.mjs
var uuid4 = function() {
const { crypto } = globalThis;
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() * 255 & 255;
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c) => (+c ^ randomByte() & 15 >> +c / 4).toString(16));
};
// dist/internal/errors.mjs
function isAbortError(err) {
return typeof err === "object" && err !== null && // Spec-compliant fetch implementations
("name" in err && err.name === "AbortError" || // Expo fetch
"message" in err && String(err.message).includes("FetchRequestCanceledException"));
}
var castToError = (err) => {
if (err instanceof Error)
return err;
if (typeof err === "object" && err !== null) {
try {
if (Object.prototype.toString.call(err) === "[object Error]") {
const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
if (err.stack)
error.stack = err.stack;
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);
};
// dist/core/error.mjs
var OpenAIError = class extends Error {
};
var APIError = class _APIError extends OpenAIError {
constructor(status, error, message, headers) {
super(`${_APIError.makeMessage(status, error, message)}`);
this.status = status;
this.headers = headers;
this.requestID = headers?.get("x-request-id");
this.error = error;
const data = error;
this.code = data?.["code"];
this.param = data?.["param"];
this.type = data?.["type"];
}
static makeMessage(status, error, message) {
const msg = error?.message ? typeof error.message === "string" ? error.message : JSON.stringify(error.message) : error ? JSON.stringify(error) : message;
if (status && msg) {
return `${status} ${msg}`;
}
if (status) {
return `${status} status code (no body)`;
}
if (msg) {
return msg;
}
return "(no status code or body)";
}
static generate(status, errorResponse, message, headers) {
if (!status || !headers) {
return new APIConnectionError({ message, cause: castToError(errorResponse) });
}
const error = errorResponse?.["error"];
if (status === 400) {
return new BadRequestError(status, error, message, headers);
}
if (status === 401) {
return new AuthenticationError(status, error, message, headers);
}
if (status === 403) {
return new PermissionDeniedError(status, error, message, headers);
}
if (status === 404) {
return new NotFoundError(status, error, message, headers);
}
if (status === 409) {
return new ConflictError(status, error, message, headers);
}
if (status === 422) {
return new UnprocessableEntityError(status, error, message, headers);
}
if (status === 429) {
return new RateLimitError(status, error, message, headers);
}
if (status >= 500) {
return new InternalServerError(status, error, message, headers);
}
return new _APIError(status, error, message, headers);
}
};
var APIUserAbortError = class extends APIError {
constructor({ message } = {}) {
super(void 0, void 0, message || "Request was aborted.", void 0);
}
};
var APIConnectionError = class extends APIError {
constructor({ message, cause }) {
super(void 0, void 0, message || "Connection error.", void 0);
if (cause)
this.cause = cause;
}
};
var APIConnectionTimeoutError = class extends APIConnectionError {
constructor({ message } = {}) {
super({ message: message ?? "Request timed out." });
}
};
var BadRequestError = class extends APIError {
};
var AuthenticationError = class extends APIError {
};
var PermissionDeniedError = class extends APIError {
};
var NotFoundError = class extends APIError {
};
var ConflictError = class extends APIError {
};
var UnprocessableEntityError = class extends APIError {
};
var RateLimitError = class extends APIError {
};
var InternalServerError = class extends APIError {
};
var LengthFinishReasonError = class extends OpenAIError {
constructor() {
super(`Could not parse response content as the length limit was reached`);
}
};
var ContentFilterFinishReasonError = class extends OpenAIError {
constructor() {
super(`Could not parse response content as the request was rejected by the content filter`);
}
};
// dist/internal/utils/values.mjs
var startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
var isAbsoluteURL = (url) => {
return startsWithSchemeRegexp.test(url);
};
var isArray = (val) => (isArray = Array.isArray, isArray(val));
var isReadonlyArray = isArray;
function maybeObj(x) {
if (typeof x !== "object") {
return {};
}
return x ?? {};
}
function isEmptyObj(obj) {
if (!obj)
return true;
for (const _k in obj)
return false;
return true;
}
function hasOwn(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}
function isObj(obj) {
return obj != null && typeof obj === "object" && !Array.isArray(obj);
}
var validatePositiveInteger = (name, n) => {
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;
};
var safeJSON = (text) => {
try {
return JSON.parse(text);
} catch (err) {
return void 0;
}
};
// dist/internal/utils/sleep.mjs
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// dist/version.mjs
var VERSION = "5.6.0";
// dist/internal/detect-platform.mjs
var isRunningInBrowser = () => {
return (
// @ts-ignore
typeof window !== "undefined" && // @ts-ignore
typeof window.document !== "undefined" && // @ts-ignore
typeof navigator !== "undefined"
);
};
function getDetectedPlatform() {
if (typeof Deno !== "undefined" && Deno.build != null) {
return "deno";
}
if (typeof EdgeRuntime !== "undefined") {
return "edge";
}
if (Object.prototype.toString.call(typeof globalThis.process !== "undefined" ? globalThis.process : 0) === "[object process]") {
return "node";
}
return "unknown";
}
var getPlatformProperties = () => {
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.process.version
};
}
if (detectedPlatform === "node") {
return {
"X-Stainless-Lang": "js",
"X-Stainless-Package-Version": VERSION,
"X-Stainless-OS": normalizePlatform(globalThis.process.platform ?? "unknown"),
"X-Stainless-Arch": normalizeArch(globalThis.process.arch ?? "unknown"),
"X-Stainless-Runtime": "node",
"X-Stainless-Runtime-Version": globalThis.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
};
}
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"
};
};
function getBrowserInfo() {
if (typeof navigator === "undefined" || !navigator) {
return null;
}
const browserPatterns = [
{ key: "edge", pattern: /Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: "ie", pattern: /MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: "ie", pattern: /Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: "chrome", pattern: /Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: "firefox", pattern: /Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/ },
{ key: "safari", pattern: /(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/ }
];
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;
}
var normalizeArch = (arch) => {
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";
};
var normalizePlatform = (platform) => {
platform = platform.toLowerCase();
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";
};
var _platformHeaders;
var getPlatformHeaders = () => {
return _platformHeaders ?? (_platformHeaders = getPlatformProperties());
};
// dist/internal/shims.mjs
function getDefaultFetch() {
if (typeof fetch !== "undefined") {
return fetch;
}
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`");
}
function makeReadableStream(...args) {
const ReadableStream = globalThis.ReadableStream;
if (typeof ReadableStream === "undefined") {
throw new Error("`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`");
}
return new ReadableStream(...args);
}
function ReadableStreamFrom(iterable) {
let iter = Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
return makeReadableStream({
start() {
},
async pull(controller) {
const { done, value } = await iter.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
},
async cancel() {
await iter.return?.();
}
});
}
function ReadableStreamToAsyncIterable(stream) {
if (stream[Symbol.asyncIterator])
return stream;
const reader = stream.getReader();
return {
async next() {
try {
const result = await reader.read();
if (result?.done)
reader.releaseLock();
return result;
} catch (e) {
reader.releaseLock();
throw e;
}
},
async return() {
const cancelPromise = reader.cancel();
reader.releaseLock();
await cancelPromise;
return { done: true, value: void 0 };
},
[Symbol.asyncIterator]() {
return this;
}
};
}
async function CancelReadableStream(stream) {
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;
}
// dist/internal/request-options.mjs
var FallbackEncoder = ({ headers, body }) => {
return {
bodyHeaders: {
"content-type": "application/json"
},
body: JSON.stringify(body)
};
};
// dist/internal/qs/formats.mjs
var default_format = "RFC3986";
var default_formatter = (v) => String(v);
var formatters = {
RFC1738: (v) => String(v).replace(/%20/g, "+"),
RFC3986: default_formatter
};
var RFC1738 = "RFC1738";
// dist/internal/qs/utils.mjs
var has = (obj, key) => (has = Object.hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty), has(obj, key));
var hex_table = /* @__PURE__ */ (() => {
const array = [];
for (let i = 0; i < 256; ++i) {
array.push("%" + ((i < 16 ? "0" : "") + i.toString(16)).toUpperCase());
}
return array;
})();
var limit = 1024;
var encode = (str2, _defaultEncoder, charset, _kind, format) => {
if (str2.length === 0) {
return str2;
}
let string = str2;
if (typeof str2 === "symbol") {
string = Symbol.prototype.toString.call(str2);
} else if (typeof str2 !== "string") {
string = String(str2);
}
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 === 45 || // -
c === 46 || // .
c === 95 || // _
c === 126 || // ~
c >= 48 && c <= 57 || // 0-9
c >= 65 && c <= 90 || // a-z
c >= 97 && c <= 122 || // A-Z
format === RFC1738 && (c === 40 || c === 41)) {
arr[arr.length] = segment.charAt(i);
continue;
}
if (c < 128) {
arr[arr.length] = hex_table[c];
continue;
}
if (c < 2048) {
arr[arr.length] = hex_table[192 | c >> 6] + hex_table[128 | c & 63];
continue;
}
if (c < 55296 || c >= 57344) {
arr[arr.length] = hex_table[224 | c >> 12] + hex_table[128 | c >> 6 & 63] + hex_table[128 | c & 63];
continue;
}
i += 1;
c = 65536 + ((c & 1023) << 10 | segment.charCodeAt(i) & 1023);
arr[arr.length] = hex_table[240 | c >> 18] + hex_table[128 | c >> 12 & 63] + hex_table[128 | c >> 6 & 63] + hex_table[128 | c & 63];
}
out += arr.join("");
}
return out;
};
function is_buffer(obj) {
if (!obj || typeof obj !== "object") {
return false;
}
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
}
function maybe_map(val, fn) {
if (isArray(val)) {
const mapped = [];
for (let i = 0; i < val.length; i += 1) {
mapped.push(fn(val[i]));
}
return mapped;
}
return fn(val);
}
// dist/internal/qs/stringify.mjs
var array_prefix_generators = {
brackets(prefix) {
return String(prefix) + "[]";
},
comma: "comma",
indices(prefix, key) {
return String(prefix) + "[" + key + "]";
},
repeat(prefix) {
return String(prefix);
}
};
var push_to_array = function(arr, value_or_array) {
Array.prototype.push.apply(arr, isArray(value_or_array) ? value_or_array : [value_or_array]);
};
var toISOString;
var 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 ?? (toISOString = Function.prototype.call.bind(Date.prototype.toISOString)))(date);
},
skipNulls: false,
strictNullHandling: false
};
function is_non_nullish_primitive(v) {
return typeof v === "string" || typeof v === "number" || typeof v === "boolean" || typeof v === "symbol" || typeof v === "bigint";
}
var sentinel = {};
function inner_stringify(object, prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, encoder, filter, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, sideChannel) {
let obj = object;
let tmp_sc = sideChannel;
let step = 0;
let find_flag = false;
while ((tmp_sc = tmp_sc.get(sentinel)) !== void 0 && !find_flag) {
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;
}
}
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 : 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 = [];
if (typeof obj === "undefined") {
return values;
}
let obj_keys;
if (generateArrayPrefix === "comma" && isArray(obj)) {
if (encodeValuesOnly && encoder) {
obj = maybe_map(obj, encoder);
}
obj_keys = [{ value: obj.length > 0 ? obj.join(",") || null : void 0 }];
} 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]
);
if (skipNulls && value === null) {
continue;
}
const encoded_key = allowDots && encodeDotInKeys ? key.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 = /* @__PURE__ */ 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 = defaults) {
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;
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,
allowEmptyArrays: typeof opts.allowEmptyArrays === "boolean" ? !!opts.allowEmptyArrays : defaults.allowEmptyArrays,
arrayFormat,
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,
format,
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
};
}
function stringify(object, opts = {}) {
let obj = object;
const options = normalize_stringify_options(opts);
let obj_keys;
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 = [];
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 = /* @__PURE__ */ 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") {
prefix += "utf8=%26%2310003%3B&";
} else {
prefix += "utf8=%E2%9C%93&";
}
}
return joined.length > 0 ? prefix + joined : "";
}
// dist/internal/utils/bytes.mjs
function concatBytes(buffers) {
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;
}
var encodeUTF8_;
function encodeUTF8(str2) {
let encoder;
return (encodeUTF8_ ?? (encoder = new globalThis.TextEncoder(), encodeUTF8_ = encoder.encode.bind(encoder)))(str2);
}
var decodeUTF8_;
function decodeUTF8(bytes) {
let decoder;
return (decodeUTF8_ ?? (decoder = new globalThis.TextDecoder(), decodeUTF8_ = decoder.decode.bind(decoder)))(bytes);
}
// dist/internal/decoders/line.mjs
var _LineDecoder_buffer;
var _LineDecoder_carriageReturnIndex;
var LineDecoder = class {
constructor() {
_LineDecoder_buffer.set(this, void 0);
_LineDecoder_carriageReturnIndex.set(this, void 0);
__classPrivateFieldSet(this, _LineDecoder_buffer, new Uint8Array(), "f");
__classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, "f");
}
decode(chunk) {
if (chunk == null) {
return [];
}
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? encodeUTF8(chunk) : chunk;
__classPrivateFieldSet(this, _LineDecoder_buffer, concatBytes([__classPrivateFieldGet(this, _LineDecoder_buffer, "f"), binaryChunk]), "f");
const lines = [];
let patternIndex;
while ((patternIndex = findNewlineIndex(__classPrivateFieldGet(this, _LineDecoder_buffer, "f"), __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f"))) != null) {
if (patternIndex.carriage && __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f") == null) {
__classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, patternIndex.index, "f");
continue;
}
if (__classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f") != null && (patternIndex.index !== __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f") + 1 || patternIndex.carriage)) {
lines.push(decodeUTF8(__classPrivateFieldGet(this, _LineDecoder_buffer, "f").subarray(0, __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f") - 1)));
__classPrivateFieldSet(this, _LineDecoder_buffer, __classPrivateFieldGet(this, _LineDecoder_buffer, "f").subarray(__classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f")), "f");
__classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, "f");
continue;
}
const endIndex = __classPrivateFieldGet(this, _LineDecoder_carriageReturnIndex, "f") !== null ? patternIndex.preceding - 1 : patternIndex.preceding;
const line = decodeUTF8(__classPrivateFieldGet(this, _LineDecoder_buffer, "f").subarray(0, endIndex));
lines.push(line);
__classPrivateFieldSet(this, _LineDecoder_buffer, __classPrivateFieldGet(this, _LineDecoder_buffer, "f").subarray(patternIndex.index), "f");
__classPrivateFieldSet(this, _LineDecoder_carriageReturnIndex, null, "f");
}
return lines;
}
flush() {
if (!__classPrivateFieldGet(this, _LineDecoder_buffer, "f").length) {
return [];
}
return this.decode("\n");
}
};
_LineDecoder_buffer = /* @__PURE__ */ new WeakMap(), _LineDecoder_carriageReturnIndex = /* @__PURE__ */ new WeakMap();
LineDecoder.NEWLINE_CHARS = /* @__PURE__ */ new Set(["\n", "\r"]);
LineDecoder.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
function findNewlineIndex(buffer, startIndex) {
const newline = 10;
const carriage = 13;
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;
}
function findDoubleNewlineIndex(buffer) {
const newline = 10;
const carriage = 13;
for (let i = 0; i < buffer.length - 1; i++) {
if (buffer[i] === newline && buffer[i + 1] === newline) {
return i + 2;
}
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
return i + 2;
}
if (buffer[i] === carriage && buffer[i + 1] === newline && i + 3 < buffer.length && buffer[i + 2] === carriage && buffer[i + 3] === newline) {
return i + 4;
}
}
return -1;
}
// dist/core/streaming.mjs
var Stream = class _Stream {
constructor(iterator, controller) {
this.iterator = iterator;
this.controller = controller;
}
static fromSSEResponse(response, controller) {
let consumed = false;
async function* iterator() {
if (consumed) {
throw new OpenAIError("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");
}
consumed = true;
let done = false;
try {
for await (const sse of _iterSSEMessages(response, controller)) {
if (done)
continue;
if (sse.data.startsWith("[DONE]")) {
done = true;
continue;
}
if (sse.event === null || sse.event.startsWith("response.") || sse.event.startsWith("transcript.")) {
let data;
try {
data = JSON.parse(sse.data);
} catch (e) {
console.error(`Could not parse message into JSON:`, sse.data);
console.error(`From chunk:`, sse.raw);
throw e;
}
if (data && data.error) {
throw new APIError(void 0, data.error, void 0, response.headers);
}
yield data;
} else {
let data;
try {
data = JSON.parse(sse.data);
} catch (e) {
console.error(`Could not parse message into JSON:`, sse.data);
console.error(`From chunk:`, sse.raw);
throw e;
}
if (sse.event == "error") {
throw new APIError(void 0, data.error, data.message, void 0);
}
yield { event: sse.event, data };
}
}
done = true;
} catch (e) {
if (isAbortError(e))
return;
throw e;
} finally {
if (!done)
controller.abort();
}
}
return new _Stream(iterator, controller);
}
/**
* Generates a Stream from a newline-separated ReadableStream
* where each item is a JSON value.
*/
static fromReadableStream(readableStream, controller) {
let consumed = false;
async function* iterLines() {
const lineDecoder = new LineDecoder();
const iter = ReadableStreamToAsyncIterable(readableStream);
for await (const chunk of iter) {
for (const line of lineDecoder.decode(chunk)) {
yield line;
}
}
for (const line of lineDecoder.flush()) {
yield line;
}
}
async function* iterator() {
if (consumed) {
throw new OpenAIError("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");
}
consumed = true;
let done = false;
try {
for await (const line of iterLines()) {
if (done)
continue;
if (line)
yield JSON.parse(line);
}
done = true;
} catch (e) {
if (isAbortError(e))
return;
throw e;
} finally {
if (!done)
controller.abort();
}
}
return new _Stream(iterator, controller);
}
[Symbol.asyncIterator]() {
return this.iterator();
}
/**
* Splits the stream into two streams which can be
* independently read from at different speeds.
*/
tee() {
const left = [];
const right = [];
const iterator = this.iterator();
const teeIterator = (queue) => {
return {
next: () => {
if (queue.length === 0) {
const result = iterator.next();
left.push(result);
right.push(result);
}
return queue.shift();
}
};
};
return [
new _Stream(() => teeIterator(left), this.controller),
new _Stream(() => teeIterator(right), this.controller)
];
}
/**
* Converts this stream to a newline-separated ReadableStream of
* JSON stringified values in the stream
* which can be turned back into a Stream with `Stream.fromReadableStream()`.
*/
toReadableStream() {
const self = this;
let iter;
return makeReadableStream({
async start() {
iter = self[Symbol.asyncIterator]();
},
async pull(ctrl) {
try {
const { value, done } = await iter.next();
if (done)
return ctrl.close();
const bytes = encodeUTF8(JSON.stringify(value) + "\n");
ctrl.enqueue(bytes);
} catch (err) {
ctrl.error(err);
}
},
async cancel() {
await iter.return?.();
}
});
}
};
async function* _iterSSEMessages(response, controller) {
if (!response.body) {
controller.abort();
if (typeof globalThis.navigator !== "undefined" && globalThis.navigator.product === "ReactNative") {
throw new OpenAIError(`The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api`);
}
throw new OpenAIError(`Attempted to iterate over a response with no body`);
}
const sseDecoder = new SSEDecoder();
const lineDecoder = new LineDecoder();
const iter = ReadableStreamToAsyncIterable(response.body);
for await (const sseChunk of iterSSEChunks(iter)) {
for (const line of lineDecoder.decode(sseChunk)) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
for (const line of lineDecoder.flush()) {
const sse = sseDecoder.decode(line);
if (sse)
yield sse;
}
}
async function* iterSSEChunks(iterator) {
let data = new Uint8Array();
for await (const chunk of iterator) {
if (chunk == null) {
continue;
}
const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? encodeUTF8(chunk) : chunk;
let newData = new Uint8Array(data.length + binaryChunk.length);
newData.set(data);
newData.set(binaryChunk, data.length);
data = newData;
let patternIndex;
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
}
}
if (data.length > 0) {
yield data;
}
}
var SSEDecoder = class {
constructor() {
this.event = null;
this.data = [];
this.chunks = [];
}
decode(line) {
if (line.endsWith("\r")) {
line = line.substring(0, line.length - 1);
}
if (!line) {
if (!this.event && !this.data.length)
return null;
const sse = {
event: this.event,
data: this.data.join("\n"),
raw: this.chunks
};
this.event = null;
this.data = [];
this.chunks = [];
return sse;
}
this.chunks.push(line);
if (line.startsWith(":")) {
return null;
}
let [fieldname, _, value] = partition(line, ":");
if (value.startsWith(" ")) {
value = value.substring(1);
}
if (fieldname === "event") {
this.event = value;
} else if (fieldname === "data") {
this.data.push(value);
}
return null;
}
};
function partition(str2, delimiter) {
const index = str2.indexOf(delimiter);
if (index !== -1) {
return [str2.substring(0, index), delimiter, str2.substring(index + delimiter.length)];
}
return [str2, "", ""];
}
// dist/internal/utils/log.mjs
var levelNumbers = {
off: 0,
error: 200,
warn: 300,
info: 400,
debug: 500
};
var parseLogLevel = (maybeLevel, sourceName, client) => {
if (!maybeLevel) {
return void 0;
}
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 void 0;
};
function noop() {
}
function makeLogFn(fnLevel, logger, logLevel) {
if (!logger || levelNumbers[fnLevel] > levelNumbers[logLevel]) {
return noop;
} else {
return logger[fnLevel].bind(logger);
}
}
var noopLogger = {
error: noop,
warn: noop,
info: noop,
debug: noop
};
var cachedLoggers = /* @__PURE__ */ new WeakMap();
function loggerFor(client) {
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;
}
var formatRequestDetails = (details) => {
if (details.options) {
details.options = { ...details.options };
delete details.options["headers"];
}
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;
};
// dist/internal/parse.mjs
async function defaultParseResponse(client, props) {
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);
if (props.options.__streamClass) {
return props.options.__streamClass.fromSSEResponse(response, props.controller);
}
return Stream.fromSSEResponse(response, props.controller);
}
if (response.status === 204) {
return null;
}
if (props.options.__binaryResponse) {
return response;
}
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, response);
}
const text = await response.text();
return text;
})();
loggerFor(client).debug(`[${requestLogID}] response parsed`, formatRequestDetails({
retryOfRequestLogID,
url: response.url,
status: response.status,
body,
durationMs: Date.now() - startTime
}));
return body;
}
function addRequestID(value, response) {
if (!value || typeof value !== "object" || Array.isArray(value)) {
return value;
}
return Object.defineProperty(value, "_request_id", {
value: response.headers.get("x-request-id"),
enumerable: false
});
}
// dist/core/api-promise.mjs
var _APIPromise_client;
var APIPromise = class _APIPromise extends Promise {
constructor(client, responsePromise, parseResponse2 = defaultParseResponse) {
super((resolve) => {
resolve(null);
});
this.responsePromise = responsePromise;
this.parseResponse = parseResponse2;
_APIPromise_client.set(this, void 0);
__classPrivateFieldSet(this, _APIPromise_client, client, "f");
}
_thenUnwrap(transform) {
return new _APIPromise(__classPrivateFieldGet(this, _APIPromise_client, "f"), this.responsePromise, async (client, props) => addRequestID(transform(await this.parseResponse(client, props), props), props.response));
}
/**
* Gets the raw `Response` instance instead of parsing the response
* data.
*
* If you want to parse the response body but still get the `Response`
* instance, you can use {@link withResponse()}.
*
* Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
asResponse() {
return this.responsePromise.then((p) => p.response);
}
/**
* Gets the parsed response data, the raw `Response` instance and the ID of the request,
* returned via the X-Request-ID header which is useful for debugging requests and reporting
* issues to OpenAI.
*
* If you just want to get the raw `Response` instance without parsing it,
* you can use {@link asResponse()}.
*
* Getting the wrong TypeScript type for `Response`?
* Try setting `"moduleResolution": "NodeNext"` or add `"lib": ["DOM"]`
* to your `tsconfig.json`.
*/
async withResponse() {
const [data, response] = await Promise.all([this.parse(), this.asResponse()]);
return { data, response, request_id: response.headers.get("x-request-id") };
}
parse() {
if (!this.parsedPromise) {
this.parsedPromise = this.responsePromise.then((data) => this.parseResponse(__classPrivateFieldGet(this, _APIPromise_client, "f"), data));
}
return this.parsedPromise;
}
then(onfulfilled, onrejected) {
return this.parse().then(onfulfilled, onrejected);
}
catch(onrejected) {
return this.parse().catch(onrejected);
}
finally(onfinally) {
return this.parse().finally(onfinally);
}
};
_APIPromise_client = /* @__PURE__ */ new WeakMap();
// dist/core/pagination.mjs
var _AbstractPage_client;
var AbstractPage = class {
constructor(client, response, body, options) {
_AbstractPage_client.set(this, void 0);
__classPrivateFieldSet(this, _AbstractPage_client, client, "f");
this.options = options;
this.response = response;
this.body = body;
}
hasNextPage() {
const items = this.getPaginatedItems();
if (!items.length)
return false;
return this.nextPageRequestOptions() != null;
}
async getNextPage() {
const nextOptions = this.nextPageRequestOptions();
if (!nextOptions) {
throw new OpenAIError("No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.");
}
return await __classPrivateFieldGet(this, _AbstractPage_client, "f").requestAPIList(this.constructor, nextOptions);
}
async *iterPages() {
let page = this;
yield page;
while (page.hasNextPage()) {
page = await page.getNextPage();
yield page;
}
}
async *[(_AbstractPage_client = /* @__PURE__ */ new WeakMap(), Symbol.asyncIterator)]() {
for await (const page of this.iterPages()) {
for (const item of page.getPaginatedItems()) {
yield item;
}
}
}
};
var PagePromise = class extends APIPromise {
constructor(client, request, Page2) {
super(client, request, async (client2, props) => new Page2(client2, props.response, await defaultParseResponse(client2, props), props.options));
}
/**
* Allow auto-paginating iteration on an unawaited list call, eg:
*
* for await (const item of client.items.list()) {
* console.log(item)
* }
*/
async *[Symbol.asyncIterator]() {
const page = await this;
for await (const item of page) {
yield item;
}
}
};
var Page = class extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.object = body.object;
}
getPaginatedItems() {
return this.data ?? [];
}
nextPageRequestOptions() {
return null;
}
};
var CursorPage = class extends AbstractPage {
constructor(client, response, body, options) {
super(client, response, body, options);
this.data = body.data || [];
this.has_more = body.has_more || false;
}
getPaginatedItems() {
return this.data ?? [];
}
hasNextPage() {
if (this.has_more === false) {
return false;
}
return super.hasNextPage();
}
nextPageRequestOptions() {
const data = this.getPaginatedItems();
const id = data[data.length - 1]?.id;
if (!id) {
return null;
}
return {
...this.options,
query: {
...maybeObj(this.options.query),
after: id
}
};
}
};
// dist/internal/uploads.mjs
var checkFileSupport = () => {
if (typeof File === "undefined") {
const { process: process2 } = globalThis;
const isOldNode = typeof process2?.versions?.node === "string" && parseInt(process2.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`." : ""));
}
};
function makeFile(fileBits, fileName, options) {
checkFileSupport();
return new File(fileBits, fileName ?? "unknown_file", options);
}
function getName(value) {
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() || void 0;
}
var isAsyncIterable = (value) => value != null && typeof value === "object" && typeof value[Symbol.asyncIterator] === "function";
var multipartFormRequestOptions = async (opts, fetch2) => {
return { ...opts, body: await createForm(opts.body, fetch2) };
};
var supportsFormDataMap = /* @__PURE__ */ new WeakMap();
function supportsFormData(fetchObject) {
const fetch2 = typeof fetchObject === "function" ? fetchObject : fetchObject.fetch;
const cached = supportsFormDataMap.get(fetch2);
if (cached)
return cached;
const promise = (async () => {
try {
const FetchResponse = "Response" in fetch2 ? fetch2.Response : (await fetch2("data:,")).constructor;
const data = new FormData();
if (data.toString() === await new FetchResponse(data).text()) {
return false;
}
return true;
} catch {
return true;
}
})();
supportsFormDataMap.set(fetch2, promise);
return promise;
}
var createForm = async (body, fetch2) => {
if (!await supportsFormData(fetch2)) {
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;
};
var isNamedBlob = (value) => value instanceof Blob && "name" in value;
var addFormValue = async (form, key, value) => {
if (value === void 0)
return;
if (value == null) {
throw new TypeError(`Received null for "${key}"; to pass null in FormData, you must use the string 'null'`);
}
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`);
}
};
// dist/internal/to-file.mjs
var isBlobLike = (value) => 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";
var isFileLike = (value) => value != null && typeof value === "object" && typeof value.name === "string" && typeof value.lastModified === "number" && isBlobLike(value);
var isResponseLike = (value) => value != null && typeof value === "object" && typeof value.url === "string" && typeof value.blob === "function";
async function toFile(value, name, options) {
checkFileSupport();
value = await value;
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 || (name = new URL(value.url).pathname.split(/[\\/]/).pop());
return makeFile(await getBytes(blob), name, options);
}
const parts = await getBytes(value);
name || (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) {
let parts = [];
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)) {
for await (const chunk of value) {
parts.push(...await getBytes(chunk));
}
} else {
const constructor = value?.constructor?.name;
throw new Error(`Unexpected data type: ${typeof value}${constructor ? `; constructor: ${constructor}` : ""}${propsForError(value)}`);
}
return parts;
}
function propsForError(value) {
if (typeof value !== "object" || value === null)
return "";
const props = Object.getOwnPropertyNames(value);
return `; props: [${props.map((p) => `"${p}"`).join(", ")}]`;
}
// dist/core/resource.mjs
var APIResource = class {
constructor(client) {
this._client = client;
}
};
// dist/internal/utils/path.mjs
function encodeURIPath(str2) {
return str2.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g, encodeURIComponent);
}
var createPathTagFunction = (pathEncoder = encodeURIPath) => function path2(statics, ...params) {
if (statics.length === 1)
return statics[0];
let postPath = false;
const path3 = statics.reduce((previousValue, currentValue, index) => {
if (/[?#]/.test(currentValue)) {
postPath = true;
}
return previousValue + currentValue + (index === params.length ? "" : (postPath ? encodeURIComponent : pathEncoder)(String(params[index])));
}, "");
const pathOnly = path3.split(/[?#]/, 1)[0];
const invalidSegments = [];
const invalidSegmentPattern = /(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi;
let match;
while ((match = invalidSegmentPattern.exec(pathOnly)) !== null) {
invalidSegments.push({
start: match.index,
length: match[0].length
});
}
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:
${path3}
${underline}`);
}
return path3;
};
var path = /* @__PURE__ */ createPathTagFunction(encodeURIPath);
// dist/resources/chat/completions/messages.mjs
var Messages = class extends APIResource {
/**
* Get the messages in a stored chat completion. Only Chat Completions that have
* been created with the `store` parameter set to `true` will be returned.
*
* @example
* ```ts
* // Automatically fetches more pages as needed.
* for await (const chatCompletionStoreMessage of client.chat.completions.messages.list(
* 'completion_id',
* )) {
* // ...
* }
* ```
*/
list(completionID, query = {}, options) {
return this._client.getAPIList(path`/chat/completions/${completionID}/messages`, CursorPage, { query, ...options });
}
};
// dist/lib/RunnableFunction.mjs
function isRunnableFunctionWithParse(fn) {
return typeof fn.parse === "function";
}
// dist/lib/chatCompletionUtils.mjs
var isAssistantMessage = (message) => {
return message?.role === "assistant";
};
var isToolMessage = (message) => {
return message?.role === "tool";
};
// dist/lib/EventStream.mjs
var _EventStream_instances;
var _EventStream_connectedPromise;
var _EventStream_resolveConnectedPromise;
var _EventStream_rejectConnectedPromise;
var _EventStream_endPromise;
var _EventStream_resolveEndPromise;
var _EventStream_rejectEndPromise;
var _EventStream_listeners;
var _EventStream_ended;
var _EventStream_errored;
var _EventStream_aborted;
var _EventStream_catchingPromiseCreated;
var _EventStream_handleError;
var EventStream = class {
constructor() {
_EventStream_instances.add(this);
this.controller = new AbortController();
_EventStream_connectedPromise.set(this, void 0);
_EventStream_resolveConnectedPromise.set(this, () => {
});
_EventStream_rejectConnectedPromise.set(this, () => {
});
_EventStream_endPromise.set(this, void 0);
_EventStream_resolveEndPromise.set(this, () => {
});
_EventStream_rejectEndPromise.set(this, () => {
});
_EventStream_listeners.set(this, {});
_EventStream_ended.set(this, false);
_EventStream_errored.set(this, false);
_EventStream_aborted.set(this, false);
_EventStream_catchingPromiseCreated.set(this, false);
__classPrivateFieldSet(this, _EventStream_connectedPromise, new Promise((resolve, reject) => {
__classPrivateFieldSet(this, _EventStream_resolveConnectedPromise, resolve, "f");
__classPrivateFieldSet(this, _EventStream_rejectConnectedPromise, reject, "f");
}), "f");
__classPrivateFieldSet(this, _EventStream_endPromise, new Promise((resolve, reject) => {
__classPrivateFieldSet(this, _EventStream_resolveEndPromise, resolve, "f");
__classPrivateFieldSet(this, _EventStream_rejectEndPromise, reject, "f");
}), "f");
__classPrivateFieldGet(this, _EventStream_connectedPromise, "f").catch(() => {
});
__classPrivateFieldGet(this, _EventStream_endPromise, "f").catch(() => {
});
}
_run(executor) {
setTimeout(() => {
executor().then(() => {
this._emitFinal();
this._emit("end");
}, __classPrivateFieldGet(this, _EventStream_instances, "m", _EventStream_handleError).bind(this));
}, 0);
}
_connected() {
if (this.ended)
return;
__classPrivateFieldGet(this, _EventStream_resolveConnectedPromise, "f").call(this);
this._emit("connect");
}
get ended() {
return __classPrivateFieldGet(this, _EventStream_ended, "f");
}
get errored() {
return __classPrivateFieldGet(this, _EventStream_errored, "f");
}
get aborted() {
return __classPrivateFieldGet(this, _EventStream_aborted, "f");
}
abort() {
this.controller.abort();
}
/**
* Adds the listener function to the end of the listeners array for the event.
* No checks are made to see if the listener has already been added. Multiple calls passing
* the same combination of event and listener will result in the listener being added, and
* called, multiple times.
* @returns this ChatCompletionStream, so that calls can be chained
*/
on(event, listener) {
const listeners = __classPrivateFieldGet(this, _EventStream_listeners, "f")[event] || (__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = []);
listeners.push({ listener });
return this;
}
/**
* Removes the specified listener from the listener array for the event.
* off() will remove, at most, one instance of a listener from the listener array. If any single
* listener has been added multiple times to the listener array for the specified event, then
* off() must be called multiple times to remove each instance.
* @returns this ChatCompletionStream, so that calls can be chained
*/
off(event, listener) {
const listeners = __classPrivateFieldGet(this, _EventStream_listeners, "f")[event];
if (!listeners)
return this;
const index = listeners.findIndex((l) => l.listener === listener);
if (index >= 0)
listeners.splice(index, 1);
return this;
}
/**
* Adds a one-time listener function for the event. The next time the event is triggered,
* this listener is removed and then invoked.
* @returns this ChatCompletionStream, so that calls can be chained
*/
once(event, listener) {
const listeners = __classPrivateFieldGet(this, _EventStream_listeners, "f")[event] || (__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = []);
listeners.push({ listener, once: true });
return this;
}
/**
* This is similar to `.once()`, but returns a Promise that resolves the next time
* the event is triggered, instead of calling a listener callback.
* @returns a Promise that resolves the next time given event is triggered,
* or rejects if an error is emitted. (If you request the 'error' event,
* returns a promise that resolves with the error).
*
* Example:
*
* const message = await stream.emitted('message') // rejects if the stream errors
*/
emitted(event) {
return new Promise((resolve, reject) => {
__classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, "f");
if (event !== "error")
this.once("error", reject);
this.once(event, resolve);
});
}
async done() {
__classPrivateFieldSet(this, _EventStream_catchingPromiseCreated, true, "f");
await __classPrivateFieldGet(this, _EventStream_endPromise, "f");
}
_emit(event, ...args) {
if (__classPrivateFieldGet(this, _EventStream_ended, "f")) {
return;
}
if (event === "end") {
__classPrivateFieldSet(this, _EventStream_ended, true, "f");
__classPrivateFieldGet(this, _EventStream_resolveEndPromise, "f").call(this);
}
const listeners = __classPrivateFieldGet(this, _EventStream_listeners, "f")[event];
if (listeners) {
__classPrivateFieldGet(this, _EventStream_listeners, "f")[event] = listeners.filter((l) => !l.once);
listeners.forEach(({ listener }) => listener(...args));
}
if (event === "abort") {
const error = args[0];
if (!__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
Promise.reject(error);
}
__classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
__classPrivateFieldGet(this, _EventStream_rejectEndPromise, "f").call(this, error);
this._emit("end");
return;
}
if (event === "error") {
const error = args[0];
if (!__classPrivateFieldGet(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
Promise.reject(error);
}
__classPrivateFieldGet(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
__classPrivateFieldGet(this, _EventStream_rejectEndPromise, "f").call(this, error);
this._emit("end");
}
}
_emitFinal() {
}
};
_EventStream_connectedPromise = /* @__PURE__ */ new WeakMap(), _EventStream_resolveConnectedPromise = /* @__PURE__ */ new WeakMap(), _EventStream_rejectConnectedPromise = /* @__PURE__ */ new WeakMap(), _EventStream_endPromise = /* @__PURE__ */ new WeakMap(), _EventStream_resolveEndPromise = /* @__PURE__ */ new WeakMap(), _EventStream_rejectEndPromise = /* @__PURE__ */ new WeakMap(), _EventStream_listeners = /* @__PURE__ */ new WeakMap(), _EventStream_ended = /* @__PURE__ */ new WeakMap(), _EventStream_errored = /* @__PURE__ */ new WeakMap(), _EventStream_aborted = /* @__PURE__ */ new WeakMap(), _EventStream_catchingPromiseCreated = /* @__PURE__ */ new WeakMap(), _EventStream_instances = /* @__PURE__ */ new WeakSet(), _EventStream_handleError = function _EventStream_handleError2(error) {
__classPrivateFieldSet(this, _EventStream_errored, true, "f");
if (error instanceof Error && error.name === "AbortError") {
error = new APIUserAbortError();
}
if (error instanceof APIUserAbortError) {
__classPrivateFieldSet(this, _EventStream_aborted, true, "f");
return this._emit("abort", error);
}
if (error instanceof OpenAIError) {
return this._emit("error", error);
}
if (error instanceof Error) {
const openAIError = new OpenAIError(error.message);
openAIError.cause = error;
return this._emit("error", openAIError);
}
return this._emit("error", new OpenAIError(String(error)));
};
// dist/lib/parser.mjs
function isAutoParsableResponseFormat(response_format) {
return response_format?.["$brand"] === "auto-parseable-response-format";
}
function isAutoParsableTool(tool) {
return tool?.["$brand"] === "auto-parseable-tool";
}
function maybeParseChatCompletion(completion, params) {
if (!params || !hasAutoParseableInput(params)) {
return {
...completion,
choices: completion.choices.map((choice) => ({
...choice,
message: {
...choice.message,
parsed: null,
...choice.message.tool_calls ? {
tool_calls: choice.message.tool_calls
} : void 0
}
}))
};
}
return parseChatCompletion(completion, params);
}
function parseChatCompletion(completion, params) {
const choices = completion.choices.map((choice) => {
if (choice.finish_reason === "length") {
throw new LengthFinishReasonError();
}
if (choice.finish_reason === "content_filter") {
throw new ContentFilterFinishReasonError();
}
return {
...choice,
message: {
...choice.message,
...choice.message.tool_calls ? {
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? void 0
} : void 0,
parsed: choice.message.content && !choice.message.refusal ? parseResponseFormat(params, choice.message.content) : null
}
};
});
return { ...completion, choices };
}
function parseResponseFormat(params, content) {
if (params.response_format?.type !== "json_schema") {
return null;
}
if (params.response_format?.type === "json_schema") {
if ("$parseRaw" in params.response_format) {
const response_format = params.response_format;
return response_format.$parseRaw(content);
}
return JSON.parse(content);
}
return null;
}
function parseToolCall(params, toolCall) {
const inputTool = params.tools?.find((inputTool2) => inputTool2.function?.name === toolCall.function.name);
return {
...toolCall,
function: {
...toolCall.function,
parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCall.function.arguments) : inputTool?.function.strict ? JSON.parse(toolCall.function.arguments) : null
}
};
}
function shouldParseToolCall(params, toolCall) {
if (!params) {
return false;
}
const inputTool = params.tools?.find((inputTool2) => inputTool2.function?.name === toolCall.function.name);
return isAutoParsableTool(inputTool) || inputTool?.function.strict || false;
}
function hasAutoParseableInput(params) {
if (isAutoParsableResponseFormat(params.response_format)) {
return true;
}
return params.tools?.some((t) => isAutoParsableTool(t) || t.type === "function" && t.function.strict === true) ?? false;
}
function validateInputTools(tools) {
for (const tool of tools ?? []) {
if (tool.type !== "function") {
throw new OpenAIError(`Currently only \`function\` tool types support auto-parsing; Received \`${tool.type}\``);
}
if (tool.function.strict !== true) {
throw new OpenAIError(`The \`${tool.function.name}\` tool is not marked with \`strict: true\`. Only strict function tools can be auto-parsed`);
}
}
}
// dist/lib/AbstractChatCompletionRunner.mjs
var _AbstractChatCompletionRunner_instances;
var _AbstractChatCompletionRunner_getFinalContent;
var _AbstractChatCompletionRunner_getFinalMessage;
var _AbstractChatCompletionRunner_getFinalFunctionToolCall;
var _AbstractChatCompletionRunner_getFinalFunctionToolCallResult;
var _AbstractChatCompletionRunner_calculateTotalUsage;
var _AbstractChatCompletionRunner_validateParams;
var _AbstractChatCompletionRunner_stringifyFunctionCallResult;
var DEFAULT_MAX_CHAT_COMPLETIONS = 10;
var AbstractChatCompletionRunner = class extends EventStream {
constructor() {
super(...arguments);
_AbstractChatCompletionRunner_instances.add(this);
this._chatCompletions = [];
this.messages = [];
}
_addChatCompletion(chatCompletion) {
this._chatCompletions.push(chatCompletion);
this._emit("chatCompletion", chatCompletion);
const message = chatCompletion.choices[0]?.message;
if (message)
this._addMessage(message);
return chatCompletion;
}
_addMessage(message, emit = true) {
if (!("content" in message))
message.content = null;
this.messages.push(message);
if (emit) {
this._emit("message", message);
if (isToolMessage(message) && message.content) {
this._emit("functionToolCallResult", message.content);
} else if (isAssistantMessage(message) && message.tool_calls) {
for (const tool_call of message.tool_calls) {
if (tool_call.type === "function") {
this._emit("functionToolCall", tool_call.function);
}
}
}
}
}
/**
* @returns a promise that resolves with the final ChatCompletion, or rejects
* if an error occurred or the stream ended prematurely without producing a ChatCompletion.
*/
async finalChatCompletion() {
await this.done();
const completion = this._chatCompletions[this._chatCompletions.length - 1];
if (!completion)
throw new OpenAIError("stream ended without producing a ChatCompletion");
return completion;
}
/**
* @returns a promise that resolves with the content of the final ChatCompletionMessage, or rejects
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
*/
async finalContent() {
await this.done();
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
}
/**
* @returns a promise that resolves with the the final assistant ChatCompletionMessage response,
* or rejects if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
*/
async finalMessage() {
await this.done();
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
}
/**
* @returns a promise that resolves with the content of the final FunctionCall, or rejects
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
*/
async finalFunctionToolCall() {
await this.done();
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionToolCall).call(this);
}
async finalFunctionToolCallResult() {
await this.done();
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionToolCallResult).call(this);
}
async totalUsage() {
await this.done();
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this);
}
allChatCompletions() {
return [...this._chatCompletions];
}
_emitFinal() {
const completion = this._chatCompletions[this._chatCompletions.length - 1];
if (completion)
this._emit("finalChatCompletion", completion);
const finalMessage = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
if (finalMessage)
this._emit("finalMessage", finalMessage);
const finalContent = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
if (finalContent)
this._emit("finalContent", finalContent);
const finalFunctionCall = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionToolCall).call(this);
if (finalFunctionCall)
this._emit("finalFunctionToolCall", finalFunctionCall);
const finalFunctionCallResult = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionToolCallResult).call(this);
if (finalFunctionCallResult != null)
this._emit("finalFunctionToolCallResult", finalFunctionCallResult);
if (this._chatCompletions.some((c) => c.usage)) {
this._emit("totalUsage", __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this));
}
}
async _createChatCompletion(client, params, options) {
const signal = options?.signal;
if (signal) {
if (signal.aborted)
this.controller.abort();
signal.addEventListener("abort", () => this.controller.abort());
}
__classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_validateParams).call(this, params);
const chatCompletion = await client.chat.completions.create({ ...params, stream: false }, { ...options, signal: this.controller.signal });
this._connected();
return this._addChatCompletion(parseChatCompletion(chatCompletion, params));
}
async _runChatCompletion(client, params, options) {
for (const message of params.messages) {
this._addMessage(message, false);
}
return await this._createChatCompletion(client, params, options);
}
async _runTools(client, params, options) {
const role = "tool";
const { tool_choice = "auto", stream, ...restParams } = params;
const singleFunctionToCall = typeof tool_choice !== "string" && tool_choice?.function?.name;
const { maxChatCompletions = DEFAULT_MAX_CHAT_COMPLETIONS } = options || {};
const inputTools = params.tools.map((tool) => {
if (isAutoParsableTool(tool)) {
if (!tool.$callback) {
throw new OpenAIError("Tool given to `.runTools()` that does not have an associated function");
}
return {
type: "function",
function: {
function: tool.$callback,
name: tool.function.name,
description: tool.function.description || "",
parameters: tool.function.parameters,
parse: tool.$parseRaw,
strict: true
}
};
}
return tool;
});
const functionsByName = {};
for (const f of inputTools) {
if (f.type === "function") {
functionsByName[f.function.name || f.function.function.name] = f.function;
}
}
const tools = "tools" in params ? inputTools.map((t) => t.type === "function" ? {
type: "function",
function: {
name: t.function.name || t.function.function.name,
parameters: t.function.parameters,
description: t.function.description,
strict: t.function.strict
}
} : t) : void 0;
for (const message of params.messages) {
this._addMessage(message, false);
}
for (let i = 0; i < maxChatCompletions; ++i) {
const chatCompletion = await this._createChatCompletion(client, {
...restParams,
tool_choice,
tools,
messages: [...this.messages]
}, options);
const message = chatCompletion.choices[0]?.message;
if (!message) {
throw new OpenAIError(`missing message in ChatCompletion response`);
}
if (!message.tool_calls?.length) {
return;
}
for (const tool_call of message.tool_calls) {
if (tool_call.type !== "function")
continue;
const tool_call_id = tool_call.id;
const { name, arguments: args } = tool_call.function;
const fn = functionsByName[name];
if (!fn) {
const content2 = `Invalid tool_call: ${JSON.stringify(name)}. Available options are: ${Object.keys(functionsByName).map((name2) => JSON.stringify(name2)).join(", ")}. Please try again`;
this._addMessage({ role, tool_call_id, content: content2 });
continue;
} else if (singleFunctionToCall && singleFunctionToCall !== name) {
const content2 = `Invalid tool_call: ${JSON.stringify(name)}. ${JSON.stringify(singleFunctionToCall)} requested. Please try again`;
this._addMessage({ role, tool_call_id, content: content2 });
continue;
}
let parsed;
try {
parsed = isRunnableFunctionWithParse(fn) ? await fn.parse(args) : args;
} catch (error) {
const content2 = error instanceof Error ? error.message : String(error);
this._addMessage({ role, tool_call_id, content: content2 });
continue;
}
const rawContent = await fn.function(parsed, this);
const content = __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
this._addMessage({ role, tool_call_id, content });
if (singleFunctionToCall) {
return;
}
}
}
return;
}
};
_AbstractChatCompletionRunner_instances = /* @__PURE__ */ new WeakSet(), _AbstractChatCompletionRunner_getFinalContent = function _AbstractChatCompletionRunner_getFinalContent2() {
return __classPrivateFieldGet(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this).content ?? null;
}, _AbstractChatCompletionRunner_getFinalMessage = function _AbstractChatCompletionRunner_getFinalMessage2() {
let i = this.messages.length;
while (i-- > 0) {
const message = this.messages[i];
if (isAssistantMessage(message)) {
const ret = {
...message,
content: message.content ?? null,
refusal: message.refusal ?? null
};
return ret;
}
}
throw new OpenAIError("stream ended without producing a ChatCompletionMessage with role=assistant");
}, _AbstractChatCompletionRunner_getFinalFunctionToolCall = function _AbstractChatCompletionRunner_getFinalFunctionToolCall2() {
for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (isAssistantMessage(message) && message?.tool_calls?.length) {
return message.tool_calls.at(-1)?.function;
}
}
return;
}, _AbstractChatCompletionRunner_getFinalFunctionToolCallResult = function _AbstractChatCompletionRunner_getFinalFunctionToolCallResult2() {
for (let i = this.messages.length - 1; i >= 0; i--) {
const message = this.messages[i];
if (isToolMessage(message) && message.content != null && typeof message.content === "string" && this.messages.some((x) => x.role === "assistant" && x.tool_calls?.some((y) => y.type === "function" && y.id === message.tool_call_id))) {
return message.content;
}
}
return;
}, _AbstractChatCompletionRunner_calculateTotalUsage = function _AbstractChatCompletionRunner_calculateTotalUsage2() {
const total = {
completion_tokens: 0,
prompt_tokens: 0,
total_tokens: 0
};
for (const { usage } of this._chatCompletions) {
if (usage) {
total.completion_tokens += usage.completion_tokens;
total.prompt_tokens += usage.prompt_tokens;
total.total_tokens += usage.total_tokens;
}
}
return total;
}, _AbstractChatCompletionRunner_validateParams = function _AbstractChatCompletionRunner_validateParams2(params) {
if (params.n != null && params.n > 1) {
throw new OpenAIError("ChatCompletion convenience helpers only support n=1 at this time. To use n>1, please use chat.completions.create() directly.");
}
}, _AbstractChatCompletionRunner_stringifyFunctionCallResult = function _AbstractChatCompletionRunner_stringifyFunctionCallResult2(rawContent) {
return typeof rawContent === "string" ? rawContent : rawContent === void 0 ? "undefined" : JSON.stringify(rawContent);
};
// dist/lib/ChatCompletionRunner.mjs
var ChatCompletionRunner = class _ChatCompletionRunner extends AbstractChatCompletionRunner {
static runTools(client, params, options) {
const runner = new _ChatCompletionRunner();
const opts = {
...options,
headers: { ...options?.headers, "X-Stainless-Helper-Method": "runTools" }
};
runner._run(() => runner._runTools(client, params, opts));
return runner;
}
_addMessage(message, emit = true) {
super._addMessage(message, emit);
if (isAssistantMessage(message) && message.content) {
this._emit("content", message.content);
}
}
};
// dist/_vendor/partial-json-parser/parser.mjs
var STR = 1;
var NUM = 2;
var ARR = 4;
var OBJ = 8;
var NULL = 16;
var BOOL = 32;
var NAN = 64;
var INFINITY = 128;
var MINUS_INFINITY = 256;
var INF = INFINITY | MINUS_INFINITY;
var SPECIAL = NULL | BOOL | INF | NAN;
var ATOM = STR | NUM | SPECIAL;
var COLLECTION = ARR | OBJ;
var ALL = ATOM | COLLECTION;
var Allow = {
STR,
NUM,
ARR,
OBJ,
NULL,
BOOL,
NAN,
INFINITY,
MINUS_INFINITY,
INF,
SPECIAL,
ATOM,
COLLECTION,
ALL
};
var PartialJSON = class extends Error {
};
var MalformedJSON = class extends Error {
};
function parseJSON(jsonString, allowPartial = Allow.ALL) {
if (typeof jsonString !== "string") {
throw new TypeError(`expecting str, got ${typeof jsonString}`);
}
if (!jsonString.trim()) {
throw new Error(`${jsonString} is empty`);
}
return _parseJSON(jsonString.trim(), allowPartial);
}
var _parseJSON = (jsonString, allow) => {
const length = jsonString.length;
let index = 0;
const markPartialJSON = (msg) => {
throw new PartialJSON(`${msg} at position ${index}`);
};
const throwMalformedError = (msg) => {
throw new MalformedJSON(`${msg} at position ${index}`);
};
const parseAny = () => {
skipBlank();
if (index >= length)
markPartialJSON("Unexpected end of input");
if (jsonString[index] === '"')
return parseStr();
if (jsonString[index] === "{")
return parseObj();
if (jsonString[index] === "[")
return parseArr();
if (jsonString.substring(index, index + 4) === "null" || Allow.NULL & allow && length - index < 4 && "null".startsWith(jsonString.substring(index))) {
index += 4;
return null;
}
if (jsonString.substring(index, index + 4) === "true" || Allow.BOOL & allow && length - index < 4 && "true".startsWith(jsonString.substring(index))) {
index += 4;
return true;
}
if (jsonString.substring(index, index + 5) === "false" || Allow.BOOL & allow && length - index < 5 && "false".startsWith(jsonString.substring(index))) {
index += 5;
return false;
}
if (jsonString.substring(index, index + 8) === "Infinity" || Allow.INFINITY & allow && length - index < 8 && "Infinity".startsWith(jsonString.substring(index))) {
index += 8;
return Infinity;
}
if (jsonString.substring(index, index + 9) === "-Infinity" || Allow.MINUS_INFINITY & allow && 1 < length - index && length - index < 9 && "-Infinity".startsWith(jsonString.substring(index))) {
index += 9;
return -Infinity;
}
if (jsonString.substring(index, index + 3) === "NaN" || Allow.NAN & allow && length - index < 3 && "NaN".startsWith(jsonString.substring(index))) {
index += 3;
return NaN;
}
return parseNum();
};
const parseStr = () => {
const start = index;
let escape2 = false;
index++;
while (index < length && (jsonString[index] !== '"' || escape2 && jsonString[index - 1] === "\\")) {
escape2 = jsonString[index] === "\\" ? !escape2 : false;
index++;
}
if (jsonString.charAt(index) == '"') {
try {
return JSON.parse(jsonString.substring(start, ++index - Number(escape2)));
} catch (e) {
throwMalformedError(String(e));
}
} else if (Allow.STR & allow) {
try {
return JSON.parse(jsonString.substring(start, index - Number(escape2)) + '"');
} catch (e) {
return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf("\\")) + '"');
}
}
markPartialJSON("Unterminated string literal");
};
const parseObj = () => {
index++;
skipBlank();
const obj = {};
try {
while (jsonString[index] !== "}") {
skipBlank();
if (index >= length && Allow.OBJ & allow)
return obj;
const key = parseStr();
skipBlank();
index++;
try {
const value = parseAny();
Object.defineProperty(obj, key, { value, writable: true, enumerable: true, configurable: true });
} catch (e) {
if (Allow.OBJ & allow)
return obj;
else
throw e;
}
skipBlank();
if (jsonString[index] === ",")
index++;
}
} catch (e) {
if (Allow.OBJ & allow)
return obj;
else
markPartialJSON("Expected '}' at end of object");
}
index++;
return obj;
};
const parseArr = () => {
index++;
const arr = [];
try {
while (jsonString[index] !== "]") {
arr.push(parseAny());
skipBlank();
if (jsonString[index] === ",") {
index++;
}
}
} catch (e) {
if (Allow.ARR & allow) {
return arr;
}
markPartialJSON("Expected ']' at end of array");
}
index++;
return arr;
};
const parseNum = () => {
if (index === 0) {
if (jsonString === "-" && Allow.NUM & allow)
markPartialJSON("Not sure what '-' is");
try {
return JSON.parse(jsonString);
} catch (e) {
if (Allow.NUM & allow) {
try {
if ("." === jsonString[jsonString.length - 1])
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf(".")));
return JSON.parse(jsonString.substring(0, jsonString.lastIndexOf("e")));
} catch (e2) {
}
}
throwMalformedError(String(e));
}
}
const start = index;
if (jsonString[index] === "-")
index++;
while (jsonString[index] && !",]}".includes(jsonString[index]))
index++;
if (index == length && !(Allow.NUM & allow))
markPartialJSON("Unterminated number literal");
try {
return JSON.parse(jsonString.substring(start, index));
} catch (e) {
if (jsonString.substring(start, index) === "-" && Allow.NUM & allow)
markPartialJSON("Not sure what '-' is");
try {
return JSON.parse(jsonString.substring(start, jsonString.lastIndexOf("e")));
} catch (e2) {
throwMalformedError(String(e2));
}
}
};
const skipBlank = () => {
while (index < length && " \n\r ".includes(jsonString[index])) {
index++;
}
};
return parseAny();
};
var partialParse = (input) => parseJSON(input, Allow.ALL ^ Allow.NUM);
// dist/lib/ChatCompletionStream.mjs
var _ChatCompletionStream_instances;
var _ChatCompletionStream_params;
var _ChatCompletionStream_choiceEventStates;
var _ChatCompletionStream_currentChatCompletionSnapshot;
var _ChatCompletionStream_beginRequest;
var _ChatCompletionStream_getChoiceEventState;
var _ChatCompletionStream_addChunk;
var _ChatCompletionStream_emitToolCallDoneEvent;
var _ChatCompletionStream_emitContentDoneEvents;
var _ChatCompletionStream_endRequest;
var _ChatCompletionStream_getAutoParseableResponseFormat;
var _ChatCompletionStream_accumulateChatCompletion;
var ChatCompletionStream = class _ChatCompletionStream extends AbstractChatCompletionRunner {
constructor(params) {
super();
_ChatCompletionStream_instances.add(this);
_ChatCompletionStream_params.set(this, void 0);
_ChatCompletionStream_choiceEventStates.set(this, void 0);
_ChatCompletionStream_currentChatCompletionSnapshot.set(this, void 0);
__classPrivateFieldSet(this, _ChatCompletionStream_params, params, "f");
__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
}
get currentChatCompletionSnapshot() {
return __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
}
/**
* Intended for use on the frontend, consuming a stream produced with
* `.toReadableStream()` on the backend.
*
* Note that messages sent to the model do not appear in `.on('message')`
* in this context.
*/
static fromReadableStream(stream) {
const runner = new _ChatCompletionStream(null);
runner._run(() => runner._fromReadableStream(stream));
return runner;
}
static createChatCompletion(client, params, options) {
const runner = new _ChatCompletionStream(params);
runner._run(() => runner._runChatCompletion(client, { ...params, stream: true }, { ...options, headers: { ...options?.headers, "X-Stainless-Helper-Method": "stream" } }));
return runner;
}
async _createChatCompletion(client, params, options) {
super._createChatCompletion;
const signal = options?.signal;
if (signal) {
if (signal.aborted)
this.controller.abort();
signal.addEventListener("abort", () => this.controller.abort());
}
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
this._connected();
for await (const chunk of stream) {
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
}
if (stream.controller.signal?.aborted) {
throw new APIUserAbortError();
}
return this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
}
async _fromReadableStream(readableStream, options) {
const signal = options?.signal;
if (signal) {
if (signal.aborted)
this.controller.abort();
signal.addEventListener("abort", () => this.controller.abort());
}
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
this._connected();
const stream = Stream.fromReadableStream(readableStream, this.controller);
let chatId;
for await (const chunk of stream) {
if (chatId && chatId !== chunk.id) {
this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
}
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
chatId = chunk.id;
}
if (stream.controller.signal?.aborted) {
throw new APIUserAbortError();
}
return this._addChatCompletion(__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
}
[(_ChatCompletionStream_params = /* @__PURE__ */ new WeakMap(), _ChatCompletionStream_choiceEventStates = /* @__PURE__ */ new WeakMap(), _ChatCompletionStream_currentChatCompletionSnapshot = /* @__PURE__ */ new WeakMap(), _ChatCompletionStream_instances = /* @__PURE__ */ new WeakSet(), _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest2() {
if (this.ended)
return;
__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, void 0, "f");
}, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState2(choice) {
let state = __classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
if (state) {
return state;
}
state = {
content_done: false,
refusal_done: false,
logprobs_content_done: false,
logprobs_refusal_done: false,
done_tool_calls: /* @__PURE__ */ new Set(),
current_tool_call_index: null
};
__classPrivateFieldGet(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
return state;
}, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk2(chunk) {
if (this.ended)
return;
const completion = __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
this._emit("chunk", chunk, completion);
for (const choice of chunk.choices) {
const choiceSnapshot = completion.choices[choice.index];
if (choice.delta.content != null && choiceSnapshot.message?.role === "assistant" && choiceSnapshot.message?.content) {
this._emit("content", choice.delta.content, choiceSnapshot.message.content);
this._emit("content.delta", {
delta: choice.delta.content,
snapshot: choiceSnapshot.message.content,
parsed: choiceSnapshot.message.parsed
});
}
if (choice.delta.refusal != null && choiceSnapshot.message?.role === "assistant" && choiceSnapshot.message?.refusal) {
this._emit("refusal.delta", {
delta: choice.delta.refusal,
snapshot: choiceSnapshot.message.refusal
});
}
if (choice.logprobs?.content != null && choiceSnapshot.message?.role === "assistant") {
this._emit("logprobs.content.delta", {
content: choice.logprobs?.content,
snapshot: choiceSnapshot.logprobs?.content ?? []
});
}
if (choice.logprobs?.refusal != null && choiceSnapshot.message?.role === "assistant") {
this._emit("logprobs.refusal.delta", {
refusal: choice.logprobs?.refusal,
snapshot: choiceSnapshot.logprobs?.refusal ?? []
});
}
const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
if (choiceSnapshot.finish_reason) {
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
if (state.current_tool_call_index != null) {
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
}
}
for (const toolCall of choice.delta.tool_calls ?? []) {
if (state.current_tool_call_index !== toolCall.index) {
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
if (state.current_tool_call_index != null) {
__classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
}
}
state.current_tool_call_index = toolCall.index;
}
for (const toolCallDelta of choice.delta.tool_calls ?? []) {
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallDelta.index];
if (!toolCallSnapshot?.type) {
continue;
}
if (toolCallSnapshot?.type === "function") {
this._emit("tool_calls.function.arguments.delta", {
name: toolCallSnapshot.function?.name,
index: toolCallDelta.index,
arguments: toolCallSnapshot.function.arguments,
parsed_arguments: toolCallSnapshot.function.parsed_arguments,
arguments_delta: toolCallDelta.function?.arguments ?? ""
});
} else {
assertNever(toolCallSnapshot?.type);
}
}
}
}, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent2(choiceSnapshot, toolCallIndex) {
const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
if (state.done_tool_calls.has(toolCallIndex)) {
return;
}
const toolCallSnapshot = choiceSnapshot.message.tool_calls?.[toolCallIndex];
if (!toolCallSnapshot) {
throw new Error("no tool call snapshot");
}
if (!toolCallSnapshot.type) {
throw new Error("tool call snapshot missing `type`");
}
if (toolCallSnapshot.type === "function") {
const inputTool = __classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => tool.type === "function" && tool.function.name === toolCallSnapshot.function.name);
this._emit("tool_calls.function.arguments.done", {
name: toolCallSnapshot.function.name,
index: toolCallIndex,
arguments: toolCallSnapshot.function.arguments,
parsed_arguments: isAutoParsableTool(inputTool) ? inputTool.$parseRaw(toolCallSnapshot.function.arguments) : inputTool?.function.strict ? JSON.parse(toolCallSnapshot.function.arguments) : null
});
} else {
assertNever(toolCallSnapshot.type);
}
}, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents2(choiceSnapshot) {
const state = __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
if (choiceSnapshot.message.content && !state.content_done) {
state.content_done = true;
const responseFormat = __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);
this._emit("content.done", {
content: choiceSnapshot.message.content,
parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null
});
}
if (choiceSnapshot.message.refusal && !state.refusal_done) {
state.refusal_done = true;
this._emit("refusal.done", { refusal: choiceSnapshot.message.refusal });
}
if (choiceSnapshot.logprobs?.content && !state.logprobs_content_done) {
state.logprobs_content_done = true;
this._emit("logprobs.content.done", { content: choiceSnapshot.logprobs.content });
}
if (choiceSnapshot.logprobs?.refusal && !state.logprobs_refusal_done) {
state.logprobs_refusal_done = true;
this._emit("logprobs.refusal.done", { refusal: choiceSnapshot.logprobs.refusal });
}
}, _ChatCompletionStream_endRequest = function _ChatCompletionStream_endRequest2() {
if (this.ended) {
throw new OpenAIError(`stream has ended, this shouldn't happen`);
}
const snapshot = __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
if (!snapshot) {
throw new OpenAIError(`request ended without sending any chunks`);
}
__classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, void 0, "f");
__classPrivateFieldSet(this, _ChatCompletionStream_choiceEventStates, [], "f");
return finalizeChatCompletion(snapshot, __classPrivateFieldGet(this, _ChatCompletionStream_params, "f"));
}, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat2() {
const responseFormat = __classPrivateFieldGet(this, _ChatCompletionStream_params, "f")?.response_format;
if (isAutoParsableResponseFormat(responseFormat)) {
return responseFormat;
}
return null;
}, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion2(chunk) {
var _a3, _b, _c, _d;
let snapshot = __classPrivateFieldGet(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
const { choices, ...rest } = chunk;
if (!snapshot) {
snapshot = __classPrivateFieldSet(this, _ChatCompletionStream_currentChatCompletionSnapshot, {
...rest,
choices: []
}, "f");
} else {
Object.assign(snapshot, rest);
}
for (const { delta, finish_reason, index, logprobs = null, ...other } of chunk.choices) {
let choice = snapshot.choices[index];
if (!choice) {
choice = snapshot.choices[index] = { finish_reason, index, message: {}, logprobs, ...other };
}
if (logprobs) {
if (!choice.logprobs) {
choice.logprobs = Object.assign({}, logprobs);
} else {
const { content: content2, refusal: refusal2, ...rest3 } = logprobs;
assertIsEmpty(rest3);
Object.assign(choice.logprobs, rest3);
if (content2) {
(_a3 = choice.logprobs).content ?? (_a3.content = []);
choice.logprobs.content.push(...content2);
}
if (refusal2) {
(_b = choice.logprobs).refusal ?? (_b.refusal = []);
choice.logprobs.refusal.push(...refusal2);
}
}
}
if (finish_reason) {
choice.finish_reason = finish_reason;
if (__classPrivateFieldGet(this, _ChatCompletionStream_params, "f") && hasAutoParseableInput(__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"))) {
if (finish_reason === "length") {
throw new LengthFinishReasonError();
}
if (finish_reason === "content_filter") {
throw new ContentFilterFinishReasonError();
}
}
}
Object.assign(choice, other);
if (!delta)
continue;
const { content, refusal, function_call, role, tool_calls, ...rest2 } = delta;
assertIsEmpty(rest2);
Object.assign(choice.message, rest2);
if (refusal) {
choice.message.refusal = (choice.message.refusal || "") + refusal;
}
if (role)
choice.message.role = role;
if (function_call) {
if (!choice.message.function_call) {
choice.message.function_call = function_call;
} else {
if (function_call.name)
choice.message.function_call.name = function_call.name;
if (function_call.arguments) {
(_c = choice.message.function_call).arguments ?? (_c.arguments = "");
choice.message.function_call.arguments += function_call.arguments;
}
}
}
if (content) {
choice.message.content = (choice.message.content || "") + content;
if (!choice.message.refusal && __classPrivateFieldGet(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this)) {
choice.message.parsed = partialParse(choice.message.content);
}
}
if (tool_calls) {
if (!choice.message.tool_calls)
choice.message.tool_calls = [];
for (const { index: index2, id, type, function: fn, ...rest3 } of tool_calls) {
const tool_call = (_d = choice.message.tool_calls)[index2] ?? (_d[index2] = {});
Object.assign(tool_call, rest3);
if (id)
tool_call.id = id;
if (type)
tool_call.type = type;
if (fn)
tool_call.function ?? (tool_call.function = { name: fn.name ?? "", arguments: "" });
if (fn?.name)
tool_call.function.name = fn.name;
if (fn?.arguments) {
tool_call.function.arguments += fn.arguments;
if (shouldParseToolCall(__classPrivateFieldGet(this, _ChatCompletionStream_params, "f"), tool_call)) {
tool_call.function.parsed_arguments = partialParse(tool_call.function.arguments);
}
}
}
}
}
return snapshot;
}, Symbol.asyncIterator)]() {
const pushQueue = [];
const readQueue = [];
let done = false;
this.on("chunk", (chunk) => {
const reader = readQueue.shift();
if (reader) {
reader.resolve(chunk);
} else {
pushQueue.push(chunk);
}
});
this.on("end", () => {
done = true;
for (const reader of readQueue) {
reader.resolve(void 0);
}
readQueue.length = 0;
});
this.on("abort", (err) => {
done = true;
for (const reader of readQueue) {
reader.reject(err);
}
readQueue.length = 0;
});
this.on("error", (err) => {
done = true;
for (const reader of readQueue) {
reader.reject(err);
}
readQueue.length = 0;
});
return {
next: async () => {
if (!pushQueue.length) {
if (done) {
return { value: void 0, done: true };
}
return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk2) => chunk2 ? { value: chunk2, done: false } : { value: void 0, done: true });
}
const chunk = pushQueue.shift();
return { value: chunk, done: false };
},
return: async () => {
this.abort();
return { value: void 0, done: true };
}
};
}
toReadableStream() {
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
return stream.toReadableStream();
}
};
function finalizeChatCompletion(snapshot, params) {
const { id, choices, created, model, system_fingerprint, ...rest } = snapshot;
const completion = {
...rest,
id,
choices: choices.map(({ message, finish_reason, index, logprobs, ...choiceRest }) => {
if (!finish_reason) {
throw new OpenAIError(`missing finish_reason for choice ${index}`);
}
const { content = null, function_call, tool_calls, ...messageRest } = message;
const role = message.role;
if (!role) {
throw new OpenAIError(`missing role for choice ${index}`);
}
if (function_call) {
const { arguments: args, name } = function_call;
if (args == null) {
throw new OpenAIError(`missing function_call.arguments for choice ${index}`);
}
if (!name) {
throw new OpenAIError(`missing function_call.name for choice ${index}`);
}
return {
...choiceRest,
message: {
content,
function_call: { arguments: args, name },
role,
refusal: message.refusal ?? null
},
finish_reason,
index,
logprobs
};
}
if (tool_calls) {
return {
...choiceRest,
index,
finish_reason,
logprobs,
message: {
...messageRest,
role,
content,
refusal: message.refusal ?? null,
tool_calls: tool_calls.map((tool_call, i) => {
const { function: fn, type, id: id2, ...toolRest } = tool_call;
const { arguments: args, name, ...fnRest } = fn || {};
if (id2 == null) {
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].id
${str(snapshot)}`);
}
if (type == null) {
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].type
${str(snapshot)}`);
}
if (name == null) {
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.name
${str(snapshot)}`);
}
if (args == null) {
throw new OpenAIError(`missing choices[${index}].tool_calls[${i}].function.arguments
${str(snapshot)}`);
}
return { ...toolRest, id: id2, type, function: { ...fnRest, name, arguments: args } };
})
}
};
}
return {
...choiceRest,
message: { ...messageRest, content, role, refusal: message.refusal ?? null },
finish_reason,
index,
logprobs
};
}),
created,
model,
object: "chat.completion",
...system_fingerprint ? { system_fingerprint } : {}
};
return maybeParseChatCompletion(completion, params);
}
function str(x) {
return JSON.stringify(x);
}
function assertIsEmpty(obj) {
return;
}
function assertNever(_x) {
}
// dist/lib/ChatCompletionStreamingRunner.mjs
var ChatCompletionStreamingRunner = class _ChatCompletionStreamingRunner extends ChatCompletionStream {
static fromReadableStream(stream) {
const runner = new _ChatCompletionStreamingRunner(null);
runner._run(() => runner._fromReadableStream(stream));
return runner;
}
static runTools(client, params, options) {
const runner = new _ChatCompletionStreamingRunner(
// @ts-expect-error TODO these types are incompatible
params
);
const opts = {
...options,
headers: { ...options?.headers, "X-Stainless-Helper-Method": "runTools" }
};
runner._run(() => runner._runTools(client, params, opts));
return runner;
}
};
// dist/resources/chat/completions/completions.mjs
var Completions = class extends APIResource {
constructor() {
super(...arguments);
this.messages = new Messages(this._client);
}
create(body, options) {
return this._client.post("/chat/completions", { body, ...options, stream: body.stream ?? false });
}
/**
* Get a stored chat completion. Only Chat Completions that have been created with
* the `store` parameter set to `true` will be returned.
*
* @example
* ```ts
* const chatCompletion =
* await client.chat.completions.retrieve('completion_id');
* ```
*/
retrieve(completionID, options) {
return this._client.get(path`/chat/completions/${completionID}`, options);
}
/**
* Modify a stored chat completion. Only Chat Completions that have been created
* with the `store` parameter set to `true` can be modified. Currently, the only
* supported modification is to update the `metadata` field.
*
* @example
* ```ts
* const chatCompletion = await client.chat.completions.update(
* 'completion_id',
* { metadata: { foo: 'string' } },
* );
* ```
*/
update(completionID, body, options) {
return this._client.post(path`/chat/completions/${completionID}`, { body, ...options });
}
/**
* List stored Chat Completions. Only Chat Completions that have been stored with
* the `store` parameter set to `true` will be returned.
*
* @example
* ```ts
* // Automatically fetches more pages as needed.
* for await (const chatCompletion of client.chat.completions.list()) {
* // ...
* }
* ```
*/
list(query = {}, options) {
return this._client.getAPIList("/chat/completions", CursorPage, { query, ...options });
}
/**
* Delete a stored chat completion. Only Chat Completions that have been created
* with the `store` parameter set to `true` can be deleted.
*
* @example
* ```ts
* const chatCompletionDeleted =
* await client.chat.completions.delete('completion_id');
* ```
*/
delete(completionID, options) {
return this._client.delete(path`/chat/completions/${completionID}`, options);
}
parse(body, options) {
validateInputTools(body.tools);
return this._client.chat.completions.create(body, {
...options,
headers: {
...options?.headers,
"X-Stainless-Helper-Method": "chat.completions.parse"
}
})._thenUnwrap((completion) => parseChatCompletion(completion, body));
}
runTools(body, options) {
if (body.stream) {
return ChatCompletionStreamingRunner.runTools(this._client, body, options);
}
return ChatCompletionRunner.runTools(this._client, body, options);
}
/**
* Creates a chat completion stream
*/
stream(body, options) {
return ChatCompletionStream.createChatCompletion(this._client, body, options);
}
};
Completions.Messages = Messages;
// dist/resources/chat/chat.mjs
var Chat = class extends APIResource {
constructor() {
super(...arguments);
this.completions = new Completions(this._client);
}
};
Chat.Completions = Completions;
// dist/internal/headers.mjs
var brand_privateNullableHeaders = /* @__PURE__ */ Symbol("brand.privateNullableHeaders");
function* iterateHeaders(headers) {
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;
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 === void 0)
continue;
if (shouldClear && !didClear) {
didClear = true;
yield [name, null];
}
yield [name, value];
}
}
}
var buildHeaders = (newHeaders) => {
const targetHeaders = new Headers();
const nullHeaders = /* @__PURE__ */ new Set();
for (const headers of newHeaders) {
const seenHeaders = /* @__PURE__ */ new Set();
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 };
};
// dist/resources/audio/speech.mjs
var Speech = class extends APIResource {
/**
* Generates audio from the input text.
*
* @example
* ```ts
* const speech = await client.audio.speech.create({
* input: 'input',
* model: 'string',
* voice: 'ash',
* });
*
* const content = await speech.blob();
* console.log(content);
* ```
*/
create(body, options) {
return this._client.post("/audio/speech", {
body,
...options,
headers: buildHeaders([{ Accept: "application/octet-stream" }, options?.headers]),
__binaryResponse: true
});
}
};
// dist/resources/audio/transcriptions.mjs
var Transcriptions = class extends APIResource {
create(body, options) {
return this._client.post("/audio/transcriptions", multipartFormRequestOptions({
body,
...options,
stream: body.stream ?? false,
__metadata: { model: body.model }
}, this._client));
}
};
// dist/resources/audio/translations.mjs
var Translations = class extends APIResource {
create(body, options) {
return this._client.post("/audio/translations", multipartFormRequestOptions({ body, ...options, __metadata: { model: body.model } }, this._client));
}
};
// dist/resources/audio/audio.mjs
var Audio = class extends APIResource {
constructor() {
super(...arguments);
this.transcriptions = new Transcriptions(this._client);
this.translations = new Translations(this._client);
this.speech = new Speech(this._client);
}
};
Audio.Transcriptions = Transcriptions;
Audio.Translations = Translations;
Audio.Speech = Speech;
// dist/resources/batches.mjs
var Batches = class extends APIResource {
/**
* Creates and executes a batch from an uploaded file of requests
*/
create(body, options) {
return this._client.post("/batches", { body, ...options });
}
/**
* Retrieves a batch.
*/
retrieve(batchID, options) {
return this._client.get(path`/batches/${batchID}`, options);
}
/**
* List your organization's batches.
*/
list(query = {}, options) {
return this._client.getAPIList("/batches", CursorPage, { query, ...options });
}
/**
* Cancels an in-progress batch. The batch will be in status `cancelling` for up to
* 10 minutes, before changing to `cancelled`, where it will have partial results
* (if any) available in the output file.
*/
cancel(batchID, options) {
return this._client.post(path`/batches/${batchID}/cancel`, options);
}
};
// dist/resources/beta/assistants.mjs
var Assistants = class extends APIResource {
/**
* Create an assistant with a model and instructions.
*
* @example
* ```ts
* const assistant = await client.beta.assistants.create({
* model: 'gpt-4o',
* });
* ```
*/
create(body, options) {
return this._client.post("/assistants", {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Retrieves an assistant.
*
* @example
* ```ts
* const assistant = await client.beta.assistants.retrieve(
* 'assistant_id',
* );
* ```
*/
retrieve(assistantID, options) {
return this._client.get(path`/assistants/${assistantID}`, {
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Modifies an assistant.
*
* @example
* ```ts
* const assistant = await client.beta.assistants.update(
* 'assistant_id',
* );
* ```
*/
update(assistantID, body, options) {
return this._client.post(path`/assistants/${assistantID}`, {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Returns a list of assistants.
*
* @example
* ```ts
* // Automatically fetches more pages as needed.
* for await (const assistant of client.beta.assistants.list()) {
* // ...
* }
* ```
*/
list(query = {}, options) {
return this._client.getAPIList("/assistants", CursorPage, {
query,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Delete an assistant.
*
* @example
* ```ts
* const assistantDeleted =
* await client.beta.assistants.delete('assistant_id');
* ```
*/
delete(assistantID, options) {
return this._client.delete(path`/assistants/${assistantID}`, {
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
};
// dist/resources/beta/realtime/sessions.mjs
var Sessions = class extends APIResource {
/**
* Create an ephemeral API token for use in client-side applications with the
* Realtime API. Can be configured with the same session parameters as the
* `session.update` client event.
*
* It responds with a session object, plus a `client_secret` key which contains a
* usable ephemeral API token that can be used to authenticate browser clients for
* the Realtime API.
*
* @example
* ```ts
* const session =
* await client.beta.realtime.sessions.create();
* ```
*/
create(body, options) {
return this._client.post("/realtime/sessions", {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
};
// dist/resources/beta/realtime/transcription-sessions.mjs
var TranscriptionSessions = class extends APIResource {
/**
* Create an ephemeral API token for use in client-side applications with the
* Realtime API specifically for realtime transcriptions. Can be configured with
* the same session parameters as the `transcription_session.update` client event.
*
* It responds with a session object, plus a `client_secret` key which contains a
* usable ephemeral API token that can be used to authenticate browser clients for
* the Realtime API.
*
* @example
* ```ts
* const transcriptionSession =
* await client.beta.realtime.transcriptionSessions.create();
* ```
*/
create(body, options) {
return this._client.post("/realtime/transcription_sessions", {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
};
// dist/resources/beta/realtime/realtime.mjs
var Realtime = class extends APIResource {
constructor() {
super(...arguments);
this.sessions = new Sessions(this._client);
this.transcriptionSessions = new TranscriptionSessions(this._client);
}
};
Realtime.Sessions = Sessions;
Realtime.TranscriptionSessions = TranscriptionSessions;
// dist/resources/beta/threads/messages.mjs
var Messages2 = class extends APIResource {
/**
* Create a message.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
create(threadID, body, options) {
return this._client.post(path`/threads/${threadID}/messages`, {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Retrieve a message.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
retrieve(messageID, params, options) {
const { thread_id } = params;
return this._client.get(path`/threads/${thread_id}/messages/${messageID}`, {
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Modifies a message.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
update(messageID, params, options) {
const { thread_id, ...body } = params;
return this._client.post(path`/threads/${thread_id}/messages/${messageID}`, {
body,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Returns a list of messages for a given thread.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
list(threadID, query = {}, options) {
return this._client.getAPIList(path`/threads/${threadID}/messages`, CursorPage, {
query,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Deletes a message.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
delete(messageID, params, options) {
const { thread_id } = params;
return this._client.delete(path`/threads/${thread_id}/messages/${messageID}`, {
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
};
// dist/resources/beta/threads/runs/steps.mjs
var Steps = class extends APIResource {
/**
* Retrieves a run step.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
retrieve(stepID, params, options) {
const { thread_id, run_id, ...query } = params;
return this._client.get(path`/threads/${thread_id}/runs/${run_id}/steps/${stepID}`, {
query,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
/**
* Returns a list of run steps belonging to a run.
*
* @deprecated The Assistants API is deprecated in favor of the Responses API
*/
list(runID, params, options) {
const { thread_id, ...query } = params;
return this._client.getAPIList(path`/threads/${thread_id}/runs/${runID}/steps`, CursorPage, {
query,
...options,
headers: buildHeaders([{ "OpenAI-Beta": "assistants=v2" }, options?.headers])
});
}
};
// dist/internal/utils/base64.mjs
var toFloat32Array = (base64Str) => {
if (typeof Buffer !== "undefined") {
const buf = Buffer.from(base64Str, "base64");
return Array.from(new Float32Array(buf.buffer, buf.byteOffset, buf.length / Float32Array.BYTES_PER_ELEMENT));
} else {
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));
}
};
// dist/internal/utils/env.mjs
var readEnv = (env) => {
if (typeof globalThis.process !== "undefined") {
return globalThis.process.env?.[env]?.trim() ?? void 0;
}
if (typeof globalThis.Deno !== "undefined") {
return globalThis.Deno.env?.get?.(env)?.trim();
}
return void 0;
};
// dist/lib/AssistantStream.mjs
var _AssistantStream_instances;
var _a;
var _AssistantStream_events;
var _AssistantStream_runStepSnapshots;
var _AssistantStream_messageSnapshots;
var _AssistantStream_messageSnapshot;
var _AssistantStream_finalRun;
var _AssistantStream_currentContentIndex;
var _AssistantStream_currentContent;
var _AssistantStream_currentToolCallIndex;
var _AssistantStream_currentToolCall;
var _AssistantStream_currentEvent;
var _AssistantStream_currentRunSnapshot;
var _AssistantStream_currentRunStepSnapshot;
var _AssistantStream_addEvent;
var _AssistantStream_endRequest;
var _AssistantStream_handleMessage;
var _AssistantStream_handleRunStep;
var _AssistantStream_handleEvent;
var _AssistantStream_accumulateRunStep;
var _AssistantStream_accumulateMessage;
var _AssistantStream_accumulateContent;
var _AssistantStream_handleRun;
var AssistantStream = class extends EventStream {
constructor() {
super(...arguments);
_AssistantStream_instances.add(this);
_AssistantStream_events.set(this, []);
_AssistantStream_runStepSnapshots.set(this, {});
_AssistantStream_messageSnapshots.set(this, {});
_AssistantStream_messageSnapshot.set(this, void 0);
_AssistantStream_finalRun.set(this, void 0);
_AssistantStream_currentContentIndex.set(this, void 0);
_AssistantStream_currentContent.set(this, void 0);
_AssistantStream_currentToolCallIndex.set(this, void 0);
_AssistantStream_currentToolCall.set(this, void 0);
_AssistantStream_currentEvent.set(this, void 0);
_AssistantStream_currentRunSnapshot.set(this, void 0);
_AssistantStream_currentRunStepSnapshot.set(this, void 0);
}
[(_AssistantStream_events = /* @__PURE__ */ new WeakMap(), _AssistantStream_runStepSnapshots = /* @__PURE__ */ new WeakMap(), _AssistantStream_messageSnapshots = /* @__PURE__ */ new WeakMap(), _AssistantStream_messageSnapshot = /* @__PURE__ */ new WeakMap(), _AssistantStream_finalRun = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentContentIndex = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentContent = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentToolCallIndex = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentToolCall = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentEvent = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentRunSnapshot = /* @__PURE__ */ new WeakMap(), _AssistantStream_currentRunStepSnapshot = /* @__PURE__ */ new WeakMap(), _AssistantStream_ | |