mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 11:42:29 +08:00
feat: add weekday calculator in time tool (#2822)
This commit is contained in:
parent
5cab2b711f
commit
19d3a56194
42
api/core/tools/provider/builtin/time/tools/weekday.py
Normal file
42
api/core/tools/provider/builtin/time/tools/weekday.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
import calendar
|
||||
from datetime import datetime
|
||||
from typing import Any, Union
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class WeekdayTool(BuiltinTool):
|
||||
def _invoke(self,
|
||||
user_id: str,
|
||||
tool_parameters: dict[str, Any],
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
Calculate the day of the week for a given date
|
||||
"""
|
||||
year = tool_parameters.get('year')
|
||||
month = tool_parameters.get('month')
|
||||
day = tool_parameters.get('day')
|
||||
|
||||
date_obj = self.convert_datetime(year, month, day)
|
||||
if not date_obj:
|
||||
return self.create_text_message(f'Invalid date: Year {year}, Month {month}, Day {day}.')
|
||||
|
||||
weekday_name = calendar.day_name[date_obj.weekday()]
|
||||
month_name = calendar.month_name[month]
|
||||
readable_date = f"{month_name} {date_obj.day}, {date_obj.year}"
|
||||
return self.create_text_message(f'{readable_date} is {weekday_name}.')
|
||||
|
||||
@staticmethod
|
||||
def convert_datetime(year, month, day) -> datetime | None:
|
||||
try:
|
||||
# allowed range in datetime module
|
||||
if not (year >= 1 and 1 <= month <= 12 and 1 <= day <= 31):
|
||||
return None
|
||||
|
||||
year = int(year)
|
||||
month = int(month)
|
||||
day = int(day)
|
||||
return datetime(year, month, day)
|
||||
except ValueError:
|
||||
return None
|
42
api/core/tools/provider/builtin/time/tools/weekday.yaml
Normal file
42
api/core/tools/provider/builtin/time/tools/weekday.yaml
Normal file
|
@ -0,0 +1,42 @@
|
|||
identity:
|
||||
name: weekday
|
||||
author: Bowen Liang
|
||||
label:
|
||||
en_US: Weekday Calculator
|
||||
zh_Hans: 星期几计算器
|
||||
description:
|
||||
human:
|
||||
en_US: A tool for calculating the weekday of a given date.
|
||||
zh_Hans: 计算指定日期为星期几的工具。
|
||||
llm: A tool for calculating the weekday of a given date by year, month and day.
|
||||
parameters:
|
||||
- name: year
|
||||
type: number
|
||||
required: true
|
||||
form: llm
|
||||
label:
|
||||
en_US: Year
|
||||
zh_Hans: 年
|
||||
human_description:
|
||||
en_US: Year
|
||||
zh_Hans: 年
|
||||
- name: month
|
||||
type: number
|
||||
required: true
|
||||
form: llm
|
||||
label:
|
||||
en_US: Month
|
||||
zh_Hans: 月
|
||||
human_description:
|
||||
en_US: Month
|
||||
zh_Hans: 月
|
||||
- name: day
|
||||
type: number
|
||||
required: true
|
||||
form: llm
|
||||
label:
|
||||
en_US: day
|
||||
zh_Hans: 日
|
||||
human_description:
|
||||
en_US: day
|
||||
zh_Hans: 日
|
Loading…
Reference in New Issue
Block a user