@recombine-ai/engine 0.3.1 → 0.3.2

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/build/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AIEngine } from './lib/ai';
2
2
  export { AIEngine } from './lib/ai';
3
3
  export declare const createAIEngine: typeof AIEngine.createAIEngine;
4
- export { delayFactory, Schedule } from './lib/schedule';
4
+ export { delayFactory, Schedule, createScheduleQuery } from './lib/schedule';
5
5
  export { Scheduler, Logger } from './lib/interfaces';
6
6
  export * from './lib/bosun';
7
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC,eAAO,MAAM,cAAc,gCAA0B,CAAA;AAErD,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAEvD,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEpD,cAAc,aAAa,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAEnC,eAAO,MAAM,cAAc,gCAA0B,CAAA;AAErD,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AAE5E,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAEpD,cAAc,aAAa,CAAA"}
package/build/index.js CHANGED
@@ -14,11 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.delayFactory = exports.createAIEngine = exports.AIEngine = void 0;
17
+ exports.createScheduleQuery = exports.delayFactory = exports.createAIEngine = exports.AIEngine = void 0;
18
18
  const ai_1 = require("./lib/ai");
19
19
  var ai_2 = require("./lib/ai");
20
20
  Object.defineProperty(exports, "AIEngine", { enumerable: true, get: function () { return ai_2.AIEngine; } });
21
21
  exports.createAIEngine = ai_1.AIEngine.createAIEngine;
22
22
  var schedule_1 = require("./lib/schedule");
23
23
  Object.defineProperty(exports, "delayFactory", { enumerable: true, get: function () { return schedule_1.delayFactory; } });
24
+ Object.defineProperty(exports, "createScheduleQuery", { enumerable: true, get: function () { return schedule_1.createScheduleQuery; } });
24
25
  __exportStar(require("./lib/bosun"), exports);
@@ -3,6 +3,29 @@
3
3
  * - note that Delay must not be negative
4
4
  */
5
5
  export declare function delayFactory(schedule: Schedule): (delay: Delay) => Date;
6
+ /**
7
+ * Query over an object that follows {@link Schedule} interface
8
+ */
9
+ export declare class ScheduleQuery {
10
+ private schedule;
11
+ constructor(schedule: Schedule);
12
+ /**
13
+ * Find next closest date after specified delay within the schedule
14
+ */
15
+ nextValidDate(delay: Delay): Date;
16
+ /**
17
+ * Finds next closest date that follows a specified date within the schedule
18
+ */
19
+ nextValidDate(date: Date): Date;
20
+ /** Returns next date the schedule starting from _now_ */
21
+ next(): Date;
22
+ /**
23
+ * Checks if a specified date is within the schedule,
24
+ * if no date provided, checks if _now_ is within the schedule
25
+ */
26
+ isValid(date: Date): boolean;
27
+ }
28
+ export declare function createScheduleQuery(schedule: Schedule): ScheduleQuery;
6
29
  type Delay = [number, 'minutes' | 'hours' | 'days'] | [number, 'hours', number, 'minutes'] | [number, 'days', number, 'hours'] | [number, 'days', number, 'hours', number, 'minutes'];
7
30
  type singleInt = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |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@@ -1,37 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScheduleQuery = void 0;
3
4
  exports.delayFactory = delayFactory;
5
+ exports.createScheduleQuery = createScheduleQuery;
4
6
  /**
5
7
  * @returns getNextTimePoint function that makes a date from a given {@link Delay}
6
8
  * - note that Delay must not be negative
7
9
  */
