mirror of
https://github.com/leon-ai/leon.git
synced 2025-01-08 10:47:45 +03:00
feat(skill/date_time): days countdown
This commit is contained in:
parent
8645ef01fd
commit
636eb0f139
@ -23,12 +23,26 @@
|
||||
"What is this week's number?",
|
||||
"What week of the year are we?"
|
||||
]
|
||||
},
|
||||
"days_countdown": {
|
||||
"type": "logic",
|
||||
"utterance_samples": [
|
||||
"How many days left until 1st July?",
|
||||
"What is the number of days remaining until December 25th?",
|
||||
"In how many days will it be February 10th?"
|
||||
]
|
||||
}
|
||||
},
|
||||
"answers": {
|
||||
"current_date_time": [
|
||||
"It is %weekday%, %month% %day%, %year%, and it is %hours%:%minutes%:%seconds%."
|
||||
],
|
||||
"current_week_number": ["It is the %week_number% week of the year."]
|
||||
"current_week_number": ["It is the %week_number% week of the year."],
|
||||
"days_countdown": [
|
||||
"There are %days% days between %month1% %day1%, %year1% and %month2% %day2%, %year2%."
|
||||
],
|
||||
"days_countdown_error": [
|
||||
"I'm sorry, I didn't understand the date you said."
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import { leon } from '@sdk/leon'
|
||||
|
||||
import { format } from 'numerable'
|
||||
|
||||
const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24
|
||||
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
|
||||
|
||||
/**
|
||||
* Get the week number (1-52) for a given date.
|
||||
|
60
skills/utilities/date_time/src/actions/days_countdown.ts
Normal file
60
skills/utilities/date_time/src/actions/days_countdown.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import type {
|
||||
ActionFunction,
|
||||
ActionParams,
|
||||
BuiltInDateRangeEntity
|
||||
} from '@sdk/types'
|
||||
import { leon } from '@sdk/leon'
|
||||
|
||||
import { ONE_DAY_MILLISECONDS } from '../lib/constants'
|
||||
|
||||
const isBuiltInDateRangeEntity = (
|
||||
entity: ActionParams['current_entities'][number]
|
||||
): entity is BuiltInDateRangeEntity => {
|
||||
return entity.entity === 'daterange'
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the number of days between two dates.
|
||||
* @example daysBetween(new Date(2020, 0, 1), new Date(2020, 0, 1)) // 0
|
||||
* @example daysBetween(new Date(2020, 0, 1), new Date(2020, 0, 2)) // 1
|
||||
*/
|
||||
const daysBetween = (date1: Date, date2: Date): number => {
|
||||
const differenceMilliseconds = Math.abs(date1.getTime() - date2.getTime())
|
||||
return Math.round(differenceMilliseconds / ONE_DAY_MILLISECONDS)
|
||||
}
|
||||
|
||||
export const run: ActionFunction = async function (params) {
|
||||
const { current_entities } = params
|
||||
|
||||
let futureDateString: string | null = null
|
||||
|
||||
for (const entity of current_entities) {
|
||||
if (isBuiltInDateRangeEntity(entity)) {
|
||||
const { futureEndDate } = entity.resolution
|
||||
futureDateString = futureEndDate
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (futureDateString == null) {
|
||||
return await leon.answer({
|
||||
key: 'days_countdown_error'
|
||||
})
|
||||
}
|
||||
|
||||
const currentDate = new Date()
|
||||
const futureDate = new Date(futureDateString)
|
||||
const daysCountdown = daysBetween(currentDate, futureDate)
|
||||
await leon.answer({
|
||||
key: 'days_countdown',
|
||||
data: {
|
||||
days: daysCountdown,
|
||||
month1: currentDate.toLocaleString(params.lang, { month: 'long' }),
|
||||
day1: currentDate.getDate(),
|
||||
year1: currentDate.getFullYear(),
|
||||
month2: futureDate.toLocaleString(params.lang, { month: 'long' }),
|
||||
day2: futureDate.getDate(),
|
||||
year2: futureDate.getFullYear()
|
||||
}
|
||||
})
|
||||
}
|
1
skills/utilities/date_time/src/lib/constants.ts
Normal file
1
skills/utilities/date_time/src/lib/constants.ts
Normal file
@ -0,0 +1 @@
|
||||
export const ONE_DAY_MILLISECONDS = 1_000 * 60 * 60 * 24
|
Loading…
Reference in New Issue
Block a user