@discomedia/utils 1.0.15 → 1.0.17

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
@@ -388,16 +388,24 @@ class MarketTimeCalculator {
388
388
  /**
389
389
  * Get the last full trading date
390
390
  */
391
- getLastFullTradingDate(currentDate = new Date()) {
391
+ getLastFullTradingDate(currentDate = new Date(), extendedHours = true) {
392
392
  const nowET = dateFnsTz.toZonedTime(currentDate, this.timezone);
393
- // If today is a market day and we're after extended hours close, return today
394
393
  if (this.calendar.isMarketDay(nowET)) {
395
394
  const timeInMinutes = nowET.getHours() * 60 + nowET.getMinutes();
396
- let extendedEndMinutes = MARKET_CONFIG.TIMES.EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EXTENDED_END.minute;
397
- if (this.calendar.isEarlyCloseDay(nowET)) {
398
- extendedEndMinutes = MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.minute;
395
+ let endMinutes;
396
+ if (extendedHours) {
397
+ endMinutes = MARKET_CONFIG.TIMES.EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EXTENDED_END.minute;
398
+ if (this.calendar.isEarlyCloseDay(nowET)) {
399
+ endMinutes = MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.hour * 60 + MARKET_CONFIG.TIMES.EARLY_EXTENDED_END.minute;
400
+ }
399
401
  }
400
- if (timeInMinutes >= extendedEndMinutes) {
402
+ else {
403
+ endMinutes = MARKET_CONFIG.TIMES.MARKET_CLOSE.hour * 60 + MARKET_CONFIG.TIMES.MARKET_CLOSE.minute;
404
+ if (this.calendar.isEarlyCloseDay(nowET)) {
405
+ endMinutes = MARKET_CONFIG.TIMES.EARLY_CLOSE.hour * 60 + MARKET_CONFIG.TIMES.EARLY_CLOSE.minute;
406
+ }
407
+ }
408
+ if (timeInMinutes >= endMinutes) {
401
409
  return dateFnsTz.fromZonedTime(dateFns.set(nowET, { hours: 0, minutes: 0, seconds: 0, milliseconds: 0 }), this.timezone);
402
410
  }
403
411
  }
@@ -768,6 +776,12 @@ function getMarketStatus(options = {}) {
768
776
  function isMarketDay(date) {
769
777
  return marketCalendar.isMarketDay(date);
770
778
  }
779
+ /**
780
+ * Check if a date is within market hours
781
+ */
782
+ function isWithinMarketHours(date, intradayReporting = 'market_hours') {
783
+ return marketTimeCalculator.isWithinMarketHours(date, intradayReporting);
784
+ }
771
785
  /**
772
786
  * Function to find complete trading date periods, starting at the beginning of one trading date and ending at the last.
773
787
  * By default, it gets the last trading date, returning the beginning and end. But we can also a) define the end date
@@ -780,9 +794,16 @@ function isMarketDay(date) {
780
794
  function getTradingStartAndEndDates(options = {}) {
781
795
  const { endDate = new Date(), days = 1 } = options;
782
796
  // Get the last full trading date for the end date
783
- const endTradingDate = getLastFullTradingDate(endDate).date;
784
- // Get the market close time for the end date (4:00 PM ET or 1:00 PM on short days)
785
- const endMarketClose = getMarketOpenClose({ date: endTradingDate }).close;
797
+ let endTradingDate;
798
+ // If within market hours, on a trading day, use the current date as end
799
+ if (isWithinMarketHours(endDate, 'market_hours')) {
800
+ endTradingDate = endDate;
801
+ }
802
+ else {
803
+ // If after market hours, use the last full trading date, which should be the previous trading day, or after extended hours, the same trading day
804
+ const lastFullTradingDate = marketTimeCalculator.getLastFullTradingDate(endDate, false);
805
+ endTradingDate = getMarketOpenClose({ date: lastFullTradingDate }).close;
806
+ }
786
807
  let startDate;
787
808
  if (days <= 1) {
788
809
  // For 1 day, start is market open of the same trading day as end
@@ -802,7 +823,7 @@ function getTradingStartAndEndDates(options = {}) {
802
823
  // Get the market open time for the start date
803
824
  startDate = getMarketOpenClose({ date: currentDate }).open;
804
825
  }
805
- return { startDate, endDate: endMarketClose };
826
+ return { startDate, endDate: endTradingDate };
806
827
  }
807
828
  // Export the MARKET_TIMES constant for backward compatibility
808
829
  const MARKET_TIMES = {
@@ -9401,7 +9422,7 @@ const makeResponsesAPICall = async (input, options = {}) => {
9401
9422
  * });
9402
9423
  */
9403
9424
  async function makeLLMCall(input, options = {}) {
9404
- const { apiKey, model = DEFAULT_MODEL$1, responseFormat = 'text', tools, useCodeInterpreter = false, useWebSearch = false, imageBase64, imageDetail = 'high', context } = options;
9425
+ const { apiKey, model = DEFAULT_MODEL$1, responseFormat = 'text', tools, useCodeInterpreter = false, useWebSearch = false, imageBase64, imageDetail = 'high', context, } = options;
9405
9426
  // Validate model
9406
9427
  const normalizedModel = normalizeModelName(model);
9407
9428
  if (!isSupportedModel(normalizedModel)) {
@@ -9417,7 +9438,7 @@ async function makeLLMCall(input, options = {}) {
9417
9438
  conversationMessages.push({
9418
9439
  role: contextMsg.role,
9419
9440
  content: contextMsg.content,
9420
- type: 'message'
9441
+ type: 'message',
9421
9442
  });
9422
9443
  }
9423
9444
  // Add current input message
@@ -9431,9 +9452,9 @@ async function makeLLMCall(input, options = {}) {
9431
9452
  type: 'input_image',
9432
9453
  detail: imageDetail,
9433
9454
  image_url: imageBase64.startsWith('data:') ? imageBase64 : `data:image/webp;base64,${imageBase64}`,
9434
- }
9455
+ },
9435
9456
  ],
9436
- type: 'message'
9457
+ type: 'message',
9437
9458
  });
9438
9459
  }
9439
9460
  else {
@@ -9441,7 +9462,7 @@ async function makeLLMCall(input, options = {}) {
9441
9462
  conversationMessages.push({
9442
9463
  role: 'user',
9443
9464
  content: input,
9444
- type: 'message'
9465
+ type: 'message',
9445
9466
  });
9446
9467
  }
9447
9468
  processedInput = conversationMessages;
@@ -9457,10 +9478,10 @@ async function makeLLMCall(input, options = {}) {
9457
9478
  type: 'input_image',
9458
9479
  detail: imageDetail,
9459
9480
  image_url: imageBase64.startsWith('data:') ? imageBase64 : `data:image/webp;base64,${imageBase64}`,
9460
- }
9481
+ },
9461
9482
  ],
9462
- type: 'message'
9463
- }
9483
+ type: 'message',
9484
+ },
9464
9485
  ];
9465
9486
  }
9466
9487
  else {