@discomedia/utils 1.0.64 → 1.0.66

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ function isOpenRouterModel(model) {
7
7
  const openRouterModels = [
8
8
  'openai/gpt-5',
9
9
  'openai/gpt-5-mini',
10
+ 'openai/gpt-5.4-mini',
10
11
  'openai/gpt-5-nano',
11
12
  'openai/gpt-5.1',
12
13
  'openai/gpt-5.4',
@@ -210,6 +211,37 @@ class InvalidWebhookSignatureError extends Error {
210
211
  super(message);
211
212
  }
212
213
  }
214
+ /**
215
+ * Error thrown by the API server during OAuth token exchange.
216
+ * Can have status codes 400, 401, or 403.
217
+ * Other status codes from OAuth endpoints are raised as normal APIError types.
218
+ */
219
+ class OAuthError extends APIError {
220
+ constructor(status, error, headers) {
221
+ let finalMessage = 'OAuth2 authentication error';
222
+ let error_code = undefined;
223
+ if (error && typeof error === 'object') {
224
+ const errorData = error;
225
+ error_code = errorData['error'];
226
+ const description = errorData['error_description'];
227
+ if (description && typeof description === 'string') {
228
+ finalMessage = description;
229
+ }
230
+ else if (error_code) {
231
+ finalMessage = error_code;
232
+ }
233
+ }
234
+ super(status, error, finalMessage, headers);
235
+ this.error_code = error_code;
236
+ }
237
+ }
238
+ class SubjectTokenProviderError extends OpenAIError {
239
+ constructor(message, provider, cause) {
240
+ super(message);
241
+ this.provider = provider;
242
+ this.cause = cause;
243
+ }
244
+ }
213
245
 
214
246
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
215
247
  // https://url.spec.whatwg.org/#url-scheme-string
@@ -262,7 +294,7 @@ const safeJSON = (text) => {
262
294
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
263
295
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
264
296
 
265
- const VERSION = '6.31.0'; // x-release-please-version
297
+ const VERSION = '6.34.0'; // x-release-please-version
266
298
 
267
299
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
268
300
  const isRunningInBrowser = () => {
@@ -1643,6 +1675,91 @@ class ConversationCursorPage extends AbstractPage {
1643
1675
  }
1644
1676
  }
1645
1677
 
1678
+ const SUBJECT_TOKEN_TYPES = {
1679
+ jwt: 'urn:ietf:params:oauth:token-type:jwt',
1680
+ id: 'urn:ietf:params:oauth:token-type:id_token',
1681
+ };
1682
+ const TOKEN_EXCHANGE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:token-exchange';
1683
+ class WorkloadIdentityAuth {
1684
+ constructor(config, fetch) {
1685
+ this.cachedToken = null;
1686
+ this.refreshPromise = null;
1687
+ this.tokenExchangeUrl = 'https://auth.openai.com/oauth/token';
1688
+ this.config = config;
1689
+ this.fetch = fetch ?? getDefaultFetch();
1690
+ }
1691
+ async getToken() {
1692
+ if (!this.cachedToken || this.isTokenExpired(this.cachedToken)) {
1693
+ if (this.refreshPromise) {
1694
+ return await this.refreshPromise;
1695
+ }
1696
+ this.refreshPromise = this.refreshToken();
1697
+ try {
1698
+ const token = await this.refreshPromise;
1699
+ return token;
1700
+ }
1701
+ finally {
1702
+ this.refreshPromise = null;
1703
+ }
1704
+ }
1705
+ if (this.needsRefresh(this.cachedToken) && !this.refreshPromise) {
1706
+ this.refreshPromise = this.refreshToken().finally(() => {
1707
+ this.refreshPromise = null;
1708
+ });
1709
+ }
1710
+ return this.cachedToken.token;
1711
+ }
1712
+ async refreshToken() {
1713
+ const subjectToken = await this.config.provider.getToken();
1714
+ const response = await this.fetch(this.tokenExchangeUrl, {
1715
+ method: 'POST',
1716
+ headers: {
1717
+ 'Content-Type': 'application/json',
1718
+ },
1719
+ body: JSON.stringify({
1720
+ grant_type: TOKEN_EXCHANGE_GRANT_TYPE,
1721
+ client_id: this.config.clientId,
1722
+ subject_token: subjectToken,
1723
+ subject_token_type: SUBJECT_TOKEN_TYPES[this.config.provider.tokenType],
1724
+ identity_provider_id: this.config.identityProviderId,
1725
+ service_account_id: this.config.serviceAccountId,
1726
+ }),
1727
+ });
1728
+ if (!response.ok) {
1729
+ const errorText = await response.text();
1730
+ let body = undefined;
1731
+ try {
1732
+ body = JSON.parse(errorText);
1733
+ }
1734
+ catch { }
1735
+ if (response.status === 400 || response.status === 401 || response.status === 403) {
1736
+ throw new OAuthError(response.status, body, response.headers);
1737
+ }
1738
+ throw APIError.generate(response.status, body, `Token exchange failed with status ${response.status}`, response.headers);
1739
+ }
1740
+ const tokenResponse = (await response.json());
1741
+ const expiresIn = tokenResponse.expires_in || 3600;
1742
+ const expiresAt = Date.now() + expiresIn * 1000;
1743
+ this.cachedToken = {
1744
+ token: tokenResponse.access_token,
1745
+ expiresAt,
1746
+ };
1747
+ return tokenResponse.access_token;
1748
+ }
1749
+ isTokenExpired(cachedToken) {
1750
+ return Date.now() >= cachedToken.expiresAt;
1751
+ }
1752
+ needsRefresh(cachedToken) {
1753
+ const bufferSeconds = this.config.refreshBufferSeconds ?? 1200;
1754
+ const bufferMs = bufferSeconds * 1000;
1755
+ return Date.now() >= cachedToken.expiresAt - bufferMs;
1756
+ }
1757
+ invalidateToken() {
1758
+ this.cachedToken = null;
1759
+ this.refreshPromise = null;
1760
+ }
1761
+ }
1762
+
1646
1763
  const checkFileSupport = () => {
1647
1764
  if (typeof File === 'undefined') {
1648
1765
  const { process } = globalThis;
@@ -6948,6 +7065,7 @@ _Webhooks_instances = new WeakSet(), _Webhooks_validateSecret = function _Webhoo
6948
7065
 
6949
7066
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
6950
7067
  var _OpenAI_instances, _a$1, _OpenAI_encoder, _OpenAI_baseURLOverridden;
7068
+ const WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER = 'workload-identity-auth';
6951
7069
  /**
6952
7070
  * API Client for interfacing with the OpenAI API.
6953
7071
  */
@@ -6968,7 +7086,7 @@ class OpenAI {
6968
7086
  * @param {Record<string, string | undefined>} opts.defaultQuery - Default query parameters to include with every request to the API.
6969
7087
  * @param {boolean} [opts.dangerouslyAllowBrowser=false] - By default, client-side use of this library is not allowed, as it risks exposing your secret API credentials to attackers.
6970
7088
  */
6971
- constructor({ baseURL = readEnv('OPENAI_BASE_URL'), apiKey = readEnv('OPENAI_API_KEY'), organization = readEnv('OPENAI_ORG_ID') ?? null, project = readEnv('OPENAI_PROJECT_ID') ?? null, webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null, ...opts } = {}) {
7089
+ constructor({ baseURL = readEnv('OPENAI_BASE_URL'), apiKey = readEnv('OPENAI_API_KEY'), organization = readEnv('OPENAI_ORG_ID') ?? null, project = readEnv('OPENAI_PROJECT_ID') ?? null, webhookSecret = readEnv('OPENAI_WEBHOOK_SECRET') ?? null, workloadIdentity, ...opts } = {}) {
6972
7090
  _OpenAI_instances.add(this);
6973
7091
  _OpenAI_encoder.set(this, void 0);
6974
7092
  /**
@@ -7023,14 +7141,21 @@ class OpenAI {
7023
7141
  this.containers = new Containers(this);
7024
7142
  this.skills = new Skills(this);
7025
7143
  this.videos = new Videos(this);
7026
- if (apiKey === undefined) {
7027
- throw new OpenAIError('Missing credentials. Please pass an `apiKey`, or set the `OPENAI_API_KEY` environment variable.');
7144
+ if (workloadIdentity) {
7145
+ if (apiKey && apiKey !== WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER) {
7146
+ throw new OpenAIError('The `apiKey` and `workloadIdentity` arguments are mutually exclusive; only one can be passed at a time.');
7147
+ }
7148
+ apiKey = WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER;
7149
+ }
7150
+ else if (apiKey === undefined) {
7151
+ throw new OpenAIError('Missing credentials. Please pass an `apiKey`, `workloadIdentity`, or set the `OPENAI_API_KEY` environment variable.');
7028
7152
  }
7029
7153
  const options = {
7030
7154
  apiKey,
7031
7155
  organization,
7032
7156
  project,
7033
7157
  webhookSecret,
7158
+ workloadIdentity,
7034
7159
  ...opts,
7035
7160
  baseURL: baseURL || `https://api.openai.com/v1`,
7036
7161
  };
@@ -7052,6 +7177,9 @@ class OpenAI {
7052
7177
  this.fetch = options.fetch ?? getDefaultFetch();
7053
7178
  __classPrivateFieldSet(this, _OpenAI_encoder, FallbackEncoder);
7054
7179
  this._options = options;
7180
+ if (workloadIdentity) {
7181
+ this._workloadIdentityAuth = new WorkloadIdentityAuth(workloadIdentity, this.fetch);
7182
+ }
7055
7183
  this.apiKey = typeof apiKey === 'string' ? apiKey : 'Missing Key';
7056
7184
  this.organization = organization;
7057
7185
  this.project = project;
@@ -7071,6 +7199,7 @@ class OpenAI {
7071
7199
  fetch: this.fetch,
7072
7200
  fetchOptions: this.fetchOptions,
7073
7201
  apiKey: this.apiKey,
7202
+ workloadIdentity: this._options.workloadIdentity,
7074
7203
  organization: this.organization,
7075
7204
  project: this.project,
7076
7205
  webhookSecret: this.webhookSecret,
@@ -7197,7 +7326,7 @@ class OpenAI {
7197
7326
  throw new APIUserAbortError();
7198
7327
  }
7199
7328
  const controller = new AbortController();
7200
- const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
7329
+ const response = await this.fetchWithAuth(url, req, timeout, controller).catch(castToError);
7201
7330
  const headersTime = Date.now();
7202
7331
  if (response instanceof globalThis.Error) {
7203
7332
  const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
@@ -7227,6 +7356,9 @@ class OpenAI {
7227
7356
  durationMs: headersTime - startTime,
7228
7357
  message: response.message,
7229
7358
  }));
7359
+ if (response instanceof OAuthError || response instanceof SubjectTokenProviderError) {
7360
+ throw response;
7361
+ }
7230
7362
  if (isTimeout) {
7231
7363
  throw new APIConnectionTimeoutError();
7232
7364
  }
@@ -7238,6 +7370,20 @@ class OpenAI {
7238
7370
  .join('');
7239
7371
  const responseInfo = `[${requestLogID}${retryLogStr}${specialHeaders}] ${req.method} ${url} ${response.ok ? 'succeeded' : 'failed'} with status ${response.status} in ${headersTime - startTime}ms`;
7240
7372
  if (!response.ok) {
7373
+ if (response.status === 401 &&
7374
+ this._workloadIdentityAuth &&
7375
+ !options.__metadata?.['hasStreamingBody'] &&
7376
+ !options.__metadata?.['workloadIdentityTokenRefreshed']) {
7377
+ await CancelReadableStream(response.body);
7378
+ this._workloadIdentityAuth.invalidateToken();
7379
+ return this.makeRequest({
7380
+ ...options,
7381
+ __metadata: {
7382
+ ...options.__metadata,
7383
+ workloadIdentityTokenRefreshed: true,
7384
+ },
7385
+ }, retriesRemaining, retryOfRequestLogID ?? requestLogID);
7386
+ }
7241
7387
  const shouldRetry = await this.shouldRetry(response);
7242
7388
  if (retriesRemaining && shouldRetry) {
7243
7389
  const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
@@ -7288,6 +7434,18 @@ class OpenAI {
7288
7434
  const request = this.makeRequest(options, null, undefined);
7289
7435
  return new PagePromise(this, request, Page);
7290
7436
  }
7437
+ async fetchWithAuth(url, init, timeout, controller) {
7438
+ if (this._workloadIdentityAuth) {
7439
+ const headers = init.headers;
7440
+ const authHeader = headers.get('Authorization');
7441
+ if (!authHeader || authHeader === `Bearer ${WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER}`) {
7442
+ const token = await this._workloadIdentityAuth.getToken();
7443
+ headers.set('Authorization', `Bearer ${token}`);
7444
+ }
7445
+ }
7446
+ const response = await this.fetchWithTimeout(url, init, timeout, controller);
7447
+ return response;
7448
+ }
7291
7449
  async fetchWithTimeout(url, init, ms, controller) {
7292
7450
  const { signal, method, ...options } = init || {};
7293
7451
  const abort = this._makeAbort(controller);
@@ -7384,7 +7542,13 @@ class OpenAI {
7384
7542
  if ('timeout' in options)
7385
7543
  validatePositiveInteger('timeout', options.timeout);
7386
7544
  options.timeout = options.timeout ?? this.timeout;
7387
- const { bodyHeaders, body } = this.buildBody({ options });
7545
+ const { bodyHeaders, body, isStreamingBody } = this.buildBody({ options });
7546
+ if (isStreamingBody) {
7547
+ inputOptions.__metadata = {
7548
+ ...inputOptions.__metadata,
7549
+ hasStreamingBody: true,
7550
+ };
7551
+ }
7388
7552
  const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
7389
7553
  const req = {
7390
7554
  method,
@@ -7431,9 +7595,18 @@ class OpenAI {
7431
7595
  }
7432
7596
  buildBody({ options: { body, headers: rawHeaders } }) {
7433
7597
  if (!body) {
7434
- return { bodyHeaders: undefined, body: undefined };
7598
+ return { bodyHeaders: undefined, body: undefined, isStreamingBody: false };
7435
7599
  }
7436
7600
  const headers = buildHeaders([rawHeaders]);
7601
+ const isReadableStream = typeof globalThis.ReadableStream !== 'undefined' &&
7602
+ body instanceof globalThis.ReadableStream;
7603
+ const isRetryableBody = !isReadableStream &&
7604
+ (typeof body === 'string' ||
7605
+ body instanceof ArrayBuffer ||
7606
+ ArrayBuffer.isView(body) ||
7607
+ (typeof globalThis.Blob !== 'undefined' && body instanceof globalThis.Blob) ||
7608
+ body instanceof URLSearchParams ||
7609
+ body instanceof FormData);
7437
7610
  if (
7438
7611
  // Pass raw type verbatim
7439
7612
  ArrayBuffer.isView(body) ||
@@ -7449,23 +7622,28 @@ class OpenAI {
7449
7622
  // `URLSearchParams` -> `application/x-www-form-urlencoded`
7450
7623
  body instanceof URLSearchParams ||
7451
7624
  // Send chunked stream (each chunk has own `length`)
7452
- (globalThis.ReadableStream && body instanceof globalThis.ReadableStream)) {
7453
- return { bodyHeaders: undefined, body: body };
7625
+ isReadableStream) {
7626
+ return { bodyHeaders: undefined, body: body, isStreamingBody: !isRetryableBody };
7454
7627
  }
7455
7628
  else if (typeof body === 'object' &&
7456
7629
  (Symbol.asyncIterator in body ||
7457
7630
  (Symbol.iterator in body && 'next' in body && typeof body.next === 'function'))) {
7458
- return { bodyHeaders: undefined, body: ReadableStreamFrom(body) };
7631
+ return {
7632
+ bodyHeaders: undefined,
7633
+ body: ReadableStreamFrom(body),
7634
+ isStreamingBody: true,
7635
+ };
7459
7636
  }
7460
7637
  else if (typeof body === 'object' &&
7461
7638
  headers.values.get('content-type') === 'application/x-www-form-urlencoded') {
7462
7639
  return {
7463
7640
  bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
7464
7641
  body: this.stringifyQuery(body),
7642
+ isStreamingBody: false,
7465
7643
  };
7466
7644
  }
7467
7645
  else {
7468
- return __classPrivateFieldGet(this, _OpenAI_encoder, "f").call(this, { body, headers });
7646
+ return { ...__classPrivateFieldGet(this, _OpenAI_encoder, "f").call(this, { body, headers }), isStreamingBody: false };
7469
7647
  }
7470
7648
  }
7471
7649
  }
@@ -10348,6 +10526,11 @@ const openAiModelCosts = {
10348
10526
  cacheHitCost: 0.025 / 1_000_000,
10349
10527
  outputCost: 2 / 1_000_000,
10350
10528
  },
10529
+ 'gpt-5.4-mini': {
10530
+ inputCost: 0.25 / 1_000_000,
10531
+ cacheHitCost: 0.025 / 1_000_000,
10532
+ outputCost: 2 / 1_000_000,
10533
+ },
10351
10534
  'gpt-5-nano': {
10352
10535
  inputCost: 0.05 / 1_000_000,
10353
10536
  cacheHitCost: 0.005 / 1_000_000,
@@ -10858,6 +11041,7 @@ const isSupportedModel = (model) => {
10858
11041
  'gpt-4.1-nano',
10859
11042
  'gpt-5',
10860
11043
  'gpt-5-mini',
11044
+ 'gpt-5.4-mini',
10861
11045
  'gpt-5-nano',
10862
11046
  'gpt-5.1',
10863
11047
  'gpt-5.4',
@@ -10886,6 +11070,7 @@ function supportsTemperature(model) {
10886
11070
  'o3',
10887
11071
  'gpt-5',
10888
11072
  'gpt-5-mini',
11073
+ 'gpt-5.4-mini',
10889
11074
  'gpt-5-nano',
10890
11075
  'gpt-5.1',
10891
11076
  'gpt-5.4',
@@ -10915,6 +11100,7 @@ function isGPT5Model(model) {
10915
11100
  const gpt5Models = [
10916
11101
  'gpt-5',
10917
11102
  'gpt-5-mini',
11103
+ 'gpt-5.4-mini',
10918
11104
  'gpt-5-nano',
10919
11105
  'gpt-5.1',
10920
11106
  'gpt-5.4',
@@ -11224,6 +11410,7 @@ const MULTIMODAL_VISION_MODELS = new Set([
11224
11410
  'gpt-4o',
11225
11411
  'gpt-5',
11226
11412
  'gpt-5-mini',
11413
+ 'gpt-5.4-mini',
11227
11414
  'gpt-5-nano',
11228
11415
  'gpt-5.1',
11229
11416
  'gpt-5.4',