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