8
10
  function delayFactory(schedule) {
9
11
  return function getNextTimePoint(delay) {
10
- const date = new Date();
11
- const { holidays, timeZone, workingHours } = schedule;
12
- const delayMs = getDelayTimeInMs(delay);
13
- const [_, offsetDiffBefore] = convertToTimeZone(date, timeZone);
12
+ return createScheduleQuery(schedule).nextValidDate(delay);
13
+ };
14
+ }
15
+ /**
16
+ * Query over an object that follows {@link Schedule} interface
17
+ */
18
+ class ScheduleQuery {
19
+ schedule;
20
+ constructor(schedule) {
21
+ this.schedule = schedule;
22
+ }
23
+ nextValidDate(dateOrDelay) {
24
+ const date = dateOrDelay instanceof Date ? dateOrDelay : new Date();
25
+ const [_, offsetDiffBefore] = convertToTimeZone(date, this.schedule.timeZone);
14
26
  const localOffsetBefore = date.getTimezoneOffset() * 60 * 1000;
15
- date.setTime(date.getTime() + delayMs);
16
- let [targetTzDate, offsetDiff] = convertToTimeZone(date, timeZone);
27
+ if (!(dateOrDelay instanceof Date)) {
28
+ const delayMs = getDelayTimeInMs(dateOrDelay);
29
+ date.setTime(date.getTime() + delayMs);
30
+ }
31
+ let [targetTzDate, offsetDiff] = convertToTimeZone(date, this.schedule.timeZone);
17
32
  const localOffsetAfterMs = targetTzDate.getTimezoneOffset() * 60 * 1000;
18
33
  const localOffsetChange = localOffsetAfterMs - localOffsetBefore;
19
34
  const targetTzOffsetChange = offsetDiff - offsetDiffBefore;
20
- if (workingHours && workingHours?.length > 0) {
21
- targetTzDate = getNextWorkingDate(targetTzDate, workingHours);
22
- }
23
- if (holidays && isHoliday(targetTzDate, holidays)) {
24
- targetTzDate = skipHoliday(targetTzDate, holidays);
35
+ if (this.schedule.workingHours && this.schedule.workingHours?.length > 0) {
36
+ targetTzDate = getNextWorkingDate(targetTzDate, this.schedule);
25
37
  }
26
38
  const targetDate = new Date(targetTzDate.getTime() + offsetDiff + targetTzOffsetChange + localOffsetChange);
27
- const [, offsetDiffTz] = convertToTimeZone(targetDate, timeZone);
39
+ const [, offsetDiffTz] = convertToTimeZone(targetDate, this.schedule.timeZone);
28
40
  const targetOffsetTzChange = offsetDiffTz - offsetDiff;
29
41
  return new Date(targetTzDate.getTime() +
30
42
  offsetDiff +
31
43
  targetTzOffsetChange +
32
44
  localOffsetChange +
33
45
  targetOffsetTzChange);
34
- };
46
+ }
47
+ /** Returns next date the schedule starting from _now_ */
48
+ next() {
49
+ return this.nextValidDate(new Date());
50
+ }
51
+ /**
52
+ * Checks if a specified date is within the schedule,
53
+ * if no date provided, checks if _now_ is within the schedule
54
+ */
55
+ isValid(date) {
56
+ const next = this.nextValidDate(date);
57
+ return date.toISOString() === next.toISOString();
58
+ }
59
+ }
60
+ exports.ScheduleQuery = ScheduleQuery;
61
+ function createScheduleQuery(schedule) {
62
+ return new ScheduleQuery(schedule);
35
63
  }
36
64
  function getDelayTimeInMs(time) {
37
65
  const minutesIndex = time.findIndex((el) => el === 'minutes');
@@ -50,12 +78,13 @@ function convertToTimeZone(date, timeZone) {
50
78
  const offsetMS = date.getTime() - convertedDate.getTime();
51
79
  return [convertedDate, offsetMS];
52
80
  }
53
- function getNextWorkingDate(date, workingHours) {
81
+ function getNextWorkingDate(date, schedule) {
54
82
  const currentDate = new Date(date);
55
83
  while (true) {
56
84
  const currentDay = getDayOfWeek(currentDate);
57
- const daySchedule = workingHours.find((interval) => interval.days.includes(currentDay));
58
- if (daySchedule) {
85
+ const daySchedule = schedule.workingHours.find((interval) => interval.days.includes(currentDay));
86
+ const holiday = schedule.holidays && isHoliday(currentDate, schedule);
87
+ if (daySchedule && !holiday) {
59
88
  const [fromHours, fromMinutes] = daySchedule.from.split(':').map(Number);
60
89
  const [toHours, toMinutes] = daySchedule.to.split(':').map(Number);
61
90
  if (fromHours > toHours) {
@@ -88,13 +117,11 @@ function getDayOfWeek(date) {
88
117
  ];
89
118
  return days[date.getDay()];
90
119
  }
91
- function isHoliday(date, holidays) {
92
- const dateString = date.toISOString().split('T')[0];
93
- return holidays.includes(dateString);
94
- }
95
- function skipHoliday(date, holidays) {
96
- while (isHoliday(date, holidays)) {
97
- date.setDate(date.getDate() + 1);
98
- }
99
- return date;
120
+ function isHoliday(date, schedule) {
121
+ if (!schedule.holidays)
122
+ return false;
123
+ const offset = date.getTimezoneOffset();
124
+ const utcDate = new Date(date.getTime() - offset * 60 * 1000);
125
+ const dateString = utcDate.toISOString().split('T')[0];
126
+ return schedule.holidays.includes(dateString);
100
127
  }
package/changelog.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ### 0.3.1 → 0.3.2 (unstable)
4
+
5
+ - add `ScheduleQuery` class that provides three additional capabilities compared to `delayFactory`:
6
+ - `query.nextValidDate(date: Date)` – to find next closest date after a specific date (rather than delay)
7
+ - `query.next()` – gets closest date within provided schedule
8
+ - `query.isValid(date)` – checks if provided date is within the schedule
9
+
3
10
  ### 0.3.0 → 0.3.1 (unstable)
4
11
 
5
12
  - Fix: allow other models (as a string)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recombine-ai/engine",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Recombine AI engine for creating conversational AI agents",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",