@discomedia/utils 1.0.55 → 1.0.57

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.
package/dist/index.cjs CHANGED
@@ -1491,8 +1491,18 @@ class Queue {
1491
1491
  }
1492
1492
 
1493
1493
  function pLimit(concurrency) {
1494
+ let rejectOnClear = false;
1495
+
1496
+ if (typeof concurrency === 'object') {
1497
+ ({concurrency, rejectOnClear = false} = concurrency);
1498
+ }
1499
+
1494
1500
  validateConcurrency(concurrency);
1495
1501
 
1502
+ if (typeof rejectOnClear !== 'boolean') {
1503
+ throw new TypeError('Expected `rejectOnClear` to be a boolean');
1504
+ }
1505
+
1496
1506
  const queue = new Queue();
1497
1507
  let activeCount = 0;
1498
1508
 
@@ -1500,7 +1510,7 @@ function pLimit(concurrency) {
1500
1510
  // Process the next queued function if we're under the concurrency limit
1501
1511
  if (activeCount < concurrency && queue.size > 0) {
1502
1512
  activeCount++;
1503
- queue.dequeue()();
1513
+ queue.dequeue().run();
1504
1514
  }
1505
1515
  };
1506
1516
 
@@ -1527,11 +1537,14 @@ function pLimit(concurrency) {
1527
1537
  next();
1528
1538
  };
1529
1539
 
1530
- const enqueue = (function_, resolve, arguments_) => {
1540
+ const enqueue = (function_, resolve, reject, arguments_) => {
1541
+ const queueItem = {reject};
1542
+
1531
1543
  // Queue the internal resolve function instead of the run function
1532
1544
  // to preserve the asynchronous execution context.
1533
1545
  new Promise(internalResolve => { // eslint-disable-line promise/param-names
1534
- queue.enqueue(internalResolve);
1546
+ queueItem.run = internalResolve;
1547
+ queue.enqueue(queueItem);
1535
1548
  }).then(run.bind(undefined, function_, resolve, arguments_)); // eslint-disable-line promise/prefer-await-to-then
1536
1549
 
1537
1550
  // Start processing immediately if we haven't reached the concurrency limit
@@ -1540,8 +1553,8 @@ function pLimit(concurrency) {
1540
1553
  }
1541
1554
  };
1542
1555
 
1543
- const generator = (function_, ...arguments_) => new Promise(resolve => {
1544
- enqueue(function_, resolve, arguments_);
1556
+ const generator = (function_, ...arguments_) => new Promise((resolve, reject) => {
1557
+ enqueue(function_, resolve, reject, arguments_);
1545
1558
  });
1546
1559
 
1547
1560
  Object.defineProperties(generator, {
@@ -1553,7 +1566,16 @@ function pLimit(concurrency) {
1553
1566
  },
1554
1567
  clearQueue: {
1555
1568
  value() {
1556
- queue.clear();
1569
+ if (!rejectOnClear) {
1570
+ queue.clear();
1571
+ return;
1572
+ }
1573
+
1574
+ const abortError = AbortSignal.abort().reason;
1575
+
1576
+ while (queue.size > 0) {
1577
+ queue.dequeue().reject(abortError);
1578
+ }
1557
1579
  },
1558
1580
  },
1559
1581
  concurrency: {
@@ -2519,7 +2541,7 @@ const safeJSON = (text) => {
2519
2541
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2520
2542
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2521
2543
 
2522
- const VERSION = '6.16.0'; // x-release-please-version
2544
+ const VERSION = '6.17.0'; // x-release-please-version
2523
2545
 
2524
2546
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2525
2547
  const isRunningInBrowser = () => {
@@ -5812,12 +5834,7 @@ class Assistants extends APIResource {
5812
5834
  /**
5813
5835
  * Create an assistant with a model and instructions.
5814
5836
  *
5815
- * @example
5816
- * ```ts
5817
- * const assistant = await client.beta.assistants.create({
5818
- * model: 'gpt-4o',
5819
- * });
5820
- * ```
5837
+ * @deprecated
5821
5838
  */
5822
5839
  create(body, options) {
5823
5840
  return this._client.post('/assistants', {
@@ -5829,12 +5846,7 @@ class Assistants extends APIResource {
5829
5846
  /**
5830
5847
  * Retrieves an assistant.
5831
5848
  *
5832
- * @example
5833
- * ```ts
5834
- * const assistant = await client.beta.assistants.retrieve(
5835
- * 'assistant_id',
5836
- * );
5837
- * ```
5849
+ * @deprecated
5838
5850
  */
5839
5851
  retrieve(assistantID, options) {
5840
5852
  return this._client.get(path `/assistants/${assistantID}`, {
@@ -5845,12 +5857,7 @@ class Assistants extends APIResource {
5845
5857
  /**
5846
5858
  * Modifies an assistant.
5847
5859
  *
5848
- * @example
5849
- * ```ts
5850
- * const assistant = await client.beta.assistants.update(
5851
- * 'assistant_id',
5852
- * );
5853
- * ```
5860
+ * @deprecated
5854
5861
  */
5855
5862
  update(assistantID, body, options) {
5856
5863
  return this._client.post(path `/assistants/${assistantID}`, {
@@ -5862,13 +5869,7 @@ class Assistants extends APIResource {
5862
5869
  /**
5863
5870
  * Returns a list of assistants.
5864
5871
  *
5865
- * @example
5866
- * ```ts
5867
- * // Automatically fetches more pages as needed.
5868
- * for await (const assistant of client.beta.assistants.list()) {
5869
- * // ...
5870
- * }
5871
- * ```
5872
+ * @deprecated
5872
5873
  */
5873
5874
  list(query = {}, options) {
5874
5875
  return this._client.getAPIList('/assistants', (CursorPage), {
@@ -5880,11 +5881,7 @@ class Assistants extends APIResource {
5880
5881
  /**
5881
5882
  * Delete an assistant.
5882
5883
  *
5883
- * @example
5884
- * ```ts
5885
- * const assistantDeleted =
5886
- * await client.beta.assistants.delete('assistant_id');
5887
- * ```
5884
+ * @deprecated
5888
5885
  */
5889
5886
  delete(assistantID, options) {
5890
5887
  return this._client.delete(path `/assistants/${assistantID}`, {
@@ -7358,8 +7355,8 @@ Evals.Runs = Runs;
7358
7355
  let Files$1 = class Files extends APIResource {
7359
7356
  /**
7360
7357
  * Upload a file that can be used across various endpoints. Individual files can be
7361
- * up to 512 MB, and the size of all files uploaded by one organization can be up
7362
- * to 1 TB.
7358
+ * up to 512 MB, and each project can store up to 2.5 TB of files in total. There
7359
+ * is no organization-wide storage limit.
7363
7360
  *
7364
7361
  * - The Assistants API supports files up to 2 million tokens and of specific file
7365
7362
  * types. See the
@@ -18536,25 +18533,75 @@ class AlpacaTradingAPI {
18536
18533
  * @param side (string) - the side of the order
18537
18534
  * @param trailPercent100 (number) - the trail percent of the order (scale 100, i.e. 0.5 = 0.5%)
18538
18535
  * @param position_intent (string) - the position intent of the order
18536
+ * @param client_order_id (string) - optional client order id
18537
+ * @returns The created trailing stop order
18539
18538
  */
18540
- async createTrailingStop(symbol, qty, side, trailPercent100, position_intent) {
18539
+ async createTrailingStop(symbol, qty, side, trailPercent100, position_intent, client_order_id) {
18541
18540
  this.log(`Creating trailing stop ${side.toUpperCase()} ${qty} shares for ${symbol} with trail percent ${trailPercent100}%`, {
18542
18541
  symbol,
18543
18542
  });
18543
+ const body = {
18544
+ symbol,
18545
+ qty: Math.abs(qty),
18546
+ side,
18547
+ position_intent,
18548
+ order_class: 'simple',
18549
+ type: 'trailing_stop',
18550
+ trail_percent: trailPercent100, // Already in decimal form (e.g., 4 for 4%)
18551
+ time_in_force: 'gtc',
18552
+ };
18553
+ if (client_order_id !== undefined) {
18554
+ body.client_order_id = client_order_id;
18555
+ }
18544
18556
  try {
18545
- await this.makeRequest(`/orders`, 'POST', {
18557
+ return await this.makeRequest(`/orders`, 'POST', body);
18558
+ }
18559
+ catch (error) {
18560
+ this.log(`Error creating trailing stop: ${error}`, {
18546
18561
  symbol,
18547
- qty: Math.abs(qty),
18548
- side,
18549
- position_intent,
18550
- order_class: 'simple',
18551
- type: 'trailing_stop',
18552
- trail_percent: trailPercent100, // Already in decimal form (e.g., 4 for 4%)
18553
- time_in_force: 'gtc',
18562
+ type: 'error',
18554
18563
  });
18564
+ throw error;
18565
+ }
18566
+ }
18567
+ /**
18568
+ * Create a stop order (stop or stop-limit)
18569
+ * @param symbol (string) - the symbol of the order
18570
+ * @param qty (number) - the quantity of the order
18571
+ * @param side (string) - the side of the order
18572
+ * @param stopPrice (number) - the stop price that triggers the order
18573
+ * @param position_intent (string) - the position intent of the order
18574
+ * @param limitPrice (number) - optional limit price (if provided, creates a stop-limit order)
18575
+ * @param client_order_id (string) - optional client order id
18576
+ * @returns The created stop order
18577
+ */
18578
+ async createStopOrder(symbol, qty, side, stopPrice, position_intent, limitPrice, client_order_id) {
18579
+ const isStopLimit = limitPrice !== undefined;
18580
+ const orderType = isStopLimit ? 'stop-limit' : 'stop';
18581
+ this.log(`Creating ${orderType} ${side.toUpperCase()} ${qty} shares for ${symbol} with stop price ${stopPrice}${isStopLimit ? ` and limit price ${limitPrice}` : ''}`, {
18582
+ symbol,
18583
+ });
18584
+ const body = {
18585
+ symbol,
18586
+ qty: Math.abs(qty).toString(),
18587
+ side,
18588
+ position_intent,
18589
+ order_class: 'simple',
18590
+ type: isStopLimit ? 'stop_limit' : 'stop',
18591
+ stop_price: this.roundPriceForAlpaca(stopPrice),
18592
+ time_in_force: 'gtc',
18593
+ };
18594
+ if (isStopLimit) {
18595
+ body.limit_price = this.roundPriceForAlpaca(limitPrice);
18596
+ }
18597
+ if (client_order_id !== undefined) {
18598
+ body.client_order_id = client_order_id;
18599
+ }
18600
+ try {
18601
+ return await this.makeRequest(`/orders`, 'POST', body);
18555
18602
  }
18556
18603
  catch (error) {
18557
- this.log(`Error creating trailing stop: ${error}`, {
18604
+ this.log(`Error creating ${orderType} order: ${error}`, {
18558
18605
  symbol,
18559
18606
  type: 'error',
18560
18607
  });
@@ -18673,6 +18720,57 @@ class AlpacaTradingAPI {
18673
18720
  throw error;
18674
18721
  }
18675
18722
  }
18723
+ /**
18724
+ * Create an OCO (One-Cancels-Other) order with take profit and stop loss
18725
+ * @param symbol (string) - the symbol of the order
18726
+ * @param qty (number) - the quantity of the order
18727
+ * @param side (string) - the side of the order (buy or sell)
18728
+ * @param position_intent (string) - the position intent of the order
18729
+ * @param limitPrice (number) - the limit price for the entry order (OCO orders must be limit orders)
18730
+ * @param takeProfitPrice (number) - the take profit price
18731
+ * @param stopLossPrice (number) - the stop loss price
18732
+ * @param stopLossLimitPrice (number) - optional limit price for stop loss (creates stop-limit instead of stop)
18733
+ * @param client_order_id (string) - optional client order id
18734
+ * @returns The created OCO order
18735
+ */
18736
+ async createOCOOrder(symbol, qty, side, position_intent, limitPrice, takeProfitPrice, stopLossPrice, stopLossLimitPrice, client_order_id) {
18737
+ this.log(`Creating OCO order ${side.toUpperCase()} ${qty} shares for ${symbol} at limit ${limitPrice} with take profit ${takeProfitPrice} and stop loss ${stopLossPrice}`, {
18738
+ symbol,
18739
+ });
18740
+ const body = {
18741
+ symbol,
18742
+ qty: Math.abs(qty).toString(),
18743
+ side,
18744
+ position_intent,
18745
+ order_class: 'oco',
18746
+ type: 'limit',
18747
+ limit_price: this.roundPriceForAlpaca(limitPrice),
18748
+ time_in_force: 'gtc',
18749
+ take_profit: {
18750
+ limit_price: this.roundPriceForAlpaca(takeProfitPrice),
18751
+ },
18752
+ stop_loss: {
18753
+ stop_price: this.roundPriceForAlpaca(stopLossPrice),
18754
+ },
18755
+ };
18756
+ // If stop loss limit price is provided, create stop-limit order
18757
+ if (stopLossLimitPrice !== undefined) {
18758
+ body.stop_loss.limit_price = this.roundPriceForAlpaca(stopLossLimitPrice);
18759
+ }
18760
+ if (client_order_id !== undefined) {
18761
+ body.client_order_id = client_order_id;
18762
+ }
18763
+ try {
18764
+ return await this.makeRequest(`/orders`, 'POST', body);
18765
+ }
18766
+ catch (error) {
18767
+ this.log(`Error creating OCO order: ${error}`, {
18768
+ symbol,
18769
+ type: 'error',
18770
+ });
18771
+ throw error;
18772
+ }
18773
+ }
18676
18774
  /**
18677
18775
  * Get the current trail percent for a symbol, assuming that it has an open position and a trailing stop order to close it. Because this relies on an orders request for one symbol, you can't do it too often.
18678
18776
  * @param symbol (string) - the symbol of the order